Home | History | Annotate | Line # | Download | only in acpi
acpi.c revision 1.104.2.2
      1 /*	$NetBSD: acpi.c,v 1.104.2.2 2008/01/02 21:53:48 bouyer 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.104.2.2 2008/01/02 21:53:48 bouyer 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 static ACPI_TABLE_HEADER *acpi_map_rsdt(void);
    178 static void		acpi_unmap_rsdt(ACPI_TABLE_HEADER *);
    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 	if (acpi_root_pointer != 0 && acpi_node != CTL_EOL) {
    480 		(void)sysctl_createv(NULL, 0, NULL, NULL,
    481 		    CTLFLAG_IMMEDIATE,
    482 		    CTLTYPE_QUAD, "root", NULL, NULL,
    483 		    acpi_root_pointer, NULL, 0,
    484 		    CTL_HW, acpi_node, CTL_CREATE, CTL_EOL);
    485 	}
    486 
    487 
    488 	/*
    489 	 * Register a shutdown hook that disables certain ACPI
    490 	 * events that might happen and confuse us while we're
    491 	 * trying to shut down.
    492 	 */
    493 	sc->sc_sdhook = shutdownhook_establish(acpi_shutdown, sc);
    494 	if (sc->sc_sdhook == NULL)
    495 		aprint_error("%s: WARNING: unable to register shutdown hook\n",
    496 		    sc->sc_dev.dv_xname);
    497 
    498 #ifdef ACPI_DEBUGGER
    499 	if (acpi_dbgr & ACPI_DBGR_RUNNING)
    500 		acpi_osd_debugger();
    501 #endif
    502 }
    503 
    504 /*
    505  * acpi_shutdown:
    506  *
    507  *	Shutdown hook for ACPI -- disable some events that
    508  *	might confuse us.
    509  */
    510 static void
    511 acpi_shutdown(void *arg)
    512 {
    513 	/* nothing */
    514 }
    515 
    516 #if 0
    517 /*
    518  * acpi_disable:
    519  *
    520  *	Disable ACPI.
    521  */
    522 static ACPI_STATUS
    523 acpi_disable(struct acpi_softc *sc)
    524 {
    525 	ACPI_STATUS rv = AE_OK;
    526 
    527 	if (acpi_active) {
    528 		rv = AcpiDisable();
    529 		if (ACPI_SUCCESS(rv))
    530 			acpi_active = 0;
    531 	}
    532 	return rv;
    533 }
    534 #endif
    535 
    536 struct acpi_make_devnode_state {
    537 	struct acpi_softc *softc;
    538 	struct acpi_scope *scope;
    539 };
    540 
    541 /*
    542  * acpi_build_tree:
    543  *
    544  *	Scan relevant portions of the ACPI namespace and attach
    545  *	child devices.
    546  */
    547 static void
    548 acpi_build_tree(struct acpi_softc *sc)
    549 {
    550 	static const char *scopes[] = {
    551 		"\\_PR_",	/* ACPI 1.0 processor namespace */
    552 		"\\_SB_",	/* system bus namespace */
    553 		"\\_SI_",	/* system indicator namespace */
    554 		"\\_TZ_",	/* ACPI 1.0 thermal zone namespace */
    555 		NULL,
    556 	};
    557 	struct acpi_attach_args aa;
    558 	struct acpi_make_devnode_state state;
    559 	struct acpi_scope *as;
    560 	struct acpi_devnode *ad;
    561 	ACPI_HANDLE parent;
    562 	ACPI_STATUS rv;
    563 	int i;
    564 
    565 	TAILQ_INIT(&sc->sc_scopes);
    566 
    567 	state.softc = sc;
    568 
    569 	/*
    570 	 * Scan the namespace and build our tree.
    571 	 */
    572 	for (i = 0; scopes[i] != NULL; i++) {
    573 		as = malloc(sizeof(*as), M_ACPI, M_WAITOK);
    574 		as->as_name = scopes[i];
    575 		TAILQ_INIT(&as->as_devnodes);
    576 
    577 		TAILQ_INSERT_TAIL(&sc->sc_scopes, as, as_list);
    578 
    579 		state.scope = as;
    580 
    581 		rv = AcpiGetHandle(ACPI_ROOT_OBJECT, scopes[i],
    582 		    &parent);
    583 		if (ACPI_SUCCESS(rv)) {
    584 			AcpiWalkNamespace(ACPI_TYPE_ANY, parent, 100,
    585 			    acpi_make_devnode, &state, NULL);
    586 		}
    587 
    588 		/* Now, for this namespace, try and attach the devices. */
    589 		TAILQ_FOREACH(ad, &as->as_devnodes, ad_list) {
    590 			aa.aa_node = ad;
    591 			aa.aa_iot = sc->sc_iot;
    592 			aa.aa_memt = sc->sc_memt;
    593 			aa.aa_pc = sc->sc_pc;
    594 			aa.aa_pciflags = sc->sc_pciflags;
    595 			aa.aa_ic = sc->sc_ic;
    596 
    597 			if (ad->ad_devinfo->Type == ACPI_TYPE_DEVICE) {
    598 				/*
    599 				 * XXX We only attach devices which are:
    600 				 *
    601 				 *	- present
    602 				 *	- enabled
    603 				 *	- functioning properly
    604 				 *
    605 				 * However, if enabled, it's decoding resources,
    606 				 * so we should claim them, if possible.
    607 				 * Requires changes to bus_space(9).
    608 				 */
    609 				if ((ad->ad_devinfo->Valid & ACPI_VALID_STA) ==
    610 				    ACPI_VALID_STA &&
    611 				    (ad->ad_devinfo->CurrentStatus &
    612 				     (ACPI_STA_DEV_PRESENT|ACPI_STA_DEV_ENABLED|
    613 				      ACPI_STA_DEV_OK)) !=
    614 				    (ACPI_STA_DEV_PRESENT|ACPI_STA_DEV_ENABLED|
    615 				     ACPI_STA_DEV_OK))
    616 					continue;
    617 			}
    618 
    619 			/*
    620 			 * XXX Same problem as above...
    621 			 *
    622 			 * Do this check only for devices, as e.g.
    623 			 * a Thermal Zone doesn't have a HID.
    624 			 */
    625 			if (ad->ad_devinfo->Type == ACPI_TYPE_DEVICE &&
    626 			    (ad->ad_devinfo->Valid & ACPI_VALID_HID) == 0)
    627 				continue;
    628 
    629 			ad->ad_device = config_found_ia(&sc->sc_dev,
    630 			    "acpinodebus", &aa, acpi_print);
    631 		}
    632 	}
    633 	config_found_ia(&sc->sc_dev, "acpiapmbus", NULL, NULL);
    634 }
    635 
    636 #ifdef ACPI_ACTIVATE_DEV
    637 static void
    638 acpi_activate_device(ACPI_HANDLE handle, ACPI_DEVICE_INFO **di)
    639 {
    640 	ACPI_STATUS rv;
    641 	ACPI_BUFFER buf;
    642 
    643 	buf.Pointer = NULL;
    644 	buf.Length = ACPI_ALLOCATE_BUFFER;
    645 
    646 #ifdef ACPI_DEBUG
    647 	aprint_normal("acpi_activate_device: %s, old status=%x\n",
    648 	       (*di)->HardwareId.Value, (*di)->CurrentStatus);
    649 #endif
    650 
    651 	rv = acpi_allocate_resources(handle);
    652 	if (ACPI_FAILURE(rv)) {
    653 		aprint_error("acpi: activate failed for %s\n",
    654 		       (*di)->HardwareId.Value);
    655 	} else {
    656 		aprint_verbose("acpi: activated %s\n",
    657 		    (*di)->HardwareId.Value);
    658 	}
    659 
    660 	(void)AcpiGetObjectInfo(handle, &buf);
    661 	AcpiOsFree(*di);
    662 	*di = buf.Pointer;
    663 
    664 #ifdef ACPI_DEBUG
    665 	aprint_normal("acpi_activate_device: %s, new status=%x\n",
    666 	       (*di)->HardwareId.Value, (*di)->CurrentStatus);
    667 #endif
    668 }
    669 #endif /* ACPI_ACTIVATE_DEV */
    670 
    671 /*
    672  * acpi_make_devnode:
    673  *
    674  *	Make an ACPI devnode.
    675  */
    676 static ACPI_STATUS
    677 acpi_make_devnode(ACPI_HANDLE handle, UINT32 level, void *context,
    678     void **status)
    679 {
    680 	struct acpi_make_devnode_state *state = context;
    681 #if defined(ACPI_DEBUG) || defined(ACPI_EXTRA_DEBUG)
    682 	struct acpi_softc *sc = state->softc;
    683 #endif
    684 	struct acpi_scope *as = state->scope;
    685 	struct acpi_devnode *ad;
    686 	ACPI_OBJECT_TYPE type;
    687 	ACPI_BUFFER buf;
    688 	ACPI_DEVICE_INFO *devinfo;
    689 	ACPI_STATUS rv;
    690 	ACPI_NAME_UNION *anu;
    691 	int i, clear = 0;
    692 
    693 	rv = AcpiGetType(handle, &type);
    694 	if (ACPI_SUCCESS(rv)) {
    695 		buf.Pointer = NULL;
    696 		buf.Length = ACPI_ALLOCATE_BUFFER;
    697 		rv = AcpiGetObjectInfo(handle, &buf);
    698 		if (ACPI_FAILURE(rv)) {
    699 #ifdef ACPI_DEBUG
    700 			aprint_normal("%s: AcpiGetObjectInfo failed: %s\n",
    701 			    sc->sc_dev.dv_xname, AcpiFormatException(rv));
    702 #endif
    703 			goto out; /* XXX why return OK */
    704 		}
    705 
    706 		devinfo = buf.Pointer;
    707 
    708 		switch (type) {
    709 		case ACPI_TYPE_DEVICE:
    710 #ifdef ACPI_ACTIVATE_DEV
    711 			if ((devinfo->Valid & (ACPI_VALID_STA|ACPI_VALID_HID)) ==
    712 			    (ACPI_VALID_STA|ACPI_VALID_HID) &&
    713 			    (devinfo->CurrentStatus &
    714 			     (ACPI_STA_DEV_PRESENT|ACPI_STA_DEV_ENABLED)) ==
    715 			    ACPI_STA_DEV_PRESENT)
    716 				acpi_activate_device(handle, &devinfo);
    717 
    718 			/* FALLTHROUGH */
    719 #endif
    720 
    721 		case ACPI_TYPE_PROCESSOR:
    722 		case ACPI_TYPE_THERMAL:
    723 		case ACPI_TYPE_POWER:
    724 			ad = malloc(sizeof(*ad), M_ACPI, M_NOWAIT|M_ZERO);
    725 			if (ad == NULL)
    726 				return AE_NO_MEMORY;
    727 
    728 			ad->ad_devinfo = devinfo;
    729 			ad->ad_handle = handle;
    730 			ad->ad_level = level;
    731 			ad->ad_scope = as;
    732 			ad->ad_type = type;
    733 
    734 			anu = (ACPI_NAME_UNION *)&devinfo->Name;
    735 			ad->ad_name[4] = '\0';
    736 			for (i = 3, clear = 0; i >= 0; i--) {
    737 				if (!clear && anu->Ascii[i] == '_')
    738 					ad->ad_name[i] = '\0';
    739 				else {
    740 					ad->ad_name[i] = anu->Ascii[i];
    741 					clear = 1;
    742 				}
    743 			}
    744 			if (ad->ad_name[0] == '\0')
    745 				ad->ad_name[0] = '_';
    746 
    747 			TAILQ_INSERT_TAIL(&as->as_devnodes, ad, ad_list);
    748 
    749 			if (type == ACPI_TYPE_DEVICE &&
    750 			    (ad->ad_devinfo->Valid & ACPI_VALID_HID) == 0)
    751 				goto out;
    752 
    753 #ifdef ACPI_EXTRA_DEBUG
    754 			aprint_normal("%s: HID %s found in scope %s level %d\n",
    755 			    sc->sc_dev.dv_xname,
    756 			    ad->ad_devinfo->HardwareId.Value,
    757 			    as->as_name, ad->ad_level);
    758 			if (ad->ad_devinfo->Valid & ACPI_VALID_UID)
    759 				aprint_normal("       UID %s\n",
    760 				    ad->ad_devinfo->UniqueId.Value);
    761 			if (ad->ad_devinfo->Valid & ACPI_VALID_ADR)
    762 				aprint_normal("       ADR 0x%016qx\n",
    763 				    ad->ad_devinfo->Address);
    764 			if (ad->ad_devinfo->Valid & ACPI_VALID_STA)
    765 				aprint_normal("       STA 0x%08x\n",
    766 				    ad->ad_devinfo->CurrentStatus);
    767 #endif
    768 		}
    769 	}
    770  out:
    771 	return AE_OK;
    772 }
    773 
    774 /*
    775  * acpi_print:
    776  *
    777  *	Autoconfiguration print routine for ACPI node bus.
    778  */
    779 static int
    780 acpi_print(void *aux, const char *pnp)
    781 {
    782 	struct acpi_attach_args *aa = aux;
    783 	ACPI_STATUS rv;
    784 
    785 	if (pnp) {
    786 		if (aa->aa_node->ad_devinfo->Valid & ACPI_VALID_HID) {
    787 			char *pnpstr =
    788 			    aa->aa_node->ad_devinfo->HardwareId.Value;
    789 			char *str;
    790 
    791 			aprint_normal("%s (%s) ", aa->aa_node->ad_name,
    792 			    pnpstr);
    793 			rv = acpi_eval_string(aa->aa_node->ad_handle,
    794 			    "_STR", &str);
    795 			if (ACPI_SUCCESS(rv)) {
    796 				aprint_normal("[%s] ", str);
    797 				AcpiOsFree(str);
    798 			}
    799 #ifdef ACPIVERBOSE
    800 			else {
    801 				int i;
    802 
    803 				for (i = 0; i < sizeof(acpi_knowndevs) /
    804 				    sizeof(acpi_knowndevs[0]); i++) {
    805 					if (strcmp(acpi_knowndevs[i].pnp,
    806 					    pnpstr) == 0) {
    807 						aprint_normal("[%s] ",
    808 						    acpi_knowndevs[i].str);
    809 					}
    810 				}
    811 			}
    812 
    813 #endif
    814 			aprint_normal("at %s", pnp);
    815 		} else if (aa->aa_node->ad_devinfo->Type != ACPI_TYPE_DEVICE) {
    816 			aprint_normal("%s (ACPI Object Type '%s' "
    817 			    "[0x%02x]) ", aa->aa_node->ad_name,
    818 			     AcpiUtGetTypeName(aa->aa_node->ad_devinfo->Type),
    819 			     aa->aa_node->ad_devinfo->Type);
    820 			aprint_normal("at %s", pnp);
    821 		} else
    822 			return 0;
    823 	} else {
    824 		aprint_normal(" (%s", aa->aa_node->ad_name);
    825 		if (aa->aa_node->ad_devinfo->Valid & ACPI_VALID_HID) {
    826 			aprint_normal(", %s", aa->aa_node->ad_devinfo->HardwareId.Value);
    827 			if (aa->aa_node->ad_devinfo->Valid & ACPI_VALID_UID) {
    828 				const char *uid;
    829 
    830 				uid = aa->aa_node->ad_devinfo->UniqueId.Value;
    831 				if (uid[0] == '\0')
    832 					uid = "<null>";
    833 				aprint_normal("-%s", uid);
    834 			}
    835 		}
    836 		aprint_normal(")");
    837 	}
    838 
    839 	return UNCONF;
    840 }
    841 
    842 /*****************************************************************************
    843  * ACPI fixed-hardware feature handlers
    844  *****************************************************************************/
    845 
    846 static UINT32	acpi_fixed_button_handler(void *);
    847 static void	acpi_fixed_button_pressed(void *);
    848 
    849 /*
    850  * acpi_enable_fixed_events:
    851  *
    852  *	Enable any fixed-hardware feature handlers.
    853  */
    854 static void
    855 acpi_enable_fixed_events(struct acpi_softc *sc)
    856 {
    857 	static int beenhere;
    858 	ACPI_STATUS rv;
    859 
    860 	KASSERT(beenhere == 0);
    861 	beenhere = 1;
    862 
    863 	/*
    864 	 * Check for fixed-hardware buttons.
    865 	 */
    866 
    867 	if ((AcpiGbl_FADT.Flags & ACPI_FADT_POWER_BUTTON) == 0) {
    868 		aprint_verbose("%s: fixed-feature power button present\n",
    869 		    sc->sc_dev.dv_xname);
    870 		sc->sc_smpsw_power.smpsw_name = sc->sc_dev.dv_xname;
    871 		sc->sc_smpsw_power.smpsw_type = PSWITCH_TYPE_POWER;
    872 		if (sysmon_pswitch_register(&sc->sc_smpsw_power) != 0) {
    873 			aprint_error("%s: unable to register fixed power "
    874 			    "button with sysmon\n", sc->sc_dev.dv_xname);
    875 		} else {
    876 			rv = AcpiInstallFixedEventHandler(
    877 			    ACPI_EVENT_POWER_BUTTON,
    878 			    acpi_fixed_button_handler, &sc->sc_smpsw_power);
    879 			if (ACPI_FAILURE(rv)) {
    880 				aprint_error("%s: unable to install handler "
    881 				    "for fixed power button: %s\n",
    882 				    sc->sc_dev.dv_xname,
    883 				    AcpiFormatException(rv));
    884 			}
    885 		}
    886 	}
    887 
    888 	if ((AcpiGbl_FADT.Flags & ACPI_FADT_SLEEP_BUTTON) == 0) {
    889 		aprint_verbose("%s: fixed-feature sleep button present\n",
    890 		    sc->sc_dev.dv_xname);
    891 		sc->sc_smpsw_sleep.smpsw_name = sc->sc_dev.dv_xname;
    892 		sc->sc_smpsw_sleep.smpsw_type = PSWITCH_TYPE_SLEEP;
    893 		if (sysmon_pswitch_register(&sc->sc_smpsw_power) != 0) {
    894 			aprint_error("%s: unable to register fixed sleep "
    895 			    "button with sysmon\n", sc->sc_dev.dv_xname);
    896 		} else {
    897 			rv = AcpiInstallFixedEventHandler(
    898 			    ACPI_EVENT_SLEEP_BUTTON,
    899 			    acpi_fixed_button_handler, &sc->sc_smpsw_sleep);
    900 			if (ACPI_FAILURE(rv)) {
    901 				aprint_error("%s: unable to install handler "
    902 				    "for fixed sleep button: %s\n",
    903 				    sc->sc_dev.dv_xname,
    904 				    AcpiFormatException(rv));
    905 			}
    906 		}
    907 	}
    908 }
    909 
    910 /*
    911  * acpi_fixed_button_handler:
    912  *
    913  *	Event handler for the fixed buttons.
    914  */
    915 static UINT32
    916 acpi_fixed_button_handler(void *context)
    917 {
    918 	struct sysmon_pswitch *smpsw = context;
    919 	int rv;
    920 
    921 #ifdef ACPI_BUT_DEBUG
    922 	printf("%s: fixed button handler\n", smpsw->smpsw_name);
    923 #endif
    924 
    925 	rv = AcpiOsExecute(OSL_NOTIFY_HANDLER,
    926 	    acpi_fixed_button_pressed, smpsw);
    927 	if (ACPI_FAILURE(rv))
    928 		printf("%s: WARNING: unable to queue fixed button pressed "
    929 		    "callback: %s\n", smpsw->smpsw_name,
    930 		    AcpiFormatException(rv));
    931 
    932 	return ACPI_INTERRUPT_HANDLED;
    933 }
    934 
    935 /*
    936  * acpi_fixed_button_pressed:
    937  *
    938  *	Deal with a fixed button being pressed.
    939  */
    940 static void
    941 acpi_fixed_button_pressed(void *context)
    942 {
    943 	struct sysmon_pswitch *smpsw = context;
    944 
    945 #ifdef ACPI_BUT_DEBUG
    946 	printf("%s: fixed button pressed, calling sysmon\n",
    947 	    smpsw->smpsw_name);
    948 #endif
    949 
    950 	sysmon_pswitch_event(smpsw, PSWITCH_EVENT_PRESSED);
    951 }
    952 
    953 /*****************************************************************************
    954  * ACPI utility routines.
    955  *****************************************************************************/
    956 
    957 /*
    958  * acpi_eval_integer:
    959  *
    960  *	Evaluate an integer object.
    961  */
    962 ACPI_STATUS
    963 acpi_eval_integer(ACPI_HANDLE handle, const char *path, ACPI_INTEGER *valp)
    964 {
    965 	ACPI_STATUS rv;
    966 	ACPI_BUFFER buf;
    967 	ACPI_OBJECT param;
    968 
    969 	if (handle == NULL)
    970 		handle = ACPI_ROOT_OBJECT;
    971 
    972 	buf.Pointer = &param;
    973 	buf.Length = sizeof(param);
    974 
    975 	rv = AcpiEvaluateObjectTyped(handle, path, NULL, &buf, ACPI_TYPE_INTEGER);
    976 	if (ACPI_SUCCESS(rv))
    977 		*valp = param.Integer.Value;
    978 
    979 	return rv;
    980 }
    981 
    982 /*
    983  * acpi_eval_string:
    984  *
    985  *	Evaluate a (Unicode) string object.
    986  */
    987 ACPI_STATUS
    988 acpi_eval_string(ACPI_HANDLE handle, const char *path, char **stringp)
    989 {
    990 	ACPI_STATUS rv;
    991 	ACPI_BUFFER buf;
    992 
    993 	if (handle == NULL)
    994 		handle = ACPI_ROOT_OBJECT;
    995 
    996 	buf.Pointer = NULL;
    997 	buf.Length = ACPI_ALLOCATE_BUFFER;
    998 
    999 	rv = AcpiEvaluateObjectTyped(handle, path, NULL, &buf, ACPI_TYPE_STRING);
   1000 	if (ACPI_SUCCESS(rv)) {
   1001 		ACPI_OBJECT *param = buf.Pointer;
   1002 		const char *ptr = param->String.Pointer;
   1003 		size_t len = param->String.Length;
   1004 		if ((*stringp = AcpiOsAllocate(len)) == NULL)
   1005 			rv = AE_NO_MEMORY;
   1006 		else
   1007 			(void)memcpy(*stringp, ptr, len);
   1008 		AcpiOsFree(param);
   1009 	}
   1010 
   1011 	return rv;
   1012 }
   1013 
   1014 
   1015 /*
   1016  * acpi_eval_struct:
   1017  *
   1018  *	Evaluate a more complex structure.
   1019  *	Caller must free buf.Pointer by AcpiOsFree().
   1020  */
   1021 ACPI_STATUS
   1022 acpi_eval_struct(ACPI_HANDLE handle, const char *path, ACPI_BUFFER *bufp)
   1023 {
   1024 	ACPI_STATUS rv;
   1025 
   1026 	if (handle == NULL)
   1027 		handle = ACPI_ROOT_OBJECT;
   1028 
   1029 	bufp->Pointer = NULL;
   1030 	bufp->Length = ACPI_ALLOCATE_BUFFER;
   1031 
   1032 	rv = AcpiEvaluateObject(handle, path, NULL, bufp);
   1033 
   1034 	return rv;
   1035 }
   1036 
   1037 /*
   1038  * acpi_foreach_package_object:
   1039  *
   1040  *	Iterate over all objects in a in a packages and pass then all
   1041  *	to a function. If the called function returns non AE_OK, the
   1042  *	iteration is stopped and that value is returned.
   1043  */
   1044 
   1045 ACPI_STATUS
   1046 acpi_foreach_package_object(ACPI_OBJECT *pkg,
   1047     ACPI_STATUS (*func)(ACPI_OBJECT *, void *),
   1048     void *arg)
   1049 {
   1050 	ACPI_STATUS rv = AE_OK;
   1051 	int i;
   1052 
   1053 	if (pkg == NULL || pkg->Type != ACPI_TYPE_PACKAGE)
   1054 		return AE_BAD_PARAMETER;
   1055 
   1056 	for (i = 0; i < pkg->Package.Count; i++) {
   1057 		rv = (*func)(&pkg->Package.Elements[i], arg);
   1058 		if (ACPI_FAILURE(rv))
   1059 			break;
   1060 	}
   1061 
   1062 	return rv;
   1063 }
   1064 
   1065 const char *
   1066 acpi_name(ACPI_HANDLE handle)
   1067 {
   1068 	static char buffer[80];
   1069 	ACPI_BUFFER buf;
   1070 	ACPI_STATUS rv;
   1071 
   1072 	buf.Length = sizeof(buffer);
   1073 	buf.Pointer = buffer;
   1074 
   1075 	rv = AcpiGetName(handle, ACPI_FULL_PATHNAME, &buf);
   1076 	if (ACPI_FAILURE(rv))
   1077 		return "(unknown acpi path)";
   1078 	return buffer;
   1079 }
   1080 
   1081 /*
   1082  * acpi_get:
   1083  *
   1084  *	Fetch data info the specified (empty) ACPI buffer.
   1085  *	Caller must free buf.Pointer by AcpiOsFree().
   1086  */
   1087 ACPI_STATUS
   1088 acpi_get(ACPI_HANDLE handle, ACPI_BUFFER *buf,
   1089     ACPI_STATUS (*getit)(ACPI_HANDLE, ACPI_BUFFER *))
   1090 {
   1091 	buf->Pointer = NULL;
   1092 	buf->Length = ACPI_ALLOCATE_BUFFER;
   1093 
   1094 	return (*getit)(handle, buf);
   1095 }
   1096 
   1097 
   1098 /*
   1099  * acpi_match_hid
   1100  *
   1101  *	Match given ids against _HID and _CIDs
   1102  */
   1103 int
   1104 acpi_match_hid(ACPI_DEVICE_INFO *ad, const char * const *ids)
   1105 {
   1106 	int i;
   1107 
   1108 	while (*ids) {
   1109 		if (ad->Valid & ACPI_VALID_HID) {
   1110 			if (pmatch(ad->HardwareId.Value, *ids, NULL) == 2)
   1111 				return 1;
   1112 		}
   1113 
   1114 		if (ad->Valid & ACPI_VALID_CID) {
   1115 			for (i = 0; i < ad->CompatibilityId.Count; i++) {
   1116 				if (pmatch(ad->CompatibilityId.Id[i].Value, *ids, NULL) == 2)
   1117 					return 1;
   1118 			}
   1119 		}
   1120 		ids++;
   1121 	}
   1122 
   1123 	return 0;
   1124 }
   1125 
   1126 /*
   1127  * acpi_set_wake_gpe
   1128  *
   1129  *	Set GPE as both Runtime and Wake
   1130  */
   1131 void
   1132 acpi_set_wake_gpe(ACPI_HANDLE handle)
   1133 {
   1134 	ACPI_BUFFER buf;
   1135 	ACPI_STATUS rv;
   1136 	ACPI_OBJECT *p, *elt;
   1137 
   1138 	rv = acpi_eval_struct(handle, METHOD_NAME__PRW, &buf);
   1139 	if (ACPI_FAILURE(rv))
   1140 		return;			/* just ignore */
   1141 
   1142 	p = buf.Pointer;
   1143 	if (p->Type != ACPI_TYPE_PACKAGE || p->Package.Count < 2)
   1144 		goto out;		/* just ignore */
   1145 
   1146 	elt = p->Package.Elements;
   1147 
   1148 	/* TBD: package support */
   1149 	AcpiSetGpeType(NULL, elt[0].Integer.Value, ACPI_GPE_TYPE_WAKE_RUN);
   1150 	AcpiEnableGpe(NULL, elt[0].Integer.Value, ACPI_NOT_ISR);
   1151 
   1152  out:
   1153 	AcpiOsFree(buf.Pointer);
   1154 }
   1155 
   1156 
   1157 /*****************************************************************************
   1158  * ACPI sleep support.
   1159  *****************************************************************************/
   1160 
   1161 static int
   1162 is_available_state(struct acpi_softc *sc, int state)
   1163 {
   1164 	UINT8 type_a, type_b;
   1165 
   1166 	return ACPI_SUCCESS(AcpiGetSleepTypeData((UINT8)state,
   1167 				&type_a, &type_b));
   1168 }
   1169 
   1170 /*
   1171  * acpi_enter_sleep_state:
   1172  *
   1173  *	enter to the specified sleep state.
   1174  */
   1175 
   1176 ACPI_STATUS
   1177 acpi_enter_sleep_state(struct acpi_softc *sc, int state)
   1178 {
   1179 	int err;
   1180 	ACPI_STATUS ret = AE_OK;
   1181 
   1182 	if (state == acpi_sleepstate)
   1183 		return AE_OK;
   1184 
   1185 	aprint_normal("%s: entering state %d\n", sc->sc_dev.dv_xname, state);
   1186 
   1187 	switch (state) {
   1188 	case ACPI_STATE_S0:
   1189 		break;
   1190 	case ACPI_STATE_S1:
   1191 	case ACPI_STATE_S2:
   1192 	case ACPI_STATE_S3:
   1193 	case ACPI_STATE_S4:
   1194 		if (!is_available_state(sc, state)) {
   1195 			aprint_error("%s: cannot enter the sleep state (%d)\n",
   1196 			    sc->sc_dev.dv_xname, state);
   1197 			break;
   1198 		}
   1199 
   1200 		if (state != ACPI_STATE_S1 && !pmf_system_suspend()) {
   1201 			aprint_error_dev(&sc->sc_dev, "aborting suspend\n");
   1202 			break;
   1203 		}
   1204 
   1205 		ret = AcpiEnterSleepStatePrep(state);
   1206 		if (ACPI_FAILURE(ret)) {
   1207 			aprint_error("%s: failed preparing to sleep (%s)\n",
   1208 			    sc->sc_dev.dv_xname, AcpiFormatException(ret));
   1209 			break;
   1210 		}
   1211 
   1212 		acpi_sleepstate = state;
   1213 		if (state == ACPI_STATE_S1) {
   1214 			/* just enter the state */
   1215 			acpi_md_OsDisableInterrupt();
   1216 			AcpiEnterSleepState((UINT8)state);
   1217 			AcpiLeaveSleepState((UINT8)state);
   1218 		} else {
   1219 			err = acpi_md_sleep(state);
   1220 			if (state == ACPI_STATE_S4)
   1221 				AcpiEnable();
   1222 			pmf_system_bus_resume();
   1223 			AcpiLeaveSleepState((UINT8)state);
   1224 			pmf_system_resume();
   1225 		}
   1226 
   1227 		break;
   1228 	case ACPI_STATE_S5:
   1229 		ret = AcpiEnterSleepStatePrep(ACPI_STATE_S5);
   1230 		if (ACPI_FAILURE(ret)) {
   1231 			aprint_error("%s: failed preparing to sleep (%s)\n",
   1232 			       sc->sc_dev.dv_xname, AcpiFormatException(ret));
   1233 			break;
   1234 		}
   1235 		DELAY(1000000);
   1236 		acpi_sleepstate = state;
   1237 		acpi_md_OsDisableInterrupt();
   1238 		AcpiEnterSleepState(ACPI_STATE_S5);
   1239 		aprint_error("%s: WARNING powerdown failed!\n",
   1240 		    sc->sc_dev.dv_xname);
   1241 		break;
   1242 	}
   1243 
   1244 	acpi_sleepstate = ACPI_STATE_S0;
   1245 	return ret;
   1246 }
   1247 
   1248 #if defined(ACPI_ACTIVATE_DEV)
   1249 /* XXX This very incomplete */
   1250 ACPI_STATUS
   1251 acpi_allocate_resources(ACPI_HANDLE handle)
   1252 {
   1253 	ACPI_BUFFER bufp, bufc, bufn;
   1254 	ACPI_RESOURCE *resp, *resc, *resn;
   1255 	ACPI_RESOURCE_IRQ *irq;
   1256 	ACPI_RESOURCE_EXTENDED_IRQ *xirq;
   1257 	ACPI_STATUS rv;
   1258 	uint delta;
   1259 
   1260 	rv = acpi_get(handle, &bufp, AcpiGetPossibleResources);
   1261 	if (ACPI_FAILURE(rv))
   1262 		goto out;
   1263 	rv = acpi_get(handle, &bufc, AcpiGetCurrentResources);
   1264 	if (ACPI_FAILURE(rv)) {
   1265 		goto out1;
   1266 	}
   1267 
   1268 	bufn.Length = 1000;
   1269 	bufn.Pointer = resn = malloc(bufn.Length, M_ACPI, M_WAITOK);
   1270 	resp = bufp.Pointer;
   1271 	resc = bufc.Pointer;
   1272 	while (resc->Type != ACPI_RESOURCE_TYPE_END_TAG &&
   1273 	       resp->Type != ACPI_RESOURCE_TYPE_END_TAG) {
   1274 		while (resc->Type != resp->Type && resp->Type != ACPI_RESOURCE_TYPE_END_TAG)
   1275 			resp = ACPI_NEXT_RESOURCE(resp);
   1276 		if (resp->Type == ACPI_RESOURCE_TYPE_END_TAG)
   1277 			break;
   1278 		/* Found identical Id */
   1279 		resn->Type = resc->Type;
   1280 		switch (resc->Type) {
   1281 		case ACPI_RESOURCE_TYPE_IRQ:
   1282 			memcpy(&resn->Data, &resp->Data,
   1283 			       sizeof(ACPI_RESOURCE_IRQ));
   1284 			irq = (ACPI_RESOURCE_IRQ *)&resn->Data;
   1285 			irq->Interrupts[0] =
   1286 			    ((ACPI_RESOURCE_IRQ *)&resp->Data)->
   1287 			        Interrupts[irq->InterruptCount-1];
   1288 			irq->InterruptCount = 1;
   1289 			resn->Length = ACPI_RS_SIZE(ACPI_RESOURCE_IRQ);
   1290 			break;
   1291 		case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
   1292 			memcpy(&resn->Data, &resp->Data,
   1293 			       sizeof(ACPI_RESOURCE_EXTENDED_IRQ));
   1294 			xirq = (ACPI_RESOURCE_EXTENDED_IRQ *)&resn->Data;
   1295 #if 0
   1296 			/*
   1297 			 * XXX not duplicating the interrupt logic above
   1298 			 * because its not clear what it accomplishes.
   1299 			 */
   1300 			xirq->Interrupts[0] =
   1301 			    ((ACPI_RESOURCE_EXT_IRQ *)&resp->Data)->
   1302 			    Interrupts[irq->NumberOfInterrupts-1];
   1303 			xirq->NumberOfInterrupts = 1;
   1304 #endif
   1305 			resn->Length = ACPI_RS_SIZE(ACPI_RESOURCE_EXTENDED_IRQ);
   1306 			break;
   1307 		case ACPI_RESOURCE_TYPE_IO:
   1308 			memcpy(&resn->Data, &resp->Data,
   1309 			       sizeof(ACPI_RESOURCE_IO));
   1310 			resn->Length = resp->Length;
   1311 			break;
   1312 		default:
   1313 			printf("acpi_allocate_resources: res=%d\n", resc->Type);
   1314 			rv = AE_BAD_DATA;
   1315 			goto out2;
   1316 		}
   1317 		resc = ACPI_NEXT_RESOURCE(resc);
   1318 		resn = ACPI_NEXT_RESOURCE(resn);
   1319 		resp = ACPI_NEXT_RESOURCE(resp);
   1320 		delta = (UINT8 *)resn - (UINT8 *)bufn.Pointer;
   1321 		if (delta >=
   1322 		    bufn.Length-ACPI_RS_SIZE(ACPI_RESOURCE_DATA)) {
   1323 			bufn.Length *= 2;
   1324 			bufn.Pointer = realloc(bufn.Pointer, bufn.Length,
   1325 					       M_ACPI, M_WAITOK);
   1326 			resn = (ACPI_RESOURCE *)((UINT8 *)bufn.Pointer + delta);
   1327 		}
   1328 	}
   1329 	if (resc->Type != ACPI_RESOURCE_TYPE_END_TAG) {
   1330 		printf("acpi_allocate_resources: resc not exhausted\n");
   1331 		rv = AE_BAD_DATA;
   1332 		goto out3;
   1333 	}
   1334 
   1335 	resn->Type = ACPI_RESOURCE_TYPE_END_TAG;
   1336 	rv = AcpiSetCurrentResources(handle, &bufn);
   1337 	if (ACPI_FAILURE(rv)) {
   1338 		printf("acpi_allocate_resources: AcpiSetCurrentResources %s\n",
   1339 		       AcpiFormatException(rv));
   1340 	}
   1341 
   1342 out3:
   1343 	free(bufn.Pointer, M_ACPI);
   1344 out2:
   1345 	AcpiOsFree(bufc.Pointer);
   1346 out1:
   1347 	AcpiOsFree(bufp.Pointer);
   1348 out:
   1349 	return rv;
   1350 }
   1351 #endif /* ACPI_ACTIVATE_DEV */
   1352 
   1353 SYSCTL_SETUP(sysctl_acpi_setup, "sysctl hw.acpi subtree setup")
   1354 {
   1355 	const struct sysctlnode *node;
   1356 	const struct sysctlnode *ssnode;
   1357 
   1358 	if (sysctl_createv(clog, 0, NULL, NULL,
   1359 	    CTLFLAG_PERMANENT,
   1360 	    CTLTYPE_NODE, "hw", NULL,
   1361 	    NULL, 0, NULL, 0,
   1362 	    CTL_HW, CTL_EOL) != 0)
   1363 		return;
   1364 
   1365 	if (sysctl_createv(clog, 0, NULL, &node,
   1366 	    CTLFLAG_PERMANENT,
   1367 	    CTLTYPE_NODE, "acpi", NULL,
   1368 	    NULL, 0, NULL, 0,
   1369 	    CTL_HW, CTL_CREATE, CTL_EOL) != 0)
   1370 		return;
   1371 
   1372 	acpi_node = node->sysctl_num;
   1373 
   1374 	/* ACPI sleepstate sysctl */
   1375 	if (sysctl_createv(NULL, 0, NULL, &node,
   1376 	    CTLFLAG_PERMANENT,
   1377 	    CTLTYPE_NODE, "machdep", NULL,
   1378 	    NULL, 0, NULL, 0, CTL_MACHDEP, CTL_EOL) != 0)
   1379 		return;
   1380 	if (sysctl_createv(NULL, 0, &node, &ssnode,
   1381 	    CTLFLAG_READWRITE, CTLTYPE_INT, "sleep_state",
   1382 	    NULL, sysctl_hw_acpi_sleepstate, 0, NULL, 0, CTL_CREATE,
   1383 	    CTL_EOL) != 0)
   1384 		return;
   1385 }
   1386 
   1387 static int
   1388 sysctl_hw_acpi_sleepstate(SYSCTLFN_ARGS)
   1389 {
   1390 	int error, t;
   1391 	struct sysctlnode node;
   1392 
   1393 	node = *rnode;
   1394 	t = acpi_sleepstate;
   1395 	node.sysctl_data = &t;
   1396 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1397 	if (error || newp == NULL)
   1398 		return error;
   1399 
   1400 	if (acpi_softc == NULL)
   1401 		return ENOSYS;
   1402 
   1403 	acpi_enter_sleep_state(acpi_softc, t);
   1404 
   1405 	return 0;
   1406 }
   1407 
   1408 static ACPI_TABLE_HEADER *
   1409 acpi_map_rsdt(void)
   1410 {
   1411 	ACPI_PHYSICAL_ADDRESS paddr;
   1412 	ACPI_TABLE_RSDP *rsdp;
   1413 
   1414 	paddr = AcpiOsGetRootPointer();
   1415 	if (paddr == 0) {
   1416 		printf("ACPI: couldn't get root pointer\n");
   1417 		return NULL;
   1418 	}
   1419 	rsdp = AcpiOsMapMemory(paddr, sizeof(ACPI_TABLE_RSDP));
   1420 	if (rsdp == NULL) {
   1421 		printf("ACPI: couldn't map RSDP\n");
   1422 		return NULL;
   1423 	}
   1424 	if (rsdp->Revision > 1 && rsdp->XsdtPhysicalAddress)
   1425 		paddr = (ACPI_PHYSICAL_ADDRESS)rsdp->XsdtPhysicalAddress;
   1426 	else
   1427 		paddr = (ACPI_PHYSICAL_ADDRESS)rsdp->RsdtPhysicalAddress;
   1428 	AcpiOsUnmapMemory(rsdp, sizeof(ACPI_TABLE_RSDP));
   1429 
   1430 	return AcpiOsMapMemory(paddr, sizeof(ACPI_TABLE_HEADER));
   1431 }
   1432 
   1433 static void
   1434 acpi_unmap_rsdt(ACPI_TABLE_HEADER *rsdt)
   1435 {
   1436 	if (rsdt == NULL)
   1437 		return;
   1438 
   1439 	AcpiOsUnmapMemory(rsdt, sizeof(ACPI_TABLE_HEADER));
   1440 }
   1441