ce789144fbc634462fd137d68663b19ec6e14392
[linux-2.6.git] / arch / x86_64 / kernel / reboot.c
1 /* 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>
9 #include <asm/io.h>
10 #include <asm/kdebug.h>
11 #include <asm/delay.h>
12 #include <asm/hw_irq.h>
13 #include <asm/system.h>
14 #include <asm/pgtable.h>
15 #include <asm/tlbflush.h>
16 #include <asm/apic.h>
17
18 /*
19  * Power off function, if any
20  */
21 void (*pm_power_off)(void);
22
23 static long no_idt[3];
24 static enum { 
25         BOOT_BIOS = 'b',
26         BOOT_TRIPLE = 't',
27         BOOT_KBD = 'k'
28 } reboot_type = BOOT_KBD;
29 static int reboot_mode = 0;
30
31 /* reboot=b[ios] | t[riple] | k[bd] [, [w]arm | [c]old]
32    bios   Use the CPU reboot vector for warm reset
33    warm   Don't set the cold reboot flag
34    cold   Set the cold reboot flag
35    triple Force a triple fault (init)
36    kbd    Use the keyboard controller. cold reset (default)
37  */ 
38 static int __init reboot_setup(char *str)
39 {
40         for (;;) {
41                 switch (*str) {
42                 case 'w': 
43                         reboot_mode = 0x1234;
44                         break;
45
46                 case 'c':
47                         reboot_mode = 0;
48                         break;
49
50                 case 't':
51                 case 'b':
52                 case 'k':
53                         reboot_type = *str;
54                         break;
55                 }
56                 if((str = strchr(str,',')) != NULL)
57                         str++;
58                 else
59                         break;
60         }
61         return 1;
62 }
63
64 __setup("reboot=", reboot_setup);
65
66 /* overwrites random kernel memory. Should not be kernel .text */
67 #define WARMBOOT_TRAMP 0x1000UL
68
69 static void reboot_warm(void)
70 {
71         extern unsigned char warm_reboot[], warm_reboot_end[];
72         printk("warm reboot\n");
73
74         local_irq_disable(); 
75                 
76         /* restore identity mapping */
77         init_level4_pgt[0] = __pml4(__pa(level3_ident_pgt) | 7); 
78         __flush_tlb_all(); 
79
80         /* Move the trampoline to low memory */
81         memcpy(__va(WARMBOOT_TRAMP), warm_reboot, warm_reboot_end - warm_reboot); 
82
83         /* Start it in compatibility mode. */
84         asm volatile( "   pushq $0\n"           /* ss */
85                      "   pushq $0x2000\n"       /* rsp */
86                      "   pushfq\n"              /* eflags */
87                      "   pushq %[cs]\n"
88                      "   pushq %[target]\n"
89                      "   iretq" :: 
90                       [cs] "i" (__KERNEL_COMPAT32_CS), 
91                       [target] "b" (WARMBOOT_TRAMP));
92 }
93
94 static inline void kb_wait(void)
95 {
96         int i;
97   
98         for (i=0; i<0x10000; i++)
99                 if ((inb_p(0x64) & 0x02) == 0)
100                         break;
101 }
102   
103 void machine_shutdown(void)
104 {
105         /* Stop the cpus and apics */
106 #ifdef CONFIG_SMP
107         int reboot_cpu_id;
108   
109         /* The boot cpu is always logical cpu 0 */
110         reboot_cpu_id = 0;
111
112         /* Make certain the cpu I'm about to reboot on is online */
113         if (!cpu_isset(reboot_cpu_id, cpu_online_map)) {
114                 reboot_cpu_id = smp_processor_id();
115         }
116
117         /* Make certain I only run on the appropriate processor */
118         set_cpus_allowed(current, cpumask_of_cpu(reboot_cpu_id));
119
120         /* O.K Now that I'm on the appropriate processor,
121          * stop all of the others.
122          */
123         smp_send_stop();
124 #endif
125
126         local_irq_disable();
127   
128 #ifndef CONFIG_SMP
129         disable_local_APIC();
130 #endif
131
132         disable_IO_APIC();
133
134         local_irq_enable();
135 }
136
137 void machine_restart(char * __unused)
138 {
139         int i;
140
141         machine_shutdown();
142
143         local_irq_disable();
144        
145 #ifndef CONFIG_SMP
146         disable_local_APIC();
147 #endif
148
149         disable_IO_APIC();
150         
151         local_irq_enable();
152         
153         /* Tell the BIOS if we want cold or warm reboot */
154         *((unsigned short *)__va(0x472)) = reboot_mode;
155        
156         for (;;) {
157                 /* Could also try the reset bit in the Hammer NB */
158                 switch (reboot_type) { 
159                 case BOOT_BIOS:
160                         reboot_warm();
161
162                 case BOOT_KBD:
163                 for (i=0; i<100; i++) {
164                         kb_wait();
165                         udelay(50);
166                         outb(0xfe,0x64);         /* pulse reset low */
167                         udelay(50);
168                 }
169
170                 case BOOT_TRIPLE: 
171                 __asm__ __volatile__("lidt (%0)": :"r" (&no_idt));
172                 __asm__ __volatile__("int3");
173
174                         reboot_type = BOOT_KBD;
175                         break;
176                 }      
177         }      
178 }
179
180 EXPORT_SYMBOL(machine_restart);
181
182 void machine_halt(void)
183 {
184 }
185
186 EXPORT_SYMBOL(machine_halt);
187
188 void machine_power_off(void)
189 {
190         if (pm_power_off)
191                 pm_power_off();
192 }
193
194 EXPORT_SYMBOL(machine_power_off);