Home | History | Annotate | Line # | Download | only in acpi
acpi.c revision 1.101.16.20
      1 /*	$NetBSD: acpi.c,v 1.101.16.20 2007/10/04 15:05:45 joerg 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  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the NetBSD
     21  *	Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * Copyright 2001, 2003 Wasabi Systems, Inc.
     41  * All rights reserved.
     42  *
     43  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
     44  *
     45  * Redistribution and use in source and binary forms, with or without
     46  * modification, are permitted provided that the following conditions
     47  * are met:
     48  * 1. Redistributions of source code must retain the above copyright
     49  *    notice, this list of conditions and the following disclaimer.
     50  * 2. Redistributions in binary form must reproduce the above copyright
     51  *    notice, this list of conditions and the following disclaimer in the
     52  *    documentation and/or other materials provided with the distribution.
     53  * 3. All advertising materials mentioning features or use of this software
     54  *    must display the following acknowledgement:
     55  *	This product includes software developed for the NetBSD Project by
     56  *	Wasabi Systems, Inc.
     57  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     58  *    or promote products derived from this software without specific prior
     59  *    written permission.
     60  *
     61  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     62  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     63  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     64  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     65  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     66  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     67  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     68  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     69  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     70  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     71  * POSSIBILITY OF SUCH DAMAGE.
     72  */
     73 
     74 /*
     75  * Autoconfiguration support for the Intel ACPI Component Architecture
     76  * ACPI reference implementation.
     77  */
     78 
     79 #include <sys/cdefs.h>
     80 __KERNEL_RCSID(0, "$NetBSD: acpi.c,v 1.101.16.20 2007/10/04 15:05:45 joerg Exp $");
     81 
     82 #include "opt_acpi.h"
     83 #include "opt_pcifixup.h"
     84 
     85 #include <sys/param.h>
     86 #include <sys/systm.h>
     87 #include <sys/device.h>
     88 #include <sys/malloc.h>
     89 #include <sys/mutex.h>
     90 #include <sys/kernel.h>
     91 #include <sys/proc.h>
     92 #include <sys/sysctl.h>
     93 
     94 #include <dev/acpi/acpica.h>
     95 #include <dev/acpi/acpireg.h>
     96 #include <dev/acpi/acpivar.h>
     97 #include <dev/acpi/acpi_osd.h>
     98 #include <dev/acpi/acpi_timer.h>
     99 #ifdef ACPIVERBOSE
    100 #include <dev/acpi/acpidevs_data.h>
    101 #endif
    102 
    103 #if defined(ACPI_PCI_FIXUP)
    104 #error The option ACPI_PCI_FIXUP has been obsoleted by PCI_INTR_FIXUP_DISABLED.  Please adjust your kernel configuration file.
    105 #endif
    106 
    107 #ifdef PCI_INTR_FIXUP_DISABLED
    108 #include <dev/pci/pcidevs.h>
    109 #endif
    110 
    111 MALLOC_DECLARE(M_ACPI);
    112 
    113 #include <machine/acpi_machdep.h>
    114 
    115 #ifdef ACPI_DEBUGGER
    116 #define	ACPI_DBGR_INIT		0x01
    117 #define	ACPI_DBGR_TABLES	0x02
    118 #define	ACPI_DBGR_ENABLE	0x04
    119 #define	ACPI_DBGR_PROBE		0x08
    120 #define	ACPI_DBGR_RUNNING	0x10
    121 
    122 static int acpi_dbgr = 0x00;
    123 #endif
    124 
    125 static ACPI_TABLE_DESC	acpi_initial_tables[128];
    126 
    127 static int	acpi_match(struct device *, struct cfdata *, void *);
    128 static void	acpi_attach(struct device *, struct device *, void *);
    129 
    130 static int	acpi_print(void *aux, const char *);
    131 
    132 static int	sysctl_hw_acpi_sleepstate(SYSCTLFN_ARGS);
    133 
    134 extern struct cfdriver acpi_cd;
    135 
    136 CFATTACH_DECL(acpi, sizeof(struct acpi_softc),
    137     acpi_match, acpi_attach, NULL, NULL);
    138 
    139 /*
    140  * This is a flag we set when the ACPI subsystem is active.  Machine
    141  * dependent code may wish to skip other steps (such as attaching
    142  * subsystems that ACPI supercedes) when ACPI is active.
    143  */
    144 int	acpi_active;
    145 int	acpi_force_load;
    146 
    147 /*
    148  * Pointer to the ACPI subsystem's state.  There can be only
    149  * one ACPI instance.
    150  */
    151 struct acpi_softc *acpi_softc;
    152 
    153 /*
    154  * Locking stuff.
    155  */
    156 static kmutex_t acpi_slock;
    157 static int acpi_locked;
    158 extern kmutex_t acpi_interrupt_list_mtx;
    159 
    160 /*
    161  * sysctl-related information
    162  */
    163 
    164 static int acpi_node = CTL_EOL;
    165 static uint64_t acpi_root_pointer;	/* found as hw.acpi.root */
    166 static int acpi_sleepstate = ACPI_STATE_S0;
    167 
    168 /*
    169  * Prototypes.
    170  */
    171 static void		acpi_shutdown(void *);
    172 static void		acpi_build_tree(struct acpi_softc *);
    173 static ACPI_STATUS	acpi_make_devnode(ACPI_HANDLE, UINT32, void *, void **);
    174 
    175 static void		acpi_enable_fixed_events(struct acpi_softc *);
    176 
    177 /*
    178  * acpi_probe:
    179  *
    180  *	Probe for ACPI support.  This is called by the
    181  *	machine-dependent ACPI front-end.  All of the
    182  *	actual work is done by ACPICA.
    183  *
    184  *	NOTE: This is not an autoconfiguration interface function.
    185  */
    186 int
    187 acpi_probe(void)
    188 {
    189 	static int beenhere;
    190 	ACPI_STATUS rv;
    191 
    192 	if (beenhere != 0)
    193 		panic("acpi_probe: ACPI has already been probed");
    194 	beenhere = 1;
    195 
    196 	mutex_init(&acpi_slock, MUTEX_DRIVER, IPL_NONE);
    197 	mutex_init(&acpi_interrupt_list_mtx, MUTEX_DRIVER, IPL_NONE);
    198 	acpi_locked = 0;
    199 
    200 	/*
    201 	 * Start up ACPICA.
    202 	 */
    203 #ifdef ACPI_DEBUGGER
    204 	if (acpi_dbgr & ACPI_DBGR_INIT)
    205 		acpi_osd_debugger();
    206 #endif
    207 
    208 	AcpiGbl_AllMethodsSerialized = FALSE;
    209 	AcpiGbl_EnableInterpreterSlack = TRUE;
    210 
    211 	rv = AcpiInitializeSubsystem();
    212 	if (ACPI_FAILURE(rv)) {
    213 		printf("ACPI: unable to initialize ACPICA: %s\n",
    214 		    AcpiFormatException(rv));
    215 		return 0;
    216 	}
    217 
    218 	rv = AcpiInitializeTables(acpi_initial_tables, 128, 0);
    219 	if (ACPI_FAILURE(rv)) {
    220 		printf("ACPI: unable to initialize ACPI tables: %s\n",
    221 		    AcpiFormatException(rv));
    222 		return 0;
    223 	}
    224 
    225 	rv = AcpiReallocateRootTable();
    226 	if (ACPI_FAILURE(rv)) {
    227 		printf("ACPI: unable to reallocate root table: %s\n",
    228 		    AcpiFormatException(rv));
    229 		return 0;
    230 	}
    231 
    232 #ifdef ACPI_DEBUGGER
    233 	if (acpi_dbgr & ACPI_DBGR_TABLES)
    234 		acpi_osd_debugger();
    235 #endif
    236 
    237 	rv = AcpiLoadTables();
    238 	if (ACPI_FAILURE(rv)) {
    239 		printf("ACPI: unable to load tables: %s\n",
    240 		    AcpiFormatException(rv));
    241 		return 0;
    242 	}
    243 
    244 #if notyet
    245 	if (!acpi_force_load && (acpi_find_quirks() & ACPI_QUIRK_BROKEN)) {
    246 		printf("ACPI: BIOS implementation in listed as broken:\n");
    247 		printf("ACPI: X/RSDT: OemId <%6.6s,%8.8s,%08x>, "
    248 		       "AslId <%4.4s,%08x>\n",
    249 			AcpiGbl_XSDT->OemId, AcpiGbl_XSDT->OemTableId,
    250 		        AcpiGbl_XSDT->OemRevision,
    251 			AcpiGbl_XSDT->AslCompilerId,
    252 		        AcpiGbl_XSDT->AslCompilerRevision);
    253 		printf("ACPI: not used. set acpi_force_load to use anyway.\n");
    254 		return 0;
    255 	}
    256 
    257 	/* Install the default address space handlers. */
    258 	rv = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
    259 	    ACPI_ADR_SPACE_SYSTEM_MEMORY, ACPI_DEFAULT_HANDLER, NULL, NULL);
    260 	if (ACPI_FAILURE(rv)) {
    261 		printf("ACPI: unable to initialise SystemMemory handler: %s\n",
    262 		    AcpiFormatException(rv));
    263 		return 0;
    264 	}
    265 	rv = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
    266 	    ACPI_ADR_SPACE_SYSTEM_IO, ACPI_DEFAULT_HANDLER, NULL, NULL);
    267 	if (ACPI_FAILURE(rv)) {
    268 		printf("ACPI: unable to initialise SystemIO handler: %s\n",
    269 		     AcpiFormatException(rv));
    270 		return 0;
    271 	}
    272 	rv = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
    273 	    ACPI_ADR_SPACE_PCI_CONFIG, ACPI_DEFAULT_HANDLER, NULL, NULL);
    274 	if (ACPI_FAILURE(rv)) {
    275 		printf("ACPI: unabled to initialise PciConfig handler: %s\n",
    276 		    AcpiFormatException(rv));
    277 		return 0;
    278 	}
    279 #endif
    280 
    281 	rv = AcpiEnableSubsystem(~(ACPI_NO_HARDWARE_INIT|ACPI_NO_ACPI_ENABLE));
    282 	if (ACPI_FAILURE(rv)) {
    283 		printf("ACPI: unable to enable: %s\n", AcpiFormatException(rv));
    284 		return 0;
    285 	}
    286 
    287 	/*
    288 	 * Looks like we have ACPI!
    289 	 */
    290 
    291 	return 1;
    292 }
    293 
    294 static int
    295 acpi_submatch(device_t parent, cfdata_t cf, const int *locs, void *aux)
    296 {
    297 	struct cfattach *ca;
    298 
    299 	ca = config_cfattach_lookup(cf->cf_name, cf->cf_atname);
    300 	return (ca == &acpi_ca);
    301 }
    302 
    303 int
    304 acpi_check(device_t parent, const char *ifattr)
    305 {
    306 	return (config_search_ia(acpi_submatch, parent, ifattr, NULL) != NULL);
    307 }
    308 
    309 ACPI_PHYSICAL_ADDRESS
    310 acpi_OsGetRootPointer(void)
    311 {
    312 	ACPI_PHYSICAL_ADDRESS PhysicalAddress;
    313 
    314 	/*
    315 	 * IA-32: Use AcpiFindRootPointer() to locate the RSDP.
    316 	 *
    317 	 * IA-64: Use the EFI.
    318 	 *
    319 	 * We let MD code handle this since there are multiple
    320 	 * ways to do it.
    321 	 */
    322 
    323 	PhysicalAddress = acpi_md_OsGetRootPointer();
    324 
    325 	if (acpi_root_pointer == 0)
    326 		acpi_root_pointer = PhysicalAddress;
    327 
    328 	return PhysicalAddress;
    329 }
    330 
    331 /*
    332  * acpi_match:
    333  *
    334  *	Autoconfiguration `match' routine.
    335  */
    336 static int
    337 acpi_match(struct device *parent, struct cfdata *match,
    338     void *aux)
    339 {
    340 	/*
    341 	 * XXX Check other locators?  Hard to know -- machine
    342 	 * dependent code has already checked for the presence
    343 	 * of ACPI by calling acpi_probe(), so I suppose we
    344 	 * don't really have to do anything else.
    345 	 */
    346 	return 1;
    347 }
    348 
    349 /*
    350  * acpi_attach:
    351  *
    352  *	Autoconfiguration `attach' routine.  Finish initializing
    353  *	ACPICA (some initialization was done in acpi_probe(),
    354  *	which was required to check for the presence of ACPI),
    355  *	and enable the ACPI subsystem.
    356  */
    357 static void
    358 acpi_attach(struct device *parent, struct device *self, void *aux)
    359 {
    360 	struct acpi_softc *sc = (void *) self;
    361 	struct acpibus_attach_args *aa = aux;
    362 	ACPI_STATUS rv;
    363 
    364 	aprint_naive(": Advanced Configuration and Power Interface\n");
    365 	aprint_normal(": Advanced Configuration and Power Interface\n");
    366 
    367 	if (acpi_softc != NULL)
    368 		panic("acpi_attach: ACPI has already been attached");
    369 
    370 	sysmon_power_settype("acpi");
    371 
    372 	aprint_verbose("%s: using Intel ACPI CA subsystem version %08x\n",
    373 	    sc->sc_dev.dv_xname, ACPI_CA_VERSION);
    374 
    375 #if 0
    376 	aprint_verbose("%s: X/RSDT: OemId <%6.6s,%8.8s,%08x>, AslId <%4.4s,%08x>\n",
    377 	    sc->sc_dev.dv_xname,
    378 	    AcpiGbl_XSDT->OemId, AcpiGbl_XSDT->OemTableId,
    379 	    AcpiGbl_XSDT->OemRevision,
    380 	    AcpiGbl_XSDT->AslCompilerId, AcpiGbl_XSDT->AslCompilerRevision);
    381 #endif
    382 
    383 	sc->sc_quirks = acpi_find_quirks();
    384 
    385 	sc->sc_iot = aa->aa_iot;
    386 	sc->sc_memt = aa->aa_memt;
    387 	sc->sc_pc = aa->aa_pc;
    388 	sc->sc_pciflags = aa->aa_pciflags;
    389 	sc->sc_ic = aa->aa_ic;
    390 
    391 	acpi_softc = sc;
    392 
    393 	/*
    394 	 * Register null power management handler
    395 	 */
    396 	(void)pnp_register(self, pnp_generic_power);
    397 
    398 	/*
    399 	 * Bring ACPI on-line.
    400 	 */
    401 #ifdef ACPI_DEBUGGER
    402 	if (acpi_dbgr & ACPI_DBGR_ENABLE)
    403 		acpi_osd_debugger();
    404 #endif
    405 
    406 #define ACPI_ENABLE_PHASE1 \
    407     (ACPI_NO_HANDLER_INIT | ACPI_NO_EVENT_INIT)
    408 #define ACPI_ENABLE_PHASE2 \
    409     (ACPI_NO_HARDWARE_INIT | ACPI_NO_ACPI_ENABLE | \
    410      ACPI_NO_ADDRESS_SPACE_INIT)
    411 
    412 	rv = AcpiEnableSubsystem(ACPI_ENABLE_PHASE1);
    413 	if (ACPI_FAILURE(rv)) {
    414 		aprint_error("%s: unable to enable ACPI: %s\n",
    415 		    sc->sc_dev.dv_xname, AcpiFormatException(rv));
    416 		return;
    417 	}
    418 
    419 	acpi_md_callback();
    420 
    421 	rv = AcpiEnableSubsystem(ACPI_ENABLE_PHASE2);
    422 	if (ACPI_FAILURE(rv)) {
    423 		aprint_error("%s: unable to enable ACPI: %s\n",
    424 		    sc->sc_dev.dv_xname, AcpiFormatException(rv));
    425 		return;
    426 	}
    427 
    428 	/* early EC handler initialization if ECDT table is available */
    429 	config_found_ia(&sc->sc_dev, "acpiecdtbus", NULL, NULL);
    430 
    431 	rv = AcpiInitializeObjects(ACPI_FULL_INITIALIZATION);
    432 	if (ACPI_FAILURE(rv)) {
    433 		aprint_error("%s: unable to initialize ACPI objects: %s\n",
    434 		    sc->sc_dev.dv_xname, AcpiFormatException(rv));
    435 		return;
    436 	}
    437 	acpi_active = 1;
    438 
    439 	/* Our current state is "awake". */
    440 	sc->sc_sleepstate = ACPI_STATE_S0;
    441 
    442 	/* Show SCI interrupt. */
    443 	aprint_verbose("%s: SCI interrupting at int %d\n",
    444 	    sc->sc_dev.dv_xname, AcpiGbl_FADT.SciInterrupt);
    445 
    446 	/*
    447 	 * Check for fixed-hardware features.
    448 	 */
    449 	acpi_enable_fixed_events(sc);
    450 	acpitimer_init();
    451 
    452 	/*
    453 	 * Scan the namespace and build our device tree.
    454 	 */
    455 #ifdef ACPI_DEBUGGER
    456 	if (acpi_dbgr & ACPI_DBGR_PROBE)
    457 		acpi_osd_debugger();
    458 #endif
    459 	acpi_build_tree(sc);
    460 
    461 	if (acpi_root_pointer != 0 && acpi_node != CTL_EOL) {
    462 		(void)sysctl_createv(NULL, 0, NULL, NULL,
    463 		    CTLFLAG_IMMEDIATE,
    464 		    CTLTYPE_QUAD, "root", NULL, NULL,
    465 		    acpi_root_pointer, NULL, 0,
    466 		    CTL_HW, acpi_node, CTL_CREATE, CTL_EOL);
    467 	}
    468 
    469 
    470 	/*
    471 	 * Register a shutdown hook that disables certain ACPI
    472 	 * events that might happen and confuse us while we're
    473 	 * trying to shut down.
    474 	 */
    475 	sc->sc_sdhook = shutdownhook_establish(acpi_shutdown, sc);
    476 	if (sc->sc_sdhook == NULL)
    477 		aprint_error("%s: WARNING: unable to register shutdown hook\n",
    478 		    sc->sc_dev.dv_xname);
    479 
    480 #ifdef ACPI_DEBUGGER
    481 	if (acpi_dbgr & ACPI_DBGR_RUNNING)
    482 		acpi_osd_debugger();
    483 #endif
    484 }
    485 
    486 /*
    487  * acpi_shutdown:
    488  *
    489  *	Shutdown hook for ACPI -- disable some events that
    490  *	might confuse us.
    491  */
    492 static void
    493 acpi_shutdown(void *arg)
    494 {
    495 	/* nothing */
    496 }
    497 
    498 #if 0
    499 /*
    500  * acpi_disable:
    501  *
    502  *	Disable ACPI.
    503  */
    504 static ACPI_STATUS
    505 acpi_disable(struct acpi_softc *sc)
    506 {
    507 	ACPI_STATUS rv = AE_OK;
    508 
    509 	if (acpi_active) {
    510 		rv = AcpiDisable();
    511 		if (ACPI_SUCCESS(rv))
    512 			acpi_active = 0;
    513 	}
    514 	return rv;
    515 }
    516 #endif
    517 
    518 struct acpi_make_devnode_state {
    519 	struct acpi_softc *softc;
    520 	struct acpi_scope *scope;
    521 };
    522 
    523 /*
    524  * acpi_build_tree:
    525  *
    526  *	Scan relevant portions of the ACPI namespace and attach
    527  *	child devices.
    528  */
    529 static void
    530 acpi_build_tree(struct acpi_softc *sc)
    531 {
    532 	static const char *scopes[] = {
    533 		"\\_PR_",	/* ACPI 1.0 processor namespace */
    534 		"\\_SB_",	/* system bus namespace */
    535 		"\\_SI_",	/* system indicator namespace */
    536 		"\\_TZ_",	/* ACPI 1.0 thermal zone namespace */
    537 		NULL,
    538 	};
    539 	struct acpi_attach_args aa;
    540 	struct acpi_make_devnode_state state;
    541 	struct acpi_scope *as;
    542 	struct acpi_devnode *ad;
    543 	ACPI_HANDLE parent;
    544 	ACPI_STATUS rv;
    545 	int i;
    546 
    547 	TAILQ_INIT(&sc->sc_scopes);
    548 
    549 	state.softc = sc;
    550 
    551 	/*
    552 	 * Scan the namespace and build our tree.
    553 	 */
    554 	for (i = 0; scopes[i] != NULL; i++) {
    555 		as = malloc(sizeof(*as), M_ACPI, M_WAITOK);
    556 		as->as_name = scopes[i];
    557 		TAILQ_INIT(&as->as_devnodes);
    558 
    559 		TAILQ_INSERT_TAIL(&sc->sc_scopes, as, as_list);
    560 
    561 		state.scope = as;
    562 
    563 		rv = AcpiGetHandle(ACPI_ROOT_OBJECT, scopes[i],
    564 		    &parent);
    565 		if (ACPI_SUCCESS(rv)) {
    566 			AcpiWalkNamespace(ACPI_TYPE_ANY, parent, 100,
    567 			    acpi_make_devnode, &state, NULL);
    568 		}
    569 
    570 		/* Now, for this namespace, try and attach the devices. */
    571 		TAILQ_FOREACH(ad, &as->as_devnodes, ad_list) {
    572 			aa.aa_node = ad;
    573 			aa.aa_iot = sc->sc_iot;
    574 			aa.aa_memt = sc->sc_memt;
    575 			aa.aa_pc = sc->sc_pc;
    576 			aa.aa_pciflags = sc->sc_pciflags;
    577 			aa.aa_ic = sc->sc_ic;
    578 
    579 			if (ad->ad_devinfo->Type == ACPI_TYPE_DEVICE) {
    580 				/*
    581 				 * XXX We only attach devices which are:
    582 				 *
    583 				 *	- present
    584 				 *	- enabled
    585 				 *	- functioning properly
    586 				 *
    587 				 * However, if enabled, it's decoding resources,
    588 				 * so we should claim them, if possible.
    589 				 * Requires changes to bus_space(9).
    590 				 */
    591 				if ((ad->ad_devinfo->Valid & ACPI_VALID_STA) ==
    592 				    ACPI_VALID_STA &&
    593 				    (ad->ad_devinfo->CurrentStatus &
    594 				     (ACPI_STA_DEV_PRESENT|ACPI_STA_DEV_ENABLED|
    595 				      ACPI_STA_DEV_OK)) !=
    596 				    (ACPI_STA_DEV_PRESENT|ACPI_STA_DEV_ENABLED|
    597 				     ACPI_STA_DEV_OK))
    598 					continue;
    599 			}
    600 
    601 			/*
    602 			 * XXX Same problem as above...
    603 			 */
    604 			if ((ad->ad_devinfo->Valid & ACPI_VALID_HID) == 0)
    605 				continue;
    606 
    607 			ad->ad_device = config_found_ia(&sc->sc_dev,
    608 			    "acpinodebus", &aa, acpi_print);
    609 		}
    610 	}
    611 	config_found_ia(&sc->sc_dev, "acpiapmbus", NULL, NULL);
    612 }
    613 
    614 #ifdef ACPI_ACTIVATE_DEV
    615 static void
    616 acpi_activate_device(ACPI_HANDLE handle, ACPI_DEVICE_INFO **di)
    617 {
    618 	ACPI_STATUS rv;
    619 	ACPI_BUFFER buf;
    620 
    621 	buf.Pointer = NULL;
    622 	buf.Length = ACPI_ALLOCATE_BUFFER;
    623 
    624 #ifdef ACPI_DEBUG
    625 	aprint_normal("acpi_activate_device: %s, old status=%x\n",
    626 	       (*di)->HardwareId.Value, (*di)->CurrentStatus);
    627 #endif
    628 
    629 	rv = acpi_allocate_resources(handle);
    630 	if (ACPI_FAILURE(rv)) {
    631 		aprint_error("acpi: activate failed for %s\n",
    632 		       (*di)->HardwareId.Value);
    633 	} else {
    634 		aprint_verbose("acpi: activated %s\n",
    635 		    (*di)->HardwareId.Value);
    636 	}
    637 
    638 	(void)AcpiGetObjectInfo(handle, &buf);
    639 	AcpiOsFree(*di);
    640 	*di = buf.Pointer;
    641 
    642 #ifdef ACPI_DEBUG
    643 	aprint_normal("acpi_activate_device: %s, new status=%x\n",
    644 	       (*di)->HardwareId.Value, (*di)->CurrentStatus);
    645 #endif
    646 }
    647 #endif /* ACPI_ACTIVATE_DEV */
    648 
    649 /*
    650  * acpi_make_devnode:
    651  *
    652  *	Make an ACPI devnode.
    653  */
    654 static ACPI_STATUS
    655 acpi_make_devnode(ACPI_HANDLE handle, UINT32 level, void *context,
    656     void **status)
    657 {
    658 	struct acpi_make_devnode_state *state = context;
    659 #if defined(ACPI_DEBUG) || defined(ACPI_EXTRA_DEBUG)
    660 	struct acpi_softc *sc = state->softc;
    661 #endif
    662 	struct acpi_scope *as = state->scope;
    663 	struct acpi_devnode *ad;
    664 	ACPI_OBJECT_TYPE type;
    665 	ACPI_BUFFER buf;
    666 	ACPI_DEVICE_INFO *devinfo;
    667 	ACPI_STATUS rv;
    668 	ACPI_NAME_UNION *anu;
    669 	int i, clear = 0;
    670 
    671 	rv = AcpiGetType(handle, &type);
    672 	if (ACPI_SUCCESS(rv)) {
    673 		buf.Pointer = NULL;
    674 		buf.Length = ACPI_ALLOCATE_BUFFER;
    675 		rv = AcpiGetObjectInfo(handle, &buf);
    676 		if (ACPI_FAILURE(rv)) {
    677 #ifdef ACPI_DEBUG
    678 			aprint_normal("%s: AcpiGetObjectInfo failed: %s\n",
    679 			    sc->sc_dev.dv_xname, AcpiFormatException(rv));
    680 #endif
    681 			goto out; /* XXX why return OK */
    682 		}
    683 
    684 		devinfo = buf.Pointer;
    685 
    686 		switch (type) {
    687 		case ACPI_TYPE_DEVICE:
    688 #ifdef ACPI_ACTIVATE_DEV
    689 			if ((devinfo->Valid & (ACPI_VALID_STA|ACPI_VALID_HID)) ==
    690 			    (ACPI_VALID_STA|ACPI_VALID_HID) &&
    691 			    (devinfo->CurrentStatus &
    692 			     (ACPI_STA_DEV_PRESENT|ACPI_STA_DEV_ENABLED)) ==
    693 			    ACPI_STA_DEV_PRESENT)
    694 				acpi_activate_device(handle, &devinfo);
    695 
    696 			/* FALLTHROUGH */
    697 #endif
    698 
    699 		case ACPI_TYPE_PROCESSOR:
    700 		case ACPI_TYPE_THERMAL:
    701 		case ACPI_TYPE_POWER:
    702 			ad = malloc(sizeof(*ad), M_ACPI, M_NOWAIT|M_ZERO);
    703 			if (ad == NULL)
    704 				return AE_NO_MEMORY;
    705 
    706 			ad->ad_devinfo = devinfo;
    707 			ad->ad_handle = handle;
    708 			ad->ad_level = level;
    709 			ad->ad_scope = as;
    710 			ad->ad_type = type;
    711 
    712 			anu = (ACPI_NAME_UNION *)&devinfo->Name;
    713 			ad->ad_name[4] = '\0';
    714 			for (i = 3, clear = 0; i >= 0; i--) {
    715 				if (!clear && anu->Ascii[i] == '_')
    716 					ad->ad_name[i] = '\0';
    717 				else {
    718 					ad->ad_name[i] = anu->Ascii[i];
    719 					clear = 1;
    720 				}
    721 			}
    722 			if (ad->ad_name[0] == '\0')
    723 				ad->ad_name[0] = '_';
    724 
    725 			TAILQ_INSERT_TAIL(&as->as_devnodes, ad, ad_list);
    726 
    727 			if (type == ACPI_TYPE_DEVICE &&
    728 			    (ad->ad_devinfo->Valid & ACPI_VALID_HID) == 0)
    729 				goto out;
    730 
    731 #ifdef ACPI_EXTRA_DEBUG
    732 			aprint_normal("%s: HID %s found in scope %s level %d\n",
    733 			    sc->sc_dev.dv_xname,
    734 			    ad->ad_devinfo->HardwareId.Value,
    735 			    as->as_name, ad->ad_level);
    736 			if (ad->ad_devinfo->Valid & ACPI_VALID_UID)
    737 				aprint_normal("       UID %s\n",
    738 				    ad->ad_devinfo->UniqueId.Value);
    739 			if (ad->ad_devinfo->Valid & ACPI_VALID_ADR)
    740 				aprint_normal("       ADR 0x%016qx\n",
    741 				    ad->ad_devinfo->Address);
    742 			if (ad->ad_devinfo->Valid & ACPI_VALID_STA)
    743 				aprint_normal("       STA 0x%08x\n",
    744 				    ad->ad_devinfo->CurrentStatus);
    745 #endif
    746 		}
    747 	}
    748  out:
    749 	return AE_OK;
    750 }
    751 
    752 /*
    753  * acpi_print:
    754  *
    755  *	Autoconfiguration print routine for ACPI node bus.
    756  */
    757 static int
    758 acpi_print(void *aux, const char *pnp)
    759 {
    760 	struct acpi_attach_args *aa = aux;
    761 	ACPI_STATUS rv;
    762 
    763 	if (pnp) {
    764 		if (aa->aa_node->ad_devinfo->Valid & ACPI_VALID_HID) {
    765 			char *pnpstr =
    766 			    aa->aa_node->ad_devinfo->HardwareId.Value;
    767 			char *str;
    768 
    769 			aprint_normal("%s (%s) ", aa->aa_node->ad_name,
    770 			    pnpstr);
    771 			rv = acpi_eval_string(aa->aa_node->ad_handle,
    772 			    "_STR", &str);
    773 			if (ACPI_SUCCESS(rv)) {
    774 				aprint_normal("[%s] ", str);
    775 				AcpiOsFree(str);
    776 			}
    777 #ifdef ACPIVERBOSE
    778 			else {
    779 				int i;
    780 
    781 				for (i = 0; i < sizeof(acpi_knowndevs) /
    782 				    sizeof(acpi_knowndevs[0]); i++) {
    783 					if (strcmp(acpi_knowndevs[i].pnp,
    784 					    pnpstr) == 0) {
    785 						aprint_normal("[%s] ",
    786 						    acpi_knowndevs[i].str);
    787 					}
    788 				}
    789 			}
    790 
    791 #endif
    792 			aprint_normal("at %s", pnp);
    793 		} else if (aa->aa_node->ad_devinfo->Type != ACPI_TYPE_DEVICE) {
    794 			aprint_normal("%s (ACPI Object Type '%s' "
    795 			    "[0x%02x]) ", aa->aa_node->ad_name,
    796 			     AcpiUtGetTypeName(aa->aa_node->ad_devinfo->Type),
    797 			     aa->aa_node->ad_devinfo->Type);
    798 			aprint_normal("at %s", pnp);
    799 		} else
    800 			return 0;
    801 	} else {
    802 		aprint_normal(" (%s", aa->aa_node->ad_name);
    803 		if (aa->aa_node->ad_devinfo->Valid & ACPI_VALID_HID) {
    804 			aprint_normal(", %s", aa->aa_node->ad_devinfo->HardwareId.Value);
    805 			if (aa->aa_node->ad_devinfo->Valid & ACPI_VALID_UID) {
    806 				const char *uid;
    807 
    808 				uid = aa->aa_node->ad_devinfo->UniqueId.Value;
    809 				if (uid[0] == '\0')
    810 					uid = "<null>";
    811 				aprint_normal("-%s", uid);
    812 			}
    813 		}
    814 		aprint_normal(")");
    815 	}
    816 
    817 	return UNCONF;
    818 }
    819 
    820 /*****************************************************************************
    821  * ACPI fixed-hardware feature handlers
    822  *****************************************************************************/
    823 
    824 static UINT32	acpi_fixed_button_handler(void *);
    825 static void	acpi_fixed_button_pressed(void *);
    826 
    827 /*
    828  * acpi_enable_fixed_events:
    829  *
    830  *	Enable any fixed-hardware feature handlers.
    831  */
    832 static void
    833 acpi_enable_fixed_events(struct acpi_softc *sc)
    834 {
    835 	static int beenhere;
    836 	ACPI_STATUS rv;
    837 
    838 	KASSERT(beenhere == 0);
    839 	beenhere = 1;
    840 
    841 	/*
    842 	 * Check for fixed-hardware buttons.
    843 	 */
    844 
    845 	if ((AcpiGbl_FADT.Flags & ACPI_FADT_POWER_BUTTON) == 0) {
    846 		aprint_verbose("%s: fixed-feature power button present\n",
    847 		    sc->sc_dev.dv_xname);
    848 		sc->sc_smpsw_power.smpsw_name = sc->sc_dev.dv_xname;
    849 		sc->sc_smpsw_power.smpsw_type = PSWITCH_TYPE_POWER;
    850 		if (sysmon_pswitch_register(&sc->sc_smpsw_power) != 0) {
    851 			aprint_error("%s: unable to register fixed power "
    852 			    "button with sysmon\n", sc->sc_dev.dv_xname);
    853 		} else {
    854 			rv = AcpiInstallFixedEventHandler(
    855 			    ACPI_EVENT_POWER_BUTTON,
    856 			    acpi_fixed_button_handler, &sc->sc_smpsw_power);
    857 			if (ACPI_FAILURE(rv)) {
    858 				aprint_error("%s: unable to install handler "
    859 				    "for fixed power button: %s\n",
    860 				    sc->sc_dev.dv_xname,
    861 				    AcpiFormatException(rv));
    862 			}
    863 		}
    864 	}
    865 
    866 	if ((AcpiGbl_FADT.Flags & ACPI_FADT_SLEEP_BUTTON) == 0) {
    867 		aprint_verbose("%s: fixed-feature sleep button present\n",
    868 		    sc->sc_dev.dv_xname);
    869 		sc->sc_smpsw_sleep.smpsw_name = sc->sc_dev.dv_xname;
    870 		sc->sc_smpsw_sleep.smpsw_type = PSWITCH_TYPE_SLEEP;
    871 		if (sysmon_pswitch_register(&sc->sc_smpsw_power) != 0) {
    872 			aprint_error("%s: unable to register fixed sleep "
    873 			    "button with sysmon\n", sc->sc_dev.dv_xname);
    874 		} else {
    875 			rv = AcpiInstallFixedEventHandler(
    876 			    ACPI_EVENT_SLEEP_BUTTON,
    877 			    acpi_fixed_button_handler, &sc->sc_smpsw_sleep);
    878 			if (ACPI_FAILURE(rv)) {
    879 				aprint_error("%s: unable to install handler "
    880 				    "for fixed sleep button: %s\n",
    881 				    sc->sc_dev.dv_xname,
    882 				    AcpiFormatException(rv));
    883 			}
    884 		}
    885 	}
    886 }
    887 
    888 /*
    889  * acpi_fixed_button_handler:
    890  *
    891  *	Event handler for the fixed buttons.
    892  */
    893 static UINT32
    894 acpi_fixed_button_handler(void *context)
    895 {
    896 	struct sysmon_pswitch *smpsw = context;
    897 	int rv;
    898 
    899 #ifdef ACPI_BUT_DEBUG
    900 	printf("%s: fixed button handler\n", smpsw->smpsw_name);
    901 #endif
    902 
    903 	rv = AcpiOsExecute(OSL_NOTIFY_HANDLER,
    904 	    acpi_fixed_button_pressed, smpsw);
    905 	if (ACPI_FAILURE(rv))
    906 		printf("%s: WARNING: unable to queue fixed button pressed "
    907 		    "callback: %s\n", smpsw->smpsw_name,
    908 		    AcpiFormatException(rv));
    909 
    910 	return ACPI_INTERRUPT_HANDLED;
    911 }
    912 
    913 /*
    914  * acpi_fixed_button_pressed:
    915  *
    916  *	Deal with a fixed button being pressed.
    917  */
    918 static void
    919 acpi_fixed_button_pressed(void *context)
    920 {
    921 	struct sysmon_pswitch *smpsw = context;
    922 
    923 #ifdef ACPI_BUT_DEBUG
    924 	printf("%s: fixed button pressed, calling sysmon\n",
    925 	    smpsw->smpsw_name);
    926 #endif
    927 
    928 	sysmon_pswitch_event(smpsw, PSWITCH_EVENT_PRESSED);
    929 }
    930 
    931 /*****************************************************************************
    932  * ACPI utility routines.
    933  *****************************************************************************/
    934 
    935 /*
    936  * acpi_eval_integer:
    937  *
    938  *	Evaluate an integer object.
    939  */
    940 ACPI_STATUS
    941 acpi_eval_integer(ACPI_HANDLE handle, const char *path, ACPI_INTEGER *valp)
    942 {
    943 	ACPI_STATUS rv;
    944 	ACPI_BUFFER buf;
    945 	ACPI_OBJECT param;
    946 
    947 	if (handle == NULL)
    948 		handle = ACPI_ROOT_OBJECT;
    949 
    950 	buf.Pointer = &param;
    951 	buf.Length = sizeof(param);
    952 
    953 	rv = AcpiEvaluateObjectTyped(handle, path, NULL, &buf, ACPI_TYPE_INTEGER);
    954 	if (ACPI_SUCCESS(rv))
    955 		*valp = param.Integer.Value;
    956 
    957 	return rv;
    958 }
    959 
    960 /*
    961  * acpi_eval_string:
    962  *
    963  *	Evaluate a (Unicode) string object.
    964  */
    965 ACPI_STATUS
    966 acpi_eval_string(ACPI_HANDLE handle, const char *path, char **stringp)
    967 {
    968 	ACPI_STATUS rv;
    969 	ACPI_BUFFER buf;
    970 
    971 	if (handle == NULL)
    972 		handle = ACPI_ROOT_OBJECT;
    973 
    974 	buf.Pointer = NULL;
    975 	buf.Length = ACPI_ALLOCATE_BUFFER;
    976 
    977 	rv = AcpiEvaluateObjectTyped(handle, path, NULL, &buf, ACPI_TYPE_STRING);
    978 	if (ACPI_SUCCESS(rv)) {
    979 		ACPI_OBJECT *param = buf.Pointer;
    980 		const char *ptr = param->String.Pointer;
    981 		size_t len = param->String.Length;
    982 		if ((*stringp = AcpiOsAllocate(len)) == NULL)
    983 			rv = AE_NO_MEMORY;
    984 		else
    985 			(void)memcpy(*stringp, ptr, len);
    986 		AcpiOsFree(param);
    987 	}
    988 
    989 	return rv;
    990 }
    991 
    992 
    993 /*
    994  * acpi_eval_struct:
    995  *
    996  *	Evaluate a more complex structure.
    997  *	Caller must free buf.Pointer by AcpiOsFree().
    998  */
    999 ACPI_STATUS
   1000 acpi_eval_struct(ACPI_HANDLE handle, const char *path, ACPI_BUFFER *bufp)
   1001 {
   1002 	ACPI_STATUS rv;
   1003 
   1004 	if (handle == NULL)
   1005 		handle = ACPI_ROOT_OBJECT;
   1006 
   1007 	bufp->Pointer = NULL;
   1008 	bufp->Length = ACPI_ALLOCATE_BUFFER;
   1009 
   1010 	rv = AcpiEvaluateObject(handle, path, NULL, bufp);
   1011 
   1012 	return rv;
   1013 }
   1014 
   1015 /*
   1016  * acpi_foreach_package_object:
   1017  *
   1018  *	Iterate over all objects in a in a packages and pass then all
   1019  *	to a function. If the called function returns non AE_OK, the
   1020  *	iteration is stopped and that value is returned.
   1021  */
   1022 
   1023 ACPI_STATUS
   1024 acpi_foreach_package_object(ACPI_OBJECT *pkg,
   1025     ACPI_STATUS (*func)(ACPI_OBJECT *, void *),
   1026     void *arg)
   1027 {
   1028 	ACPI_STATUS rv = AE_OK;
   1029 	int i;
   1030 
   1031 	if (pkg == NULL || pkg->Type != ACPI_TYPE_PACKAGE)
   1032 		return AE_BAD_PARAMETER;
   1033 
   1034 	for (i = 0; i < pkg->Package.Count; i++) {
   1035 		rv = (*func)(&pkg->Package.Elements[i], arg);
   1036 		if (ACPI_FAILURE(rv))
   1037 			break;
   1038 	}
   1039 
   1040 	return rv;
   1041 }
   1042 
   1043 const char *
   1044 acpi_name(ACPI_HANDLE handle)
   1045 {
   1046 	static char buffer[80];
   1047 	ACPI_BUFFER buf;
   1048 	ACPI_STATUS rv;
   1049 
   1050 	buf.Length = sizeof(buffer);
   1051 	buf.Pointer = buffer;
   1052 
   1053 	rv = AcpiGetName(handle, ACPI_FULL_PATHNAME, &buf);
   1054 	if (ACPI_FAILURE(rv))
   1055 		return "(unknown acpi path)";
   1056 	return buffer;
   1057 }
   1058 
   1059 /*
   1060  * acpi_get:
   1061  *
   1062  *	Fetch data info the specified (empty) ACPI buffer.
   1063  *	Caller must free buf.Pointer by AcpiOsFree().
   1064  */
   1065 ACPI_STATUS
   1066 acpi_get(ACPI_HANDLE handle, ACPI_BUFFER *buf,
   1067     ACPI_STATUS (*getit)(ACPI_HANDLE, ACPI_BUFFER *))
   1068 {
   1069 	buf->Pointer = NULL;
   1070 	buf->Length = ACPI_ALLOCATE_BUFFER;
   1071 
   1072 	return (*getit)(handle, buf);
   1073 }
   1074 
   1075 
   1076 /*
   1077  * acpi_match_hid
   1078  *
   1079  *	Match given ids against _HID and _CIDs
   1080  */
   1081 int
   1082 acpi_match_hid(ACPI_DEVICE_INFO *ad, const char * const *ids)
   1083 {
   1084 	int i;
   1085 
   1086 	while (*ids) {
   1087 		if (ad->Valid & ACPI_VALID_HID) {
   1088 			if (pmatch(ad->HardwareId.Value, *ids, NULL) == 2)
   1089 				return 1;
   1090 		}
   1091 
   1092 		if (ad->Valid & ACPI_VALID_CID) {
   1093 			for (i = 0; i < ad->CompatibilityId.Count; i++) {
   1094 				if (pmatch(ad->CompatibilityId.Id[i].Value, *ids, NULL) == 2)
   1095 					return 1;
   1096 			}
   1097 		}
   1098 		ids++;
   1099 	}
   1100 
   1101 	return 0;
   1102 }
   1103 
   1104 /*
   1105  * acpi_set_wake_gpe
   1106  *
   1107  *	Set GPE as both Runtime and Wake
   1108  */
   1109 void
   1110 acpi_set_wake_gpe(ACPI_HANDLE handle)
   1111 {
   1112 	ACPI_BUFFER buf;
   1113 	ACPI_STATUS rv;
   1114 	ACPI_OBJECT *p, *elt;
   1115 
   1116 	rv = acpi_eval_struct(handle, METHOD_NAME__PRW, &buf);
   1117 	if (ACPI_FAILURE(rv))
   1118 		return;			/* just ignore */
   1119 
   1120 	p = buf.Pointer;
   1121 	if (p->Type != ACPI_TYPE_PACKAGE || p->Package.Count < 2)
   1122 		goto out;		/* just ignore */
   1123 
   1124 	elt = p->Package.Elements;
   1125 
   1126 	/* TBD: package support */
   1127 	AcpiSetGpeType(NULL, elt[0].Integer.Value, ACPI_GPE_TYPE_WAKE_RUN);
   1128 	AcpiEnableGpe(NULL, elt[0].Integer.Value, ACPI_NOT_ISR);
   1129 
   1130  out:
   1131 	AcpiOsFree(buf.Pointer);
   1132 }
   1133 
   1134 
   1135 /*****************************************************************************
   1136  * ACPI sleep support.
   1137  *****************************************************************************/
   1138 
   1139 static int
   1140 is_available_state(struct acpi_softc *sc, int state)
   1141 {
   1142 	UINT8 type_a, type_b;
   1143 
   1144 	return ACPI_SUCCESS(AcpiGetSleepTypeData((UINT8)state,
   1145 				&type_a, &type_b));
   1146 }
   1147 
   1148 /*
   1149  * acpi_enter_sleep_state:
   1150  *
   1151  *	enter to the specified sleep state.
   1152  */
   1153 
   1154 ACPI_STATUS
   1155 acpi_enter_sleep_state(struct acpi_softc *sc, int state)
   1156 {
   1157 	int err, s;
   1158 	ACPI_STATUS ret = AE_OK;
   1159 
   1160 	if (state == acpi_sleepstate)
   1161 		return AE_OK;
   1162 
   1163 	aprint_normal("%s: entering state %d\n", sc->sc_dev.dv_xname, state);
   1164 
   1165 	switch (state) {
   1166 	case ACPI_STATE_S0:
   1167 		break;
   1168 	case ACPI_STATE_S1:
   1169 	case ACPI_STATE_S2:
   1170 	case ACPI_STATE_S3:
   1171 	case ACPI_STATE_S4:
   1172 		if (!is_available_state(sc, state)) {
   1173 			aprint_error("%s: cannot enter the sleep state (%d).\n",
   1174 			    sc->sc_dev.dv_xname, state);
   1175 			break;
   1176 		}
   1177 
   1178 		if (state != ACPI_STATE_S1)
   1179 			pnp_global_transition(PNP_STATE_D3);
   1180 
   1181 		ret = AcpiEnterSleepStatePrep(state);
   1182 		if (ACPI_FAILURE(ret)) {
   1183 			aprint_error("%s: failed preparing to sleep (%s)\n",
   1184 			    sc->sc_dev.dv_xname, AcpiFormatException(ret));
   1185 			break;
   1186 		}
   1187 
   1188 		DELAY(1000000);
   1189 		acpi_sleepstate = state;
   1190 		if (state == ACPI_STATE_S1) {
   1191 			/* just enter the state */
   1192 			acpi_md_OsDisableInterrupt();
   1193 			AcpiEnterSleepState((UINT8)state);
   1194 			AcpiLeaveSleepState((UINT8)state);
   1195 		} else {
   1196 			s = splhigh();
   1197 			err = acpi_md_sleep(state);
   1198 			if (err == 0 && state == ACPI_STATE_S3)
   1199 				AcpiClearEvent(ACPI_EVENT_POWER_BUTTON);
   1200 			splx(s);
   1201 			if (state == ACPI_STATE_S4)
   1202 				AcpiEnable();
   1203 			AcpiLeaveSleepState((UINT8)state);
   1204 			pnp_global_transition(PNP_STATE_D0);
   1205 		}
   1206 
   1207 		break;
   1208 	case ACPI_STATE_S5:
   1209 		ret = AcpiEnterSleepStatePrep(ACPI_STATE_S5);
   1210 		if (ACPI_FAILURE(ret)) {
   1211 			aprint_error("%s: failed preparing to sleep (%s)\n",
   1212 			       sc->sc_dev.dv_xname, AcpiFormatException(ret));
   1213 			break;
   1214 		}
   1215 		DELAY(1000000);
   1216 		acpi_sleepstate = state;
   1217 		acpi_md_OsDisableInterrupt();
   1218 		AcpiEnterSleepState(ACPI_STATE_S5);
   1219 		aprint_error("%s: WARNING powerdown failed!\n",
   1220 		    sc->sc_dev.dv_xname);
   1221 		break;
   1222 	}
   1223 
   1224 	acpi_sleepstate = ACPI_STATE_S0;
   1225 	return ret;
   1226 }
   1227 
   1228 #if defined(ACPI_ACTIVATE_DEV)
   1229 /* XXX This very incomplete */
   1230 ACPI_STATUS
   1231 acpi_allocate_resources(ACPI_HANDLE handle)
   1232 {
   1233 	ACPI_BUFFER bufp, bufc, bufn;
   1234 	ACPI_RESOURCE *resp, *resc, *resn;
   1235 	ACPI_RESOURCE_IRQ *irq;
   1236 	ACPI_RESOURCE_EXTENDED_IRQ *xirq;
   1237 	ACPI_STATUS rv;
   1238 	uint delta;
   1239 
   1240 	rv = acpi_get(handle, &bufp, AcpiGetPossibleResources);
   1241 	if (ACPI_FAILURE(rv))
   1242 		goto out;
   1243 	rv = acpi_get(handle, &bufc, AcpiGetCurrentResources);
   1244 	if (ACPI_FAILURE(rv)) {
   1245 		goto out1;
   1246 	}
   1247 
   1248 	bufn.Length = 1000;
   1249 	bufn.Pointer = resn = malloc(bufn.Length, M_ACPI, M_WAITOK);
   1250 	resp = bufp.Pointer;
   1251 	resc = bufc.Pointer;
   1252 	while (resc->Type != ACPI_RESOURCE_TYPE_END_TAG &&
   1253 	       resp->Type != ACPI_RESOURCE_TYPE_END_TAG) {
   1254 		while (resc->Type != resp->Type && resp->Type != ACPI_RESOURCE_TYPE_END_TAG)
   1255 			resp = ACPI_NEXT_RESOURCE(resp);
   1256 		if (resp->Type == ACPI_RESOURCE_TYPE_END_TAG)
   1257 			break;
   1258 		/* Found identical Id */
   1259 		resn->Type = resc->Type;
   1260 		switch (resc->Type) {
   1261 		case ACPI_RESOURCE_TYPE_IRQ:
   1262 			memcpy(&resn->Data, &resp->Data,
   1263 			       sizeof(ACPI_RESOURCE_IRQ));
   1264 			irq = (ACPI_RESOURCE_IRQ *)&resn->Data;
   1265 			irq->Interrupts[0] =
   1266 			    ((ACPI_RESOURCE_IRQ *)&resp->Data)->
   1267 			        Interrupts[irq->InterruptCount-1];
   1268 			irq->InterruptCount = 1;
   1269 			resn->Length = ACPI_RS_SIZE(ACPI_RESOURCE_IRQ);
   1270 			break;
   1271 		case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
   1272 			memcpy(&resn->Data, &resp->Data,
   1273 			       sizeof(ACPI_RESOURCE_EXTENDED_IRQ));
   1274 			xirq = (ACPI_RESOURCE_EXTENDED_IRQ *)&resn->Data;
   1275 #if 0
   1276 			/*
   1277 			 * XXX not duplicating the interrupt logic above
   1278 			 * because its not clear what it accomplishes.
   1279 			 */
   1280 			xirq->Interrupts[0] =
   1281 			    ((ACPI_RESOURCE_EXT_IRQ *)&resp->Data)->
   1282 			    Interrupts[irq->NumberOfInterrupts-1];
   1283 			xirq->NumberOfInterrupts = 1;
   1284 #endif
   1285 			resn->Length = ACPI_RS_SIZE(ACPI_RESOURCE_EXTENDED_IRQ);
   1286 			break;
   1287 		case ACPI_RESOURCE_TYPE_IO:
   1288 			memcpy(&resn->Data, &resp->Data,
   1289 			       sizeof(ACPI_RESOURCE_IO));
   1290 			resn->Length = resp->Length;
   1291 			break;
   1292 		default:
   1293 			printf("acpi_allocate_resources: res=%d\n", resc->Type);
   1294 			rv = AE_BAD_DATA;
   1295 			goto out2;
   1296 		}
   1297 		resc = ACPI_NEXT_RESOURCE(resc);
   1298 		resn = ACPI_NEXT_RESOURCE(resn);
   1299 		resp = ACPI_NEXT_RESOURCE(resp);
   1300 		delta = (UINT8 *)resn - (UINT8 *)bufn.Pointer;
   1301 		if (delta >=
   1302 		    bufn.Length-ACPI_RS_SIZE(ACPI_RESOURCE_DATA)) {
   1303 			bufn.Length *= 2;
   1304 			bufn.Pointer = realloc(bufn.Pointer, bufn.Length,
   1305 					       M_ACPI, M_WAITOK);
   1306 			resn = (ACPI_RESOURCE *)((UINT8 *)bufn.Pointer + delta);
   1307 		}
   1308 	}
   1309 	if (resc->Type != ACPI_RESOURCE_TYPE_END_TAG) {
   1310 		printf("acpi_allocate_resources: resc not exhausted\n");
   1311 		rv = AE_BAD_DATA;
   1312 		goto out3;
   1313 	}
   1314 
   1315 	resn->Type = ACPI_RESOURCE_TYPE_END_TAG;
   1316 	rv = AcpiSetCurrentResources(handle, &bufn);
   1317 	if (ACPI_FAILURE(rv)) {
   1318 		printf("acpi_allocate_resources: AcpiSetCurrentResources %s\n",
   1319 		       AcpiFormatException(rv));
   1320 	}
   1321 
   1322 out3:
   1323 	free(bufn.Pointer, M_ACPI);
   1324 out2:
   1325 	AcpiOsFree(bufc.Pointer);
   1326 out1:
   1327 	AcpiOsFree(bufp.Pointer);
   1328 out:
   1329 	return rv;
   1330 }
   1331 #endif /* ACPI_ACTIVATE_DEV */
   1332 
   1333 SYSCTL_SETUP(sysctl_acpi_setup, "sysctl hw.acpi subtree setup")
   1334 {
   1335 	const struct sysctlnode *node;
   1336 	const struct sysctlnode *ssnode;
   1337 
   1338 	if (sysctl_createv(clog, 0, NULL, NULL,
   1339 	    CTLFLAG_PERMANENT,
   1340 	    CTLTYPE_NODE, "hw", NULL,
   1341 	    NULL, 0, NULL, 0,
   1342 	    CTL_HW, CTL_EOL) != 0)
   1343 		return;
   1344 
   1345 	if (sysctl_createv(clog, 0, NULL, &node,
   1346 	    CTLFLAG_PERMANENT,
   1347 	    CTLTYPE_NODE, "acpi", NULL,
   1348 	    NULL, 0, NULL, 0,
   1349 	    CTL_HW, CTL_CREATE, CTL_EOL) != 0)
   1350 		return;
   1351 
   1352 	acpi_node = node->sysctl_num;
   1353 
   1354 	/* ACPI sleepstate sysctl */
   1355 	if (sysctl_createv(NULL, 0, NULL, &node,
   1356 	    CTLFLAG_PERMANENT,
   1357 	    CTLTYPE_NODE, "machdep", NULL,
   1358 	    NULL, 0, NULL, 0, CTL_MACHDEP, CTL_EOL) != 0)
   1359 		return;
   1360 	if (sysctl_createv(NULL, 0, &node, &ssnode,
   1361 	    CTLFLAG_READWRITE, CTLTYPE_INT, "sleep_state",
   1362 	    NULL, sysctl_hw_acpi_sleepstate, 0, NULL, 0, CTL_CREATE,
   1363 	    CTL_EOL) != 0)
   1364 		return;
   1365 }
   1366 
   1367 static int
   1368 sysctl_hw_acpi_sleepstate(SYSCTLFN_ARGS)
   1369 {
   1370 	int error, t;
   1371 	struct sysctlnode node;
   1372 
   1373 	node = *rnode;
   1374 	t = acpi_sleepstate;
   1375 	node.sysctl_data = &t;
   1376 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1377 	if (error || newp == NULL)
   1378 		return error;
   1379 
   1380 	if (acpi_softc == NULL)
   1381 		return ENOSYS;
   1382 
   1383 	acpi_enter_sleep_state(acpi_softc, t);
   1384 
   1385 	return 0;
   1386 }
   1387