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>
25 #include <linux/percpu.h>
26 #include <linux/clockchips.h>
28 #include <asm/atomic.h>
29 #include <asm/cacheflush.h>
31 #include <asm/cputype.h>
32 #include <asm/mmu_context.h>
33 #include <asm/pgtable.h>
34 #include <asm/pgalloc.h>
35 #include <asm/processor.h>
36 #include <asm/tlbflush.h>
37 #include <asm/ptrace.h>
38 #include <asm/localtimer.h>
41 * as from 2.5, kernels no longer have an init_tasks structure
42 * so we need some other way of telling a new secondary core
43 * where to place its SVC stack
45 struct secondary_data secondary_data
;
48 * structures for inter-processor calls
49 * - A collection of single bit ipi messages.
53 unsigned long ipi_count
;
57 static DEFINE_PER_CPU(struct ipi_data
, ipi_data
) = {
58 .lock
= SPIN_LOCK_UNLOCKED
,
69 int __cpuinit
__cpu_up(unsigned int cpu
)
71 struct cpuinfo_arm
*ci
= &per_cpu(cpu_data
, cpu
);
72 struct task_struct
*idle
= ci
->idle
;
78 * Spawn a new process manually, if not already done.
79 * Grab a pointer to its task struct so we can mess with it
82 idle
= fork_idle(cpu
);
84 printk(KERN_ERR
"CPU%u: fork() failed\n", cpu
);
91 * Allocate initial page tables to allow the new CPU to
92 * enable the MMU safely. This essentially means a set
93 * of our "standard" page tables, with the addition of
94 * a 1:1 mapping for the physical address of the kernel.
96 pgd
= pgd_alloc(&init_mm
);
97 pmd
= pmd_offset(pgd
+ pgd_index(PHYS_OFFSET
), PHYS_OFFSET
);
98 *pmd
= __pmd((PHYS_OFFSET
& PGDIR_MASK
) |
99 PMD_TYPE_SECT
| PMD_SECT_AP_WRITE
);
100 flush_pmd_entry(pmd
);
103 * We need to tell the secondary core where to find
104 * its stack and the page tables.
106 secondary_data
.stack
= task_stack_page(idle
) + THREAD_START_SP
;
107 secondary_data
.pgdir
= virt_to_phys(pgd
);
111 * Now bring the CPU into our world.
113 ret
= boot_secondary(cpu
, idle
);
115 unsigned long timeout
;
118 * CPU was successfully started, wait for it
119 * to come online or time out.
121 timeout
= jiffies
+ HZ
;
122 while (time_before(jiffies
, timeout
)) {
130 if (!cpu_online(cpu
))
134 secondary_data
.stack
= NULL
;
135 secondary_data
.pgdir
= 0;
138 clean_pmd_entry(pmd
);
139 pgd_free(&init_mm
, pgd
);
142 printk(KERN_CRIT
"CPU%u: processor failed to boot\n", cpu
);
145 * FIXME: We need to clean up the new idle thread. --rmk
152 #ifdef CONFIG_HOTPLUG_CPU
154 * __cpu_disable runs on the processor to be shutdown.
156 int __cpuexit
__cpu_disable(void)
158 unsigned int cpu
= smp_processor_id();
159 struct task_struct
*p
;
162 ret
= mach_cpu_disable(cpu
);
167 * Take this CPU offline. Once we clear this, we can't return,
168 * and we must not schedule until we're ready to give up the cpu.
170 set_cpu_online(cpu
, false);
173 * OK - migrate IRQs away from this CPU
178 * Stop the local timer for this CPU.
183 * Flush user cache and TLB mappings, and then remove this CPU
184 * from the vm mask set of all processes.
187 local_flush_tlb_all();
189 read_lock(&tasklist_lock
);
190 for_each_process(p
) {
192 cpu_clear(cpu
, p
->mm
->cpu_vm_mask
);
194 read_unlock(&tasklist_lock
);
200 * called on the thread which is asking for a CPU to be shutdown -
201 * waits until shutdown has completed, or it is timed out.
203 void __cpuexit
__cpu_die(unsigned int cpu
)
205 if (!platform_cpu_kill(cpu
))
206 printk("CPU%u: unable to kill\n", cpu
);
210 * Called from the idle thread for the CPU which has been shutdown.
212 * Note that we disable IRQs here, but do not re-enable them
213 * before returning to the caller. This is also the behaviour
214 * of the other hotplug-cpu capable cores, so presumably coming
215 * out of idle fixes this.
217 void __cpuexit
cpu_die(void)
219 unsigned int cpu
= smp_processor_id();
225 * actual CPU shutdown procedure is at least platform (if not
228 platform_cpu_die(cpu
);
231 * Do not return to the idle loop - jump back to the secondary
232 * cpu initialisation. There's some initialisation which needs
233 * to be repeated to undo the effects of taking the CPU offline.
235 __asm__("mov sp, %0\n"
236 " b secondary_start_kernel"
238 : "r" (task_stack_page(current
) + THREAD_SIZE
- 8));
240 #endif /* CONFIG_HOTPLUG_CPU */
243 * This is the secondary CPU boot entry. We're using this CPUs
244 * idle thread stack, but a set of temporary page tables.
246 asmlinkage
void __cpuinit
secondary_start_kernel(void)
248 struct mm_struct
*mm
= &init_mm
;
249 unsigned int cpu
= smp_processor_id();
251 printk("CPU%u: Booted secondary processor\n", cpu
);
254 * All kernel threads share the same mm context; grab a
255 * reference and switch to it.
257 atomic_inc(&mm
->mm_users
);
258 atomic_inc(&mm
->mm_count
);
259 current
->active_mm
= mm
;
260 cpu_set(cpu
, mm
->cpu_vm_mask
);
261 cpu_switch_mm(mm
->pgd
, mm
);
262 enter_lazy_tlb(mm
, current
);
263 local_flush_tlb_all();
269 * Give the platform a chance to do its own initialisation.
271 platform_secondary_init(cpu
);
274 * Enable local interrupts.
276 notify_cpu_starting(cpu
);
281 * Setup the percpu timer for this CPU.
283 percpu_timer_setup();
287 smp_store_cpu_info(cpu
);
290 * OK, now it's safe to let the boot CPU continue
292 set_cpu_online(cpu
, true);
295 * OK, it's off to the idle thread for us
301 * Called by both boot and secondaries to move global data into
302 * per-processor storage.
304 void __cpuinit
smp_store_cpu_info(unsigned int cpuid
)
306 struct cpuinfo_arm
*cpu_info
= &per_cpu(cpu_data
, cpuid
);
308 cpu_info
->loops_per_jiffy
= loops_per_jiffy
;
311 void __init
smp_cpus_done(unsigned int max_cpus
)
314 unsigned long bogosum
= 0;
316 for_each_online_cpu(cpu
)
317 bogosum
+= per_cpu(cpu_data
, cpu
).loops_per_jiffy
;
319 printk(KERN_INFO
"SMP: Total of %d processors activated "
320 "(%lu.%02lu BogoMIPS).\n",
322 bogosum
/ (500000/HZ
),
323 (bogosum
/ (5000/HZ
)) % 100);
326 void __init
smp_prepare_boot_cpu(void)
328 unsigned int cpu
= smp_processor_id();
330 per_cpu(cpu_data
, cpu
).idle
= current
;
333 static void send_ipi_message(const struct cpumask
*mask
, enum ipi_msg_type msg
)
338 local_irq_save(flags
);
340 for_each_cpu(cpu
, mask
) {
341 struct ipi_data
*ipi
= &per_cpu(ipi_data
, cpu
);
343 spin_lock(&ipi
->lock
);
344 ipi
->bits
|= 1 << msg
;
345 spin_unlock(&ipi
->lock
);
349 * Call the platform specific cross-CPU call function.
351 smp_cross_call(mask
);
353 local_irq_restore(flags
);
356 void arch_send_call_function_ipi_mask(const struct cpumask
*mask
)
358 send_ipi_message(mask
, IPI_CALL_FUNC
);
361 void arch_send_call_function_single_ipi(int cpu
)
363 send_ipi_message(cpumask_of(cpu
), IPI_CALL_FUNC_SINGLE
);
366 void show_ipi_list(struct seq_file
*p
)
372 for_each_present_cpu(cpu
)
373 seq_printf(p
, " %10lu", per_cpu(ipi_data
, cpu
).ipi_count
);
378 void show_local_irqs(struct seq_file
*p
)
382 seq_printf(p
, "LOC: ");
384 for_each_present_cpu(cpu
)
385 seq_printf(p
, "%10u ", irq_stat
[cpu
].local_timer_irqs
);
391 * Timer (local or broadcast) support
393 static DEFINE_PER_CPU(struct clock_event_device
, percpu_clockevent
);
395 static void ipi_timer(void)
397 struct clock_event_device
*evt
= &__get_cpu_var(percpu_clockevent
);
399 evt
->event_handler(evt
);
403 #ifdef CONFIG_LOCAL_TIMERS
404 asmlinkage
void __exception
do_local_timer(struct pt_regs
*regs
)
406 struct pt_regs
*old_regs
= set_irq_regs(regs
);
407 int cpu
= smp_processor_id();
409 if (local_timer_ack()) {
410 irq_stat
[cpu
].local_timer_irqs
++;
414 set_irq_regs(old_regs
);
418 #ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
419 static void smp_timer_broadcast(const struct cpumask
*mask
)
421 send_ipi_message(mask
, IPI_TIMER
);
424 static void broadcast_timer_set_mode(enum clock_event_mode mode
,
425 struct clock_event_device
*evt
)
429 static void local_timer_setup(struct clock_event_device
*evt
)
431 evt
->name
= "dummy_timer";
432 evt
->features
= CLOCK_EVT_FEAT_ONESHOT
|
433 CLOCK_EVT_FEAT_PERIODIC
|
434 CLOCK_EVT_FEAT_DUMMY
;
437 evt
->set_mode
= broadcast_timer_set_mode
;
438 evt
->broadcast
= smp_timer_broadcast
;
440 clockevents_register_device(evt
);
444 void __cpuinit
percpu_timer_setup(void)
446 unsigned int cpu
= smp_processor_id();
447 struct clock_event_device
*evt
= &per_cpu(percpu_clockevent
, cpu
);
449 evt
->cpumask
= cpumask_of(cpu
);
451 local_timer_setup(evt
);
454 static DEFINE_SPINLOCK(stop_lock
);
457 * ipi_cpu_stop - handle IPI from smp_send_stop()
459 static void ipi_cpu_stop(unsigned int cpu
)
461 spin_lock(&stop_lock
);
462 printk(KERN_CRIT
"CPU%u: stopping\n", cpu
);
464 spin_unlock(&stop_lock
);
466 set_cpu_online(cpu
, false);
476 * Main handler for inter-processor interrupts
478 * For ARM, the ipimask now only identifies a single
479 * category of IPI (Bit 1 IPIs have been replaced by a
480 * different mechanism):
482 * Bit 0 - Inter-processor function call
484 asmlinkage
void __exception
do_IPI(struct pt_regs
*regs
)
486 unsigned int cpu
= smp_processor_id();
487 struct ipi_data
*ipi
= &per_cpu(ipi_data
, cpu
);
488 struct pt_regs
*old_regs
= set_irq_regs(regs
);
495 spin_lock(&ipi
->lock
);
498 spin_unlock(&ipi
->lock
);
506 nextmsg
= msgs
& -msgs
;
508 nextmsg
= ffz(~nextmsg
);
517 * nothing more to do - eveything is
518 * done on the interrupt return path
523 generic_smp_call_function_interrupt();
526 case IPI_CALL_FUNC_SINGLE
:
527 generic_smp_call_function_single_interrupt();
535 printk(KERN_CRIT
"CPU%u: Unknown IPI message 0x%x\n",
542 set_irq_regs(old_regs
);
545 void smp_send_reschedule(int cpu
)
547 send_ipi_message(cpumask_of(cpu
), IPI_RESCHEDULE
);
550 void smp_send_stop(void)
552 cpumask_t mask
= cpu_online_map
;
553 cpu_clear(smp_processor_id(), mask
);
554 send_ipi_message(&mask
, IPI_CPU_STOP
);
560 int setup_profiling_timer(unsigned int multiplier
)
566 on_each_cpu_mask(void (*func
)(void *), void *info
, int wait
,
567 const struct cpumask
*mask
)
571 smp_call_function_many(mask
, func
, info
, wait
);
572 if (cpumask_test_cpu(smp_processor_id(), mask
))
578 /**********************************************************************/
584 struct vm_area_struct
*ta_vma
;
585 unsigned long ta_start
;
586 unsigned long ta_end
;
589 /* all SMP configurations have the extended CPUID registers */
590 static inline int tlb_ops_need_broadcast(void)
592 return ((read_cpuid_ext(CPUID_EXT_MMFR3
) >> 12) & 0xf) < 2;
595 static inline void ipi_flush_tlb_all(void *ignored
)
597 local_flush_tlb_all();
600 static inline void ipi_flush_tlb_mm(void *arg
)
602 struct mm_struct
*mm
= (struct mm_struct
*)arg
;
604 local_flush_tlb_mm(mm
);
607 static inline void ipi_flush_tlb_page(void *arg
)
609 struct tlb_args
*ta
= (struct tlb_args
*)arg
;
611 local_flush_tlb_page(ta
->ta_vma
, ta
->ta_start
);
614 static inline void ipi_flush_tlb_kernel_page(void *arg
)
616 struct tlb_args
*ta
= (struct tlb_args
*)arg
;
618 local_flush_tlb_kernel_page(ta
->ta_start
);
621 static inline void ipi_flush_tlb_range(void *arg
)
623 struct tlb_args
*ta
= (struct tlb_args
*)arg
;
625 local_flush_tlb_range(ta
->ta_vma
, ta
->ta_start
, ta
->ta_end
);
628 static inline void ipi_flush_tlb_kernel_range(void *arg
)
630 struct tlb_args
*ta
= (struct tlb_args
*)arg
;
632 local_flush_tlb_kernel_range(ta
->ta_start
, ta
->ta_end
);
635 void flush_tlb_all(void)
637 if (tlb_ops_need_broadcast())
638 on_each_cpu(ipi_flush_tlb_all
, NULL
, 1);
640 local_flush_tlb_all();
643 void flush_tlb_mm(struct mm_struct
*mm
)
645 if (tlb_ops_need_broadcast())
646 on_each_cpu_mask(ipi_flush_tlb_mm
, mm
, 1, &mm
->cpu_vm_mask
);
648 local_flush_tlb_mm(mm
);
651 void flush_tlb_page(struct vm_area_struct
*vma
, unsigned long uaddr
)
653 if (tlb_ops_need_broadcast()) {
657 on_each_cpu_mask(ipi_flush_tlb_page
, &ta
, 1, &vma
->vm_mm
->cpu_vm_mask
);
659 local_flush_tlb_page(vma
, uaddr
);
662 void flush_tlb_kernel_page(unsigned long kaddr
)
664 if (tlb_ops_need_broadcast()) {
667 on_each_cpu(ipi_flush_tlb_kernel_page
, &ta
, 1);
669 local_flush_tlb_kernel_page(kaddr
);
672 void flush_tlb_range(struct vm_area_struct
*vma
,
673 unsigned long start
, unsigned long end
)
675 if (tlb_ops_need_broadcast()) {
680 on_each_cpu_mask(ipi_flush_tlb_range
, &ta
, 1, &vma
->vm_mm
->cpu_vm_mask
);
682 local_flush_tlb_range(vma
, start
, end
);
685 void flush_tlb_kernel_range(unsigned long start
, unsigned long end
)
687 if (tlb_ops_need_broadcast()) {
691 on_each_cpu(ipi_flush_tlb_kernel_range
, &ta
, 1);
693 local_flush_tlb_kernel_range(start
, end
);