setup.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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/xive.h>
  33. #include <asm/opal.h>
  34. #include <asm/kexec.h>
  35. #include <asm/smp.h>
  36. #include <asm/tm.h>
  37. #include <asm/setup.h>
  38. #include <asm/security_features.h>
  39. #include "powernv.h"
  40. static bool fw_feature_is(const char *state, const char *name,
  41. struct device_node *fw_features)
  42. {
  43. struct device_node *np;
  44. bool rc = false;
  45. np = of_get_child_by_name(fw_features, name);
  46. if (np) {
  47. rc = of_property_read_bool(np, state);
  48. of_node_put(np);
  49. }
  50. return rc;
  51. }
  52. static void init_fw_feat_flags(struct device_node *np)
  53. {
  54. if (fw_feature_is("enabled", "inst-spec-barrier-ori31,31,0", np))
  55. security_ftr_set(SEC_FTR_SPEC_BAR_ORI31);
  56. if (fw_feature_is("enabled", "fw-bcctrl-serialized", np))
  57. security_ftr_set(SEC_FTR_BCCTRL_SERIALISED);
  58. if (fw_feature_is("enabled", "inst-l1d-flush-ori30,30,0", np))
  59. security_ftr_set(SEC_FTR_L1D_FLUSH_ORI30);
  60. if (fw_feature_is("enabled", "inst-l1d-flush-trig2", np))
  61. security_ftr_set(SEC_FTR_L1D_FLUSH_TRIG2);
  62. if (fw_feature_is("enabled", "fw-l1d-thread-split", np))
  63. security_ftr_set(SEC_FTR_L1D_THREAD_PRIV);
  64. if (fw_feature_is("enabled", "fw-count-cache-disabled", np))
  65. security_ftr_set(SEC_FTR_COUNT_CACHE_DISABLED);
  66. if (fw_feature_is("enabled", "fw-count-cache-flush-bcctr2,0,0", np))
  67. security_ftr_set(SEC_FTR_BCCTR_FLUSH_ASSIST);
  68. if (fw_feature_is("enabled", "needs-count-cache-flush-on-context-switch", np))
  69. security_ftr_set(SEC_FTR_FLUSH_COUNT_CACHE);
  70. /*
  71. * The features below are enabled by default, so we instead look to see
  72. * if firmware has *disabled* them, and clear them if so.
  73. */
  74. if (fw_feature_is("disabled", "speculation-policy-favor-security", np))
  75. security_ftr_clear(SEC_FTR_FAVOUR_SECURITY);
  76. if (fw_feature_is("disabled", "needs-l1d-flush-msr-pr-0-to-1", np))
  77. security_ftr_clear(SEC_FTR_L1D_FLUSH_PR);
  78. if (fw_feature_is("disabled", "needs-l1d-flush-msr-hv-1-to-0", np))
  79. security_ftr_clear(SEC_FTR_L1D_FLUSH_HV);
  80. if (fw_feature_is("disabled", "needs-spec-barrier-for-bound-checks", np))
  81. security_ftr_clear(SEC_FTR_BNDS_CHK_SPEC_BAR);
  82. }
  83. static void pnv_setup_rfi_flush(void)
  84. {
  85. struct device_node *np, *fw_features;
  86. enum l1d_flush_type type;
  87. bool enable;
  88. /* Default to fallback in case fw-features are not available */
  89. type = L1D_FLUSH_FALLBACK;
  90. np = of_find_node_by_name(NULL, "ibm,opal");
  91. fw_features = of_get_child_by_name(np, "fw-features");
  92. of_node_put(np);
  93. if (fw_features) {
  94. init_fw_feat_flags(fw_features);
  95. of_node_put(fw_features);
  96. if (security_ftr_enabled(SEC_FTR_L1D_FLUSH_TRIG2))
  97. type = L1D_FLUSH_MTTRIG;
  98. if (security_ftr_enabled(SEC_FTR_L1D_FLUSH_ORI30))
  99. type = L1D_FLUSH_ORI;
  100. }
  101. /*
  102. * If we are non-Power9 bare metal, we don't need to flush on kernel
  103. * entry or after user access: they fix a P9 specific vulnerability.
  104. */
  105. if (!pvr_version_is(PVR_POWER9)) {
  106. security_ftr_clear(SEC_FTR_L1D_FLUSH_ENTRY);
  107. security_ftr_clear(SEC_FTR_L1D_FLUSH_UACCESS);
  108. }
  109. enable = security_ftr_enabled(SEC_FTR_FAVOUR_SECURITY) && \
  110. (security_ftr_enabled(SEC_FTR_L1D_FLUSH_PR) || \
  111. security_ftr_enabled(SEC_FTR_L1D_FLUSH_HV));
  112. setup_rfi_flush(type, enable);
  113. setup_count_cache_flush();
  114. enable = security_ftr_enabled(SEC_FTR_FAVOUR_SECURITY) &&
  115. security_ftr_enabled(SEC_FTR_L1D_FLUSH_ENTRY);
  116. setup_entry_flush(enable);
  117. enable = security_ftr_enabled(SEC_FTR_FAVOUR_SECURITY) &&
  118. security_ftr_enabled(SEC_FTR_L1D_FLUSH_UACCESS);
  119. setup_uaccess_flush(enable);
  120. }
  121. static void __init pnv_setup_arch(void)
  122. {
  123. set_arch_panic_timeout(10, ARCH_PANIC_TIMEOUT);
  124. pnv_setup_rfi_flush();
  125. setup_stf_barrier();
  126. /* Initialize SMP */
  127. pnv_smp_init();
  128. /* Setup PCI */
  129. pnv_pci_init();
  130. /* Setup RTC and NVRAM callbacks */
  131. if (firmware_has_feature(FW_FEATURE_OPAL))
  132. opal_nvram_init();
  133. /* Enable NAP mode */
  134. powersave_nap = 1;
  135. /* XXX PMCS */
  136. }
  137. static void __init pnv_init(void)
  138. {
  139. /*
  140. * Initialize the LPC bus now so that legacy serial
  141. * ports can be found on it
  142. */
  143. opal_lpc_init();
  144. #ifdef CONFIG_HVC_OPAL
  145. if (firmware_has_feature(FW_FEATURE_OPAL))
  146. hvc_opal_init_early();
  147. else
  148. #endif
  149. add_preferred_console("hvc", 0, NULL);
  150. }
  151. static void __init pnv_init_IRQ(void)
  152. {
  153. /* Try using a XIVE if available, otherwise use a XICS */
  154. if (!xive_native_init())
  155. xics_init();
  156. WARN_ON(!ppc_md.get_irq);
  157. }
  158. static void pnv_show_cpuinfo(struct seq_file *m)
  159. {
  160. struct device_node *root;
  161. const char *model = "";
  162. root = of_find_node_by_path("/");
  163. if (root)
  164. model = of_get_property(root, "model", NULL);
  165. seq_printf(m, "machine\t\t: PowerNV %s\n", model);
  166. if (firmware_has_feature(FW_FEATURE_OPAL))
  167. seq_printf(m, "firmware\t: OPAL\n");
  168. else
  169. seq_printf(m, "firmware\t: BML\n");
  170. of_node_put(root);
  171. if (radix_enabled())
  172. seq_printf(m, "MMU\t\t: Radix\n");
  173. else
  174. seq_printf(m, "MMU\t\t: Hash\n");
  175. }
  176. static void pnv_prepare_going_down(void)
  177. {
  178. /*
  179. * Disable all notifiers from OPAL, we can't
  180. * service interrupts anymore anyway
  181. */
  182. opal_event_shutdown();
  183. /* Print flash update message if one is scheduled. */
  184. opal_flash_update_print_message();
  185. smp_send_stop();
  186. hard_irq_disable();
  187. }
  188. static void __noreturn pnv_restart(char *cmd)
  189. {
  190. long rc = OPAL_BUSY;
  191. pnv_prepare_going_down();
  192. while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) {
  193. rc = opal_cec_reboot();
  194. if (rc == OPAL_BUSY_EVENT)
  195. opal_poll_events(NULL);
  196. else
  197. mdelay(10);
  198. }
  199. for (;;)
  200. opal_poll_events(NULL);
  201. }
  202. static void __noreturn pnv_power_off(void)
  203. {
  204. long rc = OPAL_BUSY;
  205. pnv_prepare_going_down();
  206. while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) {
  207. rc = opal_cec_power_down(0);
  208. if (rc == OPAL_BUSY_EVENT)
  209. opal_poll_events(NULL);
  210. else
  211. mdelay(10);
  212. }
  213. for (;;)
  214. opal_poll_events(NULL);
  215. }
  216. static void __noreturn pnv_halt(void)
  217. {
  218. pnv_power_off();
  219. }
  220. static void pnv_progress(char *s, unsigned short hex)
  221. {
  222. }
  223. static void pnv_shutdown(void)
  224. {
  225. /* Let the PCI code clear up IODA tables */
  226. pnv_pci_shutdown();
  227. /*
  228. * Stop OPAL activity: Unregister all OPAL interrupts so they
  229. * don't fire up while we kexec and make sure all potentially
  230. * DMA'ing ops are complete (such as dump retrieval).
  231. */
  232. opal_shutdown();
  233. }
  234. #ifdef CONFIG_KEXEC_CORE
  235. static void pnv_kexec_wait_secondaries_down(void)
  236. {
  237. int my_cpu, i, notified = -1;
  238. my_cpu = get_cpu();
  239. for_each_online_cpu(i) {
  240. uint8_t status;
  241. int64_t rc, timeout = 1000;
  242. if (i == my_cpu)
  243. continue;
  244. for (;;) {
  245. rc = opal_query_cpu_status(get_hard_smp_processor_id(i),
  246. &status);
  247. if (rc != OPAL_SUCCESS || status != OPAL_THREAD_STARTED)
  248. break;
  249. barrier();
  250. if (i != notified) {
  251. printk(KERN_INFO "kexec: waiting for cpu %d "
  252. "(physical %d) to enter OPAL\n",
  253. i, paca_ptrs[i]->hw_cpu_id);
  254. notified = i;
  255. }
  256. /*
  257. * On crash secondaries might be unreachable or hung,
  258. * so timeout if we've waited too long
  259. * */
  260. mdelay(1);
  261. if (timeout-- == 0) {
  262. printk(KERN_ERR "kexec: timed out waiting for "
  263. "cpu %d (physical %d) to enter OPAL\n",
  264. i, paca_ptrs[i]->hw_cpu_id);
  265. break;
  266. }
  267. }
  268. }
  269. }
  270. static void pnv_kexec_cpu_down(int crash_shutdown, int secondary)
  271. {
  272. u64 reinit_flags;
  273. if (xive_enabled())
  274. xive_teardown_cpu();
  275. else
  276. xics_kexec_teardown_cpu(secondary);
  277. /* On OPAL, we return all CPUs to firmware */
  278. if (!firmware_has_feature(FW_FEATURE_OPAL))
  279. return;
  280. if (secondary) {
  281. /* Return secondary CPUs to firmware on OPAL v3 */
  282. mb();
  283. get_paca()->kexec_state = KEXEC_STATE_REAL_MODE;
  284. mb();
  285. /* Return the CPU to OPAL */
  286. opal_return_cpu();
  287. } else {
  288. /* Primary waits for the secondaries to have reached OPAL */
  289. pnv_kexec_wait_secondaries_down();
  290. /* Switch XIVE back to emulation mode */
  291. if (xive_enabled())
  292. xive_shutdown();
  293. /*
  294. * We might be running as little-endian - now that interrupts
  295. * are disabled, reset the HILE bit to big-endian so we don't
  296. * take interrupts in the wrong endian later
  297. *
  298. * We reinit to enable both radix and hash on P9 to ensure
  299. * the mode used by the next kernel is always supported.
  300. */
  301. reinit_flags = OPAL_REINIT_CPUS_HILE_BE;
  302. if (cpu_has_feature(CPU_FTR_ARCH_300))
  303. reinit_flags |= OPAL_REINIT_CPUS_MMU_RADIX |
  304. OPAL_REINIT_CPUS_MMU_HASH;
  305. opal_reinit_cpus(reinit_flags);
  306. }
  307. }
  308. #endif /* CONFIG_KEXEC_CORE */
  309. #ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
  310. static unsigned long pnv_memory_block_size(void)
  311. {
  312. return 256UL * 1024 * 1024;
  313. }
  314. #endif
  315. static void __init pnv_setup_machdep_opal(void)
  316. {
  317. ppc_md.get_boot_time = opal_get_boot_time;
  318. ppc_md.restart = pnv_restart;
  319. pm_power_off = pnv_power_off;
  320. ppc_md.halt = pnv_halt;
  321. /* ppc_md.system_reset_exception gets filled in by pnv_smp_init() */
  322. ppc_md.machine_check_exception = opal_machine_check;
  323. ppc_md.mce_check_early_recovery = opal_mce_check_early_recovery;
  324. ppc_md.hmi_exception_early = opal_hmi_exception_early;
  325. ppc_md.handle_hmi_exception = opal_handle_hmi_exception;
  326. }
  327. static int __init pnv_probe(void)
  328. {
  329. if (!of_machine_is_compatible("ibm,powernv"))
  330. return 0;
  331. if (firmware_has_feature(FW_FEATURE_OPAL))
  332. pnv_setup_machdep_opal();
  333. pr_debug("PowerNV detected !\n");
  334. pnv_init();
  335. return 1;
  336. }
  337. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  338. void __init pnv_tm_init(void)
  339. {
  340. if (!firmware_has_feature(FW_FEATURE_OPAL) ||
  341. !pvr_version_is(PVR_POWER9) ||
  342. early_cpu_has_feature(CPU_FTR_TM))
  343. return;
  344. if (opal_reinit_cpus(OPAL_REINIT_CPUS_TM_SUSPEND_DISABLED) != OPAL_SUCCESS)
  345. return;
  346. pr_info("Enabling TM (Transactional Memory) with Suspend Disabled\n");
  347. cur_cpu_spec->cpu_features |= CPU_FTR_TM;
  348. /* Make sure "normal" HTM is off (it should be) */
  349. cur_cpu_spec->cpu_user_features2 &= ~PPC_FEATURE2_HTM;
  350. /* Turn on no suspend mode, and HTM no SC */
  351. cur_cpu_spec->cpu_user_features2 |= PPC_FEATURE2_HTM_NO_SUSPEND | \
  352. PPC_FEATURE2_HTM_NOSC;
  353. tm_suspend_disabled = true;
  354. }
  355. #endif /* CONFIG_PPC_TRANSACTIONAL_MEM */
  356. /*
  357. * Returns the cpu frequency for 'cpu' in Hz. This is used by
  358. * /proc/cpuinfo
  359. */
  360. static unsigned long pnv_get_proc_freq(unsigned int cpu)
  361. {
  362. unsigned long ret_freq;
  363. ret_freq = cpufreq_get(cpu) * 1000ul;
  364. /*
  365. * If the backend cpufreq driver does not exist,
  366. * then fallback to old way of reporting the clockrate.
  367. */
  368. if (!ret_freq)
  369. ret_freq = ppc_proc_freq;
  370. return ret_freq;
  371. }
  372. define_machine(powernv) {
  373. .name = "PowerNV",
  374. .probe = pnv_probe,
  375. .setup_arch = pnv_setup_arch,
  376. .init_IRQ = pnv_init_IRQ,
  377. .show_cpuinfo = pnv_show_cpuinfo,
  378. .get_proc_freq = pnv_get_proc_freq,
  379. .progress = pnv_progress,
  380. .machine_shutdown = pnv_shutdown,
  381. .power_save = NULL,
  382. .calibrate_decr = generic_calibrate_decr,
  383. #ifdef CONFIG_KEXEC_CORE
  384. .kexec_cpu_down = pnv_kexec_cpu_down,
  385. #endif
  386. #ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
  387. .memory_block_size = pnv_memory_block_size,
  388. #endif
  389. };