Home | History | Annotate | Line # | Download | only in acpi
amdgpio.c revision 1.2
      1 /* $NetBSD: amdgpio.c,v 1.2 2025/02/26 21:49:11 jmcneill Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2024 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jared McNeill <jmcneill (at) invisible.ca>.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: amdgpio.c,v 1.2 2025/02/26 21:49:11 jmcneill Exp $");
     34 
     35 #include <sys/param.h>
     36 #include <sys/bus.h>
     37 #include <sys/cpu.h>
     38 #include <sys/device.h>
     39 #include <sys/gpio.h>
     40 #include <sys/kmem.h>
     41 #include <sys/mutex.h>
     42 #include <sys/queue.h>
     43 
     44 #include <dev/acpi/acpireg.h>
     45 #include <dev/acpi/acpivar.h>
     46 #include <dev/acpi/acpi_event.h>
     47 #include <dev/acpi/acpi_gpio.h>
     48 #include <dev/acpi/acpi_intr.h>
     49 #include <dev/acpi/amdgpioreg.h>
     50 
     51 #include <dev/gpio/gpiovar.h>
     52 
     53 struct amdgpio_config {
     54 	u_int	num_pins;
     55 	int	(*translate)(ACPI_RESOURCE_GPIO *);
     56 };
     57 
     58 struct amdgpio_intr_handler {
     59 	int	(*ih_func)(void *);
     60 	void	*ih_arg;
     61 	int	ih_pin;
     62 	LIST_ENTRY(amdgpio_intr_handler) ih_list;
     63 };
     64 
     65 struct amdgpio_softc {
     66 	device_t			sc_dev;
     67 	device_t			sc_gpiodev;
     68 	bus_space_handle_t		sc_bsh;
     69 	bus_space_tag_t			sc_bst;
     70 	const struct amdgpio_config	*sc_config;
     71 	struct gpio_chipset_tag		sc_gc;
     72 	gpio_pin_t			*sc_pins;
     73 	LIST_HEAD(, amdgpio_intr_handler) sc_intrs;
     74 	kmutex_t			sc_lock;
     75 };
     76 
     77 #define RD4(sc, reg)		\
     78 	bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
     79 #define WR4(sc, reg, val)	\
     80 	bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
     81 
     82 static int	amdgpio_match(device_t, cfdata_t, void *);
     83 static void	amdgpio_attach(device_t, device_t, void *);
     84 
     85 static int	amdgpio_pin_read(void *, int);
     86 static void	amdgpio_pin_write(void *, int, int);
     87 static void	amdgpio_pin_ctl(void *, int, int);
     88 static void *	amdgpio_intr_establish(void *, int, int, int,
     89 					int (*)(void *), void *);
     90 static void	amdgpio_intr_disestablish(void *, void *);
     91 static bool	amdgpio_intr_str(void *, int, int, char *, size_t);
     92 static void	amdgpio_intr_mask(void *, void *);
     93 static void	amdgpio_intr_unmask(void *, void *);
     94 
     95 static int	amdgpio_acpi_translate(void *, ACPI_RESOURCE_GPIO *, void **);
     96 static void	amdgpio_register_event(void *, struct acpi_event *,
     97 					ACPI_RESOURCE_GPIO *);
     98 static int	amdgpio_intr(void *);
     99 
    100 CFATTACH_DECL_NEW(amdgpio, sizeof(struct amdgpio_softc),
    101     amdgpio_match, amdgpio_attach, NULL, NULL);
    102 
    103 #define AMDGPIO_NUM_PINS	184
    104 
    105 static int
    106 amdgpio_translate(ACPI_RESOURCE_GPIO *gpio)
    107 {
    108 	const ACPI_INTEGER pin = gpio->PinTable[0];
    109 
    110 	if (pin < AMDGPIO_NUM_PINS) {
    111 		return gpio->PinTable[0];
    112 	}
    113 
    114 	switch (pin) {
    115 	case 0x0:
    116 	case 0x8: /* TPDD */
    117 	case 0x28: /* TPNL */
    118 	case 0x3a:
    119 	case 0x3b:
    120 	case 0x3d:
    121 	case 0x3e:
    122 		return pin;
    123 	default:
    124 		return -1;
    125 	}
    126 }
    127 
    128 static struct amdgpio_config amdgpio_config = {
    129 	.num_pins = AMDGPIO_NUM_PINS,
    130 	.translate = amdgpio_translate, /* TODO: REMOVE */
    131 };
    132 
    133 static const struct device_compatible_entry compat_data[] = {
    134 	{ .compat = "AMDI0030",	.data = &amdgpio_config },
    135 	DEVICE_COMPAT_EOL
    136 };
    137 
    138 static int
    139 amdgpio_match(device_t parent, cfdata_t cf, void *aux)
    140 {
    141 	struct acpi_attach_args *aa = aux;
    142 
    143 	return acpi_compatible_match(aa, compat_data);
    144 }
    145 
    146 static void
    147 amdgpio_attach(device_t parent, device_t self, void *aux)
    148 {
    149 	struct amdgpio_softc * const sc = device_private(self);
    150 	struct acpi_attach_args *aa = aux;
    151 	struct gpiobus_attach_args gba;
    152 	ACPI_HANDLE hdl = aa->aa_node->ad_handle;
    153 	struct acpi_resources res;
    154 	struct acpi_mem *mem;
    155 	struct acpi_irq *irq;
    156 	ACPI_STATUS rv;
    157 	int error, pin;
    158 	void *ih;
    159 
    160 	sc->sc_dev = self;
    161 	sc->sc_config = acpi_compatible_lookup(aa, compat_data)->data;
    162 	sc->sc_bst = aa->aa_memt;
    163 	KASSERT(sc->sc_config != NULL);
    164 	LIST_INIT(&sc->sc_intrs);
    165 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_VM);
    166 
    167 	rv = acpi_resource_parse(sc->sc_dev, hdl, "_CRS",
    168 	    &res, &acpi_resource_parse_ops_default);
    169 	if (ACPI_FAILURE(rv)) {
    170 		return;
    171 	}
    172 
    173 	mem = acpi_res_mem(&res, 0);
    174 	if (mem == NULL) {
    175 		aprint_error_dev(self, "couldn't find mem resource\n");
    176 		goto done;
    177 	}
    178 
    179 	irq = acpi_res_irq(&res, 0);
    180 	if (irq == NULL) {
    181 		aprint_error_dev(self, "couldn't find irq resource\n");
    182 		goto done;
    183 	}
    184 
    185 	error = bus_space_map(sc->sc_bst, mem->ar_base, mem->ar_length, 0,
    186 	    &sc->sc_bsh);
    187 	if (error) {
    188 		aprint_error_dev(self, "couldn't map registers\n");
    189 		goto done;
    190 	}
    191 
    192 	sc->sc_pins = kmem_zalloc(sizeof(*sc->sc_pins) *
    193 	    sc->sc_config->num_pins, KM_SLEEP);
    194 	for (pin = 0; pin < sc->sc_config->num_pins; pin++) {
    195 		sc->sc_pins[pin].pin_num = pin;
    196 		sc->sc_pins[pin].pin_caps = GPIO_PIN_INPUT | GPIO_PIN_OUTPUT;
    197 		sc->sc_pins[pin].pin_intrcaps =
    198 		    GPIO_INTR_POS_EDGE | GPIO_INTR_NEG_EDGE |
    199 		    GPIO_INTR_DOUBLE_EDGE | GPIO_INTR_HIGH_LEVEL |
    200 		    GPIO_INTR_LOW_LEVEL | GPIO_INTR_MPSAFE;
    201 		/* It's not safe to read all pins, so leave pin state unknown */
    202 		sc->sc_pins[pin].pin_state = 0;
    203 	}
    204 
    205 	sc->sc_gc.gp_cookie = sc;
    206 	sc->sc_gc.gp_pin_read = amdgpio_pin_read;
    207 	sc->sc_gc.gp_pin_write = amdgpio_pin_write;
    208 	sc->sc_gc.gp_pin_ctl = amdgpio_pin_ctl;
    209 	sc->sc_gc.gp_intr_establish = amdgpio_intr_establish;
    210 	sc->sc_gc.gp_intr_disestablish = amdgpio_intr_disestablish;
    211 	sc->sc_gc.gp_intr_str = amdgpio_intr_str;
    212 	sc->sc_gc.gp_intr_mask = amdgpio_intr_mask;
    213 	sc->sc_gc.gp_intr_unmask = amdgpio_intr_unmask;
    214 
    215 	rv = acpi_event_create_gpio(self, hdl, amdgpio_register_event, sc);
    216 	if (ACPI_FAILURE(rv)) {
    217 		if (rv != AE_NOT_FOUND) {
    218 			aprint_error_dev(self, "failed to create events: %s\n",
    219 			    AcpiFormatException(rv));
    220 		}
    221 		goto done;
    222 	}
    223 
    224 	ih = acpi_intr_establish(self, (uint64_t)(uintptr_t)hdl,
    225 	    IPL_VM, false, amdgpio_intr, sc, device_xname(self));
    226 	if (ih == NULL) {
    227 		aprint_error_dev(self, "couldn't establish interrupt\n");
    228 		goto done;
    229 	}
    230 
    231 	memset(&gba, 0, sizeof(gba));
    232 	gba.gba_gc = &sc->sc_gc;
    233 	gba.gba_pins = sc->sc_pins;
    234 	gba.gba_npins = sc->sc_config->num_pins;
    235 	sc->sc_gpiodev = config_found(self, &gba, gpiobus_print,
    236 	    CFARGS(.iattr = "gpiobus"));
    237 	if (sc->sc_gpiodev != NULL) {
    238 		acpi_gpio_register(aa->aa_node, self,
    239 		    amdgpio_acpi_translate, sc);
    240 	}
    241 
    242 done:
    243 	acpi_resource_cleanup(&res);
    244 }
    245 
    246 static int
    247 amdgpio_acpi_translate(void *priv, ACPI_RESOURCE_GPIO *gpio, void **gpiop)
    248 {
    249 	struct amdgpio_softc * const sc = priv;
    250 	const ACPI_INTEGER pin = gpio->PinTable[0];
    251 	int xpin;
    252 
    253 	xpin = sc->sc_config->translate(gpio);
    254 
    255 	aprint_debug_dev(sc->sc_dev, "translate %#lx -> %u\n", pin, xpin);
    256 
    257 	if (gpiop != NULL) {
    258 		if (sc->sc_gpiodev != NULL) {
    259 			*gpiop = device_private(sc->sc_gpiodev);
    260 		} else {
    261 			device_printf(sc->sc_dev,
    262 			    "no gpiodev for pin %#lx -> %u\n", pin, xpin);
    263 			xpin = -1;
    264 		}
    265 	}
    266 
    267 	return xpin;
    268 }
    269 
    270 static int
    271 amdgpio_acpi_event(void *priv)
    272 {
    273 	struct acpi_event * const ev = priv;
    274 
    275 	acpi_event_notify(ev);
    276 
    277 	return 1;
    278 }
    279 
    280 static void
    281 amdgpio_register_event(void *priv, struct acpi_event *ev,
    282     ACPI_RESOURCE_GPIO *gpio)
    283 {
    284 	struct amdgpio_softc * const sc = priv;
    285 	int irqmode;
    286 	void *ih;
    287 
    288 	const int pin = amdgpio_acpi_translate(sc, gpio, NULL);
    289 
    290 	if (pin < 0 || pin == 0x8) {
    291 		aprint_error_dev(sc->sc_dev,
    292 		    "ignoring event for pin %#x (out of range)\n",
    293 		    gpio->PinTable[0]);
    294 		return;
    295 	}
    296 
    297 	if (gpio->Triggering == ACPI_LEVEL_SENSITIVE) {
    298 		irqmode = gpio->Polarity == ACPI_ACTIVE_HIGH ?
    299 		    GPIO_INTR_HIGH_LEVEL : GPIO_INTR_LOW_LEVEL;
    300 	} else {
    301 		KASSERT(gpio->Triggering == ACPI_EDGE_SENSITIVE);
    302 		if (gpio->Polarity == ACPI_ACTIVE_LOW) {
    303 			irqmode = GPIO_INTR_NEG_EDGE;
    304 		} else if (gpio->Polarity == ACPI_ACTIVE_HIGH) {
    305 			irqmode = GPIO_INTR_POS_EDGE;
    306 		} else {
    307 			KASSERT(gpio->Polarity == ACPI_ACTIVE_BOTH);
    308 			irqmode = GPIO_INTR_DOUBLE_EDGE;
    309 		}
    310 	}
    311 
    312 	ih = amdgpio_intr_establish(sc, pin, IPL_VM, irqmode,
    313 	    amdgpio_acpi_event, ev);
    314 	if (ih == NULL) {
    315 		aprint_error_dev(sc->sc_dev,
    316 		    "couldn't register event for pin %#x\n",
    317 		    gpio->PinTable[0]);
    318 		return;
    319 	}
    320 	if (gpio->Triggering == ACPI_LEVEL_SENSITIVE) {
    321 		acpi_event_set_intrcookie(ev, ih);
    322 	}
    323 }
    324 
    325 static int
    326 amdgpio_pin_read(void *priv, int pin)
    327 {
    328 	struct amdgpio_softc * const sc = priv;
    329 	uint32_t val;
    330 
    331 	if (pin < 0 || pin >= sc->sc_config->num_pins) {
    332 		return 0;
    333 	}
    334 	if ((sc->sc_pins[pin].pin_caps & GPIO_PIN_INPUT) == 0) {
    335 		return 0;
    336 	}
    337 
    338 	val = RD4(sc, AMDGPIO_PIN_REG(pin));
    339 	return (val & AMDGPIO_CONF_GPIORXSTATE) ? 1 : 0;
    340 }
    341 
    342 static void
    343 amdgpio_pin_write(void *priv, int pin, int pinval)
    344 {
    345 	struct amdgpio_softc * const sc = priv;
    346 	uint32_t val;
    347 
    348 	if (pin < 0 || pin >= sc->sc_config->num_pins) {
    349 		return;
    350 	}
    351 	if ((sc->sc_pins[pin].pin_caps & GPIO_PIN_OUTPUT) == 0) {
    352 		return;
    353 	}
    354 
    355 	val = RD4(sc, AMDGPIO_PIN_REG(pin));
    356 	if (pinval) {
    357 		val |= AMDGPIO_CONF_GPIOTXSTATE;
    358 	} else {
    359 		val &= ~AMDGPIO_CONF_GPIOTXSTATE;
    360 	}
    361 	WR4(sc, AMDGPIO_PIN_REG(pin), val);
    362 }
    363 
    364 static void
    365 amdgpio_pin_ctl(void *priv, int pin, int flags)
    366 {
    367 	/* Nothing to do here, as firmware has already configured pins. */
    368 }
    369 
    370 static void *
    371 amdgpio_intr_establish(void *priv, int pin, int ipl, int irqmode,
    372 			int (*func)(void *), void *arg)
    373 {
    374 	struct amdgpio_softc * const sc = priv;
    375 	struct amdgpio_intr_handler *aih, *aihp;
    376 	uint32_t dect;
    377 	uint32_t val;
    378 
    379 	if (pin < 0 || pin >= sc->sc_config->num_pins) {
    380 		return NULL;
    381 	}
    382 	if (ipl != IPL_VM) {
    383 		device_printf(sc->sc_dev, "%s: only IPL_VM supported\n",
    384 		    __func__);
    385 		return NULL;
    386 	}
    387 
    388 	aih = kmem_alloc(sizeof(*aih), KM_SLEEP);
    389 	aih->ih_func = func;
    390 	aih->ih_arg = arg;
    391 	aih->ih_pin = pin;
    392 
    393 	mutex_enter(&sc->sc_lock);
    394 
    395 	LIST_FOREACH(aihp, &sc->sc_intrs, ih_list) {
    396 		if (aihp->ih_pin == aih->ih_pin) {
    397 			mutex_exit(&sc->sc_lock);
    398 			kmem_free(aih, sizeof(*aih));
    399 			device_printf(sc->sc_dev,
    400 			    "%s: pin %d already establish\n", __func__, pin);
    401 			return NULL;
    402 		}
    403 	}
    404 
    405 	LIST_INSERT_HEAD(&sc->sc_intrs, aih, ih_list);
    406 
    407 	if ((irqmode & GPIO_INTR_LEVEL_MASK) != 0) {
    408 		dect = AMDGPIO_CONF_LEVEL;
    409 	} else {
    410 		KASSERT((irqmode & GPIO_INTR_EDGE_MASK) != 0);
    411 		if ((irqmode & GPIO_INTR_NEG_EDGE) != 0) {
    412 			dect = AMDGPIO_CONF_ACTLO;
    413 		} else if ((irqmode & GPIO_INTR_POS_EDGE) != 0) {
    414 			dect = 0;
    415 		} else {
    416 			KASSERT((irqmode & GPIO_INTR_DOUBLE_EDGE) != 0);
    417 			dect = AMDGPIO_CONF_ACTBOTH;
    418 		}
    419 	}
    420 
    421 	val = RD4(sc, AMDGPIO_PIN_REG(pin));
    422 	val |= dect;
    423 	val |= AMDGPIO_CONF_INTR_MASK_EN | AMDGPIO_CONF_INTR_EN;
    424 	WR4(sc, AMDGPIO_PIN_REG(pin), val);
    425 
    426 	mutex_exit(&sc->sc_lock);
    427 
    428 	return aih;
    429 }
    430 
    431 static void
    432 amdgpio_intr_disestablish(void *priv, void *ih)
    433 {
    434 	struct amdgpio_softc * const sc = priv;
    435 	struct amdgpio_intr_handler *aih = ih;
    436 	uint32_t val;
    437 
    438 	mutex_enter(&sc->sc_lock);
    439 
    440 	LIST_REMOVE(aih, ih_list);
    441 
    442 	val = RD4(sc, AMDGPIO_PIN_REG(aih->ih_pin));
    443 	val &= ~(AMDGPIO_CONF_INTR_EN | AMDGPIO_CONF_INTR_MASK_EN);
    444 	WR4(sc, AMDGPIO_PIN_REG(aih->ih_pin), val);
    445 
    446 	mutex_exit(&sc->sc_lock);
    447 
    448 	kmem_free(aih, sizeof(*aih));
    449 }
    450 
    451 static bool
    452 amdgpio_intr_str(void *priv, int pin, int irqmode, char *buf, size_t buflen)
    453 {
    454 	struct amdgpio_softc * const sc = priv;
    455 	int rv;
    456 
    457 	rv = snprintf(buf, buflen, "%s pin %d", device_xname(sc->sc_dev), pin);
    458 
    459 	return rv < buflen;
    460 }
    461 
    462 static void
    463 amdgpio_intr_mask(void *priv, void *ih)
    464 {
    465 	struct amdgpio_softc * const sc = priv;
    466 	struct amdgpio_intr_handler *aih = ih;
    467 	uint32_t val;
    468 
    469 	val = RD4(sc, AMDGPIO_PIN_REG(aih->ih_pin));
    470 	val &= ~AMDGPIO_CONF_INTR_MASK_EN;
    471 	WR4(sc, AMDGPIO_PIN_REG(aih->ih_pin), val);
    472 }
    473 
    474 static void
    475 amdgpio_intr_unmask(void *priv, void *ih)
    476 {
    477 	struct amdgpio_softc * const sc = priv;
    478 	struct amdgpio_intr_handler *aih = ih;
    479 	uint32_t val;
    480 
    481 	val = RD4(sc, AMDGPIO_PIN_REG(aih->ih_pin));
    482 	val |= AMDGPIO_CONF_INTR_MASK_EN;
    483 	WR4(sc, AMDGPIO_PIN_REG(aih->ih_pin), val);
    484 }
    485 
    486 static int
    487 amdgpio_intr(void *priv)
    488 {
    489 	struct amdgpio_softc * const sc = priv;
    490 	struct amdgpio_intr_handler *aih;
    491 	int rv = 0;
    492 	uint64_t status;
    493 	uint32_t val;
    494 
    495 	mutex_enter(&sc->sc_lock);
    496 
    497 	status = RD4(sc, AMDGPIO_INTR_STATUS(1));
    498 	status <<= 32;
    499 	status |= RD4(sc, AMDGPIO_INTR_STATUS(0));
    500 	status &= __BITS(0, AMDGPIO_INTR_STATUS_NBITS - 1);
    501 
    502 	if (status == 0) {
    503 		rv = 1;
    504 		goto out;
    505 	}
    506 
    507 	LIST_FOREACH(aih, &sc->sc_intrs, ih_list) {
    508 		const int pin = aih->ih_pin;
    509 
    510 		if ((status & __BIT(pin / 4)) == 0) {
    511 			continue;
    512 		}
    513 
    514 		val = RD4(sc, AMDGPIO_PIN_REG(pin));
    515 		if ((val & AMDGPIO_CONF_INTR_STATUS) != 0) {
    516 			rv |= aih->ih_func(aih->ih_arg);
    517 
    518 			val &= ~(AMDGPIO_CONF_INTR_MASK_EN | AMDGPIO_CONF_INTR_EN);
    519 			WR4(sc, AMDGPIO_PIN_REG(pin), val);
    520 		}
    521 	}
    522 
    523 	/* Signal end of interrupt */
    524 	val = RD4(sc, AMDGPIO_INTR_MASTER);
    525 	val |= AMDGPIO_INTR_MASTER_EIO;
    526 	WR4(sc, AMDGPIO_INTR_MASTER, val);
    527 
    528 out:
    529 	mutex_exit(&sc->sc_lock);
    530 
    531 	return rv;
    532 }
    533