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