]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm/arm/gic.c
MFV r336955: 9236 nuke spa_dbgmsg
[FreeBSD/FreeBSD.git] / sys / arm / arm / gic.c
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 2011 The FreeBSD Foundation
5  * All rights reserved.
6  *
7  * Developed by Damjan Marion <damjan.marion@gmail.com>
8  *
9  * Based on OMAP4 GIC code by Ben Gray
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. The name of the company nor the name of the author may be used to
20  *    endorse or promote products derived from this software without specific
21  *    prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
38
39 #include "opt_acpi.h"
40 #include "opt_platform.h"
41
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/bus.h>
45 #include <sys/kernel.h>
46 #include <sys/ktr.h>
47 #include <sys/module.h>
48 #include <sys/malloc.h>
49 #include <sys/rman.h>
50 #include <sys/pcpu.h>
51 #include <sys/proc.h>
52 #include <sys/cpuset.h>
53 #include <sys/lock.h>
54 #include <sys/mutex.h>
55 #include <sys/smp.h>
56 #include <sys/sched.h>
57
58 #include <vm/vm.h>
59 #include <vm/pmap.h>
60
61 #include <machine/bus.h>
62 #include <machine/intr.h>
63 #include <machine/smp.h>
64
65 #ifdef FDT
66 #include <dev/fdt/fdt_intr.h>
67 #include <dev/ofw/ofw_bus_subr.h>
68 #endif
69
70 #ifdef DEV_ACPI
71 #include <contrib/dev/acpica/include/acpi.h>
72 #include <dev/acpica/acpivar.h>
73 #endif
74
75 #include <arm/arm/gic.h>
76 #include <arm/arm/gic_common.h>
77
78 #include "pic_if.h"
79 #include "msi_if.h"
80
81 /* We are using GICv2 register naming */
82
83 /* Distributor Registers */
84
85 /* CPU Registers */
86 #define GICC_CTLR               0x0000                  /* v1 ICCICR */
87 #define GICC_PMR                0x0004                  /* v1 ICCPMR */
88 #define GICC_BPR                0x0008                  /* v1 ICCBPR */
89 #define GICC_IAR                0x000C                  /* v1 ICCIAR */
90 #define GICC_EOIR               0x0010                  /* v1 ICCEOIR */
91 #define GICC_RPR                0x0014                  /* v1 ICCRPR */
92 #define GICC_HPPIR              0x0018                  /* v1 ICCHPIR */
93 #define GICC_ABPR               0x001C                  /* v1 ICCABPR */
94 #define GICC_IIDR               0x00FC                  /* v1 ICCIIDR*/
95
96 /* TYPER Registers */
97 #define GICD_TYPER_SECURITYEXT  0x400
98 #define GIC_SUPPORT_SECEXT(_sc) \
99     ((_sc->typer & GICD_TYPER_SECURITYEXT) == GICD_TYPER_SECURITYEXT)
100
101
102 #ifndef GIC_DEFAULT_ICFGR_INIT
103 #define GIC_DEFAULT_ICFGR_INIT  0x00000000
104 #endif
105
106 struct gic_irqsrc {
107         struct intr_irqsrc      gi_isrc;
108         uint32_t                gi_irq;
109         enum intr_polarity      gi_pol;
110         enum intr_trigger       gi_trig;
111 #define GI_FLAG_EARLY_EOI       (1 << 0)
112 #define GI_FLAG_MSI             (1 << 1) /* This interrupt source should only */
113                                          /* be used for MSI/MSI-X interrupts */
114 #define GI_FLAG_MSI_USED        (1 << 2) /* This irq is already allocated */
115                                          /* for a MSI/MSI-X interrupt */
116         u_int                   gi_flags;
117 };
118
119 static u_int gic_irq_cpu;
120 static int arm_gic_bind_intr(device_t dev, struct intr_irqsrc *isrc);
121
122 #ifdef SMP
123 static u_int sgi_to_ipi[GIC_LAST_SGI - GIC_FIRST_SGI + 1];
124 static u_int sgi_first_unused = GIC_FIRST_SGI;
125 #endif
126
127 #define GIC_INTR_ISRC(sc, irq)  (&sc->gic_irqs[irq].gi_isrc)
128
129 static struct resource_spec arm_gic_spec[] = {
130         { SYS_RES_MEMORY,       0,      RF_ACTIVE },    /* Distributor registers */
131         { SYS_RES_MEMORY,       1,      RF_ACTIVE },    /* CPU Interrupt Intf. registers */
132         { SYS_RES_IRQ,    0, RF_ACTIVE | RF_OPTIONAL }, /* Parent interrupt */
133         { -1, 0 }
134 };
135
136
137 #if defined(__arm__) && defined(INVARIANTS)
138 static int gic_debug_spurious = 1;
139 #else
140 static int gic_debug_spurious = 0;
141 #endif
142 TUNABLE_INT("hw.gic.debug_spurious", &gic_debug_spurious);
143
144 static u_int arm_gic_map[MAXCPU];
145
146 static struct arm_gic_softc *gic_sc = NULL;
147
148 #define gic_c_read_4(_sc, _reg)         \
149     bus_space_read_4((_sc)->gic_c_bst, (_sc)->gic_c_bsh, (_reg))
150 #define gic_c_write_4(_sc, _reg, _val)          \
151     bus_space_write_4((_sc)->gic_c_bst, (_sc)->gic_c_bsh, (_reg), (_val))
152 #define gic_d_read_4(_sc, _reg)         \
153     bus_space_read_4((_sc)->gic_d_bst, (_sc)->gic_d_bsh, (_reg))
154 #define gic_d_write_1(_sc, _reg, _val)          \
155     bus_space_write_1((_sc)->gic_d_bst, (_sc)->gic_d_bsh, (_reg), (_val))
156 #define gic_d_write_4(_sc, _reg, _val)          \
157     bus_space_write_4((_sc)->gic_d_bst, (_sc)->gic_d_bsh, (_reg), (_val))
158
159 static inline void
160 gic_irq_unmask(struct arm_gic_softc *sc, u_int irq)
161 {
162
163         gic_d_write_4(sc, GICD_ISENABLER(irq), GICD_I_MASK(irq));
164 }
165
166 static inline void
167 gic_irq_mask(struct arm_gic_softc *sc, u_int irq)
168 {
169
170         gic_d_write_4(sc, GICD_ICENABLER(irq), GICD_I_MASK(irq));
171 }
172
173 static uint8_t
174 gic_cpu_mask(struct arm_gic_softc *sc)
175 {
176         uint32_t mask;
177         int i;
178
179         /* Read the current cpuid mask by reading ITARGETSR{0..7} */
180         for (i = 0; i < 8; i++) {
181                 mask = gic_d_read_4(sc, GICD_ITARGETSR(4 * i));
182                 if (mask != 0)
183                         break;
184         }
185         /* No mask found, assume we are on CPU interface 0 */
186         if (mask == 0)
187                 return (1);
188
189         /* Collect the mask in the lower byte */
190         mask |= mask >> 16;
191         mask |= mask >> 8;
192
193         return (mask);
194 }
195
196 #ifdef SMP
197 static void
198 arm_gic_init_secondary(device_t dev)
199 {
200         struct arm_gic_softc *sc = device_get_softc(dev);
201         u_int irq, cpu;
202
203         /* Set the mask so we can find this CPU to send it IPIs */
204         cpu = PCPU_GET(cpuid);
205         arm_gic_map[cpu] = gic_cpu_mask(sc);
206
207         for (irq = 0; irq < sc->nirqs; irq += 4)
208                 gic_d_write_4(sc, GICD_IPRIORITYR(irq), 0);
209
210         /* Set all the interrupts to be in Group 0 (secure) */
211         for (irq = 0; GIC_SUPPORT_SECEXT(sc) && irq < sc->nirqs; irq += 32) {
212                 gic_d_write_4(sc, GICD_IGROUPR(irq), 0);
213         }
214
215         /* Enable CPU interface */
216         gic_c_write_4(sc, GICC_CTLR, 1);
217
218         /* Set priority mask register. */
219         gic_c_write_4(sc, GICC_PMR, 0xff);
220
221         /* Enable interrupt distribution */
222         gic_d_write_4(sc, GICD_CTLR, 0x01);
223
224         /* Unmask attached SGI interrupts. */
225         for (irq = GIC_FIRST_SGI; irq <= GIC_LAST_SGI; irq++)
226                 if (intr_isrc_init_on_cpu(GIC_INTR_ISRC(sc, irq), cpu))
227                         gic_irq_unmask(sc, irq);
228
229         /* Unmask attached PPI interrupts. */
230         for (irq = GIC_FIRST_PPI; irq <= GIC_LAST_PPI; irq++)
231                 if (intr_isrc_init_on_cpu(GIC_INTR_ISRC(sc, irq), cpu))
232                         gic_irq_unmask(sc, irq);
233 }
234 #endif /* SMP */
235
236 static int
237 arm_gic_register_isrcs(struct arm_gic_softc *sc, uint32_t num)
238 {
239         int error;
240         uint32_t irq;
241         struct gic_irqsrc *irqs;
242         struct intr_irqsrc *isrc;
243         const char *name;
244
245         irqs = malloc(num * sizeof(struct gic_irqsrc), M_DEVBUF,
246             M_WAITOK | M_ZERO);
247
248         name = device_get_nameunit(sc->gic_dev);
249         for (irq = 0; irq < num; irq++) {
250                 irqs[irq].gi_irq = irq;
251                 irqs[irq].gi_pol = INTR_POLARITY_CONFORM;
252                 irqs[irq].gi_trig = INTR_TRIGGER_CONFORM;
253
254                 isrc = &irqs[irq].gi_isrc;
255                 if (irq <= GIC_LAST_SGI) {
256                         error = intr_isrc_register(isrc, sc->gic_dev,
257                             INTR_ISRCF_IPI, "%s,i%u", name, irq - GIC_FIRST_SGI);
258                 } else if (irq <= GIC_LAST_PPI) {
259                         error = intr_isrc_register(isrc, sc->gic_dev,
260                             INTR_ISRCF_PPI, "%s,p%u", name, irq - GIC_FIRST_PPI);
261                 } else {
262                         error = intr_isrc_register(isrc, sc->gic_dev, 0,
263                             "%s,s%u", name, irq - GIC_FIRST_SPI);
264                 }
265                 if (error != 0) {
266                         /* XXX call intr_isrc_deregister() */
267                         free(irqs, M_DEVBUF);
268                         return (error);
269                 }
270         }
271         sc->gic_irqs = irqs;
272         sc->nirqs = num;
273         return (0);
274 }
275
276 static void
277 arm_gic_reserve_msi_range(device_t dev, u_int start, u_int count)
278 {
279         struct arm_gic_softc *sc;
280         int i;
281
282         sc = device_get_softc(dev);
283
284         KASSERT((start + count) < sc->nirqs,
285             ("%s: Trying to allocate too many MSI IRQs: %d + %d > %d", __func__,
286             start, count, sc->nirqs));
287         for (i = 0; i < count; i++) {
288                 KASSERT(sc->gic_irqs[start + i].gi_isrc.isrc_handlers == 0,
289                     ("%s: MSI interrupt %d already has a handler", __func__,
290                     count + i));
291                 KASSERT(sc->gic_irqs[start + i].gi_pol == INTR_POLARITY_CONFORM,
292                     ("%s: MSI interrupt %d already has a polarity", __func__,
293                     count + i));
294                 KASSERT(sc->gic_irqs[start + i].gi_trig == INTR_TRIGGER_CONFORM,
295                     ("%s: MSI interrupt %d already has a trigger", __func__,
296                     count + i));
297                 sc->gic_irqs[start + i].gi_pol = INTR_POLARITY_HIGH;
298                 sc->gic_irqs[start + i].gi_trig = INTR_TRIGGER_EDGE;
299                 sc->gic_irqs[start + i].gi_flags |= GI_FLAG_MSI;
300         }
301 }
302
303 int
304 arm_gic_attach(device_t dev)
305 {
306         struct          arm_gic_softc *sc;
307         int             i;
308         uint32_t        icciidr, mask, nirqs;
309
310         if (gic_sc)
311                 return (ENXIO);
312
313         sc = device_get_softc(dev);
314
315         if (bus_alloc_resources(dev, arm_gic_spec, sc->gic_res)) {
316                 device_printf(dev, "could not allocate resources\n");
317                 return (ENXIO);
318         }
319
320         sc->gic_dev = dev;
321         gic_sc = sc;
322
323         /* Initialize mutex */
324         mtx_init(&sc->mutex, "GIC lock", NULL, MTX_SPIN);
325
326         /* Distributor Interface */
327         sc->gic_d_bst = rman_get_bustag(sc->gic_res[0]);
328         sc->gic_d_bsh = rman_get_bushandle(sc->gic_res[0]);
329
330         /* CPU Interface */
331         sc->gic_c_bst = rman_get_bustag(sc->gic_res[1]);
332         sc->gic_c_bsh = rman_get_bushandle(sc->gic_res[1]);
333
334         /* Disable interrupt forwarding to the CPU interface */
335         gic_d_write_4(sc, GICD_CTLR, 0x00);
336
337         /* Get the number of interrupts */
338         sc->typer = gic_d_read_4(sc, GICD_TYPER);
339         nirqs = GICD_TYPER_I_NUM(sc->typer);
340
341         if (arm_gic_register_isrcs(sc, nirqs)) {
342                 device_printf(dev, "could not register irqs\n");
343                 goto cleanup;
344         }
345
346         icciidr = gic_c_read_4(sc, GICC_IIDR);
347         device_printf(dev,
348             "pn 0x%x, arch 0x%x, rev 0x%x, implementer 0x%x irqs %u\n",
349             GICD_IIDR_PROD(icciidr), GICD_IIDR_VAR(icciidr),
350             GICD_IIDR_REV(icciidr), GICD_IIDR_IMPL(icciidr), sc->nirqs);
351         sc->gic_iidr = icciidr;
352
353         /* Set all global interrupts to be level triggered, active low. */
354         for (i = 32; i < sc->nirqs; i += 16) {
355                 gic_d_write_4(sc, GICD_ICFGR(i), GIC_DEFAULT_ICFGR_INIT);
356         }
357
358         /* Disable all interrupts. */
359         for (i = 32; i < sc->nirqs; i += 32) {
360                 gic_d_write_4(sc, GICD_ICENABLER(i), 0xFFFFFFFF);
361         }
362
363         /* Find the current cpu mask */
364         mask = gic_cpu_mask(sc);
365         /* Set the mask so we can find this CPU to send it IPIs */
366         arm_gic_map[PCPU_GET(cpuid)] = mask;
367         /* Set all four targets to this cpu */
368         mask |= mask << 8;
369         mask |= mask << 16;
370
371         for (i = 0; i < sc->nirqs; i += 4) {
372                 gic_d_write_4(sc, GICD_IPRIORITYR(i), 0);
373                 if (i > 32) {
374                         gic_d_write_4(sc, GICD_ITARGETSR(i), mask);
375                 }
376         }
377
378         /* Set all the interrupts to be in Group 0 (secure) */
379         for (i = 0; GIC_SUPPORT_SECEXT(sc) && i < sc->nirqs; i += 32) {
380                 gic_d_write_4(sc, GICD_IGROUPR(i), 0);
381         }
382
383         /* Enable CPU interface */
384         gic_c_write_4(sc, GICC_CTLR, 1);
385
386         /* Set priority mask register. */
387         gic_c_write_4(sc, GICC_PMR, 0xff);
388
389         /* Enable interrupt distribution */
390         gic_d_write_4(sc, GICD_CTLR, 0x01);
391         return (0);
392
393 cleanup:
394         arm_gic_detach(dev);
395         return(ENXIO);
396 }
397
398 int
399 arm_gic_detach(device_t dev)
400 {
401         struct arm_gic_softc *sc;
402
403         sc = device_get_softc(dev);
404
405         if (sc->gic_irqs != NULL)
406                 free(sc->gic_irqs, M_DEVBUF);
407
408         bus_release_resources(dev, arm_gic_spec, sc->gic_res);
409
410         return (0);
411 }
412
413 static int
414 arm_gic_print_child(device_t bus, device_t child)
415 {
416         struct resource_list *rl;
417         int rv;
418
419         rv = bus_print_child_header(bus, child);
420
421         rl = BUS_GET_RESOURCE_LIST(bus, child);
422         if (rl != NULL) {
423                 rv += resource_list_print_type(rl, "mem", SYS_RES_MEMORY,
424                     "%#jx");
425                 rv += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%jd");
426         }
427
428         rv += bus_print_child_footer(bus, child);
429
430         return (rv);
431 }
432
433 static struct resource *
434 arm_gic_alloc_resource(device_t bus, device_t child, int type, int *rid,
435     rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
436 {
437         struct arm_gic_softc *sc;
438         struct resource_list_entry *rle;
439         struct resource_list *rl;
440         int j;
441
442         KASSERT(type == SYS_RES_MEMORY, ("Invalid resoure type %x", type));
443
444         sc = device_get_softc(bus);
445
446         /*
447          * Request for the default allocation with a given rid: use resource
448          * list stored in the local device info.
449          */
450         if (RMAN_IS_DEFAULT_RANGE(start, end)) {
451                 rl = BUS_GET_RESOURCE_LIST(bus, child);
452
453                 if (type == SYS_RES_IOPORT)
454                         type = SYS_RES_MEMORY;
455
456                 rle = resource_list_find(rl, type, *rid);
457                 if (rle == NULL) {
458                         if (bootverbose)
459                                 device_printf(bus, "no default resources for "
460                                     "rid = %d, type = %d\n", *rid, type);
461                         return (NULL);
462                 }
463                 start = rle->start;
464                 end = rle->end;
465                 count = rle->count;
466         }
467
468         /* Remap through ranges property */
469         for (j = 0; j < sc->nranges; j++) {
470                 if (start >= sc->ranges[j].bus && end <
471                     sc->ranges[j].bus + sc->ranges[j].size) {
472                         start -= sc->ranges[j].bus;
473                         start += sc->ranges[j].host;
474                         end -= sc->ranges[j].bus;
475                         end += sc->ranges[j].host;
476                         break;
477                 }
478         }
479         if (j == sc->nranges && sc->nranges != 0) {
480                 if (bootverbose)
481                         device_printf(bus, "Could not map resource "
482                             "%#jx-%#jx\n", (uintmax_t)start, (uintmax_t)end);
483
484                 return (NULL);
485         }
486
487         return (bus_generic_alloc_resource(bus, child, type, rid, start, end,
488             count, flags));
489 }
490
491 static int
492 arm_gic_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
493 {
494         struct arm_gic_softc *sc;
495
496         sc = device_get_softc(dev);
497
498         switch(which) {
499         case GIC_IVAR_HW_REV:
500                 KASSERT(GICD_IIDR_VAR(sc->gic_iidr) < 3 &&
501                     GICD_IIDR_VAR(sc->gic_iidr) != 0,
502                     ("arm_gic_read_ivar: Unknown IIDR revision %u (%.08x)",
503                      GICD_IIDR_VAR(sc->gic_iidr), sc->gic_iidr));
504                 *result = GICD_IIDR_VAR(sc->gic_iidr);
505                 return (0);
506         case GIC_IVAR_BUS:
507                 KASSERT(sc->gic_bus != GIC_BUS_UNKNOWN,
508                     ("arm_gic_read_ivar: Unknown bus type"));
509                 KASSERT(sc->gic_bus <= GIC_BUS_MAX,
510                     ("arm_gic_read_ivar: Invalid bus type %u", sc->gic_bus));
511                 *result = sc->gic_bus;
512                 return (0);
513         }
514
515         return (ENOENT);
516 }
517
518 int
519 arm_gic_intr(void *arg)
520 {
521         struct arm_gic_softc *sc = arg;
522         struct gic_irqsrc *gi;
523         uint32_t irq_active_reg, irq;
524         struct trapframe *tf;
525
526         irq_active_reg = gic_c_read_4(sc, GICC_IAR);
527         irq = irq_active_reg & 0x3FF;
528
529         /*
530          * 1. We do EOI here because recent read value from active interrupt
531          *    register must be used for it. Another approach is to save this
532          *    value into associated interrupt source.
533          * 2. EOI must be done on same CPU where interrupt has fired. Thus
534          *    we must ensure that interrupted thread does not migrate to
535          *    another CPU.
536          * 3. EOI cannot be delayed by any preemption which could happen on
537          *    critical_exit() used in MI intr code, when interrupt thread is
538          *    scheduled. See next point.
539          * 4. IPI_RENDEZVOUS assumes that no preemption is permitted during
540          *    an action and any use of critical_exit() could break this
541          *    assumption. See comments within smp_rendezvous_action().
542          * 5. We always return FILTER_HANDLED as this is an interrupt
543          *    controller dispatch function. Otherwise, in cascaded interrupt
544          *    case, the whole interrupt subtree would be masked.
545          */
546
547         if (irq >= sc->nirqs) {
548                 if (gic_debug_spurious)
549                         device_printf(sc->gic_dev,
550                             "Spurious interrupt detected: last irq: %d on CPU%d\n",
551                             sc->last_irq[PCPU_GET(cpuid)], PCPU_GET(cpuid));
552                 return (FILTER_HANDLED);
553         }
554
555         tf = curthread->td_intr_frame;
556 dispatch_irq:
557         gi = sc->gic_irqs + irq;
558         /*
559          * Note that GIC_FIRST_SGI is zero and is not used in 'if' statement
560          * as compiler complains that comparing u_int >= 0 is always true.
561          */
562         if (irq <= GIC_LAST_SGI) {
563 #ifdef SMP
564                 /* Call EOI for all IPI before dispatch. */
565                 gic_c_write_4(sc, GICC_EOIR, irq_active_reg);
566                 intr_ipi_dispatch(sgi_to_ipi[gi->gi_irq], tf);
567                 goto next_irq;
568 #else
569                 device_printf(sc->gic_dev, "SGI %u on UP system detected\n",
570                     irq - GIC_FIRST_SGI);
571                 gic_c_write_4(sc, GICC_EOIR, irq_active_reg);
572                 goto next_irq;
573 #endif
574         }
575
576         if (gic_debug_spurious)
577                 sc->last_irq[PCPU_GET(cpuid)] = irq;
578         if ((gi->gi_flags & GI_FLAG_EARLY_EOI) == GI_FLAG_EARLY_EOI)
579                 gic_c_write_4(sc, GICC_EOIR, irq_active_reg);
580
581         if (intr_isrc_dispatch(&gi->gi_isrc, tf) != 0) {
582                 gic_irq_mask(sc, irq);
583                 if ((gi->gi_flags & GI_FLAG_EARLY_EOI) != GI_FLAG_EARLY_EOI)
584                         gic_c_write_4(sc, GICC_EOIR, irq_active_reg);
585                 device_printf(sc->gic_dev, "Stray irq %u disabled\n", irq);
586         }
587
588 next_irq:
589         arm_irq_memory_barrier(irq);
590         irq_active_reg = gic_c_read_4(sc, GICC_IAR);
591         irq = irq_active_reg & 0x3FF;
592         if (irq < sc->nirqs)
593                 goto dispatch_irq;
594
595         return (FILTER_HANDLED);
596 }
597
598 static void
599 gic_config(struct arm_gic_softc *sc, u_int irq, enum intr_trigger trig,
600     enum intr_polarity pol)
601 {
602         uint32_t reg;
603         uint32_t mask;
604
605         if (irq < GIC_FIRST_SPI)
606                 return;
607
608         mtx_lock_spin(&sc->mutex);
609
610         reg = gic_d_read_4(sc, GICD_ICFGR(irq));
611         mask = (reg >> 2*(irq % 16)) & 0x3;
612
613         if (pol == INTR_POLARITY_LOW) {
614                 mask &= ~GICD_ICFGR_POL_MASK;
615                 mask |= GICD_ICFGR_POL_LOW;
616         } else if (pol == INTR_POLARITY_HIGH) {
617                 mask &= ~GICD_ICFGR_POL_MASK;
618                 mask |= GICD_ICFGR_POL_HIGH;
619         }
620
621         if (trig == INTR_TRIGGER_LEVEL) {
622                 mask &= ~GICD_ICFGR_TRIG_MASK;
623                 mask |= GICD_ICFGR_TRIG_LVL;
624         } else if (trig == INTR_TRIGGER_EDGE) {
625                 mask &= ~GICD_ICFGR_TRIG_MASK;
626                 mask |= GICD_ICFGR_TRIG_EDGE;
627         }
628
629         /* Set mask */
630         reg = reg & ~(0x3 << 2*(irq % 16));
631         reg = reg | (mask << 2*(irq % 16));
632         gic_d_write_4(sc, GICD_ICFGR(irq), reg);
633
634         mtx_unlock_spin(&sc->mutex);
635 }
636
637 static int
638 gic_bind(struct arm_gic_softc *sc, u_int irq, cpuset_t *cpus)
639 {
640         uint32_t cpu, end, mask;
641
642         end = min(mp_ncpus, 8);
643         for (cpu = end; cpu < MAXCPU; cpu++)
644                 if (CPU_ISSET(cpu, cpus))
645                         return (EINVAL);
646
647         for (mask = 0, cpu = 0; cpu < end; cpu++)
648                 if (CPU_ISSET(cpu, cpus))
649                         mask |= arm_gic_map[cpu];
650
651         gic_d_write_1(sc, GICD_ITARGETSR(0) + irq, mask);
652         return (0);
653 }
654
655 #ifdef FDT
656 static int
657 gic_map_fdt(device_t dev, u_int ncells, pcell_t *cells, u_int *irqp,
658     enum intr_polarity *polp, enum intr_trigger *trigp)
659 {
660
661         if (ncells == 1) {
662                 *irqp = cells[0];
663                 *polp = INTR_POLARITY_CONFORM;
664                 *trigp = INTR_TRIGGER_CONFORM;
665                 return (0);
666         }
667         if (ncells == 3) {
668                 u_int irq, tripol;
669
670                 /*
671                  * The 1st cell is the interrupt type:
672                  *      0 = SPI
673                  *      1 = PPI
674                  * The 2nd cell contains the interrupt number:
675                  *      [0 - 987] for SPI
676                  *      [0 -  15] for PPI
677                  * The 3rd cell is the flags, encoded as follows:
678                  *   bits[3:0] trigger type and level flags
679                  *      1 = low-to-high edge triggered
680                  *      2 = high-to-low edge triggered
681                  *      4 = active high level-sensitive
682                  *      8 = active low level-sensitive
683                  *   bits[15:8] PPI interrupt cpu mask
684                  *      Each bit corresponds to each of the 8 possible cpus
685                  *      attached to the GIC.  A bit set to '1' indicated
686                  *      the interrupt is wired to that CPU.
687                  */
688                 switch (cells[0]) {
689                 case 0:
690                         irq = GIC_FIRST_SPI + cells[1];
691                         /* SPI irq is checked later. */
692                         break;
693                 case 1:
694                         irq = GIC_FIRST_PPI + cells[1];
695                         if (irq > GIC_LAST_PPI) {
696                                 device_printf(dev, "unsupported PPI interrupt "
697                                     "number %u\n", cells[1]);
698                                 return (EINVAL);
699                         }
700                         break;
701                 default:
702                         device_printf(dev, "unsupported interrupt type "
703                             "configuration %u\n", cells[0]);
704                         return (EINVAL);
705                 }
706
707                 tripol = cells[2] & 0xff;
708                 if (tripol & 0xf0 || (tripol & FDT_INTR_LOW_MASK &&
709                     cells[0] == 0))
710                         device_printf(dev, "unsupported trigger/polarity "
711                             "configuration 0x%02x\n", tripol);
712
713                 *irqp = irq;
714                 *polp = INTR_POLARITY_CONFORM;
715                 *trigp = tripol & FDT_INTR_EDGE_MASK ?
716                     INTR_TRIGGER_EDGE : INTR_TRIGGER_LEVEL;
717                 return (0);
718         }
719         return (EINVAL);
720 }
721 #endif
722
723 static int
724 gic_map_msi(device_t dev, struct intr_map_data_msi *msi_data, u_int *irqp,
725     enum intr_polarity *polp, enum intr_trigger *trigp)
726 {
727         struct gic_irqsrc *gi;
728
729         /* Map a non-GICv2m MSI */
730         gi = (struct gic_irqsrc *)msi_data->isrc;
731         if (gi == NULL)
732                 return (ENXIO);
733
734         *irqp = gi->gi_irq;
735
736         /* MSI/MSI-X interrupts are always edge triggered with high polarity */
737         *polp = INTR_POLARITY_HIGH;
738         *trigp = INTR_TRIGGER_EDGE;
739
740         return (0);
741 }
742
743 static int
744 gic_map_intr(device_t dev, struct intr_map_data *data, u_int *irqp,
745     enum intr_polarity *polp, enum intr_trigger *trigp)
746 {
747         u_int irq;
748         enum intr_polarity pol;
749         enum intr_trigger trig;
750         struct arm_gic_softc *sc;
751         struct intr_map_data_msi *dam;
752 #ifdef FDT
753         struct intr_map_data_fdt *daf;
754 #endif
755 #ifdef DEV_ACPI
756         struct intr_map_data_acpi *daa;
757 #endif
758
759         sc = device_get_softc(dev);
760         switch (data->type) {
761 #ifdef FDT
762         case INTR_MAP_DATA_FDT:
763                 daf = (struct intr_map_data_fdt *)data;
764                 if (gic_map_fdt(dev, daf->ncells, daf->cells, &irq, &pol,
765                     &trig) != 0)
766                         return (EINVAL);
767                 KASSERT(irq >= sc->nirqs ||
768                     (sc->gic_irqs[irq].gi_flags & GI_FLAG_MSI) == 0,
769                     ("%s: Attempting to map a MSI interrupt from FDT",
770                     __func__));
771                 break;
772 #endif
773 #ifdef DEV_ACPI
774         case INTR_MAP_DATA_ACPI:
775                 daa = (struct intr_map_data_acpi *)data;
776                 irq = daa->irq;
777                 pol = daa->pol;
778                 trig = daa->trig;
779                 break;
780 #endif
781         case INTR_MAP_DATA_MSI:
782                 /* Non-GICv2m MSI */
783                 dam = (struct intr_map_data_msi *)data;
784                 if (gic_map_msi(dev, dam, &irq, &pol, &trig) != 0)
785                         return (EINVAL);
786                 break;
787         default:
788                 return (ENOTSUP);
789         }
790
791         if (irq >= sc->nirqs)
792                 return (EINVAL);
793         if (pol != INTR_POLARITY_CONFORM && pol != INTR_POLARITY_LOW &&
794             pol != INTR_POLARITY_HIGH)
795                 return (EINVAL);
796         if (trig != INTR_TRIGGER_CONFORM && trig != INTR_TRIGGER_EDGE &&
797             trig != INTR_TRIGGER_LEVEL)
798                 return (EINVAL);
799
800         *irqp = irq;
801         if (polp != NULL)
802                 *polp = pol;
803         if (trigp != NULL)
804                 *trigp = trig;
805         return (0);
806 }
807
808 static int
809 arm_gic_map_intr(device_t dev, struct intr_map_data *data,
810     struct intr_irqsrc **isrcp)
811 {
812         int error;
813         u_int irq;
814         struct arm_gic_softc *sc;
815
816         error = gic_map_intr(dev, data, &irq, NULL, NULL);
817         if (error == 0) {
818                 sc = device_get_softc(dev);
819                 *isrcp = GIC_INTR_ISRC(sc, irq);
820         }
821         return (error);
822 }
823
824 static int
825 arm_gic_setup_intr(device_t dev, struct intr_irqsrc *isrc,
826     struct resource *res, struct intr_map_data *data)
827 {
828         struct arm_gic_softc *sc = device_get_softc(dev);
829         struct gic_irqsrc *gi = (struct gic_irqsrc *)isrc;
830         enum intr_trigger trig;
831         enum intr_polarity pol;
832
833         if ((gi->gi_flags & GI_FLAG_MSI) == GI_FLAG_MSI) {
834                 /* GICv2m MSI */
835                 pol = gi->gi_pol;
836                 trig = gi->gi_trig;
837                 KASSERT(pol == INTR_POLARITY_HIGH,
838                     ("%s: MSI interrupts must be active-high", __func__));
839                 KASSERT(trig == INTR_TRIGGER_EDGE,
840                     ("%s: MSI interrupts must be edge triggered", __func__));
841         } else if (data != NULL) {
842                 u_int irq;
843
844                 /* Get config for resource. */
845                 if (gic_map_intr(dev, data, &irq, &pol, &trig) ||
846                     gi->gi_irq != irq)
847                         return (EINVAL);
848         } else {
849                 pol = INTR_POLARITY_CONFORM;
850                 trig = INTR_TRIGGER_CONFORM;
851         }
852
853         /* Compare config if this is not first setup. */
854         if (isrc->isrc_handlers != 0) {
855                 if ((pol != INTR_POLARITY_CONFORM && pol != gi->gi_pol) ||
856                     (trig != INTR_TRIGGER_CONFORM && trig != gi->gi_trig))
857                         return (EINVAL);
858                 else
859                         return (0);
860         }
861
862         /* For MSI/MSI-X we should have already configured these */
863         if ((gi->gi_flags & GI_FLAG_MSI) == 0) {
864                 if (pol == INTR_POLARITY_CONFORM)
865                         pol = INTR_POLARITY_LOW;        /* just pick some */
866                 if (trig == INTR_TRIGGER_CONFORM)
867                         trig = INTR_TRIGGER_EDGE;       /* just pick some */
868
869                 gi->gi_pol = pol;
870                 gi->gi_trig = trig;
871
872                 /* Edge triggered interrupts need an early EOI sent */
873                 if (gi->gi_trig == INTR_TRIGGER_EDGE)
874                         gi->gi_flags |= GI_FLAG_EARLY_EOI;
875         }
876
877         /*
878          * XXX - In case that per CPU interrupt is going to be enabled in time
879          *       when SMP is already started, we need some IPI call which
880          *       enables it on others CPUs. Further, it's more complicated as
881          *       pic_enable_source() and pic_disable_source() should act on
882          *       per CPU basis only. Thus, it should be solved here somehow.
883          */
884         if (isrc->isrc_flags & INTR_ISRCF_PPI)
885                 CPU_SET(PCPU_GET(cpuid), &isrc->isrc_cpu);
886
887         gic_config(sc, gi->gi_irq, gi->gi_trig, gi->gi_pol);
888         arm_gic_bind_intr(dev, isrc);
889         return (0);
890 }
891
892 static int
893 arm_gic_teardown_intr(device_t dev, struct intr_irqsrc *isrc,
894     struct resource *res, struct intr_map_data *data)
895 {
896         struct gic_irqsrc *gi = (struct gic_irqsrc *)isrc;
897
898         if (isrc->isrc_handlers == 0 && (gi->gi_flags & GI_FLAG_MSI) == 0) {
899                 gi->gi_pol = INTR_POLARITY_CONFORM;
900                 gi->gi_trig = INTR_TRIGGER_CONFORM;
901         }
902         return (0);
903 }
904
905 static void
906 arm_gic_enable_intr(device_t dev, struct intr_irqsrc *isrc)
907 {
908         struct arm_gic_softc *sc = device_get_softc(dev);
909         struct gic_irqsrc *gi = (struct gic_irqsrc *)isrc;
910
911         arm_irq_memory_barrier(gi->gi_irq);
912         gic_irq_unmask(sc, gi->gi_irq);
913 }
914
915 static void
916 arm_gic_disable_intr(device_t dev, struct intr_irqsrc *isrc)
917 {
918         struct arm_gic_softc *sc = device_get_softc(dev);
919         struct gic_irqsrc *gi = (struct gic_irqsrc *)isrc;
920
921         gic_irq_mask(sc, gi->gi_irq);
922 }
923
924 static void
925 arm_gic_pre_ithread(device_t dev, struct intr_irqsrc *isrc)
926 {
927         struct arm_gic_softc *sc = device_get_softc(dev);
928         struct gic_irqsrc *gi = (struct gic_irqsrc *)isrc;
929
930         arm_gic_disable_intr(dev, isrc);
931         gic_c_write_4(sc, GICC_EOIR, gi->gi_irq);
932 }
933
934 static void
935 arm_gic_post_ithread(device_t dev, struct intr_irqsrc *isrc)
936 {
937
938         arm_irq_memory_barrier(0);
939         arm_gic_enable_intr(dev, isrc);
940 }
941
942 static void
943 arm_gic_post_filter(device_t dev, struct intr_irqsrc *isrc)
944 {
945         struct arm_gic_softc *sc = device_get_softc(dev);
946         struct gic_irqsrc *gi = (struct gic_irqsrc *)isrc;
947
948         /* EOI for edge-triggered done earlier. */
949         if ((gi->gi_flags & GI_FLAG_EARLY_EOI) == GI_FLAG_EARLY_EOI)
950                 return;
951
952         arm_irq_memory_barrier(0);
953         gic_c_write_4(sc, GICC_EOIR, gi->gi_irq);
954 }
955
956 static int
957 arm_gic_bind_intr(device_t dev, struct intr_irqsrc *isrc)
958 {
959         struct arm_gic_softc *sc = device_get_softc(dev);
960         struct gic_irqsrc *gi = (struct gic_irqsrc *)isrc;
961
962         if (gi->gi_irq < GIC_FIRST_SPI)
963                 return (EINVAL);
964
965         if (CPU_EMPTY(&isrc->isrc_cpu)) {
966                 gic_irq_cpu = intr_irq_next_cpu(gic_irq_cpu, &all_cpus);
967                 CPU_SETOF(gic_irq_cpu, &isrc->isrc_cpu);
968         }
969         return (gic_bind(sc, gi->gi_irq, &isrc->isrc_cpu));
970 }
971
972 #ifdef SMP
973 static void
974 arm_gic_ipi_send(device_t dev, struct intr_irqsrc *isrc, cpuset_t cpus,
975     u_int ipi)
976 {
977         struct arm_gic_softc *sc = device_get_softc(dev);
978         struct gic_irqsrc *gi = (struct gic_irqsrc *)isrc;
979         uint32_t val = 0, i;
980
981         for (i = 0; i < MAXCPU; i++)
982                 if (CPU_ISSET(i, &cpus))
983                         val |= arm_gic_map[i] << GICD_SGI_TARGET_SHIFT;
984
985         gic_d_write_4(sc, GICD_SGIR, val | gi->gi_irq);
986 }
987
988 static int
989 arm_gic_ipi_setup(device_t dev, u_int ipi, struct intr_irqsrc **isrcp)
990 {
991         struct intr_irqsrc *isrc;
992         struct arm_gic_softc *sc = device_get_softc(dev);
993
994         if (sgi_first_unused > GIC_LAST_SGI)
995                 return (ENOSPC);
996
997         isrc = GIC_INTR_ISRC(sc, sgi_first_unused);
998         sgi_to_ipi[sgi_first_unused++] = ipi;
999
1000         CPU_SET(PCPU_GET(cpuid), &isrc->isrc_cpu);
1001
1002         *isrcp = isrc;
1003         return (0);
1004 }
1005 #endif
1006
1007 static device_method_t arm_gic_methods[] = {
1008         /* Bus interface */
1009         DEVMETHOD(bus_print_child,      arm_gic_print_child),
1010         DEVMETHOD(bus_add_child,        bus_generic_add_child),
1011         DEVMETHOD(bus_alloc_resource,   arm_gic_alloc_resource),
1012         DEVMETHOD(bus_release_resource, bus_generic_release_resource),
1013         DEVMETHOD(bus_activate_resource,bus_generic_activate_resource),
1014         DEVMETHOD(bus_read_ivar,        arm_gic_read_ivar),
1015
1016         /* Interrupt controller interface */
1017         DEVMETHOD(pic_disable_intr,     arm_gic_disable_intr),
1018         DEVMETHOD(pic_enable_intr,      arm_gic_enable_intr),
1019         DEVMETHOD(pic_map_intr,         arm_gic_map_intr),
1020         DEVMETHOD(pic_setup_intr,       arm_gic_setup_intr),
1021         DEVMETHOD(pic_teardown_intr,    arm_gic_teardown_intr),
1022         DEVMETHOD(pic_post_filter,      arm_gic_post_filter),
1023         DEVMETHOD(pic_post_ithread,     arm_gic_post_ithread),
1024         DEVMETHOD(pic_pre_ithread,      arm_gic_pre_ithread),
1025 #ifdef SMP
1026         DEVMETHOD(pic_bind_intr,        arm_gic_bind_intr),
1027         DEVMETHOD(pic_init_secondary,   arm_gic_init_secondary),
1028         DEVMETHOD(pic_ipi_send,         arm_gic_ipi_send),
1029         DEVMETHOD(pic_ipi_setup,        arm_gic_ipi_setup),
1030 #endif
1031         { 0, 0 }
1032 };
1033
1034 DEFINE_CLASS_0(gic, arm_gic_driver, arm_gic_methods,
1035     sizeof(struct arm_gic_softc));
1036
1037 /*
1038  * GICv2m support -- the GICv2 MSI/MSI-X controller.
1039  */
1040
1041 #define GICV2M_MSI_TYPER        0x008
1042 #define  MSI_TYPER_SPI_BASE(x)  (((x) >> 16) & 0x3ff)
1043 #define  MSI_TYPER_SPI_COUNT(x) (((x) >> 0) & 0x3ff)
1044 #define GICv2M_MSI_SETSPI_NS    0x040
1045 #define GICV2M_MSI_IIDR         0xFCC
1046
1047 int
1048 arm_gicv2m_attach(device_t dev)
1049 {
1050         struct arm_gicv2m_softc *sc;
1051         uint32_t typer;
1052         int rid;
1053
1054         sc = device_get_softc(dev);
1055
1056         rid = 0;
1057         sc->sc_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
1058             RF_ACTIVE);
1059         if (sc->sc_mem == NULL) {
1060                 device_printf(dev, "Unable to allocate resources\n");
1061                 return (ENXIO);
1062         }
1063
1064         typer = bus_read_4(sc->sc_mem, GICV2M_MSI_TYPER);
1065         sc->sc_spi_start = MSI_TYPER_SPI_BASE(typer);
1066         sc->sc_spi_count = MSI_TYPER_SPI_COUNT(typer);
1067         sc->sc_spi_end = sc->sc_spi_start + sc->sc_spi_count;
1068
1069         /* Reserve these interrupts for MSI/MSI-X use */
1070         arm_gic_reserve_msi_range(device_get_parent(dev), sc->sc_spi_start,
1071             sc->sc_spi_count);
1072
1073         mtx_init(&sc->sc_mutex, "GICv2m lock", NULL, MTX_DEF);
1074
1075         intr_msi_register(dev, sc->sc_xref);
1076
1077         if (bootverbose)
1078                 device_printf(dev, "using spi %u to %u\n", sc->sc_spi_start,
1079                     sc->sc_spi_start + sc->sc_spi_count - 1);
1080
1081         return (0);
1082 }
1083
1084 static int
1085 arm_gicv2m_alloc_msi(device_t dev, device_t child, int count, int maxcount,
1086     device_t *pic, struct intr_irqsrc **srcs)
1087 {
1088         struct arm_gic_softc *psc;
1089         struct arm_gicv2m_softc *sc;
1090         int i, irq, end_irq;
1091         bool found;
1092
1093         KASSERT(powerof2(count), ("%s: bad count", __func__));
1094         KASSERT(powerof2(maxcount), ("%s: bad maxcount", __func__));
1095
1096         psc = device_get_softc(device_get_parent(dev));
1097         sc = device_get_softc(dev);
1098
1099         mtx_lock(&sc->sc_mutex);
1100
1101         found = false;
1102         for (irq = sc->sc_spi_start; irq < sc->sc_spi_end; irq++) {
1103                 /* Start on an aligned interrupt */
1104                 if ((irq & (maxcount - 1)) != 0)
1105                         continue;
1106
1107                 /* Assume we found a valid range until shown otherwise */
1108                 found = true;
1109
1110                 /* Check this range is valid */
1111                 for (end_irq = irq; end_irq != irq + count; end_irq++) {
1112                         /* No free interrupts */
1113                         if (end_irq == sc->sc_spi_end) {
1114                                 found = false;
1115                                 break;
1116                         }
1117
1118                         KASSERT((psc->gic_irqs[end_irq].gi_flags & GI_FLAG_MSI)!= 0,
1119                             ("%s: Non-MSI interrupt found", __func__));
1120
1121                         /* This is already used */
1122                         if ((psc->gic_irqs[end_irq].gi_flags & GI_FLAG_MSI_USED) ==
1123                             GI_FLAG_MSI_USED) {
1124                                 found = false;
1125                                 break;
1126                         }
1127                 }
1128                 if (found)
1129                         break;
1130         }
1131
1132         /* Not enough interrupts were found */
1133         if (!found || irq == sc->sc_spi_end) {
1134                 mtx_unlock(&sc->sc_mutex);
1135                 return (ENXIO);
1136         }
1137
1138         for (i = 0; i < count; i++) {
1139                 /* Mark the interrupt as used */
1140                 psc->gic_irqs[irq + i].gi_flags |= GI_FLAG_MSI_USED;
1141
1142         }
1143         mtx_unlock(&sc->sc_mutex);
1144
1145         for (i = 0; i < count; i++)
1146                 srcs[i] = (struct intr_irqsrc *)&psc->gic_irqs[irq + i];
1147         *pic = device_get_parent(dev);
1148
1149         return (0);
1150 }
1151
1152 static int
1153 arm_gicv2m_release_msi(device_t dev, device_t child, int count,
1154     struct intr_irqsrc **isrc)
1155 {
1156         struct arm_gicv2m_softc *sc;
1157         struct gic_irqsrc *gi;
1158         int i;
1159
1160         sc = device_get_softc(dev);
1161
1162         mtx_lock(&sc->sc_mutex);
1163         for (i = 0; i < count; i++) {
1164                 gi = (struct gic_irqsrc *)isrc[i];
1165
1166                 KASSERT((gi->gi_flags & GI_FLAG_MSI_USED) == GI_FLAG_MSI_USED,
1167                     ("%s: Trying to release an unused MSI-X interrupt",
1168                     __func__));
1169
1170                 gi->gi_flags &= ~GI_FLAG_MSI_USED;
1171         }
1172         mtx_unlock(&sc->sc_mutex);
1173
1174         return (0);
1175 }
1176
1177 static int
1178 arm_gicv2m_alloc_msix(device_t dev, device_t child, device_t *pic,
1179     struct intr_irqsrc **isrcp)
1180 {
1181         struct arm_gicv2m_softc *sc;
1182         struct arm_gic_softc *psc;
1183         int irq;
1184
1185         psc = device_get_softc(device_get_parent(dev));
1186         sc = device_get_softc(dev);
1187
1188         mtx_lock(&sc->sc_mutex);
1189         /* Find an unused interrupt */
1190         for (irq = sc->sc_spi_start; irq < sc->sc_spi_end; irq++) {
1191                 KASSERT((psc->gic_irqs[irq].gi_flags & GI_FLAG_MSI) != 0,
1192                     ("%s: Non-MSI interrupt found", __func__));
1193                 if ((psc->gic_irqs[irq].gi_flags & GI_FLAG_MSI_USED) == 0)
1194                         break;
1195         }
1196         /* No free interrupt was found */
1197         if (irq == sc->sc_spi_end) {
1198                 mtx_unlock(&sc->sc_mutex);
1199                 return (ENXIO);
1200         }
1201
1202         /* Mark the interrupt as used */
1203         psc->gic_irqs[irq].gi_flags |= GI_FLAG_MSI_USED;
1204         mtx_unlock(&sc->sc_mutex);
1205
1206         *isrcp = (struct intr_irqsrc *)&psc->gic_irqs[irq];
1207         *pic = device_get_parent(dev);
1208
1209         return (0);
1210 }
1211
1212 static int
1213 arm_gicv2m_release_msix(device_t dev, device_t child, struct intr_irqsrc *isrc)
1214 {
1215         struct arm_gicv2m_softc *sc;
1216         struct gic_irqsrc *gi;
1217
1218         sc = device_get_softc(dev);
1219         gi = (struct gic_irqsrc *)isrc;
1220
1221         KASSERT((gi->gi_flags & GI_FLAG_MSI_USED) == GI_FLAG_MSI_USED,
1222             ("%s: Trying to release an unused MSI-X interrupt", __func__));
1223
1224         mtx_lock(&sc->sc_mutex);
1225         gi->gi_flags &= ~GI_FLAG_MSI_USED;
1226         mtx_unlock(&sc->sc_mutex);
1227
1228         return (0);
1229 }
1230
1231 static int
1232 arm_gicv2m_map_msi(device_t dev, device_t child, struct intr_irqsrc *isrc,
1233     uint64_t *addr, uint32_t *data)
1234 {
1235         struct arm_gicv2m_softc *sc = device_get_softc(dev);
1236         struct gic_irqsrc *gi = (struct gic_irqsrc *)isrc;
1237
1238         *addr = vtophys(rman_get_virtual(sc->sc_mem)) + GICv2M_MSI_SETSPI_NS;
1239         *data = gi->gi_irq;
1240
1241         return (0);
1242 }
1243
1244 static device_method_t arm_gicv2m_methods[] = {
1245         /* Device interface */
1246         DEVMETHOD(device_attach,        arm_gicv2m_attach),
1247
1248         /* MSI/MSI-X */
1249         DEVMETHOD(msi_alloc_msi,        arm_gicv2m_alloc_msi),
1250         DEVMETHOD(msi_release_msi,      arm_gicv2m_release_msi),
1251         DEVMETHOD(msi_alloc_msix,       arm_gicv2m_alloc_msix),
1252         DEVMETHOD(msi_release_msix,     arm_gicv2m_release_msix),
1253         DEVMETHOD(msi_map_msi,          arm_gicv2m_map_msi),
1254
1255         /* End */
1256         DEVMETHOD_END
1257 };
1258
1259 DEFINE_CLASS_0(gicv2m, arm_gicv2m_driver, arm_gicv2m_methods,
1260     sizeof(struct arm_gicv2m_softc));