acpi_cpu.c revision 1.17 1 /* $NetBSD: acpi_cpu.c,v 1.17 2010/08/16 17:58:42 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.17 2010/08/16 17:58:42 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
40 #include <dev/acpi/acpireg.h>
41 #include <dev/acpi/acpivar.h>
42 #include <dev/acpi/acpi_cpu.h>
43
44 #include <machine/acpi_machdep.h>
45
46 #define _COMPONENT ACPI_BUS_COMPONENT
47 ACPI_MODULE_NAME ("acpi_cpu")
48
49 static int acpicpu_match(device_t, cfdata_t, void *);
50 static void acpicpu_attach(device_t, device_t, void *);
51 static int acpicpu_detach(device_t, int);
52 static int acpicpu_once_attach(void);
53 static int acpicpu_once_detach(void);
54 static void acpicpu_start(device_t);
55
56 static int acpicpu_object(ACPI_HANDLE, struct acpicpu_object *);
57 static cpuid_t acpicpu_id(uint32_t);
58 static uint32_t acpicpu_cap(struct acpicpu_softc *);
59 static ACPI_OBJECT *acpicpu_cap_init(void);
60 static ACPI_STATUS acpicpu_cap_pdc(ACPI_HANDLE);
61 static ACPI_STATUS acpicpu_cap_osc(ACPI_HANDLE, uint32_t *);
62 static const char *acpicpu_cap_oscerr(uint32_t);
63 static void acpicpu_notify(ACPI_HANDLE, uint32_t, void *);
64 static bool acpicpu_suspend(device_t, const pmf_qual_t *);
65 static bool acpicpu_resume(device_t, const pmf_qual_t *);
66
67 struct acpicpu_softc **acpicpu_sc = NULL;
68
69 static const char * const acpicpu_hid[] = {
70 "ACPI0007",
71 NULL
72 };
73
74 CFATTACH_DECL_NEW(acpicpu, sizeof(struct acpicpu_softc),
75 acpicpu_match, acpicpu_attach, acpicpu_detach, NULL);
76
77 static int
78 acpicpu_match(device_t parent, cfdata_t match, void *aux)
79 {
80 struct acpi_attach_args *aa = aux;
81 struct acpicpu_object ao;
82 int rv;
83
84 if (aa->aa_node->ad_type != ACPI_TYPE_PROCESSOR)
85 return 0;
86
87 if (acpi_match_hid(aa->aa_node->ad_devinfo, acpicpu_hid) != 0)
88 return 1;
89
90 rv = acpicpu_object(aa->aa_node->ad_handle, &ao);
91
92 if (rv != 0 || acpicpu_id(ao.ao_procid) == 0xFFFFFF)
93 return 0;
94
95 return 1;
96 }
97
98 static void
99 acpicpu_attach(device_t parent, device_t self, void *aux)
100 {
101 struct acpicpu_softc *sc = device_private(self);
102 struct acpi_attach_args *aa = aux;
103 static ONCE_DECL(once_attach);
104 int rv;
105
106 rv = acpicpu_object(aa->aa_node->ad_handle, &sc->sc_object);
107
108 if (rv != 0)
109 return;
110
111 rv = RUN_ONCE(&once_attach, acpicpu_once_attach);
112
113 if (rv != 0)
114 return;
115
116 sc->sc_dev = self;
117 sc->sc_cold = true;
118 sc->sc_mapped = false;
119 sc->sc_iot = aa->aa_iot;
120 sc->sc_node = aa->aa_node;
121 sc->sc_cpuid = acpicpu_id(sc->sc_object.ao_procid);
122
123 if (sc->sc_cpuid == 0xFFFFFF) {
124 aprint_error(": invalid CPU ID\n");
125 return;
126 }
127
128 if (acpicpu_sc[sc->sc_cpuid] != NULL) {
129 aprint_error(": already attached\n");
130 return;
131 }
132
133 acpicpu_sc[sc->sc_cpuid] = sc;
134
135 sc->sc_cap = acpicpu_cap(sc);
136 sc->sc_flags |= acpicpu_md_quirks();
137
138 mutex_init(&sc->sc_mtx, MUTEX_DEFAULT, IPL_NONE);
139
140 aprint_naive("\n");
141 aprint_normal(": ACPI CPU\n");
142
143 /*
144 * We should claim the bus space. However, we do this only
145 * to announce that the space is in use. As is noted in
146 * ichlpcib(4), we can continue our I/O without bus_space(9).
147 */
148 if (sc->sc_object.ao_pblklen == 6 && sc->sc_object.ao_pblkaddr != 0) {
149
150 rv = bus_space_map(sc->sc_iot, sc->sc_object.ao_pblkaddr,
151 sc->sc_object.ao_pblklen, 0, &sc->sc_ioh);
152
153 if (rv == 0)
154 sc->sc_mapped = true;
155 }
156
157 acpicpu_cstate_attach(self);
158 acpicpu_pstate_attach(self);
159 acpicpu_tstate_attach(self);
160
161 (void)config_defer(self, acpicpu_start);
162 (void)acpi_register_notify(sc->sc_node, acpicpu_notify);
163 (void)pmf_device_register(self, acpicpu_suspend, acpicpu_resume);
164 }
165
166 static int
167 acpicpu_detach(device_t self, int flags)
168 {
169 struct acpicpu_softc *sc = device_private(self);
170 const bus_addr_t addr = sc->sc_object.ao_pblkaddr;
171 static ONCE_DECL(once_detach);
172 int rv = 0;
173
174 sc->sc_cold = true;
175 acpi_deregister_notify(sc->sc_node);
176
177 if ((sc->sc_flags & ACPICPU_FLAG_C) != 0)
178 rv = acpicpu_cstate_detach(self);
179
180 if (rv != 0)
181 return rv;
182
183 if ((sc->sc_flags & ACPICPU_FLAG_P) != 0)
184 rv = acpicpu_pstate_detach(self);
185
186 if (rv != 0)
187 return rv;
188
189 if ((sc->sc_flags & ACPICPU_FLAG_T) != 0)
190 rv = acpicpu_tstate_detach(self);
191
192 if (rv != 0)
193 return rv;
194
195 rv = RUN_ONCE(&once_detach, acpicpu_once_detach);
196
197 if (rv != 0)
198 return rv;
199
200 if (sc->sc_mapped != false)
201 bus_space_unmap(sc->sc_iot, sc->sc_ioh, addr);
202
203 mutex_destroy(&sc->sc_mtx);
204
205 return 0;
206 }
207
208 static int
209 acpicpu_once_attach(void)
210 {
211 struct acpicpu_softc *sc;
212 unsigned int i;
213
214 acpicpu_sc = kmem_zalloc(maxcpus * sizeof(*sc), KM_SLEEP);
215
216 if (acpicpu_sc == NULL)
217 return ENOMEM;
218
219 for (i = 0; i < maxcpus; i++)
220 acpicpu_sc[i] = NULL;
221
222 return 0;
223 }
224
225 static int
226 acpicpu_once_detach(void)
227 {
228 struct acpicpu_softc *sc;
229
230 if (acpicpu_sc != NULL)
231 kmem_free(acpicpu_sc, maxcpus * sizeof(*sc));
232
233 return 0;
234 }
235
236 static void
237 acpicpu_start(device_t self)
238 {
239 struct acpicpu_softc *sc = device_private(self);
240 static bool once = false;
241
242 if (once != false) {
243 sc->sc_cold = false;
244 return;
245 }
246
247 /*
248 * Run the state-specific initialization
249 * routines. These should be called only
250 * once, after all ACPI CPUs have attached.
251 */
252 if ((sc->sc_flags & ACPICPU_FLAG_C) != 0)
253 acpicpu_cstate_start(self);
254
255 if ((sc->sc_flags & ACPICPU_FLAG_P) != 0)
256 acpicpu_pstate_start(self);
257
258 if ((sc->sc_flags & ACPICPU_FLAG_T) != 0)
259 acpicpu_tstate_start(self);
260
261 aprint_debug_dev(sc->sc_dev, "ACPI CPUs started (cap "
262 "0x%02x, flags 0x%06x)\n", sc->sc_cap, sc->sc_flags);
263
264 once = true;
265 sc->sc_cold = false;
266 }
267
268 static int
269 acpicpu_object(ACPI_HANDLE hdl, struct acpicpu_object *ao)
270 {
271 ACPI_OBJECT *obj;
272 ACPI_BUFFER buf;
273 ACPI_STATUS rv;
274
275 rv = acpi_eval_struct(hdl, NULL, &buf);
276
277 if (ACPI_FAILURE(rv))
278 return 1;
279
280 obj = buf.Pointer;
281
282 if (obj->Type != ACPI_TYPE_PROCESSOR) {
283 rv = AE_TYPE;
284 goto out;
285 }
286
287 if (obj->Processor.ProcId > (uint32_t)maxcpus) {
288 rv = AE_LIMIT;
289 goto out;
290 }
291
292 KDASSERT((uint64_t)obj->Processor.PblkAddress < UINT32_MAX);
293
294 if (ao != NULL) {
295 ao->ao_procid = obj->Processor.ProcId;
296 ao->ao_pblklen = obj->Processor.PblkLength;
297 ao->ao_pblkaddr = obj->Processor.PblkAddress;
298 }
299
300 out:
301 if (buf.Pointer != NULL)
302 ACPI_FREE(buf.Pointer);
303
304 return ACPI_FAILURE(rv) ? 1 : 0;
305 }
306
307 static cpuid_t
308 acpicpu_id(uint32_t id)
309 {
310 CPU_INFO_ITERATOR cii;
311 struct cpu_info *ci;
312
313 for (CPU_INFO_FOREACH(cii, ci)) {
314
315 if (id == ci->ci_acpiid)
316 return id;
317 }
318
319 return 0xFFFFFF;
320 }
321
322 static uint32_t
323 acpicpu_cap(struct acpicpu_softc *sc)
324 {
325 uint32_t cap[3] = { 0 };
326 ACPI_STATUS rv;
327 int err;
328
329 /*
330 * Set machine-dependent processor capabilities.
331 *
332 * The _PDC was deprecated in ACPI 3.0 in favor of the _OSC,
333 * but firmware may expect that we evaluate it nevertheless.
334 */
335 rv = acpicpu_cap_pdc(sc->sc_node->ad_handle);
336
337 if (ACPI_FAILURE(rv) && rv != AE_NOT_FOUND)
338 aprint_error_dev(sc->sc_dev, "failed to evaluate _PDC: "
339 "%s\n", AcpiFormatException(rv));
340
341 rv = acpicpu_cap_osc(sc->sc_node->ad_handle, cap);
342
343 if (ACPI_FAILURE(rv) && rv != AE_NOT_FOUND)
344 aprint_error_dev(sc->sc_dev, "failed to evaluate _OSC: "
345 "%s\n", AcpiFormatException(rv));
346
347 if (ACPI_SUCCESS(rv)) {
348
349 err = cap[0] & ~__BIT(0);
350
351 if (err != 0) {
352 aprint_error_dev(sc->sc_dev, "errors in "
353 "_OSC: %s\n", acpicpu_cap_oscerr(err));
354 cap[2] = 0;
355 }
356 }
357
358 return cap[2];
359 }
360
361 static ACPI_OBJECT *
362 acpicpu_cap_init(void)
363 {
364 static uint32_t cap[3];
365 static ACPI_OBJECT obj;
366
367 cap[0] = ACPICPU_PDC_REVID;
368 cap[1] = 1;
369 cap[2] = acpicpu_md_cap();
370
371 obj.Type = ACPI_TYPE_BUFFER;
372 obj.Buffer.Length = sizeof(cap);
373 obj.Buffer.Pointer = (uint8_t *)cap;
374
375 return &obj;
376 }
377
378 static ACPI_STATUS
379 acpicpu_cap_pdc(ACPI_HANDLE hdl)
380 {
381 ACPI_OBJECT_LIST arg_list;
382
383 arg_list.Count = 1;
384 arg_list.Pointer = acpicpu_cap_init();
385
386 return AcpiEvaluateObject(hdl, "_PDC", &arg_list, NULL);
387 }
388
389 static ACPI_STATUS
390 acpicpu_cap_osc(ACPI_HANDLE hdl, uint32_t *val)
391 {
392 ACPI_OBJECT_LIST arg_list;
393 ACPI_OBJECT *cap, *obj;
394 ACPI_OBJECT arg[4];
395 ACPI_BUFFER buf;
396 ACPI_STATUS rv;
397
398 /* Intel. */
399 static uint8_t cpu_oscuuid[16] = {
400 0x16, 0xA6, 0x77, 0x40, 0x0C, 0x29, 0xBE, 0x47,
401 0x9E, 0xBD, 0xD8, 0x70, 0x58, 0x71, 0x39, 0x53
402 };
403
404 cap = acpicpu_cap_init();
405
406 arg_list.Count = 4;
407 arg_list.Pointer = arg;
408
409 arg[0].Type = ACPI_TYPE_BUFFER;
410 arg[0].Buffer.Length = sizeof(cpu_oscuuid);
411 arg[0].Buffer.Pointer = cpu_oscuuid;
412
413 arg[1].Type = ACPI_TYPE_INTEGER;
414 arg[1].Integer.Value = ACPICPU_PDC_REVID;
415
416 arg[2].Type = ACPI_TYPE_INTEGER;
417 arg[2].Integer.Value = cap->Buffer.Length / sizeof(uint32_t);
418
419 arg[3] = *cap;
420
421 buf.Pointer = NULL;
422 buf.Length = ACPI_ALLOCATE_LOCAL_BUFFER;
423
424 rv = AcpiEvaluateObject(hdl, "_OSC", &arg_list, &buf);
425
426 if (ACPI_FAILURE(rv))
427 return rv;
428
429 obj = buf.Pointer;
430
431 if (obj->Type != ACPI_TYPE_BUFFER) {
432 rv = AE_TYPE;
433 goto out;
434 }
435
436 if (obj->Buffer.Length != cap->Buffer.Length) {
437 rv = AE_BUFFER_OVERFLOW;
438 goto out;
439 }
440
441 (void)memcpy(val, obj->Buffer.Pointer, obj->Buffer.Length);
442
443 out:
444 if (buf.Pointer != NULL)
445 ACPI_FREE(buf.Pointer);
446
447 return rv;
448 }
449
450 static const char *
451 acpicpu_cap_oscerr(uint32_t err)
452 {
453
454 KASSERT((err & __BIT(0)) == 0);
455
456 if ((err & __BIT(1)) != 0)
457 return "_OSC failure";
458
459 if ((err & __BIT(2)) != 0)
460 return "unrecognized UUID";
461
462 if ((err & __BIT(3)) != 0)
463 return "unrecognized revision";
464
465 if ((err & __BIT(4)) != 0)
466 return "capabilities masked";
467
468 return "unknown error";
469 }
470
471 static void
472 acpicpu_notify(ACPI_HANDLE hdl, uint32_t evt, void *aux)
473 {
474 ACPI_OSD_EXEC_CALLBACK func;
475 struct acpicpu_softc *sc;
476 device_t self = aux;
477
478 sc = device_private(self);
479
480 if (sc->sc_cold != false)
481 return;
482
483 switch (evt) {
484
485 case ACPICPU_C_NOTIFY:
486
487 if ((sc->sc_flags & ACPICPU_FLAG_C) == 0)
488 return;
489
490 func = acpicpu_cstate_callback;
491 break;
492
493 case ACPICPU_P_NOTIFY:
494
495 if ((sc->sc_flags & ACPICPU_FLAG_P) == 0)
496 return;
497
498 func = acpicpu_pstate_callback;
499 break;
500
501 case ACPICPU_T_NOTIFY:
502
503 if ((sc->sc_flags & ACPICPU_FLAG_T) == 0)
504 return;
505
506 func = acpicpu_tstate_callback;
507 break;
508
509 default:
510 aprint_error_dev(sc->sc_dev, "unknown notify: 0x%02X\n", evt);
511 return;
512 }
513
514 (void)AcpiOsExecute(OSL_NOTIFY_HANDLER, func, sc->sc_dev);
515 }
516
517 static bool
518 acpicpu_suspend(device_t self, const pmf_qual_t *qual)
519 {
520 struct acpicpu_softc *sc = device_private(self);
521
522 sc->sc_cold = true;
523
524 if ((sc->sc_flags & ACPICPU_FLAG_C) != 0)
525 (void)acpicpu_cstate_suspend(self);
526
527 if ((sc->sc_flags & ACPICPU_FLAG_P) != 0)
528 (void)acpicpu_pstate_suspend(self);
529
530 if ((sc->sc_flags & ACPICPU_FLAG_T) != 0)
531 (void)acpicpu_tstate_suspend(self);
532
533 return true;
534 }
535
536 static bool
537 acpicpu_resume(device_t self, const pmf_qual_t *qual)
538 {
539 struct acpicpu_softc *sc = device_private(self);
540
541 if ((sc->sc_flags & ACPICPU_FLAG_C) != 0)
542 (void)acpicpu_cstate_resume(self);
543
544 if ((sc->sc_flags & ACPICPU_FLAG_P) != 0)
545 (void)acpicpu_pstate_resume(self);
546
547 if ((sc->sc_flags & ACPICPU_FLAG_T) != 0)
548 (void)acpicpu_tstate_resume(self);
549
550 sc->sc_cold = false;
551
552 return true;
553 }
554
555 #ifdef _MODULE
556
557 MODULE(MODULE_CLASS_DRIVER, acpicpu, NULL);
558 CFDRIVER_DECL(acpicpu, DV_DULL, NULL);
559
560 static int acpicpuloc[] = { -1 };
561 extern struct cfattach acpicpu_ca;
562
563 static struct cfparent acpiparent = {
564 "acpinodebus", NULL, DVUNIT_ANY
565 };
566
567 static struct cfdata acpicpu_cfdata[] = {
568 {
569 .cf_name = "acpicpu",
570 .cf_atname = "acpicpu",
571 .cf_unit = 0,
572 .cf_fstate = FSTATE_STAR,
573 .cf_loc = acpicpuloc,
574 .cf_flags = 0,
575 .cf_pspec = &acpiparent,
576 },
577
578 { NULL, NULL, 0, 0, NULL, 0, NULL }
579 };
580
581 static int
582 acpicpu_modcmd(modcmd_t cmd, void *context)
583 {
584 int err;
585
586 switch (cmd) {
587
588 case MODULE_CMD_INIT:
589
590 err = config_cfdriver_attach(&acpicpu_cd);
591
592 if (err != 0)
593 return err;
594
595 err = config_cfattach_attach("acpicpu", &acpicpu_ca);
596
597 if (err != 0) {
598 config_cfdriver_detach(&acpicpu_cd);
599 return err;
600 }
601
602 err = config_cfdata_attach(acpicpu_cfdata, 1);
603
604 if (err != 0) {
605 config_cfattach_detach("acpicpu", &acpicpu_ca);
606 config_cfdriver_detach(&acpicpu_cd);
607 return err;
608 }
609
610 return 0;
611
612 case MODULE_CMD_FINI:
613
614 err = config_cfdata_detach(acpicpu_cfdata);
615
616 if (err != 0)
617 return err;
618
619 config_cfattach_detach("acpicpu", &acpicpu_ca);
620 config_cfdriver_detach(&acpicpu_cd);
621
622 return 0;
623
624 default:
625 return ENOTTY;
626 }
627 }
628
629 #endif /* _MODULE */
630