[PATCH] x86: Save the MTRRs of the BSP before booting an AP
[deliverable/linux.git] / arch / x86_64 / kernel / smpboot.c
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
16 *
17 * Fixes
18 * Felix Koop : NR_CPUS used properly
19 * Jose Renau : Handle single CPU case.
20 * Alan Cox : By repeated request 8) - Total BogoMIP report.
21 * Greg Wright : Fix for kernel stacks panic.
22 * Erich Boleyn : MP v1.4 and additional changes.
23 * Matthias Sattler : Changes for 2.1 kernel map.
24 * Michel Lespinasse : Changes for 2.1 kernel map.
25 * Michael Chastain : Change trampoline.S to gnu as.
26 * Alan Cox : Dumb bug: 'B' step PPro's are fine
27 * Ingo Molnar : Added APIC timers, based on code
28 * from Jose Renau
29 * Ingo Molnar : various cleanups and rewrites
30 * Tigran Aivazian : fixed "0.00 in /proc/uptime on SMP" bug.
31 * Maciej W. Rozycki : Bits for genuine 82489DX APICs
32 * Andi Kleen : Changed for SMP boot into long mode.
33 * Rusty Russell : Hacked into shape for new "hotplug" boot process.
34 * Andi Kleen : Converted to new state machine.
35 * Various cleanups.
36 * Probably mostly hotplug CPU ready now.
37 * Ashok Raj : CPU hotplug support
38 */
39
40
41 #include <linux/init.h>
42
43 #include <linux/mm.h>
44 #include <linux/kernel_stat.h>
45 #include <linux/smp_lock.h>
46 #include <linux/bootmem.h>
47 #include <linux/thread_info.h>
48 #include <linux/module.h>
49 #include <linux/delay.h>
50 #include <linux/mc146818rtc.h>
51 #include <linux/smp.h>
52
53 #include <asm/mtrr.h>
54 #include <asm/pgalloc.h>
55 #include <asm/desc.h>
56 #include <asm/kdebug.h>
57 #include <asm/tlbflush.h>
58 #include <asm/proto.h>
59 #include <asm/nmi.h>
60 #include <asm/irq.h>
61 #include <asm/hw_irq.h>
62 #include <asm/numa.h>
63
64 /* Number of siblings per CPU package */
65 int smp_num_siblings = 1;
66 EXPORT_SYMBOL(smp_num_siblings);
67
68 /* Last level cache ID of each logical CPU */
69 u8 cpu_llc_id[NR_CPUS] __cpuinitdata = {[0 ... NR_CPUS-1] = BAD_APICID};
70 EXPORT_SYMBOL(cpu_llc_id);
71
72 /* Bitmask of currently online CPUs */
73 cpumask_t cpu_online_map __read_mostly;
74
75 EXPORT_SYMBOL(cpu_online_map);
76
77 /*
78 * Private maps to synchronize booting between AP and BP.
79 * Probably not needed anymore, but it makes for easier debugging. -AK
80 */
81 cpumask_t cpu_callin_map;
82 cpumask_t cpu_callout_map;
83 EXPORT_SYMBOL(cpu_callout_map);
84
85 cpumask_t cpu_possible_map;
86 EXPORT_SYMBOL(cpu_possible_map);
87
88 /* Per CPU bogomips and other parameters */
89 struct cpuinfo_x86 cpu_data[NR_CPUS] __cacheline_aligned;
90 EXPORT_SYMBOL(cpu_data);
91
92 /* Set when the idlers are all forked */
93 int smp_threads_ready;
94
95 /* representing HT siblings of each logical CPU */
96 cpumask_t cpu_sibling_map[NR_CPUS] __read_mostly;
97 EXPORT_SYMBOL(cpu_sibling_map);
98
99 /* representing HT and core siblings of each logical CPU */
100 cpumask_t cpu_core_map[NR_CPUS] __read_mostly;
101 EXPORT_SYMBOL(cpu_core_map);
102
103 /*
104 * Trampoline 80x86 program as an array.
105 */
106
107 extern unsigned char trampoline_data[];
108 extern unsigned char trampoline_end[];
109
110 /* State of each CPU */
111 DEFINE_PER_CPU(int, cpu_state) = { 0 };
112
113 /*
114 * Store all idle threads, this can be reused instead of creating
115 * a new thread. Also avoids complicated thread destroy functionality
116 * for idle threads.
117 */
118 struct task_struct *idle_thread_array[NR_CPUS] __cpuinitdata ;
119
120 #define get_idle_for_cpu(x) (idle_thread_array[(x)])
121 #define set_idle_for_cpu(x,p) (idle_thread_array[(x)] = (p))
122
123 /*
124 * Currently trivial. Write the real->protected mode
125 * bootstrap into the page concerned. The caller
126 * has made sure it's suitably aligned.
127 */
128
129 static unsigned long __cpuinit setup_trampoline(void)
130 {
131 void *tramp = __va(SMP_TRAMPOLINE_BASE);
132 memcpy(tramp, trampoline_data, trampoline_end - trampoline_data);
133 return virt_to_phys(tramp);
134 }
135
136 /*
137 * The bootstrap kernel entry code has set these up. Save them for
138 * a given CPU
139 */
140
141 static void __cpuinit smp_store_cpu_info(int id)
142 {
143 struct cpuinfo_x86 *c = cpu_data + id;
144
145 *c = boot_cpu_data;
146 identify_cpu(c);
147 print_cpu_info(c);
148 }
149
150 static atomic_t init_deasserted __cpuinitdata;
151
152 /*
153 * Report back to the Boot Processor.
154 * Running on AP.
155 */
156 void __cpuinit smp_callin(void)
157 {
158 int cpuid, phys_id;
159 unsigned long timeout;
160
161 /*
162 * If waken up by an INIT in an 82489DX configuration
163 * we may get here before an INIT-deassert IPI reaches
164 * our local APIC. We have to wait for the IPI or we'll
165 * lock up on an APIC access.
166 */
167 while (!atomic_read(&init_deasserted))
168 cpu_relax();
169
170 /*
171 * (This works even if the APIC is not enabled.)
172 */
173 phys_id = GET_APIC_ID(apic_read(APIC_ID));
174 cpuid = smp_processor_id();
175 if (cpu_isset(cpuid, cpu_callin_map)) {
176 panic("smp_callin: phys CPU#%d, CPU#%d already present??\n",
177 phys_id, cpuid);
178 }
179 Dprintk("CPU#%d (phys ID: %d) waiting for CALLOUT\n", cpuid, phys_id);
180
181 /*
182 * STARTUP IPIs are fragile beasts as they might sometimes
183 * trigger some glue motherboard logic. Complete APIC bus
184 * silence for 1 second, this overestimates the time the
185 * boot CPU is spending to send the up to 2 STARTUP IPIs
186 * by a factor of two. This should be enough.
187 */
188
189 /*
190 * Waiting 2s total for startup (udelay is not yet working)
191 */
192 timeout = jiffies + 2*HZ;
193 while (time_before(jiffies, timeout)) {
194 /*
195 * Has the boot CPU finished it's STARTUP sequence?
196 */
197 if (cpu_isset(cpuid, cpu_callout_map))
198 break;
199 cpu_relax();
200 }
201
202 if (!time_before(jiffies, timeout)) {
203 panic("smp_callin: CPU%d started up but did not get a callout!\n",
204 cpuid);
205 }
206
207 /*
208 * the boot CPU has finished the init stage and is spinning
209 * on callin_map until we finish. We are free to set up this
210 * CPU, first the APIC. (this is probably redundant on most
211 * boards)
212 */
213
214 Dprintk("CALLIN, before setup_local_APIC().\n");
215 setup_local_APIC();
216
217 /*
218 * Get our bogomips.
219 *
220 * Need to enable IRQs because it can take longer and then
221 * the NMI watchdog might kill us.
222 */
223 local_irq_enable();
224 calibrate_delay();
225 local_irq_disable();
226 Dprintk("Stack at about %p\n",&cpuid);
227
228 disable_APIC_timer();
229
230 /*
231 * Save our processor parameters
232 */
233 smp_store_cpu_info(cpuid);
234
235 /*
236 * Allow the master to continue.
237 */
238 cpu_set(cpuid, cpu_callin_map);
239 }
240
241 /* maps the cpu to the sched domain representing multi-core */
242 cpumask_t cpu_coregroup_map(int cpu)
243 {
244 struct cpuinfo_x86 *c = cpu_data + cpu;
245 /*
246 * For perf, we return last level cache shared map.
247 * And for power savings, we return cpu_core_map
248 */
249 if (sched_mc_power_savings || sched_smt_power_savings)
250 return cpu_core_map[cpu];
251 else
252 return c->llc_shared_map;
253 }
254
255 /* representing cpus for which sibling maps can be computed */
256 static cpumask_t cpu_sibling_setup_map;
257
258 static inline void set_cpu_sibling_map(int cpu)
259 {
260 int i;
261 struct cpuinfo_x86 *c = cpu_data;
262
263 cpu_set(cpu, cpu_sibling_setup_map);
264
265 if (smp_num_siblings > 1) {
266 for_each_cpu_mask(i, cpu_sibling_setup_map) {
267 if (c[cpu].phys_proc_id == c[i].phys_proc_id &&
268 c[cpu].cpu_core_id == c[i].cpu_core_id) {
269 cpu_set(i, cpu_sibling_map[cpu]);
270 cpu_set(cpu, cpu_sibling_map[i]);
271 cpu_set(i, cpu_core_map[cpu]);
272 cpu_set(cpu, cpu_core_map[i]);
273 cpu_set(i, c[cpu].llc_shared_map);
274 cpu_set(cpu, c[i].llc_shared_map);
275 }
276 }
277 } else {
278 cpu_set(cpu, cpu_sibling_map[cpu]);
279 }
280
281 cpu_set(cpu, c[cpu].llc_shared_map);
282
283 if (current_cpu_data.x86_max_cores == 1) {
284 cpu_core_map[cpu] = cpu_sibling_map[cpu];
285 c[cpu].booted_cores = 1;
286 return;
287 }
288
289 for_each_cpu_mask(i, cpu_sibling_setup_map) {
290 if (cpu_llc_id[cpu] != BAD_APICID &&
291 cpu_llc_id[cpu] == cpu_llc_id[i]) {
292 cpu_set(i, c[cpu].llc_shared_map);
293 cpu_set(cpu, c[i].llc_shared_map);
294 }
295 if (c[cpu].phys_proc_id == c[i].phys_proc_id) {
296 cpu_set(i, cpu_core_map[cpu]);
297 cpu_set(cpu, cpu_core_map[i]);
298 /*
299 * Does this new cpu bringup a new core?
300 */
301 if (cpus_weight(cpu_sibling_map[cpu]) == 1) {
302 /*
303 * for each core in package, increment
304 * the booted_cores for this new cpu
305 */
306 if (first_cpu(cpu_sibling_map[i]) == i)
307 c[cpu].booted_cores++;
308 /*
309 * increment the core count for all
310 * the other cpus in this package
311 */
312 if (i != cpu)
313 c[i].booted_cores++;
314 } else if (i != cpu && !c[cpu].booted_cores)
315 c[cpu].booted_cores = c[i].booted_cores;
316 }
317 }
318 }
319
320 /*
321 * Setup code on secondary processor (after comming out of the trampoline)
322 */
323 void __cpuinit start_secondary(void)
324 {
325 /*
326 * Dont put anything before smp_callin(), SMP
327 * booting is too fragile that we want to limit the
328 * things done here to the most necessary things.
329 */
330 cpu_init();
331 preempt_disable();
332 smp_callin();
333
334 /* otherwise gcc will move up the smp_processor_id before the cpu_init */
335 barrier();
336
337 /*
338 * Check TSC sync first:
339 */
340 check_tsc_sync_target();
341
342 Dprintk("cpu %d: setting up apic clock\n", smp_processor_id());
343 setup_secondary_APIC_clock();
344
345 Dprintk("cpu %d: enabling apic timer\n", smp_processor_id());
346
347 if (nmi_watchdog == NMI_IO_APIC) {
348 disable_8259A_irq(0);
349 enable_NMI_through_LVT0(NULL);
350 enable_8259A_irq(0);
351 }
352
353 enable_APIC_timer();
354
355 /*
356 * The sibling maps must be set before turing the online map on for
357 * this cpu
358 */
359 set_cpu_sibling_map(smp_processor_id());
360
361 /*
362 * We need to hold call_lock, so there is no inconsistency
363 * between the time smp_call_function() determines number of
364 * IPI receipients, and the time when the determination is made
365 * for which cpus receive the IPI in genapic_flat.c. Holding this
366 * lock helps us to not include this cpu in a currently in progress
367 * smp_call_function().
368 */
369 lock_ipi_call_lock();
370 spin_lock(&vector_lock);
371
372 /* Setup the per cpu irq handling data structures */
373 __setup_vector_irq(smp_processor_id());
374 /*
375 * Allow the master to continue.
376 */
377 cpu_set(smp_processor_id(), cpu_online_map);
378 per_cpu(cpu_state, smp_processor_id()) = CPU_ONLINE;
379 spin_unlock(&vector_lock);
380
381 unlock_ipi_call_lock();
382
383 cpu_idle();
384 }
385
386 extern volatile unsigned long init_rsp;
387 extern void (*initial_code)(void);
388
389 #ifdef APIC_DEBUG
390 static void inquire_remote_apic(int apicid)
391 {
392 unsigned i, regs[] = { APIC_ID >> 4, APIC_LVR >> 4, APIC_SPIV >> 4 };
393 char *names[] = { "ID", "VERSION", "SPIV" };
394 int timeout, status;
395
396 printk(KERN_INFO "Inquiring remote APIC #%d...\n", apicid);
397
398 for (i = 0; i < sizeof(regs) / sizeof(*regs); i++) {
399 printk("... APIC #%d %s: ", apicid, names[i]);
400
401 /*
402 * Wait for idle.
403 */
404 apic_wait_icr_idle();
405
406 apic_write(APIC_ICR2, SET_APIC_DEST_FIELD(apicid));
407 apic_write(APIC_ICR, APIC_DM_REMRD | regs[i]);
408
409 timeout = 0;
410 do {
411 udelay(100);
412 status = apic_read(APIC_ICR) & APIC_ICR_RR_MASK;
413 } while (status == APIC_ICR_RR_INPROG && timeout++ < 1000);
414
415 switch (status) {
416 case APIC_ICR_RR_VALID:
417 status = apic_read(APIC_RRR);
418 printk("%08x\n", status);
419 break;
420 default:
421 printk("failed\n");
422 }
423 }
424 }
425 #endif
426
427 /*
428 * Kick the secondary to wake up.
429 */
430 static int __cpuinit wakeup_secondary_via_INIT(int phys_apicid, unsigned int start_rip)
431 {
432 unsigned long send_status = 0, accept_status = 0;
433 int maxlvt, timeout, num_starts, j;
434
435 Dprintk("Asserting INIT.\n");
436
437 /*
438 * Turn INIT on target chip
439 */
440 apic_write(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
441
442 /*
443 * Send IPI
444 */
445 apic_write(APIC_ICR, APIC_INT_LEVELTRIG | APIC_INT_ASSERT
446 | APIC_DM_INIT);
447
448 Dprintk("Waiting for send to finish...\n");
449 timeout = 0;
450 do {
451 Dprintk("+");
452 udelay(100);
453 send_status = apic_read(APIC_ICR) & APIC_ICR_BUSY;
454 } while (send_status && (timeout++ < 1000));
455
456 mdelay(10);
457
458 Dprintk("Deasserting INIT.\n");
459
460 /* Target chip */
461 apic_write(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
462
463 /* Send IPI */
464 apic_write(APIC_ICR, APIC_INT_LEVELTRIG | APIC_DM_INIT);
465
466 Dprintk("Waiting for send to finish...\n");
467 timeout = 0;
468 do {
469 Dprintk("+");
470 udelay(100);
471 send_status = apic_read(APIC_ICR) & APIC_ICR_BUSY;
472 } while (send_status && (timeout++ < 1000));
473
474 mb();
475 atomic_set(&init_deasserted, 1);
476
477 num_starts = 2;
478
479 /*
480 * Run STARTUP IPI loop.
481 */
482 Dprintk("#startup loops: %d.\n", num_starts);
483
484 maxlvt = get_maxlvt();
485
486 for (j = 1; j <= num_starts; j++) {
487 Dprintk("Sending STARTUP #%d.\n",j);
488 apic_write(APIC_ESR, 0);
489 apic_read(APIC_ESR);
490 Dprintk("After apic_write.\n");
491
492 /*
493 * STARTUP IPI
494 */
495
496 /* Target chip */
497 apic_write(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
498
499 /* Boot on the stack */
500 /* Kick the second */
501 apic_write(APIC_ICR, APIC_DM_STARTUP | (start_rip >> 12));
502
503 /*
504 * Give the other CPU some time to accept the IPI.
505 */
506 udelay(300);
507
508 Dprintk("Startup point 1.\n");
509
510 Dprintk("Waiting for send to finish...\n");
511 timeout = 0;
512 do {
513 Dprintk("+");
514 udelay(100);
515 send_status = apic_read(APIC_ICR) & APIC_ICR_BUSY;
516 } while (send_status && (timeout++ < 1000));
517
518 /*
519 * Give the other CPU some time to accept the IPI.
520 */
521 udelay(200);
522 /*
523 * Due to the Pentium erratum 3AP.
524 */
525 if (maxlvt > 3) {
526 apic_write(APIC_ESR, 0);
527 }
528 accept_status = (apic_read(APIC_ESR) & 0xEF);
529 if (send_status || accept_status)
530 break;
531 }
532 Dprintk("After Startup.\n");
533
534 if (send_status)
535 printk(KERN_ERR "APIC never delivered???\n");
536 if (accept_status)
537 printk(KERN_ERR "APIC delivery error (%lx).\n", accept_status);
538
539 return (send_status | accept_status);
540 }
541
542 struct create_idle {
543 struct work_struct work;
544 struct task_struct *idle;
545 struct completion done;
546 int cpu;
547 };
548
549 void do_fork_idle(struct work_struct *work)
550 {
551 struct create_idle *c_idle =
552 container_of(work, struct create_idle, work);
553
554 c_idle->idle = fork_idle(c_idle->cpu);
555 complete(&c_idle->done);
556 }
557
558 /*
559 * Boot one CPU.
560 */
561 static int __cpuinit do_boot_cpu(int cpu, int apicid)
562 {
563 unsigned long boot_error;
564 int timeout;
565 unsigned long start_rip;
566 struct create_idle c_idle = {
567 .work = __WORK_INITIALIZER(c_idle.work, do_fork_idle),
568 .cpu = cpu,
569 .done = COMPLETION_INITIALIZER_ONSTACK(c_idle.done),
570 };
571
572 /* allocate memory for gdts of secondary cpus. Hotplug is considered */
573 if (!cpu_gdt_descr[cpu].address &&
574 !(cpu_gdt_descr[cpu].address = get_zeroed_page(GFP_KERNEL))) {
575 printk(KERN_ERR "Failed to allocate GDT for CPU %d\n", cpu);
576 return -1;
577 }
578
579 /* Allocate node local memory for AP pdas */
580 if (cpu_pda(cpu) == &boot_cpu_pda[cpu]) {
581 struct x8664_pda *newpda, *pda;
582 int node = cpu_to_node(cpu);
583 pda = cpu_pda(cpu);
584 newpda = kmalloc_node(sizeof (struct x8664_pda), GFP_ATOMIC,
585 node);
586 if (newpda) {
587 memcpy(newpda, pda, sizeof (struct x8664_pda));
588 cpu_pda(cpu) = newpda;
589 } else
590 printk(KERN_ERR
591 "Could not allocate node local PDA for CPU %d on node %d\n",
592 cpu, node);
593 }
594
595 alternatives_smp_switch(1);
596
597 c_idle.idle = get_idle_for_cpu(cpu);
598
599 if (c_idle.idle) {
600 c_idle.idle->thread.rsp = (unsigned long) (((struct pt_regs *)
601 (THREAD_SIZE + task_stack_page(c_idle.idle))) - 1);
602 init_idle(c_idle.idle, cpu);
603 goto do_rest;
604 }
605
606 /*
607 * During cold boot process, keventd thread is not spun up yet.
608 * When we do cpu hot-add, we create idle threads on the fly, we should
609 * not acquire any attributes from the calling context. Hence the clean
610 * way to create kernel_threads() is to do that from keventd().
611 * We do the current_is_keventd() due to the fact that ACPI notifier
612 * was also queuing to keventd() and when the caller is already running
613 * in context of keventd(), we would end up with locking up the keventd
614 * thread.
615 */
616 if (!keventd_up() || current_is_keventd())
617 c_idle.work.func(&c_idle.work);
618 else {
619 schedule_work(&c_idle.work);
620 wait_for_completion(&c_idle.done);
621 }
622
623 if (IS_ERR(c_idle.idle)) {
624 printk("failed fork for CPU %d\n", cpu);
625 return PTR_ERR(c_idle.idle);
626 }
627
628 set_idle_for_cpu(cpu, c_idle.idle);
629
630 do_rest:
631
632 cpu_pda(cpu)->pcurrent = c_idle.idle;
633
634 start_rip = setup_trampoline();
635
636 init_rsp = c_idle.idle->thread.rsp;
637 per_cpu(init_tss,cpu).rsp0 = init_rsp;
638 initial_code = start_secondary;
639 clear_tsk_thread_flag(c_idle.idle, TIF_FORK);
640
641 printk(KERN_INFO "Booting processor %d/%d APIC 0x%x\n", cpu,
642 cpus_weight(cpu_present_map),
643 apicid);
644
645 /*
646 * This grunge runs the startup process for
647 * the targeted processor.
648 */
649
650 atomic_set(&init_deasserted, 0);
651
652 Dprintk("Setting warm reset code and vector.\n");
653
654 CMOS_WRITE(0xa, 0xf);
655 local_flush_tlb();
656 Dprintk("1.\n");
657 *((volatile unsigned short *) phys_to_virt(0x469)) = start_rip >> 4;
658 Dprintk("2.\n");
659 *((volatile unsigned short *) phys_to_virt(0x467)) = start_rip & 0xf;
660 Dprintk("3.\n");
661
662 /*
663 * Be paranoid about clearing APIC errors.
664 */
665 apic_write(APIC_ESR, 0);
666 apic_read(APIC_ESR);
667
668 /*
669 * Status is now clean
670 */
671 boot_error = 0;
672
673 /*
674 * Starting actual IPI sequence...
675 */
676 boot_error = wakeup_secondary_via_INIT(apicid, start_rip);
677
678 if (!boot_error) {
679 /*
680 * allow APs to start initializing.
681 */
682 Dprintk("Before Callout %d.\n", cpu);
683 cpu_set(cpu, cpu_callout_map);
684 Dprintk("After Callout %d.\n", cpu);
685
686 /*
687 * Wait 5s total for a response
688 */
689 for (timeout = 0; timeout < 50000; timeout++) {
690 if (cpu_isset(cpu, cpu_callin_map))
691 break; /* It has booted */
692 udelay(100);
693 }
694
695 if (cpu_isset(cpu, cpu_callin_map)) {
696 /* number CPUs logically, starting from 1 (BSP is 0) */
697 Dprintk("CPU has booted.\n");
698 } else {
699 boot_error = 1;
700 if (*((volatile unsigned char *)phys_to_virt(SMP_TRAMPOLINE_BASE))
701 == 0xA5)
702 /* trampoline started but...? */
703 printk("Stuck ??\n");
704 else
705 /* trampoline code not run */
706 printk("Not responding.\n");
707 #ifdef APIC_DEBUG
708 inquire_remote_apic(apicid);
709 #endif
710 }
711 }
712 if (boot_error) {
713 cpu_clear(cpu, cpu_callout_map); /* was set here (do_boot_cpu()) */
714 clear_bit(cpu, &cpu_initialized); /* was set by cpu_init() */
715 clear_node_cpumask(cpu); /* was set by numa_add_cpu */
716 cpu_clear(cpu, cpu_present_map);
717 cpu_clear(cpu, cpu_possible_map);
718 x86_cpu_to_apicid[cpu] = BAD_APICID;
719 x86_cpu_to_log_apicid[cpu] = BAD_APICID;
720 return -EIO;
721 }
722
723 return 0;
724 }
725
726 cycles_t cacheflush_time;
727 unsigned long cache_decay_ticks;
728
729 /*
730 * Cleanup possible dangling ends...
731 */
732 static __cpuinit void smp_cleanup_boot(void)
733 {
734 /*
735 * Paranoid: Set warm reset code and vector here back
736 * to default values.
737 */
738 CMOS_WRITE(0, 0xf);
739
740 /*
741 * Reset trampoline flag
742 */
743 *((volatile int *) phys_to_virt(0x467)) = 0;
744 }
745
746 /*
747 * Fall back to non SMP mode after errors.
748 *
749 * RED-PEN audit/test this more. I bet there is more state messed up here.
750 */
751 static __init void disable_smp(void)
752 {
753 cpu_present_map = cpumask_of_cpu(0);
754 cpu_possible_map = cpumask_of_cpu(0);
755 if (smp_found_config)
756 phys_cpu_present_map = physid_mask_of_physid(boot_cpu_id);
757 else
758 phys_cpu_present_map = physid_mask_of_physid(0);
759 cpu_set(0, cpu_sibling_map[0]);
760 cpu_set(0, cpu_core_map[0]);
761 }
762
763 #ifdef CONFIG_HOTPLUG_CPU
764
765 int additional_cpus __initdata = -1;
766
767 /*
768 * cpu_possible_map should be static, it cannot change as cpu's
769 * are onlined, or offlined. The reason is per-cpu data-structures
770 * are allocated by some modules at init time, and dont expect to
771 * do this dynamically on cpu arrival/departure.
772 * cpu_present_map on the other hand can change dynamically.
773 * In case when cpu_hotplug is not compiled, then we resort to current
774 * behaviour, which is cpu_possible == cpu_present.
775 * - Ashok Raj
776 *
777 * Three ways to find out the number of additional hotplug CPUs:
778 * - If the BIOS specified disabled CPUs in ACPI/mptables use that.
779 * - The user can overwrite it with additional_cpus=NUM
780 * - Otherwise don't reserve additional CPUs.
781 * We do this because additional CPUs waste a lot of memory.
782 * -AK
783 */
784 __init void prefill_possible_map(void)
785 {
786 int i;
787 int possible;
788
789 if (additional_cpus == -1) {
790 if (disabled_cpus > 0)
791 additional_cpus = disabled_cpus;
792 else
793 additional_cpus = 0;
794 }
795 possible = num_processors + additional_cpus;
796 if (possible > NR_CPUS)
797 possible = NR_CPUS;
798
799 printk(KERN_INFO "SMP: Allowing %d CPUs, %d hotplug CPUs\n",
800 possible,
801 max_t(int, possible - num_processors, 0));
802
803 for (i = 0; i < possible; i++)
804 cpu_set(i, cpu_possible_map);
805 }
806 #endif
807
808 /*
809 * Various sanity checks.
810 */
811 static int __init smp_sanity_check(unsigned max_cpus)
812 {
813 if (!physid_isset(hard_smp_processor_id(), phys_cpu_present_map)) {
814 printk("weird, boot CPU (#%d) not listed by the BIOS.\n",
815 hard_smp_processor_id());
816 physid_set(hard_smp_processor_id(), phys_cpu_present_map);
817 }
818
819 /*
820 * If we couldn't find an SMP configuration at boot time,
821 * get out of here now!
822 */
823 if (!smp_found_config) {
824 printk(KERN_NOTICE "SMP motherboard not detected.\n");
825 disable_smp();
826 if (APIC_init_uniprocessor())
827 printk(KERN_NOTICE "Local APIC not detected."
828 " Using dummy APIC emulation.\n");
829 return -1;
830 }
831
832 /*
833 * Should not be necessary because the MP table should list the boot
834 * CPU too, but we do it for the sake of robustness anyway.
835 */
836 if (!physid_isset(boot_cpu_id, phys_cpu_present_map)) {
837 printk(KERN_NOTICE "weird, boot CPU (#%d) not listed by the BIOS.\n",
838 boot_cpu_id);
839 physid_set(hard_smp_processor_id(), phys_cpu_present_map);
840 }
841
842 /*
843 * If we couldn't find a local APIC, then get out of here now!
844 */
845 if (!cpu_has_apic) {
846 printk(KERN_ERR "BIOS bug, local APIC #%d not detected!...\n",
847 boot_cpu_id);
848 printk(KERN_ERR "... forcing use of dummy APIC emulation. (tell your hw vendor)\n");
849 nr_ioapics = 0;
850 return -1;
851 }
852
853 /*
854 * If SMP should be disabled, then really disable it!
855 */
856 if (!max_cpus) {
857 printk(KERN_INFO "SMP mode deactivated, forcing use of dummy APIC emulation.\n");
858 nr_ioapics = 0;
859 return -1;
860 }
861
862 return 0;
863 }
864
865 /*
866 * Prepare for SMP bootup. The MP table or ACPI has been read
867 * earlier. Just do some sanity checking here and enable APIC mode.
868 */
869 void __init smp_prepare_cpus(unsigned int max_cpus)
870 {
871 nmi_watchdog_default();
872 current_cpu_data = boot_cpu_data;
873 current_thread_info()->cpu = 0; /* needed? */
874 set_cpu_sibling_map(0);
875
876 if (smp_sanity_check(max_cpus) < 0) {
877 printk(KERN_INFO "SMP disabled\n");
878 disable_smp();
879 return;
880 }
881
882
883 /*
884 * Switch from PIC to APIC mode.
885 */
886 setup_local_APIC();
887
888 if (GET_APIC_ID(apic_read(APIC_ID)) != boot_cpu_id) {
889 panic("Boot APIC ID in local APIC unexpected (%d vs %d)",
890 GET_APIC_ID(apic_read(APIC_ID)), boot_cpu_id);
891 /* Or can we switch back to PIC here? */
892 }
893
894 /*
895 * Now start the IO-APICs
896 */
897 if (!skip_ioapic_setup && nr_ioapics)
898 setup_IO_APIC();
899 else
900 nr_ioapics = 0;
901
902 /*
903 * Set up local APIC timer on boot CPU.
904 */
905
906 setup_boot_APIC_clock();
907 }
908
909 /*
910 * Early setup to make printk work.
911 */
912 void __init smp_prepare_boot_cpu(void)
913 {
914 int me = smp_processor_id();
915 cpu_set(me, cpu_online_map);
916 cpu_set(me, cpu_callout_map);
917 per_cpu(cpu_state, me) = CPU_ONLINE;
918 }
919
920 /*
921 * Entry point to boot a CPU.
922 */
923 int __cpuinit __cpu_up(unsigned int cpu)
924 {
925 int apicid = cpu_present_to_apicid(cpu);
926 unsigned long flags;
927 int err;
928
929 WARN_ON(irqs_disabled());
930
931 Dprintk("++++++++++++++++++++=_---CPU UP %u\n", cpu);
932
933 if (apicid == BAD_APICID || apicid == boot_cpu_id ||
934 !physid_isset(apicid, phys_cpu_present_map)) {
935 printk("__cpu_up: bad cpu %d\n", cpu);
936 return -EINVAL;
937 }
938
939 /*
940 * Already booted CPU?
941 */
942 if (cpu_isset(cpu, cpu_callin_map)) {
943 Dprintk("do_boot_cpu %d Already started\n", cpu);
944 return -ENOSYS;
945 }
946
947 /*
948 * Save current MTRR state in case it was changed since early boot
949 * (e.g. by the ACPI SMI) to initialize new CPUs with MTRRs in sync:
950 */
951 mtrr_save_state();
952
953 per_cpu(cpu_state, cpu) = CPU_UP_PREPARE;
954 /* Boot it! */
955 err = do_boot_cpu(cpu, apicid);
956 if (err < 0) {
957 Dprintk("do_boot_cpu failed %d\n", err);
958 return err;
959 }
960
961 /* Unleash the CPU! */
962 Dprintk("waiting for cpu %d\n", cpu);
963
964 /*
965 * Make sure and check TSC sync:
966 */
967 local_irq_save(flags);
968 check_tsc_sync_source(cpu);
969 local_irq_restore(flags);
970
971 while (!cpu_isset(cpu, cpu_online_map))
972 cpu_relax();
973 err = 0;
974
975 return err;
976 }
977
978 /*
979 * Finish the SMP boot.
980 */
981 void __init smp_cpus_done(unsigned int max_cpus)
982 {
983 smp_cleanup_boot();
984 setup_ioapic_dest();
985 check_nmi_watchdog();
986 }
987
988 #ifdef CONFIG_HOTPLUG_CPU
989
990 static void remove_siblinginfo(int cpu)
991 {
992 int sibling;
993 struct cpuinfo_x86 *c = cpu_data;
994
995 for_each_cpu_mask(sibling, cpu_core_map[cpu]) {
996 cpu_clear(cpu, cpu_core_map[sibling]);
997 /*
998 * last thread sibling in this cpu core going down
999 */
1000 if (cpus_weight(cpu_sibling_map[cpu]) == 1)
1001 c[sibling].booted_cores--;
1002 }
1003
1004 for_each_cpu_mask(sibling, cpu_sibling_map[cpu])
1005 cpu_clear(cpu, cpu_sibling_map[sibling]);
1006 cpus_clear(cpu_sibling_map[cpu]);
1007 cpus_clear(cpu_core_map[cpu]);
1008 c[cpu].phys_proc_id = 0;
1009 c[cpu].cpu_core_id = 0;
1010 cpu_clear(cpu, cpu_sibling_setup_map);
1011 }
1012
1013 void remove_cpu_from_maps(void)
1014 {
1015 int cpu = smp_processor_id();
1016
1017 cpu_clear(cpu, cpu_callout_map);
1018 cpu_clear(cpu, cpu_callin_map);
1019 clear_bit(cpu, &cpu_initialized); /* was set by cpu_init() */
1020 clear_node_cpumask(cpu);
1021 }
1022
1023 int __cpu_disable(void)
1024 {
1025 int cpu = smp_processor_id();
1026
1027 /*
1028 * Perhaps use cpufreq to drop frequency, but that could go
1029 * into generic code.
1030 *
1031 * We won't take down the boot processor on i386 due to some
1032 * interrupts only being able to be serviced by the BSP.
1033 * Especially so if we're not using an IOAPIC -zwane
1034 */
1035 if (cpu == 0)
1036 return -EBUSY;
1037
1038 if (nmi_watchdog == NMI_LOCAL_APIC)
1039 stop_apic_nmi_watchdog(NULL);
1040 clear_local_APIC();
1041
1042 /*
1043 * HACK:
1044 * Allow any queued timer interrupts to get serviced
1045 * This is only a temporary solution until we cleanup
1046 * fixup_irqs as we do for IA64.
1047 */
1048 local_irq_enable();
1049 mdelay(1);
1050
1051 local_irq_disable();
1052 remove_siblinginfo(cpu);
1053
1054 spin_lock(&vector_lock);
1055 /* It's now safe to remove this processor from the online map */
1056 cpu_clear(cpu, cpu_online_map);
1057 spin_unlock(&vector_lock);
1058 remove_cpu_from_maps();
1059 fixup_irqs(cpu_online_map);
1060 return 0;
1061 }
1062
1063 void __cpu_die(unsigned int cpu)
1064 {
1065 /* We don't do anything here: idle task is faking death itself. */
1066 unsigned int i;
1067
1068 for (i = 0; i < 10; i++) {
1069 /* They ack this in play_dead by setting CPU_DEAD */
1070 if (per_cpu(cpu_state, cpu) == CPU_DEAD) {
1071 printk ("CPU %d is now offline\n", cpu);
1072 if (1 == num_online_cpus())
1073 alternatives_smp_switch(0);
1074 return;
1075 }
1076 msleep(100);
1077 }
1078 printk(KERN_ERR "CPU %u didn't die...\n", cpu);
1079 }
1080
1081 static __init int setup_additional_cpus(char *s)
1082 {
1083 return s && get_option(&s, &additional_cpus) ? 0 : -EINVAL;
1084 }
1085 early_param("additional_cpus", setup_additional_cpus);
1086
1087 #else /* ... !CONFIG_HOTPLUG_CPU */
1088
1089 int __cpu_disable(void)
1090 {
1091 return -ENOSYS;
1092 }
1093
1094 void __cpu_die(unsigned int cpu)
1095 {
1096 /* We said "no" in __cpu_disable */
1097 BUG();
1098 }
1099 #endif /* CONFIG_HOTPLUG_CPU */
This page took 0.110033 seconds and 5 git commands to generate.