Skip to content

Commit 1670d85

Browse files
committed
Merge tag 'tags/restart-handler-for-v3.18' into next
Immutable branch with restart handler patches for v3.18
2 parents b08fec2 + 6cd6d94 commit 1670d85

8 files changed

Lines changed: 165 additions & 42 deletions

File tree

‎arch/arm/kernel/process.c‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -114,18 +114,13 @@ void soft_restart(unsigned long addr)
114114
BUG();
115115
}
116116

117-
static void null_restart(enum reboot_mode reboot_mode, const char *cmd)
118-
{
119-
}
120-
121117
/*
122118
* Function pointers to optional machine specific functions
123119
*/
124120
void (*pm_power_off)(void);
125121
EXPORT_SYMBOL(pm_power_off);
126122

127-
void (*arm_pm_restart)(enum reboot_mode reboot_mode, const char *cmd) = null_restart;
128-
EXPORT_SYMBOL_GPL(arm_pm_restart);
123+
void (*arm_pm_restart)(enum reboot_mode reboot_mode, const char *cmd);
129124

130125
/*
131126
* This is our default idle handler.
@@ -230,7 +225,10 @@ void machine_restart(char *cmd)
230225
local_irq_disable();
231226
smp_send_stop();
232227

233-
arm_pm_restart(reboot_mode, cmd);
228+
if (arm_pm_restart)
229+
arm_pm_restart(reboot_mode, cmd);
230+
else
231+
do_kernel_restart(cmd);
234232

235233
/* Give a grace period for failure to restart of 1s */
236234
mdelay(1000);

‎arch/arm64/kernel/process.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ void (*pm_power_off)(void);
9898
EXPORT_SYMBOL_GPL(pm_power_off);
9999

100100
void (*arm_pm_restart)(enum reboot_mode reboot_mode, const char *cmd);
101-
EXPORT_SYMBOL_GPL(arm_pm_restart);
102101

103102
/*
104103
* This is our default idle handler.
@@ -180,6 +179,8 @@ void machine_restart(char *cmd)
180179
/* Now call the architecture specific reboot code. */
181180
if (arm_pm_restart)
182181
arm_pm_restart(reboot_mode, cmd);
182+
else
183+
do_kernel_restart(cmd);
183184

184185
/*
185186
* Whoops - the architecture was unable to reboot.

‎drivers/power/reset/restart-poweroff.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020

2121
static void restart_poweroff_do_poweroff(void)
2222
{
23-
arm_pm_restart(REBOOT_HARD, NULL);
23+
reboot_mode = REBOOT_HARD;
24+
machine_restart(NULL);
2425
}
2526

2627
static int restart_poweroff_probe(struct platform_device *pdev)

‎drivers/watchdog/alim7101_wdt.c‎

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,28 @@ static struct miscdevice wdt_miscdev = {
301301
.fops = &wdt_fops,
302302
};
303303

304+
static int wdt_restart_handle(struct notifier_block *this, unsigned long mode,
305+
void *cmd)
306+
{
307+
/*
308+
* Cobalt devices have no way of rebooting themselves other
309+
* than getting the watchdog to pull reset, so we restart the
310+
* watchdog on reboot with no heartbeat.
311+
*/
312+
wdt_change(WDT_ENABLE);
313+
314+
/* loop until the watchdog fires */
315+
while (true)
316+
;
317+
318+
return NOTIFY_DONE;
319+
}
320+
321+
static struct notifier_block wdt_restart_handler = {
322+
.notifier_call = wdt_restart_handle,
323+
.priority = 128,
324+
};
325+
304326
/*
305327
* Notifier for system down
306328
*/
@@ -311,15 +333,6 @@ static int wdt_notify_sys(struct notifier_block *this,
311333
if (code == SYS_DOWN || code == SYS_HALT)
312334
wdt_turnoff();
313335

314-
if (code == SYS_RESTART) {
315-
/*
316-
* Cobalt devices have no way of rebooting themselves other
317-
* than getting the watchdog to pull reset, so we restart the
318-
* watchdog on reboot with no heartbeat
319-
*/
320-
wdt_change(WDT_ENABLE);
321-
pr_info("Watchdog timer is now enabled with no heartbeat - should reboot in ~1 second\n");
322-
}
323336
return NOTIFY_DONE;
324337
}
325338

