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/irq_handler.h>
29 #include <asm/mmu_context.h>
30 #include <asm/pgtable.h>
31 #include <asm/pgalloc.h>
32 #include <asm/processor.h>
33 #include <asm/ptrace.h>
36 #include <linux/err.h>
40 * 05000120 - we always define corelock as 32-bit integer in L2
42 struct corelock_slot corelock
__attribute__ ((__section__(".l2.bss")));
44 #ifdef CONFIG_ICACHE_FLUSH_L1
45 unsigned long blackfin_iflush_l1_entry
[NR_CPUS
];
48 void __cpuinitdata
*init_retx_coreb
, *init_saved_retx_coreb
,
49 *init_saved_seqstat_coreb
, *init_saved_icplb_fault_addr_coreb
,
50 *init_saved_dcplb_fault_addr_coreb
;
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
);
71 static struct blackfin_flush_data smp_flush_data
;
73 static DEFINE_SPINLOCK(stop_lock
);
77 struct smp_call_struct call_struct
;
80 /* A magic number - stress test shows this is safe for common cases */
81 #define BFIN_IPI_MSGQ_LEN 5
83 /* Simple FIFO buffer, overflow leads to panic */
84 struct ipi_message_queue
{
87 unsigned long head
; /* head of the queue */
88 struct ipi_message ipi_message
[BFIN_IPI_MSGQ_LEN
];
91 static DEFINE_PER_CPU(struct ipi_message_queue
, ipi_msg_queue
);
93 static void ipi_cpu_stop(unsigned int cpu
)
95 spin_lock(&stop_lock
);
96 printk(KERN_CRIT
"CPU%u: stopping\n", cpu
);
98 spin_unlock(&stop_lock
);
100 set_cpu_online(cpu
, false);
108 static void ipi_flush_icache(void *info
)
110 struct blackfin_flush_data
*fdata
= info
;
112 /* Invalidate the memory holding the bounds of the flushed region. */
113 blackfin_dcache_invalidate_range((unsigned long)fdata
,
114 (unsigned long)fdata
+ sizeof(*fdata
));
116 /* Make sure all write buffers in the data side of the core
117 * are flushed before trying to invalidate the icache. This
118 * needs to be after the data flush and before the icache
119 * flush so that the SSYNC does the right thing in preventing
120 * the instruction prefetcher from hitting things in cached
121 * memory at the wrong time -- it runs much further ahead than
126 /* ipi_flaush_icache is invoked by generic flush_icache_range,
127 * so call blackfin arch icache flush directly here.
129 blackfin_icache_flush_range(fdata
->start
, fdata
->end
);
132 static void ipi_call_function(unsigned int cpu
, struct ipi_message
*msg
)
135 void (*func
)(void *info
);
137 func
= msg
->call_struct
.func
;
138 info
= msg
->call_struct
.info
;
139 wait
= msg
->call_struct
.wait
;
142 #ifdef __ARCH_SYNC_CORE_DCACHE
144 * 'wait' usually means synchronization between CPUs.
145 * Invalidate D cache in case shared data was changed
146 * by func() to ensure cache coherence.
148 resync_core_dcache();
150 cpumask_clear_cpu(cpu
, msg
->call_struct
.waitmask
);
154 /* Use IRQ_SUPPLE_0 to request reschedule.
155 * When returning from interrupt to user space,
156 * there is chance to reschedule */
157 static irqreturn_t
ipi_handler_int0(int irq
, void *dev_instance
)
159 unsigned int cpu
= smp_processor_id();
161 platform_clear_ipi(cpu
, IRQ_SUPPLE_0
);
165 static irqreturn_t
ipi_handler_int1(int irq
, void *dev_instance
)
167 struct ipi_message
*msg
;
168 struct ipi_message_queue
*msg_queue
;
169 unsigned int cpu
= smp_processor_id();
172 platform_clear_ipi(cpu
, IRQ_SUPPLE_1
);
174 msg_queue
= &__get_cpu_var(ipi_msg_queue
);
176 spin_lock_irqsave(&msg_queue
->lock
, flags
);
178 while (msg_queue
->count
) {
179 msg
= &msg_queue
->ipi_message
[msg_queue
->head
];
181 case BFIN_IPI_RESCHEDULE
:
184 case BFIN_IPI_CALL_FUNC
:
185 spin_unlock_irqrestore(&msg_queue
->lock
, flags
);
186 ipi_call_function(cpu
, msg
);
187 spin_lock_irqsave(&msg_queue
->lock
, flags
);
189 case BFIN_IPI_CPU_STOP
:
190 spin_unlock_irqrestore(&msg_queue
->lock
, flags
);
192 spin_lock_irqsave(&msg_queue
->lock
, flags
);
195 printk(KERN_CRIT
"CPU%u: Unknown IPI message 0x%lx\n",
200 msg_queue
->head
%= BFIN_IPI_MSGQ_LEN
;
203 spin_unlock_irqrestore(&msg_queue
->lock
, flags
);
207 static void ipi_queue_init(void)
210 struct ipi_message_queue
*msg_queue
;
211 for_each_possible_cpu(cpu
) {
212 msg_queue
= &per_cpu(ipi_msg_queue
, cpu
);
213 spin_lock_init(&msg_queue
->lock
);
214 msg_queue
->count
= 0;
219 static inline void smp_send_message(cpumask_t callmap
, unsigned long type
,
220 void (*func
) (void *info
), void *info
, int wait
)
223 struct ipi_message_queue
*msg_queue
;
224 struct ipi_message
*msg
;
225 unsigned long flags
, next_msg
;
226 cpumask_t waitmask
; /* waitmask is shared by all cpus */
228 cpumask_copy(&waitmask
, &callmap
);
229 for_each_cpu(cpu
, &callmap
) {
230 msg_queue
= &per_cpu(ipi_msg_queue
, cpu
);
231 spin_lock_irqsave(&msg_queue
->lock
, flags
);
232 if (msg_queue
->count
< BFIN_IPI_MSGQ_LEN
) {
233 next_msg
= (msg_queue
->head
+ msg_queue
->count
)
235 msg
= &msg_queue
->ipi_message
[next_msg
];
237 if (type
== BFIN_IPI_CALL_FUNC
) {
238 msg
->call_struct
.func
= func
;
239 msg
->call_struct
.info
= info
;
240 msg
->call_struct
.wait
= wait
;
241 msg
->call_struct
.waitmask
= &waitmask
;
245 panic("IPI message queue overflow\n");
246 spin_unlock_irqrestore(&msg_queue
->lock
, flags
);
247 platform_send_ipi_cpu(cpu
, IRQ_SUPPLE_1
);
251 while (!cpumask_empty(&waitmask
))
252 blackfin_dcache_invalidate_range(
253 (unsigned long)(&waitmask
),
254 (unsigned long)(&waitmask
));
255 #ifdef __ARCH_SYNC_CORE_DCACHE
257 * Invalidate D cache in case shared data was changed by
258 * other processors to ensure cache coherence.
260 resync_core_dcache();
265 int smp_call_function(void (*func
)(void *info
), void *info
, int wait
)
270 cpumask_copy(&callmap
, cpu_online_mask
);
271 cpumask_clear_cpu(smp_processor_id(), &callmap
);
272 if (!cpumask_empty(&callmap
))
273 smp_send_message(callmap
, BFIN_IPI_CALL_FUNC
, func
, info
, wait
);
279 EXPORT_SYMBOL_GPL(smp_call_function
);
281 int smp_call_function_single(int cpuid
, void (*func
) (void *info
), void *info
,
284 unsigned int cpu
= cpuid
;
287 if (cpu_is_offline(cpu
))
289 cpumask_clear(&callmap
);
290 cpumask_set_cpu(cpu
, &callmap
);
292 smp_send_message(callmap
, BFIN_IPI_CALL_FUNC
, func
, info
, wait
);
296 EXPORT_SYMBOL_GPL(smp_call_function_single
);
298 void smp_send_reschedule(int cpu
)
300 /* simply trigger an ipi */
301 if (cpu_is_offline(cpu
))
303 platform_send_ipi_cpu(cpu
, IRQ_SUPPLE_0
);
308 void smp_send_stop(void)
313 cpumask_copy(&callmap
, cpu_online_mask
);
314 cpumask_clear_cpu(smp_processor_id(), &callmap
);
315 if (!cpumask_empty(&callmap
))
316 smp_send_message(callmap
, BFIN_IPI_CPU_STOP
, NULL
, NULL
, 0);
323 int __cpuinit
__cpu_up(unsigned int cpu
)
326 static struct task_struct
*idle
;
331 idle
= fork_idle(cpu
);
333 printk(KERN_ERR
"CPU%u: fork() failed\n", cpu
);
334 return PTR_ERR(idle
);
337 secondary_stack
= task_stack_page(idle
) + THREAD_SIZE
;
339 ret
= platform_boot_secondary(cpu
, idle
);
341 secondary_stack
= NULL
;
346 static void __cpuinit
setup_secondary(unsigned int cpu
)
352 ilat
= bfin_read_ILAT();
354 bfin_write_ILAT(ilat
);
357 /* Enable interrupt levels IVG7-15. IARs have been already
358 * programmed by the boot CPU. */
359 bfin_irq_flags
|= IMASK_IVG15
|
360 IMASK_IVG14
| IMASK_IVG13
| IMASK_IVG12
| IMASK_IVG11
|
361 IMASK_IVG10
| IMASK_IVG9
| IMASK_IVG8
| IMASK_IVG7
| IMASK_IVGHW
;
364 void __cpuinit
secondary_start_kernel(void)
366 unsigned int cpu
= smp_processor_id();
367 struct mm_struct
*mm
= &init_mm
;
369 if (_bfin_swrst
& SWRST_DBL_FAULT_B
) {
370 printk(KERN_EMERG
"CoreB Recovering from DOUBLE FAULT event\n");
371 #ifdef CONFIG_DEBUG_DOUBLEFAULT
372 printk(KERN_EMERG
" While handling exception (EXCAUSE = 0x%x) at %pF\n",
373 (int)init_saved_seqstat_coreb
& SEQSTAT_EXCAUSE
, init_saved_retx_coreb
);
374 printk(KERN_NOTICE
" DCPLB_FAULT_ADDR: %pF\n", init_saved_dcplb_fault_addr_coreb
);
375 printk(KERN_NOTICE
" ICPLB_FAULT_ADDR: %pF\n", init_saved_icplb_fault_addr_coreb
);
377 printk(KERN_NOTICE
" The instruction at %pF caused a double exception\n",
382 * We want the D-cache to be enabled early, in case the atomic
383 * support code emulates cache coherence (see
384 * __ARCH_SYNC_CORE_DCACHE).
386 init_exception_vectors();
390 /* Attach the new idle task to the global mm. */
391 atomic_inc(&mm
->mm_users
);
392 atomic_inc(&mm
->mm_count
);
393 current
->active_mm
= mm
;
397 setup_secondary(cpu
);
399 platform_secondary_init(cpu
);
401 /* setup local core timer */
402 bfin_local_timer_setup();
406 bfin_setup_caches(cpu
);
409 * Calibrate loops per jiffy value.
410 * IRQs need to be enabled here - D-cache can be invalidated
411 * in timer irq handler, so core B can read correct jiffies.
418 void __init
smp_prepare_boot_cpu(void)
422 void __init
smp_prepare_cpus(unsigned int max_cpus
)
424 platform_prepare_cpus(max_cpus
);
426 platform_request_ipi(IRQ_SUPPLE_0
, ipi_handler_int0
);
427 platform_request_ipi(IRQ_SUPPLE_1
, ipi_handler_int1
);
430 void __init
smp_cpus_done(unsigned int max_cpus
)
432 unsigned long bogosum
= 0;
435 for_each_online_cpu(cpu
)
436 bogosum
+= loops_per_jiffy
;
438 printk(KERN_INFO
"SMP: Total of %d processors activated "
439 "(%lu.%02lu BogoMIPS).\n",
441 bogosum
/ (500000/HZ
),
442 (bogosum
/ (5000/HZ
)) % 100);
445 void smp_icache_flush_range_others(unsigned long start
, unsigned long end
)
447 smp_flush_data
.start
= start
;
448 smp_flush_data
.end
= end
;
450 if (smp_call_function(&ipi_flush_icache
, &smp_flush_data
, 0))
451 printk(KERN_WARNING
"SMP: failed to run I-cache flush request on other CPUs\n");
453 EXPORT_SYMBOL_GPL(smp_icache_flush_range_others
);
455 #ifdef __ARCH_SYNC_CORE_ICACHE
456 unsigned long icache_invld_count
[NR_CPUS
];
457 void resync_core_icache(void)
459 unsigned int cpu
= get_cpu();
460 blackfin_invalidate_entire_icache();
461 icache_invld_count
[cpu
]++;
464 EXPORT_SYMBOL(resync_core_icache
);
467 #ifdef __ARCH_SYNC_CORE_DCACHE
468 unsigned long dcache_invld_count
[NR_CPUS
];
469 unsigned long barrier_mask
__attribute__ ((__section__(".l2.bss")));
471 void resync_core_dcache(void)
473 unsigned int cpu
= get_cpu();
474 blackfin_invalidate_entire_dcache();
475 dcache_invld_count
[cpu
]++;
478 EXPORT_SYMBOL(resync_core_dcache
);
481 #ifdef CONFIG_HOTPLUG_CPU
482 int __cpuexit
__cpu_disable(void)
484 unsigned int cpu
= smp_processor_id();
489 set_cpu_online(cpu
, false);
493 static DECLARE_COMPLETION(cpu_killed
);
495 int __cpuexit
__cpu_die(unsigned int cpu
)
497 return wait_for_completion_timeout(&cpu_killed
, 5000);
502 complete(&cpu_killed
);
504 atomic_dec(&init_mm
.mm_users
);
505 atomic_dec(&init_mm
.mm_count
);