Merge branch for-rmk-devel of git://aeryn.fluff.org.uk/bjdooks/linux into devel
[sfrench/cifs-2.6.git] / arch / arm / mach-realview / platsmp.c
1 /*
2  *  linux/arch/arm/mach-realview/platsmp.c
3  *
4  *  Copyright (C) 2002 ARM Ltd.
5  *  All Rights Reserved
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  */
11 #include <linux/init.h>
12 #include <linux/errno.h>
13 #include <linux/delay.h>
14 #include <linux/device.h>
15 #include <linux/jiffies.h>
16 #include <linux/smp.h>
17 #include <linux/io.h>
18
19 #include <asm/cacheflush.h>
20 #include <mach/hardware.h>
21 #include <asm/mach-types.h>
22 #include <asm/localtimer.h>
23
24 #include <mach/board-eb.h>
25 #include <mach/board-pb11mp.h>
26 #include <asm/smp_scu.h>
27
28 #include "core.h"
29
30 extern void realview_secondary_startup(void);
31
32 /*
33  * control for which core is the next to come out of the secondary
34  * boot "holding pen"
35  */
36 volatile int __cpuinitdata pen_release = -1;
37
38 static void __iomem *scu_base_addr(void)
39 {
40         if (machine_is_realview_eb_mp())
41                 return __io_address(REALVIEW_EB11MP_SCU_BASE);
42         else if (machine_is_realview_pb11mp())
43                 return __io_address(REALVIEW_TC11MP_SCU_BASE);
44         else
45                 return (void __iomem *)0;
46 }
47
48 static inline unsigned int get_core_count(void)
49 {
50         void __iomem *scu_base = scu_base_addr();
51         if (scu_base)
52                 return scu_get_core_count(scu_base);
53         return 1;
54 }
55
56 static DEFINE_SPINLOCK(boot_lock);
57
58 void __cpuinit platform_secondary_init(unsigned int cpu)
59 {
60         trace_hardirqs_off();
61
62         /*
63          * if any interrupts are already enabled for the primary
64          * core (e.g. timer irq), then they will not have been enabled
65          * for us: do so
66          */
67         gic_cpu_init(0, gic_cpu_base_addr);
68
69         /*
70          * let the primary processor know we're out of the
71          * pen, then head off into the C entry point
72          */
73         pen_release = -1;
74         smp_wmb();
75
76         /*
77          * Synchronise with the boot thread.
78          */
79         spin_lock(&boot_lock);
80         spin_unlock(&boot_lock);
81 }
82
83 int __cpuinit boot_secondary(unsigned int cpu, struct task_struct *idle)
84 {
85         unsigned long timeout;
86
87         /*
88          * set synchronisation state between this boot processor
89          * and the secondary one
90          */
91         spin_lock(&boot_lock);
92
93         /*
94          * The secondary processor is waiting to be released from
95          * the holding pen - release it, then wait for it to flag
96          * that it has been released by resetting pen_release.
97          *
98          * Note that "pen_release" is the hardware CPU ID, whereas
99          * "cpu" is Linux's internal ID.
100          */
101         pen_release = cpu;
102         flush_cache_all();
103
104         /*
105          * XXX
106          *
107          * This is a later addition to the booting protocol: the
108          * bootMonitor now puts secondary cores into WFI, so
109          * poke_milo() no longer gets the cores moving; we need
110          * to send a soft interrupt to wake the secondary core.
111          * Use smp_cross_call() for this, since there's little
112          * point duplicating the code here
113          */
114         smp_cross_call(cpumask_of(cpu));
115
116         timeout = jiffies + (1 * HZ);
117         while (time_before(jiffies, timeout)) {
118                 smp_rmb();
119                 if (pen_release == -1)
120                         break;
121
122                 udelay(10);
123         }
124
125         /*
126          * now the secondary core is starting up let it run its
127          * calibrations, then wait for it to finish
128          */
129         spin_unlock(&boot_lock);
130
131         return pen_release != -1 ? -ENOSYS : 0;
132 }
133
134 static void __init poke_milo(void)
135 {
136         extern void secondary_startup(void);
137
138         /* nobody is to be released from the pen yet */
139         pen_release = -1;
140
141         /*
142          * write the address of secondary startup into the system-wide
143          * flags register, then clear the bottom two bits, which is what
144          * BootMonitor is waiting for
145          */
146 #if 1
147 #define REALVIEW_SYS_FLAGSS_OFFSET 0x30
148         __raw_writel(virt_to_phys(realview_secondary_startup),
149                      __io_address(REALVIEW_SYS_BASE) +
150                      REALVIEW_SYS_FLAGSS_OFFSET);
151 #define REALVIEW_SYS_FLAGSC_OFFSET 0x34
152         __raw_writel(3,
153                      __io_address(REALVIEW_SYS_BASE) +
154                      REALVIEW_SYS_FLAGSC_OFFSET);
155 #endif
156
157         mb();
158 }
159
160 /*
161  * Initialise the CPU possible map early - this describes the CPUs
162  * which may be present or become present in the system.
163  */
164 void __init smp_init_cpus(void)
165 {
166         unsigned int i, ncores = get_core_count();
167
168         for (i = 0; i < ncores; i++)
169                 set_cpu_possible(i, true);
170 }
171
172 void __init smp_prepare_cpus(unsigned int max_cpus)
173 {
174         unsigned int ncores = get_core_count();
175         unsigned int cpu = smp_processor_id();
176         int i;
177
178         /* sanity check */
179         if (ncores == 0) {
180                 printk(KERN_ERR
181                        "Realview: strange CM count of 0? Default to 1\n");
182
183                 ncores = 1;
184         }
185
186         if (ncores > NR_CPUS) {
187                 printk(KERN_WARNING
188                        "Realview: no. of cores (%d) greater than configured "
189                        "maximum of %d - clipping\n",
190                        ncores, NR_CPUS);
191                 ncores = NR_CPUS;
192         }
193
194         smp_store_cpu_info(cpu);
195
196         /*
197          * are we trying to boot more cores than exist?
198          */
199         if (max_cpus > ncores)
200                 max_cpus = ncores;
201
202         /*
203          * Initialise the present map, which describes the set of CPUs
204          * actually populated at the present time.
205          */
206         for (i = 0; i < max_cpus; i++)
207                 set_cpu_present(i, true);
208
209         /*
210          * Initialise the SCU if there are more than one CPU and let
211          * them know where to start. Note that, on modern versions of
212          * MILO, the "poke" doesn't actually do anything until each
213          * individual core is sent a soft interrupt to get it out of
214          * WFI
215          */
216         if (max_cpus > 1) {
217                 /*
218                  * Enable the local timer or broadcast device for the
219                  * boot CPU, but only if we have more than one CPU.
220                  */
221                 percpu_timer_setup();
222
223                 scu_enable(scu_base_addr());
224                 poke_milo();
225         }
226 }