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