smp.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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 <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/fsl_guts.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 imsr1, inia1;
  146. int nr = *(const int *)info;
  147. imsr1 = MSR_KERNEL;
  148. inia1 = *(unsigned long *)fsl_secondary_thread_init;
  149. mttmr(TMRN_IMSR1, imsr1);
  150. mttmr(TMRN_INIA1, inia1);
  151. mtspr(SPRN_TENS, TEN_THREAD(1));
  152. smp_generic_kick_cpu(nr);
  153. }
  154. #endif
  155. static int smp_85xx_kick_cpu(int nr)
  156. {
  157. unsigned long flags;
  158. const u64 *cpu_rel_addr;
  159. __iomem struct epapr_spin_table *spin_table;
  160. struct device_node *np;
  161. int hw_cpu = get_hard_smp_processor_id(nr);
  162. int ioremappable;
  163. int ret = 0;
  164. WARN_ON(nr < 0 || nr >= NR_CPUS);
  165. WARN_ON(hw_cpu < 0 || hw_cpu >= NR_CPUS);
  166. pr_debug("smp_85xx_kick_cpu: kick CPU #%d\n", nr);
  167. #ifdef CONFIG_PPC64
  168. /* Threads don't use the spin table */
  169. if (cpu_thread_in_core(nr) != 0) {
  170. int primary = cpu_first_thread_sibling(nr);
  171. if (WARN_ON_ONCE(!cpu_has_feature(CPU_FTR_SMT)))
  172. return -ENOENT;
  173. if (cpu_thread_in_core(nr) != 1) {
  174. pr_err("%s: cpu %d: invalid hw thread %d\n",
  175. __func__, nr, cpu_thread_in_core(nr));
  176. return -ENOENT;
  177. }
  178. if (!cpu_online(primary)) {
  179. pr_err("%s: cpu %d: primary %d not online\n",
  180. __func__, nr, primary);
  181. return -ENOENT;
  182. }
  183. smp_call_function_single(primary, wake_hw_thread, &nr, 0);
  184. return 0;
  185. }
  186. #endif
  187. np = of_get_cpu_node(nr, NULL);
  188. cpu_rel_addr = of_get_property(np, "cpu-release-addr", NULL);
  189. if (cpu_rel_addr == NULL) {
  190. printk(KERN_ERR "No cpu-release-addr for cpu %d\n", nr);
  191. return -ENOENT;
  192. }
  193. /*
  194. * A secondary core could be in a spinloop in the bootpage
  195. * (0xfffff000), somewhere in highmem, or somewhere in lowmem.
  196. * The bootpage and highmem can be accessed via ioremap(), but
  197. * we need to directly access the spinloop if its in lowmem.
  198. */
  199. ioremappable = *cpu_rel_addr > virt_to_phys(high_memory);
  200. /* Map the spin table */
  201. if (ioremappable)
  202. spin_table = ioremap_prot(*cpu_rel_addr,
  203. sizeof(struct epapr_spin_table), _PAGE_COHERENT);
  204. else
  205. spin_table = phys_to_virt(*cpu_rel_addr);
  206. local_irq_save(flags);
  207. #ifdef CONFIG_PPC32
  208. #ifdef CONFIG_HOTPLUG_CPU
  209. /* Corresponding to generic_set_cpu_dead() */
  210. generic_set_cpu_up(nr);
  211. if (system_state == SYSTEM_RUNNING) {
  212. /*
  213. * To keep it compatible with old boot program which uses
  214. * cache-inhibit spin table, we need to flush the cache
  215. * before accessing spin table to invalidate any staled data.
  216. * We also need to flush the cache after writing to spin
  217. * table to push data out.
  218. */
  219. flush_spin_table(spin_table);
  220. out_be32(&spin_table->addr_l, 0);
  221. flush_spin_table(spin_table);
  222. /*
  223. * We don't set the BPTR register here since it already points
  224. * to the boot page properly.
  225. */
  226. mpic_reset_core(nr);
  227. /*
  228. * wait until core is ready...
  229. * We need to invalidate the stale data, in case the boot
  230. * loader uses a cache-inhibited spin table.
  231. */
  232. if (!spin_event_timeout(
  233. read_spin_table_addr_l(spin_table) == 1,
  234. 10000, 100)) {
  235. pr_err("%s: timeout waiting for core %d to reset\n",
  236. __func__, hw_cpu);
  237. ret = -ENOENT;
  238. goto out;
  239. }
  240. /* clear the acknowledge status */
  241. __secondary_hold_acknowledge = -1;
  242. }
  243. #endif
  244. flush_spin_table(spin_table);
  245. out_be32(&spin_table->pir, hw_cpu);
  246. out_be32(&spin_table->addr_l, __pa(__early_start));
  247. flush_spin_table(spin_table);
  248. /* Wait a bit for the CPU to ack. */
  249. if (!spin_event_timeout(__secondary_hold_acknowledge == hw_cpu,
  250. 10000, 100)) {
  251. pr_err("%s: timeout waiting for core %d to ack\n",
  252. __func__, hw_cpu);
  253. ret = -ENOENT;
  254. goto out;
  255. }
  256. out:
  257. #else
  258. smp_generic_kick_cpu(nr);
  259. flush_spin_table(spin_table);
  260. out_be32(&spin_table->pir, hw_cpu);
  261. out_be64((u64 *)(&spin_table->addr_h),
  262. __pa(ppc_function_entry(generic_secondary_smp_init)));
  263. flush_spin_table(spin_table);
  264. #endif
  265. local_irq_restore(flags);
  266. if (ioremappable)
  267. iounmap(spin_table);
  268. return ret;
  269. }
  270. struct smp_ops_t smp_85xx_ops = {
  271. .kick_cpu = smp_85xx_kick_cpu,
  272. .cpu_bootable = smp_generic_cpu_bootable,
  273. #ifdef CONFIG_HOTPLUG_CPU
  274. .cpu_disable = generic_cpu_disable,
  275. .cpu_die = generic_cpu_die,
  276. #endif
  277. #ifdef CONFIG_KEXEC
  278. .give_timebase = smp_generic_give_timebase,
  279. .take_timebase = smp_generic_take_timebase,
  280. #endif
  281. };
  282. #ifdef CONFIG_KEXEC
  283. atomic_t kexec_down_cpus = ATOMIC_INIT(0);
  284. void mpc85xx_smp_kexec_cpu_down(int crash_shutdown, int secondary)
  285. {
  286. local_irq_disable();
  287. if (secondary) {
  288. __flush_disable_L1();
  289. atomic_inc(&kexec_down_cpus);
  290. /* loop forever */
  291. while (1);
  292. }
  293. }
  294. static void mpc85xx_smp_kexec_down(void *arg)
  295. {
  296. if (ppc_md.kexec_cpu_down)
  297. ppc_md.kexec_cpu_down(0,1);
  298. }
  299. static void mpc85xx_smp_machine_kexec(struct kimage *image)
  300. {
  301. int timeout = INT_MAX;
  302. int i, num_cpus = num_present_cpus();
  303. if (image->type == KEXEC_TYPE_DEFAULT)
  304. smp_call_function(mpc85xx_smp_kexec_down, NULL, 0);
  305. while ( (atomic_read(&kexec_down_cpus) != (num_cpus - 1)) &&
  306. ( timeout > 0 ) )
  307. {
  308. timeout--;
  309. }
  310. if ( !timeout )
  311. printk(KERN_ERR "Unable to bring down secondary cpu(s)");
  312. for_each_online_cpu(i)
  313. {
  314. if ( i == smp_processor_id() ) continue;
  315. mpic_reset_core(i);
  316. }
  317. default_machine_kexec(image);
  318. }
  319. #endif /* CONFIG_KEXEC */
  320. static void smp_85xx_basic_setup(int cpu_nr)
  321. {
  322. if (cpu_has_feature(CPU_FTR_DBELL))
  323. doorbell_setup_this_cpu();
  324. }
  325. static void smp_85xx_setup_cpu(int cpu_nr)
  326. {
  327. mpic_setup_this_cpu();
  328. smp_85xx_basic_setup(cpu_nr);
  329. }
  330. static const struct of_device_id mpc85xx_smp_guts_ids[] = {
  331. { .compatible = "fsl,mpc8572-guts", },
  332. { .compatible = "fsl,p1020-guts", },
  333. { .compatible = "fsl,p1021-guts", },
  334. { .compatible = "fsl,p1022-guts", },
  335. { .compatible = "fsl,p1023-guts", },
  336. { .compatible = "fsl,p2020-guts", },
  337. {},
  338. };
  339. void __init mpc85xx_smp_init(void)
  340. {
  341. struct device_node *np;
  342. np = of_find_node_by_type(NULL, "open-pic");
  343. if (np) {
  344. smp_85xx_ops.probe = smp_mpic_probe;
  345. smp_85xx_ops.setup_cpu = smp_85xx_setup_cpu;
  346. smp_85xx_ops.message_pass = smp_mpic_message_pass;
  347. } else
  348. smp_85xx_ops.setup_cpu = smp_85xx_basic_setup;
  349. if (cpu_has_feature(CPU_FTR_DBELL)) {
  350. /*
  351. * If left NULL, .message_pass defaults to
  352. * smp_muxed_ipi_message_pass
  353. */
  354. smp_85xx_ops.message_pass = NULL;
  355. smp_85xx_ops.cause_ipi = doorbell_cause_ipi;
  356. smp_85xx_ops.probe = NULL;
  357. }
  358. np = of_find_matching_node(NULL, mpc85xx_smp_guts_ids);
  359. if (np) {
  360. guts = of_iomap(np, 0);
  361. of_node_put(np);
  362. if (!guts) {
  363. pr_err("%s: Could not map guts node address\n",
  364. __func__);
  365. return;
  366. }
  367. smp_85xx_ops.give_timebase = mpc85xx_give_timebase;
  368. smp_85xx_ops.take_timebase = mpc85xx_take_timebase;
  369. #ifdef CONFIG_HOTPLUG_CPU
  370. ppc_md.cpu_die = smp_85xx_mach_cpu_die;
  371. #endif
  372. }
  373. smp_ops = &smp_85xx_ops;
  374. #ifdef CONFIG_KEXEC
  375. ppc_md.kexec_cpu_down = mpc85xx_smp_kexec_cpu_down;
  376. ppc_md.machine_kexec = mpc85xx_smp_machine_kexec;
  377. #endif
  378. }