Merge tag 'drm-sii902x' of github.com:bbrezillon/linux-at91 into drm-next
[linux-2.6/btrfs-unstable.git] / arch / xtensa / kernel / smp.c
blobfc4ad21a5ed44f9b170d9587c0934e581f75cd01
1 /*
2 * Xtensa SMP support functions.
4 * This file is subject to the terms and conditions of the GNU General Public
5 * License. See the file "COPYING" in the main directory of this archive
6 * for more details.
8 * Copyright (C) 2008 - 2013 Tensilica Inc.
10 * Chris Zankel <chris@zankel.net>
11 * Joe Taylor <joe@tensilica.com>
12 * Pete Delaney <piet@tensilica.com
15 #include <linux/cpu.h>
16 #include <linux/cpumask.h>
17 #include <linux/delay.h>
18 #include <linux/init.h>
19 #include <linux/interrupt.h>
20 #include <linux/irqdomain.h>
21 #include <linux/irq.h>
22 #include <linux/kdebug.h>
23 #include <linux/module.h>
24 #include <linux/reboot.h>
25 #include <linux/seq_file.h>
26 #include <linux/smp.h>
27 #include <linux/thread_info.h>
29 #include <asm/cacheflush.h>
30 #include <asm/kdebug.h>
31 #include <asm/mmu_context.h>
32 #include <asm/mxregs.h>
33 #include <asm/platform.h>
34 #include <asm/tlbflush.h>
35 #include <asm/traps.h>
37 #ifdef CONFIG_SMP
38 # if XCHAL_HAVE_S32C1I == 0
39 # error "The S32C1I option is required for SMP."
40 # endif
41 #endif
43 static void system_invalidate_dcache_range(unsigned long start,
44 unsigned long size);
45 static void system_flush_invalidate_dcache_range(unsigned long start,
46 unsigned long size);
48 /* IPI (Inter Process Interrupt) */
50 #define IPI_IRQ 0
52 static irqreturn_t ipi_interrupt(int irq, void *dev_id);
53 static struct irqaction ipi_irqaction = {
54 .handler = ipi_interrupt,
55 .flags = IRQF_PERCPU,
56 .name = "ipi",
59 void ipi_init(void)
61 unsigned irq = irq_create_mapping(NULL, IPI_IRQ);
62 setup_irq(irq, &ipi_irqaction);
65 static inline unsigned int get_core_count(void)
67 /* Bits 18..21 of SYSCFGID contain the core count minus 1. */
68 unsigned int syscfgid = get_er(SYSCFGID);
69 return ((syscfgid >> 18) & 0xf) + 1;
72 static inline int get_core_id(void)
74 /* Bits 0...18 of SYSCFGID contain the core id */
75 unsigned int core_id = get_er(SYSCFGID);
76 return core_id & 0x3fff;
79 void __init smp_prepare_cpus(unsigned int max_cpus)
81 unsigned i;
83 for (i = 0; i < max_cpus; ++i)
84 set_cpu_present(i, true);
87 void __init smp_init_cpus(void)
89 unsigned i;
90 unsigned int ncpus = get_core_count();
91 unsigned int core_id = get_core_id();
93 pr_info("%s: Core Count = %d\n", __func__, ncpus);
94 pr_info("%s: Core Id = %d\n", __func__, core_id);
96 for (i = 0; i < ncpus; ++i)
97 set_cpu_possible(i, true);
100 void __init smp_prepare_boot_cpu(void)
102 unsigned int cpu = smp_processor_id();
103 BUG_ON(cpu != 0);
104 cpu_asid_cache(cpu) = ASID_USER_FIRST;
107 void __init smp_cpus_done(unsigned int max_cpus)
111 static int boot_secondary_processors = 1; /* Set with xt-gdb via .xt-gdb */
112 static DECLARE_COMPLETION(cpu_running);
114 void secondary_start_kernel(void)
116 struct mm_struct *mm = &init_mm;
117 unsigned int cpu = smp_processor_id();
119 init_mmu();
121 #ifdef CONFIG_DEBUG_KERNEL
122 if (boot_secondary_processors == 0) {
123 pr_debug("%s: boot_secondary_processors:%d; Hanging cpu:%d\n",
124 __func__, boot_secondary_processors, cpu);
125 for (;;)
126 __asm__ __volatile__ ("waiti " __stringify(LOCKLEVEL));
129 pr_debug("%s: boot_secondary_processors:%d; Booting cpu:%d\n",
130 __func__, boot_secondary_processors, cpu);
131 #endif
132 /* Init EXCSAVE1 */
134 secondary_trap_init();
136 /* All kernel threads share the same mm context. */
138 atomic_inc(&mm->mm_users);
139 atomic_inc(&mm->mm_count);
140 current->active_mm = mm;
141 cpumask_set_cpu(cpu, mm_cpumask(mm));
142 enter_lazy_tlb(mm, current);
144 preempt_disable();
145 trace_hardirqs_off();
147 calibrate_delay();
149 notify_cpu_starting(cpu);
151 secondary_init_irq();
152 local_timer_setup(cpu);
154 set_cpu_online(cpu, true);
156 local_irq_enable();
158 complete(&cpu_running);
160 cpu_startup_entry(CPUHP_AP_ONLINE_IDLE);
163 static void mx_cpu_start(void *p)
165 unsigned cpu = (unsigned)p;
166 unsigned long run_stall_mask = get_er(MPSCORE);
168 set_er(run_stall_mask & ~(1u << cpu), MPSCORE);
169 pr_debug("%s: cpu: %d, run_stall_mask: %lx ---> %lx\n",
170 __func__, cpu, run_stall_mask, get_er(MPSCORE));
173 static void mx_cpu_stop(void *p)
175 unsigned cpu = (unsigned)p;
176 unsigned long run_stall_mask = get_er(MPSCORE);
178 set_er(run_stall_mask | (1u << cpu), MPSCORE);
179 pr_debug("%s: cpu: %d, run_stall_mask: %lx ---> %lx\n",
180 __func__, cpu, run_stall_mask, get_er(MPSCORE));
183 #ifdef CONFIG_HOTPLUG_CPU
184 unsigned long cpu_start_id __cacheline_aligned;
185 #endif
186 unsigned long cpu_start_ccount;
188 static int boot_secondary(unsigned int cpu, struct task_struct *ts)
190 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
191 unsigned long ccount;
192 int i;
194 #ifdef CONFIG_HOTPLUG_CPU
195 cpu_start_id = cpu;
196 system_flush_invalidate_dcache_range(
197 (unsigned long)&cpu_start_id, sizeof(cpu_start_id));
198 #endif
199 smp_call_function_single(0, mx_cpu_start, (void *)cpu, 1);
201 for (i = 0; i < 2; ++i) {
203 ccount = get_ccount();
204 while (!ccount);
206 cpu_start_ccount = ccount;
208 while (time_before(jiffies, timeout)) {
209 mb();
210 if (!cpu_start_ccount)
211 break;
214 if (cpu_start_ccount) {
215 smp_call_function_single(0, mx_cpu_stop,
216 (void *)cpu, 1);
217 cpu_start_ccount = 0;
218 return -EIO;
221 return 0;
224 int __cpu_up(unsigned int cpu, struct task_struct *idle)
226 int ret = 0;
228 if (cpu_asid_cache(cpu) == 0)
229 cpu_asid_cache(cpu) = ASID_USER_FIRST;
231 start_info.stack = (unsigned long)task_pt_regs(idle);
232 wmb();
234 pr_debug("%s: Calling wakeup_secondary(cpu:%d, idle:%p, sp: %08lx)\n",
235 __func__, cpu, idle, start_info.stack);
237 ret = boot_secondary(cpu, idle);
238 if (ret == 0) {
239 wait_for_completion_timeout(&cpu_running,
240 msecs_to_jiffies(1000));
241 if (!cpu_online(cpu))
242 ret = -EIO;
245 if (ret)
246 pr_err("CPU %u failed to boot\n", cpu);
248 return ret;
251 #ifdef CONFIG_HOTPLUG_CPU
254 * __cpu_disable runs on the processor to be shutdown.
256 int __cpu_disable(void)
258 unsigned int cpu = smp_processor_id();
261 * Take this CPU offline. Once we clear this, we can't return,
262 * and we must not schedule until we're ready to give up the cpu.
264 set_cpu_online(cpu, false);
267 * OK - migrate IRQs away from this CPU
269 migrate_irqs();
272 * Flush user cache and TLB mappings, and then remove this CPU
273 * from the vm mask set of all processes.
275 local_flush_cache_all();
276 local_flush_tlb_all();
277 invalidate_page_directory();
279 clear_tasks_mm_cpumask(cpu);
281 return 0;
284 static void platform_cpu_kill(unsigned int cpu)
286 smp_call_function_single(0, mx_cpu_stop, (void *)cpu, true);
290 * called on the thread which is asking for a CPU to be shutdown -
291 * waits until shutdown has completed, or it is timed out.
293 void __cpu_die(unsigned int cpu)
295 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
296 while (time_before(jiffies, timeout)) {
297 system_invalidate_dcache_range((unsigned long)&cpu_start_id,
298 sizeof(cpu_start_id));
299 if (cpu_start_id == -cpu) {
300 platform_cpu_kill(cpu);
301 return;
304 pr_err("CPU%u: unable to kill\n", cpu);
307 void arch_cpu_idle_dead(void)
309 cpu_die();
312 * Called from the idle thread for the CPU which has been shutdown.
314 * Note that we disable IRQs here, but do not re-enable them
315 * before returning to the caller. This is also the behaviour
316 * of the other hotplug-cpu capable cores, so presumably coming
317 * out of idle fixes this.
319 void __ref cpu_die(void)
321 idle_task_exit();
322 local_irq_disable();
323 __asm__ __volatile__(
324 " movi a2, cpu_restart\n"
325 " jx a2\n");
328 #endif /* CONFIG_HOTPLUG_CPU */
330 enum ipi_msg_type {
331 IPI_RESCHEDULE = 0,
332 IPI_CALL_FUNC,
333 IPI_CPU_STOP,
334 IPI_MAX
337 static const struct {
338 const char *short_text;
339 const char *long_text;
340 } ipi_text[] = {
341 { .short_text = "RES", .long_text = "Rescheduling interrupts" },
342 { .short_text = "CAL", .long_text = "Function call interrupts" },
343 { .short_text = "DIE", .long_text = "CPU shutdown interrupts" },
346 struct ipi_data {
347 unsigned long ipi_count[IPI_MAX];
350 static DEFINE_PER_CPU(struct ipi_data, ipi_data);
352 static void send_ipi_message(const struct cpumask *callmask,
353 enum ipi_msg_type msg_id)
355 int index;
356 unsigned long mask = 0;
358 for_each_cpu(index, callmask)
359 if (index != smp_processor_id())
360 mask |= 1 << index;
362 set_er(mask, MIPISET(msg_id));
365 void arch_send_call_function_ipi_mask(const struct cpumask *mask)
367 send_ipi_message(mask, IPI_CALL_FUNC);
370 void arch_send_call_function_single_ipi(int cpu)
372 send_ipi_message(cpumask_of(cpu), IPI_CALL_FUNC);
375 void smp_send_reschedule(int cpu)
377 send_ipi_message(cpumask_of(cpu), IPI_RESCHEDULE);
380 void smp_send_stop(void)
382 struct cpumask targets;
384 cpumask_copy(&targets, cpu_online_mask);
385 cpumask_clear_cpu(smp_processor_id(), &targets);
386 send_ipi_message(&targets, IPI_CPU_STOP);
389 static void ipi_cpu_stop(unsigned int cpu)
391 set_cpu_online(cpu, false);
392 machine_halt();
395 irqreturn_t ipi_interrupt(int irq, void *dev_id)
397 unsigned int cpu = smp_processor_id();
398 struct ipi_data *ipi = &per_cpu(ipi_data, cpu);
399 unsigned int msg;
400 unsigned i;
402 msg = get_er(MIPICAUSE(cpu));
403 for (i = 0; i < IPI_MAX; i++)
404 if (msg & (1 << i)) {
405 set_er(1 << i, MIPICAUSE(cpu));
406 ++ipi->ipi_count[i];
409 if (msg & (1 << IPI_RESCHEDULE))
410 scheduler_ipi();
411 if (msg & (1 << IPI_CALL_FUNC))
412 generic_smp_call_function_interrupt();
413 if (msg & (1 << IPI_CPU_STOP))
414 ipi_cpu_stop(cpu);
416 return IRQ_HANDLED;
419 void show_ipi_list(struct seq_file *p, int prec)
421 unsigned int cpu;
422 unsigned i;
424 for (i = 0; i < IPI_MAX; ++i) {
425 seq_printf(p, "%*s:", prec, ipi_text[i].short_text);
426 for_each_online_cpu(cpu)
427 seq_printf(p, " %10lu",
428 per_cpu(ipi_data, cpu).ipi_count[i]);
429 seq_printf(p, " %s\n", ipi_text[i].long_text);
433 int setup_profiling_timer(unsigned int multiplier)
435 pr_debug("setup_profiling_timer %d\n", multiplier);
436 return 0;
439 /* TLB flush functions */
441 struct flush_data {
442 struct vm_area_struct *vma;
443 unsigned long addr1;
444 unsigned long addr2;
447 static void ipi_flush_tlb_all(void *arg)
449 local_flush_tlb_all();
452 void flush_tlb_all(void)
454 on_each_cpu(ipi_flush_tlb_all, NULL, 1);
457 static void ipi_flush_tlb_mm(void *arg)
459 local_flush_tlb_mm(arg);
462 void flush_tlb_mm(struct mm_struct *mm)
464 on_each_cpu(ipi_flush_tlb_mm, mm, 1);
467 static void ipi_flush_tlb_page(void *arg)
469 struct flush_data *fd = arg;
470 local_flush_tlb_page(fd->vma, fd->addr1);
473 void flush_tlb_page(struct vm_area_struct *vma, unsigned long addr)
475 struct flush_data fd = {
476 .vma = vma,
477 .addr1 = addr,
479 on_each_cpu(ipi_flush_tlb_page, &fd, 1);
482 static void ipi_flush_tlb_range(void *arg)
484 struct flush_data *fd = arg;
485 local_flush_tlb_range(fd->vma, fd->addr1, fd->addr2);
488 void flush_tlb_range(struct vm_area_struct *vma,
489 unsigned long start, unsigned long end)
491 struct flush_data fd = {
492 .vma = vma,
493 .addr1 = start,
494 .addr2 = end,
496 on_each_cpu(ipi_flush_tlb_range, &fd, 1);
499 static void ipi_flush_tlb_kernel_range(void *arg)
501 struct flush_data *fd = arg;
502 local_flush_tlb_kernel_range(fd->addr1, fd->addr2);
505 void flush_tlb_kernel_range(unsigned long start, unsigned long end)
507 struct flush_data fd = {
508 .addr1 = start,
509 .addr2 = end,
511 on_each_cpu(ipi_flush_tlb_kernel_range, &fd, 1);
514 /* Cache flush functions */
516 static void ipi_flush_cache_all(void *arg)
518 local_flush_cache_all();
521 void flush_cache_all(void)
523 on_each_cpu(ipi_flush_cache_all, NULL, 1);
526 static void ipi_flush_cache_page(void *arg)
528 struct flush_data *fd = arg;
529 local_flush_cache_page(fd->vma, fd->addr1, fd->addr2);
532 void flush_cache_page(struct vm_area_struct *vma,
533 unsigned long address, unsigned long pfn)
535 struct flush_data fd = {
536 .vma = vma,
537 .addr1 = address,
538 .addr2 = pfn,
540 on_each_cpu(ipi_flush_cache_page, &fd, 1);
543 static void ipi_flush_cache_range(void *arg)
545 struct flush_data *fd = arg;
546 local_flush_cache_range(fd->vma, fd->addr1, fd->addr2);
549 void flush_cache_range(struct vm_area_struct *vma,
550 unsigned long start, unsigned long end)
552 struct flush_data fd = {
553 .vma = vma,
554 .addr1 = start,
555 .addr2 = end,
557 on_each_cpu(ipi_flush_cache_range, &fd, 1);
560 static void ipi_flush_icache_range(void *arg)
562 struct flush_data *fd = arg;
563 local_flush_icache_range(fd->addr1, fd->addr2);
566 void flush_icache_range(unsigned long start, unsigned long end)
568 struct flush_data fd = {
569 .addr1 = start,
570 .addr2 = end,
572 on_each_cpu(ipi_flush_icache_range, &fd, 1);
574 EXPORT_SYMBOL(flush_icache_range);
576 /* ------------------------------------------------------------------------- */
578 static void ipi_invalidate_dcache_range(void *arg)
580 struct flush_data *fd = arg;
581 __invalidate_dcache_range(fd->addr1, fd->addr2);
584 static void system_invalidate_dcache_range(unsigned long start,
585 unsigned long size)
587 struct flush_data fd = {
588 .addr1 = start,
589 .addr2 = size,
591 on_each_cpu(ipi_invalidate_dcache_range, &fd, 1);
594 static void ipi_flush_invalidate_dcache_range(void *arg)
596 struct flush_data *fd = arg;
597 __flush_invalidate_dcache_range(fd->addr1, fd->addr2);
600 static void system_flush_invalidate_dcache_range(unsigned long start,
601 unsigned long size)
603 struct flush_data fd = {
604 .addr1 = start,
605 .addr2 = size,
607 on_each_cpu(ipi_flush_invalidate_dcache_range, &fd, 1);