Home | History | Annotate | Line # | Download | only in acpi
acpi.c revision 1.57
      1 /*	$NetBSD: acpi.c,v 1.57 2003/11/03 18:51:31 mycroft 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.57 2003/11/03 18:51:31 mycroft 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/acpi/acpica/Subsystem/acnamesp.h> /* AcpiNsGetNodeByPath() */
    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 int	acpi_match(struct device *, struct cfdata *, void *);
    119 void	acpi_attach(struct device *, struct device *, void *);
    120 
    121 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 void		acpi_shutdown(void *);
    151 ACPI_STATUS	acpi_disable(struct acpi_softc *sc);
    152 void		acpi_build_tree(struct acpi_softc *);
    153 ACPI_STATUS	acpi_make_devnode(ACPI_HANDLE, UINT32, void *, void **);
    154 
    155 void		acpi_enable_fixed_events(struct acpi_softc *);
    156 #ifdef ACPI_PCI_FIXUP
    157 void		acpi_pci_fixup(struct acpi_softc *);
    158 #endif
    159 #if defined(ACPI_PCI_FIXUP) || defined(ACPI_ACTIVATE_DEV)
    160 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 int
    225 acpi_match(struct device *parent, struct cfdata *match, void *aux)
    226 {
    227 	struct acpibus_attach_args *aa = aux;
    228 
    229 	if (strcmp(aa->aa_busname, acpi_cd.cd_name) != 0)
    230 		return (0);
    231 
    232 	/*
    233 	 * XXX Check other locators?  Hard to know -- machine
    234 	 * dependent code has already checked for the presence
    235 	 * of ACPI by calling acpi_probe(), so I suppose we
    236 	 * don't really have to do anything else.
    237 	 */
    238 	return (1);
    239 }
    240 
    241 /*
    242  * acpi_attach:
    243  *
    244  *	Autoconfiguration `attach' routine.  Finish initializing
    245  *	ACPICA (some initialization was done in acpi_probe(),
    246  *	which was required to check for the presence of ACPI),
    247  *	and enable the ACPI subsystem.
    248  */
    249 void
    250 acpi_attach(struct device *parent, struct device *self, void *aux)
    251 {
    252 	struct acpi_softc *sc = (void *) self;
    253 	struct acpibus_attach_args *aa = aux;
    254 	ACPI_STATUS rv;
    255 
    256 	printf("\n");
    257 
    258 	if (acpi_softc != NULL)
    259 		panic("acpi_attach: ACPI has already been attached");
    260 
    261 	sysmon_power_settype("acpi");
    262 
    263 	printf("%s: using Intel ACPI CA subsystem version %08x\n",
    264 	    sc->sc_dev.dv_xname, ACPI_CA_VERSION);
    265 
    266 	printf("%s: X/RSDT: OemId <%6.6s,%8.8s,%08x>, AslId <%4.4s,%08x>\n",
    267 	    sc->sc_dev.dv_xname,
    268 	    AcpiGbl_XSDT->OemId, AcpiGbl_XSDT->OemTableId,
    269 	    AcpiGbl_XSDT->OemRevision,
    270 	    AcpiGbl_XSDT->AslCompilerId, AcpiGbl_XSDT->AslCompilerRevision);
    271 
    272 	sc->sc_quirks = acpi_find_quirks();
    273 
    274 	sc->sc_iot = aa->aa_iot;
    275 	sc->sc_memt = aa->aa_memt;
    276 	sc->sc_pc = aa->aa_pc;
    277 	sc->sc_pciflags = aa->aa_pciflags;
    278 	sc->sc_ic = aa->aa_ic;
    279 
    280 	acpi_softc = sc;
    281 
    282 	/*
    283 	 * Bring ACPI on-line.
    284 	 */
    285 #ifdef ACPI_DEBUGGER
    286 	if (acpi_dbgr & ACPI_DBGR_ENABLE)
    287 		acpi_osd_debugger();
    288 #endif
    289 
    290 	rv = AcpiEnableSubsystem(0);
    291 	if (ACPI_FAILURE(rv)) {
    292 		printf("%s: unable to enable ACPI: %s\n",
    293 		    sc->sc_dev.dv_xname, AcpiFormatException(rv));
    294 		return;
    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 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 ACPI_STATUS
    374 acpi_disable(struct acpi_softc *sc)
    375 {
    376 	ACPI_STATUS rv = AE_OK;
    377 
    378 	if (acpi_active) {
    379 		rv = AcpiDisable();
    380 		if (ACPI_SUCCESS(rv))
    381 			acpi_active = 0;
    382 	}
    383 	return (rv);
    384 }
    385 
    386 struct acpi_make_devnode_state {
    387 	struct acpi_softc *softc;
    388 	struct acpi_scope *scope;
    389 };
    390 
    391 /*
    392  * acpi_build_tree:
    393  *
    394  *	Scan relevant portions of the ACPI namespace and attach
    395  *	child devices.
    396  */
    397 void
    398 acpi_build_tree(struct acpi_softc *sc)
    399 {
    400 	static const char *scopes[] = {
    401 		"\\_PR_",	/* ACPI 1.0 processor namespace */
    402 		"\\_SB_",	/* system bus namespace */
    403 		"\\_SI_",	/* system idicator namespace */
    404 		"\\_TZ_",	/* ACPI 1.0 thermal zone namespace */
    405 		NULL,
    406 	};
    407 	struct acpi_attach_args aa;
    408 	struct acpi_make_devnode_state state;
    409 	struct acpi_scope *as;
    410 	struct acpi_devnode *ad;
    411 	ACPI_HANDLE parent;
    412 	ACPI_STATUS rv;
    413 	int i;
    414 
    415 	TAILQ_INIT(&sc->sc_scopes);
    416 
    417 	state.softc = sc;
    418 
    419 	/*
    420 	 * Scan the namespace and build our tree.
    421 	 */
    422 	for (i = 0; scopes[i] != NULL; i++) {
    423 		as = malloc(sizeof(*as), M_ACPI, M_WAITOK);
    424 		as->as_name = scopes[i];
    425 		TAILQ_INIT(&as->as_devnodes);
    426 
    427 		TAILQ_INSERT_TAIL(&sc->sc_scopes, as, as_list);
    428 
    429 		state.scope = as;
    430 
    431 		rv = AcpiGetHandle(ACPI_ROOT_OBJECT, (char *) scopes[i],
    432 		    &parent);
    433 		if (ACPI_SUCCESS(rv)) {
    434 			AcpiWalkNamespace(ACPI_TYPE_ANY, parent, 100,
    435 			    acpi_make_devnode, &state, NULL);
    436 		}
    437 
    438 		/* Now, for this namespace, try and attach the devices. */
    439 		TAILQ_FOREACH(ad, &as->as_devnodes, ad_list) {
    440 			aa.aa_node = ad;
    441 			aa.aa_iot = sc->sc_iot;
    442 			aa.aa_memt = sc->sc_memt;
    443 			aa.aa_pc = sc->sc_pc;
    444 			aa.aa_pciflags = sc->sc_pciflags;
    445 			aa.aa_ic = sc->sc_ic;
    446 
    447 			if (ad->ad_devinfo->Type == ACPI_TYPE_DEVICE) {
    448 				/*
    449 				 * XXX We only attach devices which are:
    450 				 *
    451 				 *	- present
    452 				 *	- enabled
    453 				 *	- functioning properly
    454 				 *
    455 				 * However, if enabled, it's decoding resources,
    456 				 * so we should claim them, if possible.
    457 				 * Requires changes to bus_space(9).
    458 				 */
    459 				if ((ad->ad_devinfo->Valid & ACPI_VALID_STA) ==
    460 				    ACPI_VALID_STA &&
    461 				    (ad->ad_devinfo->CurrentStatus &
    462 				     (ACPI_STA_DEV_PRESENT|ACPI_STA_DEV_ENABLED|
    463 				      ACPI_STA_DEV_OK)) !=
    464 				    (ACPI_STA_DEV_PRESENT|ACPI_STA_DEV_ENABLED|
    465 				     ACPI_STA_DEV_OK))
    466 					continue;
    467 
    468 				/*
    469 				 * XXX Same problem as above...
    470 				 */
    471 				if ((ad->ad_devinfo->Valid & ACPI_VALID_HID)
    472 				    == 0)
    473 					continue;
    474 			}
    475 
    476 			ad->ad_device = config_found(&sc->sc_dev,
    477 			    &aa, acpi_print);
    478 		}
    479 	}
    480 }
    481 
    482 #ifdef ACPI_ACTIVATE_DEV
    483 static void
    484 acpi_activate_device(ACPI_HANDLE handle, ACPI_DEVICE_INFO **di)
    485 {
    486 	ACPI_STATUS rv;
    487 	ACPI_BUFFER buf;
    488 
    489 	buf.Pointer = NULL;
    490 	buf.Length = ACPI_ALLOCATE_BUFFER;
    491 
    492 #ifdef ACPI_DEBUG
    493 	printf("acpi_activate_device: %s, old status=%x\n",
    494 	       (*di)->HardwareId.Value, (*di)->CurrentStatus);
    495 #endif
    496 
    497 	rv = acpi_allocate_resources(handle);
    498 	if (ACPI_FAILURE(rv)) {
    499 		printf("acpi: activate failed for %s\n",
    500 		       (*di)->HardwareId.Value);
    501 	} else {
    502 		printf("acpi: activated %s\n", (*di)->HardwareId.Value);
    503 	}
    504 
    505 	(void)AcpiGetObjectInfo(handle, &buf);
    506 	AcpiOsFree(*di);
    507 	*di = buf.Pointer;
    508 
    509 #ifdef ACPI_DEBUG
    510 	printf("acpi_activate_device: %s, new status=%x\n",
    511 	       (*di)->HardwareId.Value, (*di)->CurrentStatus);
    512 #endif
    513 }
    514 #endif /* ACPI_ACTIVATE_DEV */
    515 
    516 /*
    517  * acpi_make_devnode:
    518  *
    519  *	Make an ACPI devnode.
    520  */
    521 ACPI_STATUS
    522 acpi_make_devnode(ACPI_HANDLE handle, UINT32 level, void *context,
    523     void **status)
    524 {
    525 	struct acpi_make_devnode_state *state = context;
    526 #if defined(ACPI_DEBUG) || defined(ACPI_EXTRA_DEBUG)
    527 	struct acpi_softc *sc = state->softc;
    528 #endif
    529 	struct acpi_scope *as = state->scope;
    530 	struct acpi_devnode *ad;
    531 	ACPI_OBJECT_TYPE type;
    532 	ACPI_BUFFER buf;
    533 	ACPI_DEVICE_INFO *devinfo;
    534 	ACPI_STATUS rv;
    535 
    536 	rv = AcpiGetType(handle, &type);
    537 	if (ACPI_SUCCESS(rv)) {
    538 		buf.Pointer = NULL;
    539 		buf.Length = ACPI_ALLOCATE_BUFFER;
    540 		rv = AcpiGetObjectInfo(handle, &buf);
    541 		if (ACPI_FAILURE(rv)) {
    542 #ifdef ACPI_DEBUG
    543 			printf("%s: AcpiGetObjectInfo failed: %s\n",
    544 			    sc->sc_dev.dv_xname, AcpiFormatException(rv));
    545 #endif
    546 			goto out; /* XXX why return OK */
    547 		}
    548 
    549 		devinfo = buf.Pointer;
    550 
    551 		switch (type) {
    552 		case ACPI_TYPE_DEVICE:
    553 #ifdef ACPI_ACTIVATE_DEV
    554 			if ((devinfo->Valid & (ACPI_VALID_STA|ACPI_VALID_HID)) ==
    555 			    (ACPI_VALID_STA|ACPI_VALID_HID) &&
    556 			    (devinfo->CurrentStatus &
    557 			     (ACPI_STA_DEV_PRESENT|ACPI_STA_DEV_ENABLED)) ==
    558 			    ACPI_STA_DEV_PRESENT)
    559 				acpi_activate_device(handle, &devinfo);
    560 
    561 			/* FALLTHROUGH */
    562 #endif
    563 
    564 		case ACPI_TYPE_PROCESSOR:
    565 		case ACPI_TYPE_THERMAL:
    566 		case ACPI_TYPE_POWER:
    567 			ad = malloc(sizeof(*ad), M_ACPI, M_NOWAIT|M_ZERO);
    568 			if (ad == NULL)
    569 				return (AE_NO_MEMORY);
    570 
    571 			ad->ad_devinfo = devinfo;
    572 			ad->ad_handle = handle;
    573 			ad->ad_level = level;
    574 			ad->ad_scope = as;
    575 			ad->ad_type = type;
    576 
    577 			TAILQ_INSERT_TAIL(&as->as_devnodes, ad, ad_list);
    578 
    579 			if ((ad->ad_devinfo->Valid & ACPI_VALID_HID) == 0)
    580 				goto out;
    581 
    582 #ifdef ACPI_EXTRA_DEBUG
    583 			printf("%s: HID %s found in scope %s level %d\n",
    584 			    sc->sc_dev.dv_xname,
    585 			    ad->ad_devinfo->HardwareId.Value,
    586 			    as->as_name, ad->ad_level);
    587 			if (ad->ad_devinfo->Valid & ACPI_VALID_UID)
    588 				printf("       UID %s\n",
    589 				    ad->ad_devinfo->UniqueId.Value);
    590 			if (ad->ad_devinfo->Valid & ACPI_VALID_ADR)
    591 				printf("       ADR 0x%016qx\n",
    592 				    ad->ad_devinfo->Address);
    593 			if (ad->ad_devinfo->Valid & ACPI_VALID_STA)
    594 				printf("       STA 0x%08x\n",
    595 				    ad->ad_devinfo->CurrentStatus);
    596 #endif
    597 		}
    598 	}
    599  out:
    600 	return (AE_OK);
    601 }
    602 
    603 /*
    604  * acpi_print:
    605  *
    606  *	Autoconfiguration print routine.
    607  */
    608 int
    609 acpi_print(void *aux, const char *pnp)
    610 {
    611 	struct acpi_attach_args *aa = aux;
    612 	ACPI_STATUS rv;
    613 
    614 	if (pnp) {
    615 		if (aa->aa_node->ad_devinfo->Valid & ACPI_VALID_HID) {
    616 			char *pnpstr =
    617 			    aa->aa_node->ad_devinfo->HardwareId.Value;
    618 			char *str;
    619 
    620 			aprint_normal("%s ", pnpstr);
    621 			rv = acpi_eval_string(aa->aa_node->ad_handle,
    622 			    "_STR", &str);
    623 			if (ACPI_SUCCESS(rv)) {
    624 				aprint_normal("[%s] ", str);
    625 				AcpiOsFree(str);
    626 			}
    627 #ifdef ACPIVERBOSE
    628 			else {
    629 				int i;
    630 
    631 				for (i = 0; i < sizeof(acpi_knowndevs) /
    632 				    sizeof(acpi_knowndevs[0]); i++) {
    633 					if (strcmp(acpi_knowndevs[i].pnp,
    634 					    pnpstr) == 0) {
    635 						printf("[%s] ",
    636 						    acpi_knowndevs[i].str);
    637 					}
    638 				}
    639 			}
    640 
    641 #endif
    642 		} else {
    643 			aprint_normal("ACPI Object Type '%s' (0x%02x) ",
    644 			   AcpiUtGetTypeName(aa->aa_node->ad_devinfo->Type),
    645 			   aa->aa_node->ad_devinfo->Type);
    646 		}
    647 		aprint_normal("at %s", pnp);
    648 	} else {
    649 		if (aa->aa_node->ad_devinfo->Valid & ACPI_VALID_HID) {
    650 			aprint_normal(" (%s", aa->aa_node->ad_devinfo->HardwareId.Value);
    651 			if (aa->aa_node->ad_devinfo->Valid & ACPI_VALID_UID) {
    652 				char *uid;
    653 
    654 				uid = aa->aa_node->ad_devinfo->UniqueId.Value;
    655 				if (uid[0] == '\0')
    656 					uid = "<null>";
    657 				aprint_normal("-%s", uid);
    658 			}
    659 			aprint_normal(")");
    660 		}
    661 	}
    662 
    663 	return (UNCONF);
    664 }
    665 
    666 /*****************************************************************************
    667  * ACPI fixed-hardware feature handlers
    668  *****************************************************************************/
    669 
    670 UINT32		acpi_fixed_button_handler(void *);
    671 void		acpi_fixed_button_pressed(void *);
    672 
    673 /*
    674  * acpi_enable_fixed_events:
    675  *
    676  *	Enable any fixed-hardware feature handlers.
    677  */
    678 void
    679 acpi_enable_fixed_events(struct acpi_softc *sc)
    680 {
    681 	static int beenhere;
    682 	ACPI_STATUS rv;
    683 
    684 	KASSERT(beenhere == 0);
    685 	beenhere = 1;
    686 
    687 	/*
    688 	 * Check for fixed-hardware buttons.
    689 	 */
    690 
    691 	if (AcpiGbl_FADT != NULL && AcpiGbl_FADT->PwrButton == 0) {
    692 		printf("%s: fixed-feature power button present\n",
    693 		    sc->sc_dev.dv_xname);
    694 		sc->sc_smpsw_power.smpsw_name = sc->sc_dev.dv_xname;
    695 		sc->sc_smpsw_power.smpsw_type = PSWITCH_TYPE_POWER;
    696 		if (sysmon_pswitch_register(&sc->sc_smpsw_power) != 0) {
    697 			printf("%s: unable to register fixed power button "
    698 			    "with sysmon\n", sc->sc_dev.dv_xname);
    699 		} else {
    700 			rv = AcpiInstallFixedEventHandler(
    701 			    ACPI_EVENT_POWER_BUTTON,
    702 			    acpi_fixed_button_handler, &sc->sc_smpsw_power);
    703 			if (ACPI_FAILURE(rv)) {
    704 				printf("%s: unable to install handler for "
    705 				    "fixed power button: %s\n",
    706 				    sc->sc_dev.dv_xname,
    707 				    AcpiFormatException(rv));
    708 			}
    709 		}
    710 	}
    711 
    712 	if (AcpiGbl_FADT != NULL && AcpiGbl_FADT->SleepButton == 0) {
    713 		printf("%s: fixed-feature sleep button present\n",
    714 		    sc->sc_dev.dv_xname);
    715 		sc->sc_smpsw_sleep.smpsw_name = sc->sc_dev.dv_xname;
    716 		sc->sc_smpsw_sleep.smpsw_type = PSWITCH_TYPE_SLEEP;
    717 		if (sysmon_pswitch_register(&sc->sc_smpsw_power) != 0) {
    718 			printf("%s: unable to register fixed sleep button "
    719 			    "with sysmon\n", sc->sc_dev.dv_xname);
    720 		} else {
    721 			rv = AcpiInstallFixedEventHandler(
    722 			    ACPI_EVENT_SLEEP_BUTTON,
    723 			    acpi_fixed_button_handler, &sc->sc_smpsw_sleep);
    724 			if (ACPI_FAILURE(rv)) {
    725 				printf("%s: unable to install handler for "
    726 				    "fixed sleep button: %s\n",
    727 				    sc->sc_dev.dv_xname,
    728 				    AcpiFormatException(rv));
    729 			}
    730 		}
    731 	}
    732 }
    733 
    734 /*
    735  * acpi_fixed_button_handler:
    736  *
    737  *	Event handler for the fixed buttons.
    738  */
    739 UINT32
    740 acpi_fixed_button_handler(void *context)
    741 {
    742 	struct sysmon_pswitch *smpsw = context;
    743 	int rv;
    744 
    745 #ifdef ACPI_BUT_DEBUG
    746 	printf("%s: fixed button handler\n", smpsw->smpsw_name);
    747 #endif
    748 
    749 	rv = AcpiOsQueueForExecution(OSD_PRIORITY_LO,
    750 	    acpi_fixed_button_pressed, smpsw);
    751 	if (ACPI_FAILURE(rv))
    752 		printf("%s: WARNING: unable to queue fixed button pressed "
    753 		    "callback: %s\n", smpsw->smpsw_name,
    754 		    AcpiFormatException(rv));
    755 
    756 	return (ACPI_INTERRUPT_HANDLED);
    757 }
    758 
    759 /*
    760  * acpi_fixed_button_pressed:
    761  *
    762  *	Deal with a fixed button being pressed.
    763  */
    764 void
    765 acpi_fixed_button_pressed(void *context)
    766 {
    767 	struct sysmon_pswitch *smpsw = context;
    768 
    769 #ifdef ACPI_BUT_DEBUG
    770 	printf("%s: fixed button pressed, calling sysmon\n",
    771 	    smpsw->smpsw_name);
    772 #endif
    773 
    774 	sysmon_pswitch_event(smpsw, PSWITCH_EVENT_PRESSED);
    775 }
    776 
    777 /*****************************************************************************
    778  * ACPI utility routines.
    779  *****************************************************************************/
    780 
    781 /*
    782  * acpi_eval_integer:
    783  *
    784  *	Evaluate an integer object.
    785  */
    786 ACPI_STATUS
    787 acpi_eval_integer(ACPI_HANDLE handle, char *path, int *valp)
    788 {
    789 	ACPI_STATUS rv;
    790 	ACPI_BUFFER buf;
    791 	ACPI_OBJECT param;
    792 
    793 	if (handle == NULL)
    794 		handle = ACPI_ROOT_OBJECT;
    795 
    796 	buf.Pointer = &param;
    797 	buf.Length = sizeof(param);
    798 
    799 	rv = AcpiEvaluateObjectTyped(handle, path, NULL, &buf, ACPI_TYPE_INTEGER);
    800 	if (ACPI_SUCCESS(rv))
    801 		*valp = param.Integer.Value;
    802 
    803 	return (rv);
    804 }
    805 
    806 /*
    807  * acpi_eval_string:
    808  *
    809  *	Evaluate a (Unicode) string object.
    810  */
    811 ACPI_STATUS
    812 acpi_eval_string(ACPI_HANDLE handle, char *path, char **stringp)
    813 {
    814 	ACPI_STATUS rv;
    815 	ACPI_BUFFER buf;
    816 
    817 	if (handle == NULL)
    818 		handle = ACPI_ROOT_OBJECT;
    819 
    820 	buf.Pointer = NULL;
    821 	buf.Length = ACPI_ALLOCATE_BUFFER;
    822 
    823 	rv = AcpiEvaluateObjectTyped(handle, path, NULL, &buf, ACPI_TYPE_STRING);
    824 	if (ACPI_SUCCESS(rv)) {
    825 		ACPI_OBJECT *param = buf.Pointer;
    826 		char *ptr = param->String.Pointer;
    827 		size_t len = param->String.Length;
    828 		if ((*stringp = AcpiOsAllocate(len)) == NULL)
    829 			rv = AE_NO_MEMORY;
    830 		else
    831 			(void)memcpy(*stringp, ptr, len);
    832 		AcpiOsFree(param);
    833 	}
    834 
    835 	return (rv);
    836 }
    837 
    838 
    839 /*
    840  * acpi_eval_struct:
    841  *
    842  *	Evaluate a more complex structure.
    843  *	Caller must free buf.Pointer by AcpiOsFree().
    844  */
    845 ACPI_STATUS
    846 acpi_eval_struct(ACPI_HANDLE handle, char *path, ACPI_BUFFER *bufp)
    847 {
    848 	ACPI_STATUS rv;
    849 
    850 	if (handle == NULL)
    851 		handle = ACPI_ROOT_OBJECT;
    852 
    853 	bufp->Pointer = NULL;
    854 	bufp->Length = ACPI_ALLOCATE_BUFFER;
    855 
    856 	rv = AcpiEvaluateObject(handle, path, NULL, bufp);
    857 
    858 	return (rv);
    859 }
    860 
    861 /*
    862  * acpi_get:
    863  *
    864  *	Fetch data info the specified (empty) ACPI buffer.
    865  *	Caller must free buf.Pointer by AcpiOsFree().
    866  */
    867 ACPI_STATUS
    868 acpi_get(ACPI_HANDLE handle, ACPI_BUFFER *buf,
    869     ACPI_STATUS (*getit)(ACPI_HANDLE, ACPI_BUFFER *))
    870 {
    871 	buf->Pointer = NULL;
    872 	buf->Length = ACPI_ALLOCATE_BUFFER;
    873 
    874 	return ((*getit)(handle, buf));
    875 }
    876 
    877 
    878 /*
    879  * acpi_match_hid
    880  *
    881  *	Match given ids against _HID and _CIDs
    882  */
    883 int
    884 acpi_match_hid(ACPI_DEVICE_INFO *ad, const char * const *ids)
    885 {
    886 	int i;
    887 
    888 	while (*ids) {
    889 		if (ad->Valid & ACPI_VALID_HID) {
    890 			if (pmatch(ad->HardwareId.Value, *ids, NULL) == 2)
    891 				return (1);
    892 		}
    893 
    894 		if (ad->Valid & ACPI_VALID_CID) {
    895 			for (i = 0; i < ad->CompatibilityId.Count; i++) {
    896 				if (pmatch(ad->CompatibilityId.Id[i].Value, *ids, NULL) == 2)
    897 					return (1);
    898 			}
    899 		}
    900 		ids++;
    901 	}
    902 
    903 	return (0);
    904 }
    905 
    906 
    907 /*****************************************************************************
    908  * ACPI sleep support.
    909  *****************************************************************************/
    910 
    911 static int
    912 is_available_state(struct acpi_softc *sc, int state)
    913 {
    914 	UINT8 type_a, type_b;
    915 
    916 	return (ACPI_SUCCESS(AcpiGetSleepTypeData((UINT8)state,
    917 						  &type_a, &type_b)));
    918 }
    919 
    920 /*
    921  * acpi_enter_sleep_state:
    922  *
    923  *	enter to the specified sleep state.
    924  */
    925 
    926 ACPI_STATUS
    927 acpi_enter_sleep_state(struct acpi_softc *sc, int state)
    928 {
    929 	int s;
    930 	ACPI_STATUS ret = AE_OK;
    931 
    932 	switch (state) {
    933 	case ACPI_STATE_S0:
    934 		break;
    935 	case ACPI_STATE_S1:
    936 	case ACPI_STATE_S2:
    937 	case ACPI_STATE_S3:
    938 	case ACPI_STATE_S4:
    939 		if (!is_available_state(sc, state)) {
    940 			printf("acpi: cannot enter the sleep state (%d).\n",
    941 			       state);
    942 			break;
    943 		}
    944 		ret = AcpiEnterSleepStatePrep(state);
    945 		if (ACPI_FAILURE(ret)) {
    946 			printf("acpi: failed preparing to sleep (%s)\n",
    947 			       AcpiFormatException(ret));
    948 			break;
    949 		}
    950 		if (state==ACPI_STATE_S1) {
    951 			/* just enter the state */
    952 			acpi_md_OsDisableInterrupt();
    953 			AcpiEnterSleepState((UINT8)state);
    954 			AcpiUtReleaseMutex(ACPI_MTX_HARDWARE);
    955 		} else {
    956 			/* XXX: powerhooks(9) framework is too poor to
    957 			 * support ACPI sleep state...
    958 			 */
    959 			dopowerhooks(PWR_SOFTSUSPEND);
    960 			s = splhigh();
    961 			dopowerhooks(PWR_SUSPEND);
    962 			acpi_md_sleep(state);
    963 			dopowerhooks(PWR_RESUME);
    964 			splx(s);
    965 			dopowerhooks(PWR_SOFTRESUME);
    966 			if (state==ACPI_STATE_S4)
    967 				AcpiEnable();
    968 		}
    969 		AcpiLeaveSleepState((UINT8)state);
    970 		break;
    971 	case ACPI_STATE_S5:
    972 		ret = AcpiEnterSleepStatePrep(ACPI_STATE_S5);
    973 		if (ACPI_FAILURE(ret)) {
    974 			printf("acpi: failed preparing to sleep (%s)\n",
    975 			       AcpiFormatException(ret));
    976 			break;
    977 		}
    978 		acpi_md_OsDisableInterrupt();
    979 		AcpiEnterSleepState(ACPI_STATE_S5);
    980 		printf("WARNING: powerdown failed!\n");
    981 		break;
    982 	}
    983 
    984 	return (ret);
    985 }
    986 
    987 #ifdef ACPI_PCI_FIXUP
    988 ACPI_STATUS acpi_pci_fixup_bus(ACPI_HANDLE, UINT32, void *, void **);
    989 /*
    990  * acpi_pci_fixup:
    991  *
    992  *	Set up PCI devices that BIOS didn't handle right.
    993  *	Iterate through all devices and try to get the _PTR
    994  *	(PCI Routing Table).  If it exists then make sure all
    995  *	interrupt links that it uses are working.
    996  */
    997 void
    998 acpi_pci_fixup(struct acpi_softc *sc)
    999 {
   1000 	ACPI_HANDLE parent;
   1001 	ACPI_STATUS rv;
   1002 
   1003 #ifdef ACPI_DEBUG
   1004 	printf("acpi_pci_fixup starts:\n");
   1005 #endif
   1006 	rv = AcpiGetHandle(ACPI_ROOT_OBJECT, "\\_SB_", &parent);
   1007 	if (ACPI_FAILURE(rv))
   1008 		return;
   1009 	sc->sc_pci_bus = 0;
   1010 	AcpiWalkNamespace(ACPI_TYPE_DEVICE, parent, 100,
   1011 	    acpi_pci_fixup_bus, sc, NULL);
   1012 }
   1013 
   1014 static ACPI_HANDLE
   1015 acpi_get_node(char *name)
   1016 {
   1017 	ACPI_NAMESPACE_NODE *ObjDesc;
   1018 	ACPI_STATUS Status;
   1019 
   1020 	Status = AcpiNsGetNodeByPath(name, NULL, 0, &ObjDesc);
   1021 	if (ACPI_FAILURE (Status)) {
   1022 		printf("acpi_get_node: could not find: %s\n",
   1023 		       AcpiFormatException (Status));
   1024 		return NULL;
   1025 	}
   1026 	return ObjDesc;
   1027 }
   1028 
   1029 static uint
   1030 acpi_get_intr(ACPI_HANDLE handle)
   1031 {
   1032 	ACPI_BUFFER ret;
   1033 	ACPI_STATUS rv;
   1034 	ACPI_RESOURCE *res;
   1035 	ACPI_RESOURCE_IRQ *irq;
   1036 	uint intr;
   1037 
   1038 	intr = -1;
   1039 	rv = acpi_get(handle, &ret, AcpiGetCurrentResources);
   1040 	if (ACPI_FAILURE(rv))
   1041 		return (intr);
   1042 	for (res = ret.Pointer; res->Id != ACPI_RSTYPE_END_TAG;
   1043 	     res = ACPI_NEXT_RESOURCE(res)) {
   1044 		if (res->Id == ACPI_RSTYPE_IRQ) {
   1045 			irq = (ACPI_RESOURCE_IRQ *)&res->Data;
   1046 			if (irq->NumberOfInterrupts == 1)
   1047 				intr = irq->Interrupts[0];
   1048 			break;
   1049 		}
   1050 	}
   1051 	AcpiOsFree(ret.Pointer);
   1052 	return (intr);
   1053 }
   1054 
   1055 static void
   1056 acpi_pci_set_line(int bus, int dev, int pin, int line)
   1057 {
   1058 	ACPI_STATUS err;
   1059 	ACPI_PCI_ID pid;
   1060 	UINT32 intr, id, bhlc;
   1061 	int func, nfunc;
   1062 
   1063 	pid.Bus = bus;
   1064 	pid.Device = dev;
   1065 	pid.Function = 0;
   1066 
   1067 	err = AcpiOsReadPciConfiguration(&pid, PCI_BHLC_REG, &bhlc, 32);
   1068 	if (err)
   1069 		return;
   1070 	if (PCI_HDRTYPE_MULTIFN(bhlc))
   1071 		nfunc = 8;
   1072 	else
   1073 		nfunc = 1;
   1074 
   1075 	for (func = 0; func < nfunc; func++) {
   1076 		pid.Function = func;
   1077 
   1078 		err = AcpiOsReadPciConfiguration(&pid, PCI_ID_REG, &id, 32);
   1079 		if (err || PCI_VENDOR(id) == PCI_VENDOR_INVALID ||
   1080 		    PCI_VENDOR(id) == 0)
   1081 			continue;
   1082 
   1083 		err = AcpiOsReadPciConfiguration(&pid, PCI_INTERRUPT_REG,
   1084 			  &intr, 32);
   1085 		if (err) {
   1086 			printf("AcpiOsReadPciConfiguration failed %d\n", err);
   1087 			return;
   1088 		}
   1089 		if (pin == PCI_INTERRUPT_PIN(intr) &&
   1090 		    line != PCI_INTERRUPT_LINE(intr)) {
   1091 #ifdef ACPI_DEBUG
   1092 			printf("acpi fixup pci intr: %d:%d:%d %c: %d -> %d\n",
   1093 			       bus, dev, func,
   1094 			       pin + '@', PCI_INTERRUPT_LINE(intr),
   1095 			       line);
   1096 #endif
   1097 			intr &= ~(PCI_INTERRUPT_LINE_MASK <<
   1098 				  PCI_INTERRUPT_LINE_SHIFT);
   1099 			intr |= line << PCI_INTERRUPT_LINE_SHIFT;
   1100 			err = AcpiOsWritePciConfiguration(&pid,
   1101 				  PCI_INTERRUPT_REG, intr, 32);
   1102 			if (err) {
   1103 				printf("AcpiOsWritePciConfiguration failed"
   1104 				       " %d\n", err);
   1105 				return;
   1106 			}
   1107 		}
   1108 	}
   1109 }
   1110 
   1111 ACPI_STATUS
   1112 acpi_pci_fixup_bus(ACPI_HANDLE handle, UINT32 level, void *context,
   1113 		   void **status)
   1114 {
   1115 	struct acpi_softc *sc = context;
   1116 	ACPI_STATUS rv;
   1117 	ACPI_BUFFER buf;
   1118 	UINT8 *Buffer;
   1119 	ACPI_PCI_ROUTING_TABLE *PrtElement;
   1120 	ACPI_HANDLE link;
   1121 	uint line;
   1122 	ACPI_NAMESPACE_NODE *node;
   1123 	ACPI_INTEGER val;
   1124 
   1125 	rv = acpi_get(handle, &buf, AcpiGetIrqRoutingTable);
   1126 	if (ACPI_FAILURE(rv))
   1127 		return (AE_OK);
   1128 
   1129 	/*
   1130 	 * If at level 1, this is a PCI root bus. Try the _BBN method
   1131 	 * to get the right PCI bus numbering for the following
   1132 	 * busses (this is a depth-first walk). It may fail,
   1133 	 * for example if there's only one root bus, but that
   1134 	 * case should be ok, so we'll ignore that.
   1135 	 */
   1136 	if (level == 1) {
   1137 		node = AcpiNsMapHandleToNode(handle);
   1138 		rv = AcpiUtEvaluateNumericObject(METHOD_NAME__BBN, node, &val);
   1139 		if (!ACPI_FAILURE(rv)) {
   1140 #ifdef ACPI_DEBUG
   1141 			printf("%s: fixup: _BBN success, bus # was %d now %d\n",
   1142 			    sc->sc_dev.dv_xname, sc->sc_pci_bus,
   1143 			    ACPI_LOWORD(val));
   1144 #endif
   1145 			sc->sc_pci_bus = ACPI_LOWORD(val);
   1146 		}
   1147 	}
   1148 
   1149 
   1150 #ifdef ACPI_DEBUG
   1151 	printf("%s: fixing up PCI bus %d at level %u\n", sc->sc_dev.dv_xname,
   1152 	    sc->sc_pci_bus, level);
   1153 #endif
   1154 
   1155         for (Buffer = buf.Pointer; ; Buffer += PrtElement->Length) {
   1156 		PrtElement = (ACPI_PCI_ROUTING_TABLE *)Buffer;
   1157 		if (PrtElement->Length == 0)
   1158 			break;
   1159 		if (PrtElement->Source[0] == 0)
   1160 			continue;
   1161 
   1162 		link = acpi_get_node(PrtElement->Source);
   1163 		if (link == NULL)
   1164 			continue;
   1165 		line = acpi_get_intr(link);
   1166 		if (line == -1) {
   1167 #ifdef ACPI_DEBUG
   1168 			printf("%s: fixing up intr link %s\n",
   1169 			    sc->sc_dev.dv_xname, PrtElement->Source);
   1170 #endif
   1171 			rv = acpi_allocate_resources(link);
   1172 			if (ACPI_FAILURE(rv)) {
   1173 				printf("%s: interrupt allocation failed %s\n",
   1174 				    sc->sc_dev.dv_xname, PrtElement->Source);
   1175 				continue;
   1176 			}
   1177 			line = acpi_get_intr(link);
   1178 			if (line == -1) {
   1179 				printf("%s: get intr failed %s\n",
   1180 				    sc->sc_dev.dv_xname, PrtElement->Source);
   1181 				continue;
   1182 			}
   1183 		}
   1184 
   1185 		acpi_pci_set_line(sc->sc_pci_bus, PrtElement->Address >> 16,
   1186 		    PrtElement->Pin + 1, line);
   1187 	}
   1188 
   1189 	sc->sc_pci_bus++;
   1190 
   1191 	AcpiOsFree(buf.Pointer);
   1192 	return (AE_OK);
   1193 }
   1194 #endif /* ACPI_PCI_FIXUP */
   1195 
   1196 #if defined(ACPI_PCI_FIXUP) || defined(ACPI_ACTIVATE_DEV)
   1197 /* XXX This very incomplete */
   1198 ACPI_STATUS
   1199 acpi_allocate_resources(ACPI_HANDLE handle)
   1200 {
   1201 	ACPI_BUFFER bufp, bufc, bufn;
   1202 	ACPI_RESOURCE *resp, *resc, *resn;
   1203 	ACPI_RESOURCE_IRQ *irq;
   1204 	ACPI_STATUS rv;
   1205 	uint delta;
   1206 
   1207 	rv = acpi_get(handle, &bufp, AcpiGetPossibleResources);
   1208 	if (ACPI_FAILURE(rv))
   1209 		goto out;
   1210 	rv = acpi_get(handle, &bufc, AcpiGetCurrentResources);
   1211 	if (ACPI_FAILURE(rv)) {
   1212 		goto out1;
   1213 	}
   1214 
   1215 	bufn.Length = 1000;
   1216 	bufn.Pointer = resn = malloc(bufn.Length, M_ACPI, M_WAITOK);
   1217 	resp = bufp.Pointer;
   1218 	resc = bufc.Pointer;
   1219 	while (resc->Id != ACPI_RSTYPE_END_TAG &&
   1220 	       resp->Id != ACPI_RSTYPE_END_TAG) {
   1221 		while (resc->Id != resp->Id && resp->Id != ACPI_RSTYPE_END_TAG)
   1222 			resp = ACPI_NEXT_RESOURCE(resp);
   1223 		if (resp->Id == ACPI_RSTYPE_END_TAG)
   1224 			break;
   1225 		/* Found identical Id */
   1226 		resn->Id = resc->Id;
   1227 		switch (resc->Id) {
   1228 		case ACPI_RSTYPE_IRQ:
   1229 			memcpy(&resn->Data, &resp->Data,
   1230 			       sizeof(ACPI_RESOURCE_IRQ));
   1231 			irq = (ACPI_RESOURCE_IRQ *)&resn->Data;
   1232 			irq->Interrupts[0] =
   1233 			    ((ACPI_RESOURCE_IRQ *)&resp->Data)->
   1234 			        Interrupts[irq->NumberOfInterrupts-1];
   1235 			irq->NumberOfInterrupts = 1;
   1236 			resn->Length = ACPI_SIZEOF_RESOURCE(ACPI_RESOURCE_IRQ);
   1237 			break;
   1238 		case ACPI_RSTYPE_IO:
   1239 			memcpy(&resn->Data, &resp->Data,
   1240 			       sizeof(ACPI_RESOURCE_IO));
   1241 			resn->Length = resp->Length;
   1242 			break;
   1243 		default:
   1244 			printf("acpi_allocate_resources: res=%d\n", resc->Id);
   1245 			rv = AE_BAD_DATA;
   1246 			goto out2;
   1247 		}
   1248 		resc = ACPI_NEXT_RESOURCE(resc);
   1249 		resn = ACPI_NEXT_RESOURCE(resn);
   1250 		delta = (UINT8 *)resn - (UINT8 *)bufn.Pointer;
   1251 		if (delta >=
   1252 		    bufn.Length-ACPI_SIZEOF_RESOURCE(ACPI_RESOURCE_DATA)) {
   1253 			bufn.Length *= 2;
   1254 			bufn.Pointer = realloc(bufn.Pointer, bufn.Length,
   1255 					       M_ACPI, M_WAITOK);
   1256 			resn = (ACPI_RESOURCE *)((UINT8 *)bufn.Pointer + delta);
   1257 		}
   1258 	}
   1259 	if (resc->Id != ACPI_RSTYPE_END_TAG) {
   1260 		printf("acpi_allocate_resources: resc not exhausted\n");
   1261 		rv = AE_BAD_DATA;
   1262 		goto out3;
   1263 	}
   1264 
   1265 	resn->Id = ACPI_RSTYPE_END_TAG;
   1266 	rv = AcpiSetCurrentResources(handle, &bufn);
   1267 	if (ACPI_FAILURE(rv)) {
   1268 		printf("acpi_allocate_resources: AcpiSetCurrentResources %s\n",
   1269 		       AcpiFormatException(rv));
   1270 	}
   1271 
   1272 out3:
   1273 	free(bufn.Pointer, M_ACPI);
   1274 out2:
   1275 	AcpiOsFree(bufc.Pointer);
   1276 out1:
   1277 	AcpiOsFree(bufp.Pointer);
   1278 out:
   1279 	return rv;
   1280 }
   1281 #endif /* ACPI_PCI_FIXUP || ACPI_ACTIVATE_DEV */
   1282