2 * IPI management based on arch/arm/kernel/smp.c (Copyright 2002 ARM Limited)
4 * Copyright 2007-2009 Analog Devices Inc.
5 * Philippe Gerum <rpm@xenomai.org>
7 * Licensed under the GPL-2.
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>
20 #include <linux/cpu.h>
21 #include <linux/smp.h>
22 #include <linux/cpumask.h>
23 #include <linux/seq_file.h>
24 #include <linux/irq.h>
25 #include <linux/slab.h>
26 #include <asm/atomic.h>
27 #include <asm/cacheflush.h>
28 #include <asm/mmu_context.h>
29 #include <asm/pgtable.h>
30 #include <asm/pgalloc.h>
31 #include <asm/processor.h>
32 #include <asm/ptrace.h>
35 #include <linux/err.h>
39 * 05000120 - we always define corelock as 32-bit integer in L2
41 struct corelock_slot corelock __attribute__ ((__section__(".l2.bss")));
43 #ifdef CONFIG_ICACHE_FLUSH_L1
44 unsigned long blackfin_iflush_l1_entry[NR_CPUS];
47 void __cpuinitdata *init_retx_coreb, *init_saved_retx_coreb,
48 *init_saved_seqstat_coreb, *init_saved_icplb_fault_addr_coreb,
49 *init_saved_dcplb_fault_addr_coreb;
51 #define BFIN_IPI_RESCHEDULE 0
52 #define BFIN_IPI_CALL_FUNC 1
53 #define BFIN_IPI_CPU_STOP 2
55 struct blackfin_flush_data {
60 void *secondary_stack;
63 struct smp_call_struct {
64 void (*func)(void *info);
70 static struct blackfin_flush_data smp_flush_data;
72 static DEFINE_SPINLOCK(stop_lock);
76 struct smp_call_struct call_struct;
79 /* A magic number - stress test shows this is safe for common cases */
80 #define BFIN_IPI_MSGQ_LEN 5
82 /* Simple FIFO buffer, overflow leads to panic */
83 struct ipi_message_queue {
86 unsigned long head; /* head of the queue */
87 struct ipi_message ipi_message[BFIN_IPI_MSGQ_LEN];
90 static DEFINE_PER_CPU(struct ipi_message_queue, ipi_msg_queue);
92 static void ipi_cpu_stop(unsigned int cpu)
94 spin_lock(&stop_lock);
95 printk(KERN_CRIT "CPU%u: stopping\n", cpu);
97 spin_unlock(&stop_lock);
99 cpu_clear(cpu, cpu_online_map);
107 static void ipi_flush_icache(void *info)
109 struct blackfin_flush_data *fdata = info;
111 /* Invalidate the memory holding the bounds of the flushed region. */
112 blackfin_dcache_invalidate_range((unsigned long)fdata,
113 (unsigned long)fdata + sizeof(*fdata));
115 /* Make sure all write buffers in the data side of the core
116 * are flushed before trying to invalidate the icache. This
117 * needs to be after the data flush and before the icache
118 * flush so that the SSYNC does the right thing in preventing
119 * the instruction prefetcher from hitting things in cached
120 * memory at the wrong time -- it runs much further ahead than
125 /* ipi_flaush_icache is invoked by generic flush_icache_range,
126 * so call blackfin arch icache flush directly here.
128 blackfin_icache_flush_range(fdata->start, fdata->end);
131 static void ipi_call_function(unsigned int cpu, struct ipi_message *msg)
134 void (*func)(void *info);
136 func = msg->call_struct.func;
137 info = msg->call_struct.info;
138 wait = msg->call_struct.wait;
141 #ifdef __ARCH_SYNC_CORE_DCACHE
143 * 'wait' usually means synchronization between CPUs.
144 * Invalidate D cache in case shared data was changed
145 * by func() to ensure cache coherence.
147 resync_core_dcache();
149 cpu_clear(cpu, *msg->call_struct.waitmask);
153 /* Use IRQ_SUPPLE_0 to request reschedule.
154 * When returning from interrupt to user space,
155 * there is chance to reschedule */
156 static irqreturn_t ipi_handler_int0(int irq, void *dev_instance)
158 unsigned int cpu = smp_processor_id();
160 platform_clear_ipi(cpu, IRQ_SUPPLE_0);
164 static irqreturn_t ipi_handler_int1(int irq, void *dev_instance)
166 struct ipi_message *msg;
167 struct ipi_message_queue *msg_queue;
168 unsigned int cpu = smp_processor_id();
171 platform_clear_ipi(cpu, IRQ_SUPPLE_1);
173 msg_queue = &__get_cpu_var(ipi_msg_queue);
175 spin_lock_irqsave(&msg_queue->lock, flags);
177 while (msg_queue->count) {
178 msg = &msg_queue->ipi_message[msg_queue->head];
180 case BFIN_IPI_CALL_FUNC:
181 spin_unlock_irqrestore(&msg_queue->lock, flags);
182 ipi_call_function(cpu, msg);
183 spin_lock_irqsave(&msg_queue->lock, flags);
185 case BFIN_IPI_CPU_STOP:
186 spin_unlock_irqrestore(&msg_queue->lock, flags);
188 spin_lock_irqsave(&msg_queue->lock, flags);
191 printk(KERN_CRIT "CPU%u: Unknown IPI message 0x%lx\n",
196 msg_queue->head %= BFIN_IPI_MSGQ_LEN;
199 spin_unlock_irqrestore(&msg_queue->lock, flags);
203 static void ipi_queue_init(void)
206 struct ipi_message_queue *msg_queue;
207 for_each_possible_cpu(cpu) {
208 msg_queue = &per_cpu(ipi_msg_queue, cpu);
209 spin_lock_init(&msg_queue->lock);
210 msg_queue->count = 0;
215 static inline void smp_send_message(cpumask_t callmap, unsigned long type,
216 void (*func) (void *info), void *info, int wait)
219 struct ipi_message_queue *msg_queue;
220 struct ipi_message *msg;
221 unsigned long flags, next_msg;
222 cpumask_t waitmask = callmap; /* waitmask is shared by all cpus */
224 for_each_cpu_mask(cpu, callmap) {
225 msg_queue = &per_cpu(ipi_msg_queue, cpu);
226 spin_lock_irqsave(&msg_queue->lock, flags);
227 if (msg_queue->count < BFIN_IPI_MSGQ_LEN) {
228 next_msg = (msg_queue->head + msg_queue->count)
230 msg = &msg_queue->ipi_message[next_msg];
232 if (type == BFIN_IPI_CALL_FUNC) {
233 msg->call_struct.func = func;
234 msg->call_struct.info = info;
235 msg->call_struct.wait = wait;
236 msg->call_struct.waitmask = &waitmask;
240 panic("IPI message queue overflow\n");
241 spin_unlock_irqrestore(&msg_queue->lock, flags);
242 platform_send_ipi_cpu(cpu, IRQ_SUPPLE_1);
246 while (!cpus_empty(waitmask))
247 blackfin_dcache_invalidate_range(
248 (unsigned long)(&waitmask),
249 (unsigned long)(&waitmask));
250 #ifdef __ARCH_SYNC_CORE_DCACHE
252 * Invalidate D cache in case shared data was changed by
253 * other processors to ensure cache coherence.
255 resync_core_dcache();
260 int smp_call_function(void (*func)(void *info), void *info, int wait)
265 callmap = cpu_online_map;
266 cpu_clear(smp_processor_id(), callmap);
267 if (!cpus_empty(callmap))
268 smp_send_message(callmap, BFIN_IPI_CALL_FUNC, func, info, wait);
274 EXPORT_SYMBOL_GPL(smp_call_function);
276 int smp_call_function_single(int cpuid, void (*func) (void *info), void *info,
279 unsigned int cpu = cpuid;
282 if (cpu_is_offline(cpu))
285 cpu_set(cpu, callmap);
287 smp_send_message(callmap, BFIN_IPI_CALL_FUNC, func, info, wait);
291 EXPORT_SYMBOL_GPL(smp_call_function_single);
293 void smp_send_reschedule(int cpu)
295 /* simply trigger an ipi */
296 if (cpu_is_offline(cpu))
298 platform_send_ipi_cpu(cpu, IRQ_SUPPLE_0);
303 void smp_send_stop(void)
308 callmap = cpu_online_map;
309 cpu_clear(smp_processor_id(), callmap);
310 if (!cpus_empty(callmap))
311 smp_send_message(callmap, BFIN_IPI_CPU_STOP, NULL, NULL, 0);
318 int __cpuinit __cpu_up(unsigned int cpu)
321 static struct task_struct *idle;
326 idle = fork_idle(cpu);
328 printk(KERN_ERR "CPU%u: fork() failed\n", cpu);
329 return PTR_ERR(idle);
332 secondary_stack = task_stack_page(idle) + THREAD_SIZE;
334 ret = platform_boot_secondary(cpu, idle);
336 secondary_stack = NULL;
341 static void __cpuinit setup_secondary(unsigned int cpu)
347 ilat = bfin_read_ILAT();
349 bfin_write_ILAT(ilat);
352 /* Enable interrupt levels IVG7-15. IARs have been already
353 * programmed by the boot CPU. */
354 bfin_irq_flags |= IMASK_IVG15 |
355 IMASK_IVG14 | IMASK_IVG13 | IMASK_IVG12 | IMASK_IVG11 |
356 IMASK_IVG10 | IMASK_IVG9 | IMASK_IVG8 | IMASK_IVG7 | IMASK_IVGHW;
359 void __cpuinit secondary_start_kernel(void)
361 unsigned int cpu = smp_processor_id();
362 struct mm_struct *mm = &init_mm;
364 if (_bfin_swrst & SWRST_DBL_FAULT_B) {
365 printk(KERN_EMERG "CoreB Recovering from DOUBLE FAULT event\n");
366 #ifdef CONFIG_DEBUG_DOUBLEFAULT
367 printk(KERN_EMERG " While handling exception (EXCAUSE = 0x%x) at %pF\n",
368 (int)init_saved_seqstat_coreb & SEQSTAT_EXCAUSE, init_saved_retx_coreb);
369 printk(KERN_NOTICE " DCPLB_FAULT_ADDR: %pF\n", init_saved_dcplb_fault_addr_coreb);
370 printk(KERN_NOTICE " ICPLB_FAULT_ADDR: %pF\n", init_saved_icplb_fault_addr_coreb);
372 printk(KERN_NOTICE " The instruction at %pF caused a double exception\n",
377 * We want the D-cache to be enabled early, in case the atomic
378 * support code emulates cache coherence (see
379 * __ARCH_SYNC_CORE_DCACHE).
381 init_exception_vectors();
385 /* Attach the new idle task to the global mm. */
386 atomic_inc(&mm->mm_users);
387 atomic_inc(&mm->mm_count);
388 current->active_mm = mm;
392 setup_secondary(cpu);
394 platform_secondary_init(cpu);
396 /* setup local core timer */
397 bfin_local_timer_setup();
401 bfin_setup_caches(cpu);
404 * Calibrate loops per jiffy value.
405 * IRQs need to be enabled here - D-cache can be invalidated
406 * in timer irq handler, so core B can read correct jiffies.
413 void __init smp_prepare_boot_cpu(void)
417 void __init smp_prepare_cpus(unsigned int max_cpus)
419 platform_prepare_cpus(max_cpus);
421 platform_request_ipi(IRQ_SUPPLE_0, ipi_handler_int0);
422 platform_request_ipi(IRQ_SUPPLE_1, ipi_handler_int1);
425 void __init smp_cpus_done(unsigned int max_cpus)
427 unsigned long bogosum = 0;
430 for_each_online_cpu(cpu)
431 bogosum += loops_per_jiffy;
433 printk(KERN_INFO "SMP: Total of %d processors activated "
434 "(%lu.%02lu BogoMIPS).\n",
436 bogosum / (500000/HZ),
437 (bogosum / (5000/HZ)) % 100);
440 void smp_icache_flush_range_others(unsigned long start, unsigned long end)
442 smp_flush_data.start = start;
443 smp_flush_data.end = end;
445 if (smp_call_function(&ipi_flush_icache, &smp_flush_data, 0))
446 printk(KERN_WARNING "SMP: failed to run I-cache flush request on other CPUs\n");
448 EXPORT_SYMBOL_GPL(smp_icache_flush_range_others);
450 #ifdef __ARCH_SYNC_CORE_ICACHE
451 unsigned long icache_invld_count[NR_CPUS];
452 void resync_core_icache(void)
454 unsigned int cpu = get_cpu();
455 blackfin_invalidate_entire_icache();
456 icache_invld_count[cpu]++;
459 EXPORT_SYMBOL(resync_core_icache);
462 #ifdef __ARCH_SYNC_CORE_DCACHE
463 unsigned long dcache_invld_count[NR_CPUS];
464 unsigned long barrier_mask __attribute__ ((__section__(".l2.bss")));
466 void resync_core_dcache(void)
468 unsigned int cpu = get_cpu();
469 blackfin_invalidate_entire_dcache();
470 dcache_invld_count[cpu]++;
473 EXPORT_SYMBOL(resync_core_dcache);
476 #ifdef CONFIG_HOTPLUG_CPU
477 int __cpuexit __cpu_disable(void)
479 unsigned int cpu = smp_processor_id();
484 set_cpu_online(cpu, false);
488 static DECLARE_COMPLETION(cpu_killed);
490 int __cpuexit __cpu_die(unsigned int cpu)
492 return wait_for_completion_timeout(&cpu_killed, 5000);
497 complete(&cpu_killed);
499 atomic_dec(&init_mm.mm_users);
500 atomic_dec(&init_mm.mm_count);