Home | History | Annotate | Line # | Download | only in acpi
acpi_ec.c revision 1.87
      1  1.87  riastrad /*	$NetBSD: acpi_ec.c,v 1.87 2023/07/18 10:02:09 riastradh Exp $	*/
      2   1.1   thorpej 
      3  1.44  jmcneill /*-
      4  1.44  jmcneill  * Copyright (c) 2007 Joerg Sonnenberger <joerg (at) NetBSD.org>.
      5   1.1   thorpej  * All rights reserved.
      6   1.1   thorpej  *
      7   1.1   thorpej  * Redistribution and use in source and binary forms, with or without
      8   1.1   thorpej  * modification, are permitted provided that the following conditions
      9   1.1   thorpej  * are met:
     10   1.1   thorpej  *
     11   1.1   thorpej  * 1. Redistributions of source code must retain the above copyright
     12   1.1   thorpej  *    notice, this list of conditions and the following disclaimer.
     13   1.1   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.44  jmcneill  *    notice, this list of conditions and the following disclaimer in
     15  1.44  jmcneill  *    the documentation and/or other materials provided with the
     16  1.44  jmcneill  *    distribution.
     17  1.44  jmcneill  *
     18  1.44  jmcneill  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     19  1.44  jmcneill  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     20  1.44  jmcneill  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
     21  1.44  jmcneill  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
     22  1.44  jmcneill  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
     23  1.44  jmcneill  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
     24  1.44  jmcneill  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     25  1.44  jmcneill  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     26  1.44  jmcneill  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     27  1.44  jmcneill  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
     28  1.44  jmcneill  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29   1.1   thorpej  * SUCH DAMAGE.
     30   1.1   thorpej  */
     31   1.1   thorpej 
     32  1.46     joerg /*
     33  1.46     joerg  * The ACPI Embedded Controller (EC) driver serves two different purposes:
     34  1.46     joerg  * - read and write access from ASL, e.g. to read battery state
     35  1.46     joerg  * - notification of ASL of System Control Interrupts.
     36  1.46     joerg  *
     37  1.46     joerg  * Access to the EC is serialised by sc_access_mtx and optionally the
     38  1.46     joerg  * ACPI global mutex.  Both locks are held until the request is fulfilled.
     39  1.46     joerg  * All access to the softc has to hold sc_mtx to serialise against the GPE
     40  1.46     joerg  * handler and the callout.  sc_mtx is also used for wakeup conditions.
     41  1.46     joerg  *
     42  1.46     joerg  * SCIs are processed in a kernel thread. Handling gets a bit complicated
     43  1.46     joerg  * by the lock order (sc_mtx must be acquired after sc_access_mtx and the
     44  1.46     joerg  * ACPI global mutex).
     45  1.46     joerg  *
     46  1.46     joerg  * Read and write requests spin around for a short time as many requests
     47  1.46     joerg  * can be handled instantly by the EC.  During normal processing interrupt
     48  1.46     joerg  * mode is used exclusively.  At boot and resume time interrupts are not
     49  1.46     joerg  * working and the handlers just busy loop.
     50  1.46     joerg  *
     51  1.46     joerg  * A callout is scheduled to compensate for missing interrupts on some
     52  1.46     joerg  * hardware.  If the EC doesn't process a request for 5s, it is most likely
     53  1.46     joerg  * in a wedged state.  No method to reset the EC is currently known.
     54  1.46     joerg  *
     55  1.46     joerg  * Special care has to be taken to not poll the EC in a busy loop without
     56  1.46     joerg  * delay.  This can prevent processing of Power Button events. At least some
     57  1.46     joerg  * Lenovo Thinkpads seem to be implement the Power Button Override in the EC
     58  1.46     joerg  * and the only option to recover on those models is to cut off all power.
     59  1.46     joerg  */
     60  1.46     joerg 
     61   1.3     lukem #include <sys/cdefs.h>
     62  1.87  riastrad __KERNEL_RCSID(0, "$NetBSD: acpi_ec.c,v 1.87 2023/07/18 10:02:09 riastradh Exp $");
     63   1.1   thorpej 
     64   1.1   thorpej #include <sys/param.h>
     65  1.62    jruoho #include <sys/callout.h>
     66  1.44  jmcneill #include <sys/condvar.h>
     67   1.1   thorpej #include <sys/device.h>
     68   1.1   thorpej #include <sys/kernel.h>
     69  1.44  jmcneill #include <sys/kthread.h>
     70  1.44  jmcneill #include <sys/mutex.h>
     71  1.62    jruoho #include <sys/systm.h>
     72   1.1   thorpej 
     73  1.57   mlelstv #include <dev/acpi/acpireg.h>
     74   1.1   thorpej #include <dev/acpi/acpivar.h>
     75  1.48  jmcneill #include <dev/acpi/acpi_ecvar.h>
     76   1.1   thorpej 
     77  1.57   mlelstv #define _COMPONENT          ACPI_EC_COMPONENT
     78  1.57   mlelstv ACPI_MODULE_NAME            ("acpi_ec")
     79  1.57   mlelstv 
     80  1.44  jmcneill /* Maximum time to wait for global ACPI lock in ms */
     81  1.44  jmcneill #define	EC_LOCK_TIMEOUT		5
     82  1.23     kochi 
     83  1.44  jmcneill /* Maximum time to poll for completion of a command  in ms */
     84  1.44  jmcneill #define	EC_POLL_TIMEOUT		5
     85   1.1   thorpej 
     86  1.46     joerg /* Maximum time to give a single EC command in s */
     87  1.46     joerg #define EC_CMD_TIMEOUT		10
     88  1.46     joerg 
     89  1.44  jmcneill /* From ACPI 3.0b, chapter 12.3 */
     90  1.44  jmcneill #define EC_COMMAND_READ		0x80
     91  1.44  jmcneill #define	EC_COMMAND_WRITE	0x81
     92  1.44  jmcneill #define	EC_COMMAND_BURST_EN	0x82
     93  1.44  jmcneill #define	EC_COMMAND_BURST_DIS	0x83
     94  1.44  jmcneill #define	EC_COMMAND_QUERY	0x84
     95  1.44  jmcneill 
     96  1.44  jmcneill /* From ACPI 3.0b, chapter 12.2.1 */
     97  1.44  jmcneill #define	EC_STATUS_OBF		0x01
     98  1.44  jmcneill #define	EC_STATUS_IBF		0x02
     99  1.44  jmcneill #define	EC_STATUS_CMD		0x08
    100  1.44  jmcneill #define	EC_STATUS_BURST		0x10
    101  1.44  jmcneill #define	EC_STATUS_SCI		0x20
    102  1.44  jmcneill #define	EC_STATUS_SMI		0x40
    103   1.1   thorpej 
    104  1.85   thorpej static const struct device_compatible_entry compat_data[] = {
    105  1.85   thorpej 	{ .compat = "PNP0C09" },
    106  1.85   thorpej 	DEVICE_COMPAT_EOL
    107  1.44  jmcneill };
    108  1.44  jmcneill 
    109  1.44  jmcneill enum ec_state_t {
    110  1.44  jmcneill 	EC_STATE_QUERY,
    111  1.44  jmcneill 	EC_STATE_QUERY_VAL,
    112  1.44  jmcneill 	EC_STATE_READ,
    113  1.44  jmcneill 	EC_STATE_READ_ADDR,
    114  1.44  jmcneill 	EC_STATE_READ_VAL,
    115  1.44  jmcneill 	EC_STATE_WRITE,
    116  1.44  jmcneill 	EC_STATE_WRITE_ADDR,
    117  1.44  jmcneill 	EC_STATE_WRITE_VAL,
    118  1.44  jmcneill 	EC_STATE_FREE
    119  1.44  jmcneill };
    120  1.44  jmcneill 
    121  1.44  jmcneill struct acpiec_softc {
    122  1.87  riastrad 	device_t sc_dev;
    123  1.87  riastrad 
    124  1.44  jmcneill 	ACPI_HANDLE sc_ech;
    125   1.1   thorpej 
    126  1.44  jmcneill 	ACPI_HANDLE sc_gpeh;
    127  1.65    jruoho 	uint8_t sc_gpebit;
    128   1.1   thorpej 
    129  1.44  jmcneill 	bus_space_tag_t sc_data_st;
    130  1.44  jmcneill 	bus_space_handle_t sc_data_sh;
    131   1.1   thorpej 
    132  1.44  jmcneill 	bus_space_tag_t sc_csr_st;
    133  1.44  jmcneill 	bus_space_handle_t sc_csr_sh;
    134   1.1   thorpej 
    135  1.44  jmcneill 	bool sc_need_global_lock;
    136  1.65    jruoho 	uint32_t sc_global_lock;
    137  1.17   mycroft 
    138  1.44  jmcneill 	kmutex_t sc_mtx, sc_access_mtx;
    139  1.44  jmcneill 	kcondvar_t sc_cv, sc_cv_sci;
    140  1.44  jmcneill 	enum ec_state_t sc_state;
    141  1.44  jmcneill 	bool sc_got_sci;
    142  1.46     joerg 	callout_t sc_pseudo_intr;
    143  1.44  jmcneill 
    144  1.44  jmcneill 	uint8_t sc_cur_addr, sc_cur_val;
    145  1.23     kochi };
    146   1.1   thorpej 
    147  1.55    cegger static int acpiecdt_match(device_t, cfdata_t, void *);
    148  1.44  jmcneill static void acpiecdt_attach(device_t, device_t, void *);
    149  1.44  jmcneill 
    150  1.55    cegger static int acpiec_match(device_t, cfdata_t, void *);
    151  1.44  jmcneill static void acpiec_attach(device_t, device_t, void *);
    152  1.16     kochi 
    153  1.44  jmcneill static void acpiec_common_attach(device_t, device_t, ACPI_HANDLE,
    154  1.63    dyoung     bus_space_tag_t, bus_addr_t, bus_space_tag_t, bus_addr_t,
    155  1.63    dyoung     ACPI_HANDLE, uint8_t);
    156   1.1   thorpej 
    157  1.60    dyoung static bool acpiec_suspend(device_t, const pmf_qual_t *);
    158  1.60    dyoung static bool acpiec_resume(device_t, const pmf_qual_t *);
    159  1.56       alc static bool acpiec_shutdown(device_t, int);
    160  1.17   mycroft 
    161  1.44  jmcneill static bool acpiec_parse_gpe_package(device_t, ACPI_HANDLE,
    162  1.44  jmcneill     ACPI_HANDLE *, uint8_t *);
    163  1.20      yamt 
    164  1.46     joerg static void acpiec_callout(void *);
    165  1.44  jmcneill static void acpiec_gpe_query(void *);
    166  1.69    jruoho static uint32_t acpiec_gpe_handler(ACPI_HANDLE, uint32_t, void *);
    167  1.65    jruoho static ACPI_STATUS acpiec_space_setup(ACPI_HANDLE, uint32_t, void *, void **);
    168  1.65    jruoho static ACPI_STATUS acpiec_space_handler(uint32_t, ACPI_PHYSICAL_ADDRESS,
    169  1.65    jruoho     uint32_t, ACPI_INTEGER *, void *, void *);
    170  1.20      yamt 
    171  1.45  jmcneill static void acpiec_gpe_state_machine(device_t);
    172  1.20      yamt 
    173  1.44  jmcneill CFATTACH_DECL_NEW(acpiec, sizeof(struct acpiec_softc),
    174  1.20      yamt     acpiec_match, acpiec_attach, NULL, NULL);
    175  1.20      yamt 
    176  1.44  jmcneill CFATTACH_DECL_NEW(acpiecdt, sizeof(struct acpiec_softc),
    177  1.44  jmcneill     acpiecdt_match, acpiecdt_attach, NULL, NULL);
    178  1.44  jmcneill 
    179  1.44  jmcneill static device_t ec_singleton = NULL;
    180  1.44  jmcneill static bool acpiec_cold = false;
    181  1.23     kochi 
    182  1.44  jmcneill static bool
    183  1.44  jmcneill acpiecdt_find(device_t parent, ACPI_HANDLE *ec_handle,
    184  1.44  jmcneill     bus_addr_t *cmd_reg, bus_addr_t *data_reg, uint8_t *gpebit)
    185   1.9  tshiozak {
    186  1.44  jmcneill 	ACPI_TABLE_ECDT *ecdt;
    187  1.44  jmcneill 	ACPI_STATUS rv;
    188  1.44  jmcneill 
    189  1.44  jmcneill 	rv = AcpiGetTable(ACPI_SIG_ECDT, 1, (ACPI_TABLE_HEADER **)&ecdt);
    190  1.44  jmcneill 	if (ACPI_FAILURE(rv))
    191  1.44  jmcneill 		return false;
    192  1.44  jmcneill 
    193  1.44  jmcneill 	if (ecdt->Control.BitWidth != 8 || ecdt->Data.BitWidth != 8) {
    194  1.44  jmcneill 		aprint_error_dev(parent,
    195  1.61    jruoho 		    "ECDT register width invalid (%u/%u)\n",
    196  1.44  jmcneill 		    ecdt->Control.BitWidth, ecdt->Data.BitWidth);
    197  1.44  jmcneill 		return false;
    198  1.44  jmcneill 	}
    199  1.24     kochi 
    200  1.44  jmcneill 	rv = AcpiGetHandle(ACPI_ROOT_OBJECT, ecdt->Id, ec_handle);
    201  1.44  jmcneill 	if (ACPI_FAILURE(rv)) {
    202  1.44  jmcneill 		aprint_error_dev(parent,
    203  1.44  jmcneill 		    "failed to look up EC object %s: %s\n",
    204  1.44  jmcneill 		    ecdt->Id, AcpiFormatException(rv));
    205  1.44  jmcneill 		return false;
    206  1.44  jmcneill 	}
    207  1.44  jmcneill 
    208  1.44  jmcneill 	*cmd_reg = ecdt->Control.Address;
    209  1.44  jmcneill 	*data_reg = ecdt->Data.Address;
    210  1.44  jmcneill 	*gpebit = ecdt->Gpe;
    211  1.44  jmcneill 
    212  1.44  jmcneill 	return true;
    213   1.9  tshiozak }
    214   1.4   thorpej 
    215  1.44  jmcneill static int
    216  1.55    cegger acpiecdt_match(device_t parent, cfdata_t match, void *aux)
    217   1.1   thorpej {
    218  1.44  jmcneill 	ACPI_HANDLE ec_handle;
    219  1.44  jmcneill 	bus_addr_t cmd_reg, data_reg;
    220  1.44  jmcneill 	uint8_t gpebit;
    221   1.1   thorpej 
    222  1.44  jmcneill 	if (acpiecdt_find(parent, &ec_handle, &cmd_reg, &data_reg, &gpebit))
    223  1.44  jmcneill 		return 1;
    224  1.44  jmcneill 	else
    225  1.44  jmcneill 		return 0;
    226   1.1   thorpej }
    227   1.1   thorpej 
    228  1.44  jmcneill static void
    229  1.44  jmcneill acpiecdt_attach(device_t parent, device_t self, void *aux)
    230   1.1   thorpej {
    231  1.64    dyoung 	struct acpibus_attach_args *aa = aux;
    232  1.44  jmcneill 	ACPI_HANDLE ec_handle;
    233  1.44  jmcneill 	bus_addr_t cmd_reg, data_reg;
    234  1.44  jmcneill 	uint8_t gpebit;
    235  1.44  jmcneill 
    236  1.44  jmcneill 	if (!acpiecdt_find(parent, &ec_handle, &cmd_reg, &data_reg, &gpebit))
    237  1.44  jmcneill 		panic("ECDT disappeared");
    238  1.44  jmcneill 
    239  1.53  jmcneill 	aprint_naive("\n");
    240  1.44  jmcneill 	aprint_normal(": ACPI Embedded Controller via ECDT\n");
    241  1.20      yamt 
    242  1.63    dyoung 	acpiec_common_attach(parent, self, ec_handle, aa->aa_iot, cmd_reg,
    243  1.63    dyoung 	    aa->aa_iot, data_reg, NULL, gpebit);
    244   1.1   thorpej }
    245   1.1   thorpej 
    246  1.31     kochi static int
    247  1.55    cegger acpiec_match(device_t parent, cfdata_t match, void *aux)
    248   1.1   thorpej {
    249   1.1   thorpej 	struct acpi_attach_args *aa = aux;
    250   1.1   thorpej 
    251  1.85   thorpej 	return acpi_compatible_match(aa, compat_data);
    252   1.1   thorpej }
    253   1.1   thorpej 
    254  1.44  jmcneill static void
    255  1.44  jmcneill acpiec_attach(device_t parent, device_t self, void *aux)
    256  1.17   mycroft {
    257  1.44  jmcneill 	struct acpi_attach_args *aa = aux;
    258  1.44  jmcneill 	struct acpi_resources ec_res;
    259  1.44  jmcneill 	struct acpi_io *io0, *io1;
    260  1.44  jmcneill 	ACPI_HANDLE gpe_handle;
    261  1.44  jmcneill 	uint8_t gpebit;
    262  1.23     kochi 	ACPI_STATUS rv;
    263  1.17   mycroft 
    264  1.44  jmcneill 	if (ec_singleton != NULL) {
    265  1.54  jmcneill 		aprint_naive(": using %s\n", device_xname(ec_singleton));
    266  1.54  jmcneill 		aprint_normal(": using %s\n", device_xname(ec_singleton));
    267  1.73  riastrad 		goto fail0;
    268  1.44  jmcneill 	}
    269  1.44  jmcneill 
    270  1.84  jdolecek 	if (!acpi_device_present(aa->aa_node->ad_handle)) {
    271  1.84  jdolecek 		aprint_normal(": not present\n");
    272  1.84  jdolecek 		goto fail0;
    273  1.84  jdolecek 	}
    274  1.84  jdolecek 
    275  1.44  jmcneill 	if (!acpiec_parse_gpe_package(self, aa->aa_node->ad_handle,
    276  1.44  jmcneill 				      &gpe_handle, &gpebit))
    277  1.73  riastrad 		goto fail0;
    278  1.17   mycroft 
    279  1.44  jmcneill 	rv = acpi_resource_parse(self, aa->aa_node->ad_handle, "_CRS",
    280  1.44  jmcneill 	    &ec_res, &acpi_resource_parse_ops_default);
    281  1.44  jmcneill 	if (rv != AE_OK) {
    282  1.44  jmcneill 		aprint_error_dev(self, "resource parsing failed: %s\n",
    283  1.44  jmcneill 		    AcpiFormatException(rv));
    284  1.73  riastrad 		goto fail0;
    285  1.17   mycroft 	}
    286  1.17   mycroft 
    287  1.44  jmcneill 	if ((io0 = acpi_res_io(&ec_res, 0)) == NULL) {
    288  1.44  jmcneill 		aprint_error_dev(self, "no data register resource\n");
    289  1.73  riastrad 		goto fail1;
    290  1.44  jmcneill 	}
    291  1.44  jmcneill 	if ((io1 = acpi_res_io(&ec_res, 1)) == NULL) {
    292  1.44  jmcneill 		aprint_error_dev(self, "no CSR register resource\n");
    293  1.73  riastrad 		goto fail1;
    294  1.23     kochi 	}
    295  1.23     kochi 
    296  1.44  jmcneill 	acpiec_common_attach(parent, self, aa->aa_node->ad_handle,
    297  1.63    dyoung 	    aa->aa_iot, io1->ar_base, aa->aa_iot, io0->ar_base,
    298  1.63    dyoung 	    gpe_handle, gpebit);
    299  1.23     kochi 
    300  1.44  jmcneill 	acpi_resource_cleanup(&ec_res);
    301  1.73  riastrad 	return;
    302  1.73  riastrad 
    303  1.73  riastrad fail1:	acpi_resource_cleanup(&ec_res);
    304  1.73  riastrad fail0:	if (!pmf_device_register(self, NULL, NULL))
    305  1.73  riastrad 		aprint_error_dev(self, "couldn't establish power handler\n");
    306  1.44  jmcneill }
    307  1.23     kochi 
    308  1.44  jmcneill static void
    309  1.44  jmcneill acpiec_common_attach(device_t parent, device_t self,
    310  1.63    dyoung     ACPI_HANDLE ec_handle, bus_space_tag_t cmdt, bus_addr_t cmd_reg,
    311  1.63    dyoung     bus_space_tag_t datat, bus_addr_t data_reg,
    312  1.44  jmcneill     ACPI_HANDLE gpe_handle, uint8_t gpebit)
    313  1.44  jmcneill {
    314  1.44  jmcneill 	struct acpiec_softc *sc = device_private(self);
    315  1.44  jmcneill 	ACPI_STATUS rv;
    316  1.44  jmcneill 	ACPI_INTEGER val;
    317  1.23     kochi 
    318  1.87  riastrad 	sc->sc_dev = self;
    319  1.87  riastrad 
    320  1.63    dyoung 	sc->sc_csr_st = cmdt;
    321  1.63    dyoung 	sc->sc_data_st = datat;
    322  1.63    dyoung 
    323  1.44  jmcneill 	sc->sc_ech = ec_handle;
    324  1.44  jmcneill 	sc->sc_gpeh = gpe_handle;
    325  1.44  jmcneill 	sc->sc_gpebit = gpebit;
    326  1.44  jmcneill 
    327  1.44  jmcneill 	sc->sc_state = EC_STATE_FREE;
    328  1.44  jmcneill 	mutex_init(&sc->sc_mtx, MUTEX_DRIVER, IPL_TTY);
    329  1.44  jmcneill 	mutex_init(&sc->sc_access_mtx, MUTEX_DEFAULT, IPL_NONE);
    330  1.44  jmcneill 	cv_init(&sc->sc_cv, "eccv");
    331  1.44  jmcneill 	cv_init(&sc->sc_cv_sci, "ecsci");
    332  1.44  jmcneill 
    333  1.44  jmcneill 	if (bus_space_map(sc->sc_data_st, data_reg, 1, 0,
    334  1.44  jmcneill 	    &sc->sc_data_sh) != 0) {
    335  1.44  jmcneill 		aprint_error_dev(self, "unable to map data register\n");
    336  1.44  jmcneill 		return;
    337  1.44  jmcneill 	}
    338  1.23     kochi 
    339  1.44  jmcneill 	if (bus_space_map(sc->sc_csr_st, cmd_reg, 1, 0, &sc->sc_csr_sh) != 0) {
    340  1.44  jmcneill 		aprint_error_dev(self, "unable to map CSR register\n");
    341  1.44  jmcneill 		goto post_data_map;
    342  1.23     kochi 	}
    343  1.23     kochi 
    344  1.44  jmcneill 	rv = acpi_eval_integer(sc->sc_ech, "_GLK", &val);
    345  1.44  jmcneill 	if (rv == AE_OK) {
    346  1.44  jmcneill 		sc->sc_need_global_lock = val != 0;
    347  1.44  jmcneill 	} else if (rv != AE_NOT_FOUND) {
    348  1.44  jmcneill 		aprint_error_dev(self, "unable to evaluate _GLK: %s\n",
    349  1.44  jmcneill 		    AcpiFormatException(rv));
    350  1.44  jmcneill 		goto post_csr_map;
    351  1.44  jmcneill 	} else {
    352  1.44  jmcneill 		sc->sc_need_global_lock = false;
    353  1.33     kochi 	}
    354  1.44  jmcneill 	if (sc->sc_need_global_lock)
    355  1.44  jmcneill 		aprint_normal_dev(self, "using global ACPI lock\n");
    356  1.33     kochi 
    357  1.46     joerg 	callout_init(&sc->sc_pseudo_intr, CALLOUT_MPSAFE);
    358  1.46     joerg 	callout_setfunc(&sc->sc_pseudo_intr, acpiec_callout, self);
    359  1.46     joerg 
    360  1.44  jmcneill 	rv = AcpiInstallAddressSpaceHandler(sc->sc_ech, ACPI_ADR_SPACE_EC,
    361  1.44  jmcneill 	    acpiec_space_handler, acpiec_space_setup, self);
    362  1.44  jmcneill 	if (rv != AE_OK) {
    363  1.44  jmcneill 		aprint_error_dev(self,
    364  1.44  jmcneill 		    "unable to install address space handler: %s\n",
    365  1.44  jmcneill 		    AcpiFormatException(rv));
    366  1.44  jmcneill 		goto post_csr_map;
    367  1.33     kochi 	}
    368  1.33     kochi 
    369  1.44  jmcneill 	rv = AcpiInstallGpeHandler(sc->sc_gpeh, sc->sc_gpebit,
    370  1.44  jmcneill 	    ACPI_GPE_EDGE_TRIGGERED, acpiec_gpe_handler, self);
    371  1.44  jmcneill 	if (rv != AE_OK) {
    372  1.44  jmcneill 		aprint_error_dev(self, "unable to install GPE handler: %s\n",
    373  1.23     kochi 		    AcpiFormatException(rv));
    374  1.44  jmcneill 		goto post_csr_map;
    375  1.17   mycroft 	}
    376  1.23     kochi 
    377  1.69    jruoho 	rv = AcpiEnableGpe(sc->sc_gpeh, sc->sc_gpebit);
    378  1.44  jmcneill 	if (rv != AE_OK) {
    379  1.44  jmcneill 		aprint_error_dev(self, "unable to enable GPE: %s\n",
    380  1.44  jmcneill 		    AcpiFormatException(rv));
    381  1.44  jmcneill 		goto post_csr_map;
    382  1.44  jmcneill 	}
    383  1.23     kochi 
    384  1.44  jmcneill 	if (kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL, acpiec_gpe_query,
    385  1.44  jmcneill 		           self, NULL, "acpiec sci thread")) {
    386  1.44  jmcneill 		aprint_error_dev(self, "unable to create query kthread\n");
    387  1.44  jmcneill 		goto post_csr_map;
    388  1.44  jmcneill 	}
    389  1.23     kochi 
    390  1.44  jmcneill 	ec_singleton = self;
    391  1.23     kochi 
    392  1.56       alc 	if (!pmf_device_register1(self, acpiec_suspend, acpiec_resume,
    393  1.56       alc 	    acpiec_shutdown))
    394  1.44  jmcneill 		aprint_error_dev(self, "couldn't establish power handler\n");
    395  1.23     kochi 
    396  1.23     kochi 	return;
    397  1.44  jmcneill 
    398  1.44  jmcneill post_csr_map:
    399  1.44  jmcneill 	(void)AcpiRemoveGpeHandler(sc->sc_gpeh, sc->sc_gpebit,
    400  1.44  jmcneill 	    acpiec_gpe_handler);
    401  1.44  jmcneill 	(void)AcpiRemoveAddressSpaceHandler(sc->sc_ech,
    402  1.44  jmcneill 	    ACPI_ADR_SPACE_EC, acpiec_space_handler);
    403  1.44  jmcneill 	bus_space_unmap(sc->sc_csr_st, sc->sc_csr_sh, 1);
    404  1.44  jmcneill post_data_map:
    405  1.44  jmcneill 	bus_space_unmap(sc->sc_data_st, sc->sc_data_sh, 1);
    406  1.73  riastrad 	if (!pmf_device_register(self, NULL, NULL))
    407  1.73  riastrad 		aprint_error_dev(self, "couldn't establish power handler\n");
    408  1.17   mycroft }
    409  1.17   mycroft 
    410  1.44  jmcneill static bool
    411  1.60    dyoung acpiec_suspend(device_t dv, const pmf_qual_t *qual)
    412   1.1   thorpej {
    413  1.80  riastrad 
    414  1.44  jmcneill 	acpiec_cold = true;
    415   1.1   thorpej 
    416  1.44  jmcneill 	return true;
    417  1.44  jmcneill }
    418   1.1   thorpej 
    419  1.44  jmcneill static bool
    420  1.60    dyoung acpiec_resume(device_t dv, const pmf_qual_t *qual)
    421  1.44  jmcneill {
    422  1.80  riastrad 
    423  1.44  jmcneill 	acpiec_cold = false;
    424   1.1   thorpej 
    425  1.44  jmcneill 	return true;
    426  1.44  jmcneill }
    427  1.17   mycroft 
    428  1.44  jmcneill static bool
    429  1.56       alc acpiec_shutdown(device_t dv, int how)
    430  1.56       alc {
    431  1.56       alc 
    432  1.56       alc 	acpiec_cold = true;
    433  1.56       alc 	return true;
    434  1.56       alc }
    435  1.56       alc 
    436  1.56       alc static bool
    437  1.44  jmcneill acpiec_parse_gpe_package(device_t self, ACPI_HANDLE ec_handle,
    438  1.44  jmcneill     ACPI_HANDLE *gpe_handle, uint8_t *gpebit)
    439  1.44  jmcneill {
    440  1.44  jmcneill 	ACPI_BUFFER buf;
    441  1.44  jmcneill 	ACPI_OBJECT *p, *c;
    442  1.44  jmcneill 	ACPI_STATUS rv;
    443   1.1   thorpej 
    444  1.44  jmcneill 	rv = acpi_eval_struct(ec_handle, "_GPE", &buf);
    445  1.44  jmcneill 	if (rv != AE_OK) {
    446  1.44  jmcneill 		aprint_error_dev(self, "unable to evaluate _GPE: %s\n",
    447  1.44  jmcneill 		    AcpiFormatException(rv));
    448  1.44  jmcneill 		return false;
    449  1.44  jmcneill 	}
    450   1.1   thorpej 
    451  1.44  jmcneill 	p = buf.Pointer;
    452  1.23     kochi 
    453  1.44  jmcneill 	if (p->Type == ACPI_TYPE_INTEGER) {
    454  1.44  jmcneill 		*gpe_handle = NULL;
    455  1.44  jmcneill 		*gpebit = p->Integer.Value;
    456  1.57   mlelstv 		ACPI_FREE(p);
    457  1.44  jmcneill 		return true;
    458  1.44  jmcneill 	}
    459  1.23     kochi 
    460  1.44  jmcneill 	if (p->Type != ACPI_TYPE_PACKAGE) {
    461  1.44  jmcneill 		aprint_error_dev(self, "_GPE is neither integer nor package\n");
    462  1.57   mlelstv 		ACPI_FREE(p);
    463  1.44  jmcneill 		return false;
    464  1.44  jmcneill 	}
    465  1.80  riastrad 
    466  1.44  jmcneill 	if (p->Package.Count != 2) {
    467  1.80  riastrad 		aprint_error_dev(self,
    468  1.80  riastrad 		    "_GPE package does not contain 2 elements\n");
    469  1.57   mlelstv 		ACPI_FREE(p);
    470  1.44  jmcneill 		return false;
    471   1.1   thorpej 	}
    472   1.1   thorpej 
    473  1.44  jmcneill 	c = &p->Package.Elements[0];
    474  1.59    jruoho 	rv = acpi_eval_reference_handle(c, gpe_handle);
    475  1.59    jruoho 
    476  1.59    jruoho 	if (ACPI_FAILURE(rv)) {
    477  1.59    jruoho 		aprint_error_dev(self, "failed to evaluate _GPE handle\n");
    478  1.57   mlelstv 		ACPI_FREE(p);
    479  1.44  jmcneill 		return false;
    480  1.44  jmcneill 	}
    481  1.59    jruoho 
    482  1.44  jmcneill 	c = &p->Package.Elements[1];
    483  1.59    jruoho 
    484  1.44  jmcneill 	if (c->Type != ACPI_TYPE_INTEGER) {
    485  1.44  jmcneill 		aprint_error_dev(self,
    486  1.44  jmcneill 		    "_GPE package needs integer as 2nd field\n");
    487  1.57   mlelstv 		ACPI_FREE(p);
    488  1.44  jmcneill 		return false;
    489  1.44  jmcneill 	}
    490  1.44  jmcneill 	*gpebit = c->Integer.Value;
    491  1.57   mlelstv 	ACPI_FREE(p);
    492  1.44  jmcneill 	return true;
    493  1.44  jmcneill }
    494  1.23     kochi 
    495  1.44  jmcneill static uint8_t
    496  1.44  jmcneill acpiec_read_data(struct acpiec_softc *sc)
    497  1.44  jmcneill {
    498  1.44  jmcneill 	return bus_space_read_1(sc->sc_data_st, sc->sc_data_sh, 0);
    499  1.44  jmcneill }
    500  1.17   mycroft 
    501  1.44  jmcneill static void
    502  1.44  jmcneill acpiec_write_data(struct acpiec_softc *sc, uint8_t val)
    503  1.44  jmcneill {
    504  1.44  jmcneill 	bus_space_write_1(sc->sc_data_st, sc->sc_data_sh, 0, val);
    505  1.44  jmcneill }
    506   1.1   thorpej 
    507  1.44  jmcneill static uint8_t
    508  1.44  jmcneill acpiec_read_status(struct acpiec_softc *sc)
    509  1.44  jmcneill {
    510  1.44  jmcneill 	return bus_space_read_1(sc->sc_csr_st, sc->sc_csr_sh, 0);
    511  1.44  jmcneill }
    512  1.33     kochi 
    513  1.44  jmcneill static void
    514  1.44  jmcneill acpiec_write_command(struct acpiec_softc *sc, uint8_t cmd)
    515  1.44  jmcneill {
    516  1.44  jmcneill 	bus_space_write_1(sc->sc_csr_st, sc->sc_csr_sh, 0, cmd);
    517  1.44  jmcneill }
    518  1.33     kochi 
    519  1.44  jmcneill static ACPI_STATUS
    520  1.65    jruoho acpiec_space_setup(ACPI_HANDLE region, uint32_t func, void *arg,
    521  1.44  jmcneill     void **region_arg)
    522  1.44  jmcneill {
    523  1.80  riastrad 
    524  1.44  jmcneill 	if (func == ACPI_REGION_DEACTIVATE)
    525  1.44  jmcneill 		*region_arg = NULL;
    526  1.44  jmcneill 	else
    527  1.44  jmcneill 		*region_arg = arg;
    528   1.1   thorpej 
    529  1.44  jmcneill 	return AE_OK;
    530   1.1   thorpej }
    531   1.1   thorpej 
    532   1.1   thorpej static void
    533  1.44  jmcneill acpiec_lock(device_t dv)
    534   1.1   thorpej {
    535  1.44  jmcneill 	struct acpiec_softc *sc = device_private(dv);
    536  1.25     kochi 	ACPI_STATUS rv;
    537   1.1   thorpej 
    538  1.44  jmcneill 	mutex_enter(&sc->sc_access_mtx);
    539   1.1   thorpej 
    540  1.44  jmcneill 	if (sc->sc_need_global_lock) {
    541  1.80  riastrad 		rv = AcpiAcquireGlobalLock(EC_LOCK_TIMEOUT,
    542  1.80  riastrad 		    &sc->sc_global_lock);
    543  1.44  jmcneill 		if (rv != AE_OK) {
    544  1.80  riastrad 			aprint_error_dev(dv,
    545  1.80  riastrad 			    "failed to acquire global lock: %s\n",
    546  1.44  jmcneill 			    AcpiFormatException(rv));
    547  1.44  jmcneill 			return;
    548   1.1   thorpej 		}
    549  1.44  jmcneill 	}
    550  1.44  jmcneill }
    551  1.44  jmcneill 
    552  1.44  jmcneill static void
    553  1.44  jmcneill acpiec_unlock(device_t dv)
    554  1.44  jmcneill {
    555  1.44  jmcneill 	struct acpiec_softc *sc = device_private(dv);
    556  1.44  jmcneill 	ACPI_STATUS rv;
    557   1.1   thorpej 
    558  1.44  jmcneill 	if (sc->sc_need_global_lock) {
    559  1.44  jmcneill 		rv = AcpiReleaseGlobalLock(sc->sc_global_lock);
    560  1.44  jmcneill 		if (rv != AE_OK) {
    561  1.80  riastrad 			aprint_error_dev(dv,
    562  1.80  riastrad 			    "failed to release global lock: %s\n",
    563  1.25     kochi 			    AcpiFormatException(rv));
    564   1.1   thorpej 		}
    565   1.1   thorpej 	}
    566  1.44  jmcneill 	mutex_exit(&sc->sc_access_mtx);
    567  1.44  jmcneill }
    568  1.44  jmcneill 
    569  1.44  jmcneill static ACPI_STATUS
    570  1.44  jmcneill acpiec_read(device_t dv, uint8_t addr, uint8_t *val)
    571  1.44  jmcneill {
    572  1.44  jmcneill 	struct acpiec_softc *sc = device_private(dv);
    573  1.50  jmcneill 	int i, timeo = 1000 * EC_CMD_TIMEOUT;
    574   1.1   thorpej 
    575  1.44  jmcneill 	acpiec_lock(dv);
    576  1.44  jmcneill 	mutex_enter(&sc->sc_mtx);
    577   1.1   thorpej 
    578  1.44  jmcneill 	sc->sc_cur_addr = addr;
    579  1.44  jmcneill 	sc->sc_state = EC_STATE_READ;
    580   1.1   thorpej 
    581  1.44  jmcneill 	for (i = 0; i < EC_POLL_TIMEOUT; ++i) {
    582  1.45  jmcneill 		acpiec_gpe_state_machine(dv);
    583  1.44  jmcneill 		if (sc->sc_state == EC_STATE_FREE)
    584  1.44  jmcneill 			goto done;
    585  1.44  jmcneill 		delay(1);
    586  1.44  jmcneill 	}
    587   1.1   thorpej 
    588  1.44  jmcneill 	if (cold || acpiec_cold) {
    589  1.49  jmcneill 		while (sc->sc_state != EC_STATE_FREE && timeo-- > 0) {
    590  1.50  jmcneill 			delay(1000);
    591  1.45  jmcneill 			acpiec_gpe_state_machine(dv);
    592   1.1   thorpej 		}
    593  1.49  jmcneill 		if (sc->sc_state != EC_STATE_FREE) {
    594  1.49  jmcneill 			mutex_exit(&sc->sc_mtx);
    595  1.49  jmcneill 			acpiec_unlock(dv);
    596  1.49  jmcneill 			aprint_error_dev(dv, "command timed out, state %d\n",
    597  1.49  jmcneill 			    sc->sc_state);
    598  1.49  jmcneill 			return AE_ERROR;
    599  1.49  jmcneill 		}
    600  1.52     joerg 	} else if (cv_timedwait(&sc->sc_cv, &sc->sc_mtx, EC_CMD_TIMEOUT * hz)) {
    601  1.44  jmcneill 		mutex_exit(&sc->sc_mtx);
    602  1.44  jmcneill 		acpiec_unlock(dv);
    603  1.80  riastrad 		aprint_error_dev(dv,
    604  1.80  riastrad 		    "command takes over %d sec...\n", EC_CMD_TIMEOUT);
    605  1.44  jmcneill 		return AE_ERROR;
    606   1.1   thorpej 	}
    607  1.33     kochi 
    608  1.44  jmcneill done:
    609  1.44  jmcneill 	*val = sc->sc_cur_val;
    610  1.44  jmcneill 
    611  1.44  jmcneill 	mutex_exit(&sc->sc_mtx);
    612  1.44  jmcneill 	acpiec_unlock(dv);
    613  1.44  jmcneill 	return AE_OK;
    614   1.1   thorpej }
    615   1.1   thorpej 
    616   1.1   thorpej static ACPI_STATUS
    617  1.44  jmcneill acpiec_write(device_t dv, uint8_t addr, uint8_t val)
    618   1.1   thorpej {
    619  1.44  jmcneill 	struct acpiec_softc *sc = device_private(dv);
    620  1.50  jmcneill 	int i, timeo = 1000 * EC_CMD_TIMEOUT;
    621   1.1   thorpej 
    622  1.44  jmcneill 	acpiec_lock(dv);
    623  1.44  jmcneill 	mutex_enter(&sc->sc_mtx);
    624   1.1   thorpej 
    625  1.44  jmcneill 	sc->sc_cur_addr = addr;
    626  1.44  jmcneill 	sc->sc_cur_val = val;
    627  1.44  jmcneill 	sc->sc_state = EC_STATE_WRITE;
    628  1.44  jmcneill 
    629  1.44  jmcneill 	for (i = 0; i < EC_POLL_TIMEOUT; ++i) {
    630  1.45  jmcneill 		acpiec_gpe_state_machine(dv);
    631  1.44  jmcneill 		if (sc->sc_state == EC_STATE_FREE)
    632  1.44  jmcneill 			goto done;
    633  1.44  jmcneill 		delay(1);
    634  1.44  jmcneill 	}
    635  1.44  jmcneill 
    636  1.44  jmcneill 	if (cold || acpiec_cold) {
    637  1.49  jmcneill 		while (sc->sc_state != EC_STATE_FREE && timeo-- > 0) {
    638  1.50  jmcneill 			delay(1000);
    639  1.45  jmcneill 			acpiec_gpe_state_machine(dv);
    640  1.44  jmcneill 		}
    641  1.49  jmcneill 		if (sc->sc_state != EC_STATE_FREE) {
    642  1.49  jmcneill 			mutex_exit(&sc->sc_mtx);
    643  1.49  jmcneill 			acpiec_unlock(dv);
    644  1.49  jmcneill 			aprint_error_dev(dv, "command timed out, state %d\n",
    645  1.49  jmcneill 			    sc->sc_state);
    646  1.49  jmcneill 			return AE_ERROR;
    647  1.49  jmcneill 		}
    648  1.52     joerg 	} else if (cv_timedwait(&sc->sc_cv, &sc->sc_mtx, EC_CMD_TIMEOUT * hz)) {
    649  1.44  jmcneill 		mutex_exit(&sc->sc_mtx);
    650  1.44  jmcneill 		acpiec_unlock(dv);
    651  1.80  riastrad 		aprint_error_dev(dv,
    652  1.80  riastrad 		    "command takes over %d sec...\n", EC_CMD_TIMEOUT);
    653  1.44  jmcneill 		return AE_ERROR;
    654  1.44  jmcneill 	}
    655   1.1   thorpej 
    656  1.44  jmcneill done:
    657  1.44  jmcneill 	mutex_exit(&sc->sc_mtx);
    658  1.44  jmcneill 	acpiec_unlock(dv);
    659  1.44  jmcneill 	return AE_OK;
    660   1.1   thorpej }
    661   1.1   thorpej 
    662  1.86  riastrad /*
    663  1.86  riastrad  * acpiec_space_handler(func, paddr, bitwidth, value, arg, region_arg)
    664  1.86  riastrad  *
    665  1.86  riastrad  *	Transfer bitwidth/8 bytes of data between paddr and *value:
    666  1.86  riastrad  *	from paddr to *value when func is ACPI_READ, and the other way
    667  1.86  riastrad  *	when func is ACPI_WRITE.  arg is the acpiec(4) or acpiecdt(4)
    668  1.86  riastrad  *	device.  region_arg is ignored (XXX why? determined by
    669  1.86  riastrad  *	acpiec_space_setup but never used by anything that I can see).
    670  1.86  riastrad  *
    671  1.86  riastrad  *	The caller always provides storage at *value large enough for
    672  1.86  riastrad  *	an ACPI_INTEGER object, i.e., a 64-bit integer.  However,
    673  1.86  riastrad  *	bitwidth may be larger; in this case the caller provides larger
    674  1.86  riastrad  *	storage at *value, e.g. 128 bits as documented in
    675  1.86  riastrad  *	<https://gnats.netbsd.org/55206>.
    676  1.86  riastrad  *
    677  1.86  riastrad  *	On reads, this fully initializes one ACPI_INTEGER's worth of
    678  1.86  riastrad  *	data at *value, even if bitwidth < 64.  The integer is
    679  1.86  riastrad  *	interpreted in host byte order; in other words, bytes of data
    680  1.86  riastrad  *	are transferred in order between paddr and (uint8_t *)value.
    681  1.86  riastrad  *	The transfer is not atomic; it may go byte-by-byte.
    682  1.86  riastrad  *
    683  1.86  riastrad  *	XXX This only really makes sense on little-endian systems.
    684  1.86  riastrad  *	E.g., thinkpad_acpi.c assumes that a single byte is transferred
    685  1.86  riastrad  *	in the low-order bits of the result.  A big-endian system could
    686  1.86  riastrad  *	read a 64-bit integer in big-endian (and it did for a while!),
    687  1.86  riastrad  *	but what should it do for larger reads?  Unclear!
    688  1.86  riastrad  *
    689  1.86  riastrad  *	XXX It's not clear whether the object at *value is always
    690  1.86  riastrad  *	_aligned_ adequately for an ACPI_INTEGER object.  Currently it
    691  1.86  riastrad  *	always is as long as malloc, used by AcpiOsAllocate, returns
    692  1.86  riastrad  *	64-bit-aligned data.
    693  1.86  riastrad  */
    694   1.1   thorpej static ACPI_STATUS
    695  1.65    jruoho acpiec_space_handler(uint32_t func, ACPI_PHYSICAL_ADDRESS paddr,
    696  1.65    jruoho     uint32_t width, ACPI_INTEGER *value, void *arg, void *region_arg)
    697   1.1   thorpej {
    698  1.79  riastrad 	device_t dv;
    699  1.44  jmcneill 	ACPI_STATUS rv;
    700  1.82  jmcneill 	uint8_t addr, *buf;
    701  1.79  riastrad 	unsigned int i;
    702  1.44  jmcneill 
    703  1.82  jmcneill 	if (paddr > 0xff || width % 8 != 0 ||
    704  1.81  riastrad 	    value == NULL || arg == NULL || paddr + width / 8 > 0x100)
    705  1.44  jmcneill 		return AE_BAD_PARAMETER;
    706   1.1   thorpej 
    707  1.44  jmcneill 	addr = paddr;
    708  1.79  riastrad 	dv = arg;
    709  1.82  jmcneill 	buf = (uint8_t *)value;
    710   1.1   thorpej 
    711  1.44  jmcneill 	rv = AE_OK;
    712   1.1   thorpej 
    713  1.79  riastrad 	switch (func) {
    714  1.79  riastrad 	case ACPI_READ:
    715  1.82  jmcneill 		for (i = 0; i < width; i += 8, ++addr, ++buf) {
    716  1.82  jmcneill 			rv = acpiec_read(dv, addr, buf);
    717  1.79  riastrad 			if (rv != AE_OK)
    718  1.79  riastrad 				break;
    719  1.79  riastrad 		}
    720  1.86  riastrad 		/*
    721  1.86  riastrad 		 * Make sure to fully initialize at least an
    722  1.86  riastrad 		 * ACPI_INTEGER-sized object.
    723  1.86  riastrad 		 */
    724  1.86  riastrad 		for (; i < sizeof(*value)*8; i += 8, ++buf)
    725  1.86  riastrad 			*buf = 0;
    726  1.79  riastrad 		break;
    727  1.79  riastrad 	case ACPI_WRITE:
    728  1.82  jmcneill 		for (i = 0; i < width; i += 8, ++addr, ++buf) {
    729  1.82  jmcneill 			rv = acpiec_write(dv, addr, *buf);
    730  1.79  riastrad 			if (rv != AE_OK)
    731  1.79  riastrad 				break;
    732  1.78  riastrad 		}
    733  1.79  riastrad 		break;
    734  1.79  riastrad 	default:
    735  1.79  riastrad 		aprint_error("%s: invalid Address Space function called: %x\n",
    736  1.79  riastrad 		    device_xname(dv), (unsigned int)func);
    737  1.79  riastrad 		return AE_BAD_PARAMETER;
    738  1.79  riastrad 	}
    739   1.1   thorpej 
    740  1.44  jmcneill 	return rv;
    741   1.1   thorpej }
    742   1.1   thorpej 
    743  1.44  jmcneill static void
    744  1.44  jmcneill acpiec_gpe_query(void *arg)
    745   1.1   thorpej {
    746  1.44  jmcneill 	device_t dv = arg;
    747  1.44  jmcneill 	struct acpiec_softc *sc = device_private(dv);
    748  1.44  jmcneill 	uint8_t reg;
    749  1.44  jmcneill 	char qxx[5];
    750  1.44  jmcneill 	ACPI_STATUS rv;
    751   1.1   thorpej 	int i;
    752   1.1   thorpej 
    753  1.44  jmcneill loop:
    754  1.44  jmcneill 	mutex_enter(&sc->sc_mtx);
    755  1.44  jmcneill 
    756  1.44  jmcneill 	if (sc->sc_got_sci == false)
    757  1.44  jmcneill 		cv_wait(&sc->sc_cv_sci, &sc->sc_mtx);
    758  1.44  jmcneill 	mutex_exit(&sc->sc_mtx);
    759  1.44  jmcneill 
    760  1.44  jmcneill 	acpiec_lock(dv);
    761  1.44  jmcneill 	mutex_enter(&sc->sc_mtx);
    762  1.44  jmcneill 
    763  1.44  jmcneill 	/* The Query command can always be issued, so be defensive here. */
    764  1.44  jmcneill 	sc->sc_got_sci = false;
    765  1.44  jmcneill 	sc->sc_state = EC_STATE_QUERY;
    766   1.1   thorpej 
    767  1.44  jmcneill 	for (i = 0; i < EC_POLL_TIMEOUT; ++i) {
    768  1.45  jmcneill 		acpiec_gpe_state_machine(dv);
    769  1.44  jmcneill 		if (sc->sc_state == EC_STATE_FREE)
    770  1.44  jmcneill 			goto done;
    771  1.44  jmcneill 		delay(1);
    772   1.1   thorpej 	}
    773   1.1   thorpej 
    774  1.44  jmcneill 	cv_wait(&sc->sc_cv, &sc->sc_mtx);
    775  1.44  jmcneill 
    776  1.44  jmcneill done:
    777  1.44  jmcneill 	reg = sc->sc_cur_val;
    778   1.1   thorpej 
    779  1.44  jmcneill 	mutex_exit(&sc->sc_mtx);
    780  1.44  jmcneill 	acpiec_unlock(dv);
    781   1.1   thorpej 
    782  1.44  jmcneill 	if (reg == 0)
    783  1.44  jmcneill 		goto loop; /* Spurious query result */
    784   1.1   thorpej 
    785   1.1   thorpej 	/*
    786  1.44  jmcneill 	 * Evaluate _Qxx to respond to the controller.
    787   1.1   thorpej 	 */
    788  1.44  jmcneill 	snprintf(qxx, sizeof(qxx), "_Q%02X", (unsigned int)reg);
    789  1.44  jmcneill 	rv = AcpiEvaluateObject(sc->sc_ech, qxx, NULL, NULL);
    790  1.44  jmcneill 	if (rv != AE_OK && rv != AE_NOT_FOUND) {
    791  1.68    cegger 		aprint_error_dev(dv, "GPE query method %s failed: %s",
    792  1.68    cegger 		    qxx, AcpiFormatException(rv));
    793   1.1   thorpej 	}
    794   1.1   thorpej 
    795  1.44  jmcneill 	goto loop;
    796  1.24     kochi }
    797   1.1   thorpej 
    798  1.44  jmcneill static void
    799  1.45  jmcneill acpiec_gpe_state_machine(device_t dv)
    800   1.1   thorpej {
    801  1.44  jmcneill 	struct acpiec_softc *sc = device_private(dv);
    802  1.44  jmcneill 	uint8_t reg;
    803   1.1   thorpej 
    804  1.44  jmcneill 	reg = acpiec_read_status(sc);
    805   1.1   thorpej 
    806  1.44  jmcneill 	if (reg & EC_STATUS_SCI)
    807  1.44  jmcneill 		sc->sc_got_sci = true;
    808  1.44  jmcneill 
    809  1.44  jmcneill 	switch (sc->sc_state) {
    810  1.44  jmcneill 	case EC_STATE_QUERY:
    811  1.44  jmcneill 		if ((reg & EC_STATUS_IBF) != 0)
    812  1.44  jmcneill 			break; /* Nothing of interest here. */
    813  1.44  jmcneill 		acpiec_write_command(sc, EC_COMMAND_QUERY);
    814  1.44  jmcneill 		sc->sc_state = EC_STATE_QUERY_VAL;
    815  1.44  jmcneill 		break;
    816   1.1   thorpej 
    817  1.44  jmcneill 	case EC_STATE_QUERY_VAL:
    818  1.44  jmcneill 		if ((reg & EC_STATUS_OBF) == 0)
    819  1.44  jmcneill 			break; /* Nothing of interest here. */
    820   1.1   thorpej 
    821  1.44  jmcneill 		sc->sc_cur_val = acpiec_read_data(sc);
    822  1.44  jmcneill 		sc->sc_state = EC_STATE_FREE;
    823   1.1   thorpej 
    824  1.44  jmcneill 		cv_signal(&sc->sc_cv);
    825   1.1   thorpej 		break;
    826   1.1   thorpej 
    827  1.44  jmcneill 	case EC_STATE_READ:
    828  1.44  jmcneill 		if ((reg & EC_STATUS_IBF) != 0)
    829  1.44  jmcneill 			break; /* Nothing of interest here. */
    830   1.1   thorpej 
    831  1.44  jmcneill 		acpiec_write_command(sc, EC_COMMAND_READ);
    832  1.44  jmcneill 		sc->sc_state = EC_STATE_READ_ADDR;
    833   1.1   thorpej 		break;
    834   1.1   thorpej 
    835  1.44  jmcneill 	case EC_STATE_READ_ADDR:
    836  1.44  jmcneill 		if ((reg & EC_STATUS_IBF) != 0)
    837  1.44  jmcneill 			break; /* Nothing of interest here. */
    838   1.1   thorpej 
    839  1.44  jmcneill 		acpiec_write_data(sc, sc->sc_cur_addr);
    840  1.44  jmcneill 		sc->sc_state = EC_STATE_READ_VAL;
    841  1.44  jmcneill 		break;
    842   1.1   thorpej 
    843  1.44  jmcneill 	case EC_STATE_READ_VAL:
    844  1.44  jmcneill 		if ((reg & EC_STATUS_OBF) == 0)
    845  1.44  jmcneill 			break; /* Nothing of interest here. */
    846  1.44  jmcneill 		sc->sc_cur_val = acpiec_read_data(sc);
    847  1.44  jmcneill 		sc->sc_state = EC_STATE_FREE;
    848   1.1   thorpej 
    849  1.44  jmcneill 		cv_signal(&sc->sc_cv);
    850  1.44  jmcneill 		break;
    851   1.1   thorpej 
    852  1.44  jmcneill 	case EC_STATE_WRITE:
    853  1.44  jmcneill 		if ((reg & EC_STATUS_IBF) != 0)
    854  1.44  jmcneill 			break; /* Nothing of interest here. */
    855   1.1   thorpej 
    856  1.44  jmcneill 		acpiec_write_command(sc, EC_COMMAND_WRITE);
    857  1.44  jmcneill 		sc->sc_state = EC_STATE_WRITE_ADDR;
    858  1.44  jmcneill 		break;
    859   1.1   thorpej 
    860  1.44  jmcneill 	case EC_STATE_WRITE_ADDR:
    861  1.44  jmcneill 		if ((reg & EC_STATUS_IBF) != 0)
    862  1.44  jmcneill 			break; /* Nothing of interest here. */
    863  1.44  jmcneill 		acpiec_write_data(sc, sc->sc_cur_addr);
    864  1.44  jmcneill 		sc->sc_state = EC_STATE_WRITE_VAL;
    865  1.44  jmcneill 		break;
    866   1.1   thorpej 
    867  1.44  jmcneill 	case EC_STATE_WRITE_VAL:
    868  1.44  jmcneill 		if ((reg & EC_STATUS_IBF) != 0)
    869  1.44  jmcneill 			break; /* Nothing of interest here. */
    870  1.44  jmcneill 		sc->sc_state = EC_STATE_FREE;
    871  1.44  jmcneill 		cv_signal(&sc->sc_cv);
    872   1.1   thorpej 
    873  1.44  jmcneill 		acpiec_write_data(sc, sc->sc_cur_val);
    874  1.44  jmcneill 		break;
    875   1.1   thorpej 
    876  1.44  jmcneill 	case EC_STATE_FREE:
    877  1.44  jmcneill 		if (sc->sc_got_sci)
    878  1.44  jmcneill 			cv_signal(&sc->sc_cv_sci);
    879  1.44  jmcneill 		break;
    880  1.44  jmcneill 	default:
    881  1.44  jmcneill 		panic("invalid state");
    882  1.44  jmcneill 	}
    883  1.46     joerg 
    884  1.46     joerg 	if (sc->sc_state != EC_STATE_FREE)
    885  1.46     joerg 		callout_schedule(&sc->sc_pseudo_intr, 1);
    886  1.46     joerg }
    887  1.46     joerg 
    888  1.46     joerg static void
    889  1.46     joerg acpiec_callout(void *arg)
    890  1.46     joerg {
    891  1.46     joerg 	device_t dv = arg;
    892  1.46     joerg 	struct acpiec_softc *sc = device_private(dv);
    893  1.46     joerg 
    894  1.46     joerg 	mutex_enter(&sc->sc_mtx);
    895  1.46     joerg 	acpiec_gpe_state_machine(dv);
    896  1.46     joerg 	mutex_exit(&sc->sc_mtx);
    897  1.24     kochi }
    898   1.1   thorpej 
    899  1.65    jruoho static uint32_t
    900  1.69    jruoho acpiec_gpe_handler(ACPI_HANDLE hdl, uint32_t gpebit, void *arg)
    901   1.1   thorpej {
    902  1.44  jmcneill 	device_t dv = arg;
    903  1.44  jmcneill 	struct acpiec_softc *sc = device_private(dv);
    904   1.1   thorpej 
    905  1.44  jmcneill 	mutex_enter(&sc->sc_mtx);
    906  1.45  jmcneill 	acpiec_gpe_state_machine(dv);
    907  1.44  jmcneill 	mutex_exit(&sc->sc_mtx);
    908   1.1   thorpej 
    909  1.70    jruoho 	return ACPI_INTERRUPT_HANDLED | ACPI_REENABLE_GPE;
    910   1.1   thorpej }
    911  1.48  jmcneill 
    912  1.48  jmcneill ACPI_STATUS
    913  1.48  jmcneill acpiec_bus_read(device_t dv, u_int addr, ACPI_INTEGER *val, int width)
    914  1.48  jmcneill {
    915  1.48  jmcneill 	return acpiec_space_handler(ACPI_READ, addr, width * 8, val, dv, NULL);
    916  1.48  jmcneill }
    917  1.48  jmcneill 
    918  1.48  jmcneill ACPI_STATUS
    919  1.48  jmcneill acpiec_bus_write(device_t dv, u_int addr, ACPI_INTEGER val, int width)
    920  1.48  jmcneill {
    921  1.80  riastrad 	return acpiec_space_handler(ACPI_WRITE, addr, width * 8, &val, dv,
    922  1.80  riastrad 	    NULL);
    923  1.48  jmcneill }
    924  1.48  jmcneill 
    925  1.48  jmcneill ACPI_HANDLE
    926  1.48  jmcneill acpiec_get_handle(device_t dv)
    927  1.48  jmcneill {
    928  1.48  jmcneill 	struct acpiec_softc *sc = device_private(dv);
    929  1.48  jmcneill 
    930  1.48  jmcneill 	return sc->sc_ech;
    931  1.48  jmcneill }
    932