blob: cbea7ac582e57a09fe919e4cc444c53a6a525ba4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 BogoMIPS 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 * Dave Jones : Report invalid combinations of Athlon CPUs.
34* Rusty Russell : Hacked into shape for new "hotplug" boot process. */
35
36#include <linux/module.h>
37#include <linux/config.h>
38#include <linux/init.h>
39#include <linux/kernel.h>
40
41#include <linux/mm.h>
42#include <linux/sched.h>
43#include <linux/kernel_stat.h>
44#include <linux/smp_lock.h>
45#include <linux/irq.h>
46#include <linux/bootmem.h>
47
48#include <linux/delay.h>
49#include <linux/mc146818rtc.h>
50#include <asm/tlbflush.h>
51#include <asm/desc.h>
52#include <asm/arch_hooks.h>
53
54#include <mach_apic.h>
55#include <mach_wakecpu.h>
56#include <smpboot_hooks.h>
57
58/* Set if we find a B stepping CPU */
59static int __initdata smp_b_stepping;
60
61/* Number of siblings per CPU package */
62int smp_num_siblings = 1;
63int phys_proc_id[NR_CPUS]; /* Package ID of each logical CPU */
64EXPORT_SYMBOL(phys_proc_id);
Andi Kleen3dd9d512005-04-16 15:25:15 -070065int cpu_core_id[NR_CPUS]; /* Core ID of each logical CPU */
66EXPORT_SYMBOL(cpu_core_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68/* bitmap of online cpus */
69cpumask_t cpu_online_map;
70
71cpumask_t cpu_callin_map;
72cpumask_t cpu_callout_map;
73static cpumask_t smp_commenced_mask;
74
75/* Per CPU bogomips and other parameters */
76struct cpuinfo_x86 cpu_data[NR_CPUS] __cacheline_aligned;
77
78u8 x86_cpu_to_apicid[NR_CPUS] =
79 { [0 ... NR_CPUS-1] = 0xff };
80EXPORT_SYMBOL(x86_cpu_to_apicid);
81
82/*
83 * Trampoline 80x86 program as an array.
84 */
85
86extern unsigned char trampoline_data [];
87extern unsigned char trampoline_end [];
88static unsigned char *trampoline_base;
89static int trampoline_exec;
90
91static void map_cpu_to_logical_apicid(void);
92
93/*
94 * Currently trivial. Write the real->protected mode
95 * bootstrap into the page concerned. The caller
96 * has made sure it's suitably aligned.
97 */
98
99static unsigned long __init setup_trampoline(void)
100{
101 memcpy(trampoline_base, trampoline_data, trampoline_end - trampoline_data);
102 return virt_to_phys(trampoline_base);
103}
104
105/*
106 * We are called very early to get the low memory for the
107 * SMP bootup trampoline page.
108 */
109void __init smp_alloc_memory(void)
110{
111 trampoline_base = (void *) alloc_bootmem_low_pages(PAGE_SIZE);
112 /*
113 * Has to be in very low memory so we can execute
114 * real-mode AP code.
115 */
116 if (__pa(trampoline_base) >= 0x9F000)
117 BUG();
118 /*
119 * Make the SMP trampoline executable:
120 */
121 trampoline_exec = set_kernel_exec((unsigned long)trampoline_base, 1);
122}
123
124/*
125 * The bootstrap kernel entry code has set these up. Save them for
126 * a given CPU
127 */
128
129static void __init smp_store_cpu_info(int id)
130{
131 struct cpuinfo_x86 *c = cpu_data + id;
132
133 *c = boot_cpu_data;
134 if (id!=0)
135 identify_cpu(c);
136 /*
137 * Mask B, Pentium, but not Pentium MMX
138 */
139 if (c->x86_vendor == X86_VENDOR_INTEL &&
140 c->x86 == 5 &&
141 c->x86_mask >= 1 && c->x86_mask <= 4 &&
142 c->x86_model <= 3)
143 /*
144 * Remember we have B step Pentia with bugs
145 */
146 smp_b_stepping = 1;
147
148 /*
149 * Certain Athlons might work (for various values of 'work') in SMP
150 * but they are not certified as MP capable.
151 */
152 if ((c->x86_vendor == X86_VENDOR_AMD) && (c->x86 == 6)) {
153
154 /* Athlon 660/661 is valid. */
155 if ((c->x86_model==6) && ((c->x86_mask==0) || (c->x86_mask==1)))
156 goto valid_k7;
157
158 /* Duron 670 is valid */
159 if ((c->x86_model==7) && (c->x86_mask==0))
160 goto valid_k7;
161
162 /*
163 * Athlon 662, Duron 671, and Athlon >model 7 have capability bit.
164 * It's worth noting that the A5 stepping (662) of some Athlon XP's
165 * have the MP bit set.
166 * See http://www.heise.de/newsticker/data/jow-18.10.01-000 for more.
167 */
168 if (((c->x86_model==6) && (c->x86_mask>=2)) ||
169 ((c->x86_model==7) && (c->x86_mask>=1)) ||
170 (c->x86_model> 7))
171 if (cpu_has_mp)
172 goto valid_k7;
173
174 /* If we get here, it's not a certified SMP capable AMD system. */
175 tainted |= TAINT_UNSAFE_SMP;
176 }
177
178valid_k7:
179 ;
180}
181
182/*
183 * TSC synchronization.
184 *
185 * We first check whether all CPUs have their TSC's synchronized,
186 * then we print a warning if not, and always resync.
187 */
188
189static atomic_t tsc_start_flag = ATOMIC_INIT(0);
190static atomic_t tsc_count_start = ATOMIC_INIT(0);
191static atomic_t tsc_count_stop = ATOMIC_INIT(0);
192static unsigned long long tsc_values[NR_CPUS];
193
194#define NR_LOOPS 5
195
196static void __init synchronize_tsc_bp (void)
197{
198 int i;
199 unsigned long long t0;
200 unsigned long long sum, avg;
201 long long delta;
202 unsigned long one_usec;
203 int buggy = 0;
204
205 printk(KERN_INFO "checking TSC synchronization across %u CPUs: ", num_booting_cpus());
206
207 /* convert from kcyc/sec to cyc/usec */
208 one_usec = cpu_khz / 1000;
209
210 atomic_set(&tsc_start_flag, 1);
211 wmb();
212
213 /*
214 * We loop a few times to get a primed instruction cache,
215 * then the last pass is more or less synchronized and
216 * the BP and APs set their cycle counters to zero all at
217 * once. This reduces the chance of having random offsets
218 * between the processors, and guarantees that the maximum
219 * delay between the cycle counters is never bigger than
220 * the latency of information-passing (cachelines) between
221 * two CPUs.
222 */
223 for (i = 0; i < NR_LOOPS; i++) {
224 /*
225 * all APs synchronize but they loop on '== num_cpus'
226 */
227 while (atomic_read(&tsc_count_start) != num_booting_cpus()-1)
228 mb();
229 atomic_set(&tsc_count_stop, 0);
230 wmb();
231 /*
232 * this lets the APs save their current TSC:
233 */
234 atomic_inc(&tsc_count_start);
235
236 rdtscll(tsc_values[smp_processor_id()]);
237 /*
238 * We clear the TSC in the last loop:
239 */
240 if (i == NR_LOOPS-1)
241 write_tsc(0, 0);
242
243 /*
244 * Wait for all APs to leave the synchronization point:
245 */
246 while (atomic_read(&tsc_count_stop) != num_booting_cpus()-1)
247 mb();
248 atomic_set(&tsc_count_start, 0);
249 wmb();
250 atomic_inc(&tsc_count_stop);
251 }
252
253 sum = 0;
254 for (i = 0; i < NR_CPUS; i++) {
255 if (cpu_isset(i, cpu_callout_map)) {
256 t0 = tsc_values[i];
257 sum += t0;
258 }
259 }
260 avg = sum;
261 do_div(avg, num_booting_cpus());
262
263 sum = 0;
264 for (i = 0; i < NR_CPUS; i++) {
265 if (!cpu_isset(i, cpu_callout_map))
266 continue;
267 delta = tsc_values[i] - avg;
268 if (delta < 0)
269 delta = -delta;
270 /*
271 * We report bigger than 2 microseconds clock differences.
272 */
273 if (delta > 2*one_usec) {
274 long realdelta;
275 if (!buggy) {
276 buggy = 1;
277 printk("\n");
278 }
279 realdelta = delta;
280 do_div(realdelta, one_usec);
281 if (tsc_values[i] < avg)
282 realdelta = -realdelta;
283
284 printk(KERN_INFO "CPU#%d had %ld usecs TSC skew, fixed it up.\n", i, realdelta);
285 }
286
287 sum += delta;
288 }
289 if (!buggy)
290 printk("passed.\n");
291}
292
293static void __init synchronize_tsc_ap (void)
294{
295 int i;
296
297 /*
298 * Not every cpu is online at the time
299 * this gets called, so we first wait for the BP to
300 * finish SMP initialization:
301 */
302 while (!atomic_read(&tsc_start_flag)) mb();
303
304 for (i = 0; i < NR_LOOPS; i++) {
305 atomic_inc(&tsc_count_start);
306 while (atomic_read(&tsc_count_start) != num_booting_cpus())
307 mb();
308
309 rdtscll(tsc_values[smp_processor_id()]);
310 if (i == NR_LOOPS-1)
311 write_tsc(0, 0);
312
313 atomic_inc(&tsc_count_stop);
314 while (atomic_read(&tsc_count_stop) != num_booting_cpus()) mb();
315 }
316}
317#undef NR_LOOPS
318
319extern void calibrate_delay(void);
320
321static atomic_t init_deasserted;
322
323static void __init smp_callin(void)
324{
325 int cpuid, phys_id;
326 unsigned long timeout;
327
328 /*
329 * If waken up by an INIT in an 82489DX configuration
330 * we may get here before an INIT-deassert IPI reaches
331 * our local APIC. We have to wait for the IPI or we'll
332 * lock up on an APIC access.
333 */
334 wait_for_init_deassert(&init_deasserted);
335
336 /*
337 * (This works even if the APIC is not enabled.)
338 */
339 phys_id = GET_APIC_ID(apic_read(APIC_ID));
340 cpuid = smp_processor_id();
341 if (cpu_isset(cpuid, cpu_callin_map)) {
342 printk("huh, phys CPU#%d, CPU#%d already present??\n",
343 phys_id, cpuid);
344 BUG();
345 }
346 Dprintk("CPU#%d (phys ID: %d) waiting for CALLOUT\n", cpuid, phys_id);
347
348 /*
349 * STARTUP IPIs are fragile beasts as they might sometimes
350 * trigger some glue motherboard logic. Complete APIC bus
351 * silence for 1 second, this overestimates the time the
352 * boot CPU is spending to send the up to 2 STARTUP IPIs
353 * by a factor of two. This should be enough.
354 */
355
356 /*
357 * Waiting 2s total for startup (udelay is not yet working)
358 */
359 timeout = jiffies + 2*HZ;
360 while (time_before(jiffies, timeout)) {
361 /*
362 * Has the boot CPU finished it's STARTUP sequence?
363 */
364 if (cpu_isset(cpuid, cpu_callout_map))
365 break;
366 rep_nop();
367 }
368
369 if (!time_before(jiffies, timeout)) {
370 printk("BUG: CPU%d started up but did not get a callout!\n",
371 cpuid);
372 BUG();
373 }
374
375 /*
376 * the boot CPU has finished the init stage and is spinning
377 * on callin_map until we finish. We are free to set up this
378 * CPU, first the APIC. (this is probably redundant on most
379 * boards)
380 */
381
382 Dprintk("CALLIN, before setup_local_APIC().\n");
383 smp_callin_clear_local_apic();
384 setup_local_APIC();
385 map_cpu_to_logical_apicid();
386
387 /*
388 * Get our bogomips.
389 */
390 calibrate_delay();
391 Dprintk("Stack at about %p\n",&cpuid);
392
393 /*
394 * Save our processor parameters
395 */
396 smp_store_cpu_info(cpuid);
397
398 disable_APIC_timer();
399
400 /*
401 * Allow the master to continue.
402 */
403 cpu_set(cpuid, cpu_callin_map);
404
405 /*
406 * Synchronize the TSC with the BP
407 */
408 if (cpu_has_tsc && cpu_khz)
409 synchronize_tsc_ap();
410}
411
412static int cpucount;
413
414/*
415 * Activate a secondary processor.
416 */
417static void __init start_secondary(void *unused)
418{
419 /*
420 * Dont put anything before smp_callin(), SMP
421 * booting is too fragile that we want to limit the
422 * things done here to the most necessary things.
423 */
424 cpu_init();
425 smp_callin();
426 while (!cpu_isset(smp_processor_id(), smp_commenced_mask))
427 rep_nop();
428 setup_secondary_APIC_clock();
429 if (nmi_watchdog == NMI_IO_APIC) {
430 disable_8259A_irq(0);
431 enable_NMI_through_LVT0(NULL);
432 enable_8259A_irq(0);
433 }
434 enable_APIC_timer();
435 /*
436 * low-memory mappings have been cleared, flush them from
437 * the local TLBs too.
438 */
439 local_flush_tlb();
440 cpu_set(smp_processor_id(), cpu_online_map);
441
442 /* We can take interrupts now: we're officially "up". */
443 local_irq_enable();
444
445 wmb();
446 cpu_idle();
447}
448
449/*
450 * Everything has been set up for the secondary
451 * CPUs - they just need to reload everything
452 * from the task structure
453 * This function must not return.
454 */
455void __init initialize_secondary(void)
456{
457 /*
458 * We don't actually need to load the full TSS,
459 * basically just the stack pointer and the eip.
460 */
461
462 asm volatile(
463 "movl %0,%%esp\n\t"
464 "jmp *%1"
465 :
466 :"r" (current->thread.esp),"r" (current->thread.eip));
467}
468
469extern struct {
470 void * esp;
471 unsigned short ss;
472} stack_start;
473
474#ifdef CONFIG_NUMA
475
476/* which logical CPUs are on which nodes */
477cpumask_t node_2_cpu_mask[MAX_NUMNODES] =
478 { [0 ... MAX_NUMNODES-1] = CPU_MASK_NONE };
479/* which node each logical CPU is on */
480int cpu_2_node[NR_CPUS] = { [0 ... NR_CPUS-1] = 0 };
481EXPORT_SYMBOL(cpu_2_node);
482
483/* set up a mapping between cpu and node. */
484static inline void map_cpu_to_node(int cpu, int node)
485{
486 printk("Mapping cpu %d to node %d\n", cpu, node);
487 cpu_set(cpu, node_2_cpu_mask[node]);
488 cpu_2_node[cpu] = node;
489}
490
491/* undo a mapping between cpu and node. */
492static inline void unmap_cpu_to_node(int cpu)
493{
494 int node;
495
496 printk("Unmapping cpu %d from all nodes\n", cpu);
497 for (node = 0; node < MAX_NUMNODES; node ++)
498 cpu_clear(cpu, node_2_cpu_mask[node]);
499 cpu_2_node[cpu] = 0;
500}
501#else /* !CONFIG_NUMA */
502
503#define map_cpu_to_node(cpu, node) ({})
504#define unmap_cpu_to_node(cpu) ({})
505
506#endif /* CONFIG_NUMA */
507
508u8 cpu_2_logical_apicid[NR_CPUS] = { [0 ... NR_CPUS-1] = BAD_APICID };
509
510static void map_cpu_to_logical_apicid(void)
511{
512 int cpu = smp_processor_id();
513 int apicid = logical_smp_processor_id();
514
515 cpu_2_logical_apicid[cpu] = apicid;
516 map_cpu_to_node(cpu, apicid_to_node(apicid));
517}
518
519static void unmap_cpu_to_logical_apicid(int cpu)
520{
521 cpu_2_logical_apicid[cpu] = BAD_APICID;
522 unmap_cpu_to_node(cpu);
523}
524
525#if APIC_DEBUG
526static inline void __inquire_remote_apic(int apicid)
527{
528 int i, regs[] = { APIC_ID >> 4, APIC_LVR >> 4, APIC_SPIV >> 4 };
529 char *names[] = { "ID", "VERSION", "SPIV" };
530 int timeout, status;
531
532 printk("Inquiring remote APIC #%d...\n", apicid);
533
534 for (i = 0; i < sizeof(regs) / sizeof(*regs); i++) {
535 printk("... APIC #%d %s: ", apicid, names[i]);
536
537 /*
538 * Wait for idle.
539 */
540 apic_wait_icr_idle();
541
542 apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(apicid));
543 apic_write_around(APIC_ICR, APIC_DM_REMRD | regs[i]);
544
545 timeout = 0;
546 do {
547 udelay(100);
548 status = apic_read(APIC_ICR) & APIC_ICR_RR_MASK;
549 } while (status == APIC_ICR_RR_INPROG && timeout++ < 1000);
550
551 switch (status) {
552 case APIC_ICR_RR_VALID:
553 status = apic_read(APIC_RRR);
554 printk("%08x\n", status);
555 break;
556 default:
557 printk("failed\n");
558 }
559 }
560}
561#endif
562
563#ifdef WAKE_SECONDARY_VIA_NMI
564/*
565 * Poke the other CPU in the eye via NMI to wake it up. Remember that the normal
566 * INIT, INIT, STARTUP sequence will reset the chip hard for us, and this
567 * won't ... remember to clear down the APIC, etc later.
568 */
569static int __init
570wakeup_secondary_cpu(int logical_apicid, unsigned long start_eip)
571{
572 unsigned long send_status = 0, accept_status = 0;
573 int timeout, maxlvt;
574
575 /* Target chip */
576 apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(logical_apicid));
577
578 /* Boot on the stack */
579 /* Kick the second */
580 apic_write_around(APIC_ICR, APIC_DM_NMI | APIC_DEST_LOGICAL);
581
582 Dprintk("Waiting for send to finish...\n");
583 timeout = 0;
584 do {
585 Dprintk("+");
586 udelay(100);
587 send_status = apic_read(APIC_ICR) & APIC_ICR_BUSY;
588 } while (send_status && (timeout++ < 1000));
589
590 /*
591 * Give the other CPU some time to accept the IPI.
592 */
593 udelay(200);
594 /*
595 * Due to the Pentium erratum 3AP.
596 */
597 maxlvt = get_maxlvt();
598 if (maxlvt > 3) {
599 apic_read_around(APIC_SPIV);
600 apic_write(APIC_ESR, 0);
601 }
602 accept_status = (apic_read(APIC_ESR) & 0xEF);
603 Dprintk("NMI sent.\n");
604
605 if (send_status)
606 printk("APIC never delivered???\n");
607 if (accept_status)
608 printk("APIC delivery error (%lx).\n", accept_status);
609
610 return (send_status | accept_status);
611}
612#endif /* WAKE_SECONDARY_VIA_NMI */
613
614#ifdef WAKE_SECONDARY_VIA_INIT
615static int __init
616wakeup_secondary_cpu(int phys_apicid, unsigned long start_eip)
617{
618 unsigned long send_status = 0, accept_status = 0;
619 int maxlvt, timeout, num_starts, j;
620
621 /*
622 * Be paranoid about clearing APIC errors.
623 */
624 if (APIC_INTEGRATED(apic_version[phys_apicid])) {
625 apic_read_around(APIC_SPIV);
626 apic_write(APIC_ESR, 0);
627 apic_read(APIC_ESR);
628 }
629
630 Dprintk("Asserting INIT.\n");
631
632 /*
633 * Turn INIT on target chip
634 */
635 apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
636
637 /*
638 * Send IPI
639 */
640 apic_write_around(APIC_ICR, APIC_INT_LEVELTRIG | APIC_INT_ASSERT
641 | APIC_DM_INIT);
642
643 Dprintk("Waiting for send to finish...\n");
644 timeout = 0;
645 do {
646 Dprintk("+");
647 udelay(100);
648 send_status = apic_read(APIC_ICR) & APIC_ICR_BUSY;
649 } while (send_status && (timeout++ < 1000));
650
651 mdelay(10);
652
653 Dprintk("Deasserting INIT.\n");
654
655 /* Target chip */
656 apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
657
658 /* Send IPI */
659 apic_write_around(APIC_ICR, APIC_INT_LEVELTRIG | APIC_DM_INIT);
660
661 Dprintk("Waiting for send to finish...\n");
662 timeout = 0;
663 do {
664 Dprintk("+");
665 udelay(100);
666 send_status = apic_read(APIC_ICR) & APIC_ICR_BUSY;
667 } while (send_status && (timeout++ < 1000));
668
669 atomic_set(&init_deasserted, 1);
670
671 /*
672 * Should we send STARTUP IPIs ?
673 *
674 * Determine this based on the APIC version.
675 * If we don't have an integrated APIC, don't send the STARTUP IPIs.
676 */
677 if (APIC_INTEGRATED(apic_version[phys_apicid]))
678 num_starts = 2;
679 else
680 num_starts = 0;
681
682 /*
683 * Run STARTUP IPI loop.
684 */
685 Dprintk("#startup loops: %d.\n", num_starts);
686
687 maxlvt = get_maxlvt();
688
689 for (j = 1; j <= num_starts; j++) {
690 Dprintk("Sending STARTUP #%d.\n",j);
691 apic_read_around(APIC_SPIV);
692 apic_write(APIC_ESR, 0);
693 apic_read(APIC_ESR);
694 Dprintk("After apic_write.\n");
695
696 /*
697 * STARTUP IPI
698 */
699
700 /* Target chip */
701 apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
702
703 /* Boot on the stack */
704 /* Kick the second */
705 apic_write_around(APIC_ICR, APIC_DM_STARTUP
706 | (start_eip >> 12));
707
708 /*
709 * Give the other CPU some time to accept the IPI.
710 */
711 udelay(300);
712
713 Dprintk("Startup point 1.\n");
714
715 Dprintk("Waiting for send to finish...\n");
716 timeout = 0;
717 do {
718 Dprintk("+");
719 udelay(100);
720 send_status = apic_read(APIC_ICR) & APIC_ICR_BUSY;
721 } while (send_status && (timeout++ < 1000));
722
723 /*
724 * Give the other CPU some time to accept the IPI.
725 */
726 udelay(200);
727 /*
728 * Due to the Pentium erratum 3AP.
729 */
730 if (maxlvt > 3) {
731 apic_read_around(APIC_SPIV);
732 apic_write(APIC_ESR, 0);
733 }
734 accept_status = (apic_read(APIC_ESR) & 0xEF);
735 if (send_status || accept_status)
736 break;
737 }
738 Dprintk("After Startup.\n");
739
740 if (send_status)
741 printk("APIC never delivered???\n");
742 if (accept_status)
743 printk("APIC delivery error (%lx).\n", accept_status);
744
745 return (send_status | accept_status);
746}
747#endif /* WAKE_SECONDARY_VIA_INIT */
748
749extern cpumask_t cpu_initialized;
750
751static int __init do_boot_cpu(int apicid)
752/*
753 * NOTE - on most systems this is a PHYSICAL apic ID, but on multiquad
754 * (ie clustered apic addressing mode), this is a LOGICAL apic ID.
755 * Returns zero if CPU booted OK, else error code from wakeup_secondary_cpu.
756 */
757{
758 struct task_struct *idle;
759 unsigned long boot_error;
760 int timeout, cpu;
761 unsigned long start_eip;
762 unsigned short nmi_high = 0, nmi_low = 0;
763
764 cpu = ++cpucount;
765 /*
766 * We can't use kernel_thread since we must avoid to
767 * reschedule the child.
768 */
769 idle = fork_idle(cpu);
770 if (IS_ERR(idle))
771 panic("failed fork for CPU %d", cpu);
772 idle->thread.eip = (unsigned long) start_secondary;
773 /* start_eip had better be page-aligned! */
774 start_eip = setup_trampoline();
775
776 /* So we see what's up */
777 printk("Booting processor %d/%d eip %lx\n", cpu, apicid, start_eip);
778 /* Stack for startup_32 can be just as for start_secondary onwards */
779 stack_start.esp = (void *) idle->thread.esp;
780
781 irq_ctx_init(cpu);
782
783 /*
784 * This grunge runs the startup process for
785 * the targeted processor.
786 */
787
788 atomic_set(&init_deasserted, 0);
789
790 Dprintk("Setting warm reset code and vector.\n");
791
792 store_NMI_vector(&nmi_high, &nmi_low);
793
794 smpboot_setup_warm_reset_vector(start_eip);
795
796 /*
797 * Starting actual IPI sequence...
798 */
799 boot_error = wakeup_secondary_cpu(apicid, start_eip);
800
801 if (!boot_error) {
802 /*
803 * allow APs to start initializing.
804 */
805 Dprintk("Before Callout %d.\n", cpu);
806 cpu_set(cpu, cpu_callout_map);
807 Dprintk("After Callout %d.\n", cpu);
808
809 /*
810 * Wait 5s total for a response
811 */
812 for (timeout = 0; timeout < 50000; timeout++) {
813 if (cpu_isset(cpu, cpu_callin_map))
814 break; /* It has booted */
815 udelay(100);
816 }
817
818 if (cpu_isset(cpu, cpu_callin_map)) {
819 /* number CPUs logically, starting from 1 (BSP is 0) */
820 Dprintk("OK.\n");
821 printk("CPU%d: ", cpu);
822 print_cpu_info(&cpu_data[cpu]);
823 Dprintk("CPU has booted.\n");
824 } else {
825 boot_error= 1;
826 if (*((volatile unsigned char *)trampoline_base)
827 == 0xA5)
828 /* trampoline started but...? */
829 printk("Stuck ??\n");
830 else
831 /* trampoline code not run */
832 printk("Not responding.\n");
833 inquire_remote_apic(apicid);
834 }
835 }
836 x86_cpu_to_apicid[cpu] = apicid;
837 if (boot_error) {
838 /* Try to put things back the way they were before ... */
839 unmap_cpu_to_logical_apicid(cpu);
840 cpu_clear(cpu, cpu_callout_map); /* was set here (do_boot_cpu()) */
841 cpu_clear(cpu, cpu_initialized); /* was set by cpu_init() */
842 cpucount--;
843 }
844
845 /* mark "stuck" area as not stuck */
846 *((volatile unsigned long *)trampoline_base) = 0;
847
848 return boot_error;
849}
850
851static void smp_tune_scheduling (void)
852{
853 unsigned long cachesize; /* kB */
854 unsigned long bandwidth = 350; /* MB/s */
855 /*
856 * Rough estimation for SMP scheduling, this is the number of
857 * cycles it takes for a fully memory-limited process to flush
858 * the SMP-local cache.
859 *
860 * (For a P5 this pretty much means we will choose another idle
861 * CPU almost always at wakeup time (this is due to the small
862 * L1 cache), on PIIs it's around 50-100 usecs, depending on
863 * the cache size)
864 */
865
866 if (!cpu_khz) {
867 /*
868 * this basically disables processor-affinity
869 * scheduling on SMP without a TSC.
870 */
871 return;
872 } else {
873 cachesize = boot_cpu_data.x86_cache_size;
874 if (cachesize == -1) {
875 cachesize = 16; /* Pentiums, 2x8kB cache */
876 bandwidth = 100;
877 }
878 }
879}
880
881/*
882 * Cycle through the processors sending APIC IPIs to boot each.
883 */
884
885static int boot_cpu_logical_apicid;
886/* Where the IO area was mapped on multiquad, always 0 otherwise */
887void *xquad_portio;
888
889cpumask_t cpu_sibling_map[NR_CPUS] __cacheline_aligned;
Andi Kleen3dd9d512005-04-16 15:25:15 -0700890cpumask_t cpu_core_map[NR_CPUS] __cacheline_aligned;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
892static void __init smp_boot_cpus(unsigned int max_cpus)
893{
894 int apicid, cpu, bit, kicked;
895 unsigned long bogosum = 0;
896
897 /*
898 * Setup boot CPU information
899 */
900 smp_store_cpu_info(0); /* Final full version of the data */
901 printk("CPU%d: ", 0);
902 print_cpu_info(&cpu_data[0]);
903
904 boot_cpu_physical_apicid = GET_APIC_ID(apic_read(APIC_ID));
905 boot_cpu_logical_apicid = logical_smp_processor_id();
906 x86_cpu_to_apicid[0] = boot_cpu_physical_apicid;
907
908 current_thread_info()->cpu = 0;
909 smp_tune_scheduling();
910 cpus_clear(cpu_sibling_map[0]);
911 cpu_set(0, cpu_sibling_map[0]);
912
Andi Kleen3dd9d512005-04-16 15:25:15 -0700913 cpus_clear(cpu_core_map[0]);
914 cpu_set(0, cpu_core_map[0]);
915
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 /*
917 * If we couldn't find an SMP configuration at boot time,
918 * get out of here now!
919 */
920 if (!smp_found_config && !acpi_lapic) {
921 printk(KERN_NOTICE "SMP motherboard not detected.\n");
922 smpboot_clear_io_apic_irqs();
923 phys_cpu_present_map = physid_mask_of_physid(0);
924 if (APIC_init_uniprocessor())
925 printk(KERN_NOTICE "Local APIC not detected."
926 " Using dummy APIC emulation.\n");
927 map_cpu_to_logical_apicid();
Andi Kleen3dd9d512005-04-16 15:25:15 -0700928 cpu_set(0, cpu_sibling_map[0]);
929 cpu_set(0, cpu_core_map[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 return;
931 }
932
933 /*
934 * Should not be necessary because the MP table should list the boot
935 * CPU too, but we do it for the sake of robustness anyway.
936 * Makes no sense to do this check in clustered apic mode, so skip it
937 */
938 if (!check_phys_apicid_present(boot_cpu_physical_apicid)) {
939 printk("weird, boot CPU (#%d) not listed by the BIOS.\n",
940 boot_cpu_physical_apicid);
941 physid_set(hard_smp_processor_id(), phys_cpu_present_map);
942 }
943
944 /*
945 * If we couldn't find a local APIC, then get out of here now!
946 */
947 if (APIC_INTEGRATED(apic_version[boot_cpu_physical_apicid]) && !cpu_has_apic) {
948 printk(KERN_ERR "BIOS bug, local APIC #%d not detected!...\n",
949 boot_cpu_physical_apicid);
950 printk(KERN_ERR "... forcing use of dummy APIC emulation. (tell your hw vendor)\n");
951 smpboot_clear_io_apic_irqs();
952 phys_cpu_present_map = physid_mask_of_physid(0);
Andi Kleen3dd9d512005-04-16 15:25:15 -0700953 cpu_set(0, cpu_sibling_map[0]);
954 cpu_set(0, cpu_core_map[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 return;
956 }
957
958 verify_local_APIC();
959
960 /*
961 * If SMP should be disabled, then really disable it!
962 */
963 if (!max_cpus) {
964 smp_found_config = 0;
965 printk(KERN_INFO "SMP mode deactivated, forcing use of dummy APIC emulation.\n");
966 smpboot_clear_io_apic_irqs();
967 phys_cpu_present_map = physid_mask_of_physid(0);
Andi Kleen3dd9d512005-04-16 15:25:15 -0700968 cpu_set(0, cpu_sibling_map[0]);
969 cpu_set(0, cpu_core_map[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 return;
971 }
972
973 connect_bsp_APIC();
974 setup_local_APIC();
975 map_cpu_to_logical_apicid();
976
977
978 setup_portio_remap();
979
980 /*
981 * Scan the CPU present map and fire up the other CPUs via do_boot_cpu
982 *
983 * In clustered apic mode, phys_cpu_present_map is a constructed thus:
984 * bits 0-3 are quad0, 4-7 are quad1, etc. A perverse twist on the
985 * clustered apic ID.
986 */
987 Dprintk("CPU present map: %lx\n", physids_coerce(phys_cpu_present_map));
988
989 kicked = 1;
990 for (bit = 0; kicked < NR_CPUS && bit < MAX_APICS; bit++) {
991 apicid = cpu_present_to_apicid(bit);
992 /*
993 * Don't even attempt to start the boot CPU!
994 */
995 if ((apicid == boot_cpu_apicid) || (apicid == BAD_APICID))
996 continue;
997
998 if (!check_apicid_present(bit))
999 continue;
1000 if (max_cpus <= cpucount+1)
1001 continue;
1002
1003 if (do_boot_cpu(apicid))
1004 printk("CPU #%d not responding - cannot use it.\n",
1005 apicid);
1006 else
1007 ++kicked;
1008 }
1009
1010 /*
1011 * Cleanup possible dangling ends...
1012 */
1013 smpboot_restore_warm_reset_vector();
1014
1015 /*
1016 * Allow the user to impress friends.
1017 */
1018 Dprintk("Before bogomips.\n");
1019 for (cpu = 0; cpu < NR_CPUS; cpu++)
1020 if (cpu_isset(cpu, cpu_callout_map))
1021 bogosum += cpu_data[cpu].loops_per_jiffy;
1022 printk(KERN_INFO
1023 "Total of %d processors activated (%lu.%02lu BogoMIPS).\n",
1024 cpucount+1,
1025 bogosum/(500000/HZ),
1026 (bogosum/(5000/HZ))%100);
1027
1028 Dprintk("Before bogocount - setting activated=1.\n");
1029
1030 if (smp_b_stepping)
1031 printk(KERN_WARNING "WARNING: SMP operation may be unreliable with B stepping processors.\n");
1032
1033 /*
1034 * Don't taint if we are running SMP kernel on a single non-MP
1035 * approved Athlon
1036 */
1037 if (tainted & TAINT_UNSAFE_SMP) {
1038 if (cpucount)
1039 printk (KERN_INFO "WARNING: This combination of AMD processors is not suitable for SMP.\n");
1040 else
1041 tainted &= ~TAINT_UNSAFE_SMP;
1042 }
1043
1044 Dprintk("Boot done.\n");
1045
1046 /*
1047 * construct cpu_sibling_map[], so that we can tell sibling CPUs
1048 * efficiently.
1049 */
Andi Kleen3dd9d512005-04-16 15:25:15 -07001050 for (cpu = 0; cpu < NR_CPUS; cpu++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 cpus_clear(cpu_sibling_map[cpu]);
Andi Kleen3dd9d512005-04-16 15:25:15 -07001052 cpus_clear(cpu_core_map[cpu]);
1053 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
1055 for (cpu = 0; cpu < NR_CPUS; cpu++) {
Andi Kleen3dd9d512005-04-16 15:25:15 -07001056 struct cpuinfo_x86 *c = cpu_data + cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 int siblings = 0;
1058 int i;
1059 if (!cpu_isset(cpu, cpu_callout_map))
1060 continue;
1061
1062 if (smp_num_siblings > 1) {
1063 for (i = 0; i < NR_CPUS; i++) {
1064 if (!cpu_isset(i, cpu_callout_map))
1065 continue;
Andi Kleen3dd9d512005-04-16 15:25:15 -07001066 if (cpu_core_id[cpu] == cpu_core_id[i]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 siblings++;
1068 cpu_set(i, cpu_sibling_map[cpu]);
1069 }
1070 }
1071 } else {
1072 siblings++;
1073 cpu_set(cpu, cpu_sibling_map[cpu]);
1074 }
1075
1076 if (siblings != smp_num_siblings)
1077 printk(KERN_WARNING "WARNING: %d siblings found for CPU%d, should be %d\n", siblings, cpu, smp_num_siblings);
Andi Kleen3dd9d512005-04-16 15:25:15 -07001078
1079 if (c->x86_num_cores > 1) {
1080 for (i = 0; i < NR_CPUS; i++) {
1081 if (!cpu_isset(i, cpu_callout_map))
1082 continue;
1083 if (phys_proc_id[cpu] == phys_proc_id[i]) {
1084 cpu_set(i, cpu_core_map[cpu]);
1085 }
1086 }
1087 } else {
1088 cpu_core_map[cpu] = cpu_sibling_map[cpu];
1089 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 }
1091
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 smpboot_setup_io_apic();
1093
1094 setup_boot_APIC_clock();
1095
1096 /*
1097 * Synchronize the TSC with the AP
1098 */
1099 if (cpu_has_tsc && cpucount && cpu_khz)
1100 synchronize_tsc_bp();
1101}
1102
1103/* These are wrappers to interface to the new boot process. Someone
1104 who understands all this stuff should rewrite it properly. --RR 15/Jul/02 */
1105void __init smp_prepare_cpus(unsigned int max_cpus)
1106{
1107 smp_boot_cpus(max_cpus);
1108}
1109
1110void __devinit smp_prepare_boot_cpu(void)
1111{
1112 cpu_set(smp_processor_id(), cpu_online_map);
1113 cpu_set(smp_processor_id(), cpu_callout_map);
1114}
1115
1116int __devinit __cpu_up(unsigned int cpu)
1117{
1118 /* This only works at boot for x86. See "rewrite" above. */
1119 if (cpu_isset(cpu, smp_commenced_mask)) {
1120 local_irq_enable();
1121 return -ENOSYS;
1122 }
1123
1124 /* In case one didn't come up */
1125 if (!cpu_isset(cpu, cpu_callin_map)) {
1126 local_irq_enable();
1127 return -EIO;
1128 }
1129
1130 local_irq_enable();
1131 /* Unleash the CPU! */
1132 cpu_set(cpu, smp_commenced_mask);
1133 while (!cpu_isset(cpu, cpu_online_map))
1134 mb();
1135 return 0;
1136}
1137
1138void __init smp_cpus_done(unsigned int max_cpus)
1139{
1140#ifdef CONFIG_X86_IO_APIC
1141 setup_ioapic_dest();
1142#endif
1143 zap_low_mappings();
1144 /*
1145 * Disable executability of the SMP trampoline:
1146 */
1147 set_kernel_exec((unsigned long)trampoline_base, trampoline_exec);
1148}
1149
1150void __init smp_intr_init(void)
1151{
1152 /*
1153 * IRQ0 must be given a fixed assignment and initialized,
1154 * because it's used before the IO-APIC is set up.
1155 */
1156 set_intr_gate(FIRST_DEVICE_VECTOR, interrupt[0]);
1157
1158 /*
1159 * The reschedule interrupt is a CPU-to-CPU reschedule-helper
1160 * IPI, driven by wakeup.
1161 */
1162 set_intr_gate(RESCHEDULE_VECTOR, reschedule_interrupt);
1163
1164 /* IPI for invalidation */
1165 set_intr_gate(INVALIDATE_TLB_VECTOR, invalidate_interrupt);
1166
1167 /* IPI for generic function call */
1168 set_intr_gate(CALL_FUNCTION_VECTOR, call_function_interrupt);
1169}