blob: 325224d11bd87a98027fa20b149d71222e4e9733 [file] [log] [blame]
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001/*
2 * Copyright (c) 1997-2016 Wind River Systems, Inc.
3 *
David B. Kinderac74d8b2017-01-18 17:01:01 -08004 * SPDX-License-Identifier: Apache-2.0
Benjamin Walsh456c6da2016-09-02 18:55:39 -04005 */
6
Gerard Marull-Paretascffefc82022-05-06 11:04:23 +02007#include <zephyr/kernel.h>
Anas Nashif4d994af2021-04-18 23:24:40 -04008
Gerard Marull-Paretascffefc82022-05-06 11:04:23 +02009#include <zephyr/init.h>
Stephanos Ioannidis2d746042019-10-25 00:08:21 +090010#include <ksched.h>
Gerard Marull-Paretascffefc82022-05-06 11:04:23 +020011#include <zephyr/wait_q.h>
12#include <zephyr/syscall_handler.h>
Flavio Ceolin76b35182018-12-16 12:48:29 -080013#include <stdbool.h>
Gerard Marull-Paretascffefc82022-05-06 11:04:23 +020014#include <zephyr/spinlock.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040015
Andy Rossb29fb222019-02-05 16:19:30 -080016static struct k_spinlock lock;
17
Benjamin Walsh456c6da2016-09-02 18:55:39 -040018/**
Allan Stephens45bfa372016-10-12 12:39:42 -050019 * @brief Handle expiration of a kernel timer object.
Benjamin Walsh456c6da2016-09-02 18:55:39 -040020 *
Allan Stephens45bfa372016-10-12 12:39:42 -050021 * @param t Timeout used by the timer.
Benjamin Walsh456c6da2016-09-02 18:55:39 -040022 */
Patrik Flykt4344e272019-03-08 14:19:05 -070023void z_timer_expiration_handler(struct _timeout *t)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040024{
Benjamin Walsh456c6da2016-09-02 18:55:39 -040025 struct k_timer *timer = CONTAINER_OF(t, struct k_timer, timeout);
Benjamin Walshb889fa82016-12-07 22:39:31 -050026 struct k_thread *thread;
Chen Peng1dde3d6c2021-09-29 10:21:58 +080027 k_spinlock_key_t key = k_spin_lock(&lock);
Benjamin Walsh456c6da2016-09-02 18:55:39 -040028
Andrzej Głąbeke60af792023-02-07 16:21:54 +010029 /* In sys_clock_announce(), when a timeout expires, it is first removed
30 * from the timeout list, then its expiration handler is called (with
31 * unlocked interrupts). For kernel timers, the expiration handler is
32 * this function. Usually, the timeout structure related to the timer
33 * that is handled here will not be linked to the timeout list at this
34 * point. But it may happen that before this function is executed and
35 * interrupts are locked again, a given timer gets restarted from an
36 * interrupt context that has a priority higher than the system timer
37 * interrupt. Then, the timeout structure for this timer will turn out
38 * to be linked to the timeout list. And in such case, since the timer
39 * was restarted, its expiration handler should not be executed then,
40 * so the function exits immediately.
41 */
42 if (sys_dnode_is_linked(&t->node)) {
43 k_spin_unlock(&lock, key);
44 return;
45 }
46
Allan Stephens6c98c4d2016-10-17 14:34:53 -050047 /*
48 * if the timer is periodic, start it again; don't add _TICK_ALIGN
49 * since we're already aligned to a tick boundary
50 */
Andy Ross78327382020-03-05 15:18:14 -080051 if (!K_TIMEOUT_EQ(timer->period, K_NO_WAIT) &&
52 !K_TIMEOUT_EQ(timer->period, K_FOREVER)) {
Andy Ross7a59ceb2022-04-07 13:49:20 -070053 k_timeout_t next = timer->period;
54
55#ifdef CONFIG_TIMEOUT_64BIT
56 /* Exploit the fact that uptime during a kernel
57 * timeout handler reflects the time of the scheduled
58 * event and not real time to get some inexpensive
59 * protection against late interrupts. If we're
60 * delayed for any reason, we still end up calculating
61 * the next expiration as a regular stride from where
62 * we "should" have run. Requires absolute timeouts.
63 * (Note offset by one: we're nominally at the
64 * beginning of a tick, so need to defeat the "round
65 * down" behavior on timeout addition).
66 */
67 next = K_TIMEOUT_ABS_TICKS(k_uptime_ticks() + 1
68 + timer->period.ticks);
69#endif
Patrik Flykt4344e272019-03-08 14:19:05 -070070 z_add_timeout(&timer->timeout, z_timer_expiration_handler,
Andy Ross7a59ceb2022-04-07 13:49:20 -070071 next);
Benjamin Walsh456c6da2016-09-02 18:55:39 -040072 }
73
Allan Stephens45bfa372016-10-12 12:39:42 -050074 /* update timer's status */
Patrik Flykt24d71432019-03-26 19:57:45 -060075 timer->status += 1U;
Allan Stephens45bfa372016-10-12 12:39:42 -050076
77 /* invoke timer expiry function */
Flavio Ceolin76b35182018-12-16 12:48:29 -080078 if (timer->expiry_fn != NULL) {
Krzysztof Chruscinski8979fbc2021-11-03 16:24:11 +010079 /* Unlock for user handler. */
80 k_spin_unlock(&lock, key);
Allan Stephens45bfa372016-10-12 12:39:42 -050081 timer->expiry_fn(timer);
Krzysztof Chruscinski8979fbc2021-11-03 16:24:11 +010082 key = k_spin_lock(&lock);
Benjamin Walsh456c6da2016-09-02 18:55:39 -040083 }
Benjamin Walshb889fa82016-12-07 22:39:31 -050084
Krzysztof Chruscinskidd0715c2021-04-14 13:36:58 +020085 if (!IS_ENABLED(CONFIG_MULTITHREADING)) {
Chen Peng1dde3d6c2021-09-29 10:21:58 +080086 k_spin_unlock(&lock, key);
Krzysztof Chruscinskidd0715c2021-04-14 13:36:58 +020087 return;
88 }
89
Patrik Flykt4344e272019-03-08 14:19:05 -070090 thread = z_waitq_head(&timer->wait_q);
Benjamin Walshb889fa82016-12-07 22:39:31 -050091
Flavio Ceolin4218d5f2018-09-17 09:39:51 -070092 if (thread == NULL) {
Chen Peng1dde3d6c2021-09-29 10:21:58 +080093 k_spin_unlock(&lock, key);
Benjamin Walshb889fa82016-12-07 22:39:31 -050094 return;
Benjamin Walsh456c6da2016-09-02 18:55:39 -040095 }
Allan Stephens45bfa372016-10-12 12:39:42 -050096
Patrik Flykt4344e272019-03-08 14:19:05 -070097 z_unpend_thread_no_timeout(thread);
Benjamin Walshb889fa82016-12-07 22:39:31 -050098
Andrew Boie4f77c2a2019-11-07 12:43:29 -080099 arch_thread_return_value_set(thread, 0);
James Harrisc7bb4232021-03-02 13:22:52 -0800100
Chen Peng1dde3d6c2021-09-29 10:21:58 +0800101 k_spin_unlock(&lock, key);
102
James Harrisc7bb4232021-03-02 13:22:52 -0800103 z_ready_thread(thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400104}
105
106
Allan Stephens45bfa372016-10-12 12:39:42 -0500107void k_timer_init(struct k_timer *timer,
Flavio Ceolin118715c2018-11-16 19:52:37 -0800108 k_timer_expiry_t expiry_fn,
109 k_timer_stop_t stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400110{
Allan Stephens45bfa372016-10-12 12:39:42 -0500111 timer->expiry_fn = expiry_fn;
112 timer->stop_fn = stop_fn;
Patrik Flykt24d71432019-03-26 19:57:45 -0600113 timer->status = 0U;
Allan Stephens45bfa372016-10-12 12:39:42 -0500114
Krzysztof Chruscinskidd0715c2021-04-14 13:36:58 +0200115 if (IS_ENABLED(CONFIG_MULTITHREADING)) {
116 z_waitq_init(&timer->wait_q);
117 }
118
Peter A. Bigot5639ea02019-09-27 09:20:26 -0500119 z_init_timeout(&timer->timeout);
Torbjörn Leksell3a66d6c2021-03-26 14:09:10 +0100120
121 SYS_PORT_TRACING_OBJ_INIT(k_timer, timer);
122
Maciek Borzecki4fef7602017-05-18 08:49:50 +0200123 timer->user_data = NULL;
Andrew Boie945af952017-08-22 13:15:23 -0700124
Patrik Flykt4344e272019-03-08 14:19:05 -0700125 z_object_init(timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400126}
127
128
Andy Ross78327382020-03-05 15:18:14 -0800129void z_impl_k_timer_start(struct k_timer *timer, k_timeout_t duration,
130 k_timeout_t period)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400131{
Anas Nashif6a9540a2022-04-12 15:29:37 -0400132 SYS_PORT_TRACING_OBJ_FUNC(k_timer, start, timer, duration, period);
Torbjörn Leksell3a66d6c2021-03-26 14:09:10 +0100133
Andy Rossa343cf92020-05-29 07:36:39 -0700134 if (K_TIMEOUT_EQ(duration, K_FOREVER)) {
135 return;
136 }
137
Andy Ross78327382020-03-05 15:18:14 -0800138 /* z_add_timeout() always adds one to the incoming tick count
139 * to round up to the next tick (by convention it waits for
140 * "at least as long as the specified timeout"), but the
141 * period interval is always guaranteed to be reset from
142 * within the timer ISR, so no round up is desired. Subtract
143 * one.
144 *
145 * Note that the duration (!) value gets the same treatment
146 * for backwards compatibility. This is unfortunate
147 * (i.e. k_timer_start() doesn't treat its initial sleep
148 * argument the same way k_sleep() does), but historical. The
149 * timer_api test relies on this behavior.
150 */
Eric Johnsonb4aeef42021-03-05 08:31:50 -0600151 if (!K_TIMEOUT_EQ(period, K_FOREVER) && period.ticks != 0 &&
152 Z_TICK_ABS(period.ticks) < 0) {
Andy Ross3e729b22020-04-23 10:05:31 -0700153 period.ticks = MAX(period.ticks - 1, 1);
154 }
Andy Ross4c7b77a2020-03-09 09:35:35 -0700155 if (Z_TICK_ABS(duration.ticks) < 0) {
156 duration.ticks = MAX(duration.ticks - 1, 0);
157 }
Benjamin Walsh6ca6c282016-12-09 13:39:00 -0500158
Patrik Flykt4344e272019-03-08 14:19:05 -0700159 (void)z_abort_timeout(&timer->timeout);
Andy Ross78327382020-03-05 15:18:14 -0800160 timer->period = period;
Patrik Flykt24d71432019-03-26 19:57:45 -0600161 timer->status = 0U;
Andy Ross78327382020-03-05 15:18:14 -0800162
Patrik Flykt4344e272019-03-08 14:19:05 -0700163 z_add_timeout(&timer->timeout, z_timer_expiration_handler,
Andy Ross78327382020-03-05 15:18:14 -0800164 duration);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400165}
166
Andrew Boiea354d492017-09-29 16:22:28 -0700167#ifdef CONFIG_USERSPACE
Andy Ross643701a2019-08-13 12:58:38 -0700168static inline void z_vrfy_k_timer_start(struct k_timer *timer,
Andy Ross78327382020-03-05 15:18:14 -0800169 k_timeout_t duration,
170 k_timeout_t period)
Andrew Boiea354d492017-09-29 16:22:28 -0700171{
Andrew Boie8345e5e2018-05-04 15:57:57 -0700172 Z_OOPS(Z_SYSCALL_OBJ(timer, K_OBJ_TIMER));
Andy Ross65649742019-08-06 13:34:31 -0700173 z_impl_k_timer_start(timer, duration, period);
Andrew Boiea354d492017-09-29 16:22:28 -0700174}
Andy Ross65649742019-08-06 13:34:31 -0700175#include <syscalls/k_timer_start_mrsh.c>
Andrew Boiea354d492017-09-29 16:22:28 -0700176#endif
177
Patrik Flykt4344e272019-03-08 14:19:05 -0700178void z_impl_k_timer_stop(struct k_timer *timer)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400179{
Torbjörn Leksell3a66d6c2021-03-26 14:09:10 +0100180 SYS_PORT_TRACING_OBJ_FUNC(k_timer, stop, timer);
181
Simon Hein02cfbfe2022-07-19 22:30:17 +0200182 bool inactive = (z_abort_timeout(&timer->timeout) != 0);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400183
Benjamin Walshd211a522016-12-06 11:44:01 -0500184 if (inactive) {
Allan Stephens45bfa372016-10-12 12:39:42 -0500185 return;
186 }
187
Flavio Ceolin76b35182018-12-16 12:48:29 -0800188 if (timer->stop_fn != NULL) {
Allan Stephens45bfa372016-10-12 12:39:42 -0500189 timer->stop_fn(timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400190 }
191
Krzysztof Chruscinskidd0715c2021-04-14 13:36:58 +0200192 if (IS_ENABLED(CONFIG_MULTITHREADING)) {
193 struct k_thread *pending_thread = z_unpend1_no_timeout(&timer->wait_q);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400194
Krzysztof Chruscinskidd0715c2021-04-14 13:36:58 +0200195 if (pending_thread != NULL) {
196 z_ready_thread(pending_thread);
197 z_reschedule_unlocked();
198 }
Allan Stephens45bfa372016-10-12 12:39:42 -0500199 }
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400200}
201
Andrew Boiea354d492017-09-29 16:22:28 -0700202#ifdef CONFIG_USERSPACE
Andy Ross65649742019-08-06 13:34:31 -0700203static inline void z_vrfy_k_timer_stop(struct k_timer *timer)
204{
205 Z_OOPS(Z_SYSCALL_OBJ(timer, K_OBJ_TIMER));
206 z_impl_k_timer_stop(timer);
207}
208#include <syscalls/k_timer_stop_mrsh.c>
Andrew Boiea354d492017-09-29 16:22:28 -0700209#endif
210
Kumar Galaa1b77fd2020-05-27 11:26:57 -0500211uint32_t z_impl_k_timer_status_get(struct k_timer *timer)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400212{
Andy Rossb29fb222019-02-05 16:19:30 -0800213 k_spinlock_key_t key = k_spin_lock(&lock);
Kumar Galaa1b77fd2020-05-27 11:26:57 -0500214 uint32_t result = timer->status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400215
Patrik Flykt24d71432019-03-26 19:57:45 -0600216 timer->status = 0U;
Andy Rossb29fb222019-02-05 16:19:30 -0800217 k_spin_unlock(&lock, key);
Allan Stephens45bfa372016-10-12 12:39:42 -0500218
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400219 return result;
220}
221
Andrew Boiea354d492017-09-29 16:22:28 -0700222#ifdef CONFIG_USERSPACE
Kumar Galaa1b77fd2020-05-27 11:26:57 -0500223static inline uint32_t z_vrfy_k_timer_status_get(struct k_timer *timer)
Andy Ross65649742019-08-06 13:34:31 -0700224{
225 Z_OOPS(Z_SYSCALL_OBJ(timer, K_OBJ_TIMER));
226 return z_impl_k_timer_status_get(timer);
227}
228#include <syscalls/k_timer_status_get_mrsh.c>
Andrew Boiea354d492017-09-29 16:22:28 -0700229#endif
230
Kumar Galaa1b77fd2020-05-27 11:26:57 -0500231uint32_t z_impl_k_timer_status_sync(struct k_timer *timer)
Allan Stephens45bfa372016-10-12 12:39:42 -0500232{
Andrew Boie4f77c2a2019-11-07 12:43:29 -0800233 __ASSERT(!arch_is_in_isr(), "");
Torbjörn Leksell3a66d6c2021-03-26 14:09:10 +0100234 SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_timer, status_sync, timer);
Allan Stephens45bfa372016-10-12 12:39:42 -0500235
Krzysztof Chruscinskidd0715c2021-04-14 13:36:58 +0200236 if (!IS_ENABLED(CONFIG_MULTITHREADING)) {
237 uint32_t result;
238
239 do {
240 k_spinlock_key_t key = k_spin_lock(&lock);
241
242 if (!z_is_inactive_timeout(&timer->timeout)) {
243 result = *(volatile uint32_t *)&timer->status;
244 timer->status = 0U;
245 k_spin_unlock(&lock, key);
246 if (result > 0) {
247 break;
248 }
249 } else {
250 result = timer->status;
251 k_spin_unlock(&lock, key);
252 break;
253 }
254 } while (true);
255
256 return result;
257 }
258
Andy Rossb29fb222019-02-05 16:19:30 -0800259 k_spinlock_key_t key = k_spin_lock(&lock);
Kumar Galaa1b77fd2020-05-27 11:26:57 -0500260 uint32_t result = timer->status;
Allan Stephens45bfa372016-10-12 12:39:42 -0500261
Patrik Flykt24d71432019-03-26 19:57:45 -0600262 if (result == 0U) {
Patrik Flykt4344e272019-03-08 14:19:05 -0700263 if (!z_is_inactive_timeout(&timer->timeout)) {
Torbjörn Leksell3a66d6c2021-03-26 14:09:10 +0100264 SYS_PORT_TRACING_OBJ_FUNC_BLOCKING(k_timer, status_sync, timer, K_FOREVER);
265
Allan Stephens45bfa372016-10-12 12:39:42 -0500266 /* wait for timer to expire or stop */
Patrik Flykt4344e272019-03-08 14:19:05 -0700267 (void)z_pend_curr(&lock, key, &timer->wait_q, K_FOREVER);
Allan Stephens45bfa372016-10-12 12:39:42 -0500268
269 /* get updated timer status */
Andy Rossb29fb222019-02-05 16:19:30 -0800270 key = k_spin_lock(&lock);
Allan Stephens45bfa372016-10-12 12:39:42 -0500271 result = timer->status;
272 } else {
273 /* timer is already stopped */
274 }
275 } else {
276 /* timer has already expired at least once */
277 }
278
Patrik Flykt24d71432019-03-26 19:57:45 -0600279 timer->status = 0U;
Andy Rossb29fb222019-02-05 16:19:30 -0800280 k_spin_unlock(&lock, key);
Allan Stephens45bfa372016-10-12 12:39:42 -0500281
Torbjörn Leksell3a66d6c2021-03-26 14:09:10 +0100282 /**
283 * @note New tracing hook
284 */
285 SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_timer, status_sync, timer, result);
286
Allan Stephens45bfa372016-10-12 12:39:42 -0500287 return result;
288}
289
Andrew Boiea354d492017-09-29 16:22:28 -0700290#ifdef CONFIG_USERSPACE
Kumar Galaa1b77fd2020-05-27 11:26:57 -0500291static inline uint32_t z_vrfy_k_timer_status_sync(struct k_timer *timer)
Andrew Boie225e4c02017-10-12 09:54:26 -0700292{
Andrew Boie8345e5e2018-05-04 15:57:57 -0700293 Z_OOPS(Z_SYSCALL_OBJ(timer, K_OBJ_TIMER));
Andy Ross65649742019-08-06 13:34:31 -0700294 return z_impl_k_timer_status_sync(timer);
Andrew Boie225e4c02017-10-12 09:54:26 -0700295}
Andy Ross65649742019-08-06 13:34:31 -0700296#include <syscalls/k_timer_status_sync_mrsh.c>
297
Peter Bigot0ab314f2020-11-16 15:28:59 -0600298static inline k_ticks_t z_vrfy_k_timer_remaining_ticks(
299 const struct k_timer *timer)
Andy Ross65649742019-08-06 13:34:31 -0700300{
301 Z_OOPS(Z_SYSCALL_OBJ(timer, K_OBJ_TIMER));
Andy Ross5a5d3da2020-03-09 13:59:15 -0700302 return z_impl_k_timer_remaining_ticks(timer);
Andy Ross65649742019-08-06 13:34:31 -0700303}
Andy Ross5a5d3da2020-03-09 13:59:15 -0700304#include <syscalls/k_timer_remaining_ticks_mrsh.c>
305
Peter Bigot0ab314f2020-11-16 15:28:59 -0600306static inline k_ticks_t z_vrfy_k_timer_expires_ticks(
307 const struct k_timer *timer)
Andy Ross5a5d3da2020-03-09 13:59:15 -0700308{
309 Z_OOPS(Z_SYSCALL_OBJ(timer, K_OBJ_TIMER));
310 return z_impl_k_timer_expires_ticks(timer);
311}
312#include <syscalls/k_timer_expires_ticks_mrsh.c>
Andy Ross65649742019-08-06 13:34:31 -0700313
Peter A. Bigotf1b86ca2020-09-18 16:24:57 -0500314static inline void *z_vrfy_k_timer_user_data_get(const struct k_timer *timer)
Andy Ross65649742019-08-06 13:34:31 -0700315{
316 Z_OOPS(Z_SYSCALL_OBJ(timer, K_OBJ_TIMER));
317 return z_impl_k_timer_user_data_get(timer);
318}
319#include <syscalls/k_timer_user_data_get_mrsh.c>
320
Andy Ross643701a2019-08-13 12:58:38 -0700321static inline void z_vrfy_k_timer_user_data_set(struct k_timer *timer,
322 void *user_data)
Andy Ross65649742019-08-06 13:34:31 -0700323{
324 Z_OOPS(Z_SYSCALL_OBJ(timer, K_OBJ_TIMER));
325 z_impl_k_timer_user_data_set(timer, user_data);
326}
327#include <syscalls/k_timer_user_data_set_mrsh.c>
328
Andrew Boie225e4c02017-10-12 09:54:26 -0700329#endif