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