Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
Russell King | c97d486 | 2006-10-25 13:59:16 +0100 | [diff] [blame] | 10 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Alexey Dobriyan | 4e950f6 | 2007-07-30 02:36:13 +0400 | [diff] [blame] | 20 | #include <linux/err.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <linux/cpu.h> |
| 22 | #include <linux/smp.h> |
| 23 | #include <linux/seq_file.h> |
Russell King | c97d486 | 2006-10-25 13:59:16 +0100 | [diff] [blame] | 24 | #include <linux/irq.h> |
Russell King | bc28248 | 2009-05-17 18:58:34 +0100 | [diff] [blame] | 25 | #include <linux/percpu.h> |
| 26 | #include <linux/clockchips.h> |
Russell King | 3c030be | 2010-11-30 11:07:35 +0000 | [diff] [blame] | 27 | #include <linux/completion.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 29 | #include <linux/atomic.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #include <asm/cacheflush.h> |
| 31 | #include <asm/cpu.h> |
Russell King | 42578c8 | 2009-06-11 15:35:00 +0100 | [diff] [blame] | 32 | #include <asm/cputype.h> |
Jamie Iles | 5a567d7 | 2011-10-08 11:20:42 +0100 | [diff] [blame] | 33 | #include <asm/exception.h> |
Will Deacon | 8903826 | 2011-09-30 11:43:29 +0100 | [diff] [blame] | 34 | #include <asm/idmap.h> |
Vincent Guittot | c9018aa | 2011-08-08 13:21:59 +0100 | [diff] [blame] | 35 | #include <asm/topology.h> |
Russell King | e65f38e | 2005-06-18 09:33:31 +0100 | [diff] [blame] | 36 | #include <asm/mmu_context.h> |
| 37 | #include <asm/pgtable.h> |
| 38 | #include <asm/pgalloc.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | #include <asm/processor.h> |
Russell King | 37b05b6 | 2010-10-01 15:38:24 +0100 | [diff] [blame] | 40 | #include <asm/sections.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | #include <asm/tlbflush.h> |
| 42 | #include <asm/ptrace.h> |
Russell King | bc28248 | 2009-05-17 18:58:34 +0100 | [diff] [blame] | 43 | #include <asm/localtimer.h> |
Will Deacon | d625728 | 2011-08-23 22:19:29 +0100 | [diff] [blame] | 44 | #include <asm/smp_plat.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | |
| 46 | /* |
Russell King | e65f38e | 2005-06-18 09:33:31 +0100 | [diff] [blame] | 47 | * as from 2.5, kernels no longer have an init_tasks structure |
| 48 | * so we need some other way of telling a new secondary core |
| 49 | * where to place its SVC stack |
| 50 | */ |
| 51 | struct secondary_data secondary_data; |
| 52 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | enum ipi_msg_type { |
Russell King | 24480d9 | 2010-11-15 09:54:18 +0000 | [diff] [blame] | 54 | IPI_TIMER = 2, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | IPI_RESCHEDULE, |
| 56 | IPI_CALL_FUNC, |
Jens Axboe | f6dd9fa | 2008-06-10 20:48:30 +0200 | [diff] [blame] | 57 | IPI_CALL_FUNC_SINGLE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | IPI_CPU_STOP, |
| 59 | }; |
| 60 | |
Russell King | bd6f68a | 2005-07-17 21:35:41 +0100 | [diff] [blame] | 61 | int __cpuinit __cpu_up(unsigned int cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | { |
Russell King | 71f512e8 | 2005-11-02 21:51:40 +0000 | [diff] [blame] | 63 | struct cpuinfo_arm *ci = &per_cpu(cpu_data, cpu); |
| 64 | struct task_struct *idle = ci->idle; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | int ret; |
| 66 | |
| 67 | /* |
Russell King | 71f512e8 | 2005-11-02 21:51:40 +0000 | [diff] [blame] | 68 | * Spawn a new process manually, if not already done. |
| 69 | * Grab a pointer to its task struct so we can mess with it |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | */ |
Russell King | 71f512e8 | 2005-11-02 21:51:40 +0000 | [diff] [blame] | 71 | if (!idle) { |
| 72 | idle = fork_idle(cpu); |
| 73 | if (IS_ERR(idle)) { |
| 74 | printk(KERN_ERR "CPU%u: fork() failed\n", cpu); |
| 75 | return PTR_ERR(idle); |
| 76 | } |
| 77 | ci->idle = idle; |
Santosh Shilimkar | 13ea9cc | 2010-04-30 06:51:20 +0100 | [diff] [blame] | 78 | } else { |
| 79 | /* |
| 80 | * Since this idle thread is being re-used, call |
| 81 | * init_idle() to reinitialize the thread structure. |
| 82 | */ |
| 83 | init_idle(idle, cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | /* |
Russell King | e65f38e | 2005-06-18 09:33:31 +0100 | [diff] [blame] | 87 | * We need to tell the secondary core where to find |
| 88 | * its stack and the page tables. |
| 89 | */ |
Al Viro | 32d39a9 | 2006-01-12 01:05:58 -0800 | [diff] [blame] | 90 | secondary_data.stack = task_stack_page(idle) + THREAD_START_SP; |
Will Deacon | 4e8ee7d | 2011-11-23 12:26:25 +0000 | [diff] [blame] | 91 | secondary_data.pgdir = virt_to_phys(idmap_pgd); |
Catalin Marinas | d427958 | 2011-05-26 11:22:44 +0100 | [diff] [blame] | 92 | secondary_data.swapper_pg_dir = virt_to_phys(swapper_pg_dir); |
Russell King | 1027247 | 2010-02-12 14:36:24 +0000 | [diff] [blame] | 93 | __cpuc_flush_dcache_area(&secondary_data, sizeof(secondary_data)); |
| 94 | outer_clean_range(__pa(&secondary_data), __pa(&secondary_data + 1)); |
Russell King | e65f38e | 2005-06-18 09:33:31 +0100 | [diff] [blame] | 95 | |
| 96 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | * Now bring the CPU into our world. |
| 98 | */ |
| 99 | ret = boot_secondary(cpu, idle); |
Russell King | e65f38e | 2005-06-18 09:33:31 +0100 | [diff] [blame] | 100 | if (ret == 0) { |
| 101 | unsigned long timeout; |
| 102 | |
| 103 | /* |
| 104 | * CPU was successfully started, wait for it |
| 105 | * to come online or time out. |
| 106 | */ |
| 107 | timeout = jiffies + HZ; |
| 108 | while (time_before(jiffies, timeout)) { |
| 109 | if (cpu_online(cpu)) |
| 110 | break; |
| 111 | |
| 112 | udelay(10); |
| 113 | barrier(); |
| 114 | } |
| 115 | |
Russell King | 58613cd | 2010-12-18 12:34:39 +0000 | [diff] [blame] | 116 | if (!cpu_online(cpu)) { |
| 117 | pr_crit("CPU%u: failed to come online\n", cpu); |
Russell King | e65f38e | 2005-06-18 09:33:31 +0100 | [diff] [blame] | 118 | ret = -EIO; |
Russell King | 58613cd | 2010-12-18 12:34:39 +0000 | [diff] [blame] | 119 | } |
| 120 | } else { |
| 121 | pr_err("CPU%u: failed to boot: %d\n", cpu, ret); |
Russell King | e65f38e | 2005-06-18 09:33:31 +0100 | [diff] [blame] | 122 | } |
| 123 | |
Russell King | 5d43045 | 2005-11-08 10:44:46 +0000 | [diff] [blame] | 124 | secondary_data.stack = NULL; |
Russell King | e65f38e | 2005-06-18 09:33:31 +0100 | [diff] [blame] | 125 | secondary_data.pgdir = 0; |
| 126 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | return ret; |
| 128 | } |
| 129 | |
Russell King | a054a81 | 2005-11-02 22:24:33 +0000 | [diff] [blame] | 130 | #ifdef CONFIG_HOTPLUG_CPU |
Russell King | 10034aa | 2010-12-20 14:28:02 +0000 | [diff] [blame] | 131 | static void percpu_timer_stop(void); |
| 132 | |
Russell King | a054a81 | 2005-11-02 22:24:33 +0000 | [diff] [blame] | 133 | /* |
| 134 | * __cpu_disable runs on the processor to be shutdown. |
| 135 | */ |
Russell King | 90140c3 | 2009-09-27 21:04:48 +0100 | [diff] [blame] | 136 | int __cpu_disable(void) |
Russell King | a054a81 | 2005-11-02 22:24:33 +0000 | [diff] [blame] | 137 | { |
| 138 | unsigned int cpu = smp_processor_id(); |
| 139 | struct task_struct *p; |
| 140 | int ret; |
| 141 | |
Russell King | 8e2a43f | 2010-05-15 10:18:05 +0100 | [diff] [blame] | 142 | ret = platform_cpu_disable(cpu); |
Russell King | a054a81 | 2005-11-02 22:24:33 +0000 | [diff] [blame] | 143 | if (ret) |
| 144 | return ret; |
| 145 | |
| 146 | /* |
| 147 | * Take this CPU offline. Once we clear this, we can't return, |
| 148 | * and we must not schedule until we're ready to give up the cpu. |
| 149 | */ |
Russell King | e03cdad | 2009-05-28 14:16:52 +0100 | [diff] [blame] | 150 | set_cpu_online(cpu, false); |
Russell King | a054a81 | 2005-11-02 22:24:33 +0000 | [diff] [blame] | 151 | |
| 152 | /* |
| 153 | * OK - migrate IRQs away from this CPU |
| 154 | */ |
| 155 | migrate_irqs(); |
| 156 | |
| 157 | /* |
Russell King | 37ee16a | 2005-11-08 19:08:05 +0000 | [diff] [blame] | 158 | * Stop the local timer for this CPU. |
| 159 | */ |
Russell King | 10034aa | 2010-12-20 14:28:02 +0000 | [diff] [blame] | 160 | percpu_timer_stop(); |
Russell King | 37ee16a | 2005-11-08 19:08:05 +0000 | [diff] [blame] | 161 | |
| 162 | /* |
Russell King | a054a81 | 2005-11-02 22:24:33 +0000 | [diff] [blame] | 163 | * Flush user cache and TLB mappings, and then remove this CPU |
| 164 | * from the vm mask set of all processes. |
| 165 | */ |
| 166 | flush_cache_all(); |
| 167 | local_flush_tlb_all(); |
| 168 | |
| 169 | read_lock(&tasklist_lock); |
| 170 | for_each_process(p) { |
| 171 | if (p->mm) |
Rusty Russell | 56f8ba8 | 2009-09-24 09:34:49 -0600 | [diff] [blame] | 172 | cpumask_clear_cpu(cpu, mm_cpumask(p->mm)); |
Russell King | a054a81 | 2005-11-02 22:24:33 +0000 | [diff] [blame] | 173 | } |
| 174 | read_unlock(&tasklist_lock); |
| 175 | |
| 176 | return 0; |
| 177 | } |
| 178 | |
Russell King | 3c030be | 2010-11-30 11:07:35 +0000 | [diff] [blame] | 179 | static DECLARE_COMPLETION(cpu_died); |
| 180 | |
Russell King | a054a81 | 2005-11-02 22:24:33 +0000 | [diff] [blame] | 181 | /* |
| 182 | * called on the thread which is asking for a CPU to be shutdown - |
| 183 | * waits until shutdown has completed, or it is timed out. |
| 184 | */ |
Russell King | 90140c3 | 2009-09-27 21:04:48 +0100 | [diff] [blame] | 185 | void __cpu_die(unsigned int cpu) |
Russell King | a054a81 | 2005-11-02 22:24:33 +0000 | [diff] [blame] | 186 | { |
Russell King | 3c030be | 2010-11-30 11:07:35 +0000 | [diff] [blame] | 187 | if (!wait_for_completion_timeout(&cpu_died, msecs_to_jiffies(5000))) { |
| 188 | pr_err("CPU%u: cpu didn't die\n", cpu); |
| 189 | return; |
| 190 | } |
| 191 | printk(KERN_NOTICE "CPU%u: shutdown\n", cpu); |
| 192 | |
Russell King | a054a81 | 2005-11-02 22:24:33 +0000 | [diff] [blame] | 193 | if (!platform_cpu_kill(cpu)) |
| 194 | printk("CPU%u: unable to kill\n", cpu); |
| 195 | } |
| 196 | |
| 197 | /* |
| 198 | * Called from the idle thread for the CPU which has been shutdown. |
| 199 | * |
| 200 | * Note that we disable IRQs here, but do not re-enable them |
| 201 | * before returning to the caller. This is also the behaviour |
| 202 | * of the other hotplug-cpu capable cores, so presumably coming |
| 203 | * out of idle fixes this. |
| 204 | */ |
Russell King | 90140c3 | 2009-09-27 21:04:48 +0100 | [diff] [blame] | 205 | void __ref cpu_die(void) |
Russell King | a054a81 | 2005-11-02 22:24:33 +0000 | [diff] [blame] | 206 | { |
| 207 | unsigned int cpu = smp_processor_id(); |
| 208 | |
Russell King | a054a81 | 2005-11-02 22:24:33 +0000 | [diff] [blame] | 209 | idle_task_exit(); |
| 210 | |
Russell King | f36d340 | 2010-11-30 12:21:30 +0000 | [diff] [blame] | 211 | local_irq_disable(); |
| 212 | mb(); |
| 213 | |
Russell King | 3c030be | 2010-11-30 11:07:35 +0000 | [diff] [blame] | 214 | /* Tell __cpu_die() that this CPU is now safe to dispose of */ |
| 215 | complete(&cpu_died); |
| 216 | |
Russell King | a054a81 | 2005-11-02 22:24:33 +0000 | [diff] [blame] | 217 | /* |
| 218 | * actual CPU shutdown procedure is at least platform (if not |
Russell King | 3c030be | 2010-11-30 11:07:35 +0000 | [diff] [blame] | 219 | * CPU) specific. |
Russell King | a054a81 | 2005-11-02 22:24:33 +0000 | [diff] [blame] | 220 | */ |
| 221 | platform_cpu_die(cpu); |
| 222 | |
| 223 | /* |
| 224 | * Do not return to the idle loop - jump back to the secondary |
| 225 | * cpu initialisation. There's some initialisation which needs |
| 226 | * to be repeated to undo the effects of taking the CPU offline. |
| 227 | */ |
| 228 | __asm__("mov sp, %0\n" |
Russell King | faabfa0 | 2010-12-20 16:58:19 +0000 | [diff] [blame] | 229 | " mov fp, #0\n" |
Russell King | a054a81 | 2005-11-02 22:24:33 +0000 | [diff] [blame] | 230 | " b secondary_start_kernel" |
| 231 | : |
Al Viro | 32d39a9 | 2006-01-12 01:05:58 -0800 | [diff] [blame] | 232 | : "r" (task_stack_page(current) + THREAD_SIZE - 8)); |
Russell King | a054a81 | 2005-11-02 22:24:33 +0000 | [diff] [blame] | 233 | } |
| 234 | #endif /* CONFIG_HOTPLUG_CPU */ |
| 235 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | /* |
Russell King | 05c74a6 | 2010-12-03 11:09:48 +0000 | [diff] [blame] | 237 | * Called by both boot and secondaries to move global data into |
| 238 | * per-processor storage. |
| 239 | */ |
| 240 | static void __cpuinit smp_store_cpu_info(unsigned int cpuid) |
| 241 | { |
| 242 | struct cpuinfo_arm *cpu_info = &per_cpu(cpu_data, cpuid); |
| 243 | |
| 244 | cpu_info->loops_per_jiffy = loops_per_jiffy; |
Vincent Guittot | c9018aa | 2011-08-08 13:21:59 +0100 | [diff] [blame] | 245 | |
| 246 | store_cpu_topology(cpuid); |
Russell King | 05c74a6 | 2010-12-03 11:09:48 +0000 | [diff] [blame] | 247 | } |
| 248 | |
Marc Zyngier | d457859 | 2012-01-10 23:38:25 +0000 | [diff] [blame] | 249 | static void percpu_timer_setup(void); |
| 250 | |
Russell King | 05c74a6 | 2010-12-03 11:09:48 +0000 | [diff] [blame] | 251 | /* |
Russell King | e65f38e | 2005-06-18 09:33:31 +0100 | [diff] [blame] | 252 | * This is the secondary CPU boot entry. We're using this CPUs |
| 253 | * idle thread stack, but a set of temporary page tables. |
| 254 | */ |
Russell King | bd6f68a | 2005-07-17 21:35:41 +0100 | [diff] [blame] | 255 | asmlinkage void __cpuinit secondary_start_kernel(void) |
Russell King | e65f38e | 2005-06-18 09:33:31 +0100 | [diff] [blame] | 256 | { |
| 257 | struct mm_struct *mm = &init_mm; |
Russell King | da2660d | 2005-11-12 17:21:47 +0000 | [diff] [blame] | 258 | unsigned int cpu = smp_processor_id(); |
Russell King | e65f38e | 2005-06-18 09:33:31 +0100 | [diff] [blame] | 259 | |
| 260 | printk("CPU%u: Booted secondary processor\n", cpu); |
| 261 | |
| 262 | /* |
| 263 | * All kernel threads share the same mm context; grab a |
| 264 | * reference and switch to it. |
| 265 | */ |
Russell King | e65f38e | 2005-06-18 09:33:31 +0100 | [diff] [blame] | 266 | atomic_inc(&mm->mm_count); |
| 267 | current->active_mm = mm; |
Rusty Russell | 56f8ba8 | 2009-09-24 09:34:49 -0600 | [diff] [blame] | 268 | cpumask_set_cpu(cpu, mm_cpumask(mm)); |
Russell King | e65f38e | 2005-06-18 09:33:31 +0100 | [diff] [blame] | 269 | cpu_switch_mm(mm->pgd, mm); |
| 270 | enter_lazy_tlb(mm, current); |
Russell King | 505d7b1 | 2005-07-28 20:32:47 +0100 | [diff] [blame] | 271 | local_flush_tlb_all(); |
Russell King | e65f38e | 2005-06-18 09:33:31 +0100 | [diff] [blame] | 272 | |
| 273 | cpu_init(); |
Nick Piggin | 5bfb5d6 | 2005-11-08 21:39:01 -0800 | [diff] [blame] | 274 | preempt_disable(); |
Russell King | 2c0136d | 2010-12-03 15:00:49 +0000 | [diff] [blame] | 275 | trace_hardirqs_off(); |
Russell King | e65f38e | 2005-06-18 09:33:31 +0100 | [diff] [blame] | 276 | |
| 277 | /* |
| 278 | * Give the platform a chance to do its own initialisation. |
| 279 | */ |
| 280 | platform_secondary_init(cpu); |
| 281 | |
Manfred Spraul | e545a61 | 2008-09-07 16:57:22 +0200 | [diff] [blame] | 282 | notify_cpu_starting(cpu); |
Catalin Marinas | a8655e8 | 2008-02-04 17:30:57 +0100 | [diff] [blame] | 283 | |
Russell King | e65f38e | 2005-06-18 09:33:31 +0100 | [diff] [blame] | 284 | calibrate_delay(); |
| 285 | |
| 286 | smp_store_cpu_info(cpu); |
| 287 | |
| 288 | /* |
Russell King | 573619d | 2011-06-20 16:46:01 +0100 | [diff] [blame] | 289 | * OK, now it's safe to let the boot CPU continue. Wait for |
| 290 | * the CPU migration code to notice that the CPU is online |
| 291 | * before we continue. |
Russell King | e65f38e | 2005-06-18 09:33:31 +0100 | [diff] [blame] | 292 | */ |
Russell King | e03cdad | 2009-05-28 14:16:52 +0100 | [diff] [blame] | 293 | set_cpu_online(cpu, true); |
Thomas Gleinxer | eb04745 | 2011-10-14 12:44:41 +0100 | [diff] [blame] | 294 | |
| 295 | /* |
| 296 | * Setup the percpu timer for this CPU. |
| 297 | */ |
| 298 | percpu_timer_setup(); |
| 299 | |
Thomas Gleinxer | eb04745 | 2011-10-14 12:44:41 +0100 | [diff] [blame] | 300 | local_irq_enable(); |
| 301 | local_fiq_enable(); |
| 302 | |
| 303 | /* |
Russell King | e65f38e | 2005-06-18 09:33:31 +0100 | [diff] [blame] | 304 | * OK, it's off to the idle thread for us |
| 305 | */ |
| 306 | cpu_idle(); |
| 307 | } |
| 308 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | void __init smp_cpus_done(unsigned int max_cpus) |
| 310 | { |
| 311 | int cpu; |
| 312 | unsigned long bogosum = 0; |
| 313 | |
| 314 | for_each_online_cpu(cpu) |
| 315 | bogosum += per_cpu(cpu_data, cpu).loops_per_jiffy; |
| 316 | |
| 317 | printk(KERN_INFO "SMP: Total of %d processors activated " |
| 318 | "(%lu.%02lu BogoMIPS).\n", |
| 319 | num_online_cpus(), |
| 320 | bogosum / (500000/HZ), |
| 321 | (bogosum / (5000/HZ)) % 100); |
| 322 | } |
| 323 | |
| 324 | void __init smp_prepare_boot_cpu(void) |
| 325 | { |
| 326 | unsigned int cpu = smp_processor_id(); |
| 327 | |
Russell King | 71f512e8 | 2005-11-02 21:51:40 +0000 | [diff] [blame] | 328 | per_cpu(cpu_data, cpu).idle = current; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | } |
| 330 | |
Russell King | 05c74a6 | 2010-12-03 11:09:48 +0000 | [diff] [blame] | 331 | void __init smp_prepare_cpus(unsigned int max_cpus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | { |
Russell King | 05c74a6 | 2010-12-03 11:09:48 +0000 | [diff] [blame] | 333 | unsigned int ncores = num_possible_cpus(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | |
Vincent Guittot | c9018aa | 2011-08-08 13:21:59 +0100 | [diff] [blame] | 335 | init_cpu_topology(); |
| 336 | |
Russell King | 05c74a6 | 2010-12-03 11:09:48 +0000 | [diff] [blame] | 337 | smp_store_cpu_info(smp_processor_id()); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | |
| 339 | /* |
Russell King | 05c74a6 | 2010-12-03 11:09:48 +0000 | [diff] [blame] | 340 | * are we trying to boot more cores than exist? |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | */ |
Russell King | 05c74a6 | 2010-12-03 11:09:48 +0000 | [diff] [blame] | 342 | if (max_cpus > ncores) |
| 343 | max_cpus = ncores; |
Stephen Boyd | 7fa22bd | 2011-07-07 01:56:51 +0100 | [diff] [blame] | 344 | if (ncores > 1 && max_cpus) { |
Russell King | 05c74a6 | 2010-12-03 11:09:48 +0000 | [diff] [blame] | 345 | /* |
| 346 | * Enable the local timer or broadcast device for the |
| 347 | * boot CPU, but only if we have more than one CPU. |
| 348 | */ |
| 349 | percpu_timer_setup(); |
| 350 | |
| 351 | /* |
Stephen Boyd | 7fa22bd | 2011-07-07 01:56:51 +0100 | [diff] [blame] | 352 | * Initialise the present map, which describes the set of CPUs |
| 353 | * actually populated at the present time. A platform should |
| 354 | * re-initialize the map in platform_smp_prepare_cpus() if |
| 355 | * present != possible (e.g. physical hotplug). |
| 356 | */ |
Rusty Russell | 0b5f9c0 | 2012-03-29 15:38:30 +1030 | [diff] [blame^] | 357 | init_cpu_present(cpu_possible_mask); |
Stephen Boyd | 7fa22bd | 2011-07-07 01:56:51 +0100 | [diff] [blame] | 358 | |
| 359 | /* |
Russell King | 05c74a6 | 2010-12-03 11:09:48 +0000 | [diff] [blame] | 360 | * Initialise the SCU if there are more than one CPU |
| 361 | * and let them know where to start. |
| 362 | */ |
| 363 | platform_smp_prepare_cpus(max_cpus); |
| 364 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | } |
| 366 | |
Russell King | 0f7b332 | 2011-04-03 13:01:30 +0100 | [diff] [blame] | 367 | static void (*smp_cross_call)(const struct cpumask *, unsigned int); |
| 368 | |
| 369 | void __init set_smp_cross_call(void (*fn)(const struct cpumask *, unsigned int)) |
| 370 | { |
| 371 | smp_cross_call = fn; |
| 372 | } |
| 373 | |
Russell King | 8266810 | 2009-05-17 16:20:18 +0100 | [diff] [blame] | 374 | void arch_send_call_function_ipi_mask(const struct cpumask *mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | { |
Russell King | e3fbb08 | 2010-12-20 14:47:19 +0000 | [diff] [blame] | 376 | smp_cross_call(mask, IPI_CALL_FUNC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | } |
| 378 | |
Jens Axboe | f6dd9fa | 2008-06-10 20:48:30 +0200 | [diff] [blame] | 379 | void arch_send_call_function_single_ipi(int cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | { |
Russell King | e3fbb08 | 2010-12-20 14:47:19 +0000 | [diff] [blame] | 381 | smp_cross_call(cpumask_of(cpu), IPI_CALL_FUNC_SINGLE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | } |
Catalin Marinas | 3e45999 | 2008-02-04 17:28:56 +0100 | [diff] [blame] | 383 | |
Russell King | 4a88abd | 2010-11-15 14:40:29 +0000 | [diff] [blame] | 384 | static const char *ipi_types[NR_IPI] = { |
| 385 | #define S(x,s) [x - IPI_TIMER] = s |
| 386 | S(IPI_TIMER, "Timer broadcast interrupts"), |
| 387 | S(IPI_RESCHEDULE, "Rescheduling interrupts"), |
| 388 | S(IPI_CALL_FUNC, "Function call interrupts"), |
| 389 | S(IPI_CALL_FUNC_SINGLE, "Single function call interrupts"), |
| 390 | S(IPI_CPU_STOP, "CPU stop interrupts"), |
| 391 | }; |
| 392 | |
Russell King | f13cd41 | 2010-11-15 14:33:51 +0000 | [diff] [blame] | 393 | void show_ipi_list(struct seq_file *p, int prec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | { |
Russell King | 4a88abd | 2010-11-15 14:40:29 +0000 | [diff] [blame] | 395 | unsigned int cpu, i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | |
Russell King | 4a88abd | 2010-11-15 14:40:29 +0000 | [diff] [blame] | 397 | for (i = 0; i < NR_IPI; i++) { |
| 398 | seq_printf(p, "%*s%u: ", prec - 1, "IPI", i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | |
Russell King | 4a88abd | 2010-11-15 14:40:29 +0000 | [diff] [blame] | 400 | for_each_present_cpu(cpu) |
| 401 | seq_printf(p, "%10u ", |
| 402 | __get_irq_stat(cpu, ipi_irqs[i])); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | |
Russell King | 4a88abd | 2010-11-15 14:40:29 +0000 | [diff] [blame] | 404 | seq_printf(p, " %s\n", ipi_types[i]); |
| 405 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | } |
| 407 | |
Russell King | b54992f | 2010-11-15 14:46:46 +0000 | [diff] [blame] | 408 | u64 smp_irq_stat_cpu(unsigned int cpu) |
Russell King | 37ee16a | 2005-11-08 19:08:05 +0000 | [diff] [blame] | 409 | { |
Russell King | b54992f | 2010-11-15 14:46:46 +0000 | [diff] [blame] | 410 | u64 sum = 0; |
| 411 | int i; |
Russell King | 37ee16a | 2005-11-08 19:08:05 +0000 | [diff] [blame] | 412 | |
Russell King | b54992f | 2010-11-15 14:46:46 +0000 | [diff] [blame] | 413 | for (i = 0; i < NR_IPI; i++) |
| 414 | sum += __get_irq_stat(cpu, ipi_irqs[i]); |
Russell King | 37ee16a | 2005-11-08 19:08:05 +0000 | [diff] [blame] | 415 | |
Russell King | b54992f | 2010-11-15 14:46:46 +0000 | [diff] [blame] | 416 | return sum; |
Russell King | 37ee16a | 2005-11-08 19:08:05 +0000 | [diff] [blame] | 417 | } |
| 418 | |
Russell King | bc28248 | 2009-05-17 18:58:34 +0100 | [diff] [blame] | 419 | /* |
| 420 | * Timer (local or broadcast) support |
| 421 | */ |
| 422 | static DEFINE_PER_CPU(struct clock_event_device, percpu_clockevent); |
| 423 | |
Russell King | c97d486 | 2006-10-25 13:59:16 +0100 | [diff] [blame] | 424 | static void ipi_timer(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | { |
Russell King | bc28248 | 2009-05-17 18:58:34 +0100 | [diff] [blame] | 426 | struct clock_event_device *evt = &__get_cpu_var(percpu_clockevent); |
Russell King | bc28248 | 2009-05-17 18:58:34 +0100 | [diff] [blame] | 427 | evt->event_handler(evt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | } |
| 429 | |
Russell King | bc28248 | 2009-05-17 18:58:34 +0100 | [diff] [blame] | 430 | #ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST |
| 431 | static void smp_timer_broadcast(const struct cpumask *mask) |
| 432 | { |
Russell King | e3fbb08 | 2010-12-20 14:47:19 +0000 | [diff] [blame] | 433 | smp_cross_call(mask, IPI_TIMER); |
Russell King | bc28248 | 2009-05-17 18:58:34 +0100 | [diff] [blame] | 434 | } |
Russell King | 5388a6b | 2010-07-26 13:19:43 +0100 | [diff] [blame] | 435 | #else |
| 436 | #define smp_timer_broadcast NULL |
| 437 | #endif |
Russell King | bc28248 | 2009-05-17 18:58:34 +0100 | [diff] [blame] | 438 | |
| 439 | static void broadcast_timer_set_mode(enum clock_event_mode mode, |
| 440 | struct clock_event_device *evt) |
| 441 | { |
| 442 | } |
| 443 | |
Stephen Boyd | a8d2518 | 2011-04-27 01:34:27 +0100 | [diff] [blame] | 444 | static void __cpuinit broadcast_timer_setup(struct clock_event_device *evt) |
Russell King | bc28248 | 2009-05-17 18:58:34 +0100 | [diff] [blame] | 445 | { |
| 446 | evt->name = "dummy_timer"; |
| 447 | evt->features = CLOCK_EVT_FEAT_ONESHOT | |
| 448 | CLOCK_EVT_FEAT_PERIODIC | |
| 449 | CLOCK_EVT_FEAT_DUMMY; |
| 450 | evt->rating = 400; |
| 451 | evt->mult = 1; |
| 452 | evt->set_mode = broadcast_timer_set_mode; |
Russell King | bc28248 | 2009-05-17 18:58:34 +0100 | [diff] [blame] | 453 | |
| 454 | clockevents_register_device(evt); |
| 455 | } |
Russell King | bc28248 | 2009-05-17 18:58:34 +0100 | [diff] [blame] | 456 | |
Marc Zyngier | 0ef330e | 2012-01-10 19:26:45 +0000 | [diff] [blame] | 457 | static struct local_timer_ops *lt_ops; |
| 458 | |
| 459 | #ifdef CONFIG_LOCAL_TIMERS |
| 460 | int local_timer_register(struct local_timer_ops *ops) |
| 461 | { |
| 462 | if (lt_ops) |
| 463 | return -EBUSY; |
| 464 | |
| 465 | lt_ops = ops; |
| 466 | return 0; |
| 467 | } |
| 468 | #endif |
| 469 | |
Marc Zyngier | d457859 | 2012-01-10 23:38:25 +0000 | [diff] [blame] | 470 | static void __cpuinit percpu_timer_setup(void) |
Russell King | bc28248 | 2009-05-17 18:58:34 +0100 | [diff] [blame] | 471 | { |
| 472 | unsigned int cpu = smp_processor_id(); |
| 473 | struct clock_event_device *evt = &per_cpu(percpu_clockevent, cpu); |
| 474 | |
| 475 | evt->cpumask = cpumask_of(cpu); |
Russell King | 5388a6b | 2010-07-26 13:19:43 +0100 | [diff] [blame] | 476 | evt->broadcast = smp_timer_broadcast; |
Russell King | bc28248 | 2009-05-17 18:58:34 +0100 | [diff] [blame] | 477 | |
Marc Zyngier | d457859 | 2012-01-10 23:38:25 +0000 | [diff] [blame] | 478 | if (!lt_ops || lt_ops->setup(evt)) |
Santosh Shilimkar | af90f10 | 2011-02-23 18:53:15 +0100 | [diff] [blame] | 479 | broadcast_timer_setup(evt); |
Russell King | bc28248 | 2009-05-17 18:58:34 +0100 | [diff] [blame] | 480 | } |
| 481 | |
Russell King | 10034aa | 2010-12-20 14:28:02 +0000 | [diff] [blame] | 482 | #ifdef CONFIG_HOTPLUG_CPU |
| 483 | /* |
| 484 | * The generic clock events code purposely does not stop the local timer |
| 485 | * on CPU_DEAD/CPU_DEAD_FROZEN hotplug events, so we have to do it |
| 486 | * manually here. |
| 487 | */ |
| 488 | static void percpu_timer_stop(void) |
| 489 | { |
| 490 | unsigned int cpu = smp_processor_id(); |
| 491 | struct clock_event_device *evt = &per_cpu(percpu_clockevent, cpu); |
| 492 | |
Marc Zyngier | d457859 | 2012-01-10 23:38:25 +0000 | [diff] [blame] | 493 | if (lt_ops) |
| 494 | lt_ops->stop(evt); |
Russell King | 10034aa | 2010-12-20 14:28:02 +0000 | [diff] [blame] | 495 | } |
| 496 | #endif |
| 497 | |
Thomas Gleixner | bd31b85 | 2009-07-03 08:44:46 -0500 | [diff] [blame] | 498 | static DEFINE_RAW_SPINLOCK(stop_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | |
| 500 | /* |
| 501 | * ipi_cpu_stop - handle IPI from smp_send_stop() |
| 502 | */ |
| 503 | static void ipi_cpu_stop(unsigned int cpu) |
| 504 | { |
Russell King | 3d3f78d | 2010-07-26 13:31:27 +0100 | [diff] [blame] | 505 | if (system_state == SYSTEM_BOOTING || |
| 506 | system_state == SYSTEM_RUNNING) { |
Thomas Gleixner | bd31b85 | 2009-07-03 08:44:46 -0500 | [diff] [blame] | 507 | raw_spin_lock(&stop_lock); |
Russell King | 3d3f78d | 2010-07-26 13:31:27 +0100 | [diff] [blame] | 508 | printk(KERN_CRIT "CPU%u: stopping\n", cpu); |
| 509 | dump_stack(); |
Thomas Gleixner | bd31b85 | 2009-07-03 08:44:46 -0500 | [diff] [blame] | 510 | raw_spin_unlock(&stop_lock); |
Russell King | 3d3f78d | 2010-07-26 13:31:27 +0100 | [diff] [blame] | 511 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | |
Russell King | e03cdad | 2009-05-28 14:16:52 +0100 | [diff] [blame] | 513 | set_cpu_online(cpu, false); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | |
| 515 | local_fiq_disable(); |
| 516 | local_irq_disable(); |
| 517 | |
Will Deacon | 02b73e2 | 2011-06-06 15:49:23 +0100 | [diff] [blame] | 518 | #ifdef CONFIG_HOTPLUG_CPU |
| 519 | platform_cpu_kill(cpu); |
| 520 | #endif |
| 521 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | while (1) |
| 523 | cpu_relax(); |
| 524 | } |
| 525 | |
| 526 | /* |
| 527 | * Main handler for inter-processor interrupts |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | */ |
Russell King | 4073723 | 2011-01-06 22:32:52 +0000 | [diff] [blame] | 529 | asmlinkage void __exception_irq_entry do_IPI(int ipinr, struct pt_regs *regs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 530 | { |
Shawn Guo | 0b5a1b95 | 2011-10-06 15:18:14 +0100 | [diff] [blame] | 531 | handle_IPI(ipinr, regs); |
| 532 | } |
| 533 | |
| 534 | void handle_IPI(int ipinr, struct pt_regs *regs) |
| 535 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | unsigned int cpu = smp_processor_id(); |
Russell King | c97d486 | 2006-10-25 13:59:16 +0100 | [diff] [blame] | 537 | struct pt_regs *old_regs = set_irq_regs(regs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 | |
Russell King | 4a88abd | 2010-11-15 14:40:29 +0000 | [diff] [blame] | 539 | if (ipinr >= IPI_TIMER && ipinr < IPI_TIMER + NR_IPI) |
| 540 | __inc_irq_stat(cpu, ipi_irqs[ipinr - IPI_TIMER]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | |
Russell King | 24480d9 | 2010-11-15 09:54:18 +0000 | [diff] [blame] | 542 | switch (ipinr) { |
| 543 | case IPI_TIMER: |
Russell King | 7deabca | 2012-01-19 15:20:58 +0000 | [diff] [blame] | 544 | irq_enter(); |
Russell King | 24480d9 | 2010-11-15 09:54:18 +0000 | [diff] [blame] | 545 | ipi_timer(); |
Russell King | 7deabca | 2012-01-19 15:20:58 +0000 | [diff] [blame] | 546 | irq_exit(); |
Russell King | 24480d9 | 2010-11-15 09:54:18 +0000 | [diff] [blame] | 547 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | |
Russell King | 24480d9 | 2010-11-15 09:54:18 +0000 | [diff] [blame] | 549 | case IPI_RESCHEDULE: |
Peter Zijlstra | 184748c | 2011-04-05 17:23:39 +0200 | [diff] [blame] | 550 | scheduler_ipi(); |
Russell King | 24480d9 | 2010-11-15 09:54:18 +0000 | [diff] [blame] | 551 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | |
Russell King | 24480d9 | 2010-11-15 09:54:18 +0000 | [diff] [blame] | 553 | case IPI_CALL_FUNC: |
Russell King | 7deabca | 2012-01-19 15:20:58 +0000 | [diff] [blame] | 554 | irq_enter(); |
Russell King | 24480d9 | 2010-11-15 09:54:18 +0000 | [diff] [blame] | 555 | generic_smp_call_function_interrupt(); |
Russell King | 7deabca | 2012-01-19 15:20:58 +0000 | [diff] [blame] | 556 | irq_exit(); |
Russell King | 24480d9 | 2010-11-15 09:54:18 +0000 | [diff] [blame] | 557 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | |
Russell King | 24480d9 | 2010-11-15 09:54:18 +0000 | [diff] [blame] | 559 | case IPI_CALL_FUNC_SINGLE: |
Russell King | 7deabca | 2012-01-19 15:20:58 +0000 | [diff] [blame] | 560 | irq_enter(); |
Russell King | 24480d9 | 2010-11-15 09:54:18 +0000 | [diff] [blame] | 561 | generic_smp_call_function_single_interrupt(); |
Russell King | 7deabca | 2012-01-19 15:20:58 +0000 | [diff] [blame] | 562 | irq_exit(); |
Russell King | 24480d9 | 2010-11-15 09:54:18 +0000 | [diff] [blame] | 563 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | |
Russell King | 24480d9 | 2010-11-15 09:54:18 +0000 | [diff] [blame] | 565 | case IPI_CPU_STOP: |
Russell King | 7deabca | 2012-01-19 15:20:58 +0000 | [diff] [blame] | 566 | irq_enter(); |
Russell King | 24480d9 | 2010-11-15 09:54:18 +0000 | [diff] [blame] | 567 | ipi_cpu_stop(cpu); |
Russell King | 7deabca | 2012-01-19 15:20:58 +0000 | [diff] [blame] | 568 | irq_exit(); |
Russell King | 24480d9 | 2010-11-15 09:54:18 +0000 | [diff] [blame] | 569 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | |
Russell King | 24480d9 | 2010-11-15 09:54:18 +0000 | [diff] [blame] | 571 | default: |
| 572 | printk(KERN_CRIT "CPU%u: Unknown IPI message 0x%x\n", |
| 573 | cpu, ipinr); |
| 574 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | } |
Russell King | c97d486 | 2006-10-25 13:59:16 +0100 | [diff] [blame] | 576 | set_irq_regs(old_regs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | void smp_send_reschedule(int cpu) |
| 580 | { |
Russell King | e3fbb08 | 2010-12-20 14:47:19 +0000 | [diff] [blame] | 581 | smp_cross_call(cpumask_of(cpu), IPI_RESCHEDULE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | } |
| 583 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 | void smp_send_stop(void) |
| 585 | { |
Russell King | 28e1829 | 2010-12-02 09:53:54 +0000 | [diff] [blame] | 586 | unsigned long timeout; |
| 587 | |
| 588 | if (num_online_cpus() > 1) { |
Rusty Russell | 0b5f9c0 | 2012-03-29 15:38:30 +1030 | [diff] [blame^] | 589 | struct cpumask mask; |
| 590 | cpumask_copy(&mask, cpu_online_mask); |
| 591 | cpumask_clear_cpu(smp_processor_id(), &mask); |
Russell King | 28e1829 | 2010-12-02 09:53:54 +0000 | [diff] [blame] | 592 | |
Russell King | e3fbb08 | 2010-12-20 14:47:19 +0000 | [diff] [blame] | 593 | smp_cross_call(&mask, IPI_CPU_STOP); |
Russell King | 28e1829 | 2010-12-02 09:53:54 +0000 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | /* Wait up to one second for other CPUs to stop */ |
| 597 | timeout = USEC_PER_SEC; |
| 598 | while (num_online_cpus() > 1 && timeout--) |
| 599 | udelay(1); |
| 600 | |
| 601 | if (num_online_cpus() > 1) |
| 602 | pr_warning("SMP: failed to stop secondary CPUs\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | /* |
| 606 | * not supported here |
| 607 | */ |
Russell King | 5048bcb | 2007-07-23 12:59:46 +0100 | [diff] [blame] | 608 | int setup_profiling_timer(unsigned int multiplier) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 | { |
| 610 | return -EINVAL; |
| 611 | } |