From 530a1338185ad57d909eca636334eb099ffab724 Mon Sep 17 00:00:00 2001 From: "Sergey Semyonov (Serge)" Date: Thu, 3 Sep 2015 14:47:08 +0000 Subject: [PATCH] Kernel: Initial HPET support git-svn-id: svn://kolibrios.org@5787 a494cfbc-eb01-0410-851d-a64ba20cac60 --- kernel/trunk/core/apic.inc | 43 ++++++++++++++++++--- kernel/trunk/core/exports.inc | 1 + kernel/trunk/init.inc | 72 ++++++++++++++++++++++++++++++++++- kernel/trunk/kernel.asm | 18 +++++++++ 4 files changed, 127 insertions(+), 7 deletions(-) diff --git a/kernel/trunk/core/apic.inc b/kernel/trunk/core/apic.inc index e2983e3734..ef00f1429c 100644 --- a/kernel/trunk/core/apic.inc +++ b/kernel/trunk/core/apic.inc @@ -28,15 +28,15 @@ APIC_ISR equ 0x100 APIC_ESR equ 0x280 APIC_ICRL equ 0x300 APIC_ICRH equ 0x310 -APIC_LVT_LINT0 equ 0x350 -APIC_LVT_LINT1 equ 0x360 +APIC_LVT_LINT0 equ 0x350 +APIC_LVT_LINT1 equ 0x360 APIC_LVT_err equ 0x370 ; APIC timer -APIC_LVT_timer equ 0x320 -APIC_timer_div equ 0x3e0 -APIC_timer_init equ 0x380 -APIC_timer_cur equ 0x390 +APIC_LVT_timer equ 0x320 +APIC_timer_div equ 0x3e0 +APIC_timer_init equ 0x380 +APIC_timer_cur equ 0x390 ; IOAPIC IOAPIC_ID equ 0x0 IOAPIC_VER equ 0x1 @@ -439,7 +439,38 @@ pci_irq_fixup: pop ebp ret +align 4 +get_clock_ns: + mov eax, [hpet_base] + test eax, eax + jz .old_tics + push ebx + pushfd + cli + mov ebx, eax +@@: + mov edx, [ebx+0xF4] + mov eax, [ebx+0xF0] + mov ecx, [ebx+0xF4] + cmp ecx, edx + jnz @B + + mov ecx, [hpet_period] + mov ebx, edx + imul ebx, ecx + mul ecx + add edx, ebx + + popfd + pop ebx + ret + +.old_tics: + mov eax, [timer_ticks] + mov edx, 10000000 + mul edx + ret diff --git a/kernel/trunk/core/exports.inc b/kernel/trunk/core/exports.inc index 17625dc15d..672fc4417b 100644 --- a/kernel/trunk/core/exports.inc +++ b/kernel/trunk/core/exports.inc @@ -75,6 +75,7 @@ __exports: unmap_pages, 'UnmapPages', \ ; eax, ecx sys_msg_board_str, 'SysMsgBoardStr', \ sys_msg_board, 'SysMsgBoard', \ + get_clock_ns, 'GetClockNs', \ ;retval edx:eax 64-bit value get_timer_ticks, 'GetTimerTicks', \ get_stack_base, 'GetStackBase', \ delay_hs, 'Delay', \ ; ebx diff --git a/kernel/trunk/init.inc b/kernel/trunk/init.inc index 6c72d2a94a..de3b09cc43 100644 --- a/kernel/trunk/init.inc +++ b/kernel/trunk/init.inc @@ -435,6 +435,10 @@ acpi_dsdt_base rd 1 acpi_dsdt_size rd 1 acpi_madt_base rd 1 acpi_ioapic_base rd 1 +acpi_hpet_base rd 1 +hpet_base rd 1 +hpet_period rd 1 +hpet_timers rd 1 cpu_count rd 1 smpt rd 16 @@ -443,6 +447,8 @@ endg ACPI_HI_RSDP_WINDOW_START equ 0x000E0000 ACPI_HI_RSDP_WINDOW_END equ 0x00100000 ACPI_RSDP_CHECKSUM_LENGTH equ 20 + +ACPI_HPET_SIGN equ 0x54455048 ACPI_MADT_SIGN equ 0x43495041 ACPI_FADT_SIGN equ 0x50434146 @@ -516,7 +522,7 @@ check_acpi: jz .done mov ecx, [eax+16] - mov edx, 0x50434146 + mov edx, ACPI_FADT_SIGN mov [acpi_rsdt_base-OS_BASE], ecx call rsdt_find mov [acpi_fadt_base-OS_BASE], eax @@ -527,7 +533,16 @@ check_acpi: mov [acpi_dsdt_base-OS_BASE], eax mov eax, [eax+4] mov [acpi_dsdt_size-OS_BASE], eax +@@: + mov edx, ACPI_HPET_SIGN + mov ecx, [acpi_rsdt_base-OS_BASE] + call rsdt_find + test eax, eax + jz @F + mov [acpi_hpet_base-OS_BASE], eax + mov eax, [eax+44] + mov [hpet_base-OS_BASE], eax @@: mov edx, ACPI_MADT_SIGN mov ecx, [acpi_rsdt_base-OS_BASE] @@ -583,3 +598,58 @@ check_acpi: mov eax, [edx+4] mov [acpi_ioapic_base-OS_BASE], eax jmp .next + +HPET_PERIOD equ 0x0004 +HPET_CFG_ENABLE equ 0x0001 +HPET_CFG equ 0x0010 +HPET_COUNTER equ 0x00f0 +HPET_T0_CFG equ 0x0100 + +HPET_TN_LEVEL equ 0x0002 +HPET_TN_ENABLE equ 0x0004 +HPET_TN_FSB equ 0x4000 + +align 4 +init_hpet: + mov ebx, [hpet_base-OS_BASE] + test ebx, ebx + jz @F + + mov eax, [ebx] + and ah, 0x1F + inc ah + movzx eax, ah + mov [hpet_timers-OS_BASE], eax + mov ecx, eax + + mov eax, [ebx+HPET_PERIOD] + mov edx, 0x431BDE83 + mul edx + shr edx, 18 + mov [hpet_period-OS_BASE], edx + + mov esi, [ebx+HPET_CFG] + and esi, not HPET_CFG_ENABLE + mov [ebx+HPET_CFG], esi ;stop main counter + + lea edx, [ebx+HPET_T0_CFG] +@@: + jcxz @F + mov eax, [edx] + and eax, not (HPET_TN_ENABLE+HPET_TN_LEVEL+HPET_TN_FSB) + mov [edx], eax + add edx, 0x20 + dec ecx + jmp @B +@@: + mov [ebx+HPET_COUNTER], ecx ;reset main counter + mov [ebx+HPET_COUNTER+4], ecx + + or esi, HPET_CFG_ENABLE + mov [ebx+HPET_CFG], esi ;and start again +@@: + ret + + + + diff --git a/kernel/trunk/kernel.asm b/kernel/trunk/kernel.asm index 61c9b4699f..854a734a94 100644 --- a/kernel/trunk/kernel.asm +++ b/kernel/trunk/kernel.asm @@ -283,6 +283,7 @@ B32: bts [cpu_caps-OS_BASE], CAPS_TSC ;force use rdtsc call check_acpi + call init_hpet call init_BIOS32 ; MEMORY MODEL call mem_test @@ -697,6 +698,23 @@ setvideomode: @@: mov [clipboard_main_list], eax + mov eax, [hpet_base] + test eax, eax + jz @F + DEBUGF 1, "K : HPET base %x\n", eax + mov eax, [hpet_period] + DEBUGF 1, "K : HPET period %d\n", eax + mov eax, [hpet_timers] + DEBUGF 1, "K : HPET timers %d\n", eax + + mov eax, [hpet_base] + stdcall map_io_mem, [hpet_base], 1024, PG_GLOBAL+PAT_UC+PG_SWR + mov [hpet_base], eax + + mov eax, [eax] + DEBUGF 1, "K : HPET caps %x\n", eax + +@@: ; SET UP OS TASK mov esi, boot_setostask