reboot.c 19 KB

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