setup.c 11 KB

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