@@ -338,6 +351,7 @@ static void __exit alim7101_wdt_unload(void)
338351
/* Deregister */
339352
misc_deregister(&wdt_miscdev);
340353
unregister_reboot_notifier(&wdt_notifier);
354+
unregister_restart_handler(&wdt_restart_handler);
341355
pci_dev_put(alim7101_pmu);
342356
}
343357

@@ -390,11 +404,17 @@ static int __init alim7101_wdt_init(void)
390404
goto err_out;
391405
}
392406

407+
rc = register_restart_handler(&wdt_restart_handler);
408+
if (rc) {
409+
pr_err("cannot register restart handler (err=%d)\n", rc);
410+
goto err_out_reboot;
411+
}
412+
393413
rc = misc_register(&wdt_miscdev);
394414
if (rc) {
395415
pr_err("cannot register miscdev on minor=%d (err=%d)\n",
396416
wdt_miscdev.minor, rc);
397-
goto err_out_reboot;
417+
goto err_out_restart;
398418
}
399419

400420
if (nowayout)
@@ -404,6 +424,8 @@ static int __init alim7101_wdt_init(void)
404424
timeout, nowayout);
405425
return 0;
406426

427+
err_out_restart:
428+
unregister_restart_handler(&wdt_restart_handler);
407429
err_out_reboot:
408430
unregister_reboot_notifier(&wdt_notifier);
409431
err_out:

‎drivers/watchdog/moxart_wdt.c‎

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@
1515
#include <linux/module.h>
1616
#include <linux/err.h>
1717
#include <linux/kernel.h>
18+
#include <linux/notifier.h>
1819
#include <linux/platform_device.h>
20+
#include <linux/reboot.h>
1921
#include <linux/watchdog.h>
2022
#include <linux/moduleparam.h>
2123

22-
#include <asm/system_misc.h>
23-
2424
#define REG_COUNT 0x4
2525
#define REG_MODE 0x8
2626
#define REG_ENABLE 0xC
@@ -29,17 +29,22 @@ struct moxart_wdt_dev {
2929
struct watchdog_device dev;
3030
void __iomem *base;
3131
unsigned int clock_frequency;
32+
struct notifier_block restart_handler;
3233
};
3334

34-
static struct moxart_wdt_dev *moxart_restart_ctx;
35-
3635
static int heartbeat;
3736

38-
static void moxart_wdt_restart(enum reboot_mode reboot_mode, const char *cmd)
37+
static int moxart_restart_handle(struct notifier_block *this,
38+
unsigned long mode, void *cmd)
3939
{
40-
writel(1, moxart_restart_ctx->base + REG_COUNT);
41-
writel(0x5ab9, moxart_restart_ctx->base + REG_MODE);
42-
writel(0x03, moxart_restart_ctx->base + REG_ENABLE);
40+
struct moxart_wdt_dev *moxart_wdt = container_of(this,
41+
struct moxart_wdt_dev,
42+
restart_handler);
43+
writel(1, moxart_wdt->base + REG_COUNT);
44+
writel(0x5ab9, moxart_wdt->base + REG_MODE);
45+
writel(0x03, moxart_wdt->base + REG_ENABLE);
46+
47+
return NOTIFY_DONE;
4348
}
4449

4550
static int moxart_wdt_stop(struct watchdog_device *wdt_dev)
@@ -136,8 +141,12 @@ static int moxart_wdt_probe(struct platform_device *pdev)
136141
if (err)
137142
return err;
138143

139-
moxart_restart_ctx = moxart_wdt;
140-
arm_pm_restart = moxart_wdt_restart;
144+
moxart_wdt->restart_handler.notifier_call = moxart_restart_handle;
145+
moxart_wdt->restart_handler.priority = 128;
146+
err = register_restart_handler(&moxart_wdt->restart_handler);
147+
if (err)
148+
dev_err(dev, "cannot register restart notifier (err=%d)\n",
149+
err);
141150

142151
dev_dbg(dev, "Watchdog enabled (heartbeat=%d sec, nowayout=%d)\n",
143152
moxart_wdt->dev.timeout, nowayout);
@@ -149,9 +158,8 @@ static int moxart_wdt_remove(struct platform_device *pdev)
149158
{
150159
struct moxart_wdt_dev *moxart_wdt = platform_get_drvdata(pdev);
151160

152-
arm_pm_restart = NULL;
161+
unregister_restart_handler(&moxart_wdt->restart_handler);
153162
moxart_wdt_stop(&moxart_wdt->dev);
154-
watchdog_unregister_device(&moxart_wdt->dev);
155163

156164
return 0;
157165
}

‎drivers/watchdog/sunxi_wdt.c‎

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,13 @@
2121
#include <linux/kernel.h>
2222
#include <linux/module.h>
2323
#include <linux/moduleparam.h>
24+
#include <linux/notifier.h>
2425
#include <linux/of.h>
2526
#include <linux/platform_device.h>
2627
#include <linux/reboot.h>
2728
#include <linux/types.h>
2829
#include <linux/watchdog.h>
2930

30-
#include <asm/system_misc.h>
31-
3231
#define WDT_MAX_TIMEOUT 16
3332
#define WDT_MIN_TIMEOUT 1
3433
#define WDT_MODE_TIMEOUT(n) ((n) << 3)
@@ -50,6 +49,7 @@ static unsigned int timeout = WDT_MAX_TIMEOUT;
5049
struct sunxi_wdt_dev {
5150
struct watchdog_device wdt_dev;
5251
void __iomem *wdt_base;
52+
struct notifier_block restart_handler;
5353
};
5454

5555
/*
@@ -74,24 +74,29 @@ static const int wdt_timeout_map[] = {
7474
[16] = 0xB, /* 16s */
7575
};
7676

77-
static void __iomem *reboot_wdt_base;
7877

79-
static void sun4i_wdt_restart(enum reboot_mode mode, const char *cmd)
78+
static int sunxi_restart_handle(struct notifier_block *this, unsigned long mode,
79+
void *cmd)
8080
{
81+
struct sunxi_wdt_dev *sunxi_wdt = container_of(this,
82+
struct sunxi_wdt_dev,
83+
restart_handler);
84+
void __iomem *wdt_base = sunxi_wdt->wdt_base;
85+
8186
/* Enable timer and set reset bit in the watchdog */
82-
writel(WDT_MODE_EN | WDT_MODE_RST_EN, reboot_wdt_base + WDT_MODE);
87+
writel(WDT_MODE_EN | WDT_MODE_RST_EN, wdt_base + WDT_MODE);
8388

8489
/*
8590
* Restart the watchdog. The default (and lowest) interval
8691
* value for the watchdog is 0.5s.
8792
*/
88-
writel(WDT_CTRL_RELOAD, reboot_wdt_base + WDT_CTRL);
93+
writel(WDT_CTRL_RELOAD, wdt_base + WDT_CTRL);
8994

9095
while (1) {
9196
mdelay(5);
92-
writel(WDT_MODE_EN | WDT_MODE_RST_EN,
93-
reboot_wdt_base + WDT_MODE);
97+
writel(WDT_MODE_EN | WDT_MODE_RST_EN, wdt_base + WDT_MODE);
9498
}
99+
return NOTIFY_DONE;
95100
}
96101

97102
static int sunxi_wdt_ping(struct watchdog_device *wdt_dev)
@@ -205,8 +210,12 @@ static int sunxi_wdt_probe(struct platform_device *pdev)
205210
if (unlikely(err))
206211
return err;
207212

208-
reboot_wdt_base = sunxi_wdt->wdt_base;
209-
arm_pm_restart = sun4i_wdt_restart;
213+
sunxi_wdt->restart_handler.notifier_call = sunxi_restart_handle;
214+
sunxi_wdt->restart_handler.priority = 128;
215+
err = register_restart_handler(&sunxi_wdt->restart_handler);
216+
if (err)
217+
dev_err(&pdev->dev,
218+
"cannot register restart handler (err=%d)\n", err);
210219

