blob: 04a16edd540162ae3c596cd7ee2c5dc7cffe0cf5 [file] [log] [blame]
Jesper Nilsson538380d2008-01-25 16:15:44 +01001#include <linux/types.h>
Mikael Starvik51533b62005-07-27 11:44:44 -07002#include <asm/delay.h>
Jesper Nilsson538380d2008-01-25 16:15:44 +01003#include <irq.h>
4#include <hwregs/intr_vect.h>
5#include <hwregs/intr_vect_defs.h>
Mikael Starvik51533b62005-07-27 11:44:44 -07006#include <asm/tlbflush.h>
7#include <asm/mmu_context.h>
Jesper Nilsson538380d2008-01-25 16:15:44 +01008#include <hwregs/asm/mmu_defs_asm.h>
9#include <hwregs/supp_reg.h>
Arun Sharma600634972011-07-26 16:09:06 -070010#include <linux/atomic.h>
Mikael Starvik51533b62005-07-27 11:44:44 -070011
12#include <linux/err.h>
13#include <linux/init.h>
14#include <linux/timex.h>
15#include <linux/sched.h>
16#include <linux/kernel.h>
17#include <linux/cpumask.h>
18#include <linux/interrupt.h>
David S. Millerc8923c62005-10-13 14:41:23 -070019#include <linux/module.h>
Mikael Starvik51533b62005-07-27 11:44:44 -070020
21#define IPI_SCHEDULE 1
22#define IPI_CALL 2
23#define IPI_FLUSH_TLB 4
Jesper Nilsson538380d2008-01-25 16:15:44 +010024#define IPI_BOOT 8
Mikael Starvik51533b62005-07-27 11:44:44 -070025
26#define FLUSH_ALL (void*)0xffffffff
27
28/* Vector of locks used for various atomic operations */
Thomas Gleixnere41c8ab2011-01-23 15:14:15 +010029spinlock_t cris_atomic_locks[] = {
30 [0 ... LOCK_COUNT - 1] = __SPIN_LOCK_UNLOCKED(cris_atomic_locks)
31};
Mikael Starvik51533b62005-07-27 11:44:44 -070032
33/* CPU masks */
Mikael Starvik51533b62005-07-27 11:44:44 -070034cpumask_t phys_cpu_present_map = CPU_MASK_NONE;
David S. Millerc8923c62005-10-13 14:41:23 -070035EXPORT_SYMBOL(phys_cpu_present_map);
Mikael Starvik51533b62005-07-27 11:44:44 -070036
37/* Variables used during SMP boot */
38volatile int cpu_now_booting = 0;
39volatile struct thread_info *smp_init_current_idle_thread;
40
41/* Variables used during IPI */
42static DEFINE_SPINLOCK(call_lock);
43static DEFINE_SPINLOCK(tlbstate_lock);
44
45struct call_data_struct {
46 void (*func) (void *info);
47 void *info;
48 int wait;
49};
50
51static struct call_data_struct * call_data;
52
53static struct mm_struct* flush_mm;
54static struct vm_area_struct* flush_vma;
55static unsigned long flush_addr;
56
Mikael Starvik51533b62005-07-27 11:44:44 -070057/* Mode registers */
Jesper Nilsson538380d2008-01-25 16:15:44 +010058static unsigned long irq_regs[NR_CPUS] = {
Mikael Starvik51533b62005-07-27 11:44:44 -070059 regi_irq,
60 regi_irq2
61};
62
Jesper Nilsson538380d2008-01-25 16:15:44 +010063static irqreturn_t crisv32_ipi_interrupt(int irq, void *dev_id);
Mikael Starvik51533b62005-07-27 11:44:44 -070064static int send_ipi(int vector, int wait, cpumask_t cpu_mask);
Thomas Gleixnere5f71782007-10-16 01:26:38 -070065static struct irqaction irq_ipi = {
66 .handler = crisv32_ipi_interrupt,
67 .flags = IRQF_DISABLED,
Thomas Gleixnere5f71782007-10-16 01:26:38 -070068 .name = "ipi",
69};
Mikael Starvik51533b62005-07-27 11:44:44 -070070
71extern void cris_mmu_init(void);
72extern void cris_timer_init(void);
73
74/* SMP initialization */
75void __init smp_prepare_cpus(unsigned int max_cpus)
76{
77 int i;
78
79 /* From now on we can expect IPIs so set them up */
80 setup_irq(IPI_INTR_VECT, &irq_ipi);
81
82 /* Mark all possible CPUs as present */
83 for (i = 0; i < max_cpus; i++)
KOSAKI Motohiro8aebe212011-05-24 17:12:59 -070084 cpumask_set_cpu(i, &phys_cpu_present_map);
Mikael Starvik51533b62005-07-27 11:44:44 -070085}
86
Greg Kroah-Hartmanb881bc42012-12-21 14:06:37 -080087void smp_prepare_boot_cpu(void)
Mikael Starvik51533b62005-07-27 11:44:44 -070088{
89 /* PGD pointer has moved after per_cpu initialization so
90 * update the MMU.
91 */
92 pgd_t **pgd;
93 pgd = (pgd_t**)&per_cpu(current_pgd, smp_processor_id());
94
95 SUPP_BANK_SEL(1);
96 SUPP_REG_WR(RW_MM_TLB_PGD, pgd);
97 SUPP_BANK_SEL(2);
98 SUPP_REG_WR(RW_MM_TLB_PGD, pgd);
99
Rusty Russellafc6ca02009-03-16 14:11:47 +1030100 set_cpu_online(0, true);
KOSAKI Motohiro8aebe212011-05-24 17:12:59 -0700101 cpumask_set_cpu(0, &phys_cpu_present_map);
Rusty Russellafc6ca02009-03-16 14:11:47 +1030102 set_cpu_possible(0, true);
Mikael Starvik51533b62005-07-27 11:44:44 -0700103}
104
105void __init smp_cpus_done(unsigned int max_cpus)
106{
107}
108
109/* Bring one cpu online.*/
110static int __init
Thomas Gleixnera4cfc312012-04-20 13:05:53 +0000111smp_boot_one_cpu(int cpuid, struct task_struct idle)
Mikael Starvik51533b62005-07-27 11:44:44 -0700112{
113 unsigned timeout;
KOSAKI Motohiro8aebe212011-05-24 17:12:59 -0700114 cpumask_t cpu_mask;
Mikael Starvik51533b62005-07-27 11:44:44 -0700115
KOSAKI Motohiro8aebe212011-05-24 17:12:59 -0700116 cpumask_clear(&cpu_mask);
Al Viro718d6112006-01-12 01:06:04 -0800117 task_thread_info(idle)->cpu = cpuid;
Mikael Starvik51533b62005-07-27 11:44:44 -0700118
119 /* Information to the CPU that is about to boot */
Al Viro718d6112006-01-12 01:06:04 -0800120 smp_init_current_idle_thread = task_thread_info(idle);
Mikael Starvik51533b62005-07-27 11:44:44 -0700121 cpu_now_booting = cpuid;
122
Jesper Nilsson538380d2008-01-25 16:15:44 +0100123 /* Kick it */
KOSAKI Motohiro8aebe212011-05-24 17:12:59 -0700124 set_cpu_online(cpuid, true);
125 cpumask_set_cpu(cpuid, &cpu_mask);
Jesper Nilsson538380d2008-01-25 16:15:44 +0100126 send_ipi(IPI_BOOT, 0, cpu_mask);
KOSAKI Motohiro8aebe212011-05-24 17:12:59 -0700127 set_cpu_online(cpuid, false);
Jesper Nilsson538380d2008-01-25 16:15:44 +0100128
Mikael Starvik51533b62005-07-27 11:44:44 -0700129 /* Wait for CPU to come online */
130 for (timeout = 0; timeout < 10000; timeout++) {
131 if(cpu_online(cpuid)) {
132 cpu_now_booting = 0;
133 smp_init_current_idle_thread = NULL;
134 return 0; /* CPU online */
135 }
136 udelay(100);
137 barrier();
138 }
139
Mikael Starvik51533b62005-07-27 11:44:44 -0700140 printk(KERN_CRIT "SMP: CPU:%d is stuck.\n", cpuid);
141 return -1;
142}
143
Simon Arlott49b4ff32007-10-20 01:08:50 +0200144/* Secondary CPUs starts using C here. Here we need to setup CPU
Mikael Starvik51533b62005-07-27 11:44:44 -0700145 * specific stuff such as the local timer and the MMU. */
146void __init smp_callin(void)
147{
148 extern void cpu_idle(void);
149
150 int cpu = cpu_now_booting;
151 reg_intr_vect_rw_mask vect_mask = {0};
152
153 /* Initialise the idle task for this CPU */
154 atomic_inc(&init_mm.mm_count);
155 current->active_mm = &init_mm;
156
157 /* Set up MMU */
158 cris_mmu_init();
159 __flush_tlb_all();
160
161 /* Setup local timer. */
162 cris_timer_init();
163
164 /* Enable IRQ and idle */
165 REG_WR(intr_vect, irq_regs[cpu], rw_mask, vect_mask);
Jesper Nilsson41507642010-05-25 17:48:14 +0200166 crisv32_unmask_irq(IPI_INTR_VECT);
167 crisv32_unmask_irq(TIMER0_INTR_VECT);
Nick Piggin5bfb5d62005-11-08 21:39:01 -0800168 preempt_disable();
Manfred Spraule545a612008-09-07 16:57:22 +0200169 notify_cpu_starting(cpu);
Mikael Starvik51533b62005-07-27 11:44:44 -0700170 local_irq_enable();
171
KOSAKI Motohiro8aebe212011-05-24 17:12:59 -0700172 set_cpu_online(cpu, true);
Mikael Starvik51533b62005-07-27 11:44:44 -0700173 cpu_idle();
174}
175
176/* Stop execution on this CPU.*/
177void stop_this_cpu(void* dummy)
178{
179 local_irq_disable();
180 asm volatile("halt");
181}
182
183/* Other calls */
184void smp_send_stop(void)
185{
Jens Axboe8691e5a2008-06-06 11:18:06 +0200186 smp_call_function(stop_this_cpu, NULL, 0);
Mikael Starvik51533b62005-07-27 11:44:44 -0700187}
188
189int setup_profiling_timer(unsigned int multiplier)
190{
191 return -EINVAL;
192}
193
194
195/* cache_decay_ticks is used by the scheduler to decide if a process
196 * is "hot" on one CPU. A higher value means a higher penalty to move
197 * a process to another CPU. Our cache is rather small so we report
198 * 1 tick.
199 */
200unsigned long cache_decay_ticks = 1;
201
Thomas Gleixner8239c252012-04-20 13:05:42 +0000202int __cpuinit __cpu_up(unsigned int cpu, struct task_struct *tidle)
Mikael Starvik51533b62005-07-27 11:44:44 -0700203{
Thomas Gleixnera4cfc312012-04-20 13:05:53 +0000204 smp_boot_one_cpu(cpu, tidle);
Mikael Starvik51533b62005-07-27 11:44:44 -0700205 return cpu_online(cpu) ? 0 : -ENOSYS;
206}
207
208void smp_send_reschedule(int cpu)
209{
KOSAKI Motohiro8aebe212011-05-24 17:12:59 -0700210 cpumask_t cpu_mask;
211 cpumask_clear(&cpu_mask);
212 cpumask_set_cpu(cpu, &cpu_mask);
Mikael Starvik51533b62005-07-27 11:44:44 -0700213 send_ipi(IPI_SCHEDULE, 0, cpu_mask);
214}
215
216/* TLB flushing
217 *
218 * Flush needs to be done on the local CPU and on any other CPU that
219 * may have the same mapping. The mm->cpu_vm_mask is used to keep track
220 * of which CPUs that a specific process has been executed on.
221 */
222void flush_tlb_common(struct mm_struct* mm, struct vm_area_struct* vma, unsigned long addr)
223{
224 unsigned long flags;
225 cpumask_t cpu_mask;
226
227 spin_lock_irqsave(&tlbstate_lock, flags);
Rusty Russellb9d65c02009-03-16 14:11:47 +1030228 cpu_mask = (mm == FLUSH_ALL ? cpu_all_mask : *mm_cpumask(mm));
KOSAKI Motohiro8aebe212011-05-24 17:12:59 -0700229 cpumask_clear_cpu(smp_processor_id(), &cpu_mask);
Mikael Starvik51533b62005-07-27 11:44:44 -0700230 flush_mm = mm;
231 flush_vma = vma;
232 flush_addr = addr;
233 send_ipi(IPI_FLUSH_TLB, 1, cpu_mask);
234 spin_unlock_irqrestore(&tlbstate_lock, flags);
235}
236
237void flush_tlb_all(void)
238{
239 __flush_tlb_all();
240 flush_tlb_common(FLUSH_ALL, FLUSH_ALL, 0);
241}
242
243void flush_tlb_mm(struct mm_struct *mm)
244{
245 __flush_tlb_mm(mm);
246 flush_tlb_common(mm, FLUSH_ALL, 0);
247 /* No more mappings in other CPUs */
Rusty Russellb9d65c02009-03-16 14:11:47 +1030248 cpumask_clear(mm_cpumask(mm));
249 cpumask_set_cpu(smp_processor_id(), mm_cpumask(mm));
Mikael Starvik51533b62005-07-27 11:44:44 -0700250}
251
252void flush_tlb_page(struct vm_area_struct *vma,
253 unsigned long addr)
254{
255 __flush_tlb_page(vma, addr);
256 flush_tlb_common(vma->vm_mm, vma, addr);
257}
258
259/* Inter processor interrupts
260 *
261 * The IPIs are used for:
262 * * Force a schedule on a CPU
263 * * FLush TLB on other CPUs
264 * * Call a function on other CPUs
265 */
266
267int send_ipi(int vector, int wait, cpumask_t cpu_mask)
268{
269 int i = 0;
270 reg_intr_vect_rw_ipi ipi = REG_RD(intr_vect, irq_regs[i], rw_ipi);
271 int ret = 0;
272
273 /* Calculate CPUs to send to. */
KOSAKI Motohiro8aebe212011-05-24 17:12:59 -0700274 cpumask_and(&cpu_mask, &cpu_mask, cpu_online_mask);
Mikael Starvik51533b62005-07-27 11:44:44 -0700275
276 /* Send the IPI. */
KOSAKI Motohiro8aebe212011-05-24 17:12:59 -0700277 for_each_cpu(i, &cpu_mask)
Mikael Starvik51533b62005-07-27 11:44:44 -0700278 {
279 ipi.vector |= vector;
280 REG_WR(intr_vect, irq_regs[i], rw_ipi, ipi);
281 }
282
283 /* Wait for IPI to finish on other CPUS */
284 if (wait) {
KOSAKI Motohiro8aebe212011-05-24 17:12:59 -0700285 for_each_cpu(i, &cpu_mask) {
Mikael Starvik51533b62005-07-27 11:44:44 -0700286 int j;
287 for (j = 0 ; j < 1000; j++) {
288 ipi = REG_RD(intr_vect, irq_regs[i], rw_ipi);
289 if (!ipi.vector)
290 break;
291 udelay(100);
292 }
293
294 /* Timeout? */
295 if (ipi.vector) {
296 printk("SMP call timeout from %d to %d\n", smp_processor_id(), i);
297 ret = -ETIMEDOUT;
298 dump_stack();
299 }
300 }
301 }
302 return ret;
303}
304
305/*
306 * You must not call this function with disabled interrupts or from a
307 * hardware interrupt handler or from a bottom half handler.
308 */
Jens Axboe8691e5a2008-06-06 11:18:06 +0200309int smp_call_function(void (*func)(void *info), void *info, int wait)
Mikael Starvik51533b62005-07-27 11:44:44 -0700310{
KOSAKI Motohiro8aebe212011-05-24 17:12:59 -0700311 cpumask_t cpu_mask;
Mikael Starvik51533b62005-07-27 11:44:44 -0700312 struct call_data_struct data;
313 int ret;
314
KOSAKI Motohiro8aebe212011-05-24 17:12:59 -0700315 cpumask_setall(&cpu_mask);
316 cpumask_clear_cpu(smp_processor_id(), &cpu_mask);
Mikael Starvik51533b62005-07-27 11:44:44 -0700317
318 WARN_ON(irqs_disabled());
319
320 data.func = func;
321 data.info = info;
322 data.wait = wait;
323
324 spin_lock(&call_lock);
325 call_data = &data;
326 ret = send_ipi(IPI_CALL, wait, cpu_mask);
327 spin_unlock(&call_lock);
328
329 return ret;
330}
331
Jesper Nilsson538380d2008-01-25 16:15:44 +0100332irqreturn_t crisv32_ipi_interrupt(int irq, void *dev_id)
Mikael Starvik51533b62005-07-27 11:44:44 -0700333{
334 void (*func) (void *info) = call_data->func;
335 void *info = call_data->info;
336 reg_intr_vect_rw_ipi ipi;
337
338 ipi = REG_RD(intr_vect, irq_regs[smp_processor_id()], rw_ipi);
339
Peter Zijlstra184748c2011-04-05 17:23:39 +0200340 if (ipi.vector & IPI_SCHEDULE) {
341 scheduler_ipi();
342 }
Mikael Starvik51533b62005-07-27 11:44:44 -0700343 if (ipi.vector & IPI_CALL) {
Peter Zijlstra184748c2011-04-05 17:23:39 +0200344 func(info);
Mikael Starvik51533b62005-07-27 11:44:44 -0700345 }
346 if (ipi.vector & IPI_FLUSH_TLB) {
Peter Zijlstra184748c2011-04-05 17:23:39 +0200347 if (flush_mm == FLUSH_ALL)
348 __flush_tlb_all();
349 else if (flush_vma == FLUSH_ALL)
Mikael Starvik51533b62005-07-27 11:44:44 -0700350 __flush_tlb_mm(flush_mm);
Peter Zijlstra184748c2011-04-05 17:23:39 +0200351 else
Mikael Starvik51533b62005-07-27 11:44:44 -0700352 __flush_tlb_page(flush_vma, flush_addr);
353 }
354
355 ipi.vector = 0;
356 REG_WR(intr_vect, irq_regs[smp_processor_id()], rw_ipi, ipi);
357
358 return IRQ_HANDLED;
359}
360