2 * Copyright (c) 1996, by Steve Passe
3 * Copyright (c) 2003, by Peter Wemm
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. The name of the developer may NOT be used to endorse or promote products
12 * derived from this software without specific prior written permission.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
35 #include "opt_kstack_pages.h"
37 #include "opt_sched.h"
39 #include "opt_stack.h"
41 #include <sys/param.h>
42 #include <sys/systm.h>
45 #include <sys/cons.h> /* cngetc() */
46 #include <sys/cpuset.h>
48 #include <sys/interrupt.h>
50 #include <sys/kernel.h>
53 #include <sys/malloc.h>
54 #include <sys/memrange.h>
55 #include <sys/mutex.h>
58 #include <sys/sched.h>
60 #include <sys/sysctl.h>
63 #include <vm/vm_param.h>
65 #include <vm/vm_kern.h>
66 #include <vm/vm_extern.h>
67 #include <vm/vm_map.h>
69 #include <x86/apicreg.h>
70 #include <machine/clock.h>
71 #include <machine/cpu.h>
72 #include <machine/cputypes.h>
74 #include <machine/md_var.h>
75 #include <machine/pcb.h>
76 #include <machine/psl.h>
77 #include <machine/smp.h>
78 #include <machine/specialreg.h>
79 #include <machine/stack.h>
80 #include <x86/ucode.h>
83 #include <contrib/dev/acpica/include/acpi.h>
84 #include <dev/acpica/acpivar.h>
87 static MALLOC_DEFINE(M_CPUS, "cpus", "CPU items");
89 int mp_naps; /* # of Applications processors */
90 int boot_cpu_id = -1; /* designated BSP */
92 /* AP uses this during bootstrap. Do not staticize. */
96 /* Free these after use */
97 void *bootstacks[MAXCPU];
100 struct susppcb **susppcbs;
103 /* Interrupt counts. */
104 static u_long *ipi_preempt_counts[MAXCPU];
105 static u_long *ipi_ast_counts[MAXCPU];
106 u_long *ipi_invltlb_counts[MAXCPU];
107 u_long *ipi_invlrng_counts[MAXCPU];
108 u_long *ipi_invlpg_counts[MAXCPU];
109 u_long *ipi_invlcache_counts[MAXCPU];
110 u_long *ipi_rendezvous_counts[MAXCPU];
111 static u_long *ipi_hardclock_counts[MAXCPU];
114 /* Default cpu_ops implementation. */
115 struct cpu_ops cpu_ops;
118 * Local data and functions.
121 static volatile cpuset_t ipi_stop_nmi_pending;
123 volatile cpuset_t resuming_cpus;
124 volatile cpuset_t toresume_cpus;
126 /* used to hold the AP's until we are ready to release them */
127 struct mtx ap_boot_mtx;
129 /* Set to 1 once we're ready to let the APs out of the pen. */
130 volatile int aps_ready = 0;
133 * Store data from cpu_add() until later in the boot when we actually setup
136 struct cpu_info *cpu_info;
138 int cpu_apic_ids[MAXCPU];
139 _Static_assert(MAXCPU <= MAX_APIC_ID,
140 "MAXCPU cannot be larger that MAX_APIC_ID");
141 _Static_assert(xAPIC_MAX_APIC_ID <= MAX_APIC_ID,
142 "xAPIC_MAX_APIC_ID cannot be larger that MAX_APIC_ID");
144 static void release_aps(void *dummy);
145 static void cpustop_handler_post(u_int cpu);
147 static int hyperthreading_allowed = 1;
148 SYSCTL_INT(_machdep, OID_AUTO, hyperthreading_allowed, CTLFLAG_RDTUN,
149 &hyperthreading_allowed, 0, "Use Intel HTT logical CPUs");
151 static int hyperthreading_intr_allowed = 0;
152 SYSCTL_INT(_machdep, OID_AUTO, hyperthreading_intr_allowed, CTLFLAG_RDTUN,
153 &hyperthreading_intr_allowed, 0,
154 "Allow interrupts on HTT logical CPUs");
156 static int intr_apic_id_limit = -1;
157 SYSCTL_INT(_machdep, OID_AUTO, intr_apic_id_limit, CTLFLAG_RDTUN,
158 &intr_apic_id_limit, 0,
159 "Maximum permitted APIC ID for interrupt delivery (-1 is unlimited)");
161 static struct topo_node topo_root;
163 static int pkg_id_shift;
164 static int node_id_shift;
165 static int core_id_shift;
166 static int disabled_cpus;
171 } static caches[MAX_CACHE_LEVELS];
173 static bool stop_mwait = false;
174 SYSCTL_BOOL(_machdep, OID_AUTO, stop_mwait, CTLFLAG_RWTUN, &stop_mwait, 0,
175 "Use MONITOR/MWAIT when stopping CPU, if available");
178 mem_range_AP_init(void)
181 if (mem_range_softc.mr_op && mem_range_softc.mr_op->initAP)
182 mem_range_softc.mr_op->initAP(&mem_range_softc);
186 * Round up to the next power of two, if necessary, and then
188 * Returns -1 if argument is zero.
194 return (fls(x << (1 - powerof2(x))) - 1);
198 * Add a cache level to the cache topology description.
201 add_deterministic_cache(int type, int level, int share_count)
207 printf("unexpected cache type %d\n", type);
210 if (type == 2) /* ignore instruction cache */
212 if (level == 0 || level > MAX_CACHE_LEVELS) {
213 printf("unexpected cache level %d\n", level);
217 if (caches[level - 1].present) {
218 printf("WARNING: multiple entries for L%u data cache\n", level);
219 printf("%u => %u\n", caches[level - 1].id_shift,
220 mask_width(share_count));
222 caches[level - 1].id_shift = mask_width(share_count);
223 caches[level - 1].present = 1;
225 if (caches[level - 1].id_shift > pkg_id_shift) {
226 printf("WARNING: L%u data cache covers more "
227 "APIC IDs than a package (%u > %u)\n", level,
228 caches[level - 1].id_shift, pkg_id_shift);
229 caches[level - 1].id_shift = pkg_id_shift;
231 if (caches[level - 1].id_shift < core_id_shift) {
232 printf("WARNING: L%u data cache covers fewer "
233 "APIC IDs than a core (%u < %u)\n", level,
234 caches[level - 1].id_shift, core_id_shift);
235 caches[level - 1].id_shift = core_id_shift;
242 * Determine topology of processing units and caches for AMD CPUs.
244 * - AMD CPUID Specification (Publication # 25481)
245 * - BKDG for AMD NPT Family 0Fh Processors (Publication # 32559)
246 * - BKDG For AMD Family 10h Processors (Publication # 31116)
247 * - BKDG For AMD Family 15h Models 00h-0Fh Processors (Publication # 42301)
248 * - BKDG For AMD Family 16h Models 00h-0Fh Processors (Publication # 48751)
249 * - PPR For AMD Family 17h Models 00h-0Fh Processors (Publication # 54945)
257 int nodes_per_socket;
262 /* No multi-core capability. */
263 if ((amd_feature2 & AMDID2_CMP) == 0)
267 * XXX Lack of an AMD IOMMU driver prevents use of APIC IDs above
268 * xAPIC_MAX_APIC_ID. This is a workaround so we boot and function on
269 * AMD systems with high thread counts, albeit with reduced interrupt
272 * We should really set the limit to xAPIC_MAX_APIC_ID by default, and
273 * have the IOMMU driver increase it. That way if a driver is present
274 * but disabled, or is otherwise not able to route the interrupts, the
275 * system can fall back to a functional state. That will require a more
276 * substantial change though, including having the IOMMU initialize
279 if (intr_apic_id_limit == -1)
280 intr_apic_id_limit = xAPIC_MAX_APIC_ID;
282 /* For families 10h and newer. */
283 pkg_id_shift = (cpu_procinfo2 & AMDID_COREID_SIZE) >>
284 AMDID_COREID_SIZE_SHIFT;
286 /* For 0Fh family. */
287 if (pkg_id_shift == 0)
289 mask_width((cpu_procinfo2 & AMDID_CMP_CORES) + 1);
292 * Families prior to 16h define the following value as
293 * cores per compute unit and we don't really care about the AMD
294 * compute units at the moment. Perhaps we should treat them as
295 * cores and cores within the compute units as hardware threads,
296 * but that's up for debate.
297 * Later families define the value as threads per compute unit,
298 * so we are following AMD's nomenclature here.
300 if ((amd_feature2 & AMDID2_TOPOLOGY) != 0 &&
301 CPUID_TO_FAMILY(cpu_id) >= 0x16) {
302 cpuid_count(0x8000001e, 0, p);
303 share_count = ((p[1] >> 8) & 0xff) + 1;
304 core_id_shift = mask_width(share_count);
307 * For Zen (17h), gather Nodes per Processor. Each node is a
308 * Zeppelin die; TR and EPYC CPUs will have multiple dies per
309 * package. Communication latency between dies is higher than
312 nodes_per_socket = ((p[2] >> 8) & 0x7) + 1;
313 node_id_shift = pkg_id_shift - mask_width(nodes_per_socket);
316 if ((amd_feature2 & AMDID2_TOPOLOGY) != 0) {
318 cpuid_count(0x8000001d, i, p);
320 level = (p[0] >> 5) & 0x7;
321 share_count = 1 + ((p[0] >> 14) & 0xfff);
323 if (!add_deterministic_cache(type, level, share_count))
327 if (cpu_exthigh >= 0x80000005) {
328 cpuid_count(0x80000005, 0, p);
329 if (((p[2] >> 24) & 0xff) != 0) {
330 caches[0].id_shift = 0;
331 caches[0].present = 1;
334 if (cpu_exthigh >= 0x80000006) {
335 cpuid_count(0x80000006, 0, p);
336 if (((p[2] >> 16) & 0xffff) != 0) {
337 caches[1].id_shift = 0;
338 caches[1].present = 1;
340 if (((p[3] >> 18) & 0x3fff) != 0) {
341 nodes_per_socket = 1;
342 if ((amd_feature2 & AMDID2_NODE_ID) != 0) {
344 * Handle multi-node processors that
345 * have multiple chips, each with its
346 * own L3 cache, on the same die.
348 v = rdmsr(0xc001100c);
349 nodes_per_socket = 1 + ((v >> 3) & 0x7);
352 pkg_id_shift - mask_width(nodes_per_socket);
353 caches[2].present = 1;
360 * Determine topology of processing units for Intel CPUs
361 * using CPUID Leaf 1 and Leaf 4, if supported.
363 * - Intel 64 Architecture Processor Topology Enumeration
364 * - Intel 64 and IA-32 ArchitecturesSoftware Developer’s Manual,
365 * Volume 3A: System Programming Guide, PROGRAMMING CONSIDERATIONS
366 * FOR HARDWARE MULTI-THREADING CAPABLE PROCESSORS
369 topo_probe_intel_0x4(void)
375 /* Both zero and one here mean one logical processor per package. */
376 max_logical = (cpu_feature & CPUID_HTT) != 0 ?
377 (cpu_procinfo & CPUID_HTT_CORES) >> 16 : 1;
378 if (max_logical <= 1)
381 if (cpu_high >= 0x4) {
382 cpuid_count(0x04, 0, p);
383 max_cores = ((p[0] >> 26) & 0x3f) + 1;
387 core_id_shift = mask_width(max_logical/max_cores);
388 KASSERT(core_id_shift >= 0,
389 ("intel topo: max_cores > max_logical\n"));
390 pkg_id_shift = core_id_shift + mask_width(max_cores);
394 * Determine topology of processing units for Intel CPUs
395 * using CPUID Leaf 1Fh or 0Bh, if supported.
397 * - Intel 64 Architecture Processor Topology Enumeration
398 * - Intel 64 and IA-32 ArchitecturesSoftware Developer’s Manual,
399 * Volume 3A: System Programming Guide, PROGRAMMING CONSIDERATIONS
400 * FOR HARDWARE MULTI-THREADING CAPABLE PROCESSORS
403 topo_probe_intel_0xb(void)
411 /* Prefer leaf 1Fh (V2 Extended Topology Enumeration). */
412 if (cpu_high >= 0x1f) {
414 cpuid_count(leaf, 0, p);
416 /* Fall back to leaf 0Bh (Extended Topology Enumeration). */
419 cpuid_count(leaf, 0, p);
421 /* Fall back to leaf 04h (Deterministic Cache Parameters). */
423 topo_probe_intel_0x4();
427 /* We only support three levels for now. */
429 cpuid_count(leaf, i, p);
432 type = (p[2] >> 8) & 0xff;
437 if (type == CPUID_TYPE_SMT)
438 core_id_shift = bits;
439 else if (type == CPUID_TYPE_CORE)
441 else if (bootverbose)
442 printf("Topology level type %d shift: %d\n", type, bits);
445 if (pkg_id_shift < core_id_shift) {
446 printf("WARNING: core covers more APIC IDs than a package\n");
447 core_id_shift = pkg_id_shift;
452 * Determine topology of caches for Intel CPUs.
454 * - Intel 64 Architecture Processor Topology Enumeration
455 * - Intel 64 and IA-32 Architectures Software Developer’s Manual
456 * Volume 2A: Instruction Set Reference, A-M,
460 topo_probe_intel_caches(void)
468 if (cpu_high < 0x4) {
470 * Available cache level and sizes can be determined
471 * via CPUID leaf 2, but that requires a huge table of hardcoded
472 * values, so for now just assume L1 and L2 caches potentially
473 * shared only by HTT processing units, if HTT is present.
475 caches[0].id_shift = pkg_id_shift;
476 caches[0].present = 1;
477 caches[1].id_shift = pkg_id_shift;
478 caches[1].present = 1;
483 cpuid_count(0x4, i, p);
485 level = (p[0] >> 5) & 0x7;
486 share_count = 1 + ((p[0] >> 14) & 0xfff);
488 if (!add_deterministic_cache(type, level, share_count))
494 * Determine topology of processing units and caches for Intel CPUs.
496 * - Intel 64 Architecture Processor Topology Enumeration
499 topo_probe_intel(void)
503 * Note that 0x1 <= cpu_high < 4 case should be
504 * compatible with topo_probe_intel_0x4() logic when
505 * CPUID.1:EBX[23:16] > 0 (cpu_cores will be 1)
506 * or it should trigger the fallback otherwise.
509 topo_probe_intel_0xb();
510 else if (cpu_high >= 0x1)
511 topo_probe_intel_0x4();
513 topo_probe_intel_caches();
517 * Topology information is queried only on BSP, on which this
518 * code runs and for which it can query CPUID information.
519 * Then topology is extrapolated on all packages using an
520 * assumption that APIC ID to hardware component ID mapping is
522 * That doesn't necesserily imply that the topology is uniform.
527 static int cpu_topo_probed = 0;
528 struct x86_topo_layer {
532 } topo_layers[MAX_CACHE_LEVELS + 5];
533 struct topo_node *parent;
534 struct topo_node *node;
539 #if defined(DEV_ACPI) && MAXMEMDOM > 1
546 CPU_ZERO(&logical_cpus_mask);
550 else if (cpu_vendor_id == CPU_VENDOR_AMD ||
551 cpu_vendor_id == CPU_VENDOR_HYGON)
553 else if (cpu_vendor_id == CPU_VENDOR_INTEL)
556 KASSERT(pkg_id_shift >= core_id_shift,
557 ("bug in APIC topology discovery"));
560 bzero(topo_layers, sizeof(topo_layers));
562 topo_layers[nlayers].type = TOPO_TYPE_PKG;
563 topo_layers[nlayers].id_shift = pkg_id_shift;
565 printf("Package ID shift: %u\n", topo_layers[nlayers].id_shift);
568 if (pkg_id_shift > node_id_shift && node_id_shift != 0) {
569 topo_layers[nlayers].type = TOPO_TYPE_GROUP;
570 topo_layers[nlayers].id_shift = node_id_shift;
572 printf("Node ID shift: %u\n",
573 topo_layers[nlayers].id_shift);
578 * Consider all caches to be within a package/chip
579 * and "in front" of all sub-components like
580 * cores and hardware threads.
582 for (i = MAX_CACHE_LEVELS - 1; i >= 0; --i) {
583 if (caches[i].present) {
584 if (node_id_shift != 0)
585 KASSERT(caches[i].id_shift <= node_id_shift,
586 ("bug in APIC topology discovery"));
587 KASSERT(caches[i].id_shift <= pkg_id_shift,
588 ("bug in APIC topology discovery"));
589 KASSERT(caches[i].id_shift >= core_id_shift,
590 ("bug in APIC topology discovery"));
592 topo_layers[nlayers].type = TOPO_TYPE_CACHE;
593 topo_layers[nlayers].subtype = i + 1;
594 topo_layers[nlayers].id_shift = caches[i].id_shift;
596 printf("L%u cache ID shift: %u\n",
597 topo_layers[nlayers].subtype,
598 topo_layers[nlayers].id_shift);
603 if (pkg_id_shift > core_id_shift) {
604 topo_layers[nlayers].type = TOPO_TYPE_CORE;
605 topo_layers[nlayers].id_shift = core_id_shift;
607 printf("Core ID shift: %u\n",
608 topo_layers[nlayers].id_shift);
612 topo_layers[nlayers].type = TOPO_TYPE_PU;
613 topo_layers[nlayers].id_shift = 0;
616 #if defined(DEV_ACPI) && MAXMEMDOM > 1
617 if (vm_ndomains > 1) {
618 for (layer = 0; layer < nlayers; ++layer) {
619 for (i = 0; i <= max_apic_id; ++i) {
620 if ((i & ((1 << topo_layers[layer].id_shift) - 1)) == 0)
622 if (!cpu_info[i].cpu_present)
624 d = acpi_pxm_get_cpu_locality(i);
625 if (domain >= 0 && domain != d)
632 KASSERT(layer < nlayers, ("NUMA domain smaller than PU"));
633 memmove(&topo_layers[layer+1], &topo_layers[layer],
634 sizeof(*topo_layers) * (nlayers - layer));
635 topo_layers[layer].type = TOPO_TYPE_NODE;
636 topo_layers[layer].subtype = CG_SHARE_NONE;
641 topo_init_root(&topo_root);
642 for (i = 0; i <= max_apic_id; ++i) {
643 if (!cpu_info[i].cpu_present)
647 for (layer = 0; layer < nlayers; ++layer) {
648 #if defined(DEV_ACPI) && MAXMEMDOM > 1
649 if (topo_layers[layer].type == TOPO_TYPE_NODE) {
650 node_id = acpi_pxm_get_cpu_locality(i);
653 node_id = i >> topo_layers[layer].id_shift;
654 parent = topo_add_node_by_hwid(parent, node_id,
655 topo_layers[layer].type,
656 topo_layers[layer].subtype);
661 for (layer = 0; layer < nlayers; ++layer) {
662 #if defined(DEV_ACPI) && MAXMEMDOM > 1
663 if (topo_layers[layer].type == TOPO_TYPE_NODE)
664 node_id = acpi_pxm_get_cpu_locality(boot_cpu_id);
667 node_id = boot_cpu_id >> topo_layers[layer].id_shift;
668 node = topo_find_node_by_hwid(parent, node_id,
669 topo_layers[layer].type,
670 topo_layers[layer].subtype);
671 topo_promote_child(node);
679 * Assign logical CPU IDs to local APICs.
684 struct topo_node *node;
688 smt_mask = (1u << core_id_shift) - 1;
691 * Assign CPU IDs to local APIC IDs and disable any CPUs
692 * beyond MAXCPU. CPU 0 is always assigned to the BSP.
696 TOPO_FOREACH(node, &topo_root) {
697 if (node->type != TOPO_TYPE_PU)
700 if ((node->hwid & smt_mask) != (boot_cpu_id & smt_mask))
701 cpu_info[node->hwid].cpu_hyperthread = 1;
703 if (resource_disabled("lapic", node->hwid)) {
704 if (node->hwid != boot_cpu_id)
705 cpu_info[node->hwid].cpu_disabled = 1;
707 printf("Cannot disable BSP, APIC ID = %d\n",
711 if (!hyperthreading_allowed &&
712 cpu_info[node->hwid].cpu_hyperthread)
713 cpu_info[node->hwid].cpu_disabled = 1;
715 if (mp_ncpus >= MAXCPU)
716 cpu_info[node->hwid].cpu_disabled = 1;
718 if (cpu_info[node->hwid].cpu_disabled) {
723 if (cpu_info[node->hwid].cpu_hyperthread)
726 cpu_apic_ids[mp_ncpus] = node->hwid;
727 apic_cpuids[node->hwid] = mp_ncpus;
728 topo_set_pu_id(node, mp_ncpus);
732 KASSERT(mp_maxid >= mp_ncpus - 1,
733 ("%s: counters out of sync: max %d, count %d", __func__, mp_maxid,
736 mp_ncores = mp_ncpus - nhyper;
737 smp_threads_per_core = mp_ncpus / mp_ncores;
741 * Print various information about the SMP system hardware and setup.
744 cpu_mp_announce(void)
746 struct topo_node *node;
747 const char *hyperthread;
748 struct topo_analysis topology;
750 printf("FreeBSD/SMP: ");
751 if (topo_analyze(&topo_root, 1, &topology)) {
752 printf("%d package(s)", topology.entities[TOPO_LEVEL_PKG]);
753 if (topology.entities[TOPO_LEVEL_GROUP] > 1)
754 printf(" x %d groups",
755 topology.entities[TOPO_LEVEL_GROUP]);
756 if (topology.entities[TOPO_LEVEL_CACHEGROUP] > 1)
757 printf(" x %d cache groups",
758 topology.entities[TOPO_LEVEL_CACHEGROUP]);
759 if (topology.entities[TOPO_LEVEL_CORE] > 0)
760 printf(" x %d core(s)",
761 topology.entities[TOPO_LEVEL_CORE]);
762 if (topology.entities[TOPO_LEVEL_THREAD] > 1)
763 printf(" x %d hardware threads",
764 topology.entities[TOPO_LEVEL_THREAD]);
766 printf("Non-uniform topology");
771 printf("FreeBSD/SMP Online: ");
772 if (topo_analyze(&topo_root, 0, &topology)) {
773 printf("%d package(s)",
774 topology.entities[TOPO_LEVEL_PKG]);
775 if (topology.entities[TOPO_LEVEL_GROUP] > 1)
776 printf(" x %d groups",
777 topology.entities[TOPO_LEVEL_GROUP]);
778 if (topology.entities[TOPO_LEVEL_CACHEGROUP] > 1)
779 printf(" x %d cache groups",
780 topology.entities[TOPO_LEVEL_CACHEGROUP]);
781 if (topology.entities[TOPO_LEVEL_CORE] > 0)
782 printf(" x %d core(s)",
783 topology.entities[TOPO_LEVEL_CORE]);
784 if (topology.entities[TOPO_LEVEL_THREAD] > 1)
785 printf(" x %d hardware threads",
786 topology.entities[TOPO_LEVEL_THREAD]);
788 printf("Non-uniform topology");
796 TOPO_FOREACH(node, &topo_root) {
797 switch (node->type) {
799 printf("Package HW ID = %u\n", node->hwid);
802 printf("\tCore HW ID = %u\n", node->hwid);
805 if (cpu_info[node->hwid].cpu_hyperthread)
810 if (node->subtype == 0)
811 printf("\t\tCPU (AP%s): APIC ID: %u"
812 "(disabled)\n", hyperthread, node->hwid);
813 else if (node->id == 0)
814 printf("\t\tCPU0 (BSP): APIC ID: %u\n",
817 printf("\t\tCPU%u (AP%s): APIC ID: %u\n",
818 node->id, hyperthread, node->hwid);
828 * Add a scheduling group, a group of logical processors sharing
829 * a particular cache (and, thus having an affinity), to the scheduling
831 * This function recursively works on lower level caches.
834 x86topo_add_sched_group(struct topo_node *root, struct cpu_group *cg_root)
836 struct topo_node *node;
841 KASSERT(root->type == TOPO_TYPE_SYSTEM || root->type == TOPO_TYPE_CACHE ||
842 root->type == TOPO_TYPE_NODE || root->type == TOPO_TYPE_GROUP,
843 ("x86topo_add_sched_group: bad type: %u", root->type));
844 CPU_COPY(&root->cpuset, &cg_root->cg_mask);
845 cg_root->cg_count = root->cpu_count;
846 if (root->type == TOPO_TYPE_CACHE)
847 cg_root->cg_level = root->subtype;
849 cg_root->cg_level = CG_SHARE_NONE;
850 if (root->type == TOPO_TYPE_NODE)
851 cg_root->cg_flags = CG_FLAG_NODE;
853 cg_root->cg_flags = 0;
856 * Check how many core nodes we have under the given root node.
857 * If we have multiple logical processors, but not multiple
858 * cores, then those processors must be hardware threads.
862 while (node != NULL) {
863 if (node->type != TOPO_TYPE_CORE) {
864 node = topo_next_node(root, node);
869 node = topo_next_nonchild_node(root, node);
872 if (cg_root->cg_level != CG_SHARE_NONE &&
873 root->cpu_count > 1 && ncores < 2)
874 cg_root->cg_flags |= CG_FLAG_SMT;
877 * Find out how many cache nodes we have under the given root node.
878 * We ignore cache nodes that cover all the same processors as the
879 * root node. Also, we do not descend below found cache nodes.
880 * That is, we count top-level "non-redundant" caches under the root
885 while (node != NULL) {
887 * When some APICs are disabled by tunables, nodes can end up
888 * with an empty cpuset. Nodes with an empty cpuset will be
889 * translated into cpu groups with empty cpusets. smp_topo_fill
890 * will then set cg_first and cg_last to -1. This isn't
891 * correctly handled in all functions. E.g. when
892 * cpu_search_lowest and cpu_search_highest loop through all
893 * cpus, they call CPU_ISSET on cpu -1 which ends up in a
894 * general protection fault.
896 * We could fix the scheduler to handle empty cpu groups
897 * correctly. Nevertheless, empty cpu groups are causing
898 * overhead for no value. So, it makes more sense to just don't
901 if (CPU_EMPTY(&node->cpuset)) {
902 node = topo_next_node(root, node);
905 if (CPU_CMP(&node->cpuset, &root->cpuset) == 0) {
906 if (node->type == TOPO_TYPE_CACHE &&
907 cg_root->cg_level < node->subtype)
908 cg_root->cg_level = node->subtype;
909 if (node->type == TOPO_TYPE_NODE)
910 cg_root->cg_flags |= CG_FLAG_NODE;
911 node = topo_next_node(root, node);
914 if (node->type != TOPO_TYPE_GROUP &&
915 node->type != TOPO_TYPE_NODE &&
916 node->type != TOPO_TYPE_CACHE) {
917 node = topo_next_node(root, node);
921 node = topo_next_nonchild_node(root, node);
925 * We are not interested in nodes including only one CPU each.
927 if (nchildren == root->cpu_count)
931 * We are not interested in nodes without children.
933 cg_root->cg_children = nchildren;
937 cg_root->cg_child = smp_topo_alloc(nchildren);
940 * Now find again the same cache nodes as above and recursively
941 * build scheduling topologies for them.
945 while (node != NULL) {
946 if ((node->type != TOPO_TYPE_GROUP &&
947 node->type != TOPO_TYPE_NODE &&
948 node->type != TOPO_TYPE_CACHE) ||
949 CPU_CMP(&node->cpuset, &root->cpuset) == 0 ||
950 CPU_EMPTY(&node->cpuset)) {
951 node = topo_next_node(root, node);
954 cg_root->cg_child[i].cg_parent = cg_root;
955 x86topo_add_sched_group(node, &cg_root->cg_child[i]);
957 node = topo_next_nonchild_node(root, node);
962 * Build the MI scheduling topology from the discovered hardware topology.
967 struct cpu_group *cg_root;
970 return (smp_topo_none());
972 cg_root = smp_topo_alloc(1);
973 x86topo_add_sched_group(&topo_root, cg_root);
978 cpu_alloc(void *dummy __unused)
981 * Dynamically allocate the arrays that depend on the
984 cpu_info = malloc(sizeof(*cpu_info) * (max_apic_id + 1), M_CPUS,
986 apic_cpuids = malloc(sizeof(*apic_cpuids) * (max_apic_id + 1), M_CPUS,
989 SYSINIT(cpu_alloc, SI_SUB_CPU, SI_ORDER_FIRST, cpu_alloc, NULL);
992 * Add a logical CPU to the topology.
995 cpu_add(u_int apic_id, char boot_cpu)
998 if (apic_id > max_apic_id)
999 panic("SMP: APIC ID %d too high", apic_id);
1001 KASSERT(cpu_info[apic_id].cpu_present == 0, ("CPU %u added twice",
1003 cpu_info[apic_id].cpu_present = 1;
1005 KASSERT(boot_cpu_id == -1,
1006 ("CPU %u claims to be BSP, but CPU %u already is", apic_id,
1008 boot_cpu_id = apic_id;
1009 cpu_info[apic_id].cpu_bsp = 1;
1012 printf("SMP: Added CPU %u (%s)\n", apic_id, boot_cpu ? "BSP" :
1017 cpu_mp_setmaxid(void)
1021 * mp_ncpus and mp_maxid should be already set by calls to cpu_add().
1022 * If there were no calls to cpu_add() assume this is a UP system.
1033 * Always record BSP in CPU map so that the mbuf init code works
1036 CPU_SETOF(0, &all_cpus);
1037 return (mp_ncpus > 1);
1041 * AP CPU's call this to initialize themselves.
1044 init_secondary_tail(void)
1048 pmap_activate_boot(vmspace_pmap(proc0.p_vmspace));
1051 * On real hardware, switch to x2apic mode if possible. Do it
1052 * after aps_ready was signalled, to avoid manipulating the
1053 * mode while BSP might still want to send some IPI to us
1054 * (second startup IPI is ignored on modern hardware etc).
1058 /* Initialize the PAT MSR. */
1061 /* set up CPU registers and state */
1067 /* set up FPU state on the AP */
1074 if (cpu_ops.cpu_init)
1077 /* A quick check from sanity claus */
1078 cpuid = PCPU_GET(cpuid);
1079 if (PCPU_GET(apic_id) != lapic_id()) {
1080 printf("SMP: cpuid = %d\n", cpuid);
1081 printf("SMP: actual apic_id = %d\n", lapic_id());
1082 printf("SMP: correct apic_id = %d\n", PCPU_GET(apic_id));
1083 panic("cpuid mismatch! boom!!");
1086 /* Initialize curthread. */
1087 KASSERT(PCPU_GET(idlethread) != NULL, ("no idle thread"));
1088 PCPU_SET(curthread, PCPU_GET(idlethread));
1091 mtx_lock_spin(&ap_boot_mtx);
1095 /* Init local apic for irq's */
1098 /* Set memory range attributes for this CPU to match the BSP */
1099 mem_range_AP_init();
1103 CTR1(KTR_SMP, "SMP: AP CPU #%d Launched", cpuid);
1105 printf("SMP: AP CPU #%d Launched!\n", cpuid);
1107 printf("%s%d%s", smp_cpus == 2 ? "Launching APs: " : "",
1108 cpuid, smp_cpus == mp_ncpus ? "\n" : " ");
1110 /* Determine if we are a logical CPU. */
1111 if (cpu_info[PCPU_GET(apic_id)].cpu_hyperthread)
1112 CPU_SET(cpuid, &logical_cpus_mask);
1117 if (smp_cpus == mp_ncpus) {
1118 /* enable IPI's, tlb shootdown, freezes etc */
1119 atomic_store_rel_int(&smp_started, 1);
1123 if (pmap_pcid_enabled)
1124 load_cr4(rcr4() | CR4_PCIDE);
1130 mtx_unlock_spin(&ap_boot_mtx);
1132 /* Wait until all the AP's are up. */
1133 while (atomic_load_acq_int(&smp_started) == 0)
1136 kcsan_cpu_init(cpuid);
1140 panic("scheduler returned us to %s", __func__);
1145 smp_after_idle_runnable(void *arg __unused)
1152 KASSERT(smp_started != 0, ("%s: SMP not started yet", __func__));
1155 * Wait for all APs to handle an interrupt. After that, we know that
1156 * the APs have entered the scheduler at least once, so the boot stacks
1159 smp_rendezvous(smp_no_rendezvous_barrier, NULL,
1160 smp_no_rendezvous_barrier, NULL);
1162 for (cpu = 1; cpu < mp_ncpus; cpu++) {
1163 kmem_free(bootstacks[cpu], kstack_pages * PAGE_SIZE);
1166 SYSINIT(smp_after_idle_runnable, SI_SUB_SMP, SI_ORDER_ANY,
1167 smp_after_idle_runnable, NULL);
1170 * We tell the I/O APIC code about all the CPUs we want to receive
1171 * interrupts. If we don't want certain CPUs to receive IRQs we
1172 * can simply not tell the I/O APIC code about them in this function.
1173 * We also do not tell it about the BSP since it tells itself about
1174 * the BSP internally to work with UP kernels and on UP machines.
1177 set_interrupt_apic_ids(void)
1181 for (i = 0; i < MAXCPU; i++) {
1182 apic_id = cpu_apic_ids[i];
1185 if (cpu_info[apic_id].cpu_bsp)
1187 if (cpu_info[apic_id].cpu_disabled)
1189 if (intr_apic_id_limit >= 0 && apic_id > intr_apic_id_limit)
1192 /* Don't let hyperthreads service interrupts. */
1193 if (cpu_info[apic_id].cpu_hyperthread &&
1194 !hyperthreading_intr_allowed)
1201 #ifdef COUNT_XINVLTLB_HITS
1202 u_int xhits_gbl[MAXCPU];
1203 u_int xhits_pg[MAXCPU];
1204 u_int xhits_rng[MAXCPU];
1205 static SYSCTL_NODE(_debug, OID_AUTO, xhits, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
1207 SYSCTL_OPAQUE(_debug_xhits, OID_AUTO, global, CTLFLAG_RW, &xhits_gbl,
1208 sizeof(xhits_gbl), "IU", "");
1209 SYSCTL_OPAQUE(_debug_xhits, OID_AUTO, page, CTLFLAG_RW, &xhits_pg,
1210 sizeof(xhits_pg), "IU", "");
1211 SYSCTL_OPAQUE(_debug_xhits, OID_AUTO, range, CTLFLAG_RW, &xhits_rng,
1212 sizeof(xhits_rng), "IU", "");
1217 u_int ipi_range_size;
1218 SYSCTL_INT(_debug_xhits, OID_AUTO, ipi_global, CTLFLAG_RW, &ipi_global, 0, "");
1219 SYSCTL_INT(_debug_xhits, OID_AUTO, ipi_page, CTLFLAG_RW, &ipi_page, 0, "");
1220 SYSCTL_INT(_debug_xhits, OID_AUTO, ipi_range, CTLFLAG_RW, &ipi_range, 0, "");
1221 SYSCTL_INT(_debug_xhits, OID_AUTO, ipi_range_size, CTLFLAG_RW, &ipi_range_size,
1223 #endif /* COUNT_XINVLTLB_HITS */
1226 * Init and startup IPI.
1229 ipi_startup(int apic_id, int vector)
1233 * This attempts to follow the algorithm described in the
1234 * Intel Multiprocessor Specification v1.4 in section B.4.
1235 * For each IPI, we allow the local APIC ~20us to deliver the
1236 * IPI. If that times out, we panic.
1240 * first we do an INIT IPI: this INIT IPI might be run, resetting
1241 * and running the target CPU. OR this INIT IPI might be latched (P5
1242 * bug), CPU waiting for STARTUP IPI. OR this INIT IPI might be
1245 lapic_ipi_raw(APIC_DEST_DESTFLD | APIC_TRIGMOD_LEVEL |
1246 APIC_LEVEL_ASSERT | APIC_DESTMODE_PHY | APIC_DELMODE_INIT, apic_id);
1247 lapic_ipi_wait(100);
1249 /* Explicitly deassert the INIT IPI. */
1250 lapic_ipi_raw(APIC_DEST_DESTFLD | APIC_TRIGMOD_LEVEL |
1251 APIC_LEVEL_DEASSERT | APIC_DESTMODE_PHY | APIC_DELMODE_INIT,
1254 DELAY(10000); /* wait ~10mS */
1257 * next we do a STARTUP IPI: the previous INIT IPI might still be
1258 * latched, (P5 bug) this 1st STARTUP would then terminate
1259 * immediately, and the previously started INIT IPI would continue. OR
1260 * the previous INIT IPI has already run. and this STARTUP IPI will
1261 * run. OR the previous INIT IPI was ignored. and this STARTUP IPI
1264 lapic_ipi_raw(APIC_DEST_DESTFLD | APIC_TRIGMOD_EDGE |
1265 APIC_LEVEL_ASSERT | APIC_DESTMODE_PHY | APIC_DELMODE_STARTUP |
1267 if (!lapic_ipi_wait(100))
1268 panic("Failed to deliver first STARTUP IPI to APIC %d",
1270 DELAY(200); /* wait ~200uS */
1273 * finally we do a 2nd STARTUP IPI: this 2nd STARTUP IPI should run IF
1274 * the previous STARTUP IPI was cancelled by a latched INIT IPI. OR
1275 * this STARTUP IPI will be ignored, as only ONE STARTUP IPI is
1276 * recognized after hardware RESET or INIT IPI.
1278 lapic_ipi_raw(APIC_DEST_DESTFLD | APIC_TRIGMOD_EDGE |
1279 APIC_LEVEL_ASSERT | APIC_DESTMODE_PHY | APIC_DELMODE_STARTUP |
1281 if (!lapic_ipi_wait(100))
1282 panic("Failed to deliver second STARTUP IPI to APIC %d",
1285 DELAY(200); /* wait ~200uS */
1289 ipi_bitmap_set(int cpu, u_int ipi)
1291 u_int bitmap, old, new;
1295 cpu_bitmap = &cpuid_to_pcpu[cpu]->pc_ipi_bitmap;
1298 if ((old & bitmap) != 0)
1301 if (atomic_fcmpset_int(cpu_bitmap, &old, new))
1308 * Send an IPI to specified CPU handling the bitmap logic.
1311 ipi_send_cpu(int cpu, u_int ipi)
1314 KASSERT((u_int)cpu < MAXCPU && cpu_apic_ids[cpu] != -1,
1315 ("IPI to non-existent CPU %d", cpu));
1317 if (IPI_IS_BITMAPED(ipi)) {
1318 if (ipi_bitmap_set(cpu, ipi))
1320 ipi = IPI_BITMAP_VECTOR;
1322 lapic_ipi_vectored(ipi, cpu_apic_ids[cpu]);
1326 ipi_bitmap_handler(struct trapframe frame)
1328 struct trapframe *oldframe;
1330 int cpu = PCPU_GET(cpuid);
1333 kasan_mark(&frame, sizeof(frame), sizeof(frame), 0);
1336 ipi_bitmap = atomic_readandclear_int(&cpuid_to_pcpu[cpu]->
1340 * sched_preempt() must be called to clear the pending preempt
1341 * IPI to enable delivery of further preempts. However, the
1342 * critical section will cause extra scheduler lock thrashing
1343 * when used unconditionally. Only critical_enter() if
1344 * hardclock must also run, which requires the section entry.
1346 if (ipi_bitmap & (1 << IPI_HARDCLOCK))
1349 td->td_intr_nesting_level++;
1350 oldframe = td->td_intr_frame;
1351 td->td_intr_frame = &frame;
1352 #if defined(STACK) || defined(DDB)
1353 if (ipi_bitmap & (1 << IPI_TRACE))
1354 stack_capture_intr();
1356 if (ipi_bitmap & (1 << IPI_PREEMPT)) {
1358 (*ipi_preempt_counts[cpu])++;
1362 if (ipi_bitmap & (1 << IPI_AST)) {
1364 (*ipi_ast_counts[cpu])++;
1366 /* Nothing to do for AST */
1368 if (ipi_bitmap & (1 << IPI_HARDCLOCK)) {
1370 (*ipi_hardclock_counts[cpu])++;
1374 td->td_intr_frame = oldframe;
1375 td->td_intr_nesting_level--;
1376 if (ipi_bitmap & (1 << IPI_HARDCLOCK))
1381 * send an IPI to a set of cpus.
1384 ipi_selected(cpuset_t cpus, u_int ipi)
1389 * IPI_STOP_HARD maps to a NMI and the trap handler needs a bit
1390 * of help in order to understand what is the source.
1391 * Set the mask of receiving CPUs for this purpose.
1393 if (ipi == IPI_STOP_HARD)
1394 CPU_OR_ATOMIC(&ipi_stop_nmi_pending, &cpus);
1396 CPU_FOREACH_ISSET(cpu, &cpus) {
1397 CTR3(KTR_SMP, "%s: cpu: %d ipi: %x", __func__, cpu, ipi);
1398 ipi_send_cpu(cpu, ipi);
1403 * send an IPI to a specific CPU.
1406 ipi_cpu(int cpu, u_int ipi)
1410 * IPI_STOP_HARD maps to a NMI and the trap handler needs a bit
1411 * of help in order to understand what is the source.
1412 * Set the mask of receiving CPUs for this purpose.
1414 if (ipi == IPI_STOP_HARD)
1415 CPU_SET_ATOMIC(cpu, &ipi_stop_nmi_pending);
1417 CTR3(KTR_SMP, "%s: cpu: %d ipi: %x", __func__, cpu, ipi);
1418 ipi_send_cpu(cpu, ipi);
1422 * send an IPI to all CPUs EXCEPT myself
1425 ipi_all_but_self(u_int ipi)
1427 cpuset_t other_cpus;
1431 * IPI_STOP_HARD maps to a NMI and the trap handler needs a bit
1432 * of help in order to understand what is the source.
1433 * Set the mask of receiving CPUs for this purpose.
1435 if (ipi == IPI_STOP_HARD) {
1436 other_cpus = all_cpus;
1437 CPU_CLR(PCPU_GET(cpuid), &other_cpus);
1438 CPU_OR_ATOMIC(&ipi_stop_nmi_pending, &other_cpus);
1441 CTR2(KTR_SMP, "%s: ipi: %x", __func__, ipi);
1442 if (IPI_IS_BITMAPED(ipi)) {
1443 cpu = PCPU_GET(cpuid);
1446 ipi_bitmap_set(c, ipi);
1448 ipi = IPI_BITMAP_VECTOR;
1450 lapic_ipi_vectored(ipi, APIC_IPI_DEST_OTHERS);
1454 ipi_self_from_nmi(u_int vector)
1457 lapic_ipi_vectored(vector, APIC_IPI_DEST_SELF);
1459 /* Wait for IPI to finish. */
1460 if (!lapic_ipi_wait(50000)) {
1461 if (KERNEL_PANICKED())
1464 panic("APIC: IPI is stuck");
1469 ipi_nmi_handler(void)
1474 * As long as there is not a simple way to know about a NMI's
1475 * source, if the bitmask for the current CPU is present in
1476 * the global pending bitword an IPI_STOP_HARD has been issued
1477 * and should be handled.
1479 cpuid = PCPU_GET(cpuid);
1480 if (!CPU_ISSET(cpuid, &ipi_stop_nmi_pending))
1483 CPU_CLR_ATOMIC(cpuid, &ipi_stop_nmi_pending);
1491 nmi_call_kdb_smp(u_int type, struct trapframe *frame)
1496 cpu = PCPU_GET(cpuid);
1497 if (atomic_cmpset_acq_int(&nmi_kdb_lock, 0, 1)) {
1498 nmi_call_kdb(cpu, type, frame);
1501 savectx(&stoppcbs[cpu]);
1502 CPU_SET_ATOMIC(cpu, &stopped_cpus);
1503 while (!atomic_cmpset_acq_int(&nmi_kdb_lock, 0, 1))
1507 atomic_store_rel_int(&nmi_kdb_lock, 0);
1509 cpustop_handler_post(cpu);
1513 * Handle an IPI_STOP by saving our current context and spinning (or mwaiting,
1514 * if available) until we are resumed.
1517 cpustop_handler(void)
1519 struct monitorbuf *mb;
1523 cpu = PCPU_GET(cpuid);
1525 savectx(&stoppcbs[cpu]);
1527 use_mwait = (stop_mwait && (cpu_feature2 & CPUID2_MON) != 0 &&
1528 !mwait_cpustop_broken);
1530 mb = PCPU_PTR(monitorbuf);
1531 atomic_store_int(&mb->stop_state,
1532 MONITOR_STOPSTATE_STOPPED);
1535 /* Indicate that we are stopped */
1536 CPU_SET_ATOMIC(cpu, &stopped_cpus);
1538 /* Wait for restart */
1539 while (!CPU_ISSET(cpu, &started_cpus)) {
1541 cpu_monitor(mb, 0, 0);
1542 if (atomic_load_int(&mb->stop_state) ==
1543 MONITOR_STOPSTATE_STOPPED)
1544 cpu_mwait(0, MWAIT_C1);
1551 * Halt non-BSP CPUs on panic -- we're never going to need them
1552 * again, and might as well save power / release resources
1553 * (e.g., overprovisioned VM infrastructure).
1555 while (__predict_false(!IS_BSP() && KERNEL_PANICKED()))
1559 cpustop_handler_post(cpu);
1563 cpustop_handler_post(u_int cpu)
1566 CPU_CLR_ATOMIC(cpu, &started_cpus);
1567 CPU_CLR_ATOMIC(cpu, &stopped_cpus);
1570 * We don't broadcast TLB invalidations to other CPUs when they are
1571 * stopped. Hence, we clear the TLB before resuming.
1575 #if defined(__amd64__) && (defined(DDB) || defined(GDB))
1576 amd64_db_resume_dbreg();
1579 if (cpu == 0 && cpustop_restartfunc != NULL) {
1580 cpustop_restartfunc();
1581 cpustop_restartfunc = NULL;
1586 * Handle an IPI_SUSPEND by saving our current context and spinning until we
1590 cpususpend_handler(void)
1594 mtx_assert(&smp_ipi_mtx, MA_NOTOWNED);
1596 cpu = PCPU_GET(cpuid);
1600 * Some Xen guest types (PVH) expose a very minimal set of ACPI tables,
1601 * and for example have no support for SCI. That leads to the suspend
1602 * stacks not being allocated, and hence when attempting to perform a
1603 * Xen triggered suspension FreeBSD will hit a #PF. Avoid saving the
1604 * CPU and FPU contexts if the stacks are not allocated, as the
1605 * hypervisor will already take care of this. Note that we could even
1606 * do this for Xen triggered suspensions on guests that have full ACPI
1607 * support, but doing so would introduce extra complexity.
1609 if (susppcbs == NULL) {
1610 KASSERT(vm_guest == VM_GUEST_XEN, ("Missing suspend stack"));
1611 CPU_SET_ATOMIC(cpu, &suspended_cpus);
1612 CPU_SET_ATOMIC(cpu, &resuming_cpus);
1615 if (savectx(&susppcbs[cpu]->sp_pcb)) {
1617 fpususpend(susppcbs[cpu]->sp_fpususpend);
1619 npxsuspend(susppcbs[cpu]->sp_fpususpend);
1622 * suspended_cpus is cleared shortly after each AP is restarted
1623 * by a Startup IPI, so that the BSP can proceed to restarting
1626 * resuming_cpus gets cleared when the AP completes
1627 * initialization after having been released by the BSP.
1628 * resuming_cpus is probably not the best name for the
1629 * variable, because it is actually a set of processors that
1630 * haven't resumed yet and haven't necessarily started resuming.
1632 * Note that suspended_cpus is meaningful only for ACPI suspend
1633 * as it's not really used for Xen suspend since the APs are
1634 * automatically restored to the running state and the correct
1635 * context. For the same reason resumectx is never called in
1638 CPU_SET_ATOMIC(cpu, &suspended_cpus);
1639 CPU_SET_ATOMIC(cpu, &resuming_cpus);
1642 * Invalidate the cache after setting the global status bits.
1643 * The last AP to set its bit may end up being an Owner of the
1644 * corresponding cache line in MOESI protocol. The AP may be
1645 * stopped before the cache line is written to the main memory.
1650 fpuresume(susppcbs[cpu]->sp_fpususpend);
1652 npxresume(susppcbs[cpu]->sp_fpususpend);
1656 PCPU_SET(switchtime, 0);
1657 PCPU_SET(switchticks, ticks);
1659 /* Indicate that we have restarted and restored the context. */
1660 CPU_CLR_ATOMIC(cpu, &suspended_cpus);
1663 /* Wait for resume directive */
1664 while (!CPU_ISSET(cpu, &toresume_cpus))
1667 /* Re-apply microcode updates. */
1671 /* Finish removing the identity mapping of low memory for this AP. */
1675 if (cpu_ops.cpu_resume)
1676 cpu_ops.cpu_resume();
1682 /* Resume MCA and local APIC */
1687 /* Indicate that we are resumed */
1688 CPU_CLR_ATOMIC(cpu, &resuming_cpus);
1689 CPU_CLR_ATOMIC(cpu, &suspended_cpus);
1690 CPU_CLR_ATOMIC(cpu, &toresume_cpus);
1694 * Handle an IPI_SWI by waking delayed SWI thread.
1697 ipi_swi_handler(struct trapframe frame)
1700 intr_event_handle(clk_intr_event, &frame);
1704 * This is called once the rest of the system is up and running and we're
1705 * ready to let the AP's out of the pen.
1708 release_aps(void *dummy __unused)
1713 atomic_store_rel_int(&aps_ready, 1);
1714 while (smp_started == 0)
1717 SYSINIT(start_aps, SI_SUB_SMP, SI_ORDER_FIRST, release_aps, NULL);
1721 * Setup interrupt counters for IPI handlers.
1724 mp_ipi_intrcnt(void *dummy)
1730 snprintf(buf, sizeof(buf), "cpu%d:invltlb", i);
1731 intrcnt_add(buf, &ipi_invltlb_counts[i]);
1732 snprintf(buf, sizeof(buf), "cpu%d:invlrng", i);
1733 intrcnt_add(buf, &ipi_invlrng_counts[i]);
1734 snprintf(buf, sizeof(buf), "cpu%d:invlpg", i);
1735 intrcnt_add(buf, &ipi_invlpg_counts[i]);
1736 snprintf(buf, sizeof(buf), "cpu%d:invlcache", i);
1737 intrcnt_add(buf, &ipi_invlcache_counts[i]);
1738 snprintf(buf, sizeof(buf), "cpu%d:preempt", i);
1739 intrcnt_add(buf, &ipi_preempt_counts[i]);
1740 snprintf(buf, sizeof(buf), "cpu%d:ast", i);
1741 intrcnt_add(buf, &ipi_ast_counts[i]);
1742 snprintf(buf, sizeof(buf), "cpu%d:rendezvous", i);
1743 intrcnt_add(buf, &ipi_rendezvous_counts[i]);
1744 snprintf(buf, sizeof(buf), "cpu%d:hardclock", i);
1745 intrcnt_add(buf, &ipi_hardclock_counts[i]);
1748 SYSINIT(mp_ipi_intrcnt, SI_SUB_INTR, SI_ORDER_MIDDLE, mp_ipi_intrcnt, NULL);