1 /*
2  * Copyright (c) 2013 Google Inc.
3  * Copyright (c) 2015 Travis Geiselbrecht
4  *
5  * Use of this source code is governed by a MIT-style
6  * license that can be found in the LICENSE file or at
7  * https://opensource.org/licenses/MIT
8  */
9 
10 #include <assert.h>
11 #include <lk/compiler.h>
12 #include <lk/err.h>
13 #include <platform.h>
14 
15 #include <kernel/thread.h>
16 #include <kernel/timer.h>
17 #include <kernel/spinlock.h>
18 
19 #include <lib/watchdog.h>
20 
21 static spin_lock_t lock = SPIN_LOCK_INITIAL_VALUE;
22 
watchdog_handler(watchdog_t * dog)23 __WEAK void watchdog_handler(watchdog_t *dog) {
24     dprintf(INFO, "Watchdog \"%s\" (timeout %u mSec) just fired!!\n",
25             dog->name, (uint32_t)dog->timeout);
26     platform_halt(HALT_ACTION_HALT, HALT_REASON_SW_WATCHDOG);
27 }
28 
watchdog_timer_callback(struct timer * timer,lk_time_t now,void * arg)29 static enum handler_return watchdog_timer_callback(struct timer *timer, lk_time_t now, void *arg) {
30     watchdog_handler((watchdog_t *)arg);
31 
32     /* We should never get here; watchdog handlers should always be fatal. */
33     DEBUG_ASSERT(false);
34 
35     return INT_NO_RESCHEDULE;
36 }
37 
watchdog_init(watchdog_t * dog,lk_time_t timeout,const char * name)38 status_t watchdog_init(watchdog_t *dog, lk_time_t timeout, const char *name) {
39     DEBUG_ASSERT(NULL != dog);
40     DEBUG_ASSERT(INFINITE_TIME != timeout);
41 
42     dog->magic   = WATCHDOG_MAGIC;
43     dog->name    = name ? name : "unnamed watchdog";
44     dog->enabled = false;
45     dog->timeout = timeout;
46     timer_initialize(&dog->expire_timer);
47 
48     return NO_ERROR;
49 }
50 
watchdog_set_enabled(watchdog_t * dog,bool enabled)51 void watchdog_set_enabled(watchdog_t *dog, bool enabled) {
52     spin_lock_saved_state_t state;
53     spin_lock_irqsave(&lock, state);
54 
55     DEBUG_ASSERT((NULL != dog) && (WATCHDOG_MAGIC == dog->magic));
56 
57     if (dog->enabled == enabled)
58         goto done;
59 
60     dog->enabled = enabled;
61     if (enabled)
62         timer_set_oneshot(&dog->expire_timer, dog->timeout, watchdog_timer_callback, dog);
63     else
64         timer_cancel(&dog->expire_timer);
65 
66 done:
67     spin_unlock_irqrestore(&lock, state);
68 }
69 
watchdog_pet(watchdog_t * dog)70 void watchdog_pet(watchdog_t *dog) {
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)
77         goto done;
78 
79     timer_cancel(&dog->expire_timer);
80     timer_set_oneshot(&dog->expire_timer, dog->timeout, watchdog_timer_callback, dog);
81 
82 done:
83     spin_unlock_irqrestore(&lock, state);
84 }
85 
86 
87 static timer_t   hw_watchdog_timer;
88 static bool      hw_watchdog_enabled;
89 static lk_time_t hw_watchdog_pet_timeout;
90 
hw_watchdog_timer_callback(struct timer * timer,lk_time_t now,void * arg)91 static enum handler_return hw_watchdog_timer_callback(struct timer *timer, lk_time_t now, void *arg) {
92     platform_watchdog_pet();
93     return INT_NO_RESCHEDULE;
94 }
95 
watchdog_hw_init(lk_time_t timeout)96 status_t watchdog_hw_init(lk_time_t timeout) {
97     DEBUG_ASSERT(INFINITE_TIME != timeout);
98     timer_initialize(&hw_watchdog_timer);
99     return platform_watchdog_init(timeout, &hw_watchdog_pet_timeout);
100 }
101 
watchdog_hw_set_enabled(bool enabled)102 void watchdog_hw_set_enabled(bool enabled) {
103     spin_lock_saved_state_t state;
104     spin_lock_irqsave(&lock, state);
105 
106     if (hw_watchdog_enabled == enabled)
107         goto done;
108 
109     hw_watchdog_enabled = enabled;
110     platform_watchdog_set_enabled(enabled);
111     if (enabled)
112         timer_set_periodic(&hw_watchdog_timer,
113                            hw_watchdog_pet_timeout,
114                            hw_watchdog_timer_callback,
115                            NULL);
116     else
117         timer_cancel(&hw_watchdog_timer);
118 
119 done:
120     spin_unlock_irqrestore(&lock, state);
121 }
122