Module Name: src Committed By: jmcneill Date: Mon Oct 22 22:29:35 UTC 2018
Modified Files: src/sys/dev/acpi: files.acpi plgpio_acpi.c Added Files: src/sys/dev/acpi: acpi_event.c acpi_event.h Log Message: Factor out common ACPI event code into a shared module. To generate a diff of this commit: cvs rdiff -u -r0 -r1.1 src/sys/dev/acpi/acpi_event.c \ src/sys/dev/acpi/acpi_event.h cvs rdiff -u -r1.109 -r1.110 src/sys/dev/acpi/files.acpi cvs rdiff -u -r1.2 -r1.3 src/sys/dev/acpi/plgpio_acpi.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/sys/dev/acpi/files.acpi diff -u src/sys/dev/acpi/files.acpi:1.109 src/sys/dev/acpi/files.acpi:1.110 --- src/sys/dev/acpi/files.acpi:1.109 Sun Oct 21 12:26:38 2018 +++ src/sys/dev/acpi/files.acpi Mon Oct 22 22:29:35 2018 @@ -1,4 +1,4 @@ -# $NetBSD: files.acpi,v 1.109 2018/10/21 12:26:38 jmcneill Exp $ +# $NetBSD: files.acpi,v 1.110 2018/10/22 22:29:35 jmcneill Exp $ include "dev/acpi/acpica/files.acpica" @@ -21,6 +21,7 @@ device acpi: acpica, acpiapmbus, acpinod attach acpi at acpibus file dev/acpi/acpi.c acpi file dev/acpi/acpi_debug.c acpi +file dev/acpi/acpi_event.c acpi file dev/acpi/acpi_i2c.c acpi file dev/acpi/acpi_mcfg.c acpi & pci file dev/acpi/acpi_pci.c acpi Index: src/sys/dev/acpi/plgpio_acpi.c diff -u src/sys/dev/acpi/plgpio_acpi.c:1.2 src/sys/dev/acpi/plgpio_acpi.c:1.3 --- src/sys/dev/acpi/plgpio_acpi.c:1.2 Sun Oct 21 18:31:58 2018 +++ src/sys/dev/acpi/plgpio_acpi.c Mon Oct 22 22:29:35 2018 @@ -1,4 +1,4 @@ -/* $NetBSD: plgpio_acpi.c,v 1.2 2018/10/21 18:31:58 jmcneill Exp $ */ +/* $NetBSD: plgpio_acpi.c,v 1.3 2018/10/22 22:29:35 jmcneill Exp $ */ /*- * Copyright (c) 2018 The NetBSD Foundation, Inc. @@ -30,7 +30,7 @@ */ #include <sys/cdefs.h> -__KERNEL_RCSID(0, "$NetBSD: plgpio_acpi.c,v 1.2 2018/10/21 18:31:58 jmcneill Exp $"); +__KERNEL_RCSID(0, "$NetBSD: plgpio_acpi.c,v 1.3 2018/10/22 22:29:35 jmcneill Exp $"); #include <sys/param.h> #include <sys/bus.h> @@ -40,6 +40,7 @@ __KERNEL_RCSID(0, "$NetBSD: plgpio_acpi. #include <dev/acpi/acpireg.h> #include <dev/acpi/acpivar.h> +#include <dev/acpi/acpi_event.h> #include <dev/gpio/gpiovar.h> #include <dev/ic/pl061var.h> @@ -47,28 +48,18 @@ __KERNEL_RCSID(0, "$NetBSD: plgpio_acpi. struct plgpio_acpi_softc; -struct plgpio_acpi_event { - struct plgpio_acpi_softc *ev_sc; - u_int ev_pin; - ACPI_HANDLE ev_method; - bool ev_method_evt; -}; - struct plgpio_acpi_softc { struct plgpio_softc sc_base; ACPI_HANDLE sc_handle; - uint32_t sc_aei_pins; /* bitmask */ - ACPI_RESOURCE_GPIO sc_aei_res[8]; - struct plgpio_acpi_event sc_event[8]; + struct acpi_event * sc_event[8]; }; static int plgpio_acpi_match(device_t, cfdata_t, void *); static void plgpio_acpi_attach(device_t, device_t, void *); -static void plgpio_acpi_init(struct plgpio_acpi_softc *); -static void plgpio_acpi_notify(void *); +static void plgpio_acpi_register_event(void *, struct acpi_event *, ACPI_RESOURCE_GPIO *); static int plgpio_acpi_intr(void *); CFATTACH_DECL_NEW(plgpio_acpi, sizeof(struct plgpio_acpi_softc), plgpio_acpi_match, plgpio_acpi_attach, NULL, NULL); @@ -132,161 +123,83 @@ plgpio_acpi_attach(device_t parent, devi plgpio_attach(sc); + if (ACPI_FAILURE(acpi_event_create_gpio(self, asc->sc_handle, plgpio_acpi_register_event, asc))) { + aprint_error_dev(self, "failed to create events\n"); + goto done; + } + const int type = (irq->ar_type == ACPI_EDGE_SENSITIVE) ? IST_EDGE : IST_LEVEL; ih = intr_establish(irq->ar_irq, IPL_VM, type, plgpio_acpi_intr, asc); if (ih == NULL) aprint_error_dev(self, "couldn't establish interrupt\n"); - plgpio_acpi_init(asc); - done: acpi_resource_cleanup(&res); } -static ACPI_STATUS -plgpio_acpi_resource_cb(ACPI_RESOURCE *res, void *ctx) -{ - struct plgpio_acpi_softc * const asc = ctx; - ACPI_RESOURCE_GPIO *gpio; - UINT16 pin; - - if (res->Type != ACPI_RESOURCE_TYPE_GPIO) - return AE_OK; - - gpio = &res->Data.Gpio; - if (gpio->ConnectionType != ACPI_RESOURCE_GPIO_TYPE_INT || - gpio->PinTableLength != 1) - return AE_OK; - - pin = gpio->PinTable[0]; - if (pin >= __arraycount(asc->sc_aei_res)) { - aprint_error_dev(asc->sc_base.sc_dev, "_AEI pin %u out of range\n", pin); - return AE_OK; - } - - asc->sc_aei_pins |= __BIT(pin); - asc->sc_aei_res[pin] = *gpio; - - return AE_OK; -} - static void -plgpio_acpi_init(struct plgpio_acpi_softc *asc) +plgpio_acpi_register_event(void *priv, struct acpi_event *ev, ACPI_RESOURCE_GPIO *gpio) { + struct plgpio_acpi_softc * const asc = priv; struct plgpio_softc * const sc = &asc->sc_base; - ACPI_RESOURCE_GPIO *gpio; - ACPI_HANDLE handle; - char namebuf[5]; - ACPI_STATUS rv; uint32_t ibe, iev, is, ie; - int pin; - rv = AcpiWalkResources(asc->sc_handle, "_AEI", plgpio_acpi_resource_cb, asc); - if (ACPI_FAILURE(rv)) { - if (rv != AE_NOT_FOUND) - aprint_error_dev(asc->sc_base.sc_dev, "failed to parse _AEI: %s\n", - AcpiFormatException(rv)); + const int pin = gpio->PinTable[0]; + + if (pin >= __arraycount(asc->sc_event)) { + aprint_error_dev(asc->sc_base.sc_dev, + "ignoring event for pin %u (out of range)\n", pin); return; } - - if (!asc->sc_aei_pins) + if (asc->sc_event[pin] != NULL) { + aprint_error_dev(asc->sc_base.sc_dev, + "ignoring duplicate pin %u\n", pin); return; + } - aprint_verbose_dev(asc->sc_base.sc_dev, "ACPI event pins: %#x\n", asc->sc_aei_pins); - - sc->sc_reserved_mask = asc->sc_aei_pins; + asc->sc_event[pin] = ev; + asc->sc_base.sc_reserved_mask |= __BIT(pin); /* - * For each event pin, find the corresponding event method (_Exx, _Lxx, or _EVT). + * Configure and enable interrupts for this pin. */ - for (pin = 0; pin < __arraycount(asc->sc_aei_res); pin++) { - if ((asc->sc_aei_pins & __BIT(pin)) == 0) - continue; - - gpio = &asc->sc_aei_res[pin]; - - const char trig = gpio->Triggering == ACPI_LEVEL_SENSITIVE ? 'L' : 'E'; - handle = NULL; - snprintf(namebuf, sizeof(namebuf), "_%c%02X", trig, pin); - if (ACPI_FAILURE(AcpiGetHandle(asc->sc_handle, namebuf, &handle))) { - (void)AcpiGetHandle(asc->sc_handle, "_EVT", &handle); - if (handle != NULL) - asc->sc_event[pin].ev_method_evt = true; - } - - if (handle == NULL) - continue; - - asc->sc_event[pin].ev_sc = asc; - asc->sc_event[pin].ev_pin = pin; - asc->sc_event[pin].ev_method = handle; - - /* - * Configure and enable interrupts for this pin. - */ - - ibe = PLGPIO_READ(sc, PL061_GPIOIBE_REG); - iev = PLGPIO_READ(sc, PL061_GPIOIEV_REG); - switch (gpio->Polarity) { - case ACPI_ACTIVE_HIGH: - ibe &= ~__BIT(pin); - iev |= __BIT(pin); - break; - case ACPI_ACTIVE_LOW: - ibe &= ~__BIT(pin); - iev &= ~__BIT(pin); - break; - case ACPI_ACTIVE_BOTH: - ibe |= __BIT(pin); - break; - } - PLGPIO_WRITE(sc, PL061_GPIOIBE_REG, ibe); - PLGPIO_WRITE(sc, PL061_GPIOIEV_REG, iev); - - is = PLGPIO_READ(sc, PL061_GPIOIS_REG); - switch (gpio->Triggering) { - case ACPI_LEVEL_SENSITIVE: - is |= __BIT(pin); - break; - case ACPI_EDGE_SENSITIVE: - is &= ~__BIT(pin); - break; - } - PLGPIO_WRITE(sc, PL061_GPIOIS_REG, is); - - delay(20); - - PLGPIO_WRITE(sc, PL061_GPIOIC_REG, __BIT(pin)); - - ie = PLGPIO_READ(sc, PL061_GPIOIE_REG); - ie |= __BIT(pin); - PLGPIO_WRITE(sc, PL061_GPIOIE_REG, ie); - } -} - -static void -plgpio_acpi_notify(void *priv) -{ - struct plgpio_acpi_event * const ev = priv; - struct plgpio_acpi_softc * const asc = ev->ev_sc; - struct plgpio_softc * const sc = &asc->sc_base; - ACPI_STATUS rv; - if (ev->ev_method_evt) { - ACPI_OBJECT_LIST objs; - ACPI_OBJECT obj[1]; - objs.Count = 1; - objs.Pointer = obj; - obj[0].Type = ACPI_TYPE_INTEGER; - obj[0].Integer.Value = ev->ev_pin; - rv = AcpiEvaluateObject(ev->ev_method, NULL, &objs, NULL); - } else { - rv = AcpiEvaluateObject(ev->ev_method, NULL, NULL, NULL); - } - - if (ACPI_FAILURE(rv)) - device_printf(sc->sc_dev, "failed to handle %s event: %s\n", - acpi_name(ev->ev_method), AcpiFormatException(rv)); + ibe = PLGPIO_READ(sc, PL061_GPIOIBE_REG); + iev = PLGPIO_READ(sc, PL061_GPIOIEV_REG); + switch (gpio->Polarity) { + case ACPI_ACTIVE_HIGH: + ibe &= ~__BIT(pin); + iev |= __BIT(pin); + break; + case ACPI_ACTIVE_LOW: + ibe &= ~__BIT(pin); + iev &= ~__BIT(pin); + break; + case ACPI_ACTIVE_BOTH: + ibe |= __BIT(pin); + break; + } + PLGPIO_WRITE(sc, PL061_GPIOIBE_REG, ibe); + PLGPIO_WRITE(sc, PL061_GPIOIEV_REG, iev); + + is = PLGPIO_READ(sc, PL061_GPIOIS_REG); + switch (gpio->Triggering) { + case ACPI_LEVEL_SENSITIVE: + is |= __BIT(pin); + break; + case ACPI_EDGE_SENSITIVE: + is &= ~__BIT(pin); + break; + } + PLGPIO_WRITE(sc, PL061_GPIOIS_REG, is); + + delay(20); + + PLGPIO_WRITE(sc, PL061_GPIOIC_REG, __BIT(pin)); + + ie = PLGPIO_READ(sc, PL061_GPIOIE_REG); + ie |= __BIT(pin); + PLGPIO_WRITE(sc, PL061_GPIOIE_REG, ie); } static int @@ -302,11 +215,10 @@ plgpio_acpi_intr(void *priv) while ((bit = __builtin_ffs(mis)) != 0) { const int pin = bit - 1; - struct plgpio_acpi_event * const ev = &asc->sc_event[pin]; - - KASSERT(ev->ev_method != NULL); + struct acpi_event * const ev = asc->sc_event[pin]; + KASSERT(ev != NULL); - AcpiOsExecute(OSL_NOTIFY_HANDLER, plgpio_acpi_notify, ev); + acpi_event_notify(ev); mis &= ~__BIT(pin); } Added files: Index: src/sys/dev/acpi/acpi_event.c diff -u /dev/null src/sys/dev/acpi/acpi_event.c:1.1 --- /dev/null Mon Oct 22 22:29:36 2018 +++ src/sys/dev/acpi/acpi_event.c Mon Oct 22 22:29:35 2018 @@ -0,0 +1,181 @@ +/* $NetBSD: acpi_event.c,v 1.1 2018/10/22 22:29:35 jmcneill Exp $ */ + +/*- + * Copyright (c) 2018 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jared McNeill <jmcne...@invisible.ca>. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include <sys/cdefs.h> +__KERNEL_RCSID(0, "$NetBSD: acpi_event.c,v 1.1 2018/10/22 22:29:35 jmcneill Exp $"); + +#include <sys/param.h> +#include <sys/bus.h> +#include <sys/cpu.h> +#include <sys/kmem.h> + +#include <dev/acpi/acpireg.h> +#include <dev/acpi/acpivar.h> +#include <dev/acpi/acpi_event.h> + +struct acpi_event { + device_t ev_dev; + ACPI_HANDLE ev_method; + bool ev_method_evt; + UINT16 ev_data; +}; + +struct acpi_event_gpio_context { + device_t ctx_dev; + ACPI_HANDLE ctx_handle; + void (*ctx_func)(void *, struct acpi_event *, ACPI_RESOURCE_GPIO *); + void *ctx_arg; +}; + +static ACPI_STATUS +acpi_event_create(device_t dev, ACPI_HANDLE handle, UINT16 data, UINT8 trig, struct acpi_event **pev) +{ + struct acpi_event *ev; + char namebuf[5]; + + const char trigchar = trig == ACPI_LEVEL_SENSITIVE ? 'L' : 'E'; + + ev = kmem_alloc(sizeof(*ev), KM_SLEEP); + ev->ev_dev = dev; + ev->ev_method = NULL; + ev->ev_method_evt = false; + ev->ev_data = data; + + if (data <= 255) { + snprintf(namebuf, sizeof(namebuf), "_%c%02X", trigchar, data); + AcpiGetHandle(handle, namebuf, &ev->ev_method); + } + if (ev->ev_method == NULL && ACPI_SUCCESS(AcpiGetHandle(handle, "_EVT", &ev->ev_method))) + ev->ev_method_evt = true; + + if (ev->ev_method == NULL) { + aprint_error_dev(dev, "%s is missing a control method for event %#x (trig %c)\n", + acpi_name(handle), data, trigchar); + kmem_free(ev, sizeof(*ev)); + return AE_NOT_FOUND; + } + + *pev = ev; + + return AE_OK; +} + +static ACPI_STATUS +acpi_event_gpio_resource_cb(ACPI_RESOURCE *res, void *priv) +{ + struct acpi_event_gpio_context *ctx = priv; + struct acpi_event *ev; + ACPI_RESOURCE_GPIO *gpio; + ACPI_STATUS rv; + + if (res->Type != ACPI_RESOURCE_TYPE_GPIO) + return AE_OK; + + gpio = &res->Data.Gpio; + if (gpio->ConnectionType != ACPI_RESOURCE_GPIO_TYPE_INT) + return AE_OK; + if (gpio->PinTableLength != 1) + return AE_OK; + + rv = acpi_event_create(ctx->ctx_dev, ctx->ctx_handle, gpio->PinTable[0], gpio->Triggering, &ev); + if (ACPI_SUCCESS(rv)) + ctx->ctx_func(ctx->ctx_arg, ev, gpio); + + return AE_OK; +} + +ACPI_STATUS +acpi_event_create_gpio(device_t dev, ACPI_HANDLE handle, + void (*func)(void *, struct acpi_event *, ACPI_RESOURCE_GPIO *), void *arg) +{ + struct acpi_event_gpio_context ctx; + + ctx.ctx_dev = dev; + ctx.ctx_handle = handle; + ctx.ctx_func = func; + ctx.ctx_arg = arg; + + return AcpiWalkResources(handle, "_AEI", acpi_event_gpio_resource_cb, &ctx); +} + +ACPI_STATUS +acpi_event_create_int(device_t dev, ACPI_HANDLE handle, + void (*func)(void *, struct acpi_event *, struct acpi_irq *), void *arg) +{ + struct acpi_resources res; + struct acpi_event *ev; + struct acpi_irq *irq; + ACPI_STATUS rv; + int n; + + rv = acpi_resource_parse(dev, handle, "_CRS", &res, + &acpi_resource_parse_ops_default); + if (ACPI_FAILURE(rv)) + return rv; + + for (n = 0; (irq = acpi_res_irq(&res, n)) != NULL; n++) { + rv = acpi_event_create(dev, handle, irq->ar_irq, irq->ar_type, &ev); + if (ACPI_SUCCESS(rv)) + func(arg, ev, irq); + } + + acpi_resource_cleanup(&res); + + return AE_OK; +} + +static void +acpi_event_invoke(void *priv) +{ + struct acpi_event * const ev = priv; + ACPI_OBJECT_LIST objs, *arg = NULL; + ACPI_OBJECT obj[1]; + ACPI_STATUS rv; + + if (ev->ev_method_evt) { + objs.Count = 1; + objs.Pointer = obj; + obj[0].Type = ACPI_TYPE_INTEGER; + obj[0].Integer.Value = ev->ev_data; + arg = &objs; + } + + rv = AcpiEvaluateObject(ev->ev_method, NULL, arg, NULL); + if (ACPI_FAILURE(rv)) + device_printf(ev->ev_dev, "failed to handle %s event: %s\n", + acpi_name(ev->ev_method), AcpiFormatException(rv)); +} + +ACPI_STATUS +acpi_event_notify(struct acpi_event *ev) +{ + return AcpiOsExecute(OSL_NOTIFY_HANDLER, acpi_event_invoke, ev); +} Index: src/sys/dev/acpi/acpi_event.h diff -u /dev/null src/sys/dev/acpi/acpi_event.h:1.1 --- /dev/null Mon Oct 22 22:29:36 2018 +++ src/sys/dev/acpi/acpi_event.h Mon Oct 22 22:29:35 2018 @@ -0,0 +1,43 @@ +/* $NetBSD: acpi_event.h,v 1.1 2018/10/22 22:29:35 jmcneill Exp $ */ + +/*- + * Copyright (c) 2018 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jared McNeill <jmcne...@invisible.ca>. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef _DEV_ACPI_ACPI_EVENT_H +#define _DEV_ACPI_ACPI_EVENT_H + +struct acpi_event; + +ACPI_STATUS acpi_event_create_gpio(device_t, ACPI_HANDLE, + void (*)(void *, struct acpi_event *, ACPI_RESOURCE_GPIO *), void *); +ACPI_STATUS acpi_event_create_int(device_t, ACPI_HANDLE, + void (*)(void *, struct acpi_event *, struct acpi_irq *), void *); +ACPI_STATUS acpi_event_notify(struct acpi_event *); + +#endif /* !_DEV_ACPI_ACPI_EVENT_H */