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