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/seq_file.h>
23 #include <linux/irq.h>
24 #include <linux/slab.h>
25 #include <asm/atomic.h>
26 #include <asm/cacheflush.h>
27 #include <asm/mmu_context.h>
28 #include <asm/pgtable.h>
29 #include <asm/pgalloc.h>
30 #include <asm/processor.h>
31 #include <asm/ptrace.h>
34 #include <linux/err.h>
38 * 05000120 - we always define corelock as 32-bit integer in L2
40 struct corelock_slot corelock
__attribute__ ((__section__(".l2.bss")));
42 void __cpuinitdata
*init_retx_coreb
, *init_saved_retx_coreb
,
43 *init_saved_seqstat_coreb
, *init_saved_icplb_fault_addr_coreb
,
44 *init_saved_dcplb_fault_addr_coreb
;
46 cpumask_t cpu_possible_map
;
47 EXPORT_SYMBOL(cpu_possible_map
);
49 cpumask_t cpu_online_map
;
50 EXPORT_SYMBOL(cpu_online_map
);
52 #define BFIN_IPI_RESCHEDULE 0
53 #define BFIN_IPI_CALL_FUNC 1
54 #define BFIN_IPI_CPU_STOP 2
56 struct blackfin_flush_data
{
61 void *secondary_stack
;
64 struct smp_call_struct
{
65 void (*func
)(void *info
);
72 static struct blackfin_flush_data smp_flush_data
;
74 static DEFINE_SPINLOCK(stop_lock
);
77 struct list_head list
;
79 struct smp_call_struct call_struct
;
82 struct ipi_message_queue
{
83 struct list_head head
;
88 static DEFINE_PER_CPU(struct ipi_message_queue
, ipi_msg_queue
);
90 static void ipi_cpu_stop(unsigned int cpu
)
92 spin_lock(&stop_lock
);
93 printk(KERN_CRIT
"CPU%u: stopping\n", cpu
);
95 spin_unlock(&stop_lock
);
97 cpu_clear(cpu
, cpu_online_map
);
105 static void ipi_flush_icache(void *info
)
107 struct blackfin_flush_data
*fdata
= info
;
109 /* Invalidate the memory holding the bounds of the flushed region. */
110 blackfin_dcache_invalidate_range((unsigned long)fdata
,
111 (unsigned long)fdata
+ sizeof(*fdata
));
113 blackfin_icache_flush_range(fdata
->start
, fdata
->end
);
116 static void ipi_call_function(unsigned int cpu
, struct ipi_message
*msg
)
119 void (*func
)(void *info
);
121 func
= msg
->call_struct
.func
;
122 info
= msg
->call_struct
.info
;
123 wait
= msg
->call_struct
.wait
;
124 cpu_clear(cpu
, msg
->call_struct
.pending
);
127 #ifdef __ARCH_SYNC_CORE_DCACHE
129 * 'wait' usually means synchronization between CPUs.
130 * Invalidate D cache in case shared data was changed
131 * by func() to ensure cache coherence.
133 resync_core_dcache();
135 cpu_clear(cpu
, msg
->call_struct
.waitmask
);
140 static irqreturn_t
ipi_handler(int irq
, void *dev_instance
)
142 struct ipi_message
*msg
;
143 struct ipi_message_queue
*msg_queue
;
144 unsigned int cpu
= smp_processor_id();
146 platform_clear_ipi(cpu
);
148 msg_queue
= &__get_cpu_var(ipi_msg_queue
);
151 spin_lock(&msg_queue
->lock
);
152 while (!list_empty(&msg_queue
->head
)) {
153 msg
= list_entry(msg_queue
->head
.next
, typeof(*msg
), list
);
154 list_del(&msg
->list
);
156 case BFIN_IPI_RESCHEDULE
:
157 /* That's the easiest one; leave it to
158 * return_from_int. */
161 case BFIN_IPI_CALL_FUNC
:
162 spin_unlock(&msg_queue
->lock
);
163 ipi_call_function(cpu
, msg
);
164 spin_lock(&msg_queue
->lock
);
166 case BFIN_IPI_CPU_STOP
:
167 spin_unlock(&msg_queue
->lock
);
169 spin_lock(&msg_queue
->lock
);
173 printk(KERN_CRIT
"CPU%u: Unknown IPI message 0x%lx\n",
179 spin_unlock(&msg_queue
->lock
);
183 static void ipi_queue_init(void)
186 struct ipi_message_queue
*msg_queue
;
187 for_each_possible_cpu(cpu
) {
188 msg_queue
= &per_cpu(ipi_msg_queue
, cpu
);
189 INIT_LIST_HEAD(&msg_queue
->head
);
190 spin_lock_init(&msg_queue
->lock
);
191 msg_queue
->count
= 0;
195 int smp_call_function(void (*func
)(void *info
), void *info
, int wait
)
200 struct ipi_message_queue
*msg_queue
;
201 struct ipi_message
*msg
;
203 callmap
= cpu_online_map
;
204 cpu_clear(smp_processor_id(), callmap
);
205 if (cpus_empty(callmap
))
208 msg
= kmalloc(sizeof(*msg
), GFP_ATOMIC
);
211 INIT_LIST_HEAD(&msg
->list
);
212 msg
->call_struct
.func
= func
;
213 msg
->call_struct
.info
= info
;
214 msg
->call_struct
.wait
= wait
;
215 msg
->call_struct
.pending
= callmap
;
216 msg
->call_struct
.waitmask
= callmap
;
217 msg
->type
= BFIN_IPI_CALL_FUNC
;
219 for_each_cpu_mask(cpu
, callmap
) {
220 msg_queue
= &per_cpu(ipi_msg_queue
, cpu
);
221 spin_lock_irqsave(&msg_queue
->lock
, flags
);
222 list_add_tail(&msg
->list
, &msg_queue
->head
);
223 spin_unlock_irqrestore(&msg_queue
->lock
, flags
);
224 platform_send_ipi_cpu(cpu
);
227 while (!cpus_empty(msg
->call_struct
.waitmask
))
228 blackfin_dcache_invalidate_range(
229 (unsigned long)(&msg
->call_struct
.waitmask
),
230 (unsigned long)(&msg
->call_struct
.waitmask
));
231 #ifdef __ARCH_SYNC_CORE_DCACHE
233 * Invalidate D cache in case shared data was changed by
234 * other processors to ensure cache coherence.
236 resync_core_dcache();
242 EXPORT_SYMBOL_GPL(smp_call_function
);
244 int smp_call_function_single(int cpuid
, void (*func
) (void *info
), void *info
,
247 unsigned int cpu
= cpuid
;
250 struct ipi_message_queue
*msg_queue
;
251 struct ipi_message
*msg
;
253 if (cpu_is_offline(cpu
))
256 cpu_set(cpu
, callmap
);
258 msg
= kmalloc(sizeof(*msg
), GFP_ATOMIC
);
261 INIT_LIST_HEAD(&msg
->list
);
262 msg
->call_struct
.func
= func
;
263 msg
->call_struct
.info
= info
;
264 msg
->call_struct
.wait
= wait
;
265 msg
->call_struct
.pending
= callmap
;
266 msg
->call_struct
.waitmask
= callmap
;
267 msg
->type
= BFIN_IPI_CALL_FUNC
;
269 msg_queue
= &per_cpu(ipi_msg_queue
, cpu
);
270 spin_lock_irqsave(&msg_queue
->lock
, flags
);
271 list_add_tail(&msg
->list
, &msg_queue
->head
);
272 spin_unlock_irqrestore(&msg_queue
->lock
, flags
);
273 platform_send_ipi_cpu(cpu
);
276 while (!cpus_empty(msg
->call_struct
.waitmask
))
277 blackfin_dcache_invalidate_range(
278 (unsigned long)(&msg
->call_struct
.waitmask
),
279 (unsigned long)(&msg
->call_struct
.waitmask
));
280 #ifdef __ARCH_SYNC_CORE_DCACHE
282 * Invalidate D cache in case shared data was changed by
283 * other processors to ensure cache coherence.
285 resync_core_dcache();
291 EXPORT_SYMBOL_GPL(smp_call_function_single
);
293 void smp_send_reschedule(int cpu
)
296 struct ipi_message_queue
*msg_queue
;
297 struct ipi_message
*msg
;
299 if (cpu_is_offline(cpu
))
302 msg
= kzalloc(sizeof(*msg
), GFP_ATOMIC
);
305 INIT_LIST_HEAD(&msg
->list
);
306 msg
->type
= BFIN_IPI_RESCHEDULE
;
308 msg_queue
= &per_cpu(ipi_msg_queue
, cpu
);
309 spin_lock_irqsave(&msg_queue
->lock
, flags
);
310 list_add_tail(&msg
->list
, &msg_queue
->head
);
311 spin_unlock_irqrestore(&msg_queue
->lock
, flags
);
312 platform_send_ipi_cpu(cpu
);
317 void smp_send_stop(void)
322 struct ipi_message_queue
*msg_queue
;
323 struct ipi_message
*msg
;
325 callmap
= cpu_online_map
;
326 cpu_clear(smp_processor_id(), callmap
);
327 if (cpus_empty(callmap
))
330 msg
= kzalloc(sizeof(*msg
), GFP_ATOMIC
);
333 INIT_LIST_HEAD(&msg
->list
);
334 msg
->type
= BFIN_IPI_CPU_STOP
;
336 for_each_cpu_mask(cpu
, callmap
) {
337 msg_queue
= &per_cpu(ipi_msg_queue
, cpu
);
338 spin_lock_irqsave(&msg_queue
->lock
, flags
);
339 list_add_tail(&msg
->list
, &msg_queue
->head
);
340 spin_unlock_irqrestore(&msg_queue
->lock
, flags
);
341 platform_send_ipi_cpu(cpu
);
346 int __cpuinit
__cpu_up(unsigned int cpu
)
349 static struct task_struct
*idle
;
354 idle
= fork_idle(cpu
);
356 printk(KERN_ERR
"CPU%u: fork() failed\n", cpu
);
357 return PTR_ERR(idle
);
360 secondary_stack
= task_stack_page(idle
) + THREAD_SIZE
;
362 ret
= platform_boot_secondary(cpu
, idle
);
364 secondary_stack
= NULL
;
369 static void __cpuinit
setup_secondary(unsigned int cpu
)
375 ilat
= bfin_read_ILAT();
377 bfin_write_ILAT(ilat
);
380 /* Enable interrupt levels IVG7-15. IARs have been already
381 * programmed by the boot CPU. */
382 bfin_irq_flags
|= IMASK_IVG15
|
383 IMASK_IVG14
| IMASK_IVG13
| IMASK_IVG12
| IMASK_IVG11
|
384 IMASK_IVG10
| IMASK_IVG9
| IMASK_IVG8
| IMASK_IVG7
| IMASK_IVGHW
;
387 void __cpuinit
secondary_start_kernel(void)
389 unsigned int cpu
= smp_processor_id();
390 struct mm_struct
*mm
= &init_mm
;
392 if (_bfin_swrst
& SWRST_DBL_FAULT_B
) {
393 printk(KERN_EMERG
"CoreB Recovering from DOUBLE FAULT event\n");
394 #ifdef CONFIG_DEBUG_DOUBLEFAULT
395 printk(KERN_EMERG
" While handling exception (EXCAUSE = 0x%x) at %pF\n",
396 (int)init_saved_seqstat_coreb
& SEQSTAT_EXCAUSE
, init_saved_retx_coreb
);
397 printk(KERN_NOTICE
" DCPLB_FAULT_ADDR: %pF\n", init_saved_dcplb_fault_addr_coreb
);
398 printk(KERN_NOTICE
" ICPLB_FAULT_ADDR: %pF\n", init_saved_icplb_fault_addr_coreb
);
400 printk(KERN_NOTICE
" The instruction at %pF caused a double exception\n",
405 * We want the D-cache to be enabled early, in case the atomic
406 * support code emulates cache coherence (see
407 * __ARCH_SYNC_CORE_DCACHE).
409 init_exception_vectors();
411 bfin_setup_caches(cpu
);
415 /* Attach the new idle task to the global mm. */
416 atomic_inc(&mm
->mm_users
);
417 atomic_inc(&mm
->mm_count
);
418 current
->active_mm
= mm
;
422 setup_secondary(cpu
);
424 platform_secondary_init(cpu
);
426 /* setup local core timer */
427 bfin_local_timer_setup();
432 * Calibrate loops per jiffy value.
433 * IRQs need to be enabled here - D-cache can be invalidated
434 * in timer irq handler, so core B can read correct jiffies.
441 void __init
smp_prepare_boot_cpu(void)
445 void __init
smp_prepare_cpus(unsigned int max_cpus
)
447 platform_prepare_cpus(max_cpus
);
449 platform_request_ipi(&ipi_handler
);
452 void __init
smp_cpus_done(unsigned int max_cpus
)
454 unsigned long bogosum
= 0;
457 for_each_online_cpu(cpu
)
458 bogosum
+= loops_per_jiffy
;
460 printk(KERN_INFO
"SMP: Total of %d processors activated "
461 "(%lu.%02lu BogoMIPS).\n",
463 bogosum
/ (500000/HZ
),
464 (bogosum
/ (5000/HZ
)) % 100);
467 void smp_icache_flush_range_others(unsigned long start
, unsigned long end
)
469 smp_flush_data
.start
= start
;
470 smp_flush_data
.end
= end
;
472 if (smp_call_function(&ipi_flush_icache
, &smp_flush_data
, 0))
473 printk(KERN_WARNING
"SMP: failed to run I-cache flush request on other CPUs\n");
475 EXPORT_SYMBOL_GPL(smp_icache_flush_range_others
);
477 #ifdef __ARCH_SYNC_CORE_ICACHE
478 unsigned long icache_invld_count
[NR_CPUS
];
479 void resync_core_icache(void)
481 unsigned int cpu
= get_cpu();
482 blackfin_invalidate_entire_icache();
483 icache_invld_count
[cpu
]++;
486 EXPORT_SYMBOL(resync_core_icache
);
489 #ifdef __ARCH_SYNC_CORE_DCACHE
490 unsigned long dcache_invld_count
[NR_CPUS
];
491 unsigned long barrier_mask
__attribute__ ((__section__(".l2.bss")));
493 void resync_core_dcache(void)
495 unsigned int cpu
= get_cpu();
496 blackfin_invalidate_entire_dcache();
497 dcache_invld_count
[cpu
]++;
500 EXPORT_SYMBOL(resync_core_dcache
);
503 #ifdef CONFIG_HOTPLUG_CPU
504 int __cpuexit
__cpu_disable(void)
506 unsigned int cpu
= smp_processor_id();
511 set_cpu_online(cpu
, false);
515 static DECLARE_COMPLETION(cpu_killed
);
517 int __cpuexit
__cpu_die(unsigned int cpu
)
519 return wait_for_completion_timeout(&cpu_killed
, 5000);
524 complete(&cpu_killed
);
526 atomic_dec(&init_mm
.mm_users
);
527 atomic_dec(&init_mm
.mm_count
);