LCOV - code coverage report
Current view: top level - /home/ubuntu/zephyrproject/zephyr/drivers/timer - system_timer_generic.h (source / functions) Coverage Total Hit
Test: lcov.info Lines: 69.2 % 52 36
Test Date: 2026-11-06 14:43:19 Functions: 69.2 % 13 9
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 50.0 % 6 3

             Branch data     Line data    Source code
       1                 :             : /*
       2                 :             :  * Copyright (c) 2026 BayLibre SAS
       3                 :             :  *
       4                 :             :  * SPDX-License-Identifier: Apache-2.0
       5                 :             :  */
       6                 :             : 
       7                 :             : /**
       8                 :             :  * @file
       9                 :             :  * @brief Generic tickless system timer core
      10                 :             :  *
      11                 :             :  * Common code for tickless system timer drivers. The driver supplies a few
      12                 :             :  * hardware primitives (read the counter, arm the alarm) and this file does
      13                 :             :  * the rest: converting cycles to ticks, tracking what was last announced,
      14                 :             :  * computing deadlines and clamping them to what the hardware can reach.
      15                 :             :  *
      16                 :             :  * This header holds the actual sys_clock_set_timeout(), sys_clock_elapsed()
      17                 :             :  * and sys_clock_cycle_get_32/64() for the kernel, so include it once, from
      18                 :             :  * the driver's .c file, after the definitions listed below.
      19                 :             :  *
      20                 :             :  * The driver must define one backend:
      21                 :             :  *
      22                 :             :  *   TIMER_CORE_BACKEND_COMPARE_ORDERED
      23                 :             :  *      Free-running counter with a compare register that fires once the
      24                 :             :  *      counter reaches or passes it. A value already in the past fires
      25                 :             :  *      right away. Examples: arm_arch, riscv_machine.
      26                 :             :  *
      27                 :             :  *   TIMER_CORE_BACKEND_COMPARE_EXACT
      28                 :             :  *      Same, but the compare fires only on an exact match. A value written
      29                 :             :  *      too late is missed until the counter wraps, so the core verifies
      30                 :             :  *      each write (see timer_core_set_compare()). Examples: hpet, mips_cp0.
      31                 :             :  *
      32                 :             :  *   TIMER_CORE_BACKEND_RELOAD
      33                 :             :  *      A timer that fires after a given delay and then reloads by itself,
      34                 :             :  *      like a down-counter. On a ticking (not tickless) kernel, the driver
      35                 :             :  *      sets it to one tick at init and the core leaves it alone.
      36                 :             :  *
      37                 :             :  * If the alarm only works with its interrupt enabled, enable it in the arming
      38                 :             :  * function. The core doesn't track interrupt masking.
      39                 :             :  *
      40                 :             :  * The driver must also provide:
      41                 :             :  *
      42                 :             :  *   static uint32_t/uint64_t timer_driver_cycle_get(void)
      43                 :             :  *      Return the raw counter value, wraps and all. Give its width in
      44                 :             :  *      TIMER_CORE_COUNTER_WIDTH and the core handles the wraps. uint32_t for
      45                 :             :  *      up to 32 bits, uint64_t otherwise.
      46                 :             :  *
      47                 :             :  *   static void timer_driver_set_compare(uint32_t/uint64_t cycles)   (COMPARE)
      48                 :             :  *      Just write @p cycles to the compare register. Don't check whether it
      49                 :             :  *      is already in the past: depending on the backend, the hardware or the
      50                 :             :  *      core deals with that. A 32-bit compare register can take uint32_t.
      51                 :             :  *
      52                 :             :  *   static void timer_driver_set_reload(uint32_t/uint64_t cycles)   (RELOAD)
      53                 :             :  *      Fire the interrupt @p cycles from now. The core keeps @p cycles
      54                 :             :  *      between TIMER_CORE_ALARM_MIN_CYCLES and TIMER_CORE_ALARM_MAX_CYCLES.
      55                 :             :  *      uint32_t if TIMER_CORE_ALARM_MAX_CYCLES fits in 32 bits, uint64_t
      56                 :             :  *      otherwise.
      57                 :             :  *
      58                 :             :  * Optional settings:
      59                 :             :  *
      60                 :             :  *   TIMER_CORE_CYCLES_PER_SEC
      61                 :             :  *      Counter rate in Hz. Defaults to the kernel's hardware clock rate. Set
      62                 :             :  *      it if the counter runs at another rate, e.g. prescaled. The core
      63                 :             :  *      derives TIMER_CORE_CYC_PER_TICK from it; a driver can read that one
      64                 :             :  *      but must not define it.
      65                 :             :  *
      66                 :             :  *   TIMER_CORE_CYCLES_PER_SEC_RUNTIME
      67                 :             :  *      Define it when TIMER_CORE_CYCLES_PER_SEC is a variable, e.g. a rate
      68                 :             :  *      read from the clock controller. Give that variable its value before
      69                 :             :  *      calling timer_core_init().
      70                 :             :  *
      71                 :             :  *   TIMER_CORE_COUNTER_WIDTH
      72                 :             :  *      Counter width in bits, up to 64. Defaults to the CPU register width.
      73                 :             :  *      Set it for any other width, including a 64-bit counter on a 32-bit
      74                 :             :  *      CPU.
      75                 :             :  *
      76                 :             :  *   TIMER_CORE_COUNTER_NONMONOTONIC
      77                 :             :  *      The counter can briefly read lower than a value already seen (e.g. a
      78                 :             :  *      global timer under QEMU SMP). The core then treats a backwards read
      79                 :             :  *      as no time passed rather than a huge jump.
      80                 :             :  *
      81                 :             :  *   TIMER_CORE_COUNTER_NONATOMIC
      82                 :             :  *      timer_driver_cycle_get() isn't a single register read but a count
      83                 :             :  *      built in software, from state the ISR also updates. The core then
      84                 :             :  *      always reads it under the clock lock.
      85                 :             :  *
      86                 :             :  *   TIMER_CORE_HAVE_CYCLE_GET_32, TIMER_CORE_HAVE_CYCLE_GET_64
      87                 :             :  *      Define one or both if the sys_clock_cycle_get_32/64() provided here
      88                 :             :  *      don't fit, and the driver provides its own instead, e.g. to convert
      89                 :             :  *      the counter to the kernel's rate.
      90                 :             :  *
      91                 :             :  *   TIMER_CORE_ALARM_MAX_CYCLES
      92                 :             :  *      Largest value the arming function accepts. Defaults to the full
      93                 :             :  *      counter range. Set it when the compare or reload register is
      94                 :             :  *      narrower than the counter, or is a separate device. State the
      95                 :             :  *      hardware limit only; the core adds its own margin.
      96                 :             :  *
      97                 :             :  *   TIMER_CORE_ALARM_MIN_CYCLES   (RELOAD)
      98                 :             :  *      Smallest reload, in cycles. Defaults to 1.
      99                 :             :  *
     100                 :             :  *   TIMER_CORE_ALARM_LEAD_CYCLES   (COMPARE_EXACT)
     101                 :             :  *      How far ahead of the counter a compare value must be to be caught.
     102                 :             :  *      Defaults to 1. Raise it if the hardware needs time to sync the
     103                 :             :  *      write to the counter's clock.
     104                 :             :  *
     105                 :             :  * The rest of the driver is:
     106                 :             :  *   - the ISR: acknowledge the hardware, then call timer_core_announce();
     107                 :             :  *   - init: connect the IRQ, then call timer_core_init();
     108                 :             :  *   - on SMP, smp_timer_init(): call timer_core_smp_prime().
     109                 :             :  */
     110                 :             : 
     111                 :             : #ifndef ZEPHYR_DRIVERS_TIMER_SYSTEM_TIMER_GENERIC_H_
     112                 :             : #define ZEPHYR_DRIVERS_TIMER_SYSTEM_TIMER_GENERIC_H_
     113                 :             : 
     114                 :             : #include <zephyr/drivers/timer/system_timer_lpm.h>
     115                 :             : #include <zephyr/drivers/timer/system_timer.h>
     116                 :             : #include <zephyr/sys/clock.h>
     117                 :             : #include <zephyr/sys/util.h>
     118                 :             : 
     119                 :             : #if (defined(TIMER_CORE_BACKEND_COMPARE_ORDERED) + \
     120                 :             :         defined(TIMER_CORE_BACKEND_COMPARE_EXACT) + \
     121                 :             :         defined(TIMER_CORE_BACKEND_RELOAD)) != 1
     122                 :             : #error "define exactly one backend: TIMER_CORE_BACKEND_COMPARE_ORDERED, " \
     123                 :             :         "TIMER_CORE_BACKEND_COMPARE_EXACT or TIMER_CORE_BACKEND_RELOAD"
     124                 :             : #endif
     125                 :             : 
     126                 :             : /* Counter rate. Remember whether the driver set it: the cycle getters further
     127                 :             :  * down depend on that.
     128                 :             :  */
     129                 :             : #if defined(TIMER_CORE_CYCLES_PER_SEC)
     130                 :             : #define TIMER_CORE_DRIVER_CYCLES_PER_SEC
     131                 :             : #endif
     132                 :             : 
     133                 :             : #if !defined(TIMER_CORE_CYCLES_PER_SEC)
     134                 :             : #if defined(CONFIG_TIMER_READS_ITS_FREQUENCY_AT_RUNTIME) || \
     135                 :             :         defined(CONFIG_SYSTEM_CLOCK_HW_CYCLES_PER_SEC_RUNTIME_UPDATE)
     136                 :             : #define TIMER_CORE_CYCLES_PER_SEC sys_clock_hw_cycles_per_sec()
     137                 :             : #else
     138                 :             : /* The Kconfig value directly: sys_clock_hw_cycles_per_sec() has a cast,
     139                 :             :  * which #if can't evaluate.
     140                 :             :  */
     141                 :             : #define TIMER_CORE_CYCLES_PER_SEC CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC
     142                 :             : #define TIMER_CORE_CYC_PER_TICK_IS_CONSTANT
     143                 :             : #endif
     144                 :             : #endif
     145                 :             : 
     146                 :             : /*
     147                 :             :  * Cycles per tick: a constant if the rate is, or computed once in
     148                 :             :  * timer_core_init() otherwise.
     149                 :             :  */
     150                 :             : #if defined(CONFIG_TIMER_READS_ITS_FREQUENCY_AT_RUNTIME) || \
     151                 :             :         defined(TIMER_CORE_CYCLES_PER_SEC_RUNTIME)
     152                 :             : static uint32_t timer_core_cyc_per_tick;
     153                 :             : #define TIMER_CORE_CYC_PER_TICK timer_core_cyc_per_tick
     154                 :             : #define TIMER_CORE_PRECOMPUTE_CYC_PER_TICK
     155                 :             : #else
     156                 :             : #define TIMER_CORE_CYC_PER_TICK (TIMER_CORE_CYCLES_PER_SEC / CONFIG_SYS_CLOCK_TICKS_PER_SEC)
     157                 :             : /*
     158                 :             :  * This is zero if the counter runs slower than the tick rate, and we divide
     159                 :             :  * by it. Catch that at build time if constant, or in timer_core_init()
     160                 :             :  * otherwise.
     161                 :             :  */
     162                 :             : #if defined(CONFIG_SYSTEM_CLOCK_HW_CYCLES_PER_SEC_RUNTIME_UPDATE)
     163                 :             : #define TIMER_CORE_CHECK_CYC_PER_TICK_AT_INIT
     164                 :             : #else
     165                 :             : BUILD_ASSERT(TIMER_CORE_CYC_PER_TICK != 0, "timer counter rate is below the tick rate");
     166                 :             : #endif
     167                 :             : #endif
     168                 :             : 
     169                 :             : #if !defined(TIMER_CORE_COUNTER_WIDTH)
     170                 :             : #define TIMER_CORE_COUNTER_WIDTH (__SIZEOF_LONG__ * 8)
     171                 :             : #endif
     172                 :             : 
     173                 :             : #define TIMER_CORE_COUNTER_MASK (UINT64_MAX >> (64 - TIMER_CORE_COUNTER_WIDTH))
     174                 :             : 
     175                 :             : /*
     176                 :             :  * Type for a counter value, or the difference between two. 32 bits when the
     177                 :             :  * counter fits, so a 32-bit CPU does the tick math in one register. The
     178                 :             :  * baseline below is always 64 bits, as it keeps counting past the counter's
     179                 :             :  * wrap.
     180                 :             :  */
     181                 :             : #if TIMER_CORE_COUNTER_WIDTH <= 32
     182                 :             : typedef uint32_t timer_core_cycles_t;
     183                 :             : #else
     184                 :             : typedef uint64_t timer_core_cycles_t;
     185                 :             : #endif
     186                 :             : 
     187                 :             : /* The alarm is usually part of the same counter. */
     188                 :             : #ifndef TIMER_CORE_ALARM_MAX_CYCLES
     189                 :             : #define TIMER_CORE_ALARM_MAX_CYCLES TIMER_CORE_COUNTER_MASK
     190                 :             : #endif
     191                 :             : 
     192                 :             : #ifndef TIMER_CORE_ALARM_MIN_CYCLES
     193                 :             : #define TIMER_CORE_ALARM_MIN_CYCLES 1
     194                 :             : #endif
     195                 :             : 
     196                 :             : /*
     197                 :             :  * How far past the last announce the counter can go and still be measured
     198                 :             :  * correctly.
     199                 :             :  *
     200                 :             :  * Half the counter range, leaving the other half as slack in case the
     201                 :             :  * interrupt comes late. With TIMER_CORE_COUNTER_NONMONOTONIC, the upper half
     202                 :             :  * is kept to detect backwards reads, so this is a quarter, with the next
     203                 :             :  * quarter as slack.
     204                 :             :  */
     205                 :             : #ifdef TIMER_CORE_COUNTER_NONMONOTONIC
     206                 :             : #define TIMER_CORE_COUNTER_SAFE_SPAN (TIMER_CORE_COUNTER_MASK >> 2)
     207                 :             : #else
     208                 :             : #define TIMER_CORE_COUNTER_SAFE_SPAN (TIMER_CORE_COUNTER_MASK >> 1)
     209                 :             : #endif
     210                 :             : 
     211                 :             : /*
     212                 :             :  * Longest time allowed between two announces, in cycles. Only the counter
     213                 :             :  * limits this: an alarm with a shorter reach just takes several arms.
     214                 :             :  */
     215                 :             : #define TIMER_CORE_MAX_UNANNOUNCED_CYCLES TIMER_CORE_COUNTER_SAFE_SPAN
     216                 :             : 
     217                 :             : /*
     218                 :             :  * Furthest a single arm can reach: the alarm's limit, capped by the counter's.
     219                 :             :  * A deadline further out takes several arms.
     220                 :             :  */
     221                 :             : #define TIMER_CORE_HW_MAX_ARM_CYCLES                                                               \
     222                 :             :         MIN((uint64_t)TIMER_CORE_COUNTER_SAFE_SPAN, (uint64_t)TIMER_CORE_ALARM_MAX_CYCLES)
     223                 :             : 
     224                 :             : /*
     225                 :             :  * QEMU icount without sleep skips idle time by jumping straight to the next
     226                 :             :  * armed deadline. With nothing pending, that deadline is SYS_CLOCK_MAX_WAIT
     227                 :             :  * ticks out (248 days at 100 Hz), and a few hundred such jumps overflow
     228                 :             :  * QEMU's 64-bit nanosecond clock, after which no timer fires again. So cap
     229                 :             :  * each arm at one second there. This is emulator policy, not a hardware
     230                 :             :  * limit, so it isn't part of TIMER_CORE_ALARM_MAX_CYCLES.
     231                 :             :  */
     232                 :             : #if defined(CONFIG_QEMU_ICOUNT) && !defined(CONFIG_QEMU_ICOUNT_SLEEP)
     233                 :             : #define TIMER_CORE_MAX_ARM_CYCLES                                                                  \
     234                 :             :         ((timer_core_cycles_t)MIN(TIMER_CORE_HW_MAX_ARM_CYCLES,                                    \
     235                 :             :                                   (uint64_t)TIMER_CORE_CYCLES_PER_SEC))
     236                 :             : #else
     237                 :             : #define TIMER_CORE_MAX_ARM_CYCLES ((timer_core_cycles_t)TIMER_CORE_HW_MAX_ARM_CYCLES)
     238                 :             : #endif
     239                 :             : 
     240                 :             : /*
     241                 :             :  * Where the last announce left us.
     242                 :             :  *
     243                 :             :  * last_cycle: counter value at the last announce, always on a tick boundary
     244                 :             :  *             (last_tick * TIMER_CORE_CYC_PER_TICK). Its low bits follow the
     245                 :             :  *             hardware counter, so differences against it survive a wrap.
     246                 :             :  * last_tick:  ticks announced so far.
     247                 :             :  * last_elapsed (below): the last tick count sys_clock_elapsed() returned.
     248                 :             :  */
     249                 :             : static uint64_t timer_core_last_cycle;
     250                 :             : static uint64_t timer_core_last_tick;
     251                 :             : 
     252                 :             : /* Cycles from @p from to now, correct even if the counter wrapped in between. */
     253                 :         125 : static inline timer_core_cycles_t timer_core_cycles_since(uint64_t from)
     254                 :             : {
     255                 :         125 :         return ((timer_core_cycles_t)timer_driver_cycle_get() - (timer_core_cycles_t)from) &
     256                 :             :                (timer_core_cycles_t)TIMER_CORE_COUNTER_MASK;
     257                 :             : }
     258                 :             : 
     259                 :             : /* Ticks in the full counter range: an upper bound on any tick count between
     260                 :             :  * two announces.
     261                 :             :  *
     262                 :             :  * The tick length matters as much as the counter width: a 48-bit counter at
     263                 :             :  * 100000 cycles per tick tops out near 2.8e9 ticks, which fits in 32 bits.
     264                 :             :  * Divide in #if when cycles per tick is constant, or go by the width alone
     265                 :             :  * otherwise.
     266                 :             :  */
     267                 :             : #if defined(TIMER_CORE_CYC_PER_TICK_IS_CONSTANT)
     268                 :             : #define TIMER_CORE_MAX_TICKS (TIMER_CORE_COUNTER_MASK / TIMER_CORE_CYC_PER_TICK)
     269                 :             : #else
     270                 :             : #define TIMER_CORE_MAX_TICKS TIMER_CORE_COUNTER_MASK
     271                 :             : #endif
     272                 :             : 
     273                 :             : /* Type for tick counts inside the core. 64 bits when the count can exceed 32
     274                 :             :  * bits: with no timeout pending, nothing announces and the count keeps
     275                 :             :  * growing. The kernel interface is 32 bits, so timer_core_ticks_clamp() is
     276                 :             :  * used where a count is passed to it.
     277                 :             :  */
     278                 :             : #if TIMER_CORE_MAX_TICKS > UINT32_MAX
     279                 :             : typedef uint64_t timer_core_ticks_t;
     280                 :         125 : static inline uint32_t timer_core_ticks_clamp(timer_core_ticks_t ticks)
     281                 :             : {
     282                 :         125 :         return (uint32_t)MIN(ticks, (timer_core_ticks_t)UINT32_MAX);
     283                 :             : }
     284                 :             : #else
     285                 :             : typedef uint32_t timer_core_ticks_t;
     286                 :             : static inline uint32_t timer_core_ticks_clamp(timer_core_ticks_t ticks)
     287                 :             : {
     288                 :             :         return ticks;
     289                 :             : }
     290                 :             : #endif
     291                 :             : static timer_core_ticks_t timer_core_last_elapsed;
     292                 :             : 
     293                 :             : /*
     294                 :             :  * TIMER_CORE_MAX_UNANNOUNCED_CYCLES in ticks, for clamping before multiplying
     295                 :             :  * so the cycle count can't overflow. A constant if the rate is, or computed
     296                 :             :  * once in timer_core_init() otherwise.
     297                 :             :  */
     298                 :             : #if defined(TIMER_CORE_PRECOMPUTE_CYC_PER_TICK)
     299                 :             : static timer_core_ticks_t timer_core_max_span_ticks;
     300                 :             : #define TIMER_CORE_MAX_SPAN_TICKS timer_core_max_span_ticks
     301                 :             : #else
     302                 :             : #define TIMER_CORE_MAX_SPAN_TICKS (TIMER_CORE_MAX_UNANNOUNCED_CYCLES / TIMER_CORE_CYC_PER_TICK)
     303                 :             : #if !defined(TIMER_CORE_CHECK_CYC_PER_TICK_AT_INIT)
     304                 :             : /* A tick must fit in what the counter can measure. Checked here if constant,
     305                 :             :  * or in timer_core_init() otherwise. Only the counter matters: if the alarm
     306                 :             :  * can't cover a whole tick in one arm, the core just arms it several times.
     307                 :             :  */
     308                 :             : BUILD_ASSERT(TIMER_CORE_COUNTER_SAFE_SPAN >= TIMER_CORE_CYC_PER_TICK,
     309                 :             :              "a tick is longer than the counter can span: raise "
     310                 :             :              "CONFIG_SYS_CLOCK_TICKS_PER_SEC, or slow the counter");
     311                 :             : #endif
     312                 :             : #endif
     313                 :             : 
     314                 :             : #if defined(TIMER_CORE_BACKEND_RELOAD)
     315                 :             : /* Set when timer_core_arm() writes a minimum reload because the deadline is
     316                 :             :  * already due. Until the announce clears it, later calls that are also due
     317                 :             :  * skip the write, so they don't keep restarting the counter.
     318                 :             :  */
     319                 :             : static bool timer_core_catchup;
     320                 :             : 
     321                 :             : /* Deadline in the hardware, in ticks, or UINT64_MAX if none. set_timeout()
     322                 :             :  * often repeats the same deadline, since the kernel recomputes it on every
     323                 :             :  * timeout add and abort. Rewriting a reload restarts the counter, so a
     324                 :             :  * steady stream of repeats would keep the interrupt from ever firing. Skip
     325                 :             :  * the write when the deadline hasn't changed.
     326                 :             :  */
     327                 :             : static uint64_t timer_core_armed_deadline = UINT64_MAX;
     328                 :             : #endif
     329                 :             : 
     330                 :             : /* Set the compare register to an absolute counter value. Each backend
     331                 :             :  * handles a value already in the past its own way.
     332                 :             :  */
     333                 :             : #if defined(TIMER_CORE_BACKEND_COMPARE_ORDERED)
     334                 :         126 : static inline void timer_core_set_compare(uint64_t target)
     335                 :             : {
     336                 :             :         /* A value already passed fires right away. */
     337                 :         126 :         timer_driver_set_compare(target);
     338                 :         126 : }
     339                 :             : #elif defined(TIMER_CORE_BACKEND_COMPARE_EXACT)
     340                 :             : #ifndef TIMER_CORE_ALARM_LEAD_CYCLES
     341                 :             : #define TIMER_CORE_ALARM_LEAD_CYCLES 1
     342                 :             : #endif
     343                 :             : 
     344                 :             : /* True when @p target is no longer far enough ahead of @p now to be caught. */
     345                 :             : static inline bool timer_core_target_passed(uint64_t target, uint64_t now)
     346                 :             : {
     347                 :             :         timer_core_cycles_t ahead = ((timer_core_cycles_t)target - (timer_core_cycles_t)now) &
     348                 :             :                                     (timer_core_cycles_t)TIMER_CORE_COUNTER_MASK;
     349                 :             : 
     350                 :             :         /* A target already behind the counter wraps to a large value here. The
     351                 :             :          * core never arms more than half the counter range ahead, so anything
     352                 :             :          * above half is a target already passed.
     353                 :             :          */
     354                 :             :         return (ahead < TIMER_CORE_ALARM_LEAD_CYCLES) ||
     355                 :             :                (ahead > (timer_core_cycles_t)(TIMER_CORE_COUNTER_MASK >> 1));
     356                 :             : }
     357                 :             : 
     358                 :             : /* Write an exact-match compare register without losing the interrupt.
     359                 :             :  *
     360                 :             :  * If the counter passes the value before it is written, the match only
     361                 :             :  * happens after the counter wraps all the way round. So write, read the
     362                 :             :  * counter back, and if the value is no longer ahead, move it further out and
     363                 :             :  * try again. The step doubles each time, so this ends even if the counter
     364                 :             :  * keeps catching up.
     365                 :             :  */
     366                 :             : static inline void timer_core_set_compare(uint64_t target)
     367                 :             : {
     368                 :             :         timer_driver_set_compare(target);
     369                 :             : 
     370                 :             :         timer_core_cycles_t now = timer_driver_cycle_get();
     371                 :             : 
     372                 :             :         if (unlikely(timer_core_target_passed(target, now))) {
     373                 :             :                 uint32_t bump = TIMER_CORE_ALARM_LEAD_CYCLES;
     374                 :             : 
     375                 :             :                 do {
     376                 :             :                         target = now + bump;
     377                 :             :                         bump *= 2;
     378                 :             :                         timer_driver_set_compare(target);
     379                 :             :                         now = timer_driver_cycle_get();
     380                 :             :                 } while (timer_core_target_passed(target, now));
     381                 :             :         }
     382                 :             : }
     383                 :             : #endif
     384                 :             : 
     385                 :             : /* Whole ticks since the last announce. */
     386                 :         125 : static inline timer_core_ticks_t timer_core_delta_ticks(void)
     387                 :             : {
     388                 :         125 :         timer_core_cycles_t delta = timer_core_cycles_since(timer_core_last_cycle);
     389                 :             : 
     390                 :             : #ifdef TIMER_CORE_COUNTER_NONMONOTONIC
     391                 :             :         /*
     392                 :             :          * A counter read slightly behind the last announce gives a difference
     393                 :             :          * near the full counter range. The core never lets more than a quarter
     394                 :             :          * of the range go by (TIMER_CORE_COUNTER_SAFE_SPAN), so anything in the
     395                 :             :          * upper half is such a backwards read: count it as no time passed.
     396                 :             :          */
     397                 :             :         if (delta > (timer_core_cycles_t)(TIMER_CORE_COUNTER_MASK >> 1)) {
     398                 :             :                 return 0;
     399                 :             :         }
     400                 :             : #endif
     401                 :             : 
     402                 :         125 :         return delta / TIMER_CORE_CYC_PER_TICK;
     403                 :             : }
     404                 :             : 
     405                 :             : /* Arm the timer for `ticks` past the last elapsed count the kernel got, on a
     406                 :             :  * tick boundary. Called with the clock lock held.
     407                 :             :  */
     408                 :         126 : static void timer_core_arm(uint32_t ticks)
     409                 :             : {
     410                 :             : #if defined(TIMER_CORE_BACKEND_RELOAD)
     411                 :             :         uint64_t deadline_tick = timer_core_last_tick + timer_core_last_elapsed + ticks;
     412                 :             : 
     413                 :             :         if (deadline_tick == timer_core_armed_deadline) {
     414                 :             :                 return;
     415                 :             :         }
     416                 :             :         timer_core_armed_deadline = deadline_tick;
     417                 :             : 
     418                 :             :         /*
     419                 :             :          * The reload is the cycles from the last announce to the deadline,
     420                 :             :          * minus those already gone by.
     421                 :             :          *
     422                 :             :          * Clamp the span in ticks, before multiplying, so it can't overflow.
     423                 :             :          * The span counts from the last announce, so the interrupt fires
     424                 :             :          * within TIMER_CORE_MAX_UNANNOUNCED_CYCLES of it, however many
     425                 :             :          * set_timeout() calls may come in between. Without that, each call
     426                 :             :          * would re-arm from now, and a steady stream of them could hold off
     427                 :             :          * the interrupt for longer than the counter range. The cycle count
     428                 :             :          * since the last announce would then wrap, and that time would never
     429                 :             :          * be announced.
     430                 :             :          */
     431                 :             :         timer_core_ticks_t span = TIMER_CORE_MAX_SPAN_TICKS;
     432                 :             : 
     433                 :             :         if ((ticks <= span) && (timer_core_last_elapsed <= (span - ticks))) {
     434                 :             :                 span = timer_core_last_elapsed + ticks;
     435                 :             :         }
     436                 :             : 
     437                 :             :         timer_core_cycles_t want = (timer_core_cycles_t)span * TIMER_CORE_CYC_PER_TICK;
     438                 :             :         timer_core_cycles_t done = timer_core_cycles_since(timer_core_last_cycle);
     439                 :             : 
     440                 :             :         /* Deadline already passed: 0, and the minimum reload below takes over. */
     441                 :             :         timer_core_cycles_t rel = (want > done) ? (want - done) : 0;
     442                 :             : 
     443                 :             :         /* Only needed if the alarm reaches less far than the counter. Otherwise
     444                 :             :          * the span clamp above covers it, and this compiles away.
     445                 :             :          */
     446                 :             :         if ((TIMER_CORE_MAX_ARM_CYCLES < TIMER_CORE_MAX_UNANNOUNCED_CYCLES) &&
     447                 :             :             (rel > TIMER_CORE_MAX_ARM_CYCLES)) {
     448                 :             :                 rel = TIMER_CORE_MAX_ARM_CYCLES;
     449                 :             :         }
     450                 :             : 
     451                 :             :         if (rel < TIMER_CORE_ALARM_MIN_CYCLES) {
     452                 :             :                 /*
     453                 :             :                  * Due now: fire as soon as possible. If a minimum reload is
     454                 :             :                  * already pending, let it fire rather than restart the counter.
     455                 :             :                  * Otherwise set_timeout() calls coming faster than the minimum
     456                 :             :                  * reload (e.g. timeslice resets from a syscall-heavy thread)
     457                 :             :                  * would push it back for as long as they keep coming, and time
     458                 :             :                  * would stop. Nothing can be due sooner than the minimum anyway.
     459                 :             :                  */
     460                 :             :                 if (timer_core_catchup) {
     461                 :             :                         return;
     462                 :             :                 }
     463                 :             :                 timer_core_catchup = true;
     464                 :             :                 rel = TIMER_CORE_ALARM_MIN_CYCLES;
     465                 :             :         }
     466                 :             :         timer_driver_set_reload(rel);
     467                 :             : #else /* compare backends */
     468                 :             :         /*
     469                 :             :          * The deadline is (last_tick + last_elapsed + ticks) ticks, in cycles.
     470                 :             :          * Since last_cycle == last_tick * CYC exactly (CYC being
     471                 :             :          * TIMER_CORE_CYC_PER_TICK), that is
     472                 :             :          *
     473                 :             :          *   last_cycle + (last_elapsed + ticks) * CYC
     474                 :             :          *
     475                 :             :          * Computing the span part on its own keeps it in the counter's width, so
     476                 :             :          * a 32-bit counter on a 32-bit CPU needs no 64-bit math. Clamp the span
     477                 :             :          * in ticks, before multiplying, so it can't overflow.
     478                 :             :          */
     479                 :             : #if TIMER_CORE_COUNTER_WIDTH <= 32
     480                 :             :         timer_core_ticks_t span = TIMER_CORE_MAX_SPAN_TICKS;
     481                 :             : 
     482                 :             :         if ((ticks <= span) && (timer_core_last_elapsed <= (span - ticks))) {
     483                 :             :                 span = timer_core_last_elapsed + ticks;
     484                 :             :         }
     485                 :             :         timer_core_cycles_t offset = (timer_core_cycles_t)span * TIMER_CORE_CYC_PER_TICK;
     486                 :             : 
     487                 :             :         if ((TIMER_CORE_MAX_ARM_CYCLES < TIMER_CORE_MAX_UNANNOUNCED_CYCLES) &&
     488                 :             :             (offset > TIMER_CORE_MAX_ARM_CYCLES)) {
     489                 :             :                 offset = TIMER_CORE_MAX_ARM_CYCLES;
     490                 :             :         }
     491                 :             :         timer_core_set_compare(timer_core_last_cycle + offset);
     492                 :             : #else
     493                 :             :         /* 64-bit counter: the math is 64-bit anyway, so compute the deadline
     494                 :             :          * directly and clamp it.
     495                 :             :          */
     496                 :         126 :         uint64_t deadline =
     497                 :         126 :                 (timer_core_last_tick + timer_core_last_elapsed + ticks) * TIMER_CORE_CYC_PER_TICK;
     498                 :             : 
     499         [ -  + ]:         126 :         if ((deadline - timer_core_last_cycle) > TIMER_CORE_MAX_ARM_CYCLES) {
     500                 :           0 :                 deadline = timer_core_last_cycle + TIMER_CORE_MAX_ARM_CYCLES;
     501                 :             :         }
     502                 :         126 :         timer_core_set_compare(deadline);
     503                 :             : #endif
     504                 :             : #endif
     505                 :         126 : }
     506                 :             : 
     507                 :         125 : void sys_clock_set_timeout(uint32_t ticks, bool idle)
     508                 :             : {
     509                 :             :         /* Deprecated: idle entry now goes through sys_clock_idle_enter(). */
     510                 :         125 :         ARG_UNUSED(idle);
     511                 :             : 
     512         [ -  + ]:         125 :         __ASSERT(sys_clock_is_locked(), "system clock lock not held");
     513                 :             : 
     514                 :         125 :         if (!IS_ENABLED(CONFIG_TICKLESS_KERNEL)) {
     515                 :             :                 return;
     516                 :             :         }
     517                 :         125 :         timer_core_arm(ticks);
     518                 :             : }
     519                 :             : 
     520                 :         125 : uint32_t sys_clock_elapsed(void)
     521                 :             : {
     522         [ -  + ]:         125 :         __ASSERT(sys_clock_is_locked(), "system clock lock not held");
     523                 :             : 
     524                 :         125 :         if (!IS_ENABLED(CONFIG_TICKLESS_KERNEL)) {
     525                 :             :                 return 0;
     526                 :             :         }
     527                 :             : 
     528                 :         125 :         timer_core_last_elapsed = timer_core_delta_ticks();
     529                 :         125 :         return timer_core_ticks_clamp(timer_core_last_elapsed);
     530                 :             : }
     531                 :             : 
     532                 :             : /* Announce the whole ticks since the last announce, with the clock lock
     533                 :             :  * already held. @p key comes from the driver's sys_clock_lock() and is
     534                 :             :  * released here. Use this when the driver has work to do under the same lock
     535                 :             :  * first, e.g. adding a wrap to its software count.
     536                 :             :  */
     537                 :           0 : static void timer_core_announce_from(k_spinlock_key_t key)
     538                 :             : {
     539                 :           0 :         timer_core_ticks_t dticks = timer_core_delta_ticks();
     540                 :             : 
     541                 :           0 :         timer_core_last_cycle += (timer_core_cycles_t)dticks * TIMER_CORE_CYC_PER_TICK;
     542                 :           0 :         timer_core_last_tick += dticks;
     543                 :           0 :         timer_core_last_elapsed = 0;
     544                 :             : #if defined(TIMER_CORE_BACKEND_RELOAD)
     545                 :             :         /* The armed deadline is used up. The kernel sets the next one, and it
     546                 :             :          * must be written even if it is the same tick.
     547                 :             :          */
     548                 :             :         timer_core_armed_deadline = UINT64_MAX;
     549                 :             :         timer_core_catchup = false;
     550                 :             : #endif
     551                 :             : 
     552                 :             : #if !defined(TIMER_CORE_BACKEND_RELOAD)
     553                 :           0 :         if (!IS_ENABLED(CONFIG_TICKLESS_KERNEL)) {
     554                 :             :                 /* Ticking kernel: arm the next tick. A reload counter reloads
     555                 :             :                  * by itself.
     556                 :             :                  */
     557                 :             :                 timer_core_arm(1);
     558                 :             :         }
     559                 :             : #endif
     560                 :             : 
     561                 :             :         /* The baseline keeps the full count. Only the value passed to the kernel
     562                 :             :          * is clamped to 32 bits, and that only kicks in after a very long idle
     563                 :             :          * with nothing pending.
     564                 :             :          */
     565                 :           0 :         sys_clock_announce_locked(timer_core_ticks_clamp(dticks), key);
     566                 :           0 : }
     567                 :             : 
     568                 :             : /* Announce a number of cycles the driver measured itself, rather than read
     569                 :             :  * from the counter, with the clock lock held. @p key comes from the driver's
     570                 :             :  * sys_clock_lock() and is released here.
     571                 :             :  *
     572                 :             :  * For a driver whose counter was stopped while something else kept time,
     573                 :             :  * like a low-power companion timer. That time can be longer than the counter
     574                 :             :  * can measure, so the math here is always 64-bit. timer_core_announce_from()
     575                 :             :  * stays in the counter's width, so only this path pays for it.
     576                 :             :  *
     577                 :             :  * Only whole ticks are announced. The leftover fraction is still in the
     578                 :             :  * driver's counter and is picked up by the next announce.
     579                 :             :  *
     580                 :             :  * inline so drivers that don't use it get no unused-function warning.
     581                 :             :  */
     582                 :             : static inline void timer_core_announce_cycles64_from(k_spinlock_key_t key, uint64_t cycles)
     583                 :             : {
     584                 :             :         uint64_t dticks = cycles / TIMER_CORE_CYC_PER_TICK;
     585                 :             : 
     586                 :             :         timer_core_last_cycle += dticks * TIMER_CORE_CYC_PER_TICK;
     587                 :             :         timer_core_last_tick += dticks;
     588                 :             :         timer_core_last_elapsed = 0;
     589                 :             : #if defined(TIMER_CORE_BACKEND_RELOAD)
     590                 :             :         timer_core_armed_deadline = UINT64_MAX;
     591                 :             :         timer_core_catchup = false;
     592                 :             : #endif
     593                 :             : 
     594                 :             :         /*
     595                 :             :          * Not timer_core_ticks_clamp(): it takes a timer_core_ticks_t, which
     596                 :             :          * may be too narrow for this count and would truncate it.
     597                 :             :          */
     598                 :             :         sys_clock_announce_locked((uint32_t)MIN(dticks, (uint64_t)UINT32_MAX), key);
     599                 :             : }
     600                 :             : 
     601                 :             : /* Announce the ticks since the last announce. The driver ISR is just a
     602                 :             :  * hardware acknowledge followed by this. inline so a driver that uses
     603                 :             :  * timer_core_announce_from() instead gets no unused-function warning.
     604                 :             :  */
     605                 :           0 : static inline void timer_core_announce(void)
     606                 :             : {
     607                 :           0 :         timer_core_announce_from(sys_clock_lock());
     608                 :           0 : }
     609                 :             : 
     610                 :             : /*
     611                 :             :  * Full 64-bit count, in counter cycles: the baseline plus the cycles since.
     612                 :             :  * A driver that scales the count builds its own getters on this.
     613                 :             :  *
     614                 :             :  * Takes the clock lock when the read isn't atomic:
     615                 :             :  *   - TIMER_CORE_COUNTER_NONATOMIC: the count is built in software from
     616                 :             :  *     state the ISR also updates;
     617                 :             :  *   - a counter narrower than 64 bits: the 64-bit baseline is two loads on a
     618                 :             :  *     32-bit CPU.
     619                 :             :  *
     620                 :             :  * sys_clock_cycle_get_32() avoids the lock for a narrow counter by using only
     621                 :             :  * the baseline's low word. That matters because thread usage timing calls it
     622                 :             :  * a lot.
     623                 :             :  */
     624                 :           0 : static inline uint64_t timer_core_cycle_get(void)
     625                 :             : {
     626                 :             : #if defined(TIMER_CORE_COUNTER_NONATOMIC) || TIMER_CORE_COUNTER_WIDTH < 64
     627                 :             :         k_spinlock_key_t key = sys_clock_lock();
     628                 :             :         uint64_t ret = timer_core_last_cycle + timer_core_cycles_since(timer_core_last_cycle);
     629                 :             : 
     630                 :             :         sys_clock_unlock(key);
     631                 :             :         return ret;
     632                 :             : #else
     633                 :             :         /* A 64-bit counter is already the full count. */
     634                 :           0 :         return timer_driver_cycle_get();
     635                 :             : #endif
     636                 :             : }
     637                 :             : 
     638                 :             : /*
     639                 :             :  * A driver that sets TIMER_CORE_CYCLES_PER_SEC has a counter at another rate
     640                 :             :  * than the kernel's, so the core's getters would return the wrong unit. The
     641                 :             :  * driver must provide sys_clock_cycle_get_32(), and the 64-bit one if it
     642                 :             :  * needs it.
     643                 :             :  */
     644                 :             : #if defined(TIMER_CORE_DRIVER_CYCLES_PER_SEC)
     645                 :             : #if !defined(TIMER_CORE_HAVE_CYCLE_GET_32)
     646                 :             : #error "a driver setting TIMER_CORE_CYCLES_PER_SEC must define " \
     647                 :             :         "TIMER_CORE_HAVE_CYCLE_GET_32 and supply sys_clock_cycle_get_32()"
     648                 :             : #endif
     649                 :             : #ifndef TIMER_CORE_HAVE_CYCLE_GET_64
     650                 :             : #define TIMER_CORE_HAVE_CYCLE_GET_64
     651                 :             : #endif
     652                 :             : #endif
     653                 :             : 
     654                 :             : #if !defined(TIMER_CORE_HAVE_CYCLE_GET_32)
     655                 :         309 : uint32_t sys_clock_cycle_get_32(void)
     656                 :             : {
     657                 :             : #if defined(TIMER_CORE_COUNTER_NONATOMIC)
     658                 :             :         /* Software count: read it under the lock. */
     659                 :             :         return (uint32_t)timer_core_cycle_get();
     660                 :             : #elif TIMER_CORE_COUNTER_WIDTH < 32
     661                 :             :         /* Narrow counter: add the cycles since the baseline's low word. Each is
     662                 :             :          * a single load, and the result is right even if an announce happens
     663                 :             :          * in between, so no lock is needed.
     664                 :             :          */
     665                 :             :         uint32_t base = (uint32_t)timer_core_last_cycle;
     666                 :             :         uint32_t delta = ((uint32_t)timer_driver_cycle_get() - base) &
     667                 :             :                          (uint32_t)TIMER_CORE_COUNTER_MASK;
     668                 :             : 
     669                 :             :         return base + delta;
     670                 :             : #else
     671                 :         309 :         return (uint32_t)timer_driver_cycle_get();
     672                 :             : #endif
     673                 :             : }
     674                 :             : #endif
     675                 :             : 
     676                 :             : #if !defined(TIMER_CORE_HAVE_CYCLE_GET_64)
     677                 :           0 : uint64_t sys_clock_cycle_get_64(void)
     678                 :             : {
     679                 :           0 :         return timer_core_cycle_get();
     680                 :             : }
     681                 :             : #endif
     682                 :             : 
     683                 :             : /* Switch the baseline to a new counter rate. A driver that changes the timer
     684                 :             :  * frequency at run time calls this after rescaling its own counter.
     685                 :             :  */
     686                 :             : static inline void timer_core_rescale(uint32_t to_hz, uint32_t from_hz)
     687                 :             : {
     688                 :             :         ARG_UNUSED(from_hz);
     689                 :             : #ifdef TIMER_CORE_PRECOMPUTE_CYC_PER_TICK
     690                 :             :         timer_core_cyc_per_tick = to_hz / CONFIG_SYS_CLOCK_TICKS_PER_SEC;
     691                 :             : #else
     692                 :             :         ARG_UNUSED(to_hz);
     693                 :             : #endif
     694                 :             :         /*
     695                 :             :          * Recompute last_cycle from last_tick, which doesn't depend on the rate,
     696                 :             :          * so it stays on a tick boundary. Scaling the old value instead could
     697                 :             :          * leave it a fraction of a tick off, and the compare arm path could then
     698                 :             :          * underflow.
     699                 :             :          */
     700                 :             :         timer_core_last_cycle = timer_core_last_tick * TIMER_CORE_CYC_PER_TICK;
     701                 :             : }
     702                 :             : 
     703                 :             : /* Arm this CPU's timer one tick past the last announce. An SMP driver calls
     704                 :             :  * this from smp_timer_init().
     705                 :             :  */
     706                 :             : static inline void timer_core_smp_prime(void)
     707                 :             : {
     708                 :             : #if defined(TIMER_CORE_BACKEND_RELOAD)
     709                 :             :         timer_driver_set_reload(TIMER_CORE_CYC_PER_TICK);
     710                 :             : #else
     711                 :             :         timer_core_set_compare(timer_core_last_cycle + TIMER_CORE_CYC_PER_TICK);
     712                 :             : #endif
     713                 :             : }
     714                 :             : 
     715                 :             : /* Start counting from the current counter value and arm the first tick. The
     716                 :             :  * driver calls this from its init, after connecting the IRQ and, with a
     717                 :             :  * run-time rate, after setting it.
     718                 :             :  */
     719                 :           1 : static inline void timer_core_init(void)
     720                 :             : {
     721                 :             : #ifndef CONFIG_SYSTEM_TIMER_LPM_COMPANION_NONE
     722                 :           1 :         z_sys_clock_lpm_init();
     723                 :             : #endif /* !CONFIG_SYSTEM_TIMER_LPM_COMPANION_NONE */
     724                 :             : #ifdef TIMER_CORE_PRECOMPUTE_CYC_PER_TICK
     725                 :             :         /* The rate is known now that the driver has set up the counter. */
     726                 :             :         timer_core_cyc_per_tick = TIMER_CORE_CYCLES_PER_SEC / CONFIG_SYS_CLOCK_TICKS_PER_SEC;
     727                 :             :         timer_core_max_span_ticks = TIMER_CORE_MAX_UNANNOUNCED_CYCLES / TIMER_CORE_CYC_PER_TICK;
     728                 :             : #endif
     729                 :             : #if defined(TIMER_CORE_PRECOMPUTE_CYC_PER_TICK) || defined(TIMER_CORE_CHECK_CYC_PER_TICK_AT_INIT)
     730                 :             :         /* The checks a constant rate gets at build time. */
     731                 :             :         __ASSERT(TIMER_CORE_CYC_PER_TICK != 0, "timer counter rate is below the tick rate");
     732                 :             :         /* Cast both sides to uint64_t: otherwise, with a 64-bit counter, the
     733                 :             :          * compiler warns the comparison is always true.
     734                 :             :          */
     735                 :             :         __ASSERT((uint64_t)TIMER_CORE_COUNTER_SAFE_SPAN >= (uint64_t)TIMER_CORE_CYC_PER_TICK,
     736                 :             :                  "a tick is longer than the counter can span");
     737                 :             : #endif
     738                 :             :         /* seed and seed * TIMER_CORE_CYC_PER_TICK are both no larger than the
     739                 :             :          * counter value, so both fit in timer_core_cycles_t.
     740                 :             :          */
     741                 :           1 :         timer_core_cycles_t seed = timer_driver_cycle_get() / TIMER_CORE_CYC_PER_TICK;
     742                 :             : 
     743                 :           1 :         timer_core_last_tick = seed;
     744                 :           1 :         timer_core_last_cycle = seed * TIMER_CORE_CYC_PER_TICK;
     745                 :           1 :         timer_core_last_elapsed = 0;
     746                 :             : 
     747                 :             : #if defined(TIMER_CORE_BACKEND_RELOAD)
     748                 :             :         if (!IS_ENABLED(CONFIG_TICKLESS_KERNEL)) {
     749                 :             :                 /* Ticking kernel: the driver set the hardware to reload every
     750                 :             :                  * tick, and it is never rewritten. Arming here would load the
     751                 :             :                  * fraction of a tick left after the seed above, and that would
     752                 :             :                  * stick as the period.
     753                 :             :                  */
     754                 :             :                 return;
     755                 :             :         }
     756                 :             : #endif
     757                 :           1 :         timer_core_arm(1);
     758                 :           1 : }
     759                 :             : 
     760                 :             : #endif /* ZEPHYR_DRIVERS_TIMER_SYSTEM_TIMER_GENERIC_H_ */
        

Generated by: LCOV version 2.0-1