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