Home | History | Annotate | Line # | Download | only in acpi
acpi.c revision 1.219
      1 /*	$NetBSD: acpi.c,v 1.219 2010/09/06 15:54:26 jmcneill Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2003, 2007 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  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * Copyright (c) 2003 Wasabi Systems, Inc.
     34  * All rights reserved.
     35  *
     36  * Written by Frank van der Linden for Wasabi Systems, Inc.
     37  *
     38  * Redistribution and use in source and binary forms, with or without
     39  * modification, are permitted provided that the following conditions
     40  * are met:
     41  * 1. Redistributions of source code must retain the above copyright
     42  *    notice, this list of conditions and the following disclaimer.
     43  * 2. Redistributions in binary form must reproduce the above copyright
     44  *    notice, this list of conditions and the following disclaimer in the
     45  *    documentation and/or other materials provided with the distribution.
     46  * 3. All advertising materials mentioning features or use of this software
     47  *    must display the following acknowledgement:
     48  *      This product includes software developed for the NetBSD Project by
     49  *      Wasabi Systems, Inc.
     50  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     51  *    or promote products derived from this software without specific prior
     52  *    written permission.
     53  *
     54  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     55  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     56  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     57  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     58  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     59  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     60  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     61  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     62  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     63  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     64  * POSSIBILITY OF SUCH DAMAGE.
     65  */
     66 
     67 /*
     68  * Copyright 2001, 2003 Wasabi Systems, Inc.
     69  * All rights reserved.
     70  *
     71  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
     72  *
     73  * Redistribution and use in source and binary forms, with or without
     74  * modification, are permitted provided that the following conditions
     75  * are met:
     76  * 1. Redistributions of source code must retain the above copyright
     77  *    notice, this list of conditions and the following disclaimer.
     78  * 2. Redistributions in binary form must reproduce the above copyright
     79  *    notice, this list of conditions and the following disclaimer in the
     80  *    documentation and/or other materials provided with the distribution.
     81  * 3. All advertising materials mentioning features or use of this software
     82  *    must display the following acknowledgement:
     83  *	This product includes software developed for the NetBSD Project by
     84  *	Wasabi Systems, Inc.
     85  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     86  *    or promote products derived from this software without specific prior
     87  *    written permission.
     88  *
     89  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     90  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     91  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     92  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     93  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     94  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     95  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     96  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     97  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     98  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     99  * POSSIBILITY OF SUCH DAMAGE.
    100  */
    101 
    102 #include <sys/cdefs.h>
    103 __KERNEL_RCSID(0, "$NetBSD: acpi.c,v 1.219 2010/09/06 15:54:26 jmcneill Exp $");
    104 
    105 #include "opt_acpi.h"
    106 #include "opt_pcifixup.h"
    107 
    108 #include <sys/param.h>
    109 #include <sys/device.h>
    110 #include <sys/kernel.h>
    111 #include <sys/malloc.h>
    112 #include <sys/module.h>
    113 #include <sys/mutex.h>
    114 #include <sys/sysctl.h>
    115 #include <sys/systm.h>
    116 #include <sys/timetc.h>
    117 
    118 #include <dev/acpi/acpireg.h>
    119 #include <dev/acpi/acpivar.h>
    120 #include <dev/acpi/acpi_osd.h>
    121 #include <dev/acpi/acpi_pci.h>
    122 #include <dev/acpi/acpi_power.h>
    123 #include <dev/acpi/acpi_timer.h>
    124 #include <dev/acpi/acpi_wakedev.h>
    125 
    126 #define _COMPONENT	ACPI_BUS_COMPONENT
    127 ACPI_MODULE_NAME	("acpi")
    128 
    129 #if defined(ACPI_PCI_FIXUP)
    130 #error The option ACPI_PCI_FIXUP has been obsoleted by PCI_INTR_FIXUP_DISABLED.  Please adjust your kernel configuration file.
    131 #endif
    132 
    133 #ifdef PCI_INTR_FIXUP_DISABLED
    134 #include <dev/pci/pcidevs.h>
    135 #endif
    136 
    137 MALLOC_DECLARE(M_ACPI);
    138 
    139 #include <machine/acpi_machdep.h>
    140 
    141 #ifdef ACPI_DEBUGGER
    142 #define	ACPI_DBGR_INIT		0x01
    143 #define	ACPI_DBGR_TABLES	0x02
    144 #define	ACPI_DBGR_ENABLE	0x04
    145 #define	ACPI_DBGR_PROBE		0x08
    146 #define	ACPI_DBGR_RUNNING	0x10
    147 
    148 static int acpi_dbgr = 0x00;
    149 #endif
    150 
    151 /*
    152  * This is a flag we set when the ACPI subsystem is active.  Machine
    153  * dependent code may wish to skip other steps (such as attaching
    154  * subsystems that ACPI supercedes) when ACPI is active.
    155  */
    156 int	acpi_active;
    157 
    158 int	acpi_force_load = 0;
    159 int	acpi_suspended = 0;
    160 int	acpi_verbose_loaded = 0;
    161 
    162 struct acpi_softc	*acpi_softc;
    163 static uint64_t		 acpi_root_pointer;
    164 extern kmutex_t		 acpi_interrupt_list_mtx;
    165 extern struct		 cfdriver acpi_cd;
    166 static ACPI_HANDLE	 acpi_scopes[4];
    167 ACPI_TABLE_HEADER	*madt_header;
    168 
    169 /*
    170  * This structure provides a context for the ACPI
    171  * namespace walk performed in acpi_build_tree().
    172  */
    173 struct acpi_walkcontext {
    174 	struct acpi_softc	*aw_sc;
    175 	struct acpi_devnode	*aw_parent;
    176 };
    177 
    178 /*
    179  * Ignored HIDs.
    180  */
    181 static const char * const acpi_ignored_ids[] = {
    182 #if defined(i386) || defined(x86_64)
    183 	"PNP0000",	/* AT interrupt controller is handled internally */
    184 	"PNP0200",	/* AT DMA controller is handled internally */
    185 	"PNP0A??",	/* PCI Busses are handled internally */
    186 	"PNP0B00",	/* AT RTC is handled internally */
    187 	"PNP0C0F",	/* ACPI PCI link devices are handled internally */
    188 #endif
    189 #if defined(x86_64)
    190 	"PNP0C04",	/* FPU is handled internally */
    191 #endif
    192 	NULL
    193 };
    194 
    195 static int		acpi_match(device_t, cfdata_t, void *);
    196 static int		acpi_submatch(device_t, cfdata_t, const int *, void *);
    197 static void		acpi_attach(device_t, device_t, void *);
    198 static int		acpi_detach(device_t, int);
    199 static void		acpi_childdet(device_t, device_t);
    200 static bool		acpi_suspend(device_t, const pmf_qual_t *);
    201 static bool		acpi_resume(device_t, const pmf_qual_t *);
    202 
    203 static void		acpi_build_tree(struct acpi_softc *);
    204 static ACPI_STATUS	acpi_make_devnode(ACPI_HANDLE, uint32_t,
    205 					  void *, void **);
    206 static ACPI_STATUS	acpi_make_devnode_post(ACPI_HANDLE, uint32_t,
    207 					       void *, void **);
    208 
    209 #ifdef ACPI_ACTIVATE_DEV
    210 static void		acpi_activate_device(ACPI_HANDLE, ACPI_DEVICE_INFO **);
    211 static ACPI_STATUS	acpi_allocate_resources(ACPI_HANDLE);
    212 #endif
    213 
    214 static int		acpi_rescan(device_t, const char *, const int *);
    215 static void		acpi_rescan_nodes(struct acpi_softc *);
    216 static void		acpi_rescan_capabilities(struct acpi_softc *);
    217 static int		acpi_print(void *aux, const char *);
    218 
    219 static void		acpi_notify_handler(ACPI_HANDLE, uint32_t, void *);
    220 
    221 static void		acpi_register_fixed_button(struct acpi_softc *, int);
    222 static void		acpi_deregister_fixed_button(struct acpi_softc *, int);
    223 static uint32_t		acpi_fixed_button_handler(void *);
    224 static void		acpi_fixed_button_pressed(void *);
    225 
    226 static void		acpi_sleep_init(struct acpi_softc *);
    227 
    228 static int		sysctl_hw_acpi_fixedstats(SYSCTLFN_PROTO);
    229 static int		sysctl_hw_acpi_sleepstate(SYSCTLFN_PROTO);
    230 static int		sysctl_hw_acpi_sleepstates(SYSCTLFN_PROTO);
    231 
    232 static bool		  acpi_is_scope(struct acpi_devnode *);
    233 static ACPI_TABLE_HEADER *acpi_map_rsdt(void);
    234 static void		  acpi_unmap_rsdt(ACPI_TABLE_HEADER *);
    235 
    236 void			acpi_print_verbose_stub(struct acpi_softc *);
    237 void			acpi_print_dev_stub(const char *);
    238 
    239 void (*acpi_print_verbose)(struct acpi_softc *) = acpi_print_verbose_stub;
    240 void (*acpi_print_dev)(const char *) = acpi_print_dev_stub;
    241 
    242 CFATTACH_DECL2_NEW(acpi, sizeof(struct acpi_softc),
    243     acpi_match, acpi_attach, acpi_detach, NULL, acpi_rescan, acpi_childdet);
    244 
    245 /*
    246  * Probe for ACPI support.
    247  *
    248  * This is called by the machine-dependent ACPI front-end.
    249  * Note: this is not an autoconfiguration interface function.
    250  */
    251 int
    252 acpi_probe(void)
    253 {
    254 	ACPI_TABLE_HEADER *rsdt;
    255 	const char *func;
    256 	static int once;
    257 	bool initialized;
    258 	ACPI_STATUS rv;
    259 
    260 	if (once != 0)
    261 		panic("%s: already probed", __func__);
    262 
    263 	once = 1;
    264 	func = NULL;
    265 	initialized = false;
    266 
    267 	mutex_init(&acpi_interrupt_list_mtx, MUTEX_DEFAULT, IPL_NONE);
    268 
    269 	/*
    270 	 * Start up ACPICA.
    271 	 */
    272 #ifdef ACPI_DEBUGGER
    273 	if (acpi_dbgr & ACPI_DBGR_INIT)
    274 		acpi_osd_debugger();
    275 #endif
    276 
    277 	CTASSERT(TRUE == true);
    278 	CTASSERT(FALSE == false);
    279 
    280 	AcpiGbl_AllMethodsSerialized = false;
    281 	AcpiGbl_EnableInterpreterSlack = true;
    282 
    283 	rv = AcpiInitializeSubsystem();
    284 
    285 	if (ACPI_SUCCESS(rv))
    286 		initialized = true;
    287 	else {
    288 		func = "AcpiInitializeSubsystem()";
    289 		goto fail;
    290 	}
    291 
    292 	/*
    293 	 * Allocate space for RSDT/XSDT and DSDT,
    294 	 * but allow resizing if more tables exist.
    295 	 */
    296 	rv = AcpiInitializeTables(NULL, 2, true);
    297 
    298 	if (ACPI_FAILURE(rv)) {
    299 		func = "AcpiInitializeTables()";
    300 		goto fail;
    301 	}
    302 
    303 #ifdef ACPI_DEBUGGER
    304 	if (acpi_dbgr & ACPI_DBGR_TABLES)
    305 		acpi_osd_debugger();
    306 #endif
    307 
    308 	rv = AcpiLoadTables();
    309 
    310 	if (ACPI_FAILURE(rv)) {
    311 		func = "AcpiLoadTables()";
    312 		goto fail;
    313 	}
    314 
    315 	rsdt = acpi_map_rsdt();
    316 
    317 	if (rsdt == NULL) {
    318 		func = "acpi_map_rsdt()";
    319 		rv = AE_ERROR;
    320 		goto fail;
    321 	}
    322 
    323 	if (acpi_force_load == 0 && (acpi_find_quirks() & ACPI_QUIRK_BROKEN)) {
    324 		aprint_normal("ACPI: BIOS is listed as broken:\n");
    325 		aprint_normal("ACPI: X/RSDT: OemId <%6.6s,%8.8s,%08x>, "
    326 		       "AslId <%4.4s,%08x>\n",
    327 			rsdt->OemId, rsdt->OemTableId,
    328 		        rsdt->OemRevision,
    329 			rsdt->AslCompilerId,
    330 		        rsdt->AslCompilerRevision);
    331 		aprint_normal("ACPI: Not used. Set acpi_force_load to use.\n");
    332 		acpi_unmap_rsdt(rsdt);
    333 		AcpiTerminate();
    334 		return 0;
    335 	}
    336 	if (acpi_force_load == 0 && (acpi_find_quirks() & ACPI_QUIRK_OLDBIOS)) {
    337 		aprint_normal("ACPI: BIOS is too old (%s). Set acpi_force_load to use.\n",
    338 		    pmf_get_platform("firmware-date"));
    339 		acpi_unmap_rsdt(rsdt);
    340 		AcpiTerminate();
    341 		return 0;
    342 	}
    343 
    344 	acpi_unmap_rsdt(rsdt);
    345 
    346 	rv = AcpiEnableSubsystem(~(ACPI_NO_HARDWARE_INIT|ACPI_NO_ACPI_ENABLE));
    347 
    348 	if (ACPI_FAILURE(rv)) {
    349 		func = "AcpiEnableSubsystem()";
    350 		goto fail;
    351 	}
    352 
    353 	/*
    354 	 * Looks like we have ACPI!
    355 	 */
    356 	return 1;
    357 
    358 fail:
    359 	KASSERT(rv != AE_OK);
    360 	KASSERT(func != NULL);
    361 
    362 	aprint_error("%s: failed to probe ACPI: %s\n",
    363 	    func, AcpiFormatException(rv));
    364 
    365 	if (initialized != false)
    366 		(void)AcpiTerminate();
    367 
    368 	return 0;
    369 }
    370 
    371 void
    372 acpi_disable(void)
    373 {
    374 
    375 	if (AcpiGbl_FADT.SmiCommand != 0)
    376 		AcpiDisable();
    377 }
    378 
    379 int
    380 acpi_check(device_t parent, const char *ifattr)
    381 {
    382 	return (config_search_ia(acpi_submatch, parent, ifattr, NULL) != NULL);
    383 }
    384 
    385 /*
    386  * Autoconfiguration.
    387  */
    388 static int
    389 acpi_match(device_t parent, cfdata_t match, void *aux)
    390 {
    391 	/*
    392 	 * XXX: Nada; MD code has called acpi_probe().
    393 	 */
    394 	return 1;
    395 }
    396 
    397 static int
    398 acpi_submatch(device_t parent, cfdata_t cf, const int *locs, void *aux)
    399 {
    400 	struct cfattach *ca;
    401 
    402 	ca = config_cfattach_lookup(cf->cf_name, cf->cf_atname);
    403 
    404 	return (ca == &acpi_ca);
    405 }
    406 
    407 static void
    408 acpi_attach(device_t parent, device_t self, void *aux)
    409 {
    410 	struct acpi_softc *sc = device_private(self);
    411 	struct acpibus_attach_args *aa = aux;
    412 	ACPI_TABLE_HEADER *rsdt;
    413 	ACPI_STATUS rv;
    414 
    415 	aprint_naive("\n");
    416 	aprint_normal(": Intel ACPICA %08x\n", ACPI_CA_VERSION);
    417 
    418 	if (acpi_softc != NULL)
    419 		panic("%s: already attached", __func__);
    420 
    421 	rsdt = acpi_map_rsdt();
    422 
    423 	if (rsdt == NULL)
    424 		aprint_error_dev(self, "X/RSDT: Not found\n");
    425 	else {
    426 		aprint_verbose_dev(self,
    427 		    "X/RSDT: OemId <%6.6s,%8.8s,%08x>, AslId <%4.4s,%08x>\n",
    428 		    rsdt->OemId, rsdt->OemTableId,
    429 		    rsdt->OemRevision,
    430 		    rsdt->AslCompilerId, rsdt->AslCompilerRevision);
    431 	}
    432 
    433 	acpi_unmap_rsdt(rsdt);
    434 
    435 	sc->sc_dev = self;
    436 	sc->sc_root = NULL;
    437 
    438 	sc->sc_sleepstate = ACPI_STATE_S0;
    439 	sc->sc_quirks = acpi_find_quirks();
    440 
    441 	sysmon_power_settype("acpi");
    442 
    443 	sc->sc_iot = aa->aa_iot;
    444 	sc->sc_memt = aa->aa_memt;
    445 	sc->sc_pc = aa->aa_pc;
    446 	sc->sc_pciflags = aa->aa_pciflags;
    447 	sc->sc_ic = aa->aa_ic;
    448 
    449 	SIMPLEQ_INIT(&sc->ad_head);
    450 
    451 	acpi_softc = sc;
    452 
    453 	if (pmf_device_register(self, acpi_suspend, acpi_resume) != true)
    454 		aprint_error_dev(self, "couldn't establish power handler\n");
    455 
    456 	/*
    457 	 * Bring ACPI on-line.
    458 	 */
    459 #ifdef ACPI_DEBUGGER
    460 	if (acpi_dbgr & ACPI_DBGR_ENABLE)
    461 		acpi_osd_debugger();
    462 #endif
    463 
    464 #define ACPI_ENABLE_PHASE1 \
    465     (ACPI_NO_HANDLER_INIT | ACPI_NO_EVENT_INIT)
    466 #define ACPI_ENABLE_PHASE2 \
    467     (ACPI_NO_HARDWARE_INIT | ACPI_NO_ACPI_ENABLE | \
    468      ACPI_NO_ADDRESS_SPACE_INIT)
    469 
    470 	rv = AcpiEnableSubsystem(ACPI_ENABLE_PHASE1);
    471 
    472 	if (ACPI_FAILURE(rv))
    473 		goto fail;
    474 
    475 	acpi_md_callback();
    476 
    477 	rv = AcpiEnableSubsystem(ACPI_ENABLE_PHASE2);
    478 
    479 	if (ACPI_FAILURE(rv))
    480 		goto fail;
    481 
    482 	/*
    483 	 * Early EC handler initialization if ECDT table is available.
    484 	 */
    485 	config_found_ia(self, "acpiecdtbus", aa, NULL);
    486 
    487 	rv = AcpiInitializeObjects(ACPI_FULL_INITIALIZATION);
    488 
    489 	if (ACPI_FAILURE(rv))
    490 		goto fail;
    491 
    492 	/*
    493 	 * Install global notify handlers.
    494 	 */
    495 	rv = AcpiInstallNotifyHandler(ACPI_ROOT_OBJECT,
    496 	    ACPI_SYSTEM_NOTIFY, acpi_notify_handler, NULL);
    497 
    498 	if (ACPI_FAILURE(rv))
    499 		goto fail;
    500 
    501 	rv = AcpiInstallNotifyHandler(ACPI_ROOT_OBJECT,
    502 	    ACPI_DEVICE_NOTIFY, acpi_notify_handler, NULL);
    503 
    504 	if (ACPI_FAILURE(rv))
    505 		goto fail;
    506 
    507 	acpi_active = 1;
    508 
    509 	/* Show SCI interrupt. */
    510 	aprint_verbose_dev(self, "SCI interrupting at int %u\n",
    511 	    AcpiGbl_FADT.SciInterrupt);
    512 
    513 	/*
    514 	 * Install fixed-event handlers.
    515 	 */
    516 	acpi_register_fixed_button(sc, ACPI_EVENT_POWER_BUTTON);
    517 	acpi_register_fixed_button(sc, ACPI_EVENT_SLEEP_BUTTON);
    518 
    519 	acpitimer_init(sc);
    520 
    521 #ifdef ACPI_DEBUGGER
    522 	if (acpi_dbgr & ACPI_DBGR_PROBE)
    523 		acpi_osd_debugger();
    524 #endif
    525 
    526 	/*
    527 	 * Scan the namespace and build our device tree.
    528 	 */
    529 	acpi_build_tree(sc);
    530 	acpi_sleep_init(sc);
    531 
    532 #ifdef ACPI_DEBUGGER
    533 	if (acpi_dbgr & ACPI_DBGR_RUNNING)
    534 		acpi_osd_debugger();
    535 #endif
    536 
    537 #ifdef ACPI_DEBUG
    538 	acpi_debug_init();
    539 #endif
    540 
    541 	/*
    542 	 * Print debug information.
    543 	 */
    544 	acpi_print_verbose(sc);
    545 
    546 	return;
    547 
    548 fail:
    549 	aprint_error("%s: failed to initialize ACPI: %s\n",
    550 	    __func__, AcpiFormatException(rv));
    551 }
    552 
    553 /*
    554  * XXX: This is incomplete.
    555  */
    556 static int
    557 acpi_detach(device_t self, int flags)
    558 {
    559 	struct acpi_softc *sc = device_private(self);
    560 	ACPI_STATUS rv;
    561 	int rc;
    562 
    563 	rv = AcpiRemoveNotifyHandler(ACPI_ROOT_OBJECT,
    564 	    ACPI_SYSTEM_NOTIFY, acpi_notify_handler);
    565 
    566 	if (ACPI_FAILURE(rv))
    567 		return EBUSY;
    568 
    569 	rv = AcpiRemoveNotifyHandler(ACPI_ROOT_OBJECT,
    570 	    ACPI_DEVICE_NOTIFY, acpi_notify_handler);
    571 
    572 	if (ACPI_FAILURE(rv))
    573 		return EBUSY;
    574 
    575 	if ((rc = config_detach_children(self, flags)) != 0)
    576 		return rc;
    577 
    578 	if ((rc = acpitimer_detach()) != 0)
    579 		return rc;
    580 
    581 	acpi_deregister_fixed_button(sc, ACPI_EVENT_POWER_BUTTON);
    582 	acpi_deregister_fixed_button(sc, ACPI_EVENT_SLEEP_BUTTON);
    583 
    584 	pmf_device_deregister(self);
    585 
    586 	acpi_softc = NULL;
    587 
    588 	return 0;
    589 }
    590 
    591 /*
    592  * XXX: Need to reclaim any resources? Yes.
    593  */
    594 static void
    595 acpi_childdet(device_t self, device_t child)
    596 {
    597 	struct acpi_softc *sc = device_private(self);
    598 	struct acpi_devnode *ad;
    599 
    600 	if (sc->sc_apmbus == child)
    601 		sc->sc_apmbus = NULL;
    602 
    603 	SIMPLEQ_FOREACH(ad, &sc->ad_head, ad_list) {
    604 
    605 		if (ad->ad_device == child)
    606 			ad->ad_device = NULL;
    607 	}
    608 }
    609 
    610 static bool
    611 acpi_suspend(device_t dv, const pmf_qual_t *qual)
    612 {
    613 
    614 	acpi_suspended = 1;
    615 
    616 	return true;
    617 }
    618 
    619 static bool
    620 acpi_resume(device_t dv, const pmf_qual_t *qual)
    621 {
    622 
    623 	acpi_suspended = 0;
    624 
    625 	return true;
    626 }
    627 
    628 /*
    629  * Namespace scan.
    630  */
    631 static void
    632 acpi_build_tree(struct acpi_softc *sc)
    633 {
    634 	struct acpi_walkcontext awc;
    635 
    636 	/*
    637 	 * Get the root scope handles.
    638 	 */
    639 	KASSERT(__arraycount(acpi_scopes) == 4);
    640 
    641 	(void)AcpiGetHandle(ACPI_ROOT_OBJECT, "\\_PR_", &acpi_scopes[0]);
    642 	(void)AcpiGetHandle(ACPI_ROOT_OBJECT, "\\_SB_", &acpi_scopes[1]);
    643 	(void)AcpiGetHandle(ACPI_ROOT_OBJECT, "\\_SI_", &acpi_scopes[2]);
    644 	(void)AcpiGetHandle(ACPI_ROOT_OBJECT, "\\_TZ_", &acpi_scopes[3]);
    645 
    646 	/*
    647 	 * Make the root node.
    648 	 */
    649 	awc.aw_sc = sc;
    650 	awc.aw_parent = NULL;
    651 
    652 	(void)acpi_make_devnode(ACPI_ROOT_OBJECT, 0, &awc, NULL);
    653 
    654 	KASSERT(sc->sc_root == NULL);
    655 	KASSERT(awc.aw_parent != NULL);
    656 
    657 	sc->sc_root = awc.aw_parent;
    658 
    659 	/*
    660 	 * Build the internal namespace.
    661 	 */
    662 	(void)AcpiWalkNamespace(ACPI_TYPE_ANY, ACPI_ROOT_OBJECT, UINT32_MAX,
    663 	    acpi_make_devnode, acpi_make_devnode_post, &awc, NULL);
    664 
    665 	/*
    666 	 * Scan the internal namespace.
    667 	 */
    668 	(void)acpi_rescan(sc->sc_dev, NULL, NULL);
    669 
    670 	acpi_rescan_capabilities(sc);
    671 
    672 	(void)acpi_pcidev_scan(sc->sc_root);
    673 }
    674 
    675 static ACPI_STATUS
    676 acpi_make_devnode(ACPI_HANDLE handle, uint32_t level,
    677     void *context, void **status)
    678 {
    679 	struct acpi_walkcontext *awc = context;
    680 	struct acpi_softc *sc = awc->aw_sc;
    681 	struct acpi_devnode *ad;
    682 	ACPI_DEVICE_INFO *devinfo;
    683 	ACPI_OBJECT_TYPE type;
    684 	ACPI_NAME_UNION *anu;
    685 	ACPI_STATUS rv;
    686 	int clear, i;
    687 
    688 	rv = AcpiGetObjectInfo(handle, &devinfo);
    689 
    690 	if (ACPI_FAILURE(rv))
    691 		return AE_OK;	/* Do not terminate the walk. */
    692 
    693 	type = devinfo->Type;
    694 
    695 	switch (type) {
    696 
    697 	case ACPI_TYPE_DEVICE:
    698 
    699 #ifdef ACPI_ACTIVATE_DEV
    700 		acpi_activate_device(handle, &devinfo);
    701 #endif
    702 
    703 	case ACPI_TYPE_PROCESSOR:
    704 	case ACPI_TYPE_THERMAL:
    705 	case ACPI_TYPE_POWER:
    706 
    707 		ad = malloc(sizeof(*ad), M_ACPI, M_NOWAIT | M_ZERO);
    708 
    709 		if (ad == NULL)
    710 			return AE_NO_MEMORY;
    711 
    712 		ad->ad_device = NULL;
    713 		ad->ad_notify = NULL;
    714 		ad->ad_pciinfo = NULL;
    715 
    716 		ad->ad_type = type;
    717 		ad->ad_handle = handle;
    718 		ad->ad_devinfo = devinfo;
    719 
    720 		ad->ad_root = sc->sc_dev;
    721 		ad->ad_parent = awc->aw_parent;
    722 
    723 		anu = (ACPI_NAME_UNION *)&devinfo->Name;
    724 		ad->ad_name[4] = '\0';
    725 
    726 		for (i = 3, clear = 0; i >= 0; i--) {
    727 
    728 			if (clear == 0 && anu->Ascii[i] == '_')
    729 				ad->ad_name[i] = '\0';
    730 			else {
    731 				ad->ad_name[i] = anu->Ascii[i];
    732 				clear = 1;
    733 			}
    734 		}
    735 
    736 		if (ad->ad_name[0] == '\0')
    737 			ad->ad_name[0] = '_';
    738 
    739 		SIMPLEQ_INIT(&ad->ad_child_head);
    740 		SIMPLEQ_INSERT_TAIL(&sc->ad_head, ad, ad_list);
    741 
    742 		acpi_set_node(ad);
    743 
    744 		if (ad->ad_parent != NULL) {
    745 
    746 			SIMPLEQ_INSERT_TAIL(&ad->ad_parent->ad_child_head,
    747 			    ad, ad_child_list);
    748 		}
    749 
    750 		awc->aw_parent = ad;
    751 	}
    752 
    753 	return AE_OK;
    754 }
    755 
    756 static ACPI_STATUS
    757 acpi_make_devnode_post(ACPI_HANDLE handle, uint32_t level,
    758     void *context, void **status)
    759 {
    760 	struct acpi_walkcontext *awc = context;
    761 
    762 	KASSERT(awc != NULL);
    763 	KASSERT(awc->aw_parent != NULL);
    764 
    765 	if (handle == awc->aw_parent->ad_handle)
    766 		awc->aw_parent = awc->aw_parent->ad_parent;
    767 
    768 	return AE_OK;
    769 }
    770 
    771 #ifdef ACPI_ACTIVATE_DEV
    772 static void
    773 acpi_activate_device(ACPI_HANDLE handle, ACPI_DEVICE_INFO **di)
    774 {
    775 	static const int valid = ACPI_VALID_STA | ACPI_VALID_HID;
    776 	ACPI_DEVICE_INFO *newdi;
    777 	ACPI_STATUS rv;
    778 	uint32_t old;
    779 
    780 	/*
    781 	 * If the device is valid and present,
    782 	 * but not enabled, try to activate it.
    783 	 */
    784 	if (((*di)->Valid & valid) != valid)
    785 		return;
    786 
    787 	old = (*di)->CurrentStatus;
    788 
    789 	if ((old & (ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_ENABLED)) !=
    790 	    ACPI_STA_DEVICE_PRESENT)
    791 		return;
    792 
    793 	rv = acpi_allocate_resources(handle);
    794 
    795 	if (ACPI_FAILURE(rv))
    796 		goto fail;
    797 
    798 	rv = AcpiGetObjectInfo(handle, &newdi);
    799 
    800 	if (ACPI_FAILURE(rv))
    801 		goto fail;
    802 
    803 	ACPI_FREE(*di);
    804 	*di = newdi;
    805 
    806 	aprint_verbose_dev(acpi_softc->sc_dev,
    807 	    "%s activated, STA 0x%08X -> STA 0x%08X\n",
    808 	    (*di)->HardwareId.String, old, (*di)->CurrentStatus);
    809 
    810 	return;
    811 
    812 fail:
    813 	aprint_error_dev(acpi_softc->sc_dev, "failed to "
    814 	    "activate %s\n", (*di)->HardwareId.String);
    815 }
    816 
    817 /*
    818  * XXX: This very incomplete.
    819  */
    820 ACPI_STATUS
    821 acpi_allocate_resources(ACPI_HANDLE handle)
    822 {
    823 	ACPI_BUFFER bufp, bufc, bufn;
    824 	ACPI_RESOURCE *resp, *resc, *resn;
    825 	ACPI_RESOURCE_IRQ *irq;
    826 	ACPI_RESOURCE_EXTENDED_IRQ *xirq;
    827 	ACPI_STATUS rv;
    828 	uint delta;
    829 
    830 	rv = acpi_get(handle, &bufp, AcpiGetPossibleResources);
    831 	if (ACPI_FAILURE(rv))
    832 		goto out;
    833 	rv = acpi_get(handle, &bufc, AcpiGetCurrentResources);
    834 	if (ACPI_FAILURE(rv)) {
    835 		goto out1;
    836 	}
    837 
    838 	bufn.Length = 1000;
    839 	bufn.Pointer = resn = malloc(bufn.Length, M_ACPI, M_WAITOK);
    840 	resp = bufp.Pointer;
    841 	resc = bufc.Pointer;
    842 	while (resc->Type != ACPI_RESOURCE_TYPE_END_TAG &&
    843 	       resp->Type != ACPI_RESOURCE_TYPE_END_TAG) {
    844 		while (resc->Type != resp->Type && resp->Type != ACPI_RESOURCE_TYPE_END_TAG)
    845 			resp = ACPI_NEXT_RESOURCE(resp);
    846 		if (resp->Type == ACPI_RESOURCE_TYPE_END_TAG)
    847 			break;
    848 		/* Found identical Id */
    849 		resn->Type = resc->Type;
    850 		switch (resc->Type) {
    851 		case ACPI_RESOURCE_TYPE_IRQ:
    852 			memcpy(&resn->Data, &resp->Data,
    853 			       sizeof(ACPI_RESOURCE_IRQ));
    854 			irq = (ACPI_RESOURCE_IRQ *)&resn->Data;
    855 			irq->Interrupts[0] =
    856 			    ((ACPI_RESOURCE_IRQ *)&resp->Data)->
    857 			        Interrupts[irq->InterruptCount-1];
    858 			irq->InterruptCount = 1;
    859 			resn->Length = ACPI_RS_SIZE(ACPI_RESOURCE_IRQ);
    860 			break;
    861 		case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
    862 			memcpy(&resn->Data, &resp->Data,
    863 			       sizeof(ACPI_RESOURCE_EXTENDED_IRQ));
    864 			xirq = (ACPI_RESOURCE_EXTENDED_IRQ *)&resn->Data;
    865 #if 0
    866 			/*
    867 			 * XXX:	Not duplicating the interrupt logic above
    868 			 *	because its not clear what it accomplishes.
    869 			 */
    870 			xirq->Interrupts[0] =
    871 			    ((ACPI_RESOURCE_EXT_IRQ *)&resp->Data)->
    872 			    Interrupts[irq->NumberOfInterrupts-1];
    873 			xirq->NumberOfInterrupts = 1;
    874 #endif
    875 			resn->Length = ACPI_RS_SIZE(ACPI_RESOURCE_EXTENDED_IRQ);
    876 			break;
    877 		case ACPI_RESOURCE_TYPE_IO:
    878 			memcpy(&resn->Data, &resp->Data,
    879 			       sizeof(ACPI_RESOURCE_IO));
    880 			resn->Length = resp->Length;
    881 			break;
    882 		default:
    883 			aprint_error_dev(acpi_softc->sc_dev,
    884 			    "%s: invalid type %u\n", __func__, resc->Type);
    885 			rv = AE_BAD_DATA;
    886 			goto out2;
    887 		}
    888 		resc = ACPI_NEXT_RESOURCE(resc);
    889 		resn = ACPI_NEXT_RESOURCE(resn);
    890 		resp = ACPI_NEXT_RESOURCE(resp);
    891 		delta = (uint8_t *)resn - (uint8_t *)bufn.Pointer;
    892 		if (delta >=
    893 		    bufn.Length-ACPI_RS_SIZE(ACPI_RESOURCE_DATA)) {
    894 			bufn.Length *= 2;
    895 			bufn.Pointer = realloc(bufn.Pointer, bufn.Length,
    896 					       M_ACPI, M_WAITOK);
    897 			resn = (ACPI_RESOURCE *)((uint8_t *)bufn.Pointer +
    898 			    delta);
    899 		}
    900 	}
    901 
    902 	if (resc->Type != ACPI_RESOURCE_TYPE_END_TAG) {
    903 		aprint_error_dev(acpi_softc->sc_dev,
    904 		    "%s: resc not exhausted\n", __func__);
    905 		rv = AE_BAD_DATA;
    906 		goto out3;
    907 	}
    908 
    909 	resn->Type = ACPI_RESOURCE_TYPE_END_TAG;
    910 	rv = AcpiSetCurrentResources(handle, &bufn);
    911 
    912 	if (ACPI_FAILURE(rv))
    913 		aprint_error_dev(acpi_softc->sc_dev, "%s: failed to set "
    914 		    "resources: %s\n", __func__, AcpiFormatException(rv));
    915 
    916 out3:
    917 	free(bufn.Pointer, M_ACPI);
    918 out2:
    919 	ACPI_FREE(bufc.Pointer);
    920 out1:
    921 	ACPI_FREE(bufp.Pointer);
    922 out:
    923 	return rv;
    924 }
    925 #endif /* ACPI_ACTIVATE_DEV */
    926 
    927 /*
    928  * Device attachment.
    929  */
    930 static int
    931 acpi_rescan(device_t self, const char *ifattr, const int *locators)
    932 {
    933 	struct acpi_softc *sc = device_private(self);
    934 
    935 	if (ifattr_match(ifattr, "acpinodebus"))
    936 		acpi_rescan_nodes(sc);
    937 
    938 	if (ifattr_match(ifattr, "acpiapmbus") && sc->sc_apmbus == NULL)
    939 		sc->sc_apmbus = config_found_ia(sc->sc_dev,
    940 		    "acpiapmbus", NULL, NULL);
    941 
    942 	return 0;
    943 }
    944 
    945 static void
    946 acpi_rescan_nodes(struct acpi_softc *sc)
    947 {
    948 	struct acpi_attach_args aa;
    949 	struct acpi_devnode *ad;
    950 	ACPI_DEVICE_INFO *di;
    951 
    952 	SIMPLEQ_FOREACH(ad, &sc->ad_head, ad_list) {
    953 
    954 		if (ad->ad_device != NULL)
    955 			continue;
    956 
    957 		/*
    958 		 * There is a bug in ACPICA: it defines the type
    959 		 * of the scopes incorrectly for its own reasons.
    960 		 */
    961 		if (acpi_is_scope(ad) != false)
    962 			continue;
    963 
    964 		di = ad->ad_devinfo;
    965 
    966 		/*
    967 		 * We only attach devices which are present, enabled, and
    968 		 * functioning properly. However, if a device is enabled,
    969 		 * it is decoding resources and we should claim these,
    970 		 * if possible. This requires changes to bus_space(9).
    971 		 * Note: there is a possible race condition, because _STA
    972 		 * may have changed since di->CurrentStatus was set.
    973 		 */
    974 		if (di->Type == ACPI_TYPE_DEVICE) {
    975 
    976 			if ((di->Valid & ACPI_VALID_STA) != 0 &&
    977 			    (di->CurrentStatus & ACPI_STA_OK) != ACPI_STA_OK)
    978 				continue;
    979 		}
    980 
    981 		/*
    982 		 * The same problem as above. As for example
    983 		 * thermal zones and power resources do not
    984 		 * have a valid HID, only evaluate devices.
    985 		 */
    986 		if (di->Type == ACPI_TYPE_DEVICE &&
    987 		   (di->Valid & ACPI_VALID_HID) == 0)
    988 			continue;
    989 
    990 		/*
    991 		 * Handled internally.
    992 		 */
    993 		if (di->Type == ACPI_TYPE_POWER)
    994 			continue;
    995 
    996 		/*
    997 		 * Skip ignored HIDs.
    998 		 */
    999 		if (acpi_match_hid(di, acpi_ignored_ids))
   1000 			continue;
   1001 
   1002 		aa.aa_node = ad;
   1003 		aa.aa_iot = sc->sc_iot;
   1004 		aa.aa_memt = sc->sc_memt;
   1005 		aa.aa_pc = sc->sc_pc;
   1006 		aa.aa_pciflags = sc->sc_pciflags;
   1007 		aa.aa_ic = sc->sc_ic;
   1008 
   1009 		ad->ad_device = config_found_ia(sc->sc_dev,
   1010 		    "acpinodebus", &aa, acpi_print);
   1011 	}
   1012 }
   1013 
   1014 static void
   1015 acpi_rescan_capabilities(struct acpi_softc *sc)
   1016 {
   1017 	struct acpi_devnode *ad;
   1018 	ACPI_HANDLE tmp;
   1019 	ACPI_STATUS rv;
   1020 
   1021 	SIMPLEQ_FOREACH(ad, &sc->ad_head, ad_list) {
   1022 
   1023 		if (ad->ad_devinfo->Type != ACPI_TYPE_DEVICE)
   1024 			continue;
   1025 
   1026 		/*
   1027 		 * Scan power resource capabilities.
   1028 		 *
   1029 		 * If any power states are supported,
   1030 		 * at least _PR0 and _PR3 must be present.
   1031 		 */
   1032 		rv = AcpiGetHandle(ad->ad_handle, "_PR0", &tmp);
   1033 
   1034 		if (ACPI_SUCCESS(rv)) {
   1035 			ad->ad_flags |= ACPI_DEVICE_POWER;
   1036 			acpi_power_add(ad);
   1037 		}
   1038 
   1039 		/*
   1040 		 * Scan wake-up capabilities.
   1041 		 */
   1042 		rv = AcpiGetHandle(ad->ad_handle, "_PRW", &tmp);
   1043 
   1044 		if (ACPI_SUCCESS(rv)) {
   1045 			ad->ad_flags |= ACPI_DEVICE_WAKEUP;
   1046 			acpi_wakedev_add(ad);
   1047 		}
   1048 	}
   1049 }
   1050 
   1051 static int
   1052 acpi_print(void *aux, const char *pnp)
   1053 {
   1054 	struct acpi_attach_args *aa = aux;
   1055 	ACPI_STATUS rv;
   1056 
   1057 	if (pnp) {
   1058 		if (aa->aa_node->ad_devinfo->Valid & ACPI_VALID_HID) {
   1059 			char *pnpstr =
   1060 			    aa->aa_node->ad_devinfo->HardwareId.String;
   1061 			ACPI_BUFFER buf;
   1062 
   1063 			aprint_normal("%s (%s) ", aa->aa_node->ad_name,
   1064 			    pnpstr);
   1065 
   1066 			rv = acpi_eval_struct(aa->aa_node->ad_handle,
   1067 			    "_STR", &buf);
   1068 			if (ACPI_SUCCESS(rv)) {
   1069 				ACPI_OBJECT *obj = buf.Pointer;
   1070 				switch (obj->Type) {
   1071 				case ACPI_TYPE_STRING:
   1072 					aprint_normal("[%s] ", obj->String.Pointer);
   1073 					break;
   1074 				case ACPI_TYPE_BUFFER:
   1075 					aprint_normal("buffer %p ", obj->Buffer.Pointer);
   1076 					break;
   1077 				default:
   1078 					aprint_normal("type %u ",obj->Type);
   1079 					break;
   1080 				}
   1081 				ACPI_FREE(buf.Pointer);
   1082 			}
   1083 			else
   1084 				acpi_print_dev(pnpstr);
   1085 
   1086 			aprint_normal("at %s", pnp);
   1087 		} else if (aa->aa_node->ad_devinfo->Type != ACPI_TYPE_DEVICE) {
   1088 			aprint_normal("%s (ACPI Object Type '%s' "
   1089 			    "[0x%02x]) ", aa->aa_node->ad_name,
   1090 			     AcpiUtGetTypeName(aa->aa_node->ad_devinfo->Type),
   1091 			     aa->aa_node->ad_devinfo->Type);
   1092 			aprint_normal("at %s", pnp);
   1093 		} else
   1094 			return 0;
   1095 	} else {
   1096 		aprint_normal(" (%s", aa->aa_node->ad_name);
   1097 		if (aa->aa_node->ad_devinfo->Valid & ACPI_VALID_HID) {
   1098 			aprint_normal(", %s", aa->aa_node->ad_devinfo->HardwareId.String);
   1099 			if (aa->aa_node->ad_devinfo->Valid & ACPI_VALID_UID) {
   1100 				const char *uid;
   1101 
   1102 				uid = aa->aa_node->ad_devinfo->UniqueId.String;
   1103 				if (uid[0] == '\0')
   1104 					uid = "<null>";
   1105 				aprint_normal("-%s", uid);
   1106 			}
   1107 		}
   1108 		aprint_normal(")");
   1109 	}
   1110 
   1111 	return UNCONF;
   1112 }
   1113 
   1114 /*
   1115  * Notify.
   1116  */
   1117 static void
   1118 acpi_notify_handler(ACPI_HANDLE handle, uint32_t event, void *aux)
   1119 {
   1120 	struct acpi_softc *sc = acpi_softc;
   1121 	struct acpi_devnode *ad;
   1122 
   1123 	KASSERT(sc != NULL);
   1124 	KASSERT(aux == NULL);
   1125 	KASSERT(acpi_active != 0);
   1126 
   1127 	if (acpi_suspended != 0)
   1128 		return;
   1129 
   1130 	/*
   1131 	 *  System: 0x00 - 0x7F.
   1132 	 *  Device: 0x80 - 0xFF.
   1133 	 */
   1134 	switch (event) {
   1135 
   1136 	case ACPI_NOTIFY_BUS_CHECK:
   1137 	case ACPI_NOTIFY_DEVICE_CHECK:
   1138 	case ACPI_NOTIFY_DEVICE_WAKE:
   1139 	case ACPI_NOTIFY_EJECT_REQUEST:
   1140 	case ACPI_NOTIFY_DEVICE_CHECK_LIGHT:
   1141 	case ACPI_NOTIFY_FREQUENCY_MISMATCH:
   1142 	case ACPI_NOTIFY_BUS_MODE_MISMATCH:
   1143 	case ACPI_NOTIFY_POWER_FAULT:
   1144 	case ACPI_NOTIFY_CAPABILITIES_CHECK:
   1145 	case ACPI_NOTIFY_DEVICE_PLD_CHECK:
   1146 	case ACPI_NOTIFY_RESERVED:
   1147 	case ACPI_NOTIFY_LOCALITY_UPDATE:
   1148 		break;
   1149 	}
   1150 
   1151 	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "notification 0x%02X for "
   1152 		"%s (%p)\n", event, acpi_name(handle), handle));
   1153 
   1154 	/*
   1155 	 * We deliver notifications only to drivers
   1156 	 * that have been succesfully attached and
   1157 	 * that have registered a handler with us.
   1158 	 * The opaque pointer is always the device_t.
   1159 	 */
   1160 	SIMPLEQ_FOREACH(ad, &sc->ad_head, ad_list) {
   1161 
   1162 		if (ad->ad_device == NULL)
   1163 			continue;
   1164 
   1165 		if (ad->ad_notify == NULL)
   1166 			continue;
   1167 
   1168 		if (ad->ad_handle != handle)
   1169 			continue;
   1170 
   1171 		(*ad->ad_notify)(ad->ad_handle, event, ad->ad_device);
   1172 
   1173 		return;
   1174 	}
   1175 
   1176 	aprint_debug_dev(sc->sc_dev, "unhandled notify 0x%02X "
   1177 	    "for %s (%p)\n", event, acpi_name(handle), handle);
   1178 }
   1179 
   1180 bool
   1181 acpi_register_notify(struct acpi_devnode *ad, ACPI_NOTIFY_HANDLER notify)
   1182 {
   1183 	struct acpi_softc *sc = acpi_softc;
   1184 
   1185 	KASSERT(sc != NULL);
   1186 	KASSERT(acpi_active != 0);
   1187 
   1188 	if (acpi_suspended != 0)
   1189 		goto fail;
   1190 
   1191 	if (ad == NULL || notify == NULL)
   1192 		goto fail;
   1193 
   1194 	ad->ad_notify = notify;
   1195 
   1196 	return true;
   1197 
   1198 fail:
   1199 	aprint_error_dev(sc->sc_dev, "failed to register notify "
   1200 	    "handler for %s (%p)\n", ad->ad_name, ad->ad_handle);
   1201 
   1202 	return false;
   1203 }
   1204 
   1205 void
   1206 acpi_deregister_notify(struct acpi_devnode *ad)
   1207 {
   1208 
   1209 	ad->ad_notify = NULL;
   1210 }
   1211 
   1212 /*
   1213  * Fixed buttons.
   1214  */
   1215 static void
   1216 acpi_register_fixed_button(struct acpi_softc *sc, int event)
   1217 {
   1218 	struct sysmon_pswitch *smpsw;
   1219 	ACPI_STATUS rv;
   1220 	int type;
   1221 
   1222 	switch (event) {
   1223 
   1224 	case ACPI_EVENT_POWER_BUTTON:
   1225 
   1226 		if ((AcpiGbl_FADT.Flags & ACPI_FADT_POWER_BUTTON) != 0)
   1227 			return;
   1228 
   1229 		type = PSWITCH_TYPE_POWER;
   1230 		smpsw = &sc->sc_smpsw_power;
   1231 		break;
   1232 
   1233 	case ACPI_EVENT_SLEEP_BUTTON:
   1234 
   1235 		if ((AcpiGbl_FADT.Flags & ACPI_FADT_SLEEP_BUTTON) != 0)
   1236 			return;
   1237 
   1238 		type = PSWITCH_TYPE_SLEEP;
   1239 		smpsw = &sc->sc_smpsw_sleep;
   1240 		break;
   1241 
   1242 	default:
   1243 		rv = AE_TYPE;
   1244 		goto fail;
   1245 	}
   1246 
   1247 	smpsw->smpsw_type = type;
   1248 	smpsw->smpsw_name = device_xname(sc->sc_dev);
   1249 
   1250 	if (sysmon_pswitch_register(smpsw) != 0) {
   1251 		rv = AE_ERROR;
   1252 		goto fail;
   1253 	}
   1254 
   1255 	rv = AcpiInstallFixedEventHandler(event,
   1256 	    acpi_fixed_button_handler, smpsw);
   1257 
   1258 	if (ACPI_FAILURE(rv)) {
   1259 		sysmon_pswitch_unregister(smpsw);
   1260 		goto fail;
   1261 	}
   1262 
   1263 	aprint_debug_dev(sc->sc_dev, "fixed %s button present\n",
   1264 	    (type != ACPI_EVENT_SLEEP_BUTTON) ? "power" : "sleep");
   1265 
   1266 	return;
   1267 
   1268 fail:
   1269 	aprint_error_dev(sc->sc_dev, "failed to register "
   1270 	    "fixed event: %s\n", AcpiFormatException(rv));
   1271 }
   1272 
   1273 static void
   1274 acpi_deregister_fixed_button(struct acpi_softc *sc, int event)
   1275 {
   1276 	struct sysmon_pswitch *smpsw;
   1277 	ACPI_STATUS rv;
   1278 
   1279 	switch (event) {
   1280 
   1281 	case ACPI_EVENT_POWER_BUTTON:
   1282 		smpsw = &sc->sc_smpsw_power;
   1283 
   1284 		if ((AcpiGbl_FADT.Flags & ACPI_FADT_POWER_BUTTON) != 0) {
   1285 			KASSERT(smpsw->smpsw_type != PSWITCH_TYPE_POWER);
   1286 			return;
   1287 		}
   1288 
   1289 		break;
   1290 
   1291 	case ACPI_EVENT_SLEEP_BUTTON:
   1292 		smpsw = &sc->sc_smpsw_sleep;
   1293 
   1294 		if ((AcpiGbl_FADT.Flags & ACPI_FADT_SLEEP_BUTTON) != 0) {
   1295 			KASSERT(smpsw->smpsw_type != PSWITCH_TYPE_SLEEP);
   1296 			return;
   1297 		}
   1298 
   1299 		break;
   1300 
   1301 	default:
   1302 		rv = AE_TYPE;
   1303 		goto fail;
   1304 	}
   1305 
   1306 	rv = AcpiRemoveFixedEventHandler(event, acpi_fixed_button_handler);
   1307 
   1308 	if (ACPI_SUCCESS(rv)) {
   1309 		sysmon_pswitch_unregister(smpsw);
   1310 		return;
   1311 	}
   1312 
   1313 fail:
   1314 	aprint_error_dev(sc->sc_dev, "failed to deregister "
   1315 	    "fixed event: %s\n", AcpiFormatException(rv));
   1316 }
   1317 
   1318 static uint32_t
   1319 acpi_fixed_button_handler(void *context)
   1320 {
   1321 	static const int handler = OSL_NOTIFY_HANDLER;
   1322 	struct sysmon_pswitch *smpsw = context;
   1323 
   1324 	(void)AcpiOsExecute(handler, acpi_fixed_button_pressed, smpsw);
   1325 
   1326 	return ACPI_INTERRUPT_HANDLED;
   1327 }
   1328 
   1329 static void
   1330 acpi_fixed_button_pressed(void *context)
   1331 {
   1332 	struct sysmon_pswitch *smpsw = context;
   1333 
   1334 	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "%s fixed button pressed\n",
   1335 		(smpsw->smpsw_type != ACPI_EVENT_SLEEP_BUTTON) ?
   1336 		"power" : "sleep"));
   1337 
   1338 	sysmon_pswitch_event(smpsw, PSWITCH_EVENT_PRESSED);
   1339 }
   1340 
   1341 /*
   1342  * Sleep.
   1343  */
   1344 static void
   1345 acpi_sleep_init(struct acpi_softc *sc)
   1346 {
   1347 	uint8_t a, b, i;
   1348 	ACPI_STATUS rv;
   1349 
   1350 	CTASSERT(ACPI_STATE_S0 == 0 && ACPI_STATE_S1 == 1);
   1351 	CTASSERT(ACPI_STATE_S2 == 2 && ACPI_STATE_S3 == 3);
   1352 	CTASSERT(ACPI_STATE_S4 == 4 && ACPI_STATE_S5 == 5);
   1353 
   1354 	/*
   1355 	 * Evaluate supported sleep states.
   1356 	 */
   1357 	for (i = ACPI_STATE_S0; i <= ACPI_STATE_S5; i++) {
   1358 
   1359 		rv = AcpiGetSleepTypeData(i, &a, &b);
   1360 
   1361 		if (ACPI_SUCCESS(rv))
   1362 			sc->sc_sleepstates |= __BIT(i);
   1363 	}
   1364 }
   1365 
   1366 void
   1367 acpi_enter_sleep_state(struct acpi_softc *sc, int state)
   1368 {
   1369 	ACPI_STATUS rv;
   1370 	int err;
   1371 
   1372 	if (state == sc->sc_sleepstate)
   1373 		return;
   1374 
   1375 	aprint_normal_dev(sc->sc_dev, "entering state S%d\n", state);
   1376 
   1377 	switch (state) {
   1378 
   1379 	case ACPI_STATE_S0:
   1380 		sc->sc_sleepstate = ACPI_STATE_S0;
   1381 		return;
   1382 
   1383 	case ACPI_STATE_S1:
   1384 	case ACPI_STATE_S2:
   1385 	case ACPI_STATE_S3:
   1386 	case ACPI_STATE_S4:
   1387 
   1388 		if ((sc->sc_sleepstates & __BIT(state)) == 0) {
   1389 			aprint_error_dev(sc->sc_dev, "sleep state "
   1390 			    "S%d is not available\n", state);
   1391 			return;
   1392 		}
   1393 
   1394 		/*
   1395 		 * Evaluate the _TTS method. This should be done before
   1396 		 * pmf_system_suspend(9) and the evaluation of _PTS.
   1397 		 * We should also re-evaluate this once we return to
   1398 		 * S0 or if we abort the sleep state transition in the
   1399 		 * middle (see ACPI 3.0, section 7.3.6). In reality,
   1400 		 * however, the _TTS method is seldom seen in the field.
   1401 		 */
   1402 		rv = acpi_eval_set_integer(NULL, "\\_TTS", state);
   1403 
   1404 		if (ACPI_SUCCESS(rv))
   1405 			aprint_debug_dev(sc->sc_dev, "evaluated _TTS\n");
   1406 
   1407 		if (state != ACPI_STATE_S1 &&
   1408 		    pmf_system_suspend(PMF_Q_NONE) != true) {
   1409 			aprint_error_dev(sc->sc_dev, "aborting suspend\n");
   1410 			break;
   1411 		}
   1412 
   1413 		/*
   1414 		 * This will evaluate the  _PTS and _SST methods,
   1415 		 * but unlike the documentation claims, not _GTS,
   1416 		 * which is evaluated in AcpiEnterSleepState().
   1417 		 * This must be called with interrupts enabled.
   1418 		 */
   1419 		rv = AcpiEnterSleepStatePrep(state);
   1420 
   1421 		if (ACPI_FAILURE(rv)) {
   1422 			aprint_error_dev(sc->sc_dev, "failed to prepare "
   1423 			    "S%d: %s\n", state, AcpiFormatException(rv));
   1424 			break;
   1425 		}
   1426 
   1427 		/*
   1428 		 * After the _PTS method has been evaluated, we can
   1429 		 * enable wake and evaluate _PSW (ACPI 4.0, p. 284).
   1430 		 */
   1431 		acpi_wakedev_commit(sc, state);
   1432 
   1433 		sc->sc_sleepstate = state;
   1434 
   1435 		if (state == ACPI_STATE_S1) {
   1436 
   1437 			/* Just enter the state. */
   1438 			acpi_md_OsDisableInterrupt();
   1439 			rv = AcpiEnterSleepState(state);
   1440 
   1441 			if (ACPI_FAILURE(rv))
   1442 				aprint_error_dev(sc->sc_dev, "failed to "
   1443 				    "enter S1: %s\n", AcpiFormatException(rv));
   1444 
   1445 			(void)AcpiLeaveSleepState(state);
   1446 
   1447 		} else {
   1448 
   1449 			err = acpi_md_sleep(state);
   1450 
   1451 			if (state == ACPI_STATE_S4)
   1452 				AcpiEnable();
   1453 
   1454 			pmf_system_bus_resume(PMF_Q_NONE);
   1455 			(void)AcpiLeaveSleepState(state);
   1456 			pmf_system_resume(PMF_Q_NONE);
   1457 		}
   1458 
   1459 		break;
   1460 
   1461 	case ACPI_STATE_S5:
   1462 
   1463 		(void)acpi_eval_set_integer(NULL, "\\_TTS", ACPI_STATE_S5);
   1464 
   1465 		rv = AcpiEnterSleepStatePrep(ACPI_STATE_S5);
   1466 
   1467 		if (ACPI_FAILURE(rv)) {
   1468 			aprint_error_dev(sc->sc_dev, "failed to prepare "
   1469 			    "S%d: %s\n", state, AcpiFormatException(rv));
   1470 			break;
   1471 		}
   1472 
   1473 		DELAY(1000000);
   1474 
   1475 		sc->sc_sleepstate = state;
   1476 		acpi_md_OsDisableInterrupt();
   1477 
   1478 		(void)AcpiEnterSleepState(ACPI_STATE_S5);
   1479 
   1480 		aprint_error_dev(sc->sc_dev, "WARNING: powerdown failed!\n");
   1481 
   1482 		break;
   1483 	}
   1484 
   1485 	sc->sc_sleepstate = ACPI_STATE_S0;
   1486 
   1487 	(void)acpi_eval_set_integer(NULL, "\\_TTS", ACPI_STATE_S0);
   1488 }
   1489 
   1490 /*
   1491  * Sysctl.
   1492  */
   1493 SYSCTL_SETUP(sysctl_acpi_setup, "sysctl hw.acpi subtree setup")
   1494 {
   1495 	const struct sysctlnode *mnode, *rnode;
   1496 	int err;
   1497 
   1498 	err = sysctl_createv(clog, 0, NULL, &rnode,
   1499 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "hw",
   1500 	    NULL, NULL, 0, NULL, 0,
   1501 	    CTL_HW, CTL_EOL);
   1502 
   1503 	if (err != 0)
   1504 		return;
   1505 
   1506 	err = sysctl_createv(clog, 0, &rnode, &rnode,
   1507 	    CTLFLAG_PERMANENT, CTLTYPE_NODE,
   1508 	    "acpi", SYSCTL_DESCR("ACPI subsystem parameters"),
   1509 	    NULL, 0, NULL, 0,
   1510 	    CTL_CREATE, CTL_EOL);
   1511 
   1512 	if (err != 0)
   1513 		return;
   1514 
   1515 	(void)sysctl_createv(NULL, 0, &rnode, NULL,
   1516 	    CTLFLAG_PERMANENT | CTLFLAG_READONLY, CTLTYPE_QUAD,
   1517 	    "root", SYSCTL_DESCR("ACPI root pointer"),
   1518 	    NULL, 0, &acpi_root_pointer, sizeof(acpi_root_pointer),
   1519 	    CTL_CREATE, CTL_EOL);
   1520 
   1521 	(void)sysctl_createv(NULL, 0, &rnode, NULL,
   1522 	    CTLFLAG_PERMANENT | CTLFLAG_READONLY, CTLTYPE_STRING,
   1523 	    "supported_states", SYSCTL_DESCR("Supported system states"),
   1524 	    sysctl_hw_acpi_sleepstates, 0, NULL, 0,
   1525 	    CTL_CREATE, CTL_EOL);
   1526 
   1527 	err = sysctl_createv(NULL, 0, NULL, &mnode,
   1528 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "machdep",
   1529 	    NULL, NULL, 0, NULL, 0,
   1530 	    CTL_MACHDEP, CTL_EOL);
   1531 
   1532 	if (err == 0) {
   1533 
   1534 		(void)sysctl_createv(NULL, 0, &mnode, NULL,
   1535 		    CTLFLAG_PERMANENT | CTLFLAG_READWRITE, CTLTYPE_INT,
   1536 		    "sleep_state", SYSCTL_DESCR("System sleep state"),
   1537 		    sysctl_hw_acpi_sleepstate, 0, NULL, 0,
   1538 		    CTL_CREATE, CTL_EOL);
   1539 	}
   1540 
   1541 	err = sysctl_createv(clog, 0, &rnode, &rnode,
   1542 	    CTLFLAG_PERMANENT, CTLTYPE_NODE,
   1543 	    "stat", SYSCTL_DESCR("ACPI statistics"),
   1544 	    NULL, 0, NULL, 0,
   1545 	    CTL_CREATE, CTL_EOL);
   1546 
   1547 	if (err != 0)
   1548 		return;
   1549 
   1550 	(void)sysctl_createv(clog, 0, &rnode, NULL,
   1551 	    CTLFLAG_PERMANENT | CTLFLAG_READONLY, CTLTYPE_QUAD,
   1552 	    "gpe", SYSCTL_DESCR("Number of dispatched GPEs"),
   1553 	    NULL, 0, &AcpiGpeCount, sizeof(AcpiGpeCount),
   1554 	    CTL_CREATE, CTL_EOL);
   1555 
   1556 	(void)sysctl_createv(clog, 0, &rnode, NULL,
   1557 	    CTLFLAG_PERMANENT | CTLFLAG_READONLY, CTLTYPE_QUAD,
   1558 	    "sci", SYSCTL_DESCR("Number of SCI interrupts"),
   1559 	    NULL, 0, &AcpiSciCount, sizeof(AcpiSciCount),
   1560 	    CTL_CREATE, CTL_EOL);
   1561 
   1562 	(void)sysctl_createv(clog, 0, &rnode, NULL,
   1563 	    CTLFLAG_PERMANENT | CTLFLAG_READONLY, CTLTYPE_QUAD,
   1564 	    "fixed", SYSCTL_DESCR("Number of fixed events"),
   1565 	    sysctl_hw_acpi_fixedstats, 0, NULL, 0,
   1566 	    CTL_CREATE, CTL_EOL);
   1567 
   1568 	(void)sysctl_createv(clog, 0, &rnode, NULL,
   1569 	    CTLFLAG_PERMANENT | CTLFLAG_READONLY, CTLTYPE_QUAD,
   1570 	    "method", SYSCTL_DESCR("Number of methods executed"),
   1571 	    NULL, 0, &AcpiMethodCount, sizeof(AcpiMethodCount),
   1572 	    CTL_CREATE, CTL_EOL);
   1573 
   1574 	CTASSERT(sizeof(AcpiGpeCount) == sizeof(uint64_t));
   1575 	CTASSERT(sizeof(AcpiSciCount) == sizeof(uint64_t));
   1576 }
   1577 
   1578 static int
   1579 sysctl_hw_acpi_fixedstats(SYSCTLFN_ARGS)
   1580 {
   1581 	struct sysctlnode node;
   1582 	uint64_t t;
   1583 	int err, i;
   1584 
   1585 	for (i = t = 0; i < __arraycount(AcpiFixedEventCount); i++)
   1586 		t += AcpiFixedEventCount[i];
   1587 
   1588 	node = *rnode;
   1589 	node.sysctl_data = &t;
   1590 
   1591 	err = sysctl_lookup(SYSCTLFN_CALL(&node));
   1592 
   1593 	if (err || newp == NULL)
   1594 		return err;
   1595 
   1596 	return 0;
   1597 }
   1598 
   1599 static int
   1600 sysctl_hw_acpi_sleepstate(SYSCTLFN_ARGS)
   1601 {
   1602 	struct acpi_softc *sc = acpi_softc;
   1603 	struct sysctlnode node;
   1604 	int err, t;
   1605 
   1606 	if (acpi_softc == NULL)
   1607 		return ENOSYS;
   1608 
   1609 	node = *rnode;
   1610 	t = sc->sc_sleepstate;
   1611 	node.sysctl_data = &t;
   1612 
   1613 	err = sysctl_lookup(SYSCTLFN_CALL(&node));
   1614 
   1615 	if (err || newp == NULL)
   1616 		return err;
   1617 
   1618 	if (t < ACPI_STATE_S0 || t > ACPI_STATE_S5)
   1619 		return EINVAL;
   1620 
   1621 	acpi_enter_sleep_state(sc, t);
   1622 
   1623 	return 0;
   1624 }
   1625 
   1626 static int
   1627 sysctl_hw_acpi_sleepstates(SYSCTLFN_ARGS)
   1628 {
   1629 	struct acpi_softc *sc = acpi_softc;
   1630 	struct sysctlnode node;
   1631 	char t[3 * 6 + 1];
   1632 	int err;
   1633 
   1634 	if (acpi_softc == NULL)
   1635 		return ENOSYS;
   1636 
   1637 	(void)memset(t, '\0', sizeof(t));
   1638 
   1639 	(void)snprintf(t, sizeof(t), "%s%s%s%s%s%s",
   1640 	    ((sc->sc_sleepstates & __BIT(0)) != 0) ? "S0 " : "",
   1641 	    ((sc->sc_sleepstates & __BIT(1)) != 0) ? "S1 " : "",
   1642 	    ((sc->sc_sleepstates & __BIT(2)) != 0) ? "S2 " : "",
   1643 	    ((sc->sc_sleepstates & __BIT(3)) != 0) ? "S3 " : "",
   1644 	    ((sc->sc_sleepstates & __BIT(4)) != 0) ? "S4 " : "",
   1645 	    ((sc->sc_sleepstates & __BIT(5)) != 0) ? "S5 " : "");
   1646 
   1647 	node = *rnode;
   1648 	node.sysctl_data = &t;
   1649 
   1650 	err = sysctl_lookup(SYSCTLFN_CALL(&node));
   1651 
   1652 	if (err || newp == NULL)
   1653 		return err;
   1654 
   1655 	return 0;
   1656 }
   1657 
   1658 /*
   1659  * Tables.
   1660  */
   1661 ACPI_PHYSICAL_ADDRESS
   1662 acpi_OsGetRootPointer(void)
   1663 {
   1664 	ACPI_PHYSICAL_ADDRESS PhysicalAddress;
   1665 
   1666 	/*
   1667 	 * We let MD code handle this since there are multiple ways to do it:
   1668 	 *
   1669 	 *	IA-32: Use AcpiFindRootPointer() to locate the RSDP.
   1670 	 *
   1671 	 *	IA-64: Use the EFI.
   1672 	 */
   1673 	PhysicalAddress = acpi_md_OsGetRootPointer();
   1674 
   1675 	if (acpi_root_pointer == 0)
   1676 		acpi_root_pointer = PhysicalAddress;
   1677 
   1678 	return PhysicalAddress;
   1679 }
   1680 
   1681 static ACPI_TABLE_HEADER *
   1682 acpi_map_rsdt(void)
   1683 {
   1684 	ACPI_PHYSICAL_ADDRESS paddr;
   1685 	ACPI_TABLE_RSDP *rsdp;
   1686 
   1687 	paddr = AcpiOsGetRootPointer();
   1688 
   1689 	if (paddr == 0)
   1690 		return NULL;
   1691 
   1692 	rsdp = AcpiOsMapMemory(paddr, sizeof(ACPI_TABLE_RSDP));
   1693 
   1694 	if (rsdp == NULL)
   1695 		return NULL;
   1696 
   1697 	if (rsdp->Revision > 1 && rsdp->XsdtPhysicalAddress)
   1698 		paddr = rsdp->XsdtPhysicalAddress;
   1699 	else
   1700 		paddr = rsdp->RsdtPhysicalAddress;
   1701 
   1702 	AcpiOsUnmapMemory(rsdp, sizeof(ACPI_TABLE_RSDP));
   1703 
   1704 	return AcpiOsMapMemory(paddr, sizeof(ACPI_TABLE_HEADER));
   1705 }
   1706 
   1707 static void
   1708 acpi_unmap_rsdt(ACPI_TABLE_HEADER *rsdt)
   1709 {
   1710 
   1711 	if (rsdt == NULL)
   1712 		return;
   1713 
   1714 	AcpiOsUnmapMemory(rsdt, sizeof(ACPI_TABLE_HEADER));
   1715 }
   1716 
   1717 /*
   1718  * XXX: Refactor to be a generic function that maps tables.
   1719  */
   1720 ACPI_STATUS
   1721 acpi_madt_map(void)
   1722 {
   1723 	ACPI_STATUS  rv;
   1724 
   1725 	if (madt_header != NULL)
   1726 		return AE_ALREADY_EXISTS;
   1727 
   1728 	rv = AcpiGetTable(ACPI_SIG_MADT, 1, &madt_header);
   1729 
   1730 	if (ACPI_FAILURE(rv))
   1731 		return rv;
   1732 
   1733 	return AE_OK;
   1734 }
   1735 
   1736 void
   1737 acpi_madt_unmap(void)
   1738 {
   1739 	madt_header = NULL;
   1740 }
   1741 
   1742 /*
   1743  * XXX: Refactor to be a generic function that walks tables.
   1744  */
   1745 void
   1746 acpi_madt_walk(ACPI_STATUS (*func)(ACPI_SUBTABLE_HEADER *, void *), void *aux)
   1747 {
   1748 	ACPI_SUBTABLE_HEADER *hdrp;
   1749 	char *madtend, *where;
   1750 
   1751 	madtend = (char *)madt_header + madt_header->Length;
   1752 	where = (char *)madt_header + sizeof (ACPI_TABLE_MADT);
   1753 
   1754 	while (where < madtend) {
   1755 
   1756 		hdrp = (ACPI_SUBTABLE_HEADER *)where;
   1757 
   1758 		if (ACPI_FAILURE(func(hdrp, aux)))
   1759 			break;
   1760 
   1761 		where += hdrp->Length;
   1762 	}
   1763 }
   1764 
   1765 /*
   1766  * Miscellaneous.
   1767  */
   1768 static bool
   1769 acpi_is_scope(struct acpi_devnode *ad)
   1770 {
   1771 	int i;
   1772 
   1773 	/*
   1774 	 * Return true if the node is a root scope.
   1775 	 */
   1776 	if (ad->ad_parent == NULL)
   1777 		return false;
   1778 
   1779 	if (ad->ad_parent->ad_handle != ACPI_ROOT_OBJECT)
   1780 		return false;
   1781 
   1782 	for (i = 0; i < __arraycount(acpi_scopes); i++) {
   1783 
   1784 		if (acpi_scopes[i] == NULL)
   1785 			continue;
   1786 
   1787 		if (ad->ad_handle == acpi_scopes[i])
   1788 			return true;
   1789 	}
   1790 
   1791 	return false;
   1792 }
   1793 
   1794 /*
   1795  * ACPIVERBOSE.
   1796  */
   1797 void
   1798 acpi_load_verbose(void)
   1799 {
   1800 
   1801 	if (acpi_verbose_loaded == 0)
   1802 		module_autoload("acpiverbose", MODULE_CLASS_MISC);
   1803 }
   1804 
   1805 void
   1806 acpi_print_verbose_stub(struct acpi_softc *sc)
   1807 {
   1808 
   1809 	acpi_load_verbose();
   1810 
   1811 	if (acpi_verbose_loaded != 0)
   1812 		acpi_print_verbose(sc);
   1813 }
   1814 
   1815 void
   1816 acpi_print_dev_stub(const char *pnpstr)
   1817 {
   1818 
   1819 	acpi_load_verbose();
   1820 
   1821 	if (acpi_verbose_loaded != 0)
   1822 		acpi_print_dev(pnpstr);
   1823 }
   1824