1 /* $NetBSD: gic_fdt.c,v 1.8 2017/11/30 14:42:37 skrll Exp $ */ 2 3 /*- 4 * Copyright (c) 2015-2017 Jared McNeill <jmcneill@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.8 2017/11/30 14:42:37 skrll 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 41 #include <arm/cortex/gic_intr.h> 42 #include <arm/cortex/mpcore_var.h> 43 44 #include <dev/fdt/fdtvar.h> 45 46 #define GIC_MAXIRQ 1020 47 48 static int gic_fdt_match(device_t, cfdata_t, void *); 49 static void gic_fdt_attach(device_t, device_t, void *); 50 51 static int gic_fdt_intr(void *); 52 53 static void * gic_fdt_establish(device_t, u_int *, int, int, 54 int (*)(void *), void *); 55 static void gic_fdt_disestablish(device_t, void *); 56 static bool gic_fdt_intrstr(device_t, u_int *, char *, size_t); 57 58 struct fdtbus_interrupt_controller_func gic_fdt_funcs = { 59 .establish = gic_fdt_establish, 60 .disestablish = gic_fdt_disestablish, 61 .intrstr = gic_fdt_intrstr 62 }; 63 64 struct gic_fdt_softc; 65 struct gic_fdt_irq; 66 67 struct gic_fdt_irqhandler { 68 struct gic_fdt_irq *ih_irq; 69 int (*ih_fn)(void *); 70 void *ih_arg; 71 bool ih_mpsafe; 72 TAILQ_ENTRY(gic_fdt_irqhandler) ih_next; 73 }; 74 75 struct gic_fdt_irq { 76 struct gic_fdt_softc *intr_sc; 77 void *intr_ih; 78 int intr_refcnt; 79 int intr_ipl; 80 int intr_level; 81 int intr_mpsafe; 82 TAILQ_HEAD(, gic_fdt_irqhandler) intr_handlers; 83 }; 84 85 struct gic_fdt_softc { 86 device_t sc_dev; 87 int sc_phandle; 88 89 struct gic_fdt_irq *sc_irq[GIC_MAXIRQ]; 90 }; 91 92 CFATTACH_DECL_NEW(gic_fdt, sizeof(struct gic_fdt_softc), 93 gic_fdt_match, gic_fdt_attach, NULL, NULL); 94 95 static int 96 gic_fdt_match(device_t parent, cfdata_t cf, void *aux) 97 { 98 const char * const compatible[] = { 99 "arm,gic-400", 100 "arm,cortex-a15-gic", 101 "arm,cortex-a9-gic", 102 "arm,cortex-a7-gic", 103 NULL 104 }; 105 struct fdt_attach_args * const faa = aux; 106 107 return of_compatible(faa->faa_phandle, compatible) >= 0; 108 } 109 110 static void 111 gic_fdt_attach(device_t parent, device_t self, void *aux) 112 { 113 struct gic_fdt_softc * const sc = device_private(self); 114 struct fdt_attach_args * const faa = aux; 115 bus_addr_t addr_d, addr_c; 116 bus_size_t size_d, size_c; 117 bus_space_handle_t bsh; 118 int error; 119 120 sc->sc_dev = self; 121 sc->sc_phandle = faa->faa_phandle; 122 123 error = fdtbus_register_interrupt_controller(self, faa->faa_phandle, 124 &gic_fdt_funcs); 125 if (error) { 126 aprint_error(": couldn't register with fdtbus: %d\n", error); 127 return; 128 } 129 130 aprint_naive("\n"); 131 aprint_normal(": GIC\n"); 132 133 if (fdtbus_get_reg(sc->sc_phandle, 0, &addr_d, &size_d) != 0) { 134 aprint_error(": couldn't get distributor address\n"); 135 return; 136 } 137 if (fdtbus_get_reg(sc->sc_phandle, 1, &addr_c, &size_c) != 0) { 138 aprint_error(": couldn't get cpu interface address\n"); 139 return; 140 } 141 142 const bus_addr_t addr = addr_d; 143 const bus_size_t size = (addr_c + size_c) - addr_d; 144 145 error = bus_space_map(faa->faa_bst, addr, size, 0, &bsh); 146 if (error) { 147 aprint_error(": couldn't map registers: %d\n", error); 148 return; 149 } 150 151 struct mpcore_attach_args mpcaa = { 152 .mpcaa_name = "armgic", 153 .mpcaa_memt = faa->faa_bst, 154 .mpcaa_memh = bsh, 155 .mpcaa_off1 = 0, 156 .mpcaa_off2 = addr_c - addr_d 157 }; 158 159 config_found(self, &mpcaa, NULL); 160 161 arm_fdt_irq_set_handler(armgic_irq_handler); 162 } 163 164 static void * 165 gic_fdt_establish(device_t dev, u_int *specifier, int ipl, int flags, 166 int (*func)(void *), void *arg) 167 { 168 struct gic_fdt_softc * const sc = device_private(dev); 169 struct gic_fdt_irq *firq; 170 struct gic_fdt_irqhandler *firqh; 171 172 /* 1st cell is the interrupt type; 0 is SPI, 1 is PPI */ 173 /* 2nd cell is the interrupt number */ 174 /* 3rd cell is flags */ 175 176 const u_int type = be32toh(specifier[0]); 177 const u_int intr = be32toh(specifier[1]); 178 const u_int irq = type == 0 ? IRQ_SPI(intr) : IRQ_PPI(intr); 179 const u_int trig = be32toh(specifier[2]) & 0xf; 180 const u_int level = (trig & 0x3) ? IST_EDGE : IST_LEVEL; 181 182 const u_int mpsafe = (flags & FDT_INTR_MPSAFE) ? IST_MPSAFE : 0; 183 184 firq = sc->sc_irq[irq]; 185 if (firq == NULL) { 186 firq = kmem_alloc(sizeof(*firq), KM_SLEEP); 187 firq->intr_sc = sc; 188 firq->intr_refcnt = 0; 189 firq->intr_ipl = ipl; 190 firq->intr_level = level; 191 firq->intr_mpsafe = mpsafe; 192 TAILQ_INIT(&firq->intr_handlers); 193 if (arg == NULL) { 194 firq->intr_ih = intr_establish(irq, ipl, level | mpsafe, 195 func, NULL); 196 } else { 197 firq->intr_ih = intr_establish(irq, ipl, level | mpsafe, 198 gic_fdt_intr, firq); 199 } 200 if (firq->intr_ih == NULL) { 201 kmem_free(firq, sizeof(*firq)); 202 return NULL; 203 } 204 sc->sc_irq[irq] = firq; 205 } else { 206 if (arg) { 207 device_printf(dev, "cannot share irq with NULL arg\n"); 208 return NULL; 209 } 210 if (firq->intr_ipl != ipl) { 211 device_printf(dev, "cannot share irq with different " 212 "ipl\n"); 213 return NULL; 214 } 215 if (firq->intr_level != level) { 216 device_printf(dev, "cannot share edge and level " 217 "interrupts\n"); 218 return NULL; 219 } 220 if (firq->intr_mpsafe != mpsafe) { 221 device_printf(dev, "cannot share between " 222 "mpsafe/non-mpsafe\n"); 223 return NULL; 224 } 225 } 226 227 firq->intr_refcnt++; 228 229 firqh = kmem_alloc(sizeof(*firqh), KM_SLEEP); 230 firqh->ih_mpsafe = (flags & FDT_INTR_MPSAFE) != 0; 231 firqh->ih_irq = firq; 232 firqh->ih_fn = func; 233 firqh->ih_arg = arg; 234 TAILQ_INSERT_TAIL(&firq->intr_handlers, firqh, ih_next); 235 236 return firqh; 237 } 238 239 static void 240 gic_fdt_disestablish(device_t dev, void *ih) 241 { 242 struct gic_fdt_irqhandler *firqh = ih; 243 struct gic_fdt_irq *firq = firqh->ih_irq; 244 245 KASSERT(firq->intr_refcnt > 0); 246 247 TAILQ_REMOVE(&firq->intr_handlers, firqh, ih_next); 248 kmem_free(firqh, sizeof(*firqh)); 249 250 firq->intr_refcnt--; 251 if (firq->intr_refcnt == 0) { 252 intr_disestablish(firq->intr_ih); 253 kmem_free(firq, sizeof(*firq)); 254 } 255 } 256 257 static int 258 gic_fdt_intr(void *priv) 259 { 260 struct gic_fdt_irq *firq = priv; 261 struct gic_fdt_irqhandler *firqh; 262 int handled = 0; 263 264 TAILQ_FOREACH(firqh, &firq->intr_handlers, ih_next) 265 handled += firqh->ih_fn(firqh->ih_arg); 266 267 return handled; 268 } 269 270 static bool 271 gic_fdt_intrstr(device_t dev, u_int *specifier, char *buf, size_t buflen) 272 { 273 /* 1st cell is the interrupt type; 0 is SPI, 1 is PPI */ 274 /* 2nd cell is the interrupt number */ 275 /* 3rd cell is flags */ 276 277 if (!specifier) 278 return false; 279 const u_int type = be32toh(specifier[0]); 280 const u_int intr = be32toh(specifier[1]); 281 const u_int irq = type == 0 ? IRQ_SPI(intr) : IRQ_PPI(intr); 282 283 snprintf(buf, buflen, "GIC irq %d", irq); 284 285 return true; 286 } 287