This commit was manufactured by cvs2svn to create tag
[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 int reboot_mode = 0;
25 enum reboot_types reboot_type = BOOT_KBD;
26 EXPORT_SYMBOL(reboot_type);
27 int reboot_override;
28 EXPORT_SYMBOL(reboot_override);
29
30 void (*machine_reset)(void);
31 EXPORT_SYMBOL(machine_reset);
32
33 /* reboot=b[ios] | t[riple] | k[bd] [, [w]arm | [c]old] | [a]cpi
34    bios   Use the CPU reboot vector for warm reset
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    acpi   Use the ACPI reset mechanism defined in the FADT
40  */ 
41 static 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                 case 'a':
57                         reboot_type = *str;
58                         reboot_override = 1;
59                         break;
60                 }
61                 if((str = strchr(str,',')) != NULL)
62                         str++;
63                 else
64                         break;
65         }
66         return 1;
67 }
68
69 __setup("reboot=", reboot_setup);
70
71 /* overwrites random kernel memory. Should not be kernel .text */
72 #define WARMBOOT_TRAMP 0x1000UL
73
74 static void reboot_warm(void)
75 {
76         extern unsigned char warm_reboot[], warm_reboot_end[];
77         printk("warm reboot\n");
78
79         local_irq_disable(); 
80                 
81         /* restore identity mapping */
82         init_level4_pgt[0] = __pml4(__pa(level3_ident_pgt) | 7); 
83         __flush_tlb_all(); 
84
85         /* Move the trampoline to low memory */
86         memcpy(__va(WARMBOOT_TRAMP), warm_reboot, warm_reboot_end - warm_reboot); 
87
88         /* Start it in compatibility mode. */
89         asm volatile( "   pushq $0\n"           /* ss */
90                      "   pushq $0x2000\n"       /* rsp */
91                      "   pushfq\n"              /* eflags */
92                      "   pushq %[cs]\n"
93                      "   pushq %[target]\n"
94                      "   iretq" :: 
95                       [cs] "i" (__KERNEL_COMPAT32_CS), 
96                       [target] "b" (WARMBOOT_TRAMP));
97 }
98
99 static inline void kb_wait(void)
100 {
101         int i;
102
103         for (i=0; i<0x10000; i++)
104                 if ((inb_p(0x64) & 0x02) == 0)
105                         break;
106 }
107
108 void machine_shutdown(void)
109 {
110         /* Stop the cpus and apics */
111 #ifdef CONFIG_SMP
112         int reboot_cpu_id;
113
114         /* The boot cpu is always logical cpu 0 */
115         reboot_cpu_id = 0;
116
117         /* Make certain the cpu I'm about to reboot on is online */
118         if (!cpu_isset(reboot_cpu_id, cpu_online_map)) {
119                 reboot_cpu_id = smp_processor_id();
120         }
121
122         /* Make certain I only run on the appropriate processor */
123         set_cpus_allowed(current, cpumask_of_cpu(reboot_cpu_id));
124
125         /* O.K Now that I'm on the appropriate processor,
126          * stop all of the others.
127          */
128         smp_send_stop();
129 #endif
130
131         local_irq_disable();
132
133 #ifndef CONFIG_SMP
134         disable_local_APIC();
135 #endif
136
137         disable_IO_APIC();
138
139         local_irq_enable();
140 }
141
142 void machine_restart(char * __unused)
143 {
144         int i;
145
146         machine_shutdown();
147         
148         /* Tell the BIOS if we want cold or warm reboot */
149         *((unsigned short *)__va(0x472)) = reboot_mode;
150        
151         for (;;) {
152                 /* Could also try the reset bit in the Hammer NB */
153                 switch (reboot_type) { 
154                 case BOOT_ACPI:
155                         if (machine_reset)
156                                 (*machine_reset)();
157                         else
158                                 reboot_type = BOOT_KBD;
159                         break;
160                 
161                 case BOOT_BIOS:
162                         reboot_warm();
163
164                 case BOOT_KBD:
165                 for (i=0; i<100; i++) {
166                         kb_wait();
167                         udelay(50);
168                         outb(0xfe,0x64);         /* pulse reset low */
169                         udelay(50);
170                 }
171
172                 case BOOT_TRIPLE: 
173                 __asm__ __volatile__("lidt (%0)": :"r" (&no_idt));
174                 __asm__ __volatile__("int3");
175
176                         reboot_type = BOOT_KBD;
177                         break;
178                 }      
179         }      
180 }
181
182 EXPORT_SYMBOL(machine_restart);
183
184 void machine_halt(void)
185 {
186 }
187
188 EXPORT_SYMBOL(machine_halt);
189
190 void machine_power_off(void)
191 {
192         if (pm_power_off)
193                 pm_power_off();
194 }
195
196 EXPORT_SYMBOL(machine_power_off);