Home | History | Annotate | Line # | Download | only in acpi
acpi.c revision 1.157
      1 /*	$NetBSD: acpi.c,v 1.157 2010/03/09 18:15:21 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.157 2010/03/09 18:15:21 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 		if (type != ACPI_TYPE_DEVICE)
    898 			return AE_OK;
    899 
    900 		if ((ad->ad_devinfo->Valid & ACPI_VALID_HID) == 0)
    901 			return AE_OK;
    902 
    903 #ifdef ACPIVERBOSE
    904 		aprint_normal("       HID %s\n",
    905 		    ad->ad_devinfo->HardwareId.String);
    906 
    907 		if (ad->ad_devinfo->Valid & ACPI_VALID_UID)
    908 			aprint_normal("       UID %s\n",
    909 			    ad->ad_devinfo->UniqueId.String);
    910 
    911 		if (ad->ad_devinfo->Valid & ACPI_VALID_ADR)
    912 			aprint_normal("       ADR 0x%016" PRIx64 "\n",
    913 			    ad->ad_devinfo->Address);
    914 
    915 		if (ad->ad_devinfo->Valid & ACPI_VALID_STA)
    916 			aprint_normal("       STA 0x%08x\n",
    917 			    ad->ad_devinfo->CurrentStatus);
    918 #endif
    919 	}
    920 
    921 	return AE_OK;
    922 }
    923 
    924 /*
    925  * acpi_print:
    926  *
    927  *	Autoconfiguration print routine for ACPI node bus.
    928  */
    929 static int
    930 acpi_print(void *aux, const char *pnp)
    931 {
    932 	struct acpi_attach_args *aa = aux;
    933 	ACPI_STATUS rv;
    934 
    935 	if (pnp) {
    936 		if (aa->aa_node->ad_devinfo->Valid & ACPI_VALID_HID) {
    937 			char *pnpstr =
    938 			    aa->aa_node->ad_devinfo->HardwareId.String;
    939 			ACPI_BUFFER buf;
    940 
    941 			aprint_normal("%s (%s) ", aa->aa_node->ad_name,
    942 			    pnpstr);
    943 
    944 			rv = acpi_eval_struct(aa->aa_node->ad_handle,
    945 			    "_STR", &buf);
    946 			if (ACPI_SUCCESS(rv)) {
    947 				ACPI_OBJECT *obj = buf.Pointer;
    948 				switch (obj->Type) {
    949 				case ACPI_TYPE_STRING:
    950 					aprint_normal("[%s] ", obj->String.Pointer);
    951 					break;
    952 				case ACPI_TYPE_BUFFER:
    953 					aprint_normal("buffer %p ", obj->Buffer.Pointer);
    954 					break;
    955 				default:
    956 					aprint_normal("type %u ",obj->Type);
    957 					break;
    958 				}
    959 				ACPI_FREE(buf.Pointer);
    960 			}
    961 #ifdef ACPIVERBOSE
    962 			else {
    963 				int i;
    964 
    965 				for (i = 0; i < __arraycount(acpi_knowndevs);
    966 				    i++) {
    967 					if (strcmp(acpi_knowndevs[i].pnp,
    968 					    pnpstr) == 0) {
    969 						aprint_normal("[%s] ",
    970 						    acpi_knowndevs[i].str);
    971 					}
    972 				}
    973 			}
    974 
    975 #endif
    976 			aprint_normal("at %s", pnp);
    977 		} else if (aa->aa_node->ad_devinfo->Type != ACPI_TYPE_DEVICE) {
    978 			aprint_normal("%s (ACPI Object Type '%s' "
    979 			    "[0x%02x]) ", aa->aa_node->ad_name,
    980 			     AcpiUtGetTypeName(aa->aa_node->ad_devinfo->Type),
    981 			     aa->aa_node->ad_devinfo->Type);
    982 			aprint_normal("at %s", pnp);
    983 		} else
    984 			return 0;
    985 	} else {
    986 		aprint_normal(" (%s", aa->aa_node->ad_name);
    987 		if (aa->aa_node->ad_devinfo->Valid & ACPI_VALID_HID) {
    988 			aprint_normal(", %s", aa->aa_node->ad_devinfo->HardwareId.String);
    989 			if (aa->aa_node->ad_devinfo->Valid & ACPI_VALID_UID) {
    990 				const char *uid;
    991 
    992 				uid = aa->aa_node->ad_devinfo->UniqueId.String;
    993 				if (uid[0] == '\0')
    994 					uid = "<null>";
    995 				aprint_normal("-%s", uid);
    996 			}
    997 		}
    998 		aprint_normal(")");
    999 	}
   1000 
   1001 	return UNCONF;
   1002 }
   1003 
   1004 /*****************************************************************************
   1005  * ACPI fixed-hardware feature handlers
   1006  *****************************************************************************/
   1007 
   1008 static UINT32	acpi_fixed_button_handler(void *);
   1009 static void	acpi_fixed_button_pressed(void *);
   1010 
   1011 /*
   1012  * acpi_enable_fixed_events:
   1013  *
   1014  *	Enable any fixed-hardware feature handlers.
   1015  */
   1016 static void
   1017 acpi_enable_fixed_events(struct acpi_softc *sc)
   1018 {
   1019 	static int beenhere;
   1020 	ACPI_STATUS rv;
   1021 
   1022 	KASSERT(beenhere == 0);
   1023 	beenhere = 1;
   1024 
   1025 	/*
   1026 	 * Check for fixed-hardware buttons.
   1027 	 */
   1028 	if ((AcpiGbl_FADT.Flags & ACPI_FADT_POWER_BUTTON) == 0) {
   1029 		aprint_verbose_dev(sc->sc_dev,
   1030 		    "fixed-feature power button present\n");
   1031 		sc->sc_smpsw_power.smpsw_name = device_xname(sc->sc_dev);
   1032 		sc->sc_smpsw_power.smpsw_type = PSWITCH_TYPE_POWER;
   1033 		if (sysmon_pswitch_register(&sc->sc_smpsw_power) != 0) {
   1034 			aprint_error_dev(sc->sc_dev,
   1035 			    "unable to register fixed power "
   1036 			    "button with sysmon\n");
   1037 		} else {
   1038 			rv = AcpiInstallFixedEventHandler(
   1039 			    ACPI_EVENT_POWER_BUTTON,
   1040 			    acpi_fixed_button_handler, &sc->sc_smpsw_power);
   1041 			if (ACPI_FAILURE(rv)) {
   1042 				aprint_error_dev(sc->sc_dev,
   1043 				    "unable to install handler "
   1044 				    "for fixed power button: %s\n",
   1045 				    AcpiFormatException(rv));
   1046 			}
   1047 		}
   1048 	}
   1049 
   1050 	if ((AcpiGbl_FADT.Flags & ACPI_FADT_SLEEP_BUTTON) == 0) {
   1051 		aprint_verbose_dev(sc->sc_dev,
   1052 		    "fixed-feature sleep button present\n");
   1053 		sc->sc_smpsw_sleep.smpsw_name = device_xname(sc->sc_dev);
   1054 		sc->sc_smpsw_sleep.smpsw_type = PSWITCH_TYPE_SLEEP;
   1055 		if (sysmon_pswitch_register(&sc->sc_smpsw_power) != 0) {
   1056 			aprint_error_dev(sc->sc_dev,
   1057 			    "unable to register fixed sleep "
   1058 			    "button with sysmon\n");
   1059 		} else {
   1060 			rv = AcpiInstallFixedEventHandler(
   1061 			    ACPI_EVENT_SLEEP_BUTTON,
   1062 			    acpi_fixed_button_handler, &sc->sc_smpsw_sleep);
   1063 			if (ACPI_FAILURE(rv)) {
   1064 				aprint_error_dev(sc->sc_dev,
   1065 				    "unable to install handler "
   1066 				    "for fixed sleep button: %s\n",
   1067 				    AcpiFormatException(rv));
   1068 			}
   1069 		}
   1070 	}
   1071 }
   1072 
   1073 /*
   1074  * acpi_fixed_button_handler:
   1075  *
   1076  *	Event handler for the fixed buttons.
   1077  */
   1078 static UINT32
   1079 acpi_fixed_button_handler(void *context)
   1080 {
   1081 	static const int handler = OSL_NOTIFY_HANDLER;
   1082 	struct sysmon_pswitch *smpsw = context;
   1083 
   1084 	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "%s\n", __func__));
   1085 
   1086 	(void)AcpiOsExecute(handler, acpi_fixed_button_pressed, smpsw);
   1087 
   1088 	return ACPI_INTERRUPT_HANDLED;
   1089 }
   1090 
   1091 /*
   1092  * acpi_fixed_button_pressed:
   1093  *
   1094  *	Deal with a fixed button being pressed.
   1095  */
   1096 static void
   1097 acpi_fixed_button_pressed(void *context)
   1098 {
   1099 	struct sysmon_pswitch *smpsw = context;
   1100 
   1101 	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "%s: %s fixed button pressed\n",
   1102 		__func__, smpsw->smpsw_name));
   1103 
   1104 	sysmon_pswitch_event(smpsw, PSWITCH_EVENT_PRESSED);
   1105 }
   1106 
   1107 /*****************************************************************************
   1108  * ACPI utility routines.
   1109  *****************************************************************************/
   1110 
   1111 /*
   1112  * acpi_eval_integer:
   1113  *
   1114  *	Evaluate an integer object.
   1115  */
   1116 ACPI_STATUS
   1117 acpi_eval_integer(ACPI_HANDLE handle, const char *path, ACPI_INTEGER *valp)
   1118 {
   1119 	ACPI_STATUS rv;
   1120 	ACPI_BUFFER buf;
   1121 	ACPI_OBJECT param;
   1122 
   1123 	if (handle == NULL)
   1124 		handle = ACPI_ROOT_OBJECT;
   1125 
   1126 	buf.Pointer = &param;
   1127 	buf.Length = sizeof(param);
   1128 
   1129 	rv = AcpiEvaluateObjectTyped(handle, path, NULL, &buf,
   1130 	    ACPI_TYPE_INTEGER);
   1131 	if (ACPI_SUCCESS(rv))
   1132 		*valp = param.Integer.Value;
   1133 
   1134 	return rv;
   1135 }
   1136 
   1137 /*
   1138  * acpi_eval_set_integer:
   1139  *
   1140  *	Evaluate an integer object with a single integer input parameter.
   1141  */
   1142 ACPI_STATUS
   1143 acpi_eval_set_integer(ACPI_HANDLE handle, const char *path, ACPI_INTEGER arg)
   1144 {
   1145 	ACPI_OBJECT param_arg;
   1146 	ACPI_OBJECT_LIST param_args;
   1147 
   1148 	if (handle == NULL)
   1149 		handle = ACPI_ROOT_OBJECT;
   1150 
   1151 	param_arg.Type = ACPI_TYPE_INTEGER;
   1152 	param_arg.Integer.Value = arg;
   1153 
   1154 	param_args.Count = 1;
   1155 	param_args.Pointer = &param_arg;
   1156 
   1157 	return AcpiEvaluateObject(handle, path, &param_args, NULL);
   1158 }
   1159 
   1160 /*
   1161  * acpi_eval_string:
   1162  *
   1163  *	Evaluate a (Unicode) string object.
   1164  */
   1165 ACPI_STATUS
   1166 acpi_eval_string(ACPI_HANDLE handle, const char *path, char **stringp)
   1167 {
   1168 	ACPI_OBJECT *obj;
   1169 	ACPI_BUFFER buf;
   1170 	ACPI_STATUS rv;
   1171 
   1172 	rv = acpi_eval_struct(handle, path, &buf);
   1173 
   1174 	if (ACPI_FAILURE(rv))
   1175 		return rv;
   1176 
   1177 	obj = buf.Pointer;
   1178 
   1179 	if (obj->Type != ACPI_TYPE_STRING) {
   1180 		rv = AE_TYPE;
   1181 		goto out;
   1182 	}
   1183 
   1184 	if (obj->String.Length == 0) {
   1185 		rv = AE_BAD_DATA;
   1186 		goto out;
   1187 	}
   1188 
   1189 	*stringp = ACPI_ALLOCATE(obj->String.Length + 1);
   1190 
   1191 	if (*stringp == NULL) {
   1192 		rv = AE_NO_MEMORY;
   1193 		goto out;
   1194 	}
   1195 
   1196 	(void)memcpy(*stringp, obj->String.Pointer, obj->String.Length);
   1197 
   1198 	(*stringp)[obj->String.Length] = '\0';
   1199 
   1200 out:
   1201 	ACPI_FREE(buf.Pointer);
   1202 
   1203 	return rv;
   1204 }
   1205 
   1206 /*
   1207  * acpi_eval_struct:
   1208  *
   1209  *	Evaluate a more complex structure.
   1210  *	Caller must free buf.Pointer by ACPI_FREE().
   1211  */
   1212 ACPI_STATUS
   1213 acpi_eval_struct(ACPI_HANDLE handle, const char *path, ACPI_BUFFER *bufp)
   1214 {
   1215 	ACPI_STATUS rv;
   1216 
   1217 	if (handle == NULL)
   1218 		handle = ACPI_ROOT_OBJECT;
   1219 
   1220 	bufp->Pointer = NULL;
   1221 	bufp->Length = ACPI_ALLOCATE_LOCAL_BUFFER;
   1222 
   1223 	rv = AcpiEvaluateObject(handle, path, NULL, bufp);
   1224 
   1225 	return rv;
   1226 }
   1227 
   1228 /*
   1229  * acpi_eval_reference_handle:
   1230  *
   1231  *	Evaluate a reference handle from an element in a package.
   1232  */
   1233 ACPI_STATUS
   1234 acpi_eval_reference_handle(ACPI_OBJECT *elm, ACPI_HANDLE *handle)
   1235 {
   1236 
   1237 	if (elm == NULL || handle == NULL)
   1238 		return AE_BAD_PARAMETER;
   1239 
   1240 	switch (elm->Type) {
   1241 
   1242 	case ACPI_TYPE_ANY:
   1243 	case ACPI_TYPE_LOCAL_REFERENCE:
   1244 
   1245 		if (elm->Reference.Handle == NULL)
   1246 			return AE_NULL_ENTRY;
   1247 
   1248 		*handle = elm->Reference.Handle;
   1249 
   1250 		return AE_OK;
   1251 
   1252 	case ACPI_TYPE_STRING:
   1253 		return AcpiGetHandle(NULL, elm->String.Pointer, handle);
   1254 
   1255 	default:
   1256 		return AE_TYPE;
   1257 	}
   1258 }
   1259 
   1260 /*
   1261  * acpi_foreach_package_object:
   1262  *
   1263  *	Iterate over all objects in a package, and pass them all
   1264  *	to a function. If the called function returns non AE_OK, the
   1265  *	iteration is stopped and that value is returned.
   1266  */
   1267 ACPI_STATUS
   1268 acpi_foreach_package_object(ACPI_OBJECT *pkg,
   1269     ACPI_STATUS (*func)(ACPI_OBJECT *, void *),
   1270     void *arg)
   1271 {
   1272 	ACPI_STATUS rv = AE_OK;
   1273 	int i;
   1274 
   1275 	if (pkg == NULL || pkg->Type != ACPI_TYPE_PACKAGE)
   1276 		return AE_BAD_PARAMETER;
   1277 
   1278 	for (i = 0; i < pkg->Package.Count; i++) {
   1279 		rv = (*func)(&pkg->Package.Elements[i], arg);
   1280 		if (ACPI_FAILURE(rv))
   1281 			break;
   1282 	}
   1283 
   1284 	return rv;
   1285 }
   1286 
   1287 /*
   1288  * acpi_name:
   1289  *
   1290  *	Return a complete pathname from a handle.
   1291  *
   1292  *	Note that the function uses static data storage;
   1293  *	if the data is needed for future use, it should be
   1294  *	copied before any subsequent calls overwrite it.
   1295  */
   1296 const char *
   1297 acpi_name(ACPI_HANDLE handle)
   1298 {
   1299 	static char buffer[80];
   1300 	ACPI_BUFFER buf;
   1301 	ACPI_STATUS rv;
   1302 
   1303 	buf.Length = sizeof(buffer);
   1304 	buf.Pointer = buffer;
   1305 
   1306 	rv = AcpiGetName(handle, ACPI_FULL_PATHNAME, &buf);
   1307 	if (ACPI_FAILURE(rv))
   1308 		return "(unknown acpi path)";
   1309 	return buffer;
   1310 }
   1311 
   1312 /*
   1313  * acpi_get:
   1314  *
   1315  *	Fetch data info the specified (empty) ACPI buffer.
   1316  *	Caller must free buf.Pointer by ACPI_FREE().
   1317  */
   1318 ACPI_STATUS
   1319 acpi_get(ACPI_HANDLE handle, ACPI_BUFFER *buf,
   1320     ACPI_STATUS (*getit)(ACPI_HANDLE, ACPI_BUFFER *))
   1321 {
   1322 	buf->Pointer = NULL;
   1323 	buf->Length = ACPI_ALLOCATE_LOCAL_BUFFER;
   1324 
   1325 	return (*getit)(handle, buf);
   1326 }
   1327 
   1328 
   1329 /*
   1330  * acpi_match_hid
   1331  *
   1332  *	Match given ids against _HID and _CIDs.
   1333  */
   1334 int
   1335 acpi_match_hid(ACPI_DEVICE_INFO *ad, const char * const *ids)
   1336 {
   1337 	int i;
   1338 
   1339 	while (*ids) {
   1340 		if (ad->Valid & ACPI_VALID_HID) {
   1341 			if (pmatch(ad->HardwareId.String, *ids, NULL) == 2)
   1342 				return 1;
   1343 		}
   1344 
   1345 		if (ad->Valid & ACPI_VALID_CID) {
   1346 			for (i = 0; i < ad->CompatibleIdList.Count; i++) {
   1347 				if (pmatch(ad->CompatibleIdList.Ids[i].String, *ids, NULL) == 2)
   1348 					return 1;
   1349 			}
   1350 		}
   1351 		ids++;
   1352 	}
   1353 
   1354 	return 0;
   1355 }
   1356 
   1357 /*
   1358  * acpi_wake_gpe_helper
   1359  *
   1360  *	Set/unset GPE as both Runtime and Wake.
   1361  */
   1362 static void
   1363 acpi_wake_gpe_helper(ACPI_HANDLE handle, bool enable)
   1364 {
   1365 	ACPI_OBJECT *elm, *obj;
   1366 	ACPI_INTEGER val;
   1367 	ACPI_BUFFER buf;
   1368 	ACPI_STATUS rv;
   1369 
   1370 	rv = acpi_eval_struct(handle, METHOD_NAME__PRW, &buf);
   1371 
   1372 	if (ACPI_FAILURE(rv))
   1373 		return;
   1374 
   1375 	obj = buf.Pointer;
   1376 
   1377 	if (obj->Type != ACPI_TYPE_PACKAGE || obj->Package.Count < 2)
   1378 		goto out;
   1379 
   1380 	/*
   1381 	 * As noted in ACPI 3.0 (section 7.2.10), the _PRW object is
   1382 	 * a package in which the first element is either an integer
   1383 	 * or again a package. In the latter case the package inside
   1384 	 * the package element has two elements, a reference handle
   1385 	 * and the GPE number.
   1386 	 */
   1387 	elm = &obj->Package.Elements[0];
   1388 
   1389 	switch (elm->Type) {
   1390 
   1391 	case ACPI_TYPE_INTEGER:
   1392 		val = elm->Integer.Value;
   1393 		break;
   1394 
   1395 	case ACPI_TYPE_PACKAGE:
   1396 
   1397 		if (elm->Package.Count < 2)
   1398 			goto out;
   1399 
   1400 		if (elm->Package.Elements[0].Type != ACPI_TYPE_LOCAL_REFERENCE)
   1401 			goto out;
   1402 
   1403 		if (elm->Package.Elements[1].Type != ACPI_TYPE_INTEGER)
   1404 			goto out;
   1405 
   1406 		val = elm->Package.Elements[1].Integer.Value;
   1407 		break;
   1408 
   1409 	default:
   1410 		goto out;
   1411 	}
   1412 
   1413 	if (enable) {
   1414 		(void)AcpiSetGpeType(NULL, val, ACPI_GPE_TYPE_WAKE_RUN);
   1415 		(void)AcpiEnableGpe(NULL, val, ACPI_NOT_ISR);
   1416 	} else
   1417 		(void)AcpiDisableGpe(NULL, val, ACPI_NOT_ISR);
   1418 
   1419 out:
   1420 	ACPI_FREE(buf.Pointer);
   1421 }
   1422 
   1423 /*
   1424  * acpi_clear_wake_gpe
   1425  *
   1426  *	Clear GPE as both Runtime and Wake.
   1427  */
   1428 void
   1429 acpi_clear_wake_gpe(ACPI_HANDLE handle)
   1430 {
   1431 	acpi_wake_gpe_helper(handle, false);
   1432 }
   1433 
   1434 /*
   1435  * acpi_set_wake_gpe
   1436  *
   1437  *	Set GPE as both Runtime and Wake.
   1438  */
   1439 void
   1440 acpi_set_wake_gpe(ACPI_HANDLE handle)
   1441 {
   1442 	acpi_wake_gpe_helper(handle, true);
   1443 }
   1444 
   1445 
   1446 /*****************************************************************************
   1447  * ACPI sleep support.
   1448  *****************************************************************************/
   1449 
   1450 static int
   1451 is_available_state(struct acpi_softc *sc, int state)
   1452 {
   1453 	UINT8 type_a, type_b;
   1454 
   1455 	return ACPI_SUCCESS(AcpiGetSleepTypeData((UINT8)state,
   1456 				&type_a, &type_b));
   1457 }
   1458 
   1459 /*
   1460  * acpi_enter_sleep_state:
   1461  *
   1462  *	Enter to the specified sleep state.
   1463  */
   1464 
   1465 ACPI_STATUS
   1466 acpi_enter_sleep_state(struct acpi_softc *sc, int state)
   1467 {
   1468 	int err;
   1469 	ACPI_STATUS ret = AE_OK;
   1470 
   1471 	if (state == acpi_sleepstate)
   1472 		return AE_OK;
   1473 
   1474 	aprint_normal_dev(sc->sc_dev, "entering state %d\n", state);
   1475 
   1476 	switch (state) {
   1477 	case ACPI_STATE_S0:
   1478 		break;
   1479 	case ACPI_STATE_S1:
   1480 	case ACPI_STATE_S2:
   1481 	case ACPI_STATE_S3:
   1482 	case ACPI_STATE_S4:
   1483 		if (!is_available_state(sc, state)) {
   1484 			aprint_error_dev(sc->sc_dev,
   1485 			    "ACPI S%d not available on this platform\n", state);
   1486 			break;
   1487 		}
   1488 
   1489 		acpi_wakedev_commit(sc, state);
   1490 
   1491 		if (state != ACPI_STATE_S1 && !pmf_system_suspend(PMF_Q_NONE)) {
   1492 			aprint_error_dev(sc->sc_dev, "aborting suspend\n");
   1493 			break;
   1494 		}
   1495 
   1496 		ret = AcpiEnterSleepStatePrep(state);
   1497 		if (ACPI_FAILURE(ret)) {
   1498 			aprint_error_dev(sc->sc_dev,
   1499 			    "failed preparing to sleep (%s)\n",
   1500 			    AcpiFormatException(ret));
   1501 			break;
   1502 		}
   1503 
   1504 		acpi_sleepstate = state;
   1505 		if (state == ACPI_STATE_S1) {
   1506 			/* just enter the state */
   1507 			acpi_md_OsDisableInterrupt();
   1508 			ret = AcpiEnterSleepState((UINT8)state);
   1509 			if (ACPI_FAILURE(ret))
   1510 				aprint_error_dev(sc->sc_dev,
   1511 				    "failed to enter sleep state S1: %s\n",
   1512 				    AcpiFormatException(ret));
   1513 			AcpiLeaveSleepState((UINT8)state);
   1514 		} else {
   1515 			err = acpi_md_sleep(state);
   1516 			if (state == ACPI_STATE_S4)
   1517 				AcpiEnable();
   1518 			pmf_system_bus_resume(PMF_Q_NONE);
   1519 			AcpiLeaveSleepState((UINT8)state);
   1520 			pmf_system_resume(PMF_Q_NONE);
   1521 		}
   1522 
   1523 		break;
   1524 	case ACPI_STATE_S5:
   1525 		ret = AcpiEnterSleepStatePrep(ACPI_STATE_S5);
   1526 		if (ACPI_FAILURE(ret)) {
   1527 			aprint_error_dev(sc->sc_dev,
   1528 			    "failed preparing to sleep (%s)\n",
   1529 			    AcpiFormatException(ret));
   1530 			break;
   1531 		}
   1532 		DELAY(1000000);
   1533 		acpi_sleepstate = state;
   1534 		acpi_md_OsDisableInterrupt();
   1535 		AcpiEnterSleepState(ACPI_STATE_S5);
   1536 		aprint_error_dev(sc->sc_dev, "WARNING powerdown failed!\n");
   1537 		break;
   1538 	}
   1539 
   1540 	acpi_sleepstate = ACPI_STATE_S0;
   1541 	return ret;
   1542 }
   1543 
   1544 #if defined(ACPI_ACTIVATE_DEV)
   1545 /* XXX This very incomplete */
   1546 ACPI_STATUS
   1547 acpi_allocate_resources(ACPI_HANDLE handle)
   1548 {
   1549 	ACPI_BUFFER bufp, bufc, bufn;
   1550 	ACPI_RESOURCE *resp, *resc, *resn;
   1551 	ACPI_RESOURCE_IRQ *irq;
   1552 	ACPI_RESOURCE_EXTENDED_IRQ *xirq;
   1553 	ACPI_STATUS rv;
   1554 	uint delta;
   1555 
   1556 	rv = acpi_get(handle, &bufp, AcpiGetPossibleResources);
   1557 	if (ACPI_FAILURE(rv))
   1558 		goto out;
   1559 	rv = acpi_get(handle, &bufc, AcpiGetCurrentResources);
   1560 	if (ACPI_FAILURE(rv)) {
   1561 		goto out1;
   1562 	}
   1563 
   1564 	bufn.Length = 1000;
   1565 	bufn.Pointer = resn = malloc(bufn.Length, M_ACPI, M_WAITOK);
   1566 	resp = bufp.Pointer;
   1567 	resc = bufc.Pointer;
   1568 	while (resc->Type != ACPI_RESOURCE_TYPE_END_TAG &&
   1569 	       resp->Type != ACPI_RESOURCE_TYPE_END_TAG) {
   1570 		while (resc->Type != resp->Type && resp->Type != ACPI_RESOURCE_TYPE_END_TAG)
   1571 			resp = ACPI_NEXT_RESOURCE(resp);
   1572 		if (resp->Type == ACPI_RESOURCE_TYPE_END_TAG)
   1573 			break;
   1574 		/* Found identical Id */
   1575 		resn->Type = resc->Type;
   1576 		switch (resc->Type) {
   1577 		case ACPI_RESOURCE_TYPE_IRQ:
   1578 			memcpy(&resn->Data, &resp->Data,
   1579 			       sizeof(ACPI_RESOURCE_IRQ));
   1580 			irq = (ACPI_RESOURCE_IRQ *)&resn->Data;
   1581 			irq->Interrupts[0] =
   1582 			    ((ACPI_RESOURCE_IRQ *)&resp->Data)->
   1583 			        Interrupts[irq->InterruptCount-1];
   1584 			irq->InterruptCount = 1;
   1585 			resn->Length = ACPI_RS_SIZE(ACPI_RESOURCE_IRQ);
   1586 			break;
   1587 		case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
   1588 			memcpy(&resn->Data, &resp->Data,
   1589 			       sizeof(ACPI_RESOURCE_EXTENDED_IRQ));
   1590 			xirq = (ACPI_RESOURCE_EXTENDED_IRQ *)&resn->Data;
   1591 #if 0
   1592 			/*
   1593 			 * XXX not duplicating the interrupt logic above
   1594 			 * because its not clear what it accomplishes.
   1595 			 */
   1596 			xirq->Interrupts[0] =
   1597 			    ((ACPI_RESOURCE_EXT_IRQ *)&resp->Data)->
   1598 			    Interrupts[irq->NumberOfInterrupts-1];
   1599 			xirq->NumberOfInterrupts = 1;
   1600 #endif
   1601 			resn->Length = ACPI_RS_SIZE(ACPI_RESOURCE_EXTENDED_IRQ);
   1602 			break;
   1603 		case ACPI_RESOURCE_TYPE_IO:
   1604 			memcpy(&resn->Data, &resp->Data,
   1605 			       sizeof(ACPI_RESOURCE_IO));
   1606 			resn->Length = resp->Length;
   1607 			break;
   1608 		default:
   1609 			printf("acpi_allocate_resources: res=%u\n", resc->Type);
   1610 			rv = AE_BAD_DATA;
   1611 			goto out2;
   1612 		}
   1613 		resc = ACPI_NEXT_RESOURCE(resc);
   1614 		resn = ACPI_NEXT_RESOURCE(resn);
   1615 		resp = ACPI_NEXT_RESOURCE(resp);
   1616 		delta = (UINT8 *)resn - (UINT8 *)bufn.Pointer;
   1617 		if (delta >=
   1618 		    bufn.Length-ACPI_RS_SIZE(ACPI_RESOURCE_DATA)) {
   1619 			bufn.Length *= 2;
   1620 			bufn.Pointer = realloc(bufn.Pointer, bufn.Length,
   1621 					       M_ACPI, M_WAITOK);
   1622 			resn = (ACPI_RESOURCE *)((UINT8 *)bufn.Pointer + delta);
   1623 		}
   1624 	}
   1625 	if (resc->Type != ACPI_RESOURCE_TYPE_END_TAG) {
   1626 		printf("acpi_allocate_resources: resc not exhausted\n");
   1627 		rv = AE_BAD_DATA;
   1628 		goto out3;
   1629 	}
   1630 
   1631 	resn->Type = ACPI_RESOURCE_TYPE_END_TAG;
   1632 	rv = AcpiSetCurrentResources(handle, &bufn);
   1633 	if (ACPI_FAILURE(rv)) {
   1634 		printf("acpi_allocate_resources: AcpiSetCurrentResources %s\n",
   1635 		       AcpiFormatException(rv));
   1636 	}
   1637 
   1638 out3:
   1639 	free(bufn.Pointer, M_ACPI);
   1640 out2:
   1641 	ACPI_FREE(bufc.Pointer);
   1642 out1:
   1643 	ACPI_FREE(bufp.Pointer);
   1644 out:
   1645 	return rv;
   1646 }
   1647 #endif /* ACPI_ACTIVATE_DEV */
   1648 
   1649 SYSCTL_SETUP(sysctl_acpi_setup, "sysctl hw.acpi subtree setup")
   1650 {
   1651 	const struct sysctlnode *node;
   1652 	const struct sysctlnode *ssnode;
   1653 
   1654 	if (sysctl_createv(clog, 0, NULL, NULL,
   1655 	    CTLFLAG_PERMANENT,
   1656 	    CTLTYPE_NODE, "hw", NULL,
   1657 	    NULL, 0, NULL, 0,
   1658 	    CTL_HW, CTL_EOL) != 0)
   1659 		return;
   1660 
   1661 	if (sysctl_createv(clog, 0, NULL, &node,
   1662 	    CTLFLAG_PERMANENT,
   1663 	    CTLTYPE_NODE, "acpi", NULL,
   1664 	    NULL, 0, NULL, 0,
   1665 	    CTL_HW, CTL_CREATE, CTL_EOL) != 0)
   1666 		return;
   1667 
   1668 	sysctl_createv(NULL, 0, NULL, NULL, CTLFLAG_READONLY,
   1669 	    CTLTYPE_QUAD, "root",
   1670 	    SYSCTL_DESCR("ACPI root pointer"),
   1671 	    NULL, 0, &acpi_root_pointer, sizeof(acpi_root_pointer),
   1672 	    CTL_HW, node->sysctl_num, CTL_CREATE, CTL_EOL);
   1673 	sysctl_createv(NULL, 0, NULL, NULL, CTLFLAG_READONLY,
   1674 	    CTLTYPE_STRING, "supported_states",
   1675 	    SYSCTL_DESCR("Supported ACPI system states"),
   1676 	    NULL, 0, acpi_supported_states, 0,
   1677 	    CTL_HW, node->sysctl_num, CTL_CREATE, CTL_EOL);
   1678 
   1679 	/* ACPI sleepstate sysctl */
   1680 	if (sysctl_createv(NULL, 0, NULL, &node,
   1681 	    CTLFLAG_PERMANENT,
   1682 	    CTLTYPE_NODE, "machdep", NULL,
   1683 	    NULL, 0, NULL, 0, CTL_MACHDEP, CTL_EOL) != 0)
   1684 		return;
   1685 	if (sysctl_createv(NULL, 0, &node, &ssnode,
   1686 	    CTLFLAG_READWRITE, CTLTYPE_INT, "sleep_state",
   1687 	    NULL, sysctl_hw_acpi_sleepstate, 0, NULL, 0, CTL_CREATE,
   1688 	    CTL_EOL) != 0)
   1689 		return;
   1690 }
   1691 
   1692 static int
   1693 sysctl_hw_acpi_sleepstate(SYSCTLFN_ARGS)
   1694 {
   1695 	int error, t;
   1696 	struct sysctlnode node;
   1697 
   1698 	node = *rnode;
   1699 	t = acpi_sleepstate;
   1700 	node.sysctl_data = &t;
   1701 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1702 	if (error || newp == NULL)
   1703 		return error;
   1704 
   1705 	if (acpi_softc == NULL)
   1706 		return ENOSYS;
   1707 
   1708 	acpi_enter_sleep_state(acpi_softc, t);
   1709 
   1710 	return 0;
   1711 }
   1712 
   1713 static ACPI_TABLE_HEADER *
   1714 acpi_map_rsdt(void)
   1715 {
   1716 	ACPI_PHYSICAL_ADDRESS paddr;
   1717 	ACPI_TABLE_RSDP *rsdp;
   1718 
   1719 	paddr = AcpiOsGetRootPointer();
   1720 	if (paddr == 0) {
   1721 		printf("ACPI: couldn't get root pointer\n");
   1722 		return NULL;
   1723 	}
   1724 	rsdp = AcpiOsMapMemory(paddr, sizeof(ACPI_TABLE_RSDP));
   1725 	if (rsdp == NULL) {
   1726 		printf("ACPI: couldn't map RSDP\n");
   1727 		return NULL;
   1728 	}
   1729 	if (rsdp->Revision > 1 && rsdp->XsdtPhysicalAddress)
   1730 		paddr = (ACPI_PHYSICAL_ADDRESS)rsdp->XsdtPhysicalAddress;
   1731 	else
   1732 		paddr = (ACPI_PHYSICAL_ADDRESS)rsdp->RsdtPhysicalAddress;
   1733 	AcpiOsUnmapMemory(rsdp, sizeof(ACPI_TABLE_RSDP));
   1734 
   1735 	return AcpiOsMapMemory(paddr, sizeof(ACPI_TABLE_HEADER));
   1736 }
   1737 
   1738 static void
   1739 acpi_unmap_rsdt(ACPI_TABLE_HEADER *rsdt)
   1740 {
   1741 	if (rsdt == NULL)
   1742 		return;
   1743 
   1744 	AcpiOsUnmapMemory(rsdt, sizeof(ACPI_TABLE_HEADER));
   1745 }
   1746