setup.c 11 KB

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