setup.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /*
  2. * PowerNV setup code.
  3. *
  4. * Copyright 2011 IBM Corp.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #undef DEBUG
  12. #include <linux/cpu.h>
  13. #include <linux/errno.h>
  14. #include <linux/sched.h>
  15. #include <linux/kernel.h>
  16. #include <linux/tty.h>
  17. #include <linux/reboot.h>
  18. #include <linux/init.h>
  19. #include <linux/console.h>
  20. #include <linux/delay.h>
  21. #include <linux/irq.h>
  22. #include <linux/seq_file.h>
  23. #include <linux/of.h>
  24. #include <linux/of_fdt.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/bug.h>
  27. #include <linux/pci.h>
  28. #include <linux/cpufreq.h>
  29. #include <asm/machdep.h>
  30. #include <asm/firmware.h>
  31. #include <asm/xics.h>
  32. #include <asm/opal.h>
  33. #include <asm/kexec.h>
  34. #include <asm/smp.h>
  35. #include "powernv.h"
  36. static void __init pnv_setup_arch(void)
  37. {
  38. set_arch_panic_timeout(10, ARCH_PANIC_TIMEOUT);
  39. /* Initialize SMP */
  40. pnv_smp_init();
  41. /* Setup PCI */
  42. pnv_pci_init();
  43. /* Setup RTC and NVRAM callbacks */
  44. if (firmware_has_feature(FW_FEATURE_OPAL))
  45. opal_nvram_init();
  46. /* Enable NAP mode */
  47. powersave_nap = 1;
  48. /* XXX PMCS */
  49. }
  50. static void __init pnv_init(void)
  51. {
  52. /*
  53. * Initialize the LPC bus now so that legacy serial
  54. * ports can be found on it
  55. */
  56. opal_lpc_init();
  57. #ifdef CONFIG_HVC_OPAL
  58. if (firmware_has_feature(FW_FEATURE_OPAL))
  59. hvc_opal_init_early();
  60. else
  61. #endif
  62. add_preferred_console("hvc", 0, NULL);
  63. }
  64. static void __init pnv_init_IRQ(void)
  65. {
  66. xics_init();
  67. WARN_ON(!ppc_md.get_irq);
  68. }
  69. static void pnv_show_cpuinfo(struct seq_file *m)
  70. {
  71. struct device_node *root;
  72. const char *model = "";
  73. root = of_find_node_by_path("/");
  74. if (root)
  75. model = of_get_property(root, "model", NULL);
  76. seq_printf(m, "machine\t\t: PowerNV %s\n", model);
  77. if (firmware_has_feature(FW_FEATURE_OPAL))
  78. seq_printf(m, "firmware\t: OPAL\n");
  79. else
  80. seq_printf(m, "firmware\t: BML\n");
  81. of_node_put(root);
  82. }
  83. static void pnv_prepare_going_down(void)
  84. {
  85. /*
  86. * Disable all notifiers from OPAL, we can't
  87. * service interrupts anymore anyway
  88. */
  89. opal_event_shutdown();
  90. /* Soft disable interrupts */
  91. local_irq_disable();
  92. /*
  93. * Return secondary CPUs to firwmare if a flash update
  94. * is pending otherwise we will get all sort of error
  95. * messages about CPU being stuck etc.. This will also
  96. * have the side effect of hard disabling interrupts so
  97. * past this point, the kernel is effectively dead.
  98. */
  99. opal_flash_term_callback();
  100. }
  101. static void __noreturn pnv_restart(char *cmd)
  102. {
  103. long rc = OPAL_BUSY;
  104. pnv_prepare_going_down();
  105. while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) {
  106. rc = opal_cec_reboot();
  107. if (rc == OPAL_BUSY_EVENT)
  108. opal_poll_events(NULL);
  109. else
  110. mdelay(10);
  111. }
  112. for (;;)
  113. opal_poll_events(NULL);
  114. }
  115. static void __noreturn pnv_power_off(void)
  116. {
  117. long rc = OPAL_BUSY;
  118. pnv_prepare_going_down();
  119. while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) {
  120. rc = opal_cec_power_down(0);
  121. if (rc == OPAL_BUSY_EVENT)
  122. opal_poll_events(NULL);
  123. else
  124. mdelay(10);
  125. }
  126. for (;;)
  127. opal_poll_events(NULL);
  128. }
  129. static void __noreturn pnv_halt(void)
  130. {
  131. pnv_power_off();
  132. }
  133. static void pnv_progress(char *s, unsigned short hex)
  134. {
  135. }
  136. static void pnv_shutdown(void)
  137. {
  138. /* Let the PCI code clear up IODA tables */
  139. pnv_pci_shutdown();
  140. /*
  141. * Stop OPAL activity: Unregister all OPAL interrupts so they
  142. * don't fire up while we kexec and make sure all potentially
  143. * DMA'ing ops are complete (such as dump retrieval).
  144. */
  145. opal_shutdown();
  146. }
  147. #ifdef CONFIG_KEXEC_CORE
  148. static void pnv_kexec_wait_secondaries_down(void)
  149. {
  150. int my_cpu, i, notified = -1;
  151. my_cpu = get_cpu();
  152. for_each_online_cpu(i) {
  153. uint8_t status;
  154. int64_t rc, timeout = 1000;
  155. if (i == my_cpu)
  156. continue;
  157. for (;;) {
  158. rc = opal_query_cpu_status(get_hard_smp_processor_id(i),
  159. &status);
  160. if (rc != OPAL_SUCCESS || status != OPAL_THREAD_STARTED)
  161. break;
  162. barrier();
  163. if (i != notified) {
  164. printk(KERN_INFO "kexec: waiting for cpu %d "
  165. "(physical %d) to enter OPAL\n",
  166. i, paca[i].hw_cpu_id);
  167. notified = i;
  168. }
  169. /*
  170. * On crash secondaries might be unreachable or hung,
  171. * so timeout if we've waited too long
  172. * */
  173. mdelay(1);
  174. if (timeout-- == 0) {
  175. printk(KERN_ERR "kexec: timed out waiting for "
  176. "cpu %d (physical %d) to enter OPAL\n",
  177. i, paca[i].hw_cpu_id);
  178. break;
  179. }
  180. }
  181. }
  182. }
  183. static void pnv_kexec_cpu_down(int crash_shutdown, int secondary)
  184. {
  185. xics_kexec_teardown_cpu(secondary);
  186. /* On OPAL, we return all CPUs to firmware */
  187. if (!firmware_has_feature(FW_FEATURE_OPAL))
  188. return;
  189. if (secondary) {
  190. /* Return secondary CPUs to firmware on OPAL v3 */
  191. mb();
  192. get_paca()->kexec_state = KEXEC_STATE_REAL_MODE;
  193. mb();
  194. /* Return the CPU to OPAL */
  195. opal_return_cpu();
  196. } else {
  197. /* Primary waits for the secondaries to have reached OPAL */
  198. pnv_kexec_wait_secondaries_down();
  199. /*
  200. * We might be running as little-endian - now that interrupts
  201. * are disabled, reset the HILE bit to big-endian so we don't
  202. * take interrupts in the wrong endian later
  203. */
  204. opal_reinit_cpus(OPAL_REINIT_CPUS_HILE_BE);
  205. }
  206. }
  207. #endif /* CONFIG_KEXEC_CORE */
  208. #ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
  209. static unsigned long pnv_memory_block_size(void)
  210. {
  211. return 256UL * 1024 * 1024;
  212. }
  213. #endif
  214. static void __init pnv_setup_machdep_opal(void)
  215. {
  216. ppc_md.get_boot_time = opal_get_boot_time;
  217. ppc_md.restart = pnv_restart;
  218. pm_power_off = pnv_power_off;
  219. ppc_md.halt = pnv_halt;
  220. ppc_md.machine_check_exception = opal_machine_check;
  221. ppc_md.mce_check_early_recovery = opal_mce_check_early_recovery;
  222. ppc_md.hmi_exception_early = opal_hmi_exception_early;
  223. ppc_md.handle_hmi_exception = opal_handle_hmi_exception;
  224. }
  225. static int __init pnv_probe(void)
  226. {
  227. if (!of_machine_is_compatible("ibm,powernv"))
  228. return 0;
  229. if (firmware_has_feature(FW_FEATURE_OPAL))
  230. pnv_setup_machdep_opal();
  231. pr_debug("PowerNV detected !\n");
  232. pnv_init();
  233. return 1;
  234. }
  235. /*
  236. * Returns the cpu frequency for 'cpu' in Hz. This is used by
  237. * /proc/cpuinfo
  238. */
  239. static unsigned long pnv_get_proc_freq(unsigned int cpu)
  240. {
  241. unsigned long ret_freq;
  242. ret_freq = cpufreq_quick_get(cpu) * 1000ul;
  243. /*
  244. * If the backend cpufreq driver does not exist,
  245. * then fallback to old way of reporting the clockrate.
  246. */
  247. if (!ret_freq)
  248. ret_freq = ppc_proc_freq;
  249. return ret_freq;
  250. }
  251. define_machine(powernv) {
  252. .name = "PowerNV",
  253. .probe = pnv_probe,
  254. .setup_arch = pnv_setup_arch,
  255. .init_IRQ = pnv_init_IRQ,
  256. .show_cpuinfo = pnv_show_cpuinfo,
  257. .get_proc_freq = pnv_get_proc_freq,
  258. .progress = pnv_progress,
  259. .machine_shutdown = pnv_shutdown,
  260. .power_save = NULL,
  261. .calibrate_decr = generic_calibrate_decr,
  262. #ifdef CONFIG_KEXEC_CORE
  263. .kexec_cpu_down = pnv_kexec_cpu_down,
  264. #endif
  265. #ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
  266. .memory_block_size = pnv_memory_block_size,
  267. #endif
  268. };