setup.c 12 KB

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