Home | History | Annotate | Line # | Download | only in fdt
gic_fdt.c revision 1.14
      1 /* $NetBSD: gic_fdt.c,v 1.14 2018/11/11 21:24:28 jmcneill Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2015-2017 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 <sys/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: gic_fdt.c,v 1.14 2018/11/11 21:24:28 jmcneill Exp $");
     31 
     32 #include <sys/param.h>
     33 #include <sys/bus.h>
     34 #include <sys/device.h>
     35 #include <sys/intr.h>
     36 #include <sys/systm.h>
     37 #include <sys/kernel.h>
     38 #include <sys/lwp.h>
     39 #include <sys/kmem.h>
     40 #include <sys/queue.h>
     41 
     42 #include <dev/pci/pcivar.h>
     43 
     44 #include <arm/cortex/gic_intr.h>
     45 #include <arm/cortex/gic_reg.h>
     46 #include <arm/cortex/gic_v2m.h>
     47 #include <arm/cortex/mpcore_var.h>
     48 
     49 #include <dev/fdt/fdtvar.h>
     50 
     51 #define	GIC_MAXIRQ	1020
     52 
     53 extern struct pic_softc *pic_list[];
     54 
     55 struct gic_fdt_softc;
     56 struct gic_fdt_irq;
     57 
     58 static int	gic_fdt_match(device_t, cfdata_t, void *);
     59 static void	gic_fdt_attach(device_t, device_t, void *);
     60 static void	gic_fdt_attach_v2m(struct gic_fdt_softc *, bus_space_tag_t, int);
     61 
     62 static int	gic_fdt_intr(void *);
     63 
     64 static void *	gic_fdt_establish(device_t, u_int *, int, int,
     65 		    int (*)(void *), void *);
     66 static void	gic_fdt_disestablish(device_t, void *);
     67 static bool	gic_fdt_intrstr(device_t, u_int *, char *, size_t);
     68 
     69 struct fdtbus_interrupt_controller_func gic_fdt_funcs = {
     70 	.establish = gic_fdt_establish,
     71 	.disestablish = gic_fdt_disestablish,
     72 	.intrstr = gic_fdt_intrstr
     73 };
     74 
     75 struct gic_fdt_irqhandler {
     76 	struct gic_fdt_irq	*ih_irq;
     77 	int			(*ih_fn)(void *);
     78 	void			*ih_arg;
     79 	bool			ih_mpsafe;
     80 	TAILQ_ENTRY(gic_fdt_irqhandler) ih_next;
     81 };
     82 
     83 struct gic_fdt_irq {
     84 	struct gic_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(, gic_fdt_irqhandler) intr_handlers;
     92 	int			intr_irq;
     93 };
     94 
     95 struct gic_fdt_softc {
     96 	device_t		sc_dev;
     97 	device_t		sc_gicdev;
     98 	int			sc_phandle;
     99 
    100 	int			sc_v2m_count;
    101 
    102 	struct gic_fdt_irq	*sc_irq[GIC_MAXIRQ];
    103 };
    104 
    105 CFATTACH_DECL_NEW(gic_fdt, sizeof(struct gic_fdt_softc),
    106 	gic_fdt_match, gic_fdt_attach, NULL, NULL);
    107 
    108 static int
    109 gic_fdt_match(device_t parent, cfdata_t cf, void *aux)
    110 {
    111 	const char * const compatible[] = {
    112 		"arm,gic-400",
    113 		"arm,cortex-a15-gic",
    114 		"arm,cortex-a9-gic",
    115 		"arm,cortex-a7-gic",
    116 		NULL
    117 	};
    118 	struct fdt_attach_args * const faa = aux;
    119 
    120 	return of_match_compatible(faa->faa_phandle, compatible);
    121 }
    122 
    123 static void
    124 gic_fdt_attach(device_t parent, device_t self, void *aux)
    125 {
    126 	struct gic_fdt_softc * const sc = device_private(self);
    127 	struct fdt_attach_args * const faa = aux;
    128 	const int phandle = faa->faa_phandle;
    129 	bus_addr_t addr_d, addr_c;
    130 	bus_size_t size_d, size_c;
    131 	bus_space_handle_t bsh;
    132 	int error, child;
    133 
    134 	sc->sc_dev = self;
    135 	sc->sc_phandle = phandle;
    136 
    137 	error = fdtbus_register_interrupt_controller(self, phandle,
    138 	    &gic_fdt_funcs);
    139 	if (error) {
    140 		aprint_error(": couldn't register with fdtbus: %d\n", error);
    141 		return;
    142 	}
    143 
    144 	aprint_naive("\n");
    145 	aprint_normal(": GIC\n");
    146 
    147 	if (fdtbus_get_reg(sc->sc_phandle, 0, &addr_d, &size_d) != 0) {
    148 		aprint_error(": couldn't get distributor address\n");
    149 		return;
    150 	}
    151 	if (fdtbus_get_reg(sc->sc_phandle, 1, &addr_c, &size_c) != 0) {
    152 		aprint_error(": couldn't get cpu interface address\n");
    153 		return;
    154 	}
    155 
    156 	const bus_addr_t addr = uimin(addr_d, addr_c);
    157 	const bus_size_t end = uimax(addr_d + size_d, addr_c + size_c);
    158 	const bus_size_t size = end - addr;
    159 
    160 	error = bus_space_map(faa->faa_bst, addr, size, 0, &bsh);
    161 	if (error) {
    162 		aprint_error(": couldn't map registers: %d\n", error);
    163 		return;
    164 	}
    165 
    166 	struct mpcore_attach_args mpcaa = {
    167 		.mpcaa_name = "armgic",
    168 		.mpcaa_memt = faa->faa_bst,
    169 		.mpcaa_memh = bsh,
    170 		.mpcaa_off1 = addr_d - addr,
    171 		.mpcaa_off2 = addr_c - addr,
    172 	};
    173 
    174 	sc->sc_gicdev = config_found(self, &mpcaa, NULL);
    175 
    176 	arm_fdt_irq_set_handler(armgic_irq_handler);
    177 
    178 	for (child = OF_child(phandle); child; child = OF_peer(child)) {
    179 		if (!fdtbus_status_okay(child))
    180 			continue;
    181 		const char * const v2m_compat[] = { "arm,gic-v2m-frame", NULL };
    182 		if (of_match_compatible(child, v2m_compat))
    183 			gic_fdt_attach_v2m(sc, faa->faa_bst, child);
    184 	}
    185 }
    186 
    187 static void
    188 gic_fdt_attach_v2m(struct gic_fdt_softc *sc, bus_space_tag_t bst, int phandle)
    189 {
    190 	struct gic_v2m_frame *frame;
    191 	u_int base_spi, num_spis;
    192 	bus_space_handle_t bsh;
    193 	bus_addr_t addr;
    194 	bus_size_t size;
    195 
    196 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
    197 		aprint_error_dev(sc->sc_gicdev, "couldn't get V2M address\n");
    198 		return;
    199 	}
    200 
    201 	if (bus_space_map(bst, addr, size, 0, &bsh) != 0) {
    202 		aprint_error_dev(sc->sc_gicdev, "couldn't map V2M frame\n");
    203 		return;
    204 	}
    205 	const uint32_t typer = bus_space_read_4(bst, bsh, GIC_MSI_TYPER);
    206 	bus_space_unmap(bst, bsh, size);
    207 
    208 	if (of_getprop_uint32(phandle, "arm,msi-base-spi", &base_spi))
    209 		base_spi = __SHIFTOUT(typer, GIC_MSI_TYPER_BASE);
    210 	if (of_getprop_uint32(phandle, "arm,msi-num-spis", &num_spis))
    211 		num_spis = __SHIFTOUT(typer, GIC_MSI_TYPER_NUMBER);
    212 
    213 	frame = kmem_zalloc(sizeof(*frame), KM_SLEEP);
    214 	frame->frame_reg = addr;
    215 	frame->frame_pic = pic_list[0];
    216 	frame->frame_base = base_spi;
    217 	frame->frame_count = num_spis;
    218 
    219 	if (gic_v2m_init(frame, sc->sc_gicdev, sc->sc_v2m_count++) != 0) {
    220 		aprint_error_dev(sc->sc_gicdev, "failed to initialize GICv2m\n");
    221 	} else {
    222 		aprint_normal_dev(sc->sc_gicdev, "GICv2m @ %#" PRIx64 ", SPIs %u-%u\n",
    223 		    (uint64_t)frame->frame_reg, frame->frame_base,
    224 		    frame->frame_base + frame->frame_count);
    225 	}
    226 }
    227 
    228 static void *
    229 gic_fdt_establish(device_t dev, u_int *specifier, int ipl, int flags,
    230     int (*func)(void *), void *arg)
    231 {
    232 	struct gic_fdt_softc * const sc = device_private(dev);
    233 	struct gic_fdt_irq *firq;
    234 	struct gic_fdt_irqhandler *firqh;
    235 
    236 	/* 1st cell is the interrupt type; 0 is SPI, 1 is PPI */
    237 	/* 2nd cell is the interrupt number */
    238 	/* 3rd cell is flags */
    239 
    240 	const u_int type = be32toh(specifier[0]);
    241 	const u_int intr = be32toh(specifier[1]);
    242 	const u_int irq = type == 0 ? IRQ_SPI(intr) : IRQ_PPI(intr);
    243 	const u_int trig = be32toh(specifier[2]) & 0xf;
    244 	const u_int level = (trig & 0x3) ? IST_EDGE : IST_LEVEL;
    245 
    246 	const u_int mpsafe = (flags & FDT_INTR_MPSAFE) ? IST_MPSAFE : 0;
    247 
    248 	firq = sc->sc_irq[irq];
    249 	if (firq == NULL) {
    250 		firq = kmem_alloc(sizeof(*firq), KM_SLEEP);
    251 		firq->intr_sc = sc;
    252 		firq->intr_refcnt = 0;
    253 		firq->intr_arg = arg;
    254 		firq->intr_ipl = ipl;
    255 		firq->intr_level = level;
    256 		firq->intr_mpsafe = mpsafe;
    257 		TAILQ_INIT(&firq->intr_handlers);
    258 		firq->intr_irq = irq;
    259 		if (arg == NULL) {
    260 			firq->intr_ih = intr_establish(irq, ipl, level | mpsafe,
    261 			    func, NULL);
    262 		} else {
    263 			firq->intr_ih = intr_establish(irq, ipl, level | mpsafe,
    264 			    gic_fdt_intr, firq);
    265 		}
    266 		if (firq->intr_ih == NULL) {
    267 			kmem_free(firq, sizeof(*firq));
    268 			return NULL;
    269 		}
    270 		sc->sc_irq[irq] = firq;
    271 	} else {
    272 		if (firq->intr_arg == NULL && arg != NULL) {
    273 			device_printf(dev, "cannot share irq with NULL arg\n");
    274 			return NULL;
    275 		}
    276 		if (firq->intr_ipl != ipl) {
    277 			device_printf(dev, "cannot share irq with different "
    278 			    "ipl\n");
    279 			return NULL;
    280 		}
    281 		if (firq->intr_level != level) {
    282 			device_printf(dev, "cannot share edge and level "
    283 			    "interrupts\n");
    284 			return NULL;
    285 		}
    286 		if (firq->intr_mpsafe != mpsafe) {
    287 			device_printf(dev, "cannot share between "
    288 			    "mpsafe/non-mpsafe\n");
    289 			return NULL;
    290 		}
    291 	}
    292 
    293 	firq->intr_refcnt++;
    294 
    295 	firqh = kmem_alloc(sizeof(*firqh), KM_SLEEP);
    296 	firqh->ih_mpsafe = (flags & FDT_INTR_MPSAFE) != 0;
    297 	firqh->ih_irq = firq;
    298 	firqh->ih_fn = func;
    299 	firqh->ih_arg = arg;
    300 	TAILQ_INSERT_TAIL(&firq->intr_handlers, firqh, ih_next);
    301 
    302 	return firq->intr_ih;
    303 }
    304 
    305 static void
    306 gic_fdt_disestablish(device_t dev, void *ih)
    307 {
    308 	struct gic_fdt_softc * const sc = device_private(dev);
    309 	struct gic_fdt_irqhandler *firqh;
    310 	struct gic_fdt_irq *firq;
    311 	u_int n;
    312 
    313 	for (n = 0; n < GIC_MAXIRQ; n++) {
    314 		firq = sc->sc_irq[n];
    315 		if (firq->intr_ih != ih)
    316 			continue;
    317 
    318 		KASSERT(firq->intr_refcnt > 0);
    319 
    320 		if (firq->intr_refcnt > 1)
    321 			panic("%s: cannot disestablish shared irq", __func__);
    322 
    323 		firqh = TAILQ_FIRST(&firq->intr_handlers);
    324 		kmem_free(firqh, sizeof(*firqh));
    325 		intr_disestablish(firq->intr_ih);
    326 		kmem_free(firq, sizeof(*firq));
    327 		sc->sc_irq[n] = NULL;
    328 		return;
    329 	}
    330 
    331 	panic("%s: interrupt not established", __func__);
    332 }
    333 
    334 static int
    335 gic_fdt_intr(void *priv)
    336 {
    337 	struct gic_fdt_irq *firq = priv;
    338 	struct gic_fdt_irqhandler *firqh;
    339 	int handled = 0;
    340 
    341 	TAILQ_FOREACH(firqh, &firq->intr_handlers, ih_next)
    342 		handled += firqh->ih_fn(firqh->ih_arg);
    343 
    344 	return handled;
    345 }
    346 
    347 static bool
    348 gic_fdt_intrstr(device_t dev, u_int *specifier, char *buf, size_t buflen)
    349 {
    350 	/* 1st cell is the interrupt type; 0 is SPI, 1 is PPI */
    351 	/* 2nd cell is the interrupt number */
    352 	/* 3rd cell is flags */
    353 
    354 	if (!specifier)
    355 		return false;
    356 	const u_int type = be32toh(specifier[0]);
    357 	const u_int intr = be32toh(specifier[1]);
    358 	const u_int irq = type == 0 ? IRQ_SPI(intr) : IRQ_PPI(intr);
    359 
    360 	snprintf(buf, buflen, "GIC irq %d", irq);
    361 
    362 	return true;
    363 }
    364