smp.c 11 KB

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