xref: /netbsd-src/sys/dev/acpi/acpi_cpu.c (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /* $NetBSD: acpi_cpu.c,v 1.51 2017/06/01 02:45:09 chs Exp $ */
2 
3 /*-
4  * Copyright (c) 2010, 2011 Jukka Ruohonen <jruohonen@iki.fi>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: acpi_cpu.c,v 1.51 2017/06/01 02:45:09 chs Exp $");
31 
32 #include <sys/param.h>
33 #include <sys/cpu.h>
34 #include <sys/evcnt.h>
35 #include <sys/kernel.h>
36 #include <sys/kmem.h>
37 #include <sys/module.h>
38 #include <sys/mutex.h>
39 #include <sys/sysctl.h>
40 #include <sys/cpufreq.h>
41 
42 #include <dev/acpi/acpireg.h>
43 #include <dev/acpi/acpivar.h>
44 #include <dev/acpi/acpi_cpu.h>
45 
46 #include <machine/acpi_machdep.h>
47 #include <machine/cpuvar.h>
48 
49 #define _COMPONENT	  ACPI_BUS_COMPONENT
50 ACPI_MODULE_NAME	  ("acpi_cpu")
51 
52 static int		  acpicpu_match(device_t, cfdata_t, void *);
53 static void		  acpicpu_attach(device_t, device_t, void *);
54 static int		  acpicpu_detach(device_t, int);
55 static int		  acpicpu_once_attach(void);
56 static int		  acpicpu_once_detach(void);
57 static void		  acpicpu_start(device_t);
58 static void		  acpicpu_sysctl(device_t);
59 
60 static ACPI_STATUS	  acpicpu_object(ACPI_HANDLE, struct acpicpu_object *);
61 static uint32_t		  acpicpu_cap(struct acpicpu_softc *);
62 static ACPI_STATUS	  acpicpu_cap_osc(struct acpicpu_softc *,
63 					  uint32_t, uint32_t *);
64 static void		  acpicpu_notify(ACPI_HANDLE, uint32_t, void *);
65 static bool		  acpicpu_suspend(device_t, const pmf_qual_t *);
66 static bool		  acpicpu_resume(device_t, const pmf_qual_t *);
67 static void		  acpicpu_evcnt_attach(device_t);
68 static void		  acpicpu_evcnt_detach(device_t);
69 static void		  acpicpu_debug_print(device_t);
70 static const char	 *acpicpu_debug_print_method_c(uint8_t);
71 static const char	 *acpicpu_debug_print_method_pt(uint8_t);
72 static const char	 *acpicpu_debug_print_dep(uint32_t);
73 
74 static uint32_t		  acpicpu_count = 0;
75 struct acpicpu_softc	**acpicpu_sc = NULL;
76 static struct sysctllog	 *acpicpu_log = NULL;
77 static bool		  acpicpu_dynamic = true;
78 static bool		  acpicpu_passive = true;
79 
80 static const struct {
81 	const char	 *manu;
82 	const char	 *prod;
83 	const char	 *vers;
84 } acpicpu_quirks[] = {
85 	{ "Supermicro", "PDSMi-LN4", "0123456789" },
86 	{ "ASUSTeK Computer INC.", "M2A-MX", "Rev 1.xx" },
87 };
88 
89 CFATTACH_DECL_NEW(acpicpu, sizeof(struct acpicpu_softc),
90     acpicpu_match, acpicpu_attach, acpicpu_detach, NULL);
91 
92 static int
93 acpicpu_match(device_t parent, cfdata_t match, void *aux)
94 {
95 	const char *manu, *prod, *vers;
96 	struct cpu_info *ci;
97 	size_t i;
98 
99 	if (acpi_softc == NULL)
100 		return 0;
101 
102 	manu = pmf_get_platform("board-vendor");
103 	prod = pmf_get_platform("board-product");
104 	vers = pmf_get_platform("board-version");
105 
106 	if (manu != NULL && prod != NULL && vers != NULL) {
107 
108 		for (i = 0; i < __arraycount(acpicpu_quirks); i++) {
109 
110 			if (strcasecmp(acpicpu_quirks[i].manu, manu) == 0 &&
111 			    strcasecmp(acpicpu_quirks[i].prod, prod) == 0 &&
112 			    strcasecmp(acpicpu_quirks[i].vers, vers) == 0)
113 				return 0;
114 		}
115 	}
116 
117 	ci = acpicpu_md_match(parent, match, aux);
118 
119 	if (ci == NULL)
120 		return 0;
121 
122 	if (acpi_match_cpu_info(ci) == NULL)
123 		return 0;
124 
125 	return 10;
126 }
127 
128 static void
129 acpicpu_attach(device_t parent, device_t self, void *aux)
130 {
131 	struct acpicpu_softc *sc = device_private(self);
132 	struct cpu_info *ci;
133 	ACPI_HANDLE hdl;
134 	cpuid_t id;
135 	int rv;
136 
137 	ci = acpicpu_md_attach(parent, self, aux);
138 
139 	if (ci == NULL)
140 		return;
141 
142 	sc->sc_ci = ci;
143 	sc->sc_dev = self;
144 	sc->sc_cold = true;
145 
146 	hdl = acpi_match_cpu_info(ci);
147 
148 	if (hdl == NULL) {
149 		aprint_normal(": failed to match processor\n");
150 		return;
151 	}
152 
153 	sc->sc_node = acpi_match_node(hdl);
154 
155 	if (acpicpu_once_attach() != 0) {
156 		aprint_normal(": failed to initialize\n");
157 		return;
158 	}
159 
160 	KASSERT(acpi_softc != NULL);
161 	KASSERT(acpicpu_sc != NULL);
162 	KASSERT(sc->sc_node != NULL);
163 
164 	id = sc->sc_ci->ci_acpiid;
165 
166 	if (acpicpu_sc[id] != NULL) {
167 		aprint_normal(": already attached\n");
168 		return;
169 	}
170 
171 	aprint_naive("\n");
172 	aprint_normal(": ACPI CPU\n");
173 
174 	rv = acpicpu_object(sc->sc_node->ad_handle, &sc->sc_object);
175 
176 	if (ACPI_FAILURE(rv))
177 		aprint_verbose_dev(self, "failed to obtain CPU object\n");
178 
179 	acpicpu_count++;
180 	acpicpu_sc[id] = sc;
181 
182 	sc->sc_cap = acpicpu_cap(sc);
183 	sc->sc_ncpus = acpi_md_ncpus();
184 	sc->sc_flags = acpicpu_md_flags();
185 
186 	KASSERT(acpicpu_count <= sc->sc_ncpus);
187 	KASSERT(sc->sc_node->ad_device == NULL);
188 
189 	sc->sc_node->ad_device = self;
190 	mutex_init(&sc->sc_mtx, MUTEX_DEFAULT, IPL_NONE);
191 
192 	acpicpu_cstate_attach(self);
193 	acpicpu_pstate_attach(self);
194 	acpicpu_tstate_attach(self);
195 
196 	acpicpu_debug_print(self);
197 	acpicpu_evcnt_attach(self);
198 
199 	(void)config_interrupts(self, acpicpu_start);
200 	(void)acpi_register_notify(sc->sc_node, acpicpu_notify);
201 	(void)pmf_device_register(self, acpicpu_suspend, acpicpu_resume);
202 }
203 
204 static int
205 acpicpu_detach(device_t self, int flags)
206 {
207 	struct acpicpu_softc *sc = device_private(self);
208 
209 	sc->sc_cold = true;
210 
211 	acpicpu_evcnt_detach(self);
212 	acpi_deregister_notify(sc->sc_node);
213 
214 	acpicpu_cstate_detach(self);
215 	acpicpu_pstate_detach(self);
216 	acpicpu_tstate_detach(self);
217 
218 	mutex_destroy(&sc->sc_mtx);
219 	sc->sc_node->ad_device = NULL;
220 
221 	acpicpu_count--;
222 	acpicpu_once_detach();
223 
224 	return 0;
225 }
226 
227 static int
228 acpicpu_once_attach(void)
229 {
230 	struct acpicpu_softc *sc;
231 	unsigned int i;
232 
233 	if (acpicpu_count != 0)
234 		return 0;
235 
236 	KASSERT(acpicpu_sc == NULL);
237 	KASSERT(acpicpu_log == NULL);
238 
239 	acpicpu_sc = kmem_zalloc(maxcpus * sizeof(*sc), KM_SLEEP);
240 
241 	for (i = 0; i < maxcpus; i++)
242 		acpicpu_sc[i] = NULL;
243 
244 	return 0;
245 }
246 
247 static int
248 acpicpu_once_detach(void)
249 {
250 	struct acpicpu_softc *sc;
251 
252 	if (acpicpu_count != 0)
253 		return EDEADLK;
254 
255 	cpufreq_deregister();
256 
257 	if (acpicpu_log != NULL)
258 		sysctl_teardown(&acpicpu_log);
259 
260 	if (acpicpu_sc != NULL)
261 		kmem_free(acpicpu_sc, maxcpus * sizeof(*sc));
262 
263 	return 0;
264 }
265 
266 static void
267 acpicpu_start(device_t self)
268 {
269 	struct acpicpu_softc *sc = device_private(self);
270 	static uint32_t count = 0;
271 	struct cpufreq cf;
272 	uint32_t i;
273 
274 	/*
275 	 * Run the state-specific initialization routines. These
276 	 * must run only once, after interrupts have been enabled,
277 	 * all CPUs are running, and all ACPI CPUs have attached.
278 	 */
279 	if (++count != acpicpu_count || acpicpu_count != sc->sc_ncpus) {
280 		sc->sc_cold = false;
281 		return;
282 	}
283 
284 	/*
285 	 * Set the last ACPI CPU as non-cold
286 	 * only after C-states are enabled.
287 	 */
288 	if ((sc->sc_flags & ACPICPU_FLAG_C) != 0)
289 		acpicpu_cstate_start(self);
290 
291 	sc->sc_cold = false;
292 
293 	if ((sc->sc_flags & ACPICPU_FLAG_P) != 0)
294 		acpicpu_pstate_start(self);
295 
296 	if ((sc->sc_flags & ACPICPU_FLAG_T) != 0)
297 		acpicpu_tstate_start(self);
298 
299 	acpicpu_sysctl(self);
300 	aprint_debug_dev(self, "ACPI CPUs started\n");
301 
302 	/*
303 	 * Register with cpufreq(9).
304 	 */
305 	if ((sc->sc_flags & ACPICPU_FLAG_P) != 0) {
306 
307 		(void)memset(&cf, 0, sizeof(struct cpufreq));
308 
309 		cf.cf_mp = false;
310 		cf.cf_cookie = NULL;
311 		cf.cf_get_freq = acpicpu_pstate_get;
312 		cf.cf_set_freq = acpicpu_pstate_set;
313 		cf.cf_state_count = sc->sc_pstate_count;
314 
315 		(void)strlcpy(cf.cf_name, "acpicpu", sizeof(cf.cf_name));
316 
317 		for (i = 0; i < sc->sc_pstate_count; i++) {
318 
319 			if (sc->sc_pstate[i].ps_freq == 0)
320 				continue;
321 
322 			cf.cf_state[i].cfs_freq = sc->sc_pstate[i].ps_freq;
323 			cf.cf_state[i].cfs_power = sc->sc_pstate[i].ps_power;
324 		}
325 
326 		if (cpufreq_register(&cf) != 0)
327 			aprint_error_dev(self, "failed to register cpufreq\n");
328 	}
329 }
330 
331 static void
332 acpicpu_sysctl(device_t self)
333 {
334 	const struct sysctlnode *node;
335 	int err;
336 
337 	KASSERT(acpicpu_log == NULL);
338 
339 	err = sysctl_createv(&acpicpu_log, 0, NULL, &node,
340 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "acpi", NULL,
341 	    NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL);
342 
343 	if (err != 0)
344 		goto fail;
345 
346 	err = sysctl_createv(&acpicpu_log, 0, &node, &node,
347 	    0, CTLTYPE_NODE, "cpu", SYSCTL_DESCR("ACPI CPU"),
348 	    NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL);
349 
350 	if (err != 0)
351 		goto fail;
352 
353 	err = sysctl_createv(&acpicpu_log, 0, &node, NULL,
354 	    CTLFLAG_READWRITE, CTLTYPE_BOOL, "dynamic",
355 	    SYSCTL_DESCR("Dynamic states"), NULL, 0,
356 	    &acpicpu_dynamic, 0, CTL_CREATE, CTL_EOL);
357 
358 	if (err != 0)
359 		goto fail;
360 
361 	err = sysctl_createv(&acpicpu_log, 0, &node, NULL,
362 	    CTLFLAG_READWRITE, CTLTYPE_BOOL, "passive",
363 	    SYSCTL_DESCR("Passive cooling"), NULL, 0,
364 	    &acpicpu_passive, 0, CTL_CREATE, CTL_EOL);
365 
366 	if (err != 0)
367 		goto fail;
368 
369 	return;
370 
371 fail:
372 	aprint_error_dev(self, "failed to initialize sysctl (err %d)\n", err);
373 }
374 
375 static ACPI_STATUS
376 acpicpu_object(ACPI_HANDLE hdl, struct acpicpu_object *ao)
377 {
378 	ACPI_OBJECT *obj;
379 	ACPI_BUFFER buf;
380 	ACPI_STATUS rv;
381 
382 	rv = acpi_eval_struct(hdl, NULL, &buf);
383 
384 	if (ACPI_FAILURE(rv))
385 		goto out;
386 
387 	obj = buf.Pointer;
388 
389 	if (obj->Type != ACPI_TYPE_PROCESSOR) {
390 		rv = AE_TYPE;
391 		goto out;
392 	}
393 
394 	if (obj->Processor.ProcId > (uint32_t)maxcpus) {
395 		rv = AE_LIMIT;
396 		goto out;
397 	}
398 
399 	KDASSERT((uint64_t)obj->Processor.PblkAddress < UINT32_MAX);
400 
401 	if (ao != NULL) {
402 		ao->ao_procid = obj->Processor.ProcId;
403 		ao->ao_pblklen = obj->Processor.PblkLength;
404 		ao->ao_pblkaddr = obj->Processor.PblkAddress;
405 	}
406 
407 out:
408 	if (buf.Pointer != NULL)
409 		ACPI_FREE(buf.Pointer);
410 
411 	return rv;
412 }
413 
414 static uint32_t
415 acpicpu_cap(struct acpicpu_softc *sc)
416 {
417 	uint32_t flags, cap = 0;
418 	ACPI_STATUS rv;
419 
420 	/*
421 	 * Query and set machine-dependent capabilities.
422 	 * Note that the Intel-specific _PDC method has
423 	 * already been evaluated. It was furthermore
424 	 * deprecated in the ACPI 3.0 in favor of _OSC.
425 	 */
426 	flags = acpi_md_pdc();
427 	rv = acpicpu_cap_osc(sc, flags, &cap);
428 
429 	if (ACPI_FAILURE(rv) && rv != AE_NOT_FOUND) {
430 
431 		aprint_error_dev(sc->sc_dev, "failed to evaluate "
432 		    "_OSC: %s\n", AcpiFormatException(rv));
433 	}
434 
435 	return (cap != 0) ? cap : flags;
436 }
437 
438 static ACPI_STATUS
439 acpicpu_cap_osc(struct acpicpu_softc *sc, uint32_t flags, uint32_t *val)
440 {
441 	ACPI_OBJECT_LIST arg;
442 	ACPI_OBJECT obj[4];
443 	ACPI_OBJECT *osc;
444 	ACPI_BUFFER buf;
445 	ACPI_STATUS rv;
446 	uint32_t cap[2];
447 	uint32_t *ptr;
448 	int i = 5;
449 
450 	static uint8_t intel_uuid[16] = {
451 		0x16, 0xA6, 0x77, 0x40, 0x0C, 0x29, 0xBE, 0x47,
452 		0x9E, 0xBD, 0xD8, 0x70, 0x58, 0x71, 0x39, 0x53
453 	};
454 
455 	cap[0] = ACPI_OSC_QUERY;
456 	cap[1] = flags;
457 
458 again:
459 	arg.Count = 4;
460 	arg.Pointer = obj;
461 
462 	obj[0].Type = ACPI_TYPE_BUFFER;
463 	obj[0].Buffer.Length = sizeof(intel_uuid);
464 	obj[0].Buffer.Pointer = intel_uuid;
465 
466 	obj[1].Type = ACPI_TYPE_INTEGER;
467 	obj[1].Integer.Value = ACPICPU_PDC_REVID;
468 
469 	obj[2].Type = ACPI_TYPE_INTEGER;
470 	obj[2].Integer.Value = __arraycount(cap);
471 
472 	obj[3].Type = ACPI_TYPE_BUFFER;
473 	obj[3].Buffer.Length = sizeof(cap);
474 	obj[3].Buffer.Pointer = (void *)cap;
475 
476 	buf.Pointer = NULL;
477 	buf.Length = ACPI_ALLOCATE_LOCAL_BUFFER;
478 
479 	rv = AcpiEvaluateObject(sc->sc_node->ad_handle, "_OSC", &arg, &buf);
480 
481 	if (ACPI_FAILURE(rv))
482 		goto out;
483 
484 	osc = buf.Pointer;
485 
486 	if (osc->Type != ACPI_TYPE_BUFFER) {
487 		rv = AE_TYPE;
488 		goto out;
489 	}
490 
491 	if (osc->Buffer.Length != sizeof(cap)) {
492 		rv = AE_BUFFER_OVERFLOW;
493 		goto out;
494 	}
495 
496 	ptr = (uint32_t *)osc->Buffer.Pointer;
497 
498 	if ((ptr[0] & ACPI_OSC_ERROR) != 0) {
499 		rv = AE_ERROR;
500 		goto out;
501 	}
502 
503 	if ((ptr[0] & (ACPI_OSC_ERROR_REV | ACPI_OSC_ERROR_UUID)) != 0) {
504 		rv = AE_BAD_PARAMETER;
505 		goto out;
506 	}
507 
508 	/*
509 	 * "It is strongly recommended that the OS evaluate
510 	 *  _OSC with the Query Support Flag set until _OSC
511 	 *  returns the Capabilities Masked bit clear, to
512 	 *  negotiate the set of features to be granted to
513 	 *  the OS for native support (ACPI 4.0, 6.2.10)."
514 	 */
515 	if ((ptr[0] & ACPI_OSC_ERROR_MASKED) != 0 && i >= 0) {
516 
517 		ACPI_FREE(buf.Pointer);
518 		i--;
519 
520 		goto again;
521 	}
522 
523 	if ((cap[0] & ACPI_OSC_QUERY) != 0) {
524 
525 		ACPI_FREE(buf.Pointer);
526 		cap[0] &= ~ACPI_OSC_QUERY;
527 
528 		goto again;
529 	}
530 
531 	/*
532 	 * It is permitted for _OSC to return all
533 	 * bits cleared, but this is specified to
534 	 * vary on per-device basis. Assume that
535 	 * everything rather than nothing will be
536 	 * supported in this case; we do not need
537 	 * the firmware to know the CPU features.
538 	 */
539 	*val = (ptr[1] != 0) ? ptr[1] : cap[1];
540 
541 out:
542 	if (buf.Pointer != NULL)
543 		ACPI_FREE(buf.Pointer);
544 
545 	return rv;
546 }
547 
548 static void
549 acpicpu_notify(ACPI_HANDLE hdl, uint32_t evt, void *aux)
550 {
551 	ACPI_OSD_EXEC_CALLBACK func;
552 	struct acpicpu_softc *sc;
553 	device_t self = aux;
554 
555 	sc = device_private(self);
556 
557 	if (sc->sc_cold != false)
558 		return;
559 
560 	if (acpicpu_dynamic != true)
561 		return;
562 
563 	switch (evt) {
564 
565 	case ACPICPU_C_NOTIFY:
566 
567 		if ((sc->sc_flags & ACPICPU_FLAG_C) == 0)
568 			return;
569 
570 		func = acpicpu_cstate_callback;
571 		break;
572 
573 	case ACPICPU_P_NOTIFY:
574 
575 		if ((sc->sc_flags & ACPICPU_FLAG_P) == 0)
576 			return;
577 
578 		func = acpicpu_pstate_callback;
579 		break;
580 
581 	case ACPICPU_T_NOTIFY:
582 
583 		if ((sc->sc_flags & ACPICPU_FLAG_T) == 0)
584 			return;
585 
586 		func = acpicpu_tstate_callback;
587 		break;
588 
589 	default:
590 		aprint_error_dev(sc->sc_dev,  "unknown notify: 0x%02X\n", evt);
591 		return;
592 	}
593 
594 	(void)AcpiOsExecute(OSL_NOTIFY_HANDLER, func, sc->sc_dev);
595 }
596 
597 static bool
598 acpicpu_suspend(device_t self, const pmf_qual_t *qual)
599 {
600 	struct acpicpu_softc *sc = device_private(self);
601 
602 	if ((sc->sc_flags & ACPICPU_FLAG_C) != 0)
603 		(void)acpicpu_cstate_suspend(self);
604 
605 	if ((sc->sc_flags & ACPICPU_FLAG_P) != 0)
606 		(void)acpicpu_pstate_suspend(self);
607 
608 	if ((sc->sc_flags & ACPICPU_FLAG_T) != 0)
609 		(void)acpicpu_tstate_suspend(self);
610 
611 	sc->sc_cold = true;
612 
613 	return true;
614 }
615 
616 static bool
617 acpicpu_resume(device_t self, const pmf_qual_t *qual)
618 {
619 	struct acpicpu_softc *sc = device_private(self);
620 	static const int handler = OSL_NOTIFY_HANDLER;
621 
622 	sc->sc_cold = false;
623 
624 	if ((sc->sc_flags & ACPICPU_FLAG_C) != 0)
625 		(void)AcpiOsExecute(handler, acpicpu_cstate_resume, self);
626 
627 	if ((sc->sc_flags & ACPICPU_FLAG_P) != 0)
628 		(void)AcpiOsExecute(handler, acpicpu_pstate_resume, self);
629 
630 	if ((sc->sc_flags & ACPICPU_FLAG_T) != 0)
631 		(void)AcpiOsExecute(handler, acpicpu_tstate_resume, self);
632 
633 	return true;
634 }
635 
636 static void
637 acpicpu_evcnt_attach(device_t self)
638 {
639 	struct acpicpu_softc *sc = device_private(self);
640 	struct acpicpu_cstate *cs;
641 	struct acpicpu_pstate *ps;
642 	struct acpicpu_tstate *ts;
643 	const char *str;
644 	uint32_t i;
645 
646 	for (i = 0; i < __arraycount(sc->sc_cstate); i++) {
647 
648 		cs = &sc->sc_cstate[i];
649 
650 		if (cs->cs_method == 0)
651 			continue;
652 
653 		str = "HALT";
654 
655 		if (cs->cs_method == ACPICPU_C_STATE_FFH)
656 			str = "MWAIT";
657 
658 		if (cs->cs_method == ACPICPU_C_STATE_SYSIO)
659 			str = "I/O";
660 
661 		(void)snprintf(cs->cs_name, sizeof(cs->cs_name),
662 		    "C%d (%s)", i, str);
663 
664 		evcnt_attach_dynamic(&cs->cs_evcnt, EVCNT_TYPE_MISC,
665 		    NULL, device_xname(sc->sc_dev), cs->cs_name);
666 	}
667 
668 	for (i = 0; i < sc->sc_pstate_count; i++) {
669 
670 		ps = &sc->sc_pstate[i];
671 
672 		if (ps->ps_freq == 0)
673 			continue;
674 
675 		(void)snprintf(ps->ps_name, sizeof(ps->ps_name),
676 		    "P%u (%u MHz)", i, ps->ps_freq);
677 
678 		evcnt_attach_dynamic(&ps->ps_evcnt, EVCNT_TYPE_MISC,
679 		    NULL, device_xname(sc->sc_dev), ps->ps_name);
680 	}
681 
682 	for (i = 0; i < sc->sc_tstate_count; i++) {
683 
684 		ts = &sc->sc_tstate[i];
685 
686 		if (ts->ts_percent == 0)
687 			continue;
688 
689 		(void)snprintf(ts->ts_name, sizeof(ts->ts_name),
690 		    "T%u (%u %%)", i, ts->ts_percent);
691 
692 		evcnt_attach_dynamic(&ts->ts_evcnt, EVCNT_TYPE_MISC,
693 		    NULL, device_xname(sc->sc_dev), ts->ts_name);
694 	}
695 }
696 
697 static void
698 acpicpu_evcnt_detach(device_t self)
699 {
700 	struct acpicpu_softc *sc = device_private(self);
701 	struct acpicpu_cstate *cs;
702 	struct acpicpu_pstate *ps;
703 	struct acpicpu_tstate *ts;
704 	uint32_t i;
705 
706 	for (i = 0; i < __arraycount(sc->sc_cstate); i++) {
707 
708 		cs = &sc->sc_cstate[i];
709 
710 		if (cs->cs_method != 0)
711 			evcnt_detach(&cs->cs_evcnt);
712 	}
713 
714 	for (i = 0; i < sc->sc_pstate_count; i++) {
715 
716 		ps = &sc->sc_pstate[i];
717 
718 		if (ps->ps_freq != 0)
719 			evcnt_detach(&ps->ps_evcnt);
720 	}
721 
722 	for (i = 0; i < sc->sc_tstate_count; i++) {
723 
724 		ts = &sc->sc_tstate[i];
725 
726 		if (ts->ts_percent != 0)
727 			evcnt_detach(&ts->ts_evcnt);
728 	}
729 }
730 
731 static void
732 acpicpu_debug_print(device_t self)
733 {
734 	struct acpicpu_softc *sc = device_private(self);
735 	struct cpu_info *ci = sc->sc_ci;
736 	struct acpicpu_cstate *cs;
737 	struct acpicpu_pstate *ps;
738 	struct acpicpu_tstate *ts;
739 	static bool once = false;
740 	struct acpicpu_dep *dep;
741 	uint32_t i, method;
742 
743 	if (once != true) {
744 
745 		for (i = 0; i < __arraycount(sc->sc_cstate); i++) {
746 
747 			cs = &sc->sc_cstate[i];
748 
749 			if (cs->cs_method == 0)
750 				continue;
751 
752 			aprint_verbose_dev(sc->sc_dev, "C%d: %3s, "
753 			    "lat %3u us, pow %5u mW%s\n", i,
754 			    acpicpu_debug_print_method_c(cs->cs_method),
755 			    cs->cs_latency, cs->cs_power,
756 			    (cs->cs_flags != 0) ? ", bus master check" : "");
757 		}
758 
759 		method = sc->sc_pstate_control.reg_spaceid;
760 
761 		for (i = 0; i < sc->sc_pstate_count; i++) {
762 
763 			ps = &sc->sc_pstate[i];
764 
765 			if (ps->ps_freq == 0)
766 				continue;
767 
768 			aprint_verbose_dev(sc->sc_dev, "P%d: %3s, "
769 			    "lat %3u us, pow %5u mW, %4u MHz%s\n", i,
770 			    acpicpu_debug_print_method_pt(method),
771 			    ps->ps_latency, ps->ps_power, ps->ps_freq,
772 			    (ps->ps_flags & ACPICPU_FLAG_P_TURBO) != 0 ?
773 			    ", turbo boost" : "");
774 		}
775 
776 		method = sc->sc_tstate_control.reg_spaceid;
777 
778 		for (i = 0; i < sc->sc_tstate_count; i++) {
779 
780 			ts = &sc->sc_tstate[i];
781 
782 			if (ts->ts_percent == 0)
783 				continue;
784 
785 			aprint_verbose_dev(sc->sc_dev, "T%u: %3s, "
786 			    "lat %3u us, pow %5u mW, %3u %%\n", i,
787 			    acpicpu_debug_print_method_pt(method),
788 			    ts->ts_latency, ts->ts_power, ts->ts_percent);
789 		}
790 
791 		once = true;
792 	}
793 
794 	aprint_debug_dev(sc->sc_dev, "id %u, lapic id %u, "
795 	    "cap 0x%04x, flags 0x%08x\n", ci->ci_acpiid,
796 	    (uint32_t)ci->ci_cpuid, sc->sc_cap, sc->sc_flags);
797 
798 	if ((sc->sc_flags & ACPICPU_FLAG_C_DEP) != 0) {
799 
800 		dep = &sc->sc_cstate_dep;
801 
802 		aprint_debug_dev(sc->sc_dev, "C-state coordination: "
803 		    "%u CPUs, domain %u, type %s\n", dep->dep_ncpus,
804 		    dep->dep_domain, acpicpu_debug_print_dep(dep->dep_type));
805 	}
806 
807 	if ((sc->sc_flags & ACPICPU_FLAG_P_DEP) != 0) {
808 
809 		dep = &sc->sc_pstate_dep;
810 
811 		aprint_debug_dev(sc->sc_dev, "P-state coordination: "
812 		    "%u CPUs, domain %u, type %s\n", dep->dep_ncpus,
813 		    dep->dep_domain, acpicpu_debug_print_dep(dep->dep_type));
814 	}
815 
816 	if ((sc->sc_flags & ACPICPU_FLAG_T_DEP) != 0) {
817 
818 		dep = &sc->sc_tstate_dep;
819 
820 		aprint_debug_dev(sc->sc_dev, "T-state coordination: "
821 		    "%u CPUs, domain %u, type %s\n", dep->dep_ncpus,
822 		    dep->dep_domain, acpicpu_debug_print_dep(dep->dep_type));
823 	}
824 }
825 
826 static const char *
827 acpicpu_debug_print_method_c(uint8_t val)
828 {
829 
830 	if (val == ACPICPU_C_STATE_FFH)
831 		return "FFH";
832 
833 	if (val == ACPICPU_C_STATE_HALT)
834 		return "HLT";
835 
836 	if (val == ACPICPU_C_STATE_SYSIO)
837 		return "I/O";
838 
839 	return "???";
840 }
841 
842 static const char *
843 acpicpu_debug_print_method_pt(uint8_t val)
844 {
845 
846 	if (val == ACPI_ADR_SPACE_SYSTEM_IO)
847 		return "I/O";
848 
849 	if (val == ACPI_ADR_SPACE_FIXED_HARDWARE)
850 		return "FFH";
851 
852 	return "???";
853 }
854 
855 static const char *
856 acpicpu_debug_print_dep(uint32_t val)
857 {
858 
859 	switch (val) {
860 
861 	case ACPICPU_DEP_SW_ALL:
862 		return "SW_ALL";
863 
864 	case ACPICPU_DEP_SW_ANY:
865 		return "SW_ANY";
866 
867 	case ACPICPU_DEP_HW_ALL:
868 		return "HW_ALL";
869 
870 	default:
871 		return "unknown";
872 	}
873 }
874 
875 MODULE(MODULE_CLASS_DRIVER, acpicpu, NULL);
876 
877 #ifdef _MODULE
878 #include "ioconf.c"
879 #endif
880 
881 static int
882 acpicpu_modcmd(modcmd_t cmd, void *aux)
883 {
884 	int rv = 0;
885 
886 	switch (cmd) {
887 
888 	case MODULE_CMD_INIT:
889 
890 #ifdef _MODULE
891 		rv = config_init_component(cfdriver_ioconf_acpicpu,
892 		    cfattach_ioconf_acpicpu, cfdata_ioconf_acpicpu);
893 #endif
894 		break;
895 
896 	case MODULE_CMD_FINI:
897 
898 #ifdef _MODULE
899 		rv = config_fini_component(cfdriver_ioconf_acpicpu,
900 		    cfattach_ioconf_acpicpu, cfdata_ioconf_acpicpu);
901 #endif
902 		break;
903 
904 	default:
905 		rv = ENOTTY;
906 	}
907 
908 	return rv;
909 }
910