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>
39 #include <asm/smp_plat.h>
42 * as from 2.5, kernels no longer have an init_tasks structure
43 * so we need some other way of telling a new secondary core
44 * where to place its SVC stack
46 struct secondary_data secondary_data
;
49 * structures for inter-processor calls
50 * - A collection of single bit ipi messages.
54 unsigned long ipi_count
;
58 static DEFINE_PER_CPU(struct ipi_data
, ipi_data
) = {
59 .lock
= SPIN_LOCK_UNLOCKED
,
70 int __cpuinit
__cpu_up(unsigned int cpu
)
72 struct cpuinfo_arm
*ci
= &per_cpu(cpu_data
, cpu
);
73 struct task_struct
*idle
= ci
->idle
;
79 * Spawn a new process manually, if not already done.
80 * Grab a pointer to its task struct so we can mess with it
83 idle
= fork_idle(cpu
);
85 printk(KERN_ERR
"CPU%u: fork() failed\n", cpu
);
91 * Since this idle thread is being re-used, call
92 * init_idle() to reinitialize the thread structure.
98 * Allocate initial page tables to allow the new CPU to
99 * enable the MMU safely. This essentially means a set
100 * of our "standard" page tables, with the addition of
101 * a 1:1 mapping for the physical address of the kernel.
103 pgd
= pgd_alloc(&init_mm
);
104 pmd
= pmd_offset(pgd
+ pgd_index(PHYS_OFFSET
), PHYS_OFFSET
);
105 *pmd
= __pmd((PHYS_OFFSET
& PGDIR_MASK
) |
106 PMD_TYPE_SECT
| PMD_SECT_AP_WRITE
);
107 flush_pmd_entry(pmd
);
108 outer_clean_range(__pa(pmd
), __pa(pmd
+ 1));
111 * We need to tell the secondary core where to find
112 * its stack and the page tables.
114 secondary_data
.stack
= task_stack_page(idle
) + THREAD_START_SP
;
115 secondary_data
.pgdir
= virt_to_phys(pgd
);
116 __cpuc_flush_dcache_area(&secondary_data
, sizeof(secondary_data
));
117 outer_clean_range(__pa(&secondary_data
), __pa(&secondary_data
+ 1));
120 * Now bring the CPU into our world.
122 ret
= boot_secondary(cpu
, idle
);
124 unsigned long timeout
;
127 * CPU was successfully started, wait for it
128 * to come online or time out.
130 timeout
= jiffies
+ HZ
;
131 while (time_before(jiffies
, timeout
)) {
139 if (!cpu_online(cpu
))
143 secondary_data
.stack
= NULL
;
144 secondary_data
.pgdir
= 0;
147 clean_pmd_entry(pmd
);
148 pgd_free(&init_mm
, pgd
);
151 printk(KERN_CRIT
"CPU%u: processor failed to boot\n", cpu
);
154 * FIXME: We need to clean up the new idle thread. --rmk
161 #ifdef CONFIG_HOTPLUG_CPU
163 * __cpu_disable runs on the processor to be shutdown.
165 int __cpu_disable(void)
167 unsigned int cpu
= smp_processor_id();
168 struct task_struct
*p
;
171 ret
= platform_cpu_disable(cpu
);
176 * Take this CPU offline. Once we clear this, we can't return,
177 * and we must not schedule until we're ready to give up the cpu.
179 set_cpu_online(cpu
, false);
182 * OK - migrate IRQs away from this CPU
187 * Stop the local timer for this CPU.
192 * Flush user cache and TLB mappings, and then remove this CPU
193 * from the vm mask set of all processes.
196 local_flush_tlb_all();
198 read_lock(&tasklist_lock
);
199 for_each_process(p
) {
201 cpumask_clear_cpu(cpu
, mm_cpumask(p
->mm
));
203 read_unlock(&tasklist_lock
);
209 * called on the thread which is asking for a CPU to be shutdown -
210 * waits until shutdown has completed, or it is timed out.
212 void __cpu_die(unsigned int cpu
)
214 if (!platform_cpu_kill(cpu
))
215 printk("CPU%u: unable to kill\n", cpu
);
219 * Called from the idle thread for the CPU which has been shutdown.
221 * Note that we disable IRQs here, but do not re-enable them
222 * before returning to the caller. This is also the behaviour
223 * of the other hotplug-cpu capable cores, so presumably coming
224 * out of idle fixes this.
226 void __ref
cpu_die(void)
228 unsigned int cpu
= smp_processor_id();
234 * actual CPU shutdown procedure is at least platform (if not
237 platform_cpu_die(cpu
);
240 * Do not return to the idle loop - jump back to the secondary
241 * cpu initialisation. There's some initialisation which needs
242 * to be repeated to undo the effects of taking the CPU offline.
244 __asm__("mov sp, %0\n"
245 " b secondary_start_kernel"
247 : "r" (task_stack_page(current
) + THREAD_SIZE
- 8));
249 #endif /* CONFIG_HOTPLUG_CPU */
252 * This is the secondary CPU boot entry. We're using this CPUs
253 * idle thread stack, but a set of temporary page tables.
255 asmlinkage
void __cpuinit
secondary_start_kernel(void)
257 struct mm_struct
*mm
= &init_mm
;
258 unsigned int cpu
= smp_processor_id();
260 printk("CPU%u: Booted secondary processor\n", cpu
);
263 * All kernel threads share the same mm context; grab a
264 * reference and switch to it.
266 atomic_inc(&mm
->mm_users
);
267 atomic_inc(&mm
->mm_count
);
268 current
->active_mm
= mm
;
269 cpumask_set_cpu(cpu
, mm_cpumask(mm
));
270 cpu_switch_mm(mm
->pgd
, mm
);
271 enter_lazy_tlb(mm
, current
);
272 local_flush_tlb_all();
278 * Give the platform a chance to do its own initialisation.
280 platform_secondary_init(cpu
);
283 * Enable local interrupts.
285 notify_cpu_starting(cpu
);
290 * Setup the percpu timer for this CPU.
292 percpu_timer_setup();
296 smp_store_cpu_info(cpu
);
299 * OK, now it's safe to let the boot CPU continue
301 set_cpu_online(cpu
, true);
304 * OK, it's off to the idle thread for us
310 * Called by both boot and secondaries to move global data into
311 * per-processor storage.
313 void __cpuinit
smp_store_cpu_info(unsigned int cpuid
)
315 struct cpuinfo_arm
*cpu_info
= &per_cpu(cpu_data
, cpuid
);
317 cpu_info
->loops_per_jiffy
= loops_per_jiffy
;
320 void __init
smp_cpus_done(unsigned int max_cpus
)
323 unsigned long bogosum
= 0;
325 for_each_online_cpu(cpu
)
326 bogosum
+= per_cpu(cpu_data
, cpu
).loops_per_jiffy
;
328 printk(KERN_INFO
"SMP: Total of %d processors activated "
329 "(%lu.%02lu BogoMIPS).\n",
331 bogosum
/ (500000/HZ
),
332 (bogosum
/ (5000/HZ
)) % 100);
335 void __init
smp_prepare_boot_cpu(void)
337 unsigned int cpu
= smp_processor_id();
339 per_cpu(cpu_data
, cpu
).idle
= current
;
342 static void send_ipi_message(const struct cpumask
*mask
, enum ipi_msg_type msg
)
347 local_irq_save(flags
);
349 for_each_cpu(cpu
, mask
) {
350 struct ipi_data
*ipi
= &per_cpu(ipi_data
, cpu
);
352 spin_lock(&ipi
->lock
);
353 ipi
->bits
|= 1 << msg
;
354 spin_unlock(&ipi
->lock
);
358 * Call the platform specific cross-CPU call function.
360 smp_cross_call(mask
);
362 local_irq_restore(flags
);
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_SINGLE
);
375 void show_ipi_list(struct seq_file
*p
)
381 for_each_present_cpu(cpu
)
382 seq_printf(p
, " %10lu", per_cpu(ipi_data
, cpu
).ipi_count
);
387 void show_local_irqs(struct seq_file
*p
)
391 seq_printf(p
, "LOC: ");
393 for_each_present_cpu(cpu
)
394 seq_printf(p
, "%10u ", irq_stat
[cpu
].local_timer_irqs
);
400 * Timer (local or broadcast) support
402 static DEFINE_PER_CPU(struct clock_event_device
, percpu_clockevent
);
404 static void ipi_timer(void)
406 struct clock_event_device
*evt
= &__get_cpu_var(percpu_clockevent
);
408 evt
->event_handler(evt
);
412 #ifdef CONFIG_LOCAL_TIMERS
413 asmlinkage
void __exception
do_local_timer(struct pt_regs
*regs
)
415 struct pt_regs
*old_regs
= set_irq_regs(regs
);
416 int cpu
= smp_processor_id();
418 if (local_timer_ack()) {
419 irq_stat
[cpu
].local_timer_irqs
++;
423 set_irq_regs(old_regs
);
427 #ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
428 static void smp_timer_broadcast(const struct cpumask
*mask
)
430 send_ipi_message(mask
, IPI_TIMER
);
433 static void broadcast_timer_set_mode(enum clock_event_mode mode
,
434 struct clock_event_device
*evt
)
438 static void local_timer_setup(struct clock_event_device
*evt
)
440 evt
->name
= "dummy_timer";
441 evt
->features
= CLOCK_EVT_FEAT_ONESHOT
|
442 CLOCK_EVT_FEAT_PERIODIC
|
443 CLOCK_EVT_FEAT_DUMMY
;
446 evt
->set_mode
= broadcast_timer_set_mode
;
447 evt
->broadcast
= smp_timer_broadcast
;
449 clockevents_register_device(evt
);
453 void __cpuinit
percpu_timer_setup(void)
455 unsigned int cpu
= smp_processor_id();
456 struct clock_event_device
*evt
= &per_cpu(percpu_clockevent
, cpu
);
458 evt
->cpumask
= cpumask_of(cpu
);
460 local_timer_setup(evt
);
463 static DEFINE_SPINLOCK(stop_lock
);
466 * ipi_cpu_stop - handle IPI from smp_send_stop()
468 static void ipi_cpu_stop(unsigned int cpu
)
470 spin_lock(&stop_lock
);
471 printk(KERN_CRIT
"CPU%u: stopping\n", cpu
);
473 spin_unlock(&stop_lock
);
475 set_cpu_online(cpu
, false);
485 * Main handler for inter-processor interrupts
487 * For ARM, the ipimask now only identifies a single
488 * category of IPI (Bit 1 IPIs have been replaced by a
489 * different mechanism):
491 * Bit 0 - Inter-processor function call
493 asmlinkage
void __exception
do_IPI(struct pt_regs
*regs
)
495 unsigned int cpu
= smp_processor_id();
496 struct ipi_data
*ipi
= &per_cpu(ipi_data
, cpu
);
497 struct pt_regs
*old_regs
= set_irq_regs(regs
);
504 spin_lock(&ipi
->lock
);
507 spin_unlock(&ipi
->lock
);
515 nextmsg
= msgs
& -msgs
;
517 nextmsg
= ffz(~nextmsg
);
526 * nothing more to do - eveything is
527 * done on the interrupt return path
532 generic_smp_call_function_interrupt();
535 case IPI_CALL_FUNC_SINGLE
:
536 generic_smp_call_function_single_interrupt();
544 printk(KERN_CRIT
"CPU%u: Unknown IPI message 0x%x\n",
551 set_irq_regs(old_regs
);
554 void smp_send_reschedule(int cpu
)
556 send_ipi_message(cpumask_of(cpu
), IPI_RESCHEDULE
);
559 void smp_send_stop(void)
561 cpumask_t mask
= cpu_online_map
;
562 cpu_clear(smp_processor_id(), mask
);
563 send_ipi_message(&mask
, IPI_CPU_STOP
);
569 int setup_profiling_timer(unsigned int multiplier
)
575 on_each_cpu_mask(void (*func
)(void *), void *info
, int wait
,
576 const struct cpumask
*mask
)
580 smp_call_function_many(mask
, func
, info
, wait
);
581 if (cpumask_test_cpu(smp_processor_id(), mask
))
587 /**********************************************************************/
593 struct vm_area_struct
*ta_vma
;
594 unsigned long ta_start
;
595 unsigned long ta_end
;
598 static inline void ipi_flush_tlb_all(void *ignored
)
600 local_flush_tlb_all();
603 static inline void ipi_flush_tlb_mm(void *arg
)
605 struct mm_struct
*mm
= (struct mm_struct
*)arg
;
607 local_flush_tlb_mm(mm
);
610 static inline void ipi_flush_tlb_page(void *arg
)
612 struct tlb_args
*ta
= (struct tlb_args
*)arg
;
614 local_flush_tlb_page(ta
->ta_vma
, ta
->ta_start
);
617 static inline void ipi_flush_tlb_kernel_page(void *arg
)
619 struct tlb_args
*ta
= (struct tlb_args
*)arg
;
621 local_flush_tlb_kernel_page(ta
->ta_start
);
624 static inline void ipi_flush_tlb_range(void *arg
)
626 struct tlb_args
*ta
= (struct tlb_args
*)arg
;
628 local_flush_tlb_range(ta
->ta_vma
, ta
->ta_start
, ta
->ta_end
);
631 static inline void ipi_flush_tlb_kernel_range(void *arg
)
633 struct tlb_args
*ta
= (struct tlb_args
*)arg
;
635 local_flush_tlb_kernel_range(ta
->ta_start
, ta
->ta_end
);
638 void flush_tlb_all(void)
640 if (tlb_ops_need_broadcast())
641 on_each_cpu(ipi_flush_tlb_all
, NULL
, 1);
643 local_flush_tlb_all();
646 void flush_tlb_mm(struct mm_struct
*mm
)
648 if (tlb_ops_need_broadcast())
649 on_each_cpu_mask(ipi_flush_tlb_mm
, mm
, 1, mm_cpumask(mm
));
651 local_flush_tlb_mm(mm
);
654 void flush_tlb_page(struct vm_area_struct
*vma
, unsigned long uaddr
)
656 if (tlb_ops_need_broadcast()) {
660 on_each_cpu_mask(ipi_flush_tlb_page
, &ta
, 1, mm_cpumask(vma
->vm_mm
));
662 local_flush_tlb_page(vma
, uaddr
);
665 void flush_tlb_kernel_page(unsigned long kaddr
)
667 if (tlb_ops_need_broadcast()) {
670 on_each_cpu(ipi_flush_tlb_kernel_page
, &ta
, 1);
672 local_flush_tlb_kernel_page(kaddr
);
675 void flush_tlb_range(struct vm_area_struct
*vma
,
676 unsigned long start
, unsigned long end
)
678 if (tlb_ops_need_broadcast()) {
683 on_each_cpu_mask(ipi_flush_tlb_range
, &ta
, 1, mm_cpumask(vma
->vm_mm
));
685 local_flush_tlb_range(vma
, start
, end
);
688 void flush_tlb_kernel_range(unsigned long start
, unsigned long end
)
690 if (tlb_ops_need_broadcast()) {
694 on_each_cpu(ipi_flush_tlb_kernel_range
, &ta
, 1);
696 local_flush_tlb_kernel_range(start
, end
);