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