blob: 7e6aa1c790a2e6954122b782438ca5229650d8fe [file] [log] [blame]
Glauber de Oliveira Costa4cedb332008-03-19 14:26:14 -03001/*
2 * x86 SMP booting functions
3 *
4 * (c) 1995 Alan Cox, Building #3 <alan@redhat.com>
5 * (c) 1998, 1999, 2000 Ingo Molnar <mingo@redhat.com>
6 * Copyright 2001 Andi Kleen, SuSE Labs.
7 *
8 * Much of the core SMP work is based on previous work by Thomas Radke, to
9 * whom a great many thanks are extended.
10 *
11 * Thanks to Intel for making available several different Pentium,
12 * Pentium Pro and Pentium-II/Xeon MP machines.
13 * Original development of Linux SMP code supported by Caldera.
14 *
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>
Glauber Costa69c18c12008-03-03 14:13:07 -030050
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -030051#include <asm/acpi.h>
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -030052#include <asm/desc.h>
Glauber Costa69c18c12008-03-03 14:13:07 -030053#include <asm/nmi.h>
54#include <asm/irq.h>
55#include <asm/smp.h>
56#include <asm/cpu.h>
57#include <asm/numa.h>
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -030058#include <asm/pgtable.h>
59#include <asm/tlbflush.h>
60#include <asm/mtrr.h>
61#include <asm/nmi.h>
Glauber de Oliveira Costabbc2ff62008-03-19 14:26:00 -030062#include <asm/vmi.h>
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -030063#include <linux/mc146818rtc.h>
Glauber Costa68a1c3f2008-03-03 14:12:42 -030064
Glauber de Oliveira Costaf6bc4022008-03-19 14:25:53 -030065#include <mach_apic.h>
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -030066#include <mach_wakecpu.h>
67#include <smpboot_hooks.h>
68
Glauber de Oliveira Costaacbb6732008-03-19 14:26:13 -030069/*
70 * FIXME: For x86_64, those are defined in other files. But moving them here,
71 * would make the setup areas dependent on smp, which is a loss. When we
72 * integrate apic between arches, we can probably do a better job, but
73 * right now, they'll stay here -- glommer
74 */
75#ifdef CONFIG_X86_32
76/* which logical CPU number maps to which CPU (physical APIC ID) */
77u16 x86_cpu_to_apicid_init[NR_CPUS] __initdata =
78 { [0 ... NR_CPUS-1] = BAD_APICID };
79void *x86_cpu_to_apicid_early_ptr;
Glauber de Oliveira Costaacbb6732008-03-19 14:26:13 -030080
81u16 x86_bios_cpu_apicid_init[NR_CPUS] __initdata
82 = { [0 ... NR_CPUS-1] = BAD_APICID };
83void *x86_bios_cpu_apicid_early_ptr;
Alexey Starikovskiy40014ba2008-03-27 23:54:44 +030084
Glauber de Oliveira Costa4cedb332008-03-19 14:26:14 -030085u8 apicid_2_node[MAX_APICID];
Glauber de Oliveira Costaacbb6732008-03-19 14:26:13 -030086#endif
87
Alexey Starikovskiy7b8cbd22008-03-27 23:55:59 +030088/* Internal processor count */
89unsigned int num_processors;
90
Glauber de Oliveira Costaa8db8452008-03-19 14:26:01 -030091/* State of each CPU */
92DEFINE_PER_CPU(int, cpu_state) = { 0 };
93
Alexey Starikovskiy31036232008-03-27 23:56:06 +030094unsigned disabled_cpus __cpuinitdata;
95
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -030096/* Store all idle threads, this can be reused instead of creating
97* a new thread. Also avoids complicated thread destroy functionality
98* for idle threads.
99*/
100#ifdef CONFIG_HOTPLUG_CPU
101/*
102 * Needed only for CONFIG_HOTPLUG_CPU because __cpuinitdata is
103 * removed after init for !CONFIG_HOTPLUG_CPU.
104 */
105static DEFINE_PER_CPU(struct task_struct *, idle_thread_array);
106#define get_idle_for_cpu(x) (per_cpu(idle_thread_array, x))
107#define set_idle_for_cpu(x, p) (per_cpu(idle_thread_array, x) = (p))
108#else
109struct task_struct *idle_thread_array[NR_CPUS] __cpuinitdata ;
110#define get_idle_for_cpu(x) (idle_thread_array[(x)])
111#define set_idle_for_cpu(x, p) (idle_thread_array[(x)] = (p))
112#endif
Glauber de Oliveira Costaf6bc4022008-03-19 14:25:53 -0300113
Glauber Costaa3553522008-03-03 14:12:58 -0300114/* Number of siblings per CPU package */
115int smp_num_siblings = 1;
116EXPORT_SYMBOL(smp_num_siblings);
117
118/* Last level cache ID of each logical CPU */
119DEFINE_PER_CPU(u16, cpu_llc_id) = BAD_APICID;
120
121/* bitmap of online cpus */
122cpumask_t cpu_online_map __read_mostly;
123EXPORT_SYMBOL(cpu_online_map);
124
125cpumask_t cpu_callin_map;
126cpumask_t cpu_callout_map;
127cpumask_t cpu_possible_map;
128EXPORT_SYMBOL(cpu_possible_map);
129
130/* representing HT siblings of each logical CPU */
131DEFINE_PER_CPU(cpumask_t, cpu_sibling_map);
132EXPORT_PER_CPU_SYMBOL(cpu_sibling_map);
133
134/* representing HT and core siblings of each logical CPU */
135DEFINE_PER_CPU(cpumask_t, cpu_core_map);
136EXPORT_PER_CPU_SYMBOL(cpu_core_map);
137
138/* Per CPU bogomips and other parameters */
139DEFINE_PER_CPU_SHARED_ALIGNED(struct cpuinfo_x86, cpu_info);
140EXPORT_PER_CPU_SYMBOL(cpu_info);
Glauber Costa768d9502008-03-03 14:13:02 -0300141
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300142static atomic_t init_deasserted;
143
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -0300144static int boot_cpu_logical_apicid;
145
Glauber Costa91718e82008-03-03 14:13:12 -0300146/* ready for x86_64, no harm for x86, since it will overwrite after alloc */
147unsigned char *trampoline_base = __va(SMP_TRAMPOLINE_BASE);
148
Glauber Costa768d9502008-03-03 14:13:02 -0300149/* representing cpus for which sibling maps can be computed */
150static cpumask_t cpu_sibling_setup_map;
151
Glauber de Oliveira Costa1d89a7f2008-03-19 14:25:05 -0300152/* Set if we find a B stepping CPU */
153int __cpuinitdata smp_b_stepping;
Glauber de Oliveira Costa1d89a7f2008-03-19 14:25:05 -0300154
Glauber de Oliveira Costa7cc39592008-03-19 14:25:56 -0300155#if defined(CONFIG_NUMA) && defined(CONFIG_X86_32)
156
157/* which logical CPUs are on which nodes */
158cpumask_t node_to_cpumask_map[MAX_NUMNODES] __read_mostly =
159 { [0 ... MAX_NUMNODES-1] = CPU_MASK_NONE };
160EXPORT_SYMBOL(node_to_cpumask_map);
161/* which node each logical CPU is on */
162int cpu_to_node_map[NR_CPUS] __read_mostly = { [0 ... NR_CPUS-1] = 0 };
163EXPORT_SYMBOL(cpu_to_node_map);
164
165/* set up a mapping between cpu and node. */
166static void map_cpu_to_node(int cpu, int node)
167{
168 printk(KERN_INFO "Mapping cpu %d to node %d\n", cpu, node);
169 cpu_set(cpu, node_to_cpumask_map[node]);
170 cpu_to_node_map[cpu] = node;
171}
172
173/* undo a mapping between cpu and node. */
174static void unmap_cpu_to_node(int cpu)
175{
176 int node;
177
178 printk(KERN_INFO "Unmapping cpu %d from all nodes\n", cpu);
179 for (node = 0; node < MAX_NUMNODES; node++)
180 cpu_clear(cpu, node_to_cpumask_map[node]);
181 cpu_to_node_map[cpu] = 0;
182}
183#else /* !(CONFIG_NUMA && CONFIG_X86_32) */
184#define map_cpu_to_node(cpu, node) ({})
185#define unmap_cpu_to_node(cpu) ({})
186#endif
187
188#ifdef CONFIG_X86_32
189u8 cpu_2_logical_apicid[NR_CPUS] __read_mostly =
190 { [0 ... NR_CPUS-1] = BAD_APICID };
191
192void map_cpu_to_logical_apicid(void)
193{
194 int cpu = smp_processor_id();
195 int apicid = logical_smp_processor_id();
196 int node = apicid_to_node(apicid);
197
198 if (!node_online(node))
199 node = first_online_node;
200
201 cpu_2_logical_apicid[cpu] = apicid;
202 map_cpu_to_node(cpu, node);
203}
204
205void unmap_cpu_to_logical_apicid(int cpu)
206{
207 cpu_2_logical_apicid[cpu] = BAD_APICID;
208 unmap_cpu_to_node(cpu);
209}
210#else
211#define unmap_cpu_to_logical_apicid(cpu) do {} while (0)
212#define map_cpu_to_logical_apicid() do {} while (0)
213#endif
214
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300215/*
216 * Report back to the Boot Processor.
217 * Running on AP.
218 */
219void __cpuinit smp_callin(void)
220{
221 int cpuid, phys_id;
222 unsigned long timeout;
223
224 /*
225 * If waken up by an INIT in an 82489DX configuration
226 * we may get here before an INIT-deassert IPI reaches
227 * our local APIC. We have to wait for the IPI or we'll
228 * lock up on an APIC access.
229 */
230 wait_for_init_deassert(&init_deasserted);
231
232 /*
233 * (This works even if the APIC is not enabled.)
234 */
Jack Steiner05f2d122008-03-28 14:12:02 -0500235 phys_id = GET_APIC_ID(read_apic_id());
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300236 cpuid = smp_processor_id();
237 if (cpu_isset(cpuid, cpu_callin_map)) {
238 panic("%s: phys CPU#%d, CPU#%d already present??\n", __func__,
239 phys_id, cpuid);
240 }
241 Dprintk("CPU#%d (phys ID: %d) waiting for CALLOUT\n", cpuid, phys_id);
242
243 /*
244 * STARTUP IPIs are fragile beasts as they might sometimes
245 * trigger some glue motherboard logic. Complete APIC bus
246 * silence for 1 second, this overestimates the time the
247 * boot CPU is spending to send the up to 2 STARTUP IPIs
248 * by a factor of two. This should be enough.
249 */
250
251 /*
252 * Waiting 2s total for startup (udelay is not yet working)
253 */
254 timeout = jiffies + 2*HZ;
255 while (time_before(jiffies, timeout)) {
256 /*
257 * Has the boot CPU finished it's STARTUP sequence?
258 */
259 if (cpu_isset(cpuid, cpu_callout_map))
260 break;
261 cpu_relax();
262 }
263
264 if (!time_before(jiffies, timeout)) {
265 panic("%s: CPU%d started up but did not get a callout!\n",
266 __func__, cpuid);
267 }
268
269 /*
270 * the boot CPU has finished the init stage and is spinning
271 * on callin_map until we finish. We are free to set up this
272 * CPU, first the APIC. (this is probably redundant on most
273 * boards)
274 */
275
276 Dprintk("CALLIN, before setup_local_APIC().\n");
277 smp_callin_clear_local_apic();
278 setup_local_APIC();
279 end_local_APIC_setup();
280 map_cpu_to_logical_apicid();
281
282 /*
283 * Get our bogomips.
284 *
285 * Need to enable IRQs because it can take longer and then
286 * the NMI watchdog might kill us.
287 */
288 local_irq_enable();
289 calibrate_delay();
290 local_irq_disable();
291 Dprintk("Stack at about %p\n", &cpuid);
292
293 /*
294 * Save our processor parameters
295 */
296 smp_store_cpu_info(cpuid);
297
298 /*
299 * Allow the master to continue.
300 */
301 cpu_set(cpuid, cpu_callin_map);
302}
303
Glauber de Oliveira Costabbc2ff62008-03-19 14:26:00 -0300304/*
305 * Activate a secondary processor.
306 */
307void __cpuinit start_secondary(void *unused)
308{
309 /*
310 * Don't put *anything* before cpu_init(), SMP booting is too
311 * fragile that we want to limit the things done here to the
312 * most necessary things.
313 */
314#ifdef CONFIG_VMI
315 vmi_bringup();
316#endif
317 cpu_init();
318 preempt_disable();
319 smp_callin();
320
321 /* 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
328 if (nmi_watchdog == NMI_IO_APIC) {
329 disable_8259A_irq(0);
330 enable_NMI_through_LVT0();
331 enable_8259A_irq(0);
332 }
333
334 /* This must be done before setting cpu_online_map */
335 set_cpu_sibling_map(raw_smp_processor_id());
336 wmb();
337
338 /*
339 * We need to hold call_lock, so there is no inconsistency
340 * between the time smp_call_function() determines number of
341 * IPI recipients, and the time when the determination is made
342 * for which cpus receive the IPI. Holding this
343 * lock helps us to not include this cpu in a currently in progress
344 * smp_call_function().
345 */
346 lock_ipi_call_lock();
347#ifdef CONFIG_X86_64
348 spin_lock(&vector_lock);
349
350 /* Setup the per cpu irq handling data structures */
351 __setup_vector_irq(smp_processor_id());
352 /*
353 * Allow the master to continue.
354 */
355 spin_unlock(&vector_lock);
356#endif
357 cpu_set(smp_processor_id(), cpu_online_map);
358 unlock_ipi_call_lock();
359 per_cpu(cpu_state, smp_processor_id()) = CPU_ONLINE;
360
361 setup_secondary_clock();
362
363 wmb();
364 cpu_idle();
365}
366
367#ifdef CONFIG_X86_32
368/*
369 * Everything has been set up for the secondary
370 * CPUs - they just need to reload everything
371 * from the task structure
372 * This function must not return.
373 */
374void __devinit initialize_secondary(void)
375{
376 /*
377 * We don't actually need to load the full TSS,
378 * basically just the stack pointer and the ip.
379 */
380
381 asm volatile(
382 "movl %0,%%esp\n\t"
383 "jmp *%1"
384 :
385 :"m" (current->thread.sp), "m" (current->thread.ip));
386}
387#endif
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300388
Glauber de Oliveira Costa1d89a7f2008-03-19 14:25:05 -0300389static void __cpuinit smp_apply_quirks(struct cpuinfo_x86 *c)
390{
391#ifdef CONFIG_X86_32
392 /*
393 * Mask B, Pentium, but not Pentium MMX
394 */
395 if (c->x86_vendor == X86_VENDOR_INTEL &&
396 c->x86 == 5 &&
397 c->x86_mask >= 1 && c->x86_mask <= 4 &&
398 c->x86_model <= 3)
399 /*
400 * Remember we have B step Pentia with bugs
401 */
402 smp_b_stepping = 1;
403
404 /*
405 * Certain Athlons might work (for various values of 'work') in SMP
406 * but they are not certified as MP capable.
407 */
408 if ((c->x86_vendor == X86_VENDOR_AMD) && (c->x86 == 6)) {
409
410 if (num_possible_cpus() == 1)
411 goto valid_k7;
412
413 /* Athlon 660/661 is valid. */
414 if ((c->x86_model == 6) && ((c->x86_mask == 0) ||
415 (c->x86_mask == 1)))
416 goto valid_k7;
417
418 /* Duron 670 is valid */
419 if ((c->x86_model == 7) && (c->x86_mask == 0))
420 goto valid_k7;
421
422 /*
423 * Athlon 662, Duron 671, and Athlon >model 7 have capability
424 * bit. It's worth noting that the A5 stepping (662) of some
425 * Athlon XP's have the MP bit set.
426 * See http://www.heise.de/newsticker/data/jow-18.10.01-000 for
427 * more.
428 */
429 if (((c->x86_model == 6) && (c->x86_mask >= 2)) ||
430 ((c->x86_model == 7) && (c->x86_mask >= 1)) ||
431 (c->x86_model > 7))
432 if (cpu_has_mp)
433 goto valid_k7;
434
435 /* If we get here, not a certified SMP capable AMD system. */
436 add_taint(TAINT_UNSAFE_SMP);
437 }
438
439valid_k7:
440 ;
441#endif
442}
443
Glauber de Oliveira Costa693d4b82008-03-19 14:25:28 -0300444void smp_checks(void)
445{
446 if (smp_b_stepping)
447 printk(KERN_WARNING "WARNING: SMP operation may be unreliable"
448 "with B stepping processors.\n");
449
450 /*
451 * Don't taint if we are running SMP kernel on a single non-MP
452 * approved Athlon
453 */
454 if (tainted & TAINT_UNSAFE_SMP) {
Glauber de Oliveira Costaf68e00a2008-03-19 14:25:29 -0300455 if (num_online_cpus())
Glauber de Oliveira Costa693d4b82008-03-19 14:25:28 -0300456 printk(KERN_INFO "WARNING: This combination of AMD"
457 "processors is not suitable for SMP.\n");
458 else
459 tainted &= ~TAINT_UNSAFE_SMP;
460 }
461}
462
Glauber de Oliveira Costa1d89a7f2008-03-19 14:25:05 -0300463/*
464 * The bootstrap kernel entry code has set these up. Save them for
465 * a given CPU
466 */
467
468void __cpuinit smp_store_cpu_info(int id)
469{
470 struct cpuinfo_x86 *c = &cpu_data(id);
471
472 *c = boot_cpu_data;
473 c->cpu_index = id;
474 if (id != 0)
475 identify_secondary_cpu(c);
476 smp_apply_quirks(c);
477}
478
479
Glauber Costa768d9502008-03-03 14:13:02 -0300480void __cpuinit set_cpu_sibling_map(int cpu)
481{
482 int i;
483 struct cpuinfo_x86 *c = &cpu_data(cpu);
484
485 cpu_set(cpu, cpu_sibling_setup_map);
486
487 if (smp_num_siblings > 1) {
488 for_each_cpu_mask(i, cpu_sibling_setup_map) {
489 if (c->phys_proc_id == cpu_data(i).phys_proc_id &&
490 c->cpu_core_id == cpu_data(i).cpu_core_id) {
491 cpu_set(i, per_cpu(cpu_sibling_map, cpu));
492 cpu_set(cpu, per_cpu(cpu_sibling_map, i));
493 cpu_set(i, per_cpu(cpu_core_map, cpu));
494 cpu_set(cpu, per_cpu(cpu_core_map, i));
495 cpu_set(i, c->llc_shared_map);
496 cpu_set(cpu, cpu_data(i).llc_shared_map);
497 }
498 }
499 } else {
500 cpu_set(cpu, per_cpu(cpu_sibling_map, cpu));
501 }
502
503 cpu_set(cpu, c->llc_shared_map);
504
505 if (current_cpu_data.x86_max_cores == 1) {
506 per_cpu(cpu_core_map, cpu) = per_cpu(cpu_sibling_map, cpu);
507 c->booted_cores = 1;
508 return;
509 }
510
511 for_each_cpu_mask(i, cpu_sibling_setup_map) {
512 if (per_cpu(cpu_llc_id, cpu) != BAD_APICID &&
513 per_cpu(cpu_llc_id, cpu) == per_cpu(cpu_llc_id, i)) {
514 cpu_set(i, c->llc_shared_map);
515 cpu_set(cpu, cpu_data(i).llc_shared_map);
516 }
517 if (c->phys_proc_id == cpu_data(i).phys_proc_id) {
518 cpu_set(i, per_cpu(cpu_core_map, cpu));
519 cpu_set(cpu, per_cpu(cpu_core_map, i));
520 /*
521 * Does this new cpu bringup a new core?
522 */
523 if (cpus_weight(per_cpu(cpu_sibling_map, cpu)) == 1) {
524 /*
525 * for each core in package, increment
526 * the booted_cores for this new cpu
527 */
528 if (first_cpu(per_cpu(cpu_sibling_map, i)) == i)
529 c->booted_cores++;
530 /*
531 * increment the core count for all
532 * the other cpus in this package
533 */
534 if (i != cpu)
535 cpu_data(i).booted_cores++;
536 } else if (i != cpu && !c->booted_cores)
537 c->booted_cores = cpu_data(i).booted_cores;
538 }
539 }
540}
541
Glauber Costa70708a12008-03-03 14:13:03 -0300542/* maps the cpu to the sched domain representing multi-core */
543cpumask_t cpu_coregroup_map(int cpu)
544{
545 struct cpuinfo_x86 *c = &cpu_data(cpu);
546 /*
547 * For perf, we return last level cache shared map.
548 * And for power savings, we return cpu_core_map
549 */
550 if (sched_mc_power_savings || sched_smt_power_savings)
551 return per_cpu(cpu_core_map, cpu);
552 else
553 return c->llc_shared_map;
554}
555
Glauber Costa91718e82008-03-03 14:13:12 -0300556/*
557 * Currently trivial. Write the real->protected mode
558 * bootstrap into the page concerned. The caller
559 * has made sure it's suitably aligned.
560 */
561
562unsigned long __cpuinit setup_trampoline(void)
563{
564 memcpy(trampoline_base, trampoline_data,
565 trampoline_end - trampoline_data);
566 return virt_to_phys(trampoline_base);
567}
568
569#ifdef CONFIG_X86_32
570/*
571 * We are called very early to get the low memory for the
572 * SMP bootup trampoline page.
573 */
574void __init smp_alloc_memory(void)
575{
576 trampoline_base = alloc_bootmem_low_pages(PAGE_SIZE);
577 /*
578 * Has to be in very low memory so we can execute
579 * real-mode AP code.
580 */
581 if (__pa(trampoline_base) >= 0x9F000)
582 BUG();
583}
584#endif
Glauber Costa70708a12008-03-03 14:13:03 -0300585
Glauber de Oliveira Costa904541e2008-03-19 14:25:27 -0300586void impress_friends(void)
587{
588 int cpu;
589 unsigned long bogosum = 0;
590 /*
591 * Allow the user to impress friends.
592 */
593 Dprintk("Before bogomips.\n");
594 for_each_possible_cpu(cpu)
595 if (cpu_isset(cpu, cpu_callout_map))
596 bogosum += cpu_data(cpu).loops_per_jiffy;
597 printk(KERN_INFO
598 "Total of %d processors activated (%lu.%02lu BogoMIPS).\n",
Glauber de Oliveira Costaf68e00a2008-03-19 14:25:29 -0300599 num_online_cpus(),
Glauber de Oliveira Costa904541e2008-03-19 14:25:27 -0300600 bogosum/(500000/HZ),
601 (bogosum/(5000/HZ))%100);
602
603 Dprintk("Before bogocount - setting activated=1.\n");
604}
605
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300606static inline void __inquire_remote_apic(int apicid)
607{
608 unsigned i, regs[] = { APIC_ID >> 4, APIC_LVR >> 4, APIC_SPIV >> 4 };
609 char *names[] = { "ID", "VERSION", "SPIV" };
610 int timeout;
611 u32 status;
612
613 printk(KERN_INFO "Inquiring remote APIC #%d...\n", apicid);
614
615 for (i = 0; i < ARRAY_SIZE(regs); i++) {
616 printk(KERN_INFO "... APIC #%d %s: ", apicid, names[i]);
617
618 /*
619 * Wait for idle.
620 */
621 status = safe_apic_wait_icr_idle();
622 if (status)
623 printk(KERN_CONT
624 "a previous APIC delivery may have failed\n");
625
626 apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(apicid));
627 apic_write_around(APIC_ICR, APIC_DM_REMRD | regs[i]);
628
629 timeout = 0;
630 do {
631 udelay(100);
632 status = apic_read(APIC_ICR) & APIC_ICR_RR_MASK;
633 } while (status == APIC_ICR_RR_INPROG && timeout++ < 1000);
634
635 switch (status) {
636 case APIC_ICR_RR_VALID:
637 status = apic_read(APIC_RRR);
638 printk(KERN_CONT "%08x\n", status);
639 break;
640 default:
641 printk(KERN_CONT "failed\n");
642 }
643 }
644}
645
646#ifdef WAKE_SECONDARY_VIA_NMI
647/*
648 * Poke the other CPU in the eye via NMI to wake it up. Remember that the normal
649 * INIT, INIT, STARTUP sequence will reset the chip hard for us, and this
650 * won't ... remember to clear down the APIC, etc later.
651 */
652static int __devinit
653wakeup_secondary_cpu(int logical_apicid, unsigned long start_eip)
654{
655 unsigned long send_status, accept_status = 0;
656 int maxlvt;
657
658 /* Target chip */
659 apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(logical_apicid));
660
661 /* Boot on the stack */
662 /* Kick the second */
663 apic_write_around(APIC_ICR, APIC_DM_NMI | APIC_DEST_LOGICAL);
664
665 Dprintk("Waiting for send to finish...\n");
666 send_status = safe_apic_wait_icr_idle();
667
668 /*
669 * Give the other CPU some time to accept the IPI.
670 */
671 udelay(200);
672 /*
673 * Due to the Pentium erratum 3AP.
674 */
675 maxlvt = lapic_get_maxlvt();
676 if (maxlvt > 3) {
677 apic_read_around(APIC_SPIV);
678 apic_write(APIC_ESR, 0);
679 }
680 accept_status = (apic_read(APIC_ESR) & 0xEF);
681 Dprintk("NMI sent.\n");
682
683 if (send_status)
684 printk(KERN_ERR "APIC never delivered???\n");
685 if (accept_status)
686 printk(KERN_ERR "APIC delivery error (%lx).\n", accept_status);
687
688 return (send_status | accept_status);
689}
690#endif /* WAKE_SECONDARY_VIA_NMI */
691
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300692#ifdef WAKE_SECONDARY_VIA_INIT
693static int __devinit
694wakeup_secondary_cpu(int phys_apicid, unsigned long start_eip)
695{
696 unsigned long send_status, accept_status = 0;
697 int maxlvt, num_starts, j;
698
699 /*
700 * Be paranoid about clearing APIC errors.
701 */
702 if (APIC_INTEGRATED(apic_version[phys_apicid])) {
703 apic_read_around(APIC_SPIV);
704 apic_write(APIC_ESR, 0);
705 apic_read(APIC_ESR);
706 }
707
708 Dprintk("Asserting INIT.\n");
709
710 /*
711 * Turn INIT on target chip
712 */
713 apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
714
715 /*
716 * Send IPI
717 */
718 apic_write_around(APIC_ICR, APIC_INT_LEVELTRIG | APIC_INT_ASSERT
719 | APIC_DM_INIT);
720
721 Dprintk("Waiting for send to finish...\n");
722 send_status = safe_apic_wait_icr_idle();
723
724 mdelay(10);
725
726 Dprintk("Deasserting INIT.\n");
727
728 /* Target chip */
729 apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
730
731 /* Send IPI */
732 apic_write_around(APIC_ICR, APIC_INT_LEVELTRIG | APIC_DM_INIT);
733
734 Dprintk("Waiting for send to finish...\n");
735 send_status = safe_apic_wait_icr_idle();
736
737 mb();
738 atomic_set(&init_deasserted, 1);
739
740 /*
741 * Should we send STARTUP IPIs ?
742 *
743 * Determine this based on the APIC version.
744 * If we don't have an integrated APIC, don't send the STARTUP IPIs.
745 */
746 if (APIC_INTEGRATED(apic_version[phys_apicid]))
747 num_starts = 2;
748 else
749 num_starts = 0;
750
751 /*
752 * Paravirt / VMI wants a startup IPI hook here to set up the
753 * target processor state.
754 */
755 startup_ipi_hook(phys_apicid, (unsigned long) start_secondary,
756#ifdef CONFIG_X86_64
757 (unsigned long)init_rsp);
758#else
759 (unsigned long)stack_start.sp);
760#endif
761
762 /*
763 * Run STARTUP IPI loop.
764 */
765 Dprintk("#startup loops: %d.\n", num_starts);
766
767 maxlvt = lapic_get_maxlvt();
768
769 for (j = 1; j <= num_starts; j++) {
770 Dprintk("Sending STARTUP #%d.\n", j);
771 apic_read_around(APIC_SPIV);
772 apic_write(APIC_ESR, 0);
773 apic_read(APIC_ESR);
774 Dprintk("After apic_write.\n");
775
776 /*
777 * STARTUP IPI
778 */
779
780 /* Target chip */
781 apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
782
783 /* Boot on the stack */
784 /* Kick the second */
785 apic_write_around(APIC_ICR, APIC_DM_STARTUP
786 | (start_eip >> 12));
787
788 /*
789 * Give the other CPU some time to accept the IPI.
790 */
791 udelay(300);
792
793 Dprintk("Startup point 1.\n");
794
795 Dprintk("Waiting for send to finish...\n");
796 send_status = safe_apic_wait_icr_idle();
797
798 /*
799 * Give the other CPU some time to accept the IPI.
800 */
801 udelay(200);
802 /*
803 * Due to the Pentium erratum 3AP.
804 */
805 if (maxlvt > 3) {
806 apic_read_around(APIC_SPIV);
807 apic_write(APIC_ESR, 0);
808 }
809 accept_status = (apic_read(APIC_ESR) & 0xEF);
810 if (send_status || accept_status)
811 break;
812 }
813 Dprintk("After Startup.\n");
814
815 if (send_status)
816 printk(KERN_ERR "APIC never delivered???\n");
817 if (accept_status)
818 printk(KERN_ERR "APIC delivery error (%lx).\n", accept_status);
819
820 return (send_status | accept_status);
821}
822#endif /* WAKE_SECONDARY_VIA_INIT */
823
824struct create_idle {
825 struct work_struct work;
826 struct task_struct *idle;
827 struct completion done;
828 int cpu;
829};
830
831static void __cpuinit do_fork_idle(struct work_struct *work)
832{
833 struct create_idle *c_idle =
834 container_of(work, struct create_idle, work);
835
836 c_idle->idle = fork_idle(c_idle->cpu);
837 complete(&c_idle->done);
838}
839
840static int __cpuinit do_boot_cpu(int apicid, int cpu)
841/*
842 * NOTE - on most systems this is a PHYSICAL apic ID, but on multiquad
843 * (ie clustered apic addressing mode), this is a LOGICAL apic ID.
844 * Returns zero if CPU booted OK, else error code from wakeup_secondary_cpu.
845 */
846{
847 unsigned long boot_error = 0;
848 int timeout;
849 unsigned long start_ip;
850 unsigned short nmi_high = 0, nmi_low = 0;
851 struct create_idle c_idle = {
852 .cpu = cpu,
853 .done = COMPLETION_INITIALIZER_ONSTACK(c_idle.done),
854 };
855 INIT_WORK(&c_idle.work, do_fork_idle);
856#ifdef CONFIG_X86_64
857 /* allocate memory for gdts of secondary cpus. Hotplug is considered */
858 if (!cpu_gdt_descr[cpu].address &&
859 !(cpu_gdt_descr[cpu].address = get_zeroed_page(GFP_KERNEL))) {
860 printk(KERN_ERR "Failed to allocate GDT for CPU %d\n", cpu);
861 return -1;
862 }
863
864 /* Allocate node local memory for AP pdas */
865 if (cpu_pda(cpu) == &boot_cpu_pda[cpu]) {
866 struct x8664_pda *newpda, *pda;
867 int node = cpu_to_node(cpu);
868 pda = cpu_pda(cpu);
869 newpda = kmalloc_node(sizeof(struct x8664_pda), GFP_ATOMIC,
870 node);
871 if (newpda) {
872 memcpy(newpda, pda, sizeof(struct x8664_pda));
873 cpu_pda(cpu) = newpda;
874 } else
875 printk(KERN_ERR
876 "Could not allocate node local PDA for CPU %d on node %d\n",
877 cpu, node);
878 }
879#endif
880
881 alternatives_smp_switch(1);
882
883 c_idle.idle = get_idle_for_cpu(cpu);
884
885 /*
886 * We can't use kernel_thread since we must avoid to
887 * reschedule the child.
888 */
889 if (c_idle.idle) {
890 c_idle.idle->thread.sp = (unsigned long) (((struct pt_regs *)
891 (THREAD_SIZE + task_stack_page(c_idle.idle))) - 1);
892 init_idle(c_idle.idle, cpu);
893 goto do_rest;
894 }
895
896 if (!keventd_up() || current_is_keventd())
897 c_idle.work.func(&c_idle.work);
898 else {
899 schedule_work(&c_idle.work);
900 wait_for_completion(&c_idle.done);
901 }
902
903 if (IS_ERR(c_idle.idle)) {
904 printk("failed fork for CPU %d\n", cpu);
905 return PTR_ERR(c_idle.idle);
906 }
907
908 set_idle_for_cpu(cpu, c_idle.idle);
909do_rest:
910#ifdef CONFIG_X86_32
911 per_cpu(current_task, cpu) = c_idle.idle;
912 init_gdt(cpu);
913 early_gdt_descr.address = (unsigned long)get_cpu_gdt_table(cpu);
914 c_idle.idle->thread.ip = (unsigned long) start_secondary;
915 /* Stack for startup_32 can be just as for start_secondary onwards */
916 stack_start.sp = (void *) c_idle.idle->thread.sp;
917 irq_ctx_init(cpu);
918#else
919 cpu_pda(cpu)->pcurrent = c_idle.idle;
920 init_rsp = c_idle.idle->thread.sp;
921 load_sp0(&per_cpu(init_tss, cpu), &c_idle.idle->thread);
922 initial_code = (unsigned long)start_secondary;
923 clear_tsk_thread_flag(c_idle.idle, TIF_FORK);
924#endif
925
926 /* start_ip had better be page-aligned! */
927 start_ip = setup_trampoline();
928
929 /* So we see what's up */
930 printk(KERN_INFO "Booting processor %d/%d ip %lx\n",
931 cpu, apicid, start_ip);
932
933 /*
934 * This grunge runs the startup process for
935 * the targeted processor.
936 */
937
938 atomic_set(&init_deasserted, 0);
939
940 Dprintk("Setting warm reset code and vector.\n");
941
942 store_NMI_vector(&nmi_high, &nmi_low);
943
944 smpboot_setup_warm_reset_vector(start_ip);
945 /*
946 * Be paranoid about clearing APIC errors.
947 */
948 apic_write(APIC_ESR, 0);
949 apic_read(APIC_ESR);
950
Glauber de Oliveira Costacb3c8b92008-03-19 14:25:59 -0300951 /*
952 * Starting actual IPI sequence...
953 */
954 boot_error = wakeup_secondary_cpu(apicid, start_ip);
955
956 if (!boot_error) {
957 /*
958 * allow APs to start initializing.
959 */
960 Dprintk("Before Callout %d.\n", cpu);
961 cpu_set(cpu, cpu_callout_map);
962 Dprintk("After Callout %d.\n", cpu);
963
964 /*
965 * Wait 5s total for a response
966 */
967 for (timeout = 0; timeout < 50000; timeout++) {
968 if (cpu_isset(cpu, cpu_callin_map))
969 break; /* It has booted */
970 udelay(100);
971 }
972
973 if (cpu_isset(cpu, cpu_callin_map)) {
974 /* number CPUs logically, starting from 1 (BSP is 0) */
975 Dprintk("OK.\n");
976 printk(KERN_INFO "CPU%d: ", cpu);
977 print_cpu_info(&cpu_data(cpu));
978 Dprintk("CPU has booted.\n");
979 } else {
980 boot_error = 1;
981 if (*((volatile unsigned char *)trampoline_base)
982 == 0xA5)
983 /* trampoline started but...? */
984 printk(KERN_ERR "Stuck ??\n");
985 else
986 /* trampoline code not run */
987 printk(KERN_ERR "Not responding.\n");
988 inquire_remote_apic(apicid);
989 }
990 }
991
992 if (boot_error) {
993 /* Try to put things back the way they were before ... */
994 unmap_cpu_to_logical_apicid(cpu);
995#ifdef CONFIG_X86_64
996 clear_node_cpumask(cpu); /* was set by numa_add_cpu */
997#endif
998 cpu_clear(cpu, cpu_callout_map); /* was set by do_boot_cpu() */
999 cpu_clear(cpu, cpu_initialized); /* was set by cpu_init() */
1000 cpu_clear(cpu, cpu_possible_map);
1001 cpu_clear(cpu, cpu_present_map);
1002 per_cpu(x86_cpu_to_apicid, cpu) = BAD_APICID;
1003 }
1004
1005 /* mark "stuck" area as not stuck */
1006 *((volatile unsigned long *)trampoline_base) = 0;
1007
1008 return boot_error;
1009}
1010
1011int __cpuinit native_cpu_up(unsigned int cpu)
1012{
1013 int apicid = cpu_present_to_apicid(cpu);
1014 unsigned long flags;
1015 int err;
1016
1017 WARN_ON(irqs_disabled());
1018
1019 Dprintk("++++++++++++++++++++=_---CPU UP %u\n", cpu);
1020
1021 if (apicid == BAD_APICID || apicid == boot_cpu_physical_apicid ||
1022 !physid_isset(apicid, phys_cpu_present_map)) {
1023 printk(KERN_ERR "%s: bad cpu %d\n", __func__, cpu);
1024 return -EINVAL;
1025 }
1026
1027 /*
1028 * Already booted CPU?
1029 */
1030 if (cpu_isset(cpu, cpu_callin_map)) {
1031 Dprintk("do_boot_cpu %d Already started\n", cpu);
1032 return -ENOSYS;
1033 }
1034
1035 /*
1036 * Save current MTRR state in case it was changed since early boot
1037 * (e.g. by the ACPI SMI) to initialize new CPUs with MTRRs in sync:
1038 */
1039 mtrr_save_state();
1040
1041 per_cpu(cpu_state, cpu) = CPU_UP_PREPARE;
1042
1043#ifdef CONFIG_X86_32
1044 /* init low mem mapping */
1045 clone_pgd_range(swapper_pg_dir, swapper_pg_dir + USER_PGD_PTRS,
1046 min_t(unsigned long, KERNEL_PGD_PTRS, USER_PGD_PTRS));
1047 flush_tlb_all();
1048#endif
1049
1050 err = do_boot_cpu(apicid, cpu);
1051 if (err < 0) {
1052 Dprintk("do_boot_cpu failed %d\n", err);
1053 return err;
1054 }
1055
1056 /*
1057 * Check TSC synchronization with the AP (keep irqs disabled
1058 * while doing so):
1059 */
1060 local_irq_save(flags);
1061 check_tsc_sync_source(cpu);
1062 local_irq_restore(flags);
1063
1064 while (!cpu_isset(cpu, cpu_online_map)) {
1065 cpu_relax();
1066 touch_nmi_watchdog();
1067 }
1068
1069 return 0;
1070}
1071
Glauber de Oliveira Costaa8db8452008-03-19 14:26:01 -03001072/*
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001073 * Fall back to non SMP mode after errors.
1074 *
1075 * RED-PEN audit/test this more. I bet there is more state messed up here.
1076 */
1077static __init void disable_smp(void)
1078{
1079 cpu_present_map = cpumask_of_cpu(0);
1080 cpu_possible_map = cpumask_of_cpu(0);
1081#ifdef CONFIG_X86_32
1082 smpboot_clear_io_apic_irqs();
1083#endif
1084 if (smp_found_config)
1085 phys_cpu_present_map =
1086 physid_mask_of_physid(boot_cpu_physical_apicid);
1087 else
1088 phys_cpu_present_map = physid_mask_of_physid(0);
1089 map_cpu_to_logical_apicid();
1090 cpu_set(0, per_cpu(cpu_sibling_map, 0));
1091 cpu_set(0, per_cpu(cpu_core_map, 0));
1092}
1093
1094/*
1095 * Various sanity checks.
1096 */
1097static int __init smp_sanity_check(unsigned max_cpus)
1098{
Jack Steinerac23d4e2008-03-28 14:12:16 -05001099 preempt_disable();
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001100 if (!physid_isset(hard_smp_processor_id(), phys_cpu_present_map)) {
1101 printk(KERN_WARNING "weird, boot CPU (#%d) not listed"
1102 "by the BIOS.\n", hard_smp_processor_id());
1103 physid_set(hard_smp_processor_id(), phys_cpu_present_map);
1104 }
1105
1106 /*
1107 * If we couldn't find an SMP configuration at boot time,
1108 * get out of here now!
1109 */
1110 if (!smp_found_config && !acpi_lapic) {
Jack Steinerac23d4e2008-03-28 14:12:16 -05001111 preempt_enable();
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001112 printk(KERN_NOTICE "SMP motherboard not detected.\n");
1113 disable_smp();
1114 if (APIC_init_uniprocessor())
1115 printk(KERN_NOTICE "Local APIC not detected."
1116 " Using dummy APIC emulation.\n");
1117 return -1;
1118 }
1119
1120 /*
1121 * Should not be necessary because the MP table should list the boot
1122 * CPU too, but we do it for the sake of robustness anyway.
1123 */
1124 if (!check_phys_apicid_present(boot_cpu_physical_apicid)) {
1125 printk(KERN_NOTICE
1126 "weird, boot CPU (#%d) not listed by the BIOS.\n",
1127 boot_cpu_physical_apicid);
1128 physid_set(hard_smp_processor_id(), phys_cpu_present_map);
1129 }
Jack Steinerac23d4e2008-03-28 14:12:16 -05001130 preempt_enable();
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001131
1132 /*
1133 * If we couldn't find a local APIC, then get out of here now!
1134 */
1135 if (APIC_INTEGRATED(apic_version[boot_cpu_physical_apicid]) &&
1136 !cpu_has_apic) {
1137 printk(KERN_ERR "BIOS bug, local APIC #%d not detected!...\n",
1138 boot_cpu_physical_apicid);
1139 printk(KERN_ERR "... forcing use of dummy APIC emulation."
1140 "(tell your hw vendor)\n");
1141 smpboot_clear_io_apic();
1142 return -1;
1143 }
1144
1145 verify_local_APIC();
1146
1147 /*
1148 * If SMP should be disabled, then really disable it!
1149 */
1150 if (!max_cpus) {
1151 printk(KERN_INFO "SMP mode deactivated,"
1152 "forcing use of dummy APIC emulation.\n");
1153 smpboot_clear_io_apic();
1154#ifdef CONFIG_X86_32
1155 if (nmi_watchdog == NMI_LOCAL_APIC) {
1156 printk(KERN_INFO "activating minimal APIC for"
1157 "NMI watchdog use.\n");
1158 connect_bsp_APIC();
1159 setup_local_APIC();
1160 end_local_APIC_setup();
1161 }
1162#endif
1163 return -1;
1164 }
1165
1166 return 0;
1167}
1168
1169static void __init smp_cpu_index_default(void)
1170{
1171 int i;
1172 struct cpuinfo_x86 *c;
1173
1174 for_each_cpu_mask(i, cpu_possible_map) {
1175 c = &cpu_data(i);
1176 /* mark all to hotplug */
1177 c->cpu_index = NR_CPUS;
1178 }
1179}
1180
1181/*
1182 * Prepare for SMP bootup. The MP table or ACPI has been read
1183 * earlier. Just do some sanity checking here and enable APIC mode.
1184 */
1185void __init native_smp_prepare_cpus(unsigned int max_cpus)
1186{
1187 nmi_watchdog_default();
1188 smp_cpu_index_default();
1189 current_cpu_data = boot_cpu_data;
1190 cpu_callin_map = cpumask_of_cpu(0);
1191 mb();
1192 /*
1193 * Setup boot CPU information
1194 */
1195 smp_store_cpu_info(0); /* Final full version of the data */
1196 boot_cpu_logical_apicid = logical_smp_processor_id();
1197 current_thread_info()->cpu = 0; /* needed? */
1198 set_cpu_sibling_map(0);
1199
1200 if (smp_sanity_check(max_cpus) < 0) {
1201 printk(KERN_INFO "SMP disabled\n");
1202 disable_smp();
1203 return;
1204 }
1205
Jack Steinerac23d4e2008-03-28 14:12:16 -05001206 preempt_disable();
Jack Steiner05f2d122008-03-28 14:12:02 -05001207 if (GET_APIC_ID(read_apic_id()) != boot_cpu_physical_apicid) {
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001208 panic("Boot APIC ID in local APIC unexpected (%d vs %d)",
Jack Steiner05f2d122008-03-28 14:12:02 -05001209 GET_APIC_ID(read_apic_id()), boot_cpu_physical_apicid);
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001210 /* Or can we switch back to PIC here? */
1211 }
Jack Steinerac23d4e2008-03-28 14:12:16 -05001212 preempt_enable();
Glauber de Oliveira Costa8aef1352008-03-19 14:26:11 -03001213
1214#ifdef CONFIG_X86_32
1215 connect_bsp_APIC();
1216#endif
1217 /*
1218 * Switch from PIC to APIC mode.
1219 */
1220 setup_local_APIC();
1221
1222#ifdef CONFIG_X86_64
1223 /*
1224 * Enable IO APIC before setting up error vector
1225 */
1226 if (!skip_ioapic_setup && nr_ioapics)
1227 enable_IO_APIC();
1228#endif
1229 end_local_APIC_setup();
1230
1231 map_cpu_to_logical_apicid();
1232
1233 setup_portio_remap();
1234
1235 smpboot_setup_io_apic();
1236 /*
1237 * Set up local APIC timer on boot CPU.
1238 */
1239
1240 printk(KERN_INFO "CPU%d: ", 0);
1241 print_cpu_info(&cpu_data(0));
1242 setup_boot_clock();
1243}
1244/*
Glauber de Oliveira Costaa8db8452008-03-19 14:26:01 -03001245 * Early setup to make printk work.
1246 */
1247void __init native_smp_prepare_boot_cpu(void)
1248{
1249 int me = smp_processor_id();
1250#ifdef CONFIG_X86_32
1251 init_gdt(me);
1252 switch_to_new_gdt();
1253#endif
1254 /* already set me in cpu_online_map in boot_cpu_init() */
1255 cpu_set(me, cpu_callout_map);
1256 per_cpu(cpu_state, me) = CPU_ONLINE;
1257}
1258
Glauber de Oliveira Costa83f7eb92008-03-19 14:26:02 -03001259void __init native_smp_cpus_done(unsigned int max_cpus)
1260{
1261 /*
1262 * Cleanup possible dangling ends...
1263 */
1264 smpboot_restore_warm_reset_vector();
1265
1266 Dprintk("Boot done.\n");
1267
1268 impress_friends();
1269 smp_checks();
1270#ifdef CONFIG_X86_IO_APIC
1271 setup_ioapic_dest();
1272#endif
1273 check_nmi_watchdog();
1274#ifdef CONFIG_X86_32
1275 zap_low_mappings();
1276#endif
1277}
1278
Glauber Costa68a1c3f2008-03-03 14:12:42 -03001279#ifdef CONFIG_HOTPLUG_CPU
Glauber de Oliveira Costa2cd9fb72008-03-19 14:26:12 -03001280
1281# ifdef CONFIG_X86_32
1282void cpu_exit_clear(void)
1283{
1284 int cpu = raw_smp_processor_id();
1285
1286 idle_task_exit();
1287
1288 cpu_uninit();
1289 irq_ctx_exit(cpu);
1290
1291 cpu_clear(cpu, cpu_callout_map);
1292 cpu_clear(cpu, cpu_callin_map);
1293
1294 unmap_cpu_to_logical_apicid(cpu);
1295}
1296# endif /* CONFIG_X86_32 */
1297
Glauber Costa768d9502008-03-03 14:13:02 -03001298void remove_siblinginfo(int cpu)
1299{
1300 int sibling;
1301 struct cpuinfo_x86 *c = &cpu_data(cpu);
1302
1303 for_each_cpu_mask(sibling, per_cpu(cpu_core_map, cpu)) {
1304 cpu_clear(cpu, per_cpu(cpu_core_map, sibling));
1305 /*/
1306 * last thread sibling in this cpu core going down
1307 */
1308 if (cpus_weight(per_cpu(cpu_sibling_map, cpu)) == 1)
1309 cpu_data(sibling).booted_cores--;
1310 }
1311
1312 for_each_cpu_mask(sibling, per_cpu(cpu_sibling_map, cpu))
1313 cpu_clear(cpu, per_cpu(cpu_sibling_map, sibling));
1314 cpus_clear(per_cpu(cpu_sibling_map, cpu));
1315 cpus_clear(per_cpu(cpu_core_map, cpu));
1316 c->phys_proc_id = 0;
1317 c->cpu_core_id = 0;
1318 cpu_clear(cpu, cpu_sibling_setup_map);
1319}
Glauber Costa68a1c3f2008-03-03 14:12:42 -03001320
1321int additional_cpus __initdata = -1;
1322
1323static __init int setup_additional_cpus(char *s)
1324{
1325 return s && get_option(&s, &additional_cpus) ? 0 : -EINVAL;
1326}
1327early_param("additional_cpus", setup_additional_cpus);
1328
1329/*
1330 * cpu_possible_map should be static, it cannot change as cpu's
1331 * are onlined, or offlined. The reason is per-cpu data-structures
1332 * are allocated by some modules at init time, and dont expect to
1333 * do this dynamically on cpu arrival/departure.
1334 * cpu_present_map on the other hand can change dynamically.
1335 * In case when cpu_hotplug is not compiled, then we resort to current
1336 * behaviour, which is cpu_possible == cpu_present.
1337 * - Ashok Raj
1338 *
1339 * Three ways to find out the number of additional hotplug CPUs:
1340 * - If the BIOS specified disabled CPUs in ACPI/mptables use that.
1341 * - The user can overwrite it with additional_cpus=NUM
1342 * - Otherwise don't reserve additional CPUs.
1343 * We do this because additional CPUs waste a lot of memory.
1344 * -AK
1345 */
1346__init void prefill_possible_map(void)
1347{
1348 int i;
1349 int possible;
1350
1351 if (additional_cpus == -1) {
1352 if (disabled_cpus > 0)
1353 additional_cpus = disabled_cpus;
1354 else
1355 additional_cpus = 0;
1356 }
1357 possible = num_processors + additional_cpus;
1358 if (possible > NR_CPUS)
1359 possible = NR_CPUS;
1360
1361 printk(KERN_INFO "SMP: Allowing %d CPUs, %d hotplug CPUs\n",
1362 possible, max_t(int, possible - num_processors, 0));
1363
1364 for (i = 0; i < possible; i++)
1365 cpu_set(i, cpu_possible_map);
1366}
Glauber Costa69c18c12008-03-03 14:13:07 -03001367
1368static void __ref remove_cpu_from_maps(int cpu)
1369{
1370 cpu_clear(cpu, cpu_online_map);
1371#ifdef CONFIG_X86_64
1372 cpu_clear(cpu, cpu_callout_map);
1373 cpu_clear(cpu, cpu_callin_map);
1374 /* was set by cpu_init() */
1375 clear_bit(cpu, (unsigned long *)&cpu_initialized);
1376 clear_node_cpumask(cpu);
1377#endif
1378}
1379
1380int __cpu_disable(void)
1381{
1382 int cpu = smp_processor_id();
1383
1384 /*
1385 * Perhaps use cpufreq to drop frequency, but that could go
1386 * into generic code.
1387 *
1388 * We won't take down the boot processor on i386 due to some
1389 * interrupts only being able to be serviced by the BSP.
1390 * Especially so if we're not using an IOAPIC -zwane
1391 */
1392 if (cpu == 0)
1393 return -EBUSY;
1394
1395 if (nmi_watchdog == NMI_LOCAL_APIC)
1396 stop_apic_nmi_watchdog(NULL);
1397 clear_local_APIC();
1398
1399 /*
1400 * HACK:
1401 * Allow any queued timer interrupts to get serviced
1402 * This is only a temporary solution until we cleanup
1403 * fixup_irqs as we do for IA64.
1404 */
1405 local_irq_enable();
1406 mdelay(1);
1407
1408 local_irq_disable();
1409 remove_siblinginfo(cpu);
1410
1411 /* It's now safe to remove this processor from the online map */
1412 remove_cpu_from_maps(cpu);
1413 fixup_irqs(cpu_online_map);
1414 return 0;
1415}
1416
1417void __cpu_die(unsigned int cpu)
1418{
1419 /* We don't do anything here: idle task is faking death itself. */
1420 unsigned int i;
1421
1422 for (i = 0; i < 10; i++) {
1423 /* They ack this in play_dead by setting CPU_DEAD */
1424 if (per_cpu(cpu_state, cpu) == CPU_DEAD) {
1425 printk(KERN_INFO "CPU %d is now offline\n", cpu);
1426 if (1 == num_online_cpus())
1427 alternatives_smp_switch(0);
1428 return;
1429 }
1430 msleep(100);
1431 }
1432 printk(KERN_ERR "CPU %u didn't die...\n", cpu);
1433}
1434#else /* ... !CONFIG_HOTPLUG_CPU */
1435int __cpu_disable(void)
1436{
1437 return -ENOSYS;
1438}
1439
1440void __cpu_die(unsigned int cpu)
1441{
1442 /* We said "no" in __cpu_disable */
1443 BUG();
1444}
Glauber Costa68a1c3f2008-03-03 14:12:42 -03001445#endif
1446
Glauber Costa89b08202008-03-03 14:13:08 -03001447/*
1448 * If the BIOS enumerates physical processors before logical,
1449 * maxcpus=N at enumeration-time can be used to disable HT.
1450 */
1451static int __init parse_maxcpus(char *arg)
1452{
1453 extern unsigned int maxcpus;
1454
1455 maxcpus = simple_strtoul(arg, NULL, 0);
1456 return 0;
1457}
1458early_param("maxcpus", parse_maxcpus);