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