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