blob: 71b13c5f5817dd0d1ffbe2285fe003c11e6ea088 [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>
Glauber de Oliveira Costa9d1c6e72007-10-19 20:35:03 +020014#include <asm/desc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <asm/hw_irq.h>
16#include <asm/system.h>
17#include <asm/pgtable.h>
18#include <asm/tlbflush.h>
19#include <asm/apic.h>
Joerg Roedel395624f2007-10-24 12:49:47 +020020#include <asm/gart.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
22/*
23 * Power off function, if any
24 */
25void (*pm_power_off)(void);
Andi Kleen2ee60e172006-06-26 13:59:44 +020026EXPORT_SYMBOL(pm_power_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
28static long no_idt[3];
29static enum {
30 BOOT_TRIPLE = 't',
31 BOOT_KBD = 'k'
32} reboot_type = BOOT_KBD;
33static int reboot_mode = 0;
34int reboot_force;
35
36/* reboot=t[riple] | k[bd] [, [w]arm | [c]old]
37 warm Don't set the cold reboot flag
38 cold Set the cold reboot flag
39 triple Force a triple fault (init)
40 kbd Use the keyboard controller. cold reset (default)
41 force Avoid anything that could hang.
42 */
43static int __init reboot_setup(char *str)
44{
45 for (;;) {
46 switch (*str) {
47 case 'w':
48 reboot_mode = 0x1234;
49 break;
50
51 case 'c':
52 reboot_mode = 0;
53 break;
54
55 case 't':
56 case 'b':
57 case 'k':
58 reboot_type = *str;
59 break;
60 case 'f':
61 reboot_force = 1;
62 break;
63 }
64 if((str = strchr(str,',')) != NULL)
65 str++;
66 else
67 break;
68 }
69 return 1;
70}
71
72__setup("reboot=", reboot_setup);
73
Linus Torvalds1da177e2005-04-16 15:20:36 -070074static inline void kb_wait(void)
75{
76 int i;
77
78 for (i=0; i<0x10000; i++)
79 if ((inb_p(0x64) & 0x02) == 0)
80 break;
81}
82
Eric W. Biedermand8955952005-06-25 14:58:02 -070083void machine_shutdown(void)
84{
Andi Kleen35062292005-11-05 17:25:54 +010085 unsigned long flags;
Yinghai Lubc2cea62007-07-21 17:11:28 +020086
Eric W. Biedermand8955952005-06-25 14:58:02 -070087 /* Stop the cpus and apics */
88#ifdef CONFIG_SMP
89 int reboot_cpu_id;
90
91 /* The boot cpu is always logical cpu 0 */
92 reboot_cpu_id = 0;
93
94 /* Make certain the cpu I'm about to reboot on is online */
95 if (!cpu_isset(reboot_cpu_id, cpu_online_map)) {
96 reboot_cpu_id = smp_processor_id();
97 }
98
99 /* Make certain I only run on the appropriate processor */
100 set_cpus_allowed(current, cpumask_of_cpu(reboot_cpu_id));
101
102 /* O.K Now that I'm on the appropriate processor,
103 * stop all of the others.
104 */
105 smp_send_stop();
106#endif
107
Andi Kleen35062292005-11-05 17:25:54 +0100108 local_irq_save(flags);
Eric W. Biedermand8955952005-06-25 14:58:02 -0700109
110#ifndef CONFIG_SMP
111 disable_local_APIC();
112#endif
113
114 disable_IO_APIC();
115
Andi Kleen35062292005-11-05 17:25:54 +0100116 local_irq_restore(flags);
Yinghai Lubc2cea62007-07-21 17:11:28 +0200117
118 pci_iommu_shutdown();
Eric W. Biedermand8955952005-06-25 14:58:02 -0700119}
120
Eric W. Biederman62b3a042005-07-26 11:45:31 -0600121void machine_emergency_restart(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
123 int i;
124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 /* Tell the BIOS if we want cold or warm reboot */
126 *((unsigned short *)__va(0x472)) = reboot_mode;
127
128 for (;;) {
129 /* Could also try the reset bit in the Hammer NB */
130 switch (reboot_type) {
131 case BOOT_KBD:
Andi Kleena6f5deb2005-11-05 17:25:54 +0100132 for (i=0; i<10; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 kb_wait();
134 udelay(50);
135 outb(0xfe,0x64); /* pulse reset low */
136 udelay(50);
137 }
138
139 case BOOT_TRIPLE:
Glauber de Oliveira Costa9d1c6e72007-10-19 20:35:03 +0200140 load_idt((const struct desc_ptr *)&no_idt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 __asm__ __volatile__("int3");
142
143 reboot_type = BOOT_KBD;
144 break;
145 }
146 }
147}
148
Eric W. Biederman62b3a042005-07-26 11:45:31 -0600149void machine_restart(char * __unused)
150{
151 printk("machine restart\n");
152
153 if (!reboot_force) {
154 machine_shutdown();
155 }
156 machine_emergency_restart();
157}
158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159void machine_halt(void)
160{
161}
162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163void machine_power_off(void)
164{
Eric W. Biederman6e3fbee2006-01-11 22:43:12 +0100165 if (pm_power_off) {
166 if (!reboot_force) {
167 machine_shutdown();
168 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 pm_power_off();
Eric W. Biederman6e3fbee2006-01-11 22:43:12 +0100170 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171}
172