Home | History | Annotate | Line # | Download | only in acpi
acpi_ec.c revision 1.88
      1  1.88  riastrad /*	$NetBSD: acpi_ec.c,v 1.88 2023/07/18 10:02:25 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.88  riastrad __KERNEL_RCSID(0, "$NetBSD: acpi_ec.c,v 1.88 2023/07/18 10:02:25 riastradh Exp $");
     63  1.88  riastrad 
     64  1.88  riastrad #ifdef _KERNEL_OPT
     65  1.88  riastrad #include "opt_acpi_ec.h"
     66  1.88  riastrad #endif
     67   1.1   thorpej 
     68   1.1   thorpej #include <sys/param.h>
     69  1.62    jruoho #include <sys/callout.h>
     70  1.44  jmcneill #include <sys/condvar.h>
     71   1.1   thorpej #include <sys/device.h>
     72   1.1   thorpej #include <sys/kernel.h>
     73  1.44  jmcneill #include <sys/kthread.h>
     74  1.44  jmcneill #include <sys/mutex.h>
     75  1.62    jruoho #include <sys/systm.h>
     76   1.1   thorpej 
     77  1.57   mlelstv #include <dev/acpi/acpireg.h>
     78   1.1   thorpej #include <dev/acpi/acpivar.h>
     79  1.48  jmcneill #include <dev/acpi/acpi_ecvar.h>
     80   1.1   thorpej 
     81  1.57   mlelstv #define _COMPONENT          ACPI_EC_COMPONENT
     82  1.57   mlelstv ACPI_MODULE_NAME            ("acpi_ec")
     83  1.57   mlelstv 
     84  1.44  jmcneill /* Maximum time to wait for global ACPI lock in ms */
     85  1.44  jmcneill #define	EC_LOCK_TIMEOUT		5
     86  1.23     kochi 
     87  1.44  jmcneill /* Maximum time to poll for completion of a command  in ms */
     88  1.44  jmcneill #define	EC_POLL_TIMEOUT		5
     89   1.1   thorpej 
     90  1.46     joerg /* Maximum time to give a single EC command in s */
     91  1.46     joerg #define EC_CMD_TIMEOUT		10
     92  1.46     joerg 
     93  1.44  jmcneill /* From ACPI 3.0b, chapter 12.3 */
     94  1.44  jmcneill #define EC_COMMAND_READ		0x80
     95  1.44  jmcneill #define	EC_COMMAND_WRITE	0x81
     96  1.44  jmcneill #define	EC_COMMAND_BURST_EN	0x82
     97  1.44  jmcneill #define	EC_COMMAND_BURST_DIS	0x83
     98  1.44  jmcneill #define	EC_COMMAND_QUERY	0x84
     99  1.44  jmcneill 
    100  1.44  jmcneill /* From ACPI 3.0b, chapter 12.2.1 */
    101  1.44  jmcneill #define	EC_STATUS_OBF		0x01
    102  1.44  jmcneill #define	EC_STATUS_IBF		0x02
    103  1.44  jmcneill #define	EC_STATUS_CMD		0x08
    104  1.44  jmcneill #define	EC_STATUS_BURST		0x10
    105  1.44  jmcneill #define	EC_STATUS_SCI		0x20
    106  1.44  jmcneill #define	EC_STATUS_SMI		0x40
    107   1.1   thorpej 
    108  1.88  riastrad #define	EC_STATUS_FMT							      \
    109  1.88  riastrad 	"\x10\10IGN7\7SMI\6SCI\5BURST\4CMD\3IGN2\2IBF\1OBF"
    110  1.88  riastrad 
    111  1.85   thorpej static const struct device_compatible_entry compat_data[] = {
    112  1.85   thorpej 	{ .compat = "PNP0C09" },
    113  1.85   thorpej 	DEVICE_COMPAT_EOL
    114  1.44  jmcneill };
    115  1.44  jmcneill 
    116  1.88  riastrad #define	EC_STATE_ENUM(F)						      \
    117  1.88  riastrad 	F(EC_STATE_QUERY, "QUERY")					      \
    118  1.88  riastrad 	F(EC_STATE_QUERY_VAL, "QUERY_VAL")				      \
    119  1.88  riastrad 	F(EC_STATE_READ, "READ")					      \
    120  1.88  riastrad 	F(EC_STATE_READ_ADDR, "READ_ADDR")				      \
    121  1.88  riastrad 	F(EC_STATE_READ_VAL, "READ_VAL")				      \
    122  1.88  riastrad 	F(EC_STATE_WRITE, "WRITE")					      \
    123  1.88  riastrad 	F(EC_STATE_WRITE_ADDR, "WRITE_ADDR")				      \
    124  1.88  riastrad 	F(EC_STATE_WRITE_VAL, "WRITE_VAL")				      \
    125  1.88  riastrad 	F(EC_STATE_FREE, "FREE")					      \
    126  1.88  riastrad 
    127  1.44  jmcneill enum ec_state_t {
    128  1.88  riastrad #define	F(N, S)	N,
    129  1.88  riastrad 	EC_STATE_ENUM(F)
    130  1.88  riastrad #undef F
    131  1.88  riastrad };
    132  1.88  riastrad 
    133  1.88  riastrad #ifdef ACPIEC_DEBUG
    134  1.88  riastrad static const char *const acpiec_state_names[] = {
    135  1.88  riastrad #define F(N, S)	[N] = S,
    136  1.88  riastrad 	EC_STATE_ENUM(F)
    137  1.88  riastrad #undef F
    138  1.44  jmcneill };
    139  1.88  riastrad #endif
    140  1.44  jmcneill 
    141  1.44  jmcneill struct acpiec_softc {
    142  1.87  riastrad 	device_t sc_dev;
    143  1.87  riastrad 
    144  1.44  jmcneill 	ACPI_HANDLE sc_ech;
    145   1.1   thorpej 
    146  1.44  jmcneill 	ACPI_HANDLE sc_gpeh;
    147  1.65    jruoho 	uint8_t sc_gpebit;
    148   1.1   thorpej 
    149  1.44  jmcneill 	bus_space_tag_t sc_data_st;
    150  1.44  jmcneill 	bus_space_handle_t sc_data_sh;
    151   1.1   thorpej 
    152  1.44  jmcneill 	bus_space_tag_t sc_csr_st;
    153  1.44  jmcneill 	bus_space_handle_t sc_csr_sh;
    154   1.1   thorpej 
    155  1.44  jmcneill 	bool sc_need_global_lock;
    156  1.65    jruoho 	uint32_t sc_global_lock;
    157  1.17   mycroft 
    158  1.44  jmcneill 	kmutex_t sc_mtx, sc_access_mtx;
    159  1.44  jmcneill 	kcondvar_t sc_cv, sc_cv_sci;
    160  1.44  jmcneill 	enum ec_state_t sc_state;
    161  1.44  jmcneill 	bool sc_got_sci;
    162  1.46     joerg 	callout_t sc_pseudo_intr;
    163  1.44  jmcneill 
    164  1.44  jmcneill 	uint8_t sc_cur_addr, sc_cur_val;
    165  1.23     kochi };
    166   1.1   thorpej 
    167  1.88  riastrad #ifdef ACPIEC_DEBUG
    168  1.88  riastrad 
    169  1.88  riastrad #define	ACPIEC_DEBUG_ENUM(F)						      \
    170  1.88  riastrad 	F(ACPIEC_DEBUG_REG, "REG")					      \
    171  1.88  riastrad 	F(ACPIEC_DEBUG_RW, "RW")					      \
    172  1.88  riastrad 	F(ACPIEC_DEBUG_QUERY, "QUERY")					      \
    173  1.88  riastrad 	F(ACPIEC_DEBUG_TRANSITION, "TRANSITION")			      \
    174  1.88  riastrad 	F(ACPIEC_DEBUG_INTR, "INTR")					      \
    175  1.88  riastrad 
    176  1.88  riastrad enum {
    177  1.88  riastrad #define	F(N, S)	N,
    178  1.88  riastrad 	ACPIEC_DEBUG_ENUM(F)
    179  1.88  riastrad #undef F
    180  1.88  riastrad };
    181  1.88  riastrad 
    182  1.88  riastrad static const char *const acpiec_debug_names[] = {
    183  1.88  riastrad #define	F(N, S)	[N] = S,
    184  1.88  riastrad 	ACPIEC_DEBUG_ENUM(F)
    185  1.88  riastrad #undef F
    186  1.88  riastrad };
    187  1.88  riastrad 
    188  1.88  riastrad int acpiec_debug = ACPIEC_DEBUG;
    189  1.88  riastrad 
    190  1.88  riastrad #define	DPRINTF(n, sc, fmt, ...) do					      \
    191  1.88  riastrad {									      \
    192  1.88  riastrad 	if (acpiec_debug & __BIT(n)) {					      \
    193  1.88  riastrad 		char dprintbuf[16];					      \
    194  1.88  riastrad 		const char *state;					      \
    195  1.88  riastrad 									      \
    196  1.88  riastrad 		/* paranoia */						      \
    197  1.88  riastrad 		if ((sc)->sc_state < __arraycount(acpiec_state_names)) {      \
    198  1.88  riastrad 			state = acpiec_state_names[(sc)->sc_state];	      \
    199  1.88  riastrad 		} else {						      \
    200  1.88  riastrad 			snprintf(dprintbuf, sizeof(dprintbuf), "0x%x",	      \
    201  1.88  riastrad 			    (sc)->sc_state);				      \
    202  1.88  riastrad 			state = dprintbuf;				      \
    203  1.88  riastrad 		}							      \
    204  1.88  riastrad 									      \
    205  1.88  riastrad 		device_printf((sc)->sc_dev, "(%s) [%s] "fmt,		      \
    206  1.88  riastrad 		    acpiec_debug_names[n], state, ##__VA_ARGS__);	      \
    207  1.88  riastrad 	}								      \
    208  1.88  riastrad } while (0)
    209  1.88  riastrad 
    210  1.88  riastrad #else
    211  1.88  riastrad 
    212  1.88  riastrad #define	DPRINTF(n, sc, fmt, ...)	__nothing
    213  1.88  riastrad 
    214  1.88  riastrad #endif
    215  1.88  riastrad 
    216  1.55    cegger static int acpiecdt_match(device_t, cfdata_t, void *);
    217  1.44  jmcneill static void acpiecdt_attach(device_t, device_t, void *);
    218  1.44  jmcneill 
    219  1.55    cegger static int acpiec_match(device_t, cfdata_t, void *);
    220  1.44  jmcneill static void acpiec_attach(device_t, device_t, void *);
    221  1.16     kochi 
    222  1.44  jmcneill static void acpiec_common_attach(device_t, device_t, ACPI_HANDLE,
    223  1.63    dyoung     bus_space_tag_t, bus_addr_t, bus_space_tag_t, bus_addr_t,
    224  1.63    dyoung     ACPI_HANDLE, uint8_t);
    225   1.1   thorpej 
    226  1.60    dyoung static bool acpiec_suspend(device_t, const pmf_qual_t *);
    227  1.60    dyoung static bool acpiec_resume(device_t, const pmf_qual_t *);
    228  1.56       alc static bool acpiec_shutdown(device_t, int);
    229  1.17   mycroft 
    230  1.44  jmcneill static bool acpiec_parse_gpe_package(device_t, ACPI_HANDLE,
    231  1.44  jmcneill     ACPI_HANDLE *, uint8_t *);
    232  1.20      yamt 
    233  1.46     joerg static void acpiec_callout(void *);
    234  1.44  jmcneill static void acpiec_gpe_query(void *);
    235  1.69    jruoho static uint32_t acpiec_gpe_handler(ACPI_HANDLE, uint32_t, void *);
    236  1.65    jruoho static ACPI_STATUS acpiec_space_setup(ACPI_HANDLE, uint32_t, void *, void **);
    237  1.65    jruoho static ACPI_STATUS acpiec_space_handler(uint32_t, ACPI_PHYSICAL_ADDRESS,
    238  1.65    jruoho     uint32_t, ACPI_INTEGER *, void *, void *);
    239  1.20      yamt 
    240  1.45  jmcneill static void acpiec_gpe_state_machine(device_t);
    241  1.20      yamt 
    242  1.44  jmcneill CFATTACH_DECL_NEW(acpiec, sizeof(struct acpiec_softc),
    243  1.20      yamt     acpiec_match, acpiec_attach, NULL, NULL);
    244  1.20      yamt 
    245  1.44  jmcneill CFATTACH_DECL_NEW(acpiecdt, sizeof(struct acpiec_softc),
    246  1.44  jmcneill     acpiecdt_match, acpiecdt_attach, NULL, NULL);
    247  1.44  jmcneill 
    248  1.44  jmcneill static device_t ec_singleton = NULL;
    249  1.44  jmcneill static bool acpiec_cold = false;
    250  1.23     kochi 
    251  1.44  jmcneill static bool
    252  1.44  jmcneill acpiecdt_find(device_t parent, ACPI_HANDLE *ec_handle,
    253  1.44  jmcneill     bus_addr_t *cmd_reg, bus_addr_t *data_reg, uint8_t *gpebit)
    254   1.9  tshiozak {
    255  1.44  jmcneill 	ACPI_TABLE_ECDT *ecdt;
    256  1.44  jmcneill 	ACPI_STATUS rv;
    257  1.44  jmcneill 
    258  1.44  jmcneill 	rv = AcpiGetTable(ACPI_SIG_ECDT, 1, (ACPI_TABLE_HEADER **)&ecdt);
    259  1.44  jmcneill 	if (ACPI_FAILURE(rv))
    260  1.44  jmcneill 		return false;
    261  1.44  jmcneill 
    262  1.44  jmcneill 	if (ecdt->Control.BitWidth != 8 || ecdt->Data.BitWidth != 8) {
    263  1.44  jmcneill 		aprint_error_dev(parent,
    264  1.61    jruoho 		    "ECDT register width invalid (%u/%u)\n",
    265  1.44  jmcneill 		    ecdt->Control.BitWidth, ecdt->Data.BitWidth);
    266  1.44  jmcneill 		return false;
    267  1.44  jmcneill 	}
    268  1.24     kochi 
    269  1.44  jmcneill 	rv = AcpiGetHandle(ACPI_ROOT_OBJECT, ecdt->Id, ec_handle);
    270  1.44  jmcneill 	if (ACPI_FAILURE(rv)) {
    271  1.44  jmcneill 		aprint_error_dev(parent,
    272  1.44  jmcneill 		    "failed to look up EC object %s: %s\n",
    273  1.44  jmcneill 		    ecdt->Id, AcpiFormatException(rv));
    274  1.44  jmcneill 		return false;
    275  1.44  jmcneill 	}
    276  1.44  jmcneill 
    277  1.44  jmcneill 	*cmd_reg = ecdt->Control.Address;
    278  1.44  jmcneill 	*data_reg = ecdt->Data.Address;
    279  1.44  jmcneill 	*gpebit = ecdt->Gpe;
    280  1.44  jmcneill 
    281  1.44  jmcneill 	return true;
    282   1.9  tshiozak }
    283   1.4   thorpej 
    284  1.44  jmcneill static int
    285  1.55    cegger acpiecdt_match(device_t parent, cfdata_t match, void *aux)
    286   1.1   thorpej {
    287  1.44  jmcneill 	ACPI_HANDLE ec_handle;
    288  1.44  jmcneill 	bus_addr_t cmd_reg, data_reg;
    289  1.44  jmcneill 	uint8_t gpebit;
    290   1.1   thorpej 
    291  1.44  jmcneill 	if (acpiecdt_find(parent, &ec_handle, &cmd_reg, &data_reg, &gpebit))
    292  1.44  jmcneill 		return 1;
    293  1.44  jmcneill 	else
    294  1.44  jmcneill 		return 0;
    295   1.1   thorpej }
    296   1.1   thorpej 
    297  1.44  jmcneill static void
    298  1.44  jmcneill acpiecdt_attach(device_t parent, device_t self, void *aux)
    299   1.1   thorpej {
    300  1.64    dyoung 	struct acpibus_attach_args *aa = aux;
    301  1.44  jmcneill 	ACPI_HANDLE ec_handle;
    302  1.44  jmcneill 	bus_addr_t cmd_reg, data_reg;
    303  1.44  jmcneill 	uint8_t gpebit;
    304  1.44  jmcneill 
    305  1.44  jmcneill 	if (!acpiecdt_find(parent, &ec_handle, &cmd_reg, &data_reg, &gpebit))
    306  1.44  jmcneill 		panic("ECDT disappeared");
    307  1.44  jmcneill 
    308  1.53  jmcneill 	aprint_naive("\n");
    309  1.44  jmcneill 	aprint_normal(": ACPI Embedded Controller via ECDT\n");
    310  1.20      yamt 
    311  1.63    dyoung 	acpiec_common_attach(parent, self, ec_handle, aa->aa_iot, cmd_reg,
    312  1.63    dyoung 	    aa->aa_iot, data_reg, NULL, gpebit);
    313   1.1   thorpej }
    314   1.1   thorpej 
    315  1.31     kochi static int
    316  1.55    cegger acpiec_match(device_t parent, cfdata_t match, void *aux)
    317   1.1   thorpej {
    318   1.1   thorpej 	struct acpi_attach_args *aa = aux;
    319   1.1   thorpej 
    320  1.85   thorpej 	return acpi_compatible_match(aa, compat_data);
    321   1.1   thorpej }
    322   1.1   thorpej 
    323  1.44  jmcneill static void
    324  1.44  jmcneill acpiec_attach(device_t parent, device_t self, void *aux)
    325  1.17   mycroft {
    326  1.44  jmcneill 	struct acpi_attach_args *aa = aux;
    327  1.44  jmcneill 	struct acpi_resources ec_res;
    328  1.44  jmcneill 	struct acpi_io *io0, *io1;
    329  1.44  jmcneill 	ACPI_HANDLE gpe_handle;
    330  1.44  jmcneill 	uint8_t gpebit;
    331  1.23     kochi 	ACPI_STATUS rv;
    332  1.17   mycroft 
    333  1.44  jmcneill 	if (ec_singleton != NULL) {
    334  1.54  jmcneill 		aprint_naive(": using %s\n", device_xname(ec_singleton));
    335  1.54  jmcneill 		aprint_normal(": using %s\n", device_xname(ec_singleton));
    336  1.73  riastrad 		goto fail0;
    337  1.44  jmcneill 	}
    338  1.44  jmcneill 
    339  1.84  jdolecek 	if (!acpi_device_present(aa->aa_node->ad_handle)) {
    340  1.84  jdolecek 		aprint_normal(": not present\n");
    341  1.84  jdolecek 		goto fail0;
    342  1.84  jdolecek 	}
    343  1.84  jdolecek 
    344  1.44  jmcneill 	if (!acpiec_parse_gpe_package(self, aa->aa_node->ad_handle,
    345  1.44  jmcneill 				      &gpe_handle, &gpebit))
    346  1.73  riastrad 		goto fail0;
    347  1.17   mycroft 
    348  1.44  jmcneill 	rv = acpi_resource_parse(self, aa->aa_node->ad_handle, "_CRS",
    349  1.44  jmcneill 	    &ec_res, &acpi_resource_parse_ops_default);
    350  1.44  jmcneill 	if (rv != AE_OK) {
    351  1.44  jmcneill 		aprint_error_dev(self, "resource parsing failed: %s\n",
    352  1.44  jmcneill 		    AcpiFormatException(rv));
    353  1.73  riastrad 		goto fail0;
    354  1.17   mycroft 	}
    355  1.17   mycroft 
    356  1.44  jmcneill 	if ((io0 = acpi_res_io(&ec_res, 0)) == NULL) {
    357  1.44  jmcneill 		aprint_error_dev(self, "no data register resource\n");
    358  1.73  riastrad 		goto fail1;
    359  1.44  jmcneill 	}
    360  1.44  jmcneill 	if ((io1 = acpi_res_io(&ec_res, 1)) == NULL) {
    361  1.44  jmcneill 		aprint_error_dev(self, "no CSR register resource\n");
    362  1.73  riastrad 		goto fail1;
    363  1.23     kochi 	}
    364  1.23     kochi 
    365  1.44  jmcneill 	acpiec_common_attach(parent, self, aa->aa_node->ad_handle,
    366  1.63    dyoung 	    aa->aa_iot, io1->ar_base, aa->aa_iot, io0->ar_base,
    367  1.63    dyoung 	    gpe_handle, gpebit);
    368  1.23     kochi 
    369  1.44  jmcneill 	acpi_resource_cleanup(&ec_res);
    370  1.73  riastrad 	return;
    371  1.73  riastrad 
    372  1.73  riastrad fail1:	acpi_resource_cleanup(&ec_res);
    373  1.73  riastrad fail0:	if (!pmf_device_register(self, NULL, NULL))
    374  1.73  riastrad 		aprint_error_dev(self, "couldn't establish power handler\n");
    375  1.44  jmcneill }
    376  1.23     kochi 
    377  1.44  jmcneill static void
    378  1.44  jmcneill acpiec_common_attach(device_t parent, device_t self,
    379  1.63    dyoung     ACPI_HANDLE ec_handle, bus_space_tag_t cmdt, bus_addr_t cmd_reg,
    380  1.63    dyoung     bus_space_tag_t datat, bus_addr_t data_reg,
    381  1.44  jmcneill     ACPI_HANDLE gpe_handle, uint8_t gpebit)
    382  1.44  jmcneill {
    383  1.44  jmcneill 	struct acpiec_softc *sc = device_private(self);
    384  1.44  jmcneill 	ACPI_STATUS rv;
    385  1.44  jmcneill 	ACPI_INTEGER val;
    386  1.23     kochi 
    387  1.87  riastrad 	sc->sc_dev = self;
    388  1.87  riastrad 
    389  1.63    dyoung 	sc->sc_csr_st = cmdt;
    390  1.63    dyoung 	sc->sc_data_st = datat;
    391  1.63    dyoung 
    392  1.44  jmcneill 	sc->sc_ech = ec_handle;
    393  1.44  jmcneill 	sc->sc_gpeh = gpe_handle;
    394  1.44  jmcneill 	sc->sc_gpebit = gpebit;
    395  1.44  jmcneill 
    396  1.44  jmcneill 	sc->sc_state = EC_STATE_FREE;
    397  1.44  jmcneill 	mutex_init(&sc->sc_mtx, MUTEX_DRIVER, IPL_TTY);
    398  1.44  jmcneill 	mutex_init(&sc->sc_access_mtx, MUTEX_DEFAULT, IPL_NONE);
    399  1.44  jmcneill 	cv_init(&sc->sc_cv, "eccv");
    400  1.44  jmcneill 	cv_init(&sc->sc_cv_sci, "ecsci");
    401  1.44  jmcneill 
    402  1.44  jmcneill 	if (bus_space_map(sc->sc_data_st, data_reg, 1, 0,
    403  1.44  jmcneill 	    &sc->sc_data_sh) != 0) {
    404  1.44  jmcneill 		aprint_error_dev(self, "unable to map data register\n");
    405  1.44  jmcneill 		return;
    406  1.44  jmcneill 	}
    407  1.23     kochi 
    408  1.44  jmcneill 	if (bus_space_map(sc->sc_csr_st, cmd_reg, 1, 0, &sc->sc_csr_sh) != 0) {
    409  1.44  jmcneill 		aprint_error_dev(self, "unable to map CSR register\n");
    410  1.44  jmcneill 		goto post_data_map;
    411  1.23     kochi 	}
    412  1.23     kochi 
    413  1.44  jmcneill 	rv = acpi_eval_integer(sc->sc_ech, "_GLK", &val);
    414  1.44  jmcneill 	if (rv == AE_OK) {
    415  1.44  jmcneill 		sc->sc_need_global_lock = val != 0;
    416  1.44  jmcneill 	} else if (rv != AE_NOT_FOUND) {
    417  1.44  jmcneill 		aprint_error_dev(self, "unable to evaluate _GLK: %s\n",
    418  1.44  jmcneill 		    AcpiFormatException(rv));
    419  1.44  jmcneill 		goto post_csr_map;
    420  1.44  jmcneill 	} else {
    421  1.44  jmcneill 		sc->sc_need_global_lock = false;
    422  1.33     kochi 	}
    423  1.44  jmcneill 	if (sc->sc_need_global_lock)
    424  1.44  jmcneill 		aprint_normal_dev(self, "using global ACPI lock\n");
    425  1.33     kochi 
    426  1.46     joerg 	callout_init(&sc->sc_pseudo_intr, CALLOUT_MPSAFE);
    427  1.46     joerg 	callout_setfunc(&sc->sc_pseudo_intr, acpiec_callout, self);
    428  1.46     joerg 
    429  1.44  jmcneill 	rv = AcpiInstallAddressSpaceHandler(sc->sc_ech, ACPI_ADR_SPACE_EC,
    430  1.44  jmcneill 	    acpiec_space_handler, acpiec_space_setup, self);
    431  1.44  jmcneill 	if (rv != AE_OK) {
    432  1.44  jmcneill 		aprint_error_dev(self,
    433  1.44  jmcneill 		    "unable to install address space handler: %s\n",
    434  1.44  jmcneill 		    AcpiFormatException(rv));
    435  1.44  jmcneill 		goto post_csr_map;
    436  1.33     kochi 	}
    437  1.33     kochi 
    438  1.44  jmcneill 	rv = AcpiInstallGpeHandler(sc->sc_gpeh, sc->sc_gpebit,
    439  1.44  jmcneill 	    ACPI_GPE_EDGE_TRIGGERED, acpiec_gpe_handler, self);
    440  1.44  jmcneill 	if (rv != AE_OK) {
    441  1.44  jmcneill 		aprint_error_dev(self, "unable to install GPE handler: %s\n",
    442  1.23     kochi 		    AcpiFormatException(rv));
    443  1.44  jmcneill 		goto post_csr_map;
    444  1.17   mycroft 	}
    445  1.23     kochi 
    446  1.69    jruoho 	rv = AcpiEnableGpe(sc->sc_gpeh, sc->sc_gpebit);
    447  1.44  jmcneill 	if (rv != AE_OK) {
    448  1.44  jmcneill 		aprint_error_dev(self, "unable to enable GPE: %s\n",
    449  1.44  jmcneill 		    AcpiFormatException(rv));
    450  1.44  jmcneill 		goto post_csr_map;
    451  1.44  jmcneill 	}
    452  1.23     kochi 
    453  1.44  jmcneill 	if (kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL, acpiec_gpe_query,
    454  1.44  jmcneill 		           self, NULL, "acpiec sci thread")) {
    455  1.44  jmcneill 		aprint_error_dev(self, "unable to create query kthread\n");
    456  1.44  jmcneill 		goto post_csr_map;
    457  1.44  jmcneill 	}
    458  1.23     kochi 
    459  1.44  jmcneill 	ec_singleton = self;
    460  1.23     kochi 
    461  1.56       alc 	if (!pmf_device_register1(self, acpiec_suspend, acpiec_resume,
    462  1.56       alc 	    acpiec_shutdown))
    463  1.44  jmcneill 		aprint_error_dev(self, "couldn't establish power handler\n");
    464  1.23     kochi 
    465  1.23     kochi 	return;
    466  1.44  jmcneill 
    467  1.44  jmcneill post_csr_map:
    468  1.44  jmcneill 	(void)AcpiRemoveGpeHandler(sc->sc_gpeh, sc->sc_gpebit,
    469  1.44  jmcneill 	    acpiec_gpe_handler);
    470  1.44  jmcneill 	(void)AcpiRemoveAddressSpaceHandler(sc->sc_ech,
    471  1.44  jmcneill 	    ACPI_ADR_SPACE_EC, acpiec_space_handler);
    472  1.44  jmcneill 	bus_space_unmap(sc->sc_csr_st, sc->sc_csr_sh, 1);
    473  1.44  jmcneill post_data_map:
    474  1.44  jmcneill 	bus_space_unmap(sc->sc_data_st, sc->sc_data_sh, 1);
    475  1.73  riastrad 	if (!pmf_device_register(self, NULL, NULL))
    476  1.73  riastrad 		aprint_error_dev(self, "couldn't establish power handler\n");
    477  1.17   mycroft }
    478  1.17   mycroft 
    479  1.44  jmcneill static bool
    480  1.60    dyoung acpiec_suspend(device_t dv, const pmf_qual_t *qual)
    481   1.1   thorpej {
    482  1.80  riastrad 
    483  1.44  jmcneill 	acpiec_cold = true;
    484   1.1   thorpej 
    485  1.44  jmcneill 	return true;
    486  1.44  jmcneill }
    487   1.1   thorpej 
    488  1.44  jmcneill static bool
    489  1.60    dyoung acpiec_resume(device_t dv, const pmf_qual_t *qual)
    490  1.44  jmcneill {
    491  1.80  riastrad 
    492  1.44  jmcneill 	acpiec_cold = false;
    493   1.1   thorpej 
    494  1.44  jmcneill 	return true;
    495  1.44  jmcneill }
    496  1.17   mycroft 
    497  1.44  jmcneill static bool
    498  1.56       alc acpiec_shutdown(device_t dv, int how)
    499  1.56       alc {
    500  1.56       alc 
    501  1.56       alc 	acpiec_cold = true;
    502  1.56       alc 	return true;
    503  1.56       alc }
    504  1.56       alc 
    505  1.56       alc static bool
    506  1.44  jmcneill acpiec_parse_gpe_package(device_t self, ACPI_HANDLE ec_handle,
    507  1.44  jmcneill     ACPI_HANDLE *gpe_handle, uint8_t *gpebit)
    508  1.44  jmcneill {
    509  1.44  jmcneill 	ACPI_BUFFER buf;
    510  1.44  jmcneill 	ACPI_OBJECT *p, *c;
    511  1.44  jmcneill 	ACPI_STATUS rv;
    512   1.1   thorpej 
    513  1.44  jmcneill 	rv = acpi_eval_struct(ec_handle, "_GPE", &buf);
    514  1.44  jmcneill 	if (rv != AE_OK) {
    515  1.44  jmcneill 		aprint_error_dev(self, "unable to evaluate _GPE: %s\n",
    516  1.44  jmcneill 		    AcpiFormatException(rv));
    517  1.44  jmcneill 		return false;
    518  1.44  jmcneill 	}
    519   1.1   thorpej 
    520  1.44  jmcneill 	p = buf.Pointer;
    521  1.23     kochi 
    522  1.44  jmcneill 	if (p->Type == ACPI_TYPE_INTEGER) {
    523  1.44  jmcneill 		*gpe_handle = NULL;
    524  1.44  jmcneill 		*gpebit = p->Integer.Value;
    525  1.57   mlelstv 		ACPI_FREE(p);
    526  1.44  jmcneill 		return true;
    527  1.44  jmcneill 	}
    528  1.23     kochi 
    529  1.44  jmcneill 	if (p->Type != ACPI_TYPE_PACKAGE) {
    530  1.44  jmcneill 		aprint_error_dev(self, "_GPE is neither integer nor package\n");
    531  1.57   mlelstv 		ACPI_FREE(p);
    532  1.44  jmcneill 		return false;
    533  1.44  jmcneill 	}
    534  1.80  riastrad 
    535  1.44  jmcneill 	if (p->Package.Count != 2) {
    536  1.80  riastrad 		aprint_error_dev(self,
    537  1.80  riastrad 		    "_GPE package does not contain 2 elements\n");
    538  1.57   mlelstv 		ACPI_FREE(p);
    539  1.44  jmcneill 		return false;
    540   1.1   thorpej 	}
    541   1.1   thorpej 
    542  1.44  jmcneill 	c = &p->Package.Elements[0];
    543  1.59    jruoho 	rv = acpi_eval_reference_handle(c, gpe_handle);
    544  1.59    jruoho 
    545  1.59    jruoho 	if (ACPI_FAILURE(rv)) {
    546  1.59    jruoho 		aprint_error_dev(self, "failed to evaluate _GPE handle\n");
    547  1.57   mlelstv 		ACPI_FREE(p);
    548  1.44  jmcneill 		return false;
    549  1.44  jmcneill 	}
    550  1.59    jruoho 
    551  1.44  jmcneill 	c = &p->Package.Elements[1];
    552  1.59    jruoho 
    553  1.44  jmcneill 	if (c->Type != ACPI_TYPE_INTEGER) {
    554  1.44  jmcneill 		aprint_error_dev(self,
    555  1.44  jmcneill 		    "_GPE package needs integer as 2nd field\n");
    556  1.57   mlelstv 		ACPI_FREE(p);
    557  1.44  jmcneill 		return false;
    558  1.44  jmcneill 	}
    559  1.44  jmcneill 	*gpebit = c->Integer.Value;
    560  1.57   mlelstv 	ACPI_FREE(p);
    561  1.44  jmcneill 	return true;
    562  1.44  jmcneill }
    563  1.23     kochi 
    564  1.44  jmcneill static uint8_t
    565  1.44  jmcneill acpiec_read_data(struct acpiec_softc *sc)
    566  1.44  jmcneill {
    567  1.88  riastrad 	uint8_t x;
    568  1.88  riastrad 
    569  1.88  riastrad 	x = bus_space_read_1(sc->sc_data_st, sc->sc_data_sh, 0);
    570  1.88  riastrad 	DPRINTF(ACPIEC_DEBUG_REG, sc, "read data=0x%"PRIx8"\n", x);
    571  1.88  riastrad 
    572  1.88  riastrad 	return x;
    573  1.44  jmcneill }
    574  1.17   mycroft 
    575  1.44  jmcneill static void
    576  1.44  jmcneill acpiec_write_data(struct acpiec_softc *sc, uint8_t val)
    577  1.44  jmcneill {
    578  1.88  riastrad 
    579  1.88  riastrad 	DPRINTF(ACPIEC_DEBUG_REG, sc, "write data=0x%"PRIx8"\n", val);
    580  1.44  jmcneill 	bus_space_write_1(sc->sc_data_st, sc->sc_data_sh, 0, val);
    581  1.44  jmcneill }
    582   1.1   thorpej 
    583  1.44  jmcneill static uint8_t
    584  1.44  jmcneill acpiec_read_status(struct acpiec_softc *sc)
    585  1.44  jmcneill {
    586  1.88  riastrad 	uint8_t x;
    587  1.88  riastrad 
    588  1.88  riastrad 	x = bus_space_read_1(sc->sc_csr_st, sc->sc_csr_sh, 0);
    589  1.88  riastrad 	DPRINTF(ACPIEC_DEBUG_REG, sc, "read status=0x%"PRIx8"\n", x);
    590  1.88  riastrad 
    591  1.88  riastrad 	return x;
    592  1.44  jmcneill }
    593  1.33     kochi 
    594  1.44  jmcneill static void
    595  1.44  jmcneill acpiec_write_command(struct acpiec_softc *sc, uint8_t cmd)
    596  1.44  jmcneill {
    597  1.88  riastrad 
    598  1.88  riastrad 	DPRINTF(ACPIEC_DEBUG_REG, sc, "write command=0x%"PRIx8"\n", cmd);
    599  1.44  jmcneill 	bus_space_write_1(sc->sc_csr_st, sc->sc_csr_sh, 0, cmd);
    600  1.44  jmcneill }
    601  1.33     kochi 
    602  1.44  jmcneill static ACPI_STATUS
    603  1.65    jruoho acpiec_space_setup(ACPI_HANDLE region, uint32_t func, void *arg,
    604  1.44  jmcneill     void **region_arg)
    605  1.44  jmcneill {
    606  1.80  riastrad 
    607  1.44  jmcneill 	if (func == ACPI_REGION_DEACTIVATE)
    608  1.44  jmcneill 		*region_arg = NULL;
    609  1.44  jmcneill 	else
    610  1.44  jmcneill 		*region_arg = arg;
    611   1.1   thorpej 
    612  1.44  jmcneill 	return AE_OK;
    613   1.1   thorpej }
    614   1.1   thorpej 
    615   1.1   thorpej static void
    616  1.44  jmcneill acpiec_lock(device_t dv)
    617   1.1   thorpej {
    618  1.44  jmcneill 	struct acpiec_softc *sc = device_private(dv);
    619  1.25     kochi 	ACPI_STATUS rv;
    620   1.1   thorpej 
    621  1.44  jmcneill 	mutex_enter(&sc->sc_access_mtx);
    622   1.1   thorpej 
    623  1.44  jmcneill 	if (sc->sc_need_global_lock) {
    624  1.80  riastrad 		rv = AcpiAcquireGlobalLock(EC_LOCK_TIMEOUT,
    625  1.80  riastrad 		    &sc->sc_global_lock);
    626  1.44  jmcneill 		if (rv != AE_OK) {
    627  1.80  riastrad 			aprint_error_dev(dv,
    628  1.80  riastrad 			    "failed to acquire global lock: %s\n",
    629  1.44  jmcneill 			    AcpiFormatException(rv));
    630  1.44  jmcneill 			return;
    631   1.1   thorpej 		}
    632  1.44  jmcneill 	}
    633  1.44  jmcneill }
    634  1.44  jmcneill 
    635  1.44  jmcneill static void
    636  1.44  jmcneill acpiec_unlock(device_t dv)
    637  1.44  jmcneill {
    638  1.44  jmcneill 	struct acpiec_softc *sc = device_private(dv);
    639  1.44  jmcneill 	ACPI_STATUS rv;
    640   1.1   thorpej 
    641  1.44  jmcneill 	if (sc->sc_need_global_lock) {
    642  1.44  jmcneill 		rv = AcpiReleaseGlobalLock(sc->sc_global_lock);
    643  1.44  jmcneill 		if (rv != AE_OK) {
    644  1.80  riastrad 			aprint_error_dev(dv,
    645  1.80  riastrad 			    "failed to release global lock: %s\n",
    646  1.25     kochi 			    AcpiFormatException(rv));
    647   1.1   thorpej 		}
    648   1.1   thorpej 	}
    649  1.44  jmcneill 	mutex_exit(&sc->sc_access_mtx);
    650  1.44  jmcneill }
    651  1.44  jmcneill 
    652  1.44  jmcneill static ACPI_STATUS
    653  1.44  jmcneill acpiec_read(device_t dv, uint8_t addr, uint8_t *val)
    654  1.44  jmcneill {
    655  1.44  jmcneill 	struct acpiec_softc *sc = device_private(dv);
    656  1.50  jmcneill 	int i, timeo = 1000 * EC_CMD_TIMEOUT;
    657   1.1   thorpej 
    658  1.44  jmcneill 	acpiec_lock(dv);
    659  1.44  jmcneill 	mutex_enter(&sc->sc_mtx);
    660   1.1   thorpej 
    661  1.88  riastrad 	DPRINTF(ACPIEC_DEBUG_RW, sc,
    662  1.88  riastrad 	    "pid %ld %s, lid %ld%s%s: read addr 0x%"PRIx8"\n",
    663  1.88  riastrad 	    (long)curproc->p_pid, curproc->p_comm,
    664  1.88  riastrad 	    (long)curlwp->l_lid, curlwp->l_name ? " " : "",
    665  1.88  riastrad 	    curlwp->l_name ? curlwp->l_name : "",
    666  1.88  riastrad 	    addr);
    667  1.88  riastrad 
    668  1.44  jmcneill 	sc->sc_cur_addr = addr;
    669  1.44  jmcneill 	sc->sc_state = EC_STATE_READ;
    670   1.1   thorpej 
    671  1.44  jmcneill 	for (i = 0; i < EC_POLL_TIMEOUT; ++i) {
    672  1.45  jmcneill 		acpiec_gpe_state_machine(dv);
    673  1.44  jmcneill 		if (sc->sc_state == EC_STATE_FREE)
    674  1.44  jmcneill 			goto done;
    675  1.44  jmcneill 		delay(1);
    676  1.44  jmcneill 	}
    677   1.1   thorpej 
    678  1.44  jmcneill 	if (cold || acpiec_cold) {
    679  1.49  jmcneill 		while (sc->sc_state != EC_STATE_FREE && timeo-- > 0) {
    680  1.50  jmcneill 			delay(1000);
    681  1.45  jmcneill 			acpiec_gpe_state_machine(dv);
    682   1.1   thorpej 		}
    683  1.49  jmcneill 		if (sc->sc_state != EC_STATE_FREE) {
    684  1.49  jmcneill 			mutex_exit(&sc->sc_mtx);
    685  1.49  jmcneill 			acpiec_unlock(dv);
    686  1.49  jmcneill 			aprint_error_dev(dv, "command timed out, state %d\n",
    687  1.49  jmcneill 			    sc->sc_state);
    688  1.49  jmcneill 			return AE_ERROR;
    689  1.49  jmcneill 		}
    690  1.52     joerg 	} else if (cv_timedwait(&sc->sc_cv, &sc->sc_mtx, EC_CMD_TIMEOUT * hz)) {
    691  1.44  jmcneill 		mutex_exit(&sc->sc_mtx);
    692  1.44  jmcneill 		acpiec_unlock(dv);
    693  1.80  riastrad 		aprint_error_dev(dv,
    694  1.80  riastrad 		    "command takes over %d sec...\n", EC_CMD_TIMEOUT);
    695  1.44  jmcneill 		return AE_ERROR;
    696   1.1   thorpej 	}
    697  1.33     kochi 
    698  1.44  jmcneill done:
    699  1.88  riastrad 	DPRINTF(ACPIEC_DEBUG_RW, sc,
    700  1.88  riastrad 	    "pid %ld %s, lid %ld%s%s: read addr 0x%"PRIx8": 0x%"PRIx8"\n",
    701  1.88  riastrad 	    (long)curproc->p_pid, curproc->p_comm,
    702  1.88  riastrad 	    (long)curlwp->l_lid, curlwp->l_name ? " " : "",
    703  1.88  riastrad 	    curlwp->l_name ? curlwp->l_name : "",
    704  1.88  riastrad 	    addr, sc->sc_cur_val);
    705  1.88  riastrad 
    706  1.44  jmcneill 	*val = sc->sc_cur_val;
    707  1.44  jmcneill 
    708  1.44  jmcneill 	mutex_exit(&sc->sc_mtx);
    709  1.44  jmcneill 	acpiec_unlock(dv);
    710  1.44  jmcneill 	return AE_OK;
    711   1.1   thorpej }
    712   1.1   thorpej 
    713   1.1   thorpej static ACPI_STATUS
    714  1.44  jmcneill acpiec_write(device_t dv, uint8_t addr, uint8_t val)
    715   1.1   thorpej {
    716  1.44  jmcneill 	struct acpiec_softc *sc = device_private(dv);
    717  1.50  jmcneill 	int i, timeo = 1000 * EC_CMD_TIMEOUT;
    718   1.1   thorpej 
    719  1.44  jmcneill 	acpiec_lock(dv);
    720  1.44  jmcneill 	mutex_enter(&sc->sc_mtx);
    721   1.1   thorpej 
    722  1.88  riastrad 	DPRINTF(ACPIEC_DEBUG_RW, sc,
    723  1.88  riastrad 	    "pid %ld %s, lid %ld%s%s write addr 0x%"PRIx8": 0x%"PRIx8"\n",
    724  1.88  riastrad 	    (long)curproc->p_pid, curproc->p_comm,
    725  1.88  riastrad 	    (long)curlwp->l_lid, curlwp->l_name ? " " : "",
    726  1.88  riastrad 	    curlwp->l_name ? curlwp->l_name : "",
    727  1.88  riastrad 	    addr, val);
    728  1.88  riastrad 
    729  1.44  jmcneill 	sc->sc_cur_addr = addr;
    730  1.44  jmcneill 	sc->sc_cur_val = val;
    731  1.44  jmcneill 	sc->sc_state = EC_STATE_WRITE;
    732  1.44  jmcneill 
    733  1.44  jmcneill 	for (i = 0; i < EC_POLL_TIMEOUT; ++i) {
    734  1.45  jmcneill 		acpiec_gpe_state_machine(dv);
    735  1.44  jmcneill 		if (sc->sc_state == EC_STATE_FREE)
    736  1.44  jmcneill 			goto done;
    737  1.44  jmcneill 		delay(1);
    738  1.44  jmcneill 	}
    739  1.44  jmcneill 
    740  1.44  jmcneill 	if (cold || acpiec_cold) {
    741  1.49  jmcneill 		while (sc->sc_state != EC_STATE_FREE && timeo-- > 0) {
    742  1.50  jmcneill 			delay(1000);
    743  1.45  jmcneill 			acpiec_gpe_state_machine(dv);
    744  1.44  jmcneill 		}
    745  1.49  jmcneill 		if (sc->sc_state != EC_STATE_FREE) {
    746  1.49  jmcneill 			mutex_exit(&sc->sc_mtx);
    747  1.49  jmcneill 			acpiec_unlock(dv);
    748  1.49  jmcneill 			aprint_error_dev(dv, "command timed out, state %d\n",
    749  1.49  jmcneill 			    sc->sc_state);
    750  1.49  jmcneill 			return AE_ERROR;
    751  1.49  jmcneill 		}
    752  1.52     joerg 	} else if (cv_timedwait(&sc->sc_cv, &sc->sc_mtx, EC_CMD_TIMEOUT * hz)) {
    753  1.44  jmcneill 		mutex_exit(&sc->sc_mtx);
    754  1.44  jmcneill 		acpiec_unlock(dv);
    755  1.80  riastrad 		aprint_error_dev(dv,
    756  1.80  riastrad 		    "command takes over %d sec...\n", EC_CMD_TIMEOUT);
    757  1.44  jmcneill 		return AE_ERROR;
    758  1.44  jmcneill 	}
    759   1.1   thorpej 
    760  1.44  jmcneill done:
    761  1.88  riastrad 	DPRINTF(ACPIEC_DEBUG_RW, sc,
    762  1.88  riastrad 	    "pid %ld %s, lid %ld%s%s: write addr 0x%"PRIx8": 0x%"PRIx8
    763  1.88  riastrad 	    " done\n",
    764  1.88  riastrad 	    (long)curproc->p_pid, curproc->p_comm,
    765  1.88  riastrad 	    (long)curlwp->l_lid, curlwp->l_name ? " " : "",
    766  1.88  riastrad 	    curlwp->l_name ? curlwp->l_name : "",
    767  1.88  riastrad 	    addr, val);
    768  1.88  riastrad 
    769  1.44  jmcneill 	mutex_exit(&sc->sc_mtx);
    770  1.44  jmcneill 	acpiec_unlock(dv);
    771  1.44  jmcneill 	return AE_OK;
    772   1.1   thorpej }
    773   1.1   thorpej 
    774  1.86  riastrad /*
    775  1.86  riastrad  * acpiec_space_handler(func, paddr, bitwidth, value, arg, region_arg)
    776  1.86  riastrad  *
    777  1.86  riastrad  *	Transfer bitwidth/8 bytes of data between paddr and *value:
    778  1.86  riastrad  *	from paddr to *value when func is ACPI_READ, and the other way
    779  1.86  riastrad  *	when func is ACPI_WRITE.  arg is the acpiec(4) or acpiecdt(4)
    780  1.86  riastrad  *	device.  region_arg is ignored (XXX why? determined by
    781  1.86  riastrad  *	acpiec_space_setup but never used by anything that I can see).
    782  1.86  riastrad  *
    783  1.86  riastrad  *	The caller always provides storage at *value large enough for
    784  1.86  riastrad  *	an ACPI_INTEGER object, i.e., a 64-bit integer.  However,
    785  1.86  riastrad  *	bitwidth may be larger; in this case the caller provides larger
    786  1.86  riastrad  *	storage at *value, e.g. 128 bits as documented in
    787  1.86  riastrad  *	<https://gnats.netbsd.org/55206>.
    788  1.86  riastrad  *
    789  1.86  riastrad  *	On reads, this fully initializes one ACPI_INTEGER's worth of
    790  1.86  riastrad  *	data at *value, even if bitwidth < 64.  The integer is
    791  1.86  riastrad  *	interpreted in host byte order; in other words, bytes of data
    792  1.86  riastrad  *	are transferred in order between paddr and (uint8_t *)value.
    793  1.86  riastrad  *	The transfer is not atomic; it may go byte-by-byte.
    794  1.86  riastrad  *
    795  1.86  riastrad  *	XXX This only really makes sense on little-endian systems.
    796  1.86  riastrad  *	E.g., thinkpad_acpi.c assumes that a single byte is transferred
    797  1.86  riastrad  *	in the low-order bits of the result.  A big-endian system could
    798  1.86  riastrad  *	read a 64-bit integer in big-endian (and it did for a while!),
    799  1.86  riastrad  *	but what should it do for larger reads?  Unclear!
    800  1.86  riastrad  *
    801  1.86  riastrad  *	XXX It's not clear whether the object at *value is always
    802  1.86  riastrad  *	_aligned_ adequately for an ACPI_INTEGER object.  Currently it
    803  1.86  riastrad  *	always is as long as malloc, used by AcpiOsAllocate, returns
    804  1.86  riastrad  *	64-bit-aligned data.
    805  1.86  riastrad  */
    806   1.1   thorpej static ACPI_STATUS
    807  1.65    jruoho acpiec_space_handler(uint32_t func, ACPI_PHYSICAL_ADDRESS paddr,
    808  1.65    jruoho     uint32_t width, ACPI_INTEGER *value, void *arg, void *region_arg)
    809   1.1   thorpej {
    810  1.79  riastrad 	device_t dv;
    811  1.44  jmcneill 	ACPI_STATUS rv;
    812  1.82  jmcneill 	uint8_t addr, *buf;
    813  1.79  riastrad 	unsigned int i;
    814  1.44  jmcneill 
    815  1.82  jmcneill 	if (paddr > 0xff || width % 8 != 0 ||
    816  1.81  riastrad 	    value == NULL || arg == NULL || paddr + width / 8 > 0x100)
    817  1.44  jmcneill 		return AE_BAD_PARAMETER;
    818   1.1   thorpej 
    819  1.44  jmcneill 	addr = paddr;
    820  1.79  riastrad 	dv = arg;
    821  1.82  jmcneill 	buf = (uint8_t *)value;
    822   1.1   thorpej 
    823  1.44  jmcneill 	rv = AE_OK;
    824   1.1   thorpej 
    825  1.79  riastrad 	switch (func) {
    826  1.79  riastrad 	case ACPI_READ:
    827  1.82  jmcneill 		for (i = 0; i < width; i += 8, ++addr, ++buf) {
    828  1.82  jmcneill 			rv = acpiec_read(dv, addr, buf);
    829  1.79  riastrad 			if (rv != AE_OK)
    830  1.79  riastrad 				break;
    831  1.79  riastrad 		}
    832  1.86  riastrad 		/*
    833  1.86  riastrad 		 * Make sure to fully initialize at least an
    834  1.86  riastrad 		 * ACPI_INTEGER-sized object.
    835  1.86  riastrad 		 */
    836  1.86  riastrad 		for (; i < sizeof(*value)*8; i += 8, ++buf)
    837  1.86  riastrad 			*buf = 0;
    838  1.79  riastrad 		break;
    839  1.79  riastrad 	case ACPI_WRITE:
    840  1.82  jmcneill 		for (i = 0; i < width; i += 8, ++addr, ++buf) {
    841  1.82  jmcneill 			rv = acpiec_write(dv, addr, *buf);
    842  1.79  riastrad 			if (rv != AE_OK)
    843  1.79  riastrad 				break;
    844  1.78  riastrad 		}
    845  1.79  riastrad 		break;
    846  1.79  riastrad 	default:
    847  1.79  riastrad 		aprint_error("%s: invalid Address Space function called: %x\n",
    848  1.79  riastrad 		    device_xname(dv), (unsigned int)func);
    849  1.79  riastrad 		return AE_BAD_PARAMETER;
    850  1.79  riastrad 	}
    851   1.1   thorpej 
    852  1.44  jmcneill 	return rv;
    853   1.1   thorpej }
    854   1.1   thorpej 
    855  1.44  jmcneill static void
    856  1.44  jmcneill acpiec_gpe_query(void *arg)
    857   1.1   thorpej {
    858  1.44  jmcneill 	device_t dv = arg;
    859  1.44  jmcneill 	struct acpiec_softc *sc = device_private(dv);
    860  1.44  jmcneill 	uint8_t reg;
    861  1.44  jmcneill 	char qxx[5];
    862  1.44  jmcneill 	ACPI_STATUS rv;
    863   1.1   thorpej 	int i;
    864   1.1   thorpej 
    865  1.44  jmcneill loop:
    866  1.44  jmcneill 	mutex_enter(&sc->sc_mtx);
    867  1.44  jmcneill 
    868  1.44  jmcneill 	if (sc->sc_got_sci == false)
    869  1.44  jmcneill 		cv_wait(&sc->sc_cv_sci, &sc->sc_mtx);
    870  1.88  riastrad 	DPRINTF(ACPIEC_DEBUG_QUERY, sc, "SCI query requested\n");
    871  1.44  jmcneill 	mutex_exit(&sc->sc_mtx);
    872  1.44  jmcneill 
    873  1.44  jmcneill 	acpiec_lock(dv);
    874  1.44  jmcneill 	mutex_enter(&sc->sc_mtx);
    875  1.44  jmcneill 
    876  1.88  riastrad 	DPRINTF(ACPIEC_DEBUG_QUERY, sc, "SCI query\n");
    877  1.88  riastrad 
    878  1.44  jmcneill 	/* The Query command can always be issued, so be defensive here. */
    879  1.44  jmcneill 	sc->sc_got_sci = false;
    880  1.44  jmcneill 	sc->sc_state = EC_STATE_QUERY;
    881   1.1   thorpej 
    882  1.44  jmcneill 	for (i = 0; i < EC_POLL_TIMEOUT; ++i) {
    883  1.45  jmcneill 		acpiec_gpe_state_machine(dv);
    884  1.44  jmcneill 		if (sc->sc_state == EC_STATE_FREE)
    885  1.44  jmcneill 			goto done;
    886  1.44  jmcneill 		delay(1);
    887   1.1   thorpej 	}
    888   1.1   thorpej 
    889  1.88  riastrad 	DPRINTF(ACPIEC_DEBUG_QUERY, sc, "SCI polling timeout\n");
    890  1.44  jmcneill 	cv_wait(&sc->sc_cv, &sc->sc_mtx);
    891  1.44  jmcneill 
    892  1.44  jmcneill done:
    893  1.44  jmcneill 	reg = sc->sc_cur_val;
    894  1.88  riastrad 	DPRINTF(ACPIEC_DEBUG_QUERY, sc, "SCI query: 0x%"PRIx8"\n", reg);
    895   1.1   thorpej 
    896  1.44  jmcneill 	mutex_exit(&sc->sc_mtx);
    897  1.44  jmcneill 	acpiec_unlock(dv);
    898   1.1   thorpej 
    899  1.44  jmcneill 	if (reg == 0)
    900  1.44  jmcneill 		goto loop; /* Spurious query result */
    901   1.1   thorpej 
    902   1.1   thorpej 	/*
    903  1.44  jmcneill 	 * Evaluate _Qxx to respond to the controller.
    904   1.1   thorpej 	 */
    905  1.44  jmcneill 	snprintf(qxx, sizeof(qxx), "_Q%02X", (unsigned int)reg);
    906  1.44  jmcneill 	rv = AcpiEvaluateObject(sc->sc_ech, qxx, NULL, NULL);
    907  1.44  jmcneill 	if (rv != AE_OK && rv != AE_NOT_FOUND) {
    908  1.68    cegger 		aprint_error_dev(dv, "GPE query method %s failed: %s",
    909  1.68    cegger 		    qxx, AcpiFormatException(rv));
    910   1.1   thorpej 	}
    911   1.1   thorpej 
    912  1.44  jmcneill 	goto loop;
    913  1.24     kochi }
    914   1.1   thorpej 
    915  1.44  jmcneill static void
    916  1.45  jmcneill acpiec_gpe_state_machine(device_t dv)
    917   1.1   thorpej {
    918  1.44  jmcneill 	struct acpiec_softc *sc = device_private(dv);
    919  1.44  jmcneill 	uint8_t reg;
    920   1.1   thorpej 
    921  1.44  jmcneill 	reg = acpiec_read_status(sc);
    922   1.1   thorpej 
    923  1.88  riastrad #ifdef ACPIEC_DEBUG
    924  1.88  riastrad 	if (acpiec_debug & __BIT(ACPIEC_DEBUG_TRANSITION)) {
    925  1.88  riastrad 		char buf[128];
    926  1.88  riastrad 
    927  1.88  riastrad 		snprintb(buf, sizeof(buf), EC_STATUS_FMT, reg);
    928  1.88  riastrad 		DPRINTF(ACPIEC_DEBUG_TRANSITION, sc, "%s\n", buf);
    929  1.88  riastrad 	}
    930  1.88  riastrad #endif
    931  1.88  riastrad 
    932  1.44  jmcneill 	if (reg & EC_STATUS_SCI)
    933  1.44  jmcneill 		sc->sc_got_sci = true;
    934  1.44  jmcneill 
    935  1.44  jmcneill 	switch (sc->sc_state) {
    936  1.44  jmcneill 	case EC_STATE_QUERY:
    937  1.44  jmcneill 		if ((reg & EC_STATUS_IBF) != 0)
    938  1.44  jmcneill 			break; /* Nothing of interest here. */
    939  1.44  jmcneill 		acpiec_write_command(sc, EC_COMMAND_QUERY);
    940  1.44  jmcneill 		sc->sc_state = EC_STATE_QUERY_VAL;
    941  1.44  jmcneill 		break;
    942   1.1   thorpej 
    943  1.44  jmcneill 	case EC_STATE_QUERY_VAL:
    944  1.44  jmcneill 		if ((reg & EC_STATUS_OBF) == 0)
    945  1.44  jmcneill 			break; /* Nothing of interest here. */
    946   1.1   thorpej 
    947  1.44  jmcneill 		sc->sc_cur_val = acpiec_read_data(sc);
    948  1.44  jmcneill 		sc->sc_state = EC_STATE_FREE;
    949   1.1   thorpej 
    950  1.44  jmcneill 		cv_signal(&sc->sc_cv);
    951   1.1   thorpej 		break;
    952   1.1   thorpej 
    953  1.44  jmcneill 	case EC_STATE_READ:
    954  1.44  jmcneill 		if ((reg & EC_STATUS_IBF) != 0)
    955  1.44  jmcneill 			break; /* Nothing of interest here. */
    956   1.1   thorpej 
    957  1.44  jmcneill 		acpiec_write_command(sc, EC_COMMAND_READ);
    958  1.44  jmcneill 		sc->sc_state = EC_STATE_READ_ADDR;
    959   1.1   thorpej 		break;
    960   1.1   thorpej 
    961  1.44  jmcneill 	case EC_STATE_READ_ADDR:
    962  1.44  jmcneill 		if ((reg & EC_STATUS_IBF) != 0)
    963  1.44  jmcneill 			break; /* Nothing of interest here. */
    964   1.1   thorpej 
    965  1.44  jmcneill 		acpiec_write_data(sc, sc->sc_cur_addr);
    966  1.44  jmcneill 		sc->sc_state = EC_STATE_READ_VAL;
    967  1.44  jmcneill 		break;
    968   1.1   thorpej 
    969  1.44  jmcneill 	case EC_STATE_READ_VAL:
    970  1.44  jmcneill 		if ((reg & EC_STATUS_OBF) == 0)
    971  1.44  jmcneill 			break; /* Nothing of interest here. */
    972  1.44  jmcneill 		sc->sc_cur_val = acpiec_read_data(sc);
    973  1.44  jmcneill 		sc->sc_state = EC_STATE_FREE;
    974   1.1   thorpej 
    975  1.44  jmcneill 		cv_signal(&sc->sc_cv);
    976  1.44  jmcneill 		break;
    977   1.1   thorpej 
    978  1.44  jmcneill 	case EC_STATE_WRITE:
    979  1.44  jmcneill 		if ((reg & EC_STATUS_IBF) != 0)
    980  1.44  jmcneill 			break; /* Nothing of interest here. */
    981   1.1   thorpej 
    982  1.44  jmcneill 		acpiec_write_command(sc, EC_COMMAND_WRITE);
    983  1.44  jmcneill 		sc->sc_state = EC_STATE_WRITE_ADDR;
    984  1.44  jmcneill 		break;
    985   1.1   thorpej 
    986  1.44  jmcneill 	case EC_STATE_WRITE_ADDR:
    987  1.44  jmcneill 		if ((reg & EC_STATUS_IBF) != 0)
    988  1.44  jmcneill 			break; /* Nothing of interest here. */
    989  1.44  jmcneill 		acpiec_write_data(sc, sc->sc_cur_addr);
    990  1.44  jmcneill 		sc->sc_state = EC_STATE_WRITE_VAL;
    991  1.44  jmcneill 		break;
    992   1.1   thorpej 
    993  1.44  jmcneill 	case EC_STATE_WRITE_VAL:
    994  1.44  jmcneill 		if ((reg & EC_STATUS_IBF) != 0)
    995  1.44  jmcneill 			break; /* Nothing of interest here. */
    996  1.44  jmcneill 		sc->sc_state = EC_STATE_FREE;
    997  1.44  jmcneill 		cv_signal(&sc->sc_cv);
    998   1.1   thorpej 
    999  1.44  jmcneill 		acpiec_write_data(sc, sc->sc_cur_val);
   1000  1.44  jmcneill 		break;
   1001   1.1   thorpej 
   1002  1.44  jmcneill 	case EC_STATE_FREE:
   1003  1.88  riastrad 		if (sc->sc_got_sci) {
   1004  1.88  riastrad 			DPRINTF(ACPIEC_DEBUG_TRANSITION, sc,
   1005  1.88  riastrad 			    "wake SCI thread\n");
   1006  1.44  jmcneill 			cv_signal(&sc->sc_cv_sci);
   1007  1.88  riastrad 		}
   1008  1.44  jmcneill 		break;
   1009  1.44  jmcneill 	default:
   1010  1.44  jmcneill 		panic("invalid state");
   1011  1.44  jmcneill 	}
   1012  1.46     joerg 
   1013  1.88  riastrad 	if (sc->sc_state != EC_STATE_FREE) {
   1014  1.88  riastrad 		DPRINTF(ACPIEC_DEBUG_INTR, sc, "schedule callout\n");
   1015  1.46     joerg 		callout_schedule(&sc->sc_pseudo_intr, 1);
   1016  1.88  riastrad 	}
   1017  1.88  riastrad 
   1018  1.88  riastrad 	DPRINTF(ACPIEC_DEBUG_TRANSITION, sc, "return\n");
   1019  1.46     joerg }
   1020  1.46     joerg 
   1021  1.46     joerg static void
   1022  1.46     joerg acpiec_callout(void *arg)
   1023  1.46     joerg {
   1024  1.46     joerg 	device_t dv = arg;
   1025  1.46     joerg 	struct acpiec_softc *sc = device_private(dv);
   1026  1.46     joerg 
   1027  1.46     joerg 	mutex_enter(&sc->sc_mtx);
   1028  1.88  riastrad 	DPRINTF(ACPIEC_DEBUG_INTR, sc, "callout\n");
   1029  1.46     joerg 	acpiec_gpe_state_machine(dv);
   1030  1.46     joerg 	mutex_exit(&sc->sc_mtx);
   1031  1.24     kochi }
   1032   1.1   thorpej 
   1033  1.65    jruoho static uint32_t
   1034  1.69    jruoho acpiec_gpe_handler(ACPI_HANDLE hdl, uint32_t gpebit, void *arg)
   1035   1.1   thorpej {
   1036  1.44  jmcneill 	device_t dv = arg;
   1037  1.44  jmcneill 	struct acpiec_softc *sc = device_private(dv);
   1038   1.1   thorpej 
   1039  1.44  jmcneill 	mutex_enter(&sc->sc_mtx);
   1040  1.88  riastrad 	DPRINTF(ACPIEC_DEBUG_INTR, sc, "GPE\n");
   1041  1.45  jmcneill 	acpiec_gpe_state_machine(dv);
   1042  1.44  jmcneill 	mutex_exit(&sc->sc_mtx);
   1043   1.1   thorpej 
   1044  1.70    jruoho 	return ACPI_INTERRUPT_HANDLED | ACPI_REENABLE_GPE;
   1045   1.1   thorpej }
   1046  1.48  jmcneill 
   1047  1.48  jmcneill ACPI_STATUS
   1048  1.48  jmcneill acpiec_bus_read(device_t dv, u_int addr, ACPI_INTEGER *val, int width)
   1049  1.48  jmcneill {
   1050  1.48  jmcneill 	return acpiec_space_handler(ACPI_READ, addr, width * 8, val, dv, NULL);
   1051  1.48  jmcneill }
   1052  1.48  jmcneill 
   1053  1.48  jmcneill ACPI_STATUS
   1054  1.48  jmcneill acpiec_bus_write(device_t dv, u_int addr, ACPI_INTEGER val, int width)
   1055  1.48  jmcneill {
   1056  1.80  riastrad 	return acpiec_space_handler(ACPI_WRITE, addr, width * 8, &val, dv,
   1057  1.80  riastrad 	    NULL);
   1058  1.48  jmcneill }
   1059  1.48  jmcneill 
   1060  1.48  jmcneill ACPI_HANDLE
   1061  1.48  jmcneill acpiec_get_handle(device_t dv)
   1062  1.48  jmcneill {
   1063  1.48  jmcneill 	struct acpiec_softc *sc = device_private(dv);
   1064  1.48  jmcneill 
   1065  1.48  jmcneill 	return sc->sc_ech;
   1066  1.48  jmcneill }
   1067