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