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