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