acpi_cpu.c revision 1.38 1 /* $NetBSD: acpi_cpu.c,v 1.38 2011/03/19 12:57:30 jruoho Exp $ */
2
3 /*-
4 * Copyright (c) 2010, 2011 Jukka Ruohonen <jruohonen (at) 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.38 2011/03/19 12:57:30 jruoho 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
41 #include <dev/acpi/acpireg.h>
42 #include <dev/acpi/acpivar.h>
43 #include <dev/acpi/acpi_cpu.h>
44
45 #include <machine/acpi_machdep.h>
46 #include <machine/cpuvar.h>
47
48 #define _COMPONENT ACPI_BUS_COMPONENT
49 ACPI_MODULE_NAME ("acpi_cpu")
50
51 static int acpicpu_match(device_t, cfdata_t, void *);
52 static void acpicpu_attach(device_t, device_t, void *);
53 static int acpicpu_detach(device_t, int);
54 static int acpicpu_once_attach(void);
55 static int acpicpu_once_detach(void);
56 static void acpicpu_start(device_t);
57 static void acpicpu_sysctl(device_t);
58
59 static ACPI_STATUS acpicpu_object(ACPI_HANDLE, struct acpicpu_object *);
60 static int acpicpu_find(struct cpu_info *,
61 struct acpi_devnode **);
62 static uint32_t acpicpu_cap(struct acpicpu_softc *);
63 static ACPI_STATUS acpicpu_cap_pdc(struct acpicpu_softc *, uint32_t);
64 static ACPI_STATUS acpicpu_cap_osc(struct acpicpu_softc *,
65 uint32_t, uint32_t *);
66 static void acpicpu_notify(ACPI_HANDLE, uint32_t, void *);
67 static bool acpicpu_suspend(device_t, const pmf_qual_t *);
68 static bool acpicpu_resume(device_t, const pmf_qual_t *);
69 static void acpicpu_evcnt_attach(device_t);
70 static void acpicpu_evcnt_detach(device_t);
71 static void acpicpu_debug_print(device_t);
72 static const char *acpicpu_debug_print_method(uint8_t);
73 static const char *acpicpu_debug_print_dep(uint32_t);
74
75 static uint32_t acpicpu_count = 0;
76 struct acpicpu_softc **acpicpu_sc = NULL;
77 static struct sysctllog *acpicpu_log = NULL;
78 static bool acpicpu_dynamic = true;
79 static bool acpicpu_passive = true;
80
81 static const struct {
82 const char *manu;
83 const char *prod;
84 const char *vers;
85 } acpicpu_quirks[] = {
86 { "Supermicro", "PDSMi-LN4", "0123456789" },
87 };
88
89 static const char * const acpicpu_hid[] = {
90 "ACPI0007",
91 NULL
92 };
93
94 CFATTACH_DECL_NEW(acpicpu, sizeof(struct acpicpu_softc),
95 acpicpu_match, acpicpu_attach, acpicpu_detach, NULL);
96
97 static int
98 acpicpu_match(device_t parent, cfdata_t match, void *aux)
99 {
100 const char *manu, *prod, *vers;
101 struct cpu_info *ci;
102 size_t i;
103
104 if (acpi_softc == NULL)
105 return 0;
106
107 manu = pmf_get_platform("system-manufacturer");
108 prod = pmf_get_platform("system-product-name");
109 vers = pmf_get_platform("system-version");
110
111 if (manu != NULL && prod != NULL && vers != NULL) {
112
113 for (i = 0; i < __arraycount(acpicpu_quirks); i++) {
114
115 if (strcasecmp(acpicpu_quirks[i].manu, manu) == 0 &&
116 strcasecmp(acpicpu_quirks[i].prod, prod) == 0 &&
117 strcasecmp(acpicpu_quirks[i].vers, vers) == 0)
118 return 0;
119 }
120 }
121
122 ci = acpicpu_md_match(parent, match, aux);
123
124 if (ci == NULL)
125 return 0;
126
127 return acpicpu_find(ci, NULL);
128 }
129
130 static void
131 acpicpu_attach(device_t parent, device_t self, void *aux)
132 {
133 struct acpicpu_softc *sc = device_private(self);
134 struct cpu_info *ci;
135 cpuid_t id;
136 int rv;
137
138 ci = acpicpu_md_attach(parent, self, aux);
139
140 if (ci == NULL)
141 return;
142
143 sc->sc_ci = ci;
144 sc->sc_dev = self;
145 sc->sc_cold = true;
146 sc->sc_node = NULL;
147
148 rv = acpicpu_find(ci, &sc->sc_node);
149
150 if (rv == 0) {
151 aprint_normal(": failed to match processor\n");
152 return;
153 }
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
191 __cpu_simple_lock_init(&sc->sc_lock);
192 mutex_init(&sc->sc_mtx, MUTEX_DEFAULT, IPL_NONE);
193
194 acpicpu_cstate_attach(self);
195 acpicpu_pstate_attach(self);
196 acpicpu_tstate_attach(self);
197
198 acpicpu_debug_print(self);
199 acpicpu_evcnt_attach(self);
200
201 (void)config_interrupts(self, acpicpu_start);
202 (void)acpi_register_notify(sc->sc_node, acpicpu_notify);
203 (void)pmf_device_register(self, acpicpu_suspend, acpicpu_resume);
204 }
205
206 static int
207 acpicpu_detach(device_t self, int flags)
208 {
209 struct acpicpu_softc *sc = device_private(self);
210 int rv = 0;
211
212 sc->sc_cold = true;
213
214 acpicpu_evcnt_detach(self);
215 acpi_deregister_notify(sc->sc_node);
216
217 if ((sc->sc_flags & ACPICPU_FLAG_C) != 0)
218 rv = acpicpu_cstate_detach(self);
219
220 if (rv != 0)
221 return rv;
222
223 if ((sc->sc_flags & ACPICPU_FLAG_P) != 0)
224 rv = acpicpu_pstate_detach(self);
225
226 if (rv != 0)
227 return rv;
228
229 if ((sc->sc_flags & ACPICPU_FLAG_T) != 0)
230 rv = acpicpu_tstate_detach(self);
231
232 if (rv != 0)
233 return rv;
234
235 mutex_destroy(&sc->sc_mtx);
236
237 sc->sc_node->ad_device = NULL;
238
239 acpicpu_count--;
240 acpicpu_once_detach();
241
242 return 0;
243 }
244
245 static int
246 acpicpu_once_attach(void)
247 {
248 struct acpicpu_softc *sc;
249 unsigned int i;
250
251 if (acpicpu_count != 0)
252 return 0;
253
254 KASSERT(acpicpu_sc == NULL);
255 KASSERT(acpicpu_log == NULL);
256
257 acpicpu_sc = kmem_zalloc(maxcpus * sizeof(*sc), KM_SLEEP);
258
259 if (acpicpu_sc == NULL)
260 return ENOMEM;
261
262 for (i = 0; i < maxcpus; i++)
263 acpicpu_sc[i] = NULL;
264
265 return 0;
266 }
267
268 static int
269 acpicpu_once_detach(void)
270 {
271 struct acpicpu_softc *sc;
272
273 if (acpicpu_count != 0)
274 return EDEADLK;
275
276 if (acpicpu_log != NULL)
277 sysctl_teardown(&acpicpu_log);
278
279 if (acpicpu_sc != NULL)
280 kmem_free(acpicpu_sc, maxcpus * sizeof(*sc));
281
282 return 0;
283 }
284
285 static void
286 acpicpu_start(device_t self)
287 {
288 struct acpicpu_softc *sc = device_private(self);
289 static uint32_t count = 0;
290
291 /*
292 * Run the state-specific initialization routines. These
293 * must run only once, after interrupts have been enabled,
294 * all CPUs are running, and all ACPI CPUs have attached.
295 */
296 if (++count != acpicpu_count || acpicpu_count != sc->sc_ncpus) {
297 sc->sc_cold = false;
298 return;
299 }
300
301 /*
302 * Set the last ACPI CPU as non-cold
303 * only after C-states are enabled.
304 */
305 if ((sc->sc_flags & ACPICPU_FLAG_C) != 0)
306 acpicpu_cstate_start(self);
307
308 sc->sc_cold = false;
309
310 if ((sc->sc_flags & ACPICPU_FLAG_P) != 0)
311 acpicpu_pstate_start(self);
312
313 if ((sc->sc_flags & ACPICPU_FLAG_T) != 0)
314 acpicpu_tstate_start(self);
315
316 acpicpu_sysctl(self);
317 aprint_debug_dev(self, "ACPI CPUs started\n");
318 }
319
320 static void
321 acpicpu_sysctl(device_t self)
322 {
323 const struct sysctlnode *node;
324 int err;
325
326 KASSERT(acpicpu_log == NULL);
327
328 err = sysctl_createv(&acpicpu_log, 0, NULL, &node,
329 CTLFLAG_PERMANENT, CTLTYPE_NODE, "hw", NULL,
330 NULL, 0, NULL, 0, CTL_HW, CTL_EOL);
331
332 if (err != 0)
333 goto fail;
334
335 err = sysctl_createv(&acpicpu_log, 0, &node, &node,
336 CTLFLAG_PERMANENT, CTLTYPE_NODE, "acpi", NULL,
337 NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL);
338
339 if (err != 0)
340 goto fail;
341
342 err = sysctl_createv(&acpicpu_log, 0, &node, &node,
343 0, CTLTYPE_NODE, "cpu", SYSCTL_DESCR("ACPI CPU"),
344 NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL);
345
346 if (err != 0)
347 goto fail;
348
349 err = sysctl_createv(&acpicpu_log, 0, &node, NULL,
350 CTLFLAG_READWRITE, CTLTYPE_BOOL, "dynamic",
351 SYSCTL_DESCR("Dynamic states"), NULL, 0,
352 &acpicpu_dynamic, 0, CTL_CREATE, CTL_EOL);
353
354 if (err != 0)
355 goto fail;
356
357 err = sysctl_createv(&acpicpu_log, 0, &node, NULL,
358 CTLFLAG_READWRITE, CTLTYPE_BOOL, "passive",
359 SYSCTL_DESCR("Passive cooling"), NULL, 0,
360 &acpicpu_passive, 0, CTL_CREATE, CTL_EOL);
361
362 if (err != 0)
363 goto fail;
364
365 return;
366
367 fail:
368 aprint_error_dev(self, "failed to initialize sysctl (err %d)\n", err);
369 }
370
371 static ACPI_STATUS
372 acpicpu_object(ACPI_HANDLE hdl, struct acpicpu_object *ao)
373 {
374 ACPI_OBJECT *obj;
375 ACPI_BUFFER buf;
376 ACPI_STATUS rv;
377
378 rv = acpi_eval_struct(hdl, NULL, &buf);
379
380 if (ACPI_FAILURE(rv))
381 goto out;
382
383 obj = buf.Pointer;
384
385 if (obj->Type != ACPI_TYPE_PROCESSOR) {
386 rv = AE_TYPE;
387 goto out;
388 }
389
390 if (obj->Processor.ProcId > (uint32_t)maxcpus) {
391 rv = AE_LIMIT;
392 goto out;
393 }
394
395 KDASSERT((uint64_t)obj->Processor.PblkAddress < UINT32_MAX);
396
397 if (ao != NULL) {
398 ao->ao_procid = obj->Processor.ProcId;
399 ao->ao_pblklen = obj->Processor.PblkLength;
400 ao->ao_pblkaddr = obj->Processor.PblkAddress;
401 }
402
403 out:
404 if (buf.Pointer != NULL)
405 ACPI_FREE(buf.Pointer);
406
407 return rv;
408 }
409
410 static int
411 acpicpu_find(struct cpu_info *ci, struct acpi_devnode **ptr)
412 {
413 struct acpi_softc *sc = acpi_softc;
414 struct acpicpu_object ao;
415 struct acpi_devnode *ad;
416 ACPI_INTEGER val;
417 ACPI_STATUS rv;
418
419 if (sc == NULL || acpi_active == 0)
420 return 0;
421
422 /*
423 * CPUs are declared in the ACPI namespace
424 * either as a Processor() or as a Device().
425 * In both cases the MADT entries are used
426 * for the match (see ACPI 4.0, section 8.4).
427 */
428 SIMPLEQ_FOREACH(ad, &sc->ad_head, ad_list) {
429
430 if (ad->ad_type == ACPI_TYPE_PROCESSOR) {
431
432 rv = acpicpu_object(ad->ad_handle, &ao);
433
434 if (ACPI_SUCCESS(rv) && ci->ci_acpiid == ao.ao_procid)
435 goto out;
436 }
437
438 if (acpi_match_hid(ad->ad_devinfo, acpicpu_hid) != 0) {
439
440 rv = acpi_eval_integer(ad->ad_handle, "_UID", &val);
441
442 if (ACPI_SUCCESS(rv) && ci->ci_acpiid == val)
443 goto out;
444 }
445 }
446
447 return 0;
448
449 out:
450 if (ptr != NULL)
451 *ptr = ad;
452
453 return 10;
454 }
455
456 static uint32_t
457 acpicpu_cap(struct acpicpu_softc *sc)
458 {
459 uint32_t flags, cap = 0;
460 const char *str;
461 ACPI_STATUS rv;
462
463 /*
464 * Query and set machine-dependent capabilities.
465 * Note that the Intel-specific _PDC method was
466 * deprecated in the ACPI 3.0 in favor of _OSC.
467 */
468 flags = acpicpu_md_cap();
469 rv = acpicpu_cap_osc(sc, flags, &cap);
470
471 if (ACPI_FAILURE(rv) && rv != AE_NOT_FOUND) {
472 str = "_OSC";
473 goto fail;
474 }
475
476 rv = acpicpu_cap_pdc(sc, flags);
477
478 if (ACPI_FAILURE(rv) && rv != AE_NOT_FOUND) {
479 str = "_PDC";
480 goto fail;
481 }
482
483 if (cap == 0)
484 cap = flags;
485
486 return cap;
487
488 fail:
489 aprint_error_dev(sc->sc_dev, "failed to evaluate "
490 "%s: %s\n", str, AcpiFormatException(rv));
491
492 return 0;
493 }
494
495 static ACPI_STATUS
496 acpicpu_cap_pdc(struct acpicpu_softc *sc, uint32_t flags)
497 {
498 ACPI_OBJECT_LIST arg;
499 ACPI_OBJECT obj;
500 uint32_t cap[3];
501
502 arg.Count = 1;
503 arg.Pointer = &obj;
504
505 cap[0] = ACPICPU_PDC_REVID;
506 cap[1] = 1;
507 cap[2] = flags;
508
509 obj.Type = ACPI_TYPE_BUFFER;
510 obj.Buffer.Length = sizeof(cap);
511 obj.Buffer.Pointer = (void *)cap;
512
513 return AcpiEvaluateObject(sc->sc_node->ad_handle, "_PDC", &arg, NULL);
514 }
515
516 static ACPI_STATUS
517 acpicpu_cap_osc(struct acpicpu_softc *sc, uint32_t flags, uint32_t *val)
518 {
519 ACPI_OBJECT_LIST arg;
520 ACPI_OBJECT obj[4];
521 ACPI_OBJECT *osc;
522 ACPI_BUFFER buf;
523 ACPI_STATUS rv;
524 uint32_t cap[2];
525 uint32_t *ptr;
526 int i = 5;
527
528 static uint8_t intel_uuid[16] = {
529 0x16, 0xA6, 0x77, 0x40, 0x0C, 0x29, 0xBE, 0x47,
530 0x9E, 0xBD, 0xD8, 0x70, 0x58, 0x71, 0x39, 0x53
531 };
532
533 cap[0] = ACPI_OSC_QUERY;
534 cap[1] = flags;
535
536 again:
537 arg.Count = 4;
538 arg.Pointer = obj;
539
540 obj[0].Type = ACPI_TYPE_BUFFER;
541 obj[0].Buffer.Length = sizeof(intel_uuid);
542 obj[0].Buffer.Pointer = intel_uuid;
543
544 obj[1].Type = ACPI_TYPE_INTEGER;
545 obj[1].Integer.Value = ACPICPU_PDC_REVID;
546
547 obj[2].Type = ACPI_TYPE_INTEGER;
548 obj[2].Integer.Value = __arraycount(cap);
549
550 obj[3].Type = ACPI_TYPE_BUFFER;
551 obj[3].Buffer.Length = sizeof(cap);
552 obj[3].Buffer.Pointer = (void *)cap;
553
554 buf.Pointer = NULL;
555 buf.Length = ACPI_ALLOCATE_LOCAL_BUFFER;
556
557 rv = AcpiEvaluateObject(sc->sc_node->ad_handle, "_OSC", &arg, &buf);
558
559 if (ACPI_FAILURE(rv))
560 goto out;
561
562 osc = buf.Pointer;
563
564 if (osc->Type != ACPI_TYPE_BUFFER) {
565 rv = AE_TYPE;
566 goto out;
567 }
568
569 if (osc->Buffer.Length != sizeof(cap)) {
570 rv = AE_BUFFER_OVERFLOW;
571 goto out;
572 }
573
574 ptr = (uint32_t *)osc->Buffer.Pointer;
575
576 if ((ptr[0] & ACPI_OSC_ERROR) != 0) {
577 rv = AE_ERROR;
578 goto out;
579 }
580
581 if ((ptr[0] & (ACPI_OSC_ERROR_REV | ACPI_OSC_ERROR_UUID)) != 0) {
582 rv = AE_BAD_PARAMETER;
583 goto out;
584 }
585
586 /*
587 * "It is strongly recommended that the OS evaluate
588 * _OSC with the Query Support Flag set until _OSC
589 * returns the Capabilities Masked bit clear, to
590 * negotiate the set of features to be granted to
591 * the OS for native support (ACPI 4.0, 6.2.10)."
592 */
593 if ((ptr[0] & ACPI_OSC_ERROR_MASKED) != 0 && i >= 0) {
594
595 ACPI_FREE(buf.Pointer);
596 i--;
597
598 goto again;
599 }
600
601 if ((cap[0] & ACPI_OSC_QUERY) != 0) {
602
603 ACPI_FREE(buf.Pointer);
604 cap[0] &= ~ACPI_OSC_QUERY;
605
606 goto again;
607 }
608
609 /*
610 * It is permitted for _OSC to return all
611 * bits cleared, but this is specified to
612 * vary on per-device basis. Assume that
613 * everything rather than nothing will be
614 * supported in this case; we do not need
615 * the firmware to know the CPU features.
616 */
617 *val = (ptr[1] != 0) ? ptr[1] : cap[1];
618
619 out:
620 if (buf.Pointer != NULL)
621 ACPI_FREE(buf.Pointer);
622
623 return rv;
624 }
625
626 static void
627 acpicpu_notify(ACPI_HANDLE hdl, uint32_t evt, void *aux)
628 {
629 ACPI_OSD_EXEC_CALLBACK func;
630 struct acpicpu_softc *sc;
631 device_t self = aux;
632
633 sc = device_private(self);
634
635 if (sc->sc_cold != false)
636 return;
637
638 if (acpicpu_dynamic != true)
639 return;
640
641 switch (evt) {
642
643 case ACPICPU_C_NOTIFY:
644
645 if ((sc->sc_flags & ACPICPU_FLAG_C) == 0)
646 return;
647
648 func = acpicpu_cstate_callback;
649 break;
650
651 case ACPICPU_P_NOTIFY:
652
653 if ((sc->sc_flags & ACPICPU_FLAG_P) == 0)
654 return;
655
656 func = acpicpu_pstate_callback;
657 break;
658
659 case ACPICPU_T_NOTIFY:
660
661 if ((sc->sc_flags & ACPICPU_FLAG_T) == 0)
662 return;
663
664 func = acpicpu_tstate_callback;
665 break;
666
667 default:
668 aprint_error_dev(sc->sc_dev, "unknown notify: 0x%02X\n", evt);
669 return;
670 }
671
672 (void)AcpiOsExecute(OSL_NOTIFY_HANDLER, func, sc->sc_dev);
673 }
674
675 static bool
676 acpicpu_suspend(device_t self, const pmf_qual_t *qual)
677 {
678 struct acpicpu_softc *sc = device_private(self);
679
680 if ((sc->sc_flags & ACPICPU_FLAG_C) != 0)
681 (void)acpicpu_cstate_suspend(self);
682
683 if ((sc->sc_flags & ACPICPU_FLAG_P) != 0)
684 (void)acpicpu_pstate_suspend(self);
685
686 if ((sc->sc_flags & ACPICPU_FLAG_T) != 0)
687 (void)acpicpu_tstate_suspend(self);
688
689 sc->sc_cold = true;
690
691 return true;
692 }
693
694 static bool
695 acpicpu_resume(device_t self, const pmf_qual_t *qual)
696 {
697 struct acpicpu_softc *sc = device_private(self);
698 static const int handler = OSL_NOTIFY_HANDLER;
699
700 sc->sc_cold = false;
701
702 if ((sc->sc_flags & ACPICPU_FLAG_C) != 0)
703 (void)AcpiOsExecute(handler, acpicpu_cstate_resume, self);
704
705 if ((sc->sc_flags & ACPICPU_FLAG_P) != 0)
706 (void)AcpiOsExecute(handler, acpicpu_pstate_resume, self);
707
708 if ((sc->sc_flags & ACPICPU_FLAG_T) != 0)
709 (void)AcpiOsExecute(handler, acpicpu_tstate_resume, self);
710
711 return true;
712 }
713
714 static void
715 acpicpu_evcnt_attach(device_t self)
716 {
717 struct acpicpu_softc *sc = device_private(self);
718 struct acpicpu_cstate *cs;
719 struct acpicpu_pstate *ps;
720 struct acpicpu_tstate *ts;
721 const char *str;
722 uint32_t i;
723
724 for (i = 0; i < __arraycount(sc->sc_cstate); i++) {
725
726 cs = &sc->sc_cstate[i];
727
728 if (cs->cs_method == 0)
729 continue;
730
731 str = "HALT";
732
733 if (cs->cs_method == ACPICPU_C_STATE_FFH)
734 str = "MWAIT";
735
736 if (cs->cs_method == ACPICPU_C_STATE_SYSIO)
737 str = "I/O";
738
739 (void)snprintf(cs->cs_name, sizeof(cs->cs_name),
740 "C%d (%s)", i, str);
741
742 evcnt_attach_dynamic(&cs->cs_evcnt, EVCNT_TYPE_MISC,
743 NULL, device_xname(sc->sc_dev), cs->cs_name);
744 }
745
746 for (i = 0; i < sc->sc_pstate_count; i++) {
747
748 ps = &sc->sc_pstate[i];
749
750 if (ps->ps_freq == 0)
751 continue;
752
753 (void)snprintf(ps->ps_name, sizeof(ps->ps_name),
754 "P%u (%u MHz)", i, ps->ps_freq);
755
756 evcnt_attach_dynamic(&ps->ps_evcnt, EVCNT_TYPE_MISC,
757 NULL, device_xname(sc->sc_dev), ps->ps_name);
758 }
759
760 for (i = 0; i < sc->sc_tstate_count; i++) {
761
762 ts = &sc->sc_tstate[i];
763
764 if (ts->ts_percent == 0)
765 continue;
766
767 (void)snprintf(ts->ts_name, sizeof(ts->ts_name),
768 "T%u (%u %%)", i, ts->ts_percent);
769
770 evcnt_attach_dynamic(&ts->ts_evcnt, EVCNT_TYPE_MISC,
771 NULL, device_xname(sc->sc_dev), ts->ts_name);
772 }
773 }
774
775 static void
776 acpicpu_evcnt_detach(device_t self)
777 {
778 struct acpicpu_softc *sc = device_private(self);
779 struct acpicpu_cstate *cs;
780 struct acpicpu_pstate *ps;
781 struct acpicpu_tstate *ts;
782 uint32_t i;
783
784 for (i = 0; i < __arraycount(sc->sc_cstate); i++) {
785
786 cs = &sc->sc_cstate[i];
787
788 if (cs->cs_method != 0)
789 evcnt_detach(&cs->cs_evcnt);
790 }
791
792 for (i = 0; i < sc->sc_pstate_count; i++) {
793
794 ps = &sc->sc_pstate[i];
795
796 if (ps->ps_freq != 0)
797 evcnt_detach(&ps->ps_evcnt);
798 }
799
800 for (i = 0; i < sc->sc_tstate_count; i++) {
801
802 ts = &sc->sc_tstate[i];
803
804 if (ts->ts_percent != 0)
805 evcnt_detach(&ts->ts_evcnt);
806 }
807 }
808
809 static void
810 acpicpu_debug_print(device_t self)
811 {
812 struct acpicpu_softc *sc = device_private(self);
813 struct cpu_info *ci = sc->sc_ci;
814 struct acpicpu_cstate *cs;
815 struct acpicpu_pstate *ps;
816 struct acpicpu_tstate *ts;
817 static bool once = false;
818 struct acpicpu_dep *dep;
819 uint32_t i, method;
820
821 if (once != true) {
822
823 for (i = 0; i < __arraycount(sc->sc_cstate); i++) {
824
825 cs = &sc->sc_cstate[i];
826
827 if (cs->cs_method == 0)
828 continue;
829
830 aprint_verbose_dev(sc->sc_dev, "C%d: %3s, "
831 "lat %3u us, pow %5u mW%s\n", i,
832 acpicpu_debug_print_method(cs->cs_method),
833 cs->cs_latency, cs->cs_power,
834 (cs->cs_flags != 0) ? ", bus master check" : "");
835 }
836
837 method = sc->sc_pstate_control.reg_spaceid;
838
839 for (i = 0; i < sc->sc_pstate_count; i++) {
840
841 ps = &sc->sc_pstate[i];
842
843 if (ps->ps_freq == 0)
844 continue;
845
846 aprint_verbose_dev(sc->sc_dev, "P%d: %3s, "
847 "lat %3u us, pow %5u mW, %4u MHz%s\n", i,
848 acpicpu_debug_print_method(method),
849 ps->ps_latency, ps->ps_power, ps->ps_freq,
850 (ps->ps_flags & ACPICPU_FLAG_P_TURBO) != 0 ?
851 ", turbo boost" : "");
852 }
853
854 method = sc->sc_tstate_control.reg_spaceid;
855
856 for (i = 0; i < sc->sc_tstate_count; i++) {
857
858 ts = &sc->sc_tstate[i];
859
860 if (ts->ts_percent == 0)
861 continue;
862
863 aprint_verbose_dev(sc->sc_dev, "T%u: %3s, "
864 "lat %3u us, pow %5u mW, %3u %%\n", i,
865 acpicpu_debug_print_method(method),
866 ts->ts_latency, ts->ts_power, ts->ts_percent);
867 }
868
869 once = true;
870 }
871
872 aprint_debug_dev(sc->sc_dev, "id %u, lapic id %u, "
873 "cap 0x%04x, flags 0x%08x\n", ci->ci_acpiid,
874 (uint32_t)ci->ci_cpuid, sc->sc_cap, sc->sc_flags);
875
876 if ((sc->sc_flags & ACPICPU_FLAG_C_DEP) != 0) {
877
878 dep = &sc->sc_cstate_dep;
879
880 aprint_debug_dev(sc->sc_dev, "C-state coordination: "
881 "%u CPUs, domain %u, type %s\n", dep->dep_ncpus,
882 dep->dep_domain, acpicpu_debug_print_dep(dep->dep_type));
883 }
884
885 if ((sc->sc_flags & ACPICPU_FLAG_P_DEP) != 0) {
886
887 dep = &sc->sc_pstate_dep;
888
889 aprint_debug_dev(sc->sc_dev, "P-state coordination: "
890 "%u CPUs, domain %u, type %s\n", dep->dep_ncpus,
891 dep->dep_domain, acpicpu_debug_print_dep(dep->dep_type));
892 }
893
894 if ((sc->sc_flags & ACPICPU_FLAG_T_DEP) != 0) {
895
896 dep = &sc->sc_tstate_dep;
897
898 aprint_debug_dev(sc->sc_dev, "T-state coordination: "
899 "%u CPUs, domain %u, type %s\n", dep->dep_ncpus,
900 dep->dep_domain, acpicpu_debug_print_dep(dep->dep_type));
901 }
902 }
903
904 static const char *
905 acpicpu_debug_print_method(uint8_t val)
906 {
907
908 switch (val) {
909
910 case ACPICPU_C_STATE_HALT:
911 return "HLT";
912
913 case ACPICPU_C_STATE_FFH:
914 case ACPI_ADR_SPACE_FIXED_HARDWARE:
915 return "FFH";
916
917 case ACPICPU_C_STATE_SYSIO: /* ACPI_ADR_SPACE_SYSTEM_IO */
918 return "I/O";
919
920 default:
921 return "???";
922 }
923 }
924
925 static const char *
926 acpicpu_debug_print_dep(uint32_t val)
927 {
928
929 switch (val) {
930
931 case ACPICPU_DEP_SW_ALL:
932 return "SW_ALL";
933
934 case ACPICPU_DEP_SW_ANY:
935 return "SW_ANY";
936
937 case ACPICPU_DEP_HW_ALL:
938 return "HW_ALL";
939
940 default:
941 return "unknown";
942 }
943 }
944
945 MODULE(MODULE_CLASS_DRIVER, acpicpu, NULL);
946
947 #ifdef _MODULE
948 #include "ioconf.c"
949 #endif
950
951 static int
952 acpicpu_modcmd(modcmd_t cmd, void *aux)
953 {
954 int rv = 0;
955
956 switch (cmd) {
957
958 case MODULE_CMD_INIT:
959
960 #ifdef _MODULE
961 rv = config_init_component(cfdriver_ioconf_acpicpu,
962 cfattach_ioconf_acpicpu, cfdata_ioconf_acpicpu);
963 #endif
964 break;
965
966 case MODULE_CMD_FINI:
967
968 #ifdef _MODULE
969 rv = config_fini_component(cfdriver_ioconf_acpicpu,
970 cfattach_ioconf_acpicpu, cfdata_ioconf_acpicpu);
971 #endif
972 break;
973
974 default:
975 rv = ENOTTY;
976 }
977
978 return rv;
979 }
980