blob: 3e31df3cdc6d656b3896e4bd47c5e4d8c03c640b [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/*
2 * Copyright (c) 2013 Google Inc.
3 * Copyright (c) 2015 Travis Geiselbrecht
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files
7 * (the "Software"), to deal in the Software without restriction,
8 * including without limitation the rights to use, copy, modify, merge,
9 * publish, distribute, sublicense, and/or sell copies of the Software,
10 * and to permit persons to whom the Software is furnished to do so,
11 * subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25#include <assert.h>
26#include <compiler.h>
27#include <err.h>
28#include <platform.h>
29
30#include <kernel/thread.h>
31#include <kernel/timer.h>
32#include <kernel/spinlock.h>
33
34#include <lib/watchdog.h>
35
36static spin_lock_t lock = SPIN_LOCK_INITIAL_VALUE;
37
38__WEAK void watchdog_handler(watchdog_t* dog)
39{
40 dprintf(INFO, "Watchdog \"%s\" (timeout %u mSec) just fired!!\n",
41 dog->name, (uint32_t)dog->timeout);
42 platform_halt(HALT_ACTION_HALT, HALT_REASON_SW_WATCHDOG);
43}
44
45static enum handler_return watchdog_timer_callback(struct timer* timer, lk_time_t now, void* arg)
46{
47 watchdog_handler((watchdog_t*)arg);
48
49 /* We should never get here; watchdog handlers should always be fatal. */
50 DEBUG_ASSERT(false);
51
52 return INT_NO_RESCHEDULE;
53}
54
55status_t watchdog_init(watchdog_t* dog, lk_time_t timeout, const char* name)
56{
57 DEBUG_ASSERT(NULL != dog);
58 DEBUG_ASSERT(INFINITE_TIME != timeout);
59
60 dog->magic = WATCHDOG_MAGIC;
61 dog->name = name ? name : "unnamed watchdog";
62 dog->enabled = false;
63 dog->timeout = timeout;
64 timer_initialize(&dog->expire_timer);
65
66 return NO_ERROR;
67}
68
69void watchdog_set_enabled(watchdog_t* dog, bool enabled)
70{
71 spin_lock_saved_state_t state;
72 spin_lock_irqsave(&lock, state);
73
74 DEBUG_ASSERT((NULL != dog) && (WATCHDOG_MAGIC == dog->magic));
75
76 if (dog->enabled == enabled)
77 goto done;
78
79 dog->enabled = enabled;
80 if (enabled)
81 timer_set_oneshot(&dog->expire_timer, dog->timeout, watchdog_timer_callback, dog);
82 else
83 timer_cancel(&dog->expire_timer);
84
85done:
86 spin_unlock_irqrestore(&lock, state);
87}
88
89void watchdog_pet(watchdog_t* dog)
90{
91 spin_lock_saved_state_t state;
92 spin_lock_irqsave(&lock, state);
93
94 DEBUG_ASSERT((NULL != dog) && (WATCHDOG_MAGIC == dog->magic));
95
96 if (!dog->enabled)
97 goto done;
98
99 timer_cancel(&dog->expire_timer);
100 timer_set_oneshot(&dog->expire_timer, dog->timeout, watchdog_timer_callback, dog);
101
102done:
103 spin_unlock_irqrestore(&lock, state);
104}
105
106
107static timer_t hw_watchdog_timer;
108static bool hw_watchdog_enabled;
109static lk_time_t hw_watchdog_pet_timeout;
110
111static enum handler_return hw_watchdog_timer_callback(struct timer* timer, lk_time_t now, void* arg)
112{
113 platform_watchdog_pet();
114 return INT_NO_RESCHEDULE;
115}
116
117status_t watchdog_hw_init(lk_time_t timeout)
118{
119 DEBUG_ASSERT(INFINITE_TIME != timeout);
120 timer_initialize(&hw_watchdog_timer);
121 return platform_watchdog_init(timeout, &hw_watchdog_pet_timeout);
122}
123
124void watchdog_hw_set_enabled(bool enabled)
125{
126 spin_lock_saved_state_t state;
127 spin_lock_irqsave(&lock, state);
128
129 if (hw_watchdog_enabled == enabled)
130 goto done;
131
132 hw_watchdog_enabled = enabled;
133 platform_watchdog_set_enabled(enabled);
134 if (enabled)
135 timer_set_periodic(&hw_watchdog_timer,
136 hw_watchdog_pet_timeout,
137 hw_watchdog_timer_callback,
138 NULL);
139 else
140 timer_cancel(&hw_watchdog_timer);
141
142done:
143 spin_unlock_irqrestore(&lock, state);
144}