Home | History | Annotate | Line # | Download | only in fdt
gicv3_fdt.c revision 1.2
      1  1.2  jmcneill /* $NetBSD: gicv3_fdt.c,v 1.2 2018/08/12 21:44:17 jmcneill Exp $ */
      2  1.1  jmcneill 
      3  1.1  jmcneill /*-
      4  1.1  jmcneill  * Copyright (c) 2015-2018 Jared McNeill <jmcneill (at) invisible.ca>
      5  1.1  jmcneill  * All rights reserved.
      6  1.1  jmcneill  *
      7  1.1  jmcneill  * Redistribution and use in source and binary forms, with or without
      8  1.1  jmcneill  * modification, are permitted provided that the following conditions
      9  1.1  jmcneill  * are met:
     10  1.1  jmcneill  * 1. Redistributions of source code must retain the above copyright
     11  1.1  jmcneill  *    notice, this list of conditions and the following disclaimer.
     12  1.1  jmcneill  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  jmcneill  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  jmcneill  *    documentation and/or other materials provided with the distribution.
     15  1.1  jmcneill  *
     16  1.1  jmcneill  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  1.1  jmcneill  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  1.1  jmcneill  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  1.1  jmcneill  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  1.1  jmcneill  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21  1.1  jmcneill  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22  1.1  jmcneill  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23  1.1  jmcneill  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24  1.1  jmcneill  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  1.1  jmcneill  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  1.1  jmcneill  * SUCH DAMAGE.
     27  1.1  jmcneill  */
     28  1.1  jmcneill 
     29  1.1  jmcneill #define	_INTR_PRIVATE
     30  1.1  jmcneill 
     31  1.1  jmcneill #include <sys/cdefs.h>
     32  1.2  jmcneill __KERNEL_RCSID(0, "$NetBSD: gicv3_fdt.c,v 1.2 2018/08/12 21:44:17 jmcneill Exp $");
     33  1.1  jmcneill 
     34  1.1  jmcneill #include <sys/param.h>
     35  1.1  jmcneill #include <sys/bus.h>
     36  1.1  jmcneill #include <sys/device.h>
     37  1.1  jmcneill #include <sys/intr.h>
     38  1.1  jmcneill #include <sys/systm.h>
     39  1.1  jmcneill #include <sys/kernel.h>
     40  1.1  jmcneill #include <sys/lwp.h>
     41  1.1  jmcneill #include <sys/kmem.h>
     42  1.1  jmcneill #include <sys/queue.h>
     43  1.1  jmcneill 
     44  1.1  jmcneill #include <dev/fdt/fdtvar.h>
     45  1.1  jmcneill 
     46  1.1  jmcneill #include <arm/cortex/gicv3.h>
     47  1.1  jmcneill 
     48  1.1  jmcneill #define	GICV3_MAXIRQ	1020
     49  1.1  jmcneill 
     50  1.1  jmcneill #define	IRQ_PPI(n)	((n) + 16)
     51  1.1  jmcneill #define	IRQ_SPI(n)	((n) + 32)
     52  1.1  jmcneill 
     53  1.1  jmcneill struct gicv3_fdt_softc;
     54  1.1  jmcneill struct gicv3_fdt_irq;
     55  1.1  jmcneill 
     56  1.1  jmcneill static int	gicv3_fdt_match(device_t, cfdata_t, void *);
     57  1.1  jmcneill static void	gicv3_fdt_attach(device_t, device_t, void *);
     58  1.1  jmcneill 
     59  1.1  jmcneill static int	gicv3_fdt_map_registers(struct gicv3_fdt_softc *);
     60  1.1  jmcneill 
     61  1.1  jmcneill static int	gicv3_fdt_intr(void *);
     62  1.1  jmcneill 
     63  1.1  jmcneill static void *	gicv3_fdt_establish(device_t, u_int *, int, int,
     64  1.1  jmcneill 		    int (*)(void *), void *);
     65  1.1  jmcneill static void	gicv3_fdt_disestablish(device_t, void *);
     66  1.1  jmcneill static bool	gicv3_fdt_intrstr(device_t, u_int *, char *, size_t);
     67  1.1  jmcneill 
     68  1.1  jmcneill struct fdtbus_interrupt_controller_func gicv3_fdt_funcs = {
     69  1.1  jmcneill 	.establish = gicv3_fdt_establish,
     70  1.1  jmcneill 	.disestablish = gicv3_fdt_disestablish,
     71  1.1  jmcneill 	.intrstr = gicv3_fdt_intrstr
     72  1.1  jmcneill };
     73  1.1  jmcneill 
     74  1.1  jmcneill struct gicv3_fdt_irqhandler {
     75  1.1  jmcneill 	struct gicv3_fdt_irq	*ih_irq;
     76  1.1  jmcneill 	int			(*ih_fn)(void *);
     77  1.1  jmcneill 	void			*ih_arg;
     78  1.1  jmcneill 	bool			ih_mpsafe;
     79  1.1  jmcneill 	TAILQ_ENTRY(gicv3_fdt_irqhandler) ih_next;
     80  1.1  jmcneill };
     81  1.1  jmcneill 
     82  1.1  jmcneill struct gicv3_fdt_irq {
     83  1.1  jmcneill 	struct gicv3_fdt_softc	*intr_sc;
     84  1.1  jmcneill 	void			*intr_ih;
     85  1.1  jmcneill 	void			*intr_arg;
     86  1.1  jmcneill 	int			intr_refcnt;
     87  1.1  jmcneill 	int			intr_ipl;
     88  1.1  jmcneill 	int			intr_level;
     89  1.1  jmcneill 	int			intr_mpsafe;
     90  1.1  jmcneill 	TAILQ_HEAD(, gicv3_fdt_irqhandler) intr_handlers;
     91  1.1  jmcneill 	int			intr_irq;
     92  1.1  jmcneill };
     93  1.1  jmcneill 
     94  1.1  jmcneill struct gicv3_fdt_softc {
     95  1.1  jmcneill 	struct gicv3_softc	sc_gic;
     96  1.1  jmcneill 	int			sc_phandle;
     97  1.1  jmcneill 
     98  1.1  jmcneill 	struct gicv3_fdt_irq	*sc_irq[GICV3_MAXIRQ];
     99  1.1  jmcneill };
    100  1.1  jmcneill 
    101  1.1  jmcneill CFATTACH_DECL_NEW(gicv3_fdt, sizeof(struct gicv3_fdt_softc),
    102  1.1  jmcneill 	gicv3_fdt_match, gicv3_fdt_attach, NULL, NULL);
    103  1.1  jmcneill 
    104  1.1  jmcneill static int
    105  1.1  jmcneill gicv3_fdt_match(device_t parent, cfdata_t cf, void *aux)
    106  1.1  jmcneill {
    107  1.1  jmcneill 	const char * const compatible[] = {
    108  1.1  jmcneill 		"arm,gic-v3",
    109  1.1  jmcneill 		NULL
    110  1.1  jmcneill 	};
    111  1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
    112  1.1  jmcneill 	const int phandle = faa->faa_phandle;
    113  1.1  jmcneill 
    114  1.1  jmcneill 	return of_match_compatible(phandle, compatible);
    115  1.1  jmcneill }
    116  1.1  jmcneill 
    117  1.1  jmcneill static void
    118  1.1  jmcneill gicv3_fdt_attach(device_t parent, device_t self, void *aux)
    119  1.1  jmcneill {
    120  1.1  jmcneill 	struct gicv3_fdt_softc * const sc = device_private(self);
    121  1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
    122  1.1  jmcneill 	const int phandle = faa->faa_phandle;
    123  1.1  jmcneill 	int error;
    124  1.1  jmcneill 
    125  1.1  jmcneill 	error = fdtbus_register_interrupt_controller(self, phandle,
    126  1.1  jmcneill 	    &gicv3_fdt_funcs);
    127  1.1  jmcneill 	if (error) {
    128  1.1  jmcneill 		aprint_error(": couldn't register with fdtbus: %d\n", error);
    129  1.1  jmcneill 		return;
    130  1.1  jmcneill 	}
    131  1.1  jmcneill 
    132  1.1  jmcneill 	aprint_naive("\n");
    133  1.1  jmcneill 	aprint_normal(": GICv3\n");
    134  1.1  jmcneill 
    135  1.1  jmcneill 	sc->sc_phandle = phandle;
    136  1.1  jmcneill 	sc->sc_gic.sc_dev = self;
    137  1.1  jmcneill 	sc->sc_gic.sc_bst = faa->faa_bst;
    138  1.1  jmcneill 
    139  1.1  jmcneill 	error = gicv3_fdt_map_registers(sc);
    140  1.1  jmcneill 	if (error) {
    141  1.1  jmcneill 		aprint_error_dev(self, "couldn't map registers\n");
    142  1.1  jmcneill 		return;
    143  1.1  jmcneill 	}
    144  1.1  jmcneill 
    145  1.2  jmcneill 	aprint_debug_dev(self, "%d redistributors\n", sc->sc_gic.sc_bsh_r_count);
    146  1.1  jmcneill 
    147  1.1  jmcneill 	error = gicv3_init(&sc->sc_gic);
    148  1.1  jmcneill 	if (error) {
    149  1.1  jmcneill 		aprint_error_dev(self, "failed to initialize GIC: %d\n", error);
    150  1.1  jmcneill 		return;
    151  1.1  jmcneill 	}
    152  1.1  jmcneill 
    153  1.1  jmcneill 	arm_fdt_irq_set_handler(gicv3_irq_handler);
    154  1.1  jmcneill }
    155  1.1  jmcneill 
    156  1.1  jmcneill static int
    157  1.1  jmcneill gicv3_fdt_map_registers(struct gicv3_fdt_softc *sc)
    158  1.1  jmcneill {
    159  1.1  jmcneill 	struct gicv3_softc *gic = &sc->sc_gic;
    160  1.1  jmcneill 	const int phandle = sc->sc_phandle;
    161  1.1  jmcneill 	u_int redistributor_regions, redistributor_stride;
    162  1.1  jmcneill 	bus_space_handle_t bsh;
    163  1.1  jmcneill 	bus_size_t size, region_off;
    164  1.1  jmcneill 	bus_addr_t addr;
    165  1.1  jmcneill 	size_t reg_off;
    166  1.1  jmcneill 	int n, r;
    167  1.1  jmcneill 
    168  1.1  jmcneill 	if (of_getprop_uint32(phandle, "#redistributor-regions", &redistributor_regions))
    169  1.1  jmcneill 		redistributor_regions = 1;
    170  1.1  jmcneill 	if (of_getprop_uint32(phandle, "redistributor-stride", &redistributor_stride))
    171  1.1  jmcneill 		redistributor_stride = 0x20000;
    172  1.1  jmcneill 
    173  1.1  jmcneill 	/*
    174  1.1  jmcneill 	 * Map GIC Distributor interface (GICD)
    175  1.1  jmcneill 	 */
    176  1.1  jmcneill 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
    177  1.1  jmcneill 		aprint_error_dev(gic->sc_dev, "couldn't get distributor registers\n");
    178  1.1  jmcneill 		return ENXIO;
    179  1.1  jmcneill 	}
    180  1.1  jmcneill 	if (bus_space_map(sc->sc_gic.sc_bst, addr, size, 0, &sc->sc_gic.sc_bsh_d) != 0) {
    181  1.1  jmcneill 		aprint_error_dev(gic->sc_dev, "couldn't map distributor registers\n");
    182  1.1  jmcneill 		return ENXIO;
    183  1.1  jmcneill 	}
    184  1.1  jmcneill 
    185  1.1  jmcneill 	/*
    186  1.1  jmcneill 	 * GIC Redistributors (GICR)
    187  1.1  jmcneill 	 */
    188  1.1  jmcneill 	for (reg_off = 1, n = 0; n < redistributor_regions; n++, reg_off++) {
    189  1.1  jmcneill 		if (fdtbus_get_reg(phandle, reg_off, NULL, &size) != 0) {
    190  1.1  jmcneill 			aprint_error_dev(gic->sc_dev, "couldn't get redistributor registers\n");
    191  1.1  jmcneill 			return ENXIO;
    192  1.1  jmcneill 		}
    193  1.1  jmcneill 		gic->sc_bsh_r_count += howmany(size, redistributor_stride);
    194  1.1  jmcneill 	}
    195  1.1  jmcneill 	gic->sc_bsh_r = kmem_alloc(sizeof(bus_space_handle_t) * gic->sc_bsh_r_count, KM_SLEEP);
    196  1.1  jmcneill 	for (reg_off = 1, n = 0; n < redistributor_regions; n++, reg_off++) {
    197  1.1  jmcneill 		if (fdtbus_get_reg(phandle, reg_off, &addr, &size) != 0) {
    198  1.1  jmcneill 			aprint_error_dev(gic->sc_dev, "couldn't get redistributor registers\n");
    199  1.1  jmcneill 			return ENXIO;
    200  1.1  jmcneill 		}
    201  1.1  jmcneill 		if (bus_space_map(sc->sc_gic.sc_bst, addr, size, 0, &bsh) != 0) {
    202  1.1  jmcneill 			aprint_error_dev(gic->sc_dev, "couldn't map redistributor registers\n");
    203  1.1  jmcneill 			return ENXIO;
    204  1.1  jmcneill 		}
    205  1.1  jmcneill 		const int count = howmany(size, redistributor_stride);
    206  1.1  jmcneill 		for (r = 0, region_off = 0; r < count; r++, region_off += redistributor_stride) {
    207  1.1  jmcneill 			if (bus_space_subregion(sc->sc_gic.sc_bst, bsh, region_off, redistributor_stride, &gic->sc_bsh_r[r]) != 0) {
    208  1.1  jmcneill 				aprint_error_dev(gic->sc_dev, "couldn't subregion redistributor registers\n");
    209  1.1  jmcneill 				return ENXIO;
    210  1.1  jmcneill 			}
    211  1.1  jmcneill 		}
    212  1.1  jmcneill 	}
    213  1.1  jmcneill 
    214  1.1  jmcneill 	return 0;
    215  1.1  jmcneill }
    216  1.1  jmcneill 
    217  1.1  jmcneill static void *
    218  1.1  jmcneill gicv3_fdt_establish(device_t dev, u_int *specifier, int ipl, int flags,
    219  1.1  jmcneill     int (*func)(void *), void *arg)
    220  1.1  jmcneill {
    221  1.1  jmcneill 	struct gicv3_fdt_softc * const sc = device_private(dev);
    222  1.1  jmcneill 	struct gicv3_fdt_irq *firq;
    223  1.1  jmcneill 	struct gicv3_fdt_irqhandler *firqh;
    224  1.1  jmcneill 
    225  1.1  jmcneill 	/* 1st cell is the interrupt type; 0 is SPI, 1 is PPI */
    226  1.1  jmcneill 	/* 2nd cell is the interrupt number */
    227  1.1  jmcneill 	/* 3rd cell is flags */
    228  1.1  jmcneill 	/* 4th cell is affinity */
    229  1.1  jmcneill 
    230  1.1  jmcneill 	const u_int type = be32toh(specifier[0]);
    231  1.1  jmcneill 	const u_int intr = be32toh(specifier[1]);
    232  1.1  jmcneill 	const u_int irq = type == 0 ? IRQ_SPI(intr) : IRQ_PPI(intr);
    233  1.1  jmcneill 	const u_int trig = be32toh(specifier[2]) & 0xf;
    234  1.1  jmcneill 	const u_int level = (trig & 0x3) ? IST_EDGE : IST_LEVEL;
    235  1.1  jmcneill 
    236  1.1  jmcneill 	const u_int mpsafe = (flags & FDT_INTR_MPSAFE) ? IST_MPSAFE : 0;
    237  1.1  jmcneill 
    238  1.1  jmcneill 	firq = sc->sc_irq[irq];
    239  1.1  jmcneill 	if (firq == NULL) {
    240  1.1  jmcneill 		firq = kmem_alloc(sizeof(*firq), KM_SLEEP);
    241  1.1  jmcneill 		firq->intr_sc = sc;
    242  1.1  jmcneill 		firq->intr_refcnt = 0;
    243  1.1  jmcneill 		firq->intr_arg = arg;
    244  1.1  jmcneill 		firq->intr_ipl = ipl;
    245  1.1  jmcneill 		firq->intr_level = level;
    246  1.1  jmcneill 		firq->intr_mpsafe = mpsafe;
    247  1.1  jmcneill 		TAILQ_INIT(&firq->intr_handlers);
    248  1.1  jmcneill 		firq->intr_irq = irq;
    249  1.1  jmcneill 		if (arg == NULL) {
    250  1.1  jmcneill 			firq->intr_ih = intr_establish(irq, ipl, level | mpsafe,
    251  1.1  jmcneill 			    func, NULL);
    252  1.1  jmcneill 		} else {
    253  1.1  jmcneill 			firq->intr_ih = intr_establish(irq, ipl, level | mpsafe,
    254  1.1  jmcneill 			    gicv3_fdt_intr, firq);
    255  1.1  jmcneill 		}
    256  1.1  jmcneill 		if (firq->intr_ih == NULL) {
    257  1.1  jmcneill 			kmem_free(firq, sizeof(*firq));
    258  1.1  jmcneill 			return NULL;
    259  1.1  jmcneill 		}
    260  1.1  jmcneill 		sc->sc_irq[irq] = firq;
    261  1.1  jmcneill 	} else {
    262  1.1  jmcneill 		if (firq->intr_arg == NULL && arg != NULL) {
    263  1.1  jmcneill 			device_printf(dev, "cannot share irq with NULL arg\n");
    264  1.1  jmcneill 			return NULL;
    265  1.1  jmcneill 		}
    266  1.1  jmcneill 		if (firq->intr_ipl != ipl) {
    267  1.1  jmcneill 			device_printf(dev, "cannot share irq with different "
    268  1.1  jmcneill 			    "ipl\n");
    269  1.1  jmcneill 			return NULL;
    270  1.1  jmcneill 		}
    271  1.1  jmcneill 		if (firq->intr_level != level) {
    272  1.1  jmcneill 			device_printf(dev, "cannot share edge and level "
    273  1.1  jmcneill 			    "interrupts\n");
    274  1.1  jmcneill 			return NULL;
    275  1.1  jmcneill 		}
    276  1.1  jmcneill 		if (firq->intr_mpsafe != mpsafe) {
    277  1.1  jmcneill 			device_printf(dev, "cannot share between "
    278  1.1  jmcneill 			    "mpsafe/non-mpsafe\n");
    279  1.1  jmcneill 			return NULL;
    280  1.1  jmcneill 		}
    281  1.1  jmcneill 	}
    282  1.1  jmcneill 
    283  1.1  jmcneill 	firq->intr_refcnt++;
    284  1.1  jmcneill 
    285  1.1  jmcneill 	firqh = kmem_alloc(sizeof(*firqh), KM_SLEEP);
    286  1.1  jmcneill 	firqh->ih_mpsafe = (flags & FDT_INTR_MPSAFE) != 0;
    287  1.1  jmcneill 	firqh->ih_irq = firq;
    288  1.1  jmcneill 	firqh->ih_fn = func;
    289  1.1  jmcneill 	firqh->ih_arg = arg;
    290  1.1  jmcneill 	TAILQ_INSERT_TAIL(&firq->intr_handlers, firqh, ih_next);
    291  1.1  jmcneill 
    292  1.1  jmcneill 	return firq->intr_ih;
    293  1.1  jmcneill }
    294  1.1  jmcneill 
    295  1.1  jmcneill static void
    296  1.1  jmcneill gicv3_fdt_disestablish(device_t dev, void *ih)
    297  1.1  jmcneill {
    298  1.1  jmcneill 	struct gicv3_fdt_softc * const sc = device_private(dev);
    299  1.1  jmcneill 	struct gicv3_fdt_irqhandler *firqh;
    300  1.1  jmcneill 	struct gicv3_fdt_irq *firq;
    301  1.1  jmcneill 	u_int n;
    302  1.1  jmcneill 
    303  1.1  jmcneill 	for (n = 0; n < GICV3_MAXIRQ; n++) {
    304  1.1  jmcneill 		firq = sc->sc_irq[n];
    305  1.1  jmcneill 		if (firq->intr_ih != ih)
    306  1.1  jmcneill 			continue;
    307  1.1  jmcneill 
    308  1.1  jmcneill 		KASSERT(firq->intr_refcnt > 0);
    309  1.1  jmcneill 
    310  1.1  jmcneill 		if (firq->intr_refcnt > 1)
    311  1.1  jmcneill 			panic("%s: cannot disestablish shared irq", __func__);
    312  1.1  jmcneill 
    313  1.1  jmcneill 		firqh = TAILQ_FIRST(&firq->intr_handlers);
    314  1.1  jmcneill 		kmem_free(firqh, sizeof(*firqh));
    315  1.1  jmcneill 		intr_disestablish(firq->intr_ih);
    316  1.1  jmcneill 		kmem_free(firq, sizeof(*firq));
    317  1.1  jmcneill 		sc->sc_irq[n] = NULL;
    318  1.1  jmcneill 		return;
    319  1.1  jmcneill 	}
    320  1.1  jmcneill 
    321  1.1  jmcneill 	panic("%s: interrupt not established", __func__);
    322  1.1  jmcneill }
    323  1.1  jmcneill 
    324  1.1  jmcneill static int
    325  1.1  jmcneill gicv3_fdt_intr(void *priv)
    326  1.1  jmcneill {
    327  1.1  jmcneill 	struct gicv3_fdt_irq *firq = priv;
    328  1.1  jmcneill 	struct gicv3_fdt_irqhandler *firqh;
    329  1.1  jmcneill 	int handled = 0;
    330  1.1  jmcneill 
    331  1.1  jmcneill 	TAILQ_FOREACH(firqh, &firq->intr_handlers, ih_next)
    332  1.1  jmcneill 		handled += firqh->ih_fn(firqh->ih_arg);
    333  1.1  jmcneill 
    334  1.1  jmcneill 	return handled;
    335  1.1  jmcneill }
    336  1.1  jmcneill 
    337  1.1  jmcneill static bool
    338  1.1  jmcneill gicv3_fdt_intrstr(device_t dev, u_int *specifier, char *buf, size_t buflen)
    339  1.1  jmcneill {
    340  1.1  jmcneill 	/* 1st cell is the interrupt type; 0 is SPI, 1 is PPI */
    341  1.1  jmcneill 	/* 2nd cell is the interrupt number */
    342  1.1  jmcneill 	/* 3rd cell is flags */
    343  1.1  jmcneill 	/* 4th cell is affinity */
    344  1.1  jmcneill 
    345  1.1  jmcneill 	if (!specifier)
    346  1.1  jmcneill 		return false;
    347  1.1  jmcneill 	const u_int type = be32toh(specifier[0]);
    348  1.1  jmcneill 	const u_int intr = be32toh(specifier[1]);
    349  1.1  jmcneill 	const u_int irq = type == 0 ? IRQ_SPI(intr) : IRQ_PPI(intr);
    350  1.1  jmcneill 
    351  1.1  jmcneill 	snprintf(buf, buflen, "GICv3 irq %d", irq);
    352  1.1  jmcneill 
    353  1.1  jmcneill 	return true;
    354  1.1  jmcneill }
    355