Home | History | Annotate | Line # | Download | only in acpi
acpi.c revision 1.89
      1 /*	$NetBSD: acpi.c,v 1.89 2006/06/20 12:31:19 cube Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2003 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.89 2006/06/20 12:31:19 cube 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/kernel.h>
     90 #include <sys/proc.h>
     91 #include <sys/sysctl.h>
     92 
     93 #include <dev/acpi/acpica.h>
     94 #include <dev/acpi/acpireg.h>
     95 #include <dev/acpi/acpivar.h>
     96 #include <dev/acpi/acpi_osd.h>
     97 #ifdef ACPIVERBOSE
     98 #include <dev/acpi/acpidevs_data.h>
     99 #endif
    100 
    101 #if defined(ACPI_PCI_FIXUP)
    102 #error The option ACPI_PCI_FIXUP has been obsoleted by PCI_INTR_FIXUP.  Please adjust your kernel configuration file.
    103 #endif
    104 
    105 #ifdef PCI_INTR_FIXUP
    106 #include <dev/pci/pcidevs.h>
    107 #endif
    108 
    109 MALLOC_DECLARE(M_ACPI);
    110 
    111 #include <machine/acpi_machdep.h>
    112 
    113 #ifdef ACPI_DEBUGGER
    114 #define	ACPI_DBGR_INIT		0x01
    115 #define	ACPI_DBGR_TABLES	0x02
    116 #define	ACPI_DBGR_ENABLE	0x04
    117 #define	ACPI_DBGR_PROBE		0x08
    118 #define	ACPI_DBGR_RUNNING	0x10
    119 
    120 static int acpi_dbgr = 0x00;
    121 #endif
    122 
    123 static int	acpi_match(struct device *, struct cfdata *, void *);
    124 static void	acpi_attach(struct device *, struct device *, void *);
    125 
    126 static int	acpi_print(void *aux, const char *);
    127 
    128 static int	sysctl_hw_acpi_sleepstate(SYSCTLFN_ARGS);
    129 
    130 extern struct cfdriver acpi_cd;
    131 
    132 CFATTACH_DECL(acpi, sizeof(struct acpi_softc),
    133     acpi_match, acpi_attach, NULL, NULL);
    134 
    135 /*
    136  * This is a flag we set when the ACPI subsystem is active.  Machine
    137  * dependent code may wish to skip other steps (such as attaching
    138  * subsystems that ACPI supercedes) when ACPI is active.
    139  */
    140 int	acpi_active;
    141 
    142 /*
    143  * Pointer to the ACPI subsystem's state.  There can be only
    144  * one ACPI instance.
    145  */
    146 struct acpi_softc *acpi_softc;
    147 
    148 /*
    149  * Locking stuff.
    150  */
    151 static struct simplelock acpi_slock;
    152 static int acpi_locked;
    153 
    154 /*
    155  * sysctl-related information
    156  */
    157 
    158 static int acpi_node = CTL_EOL;
    159 static uint64_t acpi_root_pointer;	/* found as hw.acpi.root */
    160 static int acpi_sleepstate = ACPI_STATE_S0;
    161 
    162 /*
    163  * Prototypes.
    164  */
    165 static void		acpi_shutdown(void *);
    166 static void		acpi_build_tree(struct acpi_softc *);
    167 static ACPI_STATUS	acpi_make_devnode(ACPI_HANDLE, UINT32, void *, void **);
    168 
    169 static void		acpi_enable_fixed_events(struct acpi_softc *);
    170 #ifdef PCI_INTR_FIXUP
    171 void			acpi_pci_fixup(struct acpi_softc *);
    172 #endif
    173 #if defined(PCI_INTR_FIXUP) || defined(ACPI_ACTIVATE_DEV)
    174 static ACPI_STATUS	acpi_allocate_resources(ACPI_HANDLE handle);
    175 #endif
    176 
    177 /*
    178  * acpi_probe:
    179  *
    180  *	Probe for ACPI support.  This is called by the
    181  *	machine-dependent ACPI front-end.  All of the
    182  *	actual work is done by ACPICA.
    183  *
    184  *	NOTE: This is not an autoconfiguration interface function.
    185  */
    186 int
    187 acpi_probe(void)
    188 {
    189 	static int beenhere;
    190 	ACPI_STATUS rv;
    191 
    192 	if (beenhere != 0)
    193 		panic("acpi_probe: ACPI has already been probed");
    194 	beenhere = 1;
    195 
    196 	simple_lock_init(&acpi_slock);
    197 	acpi_locked = 0;
    198 
    199 	/*
    200 	 * Start up ACPICA.
    201 	 */
    202 #ifdef ACPI_DEBUGGER
    203 	if (acpi_dbgr & ACPI_DBGR_INIT)
    204 		acpi_osd_debugger();
    205 #endif
    206 
    207 	rv = AcpiInitializeSubsystem();
    208 	if (ACPI_FAILURE(rv)) {
    209 		printf("ACPI: unable to initialize ACPICA: %s\n",
    210 		    AcpiFormatException(rv));
    211 		return 0;
    212 	}
    213 
    214 #ifdef ACPI_DEBUGGER
    215 	if (acpi_dbgr & ACPI_DBGR_TABLES)
    216 		acpi_osd_debugger();
    217 #endif
    218 
    219 	rv = AcpiLoadTables();
    220 	if (ACPI_FAILURE(rv)) {
    221 		printf("ACPI: unable to load tables: %s\n",
    222 		    AcpiFormatException(rv));
    223 		return 0;
    224 	}
    225 
    226 	/*
    227 	 * Looks like we have ACPI!
    228 	 */
    229 
    230 	return 1;
    231 }
    232 
    233 ACPI_STATUS
    234 acpi_OsGetRootPointer(UINT32 Flags, ACPI_POINTER *PhysicalAddress)
    235 {
    236 	ACPI_STATUS rv;
    237 
    238 	/*
    239 	 * IA-32: Use AcpiFindRootPointer() to locate the RSDP.
    240 	 *
    241 	 * IA-64: Use the EFI.
    242 	 *
    243 	 * We let MD code handle this since there are multiple
    244 	 * ways to do it.
    245 	 */
    246 
    247 	rv = acpi_md_OsGetRootPointer(Flags, PhysicalAddress);
    248 
    249 	if (acpi_root_pointer == 0 && ACPI_SUCCESS(rv))
    250 		acpi_root_pointer =
    251 		    (uint64_t)PhysicalAddress->Pointer.Physical;
    252 
    253 	return rv;
    254 }
    255 
    256 /*
    257  * acpi_match:
    258  *
    259  *	Autoconfiguration `match' routine.
    260  */
    261 static int
    262 acpi_match(struct device *parent, struct cfdata *match, void *aux)
    263 {
    264 	/*
    265 	 * XXX Check other locators?  Hard to know -- machine
    266 	 * dependent code has already checked for the presence
    267 	 * of ACPI by calling acpi_probe(), so I suppose we
    268 	 * don't really have to do anything else.
    269 	 */
    270 	return 1;
    271 }
    272 
    273 /*
    274  * acpi_attach:
    275  *
    276  *	Autoconfiguration `attach' routine.  Finish initializing
    277  *	ACPICA (some initialization was done in acpi_probe(),
    278  *	which was required to check for the presence of ACPI),
    279  *	and enable the ACPI subsystem.
    280  */
    281 static void
    282 acpi_attach(struct device *parent, struct device *self, void *aux)
    283 {
    284 	struct acpi_softc *sc = (void *) self;
    285 	struct acpibus_attach_args *aa = aux;
    286 	ACPI_STATUS rv;
    287 
    288 	aprint_naive(": Advanced Configuration and Power Interface\n");
    289 	aprint_normal(": Advanced Configuration and Power Interface\n");
    290 
    291 	if (acpi_softc != NULL)
    292 		panic("acpi_attach: ACPI has already been attached");
    293 
    294 	sysmon_power_settype("acpi");
    295 
    296 	aprint_verbose("%s: using Intel ACPI CA subsystem version %08x\n",
    297 	    sc->sc_dev.dv_xname, ACPI_CA_VERSION);
    298 
    299 	aprint_verbose("%s: X/RSDT: OemId <%6.6s,%8.8s,%08x>, AslId <%4.4s,%08x>\n",
    300 	    sc->sc_dev.dv_xname,
    301 	    AcpiGbl_XSDT->OemId, AcpiGbl_XSDT->OemTableId,
    302 	    AcpiGbl_XSDT->OemRevision,
    303 	    AcpiGbl_XSDT->AslCompilerId, AcpiGbl_XSDT->AslCompilerRevision);
    304 
    305 	sc->sc_quirks = acpi_find_quirks();
    306 
    307 	sc->sc_iot = aa->aa_iot;
    308 	sc->sc_memt = aa->aa_memt;
    309 	sc->sc_pc = aa->aa_pc;
    310 	sc->sc_pciflags = aa->aa_pciflags;
    311 	sc->sc_ic = aa->aa_ic;
    312 
    313 	acpi_softc = sc;
    314 
    315 	/*
    316 	 * Bring ACPI on-line.
    317 	 */
    318 #ifdef ACPI_DEBUGGER
    319 	if (acpi_dbgr & ACPI_DBGR_ENABLE)
    320 		acpi_osd_debugger();
    321 #endif
    322 
    323 	rv = AcpiEnableSubsystem(0);
    324 	if (ACPI_FAILURE(rv)) {
    325 		aprint_error("%s: unable to enable ACPI: %s\n",
    326 		    sc->sc_dev.dv_xname, AcpiFormatException(rv));
    327 		return;
    328 	}
    329 
    330 	/* early EC handler initialization if ECDT table is available */
    331 #if NACPIEC > 0
    332 	acpiec_early_attach(&sc->sc_dev);
    333 #endif
    334 
    335 	rv = AcpiInitializeObjects(0);
    336 	if (ACPI_FAILURE(rv)) {
    337 		aprint_error("%s: unable to initialize ACPI objects: %s\n",
    338 		    sc->sc_dev.dv_xname, AcpiFormatException(rv));
    339 		return;
    340 	}
    341 	acpi_active = 1;
    342 
    343 	/* Our current state is "awake". */
    344 	sc->sc_sleepstate = ACPI_STATE_S0;
    345 
    346 	/* Show SCI interrupt. */
    347 	if (AcpiGbl_FADT != NULL)
    348 		aprint_verbose("%s: SCI interrupting at int %d\n",
    349 		    sc->sc_dev.dv_xname, AcpiGbl_FADT->SciInt);
    350 	/*
    351 	 * Check for fixed-hardware features.
    352 	 */
    353 	acpi_enable_fixed_events(sc);
    354 
    355 	/*
    356 	 * Fix up PCI devices.
    357 	 */
    358 #ifdef PCI_INTR_FIXUP
    359 	if ((sc->sc_quirks & (ACPI_QUIRK_BADPCI | ACPI_QUIRK_BADIRQ)) == 0)
    360 		acpi_pci_fixup(sc);
    361 #endif
    362 
    363 	/*
    364 	 * Scan the namespace and build our device tree.
    365 	 */
    366 #ifdef ACPI_DEBUGGER
    367 	if (acpi_dbgr & ACPI_DBGR_PROBE)
    368 		acpi_osd_debugger();
    369 #endif
    370 	acpi_md_callback((struct device *)sc);
    371 	acpi_build_tree(sc);
    372 
    373 	if (acpi_root_pointer != 0 && acpi_node != CTL_EOL) {
    374 		(void)sysctl_createv(NULL, 0, NULL, NULL,
    375 		    CTLFLAG_IMMEDIATE,
    376 		    CTLTYPE_QUAD, "root", NULL, NULL,
    377 		    acpi_root_pointer, NULL, 0,
    378 		    CTL_HW, acpi_node, CTL_CREATE, CTL_EOL);
    379 	}
    380 
    381 
    382 	/*
    383 	 * Register a shutdown hook that disables certain ACPI
    384 	 * events that might happen and confuse us while we're
    385 	 * trying to shut down.
    386 	 */
    387 	sc->sc_sdhook = shutdownhook_establish(acpi_shutdown, sc);
    388 	if (sc->sc_sdhook == NULL)
    389 		aprint_error("%s: WARNING: unable to register shutdown hook\n",
    390 		    sc->sc_dev.dv_xname);
    391 
    392 #ifdef ACPI_DEBUGGER
    393 	if (acpi_dbgr & ACPI_DBGR_RUNNING)
    394 		acpi_osd_debugger();
    395 #endif
    396 }
    397 
    398 /*
    399  * acpi_shutdown:
    400  *
    401  *	Shutdown hook for ACPI -- disable some events that
    402  *	might confuse us.
    403  */
    404 static void
    405 acpi_shutdown(void *arg)
    406 {
    407 	/* nothing */
    408 }
    409 
    410 #if 0
    411 /*
    412  * acpi_disable:
    413  *
    414  *	Disable ACPI.
    415  */
    416 static ACPI_STATUS
    417 acpi_disable(struct acpi_softc *sc)
    418 {
    419 	ACPI_STATUS rv = AE_OK;
    420 
    421 	if (acpi_active) {
    422 		rv = AcpiDisable();
    423 		if (ACPI_SUCCESS(rv))
    424 			acpi_active = 0;
    425 	}
    426 	return rv;
    427 }
    428 #endif
    429 
    430 struct acpi_make_devnode_state {
    431 	struct acpi_softc *softc;
    432 	struct acpi_scope *scope;
    433 };
    434 
    435 /*
    436  * acpi_build_tree:
    437  *
    438  *	Scan relevant portions of the ACPI namespace and attach
    439  *	child devices.
    440  */
    441 static void
    442 acpi_build_tree(struct acpi_softc *sc)
    443 {
    444 	static const char *scopes[] = {
    445 		"\\_PR_",	/* ACPI 1.0 processor namespace */
    446 		"\\_SB_",	/* system bus namespace */
    447 		"\\_SI_",	/* system idicator namespace */
    448 		"\\_TZ_",	/* ACPI 1.0 thermal zone namespace */
    449 		NULL,
    450 	};
    451 	struct acpi_attach_args aa;
    452 	struct acpi_make_devnode_state state;
    453 	struct acpi_scope *as;
    454 	struct acpi_devnode *ad;
    455 	ACPI_HANDLE parent;
    456 	ACPI_STATUS rv;
    457 	int i;
    458 
    459 	TAILQ_INIT(&sc->sc_scopes);
    460 
    461 	state.softc = sc;
    462 
    463 	/*
    464 	 * Scan the namespace and build our tree.
    465 	 */
    466 	for (i = 0; scopes[i] != NULL; i++) {
    467 		as = malloc(sizeof(*as), M_ACPI, M_WAITOK);
    468 		as->as_name = scopes[i];
    469 		TAILQ_INIT(&as->as_devnodes);
    470 
    471 		TAILQ_INSERT_TAIL(&sc->sc_scopes, as, as_list);
    472 
    473 		state.scope = as;
    474 
    475 		rv = AcpiGetHandle(ACPI_ROOT_OBJECT, scopes[i],
    476 		    &parent);
    477 		if (ACPI_SUCCESS(rv)) {
    478 			AcpiWalkNamespace(ACPI_TYPE_ANY, parent, 100,
    479 			    acpi_make_devnode, &state, NULL);
    480 		}
    481 
    482 		/* Now, for this namespace, try and attach the devices. */
    483 		TAILQ_FOREACH(ad, &as->as_devnodes, ad_list) {
    484 			aa.aa_node = ad;
    485 			aa.aa_iot = sc->sc_iot;
    486 			aa.aa_memt = sc->sc_memt;
    487 			aa.aa_pc = sc->sc_pc;
    488 			aa.aa_pciflags = sc->sc_pciflags;
    489 			aa.aa_ic = sc->sc_ic;
    490 
    491 			if (ad->ad_devinfo->Type == ACPI_TYPE_DEVICE) {
    492 				/*
    493 				 * XXX We only attach devices which are:
    494 				 *
    495 				 *	- present
    496 				 *	- enabled
    497 				 *	- functioning properly
    498 				 *
    499 				 * However, if enabled, it's decoding resources,
    500 				 * so we should claim them, if possible.
    501 				 * Requires changes to bus_space(9).
    502 				 */
    503 				if ((ad->ad_devinfo->Valid & ACPI_VALID_STA) ==
    504 				    ACPI_VALID_STA &&
    505 				    (ad->ad_devinfo->CurrentStatus &
    506 				     (ACPI_STA_DEV_PRESENT|ACPI_STA_DEV_ENABLED|
    507 				      ACPI_STA_DEV_OK)) !=
    508 				    (ACPI_STA_DEV_PRESENT|ACPI_STA_DEV_ENABLED|
    509 				     ACPI_STA_DEV_OK))
    510 					continue;
    511 
    512 				/*
    513 				 * XXX Same problem as above...
    514 				 */
    515 				if ((ad->ad_devinfo->Valid & ACPI_VALID_HID)
    516 				    == 0)
    517 					continue;
    518 			}
    519 
    520 			ad->ad_device = config_found(&sc->sc_dev,
    521 			    &aa, acpi_print);
    522 		}
    523 	}
    524 }
    525 
    526 #ifdef ACPI_ACTIVATE_DEV
    527 static void
    528 acpi_activate_device(ACPI_HANDLE handle, ACPI_DEVICE_INFO **di)
    529 {
    530 	ACPI_STATUS rv;
    531 	ACPI_BUFFER buf;
    532 
    533 	buf.Pointer = NULL;
    534 	buf.Length = ACPI_ALLOCATE_BUFFER;
    535 
    536 #ifdef ACPI_DEBUG
    537 	aprint_normal("acpi_activate_device: %s, old status=%x\n",
    538 	       (*di)->HardwareId.Value, (*di)->CurrentStatus);
    539 #endif
    540 
    541 	rv = acpi_allocate_resources(handle);
    542 	if (ACPI_FAILURE(rv)) {
    543 		aprint_error("acpi: activate failed for %s\n",
    544 		       (*di)->HardwareId.Value);
    545 	} else {
    546 		aprint_normal("acpi: activated %s\n", (*di)->HardwareId.Value);
    547 	}
    548 
    549 	(void)AcpiGetObjectInfo(handle, &buf);
    550 	AcpiOsFree(*di);
    551 	*di = buf.Pointer;
    552 
    553 #ifdef ACPI_DEBUG
    554 	aprint_normal("acpi_activate_device: %s, new status=%x\n",
    555 	       (*di)->HardwareId.Value, (*di)->CurrentStatus);
    556 #endif
    557 }
    558 #endif /* ACPI_ACTIVATE_DEV */
    559 
    560 /*
    561  * acpi_make_devnode:
    562  *
    563  *	Make an ACPI devnode.
    564  */
    565 static ACPI_STATUS
    566 acpi_make_devnode(ACPI_HANDLE handle, UINT32 level, void *context,
    567     void **status)
    568 {
    569 	struct acpi_make_devnode_state *state = context;
    570 #if defined(ACPI_DEBUG) || defined(ACPI_EXTRA_DEBUG)
    571 	struct acpi_softc *sc = state->softc;
    572 #endif
    573 	struct acpi_scope *as = state->scope;
    574 	struct acpi_devnode *ad;
    575 	ACPI_OBJECT_TYPE type;
    576 	ACPI_BUFFER buf;
    577 	ACPI_DEVICE_INFO *devinfo;
    578 	ACPI_STATUS rv;
    579 
    580 	rv = AcpiGetType(handle, &type);
    581 	if (ACPI_SUCCESS(rv)) {
    582 		buf.Pointer = NULL;
    583 		buf.Length = ACPI_ALLOCATE_BUFFER;
    584 		rv = AcpiGetObjectInfo(handle, &buf);
    585 		if (ACPI_FAILURE(rv)) {
    586 #ifdef ACPI_DEBUG
    587 			aprint_normal("%s: AcpiGetObjectInfo failed: %s\n",
    588 			    sc->sc_dev.dv_xname, AcpiFormatException(rv));
    589 #endif
    590 			goto out; /* XXX why return OK */
    591 		}
    592 
    593 		devinfo = buf.Pointer;
    594 
    595 		switch (type) {
    596 		case ACPI_TYPE_DEVICE:
    597 #ifdef ACPI_ACTIVATE_DEV
    598 			if ((devinfo->Valid & (ACPI_VALID_STA|ACPI_VALID_HID)) ==
    599 			    (ACPI_VALID_STA|ACPI_VALID_HID) &&
    600 			    (devinfo->CurrentStatus &
    601 			     (ACPI_STA_DEV_PRESENT|ACPI_STA_DEV_ENABLED)) ==
    602 			    ACPI_STA_DEV_PRESENT)
    603 				acpi_activate_device(handle, &devinfo);
    604 
    605 			/* FALLTHROUGH */
    606 #endif
    607 
    608 		case ACPI_TYPE_PROCESSOR:
    609 		case ACPI_TYPE_THERMAL:
    610 		case ACPI_TYPE_POWER:
    611 			ad = malloc(sizeof(*ad), M_ACPI, M_NOWAIT|M_ZERO);
    612 			if (ad == NULL)
    613 				return AE_NO_MEMORY;
    614 
    615 			ad->ad_devinfo = devinfo;
    616 			ad->ad_handle = handle;
    617 			ad->ad_level = level;
    618 			ad->ad_scope = as;
    619 			ad->ad_type = type;
    620 
    621 			TAILQ_INSERT_TAIL(&as->as_devnodes, ad, ad_list);
    622 
    623 			if ((ad->ad_devinfo->Valid & ACPI_VALID_HID) == 0)
    624 				goto out;
    625 
    626 #ifdef ACPI_EXTRA_DEBUG
    627 			aprint_normal("%s: HID %s found in scope %s level %d\n",
    628 			    sc->sc_dev.dv_xname,
    629 			    ad->ad_devinfo->HardwareId.Value,
    630 			    as->as_name, ad->ad_level);
    631 			if (ad->ad_devinfo->Valid & ACPI_VALID_UID)
    632 				aprint_normal("       UID %s\n",
    633 				    ad->ad_devinfo->UniqueId.Value);
    634 			if (ad->ad_devinfo->Valid & ACPI_VALID_ADR)
    635 				aprint_normal("       ADR 0x%016qx\n",
    636 				    ad->ad_devinfo->Address);
    637 			if (ad->ad_devinfo->Valid & ACPI_VALID_STA)
    638 				aprint_normal("       STA 0x%08x\n",
    639 				    ad->ad_devinfo->CurrentStatus);
    640 #endif
    641 		}
    642 	}
    643  out:
    644 	return AE_OK;
    645 }
    646 
    647 /*
    648  * acpi_print:
    649  *
    650  *	Autoconfiguration print routine.
    651  */
    652 static int
    653 acpi_print(void *aux, const char *pnp)
    654 {
    655 	struct acpi_attach_args *aa = aux;
    656 	ACPI_STATUS rv;
    657 
    658 	if (pnp) {
    659 		if (aa->aa_node->ad_devinfo->Valid & ACPI_VALID_HID) {
    660 			char *pnpstr =
    661 			    aa->aa_node->ad_devinfo->HardwareId.Value;
    662 			char *str;
    663 
    664 			aprint_normal("%s ", pnpstr);
    665 			rv = acpi_eval_string(aa->aa_node->ad_handle,
    666 			    "_STR", &str);
    667 			if (ACPI_SUCCESS(rv)) {
    668 				aprint_normal("[%s] ", str);
    669 				AcpiOsFree(str);
    670 			}
    671 #ifdef ACPIVERBOSE
    672 			else {
    673 				int i;
    674 
    675 				for (i = 0; i < sizeof(acpi_knowndevs) /
    676 				    sizeof(acpi_knowndevs[0]); i++) {
    677 					if (strcmp(acpi_knowndevs[i].pnp,
    678 					    pnpstr) == 0) {
    679 						aprint_normal("[%s] ",
    680 						    acpi_knowndevs[i].str);
    681 					}
    682 				}
    683 			}
    684 
    685 #endif
    686 		} else {
    687 			aprint_normal("ACPI Object Type '%s' (0x%02x) ",
    688 			   AcpiUtGetTypeName(aa->aa_node->ad_devinfo->Type),
    689 			   aa->aa_node->ad_devinfo->Type);
    690 		}
    691 		aprint_normal("at %s", pnp);
    692 	} else {
    693 		if (aa->aa_node->ad_devinfo->Valid & ACPI_VALID_HID) {
    694 			aprint_normal(" (%s", aa->aa_node->ad_devinfo->HardwareId.Value);
    695 			if (aa->aa_node->ad_devinfo->Valid & ACPI_VALID_UID) {
    696 				const char *uid;
    697 
    698 				uid = aa->aa_node->ad_devinfo->UniqueId.Value;
    699 				if (uid[0] == '\0')
    700 					uid = "<null>";
    701 				aprint_normal("-%s", uid);
    702 			}
    703 			aprint_normal(")");
    704 		}
    705 	}
    706 
    707 	return UNCONF;
    708 }
    709 
    710 /*****************************************************************************
    711  * ACPI fixed-hardware feature handlers
    712  *****************************************************************************/
    713 
    714 static UINT32	acpi_fixed_button_handler(void *);
    715 static void	acpi_fixed_button_pressed(void *);
    716 
    717 /*
    718  * acpi_enable_fixed_events:
    719  *
    720  *	Enable any fixed-hardware feature handlers.
    721  */
    722 static void
    723 acpi_enable_fixed_events(struct acpi_softc *sc)
    724 {
    725 	static int beenhere;
    726 	ACPI_STATUS rv;
    727 
    728 	KASSERT(beenhere == 0);
    729 	beenhere = 1;
    730 
    731 	/*
    732 	 * Check for fixed-hardware buttons.
    733 	 */
    734 
    735 	if (AcpiGbl_FADT != NULL && AcpiGbl_FADT->PwrButton == 0) {
    736 		aprint_normal("%s: fixed-feature power button present\n",
    737 		    sc->sc_dev.dv_xname);
    738 		sc->sc_smpsw_power.smpsw_name = sc->sc_dev.dv_xname;
    739 		sc->sc_smpsw_power.smpsw_type = PSWITCH_TYPE_POWER;
    740 		if (sysmon_pswitch_register(&sc->sc_smpsw_power) != 0) {
    741 			aprint_error("%s: unable to register fixed power "
    742 			    "button with sysmon\n", sc->sc_dev.dv_xname);
    743 		} else {
    744 			rv = AcpiInstallFixedEventHandler(
    745 			    ACPI_EVENT_POWER_BUTTON,
    746 			    acpi_fixed_button_handler, &sc->sc_smpsw_power);
    747 			if (ACPI_FAILURE(rv)) {
    748 				aprint_error("%s: unable to install handler "
    749 				    "for fixed power button: %s\n",
    750 				    sc->sc_dev.dv_xname,
    751 				    AcpiFormatException(rv));
    752 			}
    753 		}
    754 	}
    755 
    756 	if (AcpiGbl_FADT != NULL && AcpiGbl_FADT->SleepButton == 0) {
    757 		aprint_normal("%s: fixed-feature sleep button present\n",
    758 		    sc->sc_dev.dv_xname);
    759 		sc->sc_smpsw_sleep.smpsw_name = sc->sc_dev.dv_xname;
    760 		sc->sc_smpsw_sleep.smpsw_type = PSWITCH_TYPE_SLEEP;
    761 		if (sysmon_pswitch_register(&sc->sc_smpsw_power) != 0) {
    762 			aprint_error("%s: unable to register fixed sleep "
    763 			    "button with sysmon\n", sc->sc_dev.dv_xname);
    764 		} else {
    765 			rv = AcpiInstallFixedEventHandler(
    766 			    ACPI_EVENT_SLEEP_BUTTON,
    767 			    acpi_fixed_button_handler, &sc->sc_smpsw_sleep);
    768 			if (ACPI_FAILURE(rv)) {
    769 				aprint_error("%s: unable to install handler "
    770 				    "for fixed sleep button: %s\n",
    771 				    sc->sc_dev.dv_xname,
    772 				    AcpiFormatException(rv));
    773 			}
    774 		}
    775 	}
    776 }
    777 
    778 /*
    779  * acpi_fixed_button_handler:
    780  *
    781  *	Event handler for the fixed buttons.
    782  */
    783 static UINT32
    784 acpi_fixed_button_handler(void *context)
    785 {
    786 	struct sysmon_pswitch *smpsw = context;
    787 	int rv;
    788 
    789 #ifdef ACPI_BUT_DEBUG
    790 	printf("%s: fixed button handler\n", smpsw->smpsw_name);
    791 #endif
    792 
    793 	rv = AcpiOsQueueForExecution(OSD_PRIORITY_LO,
    794 	    acpi_fixed_button_pressed, smpsw);
    795 	if (ACPI_FAILURE(rv))
    796 		printf("%s: WARNING: unable to queue fixed button pressed "
    797 		    "callback: %s\n", smpsw->smpsw_name,
    798 		    AcpiFormatException(rv));
    799 
    800 	return ACPI_INTERRUPT_HANDLED;
    801 }
    802 
    803 /*
    804  * acpi_fixed_button_pressed:
    805  *
    806  *	Deal with a fixed button being pressed.
    807  */
    808 static void
    809 acpi_fixed_button_pressed(void *context)
    810 {
    811 	struct sysmon_pswitch *smpsw = context;
    812 
    813 #ifdef ACPI_BUT_DEBUG
    814 	printf("%s: fixed button pressed, calling sysmon\n",
    815 	    smpsw->smpsw_name);
    816 #endif
    817 
    818 	sysmon_pswitch_event(smpsw, PSWITCH_EVENT_PRESSED);
    819 }
    820 
    821 /*****************************************************************************
    822  * ACPI utility routines.
    823  *****************************************************************************/
    824 
    825 /*
    826  * acpi_eval_integer:
    827  *
    828  *	Evaluate an integer object.
    829  */
    830 ACPI_STATUS
    831 acpi_eval_integer(ACPI_HANDLE handle, const char *path, ACPI_INTEGER *valp)
    832 {
    833 	ACPI_STATUS rv;
    834 	ACPI_BUFFER buf;
    835 	ACPI_OBJECT param;
    836 
    837 	if (handle == NULL)
    838 		handle = ACPI_ROOT_OBJECT;
    839 
    840 	buf.Pointer = &param;
    841 	buf.Length = sizeof(param);
    842 
    843 	rv = AcpiEvaluateObjectTyped(handle, path, NULL, &buf, ACPI_TYPE_INTEGER);
    844 	if (ACPI_SUCCESS(rv))
    845 		*valp = param.Integer.Value;
    846 
    847 	return rv;
    848 }
    849 
    850 /*
    851  * acpi_eval_string:
    852  *
    853  *	Evaluate a (Unicode) string object.
    854  */
    855 ACPI_STATUS
    856 acpi_eval_string(ACPI_HANDLE handle, const char *path, char **stringp)
    857 {
    858 	ACPI_STATUS rv;
    859 	ACPI_BUFFER buf;
    860 
    861 	if (handle == NULL)
    862 		handle = ACPI_ROOT_OBJECT;
    863 
    864 	buf.Pointer = NULL;
    865 	buf.Length = ACPI_ALLOCATE_BUFFER;
    866 
    867 	rv = AcpiEvaluateObjectTyped(handle, path, NULL, &buf, ACPI_TYPE_STRING);
    868 	if (ACPI_SUCCESS(rv)) {
    869 		ACPI_OBJECT *param = buf.Pointer;
    870 		const char *ptr = param->String.Pointer;
    871 		size_t len = param->String.Length;
    872 		if ((*stringp = AcpiOsAllocate(len)) == NULL)
    873 			rv = AE_NO_MEMORY;
    874 		else
    875 			(void)memcpy(*stringp, ptr, len);
    876 		AcpiOsFree(param);
    877 	}
    878 
    879 	return rv;
    880 }
    881 
    882 
    883 /*
    884  * acpi_eval_struct:
    885  *
    886  *	Evaluate a more complex structure.
    887  *	Caller must free buf.Pointer by AcpiOsFree().
    888  */
    889 ACPI_STATUS
    890 acpi_eval_struct(ACPI_HANDLE handle, const char *path, ACPI_BUFFER *bufp)
    891 {
    892 	ACPI_STATUS rv;
    893 
    894 	if (handle == NULL)
    895 		handle = ACPI_ROOT_OBJECT;
    896 
    897 	bufp->Pointer = NULL;
    898 	bufp->Length = ACPI_ALLOCATE_BUFFER;
    899 
    900 	rv = AcpiEvaluateObject(handle, path, NULL, bufp);
    901 
    902 	return rv;
    903 }
    904 
    905 /*
    906  * acpi_foreach_package_object:
    907  *
    908  *	Iterate over all objects in a in a packages and pass then all
    909  *	to a function. If the called function returns non AE_OK, the
    910  *	iteration is stopped and that value is returned.
    911  */
    912 
    913 ACPI_STATUS
    914 acpi_foreach_package_object(ACPI_OBJECT *pkg,
    915     ACPI_STATUS (*func)(ACPI_OBJECT *, void *),
    916     void *arg)
    917 {
    918 	ACPI_STATUS rv = AE_OK;
    919 	int i;
    920 
    921 	if (pkg == NULL || pkg->Type != ACPI_TYPE_PACKAGE)
    922 		return AE_BAD_PARAMETER;
    923 
    924 	for (i = 0; i < pkg->Package.Count; i++) {
    925 		rv = (*func)(&pkg->Package.Elements[i], arg);
    926 		if (ACPI_FAILURE(rv))
    927 			break;
    928 	}
    929 
    930 	return rv;
    931 }
    932 
    933 const char *
    934 acpi_name(ACPI_HANDLE handle)
    935 {
    936 	static char buffer[80];
    937 	ACPI_BUFFER buf;
    938 	ACPI_STATUS rv;
    939 
    940 	buf.Length = sizeof(buffer);
    941 	buf.Pointer = buffer;
    942 
    943 	rv = AcpiGetName(handle, ACPI_FULL_PATHNAME, &buf);
    944 	if (ACPI_FAILURE(rv))
    945 		return "(unknown acpi path)";
    946 	return buffer;
    947 }
    948 
    949 /*
    950  * acpi_get:
    951  *
    952  *	Fetch data info the specified (empty) ACPI buffer.
    953  *	Caller must free buf.Pointer by AcpiOsFree().
    954  */
    955 ACPI_STATUS
    956 acpi_get(ACPI_HANDLE handle, ACPI_BUFFER *buf,
    957     ACPI_STATUS (*getit)(ACPI_HANDLE, ACPI_BUFFER *))
    958 {
    959 	buf->Pointer = NULL;
    960 	buf->Length = ACPI_ALLOCATE_BUFFER;
    961 
    962 	return (*getit)(handle, buf);
    963 }
    964 
    965 
    966 /*
    967  * acpi_match_hid
    968  *
    969  *	Match given ids against _HID and _CIDs
    970  */
    971 int
    972 acpi_match_hid(ACPI_DEVICE_INFO *ad, const char * const *ids)
    973 {
    974 	int i;
    975 
    976 	while (*ids) {
    977 		if (ad->Valid & ACPI_VALID_HID) {
    978 			if (pmatch(ad->HardwareId.Value, *ids, NULL) == 2)
    979 				return 1;
    980 		}
    981 
    982 		if (ad->Valid & ACPI_VALID_CID) {
    983 			for (i = 0; i < ad->CompatibilityId.Count; i++) {
    984 				if (pmatch(ad->CompatibilityId.Id[i].Value, *ids, NULL) == 2)
    985 					return 1;
    986 			}
    987 		}
    988 		ids++;
    989 	}
    990 
    991 	return 0;
    992 }
    993 
    994 /*
    995  * acpi_set_wake_gpe
    996  *
    997  *	Set GPE as both Runtime and Wake
    998  */
    999 void
   1000 acpi_set_wake_gpe(ACPI_HANDLE handle)
   1001 {
   1002 	ACPI_BUFFER buf;
   1003 	ACPI_STATUS rv;
   1004 	ACPI_OBJECT *p, *elt;
   1005 
   1006 	rv = acpi_eval_struct(handle, METHOD_NAME__PRW, &buf);
   1007 	if (ACPI_FAILURE(rv))
   1008 		return;			/* just ignore */
   1009 
   1010 	p = buf.Pointer;
   1011 	if (p->Type != ACPI_TYPE_PACKAGE || p->Package.Count < 2)
   1012 		goto out;		/* just ignore */
   1013 
   1014 	elt = p->Package.Elements;
   1015 
   1016 	/* TBD: package support */
   1017 	AcpiSetGpeType(NULL, elt[0].Integer.Value, ACPI_GPE_TYPE_WAKE_RUN);
   1018 	AcpiEnableGpe(NULL, elt[0].Integer.Value, ACPI_NOT_ISR);
   1019 
   1020  out:
   1021 	AcpiOsFree(buf.Pointer);
   1022 }
   1023 
   1024 
   1025 /*****************************************************************************
   1026  * ACPI sleep support.
   1027  *****************************************************************************/
   1028 
   1029 static int
   1030 is_available_state(struct acpi_softc *sc, int state)
   1031 {
   1032 	UINT8 type_a, type_b;
   1033 
   1034 	return ACPI_SUCCESS(AcpiGetSleepTypeData((UINT8)state,
   1035 				&type_a, &type_b));
   1036 }
   1037 
   1038 /*
   1039  * acpi_enter_sleep_state:
   1040  *
   1041  *	enter to the specified sleep state.
   1042  */
   1043 
   1044 ACPI_STATUS
   1045 acpi_enter_sleep_state(struct acpi_softc *sc, int state)
   1046 {
   1047 	int s;
   1048 	ACPI_STATUS ret = AE_OK;
   1049 
   1050 	switch (state) {
   1051 	case ACPI_STATE_S0:
   1052 		break;
   1053 	case ACPI_STATE_S1:
   1054 	case ACPI_STATE_S2:
   1055 	case ACPI_STATE_S3:
   1056 	case ACPI_STATE_S4:
   1057 		if (!is_available_state(sc, state)) {
   1058 			printf("acpi: cannot enter the sleep state (%d).\n",
   1059 			       state);
   1060 			break;
   1061 		}
   1062 		ret = AcpiEnterSleepStatePrep(state);
   1063 		if (ACPI_FAILURE(ret)) {
   1064 			printf("acpi: failed preparing to sleep (%s)\n",
   1065 			       AcpiFormatException(ret));
   1066 			break;
   1067 		}
   1068 		if (state==ACPI_STATE_S1) {
   1069 			/* just enter the state */
   1070 			acpi_md_OsDisableInterrupt();
   1071 			AcpiEnterSleepState((UINT8)state);
   1072 			AcpiUtReleaseMutex(ACPI_MTX_HARDWARE);
   1073 		} else {
   1074 			/* XXX: powerhooks(9) framework is too poor to
   1075 			 * support ACPI sleep state...
   1076 			 */
   1077 			dopowerhooks(PWR_SOFTSUSPEND);
   1078 			s = splhigh();
   1079 			dopowerhooks(PWR_SUSPEND);
   1080 			acpi_md_sleep(state);
   1081 			dopowerhooks(PWR_RESUME);
   1082 			splx(s);
   1083 			dopowerhooks(PWR_SOFTRESUME);
   1084 			if (state==ACPI_STATE_S4)
   1085 				AcpiEnable();
   1086 		}
   1087 		AcpiLeaveSleepState((UINT8)state);
   1088 		break;
   1089 	case ACPI_STATE_S5:
   1090 		ret = AcpiEnterSleepStatePrep(ACPI_STATE_S5);
   1091 		if (ACPI_FAILURE(ret)) {
   1092 			printf("acpi: failed preparing to sleep (%s)\n",
   1093 			       AcpiFormatException(ret));
   1094 			break;
   1095 		}
   1096 		acpi_md_OsDisableInterrupt();
   1097 		AcpiEnterSleepState(ACPI_STATE_S5);
   1098 		printf("WARNING: powerdown failed!\n");
   1099 		break;
   1100 	}
   1101 
   1102 	return ret;
   1103 }
   1104 
   1105 #ifdef PCI_INTR_FIXUP
   1106 ACPI_STATUS acpi_pci_fixup_bus(ACPI_HANDLE, UINT32, void *, void **);
   1107 /*
   1108  * acpi_pci_fixup:
   1109  *
   1110  *	Set up PCI devices that BIOS didn't handle right.
   1111  *	Iterate through all devices and try to get the _PTR
   1112  *	(PCI Routing Table).  If it exists then make sure all
   1113  *	interrupt links that it uses are working.
   1114  */
   1115 void
   1116 acpi_pci_fixup(struct acpi_softc *sc)
   1117 {
   1118 	ACPI_HANDLE parent;
   1119 	ACPI_STATUS rv;
   1120 
   1121 #ifdef ACPI_DEBUG
   1122 	printf("acpi_pci_fixup starts:\n");
   1123 #endif
   1124 	rv = AcpiGetHandle(ACPI_ROOT_OBJECT, "\\_SB_", &parent);
   1125 	if (ACPI_FAILURE(rv))
   1126 		return;
   1127 	sc->sc_pci_bus = 0;
   1128 	AcpiWalkNamespace(ACPI_TYPE_DEVICE, parent, 100,
   1129 	    acpi_pci_fixup_bus, sc, NULL);
   1130 }
   1131 
   1132 static uint
   1133 acpi_get_intr(ACPI_HANDLE handle)
   1134 {
   1135 	ACPI_BUFFER ret;
   1136 	ACPI_STATUS rv;
   1137 	ACPI_RESOURCE *res;
   1138 	ACPI_RESOURCE_IRQ *irq;
   1139 	uint intr;
   1140 
   1141 	intr = -1;
   1142 	rv = acpi_get(handle, &ret, AcpiGetCurrentResources);
   1143 	if (ACPI_FAILURE(rv))
   1144 		return intr;
   1145 	for (res = ret.Pointer; res->Type != ACPI_RESOURCE_TYPE_END_TAG;
   1146 	     res = ACPI_NEXT_RESOURCE(res)) {
   1147 		if (res->Type == ACPI_RESOURCE_TYPE_IRQ) {
   1148 			irq = (ACPI_RESOURCE_IRQ *)&res->Data;
   1149 			if (irq->InterruptCount == 1)
   1150 				intr = irq->Interrupts[0];
   1151 			break;
   1152 		}
   1153 	}
   1154 	AcpiOsFree(ret.Pointer);
   1155 	return intr;
   1156 }
   1157 
   1158 static void
   1159 acpi_pci_set_line(int bus, int dev, int pin, int line)
   1160 {
   1161 	ACPI_STATUS err;
   1162 	ACPI_PCI_ID pid;
   1163 	UINT32 intr, id, bhlc;
   1164 	int func, nfunc;
   1165 
   1166 	pid.Bus = bus;
   1167 	pid.Device = dev;
   1168 	pid.Function = 0;
   1169 
   1170 	err = AcpiOsReadPciConfiguration(&pid, PCI_BHLC_REG, &bhlc, 32);
   1171 	if (err)
   1172 		return;
   1173 	if (PCI_HDRTYPE_MULTIFN(bhlc))
   1174 		nfunc = 8;
   1175 	else
   1176 		nfunc = 1;
   1177 
   1178 	for (func = 0; func < nfunc; func++) {
   1179 		pid.Function = func;
   1180 
   1181 		err = AcpiOsReadPciConfiguration(&pid, PCI_ID_REG, &id, 32);
   1182 		if (err || PCI_VENDOR(id) == PCI_VENDOR_INVALID ||
   1183 		    PCI_VENDOR(id) == 0)
   1184 			continue;
   1185 
   1186 		err = AcpiOsReadPciConfiguration(&pid, PCI_INTERRUPT_REG,
   1187 			  &intr, 32);
   1188 		if (err) {
   1189 			printf("AcpiOsReadPciConfiguration failed %d\n", err);
   1190 			return;
   1191 		}
   1192 		if (pin == PCI_INTERRUPT_PIN(intr) &&
   1193 		    line != PCI_INTERRUPT_LINE(intr)) {
   1194 #ifdef ACPI_DEBUG
   1195 			printf("acpi fixup pci intr: %d:%d:%d %c: %d -> %d\n",
   1196 			       bus, dev, func,
   1197 			       pin + '@', PCI_INTERRUPT_LINE(intr),
   1198 			       line);
   1199 #endif
   1200 			intr &= ~(PCI_INTERRUPT_LINE_MASK <<
   1201 				  PCI_INTERRUPT_LINE_SHIFT);
   1202 			intr |= line << PCI_INTERRUPT_LINE_SHIFT;
   1203 			err = AcpiOsWritePciConfiguration(&pid,
   1204 				  PCI_INTERRUPT_REG, intr, 32);
   1205 			if (err) {
   1206 				printf("AcpiOsWritePciConfiguration failed"
   1207 				       " %d\n", err);
   1208 				return;
   1209 			}
   1210 		}
   1211 	}
   1212 }
   1213 
   1214 ACPI_STATUS
   1215 acpi_pci_fixup_bus(ACPI_HANDLE handle, UINT32 level, void *context,
   1216 		   void **status)
   1217 {
   1218 	struct acpi_softc *sc = context;
   1219 	ACPI_STATUS rv;
   1220 	ACPI_BUFFER buf;
   1221 	UINT8 *Buffer;
   1222 	ACPI_PCI_ROUTING_TABLE *PrtElement;
   1223 	ACPI_HANDLE link;
   1224 	uint line;
   1225 	ACPI_INTEGER val;
   1226 
   1227 	rv = acpi_get(handle, &buf, AcpiGetIrqRoutingTable);
   1228 	if (ACPI_FAILURE(rv))
   1229 		return AE_OK;
   1230 
   1231 	/*
   1232 	 * If at level 1, this is a PCI root bus. Try the _BBN method
   1233 	 * to get the right PCI bus numbering for the following
   1234 	 * busses (this is a depth-first walk). It may fail,
   1235 	 * for example if there's only one root bus, but that
   1236 	 * case should be ok, so we'll ignore that.
   1237 	 */
   1238 	if (level == 1) {
   1239 		rv = acpi_eval_integer(handle, METHOD_NAME__BBN, &val);
   1240 		if (!ACPI_FAILURE(rv)) {
   1241 #ifdef ACPI_DEBUG
   1242 			printf("%s: fixup: _BBN success, bus # was %d now %d\n",
   1243 			    sc->sc_dev.dv_xname, sc->sc_pci_bus,
   1244 			    ACPI_LOWORD(val));
   1245 #endif
   1246 			sc->sc_pci_bus = ACPI_LOWORD(val);
   1247 		}
   1248 	}
   1249 
   1250 
   1251 #ifdef ACPI_DEBUG
   1252 	printf("%s: fixing up PCI bus %d at level %u\n", sc->sc_dev.dv_xname,
   1253 	    sc->sc_pci_bus, level);
   1254 #endif
   1255 
   1256         for (Buffer = buf.Pointer; ; Buffer += PrtElement->Length) {
   1257 		PrtElement = (ACPI_PCI_ROUTING_TABLE *)Buffer;
   1258 		if (PrtElement->Length == 0)
   1259 			break;
   1260 		if (PrtElement->Source[0] == 0)
   1261 			continue;
   1262 
   1263 		rv = AcpiGetHandle(NULL, PrtElement->Source, &link);
   1264 		if (ACPI_FAILURE(rv))
   1265 			continue;
   1266 		line = acpi_get_intr(link);
   1267 		if (line == (uint)-1 || line == 0) {
   1268 			printf("%s: fixing up intr link %s\n",
   1269 			    sc->sc_dev.dv_xname, PrtElement->Source);
   1270 			rv = acpi_allocate_resources(link);
   1271 			if (ACPI_FAILURE(rv)) {
   1272 				printf("%s: interrupt allocation failed %s\n",
   1273 				    sc->sc_dev.dv_xname, PrtElement->Source);
   1274 				continue;
   1275 			}
   1276 			line = acpi_get_intr(link);
   1277 			if (line == (uint)-1) {
   1278 				printf("%s: get intr failed %s\n",
   1279 				    sc->sc_dev.dv_xname, PrtElement->Source);
   1280 				continue;
   1281 			}
   1282 		}
   1283 
   1284 		acpi_pci_set_line(sc->sc_pci_bus, PrtElement->Address >> 16,
   1285 		    PrtElement->Pin + 1, line);
   1286 	}
   1287 
   1288 	sc->sc_pci_bus++;
   1289 
   1290 	AcpiOsFree(buf.Pointer);
   1291 	return AE_OK;
   1292 }
   1293 #endif /* PCI_INTR_FIXUP */
   1294 
   1295 #if defined(PCI_INTR_FIXUP) || defined(ACPI_ACTIVATE_DEV)
   1296 /* XXX This very incomplete */
   1297 static ACPI_STATUS
   1298 acpi_allocate_resources(ACPI_HANDLE handle)
   1299 {
   1300 	ACPI_BUFFER bufp, bufc, bufn;
   1301 	ACPI_RESOURCE *resp, *resc, *resn;
   1302 	ACPI_RESOURCE_IRQ *irq;
   1303 	ACPI_STATUS rv;
   1304 	uint delta;
   1305 
   1306 	rv = acpi_get(handle, &bufp, AcpiGetPossibleResources);
   1307 	if (ACPI_FAILURE(rv))
   1308 		goto out;
   1309 	rv = acpi_get(handle, &bufc, AcpiGetCurrentResources);
   1310 	if (ACPI_FAILURE(rv)) {
   1311 		goto out1;
   1312 	}
   1313 
   1314 	bufn.Length = 1000;
   1315 	bufn.Pointer = resn = malloc(bufn.Length, M_ACPI, M_WAITOK);
   1316 	resp = bufp.Pointer;
   1317 	resc = bufc.Pointer;
   1318 	while (resc->Type != ACPI_RESOURCE_TYPE_END_TAG &&
   1319 	       resp->Type != ACPI_RESOURCE_TYPE_END_TAG) {
   1320 		while (resc->Type != resp->Type && resp->Type != ACPI_RESOURCE_TYPE_END_TAG)
   1321 			resp = ACPI_NEXT_RESOURCE(resp);
   1322 		if (resp->Type == ACPI_RESOURCE_TYPE_END_TAG)
   1323 			break;
   1324 		/* Found identical Id */
   1325 		resn->Type = resc->Type;
   1326 		switch (resc->Type) {
   1327 		case ACPI_RESOURCE_TYPE_IRQ:
   1328 			memcpy(&resn->Data, &resp->Data,
   1329 			       sizeof(ACPI_RESOURCE_IRQ));
   1330 			irq = (ACPI_RESOURCE_IRQ *)&resn->Data;
   1331 			irq->Interrupts[0] =
   1332 			    ((ACPI_RESOURCE_IRQ *)&resp->Data)->
   1333 			        Interrupts[irq->InterruptCount-1];
   1334 			irq->InterruptCount = 1;
   1335 			resn->Length = ACPI_RS_SIZE(ACPI_RESOURCE_IRQ);
   1336 			break;
   1337 		case ACPI_RESOURCE_TYPE_IO:
   1338 			memcpy(&resn->Data, &resp->Data,
   1339 			       sizeof(ACPI_RESOURCE_IO));
   1340 			resn->Length = resp->Length;
   1341 			break;
   1342 		default:
   1343 			printf("acpi_allocate_resources: res=%d\n", resc->Type);
   1344 			rv = AE_BAD_DATA;
   1345 			goto out2;
   1346 		}
   1347 		resc = ACPI_NEXT_RESOURCE(resc);
   1348 		resn = ACPI_NEXT_RESOURCE(resn);
   1349 		resp = ACPI_NEXT_RESOURCE(resp);
   1350 		delta = (UINT8 *)resn - (UINT8 *)bufn.Pointer;
   1351 		if (delta >=
   1352 		    bufn.Length-ACPI_RS_SIZE(ACPI_RESOURCE_DATA)) {
   1353 			bufn.Length *= 2;
   1354 			bufn.Pointer = realloc(bufn.Pointer, bufn.Length,
   1355 					       M_ACPI, M_WAITOK);
   1356 			resn = (ACPI_RESOURCE *)((UINT8 *)bufn.Pointer + delta);
   1357 		}
   1358 	}
   1359 	if (resc->Type != ACPI_RESOURCE_TYPE_END_TAG) {
   1360 		printf("acpi_allocate_resources: resc not exhausted\n");
   1361 		rv = AE_BAD_DATA;
   1362 		goto out3;
   1363 	}
   1364 
   1365 	resn->Type = ACPI_RESOURCE_TYPE_END_TAG;
   1366 	rv = AcpiSetCurrentResources(handle, &bufn);
   1367 	if (ACPI_FAILURE(rv)) {
   1368 		printf("acpi_allocate_resources: AcpiSetCurrentResources %s\n",
   1369 		       AcpiFormatException(rv));
   1370 	}
   1371 
   1372 out3:
   1373 	free(bufn.Pointer, M_ACPI);
   1374 out2:
   1375 	AcpiOsFree(bufc.Pointer);
   1376 out1:
   1377 	AcpiOsFree(bufp.Pointer);
   1378 out:
   1379 	return rv;
   1380 }
   1381 #endif /* PCI_INTR_FIXUP || ACPI_ACTIVATE_DEV */
   1382 
   1383 SYSCTL_SETUP(sysctl_acpi_setup, "sysctl hw.acpi subtree setup")
   1384 {
   1385 	const struct sysctlnode *node;
   1386 	const struct sysctlnode *ssnode;
   1387 
   1388 	if (sysctl_createv(clog, 0, NULL, NULL,
   1389 	    CTLFLAG_PERMANENT,
   1390 	    CTLTYPE_NODE, "hw", NULL,
   1391 	    NULL, 0, NULL, 0,
   1392 	    CTL_HW, CTL_EOL) != 0)
   1393 		return;
   1394 
   1395 	if (sysctl_createv(clog, 0, NULL, &node,
   1396 	    CTLFLAG_PERMANENT,
   1397 	    CTLTYPE_NODE, "acpi", NULL,
   1398 	    NULL, 0, NULL, 0,
   1399 	    CTL_HW, CTL_CREATE, CTL_EOL) != 0)
   1400 		return;
   1401 
   1402 	acpi_node = node->sysctl_num;
   1403 
   1404 	/* ACPI sleepstate sysctl */
   1405 	if (sysctl_createv(NULL, 0, NULL, &node,
   1406 	    CTLFLAG_PERMANENT,
   1407 	    CTLTYPE_NODE, "machdep", NULL,
   1408 	    NULL, 0, NULL, 0, CTL_MACHDEP, CTL_EOL) != 0)
   1409 		return;
   1410 	if (sysctl_createv(NULL, 0, &node, &ssnode,
   1411 	    CTLFLAG_READWRITE, CTLTYPE_INT, "sleep_state",
   1412 	    NULL, sysctl_hw_acpi_sleepstate, 0, NULL, 0, CTL_CREATE,
   1413 	    CTL_EOL) != 0)
   1414 		return;
   1415 }
   1416 
   1417 static int
   1418 sysctl_hw_acpi_sleepstate(SYSCTLFN_ARGS)
   1419 {
   1420 	int error, t;
   1421 	struct sysctlnode node;
   1422 
   1423 	node = *rnode;
   1424 	t = acpi_sleepstate;
   1425 	node.sysctl_data = &t;
   1426 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1427 	if (error || newp == NULL)
   1428 		return error;
   1429 
   1430 	if (t < ACPI_STATE_S0 || t > ACPI_STATE_S5)
   1431 		return EINVAL;
   1432 
   1433 	if (acpi_softc != NULL && acpi_sleepstate != t) {
   1434 		acpi_sleepstate = t;
   1435 		aprint_normal("acpi0: entering state %d\n", t);
   1436 		acpi_enter_sleep_state(acpi_softc, t);
   1437 		aprint_normal("acpi0: resuming\n");
   1438 		t = acpi_sleepstate = ACPI_STATE_S0;
   1439 	}
   1440 
   1441 	return 0;
   1442 }
   1443