reboot.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765
  1. #include <linux/module.h>
  2. #include <linux/reboot.h>
  3. #include <linux/init.h>
  4. #include <linux/pm.h>
  5. #include <linux/efi.h>
  6. #include <linux/dmi.h>
  7. #include <linux/sched.h>
  8. #include <linux/tboot.h>
  9. #include <linux/delay.h>
  10. #include <acpi/reboot.h>
  11. #include <asm/io.h>
  12. #include <asm/apic.h>
  13. #include <asm/desc.h>
  14. #include <asm/hpet.h>
  15. #include <asm/pgtable.h>
  16. #include <asm/proto.h>
  17. #include <asm/reboot_fixups.h>
  18. #include <asm/reboot.h>
  19. #include <asm/pci_x86.h>
  20. #include <asm/virtext.h>
  21. #include <asm/cpu.h>
  22. #include <asm/nmi.h>
  23. #ifdef CONFIG_X86_32
  24. # include <linux/ctype.h>
  25. # include <linux/mc146818rtc.h>
  26. #else
  27. # include <asm/x86_init.h>
  28. #endif
  29. /*
  30. * Power off function, if any
  31. */
  32. void (*pm_power_off)(void);
  33. EXPORT_SYMBOL(pm_power_off);
  34. static const struct desc_ptr no_idt = {};
  35. static int reboot_mode;
  36. enum reboot_type reboot_type = BOOT_KBD;
  37. int reboot_force;
  38. #if defined(CONFIG_X86_32) && defined(CONFIG_SMP)
  39. static int reboot_cpu = -1;
  40. #endif
  41. /* This is set if we need to go through the 'emergency' path.
  42. * When machine_emergency_restart() is called, we may be on
  43. * an inconsistent state and won't be able to do a clean cleanup
  44. */
  45. static int reboot_emergency;
  46. /* This is set by the PCI code if either type 1 or type 2 PCI is detected */
  47. bool port_cf9_safe = false;
  48. /* reboot=b[ios] | s[mp] | t[riple] | k[bd] | e[fi] [, [w]arm | [c]old] | p[ci]
  49. warm Don't set the cold reboot flag
  50. cold Set the cold reboot flag
  51. bios Reboot by jumping through the BIOS (only for X86_32)
  52. smp Reboot by executing reset on BSP or other CPU (only for X86_32)
  53. triple Force a triple fault (init)
  54. kbd Use the keyboard controller. cold reset (default)
  55. acpi Use the RESET_REG in the FADT
  56. efi Use efi reset_system runtime service
  57. pci Use the so-called "PCI reset register", CF9
  58. force Avoid anything that could hang.
  59. */
  60. static int __init reboot_setup(char *str)
  61. {
  62. for (;;) {
  63. switch (*str) {
  64. case 'w':
  65. reboot_mode = 0x1234;
  66. break;
  67. case 'c':
  68. reboot_mode = 0;
  69. break;
  70. #ifdef CONFIG_X86_32
  71. #ifdef CONFIG_SMP
  72. case 's':
  73. if (isdigit(*(str+1))) {
  74. reboot_cpu = (int) (*(str+1) - '0');
  75. if (isdigit(*(str+2)))
  76. reboot_cpu = reboot_cpu*10 + (int)(*(str+2) - '0');
  77. }
  78. /* we will leave sorting out the final value
  79. when we are ready to reboot, since we might not
  80. have detected BSP APIC ID or smp_num_cpu */
  81. break;
  82. #endif /* CONFIG_SMP */
  83. case 'b':
  84. #endif
  85. case 'a':
  86. case 'k':
  87. case 't':
  88. case 'e':
  89. case 'p':
  90. reboot_type = *str;
  91. break;
  92. case 'f':
  93. reboot_force = 1;
  94. break;
  95. }
  96. str = strchr(str, ',');
  97. if (str)
  98. str++;
  99. else
  100. break;
  101. }
  102. return 1;
  103. }
  104. __setup("reboot=", reboot_setup);
  105. #ifdef CONFIG_X86_32
  106. /*
  107. * Reboot options and system auto-detection code provided by
  108. * Dell Inc. so their systems "just work". :-)
  109. */
  110. /*
  111. * Some machines require the "reboot=b" commandline option,
  112. * this quirk makes that automatic.
  113. */
  114. static int __init set_bios_reboot(const struct dmi_system_id *d)
  115. {
  116. if (reboot_type != BOOT_BIOS) {
  117. reboot_type = BOOT_BIOS;
  118. printk(KERN_INFO "%s series board detected. Selecting BIOS-method for reboots.\n", d->ident);
  119. }
  120. return 0;
  121. }
  122. static struct dmi_system_id __initdata reboot_dmi_table[] = {
  123. { /* Handle problems with rebooting on Dell E520's */
  124. .callback = set_bios_reboot,
  125. .ident = "Dell E520",
  126. .matches = {
  127. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  128. DMI_MATCH(DMI_PRODUCT_NAME, "Dell DM061"),
  129. },
  130. },
  131. { /* Handle problems with rebooting on Dell 1300's */
  132. .callback = set_bios_reboot,
  133. .ident = "Dell PowerEdge 1300",
  134. .matches = {
  135. DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
  136. DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 1300/"),
  137. },
  138. },
  139. { /* Handle problems with rebooting on Dell 300's */
  140. .callback = set_bios_reboot,
  141. .ident = "Dell PowerEdge 300",
  142. .matches = {
  143. DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
  144. DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 300/"),
  145. },
  146. },
  147. { /* Handle problems with rebooting on Dell Optiplex 745's SFF*/
  148. .callback = set_bios_reboot,
  149. .ident = "Dell OptiPlex 745",
  150. .matches = {
  151. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  152. DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 745"),
  153. },
  154. },
  155. { /* Handle problems with rebooting on Dell Optiplex 745's DFF*/
  156. .callback = set_bios_reboot,
  157. .ident = "Dell OptiPlex 745",
  158. .matches = {
  159. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  160. DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 745"),
  161. DMI_MATCH(DMI_BOARD_NAME, "0MM599"),
  162. },
  163. },
  164. { /* Handle problems with rebooting on Dell Optiplex 745 with 0KW626 */
  165. .callback = set_bios_reboot,
  166. .ident = "Dell OptiPlex 745",
  167. .matches = {
  168. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  169. DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 745"),
  170. DMI_MATCH(DMI_BOARD_NAME, "0KW626"),
  171. },
  172. },
  173. { /* Handle problems with rebooting on Dell Optiplex 330 with 0KP561 */
  174. .callback = set_bios_reboot,
  175. .ident = "Dell OptiPlex 330",
  176. .matches = {
  177. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  178. DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 330"),
  179. DMI_MATCH(DMI_BOARD_NAME, "0KP561"),
  180. },
  181. },
  182. { /* Handle problems with rebooting on Dell Optiplex 360 with 0T656F */
  183. .callback = set_bios_reboot,
  184. .ident = "Dell OptiPlex 360",
  185. .matches = {
  186. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  187. DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 360"),
  188. DMI_MATCH(DMI_BOARD_NAME, "0T656F"),
  189. },
  190. },
  191. { /* Handle problems with rebooting on Dell OptiPlex 760 with 0G919G*/
  192. .callback = set_bios_reboot,
  193. .ident = "Dell OptiPlex 760",
  194. .matches = {
  195. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  196. DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 760"),
  197. DMI_MATCH(DMI_BOARD_NAME, "0G919G"),
  198. },
  199. },
  200. { /* Handle problems with rebooting on Dell 2400's */
  201. .callback = set_bios_reboot,
  202. .ident = "Dell PowerEdge 2400",
  203. .matches = {
  204. DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
  205. DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 2400"),
  206. },
  207. },
  208. { /* Handle problems with rebooting on Dell T5400's */
  209. .callback = set_bios_reboot,
  210. .ident = "Dell Precision T5400",
  211. .matches = {
  212. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  213. DMI_MATCH(DMI_PRODUCT_NAME, "Precision WorkStation T5400"),
  214. },
  215. },
  216. { /* Handle problems with rebooting on Dell T7400's */
  217. .callback = set_bios_reboot,
  218. .ident = "Dell Precision T7400",
  219. .matches = {
  220. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  221. DMI_MATCH(DMI_PRODUCT_NAME, "Precision WorkStation T7400"),
  222. },
  223. },
  224. { /* Handle problems with rebooting on HP laptops */
  225. .callback = set_bios_reboot,
  226. .ident = "HP Compaq Laptop",
  227. .matches = {
  228. DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
  229. DMI_MATCH(DMI_PRODUCT_NAME, "HP Compaq"),
  230. },
  231. },
  232. { /* Handle problems with rebooting on Dell XPS710 */
  233. .callback = set_bios_reboot,
  234. .ident = "Dell XPS710",
  235. .matches = {
  236. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  237. DMI_MATCH(DMI_PRODUCT_NAME, "Dell XPS710"),
  238. },
  239. },
  240. { /* Handle problems with rebooting on Dell DXP061 */
  241. .callback = set_bios_reboot,
  242. .ident = "Dell DXP061",
  243. .matches = {
  244. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  245. DMI_MATCH(DMI_PRODUCT_NAME, "Dell DXP061"),
  246. },
  247. },
  248. { /* Handle problems with rebooting on Sony VGN-Z540N */
  249. .callback = set_bios_reboot,
  250. .ident = "Sony VGN-Z540N",
  251. .matches = {
  252. DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
  253. DMI_MATCH(DMI_PRODUCT_NAME, "VGN-Z540N"),
  254. },
  255. },
  256. { /* Handle problems with rebooting on CompuLab SBC-FITPC2 */
  257. .callback = set_bios_reboot,
  258. .ident = "CompuLab SBC-FITPC2",
  259. .matches = {
  260. DMI_MATCH(DMI_SYS_VENDOR, "CompuLab"),
  261. DMI_MATCH(DMI_PRODUCT_NAME, "SBC-FITPC2"),
  262. },
  263. },
  264. { /* Handle problems with rebooting on ASUS P4S800 */
  265. .callback = set_bios_reboot,
  266. .ident = "ASUS P4S800",
  267. .matches = {
  268. DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
  269. DMI_MATCH(DMI_BOARD_NAME, "P4S800"),
  270. },
  271. },
  272. { /* Handle problems with rebooting on VersaLogic Menlow boards */
  273. .callback = set_bios_reboot,
  274. .ident = "VersaLogic Menlow based board",
  275. .matches = {
  276. DMI_MATCH(DMI_BOARD_VENDOR, "VersaLogic Corporation"),
  277. DMI_MATCH(DMI_BOARD_NAME, "VersaLogic Menlow board"),
  278. },
  279. },
  280. { }
  281. };
  282. static int __init reboot_init(void)
  283. {
  284. dmi_check_system(reboot_dmi_table);
  285. return 0;
  286. }
  287. core_initcall(reboot_init);
  288. extern const unsigned char machine_real_restart_asm[];
  289. extern const u64 machine_real_restart_gdt[3];
  290. void machine_real_restart(unsigned int type)
  291. {
  292. void *restart_va;
  293. unsigned long restart_pa;
  294. void (*restart_lowmem)(unsigned int);
  295. u64 *lowmem_gdt;
  296. local_irq_disable();
  297. /* Write zero to CMOS register number 0x0f, which the BIOS POST
  298. routine will recognize as telling it to do a proper reboot. (Well
  299. that's what this book in front of me says -- it may only apply to
  300. the Phoenix BIOS though, it's not clear). At the same time,
  301. disable NMIs by setting the top bit in the CMOS address register,
  302. as we're about to do peculiar things to the CPU. I'm not sure if
  303. `outb_p' is needed instead of just `outb'. Use it to be on the
  304. safe side. (Yes, CMOS_WRITE does outb_p's. - Paul G.)
  305. */
  306. spin_lock(&rtc_lock);
  307. CMOS_WRITE(0x00, 0x8f);
  308. spin_unlock(&rtc_lock);
  309. /*
  310. * Switch back to the initial page table.
  311. */
  312. load_cr3(initial_page_table);
  313. /* Write 0x1234 to absolute memory location 0x472. The BIOS reads
  314. this on booting to tell it to "Bypass memory test (also warm
  315. boot)". This seems like a fairly standard thing that gets set by
  316. REBOOT.COM programs, and the previous reset routine did this
  317. too. */
  318. *((unsigned short *)0x472) = reboot_mode;
  319. /* Patch the GDT in the low memory trampoline */
  320. lowmem_gdt = TRAMPOLINE_SYM(machine_real_restart_gdt);
  321. restart_va = TRAMPOLINE_SYM(machine_real_restart_asm);
  322. restart_pa = virt_to_phys(restart_va);
  323. restart_lowmem = (void (*)(unsigned int))restart_pa;
  324. /* GDT[0]: GDT self-pointer */
  325. lowmem_gdt[0] =
  326. (u64)(sizeof(machine_real_restart_gdt) - 1) +
  327. ((u64)virt_to_phys(lowmem_gdt) << 16);
  328. /* GDT[1]: 64K real mode code segment */
  329. lowmem_gdt[1] =
  330. GDT_ENTRY(0x009b, restart_pa, 0xffff);
  331. /* Jump to the identity-mapped low memory code */
  332. restart_lowmem(type);
  333. }
  334. #ifdef CONFIG_APM_MODULE
  335. EXPORT_SYMBOL(machine_real_restart);
  336. #endif
  337. #endif /* CONFIG_X86_32 */
  338. /*
  339. * Some Apple MacBook and MacBookPro's needs reboot=p to be able to reboot
  340. */
  341. static int __init set_pci_reboot(const struct dmi_system_id *d)
  342. {
  343. if (reboot_type != BOOT_CF9) {
  344. reboot_type = BOOT_CF9;
  345. printk(KERN_INFO "%s series board detected. "
  346. "Selecting PCI-method for reboots.\n", d->ident);
  347. }
  348. return 0;
  349. }
  350. static struct dmi_system_id __initdata pci_reboot_dmi_table[] = {
  351. { /* Handle problems with rebooting on Apple MacBook5 */
  352. .callback = set_pci_reboot,
  353. .ident = "Apple MacBook5",
  354. .matches = {
  355. DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
  356. DMI_MATCH(DMI_PRODUCT_NAME, "MacBook5"),
  357. },
  358. },
  359. { /* Handle problems with rebooting on Apple MacBookPro5 */
  360. .callback = set_pci_reboot,
  361. .ident = "Apple MacBookPro5",
  362. .matches = {
  363. DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
  364. DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro5"),
  365. },
  366. },
  367. { /* Handle problems with rebooting on Apple Macmini3,1 */
  368. .callback = set_pci_reboot,
  369. .ident = "Apple Macmini3,1",
  370. .matches = {
  371. DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
  372. DMI_MATCH(DMI_PRODUCT_NAME, "Macmini3,1"),
  373. },
  374. },
  375. { /* Handle problems with rebooting on the iMac9,1. */
  376. .callback = set_pci_reboot,
  377. .ident = "Apple iMac9,1",
  378. .matches = {
  379. DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
  380. DMI_MATCH(DMI_PRODUCT_NAME, "iMac9,1"),
  381. },
  382. },
  383. { }
  384. };
  385. static int __init pci_reboot_init(void)
  386. {
  387. dmi_check_system(pci_reboot_dmi_table);
  388. return 0;
  389. }
  390. core_initcall(pci_reboot_init);
  391. static inline void kb_wait(void)
  392. {
  393. int i;
  394. for (i = 0; i < 0x10000; i++) {
  395. if ((inb(0x64) & 0x02) == 0)
  396. break;
  397. udelay(2);
  398. }
  399. }
  400. static void vmxoff_nmi(int cpu, struct die_args *args)
  401. {
  402. cpu_emergency_vmxoff();
  403. }
  404. /* Use NMIs as IPIs to tell all CPUs to disable virtualization
  405. */
  406. static void emergency_vmx_disable_all(void)
  407. {
  408. /* Just make sure we won't change CPUs while doing this */
  409. local_irq_disable();
  410. /* We need to disable VMX on all CPUs before rebooting, otherwise
  411. * we risk hanging up the machine, because the CPU ignore INIT
  412. * signals when VMX is enabled.
  413. *
  414. * We can't take any locks and we may be on an inconsistent
  415. * state, so we use NMIs as IPIs to tell the other CPUs to disable
  416. * VMX and halt.
  417. *
  418. * For safety, we will avoid running the nmi_shootdown_cpus()
  419. * stuff unnecessarily, but we don't have a way to check
  420. * if other CPUs have VMX enabled. So we will call it only if the
  421. * CPU we are running on has VMX enabled.
  422. *
  423. * We will miss cases where VMX is not enabled on all CPUs. This
  424. * shouldn't do much harm because KVM always enable VMX on all
  425. * CPUs anyway. But we can miss it on the small window where KVM
  426. * is still enabling VMX.
  427. */
  428. if (cpu_has_vmx() && cpu_vmx_enabled()) {
  429. /* Disable VMX on this CPU.
  430. */
  431. cpu_vmxoff();
  432. /* Halt and disable VMX on the other CPUs */
  433. nmi_shootdown_cpus(vmxoff_nmi);
  434. }
  435. }
  436. void __attribute__((weak)) mach_reboot_fixups(void)
  437. {
  438. }
  439. static void native_machine_emergency_restart(void)
  440. {
  441. int i;
  442. if (reboot_emergency)
  443. emergency_vmx_disable_all();
  444. tboot_shutdown(TB_SHUTDOWN_REBOOT);
  445. /* Tell the BIOS if we want cold or warm reboot */
  446. *((unsigned short *)__va(0x472)) = reboot_mode;
  447. for (;;) {
  448. /* Could also try the reset bit in the Hammer NB */
  449. switch (reboot_type) {
  450. case BOOT_KBD:
  451. mach_reboot_fixups(); /* for board specific fixups */
  452. for (i = 0; i < 10; i++) {
  453. kb_wait();
  454. udelay(50);
  455. outb(0xfe, 0x64); /* pulse reset low */
  456. udelay(50);
  457. }
  458. case BOOT_TRIPLE:
  459. load_idt(&no_idt);
  460. __asm__ __volatile__("int3");
  461. reboot_type = BOOT_KBD;
  462. break;
  463. #ifdef CONFIG_X86_32
  464. case BOOT_BIOS:
  465. machine_real_restart(MRR_BIOS);
  466. reboot_type = BOOT_KBD;
  467. break;
  468. #endif
  469. case BOOT_ACPI:
  470. acpi_reboot();
  471. reboot_type = BOOT_KBD;
  472. break;
  473. case BOOT_EFI:
  474. if (efi_enabled)
  475. efi.reset_system(reboot_mode ?
  476. EFI_RESET_WARM :
  477. EFI_RESET_COLD,
  478. EFI_SUCCESS, 0, NULL);
  479. reboot_type = BOOT_KBD;
  480. break;
  481. case BOOT_CF9:
  482. port_cf9_safe = true;
  483. /* fall through */
  484. case BOOT_CF9_COND:
  485. if (port_cf9_safe) {
  486. u8 cf9 = inb(0xcf9) & ~6;
  487. outb(cf9|2, 0xcf9); /* Request hard reset */
  488. udelay(50);
  489. outb(cf9|6, 0xcf9); /* Actually do the reset */
  490. udelay(50);
  491. }
  492. reboot_type = BOOT_KBD;
  493. break;
  494. }
  495. }
  496. }
  497. void native_machine_shutdown(void)
  498. {
  499. /* Stop the cpus and apics */
  500. #ifdef CONFIG_SMP
  501. /* The boot cpu is always logical cpu 0 */
  502. int reboot_cpu_id = 0;
  503. #ifdef CONFIG_X86_32
  504. /* See if there has been given a command line override */
  505. if ((reboot_cpu != -1) && (reboot_cpu < nr_cpu_ids) &&
  506. cpu_online(reboot_cpu))
  507. reboot_cpu_id = reboot_cpu;
  508. #endif
  509. /* Make certain the cpu I'm about to reboot on is online */
  510. if (!cpu_online(reboot_cpu_id))
  511. reboot_cpu_id = smp_processor_id();
  512. /* Make certain I only run on the appropriate processor */
  513. set_cpus_allowed_ptr(current, cpumask_of(reboot_cpu_id));
  514. /* O.K Now that I'm on the appropriate processor,
  515. * stop all of the others.
  516. */
  517. stop_other_cpus();
  518. #endif
  519. lapic_shutdown();
  520. #ifdef CONFIG_X86_IO_APIC
  521. disable_IO_APIC();
  522. #endif
  523. #ifdef CONFIG_HPET_TIMER
  524. hpet_disable();
  525. #endif
  526. #ifdef CONFIG_X86_64
  527. x86_platform.iommu_shutdown();
  528. #endif
  529. }
  530. static void __machine_emergency_restart(int emergency)
  531. {
  532. reboot_emergency = emergency;
  533. machine_ops.emergency_restart();
  534. }
  535. static void native_machine_restart(char *__unused)
  536. {
  537. printk("machine restart\n");
  538. if (!reboot_force)
  539. machine_shutdown();
  540. __machine_emergency_restart(0);
  541. }
  542. static void native_machine_halt(void)
  543. {
  544. /* stop other cpus and apics */
  545. machine_shutdown();
  546. tboot_shutdown(TB_SHUTDOWN_HALT);
  547. /* stop this cpu */
  548. stop_this_cpu(NULL);
  549. }
  550. static void native_machine_power_off(void)
  551. {
  552. if (pm_power_off) {
  553. if (!reboot_force)
  554. machine_shutdown();
  555. pm_power_off();
  556. }
  557. /* a fallback in case there is no PM info available */
  558. tboot_shutdown(TB_SHUTDOWN_HALT);
  559. }
  560. struct machine_ops machine_ops = {
  561. .power_off = native_machine_power_off,
  562. .shutdown = native_machine_shutdown,
  563. .emergency_restart = native_machine_emergency_restart,
  564. .restart = native_machine_restart,
  565. .halt = native_machine_halt,
  566. #ifdef CONFIG_KEXEC
  567. .crash_shutdown = native_machine_crash_shutdown,
  568. #endif
  569. };
  570. void machine_power_off(void)
  571. {
  572. machine_ops.power_off();
  573. }
  574. void machine_shutdown(void)
  575. {
  576. machine_ops.shutdown();
  577. }
  578. void machine_emergency_restart(void)
  579. {
  580. __machine_emergency_restart(1);
  581. }
  582. void machine_restart(char *cmd)
  583. {
  584. machine_ops.restart(cmd);
  585. }
  586. void machine_halt(void)
  587. {
  588. machine_ops.halt();
  589. }
  590. #ifdef CONFIG_KEXEC
  591. void machine_crash_shutdown(struct pt_regs *regs)
  592. {
  593. machine_ops.crash_shutdown(regs);
  594. }
  595. #endif
  596. #if defined(CONFIG_SMP)
  597. /* This keeps a track of which one is crashing cpu. */
  598. static int crashing_cpu;
  599. static nmi_shootdown_cb shootdown_callback;
  600. static atomic_t waiting_for_crash_ipi;
  601. static int crash_nmi_callback(struct notifier_block *self,
  602. unsigned long val, void *data)
  603. {
  604. int cpu;
  605. if (val != DIE_NMI)
  606. return NOTIFY_OK;
  607. cpu = raw_smp_processor_id();
  608. /* Don't do anything if this handler is invoked on crashing cpu.
  609. * Otherwise, system will completely hang. Crashing cpu can get
  610. * an NMI if system was initially booted with nmi_watchdog parameter.
  611. */
  612. if (cpu == crashing_cpu)
  613. return NOTIFY_STOP;
  614. local_irq_disable();
  615. shootdown_callback(cpu, (struct die_args *)data);
  616. atomic_dec(&waiting_for_crash_ipi);
  617. /* Assume hlt works */
  618. halt();
  619. for (;;)
  620. cpu_relax();
  621. return 1;
  622. }
  623. static void smp_send_nmi_allbutself(void)
  624. {
  625. apic->send_IPI_allbutself(NMI_VECTOR);
  626. }
  627. static struct notifier_block crash_nmi_nb = {
  628. .notifier_call = crash_nmi_callback,
  629. /* we want to be the first one called */
  630. .priority = NMI_LOCAL_HIGH_PRIOR+1,
  631. };
  632. /* Halt all other CPUs, calling the specified function on each of them
  633. *
  634. * This function can be used to halt all other CPUs on crash
  635. * or emergency reboot time. The function passed as parameter
  636. * will be called inside a NMI handler on all CPUs.
  637. */
  638. void nmi_shootdown_cpus(nmi_shootdown_cb callback)
  639. {
  640. unsigned long msecs;
  641. local_irq_disable();
  642. /* Make a note of crashing cpu. Will be used in NMI callback.*/
  643. crashing_cpu = safe_smp_processor_id();
  644. shootdown_callback = callback;
  645. atomic_set(&waiting_for_crash_ipi, num_online_cpus() - 1);
  646. /* Would it be better to replace the trap vector here? */
  647. if (register_die_notifier(&crash_nmi_nb))
  648. return; /* return what? */
  649. /* Ensure the new callback function is set before sending
  650. * out the NMI
  651. */
  652. wmb();
  653. smp_send_nmi_allbutself();
  654. msecs = 1000; /* Wait at most a second for the other cpus to stop */
  655. while ((atomic_read(&waiting_for_crash_ipi) > 0) && msecs) {
  656. mdelay(1);
  657. msecs--;
  658. }
  659. /* Leave the nmi callback set */
  660. }
  661. #else /* !CONFIG_SMP */
  662. void nmi_shootdown_cpus(nmi_shootdown_cb callback)
  663. {
  664. /* No other CPUs to shoot down */
  665. }
  666. #endif