Skip to content

timer/hpet: many bug fixes and performance improvements + sdfgen bumped to 0.29.0#653

Open
dreamliner787-9 wants to merge 11 commits intomainfrom
hpet_fixes_rebased
Open

timer/hpet: many bug fixes and performance improvements + sdfgen bumped to 0.29.0#653
dreamliner787-9 wants to merge 11 commits intomainfrom
hpet_fixes_rebased

Conversation

@dreamliner787-9
Copy link
Copy Markdown
Contributor

@dreamliner787-9 dreamliner787-9 commented Mar 4, 2026

drivers/timer/hpet:

  • Greatly reduced timestamp read latency by serving it from the TSC (Time Stamp Counter) rather than the HPET's main counter. As on a real machine, the HPET is not on the same silicon as the processor, so any accesses to it must cross a bus. Thus it is faster if we take the wall clock time from the TSC instead which is local to the processor. On a 3.3GHz XEON W-1250 Comet Lake CPU, previously the driver takes ~25,000 cycles to read the time, now it just takes ~1,800 cycles.
  • Renamed from hpet to tsc_hpet.
  • Removed unused constants.
  • Switched from Message Signalled Interrupt (MSI) to legacy I/O APIC based interrupt routing. As not all HPET implementations support MSI (e.g. QEMU), and each individual machine will have different permitted I/O APIC routing for the HPET. While legacy routing guarantees that comparator 0's IRQ is always wired to I/O APIC pin 2. Which keep things simple across different machines.
  • Made timeout calculation more accurate by doing all math in-terms of ticks rather than nanoseconds. To avoid needless conversions between ticks and nanoseconds.
  • Fixed potential overflow when converting from large nanosecond timeouts to ticks by using 128-bit wide integer math.
  • Stop, reset the main counter and don't start it until we have finished initialising the comparator. Rather than leaving it at whatever stale value that was there before.
  • Made all MMIO accesses volatile.

CI:

  • Bumped to sdfgen 0.29.0 to fix x86 IRQ trigger rendering in SDF.

tools:

  • Added support for "x86_64_generic_vtx" Microkit platform.

examples/*:

  • Bumped to sdfgen 0.29.0.

examples/timer:

  • Only print messages after setting a timeout. To reduce perceived timer drift on platform that take a long time to print to console, e.g. x86.
  • Added support for "x86_64_generic_vtx" Microkit platform.
  • Made the timer IRQ on x86 the highest priority IRQ and explained how it is derived.
  • Fixed typos in metaprogram.

examples/echo_server:

  • Made the timer IRQ on x86 the highest priority IRQ and explained how it is derived.

@dreamliner787-9 dreamliner787-9 force-pushed the hpet_fixes_rebased branch 3 times, most recently from b0075ed to f86010f Compare March 4, 2026 03:58
Comment on lines +104 to +106
__uint128_t counter_as_fs = (__uint128_t)ticks * (__uint128_t)tick_period_fs;
uint64_t rem = 0;
return udiv128by64to64(HIGH_WORD(counter_as_fs), LOW_WORD(counter_as_fs), 1000000, &rem);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like there's a better way to do this...

Also we have defines for e.g. NS_IN_S, no need to hardcode this constant.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although I guess we've already had the timer-conversion functions discussion, so maybe I should leave this as is for now.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The number is for converting femtoseconds in nanosecond, I've added a constant as you suggested.

@dreamliner787-9 dreamliner787-9 force-pushed the hpet_fixes_rebased branch 4 times, most recently from 3aa1b03 to b859521 Compare March 4, 2026 23:48
@dreamliner787-9 dreamliner787-9 changed the title timer/hpet: many bug fixes and improvements timer/hpet: many bug fixes and improvements Mar 5, 2026
@dreamliner787-9 dreamliner787-9 changed the title timer/hpet: many bug fixes and improvements timer/hpet: many bug fixes and improvements + sdfgen bumped to 0.29.0 Mar 5, 2026
@dreamliner787-9 dreamliner787-9 changed the title timer/hpet: many bug fixes and improvements + sdfgen bumped to 0.29.0 timer/hpet: many bug fixes and performance improvements + sdfgen bumped to 0.29.0 Mar 25, 2026
- Removed unused constants.

- Switched from Message Signalled Interrupt (MSI) to legacy I/O APIC
  based interrupt routing. As not all HPET implementations support MSI,
  and each individual machine will have different permitted I/O APIC
  routing for the HPET. While legacy routing guarantees that comparator
  0's IRQ is always wired to I/O APIC pin 2. Which keep things simple
  across different machines.

- Made timeout calculation more accurate by doing all math in-terms of
  ticks rather than nanoseconds. To avoid needless conversions between
  ticks and nanoseconds.

- Fixed potential overflow when converting from large nanosecond
  timeouts to ticks by using 128-bit wide integer math.

- Stop, reset the main counter and don't start it until we have finished
  initialising the comparator. Rather than leaving it at whatever stale
  value that was there before.

- Made all MMIO accesses volatile.

- Leave comparator 0's IRQ masked until the first timeout request to
  prevent IRQs before the driver go passive.

Signed-off-by: Bill Nguyen <bill.nguyen@unsw.edu.au>
Signed-off-by: Bill Nguyen <bill.nguyen@unsw.edu.au>
To reduce perceived timer drift on platform that
take a long time to print to console, e.g. x86.

Signed-off-by: Bill Nguyen <bill.nguyen@unsw.edu.au>
Signed-off-by: Bill Nguyen <bill.nguyen@unsw.edu.au>
- Added support for "x86_64_generic_vtx" Microkit platform.

- Bumped to sdfgen 0.29.0 to fix IRQ trigger rendering in SDF.

- Made the timer IRQ the highest priority IRQ and explained how it is
  derived.

- Fixed typos in metaprogram.

Signed-off-by: Bill Nguyen <bill.nguyen@unsw.edu.au>
Signed-off-by: Bill Nguyen <bill.nguyen@unsw.edu.au>
Signed-off-by: Bill Nguyen <bill.nguyen@unsw.edu.au>
Signed-off-by: Bill Nguyen <bill.nguyen@unsw.edu.au>
Signed-off-by: Bill Nguyen <bill.nguyen@unsw.edu.au>
Signed-off-by: Bill Nguyen <bill.nguyen@unsw.edu.au>
Signed-off-by: Bill Nguyen <bill.nguyen@unsw.edu.au>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants