blob: 7503068e788d903565c97841482125bb9993fd3b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* Various gunk just to reboot the machine. */
2#include <linux/module.h>
3#include <linux/reboot.h>
4#include <linux/init.h>
5#include <linux/smp.h>
6#include <linux/kernel.h>
7#include <linux/ctype.h>
8#include <linux/string.h>
Eric W. Biederman6e3fbee2006-01-11 22:43:12 +01009#include <linux/pm.h>
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -070010#include <linux/kdebug.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040011#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <asm/delay.h>
14#include <asm/hw_irq.h>
15#include <asm/system.h>
16#include <asm/pgtable.h>
17#include <asm/tlbflush.h>
18#include <asm/apic.h>
19
20/*
21 * Power off function, if any
22 */
23void (*pm_power_off)(void);
Andi Kleen2ee60e172006-06-26 13:59:44 +020024EXPORT_SYMBOL(pm_power_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26static long no_idt[3];
27static enum {
28 BOOT_TRIPLE = 't',
29 BOOT_KBD = 'k'
30} reboot_type = BOOT_KBD;
31static int reboot_mode = 0;
32int reboot_force;
33
34/* reboot=t[riple] | k[bd] [, [w]arm | [c]old]
35 warm Don't set the cold reboot flag
36 cold Set the cold reboot flag
37 triple Force a triple fault (init)
38 kbd Use the keyboard controller. cold reset (default)
39 force Avoid anything that could hang.
40 */
41static int __init reboot_setup(char *str)
42{
43 for (;;) {
44 switch (*str) {
45 case 'w':
46 reboot_mode = 0x1234;
47 break;
48
49 case 'c':
50 reboot_mode = 0;
51 break;
52
53 case 't':
54 case 'b':
55 case 'k':
56 reboot_type = *str;
57 break;
58 case 'f':
59 reboot_force = 1;
60 break;
61 }
62 if((str = strchr(str,',')) != NULL)
63 str++;
64 else
65 break;
66 }
67 return 1;
68}
69
70__setup("reboot=", reboot_setup);
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072static inline void kb_wait(void)
73{
74 int i;
75
76 for (i=0; i<0x10000; i++)
77 if ((inb_p(0x64) & 0x02) == 0)
78 break;
79}
80
Eric W. Biedermand8955952005-06-25 14:58:02 -070081void machine_shutdown(void)
82{
Andi Kleen35062292005-11-05 17:25:54 +010083 unsigned long flags;
Eric W. Biedermand8955952005-06-25 14:58:02 -070084 /* Stop the cpus and apics */
85#ifdef CONFIG_SMP
86 int reboot_cpu_id;
87
88 /* The boot cpu is always logical cpu 0 */
89 reboot_cpu_id = 0;
90
91 /* Make certain the cpu I'm about to reboot on is online */
92 if (!cpu_isset(reboot_cpu_id, cpu_online_map)) {
93 reboot_cpu_id = smp_processor_id();
94 }
95
96 /* Make certain I only run on the appropriate processor */
97 set_cpus_allowed(current, cpumask_of_cpu(reboot_cpu_id));
98
99 /* O.K Now that I'm on the appropriate processor,
100 * stop all of the others.
101 */
102 smp_send_stop();
103#endif
104
Andi Kleen35062292005-11-05 17:25:54 +0100105 local_irq_save(flags);
Eric W. Biedermand8955952005-06-25 14:58:02 -0700106
107#ifndef CONFIG_SMP
108 disable_local_APIC();
109#endif
110
111 disable_IO_APIC();
112
Andi Kleen35062292005-11-05 17:25:54 +0100113 local_irq_restore(flags);
Eric W. Biedermand8955952005-06-25 14:58:02 -0700114}
115
Eric W. Biederman62b3a042005-07-26 11:45:31 -0600116void machine_emergency_restart(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117{
118 int i;
119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 /* Tell the BIOS if we want cold or warm reboot */
121 *((unsigned short *)__va(0x472)) = reboot_mode;
122
123 for (;;) {
124 /* Could also try the reset bit in the Hammer NB */
125 switch (reboot_type) {
126 case BOOT_KBD:
Andi Kleena6f5deb2005-11-05 17:25:54 +0100127 for (i=0; i<10; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 kb_wait();
129 udelay(50);
130 outb(0xfe,0x64); /* pulse reset low */
131 udelay(50);
132 }
133
134 case BOOT_TRIPLE:
135 __asm__ __volatile__("lidt (%0)": :"r" (&no_idt));
136 __asm__ __volatile__("int3");
137
138 reboot_type = BOOT_KBD;
139 break;
140 }
141 }
142}
143
Eric W. Biederman62b3a042005-07-26 11:45:31 -0600144void machine_restart(char * __unused)
145{
146 printk("machine restart\n");
147
148 if (!reboot_force) {
149 machine_shutdown();
150 }
151 machine_emergency_restart();
152}
153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154void machine_halt(void)
155{
156}
157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158void machine_power_off(void)
159{
Eric W. Biederman6e3fbee2006-01-11 22:43:12 +0100160 if (pm_power_off) {
161 if (!reboot_force) {
162 machine_shutdown();
163 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 pm_power_off();
Eric W. Biederman6e3fbee2006-01-11 22:43:12 +0100165 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166}
167