acpi_cpu.c revision 1.24 1 /* $NetBSD: acpi_cpu.c,v 1.24 2010/12/30 12:05:02 jruoho Exp $ */
2
3 /*-
4 * Copyright (c) 2010 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.24 2010/12/30 12:05:02 jruoho Exp $");
31
32 #include <sys/param.h>
33 #include <sys/cpu.h>
34 #include <sys/kernel.h>
35 #include <sys/kmem.h>
36 #include <sys/module.h>
37 #include <sys/mutex.h>
38 #include <sys/once.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
47 #define _COMPONENT ACPI_BUS_COMPONENT
48 ACPI_MODULE_NAME ("acpi_cpu")
49
50 static int acpicpu_match(device_t, cfdata_t, void *);
51 static void acpicpu_attach(device_t, device_t, void *);
52 static int acpicpu_detach(device_t, int);
53 static int acpicpu_once_attach(void);
54 static int acpicpu_once_detach(void);
55 static void acpicpu_prestart(device_t);
56 static void acpicpu_start(device_t);
57 static void acpicpu_sysctl(device_t);
58
59 static int acpicpu_object(ACPI_HANDLE, struct acpicpu_object *);
60 static cpuid_t acpicpu_id(uint32_t);
61 static uint32_t acpicpu_cap(struct acpicpu_softc *);
62 static ACPI_STATUS acpicpu_cap_pdc(struct acpicpu_softc *, uint32_t);
63 static ACPI_STATUS acpicpu_cap_osc(struct acpicpu_softc *,
64 uint32_t, uint32_t *);
65 static void acpicpu_notify(ACPI_HANDLE, uint32_t, void *);
66 static bool acpicpu_suspend(device_t, const pmf_qual_t *);
67 static bool acpicpu_resume(device_t, const pmf_qual_t *);
68
69 struct acpicpu_softc **acpicpu_sc = NULL;
70 static struct sysctllog *acpicpu_log = NULL;
71 static bool acpicpu_dynamic = true;
72 static bool acpicpu_passive = true;
73
74 static const char * const acpicpu_hid[] = {
75 "ACPI0007",
76 NULL
77 };
78
79 CFATTACH_DECL_NEW(acpicpu, sizeof(struct acpicpu_softc),
80 acpicpu_match, acpicpu_attach, acpicpu_detach, NULL);
81
82 static int
83 acpicpu_match(device_t parent, cfdata_t match, void *aux)
84 {
85 struct acpi_attach_args *aa = aux;
86 struct acpicpu_object ao;
87 int rv;
88
89 if (aa->aa_node->ad_type != ACPI_TYPE_PROCESSOR)
90 return 0;
91
92 if (acpi_match_hid(aa->aa_node->ad_devinfo, acpicpu_hid) != 0)
93 return 1;
94
95 rv = acpicpu_object(aa->aa_node->ad_handle, &ao);
96
97 if (rv != 0 || acpicpu_id(ao.ao_procid) == 0xFFFFFF)
98 return 0;
99
100 return 1;
101 }
102
103 static void
104 acpicpu_attach(device_t parent, device_t self, void *aux)
105 {
106 struct acpicpu_softc *sc = device_private(self);
107 struct acpi_attach_args *aa = aux;
108 static ONCE_DECL(once_attach);
109 int rv;
110
111 rv = acpicpu_object(aa->aa_node->ad_handle, &sc->sc_object);
112
113 if (rv != 0)
114 return;
115
116 rv = RUN_ONCE(&once_attach, acpicpu_once_attach);
117
118 if (rv != 0)
119 return;
120
121 sc->sc_dev = self;
122 sc->sc_cold = true;
123 sc->sc_node = aa->aa_node;
124 sc->sc_cpuid = acpicpu_id(sc->sc_object.ao_procid);
125
126 if (sc->sc_cpuid == 0xFFFFFF) {
127 aprint_error(": invalid CPU ID\n");
128 return;
129 }
130
131 if (acpicpu_sc[sc->sc_cpuid] != NULL) {
132 aprint_error(": already attached\n");
133 return;
134 }
135
136 aprint_naive("\n");
137 aprint_normal(": ACPI CPU\n");
138
139 acpicpu_sc[sc->sc_cpuid] = sc;
140
141 sc->sc_cap = acpicpu_cap(sc);
142 sc->sc_flags |= acpicpu_md_quirks();
143
144 mutex_init(&sc->sc_mtx, MUTEX_DEFAULT, IPL_NONE);
145
146 acpicpu_cstate_attach(self);
147 acpicpu_pstate_attach(self);
148 acpicpu_tstate_attach(self);
149
150 (void)config_defer(self, acpicpu_prestart);
151 (void)acpi_register_notify(sc->sc_node, acpicpu_notify);
152 (void)pmf_device_register(self, acpicpu_suspend, acpicpu_resume);
153 }
154
155 static int
156 acpicpu_detach(device_t self, int flags)
157 {
158 struct acpicpu_softc *sc = device_private(self);
159 static ONCE_DECL(once_detach);
160 int rv = 0;
161
162 sc->sc_cold = true;
163 acpi_deregister_notify(sc->sc_node);
164
165 if ((sc->sc_flags & ACPICPU_FLAG_C) != 0)
166 rv = acpicpu_cstate_detach(self);
167
168 if (rv != 0)
169 return rv;
170
171 if ((sc->sc_flags & ACPICPU_FLAG_P) != 0)
172 rv = acpicpu_pstate_detach(self);
173
174 if (rv != 0)
175 return rv;
176
177 if ((sc->sc_flags & ACPICPU_FLAG_T) != 0)
178 rv = acpicpu_tstate_detach(self);
179
180 if (rv != 0)
181 return rv;
182
183 rv = RUN_ONCE(&once_detach, acpicpu_once_detach);
184
185 if (rv != 0)
186 return rv;
187
188 mutex_destroy(&sc->sc_mtx);
189
190 return 0;
191 }
192
193 static int
194 acpicpu_once_attach(void)
195 {
196 struct acpicpu_softc *sc;
197 unsigned int i;
198
199 acpicpu_sc = kmem_zalloc(maxcpus * sizeof(*sc), KM_SLEEP);
200
201 if (acpicpu_sc == NULL)
202 return ENOMEM;
203
204 for (i = 0; i < maxcpus; i++)
205 acpicpu_sc[i] = NULL;
206
207 return 0;
208 }
209
210 static int
211 acpicpu_once_detach(void)
212 {
213 struct acpicpu_softc *sc;
214
215 if (acpicpu_sc != NULL)
216 kmem_free(acpicpu_sc, maxcpus * sizeof(*sc));
217
218 if (acpicpu_log != NULL)
219 sysctl_teardown(&acpicpu_log);
220
221 return 0;
222 }
223
224 static void
225 acpicpu_prestart(device_t self)
226 {
227 struct acpicpu_softc *sc = device_private(self);
228 static bool once = false;
229
230 if (once != false) {
231 sc->sc_cold = false;
232 return;
233 }
234
235 once = true;
236
237 (void)config_interrupts(self, acpicpu_start);
238 }
239
240 static void
241 acpicpu_start(device_t self)
242 {
243 struct acpicpu_softc *sc = device_private(self);
244
245 /*
246 * Run the state-specific initialization
247 * routines. These should be called only
248 * once, after interrupts are enabled and
249 * all ACPI CPUs have attached.
250 */
251 if ((sc->sc_flags & ACPICPU_FLAG_C) != 0)
252 acpicpu_cstate_start(self);
253
254 if ((sc->sc_flags & ACPICPU_FLAG_P) != 0)
255 acpicpu_pstate_start(self);
256
257 if ((sc->sc_flags & ACPICPU_FLAG_T) != 0)
258 acpicpu_tstate_start(self);
259
260 acpicpu_sysctl(self);
261
262 aprint_debug_dev(sc->sc_dev, "ACPI CPUs started (cap "
263 "0x%02x, flags 0x%06x)\n", sc->sc_cap, sc->sc_flags);
264
265 sc->sc_cold = false;
266 }
267
268 static void
269 acpicpu_sysctl(device_t self)
270 {
271 const struct sysctlnode *node;
272 int err;
273
274 err = sysctl_createv(&acpicpu_log, 0, NULL, &node,
275 CTLFLAG_PERMANENT, CTLTYPE_NODE, "hw", NULL,
276 NULL, 0, NULL, 0, CTL_HW, CTL_EOL);
277
278 if (err != 0)
279 goto fail;
280
281 err = sysctl_createv(&acpicpu_log, 0, &node, &node,
282 CTLFLAG_PERMANENT, CTLTYPE_NODE, "acpi", NULL,
283 NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL);
284
285 if (err != 0)
286 goto fail;
287
288 err = sysctl_createv(&acpicpu_log, 0, &node, &node,
289 0, CTLTYPE_NODE, "cpu", SYSCTL_DESCR("ACPI CPU"),
290 NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL);
291
292 if (err != 0)
293 goto fail;
294
295 err = sysctl_createv(&acpicpu_log, 0, &node, NULL,
296 CTLFLAG_READWRITE, CTLTYPE_BOOL, "dynamic",
297 SYSCTL_DESCR("Dynamic states"), NULL, 0,
298 &acpicpu_dynamic, 0, CTL_CREATE, CTL_EOL);
299
300 if (err != 0)
301 goto fail;
302
303 err = sysctl_createv(&acpicpu_log, 0, &node, NULL,
304 CTLFLAG_READWRITE, CTLTYPE_BOOL, "passive",
305 SYSCTL_DESCR("Passive cooling"), NULL, 0,
306 &acpicpu_passive, 0, CTL_CREATE, CTL_EOL);
307
308 if (err != 0)
309 goto fail;
310
311 return;
312
313 fail:
314 aprint_error_dev(self, "failed to initialize sysctl (err %d)\n", err);
315 }
316
317 static int
318 acpicpu_object(ACPI_HANDLE hdl, struct acpicpu_object *ao)
319 {
320 ACPI_OBJECT *obj;
321 ACPI_BUFFER buf;
322 ACPI_STATUS rv;
323
324 rv = acpi_eval_struct(hdl, NULL, &buf);
325
326 if (ACPI_FAILURE(rv))
327 return 1;
328
329 obj = buf.Pointer;
330
331 if (obj->Type != ACPI_TYPE_PROCESSOR) {
332 rv = AE_TYPE;
333 goto out;
334 }
335
336 if (obj->Processor.ProcId > (uint32_t)maxcpus) {
337 rv = AE_LIMIT;
338 goto out;
339 }
340
341 KDASSERT((uint64_t)obj->Processor.PblkAddress < UINT32_MAX);
342
343 if (ao != NULL) {
344 ao->ao_procid = obj->Processor.ProcId;
345 ao->ao_pblklen = obj->Processor.PblkLength;
346 ao->ao_pblkaddr = obj->Processor.PblkAddress;
347 }
348
349 out:
350 if (buf.Pointer != NULL)
351 ACPI_FREE(buf.Pointer);
352
353 return ACPI_FAILURE(rv) ? 1 : 0;
354 }
355
356 static cpuid_t
357 acpicpu_id(uint32_t id)
358 {
359 CPU_INFO_ITERATOR cii;
360 struct cpu_info *ci;
361
362 for (CPU_INFO_FOREACH(cii, ci)) {
363
364 if (id == ci->ci_acpiid)
365 return id;
366 }
367
368 return 0xFFFFFF;
369 }
370
371 static uint32_t
372 acpicpu_cap(struct acpicpu_softc *sc)
373 {
374 uint32_t flags, cap = 0;
375 const char *str;
376 ACPI_STATUS rv;
377
378 /*
379 * Query and set machine-dependent capabilities.
380 * Note that the Intel-specific _PDC method was
381 * deprecated in the ACPI 3.0 in favor of _OSC.
382 */
383 flags = acpicpu_md_cap();
384 rv = acpicpu_cap_osc(sc, flags, &cap);
385
386 if (ACPI_FAILURE(rv) && rv != AE_NOT_FOUND) {
387 str = "_OSC";
388 goto fail;
389 }
390
391 rv = acpicpu_cap_pdc(sc, flags);
392
393 if (ACPI_FAILURE(rv) && rv != AE_NOT_FOUND) {
394 str = "_PDC";
395 goto fail;
396 }
397
398 if (cap == 0)
399 cap = flags;
400
401 return cap;
402
403 fail:
404 aprint_error_dev(sc->sc_dev, "failed to evaluate "
405 "%s: %s\n", str, AcpiFormatException(rv));
406
407 return 0;
408 }
409
410 static ACPI_STATUS
411 acpicpu_cap_pdc(struct acpicpu_softc *sc, uint32_t flags)
412 {
413 ACPI_OBJECT_LIST arg;
414 ACPI_OBJECT obj;
415 uint32_t cap[3];
416
417 arg.Count = 1;
418 arg.Pointer = &obj;
419
420 cap[0] = ACPICPU_PDC_REVID;
421 cap[1] = 1;
422 cap[2] = flags;
423
424 obj.Type = ACPI_TYPE_BUFFER;
425 obj.Buffer.Length = sizeof(cap);
426 obj.Buffer.Pointer = (void *)cap;
427
428 return AcpiEvaluateObject(sc->sc_node->ad_handle, "_PDC", &arg, NULL);
429 }
430
431 static ACPI_STATUS
432 acpicpu_cap_osc(struct acpicpu_softc *sc, uint32_t flags, uint32_t *val)
433 {
434 ACPI_OBJECT_LIST arg;
435 ACPI_OBJECT obj[4];
436 ACPI_OBJECT *osc;
437 ACPI_BUFFER buf;
438 ACPI_STATUS rv;
439 uint32_t cap[2];
440 uint32_t *ptr;
441 int i = 5;
442
443 static uint8_t intel_uuid[16] = {
444 0x16, 0xA6, 0x77, 0x40, 0x0C, 0x29, 0xBE, 0x47,
445 0x9E, 0xBD, 0xD8, 0x70, 0x58, 0x71, 0x39, 0x53
446 };
447
448 cap[0] = ACPI_OSC_QUERY;
449 cap[1] = flags;
450
451 again:
452 arg.Count = 4;
453 arg.Pointer = obj;
454
455 obj[0].Type = ACPI_TYPE_BUFFER;
456 obj[0].Buffer.Length = sizeof(intel_uuid);
457 obj[0].Buffer.Pointer = intel_uuid;
458
459 obj[1].Type = ACPI_TYPE_INTEGER;
460 obj[1].Integer.Value = ACPICPU_PDC_REVID;
461
462 obj[2].Type = ACPI_TYPE_INTEGER;
463 obj[2].Integer.Value = __arraycount(cap);
464
465 obj[3].Type = ACPI_TYPE_BUFFER;
466 obj[3].Buffer.Length = sizeof(cap);
467 obj[3].Buffer.Pointer = (void *)cap;
468
469 buf.Pointer = NULL;
470 buf.Length = ACPI_ALLOCATE_LOCAL_BUFFER;
471
472 rv = AcpiEvaluateObject(sc->sc_node->ad_handle, "_OSC", &arg, &buf);
473
474 if (ACPI_FAILURE(rv))
475 goto out;
476
477 osc = buf.Pointer;
478
479 if (osc->Type != ACPI_TYPE_BUFFER) {
480 rv = AE_TYPE;
481 goto out;
482 }
483
484 if (osc->Buffer.Length != sizeof(cap)) {
485 rv = AE_BUFFER_OVERFLOW;
486 goto out;
487 }
488
489 ptr = (uint32_t *)osc->Buffer.Pointer;
490
491 if ((ptr[0] & ACPI_OSC_ERROR) != 0) {
492 rv = AE_ERROR;
493 goto out;
494 }
495
496 if ((ptr[0] & (ACPI_OSC_ERROR_REV | ACPI_OSC_ERROR_UUID)) != 0) {
497 rv = AE_BAD_PARAMETER;
498 goto out;
499 }
500
501 /*
502 * "It is strongly recommended that the OS evaluate
503 * _OSC with the Query Support Flag set until _OSC
504 * returns the Capabilities Masked bit clear, to
505 * negotiate the set of features to be granted to
506 * the OS for native support (ACPI 4.0, 6.2.10)."
507 */
508 if ((ptr[0] & ACPI_OSC_ERROR_MASKED) != 0 && i >= 0) {
509
510 ACPI_FREE(buf.Pointer);
511 i--;
512
513 goto again;
514 }
515
516 if ((cap[0] & ACPI_OSC_QUERY) != 0) {
517
518 ACPI_FREE(buf.Pointer);
519 cap[0] &= ~ACPI_OSC_QUERY;
520
521 goto again;
522 }
523
524 /*
525 * It is permitted for _OSC to return all
526 * bits cleared, but this is specified to
527 * vary on per-device basis. Assume that
528 * everything rather than nothing will be
529 * supported in this case; we do not need
530 * the firmware to know the CPU features.
531 */
532 *val = (ptr[1] != 0) ? ptr[1] : cap[1];
533
534 out:
535 if (buf.Pointer != NULL)
536 ACPI_FREE(buf.Pointer);
537
538 return rv;
539 }
540
541 static void
542 acpicpu_notify(ACPI_HANDLE hdl, uint32_t evt, void *aux)
543 {
544 ACPI_OSD_EXEC_CALLBACK func;
545 struct acpicpu_softc *sc;
546 device_t self = aux;
547
548 sc = device_private(self);
549
550 if (sc->sc_cold != false)
551 return;
552
553 if (acpicpu_dynamic != true)
554 return;
555
556 switch (evt) {
557
558 case ACPICPU_C_NOTIFY:
559
560 if ((sc->sc_flags & ACPICPU_FLAG_C) == 0)
561 return;
562
563 func = acpicpu_cstate_callback;
564 break;
565
566 case ACPICPU_P_NOTIFY:
567
568 if ((sc->sc_flags & ACPICPU_FLAG_P) == 0)
569 return;
570
571 func = acpicpu_pstate_callback;
572 break;
573
574 case ACPICPU_T_NOTIFY:
575
576 if ((sc->sc_flags & ACPICPU_FLAG_T) == 0)
577 return;
578
579 func = acpicpu_tstate_callback;
580 break;
581
582 default:
583 aprint_error_dev(sc->sc_dev, "unknown notify: 0x%02X\n", evt);
584 return;
585 }
586
587 (void)AcpiOsExecute(OSL_NOTIFY_HANDLER, func, sc->sc_dev);
588 }
589
590 static bool
591 acpicpu_suspend(device_t self, const pmf_qual_t *qual)
592 {
593 struct acpicpu_softc *sc = device_private(self);
594
595 if ((sc->sc_flags & ACPICPU_FLAG_C) != 0)
596 (void)acpicpu_cstate_suspend(self);
597
598 if ((sc->sc_flags & ACPICPU_FLAG_P) != 0)
599 (void)acpicpu_pstate_suspend(self);
600
601 if ((sc->sc_flags & ACPICPU_FLAG_T) != 0)
602 (void)acpicpu_tstate_suspend(self);
603
604 sc->sc_cold = true;
605
606 return true;
607 }
608
609 static bool
610 acpicpu_resume(device_t self, const pmf_qual_t *qual)
611 {
612 struct acpicpu_softc *sc = device_private(self);
613
614 sc->sc_cold = false;
615
616 if ((sc->sc_flags & ACPICPU_FLAG_C) != 0)
617 (void)acpicpu_cstate_resume(self);
618
619 if ((sc->sc_flags & ACPICPU_FLAG_P) != 0)
620 (void)acpicpu_pstate_resume(self);
621
622 if ((sc->sc_flags & ACPICPU_FLAG_T) != 0)
623 (void)acpicpu_tstate_resume(self);
624
625 return true;
626 }
627
628 #ifdef _MODULE
629
630 MODULE(MODULE_CLASS_DRIVER, acpicpu, NULL);
631 CFDRIVER_DECL(acpicpu, DV_DULL, NULL);
632
633 static int acpicpuloc[] = { -1 };
634 extern struct cfattach acpicpu_ca;
635
636 static struct cfparent acpiparent = {
637 "acpinodebus", NULL, DVUNIT_ANY
638 };
639
640 static struct cfdata acpicpu_cfdata[] = {
641 {
642 .cf_name = "acpicpu",
643 .cf_atname = "acpicpu",
644 .cf_unit = 0,
645 .cf_fstate = FSTATE_STAR,
646 .cf_loc = acpicpuloc,
647 .cf_flags = 0,
648 .cf_pspec = &acpiparent,
649 },
650
651 { NULL, NULL, 0, 0, NULL, 0, NULL }
652 };
653
654 static int
655 acpicpu_modcmd(modcmd_t cmd, void *context)
656 {
657 int err;
658
659 switch (cmd) {
660
661 case MODULE_CMD_INIT:
662
663 err = config_cfdriver_attach(&acpicpu_cd);
664
665 if (err != 0)
666 return err;
667
668 err = config_cfattach_attach("acpicpu", &acpicpu_ca);
669
670 if (err != 0) {
671 config_cfdriver_detach(&acpicpu_cd);
672 return err;
673 }
674
675 err = config_cfdata_attach(acpicpu_cfdata, 1);
676
677 if (err != 0) {
678 config_cfattach_detach("acpicpu", &acpicpu_ca);
679 config_cfdriver_detach(&acpicpu_cd);
680 return err;
681 }
682
683 return 0;
684
685 case MODULE_CMD_FINI:
686
687 err = config_cfdata_detach(acpicpu_cfdata);
688
689 if (err != 0)
690 return err;
691
692 config_cfattach_detach("acpicpu", &acpicpu_ca);
693 config_cfdriver_detach(&acpicpu_cd);
694
695 return 0;
696
697 default:
698 return ENOTTY;
699 }
700 }
701
702 #endif /* _MODULE */
703