PatchworkOS
Loading...
Searching...
No Matches

High Precision Event Timer. More...

Data Structures

struct  hpet_t
 High Precision Event Timer structure. More...
 

Macros

#define HPET_CAP_COUNTER_CLK_PERIOD_SHIFT   32
 The bit offset of the clock period in the capabilities register.
 
#define HPET_CONF_ENABLE_CNF_BIT   (1 << 0)
 The bit to set to enable the HPET in the configuration register.
 
#define HPET_CONF_LEG_RT_CNF_BIT   (1 << 1)
 The bit to set to enable legacy replacement mode in the configuration register.
 
#define HPET_ADDRESS_SPACE_MEMORY   0
 If hpet_t::addressSpaceId is equal to this, the address is in system memory space.
 
#define HPET_ADDRESS_SPACE_IO   1
 If hpet_t::addressSpaceId is equal to this, the address is in system I/O space.
 
#define HPET_FEMTOSECONDS_PER_SECOND   1000000000000000ULL
 The number of femtoseconds in one second.
 

Enumerations

enum  hpet_register_t {
  HPET_REG_GENERAL_CAPABILITIES_ID = 0x000 ,
  HPET_REG_GENERAL_CONFIG = 0x010 ,
  HPET_REG_GENERAL_INTERRUPT = 0x020 ,
  HPET_REG_MAIN_COUNTER_VALUE = 0x0F0 ,
  HPET_REG_TIMER0_CONFIG_CAP = 0x100 ,
  HPET_REG_TIMER0_COMPARATOR = 0x108
}
 HPET register offsets. More...
 

Functions

clock_t hpet_nanoseconds_per_tick (void)
 Retrieve the number of nanoseconds per HPET tick.
 
uint64_t hpet_read_counter (void)
 Read the current value of the HPET main counter.
 
void hpet_reset_counter (void)
 Reset the HPET main counter to 0 and enable the HPET.
 
void hpet_write (uint64_t reg, uint64_t value)
 Write a value to an HPET register.
 
uint64_t hpet_read (uint64_t reg)
 Read a value from an HPET register.
 
void hpet_wait (clock_t nanoseconds)
 Wait for a specified number of nanoseconds using the HPET.
 

Detailed Description

High Precision Event Timer.

The HPET is initalized via the ACPI sdt registration system.

See also
OSDev HPET

Macro Definition Documentation

◆ HPET_ADDRESS_SPACE_IO

#define HPET_ADDRESS_SPACE_IO   1

If hpet_t::addressSpaceId is equal to this, the address is in system I/O space.

Definition at line 56 of file hpet.h.

◆ HPET_ADDRESS_SPACE_MEMORY

#define HPET_ADDRESS_SPACE_MEMORY   0

If hpet_t::addressSpaceId is equal to this, the address is in system memory space.

Definition at line 51 of file hpet.h.

◆ HPET_CAP_COUNTER_CLK_PERIOD_SHIFT

#define HPET_CAP_COUNTER_CLK_PERIOD_SHIFT   32

The bit offset of the clock period in the capabilities register.

Definition at line 36 of file hpet.h.

◆ HPET_CONF_ENABLE_CNF_BIT

#define HPET_CONF_ENABLE_CNF_BIT   (1 << 0)

The bit to set to enable the HPET in the configuration register.

Definition at line 41 of file hpet.h.

◆ HPET_CONF_LEG_RT_CNF_BIT

#define HPET_CONF_LEG_RT_CNF_BIT   (1 << 1)

The bit to set to enable legacy replacement mode in the configuration register.

Definition at line 46 of file hpet.h.

◆ HPET_FEMTOSECONDS_PER_SECOND

#define HPET_FEMTOSECONDS_PER_SECOND   1000000000000000ULL

The number of femtoseconds in one second.

Definition at line 61 of file hpet.h.

Enumeration Type Documentation

◆ hpet_register_t

HPET register offsets.

Enumerator
HPET_REG_GENERAL_CAPABILITIES_ID 
HPET_REG_GENERAL_CONFIG 
HPET_REG_GENERAL_INTERRUPT 
HPET_REG_MAIN_COUNTER_VALUE 
HPET_REG_TIMER0_CONFIG_CAP 
HPET_REG_TIMER0_COMPARATOR 

Definition at line 23 of file hpet.h.

Function Documentation

◆ hpet_nanoseconds_per_tick()

clock_t hpet_nanoseconds_per_tick ( void  )

Retrieve the number of nanoseconds per HPET tick.

If the HPET is not initialized, this function will return 0.

Returns
Nanoseconds per tick

Definition at line 57 of file hpet.c.

References CLOCKS_PER_SEC, HPET_FEMTOSECONDS_PER_SECOND, isInitialized, and period.

Referenced by timer_accumulate(), and timer_uptime().

◆ hpet_read()

uint64_t hpet_read ( uint64_t  reg)

Read a value from an HPET register.

Parameters
regThe register to read from
Returns
The value read from the register

Definition at line 95 of file hpet.c.

References address, isInitialized, NULL, panic(), and READ_64.

Referenced by hpet_init(), and hpet_read_counter().

◆ hpet_read_counter()

uint64_t hpet_read_counter ( void  )

Read the current value of the HPET main counter.

If the HPET is not initialized, this function will return 0.

Returns
Current current value of the HPET main counter in ticks

Definition at line 66 of file hpet.c.

References hpet_read(), HPET_REG_MAIN_COUNTER_VALUE, and isInitialized.

Referenced by hpet_wait(), timer_accumulate(), and timer_uptime().

◆ hpet_reset_counter()

void hpet_reset_counter ( void  )

Reset the HPET main counter to 0 and enable the HPET.

If the HPET is not initialized, this function does nothing.

Definition at line 75 of file hpet.c.

References HPET_CONF_ENABLE_CNF_BIT, HPET_REG_GENERAL_CONFIG, HPET_REG_MAIN_COUNTER_VALUE, hpet_write(), and isInitialized.

Referenced by hpet_init(), and timer_accumulate().

◆ hpet_wait()

void hpet_wait ( clock_t  nanoseconds)

Wait for a specified number of nanoseconds using the HPET.

This function uses a busy-wait loop, meaning its very CPU inefficient, but its usefull during early initialization or when you are unable to block the current thread.

Parameters
nanosecondsThe number of nanoseconds to wait

Definition at line 104 of file hpet.c.

References hpet_read_counter(), isInitialized, NULL, panic(), period, and start().

Referenced by apic_timer_ticks_per_ns(), ps2_device_init(), ps2_drain(), trampoline_send_startup_ipi(), and trampoline_wait_ready().

◆ hpet_write()

void hpet_write ( uint64_t  reg,
uint64_t  value 
)

Write a value to an HPET register.

Parameters
regThe register to write to
valueThe value to write

Definition at line 86 of file hpet.c.

References address, isInitialized, NULL, panic(), and WRITE_64.

Referenced by hpet_reset_counter().