blob: d396155f436cf35c5743976c5cb646961ba6c657 [file] [log] [blame]
Glauber de Oliveira Costa4cedb332008-03-19 14:26:14 -03001/*
2 * x86 SMP booting functions
3 *
Alan Cox87c6fe22009-01-05 14:08:04 +00004 * (c) 1995 Alan Cox, Building #3 <alan@lxorguk.ukuu.org.uk>
Ingo Molnar8f47e162009-01-31 02:03:42 +01005 * (c) 1998, 1999, 2000, 2009 Ingo Molnar <mingo@redhat.com>
Glauber de Oliveira Costa4cedb332008-03-19 14:26:14 -03006 * 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 *
15 * This code is released under the GNU General Public License version 2 or
16 * later.
17 *
18 * Fixes
19 * Felix Koop : NR_CPUS used properly
20 * Jose Renau : Handle single CPU case.
21 * Alan Cox : By repeated request 8) - Total BogoMIPS report.
22 * Greg Wright : Fix for kernel stacks panic.
23 * Erich Boleyn : MP v1.4 and additional changes.
24 * Matthias Sattler : Changes for 2.1 kernel map.
25 * Michel Lespinasse : Changes for 2.1 kernel map.
26 * Michael Chastain : Change trampoline.S to gnu as.
27 * Alan Cox : Dumb bug: 'B' step PPro's are fine
28 * Ingo Molnar : Added APIC timers, based on code
29 * from Jose Renau
30 * Ingo Molnar : various cleanups and rewrites
31 * Tigran Aivazian : fixed "0.00 in /proc/uptime on SMP" bug.
32 * Maciej W. Rozycki : Bits for genuine 82489DX APICs
33 * Andi Kleen : Changed for SMP boot into long mode.
34 * Martin J. Bligh : Added support for multi-quad systems
35 * Dave Jones : Report invalid combinations of Athlon CPUs.
36 * Rusty Russell : Hacked into shape for new "hotplug" boot process.
37 * Andi Kleen : Converted to new state machine.
38 * Ashok Raj : CPU hotplug support
39 * Glauber Costa : i386 and x86_64 integration
40 */
41
Glauber Costa68a1c3f2008-03-03 14:12:42 -030042#include <linux/init.h>
43#include <linux/smp.h>
Glauber Costaa3553522008-03-03 14:12:58 -030044#include <linux/module.h>
Glauber Costa70708a12008-03-03 14:13:03 -030045#include <linux/sched.h>
Glauber Costa69c18c12008-03-03 14:13:07 -030046#include <linux/percpu.h>
Glauber Costa91718e82008-03-03 14:13:12 -030047#include <linux/bootmem.h>
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -030048#include <linux/err.h>
49#include <linux/nmi.h>
Shane Wang69575d32009-09-01 18:25:07 -070050#include <linux/tboot.h>
Jacob Pan35f720c2009-09-17 07:36:43 -070051#include <linux/stackprotector.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090052#include <linux/gfp.h>
Glauber Costa69c18c12008-03-03 14:13:07 -030053
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -030054#include <asm/acpi.h>
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -030055#include <asm/desc.h>
Glauber Costa69c18c12008-03-03 14:13:07 -030056#include <asm/nmi.h>
57#include <asm/irq.h>
Ingo Molnar07bbc162008-09-23 23:26:42 +020058#include <asm/idle.h>
Pavel Macheke44b7b72008-04-10 23:28:10 +020059#include <asm/trampoline.h>
Glauber Costa69c18c12008-03-03 14:13:07 -030060#include <asm/cpu.h>
61#include <asm/numa.h>
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -030062#include <asm/pgtable.h>
63#include <asm/tlbflush.h>
64#include <asm/mtrr.h>
H. Peter Anvinea530692010-09-17 15:39:11 -070065#include <asm/mwait.h>
Ingo Molnar7b6aa332009-02-17 13:58:15 +010066#include <asm/apic.h>
Yinghai Lu569712b2008-11-16 03:12:49 -080067#include <asm/setup.h>
Tejun Heobdbcdd42009-01-21 17:26:06 +090068#include <asm/uv/uv.h>
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -030069#include <linux/mc146818rtc.h>
Glauber Costa68a1c3f2008-03-03 14:12:42 -030070
Ingo Molnar1164dd02009-01-28 19:34:09 +010071#include <asm/smpboot_hooks.h>
Jacob Panb81bb372009-11-09 11:27:04 -080072#include <asm/i8259.h>
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -030073
Alexey Starikovskiy16ecf7a2008-04-04 23:41:00 +040074#ifdef CONFIG_X86_32
Glauber de Oliveira Costa4cedb332008-03-19 14:26:14 -030075u8 apicid_2_node[MAX_APICID];
Glauber de Oliveira Costaacbb6732008-03-19 14:26:13 -030076#endif
77
Glauber de Oliveira Costaa8db8452008-03-19 14:26:01 -030078/* State of each CPU */
79DEFINE_PER_CPU(int, cpu_state) = { 0 };
80
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -030081/* Store all idle threads, this can be reused instead of creating
82* a new thread. Also avoids complicated thread destroy functionality
83* for idle threads.
84*/
85#ifdef CONFIG_HOTPLUG_CPU
86/*
87 * Needed only for CONFIG_HOTPLUG_CPU because __cpuinitdata is
88 * removed after init for !CONFIG_HOTPLUG_CPU.
89 */
90static DEFINE_PER_CPU(struct task_struct *, idle_thread_array);
91#define get_idle_for_cpu(x) (per_cpu(idle_thread_array, x))
92#define set_idle_for_cpu(x, p) (per_cpu(idle_thread_array, x) = (p))
Borislav Petkovd7c53c92010-08-19 20:10:29 +020093
94/*
95 * We need this for trampoline_base protection from concurrent accesses when
96 * off- and onlining cores wildly.
97 */
98static DEFINE_MUTEX(x86_cpu_hotplug_driver_mutex);
99
Randy Dunlap91d88ce2011-01-08 19:59:14 -0800100void cpu_hotplug_driver_lock(void)
Borislav Petkovd7c53c92010-08-19 20:10:29 +0200101{
102 mutex_lock(&x86_cpu_hotplug_driver_mutex);
103}
104
Randy Dunlap91d88ce2011-01-08 19:59:14 -0800105void cpu_hotplug_driver_unlock(void)
Borislav Petkovd7c53c92010-08-19 20:10:29 +0200106{
107 mutex_unlock(&x86_cpu_hotplug_driver_mutex);
108}
109
110ssize_t arch_cpu_probe(const char *buf, size_t count) { return -1; }
111ssize_t arch_cpu_release(const char *buf, size_t count) { return -1; }
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300112#else
Jaswinder Singhf86c9982008-07-25 10:52:53 +0530113static struct task_struct *idle_thread_array[NR_CPUS] __cpuinitdata ;
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300114#define get_idle_for_cpu(x) (idle_thread_array[(x)])
115#define set_idle_for_cpu(x, p) (idle_thread_array[(x)] = (p))
116#endif
Glauber de Oliveira Costaf6bc4022008-03-19 14:25:53 -0300117
Glauber Costaa3553522008-03-03 14:12:58 -0300118/* Number of siblings per CPU package */
119int smp_num_siblings = 1;
120EXPORT_SYMBOL(smp_num_siblings);
121
122/* Last level cache ID of each logical CPU */
123DEFINE_PER_CPU(u16, cpu_llc_id) = BAD_APICID;
124
Glauber Costaa3553522008-03-03 14:12:58 -0300125/* representing HT siblings of each logical CPU */
Rusty Russell7ad728f2009-03-13 14:49:50 +1030126DEFINE_PER_CPU(cpumask_var_t, cpu_sibling_map);
Glauber Costaa3553522008-03-03 14:12:58 -0300127EXPORT_PER_CPU_SYMBOL(cpu_sibling_map);
128
129/* representing HT and core siblings of each logical CPU */
Rusty Russell7ad728f2009-03-13 14:49:50 +1030130DEFINE_PER_CPU(cpumask_var_t, cpu_core_map);
Glauber Costaa3553522008-03-03 14:12:58 -0300131EXPORT_PER_CPU_SYMBOL(cpu_core_map);
132
Yinghai Lub3d73362011-01-21 15:29:44 -0800133DEFINE_PER_CPU(cpumask_var_t, cpu_llc_shared_map);
134
Glauber Costaa3553522008-03-03 14:12:58 -0300135/* Per CPU bogomips and other parameters */
136DEFINE_PER_CPU_SHARED_ALIGNED(struct cpuinfo_x86, cpu_info);
137EXPORT_PER_CPU_SYMBOL(cpu_info);
Glauber Costa768d9502008-03-03 14:13:02 -0300138
Yinghai Lu2b6163b2009-02-25 20:50:49 -0800139atomic_t init_deasserted;
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300140
Glauber de Oliveira Costa7cc39592008-03-19 14:25:56 -0300141#if defined(CONFIG_NUMA) && defined(CONFIG_X86_32)
Glauber de Oliveira Costa7cc39592008-03-19 14:25:56 -0300142/* which node each logical CPU is on */
143int cpu_to_node_map[NR_CPUS] __read_mostly = { [0 ... NR_CPUS-1] = 0 };
144EXPORT_SYMBOL(cpu_to_node_map);
145
146/* set up a mapping between cpu and node. */
147static void map_cpu_to_node(int cpu, int node)
148{
149 printk(KERN_INFO "Mapping cpu %d to node %d\n", cpu, node);
Rusty Russellc032ef602009-03-13 14:49:53 +1030150 cpumask_set_cpu(cpu, node_to_cpumask_map[node]);
Glauber de Oliveira Costa7cc39592008-03-19 14:25:56 -0300151 cpu_to_node_map[cpu] = node;
152}
153
154/* undo a mapping between cpu and node. */
155static void unmap_cpu_to_node(int cpu)
156{
157 int node;
158
159 printk(KERN_INFO "Unmapping cpu %d from all nodes\n", cpu);
160 for (node = 0; node < MAX_NUMNODES; node++)
Rusty Russellc032ef602009-03-13 14:49:53 +1030161 cpumask_clear_cpu(cpu, node_to_cpumask_map[node]);
Glauber de Oliveira Costa7cc39592008-03-19 14:25:56 -0300162 cpu_to_node_map[cpu] = 0;
163}
164#else /* !(CONFIG_NUMA && CONFIG_X86_32) */
165#define map_cpu_to_node(cpu, node) ({})
166#define unmap_cpu_to_node(cpu) ({})
167#endif
168
169#ifdef CONFIG_X86_32
Suresh Siddha1b374e42008-07-10 11:16:49 -0700170static int boot_cpu_logical_apicid;
171
Glauber de Oliveira Costa7cc39592008-03-19 14:25:56 -0300172u8 cpu_2_logical_apicid[NR_CPUS] __read_mostly =
173 { [0 ... NR_CPUS-1] = BAD_APICID };
174
Ingo Molnara4928cf2008-04-23 13:20:56 +0200175static void map_cpu_to_logical_apicid(void)
Glauber de Oliveira Costa7cc39592008-03-19 14:25:56 -0300176{
177 int cpu = smp_processor_id();
178 int apicid = logical_smp_processor_id();
Ingo Molnar3f57a312009-01-28 06:50:47 +0100179 int node = apic->apicid_to_node(apicid);
Glauber de Oliveira Costa7cc39592008-03-19 14:25:56 -0300180
181 if (!node_online(node))
182 node = first_online_node;
183
184 cpu_2_logical_apicid[cpu] = apicid;
185 map_cpu_to_node(cpu, node);
186}
187
Glauber Costa1481a3d2008-06-04 15:35:03 -0300188void numa_remove_cpu(int cpu)
Glauber de Oliveira Costa7cc39592008-03-19 14:25:56 -0300189{
190 cpu_2_logical_apicid[cpu] = BAD_APICID;
191 unmap_cpu_to_node(cpu);
192}
193#else
Glauber de Oliveira Costa7cc39592008-03-19 14:25:56 -0300194#define map_cpu_to_logical_apicid() do {} while (0)
195#endif
196
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300197/*
198 * Report back to the Boot Processor.
199 * Running on AP.
200 */
Ingo Molnara4928cf2008-04-23 13:20:56 +0200201static void __cpuinit smp_callin(void)
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300202{
203 int cpuid, phys_id;
204 unsigned long timeout;
205
206 /*
207 * If waken up by an INIT in an 82489DX configuration
208 * we may get here before an INIT-deassert IPI reaches
209 * our local APIC. We have to wait for the IPI or we'll
210 * lock up on an APIC access.
211 */
Ingo Molnara9659362009-01-28 16:21:32 +0100212 if (apic->wait_for_init_deassert)
213 apic->wait_for_init_deassert(&init_deasserted);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300214
215 /*
216 * (This works even if the APIC is not enabled.)
217 */
Yinghai Lu4c9961d2008-07-11 18:44:16 -0700218 phys_id = read_apic_id();
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300219 cpuid = smp_processor_id();
Mike Travisc2d1cec2009-01-04 05:18:03 -0800220 if (cpumask_test_cpu(cpuid, cpu_callin_mask)) {
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300221 panic("%s: phys CPU#%d, CPU#%d already present??\n", __func__,
222 phys_id, cpuid);
223 }
Thomas Gleixnercfc1b9a2008-07-21 21:35:38 +0200224 pr_debug("CPU#%d (phys ID: %d) waiting for CALLOUT\n", cpuid, phys_id);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300225
226 /*
227 * STARTUP IPIs are fragile beasts as they might sometimes
228 * trigger some glue motherboard logic. Complete APIC bus
229 * silence for 1 second, this overestimates the time the
230 * boot CPU is spending to send the up to 2 STARTUP IPIs
231 * by a factor of two. This should be enough.
232 */
233
234 /*
235 * Waiting 2s total for startup (udelay is not yet working)
236 */
237 timeout = jiffies + 2*HZ;
238 while (time_before(jiffies, timeout)) {
239 /*
240 * Has the boot CPU finished it's STARTUP sequence?
241 */
Mike Travisc2d1cec2009-01-04 05:18:03 -0800242 if (cpumask_test_cpu(cpuid, cpu_callout_mask))
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300243 break;
244 cpu_relax();
245 }
246
247 if (!time_before(jiffies, timeout)) {
248 panic("%s: CPU%d started up but did not get a callout!\n",
249 __func__, cpuid);
250 }
251
252 /*
253 * the boot CPU has finished the init stage and is spinning
254 * on callin_map until we finish. We are free to set up this
255 * CPU, first the APIC. (this is probably redundant on most
256 * boards)
257 */
258
Thomas Gleixnercfc1b9a2008-07-21 21:35:38 +0200259 pr_debug("CALLIN, before setup_local_APIC().\n");
Ingo Molnar333344d2009-01-28 16:31:52 +0100260 if (apic->smp_callin_clear_local_apic)
261 apic->smp_callin_clear_local_apic();
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300262 setup_local_APIC();
263 end_local_APIC_setup();
264 map_cpu_to_logical_apicid();
265
Suresh Siddha9d133e52010-01-29 11:42:21 -0800266 /*
267 * Need to setup vector mappings before we enable interrupts.
268 */
Suresh Siddha36e9e1e2010-03-15 14:33:06 -0800269 setup_vector_irq(smp_processor_id());
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300270 /*
271 * Get our bogomips.
272 *
273 * Need to enable IRQs because it can take longer and then
274 * the NMI watchdog might kill us.
275 */
276 local_irq_enable();
277 calibrate_delay();
278 local_irq_disable();
Thomas Gleixnercfc1b9a2008-07-21 21:35:38 +0200279 pr_debug("Stack at about %p\n", &cpuid);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300280
281 /*
282 * Save our processor parameters
283 */
284 smp_store_cpu_info(cpuid);
285
Andi Kleen5ef428c2010-11-18 11:47:31 +0100286 /*
287 * This must be done before setting cpu_online_mask
288 * or calling notify_cpu_starting.
289 */
290 set_cpu_sibling_map(raw_smp_processor_id());
291 wmb();
292
Peter Zijlstra85257022010-03-23 19:30:52 +0100293 notify_cpu_starting(cpuid);
294
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300295 /*
296 * Allow the master to continue.
297 */
Mike Travisc2d1cec2009-01-04 05:18:03 -0800298 cpumask_set_cpu(cpuid, cpu_callin_mask);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300299}
300
Glauber de Oliveira Costabbc2ff62008-03-19 14:26:00 -0300301/*
302 * Activate a secondary processor.
303 */
Frederic Weisbecker0ca59dd2008-12-24 23:30:02 +0100304notrace static void __cpuinit start_secondary(void *unused)
Glauber de Oliveira Costabbc2ff62008-03-19 14:26:00 -0300305{
306 /*
307 * Don't put *anything* before cpu_init(), SMP booting is too
308 * fragile that we want to limit the things done here to the
309 * most necessary things.
310 */
Glauber de Oliveira Costabbc2ff62008-03-19 14:26:00 -0300311 cpu_init();
312 preempt_disable();
313 smp_callin();
314
Borislav Petkovb40827fa2010-08-28 15:58:33 +0200315#ifdef CONFIG_X86_32
316 /* switch away from the initial page table */
317 load_cr3(swapper_pg_dir);
318 __flush_tlb_all();
319#endif
320
Glauber de Oliveira Costabbc2ff62008-03-19 14:26:00 -0300321 /* otherwise gcc will move up smp_processor_id before the cpu_init */
322 barrier();
323 /*
324 * Check TSC synchronization with the BP:
325 */
326 check_tsc_sync_target();
327
Glauber de Oliveira Costabbc2ff62008-03-19 14:26:00 -0300328 /*
329 * We need to hold call_lock, so there is no inconsistency
330 * between the time smp_call_function() determines number of
331 * IPI recipients, and the time when the determination is made
332 * for which cpus receive the IPI. Holding this
333 * lock helps us to not include this cpu in a currently in progress
334 * smp_call_function().
Eric W. Biedermand388e5f2008-08-09 15:09:02 -0700335 *
336 * We need to hold vector_lock so there the set of online cpus
337 * does not change while we are assigning vectors to cpus. Holding
338 * this lock ensures we don't half assign or remove an irq from a cpu.
Glauber de Oliveira Costabbc2ff62008-03-19 14:26:00 -0300339 */
Manfred Spraul0cefa5b2008-09-07 11:29:58 +0200340 ipi_call_lock();
Eric W. Biedermand388e5f2008-08-09 15:09:02 -0700341 lock_vector_lock();
Mike Travisc2d1cec2009-01-04 05:18:03 -0800342 set_cpu_online(smp_processor_id(), true);
Eric W. Biedermand388e5f2008-08-09 15:09:02 -0700343 unlock_vector_lock();
Manfred Spraul0cefa5b2008-09-07 11:29:58 +0200344 ipi_call_unlock();
Glauber de Oliveira Costabbc2ff62008-03-19 14:26:00 -0300345 per_cpu(cpu_state, smp_processor_id()) = CPU_ONLINE;
Russ Anderson78c06172010-02-26 10:49:12 -0600346 x86_platform.nmi_init();
Glauber de Oliveira Costabbc2ff62008-03-19 14:26:00 -0300347
Manfred Spraul0cefa5b2008-09-07 11:29:58 +0200348 /* enable local interrupts */
349 local_irq_enable();
350
Jacob Pan35f720c2009-09-17 07:36:43 -0700351 /* to prevent fake stack check failure in clock setup */
352 boot_init_stack_canary();
353
Thomas Gleixner736deca2009-08-19 12:35:53 +0200354 x86_cpuinit.setup_percpu_clockev();
Glauber de Oliveira Costabbc2ff62008-03-19 14:26:00 -0300355
356 wmb();
357 cpu_idle();
358}
359
Glauber de Oliveira Costa1d89a7f2008-03-19 14:25:05 -0300360/*
361 * The bootstrap kernel entry code has set these up. Save them for
362 * a given CPU
363 */
364
365void __cpuinit smp_store_cpu_info(int id)
366{
367 struct cpuinfo_x86 *c = &cpu_data(id);
368
Yinghai Lub3d73362011-01-21 15:29:44 -0800369 *c = boot_cpu_data;
Glauber de Oliveira Costa1d89a7f2008-03-19 14:25:05 -0300370 c->cpu_index = id;
371 if (id != 0)
372 identify_secondary_cpu(c);
Glauber de Oliveira Costa1d89a7f2008-03-19 14:25:05 -0300373}
374
Andreas Herrmannd4fbe4f2010-09-30 14:41:56 +0200375static void __cpuinit link_thread_siblings(int cpu1, int cpu2)
376{
Andreas Herrmannd4fbe4f2010-09-30 14:41:56 +0200377 cpumask_set_cpu(cpu1, cpu_sibling_mask(cpu2));
378 cpumask_set_cpu(cpu2, cpu_sibling_mask(cpu1));
379 cpumask_set_cpu(cpu1, cpu_core_mask(cpu2));
380 cpumask_set_cpu(cpu2, cpu_core_mask(cpu1));
Yinghai Lub3d73362011-01-21 15:29:44 -0800381 cpumask_set_cpu(cpu1, cpu_llc_shared_mask(cpu2));
382 cpumask_set_cpu(cpu2, cpu_llc_shared_mask(cpu1));
Andreas Herrmannd4fbe4f2010-09-30 14:41:56 +0200383}
384
Glauber de Oliveira Costa1d89a7f2008-03-19 14:25:05 -0300385
Glauber Costa768d9502008-03-03 14:13:02 -0300386void __cpuinit set_cpu_sibling_map(int cpu)
387{
388 int i;
389 struct cpuinfo_x86 *c = &cpu_data(cpu);
390
Mike Travisc2d1cec2009-01-04 05:18:03 -0800391 cpumask_set_cpu(cpu, cpu_sibling_setup_mask);
Glauber Costa768d9502008-03-03 14:13:02 -0300392
393 if (smp_num_siblings > 1) {
Mike Travisc2d1cec2009-01-04 05:18:03 -0800394 for_each_cpu(i, cpu_sibling_setup_mask) {
395 struct cpuinfo_x86 *o = &cpu_data(i);
396
Andreas Herrmannd4fbe4f2010-09-30 14:41:56 +0200397 if (cpu_has(c, X86_FEATURE_TOPOEXT)) {
398 if (c->phys_proc_id == o->phys_proc_id &&
399 c->compute_unit_id == o->compute_unit_id)
400 link_thread_siblings(cpu, i);
401 } else if (c->phys_proc_id == o->phys_proc_id &&
402 c->cpu_core_id == o->cpu_core_id) {
403 link_thread_siblings(cpu, i);
Glauber Costa768d9502008-03-03 14:13:02 -0300404 }
405 }
406 } else {
Mike Travisc2d1cec2009-01-04 05:18:03 -0800407 cpumask_set_cpu(cpu, cpu_sibling_mask(cpu));
Glauber Costa768d9502008-03-03 14:13:02 -0300408 }
409
Yinghai Lub3d73362011-01-21 15:29:44 -0800410 cpumask_set_cpu(cpu, cpu_llc_shared_mask(cpu));
Glauber Costa768d9502008-03-03 14:13:02 -0300411
Tejun Heo7b543a52010-12-18 16:30:05 +0100412 if (__this_cpu_read(cpu_info.x86_max_cores) == 1) {
Mike Travisc2d1cec2009-01-04 05:18:03 -0800413 cpumask_copy(cpu_core_mask(cpu), cpu_sibling_mask(cpu));
Glauber Costa768d9502008-03-03 14:13:02 -0300414 c->booted_cores = 1;
415 return;
416 }
417
Mike Travisc2d1cec2009-01-04 05:18:03 -0800418 for_each_cpu(i, cpu_sibling_setup_mask) {
Glauber Costa768d9502008-03-03 14:13:02 -0300419 if (per_cpu(cpu_llc_id, cpu) != BAD_APICID &&
420 per_cpu(cpu_llc_id, cpu) == per_cpu(cpu_llc_id, i)) {
Yinghai Lub3d73362011-01-21 15:29:44 -0800421 cpumask_set_cpu(i, cpu_llc_shared_mask(cpu));
422 cpumask_set_cpu(cpu, cpu_llc_shared_mask(i));
Glauber Costa768d9502008-03-03 14:13:02 -0300423 }
424 if (c->phys_proc_id == cpu_data(i).phys_proc_id) {
Mike Travisc2d1cec2009-01-04 05:18:03 -0800425 cpumask_set_cpu(i, cpu_core_mask(cpu));
426 cpumask_set_cpu(cpu, cpu_core_mask(i));
Glauber Costa768d9502008-03-03 14:13:02 -0300427 /*
428 * Does this new cpu bringup a new core?
429 */
Mike Travisc2d1cec2009-01-04 05:18:03 -0800430 if (cpumask_weight(cpu_sibling_mask(cpu)) == 1) {
Glauber Costa768d9502008-03-03 14:13:02 -0300431 /*
432 * for each core in package, increment
433 * the booted_cores for this new cpu
434 */
Mike Travisc2d1cec2009-01-04 05:18:03 -0800435 if (cpumask_first(cpu_sibling_mask(i)) == i)
Glauber Costa768d9502008-03-03 14:13:02 -0300436 c->booted_cores++;
437 /*
438 * increment the core count for all
439 * the other cpus in this package
440 */
441 if (i != cpu)
442 cpu_data(i).booted_cores++;
443 } else if (i != cpu && !c->booted_cores)
444 c->booted_cores = cpu_data(i).booted_cores;
445 }
446 }
447}
448
Glauber Costa70708a12008-03-03 14:13:03 -0300449/* maps the cpu to the sched domain representing multi-core */
Rusty Russell030bb202008-12-26 22:23:41 +1030450const struct cpumask *cpu_coregroup_mask(int cpu)
Glauber Costa70708a12008-03-03 14:13:03 -0300451{
452 struct cpuinfo_x86 *c = &cpu_data(cpu);
453 /*
454 * For perf, we return last level cache shared map.
455 * And for power savings, we return cpu_core_map
456 */
Andreas Herrmann5a925b42009-09-03 09:44:28 +0200457 if ((sched_mc_power_savings || sched_smt_power_savings) &&
458 !(cpu_has(c, X86_FEATURE_AMD_DCM)))
Mike Travisc2d1cec2009-01-04 05:18:03 -0800459 return cpu_core_mask(cpu);
Glauber Costa70708a12008-03-03 14:13:03 -0300460 else
Yinghai Lub3d73362011-01-21 15:29:44 -0800461 return cpu_llc_shared_mask(cpu);
Rusty Russell030bb202008-12-26 22:23:41 +1030462}
463
Ingo Molnara4928cf2008-04-23 13:20:56 +0200464static void impress_friends(void)
Glauber de Oliveira Costa904541e2008-03-19 14:25:27 -0300465{
466 int cpu;
467 unsigned long bogosum = 0;
468 /*
469 * Allow the user to impress friends.
470 */
Thomas Gleixnercfc1b9a2008-07-21 21:35:38 +0200471 pr_debug("Before bogomips.\n");
Glauber de Oliveira Costa904541e2008-03-19 14:25:27 -0300472 for_each_possible_cpu(cpu)
Mike Travisc2d1cec2009-01-04 05:18:03 -0800473 if (cpumask_test_cpu(cpu, cpu_callout_mask))
Glauber de Oliveira Costa904541e2008-03-19 14:25:27 -0300474 bogosum += cpu_data(cpu).loops_per_jiffy;
475 printk(KERN_INFO
476 "Total of %d processors activated (%lu.%02lu BogoMIPS).\n",
Glauber de Oliveira Costaf68e00a2008-03-19 14:25:29 -0300477 num_online_cpus(),
Glauber de Oliveira Costa904541e2008-03-19 14:25:27 -0300478 bogosum/(500000/HZ),
479 (bogosum/(5000/HZ))%100);
480
Thomas Gleixnercfc1b9a2008-07-21 21:35:38 +0200481 pr_debug("Before bogocount - setting activated=1.\n");
Glauber de Oliveira Costa904541e2008-03-19 14:25:27 -0300482}
483
Yinghai Lu569712b2008-11-16 03:12:49 -0800484void __inquire_remote_apic(int apicid)
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300485{
486 unsigned i, regs[] = { APIC_ID >> 4, APIC_LVR >> 4, APIC_SPIV >> 4 };
487 char *names[] = { "ID", "VERSION", "SPIV" };
488 int timeout;
489 u32 status;
490
Yinghai Lu823b2592008-09-10 21:56:46 -0700491 printk(KERN_INFO "Inquiring remote APIC 0x%x...\n", apicid);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300492
493 for (i = 0; i < ARRAY_SIZE(regs); i++) {
Yinghai Lu823b2592008-09-10 21:56:46 -0700494 printk(KERN_INFO "... APIC 0x%x %s: ", apicid, names[i]);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300495
496 /*
497 * Wait for idle.
498 */
499 status = safe_apic_wait_icr_idle();
500 if (status)
501 printk(KERN_CONT
502 "a previous APIC delivery may have failed\n");
503
Suresh Siddha1b374e42008-07-10 11:16:49 -0700504 apic_icr_write(APIC_DM_REMRD | regs[i], apicid);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300505
506 timeout = 0;
507 do {
508 udelay(100);
509 status = apic_read(APIC_ICR) & APIC_ICR_RR_MASK;
510 } while (status == APIC_ICR_RR_INPROG && timeout++ < 1000);
511
512 switch (status) {
513 case APIC_ICR_RR_VALID:
514 status = apic_read(APIC_RRR);
515 printk(KERN_CONT "%08x\n", status);
516 break;
517 default:
518 printk(KERN_CONT "failed\n");
519 }
520 }
521}
522
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300523/*
524 * Poke the other CPU in the eye via NMI to wake it up. Remember that the normal
525 * INIT, INIT, STARTUP sequence will reset the chip hard for us, and this
526 * won't ... remember to clear down the APIC, etc later.
527 */
Cyrill Gorcunovcece3152009-04-18 23:45:28 +0400528int __cpuinit
Yinghai Lu569712b2008-11-16 03:12:49 -0800529wakeup_secondary_cpu_via_nmi(int logical_apicid, unsigned long start_eip)
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300530{
531 unsigned long send_status, accept_status = 0;
532 int maxlvt;
533
534 /* Target chip */
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300535 /* Boot on the stack */
536 /* Kick the second */
Ingo Molnarbdb1a9b2009-01-28 05:29:25 +0100537 apic_icr_write(APIC_DM_NMI | apic->dest_logical, logical_apicid);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300538
Thomas Gleixnercfc1b9a2008-07-21 21:35:38 +0200539 pr_debug("Waiting for send to finish...\n");
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300540 send_status = safe_apic_wait_icr_idle();
541
542 /*
543 * Give the other CPU some time to accept the IPI.
544 */
545 udelay(200);
Yinghai Lu569712b2008-11-16 03:12:49 -0800546 if (APIC_INTEGRATED(apic_version[boot_cpu_physical_apicid])) {
Cyrill Gorcunov59ef48a2008-09-14 21:58:49 +0400547 maxlvt = lapic_get_maxlvt();
548 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
549 apic_write(APIC_ESR, 0);
550 accept_status = (apic_read(APIC_ESR) & 0xEF);
551 }
Thomas Gleixnercfc1b9a2008-07-21 21:35:38 +0200552 pr_debug("NMI sent.\n");
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300553
554 if (send_status)
555 printk(KERN_ERR "APIC never delivered???\n");
556 if (accept_status)
557 printk(KERN_ERR "APIC delivery error (%lx).\n", accept_status);
558
559 return (send_status | accept_status);
560}
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300561
Cyrill Gorcunovcece3152009-04-18 23:45:28 +0400562static int __cpuinit
Yinghai Lu569712b2008-11-16 03:12:49 -0800563wakeup_secondary_cpu_via_init(int phys_apicid, unsigned long start_eip)
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300564{
565 unsigned long send_status, accept_status = 0;
566 int maxlvt, num_starts, j;
567
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +0100568 maxlvt = lapic_get_maxlvt();
569
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300570 /*
571 * Be paranoid about clearing APIC errors.
572 */
573 if (APIC_INTEGRATED(apic_version[phys_apicid])) {
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +0100574 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
575 apic_write(APIC_ESR, 0);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300576 apic_read(APIC_ESR);
577 }
578
Thomas Gleixnercfc1b9a2008-07-21 21:35:38 +0200579 pr_debug("Asserting INIT.\n");
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300580
581 /*
582 * Turn INIT on target chip
583 */
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300584 /*
585 * Send IPI
586 */
Suresh Siddha1b374e42008-07-10 11:16:49 -0700587 apic_icr_write(APIC_INT_LEVELTRIG | APIC_INT_ASSERT | APIC_DM_INIT,
588 phys_apicid);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300589
Thomas Gleixnercfc1b9a2008-07-21 21:35:38 +0200590 pr_debug("Waiting for send to finish...\n");
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300591 send_status = safe_apic_wait_icr_idle();
592
593 mdelay(10);
594
Thomas Gleixnercfc1b9a2008-07-21 21:35:38 +0200595 pr_debug("Deasserting INIT.\n");
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300596
597 /* Target chip */
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300598 /* Send IPI */
Suresh Siddha1b374e42008-07-10 11:16:49 -0700599 apic_icr_write(APIC_INT_LEVELTRIG | APIC_DM_INIT, phys_apicid);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300600
Thomas Gleixnercfc1b9a2008-07-21 21:35:38 +0200601 pr_debug("Waiting for send to finish...\n");
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300602 send_status = safe_apic_wait_icr_idle();
603
604 mb();
605 atomic_set(&init_deasserted, 1);
606
607 /*
608 * Should we send STARTUP IPIs ?
609 *
610 * Determine this based on the APIC version.
611 * If we don't have an integrated APIC, don't send the STARTUP IPIs.
612 */
613 if (APIC_INTEGRATED(apic_version[phys_apicid]))
614 num_starts = 2;
615 else
616 num_starts = 0;
617
618 /*
619 * Paravirt / VMI wants a startup IPI hook here to set up the
620 * target processor state.
621 */
622 startup_ipi_hook(phys_apicid, (unsigned long) start_secondary,
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300623 (unsigned long)stack_start.sp);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300624
625 /*
626 * Run STARTUP IPI loop.
627 */
Thomas Gleixnercfc1b9a2008-07-21 21:35:38 +0200628 pr_debug("#startup loops: %d.\n", num_starts);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300629
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300630 for (j = 1; j <= num_starts; j++) {
Thomas Gleixnercfc1b9a2008-07-21 21:35:38 +0200631 pr_debug("Sending STARTUP #%d.\n", j);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +0100632 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
633 apic_write(APIC_ESR, 0);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300634 apic_read(APIC_ESR);
Thomas Gleixnercfc1b9a2008-07-21 21:35:38 +0200635 pr_debug("After apic_write.\n");
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300636
637 /*
638 * STARTUP IPI
639 */
640
641 /* Target chip */
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300642 /* Boot on the stack */
643 /* Kick the second */
Suresh Siddha1b374e42008-07-10 11:16:49 -0700644 apic_icr_write(APIC_DM_STARTUP | (start_eip >> 12),
645 phys_apicid);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300646
647 /*
648 * Give the other CPU some time to accept the IPI.
649 */
650 udelay(300);
651
Thomas Gleixnercfc1b9a2008-07-21 21:35:38 +0200652 pr_debug("Startup point 1.\n");
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300653
Thomas Gleixnercfc1b9a2008-07-21 21:35:38 +0200654 pr_debug("Waiting for send to finish...\n");
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300655 send_status = safe_apic_wait_icr_idle();
656
657 /*
658 * Give the other CPU some time to accept the IPI.
659 */
660 udelay(200);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +0100661 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300662 apic_write(APIC_ESR, 0);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300663 accept_status = (apic_read(APIC_ESR) & 0xEF);
664 if (send_status || accept_status)
665 break;
666 }
Thomas Gleixnercfc1b9a2008-07-21 21:35:38 +0200667 pr_debug("After Startup.\n");
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300668
669 if (send_status)
670 printk(KERN_ERR "APIC never delivered???\n");
671 if (accept_status)
672 printk(KERN_ERR "APIC delivery error (%lx).\n", accept_status);
673
674 return (send_status | accept_status);
675}
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300676
677struct create_idle {
678 struct work_struct work;
679 struct task_struct *idle;
680 struct completion done;
681 int cpu;
682};
683
684static void __cpuinit do_fork_idle(struct work_struct *work)
685{
686 struct create_idle *c_idle =
687 container_of(work, struct create_idle, work);
688
689 c_idle->idle = fork_idle(c_idle->cpu);
690 complete(&c_idle->done);
691}
692
Mike Travis2eaad1f2009-12-10 17:19:36 -0800693/* reduce the number of lines printed when booting a large cpu count system */
694static void __cpuinit announce_cpu(int cpu, int apicid)
695{
696 static int current_node = -1;
Borislav Petkov4adc8b72010-06-01 21:04:55 +0200697 int node = early_cpu_to_node(cpu);
Mike Travis2eaad1f2009-12-10 17:19:36 -0800698
699 if (system_state == SYSTEM_BOOTING) {
700 if (node != current_node) {
701 if (current_node > (-1))
702 pr_cont(" Ok.\n");
703 current_node = node;
704 pr_info("Booting Node %3d, Processors ", node);
705 }
706 pr_cont(" #%d%s", cpu, cpu == (nr_cpu_ids - 1) ? " Ok.\n" : "");
707 return;
708 } else
709 pr_info("Booting Node %d Processor %d APIC 0x%x\n",
710 node, cpu, apicid);
711}
712
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300713/*
714 * NOTE - on most systems this is a PHYSICAL apic ID, but on multiquad
715 * (ie clustered apic addressing mode), this is a LOGICAL apic ID.
Ingo Molnar1f5bcab2009-02-26 13:51:40 +0100716 * Returns zero if CPU booted OK, else error code from
717 * ->wakeup_secondary_cpu.
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300718 */
Ingo Molnarab6fb7c2009-02-17 16:22:09 +0100719static int __cpuinit do_boot_cpu(int apicid, int cpu)
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300720{
721 unsigned long boot_error = 0;
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300722 unsigned long start_ip;
Ingo Molnarab6fb7c2009-02-17 16:22:09 +0100723 int timeout;
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300724 struct create_idle c_idle = {
Ingo Molnarab6fb7c2009-02-17 16:22:09 +0100725 .cpu = cpu,
726 .done = COMPLETION_INITIALIZER_ONSTACK(c_idle.done),
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300727 };
Ingo Molnarab6fb7c2009-02-17 16:22:09 +0100728
Andrew Mortonca1cab32010-10-26 14:22:34 -0700729 INIT_WORK_ONSTACK(&c_idle.work, do_fork_idle);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300730
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300731 alternatives_smp_switch(1);
732
733 c_idle.idle = get_idle_for_cpu(cpu);
734
735 /*
736 * We can't use kernel_thread since we must avoid to
737 * reschedule the child.
738 */
739 if (c_idle.idle) {
740 c_idle.idle->thread.sp = (unsigned long) (((struct pt_regs *)
741 (THREAD_SIZE + task_stack_page(c_idle.idle))) - 1);
742 init_idle(c_idle.idle, cpu);
743 goto do_rest;
744 }
745
Suresh Siddhad7a7c572010-08-09 17:20:33 -0700746 schedule_work(&c_idle.work);
747 wait_for_completion(&c_idle.done);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300748
749 if (IS_ERR(c_idle.idle)) {
750 printk("failed fork for CPU %d\n", cpu);
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900751 destroy_work_on_stack(&c_idle.work);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300752 return PTR_ERR(c_idle.idle);
753 }
754
755 set_idle_for_cpu(cpu, c_idle.idle);
756do_rest:
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300757 per_cpu(current_task, cpu) = c_idle.idle;
Brian Gerstc6f5e0a2009-01-19 00:38:58 +0900758#ifdef CONFIG_X86_32
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300759 /* Stack for startup_32 can be just as for start_secondary onwards */
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300760 irq_ctx_init(cpu);
761#else
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300762 clear_tsk_thread_flag(c_idle.idle, TIF_FORK);
Tejun Heo004aa322009-01-13 20:41:35 +0900763 initial_gs = per_cpu_offset(cpu);
Brian Gerst9af45652009-01-19 00:38:58 +0900764 per_cpu(kernel_stack, cpu) =
765 (unsigned long)task_stack_page(c_idle.idle) -
766 KERNEL_STACK_OFFSET + THREAD_SIZE;
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300767#endif
Glauber Costaa9390982008-05-28 16:19:53 -0700768 early_gdt_descr.address = (unsigned long)get_cpu_gdt_table(cpu);
Glauber Costa3e970472008-05-28 13:01:54 -0300769 initial_code = (unsigned long)start_secondary;
Glauber Costa9cf4f292008-05-27 18:22:54 -0700770 stack_start.sp = (void *) c_idle.idle->thread.sp;
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300771
772 /* start_ip had better be page-aligned! */
773 start_ip = setup_trampoline();
774
Mike Travis2eaad1f2009-12-10 17:19:36 -0800775 /* So we see what's up */
776 announce_cpu(cpu, apicid);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300777
778 /*
779 * This grunge runs the startup process for
780 * the targeted processor.
781 */
782
783 atomic_set(&init_deasserted, 0);
784
Jack Steiner34d05592008-04-16 11:45:15 -0500785 if (get_uv_system_type() != UV_NON_UNIQUE_APIC) {
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300786
Thomas Gleixnercfc1b9a2008-07-21 21:35:38 +0200787 pr_debug("Setting warm reset code and vector.\n");
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300788
Jack Steiner34d05592008-04-16 11:45:15 -0500789 smpboot_setup_warm_reset_vector(start_ip);
790 /*
791 * Be paranoid about clearing APIC errors.
Cyrill Gorcunovdb96b0a2008-10-22 18:00:09 +0400792 */
793 if (APIC_INTEGRATED(apic_version[boot_cpu_physical_apicid])) {
794 apic_write(APIC_ESR, 0);
795 apic_read(APIC_ESR);
796 }
Jack Steiner34d05592008-04-16 11:45:15 -0500797 }
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300798
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300799 /*
Ingo Molnar1f5bcab2009-02-26 13:51:40 +0100800 * Kick the secondary CPU. Use the method in the APIC driver
801 * if it's defined - or use an INIT boot APIC message otherwise:
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300802 */
Ingo Molnar1f5bcab2009-02-26 13:51:40 +0100803 if (apic->wakeup_secondary_cpu)
804 boot_error = apic->wakeup_secondary_cpu(apicid, start_ip);
805 else
806 boot_error = wakeup_secondary_cpu_via_init(apicid, start_ip);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300807
808 if (!boot_error) {
809 /*
810 * allow APs to start initializing.
811 */
Thomas Gleixnercfc1b9a2008-07-21 21:35:38 +0200812 pr_debug("Before Callout %d.\n", cpu);
Mike Travisc2d1cec2009-01-04 05:18:03 -0800813 cpumask_set_cpu(cpu, cpu_callout_mask);
Thomas Gleixnercfc1b9a2008-07-21 21:35:38 +0200814 pr_debug("After Callout %d.\n", cpu);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300815
816 /*
817 * Wait 5s total for a response
818 */
819 for (timeout = 0; timeout < 50000; timeout++) {
Mike Travisc2d1cec2009-01-04 05:18:03 -0800820 if (cpumask_test_cpu(cpu, cpu_callin_mask))
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300821 break; /* It has booted */
822 udelay(100);
Suresh Siddha68f202e2010-07-30 11:46:42 -0700823 /*
824 * Allow other tasks to run while we wait for the
825 * AP to come online. This also gives a chance
826 * for the MTRR work(triggered by the AP coming online)
827 * to be completed in the stop machine context.
828 */
829 schedule();
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300830 }
831
Mike Travis2eaad1f2009-12-10 17:19:36 -0800832 if (cpumask_test_cpu(cpu, cpu_callin_mask))
833 pr_debug("CPU%d: has booted.\n", cpu);
834 else {
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300835 boot_error = 1;
836 if (*((volatile unsigned char *)trampoline_base)
837 == 0xA5)
838 /* trampoline started but...? */
Mike Travis2eaad1f2009-12-10 17:19:36 -0800839 pr_err("CPU%d: Stuck ??\n", cpu);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300840 else
841 /* trampoline code not run */
Mike Travis2eaad1f2009-12-10 17:19:36 -0800842 pr_err("CPU%d: Not responding.\n", cpu);
Ingo Molnar25dc0042009-01-28 16:31:52 +0100843 if (apic->inquire_remote_apic)
844 apic->inquire_remote_apic(apicid);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300845 }
846 }
Tejun Heo1a51e3a2009-01-13 20:41:35 +0900847
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300848 if (boot_error) {
849 /* Try to put things back the way they were before ... */
Mike Travis23ca4bb2008-05-12 21:21:12 +0200850 numa_remove_cpu(cpu); /* was set by numa_add_cpu */
Mike Travisc2d1cec2009-01-04 05:18:03 -0800851
852 /* was set by do_boot_cpu() */
853 cpumask_clear_cpu(cpu, cpu_callout_mask);
854
855 /* was set by cpu_init() */
856 cpumask_clear_cpu(cpu, cpu_initialized_mask);
857
858 set_cpu_present(cpu, false);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300859 per_cpu(x86_cpu_to_apicid, cpu) = BAD_APICID;
860 }
861
862 /* mark "stuck" area as not stuck */
863 *((volatile unsigned long *)trampoline_base) = 0;
864
Yinghai Lu02421f92009-04-03 17:15:53 -0700865 if (get_uv_system_type() != UV_NON_UNIQUE_APIC) {
866 /*
867 * Cleanup possible dangling ends...
868 */
869 smpboot_restore_warm_reset_vector();
870 }
Alok Kataria63d38192008-04-07 11:38:33 -0700871
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900872 destroy_work_on_stack(&c_idle.work);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300873 return boot_error;
874}
875
876int __cpuinit native_cpu_up(unsigned int cpu)
877{
Ingo Molnara21769a42009-01-28 06:50:47 +0100878 int apicid = apic->cpu_present_to_apicid(cpu);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300879 unsigned long flags;
880 int err;
881
882 WARN_ON(irqs_disabled());
883
Thomas Gleixnercfc1b9a2008-07-21 21:35:38 +0200884 pr_debug("++++++++++++++++++++=_---CPU UP %u\n", cpu);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300885
886 if (apicid == BAD_APICID || apicid == boot_cpu_physical_apicid ||
887 !physid_isset(apicid, phys_cpu_present_map)) {
888 printk(KERN_ERR "%s: bad cpu %d\n", __func__, cpu);
889 return -EINVAL;
890 }
891
892 /*
893 * Already booted CPU?
894 */
Mike Travisc2d1cec2009-01-04 05:18:03 -0800895 if (cpumask_test_cpu(cpu, cpu_callin_mask)) {
Thomas Gleixnercfc1b9a2008-07-21 21:35:38 +0200896 pr_debug("do_boot_cpu %d Already started\n", cpu);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300897 return -ENOSYS;
898 }
899
900 /*
901 * Save current MTRR state in case it was changed since early boot
902 * (e.g. by the ACPI SMI) to initialize new CPUs with MTRRs in sync:
903 */
904 mtrr_save_state();
905
906 per_cpu(cpu_state, cpu) = CPU_UP_PREPARE;
907
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300908 err = do_boot_cpu(apicid, cpu);
Hugh Dickins61165d72008-05-13 14:26:57 +0100909 if (err) {
Thomas Gleixnercfc1b9a2008-07-21 21:35:38 +0200910 pr_debug("do_boot_cpu failed %d\n", err);
Hugh Dickins61165d72008-05-13 14:26:57 +0100911 return -EIO;
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300912 }
913
914 /*
915 * Check TSC synchronization with the AP (keep irqs disabled
916 * while doing so):
917 */
918 local_irq_save(flags);
919 check_tsc_sync_source(cpu);
920 local_irq_restore(flags);
921
Akinobu Mita7c04e642008-04-19 23:55:17 +0900922 while (!cpu_online(cpu)) {
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300923 cpu_relax();
924 touch_nmi_watchdog();
925 }
926
927 return 0;
928}
929
Glauber de Oliveira Costaa8db8452008-03-19 14:26:01 -0300930/*
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -0300931 * Fall back to non SMP mode after errors.
932 *
933 * RED-PEN audit/test this more. I bet there is more state messed up here.
934 */
935static __init void disable_smp(void)
936{
Rusty Russell4f062892009-03-13 14:49:54 +1030937 init_cpu_present(cpumask_of(0));
938 init_cpu_possible(cpumask_of(0));
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -0300939 smpboot_clear_io_apic_irqs();
Glauber Costa0f385d12008-05-28 17:09:53 -0700940
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -0300941 if (smp_found_config)
Jack Steinerb6df1b82008-06-19 21:51:05 -0500942 physid_set_mask_of_physid(boot_cpu_physical_apicid, &phys_cpu_present_map);
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -0300943 else
Jack Steinerb6df1b82008-06-19 21:51:05 -0500944 physid_set_mask_of_physid(0, &phys_cpu_present_map);
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -0300945 map_cpu_to_logical_apicid();
Mike Travisc2d1cec2009-01-04 05:18:03 -0800946 cpumask_set_cpu(0, cpu_sibling_mask(0));
947 cpumask_set_cpu(0, cpu_core_mask(0));
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -0300948}
949
950/*
951 * Various sanity checks.
952 */
953static int __init smp_sanity_check(unsigned max_cpus)
954{
Jack Steinerac23d4e2008-03-28 14:12:16 -0500955 preempt_disable();
Yinghai Lua58f03b2008-08-14 02:16:30 -0700956
Yinghai Lu1ff2f202009-01-29 19:30:04 -0800957#if !defined(CONFIG_X86_BIGSMP) && defined(CONFIG_X86_32)
Yinghai Lua58f03b2008-08-14 02:16:30 -0700958 if (def_to_bigsmp && nr_cpu_ids > 8) {
959 unsigned int cpu;
960 unsigned nr;
961
962 printk(KERN_WARNING
963 "More than 8 CPUs detected - skipping them.\n"
Yinghai Lu26f7ef12009-01-29 14:19:22 -0800964 "Use CONFIG_X86_BIGSMP.\n");
Yinghai Lua58f03b2008-08-14 02:16:30 -0700965
966 nr = 0;
967 for_each_present_cpu(cpu) {
968 if (nr >= 8)
Mike Travisc2d1cec2009-01-04 05:18:03 -0800969 set_cpu_present(cpu, false);
Yinghai Lua58f03b2008-08-14 02:16:30 -0700970 nr++;
971 }
972
973 nr = 0;
974 for_each_possible_cpu(cpu) {
975 if (nr >= 8)
Mike Travisc2d1cec2009-01-04 05:18:03 -0800976 set_cpu_possible(cpu, false);
Yinghai Lua58f03b2008-08-14 02:16:30 -0700977 nr++;
978 }
979
980 nr_cpu_ids = 8;
981 }
982#endif
983
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -0300984 if (!physid_isset(hard_smp_processor_id(), phys_cpu_present_map)) {
Michael Tokarev55c395b2008-12-05 14:42:20 +0300985 printk(KERN_WARNING
986 "weird, boot CPU (#%d) not listed by the BIOS.\n",
987 hard_smp_processor_id());
988
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -0300989 physid_set(hard_smp_processor_id(), phys_cpu_present_map);
990 }
991
992 /*
993 * If we couldn't find an SMP configuration at boot time,
994 * get out of here now!
995 */
996 if (!smp_found_config && !acpi_lapic) {
Jack Steinerac23d4e2008-03-28 14:12:16 -0500997 preempt_enable();
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -0300998 printk(KERN_NOTICE "SMP motherboard not detected.\n");
999 disable_smp();
1000 if (APIC_init_uniprocessor())
1001 printk(KERN_NOTICE "Local APIC not detected."
1002 " Using dummy APIC emulation.\n");
1003 return -1;
1004 }
1005
1006 /*
1007 * Should not be necessary because the MP table should list the boot
1008 * CPU too, but we do it for the sake of robustness anyway.
1009 */
Ingo Molnara27a6212009-01-28 12:43:18 +01001010 if (!apic->check_phys_apicid_present(boot_cpu_physical_apicid)) {
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001011 printk(KERN_NOTICE
1012 "weird, boot CPU (#%d) not listed by the BIOS.\n",
1013 boot_cpu_physical_apicid);
1014 physid_set(hard_smp_processor_id(), phys_cpu_present_map);
1015 }
Jack Steinerac23d4e2008-03-28 14:12:16 -05001016 preempt_enable();
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001017
1018 /*
1019 * If we couldn't find a local APIC, then get out of here now!
1020 */
1021 if (APIC_INTEGRATED(apic_version[boot_cpu_physical_apicid]) &&
1022 !cpu_has_apic) {
Cyrill Gorcunov103428e2009-06-07 16:48:40 +04001023 if (!disable_apic) {
1024 pr_err("BIOS bug, local APIC #%d not detected!...\n",
1025 boot_cpu_physical_apicid);
1026 pr_err("... forcing use of dummy APIC emulation."
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001027 "(tell your hw vendor)\n");
Cyrill Gorcunov103428e2009-06-07 16:48:40 +04001028 }
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001029 smpboot_clear_io_apic();
Ingo Molnar65a4e572009-01-31 03:36:17 +01001030 arch_disable_smp_support();
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001031 return -1;
1032 }
1033
1034 verify_local_APIC();
1035
1036 /*
1037 * If SMP should be disabled, then really disable it!
1038 */
1039 if (!max_cpus) {
Maciej W. Rozycki73d08e62008-05-21 22:09:43 +01001040 printk(KERN_INFO "SMP mode deactivated.\n");
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001041 smpboot_clear_io_apic();
Maciej W. Rozyckid54db1a2008-06-06 03:28:02 +01001042
Jesse Barnese90955c2008-04-21 14:14:44 -07001043 connect_bsp_APIC();
Jesse Barnese90955c2008-04-21 14:14:44 -07001044 setup_local_APIC();
1045 end_local_APIC_setup();
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001046 return -1;
1047 }
1048
1049 return 0;
1050}
1051
1052static void __init smp_cpu_index_default(void)
1053{
1054 int i;
1055 struct cpuinfo_x86 *c;
1056
Akinobu Mita7c04e642008-04-19 23:55:17 +09001057 for_each_possible_cpu(i) {
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001058 c = &cpu_data(i);
1059 /* mark all to hotplug */
Mike Travis96289372008-12-31 18:08:46 -08001060 c->cpu_index = nr_cpu_ids;
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001061 }
1062}
1063
1064/*
1065 * Prepare for SMP bootup. The MP table or ACPI has been read
1066 * earlier. Just do some sanity checking here and enable APIC mode.
1067 */
1068void __init native_smp_prepare_cpus(unsigned int max_cpus)
1069{
Rusty Russell7ad728f2009-03-13 14:49:50 +10301070 unsigned int i;
1071
Ingo Molnardeef3252008-05-12 15:44:38 +02001072 preempt_disable();
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001073 smp_cpu_index_default();
Tejun Heo7b543a52010-12-18 16:30:05 +01001074 memcpy(__this_cpu_ptr(&cpu_info), &boot_cpu_data, sizeof(cpu_info));
Mike Travisc2d1cec2009-01-04 05:18:03 -08001075 cpumask_copy(cpu_callin_mask, cpumask_of(0));
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001076 mb();
1077 /*
1078 * Setup boot CPU information
1079 */
1080 smp_store_cpu_info(0); /* Final full version of the data */
Suresh Siddha1b374e42008-07-10 11:16:49 -07001081#ifdef CONFIG_X86_32
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001082 boot_cpu_logical_apicid = logical_smp_processor_id();
Suresh Siddha1b374e42008-07-10 11:16:49 -07001083#endif
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001084 current_thread_info()->cpu = 0; /* needed? */
Rusty Russell7ad728f2009-03-13 14:49:50 +10301085 for_each_possible_cpu(i) {
Li Zefan79f55992009-06-15 14:58:26 +08001086 zalloc_cpumask_var(&per_cpu(cpu_sibling_map, i), GFP_KERNEL);
1087 zalloc_cpumask_var(&per_cpu(cpu_core_map, i), GFP_KERNEL);
Yinghai Lub3d73362011-01-21 15:29:44 -08001088 zalloc_cpumask_var(&per_cpu(cpu_llc_shared_map, i), GFP_KERNEL);
Rusty Russell7ad728f2009-03-13 14:49:50 +10301089 }
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001090 set_cpu_sibling_map(0);
1091
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001092
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001093 if (smp_sanity_check(max_cpus) < 0) {
1094 printk(KERN_INFO "SMP disabled\n");
1095 disable_smp();
Ingo Molnardeef3252008-05-12 15:44:38 +02001096 goto out;
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001097 }
1098
Suresh Siddhafa47f7e2010-08-27 11:09:50 -07001099 default_setup_apic_routing();
1100
Jack Steinerac23d4e2008-03-28 14:12:16 -05001101 preempt_disable();
Yinghai Lu4c9961d2008-07-11 18:44:16 -07001102 if (read_apic_id() != boot_cpu_physical_apicid) {
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001103 panic("Boot APIC ID in local APIC unexpected (%d vs %d)",
Yinghai Lu4c9961d2008-07-11 18:44:16 -07001104 read_apic_id(), boot_cpu_physical_apicid);
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001105 /* Or can we switch back to PIC here? */
1106 }
Jack Steinerac23d4e2008-03-28 14:12:16 -05001107 preempt_enable();
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001108
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001109 connect_bsp_APIC();
Glauber Costab5841762008-05-28 13:38:28 -03001110
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001111 /*
1112 * Switch from PIC to APIC mode.
1113 */
1114 setup_local_APIC();
1115
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001116 /*
1117 * Enable IO APIC before setting up error vector
1118 */
1119 if (!skip_ioapic_setup && nr_ioapics)
1120 enable_IO_APIC();
Yinghai Lu88d0f552009-02-14 23:57:28 -08001121
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001122 end_local_APIC_setup();
1123
1124 map_cpu_to_logical_apicid();
1125
Ingo Molnard83093b2009-01-28 12:43:18 +01001126 if (apic->setup_portio_remap)
1127 apic->setup_portio_remap();
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001128
1129 smpboot_setup_io_apic();
1130 /*
1131 * Set up local APIC timer on boot CPU.
1132 */
1133
1134 printk(KERN_INFO "CPU%d: ", 0);
1135 print_cpu_info(&cpu_data(0));
Thomas Gleixner736deca2009-08-19 12:35:53 +02001136 x86_init.timers.setup_percpu_clockev();
Marcin Slusarzc4bd1fd2008-08-21 20:49:05 +02001137
1138 if (is_uv_system())
1139 uv_system_init();
Suresh Siddhad0af9ee2009-08-19 18:05:36 -07001140
1141 set_mtrr_aps_delayed_init();
Ingo Molnardeef3252008-05-12 15:44:38 +02001142out:
1143 preempt_enable();
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001144}
Suresh Siddhad0af9ee2009-08-19 18:05:36 -07001145
Suresh Siddha3fb82d52010-11-23 16:11:40 -08001146void arch_disable_nonboot_cpus_begin(void)
1147{
1148 /*
1149 * Avoid the smp alternatives switch during the disable_nonboot_cpus().
1150 * In the suspend path, we will be back in the SMP mode shortly anyways.
1151 */
1152 skip_smp_alternatives = true;
1153}
1154
1155void arch_disable_nonboot_cpus_end(void)
1156{
1157 skip_smp_alternatives = false;
1158}
1159
Suresh Siddhad0af9ee2009-08-19 18:05:36 -07001160void arch_enable_nonboot_cpus_begin(void)
1161{
1162 set_mtrr_aps_delayed_init();
1163}
1164
1165void arch_enable_nonboot_cpus_end(void)
1166{
1167 mtrr_aps_init();
1168}
1169
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001170/*
Glauber de Oliveira Costaa8db8452008-03-19 14:26:01 -03001171 * Early setup to make printk work.
1172 */
1173void __init native_smp_prepare_boot_cpu(void)
1174{
1175 int me = smp_processor_id();
Brian Gerst552be872009-01-30 17:47:53 +09001176 switch_to_new_gdt(me);
Mike Travisc2d1cec2009-01-04 05:18:03 -08001177 /* already set me in cpu_online_mask in boot_cpu_init() */
1178 cpumask_set_cpu(me, cpu_callout_mask);
Glauber de Oliveira Costaa8db8452008-03-19 14:26:01 -03001179 per_cpu(cpu_state, me) = CPU_ONLINE;
1180}
1181
Glauber de Oliveira Costa83f7eb92008-03-19 14:26:02 -03001182void __init native_smp_cpus_done(unsigned int max_cpus)
1183{
Thomas Gleixnercfc1b9a2008-07-21 21:35:38 +02001184 pr_debug("Boot done.\n");
Glauber de Oliveira Costa83f7eb92008-03-19 14:26:02 -03001185
1186 impress_friends();
Glauber de Oliveira Costa83f7eb92008-03-19 14:26:02 -03001187#ifdef CONFIG_X86_IO_APIC
1188 setup_ioapic_dest();
1189#endif
Suresh Siddhad0af9ee2009-08-19 18:05:36 -07001190 mtrr_aps_init();
Glauber de Oliveira Costa83f7eb92008-03-19 14:26:02 -03001191}
1192
Mike Travis3b11ce72008-12-17 15:21:39 -08001193static int __initdata setup_possible_cpus = -1;
1194static int __init _setup_possible_cpus(char *str)
1195{
1196 get_option(&str, &setup_possible_cpus);
1197 return 0;
1198}
1199early_param("possible_cpus", _setup_possible_cpus);
1200
1201
Glauber Costa68a1c3f2008-03-03 14:12:42 -03001202/*
Rusty Russell4f062892009-03-13 14:49:54 +10301203 * cpu_possible_mask should be static, it cannot change as cpu's
Glauber Costa68a1c3f2008-03-03 14:12:42 -03001204 * are onlined, or offlined. The reason is per-cpu data-structures
1205 * are allocated by some modules at init time, and dont expect to
1206 * do this dynamically on cpu arrival/departure.
Rusty Russell4f062892009-03-13 14:49:54 +10301207 * cpu_present_mask on the other hand can change dynamically.
Glauber Costa68a1c3f2008-03-03 14:12:42 -03001208 * In case when cpu_hotplug is not compiled, then we resort to current
1209 * behaviour, which is cpu_possible == cpu_present.
1210 * - Ashok Raj
1211 *
1212 * Three ways to find out the number of additional hotplug CPUs:
1213 * - If the BIOS specified disabled CPUs in ACPI/mptables use that.
Mike Travis3b11ce72008-12-17 15:21:39 -08001214 * - The user can overwrite it with possible_cpus=NUM
Glauber Costa68a1c3f2008-03-03 14:12:42 -03001215 * - Otherwise don't reserve additional CPUs.
1216 * We do this because additional CPUs waste a lot of memory.
1217 * -AK
1218 */
1219__init void prefill_possible_map(void)
1220{
Thomas Gleixnercb48bb52008-10-05 17:51:52 +02001221 int i, possible;
Glauber Costa68a1c3f2008-03-03 14:12:42 -03001222
Yinghai Lu329513a2008-07-02 18:54:40 -07001223 /* no processor from mptable or madt */
1224 if (!num_processors)
1225 num_processors = 1;
1226
Jan Beulich5f2eb552010-05-24 12:13:17 -07001227 i = setup_max_cpus ?: 1;
1228 if (setup_possible_cpus == -1) {
1229 possible = num_processors;
1230#ifdef CONFIG_HOTPLUG_CPU
1231 if (setup_max_cpus)
1232 possible += disabled_cpus;
1233#else
1234 if (possible > i)
1235 possible = i;
1236#endif
1237 } else
Mike Travis3b11ce72008-12-17 15:21:39 -08001238 possible = setup_possible_cpus;
1239
Mike Travis730cf272008-12-31 18:08:45 -08001240 total_cpus = max_t(int, possible, num_processors + disabled_cpus);
1241
Yinghai Lu2b633e32010-02-10 01:20:37 -08001242 /* nr_cpu_ids could be reduced via nr_cpus= */
1243 if (possible > nr_cpu_ids) {
Mike Travis3b11ce72008-12-17 15:21:39 -08001244 printk(KERN_WARNING
1245 "%d Processors exceeds NR_CPUS limit of %d\n",
Yinghai Lu2b633e32010-02-10 01:20:37 -08001246 possible, nr_cpu_ids);
1247 possible = nr_cpu_ids;
Mike Travis3b11ce72008-12-17 15:21:39 -08001248 }
Glauber Costa68a1c3f2008-03-03 14:12:42 -03001249
Jan Beulich5f2eb552010-05-24 12:13:17 -07001250#ifdef CONFIG_HOTPLUG_CPU
1251 if (!setup_max_cpus)
1252#endif
1253 if (possible > i) {
1254 printk(KERN_WARNING
1255 "%d Processors exceeds max_cpus limit of %u\n",
1256 possible, setup_max_cpus);
1257 possible = i;
1258 }
1259
Glauber Costa68a1c3f2008-03-03 14:12:42 -03001260 printk(KERN_INFO "SMP: Allowing %d CPUs, %d hotplug CPUs\n",
1261 possible, max_t(int, possible - num_processors, 0));
1262
1263 for (i = 0; i < possible; i++)
Mike Travisc2d1cec2009-01-04 05:18:03 -08001264 set_cpu_possible(i, true);
Jan Beulich5f2eb552010-05-24 12:13:17 -07001265 for (; i < NR_CPUS; i++)
1266 set_cpu_possible(i, false);
Mike Travis3461b0a2008-05-12 21:21:13 +02001267
1268 nr_cpu_ids = possible;
Glauber Costa68a1c3f2008-03-03 14:12:42 -03001269}
Glauber Costa69c18c12008-03-03 14:13:07 -03001270
Chuck Ebbert14adf852008-09-29 18:29:42 -04001271#ifdef CONFIG_HOTPLUG_CPU
1272
1273static void remove_siblinginfo(int cpu)
1274{
1275 int sibling;
1276 struct cpuinfo_x86 *c = &cpu_data(cpu);
1277
Mike Travisc2d1cec2009-01-04 05:18:03 -08001278 for_each_cpu(sibling, cpu_core_mask(cpu)) {
1279 cpumask_clear_cpu(cpu, cpu_core_mask(sibling));
Chuck Ebbert14adf852008-09-29 18:29:42 -04001280 /*/
1281 * last thread sibling in this cpu core going down
1282 */
Mike Travisc2d1cec2009-01-04 05:18:03 -08001283 if (cpumask_weight(cpu_sibling_mask(cpu)) == 1)
Chuck Ebbert14adf852008-09-29 18:29:42 -04001284 cpu_data(sibling).booted_cores--;
1285 }
1286
Mike Travisc2d1cec2009-01-04 05:18:03 -08001287 for_each_cpu(sibling, cpu_sibling_mask(cpu))
1288 cpumask_clear_cpu(cpu, cpu_sibling_mask(sibling));
1289 cpumask_clear(cpu_sibling_mask(cpu));
1290 cpumask_clear(cpu_core_mask(cpu));
Chuck Ebbert14adf852008-09-29 18:29:42 -04001291 c->phys_proc_id = 0;
1292 c->cpu_core_id = 0;
Mike Travisc2d1cec2009-01-04 05:18:03 -08001293 cpumask_clear_cpu(cpu, cpu_sibling_setup_mask);
Chuck Ebbert14adf852008-09-29 18:29:42 -04001294}
1295
Glauber Costa69c18c12008-03-03 14:13:07 -03001296static void __ref remove_cpu_from_maps(int cpu)
1297{
Mike Travisc2d1cec2009-01-04 05:18:03 -08001298 set_cpu_online(cpu, false);
1299 cpumask_clear_cpu(cpu, cpu_callout_mask);
1300 cpumask_clear_cpu(cpu, cpu_callin_mask);
Glauber Costa69c18c12008-03-03 14:13:07 -03001301 /* was set by cpu_init() */
Mike Travisc2d1cec2009-01-04 05:18:03 -08001302 cpumask_clear_cpu(cpu, cpu_initialized_mask);
Mike Travis23ca4bb2008-05-12 21:21:12 +02001303 numa_remove_cpu(cpu);
Glauber Costa69c18c12008-03-03 14:13:07 -03001304}
1305
Alex Nixon8227dce2008-08-22 11:52:14 +01001306void cpu_disable_common(void)
1307{
1308 int cpu = smp_processor_id();
Alex Nixon8227dce2008-08-22 11:52:14 +01001309
Alex Nixon8227dce2008-08-22 11:52:14 +01001310 remove_siblinginfo(cpu);
1311
1312 /* It's now safe to remove this processor from the online map */
1313 lock_vector_lock();
1314 remove_cpu_from_maps(cpu);
1315 unlock_vector_lock();
Mike Travisd7b381b2008-12-16 17:33:58 -08001316 fixup_irqs();
Alex Nixon8227dce2008-08-22 11:52:14 +01001317}
1318
Alex Nixon93be71b2008-08-22 11:52:11 +01001319int native_cpu_disable(void)
Glauber Costa69c18c12008-03-03 14:13:07 -03001320{
1321 int cpu = smp_processor_id();
1322
1323 /*
1324 * Perhaps use cpufreq to drop frequency, but that could go
1325 * into generic code.
1326 *
1327 * We won't take down the boot processor on i386 due to some
1328 * interrupts only being able to be serviced by the BSP.
1329 * Especially so if we're not using an IOAPIC -zwane
1330 */
1331 if (cpu == 0)
1332 return -EBUSY;
1333
Glauber Costa69c18c12008-03-03 14:13:07 -03001334 clear_local_APIC();
1335
Alex Nixon8227dce2008-08-22 11:52:14 +01001336 cpu_disable_common();
Glauber Costa69c18c12008-03-03 14:13:07 -03001337 return 0;
1338}
1339
Alex Nixon93be71b2008-08-22 11:52:11 +01001340void native_cpu_die(unsigned int cpu)
Glauber Costa69c18c12008-03-03 14:13:07 -03001341{
1342 /* We don't do anything here: idle task is faking death itself. */
1343 unsigned int i;
1344
1345 for (i = 0; i < 10; i++) {
1346 /* They ack this in play_dead by setting CPU_DEAD */
1347 if (per_cpu(cpu_state, cpu) == CPU_DEAD) {
Mike Travis2eaad1f2009-12-10 17:19:36 -08001348 if (system_state == SYSTEM_RUNNING)
1349 pr_info("CPU %u is now offline\n", cpu);
1350
Glauber Costa69c18c12008-03-03 14:13:07 -03001351 if (1 == num_online_cpus())
1352 alternatives_smp_switch(0);
1353 return;
1354 }
1355 msleep(100);
1356 }
Mike Travis2eaad1f2009-12-10 17:19:36 -08001357 pr_err("CPU %u didn't die...\n", cpu);
Glauber Costa69c18c12008-03-03 14:13:07 -03001358}
Alex Nixona21f5d82008-08-22 11:52:13 +01001359
1360void play_dead_common(void)
1361{
1362 idle_task_exit();
1363 reset_lazy_tlbstate();
Ingo Molnar07bbc162008-09-23 23:26:42 +02001364 c1e_remove_cpu(raw_smp_processor_id());
Alex Nixona21f5d82008-08-22 11:52:13 +01001365
1366 mb();
1367 /* Ack it */
Tejun Heo0a3aee02010-12-18 16:28:55 +01001368 __this_cpu_write(cpu_state, CPU_DEAD);
Alex Nixona21f5d82008-08-22 11:52:13 +01001369
1370 /*
1371 * With physical CPU hotplug, we should halt the cpu
1372 */
1373 local_irq_disable();
1374}
1375
H. Peter Anvinea530692010-09-17 15:39:11 -07001376/*
1377 * We need to flush the caches before going to sleep, lest we have
1378 * dirty data in our caches when we come back up.
1379 */
1380static inline void mwait_play_dead(void)
1381{
1382 unsigned int eax, ebx, ecx, edx;
1383 unsigned int highest_cstate = 0;
1384 unsigned int highest_subcstate = 0;
1385 int i;
H. Peter Anvince5f6822010-09-20 13:04:45 -07001386 void *mwait_ptr;
Borislav Petkov93789b32011-01-20 15:42:52 +01001387 struct cpuinfo_x86 *c = __this_cpu_ptr(&cpu_info);
H. Peter Anvinea530692010-09-17 15:39:11 -07001388
Borislav Petkov93789b32011-01-20 15:42:52 +01001389 if (!(cpu_has(c, X86_FEATURE_MWAIT) && mwait_usable(c)))
H. Peter Anvinea530692010-09-17 15:39:11 -07001390 return;
Tejun Heo7b543a52010-12-18 16:30:05 +01001391 if (!cpu_has(__this_cpu_ptr(&cpu_info), X86_FEATURE_CLFLSH))
H. Peter Anvince5f6822010-09-20 13:04:45 -07001392 return;
Tejun Heo7b543a52010-12-18 16:30:05 +01001393 if (__this_cpu_read(cpu_info.cpuid_level) < CPUID_MWAIT_LEAF)
H. Peter Anvinea530692010-09-17 15:39:11 -07001394 return;
1395
1396 eax = CPUID_MWAIT_LEAF;
1397 ecx = 0;
1398 native_cpuid(&eax, &ebx, &ecx, &edx);
1399
1400 /*
1401 * eax will be 0 if EDX enumeration is not valid.
1402 * Initialized below to cstate, sub_cstate value when EDX is valid.
1403 */
1404 if (!(ecx & CPUID5_ECX_EXTENSIONS_SUPPORTED)) {
1405 eax = 0;
1406 } else {
1407 edx >>= MWAIT_SUBSTATE_SIZE;
1408 for (i = 0; i < 7 && edx; i++, edx >>= MWAIT_SUBSTATE_SIZE) {
1409 if (edx & MWAIT_SUBSTATE_MASK) {
1410 highest_cstate = i;
1411 highest_subcstate = edx & MWAIT_SUBSTATE_MASK;
1412 }
1413 }
1414 eax = (highest_cstate << MWAIT_SUBSTATE_SIZE) |
1415 (highest_subcstate - 1);
1416 }
1417
H. Peter Anvince5f6822010-09-20 13:04:45 -07001418 /*
1419 * This should be a memory location in a cache line which is
1420 * unlikely to be touched by other processors. The actual
1421 * content is immaterial as it is not actually modified in any way.
1422 */
1423 mwait_ptr = &current_thread_info()->flags;
1424
H. Peter Anvina68e5c92010-09-17 17:06:46 -07001425 wbinvd();
1426
H. Peter Anvinea530692010-09-17 15:39:11 -07001427 while (1) {
H. Peter Anvince5f6822010-09-20 13:04:45 -07001428 /*
1429 * The CLFLUSH is a workaround for erratum AAI65 for
1430 * the Xeon 7400 series. It's not clear it is actually
1431 * needed, but it should be harmless in either case.
1432 * The WBINVD is insufficient due to the spurious-wakeup
1433 * case where we return around the loop.
1434 */
1435 clflush(mwait_ptr);
1436 __monitor(mwait_ptr, 0, 0);
H. Peter Anvinea530692010-09-17 15:39:11 -07001437 mb();
1438 __mwait(eax, 0);
1439 }
1440}
1441
1442static inline void hlt_play_dead(void)
1443{
Tejun Heo7b543a52010-12-18 16:30:05 +01001444 if (__this_cpu_read(cpu_info.x86) >= 4)
H. Peter Anvina68e5c92010-09-17 17:06:46 -07001445 wbinvd();
1446
H. Peter Anvinea530692010-09-17 15:39:11 -07001447 while (1) {
H. Peter Anvinea530692010-09-17 15:39:11 -07001448 native_halt();
1449 }
1450}
1451
Alex Nixona21f5d82008-08-22 11:52:13 +01001452void native_play_dead(void)
1453{
1454 play_dead_common();
Joseph Cihula86886e52009-06-30 19:31:07 -07001455 tboot_shutdown(TB_SHUTDOWN_WFS);
H. Peter Anvinea530692010-09-17 15:39:11 -07001456
1457 mwait_play_dead(); /* Only returns on failure */
1458 hlt_play_dead();
Alex Nixona21f5d82008-08-22 11:52:13 +01001459}
1460
Glauber Costa69c18c12008-03-03 14:13:07 -03001461#else /* ... !CONFIG_HOTPLUG_CPU */
Alex Nixon93be71b2008-08-22 11:52:11 +01001462int native_cpu_disable(void)
Glauber Costa69c18c12008-03-03 14:13:07 -03001463{
1464 return -ENOSYS;
1465}
1466
Alex Nixon93be71b2008-08-22 11:52:11 +01001467void native_cpu_die(unsigned int cpu)
Glauber Costa69c18c12008-03-03 14:13:07 -03001468{
1469 /* We said "no" in __cpu_disable */
1470 BUG();
1471}
Alex Nixona21f5d82008-08-22 11:52:13 +01001472
1473void native_play_dead(void)
1474{
1475 BUG();
1476}
1477
Glauber Costa68a1c3f2008-03-03 14:12:42 -03001478#endif