setup.c 12 KB

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