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