xtensa: fix high memory/reserved memory collision
[cris-mirror.git] / arch / blackfin / mach-common / smp.c
blobb32ddab7966c95c167dfd533743863f88f044c0b
1 /*
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.
8 */
10 #include <linux/module.h>
11 #include <linux/delay.h>
12 #include <linux/init.h>
13 #include <linux/spinlock.h>
14 #include <linux/sched/mm.h>
15 #include <linux/sched/task_stack.h>
16 #include <linux/interrupt.h>
17 #include <linux/cache.h>
18 #include <linux/clockchips.h>
19 #include <linux/profile.h>
20 #include <linux/errno.h>
21 #include <linux/mm.h>
22 #include <linux/cpu.h>
23 #include <linux/smp.h>
24 #include <linux/cpumask.h>
25 #include <linux/seq_file.h>
26 #include <linux/irq.h>
27 #include <linux/slab.h>
28 #include <linux/atomic.h>
29 #include <asm/cacheflush.h>
30 #include <asm/irq_handler.h>
31 #include <asm/mmu_context.h>
32 #include <asm/pgtable.h>
33 #include <asm/pgalloc.h>
34 #include <asm/processor.h>
35 #include <asm/ptrace.h>
36 #include <asm/cpu.h>
37 #include <asm/time.h>
38 #include <linux/err.h>
41 * Anomaly notes:
42 * 05000120 - we always define corelock as 32-bit integer in L2
44 struct corelock_slot corelock __attribute__ ((__section__(".l2.bss")));
46 #ifdef CONFIG_ICACHE_FLUSH_L1
47 unsigned long blackfin_iflush_l1_entry[NR_CPUS];
48 #endif
50 struct blackfin_initial_pda initial_pda_coreb;
52 enum ipi_message_type {
53 BFIN_IPI_NONE,
54 BFIN_IPI_TIMER,
55 BFIN_IPI_RESCHEDULE,
56 BFIN_IPI_CALL_FUNC,
57 BFIN_IPI_CPU_STOP,
60 struct blackfin_flush_data {
61 unsigned long start;
62 unsigned long end;
65 void *secondary_stack;
67 static struct blackfin_flush_data smp_flush_data;
69 static DEFINE_SPINLOCK(stop_lock);
71 /* A magic number - stress test shows this is safe for common cases */
72 #define BFIN_IPI_MSGQ_LEN 5
74 /* Simple FIFO buffer, overflow leads to panic */
75 struct ipi_data {
76 atomic_t count;
77 atomic_t bits;
80 static DEFINE_PER_CPU(struct ipi_data, bfin_ipi);
82 static void ipi_cpu_stop(unsigned int cpu)
84 spin_lock(&stop_lock);
85 printk(KERN_CRIT "CPU%u: stopping\n", cpu);
86 dump_stack();
87 spin_unlock(&stop_lock);
89 set_cpu_online(cpu, false);
91 local_irq_disable();
93 while (1)
94 SSYNC();
97 static void ipi_flush_icache(void *info)
99 struct blackfin_flush_data *fdata = info;
101 /* Invalidate the memory holding the bounds of the flushed region. */
102 blackfin_dcache_invalidate_range((unsigned long)fdata,
103 (unsigned long)fdata + sizeof(*fdata));
105 /* Make sure all write buffers in the data side of the core
106 * are flushed before trying to invalidate the icache. This
107 * needs to be after the data flush and before the icache
108 * flush so that the SSYNC does the right thing in preventing
109 * the instruction prefetcher from hitting things in cached
110 * memory at the wrong time -- it runs much further ahead than
111 * the pipeline.
113 SSYNC();
115 /* ipi_flaush_icache is invoked by generic flush_icache_range,
116 * so call blackfin arch icache flush directly here.
118 blackfin_icache_flush_range(fdata->start, fdata->end);
121 /* Use IRQ_SUPPLE_0 to request reschedule.
122 * When returning from interrupt to user space,
123 * there is chance to reschedule */
124 static irqreturn_t ipi_handler_int0(int irq, void *dev_instance)
126 unsigned int cpu = smp_processor_id();
128 platform_clear_ipi(cpu, IRQ_SUPPLE_0);
129 return IRQ_HANDLED;
132 DECLARE_PER_CPU(struct clock_event_device, coretmr_events);
133 void ipi_timer(void)
135 int cpu = smp_processor_id();
136 struct clock_event_device *evt = &per_cpu(coretmr_events, cpu);
137 evt->event_handler(evt);
140 static irqreturn_t ipi_handler_int1(int irq, void *dev_instance)
142 struct ipi_data *bfin_ipi_data;
143 unsigned int cpu = smp_processor_id();
144 unsigned long pending;
145 unsigned long msg;
147 platform_clear_ipi(cpu, IRQ_SUPPLE_1);
149 smp_rmb();
150 bfin_ipi_data = this_cpu_ptr(&bfin_ipi);
151 while ((pending = atomic_xchg(&bfin_ipi_data->bits, 0)) != 0) {
152 msg = 0;
153 do {
154 msg = find_next_bit(&pending, BITS_PER_LONG, msg + 1);
155 switch (msg) {
156 case BFIN_IPI_TIMER:
157 ipi_timer();
158 break;
159 case BFIN_IPI_RESCHEDULE:
160 scheduler_ipi();
161 break;
162 case BFIN_IPI_CALL_FUNC:
163 generic_smp_call_function_interrupt();
164 break;
165 case BFIN_IPI_CPU_STOP:
166 ipi_cpu_stop(cpu);
167 break;
168 default:
169 goto out;
171 atomic_dec(&bfin_ipi_data->count);
172 } while (msg < BITS_PER_LONG);
175 out:
176 return IRQ_HANDLED;
179 static void bfin_ipi_init(void)
181 unsigned int cpu;
182 struct ipi_data *bfin_ipi_data;
183 for_each_possible_cpu(cpu) {
184 bfin_ipi_data = &per_cpu(bfin_ipi, cpu);
185 atomic_set(&bfin_ipi_data->bits, 0);
186 atomic_set(&bfin_ipi_data->count, 0);
190 void send_ipi(const struct cpumask *cpumask, enum ipi_message_type msg)
192 unsigned int cpu;
193 struct ipi_data *bfin_ipi_data;
194 unsigned long flags;
196 local_irq_save(flags);
197 for_each_cpu(cpu, cpumask) {
198 bfin_ipi_data = &per_cpu(bfin_ipi, cpu);
199 atomic_or((1 << msg), &bfin_ipi_data->bits);
200 atomic_inc(&bfin_ipi_data->count);
202 local_irq_restore(flags);
203 smp_wmb();
204 for_each_cpu(cpu, cpumask)
205 platform_send_ipi_cpu(cpu, IRQ_SUPPLE_1);
208 void arch_send_call_function_single_ipi(int cpu)
210 send_ipi(cpumask_of(cpu), BFIN_IPI_CALL_FUNC);
213 void arch_send_call_function_ipi_mask(const struct cpumask *mask)
215 send_ipi(mask, BFIN_IPI_CALL_FUNC);
218 void smp_send_reschedule(int cpu)
220 send_ipi(cpumask_of(cpu), BFIN_IPI_RESCHEDULE);
222 return;
225 void smp_send_msg(const struct cpumask *mask, unsigned long type)
227 send_ipi(mask, type);
230 void smp_timer_broadcast(const struct cpumask *mask)
232 smp_send_msg(mask, BFIN_IPI_TIMER);
235 void smp_send_stop(void)
237 cpumask_t callmap;
239 preempt_disable();
240 cpumask_copy(&callmap, cpu_online_mask);
241 cpumask_clear_cpu(smp_processor_id(), &callmap);
242 if (!cpumask_empty(&callmap))
243 send_ipi(&callmap, BFIN_IPI_CPU_STOP);
245 preempt_enable();
247 return;
250 int __cpu_up(unsigned int cpu, struct task_struct *idle)
252 int ret;
254 secondary_stack = task_stack_page(idle) + THREAD_SIZE;
256 ret = platform_boot_secondary(cpu, idle);
258 secondary_stack = NULL;
260 return ret;
263 static void setup_secondary(unsigned int cpu)
265 unsigned long ilat;
267 bfin_write_IMASK(0);
268 CSYNC();
269 ilat = bfin_read_ILAT();
270 CSYNC();
271 bfin_write_ILAT(ilat);
272 CSYNC();
274 /* Enable interrupt levels IVG7-15. IARs have been already
275 * programmed by the boot CPU. */
276 bfin_irq_flags |= IMASK_IVG15 |
277 IMASK_IVG14 | IMASK_IVG13 | IMASK_IVG12 | IMASK_IVG11 |
278 IMASK_IVG10 | IMASK_IVG9 | IMASK_IVG8 | IMASK_IVG7 | IMASK_IVGHW;
281 void secondary_start_kernel(void)
283 unsigned int cpu = smp_processor_id();
284 struct mm_struct *mm = &init_mm;
286 if (_bfin_swrst & SWRST_DBL_FAULT_B) {
287 printk(KERN_EMERG "CoreB Recovering from DOUBLE FAULT event\n");
288 #ifdef CONFIG_DEBUG_DOUBLEFAULT
289 printk(KERN_EMERG " While handling exception (EXCAUSE = %#x) at %pF\n",
290 initial_pda_coreb.seqstat_doublefault & SEQSTAT_EXCAUSE,
291 initial_pda_coreb.retx_doublefault);
292 printk(KERN_NOTICE " DCPLB_FAULT_ADDR: %pF\n",
293 initial_pda_coreb.dcplb_doublefault_addr);
294 printk(KERN_NOTICE " ICPLB_FAULT_ADDR: %pF\n",
295 initial_pda_coreb.icplb_doublefault_addr);
296 #endif
297 printk(KERN_NOTICE " The instruction at %pF caused a double exception\n",
298 initial_pda_coreb.retx);
302 * We want the D-cache to be enabled early, in case the atomic
303 * support code emulates cache coherence (see
304 * __ARCH_SYNC_CORE_DCACHE).
306 init_exception_vectors();
308 local_irq_disable();
310 /* Attach the new idle task to the global mm. */
311 mmget(mm);
312 mmgrab(mm);
313 current->active_mm = mm;
315 preempt_disable();
317 setup_secondary(cpu);
319 platform_secondary_init(cpu);
320 /* setup local core timer */
321 bfin_local_timer_setup();
323 local_irq_enable();
325 bfin_setup_caches(cpu);
327 notify_cpu_starting(cpu);
329 * Calibrate loops per jiffy value.
330 * IRQs need to be enabled here - D-cache can be invalidated
331 * in timer irq handler, so core B can read correct jiffies.
333 calibrate_delay();
335 /* We are done with local CPU inits, unblock the boot CPU. */
336 set_cpu_online(cpu, true);
337 cpu_startup_entry(CPUHP_AP_ONLINE_IDLE);
340 void __init smp_prepare_boot_cpu(void)
344 void __init smp_prepare_cpus(unsigned int max_cpus)
346 platform_prepare_cpus(max_cpus);
347 bfin_ipi_init();
348 platform_request_ipi(IRQ_SUPPLE_0, ipi_handler_int0);
349 platform_request_ipi(IRQ_SUPPLE_1, ipi_handler_int1);
352 void __init smp_cpus_done(unsigned int max_cpus)
354 unsigned long bogosum = 0;
355 unsigned int cpu;
357 for_each_online_cpu(cpu)
358 bogosum += loops_per_jiffy;
360 printk(KERN_INFO "SMP: Total of %d processors activated "
361 "(%lu.%02lu BogoMIPS).\n",
362 num_online_cpus(),
363 bogosum / (500000/HZ),
364 (bogosum / (5000/HZ)) % 100);
367 void smp_icache_flush_range_others(unsigned long start, unsigned long end)
369 smp_flush_data.start = start;
370 smp_flush_data.end = end;
372 preempt_disable();
373 if (smp_call_function(&ipi_flush_icache, &smp_flush_data, 1))
374 printk(KERN_WARNING "SMP: failed to run I-cache flush request on other CPUs\n");
375 preempt_enable();
377 EXPORT_SYMBOL_GPL(smp_icache_flush_range_others);
379 #ifdef __ARCH_SYNC_CORE_ICACHE
380 unsigned long icache_invld_count[NR_CPUS];
381 void resync_core_icache(void)
383 unsigned int cpu = get_cpu();
384 blackfin_invalidate_entire_icache();
385 icache_invld_count[cpu]++;
386 put_cpu();
388 EXPORT_SYMBOL(resync_core_icache);
389 #endif
391 #ifdef __ARCH_SYNC_CORE_DCACHE
392 unsigned long dcache_invld_count[NR_CPUS];
393 unsigned long barrier_mask __attribute__ ((__section__(".l2.bss")));
395 void resync_core_dcache(void)
397 unsigned int cpu = get_cpu();
398 blackfin_invalidate_entire_dcache();
399 dcache_invld_count[cpu]++;
400 put_cpu();
402 EXPORT_SYMBOL(resync_core_dcache);
403 #endif
405 #ifdef CONFIG_HOTPLUG_CPU
406 int __cpu_disable(void)
408 unsigned int cpu = smp_processor_id();
410 if (cpu == 0)
411 return -EPERM;
413 set_cpu_online(cpu, false);
414 return 0;
417 int __cpu_die(unsigned int cpu)
419 return cpu_wait_death(cpu, 5);
422 void cpu_die(void)
424 (void)cpu_report_death();
426 atomic_dec(&init_mm.mm_users);
427 atomic_dec(&init_mm.mm_count);
429 local_irq_disable();
430 platform_cpu_die();
432 #endif