blob: ff4249044a3ce3929771d0b1d69a502868644b5d [file] [log] [blame]
Kumar Galad5b26db2008-11-19 09:35:56 -06001/*
2 * Author: Andy Fleming <afleming@freescale.com>
3 * Kumar Gala <galak@kernel.crashing.org>
4 *
Laurentiu TUDOR2647aa12011-07-07 16:44:30 +03005 * Copyright 2006-2008, 2011 Freescale Semiconductor Inc.
Kumar Galad5b26db2008-11-19 09:35:56 -06006 *
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
13#include <linux/stddef.h>
14#include <linux/kernel.h>
15#include <linux/init.h>
16#include <linux/delay.h>
17#include <linux/of.h>
Matthew McClintockf933a412010-07-21 16:14:53 -050018#include <linux/kexec.h>
Matthew McClintock677de422010-09-16 17:58:26 -050019#include <linux/highmem.h>
Kumar Galad5b26db2008-11-19 09:35:56 -060020
21#include <asm/machdep.h>
22#include <asm/pgtable.h>
23#include <asm/page.h>
24#include <asm/mpic.h>
25#include <asm/cacheflush.h>
Kumar Gala563fdd42009-02-11 22:50:42 -060026#include <asm/dbell.h>
Kumar Galad5b26db2008-11-19 09:35:56 -060027
28#include <sysdev/fsl_soc.h>
Matthew McClintockf933a412010-07-21 16:14:53 -050029#include <sysdev/mpic.h>
Kyle Moffett582d3e02011-12-02 06:27:58 +000030#include "smp.h"
Kumar Galad5b26db2008-11-19 09:35:56 -060031
Kumar Galad5b26db2008-11-19 09:35:56 -060032extern void __early_start(void);
33
34#define BOOT_ENTRY_ADDR_UPPER 0
35#define BOOT_ENTRY_ADDR_LOWER 1
36#define BOOT_ENTRY_R3_UPPER 2
37#define BOOT_ENTRY_R3_LOWER 3
38#define BOOT_ENTRY_RESV 4
39#define BOOT_ENTRY_PIR 5
40#define BOOT_ENTRY_R6_UPPER 6
41#define BOOT_ENTRY_R6_LOWER 7
42#define NUM_BOOT_ENTRY 8
43#define SIZE_BOOT_ENTRY (NUM_BOOT_ENTRY * sizeof(u32))
44
Michael Ellermande300972011-04-11 21:46:19 +000045static int __init
Kumar Galad5b26db2008-11-19 09:35:56 -060046smp_85xx_kick_cpu(int nr)
47{
48 unsigned long flags;
49 const u64 *cpu_rel_addr;
50 __iomem u32 *bptr_vaddr;
51 struct device_node *np;
Kumar Gala45116802011-10-13 10:13:09 -050052 int n = 0, hw_cpu = get_hard_smp_processor_id(nr);
Peter Tyserd1d47ec2009-12-18 16:50:37 -060053 int ioremappable;
Kumar Galad5b26db2008-11-19 09:35:56 -060054
Kumar Gala45116802011-10-13 10:13:09 -050055 WARN_ON(nr < 0 || nr >= NR_CPUS);
56 WARN_ON(hw_cpu < 0 || hw_cpu >= NR_CPUS);
Kumar Galad5b26db2008-11-19 09:35:56 -060057
58 pr_debug("smp_85xx_kick_cpu: kick CPU #%d\n", nr);
59
Kumar Galad5b26db2008-11-19 09:35:56 -060060 np = of_get_cpu_node(nr, NULL);
61 cpu_rel_addr = of_get_property(np, "cpu-release-addr", NULL);
62
63 if (cpu_rel_addr == NULL) {
64 printk(KERN_ERR "No cpu-release-addr for cpu %d\n", nr);
Michael Ellermande300972011-04-11 21:46:19 +000065 return -ENOENT;
Kumar Galad5b26db2008-11-19 09:35:56 -060066 }
67
Peter Tyserd1d47ec2009-12-18 16:50:37 -060068 /*
69 * A secondary core could be in a spinloop in the bootpage
70 * (0xfffff000), somewhere in highmem, or somewhere in lowmem.
71 * The bootpage and highmem can be accessed via ioremap(), but
72 * we need to directly access the spinloop if its in lowmem.
73 */
74 ioremappable = *cpu_rel_addr > virt_to_phys(high_memory);
75
Kumar Galad5b26db2008-11-19 09:35:56 -060076 /* Map the spin table */
Peter Tyserd1d47ec2009-12-18 16:50:37 -060077 if (ioremappable)
78 bptr_vaddr = ioremap(*cpu_rel_addr, SIZE_BOOT_ENTRY);
79 else
80 bptr_vaddr = phys_to_virt(*cpu_rel_addr);
Kumar Galad5b26db2008-11-19 09:35:56 -060081
Kumar Galacb1ffb6202009-06-19 03:30:42 -050082 local_irq_save(flags);
83
Kumar Gala45116802011-10-13 10:13:09 -050084 out_be32(bptr_vaddr + BOOT_ENTRY_PIR, hw_cpu);
Kumar Gala5b8544c2010-10-08 10:37:31 -050085#ifdef CONFIG_PPC32
Kumar Galad5b26db2008-11-19 09:35:56 -060086 out_be32(bptr_vaddr + BOOT_ENTRY_ADDR_LOWER, __pa(__early_start));
87
Peter Tyserd1d47ec2009-12-18 16:50:37 -060088 if (!ioremappable)
89 flush_dcache_range((ulong)bptr_vaddr,
90 (ulong)(bptr_vaddr + SIZE_BOOT_ENTRY));
91
Kumar Galad5b26db2008-11-19 09:35:56 -060092 /* Wait a bit for the CPU to ack. */
Kumar Gala45116802011-10-13 10:13:09 -050093 while ((__secondary_hold_acknowledge != hw_cpu) && (++n < 1000))
Kumar Galad5b26db2008-11-19 09:35:56 -060094 mdelay(1);
Kumar Gala5b8544c2010-10-08 10:37:31 -050095#else
Kumar Galadecbb282011-02-14 22:45:48 -060096 smp_generic_kick_cpu(nr);
97
Kumar Gala5b8544c2010-10-08 10:37:31 -050098 out_be64((u64 *)(bptr_vaddr + BOOT_ENTRY_ADDR_UPPER),
99 __pa((u64)*((unsigned long long *) generic_secondary_smp_init)));
100
Kumar Galadecbb282011-02-14 22:45:48 -0600101 if (!ioremappable)
102 flush_dcache_range((ulong)bptr_vaddr,
103 (ulong)(bptr_vaddr + SIZE_BOOT_ENTRY));
Kumar Gala5b8544c2010-10-08 10:37:31 -0500104#endif
Kumar Galad5b26db2008-11-19 09:35:56 -0600105
Kumar Galad5b26db2008-11-19 09:35:56 -0600106 local_irq_restore(flags);
107
Peter Tyserd1d47ec2009-12-18 16:50:37 -0600108 if (ioremappable)
109 iounmap(bptr_vaddr);
Kumar Galacb1ffb6202009-06-19 03:30:42 -0500110
Kumar Galad5b26db2008-11-19 09:35:56 -0600111 pr_debug("waited %d msecs for CPU #%d.\n", n, nr);
Michael Ellermande300972011-04-11 21:46:19 +0000112
113 return 0;
Kumar Galad5b26db2008-11-19 09:35:56 -0600114}
115
Kumar Galad5b26db2008-11-19 09:35:56 -0600116struct smp_ops_t smp_85xx_ops = {
Kumar Galad5b26db2008-11-19 09:35:56 -0600117 .kick_cpu = smp_85xx_kick_cpu,
Matthew McClintockf933a412010-07-21 16:14:53 -0500118#ifdef CONFIG_KEXEC
119 .give_timebase = smp_generic_give_timebase,
120 .take_timebase = smp_generic_take_timebase,
121#endif
Kumar Galad5b26db2008-11-19 09:35:56 -0600122};
123
Matthew McClintockf933a412010-07-21 16:14:53 -0500124#ifdef CONFIG_KEXEC
Matthew McClintock5d692962010-09-16 17:58:25 -0500125atomic_t kexec_down_cpus = ATOMIC_INIT(0);
Matthew McClintockf933a412010-07-21 16:14:53 -0500126
127void mpc85xx_smp_kexec_cpu_down(int crash_shutdown, int secondary)
128{
Matthew McClintock5d692962010-09-16 17:58:25 -0500129 local_irq_disable();
Matthew McClintockf933a412010-07-21 16:14:53 -0500130
Matthew McClintock5d692962010-09-16 17:58:25 -0500131 if (secondary) {
132 atomic_inc(&kexec_down_cpus);
133 /* loop forever */
Matthew McClintockf933a412010-07-21 16:14:53 -0500134 while (1);
135 }
136}
137
138static void mpc85xx_smp_kexec_down(void *arg)
139{
140 if (ppc_md.kexec_cpu_down)
141 ppc_md.kexec_cpu_down(0,1);
142}
143
Matthew McClintock677de422010-09-16 17:58:26 -0500144static void map_and_flush(unsigned long paddr)
145{
146 struct page *page = pfn_to_page(paddr >> PAGE_SHIFT);
147 unsigned long kaddr = (unsigned long)kmap(page);
148
149 flush_dcache_range(kaddr, kaddr + PAGE_SIZE);
150 kunmap(page);
151}
152
153/**
154 * Before we reset the other cores, we need to flush relevant cache
155 * out to memory so we don't get anything corrupted, some of these flushes
156 * are performed out of an overabundance of caution as interrupts are not
157 * disabled yet and we can switch cores
158 */
159static void mpc85xx_smp_flush_dcache_kexec(struct kimage *image)
160{
161 kimage_entry_t *ptr, entry;
162 unsigned long paddr;
163 int i;
164
165 if (image->type == KEXEC_TYPE_DEFAULT) {
166 /* normal kexec images are stored in temporary pages */
167 for (ptr = &image->head; (entry = *ptr) && !(entry & IND_DONE);
168 ptr = (entry & IND_INDIRECTION) ?
169 phys_to_virt(entry & PAGE_MASK) : ptr + 1) {
170 if (!(entry & IND_DESTINATION)) {
171 map_and_flush(entry);
172 }
173 }
174 /* flush out last IND_DONE page */
175 map_and_flush(entry);
176 } else {
177 /* crash type kexec images are copied to the crash region */
178 for (i = 0; i < image->nr_segments; i++) {
179 struct kexec_segment *seg = &image->segment[i];
180 for (paddr = seg->mem; paddr < seg->mem + seg->memsz;
181 paddr += PAGE_SIZE) {
182 map_and_flush(paddr);
183 }
184 }
185 }
186
187 /* also flush the kimage struct to be passed in as well */
188 flush_dcache_range((unsigned long)image,
189 (unsigned long)image + sizeof(*image));
190}
191
Matthew McClintockf933a412010-07-21 16:14:53 -0500192static void mpc85xx_smp_machine_kexec(struct kimage *image)
193{
Matthew McClintock5d692962010-09-16 17:58:25 -0500194 int timeout = INT_MAX;
195 int i, num_cpus = num_present_cpus();
Matthew McClintockf933a412010-07-21 16:14:53 -0500196
Matthew McClintock677de422010-09-16 17:58:26 -0500197 mpc85xx_smp_flush_dcache_kexec(image);
Matthew McClintockf933a412010-07-21 16:14:53 -0500198
Matthew McClintock5d692962010-09-16 17:58:25 -0500199 if (image->type == KEXEC_TYPE_DEFAULT)
200 smp_call_function(mpc85xx_smp_kexec_down, NULL, 0);
Matthew McClintockf933a412010-07-21 16:14:53 -0500201
Matthew McClintock5d692962010-09-16 17:58:25 -0500202 while ( (atomic_read(&kexec_down_cpus) != (num_cpus - 1)) &&
Matthew McClintockf933a412010-07-21 16:14:53 -0500203 ( timeout > 0 ) )
204 {
205 timeout--;
206 }
207
208 if ( !timeout )
209 printk(KERN_ERR "Unable to bring down secondary cpu(s)");
210
Matthew McClintock43a327b2011-10-25 17:54:04 -0500211 for_each_online_cpu(i)
Matthew McClintockf933a412010-07-21 16:14:53 -0500212 {
213 if ( i == smp_processor_id() ) continue;
214 mpic_reset_core(i);
215 }
216
217 default_machine_kexec(image);
218}
219#endif /* CONFIG_KEXEC */
220
Scott Wooddc2c9c52010-08-26 02:49:07 -0500221static void __init
222smp_85xx_setup_cpu(int cpu_nr)
223{
224 if (smp_85xx_ops.probe == smp_mpic_probe)
225 mpic_setup_this_cpu();
226
227 if (cpu_has_feature(CPU_FTR_DBELL))
228 doorbell_setup_this_cpu();
229}
230
Kumar Gala563fdd42009-02-11 22:50:42 -0600231void __init mpc85xx_smp_init(void)
232{
233 struct device_node *np;
234
Scott Wooddc2c9c52010-08-26 02:49:07 -0500235 smp_85xx_ops.setup_cpu = smp_85xx_setup_cpu;
236
Kumar Gala563fdd42009-02-11 22:50:42 -0600237 np = of_find_node_by_type(NULL, "open-pic");
238 if (np) {
239 smp_85xx_ops.probe = smp_mpic_probe;
Kumar Gala563fdd42009-02-11 22:50:42 -0600240 smp_85xx_ops.message_pass = smp_mpic_message_pass;
Kumar Gala563fdd42009-02-11 22:50:42 -0600241 }
242
Milton Miller23d72bf2011-05-10 19:29:39 +0000243 if (cpu_has_feature(CPU_FTR_DBELL)) {
Laurentiu TUDOR2647aa12011-07-07 16:44:30 +0300244 /*
245 * If left NULL, .message_pass defaults to
246 * smp_muxed_ipi_message_pass
247 */
Matthew McClintockde423ff2011-10-11 19:06:42 -0500248 smp_85xx_ops.message_pass = NULL;
Milton Miller23d72bf2011-05-10 19:29:39 +0000249 smp_85xx_ops.cause_ipi = doorbell_cause_ipi;
250 }
Kumar Gala563fdd42009-02-11 22:50:42 -0600251
Kumar Galad5b26db2008-11-19 09:35:56 -0600252 smp_ops = &smp_85xx_ops;
Matthew McClintockf933a412010-07-21 16:14:53 -0500253
254#ifdef CONFIG_KEXEC
255 ppc_md.kexec_cpu_down = mpc85xx_smp_kexec_cpu_down;
256 ppc_md.machine_kexec = mpc85xx_smp_machine_kexec;
257#endif
Kumar Galad5b26db2008-11-19 09:35:56 -0600258}