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