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
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
Kconfig.apic timer: APIC timer count is a lock free accessor 2022-10-18 14:14:12 +02:00
Kconfig.arcv2
Kconfig.arm_arch
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
Kconfig.cortex_m_systick
Kconfig.esp32c3_sys
Kconfig.hpet timer: Revert "timer: HPET is also a lock free readable timer" 2022-11-01 23:15:23 +09:00
Kconfig.ite_it8xxx2
Kconfig.leon_gptimer
Kconfig.litex
Kconfig.mchp_xec_rtos
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
Kconfig.mips_cp0
Kconfig.native_posix
Kconfig.npcx_itim
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
Kconfig.riscv_machine drivers: timer: Machine timer driver enablement for NIOSV 2023-02-20 09:29:13 -05:00
Kconfig.rv32m1_lptmr
Kconfig.sam0_rtc
Kconfig.stm32_lptim
Kconfig.xlnx_psttc
Kconfig.xtensa
leon_gptimer.c include: add missing irq.h include 2022-10-11 18:05:17 +02:00
litex_timer.c
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
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
xlnx_psttc_timer_priv.h
xlnx_psttc_timer.c include: add missing sys_clock.h include 2022-10-11 18:05:17 +02:00
xtensa_sys_timer.c include: add missing zephyr/irq.h include 2022-10-17 22:57:39 +09:00