Home | History | Annotate | Line # | Download | only in acpi
acpi.c revision 1.28
      1 /*	$NetBSD: acpi.c,v 1.28 2003/01/07 18:46:48 fvdl Exp $	*/
      2 
      3 /*
      4  * Copyright 2001 Wasabi Systems, Inc.
      5  * All rights reserved.
      6  *
      7  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *	This product includes software developed for the NetBSD Project by
     20  *	Wasabi Systems, Inc.
     21  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     22  *    or promote products derived from this software without specific prior
     23  *    written permission.
     24  *
     25  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35  * POSSIBILITY OF SUCH DAMAGE.
     36  */
     37 
     38 /*
     39  * Autoconfiguration support for the Intel ACPI Component Architecture
     40  * ACPI reference implementation.
     41  */
     42 
     43 #include <sys/cdefs.h>
     44 __KERNEL_RCSID(0, "$NetBSD: acpi.c,v 1.28 2003/01/07 18:46:48 fvdl Exp $");
     45 
     46 #include "opt_acpi.h"
     47 
     48 #include <sys/param.h>
     49 #include <sys/systm.h>
     50 #include <sys/device.h>
     51 #include <sys/malloc.h>
     52 
     53 #include <dev/acpi/acpica.h>
     54 #include <dev/acpi/acpireg.h>
     55 #include <dev/acpi/acpivar.h>
     56 #include <dev/acpi/acpi_osd.h>
     57 #ifdef ACPIVERBOSE
     58 #include <dev/acpi/acpidevs_data.h>
     59 #endif
     60 
     61 #ifndef ACPI_PCI_FIXUP
     62 #define ACPI_PCI_FIXUP 1
     63 #endif
     64 
     65 #ifndef ACPI_ACTIVATE_DEV
     66 #define ACPI_ACTIVATE_DEV 0
     67 #endif
     68 
     69 #if ACPI_PCI_FIXUP
     70 #include <dev/acpi/acpica/Subsystem/acnamesp.h> /* AcpiNsGetNodeByPath() */
     71 #include <dev/pci/pcidevs.h>
     72 #endif
     73 
     74 #include <machine/acpi_machdep.h>
     75 
     76 #ifdef ENABLE_DEBUGGER
     77 #define	ACPI_DBGR_INIT		0x01
     78 #define	ACPI_DBGR_TABLES	0x02
     79 #define	ACPI_DBGR_ENABLE	0x04
     80 #define	ACPI_DBGR_PROBE		0x08
     81 #define	ACPI_DBGR_RUNNING	0x10
     82 
     83 int	acpi_dbgr = 0x00;
     84 #endif
     85 
     86 int	acpi_match(struct device *, struct cfdata *, void *);
     87 void	acpi_attach(struct device *, struct device *, void *);
     88 
     89 int	acpi_print(void *aux, const char *);
     90 
     91 extern struct cfdriver acpi_cd;
     92 
     93 CFATTACH_DECL(acpi, sizeof(struct acpi_softc),
     94     acpi_match, acpi_attach, NULL, NULL);
     95 
     96 /*
     97  * This is a flag we set when the ACPI subsystem is active.  Machine
     98  * dependent code may wish to skip other steps (such as attaching
     99  * subsystems that ACPI supercedes) when ACPI is active.
    100  */
    101 int	acpi_active;
    102 
    103 /*
    104  * Pointer to the ACPI subsystem's state.  There can be only
    105  * one ACPI instance.
    106  */
    107 struct acpi_softc *acpi_softc;
    108 
    109 void		acpi_shutdown(void *);
    110 ACPI_STATUS	acpi_disable(struct acpi_softc *sc);
    111 void		acpi_build_tree(struct acpi_softc *);
    112 ACPI_STATUS	acpi_make_devnode(ACPI_HANDLE, UINT32, void *, void **);
    113 
    114 void		acpi_enable_fixed_events(struct acpi_softc *);
    115 #if ACPI_PCI_FIXUP
    116 void		acpi_pci_fixup(struct acpi_softc *);
    117 #endif
    118 #if ACPI_PCI_FIXUP || ACPI_ACTIVATE_DEV
    119 ACPI_STATUS	acpi_allocate_resources(ACPI_HANDLE handle);
    120 #endif
    121 
    122 /*
    123  * acpi_probe:
    124  *
    125  *	Probe for ACPI support.  This is called by the
    126  *	machine-dependent ACPI front-end.  All of the
    127  *	actual work is done by ACPICA.
    128  *
    129  *	NOTE: This is not an autoconfiguration interface function.
    130  */
    131 int
    132 acpi_probe(void)
    133 {
    134 	static int beenhere;
    135 	ACPI_STATUS rv;
    136 
    137 	if (beenhere != 0)
    138 		panic("acpi_probe: ACPI has already been probed");
    139 	beenhere = 1;
    140 
    141 	/*
    142 	 * Start up ACPICA.
    143 	 */
    144 #ifdef ENABLE_DEBUGGER
    145 	if (acpi_dbgr & ACPI_DBGR_INIT)
    146 		acpi_osd_debugger();
    147 #endif
    148 
    149 	rv = AcpiInitializeSubsystem();
    150 	if (rv != AE_OK) {
    151 		printf("ACPI: unable to initialize ACPICA: %d\n", rv);
    152 		return (0);
    153 	}
    154 
    155 #ifdef ENABLE_DEBUGGER
    156 	if (acpi_dbgr & ACPI_DBGR_TABLES)
    157 		acpi_osd_debugger();
    158 #endif
    159 
    160 	rv = AcpiLoadTables();
    161 	if (rv != AE_OK) {
    162 		printf("ACPI: unable to load tables: %d\n", rv);
    163 		return (0);
    164 	}
    165 
    166 	/*
    167 	 * Looks like we have ACPI!
    168 	 */
    169 
    170 	return (1);
    171 }
    172 
    173 /*
    174  * acpi_match:
    175  *
    176  *	Autoconfiguration `match' routine.
    177  */
    178 int
    179 acpi_match(struct device *parent, struct cfdata *match, void *aux)
    180 {
    181 	struct acpibus_attach_args *aa = aux;
    182 
    183 	if (strcmp(aa->aa_busname, acpi_cd.cd_name) != 0)
    184 		return (0);
    185 
    186 	/*
    187 	 * XXX Check other locators?  Hard to know -- machine
    188 	 * dependent code has already checked for the presence
    189 	 * of ACPI by calling acpi_probe(), so I suppose we
    190 	 * don't really have to do anything else.
    191 	 */
    192 	return (1);
    193 }
    194 
    195 /*
    196  * acpi_attach:
    197  *
    198  *	Autoconfiguration `attach' routine.  Finish initializing
    199  *	ACPICA (some initialization was done in acpi_probe(),
    200  *	which was required to check for the presence of ACPI),
    201  *	and enable the ACPI subsystem.
    202  */
    203 void
    204 acpi_attach(struct device *parent, struct device *self, void *aux)
    205 {
    206 	struct acpi_softc *sc = (void *) self;
    207 	struct acpibus_attach_args *aa = aux;
    208 	ACPI_STATUS rv;
    209 
    210 	printf("\n");
    211 
    212 	if (acpi_softc != NULL)
    213 		panic("acpi_attach: ACPI has already been attached");
    214 
    215 	sc->sc_iot = aa->aa_iot;
    216 	sc->sc_memt = aa->aa_memt;
    217 	sc->sc_pc = aa->aa_pc;
    218 	sc->sc_pciflags = aa->aa_pciflags;
    219 	sc->sc_ic = aa->aa_ic;
    220 
    221 	acpi_softc = sc;
    222 
    223 	/*
    224 	 * Install the default address space handlers.
    225 	 */
    226 
    227 	rv = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
    228 	    ACPI_ADR_SPACE_SYSTEM_MEMORY, ACPI_DEFAULT_HANDLER, NULL, NULL);
    229 	if (rv != AE_OK) {
    230 		printf("%s: unable to install SYSTEM MEMORY handler: %d\n",
    231 		    sc->sc_dev.dv_xname, rv);
    232 		return;
    233 	}
    234 
    235 	rv = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
    236 	    ACPI_ADR_SPACE_SYSTEM_IO, ACPI_DEFAULT_HANDLER, NULL, NULL);
    237 	if (rv != AE_OK) {
    238 		printf("%s: unable to install SYSTEM IO handler: %d\n",
    239 		    sc->sc_dev.dv_xname, rv);
    240 		return;
    241 	}
    242 
    243 	rv = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
    244 	    ACPI_ADR_SPACE_PCI_CONFIG, ACPI_DEFAULT_HANDLER, NULL, NULL);
    245 	if (rv != AE_OK) {
    246 		printf("%s: unable to install PCI CONFIG handler: %d\n",
    247 		    sc->sc_dev.dv_xname, rv);
    248 		return;
    249 	}
    250 
    251 	/*
    252 	 * Bring ACPI on-line.
    253 	 *
    254 	 * Note that we request that _STA (device init) and _INI (object init)
    255 	 * methods not be run.
    256 	 *
    257 	 * XXX We need to arrange for the object init pass after we have
    258 	 * XXX attached all of our children.
    259 	 */
    260 #ifdef ENABLE_DEBUGGER
    261 	if (acpi_dbgr & ACPI_DBGR_ENABLE)
    262 		acpi_osd_debugger();
    263 #endif
    264 	rv = AcpiEnableSubsystem(ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT);
    265 	if (rv != AE_OK) {
    266 		printf("%s: unable to enable ACPI: %d\n",
    267 		    sc->sc_dev.dv_xname, rv);
    268 		return;
    269 	}
    270 	acpi_active = 1;
    271 
    272 	/*
    273 	 * Set up the default sleep state to enter when various
    274 	 * switches are activated.
    275 	 */
    276 	sc->sc_switch_sleep[ACPI_SWITCH_POWERBUTTON] = ACPI_STATE_S5;
    277 	sc->sc_switch_sleep[ACPI_SWITCH_SLEEPBUTTON] = ACPI_STATE_S1;
    278 	sc->sc_switch_sleep[ACPI_SWITCH_LID]	     = ACPI_STATE_S1;
    279 
    280 	/* Our current state is "awake". */
    281 	sc->sc_sleepstate = ACPI_STATE_S0;
    282 
    283 	/* Show SCI interrupt. */
    284 	if (AcpiGbl_FADT != NULL)
    285 		printf("%s: SCI interrupting at irq %d\n",
    286 			sc->sc_dev.dv_xname, AcpiGbl_FADT->SciInt);
    287 	/*
    288 	 * Check for fixed-hardware features.
    289 	 */
    290 	acpi_enable_fixed_events(sc);
    291 
    292 	/*
    293 	 * Fix up PCI devices.
    294 	 */
    295 #if ACPI_PCI_FIXUP
    296 	acpi_pci_fixup(sc);
    297 #endif
    298 
    299 	/*
    300 	 * Scan the namespace and build our device tree.
    301 	 */
    302 #ifdef ENABLE_DEBUGGER
    303 	if (acpi_dbgr & ACPI_DBGR_PROBE)
    304 		acpi_osd_debugger();
    305 #endif
    306 	acpi_md_callback((struct device *)sc);
    307 	acpi_build_tree(sc);
    308 
    309 	/*
    310 	 * Register a shutdown hook that disables certain ACPI
    311 	 * events that might happen and confuse us while we're
    312 	 * trying to shut down.
    313 	 */
    314 	sc->sc_sdhook = shutdownhook_establish(acpi_shutdown, sc);
    315 	if (sc->sc_sdhook == NULL)
    316 		printf("%s: WARNING: unable to register shutdown hook\n",
    317 		    sc->sc_dev.dv_xname);
    318 
    319 #ifdef ENABLE_DEBUGGER
    320 	if (acpi_dbgr & ACPI_DBGR_RUNNING)
    321 		acpi_osd_debugger();
    322 #endif
    323 }
    324 
    325 /*
    326  * acpi_shutdown:
    327  *
    328  *	Shutdown hook for ACPI -- disable some events that
    329  *	might confuse us.
    330  */
    331 void
    332 acpi_shutdown(void *arg)
    333 {
    334 	struct acpi_softc *sc = arg;
    335 
    336 	if (acpi_disable(sc) != AE_OK)
    337 		printf("%s: WARNING: unable to disable ACPI\n",
    338 		    sc->sc_dev.dv_xname);
    339 }
    340 
    341 /*
    342  * acpi_disable:
    343  *
    344  *	Disable ACPI.
    345  */
    346 ACPI_STATUS
    347 acpi_disable(struct acpi_softc *sc)
    348 {
    349 	ACPI_STATUS rv = AE_OK;
    350 
    351 	if (acpi_active) {
    352 		rv = AcpiDisable();
    353 		if (rv == AE_OK)
    354 			acpi_active = 0;
    355 	}
    356 	return (rv);
    357 }
    358 
    359 struct acpi_make_devnode_state {
    360 	struct acpi_softc *softc;
    361 	struct acpi_scope *scope;
    362 };
    363 
    364 /*
    365  * acpi_build_tree:
    366  *
    367  *	Scan relevant portions of the ACPI namespace and attach
    368  *	child devices.
    369  */
    370 void
    371 acpi_build_tree(struct acpi_softc *sc)
    372 {
    373 	static const char *scopes[] = {
    374 		"\\_PR_",	/* ACPI 1.0 processor namespace */
    375 		"\\_SB_",	/* system bus namespace */
    376 		"\\_SI_",	/* system idicator namespace */
    377 		"\\_TZ_",	/* ACPI 1.0 thermal zone namespace */
    378 		NULL,
    379 	};
    380 	struct acpi_attach_args aa;
    381 	struct acpi_make_devnode_state state;
    382 	struct acpi_scope *as;
    383 	struct acpi_devnode *ad;
    384 	ACPI_HANDLE parent;
    385 	int i;
    386 
    387 	TAILQ_INIT(&sc->sc_scopes);
    388 
    389 	state.softc = sc;
    390 
    391 	/*
    392 	 * Scan the namespace and build our tree.
    393 	 */
    394 	for (i = 0; scopes[i] != NULL; i++) {
    395 		as = malloc(sizeof(*as), M_DEVBUF, M_WAITOK);
    396 		as->as_name = scopes[i];
    397 		TAILQ_INIT(&as->as_devnodes);
    398 
    399 		TAILQ_INSERT_TAIL(&sc->sc_scopes, as, as_list);
    400 
    401 		state.scope = as;
    402 
    403 		if (AcpiGetHandle(ACPI_ROOT_OBJECT, (char *) scopes[i],
    404 		    &parent) == AE_OK) {
    405 			AcpiWalkNamespace(ACPI_TYPE_ANY, parent, 100,
    406 			    acpi_make_devnode, &state, NULL);
    407 		}
    408 
    409 		/* Now, for this namespace, try and attach the devices. */
    410 		TAILQ_FOREACH(ad, &as->as_devnodes, ad_list) {
    411 			aa.aa_node = ad;
    412 			aa.aa_iot = sc->sc_iot;
    413 			aa.aa_memt = sc->sc_memt;
    414 			aa.aa_pc = sc->sc_pc;
    415 			aa.aa_pciflags = sc->sc_pciflags;
    416 			aa.aa_ic = sc->sc_ic;
    417 
    418 			if (ad->ad_devinfo.Type == ACPI_TYPE_DEVICE) {
    419 				/*
    420 				 * XXX We only attach devices which are:
    421 				 *
    422 				 *	- present
    423 				 *	- enabled
    424 				 *	- functioning properly
    425 				 *
    426 				 * However, if enabled, it's decoding resources,
    427 				 * so we should claim them, if possible.
    428 				 * Requires changes to bus_space(9).
    429 				 */
    430 				if ((ad->ad_devinfo.CurrentStatus &
    431 				     (ACPI_STA_DEV_PRESENT|ACPI_STA_DEV_ENABLED|
    432 				      ACPI_STA_DEV_OK)) !=
    433 				    (ACPI_STA_DEV_PRESENT|ACPI_STA_DEV_ENABLED|
    434 				     ACPI_STA_DEV_OK))
    435 					continue;
    436 
    437 				/*
    438 				 * XXX Same problem as above...
    439 				 */
    440 				if ((ad->ad_devinfo.Valid & ACPI_VALID_HID)
    441 				    == 0)
    442 					continue;
    443 			}
    444 
    445 			ad->ad_device = config_found(&sc->sc_dev,
    446 			    &aa, acpi_print);
    447 		}
    448 	}
    449 }
    450 
    451 #if ACPI_ACTIVATE_DEV
    452 static void
    453 acpi_activate_device(ACPI_HANDLE handle, ACPI_DEVICE_INFO *di)
    454 {
    455 	ACPI_STATUS rv;
    456 
    457 #ifdef ACPI_DEBUG
    458 	printf("acpi_activate_device: %s, old status=%x\n",
    459 	       di->HardwareId, di->CurrentStatus);
    460 #endif
    461 
    462 	rv = acpi_allocate_resources(handle);
    463 	if (ACPI_FAILURE(rv)) {
    464 		printf("acpi: activate failed for %s\n", di->HardwareId);
    465 	} else {
    466 		printf("acpi: activated %s\n", di->HardwareId);
    467 	}
    468 
    469 	(void)AcpiGetObjectInfo(handle, di);
    470 #ifdef ACPI_DEBUG
    471 	printf("acpi_activate_device: %s, new status=%x\n",
    472 	       di->HardwareId, di->CurrentStatus);
    473 #endif
    474 }
    475 #endif /* ACPI_ACTIVATE_DEV */
    476 
    477 /*
    478  * acpi_make_devnode:
    479  *
    480  *	Make an ACPI devnode.
    481  */
    482 ACPI_STATUS
    483 acpi_make_devnode(ACPI_HANDLE handle, UINT32 level, void *context,
    484     void **status)
    485 {
    486 	struct acpi_make_devnode_state *state = context;
    487 #if defined(ACPI_DEBUG) || defined(ACPI_EXTRA_DEBUG)
    488 	struct acpi_softc *sc = state->softc;
    489 #endif
    490 	struct acpi_scope *as = state->scope;
    491 	struct acpi_devnode *ad;
    492 	ACPI_DEVICE_INFO devinfo;
    493 	ACPI_OBJECT_TYPE type;
    494 	ACPI_STATUS rv;
    495 
    496 	if (AcpiGetType(handle, &type) == AE_OK) {
    497 		rv = AcpiGetObjectInfo(handle, &devinfo);
    498 		if (rv != AE_OK) {
    499 #ifdef ACPI_DEBUG
    500 			printf("%s: AcpiGetObjectInfo failed\n",
    501 			    sc->sc_dev.dv_xname);
    502 #endif
    503 			goto out; /* XXX why return OK */
    504 		}
    505 
    506 		switch (type) {
    507 		case ACPI_TYPE_DEVICE:
    508 #if ACPI_ACTIVATE_DEV
    509 			if ((devinfo.Valid & ACPI_VALID_STA) &&
    510 			    (devinfo.CurrentStatus &
    511 			     (ACPI_STA_DEV_PRESENT|ACPI_STA_DEV_ENABLED)) ==
    512 			    ACPI_STA_DEV_PRESENT)
    513 				acpi_activate_device(handle, &devinfo);
    514 			/* FALLTHROUGH */
    515 #endif
    516 
    517 		case ACPI_TYPE_PROCESSOR:
    518 		case ACPI_TYPE_THERMAL:
    519 		case ACPI_TYPE_POWER:
    520 			ad = malloc(sizeof(*ad), M_DEVBUF, M_NOWAIT|M_ZERO);
    521 			if (ad == NULL)
    522 				return (AE_NO_MEMORY);
    523 
    524 			ad->ad_handle = handle;
    525 			ad->ad_level = level;
    526 			ad->ad_scope = as;
    527 			ad->ad_type = type;
    528 
    529 			TAILQ_INSERT_TAIL(&as->as_devnodes, ad, ad_list);
    530 
    531 			rv = AcpiGetObjectInfo(handle, &ad->ad_devinfo);
    532 			if (rv != AE_OK)
    533 				goto out;
    534 
    535 			if ((ad->ad_devinfo.Valid & ACPI_VALID_HID) == 0)
    536 				goto out;
    537 
    538 #ifdef ACPI_EXTRA_DEBUG
    539 			printf("%s: HID %s found in scope %s level %d\n",
    540 			    sc->sc_dev.dv_xname, ad->ad_devinfo.HardwareId,
    541 			    as->as_name, ad->ad_level);
    542 			if (ad->ad_devinfo.Valid & ACPI_VALID_UID)
    543 				printf("       UID %s\n",
    544 				    ad->ad_devinfo.UniqueId);
    545 			if (ad->ad_devinfo.Valid & ACPI_VALID_ADR)
    546 				printf("       ADR 0x%016qx\n",
    547 				    ad->ad_devinfo.Address);
    548 			if (ad->ad_devinfo.Valid & ACPI_VALID_STA)
    549 				printf("       STA 0x%08x\n",
    550 				    ad->ad_devinfo.CurrentStatus);
    551 #endif
    552 		}
    553 	}
    554  out:
    555 	return (AE_OK);
    556 }
    557 
    558 /*
    559  * acpi_print:
    560  *
    561  *	Autoconfiguration print routine.
    562  */
    563 int
    564 acpi_print(void *aux, const char *pnp)
    565 {
    566 	struct acpi_attach_args *aa = aux;
    567 
    568 	if (pnp) {
    569 		if (aa->aa_node->ad_devinfo.Valid & ACPI_VALID_HID) {
    570 			char *pnpstr = aa->aa_node->ad_devinfo.HardwareId;
    571 			char *str;
    572 
    573 			aprint_normal("%s ", pnpstr);
    574 			if (acpi_eval_string(aa->aa_node->ad_handle,
    575 			    "_STR", &str) == AE_OK) {
    576 				aprint_normal("[%s] ", str);
    577 				AcpiOsFree(str);
    578 			}
    579 #ifdef ACPIVERBOSE
    580 			else {
    581 				int i;
    582 
    583 				for (i = 0; i < sizeof(acpi_knowndevs) /
    584 				    sizeof(acpi_knowndevs[0]); i++) {
    585 					if (strcmp(acpi_knowndevs[i].pnp,
    586 					    pnpstr) == 0) {
    587 						printf("[%s] ",
    588 						    acpi_knowndevs[i].str);
    589 					}
    590 				}
    591 			}
    592 
    593 #endif
    594 		} else {
    595 			/* XXX print something more meaningful.. */
    596 			aprint_normal("ACPI Object Type 0x%02x ",
    597 			   aa->aa_node->ad_devinfo.Type);
    598 		}
    599 		aprint_normal("at %s", pnp);
    600 	} else {
    601 		aprint_normal(" (%s", aa->aa_node->ad_devinfo.HardwareId);
    602 		if (aa->aa_node->ad_devinfo.Valid & ACPI_VALID_UID) {
    603 			char *uid;
    604 
    605 			if (aa->aa_node->ad_devinfo.UniqueId[0] == '\0')
    606 				uid = "<null>";
    607 			else
    608 				uid = aa->aa_node->ad_devinfo.UniqueId;
    609 			aprint_normal("-%s", uid);
    610 		}
    611 		aprint_normal(")");
    612 	}
    613 
    614 	return (UNCONF);
    615 }
    616 
    617 /*****************************************************************************
    618  * ACPI fixed-hardware feature handlers
    619  *****************************************************************************/
    620 
    621 UINT32		acpi_fixed_power_button_handler(void *);
    622 UINT32		acpi_fixed_sleep_button_handler(void *);
    623 
    624 /*
    625  * acpi_enable_fixed_events:
    626  *
    627  *	Enable any fixed-hardware feature handlers.
    628  */
    629 void
    630 acpi_enable_fixed_events(struct acpi_softc *sc)
    631 {
    632 	static int beenhere;
    633 	ACPI_STATUS rv;
    634 
    635 	/*
    636 	 * Check for fixed-hardware buttons.
    637 	 */
    638 
    639 	if (AcpiGbl_FADT != NULL && AcpiGbl_FADT->PwrButton == 0) {
    640 		if (beenhere == 0)
    641 			printf("%s: fixed-feature power button present\n",
    642 			    sc->sc_dev.dv_xname);
    643 		rv = AcpiInstallFixedEventHandler(ACPI_EVENT_POWER_BUTTON,
    644 		    acpi_fixed_power_button_handler, sc);
    645 		if (rv != AE_OK)
    646 			printf("%s: unable to install handler for fixed "
    647 			    "power button: %d\n", sc->sc_dev.dv_xname, rv);
    648 	}
    649 
    650 	if (AcpiGbl_FADT != NULL && AcpiGbl_FADT->SleepButton == 0) {
    651 		if (beenhere == 0)
    652 			printf("%s: fixed-feature sleep button present\n",
    653 			    sc->sc_dev.dv_xname);
    654 		rv = AcpiInstallFixedEventHandler(ACPI_EVENT_SLEEP_BUTTON,
    655 		    acpi_fixed_sleep_button_handler, sc);
    656 		if (rv != AE_OK)
    657 			printf("%s: unable to install handler for fixed "
    658 			    "power button: %d\n", sc->sc_dev.dv_xname, rv);
    659 	}
    660 
    661 	beenhere = 1;
    662 }
    663 
    664 /*
    665  * acpi_fixed_power_button_handler:
    666  *
    667  *	Fixed event handler for the power button.
    668  */
    669 UINT32
    670 acpi_fixed_power_button_handler(void *context)
    671 {
    672 	struct acpi_softc *sc = context;
    673 
    674 	/* XXX XXX XXX */
    675 
    676 	printf("%s: fixed power button pressed\n", sc->sc_dev.dv_xname);
    677 
    678 	return (ACPI_INTERRUPT_HANDLED);
    679 }
    680 
    681 /*
    682  * acpi_fixed_sleep_button_handler:
    683  *
    684  *	Fixed event handler for the sleep button.
    685  */
    686 UINT32
    687 acpi_fixed_sleep_button_handler(void *context)
    688 {
    689 	struct acpi_softc *sc = context;
    690 
    691 	/* XXX XXX XXX */
    692 
    693 	printf("%s: fixed sleep button pressed\n", sc->sc_dev.dv_xname);
    694 
    695 	return (ACPI_INTERRUPT_HANDLED);
    696 }
    697 
    698 /*****************************************************************************
    699  * ACPI utility routines.
    700  *****************************************************************************/
    701 
    702 /*
    703  * acpi_eval_integer:
    704  *
    705  *	Evaluate an integer object.
    706  */
    707 ACPI_STATUS
    708 acpi_eval_integer(ACPI_HANDLE handle, char *path, int *valp)
    709 {
    710 	ACPI_STATUS rv;
    711 	ACPI_BUFFER buf;
    712 	ACPI_OBJECT param;
    713 
    714 	if (handle == NULL)
    715 		handle = ACPI_ROOT_OBJECT;
    716 
    717 	buf.Pointer = &param;
    718 	buf.Length = sizeof(param);
    719 
    720 	rv = AcpiEvaluateObject(handle, path, NULL, &buf);
    721 	if (rv == AE_OK) {
    722 		if (param.Type == ACPI_TYPE_INTEGER)
    723 			*valp = param.Integer.Value;
    724 		else
    725 			rv = AE_TYPE;
    726 	}
    727 
    728 	return (rv);
    729 }
    730 
    731 /*
    732  * acpi_eval_string:
    733  *
    734  *	Evaluate a (Unicode) string object.
    735  */
    736 ACPI_STATUS
    737 acpi_eval_string(ACPI_HANDLE handle, char *path, char **stringp)
    738 {
    739 	ACPI_STATUS rv;
    740 	ACPI_BUFFER buf;
    741 	ACPI_OBJECT *param;
    742 
    743 	if (handle == NULL)
    744 		handle = ACPI_ROOT_OBJECT;
    745 
    746 	buf.Pointer = NULL;
    747 	buf.Length = 0;
    748 
    749 	rv = AcpiEvaluateObject(handle, path, NULL, &buf);
    750 	if (rv != AE_BUFFER_OVERFLOW)
    751 		return (rv);
    752 
    753 	buf.Pointer = AcpiOsAllocate(buf.Length);
    754 	if (buf.Pointer == NULL)
    755 		return (AE_NO_MEMORY);
    756 
    757 	rv = AcpiEvaluateObject(handle, path, NULL, &buf);
    758 	param = (ACPI_OBJECT *)buf.Pointer;
    759 	if (rv == AE_OK) {
    760 		if (param->Type == ACPI_TYPE_STRING) {
    761 			char *ptr = param->String.Pointer;
    762 			size_t len;
    763 			while (*ptr++)
    764 				continue;
    765 			len = ptr - param->String.Pointer;
    766 			if ((*stringp = AcpiOsAllocate(len)) == NULL) {
    767 				rv = AE_NO_MEMORY;
    768 				goto done;
    769 			}
    770 			(void)memcpy(*stringp, param->String.Pointer, len);
    771 			goto done;
    772 		}
    773 		rv = AE_TYPE;
    774 	}
    775 done:
    776 	AcpiOsFree(buf.Pointer);
    777 	return (rv);
    778 }
    779 
    780 
    781 /*
    782  * acpi_eval_struct:
    783  *
    784  *	Evaluate a more complex structure.  Caller must free buf.Pointer.
    785  */
    786 ACPI_STATUS
    787 acpi_eval_struct(ACPI_HANDLE handle, char *path, ACPI_BUFFER *bufp)
    788 {
    789 	ACPI_STATUS rv;
    790 
    791 	if (handle == NULL)
    792 		handle = ACPI_ROOT_OBJECT;
    793 
    794 	bufp->Pointer = NULL;
    795 	bufp->Length = 0;
    796 
    797 	rv = AcpiEvaluateObject(handle, path, NULL, bufp);
    798 	if (rv != AE_BUFFER_OVERFLOW)
    799 		return (rv);
    800 
    801 	bufp->Pointer = AcpiOsAllocate(bufp->Length);
    802 	if (bufp->Pointer == NULL)
    803 		return (AE_NO_MEMORY);
    804 
    805 	rv = AcpiEvaluateObject(handle, path, NULL, bufp);
    806 
    807 	return (rv);
    808 }
    809 
    810 /*
    811  * acpi_get:
    812  *
    813  *	Fetch data info the specified (empty) ACPI buffer.
    814  */
    815 ACPI_STATUS
    816 acpi_get(ACPI_HANDLE handle, ACPI_BUFFER *buf,
    817     ACPI_STATUS (*getit)(ACPI_HANDLE, ACPI_BUFFER *))
    818 {
    819 	ACPI_STATUS rv;
    820 
    821 	buf->Pointer = NULL;
    822 	buf->Length = 0;
    823 
    824 	rv = (*getit)(handle, buf);
    825 	if (rv != AE_BUFFER_OVERFLOW)
    826 		return (rv);
    827 
    828 	buf->Pointer = AcpiOsAllocate(buf->Length);
    829 	if (buf->Pointer == NULL)
    830 		return (AE_NO_MEMORY);
    831 	memset(buf->Pointer, 0, buf->Length);
    832 
    833 	return ((*getit)(handle, buf));
    834 }
    835 
    836 
    837 /*****************************************************************************
    838  * ACPI sleep support.
    839  *****************************************************************************/
    840 
    841 static int
    842 is_available_state(struct acpi_softc *sc, int state)
    843 {
    844 	UINT8 type_a, type_b;
    845 
    846 	return (ACPI_SUCCESS(AcpiGetSleepTypeData((UINT8)state,
    847 						  &type_a, &type_b)));
    848 }
    849 
    850 /*
    851  * acpi_enter_sleep_state:
    852  *
    853  *	enter to the specified sleep state.
    854  */
    855 
    856 ACPI_STATUS
    857 acpi_enter_sleep_state(struct acpi_softc *sc, int state)
    858 {
    859 	int s;
    860 	ACPI_STATUS ret = AE_OK;
    861 
    862 	switch (state) {
    863 	case ACPI_STATE_S0:
    864 		break;
    865 	case ACPI_STATE_S1:
    866 	case ACPI_STATE_S2:
    867 	case ACPI_STATE_S3:
    868 	case ACPI_STATE_S4:
    869 		if (!is_available_state(sc, state)) {
    870 			printf("acpi: cannot enter the sleep state (%d).\n",
    871 			       state);
    872 			break;
    873 		}
    874 		ret = AcpiEnterSleepStatePrep(state);
    875 		if (ACPI_FAILURE(ret)) {
    876 			printf("acpi: failed preparing to sleep (%s)\n",
    877 			       AcpiFormatException(ret));
    878 			break;
    879 		}
    880 		if (state==ACPI_STATE_S1) {
    881 			/* just enter the state */
    882 			acpi_md_OsDisableInterrupt();
    883 			AcpiEnterSleepState((UINT8)state);
    884 			AcpiUtReleaseMutex(ACPI_MTX_HARDWARE);
    885 		} else {
    886 			/* XXX: powerhooks(9) framework is too poor to
    887 			 * support ACPI sleep state...
    888 			 */
    889 			dopowerhooks(PWR_SOFTSUSPEND);
    890 			s = splhigh();
    891 			dopowerhooks(PWR_SUSPEND);
    892 			acpi_md_sleep(state);
    893 			dopowerhooks(PWR_RESUME);
    894 			splx(s);
    895 			dopowerhooks(PWR_SOFTRESUME);
    896 			if (state==ACPI_STATE_S4)
    897 				AcpiEnable();
    898 		}
    899 		AcpiLeaveSleepState((UINT8)state);
    900 		break;
    901 	case ACPI_STATE_S5:
    902 		AcpiEnterSleepStatePrep(ACPI_STATE_S5);
    903 		acpi_md_OsDisableInterrupt();
    904 		AcpiEnterSleepState(ACPI_STATE_S5);
    905 		printf("WARNING: powerdown failed!\n");
    906 		break;
    907 	}
    908 
    909 	return (ret);
    910 }
    911 
    912 #if ACPI_PCI_FIXUP
    913 ACPI_STATUS acpi_pci_fixup_bus(ACPI_HANDLE, UINT32, void *, void **);
    914 /*
    915  * acpi_pci_fixup:
    916  *
    917  *	Set up PCI devices that BIOS didn't handle right.
    918  *	Iterate through all devices and try to get the _PTR
    919  *	(PCI Routing Table).  If it exists then make sure all
    920  *	interrupt links that it uses are working.
    921  */
    922 void
    923 acpi_pci_fixup(struct acpi_softc *sc)
    924 {
    925 	ACPI_HANDLE parent;
    926 
    927 #ifdef ACPI_DEBUG
    928 	printf("acpi_pci_fixup starts:\n");
    929 #endif
    930 	if (AcpiGetHandle(ACPI_ROOT_OBJECT, "\\_SB_", &parent) != AE_OK)
    931 		return;
    932 	sc->sc_pci_bus = 0;
    933 	AcpiWalkNamespace(ACPI_TYPE_DEVICE, parent, 100,
    934 	    acpi_pci_fixup_bus, sc, NULL);
    935 }
    936 
    937 static ACPI_HANDLE
    938 acpi_get_node(char *name)
    939 {
    940 	ACPI_NAMESPACE_NODE *ObjDesc;
    941 	ACPI_STATUS Status;
    942 
    943 	Status = AcpiNsGetNodeByPath(name, NULL, 0, &ObjDesc);
    944 	if (ACPI_FAILURE (Status)) {
    945 		printf("acpi_get_node: could not find: %s\n",
    946 		       AcpiFormatException (Status));
    947 		return NULL;
    948 	}
    949 	return ObjDesc;
    950 }
    951 
    952 static uint
    953 acpi_get_intr(ACPI_HANDLE handle)
    954 {
    955 	ACPI_BUFFER ret;
    956 	ACPI_STATUS rv;
    957 	ACPI_RESOURCE *res;
    958 	ACPI_RESOURCE_IRQ *irq;
    959 	uint intr;
    960 
    961 	intr = -1;
    962 	rv = acpi_get(handle, &ret, AcpiGetCurrentResources);
    963 	if (ACPI_FAILURE(rv))
    964 		return (intr);
    965 	for (res = ret.Pointer; res->Id != ACPI_RSTYPE_END_TAG;
    966 	     res = ACPI_NEXT_RESOURCE(res)) {
    967 		if (res->Id == ACPI_RSTYPE_IRQ) {
    968 			irq = (ACPI_RESOURCE_IRQ *)&res->Data;
    969 			if (irq->NumberOfInterrupts == 1)
    970 				intr = irq->Interrupts[0];
    971 			break;
    972 		}
    973 	}
    974 	free(ret.Pointer, M_DEVBUF);
    975 	return (intr);
    976 }
    977 
    978 static void
    979 acpi_pci_set_line(int bus, int dev, int pin, int line)
    980 {
    981 	ACPI_STATUS err;
    982 	ACPI_PCI_ID pid;
    983 	UINT32 intr, id, bhlc;
    984 	int func, nfunc;
    985 
    986 	pid.Bus = bus;
    987 	pid.Device = dev;
    988 	pid.Function = 0;
    989 
    990 	err = AcpiOsReadPciConfiguration(&pid, PCI_BHLC_REG, &bhlc, 32);
    991 	if (err)
    992 		return;
    993 	if (PCI_HDRTYPE_MULTIFN(bhlc))
    994 		nfunc = 8;
    995 	else
    996 		nfunc = 1;
    997 
    998 	for (func = 0; func < nfunc; func++) {
    999 		pid.Function = func;
   1000 
   1001 		err = AcpiOsReadPciConfiguration(&pid, PCI_ID_REG, &id, 32);
   1002 		if (err || PCI_VENDOR(id) == PCI_VENDOR_INVALID ||
   1003 		    PCI_VENDOR(id) == 0)
   1004 			continue;
   1005 
   1006 		err = AcpiOsReadPciConfiguration(&pid, PCI_INTERRUPT_REG,
   1007 			  &intr, 32);
   1008 		if (err) {
   1009 			printf("AcpiOsReadPciConfiguration failed %d\n", err);
   1010 			return;
   1011 		}
   1012 		if (pin == PCI_INTERRUPT_PIN(intr) &&
   1013 		    line != PCI_INTERRUPT_LINE(intr)) {
   1014 #ifdef ACPI_DEBUG
   1015 			printf("acpi fixup pci intr: %d:%d:%d %c: %d -> %d\n",
   1016 			       bus, dev, func,
   1017 			       pin + '@', PCI_INTERRUPT_LINE(intr),
   1018 			       line);
   1019 #endif
   1020 			intr &= ~(PCI_INTERRUPT_LINE_MASK <<
   1021 				  PCI_INTERRUPT_LINE_SHIFT);
   1022 			intr |= line << PCI_INTERRUPT_LINE_SHIFT;
   1023 			err = AcpiOsWritePciConfiguration(&pid,
   1024 				  PCI_INTERRUPT_REG, intr, 32);
   1025 			if (err) {
   1026 				printf("AcpiOsWritePciConfiguration failed"
   1027 				       " %d\n", err);
   1028 				return;
   1029 			}
   1030 		}
   1031 	}
   1032 }
   1033 
   1034 ACPI_STATUS
   1035 acpi_pci_fixup_bus(ACPI_HANDLE handle, UINT32 level, void *context,
   1036 		   void **status)
   1037 {
   1038 	struct acpi_softc *sc = context;
   1039 	ACPI_STATUS rv;
   1040 	ACPI_BUFFER buf;
   1041 	UINT8 *Buffer;
   1042 	ACPI_PCI_ROUTING_TABLE *PrtElement;
   1043 	ACPI_HANDLE link;
   1044 	uint line;
   1045 	ACPI_NAMESPACE_NODE *node;
   1046 	ACPI_INTEGER val;
   1047 
   1048 	rv = acpi_get(handle, &buf, AcpiGetIrqRoutingTable);
   1049 	if (ACPI_FAILURE(rv))
   1050 		return (AE_OK);
   1051 
   1052 	/*
   1053 	 * If at level 1, this is a PCI root bus. Try the _BBN method
   1054 	 * to get the right PCI bus numbering for the following
   1055 	 * busses (this is a depth-first walk). It may fail,
   1056 	 * for example if there's only one root bus, but that
   1057 	 * case should be ok, so we'll ignore that.
   1058 	 */
   1059 	if (level == 1) {
   1060 		node = AcpiNsMapHandleToNode(handle);
   1061 		rv = AcpiUtEvaluateNumericObject(METHOD_NAME__BBN, node, &val);
   1062 		if (!ACPI_FAILURE(rv)) {
   1063 #ifdef ACPI_DEBUG
   1064 			printf("%s: fixup: _BBN success, bus # was %d now %d\n",
   1065 			    sc->sc_dev.dv_xname, sc->sc_pci_bus,
   1066 			    ACPI_LOWORD(val));
   1067 #endif
   1068 			sc->sc_pci_bus = ACPI_LOWORD(val);
   1069 		}
   1070 	}
   1071 
   1072 
   1073 #ifdef ACPI_DEBUG
   1074 	printf("%s: fixing up PCI bus %d at level %u\n", sc->sc_dev.dv_xname,
   1075 	    sc->sc_pci_bus, level);
   1076 #endif
   1077 
   1078         for (Buffer = buf.Pointer; ; Buffer += PrtElement->Length) {
   1079 		PrtElement = (ACPI_PCI_ROUTING_TABLE *)Buffer;
   1080 		if (PrtElement->Length == 0)
   1081 			break;
   1082 		if (PrtElement->Source[0] == 0)
   1083 			continue;
   1084 
   1085 		link = acpi_get_node(PrtElement->Source);
   1086 		if (link == NULL)
   1087 			continue;
   1088 		line = acpi_get_intr(link);
   1089 		if (line == -1) {
   1090 #ifdef ACPI_DEBUG
   1091 			printf("%s: fixing up intr link %s\n",
   1092 			    sc->sc_dev.dv_xname, PrtElement->Source);
   1093 #endif
   1094 			rv = acpi_allocate_resources(link);
   1095 			if (ACPI_FAILURE(rv)) {
   1096 				printf("%s: interrupt allocation failed %s\n",
   1097 				    sc->sc_dev.dv_xname, PrtElement->Source);
   1098 				continue;
   1099 			}
   1100 			line = acpi_get_intr(link);
   1101 			if (line == -1) {
   1102 				printf("%s: get intr failed %s\n",
   1103 				    sc->sc_dev.dv_xname, PrtElement->Source);
   1104 				continue;
   1105 			}
   1106 		}
   1107 
   1108 		acpi_pci_set_line(sc->sc_pci_bus, PrtElement->Address >> 16,
   1109 		    PrtElement->Pin + 1, line);
   1110 	}
   1111 
   1112 	sc->sc_pci_bus++;
   1113 
   1114 	free(buf.Pointer, M_DEVBUF);
   1115 	return (AE_OK);
   1116 }
   1117 #endif /* ACPI_PCI_FIXUP */
   1118 
   1119 #if ACPI_PCI_FIXUP || ACPI_ACTIVATE_DEV
   1120 /* XXX This very incomplete */
   1121 ACPI_STATUS
   1122 acpi_allocate_resources(ACPI_HANDLE handle)
   1123 {
   1124 	ACPI_BUFFER bufp, bufc, bufn;
   1125 	ACPI_RESOURCE *resp, *resc, *resn;
   1126 	ACPI_RESOURCE_IRQ *irq;
   1127 	ACPI_STATUS rv;
   1128 	uint delta;
   1129 
   1130 	rv = acpi_get(handle, &bufp, AcpiGetPossibleResources);
   1131 	if (ACPI_FAILURE(rv))
   1132 		goto out;
   1133 	rv = acpi_get(handle, &bufc, AcpiGetCurrentResources);
   1134 	if (ACPI_FAILURE(rv)) {
   1135 		goto out1;
   1136 	}
   1137 
   1138 	bufn.Length = 1000;
   1139 	bufn.Pointer = resn = malloc(bufn.Length, M_DEVBUF, M_WAITOK);
   1140 	resp = bufp.Pointer;
   1141 	resc = bufc.Pointer;
   1142 	while (resc->Id != ACPI_RSTYPE_END_TAG &&
   1143 	       resp->Id != ACPI_RSTYPE_END_TAG) {
   1144 		while (resc->Id != resp->Id && resp->Id != ACPI_RSTYPE_END_TAG)
   1145 			resp = ACPI_NEXT_RESOURCE(resp);
   1146 		if (resp->Id == ACPI_RSTYPE_END_TAG)
   1147 			break;
   1148 		/* Found identical Id */
   1149 		resn->Id = resc->Id;
   1150 		switch (resc->Id) {
   1151 		case ACPI_RSTYPE_IRQ:
   1152 			memcpy(&resn->Data, &resp->Data,
   1153 			       sizeof(ACPI_RESOURCE_IRQ));
   1154 			irq = (ACPI_RESOURCE_IRQ *)&resn->Data;
   1155 			irq->Interrupts[0] =
   1156 			    ((ACPI_RESOURCE_IRQ *)&resp->Data)->
   1157 			        Interrupts[irq->NumberOfInterrupts-1];
   1158 			irq->NumberOfInterrupts = 1;
   1159 			resn->Length = ACPI_SIZEOF_RESOURCE(ACPI_RESOURCE_IRQ);
   1160 			break;
   1161 		case ACPI_RSTYPE_IO:
   1162 			memcpy(&resn->Data, &resp->Data,
   1163 			       sizeof(ACPI_RESOURCE_IO));
   1164 			resn->Length = resp->Length;
   1165 			break;
   1166 		default:
   1167 			printf("acpi_allocate_resources: res=%d\n", resc->Id);
   1168 			rv = AE_BAD_DATA;
   1169 			goto out2;
   1170 		}
   1171 		resc = ACPI_NEXT_RESOURCE(resc);
   1172 		resn = ACPI_NEXT_RESOURCE(resn);
   1173 		delta = (UINT8 *)resn - (UINT8 *)bufn.Pointer;
   1174 		if (delta >=
   1175 		    bufn.Length-ACPI_SIZEOF_RESOURCE(ACPI_RESOURCE_DATA)) {
   1176 			bufn.Length *= 2;
   1177 			bufn.Pointer = realloc(bufn.Pointer, bufn.Length,
   1178 					       M_DEVBUF, M_WAITOK);
   1179 			resn = (ACPI_RESOURCE *)((UINT8 *)bufn.Pointer + delta);
   1180 		}
   1181 	}
   1182 	if (resc->Id != ACPI_RSTYPE_END_TAG) {
   1183 		printf("acpi_allocate_resources: resc not exhausted\n");
   1184 		rv = AE_BAD_DATA;
   1185 		goto out3;
   1186 	}
   1187 
   1188 	resn->Id = ACPI_RSTYPE_END_TAG;
   1189 	rv = AcpiSetCurrentResources(handle, &bufn);
   1190 	if (ACPI_FAILURE(rv)) {
   1191 		printf("acpi_allocate_resources: AcpiSetCurrentResources %s\n",
   1192 		       AcpiFormatException(rv));
   1193 	}
   1194 
   1195 out3:
   1196 	free(bufn.Pointer, M_DEVBUF);
   1197 out2:
   1198 	free(bufc.Pointer, M_DEVBUF);
   1199 out1:
   1200 	free(bufp.Pointer, M_DEVBUF);
   1201 out:
   1202 	return rv;
   1203 }
   1204 #endif /* ACPI_PCI_FIXUP || ACPI_ACTIVATE_DEV */
   1205