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