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/seq_file.h> 23 #include <linux/irq.h> 24 #include <linux/percpu.h> 25 #include <linux/clockchips.h> 26 #include <linux/completion.h> 27 #include <linux/cpufreq.h> 28 29 #include <linux/atomic.h> 30 #include <asm/smp.h> 31 #include <asm/cacheflush.h> 32 #include <asm/cpu.h> 33 #include <asm/cputype.h> 34 #include <asm/exception.h> 35 #include <asm/idmap.h> 36 #include <asm/topology.h> 37 #include <asm/mmu_context.h> 38 #include <asm/pgtable.h> 39 #include <asm/pgalloc.h> 40 #include <asm/processor.h> 41 #include <asm/sections.h> 42 #include <asm/tlbflush.h> 43 #include <asm/ptrace.h> 44 #include <asm/localtimer.h> 45 #include <asm/smp_plat.h> 46 #include <asm/virt.h> 47 #include <asm/mach/arch.h> 48 #include <asm/mpu.h> 49 50 /* 51 * as from 2.5, kernels no longer have an init_tasks structure 52 * so we need some other way of telling a new secondary core 53 * where to place its SVC stack 54 */ 55 struct secondary_data secondary_data; 56 57 /* 58 * control for which core is the next to come out of the secondary 59 * boot "holding pen" 60 */ 61 volatile int __cpuinitdata pen_release = -1; 62 63 enum ipi_msg_type { 64 IPI_WAKEUP, 65 IPI_TIMER, 66 IPI_RESCHEDULE, 67 IPI_CALL_FUNC, 68 IPI_CALL_FUNC_SINGLE, 69 IPI_CPU_STOP, 70 }; 71 72 static DECLARE_COMPLETION(cpu_running); 73 74 static struct smp_operations smp_ops; 75 76 void __init smp_set_ops(struct smp_operations *ops) 77 { 78 if (ops) 79 smp_ops = *ops; 80 }; 81 82 int __cpuinit __cpu_up(unsigned int cpu, struct task_struct *idle) 83 { 84 int ret; 85 86 /* 87 * We need to tell the secondary core where to find 88 * its stack and the page tables. 89 */ 90 secondary_data.stack = task_stack_page(idle) + THREAD_START_SP; 91 #ifdef CONFIG_ARM_MPU 92 secondary_data.mpu_rgn_szr = mpu_rgn_info.rgns[MPU_RAM_REGION].drsr; 93 #endif 94 95 #ifdef CONFIG_MMU 96 secondary_data.pgdir = virt_to_phys(idmap_pgd); 97 secondary_data.swapper_pg_dir = virt_to_phys(swapper_pg_dir); 98 #endif 99 __cpuc_flush_dcache_area(&secondary_data, sizeof(secondary_data)); 100 outer_clean_range(__pa(&secondary_data), __pa(&secondary_data + 1)); 101 102 /* 103 * Now bring the CPU into our world. 104 */ 105 ret = boot_secondary(cpu, idle); 106 if (ret == 0) { 107 /* 108 * CPU was successfully started, wait for it 109 * to come online or time out. 110 */ 111 wait_for_completion_timeout(&cpu_running, 112 msecs_to_jiffies(1000)); 113 114 if (!cpu_online(cpu)) { 115 pr_crit("CPU%u: failed to come online\n", cpu); 116 ret = -EIO; 117 } 118 } else { 119 pr_err("CPU%u: failed to boot: %d\n", cpu, ret); 120 } 121 122 123 memset(&secondary_data, 0, sizeof(secondary_data)); 124 return ret; 125 } 126 127 /* platform specific SMP operations */ 128 void __init smp_init_cpus(void) 129 { 130 if (smp_ops.smp_init_cpus) 131 smp_ops.smp_init_cpus(); 132 } 133 134 int __cpuinit boot_secondary(unsigned int cpu, struct task_struct *idle) 135 { 136 if (smp_ops.smp_boot_secondary) 137 return smp_ops.smp_boot_secondary(cpu, idle); 138 return -ENOSYS; 139 } 140 141 #ifdef CONFIG_HOTPLUG_CPU 142 static void percpu_timer_stop(void); 143 144 static int platform_cpu_kill(unsigned int cpu) 145 { 146 if (smp_ops.cpu_kill) 147 return smp_ops.cpu_kill(cpu); 148 return 1; 149 } 150 151 static int platform_cpu_disable(unsigned int cpu) 152 { 153 if (smp_ops.cpu_disable) 154 return smp_ops.cpu_disable(cpu); 155 156 /* 157 * By default, allow disabling all CPUs except the first one, 158 * since this is special on a lot of platforms, e.g. because 159 * of clock tick interrupts. 160 */ 161 return cpu == 0 ? -EPERM : 0; 162 } 163 /* 164 * __cpu_disable runs on the processor to be shutdown. 165 */ 166 int __cpuinit __cpu_disable(void) 167 { 168 unsigned int cpu = smp_processor_id(); 169 int ret; 170 171 ret = platform_cpu_disable(cpu); 172 if (ret) 173 return ret; 174 175 /* 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. 178 */ 179 set_cpu_online(cpu, false); 180 181 /* 182 * OK - migrate IRQs away from this CPU 183 */ 184 migrate_irqs(); 185 186 /* 187 * Stop the local timer for this CPU. 188 */ 189 percpu_timer_stop(); 190 191 /* 192 * Flush user cache and TLB mappings, and then remove this CPU 193 * from the vm mask set of all processes. 194 * 195 * Caches are flushed to the Level of Unification Inner Shareable 196 * to write-back dirty lines to unified caches shared by all CPUs. 197 */ 198 flush_cache_louis(); 199 local_flush_tlb_all(); 200 201 clear_tasks_mm_cpumask(cpu); 202 203 return 0; 204 } 205 206 static DECLARE_COMPLETION(cpu_died); 207 208 /* 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. 211 */ 212 void __cpuinit __cpu_die(unsigned int cpu) 213 { 214 if (!wait_for_completion_timeout(&cpu_died, msecs_to_jiffies(5000))) { 215 pr_err("CPU%u: cpu didn't die\n", cpu); 216 return; 217 } 218 printk(KERN_NOTICE "CPU%u: shutdown\n", cpu); 219 220 /* 221 * platform_cpu_kill() is generally expected to do the powering off 222 * and/or cutting of clocks to the dying CPU. Optionally, this may 223 * be done by the CPU which is dying in preference to supporting 224 * this call, but that means there is _no_ synchronisation between 225 * the requesting CPU and the dying CPU actually losing power. 226 */ 227 if (!platform_cpu_kill(cpu)) 228 printk("CPU%u: unable to kill\n", cpu); 229 } 230 231 /* 232 * Called from the idle thread for the CPU which has been shutdown. 233 * 234 * Note that we disable IRQs here, but do not re-enable them 235 * before returning to the caller. This is also the behaviour 236 * of the other hotplug-cpu capable cores, so presumably coming 237 * out of idle fixes this. 238 */ 239 void __ref cpu_die(void) 240 { 241 unsigned int cpu = smp_processor_id(); 242 243 idle_task_exit(); 244 245 local_irq_disable(); 246 247 /* 248 * Flush the data out of the L1 cache for this CPU. This must be 249 * before the completion to ensure that data is safely written out 250 * before platform_cpu_kill() gets called - which may disable 251 * *this* CPU and power down its cache. 252 */ 253 flush_cache_louis(); 254 255 /* 256 * Tell __cpu_die() that this CPU is now safe to dispose of. Once 257 * this returns, power and/or clocks can be removed at any point 258 * from this CPU and its cache by platform_cpu_kill(). 259 */ 260 complete(&cpu_died); 261 262 /* 263 * Ensure that the cache lines associated with that completion are 264 * written out. This covers the case where _this_ CPU is doing the 265 * powering down, to ensure that the completion is visible to the 266 * CPU waiting for this one. 267 */ 268 flush_cache_louis(); 269 270 /* 271 * The actual CPU shutdown procedure is at least platform (if not 272 * CPU) specific. This may remove power, or it may simply spin. 273 * 274 * Platforms are generally expected *NOT* to return from this call, 275 * although there are some which do because they have no way to 276 * power down the CPU. These platforms are the _only_ reason we 277 * have a return path which uses the fragment of assembly below. 278 * 279 * The return path should not be used for platforms which can 280 * power off the CPU. 281 */ 282 if (smp_ops.cpu_die) 283 smp_ops.cpu_die(cpu); 284 285 /* 286 * Do not return to the idle loop - jump back to the secondary 287 * cpu initialisation. There's some initialisation which needs 288 * to be repeated to undo the effects of taking the CPU offline. 289 */ 290 __asm__("mov sp, %0\n" 291 " mov fp, #0\n" 292 " b secondary_start_kernel" 293 : 294 : "r" (task_stack_page(current) + THREAD_SIZE - 8)); 295 } 296 #endif /* CONFIG_HOTPLUG_CPU */ 297 298 /* 299 * Called by both boot and secondaries to move global data into 300 * per-processor storage. 301 */ 302 static void __cpuinit smp_store_cpu_info(unsigned int cpuid) 303 { 304 struct cpuinfo_arm *cpu_info = &per_cpu(cpu_data, cpuid); 305 306 cpu_info->loops_per_jiffy = loops_per_jiffy; 307 cpu_info->cpuid = read_cpuid_id(); 308 309 store_cpu_topology(cpuid); 310 } 311 312 static void percpu_timer_setup(void); 313 314 /* 315 * This is the secondary CPU boot entry. We're using this CPUs 316 * idle thread stack, but a set of temporary page tables. 317 */ 318 asmlinkage void __cpuinit secondary_start_kernel(void) 319 { 320 struct mm_struct *mm = &init_mm; 321 unsigned int cpu; 322 323 /* 324 * The identity mapping is uncached (strongly ordered), so 325 * switch away from it before attempting any exclusive accesses. 326 */ 327 cpu_switch_mm(mm->pgd, mm); 328 local_flush_bp_all(); 329 enter_lazy_tlb(mm, current); 330 local_flush_tlb_all(); 331 332 /* 333 * All kernel threads share the same mm context; grab a 334 * reference and switch to it. 335 */ 336 cpu = smp_processor_id(); 337 atomic_inc(&mm->mm_count); 338 current->active_mm = mm; 339 cpumask_set_cpu(cpu, mm_cpumask(mm)); 340 341 cpu_init(); 342 343 printk("CPU%u: Booted secondary processor\n", cpu); 344 345 preempt_disable(); 346 trace_hardirqs_off(); 347 348 /* 349 * Give the platform a chance to do its own initialisation. 350 */ 351 if (smp_ops.smp_secondary_init) 352 smp_ops.smp_secondary_init(cpu); 353 354 notify_cpu_starting(cpu); 355 356 calibrate_delay(); 357 358 smp_store_cpu_info(cpu); 359 360 /* 361 * OK, now it's safe to let the boot CPU continue. Wait for 362 * the CPU migration code to notice that the CPU is online 363 * before we continue - which happens after __cpu_up returns. 364 */ 365 set_cpu_online(cpu, true); 366 complete(&cpu_running); 367 368 /* 369 * Setup the percpu timer for this CPU. 370 */ 371 percpu_timer_setup(); 372 373 local_irq_enable(); 374 local_fiq_enable(); 375 376 /* 377 * OK, it's off to the idle thread for us 378 */ 379 cpu_startup_entry(CPUHP_ONLINE); 380 } 381 382 void __init smp_cpus_done(unsigned int max_cpus) 383 { 384 int cpu; 385 unsigned long bogosum = 0; 386 387 for_each_online_cpu(cpu) 388 bogosum += per_cpu(cpu_data, cpu).loops_per_jiffy; 389 390 printk(KERN_INFO "SMP: Total of %d processors activated " 391 "(%lu.%02lu BogoMIPS).\n", 392 num_online_cpus(), 393 bogosum / (500000/HZ), 394 (bogosum / (5000/HZ)) % 100); 395 396 hyp_mode_check(); 397 } 398 399 void __init smp_prepare_boot_cpu(void) 400 { 401 set_my_cpu_offset(per_cpu_offset(smp_processor_id())); 402 } 403 404 void __init smp_prepare_cpus(unsigned int max_cpus) 405 { 406 unsigned int ncores = num_possible_cpus(); 407 408 init_cpu_topology(); 409 410 smp_store_cpu_info(smp_processor_id()); 411 412 /* 413 * are we trying to boot more cores than exist? 414 */ 415 if (max_cpus > ncores) 416 max_cpus = ncores; 417 if (ncores > 1 && max_cpus) { 418 /* 419 * Enable the local timer or broadcast device for the 420 * boot CPU, but only if we have more than one CPU. 421 */ 422 percpu_timer_setup(); 423 424 /* 425 * Initialise the present map, which describes the set of CPUs 426 * actually populated at the present time. A platform should 427 * re-initialize the map in the platforms smp_prepare_cpus() 428 * if present != possible (e.g. physical hotplug). 429 */ 430 init_cpu_present(cpu_possible_mask); 431 432 /* 433 * Initialise the SCU if there are more than one CPU 434 * and let them know where to start. 435 */ 436 if (smp_ops.smp_prepare_cpus) 437 smp_ops.smp_prepare_cpus(max_cpus); 438 } 439 } 440 441 static void (*smp_cross_call)(const struct cpumask *, unsigned int); 442 443 void __init set_smp_cross_call(void (*fn)(const struct cpumask *, unsigned int)) 444 { 445 if (!smp_cross_call) 446 smp_cross_call = fn; 447 } 448 449 void arch_send_call_function_ipi_mask(const struct cpumask *mask) 450 { 451 smp_cross_call(mask, IPI_CALL_FUNC); 452 } 453 454 void arch_send_wakeup_ipi_mask(const struct cpumask *mask) 455 { 456 smp_cross_call(mask, IPI_WAKEUP); 457 } 458 459 void arch_send_call_function_single_ipi(int cpu) 460 { 461 smp_cross_call(cpumask_of(cpu), IPI_CALL_FUNC_SINGLE); 462 } 463 464 static const char *ipi_types[NR_IPI] = { 465 #define S(x,s) [x] = s 466 S(IPI_WAKEUP, "CPU wakeup interrupts"), 467 S(IPI_TIMER, "Timer broadcast interrupts"), 468 S(IPI_RESCHEDULE, "Rescheduling interrupts"), 469 S(IPI_CALL_FUNC, "Function call interrupts"), 470 S(IPI_CALL_FUNC_SINGLE, "Single function call interrupts"), 471 S(IPI_CPU_STOP, "CPU stop interrupts"), 472 }; 473 474 void show_ipi_list(struct seq_file *p, int prec) 475 { 476 unsigned int cpu, i; 477 478 for (i = 0; i < NR_IPI; i++) { 479 seq_printf(p, "%*s%u: ", prec - 1, "IPI", i); 480 481 for_each_online_cpu(cpu) 482 seq_printf(p, "%10u ", 483 __get_irq_stat(cpu, ipi_irqs[i])); 484 485 seq_printf(p, " %s\n", ipi_types[i]); 486 } 487 } 488 489 u64 smp_irq_stat_cpu(unsigned int cpu) 490 { 491 u64 sum = 0; 492 int i; 493 494 for (i = 0; i < NR_IPI; i++) 495 sum += __get_irq_stat(cpu, ipi_irqs[i]); 496 497 return sum; 498 } 499 500 /* 501 * Timer (local or broadcast) support 502 */ 503 static DEFINE_PER_CPU(struct clock_event_device, percpu_clockevent); 504 505 #ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST 506 void tick_broadcast(const struct cpumask *mask) 507 { 508 smp_cross_call(mask, IPI_TIMER); 509 } 510 #endif 511 512 static void broadcast_timer_set_mode(enum clock_event_mode mode, 513 struct clock_event_device *evt) 514 { 515 } 516 517 static void __cpuinit broadcast_timer_setup(struct clock_event_device *evt) 518 { 519 evt->name = "dummy_timer"; 520 evt->features = CLOCK_EVT_FEAT_ONESHOT | 521 CLOCK_EVT_FEAT_PERIODIC | 522 CLOCK_EVT_FEAT_DUMMY; 523 evt->rating = 100; 524 evt->mult = 1; 525 evt->set_mode = broadcast_timer_set_mode; 526 527 clockevents_register_device(evt); 528 } 529 530 static struct local_timer_ops *lt_ops; 531 532 #ifdef CONFIG_LOCAL_TIMERS 533 int local_timer_register(struct local_timer_ops *ops) 534 { 535 if (!is_smp() || !setup_max_cpus) 536 return -ENXIO; 537 538 if (lt_ops) 539 return -EBUSY; 540 541 lt_ops = ops; 542 return 0; 543 } 544 #endif 545 546 static void __cpuinit percpu_timer_setup(void) 547 { 548 unsigned int cpu = smp_processor_id(); 549 struct clock_event_device *evt = &per_cpu(percpu_clockevent, cpu); 550 551 evt->cpumask = cpumask_of(cpu); 552 553 if (!lt_ops || lt_ops->setup(evt)) 554 broadcast_timer_setup(evt); 555 } 556 557 #ifdef CONFIG_HOTPLUG_CPU 558 /* 559 * The generic clock events code purposely does not stop the local timer 560 * on CPU_DEAD/CPU_DEAD_FROZEN hotplug events, so we have to do it 561 * manually here. 562 */ 563 static void percpu_timer_stop(void) 564 { 565 unsigned int cpu = smp_processor_id(); 566 struct clock_event_device *evt = &per_cpu(percpu_clockevent, cpu); 567 568 if (lt_ops) 569 lt_ops->stop(evt); 570 } 571 #endif 572 573 static DEFINE_RAW_SPINLOCK(stop_lock); 574 575 /* 576 * ipi_cpu_stop - handle IPI from smp_send_stop() 577 */ 578 static void ipi_cpu_stop(unsigned int cpu) 579 { 580 if (system_state == SYSTEM_BOOTING || 581 system_state == SYSTEM_RUNNING) { 582 raw_spin_lock(&stop_lock); 583 printk(KERN_CRIT "CPU%u: stopping\n", cpu); 584 dump_stack(); 585 raw_spin_unlock(&stop_lock); 586 } 587 588 set_cpu_online(cpu, false); 589 590 local_fiq_disable(); 591 local_irq_disable(); 592 593 while (1) 594 cpu_relax(); 595 } 596 597 /* 598 * Main handler for inter-processor interrupts 599 */ 600 asmlinkage void __exception_irq_entry do_IPI(int ipinr, struct pt_regs *regs) 601 { 602 handle_IPI(ipinr, regs); 603 } 604 605 void handle_IPI(int ipinr, struct pt_regs *regs) 606 { 607 unsigned int cpu = smp_processor_id(); 608 struct pt_regs *old_regs = set_irq_regs(regs); 609 610 if (ipinr < NR_IPI) 611 __inc_irq_stat(cpu, ipi_irqs[ipinr]); 612 613 switch (ipinr) { 614 case IPI_WAKEUP: 615 break; 616 617 #ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST 618 case IPI_TIMER: 619 irq_enter(); 620 tick_receive_broadcast(); 621 irq_exit(); 622 break; 623 #endif 624 625 case IPI_RESCHEDULE: 626 scheduler_ipi(); 627 break; 628 629 case IPI_CALL_FUNC: 630 irq_enter(); 631 generic_smp_call_function_interrupt(); 632 irq_exit(); 633 break; 634 635 case IPI_CALL_FUNC_SINGLE: 636 irq_enter(); 637 generic_smp_call_function_single_interrupt(); 638 irq_exit(); 639 break; 640 641 case IPI_CPU_STOP: 642 irq_enter(); 643 ipi_cpu_stop(cpu); 644 irq_exit(); 645 break; 646 647 default: 648 printk(KERN_CRIT "CPU%u: Unknown IPI message 0x%x\n", 649 cpu, ipinr); 650 break; 651 } 652 set_irq_regs(old_regs); 653 } 654 655 void smp_send_reschedule(int cpu) 656 { 657 smp_cross_call(cpumask_of(cpu), IPI_RESCHEDULE); 658 } 659 660 #ifdef CONFIG_HOTPLUG_CPU 661 static void smp_kill_cpus(cpumask_t *mask) 662 { 663 unsigned int cpu; 664 for_each_cpu(cpu, mask) 665 platform_cpu_kill(cpu); 666 } 667 #else 668 static void smp_kill_cpus(cpumask_t *mask) { } 669 #endif 670 671 void smp_send_stop(void) 672 { 673 unsigned long timeout; 674 struct cpumask mask; 675 676 cpumask_copy(&mask, cpu_online_mask); 677 cpumask_clear_cpu(smp_processor_id(), &mask); 678 if (!cpumask_empty(&mask)) 679 smp_cross_call(&mask, IPI_CPU_STOP); 680 681 /* Wait up to one second for other CPUs to stop */ 682 timeout = USEC_PER_SEC; 683 while (num_online_cpus() > 1 && timeout--) 684 udelay(1); 685 686 if (num_online_cpus() > 1) 687 pr_warning("SMP: failed to stop secondary CPUs\n"); 688 689 smp_kill_cpus(&mask); 690 } 691 692 /* 693 * not supported here 694 */ 695 int setup_profiling_timer(unsigned int multiplier) 696 { 697 return -EINVAL; 698 } 699 700 #ifdef CONFIG_CPU_FREQ 701 702 static DEFINE_PER_CPU(unsigned long, l_p_j_ref); 703 static DEFINE_PER_CPU(unsigned long, l_p_j_ref_freq); 704 static unsigned long global_l_p_j_ref; 705 static unsigned long global_l_p_j_ref_freq; 706 707 static int cpufreq_callback(struct notifier_block *nb, 708 unsigned long val, void *data) 709 { 710 struct cpufreq_freqs *freq = data; 711 int cpu = freq->cpu; 712 713 if (freq->flags & CPUFREQ_CONST_LOOPS) 714 return NOTIFY_OK; 715 716 if (!per_cpu(l_p_j_ref, cpu)) { 717 per_cpu(l_p_j_ref, cpu) = 718 per_cpu(cpu_data, cpu).loops_per_jiffy; 719 per_cpu(l_p_j_ref_freq, cpu) = freq->old; 720 if (!global_l_p_j_ref) { 721 global_l_p_j_ref = loops_per_jiffy; 722 global_l_p_j_ref_freq = freq->old; 723 } 724 } 725 726 if ((val == CPUFREQ_PRECHANGE && freq->old < freq->new) || 727 (val == CPUFREQ_POSTCHANGE && freq->old > freq->new) || 728 (val == CPUFREQ_RESUMECHANGE || val == CPUFREQ_SUSPENDCHANGE)) { 729 loops_per_jiffy = cpufreq_scale(global_l_p_j_ref, 730 global_l_p_j_ref_freq, 731 freq->new); 732 per_cpu(cpu_data, cpu).loops_per_jiffy = 733 cpufreq_scale(per_cpu(l_p_j_ref, cpu), 734 per_cpu(l_p_j_ref_freq, cpu), 735 freq->new); 736 } 737 return NOTIFY_OK; 738 } 739 740 static struct notifier_block cpufreq_notifier = { 741 .notifier_call = cpufreq_callback, 742 }; 743 744 static int __init register_cpufreq_notifier(void) 745 { 746 return cpufreq_register_notifier(&cpufreq_notifier, 747 CPUFREQ_TRANSITION_NOTIFIER); 748 } 749 core_initcall(register_cpufreq_notifier); 750 751 #endif 752