2 * linux/arch/arm/kernel/smp.c
4 * Copyright (C) 2002 ARM Limited, All Rights Reserved.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
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/err.h>
21 #include <linux/cpu.h>
22 #include <linux/smp.h>
23 #include <linux/seq_file.h>
24 #include <linux/irq.h>
26 #include <asm/atomic.h>
27 #include <asm/cacheflush.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/tlbflush.h>
34 #include <asm/ptrace.h>
35 #include <asm/cputype.h>
38 * as from 2.5, kernels no longer have an init_tasks structure
39 * so we need some other way of telling a new secondary core
40 * where to place its SVC stack
42 struct secondary_data secondary_data;
45 * structures for inter-processor calls
46 * - A collection of single bit ipi messages.
50 unsigned long ipi_count;
54 static DEFINE_PER_CPU(struct ipi_data, ipi_data) = {
55 .lock = SPIN_LOCK_UNLOCKED,
66 int __cpuinit __cpu_up(unsigned int cpu)
68 struct cpuinfo_arm *ci = &per_cpu(cpu_data, cpu);
69 struct task_struct *idle = ci->idle;
75 * Spawn a new process manually, if not already done.
76 * Grab a pointer to its task struct so we can mess with it
79 idle = fork_idle(cpu);
81 printk(KERN_ERR "CPU%u: fork() failed\n", cpu);
88 * Allocate initial page tables to allow the new CPU to
89 * enable the MMU safely. This essentially means a set
90 * of our "standard" page tables, with the addition of
91 * a 1:1 mapping for the physical address of the kernel.
93 pgd = pgd_alloc(&init_mm);
94 pmd = pmd_offset(pgd + pgd_index(PHYS_OFFSET), PHYS_OFFSET);
95 *pmd = __pmd((PHYS_OFFSET & PGDIR_MASK) |
96 PMD_TYPE_SECT | PMD_SECT_AP_WRITE);
100 * We need to tell the secondary core where to find
101 * its stack and the page tables.
103 secondary_data.stack = task_stack_page(idle) + THREAD_START_SP;
104 secondary_data.pgdir = virt_to_phys(pgd);
108 * Now bring the CPU into our world.
110 ret = boot_secondary(cpu, idle);
112 unsigned long timeout;
115 * CPU was successfully started, wait for it
116 * to come online or time out.
118 timeout = jiffies + HZ;
119 while (time_before(jiffies, timeout)) {
127 if (!cpu_online(cpu))
131 secondary_data.stack = NULL;
132 secondary_data.pgdir = 0;
135 clean_pmd_entry(pmd);
136 pgd_free(&init_mm, pgd);
139 printk(KERN_CRIT "CPU%u: processor failed to boot\n", cpu);
142 * FIXME: We need to clean up the new idle thread. --rmk
149 #ifdef CONFIG_HOTPLUG_CPU
151 * __cpu_disable runs on the processor to be shutdown.
153 int __cpuexit __cpu_disable(void)
155 unsigned int cpu = smp_processor_id();
156 struct task_struct *p;
159 ret = mach_cpu_disable(cpu);
164 * Take this CPU offline. Once we clear this, we can't return,
165 * and we must not schedule until we're ready to give up the cpu.
167 cpu_clear(cpu, cpu_online_map);
170 * OK - migrate IRQs away from this CPU
175 * Stop the local timer for this CPU.
180 * Flush user cache and TLB mappings, and then remove this CPU
181 * from the vm mask set of all processes.
184 local_flush_tlb_all();
186 read_lock(&tasklist_lock);
187 for_each_process(p) {
189 cpu_clear(cpu, p->mm->cpu_vm_mask);
191 read_unlock(&tasklist_lock);
197 * called on the thread which is asking for a CPU to be shutdown -
198 * waits until shutdown has completed, or it is timed out.
200 void __cpuexit __cpu_die(unsigned int cpu)
202 if (!platform_cpu_kill(cpu))
203 printk("CPU%u: unable to kill\n", cpu);
207 * Called from the idle thread for the CPU which has been shutdown.
209 * Note that we disable IRQs here, but do not re-enable them
210 * before returning to the caller. This is also the behaviour
211 * of the other hotplug-cpu capable cores, so presumably coming
212 * out of idle fixes this.
214 void __cpuexit cpu_die(void)
216 unsigned int cpu = smp_processor_id();
222 * actual CPU shutdown procedure is at least platform (if not
225 platform_cpu_die(cpu);
228 * Do not return to the idle loop - jump back to the secondary
229 * cpu initialisation. There's some initialisation which needs
230 * to be repeated to undo the effects of taking the CPU offline.
232 __asm__("mov sp, %0\n"
233 " b secondary_start_kernel"
235 : "r" (task_stack_page(current) + THREAD_SIZE - 8));
237 #endif /* CONFIG_HOTPLUG_CPU */
240 * This is the secondary CPU boot entry. We're using this CPUs
241 * idle thread stack, but a set of temporary page tables.
243 asmlinkage void __cpuinit secondary_start_kernel(void)
245 struct mm_struct *mm = &init_mm;
246 unsigned int cpu = smp_processor_id();
248 printk("CPU%u: Booted secondary processor\n", cpu);
251 * All kernel threads share the same mm context; grab a
252 * reference and switch to it.
254 atomic_inc(&mm->mm_users);
255 atomic_inc(&mm->mm_count);
256 current->active_mm = mm;
257 cpu_set(cpu, mm->cpu_vm_mask);
258 cpu_switch_mm(mm->pgd, mm);
259 enter_lazy_tlb(mm, current);
260 local_flush_tlb_all();
266 * Give the platform a chance to do its own initialisation.
268 platform_secondary_init(cpu);
271 * Enable local interrupts.
273 notify_cpu_starting(cpu);
278 * Setup local timer for this CPU.
284 smp_store_cpu_info(cpu);
287 * OK, now it's safe to let the boot CPU continue
289 cpu_set(cpu, cpu_online_map);
292 * OK, it's off to the idle thread for us
298 * Called by both boot and secondaries to move global data into
299 * per-processor storage.
301 void __cpuinit smp_store_cpu_info(unsigned int cpuid)
303 struct cpuinfo_arm *cpu_info = &per_cpu(cpu_data, cpuid);
305 cpu_info->loops_per_jiffy = loops_per_jiffy;
308 void __init smp_cpus_done(unsigned int max_cpus)
311 unsigned long bogosum = 0;
313 for_each_online_cpu(cpu)
314 bogosum += per_cpu(cpu_data, cpu).loops_per_jiffy;
316 printk(KERN_INFO "SMP: Total of %d processors activated "
317 "(%lu.%02lu BogoMIPS).\n",
319 bogosum / (500000/HZ),
320 (bogosum / (5000/HZ)) % 100);
323 void __init smp_prepare_boot_cpu(void)
325 unsigned int cpu = smp_processor_id();
327 per_cpu(cpu_data, cpu).idle = current;
330 static void send_ipi_message(const struct cpumask *mask, enum ipi_msg_type msg)
335 local_irq_save(flags);
337 for_each_cpu(cpu, mask) {
338 struct ipi_data *ipi = &per_cpu(ipi_data, cpu);
340 spin_lock(&ipi->lock);
341 ipi->bits |= 1 << msg;
342 spin_unlock(&ipi->lock);
346 * Call the platform specific cross-CPU call function.
348 smp_cross_call(mask);
350 local_irq_restore(flags);
353 void arch_send_call_function_ipi_mask(const struct cpumask *mask)
355 send_ipi_message(mask, IPI_CALL_FUNC);
358 void arch_send_call_function_single_ipi(int cpu)
360 send_ipi_message(cpumask_of(cpu), IPI_CALL_FUNC_SINGLE);
363 void show_ipi_list(struct seq_file *p)
369 for_each_present_cpu(cpu)
370 seq_printf(p, " %10lu", per_cpu(ipi_data, cpu).ipi_count);
375 void show_local_irqs(struct seq_file *p)
379 seq_printf(p, "LOC: ");
381 for_each_present_cpu(cpu)
382 seq_printf(p, "%10u ", irq_stat[cpu].local_timer_irqs);
387 static void ipi_timer(void)
390 local_timer_interrupt();
394 #ifdef CONFIG_LOCAL_TIMERS
395 asmlinkage void __exception do_local_timer(struct pt_regs *regs)
397 struct pt_regs *old_regs = set_irq_regs(regs);
398 int cpu = smp_processor_id();
400 if (local_timer_ack()) {
401 irq_stat[cpu].local_timer_irqs++;
405 set_irq_regs(old_regs);
409 static DEFINE_SPINLOCK(stop_lock);
412 * ipi_cpu_stop - handle IPI from smp_send_stop()
414 static void ipi_cpu_stop(unsigned int cpu)
416 spin_lock(&stop_lock);
417 printk(KERN_CRIT "CPU%u: stopping\n", cpu);
419 spin_unlock(&stop_lock);
421 cpu_clear(cpu, cpu_online_map);
431 * Main handler for inter-processor interrupts
433 * For ARM, the ipimask now only identifies a single
434 * category of IPI (Bit 1 IPIs have been replaced by a
435 * different mechanism):
437 * Bit 0 - Inter-processor function call
439 asmlinkage void __exception do_IPI(struct pt_regs *regs)
441 unsigned int cpu = smp_processor_id();
442 struct ipi_data *ipi = &per_cpu(ipi_data, cpu);
443 struct pt_regs *old_regs = set_irq_regs(regs);
450 spin_lock(&ipi->lock);
453 spin_unlock(&ipi->lock);
461 nextmsg = msgs & -msgs;
463 nextmsg = ffz(~nextmsg);
472 * nothing more to do - eveything is
473 * done on the interrupt return path
478 generic_smp_call_function_interrupt();
481 case IPI_CALL_FUNC_SINGLE:
482 generic_smp_call_function_single_interrupt();
490 printk(KERN_CRIT "CPU%u: Unknown IPI message 0x%x\n",
497 set_irq_regs(old_regs);
500 void smp_send_reschedule(int cpu)
502 send_ipi_message(cpumask_of(cpu), IPI_RESCHEDULE);
505 void smp_timer_broadcast(const struct cpumask *mask)
507 send_ipi_message(mask, IPI_TIMER);
510 void smp_send_stop(void)
512 cpumask_t mask = cpu_online_map;
513 cpu_clear(smp_processor_id(), mask);
514 send_ipi_message(&mask, IPI_CPU_STOP);
520 int setup_profiling_timer(unsigned int multiplier)
526 on_each_cpu_mask(void (*func)(void *), void *info, int wait,
527 const struct cpumask *mask)
531 smp_call_function_many(mask, func, info, wait);
532 if (cpumask_test_cpu(smp_processor_id(), mask))
538 /**********************************************************************/
544 struct vm_area_struct *ta_vma;
545 unsigned long ta_start;
546 unsigned long ta_end;
549 /* all SMP configurations have the extended CPUID registers */
550 static inline int tlb_ops_need_broadcast(void)
552 return ((read_cpuid_ext(CPUID_EXT_MMFR3) >> 12) & 0xf) < 2;
555 static inline void ipi_flush_tlb_all(void *ignored)
557 local_flush_tlb_all();
560 static inline void ipi_flush_tlb_mm(void *arg)
562 struct mm_struct *mm = (struct mm_struct *)arg;
564 local_flush_tlb_mm(mm);
567 static inline void ipi_flush_tlb_page(void *arg)
569 struct tlb_args *ta = (struct tlb_args *)arg;
571 local_flush_tlb_page(ta->ta_vma, ta->ta_start);
574 static inline void ipi_flush_tlb_kernel_page(void *arg)
576 struct tlb_args *ta = (struct tlb_args *)arg;
578 local_flush_tlb_kernel_page(ta->ta_start);
581 static inline void ipi_flush_tlb_range(void *arg)
583 struct tlb_args *ta = (struct tlb_args *)arg;
585 local_flush_tlb_range(ta->ta_vma, ta->ta_start, ta->ta_end);
588 static inline void ipi_flush_tlb_kernel_range(void *arg)
590 struct tlb_args *ta = (struct tlb_args *)arg;
592 local_flush_tlb_kernel_range(ta->ta_start, ta->ta_end);
595 void flush_tlb_all(void)
597 if (tlb_ops_need_broadcast())
598 on_each_cpu(ipi_flush_tlb_all, NULL, 1);
600 local_flush_tlb_all();
603 void flush_tlb_mm(struct mm_struct *mm)
605 if (tlb_ops_need_broadcast())
606 on_each_cpu_mask(ipi_flush_tlb_mm, mm, 1, &mm->cpu_vm_mask);
608 local_flush_tlb_mm(mm);
611 void flush_tlb_page(struct vm_area_struct *vma, unsigned long uaddr)
613 if (tlb_ops_need_broadcast()) {
617 on_each_cpu_mask(ipi_flush_tlb_page, &ta, 1, &vma->vm_mm->cpu_vm_mask);
619 local_flush_tlb_page(vma, uaddr);
622 void flush_tlb_kernel_page(unsigned long kaddr)
624 if (tlb_ops_need_broadcast()) {
627 on_each_cpu(ipi_flush_tlb_kernel_page, &ta, 1);
629 local_flush_tlb_kernel_page(kaddr);
632 void flush_tlb_range(struct vm_area_struct *vma,
633 unsigned long start, unsigned long end)
635 if (tlb_ops_need_broadcast()) {
640 on_each_cpu_mask(ipi_flush_tlb_range, &ta, 1, &vma->vm_mm->cpu_vm_mask);
642 local_flush_tlb_range(vma, start, end);
645 void flush_tlb_kernel_range(unsigned long start, unsigned long end)
647 if (tlb_ops_need_broadcast()) {
651 on_each_cpu(ipi_flush_tlb_kernel_range, &ta, 1);
653 local_flush_tlb_kernel_range(start, end);