smp.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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/kexec.h>
  18. #include <linux/highmem.h>
  19. #include <linux/cpu.h>
  20. #include <asm/machdep.h>
  21. #include <asm/pgtable.h>
  22. #include <asm/page.h>
  23. #include <asm/mpic.h>
  24. #include <asm/cacheflush.h>
  25. #include <asm/dbell.h>
  26. #include <asm/fsl_guts.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 struct ccsr_guts __iomem *guts;
  39. static u64 timebase;
  40. static int tb_req;
  41. static int tb_valid;
  42. static void mpc85xx_timebase_freeze(int freeze)
  43. {
  44. uint32_t mask;
  45. mask = CCSR_GUTS_DEVDISR_TB0 | CCSR_GUTS_DEVDISR_TB1;
  46. if (freeze)
  47. setbits32(&guts->devdisr, mask);
  48. else
  49. clrbits32(&guts->devdisr, mask);
  50. in_be32(&guts->devdisr);
  51. }
  52. static void mpc85xx_give_timebase(void)
  53. {
  54. unsigned long flags;
  55. local_irq_save(flags);
  56. while (!tb_req)
  57. barrier();
  58. tb_req = 0;
  59. mpc85xx_timebase_freeze(1);
  60. #ifdef CONFIG_PPC64
  61. /*
  62. * e5500/e6500 have a workaround for erratum A-006958 in place
  63. * that will reread the timebase until TBL is non-zero.
  64. * That would be a bad thing when the timebase is frozen.
  65. *
  66. * Thus, we read it manually, and instead of checking that
  67. * TBL is non-zero, we ensure that TB does not change. We don't
  68. * do that for the main mftb implementation, because it requires
  69. * a scratch register
  70. */
  71. {
  72. u64 prev;
  73. asm volatile("mfspr %0, %1" : "=r" (timebase) :
  74. "i" (SPRN_TBRL));
  75. do {
  76. prev = timebase;
  77. asm volatile("mfspr %0, %1" : "=r" (timebase) :
  78. "i" (SPRN_TBRL));
  79. } while (prev != timebase);
  80. }
  81. #else
  82. timebase = get_tb();
  83. #endif
  84. mb();
  85. tb_valid = 1;
  86. while (tb_valid)
  87. barrier();
  88. mpc85xx_timebase_freeze(0);
  89. local_irq_restore(flags);
  90. }
  91. static void mpc85xx_take_timebase(void)
  92. {
  93. unsigned long flags;
  94. local_irq_save(flags);
  95. tb_req = 1;
  96. while (!tb_valid)
  97. barrier();
  98. set_tb(timebase >> 32, timebase & 0xffffffff);
  99. isync();
  100. tb_valid = 0;
  101. local_irq_restore(flags);
  102. }
  103. #ifdef CONFIG_HOTPLUG_CPU
  104. static void smp_85xx_mach_cpu_die(void)
  105. {
  106. unsigned int cpu = smp_processor_id();
  107. u32 tmp;
  108. local_irq_disable();
  109. idle_task_exit();
  110. generic_set_cpu_dead(cpu);
  111. mb();
  112. mtspr(SPRN_TCR, 0);
  113. __flush_disable_L1();
  114. tmp = (mfspr(SPRN_HID0) & ~(HID0_DOZE|HID0_SLEEP)) | HID0_NAP;
  115. mtspr(SPRN_HID0, tmp);
  116. isync();
  117. /* Enter NAP mode. */
  118. tmp = mfmsr();
  119. tmp |= MSR_WE;
  120. mb();
  121. mtmsr(tmp);
  122. isync();
  123. while (1)
  124. ;
  125. }
  126. #endif
  127. static inline void flush_spin_table(void *spin_table)
  128. {
  129. flush_dcache_range((ulong)spin_table,
  130. (ulong)spin_table + sizeof(struct epapr_spin_table));
  131. }
  132. static inline u32 read_spin_table_addr_l(void *spin_table)
  133. {
  134. flush_dcache_range((ulong)spin_table,
  135. (ulong)spin_table + sizeof(struct epapr_spin_table));
  136. return in_be32(&((struct epapr_spin_table *)spin_table)->addr_l);
  137. }
  138. static int smp_85xx_kick_cpu(int nr)
  139. {
  140. unsigned long flags;
  141. const u64 *cpu_rel_addr;
  142. __iomem struct epapr_spin_table *spin_table;
  143. struct device_node *np;
  144. int hw_cpu = get_hard_smp_processor_id(nr);
  145. int ioremappable;
  146. int ret = 0;
  147. WARN_ON(nr < 0 || nr >= NR_CPUS);
  148. WARN_ON(hw_cpu < 0 || hw_cpu >= NR_CPUS);
  149. pr_debug("smp_85xx_kick_cpu: kick CPU #%d\n", nr);
  150. np = of_get_cpu_node(nr, NULL);
  151. cpu_rel_addr = of_get_property(np, "cpu-release-addr", NULL);
  152. if (cpu_rel_addr == NULL) {
  153. printk(KERN_ERR "No cpu-release-addr for cpu %d\n", nr);
  154. return -ENOENT;
  155. }
  156. /*
  157. * A secondary core could be in a spinloop in the bootpage
  158. * (0xfffff000), somewhere in highmem, or somewhere in lowmem.
  159. * The bootpage and highmem can be accessed via ioremap(), but
  160. * we need to directly access the spinloop if its in lowmem.
  161. */
  162. ioremappable = *cpu_rel_addr > virt_to_phys(high_memory);
  163. /* Map the spin table */
  164. if (ioremappable)
  165. spin_table = ioremap_prot(*cpu_rel_addr,
  166. sizeof(struct epapr_spin_table), _PAGE_COHERENT);
  167. else
  168. spin_table = phys_to_virt(*cpu_rel_addr);
  169. local_irq_save(flags);
  170. #ifdef CONFIG_PPC32
  171. #ifdef CONFIG_HOTPLUG_CPU
  172. /* Corresponding to generic_set_cpu_dead() */
  173. generic_set_cpu_up(nr);
  174. if (system_state == SYSTEM_RUNNING) {
  175. /*
  176. * To keep it compatible with old boot program which uses
  177. * cache-inhibit spin table, we need to flush the cache
  178. * before accessing spin table to invalidate any staled data.
  179. * We also need to flush the cache after writing to spin
  180. * table to push data out.
  181. */
  182. flush_spin_table(spin_table);
  183. out_be32(&spin_table->addr_l, 0);
  184. flush_spin_table(spin_table);
  185. /*
  186. * We don't set the BPTR register here since it already points
  187. * to the boot page properly.
  188. */
  189. mpic_reset_core(nr);
  190. /*
  191. * wait until core is ready...
  192. * We need to invalidate the stale data, in case the boot
  193. * loader uses a cache-inhibited spin table.
  194. */
  195. if (!spin_event_timeout(
  196. read_spin_table_addr_l(spin_table) == 1,
  197. 10000, 100)) {
  198. pr_err("%s: timeout waiting for core %d to reset\n",
  199. __func__, hw_cpu);
  200. ret = -ENOENT;
  201. goto out;
  202. }
  203. /* clear the acknowledge status */
  204. __secondary_hold_acknowledge = -1;
  205. }
  206. #endif
  207. flush_spin_table(spin_table);
  208. out_be32(&spin_table->pir, hw_cpu);
  209. out_be32(&spin_table->addr_l, __pa(__early_start));
  210. flush_spin_table(spin_table);
  211. /* Wait a bit for the CPU to ack. */
  212. if (!spin_event_timeout(__secondary_hold_acknowledge == hw_cpu,
  213. 10000, 100)) {
  214. pr_err("%s: timeout waiting for core %d to ack\n",
  215. __func__, hw_cpu);
  216. ret = -ENOENT;
  217. goto out;
  218. }
  219. out:
  220. #else
  221. smp_generic_kick_cpu(nr);
  222. flush_spin_table(spin_table);
  223. out_be32(&spin_table->pir, hw_cpu);
  224. out_be64((u64 *)(&spin_table->addr_h),
  225. __pa((u64)*((unsigned long long *)generic_secondary_smp_init)));
  226. flush_spin_table(spin_table);
  227. #endif
  228. local_irq_restore(flags);
  229. if (ioremappable)
  230. iounmap(spin_table);
  231. return ret;
  232. }
  233. struct smp_ops_t smp_85xx_ops = {
  234. .kick_cpu = smp_85xx_kick_cpu,
  235. .cpu_bootable = smp_generic_cpu_bootable,
  236. #ifdef CONFIG_HOTPLUG_CPU
  237. .cpu_disable = generic_cpu_disable,
  238. .cpu_die = generic_cpu_die,
  239. #endif
  240. #ifdef CONFIG_KEXEC
  241. .give_timebase = smp_generic_give_timebase,
  242. .take_timebase = smp_generic_take_timebase,
  243. #endif
  244. };
  245. #ifdef CONFIG_KEXEC
  246. atomic_t kexec_down_cpus = ATOMIC_INIT(0);
  247. void mpc85xx_smp_kexec_cpu_down(int crash_shutdown, int secondary)
  248. {
  249. local_irq_disable();
  250. if (secondary) {
  251. atomic_inc(&kexec_down_cpus);
  252. /* loop forever */
  253. while (1);
  254. }
  255. }
  256. static void mpc85xx_smp_kexec_down(void *arg)
  257. {
  258. if (ppc_md.kexec_cpu_down)
  259. ppc_md.kexec_cpu_down(0,1);
  260. }
  261. static void map_and_flush(unsigned long paddr)
  262. {
  263. struct page *page = pfn_to_page(paddr >> PAGE_SHIFT);
  264. unsigned long kaddr = (unsigned long)kmap(page);
  265. flush_dcache_range(kaddr, kaddr + PAGE_SIZE);
  266. kunmap(page);
  267. }
  268. /**
  269. * Before we reset the other cores, we need to flush relevant cache
  270. * out to memory so we don't get anything corrupted, some of these flushes
  271. * are performed out of an overabundance of caution as interrupts are not
  272. * disabled yet and we can switch cores
  273. */
  274. static void mpc85xx_smp_flush_dcache_kexec(struct kimage *image)
  275. {
  276. kimage_entry_t *ptr, entry;
  277. unsigned long paddr;
  278. int i;
  279. if (image->type == KEXEC_TYPE_DEFAULT) {
  280. /* normal kexec images are stored in temporary pages */
  281. for (ptr = &image->head; (entry = *ptr) && !(entry & IND_DONE);
  282. ptr = (entry & IND_INDIRECTION) ?
  283. phys_to_virt(entry & PAGE_MASK) : ptr + 1) {
  284. if (!(entry & IND_DESTINATION)) {
  285. map_and_flush(entry);
  286. }
  287. }
  288. /* flush out last IND_DONE page */
  289. map_and_flush(entry);
  290. } else {
  291. /* crash type kexec images are copied to the crash region */
  292. for (i = 0; i < image->nr_segments; i++) {
  293. struct kexec_segment *seg = &image->segment[i];
  294. for (paddr = seg->mem; paddr < seg->mem + seg->memsz;
  295. paddr += PAGE_SIZE) {
  296. map_and_flush(paddr);
  297. }
  298. }
  299. }
  300. /* also flush the kimage struct to be passed in as well */
  301. flush_dcache_range((unsigned long)image,
  302. (unsigned long)image + sizeof(*image));
  303. }
  304. static void mpc85xx_smp_machine_kexec(struct kimage *image)
  305. {
  306. int timeout = INT_MAX;
  307. int i, num_cpus = num_present_cpus();
  308. mpc85xx_smp_flush_dcache_kexec(image);
  309. if (image->type == KEXEC_TYPE_DEFAULT)
  310. smp_call_function(mpc85xx_smp_kexec_down, NULL, 0);
  311. while ( (atomic_read(&kexec_down_cpus) != (num_cpus - 1)) &&
  312. ( timeout > 0 ) )
  313. {
  314. timeout--;
  315. }
  316. if ( !timeout )
  317. printk(KERN_ERR "Unable to bring down secondary cpu(s)");
  318. for_each_online_cpu(i)
  319. {
  320. if ( i == smp_processor_id() ) continue;
  321. mpic_reset_core(i);
  322. }
  323. default_machine_kexec(image);
  324. }
  325. #endif /* CONFIG_KEXEC */
  326. static void smp_85xx_setup_cpu(int cpu_nr)
  327. {
  328. if (smp_85xx_ops.probe == smp_mpic_probe)
  329. mpic_setup_this_cpu();
  330. if (cpu_has_feature(CPU_FTR_DBELL))
  331. doorbell_setup_this_cpu();
  332. }
  333. static const struct of_device_id mpc85xx_smp_guts_ids[] = {
  334. { .compatible = "fsl,mpc8572-guts", },
  335. { .compatible = "fsl,p1020-guts", },
  336. { .compatible = "fsl,p1021-guts", },
  337. { .compatible = "fsl,p1022-guts", },
  338. { .compatible = "fsl,p1023-guts", },
  339. { .compatible = "fsl,p2020-guts", },
  340. {},
  341. };
  342. void __init mpc85xx_smp_init(void)
  343. {
  344. struct device_node *np;
  345. smp_85xx_ops.setup_cpu = smp_85xx_setup_cpu;
  346. np = of_find_node_by_type(NULL, "open-pic");
  347. if (np) {
  348. smp_85xx_ops.probe = smp_mpic_probe;
  349. smp_85xx_ops.message_pass = smp_mpic_message_pass;
  350. }
  351. if (cpu_has_feature(CPU_FTR_DBELL)) {
  352. /*
  353. * If left NULL, .message_pass defaults to
  354. * smp_muxed_ipi_message_pass
  355. */
  356. smp_85xx_ops.message_pass = NULL;
  357. smp_85xx_ops.cause_ipi = doorbell_cause_ipi;
  358. }
  359. np = of_find_matching_node(NULL, mpc85xx_smp_guts_ids);
  360. if (np) {
  361. guts = of_iomap(np, 0);
  362. of_node_put(np);
  363. if (!guts) {
  364. pr_err("%s: Could not map guts node address\n",
  365. __func__);
  366. return;
  367. }
  368. smp_85xx_ops.give_timebase = mpc85xx_give_timebase;
  369. smp_85xx_ops.take_timebase = mpc85xx_take_timebase;
  370. #ifdef CONFIG_HOTPLUG_CPU
  371. ppc_md.cpu_die = smp_85xx_mach_cpu_die;
  372. #endif
  373. }
  374. smp_ops = &smp_85xx_ops;
  375. #ifdef CONFIG_KEXEC
  376. ppc_md.kexec_cpu_down = mpc85xx_smp_kexec_cpu_down;
  377. ppc_md.machine_kexec = mpc85xx_smp_machine_kexec;
  378. #endif
  379. }