gicv3_fdt.c revision 1.11 1 /* $NetBSD: gicv3_fdt.c,v 1.11 2021/01/15 00:38:22 jmcneill 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.11 2021/01/15 00:38:22 jmcneill 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 #include <arm/cortex/gic_v2m.h>
52
53 #define GICV3_MAXIRQ 1020
54
55 #define IRQ_PPI(n) ((n) + 16)
56 #define IRQ_SPI(n) ((n) + 32)
57
58 struct gicv3_fdt_softc;
59 struct gicv3_fdt_irq;
60
61 static int gicv3_fdt_match(device_t, cfdata_t, void *);
62 static void gicv3_fdt_attach(device_t, device_t, void *);
63
64 static int gicv3_fdt_map_registers(struct gicv3_fdt_softc *);
65 #if NPCI > 0 && defined(__HAVE_PCI_MSI_MSIX)
66 static void gicv3_fdt_attach_mbi(struct gicv3_fdt_softc *);
67 static void gicv3_fdt_attach_its(struct gicv3_fdt_softc *, bus_space_tag_t, int);
68 #endif
69
70 static int gicv3_fdt_intr(void *);
71
72 static void * gicv3_fdt_establish(device_t, u_int *, int, int,
73 int (*)(void *), void *, const char *);
74 static void gicv3_fdt_disestablish(device_t, void *);
75 static bool gicv3_fdt_intrstr(device_t, u_int *, char *, size_t);
76
77 struct fdtbus_interrupt_controller_func gicv3_fdt_funcs = {
78 .establish = gicv3_fdt_establish,
79 .disestablish = gicv3_fdt_disestablish,
80 .intrstr = gicv3_fdt_intrstr
81 };
82
83 struct gicv3_fdt_irqhandler {
84 struct gicv3_fdt_irq *ih_irq;
85 int (*ih_fn)(void *);
86 void *ih_arg;
87 bool ih_mpsafe;
88 TAILQ_ENTRY(gicv3_fdt_irqhandler) ih_next;
89 };
90
91 struct gicv3_fdt_irq {
92 struct gicv3_fdt_softc *intr_sc;
93 void *intr_ih;
94 void *intr_arg;
95 int intr_refcnt;
96 int intr_ipl;
97 int intr_level;
98 int intr_mpsafe;
99 TAILQ_HEAD(, gicv3_fdt_irqhandler) intr_handlers;
100 int intr_irq;
101 };
102
103 struct gicv3_fdt_softc {
104 struct gicv3_softc sc_gic;
105 int sc_phandle;
106
107 struct gicv3_fdt_irq *sc_irq[GICV3_MAXIRQ];
108 };
109
110 struct gicv3_fdt_quirk {
111 const char *compat;
112 u_int quirks;
113 };
114
115 static const struct gicv3_fdt_quirk gicv3_fdt_quirks[] = {
116 { "rockchip,rk3399", GICV3_QUIRK_RK3399 },
117 };
118
119 CFATTACH_DECL_NEW(gicv3_fdt, sizeof(struct gicv3_fdt_softc),
120 gicv3_fdt_match, gicv3_fdt_attach, NULL, NULL);
121
122 static int
123 gicv3_fdt_match(device_t parent, cfdata_t cf, void *aux)
124 {
125 const char * const compatible[] = {
126 "arm,gic-v3",
127 NULL
128 };
129 struct fdt_attach_args * const faa = aux;
130 const int phandle = faa->faa_phandle;
131
132 return of_match_compatible(phandle, compatible);
133 }
134
135 static void
136 gicv3_fdt_attach(device_t parent, device_t self, void *aux)
137 {
138 struct gicv3_fdt_softc * const sc = device_private(self);
139 struct fdt_attach_args * const faa = aux;
140 const int phandle = faa->faa_phandle;
141 int error, n;
142
143 error = fdtbus_register_interrupt_controller(self, phandle,
144 &gicv3_fdt_funcs);
145 if (error) {
146 aprint_error(": couldn't register with fdtbus: %d\n", error);
147 return;
148 }
149
150 aprint_naive("\n");
151 aprint_normal(": GICv3\n");
152
153 sc->sc_phandle = phandle;
154 sc->sc_gic.sc_dev = self;
155 sc->sc_gic.sc_bst = faa->faa_bst;
156 sc->sc_gic.sc_dmat = faa->faa_dmat;
157
158 error = gicv3_fdt_map_registers(sc);
159 if (error) {
160 aprint_error_dev(self, "couldn't map registers\n");
161 return;
162 }
163
164 aprint_debug_dev(self, "%d redistributors\n", sc->sc_gic.sc_bsh_r_count);
165
166 /* Apply quirks */
167 for (n = 0; n < __arraycount(gicv3_fdt_quirks); n++) {
168 const char *compat[] = { gicv3_fdt_quirks[n].compat, NULL };
169 if (of_match_compatible(OF_finddevice("/"), compat)) {
170 sc->sc_gic.sc_quirks |= gicv3_fdt_quirks[n].quirks;
171 }
172 }
173
174 error = gicv3_init(&sc->sc_gic);
175 if (error) {
176 aprint_error_dev(self, "failed to initialize GIC: %d\n", error);
177 return;
178 }
179
180 #if NPCI > 0 && defined(__HAVE_PCI_MSI_MSIX)
181 if (of_hasprop(phandle, "msi-controller")) {
182 /* Message Based Interrupts */
183 gicv3_fdt_attach_mbi(sc);
184 } else {
185 /* Interrupt Translation Services */
186 for (int child = OF_child(phandle); child; child = OF_peer(child)) {
187 if (!fdtbus_status_okay(child))
188 continue;
189 const char * const its_compat[] = { "arm,gic-v3-its", NULL };
190 if (of_match_compatible(child, its_compat))
191 gicv3_fdt_attach_its(sc, faa->faa_bst, child);
192 }
193 }
194 #endif
195
196 arm_fdt_irq_set_handler(gicv3_irq_handler);
197 }
198
199 static int
200 gicv3_fdt_map_registers(struct gicv3_fdt_softc *sc)
201 {
202 struct gicv3_softc *gic = &sc->sc_gic;
203 const int phandle = sc->sc_phandle;
204 u_int redistributor_regions, redistributor_stride;
205 bus_space_handle_t bsh;
206 bus_size_t size, region_off;
207 bus_addr_t addr;
208 size_t reg_off;
209 int n, r, max_redist, redist;
210
211 if (of_getprop_uint32(phandle, "#redistributor-regions", &redistributor_regions))
212 redistributor_regions = 1;
213 if (of_getprop_uint32(phandle, "redistributor-stride", &redistributor_stride))
214 redistributor_stride = 0x20000;
215
216 /*
217 * Map GIC Distributor interface (GICD)
218 */
219 if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
220 aprint_error_dev(gic->sc_dev, "couldn't get distributor registers\n");
221 return ENXIO;
222 }
223 if (bus_space_map(sc->sc_gic.sc_bst, addr, size, 0, &sc->sc_gic.sc_bsh_d) != 0) {
224 aprint_error_dev(gic->sc_dev, "couldn't map distributor registers\n");
225 return ENXIO;
226 }
227
228 /*
229 * GIC Redistributors (GICR)
230 */
231 for (reg_off = 1, max_redist = 0, n = 0; n < redistributor_regions; n++, reg_off++) {
232 if (fdtbus_get_reg(phandle, reg_off, NULL, &size) != 0) {
233 aprint_error_dev(gic->sc_dev, "couldn't get redistributor registers\n");
234 return ENXIO;
235 }
236 max_redist += howmany(size, redistributor_stride);
237 }
238 gic->sc_bsh_r = kmem_alloc(sizeof(bus_space_handle_t) * max_redist, KM_SLEEP);
239 for (reg_off = 1, redist = 0, n = 0; n < redistributor_regions; n++, reg_off++) {
240 if (fdtbus_get_reg(phandle, reg_off, &addr, &size) != 0) {
241 aprint_error_dev(gic->sc_dev, "couldn't get redistributor registers\n");
242 return ENXIO;
243 }
244 if (bus_space_map(sc->sc_gic.sc_bst, addr, size, 0, &bsh) != 0) {
245 aprint_error_dev(gic->sc_dev, "couldn't map redistributor registers\n");
246 return ENXIO;
247 }
248 const int count = howmany(size, redistributor_stride);
249 for (r = 0, region_off = 0; r < count; r++, region_off += redistributor_stride) {
250 if (bus_space_subregion(sc->sc_gic.sc_bst, bsh, region_off, redistributor_stride, &gic->sc_bsh_r[redist++]) != 0) {
251 aprint_error_dev(gic->sc_dev, "couldn't subregion redistributor registers\n");
252 return ENXIO;
253 }
254
255 /* If this is the last redist in this region, skip to the next one */
256 const uint32_t typer = bus_space_read_4(sc->sc_gic.sc_bst, gic->sc_bsh_r[redist - 1], GICR_TYPER);
257 if (typer & GICR_TYPER_Last)
258 break;
259 }
260 }
261 gic->sc_bsh_r_count = redist;
262
263 return 0;
264 }
265
266 #if NPCI > 0 && defined(__HAVE_PCI_MSI_MSIX)
267 static void
268 gicv3_fdt_attach_mbi(struct gicv3_fdt_softc *sc)
269 {
270 struct gic_v2m_frame *frame;
271 const u_int *ranges;
272 bus_addr_t addr;
273 int len, frame_count;
274
275 if (of_hasprop(sc->sc_phandle, "mbi-alias")) {
276 aprint_error_dev(sc->sc_gic.sc_dev, "'mbi-alias' property not supported\n");
277 return;
278 }
279
280 if (fdtbus_get_reg(sc->sc_phandle, 0, &addr, NULL) != 0)
281 return;
282
283 ranges = fdtbus_get_prop(sc->sc_phandle, "mbi-ranges", &len);
284 if (ranges == NULL) {
285 aprint_error_dev(sc->sc_gic.sc_dev, "missing 'mbi-ranges' property\n");
286 return;
287 }
288
289 frame_count = 0;
290 while (len >= 8) {
291 const u_int base_spi = be32dec(&ranges[0]);
292 const u_int num_spis = be32dec(&ranges[1]);
293
294 frame = kmem_zalloc(sizeof(*frame), KM_SLEEP);
295 frame->frame_reg = addr;
296 frame->frame_pic = pic_list[0];
297 frame->frame_base = base_spi;
298 frame->frame_count = num_spis;
299
300 if (gic_v2m_init(frame, sc->sc_gic.sc_dev, frame_count++) != 0) {
301 aprint_error_dev(sc->sc_gic.sc_dev, "failed to initialize MBI frame\n");
302 } else {
303 aprint_normal_dev(sc->sc_gic.sc_dev, "MBI frame @ %#" PRIx64
304 ", SPIs %u-%u\n", frame->frame_reg,
305 frame->frame_base, frame->frame_base + frame->frame_count - 1);
306 }
307
308 ranges += 2;
309 len -= 8;
310 }
311 }
312
313 static void
314 gicv3_fdt_attach_its(struct gicv3_fdt_softc *sc, bus_space_tag_t bst, int phandle)
315 {
316 bus_space_handle_t bsh;
317 bus_addr_t addr;
318 bus_size_t size;
319
320 if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
321 aprint_error_dev(sc->sc_gic.sc_dev, "couldn't get ITS address\n");
322 return;
323 }
324
325 if (bus_space_map(bst, addr, size, 0, &bsh) != 0) {
326 aprint_error_dev(sc->sc_gic.sc_dev, "couldn't map ITS\n");
327 return;
328 }
329
330 gicv3_its_init(&sc->sc_gic, bsh, addr, 0);
331
332 aprint_verbose_dev(sc->sc_gic.sc_dev, "ITS @ %#" PRIxBUSADDR "\n",
333 addr);
334 }
335 #endif
336
337 static void *
338 gicv3_fdt_establish(device_t dev, u_int *specifier, int ipl, int flags,
339 int (*func)(void *), void *arg, const char *xname)
340 {
341 struct gicv3_fdt_softc * const sc = device_private(dev);
342 struct gicv3_fdt_irq *firq;
343 struct gicv3_fdt_irqhandler *firqh;
344
345 /* 1st cell is the interrupt type; 0 is SPI, 1 is PPI */
346 /* 2nd cell is the interrupt number */
347 /* 3rd cell is flags */
348 /* 4th cell is affinity */
349
350 const u_int type = be32toh(specifier[0]);
351 const u_int intr = be32toh(specifier[1]);
352 const u_int irq = type == 0 ? IRQ_SPI(intr) : IRQ_PPI(intr);
353 const u_int trig = be32toh(specifier[2]) & 0xf;
354 const u_int level = (trig & FDT_INTR_TYPE_DOUBLE_EDGE)
355 ? IST_EDGE : IST_LEVEL;
356
357 const u_int mpsafe = (flags & FDT_INTR_MPSAFE) ? IST_MPSAFE : 0;
358
359 firq = sc->sc_irq[irq];
360 if (firq == NULL) {
361 firq = kmem_alloc(sizeof(*firq), KM_SLEEP);
362 firq->intr_sc = sc;
363 firq->intr_refcnt = 0;
364 firq->intr_arg = arg;
365 firq->intr_ipl = ipl;
366 firq->intr_level = level;
367 firq->intr_mpsafe = mpsafe;
368 TAILQ_INIT(&firq->intr_handlers);
369 firq->intr_irq = irq;
370 if (arg == NULL) {
371 firq->intr_ih = intr_establish_xname(irq, ipl,
372 level | mpsafe, func, NULL, xname);
373 } else {
374 firq->intr_ih = intr_establish_xname(irq, ipl,
375 level | mpsafe, gicv3_fdt_intr, firq, xname);
376 }
377 if (firq->intr_ih == NULL) {
378 kmem_free(firq, sizeof(*firq));
379 return NULL;
380 }
381 sc->sc_irq[irq] = firq;
382 } else {
383 if (firq->intr_arg == NULL && arg != NULL) {
384 device_printf(dev, "cannot share irq with NULL arg\n");
385 return NULL;
386 }
387 if (firq->intr_ipl != ipl) {
388 device_printf(dev, "cannot share irq with different "
389 "ipl\n");
390 return NULL;
391 }
392 if (firq->intr_level != level) {
393 device_printf(dev, "cannot share edge and level "
394 "interrupts\n");
395 return NULL;
396 }
397 if (firq->intr_mpsafe != mpsafe) {
398 device_printf(dev, "cannot share between "
399 "mpsafe/non-mpsafe\n");
400 return NULL;
401 }
402 }
403
404 firq->intr_refcnt++;
405
406 firqh = kmem_alloc(sizeof(*firqh), KM_SLEEP);
407 firqh->ih_mpsafe = (flags & FDT_INTR_MPSAFE) != 0;
408 firqh->ih_irq = firq;
409 firqh->ih_fn = func;
410 firqh->ih_arg = arg;
411 TAILQ_INSERT_TAIL(&firq->intr_handlers, firqh, ih_next);
412
413 return firq->intr_ih;
414 }
415
416 static void
417 gicv3_fdt_disestablish(device_t dev, void *ih)
418 {
419 struct gicv3_fdt_softc * const sc = device_private(dev);
420 struct gicv3_fdt_irqhandler *firqh;
421 struct gicv3_fdt_irq *firq;
422 u_int n;
423
424 for (n = 0; n < GICV3_MAXIRQ; n++) {
425 firq = sc->sc_irq[n];
426 if (firq == NULL || firq->intr_ih != ih)
427 continue;
428
429 KASSERT(firq->intr_refcnt > 0);
430
431 if (firq->intr_refcnt > 1)
432 panic("%s: cannot disestablish shared irq", __func__);
433
434 firqh = TAILQ_FIRST(&firq->intr_handlers);
435 kmem_free(firqh, sizeof(*firqh));
436 intr_disestablish(firq->intr_ih);
437 kmem_free(firq, sizeof(*firq));
438 sc->sc_irq[n] = NULL;
439 return;
440 }
441
442 panic("%s: interrupt not established", __func__);
443 }
444
445 static int
446 gicv3_fdt_intr(void *priv)
447 {
448 struct gicv3_fdt_irq *firq = priv;
449 struct gicv3_fdt_irqhandler *firqh;
450 int handled = 0;
451
452 TAILQ_FOREACH(firqh, &firq->intr_handlers, ih_next)
453 handled += firqh->ih_fn(firqh->ih_arg);
454
455 return handled;
456 }
457
458 static bool
459 gicv3_fdt_intrstr(device_t dev, u_int *specifier, char *buf, size_t buflen)
460 {
461 /* 1st cell is the interrupt type; 0 is SPI, 1 is PPI */
462 /* 2nd cell is the interrupt number */
463 /* 3rd cell is flags */
464 /* 4th cell is affinity */
465
466 if (!specifier)
467 return false;
468 const u_int type = be32toh(specifier[0]);
469 const u_int intr = be32toh(specifier[1]);
470 const u_int irq = type == 0 ? IRQ_SPI(intr) : IRQ_PPI(intr);
471
472 snprintf(buf, buflen, "GICv3 irq %d", irq);
473
474 return true;
475 }
476