xref: /linux/arch/arm/kernel/smp.c (revision 03b505eae6a276b8c38b6222694afb6cea10b1cc)
1 /*
2  *  linux/arch/arm/kernel/smp.c
3  *
4  *  Copyright (C) 2002 ARM Limited, All Rights Reserved.
5  *
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.
9  */
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>
19 #include <linux/mm.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>
27 #include <linux/completion.h>
28 
29 #include <asm/atomic.h>
30 #include <asm/cacheflush.h>
31 #include <asm/cpu.h>
32 #include <asm/cputype.h>
33 #include <asm/mmu_context.h>
34 #include <asm/pgtable.h>
35 #include <asm/pgalloc.h>
36 #include <asm/processor.h>
37 #include <asm/sections.h>
38 #include <asm/tlbflush.h>
39 #include <asm/ptrace.h>
40 #include <asm/localtimer.h>
41 
42 /*
43  * as from 2.5, kernels no longer have an init_tasks structure
44  * so we need some other way of telling a new secondary core
45  * where to place its SVC stack
46  */
47 struct secondary_data secondary_data;
48 
49 enum ipi_msg_type {
50 	IPI_TIMER = 2,
51 	IPI_RESCHEDULE,
52 	IPI_CALL_FUNC,
53 	IPI_CALL_FUNC_SINGLE,
54 	IPI_CPU_STOP,
55 };
56 
57 static inline void identity_mapping_add(pgd_t *pgd, unsigned long start,
58 	unsigned long end)
59 {
60 	unsigned long addr, prot;
61 	pmd_t *pmd;
62 
63 	prot = PMD_TYPE_SECT | PMD_SECT_AP_WRITE;
64 	if (cpu_architecture() <= CPU_ARCH_ARMv5TEJ && !cpu_is_xscale())
65 		prot |= PMD_BIT4;
66 
67 	for (addr = start & PGDIR_MASK; addr < end;) {
68 		pmd = pmd_offset(pgd + pgd_index(addr), addr);
69 		pmd[0] = __pmd(addr | prot);
70 		addr += SECTION_SIZE;
71 		pmd[1] = __pmd(addr | prot);
72 		addr += SECTION_SIZE;
73 		flush_pmd_entry(pmd);
74 		outer_clean_range(__pa(pmd), __pa(pmd + 1));
75 	}
76 }
77 
78 static inline void identity_mapping_del(pgd_t *pgd, unsigned long start,
79 	unsigned long end)
80 {
81 	unsigned long addr;
82 	pmd_t *pmd;
83 
84 	for (addr = start & PGDIR_MASK; addr < end; addr += PGDIR_SIZE) {
85 		pmd = pmd_offset(pgd + pgd_index(addr), addr);
86 		pmd[0] = __pmd(0);
87 		pmd[1] = __pmd(0);
88 		clean_pmd_entry(pmd);
89 		outer_clean_range(__pa(pmd), __pa(pmd + 1));
90 	}
91 }
92 
93 int __cpuinit __cpu_up(unsigned int cpu)
94 {
95 	struct cpuinfo_arm *ci = &per_cpu(cpu_data, cpu);
96 	struct task_struct *idle = ci->idle;
97 	pgd_t *pgd;
98 	int ret;
99 
100 	/*
101 	 * Spawn a new process manually, if not already done.
102 	 * Grab a pointer to its task struct so we can mess with it
103 	 */
104 	if (!idle) {
105 		idle = fork_idle(cpu);
106 		if (IS_ERR(idle)) {
107 			printk(KERN_ERR "CPU%u: fork() failed\n", cpu);
108 			return PTR_ERR(idle);
109 		}
110 		ci->idle = idle;
111 	} else {
112 		/*
113 		 * Since this idle thread is being re-used, call
114 		 * init_idle() to reinitialize the thread structure.
115 		 */
116 		init_idle(idle, cpu);
117 	}
118 
119 	/*
120 	 * Allocate initial page tables to allow the new CPU to
121 	 * enable the MMU safely.  This essentially means a set
122 	 * of our "standard" page tables, with the addition of
123 	 * a 1:1 mapping for the physical address of the kernel.
124 	 */
125 	pgd = pgd_alloc(&init_mm);
126 	if (!pgd)
127 		return -ENOMEM;
128 
129 	if (PHYS_OFFSET != PAGE_OFFSET) {
130 #ifndef CONFIG_HOTPLUG_CPU
131 		identity_mapping_add(pgd, __pa(__init_begin), __pa(__init_end));
132 #endif
133 		identity_mapping_add(pgd, __pa(_stext), __pa(_etext));
134 		identity_mapping_add(pgd, __pa(_sdata), __pa(_edata));
135 	}
136 
137 	/*
138 	 * We need to tell the secondary core where to find
139 	 * its stack and the page tables.
140 	 */
141 	secondary_data.stack = task_stack_page(idle) + THREAD_START_SP;
142 	secondary_data.pgdir = virt_to_phys(pgd);
143 	__cpuc_flush_dcache_area(&secondary_data, sizeof(secondary_data));
144 	outer_clean_range(__pa(&secondary_data), __pa(&secondary_data + 1));
145 
146 	/*
147 	 * Now bring the CPU into our world.
148 	 */
149 	ret = boot_secondary(cpu, idle);
150 	if (ret == 0) {
151 		unsigned long timeout;
152 
153 		/*
154 		 * CPU was successfully started, wait for it
155 		 * to come online or time out.
156 		 */
157 		timeout = jiffies + HZ;
158 		while (time_before(jiffies, timeout)) {
159 			if (cpu_online(cpu))
160 				break;
161 
162 			udelay(10);
163 			barrier();
164 		}
165 
166 		if (!cpu_online(cpu)) {
167 			pr_crit("CPU%u: failed to come online\n", cpu);
168 			ret = -EIO;
169 		}
170 	} else {
171 		pr_err("CPU%u: failed to boot: %d\n", cpu, ret);
172 	}
173 
174 	secondary_data.stack = NULL;
175 	secondary_data.pgdir = 0;
176 
177 	if (PHYS_OFFSET != PAGE_OFFSET) {
178 #ifndef CONFIG_HOTPLUG_CPU
179 		identity_mapping_del(pgd, __pa(__init_begin), __pa(__init_end));
180 #endif
181 		identity_mapping_del(pgd, __pa(_stext), __pa(_etext));
182 		identity_mapping_del(pgd, __pa(_sdata), __pa(_edata));
183 	}
184 
185 	pgd_free(&init_mm, pgd);
186 
187 	return ret;
188 }
189 
190 #ifdef CONFIG_HOTPLUG_CPU
191 static void percpu_timer_stop(void);
192 
193 /*
194  * __cpu_disable runs on the processor to be shutdown.
195  */
196 int __cpu_disable(void)
197 {
198 	unsigned int cpu = smp_processor_id();
199 	struct task_struct *p;
200 	int ret;
201 
202 	ret = platform_cpu_disable(cpu);
203 	if (ret)
204 		return ret;
205 
206 	/*
207 	 * Take this CPU offline.  Once we clear this, we can't return,
208 	 * and we must not schedule until we're ready to give up the cpu.
209 	 */
210 	set_cpu_online(cpu, false);
211 
212 	/*
213 	 * OK - migrate IRQs away from this CPU
214 	 */
215 	migrate_irqs();
216 
217 	/*
218 	 * Stop the local timer for this CPU.
219 	 */
220 	percpu_timer_stop();
221 
222 	/*
223 	 * Flush user cache and TLB mappings, and then remove this CPU
224 	 * from the vm mask set of all processes.
225 	 */
226 	flush_cache_all();
227 	local_flush_tlb_all();
228 
229 	read_lock(&tasklist_lock);
230 	for_each_process(p) {
231 		if (p->mm)
232 			cpumask_clear_cpu(cpu, mm_cpumask(p->mm));
233 	}
234 	read_unlock(&tasklist_lock);
235 
236 	return 0;
237 }
238 
239 static DECLARE_COMPLETION(cpu_died);
240 
241 /*
242  * called on the thread which is asking for a CPU to be shutdown -
243  * waits until shutdown has completed, or it is timed out.
244  */
245 void __cpu_die(unsigned int cpu)
246 {
247 	if (!wait_for_completion_timeout(&cpu_died, msecs_to_jiffies(5000))) {
248 		pr_err("CPU%u: cpu didn't die\n", cpu);
249 		return;
250 	}
251 	printk(KERN_NOTICE "CPU%u: shutdown\n", cpu);
252 
253 	if (!platform_cpu_kill(cpu))
254 		printk("CPU%u: unable to kill\n", cpu);
255 }
256 
257 /*
258  * Called from the idle thread for the CPU which has been shutdown.
259  *
260  * Note that we disable IRQs here, but do not re-enable them
261  * before returning to the caller. This is also the behaviour
262  * of the other hotplug-cpu capable cores, so presumably coming
263  * out of idle fixes this.
264  */
265 void __ref cpu_die(void)
266 {
267 	unsigned int cpu = smp_processor_id();
268 
269 	idle_task_exit();
270 
271 	local_irq_disable();
272 	mb();
273 
274 	/* Tell __cpu_die() that this CPU is now safe to dispose of */
275 	complete(&cpu_died);
276 
277 	/*
278 	 * actual CPU shutdown procedure is at least platform (if not
279 	 * CPU) specific.
280 	 */
281 	platform_cpu_die(cpu);
282 
283 	/*
284 	 * Do not return to the idle loop - jump back to the secondary
285 	 * cpu initialisation.  There's some initialisation which needs
286 	 * to be repeated to undo the effects of taking the CPU offline.
287 	 */
288 	__asm__("mov	sp, %0\n"
289 	"	b	secondary_start_kernel"
290 		:
291 		: "r" (task_stack_page(current) + THREAD_SIZE - 8));
292 }
293 #endif /* CONFIG_HOTPLUG_CPU */
294 
295 /*
296  * Called by both boot and secondaries to move global data into
297  * per-processor storage.
298  */
299 static void __cpuinit smp_store_cpu_info(unsigned int cpuid)
300 {
301 	struct cpuinfo_arm *cpu_info = &per_cpu(cpu_data, cpuid);
302 
303 	cpu_info->loops_per_jiffy = loops_per_jiffy;
304 }
305 
306 /*
307  * This is the secondary CPU boot entry.  We're using this CPUs
308  * idle thread stack, but a set of temporary page tables.
309  */
310 asmlinkage void __cpuinit secondary_start_kernel(void)
311 {
312 	struct mm_struct *mm = &init_mm;
313 	unsigned int cpu = smp_processor_id();
314 
315 	printk("CPU%u: Booted secondary processor\n", cpu);
316 
317 	/*
318 	 * All kernel threads share the same mm context; grab a
319 	 * reference and switch to it.
320 	 */
321 	atomic_inc(&mm->mm_users);
322 	atomic_inc(&mm->mm_count);
323 	current->active_mm = mm;
324 	cpumask_set_cpu(cpu, mm_cpumask(mm));
325 	cpu_switch_mm(mm->pgd, mm);
326 	enter_lazy_tlb(mm, current);
327 	local_flush_tlb_all();
328 
329 	cpu_init();
330 	preempt_disable();
331 	trace_hardirqs_off();
332 
333 	/*
334 	 * Give the platform a chance to do its own initialisation.
335 	 */
336 	platform_secondary_init(cpu);
337 
338 	/*
339 	 * Enable local interrupts.
340 	 */
341 	notify_cpu_starting(cpu);
342 	local_irq_enable();
343 	local_fiq_enable();
344 
345 	/*
346 	 * Setup the percpu timer for this CPU.
347 	 */
348 	percpu_timer_setup();
349 
350 	calibrate_delay();
351 
352 	smp_store_cpu_info(cpu);
353 
354 	/*
355 	 * OK, now it's safe to let the boot CPU continue
356 	 */
357 	set_cpu_online(cpu, true);
358 
359 	/*
360 	 * OK, it's off to the idle thread for us
361 	 */
362 	cpu_idle();
363 }
364 
365 void __init smp_cpus_done(unsigned int max_cpus)
366 {
367 	int cpu;
368 	unsigned long bogosum = 0;
369 
370 	for_each_online_cpu(cpu)
371 		bogosum += per_cpu(cpu_data, cpu).loops_per_jiffy;
372 
373 	printk(KERN_INFO "SMP: Total of %d processors activated "
374 	       "(%lu.%02lu BogoMIPS).\n",
375 	       num_online_cpus(),
376 	       bogosum / (500000/HZ),
377 	       (bogosum / (5000/HZ)) % 100);
378 }
379 
380 void __init smp_prepare_boot_cpu(void)
381 {
382 	unsigned int cpu = smp_processor_id();
383 
384 	per_cpu(cpu_data, cpu).idle = current;
385 }
386 
387 void __init smp_prepare_cpus(unsigned int max_cpus)
388 {
389 	unsigned int ncores = num_possible_cpus();
390 
391 	smp_store_cpu_info(smp_processor_id());
392 
393 	/*
394 	 * are we trying to boot more cores than exist?
395 	 */
396 	if (max_cpus > ncores)
397 		max_cpus = ncores;
398 
399 	if (max_cpus > 1) {
400 		/*
401 		 * Enable the local timer or broadcast device for the
402 		 * boot CPU, but only if we have more than one CPU.
403 		 */
404 		percpu_timer_setup();
405 
406 		/*
407 		 * Initialise the SCU if there are more than one CPU
408 		 * and let them know where to start.
409 		 */
410 		platform_smp_prepare_cpus(max_cpus);
411 	}
412 }
413 
414 void arch_send_call_function_ipi_mask(const struct cpumask *mask)
415 {
416 	smp_cross_call(mask, IPI_CALL_FUNC);
417 }
418 
419 void arch_send_call_function_single_ipi(int cpu)
420 {
421 	smp_cross_call(cpumask_of(cpu), IPI_CALL_FUNC_SINGLE);
422 }
423 
424 static const char *ipi_types[NR_IPI] = {
425 #define S(x,s)	[x - IPI_TIMER] = s
426 	S(IPI_TIMER, "Timer broadcast interrupts"),
427 	S(IPI_RESCHEDULE, "Rescheduling interrupts"),
428 	S(IPI_CALL_FUNC, "Function call interrupts"),
429 	S(IPI_CALL_FUNC_SINGLE, "Single function call interrupts"),
430 	S(IPI_CPU_STOP, "CPU stop interrupts"),
431 };
432 
433 void show_ipi_list(struct seq_file *p, int prec)
434 {
435 	unsigned int cpu, i;
436 
437 	for (i = 0; i < NR_IPI; i++) {
438 		seq_printf(p, "%*s%u: ", prec - 1, "IPI", i);
439 
440 		for_each_present_cpu(cpu)
441 			seq_printf(p, "%10u ",
442 				   __get_irq_stat(cpu, ipi_irqs[i]));
443 
444 		seq_printf(p, " %s\n", ipi_types[i]);
445 	}
446 }
447 
448 u64 smp_irq_stat_cpu(unsigned int cpu)
449 {
450 	u64 sum = 0;
451 	int i;
452 
453 	for (i = 0; i < NR_IPI; i++)
454 		sum += __get_irq_stat(cpu, ipi_irqs[i]);
455 
456 #ifdef CONFIG_LOCAL_TIMERS
457 	sum += __get_irq_stat(cpu, local_timer_irqs);
458 #endif
459 
460 	return sum;
461 }
462 
463 /*
464  * Timer (local or broadcast) support
465  */
466 static DEFINE_PER_CPU(struct clock_event_device, percpu_clockevent);
467 
468 static void ipi_timer(void)
469 {
470 	struct clock_event_device *evt = &__get_cpu_var(percpu_clockevent);
471 	irq_enter();
472 	evt->event_handler(evt);
473 	irq_exit();
474 }
475 
476 #ifdef CONFIG_LOCAL_TIMERS
477 asmlinkage void __exception do_local_timer(struct pt_regs *regs)
478 {
479 	struct pt_regs *old_regs = set_irq_regs(regs);
480 	int cpu = smp_processor_id();
481 
482 	if (local_timer_ack()) {
483 		__inc_irq_stat(cpu, local_timer_irqs);
484 		ipi_timer();
485 	}
486 
487 	set_irq_regs(old_regs);
488 }
489 
490 void show_local_irqs(struct seq_file *p, int prec)
491 {
492 	unsigned int cpu;
493 
494 	seq_printf(p, "%*s: ", prec, "LOC");
495 
496 	for_each_present_cpu(cpu)
497 		seq_printf(p, "%10u ", __get_irq_stat(cpu, local_timer_irqs));
498 
499 	seq_printf(p, " Local timer interrupts\n");
500 }
501 #endif
502 
503 #ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
504 static void smp_timer_broadcast(const struct cpumask *mask)
505 {
506 	smp_cross_call(mask, IPI_TIMER);
507 }
508 #else
509 #define smp_timer_broadcast	NULL
510 #endif
511 
512 #ifndef CONFIG_LOCAL_TIMERS
513 static void broadcast_timer_set_mode(enum clock_event_mode mode,
514 	struct clock_event_device *evt)
515 {
516 }
517 
518 static void local_timer_setup(struct clock_event_device *evt)
519 {
520 	evt->name	= "dummy_timer";
521 	evt->features	= CLOCK_EVT_FEAT_ONESHOT |
522 			  CLOCK_EVT_FEAT_PERIODIC |
523 			  CLOCK_EVT_FEAT_DUMMY;
524 	evt->rating	= 400;
525 	evt->mult	= 1;
526 	evt->set_mode	= broadcast_timer_set_mode;
527 
528 	clockevents_register_device(evt);
529 }
530 #endif
531 
532 void __cpuinit percpu_timer_setup(void)
533 {
534 	unsigned int cpu = smp_processor_id();
535 	struct clock_event_device *evt = &per_cpu(percpu_clockevent, cpu);
536 
537 	evt->cpumask = cpumask_of(cpu);
538 	evt->broadcast = smp_timer_broadcast;
539 
540 	local_timer_setup(evt);
541 }
542 
543 #ifdef CONFIG_HOTPLUG_CPU
544 /*
545  * The generic clock events code purposely does not stop the local timer
546  * on CPU_DEAD/CPU_DEAD_FROZEN hotplug events, so we have to do it
547  * manually here.
548  */
549 static void percpu_timer_stop(void)
550 {
551 	unsigned int cpu = smp_processor_id();
552 	struct clock_event_device *evt = &per_cpu(percpu_clockevent, cpu);
553 
554 	evt->set_mode(CLOCK_EVT_MODE_UNUSED, evt);
555 }
556 #endif
557 
558 static DEFINE_SPINLOCK(stop_lock);
559 
560 /*
561  * ipi_cpu_stop - handle IPI from smp_send_stop()
562  */
563 static void ipi_cpu_stop(unsigned int cpu)
564 {
565 	if (system_state == SYSTEM_BOOTING ||
566 	    system_state == SYSTEM_RUNNING) {
567 		spin_lock(&stop_lock);
568 		printk(KERN_CRIT "CPU%u: stopping\n", cpu);
569 		dump_stack();
570 		spin_unlock(&stop_lock);
571 	}
572 
573 	set_cpu_online(cpu, false);
574 
575 	local_fiq_disable();
576 	local_irq_disable();
577 
578 	while (1)
579 		cpu_relax();
580 }
581 
582 /*
583  * Main handler for inter-processor interrupts
584  */
585 asmlinkage void __exception do_IPI(int ipinr, struct pt_regs *regs)
586 {
587 	unsigned int cpu = smp_processor_id();
588 	struct pt_regs *old_regs = set_irq_regs(regs);
589 
590 	if (ipinr >= IPI_TIMER && ipinr < IPI_TIMER + NR_IPI)
591 		__inc_irq_stat(cpu, ipi_irqs[ipinr - IPI_TIMER]);
592 
593 	switch (ipinr) {
594 	case IPI_TIMER:
595 		ipi_timer();
596 		break;
597 
598 	case IPI_RESCHEDULE:
599 		/*
600 		 * nothing more to do - eveything is
601 		 * done on the interrupt return path
602 		 */
603 		break;
604 
605 	case IPI_CALL_FUNC:
606 		generic_smp_call_function_interrupt();
607 		break;
608 
609 	case IPI_CALL_FUNC_SINGLE:
610 		generic_smp_call_function_single_interrupt();
611 		break;
612 
613 	case IPI_CPU_STOP:
614 		ipi_cpu_stop(cpu);
615 		break;
616 
617 	default:
618 		printk(KERN_CRIT "CPU%u: Unknown IPI message 0x%x\n",
619 		       cpu, ipinr);
620 		break;
621 	}
622 	set_irq_regs(old_regs);
623 }
624 
625 void smp_send_reschedule(int cpu)
626 {
627 	smp_cross_call(cpumask_of(cpu), IPI_RESCHEDULE);
628 }
629 
630 void smp_send_stop(void)
631 {
632 	unsigned long timeout;
633 
634 	if (num_online_cpus() > 1) {
635 		cpumask_t mask = cpu_online_map;
636 		cpu_clear(smp_processor_id(), mask);
637 
638 		smp_cross_call(&mask, IPI_CPU_STOP);
639 	}
640 
641 	/* Wait up to one second for other CPUs to stop */
642 	timeout = USEC_PER_SEC;
643 	while (num_online_cpus() > 1 && timeout--)
644 		udelay(1);
645 
646 	if (num_online_cpus() > 1)
647 		pr_warning("SMP: failed to stop secondary CPUs\n");
648 }
649 
650 /*
651  * not supported here
652  */
653 int setup_profiling_timer(unsigned int multiplier)
654 {
655 	return -EINVAL;
656 }
657