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