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