reboot.c 19 KB

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