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