setup.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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 <asm/tm.h>
  36. #include <asm/setup.h>
  37. #include <asm/security_features.h>
  38. #include "powernv.h"
  39. static bool fw_feature_is(const char *state, const char *name,
  40. struct device_node *fw_features)
  41. {
  42. struct device_node *np;
  43. bool rc = false;
  44. np = of_get_child_by_name(fw_features, name);
  45. if (np) {
  46. rc = of_property_read_bool(np, state);
  47. of_node_put(np);
  48. }
  49. return rc;
  50. }
  51. static void init_fw_feat_flags(struct device_node *np)
  52. {
  53. if (fw_feature_is("enabled", "inst-spec-barrier-ori31,31,0", np))
  54. security_ftr_set(SEC_FTR_SPEC_BAR_ORI31);
  55. if (fw_feature_is("enabled", "fw-bcctrl-serialized", np))
  56. security_ftr_set(SEC_FTR_BCCTRL_SERIALISED);
  57. if (fw_feature_is("enabled", "inst-l1d-flush-ori30,30,0", np))
  58. security_ftr_set(SEC_FTR_L1D_FLUSH_ORI30);
  59. if (fw_feature_is("enabled", "inst-l1d-flush-trig2", np))
  60. security_ftr_set(SEC_FTR_L1D_FLUSH_TRIG2);
  61. if (fw_feature_is("enabled", "fw-l1d-thread-split", np))
  62. security_ftr_set(SEC_FTR_L1D_THREAD_PRIV);
  63. if (fw_feature_is("enabled", "fw-count-cache-disabled", np))
  64. security_ftr_set(SEC_FTR_COUNT_CACHE_DISABLED);
  65. /*
  66. * The features below are enabled by default, so we instead look to see
  67. * if firmware has *disabled* them, and clear them if so.
  68. */
  69. if (fw_feature_is("disabled", "speculation-policy-favor-security", np))
  70. security_ftr_clear(SEC_FTR_FAVOUR_SECURITY);
  71. if (fw_feature_is("disabled", "needs-l1d-flush-msr-pr-0-to-1", np))
  72. security_ftr_clear(SEC_FTR_L1D_FLUSH_PR);
  73. if (fw_feature_is("disabled", "needs-l1d-flush-msr-hv-1-to-0", np))
  74. security_ftr_clear(SEC_FTR_L1D_FLUSH_HV);
  75. if (fw_feature_is("disabled", "needs-spec-barrier-for-bound-checks", np))
  76. security_ftr_clear(SEC_FTR_BNDS_CHK_SPEC_BAR);
  77. }
  78. static void pnv_setup_rfi_flush(void)
  79. {
  80. struct device_node *np, *fw_features;
  81. enum l1d_flush_type type;
  82. bool enable;
  83. /* Default to fallback in case fw-features are not available */
  84. type = L1D_FLUSH_FALLBACK;
  85. np = of_find_node_by_name(NULL, "ibm,opal");
  86. fw_features = of_get_child_by_name(np, "fw-features");
  87. of_node_put(np);
  88. if (fw_features) {
  89. init_fw_feat_flags(fw_features);
  90. of_node_put(fw_features);
  91. if (security_ftr_enabled(SEC_FTR_L1D_FLUSH_TRIG2))
  92. type = L1D_FLUSH_MTTRIG;
  93. if (security_ftr_enabled(SEC_FTR_L1D_FLUSH_ORI30))
  94. type = L1D_FLUSH_ORI;
  95. }
  96. enable = security_ftr_enabled(SEC_FTR_FAVOUR_SECURITY) && \
  97. (security_ftr_enabled(SEC_FTR_L1D_FLUSH_PR) || \
  98. security_ftr_enabled(SEC_FTR_L1D_FLUSH_HV));
  99. setup_rfi_flush(type, enable);
  100. }
  101. static void __init pnv_setup_arch(void)
  102. {
  103. set_arch_panic_timeout(10, ARCH_PANIC_TIMEOUT);
  104. pnv_setup_rfi_flush();
  105. setup_stf_barrier();
  106. /* Initialize SMP */
  107. pnv_smp_init();
  108. /* Setup PCI */
  109. pnv_pci_init();
  110. /* Setup RTC and NVRAM callbacks */
  111. if (firmware_has_feature(FW_FEATURE_OPAL))
  112. opal_nvram_init();
  113. /* Enable NAP mode */
  114. powersave_nap = 1;
  115. /* XXX PMCS */
  116. }
  117. static void __init pnv_init(void)
  118. {
  119. /*
  120. * Initialize the LPC bus now so that legacy serial
  121. * ports can be found on it
  122. */
  123. opal_lpc_init();
  124. #ifdef CONFIG_HVC_OPAL
  125. if (firmware_has_feature(FW_FEATURE_OPAL))
  126. hvc_opal_init_early();
  127. else
  128. #endif
  129. add_preferred_console("hvc", 0, NULL);
  130. }
  131. static void __init pnv_init_IRQ(void)
  132. {
  133. xics_init();
  134. WARN_ON(!ppc_md.get_irq);
  135. }
  136. static void pnv_show_cpuinfo(struct seq_file *m)
  137. {
  138. struct device_node *root;
  139. const char *model = "";
  140. root = of_find_node_by_path("/");
  141. if (root)
  142. model = of_get_property(root, "model", NULL);
  143. seq_printf(m, "machine\t\t: PowerNV %s\n", model);
  144. if (firmware_has_feature(FW_FEATURE_OPAL))
  145. seq_printf(m, "firmware\t: OPAL\n");
  146. else
  147. seq_printf(m, "firmware\t: BML\n");
  148. of_node_put(root);
  149. }
  150. static void pnv_prepare_going_down(void)
  151. {
  152. /*
  153. * Disable all notifiers from OPAL, we can't
  154. * service interrupts anymore anyway
  155. */
  156. opal_event_shutdown();
  157. /* Soft disable interrupts */
  158. local_irq_disable();
  159. /*
  160. * Return secondary CPUs to firwmare if a flash update
  161. * is pending otherwise we will get all sort of error
  162. * messages about CPU being stuck etc.. This will also
  163. * have the side effect of hard disabling interrupts so
  164. * past this point, the kernel is effectively dead.
  165. */
  166. opal_flash_term_callback();
  167. }
  168. static void __noreturn pnv_restart(char *cmd)
  169. {
  170. long rc = OPAL_BUSY;
  171. pnv_prepare_going_down();
  172. while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) {
  173. rc = opal_cec_reboot();
  174. if (rc == OPAL_BUSY_EVENT)
  175. opal_poll_events(NULL);
  176. else
  177. mdelay(10);
  178. }
  179. for (;;)
  180. opal_poll_events(NULL);
  181. }
  182. static void __noreturn pnv_power_off(void)
  183. {
  184. long rc = OPAL_BUSY;
  185. pnv_prepare_going_down();
  186. while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) {
  187. rc = opal_cec_power_down(0);
  188. if (rc == OPAL_BUSY_EVENT)
  189. opal_poll_events(NULL);
  190. else
  191. mdelay(10);
  192. }
  193. for (;;)
  194. opal_poll_events(NULL);
  195. }
  196. static void __noreturn pnv_halt(void)
  197. {
  198. pnv_power_off();
  199. }
  200. static void pnv_progress(char *s, unsigned short hex)
  201. {
  202. }
  203. static void pnv_shutdown(void)
  204. {
  205. /* Let the PCI code clear up IODA tables */
  206. pnv_pci_shutdown();
  207. /*
  208. * Stop OPAL activity: Unregister all OPAL interrupts so they
  209. * don't fire up while we kexec and make sure all potentially
  210. * DMA'ing ops are complete (such as dump retrieval).
  211. */
  212. opal_shutdown();
  213. }
  214. #ifdef CONFIG_KEXEC
  215. static void pnv_kexec_wait_secondaries_down(void)
  216. {
  217. int my_cpu, i, notified = -1;
  218. my_cpu = get_cpu();
  219. for_each_online_cpu(i) {
  220. uint8_t status;
  221. int64_t rc, timeout = 1000;
  222. if (i == my_cpu)
  223. continue;
  224. for (;;) {
  225. rc = opal_query_cpu_status(get_hard_smp_processor_id(i),
  226. &status);
  227. if (rc != OPAL_SUCCESS || status != OPAL_THREAD_STARTED)
  228. break;
  229. barrier();
  230. if (i != notified) {
  231. printk(KERN_INFO "kexec: waiting for cpu %d "
  232. "(physical %d) to enter OPAL\n",
  233. i, paca[i].hw_cpu_id);
  234. notified = i;
  235. }
  236. /*
  237. * On crash secondaries might be unreachable or hung,
  238. * so timeout if we've waited too long
  239. * */
  240. mdelay(1);
  241. if (timeout-- == 0) {
  242. printk(KERN_ERR "kexec: timed out waiting for "
  243. "cpu %d (physical %d) to enter OPAL\n",
  244. i, paca[i].hw_cpu_id);
  245. break;
  246. }
  247. }
  248. }
  249. }
  250. static void pnv_kexec_cpu_down(int crash_shutdown, int secondary)
  251. {
  252. xics_kexec_teardown_cpu(secondary);
  253. /* On OPAL, we return all CPUs to firmware */
  254. if (!firmware_has_feature(FW_FEATURE_OPAL))
  255. return;
  256. if (secondary) {
  257. /* Return secondary CPUs to firmware on OPAL v3 */
  258. mb();
  259. get_paca()->kexec_state = KEXEC_STATE_REAL_MODE;
  260. mb();
  261. /* Return the CPU to OPAL */
  262. opal_return_cpu();
  263. } else {
  264. /* Primary waits for the secondaries to have reached OPAL */
  265. pnv_kexec_wait_secondaries_down();
  266. /*
  267. * We might be running as little-endian - now that interrupts
  268. * are disabled, reset the HILE bit to big-endian so we don't
  269. * take interrupts in the wrong endian later
  270. */
  271. opal_reinit_cpus(OPAL_REINIT_CPUS_HILE_BE);
  272. }
  273. }
  274. #endif /* CONFIG_KEXEC */
  275. #ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
  276. static unsigned long pnv_memory_block_size(void)
  277. {
  278. return 256UL * 1024 * 1024;
  279. }
  280. #endif
  281. static void __init pnv_setup_machdep_opal(void)
  282. {
  283. ppc_md.get_boot_time = opal_get_boot_time;
  284. ppc_md.restart = pnv_restart;
  285. pm_power_off = pnv_power_off;
  286. ppc_md.halt = pnv_halt;
  287. ppc_md.machine_check_exception = opal_machine_check;
  288. ppc_md.mce_check_early_recovery = opal_mce_check_early_recovery;
  289. ppc_md.hmi_exception_early = opal_hmi_exception_early;
  290. ppc_md.handle_hmi_exception = opal_handle_hmi_exception;
  291. }
  292. static int __init pnv_probe(void)
  293. {
  294. if (!of_machine_is_compatible("ibm,powernv"))
  295. return 0;
  296. if (firmware_has_feature(FW_FEATURE_OPAL))
  297. pnv_setup_machdep_opal();
  298. pr_debug("PowerNV detected !\n");
  299. pnv_init();
  300. return 1;
  301. }
  302. /*
  303. * Returns the cpu frequency for 'cpu' in Hz. This is used by
  304. * /proc/cpuinfo
  305. */
  306. static unsigned long pnv_get_proc_freq(unsigned int cpu)
  307. {
  308. unsigned long ret_freq;
  309. ret_freq = cpufreq_get(cpu) * 1000ul;
  310. /*
  311. * If the backend cpufreq driver does not exist,
  312. * then fallback to old way of reporting the clockrate.
  313. */
  314. if (!ret_freq)
  315. ret_freq = ppc_proc_freq;
  316. return ret_freq;
  317. }
  318. define_machine(powernv) {
  319. .name = "PowerNV",
  320. .probe = pnv_probe,
  321. .setup_arch = pnv_setup_arch,
  322. .init_IRQ = pnv_init_IRQ,
  323. .show_cpuinfo = pnv_show_cpuinfo,
  324. .get_proc_freq = pnv_get_proc_freq,
  325. .progress = pnv_progress,
  326. .machine_shutdown = pnv_shutdown,
  327. .power_save = NULL,
  328. .calibrate_decr = generic_calibrate_decr,
  329. #ifdef CONFIG_KEXEC
  330. .kexec_cpu_down = pnv_kexec_cpu_down,
  331. #endif
  332. #ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
  333. .memory_block_size = pnv_memory_block_size,
  334. #endif
  335. };