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