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