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