Home | History | Annotate | Line # | Download | only in acpi
acpi.c revision 1.272
      1 /*	$NetBSD: acpi.c,v 1.272 2018/10/11 22:58:36 jmcneill Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2003, 2007 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  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * Copyright (c) 2003 Wasabi Systems, Inc.
     34  * All rights reserved.
     35  *
     36  * Written by Frank van der Linden for Wasabi Systems, Inc.
     37  *
     38  * Redistribution and use in source and binary forms, with or without
     39  * modification, are permitted provided that the following conditions
     40  * are met:
     41  * 1. Redistributions of source code must retain the above copyright
     42  *    notice, this list of conditions and the following disclaimer.
     43  * 2. Redistributions in binary form must reproduce the above copyright
     44  *    notice, this list of conditions and the following disclaimer in the
     45  *    documentation and/or other materials provided with the distribution.
     46  * 3. All advertising materials mentioning features or use of this software
     47  *    must display the following acknowledgement:
     48  *      This product includes software developed for the NetBSD Project by
     49  *      Wasabi Systems, Inc.
     50  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     51  *    or promote products derived from this software without specific prior
     52  *    written permission.
     53  *
     54  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     55  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     56  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     57  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     58  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     59  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     60  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     61  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     62  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     63  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     64  * POSSIBILITY OF SUCH DAMAGE.
     65  */
     66 
     67 /*
     68  * Copyright 2001, 2003 Wasabi Systems, Inc.
     69  * All rights reserved.
     70  *
     71  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
     72  *
     73  * Redistribution and use in source and binary forms, with or without
     74  * modification, are permitted provided that the following conditions
     75  * are met:
     76  * 1. Redistributions of source code must retain the above copyright
     77  *    notice, this list of conditions and the following disclaimer.
     78  * 2. Redistributions in binary form must reproduce the above copyright
     79  *    notice, this list of conditions and the following disclaimer in the
     80  *    documentation and/or other materials provided with the distribution.
     81  * 3. All advertising materials mentioning features or use of this software
     82  *    must display the following acknowledgement:
     83  *	This product includes software developed for the NetBSD Project by
     84  *	Wasabi Systems, Inc.
     85  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     86  *    or promote products derived from this software without specific prior
     87  *    written permission.
     88  *
     89  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     90  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     91  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     92  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     93  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     94  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     95  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     96  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     97  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     98  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     99  * POSSIBILITY OF SUCH DAMAGE.
    100  */
    101 
    102 #include <sys/cdefs.h>
    103 __KERNEL_RCSID(0, "$NetBSD: acpi.c,v 1.272 2018/10/11 22:58:36 jmcneill Exp $");
    104 
    105 #include "pci.h"
    106 #include "opt_acpi.h"
    107 #include "opt_pcifixup.h"
    108 
    109 #include <sys/param.h>
    110 #include <sys/device.h>
    111 #include <sys/kernel.h>
    112 #include <sys/kmem.h>
    113 #include <sys/malloc.h>
    114 #include <sys/module.h>
    115 #include <sys/mutex.h>
    116 #include <sys/sysctl.h>
    117 #include <sys/systm.h>
    118 #include <sys/timetc.h>
    119 
    120 #include <dev/acpi/acpireg.h>
    121 #include <dev/acpi/acpivar.h>
    122 #include <dev/acpi/acpi_mcfg.h>
    123 #include <dev/acpi/acpi_osd.h>
    124 #include <dev/acpi/acpi_pci.h>
    125 #include <dev/acpi/acpi_power.h>
    126 #include <dev/acpi/acpi_timer.h>
    127 #include <dev/acpi/acpi_wakedev.h>
    128 
    129 #include <machine/acpi_machdep.h>
    130 
    131 #include "ioconf.h"
    132 
    133 #define _COMPONENT	ACPI_BUS_COMPONENT
    134 ACPI_MODULE_NAME	("acpi")
    135 
    136 /*
    137  * The acpi_active variable is set when the ACPI subsystem is active.
    138  * Machine-dependent code may wish to skip other steps (such as attaching
    139  * subsystems that ACPI supercedes) when ACPI is active.
    140  */
    141 int		acpi_active = 0;
    142 int		acpi_suspended = 0;
    143 int		acpi_force_load = 0;
    144 int		acpi_verbose_loaded = 0;
    145 
    146 struct acpi_softc	*acpi_softc = NULL;
    147 static uint64_t		 acpi_root_pointer;
    148 extern kmutex_t		 acpi_interrupt_list_mtx;
    149 static ACPI_HANDLE	 acpi_scopes[4];
    150 ACPI_TABLE_HEADER	*madt_header;
    151 
    152 /*
    153  * This structure provides a context for the ACPI
    154  * namespace walk performed in acpi_build_tree().
    155  */
    156 struct acpi_walkcontext {
    157 	struct acpi_softc	*aw_sc;
    158 	struct acpi_devnode	*aw_parent;
    159 };
    160 
    161 /*
    162  * Ignored HIDs.
    163  */
    164 static const char * const acpi_ignored_ids[] = {
    165 #if defined(i386) || defined(x86_64)
    166 	"ACPI0007",	/* ACPI CPUs do not attach to acpi(4) */
    167 	"PNP0000",	/* AT interrupt controller is handled internally */
    168 	"PNP0200",	/* AT DMA controller is handled internally */
    169 	"PNP0A??",	/* PCI Busses are handled internally */
    170 	"PNP0B00",	/* AT RTC is handled internally */
    171 	"PNP0C0F",	/* ACPI PCI link devices are handled internally */
    172 #endif
    173 #if defined(x86_64)
    174 	"PNP0C04",	/* FPU is handled internally */
    175 #endif
    176 	NULL
    177 };
    178 
    179 /*
    180  * Devices that should be attached early.
    181  */
    182 static const char * const acpi_early_ids[] = {
    183 	"PNP0C09",	/* acpiec(4) */
    184 	NULL
    185 };
    186 
    187 static int		acpi_match(device_t, cfdata_t, void *);
    188 static int		acpi_submatch(device_t, cfdata_t, const int *, void *);
    189 static void		acpi_attach(device_t, device_t, void *);
    190 static int		acpi_detach(device_t, int);
    191 static void		acpi_childdet(device_t, device_t);
    192 static bool		acpi_suspend(device_t, const pmf_qual_t *);
    193 static bool		acpi_resume(device_t, const pmf_qual_t *);
    194 
    195 static void		acpi_build_tree(struct acpi_softc *);
    196 static void		acpi_config_tree(struct acpi_softc *);
    197 static ACPI_STATUS	acpi_make_devnode(ACPI_HANDLE, uint32_t,
    198 					  void *, void **);
    199 static ACPI_STATUS	acpi_make_devnode_post(ACPI_HANDLE, uint32_t,
    200 					       void *, void **);
    201 static void		acpi_make_name(struct acpi_devnode *, uint32_t);
    202 
    203 static int		acpi_rescan(device_t, const char *, const int *);
    204 static void		acpi_rescan_early(struct acpi_softc *);
    205 static void		acpi_rescan_nodes(struct acpi_softc *);
    206 static void		acpi_rescan_capabilities(device_t);
    207 static int		acpi_print(void *aux, const char *);
    208 
    209 static void		acpi_notify_handler(ACPI_HANDLE, uint32_t, void *);
    210 
    211 static void		acpi_register_fixed_button(struct acpi_softc *, int);
    212 static void		acpi_deregister_fixed_button(struct acpi_softc *, int);
    213 static uint32_t		acpi_fixed_button_handler(void *);
    214 static void		acpi_fixed_button_pressed(void *);
    215 
    216 static void		acpi_sleep_init(struct acpi_softc *);
    217 
    218 static int		sysctl_hw_acpi_fixedstats(SYSCTLFN_PROTO);
    219 static int		sysctl_hw_acpi_sleepstate(SYSCTLFN_PROTO);
    220 static int		sysctl_hw_acpi_sleepstates(SYSCTLFN_PROTO);
    221 
    222 static bool		  acpi_is_scope(struct acpi_devnode *);
    223 static ACPI_TABLE_HEADER *acpi_map_rsdt(void);
    224 static void		  acpi_unmap_rsdt(ACPI_TABLE_HEADER *);
    225 
    226 void			acpi_print_verbose_stub(struct acpi_softc *);
    227 void			acpi_print_dev_stub(const char *);
    228 
    229 static void		acpi_activate_device(ACPI_HANDLE, ACPI_DEVICE_INFO **);
    230 ACPI_STATUS		acpi_allocate_resources(ACPI_HANDLE);
    231 
    232 void (*acpi_print_verbose)(struct acpi_softc *) = acpi_print_verbose_stub;
    233 void (*acpi_print_dev)(const char *) = acpi_print_dev_stub;
    234 
    235 CFATTACH_DECL2_NEW(acpi, sizeof(struct acpi_softc),
    236     acpi_match, acpi_attach, acpi_detach, NULL, acpi_rescan, acpi_childdet);
    237 
    238 /*
    239  * Probe for ACPI support.
    240  *
    241  * This is called by the machine-dependent ACPI front-end.
    242  * Note: this is not an autoconfiguration interface function.
    243  */
    244 int
    245 acpi_probe(void)
    246 {
    247 	ACPI_TABLE_HEADER *rsdt;
    248 	ACPI_STATUS rv;
    249 	int quirks;
    250 
    251 	if (acpi_softc != NULL)
    252 		panic("%s: already probed", __func__);
    253 
    254 	mutex_init(&acpi_interrupt_list_mtx, MUTEX_DEFAULT, IPL_NONE);
    255 
    256 	/*
    257 	 * Start up ACPICA.
    258 	 */
    259 	AcpiGbl_EnableInterpreterSlack = true;
    260 
    261 	rv = AcpiInitializeSubsystem();
    262 
    263 	if (ACPI_FAILURE(rv)) {
    264 		aprint_error("%s: failed to initialize subsystem\n", __func__);
    265 		return 0;
    266 	}
    267 
    268 	/*
    269 	 * Allocate space for RSDT/XSDT and DSDT,
    270 	 * but allow resizing if more tables exist.
    271 	 */
    272 	rv = AcpiInitializeTables(NULL, 2, true);
    273 
    274 	if (ACPI_FAILURE(rv)) {
    275 		aprint_error("%s: failed to initialize tables\n", __func__);
    276 		goto fail;
    277 	}
    278 
    279 	rv = AcpiLoadTables();
    280 
    281 	if (ACPI_FAILURE(rv)) {
    282 		aprint_error("%s: failed to load tables\n", __func__);
    283 		goto fail;
    284 	}
    285 
    286 	rsdt = acpi_map_rsdt();
    287 
    288 	if (rsdt == NULL) {
    289 		aprint_error("%s: failed to map RSDT\n", __func__);
    290 		goto fail;
    291 	}
    292 
    293 	quirks = acpi_find_quirks();
    294 
    295 	if (acpi_force_load == 0 && (quirks & ACPI_QUIRK_BROKEN) != 0) {
    296 
    297 		aprint_normal("ACPI: BIOS is listed as broken:\n");
    298 		aprint_normal("ACPI: X/RSDT: OemId <%6.6s,%8.8s,%08x>, "
    299 		       "AslId <%4.4s,%08x>\n", rsdt->OemId, rsdt->OemTableId,
    300 		        rsdt->OemRevision, rsdt->AslCompilerId,
    301 		        rsdt->AslCompilerRevision);
    302 		aprint_normal("ACPI: Not used. Set acpi_force_load to use.\n");
    303 
    304 		acpi_unmap_rsdt(rsdt);
    305 		goto fail;
    306 	}
    307 
    308 	if (acpi_force_load == 0 && (quirks & ACPI_QUIRK_OLDBIOS) != 0) {
    309 
    310 		aprint_normal("ACPI: BIOS is too old (%s). "
    311 		    "Set acpi_force_load to use.\n",
    312 		    pmf_get_platform("bios-date"));
    313 
    314 		acpi_unmap_rsdt(rsdt);
    315 		goto fail;
    316 	}
    317 
    318 	acpi_unmap_rsdt(rsdt);
    319 
    320 	rv = AcpiEnableSubsystem(~(ACPI_NO_HARDWARE_INIT|ACPI_NO_ACPI_ENABLE));
    321 
    322 	if (ACPI_FAILURE(rv)) {
    323 		aprint_error("%s: failed to enable subsystem\n", __func__);
    324 		goto fail;
    325 	}
    326 
    327 	return 1;
    328 
    329 fail:
    330 	(void)AcpiTerminate();
    331 
    332 	return 0;
    333 }
    334 
    335 void
    336 acpi_disable(void)
    337 {
    338 
    339 	if (acpi_softc == NULL)
    340 		return;
    341 
    342 	KASSERT(acpi_active != 0);
    343 
    344 	if (AcpiGbl_FADT.SmiCommand != 0)
    345 		AcpiDisable();
    346 }
    347 
    348 int
    349 acpi_check(device_t parent, const char *ifattr)
    350 {
    351 	return (config_search_ia(acpi_submatch, parent, ifattr, NULL) != NULL);
    352 }
    353 
    354 int
    355 acpi_reset(void)
    356 {
    357 	struct acpi_softc *sc = acpi_softc;
    358 	ACPI_GENERIC_ADDRESS *ResetReg;
    359 	ACPI_PCI_ID PciId;
    360 	ACPI_STATUS status;
    361 
    362 	if (sc == NULL)
    363 		return ENXIO;
    364 
    365 	ResetReg = &AcpiGbl_FADT.ResetRegister;
    366 
    367 	/* Check if the reset register is supported */
    368 	if (!(AcpiGbl_FADT.Flags & ACPI_FADT_RESET_REGISTER) ||
    369 	    !ResetReg->Address) {
    370 		return ENOENT;
    371 	}
    372 
    373 	switch (ResetReg->SpaceId) {
    374 	case ACPI_ADR_SPACE_PCI_CONFIG:
    375 		PciId.Segment = PciId.Bus = 0;
    376 		PciId.Device = ACPI_GAS_PCI_DEV(ResetReg->Address);
    377 		PciId.Function = ACPI_GAS_PCI_FUNC(ResetReg->Address);
    378 		status = AcpiOsWritePciConfiguration(&PciId,
    379 		    ACPI_GAS_PCI_REGOFF(ResetReg->Address),
    380 		    AcpiGbl_FADT.ResetValue, ResetReg->BitWidth);
    381 		break;
    382 	case ACPI_ADR_SPACE_SYSTEM_IO:
    383 	case ACPI_ADR_SPACE_SYSTEM_MEMORY:
    384 		status = AcpiReset();
    385 		break;
    386 	default:
    387 		status = AE_TYPE;
    388 		break;
    389 	}
    390 
    391 	return ACPI_FAILURE(status) ? EIO : 0;
    392 }
    393 
    394 /*
    395  * Autoconfiguration.
    396  */
    397 static int
    398 acpi_match(device_t parent, cfdata_t match, void *aux)
    399 {
    400 	/*
    401 	 * XXX: Nada; MD code has called acpi_probe().
    402 	 */
    403 	return 1;
    404 }
    405 
    406 static int
    407 acpi_submatch(device_t parent, cfdata_t cf, const int *locs, void *aux)
    408 {
    409 	struct cfattach *ca;
    410 
    411 	ca = config_cfattach_lookup(cf->cf_name, cf->cf_atname);
    412 
    413 	return (ca == &acpi_ca);
    414 }
    415 
    416 static void
    417 acpi_attach(device_t parent, device_t self, void *aux)
    418 {
    419 	struct acpi_softc *sc = device_private(self);
    420 	struct acpibus_attach_args *aa = aux;
    421 	ACPI_TABLE_HEADER *rsdt;
    422 	ACPI_STATUS rv;
    423 
    424 	aprint_naive("\n");
    425 	aprint_normal(": Intel ACPICA %08x\n", ACPI_CA_VERSION);
    426 
    427 	if (acpi_softc != NULL)
    428 		panic("%s: already attached", __func__);
    429 
    430 	rsdt = acpi_map_rsdt();
    431 
    432 	if (rsdt == NULL)
    433 		aprint_error_dev(self, "X/RSDT: Not found\n");
    434 	else {
    435 		aprint_verbose_dev(self,
    436 		    "X/RSDT: OemId <%6.6s,%8.8s,%08x>, AslId <%4.4s,%08x>\n",
    437 		    rsdt->OemId, rsdt->OemTableId,
    438 		    rsdt->OemRevision,
    439 		    rsdt->AslCompilerId, rsdt->AslCompilerRevision);
    440 	}
    441 
    442 	acpi_unmap_rsdt(rsdt);
    443 
    444 	sc->sc_dev = self;
    445 	sc->sc_root = NULL;
    446 
    447 	sc->sc_sleepstate = ACPI_STATE_S0;
    448 	sc->sc_quirks = acpi_find_quirks();
    449 
    450 	sysmon_power_settype("acpi");
    451 
    452 	sc->sc_iot = aa->aa_iot;
    453 	sc->sc_memt = aa->aa_memt;
    454 	sc->sc_pc = aa->aa_pc;
    455 	sc->sc_pciflags = aa->aa_pciflags;
    456 	sc->sc_ic = aa->aa_ic;
    457 	sc->sc_dmat = aa->aa_dmat;
    458 	sc->sc_dmat64 = aa->aa_dmat64;
    459 
    460 	SIMPLEQ_INIT(&sc->ad_head);
    461 
    462 	acpi_softc = sc;
    463 
    464 	if (pmf_device_register(self, acpi_suspend, acpi_resume) != true)
    465 		aprint_error_dev(self, "couldn't establish power handler\n");
    466 
    467 	/*
    468 	 * Bring ACPICA on-line.
    469 	 */
    470 
    471 	rv = AcpiEnableSubsystem(ACPI_FULL_INITIALIZATION);
    472 
    473 	if (ACPI_FAILURE(rv))
    474 		goto fail;
    475 
    476 	/*
    477 	 * Early initialization of acpiec(4) via ECDT.
    478 	 */
    479 	(void)config_found_ia(self, "acpiecdtbus", aa, NULL);
    480 
    481 	rv = AcpiInitializeObjects(ACPI_FULL_INITIALIZATION);
    482 
    483 	if (ACPI_FAILURE(rv))
    484 		goto fail;
    485 
    486 	/*
    487 	 * Scan the namespace and build our device tree.
    488 	 */
    489 	acpi_build_tree(sc);
    490 
    491 #if NPCI > 0
    492 	/*
    493 	 * Probe MCFG table
    494 	 */
    495 	acpimcfg_probe(sc);
    496 #endif
    497 
    498 	acpi_md_callback(sc);
    499 
    500 	/*
    501 	 * Early initialization of the _PDC control method
    502 	 * that may load additional SSDT tables dynamically.
    503 	 */
    504 	(void)acpi_md_pdc();
    505 
    506 	/*
    507 	 * Install global notify handlers.
    508 	 */
    509 	rv = AcpiInstallNotifyHandler(ACPI_ROOT_OBJECT,
    510 	    ACPI_SYSTEM_NOTIFY, acpi_notify_handler, NULL);
    511 
    512 	if (ACPI_FAILURE(rv))
    513 		goto fail;
    514 
    515 	rv = AcpiInstallNotifyHandler(ACPI_ROOT_OBJECT,
    516 	    ACPI_DEVICE_NOTIFY, acpi_notify_handler, NULL);
    517 
    518 	if (ACPI_FAILURE(rv))
    519 		goto fail;
    520 
    521 	acpi_active = 1;
    522 
    523 	/* Show SCI interrupt. */
    524 	aprint_verbose_dev(self, "SCI interrupting at int %u\n",
    525 	    AcpiGbl_FADT.SciInterrupt);
    526 
    527 	/*
    528 	 * Install fixed-event handlers.
    529 	 */
    530 	acpi_register_fixed_button(sc, ACPI_EVENT_POWER_BUTTON);
    531 	acpi_register_fixed_button(sc, ACPI_EVENT_SLEEP_BUTTON);
    532 
    533 	acpitimer_init(sc);
    534 	acpi_config_tree(sc);
    535 	acpi_sleep_init(sc);
    536 
    537 #ifdef ACPI_DEBUG
    538 	acpi_debug_init();
    539 #endif
    540 
    541 	/*
    542 	 * Print debug information.
    543 	 */
    544 	acpi_print_verbose(sc);
    545 
    546 	return;
    547 
    548 fail:
    549 	aprint_error("%s: failed to initialize ACPI: %s\n",
    550 	    __func__, AcpiFormatException(rv));
    551 }
    552 
    553 /*
    554  * XXX: This is incomplete.
    555  */
    556 static int
    557 acpi_detach(device_t self, int flags)
    558 {
    559 	struct acpi_softc *sc = device_private(self);
    560 	ACPI_STATUS rv;
    561 	int rc;
    562 
    563 	rv = AcpiRemoveNotifyHandler(ACPI_ROOT_OBJECT,
    564 	    ACPI_SYSTEM_NOTIFY, acpi_notify_handler);
    565 
    566 	if (ACPI_FAILURE(rv))
    567 		return EBUSY;
    568 
    569 	rv = AcpiRemoveNotifyHandler(ACPI_ROOT_OBJECT,
    570 	    ACPI_DEVICE_NOTIFY, acpi_notify_handler);
    571 
    572 	if (ACPI_FAILURE(rv))
    573 		return EBUSY;
    574 
    575 	if ((rc = config_detach_children(self, flags)) != 0)
    576 		return rc;
    577 
    578 	if ((rc = acpitimer_detach()) != 0)
    579 		return rc;
    580 
    581 	acpi_deregister_fixed_button(sc, ACPI_EVENT_POWER_BUTTON);
    582 	acpi_deregister_fixed_button(sc, ACPI_EVENT_SLEEP_BUTTON);
    583 
    584 	pmf_device_deregister(self);
    585 
    586 	acpi_softc = NULL;
    587 
    588 	return 0;
    589 }
    590 
    591 static void
    592 acpi_childdet(device_t self, device_t child)
    593 {
    594 	struct acpi_softc *sc = device_private(self);
    595 	struct acpi_devnode *ad;
    596 
    597 	if (sc->sc_apmbus == child)
    598 		sc->sc_apmbus = NULL;
    599 
    600 	if (sc->sc_hpet == child)
    601 		sc->sc_hpet = NULL;
    602 
    603 	if (sc->sc_wdrt == child)
    604 		sc->sc_wdrt = NULL;
    605 
    606 	SIMPLEQ_FOREACH(ad, &sc->ad_head, ad_list) {
    607 
    608 		if (ad->ad_device == child)
    609 			ad->ad_device = NULL;
    610 	}
    611 }
    612 
    613 static bool
    614 acpi_suspend(device_t dv, const pmf_qual_t *qual)
    615 {
    616 
    617 	acpi_suspended = 1;
    618 
    619 	return true;
    620 }
    621 
    622 static bool
    623 acpi_resume(device_t dv, const pmf_qual_t *qual)
    624 {
    625 
    626 	acpi_suspended = 0;
    627 
    628 	return true;
    629 }
    630 
    631 /*
    632  * Namespace scan.
    633  */
    634 static void
    635 acpi_build_tree(struct acpi_softc *sc)
    636 {
    637 	struct acpi_walkcontext awc;
    638 
    639 	/*
    640 	 * Get the root scope handles.
    641 	 */
    642 	KASSERT(__arraycount(acpi_scopes) == 4);
    643 
    644 	(void)AcpiGetHandle(ACPI_ROOT_OBJECT, "\\_PR_", &acpi_scopes[0]);
    645 	(void)AcpiGetHandle(ACPI_ROOT_OBJECT, "\\_SB_", &acpi_scopes[1]);
    646 	(void)AcpiGetHandle(ACPI_ROOT_OBJECT, "\\_SI_", &acpi_scopes[2]);
    647 	(void)AcpiGetHandle(ACPI_ROOT_OBJECT, "\\_TZ_", &acpi_scopes[3]);
    648 
    649 	/*
    650 	 * Make the root node.
    651 	 */
    652 	awc.aw_sc = sc;
    653 	awc.aw_parent = NULL;
    654 
    655 	(void)acpi_make_devnode(ACPI_ROOT_OBJECT, 0, &awc, NULL);
    656 
    657 	KASSERT(sc->sc_root == NULL);
    658 	KASSERT(awc.aw_parent != NULL);
    659 
    660 	sc->sc_root = awc.aw_parent;
    661 
    662 	/*
    663 	 * Build the internal namespace.
    664 	 */
    665 	(void)AcpiWalkNamespace(ACPI_TYPE_ANY, ACPI_ROOT_OBJECT, UINT32_MAX,
    666 	    acpi_make_devnode, acpi_make_devnode_post, &awc, NULL);
    667 
    668 	/*
    669 	 * Scan the internal namespace.
    670 	 */
    671 	(void)acpi_pcidev_scan(sc->sc_root);
    672 }
    673 
    674 static void
    675 acpi_config_tree(struct acpi_softc *sc)
    676 {
    677 
    678 	/*
    679 	 * Configure all everything found "at acpi?".
    680 	 */
    681 	(void)acpi_rescan(sc->sc_dev, NULL, NULL);
    682 
    683 	/*
    684 	 * Update GPE information.
    685 	 *
    686 	 * Note that this must be called after
    687 	 * all GPE handlers have been installed.
    688 	 */
    689 	(void)AcpiUpdateAllGpes();
    690 
    691 	/*
    692 	 * Defer rest of the configuration.
    693 	 */
    694 	(void)config_defer(sc->sc_dev, acpi_rescan_capabilities);
    695 }
    696 
    697 static ACPI_STATUS
    698 acpi_make_devnode(ACPI_HANDLE handle, uint32_t level,
    699     void *context, void **status)
    700 {
    701 	struct acpi_walkcontext *awc = context;
    702 	struct acpi_softc *sc = awc->aw_sc;
    703 	struct acpi_devnode *ad;
    704 	ACPI_DEVICE_INFO *devinfo;
    705 	ACPI_OBJECT_TYPE type;
    706 	ACPI_STATUS rv;
    707 
    708 	rv = AcpiGetObjectInfo(handle, &devinfo);
    709 
    710 	if (ACPI_FAILURE(rv))
    711 		return AE_OK;	/* Do not terminate the walk. */
    712 
    713 	type = devinfo->Type;
    714 
    715 	switch (type) {
    716 
    717 	case ACPI_TYPE_DEVICE:
    718 		acpi_activate_device(handle, &devinfo);
    719 		/* FALLTHROUGH */
    720 
    721 	case ACPI_TYPE_PROCESSOR:
    722 	case ACPI_TYPE_THERMAL:
    723 	case ACPI_TYPE_POWER:
    724 
    725 		ad = kmem_zalloc(sizeof(*ad), KM_SLEEP);
    726 
    727 		ad->ad_device = NULL;
    728 		ad->ad_notify = NULL;
    729 		ad->ad_pciinfo = NULL;
    730 		ad->ad_wakedev = NULL;
    731 
    732 		ad->ad_type = type;
    733 		ad->ad_handle = handle;
    734 		ad->ad_devinfo = devinfo;
    735 
    736 		ad->ad_root = sc->sc_dev;
    737 		ad->ad_parent = awc->aw_parent;
    738 
    739 		acpi_match_node_init(ad);
    740 		acpi_make_name(ad, devinfo->Name);
    741 
    742 		/*
    743 		 * Identify wake GPEs from the _PRW. Note that
    744 		 * AcpiUpdateAllGpes() must be called afterwards.
    745 		 */
    746 		if (ad->ad_devinfo->Type == ACPI_TYPE_DEVICE)
    747 			acpi_wakedev_init(ad);
    748 
    749 		SIMPLEQ_INIT(&ad->ad_child_head);
    750 		SIMPLEQ_INSERT_TAIL(&sc->ad_head, ad, ad_list);
    751 
    752 		if (ad->ad_parent != NULL) {
    753 
    754 			SIMPLEQ_INSERT_TAIL(&ad->ad_parent->ad_child_head,
    755 			    ad, ad_child_list);
    756 		}
    757 
    758 		awc->aw_parent = ad;
    759 	}
    760 
    761 	return AE_OK;
    762 }
    763 
    764 static ACPI_STATUS
    765 acpi_make_devnode_post(ACPI_HANDLE handle, uint32_t level,
    766     void *context, void **status)
    767 {
    768 	struct acpi_walkcontext *awc = context;
    769 
    770 	KASSERT(awc != NULL);
    771 	KASSERT(awc->aw_parent != NULL);
    772 
    773 	if (handle == awc->aw_parent->ad_handle)
    774 		awc->aw_parent = awc->aw_parent->ad_parent;
    775 
    776 	return AE_OK;
    777 }
    778 
    779 static void
    780 acpi_make_name(struct acpi_devnode *ad, uint32_t name)
    781 {
    782 	ACPI_NAME_UNION *anu;
    783 	int clear, i;
    784 
    785 	anu = (ACPI_NAME_UNION *)&name;
    786 	ad->ad_name[4] = '\0';
    787 
    788 	for (i = 3, clear = 0; i >= 0; i--) {
    789 
    790 		if (clear == 0 && anu->Ascii[i] == '_')
    791 			ad->ad_name[i] = '\0';
    792 		else {
    793 			ad->ad_name[i] = anu->Ascii[i];
    794 			clear = 1;
    795 		}
    796 	}
    797 
    798 	if (ad->ad_name[0] == '\0')
    799 		ad->ad_name[0] = '_';
    800 }
    801 
    802 /*
    803  * Device attachment.
    804  */
    805 static int
    806 acpi_rescan(device_t self, const char *ifattr, const int *locators)
    807 {
    808 	struct acpi_softc *sc = device_private(self);
    809 	struct acpi_attach_args aa;
    810 
    811 	/*
    812 	 * Try to attach hpet(4) first via a specific table.
    813 	 */
    814 	aa.aa_memt = sc->sc_memt;
    815 
    816 	if (ifattr_match(ifattr, "acpihpetbus") && sc->sc_hpet == NULL)
    817 		sc->sc_hpet = config_found_ia(sc->sc_dev,
    818 		    "acpihpetbus", &aa, NULL);
    819 
    820 	/*
    821 	 * A two-pass scan for acpinodebus.
    822 	 */
    823 	if (ifattr_match(ifattr, "acpinodebus")) {
    824 		acpi_rescan_early(sc);
    825 		acpi_rescan_nodes(sc);
    826 	}
    827 
    828 	/*
    829 	 * Attach APM emulation and acpiwdrt(4).
    830 	 */
    831 	if (ifattr_match(ifattr, "acpiapmbus") && sc->sc_apmbus == NULL)
    832 		sc->sc_apmbus = config_found_ia(sc->sc_dev,
    833 		    "acpiapmbus", NULL, NULL);
    834 
    835 	if (ifattr_match(ifattr, "acpiwdrtbus") && sc->sc_wdrt == NULL)
    836 		sc->sc_wdrt = config_found_ia(sc->sc_dev,
    837 		    "acpiwdrtbus", NULL, NULL);
    838 
    839 	return 0;
    840 }
    841 
    842 static void
    843 acpi_rescan_early(struct acpi_softc *sc)
    844 {
    845 	struct acpi_attach_args aa;
    846 	struct acpi_devnode *ad;
    847 
    848 	/*
    849 	 * First scan for devices such as acpiec(4) that
    850 	 * should be always attached before anything else.
    851 	 * We want these devices to attach regardless of
    852 	 * the device status and other restrictions.
    853 	 */
    854 	SIMPLEQ_FOREACH(ad, &sc->ad_head, ad_list) {
    855 
    856 		if (ad->ad_device != NULL)
    857 			continue;
    858 
    859 		if (ad->ad_devinfo->Type != ACPI_TYPE_DEVICE)
    860 			continue;
    861 
    862 		if (acpi_match_hid(ad->ad_devinfo, acpi_early_ids) == 0)
    863 			continue;
    864 
    865 		aa.aa_node = ad;
    866 		aa.aa_iot = sc->sc_iot;
    867 		aa.aa_memt = sc->sc_memt;
    868 		aa.aa_pc = sc->sc_pc;
    869 		aa.aa_pciflags = sc->sc_pciflags;
    870 		aa.aa_ic = sc->sc_ic;
    871 		aa.aa_dmat = sc->sc_dmat;
    872 		aa.aa_dmat64 = sc->sc_dmat64;
    873 
    874 		ad->ad_device = config_found_ia(sc->sc_dev,
    875 		    "acpinodebus", &aa, acpi_print);
    876 	}
    877 }
    878 
    879 static void
    880 acpi_rescan_nodes(struct acpi_softc *sc)
    881 {
    882 	const char * const hpet_ids[] = { "PNP0103", NULL };
    883 	struct acpi_attach_args aa;
    884 	struct acpi_devnode *ad;
    885 	ACPI_DEVICE_INFO *di;
    886 
    887 	SIMPLEQ_FOREACH(ad, &sc->ad_head, ad_list) {
    888 
    889 		if (ad->ad_device != NULL)
    890 			continue;
    891 
    892 		/*
    893 		 * There is a bug in ACPICA: it defines the type
    894 		 * of the scopes incorrectly for its own reasons.
    895 		 */
    896 		if (acpi_is_scope(ad) != false)
    897 			continue;
    898 
    899 		di = ad->ad_devinfo;
    900 
    901 		/*
    902 		 * We only attach devices which are present, enabled, and
    903 		 * functioning properly. However, if a device is enabled,
    904 		 * it is decoding resources and we should claim these,
    905 		 * if possible. This requires changes to bus_space(9).
    906 		 */
    907 		if (di->Type == ACPI_TYPE_DEVICE &&
    908 		    !acpi_device_present(ad->ad_handle)) {
    909 			continue;
    910 		}
    911 
    912 		if (di->Type == ACPI_TYPE_POWER)
    913 			continue;
    914 
    915 		if (di->Type == ACPI_TYPE_PROCESSOR)
    916 			continue;
    917 
    918 		if (acpi_match_hid(di, acpi_early_ids) != 0)
    919 			continue;
    920 
    921 		if (acpi_match_hid(di, acpi_ignored_ids) != 0)
    922 			continue;
    923 
    924 		if (acpi_match_hid(di, hpet_ids) != 0 && sc->sc_hpet != NULL)
    925 			continue;
    926 
    927 		aa.aa_node = ad;
    928 		aa.aa_iot = sc->sc_iot;
    929 		aa.aa_memt = sc->sc_memt;
    930 		aa.aa_pc = sc->sc_pc;
    931 		aa.aa_pciflags = sc->sc_pciflags;
    932 		aa.aa_ic = sc->sc_ic;
    933 		aa.aa_dmat = sc->sc_dmat;
    934 		aa.aa_dmat64 = sc->sc_dmat64;
    935 
    936 		ad->ad_device = config_found_ia(sc->sc_dev,
    937 		    "acpinodebus", &aa, acpi_print);
    938 	}
    939 }
    940 
    941 static void
    942 acpi_rescan_capabilities(device_t self)
    943 {
    944 	struct acpi_softc *sc = device_private(self);
    945 	struct acpi_devnode *ad;
    946 	ACPI_HANDLE tmp;
    947 	ACPI_STATUS rv;
    948 
    949 	SIMPLEQ_FOREACH(ad, &sc->ad_head, ad_list) {
    950 
    951 		if (ad->ad_devinfo->Type != ACPI_TYPE_DEVICE)
    952 			continue;
    953 
    954 		/*
    955 		 * Scan power resource capabilities.
    956 		 *
    957 		 * If any power states are supported,
    958 		 * at least _PR0 and _PR3 must be present.
    959 		 */
    960 		rv = AcpiGetHandle(ad->ad_handle, "_PR0", &tmp);
    961 
    962 		if (ACPI_SUCCESS(rv)) {
    963 			ad->ad_flags |= ACPI_DEVICE_POWER;
    964 			acpi_power_add(ad);
    965 		}
    966 
    967 		/*
    968 		 * Scan wake-up capabilities.
    969 		 */
    970 		if (ad->ad_wakedev != NULL) {
    971 			ad->ad_flags |= ACPI_DEVICE_WAKEUP;
    972 			acpi_wakedev_add(ad);
    973 		}
    974 
    975 		/*
    976 		 * Scan docking stations.
    977 		 */
    978 		rv = AcpiGetHandle(ad->ad_handle, "_DCK", &tmp);
    979 
    980 		if (ACPI_SUCCESS(rv))
    981 			ad->ad_flags |= ACPI_DEVICE_DOCK;
    982 
    983 		/*
    984 		 * Scan devices that are ejectable.
    985 		 */
    986 		rv = AcpiGetHandle(ad->ad_handle, "_EJ0", &tmp);
    987 
    988 		if (ACPI_SUCCESS(rv))
    989 			ad->ad_flags |= ACPI_DEVICE_EJECT;
    990 	}
    991 }
    992 
    993 static int
    994 acpi_print(void *aux, const char *pnp)
    995 {
    996 	struct acpi_attach_args *aa = aux;
    997 	struct acpi_devnode *ad;
    998 	const char *hid, *uid;
    999 	ACPI_DEVICE_INFO *di;
   1000 
   1001 	ad = aa->aa_node;
   1002 	di = ad->ad_devinfo;
   1003 
   1004 	hid = di->HardwareId.String;
   1005 	uid = di->UniqueId.String;
   1006 
   1007 	if (pnp != NULL) {
   1008 
   1009 		if (di->Type != ACPI_TYPE_DEVICE) {
   1010 
   1011 			aprint_normal("%s (ACPI Object Type '%s') at %s",
   1012 			    ad->ad_name, AcpiUtGetTypeName(ad->ad_type), pnp);
   1013 
   1014 			return UNCONF;
   1015 		}
   1016 
   1017 		if ((di->Valid & ACPI_VALID_HID) == 0 || hid == NULL)
   1018 			return 0;
   1019 
   1020 		aprint_normal("%s (%s) ", ad->ad_name, hid);
   1021 		acpi_print_dev(hid);
   1022 		aprint_normal("at %s", pnp);
   1023 
   1024 		return UNCONF;
   1025 	}
   1026 
   1027 	aprint_normal(" (%s", ad->ad_name);
   1028 
   1029 	if ((di->Valid & ACPI_VALID_HID) != 0 && hid != NULL) {
   1030 
   1031 		aprint_normal(", %s", hid);
   1032 
   1033 		if ((di->Valid & ACPI_VALID_UID) != 0 && uid != NULL) {
   1034 
   1035 			if (uid[0] == '\0')
   1036 				uid = "<null>";
   1037 
   1038 			aprint_normal("-%s", uid);
   1039 		}
   1040 	}
   1041 
   1042 	aprint_normal(")");
   1043 
   1044 	return UNCONF;
   1045 }
   1046 
   1047 /*
   1048  * Notify.
   1049  */
   1050 static void
   1051 acpi_notify_handler(ACPI_HANDLE handle, uint32_t event, void *aux)
   1052 {
   1053 	struct acpi_softc *sc = acpi_softc;
   1054 	struct acpi_devnode *ad;
   1055 
   1056 	KASSERT(sc != NULL);
   1057 	KASSERT(aux == NULL);
   1058 	KASSERT(acpi_active != 0);
   1059 
   1060 	if (acpi_suspended != 0)
   1061 		return;
   1062 
   1063 	/*
   1064 	 *  System: 0x00 - 0x7F.
   1065 	 *  Device: 0x80 - 0xFF.
   1066 	 */
   1067 	switch (event) {
   1068 
   1069 	case ACPI_NOTIFY_BUS_CHECK:
   1070 	case ACPI_NOTIFY_DEVICE_CHECK:
   1071 	case ACPI_NOTIFY_DEVICE_WAKE:
   1072 	case ACPI_NOTIFY_EJECT_REQUEST:
   1073 	case ACPI_NOTIFY_DEVICE_CHECK_LIGHT:
   1074 	case ACPI_NOTIFY_FREQUENCY_MISMATCH:
   1075 	case ACPI_NOTIFY_BUS_MODE_MISMATCH:
   1076 	case ACPI_NOTIFY_POWER_FAULT:
   1077 	case ACPI_NOTIFY_CAPABILITIES_CHECK:
   1078 	case ACPI_NOTIFY_DEVICE_PLD_CHECK:
   1079 	case ACPI_NOTIFY_RESERVED:
   1080 	case ACPI_NOTIFY_LOCALITY_UPDATE:
   1081 		break;
   1082 	}
   1083 
   1084 	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "notification 0x%02X for "
   1085 		"%s (%p)\n", event, acpi_name(handle), handle));
   1086 
   1087 	/*
   1088 	 * We deliver notifications only to drivers
   1089 	 * that have been successfully attached and
   1090 	 * that have registered a handler with us.
   1091 	 * The opaque pointer is always the device_t.
   1092 	 */
   1093 	SIMPLEQ_FOREACH(ad, &sc->ad_head, ad_list) {
   1094 
   1095 		if (ad->ad_device == NULL)
   1096 			continue;
   1097 
   1098 		if (ad->ad_notify == NULL)
   1099 			continue;
   1100 
   1101 		if (ad->ad_handle != handle)
   1102 			continue;
   1103 
   1104 		(*ad->ad_notify)(ad->ad_handle, event, ad->ad_device);
   1105 
   1106 		return;
   1107 	}
   1108 
   1109 	aprint_debug_dev(sc->sc_dev, "unhandled notify 0x%02X "
   1110 	    "for %s (%p)\n", event, acpi_name(handle), handle);
   1111 }
   1112 
   1113 bool
   1114 acpi_register_notify(struct acpi_devnode *ad, ACPI_NOTIFY_HANDLER notify)
   1115 {
   1116 	struct acpi_softc *sc = acpi_softc;
   1117 
   1118 	KASSERT(sc != NULL);
   1119 	KASSERT(acpi_active != 0);
   1120 
   1121 	if (acpi_suspended != 0)
   1122 		goto fail;
   1123 
   1124 	if (ad == NULL || notify == NULL)
   1125 		goto fail;
   1126 
   1127 	ad->ad_notify = notify;
   1128 
   1129 	return true;
   1130 
   1131 fail:
   1132 	aprint_error_dev(sc->sc_dev, "failed to register notify "
   1133 	    "handler for %s (%p)\n", ad->ad_name, ad->ad_handle);
   1134 
   1135 	return false;
   1136 }
   1137 
   1138 void
   1139 acpi_deregister_notify(struct acpi_devnode *ad)
   1140 {
   1141 
   1142 	ad->ad_notify = NULL;
   1143 }
   1144 
   1145 /*
   1146  * Fixed buttons.
   1147  */
   1148 static void
   1149 acpi_register_fixed_button(struct acpi_softc *sc, int event)
   1150 {
   1151 	struct sysmon_pswitch *smpsw;
   1152 	ACPI_STATUS rv;
   1153 	int type;
   1154 
   1155 	switch (event) {
   1156 
   1157 	case ACPI_EVENT_POWER_BUTTON:
   1158 
   1159 		if ((AcpiGbl_FADT.Flags & ACPI_FADT_POWER_BUTTON) != 0)
   1160 			return;
   1161 
   1162 		type = PSWITCH_TYPE_POWER;
   1163 		smpsw = &sc->sc_smpsw_power;
   1164 		break;
   1165 
   1166 	case ACPI_EVENT_SLEEP_BUTTON:
   1167 
   1168 		if ((AcpiGbl_FADT.Flags & ACPI_FADT_SLEEP_BUTTON) != 0)
   1169 			return;
   1170 
   1171 		type = PSWITCH_TYPE_SLEEP;
   1172 		smpsw = &sc->sc_smpsw_sleep;
   1173 		break;
   1174 
   1175 	default:
   1176 		rv = AE_TYPE;
   1177 		goto fail;
   1178 	}
   1179 
   1180 	smpsw->smpsw_type = type;
   1181 	smpsw->smpsw_name = device_xname(sc->sc_dev);
   1182 
   1183 	if (sysmon_pswitch_register(smpsw) != 0) {
   1184 		rv = AE_ERROR;
   1185 		goto fail;
   1186 	}
   1187 
   1188 	AcpiClearEvent(event);
   1189 
   1190 	rv = AcpiInstallFixedEventHandler(event,
   1191 	    acpi_fixed_button_handler, smpsw);
   1192 
   1193 	if (ACPI_FAILURE(rv)) {
   1194 		sysmon_pswitch_unregister(smpsw);
   1195 		goto fail;
   1196 	}
   1197 
   1198 	aprint_normal_dev(sc->sc_dev, "fixed %s button present\n",
   1199 	    (type != PSWITCH_TYPE_SLEEP) ? "power" : "sleep");
   1200 
   1201 	return;
   1202 
   1203 fail:
   1204 	aprint_error_dev(sc->sc_dev, "failed to register "
   1205 	    "fixed event %d: %s\n", event, AcpiFormatException(rv));
   1206 }
   1207 
   1208 static void
   1209 acpi_deregister_fixed_button(struct acpi_softc *sc, int event)
   1210 {
   1211 	struct sysmon_pswitch *smpsw;
   1212 	ACPI_STATUS rv;
   1213 
   1214 	switch (event) {
   1215 
   1216 	case ACPI_EVENT_POWER_BUTTON:
   1217 		smpsw = &sc->sc_smpsw_power;
   1218 
   1219 		if ((AcpiGbl_FADT.Flags & ACPI_FADT_POWER_BUTTON) != 0) {
   1220 			KASSERT(smpsw->smpsw_type != PSWITCH_TYPE_POWER);
   1221 			return;
   1222 		}
   1223 
   1224 		break;
   1225 
   1226 	case ACPI_EVENT_SLEEP_BUTTON:
   1227 		smpsw = &sc->sc_smpsw_sleep;
   1228 
   1229 		if ((AcpiGbl_FADT.Flags & ACPI_FADT_SLEEP_BUTTON) != 0) {
   1230 			KASSERT(smpsw->smpsw_type != PSWITCH_TYPE_SLEEP);
   1231 			return;
   1232 		}
   1233 
   1234 		break;
   1235 
   1236 	default:
   1237 		rv = AE_TYPE;
   1238 		goto fail;
   1239 	}
   1240 
   1241 	rv = AcpiRemoveFixedEventHandler(event, acpi_fixed_button_handler);
   1242 
   1243 	if (ACPI_SUCCESS(rv)) {
   1244 		sysmon_pswitch_unregister(smpsw);
   1245 		return;
   1246 	}
   1247 
   1248 fail:
   1249 	aprint_error_dev(sc->sc_dev, "failed to deregister "
   1250 	    "fixed event: %s\n", AcpiFormatException(rv));
   1251 }
   1252 
   1253 static uint32_t
   1254 acpi_fixed_button_handler(void *context)
   1255 {
   1256 	static const int handler = OSL_NOTIFY_HANDLER;
   1257 	struct sysmon_pswitch *smpsw = context;
   1258 
   1259 	(void)AcpiOsExecute(handler, acpi_fixed_button_pressed, smpsw);
   1260 
   1261 	return ACPI_INTERRUPT_HANDLED;
   1262 }
   1263 
   1264 static void
   1265 acpi_fixed_button_pressed(void *context)
   1266 {
   1267 	struct sysmon_pswitch *smpsw = context;
   1268 
   1269 	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "%s fixed button pressed\n",
   1270 		(smpsw->smpsw_type != ACPI_EVENT_SLEEP_BUTTON) ?
   1271 		"power" : "sleep"));
   1272 
   1273 	sysmon_pswitch_event(smpsw, PSWITCH_EVENT_PRESSED);
   1274 }
   1275 
   1276 /*
   1277  * Sleep.
   1278  */
   1279 static void
   1280 acpi_sleep_init(struct acpi_softc *sc)
   1281 {
   1282 	uint8_t a, b, i;
   1283 	ACPI_STATUS rv;
   1284 
   1285 	CTASSERT(ACPI_STATE_S0 == 0 && ACPI_STATE_S1 == 1);
   1286 	CTASSERT(ACPI_STATE_S2 == 2 && ACPI_STATE_S3 == 3);
   1287 	CTASSERT(ACPI_STATE_S4 == 4 && ACPI_STATE_S5 == 5);
   1288 
   1289 	/*
   1290 	 * Evaluate supported sleep states.
   1291 	 */
   1292 	for (i = ACPI_STATE_S0; i <= ACPI_STATE_S5; i++) {
   1293 
   1294 		rv = AcpiGetSleepTypeData(i, &a, &b);
   1295 
   1296 		if (ACPI_SUCCESS(rv))
   1297 			sc->sc_sleepstates |= __BIT(i);
   1298 	}
   1299 }
   1300 
   1301 /*
   1302  * Must be called with interrupts enabled.
   1303  */
   1304 void
   1305 acpi_enter_sleep_state(int state)
   1306 {
   1307 	struct acpi_softc *sc = acpi_softc;
   1308 	ACPI_STATUS rv;
   1309 
   1310 	if (acpi_softc == NULL)
   1311 		return;
   1312 
   1313 	if (state == sc->sc_sleepstate)
   1314 		return;
   1315 
   1316 	if (state < ACPI_STATE_S0 || state > ACPI_STATE_S5)
   1317 		return;
   1318 
   1319 	aprint_normal_dev(sc->sc_dev, "entering state S%d\n", state);
   1320 
   1321 	switch (state) {
   1322 
   1323 	case ACPI_STATE_S0:
   1324 		sc->sc_sleepstate = ACPI_STATE_S0;
   1325 		return;
   1326 
   1327 	case ACPI_STATE_S1:
   1328 	case ACPI_STATE_S2:
   1329 	case ACPI_STATE_S3:
   1330 	case ACPI_STATE_S4:
   1331 
   1332 		if ((sc->sc_sleepstates & __BIT(state)) == 0) {
   1333 			aprint_error_dev(sc->sc_dev, "sleep state "
   1334 			    "S%d is not available\n", state);
   1335 			return;
   1336 		}
   1337 
   1338 		/*
   1339 		 * Evaluate the _TTS method. This should be done before
   1340 		 * pmf_system_suspend(9) and the evaluation of _PTS.
   1341 		 * We should also re-evaluate this once we return to
   1342 		 * S0 or if we abort the sleep state transition in the
   1343 		 * middle (see ACPI 3.0, section 7.3.6). In reality,
   1344 		 * however, the _TTS method is seldom seen in the field.
   1345 		 */
   1346 		rv = acpi_eval_set_integer(NULL, "\\_TTS", state);
   1347 
   1348 		if (ACPI_SUCCESS(rv))
   1349 			aprint_debug_dev(sc->sc_dev, "evaluated _TTS\n");
   1350 
   1351 		if (state != ACPI_STATE_S1 &&
   1352 		    pmf_system_suspend(PMF_Q_NONE) != true) {
   1353 			aprint_error_dev(sc->sc_dev, "aborting suspend\n");
   1354 			break;
   1355 		}
   1356 
   1357 		/*
   1358 		 * This will evaluate the  _PTS and _SST methods,
   1359 		 * but unlike the documentation claims, not _GTS,
   1360 		 * which is evaluated in AcpiEnterSleepState().
   1361 		 * This must be called with interrupts enabled.
   1362 		 */
   1363 		rv = AcpiEnterSleepStatePrep(state);
   1364 
   1365 		if (ACPI_FAILURE(rv)) {
   1366 			aprint_error_dev(sc->sc_dev, "failed to prepare "
   1367 			    "S%d: %s\n", state, AcpiFormatException(rv));
   1368 			break;
   1369 		}
   1370 
   1371 		/*
   1372 		 * After the _PTS method has been evaluated, we can
   1373 		 * enable wake and evaluate _PSW (ACPI 4.0, p. 284).
   1374 		 */
   1375 		acpi_wakedev_commit(sc, state);
   1376 
   1377 		sc->sc_sleepstate = state;
   1378 
   1379 		if (state == ACPI_STATE_S1) {
   1380 
   1381 			/*
   1382 			 * Before the transition to S1, CPU caches
   1383 			 * must be flushed (see ACPI 4.0, 7.3.4.2).
   1384 			 *
   1385 			 * Note that interrupts must be off before
   1386 			 * calling AcpiEnterSleepState(). Conversely,
   1387 			 * AcpiLeaveSleepState() should always be
   1388 			 * called with interrupts enabled.
   1389 			 */
   1390 			acpi_md_OsDisableInterrupt();
   1391 
   1392 			ACPI_FLUSH_CPU_CACHE();
   1393 			rv = AcpiEnterSleepState(state);
   1394 
   1395 			if (ACPI_FAILURE(rv))
   1396 				aprint_error_dev(sc->sc_dev, "failed to "
   1397 				    "enter S1: %s\n", AcpiFormatException(rv));
   1398 
   1399 			/*
   1400 			 * Clear fixed events and disable all GPEs before
   1401 			 * interrupts are enabled.
   1402 			 */
   1403 			AcpiClearEvent(ACPI_EVENT_PMTIMER);
   1404 			AcpiClearEvent(ACPI_EVENT_GLOBAL);
   1405 			AcpiClearEvent(ACPI_EVENT_POWER_BUTTON);
   1406 			AcpiClearEvent(ACPI_EVENT_SLEEP_BUTTON);
   1407 			AcpiClearEvent(ACPI_EVENT_RTC);
   1408 			AcpiHwDisableAllGpes();
   1409 
   1410 			acpi_md_OsEnableInterrupt();
   1411 			rv = AcpiLeaveSleepState(state);
   1412 
   1413 		} else {
   1414 
   1415 			(void)acpi_md_sleep(state);
   1416 
   1417 			if (state == ACPI_STATE_S4)
   1418 				AcpiEnable();
   1419 
   1420 			(void)pmf_system_bus_resume(PMF_Q_NONE);
   1421 			(void)AcpiLeaveSleepState(state);
   1422 			(void)AcpiSetFirmwareWakingVector(0, 0);
   1423 			(void)pmf_system_resume(PMF_Q_NONE);
   1424 		}
   1425 
   1426 		/*
   1427 		 * No wake GPEs should be enabled at runtime.
   1428 		 */
   1429 		acpi_wakedev_commit(sc, ACPI_STATE_S0);
   1430 		break;
   1431 
   1432 	case ACPI_STATE_S5:
   1433 
   1434 		(void)acpi_eval_set_integer(NULL, "\\_TTS", ACPI_STATE_S5);
   1435 
   1436 		rv = AcpiEnterSleepStatePrep(ACPI_STATE_S5);
   1437 
   1438 		if (ACPI_FAILURE(rv)) {
   1439 			aprint_error_dev(sc->sc_dev, "failed to prepare "
   1440 			    "S%d: %s\n", state, AcpiFormatException(rv));
   1441 			break;
   1442 		}
   1443 
   1444 		(void)AcpiDisableAllGpes();
   1445 
   1446 		DELAY(1000000);
   1447 
   1448 		sc->sc_sleepstate = state;
   1449 		acpi_md_OsDisableInterrupt();
   1450 
   1451 		(void)AcpiEnterSleepState(ACPI_STATE_S5);
   1452 
   1453 		aprint_error_dev(sc->sc_dev, "WARNING: powerdown failed!\n");
   1454 
   1455 		break;
   1456 	}
   1457 
   1458 	sc->sc_sleepstate = ACPI_STATE_S0;
   1459 
   1460 	(void)acpi_eval_set_integer(NULL, "\\_TTS", ACPI_STATE_S0);
   1461 }
   1462 
   1463 /*
   1464  * Sysctl.
   1465  */
   1466 SYSCTL_SETUP(sysctl_acpi_setup, "sysctl hw.acpi subtree setup")
   1467 {
   1468 	const struct sysctlnode *rnode, *snode;
   1469 	int err;
   1470 
   1471 	err = sysctl_createv(clog, 0, NULL, &rnode,
   1472 	    CTLFLAG_PERMANENT, CTLTYPE_NODE,
   1473 	    "acpi", SYSCTL_DESCR("ACPI subsystem parameters"),
   1474 	    NULL, 0, NULL, 0,
   1475 	    CTL_HW, CTL_CREATE, CTL_EOL);
   1476 
   1477 	if (err != 0)
   1478 		return;
   1479 
   1480 	(void)sysctl_createv(NULL, 0, &rnode, NULL,
   1481 	    CTLFLAG_PERMANENT | CTLFLAG_READONLY, CTLTYPE_QUAD,
   1482 	    "root", SYSCTL_DESCR("ACPI root pointer"),
   1483 	    NULL, 0, &acpi_root_pointer, sizeof(acpi_root_pointer),
   1484 	    CTL_CREATE, CTL_EOL);
   1485 
   1486 	err = sysctl_createv(clog, 0, &rnode, &snode,
   1487 	    CTLFLAG_PERMANENT, CTLTYPE_NODE,
   1488 	    "sleep", SYSCTL_DESCR("ACPI sleep"),
   1489 	    NULL, 0, NULL, 0,
   1490 	    CTL_CREATE, CTL_EOL);
   1491 
   1492 	if (err != 0)
   1493 		return;
   1494 
   1495 	(void)sysctl_createv(NULL, 0, &snode, NULL,
   1496 	    CTLFLAG_PERMANENT | CTLFLAG_READWRITE, CTLTYPE_INT,
   1497 	    "state", SYSCTL_DESCR("System sleep state"),
   1498 	    sysctl_hw_acpi_sleepstate, 0, NULL, 0,
   1499 	    CTL_CREATE, CTL_EOL);
   1500 
   1501 	(void)sysctl_createv(NULL, 0, &snode, NULL,
   1502 	    CTLFLAG_PERMANENT | CTLFLAG_READONLY, CTLTYPE_STRING,
   1503 	    "states", SYSCTL_DESCR("Supported sleep states"),
   1504 	    sysctl_hw_acpi_sleepstates, 0, NULL, 0,
   1505 	    CTL_CREATE, CTL_EOL);
   1506 
   1507 	err = sysctl_createv(clog, 0, &rnode, &rnode,
   1508 	    CTLFLAG_PERMANENT, CTLTYPE_NODE,
   1509 	    "stat", SYSCTL_DESCR("ACPI statistics"),
   1510 	    NULL, 0, NULL, 0,
   1511 	    CTL_CREATE, CTL_EOL);
   1512 
   1513 	if (err != 0)
   1514 		return;
   1515 
   1516 	(void)sysctl_createv(clog, 0, &rnode, NULL,
   1517 	    CTLFLAG_PERMANENT | CTLFLAG_READONLY, CTLTYPE_QUAD,
   1518 	    "gpe", SYSCTL_DESCR("Number of dispatched GPEs"),
   1519 	    NULL, 0, &AcpiGpeCount, sizeof(AcpiGpeCount),
   1520 	    CTL_CREATE, CTL_EOL);
   1521 
   1522 	(void)sysctl_createv(clog, 0, &rnode, NULL,
   1523 	    CTLFLAG_PERMANENT | CTLFLAG_READONLY, CTLTYPE_QUAD,
   1524 	    "sci", SYSCTL_DESCR("Number of SCI interrupts"),
   1525 	    NULL, 0, &AcpiSciCount, sizeof(AcpiSciCount),
   1526 	    CTL_CREATE, CTL_EOL);
   1527 
   1528 	(void)sysctl_createv(clog, 0, &rnode, NULL,
   1529 	    CTLFLAG_PERMANENT | CTLFLAG_READONLY, CTLTYPE_QUAD,
   1530 	    "fixed", SYSCTL_DESCR("Number of fixed events"),
   1531 	    sysctl_hw_acpi_fixedstats, 0, NULL, 0,
   1532 	    CTL_CREATE, CTL_EOL);
   1533 
   1534 	(void)sysctl_createv(clog, 0, &rnode, NULL,
   1535 	    CTLFLAG_PERMANENT | CTLFLAG_READONLY, CTLTYPE_QUAD,
   1536 	    "method", SYSCTL_DESCR("Number of methods executed"),
   1537 	    NULL, 0, &AcpiMethodCount, sizeof(AcpiMethodCount),
   1538 	    CTL_CREATE, CTL_EOL);
   1539 
   1540 	CTASSERT(sizeof(AcpiGpeCount) == sizeof(uint64_t));
   1541 	CTASSERT(sizeof(AcpiSciCount) == sizeof(uint64_t));
   1542 }
   1543 
   1544 static int
   1545 sysctl_hw_acpi_fixedstats(SYSCTLFN_ARGS)
   1546 {
   1547 	struct sysctlnode node;
   1548 	uint64_t t;
   1549 	int err, i;
   1550 
   1551 	for (i = t = 0; i < __arraycount(AcpiFixedEventCount); i++)
   1552 		t += AcpiFixedEventCount[i];
   1553 
   1554 	node = *rnode;
   1555 	node.sysctl_data = &t;
   1556 
   1557 	err = sysctl_lookup(SYSCTLFN_CALL(&node));
   1558 
   1559 	if (err || newp == NULL)
   1560 		return err;
   1561 
   1562 	return 0;
   1563 }
   1564 
   1565 static int
   1566 sysctl_hw_acpi_sleepstate(SYSCTLFN_ARGS)
   1567 {
   1568 	struct acpi_softc *sc = acpi_softc;
   1569 	struct sysctlnode node;
   1570 	int err, t;
   1571 
   1572 	if (acpi_softc == NULL)
   1573 		return ENOSYS;
   1574 
   1575 	node = *rnode;
   1576 	t = sc->sc_sleepstate;
   1577 	node.sysctl_data = &t;
   1578 
   1579 	err = sysctl_lookup(SYSCTLFN_CALL(&node));
   1580 
   1581 	if (err || newp == NULL)
   1582 		return err;
   1583 
   1584 	if (t < ACPI_STATE_S0 || t > ACPI_STATE_S5)
   1585 		return EINVAL;
   1586 
   1587 	acpi_enter_sleep_state(t);
   1588 
   1589 	return 0;
   1590 }
   1591 
   1592 static int
   1593 sysctl_hw_acpi_sleepstates(SYSCTLFN_ARGS)
   1594 {
   1595 	struct acpi_softc *sc = acpi_softc;
   1596 	struct sysctlnode node;
   1597 	char t[3 * 6 + 1];
   1598 	int err;
   1599 
   1600 	if (acpi_softc == NULL)
   1601 		return ENOSYS;
   1602 
   1603 	(void)memset(t, '\0', sizeof(t));
   1604 
   1605 	(void)snprintf(t, sizeof(t), "%s%s%s%s%s%s",
   1606 	    ((sc->sc_sleepstates & __BIT(0)) != 0) ? "S0 " : "",
   1607 	    ((sc->sc_sleepstates & __BIT(1)) != 0) ? "S1 " : "",
   1608 	    ((sc->sc_sleepstates & __BIT(2)) != 0) ? "S2 " : "",
   1609 	    ((sc->sc_sleepstates & __BIT(3)) != 0) ? "S3 " : "",
   1610 	    ((sc->sc_sleepstates & __BIT(4)) != 0) ? "S4 " : "",
   1611 	    ((sc->sc_sleepstates & __BIT(5)) != 0) ? "S5 " : "");
   1612 
   1613 	node = *rnode;
   1614 	node.sysctl_data = &t;
   1615 
   1616 	err = sysctl_lookup(SYSCTLFN_CALL(&node));
   1617 
   1618 	if (err || newp == NULL)
   1619 		return err;
   1620 
   1621 	return 0;
   1622 }
   1623 
   1624 /*
   1625  * Tables.
   1626  */
   1627 ACPI_PHYSICAL_ADDRESS
   1628 acpi_OsGetRootPointer(void)
   1629 {
   1630 	ACPI_PHYSICAL_ADDRESS PhysicalAddress;
   1631 
   1632 	/*
   1633 	 * We let MD code handle this since there are multiple ways to do it:
   1634 	 *
   1635 	 *	IA-32: Use AcpiFindRootPointer() to locate the RSDP.
   1636 	 *
   1637 	 *	IA-64: Use the EFI.
   1638 	 */
   1639 	PhysicalAddress = acpi_md_OsGetRootPointer();
   1640 
   1641 	if (acpi_root_pointer == 0)
   1642 		acpi_root_pointer = PhysicalAddress;
   1643 
   1644 	return PhysicalAddress;
   1645 }
   1646 
   1647 static ACPI_TABLE_HEADER *
   1648 acpi_map_rsdt(void)
   1649 {
   1650 	ACPI_PHYSICAL_ADDRESS paddr;
   1651 	ACPI_TABLE_RSDP *rsdp;
   1652 
   1653 	paddr = AcpiOsGetRootPointer();
   1654 
   1655 	if (paddr == 0)
   1656 		return NULL;
   1657 
   1658 	rsdp = AcpiOsMapMemory(paddr, sizeof(ACPI_TABLE_RSDP));
   1659 
   1660 	if (rsdp == NULL)
   1661 		return NULL;
   1662 
   1663 	if (rsdp->Revision > 1 && rsdp->XsdtPhysicalAddress)
   1664 		paddr = rsdp->XsdtPhysicalAddress;
   1665 	else
   1666 		paddr = rsdp->RsdtPhysicalAddress;
   1667 
   1668 	AcpiOsUnmapMemory(rsdp, sizeof(ACPI_TABLE_RSDP));
   1669 
   1670 	return AcpiOsMapMemory(paddr, sizeof(ACPI_TABLE_HEADER));
   1671 }
   1672 
   1673 /*
   1674  * XXX: Refactor to be a generic function that unmaps tables.
   1675  */
   1676 static void
   1677 acpi_unmap_rsdt(ACPI_TABLE_HEADER *rsdt)
   1678 {
   1679 
   1680 	if (rsdt == NULL)
   1681 		return;
   1682 
   1683 	AcpiOsUnmapMemory(rsdt, sizeof(ACPI_TABLE_HEADER));
   1684 }
   1685 
   1686 /*
   1687  * XXX: Refactor to be a generic function that maps tables.
   1688  */
   1689 ACPI_STATUS
   1690 acpi_madt_map(void)
   1691 {
   1692 	ACPI_STATUS  rv;
   1693 
   1694 	if (madt_header != NULL)
   1695 		return AE_ALREADY_EXISTS;
   1696 
   1697 	rv = AcpiGetTable(ACPI_SIG_MADT, 1, &madt_header);
   1698 
   1699 	if (ACPI_FAILURE(rv))
   1700 		return rv;
   1701 
   1702 	return AE_OK;
   1703 }
   1704 
   1705 void
   1706 acpi_madt_unmap(void)
   1707 {
   1708 	madt_header = NULL;
   1709 }
   1710 
   1711 /*
   1712  * XXX: Refactor to be a generic function that walks tables.
   1713  */
   1714 void
   1715 acpi_madt_walk(ACPI_STATUS (*func)(ACPI_SUBTABLE_HEADER *, void *), void *aux)
   1716 {
   1717 	ACPI_SUBTABLE_HEADER *hdrp;
   1718 	char *madtend, *where;
   1719 
   1720 	madtend = (char *)madt_header + madt_header->Length;
   1721 	where = (char *)madt_header + sizeof (ACPI_TABLE_MADT);
   1722 
   1723 	while (where < madtend) {
   1724 
   1725 		hdrp = (ACPI_SUBTABLE_HEADER *)where;
   1726 
   1727 		if (ACPI_FAILURE(func(hdrp, aux)))
   1728 			break;
   1729 
   1730 		where += hdrp->Length;
   1731 	}
   1732 }
   1733 
   1734 /*
   1735  * Miscellaneous.
   1736  */
   1737 static bool
   1738 acpi_is_scope(struct acpi_devnode *ad)
   1739 {
   1740 	int i;
   1741 
   1742 	/*
   1743 	 * Return true if the node is a root scope.
   1744 	 */
   1745 	if (ad->ad_parent == NULL)
   1746 		return false;
   1747 
   1748 	if (ad->ad_parent->ad_handle != ACPI_ROOT_OBJECT)
   1749 		return false;
   1750 
   1751 	for (i = 0; i < __arraycount(acpi_scopes); i++) {
   1752 
   1753 		if (acpi_scopes[i] == NULL)
   1754 			continue;
   1755 
   1756 		if (ad->ad_handle == acpi_scopes[i])
   1757 			return true;
   1758 	}
   1759 
   1760 	return false;
   1761 }
   1762 
   1763 bool
   1764 acpi_device_present(ACPI_HANDLE handle)
   1765 {
   1766 	ACPI_STATUS rv;
   1767 	ACPI_INTEGER sta;
   1768 
   1769 	rv = acpi_eval_integer(handle, "_STA", &sta);
   1770 
   1771 	if (ACPI_FAILURE(rv)) {
   1772 		/* No _STA method -> must be there */
   1773 		return rv == AE_NOT_FOUND;
   1774 	}
   1775 
   1776 	return (sta & ACPI_STA_OK) == ACPI_STA_OK;
   1777 }
   1778 
   1779 /*
   1780  * ACPIVERBOSE.
   1781  */
   1782 void
   1783 acpi_load_verbose(void)
   1784 {
   1785 
   1786 	if (acpi_verbose_loaded == 0)
   1787 		module_autoload("acpiverbose", MODULE_CLASS_MISC);
   1788 }
   1789 
   1790 void
   1791 acpi_print_verbose_stub(struct acpi_softc *sc)
   1792 {
   1793 
   1794 	acpi_load_verbose();
   1795 
   1796 	if (acpi_verbose_loaded != 0)
   1797 		acpi_print_verbose(sc);
   1798 }
   1799 
   1800 void
   1801 acpi_print_dev_stub(const char *pnpstr)
   1802 {
   1803 
   1804 	acpi_load_verbose();
   1805 
   1806 	if (acpi_verbose_loaded != 0)
   1807 		acpi_print_dev(pnpstr);
   1808 }
   1809 
   1810 MALLOC_DECLARE(M_ACPI); /* XXX: ACPI_ACTIVATE_DEV should use kmem(9). */
   1811 
   1812 /*
   1813  * ACPI_ACTIVATE_DEV.
   1814  */
   1815 static void
   1816 acpi_activate_device(ACPI_HANDLE handle, ACPI_DEVICE_INFO **di)
   1817 {
   1818 
   1819 #ifndef ACPI_ACTIVATE_DEV
   1820 	return;
   1821 }
   1822 #else
   1823 	static const int valid = ACPI_VALID_HID;
   1824 	ACPI_DEVICE_INFO *newdi;
   1825 	ACPI_STATUS rv;
   1826 
   1827 
   1828 	/*
   1829 	 * If the device is valid and present,
   1830 	 * but not enabled, try to activate it.
   1831 	 */
   1832 	if (((*di)->Valid & valid) != valid)
   1833 		return;
   1834 
   1835 	if (!acpi_device_present(handle))
   1836 		return;
   1837 
   1838 	rv = acpi_allocate_resources(handle);
   1839 
   1840 	if (ACPI_FAILURE(rv))
   1841 		goto fail;
   1842 
   1843 	rv = AcpiGetObjectInfo(handle, &newdi);
   1844 
   1845 	if (ACPI_FAILURE(rv))
   1846 		goto fail;
   1847 
   1848 	ACPI_FREE(*di);
   1849 	*di = newdi;
   1850 
   1851 	aprint_verbose_dev(acpi_softc->sc_dev,
   1852 	    "%s activated\n", (*di)->HardwareId.String);
   1853 
   1854 	return;
   1855 
   1856 fail:
   1857 	aprint_error_dev(acpi_softc->sc_dev, "failed to "
   1858 	    "activate %s\n", (*di)->HardwareId.String);
   1859 }
   1860 
   1861 /*
   1862  * XXX: This very incomplete.
   1863  */
   1864 ACPI_STATUS
   1865 acpi_allocate_resources(ACPI_HANDLE handle)
   1866 {
   1867 	ACPI_BUFFER bufp, bufc, bufn;
   1868 	ACPI_RESOURCE *resp, *resc, *resn;
   1869 	ACPI_RESOURCE_IRQ *irq;
   1870 #if 0
   1871 	ACPI_RESOURCE_EXTENDED_IRQ *xirq;
   1872 #endif
   1873 	ACPI_STATUS rv;
   1874 	uint delta;
   1875 
   1876 	rv = acpi_get(handle, &bufp, AcpiGetPossibleResources);
   1877 	if (ACPI_FAILURE(rv))
   1878 		goto out;
   1879 	rv = acpi_get(handle, &bufc, AcpiGetCurrentResources);
   1880 	if (ACPI_FAILURE(rv)) {
   1881 		goto out1;
   1882 	}
   1883 
   1884 	bufn.Length = 1000;
   1885 	bufn.Pointer = resn = malloc(bufn.Length, M_ACPI, M_WAITOK);
   1886 	resp = bufp.Pointer;
   1887 	resc = bufc.Pointer;
   1888 	while (resc->Type != ACPI_RESOURCE_TYPE_END_TAG &&
   1889 	       resp->Type != ACPI_RESOURCE_TYPE_END_TAG) {
   1890 		while (resc->Type != resp->Type && resp->Type != ACPI_RESOURCE_TYPE_END_TAG)
   1891 			resp = ACPI_NEXT_RESOURCE(resp);
   1892 		if (resp->Type == ACPI_RESOURCE_TYPE_END_TAG)
   1893 			break;
   1894 		/* Found identical Id */
   1895 		resn->Type = resc->Type;
   1896 		switch (resc->Type) {
   1897 		case ACPI_RESOURCE_TYPE_IRQ:
   1898 			memcpy(&resn->Data, &resp->Data,
   1899 			       sizeof(ACPI_RESOURCE_IRQ));
   1900 			irq = (ACPI_RESOURCE_IRQ *)&resn->Data;
   1901 			irq->Interrupts[0] =
   1902 			    ((ACPI_RESOURCE_IRQ *)&resp->Data)->
   1903 			        Interrupts[irq->InterruptCount-1];
   1904 			irq->InterruptCount = 1;
   1905 			resn->Length = ACPI_RS_SIZE(ACPI_RESOURCE_IRQ);
   1906 			break;
   1907 		case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
   1908 			memcpy(&resn->Data, &resp->Data,
   1909 			       sizeof(ACPI_RESOURCE_EXTENDED_IRQ));
   1910 #if 0
   1911 			xirq = (ACPI_RESOURCE_EXTENDED_IRQ *)&resn->Data;
   1912 			/*
   1913 			 * XXX:	Not duplicating the interrupt logic above
   1914 			 *	because its not clear what it accomplishes.
   1915 			 */
   1916 			xirq->Interrupts[0] =
   1917 			    ((ACPI_RESOURCE_EXT_IRQ *)&resp->Data)->
   1918 			    Interrupts[irq->NumberOfInterrupts-1];
   1919 			xirq->NumberOfInterrupts = 1;
   1920 #endif
   1921 			resn->Length = ACPI_RS_SIZE(ACPI_RESOURCE_EXTENDED_IRQ);
   1922 			break;
   1923 		case ACPI_RESOURCE_TYPE_IO:
   1924 			memcpy(&resn->Data, &resp->Data,
   1925 			       sizeof(ACPI_RESOURCE_IO));
   1926 			resn->Length = resp->Length;
   1927 			break;
   1928 		default:
   1929 			aprint_error_dev(acpi_softc->sc_dev,
   1930 			    "%s: invalid type %u\n", __func__, resc->Type);
   1931 			rv = AE_BAD_DATA;
   1932 			goto out2;
   1933 		}
   1934 		resc = ACPI_NEXT_RESOURCE(resc);
   1935 		resn = ACPI_NEXT_RESOURCE(resn);
   1936 		resp = ACPI_NEXT_RESOURCE(resp);
   1937 		delta = (uint8_t *)resn - (uint8_t *)bufn.Pointer;
   1938 		if (delta >=
   1939 		    bufn.Length-ACPI_RS_SIZE(ACPI_RESOURCE_DATA)) {
   1940 			bufn.Length *= 2;
   1941 			bufn.Pointer = realloc(bufn.Pointer, bufn.Length,
   1942 					       M_ACPI, M_WAITOK);
   1943 			resn = (ACPI_RESOURCE *)((uint8_t *)bufn.Pointer +
   1944 			    delta);
   1945 		}
   1946 	}
   1947 
   1948 	if (resc->Type != ACPI_RESOURCE_TYPE_END_TAG) {
   1949 		aprint_error_dev(acpi_softc->sc_dev,
   1950 		    "%s: resc not exhausted\n", __func__);
   1951 		rv = AE_BAD_DATA;
   1952 		goto out3;
   1953 	}
   1954 
   1955 	resn->Type = ACPI_RESOURCE_TYPE_END_TAG;
   1956 	rv = AcpiSetCurrentResources(handle, &bufn);
   1957 
   1958 	if (ACPI_FAILURE(rv))
   1959 		aprint_error_dev(acpi_softc->sc_dev, "%s: failed to set "
   1960 		    "resources: %s\n", __func__, AcpiFormatException(rv));
   1961 
   1962 out3:
   1963 	free(bufn.Pointer, M_ACPI);
   1964 out2:
   1965 	ACPI_FREE(bufc.Pointer);
   1966 out1:
   1967 	ACPI_FREE(bufp.Pointer);
   1968 out:
   1969 	return rv;
   1970 }
   1971 
   1972 #endif	/* ACPI_ACTIVATE_DEV */
   1973