powerpc/85xx: implement hardware timebase sync
[firefly-linux-kernel-4.4.55.git] / arch / powerpc / platforms / 85xx / smp.c
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
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>
18 #include <linux/kexec.h>
19 #include <linux/highmem.h>
20 #include <linux/cpu.h>
21
22 #include <asm/machdep.h>
23 #include <asm/pgtable.h>
24 #include <asm/page.h>
25 #include <asm/mpic.h>
26 #include <asm/cacheflush.h>
27 #include <asm/dbell.h>
28 #include <asm/fsl_guts.h>
29
30 #include <sysdev/fsl_soc.h>
31 #include <sysdev/mpic.h>
32 #include "smp.h"
33
34 extern void __early_start(void);
35
36 struct epapr_spin_table {
37         u32     addr_h;
38         u32     addr_l;
39         u32     r3_h;
40         u32     r3_l;
41         u32     reserved;
42         u32     pir;
43 };
44
45 static struct ccsr_guts __iomem *guts;
46 static u64 timebase;
47 static int tb_req;
48 static int tb_valid;
49
50 static void mpc85xx_timebase_freeze(int freeze)
51 {
52         uint32_t mask;
53
54         mask = CCSR_GUTS_DEVDISR_TB0 | CCSR_GUTS_DEVDISR_TB1;
55         if (freeze)
56                 setbits32(&guts->devdisr, mask);
57         else
58                 clrbits32(&guts->devdisr, mask);
59
60         in_be32(&guts->devdisr);
61 }
62
63 static void mpc85xx_give_timebase(void)
64 {
65         unsigned long flags;
66
67         local_irq_save(flags);
68
69         while (!tb_req)
70                 barrier();
71         tb_req = 0;
72
73         mpc85xx_timebase_freeze(1);
74         timebase = get_tb();
75         mb();
76         tb_valid = 1;
77
78         while (tb_valid)
79                 barrier();
80
81         mpc85xx_timebase_freeze(0);
82
83         local_irq_restore(flags);
84 }
85
86 static void mpc85xx_take_timebase(void)
87 {
88         unsigned long flags;
89
90         local_irq_save(flags);
91
92         tb_req = 1;
93         while (!tb_valid)
94                 barrier();
95
96         set_tb(timebase >> 32, timebase & 0xffffffff);
97         isync();
98         tb_valid = 0;
99
100         local_irq_restore(flags);
101 }
102
103 static int __init
104 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 n = 0, hw_cpu = get_hard_smp_processor_id(nr);
111         int ioremappable;
112
113         WARN_ON(nr < 0 || nr >= NR_CPUS);
114         WARN_ON(hw_cpu < 0 || hw_cpu >= NR_CPUS);
115
116         pr_debug("smp_85xx_kick_cpu: kick CPU #%d\n", nr);
117
118         np = of_get_cpu_node(nr, NULL);
119         cpu_rel_addr = of_get_property(np, "cpu-release-addr", NULL);
120
121         if (cpu_rel_addr == NULL) {
122                 printk(KERN_ERR "No cpu-release-addr for cpu %d\n", nr);
123                 return -ENOENT;
124         }
125
126         /*
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.
131          */
132         ioremappable = *cpu_rel_addr > virt_to_phys(high_memory);
133
134         /* Map the spin table */
135         if (ioremappable)
136                 spin_table = ioremap(*cpu_rel_addr,
137                                 sizeof(struct epapr_spin_table));
138         else
139                 spin_table = phys_to_virt(*cpu_rel_addr);
140
141         local_irq_save(flags);
142
143         out_be32(&spin_table->pir, hw_cpu);
144 #ifdef CONFIG_PPC32
145         out_be32(&spin_table->addr_l, __pa(__early_start));
146
147         if (!ioremappable)
148                 flush_dcache_range((ulong)spin_table,
149                         (ulong)spin_table + sizeof(struct epapr_spin_table));
150
151         /* Wait a bit for the CPU to ack. */
152         while ((__secondary_hold_acknowledge != hw_cpu) && (++n < 1000))
153                 mdelay(1);
154 #else
155         smp_generic_kick_cpu(nr);
156
157         out_be64((u64 *)(&spin_table->addr_h),
158           __pa((u64)*((unsigned long long *)generic_secondary_smp_init)));
159
160         if (!ioremappable)
161                 flush_dcache_range((ulong)spin_table,
162                         (ulong)spin_table + sizeof(struct epapr_spin_table));
163 #endif
164
165         local_irq_restore(flags);
166
167         if (ioremappable)
168                 iounmap(spin_table);
169
170         pr_debug("waited %d msecs for CPU #%d.\n", n, nr);
171
172         return 0;
173 }
174
175 struct smp_ops_t smp_85xx_ops = {
176         .kick_cpu = smp_85xx_kick_cpu,
177 #ifdef CONFIG_KEXEC
178         .give_timebase  = smp_generic_give_timebase,
179         .take_timebase  = smp_generic_take_timebase,
180 #endif
181 };
182
183 #ifdef CONFIG_KEXEC
184 atomic_t kexec_down_cpus = ATOMIC_INIT(0);
185
186 void mpc85xx_smp_kexec_cpu_down(int crash_shutdown, int secondary)
187 {
188         local_irq_disable();
189
190         if (secondary) {
191                 atomic_inc(&kexec_down_cpus);
192                 /* loop forever */
193                 while (1);
194         }
195 }
196
197 static void mpc85xx_smp_kexec_down(void *arg)
198 {
199         if (ppc_md.kexec_cpu_down)
200                 ppc_md.kexec_cpu_down(0,1);
201 }
202
203 static void map_and_flush(unsigned long paddr)
204 {
205         struct page *page = pfn_to_page(paddr >> PAGE_SHIFT);
206         unsigned long kaddr  = (unsigned long)kmap(page);
207
208         flush_dcache_range(kaddr, kaddr + PAGE_SIZE);
209         kunmap(page);
210 }
211
212 /**
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
217  */
218 static void mpc85xx_smp_flush_dcache_kexec(struct kimage *image)
219 {
220         kimage_entry_t *ptr, entry;
221         unsigned long paddr;
222         int i;
223
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);
231                         }
232                 }
233                 /* flush out last IND_DONE page */
234                 map_and_flush(entry);
235         } else {
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);
242                         }
243                 }
244         }
245
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));
249 }
250
251 static void mpc85xx_smp_machine_kexec(struct kimage *image)
252 {
253         int timeout = INT_MAX;
254         int i, num_cpus = num_present_cpus();
255
256         mpc85xx_smp_flush_dcache_kexec(image);
257
258         if (image->type == KEXEC_TYPE_DEFAULT)
259                 smp_call_function(mpc85xx_smp_kexec_down, NULL, 0);
260
261         while ( (atomic_read(&kexec_down_cpus) != (num_cpus - 1)) &&
262                 ( timeout > 0 ) )
263         {
264                 timeout--;
265         }
266
267         if ( !timeout )
268                 printk(KERN_ERR "Unable to bring down secondary cpu(s)");
269
270         for_each_online_cpu(i)
271         {
272                 if ( i == smp_processor_id() ) continue;
273                 mpic_reset_core(i);
274         }
275
276         default_machine_kexec(image);
277 }
278 #endif /* CONFIG_KEXEC */
279
280 static void __init
281 smp_85xx_setup_cpu(int cpu_nr)
282 {
283         if (smp_85xx_ops.probe == smp_mpic_probe)
284                 mpic_setup_this_cpu();
285
286         if (cpu_has_feature(CPU_FTR_DBELL))
287                 doorbell_setup_this_cpu();
288 }
289
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", },
297         {},
298 };
299
300 void __init mpc85xx_smp_init(void)
301 {
302         struct device_node *np;
303
304         smp_85xx_ops.setup_cpu = smp_85xx_setup_cpu;
305
306         np = of_find_node_by_type(NULL, "open-pic");
307         if (np) {
308                 smp_85xx_ops.probe = smp_mpic_probe;
309                 smp_85xx_ops.message_pass = smp_mpic_message_pass;
310         }
311
312         if (cpu_has_feature(CPU_FTR_DBELL)) {
313                 /*
314                  * If left NULL, .message_pass defaults to
315                  * smp_muxed_ipi_message_pass
316                  */
317                 smp_85xx_ops.message_pass = NULL;
318                 smp_85xx_ops.cause_ipi = doorbell_cause_ipi;
319         }
320
321         np = of_find_matching_node(NULL, mpc85xx_smp_guts_ids);
322         if (np) {
323                 guts = of_iomap(np, 0);
324                 of_node_put(np);
325                 if (!guts) {
326                         pr_err("%s: Could not map guts node address\n",
327                                                                 __func__);
328                         return;
329                 }
330                 smp_85xx_ops.give_timebase = mpc85xx_give_timebase;
331                 smp_85xx_ops.take_timebase = mpc85xx_take_timebase;
332         }
333
334         smp_ops = &smp_85xx_ops;
335
336 #ifdef CONFIG_KEXEC
337         ppc_md.kexec_cpu_down = mpc85xx_smp_kexec_cpu_down;
338         ppc_md.machine_kexec = mpc85xx_smp_machine_kexec;
339 #endif
340 }