blob: cf4a6d681d585d9b11aa32b40f5c9570fa09ae93 [file] [log] [blame]
Andy Ross987c0e52018-09-27 16:50:00 -07001/*
2 * Copyright (c) 2018 Intel Corporation
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
Stephanos Ioannidis2d746042019-10-25 00:08:21 +09006
Gerard Marull-Paretascffefc82022-05-06 11:04:23 +02007#include <zephyr/kernel.h>
8#include <zephyr/spinlock.h>
Andy Ross987c0e52018-09-27 16:50:00 -07009#include <ksched.h>
Anas Nashiffcf50ed2023-08-29 19:32:46 +000010#include <timeout_q.h>
Anas Nashif4e396172023-09-26 22:46:01 +000011#include <zephyr/internal/syscall_handler.h>
Gerard Marull-Paretascffefc82022-05-06 11:04:23 +020012#include <zephyr/drivers/timer/system_timer.h>
13#include <zephyr/sys_clock.h>
Andy Ross987c0e52018-09-27 16:50:00 -070014
Kumar Galaa1b77fd2020-05-27 11:26:57 -050015static uint64_t curr_tick;
Andy Ross987c0e52018-09-27 16:50:00 -070016
17static sys_dlist_t timeout_list = SYS_DLIST_STATIC_INIT(&timeout_list);
18
19static struct k_spinlock timeout_lock;
20
Andy Ross1db9f182019-06-25 10:09:45 -070021#define MAX_WAIT (IS_ENABLED(CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE) \
Andy Ross78327382020-03-05 15:18:14 -080022 ? K_TICKS_FOREVER : INT_MAX)
Andy Ross987c0e52018-09-27 16:50:00 -070023
Florian Grandel5fa55342023-06-28 23:17:45 +020024/* Ticks left to process in the currently-executing sys_clock_announce() */
Andy Ross1cfff072018-10-03 08:50:52 -070025static int announce_remaining;
Andy Ross987c0e52018-09-27 16:50:00 -070026
27#if defined(CONFIG_TIMER_READS_ITS_FREQUENCY_AT_RUNTIME)
Alberto Escolar Piedras2f5e9392024-05-06 10:57:02 +020028int z_clock_hw_cycles_per_sec = CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC;
Andrew Boiefd49cf72019-05-21 14:02:26 -070029
30#ifdef CONFIG_USERSPACE
Anas Nashifa3872212021-03-13 08:16:53 -050031static inline int z_vrfy_sys_clock_hw_cycles_per_sec_runtime_get(void)
Andrew Boiefd49cf72019-05-21 14:02:26 -070032{
Anas Nashifa3872212021-03-13 08:16:53 -050033 return z_impl_sys_clock_hw_cycles_per_sec_runtime_get();
Andrew Boiefd49cf72019-05-21 14:02:26 -070034}
Anas Nashifa3872212021-03-13 08:16:53 -050035#include <syscalls/sys_clock_hw_cycles_per_sec_runtime_get_mrsh.c>
Andrew Boiefd49cf72019-05-21 14:02:26 -070036#endif /* CONFIG_USERSPACE */
37#endif /* CONFIG_TIMER_READS_ITS_FREQUENCY_AT_RUNTIME */
Andy Ross987c0e52018-09-27 16:50:00 -070038
39static struct _timeout *first(void)
40{
41 sys_dnode_t *t = sys_dlist_peek_head(&timeout_list);
42
Hess Nathan6d417d52024-04-30 13:26:35 +020043 return (t == NULL) ? NULL : CONTAINER_OF(t, struct _timeout, node);
Andy Ross987c0e52018-09-27 16:50:00 -070044}
45
46static struct _timeout *next(struct _timeout *t)
47{
48 sys_dnode_t *n = sys_dlist_peek_next(&timeout_list, &t->node);
49
Hess Nathan6d417d52024-04-30 13:26:35 +020050 return (n == NULL) ? NULL : CONTAINER_OF(n, struct _timeout, node);
Andy Ross987c0e52018-09-27 16:50:00 -070051}
52
Andy Ross386894c2018-10-17 08:29:19 -070053static void remove_timeout(struct _timeout *t)
Andy Ross987c0e52018-09-27 16:50:00 -070054{
Peter A. Bigot25fbe7b2018-12-30 06:05:03 -060055 if (next(t) != NULL) {
56 next(t)->dticks += t->dticks;
Andy Ross71f5e562018-12-06 15:39:28 -080057 }
Peter A. Bigot25fbe7b2018-12-30 06:05:03 -060058
59 sys_dlist_remove(&t->node);
Andy Ross987c0e52018-09-27 16:50:00 -070060}
61
Kumar Galaa1b77fd2020-05-27 11:26:57 -050062static int32_t elapsed(void)
Andy Ross987c0e52018-09-27 16:50:00 -070063{
Florian Grandel5fa55342023-06-28 23:17:45 +020064 /* While sys_clock_announce() is executing, new relative timeouts will be
65 * scheduled relatively to the currently firing timeout's original tick
66 * value (=curr_tick) rather than relative to the current
67 * sys_clock_elapsed().
68 *
69 * This means that timeouts being scheduled from within timeout callbacks
70 * will be scheduled at well-defined offsets from the currently firing
71 * timeout.
72 *
73 * As a side effect, the same will happen if an ISR with higher priority
74 * preempts a timeout callback and schedules a timeout.
75 *
76 * The distinction is implemented by looking at announce_remaining which
77 * will be non-zero while sys_clock_announce() is executing and zero
78 * otherwise.
79 */
Anas Nashif9c1efe62021-02-25 15:33:15 -050080 return announce_remaining == 0 ? sys_clock_elapsed() : 0U;
Andy Ross987c0e52018-09-27 16:50:00 -070081}
82
Kumar Galaa1b77fd2020-05-27 11:26:57 -050083static int32_t next_timeout(void)
Andy Rosse664c782019-01-16 08:54:38 -080084{
Andy Rosse664c782019-01-16 08:54:38 -080085 struct _timeout *to = first();
Kumar Galaa1b77fd2020-05-27 11:26:57 -050086 int32_t ticks_elapsed = elapsed();
Flavio Ceolin47b7c2e2022-02-08 15:28:09 -080087 int32_t ret;
88
89 if ((to == NULL) ||
90 ((int64_t)(to->dticks - ticks_elapsed) > (int64_t)INT_MAX)) {
91 ret = MAX_WAIT;
92 } else {
93 ret = MAX(0, to->dticks - ticks_elapsed);
94 }
Andy Rosse664c782019-01-16 08:54:38 -080095
Andy Rosse664c782019-01-16 08:54:38 -080096 return ret;
97}
98
Andy Ross78327382020-03-05 15:18:14 -080099void z_add_timeout(struct _timeout *to, _timeout_func_t fn,
100 k_timeout_t timeout)
Andy Ross987c0e52018-09-27 16:50:00 -0700101{
Andy Ross12bd1872020-04-21 11:07:07 -0700102 if (K_TIMEOUT_EQ(timeout, K_FOREVER)) {
103 return;
104 }
105
Anas Nashif39f632e2020-12-07 13:15:42 -0500106#ifdef CONFIG_KERNEL_COHERENCE
Andy Rossf6d32ab2020-05-13 15:34:04 +0000107 __ASSERT_NO_MSG(arch_mem_coherent(to));
Simon Heinbcd1d192024-03-08 12:00:10 +0100108#endif /* CONFIG_KERNEL_COHERENCE */
Andy Rossf6d32ab2020-05-13 15:34:04 +0000109
Peter A. Bigotb4ece0a2019-01-02 08:29:43 -0600110 __ASSERT(!sys_dnode_is_linked(&to->node), "");
Andy Ross987c0e52018-09-27 16:50:00 -0700111 to->fn = fn;
112
Florian Grandele256b7d2023-07-07 09:12:38 +0200113 K_SPINLOCK(&timeout_lock) {
Andy Ross987c0e52018-09-27 16:50:00 -0700114 struct _timeout *t;
115
Andrzej GÅ‚Ä…bek59b21a22021-05-24 11:24:13 +0200116 if (IS_ENABLED(CONFIG_TIMEOUT_64BIT) &&
Hess Nathan6d417d52024-04-30 13:26:35 +0200117 (Z_TICK_ABS(timeout.ticks) >= 0)) {
Andrzej GÅ‚Ä…bek59b21a22021-05-24 11:24:13 +0200118 k_ticks_t ticks = Z_TICK_ABS(timeout.ticks) - curr_tick;
119
120 to->dticks = MAX(1, ticks);
121 } else {
122 to->dticks = timeout.ticks + 1 + elapsed();
123 }
124
Andy Ross987c0e52018-09-27 16:50:00 -0700125 for (t = first(); t != NULL; t = next(t)) {
Andy Ross987c0e52018-09-27 16:50:00 -0700126 if (t->dticks > to->dticks) {
127 t->dticks -= to->dticks;
Andy Rosseda4c022019-01-28 09:35:27 -0800128 sys_dlist_insert(&t->node, &to->node);
Andy Ross987c0e52018-09-27 16:50:00 -0700129 break;
130 }
131 to->dticks -= t->dticks;
132 }
133
134 if (t == NULL) {
135 sys_dlist_append(&timeout_list, &to->node);
136 }
Andy Ross987c0e52018-09-27 16:50:00 -0700137
Krzysztof Chruścińskiaee8dd12024-03-26 16:33:06 +0100138 if (to == first() && announce_remaining == 0) {
Anas Nashif9c1efe62021-02-25 15:33:15 -0500139 sys_clock_set_timeout(next_timeout(), false);
Pawel Dunajbaea2242018-11-22 11:49:32 +0100140 }
Andy Ross02165d72018-11-20 08:26:34 -0800141 }
Andy Ross987c0e52018-09-27 16:50:00 -0700142}
143
Patrik Flykt4344e272019-03-08 14:19:05 -0700144int z_abort_timeout(struct _timeout *to)
Andy Ross987c0e52018-09-27 16:50:00 -0700145{
Peter A. Bigotb4ece0a2019-01-02 08:29:43 -0600146 int ret = -EINVAL;
Andy Ross987c0e52018-09-27 16:50:00 -0700147
Florian Grandele256b7d2023-07-07 09:12:38 +0200148 K_SPINLOCK(&timeout_lock) {
Peter A. Bigot25fbe7b2018-12-30 06:05:03 -0600149 if (sys_dnode_is_linked(&to->node)) {
Andy Ross386894c2018-10-17 08:29:19 -0700150 remove_timeout(to);
Andy Ross987c0e52018-09-27 16:50:00 -0700151 ret = 0;
152 }
153 }
154
155 return ret;
156}
157
Andy Ross5a5d3da2020-03-09 13:59:15 -0700158/* must be locked */
Peter A. Bigot16a40812020-09-18 16:24:57 -0500159static k_ticks_t timeout_rem(const struct _timeout *timeout)
Andy Ross987c0e52018-09-27 16:50:00 -0700160{
Andy Ross5a5d3da2020-03-09 13:59:15 -0700161 k_ticks_t ticks = 0;
Andy Ross987c0e52018-09-27 16:50:00 -0700162
Andy Ross5a5d3da2020-03-09 13:59:15 -0700163 for (struct _timeout *t = first(); t != NULL; t = next(t)) {
164 ticks += t->dticks;
165 if (timeout == t) {
166 break;
Andy Ross987c0e52018-09-27 16:50:00 -0700167 }
168 }
169
Nicolas Pitrec31d6462024-03-06 11:12:42 -0500170 return ticks;
Andy Ross987c0e52018-09-27 16:50:00 -0700171}
172
Peter A. Bigot16a40812020-09-18 16:24:57 -0500173k_ticks_t z_timeout_remaining(const struct _timeout *timeout)
Andy Ross5a5d3da2020-03-09 13:59:15 -0700174{
175 k_ticks_t ticks = 0;
176
Florian Grandele256b7d2023-07-07 09:12:38 +0200177 K_SPINLOCK(&timeout_lock) {
Nicolas Pitrec31d6462024-03-06 11:12:42 -0500178 if (!z_is_inactive_timeout(timeout)) {
179 ticks = timeout_rem(timeout) - elapsed();
180 }
Andy Ross5a5d3da2020-03-09 13:59:15 -0700181 }
182
183 return ticks;
184}
185
Peter A. Bigot16a40812020-09-18 16:24:57 -0500186k_ticks_t z_timeout_expires(const struct _timeout *timeout)
Andy Ross5a5d3da2020-03-09 13:59:15 -0700187{
188 k_ticks_t ticks = 0;
189
Florian Grandele256b7d2023-07-07 09:12:38 +0200190 K_SPINLOCK(&timeout_lock) {
Nicolas Pitrec31d6462024-03-06 11:12:42 -0500191 ticks = curr_tick;
192 if (!z_is_inactive_timeout(timeout)) {
193 ticks += timeout_rem(timeout);
194 }
Andy Ross5a5d3da2020-03-09 13:59:15 -0700195 }
196
197 return ticks;
198}
199
Kumar Galaa1b77fd2020-05-27 11:26:57 -0500200int32_t z_get_next_timeout_expiry(void)
Andy Ross987c0e52018-09-27 16:50:00 -0700201{
Kumar Galaa1b77fd2020-05-27 11:26:57 -0500202 int32_t ret = (int32_t) K_TICKS_FOREVER;
Andy Ross987c0e52018-09-27 16:50:00 -0700203
Florian Grandele256b7d2023-07-07 09:12:38 +0200204 K_SPINLOCK(&timeout_lock) {
Andy Rosse664c782019-01-16 08:54:38 -0800205 ret = next_timeout();
Andy Ross987c0e52018-09-27 16:50:00 -0700206 }
Andy Ross987c0e52018-09-27 16:50:00 -0700207 return ret;
208}
209
Anas Nashif9c1efe62021-02-25 15:33:15 -0500210void sys_clock_announce(int32_t ticks)
Andy Ross43ab8da2018-12-20 09:23:31 -0800211{
Andy Ross43ab8da2018-12-20 09:23:31 -0800212 k_spinlock_key_t key = k_spin_lock(&timeout_lock);
213
Andy Ross0b2ed382022-04-12 09:52:39 -0700214 /* We release the lock around the callbacks below, so on SMP
215 * systems someone might be already running the loop. Don't
216 * race (which will cause paralllel execution of "sequential"
217 * timeouts and confuse apps), just increment the tick count
218 * and return.
219 */
Peter Mitsis3e2f30a2022-07-29 09:29:32 -0400220 if (IS_ENABLED(CONFIG_SMP) && (announce_remaining != 0)) {
Andy Ross0b2ed382022-04-12 09:52:39 -0700221 announce_remaining += ticks;
222 k_spin_unlock(&timeout_lock, key);
223 return;
224 }
225
Andy Ross43ab8da2018-12-20 09:23:31 -0800226 announce_remaining = ticks;
227
Andrei Emeltchenko9551f272023-08-21 16:00:22 +0300228 struct _timeout *t;
Peter Mitsis42db0962022-12-14 11:53:58 -0500229
230 for (t = first();
231 (t != NULL) && (t->dticks <= announce_remaining);
232 t = first()) {
Andy Ross43ab8da2018-12-20 09:23:31 -0800233 int dt = t->dticks;
234
235 curr_tick += dt;
Andy Ross43ab8da2018-12-20 09:23:31 -0800236 t->dticks = 0;
237 remove_timeout(t);
238
239 k_spin_unlock(&timeout_lock, key);
240 t->fn(t);
241 key = k_spin_lock(&timeout_lock);
Peter Mitsis3e2f30a2022-07-29 09:29:32 -0400242 announce_remaining -= dt;
Andy Ross43ab8da2018-12-20 09:23:31 -0800243 }
244
Peter Mitsis42db0962022-12-14 11:53:58 -0500245 if (t != NULL) {
246 t->dticks -= announce_remaining;
Andy Ross43ab8da2018-12-20 09:23:31 -0800247 }
248
249 curr_tick += announce_remaining;
250 announce_remaining = 0;
251
Anas Nashif9c1efe62021-02-25 15:33:15 -0500252 sys_clock_set_timeout(next_timeout(), false);
Andy Ross43ab8da2018-12-20 09:23:31 -0800253
254 k_spin_unlock(&timeout_lock, key);
Andy Rossf3afd5a2023-03-06 14:31:35 -0800255
256#ifdef CONFIG_TIMESLICING
257 z_time_slice();
Simon Heinbcd1d192024-03-08 12:00:10 +0100258#endif /* CONFIG_TIMESLICING */
Andy Ross43ab8da2018-12-20 09:23:31 -0800259}
260
Anas Nashiffe0872c2021-03-13 08:21:21 -0500261int64_t sys_clock_tick_get(void)
Andy Ross987c0e52018-09-27 16:50:00 -0700262{
Kumar Galaa1b77fd2020-05-27 11:26:57 -0500263 uint64_t t = 0U;
Andy Ross987c0e52018-09-27 16:50:00 -0700264
Florian Grandele256b7d2023-07-07 09:12:38 +0200265 K_SPINLOCK(&timeout_lock) {
Peter Mitsis71ef6692022-08-03 16:11:32 -0400266 t = curr_tick + elapsed();
Andy Ross987c0e52018-09-27 16:50:00 -0700267 }
268 return t;
269}
270
Anas Nashif5c90ceb2021-03-13 08:19:53 -0500271uint32_t sys_clock_tick_get_32(void)
Andy Ross987c0e52018-09-27 16:50:00 -0700272{
Andy Rossd8421ad2018-10-02 11:12:08 -0700273#ifdef CONFIG_TICKLESS_KERNEL
Anas Nashiffe0872c2021-03-13 08:21:21 -0500274 return (uint32_t)sys_clock_tick_get();
Andy Rossd8421ad2018-10-02 11:12:08 -0700275#else
Kumar Galaa1b77fd2020-05-27 11:26:57 -0500276 return (uint32_t)curr_tick;
Simon Heinbcd1d192024-03-08 12:00:10 +0100277#endif /* CONFIG_TICKLESS_KERNEL */
Andy Ross987c0e52018-09-27 16:50:00 -0700278}
279
Kumar Galaa1b77fd2020-05-27 11:26:57 -0500280int64_t z_impl_k_uptime_ticks(void)
Andy Ross987c0e52018-09-27 16:50:00 -0700281{
Anas Nashiffe0872c2021-03-13 08:21:21 -0500282 return sys_clock_tick_get();
Andy Ross987c0e52018-09-27 16:50:00 -0700283}
284
285#ifdef CONFIG_USERSPACE
Kumar Galaa1b77fd2020-05-27 11:26:57 -0500286static inline int64_t z_vrfy_k_uptime_ticks(void)
Andy Ross987c0e52018-09-27 16:50:00 -0700287{
Andy Ross914205c2020-03-10 15:26:38 -0700288 return z_impl_k_uptime_ticks();
Andy Ross987c0e52018-09-27 16:50:00 -0700289}
Andy Ross914205c2020-03-10 15:26:38 -0700290#include <syscalls/k_uptime_ticks_mrsh.c>
Simon Heinbcd1d192024-03-08 12:00:10 +0100291#endif /* CONFIG_USERSPACE */
Andy Ross78327382020-03-05 15:18:14 -0800292
Nicolas Pitre52e2f832023-07-06 13:56:01 -0400293k_timepoint_t sys_timepoint_calc(k_timeout_t timeout)
Andy Ross78327382020-03-05 15:18:14 -0800294{
Nicolas Pitre52e2f832023-07-06 13:56:01 -0400295 k_timepoint_t timepoint;
Andy Ross78327382020-03-05 15:18:14 -0800296
297 if (K_TIMEOUT_EQ(timeout, K_FOREVER)) {
Nicolas Pitre52e2f832023-07-06 13:56:01 -0400298 timepoint.tick = UINT64_MAX;
Andy Ross78327382020-03-05 15:18:14 -0800299 } else if (K_TIMEOUT_EQ(timeout, K_NO_WAIT)) {
Nicolas Pitre52e2f832023-07-06 13:56:01 -0400300 timepoint.tick = 0;
Jennifer Williamsdc11ffb2021-03-20 00:36:55 +0200301 } else {
Nicolas Pitre52e2f832023-07-06 13:56:01 -0400302 k_ticks_t dt = timeout.ticks;
Andy Ross4c7b77a2020-03-09 09:35:35 -0700303
Jennifer Williamsdc11ffb2021-03-20 00:36:55 +0200304 if (IS_ENABLED(CONFIG_TIMEOUT_64BIT) && Z_TICK_ABS(dt) >= 0) {
Nicolas Pitre52e2f832023-07-06 13:56:01 -0400305 timepoint.tick = Z_TICK_ABS(dt);
306 } else {
307 timepoint.tick = sys_clock_tick_get() + MAX(1, dt);
Jennifer Williamsdc11ffb2021-03-20 00:36:55 +0200308 }
Andy Ross4c7b77a2020-03-09 09:35:35 -0700309 }
Nicolas Pitre52e2f832023-07-06 13:56:01 -0400310
311 return timepoint;
312}
313
314k_timeout_t sys_timepoint_timeout(k_timepoint_t timepoint)
315{
316 uint64_t now, remaining;
317
318 if (timepoint.tick == UINT64_MAX) {
319 return K_FOREVER;
320 }
321 if (timepoint.tick == 0) {
322 return K_NO_WAIT;
323 }
324
325 now = sys_clock_tick_get();
326 remaining = (timepoint.tick > now) ? (timepoint.tick - now) : 0;
327 return K_TICKS(remaining);
Andy Ross78327382020-03-05 15:18:14 -0800328}
Chris Friedt4108e142022-12-15 10:14:43 -0800329
330#ifdef CONFIG_ZTEST
331void z_impl_sys_clock_tick_set(uint64_t tick)
332{
333 curr_tick = tick;
334}
335
336void z_vrfy_sys_clock_tick_set(uint64_t tick)
337{
338 z_impl_sys_clock_tick_set(tick);
339}
Simon Heinbcd1d192024-03-08 12:00:10 +0100340#endif /* CONFIG_ZTEST */