Voxia OS v0.0.1
Hobby Project Operating System Targeting x86-64
Loading...
Searching...
No Matches
hpet.c
Go to the documentation of this file.
1#include "hal/acpi/acpi.h"
2#include "hal/cpu/cpuid.h"
3#include "libk/io.h"
4#include "libk/serial.h"
5#include <type.h>
6#include "type.h"
7#include <hal/acpi/hpet.h>
8#include <str.h>
9
10static volatile uintptr_t hpet_address = 0;
13
17
21
23 return mmio_inll((hpet_address + reg));
24}
25
28 cfg |= 1; // bit enable
30}
31
35
37 uint64_t min_tick = (hpet_read(HPET_GENERAL_CAP_ID) >> 32);
38 uint64_t ns_per_tick = min_tick / 1000000;
39 return ns_per_tick;
40}
41
42void hpet_level_timer_setup(uint32_t n, uint64_t tick_count, int irq) {
44 if ((cap & (1U << irq)) == 0) {
45 LOG2_ERROR("HPET", "invalid irq %d, available %b", irq, cap);
46 return;
47 }
48
50 (uint64_t) ((1 << 1) | (1 << 2) | (irq << 9) | (1 << 3)));
51 hpet_write(HPET_TIMER_COMPARATOR(n), tick_count);
52}
53
55 struct hpet table;
56 memcopy(&table, (void*) addr, sizeof(struct hpet));
57
58 LOG2_INFO("HPET", "signature %s", table.header.signature);
59 if (strncmp(table.header.signature, "HPET", 4) != 0)
60 return;
62 (uint64_t) (table.address.address & ~((uint64_t) 0x1000 - 1)),
63 2);
64 LOG2_INFO("HPET", "address 0x%x", hpet_address);
65
66 uint64_t min_tick = (hpet_read(HPET_GENERAL_CAP_ID) >> 32);
67 min_tick_ns = min_tick / 1000000;
68 LOG2_INFO("HPET", "minimum ticks : %lu fs (%d ns)", (uint64_t) min_tick,
70
71 uint64_t cap = hpet_read(0x0);
72 uint8_t num_timers = ((cap >> 8) & 0x1F) + 1;
73 LOG2_INFO("HPET", "num timers %d", num_timers);
74
76
80}
81
85
87 if (!hpet_available || min_tick_ns == 0) {
88 // Fallback to simple busy loop if HPET is not ready
89 for (volatile uint64_t i = 0; i < ns * 10; i++)
90 __asm__ volatile("pause");
91 return;
92 }
93 uint64_t ticks = ns / min_tick_ns;
95 while ((vxHPETGetMainCount() - start) < ticks)
96 ;
97}
uintptr_t acpi_map_phys_page(uintptr_t phys_addr, size_t len)
Definition acpi.c:55
madt_record_table_entry_t table[]
Definition acpi.h:3
volatile uint64_t addr
Definition e1000.hpp:0
volatile uint32_t cap
Definition ehci.hpp:2
void hpet_disable()
Definition hpet.c:32
boolean_t vxHPETIsAvailable()
Definition hpet.c:14
static volatile uintptr_t hpet_address
Definition hpet.c:10
void vxHPETInitialize(uintptr_t addr)
Definition hpet.c:54
void vxHPETSleep(uint64_t ns)
Definition hpet.c:86
void hpet_level_timer_setup(uint32_t n, uint64_t tick_count, int irq)
Definition hpet.c:42
void hpet_enable()
Definition hpet.c:26
uint64_t vxHPETGetMainCount()
Definition hpet.c:82
uint64_t vxHPETMinTickNs(void)
Definition hpet.c:36
static uint64_t min_tick_ns
Definition hpet.c:11
boolean_t hpet_available
Definition hpet.c:12
static void hpet_write(uint32_t reg, uint64_t value)
Definition hpet.c:18
static uint64_t hpet_read(uint32_t reg)
Definition hpet.c:22
#define HPET_MAIN_COUNT
Definition hpet.h:9
#define HPET_TIMER_COMPARATOR(N)
Definition hpet.h:11
#define HPET_GENERAL_CONFIG
Definition hpet.h:7
#define HPET_GENERAL_CAP_ID
Definition hpet.h:6
#define HPET_TIMER_CONFIG(N)
Definition hpet.h:10
cpu_register_t reg
Definition thread.h:14
static uint64_t mmio_inll(uintptr_t addr)
Definition io.h:63
static void mmio_outll(uintptr_t addr, uint64_t value)
Definition io.h:59
return value
Definition oct2bin.h:22
#define LOG2_ERROR(mod, fmt,...)
Definition serial.h:38
#define LOG2_INFO(mod, fmt,...)
Definition serial.h:33
int strncmp(const char *s1, const char *s2, size_t n)
void memcopy(void *dest, void *src, size_t size)
Definition hpet.h:36
unsigned int uint32_t
Definition type.h:19
uint8_t boolean_t
Definition type.h:89
unsigned long uintptr_t
Definition type.h:73
unsigned long uint64_t
Definition type.h:25
unsigned char uint8_t
Definition type.h:7