]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - arch/x86_64/kernel/smpboot.c
66e98659d077ea9c6f9ab8ce3890aa7318113d63
[mirror_ubuntu-bionic-kernel.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/config.h>
42 #include <linux/init.h>
43
44 #include <linux/mm.h>
45 #include <linux/kernel_stat.h>
46 #include <linux/smp_lock.h>
47 #include <linux/bootmem.h>
48 #include <linux/thread_info.h>
49 #include <linux/module.h>
50
51 #include <linux/delay.h>
52 #include <linux/mc146818rtc.h>
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 /* Package ID of each logical CPU */
67 u8 phys_proc_id[NR_CPUS] __read_mostly = { [0 ... NR_CPUS-1] = BAD_APICID };
68 /* core ID of each logical CPU */
69 u8 cpu_core_id[NR_CPUS] __read_mostly = { [0 ... NR_CPUS-1] = BAD_APICID };
70
71 /* Bitmask of currently online CPUs */
72 cpumask_t cpu_online_map __read_mostly;
73
74 EXPORT_SYMBOL(cpu_online_map);
75
76 /*
77 * Private maps to synchronize booting between AP and BP.
78 * Probably not needed anymore, but it makes for easier debugging. -AK
79 */
80 cpumask_t cpu_callin_map;
81 cpumask_t cpu_callout_map;
82
83 cpumask_t cpu_possible_map;
84 EXPORT_SYMBOL(cpu_possible_map);
85
86 /* Per CPU bogomips and other parameters */
87 struct cpuinfo_x86 cpu_data[NR_CPUS] __cacheline_aligned;
88
89 /* Set when the idlers are all forked */
90 int smp_threads_ready;
91
92 /* representing HT siblings of each logical CPU */
93 cpumask_t cpu_sibling_map[NR_CPUS] __read_mostly;
94
95 /* representing HT and core siblings of each logical CPU */
96 cpumask_t cpu_core_map[NR_CPUS] __read_mostly;
97 EXPORT_SYMBOL(cpu_core_map);
98
99 /*
100 * Trampoline 80x86 program as an array.
101 */
102
103 extern unsigned char trampoline_data[];
104 extern unsigned char trampoline_end[];
105
106 /* State of each CPU */
107 DEFINE_PER_CPU(int, cpu_state) = { 0 };
108
109 /*
110 * Store all idle threads, this can be reused instead of creating
111 * a new thread. Also avoids complicated thread destroy functionality
112 * for idle threads.
113 */
114 struct task_struct *idle_thread_array[NR_CPUS] __cpuinitdata ;
115
116 #define get_idle_for_cpu(x) (idle_thread_array[(x)])
117 #define set_idle_for_cpu(x,p) (idle_thread_array[(x)] = (p))
118
119 /*
120 * Currently trivial. Write the real->protected mode
121 * bootstrap into the page concerned. The caller
122 * has made sure it's suitably aligned.
123 */
124
125 static unsigned long __cpuinit setup_trampoline(void)
126 {
127 void *tramp = __va(SMP_TRAMPOLINE_BASE);
128 memcpy(tramp, trampoline_data, trampoline_end - trampoline_data);
129 return virt_to_phys(tramp);
130 }
131
132 /*
133 * The bootstrap kernel entry code has set these up. Save them for
134 * a given CPU
135 */
136
137 static void __cpuinit smp_store_cpu_info(int id)
138 {
139 struct cpuinfo_x86 *c = cpu_data + id;
140
141 *c = boot_cpu_data;
142 identify_cpu(c);
143 print_cpu_info(c);
144 }
145
146 /*
147 * New Funky TSC sync algorithm borrowed from IA64.
148 * Main advantage is that it doesn't reset the TSCs fully and
149 * in general looks more robust and it works better than my earlier
150 * attempts. I believe it was written by David Mosberger. Some minor
151 * adjustments for x86-64 by me -AK
152 *
153 * Original comment reproduced below.
154 *
155 * Synchronize TSC of the current (slave) CPU with the TSC of the
156 * MASTER CPU (normally the time-keeper CPU). We use a closed loop to
157 * eliminate the possibility of unaccounted-for errors (such as
158 * getting a machine check in the middle of a calibration step). The
159 * basic idea is for the slave to ask the master what itc value it has
160 * and to read its own itc before and after the master responds. Each
161 * iteration gives us three timestamps:
162 *
163 * slave master
164 *
165 * t0 ---\
166 * ---\
167 * --->
168 * tm
169 * /---
170 * /---
171 * t1 <---
172 *
173 *
174 * The goal is to adjust the slave's TSC such that tm falls exactly
175 * half-way between t0 and t1. If we achieve this, the clocks are
176 * synchronized provided the interconnect between the slave and the
177 * master is symmetric. Even if the interconnect were asymmetric, we
178 * would still know that the synchronization error is smaller than the
179 * roundtrip latency (t0 - t1).
180 *
181 * When the interconnect is quiet and symmetric, this lets us
182 * synchronize the TSC to within one or two cycles. However, we can
183 * only *guarantee* that the synchronization is accurate to within a
184 * round-trip time, which is typically in the range of several hundred
185 * cycles (e.g., ~500 cycles). In practice, this means that the TSCs
186 * are usually almost perfectly synchronized, but we shouldn't assume
187 * that the accuracy is much better than half a micro second or so.
188 *
189 * [there are other errors like the latency of RDTSC and of the
190 * WRMSR. These can also account to hundreds of cycles. So it's
191 * probably worse. It claims 153 cycles error on a dual Opteron,
192 * but I suspect the numbers are actually somewhat worse -AK]
193 */
194
195 #define MASTER 0
196 #define SLAVE (SMP_CACHE_BYTES/8)
197
198 /* Intentionally don't use cpu_relax() while TSC synchronization
199 because we don't want to go into funky power save modi or cause
200 hypervisors to schedule us away. Going to sleep would likely affect
201 latency and low latency is the primary objective here. -AK */
202 #define no_cpu_relax() barrier()
203
204 static __cpuinitdata DEFINE_SPINLOCK(tsc_sync_lock);
205 static volatile __cpuinitdata unsigned long go[SLAVE + 1];
206 static int notscsync __cpuinitdata;
207
208 #undef DEBUG_TSC_SYNC
209
210 #define NUM_ROUNDS 64 /* magic value */
211 #define NUM_ITERS 5 /* likewise */
212
213 /* Callback on boot CPU */
214 static __cpuinit void sync_master(void *arg)
215 {
216 unsigned long flags, i;
217
218 go[MASTER] = 0;
219
220 local_irq_save(flags);
221 {
222 for (i = 0; i < NUM_ROUNDS*NUM_ITERS; ++i) {
223 while (!go[MASTER])
224 no_cpu_relax();
225 go[MASTER] = 0;
226 rdtscll(go[SLAVE]);
227 }
228 }
229 local_irq_restore(flags);
230 }
231
232 /*
233 * Return the number of cycles by which our tsc differs from the tsc
234 * on the master (time-keeper) CPU. A positive number indicates our
235 * tsc is ahead of the master, negative that it is behind.
236 */
237 static inline long
238 get_delta(long *rt, long *master)
239 {
240 unsigned long best_t0 = 0, best_t1 = ~0UL, best_tm = 0;
241 unsigned long tcenter, t0, t1, tm;
242 int i;
243
244 for (i = 0; i < NUM_ITERS; ++i) {
245 rdtscll(t0);
246 go[MASTER] = 1;
247 while (!(tm = go[SLAVE]))
248 no_cpu_relax();
249 go[SLAVE] = 0;
250 rdtscll(t1);
251
252 if (t1 - t0 < best_t1 - best_t0)
253 best_t0 = t0, best_t1 = t1, best_tm = tm;
254 }
255
256 *rt = best_t1 - best_t0;
257 *master = best_tm - best_t0;
258
259 /* average best_t0 and best_t1 without overflow: */
260 tcenter = (best_t0/2 + best_t1/2);
261 if (best_t0 % 2 + best_t1 % 2 == 2)
262 ++tcenter;
263 return tcenter - best_tm;
264 }
265
266 static __cpuinit void sync_tsc(unsigned int master)
267 {
268 int i, done = 0;
269 long delta, adj, adjust_latency = 0;
270 unsigned long flags, rt, master_time_stamp, bound;
271 #ifdef DEBUG_TSC_SYNC
272 static struct syncdebug {
273 long rt; /* roundtrip time */
274 long master; /* master's timestamp */
275 long diff; /* difference between midpoint and master's timestamp */
276 long lat; /* estimate of tsc adjustment latency */
277 } t[NUM_ROUNDS] __cpuinitdata;
278 #endif
279
280 printk(KERN_INFO "CPU %d: Syncing TSC to CPU %u.\n",
281 smp_processor_id(), master);
282
283 go[MASTER] = 1;
284
285 /* It is dangerous to broadcast IPI as cpus are coming up,
286 * as they may not be ready to accept them. So since
287 * we only need to send the ipi to the boot cpu direct
288 * the message, and avoid the race.
289 */
290 smp_call_function_single(master, sync_master, NULL, 1, 0);
291
292 while (go[MASTER]) /* wait for master to be ready */
293 no_cpu_relax();
294
295 spin_lock_irqsave(&tsc_sync_lock, flags);
296 {
297 for (i = 0; i < NUM_ROUNDS; ++i) {
298 delta = get_delta(&rt, &master_time_stamp);
299 if (delta == 0) {
300 done = 1; /* let's lock on to this... */
301 bound = rt;
302 }
303
304 if (!done) {
305 unsigned long t;
306 if (i > 0) {
307 adjust_latency += -delta;
308 adj = -delta + adjust_latency/4;
309 } else
310 adj = -delta;
311
312 rdtscll(t);
313 wrmsrl(MSR_IA32_TSC, t + adj);
314 }
315 #ifdef DEBUG_TSC_SYNC
316 t[i].rt = rt;
317 t[i].master = master_time_stamp;
318 t[i].diff = delta;
319 t[i].lat = adjust_latency/4;
320 #endif
321 }
322 }
323 spin_unlock_irqrestore(&tsc_sync_lock, flags);
324
325 #ifdef DEBUG_TSC_SYNC
326 for (i = 0; i < NUM_ROUNDS; ++i)
327 printk("rt=%5ld master=%5ld diff=%5ld adjlat=%5ld\n",
328 t[i].rt, t[i].master, t[i].diff, t[i].lat);
329 #endif
330
331 printk(KERN_INFO
332 "CPU %d: synchronized TSC with CPU %u (last diff %ld cycles, "
333 "maxerr %lu cycles)\n",
334 smp_processor_id(), master, delta, rt);
335 }
336
337 static void __cpuinit tsc_sync_wait(void)
338 {
339 /*
340 * When the CPU has synchronized TSCs assume the BIOS
341 * or the hardware already synced. Otherwise we could
342 * mess up a possible perfect synchronization with a
343 * not-quite-perfect algorithm.
344 */
345 if (notscsync || !cpu_has_tsc || !unsynchronized_tsc())
346 return;
347 sync_tsc(0);
348 }
349
350 static __init int notscsync_setup(char *s)
351 {
352 notscsync = 1;
353 return 0;
354 }
355 __setup("notscsync", notscsync_setup);
356
357 static atomic_t init_deasserted __cpuinitdata;
358
359 /*
360 * Report back to the Boot Processor.
361 * Running on AP.
362 */
363 void __cpuinit smp_callin(void)
364 {
365 int cpuid, phys_id;
366 unsigned long timeout;
367
368 /*
369 * If waken up by an INIT in an 82489DX configuration
370 * we may get here before an INIT-deassert IPI reaches
371 * our local APIC. We have to wait for the IPI or we'll
372 * lock up on an APIC access.
373 */
374 while (!atomic_read(&init_deasserted))
375 cpu_relax();
376
377 /*
378 * (This works even if the APIC is not enabled.)
379 */
380 phys_id = GET_APIC_ID(apic_read(APIC_ID));
381 cpuid = smp_processor_id();
382 if (cpu_isset(cpuid, cpu_callin_map)) {
383 panic("smp_callin: phys CPU#%d, CPU#%d already present??\n",
384 phys_id, cpuid);
385 }
386 Dprintk("CPU#%d (phys ID: %d) waiting for CALLOUT\n", cpuid, phys_id);
387
388 /*
389 * STARTUP IPIs are fragile beasts as they might sometimes
390 * trigger some glue motherboard logic. Complete APIC bus
391 * silence for 1 second, this overestimates the time the
392 * boot CPU is spending to send the up to 2 STARTUP IPIs
393 * by a factor of two. This should be enough.
394 */
395
396 /*
397 * Waiting 2s total for startup (udelay is not yet working)
398 */
399 timeout = jiffies + 2*HZ;
400 while (time_before(jiffies, timeout)) {
401 /*
402 * Has the boot CPU finished it's STARTUP sequence?
403 */
404 if (cpu_isset(cpuid, cpu_callout_map))
405 break;
406 cpu_relax();
407 }
408
409 if (!time_before(jiffies, timeout)) {
410 panic("smp_callin: CPU%d started up but did not get a callout!\n",
411 cpuid);
412 }
413
414 /*
415 * the boot CPU has finished the init stage and is spinning
416 * on callin_map until we finish. We are free to set up this
417 * CPU, first the APIC. (this is probably redundant on most
418 * boards)
419 */
420
421 Dprintk("CALLIN, before setup_local_APIC().\n");
422 setup_local_APIC();
423
424 /*
425 * Get our bogomips.
426 *
427 * Need to enable IRQs because it can take longer and then
428 * the NMI watchdog might kill us.
429 */
430 local_irq_enable();
431 calibrate_delay();
432 local_irq_disable();
433 Dprintk("Stack at about %p\n",&cpuid);
434
435 disable_APIC_timer();
436
437 /*
438 * Save our processor parameters
439 */
440 smp_store_cpu_info(cpuid);
441
442 /*
443 * Allow the master to continue.
444 */
445 cpu_set(cpuid, cpu_callin_map);
446 }
447
448 /* representing cpus for which sibling maps can be computed */
449 static cpumask_t cpu_sibling_setup_map;
450
451 static inline void set_cpu_sibling_map(int cpu)
452 {
453 int i;
454 struct cpuinfo_x86 *c = cpu_data;
455
456 cpu_set(cpu, cpu_sibling_setup_map);
457
458 if (smp_num_siblings > 1) {
459 for_each_cpu_mask(i, cpu_sibling_setup_map) {
460 if (phys_proc_id[cpu] == phys_proc_id[i] &&
461 cpu_core_id[cpu] == cpu_core_id[i]) {
462 cpu_set(i, cpu_sibling_map[cpu]);
463 cpu_set(cpu, cpu_sibling_map[i]);
464 cpu_set(i, cpu_core_map[cpu]);
465 cpu_set(cpu, cpu_core_map[i]);
466 }
467 }
468 } else {
469 cpu_set(cpu, cpu_sibling_map[cpu]);
470 }
471
472 if (current_cpu_data.x86_max_cores == 1) {
473 cpu_core_map[cpu] = cpu_sibling_map[cpu];
474 c[cpu].booted_cores = 1;
475 return;
476 }
477
478 for_each_cpu_mask(i, cpu_sibling_setup_map) {
479 if (phys_proc_id[cpu] == phys_proc_id[i]) {
480 cpu_set(i, cpu_core_map[cpu]);
481 cpu_set(cpu, cpu_core_map[i]);
482 /*
483 * Does this new cpu bringup a new core?
484 */
485 if (cpus_weight(cpu_sibling_map[cpu]) == 1) {
486 /*
487 * for each core in package, increment
488 * the booted_cores for this new cpu
489 */
490 if (first_cpu(cpu_sibling_map[i]) == i)
491 c[cpu].booted_cores++;
492 /*
493 * increment the core count for all
494 * the other cpus in this package
495 */
496 if (i != cpu)
497 c[i].booted_cores++;
498 } else if (i != cpu && !c[cpu].booted_cores)
499 c[cpu].booted_cores = c[i].booted_cores;
500 }
501 }
502 }
503
504 /*
505 * Setup code on secondary processor (after comming out of the trampoline)
506 */
507 void __cpuinit start_secondary(void)
508 {
509 /*
510 * Dont put anything before smp_callin(), SMP
511 * booting is too fragile that we want to limit the
512 * things done here to the most necessary things.
513 */
514 cpu_init();
515 preempt_disable();
516 smp_callin();
517
518 /* otherwise gcc will move up the smp_processor_id before the cpu_init */
519 barrier();
520
521 Dprintk("cpu %d: setting up apic clock\n", smp_processor_id());
522 setup_secondary_APIC_clock();
523
524 Dprintk("cpu %d: enabling apic timer\n", smp_processor_id());
525
526 if (nmi_watchdog == NMI_IO_APIC) {
527 disable_8259A_irq(0);
528 enable_NMI_through_LVT0(NULL);
529 enable_8259A_irq(0);
530 }
531
532 enable_APIC_timer();
533
534 /*
535 * The sibling maps must be set before turing the online map on for
536 * this cpu
537 */
538 set_cpu_sibling_map(smp_processor_id());
539
540 /*
541 * Wait for TSC sync to not schedule things before.
542 * We still process interrupts, which could see an inconsistent
543 * time in that window unfortunately.
544 * Do this here because TSC sync has global unprotected state.
545 */
546 tsc_sync_wait();
547
548 /*
549 * We need to hold call_lock, so there is no inconsistency
550 * between the time smp_call_function() determines number of
551 * IPI receipients, and the time when the determination is made
552 * for which cpus receive the IPI in genapic_flat.c. Holding this
553 * lock helps us to not include this cpu in a currently in progress
554 * smp_call_function().
555 */
556 lock_ipi_call_lock();
557
558 /*
559 * Allow the master to continue.
560 */
561 cpu_set(smp_processor_id(), cpu_online_map);
562 per_cpu(cpu_state, smp_processor_id()) = CPU_ONLINE;
563 unlock_ipi_call_lock();
564
565 cpu_idle();
566 }
567
568 extern volatile unsigned long init_rsp;
569 extern void (*initial_code)(void);
570
571 #ifdef APIC_DEBUG
572 static void inquire_remote_apic(int apicid)
573 {
574 unsigned i, regs[] = { APIC_ID >> 4, APIC_LVR >> 4, APIC_SPIV >> 4 };
575 char *names[] = { "ID", "VERSION", "SPIV" };
576 int timeout, status;
577
578 printk(KERN_INFO "Inquiring remote APIC #%d...\n", apicid);
579
580 for (i = 0; i < sizeof(regs) / sizeof(*regs); i++) {
581 printk("... APIC #%d %s: ", apicid, names[i]);
582
583 /*
584 * Wait for idle.
585 */
586 apic_wait_icr_idle();
587
588 apic_write(APIC_ICR2, SET_APIC_DEST_FIELD(apicid));
589 apic_write(APIC_ICR, APIC_DM_REMRD | regs[i]);
590
591 timeout = 0;
592 do {
593 udelay(100);
594 status = apic_read(APIC_ICR) & APIC_ICR_RR_MASK;
595 } while (status == APIC_ICR_RR_INPROG && timeout++ < 1000);
596
597 switch (status) {
598 case APIC_ICR_RR_VALID:
599 status = apic_read(APIC_RRR);
600 printk("%08x\n", status);
601 break;
602 default:
603 printk("failed\n");
604 }
605 }
606 }
607 #endif
608
609 /*
610 * Kick the secondary to wake up.
611 */
612 static int __cpuinit wakeup_secondary_via_INIT(int phys_apicid, unsigned int start_rip)
613 {
614 unsigned long send_status = 0, accept_status = 0;
615 int maxlvt, timeout, num_starts, j;
616
617 Dprintk("Asserting INIT.\n");
618
619 /*
620 * Turn INIT on target chip
621 */
622 apic_write(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
623
624 /*
625 * Send IPI
626 */
627 apic_write(APIC_ICR, APIC_INT_LEVELTRIG | APIC_INT_ASSERT
628 | APIC_DM_INIT);
629
630 Dprintk("Waiting for send to finish...\n");
631 timeout = 0;
632 do {
633 Dprintk("+");
634 udelay(100);
635 send_status = apic_read(APIC_ICR) & APIC_ICR_BUSY;
636 } while (send_status && (timeout++ < 1000));
637
638 mdelay(10);
639
640 Dprintk("Deasserting INIT.\n");
641
642 /* Target chip */
643 apic_write(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
644
645 /* Send IPI */
646 apic_write(APIC_ICR, APIC_INT_LEVELTRIG | APIC_DM_INIT);
647
648 Dprintk("Waiting for send to finish...\n");
649 timeout = 0;
650 do {
651 Dprintk("+");
652 udelay(100);
653 send_status = apic_read(APIC_ICR) & APIC_ICR_BUSY;
654 } while (send_status && (timeout++ < 1000));
655
656 mb();
657 atomic_set(&init_deasserted, 1);
658
659 num_starts = 2;
660
661 /*
662 * Run STARTUP IPI loop.
663 */
664 Dprintk("#startup loops: %d.\n", num_starts);
665
666 maxlvt = get_maxlvt();
667
668 for (j = 1; j <= num_starts; j++) {
669 Dprintk("Sending STARTUP #%d.\n",j);
670 apic_write(APIC_ESR, 0);
671 apic_read(APIC_ESR);
672 Dprintk("After apic_write.\n");
673
674 /*
675 * STARTUP IPI
676 */
677
678 /* Target chip */
679 apic_write(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
680
681 /* Boot on the stack */
682 /* Kick the second */
683 apic_write(APIC_ICR, APIC_DM_STARTUP | (start_rip >> 12));
684
685 /*
686 * Give the other CPU some time to accept the IPI.
687 */
688 udelay(300);
689
690 Dprintk("Startup point 1.\n");
691
692 Dprintk("Waiting for send to finish...\n");
693 timeout = 0;
694 do {
695 Dprintk("+");
696 udelay(100);
697 send_status = apic_read(APIC_ICR) & APIC_ICR_BUSY;
698 } while (send_status && (timeout++ < 1000));
699
700 /*
701 * Give the other CPU some time to accept the IPI.
702 */
703 udelay(200);
704 /*
705 * Due to the Pentium erratum 3AP.
706 */
707 if (maxlvt > 3) {
708 apic_write(APIC_ESR, 0);
709 }
710 accept_status = (apic_read(APIC_ESR) & 0xEF);
711 if (send_status || accept_status)
712 break;
713 }
714 Dprintk("After Startup.\n");
715
716 if (send_status)
717 printk(KERN_ERR "APIC never delivered???\n");
718 if (accept_status)
719 printk(KERN_ERR "APIC delivery error (%lx).\n", accept_status);
720
721 return (send_status | accept_status);
722 }
723
724 struct create_idle {
725 struct task_struct *idle;
726 struct completion done;
727 int cpu;
728 };
729
730 void do_fork_idle(void *_c_idle)
731 {
732 struct create_idle *c_idle = _c_idle;
733
734 c_idle->idle = fork_idle(c_idle->cpu);
735 complete(&c_idle->done);
736 }
737
738 /*
739 * Boot one CPU.
740 */
741 static int __cpuinit do_boot_cpu(int cpu, int apicid)
742 {
743 unsigned long boot_error;
744 int timeout;
745 unsigned long start_rip;
746 struct create_idle c_idle = {
747 .cpu = cpu,
748 .done = COMPLETION_INITIALIZER(c_idle.done),
749 };
750 DECLARE_WORK(work, do_fork_idle, &c_idle);
751
752 /* allocate memory for gdts of secondary cpus. Hotplug is considered */
753 if (!cpu_gdt_descr[cpu].address &&
754 !(cpu_gdt_descr[cpu].address = get_zeroed_page(GFP_KERNEL))) {
755 printk(KERN_ERR "Failed to allocate GDT for CPU %d\n", cpu);
756 return -1;
757 }
758
759 /* Allocate node local memory for AP pdas */
760 if (cpu_pda(cpu) == &boot_cpu_pda[cpu]) {
761 struct x8664_pda *newpda, *pda;
762 int node = cpu_to_node(cpu);
763 pda = cpu_pda(cpu);
764 newpda = kmalloc_node(sizeof (struct x8664_pda), GFP_ATOMIC,
765 node);
766 if (newpda) {
767 memcpy(newpda, pda, sizeof (struct x8664_pda));
768 cpu_pda(cpu) = newpda;
769 } else
770 printk(KERN_ERR
771 "Could not allocate node local PDA for CPU %d on node %d\n",
772 cpu, node);
773 }
774
775
776 c_idle.idle = get_idle_for_cpu(cpu);
777
778 if (c_idle.idle) {
779 c_idle.idle->thread.rsp = (unsigned long) (((struct pt_regs *)
780 (THREAD_SIZE + task_stack_page(c_idle.idle))) - 1);
781 init_idle(c_idle.idle, cpu);
782 goto do_rest;
783 }
784
785 /*
786 * During cold boot process, keventd thread is not spun up yet.
787 * When we do cpu hot-add, we create idle threads on the fly, we should
788 * not acquire any attributes from the calling context. Hence the clean
789 * way to create kernel_threads() is to do that from keventd().
790 * We do the current_is_keventd() due to the fact that ACPI notifier
791 * was also queuing to keventd() and when the caller is already running
792 * in context of keventd(), we would end up with locking up the keventd
793 * thread.
794 */
795 if (!keventd_up() || current_is_keventd())
796 work.func(work.data);
797 else {
798 schedule_work(&work);
799 wait_for_completion(&c_idle.done);
800 }
801
802 if (IS_ERR(c_idle.idle)) {
803 printk("failed fork for CPU %d\n", cpu);
804 return PTR_ERR(c_idle.idle);
805 }
806
807 set_idle_for_cpu(cpu, c_idle.idle);
808
809 do_rest:
810
811 cpu_pda(cpu)->pcurrent = c_idle.idle;
812
813 start_rip = setup_trampoline();
814
815 init_rsp = c_idle.idle->thread.rsp;
816 per_cpu(init_tss,cpu).rsp0 = init_rsp;
817 initial_code = start_secondary;
818 clear_tsk_thread_flag(c_idle.idle, TIF_FORK);
819
820 printk(KERN_INFO "Booting processor %d/%d APIC 0x%x\n", cpu,
821 cpus_weight(cpu_present_map),
822 apicid);
823
824 /*
825 * This grunge runs the startup process for
826 * the targeted processor.
827 */
828
829 atomic_set(&init_deasserted, 0);
830
831 Dprintk("Setting warm reset code and vector.\n");
832
833 CMOS_WRITE(0xa, 0xf);
834 local_flush_tlb();
835 Dprintk("1.\n");
836 *((volatile unsigned short *) phys_to_virt(0x469)) = start_rip >> 4;
837 Dprintk("2.\n");
838 *((volatile unsigned short *) phys_to_virt(0x467)) = start_rip & 0xf;
839 Dprintk("3.\n");
840
841 /*
842 * Be paranoid about clearing APIC errors.
843 */
844 apic_write(APIC_ESR, 0);
845 apic_read(APIC_ESR);
846
847 /*
848 * Status is now clean
849 */
850 boot_error = 0;
851
852 /*
853 * Starting actual IPI sequence...
854 */
855 boot_error = wakeup_secondary_via_INIT(apicid, start_rip);
856
857 if (!boot_error) {
858 /*
859 * allow APs to start initializing.
860 */
861 Dprintk("Before Callout %d.\n", cpu);
862 cpu_set(cpu, cpu_callout_map);
863 Dprintk("After Callout %d.\n", cpu);
864
865 /*
866 * Wait 5s total for a response
867 */
868 for (timeout = 0; timeout < 50000; timeout++) {
869 if (cpu_isset(cpu, cpu_callin_map))
870 break; /* It has booted */
871 udelay(100);
872 }
873
874 if (cpu_isset(cpu, cpu_callin_map)) {
875 /* number CPUs logically, starting from 1 (BSP is 0) */
876 Dprintk("CPU has booted.\n");
877 } else {
878 boot_error = 1;
879 if (*((volatile unsigned char *)phys_to_virt(SMP_TRAMPOLINE_BASE))
880 == 0xA5)
881 /* trampoline started but...? */
882 printk("Stuck ??\n");
883 else
884 /* trampoline code not run */
885 printk("Not responding.\n");
886 #ifdef APIC_DEBUG
887 inquire_remote_apic(apicid);
888 #endif
889 }
890 }
891 if (boot_error) {
892 cpu_clear(cpu, cpu_callout_map); /* was set here (do_boot_cpu()) */
893 clear_bit(cpu, &cpu_initialized); /* was set by cpu_init() */
894 clear_node_cpumask(cpu); /* was set by numa_add_cpu */
895 cpu_clear(cpu, cpu_present_map);
896 cpu_clear(cpu, cpu_possible_map);
897 x86_cpu_to_apicid[cpu] = BAD_APICID;
898 x86_cpu_to_log_apicid[cpu] = BAD_APICID;
899 return -EIO;
900 }
901
902 return 0;
903 }
904
905 cycles_t cacheflush_time;
906 unsigned long cache_decay_ticks;
907
908 /*
909 * Cleanup possible dangling ends...
910 */
911 static __cpuinit void smp_cleanup_boot(void)
912 {
913 /*
914 * Paranoid: Set warm reset code and vector here back
915 * to default values.
916 */
917 CMOS_WRITE(0, 0xf);
918
919 /*
920 * Reset trampoline flag
921 */
922 *((volatile int *) phys_to_virt(0x467)) = 0;
923 }
924
925 /*
926 * Fall back to non SMP mode after errors.
927 *
928 * RED-PEN audit/test this more. I bet there is more state messed up here.
929 */
930 static __init void disable_smp(void)
931 {
932 cpu_present_map = cpumask_of_cpu(0);
933 cpu_possible_map = cpumask_of_cpu(0);
934 if (smp_found_config)
935 phys_cpu_present_map = physid_mask_of_physid(boot_cpu_id);
936 else
937 phys_cpu_present_map = physid_mask_of_physid(0);
938 cpu_set(0, cpu_sibling_map[0]);
939 cpu_set(0, cpu_core_map[0]);
940 }
941
942 #ifdef CONFIG_HOTPLUG_CPU
943
944 int additional_cpus __initdata = -1;
945
946 /*
947 * cpu_possible_map should be static, it cannot change as cpu's
948 * are onlined, or offlined. The reason is per-cpu data-structures
949 * are allocated by some modules at init time, and dont expect to
950 * do this dynamically on cpu arrival/departure.
951 * cpu_present_map on the other hand can change dynamically.
952 * In case when cpu_hotplug is not compiled, then we resort to current
953 * behaviour, which is cpu_possible == cpu_present.
954 * - Ashok Raj
955 *
956 * Three ways to find out the number of additional hotplug CPUs:
957 * - If the BIOS specified disabled CPUs in ACPI/mptables use that.
958 * - The user can overwrite it with additional_cpus=NUM
959 * - Otherwise don't reserve additional CPUs.
960 * We do this because additional CPUs waste a lot of memory.
961 * -AK
962 */
963 __init void prefill_possible_map(void)
964 {
965 int i;
966 int possible;
967
968 if (additional_cpus == -1) {
969 if (disabled_cpus > 0)
970 additional_cpus = disabled_cpus;
971 else
972 additional_cpus = 0;
973 }
974 possible = num_processors + additional_cpus;
975 if (possible > NR_CPUS)
976 possible = NR_CPUS;
977
978 printk(KERN_INFO "SMP: Allowing %d CPUs, %d hotplug CPUs\n",
979 possible,
980 max_t(int, possible - num_processors, 0));
981
982 for (i = 0; i < possible; i++)
983 cpu_set(i, cpu_possible_map);
984 }
985 #endif
986
987 /*
988 * Various sanity checks.
989 */
990 static int __init smp_sanity_check(unsigned max_cpus)
991 {
992 if (!physid_isset(hard_smp_processor_id(), phys_cpu_present_map)) {
993 printk("weird, boot CPU (#%d) not listed by the BIOS.\n",
994 hard_smp_processor_id());
995 physid_set(hard_smp_processor_id(), phys_cpu_present_map);
996 }
997
998 /*
999 * If we couldn't find an SMP configuration at boot time,
1000 * get out of here now!
1001 */
1002 if (!smp_found_config) {
1003 printk(KERN_NOTICE "SMP motherboard not detected.\n");
1004 disable_smp();
1005 if (APIC_init_uniprocessor())
1006 printk(KERN_NOTICE "Local APIC not detected."
1007 " Using dummy APIC emulation.\n");
1008 return -1;
1009 }
1010
1011 /*
1012 * Should not be necessary because the MP table should list the boot
1013 * CPU too, but we do it for the sake of robustness anyway.
1014 */
1015 if (!physid_isset(boot_cpu_id, phys_cpu_present_map)) {
1016 printk(KERN_NOTICE "weird, boot CPU (#%d) not listed by the BIOS.\n",
1017 boot_cpu_id);
1018 physid_set(hard_smp_processor_id(), phys_cpu_present_map);
1019 }
1020
1021 /*
1022 * If we couldn't find a local APIC, then get out of here now!
1023 */
1024 if (!cpu_has_apic) {
1025 printk(KERN_ERR "BIOS bug, local APIC #%d not detected!...\n",
1026 boot_cpu_id);
1027 printk(KERN_ERR "... forcing use of dummy APIC emulation. (tell your hw vendor)\n");
1028 nr_ioapics = 0;
1029 return -1;
1030 }
1031
1032 /*
1033 * If SMP should be disabled, then really disable it!
1034 */
1035 if (!max_cpus) {
1036 printk(KERN_INFO "SMP mode deactivated, forcing use of dummy APIC emulation.\n");
1037 nr_ioapics = 0;
1038 return -1;
1039 }
1040
1041 return 0;
1042 }
1043
1044 /*
1045 * Prepare for SMP bootup. The MP table or ACPI has been read
1046 * earlier. Just do some sanity checking here and enable APIC mode.
1047 */
1048 void __init smp_prepare_cpus(unsigned int max_cpus)
1049 {
1050 nmi_watchdog_default();
1051 current_cpu_data = boot_cpu_data;
1052 current_thread_info()->cpu = 0; /* needed? */
1053 set_cpu_sibling_map(0);
1054
1055 if (smp_sanity_check(max_cpus) < 0) {
1056 printk(KERN_INFO "SMP disabled\n");
1057 disable_smp();
1058 return;
1059 }
1060
1061
1062 /*
1063 * Switch from PIC to APIC mode.
1064 */
1065 connect_bsp_APIC();
1066 setup_local_APIC();
1067
1068 if (GET_APIC_ID(apic_read(APIC_ID)) != boot_cpu_id) {
1069 panic("Boot APIC ID in local APIC unexpected (%d vs %d)",
1070 GET_APIC_ID(apic_read(APIC_ID)), boot_cpu_id);
1071 /* Or can we switch back to PIC here? */
1072 }
1073
1074 /*
1075 * Now start the IO-APICs
1076 */
1077 if (!skip_ioapic_setup && nr_ioapics)
1078 setup_IO_APIC();
1079 else
1080 nr_ioapics = 0;
1081
1082 /*
1083 * Set up local APIC timer on boot CPU.
1084 */
1085
1086 setup_boot_APIC_clock();
1087 }
1088
1089 /*
1090 * Early setup to make printk work.
1091 */
1092 void __init smp_prepare_boot_cpu(void)
1093 {
1094 int me = smp_processor_id();
1095 cpu_set(me, cpu_online_map);
1096 cpu_set(me, cpu_callout_map);
1097 per_cpu(cpu_state, me) = CPU_ONLINE;
1098 }
1099
1100 /*
1101 * Entry point to boot a CPU.
1102 */
1103 int __cpuinit __cpu_up(unsigned int cpu)
1104 {
1105 int err;
1106 int apicid = cpu_present_to_apicid(cpu);
1107
1108 WARN_ON(irqs_disabled());
1109
1110 Dprintk("++++++++++++++++++++=_---CPU UP %u\n", cpu);
1111
1112 if (apicid == BAD_APICID || apicid == boot_cpu_id ||
1113 !physid_isset(apicid, phys_cpu_present_map)) {
1114 printk("__cpu_up: bad cpu %d\n", cpu);
1115 return -EINVAL;
1116 }
1117
1118 /*
1119 * Already booted CPU?
1120 */
1121 if (cpu_isset(cpu, cpu_callin_map)) {
1122 Dprintk("do_boot_cpu %d Already started\n", cpu);
1123 return -ENOSYS;
1124 }
1125
1126 per_cpu(cpu_state, cpu) = CPU_UP_PREPARE;
1127 /* Boot it! */
1128 err = do_boot_cpu(cpu, apicid);
1129 if (err < 0) {
1130 Dprintk("do_boot_cpu failed %d\n", err);
1131 return err;
1132 }
1133
1134 /* Unleash the CPU! */
1135 Dprintk("waiting for cpu %d\n", cpu);
1136
1137 while (!cpu_isset(cpu, cpu_online_map))
1138 cpu_relax();
1139 err = 0;
1140
1141 return err;
1142 }
1143
1144 /*
1145 * Finish the SMP boot.
1146 */
1147 void __init smp_cpus_done(unsigned int max_cpus)
1148 {
1149 smp_cleanup_boot();
1150
1151 #ifdef CONFIG_X86_IO_APIC
1152 setup_ioapic_dest();
1153 #endif
1154
1155 check_nmi_watchdog();
1156 }
1157
1158 #ifdef CONFIG_HOTPLUG_CPU
1159
1160 static void remove_siblinginfo(int cpu)
1161 {
1162 int sibling;
1163 struct cpuinfo_x86 *c = cpu_data;
1164
1165 for_each_cpu_mask(sibling, cpu_core_map[cpu]) {
1166 cpu_clear(cpu, cpu_core_map[sibling]);
1167 /*
1168 * last thread sibling in this cpu core going down
1169 */
1170 if (cpus_weight(cpu_sibling_map[cpu]) == 1)
1171 c[sibling].booted_cores--;
1172 }
1173
1174 for_each_cpu_mask(sibling, cpu_sibling_map[cpu])
1175 cpu_clear(cpu, cpu_sibling_map[sibling]);
1176 cpus_clear(cpu_sibling_map[cpu]);
1177 cpus_clear(cpu_core_map[cpu]);
1178 phys_proc_id[cpu] = BAD_APICID;
1179 cpu_core_id[cpu] = BAD_APICID;
1180 cpu_clear(cpu, cpu_sibling_setup_map);
1181 }
1182
1183 void remove_cpu_from_maps(void)
1184 {
1185 int cpu = smp_processor_id();
1186
1187 cpu_clear(cpu, cpu_callout_map);
1188 cpu_clear(cpu, cpu_callin_map);
1189 clear_bit(cpu, &cpu_initialized); /* was set by cpu_init() */
1190 clear_node_cpumask(cpu);
1191 }
1192
1193 int __cpu_disable(void)
1194 {
1195 int cpu = smp_processor_id();
1196
1197 /*
1198 * Perhaps use cpufreq to drop frequency, but that could go
1199 * into generic code.
1200 *
1201 * We won't take down the boot processor on i386 due to some
1202 * interrupts only being able to be serviced by the BSP.
1203 * Especially so if we're not using an IOAPIC -zwane
1204 */
1205 if (cpu == 0)
1206 return -EBUSY;
1207
1208 clear_local_APIC();
1209
1210 /*
1211 * HACK:
1212 * Allow any queued timer interrupts to get serviced
1213 * This is only a temporary solution until we cleanup
1214 * fixup_irqs as we do for IA64.
1215 */
1216 local_irq_enable();
1217 mdelay(1);
1218
1219 local_irq_disable();
1220 remove_siblinginfo(cpu);
1221
1222 /* It's now safe to remove this processor from the online map */
1223 cpu_clear(cpu, cpu_online_map);
1224 remove_cpu_from_maps();
1225 fixup_irqs(cpu_online_map);
1226 return 0;
1227 }
1228
1229 void __cpu_die(unsigned int cpu)
1230 {
1231 /* We don't do anything here: idle task is faking death itself. */
1232 unsigned int i;
1233
1234 for (i = 0; i < 10; i++) {
1235 /* They ack this in play_dead by setting CPU_DEAD */
1236 if (per_cpu(cpu_state, cpu) == CPU_DEAD) {
1237 printk ("CPU %d is now offline\n", cpu);
1238 return;
1239 }
1240 msleep(100);
1241 }
1242 printk(KERN_ERR "CPU %u didn't die...\n", cpu);
1243 }
1244
1245 __init int setup_additional_cpus(char *s)
1246 {
1247 return get_option(&s, &additional_cpus);
1248 }
1249 __setup("additional_cpus=", setup_additional_cpus);
1250
1251 #else /* ... !CONFIG_HOTPLUG_CPU */
1252
1253 int __cpu_disable(void)
1254 {
1255 return -ENOSYS;
1256 }
1257
1258 void __cpu_die(unsigned int cpu)
1259 {
1260 /* We said "no" in __cpu_disable */
1261 BUG();
1262 }
1263 #endif /* CONFIG_HOTPLUG_CPU */