subr_cpu.c revision 1.6 1 /* $NetBSD: subr_cpu.c,v 1.6 2020/01/09 16:35:03 ad Exp $ */
2
3 /*-
4 * Copyright (c) 2007, 2008, 2009, 2010, 2012, 2019, 2020
5 * The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Andrew Doran.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 /*-
34 * Copyright (c)2007 YAMAMOTO Takashi,
35 * All rights reserved.
36 *
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
39 * are met:
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 *
46 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
47 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
50 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56 * SUCH DAMAGE.
57 */
58
59 /*
60 * CPU related routines shared with rump.
61 */
62
63 #include <sys/cdefs.h>
64 __KERNEL_RCSID(0, "$NetBSD: subr_cpu.c,v 1.6 2020/01/09 16:35:03 ad Exp $");
65
66 #include <sys/param.h>
67 #include <sys/systm.h>
68 #include <sys/sched.h>
69 #include <sys/conf.h>
70 #include <sys/cpu.h>
71 #include <sys/proc.h>
72 #include <sys/kernel.h>
73 #include <sys/kmem.h>
74
75 static void cpu_topology_fake1(struct cpu_info *);
76
77 kmutex_t cpu_lock __cacheline_aligned;
78 int ncpu __read_mostly;
79 int ncpuonline __read_mostly;
80 bool mp_online __read_mostly;
81 static bool cpu_topology_present __read_mostly;
82 static bool cpu_topology_haveslow __read_mostly;
83 int64_t cpu_counts[CPU_COUNT_MAX];
84
85 /* An array of CPUs. There are ncpu entries. */
86 struct cpu_info **cpu_infos __read_mostly;
87
88 /* Note: set on mi_cpu_attach() and idle_loop(). */
89 kcpuset_t * kcpuset_attached __read_mostly = NULL;
90 kcpuset_t * kcpuset_running __read_mostly = NULL;
91
92 static char cpu_model[128];
93
94 /*
95 * mi_cpu_init: early initialisation of MI CPU related structures.
96 *
97 * Note: may not block and memory allocator is not yet available.
98 */
99 void
100 mi_cpu_init(void)
101 {
102 struct cpu_info *ci;
103
104 mutex_init(&cpu_lock, MUTEX_DEFAULT, IPL_NONE);
105
106 kcpuset_create(&kcpuset_attached, true);
107 kcpuset_create(&kcpuset_running, true);
108 kcpuset_set(kcpuset_running, 0);
109
110 ci = curcpu();
111 cpu_topology_fake1(ci);
112 }
113
114 int
115 cpu_setmodel(const char *fmt, ...)
116 {
117 int len;
118 va_list ap;
119
120 va_start(ap, fmt);
121 len = vsnprintf(cpu_model, sizeof(cpu_model), fmt, ap);
122 va_end(ap);
123 return len;
124 }
125
126 const char *
127 cpu_getmodel(void)
128 {
129 return cpu_model;
130 }
131
132 bool
133 cpu_softintr_p(void)
134 {
135
136 return (curlwp->l_pflag & LP_INTR) != 0;
137 }
138
139 /*
140 * Collect CPU topology information as each CPU is attached. This can be
141 * called early during boot, so we need to be careful what we do.
142 */
143 void
144 cpu_topology_set(struct cpu_info *ci, u_int package_id, u_int core_id,
145 u_int smt_id, u_int numa_id, bool slow)
146 {
147 enum cpu_rel rel;
148
149 cpu_topology_present = true;
150 cpu_topology_haveslow |= slow;
151 ci->ci_package_id = package_id;
152 ci->ci_core_id = core_id;
153 ci->ci_smt_id = smt_id;
154 ci->ci_numa_id = numa_id;
155 for (rel = 0; rel < __arraycount(ci->ci_sibling); rel++) {
156 ci->ci_sibling[rel] = ci;
157 ci->ci_nsibling[rel] = 1;
158 }
159 }
160
161 /*
162 * Link a CPU into the given circular list.
163 */
164 static void
165 cpu_topology_link(struct cpu_info *ci, struct cpu_info *ci2, enum cpu_rel rel)
166 {
167 struct cpu_info *ci3;
168
169 /* Walk to the end of the existing circular list and append. */
170 for (ci3 = ci2;; ci3 = ci3->ci_sibling[rel]) {
171 ci3->ci_nsibling[rel]++;
172 if (ci3->ci_sibling[rel] == ci2) {
173 break;
174 }
175 }
176 ci->ci_sibling[rel] = ci2;
177 ci3->ci_sibling[rel] = ci;
178 ci->ci_nsibling[rel] = ci3->ci_nsibling[rel];
179 }
180
181 /*
182 * Print out the topology lists.
183 */
184 static void
185 cpu_topology_dump(void)
186 {
187 #ifdef DEBUG
188 CPU_INFO_ITERATOR cii;
189 struct cpu_info *ci, *ci2;
190 const char *names[] = { "core", "pkg", "1st" };
191 enum cpu_rel rel;
192 int i;
193
194 for (CPU_INFO_FOREACH(cii, ci)) {
195 for (rel = 0; rel < __arraycount(ci->ci_sibling); rel++) {
196 printf("%s has %d %s siblings:", cpu_name(ci),
197 ci->ci_nsibling[rel], names[rel]);
198 ci2 = ci->ci_sibling[rel];
199 i = 0;
200 do {
201 printf(" %s", cpu_name(ci2));
202 ci2 = ci2->ci_sibling[rel];
203 } while (++i < 64 && ci2 != ci->ci_sibling[rel]);
204 if (i == 64) {
205 printf(" GAVE UP");
206 }
207 printf("\n");
208 }
209 }
210 #endif /* DEBUG */
211 }
212
213 /*
214 * Fake up topology info if we have none, or if what we got was bogus.
215 * Used early in boot, and by cpu_topology_fake().
216 */
217 static void
218 cpu_topology_fake1(struct cpu_info *ci)
219 {
220 enum cpu_rel rel;
221
222 for (rel = 0; rel < __arraycount(ci->ci_sibling); rel++) {
223 ci->ci_sibling[rel] = ci;
224 ci->ci_nsibling[rel] = 1;
225 }
226 if (!cpu_topology_present) {
227 ci->ci_package_id = cpu_index(ci);
228 }
229 ci->ci_schedstate.spc_flags |=
230 (SPCF_CORE1ST | SPCF_PACKAGE1ST | SPCF_1STCLASS);
231 }
232
233 /*
234 * Fake up topology info if we have none, or if what we got was bogus.
235 * Don't override ci_package_id, etc, if cpu_topology_present is set.
236 * MD code also uses these.
237 */
238 static void
239 cpu_topology_fake(void)
240 {
241 CPU_INFO_ITERATOR cii;
242 struct cpu_info *ci;
243
244 for (CPU_INFO_FOREACH(cii, ci)) {
245 cpu_topology_fake1(ci);
246 }
247 cpu_topology_dump();
248 }
249
250 /*
251 * Fix up basic CPU topology info. Right now that means attach each CPU to
252 * circular lists of its siblings in the same core, and in the same package.
253 */
254 void
255 cpu_topology_init(void)
256 {
257 CPU_INFO_ITERATOR cii, cii2;
258 struct cpu_info *ci, *ci2, *ci3;
259 u_int minsmt, mincore;
260
261 if (!cpu_topology_present) {
262 cpu_topology_fake();
263 return;
264 }
265
266 /* Find siblings in same core and package. */
267 for (CPU_INFO_FOREACH(cii, ci)) {
268 ci->ci_schedstate.spc_flags &=
269 ~(SPCF_CORE1ST | SPCF_PACKAGE1ST | SPCF_1STCLASS);
270 for (CPU_INFO_FOREACH(cii2, ci2)) {
271 /* Avoid bad things happening. */
272 if (ci2->ci_package_id == ci->ci_package_id &&
273 ci2->ci_core_id == ci->ci_core_id &&
274 ci2->ci_smt_id == ci->ci_smt_id &&
275 ci2 != ci) {
276 printf("cpu_topology_init: info bogus, "
277 "faking it\n");
278 cpu_topology_fake();
279 return;
280 }
281 if (ci2 == ci ||
282 ci2->ci_package_id != ci->ci_package_id) {
283 continue;
284 }
285 /* Find CPUs in the same core. */
286 if (ci->ci_nsibling[CPUREL_CORE] == 1 &&
287 ci->ci_core_id == ci2->ci_core_id) {
288 cpu_topology_link(ci, ci2, CPUREL_CORE);
289 }
290 /* Find CPUs in the same package. */
291 if (ci->ci_nsibling[CPUREL_PACKAGE] == 1) {
292 cpu_topology_link(ci, ci2, CPUREL_PACKAGE);
293 }
294 if (ci->ci_nsibling[CPUREL_CORE] > 1 &&
295 ci->ci_nsibling[CPUREL_PACKAGE] > 1) {
296 break;
297 }
298 }
299 }
300
301 /* Identify lowest numbered SMT in each core. */
302 for (CPU_INFO_FOREACH(cii, ci)) {
303 ci2 = ci3 = ci;
304 minsmt = ci->ci_smt_id;
305 do {
306 if (ci2->ci_smt_id < minsmt) {
307 ci3 = ci2;
308 minsmt = ci2->ci_smt_id;
309 }
310 ci2 = ci2->ci_sibling[CPUREL_CORE];
311 } while (ci2 != ci);
312 ci3->ci_schedstate.spc_flags |= SPCF_CORE1ST;
313 }
314
315 /* Identify lowest numbered SMT in each package. */
316 ci3 = NULL;
317 for (CPU_INFO_FOREACH(cii, ci)) {
318 if ((ci->ci_schedstate.spc_flags & SPCF_CORE1ST) == 0) {
319 continue;
320 }
321 ci2 = ci3 = ci;
322 mincore = ci->ci_core_id;
323 do {
324 if ((ci2->ci_schedstate.spc_flags &
325 SPCF_CORE1ST) != 0 &&
326 ci2->ci_core_id < mincore) {
327 ci3 = ci2;
328 mincore = ci2->ci_core_id;
329 }
330 ci2 = ci2->ci_sibling[CPUREL_PACKAGE];
331 } while (ci2 != ci);
332
333 if ((ci3->ci_schedstate.spc_flags & SPCF_PACKAGE1ST) != 0) {
334 /* Already identified - nothing more to do. */
335 continue;
336 }
337 ci3->ci_schedstate.spc_flags |= SPCF_PACKAGE1ST;
338
339 /* Walk through all CPUs in package and point to first. */
340 ci2 = ci;
341 do {
342 ci2->ci_sibling[CPUREL_PACKAGE1ST] = ci3;
343 ci2 = ci2->ci_sibling[CPUREL_PACKAGE];
344 } while (ci2 != ci);
345
346 /* Now look for somebody else to link to. */
347 for (CPU_INFO_FOREACH(cii2, ci2)) {
348 if ((ci2->ci_schedstate.spc_flags & SPCF_PACKAGE1ST)
349 != 0 && ci2 != ci3) {
350 cpu_topology_link(ci3, ci2, CPUREL_PACKAGE1ST);
351 break;
352 }
353 }
354 }
355
356 /* Walk through all packages, starting with value of ci3 from above. */
357 KASSERT(ci3 != NULL);
358 ci = ci3;
359 do {
360 /* Walk through CPUs in the package and copy in PACKAGE1ST. */
361 ci2 = ci;
362 do {
363 ci2->ci_sibling[CPUREL_PACKAGE1ST] =
364 ci->ci_sibling[CPUREL_PACKAGE1ST];
365 ci2->ci_nsibling[CPUREL_PACKAGE1ST] =
366 ci->ci_nsibling[CPUREL_PACKAGE1ST];
367 ci2 = ci2->ci_sibling[CPUREL_PACKAGE];
368 } while (ci2 != ci);
369 ci = ci->ci_sibling[CPUREL_PACKAGE1ST];
370 } while (ci != ci3);
371
372 if (cpu_topology_haveslow) {
373 /*
374 * For assymmetric systems where some CPUs are slower than
375 * others, mark first class CPUs for the scheduler. This
376 * conflicts with SMT right now so whinge if observed.
377 */
378 if (curcpu()->ci_nsibling[CPUREL_CORE] == 1) {
379 printf("cpu_topology_init: asymmetric & SMT??\n");
380 }
381 for (CPU_INFO_FOREACH(cii, ci)) {
382 if (!ci->ci_is_slow) {
383 ci->ci_schedstate.spc_flags |= SPCF_1STCLASS;
384 }
385 }
386 } else {
387 /*
388 * For any other configuration mark the 1st CPU in each
389 * core as a first class CPU.
390 */
391 for (CPU_INFO_FOREACH(cii, ci)) {
392 if ((ci->ci_schedstate.spc_flags & SPCF_CORE1ST) != 0) {
393 ci->ci_schedstate.spc_flags |= SPCF_1STCLASS;
394 }
395 }
396 }
397
398 cpu_topology_dump();
399 }
400
401 /*
402 * Adjust one count, for a counter that's NOT updated from interrupt
403 * context. Hardly worth making an inline due to preemption stuff.
404 */
405 void
406 cpu_count(enum cpu_count idx, int64_t delta)
407 {
408 lwp_t *l = curlwp;
409 KPREEMPT_DISABLE(l);
410 l->l_cpu->ci_counts[idx] += delta;
411 KPREEMPT_ENABLE(l);
412 }
413
414 /*
415 * Fetch fresh sum total for all counts. Expensive - don't call often.
416 */
417 void
418 cpu_count_sync_all(void)
419 {
420 CPU_INFO_ITERATOR cii;
421 struct cpu_info *ci;
422 int64_t sum[CPU_COUNT_MAX], *ptr;
423 enum cpu_count i;
424 int s;
425
426 KASSERT(sizeof(ci->ci_counts) == sizeof(cpu_counts));
427
428 if (__predict_true(mp_online)) {
429 memset(sum, 0, sizeof(sum));
430 /*
431 * We want this to be reasonably quick, so any value we get
432 * isn't totally out of whack, so don't let the current LWP
433 * get preempted.
434 */
435 s = splvm();
436 curcpu()->ci_counts[CPU_COUNT_SYNC_ALL]++;
437 for (CPU_INFO_FOREACH(cii, ci)) {
438 ptr = ci->ci_counts;
439 for (i = 0; i < CPU_COUNT_MAX; i += 8) {
440 sum[i+0] += ptr[i+0];
441 sum[i+1] += ptr[i+1];
442 sum[i+2] += ptr[i+2];
443 sum[i+3] += ptr[i+3];
444 sum[i+4] += ptr[i+4];
445 sum[i+5] += ptr[i+5];
446 sum[i+6] += ptr[i+6];
447 sum[i+7] += ptr[i+7];
448 }
449 KASSERT(i == CPU_COUNT_MAX);
450 }
451 memcpy(cpu_counts, sum, sizeof(cpu_counts));
452 splx(s);
453 } else {
454 memcpy(cpu_counts, curcpu()->ci_counts, sizeof(cpu_counts));
455 }
456 }
457
458 /*
459 * Fetch a fresh sum total for one single count. Expensive - don't call often.
460 */
461 int64_t
462 cpu_count_sync(enum cpu_count count)
463 {
464 CPU_INFO_ITERATOR cii;
465 struct cpu_info *ci;
466 int64_t sum;
467 int s;
468
469 if (__predict_true(mp_online)) {
470 s = splvm();
471 curcpu()->ci_counts[CPU_COUNT_SYNC_ONE]++;
472 sum = 0;
473 for (CPU_INFO_FOREACH(cii, ci)) {
474 sum += ci->ci_counts[count];
475 }
476 splx(s);
477 } else {
478 /* XXX Early boot, iterator might not be available. */
479 sum = curcpu()->ci_counts[count];
480 }
481 return cpu_counts[count] = sum;
482 }
483