zephyr/drivers/timer
Nicolas Pitre 11a2107d99 riscv: timer: driver revamp
Several issues:

- `last_count` should not be updated with current time or this will
  cause a time drift and induce jitter due to IRQ servicing latency.

- `sys_clock_set_timeout()` should not base its `mtime` on the current
  time either. Tracking the `last_tick` and `last_elapsed` values avoids
  the need for all the tick rounding computation.

- The MIN_DELAY thing is pointless. If the delay gets close or even behind
  current time then the IRQ will be triggered right away. This is unlikely
  to happen very often anyway so the constant overhead is uncalled for.

- Runtime 64-bits divisions on 32-bits hardware are very expensive.

Fix the above, and improve the following:

- Prime the accounting by simply invoking the IRQ handler from the init
  code. That will make the "ticks since boot" counter right.

- Remove excessive casts, especially a few wrong ones.

- Simplify the code overall.

Here's the output from the timer_jitter_drift test.

Before this patch:

|timer clock rate 60000000, kernel tick rate 10000
|period duration statistics for 10000 samples (0 rollovers):
|  expected: 1000 us,            60000.000000 cycles
|  min:      907.600000 us,      54456 cycles
|  max:      1099.750000 us,     65985 cycles
|  mean:     1008.594633 us,     60515.678000 cycles
|  variance: 2.184205 us,        7863.136316 cycles
|  stddev:   1.477906 us,        88.674332 cycles
|timer start cycle 995589, end cycle 606152369,
|total time 10085946.333333 us, expected time 10000000.000000 us,
|expected time drift 0.000000 us, difference 85946.333333 us

After this patch:

|timer clock rate 60000000, kernel tick rate 10000
|period duration statistics for 10000 samples (0 rollovers):
|  expected: 1000 us,            60000.000000 cycles
|  min:      992.116667 us,      59527 cycles
|  max:      1030.366667 us,     61822 cycles
|  mean:     1000.001902 us,     60000.114100 cycles
|  variance: 0.105334 us,        379.201081 cycles
|  stddev:   0.324551 us,        19.473087 cycles
|timer start cycle 987431, end cycle 600988572,
|total time 10000019.016667 us, expected time 10000000.000000 us,
|expected time drift 0.000000 us, difference 19.016667 us

