PatchworkOS
Loading...
Searching...
No Matches
hpet.c
Go to the documentation of this file.
2
4#include <kernel/log/log.h>
5#include <kernel/log/panic.h>
6#include <kernel/mem/vmm.h>
8
9#include <assert.h>
10
11static hpet_t* hpet;
13static uint64_t period; // Main counter tick period in femtoseconds (10^-15 s).
14
15static bool isInitialized = false;
16
18{
19 hpet = (hpet_t*)table;
20
22 {
23 LOG_ERR("HPET address space is not memory (id=%u) which is not supported\n", hpet->addressSpaceId);
24 return ERR;
25 }
26
29 NULL) == NULL)
30 {
31 LOG_ERR("failed to map HPET memory at 0x%016lx\n", hpet->address);
32 return ERR;
33 }
34
35 isInitialized = true;
36
39
40 if (period == 0 || period >= 0x05F5E100)
41 {
42 LOG_ERR("HPET reported an invalid counter period %llu fs\n", period);
43 isInitialized = false;
44 return ERR;
45 }
46
47 LOG_INFO("started HPET timer phys=0x%016lx virt=0x%016lx period=%lluns timers=%u %s-bit\n", hpet->address, address,
49 hpet->counterIs64Bit ? "64" : "32");
50
52 return 0;
53}
54
56
58{
59 if (!isInitialized)
60 {
61 return 0;
62 }
64}
65
67{
68 if (!isInitialized)
69 {
70 return 0;
71 }
73}
74
85
87{
88 if (!isInitialized)
89 {
90 panic(NULL, "HPET not initialized");
91 }
92 WRITE_64(address + reg, value);
93}
94
96{
97 if (!isInitialized)
98 {
99 panic(NULL, "HPET not initialized");
100 }
101 return READ_64(address + reg);
102}
103
104void hpet_wait(clock_t nanoseconds)
105{
106 if (!isInitialized)
107 {
108 panic(NULL, "HPET not initialized");
109 }
110
111 if (nanoseconds == 0)
112 {
113 return;
114 }
115
116 uint64_t ticks = (nanoseconds * 1000000) / period;
118 while (hpet_read_counter() < start + ticks)
119 {
120 asm volatile("pause");
121 }
122}
#define CLOCKS_PER_SEC
Definition clock_t.h:15
#define ACPI_SDT_HANDLER_REGISTER(sig, initHandler)
Macro to register an ACPI SDT handler.
Definition tables.h:276
void hpet_write(uint64_t reg, uint64_t value)
Write a value to an HPET register.
Definition hpet.c:86
#define HPET_CONF_ENABLE_CNF_BIT
The bit to set to enable the HPET in the configuration register.
Definition hpet.h:41
void hpet_wait(clock_t nanoseconds)
Wait for a specified number of nanoseconds using the HPET.
Definition hpet.c:104
#define HPET_FEMTOSECONDS_PER_SECOND
The number of femtoseconds in one second.
Definition hpet.h:61
#define HPET_CAP_COUNTER_CLK_PERIOD_SHIFT
The bit offset of the clock period in the capabilities register.
Definition hpet.h:36
clock_t hpet_nanoseconds_per_tick(void)
Retrieve the number of nanoseconds per HPET tick.
Definition hpet.c:57
void hpet_reset_counter(void)
Reset the HPET main counter to 0 and enable the HPET.
Definition hpet.c:75
uint64_t hpet_read(uint64_t reg)
Read a value from an HPET register.
Definition hpet.c:95
#define HPET_ADDRESS_SPACE_MEMORY
If hpet_t::addressSpaceId is equal to this, the address is in system memory space.
Definition hpet.h:51
uint64_t hpet_read_counter(void)
Read the current value of the HPET main counter.
Definition hpet.c:66
@ HPET_REG_GENERAL_CONFIG
Definition hpet.h:26
@ HPET_REG_GENERAL_CAPABILITIES_ID
Definition hpet.h:25
@ HPET_REG_MAIN_COUNTER_VALUE
Definition hpet.h:28
NORETURN void panic(const interrupt_frame_t *frame, const char *format,...)
Panic the kernel, printing a message and halting.
Definition panic.c:362
#define LOG_ERR(format,...)
Definition log.h:89
#define LOG_INFO(format,...)
Definition log.h:87
#define PML_LOWER_TO_HIGHER(addr)
Converts an address from the lower half to the higher half.
@ PML_PRESENT
@ PML_WRITE
@ PML_GLOBAL
void * vmm_map(space_t *space, void *virtAddr, void *physAddr, uint64_t length, pml_flags_t flags, space_callback_func_t func, void *private)
Maps physical memory to virtual memory in a given address space.
Definition vmm.c:231
#define PAGE_SIZE
Memory page size.
Definition proc.h:140
#define NULL
Pointer error value.
Definition NULL.h:23
#define ERR
Integer error value.
Definition ERR.h:17
__UINT64_TYPE__ clock_t
A nanosecond time.
Definition clock_t.h:13
static bool isInitialized
Definition hpet.c:15
static uint64_t period
Definition hpet.c:13
static hpet_t * hpet
Definition hpet.c:11
static uint64_t hpet_init(sdt_header_t *table)
Definition hpet.c:17
static uintptr_t address
Definition hpet.c:12
static void start()
Definition main.c:542
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
__UINTPTR_TYPE__ uintptr_t
Definition stdint.h:43
High Precision Event Timer structure.
Definition hpet.h:68
uint64_t address
Definition hpet.h:80
uint8_t addressSpaceId
Definition hpet.h:76
uint8_t counterIs64Bit
Definition hpet.h:72
uint8_t comparatorCount
Definition hpet.h:71
System Description Table Header.
Definition acpi.h:90
#define WRITE_64(address, value)
Definition utils.h:13
#define READ_64(address)
Definition utils.h:12