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