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