acpi.c revision 1.100 1 /* $NetBSD: acpi.c,v 1.100 2007/02/18 23:39:20 xtraeme Exp $ */
2
3 /*-
4 * Copyright (c) 2003 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Charles M. Hannum of By Noon Software, Inc.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * Copyright 2001, 2003 Wasabi Systems, Inc.
41 * All rights reserved.
42 *
43 * Written by Jason R. Thorpe for Wasabi Systems, Inc.
44 *
45 * Redistribution and use in source and binary forms, with or without
46 * modification, are permitted provided that the following conditions
47 * are met:
48 * 1. Redistributions of source code must retain the above copyright
49 * notice, this list of conditions and the following disclaimer.
50 * 2. Redistributions in binary form must reproduce the above copyright
51 * notice, this list of conditions and the following disclaimer in the
52 * documentation and/or other materials provided with the distribution.
53 * 3. All advertising materials mentioning features or use of this software
54 * must display the following acknowledgement:
55 * This product includes software developed for the NetBSD Project by
56 * Wasabi Systems, Inc.
57 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
58 * or promote products derived from this software without specific prior
59 * written permission.
60 *
61 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
62 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
63 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
64 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
65 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
66 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
67 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
68 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
69 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
70 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
71 * POSSIBILITY OF SUCH DAMAGE.
72 */
73
74 /*
75 * Autoconfiguration support for the Intel ACPI Component Architecture
76 * ACPI reference implementation.
77 */
78
79 #include <sys/cdefs.h>
80 __KERNEL_RCSID(0, "$NetBSD: acpi.c,v 1.100 2007/02/18 23:39:20 xtraeme Exp $");
81
82 #include "opt_acpi.h"
83 #include "opt_pcifixup.h"
84
85 #include <sys/param.h>
86 #include <sys/systm.h>
87 #include <sys/device.h>
88 #include <sys/malloc.h>
89 #include <sys/mutex.h>
90 #include <sys/kernel.h>
91 #include <sys/proc.h>
92 #include <sys/sysctl.h>
93
94 #include <dev/acpi/acpica.h>
95 #include <dev/acpi/acpireg.h>
96 #include <dev/acpi/acpivar.h>
97 #include <dev/acpi/acpi_osd.h>
98 #include <dev/acpi/acpi_timer.h>
99 #ifdef ACPIVERBOSE
100 #include <dev/acpi/acpidevs_data.h>
101 #endif
102
103 #if defined(ACPI_PCI_FIXUP)
104 #error The option ACPI_PCI_FIXUP has been obsoleted by PCI_INTR_FIXUP_DISABLED. Please adjust your kernel configuration file.
105 #endif
106
107 #ifdef PCI_INTR_FIXUP_DISABLED
108 #include <dev/pci/pcidevs.h>
109 #endif
110
111 MALLOC_DECLARE(M_ACPI);
112
113 #include <machine/acpi_machdep.h>
114
115 #ifdef ACPI_DEBUGGER
116 #define ACPI_DBGR_INIT 0x01
117 #define ACPI_DBGR_TABLES 0x02
118 #define ACPI_DBGR_ENABLE 0x04
119 #define ACPI_DBGR_PROBE 0x08
120 #define ACPI_DBGR_RUNNING 0x10
121
122 static int acpi_dbgr = 0x00;
123 #endif
124
125 static int acpi_match(struct device *, struct cfdata *, void *);
126 static void acpi_attach(struct device *, struct device *, void *);
127
128 static int acpi_print(void *aux, const char *);
129
130 static int sysctl_hw_acpi_sleepstate(SYSCTLFN_ARGS);
131
132 extern struct cfdriver acpi_cd;
133
134 CFATTACH_DECL(acpi, sizeof(struct acpi_softc),
135 acpi_match, acpi_attach, NULL, NULL);
136
137 /*
138 * This is a flag we set when the ACPI subsystem is active. Machine
139 * dependent code may wish to skip other steps (such as attaching
140 * subsystems that ACPI supercedes) when ACPI is active.
141 */
142 int acpi_active;
143 int acpi_force_load;
144
145 /*
146 * Pointer to the ACPI subsystem's state. There can be only
147 * one ACPI instance.
148 */
149 struct acpi_softc *acpi_softc;
150
151 /*
152 * Locking stuff.
153 */
154 static kmutex_t acpi_slock;
155 static int acpi_locked;
156
157 /*
158 * sysctl-related information
159 */
160
161 static int acpi_node = CTL_EOL;
162 static uint64_t acpi_root_pointer; /* found as hw.acpi.root */
163 static int acpi_sleepstate = ACPI_STATE_S0;
164
165 /*
166 * Prototypes.
167 */
168 static void acpi_shutdown(void *);
169 static void acpi_build_tree(struct acpi_softc *);
170 static ACPI_STATUS acpi_make_devnode(ACPI_HANDLE, UINT32, void *, void **);
171
172 static void acpi_enable_fixed_events(struct acpi_softc *);
173
174 /*
175 * acpi_probe:
176 *
177 * Probe for ACPI support. This is called by the
178 * machine-dependent ACPI front-end. All of the
179 * actual work is done by ACPICA.
180 *
181 * NOTE: This is not an autoconfiguration interface function.
182 */
183 int
184 acpi_probe(void)
185 {
186 static int beenhere;
187 ACPI_STATUS rv;
188
189 if (beenhere != 0)
190 panic("acpi_probe: ACPI has already been probed");
191 beenhere = 1;
192
193 mutex_init(&acpi_slock, MUTEX_DRIVER, IPL_NONE);
194 acpi_locked = 0;
195
196 /*
197 * Start up ACPICA.
198 */
199 #ifdef ACPI_DEBUGGER
200 if (acpi_dbgr & ACPI_DBGR_INIT)
201 acpi_osd_debugger();
202 #endif
203
204 rv = AcpiInitializeSubsystem();
205 if (ACPI_FAILURE(rv)) {
206 printf("ACPI: unable to initialize ACPICA: %s\n",
207 AcpiFormatException(rv));
208 return 0;
209 }
210
211 #ifdef ACPI_DEBUGGER
212 if (acpi_dbgr & ACPI_DBGR_TABLES)
213 acpi_osd_debugger();
214 #endif
215
216 rv = AcpiLoadTables();
217 if (ACPI_FAILURE(rv)) {
218 printf("ACPI: unable to load tables: %s\n",
219 AcpiFormatException(rv));
220 return 0;
221 }
222
223
224 if (!acpi_force_load && (acpi_find_quirks() & ACPI_QUIRK_BROKEN)) {
225 printf("ACPI: BIOS implementation in listed as broken:\n");
226 printf("ACPI: X/RSDT: OemId <%6.6s,%8.8s,%08x>, "
227 "AslId <%4.4s,%08x>\n",
228 AcpiGbl_XSDT->OemId, AcpiGbl_XSDT->OemTableId,
229 AcpiGbl_XSDT->OemRevision,
230 AcpiGbl_XSDT->AslCompilerId,
231 AcpiGbl_XSDT->AslCompilerRevision);
232 printf("ACPI: not used. set acpi_force_load to use anyway.\n");
233 return 0;
234 }
235
236 /*
237 * Looks like we have ACPI!
238 */
239
240 return 1;
241 }
242
243 static int
244 acpi_submatch(device_t parent, cfdata_t cf, const int *locs, void *aux)
245 {
246 struct cfattach *ca;
247
248 ca = config_cfattach_lookup(cf->cf_name, cf->cf_atname);
249 return (ca == &acpi_ca);
250 }
251
252 int
253 acpi_check(device_t parent, const char *ifattr)
254 {
255 return (config_search_ia(acpi_submatch, parent, ifattr, NULL) != NULL);
256 }
257
258 ACPI_STATUS
259 acpi_OsGetRootPointer(UINT32 Flags, ACPI_POINTER *PhysicalAddress)
260 {
261 ACPI_STATUS rv;
262
263 /*
264 * IA-32: Use AcpiFindRootPointer() to locate the RSDP.
265 *
266 * IA-64: Use the EFI.
267 *
268 * We let MD code handle this since there are multiple
269 * ways to do it.
270 */
271
272 rv = acpi_md_OsGetRootPointer(Flags, PhysicalAddress);
273
274 if (acpi_root_pointer == 0 && ACPI_SUCCESS(rv))
275 acpi_root_pointer =
276 (uint64_t)PhysicalAddress->Pointer.Physical;
277
278 return rv;
279 }
280
281 /*
282 * acpi_match:
283 *
284 * Autoconfiguration `match' routine.
285 */
286 static int
287 acpi_match(struct device *parent, struct cfdata *match,
288 void *aux)
289 {
290 /*
291 * XXX Check other locators? Hard to know -- machine
292 * dependent code has already checked for the presence
293 * of ACPI by calling acpi_probe(), so I suppose we
294 * don't really have to do anything else.
295 */
296 return 1;
297 }
298
299 /*
300 * acpi_attach:
301 *
302 * Autoconfiguration `attach' routine. Finish initializing
303 * ACPICA (some initialization was done in acpi_probe(),
304 * which was required to check for the presence of ACPI),
305 * and enable the ACPI subsystem.
306 */
307 static void
308 acpi_attach(struct device *parent, struct device *self, void *aux)
309 {
310 struct acpi_softc *sc = (void *) self;
311 struct acpibus_attach_args *aa = aux;
312 ACPI_STATUS rv;
313
314 aprint_naive(": Advanced Configuration and Power Interface\n");
315 aprint_normal(": Advanced Configuration and Power Interface\n");
316
317 if (acpi_softc != NULL)
318 panic("acpi_attach: ACPI has already been attached");
319
320 sysmon_power_settype("acpi");
321
322 aprint_verbose("%s: using Intel ACPI CA subsystem version %08x\n",
323 sc->sc_dev.dv_xname, ACPI_CA_VERSION);
324
325 aprint_verbose("%s: X/RSDT: OemId <%6.6s,%8.8s,%08x>, AslId <%4.4s,%08x>\n",
326 sc->sc_dev.dv_xname,
327 AcpiGbl_XSDT->OemId, AcpiGbl_XSDT->OemTableId,
328 AcpiGbl_XSDT->OemRevision,
329 AcpiGbl_XSDT->AslCompilerId, AcpiGbl_XSDT->AslCompilerRevision);
330
331 sc->sc_quirks = acpi_find_quirks();
332
333 sc->sc_iot = aa->aa_iot;
334 sc->sc_memt = aa->aa_memt;
335 sc->sc_pc = aa->aa_pc;
336 sc->sc_pciflags = aa->aa_pciflags;
337 sc->sc_ic = aa->aa_ic;
338
339 acpi_softc = sc;
340
341 /*
342 * Bring ACPI on-line.
343 */
344 #ifdef ACPI_DEBUGGER
345 if (acpi_dbgr & ACPI_DBGR_ENABLE)
346 acpi_osd_debugger();
347 #endif
348
349 rv = AcpiEnableSubsystem(0);
350 if (ACPI_FAILURE(rv)) {
351 aprint_error("%s: unable to enable ACPI: %s\n",
352 sc->sc_dev.dv_xname, AcpiFormatException(rv));
353 return;
354 }
355
356 /* early EC handler initialization if ECDT table is available */
357 #if NACPIEC > 0
358 acpiec_early_attach(&sc->sc_dev);
359 #endif
360
361 rv = AcpiInitializeObjects(0);
362 if (ACPI_FAILURE(rv)) {
363 aprint_error("%s: unable to initialize ACPI objects: %s\n",
364 sc->sc_dev.dv_xname, AcpiFormatException(rv));
365 return;
366 }
367 acpi_active = 1;
368
369 /* Our current state is "awake". */
370 sc->sc_sleepstate = ACPI_STATE_S0;
371
372 /* Show SCI interrupt. */
373 if (AcpiGbl_FADT != NULL)
374 aprint_verbose("%s: SCI interrupting at int %d\n",
375 sc->sc_dev.dv_xname, AcpiGbl_FADT->SciInt);
376 /*
377 * Check for fixed-hardware features.
378 */
379 acpi_enable_fixed_events(sc);
380 acpitimer_init();
381
382 /*
383 * Scan the namespace and build our device tree.
384 */
385 #ifdef ACPI_DEBUGGER
386 if (acpi_dbgr & ACPI_DBGR_PROBE)
387 acpi_osd_debugger();
388 #endif
389 acpi_md_callback((struct device *)sc);
390 acpi_build_tree(sc);
391
392 if (acpi_root_pointer != 0 && acpi_node != CTL_EOL) {
393 (void)sysctl_createv(NULL, 0, NULL, NULL,
394 CTLFLAG_IMMEDIATE,
395 CTLTYPE_QUAD, "root", NULL, NULL,
396 acpi_root_pointer, NULL, 0,
397 CTL_HW, acpi_node, CTL_CREATE, CTL_EOL);
398 }
399
400
401 /*
402 * Register a shutdown hook that disables certain ACPI
403 * events that might happen and confuse us while we're
404 * trying to shut down.
405 */
406 sc->sc_sdhook = shutdownhook_establish(acpi_shutdown, sc);
407 if (sc->sc_sdhook == NULL)
408 aprint_error("%s: WARNING: unable to register shutdown hook\n",
409 sc->sc_dev.dv_xname);
410
411 #ifdef ACPI_DEBUGGER
412 if (acpi_dbgr & ACPI_DBGR_RUNNING)
413 acpi_osd_debugger();
414 #endif
415 }
416
417 /*
418 * acpi_shutdown:
419 *
420 * Shutdown hook for ACPI -- disable some events that
421 * might confuse us.
422 */
423 static void
424 acpi_shutdown(void *arg)
425 {
426 /* nothing */
427 }
428
429 #if 0
430 /*
431 * acpi_disable:
432 *
433 * Disable ACPI.
434 */
435 static ACPI_STATUS
436 acpi_disable(struct acpi_softc *sc)
437 {
438 ACPI_STATUS rv = AE_OK;
439
440 if (acpi_active) {
441 rv = AcpiDisable();
442 if (ACPI_SUCCESS(rv))
443 acpi_active = 0;
444 }
445 return rv;
446 }
447 #endif
448
449 struct acpi_make_devnode_state {
450 struct acpi_softc *softc;
451 struct acpi_scope *scope;
452 };
453
454 /*
455 * acpi_build_tree:
456 *
457 * Scan relevant portions of the ACPI namespace and attach
458 * child devices.
459 */
460 static void
461 acpi_build_tree(struct acpi_softc *sc)
462 {
463 static const char *scopes[] = {
464 "\\_PR_", /* ACPI 1.0 processor namespace */
465 "\\_SB_", /* system bus namespace */
466 "\\_SI_", /* system indicator namespace */
467 "\\_TZ_", /* ACPI 1.0 thermal zone namespace */
468 NULL,
469 };
470 struct acpi_attach_args aa;
471 struct acpi_make_devnode_state state;
472 struct acpi_scope *as;
473 struct acpi_devnode *ad;
474 ACPI_HANDLE parent;
475 ACPI_STATUS rv;
476 int i;
477
478 TAILQ_INIT(&sc->sc_scopes);
479
480 state.softc = sc;
481
482 /*
483 * Scan the namespace and build our tree.
484 */
485 for (i = 0; scopes[i] != NULL; i++) {
486 as = malloc(sizeof(*as), M_ACPI, M_WAITOK);
487 as->as_name = scopes[i];
488 TAILQ_INIT(&as->as_devnodes);
489
490 TAILQ_INSERT_TAIL(&sc->sc_scopes, as, as_list);
491
492 state.scope = as;
493
494 rv = AcpiGetHandle(ACPI_ROOT_OBJECT, scopes[i],
495 &parent);
496 if (ACPI_SUCCESS(rv)) {
497 AcpiWalkNamespace(ACPI_TYPE_ANY, parent, 100,
498 acpi_make_devnode, &state, NULL);
499 }
500
501 /* Now, for this namespace, try and attach the devices. */
502 TAILQ_FOREACH(ad, &as->as_devnodes, ad_list) {
503 aa.aa_node = ad;
504 aa.aa_iot = sc->sc_iot;
505 aa.aa_memt = sc->sc_memt;
506 aa.aa_pc = sc->sc_pc;
507 aa.aa_pciflags = sc->sc_pciflags;
508 aa.aa_ic = sc->sc_ic;
509
510 if (ad->ad_devinfo->Type == ACPI_TYPE_DEVICE) {
511 /*
512 * XXX We only attach devices which are:
513 *
514 * - present
515 * - enabled
516 * - functioning properly
517 *
518 * However, if enabled, it's decoding resources,
519 * so we should claim them, if possible.
520 * Requires changes to bus_space(9).
521 */
522 if ((ad->ad_devinfo->Valid & ACPI_VALID_STA) ==
523 ACPI_VALID_STA &&
524 (ad->ad_devinfo->CurrentStatus &
525 (ACPI_STA_DEV_PRESENT|ACPI_STA_DEV_ENABLED|
526 ACPI_STA_DEV_OK)) !=
527 (ACPI_STA_DEV_PRESENT|ACPI_STA_DEV_ENABLED|
528 ACPI_STA_DEV_OK))
529 continue;
530
531 /*
532 * XXX Same problem as above...
533 */
534 if ((ad->ad_devinfo->Valid & ACPI_VALID_HID)
535 == 0)
536 continue;
537 }
538
539 ad->ad_device = config_found_ia(&sc->sc_dev,
540 "acpinodebus", &aa, acpi_print);
541 }
542 }
543 config_found_ia(&sc->sc_dev, "acpiapmbus", NULL, NULL);
544 }
545
546 #ifdef ACPI_ACTIVATE_DEV
547 static void
548 acpi_activate_device(ACPI_HANDLE handle, ACPI_DEVICE_INFO **di)
549 {
550 ACPI_STATUS rv;
551 ACPI_BUFFER buf;
552
553 buf.Pointer = NULL;
554 buf.Length = ACPI_ALLOCATE_BUFFER;
555
556 #ifdef ACPI_DEBUG
557 aprint_normal("acpi_activate_device: %s, old status=%x\n",
558 (*di)->HardwareId.Value, (*di)->CurrentStatus);
559 #endif
560
561 rv = acpi_allocate_resources(handle);
562 if (ACPI_FAILURE(rv)) {
563 aprint_error("acpi: activate failed for %s\n",
564 (*di)->HardwareId.Value);
565 } else {
566 aprint_verbose("acpi: activated %s\n",
567 (*di)->HardwareId.Value);
568 }
569
570 (void)AcpiGetObjectInfo(handle, &buf);
571 AcpiOsFree(*di);
572 *di = buf.Pointer;
573
574 #ifdef ACPI_DEBUG
575 aprint_normal("acpi_activate_device: %s, new status=%x\n",
576 (*di)->HardwareId.Value, (*di)->CurrentStatus);
577 #endif
578 }
579 #endif /* ACPI_ACTIVATE_DEV */
580
581 /*
582 * acpi_make_devnode:
583 *
584 * Make an ACPI devnode.
585 */
586 static ACPI_STATUS
587 acpi_make_devnode(ACPI_HANDLE handle, UINT32 level, void *context,
588 void **status)
589 {
590 struct acpi_make_devnode_state *state = context;
591 #if defined(ACPI_DEBUG) || defined(ACPI_EXTRA_DEBUG)
592 struct acpi_softc *sc = state->softc;
593 #endif
594 struct acpi_scope *as = state->scope;
595 struct acpi_devnode *ad;
596 ACPI_OBJECT_TYPE type;
597 ACPI_BUFFER buf;
598 ACPI_DEVICE_INFO *devinfo;
599 ACPI_STATUS rv;
600
601 rv = AcpiGetType(handle, &type);
602 if (ACPI_SUCCESS(rv)) {
603 buf.Pointer = NULL;
604 buf.Length = ACPI_ALLOCATE_BUFFER;
605 rv = AcpiGetObjectInfo(handle, &buf);
606 if (ACPI_FAILURE(rv)) {
607 #ifdef ACPI_DEBUG
608 aprint_normal("%s: AcpiGetObjectInfo failed: %s\n",
609 sc->sc_dev.dv_xname, AcpiFormatException(rv));
610 #endif
611 goto out; /* XXX why return OK */
612 }
613
614 devinfo = buf.Pointer;
615
616 switch (type) {
617 case ACPI_TYPE_DEVICE:
618 #ifdef ACPI_ACTIVATE_DEV
619 if ((devinfo->Valid & (ACPI_VALID_STA|ACPI_VALID_HID)) ==
620 (ACPI_VALID_STA|ACPI_VALID_HID) &&
621 (devinfo->CurrentStatus &
622 (ACPI_STA_DEV_PRESENT|ACPI_STA_DEV_ENABLED)) ==
623 ACPI_STA_DEV_PRESENT)
624 acpi_activate_device(handle, &devinfo);
625
626 /* FALLTHROUGH */
627 #endif
628
629 case ACPI_TYPE_PROCESSOR:
630 case ACPI_TYPE_THERMAL:
631 case ACPI_TYPE_POWER:
632 ad = malloc(sizeof(*ad), M_ACPI, M_NOWAIT|M_ZERO);
633 if (ad == NULL)
634 return AE_NO_MEMORY;
635
636 ad->ad_devinfo = devinfo;
637 ad->ad_handle = handle;
638 ad->ad_level = level;
639 ad->ad_scope = as;
640 ad->ad_type = type;
641
642 TAILQ_INSERT_TAIL(&as->as_devnodes, ad, ad_list);
643
644 if ((ad->ad_devinfo->Valid & ACPI_VALID_HID) == 0)
645 goto out;
646
647 #ifdef ACPI_EXTRA_DEBUG
648 aprint_normal("%s: HID %s found in scope %s level %d\n",
649 sc->sc_dev.dv_xname,
650 ad->ad_devinfo->HardwareId.Value,
651 as->as_name, ad->ad_level);
652 if (ad->ad_devinfo->Valid & ACPI_VALID_UID)
653 aprint_normal(" UID %s\n",
654 ad->ad_devinfo->UniqueId.Value);
655 if (ad->ad_devinfo->Valid & ACPI_VALID_ADR)
656 aprint_normal(" ADR 0x%016qx\n",
657 ad->ad_devinfo->Address);
658 if (ad->ad_devinfo->Valid & ACPI_VALID_STA)
659 aprint_normal(" STA 0x%08x\n",
660 ad->ad_devinfo->CurrentStatus);
661 #endif
662 }
663 }
664 out:
665 return AE_OK;
666 }
667
668 /*
669 * acpi_print:
670 *
671 * Autoconfiguration print routine for ACPI node bus.
672 */
673 static int
674 acpi_print(void *aux, const char *pnp)
675 {
676 struct acpi_attach_args *aa = aux;
677 ACPI_STATUS rv;
678
679 if (pnp) {
680 if (aa->aa_node->ad_devinfo->Valid & ACPI_VALID_HID) {
681 char *pnpstr =
682 aa->aa_node->ad_devinfo->HardwareId.Value;
683 char *str;
684
685 aprint_normal("%s ", pnpstr);
686 rv = acpi_eval_string(aa->aa_node->ad_handle,
687 "_STR", &str);
688 if (ACPI_SUCCESS(rv)) {
689 aprint_normal("[%s] ", str);
690 AcpiOsFree(str);
691 }
692 #ifdef ACPIVERBOSE
693 else {
694 int i;
695
696 for (i = 0; i < sizeof(acpi_knowndevs) /
697 sizeof(acpi_knowndevs[0]); i++) {
698 if (strcmp(acpi_knowndevs[i].pnp,
699 pnpstr) == 0) {
700 aprint_normal("[%s] ",
701 acpi_knowndevs[i].str);
702 }
703 }
704 }
705
706 #endif
707 } else {
708 aprint_normal("ACPI Object Type '%s' (0x%02x) ",
709 AcpiUtGetTypeName(aa->aa_node->ad_devinfo->Type),
710 aa->aa_node->ad_devinfo->Type);
711 }
712 aprint_normal("at %s", pnp);
713 } else {
714 if (aa->aa_node->ad_devinfo->Valid & ACPI_VALID_HID) {
715 aprint_normal(" (%s", aa->aa_node->ad_devinfo->HardwareId.Value);
716 if (aa->aa_node->ad_devinfo->Valid & ACPI_VALID_UID) {
717 const char *uid;
718
719 uid = aa->aa_node->ad_devinfo->UniqueId.Value;
720 if (uid[0] == '\0')
721 uid = "<null>";
722 aprint_normal("-%s", uid);
723 }
724 aprint_normal(")");
725 }
726 }
727
728 return UNCONF;
729 }
730
731 /*****************************************************************************
732 * ACPI fixed-hardware feature handlers
733 *****************************************************************************/
734
735 static UINT32 acpi_fixed_button_handler(void *);
736 static void acpi_fixed_button_pressed(void *);
737
738 /*
739 * acpi_enable_fixed_events:
740 *
741 * Enable any fixed-hardware feature handlers.
742 */
743 static void
744 acpi_enable_fixed_events(struct acpi_softc *sc)
745 {
746 static int beenhere;
747 ACPI_STATUS rv;
748
749 KASSERT(beenhere == 0);
750 beenhere = 1;
751
752 /*
753 * Check for fixed-hardware buttons.
754 */
755
756 if (AcpiGbl_FADT != NULL && AcpiGbl_FADT->PwrButton == 0) {
757 aprint_verbose("%s: fixed-feature power button present\n",
758 sc->sc_dev.dv_xname);
759 sc->sc_smpsw_power.smpsw_name = sc->sc_dev.dv_xname;
760 sc->sc_smpsw_power.smpsw_type = PSWITCH_TYPE_POWER;
761 if (sysmon_pswitch_register(&sc->sc_smpsw_power) != 0) {
762 aprint_error("%s: unable to register fixed power "
763 "button with sysmon\n", sc->sc_dev.dv_xname);
764 } else {
765 rv = AcpiInstallFixedEventHandler(
766 ACPI_EVENT_POWER_BUTTON,
767 acpi_fixed_button_handler, &sc->sc_smpsw_power);
768 if (ACPI_FAILURE(rv)) {
769 aprint_error("%s: unable to install handler "
770 "for fixed power button: %s\n",
771 sc->sc_dev.dv_xname,
772 AcpiFormatException(rv));
773 }
774 }
775 }
776
777 if (AcpiGbl_FADT != NULL && AcpiGbl_FADT->SleepButton == 0) {
778 aprint_verbose("%s: fixed-feature sleep button present\n",
779 sc->sc_dev.dv_xname);
780 sc->sc_smpsw_sleep.smpsw_name = sc->sc_dev.dv_xname;
781 sc->sc_smpsw_sleep.smpsw_type = PSWITCH_TYPE_SLEEP;
782 if (sysmon_pswitch_register(&sc->sc_smpsw_power) != 0) {
783 aprint_error("%s: unable to register fixed sleep "
784 "button with sysmon\n", sc->sc_dev.dv_xname);
785 } else {
786 rv = AcpiInstallFixedEventHandler(
787 ACPI_EVENT_SLEEP_BUTTON,
788 acpi_fixed_button_handler, &sc->sc_smpsw_sleep);
789 if (ACPI_FAILURE(rv)) {
790 aprint_error("%s: unable to install handler "
791 "for fixed sleep button: %s\n",
792 sc->sc_dev.dv_xname,
793 AcpiFormatException(rv));
794 }
795 }
796 }
797 }
798
799 /*
800 * acpi_fixed_button_handler:
801 *
802 * Event handler for the fixed buttons.
803 */
804 static UINT32
805 acpi_fixed_button_handler(void *context)
806 {
807 struct sysmon_pswitch *smpsw = context;
808 int rv;
809
810 #ifdef ACPI_BUT_DEBUG
811 printf("%s: fixed button handler\n", smpsw->smpsw_name);
812 #endif
813
814 rv = AcpiOsQueueForExecution(OSD_PRIORITY_LO,
815 acpi_fixed_button_pressed, smpsw);
816 if (ACPI_FAILURE(rv))
817 printf("%s: WARNING: unable to queue fixed button pressed "
818 "callback: %s\n", smpsw->smpsw_name,
819 AcpiFormatException(rv));
820
821 return ACPI_INTERRUPT_HANDLED;
822 }
823
824 /*
825 * acpi_fixed_button_pressed:
826 *
827 * Deal with a fixed button being pressed.
828 */
829 static void
830 acpi_fixed_button_pressed(void *context)
831 {
832 struct sysmon_pswitch *smpsw = context;
833
834 #ifdef ACPI_BUT_DEBUG
835 printf("%s: fixed button pressed, calling sysmon\n",
836 smpsw->smpsw_name);
837 #endif
838
839 sysmon_pswitch_event(smpsw, PSWITCH_EVENT_PRESSED);
840 }
841
842 /*****************************************************************************
843 * ACPI utility routines.
844 *****************************************************************************/
845
846 /*
847 * acpi_eval_integer:
848 *
849 * Evaluate an integer object.
850 */
851 ACPI_STATUS
852 acpi_eval_integer(ACPI_HANDLE handle, const char *path, ACPI_INTEGER *valp)
853 {
854 ACPI_STATUS rv;
855 ACPI_BUFFER buf;
856 ACPI_OBJECT param;
857
858 if (handle == NULL)
859 handle = ACPI_ROOT_OBJECT;
860
861 buf.Pointer = ¶m;
862 buf.Length = sizeof(param);
863
864 rv = AcpiEvaluateObjectTyped(handle, path, NULL, &buf, ACPI_TYPE_INTEGER);
865 if (ACPI_SUCCESS(rv))
866 *valp = param.Integer.Value;
867
868 return rv;
869 }
870
871 /*
872 * acpi_eval_string:
873 *
874 * Evaluate a (Unicode) string object.
875 */
876 ACPI_STATUS
877 acpi_eval_string(ACPI_HANDLE handle, const char *path, char **stringp)
878 {
879 ACPI_STATUS rv;
880 ACPI_BUFFER buf;
881
882 if (handle == NULL)
883 handle = ACPI_ROOT_OBJECT;
884
885 buf.Pointer = NULL;
886 buf.Length = ACPI_ALLOCATE_BUFFER;
887
888 rv = AcpiEvaluateObjectTyped(handle, path, NULL, &buf, ACPI_TYPE_STRING);
889 if (ACPI_SUCCESS(rv)) {
890 ACPI_OBJECT *param = buf.Pointer;
891 const char *ptr = param->String.Pointer;
892 size_t len = param->String.Length;
893 if ((*stringp = AcpiOsAllocate(len)) == NULL)
894 rv = AE_NO_MEMORY;
895 else
896 (void)memcpy(*stringp, ptr, len);
897 AcpiOsFree(param);
898 }
899
900 return rv;
901 }
902
903
904 /*
905 * acpi_eval_struct:
906 *
907 * Evaluate a more complex structure.
908 * Caller must free buf.Pointer by AcpiOsFree().
909 */
910 ACPI_STATUS
911 acpi_eval_struct(ACPI_HANDLE handle, const char *path, ACPI_BUFFER *bufp)
912 {
913 ACPI_STATUS rv;
914
915 if (handle == NULL)
916 handle = ACPI_ROOT_OBJECT;
917
918 bufp->Pointer = NULL;
919 bufp->Length = ACPI_ALLOCATE_BUFFER;
920
921 rv = AcpiEvaluateObject(handle, path, NULL, bufp);
922
923 return rv;
924 }
925
926 /*
927 * acpi_foreach_package_object:
928 *
929 * Iterate over all objects in a in a packages and pass then all
930 * to a function. If the called function returns non AE_OK, the
931 * iteration is stopped and that value is returned.
932 */
933
934 ACPI_STATUS
935 acpi_foreach_package_object(ACPI_OBJECT *pkg,
936 ACPI_STATUS (*func)(ACPI_OBJECT *, void *),
937 void *arg)
938 {
939 ACPI_STATUS rv = AE_OK;
940 int i;
941
942 if (pkg == NULL || pkg->Type != ACPI_TYPE_PACKAGE)
943 return AE_BAD_PARAMETER;
944
945 for (i = 0; i < pkg->Package.Count; i++) {
946 rv = (*func)(&pkg->Package.Elements[i], arg);
947 if (ACPI_FAILURE(rv))
948 break;
949 }
950
951 return rv;
952 }
953
954 const char *
955 acpi_name(ACPI_HANDLE handle)
956 {
957 static char buffer[80];
958 ACPI_BUFFER buf;
959 ACPI_STATUS rv;
960
961 buf.Length = sizeof(buffer);
962 buf.Pointer = buffer;
963
964 rv = AcpiGetName(handle, ACPI_FULL_PATHNAME, &buf);
965 if (ACPI_FAILURE(rv))
966 return "(unknown acpi path)";
967 return buffer;
968 }
969
970 /*
971 * acpi_get:
972 *
973 * Fetch data info the specified (empty) ACPI buffer.
974 * Caller must free buf.Pointer by AcpiOsFree().
975 */
976 ACPI_STATUS
977 acpi_get(ACPI_HANDLE handle, ACPI_BUFFER *buf,
978 ACPI_STATUS (*getit)(ACPI_HANDLE, ACPI_BUFFER *))
979 {
980 buf->Pointer = NULL;
981 buf->Length = ACPI_ALLOCATE_BUFFER;
982
983 return (*getit)(handle, buf);
984 }
985
986
987 /*
988 * acpi_match_hid
989 *
990 * Match given ids against _HID and _CIDs
991 */
992 int
993 acpi_match_hid(ACPI_DEVICE_INFO *ad, const char * const *ids)
994 {
995 int i;
996
997 while (*ids) {
998 if (ad->Valid & ACPI_VALID_HID) {
999 if (pmatch(ad->HardwareId.Value, *ids, NULL) == 2)
1000 return 1;
1001 }
1002
1003 if (ad->Valid & ACPI_VALID_CID) {
1004 for (i = 0; i < ad->CompatibilityId.Count; i++) {
1005 if (pmatch(ad->CompatibilityId.Id[i].Value, *ids, NULL) == 2)
1006 return 1;
1007 }
1008 }
1009 ids++;
1010 }
1011
1012 return 0;
1013 }
1014
1015 /*
1016 * acpi_set_wake_gpe
1017 *
1018 * Set GPE as both Runtime and Wake
1019 */
1020 void
1021 acpi_set_wake_gpe(ACPI_HANDLE handle)
1022 {
1023 ACPI_BUFFER buf;
1024 ACPI_STATUS rv;
1025 ACPI_OBJECT *p, *elt;
1026
1027 rv = acpi_eval_struct(handle, METHOD_NAME__PRW, &buf);
1028 if (ACPI_FAILURE(rv))
1029 return; /* just ignore */
1030
1031 p = buf.Pointer;
1032 if (p->Type != ACPI_TYPE_PACKAGE || p->Package.Count < 2)
1033 goto out; /* just ignore */
1034
1035 elt = p->Package.Elements;
1036
1037 /* TBD: package support */
1038 AcpiSetGpeType(NULL, elt[0].Integer.Value, ACPI_GPE_TYPE_WAKE_RUN);
1039 AcpiEnableGpe(NULL, elt[0].Integer.Value, ACPI_NOT_ISR);
1040
1041 out:
1042 AcpiOsFree(buf.Pointer);
1043 }
1044
1045
1046 /*****************************************************************************
1047 * ACPI sleep support.
1048 *****************************************************************************/
1049
1050 static int
1051 is_available_state(struct acpi_softc *sc, int state)
1052 {
1053 UINT8 type_a, type_b;
1054
1055 return ACPI_SUCCESS(AcpiGetSleepTypeData((UINT8)state,
1056 &type_a, &type_b));
1057 }
1058
1059 /*
1060 * acpi_enter_sleep_state:
1061 *
1062 * enter to the specified sleep state.
1063 */
1064
1065 ACPI_STATUS
1066 acpi_enter_sleep_state(struct acpi_softc *sc, int state)
1067 {
1068 int s;
1069 ACPI_STATUS ret = AE_OK;
1070
1071 if (state == acpi_sleepstate)
1072 return AE_OK;
1073
1074 aprint_normal("%s: entering state %d\n", sc->sc_dev.dv_xname, state);
1075
1076 switch (state) {
1077 case ACPI_STATE_S0:
1078 break;
1079 case ACPI_STATE_S1:
1080 case ACPI_STATE_S2:
1081 case ACPI_STATE_S3:
1082 case ACPI_STATE_S4:
1083 if (!is_available_state(sc, state)) {
1084 aprint_error("%s: cannot enter the sleep state (%d).\n",
1085 sc->sc_dev.dv_xname, state);
1086 break;
1087 }
1088 ret = AcpiEnterSleepStatePrep(state);
1089 if (ACPI_FAILURE(ret)) {
1090 aprint_error("%s: failed preparing to sleep (%s)\n",
1091 sc->sc_dev.dv_xname, AcpiFormatException(ret));
1092 break;
1093 }
1094 acpi_sleepstate = state;
1095 if (state == ACPI_STATE_S1) {
1096 /* just enter the state */
1097 acpi_md_OsDisableInterrupt();
1098 AcpiEnterSleepState((UINT8)state);
1099 } else {
1100 /* XXX: powerhooks(9) framework is too poor to
1101 * support ACPI sleep state...
1102 */
1103 dopowerhooks(PWR_SOFTSUSPEND);
1104 s = splhigh();
1105 dopowerhooks(PWR_SUSPEND);
1106 acpi_md_sleep(state);
1107 dopowerhooks(PWR_RESUME);
1108 splx(s);
1109 dopowerhooks(PWR_SOFTRESUME);
1110 if (state==ACPI_STATE_S4)
1111 AcpiEnable();
1112 }
1113 AcpiLeaveSleepState((UINT8)state);
1114 break;
1115 case ACPI_STATE_S5:
1116 ret = AcpiEnterSleepStatePrep(ACPI_STATE_S5);
1117 if (ACPI_FAILURE(ret)) {
1118 aprint_error("%s: failed preparing to sleep (%s)\n",
1119 sc->sc_dev.dv_xname, AcpiFormatException(ret));
1120 break;
1121 }
1122 acpi_sleepstate = state;
1123 acpi_md_OsDisableInterrupt();
1124 AcpiEnterSleepState(ACPI_STATE_S5);
1125 aprint_error("%s: WARNING powerdown failed!\n",
1126 sc->sc_dev.dv_xname);
1127 break;
1128 }
1129
1130 aprint_normal("%s: resuming\n", sc->sc_dev.dv_xname);
1131 acpi_sleepstate = ACPI_STATE_S0;
1132 return ret;
1133 }
1134
1135 #if defined(ACPI_ACTIVATE_DEV)
1136 /* XXX This very incomplete */
1137 ACPI_STATUS
1138 acpi_allocate_resources(ACPI_HANDLE handle)
1139 {
1140 ACPI_BUFFER bufp, bufc, bufn;
1141 ACPI_RESOURCE *resp, *resc, *resn;
1142 ACPI_RESOURCE_IRQ *irq;
1143 ACPI_RESOURCE_EXTENDED_IRQ *xirq;
1144 ACPI_STATUS rv;
1145 uint delta;
1146
1147 rv = acpi_get(handle, &bufp, AcpiGetPossibleResources);
1148 if (ACPI_FAILURE(rv))
1149 goto out;
1150 rv = acpi_get(handle, &bufc, AcpiGetCurrentResources);
1151 if (ACPI_FAILURE(rv)) {
1152 goto out1;
1153 }
1154
1155 bufn.Length = 1000;
1156 bufn.Pointer = resn = malloc(bufn.Length, M_ACPI, M_WAITOK);
1157 resp = bufp.Pointer;
1158 resc = bufc.Pointer;
1159 while (resc->Type != ACPI_RESOURCE_TYPE_END_TAG &&
1160 resp->Type != ACPI_RESOURCE_TYPE_END_TAG) {
1161 while (resc->Type != resp->Type && resp->Type != ACPI_RESOURCE_TYPE_END_TAG)
1162 resp = ACPI_NEXT_RESOURCE(resp);
1163 if (resp->Type == ACPI_RESOURCE_TYPE_END_TAG)
1164 break;
1165 /* Found identical Id */
1166 resn->Type = resc->Type;
1167 switch (resc->Type) {
1168 case ACPI_RESOURCE_TYPE_IRQ:
1169 memcpy(&resn->Data, &resp->Data,
1170 sizeof(ACPI_RESOURCE_IRQ));
1171 irq = (ACPI_RESOURCE_IRQ *)&resn->Data;
1172 irq->Interrupts[0] =
1173 ((ACPI_RESOURCE_IRQ *)&resp->Data)->
1174 Interrupts[irq->InterruptCount-1];
1175 irq->InterruptCount = 1;
1176 resn->Length = ACPI_RS_SIZE(ACPI_RESOURCE_IRQ);
1177 break;
1178 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
1179 memcpy(&resn->Data, &resp->Data,
1180 sizeof(ACPI_RESOURCE_EXTENDED_IRQ));
1181 xirq = (ACPI_RESOURCE_EXTENDED_IRQ *)&resn->Data;
1182 #if 0
1183 /*
1184 * XXX not duplicating the interrupt logic above
1185 * because its not clear what it accomplishes.
1186 */
1187 xirq->Interrupts[0] =
1188 ((ACPI_RESOURCE_EXT_IRQ *)&resp->Data)->
1189 Interrupts[irq->NumberOfInterrupts-1];
1190 xirq->NumberOfInterrupts = 1;
1191 #endif
1192 resn->Length = ACPI_RS_SIZE(ACPI_RESOURCE_EXTENDED_IRQ);
1193 break;
1194 case ACPI_RESOURCE_TYPE_IO:
1195 memcpy(&resn->Data, &resp->Data,
1196 sizeof(ACPI_RESOURCE_IO));
1197 resn->Length = resp->Length;
1198 break;
1199 default:
1200 printf("acpi_allocate_resources: res=%d\n", resc->Type);
1201 rv = AE_BAD_DATA;
1202 goto out2;
1203 }
1204 resc = ACPI_NEXT_RESOURCE(resc);
1205 resn = ACPI_NEXT_RESOURCE(resn);
1206 resp = ACPI_NEXT_RESOURCE(resp);
1207 delta = (UINT8 *)resn - (UINT8 *)bufn.Pointer;
1208 if (delta >=
1209 bufn.Length-ACPI_RS_SIZE(ACPI_RESOURCE_DATA)) {
1210 bufn.Length *= 2;
1211 bufn.Pointer = realloc(bufn.Pointer, bufn.Length,
1212 M_ACPI, M_WAITOK);
1213 resn = (ACPI_RESOURCE *)((UINT8 *)bufn.Pointer + delta);
1214 }
1215 }
1216 if (resc->Type != ACPI_RESOURCE_TYPE_END_TAG) {
1217 printf("acpi_allocate_resources: resc not exhausted\n");
1218 rv = AE_BAD_DATA;
1219 goto out3;
1220 }
1221
1222 resn->Type = ACPI_RESOURCE_TYPE_END_TAG;
1223 rv = AcpiSetCurrentResources(handle, &bufn);
1224 if (ACPI_FAILURE(rv)) {
1225 printf("acpi_allocate_resources: AcpiSetCurrentResources %s\n",
1226 AcpiFormatException(rv));
1227 }
1228
1229 out3:
1230 free(bufn.Pointer, M_ACPI);
1231 out2:
1232 AcpiOsFree(bufc.Pointer);
1233 out1:
1234 AcpiOsFree(bufp.Pointer);
1235 out:
1236 return rv;
1237 }
1238 #endif /* ACPI_ACTIVATE_DEV */
1239
1240 SYSCTL_SETUP(sysctl_acpi_setup, "sysctl hw.acpi subtree setup")
1241 {
1242 const struct sysctlnode *node;
1243 const struct sysctlnode *ssnode;
1244
1245 if (sysctl_createv(clog, 0, NULL, NULL,
1246 CTLFLAG_PERMANENT,
1247 CTLTYPE_NODE, "hw", NULL,
1248 NULL, 0, NULL, 0,
1249 CTL_HW, CTL_EOL) != 0)
1250 return;
1251
1252 if (sysctl_createv(clog, 0, NULL, &node,
1253 CTLFLAG_PERMANENT,
1254 CTLTYPE_NODE, "acpi", NULL,
1255 NULL, 0, NULL, 0,
1256 CTL_HW, CTL_CREATE, CTL_EOL) != 0)
1257 return;
1258
1259 acpi_node = node->sysctl_num;
1260
1261 /* ACPI sleepstate sysctl */
1262 if (sysctl_createv(NULL, 0, NULL, &node,
1263 CTLFLAG_PERMANENT,
1264 CTLTYPE_NODE, "machdep", NULL,
1265 NULL, 0, NULL, 0, CTL_MACHDEP, CTL_EOL) != 0)
1266 return;
1267 if (sysctl_createv(NULL, 0, &node, &ssnode,
1268 CTLFLAG_READWRITE, CTLTYPE_INT, "sleep_state",
1269 NULL, sysctl_hw_acpi_sleepstate, 0, NULL, 0, CTL_CREATE,
1270 CTL_EOL) != 0)
1271 return;
1272 }
1273
1274 static int
1275 sysctl_hw_acpi_sleepstate(SYSCTLFN_ARGS)
1276 {
1277 int error, t;
1278 struct sysctlnode node;
1279
1280 node = *rnode;
1281 t = acpi_sleepstate;
1282 node.sysctl_data = &t;
1283 error = sysctl_lookup(SYSCTLFN_CALL(&node));
1284 if (error || newp == NULL)
1285 return error;
1286
1287 if (acpi_softc == NULL)
1288 return ENOSYS;
1289
1290 acpi_enter_sleep_state(acpi_softc, t);
1291
1292 return 0;
1293 }
1294