[lib][watchdog] a library to manage hardware and software watchdogs

Change-Id: If31c3b4f7f89c1045bea7f54fe1e321a0c6479b8
This commit is contained in:
Travis Geiselbrecht
2015-05-11 17:25:11 -07:00
parent 493b7667c1
commit ba384928e2
3 changed files with 230 additions and 0 deletions

View File

@@ -0,0 +1,85 @@
/*
* Copyright (c) 2013 Google Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files
* (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef __LIB_WATCHDOG_H__
#define __LIB_WATCHDOG_H__
#include <stdbool.h>
#include <stdint.h>
#include <kernel/thread.h>
#include <kernel/timer.h>
#define WATCHDOG_MAGIC 'wdog'
typedef struct watchdog {
uint32_t magic;
const char* name;
bool enabled;
lk_time_t timeout;
timer_t expire_timer;
} watchdog_t;
/* A global weak-reference to the common watchdog handler. By default, this
* function will dprintf a message indicating which watchdog fired, and then
* request a halt using platform_halt, indicating that a SW watchdog was the
* reason for the reset. It is the platform/target's responsibility to either
* implement the panic behavior they want when a watchdog fires in
* platform_halt, or to replace the implementation of the watchdog handler with
* their own appropriate implementation.
*/
void watchdog_handler(watchdog_t* dog) __NO_RETURN;
status_t watchdog_init(watchdog_t* dog, lk_time_t timeout, const char* name);
void watchdog_set_enabled(watchdog_t* dog, bool enabled);
void watchdog_pet(watchdog_t* dog);
/* HW watchdog support. This is nothing but a simple helper used to
* automatically dismiss a platform's HW watchdog using LK timers. Platforms
* must supply
*
* platform_watchdog_init
* platform_watchdog_set_enabled
* platform_watchdog_pet
*
* in order to use the HW watchdog helper functions. After initialized, users
* may enable and disable the HW watchdog whenever appropriate. The helper will
* maintain a timer which dismisses the watchdog at the pet interval recommended
* by the platform. Any programming error which prevents the scheduler timer
* mechanism from running properly will eventually result in the watchdog firing
* and the system rebooting. Whenever possible, when using SW based watchdogs,
* it is recommended that systems provide platform support for a HW watchdog and
* enable the HW watchdog. SW watchdogs are based on LK timers, and should be
* reliable as long as the scheduler and timer mechanism is running properly;
* the HW watchdog functionality provided here should protect the system in case
* something managed to break timers on LK.
*/
extern status_t platform_watchdog_init(lk_time_t target_timeout,
lk_time_t* recommended_pet_period);
extern void platform_watchdog_set_enabled(bool enabled);
extern void platform_watchdog_pet(void);
status_t watchdog_hw_init(lk_time_t timeout);
void watchdog_hw_set_enabled(bool enabled);
#endif // __LIB_WATCHDOG_H__

10
lib/watchdog/rules.mk Normal file
View File

@@ -0,0 +1,10 @@
LOCAL_DIR := $(GET_LOCAL_DIR)
GLOBAL_INCLUDES += $(LOCAL_DIR)/include
MODULE := $(LOCAL_DIR)
MODULE_SRCS := \
$(LOCAL_DIR)/watchdog.c
include make/module.mk

135
lib/watchdog/watchdog.c Normal file
View File

@@ -0,0 +1,135 @@
/*
* Copyright (c) 2013 Google Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files
* (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <assert.h>
#include <compiler.h>
#include <err.h>
#include <platform.h>
#include <kernel/thread.h>
#include <kernel/timer.h>
#include <lib/watchdog.h>
__WEAK void watchdog_handler(watchdog_t* dog)
{
dprintf(INFO, "Watchdog \"%s\" (timeout %u mSec) just fired!!\n",
dog->name, (uint32_t)dog->timeout);
platform_halt(HALT_ACTION_HALT, HALT_REASON_SW_WATCHDOG);
}
static enum handler_return watchdog_timer_callback(struct timer* timer, lk_time_t now, void* arg)
{
watchdog_handler((watchdog_t*)arg);
/* We should never get here; watchdog handlers should always be fatal. */
DEBUG_ASSERT(false);
return INT_NO_RESCHEDULE;
}
status_t watchdog_init(watchdog_t* dog, lk_time_t timeout, const char* name)
{
DEBUG_ASSERT(NULL != dog);
DEBUG_ASSERT(INFINITE_TIME != timeout);
dog->magic = WATCHDOG_MAGIC;
dog->name = name ? name : "unnamed watchdog";
dog->enabled = false;
dog->timeout = timeout;
timer_initialize(&dog->expire_timer);
return NO_ERROR;
}
void watchdog_set_enabled(watchdog_t* dog, bool enabled)
{
enter_critical_section();
DEBUG_ASSERT((NULL != dog) && (WATCHDOG_MAGIC == dog->magic));
if (dog->enabled == enabled)
goto done;
dog->enabled = enabled;
if (enabled)
timer_set_oneshot(&dog->expire_timer, dog->timeout, watchdog_timer_callback, dog);
else
timer_cancel(&dog->expire_timer);
done:
exit_critical_section();
}
void watchdog_pet(watchdog_t* dog)
{
enter_critical_section();
DEBUG_ASSERT((NULL != dog) && (WATCHDOG_MAGIC == dog->magic));
if (!dog->enabled)
goto done;
timer_cancel(&dog->expire_timer);
timer_set_oneshot(&dog->expire_timer, dog->timeout, watchdog_timer_callback, dog);
done:
exit_critical_section();
}
static timer_t hw_watchdog_timer;
static bool hw_watchdog_enabled;
static lk_time_t hw_watchdog_pet_timeout;
static enum handler_return hw_watchdog_timer_callback(struct timer* timer, lk_time_t now, void* arg)
{
platform_watchdog_pet();
return INT_NO_RESCHEDULE;
}
status_t watchdog_hw_init(lk_time_t timeout)
{
DEBUG_ASSERT(INFINITE_TIME != timeout);
timer_initialize(&hw_watchdog_timer);
return platform_watchdog_init(timeout, &hw_watchdog_pet_timeout);
}
void watchdog_hw_set_enabled(bool enabled)
{
enter_critical_section();
if (hw_watchdog_enabled == enabled)
goto done;
hw_watchdog_enabled = enabled;
platform_watchdog_set_enabled(enabled);
if (enabled)
timer_set_periodic(&hw_watchdog_timer,
hw_watchdog_pet_timeout,
hw_watchdog_timer_callback,
NULL);
else
timer_cancel(&hw_watchdog_timer);
done:
exit_critical_section();
}