From 0c75ed9fb14275603b478c5bbd96bcd4d3f982e6 Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Sun, 21 Sep 2025 01:02:53 -0400 Subject: kern: clkdev: Add callback to get elapsed time Add new clock device callback to get the elapsed time from the timer being initialized, in microseconds. Signed-off-by: Ian Moffett --- src/sys/arch/amd64/io/hpet.c | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) (limited to 'src/sys/arch') diff --git a/src/sys/arch/amd64/io/hpet.c b/src/sys/arch/amd64/io/hpet.c index 36f19a8..d733d9b 100644 --- a/src/sys/arch/amd64/io/hpet.c +++ b/src/sys/arch/amd64/io/hpet.c @@ -42,6 +42,9 @@ #include #include +#define FSEC_PER_SECOND 1000000000000000ULL +#define USEC_PER_SECOND 1000000ULL + #define HPET_REG_CAPS 0x00 #define HPET_GENERAL_CONFIG 0x10 #define HPET_REG_MAIN_COUNTER 0xF0 @@ -118,6 +121,23 @@ hpet_msleep(size_t ms) return hpet_sleep(ms, 1000000000000); } +/* + * Get time since init in usec + */ +static size_t +hpet_time_usec(void) +{ + uint64_t period, freq, caps; + uint64_t counter; + + caps = hpet_read(HPET_REG_CAPS); + period = CAP_CLK_PERIOD(caps); + freq = FSEC_PER_SECOND / period; + + counter = hpet_read(HPET_REG_MAIN_COUNTER); + return (counter * USEC_PER_SECOND) / freq; +} + /* * Initialize the HPET */ @@ -162,8 +182,9 @@ hpet_init(void) /* Initialize as clock device */ clkdev.name = "IA-PC HPET"; - clkdev.attr = CLKDEV_MSLEEP; + clkdev.attr = CLKDEV_MSLEEP | CLKDEV_GET_USEC; clkdev.msleep = hpet_msleep; + clkdev.get_time_usec = hpet_time_usec; if (clkdev_register(&clkdev) < 0) { printf("hpet_init: could not register clock device\n"); return -1; -- cgit v1.2.3