setup.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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/cputhreads.h>
  36. #include <asm/cpuidle.h>
  37. #include <asm/code-patching.h>
  38. #include "powernv.h"
  39. #include "subcore.h"
  40. static void __init pnv_setup_arch(void)
  41. {
  42. set_arch_panic_timeout(10, ARCH_PANIC_TIMEOUT);
  43. /* Initialize SMP */
  44. pnv_smp_init();
  45. /* Setup PCI */
  46. pnv_pci_init();
  47. /* Setup RTC and NVRAM callbacks */
  48. if (firmware_has_feature(FW_FEATURE_OPAL))
  49. opal_nvram_init();
  50. /* Enable NAP mode */
  51. powersave_nap = 1;
  52. /* XXX PMCS */
  53. }
  54. static void __init pnv_init_early(void)
  55. {
  56. /*
  57. * Initialize the LPC bus now so that legacy serial
  58. * ports can be found on it
  59. */
  60. opal_lpc_init();
  61. #ifdef CONFIG_HVC_OPAL
  62. if (firmware_has_feature(FW_FEATURE_OPAL))
  63. hvc_opal_init_early();
  64. else
  65. #endif
  66. add_preferred_console("hvc", 0, NULL);
  67. }
  68. static void __init pnv_init_IRQ(void)
  69. {
  70. xics_init();
  71. WARN_ON(!ppc_md.get_irq);
  72. }
  73. static void pnv_show_cpuinfo(struct seq_file *m)
  74. {
  75. struct device_node *root;
  76. const char *model = "";
  77. root = of_find_node_by_path("/");
  78. if (root)
  79. model = of_get_property(root, "model", NULL);
  80. seq_printf(m, "machine\t\t: PowerNV %s\n", model);
  81. if (firmware_has_feature(FW_FEATURE_OPALv3))
  82. seq_printf(m, "firmware\t: OPAL v3\n");
  83. else if (firmware_has_feature(FW_FEATURE_OPALv2))
  84. seq_printf(m, "firmware\t: OPAL v2\n");
  85. else if (firmware_has_feature(FW_FEATURE_OPAL))
  86. seq_printf(m, "firmware\t: OPAL v1\n");
  87. else
  88. seq_printf(m, "firmware\t: BML\n");
  89. of_node_put(root);
  90. }
  91. static void pnv_prepare_going_down(void)
  92. {
  93. /*
  94. * Disable all notifiers from OPAL, we can't
  95. * service interrupts anymore anyway
  96. */
  97. opal_notifier_disable();
  98. /* Soft disable interrupts */
  99. local_irq_disable();
  100. /*
  101. * Return secondary CPUs to firwmare if a flash update
  102. * is pending otherwise we will get all sort of error
  103. * messages about CPU being stuck etc.. This will also
  104. * have the side effect of hard disabling interrupts so
  105. * past this point, the kernel is effectively dead.
  106. */
  107. opal_flash_term_callback();
  108. }
  109. static void __noreturn pnv_restart(char *cmd)
  110. {
  111. long rc = OPAL_BUSY;
  112. pnv_prepare_going_down();
  113. while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) {
  114. rc = opal_cec_reboot();
  115. if (rc == OPAL_BUSY_EVENT)
  116. opal_poll_events(NULL);
  117. else
  118. mdelay(10);
  119. }
  120. for (;;)
  121. opal_poll_events(NULL);
  122. }
  123. static void __noreturn pnv_power_off(void)
  124. {
  125. long rc = OPAL_BUSY;
  126. pnv_prepare_going_down();
  127. while (rc == OPAL_BUSY || rc == OPAL_BUSY_EVENT) {
  128. rc = opal_cec_power_down(0);
  129. if (rc == OPAL_BUSY_EVENT)
  130. opal_poll_events(NULL);
  131. else
  132. mdelay(10);
  133. }
  134. for (;;)
  135. opal_poll_events(NULL);
  136. }
  137. static void __noreturn pnv_halt(void)
  138. {
  139. pnv_power_off();
  140. }
  141. static void pnv_progress(char *s, unsigned short hex)
  142. {
  143. }
  144. static int pnv_dma_set_mask(struct device *dev, u64 dma_mask)
  145. {
  146. if (dev_is_pci(dev))
  147. return pnv_pci_dma_set_mask(to_pci_dev(dev), dma_mask);
  148. return __dma_set_mask(dev, dma_mask);
  149. }
  150. static u64 pnv_dma_get_required_mask(struct device *dev)
  151. {
  152. if (dev_is_pci(dev))
  153. return pnv_pci_dma_get_required_mask(to_pci_dev(dev));
  154. return __dma_get_required_mask(dev);
  155. }
  156. static void pnv_shutdown(void)
  157. {
  158. /* Let the PCI code clear up IODA tables */
  159. pnv_pci_shutdown();
  160. /*
  161. * Stop OPAL activity: Unregister all OPAL interrupts so they
  162. * don't fire up while we kexec and make sure all potentially
  163. * DMA'ing ops are complete (such as dump retrieval).
  164. */
  165. opal_shutdown();
  166. }
  167. #ifdef CONFIG_KEXEC
  168. static void pnv_kexec_wait_secondaries_down(void)
  169. {
  170. int my_cpu, i, notified = -1;
  171. my_cpu = get_cpu();
  172. for_each_online_cpu(i) {
  173. uint8_t status;
  174. int64_t rc;
  175. if (i == my_cpu)
  176. continue;
  177. for (;;) {
  178. rc = opal_query_cpu_status(get_hard_smp_processor_id(i),
  179. &status);
  180. if (rc != OPAL_SUCCESS || status != OPAL_THREAD_STARTED)
  181. break;
  182. barrier();
  183. if (i != notified) {
  184. printk(KERN_INFO "kexec: waiting for cpu %d "
  185. "(physical %d) to enter OPAL\n",
  186. i, paca[i].hw_cpu_id);
  187. notified = i;
  188. }
  189. }
  190. }
  191. }
  192. static void pnv_kexec_cpu_down(int crash_shutdown, int secondary)
  193. {
  194. xics_kexec_teardown_cpu(secondary);
  195. /* On OPAL v3, we return all CPUs to firmware */
  196. if (!firmware_has_feature(FW_FEATURE_OPALv3))
  197. return;
  198. if (secondary) {
  199. /* Return secondary CPUs to firmware on OPAL v3 */
  200. mb();
  201. get_paca()->kexec_state = KEXEC_STATE_REAL_MODE;
  202. mb();
  203. /* Return the CPU to OPAL */
  204. opal_return_cpu();
  205. } else if (crash_shutdown) {
  206. /*
  207. * On crash, we don't wait for secondaries to go
  208. * down as they might be unreachable or hung, so
  209. * instead we just wait a bit and move on.
  210. */
  211. mdelay(1);
  212. } else {
  213. /* Primary waits for the secondaries to have reached OPAL */
  214. pnv_kexec_wait_secondaries_down();
  215. }
  216. }
  217. #endif /* CONFIG_KEXEC */
  218. #ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
  219. static unsigned long pnv_memory_block_size(void)
  220. {
  221. return 256UL * 1024 * 1024;
  222. }
  223. #endif
  224. static void __init pnv_setup_machdep_opal(void)
  225. {
  226. ppc_md.get_boot_time = opal_get_boot_time;
  227. ppc_md.restart = pnv_restart;
  228. pm_power_off = pnv_power_off;
  229. ppc_md.halt = pnv_halt;
  230. ppc_md.machine_check_exception = opal_machine_check;
  231. ppc_md.mce_check_early_recovery = opal_mce_check_early_recovery;
  232. ppc_md.hmi_exception_early = opal_hmi_exception_early;
  233. ppc_md.handle_hmi_exception = opal_handle_hmi_exception;
  234. }
  235. static u32 supported_cpuidle_states;
  236. int pnv_save_sprs_for_winkle(void)
  237. {
  238. int cpu;
  239. int rc;
  240. /*
  241. * hid0, hid1, hid4, hid5, hmeer and lpcr values are symmetric accross
  242. * all cpus at boot. Get these reg values of current cpu and use the
  243. * same accross all cpus.
  244. */
  245. uint64_t lpcr_val = mfspr(SPRN_LPCR) & ~(u64)LPCR_PECE1;
  246. uint64_t hid0_val = mfspr(SPRN_HID0);
  247. uint64_t hid1_val = mfspr(SPRN_HID1);
  248. uint64_t hid4_val = mfspr(SPRN_HID4);
  249. uint64_t hid5_val = mfspr(SPRN_HID5);
  250. uint64_t hmeer_val = mfspr(SPRN_HMEER);
  251. for_each_possible_cpu(cpu) {
  252. uint64_t pir = get_hard_smp_processor_id(cpu);
  253. uint64_t hsprg0_val = (uint64_t)&paca[cpu];
  254. /*
  255. * HSPRG0 is used to store the cpu's pointer to paca. Hence last
  256. * 3 bits are guaranteed to be 0. Program slw to restore HSPRG0
  257. * with 63rd bit set, so that when a thread wakes up at 0x100 we
  258. * can use this bit to distinguish between fastsleep and
  259. * deep winkle.
  260. */
  261. hsprg0_val |= 1;
  262. rc = opal_slw_set_reg(pir, SPRN_HSPRG0, hsprg0_val);
  263. if (rc != 0)
  264. return rc;
  265. rc = opal_slw_set_reg(pir, SPRN_LPCR, lpcr_val);
  266. if (rc != 0)
  267. return rc;
  268. /* HIDs are per core registers */
  269. if (cpu_thread_in_core(cpu) == 0) {
  270. rc = opal_slw_set_reg(pir, SPRN_HMEER, hmeer_val);
  271. if (rc != 0)
  272. return rc;
  273. rc = opal_slw_set_reg(pir, SPRN_HID0, hid0_val);
  274. if (rc != 0)
  275. return rc;
  276. rc = opal_slw_set_reg(pir, SPRN_HID1, hid1_val);
  277. if (rc != 0)
  278. return rc;
  279. rc = opal_slw_set_reg(pir, SPRN_HID4, hid4_val);
  280. if (rc != 0)
  281. return rc;
  282. rc = opal_slw_set_reg(pir, SPRN_HID5, hid5_val);
  283. if (rc != 0)
  284. return rc;
  285. }
  286. }
  287. return 0;
  288. }
  289. static void pnv_alloc_idle_core_states(void)
  290. {
  291. int i, j;
  292. int nr_cores = cpu_nr_cores();
  293. u32 *core_idle_state;
  294. /*
  295. * core_idle_state - First 8 bits track the idle state of each thread
  296. * of the core. The 8th bit is the lock bit. Initially all thread bits
  297. * are set. They are cleared when the thread enters deep idle state
  298. * like sleep and winkle. Initially the lock bit is cleared.
  299. * The lock bit has 2 purposes
  300. * a. While the first thread is restoring core state, it prevents
  301. * other threads in the core from switching to process context.
  302. * b. While the last thread in the core is saving the core state, it
  303. * prevents a different thread from waking up.
  304. */
  305. for (i = 0; i < nr_cores; i++) {
  306. int first_cpu = i * threads_per_core;
  307. int node = cpu_to_node(first_cpu);
  308. core_idle_state = kmalloc_node(sizeof(u32), GFP_KERNEL, node);
  309. *core_idle_state = PNV_CORE_IDLE_THREAD_BITS;
  310. for (j = 0; j < threads_per_core; j++) {
  311. int cpu = first_cpu + j;
  312. paca[cpu].core_idle_state_ptr = core_idle_state;
  313. paca[cpu].thread_idle_state = PNV_THREAD_RUNNING;
  314. paca[cpu].thread_mask = 1 << j;
  315. }
  316. }
  317. update_subcore_sibling_mask();
  318. if (supported_cpuidle_states & OPAL_PM_WINKLE_ENABLED)
  319. pnv_save_sprs_for_winkle();
  320. }
  321. u32 pnv_get_supported_cpuidle_states(void)
  322. {
  323. return supported_cpuidle_states;
  324. }
  325. EXPORT_SYMBOL_GPL(pnv_get_supported_cpuidle_states);
  326. static int __init pnv_init_idle_states(void)
  327. {
  328. struct device_node *power_mgt;
  329. int dt_idle_states;
  330. u32 *flags;
  331. int i;
  332. supported_cpuidle_states = 0;
  333. if (cpuidle_disable != IDLE_NO_OVERRIDE)
  334. goto out;
  335. if (!firmware_has_feature(FW_FEATURE_OPALv3))
  336. goto out;
  337. power_mgt = of_find_node_by_path("/ibm,opal/power-mgt");
  338. if (!power_mgt) {
  339. pr_warn("opal: PowerMgmt Node not found\n");
  340. goto out;
  341. }
  342. dt_idle_states = of_property_count_u32_elems(power_mgt,
  343. "ibm,cpu-idle-state-flags");
  344. if (dt_idle_states < 0) {
  345. pr_warn("cpuidle-powernv: no idle states found in the DT\n");
  346. goto out;
  347. }
  348. flags = kzalloc(sizeof(*flags) * dt_idle_states, GFP_KERNEL);
  349. if (of_property_read_u32_array(power_mgt,
  350. "ibm,cpu-idle-state-flags", flags, dt_idle_states)) {
  351. pr_warn("cpuidle-powernv: missing ibm,cpu-idle-state-flags in DT\n");
  352. goto out_free;
  353. }
  354. for (i = 0; i < dt_idle_states; i++)
  355. supported_cpuidle_states |= flags[i];
  356. if (!(supported_cpuidle_states & OPAL_PM_SLEEP_ENABLED_ER1)) {
  357. patch_instruction(
  358. (unsigned int *)pnv_fastsleep_workaround_at_entry,
  359. PPC_INST_NOP);
  360. patch_instruction(
  361. (unsigned int *)pnv_fastsleep_workaround_at_exit,
  362. PPC_INST_NOP);
  363. }
  364. pnv_alloc_idle_core_states();
  365. out_free:
  366. kfree(flags);
  367. out:
  368. return 0;
  369. }
  370. subsys_initcall(pnv_init_idle_states);
  371. static int __init pnv_probe(void)
  372. {
  373. unsigned long root = of_get_flat_dt_root();
  374. if (!of_flat_dt_is_compatible(root, "ibm,powernv"))
  375. return 0;
  376. hpte_init_native();
  377. if (firmware_has_feature(FW_FEATURE_OPAL))
  378. pnv_setup_machdep_opal();
  379. pr_debug("PowerNV detected !\n");
  380. return 1;
  381. }
  382. /*
  383. * Returns the cpu frequency for 'cpu' in Hz. This is used by
  384. * /proc/cpuinfo
  385. */
  386. static unsigned long pnv_get_proc_freq(unsigned int cpu)
  387. {
  388. unsigned long ret_freq;
  389. ret_freq = cpufreq_quick_get(cpu) * 1000ul;
  390. /*
  391. * If the backend cpufreq driver does not exist,
  392. * then fallback to old way of reporting the clockrate.
  393. */
  394. if (!ret_freq)
  395. ret_freq = ppc_proc_freq;
  396. return ret_freq;
  397. }
  398. define_machine(powernv) {
  399. .name = "PowerNV",
  400. .probe = pnv_probe,
  401. .init_early = pnv_init_early,
  402. .setup_arch = pnv_setup_arch,
  403. .init_IRQ = pnv_init_IRQ,
  404. .show_cpuinfo = pnv_show_cpuinfo,
  405. .get_proc_freq = pnv_get_proc_freq,
  406. .progress = pnv_progress,
  407. .machine_shutdown = pnv_shutdown,
  408. .power_save = power7_idle,
  409. .calibrate_decr = generic_calibrate_decr,
  410. .dma_set_mask = pnv_dma_set_mask,
  411. .dma_get_required_mask = pnv_dma_get_required_mask,
  412. #ifdef CONFIG_KEXEC
  413. .kexec_cpu_down = pnv_kexec_cpu_down,
  414. #endif
  415. #ifdef CONFIG_MEMORY_HOTPLUG_SPARSE
  416. .memory_block_size = pnv_memory_block_size,
  417. #endif
  418. };