acpi_cpu.c revision 1.2 1 /* $NetBSD: acpi_cpu.c,v 1.2 2010/07/18 09:39:45 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.2 2010/07/18 09:39:45 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 kmutex_t acpicpu_mtx;
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
81 if (aa->aa_node->ad_type != ACPI_TYPE_PROCESSOR)
82 return 0;
83
84 if (acpi_match_hid(aa->aa_node->ad_devinfo, acpicpu_hid) != 0)
85 return 1;
86
87 return acpicpu_object(aa->aa_node->ad_handle, NULL);
88 }
89
90 static void
91 acpicpu_attach(device_t parent, device_t self, void *aux)
92 {
93 struct acpicpu_softc *sc = device_private(self);
94 struct acpi_attach_args *aa = aux;
95 static ONCE_DECL(once_attach);
96 int rv;
97
98 rv = acpicpu_object(aa->aa_node->ad_handle, &sc->sc_object);
99
100 if (rv == 0)
101 return;
102
103 rv = RUN_ONCE(&once_attach, acpicpu_once_attach);
104
105 if (rv != 0)
106 return;
107
108 KASSERT(acpicpu_sc != NULL);
109
110 mutex_enter(&acpicpu_mtx);
111
112 sc->sc_dev = self;
113 sc->sc_iot = aa->aa_iot;
114 sc->sc_node = aa->aa_node;
115 sc->sc_cpuid = acpicpu_id(sc->sc_object.ao_procid);
116
117 if (sc->sc_cpuid == 0xFFFFFF) {
118 mutex_exit(&acpicpu_mtx);
119 aprint_error(": invalid CPU ID\n");
120 return;
121 }
122
123 if (acpicpu_sc[sc->sc_cpuid] != NULL) {
124 mutex_exit(&acpicpu_mtx);
125 aprint_error(": already probed\n");
126 return;
127 }
128
129 acpicpu_sc[sc->sc_cpuid] = sc;
130 mutex_exit(&acpicpu_mtx);
131
132 sc->sc_cap = acpicpu_cap(sc);
133 sc->sc_flags |= acpicpu_md_quirks();
134
135 aprint_naive("\n");
136 aprint_normal(": ACPI CPU");
137 aprint_verbose(", cap 0x%02x, addr 0x%06x, len 0x%02x",
138 sc->sc_cap, sc->sc_object.ao_pblkaddr, sc->sc_object.ao_pblklen);
139 aprint_normal("\n");
140
141 /*
142 * We should claim the bus space. However, we do this only
143 * to announce that the space is in use. This is unnecessary
144 * if system I/O type C-states are not used. Many systems also
145 * report invalid values in the processor object. Finally, this
146 * is known to conflict with other devices. But 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_ioh = 0;
156 }
157
158 acpicpu_cstate_attach(self);
159
160 (void)acpi_register_notify(sc->sc_node, acpicpu_notify);
161 (void)config_finalize_register(self, acpicpu_cstate_start);
162 (void)pmf_device_register(self, acpicpu_suspend, acpicpu_resume);
163 }
164
165 static int
166 acpicpu_detach(device_t self, int flags)
167 {
168 struct acpicpu_softc *sc = device_private(self);
169 const bus_addr_t addr = sc->sc_object.ao_pblkaddr;
170 static ONCE_DECL(once_detach);
171 int rv = 0;
172
173 acpi_deregister_notify(sc->sc_node);
174
175 if ((sc->sc_flags & ACPICPU_FLAG_C) != 0)
176 rv = acpicpu_cstate_detach(self);
177
178 if (rv != 0)
179 return rv;
180
181 rv = RUN_ONCE(&once_detach, acpicpu_once_detach);
182
183 if (rv != 0)
184 return rv;
185
186 if (sc->sc_ioh != 0)
187 bus_space_unmap(sc->sc_iot, sc->sc_ioh, addr);
188
189 return 0;
190 }
191
192 static int
193 acpicpu_once_attach(void)
194 {
195 struct acpicpu_softc *sc;
196 unsigned int i;
197
198 acpicpu_sc = kmem_zalloc(maxcpus * sizeof(*sc), KM_SLEEP);
199
200 if (acpicpu_sc == NULL)
201 return ENOMEM;
202
203 for (i = 0; i < maxcpus; i++)
204 acpicpu_sc[i] = NULL;
205
206 mutex_init(&acpicpu_mtx, MUTEX_DEFAULT, IPL_VM);
207
208 return 0;
209 }
210
211 static int
212 acpicpu_once_detach(void)
213 {
214 struct acpicpu_softc *sc;
215
216 KASSERT(acpicpu_sc != NULL);
217
218 mutex_destroy(&acpicpu_mtx);
219 kmem_free(acpicpu_sc, maxcpus * sizeof(*sc));
220 acpicpu_sc = NULL;
221
222 return 0;
223 }
224
225 static int
226 acpicpu_object(ACPI_HANDLE hdl, struct acpicpu_object *ao)
227 {
228 ACPI_OBJECT *obj;
229 ACPI_BUFFER buf;
230 ACPI_STATUS rv;
231
232 rv = acpi_eval_struct(hdl, NULL, &buf);
233
234 if (ACPI_FAILURE(rv))
235 return 0;
236
237 obj = buf.Pointer;
238
239 if (obj->Type != ACPI_TYPE_PROCESSOR) {
240 rv = AE_TYPE;
241 goto out;
242 }
243
244 if (obj->Processor.ProcId > (uint32_t)maxcpus) {
245 rv = AE_LIMIT;
246 goto out;
247 }
248
249 KDASSERT((uint64_t)obj->Processor.PblkAddress < UINT32_MAX);
250
251 if (ao != NULL) {
252 ao->ao_procid = obj->Processor.ProcId;
253 ao->ao_pblklen = obj->Processor.PblkLength;
254 ao->ao_pblkaddr = obj->Processor.PblkAddress;
255 }
256
257 out:
258 if (buf.Pointer != NULL)
259 ACPI_FREE(buf.Pointer);
260
261 return ACPI_FAILURE(rv) ? 0 : 1;
262 }
263
264 static cpuid_t
265 acpicpu_id(uint32_t id)
266 {
267 CPU_INFO_ITERATOR cii;
268 struct cpu_info *ci;
269
270 for (CPU_INFO_FOREACH(cii, ci)) {
271
272 if (id == ci->ci_cpuid)
273 return id;
274 }
275
276 return 0xFFFFFF;
277 }
278
279 static uint32_t
280 acpicpu_cap(struct acpicpu_softc *sc)
281 {
282 uint32_t cap[3] = { 0 };
283 ACPI_STATUS rv;
284 int err;
285
286 /*
287 * Set machine-dependent processor capabilities.
288 *
289 * The _PDC was deprecated in ACPI 3.0 in favor of the _OSC,
290 * but firmware may expect that we evaluate it nevertheless.
291 */
292 rv = acpicpu_cap_pdc(sc->sc_node->ad_handle);
293
294 if (ACPI_FAILURE(rv) && rv != AE_NOT_FOUND)
295 aprint_error_dev(sc->sc_dev, "failed to evaluate _PDC: "
296 "%s\n", AcpiFormatException(rv));
297
298 rv = acpicpu_cap_osc(sc->sc_node->ad_handle, cap);
299
300 if (ACPI_FAILURE(rv) && rv != AE_NOT_FOUND)
301 aprint_error_dev(sc->sc_dev, "failed to evaluate _OSC: "
302 "%s\n", AcpiFormatException(rv));
303
304 if (ACPI_SUCCESS(rv)) {
305
306 err = cap[0] & ~__BIT(0);
307
308 if (err != 0) {
309 aprint_error_dev(sc->sc_dev, "errors in "
310 "_OSC: %s\n", acpicpu_cap_oscerr(err));
311 cap[2] = 0;
312 }
313 }
314
315 return cap[2];
316 }
317
318 static ACPI_OBJECT *
319 acpicpu_cap_init(void)
320 {
321 static uint32_t cap[3];
322 static ACPI_OBJECT obj;
323
324 cap[0] = ACPICPU_PDC_REVID;
325 cap[1] = 1;
326 cap[2] = acpicpu_md_cap();
327
328 obj.Type = ACPI_TYPE_BUFFER;
329 obj.Buffer.Length = sizeof(cap);
330 obj.Buffer.Pointer = (uint8_t *)cap;
331
332 return &obj;
333 }
334
335 static ACPI_STATUS
336 acpicpu_cap_pdc(ACPI_HANDLE hdl)
337 {
338 ACPI_OBJECT_LIST arg_list;
339
340 arg_list.Count = 1;
341 arg_list.Pointer = acpicpu_cap_init();
342
343 return AcpiEvaluateObject(hdl, "_PDC", &arg_list, NULL);
344 }
345
346 static ACPI_STATUS
347 acpicpu_cap_osc(ACPI_HANDLE hdl, uint32_t *val)
348 {
349 ACPI_OBJECT_LIST arg_list;
350 ACPI_OBJECT *cap, *obj;
351 ACPI_OBJECT arg[4];
352 ACPI_BUFFER buf;
353 ACPI_STATUS rv;
354
355 /* Intel. */
356 static uint8_t cpu_oscuuid[16] = {
357 0x16, 0xA6, 0x77, 0x40, 0x0C, 0x29, 0xBE, 0x47,
358 0x9E, 0xBD, 0xD8, 0x70, 0x58, 0x71, 0x39, 0x53
359 };
360
361 cap = acpicpu_cap_init();
362
363 arg_list.Count = 4;
364 arg_list.Pointer = arg;
365
366 arg[0].Type = ACPI_TYPE_BUFFER;
367 arg[0].Buffer.Length = sizeof(cpu_oscuuid);
368 arg[0].Buffer.Pointer = cpu_oscuuid;
369
370 arg[1].Type = ACPI_TYPE_INTEGER;
371 arg[1].Integer.Value = ACPICPU_PDC_REVID;
372
373 arg[2].Type = ACPI_TYPE_INTEGER;
374 arg[2].Integer.Value = cap->Buffer.Length / sizeof(uint32_t);
375
376 arg[3] = *cap;
377
378 buf.Pointer = NULL;
379 buf.Length = ACPI_ALLOCATE_LOCAL_BUFFER;
380
381 rv = AcpiEvaluateObject(hdl, "_OSC", &arg_list, &buf);
382
383 if (ACPI_FAILURE(rv))
384 return rv;
385
386 obj = buf.Pointer;
387
388 if (obj->Type != ACPI_TYPE_BUFFER) {
389 rv = AE_TYPE;
390 goto out;
391 }
392
393 if (obj->Buffer.Length != cap->Buffer.Length) {
394 rv = AE_BUFFER_OVERFLOW;
395 goto out;
396 }
397
398 (void)memcpy(val, obj->Buffer.Pointer, obj->Buffer.Length);
399
400 out:
401 if (buf.Pointer != NULL)
402 ACPI_FREE(buf.Pointer);
403
404 return rv;
405 }
406
407 static const char *
408 acpicpu_cap_oscerr(uint32_t err)
409 {
410
411 KASSERT((err & __BIT(0)) == 0);
412
413 if ((err & __BIT(1)) != 0)
414 return "_OSC failure";
415
416 if ((err & __BIT(2)) != 0)
417 return "unrecognized UUID";
418
419 if ((err & __BIT(3)) != 0)
420 return "unrecognized revision";
421
422 if ((err & __BIT(4)) != 0)
423 return "capabilities masked";
424
425 return "unknown error";
426 }
427
428 static void
429 acpicpu_notify(ACPI_HANDLE hdl, uint32_t evt, void *aux)
430 {
431 ACPI_OSD_EXEC_CALLBACK func;
432 struct acpicpu_softc *sc;
433 device_t self = aux;
434
435 sc = device_private(self);
436
437 switch (evt) {
438
439 case ACPICPU_C_NOTIFY:
440
441 if ((sc->sc_flags & ACPICPU_FLAG_C) == 0)
442 return;
443
444 func = acpicpu_cstate_callback;
445 break;
446
447 case ACPICPU_P_NOTIFY:
448
449 if ((sc->sc_flags & ACPICPU_FLAG_P) == 0)
450 return;
451
452 func = NULL;
453 break;
454
455 case ACPICPU_T_NOTIFY:
456
457 if ((sc->sc_flags & ACPICPU_FLAG_T) == 0)
458 return;
459
460 func = NULL;
461 break;
462
463 default:
464 aprint_error_dev(sc->sc_dev, "unknown notify: 0x%02X\n", evt);
465 return;
466 }
467
468 (void)AcpiOsExecute(OSL_NOTIFY_HANDLER, func, sc->sc_dev);
469 }
470
471 static bool
472 acpicpu_suspend(device_t self, const pmf_qual_t *qual)
473 {
474 struct acpicpu_softc *sc = device_private(self);
475
476 if ((sc->sc_flags & ACPICPU_FLAG_C) != 0)
477 (void)acpicpu_cstate_suspend(self);
478
479 return true;
480 }
481
482 static bool
483 acpicpu_resume(device_t self, const pmf_qual_t *qual)
484 {
485 struct acpicpu_softc *sc = device_private(self);
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