smp.c 11 KB

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