Home | History | Annotate | Line # | Download | only in fdt
gicv3_fdt.c revision 1.12
      1 /* $NetBSD: gicv3_fdt.c,v 1.12 2021/01/19 00:40:17 thorpej Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2015-2018 Jared McNeill <jmcneill (at) invisible.ca>
      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  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  */
     28 
     29 #include "pci.h"
     30 
     31 #define	_INTR_PRIVATE
     32 
     33 #include <sys/cdefs.h>
     34 __KERNEL_RCSID(0, "$NetBSD: gicv3_fdt.c,v 1.12 2021/01/19 00:40:17 thorpej Exp $");
     35 
     36 #include <sys/param.h>
     37 #include <sys/bus.h>
     38 #include <sys/device.h>
     39 #include <sys/intr.h>
     40 #include <sys/systm.h>
     41 #include <sys/kernel.h>
     42 #include <sys/lwp.h>
     43 #include <sys/kmem.h>
     44 #include <sys/queue.h>
     45 
     46 #include <dev/fdt/fdtvar.h>
     47 
     48 #include <arm/cortex/gicv3.h>
     49 #include <arm/cortex/gicv3_its.h>
     50 #include <arm/cortex/gic_reg.h>
     51 #include <arm/cortex/gic_v2m.h>
     52 
     53 #define	GICV3_MAXIRQ	1020
     54 
     55 #define	IRQ_PPI(n)	((n) + 16)
     56 #define	IRQ_SPI(n)	((n) + 32)
     57 
     58 struct gicv3_fdt_softc;
     59 struct gicv3_fdt_irq;
     60 
     61 static int	gicv3_fdt_match(device_t, cfdata_t, void *);
     62 static void	gicv3_fdt_attach(device_t, device_t, void *);
     63 
     64 static int	gicv3_fdt_map_registers(struct gicv3_fdt_softc *);
     65 #if NPCI > 0 && defined(__HAVE_PCI_MSI_MSIX)
     66 static void	gicv3_fdt_attach_mbi(struct gicv3_fdt_softc *);
     67 static void	gicv3_fdt_attach_its(struct gicv3_fdt_softc *, bus_space_tag_t, int);
     68 #endif
     69 
     70 static int	gicv3_fdt_intr(void *);
     71 
     72 static void *	gicv3_fdt_establish(device_t, u_int *, int, int,
     73 		    int (*)(void *), void *, const char *);
     74 static void	gicv3_fdt_disestablish(device_t, void *);
     75 static bool	gicv3_fdt_intrstr(device_t, u_int *, char *, size_t);
     76 
     77 struct fdtbus_interrupt_controller_func gicv3_fdt_funcs = {
     78 	.establish = gicv3_fdt_establish,
     79 	.disestablish = gicv3_fdt_disestablish,
     80 	.intrstr = gicv3_fdt_intrstr
     81 };
     82 
     83 struct gicv3_fdt_irqhandler {
     84 	struct gicv3_fdt_irq	*ih_irq;
     85 	int			(*ih_fn)(void *);
     86 	void			*ih_arg;
     87 	bool			ih_mpsafe;
     88 	TAILQ_ENTRY(gicv3_fdt_irqhandler) ih_next;
     89 };
     90 
     91 struct gicv3_fdt_irq {
     92 	struct gicv3_fdt_softc	*intr_sc;
     93 	void			*intr_ih;
     94 	void			*intr_arg;
     95 	int			intr_refcnt;
     96 	int			intr_ipl;
     97 	int			intr_level;
     98 	int			intr_mpsafe;
     99 	TAILQ_HEAD(, gicv3_fdt_irqhandler) intr_handlers;
    100 	int			intr_irq;
    101 };
    102 
    103 struct gicv3_fdt_softc {
    104 	struct gicv3_softc	sc_gic;
    105 	int			sc_phandle;
    106 
    107 	struct gicv3_fdt_irq	*sc_irq[GICV3_MAXIRQ];
    108 };
    109 
    110 static const struct device_compatible_entry gicv3_fdt_quirks[] = {
    111 	{ .compat = "rockchip,rk3399",		.value = GICV3_QUIRK_RK3399 },
    112 	{ 0 }
    113 };
    114 
    115 CFATTACH_DECL_NEW(gicv3_fdt, sizeof(struct gicv3_fdt_softc),
    116 	gicv3_fdt_match, gicv3_fdt_attach, NULL, NULL);
    117 
    118 static const struct device_compatible_entry compat_data[] = {
    119 	{ .compat = "arm,gic-v3" },
    120 	{ 0 }
    121 };
    122 
    123 static int
    124 gicv3_fdt_match(device_t parent, cfdata_t cf, void *aux)
    125 {
    126 	struct fdt_attach_args * const faa = aux;
    127 	const int phandle = faa->faa_phandle;
    128 
    129 	return of_match_compat_data(phandle, compat_data);
    130 }
    131 
    132 static void
    133 gicv3_fdt_attach(device_t parent, device_t self, void *aux)
    134 {
    135 	struct gicv3_fdt_softc * const sc = device_private(self);
    136 	struct fdt_attach_args * const faa = aux;
    137 	const int phandle = faa->faa_phandle;
    138 	int error;
    139 
    140 	error = fdtbus_register_interrupt_controller(self, phandle,
    141 	    &gicv3_fdt_funcs);
    142 	if (error) {
    143 		aprint_error(": couldn't register with fdtbus: %d\n", error);
    144 		return;
    145 	}
    146 
    147 	aprint_naive("\n");
    148 	aprint_normal(": GICv3\n");
    149 
    150 	sc->sc_phandle = phandle;
    151 	sc->sc_gic.sc_dev = self;
    152 	sc->sc_gic.sc_bst = faa->faa_bst;
    153 	sc->sc_gic.sc_dmat = faa->faa_dmat;
    154 
    155 	error = gicv3_fdt_map_registers(sc);
    156 	if (error) {
    157 		aprint_error_dev(self, "couldn't map registers\n");
    158 		return;
    159 	}
    160 
    161 	aprint_debug_dev(self, "%d redistributors\n", sc->sc_gic.sc_bsh_r_count);
    162 
    163 	/* Apply quirks */
    164 	const struct device_compatible_entry *dce =
    165 	    of_search_compatible(OF_finddevice("/"), gicv3_fdt_quirks);
    166 	if (dce != NULL) {
    167 		sc->sc_gic.sc_quirks |= dce->value;
    168 	}
    169 
    170 	error = gicv3_init(&sc->sc_gic);
    171 	if (error) {
    172 		aprint_error_dev(self, "failed to initialize GIC: %d\n", error);
    173 		return;
    174 	}
    175 
    176 #if NPCI > 0 && defined(__HAVE_PCI_MSI_MSIX)
    177 	if (of_hasprop(phandle, "msi-controller")) {
    178 		/* Message Based Interrupts */
    179 		gicv3_fdt_attach_mbi(sc);
    180 	} else {
    181 		/* Interrupt Translation Services */
    182 		static const struct device_compatible_entry its_compat[] = {
    183 			{ .compat = "arm,gic-v3-its" },
    184 			{ 0 }
    185 		};
    186 
    187 		for (int child = OF_child(phandle); child;
    188 		     child = OF_peer(child)) {
    189 			if (!fdtbus_status_okay(child))
    190 				continue;
    191 			if (of_match_compat_data(child, its_compat))
    192 				gicv3_fdt_attach_its(sc, faa->faa_bst, child);
    193 		}
    194 	}
    195 #endif
    196 
    197 	arm_fdt_irq_set_handler(gicv3_irq_handler);
    198 }
    199 
    200 static int
    201 gicv3_fdt_map_registers(struct gicv3_fdt_softc *sc)
    202 {
    203 	struct gicv3_softc *gic = &sc->sc_gic;
    204 	const int phandle = sc->sc_phandle;
    205 	u_int redistributor_regions, redistributor_stride;
    206 	bus_space_handle_t bsh;
    207 	bus_size_t size, region_off;
    208 	bus_addr_t addr;
    209 	size_t reg_off;
    210 	int n, r, max_redist, redist;
    211 
    212 	if (of_getprop_uint32(phandle, "#redistributor-regions", &redistributor_regions))
    213 		redistributor_regions = 1;
    214 	if (of_getprop_uint32(phandle, "redistributor-stride", &redistributor_stride))
    215 		redistributor_stride = 0x20000;
    216 
    217 	/*
    218 	 * Map GIC Distributor interface (GICD)
    219 	 */
    220 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
    221 		aprint_error_dev(gic->sc_dev, "couldn't get distributor registers\n");
    222 		return ENXIO;
    223 	}
    224 	if (bus_space_map(sc->sc_gic.sc_bst, addr, size, 0, &sc->sc_gic.sc_bsh_d) != 0) {
    225 		aprint_error_dev(gic->sc_dev, "couldn't map distributor registers\n");
    226 		return ENXIO;
    227 	}
    228 
    229 	/*
    230 	 * GIC Redistributors (GICR)
    231 	 */
    232 	for (reg_off = 1, max_redist = 0, n = 0; n < redistributor_regions; n++, reg_off++) {
    233 		if (fdtbus_get_reg(phandle, reg_off, NULL, &size) != 0) {
    234 			aprint_error_dev(gic->sc_dev, "couldn't get redistributor registers\n");
    235 			return ENXIO;
    236 		}
    237 		max_redist += howmany(size, redistributor_stride);
    238 	}
    239 	gic->sc_bsh_r = kmem_alloc(sizeof(bus_space_handle_t) * max_redist, KM_SLEEP);
    240 	for (reg_off = 1, redist = 0, n = 0; n < redistributor_regions; n++, reg_off++) {
    241 		if (fdtbus_get_reg(phandle, reg_off, &addr, &size) != 0) {
    242 			aprint_error_dev(gic->sc_dev, "couldn't get redistributor registers\n");
    243 			return ENXIO;
    244 		}
    245 		if (bus_space_map(sc->sc_gic.sc_bst, addr, size, 0, &bsh) != 0) {
    246 			aprint_error_dev(gic->sc_dev, "couldn't map redistributor registers\n");
    247 			return ENXIO;
    248 		}
    249 		const int count = howmany(size, redistributor_stride);
    250 		for (r = 0, region_off = 0; r < count; r++, region_off += redistributor_stride) {
    251 			if (bus_space_subregion(sc->sc_gic.sc_bst, bsh, region_off, redistributor_stride, &gic->sc_bsh_r[redist++]) != 0) {
    252 				aprint_error_dev(gic->sc_dev, "couldn't subregion redistributor registers\n");
    253 				return ENXIO;
    254 			}
    255 
    256 			/* If this is the last redist in this region, skip to the next one */
    257 			const uint32_t typer = bus_space_read_4(sc->sc_gic.sc_bst, gic->sc_bsh_r[redist - 1], GICR_TYPER);
    258 			if (typer & GICR_TYPER_Last)
    259 				break;
    260 		}
    261 	}
    262 	gic->sc_bsh_r_count = redist;
    263 
    264 	return 0;
    265 }
    266 
    267 #if NPCI > 0 && defined(__HAVE_PCI_MSI_MSIX)
    268 static void
    269 gicv3_fdt_attach_mbi(struct gicv3_fdt_softc *sc)
    270 {
    271 	struct gic_v2m_frame *frame;
    272 	const u_int *ranges;
    273 	bus_addr_t addr;
    274 	int len, frame_count;
    275 
    276 	if (of_hasprop(sc->sc_phandle, "mbi-alias")) {
    277 		aprint_error_dev(sc->sc_gic.sc_dev, "'mbi-alias' property not supported\n");
    278 		return;
    279 	}
    280 
    281 	if (fdtbus_get_reg(sc->sc_phandle, 0, &addr, NULL) != 0)
    282 		return;
    283 
    284 	ranges = fdtbus_get_prop(sc->sc_phandle, "mbi-ranges", &len);
    285 	if (ranges == NULL) {
    286 		aprint_error_dev(sc->sc_gic.sc_dev, "missing 'mbi-ranges' property\n");
    287 		return;
    288 	}
    289 
    290 	frame_count = 0;
    291 	while (len >= 8) {
    292 		const u_int base_spi = be32dec(&ranges[0]);
    293 		const u_int num_spis = be32dec(&ranges[1]);
    294 
    295 		frame = kmem_zalloc(sizeof(*frame), KM_SLEEP);
    296 		frame->frame_reg = addr;
    297 		frame->frame_pic = pic_list[0];
    298 		frame->frame_base = base_spi;
    299 		frame->frame_count = num_spis;
    300 
    301 		if (gic_v2m_init(frame, sc->sc_gic.sc_dev, frame_count++) != 0) {
    302 			aprint_error_dev(sc->sc_gic.sc_dev, "failed to initialize MBI frame\n");
    303 		} else {
    304 			aprint_normal_dev(sc->sc_gic.sc_dev, "MBI frame @ %#" PRIx64
    305 			    ", SPIs %u-%u\n", frame->frame_reg,
    306 			    frame->frame_base, frame->frame_base + frame->frame_count - 1);
    307 		}
    308 
    309 		ranges += 2;
    310 		len -= 8;
    311 	}
    312 }
    313 
    314 static void
    315 gicv3_fdt_attach_its(struct gicv3_fdt_softc *sc, bus_space_tag_t bst, int phandle)
    316 {
    317 	bus_space_handle_t bsh;
    318 	bus_addr_t addr;
    319 	bus_size_t size;
    320 
    321 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
    322 		aprint_error_dev(sc->sc_gic.sc_dev, "couldn't get ITS address\n");
    323 		return;
    324 	}
    325 
    326 	if (bus_space_map(bst, addr, size, 0, &bsh) != 0) {
    327 		aprint_error_dev(sc->sc_gic.sc_dev, "couldn't map ITS\n");
    328 		return;
    329 	}
    330 
    331 	gicv3_its_init(&sc->sc_gic, bsh, addr, 0);
    332 
    333 	aprint_verbose_dev(sc->sc_gic.sc_dev, "ITS @ %#" PRIxBUSADDR "\n",
    334 	    addr);
    335 }
    336 #endif
    337 
    338 static void *
    339 gicv3_fdt_establish(device_t dev, u_int *specifier, int ipl, int flags,
    340     int (*func)(void *), void *arg, const char *xname)
    341 {
    342 	struct gicv3_fdt_softc * const sc = device_private(dev);
    343 	struct gicv3_fdt_irq *firq;
    344 	struct gicv3_fdt_irqhandler *firqh;
    345 
    346 	/* 1st cell is the interrupt type; 0 is SPI, 1 is PPI */
    347 	/* 2nd cell is the interrupt number */
    348 	/* 3rd cell is flags */
    349 	/* 4th cell is affinity */
    350 
    351 	const u_int type = be32toh(specifier[0]);
    352 	const u_int intr = be32toh(specifier[1]);
    353 	const u_int irq = type == 0 ? IRQ_SPI(intr) : IRQ_PPI(intr);
    354 	const u_int trig = be32toh(specifier[2]) & 0xf;
    355 	const u_int level = (trig & FDT_INTR_TYPE_DOUBLE_EDGE)
    356 	    ? IST_EDGE : IST_LEVEL;
    357 
    358 	const u_int mpsafe = (flags & FDT_INTR_MPSAFE) ? IST_MPSAFE : 0;
    359 
    360 	firq = sc->sc_irq[irq];
    361 	if (firq == NULL) {
    362 		firq = kmem_alloc(sizeof(*firq), KM_SLEEP);
    363 		firq->intr_sc = sc;
    364 		firq->intr_refcnt = 0;
    365 		firq->intr_arg = arg;
    366 		firq->intr_ipl = ipl;
    367 		firq->intr_level = level;
    368 		firq->intr_mpsafe = mpsafe;
    369 		TAILQ_INIT(&firq->intr_handlers);
    370 		firq->intr_irq = irq;
    371 		if (arg == NULL) {
    372 			firq->intr_ih = intr_establish_xname(irq, ipl,
    373 			    level | mpsafe, func, NULL, xname);
    374 		} else {
    375 			firq->intr_ih = intr_establish_xname(irq, ipl,
    376 			    level | mpsafe, gicv3_fdt_intr, firq, xname);
    377 		}
    378 		if (firq->intr_ih == NULL) {
    379 			kmem_free(firq, sizeof(*firq));
    380 			return NULL;
    381 		}
    382 		sc->sc_irq[irq] = firq;
    383 	} else {
    384 		if (firq->intr_arg == NULL && arg != NULL) {
    385 			device_printf(dev, "cannot share irq with NULL arg\n");
    386 			return NULL;
    387 		}
    388 		if (firq->intr_ipl != ipl) {
    389 			device_printf(dev, "cannot share irq with different "
    390 			    "ipl\n");
    391 			return NULL;
    392 		}
    393 		if (firq->intr_level != level) {
    394 			device_printf(dev, "cannot share edge and level "
    395 			    "interrupts\n");
    396 			return NULL;
    397 		}
    398 		if (firq->intr_mpsafe != mpsafe) {
    399 			device_printf(dev, "cannot share between "
    400 			    "mpsafe/non-mpsafe\n");
    401 			return NULL;
    402 		}
    403 	}
    404 
    405 	firq->intr_refcnt++;
    406 
    407 	firqh = kmem_alloc(sizeof(*firqh), KM_SLEEP);
    408 	firqh->ih_mpsafe = (flags & FDT_INTR_MPSAFE) != 0;
    409 	firqh->ih_irq = firq;
    410 	firqh->ih_fn = func;
    411 	firqh->ih_arg = arg;
    412 	TAILQ_INSERT_TAIL(&firq->intr_handlers, firqh, ih_next);
    413 
    414 	return firq->intr_ih;
    415 }
    416 
    417 static void
    418 gicv3_fdt_disestablish(device_t dev, void *ih)
    419 {
    420 	struct gicv3_fdt_softc * const sc = device_private(dev);
    421 	struct gicv3_fdt_irqhandler *firqh;
    422 	struct gicv3_fdt_irq *firq;
    423 	u_int n;
    424 
    425 	for (n = 0; n < GICV3_MAXIRQ; n++) {
    426 		firq = sc->sc_irq[n];
    427 		if (firq == NULL || firq->intr_ih != ih)
    428 			continue;
    429 
    430 		KASSERT(firq->intr_refcnt > 0);
    431 
    432 		if (firq->intr_refcnt > 1)
    433 			panic("%s: cannot disestablish shared irq", __func__);
    434 
    435 		firqh = TAILQ_FIRST(&firq->intr_handlers);
    436 		kmem_free(firqh, sizeof(*firqh));
    437 		intr_disestablish(firq->intr_ih);
    438 		kmem_free(firq, sizeof(*firq));
    439 		sc->sc_irq[n] = NULL;
    440 		return;
    441 	}
    442 
    443 	panic("%s: interrupt not established", __func__);
    444 }
    445 
    446 static int
    447 gicv3_fdt_intr(void *priv)
    448 {
    449 	struct gicv3_fdt_irq *firq = priv;
    450 	struct gicv3_fdt_irqhandler *firqh;
    451 	int handled = 0;
    452 
    453 	TAILQ_FOREACH(firqh, &firq->intr_handlers, ih_next)
    454 		handled += firqh->ih_fn(firqh->ih_arg);
    455 
    456 	return handled;
    457 }
    458 
    459 static bool
    460 gicv3_fdt_intrstr(device_t dev, u_int *specifier, char *buf, size_t buflen)
    461 {
    462 	/* 1st cell is the interrupt type; 0 is SPI, 1 is PPI */
    463 	/* 2nd cell is the interrupt number */
    464 	/* 3rd cell is flags */
    465 	/* 4th cell is affinity */
    466 
    467 	if (!specifier)
    468 		return false;
    469 	const u_int type = be32toh(specifier[0]);
    470 	const u_int intr = be32toh(specifier[1]);
    471 	const u_int irq = type == 0 ? IRQ_SPI(intr) : IRQ_PPI(intr);
    472 
    473 	snprintf(buf, buflen, "GICv3 irq %d", irq);
    474 
    475 	return true;
    476 }
    477