Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * x86 SMP booting functions |
| 3 | * |
| 4 | * (c) 1995 Alan Cox, Building #3 <alan@redhat.com> |
| 5 | * (c) 1998, 1999, 2000 Ingo Molnar <mingo@redhat.com> |
| 6 | * Copyright 2001 Andi Kleen, SuSE Labs. |
| 7 | * |
| 8 | * Much of the core SMP work is based on previous work by Thomas Radke, to |
| 9 | * whom a great many thanks are extended. |
| 10 | * |
| 11 | * Thanks to Intel for making available several different Pentium, |
| 12 | * Pentium Pro and Pentium-II/Xeon MP machines. |
| 13 | * Original development of Linux SMP code supported by Caldera. |
| 14 | * |
Andi Kleen | a8ab26f | 2005-04-16 15:25:19 -0700 | [diff] [blame] | 15 | * This code is released under the GNU General Public License version 2 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | * |
| 17 | * Fixes |
| 18 | * Felix Koop : NR_CPUS used properly |
| 19 | * Jose Renau : Handle single CPU case. |
| 20 | * Alan Cox : By repeated request 8) - Total BogoMIP report. |
| 21 | * Greg Wright : Fix for kernel stacks panic. |
| 22 | * Erich Boleyn : MP v1.4 and additional changes. |
| 23 | * Matthias Sattler : Changes for 2.1 kernel map. |
| 24 | * Michel Lespinasse : Changes for 2.1 kernel map. |
| 25 | * Michael Chastain : Change trampoline.S to gnu as. |
| 26 | * Alan Cox : Dumb bug: 'B' step PPro's are fine |
| 27 | * Ingo Molnar : Added APIC timers, based on code |
| 28 | * from Jose Renau |
| 29 | * Ingo Molnar : various cleanups and rewrites |
| 30 | * Tigran Aivazian : fixed "0.00 in /proc/uptime on SMP" bug. |
| 31 | * Maciej W. Rozycki : Bits for genuine 82489DX APICs |
| 32 | * Andi Kleen : Changed for SMP boot into long mode. |
Andi Kleen | a8ab26f | 2005-04-16 15:25:19 -0700 | [diff] [blame] | 33 | * Rusty Russell : Hacked into shape for new "hotplug" boot process. |
| 34 | * Andi Kleen : Converted to new state machine. |
| 35 | * Various cleanups. |
| 36 | * Probably mostly hotplug CPU ready now. |
Ashok Raj | 76e4f66 | 2005-06-25 14:55:00 -0700 | [diff] [blame] | 37 | * Ashok Raj : CPU hotplug support |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | */ |
| 39 | |
Andi Kleen | a8ab26f | 2005-04-16 15:25:19 -0700 | [diff] [blame] | 40 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | #include <linux/init.h> |
| 42 | |
| 43 | #include <linux/mm.h> |
| 44 | #include <linux/kernel_stat.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | #include <linux/bootmem.h> |
| 46 | #include <linux/thread_info.h> |
| 47 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | #include <linux/delay.h> |
| 49 | #include <linux/mc146818rtc.h> |
Andrew Morton | a3bc0db | 2006-09-25 23:32:33 -0700 | [diff] [blame] | 50 | #include <linux/smp.h> |
Christoph Hellwig | 1eeb66a | 2007-05-08 00:27:03 -0700 | [diff] [blame] | 51 | #include <linux/kdebug.h> |
Andrew Morton | a3bc0db | 2006-09-25 23:32:33 -0700 | [diff] [blame] | 52 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | #include <asm/mtrr.h> |
| 54 | #include <asm/pgalloc.h> |
| 55 | #include <asm/desc.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | #include <asm/tlbflush.h> |
| 57 | #include <asm/proto.h> |
Andi Kleen | 7515211 | 2005-05-16 21:53:34 -0700 | [diff] [blame] | 58 | #include <asm/nmi.h> |
Al Viro | 9cdd304 | 2005-09-12 18:49:25 +0200 | [diff] [blame] | 59 | #include <asm/irq.h> |
| 60 | #include <asm/hw_irq.h> |
Ravikiran G Thirumalai | 488fc08 | 2006-02-07 12:58:23 -0800 | [diff] [blame] | 61 | #include <asm/numa.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | |
Andi Kleen | a8ab26f | 2005-04-16 15:25:19 -0700 | [diff] [blame] | 63 | /* Set when the idlers are all forked */ |
| 64 | int smp_threads_ready; |
| 65 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | /* |
| 67 | * Trampoline 80x86 program as an array. |
| 68 | */ |
| 69 | |
Jan Beulich | 121d7bf | 2007-10-17 18:04:37 +0200 | [diff] [blame] | 70 | extern const unsigned char trampoline_data[]; |
| 71 | extern const unsigned char trampoline_end[]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | |
Ashok Raj | 76e4f66 | 2005-06-25 14:55:00 -0700 | [diff] [blame] | 73 | /* State of each CPU */ |
| 74 | DEFINE_PER_CPU(int, cpu_state) = { 0 }; |
| 75 | |
| 76 | /* |
| 77 | * Store all idle threads, this can be reused instead of creating |
| 78 | * a new thread. Also avoids complicated thread destroy functionality |
| 79 | * for idle threads. |
| 80 | */ |
travis@sgi.com | 24b0d22 | 2008-01-30 13:33:11 +0100 | [diff] [blame] | 81 | #ifdef CONFIG_HOTPLUG_CPU |
| 82 | /* |
| 83 | * Needed only for CONFIG_HOTPLUG_CPU because __cpuinitdata is |
| 84 | * removed after init for !CONFIG_HOTPLUG_CPU. |
| 85 | */ |
| 86 | static DEFINE_PER_CPU(struct task_struct *, idle_thread_array); |
| 87 | #define get_idle_for_cpu(x) (per_cpu(idle_thread_array, x)) |
| 88 | #define set_idle_for_cpu(x,p) (per_cpu(idle_thread_array, x) = (p)) |
| 89 | #else |
Ashok Raj | 76e4f66 | 2005-06-25 14:55:00 -0700 | [diff] [blame] | 90 | struct task_struct *idle_thread_array[NR_CPUS] __cpuinitdata ; |
Ashok Raj | 76e4f66 | 2005-06-25 14:55:00 -0700 | [diff] [blame] | 91 | #define get_idle_for_cpu(x) (idle_thread_array[(x)]) |
| 92 | #define set_idle_for_cpu(x,p) (idle_thread_array[(x)] = (p)) |
travis@sgi.com | 24b0d22 | 2008-01-30 13:33:11 +0100 | [diff] [blame] | 93 | #endif |
| 94 | |
Ashok Raj | 76e4f66 | 2005-06-25 14:55:00 -0700 | [diff] [blame] | 95 | |
| 96 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | * Currently trivial. Write the real->protected mode |
| 98 | * bootstrap into the page concerned. The caller |
| 99 | * has made sure it's suitably aligned. |
| 100 | */ |
| 101 | |
Andi Kleen | a8ab26f | 2005-04-16 15:25:19 -0700 | [diff] [blame] | 102 | static unsigned long __cpuinit setup_trampoline(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | { |
| 104 | void *tramp = __va(SMP_TRAMPOLINE_BASE); |
| 105 | memcpy(tramp, trampoline_data, trampoline_end - trampoline_data); |
| 106 | return virt_to_phys(tramp); |
| 107 | } |
| 108 | |
| 109 | /* |
| 110 | * The bootstrap kernel entry code has set these up. Save them for |
| 111 | * a given CPU |
| 112 | */ |
| 113 | |
Andi Kleen | a8ab26f | 2005-04-16 15:25:19 -0700 | [diff] [blame] | 114 | static void __cpuinit smp_store_cpu_info(int id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | { |
Mike Travis | 92cb761 | 2007-10-19 20:35:04 +0200 | [diff] [blame] | 116 | struct cpuinfo_x86 *c = &cpu_data(id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | |
| 118 | *c = boot_cpu_data; |
Mike Travis | fbdcf18 | 2007-12-19 23:20:19 +0100 | [diff] [blame] | 119 | c->cpu_index = id; |
Linus Torvalds | 0068441 | 2007-12-25 20:16:16 -0800 | [diff] [blame] | 120 | identify_cpu(c); |
Andi Kleen | dda50e7 | 2005-05-16 21:53:25 -0700 | [diff] [blame] | 121 | print_cpu_info(c); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Andi Kleen | a8ab26f | 2005-04-16 15:25:19 -0700 | [diff] [blame] | 124 | static atomic_t init_deasserted __cpuinitdata; |
| 125 | |
| 126 | /* |
| 127 | * Report back to the Boot Processor. |
| 128 | * Running on AP. |
| 129 | */ |
| 130 | void __cpuinit smp_callin(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | { |
| 132 | int cpuid, phys_id; |
| 133 | unsigned long timeout; |
| 134 | |
| 135 | /* |
| 136 | * If waken up by an INIT in an 82489DX configuration |
| 137 | * we may get here before an INIT-deassert IPI reaches |
| 138 | * our local APIC. We have to wait for the IPI or we'll |
| 139 | * lock up on an APIC access. |
| 140 | */ |
Andi Kleen | a8ab26f | 2005-04-16 15:25:19 -0700 | [diff] [blame] | 141 | while (!atomic_read(&init_deasserted)) |
| 142 | cpu_relax(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | |
| 144 | /* |
| 145 | * (This works even if the APIC is not enabled.) |
| 146 | */ |
| 147 | phys_id = GET_APIC_ID(apic_read(APIC_ID)); |
| 148 | cpuid = smp_processor_id(); |
| 149 | if (cpu_isset(cpuid, cpu_callin_map)) { |
| 150 | panic("smp_callin: phys CPU#%d, CPU#%d already present??\n", |
| 151 | phys_id, cpuid); |
| 152 | } |
| 153 | Dprintk("CPU#%d (phys ID: %d) waiting for CALLOUT\n", cpuid, phys_id); |
| 154 | |
| 155 | /* |
| 156 | * STARTUP IPIs are fragile beasts as they might sometimes |
| 157 | * trigger some glue motherboard logic. Complete APIC bus |
| 158 | * silence for 1 second, this overestimates the time the |
| 159 | * boot CPU is spending to send the up to 2 STARTUP IPIs |
| 160 | * by a factor of two. This should be enough. |
| 161 | */ |
| 162 | |
| 163 | /* |
| 164 | * Waiting 2s total for startup (udelay is not yet working) |
| 165 | */ |
| 166 | timeout = jiffies + 2*HZ; |
| 167 | while (time_before(jiffies, timeout)) { |
| 168 | /* |
| 169 | * Has the boot CPU finished it's STARTUP sequence? |
| 170 | */ |
| 171 | if (cpu_isset(cpuid, cpu_callout_map)) |
| 172 | break; |
Andi Kleen | a8ab26f | 2005-04-16 15:25:19 -0700 | [diff] [blame] | 173 | cpu_relax(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | if (!time_before(jiffies, timeout)) { |
| 177 | panic("smp_callin: CPU%d started up but did not get a callout!\n", |
| 178 | cpuid); |
| 179 | } |
| 180 | |
| 181 | /* |
| 182 | * the boot CPU has finished the init stage and is spinning |
| 183 | * on callin_map until we finish. We are free to set up this |
| 184 | * CPU, first the APIC. (this is probably redundant on most |
| 185 | * boards) |
| 186 | */ |
| 187 | |
| 188 | Dprintk("CALLIN, before setup_local_APIC().\n"); |
| 189 | setup_local_APIC(); |
Andi Kleen | 739f33b | 2008-01-30 13:30:40 +0100 | [diff] [blame] | 190 | end_local_APIC_setup(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | /* |
| 193 | * Get our bogomips. |
Andi Kleen | b445221 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 194 | * |
| 195 | * Need to enable IRQs because it can take longer and then |
| 196 | * the NMI watchdog might kill us. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | */ |
Andi Kleen | b445221 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 198 | local_irq_enable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | calibrate_delay(); |
Andi Kleen | b445221 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 200 | local_irq_disable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | Dprintk("Stack at about %p\n",&cpuid); |
| 202 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | /* |
| 204 | * Save our processor parameters |
| 205 | */ |
| 206 | smp_store_cpu_info(cpuid); |
| 207 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | /* |
| 209 | * Allow the master to continue. |
| 210 | */ |
| 211 | cpu_set(cpuid, cpu_callin_map); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | } |
| 213 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | /* |
Andi Kleen | a8ab26f | 2005-04-16 15:25:19 -0700 | [diff] [blame] | 215 | * Setup code on secondary processor (after comming out of the trampoline) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | */ |
Andi Kleen | a8ab26f | 2005-04-16 15:25:19 -0700 | [diff] [blame] | 217 | void __cpuinit start_secondary(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | { |
| 219 | /* |
| 220 | * Dont put anything before smp_callin(), SMP |
| 221 | * booting is too fragile that we want to limit the |
| 222 | * things done here to the most necessary things. |
| 223 | */ |
| 224 | cpu_init(); |
Nick Piggin | 5bfb5d6 | 2005-11-08 21:39:01 -0800 | [diff] [blame] | 225 | preempt_disable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | smp_callin(); |
| 227 | |
| 228 | /* otherwise gcc will move up the smp_processor_id before the cpu_init */ |
| 229 | barrier(); |
| 230 | |
Ingo Molnar | 95492e4 | 2007-02-16 01:27:34 -0800 | [diff] [blame] | 231 | /* |
| 232 | * Check TSC sync first: |
| 233 | */ |
| 234 | check_tsc_sync_target(); |
| 235 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | if (nmi_watchdog == NMI_IO_APIC) { |
| 237 | disable_8259A_irq(0); |
Jan Beulich | e942710 | 2008-01-30 13:31:24 +0100 | [diff] [blame] | 238 | enable_NMI_through_LVT0(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | enable_8259A_irq(0); |
| 240 | } |
| 241 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | /* |
Ashok Raj | cb0cd8d | 2005-06-25 14:55:01 -0700 | [diff] [blame] | 243 | * The sibling maps must be set before turing the online map on for |
| 244 | * this cpu |
| 245 | */ |
| 246 | set_cpu_sibling_map(smp_processor_id()); |
| 247 | |
| 248 | /* |
Ashok Raj | 884d9e4 | 2005-06-25 14:55:02 -0700 | [diff] [blame] | 249 | * We need to hold call_lock, so there is no inconsistency |
| 250 | * between the time smp_call_function() determines number of |
Simon Arlott | 676b185 | 2007-10-20 01:25:36 +0200 | [diff] [blame] | 251 | * IPI recipients, and the time when the determination is made |
Ashok Raj | 884d9e4 | 2005-06-25 14:55:02 -0700 | [diff] [blame] | 252 | * for which cpus receive the IPI in genapic_flat.c. Holding this |
| 253 | * lock helps us to not include this cpu in a currently in progress |
| 254 | * smp_call_function(). |
| 255 | */ |
| 256 | lock_ipi_call_lock(); |
Eric W. Biederman | 70a0a53 | 2006-10-25 01:00:23 +0200 | [diff] [blame] | 257 | spin_lock(&vector_lock); |
Ashok Raj | 884d9e4 | 2005-06-25 14:55:02 -0700 | [diff] [blame] | 258 | |
Eric W. Biederman | 70a0a53 | 2006-10-25 01:00:23 +0200 | [diff] [blame] | 259 | /* Setup the per cpu irq handling data structures */ |
| 260 | __setup_vector_irq(smp_processor_id()); |
Ashok Raj | 884d9e4 | 2005-06-25 14:55:02 -0700 | [diff] [blame] | 261 | /* |
Andi Kleen | a8ab26f | 2005-04-16 15:25:19 -0700 | [diff] [blame] | 262 | * Allow the master to continue. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | */ |
Glauber Costa | fc25da9 | 2008-03-03 14:13:04 -0300 | [diff] [blame^] | 264 | spin_unlock(&vector_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | cpu_set(smp_processor_id(), cpu_online_map); |
Ashok Raj | 884d9e4 | 2005-06-25 14:55:02 -0700 | [diff] [blame] | 266 | per_cpu(cpu_state, smp_processor_id()) = CPU_ONLINE; |
Ingo Molnar | 95492e4 | 2007-02-16 01:27:34 -0800 | [diff] [blame] | 267 | |
Ashok Raj | 884d9e4 | 2005-06-25 14:55:02 -0700 | [diff] [blame] | 268 | unlock_ipi_call_lock(); |
| 269 | |
Glauber de Oliveira Costa | 746ef0c | 2008-01-30 13:31:11 +0100 | [diff] [blame] | 270 | setup_secondary_clock(); |
Thomas Gleixner | 3ac508b | 2007-10-14 22:57:45 +0200 | [diff] [blame] | 271 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | cpu_idle(); |
| 273 | } |
| 274 | |
Andi Kleen | a8ab26f | 2005-04-16 15:25:19 -0700 | [diff] [blame] | 275 | extern volatile unsigned long init_rsp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | extern void (*initial_code)(void); |
| 277 | |
Olaf Hering | 44456d3 | 2005-07-27 11:45:17 -0700 | [diff] [blame] | 278 | #ifdef APIC_DEBUG |
Andi Kleen | a8ab26f | 2005-04-16 15:25:19 -0700 | [diff] [blame] | 279 | static void inquire_remote_apic(int apicid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | { |
| 281 | unsigned i, regs[] = { APIC_ID >> 4, APIC_LVR >> 4, APIC_SPIV >> 4 }; |
| 282 | char *names[] = { "ID", "VERSION", "SPIV" }; |
Fernando Luis VazquezCao | 3144c33 | 2007-05-02 19:27:17 +0200 | [diff] [blame] | 283 | int timeout; |
Thomas Gleixner | 3c6bb07 | 2008-01-30 13:30:15 +0100 | [diff] [blame] | 284 | u32 status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | |
| 286 | printk(KERN_INFO "Inquiring remote APIC #%d...\n", apicid); |
| 287 | |
Alejandro Martinez Ruiz | 4d022ad | 2007-10-17 14:38:58 +0200 | [diff] [blame] | 288 | for (i = 0; i < ARRAY_SIZE(regs); i++) { |
Thomas Gleixner | 3c6bb07 | 2008-01-30 13:30:15 +0100 | [diff] [blame] | 289 | printk(KERN_INFO "... APIC #%d %s: ", apicid, names[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | |
| 291 | /* |
| 292 | * Wait for idle. |
| 293 | */ |
Fernando Luis VazquezCao | 3144c33 | 2007-05-02 19:27:17 +0200 | [diff] [blame] | 294 | status = safe_apic_wait_icr_idle(); |
| 295 | if (status) |
Thomas Gleixner | 3c6bb07 | 2008-01-30 13:30:15 +0100 | [diff] [blame] | 296 | printk(KERN_CONT |
| 297 | "a previous APIC delivery may have failed\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | |
Andi Kleen | c1507eb | 2005-09-12 18:49:23 +0200 | [diff] [blame] | 299 | apic_write(APIC_ICR2, SET_APIC_DEST_FIELD(apicid)); |
| 300 | apic_write(APIC_ICR, APIC_DM_REMRD | regs[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | |
| 302 | timeout = 0; |
| 303 | do { |
| 304 | udelay(100); |
| 305 | status = apic_read(APIC_ICR) & APIC_ICR_RR_MASK; |
| 306 | } while (status == APIC_ICR_RR_INPROG && timeout++ < 1000); |
| 307 | |
| 308 | switch (status) { |
| 309 | case APIC_ICR_RR_VALID: |
| 310 | status = apic_read(APIC_RRR); |
Thomas Gleixner | 3c6bb07 | 2008-01-30 13:30:15 +0100 | [diff] [blame] | 311 | printk(KERN_CONT "%08x\n", status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | break; |
| 313 | default: |
Thomas Gleixner | 3c6bb07 | 2008-01-30 13:30:15 +0100 | [diff] [blame] | 314 | printk(KERN_CONT "failed\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | } |
| 316 | } |
| 317 | } |
| 318 | #endif |
| 319 | |
Andi Kleen | a8ab26f | 2005-04-16 15:25:19 -0700 | [diff] [blame] | 320 | /* |
| 321 | * Kick the secondary to wake up. |
| 322 | */ |
| 323 | static int __cpuinit wakeup_secondary_via_INIT(int phys_apicid, unsigned int start_rip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | { |
Fernando Luis VazquezCao | ea8c733 | 2007-05-02 19:27:17 +0200 | [diff] [blame] | 325 | unsigned long send_status, accept_status = 0; |
| 326 | int maxlvt, num_starts, j; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | |
| 328 | Dprintk("Asserting INIT.\n"); |
| 329 | |
| 330 | /* |
| 331 | * Turn INIT on target chip |
| 332 | */ |
Andi Kleen | c1507eb | 2005-09-12 18:49:23 +0200 | [diff] [blame] | 333 | apic_write(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | |
| 335 | /* |
| 336 | * Send IPI |
| 337 | */ |
Andi Kleen | c1507eb | 2005-09-12 18:49:23 +0200 | [diff] [blame] | 338 | apic_write(APIC_ICR, APIC_INT_LEVELTRIG | APIC_INT_ASSERT |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | | APIC_DM_INIT); |
| 340 | |
| 341 | Dprintk("Waiting for send to finish...\n"); |
Fernando Luis VazquezCao | ea8c733 | 2007-05-02 19:27:17 +0200 | [diff] [blame] | 342 | send_status = safe_apic_wait_icr_idle(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | |
| 344 | mdelay(10); |
| 345 | |
| 346 | Dprintk("Deasserting INIT.\n"); |
| 347 | |
| 348 | /* Target chip */ |
Andi Kleen | c1507eb | 2005-09-12 18:49:23 +0200 | [diff] [blame] | 349 | apic_write(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | |
| 351 | /* Send IPI */ |
Andi Kleen | c1507eb | 2005-09-12 18:49:23 +0200 | [diff] [blame] | 352 | apic_write(APIC_ICR, APIC_INT_LEVELTRIG | APIC_DM_INIT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | |
| 354 | Dprintk("Waiting for send to finish...\n"); |
Fernando Luis VazquezCao | ea8c733 | 2007-05-02 19:27:17 +0200 | [diff] [blame] | 355 | send_status = safe_apic_wait_icr_idle(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | |
Benjamin LaHaise | f2ecfab | 2006-01-11 22:43:03 +0100 | [diff] [blame] | 357 | mb(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | atomic_set(&init_deasserted, 1); |
| 359 | |
Andi Kleen | 5a40b7c | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 360 | num_starts = 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | |
| 362 | /* |
| 363 | * Run STARTUP IPI loop. |
| 364 | */ |
| 365 | Dprintk("#startup loops: %d.\n", num_starts); |
| 366 | |
Thomas Gleixner | 37e650c | 2008-01-30 13:30:14 +0100 | [diff] [blame] | 367 | maxlvt = lapic_get_maxlvt(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | |
| 369 | for (j = 1; j <= num_starts; j++) { |
| 370 | Dprintk("Sending STARTUP #%d.\n",j); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | apic_write(APIC_ESR, 0); |
| 372 | apic_read(APIC_ESR); |
| 373 | Dprintk("After apic_write.\n"); |
| 374 | |
| 375 | /* |
| 376 | * STARTUP IPI |
| 377 | */ |
| 378 | |
| 379 | /* Target chip */ |
Andi Kleen | c1507eb | 2005-09-12 18:49:23 +0200 | [diff] [blame] | 380 | apic_write(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | |
| 382 | /* Boot on the stack */ |
| 383 | /* Kick the second */ |
Andi Kleen | c1507eb | 2005-09-12 18:49:23 +0200 | [diff] [blame] | 384 | apic_write(APIC_ICR, APIC_DM_STARTUP | (start_rip >> 12)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | |
| 386 | /* |
| 387 | * Give the other CPU some time to accept the IPI. |
| 388 | */ |
| 389 | udelay(300); |
| 390 | |
| 391 | Dprintk("Startup point 1.\n"); |
| 392 | |
| 393 | Dprintk("Waiting for send to finish...\n"); |
Fernando Luis VazquezCao | ea8c733 | 2007-05-02 19:27:17 +0200 | [diff] [blame] | 394 | send_status = safe_apic_wait_icr_idle(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | |
| 396 | /* |
| 397 | * Give the other CPU some time to accept the IPI. |
| 398 | */ |
| 399 | udelay(200); |
| 400 | /* |
| 401 | * Due to the Pentium erratum 3AP. |
| 402 | */ |
| 403 | if (maxlvt > 3) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | apic_write(APIC_ESR, 0); |
| 405 | } |
| 406 | accept_status = (apic_read(APIC_ESR) & 0xEF); |
| 407 | if (send_status || accept_status) |
| 408 | break; |
| 409 | } |
| 410 | Dprintk("After Startup.\n"); |
| 411 | |
| 412 | if (send_status) |
| 413 | printk(KERN_ERR "APIC never delivered???\n"); |
| 414 | if (accept_status) |
| 415 | printk(KERN_ERR "APIC delivery error (%lx).\n", accept_status); |
| 416 | |
| 417 | return (send_status | accept_status); |
| 418 | } |
| 419 | |
Ashok Raj | 76e4f66 | 2005-06-25 14:55:00 -0700 | [diff] [blame] | 420 | struct create_idle { |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 421 | struct work_struct work; |
Ashok Raj | 76e4f66 | 2005-06-25 14:55:00 -0700 | [diff] [blame] | 422 | struct task_struct *idle; |
| 423 | struct completion done; |
| 424 | int cpu; |
| 425 | }; |
| 426 | |
Thomas Gleixner | a2b484a | 2008-01-09 00:18:28 +0100 | [diff] [blame] | 427 | static void __cpuinit do_fork_idle(struct work_struct *work) |
Ashok Raj | 76e4f66 | 2005-06-25 14:55:00 -0700 | [diff] [blame] | 428 | { |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 429 | struct create_idle *c_idle = |
| 430 | container_of(work, struct create_idle, work); |
Ashok Raj | 76e4f66 | 2005-06-25 14:55:00 -0700 | [diff] [blame] | 431 | |
| 432 | c_idle->idle = fork_idle(c_idle->cpu); |
| 433 | complete(&c_idle->done); |
| 434 | } |
| 435 | |
Andi Kleen | a8ab26f | 2005-04-16 15:25:19 -0700 | [diff] [blame] | 436 | /* |
| 437 | * Boot one CPU. |
| 438 | */ |
| 439 | static int __cpuinit do_boot_cpu(int cpu, int apicid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | unsigned long boot_error; |
Andi Kleen | a8ab26f | 2005-04-16 15:25:19 -0700 | [diff] [blame] | 442 | int timeout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | unsigned long start_rip; |
Ashok Raj | 76e4f66 | 2005-06-25 14:55:00 -0700 | [diff] [blame] | 444 | struct create_idle c_idle = { |
| 445 | .cpu = cpu, |
Ingo Molnar | f86bf9b | 2006-07-10 04:44:05 -0700 | [diff] [blame] | 446 | .done = COMPLETION_INITIALIZER_ONSTACK(c_idle.done), |
Ashok Raj | 76e4f66 | 2005-06-25 14:55:00 -0700 | [diff] [blame] | 447 | }; |
Glauber Costa | 2b775a2 | 2008-02-22 12:09:29 -0300 | [diff] [blame] | 448 | INIT_WORK(&c_idle.work, do_fork_idle); |
Ashok Raj | 76e4f66 | 2005-06-25 14:55:00 -0700 | [diff] [blame] | 449 | |
Ravikiran G Thirumalai | c11efdf | 2006-01-11 22:43:57 +0100 | [diff] [blame] | 450 | /* allocate memory for gdts of secondary cpus. Hotplug is considered */ |
| 451 | if (!cpu_gdt_descr[cpu].address && |
| 452 | !(cpu_gdt_descr[cpu].address = get_zeroed_page(GFP_KERNEL))) { |
| 453 | printk(KERN_ERR "Failed to allocate GDT for CPU %d\n", cpu); |
| 454 | return -1; |
| 455 | } |
| 456 | |
Ravikiran G Thirumalai | 365ba91 | 2006-01-11 22:45:42 +0100 | [diff] [blame] | 457 | /* Allocate node local memory for AP pdas */ |
| 458 | if (cpu_pda(cpu) == &boot_cpu_pda[cpu]) { |
| 459 | struct x8664_pda *newpda, *pda; |
| 460 | int node = cpu_to_node(cpu); |
| 461 | pda = cpu_pda(cpu); |
| 462 | newpda = kmalloc_node(sizeof (struct x8664_pda), GFP_ATOMIC, |
| 463 | node); |
| 464 | if (newpda) { |
| 465 | memcpy(newpda, pda, sizeof (struct x8664_pda)); |
| 466 | cpu_pda(cpu) = newpda; |
| 467 | } else |
| 468 | printk(KERN_ERR |
| 469 | "Could not allocate node local PDA for CPU %d on node %d\n", |
| 470 | cpu, node); |
| 471 | } |
| 472 | |
Gerd Hoffmann | d167a51 | 2006-06-26 13:56:16 +0200 | [diff] [blame] | 473 | alternatives_smp_switch(1); |
| 474 | |
Ashok Raj | 76e4f66 | 2005-06-25 14:55:00 -0700 | [diff] [blame] | 475 | c_idle.idle = get_idle_for_cpu(cpu); |
| 476 | |
| 477 | if (c_idle.idle) { |
H. Peter Anvin | faca622 | 2008-01-30 13:31:02 +0100 | [diff] [blame] | 478 | c_idle.idle->thread.sp = (unsigned long) (((struct pt_regs *) |
Al Viro | 57eafdc | 2006-01-12 01:05:39 -0800 | [diff] [blame] | 479 | (THREAD_SIZE + task_stack_page(c_idle.idle))) - 1); |
Ashok Raj | 76e4f66 | 2005-06-25 14:55:00 -0700 | [diff] [blame] | 480 | init_idle(c_idle.idle, cpu); |
| 481 | goto do_rest; |
Andi Kleen | a8ab26f | 2005-04-16 15:25:19 -0700 | [diff] [blame] | 482 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | |
Ashok Raj | 76e4f66 | 2005-06-25 14:55:00 -0700 | [diff] [blame] | 484 | /* |
| 485 | * During cold boot process, keventd thread is not spun up yet. |
| 486 | * When we do cpu hot-add, we create idle threads on the fly, we should |
| 487 | * not acquire any attributes from the calling context. Hence the clean |
| 488 | * way to create kernel_threads() is to do that from keventd(). |
| 489 | * We do the current_is_keventd() due to the fact that ACPI notifier |
| 490 | * was also queuing to keventd() and when the caller is already running |
| 491 | * in context of keventd(), we would end up with locking up the keventd |
| 492 | * thread. |
| 493 | */ |
| 494 | if (!keventd_up() || current_is_keventd()) |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 495 | c_idle.work.func(&c_idle.work); |
Ashok Raj | 76e4f66 | 2005-06-25 14:55:00 -0700 | [diff] [blame] | 496 | else { |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 497 | schedule_work(&c_idle.work); |
Ashok Raj | 76e4f66 | 2005-06-25 14:55:00 -0700 | [diff] [blame] | 498 | wait_for_completion(&c_idle.done); |
| 499 | } |
| 500 | |
| 501 | if (IS_ERR(c_idle.idle)) { |
| 502 | printk("failed fork for CPU %d\n", cpu); |
| 503 | return PTR_ERR(c_idle.idle); |
| 504 | } |
| 505 | |
| 506 | set_idle_for_cpu(cpu, c_idle.idle); |
| 507 | |
| 508 | do_rest: |
| 509 | |
Ravikiran G Thirumalai | df79efd | 2006-01-11 22:45:39 +0100 | [diff] [blame] | 510 | cpu_pda(cpu)->pcurrent = c_idle.idle; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | |
| 512 | start_rip = setup_trampoline(); |
| 513 | |
H. Peter Anvin | faca622 | 2008-01-30 13:31:02 +0100 | [diff] [blame] | 514 | init_rsp = c_idle.idle->thread.sp; |
Glauber de Oliveira Costa | 7818a1e | 2008-01-30 13:31:31 +0100 | [diff] [blame] | 515 | load_sp0(&per_cpu(init_tss, cpu), &c_idle.idle->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | initial_code = start_secondary; |
Al Viro | e4f17c4 | 2006-01-12 01:05:38 -0800 | [diff] [blame] | 517 | clear_tsk_thread_flag(c_idle.idle, TIF_FORK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | |
Andi Kleen | de04f32 | 2005-07-28 21:15:29 -0700 | [diff] [blame] | 519 | printk(KERN_INFO "Booting processor %d/%d APIC 0x%x\n", cpu, |
| 520 | cpus_weight(cpu_present_map), |
| 521 | apicid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | |
| 523 | /* |
| 524 | * This grunge runs the startup process for |
| 525 | * the targeted processor. |
| 526 | */ |
| 527 | |
| 528 | atomic_set(&init_deasserted, 0); |
| 529 | |
| 530 | Dprintk("Setting warm reset code and vector.\n"); |
| 531 | |
| 532 | CMOS_WRITE(0xa, 0xf); |
| 533 | local_flush_tlb(); |
| 534 | Dprintk("1.\n"); |
| 535 | *((volatile unsigned short *) phys_to_virt(0x469)) = start_rip >> 4; |
| 536 | Dprintk("2.\n"); |
| 537 | *((volatile unsigned short *) phys_to_virt(0x467)) = start_rip & 0xf; |
| 538 | Dprintk("3.\n"); |
| 539 | |
| 540 | /* |
| 541 | * Be paranoid about clearing APIC errors. |
| 542 | */ |
Andi Kleen | 11a8e77 | 2006-01-11 22:46:51 +0100 | [diff] [blame] | 543 | apic_write(APIC_ESR, 0); |
| 544 | apic_read(APIC_ESR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | |
| 546 | /* |
| 547 | * Status is now clean |
| 548 | */ |
| 549 | boot_error = 0; |
| 550 | |
| 551 | /* |
| 552 | * Starting actual IPI sequence... |
| 553 | */ |
Andi Kleen | a8ab26f | 2005-04-16 15:25:19 -0700 | [diff] [blame] | 554 | boot_error = wakeup_secondary_via_INIT(apicid, start_rip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | |
| 556 | if (!boot_error) { |
| 557 | /* |
| 558 | * allow APs to start initializing. |
| 559 | */ |
| 560 | Dprintk("Before Callout %d.\n", cpu); |
| 561 | cpu_set(cpu, cpu_callout_map); |
| 562 | Dprintk("After Callout %d.\n", cpu); |
| 563 | |
| 564 | /* |
| 565 | * Wait 5s total for a response |
| 566 | */ |
| 567 | for (timeout = 0; timeout < 50000; timeout++) { |
| 568 | if (cpu_isset(cpu, cpu_callin_map)) |
| 569 | break; /* It has booted */ |
| 570 | udelay(100); |
| 571 | } |
| 572 | |
| 573 | if (cpu_isset(cpu, cpu_callin_map)) { |
| 574 | /* number CPUs logically, starting from 1 (BSP is 0) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | Dprintk("CPU has booted.\n"); |
| 576 | } else { |
| 577 | boot_error = 1; |
| 578 | if (*((volatile unsigned char *)phys_to_virt(SMP_TRAMPOLINE_BASE)) |
| 579 | == 0xA5) |
| 580 | /* trampoline started but...? */ |
| 581 | printk("Stuck ??\n"); |
| 582 | else |
| 583 | /* trampoline code not run */ |
| 584 | printk("Not responding.\n"); |
Olaf Hering | 44456d3 | 2005-07-27 11:45:17 -0700 | [diff] [blame] | 585 | #ifdef APIC_DEBUG |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | inquire_remote_apic(apicid); |
| 587 | #endif |
| 588 | } |
| 589 | } |
| 590 | if (boot_error) { |
| 591 | cpu_clear(cpu, cpu_callout_map); /* was set here (do_boot_cpu()) */ |
Jeremy Fitzhardinge | 5548fec | 2008-01-30 13:30:55 +0100 | [diff] [blame] | 592 | clear_bit(cpu, (unsigned long *)&cpu_initialized); /* was set by cpu_init() */ |
Ravikiran G Thirumalai | 488fc08 | 2006-02-07 12:58:23 -0800 | [diff] [blame] | 593 | clear_node_cpumask(cpu); /* was set by numa_add_cpu */ |
Andi Kleen | a8ab26f | 2005-04-16 15:25:19 -0700 | [diff] [blame] | 594 | cpu_clear(cpu, cpu_present_map); |
| 595 | cpu_clear(cpu, cpu_possible_map); |
Mike Travis | 71fff5e | 2007-10-19 20:35:03 +0200 | [diff] [blame] | 596 | per_cpu(x86_cpu_to_apicid, cpu) = BAD_APICID; |
Andi Kleen | a8ab26f | 2005-04-16 15:25:19 -0700 | [diff] [blame] | 597 | return -EIO; |
| 598 | } |
| 599 | |
| 600 | return 0; |
| 601 | } |
| 602 | |
| 603 | cycles_t cacheflush_time; |
| 604 | unsigned long cache_decay_ticks; |
| 605 | |
| 606 | /* |
Andi Kleen | a8ab26f | 2005-04-16 15:25:19 -0700 | [diff] [blame] | 607 | * Cleanup possible dangling ends... |
| 608 | */ |
| 609 | static __cpuinit void smp_cleanup_boot(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | /* |
Andi Kleen | a8ab26f | 2005-04-16 15:25:19 -0700 | [diff] [blame] | 612 | * Paranoid: Set warm reset code and vector here back |
| 613 | * to default values. |
| 614 | */ |
| 615 | CMOS_WRITE(0, 0xf); |
| 616 | |
| 617 | /* |
| 618 | * Reset trampoline flag |
| 619 | */ |
| 620 | *((volatile int *) phys_to_virt(0x467)) = 0; |
Andi Kleen | a8ab26f | 2005-04-16 15:25:19 -0700 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | /* |
| 624 | * Fall back to non SMP mode after errors. |
| 625 | * |
| 626 | * RED-PEN audit/test this more. I bet there is more state messed up here. |
| 627 | */ |
Ashok Raj | e6982c6 | 2005-06-25 14:54:58 -0700 | [diff] [blame] | 628 | static __init void disable_smp(void) |
Andi Kleen | a8ab26f | 2005-04-16 15:25:19 -0700 | [diff] [blame] | 629 | { |
| 630 | cpu_present_map = cpumask_of_cpu(0); |
| 631 | cpu_possible_map = cpumask_of_cpu(0); |
| 632 | if (smp_found_config) |
| 633 | phys_cpu_present_map = physid_mask_of_physid(boot_cpu_id); |
| 634 | else |
| 635 | phys_cpu_present_map = physid_mask_of_physid(0); |
Mike Travis | d5a7430 | 2007-10-16 01:24:05 -0700 | [diff] [blame] | 636 | cpu_set(0, per_cpu(cpu_sibling_map, 0)); |
Mike Travis | 0835761 | 2007-10-16 01:24:04 -0700 | [diff] [blame] | 637 | cpu_set(0, per_cpu(cpu_core_map, 0)); |
Andi Kleen | a8ab26f | 2005-04-16 15:25:19 -0700 | [diff] [blame] | 638 | } |
| 639 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | /* |
Andi Kleen | a8ab26f | 2005-04-16 15:25:19 -0700 | [diff] [blame] | 641 | * Various sanity checks. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | */ |
Ashok Raj | e6982c6 | 2005-06-25 14:54:58 -0700 | [diff] [blame] | 643 | static int __init smp_sanity_check(unsigned max_cpus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | if (!physid_isset(hard_smp_processor_id(), phys_cpu_present_map)) { |
| 646 | printk("weird, boot CPU (#%d) not listed by the BIOS.\n", |
| 647 | hard_smp_processor_id()); |
| 648 | physid_set(hard_smp_processor_id(), phys_cpu_present_map); |
| 649 | } |
| 650 | |
| 651 | /* |
| 652 | * If we couldn't find an SMP configuration at boot time, |
| 653 | * get out of here now! |
| 654 | */ |
| 655 | if (!smp_found_config) { |
| 656 | printk(KERN_NOTICE "SMP motherboard not detected.\n"); |
Andi Kleen | a8ab26f | 2005-04-16 15:25:19 -0700 | [diff] [blame] | 657 | disable_smp(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 658 | if (APIC_init_uniprocessor()) |
| 659 | printk(KERN_NOTICE "Local APIC not detected." |
| 660 | " Using dummy APIC emulation.\n"); |
Andi Kleen | a8ab26f | 2005-04-16 15:25:19 -0700 | [diff] [blame] | 661 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | /* |
| 665 | * Should not be necessary because the MP table should list the boot |
| 666 | * CPU too, but we do it for the sake of robustness anyway. |
| 667 | */ |
| 668 | if (!physid_isset(boot_cpu_id, phys_cpu_present_map)) { |
| 669 | printk(KERN_NOTICE "weird, boot CPU (#%d) not listed by the BIOS.\n", |
| 670 | boot_cpu_id); |
| 671 | physid_set(hard_smp_processor_id(), phys_cpu_present_map); |
| 672 | } |
| 673 | |
| 674 | /* |
| 675 | * If we couldn't find a local APIC, then get out of here now! |
| 676 | */ |
Andi Kleen | 11a8e77 | 2006-01-11 22:46:51 +0100 | [diff] [blame] | 677 | if (!cpu_has_apic) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 678 | printk(KERN_ERR "BIOS bug, local APIC #%d not detected!...\n", |
| 679 | boot_cpu_id); |
| 680 | printk(KERN_ERR "... forcing use of dummy APIC emulation. (tell your hw vendor)\n"); |
Andi Kleen | a8ab26f | 2005-04-16 15:25:19 -0700 | [diff] [blame] | 681 | nr_ioapics = 0; |
| 682 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 683 | } |
| 684 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | /* |
| 686 | * If SMP should be disabled, then really disable it! |
| 687 | */ |
| 688 | if (!max_cpus) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | printk(KERN_INFO "SMP mode deactivated, forcing use of dummy APIC emulation.\n"); |
Andi Kleen | a8ab26f | 2005-04-16 15:25:19 -0700 | [diff] [blame] | 690 | nr_ioapics = 0; |
| 691 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 692 | } |
| 693 | |
Andi Kleen | a8ab26f | 2005-04-16 15:25:19 -0700 | [diff] [blame] | 694 | return 0; |
| 695 | } |
| 696 | |
Yinghai Lu | 949ec32 | 2008-01-30 13:30:46 +0100 | [diff] [blame] | 697 | static void __init smp_cpu_index_default(void) |
| 698 | { |
| 699 | int i; |
| 700 | struct cpuinfo_x86 *c; |
| 701 | |
| 702 | for_each_cpu_mask(i, cpu_possible_map) { |
| 703 | c = &cpu_data(i); |
| 704 | /* mark all to hotplug */ |
| 705 | c->cpu_index = NR_CPUS; |
| 706 | } |
| 707 | } |
| 708 | |
Mike Travis | 71fff5e | 2007-10-19 20:35:03 +0200 | [diff] [blame] | 709 | /* |
Andi Kleen | a8ab26f | 2005-04-16 15:25:19 -0700 | [diff] [blame] | 710 | * Prepare for SMP bootup. The MP table or ACPI has been read |
| 711 | * earlier. Just do some sanity checking here and enable APIC mode. |
| 712 | */ |
Glauber Costa | 7557da6 | 2008-03-03 14:12:38 -0300 | [diff] [blame] | 713 | void __init native_smp_prepare_cpus(unsigned int max_cpus) |
Andi Kleen | a8ab26f | 2005-04-16 15:25:19 -0700 | [diff] [blame] | 714 | { |
Andi Kleen | a8ab26f | 2005-04-16 15:25:19 -0700 | [diff] [blame] | 715 | nmi_watchdog_default(); |
Yinghai Lu | 949ec32 | 2008-01-30 13:30:46 +0100 | [diff] [blame] | 716 | smp_cpu_index_default(); |
Andi Kleen | a8ab26f | 2005-04-16 15:25:19 -0700 | [diff] [blame] | 717 | current_cpu_data = boot_cpu_data; |
| 718 | current_thread_info()->cpu = 0; /* needed? */ |
Siddha, Suresh B | 94605ef | 2005-11-05 17:25:54 +0100 | [diff] [blame] | 719 | set_cpu_sibling_map(0); |
Andi Kleen | a8ab26f | 2005-04-16 15:25:19 -0700 | [diff] [blame] | 720 | |
Andi Kleen | a8ab26f | 2005-04-16 15:25:19 -0700 | [diff] [blame] | 721 | if (smp_sanity_check(max_cpus) < 0) { |
| 722 | printk(KERN_INFO "SMP disabled\n"); |
| 723 | disable_smp(); |
| 724 | return; |
| 725 | } |
| 726 | |
| 727 | |
| 728 | /* |
| 729 | * Switch from PIC to APIC mode. |
| 730 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | setup_local_APIC(); |
| 732 | |
Andi Kleen | 739f33b | 2008-01-30 13:30:40 +0100 | [diff] [blame] | 733 | /* |
| 734 | * Enable IO APIC before setting up error vector |
| 735 | */ |
| 736 | if (!skip_ioapic_setup && nr_ioapics) |
| 737 | enable_IO_APIC(); |
| 738 | end_local_APIC_setup(); |
| 739 | |
Andi Kleen | a8ab26f | 2005-04-16 15:25:19 -0700 | [diff] [blame] | 740 | if (GET_APIC_ID(apic_read(APIC_ID)) != boot_cpu_id) { |
| 741 | panic("Boot APIC ID in local APIC unexpected (%d vs %d)", |
| 742 | GET_APIC_ID(apic_read(APIC_ID)), boot_cpu_id); |
| 743 | /* Or can we switch back to PIC here? */ |
| 744 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 745 | |
| 746 | /* |
Andi Kleen | a8ab26f | 2005-04-16 15:25:19 -0700 | [diff] [blame] | 747 | * Now start the IO-APICs |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 748 | */ |
| 749 | if (!skip_ioapic_setup && nr_ioapics) |
| 750 | setup_IO_APIC(); |
| 751 | else |
| 752 | nr_ioapics = 0; |
| 753 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 754 | /* |
Andi Kleen | a8ab26f | 2005-04-16 15:25:19 -0700 | [diff] [blame] | 755 | * Set up local APIC timer on boot CPU. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 757 | |
Glauber de Oliveira Costa | 746ef0c | 2008-01-30 13:31:11 +0100 | [diff] [blame] | 758 | setup_boot_clock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 759 | } |
| 760 | |
Andi Kleen | a8ab26f | 2005-04-16 15:25:19 -0700 | [diff] [blame] | 761 | /* |
| 762 | * Early setup to make printk work. |
| 763 | */ |
Glauber Costa | 1e3fac8 | 2008-03-03 14:12:37 -0300 | [diff] [blame] | 764 | void __init native_smp_prepare_boot_cpu(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 765 | { |
Andi Kleen | a8ab26f | 2005-04-16 15:25:19 -0700 | [diff] [blame] | 766 | int me = smp_processor_id(); |
Yinghai Lu | 23916d4 | 2008-01-30 13:33:39 +0100 | [diff] [blame] | 767 | /* already set me in cpu_online_map in boot_cpu_init() */ |
Andi Kleen | a8ab26f | 2005-04-16 15:25:19 -0700 | [diff] [blame] | 768 | cpu_set(me, cpu_callout_map); |
Ashok Raj | 884d9e4 | 2005-06-25 14:55:02 -0700 | [diff] [blame] | 769 | per_cpu(cpu_state, me) = CPU_ONLINE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 770 | } |
| 771 | |
Andi Kleen | a8ab26f | 2005-04-16 15:25:19 -0700 | [diff] [blame] | 772 | /* |
| 773 | * Entry point to boot a CPU. |
Andi Kleen | a8ab26f | 2005-04-16 15:25:19 -0700 | [diff] [blame] | 774 | */ |
Glauber Costa | 71d1954 | 2008-03-03 14:12:36 -0300 | [diff] [blame] | 775 | int __cpuinit native_cpu_up(unsigned int cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 776 | { |
Andi Kleen | a8ab26f | 2005-04-16 15:25:19 -0700 | [diff] [blame] | 777 | int apicid = cpu_present_to_apicid(cpu); |
Ingo Molnar | d04f41e | 2007-03-07 18:12:31 +0100 | [diff] [blame] | 778 | unsigned long flags; |
| 779 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 780 | |
Andi Kleen | a8ab26f | 2005-04-16 15:25:19 -0700 | [diff] [blame] | 781 | WARN_ON(irqs_disabled()); |
| 782 | |
| 783 | Dprintk("++++++++++++++++++++=_---CPU UP %u\n", cpu); |
| 784 | |
| 785 | if (apicid == BAD_APICID || apicid == boot_cpu_id || |
| 786 | !physid_isset(apicid, phys_cpu_present_map)) { |
| 787 | printk("__cpu_up: bad cpu %d\n", cpu); |
| 788 | return -EINVAL; |
| 789 | } |
Andi Kleen | a8ab26f | 2005-04-16 15:25:19 -0700 | [diff] [blame] | 790 | |
Ashok Raj | 76e4f66 | 2005-06-25 14:55:00 -0700 | [diff] [blame] | 791 | /* |
| 792 | * Already booted CPU? |
| 793 | */ |
| 794 | if (cpu_isset(cpu, cpu_callin_map)) { |
| 795 | Dprintk("do_boot_cpu %d Already started\n", cpu); |
| 796 | return -ENOSYS; |
| 797 | } |
| 798 | |
Bernhard Kaindl | 2b1f627 | 2007-05-02 19:27:17 +0200 | [diff] [blame] | 799 | /* |
| 800 | * Save current MTRR state in case it was changed since early boot |
| 801 | * (e.g. by the ACPI SMI) to initialize new CPUs with MTRRs in sync: |
| 802 | */ |
| 803 | mtrr_save_state(); |
| 804 | |
Ashok Raj | 884d9e4 | 2005-06-25 14:55:02 -0700 | [diff] [blame] | 805 | per_cpu(cpu_state, cpu) = CPU_UP_PREPARE; |
Andi Kleen | a8ab26f | 2005-04-16 15:25:19 -0700 | [diff] [blame] | 806 | /* Boot it! */ |
| 807 | err = do_boot_cpu(cpu, apicid); |
| 808 | if (err < 0) { |
Andi Kleen | a8ab26f | 2005-04-16 15:25:19 -0700 | [diff] [blame] | 809 | Dprintk("do_boot_cpu failed %d\n", err); |
| 810 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 811 | } |
| 812 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 813 | /* Unleash the CPU! */ |
| 814 | Dprintk("waiting for cpu %d\n", cpu); |
| 815 | |
Ingo Molnar | 95492e4 | 2007-02-16 01:27:34 -0800 | [diff] [blame] | 816 | /* |
| 817 | * Make sure and check TSC sync: |
| 818 | */ |
Ingo Molnar | d04f41e | 2007-03-07 18:12:31 +0100 | [diff] [blame] | 819 | local_irq_save(flags); |
Ingo Molnar | 95492e4 | 2007-02-16 01:27:34 -0800 | [diff] [blame] | 820 | check_tsc_sync_source(cpu); |
Ingo Molnar | d04f41e | 2007-03-07 18:12:31 +0100 | [diff] [blame] | 821 | local_irq_restore(flags); |
Ingo Molnar | 95492e4 | 2007-02-16 01:27:34 -0800 | [diff] [blame] | 822 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 823 | while (!cpu_isset(cpu, cpu_online_map)) |
Andi Kleen | a8ab26f | 2005-04-16 15:25:19 -0700 | [diff] [blame] | 824 | cpu_relax(); |
Ashok Raj | 76e4f66 | 2005-06-25 14:55:00 -0700 | [diff] [blame] | 825 | err = 0; |
| 826 | |
| 827 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 828 | } |
| 829 | |
Andi Kleen | a8ab26f | 2005-04-16 15:25:19 -0700 | [diff] [blame] | 830 | /* |
| 831 | * Finish the SMP boot. |
| 832 | */ |
Glauber Costa | c559764 | 2008-03-03 14:12:39 -0300 | [diff] [blame] | 833 | void __init native_smp_cpus_done(unsigned int max_cpus) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 834 | { |
Andi Kleen | a8ab26f | 2005-04-16 15:25:19 -0700 | [diff] [blame] | 835 | smp_cleanup_boot(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 836 | setup_ioapic_dest(); |
Andi Kleen | 7515211 | 2005-05-16 21:53:34 -0700 | [diff] [blame] | 837 | check_nmi_watchdog(); |
Andi Kleen | a8ab26f | 2005-04-16 15:25:19 -0700 | [diff] [blame] | 838 | } |
Ashok Raj | 76e4f66 | 2005-06-25 14:55:00 -0700 | [diff] [blame] | 839 | |
| 840 | #ifdef CONFIG_HOTPLUG_CPU |
Sam Ravnborg | 69e97c0 | 2008-02-01 17:49:41 +0100 | [diff] [blame] | 841 | static void __ref remove_cpu_from_maps(void) |
Ashok Raj | 76e4f66 | 2005-06-25 14:55:00 -0700 | [diff] [blame] | 842 | { |
| 843 | int cpu = smp_processor_id(); |
| 844 | |
| 845 | cpu_clear(cpu, cpu_callout_map); |
| 846 | cpu_clear(cpu, cpu_callin_map); |
Jeremy Fitzhardinge | 5548fec | 2008-01-30 13:30:55 +0100 | [diff] [blame] | 847 | clear_bit(cpu, (unsigned long *)&cpu_initialized); /* was set by cpu_init() */ |
Ravikiran G Thirumalai | 488fc08 | 2006-02-07 12:58:23 -0800 | [diff] [blame] | 848 | clear_node_cpumask(cpu); |
Ashok Raj | 76e4f66 | 2005-06-25 14:55:00 -0700 | [diff] [blame] | 849 | } |
| 850 | |
| 851 | int __cpu_disable(void) |
| 852 | { |
| 853 | int cpu = smp_processor_id(); |
| 854 | |
| 855 | /* |
| 856 | * Perhaps use cpufreq to drop frequency, but that could go |
| 857 | * into generic code. |
| 858 | * |
| 859 | * We won't take down the boot processor on i386 due to some |
| 860 | * interrupts only being able to be serviced by the BSP. |
| 861 | * Especially so if we're not using an IOAPIC -zwane |
| 862 | */ |
| 863 | if (cpu == 0) |
| 864 | return -EBUSY; |
| 865 | |
Shaohua Li | 4038f90 | 2006-09-26 10:52:27 +0200 | [diff] [blame] | 866 | if (nmi_watchdog == NMI_LOCAL_APIC) |
| 867 | stop_apic_nmi_watchdog(NULL); |
Shaohua Li | 5e9ef02 | 2005-12-12 22:17:08 -0800 | [diff] [blame] | 868 | clear_local_APIC(); |
Ashok Raj | 76e4f66 | 2005-06-25 14:55:00 -0700 | [diff] [blame] | 869 | |
| 870 | /* |
| 871 | * HACK: |
| 872 | * Allow any queued timer interrupts to get serviced |
| 873 | * This is only a temporary solution until we cleanup |
| 874 | * fixup_irqs as we do for IA64. |
| 875 | */ |
| 876 | local_irq_enable(); |
| 877 | mdelay(1); |
| 878 | |
| 879 | local_irq_disable(); |
| 880 | remove_siblinginfo(cpu); |
| 881 | |
| 882 | /* It's now safe to remove this processor from the online map */ |
| 883 | cpu_clear(cpu, cpu_online_map); |
| 884 | remove_cpu_from_maps(); |
| 885 | fixup_irqs(cpu_online_map); |
| 886 | return 0; |
| 887 | } |
| 888 | |
| 889 | void __cpu_die(unsigned int cpu) |
| 890 | { |
| 891 | /* We don't do anything here: idle task is faking death itself. */ |
| 892 | unsigned int i; |
| 893 | |
| 894 | for (i = 0; i < 10; i++) { |
| 895 | /* They ack this in play_dead by setting CPU_DEAD */ |
Ashok Raj | 884d9e4 | 2005-06-25 14:55:02 -0700 | [diff] [blame] | 896 | if (per_cpu(cpu_state, cpu) == CPU_DEAD) { |
| 897 | printk ("CPU %d is now offline\n", cpu); |
Gerd Hoffmann | d167a51 | 2006-06-26 13:56:16 +0200 | [diff] [blame] | 898 | if (1 == num_online_cpus()) |
| 899 | alternatives_smp_switch(0); |
Ashok Raj | 76e4f66 | 2005-06-25 14:55:00 -0700 | [diff] [blame] | 900 | return; |
Ashok Raj | 884d9e4 | 2005-06-25 14:55:02 -0700 | [diff] [blame] | 901 | } |
Nishanth Aravamudan | ef6e525 | 2005-07-28 21:15:53 -0700 | [diff] [blame] | 902 | msleep(100); |
Ashok Raj | 76e4f66 | 2005-06-25 14:55:00 -0700 | [diff] [blame] | 903 | } |
| 904 | printk(KERN_ERR "CPU %u didn't die...\n", cpu); |
| 905 | } |
| 906 | |
| 907 | #else /* ... !CONFIG_HOTPLUG_CPU */ |
| 908 | |
| 909 | int __cpu_disable(void) |
| 910 | { |
| 911 | return -ENOSYS; |
| 912 | } |
| 913 | |
| 914 | void __cpu_die(unsigned int cpu) |
| 915 | { |
| 916 | /* We said "no" in __cpu_disable */ |
| 917 | BUG(); |
| 918 | } |
| 919 | #endif /* CONFIG_HOTPLUG_CPU */ |