The mean, variance and standard deviation number differences speak for
themselves, even in the absence of competing ISRs and/or IRQ-disabled
periods which would have made the comparison even worse.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
2023-02-21 18:04:55 -05:00
..
altera_avalon_timer_hal.c include: add missing zephyr/irq.h include 2022-10-17 22:57:39 +09:00
apic_timer.c include: add missing zephyr/irq.h include 2022-10-17 22:57:39 +09:00
apic_tsc.c include: add missing zephyr/irq.h include 2022-10-17 22:57:39 +09:00
arcv2_timer0.c smp: Convert #if to use CONFIG_MP_MAX_NUM_CPUS 2022-10-20 22:04:10 +09:00
arm_arch_timer.c include: add missing irq.h include 2022-10-11 18:05:17 +02:00
cc13x2_cc26x2_rtc_timer.c include: add missing spinlock.h include 2022-10-11 18:05:17 +02:00
CMakeLists.txt intel_adsp: Unify cavs and ace timers 2022-07-06 15:11:07 -04:00
cortex_m_systick.c cortex-m: warnings: Address -Wextra warnings 2023-02-19 20:58:44 -05:00
esp32c3_sys_timer.c driver: systimer: increase esp32c3 tick resolution 2023-01-04 14:24:25 +01:00
hpet.c timer: hpet: Remove deadcode 2023-01-09 12:07:28 -05:00
intel_adsp_timer.c include: add missing zephyr/irq.h include 2022-10-17 22:57:39 +09:00
ite_it8xxx2_timer.c include: add missing zephyr/irq.h include 2022-10-17 22:57:39 +09:00
Kconfig drivers: timers: Add dependency on SYS_CLOCK_EXISTS to all timer configs 2023-01-20 20:02:20 +01:00
Kconfig.altera_avalon drivers: timer: split Kconfig 2021-12-04 07:34:53 -05:00
Kconfig.apic timer: APIC timer count is a lock free accessor 2022-10-18 14:14:12 +02:00
Kconfig.arcv2 drivers: timer: split Kconfig 2021-12-04 07:34:53 -05:00
Kconfig.arm_arch drivers: timer: split Kconfig 2021-12-04 07:34:53 -05:00
Kconfig.cavs drivers: timer: intel_adsp: Update driver to use dts Kconfig symbol 2022-11-01 09:03:17 +00:00
Kconfig.cc13x2_cc26x2_rtc drivers: timer: Update drivers to use devicetree Kconfig symbol 2022-09-09 09:58:48 +00:00
Kconfig.cortex_m_systick drivers: timer: Update drivers to use devicetree Kconfig symbol 2022-09-09 09:58:48 +00:00
Kconfig.esp32c3_sys drivers: timer: split Kconfig 2021-12-04 07:34:53 -05:00
Kconfig.hpet timer: Revert "timer: HPET is also a lock free readable timer" 2022-11-01 23:15:23 +09:00
Kconfig.ite_it8xxx2 drivers: timer: Update drivers to use devicetree Kconfig symbol 2022-09-09 09:58:48 +00:00
Kconfig.leon_gptimer drivers: timer: Update drivers to use devicetree Kconfig symbol 2022-09-09 09:58:48 +00:00
Kconfig.litex drivers: timer: Update drivers to use devicetree Kconfig symbol 2022-09-09 09:58:48 +00:00
Kconfig.mchp_xec_rtos drivers: timer: Update drivers to use devicetree Kconfig symbol 2022-09-09 09:58:48 +00:00
Kconfig.mcux_gpt soc: arm: nxp: switch imxrt boards to use systick timer unless CONFIG_PM=y 2022-12-16 09:31:48 +01:00
Kconfig.mcux_lptmr drivers: timer: mcux: lptmr: add dependency on CONFIG_PM 2023-01-31 22:10:28 -06:00
Kconfig.mcux_os drivers: timer: Update drivers to use devicetree Kconfig symbol 2022-09-09 09:58:48 +00:00
Kconfig.mips_cp0 timer: Add tickless support for the MIPS CP0 timer 2022-01-19 13:48:21 -05:00
Kconfig.native_posix drivers: timer: improve sys_timer_disable usage 2021-12-04 07:34:53 -05:00
Kconfig.npcx_itim drivers: timer: Update drivers to use devicetree Kconfig symbol 2022-09-09 09:58:48 +00:00
Kconfig.nrf_rtc drivers: tiemr: nrf_rtc_timer: Add test function for shifting time 2022-11-17 11:24:23 +01:00
Kconfig.rcar_cmt drivers: timer: Update drivers to use devicetree Kconfig symbol 2022-09-09 09:58:48 +00:00
Kconfig.riscv_machine drivers: timer: Machine timer driver enablement for NIOSV 2023-02-20 09:29:13 -05:00
Kconfig.rv32m1_lptmr drivers: timer: Update drivers to use devicetree Kconfig symbol 2022-09-09 09:58:48 +00:00
Kconfig.sam0_rtc drivers: timer: Update drivers to use devicetree Kconfig symbol 2022-09-09 09:58:48 +00:00
Kconfig.stm32_lptim drivers: timer: stm32_lptim: Change clock source config check sanction 2022-09-21 18:39:07 +00:00
Kconfig.xlnx_psttc drivers: timer: Update drivers to use devicetree Kconfig symbol 2022-09-09 09:58:48 +00:00
Kconfig.xtensa drivers/xtensa_sys_timer: Change default timer ID 2022-02-21 22:10:03 -05:00
leon_gptimer.c include: add missing irq.h include 2022-10-11 18:05:17 +02:00
litex_timer.c drivers: use unsigned int for irq_lock() 2022-07-14 14:37:13 -05:00
mchp_xec_rtos_timer.c include: add missing irq.h include 2022-10-11 18:05:17 +02:00
mcux_gpt_timer.c include: add missing irq.h include 2022-10-11 18:05:17 +02:00
mcux_lptmr_timer.c include: add missing zephyr/irq.h include 2022-10-17 22:57:39 +09:00
mcux_os_timer.c timer: nxp: Update OS-Timer to use wakeup-source flag 2022-10-27 11:03:13 +02:00
mips_cp0_timer.c include: add missing limits.h include 2022-10-11 18:05:17 +02:00
native_posix_timer.c include: add more missing zephyr/ prefixes 2022-05-27 15:20:27 -07:00
npcx_itim_timer.c include: add missing zephyr/irq.h include 2022-10-17 22:57:39 +09:00
nrf_rtc_timer.c drivers: nrf_rtc_timer: Fix handling of COMPARE events in set_alarm() 2023-02-14 08:28:28 +01:00
rcar_cmt_timer.c include: add missing irq.h include 2022-10-11 18:05:17 +02:00
riscv_machine_timer.c riscv: timer: driver revamp 2023-02-21 18:04:55 -05:00
rv32m1_lptmr_timer.c include: add missing zephyr/irq.h include 2022-10-17 22:57:39 +09:00
sam0_rtc_timer.c include: add missing zephyr/irq.h include 2022-10-17 22:57:39 +09:00
stm32_lptim_timer.c drivers: timer: stm32u5 lptimer waits for DIER complete 2023-02-09 13:17:03 +01:00
sys_clock_init.c drivers: migrate includes to <zephyr/...> 2022-05-06 19:58:21 +02:00
xlnx_psttc_timer.c include: add missing sys_clock.h include 2022-10-11 18:05:17 +02:00
xlnx_psttc_timer_priv.h drivers: timer: xlnx_psttc_timer: Implement tickless support 2020-03-21 14:20:17 -04:00
xtensa_sys_timer.c include: add missing zephyr/irq.h include 2022-10-17 22:57:39 +09:00