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