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