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