Home | History | Annotate | Line # | Download | only in acpi
acpi.c revision 1.107
      1  1.107  jmcneill /*	$NetBSD: acpi.c,v 1.107 2007/12/15 02:19:55 jmcneill 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  * 3. All advertising materials mentioning features or use of this software
     19   1.50   mycroft  *    must display the following acknowledgement:
     20   1.50   mycroft  *	This product includes software developed by the NetBSD
     21   1.50   mycroft  *	Foundation, Inc. and its contributors.
     22   1.50   mycroft  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23   1.50   mycroft  *    contributors may be used to endorse or promote products derived
     24   1.50   mycroft  *    from this software without specific prior written permission.
     25   1.50   mycroft  *
     26   1.50   mycroft  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27   1.50   mycroft  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28   1.50   mycroft  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29   1.50   mycroft  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30   1.50   mycroft  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31   1.50   mycroft  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32   1.50   mycroft  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33   1.50   mycroft  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34   1.50   mycroft  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35   1.50   mycroft  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36   1.50   mycroft  * POSSIBILITY OF SUCH DAMAGE.
     37   1.50   mycroft  */
     38    1.1   thorpej 
     39    1.1   thorpej /*
     40   1.34   thorpej  * Copyright 2001, 2003 Wasabi Systems, Inc.
     41    1.1   thorpej  * All rights reserved.
     42    1.1   thorpej  *
     43    1.1   thorpej  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
     44    1.1   thorpej  *
     45    1.1   thorpej  * Redistribution and use in source and binary forms, with or without
     46    1.1   thorpej  * modification, are permitted provided that the following conditions
     47    1.1   thorpej  * are met:
     48    1.1   thorpej  * 1. Redistributions of source code must retain the above copyright
     49    1.1   thorpej  *    notice, this list of conditions and the following disclaimer.
     50    1.1   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     51    1.1   thorpej  *    notice, this list of conditions and the following disclaimer in the
     52    1.1   thorpej  *    documentation and/or other materials provided with the distribution.
     53    1.1   thorpej  * 3. All advertising materials mentioning features or use of this software
     54    1.1   thorpej  *    must display the following acknowledgement:
     55    1.1   thorpej  *	This product includes software developed for the NetBSD Project by
     56    1.1   thorpej  *	Wasabi Systems, Inc.
     57    1.1   thorpej  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     58    1.1   thorpej  *    or promote products derived from this software without specific prior
     59    1.1   thorpej  *    written permission.
     60    1.1   thorpej  *
     61    1.1   thorpej  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     62    1.1   thorpej  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     63    1.1   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     64    1.1   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     65    1.1   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     66    1.1   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     67    1.1   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     68    1.1   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     69    1.1   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     70    1.1   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     71    1.1   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     72    1.1   thorpej  */
     73    1.1   thorpej 
     74    1.1   thorpej /*
     75    1.1   thorpej  * Autoconfiguration support for the Intel ACPI Component Architecture
     76    1.1   thorpej  * ACPI reference implementation.
     77    1.1   thorpej  */
     78    1.5     lukem 
     79    1.5     lukem #include <sys/cdefs.h>
     80  1.107  jmcneill __KERNEL_RCSID(0, "$NetBSD: acpi.c,v 1.107 2007/12/15 02:19:55 jmcneill Exp $");
     81   1.27  christos 
     82   1.27  christos #include "opt_acpi.h"
     83   1.73    sekiya #include "opt_pcifixup.h"
     84    1.1   thorpej 
     85    1.1   thorpej #include <sys/param.h>
     86    1.1   thorpej #include <sys/systm.h>
     87    1.1   thorpej #include <sys/device.h>
     88    1.1   thorpej #include <sys/malloc.h>
     89  1.100   xtraeme #include <sys/mutex.h>
     90   1.32  tshiozak #include <sys/kernel.h>
     91   1.32  tshiozak #include <sys/proc.h>
     92   1.78      cube #include <sys/sysctl.h>
     93    1.1   thorpej 
     94    1.1   thorpej #include <dev/acpi/acpica.h>
     95    1.1   thorpej #include <dev/acpi/acpireg.h>
     96    1.1   thorpej #include <dev/acpi/acpivar.h>
     97    1.1   thorpej #include <dev/acpi/acpi_osd.h>
     98   1.90  drochner #include <dev/acpi/acpi_timer.h>
     99   1.27  christos #ifdef ACPIVERBOSE
    100   1.27  christos #include <dev/acpi/acpidevs_data.h>
    101   1.27  christos #endif
    102    1.1   thorpej 
    103   1.74    sekiya #if defined(ACPI_PCI_FIXUP)
    104   1.93  christos #error The option ACPI_PCI_FIXUP has been obsoleted by PCI_INTR_FIXUP_DISABLED.  Please adjust your kernel configuration file.
    105   1.74    sekiya #endif
    106   1.74    sekiya 
    107   1.93  christos #ifdef PCI_INTR_FIXUP_DISABLED
    108   1.13  augustss #include <dev/pci/pcidevs.h>
    109   1.13  augustss #endif
    110   1.13  augustss 
    111   1.33  christos MALLOC_DECLARE(M_ACPI);
    112   1.33  christos 
    113   1.10  tshiozak #include <machine/acpi_machdep.h>
    114   1.39     kochi 
    115   1.40     kochi #ifdef ACPI_DEBUGGER
    116    1.1   thorpej #define	ACPI_DBGR_INIT		0x01
    117    1.1   thorpej #define	ACPI_DBGR_TABLES	0x02
    118    1.1   thorpej #define	ACPI_DBGR_ENABLE	0x04
    119    1.1   thorpej #define	ACPI_DBGR_PROBE		0x08
    120    1.1   thorpej #define	ACPI_DBGR_RUNNING	0x10
    121    1.1   thorpej 
    122   1.82     kochi static int acpi_dbgr = 0x00;
    123    1.1   thorpej #endif
    124    1.1   thorpej 
    125  1.104  jmcneill static ACPI_TABLE_DESC	acpi_initial_tables[128];
    126  1.104  jmcneill 
    127   1.64     kochi static int	acpi_match(struct device *, struct cfdata *, void *);
    128   1.64     kochi static void	acpi_attach(struct device *, struct device *, void *);
    129    1.1   thorpej 
    130   1.64     kochi static int	acpi_print(void *aux, const char *);
    131    1.1   thorpej 
    132   1.86  jmcneill static int	sysctl_hw_acpi_sleepstate(SYSCTLFN_ARGS);
    133   1.86  jmcneill 
    134    1.1   thorpej extern struct cfdriver acpi_cd;
    135    1.1   thorpej 
    136   1.17   thorpej CFATTACH_DECL(acpi, sizeof(struct acpi_softc),
    137   1.18   thorpej     acpi_match, acpi_attach, NULL, NULL);
    138    1.1   thorpej 
    139    1.1   thorpej /*
    140    1.1   thorpej  * This is a flag we set when the ACPI subsystem is active.  Machine
    141    1.1   thorpej  * dependent code may wish to skip other steps (such as attaching
    142    1.1   thorpej  * subsystems that ACPI supercedes) when ACPI is active.
    143    1.1   thorpej  */
    144    1.1   thorpej int	acpi_active;
    145   1.93  christos int	acpi_force_load;
    146    1.1   thorpej 
    147    1.1   thorpej /*
    148    1.1   thorpej  * Pointer to the ACPI subsystem's state.  There can be only
    149    1.1   thorpej  * one ACPI instance.
    150    1.1   thorpej  */
    151    1.1   thorpej struct acpi_softc *acpi_softc;
    152    1.1   thorpej 
    153   1.32  tshiozak /*
    154   1.32  tshiozak  * Locking stuff.
    155   1.32  tshiozak  */
    156  1.100   xtraeme static kmutex_t acpi_slock;
    157   1.32  tshiozak static int acpi_locked;
    158  1.101        ad extern kmutex_t acpi_interrupt_list_mtx;
    159   1.32  tshiozak 
    160   1.79      cube /*
    161   1.79      cube  * sysctl-related information
    162   1.79      cube  */
    163   1.79      cube 
    164   1.79      cube static int acpi_node = CTL_EOL;
    165   1.79      cube static uint64_t acpi_root_pointer;	/* found as hw.acpi.root */
    166   1.86  jmcneill static int acpi_sleepstate = ACPI_STATE_S0;
    167   1.78      cube 
    168   1.32  tshiozak /*
    169   1.32  tshiozak  * Prototypes.
    170   1.32  tshiozak  */
    171   1.64     kochi static void		acpi_shutdown(void *);
    172   1.64     kochi static void		acpi_build_tree(struct acpi_softc *);
    173   1.64     kochi static ACPI_STATUS	acpi_make_devnode(ACPI_HANDLE, UINT32, void *, void **);
    174    1.1   thorpej 
    175   1.64     kochi static void		acpi_enable_fixed_events(struct acpi_softc *);
    176    1.1   thorpej 
    177    1.1   thorpej /*
    178    1.1   thorpej  * acpi_probe:
    179    1.1   thorpej  *
    180    1.1   thorpej  *	Probe for ACPI support.  This is called by the
    181    1.1   thorpej  *	machine-dependent ACPI front-end.  All of the
    182    1.1   thorpej  *	actual work is done by ACPICA.
    183    1.1   thorpej  *
    184    1.1   thorpej  *	NOTE: This is not an autoconfiguration interface function.
    185    1.1   thorpej  */
    186    1.1   thorpej int
    187    1.1   thorpej acpi_probe(void)
    188    1.1   thorpej {
    189    1.1   thorpej 	static int beenhere;
    190  1.107  jmcneill 	ACPI_TABLE_HEADER xsdt;
    191    1.1   thorpej 	ACPI_STATUS rv;
    192    1.1   thorpej 
    193    1.1   thorpej 	if (beenhere != 0)
    194    1.1   thorpej 		panic("acpi_probe: ACPI has already been probed");
    195    1.1   thorpej 	beenhere = 1;
    196    1.1   thorpej 
    197  1.103        ad 	mutex_init(&acpi_slock, MUTEX_DEFAULT, IPL_NONE);
    198  1.103        ad 	mutex_init(&acpi_interrupt_list_mtx, MUTEX_DEFAULT, IPL_NONE);
    199   1.32  tshiozak 	acpi_locked = 0;
    200   1.32  tshiozak 
    201    1.1   thorpej 	/*
    202    1.1   thorpej 	 * Start up ACPICA.
    203    1.1   thorpej 	 */
    204   1.40     kochi #ifdef ACPI_DEBUGGER
    205    1.1   thorpej 	if (acpi_dbgr & ACPI_DBGR_INIT)
    206    1.1   thorpej 		acpi_osd_debugger();
    207    1.1   thorpej #endif
    208    1.1   thorpej 
    209  1.104  jmcneill 	AcpiGbl_AllMethodsSerialized = FALSE;
    210  1.104  jmcneill 	AcpiGbl_EnableInterpreterSlack = TRUE;
    211  1.104  jmcneill 
    212    1.1   thorpej 	rv = AcpiInitializeSubsystem();
    213   1.56   mycroft 	if (ACPI_FAILURE(rv)) {
    214   1.55   mycroft 		printf("ACPI: unable to initialize ACPICA: %s\n",
    215   1.55   mycroft 		    AcpiFormatException(rv));
    216   1.63     kochi 		return 0;
    217    1.1   thorpej 	}
    218    1.1   thorpej 
    219  1.104  jmcneill 	rv = AcpiInitializeTables(acpi_initial_tables, 128, 0);
    220  1.104  jmcneill 	if (ACPI_FAILURE(rv)) {
    221  1.104  jmcneill 		printf("ACPI: unable to initialize ACPI tables: %s\n",
    222  1.104  jmcneill 		    AcpiFormatException(rv));
    223  1.104  jmcneill 		return 0;
    224  1.104  jmcneill 	}
    225  1.104  jmcneill 
    226  1.104  jmcneill 	rv = AcpiReallocateRootTable();
    227  1.104  jmcneill 	if (ACPI_FAILURE(rv)) {
    228  1.104  jmcneill 		printf("ACPI: unable to reallocate root table: %s\n",
    229  1.104  jmcneill 		    AcpiFormatException(rv));
    230  1.104  jmcneill 		return 0;
    231  1.104  jmcneill 	}
    232  1.104  jmcneill 
    233   1.40     kochi #ifdef ACPI_DEBUGGER
    234    1.1   thorpej 	if (acpi_dbgr & ACPI_DBGR_TABLES)
    235    1.1   thorpej 		acpi_osd_debugger();
    236    1.1   thorpej #endif
    237    1.1   thorpej 
    238    1.1   thorpej 	rv = AcpiLoadTables();
    239   1.56   mycroft 	if (ACPI_FAILURE(rv)) {
    240   1.55   mycroft 		printf("ACPI: unable to load tables: %s\n",
    241   1.55   mycroft 		    AcpiFormatException(rv));
    242   1.63     kochi 		return 0;
    243    1.1   thorpej 	}
    244    1.1   thorpej 
    245  1.107  jmcneill 	if (ACPI_FAILURE(AcpiGetTableHeader(ACPI_SIG_XSDT, 0, &xsdt)))
    246  1.107  jmcneill 		memset(&xsdt, 0, sizeof(xsdt));
    247  1.107  jmcneill 
    248   1.93  christos 	if (!acpi_force_load && (acpi_find_quirks() & ACPI_QUIRK_BROKEN)) {
    249   1.93  christos 		printf("ACPI: BIOS implementation in listed as broken:\n");
    250   1.93  christos 		printf("ACPI: X/RSDT: OemId <%6.6s,%8.8s,%08x>, "
    251   1.93  christos 		       "AslId <%4.4s,%08x>\n",
    252  1.107  jmcneill 			xsdt.OemId, xsdt.OemTableId,
    253  1.107  jmcneill 		        xsdt.OemRevision,
    254  1.107  jmcneill 			xsdt.AslCompilerId,
    255  1.107  jmcneill 		        xsdt.AslCompilerRevision);
    256   1.93  christos 		printf("ACPI: not used. set acpi_force_load to use anyway.\n");
    257   1.93  christos 		return 0;
    258   1.93  christos 	}
    259   1.93  christos 
    260  1.107  jmcneill #if notyet
    261  1.104  jmcneill 	/* Install the default address space handlers. */
    262  1.104  jmcneill 	rv = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
    263  1.104  jmcneill 	    ACPI_ADR_SPACE_SYSTEM_MEMORY, ACPI_DEFAULT_HANDLER, NULL, NULL);
    264  1.104  jmcneill 	if (ACPI_FAILURE(rv)) {
    265  1.104  jmcneill 		printf("ACPI: unable to initialise SystemMemory handler: %s\n",
    266  1.104  jmcneill 		    AcpiFormatException(rv));
    267  1.104  jmcneill 		return 0;
    268  1.104  jmcneill 	}
    269  1.104  jmcneill 	rv = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
    270  1.104  jmcneill 	    ACPI_ADR_SPACE_SYSTEM_IO, ACPI_DEFAULT_HANDLER, NULL, NULL);
    271  1.104  jmcneill 	if (ACPI_FAILURE(rv)) {
    272  1.104  jmcneill 		printf("ACPI: unable to initialise SystemIO handler: %s\n",
    273  1.104  jmcneill 		     AcpiFormatException(rv));
    274  1.104  jmcneill 		return 0;
    275  1.104  jmcneill 	}
    276  1.104  jmcneill 	rv = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
    277  1.104  jmcneill 	    ACPI_ADR_SPACE_PCI_CONFIG, ACPI_DEFAULT_HANDLER, NULL, NULL);
    278  1.104  jmcneill 	if (ACPI_FAILURE(rv)) {
    279  1.104  jmcneill 		printf("ACPI: unabled to initialise PciConfig handler: %s\n",
    280  1.104  jmcneill 		    AcpiFormatException(rv));
    281  1.104  jmcneill 		return 0;
    282  1.104  jmcneill 	}
    283  1.104  jmcneill #endif
    284  1.104  jmcneill 
    285  1.104  jmcneill 	rv = AcpiEnableSubsystem(~(ACPI_NO_HARDWARE_INIT|ACPI_NO_ACPI_ENABLE));
    286  1.104  jmcneill 	if (ACPI_FAILURE(rv)) {
    287  1.104  jmcneill 		printf("ACPI: unable to enable: %s\n", AcpiFormatException(rv));
    288  1.104  jmcneill 		return 0;
    289  1.104  jmcneill 	}
    290  1.104  jmcneill 
    291    1.1   thorpej 	/*
    292    1.1   thorpej 	 * Looks like we have ACPI!
    293    1.1   thorpej 	 */
    294    1.1   thorpej 
    295   1.63     kochi 	return 1;
    296    1.1   thorpej }
    297    1.1   thorpej 
    298   1.98      cube static int
    299   1.98      cube acpi_submatch(device_t parent, cfdata_t cf, const int *locs, void *aux)
    300   1.98      cube {
    301   1.98      cube 	struct cfattach *ca;
    302   1.98      cube 
    303   1.98      cube 	ca = config_cfattach_lookup(cf->cf_name, cf->cf_atname);
    304   1.98      cube 	return (ca == &acpi_ca);
    305   1.98      cube }
    306   1.98      cube 
    307   1.98      cube int
    308   1.98      cube acpi_check(device_t parent, const char *ifattr)
    309   1.98      cube {
    310   1.98      cube 	return (config_search_ia(acpi_submatch, parent, ifattr, NULL) != NULL);
    311   1.98      cube }
    312   1.98      cube 
    313  1.104  jmcneill ACPI_PHYSICAL_ADDRESS
    314  1.104  jmcneill acpi_OsGetRootPointer(void)
    315   1.78      cube {
    316  1.104  jmcneill 	ACPI_PHYSICAL_ADDRESS PhysicalAddress;
    317   1.78      cube 
    318   1.78      cube 	/*
    319   1.78      cube 	 * IA-32: Use AcpiFindRootPointer() to locate the RSDP.
    320   1.78      cube 	 *
    321   1.78      cube 	 * IA-64: Use the EFI.
    322   1.78      cube 	 *
    323   1.78      cube 	 * We let MD code handle this since there are multiple
    324   1.78      cube 	 * ways to do it.
    325   1.78      cube 	 */
    326   1.78      cube 
    327  1.104  jmcneill 	PhysicalAddress = acpi_md_OsGetRootPointer();
    328   1.78      cube 
    329  1.104  jmcneill 	if (acpi_root_pointer == 0)
    330  1.104  jmcneill 		acpi_root_pointer = PhysicalAddress;
    331   1.78      cube 
    332  1.104  jmcneill 	return PhysicalAddress;
    333   1.78      cube }
    334   1.78      cube 
    335    1.1   thorpej /*
    336    1.1   thorpej  * acpi_match:
    337    1.1   thorpej  *
    338    1.1   thorpej  *	Autoconfiguration `match' routine.
    339    1.1   thorpej  */
    340   1.64     kochi static int
    341   1.97  christos acpi_match(struct device *parent, struct cfdata *match,
    342   1.97  christos     void *aux)
    343    1.1   thorpej {
    344    1.1   thorpej 	/*
    345    1.1   thorpej 	 * XXX Check other locators?  Hard to know -- machine
    346    1.1   thorpej 	 * dependent code has already checked for the presence
    347    1.1   thorpej 	 * of ACPI by calling acpi_probe(), so I suppose we
    348    1.1   thorpej 	 * don't really have to do anything else.
    349    1.1   thorpej 	 */
    350   1.63     kochi 	return 1;
    351    1.1   thorpej }
    352    1.1   thorpej 
    353    1.1   thorpej /*
    354    1.1   thorpej  * acpi_attach:
    355    1.1   thorpej  *
    356    1.1   thorpej  *	Autoconfiguration `attach' routine.  Finish initializing
    357    1.1   thorpej  *	ACPICA (some initialization was done in acpi_probe(),
    358    1.1   thorpej  *	which was required to check for the presence of ACPI),
    359    1.1   thorpej  *	and enable the ACPI subsystem.
    360    1.1   thorpej  */
    361   1.64     kochi static void
    362   1.97  christos acpi_attach(struct device *parent, struct device *self, void *aux)
    363    1.1   thorpej {
    364    1.1   thorpej 	struct acpi_softc *sc = (void *) self;
    365    1.1   thorpej 	struct acpibus_attach_args *aa = aux;
    366    1.1   thorpej 	ACPI_STATUS rv;
    367  1.107  jmcneill 	ACPI_TABLE_HEADER xsdt;
    368    1.1   thorpej 
    369   1.84       wiz 	aprint_naive(": Advanced Configuration and Power Interface\n");
    370   1.84       wiz 	aprint_normal(": Advanced Configuration and Power Interface\n");
    371    1.1   thorpej 
    372    1.1   thorpej 	if (acpi_softc != NULL)
    373    1.1   thorpej 		panic("acpi_attach: ACPI has already been attached");
    374   1.29      fvdl 
    375   1.35   thorpej 	sysmon_power_settype("acpi");
    376   1.37     kochi 
    377   1.83     kochi 	aprint_verbose("%s: using Intel ACPI CA subsystem version %08x\n",
    378   1.37     kochi 	    sc->sc_dev.dv_xname, ACPI_CA_VERSION);
    379   1.35   thorpej 
    380  1.107  jmcneill 	if (ACPI_FAILURE(AcpiGetTableHeader(ACPI_SIG_XSDT, 0, &xsdt)))
    381  1.107  jmcneill 		memset(&xsdt, 0, sizeof(xsdt));
    382  1.107  jmcneill 
    383   1.83     kochi 	aprint_verbose("%s: X/RSDT: OemId <%6.6s,%8.8s,%08x>, AslId <%4.4s,%08x>\n",
    384   1.29      fvdl 	    sc->sc_dev.dv_xname,
    385  1.107  jmcneill 	    xsdt.OemId, xsdt.OemTableId,
    386  1.107  jmcneill 	    xsdt.OemRevision,
    387  1.107  jmcneill 	    xsdt.AslCompilerId, xsdt.AslCompilerRevision);
    388    1.1   thorpej 
    389   1.36      fvdl 	sc->sc_quirks = acpi_find_quirks();
    390   1.36      fvdl 
    391    1.1   thorpej 	sc->sc_iot = aa->aa_iot;
    392    1.1   thorpej 	sc->sc_memt = aa->aa_memt;
    393    1.1   thorpej 	sc->sc_pc = aa->aa_pc;
    394    1.1   thorpej 	sc->sc_pciflags = aa->aa_pciflags;
    395   1.19  jmcneill 	sc->sc_ic = aa->aa_ic;
    396    1.1   thorpej 
    397    1.1   thorpej 	acpi_softc = sc;
    398    1.1   thorpej 
    399    1.1   thorpej 	/*
    400  1.104  jmcneill 	 * Register null power management handler
    401  1.104  jmcneill 	 */
    402  1.104  jmcneill 	if (!pmf_device_register(self, NULL, NULL))
    403  1.104  jmcneill 		aprint_error_dev(self, "couldn't establish power handler\n");
    404  1.104  jmcneill 
    405  1.104  jmcneill 	/*
    406    1.1   thorpej 	 * Bring ACPI on-line.
    407    1.1   thorpej 	 */
    408   1.40     kochi #ifdef ACPI_DEBUGGER
    409    1.1   thorpej 	if (acpi_dbgr & ACPI_DBGR_ENABLE)
    410    1.1   thorpej 		acpi_osd_debugger();
    411    1.1   thorpej #endif
    412   1.47   mycroft 
    413  1.104  jmcneill #define ACPI_ENABLE_PHASE1 \
    414  1.104  jmcneill     (ACPI_NO_HANDLER_INIT | ACPI_NO_EVENT_INIT)
    415  1.104  jmcneill #define ACPI_ENABLE_PHASE2 \
    416  1.104  jmcneill     (ACPI_NO_HARDWARE_INIT | ACPI_NO_ACPI_ENABLE | \
    417  1.104  jmcneill      ACPI_NO_ADDRESS_SPACE_INIT)
    418  1.104  jmcneill 
    419  1.104  jmcneill 	rv = AcpiEnableSubsystem(ACPI_ENABLE_PHASE1);
    420  1.104  jmcneill 	if (ACPI_FAILURE(rv)) {
    421  1.104  jmcneill 		aprint_error("%s: unable to enable ACPI: %s\n",
    422  1.104  jmcneill 		    sc->sc_dev.dv_xname, AcpiFormatException(rv));
    423  1.104  jmcneill 		return;
    424  1.104  jmcneill 	}
    425  1.104  jmcneill 
    426  1.104  jmcneill 	acpi_md_callback();
    427  1.104  jmcneill 
    428  1.104  jmcneill 	rv = AcpiEnableSubsystem(ACPI_ENABLE_PHASE2);
    429   1.56   mycroft 	if (ACPI_FAILURE(rv)) {
    430   1.83     kochi 		aprint_error("%s: unable to enable ACPI: %s\n",
    431   1.55   mycroft 		    sc->sc_dev.dv_xname, AcpiFormatException(rv));
    432   1.47   mycroft 		return;
    433   1.47   mycroft 	}
    434   1.60     kochi 
    435   1.60     kochi 	/* early EC handler initialization if ECDT table is available */
    436  1.104  jmcneill 	config_found_ia(&sc->sc_dev, "acpiecdtbus", NULL, NULL);
    437   1.60     kochi 
    438  1.104  jmcneill 	rv = AcpiInitializeObjects(ACPI_FULL_INITIALIZATION);
    439   1.56   mycroft 	if (ACPI_FAILURE(rv)) {
    440   1.83     kochi 		aprint_error("%s: unable to initialize ACPI objects: %s\n",
    441   1.55   mycroft 		    sc->sc_dev.dv_xname, AcpiFormatException(rv));
    442    1.1   thorpej 		return;
    443    1.1   thorpej 	}
    444    1.1   thorpej 	acpi_active = 1;
    445    1.1   thorpej 
    446    1.1   thorpej 	/* Our current state is "awake". */
    447    1.1   thorpej 	sc->sc_sleepstate = ACPI_STATE_S0;
    448    1.1   thorpej 
    449    1.9   kanaoka 	/* Show SCI interrupt. */
    450  1.104  jmcneill 	aprint_verbose("%s: SCI interrupting at int %d\n",
    451  1.104  jmcneill 	    sc->sc_dev.dv_xname, AcpiGbl_FADT.SciInterrupt);
    452  1.104  jmcneill 
    453    1.1   thorpej 	/*
    454    1.1   thorpej 	 * Check for fixed-hardware features.
    455    1.1   thorpej 	 */
    456    1.1   thorpej 	acpi_enable_fixed_events(sc);
    457   1.90  drochner 	acpitimer_init();
    458    1.1   thorpej 
    459    1.1   thorpej 	/*
    460    1.1   thorpej 	 * Scan the namespace and build our device tree.
    461    1.1   thorpej 	 */
    462   1.40     kochi #ifdef ACPI_DEBUGGER
    463    1.1   thorpej 	if (acpi_dbgr & ACPI_DBGR_PROBE)
    464    1.1   thorpej 		acpi_osd_debugger();
    465    1.1   thorpej #endif
    466    1.1   thorpej 	acpi_build_tree(sc);
    467    1.1   thorpej 
    468   1.86  jmcneill 	if (acpi_root_pointer != 0 && acpi_node != CTL_EOL) {
    469   1.78      cube 		(void)sysctl_createv(NULL, 0, NULL, NULL,
    470   1.78      cube 		    CTLFLAG_IMMEDIATE,
    471   1.79      cube 		    CTLTYPE_QUAD, "root", NULL, NULL,
    472   1.78      cube 		    acpi_root_pointer, NULL, 0,
    473   1.79      cube 		    CTL_HW, acpi_node, CTL_CREATE, CTL_EOL);
    474   1.86  jmcneill 	}
    475   1.86  jmcneill 
    476   1.78      cube 
    477    1.1   thorpej 	/*
    478    1.1   thorpej 	 * Register a shutdown hook that disables certain ACPI
    479    1.1   thorpej 	 * events that might happen and confuse us while we're
    480    1.1   thorpej 	 * trying to shut down.
    481    1.1   thorpej 	 */
    482    1.1   thorpej 	sc->sc_sdhook = shutdownhook_establish(acpi_shutdown, sc);
    483    1.1   thorpej 	if (sc->sc_sdhook == NULL)
    484   1.83     kochi 		aprint_error("%s: WARNING: unable to register shutdown hook\n",
    485    1.1   thorpej 		    sc->sc_dev.dv_xname);
    486    1.1   thorpej 
    487   1.40     kochi #ifdef ACPI_DEBUGGER
    488    1.1   thorpej 	if (acpi_dbgr & ACPI_DBGR_RUNNING)
    489    1.1   thorpej 		acpi_osd_debugger();
    490    1.1   thorpej #endif
    491    1.1   thorpej }
    492    1.1   thorpej 
    493    1.1   thorpej /*
    494    1.1   thorpej  * acpi_shutdown:
    495    1.1   thorpej  *
    496    1.1   thorpej  *	Shutdown hook for ACPI -- disable some events that
    497    1.1   thorpej  *	might confuse us.
    498    1.1   thorpej  */
    499   1.64     kochi static void
    500   1.97  christos acpi_shutdown(void *arg)
    501    1.1   thorpej {
    502   1.81     kochi 	/* nothing */
    503    1.1   thorpej }
    504    1.1   thorpej 
    505   1.81     kochi #if 0
    506    1.1   thorpej /*
    507    1.1   thorpej  * acpi_disable:
    508    1.1   thorpej  *
    509    1.1   thorpej  *	Disable ACPI.
    510    1.1   thorpej  */
    511   1.64     kochi static ACPI_STATUS
    512    1.1   thorpej acpi_disable(struct acpi_softc *sc)
    513    1.1   thorpej {
    514    1.1   thorpej 	ACPI_STATUS rv = AE_OK;
    515    1.1   thorpej 
    516    1.1   thorpej 	if (acpi_active) {
    517    1.1   thorpej 		rv = AcpiDisable();
    518   1.56   mycroft 		if (ACPI_SUCCESS(rv))
    519    1.1   thorpej 			acpi_active = 0;
    520    1.1   thorpej 	}
    521   1.63     kochi 	return rv;
    522    1.1   thorpej }
    523   1.81     kochi #endif
    524    1.1   thorpej 
    525    1.1   thorpej struct acpi_make_devnode_state {
    526    1.1   thorpej 	struct acpi_softc *softc;
    527    1.1   thorpej 	struct acpi_scope *scope;
    528    1.1   thorpej };
    529    1.1   thorpej 
    530    1.1   thorpej /*
    531    1.1   thorpej  * acpi_build_tree:
    532    1.1   thorpej  *
    533    1.1   thorpej  *	Scan relevant portions of the ACPI namespace and attach
    534    1.1   thorpej  *	child devices.
    535    1.1   thorpej  */
    536   1.64     kochi static void
    537    1.1   thorpej acpi_build_tree(struct acpi_softc *sc)
    538    1.1   thorpej {
    539    1.1   thorpej 	static const char *scopes[] = {
    540    1.1   thorpej 		"\\_PR_",	/* ACPI 1.0 processor namespace */
    541    1.1   thorpej 		"\\_SB_",	/* system bus namespace */
    542   1.91    tsarna 		"\\_SI_",	/* system indicator namespace */
    543    1.1   thorpej 		"\\_TZ_",	/* ACPI 1.0 thermal zone namespace */
    544    1.1   thorpej 		NULL,
    545    1.1   thorpej 	};
    546    1.1   thorpej 	struct acpi_attach_args aa;
    547    1.1   thorpej 	struct acpi_make_devnode_state state;
    548    1.1   thorpej 	struct acpi_scope *as;
    549    1.1   thorpej 	struct acpi_devnode *ad;
    550    1.1   thorpej 	ACPI_HANDLE parent;
    551   1.56   mycroft 	ACPI_STATUS rv;
    552    1.1   thorpej 	int i;
    553    1.1   thorpej 
    554    1.1   thorpej 	TAILQ_INIT(&sc->sc_scopes);
    555    1.1   thorpej 
    556    1.1   thorpej 	state.softc = sc;
    557    1.1   thorpej 
    558    1.1   thorpej 	/*
    559    1.1   thorpej 	 * Scan the namespace and build our tree.
    560    1.1   thorpej 	 */
    561    1.1   thorpej 	for (i = 0; scopes[i] != NULL; i++) {
    562   1.33  christos 		as = malloc(sizeof(*as), M_ACPI, M_WAITOK);
    563    1.1   thorpej 		as->as_name = scopes[i];
    564    1.1   thorpej 		TAILQ_INIT(&as->as_devnodes);
    565    1.1   thorpej 
    566    1.1   thorpej 		TAILQ_INSERT_TAIL(&sc->sc_scopes, as, as_list);
    567    1.1   thorpej 
    568    1.1   thorpej 		state.scope = as;
    569    1.1   thorpej 
    570   1.70  christos 		rv = AcpiGetHandle(ACPI_ROOT_OBJECT, scopes[i],
    571   1.56   mycroft 		    &parent);
    572   1.56   mycroft 		if (ACPI_SUCCESS(rv)) {
    573    1.1   thorpej 			AcpiWalkNamespace(ACPI_TYPE_ANY, parent, 100,
    574    1.1   thorpej 			    acpi_make_devnode, &state, NULL);
    575    1.1   thorpej 		}
    576    1.1   thorpej 
    577    1.1   thorpej 		/* Now, for this namespace, try and attach the devices. */
    578    1.1   thorpej 		TAILQ_FOREACH(ad, &as->as_devnodes, ad_list) {
    579    1.1   thorpej 			aa.aa_node = ad;
    580    1.1   thorpej 			aa.aa_iot = sc->sc_iot;
    581    1.1   thorpej 			aa.aa_memt = sc->sc_memt;
    582    1.1   thorpej 			aa.aa_pc = sc->sc_pc;
    583    1.1   thorpej 			aa.aa_pciflags = sc->sc_pciflags;
    584   1.19  jmcneill 			aa.aa_ic = sc->sc_ic;
    585    1.1   thorpej 
    586   1.54     kochi 			if (ad->ad_devinfo->Type == ACPI_TYPE_DEVICE) {
    587   1.25  jmcneill 				/*
    588   1.25  jmcneill 				 * XXX We only attach devices which are:
    589   1.25  jmcneill 				 *
    590   1.25  jmcneill 				 *	- present
    591   1.25  jmcneill 				 *	- enabled
    592   1.25  jmcneill 				 *	- functioning properly
    593   1.25  jmcneill 				 *
    594   1.25  jmcneill 				 * However, if enabled, it's decoding resources,
    595   1.25  jmcneill 				 * so we should claim them, if possible.
    596   1.25  jmcneill 				 * Requires changes to bus_space(9).
    597   1.25  jmcneill 				 */
    598   1.54     kochi 				if ((ad->ad_devinfo->Valid & ACPI_VALID_STA) ==
    599   1.45   mycroft 				    ACPI_VALID_STA &&
    600   1.54     kochi 				    (ad->ad_devinfo->CurrentStatus &
    601   1.25  jmcneill 				     (ACPI_STA_DEV_PRESENT|ACPI_STA_DEV_ENABLED|
    602   1.25  jmcneill 				      ACPI_STA_DEV_OK)) !=
    603   1.25  jmcneill 				    (ACPI_STA_DEV_PRESENT|ACPI_STA_DEV_ENABLED|
    604   1.25  jmcneill 				     ACPI_STA_DEV_OK))
    605   1.25  jmcneill 					continue;
    606  1.104  jmcneill 			}
    607   1.25  jmcneill 
    608  1.104  jmcneill 			/*
    609  1.104  jmcneill 			 * XXX Same problem as above...
    610  1.104  jmcneill 			 *
    611  1.104  jmcneill 			 * Do this check only for devices, as e.g.
    612  1.104  jmcneill 			 * a Thermal Zone doesn't have a HID.
    613  1.104  jmcneill 			 */
    614  1.104  jmcneill 			if (ad->ad_devinfo->Type == ACPI_TYPE_DEVICE &&
    615  1.104  jmcneill 			    (ad->ad_devinfo->Valid & ACPI_VALID_HID) == 0)
    616  1.104  jmcneill 				continue;
    617    1.1   thorpej 
    618   1.94  christos 			ad->ad_device = config_found_ia(&sc->sc_dev,
    619   1.94  christos 			    "acpinodebus", &aa, acpi_print);
    620    1.1   thorpej 		}
    621    1.1   thorpej 	}
    622   1.94  christos 	config_found_ia(&sc->sc_dev, "acpiapmbus", NULL, NULL);
    623    1.1   thorpej }
    624    1.1   thorpej 
    625   1.40     kochi #ifdef ACPI_ACTIVATE_DEV
    626   1.15  augustss static void
    627   1.54     kochi acpi_activate_device(ACPI_HANDLE handle, ACPI_DEVICE_INFO **di)
    628   1.15  augustss {
    629   1.15  augustss 	ACPI_STATUS rv;
    630   1.54     kochi 	ACPI_BUFFER buf;
    631   1.54     kochi 
    632   1.54     kochi 	buf.Pointer = NULL;
    633   1.54     kochi 	buf.Length = ACPI_ALLOCATE_BUFFER;
    634   1.15  augustss 
    635   1.15  augustss #ifdef ACPI_DEBUG
    636   1.83     kochi 	aprint_normal("acpi_activate_device: %s, old status=%x\n",
    637   1.54     kochi 	       (*di)->HardwareId.Value, (*di)->CurrentStatus);
    638   1.15  augustss #endif
    639   1.15  augustss 
    640   1.15  augustss 	rv = acpi_allocate_resources(handle);
    641   1.15  augustss 	if (ACPI_FAILURE(rv)) {
    642   1.83     kochi 		aprint_error("acpi: activate failed for %s\n",
    643   1.54     kochi 		       (*di)->HardwareId.Value);
    644   1.23  augustss 	} else {
    645   1.99        ad 		aprint_verbose("acpi: activated %s\n",
    646   1.99        ad 		    (*di)->HardwareId.Value);
    647   1.15  augustss 	}
    648   1.15  augustss 
    649   1.54     kochi 	(void)AcpiGetObjectInfo(handle, &buf);
    650   1.54     kochi 	AcpiOsFree(*di);
    651   1.54     kochi 	*di = buf.Pointer;
    652   1.54     kochi 
    653   1.44   mycroft #ifdef ACPI_DEBUG
    654   1.83     kochi 	aprint_normal("acpi_activate_device: %s, new status=%x\n",
    655   1.54     kochi 	       (*di)->HardwareId.Value, (*di)->CurrentStatus);
    656   1.15  augustss #endif
    657   1.15  augustss }
    658   1.15  augustss #endif /* ACPI_ACTIVATE_DEV */
    659   1.15  augustss 
    660    1.1   thorpej /*
    661    1.1   thorpej  * acpi_make_devnode:
    662    1.1   thorpej  *
    663    1.1   thorpej  *	Make an ACPI devnode.
    664    1.1   thorpej  */
    665   1.64     kochi static ACPI_STATUS
    666    1.1   thorpej acpi_make_devnode(ACPI_HANDLE handle, UINT32 level, void *context,
    667   1.97  christos     void **status)
    668    1.1   thorpej {
    669    1.1   thorpej 	struct acpi_make_devnode_state *state = context;
    670   1.20  jmcneill #if defined(ACPI_DEBUG) || defined(ACPI_EXTRA_DEBUG)
    671    1.1   thorpej 	struct acpi_softc *sc = state->softc;
    672    1.4   thorpej #endif
    673    1.1   thorpej 	struct acpi_scope *as = state->scope;
    674    1.1   thorpej 	struct acpi_devnode *ad;
    675   1.48   mycroft 	ACPI_OBJECT_TYPE type;
    676   1.48   mycroft 	ACPI_BUFFER buf;
    677   1.54     kochi 	ACPI_DEVICE_INFO *devinfo;
    678    1.1   thorpej 	ACPI_STATUS rv;
    679  1.102      cube 	ACPI_NAME_UNION *anu;
    680  1.102      cube 	int i, clear = 0;
    681    1.1   thorpej 
    682   1.56   mycroft 	rv = AcpiGetType(handle, &type);
    683   1.56   mycroft 	if (ACPI_SUCCESS(rv)) {
    684   1.54     kochi 		buf.Pointer = NULL;
    685   1.54     kochi 		buf.Length = ACPI_ALLOCATE_BUFFER;
    686   1.48   mycroft 		rv = AcpiGetObjectInfo(handle, &buf);
    687   1.56   mycroft 		if (ACPI_FAILURE(rv)) {
    688   1.15  augustss #ifdef ACPI_DEBUG
    689   1.83     kochi 			aprint_normal("%s: AcpiGetObjectInfo failed: %s\n",
    690   1.55   mycroft 			    sc->sc_dev.dv_xname, AcpiFormatException(rv));
    691   1.15  augustss #endif
    692   1.15  augustss 			goto out; /* XXX why return OK */
    693   1.15  augustss 		}
    694   1.15  augustss 
    695   1.54     kochi 		devinfo = buf.Pointer;
    696   1.54     kochi 
    697    1.1   thorpej 		switch (type) {
    698    1.1   thorpej 		case ACPI_TYPE_DEVICE:
    699   1.40     kochi #ifdef ACPI_ACTIVATE_DEV
    700   1.54     kochi 			if ((devinfo->Valid & (ACPI_VALID_STA|ACPI_VALID_HID)) ==
    701   1.43   mycroft 			    (ACPI_VALID_STA|ACPI_VALID_HID) &&
    702   1.54     kochi 			    (devinfo->CurrentStatus &
    703   1.15  augustss 			     (ACPI_STA_DEV_PRESENT|ACPI_STA_DEV_ENABLED)) ==
    704   1.54     kochi 			    ACPI_STA_DEV_PRESENT)
    705   1.15  augustss 				acpi_activate_device(handle, &devinfo);
    706   1.43   mycroft 
    707   1.15  augustss 			/* FALLTHROUGH */
    708   1.15  augustss #endif
    709   1.15  augustss 
    710    1.1   thorpej 		case ACPI_TYPE_PROCESSOR:
    711    1.1   thorpej 		case ACPI_TYPE_THERMAL:
    712    1.1   thorpej 		case ACPI_TYPE_POWER:
    713   1.33  christos 			ad = malloc(sizeof(*ad), M_ACPI, M_NOWAIT|M_ZERO);
    714    1.1   thorpej 			if (ad == NULL)
    715   1.63     kochi 				return AE_NO_MEMORY;
    716    1.1   thorpej 
    717   1.43   mycroft 			ad->ad_devinfo = devinfo;
    718    1.1   thorpej 			ad->ad_handle = handle;
    719    1.1   thorpej 			ad->ad_level = level;
    720    1.1   thorpej 			ad->ad_scope = as;
    721    1.1   thorpej 			ad->ad_type = type;
    722    1.1   thorpej 
    723  1.102      cube 			anu = (ACPI_NAME_UNION *)&devinfo->Name;
    724  1.102      cube 			ad->ad_name[4] = '\0';
    725  1.102      cube 			for (i = 3, clear = 0; i >= 0; i--) {
    726  1.102      cube 				if (!clear && anu->Ascii[i] == '_')
    727  1.102      cube 					ad->ad_name[i] = '\0';
    728  1.102      cube 				else {
    729  1.102      cube 					ad->ad_name[i] = anu->Ascii[i];
    730  1.102      cube 					clear = 1;
    731  1.102      cube 				}
    732  1.102      cube 			}
    733  1.102      cube 			if (ad->ad_name[0] == '\0')
    734  1.102      cube 				ad->ad_name[0] = '_';
    735  1.102      cube 
    736    1.1   thorpej 			TAILQ_INSERT_TAIL(&as->as_devnodes, ad, ad_list);
    737    1.1   thorpej 
    738  1.104  jmcneill 			if (type == ACPI_TYPE_DEVICE &&
    739  1.104  jmcneill 			    (ad->ad_devinfo->Valid & ACPI_VALID_HID) == 0)
    740    1.1   thorpej 				goto out;
    741    1.1   thorpej 
    742   1.14  augustss #ifdef ACPI_EXTRA_DEBUG
    743   1.83     kochi 			aprint_normal("%s: HID %s found in scope %s level %d\n",
    744   1.54     kochi 			    sc->sc_dev.dv_xname,
    745   1.54     kochi 			    ad->ad_devinfo->HardwareId.Value,
    746    1.1   thorpej 			    as->as_name, ad->ad_level);
    747   1.54     kochi 			if (ad->ad_devinfo->Valid & ACPI_VALID_UID)
    748   1.83     kochi 				aprint_normal("       UID %s\n",
    749   1.54     kochi 				    ad->ad_devinfo->UniqueId.Value);
    750   1.54     kochi 			if (ad->ad_devinfo->Valid & ACPI_VALID_ADR)
    751   1.83     kochi 				aprint_normal("       ADR 0x%016qx\n",
    752   1.54     kochi 				    ad->ad_devinfo->Address);
    753   1.54     kochi 			if (ad->ad_devinfo->Valid & ACPI_VALID_STA)
    754   1.83     kochi 				aprint_normal("       STA 0x%08x\n",
    755   1.54     kochi 				    ad->ad_devinfo->CurrentStatus);
    756    1.1   thorpej #endif
    757    1.1   thorpej 		}
    758    1.1   thorpej 	}
    759    1.1   thorpej  out:
    760   1.63     kochi 	return AE_OK;
    761    1.1   thorpej }
    762    1.1   thorpej 
    763    1.1   thorpej /*
    764    1.1   thorpej  * acpi_print:
    765    1.1   thorpej  *
    766   1.94  christos  *	Autoconfiguration print routine for ACPI node bus.
    767    1.1   thorpej  */
    768   1.64     kochi static int
    769    1.1   thorpej acpi_print(void *aux, const char *pnp)
    770    1.1   thorpej {
    771    1.1   thorpej 	struct acpi_attach_args *aa = aux;
    772   1.56   mycroft 	ACPI_STATUS rv;
    773    1.1   thorpej 
    774    1.4   thorpej 	if (pnp) {
    775   1.54     kochi 		if (aa->aa_node->ad_devinfo->Valid & ACPI_VALID_HID) {
    776   1.54     kochi 			char *pnpstr =
    777   1.54     kochi 			    aa->aa_node->ad_devinfo->HardwareId.Value;
    778   1.27  christos 			char *str;
    779   1.27  christos 
    780  1.102      cube 			aprint_normal("%s (%s) ", aa->aa_node->ad_name,
    781  1.102      cube 			    pnpstr);
    782   1.56   mycroft 			rv = acpi_eval_string(aa->aa_node->ad_handle,
    783   1.56   mycroft 			    "_STR", &str);
    784   1.56   mycroft 			if (ACPI_SUCCESS(rv)) {
    785   1.27  christos 				aprint_normal("[%s] ", str);
    786   1.27  christos 				AcpiOsFree(str);
    787   1.27  christos 			}
    788   1.27  christos #ifdef ACPIVERBOSE
    789   1.27  christos 			else {
    790   1.27  christos 				int i;
    791   1.27  christos 
    792   1.27  christos 				for (i = 0; i < sizeof(acpi_knowndevs) /
    793   1.27  christos 				    sizeof(acpi_knowndevs[0]); i++) {
    794   1.27  christos 					if (strcmp(acpi_knowndevs[i].pnp,
    795   1.27  christos 					    pnpstr) == 0) {
    796   1.83     kochi 						aprint_normal("[%s] ",
    797   1.27  christos 						    acpi_knowndevs[i].str);
    798   1.27  christos 					}
    799   1.27  christos 				}
    800   1.27  christos 			}
    801   1.62     kochi 
    802   1.27  christos #endif
    803  1.104  jmcneill 			aprint_normal("at %s", pnp);
    804  1.104  jmcneill 		} else if (aa->aa_node->ad_devinfo->Type != ACPI_TYPE_DEVICE) {
    805  1.102      cube 			aprint_normal("%s (ACPI Object Type '%s' "
    806  1.102      cube 			    "[0x%02x]) ", aa->aa_node->ad_name,
    807  1.102      cube 			     AcpiUtGetTypeName(aa->aa_node->ad_devinfo->Type),
    808  1.102      cube 			     aa->aa_node->ad_devinfo->Type);
    809  1.104  jmcneill 			aprint_normal("at %s", pnp);
    810  1.104  jmcneill 		} else
    811  1.104  jmcneill 			return 0;
    812   1.21      matt 	} else {
    813  1.102      cube 		aprint_normal(" (%s", aa->aa_node->ad_name);
    814   1.54     kochi 		if (aa->aa_node->ad_devinfo->Valid & ACPI_VALID_HID) {
    815  1.102      cube 			aprint_normal(", %s", aa->aa_node->ad_devinfo->HardwareId.Value);
    816   1.54     kochi 			if (aa->aa_node->ad_devinfo->Valid & ACPI_VALID_UID) {
    817   1.70  christos 				const char *uid;
    818   1.41     kochi 
    819   1.54     kochi 				uid = aa->aa_node->ad_devinfo->UniqueId.Value;
    820   1.48   mycroft 				if (uid[0] == '\0')
    821   1.41     kochi 					uid = "<null>";
    822   1.41     kochi 				aprint_normal("-%s", uid);
    823   1.41     kochi 			}
    824   1.22  jmcneill 		}
    825  1.102      cube 		aprint_normal(")");
    826    1.4   thorpej 	}
    827    1.1   thorpej 
    828   1.63     kochi 	return UNCONF;
    829    1.1   thorpej }
    830    1.1   thorpej 
    831    1.1   thorpej /*****************************************************************************
    832    1.1   thorpej  * ACPI fixed-hardware feature handlers
    833    1.1   thorpej  *****************************************************************************/
    834    1.1   thorpej 
    835   1.64     kochi static UINT32	acpi_fixed_button_handler(void *);
    836   1.64     kochi static void	acpi_fixed_button_pressed(void *);
    837    1.1   thorpej 
    838    1.1   thorpej /*
    839    1.1   thorpej  * acpi_enable_fixed_events:
    840    1.1   thorpej  *
    841    1.1   thorpej  *	Enable any fixed-hardware feature handlers.
    842    1.1   thorpej  */
    843   1.64     kochi static void
    844    1.1   thorpej acpi_enable_fixed_events(struct acpi_softc *sc)
    845    1.1   thorpej {
    846    1.1   thorpej 	static int beenhere;
    847    1.1   thorpej 	ACPI_STATUS rv;
    848    1.1   thorpej 
    849   1.34   thorpej 	KASSERT(beenhere == 0);
    850   1.34   thorpej 	beenhere = 1;
    851   1.34   thorpej 
    852    1.1   thorpej 	/*
    853    1.1   thorpej 	 * Check for fixed-hardware buttons.
    854    1.1   thorpej 	 */
    855    1.1   thorpej 
    856  1.104  jmcneill 	if ((AcpiGbl_FADT.Flags & ACPI_FADT_POWER_BUTTON) == 0) {
    857   1.99        ad 		aprint_verbose("%s: fixed-feature power button present\n",
    858   1.34   thorpej 		    sc->sc_dev.dv_xname);
    859   1.34   thorpej 		sc->sc_smpsw_power.smpsw_name = sc->sc_dev.dv_xname;
    860   1.35   thorpej 		sc->sc_smpsw_power.smpsw_type = PSWITCH_TYPE_POWER;
    861   1.34   thorpej 		if (sysmon_pswitch_register(&sc->sc_smpsw_power) != 0) {
    862   1.83     kochi 			aprint_error("%s: unable to register fixed power "
    863   1.83     kochi 			    "button with sysmon\n", sc->sc_dev.dv_xname);
    864   1.34   thorpej 		} else {
    865   1.34   thorpej 			rv = AcpiInstallFixedEventHandler(
    866   1.34   thorpej 			    ACPI_EVENT_POWER_BUTTON,
    867   1.34   thorpej 			    acpi_fixed_button_handler, &sc->sc_smpsw_power);
    868   1.56   mycroft 			if (ACPI_FAILURE(rv)) {
    869   1.83     kochi 				aprint_error("%s: unable to install handler "
    870   1.83     kochi 				    "for fixed power button: %s\n",
    871   1.56   mycroft 				    sc->sc_dev.dv_xname,
    872   1.56   mycroft 				    AcpiFormatException(rv));
    873   1.34   thorpej 			}
    874   1.34   thorpej 		}
    875    1.1   thorpej 	}
    876    1.1   thorpej 
    877  1.104  jmcneill 	if ((AcpiGbl_FADT.Flags & ACPI_FADT_SLEEP_BUTTON) == 0) {
    878   1.99        ad 		aprint_verbose("%s: fixed-feature sleep button present\n",
    879   1.34   thorpej 		    sc->sc_dev.dv_xname);
    880   1.34   thorpej 		sc->sc_smpsw_sleep.smpsw_name = sc->sc_dev.dv_xname;
    881   1.35   thorpej 		sc->sc_smpsw_sleep.smpsw_type = PSWITCH_TYPE_SLEEP;
    882   1.34   thorpej 		if (sysmon_pswitch_register(&sc->sc_smpsw_power) != 0) {
    883   1.83     kochi 			aprint_error("%s: unable to register fixed sleep "
    884   1.83     kochi 			    "button with sysmon\n", sc->sc_dev.dv_xname);
    885   1.34   thorpej 		} else {
    886   1.34   thorpej 			rv = AcpiInstallFixedEventHandler(
    887   1.34   thorpej 			    ACPI_EVENT_SLEEP_BUTTON,
    888   1.34   thorpej 			    acpi_fixed_button_handler, &sc->sc_smpsw_sleep);
    889   1.56   mycroft 			if (ACPI_FAILURE(rv)) {
    890   1.83     kochi 				aprint_error("%s: unable to install handler "
    891   1.83     kochi 				    "for fixed sleep button: %s\n",
    892   1.56   mycroft 				    sc->sc_dev.dv_xname,
    893   1.56   mycroft 				    AcpiFormatException(rv));
    894   1.34   thorpej 			}
    895   1.34   thorpej 		}
    896    1.1   thorpej 	}
    897    1.1   thorpej }
    898    1.1   thorpej 
    899    1.1   thorpej /*
    900   1.34   thorpej  * acpi_fixed_button_handler:
    901    1.1   thorpej  *
    902   1.34   thorpej  *	Event handler for the fixed buttons.
    903    1.1   thorpej  */
    904   1.64     kochi static UINT32
    905   1.34   thorpej acpi_fixed_button_handler(void *context)
    906    1.1   thorpej {
    907   1.34   thorpej 	struct sysmon_pswitch *smpsw = context;
    908   1.34   thorpej 	int rv;
    909    1.1   thorpej 
    910   1.34   thorpej #ifdef ACPI_BUT_DEBUG
    911   1.34   thorpej 	printf("%s: fixed button handler\n", smpsw->smpsw_name);
    912   1.34   thorpej #endif
    913    1.1   thorpej 
    914  1.104  jmcneill 	rv = AcpiOsExecute(OSL_NOTIFY_HANDLER,
    915   1.34   thorpej 	    acpi_fixed_button_pressed, smpsw);
    916   1.56   mycroft 	if (ACPI_FAILURE(rv))
    917   1.34   thorpej 		printf("%s: WARNING: unable to queue fixed button pressed "
    918   1.55   mycroft 		    "callback: %s\n", smpsw->smpsw_name,
    919   1.55   mycroft 		    AcpiFormatException(rv));
    920    1.1   thorpej 
    921   1.63     kochi 	return ACPI_INTERRUPT_HANDLED;
    922    1.1   thorpej }
    923    1.1   thorpej 
    924    1.1   thorpej /*
    925   1.34   thorpej  * acpi_fixed_button_pressed:
    926    1.1   thorpej  *
    927   1.34   thorpej  *	Deal with a fixed button being pressed.
    928    1.1   thorpej  */
    929   1.64     kochi static void
    930   1.34   thorpej acpi_fixed_button_pressed(void *context)
    931    1.1   thorpej {
    932   1.34   thorpej 	struct sysmon_pswitch *smpsw = context;
    933    1.1   thorpej 
    934   1.34   thorpej #ifdef ACPI_BUT_DEBUG
    935   1.34   thorpej 	printf("%s: fixed button pressed, calling sysmon\n",
    936   1.34   thorpej 	    smpsw->smpsw_name);
    937   1.34   thorpej #endif
    938    1.1   thorpej 
    939   1.35   thorpej 	sysmon_pswitch_event(smpsw, PSWITCH_EVENT_PRESSED);
    940    1.1   thorpej }
    941    1.1   thorpej 
    942    1.1   thorpej /*****************************************************************************
    943    1.1   thorpej  * ACPI utility routines.
    944    1.1   thorpej  *****************************************************************************/
    945    1.1   thorpej 
    946    1.2   thorpej /*
    947    1.2   thorpej  * acpi_eval_integer:
    948    1.2   thorpej  *
    949    1.2   thorpej  *	Evaluate an integer object.
    950    1.2   thorpej  */
    951    1.1   thorpej ACPI_STATUS
    952   1.70  christos acpi_eval_integer(ACPI_HANDLE handle, const char *path, ACPI_INTEGER *valp)
    953    1.1   thorpej {
    954    1.1   thorpej 	ACPI_STATUS rv;
    955    1.1   thorpej 	ACPI_BUFFER buf;
    956    1.1   thorpej 	ACPI_OBJECT param;
    957    1.1   thorpej 
    958    1.1   thorpej 	if (handle == NULL)
    959    1.1   thorpej 		handle = ACPI_ROOT_OBJECT;
    960    1.1   thorpej 
    961    1.1   thorpej 	buf.Pointer = &param;
    962    1.1   thorpej 	buf.Length = sizeof(param);
    963    1.1   thorpej 
    964   1.46   mycroft 	rv = AcpiEvaluateObjectTyped(handle, path, NULL, &buf, ACPI_TYPE_INTEGER);
    965   1.56   mycroft 	if (ACPI_SUCCESS(rv))
    966   1.46   mycroft 		*valp = param.Integer.Value;
    967    1.1   thorpej 
    968   1.63     kochi 	return rv;
    969    1.4   thorpej }
    970    1.4   thorpej 
    971    1.4   thorpej /*
    972    1.4   thorpej  * acpi_eval_string:
    973    1.4   thorpej  *
    974    1.7  sommerfe  *	Evaluate a (Unicode) string object.
    975    1.4   thorpej  */
    976    1.4   thorpej ACPI_STATUS
    977   1.70  christos acpi_eval_string(ACPI_HANDLE handle, const char *path, char **stringp)
    978    1.4   thorpej {
    979    1.4   thorpej 	ACPI_STATUS rv;
    980    1.4   thorpej 	ACPI_BUFFER buf;
    981    1.4   thorpej 
    982    1.4   thorpej 	if (handle == NULL)
    983    1.4   thorpej 		handle = ACPI_ROOT_OBJECT;
    984    1.4   thorpej 
    985    1.4   thorpej 	buf.Pointer = NULL;
    986   1.32  tshiozak 	buf.Length = ACPI_ALLOCATE_BUFFER;
    987    1.4   thorpej 
    988   1.46   mycroft 	rv = AcpiEvaluateObjectTyped(handle, path, NULL, &buf, ACPI_TYPE_STRING);
    989   1.56   mycroft 	if (ACPI_SUCCESS(rv)) {
    990   1.49   mycroft 		ACPI_OBJECT *param = buf.Pointer;
    991   1.71  drochner 		const char *ptr = param->String.Pointer;
    992   1.46   mycroft 		size_t len = param->String.Length;
    993   1.46   mycroft 		if ((*stringp = AcpiOsAllocate(len)) == NULL)
    994   1.46   mycroft 			rv = AE_NO_MEMORY;
    995   1.46   mycroft 		else
    996   1.46   mycroft 			(void)memcpy(*stringp, ptr, len);
    997   1.46   mycroft 		AcpiOsFree(param);
    998    1.4   thorpej 	}
    999   1.46   mycroft 
   1000   1.63     kochi 	return rv;
   1001    1.7  sommerfe }
   1002    1.7  sommerfe 
   1003    1.7  sommerfe 
   1004    1.7  sommerfe /*
   1005    1.7  sommerfe  * acpi_eval_struct:
   1006    1.7  sommerfe  *
   1007   1.38     kochi  *	Evaluate a more complex structure.
   1008   1.38     kochi  *	Caller must free buf.Pointer by AcpiOsFree().
   1009    1.7  sommerfe  */
   1010    1.7  sommerfe ACPI_STATUS
   1011   1.70  christos acpi_eval_struct(ACPI_HANDLE handle, const char *path, ACPI_BUFFER *bufp)
   1012    1.7  sommerfe {
   1013    1.7  sommerfe 	ACPI_STATUS rv;
   1014    1.7  sommerfe 
   1015    1.7  sommerfe 	if (handle == NULL)
   1016    1.7  sommerfe 		handle = ACPI_ROOT_OBJECT;
   1017    1.7  sommerfe 
   1018    1.7  sommerfe 	bufp->Pointer = NULL;
   1019   1.32  tshiozak 	bufp->Length = ACPI_ALLOCATE_BUFFER;
   1020    1.7  sommerfe 
   1021    1.7  sommerfe 	rv = AcpiEvaluateObject(handle, path, NULL, bufp);
   1022    1.7  sommerfe 
   1023   1.63     kochi 	return rv;
   1024    1.2   thorpej }
   1025    1.2   thorpej 
   1026    1.2   thorpej /*
   1027   1.65     kochi  * acpi_foreach_package_object:
   1028   1.65     kochi  *
   1029   1.65     kochi  *	Iterate over all objects in a in a packages and pass then all
   1030   1.65     kochi  *	to a function. If the called function returns non AE_OK, the
   1031   1.65     kochi  *	iteration is stopped and that value is returned.
   1032   1.65     kochi  */
   1033   1.65     kochi 
   1034   1.65     kochi ACPI_STATUS
   1035   1.68     perry acpi_foreach_package_object(ACPI_OBJECT *pkg,
   1036   1.68     perry     ACPI_STATUS (*func)(ACPI_OBJECT *, void *),
   1037   1.65     kochi     void *arg)
   1038   1.65     kochi {
   1039   1.65     kochi 	ACPI_STATUS rv = AE_OK;
   1040   1.65     kochi 	int i;
   1041   1.65     kochi 
   1042   1.65     kochi 	if (pkg == NULL || pkg->Type != ACPI_TYPE_PACKAGE)
   1043   1.65     kochi 		return AE_BAD_PARAMETER;
   1044   1.65     kochi 
   1045   1.65     kochi 	for (i = 0; i < pkg->Package.Count; i++) {
   1046   1.65     kochi 		rv = (*func)(&pkg->Package.Elements[i], arg);
   1047   1.65     kochi 		if (ACPI_FAILURE(rv))
   1048   1.65     kochi 			break;
   1049   1.65     kochi 	}
   1050   1.65     kochi 
   1051   1.65     kochi 	return rv;
   1052   1.65     kochi }
   1053   1.65     kochi 
   1054   1.65     kochi const char *
   1055   1.65     kochi acpi_name(ACPI_HANDLE handle)
   1056   1.65     kochi {
   1057   1.65     kochi 	static char buffer[80];
   1058   1.65     kochi 	ACPI_BUFFER buf;
   1059   1.65     kochi 	ACPI_STATUS rv;
   1060   1.65     kochi 
   1061   1.65     kochi 	buf.Length = sizeof(buffer);
   1062   1.65     kochi 	buf.Pointer = buffer;
   1063   1.65     kochi 
   1064   1.65     kochi 	rv = AcpiGetName(handle, ACPI_FULL_PATHNAME, &buf);
   1065   1.65     kochi 	if (ACPI_FAILURE(rv))
   1066   1.65     kochi 		return "(unknown acpi path)";
   1067   1.65     kochi 	return buffer;
   1068   1.65     kochi }
   1069   1.65     kochi 
   1070   1.65     kochi /*
   1071    1.2   thorpej  * acpi_get:
   1072    1.2   thorpej  *
   1073    1.2   thorpej  *	Fetch data info the specified (empty) ACPI buffer.
   1074   1.38     kochi  *	Caller must free buf.Pointer by AcpiOsFree().
   1075    1.2   thorpej  */
   1076    1.2   thorpej ACPI_STATUS
   1077    1.2   thorpej acpi_get(ACPI_HANDLE handle, ACPI_BUFFER *buf,
   1078    1.2   thorpej     ACPI_STATUS (*getit)(ACPI_HANDLE, ACPI_BUFFER *))
   1079    1.2   thorpej {
   1080    1.2   thorpej 	buf->Pointer = NULL;
   1081   1.38     kochi 	buf->Length = ACPI_ALLOCATE_BUFFER;
   1082    1.2   thorpej 
   1083   1.63     kochi 	return (*getit)(handle, buf);
   1084   1.54     kochi }
   1085   1.54     kochi 
   1086   1.54     kochi 
   1087   1.54     kochi /*
   1088   1.54     kochi  * acpi_match_hid
   1089   1.54     kochi  *
   1090   1.54     kochi  *	Match given ids against _HID and _CIDs
   1091   1.54     kochi  */
   1092   1.54     kochi int
   1093   1.54     kochi acpi_match_hid(ACPI_DEVICE_INFO *ad, const char * const *ids)
   1094   1.54     kochi {
   1095   1.54     kochi 	int i;
   1096   1.54     kochi 
   1097   1.54     kochi 	while (*ids) {
   1098   1.57   mycroft 		if (ad->Valid & ACPI_VALID_HID) {
   1099   1.57   mycroft 			if (pmatch(ad->HardwareId.Value, *ids, NULL) == 2)
   1100   1.63     kochi 				return 1;
   1101   1.57   mycroft 		}
   1102   1.54     kochi 
   1103   1.54     kochi 		if (ad->Valid & ACPI_VALID_CID) {
   1104   1.54     kochi 			for (i = 0; i < ad->CompatibilityId.Count; i++) {
   1105   1.57   mycroft 				if (pmatch(ad->CompatibilityId.Id[i].Value, *ids, NULL) == 2)
   1106   1.63     kochi 					return 1;
   1107   1.54     kochi 			}
   1108   1.54     kochi 		}
   1109   1.54     kochi 		ids++;
   1110   1.54     kochi 	}
   1111   1.54     kochi 
   1112   1.63     kochi 	return 0;
   1113   1.10  tshiozak }
   1114   1.10  tshiozak 
   1115   1.69     kochi /*
   1116   1.69     kochi  * acpi_set_wake_gpe
   1117   1.69     kochi  *
   1118   1.69     kochi  *	Set GPE as both Runtime and Wake
   1119   1.69     kochi  */
   1120   1.69     kochi void
   1121   1.69     kochi acpi_set_wake_gpe(ACPI_HANDLE handle)
   1122   1.69     kochi {
   1123   1.69     kochi 	ACPI_BUFFER buf;
   1124   1.69     kochi 	ACPI_STATUS rv;
   1125   1.69     kochi 	ACPI_OBJECT *p, *elt;
   1126   1.69     kochi 
   1127   1.69     kochi 	rv = acpi_eval_struct(handle, METHOD_NAME__PRW, &buf);
   1128   1.69     kochi 	if (ACPI_FAILURE(rv))
   1129   1.69     kochi 		return;			/* just ignore */
   1130   1.69     kochi 
   1131   1.69     kochi 	p = buf.Pointer;
   1132   1.69     kochi 	if (p->Type != ACPI_TYPE_PACKAGE || p->Package.Count < 2)
   1133   1.69     kochi 		goto out;		/* just ignore */
   1134   1.69     kochi 
   1135   1.69     kochi 	elt = p->Package.Elements;
   1136   1.69     kochi 
   1137   1.69     kochi 	/* TBD: package support */
   1138   1.69     kochi 	AcpiSetGpeType(NULL, elt[0].Integer.Value, ACPI_GPE_TYPE_WAKE_RUN);
   1139   1.69     kochi 	AcpiEnableGpe(NULL, elt[0].Integer.Value, ACPI_NOT_ISR);
   1140   1.69     kochi 
   1141   1.69     kochi  out:
   1142   1.69     kochi 	AcpiOsFree(buf.Pointer);
   1143   1.69     kochi }
   1144   1.69     kochi 
   1145   1.10  tshiozak 
   1146   1.10  tshiozak /*****************************************************************************
   1147   1.10  tshiozak  * ACPI sleep support.
   1148   1.10  tshiozak  *****************************************************************************/
   1149   1.10  tshiozak 
   1150   1.10  tshiozak static int
   1151   1.97  christos is_available_state(struct acpi_softc *sc, int state)
   1152   1.10  tshiozak {
   1153   1.10  tshiozak 	UINT8 type_a, type_b;
   1154   1.10  tshiozak 
   1155   1.63     kochi 	return ACPI_SUCCESS(AcpiGetSleepTypeData((UINT8)state,
   1156   1.63     kochi 				&type_a, &type_b));
   1157   1.10  tshiozak }
   1158   1.10  tshiozak 
   1159   1.10  tshiozak /*
   1160   1.10  tshiozak  * acpi_enter_sleep_state:
   1161   1.10  tshiozak  *
   1162   1.10  tshiozak  *	enter to the specified sleep state.
   1163   1.10  tshiozak  */
   1164   1.10  tshiozak 
   1165   1.10  tshiozak ACPI_STATUS
   1166   1.10  tshiozak acpi_enter_sleep_state(struct acpi_softc *sc, int state)
   1167   1.10  tshiozak {
   1168  1.104  jmcneill 	int err;
   1169   1.10  tshiozak 	ACPI_STATUS ret = AE_OK;
   1170   1.10  tshiozak 
   1171   1.92  christos 	if (state == acpi_sleepstate)
   1172   1.92  christos 		return AE_OK;
   1173   1.92  christos 
   1174   1.92  christos 	aprint_normal("%s: entering state %d\n", sc->sc_dev.dv_xname, state);
   1175   1.92  christos 
   1176   1.10  tshiozak 	switch (state) {
   1177   1.10  tshiozak 	case ACPI_STATE_S0:
   1178   1.10  tshiozak 		break;
   1179   1.10  tshiozak 	case ACPI_STATE_S1:
   1180   1.10  tshiozak 	case ACPI_STATE_S2:
   1181   1.10  tshiozak 	case ACPI_STATE_S3:
   1182   1.10  tshiozak 	case ACPI_STATE_S4:
   1183   1.10  tshiozak 		if (!is_available_state(sc, state)) {
   1184  1.105   reinoud 			aprint_error("%s: cannot enter the sleep state (%d)\n",
   1185   1.92  christos 			    sc->sc_dev.dv_xname, state);
   1186   1.10  tshiozak 			break;
   1187   1.10  tshiozak 		}
   1188  1.104  jmcneill 
   1189  1.104  jmcneill 		if (state != ACPI_STATE_S1 && !pmf_system_suspend()) {
   1190  1.105   reinoud 			aprint_error_dev(&sc->sc_dev, "aborting suspend\n");
   1191  1.104  jmcneill 			break;
   1192  1.104  jmcneill 		}
   1193  1.104  jmcneill 
   1194   1.10  tshiozak 		ret = AcpiEnterSleepStatePrep(state);
   1195   1.10  tshiozak 		if (ACPI_FAILURE(ret)) {
   1196   1.92  christos 			aprint_error("%s: failed preparing to sleep (%s)\n",
   1197   1.92  christos 			    sc->sc_dev.dv_xname, AcpiFormatException(ret));
   1198   1.10  tshiozak 			break;
   1199   1.10  tshiozak 		}
   1200  1.104  jmcneill 
   1201   1.92  christos 		acpi_sleepstate = state;
   1202   1.92  christos 		if (state == ACPI_STATE_S1) {
   1203   1.10  tshiozak 			/* just enter the state */
   1204   1.12   kanaoka 			acpi_md_OsDisableInterrupt();
   1205   1.10  tshiozak 			AcpiEnterSleepState((UINT8)state);
   1206  1.104  jmcneill 			AcpiLeaveSleepState((UINT8)state);
   1207   1.10  tshiozak 		} else {
   1208  1.104  jmcneill 			err = acpi_md_sleep(state);
   1209  1.104  jmcneill 			if (state == ACPI_STATE_S4)
   1210   1.10  tshiozak 				AcpiEnable();
   1211  1.106  jmcneill 			pmf_system_bus_resume();
   1212  1.104  jmcneill 			AcpiLeaveSleepState((UINT8)state);
   1213  1.104  jmcneill 			pmf_system_resume();
   1214   1.10  tshiozak 		}
   1215  1.104  jmcneill 
   1216   1.10  tshiozak 		break;
   1217   1.10  tshiozak 	case ACPI_STATE_S5:
   1218   1.42     kochi 		ret = AcpiEnterSleepStatePrep(ACPI_STATE_S5);
   1219   1.42     kochi 		if (ACPI_FAILURE(ret)) {
   1220   1.92  christos 			aprint_error("%s: failed preparing to sleep (%s)\n",
   1221   1.92  christos 			       sc->sc_dev.dv_xname, AcpiFormatException(ret));
   1222   1.42     kochi 			break;
   1223   1.42     kochi 		}
   1224  1.104  jmcneill 		DELAY(1000000);
   1225   1.92  christos 		acpi_sleepstate = state;
   1226   1.12   kanaoka 		acpi_md_OsDisableInterrupt();
   1227   1.10  tshiozak 		AcpiEnterSleepState(ACPI_STATE_S5);
   1228   1.92  christos 		aprint_error("%s: WARNING powerdown failed!\n",
   1229   1.92  christos 		    sc->sc_dev.dv_xname);
   1230   1.10  tshiozak 		break;
   1231   1.10  tshiozak 	}
   1232   1.10  tshiozak 
   1233   1.92  christos 	acpi_sleepstate = ACPI_STATE_S0;
   1234   1.63     kochi 	return ret;
   1235    1.1   thorpej }
   1236   1.13  augustss 
   1237   1.93  christos #if defined(ACPI_ACTIVATE_DEV)
   1238   1.93  christos /* XXX This very incomplete */
   1239   1.13  augustss ACPI_STATUS
   1240   1.13  augustss acpi_allocate_resources(ACPI_HANDLE handle)
   1241   1.13  augustss {
   1242   1.13  augustss 	ACPI_BUFFER bufp, bufc, bufn;
   1243   1.13  augustss 	ACPI_RESOURCE *resp, *resc, *resn;
   1244   1.13  augustss 	ACPI_RESOURCE_IRQ *irq;
   1245   1.93  christos 	ACPI_RESOURCE_EXTENDED_IRQ *xirq;
   1246   1.13  augustss 	ACPI_STATUS rv;
   1247   1.13  augustss 	uint delta;
   1248   1.13  augustss 
   1249   1.13  augustss 	rv = acpi_get(handle, &bufp, AcpiGetPossibleResources);
   1250   1.13  augustss 	if (ACPI_FAILURE(rv))
   1251   1.13  augustss 		goto out;
   1252   1.13  augustss 	rv = acpi_get(handle, &bufc, AcpiGetCurrentResources);
   1253   1.13  augustss 	if (ACPI_FAILURE(rv)) {
   1254   1.13  augustss 		goto out1;
   1255   1.13  augustss 	}
   1256   1.13  augustss 
   1257   1.13  augustss 	bufn.Length = 1000;
   1258   1.33  christos 	bufn.Pointer = resn = malloc(bufn.Length, M_ACPI, M_WAITOK);
   1259   1.13  augustss 	resp = bufp.Pointer;
   1260   1.13  augustss 	resc = bufc.Pointer;
   1261   1.85      cube 	while (resc->Type != ACPI_RESOURCE_TYPE_END_TAG &&
   1262   1.85      cube 	       resp->Type != ACPI_RESOURCE_TYPE_END_TAG) {
   1263   1.85      cube 		while (resc->Type != resp->Type && resp->Type != ACPI_RESOURCE_TYPE_END_TAG)
   1264   1.13  augustss 			resp = ACPI_NEXT_RESOURCE(resp);
   1265   1.85      cube 		if (resp->Type == ACPI_RESOURCE_TYPE_END_TAG)
   1266   1.13  augustss 			break;
   1267   1.13  augustss 		/* Found identical Id */
   1268   1.80     kochi 		resn->Type = resc->Type;
   1269   1.80     kochi 		switch (resc->Type) {
   1270   1.80     kochi 		case ACPI_RESOURCE_TYPE_IRQ:
   1271   1.13  augustss 			memcpy(&resn->Data, &resp->Data,
   1272   1.13  augustss 			       sizeof(ACPI_RESOURCE_IRQ));
   1273   1.13  augustss 			irq = (ACPI_RESOURCE_IRQ *)&resn->Data;
   1274   1.13  augustss 			irq->Interrupts[0] =
   1275   1.13  augustss 			    ((ACPI_RESOURCE_IRQ *)&resp->Data)->
   1276   1.80     kochi 			        Interrupts[irq->InterruptCount-1];
   1277   1.80     kochi 			irq->InterruptCount = 1;
   1278   1.80     kochi 			resn->Length = ACPI_RS_SIZE(ACPI_RESOURCE_IRQ);
   1279   1.13  augustss 			break;
   1280   1.93  christos 		case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
   1281   1.93  christos 			memcpy(&resn->Data, &resp->Data,
   1282   1.93  christos 			       sizeof(ACPI_RESOURCE_EXTENDED_IRQ));
   1283   1.93  christos 			xirq = (ACPI_RESOURCE_EXTENDED_IRQ *)&resn->Data;
   1284   1.93  christos #if 0
   1285   1.93  christos 			/*
   1286   1.93  christos 			 * XXX not duplicating the interrupt logic above
   1287   1.93  christos 			 * because its not clear what it accomplishes.
   1288   1.93  christos 			 */
   1289   1.93  christos 			xirq->Interrupts[0] =
   1290   1.93  christos 			    ((ACPI_RESOURCE_EXT_IRQ *)&resp->Data)->
   1291   1.93  christos 			    Interrupts[irq->NumberOfInterrupts-1];
   1292   1.93  christos 			xirq->NumberOfInterrupts = 1;
   1293   1.93  christos #endif
   1294   1.93  christos 			resn->Length = ACPI_RS_SIZE(ACPI_RESOURCE_EXTENDED_IRQ);
   1295   1.93  christos 			break;
   1296   1.80     kochi 		case ACPI_RESOURCE_TYPE_IO:
   1297   1.13  augustss 			memcpy(&resn->Data, &resp->Data,
   1298   1.13  augustss 			       sizeof(ACPI_RESOURCE_IO));
   1299   1.13  augustss 			resn->Length = resp->Length;
   1300   1.13  augustss 			break;
   1301   1.13  augustss 		default:
   1302   1.80     kochi 			printf("acpi_allocate_resources: res=%d\n", resc->Type);
   1303   1.13  augustss 			rv = AE_BAD_DATA;
   1304   1.13  augustss 			goto out2;
   1305   1.13  augustss 		}
   1306   1.13  augustss 		resc = ACPI_NEXT_RESOURCE(resc);
   1307   1.13  augustss 		resn = ACPI_NEXT_RESOURCE(resn);
   1308   1.89      cube 		resp = ACPI_NEXT_RESOURCE(resp);
   1309   1.13  augustss 		delta = (UINT8 *)resn - (UINT8 *)bufn.Pointer;
   1310   1.62     kochi 		if (delta >=
   1311   1.80     kochi 		    bufn.Length-ACPI_RS_SIZE(ACPI_RESOURCE_DATA)) {
   1312   1.13  augustss 			bufn.Length *= 2;
   1313   1.13  augustss 			bufn.Pointer = realloc(bufn.Pointer, bufn.Length,
   1314   1.33  christos 					       M_ACPI, M_WAITOK);
   1315   1.13  augustss 			resn = (ACPI_RESOURCE *)((UINT8 *)bufn.Pointer + delta);
   1316   1.13  augustss 		}
   1317   1.13  augustss 	}
   1318   1.85      cube 	if (resc->Type != ACPI_RESOURCE_TYPE_END_TAG) {
   1319   1.13  augustss 		printf("acpi_allocate_resources: resc not exhausted\n");
   1320   1.13  augustss 		rv = AE_BAD_DATA;
   1321   1.13  augustss 		goto out3;
   1322   1.13  augustss 	}
   1323   1.13  augustss 
   1324   1.85      cube 	resn->Type = ACPI_RESOURCE_TYPE_END_TAG;
   1325   1.13  augustss 	rv = AcpiSetCurrentResources(handle, &bufn);
   1326   1.13  augustss 	if (ACPI_FAILURE(rv)) {
   1327   1.13  augustss 		printf("acpi_allocate_resources: AcpiSetCurrentResources %s\n",
   1328   1.13  augustss 		       AcpiFormatException(rv));
   1329   1.13  augustss 	}
   1330   1.13  augustss 
   1331   1.13  augustss out3:
   1332   1.33  christos 	free(bufn.Pointer, M_ACPI);
   1333   1.13  augustss out2:
   1334   1.38     kochi 	AcpiOsFree(bufc.Pointer);
   1335   1.13  augustss out1:
   1336   1.38     kochi 	AcpiOsFree(bufp.Pointer);
   1337   1.13  augustss out:
   1338   1.13  augustss 	return rv;
   1339   1.13  augustss }
   1340   1.93  christos #endif /* ACPI_ACTIVATE_DEV */
   1341   1.79      cube 
   1342   1.79      cube SYSCTL_SETUP(sysctl_acpi_setup, "sysctl hw.acpi subtree setup")
   1343   1.79      cube {
   1344   1.79      cube 	const struct sysctlnode *node;
   1345   1.86  jmcneill 	const struct sysctlnode *ssnode;
   1346   1.79      cube 
   1347   1.79      cube 	if (sysctl_createv(clog, 0, NULL, NULL,
   1348   1.79      cube 	    CTLFLAG_PERMANENT,
   1349   1.79      cube 	    CTLTYPE_NODE, "hw", NULL,
   1350   1.79      cube 	    NULL, 0, NULL, 0,
   1351   1.79      cube 	    CTL_HW, CTL_EOL) != 0)
   1352   1.79      cube 		return;
   1353   1.79      cube 
   1354   1.79      cube 	if (sysctl_createv(clog, 0, NULL, &node,
   1355   1.79      cube 	    CTLFLAG_PERMANENT,
   1356   1.79      cube 	    CTLTYPE_NODE, "acpi", NULL,
   1357   1.79      cube 	    NULL, 0, NULL, 0,
   1358   1.79      cube 	    CTL_HW, CTL_CREATE, CTL_EOL) != 0)
   1359   1.79      cube 		return;
   1360   1.79      cube 
   1361   1.79      cube 	acpi_node = node->sysctl_num;
   1362   1.86  jmcneill 
   1363   1.86  jmcneill 	/* ACPI sleepstate sysctl */
   1364   1.86  jmcneill 	if (sysctl_createv(NULL, 0, NULL, &node,
   1365   1.86  jmcneill 	    CTLFLAG_PERMANENT,
   1366   1.86  jmcneill 	    CTLTYPE_NODE, "machdep", NULL,
   1367   1.86  jmcneill 	    NULL, 0, NULL, 0, CTL_MACHDEP, CTL_EOL) != 0)
   1368   1.86  jmcneill 		return;
   1369   1.86  jmcneill 	if (sysctl_createv(NULL, 0, &node, &ssnode,
   1370   1.86  jmcneill 	    CTLFLAG_READWRITE, CTLTYPE_INT, "sleep_state",
   1371   1.86  jmcneill 	    NULL, sysctl_hw_acpi_sleepstate, 0, NULL, 0, CTL_CREATE,
   1372   1.86  jmcneill 	    CTL_EOL) != 0)
   1373   1.86  jmcneill 		return;
   1374   1.86  jmcneill }
   1375   1.86  jmcneill 
   1376   1.86  jmcneill static int
   1377   1.86  jmcneill sysctl_hw_acpi_sleepstate(SYSCTLFN_ARGS)
   1378   1.86  jmcneill {
   1379   1.86  jmcneill 	int error, t;
   1380   1.86  jmcneill 	struct sysctlnode node;
   1381   1.86  jmcneill 
   1382   1.86  jmcneill 	node = *rnode;
   1383   1.86  jmcneill 	t = acpi_sleepstate;
   1384   1.86  jmcneill 	node.sysctl_data = &t;
   1385   1.86  jmcneill 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
   1386   1.86  jmcneill 	if (error || newp == NULL)
   1387   1.86  jmcneill 		return error;
   1388   1.86  jmcneill 
   1389   1.92  christos 	if (acpi_softc == NULL)
   1390   1.92  christos 		return ENOSYS;
   1391   1.86  jmcneill 
   1392   1.92  christos 	acpi_enter_sleep_state(acpi_softc, t);
   1393   1.86  jmcneill 
   1394   1.86  jmcneill 	return 0;
   1395   1.79      cube }
   1396