This commit was manufactured by cvs2svn to create branch
[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_TRIPLE = 't',
26         BOOT_KBD = 'k'
27 } reboot_type = BOOT_KBD;
28 static int reboot_mode = 0;
29 int reboot_force;
30
31 /* reboot=t[riple] | k[bd] [, [w]arm | [c]old]
32    warm   Don't set the cold reboot flag
33    cold   Set the cold reboot flag
34    triple Force a triple fault (init)
35    kbd    Use the keyboard controller. cold reset (default)
36    force  Avoid anything that could hang.
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                 case 'f':
56                         reboot_force = 1;
57                         break;
58                 }
59                 if((str = strchr(str,',')) != NULL)
60                         str++;
61                 else
62                         break;
63         }
64         return 1;
65 }
66
67 __setup("reboot=", reboot_setup);
68
69 /* overwrites random kernel memory. Should not be kernel .text */
70 #define WARMBOOT_TRAMP 0x1000UL
71
72 static void reboot_warm(void)
73 {
74         extern unsigned char warm_reboot[], warm_reboot_end[];
75         printk("warm reboot\n");
76
77         local_irq_disable(); 
78                 
79         /* restore identity mapping */
80         init_level4_pgt[0] = __pml4(__pa(level3_ident_pgt) | 7); 
81         __flush_tlb_all(); 
82
83         /* Move the trampoline to low memory */
84         memcpy(__va(WARMBOOT_TRAMP), warm_reboot, warm_reboot_end - warm_reboot); 
85
86         /* Start it in compatibility mode. */
87         asm volatile( "   pushq $0\n"           /* ss */
88                      "   pushq $0x2000\n"       /* rsp */
89                      "   pushfq\n"              /* eflags */
90                      "   pushq %[cs]\n"
91                      "   pushq %[target]\n"
92                      "   iretq" :: 
93                       [cs] "i" (__KERNEL_COMPAT32_CS), 
94                       [target] "b" (WARMBOOT_TRAMP));
95 }
96
97 static inline void kb_wait(void)
98 {
99         int i;
100   
101         for (i=0; i<0x10000; i++)
102                 if ((inb_p(0x64) & 0x02) == 0)
103                         break;
104 }
105   
106 void machine_shutdown(void)
107 {
108         /* Stop the cpus and apics */
109 #ifdef CONFIG_SMP
110         int reboot_cpu_id;
111   
112         /* The boot cpu is always logical cpu 0 */
113         reboot_cpu_id = 0;
114
115         /* Make certain the cpu I'm about to reboot on is online */
116         if (!cpu_isset(reboot_cpu_id, cpu_online_map)) {
117                 reboot_cpu_id = smp_processor_id();
118         }
119
120         /* Make certain I only run on the appropriate processor */
121         set_cpus_allowed(current, cpumask_of_cpu(reboot_cpu_id));
122
123         /* O.K Now that I'm on the appropriate processor,
124          * stop all of the others.
125          */
126         smp_send_stop();
127 #endif
128
129         local_irq_disable();
130   
131 #ifndef CONFIG_SMP
132         disable_local_APIC();
133 #endif
134
135         disable_IO_APIC();
136
137         local_irq_enable();
138 }
139
140 static void smp_halt(void)
141 {
142         int cpuid = safe_smp_processor_id(); 
143         static int first_entry = 1;
144
145         if (reboot_force)
146                 return;
147
148         if (first_entry) {
149                 first_entry = 0;
150                 smp_call_function((void *)machine_restart, NULL, 1, 0);
151         }
152                         
153         smp_stop_cpu(); 
154
155         /* AP calling this. Just halt */
156         if (cpuid != boot_cpu_id) { 
157                 for (;;) 
158                         asm("hlt");
159         }
160
161         /* Wait for all other CPUs to have run smp_stop_cpu */
162         while (!cpus_empty(cpu_online_map))
163                 rep_nop(); 
164 }
165
166 void machine_restart(char * __unused)
167 {
168         int i;
169
170         machine_shutdown();
171         printk("machine restart\n");
172
173 #ifdef CONFIG_SMP
174         smp_halt(); 
175 #endif
176
177         if (!reboot_force) {
178                 local_irq_disable();
179 #ifndef CONFIG_SMP
180                 disable_local_APIC();
181 #endif
182                 disable_IO_APIC();
183                 local_irq_enable();
184         }
185         
186         /* Tell the BIOS if we want cold or warm reboot */
187         *((unsigned short *)__va(0x472)) = reboot_mode;
188        
189         for (;;) {
190                 /* Could also try the reset bit in the Hammer NB */
191                 switch (reboot_type) { 
192                 case BOOT_KBD:
193                 for (i=0; i<100; i++) {
194                         kb_wait();
195                         udelay(50);
196                         outb(0xfe,0x64);         /* pulse reset low */
197                         udelay(50);
198                 }
199
200                 case BOOT_TRIPLE: 
201                         __asm__ __volatile__("lidt (%0)": :"r" (&no_idt));
202                         __asm__ __volatile__("int3");
203
204                         reboot_type = BOOT_KBD;
205                         break;
206                 }      
207         }      
208 }
209
210 EXPORT_SYMBOL(machine_restart);
211
212 void machine_halt(void)
213 {
214 }
215
216 EXPORT_SYMBOL(machine_halt);
217
218 void machine_power_off(void)
219 {
220         if (pm_power_off)
221                 pm_power_off();
222 }
223
224 EXPORT_SYMBOL(machine_power_off);