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