Home | History | Annotate | Line # | Download | only in acpi
acpi.c revision 1.101.16.8
      1 /*	$NetBSD: acpi.c,v 1.101.16.8 2007/08/23 09:25:29 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.8 2007/08/23 09:25:29 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 	/* Show SCI interrupt. */
    379 	if (AcpiGbl_FADT != NULL)
    380 		aprint_verbose("%s: SCI interrupting at int %d\n",
    381 		    sc->sc_dev.dv_xname, AcpiGbl_FADT->SciInt);
    382 
    383 	acpi_md_callback();
    384 
    385 	rv = AcpiEnableSubsystem(0);
    386 	if (ACPI_FAILURE(rv)) {
    387 		aprint_error("%s: unable to enable ACPI: %s\n",
    388 		    sc->sc_dev.dv_xname, AcpiFormatException(rv));
    389 		return;
    390 	}
    391 
    392 	/* early EC handler initialization if ECDT table is available */
    393 #if NACPIEC > 0
    394 	acpiec_early_attach(&sc->sc_dev);
    395 #endif
    396 
    397 	rv = AcpiInitializeObjects(0);
    398 	if (ACPI_FAILURE(rv)) {
    399 		aprint_error("%s: unable to initialize ACPI objects: %s\n",
    400 		    sc->sc_dev.dv_xname, AcpiFormatException(rv));
    401 		return;
    402 	}
    403 	acpi_active = 1;
    404 
    405 	/* Our current state is "awake". */
    406 	sc->sc_sleepstate = ACPI_STATE_S0;
    407 	/*
    408 	 * Check for fixed-hardware features.
    409 	 */
    410 	acpi_enable_fixed_events(sc);
    411 	acpitimer_init();
    412 
    413 	/*
    414 	 * Scan the namespace and build our device tree.
    415 	 */
    416 #ifdef ACPI_DEBUGGER
    417 	if (acpi_dbgr & ACPI_DBGR_PROBE)
    418 		acpi_osd_debugger();
    419 #endif
    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 			ad->ad_device = config_found_ia(&sc->sc_dev,
    563 			    "acpinodebus", &aa, acpi_print);
    564 		}
    565 	}
    566 	config_found_ia(&sc->sc_dev, "acpiapmbus", NULL, NULL);
    567 }
    568 
    569 #ifdef ACPI_ACTIVATE_DEV
    570 static void
    571 acpi_activate_device(ACPI_HANDLE handle, ACPI_DEVICE_INFO **di)
    572 {
    573 	ACPI_STATUS rv;
    574 	ACPI_BUFFER buf;
    575 
    576 	buf.Pointer = NULL;
    577 	buf.Length = ACPI_ALLOCATE_BUFFER;
    578 
    579 #ifdef ACPI_DEBUG
    580 	aprint_normal("acpi_activate_device: %s, old status=%x\n",
    581 	       (*di)->HardwareId.Value, (*di)->CurrentStatus);
    582 #endif
    583 
    584 	rv = acpi_allocate_resources(handle);
    585 	if (ACPI_FAILURE(rv)) {
    586 		aprint_error("acpi: activate failed for %s\n",
    587 		       (*di)->HardwareId.Value);
    588 	} else {
    589 		aprint_verbose("acpi: activated %s\n",
    590 		    (*di)->HardwareId.Value);
    591 	}
    592 
    593 	(void)AcpiGetObjectInfo(handle, &buf);
    594 	AcpiOsFree(*di);
    595 	*di = buf.Pointer;
    596 
    597 #ifdef ACPI_DEBUG
    598 	aprint_normal("acpi_activate_device: %s, new status=%x\n",
    599 	       (*di)->HardwareId.Value, (*di)->CurrentStatus);
    600 #endif
    601 }
    602 #endif /* ACPI_ACTIVATE_DEV */
    603 
    604 /*
    605  * acpi_make_devnode:
    606  *
    607  *	Make an ACPI devnode.
    608  */
    609 static ACPI_STATUS
    610 acpi_make_devnode(ACPI_HANDLE handle, UINT32 level, void *context,
    611     void **status)
    612 {
    613 	struct acpi_make_devnode_state *state = context;
    614 #if defined(ACPI_DEBUG) || defined(ACPI_EXTRA_DEBUG)
    615 	struct acpi_softc *sc = state->softc;
    616 #endif
    617 	struct acpi_scope *as = state->scope;
    618 	struct acpi_devnode *ad;
    619 	ACPI_OBJECT_TYPE type;
    620 	ACPI_BUFFER buf;
    621 	ACPI_DEVICE_INFO *devinfo;
    622 	ACPI_STATUS rv;
    623 	ACPI_NAME_UNION *anu;
    624 	int i, clear = 0;
    625 
    626 	rv = AcpiGetType(handle, &type);
    627 	if (ACPI_SUCCESS(rv)) {
    628 		buf.Pointer = NULL;
    629 		buf.Length = ACPI_ALLOCATE_BUFFER;
    630 		rv = AcpiGetObjectInfo(handle, &buf);
    631 		if (ACPI_FAILURE(rv)) {
    632 #ifdef ACPI_DEBUG
    633 			aprint_normal("%s: AcpiGetObjectInfo failed: %s\n",
    634 			    sc->sc_dev.dv_xname, AcpiFormatException(rv));
    635 #endif
    636 			goto out; /* XXX why return OK */
    637 		}
    638 
    639 		devinfo = buf.Pointer;
    640 
    641 		switch (type) {
    642 		case ACPI_TYPE_DEVICE:
    643 #ifdef ACPI_ACTIVATE_DEV
    644 			if ((devinfo->Valid & (ACPI_VALID_STA|ACPI_VALID_HID)) ==
    645 			    (ACPI_VALID_STA|ACPI_VALID_HID) &&
    646 			    (devinfo->CurrentStatus &
    647 			     (ACPI_STA_DEV_PRESENT|ACPI_STA_DEV_ENABLED)) ==
    648 			    ACPI_STA_DEV_PRESENT)
    649 				acpi_activate_device(handle, &devinfo);
    650 
    651 			/* FALLTHROUGH */
    652 #endif
    653 
    654 		case ACPI_TYPE_PROCESSOR:
    655 		case ACPI_TYPE_THERMAL:
    656 		case ACPI_TYPE_POWER:
    657 			ad = malloc(sizeof(*ad), M_ACPI, M_NOWAIT|M_ZERO);
    658 			if (ad == NULL)
    659 				return AE_NO_MEMORY;
    660 
    661 			ad->ad_devinfo = devinfo;
    662 			ad->ad_handle = handle;
    663 			ad->ad_level = level;
    664 			ad->ad_scope = as;
    665 			ad->ad_type = type;
    666 
    667 			anu = (ACPI_NAME_UNION *)&devinfo->Name;
    668 			ad->ad_name[4] = '\0';
    669 			for (i = 3, clear = 0; i >= 0; i--) {
    670 				if (!clear && anu->Ascii[i] == '_')
    671 					ad->ad_name[i] = '\0';
    672 				else {
    673 					ad->ad_name[i] = anu->Ascii[i];
    674 					clear = 1;
    675 				}
    676 			}
    677 			if (ad->ad_name[0] == '\0')
    678 				ad->ad_name[0] = '_';
    679 
    680 			TAILQ_INSERT_TAIL(&as->as_devnodes, ad, ad_list);
    681 
    682 			if ((ad->ad_devinfo->Valid & ACPI_VALID_HID) == 0)
    683 				goto out;
    684 
    685 #ifdef ACPI_EXTRA_DEBUG
    686 			aprint_normal("%s: HID %s found in scope %s level %d\n",
    687 			    sc->sc_dev.dv_xname,
    688 			    ad->ad_devinfo->HardwareId.Value,
    689 			    as->as_name, ad->ad_level);
    690 			if (ad->ad_devinfo->Valid & ACPI_VALID_UID)
    691 				aprint_normal("       UID %s\n",
    692 				    ad->ad_devinfo->UniqueId.Value);
    693 			if (ad->ad_devinfo->Valid & ACPI_VALID_ADR)
    694 				aprint_normal("       ADR 0x%016qx\n",
    695 				    ad->ad_devinfo->Address);
    696 			if (ad->ad_devinfo->Valid & ACPI_VALID_STA)
    697 				aprint_normal("       STA 0x%08x\n",
    698 				    ad->ad_devinfo->CurrentStatus);
    699 #endif
    700 		}
    701 	}
    702  out:
    703 	return AE_OK;
    704 }
    705 
    706 /*
    707  * acpi_print:
    708  *
    709  *	Autoconfiguration print routine for ACPI node bus.
    710  */
    711 static int
    712 acpi_print(void *aux, const char *pnp)
    713 {
    714 	struct acpi_attach_args *aa = aux;
    715 	ACPI_STATUS rv;
    716 
    717 	if (pnp) {
    718 		if (aa->aa_node->ad_devinfo->Valid & ACPI_VALID_HID) {
    719 			char *pnpstr =
    720 			    aa->aa_node->ad_devinfo->HardwareId.Value;
    721 			char *str;
    722 
    723 			aprint_normal("%s (%s) ", aa->aa_node->ad_name,
    724 			    pnpstr);
    725 			rv = acpi_eval_string(aa->aa_node->ad_handle,
    726 			    "_STR", &str);
    727 			if (ACPI_SUCCESS(rv)) {
    728 				aprint_normal("[%s] ", str);
    729 				AcpiOsFree(str);
    730 			}
    731 #ifdef ACPIVERBOSE
    732 			else {
    733 				int i;
    734 
    735 				for (i = 0; i < sizeof(acpi_knowndevs) /
    736 				    sizeof(acpi_knowndevs[0]); i++) {
    737 					if (strcmp(acpi_knowndevs[i].pnp,
    738 					    pnpstr) == 0) {
    739 						aprint_normal("[%s] ",
    740 						    acpi_knowndevs[i].str);
    741 					}
    742 				}
    743 			}
    744 
    745 #endif
    746 			aprint_normal("at %s", pnp);
    747 		} else if (aa->aa_node->ad_devinfo->Type != ACPI_TYPE_DEVICE) {
    748 			aprint_normal("%s (ACPI Object Type '%s' "
    749 			    "[0x%02x]) ", aa->aa_node->ad_name,
    750 			     AcpiUtGetTypeName(aa->aa_node->ad_devinfo->Type),
    751 			     aa->aa_node->ad_devinfo->Type);
    752 			aprint_normal("at %s", pnp);
    753 		} else
    754 			return 0;
    755 	} else {
    756 		aprint_normal(" (%s", aa->aa_node->ad_name);
    757 		if (aa->aa_node->ad_devinfo->Valid & ACPI_VALID_HID) {
    758 			aprint_normal(", %s", aa->aa_node->ad_devinfo->HardwareId.Value);
    759 			if (aa->aa_node->ad_devinfo->Valid & ACPI_VALID_UID) {
    760 				const char *uid;
    761 
    762 				uid = aa->aa_node->ad_devinfo->UniqueId.Value;
    763 				if (uid[0] == '\0')
    764 					uid = "<null>";
    765 				aprint_normal("-%s", uid);
    766 			}
    767 		}
    768 		aprint_normal(")");
    769 	}
    770 
    771 	return UNCONF;
    772 }
    773 
    774 /*****************************************************************************
    775  * ACPI fixed-hardware feature handlers
    776  *****************************************************************************/
    777 
    778 static UINT32	acpi_fixed_button_handler(void *);
    779 static void	acpi_fixed_button_pressed(void *);
    780 
    781 /*
    782  * acpi_enable_fixed_events:
    783  *
    784  *	Enable any fixed-hardware feature handlers.
    785  */
    786 static void
    787 acpi_enable_fixed_events(struct acpi_softc *sc)
    788 {
    789 	static int beenhere;
    790 	ACPI_STATUS rv;
    791 
    792 	KASSERT(beenhere == 0);
    793 	beenhere = 1;
    794 
    795 	/*
    796 	 * Check for fixed-hardware buttons.
    797 	 */
    798 
    799 	if (AcpiGbl_FADT != NULL && AcpiGbl_FADT->PwrButton == 0) {
    800 		aprint_verbose("%s: fixed-feature power button present\n",
    801 		    sc->sc_dev.dv_xname);
    802 		sc->sc_smpsw_power.smpsw_name = sc->sc_dev.dv_xname;
    803 		sc->sc_smpsw_power.smpsw_type = PSWITCH_TYPE_POWER;
    804 		if (sysmon_pswitch_register(&sc->sc_smpsw_power) != 0) {
    805 			aprint_error("%s: unable to register fixed power "
    806 			    "button with sysmon\n", sc->sc_dev.dv_xname);
    807 		} else {
    808 			rv = AcpiInstallFixedEventHandler(
    809 			    ACPI_EVENT_POWER_BUTTON,
    810 			    acpi_fixed_button_handler, &sc->sc_smpsw_power);
    811 			if (ACPI_FAILURE(rv)) {
    812 				aprint_error("%s: unable to install handler "
    813 				    "for fixed power button: %s\n",
    814 				    sc->sc_dev.dv_xname,
    815 				    AcpiFormatException(rv));
    816 			}
    817 		}
    818 	}
    819 
    820 	if (AcpiGbl_FADT != NULL && AcpiGbl_FADT->SleepButton == 0) {
    821 		aprint_verbose("%s: fixed-feature sleep button present\n",
    822 		    sc->sc_dev.dv_xname);
    823 		sc->sc_smpsw_sleep.smpsw_name = sc->sc_dev.dv_xname;
    824 		sc->sc_smpsw_sleep.smpsw_type = PSWITCH_TYPE_SLEEP;
    825 		if (sysmon_pswitch_register(&sc->sc_smpsw_power) != 0) {
    826 			aprint_error("%s: unable to register fixed sleep "
    827 			    "button with sysmon\n", sc->sc_dev.dv_xname);
    828 		} else {
    829 			rv = AcpiInstallFixedEventHandler(
    830 			    ACPI_EVENT_SLEEP_BUTTON,
    831 			    acpi_fixed_button_handler, &sc->sc_smpsw_sleep);
    832 			if (ACPI_FAILURE(rv)) {
    833 				aprint_error("%s: unable to install handler "
    834 				    "for fixed sleep button: %s\n",
    835 				    sc->sc_dev.dv_xname,
    836 				    AcpiFormatException(rv));
    837 			}
    838 		}
    839 	}
    840 }
    841 
    842 /*
    843  * acpi_fixed_button_handler:
    844  *
    845  *	Event handler for the fixed buttons.
    846  */
    847 static UINT32
    848 acpi_fixed_button_handler(void *context)
    849 {
    850 	struct sysmon_pswitch *smpsw = context;
    851 	int rv;
    852 
    853 #ifdef ACPI_BUT_DEBUG
    854 	printf("%s: fixed button handler\n", smpsw->smpsw_name);
    855 #endif
    856 
    857 	rv = AcpiOsQueueForExecution(OSD_PRIORITY_LO,
    858 	    acpi_fixed_button_pressed, smpsw);
    859 	if (ACPI_FAILURE(rv))
    860 		printf("%s: WARNING: unable to queue fixed button pressed "
    861 		    "callback: %s\n", smpsw->smpsw_name,
    862 		    AcpiFormatException(rv));
    863 
    864 	return ACPI_INTERRUPT_HANDLED;
    865 }
    866 
    867 /*
    868  * acpi_fixed_button_pressed:
    869  *
    870  *	Deal with a fixed button being pressed.
    871  */
    872 static void
    873 acpi_fixed_button_pressed(void *context)
    874 {
    875 	struct sysmon_pswitch *smpsw = context;
    876 
    877 #ifdef ACPI_BUT_DEBUG
    878 	printf("%s: fixed button pressed, calling sysmon\n",
    879 	    smpsw->smpsw_name);
    880 #endif
    881 
    882 	sysmon_pswitch_event(smpsw, PSWITCH_EVENT_PRESSED);
    883 }
    884 
    885 /*****************************************************************************
    886  * ACPI utility routines.
    887  *****************************************************************************/
    888 
    889 /*
    890  * acpi_eval_integer:
    891  *
    892  *	Evaluate an integer object.
    893  */
    894 ACPI_STATUS
    895 acpi_eval_integer(ACPI_HANDLE handle, const char *path, ACPI_INTEGER *valp)
    896 {
    897 	ACPI_STATUS rv;
    898 	ACPI_BUFFER buf;
    899 	ACPI_OBJECT param;
    900 
    901 	if (handle == NULL)
    902 		handle = ACPI_ROOT_OBJECT;
    903 
    904 	buf.Pointer = &param;
    905 	buf.Length = sizeof(param);
    906 
    907 	rv = AcpiEvaluateObjectTyped(handle, path, NULL, &buf, ACPI_TYPE_INTEGER);
    908 	if (ACPI_SUCCESS(rv))
    909 		*valp = param.Integer.Value;
    910 
    911 	return rv;
    912 }
    913 
    914 /*
    915  * acpi_eval_string:
    916  *
    917  *	Evaluate a (Unicode) string object.
    918  */
    919 ACPI_STATUS
    920 acpi_eval_string(ACPI_HANDLE handle, const char *path, char **stringp)
    921 {
    922 	ACPI_STATUS rv;
    923 	ACPI_BUFFER buf;
    924 
    925 	if (handle == NULL)
    926 		handle = ACPI_ROOT_OBJECT;
    927 
    928 	buf.Pointer = NULL;
    929 	buf.Length = ACPI_ALLOCATE_BUFFER;
    930 
    931 	rv = AcpiEvaluateObjectTyped(handle, path, NULL, &buf, ACPI_TYPE_STRING);
    932 	if (ACPI_SUCCESS(rv)) {
    933 		ACPI_OBJECT *param = buf.Pointer;
    934 		const char *ptr = param->String.Pointer;
    935 		size_t len = param->String.Length;
    936 		if ((*stringp = AcpiOsAllocate(len)) == NULL)
    937 			rv = AE_NO_MEMORY;
    938 		else
    939 			(void)memcpy(*stringp, ptr, len);
    940 		AcpiOsFree(param);
    941 	}
    942 
    943 	return rv;
    944 }
    945 
    946 
    947 /*
    948  * acpi_eval_struct:
    949  *
    950  *	Evaluate a more complex structure.
    951  *	Caller must free buf.Pointer by AcpiOsFree().
    952  */
    953 ACPI_STATUS
    954 acpi_eval_struct(ACPI_HANDLE handle, const char *path, ACPI_BUFFER *bufp)
    955 {
    956 	ACPI_STATUS rv;
    957 
    958 	if (handle == NULL)
    959 		handle = ACPI_ROOT_OBJECT;
    960 
    961 	bufp->Pointer = NULL;
    962 	bufp->Length = ACPI_ALLOCATE_BUFFER;
    963 
    964 	rv = AcpiEvaluateObject(handle, path, NULL, bufp);
    965 
    966 	return rv;
    967 }
    968 
    969 /*
    970  * acpi_foreach_package_object:
    971  *
    972  *	Iterate over all objects in a in a packages and pass then all
    973  *	to a function. If the called function returns non AE_OK, the
    974  *	iteration is stopped and that value is returned.
    975  */
    976 
    977 ACPI_STATUS
    978 acpi_foreach_package_object(ACPI_OBJECT *pkg,
    979     ACPI_STATUS (*func)(ACPI_OBJECT *, void *),
    980     void *arg)
    981 {
    982 	ACPI_STATUS rv = AE_OK;
    983 	int i;
    984 
    985 	if (pkg == NULL || pkg->Type != ACPI_TYPE_PACKAGE)
    986 		return AE_BAD_PARAMETER;
    987 
    988 	for (i = 0; i < pkg->Package.Count; i++) {
    989 		rv = (*func)(&pkg->Package.Elements[i], arg);
    990 		if (ACPI_FAILURE(rv))
    991 			break;
    992 	}
    993 
    994 	return rv;
    995 }
    996 
    997 const char *
    998 acpi_name(ACPI_HANDLE handle)
    999 {
   1000 	static char buffer[80];
   1001 	ACPI_BUFFER buf;
   1002 	ACPI_STATUS rv;
   1003 
   1004 	buf.Length = sizeof(buffer);
   1005 	buf.Pointer = buffer;
   1006 
   1007 	rv = AcpiGetName(handle, ACPI_FULL_PATHNAME, &buf);
   1008 	if (ACPI_FAILURE(rv))
   1009 		return "(unknown acpi path)";
   1010 	return buffer;
   1011 }
   1012 
   1013 /*
   1014  * acpi_get:
   1015  *
   1016  *	Fetch data info the specified (empty) ACPI buffer.
   1017  *	Caller must free buf.Pointer by AcpiOsFree().
   1018  */
   1019 ACPI_STATUS
   1020 acpi_get(ACPI_HANDLE handle, ACPI_BUFFER *buf,
   1021     ACPI_STATUS (*getit)(ACPI_HANDLE, ACPI_BUFFER *))
   1022 {
   1023 	buf->Pointer = NULL;
   1024 	buf->Length = ACPI_ALLOCATE_BUFFER;
   1025 
   1026 	return (*getit)(handle, buf);
   1027 }
   1028 
   1029 
   1030 /*
   1031  * acpi_match_hid
   1032  *
   1033  *	Match given ids against _HID and _CIDs
   1034  */
   1035 int
   1036 acpi_match_hid(ACPI_DEVICE_INFO *ad, const char * const *ids)
   1037 {
   1038 	int i;
   1039 
   1040 	while (*ids) {
   1041 		if (ad->Valid & ACPI_VALID_HID) {
   1042 			if (pmatch(ad->HardwareId.Value, *ids, NULL) == 2)
   1043 				return 1;
   1044 		}
   1045 
   1046 		if (ad->Valid & ACPI_VALID_CID) {
   1047 			for (i = 0; i < ad->CompatibilityId.Count; i++) {
   1048 				if (pmatch(ad->CompatibilityId.Id[i].Value, *ids, NULL) == 2)
   1049 					return 1;
   1050 			}
   1051 		}
   1052 		ids++;
   1053 	}
   1054 
   1055 	return 0;
   1056 }
   1057 
   1058 /*
   1059  * acpi_set_wake_gpe
   1060  *
   1061  *	Set GPE as both Runtime and Wake
   1062  */
   1063 void
   1064 acpi_set_wake_gpe(ACPI_HANDLE handle)
   1065 {
   1066 	ACPI_BUFFER buf;
   1067 	ACPI_STATUS rv;
   1068 	ACPI_OBJECT *p, *elt;
   1069 
   1070 	rv = acpi_eval_struct(handle, METHOD_NAME__PRW, &buf);
   1071 	if (ACPI_FAILURE(rv))
   1072 		return;			/* just ignore */
   1073 
   1074 	p = buf.Pointer;
   1075 	if (p->Type != ACPI_TYPE_PACKAGE || p->Package.Count < 2)
   1076 		goto out;		/* just ignore */
   1077 
   1078 	elt = p->Package.Elements;
   1079 
   1080 	/* TBD: package support */
   1081 	AcpiSetGpeType(NULL, elt[0].Integer.Value, ACPI_GPE_TYPE_WAKE_RUN);
   1082 	AcpiEnableGpe(NULL, elt[0].Integer.Value, ACPI_NOT_ISR);
   1083 
   1084  out:
   1085 	AcpiOsFree(buf.Pointer);
   1086 }
   1087 
   1088 
   1089 /*****************************************************************************
   1090  * ACPI sleep support.
   1091  *****************************************************************************/
   1092 
   1093 static int
   1094 is_available_state(struct acpi_softc *sc, int state)
   1095 {
   1096 	UINT8 type_a, type_b;
   1097 
   1098 	return ACPI_SUCCESS(AcpiGetSleepTypeData((UINT8)state,
   1099 				&type_a, &type_b));
   1100 }
   1101 
   1102 /*
   1103  * acpi_enter_sleep_state:
   1104  *
   1105  *	enter to the specified sleep state.
   1106  */
   1107 
   1108 ACPI_STATUS
   1109 acpi_enter_sleep_state(struct acpi_softc *sc, int state)
   1110 {
   1111 	int s, err;
   1112 	ACPI_STATUS ret = AE_OK;
   1113 
   1114 	if (state == acpi_sleepstate)
   1115 		return AE_OK;
   1116 
   1117 	aprint_normal("%s: entering state %d\n", sc->sc_dev.dv_xname, state);
   1118 
   1119 	switch (state) {
   1120 	case ACPI_STATE_S0:
   1121 		break;
   1122 	case ACPI_STATE_S1:
   1123 	case ACPI_STATE_S2:
   1124 	case ACPI_STATE_S3:
   1125 	case ACPI_STATE_S4:
   1126 		if (!is_available_state(sc, state)) {
   1127 			aprint_error("%s: cannot enter the sleep state (%d).\n",
   1128 			    sc->sc_dev.dv_xname, state);
   1129 			break;
   1130 		}
   1131 
   1132 		if (state != ACPI_STATE_S1)
   1133 			pnp_global_transition(PNP_STATE_D3);
   1134 
   1135 		ret = AcpiEnterSleepStatePrep(state);
   1136 		if (ACPI_FAILURE(ret)) {
   1137 			aprint_error("%s: failed preparing to sleep (%s)\n",
   1138 			    sc->sc_dev.dv_xname, AcpiFormatException(ret));
   1139 			break;
   1140 		}
   1141 
   1142 		DELAY(1000000);
   1143 		acpi_sleepstate = state;
   1144 		if (state == ACPI_STATE_S1) {
   1145 			/* just enter the state */
   1146 			acpi_md_OsDisableInterrupt();
   1147 			AcpiEnterSleepState((UINT8)state);
   1148 		} else {
   1149 			s = splhigh();
   1150 			err = acpi_md_sleep(state);
   1151 			if (err == 0 && state == ACPI_STATE_S3)
   1152 				AcpiClearEvent(ACPI_EVENT_POWER_BUTTON);
   1153 			splx(s);
   1154 			if (state == ACPI_STATE_S4)
   1155 				AcpiEnable();
   1156 		}
   1157 		AcpiLeaveSleepState((UINT8)state);
   1158 		if (state != ACPI_STATE_S1)
   1159 			pnp_global_transition(PNP_STATE_D0);
   1160 
   1161 		break;
   1162 	case ACPI_STATE_S5:
   1163 		ret = AcpiEnterSleepStatePrep(ACPI_STATE_S5);
   1164 		if (ACPI_FAILURE(ret)) {
   1165 			aprint_error("%s: failed preparing to sleep (%s)\n",
   1166 			       sc->sc_dev.dv_xname, AcpiFormatException(ret));
   1167 			break;
   1168 		}
   1169 		DELAY(1000000);
   1170 		acpi_sleepstate = state;
   1171 		acpi_md_OsDisableInterrupt();
   1172 		AcpiEnterSleepState(ACPI_STATE_S5);
   1173 		aprint_error("%s: WARNING powerdown failed!\n",
   1174 		    sc->sc_dev.dv_xname);
   1175 		break;
   1176 	}
   1177 
   1178 	acpi_sleepstate = ACPI_STATE_S0;
   1179 	return ret;
   1180 }
   1181 
   1182 #if defined(ACPI_ACTIVATE_DEV)
   1183 /* XXX This very incomplete */
   1184 ACPI_STATUS
   1185 acpi_allocate_resources(ACPI_HANDLE handle)
   1186 {
   1187 	ACPI_BUFFER bufp, bufc, bufn;
   1188 	ACPI_RESOURCE *resp, *resc, *resn;
   1189 	ACPI_RESOURCE_IRQ *irq;
   1190 	ACPI_RESOURCE_EXTENDED_IRQ *xirq;
   1191 	ACPI_STATUS rv;
   1192 	uint delta;
   1193 
   1194 	rv = acpi_get(handle, &bufp, AcpiGetPossibleResources);
   1195 	if (ACPI_FAILURE(rv))
   1196 		goto out;
   1197 	rv = acpi_get(handle, &bufc, AcpiGetCurrentResources);
   1198 	if (ACPI_FAILURE(rv)) {
   1199 		goto out1;
   1200 	}
   1201 
   1202 	bufn.Length = 1000;
   1203 	bufn.Pointer = resn = malloc(bufn.Length, M_ACPI, M_WAITOK);
   1204 	resp = bufp.Pointer;
   1205 	resc = bufc.Pointer;
   1206 	while (resc->Type != ACPI_RESOURCE_TYPE_END_TAG &&
   1207 	       resp->Type != ACPI_RESOURCE_TYPE_END_TAG) {
   1208 		while (resc->Type != resp->Type && resp->Type != ACPI_RESOURCE_TYPE_END_TAG)
   1209 			resp = ACPI_NEXT_RESOURCE(resp);
   1210 		if (resp->Type == ACPI_RESOURCE_TYPE_END_TAG)
   1211 			break;
   1212 		/* Found identical Id */
   1213 		resn->Type = resc->Type;
   1214 		switch (resc->Type) {
   1215 		case ACPI_RESOURCE_TYPE_IRQ:
   1216 			memcpy(&resn->Data, &resp->Data,
   1217 			       sizeof(ACPI_RESOURCE_IRQ));
   1218 			irq = (ACPI_RESOURCE_IRQ *)&resn->Data;
   1219 			irq->Interrupts[0] =
   1220 			    ((ACPI_RESOURCE_IRQ *)&resp->Data)->
   1221 			        Interrupts[irq->InterruptCount-1];
   1222 			irq->InterruptCount = 1;
   1223 			resn->Length = ACPI_RS_SIZE(ACPI_RESOURCE_IRQ);
   1224 			break;
   1225 		case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
   1226 			memcpy(&resn->Data, &resp->Data,
   1227 			       sizeof(ACPI_RESOURCE_EXTENDED_IRQ));
   1228 			xirq = (ACPI_RESOURCE_EXTENDED_IRQ *)&resn->Data;
   1229 #if 0
   1230 			/*
   1231 			 * XXX not duplicating the interrupt logic above
   1232 			 * because its not clear what it accomplishes.
   1233 			 */
   1234 			xirq->Interrupts[0] =
   1235 			    ((ACPI_RESOURCE_EXT_IRQ *)&resp->Data)->
   1236 			    Interrupts[irq->NumberOfInterrupts-1];
   1237 			xirq->NumberOfInterrupts = 1;
   1238 #endif
   1239 			resn->Length = ACPI_RS_SIZE(ACPI_RESOURCE_EXTENDED_IRQ);
   1240 			break;
   1241 		case ACPI_RESOURCE_TYPE_IO:
   1242 			memcpy(&resn->Data, &resp->Data,
   1243 			       sizeof(ACPI_RESOURCE_IO));
   1244 			resn->Length = resp->Length;
   1245 			break;
   1246 		default:
   1247 			printf("acpi_allocate_resources: res=%d\n", resc->Type);
   1248 			rv = AE_BAD_DATA;
   1249 			goto out2;
   1250 		}
   1251 		resc = ACPI_NEXT_RESOURCE(resc);
   1252 		resn = ACPI_NEXT_RESOURCE(resn);
   1253 		resp = ACPI_NEXT_RESOURCE(resp);
   1254 		delta = (UINT8 *)resn - (UINT8 *)bufn.Pointer;
   1255 		if (delta >=
   1256 		    bufn.Length-ACPI_RS_SIZE(ACPI_RESOURCE_DATA)) {
   1257 			bufn.Length *= 2;
   1258 			bufn.Pointer = realloc(bufn.Pointer, bufn.Length,
   1259 					       M_ACPI, M_WAITOK);
   1260 			resn = (ACPI_RESOURCE *)((UINT8 *)bufn.Pointer + delta);
   1261 		}
   1262 	}
   1263 	if (resc->Type != ACPI_RESOURCE_TYPE_END_TAG) {
   1264 		printf("acpi_allocate_resources: resc not exhausted\n");
   1265 		rv = AE_BAD_DATA;
   1266 		goto out3;
   1267 	}
   1268 
   1269 	resn->Type = ACPI_RESOURCE_TYPE_END_TAG;
   1270 	rv = AcpiSetCurrentResources(handle, &bufn);
   1271 	if (ACPI_FAILURE(rv)) {
   1272 		printf("acpi_allocate_resources: AcpiSetCurrentResources %s\n",
   1273 		       AcpiFormatException(rv));
   1274 	}
   1275 
   1276 out3:
   1277 	free(bufn.Pointer, M_ACPI);
   1278 out2:
   1279 	AcpiOsFree(bufc.Pointer);
   1280 out1:
   1281 	AcpiOsFree(bufp.Pointer);
   1282 out:
   1283 	return rv;
   1284 }
   1285 #endif /* ACPI_ACTIVATE_DEV */
   1286 
   1287 SYSCTL_SETUP(sysctl_acpi_setup, "sysctl hw.acpi subtree setup")
   1288 {
   1289 	const struct sysctlnode *node;
   1290 	const struct sysctlnode *ssnode;
   1291 
   1292 	if (sysctl_createv(clog, 0, NULL, NULL,
   1293 	    CTLFLAG_PERMANENT,
   1294 	    CTLTYPE_NODE, "hw", NULL,
   1295 	    NULL, 0, NULL, 0,
   1296 	    CTL_HW, CTL_EOL) != 0)
   1297 		return;
   1298 
   1299 	if (sysctl_createv(clog, 0, NULL, &node,
   1300 	    CTLFLAG_PERMANENT,
   1301 	    CTLTYPE_NODE, "acpi", NULL,
   1302 	    NULL, 0, NULL, 0,
   1303 	    CTL_HW, CTL_CREATE, CTL_EOL) != 0)
   1304 		return;
   1305 
   1306 	acpi_node = node->sysctl_num;
   1307 
   1308 	/* ACPI sleepstate sysctl */
   1309 	if (sysctl_createv(NULL, 0, NULL, &node,
   1310 	    CTLFLAG_PERMANENT,
   1311 	    CTLTYPE_NODE, "machdep", NULL,
   1312 	    NULL, 0, NULL, 0, CTL_MACHDEP, CTL_EOL) != 0)
   1313 		return;
   1314 	if (sysctl_createv(NULL, 0, &node, &ssnode,
   1315 	    CTLFLAG_READWRITE, CTLTYPE_INT, "sleep_state",
   1316 	    NULL, sysctl_hw_acpi_sleepstate, 0, NULL, 0, CTL_CREATE,
   1317 	    CTL_EOL) != 0)
   1318 		return;
   1319 }
   1320 
   1321 static int
   1322 sysctl_hw_acpi_sleepstate(SYSCTLFN_ARGS)
   1323 {
   1324 	int error, t;
   1325 	struct sysctlnode node;
   1326 
   1327 	node = *rnode;
   1328 	t = acpi_sleepstate;
   1329 	node.sysctl_data = &t;
   1330 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1331 	if (error || newp == NULL)
   1332 		return error;
   1333 
   1334 	if (acpi_softc == NULL)
   1335 		return ENOSYS;
   1336 
   1337 	acpi_enter_sleep_state(acpi_softc, t);
   1338 
   1339 	return 0;
   1340 }
   1341