Home | History | Annotate | Line # | Download | only in acpi
acpi.c revision 1.148.2.3
      1 /*	$NetBSD: acpi.c,v 1.148.2.3 2010/10/22 07:21:51 uebayasi 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.148.2.3 2010/10/22 07:21:51 uebayasi 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_pcidev_scan(sc->sc_root);
    669 	(void)acpi_rescan(sc->sc_dev, NULL, NULL);
    670 
    671 	acpi_rescan_capabilities(sc);
    672 }
    673 
    674 static ACPI_STATUS
    675 acpi_make_devnode(ACPI_HANDLE handle, uint32_t level,
    676     void *context, void **status)
    677 {
    678 	struct acpi_walkcontext *awc = context;
    679 	struct acpi_softc *sc = awc->aw_sc;
    680 	struct acpi_devnode *ad;
    681 	ACPI_DEVICE_INFO *devinfo;
    682 	ACPI_OBJECT_TYPE type;
    683 	ACPI_NAME_UNION *anu;
    684 	ACPI_STATUS rv;
    685 	int clear, i;
    686 
    687 	rv = AcpiGetObjectInfo(handle, &devinfo);
    688 
    689 	if (ACPI_FAILURE(rv))
    690 		return AE_OK;	/* Do not terminate the walk. */
    691 
    692 	type = devinfo->Type;
    693 
    694 	switch (type) {
    695 
    696 	case ACPI_TYPE_DEVICE:
    697 
    698 #ifdef ACPI_ACTIVATE_DEV
    699 		acpi_activate_device(handle, &devinfo);
    700 #endif
    701 
    702 	case ACPI_TYPE_PROCESSOR:
    703 	case ACPI_TYPE_THERMAL:
    704 	case ACPI_TYPE_POWER:
    705 
    706 		ad = malloc(sizeof(*ad), M_ACPI, M_NOWAIT | M_ZERO);
    707 
    708 		if (ad == NULL)
    709 			return AE_NO_MEMORY;
    710 
    711 		ad->ad_device = NULL;
    712 		ad->ad_notify = NULL;
    713 		ad->ad_pciinfo = NULL;
    714 
    715 		ad->ad_type = type;
    716 		ad->ad_handle = handle;
    717 		ad->ad_devinfo = devinfo;
    718 
    719 		ad->ad_root = sc->sc_dev;
    720 		ad->ad_parent = awc->aw_parent;
    721 
    722 		anu = (ACPI_NAME_UNION *)&devinfo->Name;
    723 		ad->ad_name[4] = '\0';
    724 
    725 		for (i = 3, clear = 0; i >= 0; i--) {
    726 
    727 			if (clear == 0 && anu->Ascii[i] == '_')
    728 				ad->ad_name[i] = '\0';
    729 			else {
    730 				ad->ad_name[i] = anu->Ascii[i];
    731 				clear = 1;
    732 			}
    733 		}
    734 
    735 		if (ad->ad_name[0] == '\0')
    736 			ad->ad_name[0] = '_';
    737 
    738 		SIMPLEQ_INIT(&ad->ad_child_head);
    739 		SIMPLEQ_INSERT_TAIL(&sc->ad_head, ad, ad_list);
    740 
    741 		acpi_set_node(ad);
    742 
    743 		if (ad->ad_parent != NULL) {
    744 
    745 			SIMPLEQ_INSERT_TAIL(&ad->ad_parent->ad_child_head,
    746 			    ad, ad_child_list);
    747 		}
    748 
    749 		awc->aw_parent = ad;
    750 	}
    751 
    752 	return AE_OK;
    753 }
    754 
    755 static ACPI_STATUS
    756 acpi_make_devnode_post(ACPI_HANDLE handle, uint32_t level,
    757     void *context, void **status)
    758 {
    759 	struct acpi_walkcontext *awc = context;
    760 
    761 	KASSERT(awc != NULL);
    762 	KASSERT(awc->aw_parent != NULL);
    763 
    764 	if (handle == awc->aw_parent->ad_handle)
    765 		awc->aw_parent = awc->aw_parent->ad_parent;
    766 
    767 	return AE_OK;
    768 }
    769 
    770 #ifdef ACPI_ACTIVATE_DEV
    771 static void
    772 acpi_activate_device(ACPI_HANDLE handle, ACPI_DEVICE_INFO **di)
    773 {
    774 	static const int valid = ACPI_VALID_STA | ACPI_VALID_HID;
    775 	ACPI_DEVICE_INFO *newdi;
    776 	ACPI_STATUS rv;
    777 	uint32_t old;
    778 
    779 	/*
    780 	 * If the device is valid and present,
    781 	 * but not enabled, try to activate it.
    782 	 */
    783 	if (((*di)->Valid & valid) != valid)
    784 		return;
    785 
    786 	old = (*di)->CurrentStatus;
    787 
    788 	if ((old & (ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_ENABLED)) !=
    789 	    ACPI_STA_DEVICE_PRESENT)
    790 		return;
    791 
    792 	rv = acpi_allocate_resources(handle);
    793 
    794 	if (ACPI_FAILURE(rv))
    795 		goto fail;
    796 
    797 	rv = AcpiGetObjectInfo(handle, &newdi);
    798 
    799 	if (ACPI_FAILURE(rv))
    800 		goto fail;
    801 
    802 	ACPI_FREE(*di);
    803 	*di = newdi;
    804 
    805 	aprint_verbose_dev(acpi_softc->sc_dev,
    806 	    "%s activated, STA 0x%08X -> STA 0x%08X\n",
    807 	    (*di)->HardwareId.String, old, (*di)->CurrentStatus);
    808 
    809 	return;
    810 
    811 fail:
    812 	aprint_error_dev(acpi_softc->sc_dev, "failed to "
    813 	    "activate %s\n", (*di)->HardwareId.String);
    814 }
    815 
    816 /*
    817  * XXX: This very incomplete.
    818  */
    819 ACPI_STATUS
    820 acpi_allocate_resources(ACPI_HANDLE handle)
    821 {
    822 	ACPI_BUFFER bufp, bufc, bufn;
    823 	ACPI_RESOURCE *resp, *resc, *resn;
    824 	ACPI_RESOURCE_IRQ *irq;
    825 	ACPI_RESOURCE_EXTENDED_IRQ *xirq;
    826 	ACPI_STATUS rv;
    827 	uint delta;
    828 
    829 	rv = acpi_get(handle, &bufp, AcpiGetPossibleResources);
    830 	if (ACPI_FAILURE(rv))
    831 		goto out;
    832 	rv = acpi_get(handle, &bufc, AcpiGetCurrentResources);
    833 	if (ACPI_FAILURE(rv)) {
    834 		goto out1;
    835 	}
    836 
    837 	bufn.Length = 1000;
    838 	bufn.Pointer = resn = malloc(bufn.Length, M_ACPI, M_WAITOK);
    839 	resp = bufp.Pointer;
    840 	resc = bufc.Pointer;
    841 	while (resc->Type != ACPI_RESOURCE_TYPE_END_TAG &&
    842 	       resp->Type != ACPI_RESOURCE_TYPE_END_TAG) {
    843 		while (resc->Type != resp->Type && resp->Type != ACPI_RESOURCE_TYPE_END_TAG)
    844 			resp = ACPI_NEXT_RESOURCE(resp);
    845 		if (resp->Type == ACPI_RESOURCE_TYPE_END_TAG)
    846 			break;
    847 		/* Found identical Id */
    848 		resn->Type = resc->Type;
    849 		switch (resc->Type) {
    850 		case ACPI_RESOURCE_TYPE_IRQ:
    851 			memcpy(&resn->Data, &resp->Data,
    852 			       sizeof(ACPI_RESOURCE_IRQ));
    853 			irq = (ACPI_RESOURCE_IRQ *)&resn->Data;
    854 			irq->Interrupts[0] =
    855 			    ((ACPI_RESOURCE_IRQ *)&resp->Data)->
    856 			        Interrupts[irq->InterruptCount-1];
    857 			irq->InterruptCount = 1;
    858 			resn->Length = ACPI_RS_SIZE(ACPI_RESOURCE_IRQ);
    859 			break;
    860 		case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
    861 			memcpy(&resn->Data, &resp->Data,
    862 			       sizeof(ACPI_RESOURCE_EXTENDED_IRQ));
    863 			xirq = (ACPI_RESOURCE_EXTENDED_IRQ *)&resn->Data;
    864 #if 0
    865 			/*
    866 			 * XXX:	Not duplicating the interrupt logic above
    867 			 *	because its not clear what it accomplishes.
    868 			 */
    869 			xirq->Interrupts[0] =
    870 			    ((ACPI_RESOURCE_EXT_IRQ *)&resp->Data)->
    871 			    Interrupts[irq->NumberOfInterrupts-1];
    872 			xirq->NumberOfInterrupts = 1;
    873 #endif
    874 			resn->Length = ACPI_RS_SIZE(ACPI_RESOURCE_EXTENDED_IRQ);
    875 			break;
    876 		case ACPI_RESOURCE_TYPE_IO:
    877 			memcpy(&resn->Data, &resp->Data,
    878 			       sizeof(ACPI_RESOURCE_IO));
    879 			resn->Length = resp->Length;
    880 			break;
    881 		default:
    882 			aprint_error_dev(acpi_softc->sc_dev,
    883 			    "%s: invalid type %u\n", __func__, resc->Type);
    884 			rv = AE_BAD_DATA;
    885 			goto out2;
    886 		}
    887 		resc = ACPI_NEXT_RESOURCE(resc);
    888 		resn = ACPI_NEXT_RESOURCE(resn);
    889 		resp = ACPI_NEXT_RESOURCE(resp);
    890 		delta = (uint8_t *)resn - (uint8_t *)bufn.Pointer;
    891 		if (delta >=
    892 		    bufn.Length-ACPI_RS_SIZE(ACPI_RESOURCE_DATA)) {
    893 			bufn.Length *= 2;
    894 			bufn.Pointer = realloc(bufn.Pointer, bufn.Length,
    895 					       M_ACPI, M_WAITOK);
    896 			resn = (ACPI_RESOURCE *)((uint8_t *)bufn.Pointer +
    897 			    delta);
    898 		}
    899 	}
    900 
    901 	if (resc->Type != ACPI_RESOURCE_TYPE_END_TAG) {
    902 		aprint_error_dev(acpi_softc->sc_dev,
    903 		    "%s: resc not exhausted\n", __func__);
    904 		rv = AE_BAD_DATA;
    905 		goto out3;
    906 	}
    907 
    908 	resn->Type = ACPI_RESOURCE_TYPE_END_TAG;
    909 	rv = AcpiSetCurrentResources(handle, &bufn);
    910 
    911 	if (ACPI_FAILURE(rv))
    912 		aprint_error_dev(acpi_softc->sc_dev, "%s: failed to set "
    913 		    "resources: %s\n", __func__, AcpiFormatException(rv));
    914 
    915 out3:
    916 	free(bufn.Pointer, M_ACPI);
    917 out2:
    918 	ACPI_FREE(bufc.Pointer);
    919 out1:
    920 	ACPI_FREE(bufp.Pointer);
    921 out:
    922 	return rv;
    923 }
    924 #endif /* ACPI_ACTIVATE_DEV */
    925 
    926 /*
    927  * Device attachment.
    928  */
    929 static int
    930 acpi_rescan(device_t self, const char *ifattr, const int *locators)
    931 {
    932 	struct acpi_softc *sc = device_private(self);
    933 
    934 	if (ifattr_match(ifattr, "acpinodebus"))
    935 		acpi_rescan_nodes(sc);
    936 
    937 	if (ifattr_match(ifattr, "acpiapmbus") && sc->sc_apmbus == NULL)
    938 		sc->sc_apmbus = config_found_ia(sc->sc_dev,
    939 		    "acpiapmbus", NULL, NULL);
    940 
    941 	return 0;
    942 }
    943 
    944 static void
    945 acpi_rescan_nodes(struct acpi_softc *sc)
    946 {
    947 	struct acpi_attach_args aa;
    948 	struct acpi_devnode *ad;
    949 	ACPI_DEVICE_INFO *di;
    950 
    951 	SIMPLEQ_FOREACH(ad, &sc->ad_head, ad_list) {
    952 
    953 		if (ad->ad_device != NULL)
    954 			continue;
    955 
    956 		/*
    957 		 * There is a bug in ACPICA: it defines the type
    958 		 * of the scopes incorrectly for its own reasons.
    959 		 */
    960 		if (acpi_is_scope(ad) != false)
    961 			continue;
    962 
    963 		di = ad->ad_devinfo;
    964 
    965 		/*
    966 		 * We only attach devices which are present, enabled, and
    967 		 * functioning properly. However, if a device is enabled,
    968 		 * it is decoding resources and we should claim these,
    969 		 * if possible. This requires changes to bus_space(9).
    970 		 * Note: there is a possible race condition, because _STA
    971 		 * may have changed since di->CurrentStatus was set.
    972 		 */
    973 		if (di->Type == ACPI_TYPE_DEVICE) {
    974 
    975 			if ((di->Valid & ACPI_VALID_STA) != 0 &&
    976 			    (di->CurrentStatus & ACPI_STA_OK) != ACPI_STA_OK)
    977 				continue;
    978 		}
    979 
    980 		/*
    981 		 * Handled internally.
    982 		 */
    983 		if (di->Type == ACPI_TYPE_POWER)
    984 			continue;
    985 
    986 		/*
    987 		 * Skip ignored HIDs.
    988 		 */
    989 		if (acpi_match_hid(di, acpi_ignored_ids))
    990 			continue;
    991 
    992 		aa.aa_node = ad;
    993 		aa.aa_iot = sc->sc_iot;
    994 		aa.aa_memt = sc->sc_memt;
    995 		aa.aa_pc = sc->sc_pc;
    996 		aa.aa_pciflags = sc->sc_pciflags;
    997 		aa.aa_ic = sc->sc_ic;
    998 
    999 		ad->ad_device = config_found_ia(sc->sc_dev,
   1000 		    "acpinodebus", &aa, acpi_print);
   1001 	}
   1002 }
   1003 
   1004 static void
   1005 acpi_rescan_capabilities(struct acpi_softc *sc)
   1006 {
   1007 	struct acpi_devnode *ad;
   1008 	ACPI_HANDLE tmp;
   1009 	ACPI_STATUS rv;
   1010 
   1011 	SIMPLEQ_FOREACH(ad, &sc->ad_head, ad_list) {
   1012 
   1013 		if (ad->ad_devinfo->Type != ACPI_TYPE_DEVICE)
   1014 			continue;
   1015 
   1016 		/*
   1017 		 * Scan power resource capabilities.
   1018 		 *
   1019 		 * If any power states are supported,
   1020 		 * at least _PR0 and _PR3 must be present.
   1021 		 */
   1022 		rv = AcpiGetHandle(ad->ad_handle, "_PR0", &tmp);
   1023 
   1024 		if (ACPI_SUCCESS(rv)) {
   1025 			ad->ad_flags |= ACPI_DEVICE_POWER;
   1026 			acpi_power_add(ad);
   1027 		}
   1028 
   1029 		/*
   1030 		 * Scan wake-up capabilities.
   1031 		 */
   1032 		rv = AcpiGetHandle(ad->ad_handle, "_PRW", &tmp);
   1033 
   1034 		if (ACPI_SUCCESS(rv)) {
   1035 			ad->ad_flags |= ACPI_DEVICE_WAKEUP;
   1036 			acpi_wakedev_add(ad);
   1037 		}
   1038 	}
   1039 }
   1040 
   1041 static int
   1042 acpi_print(void *aux, const char *pnp)
   1043 {
   1044 	struct acpi_attach_args *aa = aux;
   1045 	ACPI_STATUS rv;
   1046 
   1047 	if (pnp) {
   1048 		if (aa->aa_node->ad_devinfo->Valid & ACPI_VALID_HID) {
   1049 			char *pnpstr =
   1050 			    aa->aa_node->ad_devinfo->HardwareId.String;
   1051 			ACPI_BUFFER buf;
   1052 
   1053 			aprint_normal("%s (%s) ", aa->aa_node->ad_name,
   1054 			    pnpstr);
   1055 
   1056 			rv = acpi_eval_struct(aa->aa_node->ad_handle,
   1057 			    "_STR", &buf);
   1058 			if (ACPI_SUCCESS(rv)) {
   1059 				ACPI_OBJECT *obj = buf.Pointer;
   1060 				switch (obj->Type) {
   1061 				case ACPI_TYPE_STRING:
   1062 					aprint_normal("[%s] ", obj->String.Pointer);
   1063 					break;
   1064 				case ACPI_TYPE_BUFFER:
   1065 					aprint_normal("buffer %p ", obj->Buffer.Pointer);
   1066 					break;
   1067 				default:
   1068 					aprint_normal("type %u ",obj->Type);
   1069 					break;
   1070 				}
   1071 				ACPI_FREE(buf.Pointer);
   1072 			}
   1073 			else
   1074 				acpi_print_dev(pnpstr);
   1075 
   1076 			aprint_normal("at %s", pnp);
   1077 		} else if (aa->aa_node->ad_devinfo->Type != ACPI_TYPE_DEVICE) {
   1078 			aprint_normal("%s (ACPI Object Type '%s' "
   1079 			    "[0x%02x]) ", aa->aa_node->ad_name,
   1080 			     AcpiUtGetTypeName(aa->aa_node->ad_devinfo->Type),
   1081 			     aa->aa_node->ad_devinfo->Type);
   1082 			aprint_normal("at %s", pnp);
   1083 		} else
   1084 			return 0;
   1085 	} else {
   1086 		aprint_normal(" (%s", aa->aa_node->ad_name);
   1087 		if (aa->aa_node->ad_devinfo->Valid & ACPI_VALID_HID) {
   1088 			aprint_normal(", %s", aa->aa_node->ad_devinfo->HardwareId.String);
   1089 			if (aa->aa_node->ad_devinfo->Valid & ACPI_VALID_UID) {
   1090 				const char *uid;
   1091 
   1092 				uid = aa->aa_node->ad_devinfo->UniqueId.String;
   1093 				if (uid[0] == '\0')
   1094 					uid = "<null>";
   1095 				aprint_normal("-%s", uid);
   1096 			}
   1097 		}
   1098 		aprint_normal(")");
   1099 	}
   1100 
   1101 	return UNCONF;
   1102 }
   1103 
   1104 /*
   1105  * Notify.
   1106  */
   1107 static void
   1108 acpi_notify_handler(ACPI_HANDLE handle, uint32_t event, void *aux)
   1109 {
   1110 	struct acpi_softc *sc = acpi_softc;
   1111 	struct acpi_devnode *ad;
   1112 
   1113 	KASSERT(sc != NULL);
   1114 	KASSERT(aux == NULL);
   1115 	KASSERT(acpi_active != 0);
   1116 
   1117 	if (acpi_suspended != 0)
   1118 		return;
   1119 
   1120 	/*
   1121 	 *  System: 0x00 - 0x7F.
   1122 	 *  Device: 0x80 - 0xFF.
   1123 	 */
   1124 	switch (event) {
   1125 
   1126 	case ACPI_NOTIFY_BUS_CHECK:
   1127 	case ACPI_NOTIFY_DEVICE_CHECK:
   1128 	case ACPI_NOTIFY_DEVICE_WAKE:
   1129 	case ACPI_NOTIFY_EJECT_REQUEST:
   1130 	case ACPI_NOTIFY_DEVICE_CHECK_LIGHT:
   1131 	case ACPI_NOTIFY_FREQUENCY_MISMATCH:
   1132 	case ACPI_NOTIFY_BUS_MODE_MISMATCH:
   1133 	case ACPI_NOTIFY_POWER_FAULT:
   1134 	case ACPI_NOTIFY_CAPABILITIES_CHECK:
   1135 	case ACPI_NOTIFY_DEVICE_PLD_CHECK:
   1136 	case ACPI_NOTIFY_RESERVED:
   1137 	case ACPI_NOTIFY_LOCALITY_UPDATE:
   1138 		break;
   1139 	}
   1140 
   1141 	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "notification 0x%02X for "
   1142 		"%s (%p)\n", event, acpi_name(handle), handle));
   1143 
   1144 	/*
   1145 	 * We deliver notifications only to drivers
   1146 	 * that have been succesfully attached and
   1147 	 * that have registered a handler with us.
   1148 	 * The opaque pointer is always the device_t.
   1149 	 */
   1150 	SIMPLEQ_FOREACH(ad, &sc->ad_head, ad_list) {
   1151 
   1152 		if (ad->ad_device == NULL)
   1153 			continue;
   1154 
   1155 		if (ad->ad_notify == NULL)
   1156 			continue;
   1157 
   1158 		if (ad->ad_handle != handle)
   1159 			continue;
   1160 
   1161 		(*ad->ad_notify)(ad->ad_handle, event, ad->ad_device);
   1162 
   1163 		return;
   1164 	}
   1165 
   1166 	aprint_debug_dev(sc->sc_dev, "unhandled notify 0x%02X "
   1167 	    "for %s (%p)\n", event, acpi_name(handle), handle);
   1168 }
   1169 
   1170 bool
   1171 acpi_register_notify(struct acpi_devnode *ad, ACPI_NOTIFY_HANDLER notify)
   1172 {
   1173 	struct acpi_softc *sc = acpi_softc;
   1174 
   1175 	KASSERT(sc != NULL);
   1176 	KASSERT(acpi_active != 0);
   1177 
   1178 	if (acpi_suspended != 0)
   1179 		goto fail;
   1180 
   1181 	if (ad == NULL || notify == NULL)
   1182 		goto fail;
   1183 
   1184 	ad->ad_notify = notify;
   1185 
   1186 	return true;
   1187 
   1188 fail:
   1189 	aprint_error_dev(sc->sc_dev, "failed to register notify "
   1190 	    "handler for %s (%p)\n", ad->ad_name, ad->ad_handle);
   1191 
   1192 	return false;
   1193 }
   1194 
   1195 void
   1196 acpi_deregister_notify(struct acpi_devnode *ad)
   1197 {
   1198 
   1199 	ad->ad_notify = NULL;
   1200 }
   1201 
   1202 /*
   1203  * Fixed buttons.
   1204  */
   1205 static void
   1206 acpi_register_fixed_button(struct acpi_softc *sc, int event)
   1207 {
   1208 	struct sysmon_pswitch *smpsw;
   1209 	ACPI_STATUS rv;
   1210 	int type;
   1211 
   1212 	switch (event) {
   1213 
   1214 	case ACPI_EVENT_POWER_BUTTON:
   1215 
   1216 		if ((AcpiGbl_FADT.Flags & ACPI_FADT_POWER_BUTTON) != 0)
   1217 			return;
   1218 
   1219 		type = PSWITCH_TYPE_POWER;
   1220 		smpsw = &sc->sc_smpsw_power;
   1221 		break;
   1222 
   1223 	case ACPI_EVENT_SLEEP_BUTTON:
   1224 
   1225 		if ((AcpiGbl_FADT.Flags & ACPI_FADT_SLEEP_BUTTON) != 0)
   1226 			return;
   1227 
   1228 		type = PSWITCH_TYPE_SLEEP;
   1229 		smpsw = &sc->sc_smpsw_sleep;
   1230 		break;
   1231 
   1232 	default:
   1233 		rv = AE_TYPE;
   1234 		goto fail;
   1235 	}
   1236 
   1237 	smpsw->smpsw_type = type;
   1238 	smpsw->smpsw_name = device_xname(sc->sc_dev);
   1239 
   1240 	if (sysmon_pswitch_register(smpsw) != 0) {
   1241 		rv = AE_ERROR;
   1242 		goto fail;
   1243 	}
   1244 
   1245 	rv = AcpiInstallFixedEventHandler(event,
   1246 	    acpi_fixed_button_handler, smpsw);
   1247 
   1248 	if (ACPI_FAILURE(rv)) {
   1249 		sysmon_pswitch_unregister(smpsw);
   1250 		goto fail;
   1251 	}
   1252 
   1253 	aprint_debug_dev(sc->sc_dev, "fixed %s button present\n",
   1254 	    (type != ACPI_EVENT_SLEEP_BUTTON) ? "power" : "sleep");
   1255 
   1256 	return;
   1257 
   1258 fail:
   1259 	aprint_error_dev(sc->sc_dev, "failed to register "
   1260 	    "fixed event: %s\n", AcpiFormatException(rv));
   1261 }
   1262 
   1263 static void
   1264 acpi_deregister_fixed_button(struct acpi_softc *sc, int event)
   1265 {
   1266 	struct sysmon_pswitch *smpsw;
   1267 	ACPI_STATUS rv;
   1268 
   1269 	switch (event) {
   1270 
   1271 	case ACPI_EVENT_POWER_BUTTON:
   1272 		smpsw = &sc->sc_smpsw_power;
   1273 
   1274 		if ((AcpiGbl_FADT.Flags & ACPI_FADT_POWER_BUTTON) != 0) {
   1275 			KASSERT(smpsw->smpsw_type != PSWITCH_TYPE_POWER);
   1276 			return;
   1277 		}
   1278 
   1279 		break;
   1280 
   1281 	case ACPI_EVENT_SLEEP_BUTTON:
   1282 		smpsw = &sc->sc_smpsw_sleep;
   1283 
   1284 		if ((AcpiGbl_FADT.Flags & ACPI_FADT_SLEEP_BUTTON) != 0) {
   1285 			KASSERT(smpsw->smpsw_type != PSWITCH_TYPE_SLEEP);
   1286 			return;
   1287 		}
   1288 
   1289 		break;
   1290 
   1291 	default:
   1292 		rv = AE_TYPE;
   1293 		goto fail;
   1294 	}
   1295 
   1296 	rv = AcpiRemoveFixedEventHandler(event, acpi_fixed_button_handler);
   1297 
   1298 	if (ACPI_SUCCESS(rv)) {
   1299 		sysmon_pswitch_unregister(smpsw);
   1300 		return;
   1301 	}
   1302 
   1303 fail:
   1304 	aprint_error_dev(sc->sc_dev, "failed to deregister "
   1305 	    "fixed event: %s\n", AcpiFormatException(rv));
   1306 }
   1307 
   1308 static uint32_t
   1309 acpi_fixed_button_handler(void *context)
   1310 {
   1311 	static const int handler = OSL_NOTIFY_HANDLER;
   1312 	struct sysmon_pswitch *smpsw = context;
   1313 
   1314 	(void)AcpiOsExecute(handler, acpi_fixed_button_pressed, smpsw);
   1315 
   1316 	return ACPI_INTERRUPT_HANDLED;
   1317 }
   1318 
   1319 static void
   1320 acpi_fixed_button_pressed(void *context)
   1321 {
   1322 	struct sysmon_pswitch *smpsw = context;
   1323 
   1324 	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "%s fixed button pressed\n",
   1325 		(smpsw->smpsw_type != ACPI_EVENT_SLEEP_BUTTON) ?
   1326 		"power" : "sleep"));
   1327 
   1328 	sysmon_pswitch_event(smpsw, PSWITCH_EVENT_PRESSED);
   1329 }
   1330 
   1331 /*
   1332  * Sleep.
   1333  */
   1334 static void
   1335 acpi_sleep_init(struct acpi_softc *sc)
   1336 {
   1337 	uint8_t a, b, i;
   1338 	ACPI_STATUS rv;
   1339 
   1340 	CTASSERT(ACPI_STATE_S0 == 0 && ACPI_STATE_S1 == 1);
   1341 	CTASSERT(ACPI_STATE_S2 == 2 && ACPI_STATE_S3 == 3);
   1342 	CTASSERT(ACPI_STATE_S4 == 4 && ACPI_STATE_S5 == 5);
   1343 
   1344 	/*
   1345 	 * Evaluate supported sleep states.
   1346 	 */
   1347 	for (i = ACPI_STATE_S0; i <= ACPI_STATE_S5; i++) {
   1348 
   1349 		rv = AcpiGetSleepTypeData(i, &a, &b);
   1350 
   1351 		if (ACPI_SUCCESS(rv))
   1352 			sc->sc_sleepstates |= __BIT(i);
   1353 	}
   1354 }
   1355 
   1356 void
   1357 acpi_enter_sleep_state(struct acpi_softc *sc, int state)
   1358 {
   1359 	ACPI_STATUS rv;
   1360 	int err;
   1361 
   1362 	if (state == sc->sc_sleepstate)
   1363 		return;
   1364 
   1365 	aprint_normal_dev(sc->sc_dev, "entering state S%d\n", state);
   1366 
   1367 	switch (state) {
   1368 
   1369 	case ACPI_STATE_S0:
   1370 		sc->sc_sleepstate = ACPI_STATE_S0;
   1371 		return;
   1372 
   1373 	case ACPI_STATE_S1:
   1374 	case ACPI_STATE_S2:
   1375 	case ACPI_STATE_S3:
   1376 	case ACPI_STATE_S4:
   1377 
   1378 		if ((sc->sc_sleepstates & __BIT(state)) == 0) {
   1379 			aprint_error_dev(sc->sc_dev, "sleep state "
   1380 			    "S%d is not available\n", state);
   1381 			return;
   1382 		}
   1383 
   1384 		/*
   1385 		 * Evaluate the _TTS method. This should be done before
   1386 		 * pmf_system_suspend(9) and the evaluation of _PTS.
   1387 		 * We should also re-evaluate this once we return to
   1388 		 * S0 or if we abort the sleep state transition in the
   1389 		 * middle (see ACPI 3.0, section 7.3.6). In reality,
   1390 		 * however, the _TTS method is seldom seen in the field.
   1391 		 */
   1392 		rv = acpi_eval_set_integer(NULL, "\\_TTS", state);
   1393 
   1394 		if (ACPI_SUCCESS(rv))
   1395 			aprint_debug_dev(sc->sc_dev, "evaluated _TTS\n");
   1396 
   1397 		if (state != ACPI_STATE_S1 &&
   1398 		    pmf_system_suspend(PMF_Q_NONE) != true) {
   1399 			aprint_error_dev(sc->sc_dev, "aborting suspend\n");
   1400 			break;
   1401 		}
   1402 
   1403 		/*
   1404 		 * This will evaluate the  _PTS and _SST methods,
   1405 		 * but unlike the documentation claims, not _GTS,
   1406 		 * which is evaluated in AcpiEnterSleepState().
   1407 		 * This must be called with interrupts enabled.
   1408 		 */
   1409 		rv = AcpiEnterSleepStatePrep(state);
   1410 
   1411 		if (ACPI_FAILURE(rv)) {
   1412 			aprint_error_dev(sc->sc_dev, "failed to prepare "
   1413 			    "S%d: %s\n", state, AcpiFormatException(rv));
   1414 			break;
   1415 		}
   1416 
   1417 		/*
   1418 		 * After the _PTS method has been evaluated, we can
   1419 		 * enable wake and evaluate _PSW (ACPI 4.0, p. 284).
   1420 		 */
   1421 		acpi_wakedev_commit(sc, state);
   1422 
   1423 		sc->sc_sleepstate = state;
   1424 
   1425 		if (state == ACPI_STATE_S1) {
   1426 
   1427 			/* Just enter the state. */
   1428 			acpi_md_OsDisableInterrupt();
   1429 			rv = AcpiEnterSleepState(state);
   1430 
   1431 			if (ACPI_FAILURE(rv))
   1432 				aprint_error_dev(sc->sc_dev, "failed to "
   1433 				    "enter S1: %s\n", AcpiFormatException(rv));
   1434 
   1435 			(void)AcpiLeaveSleepState(state);
   1436 
   1437 		} else {
   1438 
   1439 			err = acpi_md_sleep(state);
   1440 
   1441 			if (state == ACPI_STATE_S4)
   1442 				AcpiEnable();
   1443 
   1444 			pmf_system_bus_resume(PMF_Q_NONE);
   1445 			(void)AcpiLeaveSleepState(state);
   1446 			pmf_system_resume(PMF_Q_NONE);
   1447 		}
   1448 
   1449 		break;
   1450 
   1451 	case ACPI_STATE_S5:
   1452 
   1453 		(void)acpi_eval_set_integer(NULL, "\\_TTS", ACPI_STATE_S5);
   1454 
   1455 		rv = AcpiEnterSleepStatePrep(ACPI_STATE_S5);
   1456 
   1457 		if (ACPI_FAILURE(rv)) {
   1458 			aprint_error_dev(sc->sc_dev, "failed to prepare "
   1459 			    "S%d: %s\n", state, AcpiFormatException(rv));
   1460 			break;
   1461 		}
   1462 
   1463 		DELAY(1000000);
   1464 
   1465 		sc->sc_sleepstate = state;
   1466 		acpi_md_OsDisableInterrupt();
   1467 
   1468 		(void)AcpiEnterSleepState(ACPI_STATE_S5);
   1469 
   1470 		aprint_error_dev(sc->sc_dev, "WARNING: powerdown failed!\n");
   1471 
   1472 		break;
   1473 	}
   1474 
   1475 	sc->sc_sleepstate = ACPI_STATE_S0;
   1476 
   1477 	(void)acpi_eval_set_integer(NULL, "\\_TTS", ACPI_STATE_S0);
   1478 }
   1479 
   1480 /*
   1481  * Sysctl.
   1482  */
   1483 SYSCTL_SETUP(sysctl_acpi_setup, "sysctl hw.acpi subtree setup")
   1484 {
   1485 	const struct sysctlnode *mnode, *rnode;
   1486 	int err;
   1487 
   1488 	err = sysctl_createv(clog, 0, NULL, &rnode,
   1489 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "hw",
   1490 	    NULL, NULL, 0, NULL, 0,
   1491 	    CTL_HW, CTL_EOL);
   1492 
   1493 	if (err != 0)
   1494 		return;
   1495 
   1496 	err = sysctl_createv(clog, 0, &rnode, &rnode,
   1497 	    CTLFLAG_PERMANENT, CTLTYPE_NODE,
   1498 	    "acpi", SYSCTL_DESCR("ACPI subsystem parameters"),
   1499 	    NULL, 0, NULL, 0,
   1500 	    CTL_CREATE, CTL_EOL);
   1501 
   1502 	if (err != 0)
   1503 		return;
   1504 
   1505 	(void)sysctl_createv(NULL, 0, &rnode, NULL,
   1506 	    CTLFLAG_PERMANENT | CTLFLAG_READONLY, CTLTYPE_QUAD,
   1507 	    "root", SYSCTL_DESCR("ACPI root pointer"),
   1508 	    NULL, 0, &acpi_root_pointer, sizeof(acpi_root_pointer),
   1509 	    CTL_CREATE, CTL_EOL);
   1510 
   1511 	(void)sysctl_createv(NULL, 0, &rnode, NULL,
   1512 	    CTLFLAG_PERMANENT | CTLFLAG_READONLY, CTLTYPE_STRING,
   1513 	    "supported_states", SYSCTL_DESCR("Supported system states"),
   1514 	    sysctl_hw_acpi_sleepstates, 0, NULL, 0,
   1515 	    CTL_CREATE, CTL_EOL);
   1516 
   1517 	err = sysctl_createv(NULL, 0, NULL, &mnode,
   1518 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "machdep",
   1519 	    NULL, NULL, 0, NULL, 0,
   1520 	    CTL_MACHDEP, CTL_EOL);
   1521 
   1522 	if (err == 0) {
   1523 
   1524 		(void)sysctl_createv(NULL, 0, &mnode, NULL,
   1525 		    CTLFLAG_PERMANENT | CTLFLAG_READWRITE, CTLTYPE_INT,
   1526 		    "sleep_state", SYSCTL_DESCR("System sleep state"),
   1527 		    sysctl_hw_acpi_sleepstate, 0, NULL, 0,
   1528 		    CTL_CREATE, CTL_EOL);
   1529 	}
   1530 
   1531 	err = sysctl_createv(clog, 0, &rnode, &rnode,
   1532 	    CTLFLAG_PERMANENT, CTLTYPE_NODE,
   1533 	    "stat", SYSCTL_DESCR("ACPI statistics"),
   1534 	    NULL, 0, NULL, 0,
   1535 	    CTL_CREATE, CTL_EOL);
   1536 
   1537 	if (err != 0)
   1538 		return;
   1539 
   1540 	(void)sysctl_createv(clog, 0, &rnode, NULL,
   1541 	    CTLFLAG_PERMANENT | CTLFLAG_READONLY, CTLTYPE_QUAD,
   1542 	    "gpe", SYSCTL_DESCR("Number of dispatched GPEs"),
   1543 	    NULL, 0, &AcpiGpeCount, sizeof(AcpiGpeCount),
   1544 	    CTL_CREATE, CTL_EOL);
   1545 
   1546 	(void)sysctl_createv(clog, 0, &rnode, NULL,
   1547 	    CTLFLAG_PERMANENT | CTLFLAG_READONLY, CTLTYPE_QUAD,
   1548 	    "sci", SYSCTL_DESCR("Number of SCI interrupts"),
   1549 	    NULL, 0, &AcpiSciCount, sizeof(AcpiSciCount),
   1550 	    CTL_CREATE, CTL_EOL);
   1551 
   1552 	(void)sysctl_createv(clog, 0, &rnode, NULL,
   1553 	    CTLFLAG_PERMANENT | CTLFLAG_READONLY, CTLTYPE_QUAD,
   1554 	    "fixed", SYSCTL_DESCR("Number of fixed events"),
   1555 	    sysctl_hw_acpi_fixedstats, 0, NULL, 0,
   1556 	    CTL_CREATE, CTL_EOL);
   1557 
   1558 	(void)sysctl_createv(clog, 0, &rnode, NULL,
   1559 	    CTLFLAG_PERMANENT | CTLFLAG_READONLY, CTLTYPE_QUAD,
   1560 	    "method", SYSCTL_DESCR("Number of methods executed"),
   1561 	    NULL, 0, &AcpiMethodCount, sizeof(AcpiMethodCount),
   1562 	    CTL_CREATE, CTL_EOL);
   1563 
   1564 	CTASSERT(sizeof(AcpiGpeCount) == sizeof(uint64_t));
   1565 	CTASSERT(sizeof(AcpiSciCount) == sizeof(uint64_t));
   1566 }
   1567 
   1568 static int
   1569 sysctl_hw_acpi_fixedstats(SYSCTLFN_ARGS)
   1570 {
   1571 	struct sysctlnode node;
   1572 	uint64_t t;
   1573 	int err, i;
   1574 
   1575 	for (i = t = 0; i < __arraycount(AcpiFixedEventCount); i++)
   1576 		t += AcpiFixedEventCount[i];
   1577 
   1578 	node = *rnode;
   1579 	node.sysctl_data = &t;
   1580 
   1581 	err = sysctl_lookup(SYSCTLFN_CALL(&node));
   1582 
   1583 	if (err || newp == NULL)
   1584 		return err;
   1585 
   1586 	return 0;
   1587 }
   1588 
   1589 static int
   1590 sysctl_hw_acpi_sleepstate(SYSCTLFN_ARGS)
   1591 {
   1592 	struct acpi_softc *sc = acpi_softc;
   1593 	struct sysctlnode node;
   1594 	int err, t;
   1595 
   1596 	if (acpi_softc == NULL)
   1597 		return ENOSYS;
   1598 
   1599 	node = *rnode;
   1600 	t = sc->sc_sleepstate;
   1601 	node.sysctl_data = &t;
   1602 
   1603 	err = sysctl_lookup(SYSCTLFN_CALL(&node));
   1604 
   1605 	if (err || newp == NULL)
   1606 		return err;
   1607 
   1608 	if (t < ACPI_STATE_S0 || t > ACPI_STATE_S5)
   1609 		return EINVAL;
   1610 
   1611 	acpi_enter_sleep_state(sc, t);
   1612 
   1613 	return 0;
   1614 }
   1615 
   1616 static int
   1617 sysctl_hw_acpi_sleepstates(SYSCTLFN_ARGS)
   1618 {
   1619 	struct acpi_softc *sc = acpi_softc;
   1620 	struct sysctlnode node;
   1621 	char t[3 * 6 + 1];
   1622 	int err;
   1623 
   1624 	if (acpi_softc == NULL)
   1625 		return ENOSYS;
   1626 
   1627 	(void)memset(t, '\0', sizeof(t));
   1628 
   1629 	(void)snprintf(t, sizeof(t), "%s%s%s%s%s%s",
   1630 	    ((sc->sc_sleepstates & __BIT(0)) != 0) ? "S0 " : "",
   1631 	    ((sc->sc_sleepstates & __BIT(1)) != 0) ? "S1 " : "",
   1632 	    ((sc->sc_sleepstates & __BIT(2)) != 0) ? "S2 " : "",
   1633 	    ((sc->sc_sleepstates & __BIT(3)) != 0) ? "S3 " : "",
   1634 	    ((sc->sc_sleepstates & __BIT(4)) != 0) ? "S4 " : "",
   1635 	    ((sc->sc_sleepstates & __BIT(5)) != 0) ? "S5 " : "");
   1636 
   1637 	node = *rnode;
   1638 	node.sysctl_data = &t;
   1639 
   1640 	err = sysctl_lookup(SYSCTLFN_CALL(&node));
   1641 
   1642 	if (err || newp == NULL)
   1643 		return err;
   1644 
   1645 	return 0;
   1646 }
   1647 
   1648 /*
   1649  * Tables.
   1650  */
   1651 ACPI_PHYSICAL_ADDRESS
   1652 acpi_OsGetRootPointer(void)
   1653 {
   1654 	ACPI_PHYSICAL_ADDRESS PhysicalAddress;
   1655 
   1656 	/*
   1657 	 * We let MD code handle this since there are multiple ways to do it:
   1658 	 *
   1659 	 *	IA-32: Use AcpiFindRootPointer() to locate the RSDP.
   1660 	 *
   1661 	 *	IA-64: Use the EFI.
   1662 	 */
   1663 	PhysicalAddress = acpi_md_OsGetRootPointer();
   1664 
   1665 	if (acpi_root_pointer == 0)
   1666 		acpi_root_pointer = PhysicalAddress;
   1667 
   1668 	return PhysicalAddress;
   1669 }
   1670 
   1671 static ACPI_TABLE_HEADER *
   1672 acpi_map_rsdt(void)
   1673 {
   1674 	ACPI_PHYSICAL_ADDRESS paddr;
   1675 	ACPI_TABLE_RSDP *rsdp;
   1676 
   1677 	paddr = AcpiOsGetRootPointer();
   1678 
   1679 	if (paddr == 0)
   1680 		return NULL;
   1681 
   1682 	rsdp = AcpiOsMapMemory(paddr, sizeof(ACPI_TABLE_RSDP));
   1683 
   1684 	if (rsdp == NULL)
   1685 		return NULL;
   1686 
   1687 	if (rsdp->Revision > 1 && rsdp->XsdtPhysicalAddress)
   1688 		paddr = rsdp->XsdtPhysicalAddress;
   1689 	else
   1690 		paddr = rsdp->RsdtPhysicalAddress;
   1691 
   1692 	AcpiOsUnmapMemory(rsdp, sizeof(ACPI_TABLE_RSDP));
   1693 
   1694 	return AcpiOsMapMemory(paddr, sizeof(ACPI_TABLE_HEADER));
   1695 }
   1696 
   1697 static void
   1698 acpi_unmap_rsdt(ACPI_TABLE_HEADER *rsdt)
   1699 {
   1700 
   1701 	if (rsdt == NULL)
   1702 		return;
   1703 
   1704 	AcpiOsUnmapMemory(rsdt, sizeof(ACPI_TABLE_HEADER));
   1705 }
   1706 
   1707 /*
   1708  * XXX: Refactor to be a generic function that maps tables.
   1709  */
   1710 ACPI_STATUS
   1711 acpi_madt_map(void)
   1712 {
   1713 	ACPI_STATUS  rv;
   1714 
   1715 	if (madt_header != NULL)
   1716 		return AE_ALREADY_EXISTS;
   1717 
   1718 	rv = AcpiGetTable(ACPI_SIG_MADT, 1, &madt_header);
   1719 
   1720 	if (ACPI_FAILURE(rv))
   1721 		return rv;
   1722 
   1723 	return AE_OK;
   1724 }
   1725 
   1726 void
   1727 acpi_madt_unmap(void)
   1728 {
   1729 	madt_header = NULL;
   1730 }
   1731 
   1732 /*
   1733  * XXX: Refactor to be a generic function that walks tables.
   1734  */
   1735 void
   1736 acpi_madt_walk(ACPI_STATUS (*func)(ACPI_SUBTABLE_HEADER *, void *), void *aux)
   1737 {
   1738 	ACPI_SUBTABLE_HEADER *hdrp;
   1739 	char *madtend, *where;
   1740 
   1741 	madtend = (char *)madt_header + madt_header->Length;
   1742 	where = (char *)madt_header + sizeof (ACPI_TABLE_MADT);
   1743 
   1744 	while (where < madtend) {
   1745 
   1746 		hdrp = (ACPI_SUBTABLE_HEADER *)where;
   1747 
   1748 		if (ACPI_FAILURE(func(hdrp, aux)))
   1749 			break;
   1750 
   1751 		where += hdrp->Length;
   1752 	}
   1753 }
   1754 
   1755 /*
   1756  * Miscellaneous.
   1757  */
   1758 static bool
   1759 acpi_is_scope(struct acpi_devnode *ad)
   1760 {
   1761 	int i;
   1762 
   1763 	/*
   1764 	 * Return true if the node is a root scope.
   1765 	 */
   1766 	if (ad->ad_parent == NULL)
   1767 		return false;
   1768 
   1769 	if (ad->ad_parent->ad_handle != ACPI_ROOT_OBJECT)
   1770 		return false;
   1771 
   1772 	for (i = 0; i < __arraycount(acpi_scopes); i++) {
   1773 
   1774 		if (acpi_scopes[i] == NULL)
   1775 			continue;
   1776 
   1777 		if (ad->ad_handle == acpi_scopes[i])
   1778 			return true;
   1779 	}
   1780 
   1781 	return false;
   1782 }
   1783 
   1784 /*
   1785  * ACPIVERBOSE.
   1786  */
   1787 void
   1788 acpi_load_verbose(void)
   1789 {
   1790 
   1791 	if (acpi_verbose_loaded == 0)
   1792 		module_autoload("acpiverbose", MODULE_CLASS_MISC);
   1793 }
   1794 
   1795 void
   1796 acpi_print_verbose_stub(struct acpi_softc *sc)
   1797 {
   1798 
   1799 	acpi_load_verbose();
   1800 
   1801 	if (acpi_verbose_loaded != 0)
   1802 		acpi_print_verbose(sc);
   1803 }
   1804 
   1805 void
   1806 acpi_print_dev_stub(const char *pnpstr)
   1807 {
   1808 
   1809 	acpi_load_verbose();
   1810 
   1811 	if (acpi_verbose_loaded != 0)
   1812 		acpi_print_dev(pnpstr);
   1813 }
   1814