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