smp.c 8.9 KB

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