211220
dev_info(&pdev->dev, "Watchdog enabled (timeout=%d sec, nowayout=%d)",
212221
sunxi_wdt->wdt_dev.timeout, nowayout);
@@ -218,7 +227,7 @@ static int sunxi_wdt_remove(struct platform_device *pdev)
218227
{
219228
struct sunxi_wdt_dev *sunxi_wdt = platform_get_drvdata(pdev);
220229

221-
arm_pm_restart = NULL;
230+
unregister_restart_handler(&sunxi_wdt->restart_handler);
222231

223232
watchdog_unregister_device(&sunxi_wdt->wdt_dev);
224233
watchdog_set_drvdata(&sunxi_wdt->wdt_dev, NULL);

‎include/linux/reboot.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ extern int reboot_force;
3838
extern int register_reboot_notifier(struct notifier_block *);
3939
extern int unregister_reboot_notifier(struct notifier_block *);
4040

41+
extern int register_restart_handler(struct notifier_block *);
42+
extern int unregister_restart_handler(struct notifier_block *);
43+
extern void do_kernel_restart(char *cmd);
4144

4245
/*
4346
* Architecture-specific implementations of sys_reboot commands.

‎kernel/reboot.c‎

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,87 @@ int unregister_reboot_notifier(struct notifier_block *nb)
104104
}
105105
EXPORT_SYMBOL(unregister_reboot_notifier);
106106

107+
/*
108+
* Notifier list for kernel code which wants to be called
109+
* to restart the system.
110+
*/
111+
static ATOMIC_NOTIFIER_HEAD(restart_handler_list);
112+
113+
/**
114+
* register_restart_handler - Register function to be called to reset
115+
* the system
116+
* @nb: Info about handler function to be called
117+
* @nb->priority: Handler priority. Handlers should follow the
118+
* following guidelines for setting priorities.
119+
* 0: Restart handler of last resort,
120+
* with limited restart capabilities
121+
* 128: Default restart handler; use if no other
122+
* restart handler is expected to be available,
123+
* and/or if restart functionality is
124+
* sufficient to restart the entire system
125+
* 255: Highest priority restart handler, will
126+
* preempt all other restart handlers
127+
*
128+
* Registers a function with code to be called to restart the
129+
* system.
130+
*
131+
* Registered functions will be called from machine_restart as last
132+
* step of the restart sequence (if the architecture specific
133+
* machine_restart function calls do_kernel_restart - see below
134+
* for details).
135+
* Registered functions are expected to restart the system immediately.
136+
* If more than one function is registered, the restart handler priority
137+
* selects which function will be called first.
138+
*
139+
* Restart handlers are expected to be registered from non-architecture
140+
* code, typically from drivers. A typical use case would be a system
141+
* where restart functionality is provided through a watchdog. Multiple
142+
* restart handlers may exist; for example, one restart handler might
143+
* restart the entire system, while another only restarts the CPU.
144+
* In such cases, the restart handler which only restarts part of the
145+
* hardware is expected to register with low priority to ensure that
146+
* it only runs if no other means to restart the system is available.
147+
*
148+
* Currently always returns zero, as atomic_notifier_chain_register()
149+
* always returns zero.
150+
*/
151+
int register_restart_handler(struct notifier_block *nb)
152+
{
153+
return atomic_notifier_chain_register(&restart_handler_list, nb);
154+
}
155+
EXPORT_SYMBOL(register_restart_handler);
156+
157+
/**
158+
* unregister_restart_handler - Unregister previously registered
159+
* restart handler
160+
* @nb: Hook to be unregistered
161+
*
162+
* Unregisters a previously registered restart handler function.
163+
*
164+
* Returns zero on success, or %-ENOENT on failure.
165+
*/
166+
int unregister_restart_handler(struct notifier_block *nb)
167+
{
168+
return atomic_notifier_chain_unregister(&restart_handler_list, nb);
169+
}
170+
EXPORT_SYMBOL(unregister_restart_handler);
171+
172+
/**
173+
* do_kernel_restart - Execute kernel restart handler call chain
174+
*
175+
* Calls functions registered with register_restart_handler.
176+
*
177+
* Expected to be called from machine_restart as last step of the restart
178+
* sequence.
179+
*
180+
* Restarts the system immediately if a restart handler function has been
181+
* registered. Otherwise does nothing.
182+
*/
183+
void do_kernel_restart(char *cmd)
184+
{
185+
atomic_notifier_call_chain(&restart_handler_list, reboot_mode, cmd);
186+
}
187+
107188
void migrate_to_reboot_cpu(void)
108189
{
109190
/* The boot cpu is always logical cpu 0 */

0 commit comments

Comments
 (0)