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