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