blob: 307f996a39331ce63bfde75160f429d037e2f92b [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>
Aaron Durbinfa20efd2008-01-30 13:31:17 +010012#include <acpi/reboot.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <asm/delay.h>
Glauber de Oliveira Costa9d1c6e72007-10-19 20:35:03 +020015#include <asm/desc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <asm/hw_irq.h>
17#include <asm/system.h>
18#include <asm/pgtable.h>
19#include <asm/tlbflush.h>
20#include <asm/apic.h>
OGAWA Hirofumic86c7fb2007-12-03 17:17:10 +010021#include <asm/hpet.h>
Joerg Roedel395624f2007-10-24 12:49:47 +020022#include <asm/gart.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24/*
25 * Power off function, if any
26 */
27void (*pm_power_off)(void);
Andi Kleen2ee60e172006-06-26 13:59:44 +020028EXPORT_SYMBOL(pm_power_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30static long no_idt[3];
31static enum {
32 BOOT_TRIPLE = 't',
Aaron Durbinfa20efd2008-01-30 13:31:17 +010033 BOOT_KBD = 'k',
34 BOOT_ACPI = 'a'
Linus Torvalds1da177e2005-04-16 15:20:36 -070035} reboot_type = BOOT_KBD;
36static int reboot_mode = 0;
37int reboot_force;
38
39/* reboot=t[riple] | k[bd] [, [w]arm | [c]old]
40 warm Don't set the cold reboot flag
41 cold Set the cold reboot flag
42 triple Force a triple fault (init)
43 kbd Use the keyboard controller. cold reset (default)
Aaron Durbinfa20efd2008-01-30 13:31:17 +010044 acpi Use the RESET_REG in the FADT
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 force Avoid anything that could hang.
46 */
47static int __init reboot_setup(char *str)
48{
49 for (;;) {
50 switch (*str) {
51 case 'w':
52 reboot_mode = 0x1234;
53 break;
54
55 case 'c':
56 reboot_mode = 0;
57 break;
58
59 case 't':
Aaron Durbinfa20efd2008-01-30 13:31:17 +010060 case 'a':
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 case 'b':
62 case 'k':
63 reboot_type = *str;
64 break;
65 case 'f':
66 reboot_force = 1;
67 break;
68 }
69 if((str = strchr(str,',')) != NULL)
70 str++;
71 else
72 break;
73 }
74 return 1;
75}
76
77__setup("reboot=", reboot_setup);
78
Linus Torvalds1da177e2005-04-16 15:20:36 -070079static inline void kb_wait(void)
80{
81 int i;
82
83 for (i=0; i<0x10000; i++)
84 if ((inb_p(0x64) & 0x02) == 0)
85 break;
86}
87
Eric W. Biedermand8955952005-06-25 14:58:02 -070088void machine_shutdown(void)
89{
Andi Kleen35062292005-11-05 17:25:54 +010090 unsigned long flags;
Yinghai Lubc2cea62007-07-21 17:11:28 +020091
Eric W. Biedermand8955952005-06-25 14:58:02 -070092 /* Stop the cpus and apics */
93#ifdef CONFIG_SMP
94 int reboot_cpu_id;
95
96 /* The boot cpu is always logical cpu 0 */
97 reboot_cpu_id = 0;
98
99 /* Make certain the cpu I'm about to reboot on is online */
100 if (!cpu_isset(reboot_cpu_id, cpu_online_map)) {
101 reboot_cpu_id = smp_processor_id();
102 }
103
104 /* Make certain I only run on the appropriate processor */
105 set_cpus_allowed(current, cpumask_of_cpu(reboot_cpu_id));
106
107 /* O.K Now that I'm on the appropriate processor,
108 * stop all of the others.
109 */
110 smp_send_stop();
111#endif
112
Andi Kleen35062292005-11-05 17:25:54 +0100113 local_irq_save(flags);
Eric W. Biedermand8955952005-06-25 14:58:02 -0700114
115#ifndef CONFIG_SMP
116 disable_local_APIC();
117#endif
118
119 disable_IO_APIC();
120
OGAWA Hirofumic86c7fb2007-12-03 17:17:10 +0100121#ifdef CONFIG_HPET_TIMER
122 hpet_disable();
123#endif
Andi Kleen35062292005-11-05 17:25:54 +0100124 local_irq_restore(flags);
Yinghai Lubc2cea62007-07-21 17:11:28 +0200125
126 pci_iommu_shutdown();
Eric W. Biedermand8955952005-06-25 14:58:02 -0700127}
128
Eric W. Biederman62b3a042005-07-26 11:45:31 -0600129void machine_emergency_restart(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130{
131 int i;
132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 /* Tell the BIOS if we want cold or warm reboot */
134 *((unsigned short *)__va(0x472)) = reboot_mode;
135
136 for (;;) {
137 /* Could also try the reset bit in the Hammer NB */
138 switch (reboot_type) {
139 case BOOT_KBD:
Andi Kleena6f5deb2005-11-05 17:25:54 +0100140 for (i=0; i<10; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 kb_wait();
142 udelay(50);
143 outb(0xfe,0x64); /* pulse reset low */
144 udelay(50);
145 }
146
147 case BOOT_TRIPLE:
Glauber de Oliveira Costa9d1c6e72007-10-19 20:35:03 +0200148 load_idt((const struct desc_ptr *)&no_idt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 __asm__ __volatile__("int3");
150
151 reboot_type = BOOT_KBD;
152 break;
Aaron Durbinfa20efd2008-01-30 13:31:17 +0100153
154 case BOOT_ACPI:
155 acpi_reboot();
156 reboot_type = BOOT_KBD;
157 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 }
159 }
160}
161
Eric W. Biederman62b3a042005-07-26 11:45:31 -0600162void machine_restart(char * __unused)
163{
164 printk("machine restart\n");
165
166 if (!reboot_force) {
167 machine_shutdown();
168 }
169 machine_emergency_restart();
170}
171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172void machine_halt(void)
173{
174}
175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176void machine_power_off(void)
177{
Eric W. Biederman6e3fbee2006-01-11 22:43:12 +0100178 if (pm_power_off) {
179 if (!reboot_force) {
180 machine_shutdown();
181 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 pm_power_off();
Eric W. Biederman6e3fbee2006-01-11 22:43:12 +0100183 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184}
185