./
Coverage:
low: ≥ 0%
medium: ≥ 50.0%
high: ≥ 80.0%
Lines:
110 / 113
97.3%
Branches:
31 / 38
81.6%
Functions:
10 / 10
100.0%

kfsw-platform/src/watchdog.c
Line Branch Exec Source
1 #include <errno.h>
2 #include <stdbool.h>
3 #include <stdint.h>
4
5 #include <zephyr/device.h>
6 #include <zephyr/devicetree.h>
7 #include <zephyr/drivers/watchdog.h>
8 #include <zephyr/kernel.h>
9 #include <zephyr/sys/util.h>
10
11 #include <kfsw/platform/time.h>
12 #include <kfsw/platform/watchdog.h>
13
14 /* The board's watchdog0 alias is the window watchdog, so the device comes
15 * from the kfsw,watchdog chosen property instead.
16 */
17 #define KFSW_WATCHDOG_NODE DT_CHOSEN(kfsw_watchdog)
18 #define KFSW_WATCHDOG_PRESENT DT_NODE_HAS_STATUS(KFSW_WATCHDOG_NODE, okay)
19
20 /* Feeding at a third of the timeout leaves room for two consecutive misses. */
21 #define KFSW_WATCHDOG_FEED_DIVISOR 3U
22
23 static struct k_spinlock watchdog_lock;
24 static struct kfsw_platform_watchdog_info watchdog_state = {
25 .state = KFSW_PLATFORM_WATCHDOG_UNCONFIGURED,
26 };
27 static uint64_t watchdog_last_feed_ms;
28
29 #if KFSW_WATCHDOG_PRESENT
30 static const struct device *const watchdog_device = DEVICE_DT_GET(KFSW_WATCHDOG_NODE);
31 static int watchdog_channel = -1;
32 K_MUTEX_DEFINE(watchdog_handover_lock);
33 static struct k_work_sync watchdog_release_sync;
34
35 static void watchdog_keepalive_handler(struct k_work *work);
36 K_WORK_DELAYABLE_DEFINE(watchdog_keepalive, watchdog_keepalive_handler);
37 #endif
38
39 664 uint32_t kfsw_platform_watchdog_feed_interval_ms(uint32_t timeout_ms)
40 {
41 664 uint32_t interval = timeout_ms / KFSW_WATCHDOG_FEED_DIVISOR;
42
43 /* A timeout below the divisor would give a zero feed interval. */
44
2/2
✓ Branch 0 taken 658 times.
✓ Branch 1 taken 6 times.
664 return (interval == 0U) ? 1U : interval;
45 }
46
47 20 uint32_t kfsw_platform_watchdog_configured_timeout_ms(void)
48 {
49 #if KFSW_WATCHDOG_PRESENT
50 2 return CONFIG_KFSW_WATCHDOG_TIMEOUT_MS;
51 #else
52 18 return 0U;
53 #endif
54 }
55
56 #if KFSW_WATCHDOG_PRESENT
57 /* Caller holds watchdog_lock. */
58 8 static int watchdog_feed_locked(void)
59 {
60 int result;
61
62 8 result = wdt_feed(watchdog_device, watchdog_channel);
63
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 6 times.
8 if (result != 0) {
64 2 return result;
65 }
66
67
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (watchdog_state.feeds < UINT32_MAX) {
68 6 watchdog_state.feeds++;
69 }
70 6 watchdog_last_feed_ms = kfsw_time_monotonic_ms();
71 6 return 0;
72 }
73
74 2 static void watchdog_keepalive_handler(struct k_work *work)
75 {
76 k_spinlock_key_t key;
77
78 ARG_UNUSED(work);
79
80 2 key = k_spin_lock(&watchdog_lock);
81
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (watchdog_state.keepalive_owned &&
82
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 (watchdog_state.state == KFSW_PLATFORM_WATCHDOG_RUNNING)) {
83 2 (void)watchdog_feed_locked();
84 2 (void)k_work_reschedule(&watchdog_keepalive,
85 2 K_MSEC(watchdog_state.feed_interval_ms));
86 }
87 2 k_spin_unlock(&watchdog_lock, key);
88 2 }
89 #endif /* KFSW_WATCHDOG_PRESENT */
90
91 8 int kfsw_platform_watchdog_init(void)
92 {
93 #if KFSW_WATCHDOG_PRESENT
94 4 const struct wdt_timeout_cfg timeout = {
95 .window =
96 {
97 .min = 0U,
98 .max = CONFIG_KFSW_WATCHDOG_TIMEOUT_MS,
99 },
100 /* No callback, so the reset doesn't depend on an interrupt. */
101 .callback = NULL,
102 .flags = WDT_FLAG_RESET_SOC,
103 };
104 k_spinlock_key_t key;
105 int result;
106
107
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
4 if (!device_is_ready(watchdog_device)) {
108 ✗ return -ENODEV;
109 }
110
111 4 key = k_spin_lock(&watchdog_lock);
112
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 if (watchdog_state.state != KFSW_PLATFORM_WATCHDOG_UNCONFIGURED) {
113 2 k_spin_unlock(&watchdog_lock, key);
114 2 return -EALREADY;
115 }
116 2 k_spin_unlock(&watchdog_lock, key);
117
118 2 result = wdt_install_timeout(watchdog_device, &timeout);
119
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (result < 0) {
120 ✗ return result;
121 }
122
123 2 key = k_spin_lock(&watchdog_lock);
124 2 watchdog_channel = result;
125 2 watchdog_state.timeout_ms = CONFIG_KFSW_WATCHDOG_TIMEOUT_MS;
126 2 watchdog_state.feed_interval_ms =
127 2 kfsw_platform_watchdog_feed_interval_ms(CONFIG_KFSW_WATCHDOG_TIMEOUT_MS);
128 2 watchdog_state.device_bound = true;
129 2 watchdog_state.state = KFSW_PLATFORM_WATCHDOG_CONFIGURED;
130 2 k_spin_unlock(&watchdog_lock, key);
131
132 2 return 0;
133 #else
134 4 return -ENODEV;
135 #endif
136 }
137
138 8 int kfsw_platform_watchdog_start(void)
139 {
140 #if KFSW_WATCHDOG_PRESENT
141 k_spinlock_key_t key;
142 uint32_t interval;
143 int result;
144
145 6 key = k_spin_lock(&watchdog_lock);
146
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
6 if (watchdog_state.state != KFSW_PLATFORM_WATCHDOG_CONFIGURED) {
147 4 k_spin_unlock(&watchdog_lock, key);
148 4 return -EINVAL;
149 }
150 2 interval = watchdog_state.feed_interval_ms;
151 2 k_spin_unlock(&watchdog_lock, key);
152
153 /* Pause while halted in the debugger, where the driver supports it. */
154 2 result = wdt_setup(watchdog_device, WDT_OPT_PAUSE_HALTED_BY_DBG);
155
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (result == -ENOTSUP) {
156 2 result = wdt_setup(watchdog_device, 0);
157 }
158
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (result != 0) {
159 ✗ return result;
160 }
161
162 2 key = k_spin_lock(&watchdog_lock);
163 2 watchdog_state.state = KFSW_PLATFORM_WATCHDOG_RUNNING;
164 2 watchdog_state.keepalive_owned = true;
165 2 (void)watchdog_feed_locked();
166 2 (void)k_work_reschedule(&watchdog_keepalive, K_MSEC(interval));
167 2 k_spin_unlock(&watchdog_lock, key);
168
169 2 return 0;
170 #else
171 2 return -ENODEV;
172 #endif
173 }
174
175 10 int kfsw_platform_watchdog_feed(void)
176 {
177 #if KFSW_WATCHDOG_PRESENT
178 k_spinlock_key_t key;
179 int result;
180
181 8 key = k_spin_lock(&watchdog_lock);
182
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 6 times.
8 if (watchdog_state.state == KFSW_PLATFORM_WATCHDOG_STARVED) {
183 2 k_spin_unlock(&watchdog_lock, key);
184 2 return -EPERM;
185 }
186
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4 times.
6 if (watchdog_state.state != KFSW_PLATFORM_WATCHDOG_RUNNING) {
187 2 k_spin_unlock(&watchdog_lock, key);
188 2 return -EINVAL;
189 }
190 4 result = watchdog_feed_locked();
191 4 k_spin_unlock(&watchdog_lock, key);
192
193 4 return result;
194 #else
195 2 return -ENODEV;
196 #endif
197 }
198
199 14 int kfsw_platform_watchdog_release(void)
200 {
201 #if KFSW_WATCHDOG_PRESENT
202 k_spinlock_key_t key;
203
204
4/4
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 2 times.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 6 times.
10 if (!k_is_preempt_thread() || (k_current_get() == k_work_queue_thread_get(&k_sys_work_q))) {
205 4 return -EWOULDBLOCK;
206 }
207 6 k_mutex_lock(&watchdog_handover_lock, K_FOREVER);
208 6 key = k_spin_lock(&watchdog_lock);
209
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
6 if (watchdog_state.state != KFSW_PLATFORM_WATCHDOG_RUNNING) {
210 4 k_spin_unlock(&watchdog_lock, key);
211 4 k_mutex_unlock(&watchdog_handover_lock);
212 4 return -EINVAL;
213 }
214 2 watchdog_state.keepalive_owned = false;
215 2 k_spin_unlock(&watchdog_lock, key);
216
217 /* Wait for a handler that was already running before the handover. */
218 2 (void)k_work_cancel_delayable_sync(&watchdog_keepalive, &watchdog_release_sync);
219 2 k_mutex_unlock(&watchdog_handover_lock);
220 2 return 0;
221 #else
222 4 return -ENODEV;
223 #endif
224 }
225
226 8 int kfsw_platform_watchdog_stop_feeding(void)
227 {
228 #if KFSW_WATCHDOG_PRESENT
229 k_spinlock_key_t key;
230
231 6 key = k_spin_lock(&watchdog_lock);
232
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
6 if (watchdog_state.state != KFSW_PLATFORM_WATCHDOG_RUNNING) {
233 4 k_spin_unlock(&watchdog_lock, key);
234 4 return -EINVAL;
235 }
236 2 watchdog_state.state = KFSW_PLATFORM_WATCHDOG_STARVED;
237 2 k_spin_unlock(&watchdog_lock, key);
238
239 /* Don't wait here; the handler checks the state before feeding. */
240 2 (void)k_work_cancel_delayable(&watchdog_keepalive);
241 2 return 0;
242 #else
243 2 return -ENODEV;
244 #endif
245 }
246
247 122 int kfsw_platform_watchdog_get_info(struct kfsw_platform_watchdog_info *info)
248 {
249 k_spinlock_key_t key;
250 uint64_t now;
251
252
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 120 times.
122 if (info == NULL) {
253 2 return -EINVAL;
254 }
255
256 120 now = kfsw_time_monotonic_ms();
257
258 120 key = k_spin_lock(&watchdog_lock);
259 120 *info = watchdog_state;
260
2/2
✓ Branch 0 taken 110 times.
✓ Branch 1 taken 10 times.
120 if (watchdog_state.feeds == 0U) {
261 110 info->since_feed_ms = 0U;
262 } else {
263 10 info->since_feed_ms = (uint32_t)(now - watchdog_last_feed_ms);
264 }
265 120 k_spin_unlock(&watchdog_lock, key);
266
267 120 return 0;
268 }
269