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