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