debuggers.hg

view xen/arch/x86/smpboot.c @ 4657:67c40314aa6e

bitkeeper revision 1.1356 (4267b673efcxbbD0RRVHEKvD5FY3Ng)

A few APIC cleanups. I hate '#if 0' code.
Signed-off-by: Keir Fraser <keir@xensource.com>
author kaf24@firebug.cl.cam.ac.uk
date Thu Apr 21 14:19:31 2005 +0000 (2005-04-21)
parents 9e4daf2e49d2
children 979aa5d4764e 487de0451d2b ddd290cc8f0d
line source
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 *
7 * Much of the core SMP work is based on previous work by Thomas Radke, to
8 * whom a great many thanks are extended.
9 *
10 * Thanks to Intel for making available several different Pentium,
11 * Pentium Pro and Pentium-II/Xeon MP machines.
12 * Original development of Linux SMP code supported by Caldera.
13 *
14 * This code is released under the GNU General Public License version 2 or
15 * later.
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 * Martin J. Bligh : Added support for multi-quad systems
33 */
35 #include <xen/config.h>
36 #include <xen/init.h>
37 #include <xen/irq.h>
38 #include <xen/mm.h>
39 #include <xen/slab.h>
40 #include <asm/flushtlb.h>
41 #include <asm/mc146818rtc.h>
42 #include <asm/smpboot.h>
43 #include <xen/smp.h>
44 #include <asm/msr.h>
45 #include <asm/system.h>
46 #include <asm/mpspec.h>
47 #include <asm/io_apic.h>
48 #include <xen/sched.h>
49 #include <xen/delay.h>
50 #include <xen/lib.h>
52 #ifdef CONFIG_SMP
54 /* Cconfigured maximum number of CPUs to activate. We name the parameter
55 "maxcpus" rather than max_cpus to be compatible with Linux */
56 static int max_cpus = -1;
57 integer_param("maxcpus", max_cpus);
59 /* Total count of live CPUs */
60 int smp_num_cpus = 1;
62 /* Number of hyperthreads per core */
63 int ht_per_core = 1;
65 /* Bitmask of currently online CPUs */
66 unsigned long cpu_online_map;
68 static volatile unsigned long cpu_callin_map;
69 static volatile unsigned long cpu_callout_map;
71 /* Per CPU bogomips and other parameters */
72 struct cpuinfo_x86 cpu_data[NR_CPUS];
74 /* Set when the idlers are all forked */
75 int smp_threads_ready;
77 /*
78 * Trampoline 80x86 program as an array.
79 */
81 extern unsigned char trampoline_data [];
82 extern unsigned char trampoline_end [];
83 static unsigned char *trampoline_base;
85 /*
86 * Currently trivial. Write the real->protected mode
87 * bootstrap into the page concerned. The caller
88 * has made sure it's suitably aligned.
89 */
91 static unsigned long __init setup_trampoline(void)
92 {
93 memcpy(trampoline_base, trampoline_data, trampoline_end - trampoline_data);
94 return virt_to_phys(trampoline_base);
95 }
97 /*
98 * We are called very early to get the low memory for the
99 * SMP bootup trampoline page.
100 */
101 void __init smp_alloc_memory(void)
102 {
103 /*
104 * Has to be in very low memory so we can execute
105 * real-mode AP code.
106 */
107 trampoline_base = __va(0x90000);
108 }
110 /*
111 * The bootstrap kernel entry code has set these up. Save them for
112 * a given CPU
113 */
115 void __init smp_store_cpu_info(int id)
116 {
117 cpu_data[id] = boot_cpu_data;
118 if (id != 0)
119 identify_cpu(&cpu_data[id]);
120 }
122 /*
123 * Architecture specific routine called by the kernel just before init is
124 * fired off. This allows the BP to have everything in order [we hope].
125 * At the end of this all the APs will hit the system scheduling and off
126 * we go. Each AP will load the system gdt's and jump through the kernel
127 * init into idle(). At this point the scheduler will one day take over
128 * and give them jobs to do. smp_callin is a standard routine
129 * we use to track CPUs as they power up.
130 */
132 static atomic_t smp_commenced = ATOMIC_INIT(0);
134 void __init smp_commence(void)
135 {
136 /*
137 * Lets the callins below out of their loop.
138 */
139 Dprintk("Setting commenced=1, go go go\n");
141 wmb();
142 atomic_set(&smp_commenced,1);
143 }
145 /*
146 * TSC synchronization.
147 *
148 * We first check wether all CPUs have their TSC's synchronized,
149 * then we print a warning if not, and always resync.
150 */
152 static atomic_t tsc_start_flag = ATOMIC_INIT(0);
153 static atomic_t tsc_count_start = ATOMIC_INIT(0);
154 static atomic_t tsc_count_stop = ATOMIC_INIT(0);
155 static unsigned long long tsc_values[NR_CPUS];
157 #define NR_LOOPS 5
159 /*
160 * accurate 64-bit/32-bit division, expanded to 32-bit divisions and 64-bit
161 * multiplication. Not terribly optimized but we need it at boot time only
162 * anyway.
163 *
164 * result == a / b
165 * == (a1 + a2*(2^32)) / b
166 * == a1/b + a2*(2^32/b)
167 * == a1/b + a2*((2^32-1)/b) + a2/b + (a2*((2^32-1) % b))/b
168 * ^---- (this multiplication can overflow)
169 */
171 static unsigned long long div64 (unsigned long long a, unsigned long b0)
172 {
173 unsigned int a1, a2;
174 unsigned long long res;
176 a1 = ((unsigned int*)&a)[0];
177 a2 = ((unsigned int*)&a)[1];
179 res = a1/b0 +
180 (unsigned long long)a2 * (unsigned long long)(0xffffffff/b0) +
181 a2 / b0 +
182 (a2 * (0xffffffff % b0)) / b0;
184 return res;
185 }
187 static void __init synchronize_tsc_bp (void)
188 {
189 int i;
190 unsigned long long t0;
191 unsigned long long sum, avg;
192 long long delta;
193 int buggy = 0;
195 printk("checking TSC synchronization across CPUs: ");
197 atomic_set(&tsc_start_flag, 1);
198 wmb();
200 /*
201 * We loop a few times to get a primed instruction cache,
202 * then the last pass is more or less synchronized and
203 * the BP and APs set their cycle counters to zero all at
204 * once. This reduces the chance of having random offsets
205 * between the processors, and guarantees that the maximum
206 * delay between the cycle counters is never bigger than
207 * the latency of information-passing (cachelines) between
208 * two CPUs.
209 */
210 for (i = 0; i < NR_LOOPS; i++) {
211 /*
212 * all APs synchronize but they loop on '== num_cpus'
213 */
214 while (atomic_read(&tsc_count_start) != smp_num_cpus-1) mb();
215 atomic_set(&tsc_count_stop, 0);
216 wmb();
217 /*
218 * this lets the APs save their current TSC:
219 */
220 atomic_inc(&tsc_count_start);
222 rdtscll(tsc_values[smp_processor_id()]);
223 /*
224 * We clear the TSC in the last loop:
225 */
226 if (i == NR_LOOPS-1)
227 write_tsc(0, 0);
229 /*
230 * Wait for all APs to leave the synchronization point:
231 */
232 while (atomic_read(&tsc_count_stop) != smp_num_cpus-1) mb();
233 atomic_set(&tsc_count_start, 0);
234 wmb();
235 atomic_inc(&tsc_count_stop);
236 }
238 sum = 0;
239 for (i = 0; i < smp_num_cpus; i++) {
240 t0 = tsc_values[i];
241 sum += t0;
242 }
243 avg = div64(sum, smp_num_cpus);
245 sum = 0;
246 for (i = 0; i < smp_num_cpus; i++) {
247 delta = tsc_values[i] - avg;
248 if (delta < 0)
249 delta = -delta;
250 /*
251 * We report bigger than 2 microseconds clock differences.
252 */
253 if (delta > 2*ticks_per_usec) {
254 long realdelta;
255 if (!buggy) {
256 buggy = 1;
257 printk("\n");
258 }
259 realdelta = div64(delta, ticks_per_usec);
260 if (tsc_values[i] < avg)
261 realdelta = -realdelta;
263 printk("BIOS BUG: CPU#%d improperly initialized, has %ld usecs TSC skew! FIXED.\n",
264 i, realdelta);
265 }
267 sum += delta;
268 }
269 if (!buggy)
270 printk("passed.\n");
271 }
273 static void __init synchronize_tsc_ap (void)
274 {
275 int i;
277 /*
278 * smp_num_cpus is not necessarily known at the time
279 * this gets called, so we first wait for the BP to
280 * finish SMP initialization:
281 */
282 while (!atomic_read(&tsc_start_flag)) mb();
284 for (i = 0; i < NR_LOOPS; i++) {
285 atomic_inc(&tsc_count_start);
286 while (atomic_read(&tsc_count_start) != smp_num_cpus) mb();
288 rdtscll(tsc_values[smp_processor_id()]);
289 if (i == NR_LOOPS-1)
290 write_tsc(0, 0);
292 atomic_inc(&tsc_count_stop);
293 while (atomic_read(&tsc_count_stop) != smp_num_cpus) mb();
294 }
295 }
296 #undef NR_LOOPS
298 static atomic_t init_deasserted;
300 void __init smp_callin(void)
301 {
302 int cpuid, phys_id, i;
304 /*
305 * If waken up by an INIT in an 82489DX configuration
306 * we may get here before an INIT-deassert IPI reaches
307 * our local APIC. We have to wait for the IPI or we'll
308 * lock up on an APIC access.
309 */
310 while (!atomic_read(&init_deasserted));
312 /*
313 * (This works even if the APIC is not enabled.)
314 */
315 phys_id = GET_APIC_ID(apic_read(APIC_ID));
316 cpuid = smp_processor_id();
317 if (test_and_set_bit(cpuid, &cpu_online_map)) {
318 printk("huh, phys CPU#%d, CPU#%d already present??\n",
319 phys_id, cpuid);
320 BUG();
321 }
322 Dprintk("CPU#%d (phys ID: %d) waiting for CALLOUT\n", cpuid, phys_id);
324 /*
325 * STARTUP IPIs are fragile beasts as they might sometimes
326 * trigger some glue motherboard logic. Complete APIC bus
327 * silence for 1 second, this overestimates the time the
328 * boot CPU is spending to send the up to 2 STARTUP IPIs
329 * by a factor of two. This should be enough.
330 */
332 for ( i = 0; i < 200; i++ )
333 {
334 if ( test_bit(cpuid, &cpu_callout_map) ) break;
335 mdelay(10);
336 }
338 if (!test_bit(cpuid, &cpu_callout_map)) {
339 printk("BUG: CPU%d started up but did not get a callout!\n",
340 cpuid);
341 BUG();
342 }
344 /*
345 * the boot CPU has finished the init stage and is spinning
346 * on callin_map until we finish. We are free to set up this
347 * CPU, first the APIC. (this is probably redundant on most
348 * boards)
349 */
351 Dprintk("CALLIN, before setup_local_APIC().\n");
353 setup_local_APIC();
355 __sti();
357 Dprintk("Stack at about %p\n",&cpuid);
359 /*
360 * Save our processor parameters
361 */
362 smp_store_cpu_info(cpuid);
364 /*
365 * Allow the master to continue.
366 */
367 set_bit(cpuid, &cpu_callin_map);
369 /*
370 * Synchronize the TSC with the BP
371 */
372 synchronize_tsc_ap();
373 }
375 static int cpucount;
377 #ifdef __i386__
378 static void construct_percpu_idt(unsigned int cpu)
379 {
380 unsigned char idt_load[10];
382 idt_tables[cpu] = xmalloc_array(idt_entry_t, IDT_ENTRIES);
383 memcpy(idt_tables[cpu], idt_table, IDT_ENTRIES*sizeof(idt_entry_t));
385 *(unsigned short *)(&idt_load[0]) = (IDT_ENTRIES*sizeof(idt_entry_t))-1;
386 *(unsigned long *)(&idt_load[2]) = (unsigned long)idt_tables[cpu];
387 __asm__ __volatile__ ( "lidt %0" : "=m" (idt_load) );
388 }
389 #endif
391 /*
392 * Activate a secondary processor.
393 */
394 void __init start_secondary(void)
395 {
396 unsigned int cpu = cpucount;
398 extern void percpu_traps_init(void);
399 extern void cpu_init(void);
401 set_current(idle_task[cpu]);
403 percpu_traps_init();
405 cpu_init();
407 smp_callin();
409 while (!atomic_read(&smp_commenced))
410 cpu_relax();
412 #ifdef __i386__
413 /*
414 * At this point, boot CPU has fully initialised the IDT. It is
415 * now safe to make ourselves a private copy.
416 */
417 construct_percpu_idt(cpu);
418 #endif
420 local_flush_tlb();
422 startup_cpu_idle_loop();
424 BUG();
425 }
427 extern struct {
428 unsigned long esp, ss;
429 } stack_start;
431 /* which physical APIC ID maps to which logical CPU number */
432 volatile int physical_apicid_2_cpu[MAX_APICID];
433 /* which logical CPU number maps to which physical APIC ID */
434 volatile int cpu_2_physical_apicid[NR_CPUS];
436 /* which logical APIC ID maps to which logical CPU number */
437 volatile int logical_apicid_2_cpu[MAX_APICID];
438 /* which logical CPU number maps to which logical APIC ID */
439 volatile int cpu_2_logical_apicid[NR_CPUS];
441 static inline void init_cpu_to_apicid(void)
442 /* Initialize all maps between cpu number and apicids */
443 {
444 int apicid, cpu;
446 for (apicid = 0; apicid < MAX_APICID; apicid++) {
447 physical_apicid_2_cpu[apicid] = -1;
448 logical_apicid_2_cpu[apicid] = -1;
449 }
450 for (cpu = 0; cpu < NR_CPUS; cpu++) {
451 cpu_2_physical_apicid[cpu] = -1;
452 cpu_2_logical_apicid[cpu] = -1;
453 }
454 }
456 static inline void map_cpu_to_boot_apicid(int cpu, int apicid)
457 /*
458 * set up a mapping between cpu and apicid. Uses logical apicids for multiquad,
459 * else physical apic ids
460 */
461 {
462 physical_apicid_2_cpu[apicid] = cpu;
463 cpu_2_physical_apicid[cpu] = apicid;
464 }
466 static inline void unmap_cpu_to_boot_apicid(int cpu, int apicid)
467 /*
468 * undo a mapping between cpu and apicid. Uses logical apicids for multiquad,
469 * else physical apic ids
470 */
471 {
472 physical_apicid_2_cpu[apicid] = -1;
473 cpu_2_physical_apicid[cpu] = -1;
474 }
476 #if APIC_DEBUG
477 static inline void inquire_remote_apic(int apicid)
478 {
479 int i, regs[] = { APIC_ID >> 4, APIC_LVR >> 4, APIC_SPIV >> 4 };
480 char *names[] = { "ID", "VERSION", "SPIV" };
481 int timeout, status;
483 printk("Inquiring remote APIC #%d...\n", apicid);
485 for (i = 0; i < sizeof(regs) / sizeof(*regs); i++) {
486 printk("... APIC #%d %s: ", apicid, names[i]);
488 /*
489 * Wait for idle.
490 */
491 apic_wait_icr_idle();
493 apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(apicid));
494 apic_write_around(APIC_ICR, APIC_DM_REMRD | regs[i]);
496 timeout = 0;
497 do {
498 udelay(100);
499 status = apic_read(APIC_ICR) & APIC_ICR_RR_MASK;
500 } while (status == APIC_ICR_RR_INPROG && timeout++ < 1000);
502 switch (status) {
503 case APIC_ICR_RR_VALID:
504 status = apic_read(APIC_RRR);
505 printk("%08x\n", status);
506 break;
507 default:
508 printk("failed\n");
509 }
510 }
511 }
512 #endif
515 static int wakeup_secondary_via_INIT(int phys_apicid, unsigned long start_eip)
516 {
517 unsigned long send_status = 0, accept_status = 0;
518 int maxlvt, timeout, num_starts, j;
520 Dprintk("Asserting INIT.\n");
522 /*
523 * Turn INIT on target chip
524 */
525 apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
527 /*
528 * Send IPI
529 */
530 apic_write_around(APIC_ICR, APIC_INT_LEVELTRIG | APIC_INT_ASSERT
531 | APIC_DM_INIT);
533 Dprintk("Waiting for send to finish...\n");
534 timeout = 0;
535 do {
536 Dprintk("+");
537 udelay(100);
538 send_status = apic_read(APIC_ICR) & APIC_ICR_BUSY;
539 } while (send_status && (timeout++ < 1000));
541 mdelay(10);
543 Dprintk("Deasserting INIT.\n");
545 /* Target chip */
546 apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
548 /* Send IPI */
549 apic_write_around(APIC_ICR, APIC_INT_LEVELTRIG | APIC_DM_INIT);
551 Dprintk("Waiting for send to finish...\n");
552 timeout = 0;
553 do {
554 Dprintk("+");
555 udelay(100);
556 send_status = apic_read(APIC_ICR) & APIC_ICR_BUSY;
557 } while (send_status && (timeout++ < 1000));
559 atomic_set(&init_deasserted, 1);
561 /*
562 * Should we send STARTUP IPIs ?
563 *
564 * Determine this based on the APIC version.
565 * If we don't have an integrated APIC, don't send the STARTUP IPIs.
566 */
567 if (APIC_INTEGRATED(apic_version[phys_apicid]))
568 num_starts = 2;
569 else
570 num_starts = 0;
572 /*
573 * Run STARTUP IPI loop.
574 */
575 Dprintk("#startup loops: %d.\n", num_starts);
577 maxlvt = get_maxlvt();
579 for (j = 1; j <= num_starts; j++) {
580 Dprintk("Sending STARTUP #%d.\n",j);
582 apic_read_around(APIC_SPIV);
583 apic_write(APIC_ESR, 0);
584 apic_read(APIC_ESR);
585 Dprintk("After apic_write.\n");
587 /*
588 * STARTUP IPI
589 */
591 /* Target chip */
592 apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
594 /* Boot on the stack */
595 /* Kick the second */
596 apic_write_around(APIC_ICR, APIC_DM_STARTUP
597 | (start_eip >> 12));
599 /*
600 * Give the other CPU some time to accept the IPI.
601 */
602 udelay(300);
604 Dprintk("Startup point 1.\n");
606 Dprintk("Waiting for send to finish...\n");
607 timeout = 0;
608 do {
609 Dprintk("+");
610 udelay(100);
611 send_status = apic_read(APIC_ICR) & APIC_ICR_BUSY;
612 } while (send_status && (timeout++ < 1000));
614 /*
615 * Give the other CPU some time to accept the IPI.
616 */
617 udelay(200);
618 /*
619 * Due to the Pentium erratum 3AP.
620 */
621 if (maxlvt > 3) {
622 apic_read_around(APIC_SPIV);
623 apic_write(APIC_ESR, 0);
624 }
625 accept_status = (apic_read(APIC_ESR) & 0xEF);
626 if (send_status || accept_status)
627 break;
628 }
629 Dprintk("After Startup.\n");
631 if (send_status)
632 printk("APIC never delivered???\n");
633 if (accept_status)
634 printk("APIC delivery error (%lx).\n", accept_status);
636 return (send_status | accept_status);
637 }
639 extern unsigned long cpu_initialized;
641 static void __init do_boot_cpu (int apicid)
642 /*
643 * NOTE - on most systems this is a PHYSICAL apic ID, but on multiquad
644 * (ie clustered apic addressing mode), this is a LOGICAL apic ID.
645 */
646 {
647 struct domain *idle;
648 struct exec_domain *ed;
649 unsigned long boot_error = 0;
650 int timeout, cpu;
651 unsigned long start_eip;
652 void *stack;
654 cpu = ++cpucount;
656 if ( (idle = do_createdomain(IDLE_DOMAIN_ID, cpu)) == NULL )
657 panic("failed 'createdomain' for CPU %d", cpu);
659 ed = idle->exec_domain[0];
661 set_bit(DF_IDLETASK, &idle->d_flags);
663 ed->arch.monitor_table = mk_pagetable(__pa(idle_pg_table));
665 map_cpu_to_boot_apicid(cpu, apicid);
667 idle_task[cpu] = ed;
669 /* start_eip had better be page-aligned! */
670 start_eip = setup_trampoline();
672 /* So we see what's up. */
673 printk("Booting processor %d/%d eip %lx\n", cpu, apicid, start_eip);
675 stack = (void *)alloc_xenheap_pages(STACK_ORDER);
676 #if defined(__i386__)
677 stack_start.esp = __pa(stack) + STACK_SIZE - STACK_RESERVED;
678 #elif defined(__x86_64__)
679 stack_start.esp = (unsigned long)stack + STACK_SIZE - STACK_RESERVED;
680 #endif
682 /* Debug build: detect stack overflow by setting up a guard page. */
683 memguard_guard_stack(stack);
685 /*
686 * This grunge runs the startup process for
687 * the targeted processor.
688 */
690 atomic_set(&init_deasserted, 0);
692 Dprintk("Setting warm reset code and vector.\n");
694 CMOS_WRITE(0xa, 0xf);
695 local_flush_tlb();
696 Dprintk("1.\n");
697 *((volatile unsigned short *) TRAMPOLINE_HIGH) = start_eip >> 4;
698 Dprintk("2.\n");
699 *((volatile unsigned short *) TRAMPOLINE_LOW) = start_eip & 0xf;
700 Dprintk("3.\n");
702 /*
703 * Be paranoid about clearing APIC errors.
704 */
705 if ( APIC_INTEGRATED(apic_version[apicid]) )
706 {
707 apic_read_around(APIC_SPIV);
708 apic_write(APIC_ESR, 0);
709 apic_read(APIC_ESR);
710 }
712 /*
713 * Status is now clean
714 */
715 boot_error = 0;
717 /*
718 * Starting actual IPI sequence...
719 */
721 boot_error = wakeup_secondary_via_INIT(apicid, start_eip);
723 if (!boot_error) {
724 /*
725 * allow APs to start initializing.
726 */
727 Dprintk("Before Callout %d.\n", cpu);
728 set_bit(cpu, &cpu_callout_map);
729 Dprintk("After Callout %d.\n", cpu);
731 /*
732 * Wait 5s total for a response
733 */
734 for (timeout = 0; timeout < 50000; timeout++) {
735 if (test_bit(cpu, &cpu_callin_map))
736 break; /* It has booted */
737 udelay(100);
738 }
740 if (test_bit(cpu, &cpu_callin_map)) {
741 /* number CPUs logically, starting from 1 (BSP is 0) */
742 printk("CPU%d has booted.\n", cpu);
743 } else {
744 boot_error= 1;
745 if (*((volatile unsigned int *)phys_to_virt(start_eip))
746 == 0xA5A5A5A5)
747 /* trampoline started but...? */
748 printk("Stuck ??\n");
749 else
750 /* trampoline code not run */
751 printk("Not responding.\n");
752 #if APIC_DEBUG
753 inquire_remote_apic(apicid);
754 #endif
755 }
756 }
757 if (boot_error) {
758 /* Try to put things back the way they were before ... */
759 unmap_cpu_to_boot_apicid(cpu, apicid);
760 clear_bit(cpu, &cpu_callout_map); /* was set here (do_boot_cpu()) */
761 clear_bit(cpu, &cpu_initialized); /* was set by cpu_init() */
762 clear_bit(cpu, &cpu_online_map); /* was set in smp_callin() */
763 cpucount--;
764 }
765 }
768 /*
769 * Cycle through the processors sending APIC IPIs to boot each.
770 */
772 static int boot_cpu_logical_apicid;
773 /* Where the IO area was mapped on multiquad, always 0 otherwise */
774 void *xquad_portio = NULL;
776 void __init smp_boot_cpus(void)
777 {
778 int apicid, bit;
780 /* Initialize the logical to physical CPU number mapping */
781 init_cpu_to_apicid();
783 /*
784 * Setup boot CPU information
785 */
786 smp_store_cpu_info(0); /* Final full version of the data */
787 printk("CPU%d booted\n", 0);
789 /*
790 * We have the boot CPU online for sure.
791 */
792 set_bit(0, &cpu_online_map);
793 boot_cpu_logical_apicid = logical_smp_processor_id();
794 map_cpu_to_boot_apicid(0, boot_cpu_apicid);
796 /*
797 * If we couldnt find an SMP configuration at boot time,
798 * get out of here now!
799 */
800 if (!smp_found_config) {
801 printk("SMP motherboard not detected.\n");
802 io_apic_irqs = 0;
803 cpu_online_map = phys_cpu_present_map = 1;
804 smp_num_cpus = 1;
805 if (APIC_init_uniprocessor())
806 printk("Local APIC not detected."
807 " Using dummy APIC emulation.\n");
808 goto smp_done;
809 }
811 /*
812 * Should not be necessary because the MP table should list the boot
813 * CPU too, but we do it for the sake of robustness anyway.
814 */
815 if (!test_bit(boot_cpu_physical_apicid, &phys_cpu_present_map)) {
816 printk("weird, boot CPU (#%d) not listed by the BIOS.\n",
817 boot_cpu_physical_apicid);
818 phys_cpu_present_map |= (1 << hard_smp_processor_id());
819 }
821 /*
822 * If we couldn't find a local APIC, then get out of here now!
823 */
824 if (APIC_INTEGRATED(apic_version[boot_cpu_physical_apicid]) &&
825 !test_bit(X86_FEATURE_APIC, boot_cpu_data.x86_capability)) {
826 printk("BIOS bug, local APIC #%d not detected!...\n",
827 boot_cpu_physical_apicid);
828 printk("... forcing use of dummy APIC emulation. (tell your hw vendor)\n");
829 io_apic_irqs = 0;
830 cpu_online_map = phys_cpu_present_map = 1;
831 smp_num_cpus = 1;
832 goto smp_done;
833 }
835 verify_local_APIC();
837 /*
838 * If SMP should be disabled, then really disable it!
839 */
840 if (!max_cpus) {
841 smp_found_config = 0;
842 printk("SMP mode deactivated, forcing use of dummy APIC emulation.\n");
843 io_apic_irqs = 0;
844 cpu_online_map = phys_cpu_present_map = 1;
845 smp_num_cpus = 1;
846 goto smp_done;
847 }
849 connect_bsp_APIC();
850 setup_local_APIC();
852 if (GET_APIC_ID(apic_read(APIC_ID)) != boot_cpu_physical_apicid)
853 BUG();
855 /*
856 * Scan the CPU present map and fire up the other CPUs via do_boot_cpu
857 *
858 * In clustered apic mode, phys_cpu_present_map is a constructed thus:
859 * bits 0-3 are quad0, 4-7 are quad1, etc. A perverse twist on the
860 * clustered apic ID.
861 */
862 Dprintk("CPU present map: %lx\n", phys_cpu_present_map);
864 for (bit = 0; bit < NR_CPUS; bit++) {
865 apicid = cpu_present_to_apicid(bit);
866 /*
867 * Don't even attempt to start the boot CPU!
868 */
869 if (apicid == boot_cpu_apicid)
870 continue;
872 /*
873 * Don't start hyperthreads if option noht requested.
874 */
875 if (opt_noht && (apicid & (ht_per_core - 1)))
876 continue;
878 if (!(phys_cpu_present_map & (1 << bit)))
879 continue;
880 if ((max_cpus >= 0) && (max_cpus <= cpucount+1))
881 continue;
883 do_boot_cpu(apicid);
885 /*
886 * Make sure we unmap all failed CPUs
887 */
888 if ((boot_apicid_to_cpu(apicid) == -1) &&
889 (phys_cpu_present_map & (1 << bit)))
890 printk("CPU #%d not responding - cannot use it.\n",
891 apicid);
892 }
894 /*
895 * Cleanup possible dangling ends...
896 */
897 /*
898 * Install writable page 0 entry to set BIOS data area.
899 */
900 local_flush_tlb();
902 /*
903 * Paranoid: Set warm reset code and vector here back
904 * to default values.
905 */
906 CMOS_WRITE(0, 0xf);
908 *((volatile long *) phys_to_virt(0x467)) = 0;
910 if (!cpucount) {
911 printk("Error: only one processor found.\n");
912 } else {
913 printk("Total of %d processors activated.\n", cpucount+1);
914 }
915 smp_num_cpus = cpucount + 1;
917 Dprintk("Boot done.\n");
919 /*
920 * Here we can be sure that there is an IO-APIC in the system. Let's
921 * go and set it up:
922 */
923 if ( nr_ioapics ) setup_IO_APIC();
925 /* Set up all local APIC timers in the system. */
926 setup_APIC_clocks();
928 /* Synchronize the TSC with the AP(s). */
929 if ( cpucount ) synchronize_tsc_bp();
931 smp_done:
932 ;
933 }
935 #endif /* CONFIG_SMP */
937 /*
938 * Local variables:
939 * mode: C
940 * c-set-style: "BSD"
941 * c-basic-offset: 4
942 * tab-width: 4
943 * indent-tabs-mode: nil
944 * End:
945 */