1 /*
2 * x86 SMP booting functions
3 *
4 * (c) 1995 Alan Cox, Building #3 <alan@lxorguk.ukuu.org.uk>
5 * (c) 1998, 1999, 2000, 2009 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 or
16 * later.
17 *
18 * Fixes
19 * Felix Koop : NR_CPUS used properly
20 * Jose Renau : Handle single CPU case.
21 * Alan Cox : By repeated request 8) - Total BogoMIPS report.
22 * Greg Wright : Fix for kernel stacks panic.
23 * Erich Boleyn : MP v1.4 and additional changes.
24 * Matthias Sattler : Changes for 2.1 kernel map.
25 * Michel Lespinasse : Changes for 2.1 kernel map.
26 * Michael Chastain : Change trampoline.S to gnu as.
27 * Alan Cox : Dumb bug: 'B' step PPro's are fine
28 * Ingo Molnar : Added APIC timers, based on code
29 * from Jose Renau
30 * Ingo Molnar : various cleanups and rewrites
31 * Tigran Aivazian : fixed "0.00 in /proc/uptime on SMP" bug.
32 * Maciej W. Rozycki : Bits for genuine 82489DX APICs
33 * Andi Kleen : Changed for SMP boot into long mode.
34 * Martin J. Bligh : Added support for multi-quad systems
35 * Dave Jones : Report invalid combinations of Athlon CPUs.
36 * Rusty Russell : Hacked into shape for new "hotplug" boot process.
37 * Andi Kleen : Converted to new state machine.
38 * Ashok Raj : CPU hotplug support
39 * Glauber Costa : i386 and x86_64 integration
40 */
41
42 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
43
44 #include <linux/init.h>
45 #include <linux/smp.h>
46 #include <linux/export.h>
47 #include <linux/sched.h>
48 #include <linux/sched/topology.h>
49 #include <linux/sched/hotplug.h>
50 #include <linux/sched/task_stack.h>
51 #include <linux/percpu.h>
52 #include <linux/bootmem.h>
53 #include <linux/err.h>
54 #include <linux/nmi.h>
55 #include <linux/tboot.h>
56 #include <linux/stackprotector.h>
57 #include <linux/gfp.h>
58 #include <linux/cpuidle.h>
59
60 #include <asm/acpi.h>
61 #include <asm/desc.h>
62 #include <asm/nmi.h>
63 #include <asm/irq.h>
64 #include <asm/realmode.h>
65 #include <asm/cpu.h>
66 #include <asm/numa.h>
67 #include <asm/pgtable.h>
68 #include <asm/tlbflush.h>
69 #include <asm/mtrr.h>
70 #include <asm/mwait.h>
71 #include <asm/apic.h>
72 #include <asm/io_apic.h>
73 #include <asm/fpu/internal.h>
74 #include <asm/setup.h>
75 #include <asm/uv/uv.h>
76 #include <linux/mc146818rtc.h>
77 #include <asm/i8259.h>
78 #include <asm/realmode.h>
79 #include <asm/misc.h>
80 #include <asm/spec-ctrl.h>
81 #include <asm/hw_irq.h>
82
83 /* representing HT siblings of each logical CPU */
84 DEFINE_PER_CPU_READ_MOSTLY(cpumask_var_t, cpu_sibling_map);
85 EXPORT_PER_CPU_SYMBOL(cpu_sibling_map);
86
87 /* representing HT and core siblings of each logical CPU */
88 DEFINE_PER_CPU_READ_MOSTLY(cpumask_var_t, cpu_core_map);
89 EXPORT_PER_CPU_SYMBOL(cpu_core_map);
90
91 DEFINE_PER_CPU_READ_MOSTLY(cpumask_var_t, cpu_llc_shared_map);
92
93 /* Per CPU bogomips and other parameters */
94 DEFINE_PER_CPU_READ_MOSTLY(struct cpuinfo_x86, cpu_info);
95 EXPORT_PER_CPU_SYMBOL(cpu_info);
96
97 /* Logical package management. We might want to allocate that dynamically */
98 static int *physical_to_logical_pkg __read_mostly;
99 static unsigned long *physical_package_map __read_mostly;;
100 static unsigned int max_physical_pkg_id __read_mostly;
101 unsigned int __max_logical_packages __read_mostly;
102 EXPORT_SYMBOL(__max_logical_packages);
103 static unsigned int logical_packages __read_mostly;
104
105 /* Maximum number of SMT threads on any online core */
106 int __max_smt_threads __read_mostly;
107
108 /* Flag to indicate if a complete sched domain rebuild is required */
109 bool x86_topology_update;
110
arch_update_cpu_topology(void)111 int arch_update_cpu_topology(void)
112 {
113 int retval = x86_topology_update;
114
115 x86_topology_update = false;
116 return retval;
117 }
118
smpboot_setup_warm_reset_vector(unsigned long start_eip)119 static inline void smpboot_setup_warm_reset_vector(unsigned long start_eip)
120 {
121 unsigned long flags;
122
123 spin_lock_irqsave(&rtc_lock, flags);
124 CMOS_WRITE(0xa, 0xf);
125 spin_unlock_irqrestore(&rtc_lock, flags);
126 *((volatile unsigned short *)phys_to_virt(TRAMPOLINE_PHYS_HIGH)) =
127 start_eip >> 4;
128 *((volatile unsigned short *)phys_to_virt(TRAMPOLINE_PHYS_LOW)) =
129 start_eip & 0xf;
130 }
131
smpboot_restore_warm_reset_vector(void)132 static inline void smpboot_restore_warm_reset_vector(void)
133 {
134 unsigned long flags;
135
136 /*
137 * Paranoid: Set warm reset code and vector here back
138 * to default values.
139 */
140 spin_lock_irqsave(&rtc_lock, flags);
141 CMOS_WRITE(0, 0xf);
142 spin_unlock_irqrestore(&rtc_lock, flags);
143
144 *((volatile u32 *)phys_to_virt(TRAMPOLINE_PHYS_LOW)) = 0;
145 }
146
147 /*
148 * Report back to the Boot Processor during boot time or to the caller processor
149 * during CPU online.
150 */
smp_callin(void)151 static void smp_callin(void)
152 {
153 int cpuid, phys_id;
154
155 /*
156 * If waken up by an INIT in an 82489DX configuration
157 * cpu_callout_mask guarantees we don't get here before
158 * an INIT_deassert IPI reaches our local APIC, so it is
159 * now safe to touch our local APIC.
160 */
161 cpuid = smp_processor_id();
162
163 /*
164 * (This works even if the APIC is not enabled.)
165 */
166 phys_id = read_apic_id();
167
168 /*
169 * the boot CPU has finished the init stage and is spinning
170 * on callin_map until we finish. We are free to set up this
171 * CPU, first the APIC. (this is probably redundant on most
172 * boards)
173 */
174 apic_ap_setup();
175
176 /*
177 * Save our processor parameters. Note: this information
178 * is needed for clock calibration.
179 */
180 smp_store_cpu_info(cpuid);
181
182 /*
183 * The topology information must be up to date before
184 * calibrate_delay() and notify_cpu_starting().
185 */
186 set_cpu_sibling_map(raw_smp_processor_id());
187
188 /*
189 * Get our bogomips.
190 * Update loops_per_jiffy in cpu_data. Previous call to
191 * smp_store_cpu_info() stored a value that is close but not as
192 * accurate as the value just calculated.
193 */
194 calibrate_delay();
195 cpu_data(cpuid).loops_per_jiffy = loops_per_jiffy;
196 pr_debug("Stack at about %p\n", &cpuid);
197
198 wmb();
199
200 notify_cpu_starting(cpuid);
201
202 /*
203 * Allow the master to continue.
204 */
205 cpumask_set_cpu(cpuid, cpu_callin_mask);
206 }
207
208 static int cpu0_logical_apicid;
209 static int enable_start_cpu0;
210 /*
211 * Activate a secondary processor.
212 */
start_secondary(void * unused)213 static void notrace start_secondary(void *unused)
214 {
215 /*
216 * Don't put *anything* except direct CPU state initialization
217 * before cpu_init(), SMP booting is too fragile that we want to
218 * limit the things done here to the most necessary things.
219 */
220 if (boot_cpu_has(X86_FEATURE_PCID))
221 __write_cr4(__read_cr4() | X86_CR4_PCIDE);
222
223 #ifdef CONFIG_X86_32
224 /* switch away from the initial page table */
225 load_cr3(swapper_pg_dir);
226 /*
227 * Initialize the CR4 shadow before doing anything that could
228 * try to read it.
229 */
230 cr4_init_shadow();
231 __flush_tlb_all();
232 #endif
233 load_current_idt();
234 cpu_init();
235 x86_cpuinit.early_percpu_clock_init();
236 preempt_disable();
237 smp_callin();
238
239 enable_start_cpu0 = 0;
240
241 /* otherwise gcc will move up smp_processor_id before the cpu_init */
242 barrier();
243 /*
244 * Check TSC synchronization with the BP:
245 */
246 check_tsc_sync_target();
247
248 speculative_store_bypass_ht_init();
249
250 /*
251 * Lock vector_lock and initialize the vectors on this cpu
252 * before setting the cpu online. We must set it online with
253 * vector_lock held to prevent a concurrent setup/teardown
254 * from seeing a half valid vector space.
255 */
256 lock_vector_lock();
257 setup_vector_irq(smp_processor_id());
258 set_cpu_online(smp_processor_id(), true);
259 unlock_vector_lock();
260 cpu_set_state_online(smp_processor_id());
261 x86_platform.nmi_init();
262
263 /* enable local interrupts */
264 local_irq_enable();
265
266 /* to prevent fake stack check failure in clock setup */
267 boot_init_stack_canary();
268
269 x86_cpuinit.setup_percpu_clockev();
270
271 wmb();
272 cpu_startup_entry(CPUHP_AP_ONLINE_IDLE);
273 }
274
275 /**
276 * topology_update_package_map - Update the physical to logical package map
277 * @pkg: The physical package id as retrieved via CPUID
278 * @cpu: The cpu for which this is updated
279 */
topology_update_package_map(unsigned int pkg,unsigned int cpu)280 int topology_update_package_map(unsigned int pkg, unsigned int cpu)
281 {
282 unsigned int new;
283
284 /* Called from early boot ? */
285 if (!physical_package_map)
286 return 0;
287
288 if (pkg >= max_physical_pkg_id)
289 return -EINVAL;
290
291 /* Set the logical package id */
292 if (test_and_set_bit(pkg, physical_package_map))
293 goto found;
294
295 if (logical_packages >= __max_logical_packages) {
296 pr_warn("Package %u of CPU %u exceeds BIOS package data %u.\n",
297 logical_packages, cpu, __max_logical_packages);
298 return -ENOSPC;
299 }
300
301 new = logical_packages++;
302 if (new != pkg) {
303 pr_info("CPU %u Converting physical %u to logical package %u\n",
304 cpu, pkg, new);
305 }
306 physical_to_logical_pkg[pkg] = new;
307
308 found:
309 cpu_data(cpu).logical_proc_id = physical_to_logical_pkg[pkg];
310 return 0;
311 }
312
313 /**
314 * topology_is_primary_thread - Check whether CPU is the primary SMT thread
315 * @cpu: CPU to check
316 */
topology_is_primary_thread(unsigned int cpu)317 bool topology_is_primary_thread(unsigned int cpu)
318 {
319 return apic_id_is_primary_thread(per_cpu(x86_cpu_to_apicid, cpu));
320 }
321
322 /**
323 * topology_smt_supported - Check whether SMT is supported by the CPUs
324 */
topology_smt_supported(void)325 bool topology_smt_supported(void)
326 {
327 return smp_num_siblings > 1;
328 }
329
330 /**
331 * topology_phys_to_logical_pkg - Map a physical package id to a logical
332 *
333 * Returns logical package id or -1 if not found
334 */
topology_phys_to_logical_pkg(unsigned int phys_pkg)335 int topology_phys_to_logical_pkg(unsigned int phys_pkg)
336 {
337 if (phys_pkg >= max_physical_pkg_id)
338 return -1;
339 return physical_to_logical_pkg[phys_pkg];
340 }
341 EXPORT_SYMBOL(topology_phys_to_logical_pkg);
342
smp_init_package_map(struct cpuinfo_x86 * c,unsigned int cpu)343 static void __init smp_init_package_map(struct cpuinfo_x86 *c, unsigned int cpu)
344 {
345 unsigned int ncpus;
346 size_t size;
347
348 /*
349 * Today neither Intel nor AMD support heterogenous systems. That
350 * might change in the future....
351 *
352 * While ideally we'd want '* smp_num_siblings' in the below @ncpus
353 * computation, this won't actually work since some Intel BIOSes
354 * report inconsistent HT data when they disable HT.
355 *
356 * In particular, they reduce the APIC-IDs to only include the cores,
357 * but leave the CPUID topology to say there are (2) siblings.
358 * This means we don't know how many threads there will be until
359 * after the APIC enumeration.
360 *
361 * By not including this we'll sometimes over-estimate the number of
362 * logical packages by the amount of !present siblings, but this is
363 * still better than MAX_LOCAL_APIC.
364 *
365 * We use total_cpus not nr_cpu_ids because nr_cpu_ids can be limited
366 * on the command line leading to a similar issue as the HT disable
367 * problem because the hyperthreads are usually enumerated after the
368 * primary cores.
369 */
370 ncpus = boot_cpu_data.x86_max_cores;
371 if (!ncpus) {
372 pr_warn("x86_max_cores == zero !?!?");
373 ncpus = 1;
374 }
375
376 __max_logical_packages = DIV_ROUND_UP(total_cpus, ncpus);
377 logical_packages = 0;
378
379 /*
380 * Possibly larger than what we need as the number of apic ids per
381 * package can be smaller than the actual used apic ids.
382 */
383 max_physical_pkg_id = DIV_ROUND_UP(MAX_LOCAL_APIC, ncpus);
384 size = max_physical_pkg_id * sizeof(unsigned int);
385 physical_to_logical_pkg = kmalloc(size, GFP_KERNEL);
386 memset(physical_to_logical_pkg, 0xff, size);
387 size = BITS_TO_LONGS(max_physical_pkg_id) * sizeof(unsigned long);
388 physical_package_map = kzalloc(size, GFP_KERNEL);
389
390 pr_info("Max logical packages: %u\n", __max_logical_packages);
391
392 topology_update_package_map(c->phys_proc_id, cpu);
393 }
394
smp_store_boot_cpu_info(void)395 void __init smp_store_boot_cpu_info(void)
396 {
397 int id = 0; /* CPU 0 */
398 struct cpuinfo_x86 *c = &cpu_data(id);
399
400 *c = boot_cpu_data;
401 c->cpu_index = id;
402 smp_init_package_map(c, id);
403 }
404
405 /*
406 * The bootstrap kernel entry code has set these up. Save them for
407 * a given CPU
408 */
smp_store_cpu_info(int id)409 void smp_store_cpu_info(int id)
410 {
411 struct cpuinfo_x86 *c = &cpu_data(id);
412
413 *c = boot_cpu_data;
414 c->cpu_index = id;
415 /*
416 * During boot time, CPU0 has this setup already. Save the info when
417 * bringing up AP or offlined CPU0.
418 */
419 identify_secondary_cpu(c);
420 }
421
422 static bool
topology_same_node(struct cpuinfo_x86 * c,struct cpuinfo_x86 * o)423 topology_same_node(struct cpuinfo_x86 *c, struct cpuinfo_x86 *o)
424 {
425 int cpu1 = c->cpu_index, cpu2 = o->cpu_index;
426
427 return (cpu_to_node(cpu1) == cpu_to_node(cpu2));
428 }
429
430 static bool
topology_sane(struct cpuinfo_x86 * c,struct cpuinfo_x86 * o,const char * name)431 topology_sane(struct cpuinfo_x86 *c, struct cpuinfo_x86 *o, const char *name)
432 {
433 int cpu1 = c->cpu_index, cpu2 = o->cpu_index;
434
435 return !WARN_ONCE(!topology_same_node(c, o),
436 "sched: CPU #%d's %s-sibling CPU #%d is not on the same node! "
437 "[node: %d != %d]. Ignoring dependency.\n",
438 cpu1, name, cpu2, cpu_to_node(cpu1), cpu_to_node(cpu2));
439 }
440
441 #define link_mask(mfunc, c1, c2) \
442 do { \
443 cpumask_set_cpu((c1), mfunc(c2)); \
444 cpumask_set_cpu((c2), mfunc(c1)); \
445 } while (0)
446
match_smt(struct cpuinfo_x86 * c,struct cpuinfo_x86 * o)447 static bool match_smt(struct cpuinfo_x86 *c, struct cpuinfo_x86 *o)
448 {
449 if (boot_cpu_has(X86_FEATURE_TOPOEXT)) {
450 int cpu1 = c->cpu_index, cpu2 = o->cpu_index;
451
452 if (c->phys_proc_id == o->phys_proc_id &&
453 per_cpu(cpu_llc_id, cpu1) == per_cpu(cpu_llc_id, cpu2)) {
454 if (c->cpu_core_id == o->cpu_core_id)
455 return topology_sane(c, o, "smt");
456
457 if ((c->cu_id != 0xff) &&
458 (o->cu_id != 0xff) &&
459 (c->cu_id == o->cu_id))
460 return topology_sane(c, o, "smt");
461 }
462
463 } else if (c->phys_proc_id == o->phys_proc_id &&
464 c->cpu_core_id == o->cpu_core_id) {
465 return topology_sane(c, o, "smt");
466 }
467
468 return false;
469 }
470
match_llc(struct cpuinfo_x86 * c,struct cpuinfo_x86 * o)471 static bool match_llc(struct cpuinfo_x86 *c, struct cpuinfo_x86 *o)
472 {
473 int cpu1 = c->cpu_index, cpu2 = o->cpu_index;
474
475 if (per_cpu(cpu_llc_id, cpu1) != BAD_APICID &&
476 per_cpu(cpu_llc_id, cpu1) == per_cpu(cpu_llc_id, cpu2))
477 return topology_sane(c, o, "llc");
478
479 return false;
480 }
481
482 /*
483 * Unlike the other levels, we do not enforce keeping a
484 * multicore group inside a NUMA node. If this happens, we will
485 * discard the MC level of the topology later.
486 */
match_die(struct cpuinfo_x86 * c,struct cpuinfo_x86 * o)487 static bool match_die(struct cpuinfo_x86 *c, struct cpuinfo_x86 *o)
488 {
489 if (c->phys_proc_id == o->phys_proc_id)
490 return true;
491 return false;
492 }
493
494 #if defined(CONFIG_SCHED_SMT) || defined(CONFIG_SCHED_MC)
x86_sched_itmt_flags(void)495 static inline int x86_sched_itmt_flags(void)
496 {
497 return sysctl_sched_itmt_enabled ? SD_ASYM_PACKING : 0;
498 }
499
500 #ifdef CONFIG_SCHED_MC
x86_core_flags(void)501 static int x86_core_flags(void)
502 {
503 return cpu_core_flags() | x86_sched_itmt_flags();
504 }
505 #endif
506 #ifdef CONFIG_SCHED_SMT
x86_smt_flags(void)507 static int x86_smt_flags(void)
508 {
509 return cpu_smt_flags() | x86_sched_itmt_flags();
510 }
511 #endif
512 #endif
513
514 static struct sched_domain_topology_level x86_numa_in_package_topology[] = {
515 #ifdef CONFIG_SCHED_SMT
516 { cpu_smt_mask, x86_smt_flags, SD_INIT_NAME(SMT) },
517 #endif
518 #ifdef CONFIG_SCHED_MC
519 { cpu_coregroup_mask, x86_core_flags, SD_INIT_NAME(MC) },
520 #endif
521 { NULL, },
522 };
523
524 static struct sched_domain_topology_level x86_topology[] = {
525 #ifdef CONFIG_SCHED_SMT
526 { cpu_smt_mask, x86_smt_flags, SD_INIT_NAME(SMT) },
527 #endif
528 #ifdef CONFIG_SCHED_MC
529 { cpu_coregroup_mask, x86_core_flags, SD_INIT_NAME(MC) },
530 #endif
531 { cpu_cpu_mask, SD_INIT_NAME(DIE) },
532 { NULL, },
533 };
534
535 /*
536 * Set if a package/die has multiple NUMA nodes inside.
537 * AMD Magny-Cours and Intel Cluster-on-Die have this.
538 */
539 static bool x86_has_numa_in_package;
540
set_cpu_sibling_map(int cpu)541 void set_cpu_sibling_map(int cpu)
542 {
543 bool has_smt = smp_num_siblings > 1;
544 bool has_mp = has_smt || boot_cpu_data.x86_max_cores > 1;
545 struct cpuinfo_x86 *c = &cpu_data(cpu);
546 struct cpuinfo_x86 *o;
547 int i, threads;
548
549 cpumask_set_cpu(cpu, cpu_sibling_setup_mask);
550
551 if (!has_mp) {
552 cpumask_set_cpu(cpu, topology_sibling_cpumask(cpu));
553 cpumask_set_cpu(cpu, cpu_llc_shared_mask(cpu));
554 cpumask_set_cpu(cpu, topology_core_cpumask(cpu));
555 c->booted_cores = 1;
556 return;
557 }
558
559 for_each_cpu(i, cpu_sibling_setup_mask) {
560 o = &cpu_data(i);
561
562 if ((i == cpu) || (has_smt && match_smt(c, o)))
563 link_mask(topology_sibling_cpumask, cpu, i);
564
565 if ((i == cpu) || (has_mp && match_llc(c, o)))
566 link_mask(cpu_llc_shared_mask, cpu, i);
567
568 }
569
570 /*
571 * This needs a separate iteration over the cpus because we rely on all
572 * topology_sibling_cpumask links to be set-up.
573 */
574 for_each_cpu(i, cpu_sibling_setup_mask) {
575 o = &cpu_data(i);
576
577 if ((i == cpu) || (has_mp && match_die(c, o))) {
578 link_mask(topology_core_cpumask, cpu, i);
579
580 /*
581 * Does this new cpu bringup a new core?
582 */
583 if (cpumask_weight(
584 topology_sibling_cpumask(cpu)) == 1) {
585 /*
586 * for each core in package, increment
587 * the booted_cores for this new cpu
588 */
589 if (cpumask_first(
590 topology_sibling_cpumask(i)) == i)
591 c->booted_cores++;
592 /*
593 * increment the core count for all
594 * the other cpus in this package
595 */
596 if (i != cpu)
597 cpu_data(i).booted_cores++;
598 } else if (i != cpu && !c->booted_cores)
599 c->booted_cores = cpu_data(i).booted_cores;
600 }
601 if (match_die(c, o) && !topology_same_node(c, o))
602 x86_has_numa_in_package = true;
603 }
604
605 threads = cpumask_weight(topology_sibling_cpumask(cpu));
606 if (threads > __max_smt_threads)
607 __max_smt_threads = threads;
608 }
609
610 /* maps the cpu to the sched domain representing multi-core */
cpu_coregroup_mask(int cpu)611 const struct cpumask *cpu_coregroup_mask(int cpu)
612 {
613 return cpu_llc_shared_mask(cpu);
614 }
615
impress_friends(void)616 static void impress_friends(void)
617 {
618 int cpu;
619 unsigned long bogosum = 0;
620 /*
621 * Allow the user to impress friends.
622 */
623 pr_debug("Before bogomips\n");
624 for_each_possible_cpu(cpu)
625 if (cpumask_test_cpu(cpu, cpu_callout_mask))
626 bogosum += cpu_data(cpu).loops_per_jiffy;
627 pr_info("Total of %d processors activated (%lu.%02lu BogoMIPS)\n",
628 num_online_cpus(),
629 bogosum/(500000/HZ),
630 (bogosum/(5000/HZ))%100);
631
632 pr_debug("Before bogocount - setting activated=1\n");
633 }
634
__inquire_remote_apic(int apicid)635 void __inquire_remote_apic(int apicid)
636 {
637 unsigned i, regs[] = { APIC_ID >> 4, APIC_LVR >> 4, APIC_SPIV >> 4 };
638 const char * const names[] = { "ID", "VERSION", "SPIV" };
639 int timeout;
640 u32 status;
641
642 pr_info("Inquiring remote APIC 0x%x...\n", apicid);
643
644 for (i = 0; i < ARRAY_SIZE(regs); i++) {
645 pr_info("... APIC 0x%x %s: ", apicid, names[i]);
646
647 /*
648 * Wait for idle.
649 */
650 status = safe_apic_wait_icr_idle();
651 if (status)
652 pr_cont("a previous APIC delivery may have failed\n");
653
654 apic_icr_write(APIC_DM_REMRD | regs[i], apicid);
655
656 timeout = 0;
657 do {
658 udelay(100);
659 status = apic_read(APIC_ICR) & APIC_ICR_RR_MASK;
660 } while (status == APIC_ICR_RR_INPROG && timeout++ < 1000);
661
662 switch (status) {
663 case APIC_ICR_RR_VALID:
664 status = apic_read(APIC_RRR);
665 pr_cont("%08x\n", status);
666 break;
667 default:
668 pr_cont("failed\n");
669 }
670 }
671 }
672
673 /*
674 * The Multiprocessor Specification 1.4 (1997) example code suggests
675 * that there should be a 10ms delay between the BSP asserting INIT
676 * and de-asserting INIT, when starting a remote processor.
677 * But that slows boot and resume on modern processors, which include
678 * many cores and don't require that delay.
679 *
680 * Cmdline "init_cpu_udelay=" is available to over-ride this delay.
681 * Modern processor families are quirked to remove the delay entirely.
682 */
683 #define UDELAY_10MS_DEFAULT 10000
684
685 static unsigned int init_udelay = UINT_MAX;
686
cpu_init_udelay(char * str)687 static int __init cpu_init_udelay(char *str)
688 {
689 get_option(&str, &init_udelay);
690
691 return 0;
692 }
693 early_param("cpu_init_udelay", cpu_init_udelay);
694
smp_quirk_init_udelay(void)695 static void __init smp_quirk_init_udelay(void)
696 {
697 /* if cmdline changed it from default, leave it alone */
698 if (init_udelay != UINT_MAX)
699 return;
700
701 /* if modern processor, use no delay */
702 if (((boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) && (boot_cpu_data.x86 == 6)) ||
703 ((boot_cpu_data.x86_vendor == X86_VENDOR_AMD) && (boot_cpu_data.x86 >= 0xF))) {
704 init_udelay = 0;
705 return;
706 }
707 /* else, use legacy delay */
708 init_udelay = UDELAY_10MS_DEFAULT;
709 }
710
711 /*
712 * Poke the other CPU in the eye via NMI to wake it up. Remember that the normal
713 * INIT, INIT, STARTUP sequence will reset the chip hard for us, and this
714 * won't ... remember to clear down the APIC, etc later.
715 */
716 int
wakeup_secondary_cpu_via_nmi(int apicid,unsigned long start_eip)717 wakeup_secondary_cpu_via_nmi(int apicid, unsigned long start_eip)
718 {
719 unsigned long send_status, accept_status = 0;
720 int maxlvt;
721
722 /* Target chip */
723 /* Boot on the stack */
724 /* Kick the second */
725 apic_icr_write(APIC_DM_NMI | apic->dest_logical, apicid);
726
727 pr_debug("Waiting for send to finish...\n");
728 send_status = safe_apic_wait_icr_idle();
729
730 /*
731 * Give the other CPU some time to accept the IPI.
732 */
733 udelay(200);
734 if (APIC_INTEGRATED(boot_cpu_apic_version)) {
735 maxlvt = lapic_get_maxlvt();
736 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
737 apic_write(APIC_ESR, 0);
738 accept_status = (apic_read(APIC_ESR) & 0xEF);
739 }
740 pr_debug("NMI sent\n");
741
742 if (send_status)
743 pr_err("APIC never delivered???\n");
744 if (accept_status)
745 pr_err("APIC delivery error (%lx)\n", accept_status);
746
747 return (send_status | accept_status);
748 }
749
750 static int
wakeup_secondary_cpu_via_init(int phys_apicid,unsigned long start_eip)751 wakeup_secondary_cpu_via_init(int phys_apicid, unsigned long start_eip)
752 {
753 unsigned long send_status = 0, accept_status = 0;
754 int maxlvt, num_starts, j;
755
756 maxlvt = lapic_get_maxlvt();
757
758 /*
759 * Be paranoid about clearing APIC errors.
760 */
761 if (APIC_INTEGRATED(boot_cpu_apic_version)) {
762 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
763 apic_write(APIC_ESR, 0);
764 apic_read(APIC_ESR);
765 }
766
767 pr_debug("Asserting INIT\n");
768
769 /*
770 * Turn INIT on target chip
771 */
772 /*
773 * Send IPI
774 */
775 apic_icr_write(APIC_INT_LEVELTRIG | APIC_INT_ASSERT | APIC_DM_INIT,
776 phys_apicid);
777
778 pr_debug("Waiting for send to finish...\n");
779 send_status = safe_apic_wait_icr_idle();
780
781 udelay(init_udelay);
782
783 pr_debug("Deasserting INIT\n");
784
785 /* Target chip */
786 /* Send IPI */
787 apic_icr_write(APIC_INT_LEVELTRIG | APIC_DM_INIT, phys_apicid);
788
789 pr_debug("Waiting for send to finish...\n");
790 send_status = safe_apic_wait_icr_idle();
791
792 mb();
793
794 /*
795 * Should we send STARTUP IPIs ?
796 *
797 * Determine this based on the APIC version.
798 * If we don't have an integrated APIC, don't send the STARTUP IPIs.
799 */
800 if (APIC_INTEGRATED(boot_cpu_apic_version))
801 num_starts = 2;
802 else
803 num_starts = 0;
804
805 /*
806 * Run STARTUP IPI loop.
807 */
808 pr_debug("#startup loops: %d\n", num_starts);
809
810 for (j = 1; j <= num_starts; j++) {
811 pr_debug("Sending STARTUP #%d\n", j);
812 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
813 apic_write(APIC_ESR, 0);
814 apic_read(APIC_ESR);
815 pr_debug("After apic_write\n");
816
817 /*
818 * STARTUP IPI
819 */
820
821 /* Target chip */
822 /* Boot on the stack */
823 /* Kick the second */
824 apic_icr_write(APIC_DM_STARTUP | (start_eip >> 12),
825 phys_apicid);
826
827 /*
828 * Give the other CPU some time to accept the IPI.
829 */
830 if (init_udelay == 0)
831 udelay(10);
832 else
833 udelay(300);
834
835 pr_debug("Startup point 1\n");
836
837 pr_debug("Waiting for send to finish...\n");
838 send_status = safe_apic_wait_icr_idle();
839
840 /*
841 * Give the other CPU some time to accept the IPI.
842 */
843 if (init_udelay == 0)
844 udelay(10);
845 else
846 udelay(200);
847
848 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
849 apic_write(APIC_ESR, 0);
850 accept_status = (apic_read(APIC_ESR) & 0xEF);
851 if (send_status || accept_status)
852 break;
853 }
854 pr_debug("After Startup\n");
855
856 if (send_status)
857 pr_err("APIC never delivered???\n");
858 if (accept_status)
859 pr_err("APIC delivery error (%lx)\n", accept_status);
860
861 return (send_status | accept_status);
862 }
863
864 /* reduce the number of lines printed when booting a large cpu count system */
announce_cpu(int cpu,int apicid)865 static void announce_cpu(int cpu, int apicid)
866 {
867 static int current_node = -1;
868 int node = early_cpu_to_node(cpu);
869 static int width, node_width;
870
871 if (!width)
872 width = num_digits(num_possible_cpus()) + 1; /* + '#' sign */
873
874 if (!node_width)
875 node_width = num_digits(num_possible_nodes()) + 1; /* + '#' */
876
877 if (cpu == 1)
878 printk(KERN_INFO "x86: Booting SMP configuration:\n");
879
880 if (system_state < SYSTEM_RUNNING) {
881 if (node != current_node) {
882 if (current_node > (-1))
883 pr_cont("\n");
884 current_node = node;
885
886 printk(KERN_INFO ".... node %*s#%d, CPUs: ",
887 node_width - num_digits(node), " ", node);
888 }
889
890 /* Add padding for the BSP */
891 if (cpu == 1)
892 pr_cont("%*s", width + 1, " ");
893
894 pr_cont("%*s#%d", width - num_digits(cpu), " ", cpu);
895
896 } else
897 pr_info("Booting Node %d Processor %d APIC 0x%x\n",
898 node, cpu, apicid);
899 }
900
wakeup_cpu0_nmi(unsigned int cmd,struct pt_regs * regs)901 static int wakeup_cpu0_nmi(unsigned int cmd, struct pt_regs *regs)
902 {
903 int cpu;
904
905 cpu = smp_processor_id();
906 if (cpu == 0 && !cpu_online(cpu) && enable_start_cpu0)
907 return NMI_HANDLED;
908
909 return NMI_DONE;
910 }
911
912 /*
913 * Wake up AP by INIT, INIT, STARTUP sequence.
914 *
915 * Instead of waiting for STARTUP after INITs, BSP will execute the BIOS
916 * boot-strap code which is not a desired behavior for waking up BSP. To
917 * void the boot-strap code, wake up CPU0 by NMI instead.
918 *
919 * This works to wake up soft offlined CPU0 only. If CPU0 is hard offlined
920 * (i.e. physically hot removed and then hot added), NMI won't wake it up.
921 * We'll change this code in the future to wake up hard offlined CPU0 if
922 * real platform and request are available.
923 */
924 static int
wakeup_cpu_via_init_nmi(int cpu,unsigned long start_ip,int apicid,int * cpu0_nmi_registered)925 wakeup_cpu_via_init_nmi(int cpu, unsigned long start_ip, int apicid,
926 int *cpu0_nmi_registered)
927 {
928 int id;
929 int boot_error;
930
931 preempt_disable();
932
933 /*
934 * Wake up AP by INIT, INIT, STARTUP sequence.
935 */
936 if (cpu) {
937 boot_error = wakeup_secondary_cpu_via_init(apicid, start_ip);
938 goto out;
939 }
940
941 /*
942 * Wake up BSP by nmi.
943 *
944 * Register a NMI handler to help wake up CPU0.
945 */
946 boot_error = register_nmi_handler(NMI_LOCAL,
947 wakeup_cpu0_nmi, 0, "wake_cpu0");
948
949 if (!boot_error) {
950 enable_start_cpu0 = 1;
951 *cpu0_nmi_registered = 1;
952 if (apic->dest_logical == APIC_DEST_LOGICAL)
953 id = cpu0_logical_apicid;
954 else
955 id = apicid;
956 boot_error = wakeup_secondary_cpu_via_nmi(id, start_ip);
957 }
958
959 out:
960 preempt_enable();
961
962 return boot_error;
963 }
964
common_cpu_up(unsigned int cpu,struct task_struct * idle)965 void common_cpu_up(unsigned int cpu, struct task_struct *idle)
966 {
967 /* Just in case we booted with a single CPU. */
968 alternatives_enable_smp();
969
970 per_cpu(current_task, cpu) = idle;
971
972 #ifdef CONFIG_X86_32
973 /* Stack for startup_32 can be just as for start_secondary onwards */
974 irq_ctx_init(cpu);
975 per_cpu(cpu_current_top_of_stack, cpu) = task_top_of_stack(idle);
976 #else
977 initial_gs = per_cpu_offset(cpu);
978 #endif
979 }
980
981 /*
982 * NOTE - on most systems this is a PHYSICAL apic ID, but on multiquad
983 * (ie clustered apic addressing mode), this is a LOGICAL apic ID.
984 * Returns zero if CPU booted OK, else error code from
985 * ->wakeup_secondary_cpu.
986 */
do_boot_cpu(int apicid,int cpu,struct task_struct * idle,int * cpu0_nmi_registered)987 static int do_boot_cpu(int apicid, int cpu, struct task_struct *idle,
988 int *cpu0_nmi_registered)
989 {
990 volatile u32 *trampoline_status =
991 (volatile u32 *) __va(real_mode_header->trampoline_status);
992 /* start_ip had better be page-aligned! */
993 unsigned long start_ip = real_mode_header->trampoline_start;
994
995 unsigned long boot_error = 0;
996 unsigned long timeout;
997
998 idle->thread.sp = (unsigned long)task_pt_regs(idle);
999 early_gdt_descr.address = (unsigned long)get_cpu_gdt_rw(cpu);
1000 initial_code = (unsigned long)start_secondary;
1001 initial_stack = idle->thread.sp;
1002
1003 /* Enable the espfix hack for this CPU */
1004 init_espfix_ap(cpu);
1005
1006 /* So we see what's up */
1007 announce_cpu(cpu, apicid);
1008
1009 /*
1010 * This grunge runs the startup process for
1011 * the targeted processor.
1012 */
1013
1014 if (get_uv_system_type() != UV_NON_UNIQUE_APIC) {
1015
1016 pr_debug("Setting warm reset code and vector.\n");
1017
1018 smpboot_setup_warm_reset_vector(start_ip);
1019 /*
1020 * Be paranoid about clearing APIC errors.
1021 */
1022 if (APIC_INTEGRATED(boot_cpu_apic_version)) {
1023 apic_write(APIC_ESR, 0);
1024 apic_read(APIC_ESR);
1025 }
1026 }
1027
1028 /*
1029 * AP might wait on cpu_callout_mask in cpu_init() with
1030 * cpu_initialized_mask set if previous attempt to online
1031 * it timed-out. Clear cpu_initialized_mask so that after
1032 * INIT/SIPI it could start with a clean state.
1033 */
1034 cpumask_clear_cpu(cpu, cpu_initialized_mask);
1035 smp_mb();
1036
1037 /*
1038 * Wake up a CPU in difference cases:
1039 * - Use the method in the APIC driver if it's defined
1040 * Otherwise,
1041 * - Use an INIT boot APIC message for APs or NMI for BSP.
1042 */
1043 if (apic->wakeup_secondary_cpu)
1044 boot_error = apic->wakeup_secondary_cpu(apicid, start_ip);
1045 else
1046 boot_error = wakeup_cpu_via_init_nmi(cpu, start_ip, apicid,
1047 cpu0_nmi_registered);
1048
1049 if (!boot_error) {
1050 /*
1051 * Wait 10s total for first sign of life from AP
1052 */
1053 boot_error = -1;
1054 timeout = jiffies + 10*HZ;
1055 while (time_before(jiffies, timeout)) {
1056 if (cpumask_test_cpu(cpu, cpu_initialized_mask)) {
1057 /*
1058 * Tell AP to proceed with initialization
1059 */
1060 cpumask_set_cpu(cpu, cpu_callout_mask);
1061 boot_error = 0;
1062 break;
1063 }
1064 schedule();
1065 }
1066 }
1067
1068 if (!boot_error) {
1069 /*
1070 * Wait till AP completes initial initialization
1071 */
1072 while (!cpumask_test_cpu(cpu, cpu_callin_mask)) {
1073 /*
1074 * Allow other tasks to run while we wait for the
1075 * AP to come online. This also gives a chance
1076 * for the MTRR work(triggered by the AP coming online)
1077 * to be completed in the stop machine context.
1078 */
1079 schedule();
1080 }
1081 }
1082
1083 /* mark "stuck" area as not stuck */
1084 *trampoline_status = 0;
1085
1086 if (get_uv_system_type() != UV_NON_UNIQUE_APIC) {
1087 /*
1088 * Cleanup possible dangling ends...
1089 */
1090 smpboot_restore_warm_reset_vector();
1091 }
1092
1093 return boot_error;
1094 }
1095
native_cpu_up(unsigned int cpu,struct task_struct * tidle)1096 int native_cpu_up(unsigned int cpu, struct task_struct *tidle)
1097 {
1098 int apicid = apic->cpu_present_to_apicid(cpu);
1099 int cpu0_nmi_registered = 0;
1100 unsigned long flags;
1101 int err, ret = 0;
1102
1103 WARN_ON(irqs_disabled());
1104
1105 pr_debug("++++++++++++++++++++=_---CPU UP %u\n", cpu);
1106
1107 if (apicid == BAD_APICID ||
1108 !physid_isset(apicid, phys_cpu_present_map) ||
1109 !apic->apic_id_valid(apicid)) {
1110 pr_err("%s: bad cpu %d\n", __func__, cpu);
1111 return -EINVAL;
1112 }
1113
1114 /*
1115 * Already booted CPU?
1116 */
1117 if (cpumask_test_cpu(cpu, cpu_callin_mask)) {
1118 pr_debug("do_boot_cpu %d Already started\n", cpu);
1119 return -ENOSYS;
1120 }
1121
1122 /*
1123 * Save current MTRR state in case it was changed since early boot
1124 * (e.g. by the ACPI SMI) to initialize new CPUs with MTRRs in sync:
1125 */
1126 mtrr_save_state();
1127
1128 /* x86 CPUs take themselves offline, so delayed offline is OK. */
1129 err = cpu_check_up_prepare(cpu);
1130 if (err && err != -EBUSY)
1131 return err;
1132
1133 /* the FPU context is blank, nobody can own it */
1134 per_cpu(fpu_fpregs_owner_ctx, cpu) = NULL;
1135
1136 common_cpu_up(cpu, tidle);
1137
1138 err = do_boot_cpu(apicid, cpu, tidle, &cpu0_nmi_registered);
1139 if (err) {
1140 pr_err("do_boot_cpu failed(%d) to wakeup CPU#%u\n", err, cpu);
1141 ret = -EIO;
1142 goto unreg_nmi;
1143 }
1144
1145 /*
1146 * Check TSC synchronization with the AP (keep irqs disabled
1147 * while doing so):
1148 */
1149 local_irq_save(flags);
1150 check_tsc_sync_source(cpu);
1151 local_irq_restore(flags);
1152
1153 while (!cpu_online(cpu)) {
1154 cpu_relax();
1155 touch_nmi_watchdog();
1156 }
1157
1158 unreg_nmi:
1159 /*
1160 * Clean up the nmi handler. Do this after the callin and callout sync
1161 * to avoid impact of possible long unregister time.
1162 */
1163 if (cpu0_nmi_registered)
1164 unregister_nmi_handler(NMI_LOCAL, "wake_cpu0");
1165
1166 return ret;
1167 }
1168
1169 /**
1170 * arch_disable_smp_support() - disables SMP support for x86 at runtime
1171 */
arch_disable_smp_support(void)1172 void arch_disable_smp_support(void)
1173 {
1174 disable_ioapic_support();
1175 }
1176
1177 /*
1178 * Fall back to non SMP mode after errors.
1179 *
1180 * RED-PEN audit/test this more. I bet there is more state messed up here.
1181 */
disable_smp(void)1182 static __init void disable_smp(void)
1183 {
1184 pr_info("SMP disabled\n");
1185
1186 disable_ioapic_support();
1187
1188 init_cpu_present(cpumask_of(0));
1189 init_cpu_possible(cpumask_of(0));
1190
1191 if (smp_found_config)
1192 physid_set_mask_of_physid(boot_cpu_physical_apicid, &phys_cpu_present_map);
1193 else
1194 physid_set_mask_of_physid(0, &phys_cpu_present_map);
1195 cpumask_set_cpu(0, topology_sibling_cpumask(0));
1196 cpumask_set_cpu(0, topology_core_cpumask(0));
1197 }
1198
1199 enum {
1200 SMP_OK,
1201 SMP_NO_CONFIG,
1202 SMP_NO_APIC,
1203 SMP_FORCE_UP,
1204 };
1205
1206 /*
1207 * Various sanity checks.
1208 */
smp_sanity_check(unsigned max_cpus)1209 static int __init smp_sanity_check(unsigned max_cpus)
1210 {
1211 preempt_disable();
1212
1213 #if !defined(CONFIG_X86_BIGSMP) && defined(CONFIG_X86_32)
1214 if (def_to_bigsmp && nr_cpu_ids > 8) {
1215 unsigned int cpu;
1216 unsigned nr;
1217
1218 pr_warn("More than 8 CPUs detected - skipping them\n"
1219 "Use CONFIG_X86_BIGSMP\n");
1220
1221 nr = 0;
1222 for_each_present_cpu(cpu) {
1223 if (nr >= 8)
1224 set_cpu_present(cpu, false);
1225 nr++;
1226 }
1227
1228 nr = 0;
1229 for_each_possible_cpu(cpu) {
1230 if (nr >= 8)
1231 set_cpu_possible(cpu, false);
1232 nr++;
1233 }
1234
1235 nr_cpu_ids = 8;
1236 }
1237 #endif
1238
1239 if (!physid_isset(hard_smp_processor_id(), phys_cpu_present_map)) {
1240 pr_warn("weird, boot CPU (#%d) not listed by the BIOS\n",
1241 hard_smp_processor_id());
1242
1243 physid_set(hard_smp_processor_id(), phys_cpu_present_map);
1244 }
1245
1246 /*
1247 * If we couldn't find an SMP configuration at boot time,
1248 * get out of here now!
1249 */
1250 if (!smp_found_config && !acpi_lapic) {
1251 preempt_enable();
1252 pr_notice("SMP motherboard not detected\n");
1253 return SMP_NO_CONFIG;
1254 }
1255
1256 /*
1257 * Should not be necessary because the MP table should list the boot
1258 * CPU too, but we do it for the sake of robustness anyway.
1259 */
1260 if (!apic->check_phys_apicid_present(boot_cpu_physical_apicid)) {
1261 pr_notice("weird, boot CPU (#%d) not listed by the BIOS\n",
1262 boot_cpu_physical_apicid);
1263 physid_set(hard_smp_processor_id(), phys_cpu_present_map);
1264 }
1265 preempt_enable();
1266
1267 /*
1268 * If we couldn't find a local APIC, then get out of here now!
1269 */
1270 if (APIC_INTEGRATED(boot_cpu_apic_version) &&
1271 !boot_cpu_has(X86_FEATURE_APIC)) {
1272 if (!disable_apic) {
1273 pr_err("BIOS bug, local APIC #%d not detected!...\n",
1274 boot_cpu_physical_apicid);
1275 pr_err("... forcing use of dummy APIC emulation (tell your hw vendor)\n");
1276 }
1277 return SMP_NO_APIC;
1278 }
1279
1280 /*
1281 * If SMP should be disabled, then really disable it!
1282 */
1283 if (!max_cpus) {
1284 pr_info("SMP mode deactivated\n");
1285 return SMP_FORCE_UP;
1286 }
1287
1288 return SMP_OK;
1289 }
1290
smp_cpu_index_default(void)1291 static void __init smp_cpu_index_default(void)
1292 {
1293 int i;
1294 struct cpuinfo_x86 *c;
1295
1296 for_each_possible_cpu(i) {
1297 c = &cpu_data(i);
1298 /* mark all to hotplug */
1299 c->cpu_index = nr_cpu_ids;
1300 }
1301 }
1302
1303 /*
1304 * Prepare for SMP bootup. The MP table or ACPI has been read
1305 * earlier. Just do some sanity checking here and enable APIC mode.
1306 */
native_smp_prepare_cpus(unsigned int max_cpus)1307 void __init native_smp_prepare_cpus(unsigned int max_cpus)
1308 {
1309 unsigned int i;
1310
1311 smp_cpu_index_default();
1312
1313 /*
1314 * Setup boot CPU information
1315 */
1316 smp_store_boot_cpu_info(); /* Final full version of the data */
1317 cpumask_copy(cpu_callin_mask, cpumask_of(0));
1318 mb();
1319
1320 for_each_possible_cpu(i) {
1321 zalloc_cpumask_var(&per_cpu(cpu_sibling_map, i), GFP_KERNEL);
1322 zalloc_cpumask_var(&per_cpu(cpu_core_map, i), GFP_KERNEL);
1323 zalloc_cpumask_var(&per_cpu(cpu_llc_shared_map, i), GFP_KERNEL);
1324 }
1325
1326 /*
1327 * Set 'default' x86 topology, this matches default_topology() in that
1328 * it has NUMA nodes as a topology level. See also
1329 * native_smp_cpus_done().
1330 *
1331 * Must be done before set_cpus_sibling_map() is ran.
1332 */
1333 set_sched_topology(x86_topology);
1334
1335 set_cpu_sibling_map(0);
1336
1337 switch (smp_sanity_check(max_cpus)) {
1338 case SMP_NO_CONFIG:
1339 disable_smp();
1340 if (APIC_init_uniprocessor())
1341 pr_notice("Local APIC not detected. Using dummy APIC emulation.\n");
1342 return;
1343 case SMP_NO_APIC:
1344 disable_smp();
1345 return;
1346 case SMP_FORCE_UP:
1347 disable_smp();
1348 apic_bsp_setup(false);
1349 return;
1350 case SMP_OK:
1351 break;
1352 }
1353
1354 if (read_apic_id() != boot_cpu_physical_apicid) {
1355 panic("Boot APIC ID in local APIC unexpected (%d vs %d)",
1356 read_apic_id(), boot_cpu_physical_apicid);
1357 /* Or can we switch back to PIC here? */
1358 }
1359
1360 default_setup_apic_routing();
1361 cpu0_logical_apicid = apic_bsp_setup(false);
1362
1363 pr_info("CPU0: ");
1364 print_cpu_info(&cpu_data(0));
1365
1366 uv_system_init();
1367
1368 set_mtrr_aps_delayed_init();
1369
1370 smp_quirk_init_udelay();
1371
1372 speculative_store_bypass_ht_init();
1373 }
1374
arch_enable_nonboot_cpus_begin(void)1375 void arch_enable_nonboot_cpus_begin(void)
1376 {
1377 set_mtrr_aps_delayed_init();
1378 }
1379
arch_enable_nonboot_cpus_end(void)1380 void arch_enable_nonboot_cpus_end(void)
1381 {
1382 mtrr_aps_init();
1383 }
1384
1385 /*
1386 * Early setup to make printk work.
1387 */
native_smp_prepare_boot_cpu(void)1388 void __init native_smp_prepare_boot_cpu(void)
1389 {
1390 int me = smp_processor_id();
1391 switch_to_new_gdt(me);
1392 /* already set me in cpu_online_mask in boot_cpu_init() */
1393 cpumask_set_cpu(me, cpu_callout_mask);
1394 cpu_set_state_online(me);
1395 }
1396
native_smp_cpus_done(unsigned int max_cpus)1397 void __init native_smp_cpus_done(unsigned int max_cpus)
1398 {
1399 pr_debug("Boot done\n");
1400
1401 if (x86_has_numa_in_package)
1402 set_sched_topology(x86_numa_in_package_topology);
1403
1404 nmi_selftest();
1405 impress_friends();
1406 setup_ioapic_dest();
1407 mtrr_aps_init();
1408 }
1409
1410 static int __initdata setup_possible_cpus = -1;
_setup_possible_cpus(char * str)1411 static int __init _setup_possible_cpus(char *str)
1412 {
1413 get_option(&str, &setup_possible_cpus);
1414 return 0;
1415 }
1416 early_param("possible_cpus", _setup_possible_cpus);
1417
1418
1419 /*
1420 * cpu_possible_mask should be static, it cannot change as cpu's
1421 * are onlined, or offlined. The reason is per-cpu data-structures
1422 * are allocated by some modules at init time, and dont expect to
1423 * do this dynamically on cpu arrival/departure.
1424 * cpu_present_mask on the other hand can change dynamically.
1425 * In case when cpu_hotplug is not compiled, then we resort to current
1426 * behaviour, which is cpu_possible == cpu_present.
1427 * - Ashok Raj
1428 *
1429 * Three ways to find out the number of additional hotplug CPUs:
1430 * - If the BIOS specified disabled CPUs in ACPI/mptables use that.
1431 * - The user can overwrite it with possible_cpus=NUM
1432 * - Otherwise don't reserve additional CPUs.
1433 * We do this because additional CPUs waste a lot of memory.
1434 * -AK
1435 */
prefill_possible_map(void)1436 __init void prefill_possible_map(void)
1437 {
1438 int i, possible;
1439
1440 /* No boot processor was found in mptable or ACPI MADT */
1441 if (!num_processors) {
1442 if (boot_cpu_has(X86_FEATURE_APIC)) {
1443 int apicid = boot_cpu_physical_apicid;
1444 int cpu = hard_smp_processor_id();
1445
1446 pr_warn("Boot CPU (id %d) not listed by BIOS\n", cpu);
1447
1448 /* Make sure boot cpu is enumerated */
1449 if (apic->cpu_present_to_apicid(0) == BAD_APICID &&
1450 apic->apic_id_valid(apicid))
1451 generic_processor_info(apicid, boot_cpu_apic_version);
1452 }
1453
1454 if (!num_processors)
1455 num_processors = 1;
1456 }
1457
1458 i = setup_max_cpus ?: 1;
1459 if (setup_possible_cpus == -1) {
1460 possible = num_processors;
1461 #ifdef CONFIG_HOTPLUG_CPU
1462 if (setup_max_cpus)
1463 possible += disabled_cpus;
1464 #else
1465 if (possible > i)
1466 possible = i;
1467 #endif
1468 } else
1469 possible = setup_possible_cpus;
1470
1471 total_cpus = max_t(int, possible, num_processors + disabled_cpus);
1472
1473 /* nr_cpu_ids could be reduced via nr_cpus= */
1474 if (possible > nr_cpu_ids) {
1475 pr_warn("%d Processors exceeds NR_CPUS limit of %u\n",
1476 possible, nr_cpu_ids);
1477 possible = nr_cpu_ids;
1478 }
1479
1480 #ifdef CONFIG_HOTPLUG_CPU
1481 if (!setup_max_cpus)
1482 #endif
1483 if (possible > i) {
1484 pr_warn("%d Processors exceeds max_cpus limit of %u\n",
1485 possible, setup_max_cpus);
1486 possible = i;
1487 }
1488
1489 nr_cpu_ids = possible;
1490
1491 pr_info("Allowing %d CPUs, %d hotplug CPUs\n",
1492 possible, max_t(int, possible - num_processors, 0));
1493
1494 reset_cpu_possible_mask();
1495
1496 for (i = 0; i < possible; i++)
1497 set_cpu_possible(i, true);
1498 }
1499
1500 #ifdef CONFIG_HOTPLUG_CPU
1501
1502 /* Recompute SMT state for all CPUs on offline */
recompute_smt_state(void)1503 static void recompute_smt_state(void)
1504 {
1505 int max_threads, cpu;
1506
1507 max_threads = 0;
1508 for_each_online_cpu (cpu) {
1509 int threads = cpumask_weight(topology_sibling_cpumask(cpu));
1510
1511 if (threads > max_threads)
1512 max_threads = threads;
1513 }
1514 __max_smt_threads = max_threads;
1515 }
1516
remove_siblinginfo(int cpu)1517 static void remove_siblinginfo(int cpu)
1518 {
1519 int sibling;
1520 struct cpuinfo_x86 *c = &cpu_data(cpu);
1521
1522 for_each_cpu(sibling, topology_core_cpumask(cpu)) {
1523 cpumask_clear_cpu(cpu, topology_core_cpumask(sibling));
1524 /*/
1525 * last thread sibling in this cpu core going down
1526 */
1527 if (cpumask_weight(topology_sibling_cpumask(cpu)) == 1)
1528 cpu_data(sibling).booted_cores--;
1529 }
1530
1531 for_each_cpu(sibling, topology_sibling_cpumask(cpu))
1532 cpumask_clear_cpu(cpu, topology_sibling_cpumask(sibling));
1533 for_each_cpu(sibling, cpu_llc_shared_mask(cpu))
1534 cpumask_clear_cpu(cpu, cpu_llc_shared_mask(sibling));
1535 cpumask_clear(cpu_llc_shared_mask(cpu));
1536 cpumask_clear(topology_sibling_cpumask(cpu));
1537 cpumask_clear(topology_core_cpumask(cpu));
1538 c->phys_proc_id = 0;
1539 c->cpu_core_id = 0;
1540 c->booted_cores = 0;
1541 cpumask_clear_cpu(cpu, cpu_sibling_setup_mask);
1542 recompute_smt_state();
1543 }
1544
remove_cpu_from_maps(int cpu)1545 static void remove_cpu_from_maps(int cpu)
1546 {
1547 set_cpu_online(cpu, false);
1548 cpumask_clear_cpu(cpu, cpu_callout_mask);
1549 cpumask_clear_cpu(cpu, cpu_callin_mask);
1550 /* was set by cpu_init() */
1551 cpumask_clear_cpu(cpu, cpu_initialized_mask);
1552 numa_remove_cpu(cpu);
1553 }
1554
cpu_disable_common(void)1555 void cpu_disable_common(void)
1556 {
1557 int cpu = smp_processor_id();
1558
1559 remove_siblinginfo(cpu);
1560
1561 /* It's now safe to remove this processor from the online map */
1562 lock_vector_lock();
1563 remove_cpu_from_maps(cpu);
1564 unlock_vector_lock();
1565 fixup_irqs();
1566 }
1567
native_cpu_disable(void)1568 int native_cpu_disable(void)
1569 {
1570 int ret;
1571
1572 ret = check_irq_vectors_for_cpu_disable();
1573 if (ret)
1574 return ret;
1575
1576 clear_local_APIC();
1577 cpu_disable_common();
1578
1579 return 0;
1580 }
1581
common_cpu_die(unsigned int cpu)1582 int common_cpu_die(unsigned int cpu)
1583 {
1584 int ret = 0;
1585
1586 /* We don't do anything here: idle task is faking death itself. */
1587
1588 /* They ack this in play_dead() by setting CPU_DEAD */
1589 if (cpu_wait_death(cpu, 5)) {
1590 if (system_state == SYSTEM_RUNNING)
1591 pr_info("CPU %u is now offline\n", cpu);
1592 } else {
1593 pr_err("CPU %u didn't die...\n", cpu);
1594 ret = -1;
1595 }
1596
1597 return ret;
1598 }
1599
native_cpu_die(unsigned int cpu)1600 void native_cpu_die(unsigned int cpu)
1601 {
1602 common_cpu_die(cpu);
1603 }
1604
play_dead_common(void)1605 void play_dead_common(void)
1606 {
1607 idle_task_exit();
1608
1609 /* Ack it */
1610 (void)cpu_report_death();
1611
1612 /*
1613 * With physical CPU hotplug, we should halt the cpu
1614 */
1615 local_irq_disable();
1616 }
1617
wakeup_cpu0(void)1618 static bool wakeup_cpu0(void)
1619 {
1620 if (smp_processor_id() == 0 && enable_start_cpu0)
1621 return true;
1622
1623 return false;
1624 }
1625
1626 /*
1627 * We need to flush the caches before going to sleep, lest we have
1628 * dirty data in our caches when we come back up.
1629 */
mwait_play_dead(void)1630 static inline void mwait_play_dead(void)
1631 {
1632 unsigned int eax, ebx, ecx, edx;
1633 unsigned int highest_cstate = 0;
1634 unsigned int highest_subcstate = 0;
1635 void *mwait_ptr;
1636 int i;
1637
1638 if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
1639 return;
1640 if (!this_cpu_has(X86_FEATURE_MWAIT))
1641 return;
1642 if (!this_cpu_has(X86_FEATURE_CLFLUSH))
1643 return;
1644 if (__this_cpu_read(cpu_info.cpuid_level) < CPUID_MWAIT_LEAF)
1645 return;
1646
1647 eax = CPUID_MWAIT_LEAF;
1648 ecx = 0;
1649 native_cpuid(&eax, &ebx, &ecx, &edx);
1650
1651 /*
1652 * eax will be 0 if EDX enumeration is not valid.
1653 * Initialized below to cstate, sub_cstate value when EDX is valid.
1654 */
1655 if (!(ecx & CPUID5_ECX_EXTENSIONS_SUPPORTED)) {
1656 eax = 0;
1657 } else {
1658 edx >>= MWAIT_SUBSTATE_SIZE;
1659 for (i = 0; i < 7 && edx; i++, edx >>= MWAIT_SUBSTATE_SIZE) {
1660 if (edx & MWAIT_SUBSTATE_MASK) {
1661 highest_cstate = i;
1662 highest_subcstate = edx & MWAIT_SUBSTATE_MASK;
1663 }
1664 }
1665 eax = (highest_cstate << MWAIT_SUBSTATE_SIZE) |
1666 (highest_subcstate - 1);
1667 }
1668
1669 /*
1670 * This should be a memory location in a cache line which is
1671 * unlikely to be touched by other processors. The actual
1672 * content is immaterial as it is not actually modified in any way.
1673 */
1674 mwait_ptr = ¤t_thread_info()->flags;
1675
1676 wbinvd();
1677
1678 while (1) {
1679 /*
1680 * The CLFLUSH is a workaround for erratum AAI65 for
1681 * the Xeon 7400 series. It's not clear it is actually
1682 * needed, but it should be harmless in either case.
1683 * The WBINVD is insufficient due to the spurious-wakeup
1684 * case where we return around the loop.
1685 */
1686 mb();
1687 clflush(mwait_ptr);
1688 mb();
1689 __monitor(mwait_ptr, 0, 0);
1690 mb();
1691 __mwait(eax, 0);
1692 /*
1693 * If NMI wants to wake up CPU0, start CPU0.
1694 */
1695 if (wakeup_cpu0())
1696 start_cpu0();
1697 }
1698 }
1699
hlt_play_dead(void)1700 void hlt_play_dead(void)
1701 {
1702 if (__this_cpu_read(cpu_info.x86) >= 4)
1703 wbinvd();
1704
1705 while (1) {
1706 native_halt();
1707 /*
1708 * If NMI wants to wake up CPU0, start CPU0.
1709 */
1710 if (wakeup_cpu0())
1711 start_cpu0();
1712 }
1713 }
1714
native_play_dead(void)1715 void native_play_dead(void)
1716 {
1717 play_dead_common();
1718 tboot_shutdown(TB_SHUTDOWN_WFS);
1719
1720 mwait_play_dead(); /* Only returns on failure */
1721 if (cpuidle_play_dead())
1722 hlt_play_dead();
1723 }
1724
1725 #else /* ... !CONFIG_HOTPLUG_CPU */
native_cpu_disable(void)1726 int native_cpu_disable(void)
1727 {
1728 return -ENOSYS;
1729 }
1730
native_cpu_die(unsigned int cpu)1731 void native_cpu_die(unsigned int cpu)
1732 {
1733 /* We said "no" in __cpu_disable */
1734 BUG();
1735 }
1736
native_play_dead(void)1737 void native_play_dead(void)
1738 {
1739 BUG();
1740 }
1741
1742 #endif
1743