1*7dff59c8Sthorpej /* $NetBSD: subr_cpu.c,v 1.22 2024/03/05 20:59:41 thorpej Exp $ */
2dd632e58Sad
3dd632e58Sad /*-
4c5b06097Sad * Copyright (c) 2007, 2008, 2009, 2010, 2012, 2019, 2020
5c5b06097Sad * The NetBSD Foundation, Inc.
6dd632e58Sad * All rights reserved.
7dd632e58Sad *
8dd632e58Sad * This code is derived from software contributed to The NetBSD Foundation
9dd632e58Sad * by Andrew Doran.
10dd632e58Sad *
11dd632e58Sad * Redistribution and use in source and binary forms, with or without
12dd632e58Sad * modification, are permitted provided that the following conditions
13dd632e58Sad * are met:
14dd632e58Sad * 1. Redistributions of source code must retain the above copyright
15dd632e58Sad * notice, this list of conditions and the following disclaimer.
16dd632e58Sad * 2. Redistributions in binary form must reproduce the above copyright
17dd632e58Sad * notice, this list of conditions and the following disclaimer in the
18dd632e58Sad * documentation and/or other materials provided with the distribution.
19dd632e58Sad *
20dd632e58Sad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21dd632e58Sad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22dd632e58Sad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23dd632e58Sad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24dd632e58Sad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25dd632e58Sad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26dd632e58Sad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27dd632e58Sad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28dd632e58Sad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29dd632e58Sad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30dd632e58Sad * POSSIBILITY OF SUCH DAMAGE.
31dd632e58Sad */
32dd632e58Sad
33dd632e58Sad /*-
34dd632e58Sad * Copyright (c)2007 YAMAMOTO Takashi,
35dd632e58Sad * All rights reserved.
36dd632e58Sad *
37dd632e58Sad * Redistribution and use in source and binary forms, with or without
38dd632e58Sad * modification, are permitted provided that the following conditions
39dd632e58Sad * are met:
40dd632e58Sad * 1. Redistributions of source code must retain the above copyright
41dd632e58Sad * notice, this list of conditions and the following disclaimer.
42dd632e58Sad * 2. Redistributions in binary form must reproduce the above copyright
43dd632e58Sad * notice, this list of conditions and the following disclaimer in the
44dd632e58Sad * documentation and/or other materials provided with the distribution.
45dd632e58Sad *
46dd632e58Sad * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
47dd632e58Sad * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48dd632e58Sad * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49dd632e58Sad * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
50dd632e58Sad * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51dd632e58Sad * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52dd632e58Sad * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53dd632e58Sad * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54dd632e58Sad * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55dd632e58Sad * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56dd632e58Sad * SUCH DAMAGE.
57dd632e58Sad */
58dd632e58Sad
59dd632e58Sad /*
60dd632e58Sad * CPU related routines shared with rump.
61dd632e58Sad */
62dd632e58Sad
63dd632e58Sad #include <sys/cdefs.h>
64*7dff59c8Sthorpej __KERNEL_RCSID(0, "$NetBSD: subr_cpu.c,v 1.22 2024/03/05 20:59:41 thorpej Exp $");
65dd632e58Sad
66dd632e58Sad #include <sys/param.h>
67ba90a6baSad #include <sys/atomic.h>
68dd632e58Sad #include <sys/systm.h>
69dd632e58Sad #include <sys/sched.h>
70dd632e58Sad #include <sys/conf.h>
71dd632e58Sad #include <sys/cpu.h>
72dd632e58Sad #include <sys/proc.h>
73dd632e58Sad #include <sys/kernel.h>
74dd632e58Sad #include <sys/kmem.h>
75dd632e58Sad
765090a56bSad static void cpu_topology_fake1(struct cpu_info *);
775090a56bSad
78dd632e58Sad kmutex_t cpu_lock __cacheline_aligned;
79dd632e58Sad int ncpu __read_mostly;
80dd632e58Sad int ncpuonline __read_mostly;
81dd632e58Sad bool mp_online __read_mostly;
82dd632e58Sad static bool cpu_topology_present __read_mostly;
83c5b06097Sad static bool cpu_topology_haveslow __read_mostly;
84dd632e58Sad int64_t cpu_counts[CPU_COUNT_MAX];
85dd632e58Sad
86dd632e58Sad /* An array of CPUs. There are ncpu entries. */
87dd632e58Sad struct cpu_info **cpu_infos __read_mostly;
88dd632e58Sad
89dd632e58Sad /* Note: set on mi_cpu_attach() and idle_loop(). */
90dd632e58Sad kcpuset_t * kcpuset_attached __read_mostly = NULL;
91dd632e58Sad kcpuset_t * kcpuset_running __read_mostly = NULL;
92dd632e58Sad
93dd632e58Sad static char cpu_model[128];
94dd632e58Sad
95dd632e58Sad /*
96dd632e58Sad * mi_cpu_init: early initialisation of MI CPU related structures.
97dd632e58Sad *
98dd632e58Sad * Note: may not block and memory allocator is not yet available.
99dd632e58Sad */
100dd632e58Sad void
mi_cpu_init(void)101dd632e58Sad mi_cpu_init(void)
102dd632e58Sad {
103869ac779Sad struct cpu_info *ci;
104dd632e58Sad
105dd632e58Sad mutex_init(&cpu_lock, MUTEX_DEFAULT, IPL_NONE);
106dd632e58Sad
107dd632e58Sad kcpuset_create(&kcpuset_attached, true);
108dd632e58Sad kcpuset_create(&kcpuset_running, true);
109dd632e58Sad kcpuset_set(kcpuset_running, 0);
110869ac779Sad
111869ac779Sad ci = curcpu();
1125090a56bSad cpu_topology_fake1(ci);
113dd632e58Sad }
114dd632e58Sad
115dd632e58Sad int
cpu_setmodel(const char * fmt,...)116dd632e58Sad cpu_setmodel(const char *fmt, ...)
117dd632e58Sad {
118dd632e58Sad int len;
119dd632e58Sad va_list ap;
120dd632e58Sad
121dd632e58Sad va_start(ap, fmt);
122dd632e58Sad len = vsnprintf(cpu_model, sizeof(cpu_model), fmt, ap);
123dd632e58Sad va_end(ap);
124dd632e58Sad return len;
125dd632e58Sad }
126dd632e58Sad
127dd632e58Sad const char *
cpu_getmodel(void)128dd632e58Sad cpu_getmodel(void)
129dd632e58Sad {
130dd632e58Sad return cpu_model;
131dd632e58Sad }
132dd632e58Sad
133dd632e58Sad bool
cpu_softintr_p(void)134dd632e58Sad cpu_softintr_p(void)
135dd632e58Sad {
136dd632e58Sad
137dd632e58Sad return (curlwp->l_pflag & LP_INTR) != 0;
138dd632e58Sad }
139dd632e58Sad
1403271fbd4Sriastradh bool
curcpu_stable(void)1413271fbd4Sriastradh curcpu_stable(void)
1423271fbd4Sriastradh {
1433271fbd4Sriastradh struct lwp *const l = curlwp;
1443271fbd4Sriastradh const int pflag = l->l_pflag;
1453271fbd4Sriastradh const int nopreempt = l->l_nopreempt;
1463271fbd4Sriastradh
1473271fbd4Sriastradh /*
1483271fbd4Sriastradh * - Softints (LP_INTR) never migrate between CPUs.
1493271fbd4Sriastradh * - Bound lwps (LP_BOUND), either kthreads created bound to
1503271fbd4Sriastradh * a CPU or any lwps bound with curlwp_bind, never migrate.
1513271fbd4Sriastradh * - If kpreemption is disabled, the lwp can't migrate.
1523271fbd4Sriastradh * - If we're in interrupt context, preemption is blocked.
1533271fbd4Sriastradh *
1543271fbd4Sriastradh * We combine the LP_INTR, LP_BOUND, and l_nopreempt test into
1553271fbd4Sriastradh * a single predicted-true branch so this is cheap to assert in
1563271fbd4Sriastradh * most contexts where it will be used, then fall back to
1573271fbd4Sriastradh * calling the full kpreempt_disabled() and cpu_intr_p() as
1583271fbd4Sriastradh * subroutines.
1593271fbd4Sriastradh *
1603271fbd4Sriastradh * XXX Is cpu_intr_p redundant with kpreempt_disabled?
1613271fbd4Sriastradh */
1623271fbd4Sriastradh return __predict_true(((pflag & (LP_INTR|LP_BOUND)) | nopreempt)
1633271fbd4Sriastradh != 0) ||
1643271fbd4Sriastradh kpreempt_disabled() ||
1653271fbd4Sriastradh cpu_intr_p();
1663271fbd4Sriastradh }
1673271fbd4Sriastradh
168dd632e58Sad /*
169dd632e58Sad * Collect CPU topology information as each CPU is attached. This can be
170dd632e58Sad * called early during boot, so we need to be careful what we do.
171dd632e58Sad */
172dd632e58Sad void
cpu_topology_set(struct cpu_info * ci,u_int package_id,u_int core_id,u_int smt_id,u_int numa_id)173dd632e58Sad cpu_topology_set(struct cpu_info *ci, u_int package_id, u_int core_id,
1746149350cSskrll u_int smt_id, u_int numa_id)
175dd632e58Sad {
176dd632e58Sad enum cpu_rel rel;
177dd632e58Sad
178dd632e58Sad cpu_topology_present = true;
179dd632e58Sad ci->ci_package_id = package_id;
180dd632e58Sad ci->ci_core_id = core_id;
181dd632e58Sad ci->ci_smt_id = smt_id;
182dd632e58Sad ci->ci_numa_id = numa_id;
183dd632e58Sad for (rel = 0; rel < __arraycount(ci->ci_sibling); rel++) {
184dd632e58Sad ci->ci_sibling[rel] = ci;
185dd632e58Sad ci->ci_nsibling[rel] = 1;
186dd632e58Sad }
187dd632e58Sad }
188dd632e58Sad
189dd632e58Sad /*
1906149350cSskrll * Collect CPU relative speed
1916149350cSskrll */
1926149350cSskrll void
cpu_topology_setspeed(struct cpu_info * ci,bool slow)1936149350cSskrll cpu_topology_setspeed(struct cpu_info *ci, bool slow)
1946149350cSskrll {
1956149350cSskrll
1966149350cSskrll cpu_topology_haveslow |= slow;
1976149350cSskrll ci->ci_is_slow = slow;
1986149350cSskrll }
1996149350cSskrll
2006149350cSskrll /*
201dd632e58Sad * Link a CPU into the given circular list.
202dd632e58Sad */
203dd632e58Sad static void
cpu_topology_link(struct cpu_info * ci,struct cpu_info * ci2,enum cpu_rel rel)204dd632e58Sad cpu_topology_link(struct cpu_info *ci, struct cpu_info *ci2, enum cpu_rel rel)
205dd632e58Sad {
206dd632e58Sad struct cpu_info *ci3;
207dd632e58Sad
208dd632e58Sad /* Walk to the end of the existing circular list and append. */
209dd632e58Sad for (ci3 = ci2;; ci3 = ci3->ci_sibling[rel]) {
210dd632e58Sad ci3->ci_nsibling[rel]++;
211dd632e58Sad if (ci3->ci_sibling[rel] == ci2) {
212dd632e58Sad break;
213dd632e58Sad }
214dd632e58Sad }
215dd632e58Sad ci->ci_sibling[rel] = ci2;
216dd632e58Sad ci3->ci_sibling[rel] = ci;
217dd632e58Sad ci->ci_nsibling[rel] = ci3->ci_nsibling[rel];
218dd632e58Sad }
219dd632e58Sad
220dd632e58Sad /*
221dd632e58Sad * Print out the topology lists.
222dd632e58Sad */
223dd632e58Sad static void
cpu_topology_dump(void)224dd632e58Sad cpu_topology_dump(void)
225dd632e58Sad {
226dd632e58Sad CPU_INFO_ITERATOR cii;
227dd632e58Sad struct cpu_info *ci, *ci2;
228c5b06097Sad const char *names[] = { "core", "pkg", "1st" };
229dd632e58Sad enum cpu_rel rel;
230dd632e58Sad int i;
231dd632e58Sad
232600b17ecSmrg CTASSERT(__arraycount(names) >= __arraycount(ci->ci_sibling));
2337acb61f4Ssimonb if (ncpu == 1) {
2347acb61f4Ssimonb return;
2357acb61f4Ssimonb }
236600b17ecSmrg
237dd632e58Sad for (CPU_INFO_FOREACH(cii, ci)) {
238600b17ecSmrg if (cpu_topology_haveslow)
2395238b8a3Smlelstv aprint_debug("%s ", ci->ci_is_slow ? "slow" : "fast");
240dd632e58Sad for (rel = 0; rel < __arraycount(ci->ci_sibling); rel++) {
2415238b8a3Smlelstv aprint_debug("%s has %d %s siblings:", cpu_name(ci),
242dd632e58Sad ci->ci_nsibling[rel], names[rel]);
243dd632e58Sad ci2 = ci->ci_sibling[rel];
244dd632e58Sad i = 0;
245dd632e58Sad do {
2465238b8a3Smlelstv aprint_debug(" %s", cpu_name(ci2));
247dd632e58Sad ci2 = ci2->ci_sibling[rel];
248dd632e58Sad } while (++i < 64 && ci2 != ci->ci_sibling[rel]);
249dd632e58Sad if (i == 64) {
2505238b8a3Smlelstv aprint_debug(" GAVE UP");
251dd632e58Sad }
2525238b8a3Smlelstv aprint_debug("\n");
253dd632e58Sad }
2545238b8a3Smlelstv aprint_debug("%s first in package: %s\n", cpu_name(ci),
2556f4ef5e0Sad cpu_name(ci->ci_package1st));
256dd632e58Sad }
257dd632e58Sad }
258dd632e58Sad
259dd632e58Sad /*
260dd632e58Sad * Fake up topology info if we have none, or if what we got was bogus.
2615090a56bSad * Used early in boot, and by cpu_topology_fake().
262dd632e58Sad */
263dd632e58Sad static void
cpu_topology_fake1(struct cpu_info * ci)2645090a56bSad cpu_topology_fake1(struct cpu_info *ci)
265dd632e58Sad {
266dd632e58Sad enum cpu_rel rel;
267dd632e58Sad
268dd632e58Sad for (rel = 0; rel < __arraycount(ci->ci_sibling); rel++) {
269dd632e58Sad ci->ci_sibling[rel] = ci;
270dd632e58Sad ci->ci_nsibling[rel] = 1;
271dd632e58Sad }
272dd632e58Sad if (!cpu_topology_present) {
273dd632e58Sad ci->ci_package_id = cpu_index(ci);
274dd632e58Sad }
275c5b06097Sad ci->ci_schedstate.spc_flags |=
276c5b06097Sad (SPCF_CORE1ST | SPCF_PACKAGE1ST | SPCF_1STCLASS);
2776f4ef5e0Sad ci->ci_package1st = ci;
2787d77d426Sad if (!cpu_topology_haveslow) {
279d2f8c8b2Sad ci->ci_is_slow = false;
2807d77d426Sad }
2815090a56bSad }
2825090a56bSad
2835090a56bSad /*
2845090a56bSad * Fake up topology info if we have none, or if what we got was bogus.
2855090a56bSad * Don't override ci_package_id, etc, if cpu_topology_present is set.
2865090a56bSad * MD code also uses these.
2875090a56bSad */
2885090a56bSad static void
cpu_topology_fake(void)2895090a56bSad cpu_topology_fake(void)
2905090a56bSad {
2915090a56bSad CPU_INFO_ITERATOR cii;
2925090a56bSad struct cpu_info *ci;
2935090a56bSad
2945090a56bSad for (CPU_INFO_FOREACH(cii, ci)) {
2955090a56bSad cpu_topology_fake1(ci);
296d2f8c8b2Sad /* Undo (early boot) flag set so everything links OK. */
297d2f8c8b2Sad ci->ci_schedstate.spc_flags &=
298d2f8c8b2Sad ~(SPCF_CORE1ST | SPCF_PACKAGE1ST | SPCF_1STCLASS);
299dd632e58Sad }
300dd632e58Sad }
301dd632e58Sad
302dd632e58Sad /*
303dd632e58Sad * Fix up basic CPU topology info. Right now that means attach each CPU to
304dd632e58Sad * circular lists of its siblings in the same core, and in the same package.
305dd632e58Sad */
306dd632e58Sad void
cpu_topology_init(void)307dd632e58Sad cpu_topology_init(void)
308dd632e58Sad {
309dd632e58Sad CPU_INFO_ITERATOR cii, cii2;
310dd632e58Sad struct cpu_info *ci, *ci2, *ci3;
311c5b06097Sad u_int minsmt, mincore;
312dd632e58Sad
313dd632e58Sad if (!cpu_topology_present) {
314dd632e58Sad cpu_topology_fake();
315d2f8c8b2Sad goto linkit;
316dd632e58Sad }
317dd632e58Sad
318dd632e58Sad /* Find siblings in same core and package. */
319dd632e58Sad for (CPU_INFO_FOREACH(cii, ci)) {
320c5b06097Sad ci->ci_schedstate.spc_flags &=
321c5b06097Sad ~(SPCF_CORE1ST | SPCF_PACKAGE1ST | SPCF_1STCLASS);
322dd632e58Sad for (CPU_INFO_FOREACH(cii2, ci2)) {
323dd632e58Sad /* Avoid bad things happening. */
324dd632e58Sad if (ci2->ci_package_id == ci->ci_package_id &&
325dd632e58Sad ci2->ci_core_id == ci->ci_core_id &&
326dd632e58Sad ci2->ci_smt_id == ci->ci_smt_id &&
327dd632e58Sad ci2 != ci) {
328600b17ecSmrg #ifdef DEBUG
329600b17ecSmrg printf("cpu%u %p pkg %u core %u smt %u same as "
330600b17ecSmrg "cpu%u %p pkg %u core %u smt %u\n",
331600b17ecSmrg cpu_index(ci), ci, ci->ci_package_id,
332600b17ecSmrg ci->ci_core_id, ci->ci_smt_id,
333600b17ecSmrg cpu_index(ci2), ci2, ci2->ci_package_id,
334600b17ecSmrg ci2->ci_core_id, ci2->ci_smt_id);
335600b17ecSmrg #endif
336dd632e58Sad printf("cpu_topology_init: info bogus, "
337dd632e58Sad "faking it\n");
338dd632e58Sad cpu_topology_fake();
339d2f8c8b2Sad goto linkit;
340dd632e58Sad }
341dd632e58Sad if (ci2 == ci ||
342dd632e58Sad ci2->ci_package_id != ci->ci_package_id) {
343dd632e58Sad continue;
344dd632e58Sad }
345dd632e58Sad /* Find CPUs in the same core. */
346dd632e58Sad if (ci->ci_nsibling[CPUREL_CORE] == 1 &&
347dd632e58Sad ci->ci_core_id == ci2->ci_core_id) {
348dd632e58Sad cpu_topology_link(ci, ci2, CPUREL_CORE);
349dd632e58Sad }
350dd632e58Sad /* Find CPUs in the same package. */
351dd632e58Sad if (ci->ci_nsibling[CPUREL_PACKAGE] == 1) {
352dd632e58Sad cpu_topology_link(ci, ci2, CPUREL_PACKAGE);
353dd632e58Sad }
354dd632e58Sad if (ci->ci_nsibling[CPUREL_CORE] > 1 &&
355dd632e58Sad ci->ci_nsibling[CPUREL_PACKAGE] > 1) {
356dd632e58Sad break;
357dd632e58Sad }
358dd632e58Sad }
359dd632e58Sad }
360dd632e58Sad
361d2f8c8b2Sad linkit:
362c5b06097Sad /* Identify lowest numbered SMT in each core. */
363dd632e58Sad for (CPU_INFO_FOREACH(cii, ci)) {
364dd632e58Sad ci2 = ci3 = ci;
365dd632e58Sad minsmt = ci->ci_smt_id;
366dd632e58Sad do {
367dd632e58Sad if (ci2->ci_smt_id < minsmt) {
368dd632e58Sad ci3 = ci2;
369dd632e58Sad minsmt = ci2->ci_smt_id;
370dd632e58Sad }
371dd632e58Sad ci2 = ci2->ci_sibling[CPUREL_CORE];
372dd632e58Sad } while (ci2 != ci);
373c5b06097Sad ci3->ci_schedstate.spc_flags |= SPCF_CORE1ST;
374c5b06097Sad }
375dd632e58Sad
376c5b06097Sad /* Identify lowest numbered SMT in each package. */
377c5b06097Sad ci3 = NULL;
378c5b06097Sad for (CPU_INFO_FOREACH(cii, ci)) {
379c5b06097Sad if ((ci->ci_schedstate.spc_flags & SPCF_CORE1ST) == 0) {
380c5b06097Sad continue;
381c5b06097Sad }
382c5b06097Sad ci2 = ci3 = ci;
383c5b06097Sad mincore = ci->ci_core_id;
384c5b06097Sad do {
385c5b06097Sad if ((ci2->ci_schedstate.spc_flags &
386c5b06097Sad SPCF_CORE1ST) != 0 &&
387c5b06097Sad ci2->ci_core_id < mincore) {
388c5b06097Sad ci3 = ci2;
389c5b06097Sad mincore = ci2->ci_core_id;
390c5b06097Sad }
391c5b06097Sad ci2 = ci2->ci_sibling[CPUREL_PACKAGE];
392c5b06097Sad } while (ci2 != ci);
393c5b06097Sad
394c5b06097Sad if ((ci3->ci_schedstate.spc_flags & SPCF_PACKAGE1ST) != 0) {
395c5b06097Sad /* Already identified - nothing more to do. */
396c5b06097Sad continue;
397c5b06097Sad }
398c5b06097Sad ci3->ci_schedstate.spc_flags |= SPCF_PACKAGE1ST;
399c5b06097Sad
400c5b06097Sad /* Walk through all CPUs in package and point to first. */
4016f4ef5e0Sad ci2 = ci3;
402dd632e58Sad do {
4036f4ef5e0Sad ci2->ci_package1st = ci3;
404c5b06097Sad ci2->ci_sibling[CPUREL_PACKAGE1ST] = ci3;
405c5b06097Sad ci2 = ci2->ci_sibling[CPUREL_PACKAGE];
406d2f8c8b2Sad } while (ci2 != ci3);
407c5b06097Sad
408c5b06097Sad /* Now look for somebody else to link to. */
409c5b06097Sad for (CPU_INFO_FOREACH(cii2, ci2)) {
410c5b06097Sad if ((ci2->ci_schedstate.spc_flags & SPCF_PACKAGE1ST)
411c5b06097Sad != 0 && ci2 != ci3) {
412c5b06097Sad cpu_topology_link(ci3, ci2, CPUREL_PACKAGE1ST);
413c5b06097Sad break;
414dd632e58Sad }
415dd632e58Sad }
416c5b06097Sad }
417c5b06097Sad
418c5b06097Sad /* Walk through all packages, starting with value of ci3 from above. */
419c5b06097Sad KASSERT(ci3 != NULL);
420c5b06097Sad ci = ci3;
421c5b06097Sad do {
422c5b06097Sad /* Walk through CPUs in the package and copy in PACKAGE1ST. */
423c5b06097Sad ci2 = ci;
424c5b06097Sad do {
425c5b06097Sad ci2->ci_sibling[CPUREL_PACKAGE1ST] =
426c5b06097Sad ci->ci_sibling[CPUREL_PACKAGE1ST];
427c5b06097Sad ci2->ci_nsibling[CPUREL_PACKAGE1ST] =
428c5b06097Sad ci->ci_nsibling[CPUREL_PACKAGE1ST];
429c5b06097Sad ci2 = ci2->ci_sibling[CPUREL_PACKAGE];
430c5b06097Sad } while (ci2 != ci);
431c5b06097Sad ci = ci->ci_sibling[CPUREL_PACKAGE1ST];
432c5b06097Sad } while (ci != ci3);
433c5b06097Sad
434c5b06097Sad if (cpu_topology_haveslow) {
435c5b06097Sad /*
436aeb90ec8Sad * For asymmetric systems where some CPUs are slower than
437c5b06097Sad * others, mark first class CPUs for the scheduler. This
438c5b06097Sad * conflicts with SMT right now so whinge if observed.
439c5b06097Sad */
4406f4ef5e0Sad if (curcpu()->ci_nsibling[CPUREL_CORE] > 1) {
441c5b06097Sad printf("cpu_topology_init: asymmetric & SMT??\n");
442c5b06097Sad }
443c5b06097Sad for (CPU_INFO_FOREACH(cii, ci)) {
444c5b06097Sad if (!ci->ci_is_slow) {
445c5b06097Sad ci->ci_schedstate.spc_flags |= SPCF_1STCLASS;
446c5b06097Sad }
447c5b06097Sad }
448c5b06097Sad } else {
449c5b06097Sad /*
450c5b06097Sad * For any other configuration mark the 1st CPU in each
451c5b06097Sad * core as a first class CPU.
452c5b06097Sad */
453c5b06097Sad for (CPU_INFO_FOREACH(cii, ci)) {
454c5b06097Sad if ((ci->ci_schedstate.spc_flags & SPCF_CORE1ST) != 0) {
455c5b06097Sad ci->ci_schedstate.spc_flags |= SPCF_1STCLASS;
456c5b06097Sad }
457c5b06097Sad }
458c5b06097Sad }
459c5b06097Sad
460c5b06097Sad cpu_topology_dump();
461c5b06097Sad }
462dd632e58Sad
463dd632e58Sad /*
464dd632e58Sad * Adjust one count, for a counter that's NOT updated from interrupt
465dd632e58Sad * context. Hardly worth making an inline due to preemption stuff.
466dd632e58Sad */
467dd632e58Sad void
cpu_count(enum cpu_count idx,int64_t delta)468dd632e58Sad cpu_count(enum cpu_count idx, int64_t delta)
469dd632e58Sad {
470dd632e58Sad lwp_t *l = curlwp;
471dd632e58Sad KPREEMPT_DISABLE(l);
472dd632e58Sad l->l_cpu->ci_counts[idx] += delta;
473dd632e58Sad KPREEMPT_ENABLE(l);
474dd632e58Sad }
475dd632e58Sad
476dd632e58Sad /*
477dd632e58Sad * Fetch fresh sum total for all counts. Expensive - don't call often.
478ba90a6baSad *
47934df0b37Sandvar * If poll is true, the caller is okay with less recent values (but
480ba90a6baSad * no more than 1/hz seconds old). Where this is called very often that
481ba90a6baSad * should be the case.
482ba90a6baSad *
483ba90a6baSad * This should be reasonably quick so that any value collected get isn't
484ba90a6baSad * totally out of whack, and it can also be called from interrupt context,
485ba90a6baSad * so go to splvm() while summing the counters. It's tempting to use a spin
486ba90a6baSad * mutex here but this routine is called from DDB.
487dd632e58Sad */
488dd632e58Sad void
cpu_count_sync(bool poll)489ba90a6baSad cpu_count_sync(bool poll)
490dd632e58Sad {
491dd632e58Sad CPU_INFO_ITERATOR cii;
492dd632e58Sad struct cpu_info *ci;
493dd632e58Sad int64_t sum[CPU_COUNT_MAX], *ptr;
494ba90a6baSad static int lasttick;
495ba90a6baSad int curtick, s;
496dd632e58Sad enum cpu_count i;
497dd632e58Sad
498dd632e58Sad KASSERT(sizeof(ci->ci_counts) == sizeof(cpu_counts));
499dd632e58Sad
500ba90a6baSad if (__predict_false(!mp_online)) {
501ba90a6baSad memcpy(cpu_counts, curcpu()->ci_counts, sizeof(cpu_counts));
502ba90a6baSad return;
503ba90a6baSad }
504ba90a6baSad
505dd632e58Sad s = splvm();
506ba90a6baSad curtick = getticks();
507ba90a6baSad if (poll && atomic_load_acquire(&lasttick) == curtick) {
508ba90a6baSad splx(s);
509ba90a6baSad return;
510ba90a6baSad }
511ba90a6baSad memset(sum, 0, sizeof(sum));
512ba90a6baSad curcpu()->ci_counts[CPU_COUNT_SYNC]++;
513dd632e58Sad for (CPU_INFO_FOREACH(cii, ci)) {
514dd632e58Sad ptr = ci->ci_counts;
515dd632e58Sad for (i = 0; i < CPU_COUNT_MAX; i += 8) {
516dd632e58Sad sum[i+0] += ptr[i+0];
517dd632e58Sad sum[i+1] += ptr[i+1];
518dd632e58Sad sum[i+2] += ptr[i+2];
519dd632e58Sad sum[i+3] += ptr[i+3];
520dd632e58Sad sum[i+4] += ptr[i+4];
521dd632e58Sad sum[i+5] += ptr[i+5];
522dd632e58Sad sum[i+6] += ptr[i+6];
523dd632e58Sad sum[i+7] += ptr[i+7];
524dd632e58Sad }
525dd632e58Sad KASSERT(i == CPU_COUNT_MAX);
526dd632e58Sad }
527dd632e58Sad memcpy(cpu_counts, sum, sizeof(cpu_counts));
528ba90a6baSad atomic_store_release(&lasttick, curtick);
529dd632e58Sad splx(s);
530dd632e58Sad }
531