smp.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. /*
  2. * Author: Andy Fleming <afleming@freescale.com>
  3. * Kumar Gala <galak@kernel.crashing.org>
  4. *
  5. * Copyright 2006-2008, 2011-2012 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/of_address.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 <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. static struct ccsr_guts __iomem *guts;
  42. static u64 timebase;
  43. static int tb_req;
  44. static int tb_valid;
  45. static void mpc85xx_timebase_freeze(int freeze)
  46. {
  47. uint32_t mask;
  48. mask = CCSR_GUTS_DEVDISR_TB0 | CCSR_GUTS_DEVDISR_TB1;
  49. if (freeze)
  50. setbits32(&guts->devdisr, mask);
  51. else
  52. clrbits32(&guts->devdisr, mask);
  53. in_be32(&guts->devdisr);
  54. }
  55. static void mpc85xx_give_timebase(void)
  56. {
  57. unsigned long flags;
  58. local_irq_save(flags);
  59. while (!tb_req)
  60. barrier();
  61. tb_req = 0;
  62. mpc85xx_timebase_freeze(1);
  63. #ifdef CONFIG_PPC64
  64. /*
  65. * e5500/e6500 have a workaround for erratum A-006958 in place
  66. * that will reread the timebase until TBL is non-zero.
  67. * That would be a bad thing when the timebase is frozen.
  68. *
  69. * Thus, we read it manually, and instead of checking that
  70. * TBL is non-zero, we ensure that TB does not change. We don't
  71. * do that for the main mftb implementation, because it requires
  72. * a scratch register
  73. */
  74. {
  75. u64 prev;
  76. asm volatile("mfspr %0, %1" : "=r" (timebase) :
  77. "i" (SPRN_TBRL));
  78. do {
  79. prev = timebase;
  80. asm volatile("mfspr %0, %1" : "=r" (timebase) :
  81. "i" (SPRN_TBRL));
  82. } while (prev != timebase);
  83. }
  84. #else
  85. timebase = get_tb();
  86. #endif
  87. mb();
  88. tb_valid = 1;
  89. while (tb_valid)
  90. barrier();
  91. mpc85xx_timebase_freeze(0);
  92. local_irq_restore(flags);
  93. }
  94. static void mpc85xx_take_timebase(void)
  95. {
  96. unsigned long flags;
  97. local_irq_save(flags);
  98. tb_req = 1;
  99. while (!tb_valid)
  100. barrier();
  101. set_tb(timebase >> 32, timebase & 0xffffffff);
  102. isync();
  103. tb_valid = 0;
  104. local_irq_restore(flags);
  105. }
  106. #ifdef CONFIG_HOTPLUG_CPU
  107. static void smp_85xx_mach_cpu_die(void)
  108. {
  109. unsigned int cpu = smp_processor_id();
  110. u32 tmp;
  111. local_irq_disable();
  112. idle_task_exit();
  113. generic_set_cpu_dead(cpu);
  114. mb();
  115. mtspr(SPRN_TCR, 0);
  116. __flush_disable_L1();
  117. tmp = (mfspr(SPRN_HID0) & ~(HID0_DOZE|HID0_SLEEP)) | HID0_NAP;
  118. mtspr(SPRN_HID0, tmp);
  119. isync();
  120. /* Enter NAP mode. */
  121. tmp = mfmsr();
  122. tmp |= MSR_WE;
  123. mb();
  124. mtmsr(tmp);
  125. isync();
  126. while (1)
  127. ;
  128. }
  129. #endif
  130. static inline void flush_spin_table(void *spin_table)
  131. {
  132. flush_dcache_range((ulong)spin_table,
  133. (ulong)spin_table + sizeof(struct epapr_spin_table));
  134. }
  135. static inline u32 read_spin_table_addr_l(void *spin_table)
  136. {
  137. flush_dcache_range((ulong)spin_table,
  138. (ulong)spin_table + sizeof(struct epapr_spin_table));
  139. return in_be32(&((struct epapr_spin_table *)spin_table)->addr_l);
  140. }
  141. #ifdef CONFIG_PPC64
  142. static void wake_hw_thread(void *info)
  143. {
  144. void fsl_secondary_thread_init(void);
  145. unsigned long imsr, inia;
  146. int nr = *(const int *)info;
  147. imsr = MSR_KERNEL;
  148. inia = *(unsigned long *)fsl_secondary_thread_init;
  149. if (cpu_thread_in_core(nr) == 0) {
  150. /* For when we boot on a secondary thread with kdump */
  151. mttmr(TMRN_IMSR0, imsr);
  152. mttmr(TMRN_INIA0, inia);
  153. mtspr(SPRN_TENS, TEN_THREAD(0));
  154. } else {
  155. mttmr(TMRN_IMSR1, imsr);
  156. mttmr(TMRN_INIA1, inia);
  157. mtspr(SPRN_TENS, TEN_THREAD(1));
  158. }
  159. smp_generic_kick_cpu(nr);
  160. }
  161. #endif
  162. static int smp_85xx_kick_cpu(int nr)
  163. {
  164. unsigned long flags;
  165. const u64 *cpu_rel_addr;
  166. __iomem struct epapr_spin_table *spin_table;
  167. struct device_node *np;
  168. int hw_cpu = get_hard_smp_processor_id(nr);
  169. int ioremappable;
  170. int ret = 0;
  171. WARN_ON(nr < 0 || nr >= NR_CPUS);
  172. WARN_ON(hw_cpu < 0 || hw_cpu >= NR_CPUS);
  173. pr_debug("smp_85xx_kick_cpu: kick CPU #%d\n", nr);
  174. #ifdef CONFIG_PPC64
  175. /* Threads don't use the spin table */
  176. if (cpu_thread_in_core(nr) != 0) {
  177. int primary = cpu_first_thread_sibling(nr);
  178. if (WARN_ON_ONCE(!cpu_has_feature(CPU_FTR_SMT)))
  179. return -ENOENT;
  180. if (cpu_thread_in_core(nr) != 1) {
  181. pr_err("%s: cpu %d: invalid hw thread %d\n",
  182. __func__, nr, cpu_thread_in_core(nr));
  183. return -ENOENT;
  184. }
  185. if (!cpu_online(primary)) {
  186. pr_err("%s: cpu %d: primary %d not online\n",
  187. __func__, nr, primary);
  188. return -ENOENT;
  189. }
  190. smp_call_function_single(primary, wake_hw_thread, &nr, 0);
  191. return 0;
  192. } else if (cpu_thread_in_core(boot_cpuid) != 0 &&
  193. cpu_first_thread_sibling(boot_cpuid) == nr) {
  194. if (WARN_ON_ONCE(!cpu_has_feature(CPU_FTR_SMT)))
  195. return -ENOENT;
  196. smp_call_function_single(boot_cpuid, wake_hw_thread, &nr, 0);
  197. }
  198. #endif
  199. np = of_get_cpu_node(nr, NULL);
  200. cpu_rel_addr = of_get_property(np, "cpu-release-addr", NULL);
  201. if (cpu_rel_addr == NULL) {
  202. printk(KERN_ERR "No cpu-release-addr for cpu %d\n", nr);
  203. return -ENOENT;
  204. }
  205. /*
  206. * A secondary core could be in a spinloop in the bootpage
  207. * (0xfffff000), somewhere in highmem, or somewhere in lowmem.
  208. * The bootpage and highmem can be accessed via ioremap(), but
  209. * we need to directly access the spinloop if its in lowmem.
  210. */
  211. ioremappable = *cpu_rel_addr > virt_to_phys(high_memory);
  212. /* Map the spin table */
  213. if (ioremappable)
  214. spin_table = ioremap_prot(*cpu_rel_addr,
  215. sizeof(struct epapr_spin_table), _PAGE_COHERENT);
  216. else
  217. spin_table = phys_to_virt(*cpu_rel_addr);
  218. local_irq_save(flags);
  219. #ifdef CONFIG_PPC32
  220. #ifdef CONFIG_HOTPLUG_CPU
  221. /* Corresponding to generic_set_cpu_dead() */
  222. generic_set_cpu_up(nr);
  223. if (system_state == SYSTEM_RUNNING) {
  224. /*
  225. * To keep it compatible with old boot program which uses
  226. * cache-inhibit spin table, we need to flush the cache
  227. * before accessing spin table to invalidate any staled data.
  228. * We also need to flush the cache after writing to spin
  229. * table to push data out.
  230. */
  231. flush_spin_table(spin_table);
  232. out_be32(&spin_table->addr_l, 0);
  233. flush_spin_table(spin_table);
  234. /*
  235. * We don't set the BPTR register here since it already points
  236. * to the boot page properly.
  237. */
  238. mpic_reset_core(nr);
  239. /*
  240. * wait until core is ready...
  241. * We need to invalidate the stale data, in case the boot
  242. * loader uses a cache-inhibited spin table.
  243. */
  244. if (!spin_event_timeout(
  245. read_spin_table_addr_l(spin_table) == 1,
  246. 10000, 100)) {
  247. pr_err("%s: timeout waiting for core %d to reset\n",
  248. __func__, hw_cpu);
  249. ret = -ENOENT;
  250. goto out;
  251. }
  252. /* clear the acknowledge status */
  253. __secondary_hold_acknowledge = -1;
  254. }
  255. #endif
  256. flush_spin_table(spin_table);
  257. out_be32(&spin_table->pir, hw_cpu);
  258. out_be32(&spin_table->addr_l, __pa(__early_start));
  259. flush_spin_table(spin_table);
  260. /* Wait a bit for the CPU to ack. */
  261. if (!spin_event_timeout(__secondary_hold_acknowledge == hw_cpu,
  262. 10000, 100)) {
  263. pr_err("%s: timeout waiting for core %d to ack\n",
  264. __func__, hw_cpu);
  265. ret = -ENOENT;
  266. goto out;
  267. }
  268. out:
  269. #else
  270. smp_generic_kick_cpu(nr);
  271. flush_spin_table(spin_table);
  272. out_be32(&spin_table->pir, hw_cpu);
  273. out_be64((u64 *)(&spin_table->addr_h),
  274. __pa(ppc_function_entry(generic_secondary_smp_init)));
  275. flush_spin_table(spin_table);
  276. #endif
  277. local_irq_restore(flags);
  278. if (ioremappable)
  279. iounmap(spin_table);
  280. return ret;
  281. }
  282. struct smp_ops_t smp_85xx_ops = {
  283. .kick_cpu = smp_85xx_kick_cpu,
  284. .cpu_bootable = smp_generic_cpu_bootable,
  285. #ifdef CONFIG_HOTPLUG_CPU
  286. .cpu_disable = generic_cpu_disable,
  287. .cpu_die = generic_cpu_die,
  288. #endif
  289. #if defined(CONFIG_KEXEC) && !defined(CONFIG_PPC64)
  290. .give_timebase = smp_generic_give_timebase,
  291. .take_timebase = smp_generic_take_timebase,
  292. #endif
  293. };
  294. #ifdef CONFIG_KEXEC
  295. #ifdef CONFIG_PPC32
  296. atomic_t kexec_down_cpus = ATOMIC_INIT(0);
  297. void mpc85xx_smp_kexec_cpu_down(int crash_shutdown, int secondary)
  298. {
  299. local_irq_disable();
  300. if (secondary) {
  301. __flush_disable_L1();
  302. atomic_inc(&kexec_down_cpus);
  303. /* loop forever */
  304. while (1);
  305. }
  306. }
  307. static void mpc85xx_smp_kexec_down(void *arg)
  308. {
  309. if (ppc_md.kexec_cpu_down)
  310. ppc_md.kexec_cpu_down(0,1);
  311. }
  312. #else
  313. void mpc85xx_smp_kexec_cpu_down(int crash_shutdown, int secondary)
  314. {
  315. int cpu = smp_processor_id();
  316. int sibling = cpu_last_thread_sibling(cpu);
  317. bool notified = false;
  318. int disable_cpu;
  319. int disable_threadbit = 0;
  320. long start = mftb();
  321. long now;
  322. local_irq_disable();
  323. hard_irq_disable();
  324. mpic_teardown_this_cpu(secondary);
  325. if (cpu == crashing_cpu && cpu_thread_in_core(cpu) != 0) {
  326. /*
  327. * We enter the crash kernel on whatever cpu crashed,
  328. * even if it's a secondary thread. If that's the case,
  329. * disable the corresponding primary thread.
  330. */
  331. disable_threadbit = 1;
  332. disable_cpu = cpu_first_thread_sibling(cpu);
  333. } else if (sibling != crashing_cpu &&
  334. cpu_thread_in_core(cpu) == 0 &&
  335. cpu_thread_in_core(sibling) != 0) {
  336. disable_threadbit = 2;
  337. disable_cpu = sibling;
  338. }
  339. if (disable_threadbit) {
  340. while (paca[disable_cpu].kexec_state < KEXEC_STATE_REAL_MODE) {
  341. barrier();
  342. now = mftb();
  343. if (!notified && now - start > 1000000) {
  344. pr_info("%s/%d: waiting for cpu %d to enter KEXEC_STATE_REAL_MODE (%d)\n",
  345. __func__, smp_processor_id(),
  346. disable_cpu,
  347. paca[disable_cpu].kexec_state);
  348. notified = true;
  349. }
  350. }
  351. if (notified) {
  352. pr_info("%s: cpu %d done waiting\n",
  353. __func__, disable_cpu);
  354. }
  355. mtspr(SPRN_TENC, disable_threadbit);
  356. while (mfspr(SPRN_TENSR) & disable_threadbit)
  357. cpu_relax();
  358. }
  359. }
  360. #endif
  361. static void mpc85xx_smp_machine_kexec(struct kimage *image)
  362. {
  363. #ifdef CONFIG_PPC32
  364. int timeout = INT_MAX;
  365. int i, num_cpus = num_present_cpus();
  366. if (image->type == KEXEC_TYPE_DEFAULT)
  367. smp_call_function(mpc85xx_smp_kexec_down, NULL, 0);
  368. while ( (atomic_read(&kexec_down_cpus) != (num_cpus - 1)) &&
  369. ( timeout > 0 ) )
  370. {
  371. timeout--;
  372. }
  373. if ( !timeout )
  374. printk(KERN_ERR "Unable to bring down secondary cpu(s)");
  375. for_each_online_cpu(i)
  376. {
  377. if ( i == smp_processor_id() ) continue;
  378. mpic_reset_core(i);
  379. }
  380. #endif
  381. default_machine_kexec(image);
  382. }
  383. #endif /* CONFIG_KEXEC */
  384. static void smp_85xx_basic_setup(int cpu_nr)
  385. {
  386. if (cpu_has_feature(CPU_FTR_DBELL))
  387. doorbell_setup_this_cpu();
  388. }
  389. static void smp_85xx_setup_cpu(int cpu_nr)
  390. {
  391. mpic_setup_this_cpu();
  392. smp_85xx_basic_setup(cpu_nr);
  393. }
  394. static const struct of_device_id mpc85xx_smp_guts_ids[] = {
  395. { .compatible = "fsl,mpc8572-guts", },
  396. { .compatible = "fsl,p1020-guts", },
  397. { .compatible = "fsl,p1021-guts", },
  398. { .compatible = "fsl,p1022-guts", },
  399. { .compatible = "fsl,p1023-guts", },
  400. { .compatible = "fsl,p2020-guts", },
  401. {},
  402. };
  403. void __init mpc85xx_smp_init(void)
  404. {
  405. struct device_node *np;
  406. np = of_find_node_by_type(NULL, "open-pic");
  407. if (np) {
  408. smp_85xx_ops.probe = smp_mpic_probe;
  409. smp_85xx_ops.setup_cpu = smp_85xx_setup_cpu;
  410. smp_85xx_ops.message_pass = smp_mpic_message_pass;
  411. } else
  412. smp_85xx_ops.setup_cpu = smp_85xx_basic_setup;
  413. if (cpu_has_feature(CPU_FTR_DBELL)) {
  414. /*
  415. * If left NULL, .message_pass defaults to
  416. * smp_muxed_ipi_message_pass
  417. */
  418. smp_85xx_ops.message_pass = NULL;
  419. smp_85xx_ops.cause_ipi = doorbell_cause_ipi;
  420. smp_85xx_ops.probe = NULL;
  421. }
  422. np = of_find_matching_node(NULL, mpc85xx_smp_guts_ids);
  423. if (np) {
  424. guts = of_iomap(np, 0);
  425. of_node_put(np);
  426. if (!guts) {
  427. pr_err("%s: Could not map guts node address\n",
  428. __func__);
  429. return;
  430. }
  431. smp_85xx_ops.give_timebase = mpc85xx_give_timebase;
  432. smp_85xx_ops.take_timebase = mpc85xx_take_timebase;
  433. #ifdef CONFIG_HOTPLUG_CPU
  434. ppc_md.cpu_die = smp_85xx_mach_cpu_die;
  435. #endif
  436. }
  437. smp_ops = &smp_85xx_ops;
  438. #ifdef CONFIG_KEXEC
  439. ppc_md.kexec_cpu_down = mpc85xx_smp_kexec_cpu_down;
  440. ppc_md.machine_kexec = mpc85xx_smp_machine_kexec;
  441. #endif
  442. }