blob: 5319cdd537651cedec911dc74935bf2832220753 [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
Tejun Heob78aa662011-01-23 14:37:28 +010075u8 apicid_2_node[MAX_LOCAL_APIC];
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
133/* Per CPU bogomips and other parameters */
134DEFINE_PER_CPU_SHARED_ALIGNED(struct cpuinfo_x86, cpu_info);
135EXPORT_PER_CPU_SYMBOL(cpu_info);
Glauber Costa768d9502008-03-03 14:13:02 -0300136
Yinghai Lu2b6163b2009-02-25 20:50:49 -0800137atomic_t init_deasserted;
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300138
Glauber de Oliveira Costa7cc39592008-03-19 14:25:56 -0300139#if defined(CONFIG_NUMA) && defined(CONFIG_X86_32)
Glauber de Oliveira Costa7cc39592008-03-19 14:25:56 -0300140/* which node each logical CPU is on */
141int cpu_to_node_map[NR_CPUS] __read_mostly = { [0 ... NR_CPUS-1] = 0 };
142EXPORT_SYMBOL(cpu_to_node_map);
143
144/* set up a mapping between cpu and node. */
145static void map_cpu_to_node(int cpu, int node)
146{
147 printk(KERN_INFO "Mapping cpu %d to node %d\n", cpu, node);
Rusty Russellc032ef602009-03-13 14:49:53 +1030148 cpumask_set_cpu(cpu, node_to_cpumask_map[node]);
Glauber de Oliveira Costa7cc39592008-03-19 14:25:56 -0300149 cpu_to_node_map[cpu] = node;
150}
151
152/* undo a mapping between cpu and node. */
153static void unmap_cpu_to_node(int cpu)
154{
155 int node;
156
157 printk(KERN_INFO "Unmapping cpu %d from all nodes\n", cpu);
158 for (node = 0; node < MAX_NUMNODES; node++)
Rusty Russellc032ef602009-03-13 14:49:53 +1030159 cpumask_clear_cpu(cpu, node_to_cpumask_map[node]);
Glauber de Oliveira Costa7cc39592008-03-19 14:25:56 -0300160 cpu_to_node_map[cpu] = 0;
161}
162#else /* !(CONFIG_NUMA && CONFIG_X86_32) */
163#define map_cpu_to_node(cpu, node) ({})
164#define unmap_cpu_to_node(cpu) ({})
165#endif
166
167#ifdef CONFIG_X86_32
Ingo Molnara4928cf2008-04-23 13:20:56 +0200168static void map_cpu_to_logical_apicid(void)
Glauber de Oliveira Costa7cc39592008-03-19 14:25:56 -0300169{
170 int cpu = smp_processor_id();
Tejun Heo6f802c42011-01-23 14:37:31 +0100171 int node;
Glauber de Oliveira Costa7cc39592008-03-19 14:25:56 -0300172
Tejun Heo89e5dc22011-01-23 14:37:38 +0100173 node = apic->x86_32_numa_cpu_node(cpu);
Glauber de Oliveira Costa7cc39592008-03-19 14:25:56 -0300174 if (!node_online(node))
175 node = first_online_node;
176
Glauber de Oliveira Costa7cc39592008-03-19 14:25:56 -0300177 map_cpu_to_node(cpu, node);
178}
179
Glauber Costa1481a3d2008-06-04 15:35:03 -0300180void numa_remove_cpu(int cpu)
Glauber de Oliveira Costa7cc39592008-03-19 14:25:56 -0300181{
Glauber de Oliveira Costa7cc39592008-03-19 14:25:56 -0300182 unmap_cpu_to_node(cpu);
183}
184#else
Glauber de Oliveira Costa7cc39592008-03-19 14:25:56 -0300185#define map_cpu_to_logical_apicid() do {} while (0)
186#endif
187
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300188/*
189 * Report back to the Boot Processor.
190 * Running on AP.
191 */
Ingo Molnara4928cf2008-04-23 13:20:56 +0200192static void __cpuinit smp_callin(void)
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300193{
194 int cpuid, phys_id;
195 unsigned long timeout;
196
197 /*
198 * If waken up by an INIT in an 82489DX configuration
199 * we may get here before an INIT-deassert IPI reaches
200 * our local APIC. We have to wait for the IPI or we'll
201 * lock up on an APIC access.
202 */
Ingo Molnara9659362009-01-28 16:21:32 +0100203 if (apic->wait_for_init_deassert)
204 apic->wait_for_init_deassert(&init_deasserted);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300205
206 /*
207 * (This works even if the APIC is not enabled.)
208 */
Yinghai Lu4c9961d2008-07-11 18:44:16 -0700209 phys_id = read_apic_id();
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300210 cpuid = smp_processor_id();
Mike Travisc2d1cec2009-01-04 05:18:03 -0800211 if (cpumask_test_cpu(cpuid, cpu_callin_mask)) {
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300212 panic("%s: phys CPU#%d, CPU#%d already present??\n", __func__,
213 phys_id, cpuid);
214 }
Thomas Gleixnercfc1b9a2008-07-21 21:35:38 +0200215 pr_debug("CPU#%d (phys ID: %d) waiting for CALLOUT\n", cpuid, phys_id);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300216
217 /*
218 * STARTUP IPIs are fragile beasts as they might sometimes
219 * trigger some glue motherboard logic. Complete APIC bus
220 * silence for 1 second, this overestimates the time the
221 * boot CPU is spending to send the up to 2 STARTUP IPIs
222 * by a factor of two. This should be enough.
223 */
224
225 /*
226 * Waiting 2s total for startup (udelay is not yet working)
227 */
228 timeout = jiffies + 2*HZ;
229 while (time_before(jiffies, timeout)) {
230 /*
231 * Has the boot CPU finished it's STARTUP sequence?
232 */
Mike Travisc2d1cec2009-01-04 05:18:03 -0800233 if (cpumask_test_cpu(cpuid, cpu_callout_mask))
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300234 break;
235 cpu_relax();
236 }
237
238 if (!time_before(jiffies, timeout)) {
239 panic("%s: CPU%d started up but did not get a callout!\n",
240 __func__, cpuid);
241 }
242
243 /*
244 * the boot CPU has finished the init stage and is spinning
245 * on callin_map until we finish. We are free to set up this
246 * CPU, first the APIC. (this is probably redundant on most
247 * boards)
248 */
249
Thomas Gleixnercfc1b9a2008-07-21 21:35:38 +0200250 pr_debug("CALLIN, before setup_local_APIC().\n");
Ingo Molnar333344d2009-01-28 16:31:52 +0100251 if (apic->smp_callin_clear_local_apic)
252 apic->smp_callin_clear_local_apic();
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300253 setup_local_APIC();
254 end_local_APIC_setup();
255 map_cpu_to_logical_apicid();
256
Suresh Siddha9d133e52010-01-29 11:42:21 -0800257 /*
258 * Need to setup vector mappings before we enable interrupts.
259 */
Suresh Siddha36e9e1e2010-03-15 14:33:06 -0800260 setup_vector_irq(smp_processor_id());
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300261 /*
262 * Get our bogomips.
263 *
264 * Need to enable IRQs because it can take longer and then
265 * the NMI watchdog might kill us.
266 */
267 local_irq_enable();
268 calibrate_delay();
269 local_irq_disable();
Thomas Gleixnercfc1b9a2008-07-21 21:35:38 +0200270 pr_debug("Stack at about %p\n", &cpuid);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300271
272 /*
273 * Save our processor parameters
274 */
275 smp_store_cpu_info(cpuid);
276
Andi Kleen5ef428c2010-11-18 11:47:31 +0100277 /*
278 * This must be done before setting cpu_online_mask
279 * or calling notify_cpu_starting.
280 */
281 set_cpu_sibling_map(raw_smp_processor_id());
282 wmb();
283
Peter Zijlstra85257022010-03-23 19:30:52 +0100284 notify_cpu_starting(cpuid);
285
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300286 /*
287 * Allow the master to continue.
288 */
Mike Travisc2d1cec2009-01-04 05:18:03 -0800289 cpumask_set_cpu(cpuid, cpu_callin_mask);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300290}
291
Glauber de Oliveira Costabbc2ff62008-03-19 14:26:00 -0300292/*
293 * Activate a secondary processor.
294 */
Frederic Weisbecker0ca59dd2008-12-24 23:30:02 +0100295notrace static void __cpuinit start_secondary(void *unused)
Glauber de Oliveira Costabbc2ff62008-03-19 14:26:00 -0300296{
297 /*
298 * Don't put *anything* before cpu_init(), SMP booting is too
299 * fragile that we want to limit the things done here to the
300 * most necessary things.
301 */
Glauber de Oliveira Costabbc2ff62008-03-19 14:26:00 -0300302 cpu_init();
303 preempt_disable();
304 smp_callin();
305
Borislav Petkovb40827f2010-08-28 15:58:33 +0200306#ifdef CONFIG_X86_32
307 /* switch away from the initial page table */
308 load_cr3(swapper_pg_dir);
309 __flush_tlb_all();
310#endif
311
Glauber de Oliveira Costabbc2ff62008-03-19 14:26:00 -0300312 /* otherwise gcc will move up smp_processor_id before the cpu_init */
313 barrier();
314 /*
315 * Check TSC synchronization with the BP:
316 */
317 check_tsc_sync_target();
318
Glauber de Oliveira Costabbc2ff62008-03-19 14:26:00 -0300319 /*
320 * We need to hold call_lock, so there is no inconsistency
321 * between the time smp_call_function() determines number of
322 * IPI recipients, and the time when the determination is made
323 * for which cpus receive the IPI. Holding this
324 * lock helps us to not include this cpu in a currently in progress
325 * smp_call_function().
Eric W. Biedermand388e5f2008-08-09 15:09:02 -0700326 *
327 * We need to hold vector_lock so there the set of online cpus
328 * does not change while we are assigning vectors to cpus. Holding
329 * this lock ensures we don't half assign or remove an irq from a cpu.
Glauber de Oliveira Costabbc2ff62008-03-19 14:26:00 -0300330 */
Manfred Spraul0cefa5b2008-09-07 11:29:58 +0200331 ipi_call_lock();
Eric W. Biedermand388e5f2008-08-09 15:09:02 -0700332 lock_vector_lock();
Mike Travisc2d1cec2009-01-04 05:18:03 -0800333 set_cpu_online(smp_processor_id(), true);
Eric W. Biedermand388e5f2008-08-09 15:09:02 -0700334 unlock_vector_lock();
Manfred Spraul0cefa5b2008-09-07 11:29:58 +0200335 ipi_call_unlock();
Glauber de Oliveira Costabbc2ff62008-03-19 14:26:00 -0300336 per_cpu(cpu_state, smp_processor_id()) = CPU_ONLINE;
Russ Anderson78c06172010-02-26 10:49:12 -0600337 x86_platform.nmi_init();
Glauber de Oliveira Costabbc2ff62008-03-19 14:26:00 -0300338
Manfred Spraul0cefa5b2008-09-07 11:29:58 +0200339 /* enable local interrupts */
340 local_irq_enable();
341
Jacob Pan35f720c2009-09-17 07:36:43 -0700342 /* to prevent fake stack check failure in clock setup */
343 boot_init_stack_canary();
344
Thomas Gleixner736deca2009-08-19 12:35:53 +0200345 x86_cpuinit.setup_percpu_clockev();
Glauber de Oliveira Costabbc2ff62008-03-19 14:26:00 -0300346
347 wmb();
348 cpu_idle();
349}
350
Rusty Russell155dd722009-03-13 14:49:53 +1030351#ifdef CONFIG_CPUMASK_OFFSTACK
352/* In this case, llc_shared_map is a pointer to a cpumask. */
353static inline void copy_cpuinfo_x86(struct cpuinfo_x86 *dst,
354 const struct cpuinfo_x86 *src)
355{
356 struct cpumask *llc = dst->llc_shared_map;
357 *dst = *src;
358 dst->llc_shared_map = llc;
359}
360#else
361static inline void copy_cpuinfo_x86(struct cpuinfo_x86 *dst,
362 const struct cpuinfo_x86 *src)
363{
364 *dst = *src;
365}
366#endif /* CONFIG_CPUMASK_OFFSTACK */
367
Glauber de Oliveira Costa1d89a7f2008-03-19 14:25:05 -0300368/*
369 * The bootstrap kernel entry code has set these up. Save them for
370 * a given CPU
371 */
372
373void __cpuinit smp_store_cpu_info(int id)
374{
375 struct cpuinfo_x86 *c = &cpu_data(id);
376
Rusty Russell155dd722009-03-13 14:49:53 +1030377 copy_cpuinfo_x86(c, &boot_cpu_data);
Glauber de Oliveira Costa1d89a7f2008-03-19 14:25:05 -0300378 c->cpu_index = id;
379 if (id != 0)
380 identify_secondary_cpu(c);
Glauber de Oliveira Costa1d89a7f2008-03-19 14:25:05 -0300381}
382
Andreas Herrmannd4fbe4f2010-09-30 14:41:56 +0200383static void __cpuinit link_thread_siblings(int cpu1, int cpu2)
384{
385 struct cpuinfo_x86 *c1 = &cpu_data(cpu1);
386 struct cpuinfo_x86 *c2 = &cpu_data(cpu2);
387
388 cpumask_set_cpu(cpu1, cpu_sibling_mask(cpu2));
389 cpumask_set_cpu(cpu2, cpu_sibling_mask(cpu1));
390 cpumask_set_cpu(cpu1, cpu_core_mask(cpu2));
391 cpumask_set_cpu(cpu2, cpu_core_mask(cpu1));
392 cpumask_set_cpu(cpu1, c2->llc_shared_map);
393 cpumask_set_cpu(cpu2, c1->llc_shared_map);
394}
395
Glauber de Oliveira Costa1d89a7f2008-03-19 14:25:05 -0300396
Glauber Costa768d9502008-03-03 14:13:02 -0300397void __cpuinit set_cpu_sibling_map(int cpu)
398{
399 int i;
400 struct cpuinfo_x86 *c = &cpu_data(cpu);
401
Mike Travisc2d1cec2009-01-04 05:18:03 -0800402 cpumask_set_cpu(cpu, cpu_sibling_setup_mask);
Glauber Costa768d9502008-03-03 14:13:02 -0300403
404 if (smp_num_siblings > 1) {
Mike Travisc2d1cec2009-01-04 05:18:03 -0800405 for_each_cpu(i, cpu_sibling_setup_mask) {
406 struct cpuinfo_x86 *o = &cpu_data(i);
407
Andreas Herrmannd4fbe4f2010-09-30 14:41:56 +0200408 if (cpu_has(c, X86_FEATURE_TOPOEXT)) {
409 if (c->phys_proc_id == o->phys_proc_id &&
410 c->compute_unit_id == o->compute_unit_id)
411 link_thread_siblings(cpu, i);
412 } else if (c->phys_proc_id == o->phys_proc_id &&
413 c->cpu_core_id == o->cpu_core_id) {
414 link_thread_siblings(cpu, i);
Glauber Costa768d9502008-03-03 14:13:02 -0300415 }
416 }
417 } else {
Mike Travisc2d1cec2009-01-04 05:18:03 -0800418 cpumask_set_cpu(cpu, cpu_sibling_mask(cpu));
Glauber Costa768d9502008-03-03 14:13:02 -0300419 }
420
Rusty Russell155dd722009-03-13 14:49:53 +1030421 cpumask_set_cpu(cpu, c->llc_shared_map);
Glauber Costa768d9502008-03-03 14:13:02 -0300422
Tejun Heo7b543a52010-12-18 16:30:05 +0100423 if (__this_cpu_read(cpu_info.x86_max_cores) == 1) {
Mike Travisc2d1cec2009-01-04 05:18:03 -0800424 cpumask_copy(cpu_core_mask(cpu), cpu_sibling_mask(cpu));
Glauber Costa768d9502008-03-03 14:13:02 -0300425 c->booted_cores = 1;
426 return;
427 }
428
Mike Travisc2d1cec2009-01-04 05:18:03 -0800429 for_each_cpu(i, cpu_sibling_setup_mask) {
Glauber Costa768d9502008-03-03 14:13:02 -0300430 if (per_cpu(cpu_llc_id, cpu) != BAD_APICID &&
431 per_cpu(cpu_llc_id, cpu) == per_cpu(cpu_llc_id, i)) {
Rusty Russell155dd722009-03-13 14:49:53 +1030432 cpumask_set_cpu(i, c->llc_shared_map);
433 cpumask_set_cpu(cpu, cpu_data(i).llc_shared_map);
Glauber Costa768d9502008-03-03 14:13:02 -0300434 }
435 if (c->phys_proc_id == cpu_data(i).phys_proc_id) {
Mike Travisc2d1cec2009-01-04 05:18:03 -0800436 cpumask_set_cpu(i, cpu_core_mask(cpu));
437 cpumask_set_cpu(cpu, cpu_core_mask(i));
Glauber Costa768d9502008-03-03 14:13:02 -0300438 /*
439 * Does this new cpu bringup a new core?
440 */
Mike Travisc2d1cec2009-01-04 05:18:03 -0800441 if (cpumask_weight(cpu_sibling_mask(cpu)) == 1) {
Glauber Costa768d9502008-03-03 14:13:02 -0300442 /*
443 * for each core in package, increment
444 * the booted_cores for this new cpu
445 */
Mike Travisc2d1cec2009-01-04 05:18:03 -0800446 if (cpumask_first(cpu_sibling_mask(i)) == i)
Glauber Costa768d9502008-03-03 14:13:02 -0300447 c->booted_cores++;
448 /*
449 * increment the core count for all
450 * the other cpus in this package
451 */
452 if (i != cpu)
453 cpu_data(i).booted_cores++;
454 } else if (i != cpu && !c->booted_cores)
455 c->booted_cores = cpu_data(i).booted_cores;
456 }
457 }
458}
459
Glauber Costa70708a12008-03-03 14:13:03 -0300460/* maps the cpu to the sched domain representing multi-core */
Rusty Russell030bb202008-12-26 22:23:41 +1030461const struct cpumask *cpu_coregroup_mask(int cpu)
Glauber Costa70708a12008-03-03 14:13:03 -0300462{
463 struct cpuinfo_x86 *c = &cpu_data(cpu);
464 /*
465 * For perf, we return last level cache shared map.
466 * And for power savings, we return cpu_core_map
467 */
Andreas Herrmann5a925b42009-09-03 09:44:28 +0200468 if ((sched_mc_power_savings || sched_smt_power_savings) &&
469 !(cpu_has(c, X86_FEATURE_AMD_DCM)))
Mike Travisc2d1cec2009-01-04 05:18:03 -0800470 return cpu_core_mask(cpu);
Glauber Costa70708a12008-03-03 14:13:03 -0300471 else
Rusty Russell155dd722009-03-13 14:49:53 +1030472 return c->llc_shared_map;
Rusty Russell030bb202008-12-26 22:23:41 +1030473}
474
Ingo Molnara4928cf2008-04-23 13:20:56 +0200475static void impress_friends(void)
Glauber de Oliveira Costa904541e2008-03-19 14:25:27 -0300476{
477 int cpu;
478 unsigned long bogosum = 0;
479 /*
480 * Allow the user to impress friends.
481 */
Thomas Gleixnercfc1b9a2008-07-21 21:35:38 +0200482 pr_debug("Before bogomips.\n");
Glauber de Oliveira Costa904541e2008-03-19 14:25:27 -0300483 for_each_possible_cpu(cpu)
Mike Travisc2d1cec2009-01-04 05:18:03 -0800484 if (cpumask_test_cpu(cpu, cpu_callout_mask))
Glauber de Oliveira Costa904541e2008-03-19 14:25:27 -0300485 bogosum += cpu_data(cpu).loops_per_jiffy;
486 printk(KERN_INFO
487 "Total of %d processors activated (%lu.%02lu BogoMIPS).\n",
Glauber de Oliveira Costaf68e00a2008-03-19 14:25:29 -0300488 num_online_cpus(),
Glauber de Oliveira Costa904541e2008-03-19 14:25:27 -0300489 bogosum/(500000/HZ),
490 (bogosum/(5000/HZ))%100);
491
Thomas Gleixnercfc1b9a2008-07-21 21:35:38 +0200492 pr_debug("Before bogocount - setting activated=1.\n");
Glauber de Oliveira Costa904541e2008-03-19 14:25:27 -0300493}
494
Yinghai Lu569712b2008-11-16 03:12:49 -0800495void __inquire_remote_apic(int apicid)
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300496{
497 unsigned i, regs[] = { APIC_ID >> 4, APIC_LVR >> 4, APIC_SPIV >> 4 };
498 char *names[] = { "ID", "VERSION", "SPIV" };
499 int timeout;
500 u32 status;
501
Yinghai Lu823b2592008-09-10 21:56:46 -0700502 printk(KERN_INFO "Inquiring remote APIC 0x%x...\n", apicid);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300503
504 for (i = 0; i < ARRAY_SIZE(regs); i++) {
Yinghai Lu823b2592008-09-10 21:56:46 -0700505 printk(KERN_INFO "... APIC 0x%x %s: ", apicid, names[i]);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300506
507 /*
508 * Wait for idle.
509 */
510 status = safe_apic_wait_icr_idle();
511 if (status)
512 printk(KERN_CONT
513 "a previous APIC delivery may have failed\n");
514
Suresh Siddha1b374e42008-07-10 11:16:49 -0700515 apic_icr_write(APIC_DM_REMRD | regs[i], apicid);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300516
517 timeout = 0;
518 do {
519 udelay(100);
520 status = apic_read(APIC_ICR) & APIC_ICR_RR_MASK;
521 } while (status == APIC_ICR_RR_INPROG && timeout++ < 1000);
522
523 switch (status) {
524 case APIC_ICR_RR_VALID:
525 status = apic_read(APIC_RRR);
526 printk(KERN_CONT "%08x\n", status);
527 break;
528 default:
529 printk(KERN_CONT "failed\n");
530 }
531 }
532}
533
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300534/*
535 * Poke the other CPU in the eye via NMI to wake it up. Remember that the normal
536 * INIT, INIT, STARTUP sequence will reset the chip hard for us, and this
537 * won't ... remember to clear down the APIC, etc later.
538 */
Cyrill Gorcunovcece3152009-04-18 23:45:28 +0400539int __cpuinit
Yinghai Lu569712b2008-11-16 03:12:49 -0800540wakeup_secondary_cpu_via_nmi(int logical_apicid, unsigned long start_eip)
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300541{
542 unsigned long send_status, accept_status = 0;
543 int maxlvt;
544
545 /* Target chip */
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300546 /* Boot on the stack */
547 /* Kick the second */
Ingo Molnarbdb1a9b2009-01-28 05:29:25 +0100548 apic_icr_write(APIC_DM_NMI | apic->dest_logical, logical_apicid);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300549
Thomas Gleixnercfc1b9a2008-07-21 21:35:38 +0200550 pr_debug("Waiting for send to finish...\n");
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300551 send_status = safe_apic_wait_icr_idle();
552
553 /*
554 * Give the other CPU some time to accept the IPI.
555 */
556 udelay(200);
Yinghai Lu569712b2008-11-16 03:12:49 -0800557 if (APIC_INTEGRATED(apic_version[boot_cpu_physical_apicid])) {
Cyrill Gorcunov59ef48a2008-09-14 21:58:49 +0400558 maxlvt = lapic_get_maxlvt();
559 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
560 apic_write(APIC_ESR, 0);
561 accept_status = (apic_read(APIC_ESR) & 0xEF);
562 }
Thomas Gleixnercfc1b9a2008-07-21 21:35:38 +0200563 pr_debug("NMI sent.\n");
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300564
565 if (send_status)
566 printk(KERN_ERR "APIC never delivered???\n");
567 if (accept_status)
568 printk(KERN_ERR "APIC delivery error (%lx).\n", accept_status);
569
570 return (send_status | accept_status);
571}
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300572
Cyrill Gorcunovcece3152009-04-18 23:45:28 +0400573static int __cpuinit
Yinghai Lu569712b2008-11-16 03:12:49 -0800574wakeup_secondary_cpu_via_init(int phys_apicid, unsigned long start_eip)
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300575{
576 unsigned long send_status, accept_status = 0;
577 int maxlvt, num_starts, j;
578
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +0100579 maxlvt = lapic_get_maxlvt();
580
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300581 /*
582 * Be paranoid about clearing APIC errors.
583 */
584 if (APIC_INTEGRATED(apic_version[phys_apicid])) {
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +0100585 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
586 apic_write(APIC_ESR, 0);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300587 apic_read(APIC_ESR);
588 }
589
Thomas Gleixnercfc1b9a2008-07-21 21:35:38 +0200590 pr_debug("Asserting INIT.\n");
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300591
592 /*
593 * Turn INIT on target chip
594 */
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300595 /*
596 * Send IPI
597 */
Suresh Siddha1b374e42008-07-10 11:16:49 -0700598 apic_icr_write(APIC_INT_LEVELTRIG | APIC_INT_ASSERT | APIC_DM_INIT,
599 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 mdelay(10);
605
Thomas Gleixnercfc1b9a2008-07-21 21:35:38 +0200606 pr_debug("Deasserting INIT.\n");
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300607
608 /* Target chip */
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300609 /* Send IPI */
Suresh Siddha1b374e42008-07-10 11:16:49 -0700610 apic_icr_write(APIC_INT_LEVELTRIG | APIC_DM_INIT, phys_apicid);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300611
Thomas Gleixnercfc1b9a2008-07-21 21:35:38 +0200612 pr_debug("Waiting for send to finish...\n");
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300613 send_status = safe_apic_wait_icr_idle();
614
615 mb();
616 atomic_set(&init_deasserted, 1);
617
618 /*
619 * Should we send STARTUP IPIs ?
620 *
621 * Determine this based on the APIC version.
622 * If we don't have an integrated APIC, don't send the STARTUP IPIs.
623 */
624 if (APIC_INTEGRATED(apic_version[phys_apicid]))
625 num_starts = 2;
626 else
627 num_starts = 0;
628
629 /*
630 * Paravirt / VMI wants a startup IPI hook here to set up the
631 * target processor state.
632 */
633 startup_ipi_hook(phys_apicid, (unsigned long) start_secondary,
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300634 (unsigned long)stack_start.sp);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300635
636 /*
637 * Run STARTUP IPI loop.
638 */
Thomas Gleixnercfc1b9a2008-07-21 21:35:38 +0200639 pr_debug("#startup loops: %d.\n", num_starts);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300640
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300641 for (j = 1; j <= num_starts; j++) {
Thomas Gleixnercfc1b9a2008-07-21 21:35:38 +0200642 pr_debug("Sending STARTUP #%d.\n", j);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +0100643 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
644 apic_write(APIC_ESR, 0);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300645 apic_read(APIC_ESR);
Thomas Gleixnercfc1b9a2008-07-21 21:35:38 +0200646 pr_debug("After apic_write.\n");
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300647
648 /*
649 * STARTUP IPI
650 */
651
652 /* Target chip */
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300653 /* Boot on the stack */
654 /* Kick the second */
Suresh Siddha1b374e42008-07-10 11:16:49 -0700655 apic_icr_write(APIC_DM_STARTUP | (start_eip >> 12),
656 phys_apicid);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300657
658 /*
659 * Give the other CPU some time to accept the IPI.
660 */
661 udelay(300);
662
Thomas Gleixnercfc1b9a2008-07-21 21:35:38 +0200663 pr_debug("Startup point 1.\n");
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300664
Thomas Gleixnercfc1b9a2008-07-21 21:35:38 +0200665 pr_debug("Waiting for send to finish...\n");
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300666 send_status = safe_apic_wait_icr_idle();
667
668 /*
669 * Give the other CPU some time to accept the IPI.
670 */
671 udelay(200);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +0100672 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300673 apic_write(APIC_ESR, 0);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300674 accept_status = (apic_read(APIC_ESR) & 0xEF);
675 if (send_status || accept_status)
676 break;
677 }
Thomas Gleixnercfc1b9a2008-07-21 21:35:38 +0200678 pr_debug("After Startup.\n");
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300679
680 if (send_status)
681 printk(KERN_ERR "APIC never delivered???\n");
682 if (accept_status)
683 printk(KERN_ERR "APIC delivery error (%lx).\n", accept_status);
684
685 return (send_status | accept_status);
686}
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300687
688struct create_idle {
689 struct work_struct work;
690 struct task_struct *idle;
691 struct completion done;
692 int cpu;
693};
694
695static void __cpuinit do_fork_idle(struct work_struct *work)
696{
697 struct create_idle *c_idle =
698 container_of(work, struct create_idle, work);
699
700 c_idle->idle = fork_idle(c_idle->cpu);
701 complete(&c_idle->done);
702}
703
Mike Travis2eaad1f2009-12-10 17:19:36 -0800704/* reduce the number of lines printed when booting a large cpu count system */
705static void __cpuinit announce_cpu(int cpu, int apicid)
706{
707 static int current_node = -1;
Borislav Petkov4adc8b72010-06-01 21:04:55 +0200708 int node = early_cpu_to_node(cpu);
Mike Travis2eaad1f2009-12-10 17:19:36 -0800709
710 if (system_state == SYSTEM_BOOTING) {
711 if (node != current_node) {
712 if (current_node > (-1))
713 pr_cont(" Ok.\n");
714 current_node = node;
715 pr_info("Booting Node %3d, Processors ", node);
716 }
717 pr_cont(" #%d%s", cpu, cpu == (nr_cpu_ids - 1) ? " Ok.\n" : "");
718 return;
719 } else
720 pr_info("Booting Node %d Processor %d APIC 0x%x\n",
721 node, cpu, apicid);
722}
723
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300724/*
725 * NOTE - on most systems this is a PHYSICAL apic ID, but on multiquad
726 * (ie clustered apic addressing mode), this is a LOGICAL apic ID.
Ingo Molnar1f5bcab2009-02-26 13:51:40 +0100727 * Returns zero if CPU booted OK, else error code from
728 * ->wakeup_secondary_cpu.
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300729 */
Ingo Molnarab6fb7c2009-02-17 16:22:09 +0100730static int __cpuinit do_boot_cpu(int apicid, int cpu)
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300731{
732 unsigned long boot_error = 0;
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300733 unsigned long start_ip;
Ingo Molnarab6fb7c2009-02-17 16:22:09 +0100734 int timeout;
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300735 struct create_idle c_idle = {
Ingo Molnarab6fb7c2009-02-17 16:22:09 +0100736 .cpu = cpu,
737 .done = COMPLETION_INITIALIZER_ONSTACK(c_idle.done),
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300738 };
Ingo Molnarab6fb7c2009-02-17 16:22:09 +0100739
Andrew Mortonca1cab32010-10-26 14:22:34 -0700740 INIT_WORK_ONSTACK(&c_idle.work, do_fork_idle);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300741
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300742 alternatives_smp_switch(1);
743
744 c_idle.idle = get_idle_for_cpu(cpu);
745
746 /*
747 * We can't use kernel_thread since we must avoid to
748 * reschedule the child.
749 */
750 if (c_idle.idle) {
751 c_idle.idle->thread.sp = (unsigned long) (((struct pt_regs *)
752 (THREAD_SIZE + task_stack_page(c_idle.idle))) - 1);
753 init_idle(c_idle.idle, cpu);
754 goto do_rest;
755 }
756
Suresh Siddhad7a7c572010-08-09 17:20:33 -0700757 schedule_work(&c_idle.work);
758 wait_for_completion(&c_idle.done);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300759
760 if (IS_ERR(c_idle.idle)) {
761 printk("failed fork for CPU %d\n", cpu);
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900762 destroy_work_on_stack(&c_idle.work);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300763 return PTR_ERR(c_idle.idle);
764 }
765
766 set_idle_for_cpu(cpu, c_idle.idle);
767do_rest:
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300768 per_cpu(current_task, cpu) = c_idle.idle;
Brian Gerstc6f5e0a2009-01-19 00:38:58 +0900769#ifdef CONFIG_X86_32
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300770 /* Stack for startup_32 can be just as for start_secondary onwards */
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300771 irq_ctx_init(cpu);
772#else
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300773 clear_tsk_thread_flag(c_idle.idle, TIF_FORK);
Tejun Heo004aa322009-01-13 20:41:35 +0900774 initial_gs = per_cpu_offset(cpu);
Brian Gerst9af45652009-01-19 00:38:58 +0900775 per_cpu(kernel_stack, cpu) =
776 (unsigned long)task_stack_page(c_idle.idle) -
777 KERNEL_STACK_OFFSET + THREAD_SIZE;
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300778#endif
Glauber Costaa9390982008-05-28 16:19:53 -0700779 early_gdt_descr.address = (unsigned long)get_cpu_gdt_table(cpu);
Glauber Costa3e970472008-05-28 13:01:54 -0300780 initial_code = (unsigned long)start_secondary;
Glauber Costa9cf4f292008-05-27 18:22:54 -0700781 stack_start.sp = (void *) c_idle.idle->thread.sp;
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300782
783 /* start_ip had better be page-aligned! */
784 start_ip = setup_trampoline();
785
Mike Travis2eaad1f2009-12-10 17:19:36 -0800786 /* So we see what's up */
787 announce_cpu(cpu, apicid);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300788
789 /*
790 * This grunge runs the startup process for
791 * the targeted processor.
792 */
793
794 atomic_set(&init_deasserted, 0);
795
Jack Steiner34d05592008-04-16 11:45:15 -0500796 if (get_uv_system_type() != UV_NON_UNIQUE_APIC) {
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300797
Thomas Gleixnercfc1b9a2008-07-21 21:35:38 +0200798 pr_debug("Setting warm reset code and vector.\n");
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300799
Jack Steiner34d05592008-04-16 11:45:15 -0500800 smpboot_setup_warm_reset_vector(start_ip);
801 /*
802 * Be paranoid about clearing APIC errors.
Cyrill Gorcunovdb96b0a2008-10-22 18:00:09 +0400803 */
804 if (APIC_INTEGRATED(apic_version[boot_cpu_physical_apicid])) {
805 apic_write(APIC_ESR, 0);
806 apic_read(APIC_ESR);
807 }
Jack Steiner34d05592008-04-16 11:45:15 -0500808 }
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300809
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300810 /*
Ingo Molnar1f5bcab2009-02-26 13:51:40 +0100811 * Kick the secondary CPU. Use the method in the APIC driver
812 * if it's defined - or use an INIT boot APIC message otherwise:
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300813 */
Ingo Molnar1f5bcab2009-02-26 13:51:40 +0100814 if (apic->wakeup_secondary_cpu)
815 boot_error = apic->wakeup_secondary_cpu(apicid, start_ip);
816 else
817 boot_error = wakeup_secondary_cpu_via_init(apicid, start_ip);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300818
819 if (!boot_error) {
820 /*
821 * allow APs to start initializing.
822 */
Thomas Gleixnercfc1b9a2008-07-21 21:35:38 +0200823 pr_debug("Before Callout %d.\n", cpu);
Mike Travisc2d1cec2009-01-04 05:18:03 -0800824 cpumask_set_cpu(cpu, cpu_callout_mask);
Thomas Gleixnercfc1b9a2008-07-21 21:35:38 +0200825 pr_debug("After Callout %d.\n", cpu);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300826
827 /*
828 * Wait 5s total for a response
829 */
830 for (timeout = 0; timeout < 50000; timeout++) {
Mike Travisc2d1cec2009-01-04 05:18:03 -0800831 if (cpumask_test_cpu(cpu, cpu_callin_mask))
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300832 break; /* It has booted */
833 udelay(100);
Suresh Siddha68f202e2010-07-30 11:46:42 -0700834 /*
835 * Allow other tasks to run while we wait for the
836 * AP to come online. This also gives a chance
837 * for the MTRR work(triggered by the AP coming online)
838 * to be completed in the stop machine context.
839 */
840 schedule();
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300841 }
842
Mike Travis2eaad1f2009-12-10 17:19:36 -0800843 if (cpumask_test_cpu(cpu, cpu_callin_mask))
844 pr_debug("CPU%d: has booted.\n", cpu);
845 else {
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300846 boot_error = 1;
847 if (*((volatile unsigned char *)trampoline_base)
848 == 0xA5)
849 /* trampoline started but...? */
Mike Travis2eaad1f2009-12-10 17:19:36 -0800850 pr_err("CPU%d: Stuck ??\n", cpu);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300851 else
852 /* trampoline code not run */
Mike Travis2eaad1f2009-12-10 17:19:36 -0800853 pr_err("CPU%d: Not responding.\n", cpu);
Ingo Molnar25dc0042009-01-28 16:31:52 +0100854 if (apic->inquire_remote_apic)
855 apic->inquire_remote_apic(apicid);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300856 }
857 }
Tejun Heo1a51e3a2009-01-13 20:41:35 +0900858
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300859 if (boot_error) {
860 /* Try to put things back the way they were before ... */
Mike Travis23ca4bb2008-05-12 21:21:12 +0200861 numa_remove_cpu(cpu); /* was set by numa_add_cpu */
Mike Travisc2d1cec2009-01-04 05:18:03 -0800862
863 /* was set by do_boot_cpu() */
864 cpumask_clear_cpu(cpu, cpu_callout_mask);
865
866 /* was set by cpu_init() */
867 cpumask_clear_cpu(cpu, cpu_initialized_mask);
868
869 set_cpu_present(cpu, false);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300870 per_cpu(x86_cpu_to_apicid, cpu) = BAD_APICID;
871 }
872
873 /* mark "stuck" area as not stuck */
874 *((volatile unsigned long *)trampoline_base) = 0;
875
Yinghai Lu02421f92009-04-03 17:15:53 -0700876 if (get_uv_system_type() != UV_NON_UNIQUE_APIC) {
877 /*
878 * Cleanup possible dangling ends...
879 */
880 smpboot_restore_warm_reset_vector();
881 }
Alok Kataria63d38192008-04-07 11:38:33 -0700882
Thomas Gleixnerdc186ad2009-11-16 01:09:48 +0900883 destroy_work_on_stack(&c_idle.work);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300884 return boot_error;
885}
886
887int __cpuinit native_cpu_up(unsigned int cpu)
888{
Ingo Molnara21769a42009-01-28 06:50:47 +0100889 int apicid = apic->cpu_present_to_apicid(cpu);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300890 unsigned long flags;
891 int err;
892
893 WARN_ON(irqs_disabled());
894
Thomas Gleixnercfc1b9a2008-07-21 21:35:38 +0200895 pr_debug("++++++++++++++++++++=_---CPU UP %u\n", cpu);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300896
897 if (apicid == BAD_APICID || apicid == boot_cpu_physical_apicid ||
898 !physid_isset(apicid, phys_cpu_present_map)) {
899 printk(KERN_ERR "%s: bad cpu %d\n", __func__, cpu);
900 return -EINVAL;
901 }
902
903 /*
904 * Already booted CPU?
905 */
Mike Travisc2d1cec2009-01-04 05:18:03 -0800906 if (cpumask_test_cpu(cpu, cpu_callin_mask)) {
Thomas Gleixnercfc1b9a2008-07-21 21:35:38 +0200907 pr_debug("do_boot_cpu %d Already started\n", cpu);
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300908 return -ENOSYS;
909 }
910
911 /*
912 * Save current MTRR state in case it was changed since early boot
913 * (e.g. by the ACPI SMI) to initialize new CPUs with MTRRs in sync:
914 */
915 mtrr_save_state();
916
917 per_cpu(cpu_state, cpu) = CPU_UP_PREPARE;
918
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300919 err = do_boot_cpu(apicid, cpu);
Hugh Dickins61165d72008-05-13 14:26:57 +0100920 if (err) {
Thomas Gleixnercfc1b9a2008-07-21 21:35:38 +0200921 pr_debug("do_boot_cpu failed %d\n", err);
Hugh Dickins61165d72008-05-13 14:26:57 +0100922 return -EIO;
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300923 }
924
925 /*
926 * Check TSC synchronization with the AP (keep irqs disabled
927 * while doing so):
928 */
929 local_irq_save(flags);
930 check_tsc_sync_source(cpu);
931 local_irq_restore(flags);
932
Akinobu Mita7c04e642008-04-19 23:55:17 +0900933 while (!cpu_online(cpu)) {
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300934 cpu_relax();
935 touch_nmi_watchdog();
936 }
937
938 return 0;
939}
940
Glauber de Oliveira Costaa8db8452008-03-19 14:26:01 -0300941/*
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -0300942 * Fall back to non SMP mode after errors.
943 *
944 * RED-PEN audit/test this more. I bet there is more state messed up here.
945 */
946static __init void disable_smp(void)
947{
Rusty Russell4f062892009-03-13 14:49:54 +1030948 init_cpu_present(cpumask_of(0));
949 init_cpu_possible(cpumask_of(0));
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -0300950 smpboot_clear_io_apic_irqs();
Glauber Costa0f385d12008-05-28 17:09:53 -0700951
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -0300952 if (smp_found_config)
Jack Steinerb6df1b82008-06-19 21:51:05 -0500953 physid_set_mask_of_physid(boot_cpu_physical_apicid, &phys_cpu_present_map);
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -0300954 else
Jack Steinerb6df1b82008-06-19 21:51:05 -0500955 physid_set_mask_of_physid(0, &phys_cpu_present_map);
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -0300956 map_cpu_to_logical_apicid();
Mike Travisc2d1cec2009-01-04 05:18:03 -0800957 cpumask_set_cpu(0, cpu_sibling_mask(0));
958 cpumask_set_cpu(0, cpu_core_mask(0));
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -0300959}
960
961/*
962 * Various sanity checks.
963 */
964static int __init smp_sanity_check(unsigned max_cpus)
965{
Jack Steinerac23d4e2008-03-28 14:12:16 -0500966 preempt_disable();
Yinghai Lua58f03b2008-08-14 02:16:30 -0700967
Yinghai Lu1ff2f202009-01-29 19:30:04 -0800968#if !defined(CONFIG_X86_BIGSMP) && defined(CONFIG_X86_32)
Yinghai Lua58f03b2008-08-14 02:16:30 -0700969 if (def_to_bigsmp && nr_cpu_ids > 8) {
970 unsigned int cpu;
971 unsigned nr;
972
973 printk(KERN_WARNING
974 "More than 8 CPUs detected - skipping them.\n"
Yinghai Lu26f7ef12009-01-29 14:19:22 -0800975 "Use CONFIG_X86_BIGSMP.\n");
Yinghai Lua58f03b2008-08-14 02:16:30 -0700976
977 nr = 0;
978 for_each_present_cpu(cpu) {
979 if (nr >= 8)
Mike Travisc2d1cec2009-01-04 05:18:03 -0800980 set_cpu_present(cpu, false);
Yinghai Lua58f03b2008-08-14 02:16:30 -0700981 nr++;
982 }
983
984 nr = 0;
985 for_each_possible_cpu(cpu) {
986 if (nr >= 8)
Mike Travisc2d1cec2009-01-04 05:18:03 -0800987 set_cpu_possible(cpu, false);
Yinghai Lua58f03b2008-08-14 02:16:30 -0700988 nr++;
989 }
990
991 nr_cpu_ids = 8;
992 }
993#endif
994
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -0300995 if (!physid_isset(hard_smp_processor_id(), phys_cpu_present_map)) {
Michael Tokarev55c395b2008-12-05 14:42:20 +0300996 printk(KERN_WARNING
997 "weird, boot CPU (#%d) not listed by the BIOS.\n",
998 hard_smp_processor_id());
999
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001000 physid_set(hard_smp_processor_id(), phys_cpu_present_map);
1001 }
1002
1003 /*
1004 * If we couldn't find an SMP configuration at boot time,
1005 * get out of here now!
1006 */
1007 if (!smp_found_config && !acpi_lapic) {
Jack Steinerac23d4e2008-03-28 14:12:16 -05001008 preempt_enable();
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001009 printk(KERN_NOTICE "SMP motherboard not detected.\n");
1010 disable_smp();
1011 if (APIC_init_uniprocessor())
1012 printk(KERN_NOTICE "Local APIC not detected."
1013 " Using dummy APIC emulation.\n");
1014 return -1;
1015 }
1016
1017 /*
1018 * Should not be necessary because the MP table should list the boot
1019 * CPU too, but we do it for the sake of robustness anyway.
1020 */
Ingo Molnara27a6212009-01-28 12:43:18 +01001021 if (!apic->check_phys_apicid_present(boot_cpu_physical_apicid)) {
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001022 printk(KERN_NOTICE
1023 "weird, boot CPU (#%d) not listed by the BIOS.\n",
1024 boot_cpu_physical_apicid);
1025 physid_set(hard_smp_processor_id(), phys_cpu_present_map);
1026 }
Jack Steinerac23d4e2008-03-28 14:12:16 -05001027 preempt_enable();
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001028
1029 /*
1030 * If we couldn't find a local APIC, then get out of here now!
1031 */
1032 if (APIC_INTEGRATED(apic_version[boot_cpu_physical_apicid]) &&
1033 !cpu_has_apic) {
Cyrill Gorcunov103428e2009-06-07 16:48:40 +04001034 if (!disable_apic) {
1035 pr_err("BIOS bug, local APIC #%d not detected!...\n",
1036 boot_cpu_physical_apicid);
1037 pr_err("... forcing use of dummy APIC emulation."
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001038 "(tell your hw vendor)\n");
Cyrill Gorcunov103428e2009-06-07 16:48:40 +04001039 }
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001040 smpboot_clear_io_apic();
Ingo Molnar65a4e572009-01-31 03:36:17 +01001041 arch_disable_smp_support();
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001042 return -1;
1043 }
1044
1045 verify_local_APIC();
1046
1047 /*
1048 * If SMP should be disabled, then really disable it!
1049 */
1050 if (!max_cpus) {
Maciej W. Rozycki73d08e62008-05-21 22:09:43 +01001051 printk(KERN_INFO "SMP mode deactivated.\n");
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001052 smpboot_clear_io_apic();
Maciej W. Rozyckid54db1a2008-06-06 03:28:02 +01001053
Jesse Barnese90955c2008-04-21 14:14:44 -07001054 connect_bsp_APIC();
Jesse Barnese90955c2008-04-21 14:14:44 -07001055 setup_local_APIC();
1056 end_local_APIC_setup();
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001057 return -1;
1058 }
1059
1060 return 0;
1061}
1062
1063static void __init smp_cpu_index_default(void)
1064{
1065 int i;
1066 struct cpuinfo_x86 *c;
1067
Akinobu Mita7c04e642008-04-19 23:55:17 +09001068 for_each_possible_cpu(i) {
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001069 c = &cpu_data(i);
1070 /* mark all to hotplug */
Mike Travis96289372008-12-31 18:08:46 -08001071 c->cpu_index = nr_cpu_ids;
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001072 }
1073}
1074
1075/*
1076 * Prepare for SMP bootup. The MP table or ACPI has been read
1077 * earlier. Just do some sanity checking here and enable APIC mode.
1078 */
1079void __init native_smp_prepare_cpus(unsigned int max_cpus)
1080{
Rusty Russell7ad728f2009-03-13 14:49:50 +10301081 unsigned int i;
1082
Ingo Molnardeef3252008-05-12 15:44:38 +02001083 preempt_disable();
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001084 smp_cpu_index_default();
Tejun Heo7b543a52010-12-18 16:30:05 +01001085 memcpy(__this_cpu_ptr(&cpu_info), &boot_cpu_data, sizeof(cpu_info));
Mike Travisc2d1cec2009-01-04 05:18:03 -08001086 cpumask_copy(cpu_callin_mask, cpumask_of(0));
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001087 mb();
1088 /*
1089 * Setup boot CPU information
1090 */
1091 smp_store_cpu_info(0); /* Final full version of the data */
Tejun Heobd22a2f2011-01-23 14:37:27 +01001092
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001093 current_thread_info()->cpu = 0; /* needed? */
Rusty Russell7ad728f2009-03-13 14:49:50 +10301094 for_each_possible_cpu(i) {
Li Zefan79f55992009-06-15 14:58:26 +08001095 zalloc_cpumask_var(&per_cpu(cpu_sibling_map, i), GFP_KERNEL);
1096 zalloc_cpumask_var(&per_cpu(cpu_core_map, i), GFP_KERNEL);
1097 zalloc_cpumask_var(&cpu_data(i).llc_shared_map, GFP_KERNEL);
Rusty Russell7ad728f2009-03-13 14:49:50 +10301098 }
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001099 set_cpu_sibling_map(0);
1100
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001101
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001102 if (smp_sanity_check(max_cpus) < 0) {
1103 printk(KERN_INFO "SMP disabled\n");
1104 disable_smp();
Ingo Molnardeef3252008-05-12 15:44:38 +02001105 goto out;
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001106 }
1107
Suresh Siddhafa47f7e2010-08-27 11:09:50 -07001108 default_setup_apic_routing();
1109
Jack Steinerac23d4e2008-03-28 14:12:16 -05001110 preempt_disable();
Yinghai Lu4c9961d2008-07-11 18:44:16 -07001111 if (read_apic_id() != boot_cpu_physical_apicid) {
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001112 panic("Boot APIC ID in local APIC unexpected (%d vs %d)",
Yinghai Lu4c9961d2008-07-11 18:44:16 -07001113 read_apic_id(), boot_cpu_physical_apicid);
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001114 /* Or can we switch back to PIC here? */
1115 }
Jack Steinerac23d4e2008-03-28 14:12:16 -05001116 preempt_enable();
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001117
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001118 connect_bsp_APIC();
Glauber Costab5841762008-05-28 13:38:28 -03001119
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001120 /*
1121 * Switch from PIC to APIC mode.
1122 */
1123 setup_local_APIC();
1124
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001125 /*
1126 * Enable IO APIC before setting up error vector
1127 */
1128 if (!skip_ioapic_setup && nr_ioapics)
1129 enable_IO_APIC();
Yinghai Lu88d0f552009-02-14 23:57:28 -08001130
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001131 end_local_APIC_setup();
1132
1133 map_cpu_to_logical_apicid();
1134
Ingo Molnard83093b2009-01-28 12:43:18 +01001135 if (apic->setup_portio_remap)
1136 apic->setup_portio_remap();
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001137
1138 smpboot_setup_io_apic();
1139 /*
1140 * Set up local APIC timer on boot CPU.
1141 */
1142
1143 printk(KERN_INFO "CPU%d: ", 0);
1144 print_cpu_info(&cpu_data(0));
Thomas Gleixner736deca2009-08-19 12:35:53 +02001145 x86_init.timers.setup_percpu_clockev();
Marcin Slusarzc4bd1fd2008-08-21 20:49:05 +02001146
1147 if (is_uv_system())
1148 uv_system_init();
Suresh Siddhad0af9ee2009-08-19 18:05:36 -07001149
1150 set_mtrr_aps_delayed_init();
Ingo Molnardeef3252008-05-12 15:44:38 +02001151out:
1152 preempt_enable();
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001153}
Suresh Siddhad0af9ee2009-08-19 18:05:36 -07001154
Suresh Siddha3fb82d52010-11-23 16:11:40 -08001155void arch_disable_nonboot_cpus_begin(void)
1156{
1157 /*
1158 * Avoid the smp alternatives switch during the disable_nonboot_cpus().
1159 * In the suspend path, we will be back in the SMP mode shortly anyways.
1160 */
1161 skip_smp_alternatives = true;
1162}
1163
1164void arch_disable_nonboot_cpus_end(void)
1165{
1166 skip_smp_alternatives = false;
1167}
1168
Suresh Siddhad0af9ee2009-08-19 18:05:36 -07001169void arch_enable_nonboot_cpus_begin(void)
1170{
1171 set_mtrr_aps_delayed_init();
1172}
1173
1174void arch_enable_nonboot_cpus_end(void)
1175{
1176 mtrr_aps_init();
1177}
1178
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001179/*
Glauber de Oliveira Costaa8db8452008-03-19 14:26:01 -03001180 * Early setup to make printk work.
1181 */
1182void __init native_smp_prepare_boot_cpu(void)
1183{
1184 int me = smp_processor_id();
Brian Gerst552be872009-01-30 17:47:53 +09001185 switch_to_new_gdt(me);
Mike Travisc2d1cec2009-01-04 05:18:03 -08001186 /* already set me in cpu_online_mask in boot_cpu_init() */
1187 cpumask_set_cpu(me, cpu_callout_mask);
Glauber de Oliveira Costaa8db8452008-03-19 14:26:01 -03001188 per_cpu(cpu_state, me) = CPU_ONLINE;
1189}
1190
Glauber de Oliveira Costa83f7eb92008-03-19 14:26:02 -03001191void __init native_smp_cpus_done(unsigned int max_cpus)
1192{
Thomas Gleixnercfc1b9a2008-07-21 21:35:38 +02001193 pr_debug("Boot done.\n");
Glauber de Oliveira Costa83f7eb92008-03-19 14:26:02 -03001194
1195 impress_friends();
Glauber de Oliveira Costa83f7eb92008-03-19 14:26:02 -03001196#ifdef CONFIG_X86_IO_APIC
1197 setup_ioapic_dest();
1198#endif
Suresh Siddhad0af9ee2009-08-19 18:05:36 -07001199 mtrr_aps_init();
Glauber de Oliveira Costa83f7eb92008-03-19 14:26:02 -03001200}
1201
Mike Travis3b11ce72008-12-17 15:21:39 -08001202static int __initdata setup_possible_cpus = -1;
1203static int __init _setup_possible_cpus(char *str)
1204{
1205 get_option(&str, &setup_possible_cpus);
1206 return 0;
1207}
1208early_param("possible_cpus", _setup_possible_cpus);
1209
1210
Glauber Costa68a1c3f2008-03-03 14:12:42 -03001211/*
Rusty Russell4f062892009-03-13 14:49:54 +10301212 * cpu_possible_mask should be static, it cannot change as cpu's
Glauber Costa68a1c3f2008-03-03 14:12:42 -03001213 * are onlined, or offlined. The reason is per-cpu data-structures
1214 * are allocated by some modules at init time, and dont expect to
1215 * do this dynamically on cpu arrival/departure.
Rusty Russell4f062892009-03-13 14:49:54 +10301216 * cpu_present_mask on the other hand can change dynamically.
Glauber Costa68a1c3f2008-03-03 14:12:42 -03001217 * In case when cpu_hotplug is not compiled, then we resort to current
1218 * behaviour, which is cpu_possible == cpu_present.
1219 * - Ashok Raj
1220 *
1221 * Three ways to find out the number of additional hotplug CPUs:
1222 * - If the BIOS specified disabled CPUs in ACPI/mptables use that.
Mike Travis3b11ce72008-12-17 15:21:39 -08001223 * - The user can overwrite it with possible_cpus=NUM
Glauber Costa68a1c3f2008-03-03 14:12:42 -03001224 * - Otherwise don't reserve additional CPUs.
1225 * We do this because additional CPUs waste a lot of memory.
1226 * -AK
1227 */
1228__init void prefill_possible_map(void)
1229{
Thomas Gleixnercb48bb52008-10-05 17:51:52 +02001230 int i, possible;
Glauber Costa68a1c3f2008-03-03 14:12:42 -03001231
Yinghai Lu329513a2008-07-02 18:54:40 -07001232 /* no processor from mptable or madt */
1233 if (!num_processors)
1234 num_processors = 1;
1235
Jan Beulich5f2eb552010-05-24 12:13:17 -07001236 i = setup_max_cpus ?: 1;
1237 if (setup_possible_cpus == -1) {
1238 possible = num_processors;
1239#ifdef CONFIG_HOTPLUG_CPU
1240 if (setup_max_cpus)
1241 possible += disabled_cpus;
1242#else
1243 if (possible > i)
1244 possible = i;
1245#endif
1246 } else
Mike Travis3b11ce72008-12-17 15:21:39 -08001247 possible = setup_possible_cpus;
1248
Mike Travis730cf272008-12-31 18:08:45 -08001249 total_cpus = max_t(int, possible, num_processors + disabled_cpus);
1250
Yinghai Lu2b633e32010-02-10 01:20:37 -08001251 /* nr_cpu_ids could be reduced via nr_cpus= */
1252 if (possible > nr_cpu_ids) {
Mike Travis3b11ce72008-12-17 15:21:39 -08001253 printk(KERN_WARNING
1254 "%d Processors exceeds NR_CPUS limit of %d\n",
Yinghai Lu2b633e32010-02-10 01:20:37 -08001255 possible, nr_cpu_ids);
1256 possible = nr_cpu_ids;
Mike Travis3b11ce72008-12-17 15:21:39 -08001257 }
Glauber Costa68a1c3f2008-03-03 14:12:42 -03001258
Jan Beulich5f2eb552010-05-24 12:13:17 -07001259#ifdef CONFIG_HOTPLUG_CPU
1260 if (!setup_max_cpus)
1261#endif
1262 if (possible > i) {
1263 printk(KERN_WARNING
1264 "%d Processors exceeds max_cpus limit of %u\n",
1265 possible, setup_max_cpus);
1266 possible = i;
1267 }
1268
Glauber Costa68a1c3f2008-03-03 14:12:42 -03001269 printk(KERN_INFO "SMP: Allowing %d CPUs, %d hotplug CPUs\n",
1270 possible, max_t(int, possible - num_processors, 0));
1271
1272 for (i = 0; i < possible; i++)
Mike Travisc2d1cec2009-01-04 05:18:03 -08001273 set_cpu_possible(i, true);
Jan Beulich5f2eb552010-05-24 12:13:17 -07001274 for (; i < NR_CPUS; i++)
1275 set_cpu_possible(i, false);
Mike Travis3461b0a2008-05-12 21:21:13 +02001276
1277 nr_cpu_ids = possible;
Glauber Costa68a1c3f2008-03-03 14:12:42 -03001278}
Glauber Costa69c18c12008-03-03 14:13:07 -03001279
Chuck Ebbert14adf852008-09-29 18:29:42 -04001280#ifdef CONFIG_HOTPLUG_CPU
1281
1282static void remove_siblinginfo(int cpu)
1283{
1284 int sibling;
1285 struct cpuinfo_x86 *c = &cpu_data(cpu);
1286
Mike Travisc2d1cec2009-01-04 05:18:03 -08001287 for_each_cpu(sibling, cpu_core_mask(cpu)) {
1288 cpumask_clear_cpu(cpu, cpu_core_mask(sibling));
Chuck Ebbert14adf852008-09-29 18:29:42 -04001289 /*/
1290 * last thread sibling in this cpu core going down
1291 */
Mike Travisc2d1cec2009-01-04 05:18:03 -08001292 if (cpumask_weight(cpu_sibling_mask(cpu)) == 1)
Chuck Ebbert14adf852008-09-29 18:29:42 -04001293 cpu_data(sibling).booted_cores--;
1294 }
1295
Mike Travisc2d1cec2009-01-04 05:18:03 -08001296 for_each_cpu(sibling, cpu_sibling_mask(cpu))
1297 cpumask_clear_cpu(cpu, cpu_sibling_mask(sibling));
1298 cpumask_clear(cpu_sibling_mask(cpu));
1299 cpumask_clear(cpu_core_mask(cpu));
Chuck Ebbert14adf852008-09-29 18:29:42 -04001300 c->phys_proc_id = 0;
1301 c->cpu_core_id = 0;
Mike Travisc2d1cec2009-01-04 05:18:03 -08001302 cpumask_clear_cpu(cpu, cpu_sibling_setup_mask);
Chuck Ebbert14adf852008-09-29 18:29:42 -04001303}
1304
Glauber Costa69c18c12008-03-03 14:13:07 -03001305static void __ref remove_cpu_from_maps(int cpu)
1306{
Mike Travisc2d1cec2009-01-04 05:18:03 -08001307 set_cpu_online(cpu, false);
1308 cpumask_clear_cpu(cpu, cpu_callout_mask);
1309 cpumask_clear_cpu(cpu, cpu_callin_mask);
Glauber Costa69c18c12008-03-03 14:13:07 -03001310 /* was set by cpu_init() */
Mike Travisc2d1cec2009-01-04 05:18:03 -08001311 cpumask_clear_cpu(cpu, cpu_initialized_mask);
Mike Travis23ca4bb2008-05-12 21:21:12 +02001312 numa_remove_cpu(cpu);
Glauber Costa69c18c12008-03-03 14:13:07 -03001313}
1314
Alex Nixon8227dce2008-08-22 11:52:14 +01001315void cpu_disable_common(void)
1316{
1317 int cpu = smp_processor_id();
Alex Nixon8227dce2008-08-22 11:52:14 +01001318
Alex Nixon8227dce2008-08-22 11:52:14 +01001319 remove_siblinginfo(cpu);
1320
1321 /* It's now safe to remove this processor from the online map */
1322 lock_vector_lock();
1323 remove_cpu_from_maps(cpu);
1324 unlock_vector_lock();
Mike Travisd7b381b2008-12-16 17:33:58 -08001325 fixup_irqs();
Alex Nixon8227dce2008-08-22 11:52:14 +01001326}
1327
Alex Nixon93be71b2008-08-22 11:52:11 +01001328int native_cpu_disable(void)
Glauber Costa69c18c12008-03-03 14:13:07 -03001329{
1330 int cpu = smp_processor_id();
1331
1332 /*
1333 * Perhaps use cpufreq to drop frequency, but that could go
1334 * into generic code.
1335 *
1336 * We won't take down the boot processor on i386 due to some
1337 * interrupts only being able to be serviced by the BSP.
1338 * Especially so if we're not using an IOAPIC -zwane
1339 */
1340 if (cpu == 0)
1341 return -EBUSY;
1342
Glauber Costa69c18c12008-03-03 14:13:07 -03001343 clear_local_APIC();
1344
Alex Nixon8227dce2008-08-22 11:52:14 +01001345 cpu_disable_common();
Glauber Costa69c18c12008-03-03 14:13:07 -03001346 return 0;
1347}
1348
Alex Nixon93be71b2008-08-22 11:52:11 +01001349void native_cpu_die(unsigned int cpu)
Glauber Costa69c18c12008-03-03 14:13:07 -03001350{
1351 /* We don't do anything here: idle task is faking death itself. */
1352 unsigned int i;
1353
1354 for (i = 0; i < 10; i++) {
1355 /* They ack this in play_dead by setting CPU_DEAD */
1356 if (per_cpu(cpu_state, cpu) == CPU_DEAD) {
Mike Travis2eaad1f2009-12-10 17:19:36 -08001357 if (system_state == SYSTEM_RUNNING)
1358 pr_info("CPU %u is now offline\n", cpu);
1359
Glauber Costa69c18c12008-03-03 14:13:07 -03001360 if (1 == num_online_cpus())
1361 alternatives_smp_switch(0);
1362 return;
1363 }
1364 msleep(100);
1365 }
Mike Travis2eaad1f2009-12-10 17:19:36 -08001366 pr_err("CPU %u didn't die...\n", cpu);
Glauber Costa69c18c12008-03-03 14:13:07 -03001367}
Alex Nixona21f5d82008-08-22 11:52:13 +01001368
1369void play_dead_common(void)
1370{
1371 idle_task_exit();
1372 reset_lazy_tlbstate();
Ingo Molnar07bbc162008-09-23 23:26:42 +02001373 c1e_remove_cpu(raw_smp_processor_id());
Alex Nixona21f5d82008-08-22 11:52:13 +01001374
1375 mb();
1376 /* Ack it */
Tejun Heo0a3aee02010-12-18 16:28:55 +01001377 __this_cpu_write(cpu_state, CPU_DEAD);
Alex Nixona21f5d82008-08-22 11:52:13 +01001378
1379 /*
1380 * With physical CPU hotplug, we should halt the cpu
1381 */
1382 local_irq_disable();
1383}
1384
H. Peter Anvinea530692010-09-17 15:39:11 -07001385/*
1386 * We need to flush the caches before going to sleep, lest we have
1387 * dirty data in our caches when we come back up.
1388 */
1389static inline void mwait_play_dead(void)
1390{
1391 unsigned int eax, ebx, ecx, edx;
1392 unsigned int highest_cstate = 0;
1393 unsigned int highest_subcstate = 0;
1394 int i;
H. Peter Anvince5f6822010-09-20 13:04:45 -07001395 void *mwait_ptr;
Borislav Petkov93789b32011-01-20 15:42:52 +01001396 struct cpuinfo_x86 *c = __this_cpu_ptr(&cpu_info);
H. Peter Anvinea530692010-09-17 15:39:11 -07001397
Borislav Petkov93789b32011-01-20 15:42:52 +01001398 if (!(cpu_has(c, X86_FEATURE_MWAIT) && mwait_usable(c)))
H. Peter Anvinea530692010-09-17 15:39:11 -07001399 return;
Tejun Heo7b543a52010-12-18 16:30:05 +01001400 if (!cpu_has(__this_cpu_ptr(&cpu_info), X86_FEATURE_CLFLSH))
H. Peter Anvince5f6822010-09-20 13:04:45 -07001401 return;
Tejun Heo7b543a52010-12-18 16:30:05 +01001402 if (__this_cpu_read(cpu_info.cpuid_level) < CPUID_MWAIT_LEAF)
H. Peter Anvinea530692010-09-17 15:39:11 -07001403 return;
1404
1405 eax = CPUID_MWAIT_LEAF;
1406 ecx = 0;
1407 native_cpuid(&eax, &ebx, &ecx, &edx);
1408
1409 /*
1410 * eax will be 0 if EDX enumeration is not valid.
1411 * Initialized below to cstate, sub_cstate value when EDX is valid.
1412 */
1413 if (!(ecx & CPUID5_ECX_EXTENSIONS_SUPPORTED)) {
1414 eax = 0;
1415 } else {
1416 edx >>= MWAIT_SUBSTATE_SIZE;
1417 for (i = 0; i < 7 && edx; i++, edx >>= MWAIT_SUBSTATE_SIZE) {
1418 if (edx & MWAIT_SUBSTATE_MASK) {
1419 highest_cstate = i;
1420 highest_subcstate = edx & MWAIT_SUBSTATE_MASK;
1421 }
1422 }
1423 eax = (highest_cstate << MWAIT_SUBSTATE_SIZE) |
1424 (highest_subcstate - 1);
1425 }
1426
H. Peter Anvince5f6822010-09-20 13:04:45 -07001427 /*
1428 * This should be a memory location in a cache line which is
1429 * unlikely to be touched by other processors. The actual
1430 * content is immaterial as it is not actually modified in any way.
1431 */
1432 mwait_ptr = &current_thread_info()->flags;
1433
H. Peter Anvina68e5c92010-09-17 17:06:46 -07001434 wbinvd();
1435
H. Peter Anvinea530692010-09-17 15:39:11 -07001436 while (1) {
H. Peter Anvince5f6822010-09-20 13:04:45 -07001437 /*
1438 * The CLFLUSH is a workaround for erratum AAI65 for
1439 * the Xeon 7400 series. It's not clear it is actually
1440 * needed, but it should be harmless in either case.
1441 * The WBINVD is insufficient due to the spurious-wakeup
1442 * case where we return around the loop.
1443 */
1444 clflush(mwait_ptr);
1445 __monitor(mwait_ptr, 0, 0);
H. Peter Anvinea530692010-09-17 15:39:11 -07001446 mb();
1447 __mwait(eax, 0);
1448 }
1449}
1450
1451static inline void hlt_play_dead(void)
1452{
Tejun Heo7b543a52010-12-18 16:30:05 +01001453 if (__this_cpu_read(cpu_info.x86) >= 4)
H. Peter Anvina68e5c92010-09-17 17:06:46 -07001454 wbinvd();
1455
H. Peter Anvinea530692010-09-17 15:39:11 -07001456 while (1) {
H. Peter Anvinea530692010-09-17 15:39:11 -07001457 native_halt();
1458 }
1459}
1460
Alex Nixona21f5d82008-08-22 11:52:13 +01001461void native_play_dead(void)
1462{
1463 play_dead_common();
Joseph Cihula86886e52009-06-30 19:31:07 -07001464 tboot_shutdown(TB_SHUTDOWN_WFS);
H. Peter Anvinea530692010-09-17 15:39:11 -07001465
1466 mwait_play_dead(); /* Only returns on failure */
1467 hlt_play_dead();
Alex Nixona21f5d82008-08-22 11:52:13 +01001468}
1469
Glauber Costa69c18c12008-03-03 14:13:07 -03001470#else /* ... !CONFIG_HOTPLUG_CPU */
Alex Nixon93be71b2008-08-22 11:52:11 +01001471int native_cpu_disable(void)
Glauber Costa69c18c12008-03-03 14:13:07 -03001472{
1473 return -ENOSYS;
1474}
1475
Alex Nixon93be71b2008-08-22 11:52:11 +01001476void native_cpu_die(unsigned int cpu)
Glauber Costa69c18c12008-03-03 14:13:07 -03001477{
1478 /* We said "no" in __cpu_disable */
1479 BUG();
1480}
Alex Nixona21f5d82008-08-22 11:52:13 +01001481
1482void native_play_dead(void)
1483{
1484 BUG();
1485}
1486
Glauber Costa68a1c3f2008-03-03 14:12:42 -03001487#endif