df428caf749a187b417cbfa6f67b2a1223e19aa2
[openwrt.git] / target / linux / cns3xxx / patches-3.0 / 049-cns3xxx_smp_support.patch
1 --- a/arch/arm/mach-cns3xxx/Makefile
2 +++ b/arch/arm/mach-cns3xxx/Makefile
3 @@ -1,3 +1,6 @@
4  obj-$(CONFIG_ARCH_CNS3XXX)             += core.o pm.o devices.o
5  obj-$(CONFIG_PCI)                      += pcie.o
6  obj-$(CONFIG_MACH_CNS3420VB)           += cns3420vb.o
7 +obj-$(CONFIG_SMP)                      += platsmp.o headsmp.o
8 +obj-$(CONFIG_HOTPLUG_CPU)              += hotplug.o
9 +obj-$(CONFIG_LOCAL_TIMERS)             += localtimer.o
10 --- /dev/null
11 +++ b/arch/arm/mach-cns3xxx/headsmp.S
12 @@ -0,0 +1,42 @@
13 +/*
14 + *  linux/arch/arm/mach-cns3xxx/headsmp.S
15 + *
16 + *  Cloned from linux/arch/arm/plat-versatile/headsmp.S
17 + *
18 + *  Copyright (c) 2003 ARM Limited
19 + *  All Rights Reserved
20 + *
21 + * This program is free software; you can redistribute it and/or modify
22 + * it under the terms of the GNU General Public License version 2 as
23 + * published by the Free Software Foundation.
24 + */
25 +#include <linux/linkage.h>
26 +#include <linux/init.h>
27 +
28 +       __INIT
29 +
30 +/*
31 + * CNS3XXX specific entry point for secondary CPUs.  This provides
32 + * a "holding pen" into which all secondary cores are held until we're
33 + * ready for them to initialise.
34 + */
35 +ENTRY(cns3xxx_secondary_startup)
36 +       mrc     p15, 0, r0, c0, c0, 5
37 +       and     r0, r0, #15
38 +       adr     r4, 1f
39 +       ldmia   r4, {r5, r6}
40 +       sub     r4, r4, r5
41 +       add     r6, r6, r4
42 +pen:   ldr     r7, [r6]
43 +       cmp     r7, r0
44 +       bne     pen
45 +
46 +       /*
47 +        * we've been released from the holding pen: secondary_stack
48 +        * should now contain the SVC stack for this core
49 +        */
50 +       b       secondary_startup
51 +
52 +       .align
53 +1:     .long   .
54 +       .long   pen_release
55 --- /dev/null
56 +++ b/arch/arm/mach-cns3xxx/hotplug.c
57 @@ -0,0 +1,130 @@
58 +/* linux arch/arm/mach-cns3xxx/hotplug.c
59 + *
60 + *  Cloned from linux/arch/arm/mach-realview/hotplug.c
61 + *
62 + *  Copyright (C) 2002 ARM Ltd.
63 + *  All Rights Reserved
64 + *
65 + * This program is free software; you can redistribute it and/or modify
66 + * it under the terms of the GNU General Public License version 2 as
67 + * published by the Free Software Foundation.
68 +*/
69 +
70 +#include <linux/kernel.h>
71 +#include <linux/errno.h>
72 +#include <linux/smp.h>
73 +
74 +#include <asm/cacheflush.h>
75 +
76 +extern volatile int pen_release;
77 +
78 +static inline void cpu_enter_lowpower(void)
79 +{
80 +       unsigned int v;
81 +
82 +       flush_cache_all();
83 +       asm volatile(
84 +       "       mcr     p15, 0, %1, c7, c5, 0\n"
85 +       "       mcr     p15, 0, %1, c7, c10, 4\n"
86 +       /*
87 +        * Turn off coherency
88 +        */
89 +       "       mrc     p15, 0, %0, c1, c0, 1\n"
90 +       "       bic     %0, %0, %3\n"
91 +       "       mcr     p15, 0, %0, c1, c0, 1\n"
92 +       "       mrc     p15, 0, %0, c1, c0, 0\n"
93 +       "       bic     %0, %0, %2\n"
94 +       "       mcr     p15, 0, %0, c1, c0, 0\n"
95 +         : "=&r" (v)
96 +         : "r" (0), "Ir" (CR_C), "Ir" (0x40)
97 +         : "cc");
98 +}
99 +
100 +static inline void cpu_leave_lowpower(void)
101 +{
102 +       unsigned int v;
103 +
104 +       asm volatile(
105 +       "mrc    p15, 0, %0, c1, c0, 0\n"
106 +       "       orr     %0, %0, %1\n"
107 +       "       mcr     p15, 0, %0, c1, c0, 0\n"
108 +       "       mrc     p15, 0, %0, c1, c0, 1\n"
109 +       "       orr     %0, %0, %2\n"
110 +       "       mcr     p15, 0, %0, c1, c0, 1\n"
111 +         : "=&r" (v)
112 +         : "Ir" (CR_C), "Ir" (0x40)
113 +         : "cc");
114 +}
115 +
116 +static inline void platform_do_lowpower(unsigned int cpu, int *spurious)
117 +{
118 +       /*
119 +        * there is no power-control hardware on this platform, so all
120 +        * we can do is put the core into WFI; this is safe as the calling
121 +        * code will have already disabled interrupts
122 +        */
123 +       for (;;) {
124 +               /*
125 +                * here's the WFI
126 +                */
127 +               asm(".word      0xe320f003\n"
128 +                   :
129 +                   :
130 +                   : "memory", "cc");
131 +
132 +               if (pen_release == cpu) {
133 +                       /*
134 +                        * OK, proper wakeup, we're done
135 +                        */
136 +                       break;
137 +               }
138 +
139 +               /*
140 +                * Getting here, means that we have come out of WFI without
141 +                * having been woken up - this shouldn't happen
142 +                *
143 +                * Just note it happening - when we're woken, we can report
144 +                * its occurrence.
145 +                */
146 +               (*spurious)++;
147 +       }
148 +}
149 +
150 +int platform_cpu_kill(unsigned int cpu)
151 +{
152 +       return 1;
153 +}
154 +
155 +/*
156 + * platform-specific code to shutdown a CPU
157 + *
158 + * Called with IRQs disabled
159 + */
160 +void platform_cpu_die(unsigned int cpu)
161 +{
162 +       int spurious = 0;
163 +
164 +       /*
165 +        * we're ready for shutdown now, so do it
166 +        */
167 +       cpu_enter_lowpower();
168 +       platform_do_lowpower(cpu, &spurious);
169 +
170 +       /*
171 +        * bring this CPU back into the world of cache
172 +        * coherency, and then restore interrupts
173 +        */
174 +       cpu_leave_lowpower();
175 +
176 +       if (spurious)
177 +               pr_warn("CPU%u: %u spurious wakeup calls\n", cpu, spurious);
178 +}
179 +
180 +int platform_cpu_disable(unsigned int cpu)
181 +{
182 +       /*
183 +        * we don't allow CPU 0 to be shutdown (it is still too special
184 +        * e.g. clock tick interrupts)
185 +        */
186 +       return cpu == 0 ? -EPERM : 0;
187 +}
188 --- /dev/null
189 +++ b/arch/arm/mach-cns3xxx/localtimer.c
190 @@ -0,0 +1,26 @@
191 +/* linux/arch/arm/mach-cns3xxx/localtimer.c
192 + *
193 + * Cloned from linux/arch/arm/mach-realview/localtimer.c
194 + *
195 + *  Copyright (C) 2002 ARM Ltd.
196 + *  All Rights Reserved
197 + *
198 + * This program is free software; you can redistribute it and/or modify
199 + * it under the terms of the GNU General Public License version 2 as
200 + * published by the Free Software Foundation.
201 +*/
202 +
203 +#include <linux/clockchips.h>
204 +
205 +#include <asm/irq.h>
206 +#include <asm/localtimer.h>
207 +
208 +/*
209 + * Setup the local clock events for a CPU.
210 + */
211 +int __cpuinit local_timer_setup(struct clock_event_device *evt)
212 +{
213 +       evt->irq = IRQ_LOCALTIMER;
214 +       twd_timer_setup(evt);
215 +       return 0;
216 +}
217 --- /dev/null
218 +++ b/arch/arm/mach-cns3xxx/platsmp.c
219 @@ -0,0 +1,175 @@
220 +/* linux/arch/arm/mach-cns3xxx/platsmp.c
221 + *
222 + * Copyright 2011 Gateworks Corporation
223 + *                Chris Lang <clang@gateworks.com>
224 + * Cloned from linux/arch/arm/mach-vexpress/platsmp.c
225 + *
226 + *  Copyright (C) 2002 ARM Ltd.
227 + *  All Rights Reserved
228 + *
229 + * This program is free software; you can redistribute it and/or modify
230 + * it under the terms of the GNU General Public License version 2 as
231 + * published by the Free Software Foundation.
232 +*/
233 +
234 +#include <linux/init.h>
235 +#include <linux/errno.h>
236 +#include <linux/delay.h>
237 +#include <linux/device.h>
238 +#include <linux/jiffies.h>
239 +#include <linux/smp.h>
240 +#include <linux/io.h>
241 +
242 +#include <asm/cacheflush.h>
243 +#include <asm/hardware/gic.h>
244 +#include <asm/smp_scu.h>
245 +#include <asm/unified.h>
246 +
247 +#include <mach/hardware.h>
248 +#include <mach/cns3xxx.h>
249 +
250 +extern void cns3xxx_secondary_startup(void);
251 +
252 +/*
253 + * control for which core is the next to come out of the secondary
254 + * boot "holding pen"
255 + */
256 +
257 +volatile int __cpuinitdata pen_release = -1;
258 +
259 +/*
260 + * Write pen_release in a way that is guaranteed to be visible to all
261 + * observers, irrespective of whether they're taking part in coherency
262 + * or not.  This is necessary for the hotplug code to work reliably.
263 + */
264 +static void write_pen_release(int val)
265 +{
266 +       pen_release = val;
267 +       smp_wmb();
268 +       __cpuc_flush_dcache_area((void *)&pen_release, sizeof(pen_release));
269 +       outer_clean_range(__pa(&pen_release), __pa(&pen_release + 1));
270 +}
271 +
272 +static void __iomem *scu_base_addr(void)
273 +{
274 +       return (void __iomem *)(CNS3XXX_TC11MP_SCU_BASE_VIRT);
275 +}
276 +
277 +static DEFINE_SPINLOCK(boot_lock);
278 +
279 +void __cpuinit platform_secondary_init(unsigned int cpu)
280 +{
281 +       /*
282 +        * if any interrupts are already enabled for the primary
283 +        * core (e.g. timer irq), then they will not have been enabled
284 +        * for us: do so
285 +        */
286 +       gic_secondary_init(0);
287 +
288 +       /*
289 +        * let the primary processor know we're out of the
290 +        * pen, then head off into the C entry point
291 +        */
292 +       write_pen_release(-1);
293 +
294 +       /*
295 +        * Synchronise with the boot thread.
296 +        */
297 +       spin_lock(&boot_lock);
298 +       spin_unlock(&boot_lock);
299 +}
300 +
301 +int __cpuinit boot_secondary(unsigned int cpu, struct task_struct *idle)
302 +{
303 +       unsigned long timeout;
304 +
305 +       /*
306 +        * Set synchronisation state between this boot processor
307 +        * and the secondary one
308 +        */
309 +       spin_lock(&boot_lock);
310 +
311 +       /*
312 +        * The secondary processor is waiting to be released from
313 +        * the holding pen - release it, then wait for it to flag
314 +        * that it has been released by resetting pen_release.
315 +        *
316 +        * Note that "pen_release" is the hardware CPU ID, whereas
317 +        * "cpu" is Linux's internal ID.
318 +        */
319 +       write_pen_release(cpu);
320 +
321 +       /*
322 +        * Send the secondary CPU a soft interrupt, thereby causing
323 +        * the boot monitor to read the system wide flags register,
324 +        * and branch to the address found there.
325 +        */
326 +       gic_raise_softirq(cpumask_of(cpu), 1);
327 +
328 +       timeout = jiffies + (1 * HZ);
329 +       while (time_before(jiffies, timeout)) {
330 +               smp_rmb();
331 +               if (pen_release == -1)
332 +                       break;
333 +
334 +               udelay(10);
335 +       }
336 +
337 +       /*
338 +        * now the secondary core is starting up let it run its
339 +        * calibrations, then wait for it to finish
340 +        */
341 +       spin_unlock(&boot_lock);
342 +
343 +       return pen_release != -1 ? -ENOSYS : 0;
344 +}
345 +
346 +/*
347 + * Initialise the CPU possible map early - this describes the CPUs
348 + * which may be present or become present in the system.
349 + */
350 +
351 +void __init smp_init_cpus(void)
352 +{
353 +       void __iomem *scu_base = scu_base_addr();
354 +       unsigned int i, ncores;
355 +
356 +       ncores = scu_base ? scu_get_core_count(scu_base) : 1;
357 +
358 +       /* sanity check */
359 +       if (ncores > NR_CPUS) {
360 +               printk(KERN_WARNING
361 +                      "cns3xxx: no. of cores (%d) greater than configured "
362 +                      "maximum of %d - clipping\n",
363 +                      ncores, NR_CPUS);
364 +               ncores = NR_CPUS;
365 +       }
366 +
367 +       for (i = 0; i < ncores; i++)
368 +               set_cpu_possible(i, true);
369 +
370 +       set_smp_cross_call(gic_raise_softirq);
371 +}
372 +
373 +void __init platform_smp_prepare_cpus(unsigned int max_cpus)
374 +{
375 +       int i;
376 +
377 +       /*
378 +        * Initialise the present map, which describes the set of CPUs
379 +        * actually populated at the present time.
380 +        */
381 +       for (i = 0; i < max_cpus; i++)
382 +               set_cpu_present(i, true);
383 +
384 +       scu_enable(scu_base_addr());
385 +
386 +       /*
387 +        * Write the address of secondary startup into the
388 +        * system-wide flags register. The boot monitor waits
389 +        * until it receives a soft interrupt, and then the
390 +        * secondary CPU branches to this address.
391 +        */
392 +       __raw_writel(virt_to_phys(cns3xxx_secondary_startup),
393 +                       (void __iomem *)(CNS3XXX_MISC_BASE_VIRT + 0x0600));
394 +}
395 --- a/arch/arm/Kconfig
396 +++ b/arch/arm/Kconfig
397 @@ -1312,7 +1312,7 @@ config SMP
398         depends on REALVIEW_EB_ARM11MP || REALVIEW_EB_A9MP || \
399                  MACH_REALVIEW_PB11MP || MACH_REALVIEW_PBX || ARCH_OMAP4 || \
400                  ARCH_EXYNOS4 || ARCH_TEGRA || ARCH_U8500 || ARCH_VEXPRESS_CA9X4 || \
401 -                ARCH_MSM_SCORPIONMP || ARCH_SHMOBILE
402 +                ARCH_MSM_SCORPIONMP || ARCH_SHMOBILE || ARCH_CNS3XXX
403         select USE_GENERIC_SMP_HELPERS
404         select HAVE_ARM_SCU if !ARCH_MSM_SCORPIONMP
405         help