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