2 * linux/arch/arm/mach-cns3xxx/platsmp.c
4 * Copyright (C) 2002 ARM Ltd.
5 * Copyright 2012 Gateworks Corporation
6 * Chris Lang <clang@gateworks.com>
7 * Tim Harvey <tharvey@gateworks.com>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
15 #include <linux/init.h>
16 #include <linux/errno.h>
17 #include <linux/delay.h>
18 #include <linux/device.h>
19 #include <linux/jiffies.h>
20 #include <linux/smp.h>
23 #include <asm/cacheflush.h>
24 #include <asm/hardware/gic.h>
25 #include <asm/smp_scu.h>
26 #include <asm/unified.h>
29 #include <mach/cns3xxx.h>
31 static struct fiq_handler fh = {
35 static unsigned int fiq_buffer[8];
37 #define FIQ_ENABLED 0x80000000
38 #define FIQ_GENERATE 0x00010000
39 #define CNS3XXX_MAP_AREA 0x01000000
40 #define CNS3XXX_UNMAP_AREA 0x02000000
41 #define CNS3XXX_FLUSH_RANGE 0x03000000
43 extern void cns3xxx_secondary_startup(void);
44 extern unsigned char cns3xxx_fiq_start, cns3xxx_fiq_end;
45 extern unsigned int fiq_number[2];
46 extern struct cpu_cache_fns cpu_cache;
47 struct cpu_cache_fns cpu_cache_save;
49 #define SCU_CPU_STATUS 0x08
50 static void __iomem *scu_base;
52 static void __init cns3xxx_set_fiq_regs(void)
54 struct pt_regs FIQ_regs;
55 unsigned int cpu = smp_processor_id();
58 FIQ_regs.ARM_ip = (unsigned int)&fiq_buffer[4];
59 FIQ_regs.ARM_sp = (unsigned int)MISC_FIQ_CPU(0);
61 FIQ_regs.ARM_ip = (unsigned int)&fiq_buffer[0];
62 FIQ_regs.ARM_sp = (unsigned int)MISC_FIQ_CPU(1);
64 set_fiq_regs(&FIQ_regs);
67 static void __init cns3xxx_init_fiq(void)
69 void *fiqhandler_start;
70 unsigned int fiqhandler_length;
73 fiqhandler_start = &cns3xxx_fiq_start;
74 fiqhandler_length = &cns3xxx_fiq_end - &cns3xxx_fiq_start;
82 set_fiq_handler(fiqhandler_start, fiqhandler_length);
83 fiq_buffer[0] = (unsigned int)&fiq_number[0];
85 fiq_buffer[4] = (unsigned int)&fiq_number[1];
91 * Write pen_release in a way that is guaranteed to be visible to all
92 * observers, irrespective of whether they're taking part in coherency
93 * or not. This is necessary for the hotplug code to work reliably.
95 static void __cpuinit write_pen_release(int val)
99 __cpuc_flush_dcache_area((void *)&pen_release, sizeof(pen_release));
100 outer_clean_range(__pa(&pen_release), __pa(&pen_release + 1));
103 static DEFINE_SPINLOCK(boot_lock);
105 static void __cpuinit cns3xxx_secondary_init(unsigned int cpu)
108 * if any interrupts are already enabled for the primary
109 * core (e.g. timer irq), then they will not have been enabled
112 gic_secondary_init(0);
115 * Setup Secondary Core FIQ regs
117 cns3xxx_set_fiq_regs();
120 * let the primary processor know we're out of the
121 * pen, then head off into the C entry point
123 write_pen_release(-1);
126 * Fixup DMA Operations
129 cpu_cache.dma_map_area = (void *)smp_dma_map_area;
130 cpu_cache.dma_unmap_area = (void *)smp_dma_unmap_area;
131 cpu_cache.dma_flush_range = (void *)smp_dma_flush_range;
134 * Synchronise with the boot thread.
136 spin_lock(&boot_lock);
137 spin_unlock(&boot_lock);
140 static int __cpuinit cns3xxx_boot_secondary(unsigned int cpu, struct task_struct *idle)
142 unsigned long timeout;
145 * Set synchronisation state between this boot processor
146 * and the secondary one
148 spin_lock(&boot_lock);
151 * The secondary processor is waiting to be released from
152 * the holding pen - release it, then wait for it to flag
153 * that it has been released by resetting pen_release.
155 * Note that "pen_release" is the hardware CPU ID, whereas
156 * "cpu" is Linux's internal ID.
158 write_pen_release(cpu);
161 * Send the secondary CPU a soft interrupt, thereby causing
162 * the boot monitor to read the system wide flags register,
163 * and branch to the address found there.
165 gic_raise_softirq(cpumask_of(cpu), 1);
167 timeout = jiffies + (1 * HZ);
168 while (time_before(jiffies, timeout)) {
170 if (pen_release == -1)
177 * now the secondary core is starting up let it run its
178 * calibrations, then wait for it to finish
180 spin_unlock(&boot_lock);
182 return pen_release != -1 ? -ENOSYS : 0;
186 * Initialise the CPU possible map early - this describes the CPUs
187 * which may be present or become present in the system.
189 static void __init cns3xxx_smp_init_cpus(void)
191 unsigned int i, ncores;
194 scu_base = (void __iomem *) CNS3XXX_TC11MP_SCU_BASE_VIRT;
196 /* for CNS3xxx SCU_CPU_STATUS must be examined instead of SCU_CONFIGURATION
197 * used in scu_get_core_count
199 status = __raw_readl(scu_base + SCU_CPU_STATUS);
200 for (i = 0; i < NR_CPUS+1; i++) {
201 if (((status >> (i*2)) & 0x3) == 0)
202 set_cpu_possible(i, true);
208 set_smp_cross_call(gic_raise_softirq);
211 static void __init cns3xxx_smp_prepare_cpus(unsigned int max_cpus)
216 * Initialise the present map, which describes the set of CPUs
217 * actually populated at the present time.
219 for (i = 0; i < max_cpus; i++) {
220 set_cpu_present(i, true);
226 scu_enable(scu_base);
229 * Write the address of secondary startup into the
230 * system-wide flags register. The boot monitor waits
231 * until it receives a soft interrupt, and then the
232 * secondary CPU branches to this address.
234 __raw_writel(virt_to_phys(cns3xxx_secondary_startup),
235 (void __iomem *)(CNS3XXX_MISC_BASE_VIRT + 0x0600));
238 * Setup FIQ's for main cpu
241 cns3xxx_set_fiq_regs();
242 memcpy((void *)&cpu_cache_save, (void *)&cpu_cache, sizeof(struct cpu_cache_fns));
246 static inline unsigned long cns3xxx_cpu_id(void)
251 " mrc p15, 0, %0, c0, c0, 5 @ cns3xxx_cpu_id\n"
252 : "=r" (cpu) : : "memory", "cc");
256 void smp_dma_map_area(const void *addr, size_t size, int dir)
260 raw_local_irq_save(flags);
261 cpu = cns3xxx_cpu_id();
263 fiq_buffer[1] = (unsigned int)addr;
264 fiq_buffer[2] = size;
265 fiq_buffer[3] = dir | CNS3XXX_MAP_AREA | FIQ_ENABLED;
267 __raw_writel(FIQ_GENERATE, MISC_FIQ_CPU(1));
269 cpu_cache_save.dma_map_area(addr, size, dir);
270 while ((fiq_buffer[3]) & FIQ_ENABLED) { barrier(); }
273 fiq_buffer[5] = (unsigned int)addr;
274 fiq_buffer[6] = size;
275 fiq_buffer[7] = dir | CNS3XXX_MAP_AREA | FIQ_ENABLED;
277 __raw_writel(FIQ_GENERATE, MISC_FIQ_CPU(0));
279 cpu_cache_save.dma_map_area(addr, size, dir);
280 while ((fiq_buffer[7]) & FIQ_ENABLED) { barrier(); }
282 raw_local_irq_restore(flags);
285 void smp_dma_unmap_area(const void *addr, size_t size, int dir)
290 raw_local_irq_save(flags);
291 cpu = cns3xxx_cpu_id();
294 fiq_buffer[1] = (unsigned int)addr;
295 fiq_buffer[2] = size;
296 fiq_buffer[3] = dir | CNS3XXX_UNMAP_AREA | FIQ_ENABLED;
298 __raw_writel(FIQ_GENERATE, MISC_FIQ_CPU(1));
300 cpu_cache_save.dma_unmap_area(addr, size, dir);
301 while ((fiq_buffer[3]) & FIQ_ENABLED) { barrier(); }
304 fiq_buffer[5] = (unsigned int)addr;
305 fiq_buffer[6] = size;
306 fiq_buffer[7] = dir | CNS3XXX_UNMAP_AREA | FIQ_ENABLED;
308 __raw_writel(FIQ_GENERATE, MISC_FIQ_CPU(0));
310 cpu_cache_save.dma_unmap_area(addr, size, dir);
311 while ((fiq_buffer[7]) & FIQ_ENABLED) { barrier(); }
313 raw_local_irq_restore(flags);
316 void smp_dma_flush_range(const void *start, const void *end)
320 raw_local_irq_save(flags);
321 cpu = cns3xxx_cpu_id();
324 fiq_buffer[1] = (unsigned int)start;
325 fiq_buffer[2] = (unsigned int)end;
326 fiq_buffer[3] = CNS3XXX_FLUSH_RANGE | FIQ_ENABLED;
328 __raw_writel(FIQ_GENERATE, MISC_FIQ_CPU(1));
330 cpu_cache_save.dma_flush_range(start, end);
331 while ((fiq_buffer[3]) & FIQ_ENABLED) { barrier(); }
334 fiq_buffer[5] = (unsigned int)start;
335 fiq_buffer[6] = (unsigned int)end;
336 fiq_buffer[7] = CNS3XXX_FLUSH_RANGE | FIQ_ENABLED;
338 __raw_writel(FIQ_GENERATE, MISC_FIQ_CPU(0));
340 cpu_cache_save.dma_flush_range(start, end);
341 while ((fiq_buffer[7]) & FIQ_ENABLED) { barrier(); }
343 raw_local_irq_restore(flags);
346 struct smp_operations cns3xxx_smp_ops __initdata = {
347 .smp_init_cpus = cns3xxx_smp_init_cpus,
348 .smp_prepare_cpus = cns3xxx_smp_prepare_cpus,
349 .smp_secondary_init = cns3xxx_secondary_init,
350 .smp_boot_secondary = cns3xxx_boot_secondary,