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