Home | History | Annotate | Line # | Download | only in acpi
acpi.c revision 1.208
      1 /*	$NetBSD: acpi.c,v 1.208 2010/07/25 12:54:46 pgoyette 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 2001, 2003 Wasabi Systems, Inc.
     34  * All rights reserved.
     35  *
     36  * Written by Jason R. Thorpe 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 #include <sys/cdefs.h>
     68 __KERNEL_RCSID(0, "$NetBSD: acpi.c,v 1.208 2010/07/25 12:54:46 pgoyette Exp $");
     69 
     70 #include "opt_acpi.h"
     71 #include "opt_pcifixup.h"
     72 
     73 #include <sys/param.h>
     74 #include <sys/device.h>
     75 #include <sys/kernel.h>
     76 #include <sys/malloc.h>
     77 #include <sys/module.h>
     78 #include <sys/mutex.h>
     79 #include <sys/sysctl.h>
     80 #include <sys/systm.h>
     81 #include <sys/timetc.h>
     82 
     83 #include <dev/acpi/acpireg.h>
     84 #include <dev/acpi/acpivar.h>
     85 #include <dev/acpi/acpi_osd.h>
     86 #include <dev/acpi/acpi_pci.h>
     87 #include <dev/acpi/acpi_power.h>
     88 #include <dev/acpi/acpi_timer.h>
     89 #include <dev/acpi/acpi_wakedev.h>
     90 
     91 #define _COMPONENT	ACPI_BUS_COMPONENT
     92 ACPI_MODULE_NAME	("acpi")
     93 
     94 #if defined(ACPI_PCI_FIXUP)
     95 #error The option ACPI_PCI_FIXUP has been obsoleted by PCI_INTR_FIXUP_DISABLED.  Please adjust your kernel configuration file.
     96 #endif
     97 
     98 #ifdef PCI_INTR_FIXUP_DISABLED
     99 #include <dev/pci/pcidevs.h>
    100 #endif
    101 
    102 MALLOC_DECLARE(M_ACPI);
    103 
    104 #include <machine/acpi_machdep.h>
    105 
    106 #ifdef ACPI_DEBUGGER
    107 #define	ACPI_DBGR_INIT		0x01
    108 #define	ACPI_DBGR_TABLES	0x02
    109 #define	ACPI_DBGR_ENABLE	0x04
    110 #define	ACPI_DBGR_PROBE		0x08
    111 #define	ACPI_DBGR_RUNNING	0x10
    112 
    113 static int acpi_dbgr = 0x00;
    114 #endif
    115 
    116 /*
    117  * This is a flag we set when the ACPI subsystem is active.  Machine
    118  * dependent code may wish to skip other steps (such as attaching
    119  * subsystems that ACPI supercedes) when ACPI is active.
    120  */
    121 int	acpi_active;
    122 int	acpi_force_load;
    123 int	acpi_suspended = 0;
    124 
    125 struct acpi_softc *acpi_softc;
    126 static uint64_t acpi_root_pointer;
    127 extern kmutex_t acpi_interrupt_list_mtx;
    128 static ACPI_HANDLE acpi_scopes[4];
    129 
    130 /*
    131  * This structure provides a context for the ACPI
    132  * namespace walk performed in acpi_build_tree().
    133  */
    134 struct acpi_walkcontext {
    135 	struct acpi_softc	*aw_sc;
    136 	struct acpi_devnode	*aw_parent;
    137 };
    138 
    139 /*
    140  * Ignored HIDs.
    141  */
    142 static const char * const acpi_ignored_ids[] = {
    143 #if defined(i386) || defined(x86_64)
    144 	"PNP0000",	/* AT interrupt controller is handled internally */
    145 	"PNP0200",	/* AT DMA controller is handled internally */
    146 	"PNP0A??",	/* PCI Busses are handled internally */
    147 	"PNP0B00",	/* AT RTC is handled internally */
    148 	"PNP0C0B",	/* No need for "ACPI fan" driver */
    149 	"PNP0C0F",	/* ACPI PCI link devices are handled internally */
    150 	"IFX0102",	/* No driver for Infineon TPM */
    151 	"INT0800",	/* No driver for Intel Firmware Hub device */
    152 #endif
    153 #if defined(x86_64)
    154 	"PNP0C04",	/* FPU is handled internally */
    155 #endif
    156 	NULL
    157 };
    158 
    159 static int		acpi_match(device_t, cfdata_t, void *);
    160 static int		acpi_submatch(device_t, cfdata_t, const int *, void *);
    161 static void		acpi_attach(device_t, device_t, void *);
    162 static int		acpi_detach(device_t, int);
    163 static void		acpi_childdet(device_t, device_t);
    164 static bool		acpi_suspend(device_t, const pmf_qual_t *);
    165 static bool		acpi_resume(device_t, const pmf_qual_t *);
    166 
    167 static void		acpi_build_tree(struct acpi_softc *);
    168 static ACPI_STATUS	acpi_make_devnode(ACPI_HANDLE, uint32_t,
    169 					  void *, void **);
    170 static ACPI_STATUS	acpi_make_devnode_post(ACPI_HANDLE, uint32_t,
    171 					       void *, void **);
    172 
    173 #ifdef ACPI_ACTIVATE_DEV
    174 static void		acpi_activate_device(ACPI_HANDLE, ACPI_DEVICE_INFO **);
    175 static ACPI_STATUS	acpi_allocate_resources(ACPI_HANDLE);
    176 #endif
    177 
    178 static int		acpi_rescan(device_t, const char *, const int *);
    179 static void		acpi_rescan_nodes(struct acpi_softc *);
    180 static void		acpi_rescan_capabilities(struct acpi_softc *);
    181 static int		acpi_print(void *aux, const char *);
    182 
    183 static void		acpi_notify_handler(ACPI_HANDLE, uint32_t, void *);
    184 
    185 static void		acpi_register_fixed_button(struct acpi_softc *, int);
    186 static void		acpi_deregister_fixed_button(struct acpi_softc *, int);
    187 static uint32_t		acpi_fixed_button_handler(void *);
    188 static void		acpi_fixed_button_pressed(void *);
    189 
    190 static void		acpi_sleep_init(struct acpi_softc *);
    191 
    192 static int		sysctl_hw_acpi_fixedstats(SYSCTLFN_ARGS);
    193 static int		sysctl_hw_acpi_sleepstate(SYSCTLFN_ARGS);
    194 static int		sysctl_hw_acpi_sleepstates(SYSCTLFN_ARGS);
    195 
    196 static bool		  acpi_is_scope(struct acpi_devnode *);
    197 static ACPI_TABLE_HEADER *acpi_map_rsdt(void);
    198 static void		  acpi_unmap_rsdt(ACPI_TABLE_HEADER *);
    199 
    200 extern struct cfdriver acpi_cd;
    201 
    202 /* Handle routine vectors and loading for acpiverbose module */
    203 void acpi_null(void);
    204 
    205 void acpi_print_devnodes_stub(struct acpi_softc *);
    206 void acpi_print_tree_stub(struct acpi_devnode *, uint32_t);
    207 void acpi_print_dev_stub(const char *);
    208 void acpi_wmidump_stub(void *);
    209 
    210 void (*acpi_print_devnodes)(struct acpi_softc *) = acpi_print_devnodes_stub;
    211 void (*acpi_print_tree)(struct acpi_devnode *, uint32_t) = acpi_print_tree_stub;
    212 void (*acpi_print_dev)(const char *) = acpi_print_dev_stub;
    213 void (*acpi_wmidump)(void *) = acpi_wmidump_stub;
    214 
    215 int acpi_verbose_loaded = 0;
    216 
    217 /*
    218  * Support for ACPIVERBOSE.
    219  */
    220 void
    221 acpi_null(void)
    222 {
    223 	/* Nothing to do. */
    224 }
    225 
    226 void
    227 acpi_load_verbose(void)
    228 {
    229 	if (acpi_verbose_loaded == 0) {
    230 		mutex_enter(&module_lock);
    231 		module_autoload("acpiverbose", MODULE_CLASS_MISC);
    232 		mutex_exit(&module_lock);
    233 	}
    234 }
    235 
    236 void
    237 acpi_print_devnodes_stub(struct acpi_softc *sc)
    238 {
    239 	acpi_load_verbose();
    240 	if (acpi_verbose_loaded)
    241 		acpi_print_devnodes(sc);
    242 }
    243 
    244 void
    245 acpi_print_tree_stub(struct acpi_devnode *ad, uint32_t level)
    246 {
    247 	acpi_load_verbose();
    248 	if (acpi_verbose_loaded)
    249 		acpi_print_tree(ad, level);
    250 }
    251 
    252 void
    253 acpi_print_dev_stub(const char *pnpstr)
    254 {
    255 	acpi_load_verbose();
    256 	if (acpi_verbose_loaded)
    257 		acpi_print_dev(pnpstr);
    258 }
    259 
    260 void
    261 acpi_wmidump_stub(void *arg)
    262 {
    263 	acpi_load_verbose();
    264 	if (acpi_verbose_loaded)
    265 		acpi_wmidump(arg);
    266 }
    267 
    268 CFATTACH_DECL2_NEW(acpi, sizeof(struct acpi_softc),
    269     acpi_match, acpi_attach, acpi_detach, NULL, acpi_rescan, acpi_childdet);
    270 
    271 /*
    272  * Probe for ACPI support.
    273  *
    274  * This is called by the machine-dependent ACPI front-end.
    275  * Note: this is not an autoconfiguration interface function.
    276  */
    277 int
    278 acpi_probe(void)
    279 {
    280 	ACPI_TABLE_HEADER *rsdt;
    281 	const char *func;
    282 	static int once;
    283 	bool initialized;
    284 	ACPI_STATUS rv;
    285 
    286 	if (once != 0)
    287 		panic("%s: already probed", __func__);
    288 
    289 	once = 1;
    290 	func = NULL;
    291 	initialized = false;
    292 
    293 	mutex_init(&acpi_interrupt_list_mtx, MUTEX_DEFAULT, IPL_NONE);
    294 
    295 	/*
    296 	 * Start up ACPICA.
    297 	 */
    298 #ifdef ACPI_DEBUGGER
    299 	if (acpi_dbgr & ACPI_DBGR_INIT)
    300 		acpi_osd_debugger();
    301 #endif
    302 
    303 	CTASSERT(TRUE == true);
    304 	CTASSERT(FALSE == false);
    305 
    306 	AcpiGbl_AllMethodsSerialized = false;
    307 	AcpiGbl_EnableInterpreterSlack = true;
    308 
    309 	rv = AcpiInitializeSubsystem();
    310 
    311 	if (ACPI_SUCCESS(rv))
    312 		initialized = true;
    313 	else {
    314 		func = "AcpiInitializeSubsystem()";
    315 		goto fail;
    316 	}
    317 
    318 	/*
    319 	 * Allocate space for RSDT/XSDT and DSDT,
    320 	 * but allow resizing if more tables exist.
    321 	 */
    322 	rv = AcpiInitializeTables(NULL, 2, true);
    323 
    324 	if (ACPI_FAILURE(rv)) {
    325 		func = "AcpiInitializeTables()";
    326 		goto fail;
    327 	}
    328 
    329 #ifdef ACPI_DEBUGGER
    330 	if (acpi_dbgr & ACPI_DBGR_TABLES)
    331 		acpi_osd_debugger();
    332 #endif
    333 
    334 	rv = AcpiLoadTables();
    335 
    336 	if (ACPI_FAILURE(rv)) {
    337 		func = "AcpiLoadTables()";
    338 		goto fail;
    339 	}
    340 
    341 	rsdt = acpi_map_rsdt();
    342 
    343 	if (rsdt == NULL) {
    344 		func = "acpi_map_rsdt()";
    345 		rv = AE_ERROR;
    346 		goto fail;
    347 	}
    348 
    349 	if (acpi_force_load == 0 && (acpi_find_quirks() & ACPI_QUIRK_BROKEN)) {
    350 		aprint_normal("ACPI: BIOS is listed as broken:\n");
    351 		aprint_normal("ACPI: X/RSDT: OemId <%6.6s,%8.8s,%08x>, "
    352 		       "AslId <%4.4s,%08x>\n",
    353 			rsdt->OemId, rsdt->OemTableId,
    354 		        rsdt->OemRevision,
    355 			rsdt->AslCompilerId,
    356 		        rsdt->AslCompilerRevision);
    357 		aprint_normal("ACPI: Not used. Set acpi_force_load to use.\n");
    358 		acpi_unmap_rsdt(rsdt);
    359 		AcpiTerminate();
    360 		return 0;
    361 	}
    362 
    363 	acpi_unmap_rsdt(rsdt);
    364 
    365 	rv = AcpiEnableSubsystem(~(ACPI_NO_HARDWARE_INIT|ACPI_NO_ACPI_ENABLE));
    366 
    367 	if (ACPI_FAILURE(rv)) {
    368 		func = "AcpiEnableSubsystem()";
    369 		goto fail;
    370 	}
    371 
    372 	/*
    373 	 * Looks like we have ACPI!
    374 	 */
    375 	return 1;
    376 
    377 fail:
    378 	KASSERT(rv != AE_OK);
    379 	KASSERT(func != NULL);
    380 
    381 	aprint_error("%s: failed to probe ACPI: %s\n",
    382 	    func, AcpiFormatException(rv));
    383 
    384 	if (initialized != false)
    385 		(void)AcpiTerminate();
    386 
    387 	return 0;
    388 }
    389 
    390 int
    391 acpi_check(device_t parent, const char *ifattr)
    392 {
    393 	return (config_search_ia(acpi_submatch, parent, ifattr, NULL) != NULL);
    394 }
    395 
    396 /*
    397  * Autoconfiguration.
    398  */
    399 static int
    400 acpi_match(device_t parent, cfdata_t match, void *aux)
    401 {
    402 	/*
    403 	 * XXX: Nada; MD code has called acpi_probe().
    404 	 */
    405 	return 1;
    406 }
    407 
    408 static int
    409 acpi_submatch(device_t parent, cfdata_t cf, const int *locs, void *aux)
    410 {
    411 	struct cfattach *ca;
    412 
    413 	ca = config_cfattach_lookup(cf->cf_name, cf->cf_atname);
    414 
    415 	return (ca == &acpi_ca);
    416 }
    417 
    418 static void
    419 acpi_attach(device_t parent, device_t self, void *aux)
    420 {
    421 	struct acpi_softc *sc = device_private(self);
    422 	struct acpibus_attach_args *aa = aux;
    423 	ACPI_TABLE_HEADER *rsdt;
    424 	ACPI_STATUS rv;
    425 
    426 	aprint_naive("\n");
    427 	aprint_normal(": Intel ACPICA %08x\n", ACPI_CA_VERSION);
    428 
    429 	if (acpi_softc != NULL)
    430 		panic("%s: already attached", __func__);
    431 
    432 	rsdt = acpi_map_rsdt();
    433 
    434 	if (rsdt == NULL)
    435 		aprint_error_dev(self, "X/RSDT: Not found\n");
    436 	else {
    437 		aprint_verbose_dev(self,
    438 		    "X/RSDT: OemId <%6.6s,%8.8s,%08x>, AslId <%4.4s,%08x>\n",
    439 		    rsdt->OemId, rsdt->OemTableId,
    440 		    rsdt->OemRevision,
    441 		    rsdt->AslCompilerId, rsdt->AslCompilerRevision);
    442 	}
    443 
    444 	acpi_unmap_rsdt(rsdt);
    445 
    446 	sc->sc_dev = self;
    447 	sc->sc_root = NULL;
    448 
    449 	sc->sc_sleepstate = ACPI_STATE_S0;
    450 	sc->sc_quirks = acpi_find_quirks();
    451 
    452 	sysmon_power_settype("acpi");
    453 
    454 	sc->sc_iot = aa->aa_iot;
    455 	sc->sc_memt = aa->aa_memt;
    456 	sc->sc_pc = aa->aa_pc;
    457 	sc->sc_pciflags = aa->aa_pciflags;
    458 	sc->sc_ic = aa->aa_ic;
    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 ACPI on-line.
    469 	 */
    470 #ifdef ACPI_DEBUGGER
    471 	if (acpi_dbgr & ACPI_DBGR_ENABLE)
    472 		acpi_osd_debugger();
    473 #endif
    474 
    475 #define ACPI_ENABLE_PHASE1 \
    476     (ACPI_NO_HANDLER_INIT | ACPI_NO_EVENT_INIT)
    477 #define ACPI_ENABLE_PHASE2 \
    478     (ACPI_NO_HARDWARE_INIT | ACPI_NO_ACPI_ENABLE | \
    479      ACPI_NO_ADDRESS_SPACE_INIT)
    480 
    481 	rv = AcpiEnableSubsystem(ACPI_ENABLE_PHASE1);
    482 
    483 	if (ACPI_FAILURE(rv))
    484 		goto fail;
    485 
    486 	acpi_md_callback();
    487 
    488 	rv = AcpiEnableSubsystem(ACPI_ENABLE_PHASE2);
    489 
    490 	if (ACPI_FAILURE(rv))
    491 		goto fail;
    492 
    493 	/*
    494 	 * Early EC handler initialization if ECDT table is available.
    495 	 */
    496 	config_found_ia(self, "acpiecdtbus", aa, NULL);
    497 
    498 	rv = AcpiInitializeObjects(ACPI_FULL_INITIALIZATION);
    499 
    500 	if (ACPI_FAILURE(rv))
    501 		goto fail;
    502 
    503 	/*
    504 	 * Install global notify handlers.
    505 	 */
    506 	rv = AcpiInstallNotifyHandler(ACPI_ROOT_OBJECT,
    507 	    ACPI_SYSTEM_NOTIFY, acpi_notify_handler, NULL);
    508 
    509 	if (ACPI_FAILURE(rv))
    510 		goto fail;
    511 
    512 	rv = AcpiInstallNotifyHandler(ACPI_ROOT_OBJECT,
    513 	    ACPI_DEVICE_NOTIFY, acpi_notify_handler, NULL);
    514 
    515 	if (ACPI_FAILURE(rv))
    516 		goto fail;
    517 
    518 	acpi_active = 1;
    519 
    520 	/* Show SCI interrupt. */
    521 	aprint_verbose_dev(self, "SCI interrupting at int %u\n",
    522 	    AcpiGbl_FADT.SciInterrupt);
    523 
    524 	/*
    525 	 * Install fixed-event handlers.
    526 	 */
    527 	acpi_register_fixed_button(sc, ACPI_EVENT_POWER_BUTTON);
    528 	acpi_register_fixed_button(sc, ACPI_EVENT_SLEEP_BUTTON);
    529 
    530 	acpitimer_init(sc);
    531 
    532 #ifdef ACPI_DEBUGGER
    533 	if (acpi_dbgr & ACPI_DBGR_PROBE)
    534 		acpi_osd_debugger();
    535 #endif
    536 
    537 	/*
    538 	 * Scan the namespace and build our device tree.
    539 	 */
    540 	acpi_build_tree(sc);
    541 	acpi_sleep_init(sc);
    542 
    543 #ifdef ACPI_DEBUGGER
    544 	if (acpi_dbgr & ACPI_DBGR_RUNNING)
    545 		acpi_osd_debugger();
    546 #endif
    547 
    548 #ifdef ACPI_DEBUG
    549 	acpi_debug_init();
    550 #endif
    551 
    552 	return;
    553 
    554 fail:
    555 	KASSERT(rv != AE_OK);
    556 
    557 	aprint_error("%s: failed to initialize ACPI: %s\n",
    558 	    __func__, AcpiFormatException(rv));
    559 }
    560 
    561 /*
    562  * XXX: This is incomplete.
    563  */
    564 static int
    565 acpi_detach(device_t self, int flags)
    566 {
    567 	struct acpi_softc *sc = device_private(self);
    568 	ACPI_STATUS rv;
    569 	int rc;
    570 
    571 	rv = AcpiRemoveNotifyHandler(ACPI_ROOT_OBJECT,
    572 	    ACPI_SYSTEM_NOTIFY, acpi_notify_handler);
    573 
    574 	if (ACPI_FAILURE(rv))
    575 		return EBUSY;
    576 
    577 	rv = AcpiRemoveNotifyHandler(ACPI_ROOT_OBJECT,
    578 	    ACPI_DEVICE_NOTIFY, acpi_notify_handler);
    579 
    580 	if (ACPI_FAILURE(rv))
    581 		return EBUSY;
    582 
    583 	if ((rc = config_detach_children(self, flags)) != 0)
    584 		return rc;
    585 
    586 	if ((rc = acpitimer_detach()) != 0)
    587 		return rc;
    588 
    589 	acpi_deregister_fixed_button(sc, ACPI_EVENT_POWER_BUTTON);
    590 	acpi_deregister_fixed_button(sc, ACPI_EVENT_SLEEP_BUTTON);
    591 
    592 	pmf_device_deregister(self);
    593 
    594 	acpi_softc = NULL;
    595 
    596 	return 0;
    597 }
    598 
    599 /*
    600  * XXX: Need to reclaim any resources? Yes.
    601  */
    602 static void
    603 acpi_childdet(device_t self, device_t child)
    604 {
    605 	struct acpi_softc *sc = device_private(self);
    606 	struct acpi_devnode *ad;
    607 
    608 	if (sc->sc_apmbus == child)
    609 		sc->sc_apmbus = NULL;
    610 
    611 	SIMPLEQ_FOREACH(ad, &sc->ad_head, ad_list) {
    612 
    613 		if (ad->ad_device == child)
    614 			ad->ad_device = NULL;
    615 	}
    616 }
    617 
    618 static bool
    619 acpi_suspend(device_t dv, const pmf_qual_t *qual)
    620 {
    621 
    622 	acpi_suspended = 1;
    623 
    624 	return true;
    625 }
    626 
    627 static bool
    628 acpi_resume(device_t dv, const pmf_qual_t *qual)
    629 {
    630 
    631 	acpi_suspended = 0;
    632 
    633 	return true;
    634 }
    635 
    636 /*
    637  * Namespace scan.
    638  */
    639 static void
    640 acpi_build_tree(struct acpi_softc *sc)
    641 {
    642 	struct acpi_walkcontext awc;
    643 
    644 	/*
    645 	 * Get the root scope handles.
    646 	 */
    647 	KASSERT(__arraycount(acpi_scopes) == 4);
    648 
    649 	(void)AcpiGetHandle(ACPI_ROOT_OBJECT, "\\_PR_", &acpi_scopes[0]);
    650 	(void)AcpiGetHandle(ACPI_ROOT_OBJECT, "\\_SB_", &acpi_scopes[1]);
    651 	(void)AcpiGetHandle(ACPI_ROOT_OBJECT, "\\_SI_", &acpi_scopes[2]);
    652 	(void)AcpiGetHandle(ACPI_ROOT_OBJECT, "\\_TZ_", &acpi_scopes[3]);
    653 
    654 	/*
    655 	 * Make the root node.
    656 	 */
    657 	awc.aw_sc = sc;
    658 	awc.aw_parent = NULL;
    659 
    660 	(void)acpi_make_devnode(ACPI_ROOT_OBJECT, 0, &awc, NULL);
    661 
    662 	KASSERT(sc->sc_root == NULL);
    663 	KASSERT(awc.aw_parent != NULL);
    664 
    665 	sc->sc_root = awc.aw_parent;
    666 
    667 	/*
    668 	 * Build the internal namespace.
    669 	 */
    670 	(void)AcpiWalkNamespace(ACPI_TYPE_ANY, ACPI_ROOT_OBJECT, UINT32_MAX,
    671 	    acpi_make_devnode, acpi_make_devnode_post, &awc, NULL);
    672 
    673 	/*
    674 	 * Scan the internal namespace.
    675 	 */
    676 	(void)acpi_rescan(sc->sc_dev, NULL, NULL);
    677 
    678 	acpi_rescan_capabilities(sc);
    679 
    680 	(void)acpi_pcidev_scan(sc->sc_root);
    681 
    682 	acpi_print_devnodes(sc);
    683 	acpi_print_tree(sc->sc_root, 0);
    684 }
    685 
    686 static ACPI_STATUS
    687 acpi_make_devnode(ACPI_HANDLE handle, uint32_t level,
    688     void *context, void **status)
    689 {
    690 	struct acpi_walkcontext *awc = context;
    691 	struct acpi_softc *sc = awc->aw_sc;
    692 	struct acpi_devnode *ad;
    693 	ACPI_DEVICE_INFO *devinfo;
    694 	ACPI_OBJECT_TYPE type;
    695 	ACPI_NAME_UNION *anu;
    696 	ACPI_STATUS rv;
    697 	int clear, i;
    698 
    699 	rv = AcpiGetObjectInfo(handle, &devinfo);
    700 
    701 	if (ACPI_FAILURE(rv))
    702 		return AE_OK;	/* Do not terminate the walk. */
    703 
    704 	type = devinfo->Type;
    705 
    706 	switch (type) {
    707 
    708 	case ACPI_TYPE_DEVICE:
    709 
    710 #ifdef ACPI_ACTIVATE_DEV
    711 		acpi_activate_device(handle, &devinfo);
    712 #endif
    713 
    714 	case ACPI_TYPE_PROCESSOR:
    715 	case ACPI_TYPE_THERMAL:
    716 	case ACPI_TYPE_POWER:
    717 
    718 		ad = malloc(sizeof(*ad), M_ACPI, M_NOWAIT | M_ZERO);
    719 
    720 		if (ad == NULL)
    721 			return AE_NO_MEMORY;
    722 
    723 		ad->ad_device = NULL;
    724 		ad->ad_notify = NULL;
    725 		ad->ad_pciinfo = NULL;
    726 
    727 		ad->ad_type = type;
    728 		ad->ad_handle = handle;
    729 		ad->ad_devinfo = devinfo;
    730 
    731 		ad->ad_root = sc->sc_dev;
    732 		ad->ad_parent = awc->aw_parent;
    733 
    734 		anu = (ACPI_NAME_UNION *)&devinfo->Name;
    735 		ad->ad_name[4] = '\0';
    736 
    737 		for (i = 3, clear = 0; i >= 0; i--) {
    738 
    739 			if (clear == 0 && anu->Ascii[i] == '_')
    740 				ad->ad_name[i] = '\0';
    741 			else {
    742 				ad->ad_name[i] = anu->Ascii[i];
    743 				clear = 1;
    744 			}
    745 		}
    746 
    747 		if (ad->ad_name[0] == '\0')
    748 			ad->ad_name[0] = '_';
    749 
    750 		SIMPLEQ_INIT(&ad->ad_child_head);
    751 		SIMPLEQ_INSERT_TAIL(&sc->ad_head, ad, ad_list);
    752 
    753 		acpi_set_node(ad);
    754 
    755 		if (ad->ad_parent != NULL) {
    756 
    757 			SIMPLEQ_INSERT_TAIL(&ad->ad_parent->ad_child_head,
    758 			    ad, ad_child_list);
    759 		}
    760 
    761 		awc->aw_parent = ad;
    762 	}
    763 
    764 	return AE_OK;
    765 }
    766 
    767 static ACPI_STATUS
    768 acpi_make_devnode_post(ACPI_HANDLE handle, uint32_t level,
    769     void *context, void **status)
    770 {
    771 	struct acpi_walkcontext *awc = context;
    772 
    773 	KASSERT(awc != NULL);
    774 	KASSERT(awc->aw_parent != NULL);
    775 
    776 	if (handle == awc->aw_parent->ad_handle)
    777 		awc->aw_parent = awc->aw_parent->ad_parent;
    778 
    779 	return AE_OK;
    780 }
    781 
    782 #ifdef ACPI_ACTIVATE_DEV
    783 static void
    784 acpi_activate_device(ACPI_HANDLE handle, ACPI_DEVICE_INFO **di)
    785 {
    786 	static const int valid = ACPI_VALID_STA | ACPI_VALID_HID;
    787 	ACPI_DEVICE_INFO *newdi;
    788 	ACPI_STATUS rv;
    789 	uint32_t old;
    790 
    791 	/*
    792 	 * If the device is valid and present,
    793 	 * but not enabled, try to activate it.
    794 	 */
    795 	if (((*di)->Valid & valid) != valid)
    796 		return;
    797 
    798 	old = (*di)->CurrentStatus;
    799 
    800 	if ((old & (ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_ENABLED)) !=
    801 	    ACPI_STA_DEVICE_PRESENT)
    802 		return;
    803 
    804 	rv = acpi_allocate_resources(handle);
    805 
    806 	if (ACPI_FAILURE(rv))
    807 		goto fail;
    808 
    809 	rv = AcpiGetObjectInfo(handle, &newdi);
    810 
    811 	if (ACPI_FAILURE(rv))
    812 		goto fail;
    813 
    814 	ACPI_FREE(*di);
    815 	*di = newdi;
    816 
    817 	aprint_verbose_dev(acpi_softc->sc_dev,
    818 	    "%s activated, STA 0x%08X -> STA 0x%08X\n",
    819 	    (*di)->HardwareId.String, old, (*di)->CurrentStatus);
    820 
    821 	return;
    822 
    823 fail:
    824 	aprint_error_dev(acpi_softc->sc_dev, "failed to "
    825 	    "activate %s\n", (*di)->HardwareId.String);
    826 }
    827 
    828 /*
    829  * XXX: This very incomplete.
    830  */
    831 ACPI_STATUS
    832 acpi_allocate_resources(ACPI_HANDLE handle)
    833 {
    834 	ACPI_BUFFER bufp, bufc, bufn;
    835 	ACPI_RESOURCE *resp, *resc, *resn;
    836 	ACPI_RESOURCE_IRQ *irq;
    837 	ACPI_RESOURCE_EXTENDED_IRQ *xirq;
    838 	ACPI_STATUS rv;
    839 	uint delta;
    840 
    841 	rv = acpi_get(handle, &bufp, AcpiGetPossibleResources);
    842 	if (ACPI_FAILURE(rv))
    843 		goto out;
    844 	rv = acpi_get(handle, &bufc, AcpiGetCurrentResources);
    845 	if (ACPI_FAILURE(rv)) {
    846 		goto out1;
    847 	}
    848 
    849 	bufn.Length = 1000;
    850 	bufn.Pointer = resn = malloc(bufn.Length, M_ACPI, M_WAITOK);
    851 	resp = bufp.Pointer;
    852 	resc = bufc.Pointer;
    853 	while (resc->Type != ACPI_RESOURCE_TYPE_END_TAG &&
    854 	       resp->Type != ACPI_RESOURCE_TYPE_END_TAG) {
    855 		while (resc->Type != resp->Type && resp->Type != ACPI_RESOURCE_TYPE_END_TAG)
    856 			resp = ACPI_NEXT_RESOURCE(resp);
    857 		if (resp->Type == ACPI_RESOURCE_TYPE_END_TAG)
    858 			break;
    859 		/* Found identical Id */
    860 		resn->Type = resc->Type;
    861 		switch (resc->Type) {
    862 		case ACPI_RESOURCE_TYPE_IRQ:
    863 			memcpy(&resn->Data, &resp->Data,
    864 			       sizeof(ACPI_RESOURCE_IRQ));
    865 			irq = (ACPI_RESOURCE_IRQ *)&resn->Data;
    866 			irq->Interrupts[0] =
    867 			    ((ACPI_RESOURCE_IRQ *)&resp->Data)->
    868 			        Interrupts[irq->InterruptCount-1];
    869 			irq->InterruptCount = 1;
    870 			resn->Length = ACPI_RS_SIZE(ACPI_RESOURCE_IRQ);
    871 			break;
    872 		case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
    873 			memcpy(&resn->Data, &resp->Data,
    874 			       sizeof(ACPI_RESOURCE_EXTENDED_IRQ));
    875 			xirq = (ACPI_RESOURCE_EXTENDED_IRQ *)&resn->Data;
    876 #if 0
    877 			/*
    878 			 * XXX:	Not duplicating the interrupt logic above
    879 			 *	because its not clear what it accomplishes.
    880 			 */
    881 			xirq->Interrupts[0] =
    882 			    ((ACPI_RESOURCE_EXT_IRQ *)&resp->Data)->
    883 			    Interrupts[irq->NumberOfInterrupts-1];
    884 			xirq->NumberOfInterrupts = 1;
    885 #endif
    886 			resn->Length = ACPI_RS_SIZE(ACPI_RESOURCE_EXTENDED_IRQ);
    887 			break;
    888 		case ACPI_RESOURCE_TYPE_IO:
    889 			memcpy(&resn->Data, &resp->Data,
    890 			       sizeof(ACPI_RESOURCE_IO));
    891 			resn->Length = resp->Length;
    892 			break;
    893 		default:
    894 			aprint_error_dev(acpi_softc->sc_dev,
    895 			    "%s: invalid type %u\n", __func__, resc->Type);
    896 			rv = AE_BAD_DATA;
    897 			goto out2;
    898 		}
    899 		resc = ACPI_NEXT_RESOURCE(resc);
    900 		resn = ACPI_NEXT_RESOURCE(resn);
    901 		resp = ACPI_NEXT_RESOURCE(resp);
    902 		delta = (uint8_t *)resn - (uint8_t *)bufn.Pointer;
    903 		if (delta >=
    904 		    bufn.Length-ACPI_RS_SIZE(ACPI_RESOURCE_DATA)) {
    905 			bufn.Length *= 2;
    906 			bufn.Pointer = realloc(bufn.Pointer, bufn.Length,
    907 					       M_ACPI, M_WAITOK);
    908 			resn = (ACPI_RESOURCE *)((uint8_t *)bufn.Pointer +
    909 			    delta);
    910 		}
    911 	}
    912 
    913 	if (resc->Type != ACPI_RESOURCE_TYPE_END_TAG) {
    914 		aprint_error_dev(acpi_softc->sc_dev,
    915 		    "%s: resc not exhausted\n", __func__);
    916 		rv = AE_BAD_DATA;
    917 		goto out3;
    918 	}
    919 
    920 	resn->Type = ACPI_RESOURCE_TYPE_END_TAG;
    921 	rv = AcpiSetCurrentResources(handle, &bufn);
    922 
    923 	if (ACPI_FAILURE(rv))
    924 		aprint_error_dev(acpi_softc->sc_dev, "%s: failed to set "
    925 		    "resources: %s\n", __func__, AcpiFormatException(rv));
    926 
    927 out3:
    928 	free(bufn.Pointer, M_ACPI);
    929 out2:
    930 	ACPI_FREE(bufc.Pointer);
    931 out1:
    932 	ACPI_FREE(bufp.Pointer);
    933 out:
    934 	return rv;
    935 }
    936 #endif /* ACPI_ACTIVATE_DEV */
    937 
    938 /*
    939  * Device attachment.
    940  */
    941 static int
    942 acpi_rescan(device_t self, const char *ifattr, const int *locators)
    943 {
    944 	struct acpi_softc *sc = device_private(self);
    945 
    946 	if (ifattr_match(ifattr, "acpinodebus"))
    947 		acpi_rescan_nodes(sc);
    948 
    949 	if (ifattr_match(ifattr, "acpiapmbus") && sc->sc_apmbus == NULL)
    950 		sc->sc_apmbus = config_found_ia(sc->sc_dev,
    951 		    "acpiapmbus", NULL, NULL);
    952 
    953 	return 0;
    954 }
    955 
    956 static void
    957 acpi_rescan_nodes(struct acpi_softc *sc)
    958 {
    959 	struct acpi_attach_args aa;
    960 	struct acpi_devnode *ad;
    961 	ACPI_DEVICE_INFO *di;
    962 
    963 	SIMPLEQ_FOREACH(ad, &sc->ad_head, ad_list) {
    964 
    965 		if (ad->ad_device != NULL)
    966 			continue;
    967 
    968 		/*
    969 		 * There is a bug in ACPICA: it defines the type
    970 		 * of the scopes incorrectly for its own reasons.
    971 		 */
    972 		if (acpi_is_scope(ad) != false)
    973 			continue;
    974 
    975 		di = ad->ad_devinfo;
    976 
    977 		/*
    978 		 * We only attach devices which are present, enabled, and
    979 		 * functioning properly. However, if a device is enabled,
    980 		 * it is decoding resources and we should claim these,
    981 		 * if possible. This requires changes to bus_space(9).
    982 		 */
    983 		if (di->Type == ACPI_TYPE_DEVICE) {
    984 
    985 			if ((di->Valid & ACPI_VALID_STA) != 0 &&
    986 			    (di->CurrentStatus & ACPI_STA_OK) != ACPI_STA_OK)
    987 				continue;
    988 		}
    989 
    990 		/*
    991 		 * The same problem as above. As for example
    992 		 * thermal zones and power resources do not
    993 		 * have a valid HID, only evaluate devices.
    994 		 */
    995 		if (di->Type == ACPI_TYPE_DEVICE &&
    996 		   (di->Valid & ACPI_VALID_HID) == 0)
    997 			continue;
    998 
    999 		/*
   1000 		 * Handled internally.
   1001 		 */
   1002 		if (di->Type == ACPI_TYPE_POWER)
   1003 			continue;
   1004 
   1005 		/*
   1006 		 * Skip ignored HIDs.
   1007 		 */
   1008 		if (acpi_match_hid(di, acpi_ignored_ids))
   1009 			continue;
   1010 
   1011 		aa.aa_node = ad;
   1012 		aa.aa_iot = sc->sc_iot;
   1013 		aa.aa_memt = sc->sc_memt;
   1014 		aa.aa_pc = sc->sc_pc;
   1015 		aa.aa_pciflags = sc->sc_pciflags;
   1016 		aa.aa_ic = sc->sc_ic;
   1017 
   1018 		ad->ad_device = config_found_ia(sc->sc_dev,
   1019 		    "acpinodebus", &aa, acpi_print);
   1020 	}
   1021 }
   1022 
   1023 static void
   1024 acpi_rescan_capabilities(struct acpi_softc *sc)
   1025 {
   1026 	struct acpi_devnode *ad;
   1027 	ACPI_HANDLE tmp;
   1028 	ACPI_STATUS rv;
   1029 
   1030 	SIMPLEQ_FOREACH(ad, &sc->ad_head, ad_list) {
   1031 
   1032 		if (ad->ad_devinfo->Type != ACPI_TYPE_DEVICE)
   1033 			continue;
   1034 
   1035 		/*
   1036 		 * Scan power resource capabilities.
   1037 		 *
   1038 		 * If any power states are supported,
   1039 		 * at least _PR0 and _PR3 must be present.
   1040 		 */
   1041 		rv = AcpiGetHandle(ad->ad_handle, "_PR0", &tmp);
   1042 
   1043 		if (ACPI_SUCCESS(rv)) {
   1044 			ad->ad_flags |= ACPI_DEVICE_POWER;
   1045 			acpi_power_add(ad);
   1046 		}
   1047 
   1048 		/*
   1049 		 * Scan wake-up capabilities.
   1050 		 */
   1051 		rv = AcpiGetHandle(ad->ad_handle, "_PRW", &tmp);
   1052 
   1053 		if (ACPI_SUCCESS(rv)) {
   1054 			ad->ad_flags |= ACPI_DEVICE_WAKEUP;
   1055 			acpi_wakedev_add(ad);
   1056 		}
   1057 	}
   1058 }
   1059 
   1060 static int
   1061 acpi_print(void *aux, const char *pnp)
   1062 {
   1063 	struct acpi_attach_args *aa = aux;
   1064 	ACPI_STATUS rv;
   1065 
   1066 	if (pnp) {
   1067 		if (aa->aa_node->ad_devinfo->Valid & ACPI_VALID_HID) {
   1068 			char *pnpstr =
   1069 			    aa->aa_node->ad_devinfo->HardwareId.String;
   1070 			ACPI_BUFFER buf;
   1071 
   1072 			aprint_normal("%s (%s) ", aa->aa_node->ad_name,
   1073 			    pnpstr);
   1074 
   1075 			rv = acpi_eval_struct(aa->aa_node->ad_handle,
   1076 			    "_STR", &buf);
   1077 			if (ACPI_SUCCESS(rv)) {
   1078 				ACPI_OBJECT *obj = buf.Pointer;
   1079 				switch (obj->Type) {
   1080 				case ACPI_TYPE_STRING:
   1081 					aprint_normal("[%s] ", obj->String.Pointer);
   1082 					break;
   1083 				case ACPI_TYPE_BUFFER:
   1084 					aprint_normal("buffer %p ", obj->Buffer.Pointer);
   1085 					break;
   1086 				default:
   1087 					aprint_normal("type %u ",obj->Type);
   1088 					break;
   1089 				}
   1090 				ACPI_FREE(buf.Pointer);
   1091 			}
   1092 			else
   1093 				acpi_print_dev(pnpstr);
   1094 
   1095 			aprint_normal("at %s", pnp);
   1096 		} else if (aa->aa_node->ad_devinfo->Type != ACPI_TYPE_DEVICE) {
   1097 			aprint_normal("%s (ACPI Object Type '%s' "
   1098 			    "[0x%02x]) ", aa->aa_node->ad_name,
   1099 			     AcpiUtGetTypeName(aa->aa_node->ad_devinfo->Type),
   1100 			     aa->aa_node->ad_devinfo->Type);
   1101 			aprint_normal("at %s", pnp);
   1102 		} else
   1103 			return 0;
   1104 	} else {
   1105 		aprint_normal(" (%s", aa->aa_node->ad_name);
   1106 		if (aa->aa_node->ad_devinfo->Valid & ACPI_VALID_HID) {
   1107 			aprint_normal(", %s", aa->aa_node->ad_devinfo->HardwareId.String);
   1108 			if (aa->aa_node->ad_devinfo->Valid & ACPI_VALID_UID) {
   1109 				const char *uid;
   1110 
   1111 				uid = aa->aa_node->ad_devinfo->UniqueId.String;
   1112 				if (uid[0] == '\0')
   1113 					uid = "<null>";
   1114 				aprint_normal("-%s", uid);
   1115 			}
   1116 		}
   1117 		aprint_normal(")");
   1118 	}
   1119 
   1120 	return UNCONF;
   1121 }
   1122 
   1123 /*
   1124  * Notify.
   1125  */
   1126 static void
   1127 acpi_notify_handler(ACPI_HANDLE handle, uint32_t event, void *aux)
   1128 {
   1129 	struct acpi_softc *sc = acpi_softc;
   1130 	struct acpi_devnode *ad;
   1131 
   1132 	KASSERT(sc != NULL);
   1133 	KASSERT(aux == NULL);
   1134 	KASSERT(acpi_active != 0);
   1135 
   1136 	if (acpi_suspended != 0)
   1137 		return;
   1138 
   1139 	/*
   1140 	 *  System: 0x00 - 0x7F.
   1141 	 *  Device: 0x80 - 0xFF.
   1142 	 */
   1143 	switch (event) {
   1144 
   1145 	case ACPI_NOTIFY_BUS_CHECK:
   1146 	case ACPI_NOTIFY_DEVICE_CHECK:
   1147 	case ACPI_NOTIFY_DEVICE_WAKE:
   1148 	case ACPI_NOTIFY_EJECT_REQUEST:
   1149 	case ACPI_NOTIFY_DEVICE_CHECK_LIGHT:
   1150 	case ACPI_NOTIFY_FREQUENCY_MISMATCH:
   1151 	case ACPI_NOTIFY_BUS_MODE_MISMATCH:
   1152 	case ACPI_NOTIFY_POWER_FAULT:
   1153 	case ACPI_NOTIFY_CAPABILITIES_CHECK:
   1154 	case ACPI_NOTIFY_DEVICE_PLD_CHECK:
   1155 	case ACPI_NOTIFY_RESERVED:
   1156 	case ACPI_NOTIFY_LOCALITY_UPDATE:
   1157 		break;
   1158 	}
   1159 
   1160 	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "notification 0x%02X for "
   1161 		"%s (%p)\n", event, acpi_name(handle), handle));
   1162 
   1163 	/*
   1164 	 * We deliver notifications only to drivers
   1165 	 * that have been succesfully attached and
   1166 	 * that have registered a handler with us.
   1167 	 * The opaque pointer is always the device_t.
   1168 	 */
   1169 	SIMPLEQ_FOREACH(ad, &sc->ad_head, ad_list) {
   1170 
   1171 		if (ad->ad_device == NULL)
   1172 			continue;
   1173 
   1174 		if (ad->ad_notify == NULL)
   1175 			continue;
   1176 
   1177 		if (ad->ad_handle != handle)
   1178 			continue;
   1179 
   1180 		(*ad->ad_notify)(ad->ad_handle, event, ad->ad_device);
   1181 
   1182 		return;
   1183 	}
   1184 
   1185 	aprint_debug_dev(sc->sc_dev, "unhandled notify 0x%02X "
   1186 	    "for %s (%p)\n", event, acpi_name(handle), handle);
   1187 }
   1188 
   1189 bool
   1190 acpi_register_notify(struct acpi_devnode *ad, ACPI_NOTIFY_HANDLER notify)
   1191 {
   1192 	struct acpi_softc *sc = acpi_softc;
   1193 
   1194 	KASSERT(sc != NULL);
   1195 	KASSERT(acpi_active != 0);
   1196 
   1197 	if (acpi_suspended != 0)
   1198 		goto fail;
   1199 
   1200 	if (ad == NULL || notify == NULL)
   1201 		goto fail;
   1202 
   1203 	ad->ad_notify = notify;
   1204 
   1205 	return true;
   1206 
   1207 fail:
   1208 	aprint_error_dev(sc->sc_dev, "failed to register notify "
   1209 	    "handler for %s (%p)\n", ad->ad_name, ad->ad_handle);
   1210 
   1211 	return false;
   1212 }
   1213 
   1214 void
   1215 acpi_deregister_notify(struct acpi_devnode *ad)
   1216 {
   1217 
   1218 	ad->ad_notify = NULL;
   1219 }
   1220 
   1221 /*
   1222  * Fixed buttons.
   1223  */
   1224 static void
   1225 acpi_register_fixed_button(struct acpi_softc *sc, int event)
   1226 {
   1227 	struct sysmon_pswitch *smpsw;
   1228 	ACPI_STATUS rv;
   1229 	int type;
   1230 
   1231 	switch (event) {
   1232 
   1233 	case ACPI_EVENT_POWER_BUTTON:
   1234 
   1235 		if ((AcpiGbl_FADT.Flags & ACPI_FADT_POWER_BUTTON) != 0)
   1236 			return;
   1237 
   1238 		type = PSWITCH_TYPE_POWER;
   1239 		smpsw = &sc->sc_smpsw_power;
   1240 		break;
   1241 
   1242 	case ACPI_EVENT_SLEEP_BUTTON:
   1243 
   1244 		if ((AcpiGbl_FADT.Flags & ACPI_FADT_SLEEP_BUTTON) != 0)
   1245 			return;
   1246 
   1247 		type = PSWITCH_TYPE_SLEEP;
   1248 		smpsw = &sc->sc_smpsw_sleep;
   1249 		break;
   1250 
   1251 	default:
   1252 		rv = AE_TYPE;
   1253 		goto fail;
   1254 	}
   1255 
   1256 	smpsw->smpsw_type = type;
   1257 	smpsw->smpsw_name = device_xname(sc->sc_dev);
   1258 
   1259 	if (sysmon_pswitch_register(smpsw) != 0) {
   1260 		rv = AE_ERROR;
   1261 		goto fail;
   1262 	}
   1263 
   1264 	rv = AcpiInstallFixedEventHandler(event,
   1265 	    acpi_fixed_button_handler, smpsw);
   1266 
   1267 	if (ACPI_FAILURE(rv)) {
   1268 		sysmon_pswitch_unregister(smpsw);
   1269 		goto fail;
   1270 	}
   1271 
   1272 	aprint_debug_dev(sc->sc_dev, "fixed %s button present\n",
   1273 	    (type != ACPI_EVENT_SLEEP_BUTTON) ? "power" : "sleep");
   1274 
   1275 	return;
   1276 
   1277 fail:
   1278 	aprint_error_dev(sc->sc_dev, "failed to register "
   1279 	    "fixed event: %s\n", AcpiFormatException(rv));
   1280 }
   1281 
   1282 static void
   1283 acpi_deregister_fixed_button(struct acpi_softc *sc, int event)
   1284 {
   1285 	struct sysmon_pswitch *smpsw;
   1286 	ACPI_STATUS rv;
   1287 
   1288 	switch (event) {
   1289 
   1290 	case ACPI_EVENT_POWER_BUTTON:
   1291 		smpsw = &sc->sc_smpsw_power;
   1292 
   1293 		if ((AcpiGbl_FADT.Flags & ACPI_FADT_POWER_BUTTON) != 0) {
   1294 			KASSERT(smpsw->smpsw_type != PSWITCH_TYPE_POWER);
   1295 			return;
   1296 		}
   1297 
   1298 		break;
   1299 
   1300 	case ACPI_EVENT_SLEEP_BUTTON:
   1301 		smpsw = &sc->sc_smpsw_sleep;
   1302 
   1303 		if ((AcpiGbl_FADT.Flags & ACPI_FADT_SLEEP_BUTTON) != 0) {
   1304 			KASSERT(smpsw->smpsw_type != PSWITCH_TYPE_SLEEP);
   1305 			return;
   1306 		}
   1307 
   1308 		break;
   1309 
   1310 	default:
   1311 		rv = AE_TYPE;
   1312 		goto fail;
   1313 	}
   1314 
   1315 	rv = AcpiRemoveFixedEventHandler(event, acpi_fixed_button_handler);
   1316 
   1317 	if (ACPI_SUCCESS(rv)) {
   1318 		sysmon_pswitch_unregister(smpsw);
   1319 		return;
   1320 	}
   1321 
   1322 fail:
   1323 	aprint_error_dev(sc->sc_dev, "failed to deregister "
   1324 	    "fixed event: %s\n", AcpiFormatException(rv));
   1325 }
   1326 
   1327 static uint32_t
   1328 acpi_fixed_button_handler(void *context)
   1329 {
   1330 	static const int handler = OSL_NOTIFY_HANDLER;
   1331 	struct sysmon_pswitch *smpsw = context;
   1332 
   1333 	(void)AcpiOsExecute(handler, acpi_fixed_button_pressed, smpsw);
   1334 
   1335 	return ACPI_INTERRUPT_HANDLED;
   1336 }
   1337 
   1338 static void
   1339 acpi_fixed_button_pressed(void *context)
   1340 {
   1341 	struct sysmon_pswitch *smpsw = context;
   1342 
   1343 	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "%s fixed button pressed\n",
   1344 		(smpsw->smpsw_type != ACPI_EVENT_SLEEP_BUTTON) ?
   1345 		"power" : "sleep"));
   1346 
   1347 	sysmon_pswitch_event(smpsw, PSWITCH_EVENT_PRESSED);
   1348 }
   1349 
   1350 /*
   1351  * Sleep.
   1352  */
   1353 static void
   1354 acpi_sleep_init(struct acpi_softc *sc)
   1355 {
   1356 	uint8_t a, b, i;
   1357 	ACPI_STATUS rv;
   1358 
   1359 	CTASSERT(ACPI_STATE_S0 == 0 && ACPI_STATE_S1 == 1);
   1360 	CTASSERT(ACPI_STATE_S2 == 2 && ACPI_STATE_S3 == 3);
   1361 	CTASSERT(ACPI_STATE_S4 == 4 && ACPI_STATE_S5 == 5);
   1362 
   1363 	/*
   1364 	 * Evaluate supported sleep states.
   1365 	 */
   1366 	for (i = ACPI_STATE_S0; i <= ACPI_STATE_S5; i++) {
   1367 
   1368 		rv = AcpiGetSleepTypeData(i, &a, &b);
   1369 
   1370 		if (ACPI_SUCCESS(rv))
   1371 			sc->sc_sleepstates |= __BIT(i);
   1372 	}
   1373 }
   1374 
   1375 void
   1376 acpi_enter_sleep_state(struct acpi_softc *sc, int state)
   1377 {
   1378 	ACPI_STATUS rv;
   1379 	int err;
   1380 
   1381 	if (state == sc->sc_sleepstate)
   1382 		return;
   1383 
   1384 	aprint_normal_dev(sc->sc_dev, "entering state S%d\n", state);
   1385 
   1386 	switch (state) {
   1387 
   1388 	case ACPI_STATE_S0:
   1389 		sc->sc_sleepstate = ACPI_STATE_S0;
   1390 		return;
   1391 
   1392 	case ACPI_STATE_S1:
   1393 	case ACPI_STATE_S2:
   1394 	case ACPI_STATE_S3:
   1395 	case ACPI_STATE_S4:
   1396 
   1397 		if ((sc->sc_sleepstates & __BIT(state)) == 0) {
   1398 			aprint_error_dev(sc->sc_dev, "sleep state "
   1399 			    "S%d is not available\n", state);
   1400 			return;
   1401 		}
   1402 
   1403 		/*
   1404 		 * Evaluate the _TTS method. This should be done before
   1405 		 * pmf_system_suspend(9) and the evaluation of _PTS.
   1406 		 * We should also re-evaluate this once we return to
   1407 		 * S0 or if we abort the sleep state transition in the
   1408 		 * middle (see ACPI 3.0, section 7.3.6). In reality,
   1409 		 * however, the _TTS method is seldom seen in the field.
   1410 		 */
   1411 		rv = acpi_eval_set_integer(NULL, "\\_TTS", state);
   1412 
   1413 		if (ACPI_SUCCESS(rv))
   1414 			aprint_debug_dev(sc->sc_dev, "evaluated _TTS\n");
   1415 
   1416 		if (state != ACPI_STATE_S1 &&
   1417 		    pmf_system_suspend(PMF_Q_NONE) != true) {
   1418 			aprint_error_dev(sc->sc_dev, "aborting suspend\n");
   1419 			break;
   1420 		}
   1421 
   1422 		/*
   1423 		 * This will evaluate the  _PTS and _SST methods,
   1424 		 * but unlike the documentation claims, not _GTS,
   1425 		 * which is evaluated in AcpiEnterSleepState().
   1426 		 * This must be called with interrupts enabled.
   1427 		 */
   1428 		rv = AcpiEnterSleepStatePrep(state);
   1429 
   1430 		if (ACPI_FAILURE(rv)) {
   1431 			aprint_error_dev(sc->sc_dev, "failed to prepare "
   1432 			    "S%d: %s\n", state, AcpiFormatException(rv));
   1433 			break;
   1434 		}
   1435 
   1436 		/*
   1437 		 * After the _PTS method has been evaluated, we can
   1438 		 * enable wake and evaluate _PSW (ACPI 4.0, p. 284).
   1439 		 */
   1440 		acpi_wakedev_commit(sc, state);
   1441 
   1442 		sc->sc_sleepstate = state;
   1443 
   1444 		if (state == ACPI_STATE_S1) {
   1445 
   1446 			/* Just enter the state. */
   1447 			acpi_md_OsDisableInterrupt();
   1448 			rv = AcpiEnterSleepState(state);
   1449 
   1450 			if (ACPI_FAILURE(rv))
   1451 				aprint_error_dev(sc->sc_dev, "failed to "
   1452 				    "enter S1: %s\n", AcpiFormatException(rv));
   1453 
   1454 			(void)AcpiLeaveSleepState(state);
   1455 
   1456 		} else {
   1457 
   1458 			err = acpi_md_sleep(state);
   1459 
   1460 			if (state == ACPI_STATE_S4)
   1461 				AcpiEnable();
   1462 
   1463 			pmf_system_bus_resume(PMF_Q_NONE);
   1464 			(void)AcpiLeaveSleepState(state);
   1465 			pmf_system_resume(PMF_Q_NONE);
   1466 		}
   1467 
   1468 		break;
   1469 
   1470 	case ACPI_STATE_S5:
   1471 
   1472 		(void)acpi_eval_set_integer(NULL, "\\_TTS", ACPI_STATE_S5);
   1473 
   1474 		rv = AcpiEnterSleepStatePrep(ACPI_STATE_S5);
   1475 
   1476 		if (ACPI_FAILURE(rv)) {
   1477 			aprint_error_dev(sc->sc_dev, "failed to prepare "
   1478 			    "S%d: %s\n", state, AcpiFormatException(rv));
   1479 			break;
   1480 		}
   1481 
   1482 		DELAY(1000000);
   1483 
   1484 		sc->sc_sleepstate = state;
   1485 		acpi_md_OsDisableInterrupt();
   1486 
   1487 		(void)AcpiEnterSleepState(ACPI_STATE_S5);
   1488 
   1489 		aprint_error_dev(sc->sc_dev, "WARNING: powerdown failed!\n");
   1490 
   1491 		break;
   1492 	}
   1493 
   1494 	sc->sc_sleepstate = ACPI_STATE_S0;
   1495 
   1496 	(void)acpi_eval_set_integer(NULL, "\\_TTS", ACPI_STATE_S0);
   1497 }
   1498 
   1499 /*
   1500  * Sysctl.
   1501  */
   1502 SYSCTL_SETUP(sysctl_acpi_setup, "sysctl hw.acpi subtree setup")
   1503 {
   1504 	const struct sysctlnode *mnode, *rnode;
   1505 	int err;
   1506 
   1507 	err = sysctl_createv(clog, 0, NULL, &rnode,
   1508 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "hw",
   1509 	    NULL, NULL, 0, NULL, 0,
   1510 	    CTL_HW, CTL_EOL);
   1511 
   1512 	if (err != 0)
   1513 		return;
   1514 
   1515 	err = sysctl_createv(clog, 0, &rnode, &rnode,
   1516 	    CTLFLAG_PERMANENT, CTLTYPE_NODE,
   1517 	    "acpi", SYSCTL_DESCR("ACPI subsystem parameters"),
   1518 	    NULL, 0, NULL, 0,
   1519 	    CTL_CREATE, CTL_EOL);
   1520 
   1521 	if (err != 0)
   1522 		return;
   1523 
   1524 	(void)sysctl_createv(NULL, 0, &rnode, NULL,
   1525 	    CTLFLAG_PERMANENT | CTLFLAG_READONLY, CTLTYPE_QUAD,
   1526 	    "root", SYSCTL_DESCR("ACPI root pointer"),
   1527 	    NULL, 0, &acpi_root_pointer, sizeof(acpi_root_pointer),
   1528 	    CTL_CREATE, CTL_EOL);
   1529 
   1530 	(void)sysctl_createv(NULL, 0, &rnode, NULL,
   1531 	    CTLFLAG_PERMANENT | CTLFLAG_READONLY, CTLTYPE_STRING,
   1532 	    "supported_states", SYSCTL_DESCR("Supported system states"),
   1533 	    sysctl_hw_acpi_sleepstates, 0, NULL, 0,
   1534 	    CTL_CREATE, CTL_EOL);
   1535 
   1536 	err = sysctl_createv(NULL, 0, NULL, &mnode,
   1537 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "machdep",
   1538 	    NULL, NULL, 0, NULL, 0,
   1539 	    CTL_MACHDEP, CTL_EOL);
   1540 
   1541 	if (err == 0) {
   1542 
   1543 		(void)sysctl_createv(NULL, 0, &mnode, NULL,
   1544 		    CTLFLAG_PERMANENT | CTLFLAG_READWRITE, CTLTYPE_INT,
   1545 		    "sleep_state", SYSCTL_DESCR("System sleep state"),
   1546 		    sysctl_hw_acpi_sleepstate, 0, NULL, 0,
   1547 		    CTL_CREATE, CTL_EOL);
   1548 	}
   1549 
   1550 	err = sysctl_createv(clog, 0, &rnode, &rnode,
   1551 	    CTLFLAG_PERMANENT, CTLTYPE_NODE,
   1552 	    "stat", SYSCTL_DESCR("ACPI statistics"),
   1553 	    NULL, 0, NULL, 0,
   1554 	    CTL_CREATE, CTL_EOL);
   1555 
   1556 	if (err != 0)
   1557 		return;
   1558 
   1559 	(void)sysctl_createv(clog, 0, &rnode, NULL,
   1560 	    CTLFLAG_PERMANENT | CTLFLAG_READONLY, CTLTYPE_QUAD,
   1561 	    "gpe", SYSCTL_DESCR("Number of dispatched GPEs"),
   1562 	    NULL, 0, &AcpiGpeCount, sizeof(AcpiGpeCount),
   1563 	    CTL_CREATE, CTL_EOL);
   1564 
   1565 	(void)sysctl_createv(clog, 0, &rnode, NULL,
   1566 	    CTLFLAG_PERMANENT | CTLFLAG_READONLY, CTLTYPE_QUAD,
   1567 	    "sci", SYSCTL_DESCR("Number of SCI interrupts"),
   1568 	    NULL, 0, &AcpiSciCount, sizeof(AcpiSciCount),
   1569 	    CTL_CREATE, CTL_EOL);
   1570 
   1571 	(void)sysctl_createv(clog, 0, &rnode, NULL,
   1572 	    CTLFLAG_PERMANENT | CTLFLAG_READONLY, CTLTYPE_QUAD,
   1573 	    "fixed", SYSCTL_DESCR("Number of fixed events"),
   1574 	    sysctl_hw_acpi_fixedstats, 0, NULL, 0,
   1575 	    CTL_CREATE, CTL_EOL);
   1576 
   1577 	(void)sysctl_createv(clog, 0, &rnode, NULL,
   1578 	    CTLFLAG_PERMANENT | CTLFLAG_READONLY, CTLTYPE_QUAD,
   1579 	    "method", SYSCTL_DESCR("Number of methods executed"),
   1580 	    NULL, 0, &AcpiMethodCount, sizeof(AcpiMethodCount),
   1581 	    CTL_CREATE, CTL_EOL);
   1582 
   1583 	CTASSERT(sizeof(AcpiGpeCount) == sizeof(uint64_t));
   1584 	CTASSERT(sizeof(AcpiSciCount) == sizeof(uint64_t));
   1585 }
   1586 
   1587 static int
   1588 sysctl_hw_acpi_fixedstats(SYSCTLFN_ARGS)
   1589 {
   1590 	struct sysctlnode node;
   1591 	uint64_t t;
   1592 	int err, i;
   1593 
   1594 	for (i = t = 0; i < __arraycount(AcpiFixedEventCount); i++)
   1595 		t += AcpiFixedEventCount[i];
   1596 
   1597 	node = *rnode;
   1598 	node.sysctl_data = &t;
   1599 
   1600 	err = sysctl_lookup(SYSCTLFN_CALL(&node));
   1601 
   1602 	if (err || newp == NULL)
   1603 		return err;
   1604 
   1605 	return 0;
   1606 }
   1607 
   1608 static int
   1609 sysctl_hw_acpi_sleepstate(SYSCTLFN_ARGS)
   1610 {
   1611 	struct acpi_softc *sc = acpi_softc;
   1612 	struct sysctlnode node;
   1613 	int err, t;
   1614 
   1615 	if (acpi_softc == NULL)
   1616 		return ENOSYS;
   1617 
   1618 	node = *rnode;
   1619 	t = sc->sc_sleepstate;
   1620 	node.sysctl_data = &t;
   1621 
   1622 	err = sysctl_lookup(SYSCTLFN_CALL(&node));
   1623 
   1624 	if (err || newp == NULL)
   1625 		return err;
   1626 
   1627 	if (t < ACPI_STATE_S0 || t > ACPI_STATE_S5)
   1628 		return EINVAL;
   1629 
   1630 	acpi_enter_sleep_state(sc, t);
   1631 
   1632 	return 0;
   1633 }
   1634 
   1635 static int
   1636 sysctl_hw_acpi_sleepstates(SYSCTLFN_ARGS)
   1637 {
   1638 	struct acpi_softc *sc = acpi_softc;
   1639 	struct sysctlnode node;
   1640 	char t[3 * 6 + 1];
   1641 	int err;
   1642 
   1643 	if (acpi_softc == NULL)
   1644 		return ENOSYS;
   1645 
   1646 	(void)memset(t, '\0', sizeof(t));
   1647 
   1648 	(void)snprintf(t, sizeof(t), "%s%s%s%s%s%s",
   1649 	    ((sc->sc_sleepstates & __BIT(0)) != 0) ? "S0 " : "",
   1650 	    ((sc->sc_sleepstates & __BIT(1)) != 0) ? "S1 " : "",
   1651 	    ((sc->sc_sleepstates & __BIT(2)) != 0) ? "S2 " : "",
   1652 	    ((sc->sc_sleepstates & __BIT(3)) != 0) ? "S3 " : "",
   1653 	    ((sc->sc_sleepstates & __BIT(4)) != 0) ? "S4 " : "",
   1654 	    ((sc->sc_sleepstates & __BIT(5)) != 0) ? "S5 " : "");
   1655 
   1656 	node = *rnode;
   1657 	node.sysctl_data = &t;
   1658 
   1659 	err = sysctl_lookup(SYSCTLFN_CALL(&node));
   1660 
   1661 	if (err || newp == NULL)
   1662 		return err;
   1663 
   1664 	return 0;
   1665 }
   1666 
   1667 /*
   1668  * Miscellaneous.
   1669  */
   1670 static bool
   1671 acpi_is_scope(struct acpi_devnode *ad)
   1672 {
   1673 	int i;
   1674 
   1675 	/*
   1676 	 * Return true if the node is a root scope.
   1677 	 */
   1678 	if (ad->ad_parent == NULL)
   1679 		return false;
   1680 
   1681 	if (ad->ad_parent->ad_handle != ACPI_ROOT_OBJECT)
   1682 		return false;
   1683 
   1684 	for (i = 0; i < __arraycount(acpi_scopes); i++) {
   1685 
   1686 		if (acpi_scopes[i] == NULL)
   1687 			continue;
   1688 
   1689 		if (ad->ad_handle == acpi_scopes[i])
   1690 			return true;
   1691 	}
   1692 
   1693 	return false;
   1694 }
   1695 
   1696 ACPI_PHYSICAL_ADDRESS
   1697 acpi_OsGetRootPointer(void)
   1698 {
   1699 	ACPI_PHYSICAL_ADDRESS PhysicalAddress;
   1700 
   1701 	/*
   1702 	 * We let MD code handle this since there are multiple ways to do it:
   1703 	 *
   1704 	 *	IA-32: Use AcpiFindRootPointer() to locate the RSDP.
   1705 	 *
   1706 	 *	IA-64: Use the EFI.
   1707 	 */
   1708 	PhysicalAddress = acpi_md_OsGetRootPointer();
   1709 
   1710 	if (acpi_root_pointer == 0)
   1711 		acpi_root_pointer = PhysicalAddress;
   1712 
   1713 	return PhysicalAddress;
   1714 }
   1715 
   1716 static ACPI_TABLE_HEADER *
   1717 acpi_map_rsdt(void)
   1718 {
   1719 	ACPI_PHYSICAL_ADDRESS paddr;
   1720 	ACPI_TABLE_RSDP *rsdp;
   1721 
   1722 	paddr = AcpiOsGetRootPointer();
   1723 
   1724 	if (paddr == 0)
   1725 		return NULL;
   1726 
   1727 	rsdp = AcpiOsMapMemory(paddr, sizeof(ACPI_TABLE_RSDP));
   1728 
   1729 	if (rsdp == NULL)
   1730 		return NULL;
   1731 
   1732 	if (rsdp->Revision > 1 && rsdp->XsdtPhysicalAddress)
   1733 		paddr = rsdp->XsdtPhysicalAddress;
   1734 	else
   1735 		paddr = rsdp->RsdtPhysicalAddress;
   1736 
   1737 	AcpiOsUnmapMemory(rsdp, sizeof(ACPI_TABLE_RSDP));
   1738 
   1739 	return AcpiOsMapMemory(paddr, sizeof(ACPI_TABLE_HEADER));
   1740 }
   1741 
   1742 static void
   1743 acpi_unmap_rsdt(ACPI_TABLE_HEADER *rsdt)
   1744 {
   1745 
   1746 	if (rsdt == NULL)
   1747 		return;
   1748 
   1749 	AcpiOsUnmapMemory(rsdt, sizeof(ACPI_TABLE_HEADER));
   1750 }
   1751