blob: d98585f3237d5114f9d054e386c64898a13ce08a [file] [log] [blame]
Graf Yang6b3087c2009-01-07 23:14:39 +08001/*
Robin Getz96f10502009-09-24 14:11:24 +00002 * IPI management based on arch/arm/kernel/smp.c (Copyright 2002 ARM Limited)
Graf Yang6b3087c2009-01-07 23:14:39 +08003 *
Robin Getz96f10502009-09-24 14:11:24 +00004 * Copyright 2007-2009 Analog Devices Inc.
5 * Philippe Gerum <rpm@xenomai.org>
Graf Yang6b3087c2009-01-07 23:14:39 +08006 *
Robin Getz96f10502009-09-24 14:11:24 +00007 * Licensed under the GPL-2.
Graf Yang6b3087c2009-01-07 23:14:39 +08008 */
9
10#include <linux/module.h>
11#include <linux/delay.h>
12#include <linux/init.h>
13#include <linux/spinlock.h>
14#include <linux/sched.h>
15#include <linux/interrupt.h>
16#include <linux/cache.h>
17#include <linux/profile.h>
18#include <linux/errno.h>
19#include <linux/mm.h>
20#include <linux/cpu.h>
21#include <linux/smp.h>
22#include <linux/seq_file.h>
23#include <linux/irq.h>
24#include <asm/atomic.h>
25#include <asm/cacheflush.h>
26#include <asm/mmu_context.h>
27#include <asm/pgtable.h>
28#include <asm/pgalloc.h>
29#include <asm/processor.h>
30#include <asm/ptrace.h>
31#include <asm/cpu.h>
Graf Yang1fa9be72009-05-15 11:01:59 +000032#include <asm/time.h>
Graf Yang6b3087c2009-01-07 23:14:39 +080033#include <linux/err.h>
34
Graf Yang555487b2009-05-06 10:38:07 +000035/*
36 * Anomaly notes:
37 * 05000120 - we always define corelock as 32-bit integer in L2
38 */
Graf Yang6b3087c2009-01-07 23:14:39 +080039struct corelock_slot corelock __attribute__ ((__section__(".l2.bss")));
40
41void __cpuinitdata *init_retx_coreb, *init_saved_retx_coreb,
42 *init_saved_seqstat_coreb, *init_saved_icplb_fault_addr_coreb,
43 *init_saved_dcplb_fault_addr_coreb;
44
45cpumask_t cpu_possible_map;
46EXPORT_SYMBOL(cpu_possible_map);
47
48cpumask_t cpu_online_map;
49EXPORT_SYMBOL(cpu_online_map);
50
51#define BFIN_IPI_RESCHEDULE 0
52#define BFIN_IPI_CALL_FUNC 1
53#define BFIN_IPI_CPU_STOP 2
54
55struct blackfin_flush_data {
56 unsigned long start;
57 unsigned long end;
58};
59
60void *secondary_stack;
61
62
63struct smp_call_struct {
64 void (*func)(void *info);
65 void *info;
66 int wait;
67 cpumask_t pending;
68 cpumask_t waitmask;
69};
70
71static struct blackfin_flush_data smp_flush_data;
72
73static DEFINE_SPINLOCK(stop_lock);
74
75struct ipi_message {
76 struct list_head list;
77 unsigned long type;
78 struct smp_call_struct call_struct;
79};
80
81struct ipi_message_queue {
82 struct list_head head;
83 spinlock_t lock;
84 unsigned long count;
85};
86
87static DEFINE_PER_CPU(struct ipi_message_queue, ipi_msg_queue);
88
89static void ipi_cpu_stop(unsigned int cpu)
90{
91 spin_lock(&stop_lock);
92 printk(KERN_CRIT "CPU%u: stopping\n", cpu);
93 dump_stack();
94 spin_unlock(&stop_lock);
95
96 cpu_clear(cpu, cpu_online_map);
97
98 local_irq_disable();
99
100 while (1)
101 SSYNC();
102}
103
104static void ipi_flush_icache(void *info)
105{
106 struct blackfin_flush_data *fdata = info;
107
108 /* Invalidate the memory holding the bounds of the flushed region. */
109 blackfin_dcache_invalidate_range((unsigned long)fdata,
110 (unsigned long)fdata + sizeof(*fdata));
111
112 blackfin_icache_flush_range(fdata->start, fdata->end);
113}
114
115static void ipi_call_function(unsigned int cpu, struct ipi_message *msg)
116{
117 int wait;
118 void (*func)(void *info);
119 void *info;
120 func = msg->call_struct.func;
121 info = msg->call_struct.info;
122 wait = msg->call_struct.wait;
123 cpu_clear(cpu, msg->call_struct.pending);
124 func(info);
125 if (wait)
126 cpu_clear(cpu, msg->call_struct.waitmask);
127 else
128 kfree(msg);
129}
130
131static irqreturn_t ipi_handler(int irq, void *dev_instance)
132{
Sonic Zhang86f20082009-06-10 08:42:41 +0000133 struct ipi_message *msg;
Graf Yang6b3087c2009-01-07 23:14:39 +0800134 struct ipi_message_queue *msg_queue;
135 unsigned int cpu = smp_processor_id();
136
137 platform_clear_ipi(cpu);
138
139 msg_queue = &__get_cpu_var(ipi_msg_queue);
140 msg_queue->count++;
141
142 spin_lock(&msg_queue->lock);
Sonic Zhang86f20082009-06-10 08:42:41 +0000143 while (!list_empty(&msg_queue->head)) {
144 msg = list_entry(msg_queue->head.next, typeof(*msg), list);
Graf Yang6b3087c2009-01-07 23:14:39 +0800145 list_del(&msg->list);
146 switch (msg->type) {
147 case BFIN_IPI_RESCHEDULE:
148 /* That's the easiest one; leave it to
149 * return_from_int. */
150 kfree(msg);
151 break;
152 case BFIN_IPI_CALL_FUNC:
Sonic Zhang0bf3d932009-03-05 16:44:53 +0800153 spin_unlock(&msg_queue->lock);
Graf Yang6b3087c2009-01-07 23:14:39 +0800154 ipi_call_function(cpu, msg);
Sonic Zhang0bf3d932009-03-05 16:44:53 +0800155 spin_lock(&msg_queue->lock);
Graf Yang6b3087c2009-01-07 23:14:39 +0800156 break;
157 case BFIN_IPI_CPU_STOP:
Sonic Zhang0bf3d932009-03-05 16:44:53 +0800158 spin_unlock(&msg_queue->lock);
Graf Yang6b3087c2009-01-07 23:14:39 +0800159 ipi_cpu_stop(cpu);
Sonic Zhang0bf3d932009-03-05 16:44:53 +0800160 spin_lock(&msg_queue->lock);
Graf Yang6b3087c2009-01-07 23:14:39 +0800161 kfree(msg);
162 break;
163 default:
164 printk(KERN_CRIT "CPU%u: Unknown IPI message \
165 0x%lx\n", cpu, msg->type);
166 kfree(msg);
167 break;
168 }
169 }
170 spin_unlock(&msg_queue->lock);
171 return IRQ_HANDLED;
172}
173
174static void ipi_queue_init(void)
175{
176 unsigned int cpu;
177 struct ipi_message_queue *msg_queue;
178 for_each_possible_cpu(cpu) {
179 msg_queue = &per_cpu(ipi_msg_queue, cpu);
180 INIT_LIST_HEAD(&msg_queue->head);
181 spin_lock_init(&msg_queue->lock);
182 msg_queue->count = 0;
183 }
184}
185
186int smp_call_function(void (*func)(void *info), void *info, int wait)
187{
188 unsigned int cpu;
189 cpumask_t callmap;
190 unsigned long flags;
191 struct ipi_message_queue *msg_queue;
192 struct ipi_message *msg;
193
194 callmap = cpu_online_map;
195 cpu_clear(smp_processor_id(), callmap);
196 if (cpus_empty(callmap))
197 return 0;
198
199 msg = kmalloc(sizeof(*msg), GFP_ATOMIC);
Julia Lawall994e9a22009-07-14 03:15:19 +0000200 if (!msg)
201 return -ENOMEM;
Graf Yang6b3087c2009-01-07 23:14:39 +0800202 INIT_LIST_HEAD(&msg->list);
203 msg->call_struct.func = func;
204 msg->call_struct.info = info;
205 msg->call_struct.wait = wait;
206 msg->call_struct.pending = callmap;
207 msg->call_struct.waitmask = callmap;
208 msg->type = BFIN_IPI_CALL_FUNC;
209
210 for_each_cpu_mask(cpu, callmap) {
211 msg_queue = &per_cpu(ipi_msg_queue, cpu);
212 spin_lock_irqsave(&msg_queue->lock, flags);
Sonic Zhang86f20082009-06-10 08:42:41 +0000213 list_add_tail(&msg->list, &msg_queue->head);
Graf Yang6b3087c2009-01-07 23:14:39 +0800214 spin_unlock_irqrestore(&msg_queue->lock, flags);
215 platform_send_ipi_cpu(cpu);
216 }
217 if (wait) {
218 while (!cpus_empty(msg->call_struct.waitmask))
219 blackfin_dcache_invalidate_range(
220 (unsigned long)(&msg->call_struct.waitmask),
221 (unsigned long)(&msg->call_struct.waitmask));
222 kfree(msg);
223 }
224 return 0;
225}
226EXPORT_SYMBOL_GPL(smp_call_function);
227
228int smp_call_function_single(int cpuid, void (*func) (void *info), void *info,
229 int wait)
230{
231 unsigned int cpu = cpuid;
232 cpumask_t callmap;
233 unsigned long flags;
234 struct ipi_message_queue *msg_queue;
235 struct ipi_message *msg;
236
237 if (cpu_is_offline(cpu))
238 return 0;
239 cpus_clear(callmap);
240 cpu_set(cpu, callmap);
241
242 msg = kmalloc(sizeof(*msg), GFP_ATOMIC);
Julia Lawall994e9a22009-07-14 03:15:19 +0000243 if (!msg)
244 return -ENOMEM;
Graf Yang6b3087c2009-01-07 23:14:39 +0800245 INIT_LIST_HEAD(&msg->list);
246 msg->call_struct.func = func;
247 msg->call_struct.info = info;
248 msg->call_struct.wait = wait;
249 msg->call_struct.pending = callmap;
250 msg->call_struct.waitmask = callmap;
251 msg->type = BFIN_IPI_CALL_FUNC;
252
253 msg_queue = &per_cpu(ipi_msg_queue, cpu);
254 spin_lock_irqsave(&msg_queue->lock, flags);
Sonic Zhang86f20082009-06-10 08:42:41 +0000255 list_add_tail(&msg->list, &msg_queue->head);
Graf Yang6b3087c2009-01-07 23:14:39 +0800256 spin_unlock_irqrestore(&msg_queue->lock, flags);
257 platform_send_ipi_cpu(cpu);
258
259 if (wait) {
260 while (!cpus_empty(msg->call_struct.waitmask))
261 blackfin_dcache_invalidate_range(
262 (unsigned long)(&msg->call_struct.waitmask),
263 (unsigned long)(&msg->call_struct.waitmask));
264 kfree(msg);
265 }
266 return 0;
267}
268EXPORT_SYMBOL_GPL(smp_call_function_single);
269
270void smp_send_reschedule(int cpu)
271{
272 unsigned long flags;
273 struct ipi_message_queue *msg_queue;
274 struct ipi_message *msg;
275
276 if (cpu_is_offline(cpu))
277 return;
278
279 msg = kmalloc(sizeof(*msg), GFP_ATOMIC);
Julia Lawall994e9a22009-07-14 03:15:19 +0000280 if (!msg)
281 return;
Graf Yang6b3087c2009-01-07 23:14:39 +0800282 memset(msg, 0, sizeof(msg));
283 INIT_LIST_HEAD(&msg->list);
284 msg->type = BFIN_IPI_RESCHEDULE;
285
286 msg_queue = &per_cpu(ipi_msg_queue, cpu);
287 spin_lock_irqsave(&msg_queue->lock, flags);
Sonic Zhang86f20082009-06-10 08:42:41 +0000288 list_add_tail(&msg->list, &msg_queue->head);
Graf Yang6b3087c2009-01-07 23:14:39 +0800289 spin_unlock_irqrestore(&msg_queue->lock, flags);
290 platform_send_ipi_cpu(cpu);
291
292 return;
293}
294
295void smp_send_stop(void)
296{
297 unsigned int cpu;
298 cpumask_t callmap;
299 unsigned long flags;
300 struct ipi_message_queue *msg_queue;
301 struct ipi_message *msg;
302
303 callmap = cpu_online_map;
304 cpu_clear(smp_processor_id(), callmap);
305 if (cpus_empty(callmap))
306 return;
307
308 msg = kmalloc(sizeof(*msg), GFP_ATOMIC);
Julia Lawall994e9a22009-07-14 03:15:19 +0000309 if (!msg)
310 return;
Graf Yang6b3087c2009-01-07 23:14:39 +0800311 memset(msg, 0, sizeof(msg));
312 INIT_LIST_HEAD(&msg->list);
313 msg->type = BFIN_IPI_CPU_STOP;
314
315 for_each_cpu_mask(cpu, callmap) {
316 msg_queue = &per_cpu(ipi_msg_queue, cpu);
317 spin_lock_irqsave(&msg_queue->lock, flags);
Sonic Zhang86f20082009-06-10 08:42:41 +0000318 list_add_tail(&msg->list, &msg_queue->head);
Graf Yang6b3087c2009-01-07 23:14:39 +0800319 spin_unlock_irqrestore(&msg_queue->lock, flags);
320 platform_send_ipi_cpu(cpu);
321 }
322 return;
323}
324
325int __cpuinit __cpu_up(unsigned int cpu)
326{
327 struct task_struct *idle;
328 int ret;
329
330 idle = fork_idle(cpu);
331 if (IS_ERR(idle)) {
332 printk(KERN_ERR "CPU%u: fork() failed\n", cpu);
333 return PTR_ERR(idle);
334 }
335
336 secondary_stack = task_stack_page(idle) + THREAD_SIZE;
337 smp_wmb();
338
339 ret = platform_boot_secondary(cpu, idle);
340
341 if (ret) {
342 cpu_clear(cpu, cpu_present_map);
343 printk(KERN_CRIT "CPU%u: processor failed to boot (%d)\n", cpu, ret);
344 free_task(idle);
345 } else
346 cpu_set(cpu, cpu_online_map);
347
348 secondary_stack = NULL;
349
350 return ret;
351}
352
353static void __cpuinit setup_secondary(unsigned int cpu)
354{
Graf Yang1fa9be72009-05-15 11:01:59 +0000355#if !defined(CONFIG_TICKSOURCE_GPTMR0)
Graf Yang6b3087c2009-01-07 23:14:39 +0800356 struct irq_desc *timer_desc;
357#endif
358 unsigned long ilat;
359
360 bfin_write_IMASK(0);
361 CSYNC();
362 ilat = bfin_read_ILAT();
363 CSYNC();
364 bfin_write_ILAT(ilat);
365 CSYNC();
366
Graf Yang6b3087c2009-01-07 23:14:39 +0800367 /* Enable interrupt levels IVG7-15. IARs have been already
368 * programmed by the boot CPU. */
Mike Frysinger40059782008-11-18 17:48:22 +0800369 bfin_irq_flags |= IMASK_IVG15 |
Graf Yang6b3087c2009-01-07 23:14:39 +0800370 IMASK_IVG14 | IMASK_IVG13 | IMASK_IVG12 | IMASK_IVG11 |
371 IMASK_IVG10 | IMASK_IVG9 | IMASK_IVG8 | IMASK_IVG7 | IMASK_IVGHW;
372
Graf Yang1fa9be72009-05-15 11:01:59 +0000373#if defined(CONFIG_TICKSOURCE_GPTMR0)
Graf Yang6b3087c2009-01-07 23:14:39 +0800374 /* Power down the core timer, just to play safe. */
375 bfin_write_TCNTL(0);
376
377 /* system timer0 has been setup by CoreA. */
378#else
379 timer_desc = irq_desc + IRQ_CORETMR;
380 setup_core_timer();
381 timer_desc->chip->enable(IRQ_CORETMR);
382#endif
383}
384
385void __cpuinit secondary_start_kernel(void)
386{
387 unsigned int cpu = smp_processor_id();
388 struct mm_struct *mm = &init_mm;
389
390 if (_bfin_swrst & SWRST_DBL_FAULT_B) {
391 printk(KERN_EMERG "CoreB Recovering from DOUBLE FAULT event\n");
392#ifdef CONFIG_DEBUG_DOUBLEFAULT
393 printk(KERN_EMERG " While handling exception (EXCAUSE = 0x%x) at %pF\n",
394 (int)init_saved_seqstat_coreb & SEQSTAT_EXCAUSE, init_saved_retx_coreb);
395 printk(KERN_NOTICE " DCPLB_FAULT_ADDR: %pF\n", init_saved_dcplb_fault_addr_coreb);
396 printk(KERN_NOTICE " ICPLB_FAULT_ADDR: %pF\n", init_saved_icplb_fault_addr_coreb);
397#endif
398 printk(KERN_NOTICE " The instruction at %pF caused a double exception\n",
399 init_retx_coreb);
400 }
401
402 /*
403 * We want the D-cache to be enabled early, in case the atomic
404 * support code emulates cache coherence (see
405 * __ARCH_SYNC_CORE_DCACHE).
406 */
407 init_exception_vectors();
408
409 bfin_setup_caches(cpu);
410
411 local_irq_disable();
412
413 /* Attach the new idle task to the global mm. */
414 atomic_inc(&mm->mm_users);
415 atomic_inc(&mm->mm_count);
416 current->active_mm = mm;
417 BUG_ON(current->mm); /* Can't be, but better be safe than sorry. */
418
419 preempt_disable();
420
421 setup_secondary(cpu);
422
423 local_irq_enable();
424
425 platform_secondary_init(cpu);
426
427 cpu_idle();
428}
429
430void __init smp_prepare_boot_cpu(void)
431{
432}
433
434void __init smp_prepare_cpus(unsigned int max_cpus)
435{
436 platform_prepare_cpus(max_cpus);
437 ipi_queue_init();
438 platform_request_ipi(&ipi_handler);
439}
440
441void __init smp_cpus_done(unsigned int max_cpus)
442{
443 unsigned long bogosum = 0;
444 unsigned int cpu;
445
446 for_each_online_cpu(cpu)
Michael Hennerichc70c7542009-07-09 09:58:52 +0000447 bogosum += loops_per_jiffy;
Graf Yang6b3087c2009-01-07 23:14:39 +0800448
449 printk(KERN_INFO "SMP: Total of %d processors activated "
450 "(%lu.%02lu BogoMIPS).\n",
451 num_online_cpus(),
452 bogosum / (500000/HZ),
453 (bogosum / (5000/HZ)) % 100);
454}
455
456void smp_icache_flush_range_others(unsigned long start, unsigned long end)
457{
458 smp_flush_data.start = start;
459 smp_flush_data.end = end;
460
Sonic Zhang0bf3d932009-03-05 16:44:53 +0800461 if (smp_call_function(&ipi_flush_icache, &smp_flush_data, 0))
Graf Yang6b3087c2009-01-07 23:14:39 +0800462 printk(KERN_WARNING "SMP: failed to run I-cache flush request on other CPUs\n");
463}
464EXPORT_SYMBOL_GPL(smp_icache_flush_range_others);
465
Sonic Zhang47e9ded2009-06-10 08:57:08 +0000466#ifdef __ARCH_SYNC_CORE_ICACHE
467void resync_core_icache(void)
468{
469 unsigned int cpu = get_cpu();
470 blackfin_invalidate_entire_icache();
471 ++per_cpu(cpu_data, cpu).icache_invld_count;
472 put_cpu();
473}
474EXPORT_SYMBOL(resync_core_icache);
475#endif
476
Graf Yang6b3087c2009-01-07 23:14:39 +0800477#ifdef __ARCH_SYNC_CORE_DCACHE
478unsigned long barrier_mask __attribute__ ((__section__(".l2.bss")));
479
480void resync_core_dcache(void)
481{
482 unsigned int cpu = get_cpu();
483 blackfin_invalidate_entire_dcache();
484 ++per_cpu(cpu_data, cpu).dcache_invld_count;
485 put_cpu();
486}
487EXPORT_SYMBOL(resync_core_dcache);
488#endif