Home | History | Annotate | Line # | Download | only in acpi
acpi.c revision 1.114.2.8
      1 /*	$NetBSD: acpi.c,v 1.114.2.8 2010/10/09 03:32:03 yamt 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.114.2.8 2010/10/09 03:32:03 yamt 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 		 * Handled internally.
    983 		 */
    984 		if (di->Type == ACPI_TYPE_POWER)
    985 			continue;
    986 
    987 		/*
    988 		 * Skip ignored HIDs.
    989 		 */
    990 		if (acpi_match_hid(di, acpi_ignored_ids))
    991 			continue;
    992 
    993 		aa.aa_node = ad;
    994 		aa.aa_iot = sc->sc_iot;
    995 		aa.aa_memt = sc->sc_memt;
    996 		aa.aa_pc = sc->sc_pc;
    997 		aa.aa_pciflags = sc->sc_pciflags;
    998 		aa.aa_ic = sc->sc_ic;
    999 
   1000 		ad->ad_device = config_found_ia(sc->sc_dev,
   1001 		    "acpinodebus", &aa, acpi_print);
   1002 	}
   1003 }
   1004 
   1005 static void
   1006 acpi_rescan_capabilities(struct acpi_softc *sc)
   1007 {
   1008 	struct acpi_devnode *ad;
   1009 	ACPI_HANDLE tmp;
   1010 	ACPI_STATUS rv;
   1011 
   1012 	SIMPLEQ_FOREACH(ad, &sc->ad_head, ad_list) {
   1013 
   1014 		if (ad->ad_devinfo->Type != ACPI_TYPE_DEVICE)
   1015 			continue;
   1016 
   1017 		/*
   1018 		 * Scan power resource capabilities.
   1019 		 *
   1020 		 * If any power states are supported,
   1021 		 * at least _PR0 and _PR3 must be present.
   1022 		 */
   1023 		rv = AcpiGetHandle(ad->ad_handle, "_PR0", &tmp);
   1024 
   1025 		if (ACPI_SUCCESS(rv)) {
   1026 			ad->ad_flags |= ACPI_DEVICE_POWER;
   1027 			acpi_power_add(ad);
   1028 		}
   1029 
   1030 		/*
   1031 		 * Scan wake-up capabilities.
   1032 		 */
   1033 		rv = AcpiGetHandle(ad->ad_handle, "_PRW", &tmp);
   1034 
   1035 		if (ACPI_SUCCESS(rv)) {
   1036 			ad->ad_flags |= ACPI_DEVICE_WAKEUP;
   1037 			acpi_wakedev_add(ad);
   1038 		}
   1039 	}
   1040 }
   1041 
   1042 static int
   1043 acpi_print(void *aux, const char *pnp)
   1044 {
   1045 	struct acpi_attach_args *aa = aux;
   1046 	ACPI_STATUS rv;
   1047 
   1048 	if (pnp) {
   1049 		if (aa->aa_node->ad_devinfo->Valid & ACPI_VALID_HID) {
   1050 			char *pnpstr =
   1051 			    aa->aa_node->ad_devinfo->HardwareId.String;
   1052 			ACPI_BUFFER buf;
   1053 
   1054 			aprint_normal("%s (%s) ", aa->aa_node->ad_name,
   1055 			    pnpstr);
   1056 
   1057 			rv = acpi_eval_struct(aa->aa_node->ad_handle,
   1058 			    "_STR", &buf);
   1059 			if (ACPI_SUCCESS(rv)) {
   1060 				ACPI_OBJECT *obj = buf.Pointer;
   1061 				switch (obj->Type) {
   1062 				case ACPI_TYPE_STRING:
   1063 					aprint_normal("[%s] ", obj->String.Pointer);
   1064 					break;
   1065 				case ACPI_TYPE_BUFFER:
   1066 					aprint_normal("buffer %p ", obj->Buffer.Pointer);
   1067 					break;
   1068 				default:
   1069 					aprint_normal("type %u ",obj->Type);
   1070 					break;
   1071 				}
   1072 				ACPI_FREE(buf.Pointer);
   1073 			}
   1074 			else
   1075 				acpi_print_dev(pnpstr);
   1076 
   1077 			aprint_normal("at %s", pnp);
   1078 		} else if (aa->aa_node->ad_devinfo->Type != ACPI_TYPE_DEVICE) {
   1079 			aprint_normal("%s (ACPI Object Type '%s' "
   1080 			    "[0x%02x]) ", aa->aa_node->ad_name,
   1081 			     AcpiUtGetTypeName(aa->aa_node->ad_devinfo->Type),
   1082 			     aa->aa_node->ad_devinfo->Type);
   1083 			aprint_normal("at %s", pnp);
   1084 		} else
   1085 			return 0;
   1086 	} else {
   1087 		aprint_normal(" (%s", aa->aa_node->ad_name);
   1088 		if (aa->aa_node->ad_devinfo->Valid & ACPI_VALID_HID) {
   1089 			aprint_normal(", %s", aa->aa_node->ad_devinfo->HardwareId.String);
   1090 			if (aa->aa_node->ad_devinfo->Valid & ACPI_VALID_UID) {
   1091 				const char *uid;
   1092 
   1093 				uid = aa->aa_node->ad_devinfo->UniqueId.String;
   1094 				if (uid[0] == '\0')
   1095 					uid = "<null>";
   1096 				aprint_normal("-%s", uid);
   1097 			}
   1098 		}
   1099 		aprint_normal(")");
   1100 	}
   1101 
   1102 	return UNCONF;
   1103 }
   1104 
   1105 /*
   1106  * Notify.
   1107  */
   1108 static void
   1109 acpi_notify_handler(ACPI_HANDLE handle, uint32_t event, void *aux)
   1110 {
   1111 	struct acpi_softc *sc = acpi_softc;
   1112 	struct acpi_devnode *ad;
   1113 
   1114 	KASSERT(sc != NULL);
   1115 	KASSERT(aux == NULL);
   1116 	KASSERT(acpi_active != 0);
   1117 
   1118 	if (acpi_suspended != 0)
   1119 		return;
   1120 
   1121 	/*
   1122 	 *  System: 0x00 - 0x7F.
   1123 	 *  Device: 0x80 - 0xFF.
   1124 	 */
   1125 	switch (event) {
   1126 
   1127 	case ACPI_NOTIFY_BUS_CHECK:
   1128 	case ACPI_NOTIFY_DEVICE_CHECK:
   1129 	case ACPI_NOTIFY_DEVICE_WAKE:
   1130 	case ACPI_NOTIFY_EJECT_REQUEST:
   1131 	case ACPI_NOTIFY_DEVICE_CHECK_LIGHT:
   1132 	case ACPI_NOTIFY_FREQUENCY_MISMATCH:
   1133 	case ACPI_NOTIFY_BUS_MODE_MISMATCH:
   1134 	case ACPI_NOTIFY_POWER_FAULT:
   1135 	case ACPI_NOTIFY_CAPABILITIES_CHECK:
   1136 	case ACPI_NOTIFY_DEVICE_PLD_CHECK:
   1137 	case ACPI_NOTIFY_RESERVED:
   1138 	case ACPI_NOTIFY_LOCALITY_UPDATE:
   1139 		break;
   1140 	}
   1141 
   1142 	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "notification 0x%02X for "
   1143 		"%s (%p)\n", event, acpi_name(handle), handle));
   1144 
   1145 	/*
   1146 	 * We deliver notifications only to drivers
   1147 	 * that have been succesfully attached and
   1148 	 * that have registered a handler with us.
   1149 	 * The opaque pointer is always the device_t.
   1150 	 */
   1151 	SIMPLEQ_FOREACH(ad, &sc->ad_head, ad_list) {
   1152 
   1153 		if (ad->ad_device == NULL)
   1154 			continue;
   1155 
   1156 		if (ad->ad_notify == NULL)
   1157 			continue;
   1158 
   1159 		if (ad->ad_handle != handle)
   1160 			continue;
   1161 
   1162 		(*ad->ad_notify)(ad->ad_handle, event, ad->ad_device);
   1163 
   1164 		return;
   1165 	}
   1166 
   1167 	aprint_debug_dev(sc->sc_dev, "unhandled notify 0x%02X "
   1168 	    "for %s (%p)\n", event, acpi_name(handle), handle);
   1169 }
   1170 
   1171 bool
   1172 acpi_register_notify(struct acpi_devnode *ad, ACPI_NOTIFY_HANDLER notify)
   1173 {
   1174 	struct acpi_softc *sc = acpi_softc;
   1175 
   1176 	KASSERT(sc != NULL);
   1177 	KASSERT(acpi_active != 0);
   1178 
   1179 	if (acpi_suspended != 0)
   1180 		goto fail;
   1181 
   1182 	if (ad == NULL || notify == NULL)
   1183 		goto fail;
   1184 
   1185 	ad->ad_notify = notify;
   1186 
   1187 	return true;
   1188 
   1189 fail:
   1190 	aprint_error_dev(sc->sc_dev, "failed to register notify "
   1191 	    "handler for %s (%p)\n", ad->ad_name, ad->ad_handle);
   1192 
   1193 	return false;
   1194 }
   1195 
   1196 void
   1197 acpi_deregister_notify(struct acpi_devnode *ad)
   1198 {
   1199 
   1200 	ad->ad_notify = NULL;
   1201 }
   1202 
   1203 /*
   1204  * Fixed buttons.
   1205  */
   1206 static void
   1207 acpi_register_fixed_button(struct acpi_softc *sc, int event)
   1208 {
   1209 	struct sysmon_pswitch *smpsw;
   1210 	ACPI_STATUS rv;
   1211 	int type;
   1212 
   1213 	switch (event) {
   1214 
   1215 	case ACPI_EVENT_POWER_BUTTON:
   1216 
   1217 		if ((AcpiGbl_FADT.Flags & ACPI_FADT_POWER_BUTTON) != 0)
   1218 			return;
   1219 
   1220 		type = PSWITCH_TYPE_POWER;
   1221 		smpsw = &sc->sc_smpsw_power;
   1222 		break;
   1223 
   1224 	case ACPI_EVENT_SLEEP_BUTTON:
   1225 
   1226 		if ((AcpiGbl_FADT.Flags & ACPI_FADT_SLEEP_BUTTON) != 0)
   1227 			return;
   1228 
   1229 		type = PSWITCH_TYPE_SLEEP;
   1230 		smpsw = &sc->sc_smpsw_sleep;
   1231 		break;
   1232 
   1233 	default:
   1234 		rv = AE_TYPE;
   1235 		goto fail;
   1236 	}
   1237 
   1238 	smpsw->smpsw_type = type;
   1239 	smpsw->smpsw_name = device_xname(sc->sc_dev);
   1240 
   1241 	if (sysmon_pswitch_register(smpsw) != 0) {
   1242 		rv = AE_ERROR;
   1243 		goto fail;
   1244 	}
   1245 
   1246 	rv = AcpiInstallFixedEventHandler(event,
   1247 	    acpi_fixed_button_handler, smpsw);
   1248 
   1249 	if (ACPI_FAILURE(rv)) {
   1250 		sysmon_pswitch_unregister(smpsw);
   1251 		goto fail;
   1252 	}
   1253 
   1254 	aprint_debug_dev(sc->sc_dev, "fixed %s button present\n",
   1255 	    (type != ACPI_EVENT_SLEEP_BUTTON) ? "power" : "sleep");
   1256 
   1257 	return;
   1258 
   1259 fail:
   1260 	aprint_error_dev(sc->sc_dev, "failed to register "
   1261 	    "fixed event: %s\n", AcpiFormatException(rv));
   1262 }
   1263 
   1264 static void
   1265 acpi_deregister_fixed_button(struct acpi_softc *sc, int event)
   1266 {
   1267 	struct sysmon_pswitch *smpsw;
   1268 	ACPI_STATUS rv;
   1269 
   1270 	switch (event) {
   1271 
   1272 	case ACPI_EVENT_POWER_BUTTON:
   1273 		smpsw = &sc->sc_smpsw_power;
   1274 
   1275 		if ((AcpiGbl_FADT.Flags & ACPI_FADT_POWER_BUTTON) != 0) {
   1276 			KASSERT(smpsw->smpsw_type != PSWITCH_TYPE_POWER);
   1277 			return;
   1278 		}
   1279 
   1280 		break;
   1281 
   1282 	case ACPI_EVENT_SLEEP_BUTTON:
   1283 		smpsw = &sc->sc_smpsw_sleep;
   1284 
   1285 		if ((AcpiGbl_FADT.Flags & ACPI_FADT_SLEEP_BUTTON) != 0) {
   1286 			KASSERT(smpsw->smpsw_type != PSWITCH_TYPE_SLEEP);
   1287 			return;
   1288 		}
   1289 
   1290 		break;
   1291 
   1292 	default:
   1293 		rv = AE_TYPE;
   1294 		goto fail;
   1295 	}
   1296 
   1297 	rv = AcpiRemoveFixedEventHandler(event, acpi_fixed_button_handler);
   1298 
   1299 	if (ACPI_SUCCESS(rv)) {
   1300 		sysmon_pswitch_unregister(smpsw);
   1301 		return;
   1302 	}
   1303 
   1304 fail:
   1305 	aprint_error_dev(sc->sc_dev, "failed to deregister "
   1306 	    "fixed event: %s\n", AcpiFormatException(rv));
   1307 }
   1308 
   1309 static uint32_t
   1310 acpi_fixed_button_handler(void *context)
   1311 {
   1312 	static const int handler = OSL_NOTIFY_HANDLER;
   1313 	struct sysmon_pswitch *smpsw = context;
   1314 
   1315 	(void)AcpiOsExecute(handler, acpi_fixed_button_pressed, smpsw);
   1316 
   1317 	return ACPI_INTERRUPT_HANDLED;
   1318 }
   1319 
   1320 static void
   1321 acpi_fixed_button_pressed(void *context)
   1322 {
   1323 	struct sysmon_pswitch *smpsw = context;
   1324 
   1325 	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "%s fixed button pressed\n",
   1326 		(smpsw->smpsw_type != ACPI_EVENT_SLEEP_BUTTON) ?
   1327 		"power" : "sleep"));
   1328 
   1329 	sysmon_pswitch_event(smpsw, PSWITCH_EVENT_PRESSED);
   1330 }
   1331 
   1332 /*
   1333  * Sleep.
   1334  */
   1335 static void
   1336 acpi_sleep_init(struct acpi_softc *sc)
   1337 {
   1338 	uint8_t a, b, i;
   1339 	ACPI_STATUS rv;
   1340 
   1341 	CTASSERT(ACPI_STATE_S0 == 0 && ACPI_STATE_S1 == 1);
   1342 	CTASSERT(ACPI_STATE_S2 == 2 && ACPI_STATE_S3 == 3);
   1343 	CTASSERT(ACPI_STATE_S4 == 4 && ACPI_STATE_S5 == 5);
   1344 
   1345 	/*
   1346 	 * Evaluate supported sleep states.
   1347 	 */
   1348 	for (i = ACPI_STATE_S0; i <= ACPI_STATE_S5; i++) {
   1349 
   1350 		rv = AcpiGetSleepTypeData(i, &a, &b);
   1351 
   1352 		if (ACPI_SUCCESS(rv))
   1353 			sc->sc_sleepstates |= __BIT(i);
   1354 	}
   1355 }
   1356 
   1357 void
   1358 acpi_enter_sleep_state(struct acpi_softc *sc, int state)
   1359 {
   1360 	ACPI_STATUS rv;
   1361 	int err;
   1362 
   1363 	if (state == sc->sc_sleepstate)
   1364 		return;
   1365 
   1366 	aprint_normal_dev(sc->sc_dev, "entering state S%d\n", state);
   1367 
   1368 	switch (state) {
   1369 
   1370 	case ACPI_STATE_S0:
   1371 		sc->sc_sleepstate = ACPI_STATE_S0;
   1372 		return;
   1373 
   1374 	case ACPI_STATE_S1:
   1375 	case ACPI_STATE_S2:
   1376 	case ACPI_STATE_S3:
   1377 	case ACPI_STATE_S4:
   1378 
   1379 		if ((sc->sc_sleepstates & __BIT(state)) == 0) {
   1380 			aprint_error_dev(sc->sc_dev, "sleep state "
   1381 			    "S%d is not available\n", state);
   1382 			return;
   1383 		}
   1384 
   1385 		/*
   1386 		 * Evaluate the _TTS method. This should be done before
   1387 		 * pmf_system_suspend(9) and the evaluation of _PTS.
   1388 		 * We should also re-evaluate this once we return to
   1389 		 * S0 or if we abort the sleep state transition in the
   1390 		 * middle (see ACPI 3.0, section 7.3.6). In reality,
   1391 		 * however, the _TTS method is seldom seen in the field.
   1392 		 */
   1393 		rv = acpi_eval_set_integer(NULL, "\\_TTS", state);
   1394 
   1395 		if (ACPI_SUCCESS(rv))
   1396 			aprint_debug_dev(sc->sc_dev, "evaluated _TTS\n");
   1397 
   1398 		if (state != ACPI_STATE_S1 &&
   1399 		    pmf_system_suspend(PMF_Q_NONE) != true) {
   1400 			aprint_error_dev(sc->sc_dev, "aborting suspend\n");
   1401 			break;
   1402 		}
   1403 
   1404 		/*
   1405 		 * This will evaluate the  _PTS and _SST methods,
   1406 		 * but unlike the documentation claims, not _GTS,
   1407 		 * which is evaluated in AcpiEnterSleepState().
   1408 		 * This must be called with interrupts enabled.
   1409 		 */
   1410 		rv = AcpiEnterSleepStatePrep(state);
   1411 
   1412 		if (ACPI_FAILURE(rv)) {
   1413 			aprint_error_dev(sc->sc_dev, "failed to prepare "
   1414 			    "S%d: %s\n", state, AcpiFormatException(rv));
   1415 			break;
   1416 		}
   1417 
   1418 		/*
   1419 		 * After the _PTS method has been evaluated, we can
   1420 		 * enable wake and evaluate _PSW (ACPI 4.0, p. 284).
   1421 		 */
   1422 		acpi_wakedev_commit(sc, state);
   1423 
   1424 		sc->sc_sleepstate = state;
   1425 
   1426 		if (state == ACPI_STATE_S1) {
   1427 
   1428 			/* Just enter the state. */
   1429 			acpi_md_OsDisableInterrupt();
   1430 			rv = AcpiEnterSleepState(state);
   1431 
   1432 			if (ACPI_FAILURE(rv))
   1433 				aprint_error_dev(sc->sc_dev, "failed to "
   1434 				    "enter S1: %s\n", AcpiFormatException(rv));
   1435 
   1436 			(void)AcpiLeaveSleepState(state);
   1437 
   1438 		} else {
   1439 
   1440 			err = acpi_md_sleep(state);
   1441 
   1442 			if (state == ACPI_STATE_S4)
   1443 				AcpiEnable();
   1444 
   1445 			pmf_system_bus_resume(PMF_Q_NONE);
   1446 			(void)AcpiLeaveSleepState(state);
   1447 			pmf_system_resume(PMF_Q_NONE);
   1448 		}
   1449 
   1450 		break;
   1451 
   1452 	case ACPI_STATE_S5:
   1453 
   1454 		(void)acpi_eval_set_integer(NULL, "\\_TTS", ACPI_STATE_S5);
   1455 
   1456 		rv = AcpiEnterSleepStatePrep(ACPI_STATE_S5);
   1457 
   1458 		if (ACPI_FAILURE(rv)) {
   1459 			aprint_error_dev(sc->sc_dev, "failed to prepare "
   1460 			    "S%d: %s\n", state, AcpiFormatException(rv));
   1461 			break;
   1462 		}
   1463 
   1464 		DELAY(1000000);
   1465 
   1466 		sc->sc_sleepstate = state;
   1467 		acpi_md_OsDisableInterrupt();
   1468 
   1469 		(void)AcpiEnterSleepState(ACPI_STATE_S5);
   1470 
   1471 		aprint_error_dev(sc->sc_dev, "WARNING: powerdown failed!\n");
   1472 
   1473 		break;
   1474 	}
   1475 
   1476 	sc->sc_sleepstate = ACPI_STATE_S0;
   1477 
   1478 	(void)acpi_eval_set_integer(NULL, "\\_TTS", ACPI_STATE_S0);
   1479 }
   1480 
   1481 /*
   1482  * Sysctl.
   1483  */
   1484 SYSCTL_SETUP(sysctl_acpi_setup, "sysctl hw.acpi subtree setup")
   1485 {
   1486 	const struct sysctlnode *mnode, *rnode;
   1487 	int err;
   1488 
   1489 	err = sysctl_createv(clog, 0, NULL, &rnode,
   1490 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "hw",
   1491 	    NULL, NULL, 0, NULL, 0,
   1492 	    CTL_HW, CTL_EOL);
   1493 
   1494 	if (err != 0)
   1495 		return;
   1496 
   1497 	err = sysctl_createv(clog, 0, &rnode, &rnode,
   1498 	    CTLFLAG_PERMANENT, CTLTYPE_NODE,
   1499 	    "acpi", SYSCTL_DESCR("ACPI subsystem parameters"),
   1500 	    NULL, 0, NULL, 0,
   1501 	    CTL_CREATE, CTL_EOL);
   1502 
   1503 	if (err != 0)
   1504 		return;
   1505 
   1506 	(void)sysctl_createv(NULL, 0, &rnode, NULL,
   1507 	    CTLFLAG_PERMANENT | CTLFLAG_READONLY, CTLTYPE_QUAD,
   1508 	    "root", SYSCTL_DESCR("ACPI root pointer"),
   1509 	    NULL, 0, &acpi_root_pointer, sizeof(acpi_root_pointer),
   1510 	    CTL_CREATE, CTL_EOL);
   1511 
   1512 	(void)sysctl_createv(NULL, 0, &rnode, NULL,
   1513 	    CTLFLAG_PERMANENT | CTLFLAG_READONLY, CTLTYPE_STRING,
   1514 	    "supported_states", SYSCTL_DESCR("Supported system states"),
   1515 	    sysctl_hw_acpi_sleepstates, 0, NULL, 0,
   1516 	    CTL_CREATE, CTL_EOL);
   1517 
   1518 	err = sysctl_createv(NULL, 0, NULL, &mnode,
   1519 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "machdep",
   1520 	    NULL, NULL, 0, NULL, 0,
   1521 	    CTL_MACHDEP, CTL_EOL);
   1522 
   1523 	if (err == 0) {
   1524 
   1525 		(void)sysctl_createv(NULL, 0, &mnode, NULL,
   1526 		    CTLFLAG_PERMANENT | CTLFLAG_READWRITE, CTLTYPE_INT,
   1527 		    "sleep_state", SYSCTL_DESCR("System sleep state"),
   1528 		    sysctl_hw_acpi_sleepstate, 0, NULL, 0,
   1529 		    CTL_CREATE, CTL_EOL);
   1530 	}
   1531 
   1532 	err = sysctl_createv(clog, 0, &rnode, &rnode,
   1533 	    CTLFLAG_PERMANENT, CTLTYPE_NODE,
   1534 	    "stat", SYSCTL_DESCR("ACPI statistics"),
   1535 	    NULL, 0, NULL, 0,
   1536 	    CTL_CREATE, CTL_EOL);
   1537 
   1538 	if (err != 0)
   1539 		return;
   1540 
   1541 	(void)sysctl_createv(clog, 0, &rnode, NULL,
   1542 	    CTLFLAG_PERMANENT | CTLFLAG_READONLY, CTLTYPE_QUAD,
   1543 	    "gpe", SYSCTL_DESCR("Number of dispatched GPEs"),
   1544 	    NULL, 0, &AcpiGpeCount, sizeof(AcpiGpeCount),
   1545 	    CTL_CREATE, CTL_EOL);
   1546 
   1547 	(void)sysctl_createv(clog, 0, &rnode, NULL,
   1548 	    CTLFLAG_PERMANENT | CTLFLAG_READONLY, CTLTYPE_QUAD,
   1549 	    "sci", SYSCTL_DESCR("Number of SCI interrupts"),
   1550 	    NULL, 0, &AcpiSciCount, sizeof(AcpiSciCount),
   1551 	    CTL_CREATE, CTL_EOL);
   1552 
   1553 	(void)sysctl_createv(clog, 0, &rnode, NULL,
   1554 	    CTLFLAG_PERMANENT | CTLFLAG_READONLY, CTLTYPE_QUAD,
   1555 	    "fixed", SYSCTL_DESCR("Number of fixed events"),
   1556 	    sysctl_hw_acpi_fixedstats, 0, NULL, 0,
   1557 	    CTL_CREATE, CTL_EOL);
   1558 
   1559 	(void)sysctl_createv(clog, 0, &rnode, NULL,
   1560 	    CTLFLAG_PERMANENT | CTLFLAG_READONLY, CTLTYPE_QUAD,
   1561 	    "method", SYSCTL_DESCR("Number of methods executed"),
   1562 	    NULL, 0, &AcpiMethodCount, sizeof(AcpiMethodCount),
   1563 	    CTL_CREATE, CTL_EOL);
   1564 
   1565 	CTASSERT(sizeof(AcpiGpeCount) == sizeof(uint64_t));
   1566 	CTASSERT(sizeof(AcpiSciCount) == sizeof(uint64_t));
   1567 }
   1568 
   1569 static int
   1570 sysctl_hw_acpi_fixedstats(SYSCTLFN_ARGS)
   1571 {
   1572 	struct sysctlnode node;
   1573 	uint64_t t;
   1574 	int err, i;
   1575 
   1576 	for (i = t = 0; i < __arraycount(AcpiFixedEventCount); i++)
   1577 		t += AcpiFixedEventCount[i];
   1578 
   1579 	node = *rnode;
   1580 	node.sysctl_data = &t;
   1581 
   1582 	err = sysctl_lookup(SYSCTLFN_CALL(&node));
   1583 
   1584 	if (err || newp == NULL)
   1585 		return err;
   1586 
   1587 	return 0;
   1588 }
   1589 
   1590 static int
   1591 sysctl_hw_acpi_sleepstate(SYSCTLFN_ARGS)
   1592 {
   1593 	struct acpi_softc *sc = acpi_softc;
   1594 	struct sysctlnode node;
   1595 	int err, t;
   1596 
   1597 	if (acpi_softc == NULL)
   1598 		return ENOSYS;
   1599 
   1600 	node = *rnode;
   1601 	t = sc->sc_sleepstate;
   1602 	node.sysctl_data = &t;
   1603 
   1604 	err = sysctl_lookup(SYSCTLFN_CALL(&node));
   1605 
   1606 	if (err || newp == NULL)
   1607 		return err;
   1608 
   1609 	if (t < ACPI_STATE_S0 || t > ACPI_STATE_S5)
   1610 		return EINVAL;
   1611 
   1612 	acpi_enter_sleep_state(sc, t);
   1613 
   1614 	return 0;
   1615 }
   1616 
   1617 static int
   1618 sysctl_hw_acpi_sleepstates(SYSCTLFN_ARGS)
   1619 {
   1620 	struct acpi_softc *sc = acpi_softc;
   1621 	struct sysctlnode node;
   1622 	char t[3 * 6 + 1];
   1623 	int err;
   1624 
   1625 	if (acpi_softc == NULL)
   1626 		return ENOSYS;
   1627 
   1628 	(void)memset(t, '\0', sizeof(t));
   1629 
   1630 	(void)snprintf(t, sizeof(t), "%s%s%s%s%s%s",
   1631 	    ((sc->sc_sleepstates & __BIT(0)) != 0) ? "S0 " : "",
   1632 	    ((sc->sc_sleepstates & __BIT(1)) != 0) ? "S1 " : "",
   1633 	    ((sc->sc_sleepstates & __BIT(2)) != 0) ? "S2 " : "",
   1634 	    ((sc->sc_sleepstates & __BIT(3)) != 0) ? "S3 " : "",
   1635 	    ((sc->sc_sleepstates & __BIT(4)) != 0) ? "S4 " : "",
   1636 	    ((sc->sc_sleepstates & __BIT(5)) != 0) ? "S5 " : "");
   1637 
   1638 	node = *rnode;
   1639 	node.sysctl_data = &t;
   1640 
   1641 	err = sysctl_lookup(SYSCTLFN_CALL(&node));
   1642 
   1643 	if (err || newp == NULL)
   1644 		return err;
   1645 
   1646 	return 0;
   1647 }
   1648 
   1649 /*
   1650  * Tables.
   1651  */
   1652 ACPI_PHYSICAL_ADDRESS
   1653 acpi_OsGetRootPointer(void)
   1654 {
   1655 	ACPI_PHYSICAL_ADDRESS PhysicalAddress;
   1656 
   1657 	/*
   1658 	 * We let MD code handle this since there are multiple ways to do it:
   1659 	 *
   1660 	 *	IA-32: Use AcpiFindRootPointer() to locate the RSDP.
   1661 	 *
   1662 	 *	IA-64: Use the EFI.
   1663 	 */
   1664 	PhysicalAddress = acpi_md_OsGetRootPointer();
   1665 
   1666 	if (acpi_root_pointer == 0)
   1667 		acpi_root_pointer = PhysicalAddress;
   1668 
   1669 	return PhysicalAddress;
   1670 }
   1671 
   1672 static ACPI_TABLE_HEADER *
   1673 acpi_map_rsdt(void)
   1674 {
   1675 	ACPI_PHYSICAL_ADDRESS paddr;
   1676 	ACPI_TABLE_RSDP *rsdp;
   1677 
   1678 	paddr = AcpiOsGetRootPointer();
   1679 
   1680 	if (paddr == 0)
   1681 		return NULL;
   1682 
   1683 	rsdp = AcpiOsMapMemory(paddr, sizeof(ACPI_TABLE_RSDP));
   1684 
   1685 	if (rsdp == NULL)
   1686 		return NULL;
   1687 
   1688 	if (rsdp->Revision > 1 && rsdp->XsdtPhysicalAddress)
   1689 		paddr = rsdp->XsdtPhysicalAddress;
   1690 	else
   1691 		paddr = rsdp->RsdtPhysicalAddress;
   1692 
   1693 	AcpiOsUnmapMemory(rsdp, sizeof(ACPI_TABLE_RSDP));
   1694 
   1695 	return AcpiOsMapMemory(paddr, sizeof(ACPI_TABLE_HEADER));
   1696 }
   1697 
   1698 static void
   1699 acpi_unmap_rsdt(ACPI_TABLE_HEADER *rsdt)
   1700 {
   1701 
   1702 	if (rsdt == NULL)
   1703 		return;
   1704 
   1705 	AcpiOsUnmapMemory(rsdt, sizeof(ACPI_TABLE_HEADER));
   1706 }
   1707 
   1708 /*
   1709  * XXX: Refactor to be a generic function that maps tables.
   1710  */
   1711 ACPI_STATUS
   1712 acpi_madt_map(void)
   1713 {
   1714 	ACPI_STATUS  rv;
   1715 
   1716 	if (madt_header != NULL)
   1717 		return AE_ALREADY_EXISTS;
   1718 
   1719 	rv = AcpiGetTable(ACPI_SIG_MADT, 1, &madt_header);
   1720 
   1721 	if (ACPI_FAILURE(rv))
   1722 		return rv;
   1723 
   1724 	return AE_OK;
   1725 }
   1726 
   1727 void
   1728 acpi_madt_unmap(void)
   1729 {
   1730 	madt_header = NULL;
   1731 }
   1732 
   1733 /*
   1734  * XXX: Refactor to be a generic function that walks tables.
   1735  */
   1736 void
   1737 acpi_madt_walk(ACPI_STATUS (*func)(ACPI_SUBTABLE_HEADER *, void *), void *aux)
   1738 {
   1739 	ACPI_SUBTABLE_HEADER *hdrp;
   1740 	char *madtend, *where;
   1741 
   1742 	madtend = (char *)madt_header + madt_header->Length;
   1743 	where = (char *)madt_header + sizeof (ACPI_TABLE_MADT);
   1744 
   1745 	while (where < madtend) {
   1746 
   1747 		hdrp = (ACPI_SUBTABLE_HEADER *)where;
   1748 
   1749 		if (ACPI_FAILURE(func(hdrp, aux)))
   1750 			break;
   1751 
   1752 		where += hdrp->Length;
   1753 	}
   1754 }
   1755 
   1756 /*
   1757  * Miscellaneous.
   1758  */
   1759 static bool
   1760 acpi_is_scope(struct acpi_devnode *ad)
   1761 {
   1762 	int i;
   1763 
   1764 	/*
   1765 	 * Return true if the node is a root scope.
   1766 	 */
   1767 	if (ad->ad_parent == NULL)
   1768 		return false;
   1769 
   1770 	if (ad->ad_parent->ad_handle != ACPI_ROOT_OBJECT)
   1771 		return false;
   1772 
   1773 	for (i = 0; i < __arraycount(acpi_scopes); i++) {
   1774 
   1775 		if (acpi_scopes[i] == NULL)
   1776 			continue;
   1777 
   1778 		if (ad->ad_handle == acpi_scopes[i])
   1779 			return true;
   1780 	}
   1781 
   1782 	return false;
   1783 }
   1784 
   1785 /*
   1786  * ACPIVERBOSE.
   1787  */
   1788 void
   1789 acpi_load_verbose(void)
   1790 {
   1791 
   1792 	if (acpi_verbose_loaded == 0)
   1793 		module_autoload("acpiverbose", MODULE_CLASS_MISC);
   1794 }
   1795 
   1796 void
   1797 acpi_print_verbose_stub(struct acpi_softc *sc)
   1798 {
   1799 
   1800 	acpi_load_verbose();
   1801 
   1802 	if (acpi_verbose_loaded != 0)
   1803 		acpi_print_verbose(sc);
   1804 }
   1805 
   1806 void
   1807 acpi_print_dev_stub(const char *pnpstr)
   1808 {
   1809 
   1810 	acpi_load_verbose();
   1811 
   1812 	if (acpi_verbose_loaded != 0)
   1813 		acpi_print_dev(pnpstr);
   1814 }
   1815