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