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/of_address.h>
19 #include <linux/kexec.h>
20 #include <linux/highmem.h>
21 #include <linux/cpu.h>
22 #include <linux/fsl/guts.h>
24 #include <asm/machdep.h>
25 #include <asm/pgtable.h>
28 #include <asm/cacheflush.h>
29 #include <asm/dbell.h>
30 #include <asm/code-patching.h>
31 #include <asm/cputhreads.h>
33 #include <sysdev/fsl_soc.h>
34 #include <sysdev/mpic.h>
37 struct epapr_spin_table
{
46 static struct ccsr_guts __iomem
*guts
;
51 static void mpc85xx_timebase_freeze(int freeze
)
55 mask
= CCSR_GUTS_DEVDISR_TB0
| CCSR_GUTS_DEVDISR_TB1
;
57 setbits32(&guts
->devdisr
, mask
);
59 clrbits32(&guts
->devdisr
, mask
);
61 in_be32(&guts
->devdisr
);
64 static void mpc85xx_give_timebase(void)
68 local_irq_save(flags
);
74 mpc85xx_timebase_freeze(1);
77 * e5500/e6500 have a workaround for erratum A-006958 in place
78 * that will reread the timebase until TBL is non-zero.
79 * That would be a bad thing when the timebase is frozen.
81 * Thus, we read it manually, and instead of checking that
82 * TBL is non-zero, we ensure that TB does not change. We don't
83 * do that for the main mftb implementation, because it requires
89 asm volatile("mfspr %0, %1" : "=r" (timebase
) :
94 asm volatile("mfspr %0, %1" : "=r" (timebase
) :
96 } while (prev
!= timebase
);
107 mpc85xx_timebase_freeze(0);
109 local_irq_restore(flags
);
112 static void mpc85xx_take_timebase(void)
116 local_irq_save(flags
);
122 set_tb(timebase
>> 32, timebase
& 0xffffffff);
126 local_irq_restore(flags
);
129 #ifdef CONFIG_HOTPLUG_CPU
130 static void smp_85xx_mach_cpu_die(void)
132 unsigned int cpu
= smp_processor_id();
137 generic_set_cpu_dead(cpu
);
142 __flush_disable_L1();
143 tmp
= (mfspr(SPRN_HID0
) & ~(HID0_DOZE
|HID0_SLEEP
)) | HID0_NAP
;
144 mtspr(SPRN_HID0
, tmp
);
147 /* Enter NAP mode. */
159 static inline void flush_spin_table(void *spin_table
)
161 flush_dcache_range((ulong
)spin_table
,
162 (ulong
)spin_table
+ sizeof(struct epapr_spin_table
));
165 static inline u32
read_spin_table_addr_l(void *spin_table
)
167 flush_dcache_range((ulong
)spin_table
,
168 (ulong
)spin_table
+ sizeof(struct epapr_spin_table
));
169 return in_be32(&((struct epapr_spin_table
*)spin_table
)->addr_l
);
173 static void wake_hw_thread(void *info
)
175 void fsl_secondary_thread_init(void);
176 unsigned long imsr
, inia
;
177 int nr
= *(const int *)info
;
180 inia
= *(unsigned long *)fsl_secondary_thread_init
;
182 if (cpu_thread_in_core(nr
) == 0) {
183 /* For when we boot on a secondary thread with kdump */
184 mttmr(TMRN_IMSR0
, imsr
);
185 mttmr(TMRN_INIA0
, inia
);
186 mtspr(SPRN_TENS
, TEN_THREAD(0));
188 mttmr(TMRN_IMSR1
, imsr
);
189 mttmr(TMRN_INIA1
, inia
);
190 mtspr(SPRN_TENS
, TEN_THREAD(1));
193 smp_generic_kick_cpu(nr
);
197 static int smp_85xx_kick_cpu(int nr
)
200 const u64
*cpu_rel_addr
;
201 __iomem
struct epapr_spin_table
*spin_table
;
202 struct device_node
*np
;
203 int hw_cpu
= get_hard_smp_processor_id(nr
);
207 WARN_ON(nr
< 0 || nr
>= NR_CPUS
);
208 WARN_ON(hw_cpu
< 0 || hw_cpu
>= NR_CPUS
);
210 pr_debug("smp_85xx_kick_cpu: kick CPU #%d\n", nr
);
213 /* Threads don't use the spin table */
214 if (cpu_thread_in_core(nr
) != 0) {
215 int primary
= cpu_first_thread_sibling(nr
);
217 if (WARN_ON_ONCE(!cpu_has_feature(CPU_FTR_SMT
)))
220 if (cpu_thread_in_core(nr
) != 1) {
221 pr_err("%s: cpu %d: invalid hw thread %d\n",
222 __func__
, nr
, cpu_thread_in_core(nr
));
226 if (!cpu_online(primary
)) {
227 pr_err("%s: cpu %d: primary %d not online\n",
228 __func__
, nr
, primary
);
232 smp_call_function_single(primary
, wake_hw_thread
, &nr
, 0);
234 } else if (cpu_thread_in_core(boot_cpuid
) != 0 &&
235 cpu_first_thread_sibling(boot_cpuid
) == nr
) {
236 if (WARN_ON_ONCE(!cpu_has_feature(CPU_FTR_SMT
)))
239 smp_call_function_single(boot_cpuid
, wake_hw_thread
, &nr
, 0);
243 np
= of_get_cpu_node(nr
, NULL
);
244 cpu_rel_addr
= of_get_property(np
, "cpu-release-addr", NULL
);
246 if (cpu_rel_addr
== NULL
) {
247 printk(KERN_ERR
"No cpu-release-addr for cpu %d\n", nr
);
252 * A secondary core could be in a spinloop in the bootpage
253 * (0xfffff000), somewhere in highmem, or somewhere in lowmem.
254 * The bootpage and highmem can be accessed via ioremap(), but
255 * we need to directly access the spinloop if its in lowmem.
257 ioremappable
= *cpu_rel_addr
> virt_to_phys(high_memory
);
259 /* Map the spin table */
261 spin_table
= ioremap_prot(*cpu_rel_addr
,
262 sizeof(struct epapr_spin_table
), _PAGE_COHERENT
);
264 spin_table
= phys_to_virt(*cpu_rel_addr
);
266 local_irq_save(flags
);
268 #ifdef CONFIG_HOTPLUG_CPU
269 /* Corresponding to generic_set_cpu_dead() */
270 generic_set_cpu_up(nr
);
272 if (system_state
== SYSTEM_RUNNING
) {
274 * To keep it compatible with old boot program which uses
275 * cache-inhibit spin table, we need to flush the cache
276 * before accessing spin table to invalidate any staled data.
277 * We also need to flush the cache after writing to spin
278 * table to push data out.
280 flush_spin_table(spin_table
);
281 out_be32(&spin_table
->addr_l
, 0);
282 flush_spin_table(spin_table
);
285 * We don't set the BPTR register here since it already points
286 * to the boot page properly.
291 * wait until core is ready...
292 * We need to invalidate the stale data, in case the boot
293 * loader uses a cache-inhibited spin table.
295 if (!spin_event_timeout(
296 read_spin_table_addr_l(spin_table
) == 1,
298 pr_err("%s: timeout waiting for core %d to reset\n",
304 /* clear the acknowledge status */
305 __secondary_hold_acknowledge
= -1;
308 flush_spin_table(spin_table
);
309 out_be32(&spin_table
->pir
, hw_cpu
);
310 out_be32(&spin_table
->addr_l
, __pa(__early_start
));
311 flush_spin_table(spin_table
);
313 /* Wait a bit for the CPU to ack. */
314 if (!spin_event_timeout(__secondary_hold_acknowledge
== hw_cpu
,
316 pr_err("%s: timeout waiting for core %d to ack\n",
323 smp_generic_kick_cpu(nr
);
325 flush_spin_table(spin_table
);
326 out_be32(&spin_table
->pir
, hw_cpu
);
327 out_be64((u64
*)(&spin_table
->addr_h
),
328 __pa(ppc_function_entry(generic_secondary_smp_init
)));
329 flush_spin_table(spin_table
);
332 local_irq_restore(flags
);
340 struct smp_ops_t smp_85xx_ops
= {
341 .kick_cpu
= smp_85xx_kick_cpu
,
342 .cpu_bootable
= smp_generic_cpu_bootable
,
343 #ifdef CONFIG_HOTPLUG_CPU
344 .cpu_disable
= generic_cpu_disable
,
345 .cpu_die
= generic_cpu_die
,
347 #if defined(CONFIG_KEXEC) && !defined(CONFIG_PPC64)
348 .give_timebase
= smp_generic_give_timebase
,
349 .take_timebase
= smp_generic_take_timebase
,
355 atomic_t kexec_down_cpus
= ATOMIC_INIT(0);
357 void mpc85xx_smp_kexec_cpu_down(int crash_shutdown
, int secondary
)
362 __flush_disable_L1();
363 atomic_inc(&kexec_down_cpus
);
369 static void mpc85xx_smp_kexec_down(void *arg
)
371 if (ppc_md
.kexec_cpu_down
)
372 ppc_md
.kexec_cpu_down(0,1);
375 void mpc85xx_smp_kexec_cpu_down(int crash_shutdown
, int secondary
)
377 int cpu
= smp_processor_id();
378 int sibling
= cpu_last_thread_sibling(cpu
);
379 bool notified
= false;
381 int disable_threadbit
= 0;
387 mpic_teardown_this_cpu(secondary
);
389 if (cpu
== crashing_cpu
&& cpu_thread_in_core(cpu
) != 0) {
391 * We enter the crash kernel on whatever cpu crashed,
392 * even if it's a secondary thread. If that's the case,
393 * disable the corresponding primary thread.
395 disable_threadbit
= 1;
396 disable_cpu
= cpu_first_thread_sibling(cpu
);
397 } else if (sibling
!= crashing_cpu
&&
398 cpu_thread_in_core(cpu
) == 0 &&
399 cpu_thread_in_core(sibling
) != 0) {
400 disable_threadbit
= 2;
401 disable_cpu
= sibling
;
404 if (disable_threadbit
) {
405 while (paca
[disable_cpu
].kexec_state
< KEXEC_STATE_REAL_MODE
) {
408 if (!notified
&& now
- start
> 1000000) {
409 pr_info("%s/%d: waiting for cpu %d to enter KEXEC_STATE_REAL_MODE (%d)\n",
410 __func__
, smp_processor_id(),
412 paca
[disable_cpu
].kexec_state
);
418 pr_info("%s: cpu %d done waiting\n",
419 __func__
, disable_cpu
);
422 mtspr(SPRN_TENC
, disable_threadbit
);
423 while (mfspr(SPRN_TENSR
) & disable_threadbit
)
429 static void mpc85xx_smp_machine_kexec(struct kimage
*image
)
432 int timeout
= INT_MAX
;
433 int i
, num_cpus
= num_present_cpus();
435 if (image
->type
== KEXEC_TYPE_DEFAULT
)
436 smp_call_function(mpc85xx_smp_kexec_down
, NULL
, 0);
438 while ( (atomic_read(&kexec_down_cpus
) != (num_cpus
- 1)) &&
445 printk(KERN_ERR
"Unable to bring down secondary cpu(s)");
447 for_each_online_cpu(i
)
449 if ( i
== smp_processor_id() ) continue;
454 default_machine_kexec(image
);
456 #endif /* CONFIG_KEXEC */
458 static void smp_85xx_basic_setup(int cpu_nr
)
460 if (cpu_has_feature(CPU_FTR_DBELL
))
461 doorbell_setup_this_cpu();
464 static void smp_85xx_setup_cpu(int cpu_nr
)
466 mpic_setup_this_cpu();
467 smp_85xx_basic_setup(cpu_nr
);
470 static const struct of_device_id mpc85xx_smp_guts_ids
[] = {
471 { .compatible
= "fsl,mpc8572-guts", },
472 { .compatible
= "fsl,p1020-guts", },
473 { .compatible
= "fsl,p1021-guts", },
474 { .compatible
= "fsl,p1022-guts", },
475 { .compatible
= "fsl,p1023-guts", },
476 { .compatible
= "fsl,p2020-guts", },
480 void __init
mpc85xx_smp_init(void)
482 struct device_node
*np
;
485 np
= of_find_node_by_type(NULL
, "open-pic");
487 smp_85xx_ops
.probe
= smp_mpic_probe
;
488 smp_85xx_ops
.setup_cpu
= smp_85xx_setup_cpu
;
489 smp_85xx_ops
.message_pass
= smp_mpic_message_pass
;
491 smp_85xx_ops
.setup_cpu
= smp_85xx_basic_setup
;
493 if (cpu_has_feature(CPU_FTR_DBELL
)) {
495 * If left NULL, .message_pass defaults to
496 * smp_muxed_ipi_message_pass
498 smp_85xx_ops
.message_pass
= NULL
;
499 smp_85xx_ops
.cause_ipi
= doorbell_cause_ipi
;
500 smp_85xx_ops
.probe
= NULL
;
503 np
= of_find_matching_node(NULL
, mpc85xx_smp_guts_ids
);
505 guts
= of_iomap(np
, 0);
508 pr_err("%s: Could not map guts node address\n",
512 smp_85xx_ops
.give_timebase
= mpc85xx_give_timebase
;
513 smp_85xx_ops
.take_timebase
= mpc85xx_take_timebase
;
514 #ifdef CONFIG_HOTPLUG_CPU
515 ppc_md
.cpu_die
= smp_85xx_mach_cpu_die
;
519 smp_ops
= &smp_85xx_ops
;
522 ppc_md
.kexec_cpu_down
= mpc85xx_smp_kexec_cpu_down
;
523 ppc_md
.machine_kexec
= mpc85xx_smp_machine_kexec
;