smp.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. extern void __early_start(void);
  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. timebase = get_tb();
  62. mb();
  63. tb_valid = 1;
  64. while (tb_valid)
  65. barrier();
  66. mpc85xx_timebase_freeze(0);
  67. local_irq_restore(flags);
  68. }
  69. static void mpc85xx_take_timebase(void)
  70. {
  71. unsigned long flags;
  72. local_irq_save(flags);
  73. tb_req = 1;
  74. while (!tb_valid)
  75. barrier();
  76. set_tb(timebase >> 32, timebase & 0xffffffff);
  77. isync();
  78. tb_valid = 0;
  79. local_irq_restore(flags);
  80. }
  81. static int __init
  82. smp_85xx_kick_cpu(int nr)
  83. {
  84. unsigned long flags;
  85. const u64 *cpu_rel_addr;
  86. __iomem struct epapr_spin_table *spin_table;
  87. struct device_node *np;
  88. int n = 0, hw_cpu = get_hard_smp_processor_id(nr);
  89. int ioremappable;
  90. WARN_ON(nr < 0 || nr >= NR_CPUS);
  91. WARN_ON(hw_cpu < 0 || hw_cpu >= NR_CPUS);
  92. pr_debug("smp_85xx_kick_cpu: kick CPU #%d\n", nr);
  93. np = of_get_cpu_node(nr, NULL);
  94. cpu_rel_addr = of_get_property(np, "cpu-release-addr", NULL);
  95. if (cpu_rel_addr == NULL) {
  96. printk(KERN_ERR "No cpu-release-addr for cpu %d\n", nr);
  97. return -ENOENT;
  98. }
  99. /*
  100. * A secondary core could be in a spinloop in the bootpage
  101. * (0xfffff000), somewhere in highmem, or somewhere in lowmem.
  102. * The bootpage and highmem can be accessed via ioremap(), but
  103. * we need to directly access the spinloop if its in lowmem.
  104. */
  105. ioremappable = *cpu_rel_addr > virt_to_phys(high_memory);
  106. /* Map the spin table */
  107. if (ioremappable)
  108. spin_table = ioremap(*cpu_rel_addr,
  109. sizeof(struct epapr_spin_table));
  110. else
  111. spin_table = phys_to_virt(*cpu_rel_addr);
  112. local_irq_save(flags);
  113. out_be32(&spin_table->pir, hw_cpu);
  114. #ifdef CONFIG_PPC32
  115. out_be32(&spin_table->addr_l, __pa(__early_start));
  116. if (!ioremappable)
  117. flush_dcache_range((ulong)spin_table,
  118. (ulong)spin_table + sizeof(struct epapr_spin_table));
  119. /* Wait a bit for the CPU to ack. */
  120. while ((__secondary_hold_acknowledge != hw_cpu) && (++n < 1000))
  121. mdelay(1);
  122. #else
  123. smp_generic_kick_cpu(nr);
  124. out_be64((u64 *)(&spin_table->addr_h),
  125. __pa((u64)*((unsigned long long *)generic_secondary_smp_init)));
  126. if (!ioremappable)
  127. flush_dcache_range((ulong)spin_table,
  128. (ulong)spin_table + sizeof(struct epapr_spin_table));
  129. #endif
  130. local_irq_restore(flags);
  131. if (ioremappable)
  132. iounmap(spin_table);
  133. pr_debug("waited %d msecs for CPU #%d.\n", n, nr);
  134. return 0;
  135. }
  136. struct smp_ops_t smp_85xx_ops = {
  137. .kick_cpu = smp_85xx_kick_cpu,
  138. #ifdef CONFIG_KEXEC
  139. .give_timebase = smp_generic_give_timebase,
  140. .take_timebase = smp_generic_take_timebase,
  141. #endif
  142. };
  143. #ifdef CONFIG_KEXEC
  144. atomic_t kexec_down_cpus = ATOMIC_INIT(0);
  145. void mpc85xx_smp_kexec_cpu_down(int crash_shutdown, int secondary)
  146. {
  147. local_irq_disable();
  148. if (secondary) {
  149. atomic_inc(&kexec_down_cpus);
  150. /* loop forever */
  151. while (1);
  152. }
  153. }
  154. static void mpc85xx_smp_kexec_down(void *arg)
  155. {
  156. if (ppc_md.kexec_cpu_down)
  157. ppc_md.kexec_cpu_down(0,1);
  158. }
  159. static void map_and_flush(unsigned long paddr)
  160. {
  161. struct page *page = pfn_to_page(paddr >> PAGE_SHIFT);
  162. unsigned long kaddr = (unsigned long)kmap(page);
  163. flush_dcache_range(kaddr, kaddr + PAGE_SIZE);
  164. kunmap(page);
  165. }
  166. /**
  167. * Before we reset the other cores, we need to flush relevant cache
  168. * out to memory so we don't get anything corrupted, some of these flushes
  169. * are performed out of an overabundance of caution as interrupts are not
  170. * disabled yet and we can switch cores
  171. */
  172. static void mpc85xx_smp_flush_dcache_kexec(struct kimage *image)
  173. {
  174. kimage_entry_t *ptr, entry;
  175. unsigned long paddr;
  176. int i;
  177. if (image->type == KEXEC_TYPE_DEFAULT) {
  178. /* normal kexec images are stored in temporary pages */
  179. for (ptr = &image->head; (entry = *ptr) && !(entry & IND_DONE);
  180. ptr = (entry & IND_INDIRECTION) ?
  181. phys_to_virt(entry & PAGE_MASK) : ptr + 1) {
  182. if (!(entry & IND_DESTINATION)) {
  183. map_and_flush(entry);
  184. }
  185. }
  186. /* flush out last IND_DONE page */
  187. map_and_flush(entry);
  188. } else {
  189. /* crash type kexec images are copied to the crash region */
  190. for (i = 0; i < image->nr_segments; i++) {
  191. struct kexec_segment *seg = &image->segment[i];
  192. for (paddr = seg->mem; paddr < seg->mem + seg->memsz;
  193. paddr += PAGE_SIZE) {
  194. map_and_flush(paddr);
  195. }
  196. }
  197. }
  198. /* also flush the kimage struct to be passed in as well */
  199. flush_dcache_range((unsigned long)image,
  200. (unsigned long)image + sizeof(*image));
  201. }
  202. static void mpc85xx_smp_machine_kexec(struct kimage *image)
  203. {
  204. int timeout = INT_MAX;
  205. int i, num_cpus = num_present_cpus();
  206. mpc85xx_smp_flush_dcache_kexec(image);
  207. if (image->type == KEXEC_TYPE_DEFAULT)
  208. smp_call_function(mpc85xx_smp_kexec_down, NULL, 0);
  209. while ( (atomic_read(&kexec_down_cpus) != (num_cpus - 1)) &&
  210. ( timeout > 0 ) )
  211. {
  212. timeout--;
  213. }
  214. if ( !timeout )
  215. printk(KERN_ERR "Unable to bring down secondary cpu(s)");
  216. for_each_online_cpu(i)
  217. {
  218. if ( i == smp_processor_id() ) continue;
  219. mpic_reset_core(i);
  220. }
  221. default_machine_kexec(image);
  222. }
  223. #endif /* CONFIG_KEXEC */
  224. static void __init
  225. smp_85xx_setup_cpu(int cpu_nr)
  226. {
  227. if (smp_85xx_ops.probe == smp_mpic_probe)
  228. mpic_setup_this_cpu();
  229. if (cpu_has_feature(CPU_FTR_DBELL))
  230. doorbell_setup_this_cpu();
  231. }
  232. static const struct of_device_id mpc85xx_smp_guts_ids[] = {
  233. { .compatible = "fsl,mpc8572-guts", },
  234. { .compatible = "fsl,p1020-guts", },
  235. { .compatible = "fsl,p1021-guts", },
  236. { .compatible = "fsl,p1022-guts", },
  237. { .compatible = "fsl,p1023-guts", },
  238. { .compatible = "fsl,p2020-guts", },
  239. {},
  240. };
  241. void __init mpc85xx_smp_init(void)
  242. {
  243. struct device_node *np;
  244. smp_85xx_ops.setup_cpu = smp_85xx_setup_cpu;
  245. np = of_find_node_by_type(NULL, "open-pic");
  246. if (np) {
  247. smp_85xx_ops.probe = smp_mpic_probe;
  248. smp_85xx_ops.message_pass = smp_mpic_message_pass;
  249. }
  250. if (cpu_has_feature(CPU_FTR_DBELL)) {
  251. /*
  252. * If left NULL, .message_pass defaults to
  253. * smp_muxed_ipi_message_pass
  254. */
  255. smp_85xx_ops.message_pass = NULL;
  256. smp_85xx_ops.cause_ipi = doorbell_cause_ipi;
  257. }
  258. np = of_find_matching_node(NULL, mpc85xx_smp_guts_ids);
  259. if (np) {
  260. guts = of_iomap(np, 0);
  261. of_node_put(np);
  262. if (!guts) {
  263. pr_err("%s: Could not map guts node address\n",
  264. __func__);
  265. return;
  266. }
  267. smp_85xx_ops.give_timebase = mpc85xx_give_timebase;
  268. smp_85xx_ops.take_timebase = mpc85xx_take_timebase;
  269. }
  270. smp_ops = &smp_85xx_ops;
  271. #ifdef CONFIG_KEXEC
  272. ppc_md.kexec_cpu_down = mpc85xx_smp_kexec_cpu_down;
  273. ppc_md.machine_kexec = mpc85xx_smp_machine_kexec;
  274. #endif
  275. }