2 * Author: Andy Fleming <afleming@freescale.com>
3 * Kumar Gala <galak@kernel.crashing.org>
5 * Copyright 2006-2008, 2011-2012 Freescale Semiconductor Inc.
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.
13 #include <linux/stddef.h>
14 #include <linux/kernel.h>
15 #include <linux/init.h>
16 #include <linux/delay.h>
18 #include <linux/kexec.h>
19 #include <linux/highmem.h>
20 #include <linux/cpu.h>
22 #include <asm/machdep.h>
23 #include <asm/pgtable.h>
26 #include <asm/cacheflush.h>
27 #include <asm/dbell.h>
28 #include <asm/fsl_guts.h>
30 #include <sysdev/fsl_soc.h>
31 #include <sysdev/mpic.h>
34 extern void __early_start(void);
36 struct epapr_spin_table {
45 static struct ccsr_guts __iomem *guts;
50 static void mpc85xx_timebase_freeze(int freeze)
54 mask = CCSR_GUTS_DEVDISR_TB0 | CCSR_GUTS_DEVDISR_TB1;
56 setbits32(&guts->devdisr, mask);
58 clrbits32(&guts->devdisr, mask);
60 in_be32(&guts->devdisr);
63 static void mpc85xx_give_timebase(void)
67 local_irq_save(flags);
73 mpc85xx_timebase_freeze(1);
81 mpc85xx_timebase_freeze(0);
83 local_irq_restore(flags);
86 static void mpc85xx_take_timebase(void)
90 local_irq_save(flags);
96 set_tb(timebase >> 32, timebase & 0xffffffff);
100 local_irq_restore(flags);
104 smp_85xx_kick_cpu(int nr)
107 const u64 *cpu_rel_addr;
108 __iomem struct epapr_spin_table *spin_table;
109 struct device_node *np;
110 int n = 0, hw_cpu = get_hard_smp_processor_id(nr);
113 WARN_ON(nr < 0 || nr >= NR_CPUS);
114 WARN_ON(hw_cpu < 0 || hw_cpu >= NR_CPUS);
116 pr_debug("smp_85xx_kick_cpu: kick CPU #%d\n", nr);
118 np = of_get_cpu_node(nr, NULL);
119 cpu_rel_addr = of_get_property(np, "cpu-release-addr", NULL);
121 if (cpu_rel_addr == NULL) {
122 printk(KERN_ERR "No cpu-release-addr for cpu %d\n", nr);
127 * A secondary core could be in a spinloop in the bootpage
128 * (0xfffff000), somewhere in highmem, or somewhere in lowmem.
129 * The bootpage and highmem can be accessed via ioremap(), but
130 * we need to directly access the spinloop if its in lowmem.
132 ioremappable = *cpu_rel_addr > virt_to_phys(high_memory);
134 /* Map the spin table */
136 spin_table = ioremap(*cpu_rel_addr,
137 sizeof(struct epapr_spin_table));
139 spin_table = phys_to_virt(*cpu_rel_addr);
141 local_irq_save(flags);
143 out_be32(&spin_table->pir, hw_cpu);
145 out_be32(&spin_table->addr_l, __pa(__early_start));
148 flush_dcache_range((ulong)spin_table,
149 (ulong)spin_table + sizeof(struct epapr_spin_table));
151 /* Wait a bit for the CPU to ack. */
152 while ((__secondary_hold_acknowledge != hw_cpu) && (++n < 1000))
155 smp_generic_kick_cpu(nr);
157 out_be64((u64 *)(&spin_table->addr_h),
158 __pa((u64)*((unsigned long long *)generic_secondary_smp_init)));
161 flush_dcache_range((ulong)spin_table,
162 (ulong)spin_table + sizeof(struct epapr_spin_table));
165 local_irq_restore(flags);
170 pr_debug("waited %d msecs for CPU #%d.\n", n, nr);
175 struct smp_ops_t smp_85xx_ops = {
176 .kick_cpu = smp_85xx_kick_cpu,
178 .give_timebase = smp_generic_give_timebase,
179 .take_timebase = smp_generic_take_timebase,
184 atomic_t kexec_down_cpus = ATOMIC_INIT(0);
186 void mpc85xx_smp_kexec_cpu_down(int crash_shutdown, int secondary)
191 atomic_inc(&kexec_down_cpus);
197 static void mpc85xx_smp_kexec_down(void *arg)
199 if (ppc_md.kexec_cpu_down)
200 ppc_md.kexec_cpu_down(0,1);
203 static void map_and_flush(unsigned long paddr)
205 struct page *page = pfn_to_page(paddr >> PAGE_SHIFT);
206 unsigned long kaddr = (unsigned long)kmap(page);
208 flush_dcache_range(kaddr, kaddr + PAGE_SIZE);
213 * Before we reset the other cores, we need to flush relevant cache
214 * out to memory so we don't get anything corrupted, some of these flushes
215 * are performed out of an overabundance of caution as interrupts are not
216 * disabled yet and we can switch cores
218 static void mpc85xx_smp_flush_dcache_kexec(struct kimage *image)
220 kimage_entry_t *ptr, entry;
224 if (image->type == KEXEC_TYPE_DEFAULT) {
225 /* normal kexec images are stored in temporary pages */
226 for (ptr = &image->head; (entry = *ptr) && !(entry & IND_DONE);
227 ptr = (entry & IND_INDIRECTION) ?
228 phys_to_virt(entry & PAGE_MASK) : ptr + 1) {
229 if (!(entry & IND_DESTINATION)) {
230 map_and_flush(entry);
233 /* flush out last IND_DONE page */
234 map_and_flush(entry);
236 /* crash type kexec images are copied to the crash region */
237 for (i = 0; i < image->nr_segments; i++) {
238 struct kexec_segment *seg = &image->segment[i];
239 for (paddr = seg->mem; paddr < seg->mem + seg->memsz;
240 paddr += PAGE_SIZE) {
241 map_and_flush(paddr);
246 /* also flush the kimage struct to be passed in as well */
247 flush_dcache_range((unsigned long)image,
248 (unsigned long)image + sizeof(*image));
251 static void mpc85xx_smp_machine_kexec(struct kimage *image)
253 int timeout = INT_MAX;
254 int i, num_cpus = num_present_cpus();
256 mpc85xx_smp_flush_dcache_kexec(image);
258 if (image->type == KEXEC_TYPE_DEFAULT)
259 smp_call_function(mpc85xx_smp_kexec_down, NULL, 0);
261 while ( (atomic_read(&kexec_down_cpus) != (num_cpus - 1)) &&
268 printk(KERN_ERR "Unable to bring down secondary cpu(s)");
270 for_each_online_cpu(i)
272 if ( i == smp_processor_id() ) continue;
276 default_machine_kexec(image);
278 #endif /* CONFIG_KEXEC */
281 smp_85xx_setup_cpu(int cpu_nr)
283 if (smp_85xx_ops.probe == smp_mpic_probe)
284 mpic_setup_this_cpu();
286 if (cpu_has_feature(CPU_FTR_DBELL))
287 doorbell_setup_this_cpu();
290 static const struct of_device_id mpc85xx_smp_guts_ids[] = {
291 { .compatible = "fsl,mpc8572-guts", },
292 { .compatible = "fsl,p1020-guts", },
293 { .compatible = "fsl,p1021-guts", },
294 { .compatible = "fsl,p1022-guts", },
295 { .compatible = "fsl,p1023-guts", },
296 { .compatible = "fsl,p2020-guts", },
300 void __init mpc85xx_smp_init(void)
302 struct device_node *np;
304 smp_85xx_ops.setup_cpu = smp_85xx_setup_cpu;
306 np = of_find_node_by_type(NULL, "open-pic");
308 smp_85xx_ops.probe = smp_mpic_probe;
309 smp_85xx_ops.message_pass = smp_mpic_message_pass;
312 if (cpu_has_feature(CPU_FTR_DBELL)) {
314 * If left NULL, .message_pass defaults to
315 * smp_muxed_ipi_message_pass
317 smp_85xx_ops.message_pass = NULL;
318 smp_85xx_ops.cause_ipi = doorbell_cause_ipi;
321 np = of_find_matching_node(NULL, mpc85xx_smp_guts_ids);
323 guts = of_iomap(np, 0);
326 pr_err("%s: Could not map guts node address\n",
330 smp_85xx_ops.give_timebase = mpc85xx_give_timebase;
331 smp_85xx_ops.take_timebase = mpc85xx_take_timebase;
334 smp_ops = &smp_85xx_ops;
337 ppc_md.kexec_cpu_down = mpc85xx_smp_kexec_cpu_down;
338 ppc_md.machine_kexec = mpc85xx_smp_machine_kexec;