smp.c 10 KB

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