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