smp.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. /*
  2. * Author: Andy Fleming <afleming@freescale.com>
  3. * Kumar Gala <galak@kernel.crashing.org>
  4. *
  5. * Copyright 2006-2008, 2011-2012, 2015 Freescale Semiconductor Inc.
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation; either version 2 of the License, or (at your
  10. * option) any later version.
  11. */
  12. #include <linux/stddef.h>
  13. #include <linux/kernel.h>
  14. #include <linux/sched/hotplug.h>
  15. #include <linux/init.h>
  16. #include <linux/delay.h>
  17. #include <linux/of.h>
  18. #include <linux/kexec.h>
  19. #include <linux/highmem.h>
  20. #include <linux/cpu.h>
  21. #include <linux/fsl/guts.h>
  22. #include <asm/machdep.h>
  23. #include <asm/pgtable.h>
  24. #include <asm/page.h>
  25. #include <asm/mpic.h>
  26. #include <asm/cacheflush.h>
  27. #include <asm/dbell.h>
  28. #include <asm/code-patching.h>
  29. #include <asm/cputhreads.h>
  30. #include <asm/fsl_pm.h>
  31. #include <sysdev/fsl_soc.h>
  32. #include <sysdev/mpic.h>
  33. #include "smp.h"
  34. struct epapr_spin_table {
  35. u32 addr_h;
  36. u32 addr_l;
  37. u32 r3_h;
  38. u32 r3_l;
  39. u32 reserved;
  40. u32 pir;
  41. };
  42. #ifdef CONFIG_HOTPLUG_CPU
  43. static u64 timebase;
  44. static int tb_req;
  45. static int tb_valid;
  46. static void mpc85xx_give_timebase(void)
  47. {
  48. unsigned long flags;
  49. local_irq_save(flags);
  50. hard_irq_disable();
  51. while (!tb_req)
  52. barrier();
  53. tb_req = 0;
  54. qoriq_pm_ops->freeze_time_base(true);
  55. #ifdef CONFIG_PPC64
  56. /*
  57. * e5500/e6500 have a workaround for erratum A-006958 in place
  58. * that will reread the timebase until TBL is non-zero.
  59. * That would be a bad thing when the timebase is frozen.
  60. *
  61. * Thus, we read it manually, and instead of checking that
  62. * TBL is non-zero, we ensure that TB does not change. We don't
  63. * do that for the main mftb implementation, because it requires
  64. * a scratch register
  65. */
  66. {
  67. u64 prev;
  68. asm volatile("mfspr %0, %1" : "=r" (timebase) :
  69. "i" (SPRN_TBRL));
  70. do {
  71. prev = timebase;
  72. asm volatile("mfspr %0, %1" : "=r" (timebase) :
  73. "i" (SPRN_TBRL));
  74. } while (prev != timebase);
  75. }
  76. #else
  77. timebase = get_tb();
  78. #endif
  79. mb();
  80. tb_valid = 1;
  81. while (tb_valid)
  82. barrier();
  83. qoriq_pm_ops->freeze_time_base(false);
  84. local_irq_restore(flags);
  85. }
  86. static void mpc85xx_take_timebase(void)
  87. {
  88. unsigned long flags;
  89. local_irq_save(flags);
  90. hard_irq_disable();
  91. tb_req = 1;
  92. while (!tb_valid)
  93. barrier();
  94. set_tb(timebase >> 32, timebase & 0xffffffff);
  95. isync();
  96. tb_valid = 0;
  97. local_irq_restore(flags);
  98. }
  99. static void smp_85xx_mach_cpu_die(void)
  100. {
  101. unsigned int cpu = smp_processor_id();
  102. local_irq_disable();
  103. hard_irq_disable();
  104. /* mask all irqs to prevent cpu wakeup */
  105. qoriq_pm_ops->irq_mask(cpu);
  106. idle_task_exit();
  107. mtspr(SPRN_TCR, 0);
  108. mtspr(SPRN_TSR, mfspr(SPRN_TSR));
  109. generic_set_cpu_dead(cpu);
  110. cur_cpu_spec->cpu_down_flush();
  111. qoriq_pm_ops->cpu_die(cpu);
  112. while (1)
  113. ;
  114. }
  115. static void qoriq_cpu_kill(unsigned int cpu)
  116. {
  117. int i;
  118. for (i = 0; i < 500; i++) {
  119. if (is_cpu_dead(cpu)) {
  120. #ifdef CONFIG_PPC64
  121. paca_ptrs[cpu]->cpu_start = 0;
  122. #endif
  123. return;
  124. }
  125. msleep(20);
  126. }
  127. pr_err("CPU%d didn't die...\n", cpu);
  128. }
  129. #endif
  130. /*
  131. * To keep it compatible with old boot program which uses
  132. * cache-inhibit spin table, we need to flush the cache
  133. * before accessing spin table to invalidate any staled data.
  134. * We also need to flush the cache after writing to spin
  135. * table to push data out.
  136. */
  137. static inline void flush_spin_table(void *spin_table)
  138. {
  139. flush_dcache_range((ulong)spin_table,
  140. (ulong)spin_table + sizeof(struct epapr_spin_table));
  141. }
  142. static inline u32 read_spin_table_addr_l(void *spin_table)
  143. {
  144. flush_dcache_range((ulong)spin_table,
  145. (ulong)spin_table + sizeof(struct epapr_spin_table));
  146. return in_be32(&((struct epapr_spin_table *)spin_table)->addr_l);
  147. }
  148. #ifdef CONFIG_PPC64
  149. static void wake_hw_thread(void *info)
  150. {
  151. void fsl_secondary_thread_init(void);
  152. unsigned long inia;
  153. int cpu = *(const int *)info;
  154. inia = *(unsigned long *)fsl_secondary_thread_init;
  155. book3e_start_thread(cpu_thread_in_core(cpu), inia);
  156. }
  157. #endif
  158. static int smp_85xx_start_cpu(int cpu)
  159. {
  160. int ret = 0;
  161. struct device_node *np;
  162. const u64 *cpu_rel_addr;
  163. unsigned long flags;
  164. int ioremappable;
  165. int hw_cpu = get_hard_smp_processor_id(cpu);
  166. struct epapr_spin_table __iomem *spin_table;
  167. np = of_get_cpu_node(cpu, NULL);
  168. cpu_rel_addr = of_get_property(np, "cpu-release-addr", NULL);
  169. if (!cpu_rel_addr) {
  170. pr_err("No cpu-release-addr for cpu %d\n", cpu);
  171. return -ENOENT;
  172. }
  173. /*
  174. * A secondary core could be in a spinloop in the bootpage
  175. * (0xfffff000), somewhere in highmem, or somewhere in lowmem.
  176. * The bootpage and highmem can be accessed via ioremap(), but
  177. * we need to directly access the spinloop if its in lowmem.
  178. */
  179. ioremappable = *cpu_rel_addr > virt_to_phys(high_memory);
  180. /* Map the spin table */
  181. if (ioremappable)
  182. spin_table = ioremap_coherent(*cpu_rel_addr,
  183. sizeof(struct epapr_spin_table));
  184. else
  185. spin_table = phys_to_virt(*cpu_rel_addr);
  186. local_irq_save(flags);
  187. hard_irq_disable();
  188. if (qoriq_pm_ops)
  189. qoriq_pm_ops->cpu_up_prepare(cpu);
  190. /* if cpu is not spinning, reset it */
  191. if (read_spin_table_addr_l(spin_table) != 1) {
  192. /*
  193. * We don't set the BPTR register here since it already points
  194. * to the boot page properly.
  195. */
  196. mpic_reset_core(cpu);
  197. /*
  198. * wait until core is ready...
  199. * We need to invalidate the stale data, in case the boot
  200. * loader uses a cache-inhibited spin table.
  201. */
  202. if (!spin_event_timeout(
  203. read_spin_table_addr_l(spin_table) == 1,
  204. 10000, 100)) {
  205. pr_err("timeout waiting for cpu %d to reset\n",
  206. hw_cpu);
  207. ret = -EAGAIN;
  208. goto err;
  209. }
  210. }
  211. flush_spin_table(spin_table);
  212. out_be32(&spin_table->pir, hw_cpu);
  213. #ifdef CONFIG_PPC64
  214. out_be64((u64 *)(&spin_table->addr_h),
  215. __pa(ppc_function_entry(generic_secondary_smp_init)));
  216. #else
  217. out_be32(&spin_table->addr_l, __pa(__early_start));
  218. #endif
  219. flush_spin_table(spin_table);
  220. err:
  221. local_irq_restore(flags);
  222. if (ioremappable)
  223. iounmap(spin_table);
  224. return ret;
  225. }
  226. static int smp_85xx_kick_cpu(int nr)
  227. {
  228. int ret = 0;
  229. #ifdef CONFIG_PPC64
  230. int primary = nr;
  231. #endif
  232. WARN_ON(nr < 0 || nr >= num_possible_cpus());
  233. pr_debug("kick CPU #%d\n", nr);
  234. #ifdef CONFIG_PPC64
  235. if (threads_per_core == 2) {
  236. if (WARN_ON_ONCE(!cpu_has_feature(CPU_FTR_SMT)))
  237. return -ENOENT;
  238. booting_thread_hwid = cpu_thread_in_core(nr);
  239. primary = cpu_first_thread_sibling(nr);
  240. if (qoriq_pm_ops)
  241. qoriq_pm_ops->cpu_up_prepare(nr);
  242. /*
  243. * If either thread in the core is online, use it to start
  244. * the other.
  245. */
  246. if (cpu_online(primary)) {
  247. smp_call_function_single(primary,
  248. wake_hw_thread, &nr, 1);
  249. goto done;
  250. } else if (cpu_online(primary + 1)) {
  251. smp_call_function_single(primary + 1,
  252. wake_hw_thread, &nr, 1);
  253. goto done;
  254. }
  255. /*
  256. * If getting here, it means both threads in the core are
  257. * offline. So start the primary thread, then it will start
  258. * the thread specified in booting_thread_hwid, the one
  259. * corresponding to nr.
  260. */
  261. } else if (threads_per_core == 1) {
  262. /*
  263. * If one core has only one thread, set booting_thread_hwid to
  264. * an invalid value.
  265. */
  266. booting_thread_hwid = INVALID_THREAD_HWID;
  267. } else if (threads_per_core > 2) {
  268. pr_err("Do not support more than 2 threads per CPU.");
  269. return -EINVAL;
  270. }
  271. ret = smp_85xx_start_cpu(primary);
  272. if (ret)
  273. return ret;
  274. done:
  275. paca_ptrs[nr]->cpu_start = 1;
  276. generic_set_cpu_up(nr);
  277. return ret;
  278. #else
  279. ret = smp_85xx_start_cpu(nr);
  280. if (ret)
  281. return ret;
  282. generic_set_cpu_up(nr);
  283. return ret;
  284. #endif
  285. }
  286. struct smp_ops_t smp_85xx_ops = {
  287. .cause_nmi_ipi = NULL,
  288. .kick_cpu = smp_85xx_kick_cpu,
  289. .cpu_bootable = smp_generic_cpu_bootable,
  290. #ifdef CONFIG_HOTPLUG_CPU
  291. .cpu_disable = generic_cpu_disable,
  292. .cpu_die = generic_cpu_die,
  293. #endif
  294. #if defined(CONFIG_KEXEC_CORE) && !defined(CONFIG_PPC64)
  295. .give_timebase = smp_generic_give_timebase,
  296. .take_timebase = smp_generic_take_timebase,
  297. #endif
  298. };
  299. #ifdef CONFIG_KEXEC_CORE
  300. #ifdef CONFIG_PPC32
  301. atomic_t kexec_down_cpus = ATOMIC_INIT(0);
  302. void mpc85xx_smp_kexec_cpu_down(int crash_shutdown, int secondary)
  303. {
  304. local_irq_disable();
  305. if (secondary) {
  306. cur_cpu_spec->cpu_down_flush();
  307. atomic_inc(&kexec_down_cpus);
  308. /* loop forever */
  309. while (1);
  310. }
  311. }
  312. static void mpc85xx_smp_kexec_down(void *arg)
  313. {
  314. if (ppc_md.kexec_cpu_down)
  315. ppc_md.kexec_cpu_down(0,1);
  316. }
  317. #else
  318. void mpc85xx_smp_kexec_cpu_down(int crash_shutdown, int secondary)
  319. {
  320. int cpu = smp_processor_id();
  321. int sibling = cpu_last_thread_sibling(cpu);
  322. bool notified = false;
  323. int disable_cpu;
  324. int disable_threadbit = 0;
  325. long start = mftb();
  326. long now;
  327. local_irq_disable();
  328. hard_irq_disable();
  329. mpic_teardown_this_cpu(secondary);
  330. if (cpu == crashing_cpu && cpu_thread_in_core(cpu) != 0) {
  331. /*
  332. * We enter the crash kernel on whatever cpu crashed,
  333. * even if it's a secondary thread. If that's the case,
  334. * disable the corresponding primary thread.
  335. */
  336. disable_threadbit = 1;
  337. disable_cpu = cpu_first_thread_sibling(cpu);
  338. } else if (sibling != crashing_cpu &&
  339. cpu_thread_in_core(cpu) == 0 &&
  340. cpu_thread_in_core(sibling) != 0) {
  341. disable_threadbit = 2;
  342. disable_cpu = sibling;
  343. }
  344. if (disable_threadbit) {
  345. while (paca_ptrs[disable_cpu]->kexec_state < KEXEC_STATE_REAL_MODE) {
  346. barrier();
  347. now = mftb();
  348. if (!notified && now - start > 1000000) {
  349. pr_info("%s/%d: waiting for cpu %d to enter KEXEC_STATE_REAL_MODE (%d)\n",
  350. __func__, smp_processor_id(),
  351. disable_cpu,
  352. paca_ptrs[disable_cpu]->kexec_state);
  353. notified = true;
  354. }
  355. }
  356. if (notified) {
  357. pr_info("%s: cpu %d done waiting\n",
  358. __func__, disable_cpu);
  359. }
  360. mtspr(SPRN_TENC, disable_threadbit);
  361. while (mfspr(SPRN_TENSR) & disable_threadbit)
  362. cpu_relax();
  363. }
  364. }
  365. #endif
  366. static void mpc85xx_smp_machine_kexec(struct kimage *image)
  367. {
  368. #ifdef CONFIG_PPC32
  369. int timeout = INT_MAX;
  370. int i, num_cpus = num_present_cpus();
  371. if (image->type == KEXEC_TYPE_DEFAULT)
  372. smp_call_function(mpc85xx_smp_kexec_down, NULL, 0);
  373. while ( (atomic_read(&kexec_down_cpus) != (num_cpus - 1)) &&
  374. ( timeout > 0 ) )
  375. {
  376. timeout--;
  377. }
  378. if ( !timeout )
  379. printk(KERN_ERR "Unable to bring down secondary cpu(s)");
  380. for_each_online_cpu(i)
  381. {
  382. if ( i == smp_processor_id() ) continue;
  383. mpic_reset_core(i);
  384. }
  385. #endif
  386. default_machine_kexec(image);
  387. }
  388. #endif /* CONFIG_KEXEC_CORE */
  389. static void smp_85xx_setup_cpu(int cpu_nr)
  390. {
  391. mpic_setup_this_cpu();
  392. }
  393. void __init mpc85xx_smp_init(void)
  394. {
  395. struct device_node *np;
  396. np = of_find_node_by_type(NULL, "open-pic");
  397. if (np) {
  398. smp_85xx_ops.probe = smp_mpic_probe;
  399. smp_85xx_ops.setup_cpu = smp_85xx_setup_cpu;
  400. smp_85xx_ops.message_pass = smp_mpic_message_pass;
  401. } else
  402. smp_85xx_ops.setup_cpu = NULL;
  403. if (cpu_has_feature(CPU_FTR_DBELL)) {
  404. /*
  405. * If left NULL, .message_pass defaults to
  406. * smp_muxed_ipi_message_pass
  407. */
  408. smp_85xx_ops.message_pass = NULL;
  409. smp_85xx_ops.cause_ipi = doorbell_global_ipi;
  410. smp_85xx_ops.probe = NULL;
  411. }
  412. #ifdef CONFIG_HOTPLUG_CPU
  413. #ifdef CONFIG_FSL_CORENET_RCPM
  414. fsl_rcpm_init();
  415. #endif
  416. #ifdef CONFIG_FSL_PMC
  417. mpc85xx_setup_pmc();
  418. #endif
  419. if (qoriq_pm_ops) {
  420. smp_85xx_ops.give_timebase = mpc85xx_give_timebase;
  421. smp_85xx_ops.take_timebase = mpc85xx_take_timebase;
  422. ppc_md.cpu_die = smp_85xx_mach_cpu_die;
  423. smp_85xx_ops.cpu_die = qoriq_cpu_kill;
  424. }
  425. #endif
  426. smp_ops = &smp_85xx_ops;
  427. #ifdef CONFIG_KEXEC_CORE
  428. ppc_md.kexec_cpu_down = mpc85xx_smp_kexec_cpu_down;
  429. ppc_md.machine_kexec = mpc85xx_smp_machine_kexec;
  430. #endif
  431. }