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