Home | History | Annotate | Line # | Download | only in fdt
gicv3_fdt.c revision 1.3
      1 /* $NetBSD: gicv3_fdt.c,v 1.3 2018/09/29 18:27:36 jmcneill 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 #define	_INTR_PRIVATE
     30 
     31 #include <sys/cdefs.h>
     32 __KERNEL_RCSID(0, "$NetBSD: gicv3_fdt.c,v 1.3 2018/09/29 18:27:36 jmcneill Exp $");
     33 
     34 #include <sys/param.h>
     35 #include <sys/bus.h>
     36 #include <sys/device.h>
     37 #include <sys/intr.h>
     38 #include <sys/systm.h>
     39 #include <sys/kernel.h>
     40 #include <sys/lwp.h>
     41 #include <sys/kmem.h>
     42 #include <sys/queue.h>
     43 
     44 #include <dev/fdt/fdtvar.h>
     45 
     46 #include <arm/cortex/gicv3.h>
     47 #include <arm/cortex/gic_reg.h>
     48 
     49 #define	GICV3_MAXIRQ	1020
     50 
     51 #define	IRQ_PPI(n)	((n) + 16)
     52 #define	IRQ_SPI(n)	((n) + 32)
     53 
     54 struct gicv3_fdt_softc;
     55 struct gicv3_fdt_irq;
     56 
     57 static int	gicv3_fdt_match(device_t, cfdata_t, void *);
     58 static void	gicv3_fdt_attach(device_t, device_t, void *);
     59 
     60 static int	gicv3_fdt_map_registers(struct gicv3_fdt_softc *);
     61 
     62 static int	gicv3_fdt_intr(void *);
     63 
     64 static void *	gicv3_fdt_establish(device_t, u_int *, int, int,
     65 		    int (*)(void *), void *);
     66 static void	gicv3_fdt_disestablish(device_t, void *);
     67 static bool	gicv3_fdt_intrstr(device_t, u_int *, char *, size_t);
     68 
     69 struct fdtbus_interrupt_controller_func gicv3_fdt_funcs = {
     70 	.establish = gicv3_fdt_establish,
     71 	.disestablish = gicv3_fdt_disestablish,
     72 	.intrstr = gicv3_fdt_intrstr
     73 };
     74 
     75 struct gicv3_fdt_irqhandler {
     76 	struct gicv3_fdt_irq	*ih_irq;
     77 	int			(*ih_fn)(void *);
     78 	void			*ih_arg;
     79 	bool			ih_mpsafe;
     80 	TAILQ_ENTRY(gicv3_fdt_irqhandler) ih_next;
     81 };
     82 
     83 struct gicv3_fdt_irq {
     84 	struct gicv3_fdt_softc	*intr_sc;
     85 	void			*intr_ih;
     86 	void			*intr_arg;
     87 	int			intr_refcnt;
     88 	int			intr_ipl;
     89 	int			intr_level;
     90 	int			intr_mpsafe;
     91 	TAILQ_HEAD(, gicv3_fdt_irqhandler) intr_handlers;
     92 	int			intr_irq;
     93 };
     94 
     95 struct gicv3_fdt_softc {
     96 	struct gicv3_softc	sc_gic;
     97 	int			sc_phandle;
     98 
     99 	struct gicv3_fdt_irq	*sc_irq[GICV3_MAXIRQ];
    100 };
    101 
    102 CFATTACH_DECL_NEW(gicv3_fdt, sizeof(struct gicv3_fdt_softc),
    103 	gicv3_fdt_match, gicv3_fdt_attach, NULL, NULL);
    104 
    105 static int
    106 gicv3_fdt_match(device_t parent, cfdata_t cf, void *aux)
    107 {
    108 	const char * const compatible[] = {
    109 		"arm,gic-v3",
    110 		NULL
    111 	};
    112 	struct fdt_attach_args * const faa = aux;
    113 	const int phandle = faa->faa_phandle;
    114 
    115 	return of_match_compatible(phandle, compatible);
    116 }
    117 
    118 static void
    119 gicv3_fdt_attach(device_t parent, device_t self, void *aux)
    120 {
    121 	struct gicv3_fdt_softc * const sc = device_private(self);
    122 	struct fdt_attach_args * const faa = aux;
    123 	const int phandle = faa->faa_phandle;
    124 	int error;
    125 
    126 	error = fdtbus_register_interrupt_controller(self, phandle,
    127 	    &gicv3_fdt_funcs);
    128 	if (error) {
    129 		aprint_error(": couldn't register with fdtbus: %d\n", error);
    130 		return;
    131 	}
    132 
    133 	aprint_naive("\n");
    134 	aprint_normal(": GICv3\n");
    135 
    136 	sc->sc_phandle = phandle;
    137 	sc->sc_gic.sc_dev = self;
    138 	sc->sc_gic.sc_bst = faa->faa_bst;
    139 
    140 	error = gicv3_fdt_map_registers(sc);
    141 	if (error) {
    142 		aprint_error_dev(self, "couldn't map registers\n");
    143 		return;
    144 	}
    145 
    146 	aprint_debug_dev(self, "%d redistributors\n", sc->sc_gic.sc_bsh_r_count);
    147 
    148 	error = gicv3_init(&sc->sc_gic);
    149 	if (error) {
    150 		aprint_error_dev(self, "failed to initialize GIC: %d\n", error);
    151 		return;
    152 	}
    153 
    154 	arm_fdt_irq_set_handler(gicv3_irq_handler);
    155 }
    156 
    157 static int
    158 gicv3_fdt_map_registers(struct gicv3_fdt_softc *sc)
    159 {
    160 	struct gicv3_softc *gic = &sc->sc_gic;
    161 	const int phandle = sc->sc_phandle;
    162 	u_int redistributor_regions, redistributor_stride;
    163 	bus_space_handle_t bsh;
    164 	bus_size_t size, region_off;
    165 	bus_addr_t addr;
    166 	size_t reg_off;
    167 	int n, r, max_redist, redist;
    168 
    169 	if (of_getprop_uint32(phandle, "#redistributor-regions", &redistributor_regions))
    170 		redistributor_regions = 1;
    171 	if (of_getprop_uint32(phandle, "redistributor-stride", &redistributor_stride))
    172 		redistributor_stride = 0x20000;
    173 
    174 	/*
    175 	 * Map GIC Distributor interface (GICD)
    176 	 */
    177 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
    178 		aprint_error_dev(gic->sc_dev, "couldn't get distributor registers\n");
    179 		return ENXIO;
    180 	}
    181 	if (bus_space_map(sc->sc_gic.sc_bst, addr, size, 0, &sc->sc_gic.sc_bsh_d) != 0) {
    182 		aprint_error_dev(gic->sc_dev, "couldn't map distributor registers\n");
    183 		return ENXIO;
    184 	}
    185 
    186 	/*
    187 	 * GIC Redistributors (GICR)
    188 	 */
    189 	for (reg_off = 1, max_redist = 0, n = 0; n < redistributor_regions; n++, reg_off++) {
    190 		if (fdtbus_get_reg(phandle, reg_off, NULL, &size) != 0) {
    191 			aprint_error_dev(gic->sc_dev, "couldn't get redistributor registers\n");
    192 			return ENXIO;
    193 		}
    194 		max_redist += howmany(size, redistributor_stride);
    195 	}
    196 	gic->sc_bsh_r = kmem_alloc(sizeof(bus_space_handle_t) * max_redist, KM_SLEEP);
    197 	for (reg_off = 1, redist = 0, n = 0; n < redistributor_regions; n++, reg_off++) {
    198 		if (fdtbus_get_reg(phandle, reg_off, &addr, &size) != 0) {
    199 			aprint_error_dev(gic->sc_dev, "couldn't get redistributor registers\n");
    200 			return ENXIO;
    201 		}
    202 		if (bus_space_map(sc->sc_gic.sc_bst, addr, size, 0, &bsh) != 0) {
    203 			aprint_error_dev(gic->sc_dev, "couldn't map redistributor registers\n");
    204 			return ENXIO;
    205 		}
    206 		const int count = howmany(size, redistributor_stride);
    207 		for (r = 0, region_off = 0; r < count; r++, region_off += redistributor_stride) {
    208 			if (bus_space_subregion(sc->sc_gic.sc_bst, bsh, region_off, redistributor_stride, &gic->sc_bsh_r[redist++]) != 0) {
    209 				aprint_error_dev(gic->sc_dev, "couldn't subregion redistributor registers\n");
    210 				return ENXIO;
    211 			}
    212 
    213 			/* If this is the last redist in this region, skip to the next one */
    214 			const uint32_t typer = bus_space_read_4(sc->sc_gic.sc_bst, gic->sc_bsh_r[redist - 1], GICR_TYPER);
    215 			if (typer & GICR_TYPER_Last)
    216 				break;
    217 		}
    218 	}
    219 	gic->sc_bsh_r_count = redist;
    220 
    221 	return 0;
    222 }
    223 
    224 static void *
    225 gicv3_fdt_establish(device_t dev, u_int *specifier, int ipl, int flags,
    226     int (*func)(void *), void *arg)
    227 {
    228 	struct gicv3_fdt_softc * const sc = device_private(dev);
    229 	struct gicv3_fdt_irq *firq;
    230 	struct gicv3_fdt_irqhandler *firqh;
    231 
    232 	/* 1st cell is the interrupt type; 0 is SPI, 1 is PPI */
    233 	/* 2nd cell is the interrupt number */
    234 	/* 3rd cell is flags */
    235 	/* 4th cell is affinity */
    236 
    237 	const u_int type = be32toh(specifier[0]);
    238 	const u_int intr = be32toh(specifier[1]);
    239 	const u_int irq = type == 0 ? IRQ_SPI(intr) : IRQ_PPI(intr);
    240 	const u_int trig = be32toh(specifier[2]) & 0xf;
    241 	const u_int level = (trig & 0x3) ? IST_EDGE : IST_LEVEL;
    242 
    243 	const u_int mpsafe = (flags & FDT_INTR_MPSAFE) ? IST_MPSAFE : 0;
    244 
    245 	firq = sc->sc_irq[irq];
    246 	if (firq == NULL) {
    247 		firq = kmem_alloc(sizeof(*firq), KM_SLEEP);
    248 		firq->intr_sc = sc;
    249 		firq->intr_refcnt = 0;
    250 		firq->intr_arg = arg;
    251 		firq->intr_ipl = ipl;
    252 		firq->intr_level = level;
    253 		firq->intr_mpsafe = mpsafe;
    254 		TAILQ_INIT(&firq->intr_handlers);
    255 		firq->intr_irq = irq;
    256 		if (arg == NULL) {
    257 			firq->intr_ih = intr_establish(irq, ipl, level | mpsafe,
    258 			    func, NULL);
    259 		} else {
    260 			firq->intr_ih = intr_establish(irq, ipl, level | mpsafe,
    261 			    gicv3_fdt_intr, firq);
    262 		}
    263 		if (firq->intr_ih == NULL) {
    264 			kmem_free(firq, sizeof(*firq));
    265 			return NULL;
    266 		}
    267 		sc->sc_irq[irq] = firq;
    268 	} else {
    269 		if (firq->intr_arg == NULL && arg != NULL) {
    270 			device_printf(dev, "cannot share irq with NULL arg\n");
    271 			return NULL;
    272 		}
    273 		if (firq->intr_ipl != ipl) {
    274 			device_printf(dev, "cannot share irq with different "
    275 			    "ipl\n");
    276 			return NULL;
    277 		}
    278 		if (firq->intr_level != level) {
    279 			device_printf(dev, "cannot share edge and level "
    280 			    "interrupts\n");
    281 			return NULL;
    282 		}
    283 		if (firq->intr_mpsafe != mpsafe) {
    284 			device_printf(dev, "cannot share between "
    285 			    "mpsafe/non-mpsafe\n");
    286 			return NULL;
    287 		}
    288 	}
    289 
    290 	firq->intr_refcnt++;
    291 
    292 	firqh = kmem_alloc(sizeof(*firqh), KM_SLEEP);
    293 	firqh->ih_mpsafe = (flags & FDT_INTR_MPSAFE) != 0;
    294 	firqh->ih_irq = firq;
    295 	firqh->ih_fn = func;
    296 	firqh->ih_arg = arg;
    297 	TAILQ_INSERT_TAIL(&firq->intr_handlers, firqh, ih_next);
    298 
    299 	return firq->intr_ih;
    300 }
    301 
    302 static void
    303 gicv3_fdt_disestablish(device_t dev, void *ih)
    304 {
    305 	struct gicv3_fdt_softc * const sc = device_private(dev);
    306 	struct gicv3_fdt_irqhandler *firqh;
    307 	struct gicv3_fdt_irq *firq;
    308 	u_int n;
    309 
    310 	for (n = 0; n < GICV3_MAXIRQ; n++) {
    311 		firq = sc->sc_irq[n];
    312 		if (firq->intr_ih != ih)
    313 			continue;
    314 
    315 		KASSERT(firq->intr_refcnt > 0);
    316 
    317 		if (firq->intr_refcnt > 1)
    318 			panic("%s: cannot disestablish shared irq", __func__);
    319 
    320 		firqh = TAILQ_FIRST(&firq->intr_handlers);
    321 		kmem_free(firqh, sizeof(*firqh));
    322 		intr_disestablish(firq->intr_ih);
    323 		kmem_free(firq, sizeof(*firq));
    324 		sc->sc_irq[n] = NULL;
    325 		return;
    326 	}
    327 
    328 	panic("%s: interrupt not established", __func__);
    329 }
    330 
    331 static int
    332 gicv3_fdt_intr(void *priv)
    333 {
    334 	struct gicv3_fdt_irq *firq = priv;
    335 	struct gicv3_fdt_irqhandler *firqh;
    336 	int handled = 0;
    337 
    338 	TAILQ_FOREACH(firqh, &firq->intr_handlers, ih_next)
    339 		handled += firqh->ih_fn(firqh->ih_arg);
    340 
    341 	return handled;
    342 }
    343 
    344 static bool
    345 gicv3_fdt_intrstr(device_t dev, u_int *specifier, char *buf, size_t buflen)
    346 {
    347 	/* 1st cell is the interrupt type; 0 is SPI, 1 is PPI */
    348 	/* 2nd cell is the interrupt number */
    349 	/* 3rd cell is flags */
    350 	/* 4th cell is affinity */
    351 
    352 	if (!specifier)
    353 		return false;
    354 	const u_int type = be32toh(specifier[0]);
    355 	const u_int intr = be32toh(specifier[1]);
    356 	const u_int irq = type == 0 ? IRQ_SPI(intr) : IRQ_PPI(intr);
    357 
    358 	snprintf(buf, buflen, "GICv3 irq %d", irq);
    359 
    360 	return true;
    361 }
    362