2018-03-16 23:14:11 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2008-06-23 23:05:49 +08:00
|
|
|
/*
|
|
|
|
* Watchdog driver for Atmel AT91SAM9x processors.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2008 Renaud CERRATO r.cerrato@til-technologies.fr
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The Watchdog Timer Mode Register can be only written to once. If the
|
|
|
|
* timeout need to be set from Linux, be sure that the bootstrap or the
|
|
|
|
* bootloader doesn't write to this register.
|
|
|
|
*/
|
|
|
|
|
2012-02-16 07:06:19 +08:00
|
|
|
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
|
|
|
|
|
2015-08-16 17:23:43 +08:00
|
|
|
#include <linux/clk.h>
|
2008-06-23 23:05:49 +08:00
|
|
|
#include <linux/errno.h>
|
|
|
|
#include <linux/init.h>
|
2013-10-04 15:24:12 +08:00
|
|
|
#include <linux/interrupt.h>
|
2009-02-12 04:23:10 +08:00
|
|
|
#include <linux/io.h>
|
2008-06-23 23:05:49 +08:00
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/moduleparam.h>
|
|
|
|
#include <linux/platform_device.h>
|
2013-10-04 15:24:12 +08:00
|
|
|
#include <linux/reboot.h>
|
2008-06-23 23:05:49 +08:00
|
|
|
#include <linux/types.h>
|
|
|
|
#include <linux/watchdog.h>
|
|
|
|
#include <linux/jiffies.h>
|
|
|
|
#include <linux/timer.h>
|
|
|
|
#include <linux/bitops.h>
|
|
|
|
#include <linux/uaccess.h>
|
2012-11-12 16:37:25 +08:00
|
|
|
#include <linux/of.h>
|
2013-10-04 15:24:12 +08:00
|
|
|
#include <linux/of_irq.h>
|
2008-06-23 23:05:49 +08:00
|
|
|
|
2011-07-15 07:52:05 +08:00
|
|
|
#include "at91sam9_wdt.h"
|
2008-06-23 23:05:49 +08:00
|
|
|
|
|
|
|
#define DRV_NAME "AT91SAM9 Watchdog"
|
|
|
|
|
2013-10-04 15:24:12 +08:00
|
|
|
#define wdt_read(wdt, field) \
|
2015-03-26 22:34:14 +08:00
|
|
|
readl_relaxed((wdt)->base + (field))
|
2013-10-04 15:24:12 +08:00
|
|
|
#define wdt_write(wtd, field, val) \
|
2015-03-26 22:34:14 +08:00
|
|
|
writel_relaxed((val), (wdt)->base + (field))
|
2011-11-02 01:43:31 +08:00
|
|
|
|
2008-06-23 23:05:49 +08:00
|
|
|
/* AT91SAM9 watchdog runs a 12bit counter @ 256Hz,
|
|
|
|
* use this to convert a watchdog
|
|
|
|
* value from/to milliseconds.
|
|
|
|
*/
|
2013-10-04 15:24:12 +08:00
|
|
|
#define ticks_to_hz_rounddown(t) ((((t) + 1) * HZ) >> 8)
|
|
|
|
#define ticks_to_hz_roundup(t) (((((t) + 1) * HZ) + 255) >> 8)
|
|
|
|
#define ticks_to_secs(t) (((t) + 1) >> 8)
|
2013-11-04 01:52:42 +08:00
|
|
|
#define secs_to_ticks(s) ((s) ? (((s) << 8) - 1) : 0)
|
2013-10-04 15:24:12 +08:00
|
|
|
|
|
|
|
#define WDT_MR_RESET 0x3FFF2FFF
|
|
|
|
|
|
|
|
/* Watchdog max counter value in ticks */
|
|
|
|
#define WDT_COUNTER_MAX_TICKS 0xFFF
|
|
|
|
|
|
|
|
/* Watchdog max delta/value in secs */
|
|
|
|
#define WDT_COUNTER_MAX_SECS ticks_to_secs(WDT_COUNTER_MAX_TICKS)
|
2008-06-23 23:05:49 +08:00
|
|
|
|
|
|
|
/* Hardware timeout in seconds */
|
|
|
|
#define WDT_HW_TIMEOUT 2
|
|
|
|
|
|
|
|
/* Timer heartbeat (500ms) */
|
|
|
|
#define WDT_TIMEOUT (HZ/2)
|
|
|
|
|
|
|
|
/* User land timeout */
|
|
|
|
#define WDT_HEARTBEAT 15
|
2013-02-14 16:14:25 +08:00
|
|
|
static int heartbeat;
|
2008-06-23 23:05:49 +08:00
|
|
|
module_param(heartbeat, int, 0);
|
|
|
|
MODULE_PARM_DESC(heartbeat, "Watchdog heartbeats in seconds. "
|
|
|
|
"(default = " __MODULE_STRING(WDT_HEARTBEAT) ")");
|
|
|
|
|
2012-03-05 23:51:11 +08:00
|
|
|
static bool nowayout = WATCHDOG_NOWAYOUT;
|
|
|
|
module_param(nowayout, bool, 0);
|
2008-06-23 23:05:49 +08:00
|
|
|
MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started "
|
|
|
|
"(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
|
|
|
|
|
2013-10-04 15:24:12 +08:00
|
|
|
#define to_wdt(wdd) container_of(wdd, struct at91wdt, wdd)
|
|
|
|
struct at91wdt {
|
|
|
|
struct watchdog_device wdd;
|
2011-11-02 01:43:31 +08:00
|
|
|
void __iomem *base;
|
2008-06-23 23:05:49 +08:00
|
|
|
unsigned long next_heartbeat; /* the next_heartbeat for the timer */
|
|
|
|
struct timer_list timer; /* The timer that pings the watchdog */
|
2013-10-04 15:24:12 +08:00
|
|
|
u32 mr;
|
|
|
|
u32 mr_mask;
|
|
|
|
unsigned long heartbeat; /* WDT heartbeat in jiffies */
|
|
|
|
bool nowayout;
|
|
|
|
unsigned int irq;
|
2015-08-16 17:23:43 +08:00
|
|
|
struct clk *sclk;
|
2013-10-04 15:24:12 +08:00
|
|
|
};
|
2008-06-23 23:05:49 +08:00
|
|
|
|
|
|
|
/* ......................................................................... */
|
|
|
|
|
2013-10-04 15:24:12 +08:00
|
|
|
static irqreturn_t wdt_interrupt(int irq, void *dev_id)
|
|
|
|
{
|
|
|
|
struct at91wdt *wdt = (struct at91wdt *)dev_id;
|
|
|
|
|
|
|
|
if (wdt_read(wdt, AT91_WDT_SR)) {
|
|
|
|
pr_crit("at91sam9 WDT software reset\n");
|
|
|
|
emergency_restart();
|
|
|
|
pr_crit("Reboot didn't ?????\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
return IRQ_HANDLED;
|
|
|
|
}
|
|
|
|
|
2008-06-23 23:05:49 +08:00
|
|
|
/*
|
|
|
|
* Reload the watchdog timer. (ie, pat the watchdog)
|
|
|
|
*/
|
2013-10-04 15:24:12 +08:00
|
|
|
static inline void at91_wdt_reset(struct at91wdt *wdt)
|
2008-06-23 23:05:49 +08:00
|
|
|
{
|
2013-10-04 15:24:12 +08:00
|
|
|
wdt_write(wdt, AT91_WDT_CR, AT91_WDT_KEY | AT91_WDT_WDRSTT);
|
2008-06-23 23:05:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Timer tick
|
|
|
|
*/
|
treewide: setup_timer() -> timer_setup()
This converts all remaining cases of the old setup_timer() API into using
timer_setup(), where the callback argument is the structure already
holding the struct timer_list. These should have no behavioral changes,
since they just change which pointer is passed into the callback with
the same available pointers after conversion. It handles the following
examples, in addition to some other variations.
Casting from unsigned long:
void my_callback(unsigned long data)
{
struct something *ptr = (struct something *)data;
...
}
...
setup_timer(&ptr->my_timer, my_callback, ptr);
and forced object casts:
void my_callback(struct something *ptr)
{
...
}
...
setup_timer(&ptr->my_timer, my_callback, (unsigned long)ptr);
become:
void my_callback(struct timer_list *t)
{
struct something *ptr = from_timer(ptr, t, my_timer);
...
}
...
timer_setup(&ptr->my_timer, my_callback, 0);
Direct function assignments:
void my_callback(unsigned long data)
{
struct something *ptr = (struct something *)data;
...
}
...
ptr->my_timer.function = my_callback;
have a temporary cast added, along with converting the args:
void my_callback(struct timer_list *t)
{
struct something *ptr = from_timer(ptr, t, my_timer);
...
}
...
ptr->my_timer.function = (TIMER_FUNC_TYPE)my_callback;
And finally, callbacks without a data assignment:
void my_callback(unsigned long data)
{
...
}
...
setup_timer(&ptr->my_timer, my_callback, 0);
have their argument renamed to verify they're unused during conversion:
void my_callback(struct timer_list *unused)
{
...
}
...
timer_setup(&ptr->my_timer, my_callback, 0);
The conversion is done with the following Coccinelle script:
spatch --very-quiet --all-includes --include-headers \
-I ./arch/x86/include -I ./arch/x86/include/generated \
-I ./include -I ./arch/x86/include/uapi \
-I ./arch/x86/include/generated/uapi -I ./include/uapi \
-I ./include/generated/uapi --include ./include/linux/kconfig.h \
--dir . \
--cocci-file ~/src/data/timer_setup.cocci
@fix_address_of@
expression e;
@@
setup_timer(
-&(e)
+&e
, ...)
// Update any raw setup_timer() usages that have a NULL callback, but
// would otherwise match change_timer_function_usage, since the latter
// will update all function assignments done in the face of a NULL
// function initialization in setup_timer().
@change_timer_function_usage_NULL@
expression _E;
identifier _timer;
type _cast_data;
@@
(
-setup_timer(&_E->_timer, NULL, _E);
+timer_setup(&_E->_timer, NULL, 0);
|
-setup_timer(&_E->_timer, NULL, (_cast_data)_E);
+timer_setup(&_E->_timer, NULL, 0);
|
-setup_timer(&_E._timer, NULL, &_E);
+timer_setup(&_E._timer, NULL, 0);
|
-setup_timer(&_E._timer, NULL, (_cast_data)&_E);
+timer_setup(&_E._timer, NULL, 0);
)
@change_timer_function_usage@
expression _E;
identifier _timer;
struct timer_list _stl;
identifier _callback;
type _cast_func, _cast_data;
@@
(
-setup_timer(&_E->_timer, _callback, _E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, &_callback, _E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, _callback, (_cast_data)_E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, &_callback, (_cast_data)_E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, (_cast_func)_callback, _E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, (_cast_func)&_callback, _E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, (_cast_func)_callback, (_cast_data)_E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, (_cast_func)&_callback, (_cast_data)_E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E._timer, _callback, (_cast_data)_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, _callback, (_cast_data)&_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, &_callback, (_cast_data)_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, &_callback, (_cast_data)&_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, (_cast_func)_callback, (_cast_data)_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, (_cast_func)_callback, (_cast_data)&_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, (_cast_func)&_callback, (_cast_data)_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, (_cast_func)&_callback, (_cast_data)&_E);
+timer_setup(&_E._timer, _callback, 0);
|
_E->_timer@_stl.function = _callback;
|
_E->_timer@_stl.function = &_callback;
|
_E->_timer@_stl.function = (_cast_func)_callback;
|
_E->_timer@_stl.function = (_cast_func)&_callback;
|
_E._timer@_stl.function = _callback;
|
_E._timer@_stl.function = &_callback;
|
_E._timer@_stl.function = (_cast_func)_callback;
|
_E._timer@_stl.function = (_cast_func)&_callback;
)
// callback(unsigned long arg)
@change_callback_handle_cast
depends on change_timer_function_usage@
identifier change_timer_function_usage._callback;
identifier change_timer_function_usage._timer;
type _origtype;
identifier _origarg;
type _handletype;
identifier _handle;
@@
void _callback(
-_origtype _origarg
+struct timer_list *t
)
{
(
... when != _origarg
_handletype *_handle =
-(_handletype *)_origarg;
+from_timer(_handle, t, _timer);
... when != _origarg
|
... when != _origarg
_handletype *_handle =
-(void *)_origarg;
+from_timer(_handle, t, _timer);
... when != _origarg
|
... when != _origarg
_handletype *_handle;
... when != _handle
_handle =
-(_handletype *)_origarg;
+from_timer(_handle, t, _timer);
... when != _origarg
|
... when != _origarg
_handletype *_handle;
... when != _handle
_handle =
-(void *)_origarg;
+from_timer(_handle, t, _timer);
... when != _origarg
)
}
// callback(unsigned long arg) without existing variable
@change_callback_handle_cast_no_arg
depends on change_timer_function_usage &&
!change_callback_handle_cast@
identifier change_timer_function_usage._callback;
identifier change_timer_function_usage._timer;
type _origtype;
identifier _origarg;
type _handletype;
@@
void _callback(
-_origtype _origarg
+struct timer_list *t
)
{
+ _handletype *_origarg = from_timer(_origarg, t, _timer);
+
... when != _origarg
- (_handletype *)_origarg
+ _origarg
... when != _origarg
}
// Avoid already converted callbacks.
@match_callback_converted
depends on change_timer_function_usage &&
!change_callback_handle_cast &&
!change_callback_handle_cast_no_arg@
identifier change_timer_function_usage._callback;
identifier t;
@@
void _callback(struct timer_list *t)
{ ... }
// callback(struct something *handle)
@change_callback_handle_arg
depends on change_timer_function_usage &&
!match_callback_converted &&
!change_callback_handle_cast &&
!change_callback_handle_cast_no_arg@
identifier change_timer_function_usage._callback;
identifier change_timer_function_usage._timer;
type _handletype;
identifier _handle;
@@
void _callback(
-_handletype *_handle
+struct timer_list *t
)
{
+ _handletype *_handle = from_timer(_handle, t, _timer);
...
}
// If change_callback_handle_arg ran on an empty function, remove
// the added handler.
@unchange_callback_handle_arg
depends on change_timer_function_usage &&
change_callback_handle_arg@
identifier change_timer_function_usage._callback;
identifier change_timer_function_usage._timer;
type _handletype;
identifier _handle;
identifier t;
@@
void _callback(struct timer_list *t)
{
- _handletype *_handle = from_timer(_handle, t, _timer);
}
// We only want to refactor the setup_timer() data argument if we've found
// the matching callback. This undoes changes in change_timer_function_usage.
@unchange_timer_function_usage
depends on change_timer_function_usage &&
!change_callback_handle_cast &&
!change_callback_handle_cast_no_arg &&
!change_callback_handle_arg@
expression change_timer_function_usage._E;
identifier change_timer_function_usage._timer;
identifier change_timer_function_usage._callback;
type change_timer_function_usage._cast_data;
@@
(
-timer_setup(&_E->_timer, _callback, 0);
+setup_timer(&_E->_timer, _callback, (_cast_data)_E);
|
-timer_setup(&_E._timer, _callback, 0);
+setup_timer(&_E._timer, _callback, (_cast_data)&_E);
)
// If we fixed a callback from a .function assignment, fix the
// assignment cast now.
@change_timer_function_assignment
depends on change_timer_function_usage &&
(change_callback_handle_cast ||
change_callback_handle_cast_no_arg ||
change_callback_handle_arg)@
expression change_timer_function_usage._E;
identifier change_timer_function_usage._timer;
identifier change_timer_function_usage._callback;
type _cast_func;
typedef TIMER_FUNC_TYPE;
@@
(
_E->_timer.function =
-_callback
+(TIMER_FUNC_TYPE)_callback
;
|
_E->_timer.function =
-&_callback
+(TIMER_FUNC_TYPE)_callback
;
|
_E->_timer.function =
-(_cast_func)_callback;
+(TIMER_FUNC_TYPE)_callback
;
|
_E->_timer.function =
-(_cast_func)&_callback
+(TIMER_FUNC_TYPE)_callback
;
|
_E._timer.function =
-_callback
+(TIMER_FUNC_TYPE)_callback
;
|
_E._timer.function =
-&_callback;
+(TIMER_FUNC_TYPE)_callback
;
|
_E._timer.function =
-(_cast_func)_callback
+(TIMER_FUNC_TYPE)_callback
;
|
_E._timer.function =
-(_cast_func)&_callback
+(TIMER_FUNC_TYPE)_callback
;
)
// Sometimes timer functions are called directly. Replace matched args.
@change_timer_function_calls
depends on change_timer_function_usage &&
(change_callback_handle_cast ||
change_callback_handle_cast_no_arg ||
change_callback_handle_arg)@
expression _E;
identifier change_timer_function_usage._timer;
identifier change_timer_function_usage._callback;
type _cast_data;
@@
_callback(
(
-(_cast_data)_E
+&_E->_timer
|
-(_cast_data)&_E
+&_E._timer
|
-_E
+&_E->_timer
)
)
// If a timer has been configured without a data argument, it can be
// converted without regard to the callback argument, since it is unused.
@match_timer_function_unused_data@
expression _E;
identifier _timer;
identifier _callback;
@@
(
-setup_timer(&_E->_timer, _callback, 0);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, _callback, 0L);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, _callback, 0UL);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E._timer, _callback, 0);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, _callback, 0L);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, _callback, 0UL);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_timer, _callback, 0);
+timer_setup(&_timer, _callback, 0);
|
-setup_timer(&_timer, _callback, 0L);
+timer_setup(&_timer, _callback, 0);
|
-setup_timer(&_timer, _callback, 0UL);
+timer_setup(&_timer, _callback, 0);
|
-setup_timer(_timer, _callback, 0);
+timer_setup(_timer, _callback, 0);
|
-setup_timer(_timer, _callback, 0L);
+timer_setup(_timer, _callback, 0);
|
-setup_timer(_timer, _callback, 0UL);
+timer_setup(_timer, _callback, 0);
)
@change_callback_unused_data
depends on match_timer_function_unused_data@
identifier match_timer_function_unused_data._callback;
type _origtype;
identifier _origarg;
@@
void _callback(
-_origtype _origarg
+struct timer_list *unused
)
{
... when != _origarg
}
Signed-off-by: Kees Cook <keescook@chromium.org>
2017-10-17 05:43:17 +08:00
|
|
|
static void at91_ping(struct timer_list *t)
|
2008-06-23 23:05:49 +08:00
|
|
|
{
|
treewide: setup_timer() -> timer_setup()
This converts all remaining cases of the old setup_timer() API into using
timer_setup(), where the callback argument is the structure already
holding the struct timer_list. These should have no behavioral changes,
since they just change which pointer is passed into the callback with
the same available pointers after conversion. It handles the following
examples, in addition to some other variations.
Casting from unsigned long:
void my_callback(unsigned long data)
{
struct something *ptr = (struct something *)data;
...
}
...
setup_timer(&ptr->my_timer, my_callback, ptr);
and forced object casts:
void my_callback(struct something *ptr)
{
...
}
...
setup_timer(&ptr->my_timer, my_callback, (unsigned long)ptr);
become:
void my_callback(struct timer_list *t)
{
struct something *ptr = from_timer(ptr, t, my_timer);
...
}
...
timer_setup(&ptr->my_timer, my_callback, 0);
Direct function assignments:
void my_callback(unsigned long data)
{
struct something *ptr = (struct something *)data;
...
}
...
ptr->my_timer.function = my_callback;
have a temporary cast added, along with converting the args:
void my_callback(struct timer_list *t)
{
struct something *ptr = from_timer(ptr, t, my_timer);
...
}
...
ptr->my_timer.function = (TIMER_FUNC_TYPE)my_callback;
And finally, callbacks without a data assignment:
void my_callback(unsigned long data)
{
...
}
...
setup_timer(&ptr->my_timer, my_callback, 0);
have their argument renamed to verify they're unused during conversion:
void my_callback(struct timer_list *unused)
{
...
}
...
timer_setup(&ptr->my_timer, my_callback, 0);
The conversion is done with the following Coccinelle script:
spatch --very-quiet --all-includes --include-headers \
-I ./arch/x86/include -I ./arch/x86/include/generated \
-I ./include -I ./arch/x86/include/uapi \
-I ./arch/x86/include/generated/uapi -I ./include/uapi \
-I ./include/generated/uapi --include ./include/linux/kconfig.h \
--dir . \
--cocci-file ~/src/data/timer_setup.cocci
@fix_address_of@
expression e;
@@
setup_timer(
-&(e)
+&e
, ...)
// Update any raw setup_timer() usages that have a NULL callback, but
// would otherwise match change_timer_function_usage, since the latter
// will update all function assignments done in the face of a NULL
// function initialization in setup_timer().
@change_timer_function_usage_NULL@
expression _E;
identifier _timer;
type _cast_data;
@@
(
-setup_timer(&_E->_timer, NULL, _E);
+timer_setup(&_E->_timer, NULL, 0);
|
-setup_timer(&_E->_timer, NULL, (_cast_data)_E);
+timer_setup(&_E->_timer, NULL, 0);
|
-setup_timer(&_E._timer, NULL, &_E);
+timer_setup(&_E._timer, NULL, 0);
|
-setup_timer(&_E._timer, NULL, (_cast_data)&_E);
+timer_setup(&_E._timer, NULL, 0);
)
@change_timer_function_usage@
expression _E;
identifier _timer;
struct timer_list _stl;
identifier _callback;
type _cast_func, _cast_data;
@@
(
-setup_timer(&_E->_timer, _callback, _E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, &_callback, _E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, _callback, (_cast_data)_E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, &_callback, (_cast_data)_E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, (_cast_func)_callback, _E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, (_cast_func)&_callback, _E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, (_cast_func)_callback, (_cast_data)_E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, (_cast_func)&_callback, (_cast_data)_E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E._timer, _callback, (_cast_data)_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, _callback, (_cast_data)&_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, &_callback, (_cast_data)_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, &_callback, (_cast_data)&_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, (_cast_func)_callback, (_cast_data)_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, (_cast_func)_callback, (_cast_data)&_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, (_cast_func)&_callback, (_cast_data)_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, (_cast_func)&_callback, (_cast_data)&_E);
+timer_setup(&_E._timer, _callback, 0);
|
_E->_timer@_stl.function = _callback;
|
_E->_timer@_stl.function = &_callback;
|
_E->_timer@_stl.function = (_cast_func)_callback;
|
_E->_timer@_stl.function = (_cast_func)&_callback;
|
_E._timer@_stl.function = _callback;
|
_E._timer@_stl.function = &_callback;
|
_E._timer@_stl.function = (_cast_func)_callback;
|
_E._timer@_stl.function = (_cast_func)&_callback;
)
// callback(unsigned long arg)
@change_callback_handle_cast
depends on change_timer_function_usage@
identifier change_timer_function_usage._callback;
identifier change_timer_function_usage._timer;
type _origtype;
identifier _origarg;
type _handletype;
identifier _handle;
@@
void _callback(
-_origtype _origarg
+struct timer_list *t
)
{
(
... when != _origarg
_handletype *_handle =
-(_handletype *)_origarg;
+from_timer(_handle, t, _timer);
... when != _origarg
|
... when != _origarg
_handletype *_handle =
-(void *)_origarg;
+from_timer(_handle, t, _timer);
... when != _origarg
|
... when != _origarg
_handletype *_handle;
... when != _handle
_handle =
-(_handletype *)_origarg;
+from_timer(_handle, t, _timer);
... when != _origarg
|
... when != _origarg
_handletype *_handle;
... when != _handle
_handle =
-(void *)_origarg;
+from_timer(_handle, t, _timer);
... when != _origarg
)
}
// callback(unsigned long arg) without existing variable
@change_callback_handle_cast_no_arg
depends on change_timer_function_usage &&
!change_callback_handle_cast@
identifier change_timer_function_usage._callback;
identifier change_timer_function_usage._timer;
type _origtype;
identifier _origarg;
type _handletype;
@@
void _callback(
-_origtype _origarg
+struct timer_list *t
)
{
+ _handletype *_origarg = from_timer(_origarg, t, _timer);
+
... when != _origarg
- (_handletype *)_origarg
+ _origarg
... when != _origarg
}
// Avoid already converted callbacks.
@match_callback_converted
depends on change_timer_function_usage &&
!change_callback_handle_cast &&
!change_callback_handle_cast_no_arg@
identifier change_timer_function_usage._callback;
identifier t;
@@
void _callback(struct timer_list *t)
{ ... }
// callback(struct something *handle)
@change_callback_handle_arg
depends on change_timer_function_usage &&
!match_callback_converted &&
!change_callback_handle_cast &&
!change_callback_handle_cast_no_arg@
identifier change_timer_function_usage._callback;
identifier change_timer_function_usage._timer;
type _handletype;
identifier _handle;
@@
void _callback(
-_handletype *_handle
+struct timer_list *t
)
{
+ _handletype *_handle = from_timer(_handle, t, _timer);
...
}
// If change_callback_handle_arg ran on an empty function, remove
// the added handler.
@unchange_callback_handle_arg
depends on change_timer_function_usage &&
change_callback_handle_arg@
identifier change_timer_function_usage._callback;
identifier change_timer_function_usage._timer;
type _handletype;
identifier _handle;
identifier t;
@@
void _callback(struct timer_list *t)
{
- _handletype *_handle = from_timer(_handle, t, _timer);
}
// We only want to refactor the setup_timer() data argument if we've found
// the matching callback. This undoes changes in change_timer_function_usage.
@unchange_timer_function_usage
depends on change_timer_function_usage &&
!change_callback_handle_cast &&
!change_callback_handle_cast_no_arg &&
!change_callback_handle_arg@
expression change_timer_function_usage._E;
identifier change_timer_function_usage._timer;
identifier change_timer_function_usage._callback;
type change_timer_function_usage._cast_data;
@@
(
-timer_setup(&_E->_timer, _callback, 0);
+setup_timer(&_E->_timer, _callback, (_cast_data)_E);
|
-timer_setup(&_E._timer, _callback, 0);
+setup_timer(&_E._timer, _callback, (_cast_data)&_E);
)
// If we fixed a callback from a .function assignment, fix the
// assignment cast now.
@change_timer_function_assignment
depends on change_timer_function_usage &&
(change_callback_handle_cast ||
change_callback_handle_cast_no_arg ||
change_callback_handle_arg)@
expression change_timer_function_usage._E;
identifier change_timer_function_usage._timer;
identifier change_timer_function_usage._callback;
type _cast_func;
typedef TIMER_FUNC_TYPE;
@@
(
_E->_timer.function =
-_callback
+(TIMER_FUNC_TYPE)_callback
;
|
_E->_timer.function =
-&_callback
+(TIMER_FUNC_TYPE)_callback
;
|
_E->_timer.function =
-(_cast_func)_callback;
+(TIMER_FUNC_TYPE)_callback
;
|
_E->_timer.function =
-(_cast_func)&_callback
+(TIMER_FUNC_TYPE)_callback
;
|
_E._timer.function =
-_callback
+(TIMER_FUNC_TYPE)_callback
;
|
_E._timer.function =
-&_callback;
+(TIMER_FUNC_TYPE)_callback
;
|
_E._timer.function =
-(_cast_func)_callback
+(TIMER_FUNC_TYPE)_callback
;
|
_E._timer.function =
-(_cast_func)&_callback
+(TIMER_FUNC_TYPE)_callback
;
)
// Sometimes timer functions are called directly. Replace matched args.
@change_timer_function_calls
depends on change_timer_function_usage &&
(change_callback_handle_cast ||
change_callback_handle_cast_no_arg ||
change_callback_handle_arg)@
expression _E;
identifier change_timer_function_usage._timer;
identifier change_timer_function_usage._callback;
type _cast_data;
@@
_callback(
(
-(_cast_data)_E
+&_E->_timer
|
-(_cast_data)&_E
+&_E._timer
|
-_E
+&_E->_timer
)
)
// If a timer has been configured without a data argument, it can be
// converted without regard to the callback argument, since it is unused.
@match_timer_function_unused_data@
expression _E;
identifier _timer;
identifier _callback;
@@
(
-setup_timer(&_E->_timer, _callback, 0);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, _callback, 0L);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, _callback, 0UL);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E._timer, _callback, 0);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, _callback, 0L);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, _callback, 0UL);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_timer, _callback, 0);
+timer_setup(&_timer, _callback, 0);
|
-setup_timer(&_timer, _callback, 0L);
+timer_setup(&_timer, _callback, 0);
|
-setup_timer(&_timer, _callback, 0UL);
+timer_setup(&_timer, _callback, 0);
|
-setup_timer(_timer, _callback, 0);
+timer_setup(_timer, _callback, 0);
|
-setup_timer(_timer, _callback, 0L);
+timer_setup(_timer, _callback, 0);
|
-setup_timer(_timer, _callback, 0UL);
+timer_setup(_timer, _callback, 0);
)
@change_callback_unused_data
depends on match_timer_function_unused_data@
identifier match_timer_function_unused_data._callback;
type _origtype;
identifier _origarg;
@@
void _callback(
-_origtype _origarg
+struct timer_list *unused
)
{
... when != _origarg
}
Signed-off-by: Kees Cook <keescook@chromium.org>
2017-10-17 05:43:17 +08:00
|
|
|
struct at91wdt *wdt = from_timer(wdt, t, timer);
|
2013-10-04 15:24:12 +08:00
|
|
|
if (time_before(jiffies, wdt->next_heartbeat) ||
|
|
|
|
!watchdog_active(&wdt->wdd)) {
|
|
|
|
at91_wdt_reset(wdt);
|
|
|
|
mod_timer(&wdt->timer, jiffies + wdt->heartbeat);
|
|
|
|
} else {
|
2012-02-16 07:06:19 +08:00
|
|
|
pr_crit("I will reset your machine !\n");
|
2013-10-04 15:24:12 +08:00
|
|
|
}
|
2013-02-01 15:06:21 +08:00
|
|
|
}
|
2008-06-23 23:05:49 +08:00
|
|
|
|
2013-02-01 15:06:21 +08:00
|
|
|
static int at91_wdt_start(struct watchdog_device *wdd)
|
|
|
|
{
|
2013-10-04 15:24:12 +08:00
|
|
|
struct at91wdt *wdt = to_wdt(wdd);
|
|
|
|
/* calculate when the next userspace timeout will be */
|
|
|
|
wdt->next_heartbeat = jiffies + wdd->timeout * HZ;
|
2013-02-01 15:06:21 +08:00
|
|
|
return 0;
|
2008-06-23 23:05:49 +08:00
|
|
|
}
|
|
|
|
|
2013-02-01 15:06:21 +08:00
|
|
|
static int at91_wdt_stop(struct watchdog_device *wdd)
|
2008-06-23 23:05:49 +08:00
|
|
|
{
|
2013-02-01 15:06:21 +08:00
|
|
|
/* The watchdog timer hardware can not be stopped... */
|
|
|
|
return 0;
|
|
|
|
}
|
2008-06-23 23:05:49 +08:00
|
|
|
|
2013-02-01 15:06:21 +08:00
|
|
|
static int at91_wdt_set_timeout(struct watchdog_device *wdd, unsigned int new_timeout)
|
|
|
|
{
|
|
|
|
wdd->timeout = new_timeout;
|
2013-10-04 15:24:12 +08:00
|
|
|
return at91_wdt_start(wdd);
|
2008-06-23 23:05:49 +08:00
|
|
|
}
|
|
|
|
|
2013-10-04 15:24:12 +08:00
|
|
|
static int at91_wdt_init(struct platform_device *pdev, struct at91wdt *wdt)
|
2008-06-23 23:05:49 +08:00
|
|
|
{
|
2013-10-04 15:24:12 +08:00
|
|
|
u32 tmp;
|
|
|
|
u32 delta;
|
|
|
|
u32 value;
|
|
|
|
int err;
|
|
|
|
u32 mask = wdt->mr_mask;
|
|
|
|
unsigned long min_heartbeat = 1;
|
2013-11-04 01:52:44 +08:00
|
|
|
unsigned long max_heartbeat;
|
2013-10-04 15:24:12 +08:00
|
|
|
struct device *dev = &pdev->dev;
|
|
|
|
|
|
|
|
tmp = wdt_read(wdt, AT91_WDT_MR);
|
|
|
|
if ((tmp & mask) != (wdt->mr & mask)) {
|
|
|
|
if (tmp == WDT_MR_RESET) {
|
|
|
|
wdt_write(wdt, AT91_WDT_MR, wdt->mr);
|
|
|
|
tmp = wdt_read(wdt, AT91_WDT_MR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tmp & AT91_WDT_WDDIS) {
|
|
|
|
if (wdt->mr & AT91_WDT_WDDIS)
|
|
|
|
return 0;
|
|
|
|
dev_err(dev, "watchdog is disabled\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
value = tmp & AT91_WDT_WDV;
|
|
|
|
delta = (tmp & AT91_WDT_WDD) >> 16;
|
|
|
|
|
|
|
|
if (delta < value)
|
|
|
|
min_heartbeat = ticks_to_hz_roundup(value - delta);
|
|
|
|
|
2013-11-04 01:52:44 +08:00
|
|
|
max_heartbeat = ticks_to_hz_rounddown(value);
|
|
|
|
if (!max_heartbeat) {
|
2013-10-04 15:24:12 +08:00
|
|
|
dev_err(dev,
|
|
|
|
"heartbeat is too small for the system to handle it correctly\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2013-11-04 01:52:44 +08:00
|
|
|
/*
|
|
|
|
* Try to reset the watchdog counter 4 or 2 times more often than
|
|
|
|
* actually requested, to avoid spurious watchdog reset.
|
|
|
|
* If this is not possible because of the min_heartbeat value, reset
|
|
|
|
* it at the min_heartbeat period.
|
|
|
|
*/
|
|
|
|
if ((max_heartbeat / 4) >= min_heartbeat)
|
|
|
|
wdt->heartbeat = max_heartbeat / 4;
|
|
|
|
else if ((max_heartbeat / 2) >= min_heartbeat)
|
|
|
|
wdt->heartbeat = max_heartbeat / 2;
|
|
|
|
else
|
2013-10-04 15:24:12 +08:00
|
|
|
wdt->heartbeat = min_heartbeat;
|
2013-11-04 01:52:44 +08:00
|
|
|
|
|
|
|
if (max_heartbeat < min_heartbeat + 4)
|
2013-10-04 15:24:12 +08:00
|
|
|
dev_warn(dev,
|
|
|
|
"min heartbeat and max heartbeat might be too close for the system to handle it correctly\n");
|
2008-06-23 23:05:49 +08:00
|
|
|
|
2013-10-04 15:24:12 +08:00
|
|
|
if ((tmp & AT91_WDT_WDFIEN) && wdt->irq) {
|
|
|
|
err = request_irq(wdt->irq, wdt_interrupt,
|
2015-03-02 17:18:17 +08:00
|
|
|
IRQF_SHARED | IRQF_IRQPOLL |
|
|
|
|
IRQF_NO_SUSPEND,
|
2013-10-04 15:24:12 +08:00
|
|
|
pdev->name, wdt);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((tmp & wdt->mr_mask) != (wdt->mr & wdt->mr_mask))
|
|
|
|
dev_warn(dev,
|
|
|
|
"watchdog already configured differently (mr = %x expecting %x)\n",
|
|
|
|
tmp & wdt->mr_mask, wdt->mr & wdt->mr_mask);
|
|
|
|
|
treewide: setup_timer() -> timer_setup()
This converts all remaining cases of the old setup_timer() API into using
timer_setup(), where the callback argument is the structure already
holding the struct timer_list. These should have no behavioral changes,
since they just change which pointer is passed into the callback with
the same available pointers after conversion. It handles the following
examples, in addition to some other variations.
Casting from unsigned long:
void my_callback(unsigned long data)
{
struct something *ptr = (struct something *)data;
...
}
...
setup_timer(&ptr->my_timer, my_callback, ptr);
and forced object casts:
void my_callback(struct something *ptr)
{
...
}
...
setup_timer(&ptr->my_timer, my_callback, (unsigned long)ptr);
become:
void my_callback(struct timer_list *t)
{
struct something *ptr = from_timer(ptr, t, my_timer);
...
}
...
timer_setup(&ptr->my_timer, my_callback, 0);
Direct function assignments:
void my_callback(unsigned long data)
{
struct something *ptr = (struct something *)data;
...
}
...
ptr->my_timer.function = my_callback;
have a temporary cast added, along with converting the args:
void my_callback(struct timer_list *t)
{
struct something *ptr = from_timer(ptr, t, my_timer);
...
}
...
ptr->my_timer.function = (TIMER_FUNC_TYPE)my_callback;
And finally, callbacks without a data assignment:
void my_callback(unsigned long data)
{
...
}
...
setup_timer(&ptr->my_timer, my_callback, 0);
have their argument renamed to verify they're unused during conversion:
void my_callback(struct timer_list *unused)
{
...
}
...
timer_setup(&ptr->my_timer, my_callback, 0);
The conversion is done with the following Coccinelle script:
spatch --very-quiet --all-includes --include-headers \
-I ./arch/x86/include -I ./arch/x86/include/generated \
-I ./include -I ./arch/x86/include/uapi \
-I ./arch/x86/include/generated/uapi -I ./include/uapi \
-I ./include/generated/uapi --include ./include/linux/kconfig.h \
--dir . \
--cocci-file ~/src/data/timer_setup.cocci
@fix_address_of@
expression e;
@@
setup_timer(
-&(e)
+&e
, ...)
// Update any raw setup_timer() usages that have a NULL callback, but
// would otherwise match change_timer_function_usage, since the latter
// will update all function assignments done in the face of a NULL
// function initialization in setup_timer().
@change_timer_function_usage_NULL@
expression _E;
identifier _timer;
type _cast_data;
@@
(
-setup_timer(&_E->_timer, NULL, _E);
+timer_setup(&_E->_timer, NULL, 0);
|
-setup_timer(&_E->_timer, NULL, (_cast_data)_E);
+timer_setup(&_E->_timer, NULL, 0);
|
-setup_timer(&_E._timer, NULL, &_E);
+timer_setup(&_E._timer, NULL, 0);
|
-setup_timer(&_E._timer, NULL, (_cast_data)&_E);
+timer_setup(&_E._timer, NULL, 0);
)
@change_timer_function_usage@
expression _E;
identifier _timer;
struct timer_list _stl;
identifier _callback;
type _cast_func, _cast_data;
@@
(
-setup_timer(&_E->_timer, _callback, _E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, &_callback, _E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, _callback, (_cast_data)_E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, &_callback, (_cast_data)_E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, (_cast_func)_callback, _E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, (_cast_func)&_callback, _E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, (_cast_func)_callback, (_cast_data)_E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, (_cast_func)&_callback, (_cast_data)_E);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E._timer, _callback, (_cast_data)_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, _callback, (_cast_data)&_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, &_callback, (_cast_data)_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, &_callback, (_cast_data)&_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, (_cast_func)_callback, (_cast_data)_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, (_cast_func)_callback, (_cast_data)&_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, (_cast_func)&_callback, (_cast_data)_E);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, (_cast_func)&_callback, (_cast_data)&_E);
+timer_setup(&_E._timer, _callback, 0);
|
_E->_timer@_stl.function = _callback;
|
_E->_timer@_stl.function = &_callback;
|
_E->_timer@_stl.function = (_cast_func)_callback;
|
_E->_timer@_stl.function = (_cast_func)&_callback;
|
_E._timer@_stl.function = _callback;
|
_E._timer@_stl.function = &_callback;
|
_E._timer@_stl.function = (_cast_func)_callback;
|
_E._timer@_stl.function = (_cast_func)&_callback;
)
// callback(unsigned long arg)
@change_callback_handle_cast
depends on change_timer_function_usage@
identifier change_timer_function_usage._callback;
identifier change_timer_function_usage._timer;
type _origtype;
identifier _origarg;
type _handletype;
identifier _handle;
@@
void _callback(
-_origtype _origarg
+struct timer_list *t
)
{
(
... when != _origarg
_handletype *_handle =
-(_handletype *)_origarg;
+from_timer(_handle, t, _timer);
... when != _origarg
|
... when != _origarg
_handletype *_handle =
-(void *)_origarg;
+from_timer(_handle, t, _timer);
... when != _origarg
|
... when != _origarg
_handletype *_handle;
... when != _handle
_handle =
-(_handletype *)_origarg;
+from_timer(_handle, t, _timer);
... when != _origarg
|
... when != _origarg
_handletype *_handle;
... when != _handle
_handle =
-(void *)_origarg;
+from_timer(_handle, t, _timer);
... when != _origarg
)
}
// callback(unsigned long arg) without existing variable
@change_callback_handle_cast_no_arg
depends on change_timer_function_usage &&
!change_callback_handle_cast@
identifier change_timer_function_usage._callback;
identifier change_timer_function_usage._timer;
type _origtype;
identifier _origarg;
type _handletype;
@@
void _callback(
-_origtype _origarg
+struct timer_list *t
)
{
+ _handletype *_origarg = from_timer(_origarg, t, _timer);
+
... when != _origarg
- (_handletype *)_origarg
+ _origarg
... when != _origarg
}
// Avoid already converted callbacks.
@match_callback_converted
depends on change_timer_function_usage &&
!change_callback_handle_cast &&
!change_callback_handle_cast_no_arg@
identifier change_timer_function_usage._callback;
identifier t;
@@
void _callback(struct timer_list *t)
{ ... }
// callback(struct something *handle)
@change_callback_handle_arg
depends on change_timer_function_usage &&
!match_callback_converted &&
!change_callback_handle_cast &&
!change_callback_handle_cast_no_arg@
identifier change_timer_function_usage._callback;
identifier change_timer_function_usage._timer;
type _handletype;
identifier _handle;
@@
void _callback(
-_handletype *_handle
+struct timer_list *t
)
{
+ _handletype *_handle = from_timer(_handle, t, _timer);
...
}
// If change_callback_handle_arg ran on an empty function, remove
// the added handler.
@unchange_callback_handle_arg
depends on change_timer_function_usage &&
change_callback_handle_arg@
identifier change_timer_function_usage._callback;
identifier change_timer_function_usage._timer;
type _handletype;
identifier _handle;
identifier t;
@@
void _callback(struct timer_list *t)
{
- _handletype *_handle = from_timer(_handle, t, _timer);
}
// We only want to refactor the setup_timer() data argument if we've found
// the matching callback. This undoes changes in change_timer_function_usage.
@unchange_timer_function_usage
depends on change_timer_function_usage &&
!change_callback_handle_cast &&
!change_callback_handle_cast_no_arg &&
!change_callback_handle_arg@
expression change_timer_function_usage._E;
identifier change_timer_function_usage._timer;
identifier change_timer_function_usage._callback;
type change_timer_function_usage._cast_data;
@@
(
-timer_setup(&_E->_timer, _callback, 0);
+setup_timer(&_E->_timer, _callback, (_cast_data)_E);
|
-timer_setup(&_E._timer, _callback, 0);
+setup_timer(&_E._timer, _callback, (_cast_data)&_E);
)
// If we fixed a callback from a .function assignment, fix the
// assignment cast now.
@change_timer_function_assignment
depends on change_timer_function_usage &&
(change_callback_handle_cast ||
change_callback_handle_cast_no_arg ||
change_callback_handle_arg)@
expression change_timer_function_usage._E;
identifier change_timer_function_usage._timer;
identifier change_timer_function_usage._callback;
type _cast_func;
typedef TIMER_FUNC_TYPE;
@@
(
_E->_timer.function =
-_callback
+(TIMER_FUNC_TYPE)_callback
;
|
_E->_timer.function =
-&_callback
+(TIMER_FUNC_TYPE)_callback
;
|
_E->_timer.function =
-(_cast_func)_callback;
+(TIMER_FUNC_TYPE)_callback
;
|
_E->_timer.function =
-(_cast_func)&_callback
+(TIMER_FUNC_TYPE)_callback
;
|
_E._timer.function =
-_callback
+(TIMER_FUNC_TYPE)_callback
;
|
_E._timer.function =
-&_callback;
+(TIMER_FUNC_TYPE)_callback
;
|
_E._timer.function =
-(_cast_func)_callback
+(TIMER_FUNC_TYPE)_callback
;
|
_E._timer.function =
-(_cast_func)&_callback
+(TIMER_FUNC_TYPE)_callback
;
)
// Sometimes timer functions are called directly. Replace matched args.
@change_timer_function_calls
depends on change_timer_function_usage &&
(change_callback_handle_cast ||
change_callback_handle_cast_no_arg ||
change_callback_handle_arg)@
expression _E;
identifier change_timer_function_usage._timer;
identifier change_timer_function_usage._callback;
type _cast_data;
@@
_callback(
(
-(_cast_data)_E
+&_E->_timer
|
-(_cast_data)&_E
+&_E._timer
|
-_E
+&_E->_timer
)
)
// If a timer has been configured without a data argument, it can be
// converted without regard to the callback argument, since it is unused.
@match_timer_function_unused_data@
expression _E;
identifier _timer;
identifier _callback;
@@
(
-setup_timer(&_E->_timer, _callback, 0);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, _callback, 0L);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E->_timer, _callback, 0UL);
+timer_setup(&_E->_timer, _callback, 0);
|
-setup_timer(&_E._timer, _callback, 0);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, _callback, 0L);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_E._timer, _callback, 0UL);
+timer_setup(&_E._timer, _callback, 0);
|
-setup_timer(&_timer, _callback, 0);
+timer_setup(&_timer, _callback, 0);
|
-setup_timer(&_timer, _callback, 0L);
+timer_setup(&_timer, _callback, 0);
|
-setup_timer(&_timer, _callback, 0UL);
+timer_setup(&_timer, _callback, 0);
|
-setup_timer(_timer, _callback, 0);
+timer_setup(_timer, _callback, 0);
|
-setup_timer(_timer, _callback, 0L);
+timer_setup(_timer, _callback, 0);
|
-setup_timer(_timer, _callback, 0UL);
+timer_setup(_timer, _callback, 0);
)
@change_callback_unused_data
depends on match_timer_function_unused_data@
identifier match_timer_function_unused_data._callback;
type _origtype;
identifier _origarg;
@@
void _callback(
-_origtype _origarg
+struct timer_list *unused
)
{
... when != _origarg
}
Signed-off-by: Kees Cook <keescook@chromium.org>
2017-10-17 05:43:17 +08:00
|
|
|
timer_setup(&wdt->timer, at91_ping, 0);
|
2013-11-04 01:52:43 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Use min_heartbeat the first time to avoid spurious watchdog reset:
|
|
|
|
* we don't know for how long the watchdog counter is running, and
|
|
|
|
* - resetting it right now might trigger a watchdog fault reset
|
|
|
|
* - waiting for heartbeat time might lead to a watchdog timeout
|
|
|
|
* reset
|
|
|
|
*/
|
|
|
|
mod_timer(&wdt->timer, jiffies + min_heartbeat);
|
2013-10-04 15:24:12 +08:00
|
|
|
|
|
|
|
/* Try to set timeout from device tree first */
|
|
|
|
if (watchdog_init_timeout(&wdt->wdd, 0, dev))
|
|
|
|
watchdog_init_timeout(&wdt->wdd, heartbeat, dev);
|
|
|
|
watchdog_set_nowayout(&wdt->wdd, wdt->nowayout);
|
|
|
|
err = watchdog_register_device(&wdt->wdd);
|
|
|
|
if (err)
|
|
|
|
goto out_stop_timer;
|
|
|
|
|
|
|
|
wdt->next_heartbeat = jiffies + wdt->wdd.timeout * HZ;
|
2008-06-23 23:05:49 +08:00
|
|
|
|
|
|
|
return 0;
|
2013-10-04 15:24:12 +08:00
|
|
|
|
|
|
|
out_stop_timer:
|
|
|
|
del_timer(&wdt->timer);
|
|
|
|
return err;
|
2008-06-23 23:05:49 +08:00
|
|
|
}
|
|
|
|
|
2013-02-01 15:06:21 +08:00
|
|
|
/* ......................................................................... */
|
|
|
|
|
2008-06-23 23:05:49 +08:00
|
|
|
static const struct watchdog_info at91_wdt_info = {
|
|
|
|
.identity = DRV_NAME,
|
2009-05-12 02:33:00 +08:00
|
|
|
.options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING |
|
|
|
|
WDIOF_MAGICCLOSE,
|
2008-06-23 23:05:49 +08:00
|
|
|
};
|
|
|
|
|
2013-02-01 15:06:21 +08:00
|
|
|
static const struct watchdog_ops at91_wdt_ops = {
|
|
|
|
.owner = THIS_MODULE,
|
|
|
|
.start = at91_wdt_start,
|
|
|
|
.stop = at91_wdt_stop,
|
|
|
|
.set_timeout = at91_wdt_set_timeout,
|
2008-06-23 23:05:49 +08:00
|
|
|
};
|
|
|
|
|
2013-10-04 15:24:12 +08:00
|
|
|
#if defined(CONFIG_OF)
|
|
|
|
static int of_at91wdt_init(struct device_node *np, struct at91wdt *wdt)
|
|
|
|
{
|
|
|
|
u32 min = 0;
|
|
|
|
u32 max = WDT_COUNTER_MAX_SECS;
|
|
|
|
const char *tmp;
|
|
|
|
|
|
|
|
/* Get the interrupts property */
|
|
|
|
wdt->irq = irq_of_parse_and_map(np, 0);
|
|
|
|
if (!wdt->irq)
|
|
|
|
dev_warn(wdt->wdd.parent, "failed to get IRQ from DT\n");
|
|
|
|
|
|
|
|
if (!of_property_read_u32_index(np, "atmel,max-heartbeat-sec", 0,
|
|
|
|
&max)) {
|
|
|
|
if (!max || max > WDT_COUNTER_MAX_SECS)
|
|
|
|
max = WDT_COUNTER_MAX_SECS;
|
|
|
|
|
|
|
|
if (!of_property_read_u32_index(np, "atmel,min-heartbeat-sec",
|
|
|
|
0, &min)) {
|
|
|
|
if (min >= max)
|
|
|
|
min = max - 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
min = secs_to_ticks(min);
|
|
|
|
max = secs_to_ticks(max);
|
|
|
|
|
|
|
|
wdt->mr_mask = 0x3FFFFFFF;
|
|
|
|
wdt->mr = 0;
|
|
|
|
if (!of_property_read_string(np, "atmel,watchdog-type", &tmp) &&
|
|
|
|
!strcmp(tmp, "software")) {
|
|
|
|
wdt->mr |= AT91_WDT_WDFIEN;
|
|
|
|
wdt->mr_mask &= ~AT91_WDT_WDRPROC;
|
|
|
|
} else {
|
|
|
|
wdt->mr |= AT91_WDT_WDRSTEN;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!of_property_read_string(np, "atmel,reset-type", &tmp) &&
|
|
|
|
!strcmp(tmp, "proc"))
|
|
|
|
wdt->mr |= AT91_WDT_WDRPROC;
|
|
|
|
|
|
|
|
if (of_property_read_bool(np, "atmel,disable")) {
|
|
|
|
wdt->mr |= AT91_WDT_WDDIS;
|
|
|
|
wdt->mr_mask &= AT91_WDT_WDDIS;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (of_property_read_bool(np, "atmel,idle-halt"))
|
|
|
|
wdt->mr |= AT91_WDT_WDIDLEHLT;
|
|
|
|
|
|
|
|
if (of_property_read_bool(np, "atmel,dbg-halt"))
|
|
|
|
wdt->mr |= AT91_WDT_WDDBGHLT;
|
|
|
|
|
|
|
|
wdt->mr |= max | ((max - min) << 16);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
static inline int of_at91wdt_init(struct device_node *np, struct at91wdt *wdt)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
2008-06-23 23:05:49 +08:00
|
|
|
|
|
|
|
static int __init at91wdt_probe(struct platform_device *pdev)
|
|
|
|
{
|
2011-11-02 01:43:31 +08:00
|
|
|
struct resource *r;
|
2013-10-04 15:24:12 +08:00
|
|
|
int err;
|
|
|
|
struct at91wdt *wdt;
|
2008-06-23 23:05:49 +08:00
|
|
|
|
2013-10-04 15:24:12 +08:00
|
|
|
wdt = devm_kzalloc(&pdev->dev, sizeof(*wdt), GFP_KERNEL);
|
|
|
|
if (!wdt)
|
2011-11-02 01:43:31 +08:00
|
|
|
return -ENOMEM;
|
|
|
|
|
2013-10-04 15:24:12 +08:00
|
|
|
wdt->mr = (WDT_HW_TIMEOUT * 256) | AT91_WDT_WDRSTEN | AT91_WDT_WDD |
|
|
|
|
AT91_WDT_WDDBGHLT | AT91_WDT_WDIDLEHLT;
|
|
|
|
wdt->mr_mask = 0x3FFFFFFF;
|
|
|
|
wdt->nowayout = nowayout;
|
|
|
|
wdt->wdd.parent = &pdev->dev;
|
|
|
|
wdt->wdd.info = &at91_wdt_info;
|
|
|
|
wdt->wdd.ops = &at91_wdt_ops;
|
|
|
|
wdt->wdd.timeout = WDT_HEARTBEAT;
|
|
|
|
wdt->wdd.min_timeout = 1;
|
|
|
|
wdt->wdd.max_timeout = 0xFFFF;
|
2013-02-01 15:06:21 +08:00
|
|
|
|
2013-10-04 15:24:12 +08:00
|
|
|
r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
|
|
|
|
wdt->base = devm_ioremap_resource(&pdev->dev, r);
|
|
|
|
if (IS_ERR(wdt->base))
|
|
|
|
return PTR_ERR(wdt->base);
|
|
|
|
|
2015-08-16 17:23:43 +08:00
|
|
|
wdt->sclk = devm_clk_get(&pdev->dev, NULL);
|
|
|
|
if (IS_ERR(wdt->sclk))
|
|
|
|
return PTR_ERR(wdt->sclk);
|
|
|
|
|
|
|
|
err = clk_prepare_enable(wdt->sclk);
|
|
|
|
if (err) {
|
|
|
|
dev_err(&pdev->dev, "Could not enable slow clock\n");
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2013-10-04 15:24:12 +08:00
|
|
|
if (pdev->dev.of_node) {
|
|
|
|
err = of_at91wdt_init(pdev->dev.of_node, wdt);
|
|
|
|
if (err)
|
2015-08-16 17:23:43 +08:00
|
|
|
goto err_clk;
|
2013-10-04 15:24:12 +08:00
|
|
|
}
|
2008-06-23 23:05:49 +08:00
|
|
|
|
2013-10-04 15:24:12 +08:00
|
|
|
err = at91_wdt_init(pdev, wdt);
|
|
|
|
if (err)
|
2015-08-16 17:23:43 +08:00
|
|
|
goto err_clk;
|
2008-06-23 23:05:49 +08:00
|
|
|
|
2013-10-04 15:24:12 +08:00
|
|
|
platform_set_drvdata(pdev, wdt);
|
2008-06-23 23:05:49 +08:00
|
|
|
|
2012-02-16 07:06:19 +08:00
|
|
|
pr_info("enabled (heartbeat=%d sec, nowayout=%d)\n",
|
2013-10-04 15:24:12 +08:00
|
|
|
wdt->wdd.timeout, wdt->nowayout);
|
2008-06-23 23:05:49 +08:00
|
|
|
|
|
|
|
return 0;
|
2015-08-16 17:23:43 +08:00
|
|
|
|
|
|
|
err_clk:
|
|
|
|
clk_disable_unprepare(wdt->sclk);
|
|
|
|
|
|
|
|
return err;
|
2008-06-23 23:05:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int __exit at91wdt_remove(struct platform_device *pdev)
|
|
|
|
{
|
2013-10-04 15:24:12 +08:00
|
|
|
struct at91wdt *wdt = platform_get_drvdata(pdev);
|
|
|
|
watchdog_unregister_device(&wdt->wdd);
|
2008-06-23 23:05:49 +08:00
|
|
|
|
2013-02-01 15:06:21 +08:00
|
|
|
pr_warn("I quit now, hardware will probably reboot!\n");
|
2013-10-04 15:24:12 +08:00
|
|
|
del_timer(&wdt->timer);
|
2015-08-16 17:23:43 +08:00
|
|
|
clk_disable_unprepare(wdt->sclk);
|
2008-06-23 23:05:49 +08:00
|
|
|
|
2013-02-01 15:06:21 +08:00
|
|
|
return 0;
|
2008-06-23 23:05:49 +08:00
|
|
|
}
|
|
|
|
|
2012-11-12 16:37:25 +08:00
|
|
|
#if defined(CONFIG_OF)
|
2013-01-25 22:14:27 +08:00
|
|
|
static const struct of_device_id at91_wdt_dt_ids[] = {
|
2012-11-12 16:37:25 +08:00
|
|
|
{ .compatible = "atmel,at91sam9260-wdt" },
|
|
|
|
{ /* sentinel */ }
|
|
|
|
};
|
|
|
|
|
|
|
|
MODULE_DEVICE_TABLE(of, at91_wdt_dt_ids);
|
|
|
|
#endif
|
|
|
|
|
2008-06-23 23:05:49 +08:00
|
|
|
static struct platform_driver at91wdt_driver = {
|
|
|
|
.remove = __exit_p(at91wdt_remove),
|
|
|
|
.driver = {
|
|
|
|
.name = "at91_wdt",
|
2012-11-12 16:37:25 +08:00
|
|
|
.of_match_table = of_match_ptr(at91_wdt_dt_ids),
|
2008-06-23 23:05:49 +08:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2013-01-09 19:15:27 +08:00
|
|
|
module_platform_driver_probe(at91wdt_driver, at91wdt_probe);
|
2008-06-23 23:05:49 +08:00
|
|
|
|
|
|
|
MODULE_AUTHOR("Renaud CERRATO <r.cerrato@til-technologies.fr>");
|
|
|
|
MODULE_DESCRIPTION("Watchdog driver for Atmel AT91SAM9x processors");
|
|
|
|
MODULE_LICENSE("GPL");
|