smp.c 9.7 KB

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