]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm/arm/gic.c
MFV d60fa10fd872db7e3d8cb1e161cfdae026c43b14:
[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 #ifndef GIC_DEFAULT_ICFGR_INIT
102 #define GIC_DEFAULT_ICFGR_INIT  0x00000000
103 #endif
104
105 struct gic_irqsrc {
106         struct intr_irqsrc      gi_isrc;
107         uint32_t                gi_irq;
108         enum intr_polarity      gi_pol;
109         enum intr_trigger       gi_trig;
110 #define GI_FLAG_EARLY_EOI       (1 << 0)
111 #define GI_FLAG_MSI             (1 << 1) /* This interrupt source should only */
112                                          /* be used for MSI/MSI-X interrupts */
113 #define GI_FLAG_MSI_USED        (1 << 2) /* This irq is already allocated */
114                                          /* for a MSI/MSI-X interrupt */
115         u_int                   gi_flags;
116 };
117
118 static u_int gic_irq_cpu;
119 static int arm_gic_bind_intr(device_t dev, struct intr_irqsrc *isrc);
120
121 #ifdef SMP
122 static u_int sgi_to_ipi[GIC_LAST_SGI - GIC_FIRST_SGI + 1];
123 static u_int sgi_first_unused = GIC_FIRST_SGI;
124 #endif
125
126 #define GIC_INTR_ISRC(sc, irq)  (&sc->gic_irqs[irq].gi_isrc)
127
128 static struct resource_spec arm_gic_spec[] = {
129         { SYS_RES_MEMORY,       0,      RF_ACTIVE },    /* Distributor registers */
130         { SYS_RES_MEMORY,       1,      RF_ACTIVE },    /* CPU Interrupt Intf. registers */
131         { SYS_RES_IRQ,    0, RF_ACTIVE | RF_OPTIONAL }, /* Parent interrupt */
132         { -1, 0 }
133 };
134
135 #if defined(__arm__) && defined(INVARIANTS)
136 static int gic_debug_spurious = 1;
137 #else
138 static int gic_debug_spurious = 0;
139 #endif
140 TUNABLE_INT("hw.gic.debug_spurious", &gic_debug_spurious);
141
142 static u_int arm_gic_map[MAXCPU];
143
144 static struct arm_gic_softc *gic_sc = NULL;
145
146 /* CPU Interface */
147 #define gic_c_read_4(_sc, _reg)         \
148     bus_read_4((_sc)->gic_res[GIC_RES_CPU], (_reg))
149 #define gic_c_write_4(_sc, _reg, _val)          \
150     bus_write_4((_sc)->gic_res[GIC_RES_CPU], (_reg), (_val))
151 /* Distributor Interface */
152 #define gic_d_read_4(_sc, _reg)         \
153     bus_read_4((_sc)->gic_res[GIC_RES_DIST], (_reg))
154 #define gic_d_write_1(_sc, _reg, _val)          \
155     bus_write_1((_sc)->gic_res[GIC_RES_DIST], (_reg), (_val))
156 #define gic_d_write_4(_sc, _reg, _val)          \
157     bus_write_4((_sc)->gic_res[GIC_RES_DIST], (_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         /* Disable interrupt forwarding to the CPU interface */
327         gic_d_write_4(sc, GICD_CTLR, 0x00);
328
329         /* Get the number of interrupts */
330         sc->typer = gic_d_read_4(sc, GICD_TYPER);
331         nirqs = GICD_TYPER_I_NUM(sc->typer);
332
333         if (arm_gic_register_isrcs(sc, nirqs)) {
334                 device_printf(dev, "could not register irqs\n");
335                 goto cleanup;
336         }
337
338         icciidr = gic_c_read_4(sc, GICC_IIDR);
339         device_printf(dev,
340             "pn 0x%x, arch 0x%x, rev 0x%x, implementer 0x%x irqs %u\n",
341             GICD_IIDR_PROD(icciidr), GICD_IIDR_VAR(icciidr),
342             GICD_IIDR_REV(icciidr), GICD_IIDR_IMPL(icciidr), sc->nirqs);
343         sc->gic_iidr = icciidr;
344
345         /* Set all global interrupts to be level triggered, active low. */
346         for (i = 32; i < sc->nirqs; i += 16) {
347                 gic_d_write_4(sc, GICD_ICFGR(i), GIC_DEFAULT_ICFGR_INIT);
348         }
349
350         /* Disable all interrupts. */
351         for (i = 32; i < sc->nirqs; i += 32) {
352                 gic_d_write_4(sc, GICD_ICENABLER(i), 0xFFFFFFFF);
353         }
354
355         /* Find the current cpu mask */
356         mask = gic_cpu_mask(sc);
357         /* Set the mask so we can find this CPU to send it IPIs */
358         arm_gic_map[PCPU_GET(cpuid)] = mask;
359         /* Set all four targets to this cpu */
360         mask |= mask << 8;
361         mask |= mask << 16;
362
363         for (i = 0; i < sc->nirqs; i += 4) {
364                 gic_d_write_4(sc, GICD_IPRIORITYR(i), 0);
365                 if (i > 32) {
366                         gic_d_write_4(sc, GICD_ITARGETSR(i), mask);
367                 }
368         }
369
370         /* Set all the interrupts to be in Group 0 (secure) */
371         for (i = 0; GIC_SUPPORT_SECEXT(sc) && i < sc->nirqs; i += 32) {
372                 gic_d_write_4(sc, GICD_IGROUPR(i), 0);
373         }
374
375         /* Enable CPU interface */
376         gic_c_write_4(sc, GICC_CTLR, 1);
377
378         /* Set priority mask register. */
379         gic_c_write_4(sc, GICC_PMR, 0xff);
380
381         /* Enable interrupt distribution */
382         gic_d_write_4(sc, GICD_CTLR, 0x01);
383         return (0);
384
385 cleanup:
386         arm_gic_detach(dev);
387         return(ENXIO);
388 }
389
390 int
391 arm_gic_detach(device_t dev)
392 {
393         struct arm_gic_softc *sc;
394
395         sc = device_get_softc(dev);
396
397         if (sc->gic_irqs != NULL)
398                 free(sc->gic_irqs, M_DEVBUF);
399
400         bus_release_resources(dev, arm_gic_spec, sc->gic_res);
401
402         return (0);
403 }
404
405 static int
406 arm_gic_print_child(device_t bus, device_t child)
407 {
408         struct resource_list *rl;
409         int rv;
410
411         rv = bus_print_child_header(bus, child);
412
413         rl = BUS_GET_RESOURCE_LIST(bus, child);
414         if (rl != NULL) {
415                 rv += resource_list_print_type(rl, "mem", SYS_RES_MEMORY,
416                     "%#jx");
417                 rv += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%jd");
418         }
419
420         rv += bus_print_child_footer(bus, child);
421
422         return (rv);
423 }
424
425 static struct resource *
426 arm_gic_alloc_resource(device_t bus, device_t child, int type, int *rid,
427     rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
428 {
429         struct arm_gic_softc *sc;
430         struct resource_list_entry *rle;
431         struct resource_list *rl;
432         int j;
433
434         KASSERT(type == SYS_RES_MEMORY, ("Invalid resoure type %x", type));
435
436         sc = device_get_softc(bus);
437
438         /*
439          * Request for the default allocation with a given rid: use resource
440          * list stored in the local device info.
441          */
442         if (RMAN_IS_DEFAULT_RANGE(start, end)) {
443                 rl = BUS_GET_RESOURCE_LIST(bus, child);
444
445                 if (type == SYS_RES_IOPORT)
446                         type = SYS_RES_MEMORY;
447
448                 rle = resource_list_find(rl, type, *rid);
449                 if (rle == NULL) {
450                         if (bootverbose)
451                                 device_printf(bus, "no default resources for "
452                                     "rid = %d, type = %d\n", *rid, type);
453                         return (NULL);
454                 }
455                 start = rle->start;
456                 end = rle->end;
457                 count = rle->count;
458         }
459
460         /* Remap through ranges property */
461         for (j = 0; j < sc->nranges; j++) {
462                 if (start >= sc->ranges[j].bus && end <
463                     sc->ranges[j].bus + sc->ranges[j].size) {
464                         start -= sc->ranges[j].bus;
465                         start += sc->ranges[j].host;
466                         end -= sc->ranges[j].bus;
467                         end += sc->ranges[j].host;
468                         break;
469                 }
470         }
471         if (j == sc->nranges && sc->nranges != 0) {
472                 if (bootverbose)
473                         device_printf(bus, "Could not map resource "
474                             "%#jx-%#jx\n", (uintmax_t)start, (uintmax_t)end);
475
476                 return (NULL);
477         }
478
479         return (bus_generic_alloc_resource(bus, child, type, rid, start, end,
480             count, flags));
481 }
482
483 static int
484 arm_gic_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
485 {
486         struct arm_gic_softc *sc;
487
488         sc = device_get_softc(dev);
489
490         switch(which) {
491         case GIC_IVAR_HW_REV:
492                 KASSERT(GICD_IIDR_VAR(sc->gic_iidr) < 3,
493                     ("arm_gic_read_ivar: Unknown IIDR revision %u (%.08x)",
494                      GICD_IIDR_VAR(sc->gic_iidr), sc->gic_iidr));
495                 *result = GICD_IIDR_VAR(sc->gic_iidr);
496                 return (0);
497         case GIC_IVAR_BUS:
498                 KASSERT(sc->gic_bus != GIC_BUS_UNKNOWN,
499                     ("arm_gic_read_ivar: Unknown bus type"));
500                 KASSERT(sc->gic_bus <= GIC_BUS_MAX,
501                     ("arm_gic_read_ivar: Invalid bus type %u", sc->gic_bus));
502                 *result = sc->gic_bus;
503                 return (0);
504         }
505
506         return (ENOENT);
507 }
508
509 int
510 arm_gic_intr(void *arg)
511 {
512         struct arm_gic_softc *sc = arg;
513         struct gic_irqsrc *gi;
514         uint32_t irq_active_reg, irq;
515         struct trapframe *tf;
516
517         irq_active_reg = gic_c_read_4(sc, GICC_IAR);
518         irq = irq_active_reg & 0x3FF;
519
520         /*
521          * 1. We do EOI here because recent read value from active interrupt
522          *    register must be used for it. Another approach is to save this
523          *    value into associated interrupt source.
524          * 2. EOI must be done on same CPU where interrupt has fired. Thus
525          *    we must ensure that interrupted thread does not migrate to
526          *    another CPU.
527          * 3. EOI cannot be delayed by any preemption which could happen on
528          *    critical_exit() used in MI intr code, when interrupt thread is
529          *    scheduled. See next point.
530          * 4. IPI_RENDEZVOUS assumes that no preemption is permitted during
531          *    an action and any use of critical_exit() could break this
532          *    assumption. See comments within smp_rendezvous_action().
533          * 5. We always return FILTER_HANDLED as this is an interrupt
534          *    controller dispatch function. Otherwise, in cascaded interrupt
535          *    case, the whole interrupt subtree would be masked.
536          */
537
538         if (irq >= sc->nirqs) {
539                 if (gic_debug_spurious)
540                         device_printf(sc->gic_dev,
541                             "Spurious interrupt detected: last irq: %d on CPU%d\n",
542                             sc->last_irq[PCPU_GET(cpuid)], PCPU_GET(cpuid));
543                 return (FILTER_HANDLED);
544         }
545
546         tf = curthread->td_intr_frame;
547 dispatch_irq:
548         gi = sc->gic_irqs + irq;
549         /*
550          * Note that GIC_FIRST_SGI is zero and is not used in 'if' statement
551          * as compiler complains that comparing u_int >= 0 is always true.
552          */
553         if (irq <= GIC_LAST_SGI) {
554 #ifdef SMP
555                 /* Call EOI for all IPI before dispatch. */
556                 gic_c_write_4(sc, GICC_EOIR, irq_active_reg);
557                 intr_ipi_dispatch(sgi_to_ipi[gi->gi_irq], tf);
558                 goto next_irq;
559 #else
560                 device_printf(sc->gic_dev, "SGI %u on UP system detected\n",
561                     irq - GIC_FIRST_SGI);
562                 gic_c_write_4(sc, GICC_EOIR, irq_active_reg);
563                 goto next_irq;
564 #endif
565         }
566
567         if (gic_debug_spurious)
568                 sc->last_irq[PCPU_GET(cpuid)] = irq;
569         if ((gi->gi_flags & GI_FLAG_EARLY_EOI) == GI_FLAG_EARLY_EOI)
570                 gic_c_write_4(sc, GICC_EOIR, irq_active_reg);
571
572         if (intr_isrc_dispatch(&gi->gi_isrc, tf) != 0) {
573                 gic_irq_mask(sc, irq);
574                 if ((gi->gi_flags & GI_FLAG_EARLY_EOI) != GI_FLAG_EARLY_EOI)
575                         gic_c_write_4(sc, GICC_EOIR, irq_active_reg);
576                 device_printf(sc->gic_dev, "Stray irq %u disabled\n", irq);
577         }
578
579 next_irq:
580         arm_irq_memory_barrier(irq);
581         irq_active_reg = gic_c_read_4(sc, GICC_IAR);
582         irq = irq_active_reg & 0x3FF;
583         if (irq < sc->nirqs)
584                 goto dispatch_irq;
585
586         return (FILTER_HANDLED);
587 }
588
589 static void
590 gic_config(struct arm_gic_softc *sc, u_int irq, enum intr_trigger trig,
591     enum intr_polarity pol)
592 {
593         uint32_t reg;
594         uint32_t mask;
595
596         if (irq < GIC_FIRST_SPI)
597                 return;
598
599         mtx_lock_spin(&sc->mutex);
600
601         reg = gic_d_read_4(sc, GICD_ICFGR(irq));
602         mask = (reg >> 2*(irq % 16)) & 0x3;
603
604         if (pol == INTR_POLARITY_LOW) {
605                 mask &= ~GICD_ICFGR_POL_MASK;
606                 mask |= GICD_ICFGR_POL_LOW;
607         } else if (pol == INTR_POLARITY_HIGH) {
608                 mask &= ~GICD_ICFGR_POL_MASK;
609                 mask |= GICD_ICFGR_POL_HIGH;
610         }
611
612         if (trig == INTR_TRIGGER_LEVEL) {
613                 mask &= ~GICD_ICFGR_TRIG_MASK;
614                 mask |= GICD_ICFGR_TRIG_LVL;
615         } else if (trig == INTR_TRIGGER_EDGE) {
616                 mask &= ~GICD_ICFGR_TRIG_MASK;
617                 mask |= GICD_ICFGR_TRIG_EDGE;
618         }
619
620         /* Set mask */
621         reg = reg & ~(0x3 << 2*(irq % 16));
622         reg = reg | (mask << 2*(irq % 16));
623         gic_d_write_4(sc, GICD_ICFGR(irq), reg);
624
625         mtx_unlock_spin(&sc->mutex);
626 }
627
628 static int
629 gic_bind(struct arm_gic_softc *sc, u_int irq, cpuset_t *cpus)
630 {
631         uint32_t cpu, end, mask;
632
633         end = min(mp_ncpus, 8);
634         for (cpu = end; cpu < MAXCPU; cpu++)
635                 if (CPU_ISSET(cpu, cpus))
636                         return (EINVAL);
637
638         for (mask = 0, cpu = 0; cpu < end; cpu++)
639                 if (CPU_ISSET(cpu, cpus))
640                         mask |= arm_gic_map[cpu];
641
642         gic_d_write_1(sc, GICD_ITARGETSR(0) + irq, mask);
643         return (0);
644 }
645
646 #ifdef FDT
647 static int
648 gic_map_fdt(device_t dev, u_int ncells, pcell_t *cells, u_int *irqp,
649     enum intr_polarity *polp, enum intr_trigger *trigp)
650 {
651
652         if (ncells == 1) {
653                 *irqp = cells[0];
654                 *polp = INTR_POLARITY_CONFORM;
655                 *trigp = INTR_TRIGGER_CONFORM;
656                 return (0);
657         }
658         if (ncells == 3) {
659                 u_int irq, tripol;
660
661                 /*
662                  * The 1st cell is the interrupt type:
663                  *      0 = SPI
664                  *      1 = PPI
665                  * The 2nd cell contains the interrupt number:
666                  *      [0 - 987] for SPI
667                  *      [0 -  15] for PPI
668                  * The 3rd cell is the flags, encoded as follows:
669                  *   bits[3:0] trigger type and level flags
670                  *      1 = low-to-high edge triggered
671                  *      2 = high-to-low edge triggered
672                  *      4 = active high level-sensitive
673                  *      8 = active low level-sensitive
674                  *   bits[15:8] PPI interrupt cpu mask
675                  *      Each bit corresponds to each of the 8 possible cpus
676                  *      attached to the GIC.  A bit set to '1' indicated
677                  *      the interrupt is wired to that CPU.
678                  */
679                 switch (cells[0]) {
680                 case 0:
681                         irq = GIC_FIRST_SPI + cells[1];
682                         /* SPI irq is checked later. */
683                         break;
684                 case 1:
685                         irq = GIC_FIRST_PPI + cells[1];
686                         if (irq > GIC_LAST_PPI) {
687                                 device_printf(dev, "unsupported PPI interrupt "
688                                     "number %u\n", cells[1]);
689                                 return (EINVAL);
690                         }
691                         break;
692                 default:
693                         device_printf(dev, "unsupported interrupt type "
694                             "configuration %u\n", cells[0]);
695                         return (EINVAL);
696                 }
697
698                 tripol = cells[2] & 0xff;
699                 if (tripol & 0xf0 || (tripol & FDT_INTR_LOW_MASK &&
700                     cells[0] == 0))
701                         device_printf(dev, "unsupported trigger/polarity "
702                             "configuration 0x%02x\n", tripol);
703
704                 *irqp = irq;
705                 *polp = INTR_POLARITY_CONFORM;
706                 *trigp = tripol & FDT_INTR_EDGE_MASK ?
707                     INTR_TRIGGER_EDGE : INTR_TRIGGER_LEVEL;
708                 return (0);
709         }
710         return (EINVAL);
711 }
712 #endif
713
714 static int
715 gic_map_msi(device_t dev, struct intr_map_data_msi *msi_data, u_int *irqp,
716     enum intr_polarity *polp, enum intr_trigger *trigp)
717 {
718         struct gic_irqsrc *gi;
719
720         /* Map a non-GICv2m MSI */
721         gi = (struct gic_irqsrc *)msi_data->isrc;
722         if (gi == NULL)
723                 return (ENXIO);
724
725         *irqp = gi->gi_irq;
726
727         /* MSI/MSI-X interrupts are always edge triggered with high polarity */
728         *polp = INTR_POLARITY_HIGH;
729         *trigp = INTR_TRIGGER_EDGE;
730
731         return (0);
732 }
733
734 static int
735 gic_map_intr(device_t dev, struct intr_map_data *data, u_int *irqp,
736     enum intr_polarity *polp, enum intr_trigger *trigp)
737 {
738         u_int irq;
739         enum intr_polarity pol;
740         enum intr_trigger trig;
741         struct arm_gic_softc *sc;
742         struct intr_map_data_msi *dam;
743 #ifdef FDT
744         struct intr_map_data_fdt *daf;
745 #endif
746 #ifdef DEV_ACPI
747         struct intr_map_data_acpi *daa;
748 #endif
749
750         sc = device_get_softc(dev);
751         switch (data->type) {
752 #ifdef FDT
753         case INTR_MAP_DATA_FDT:
754                 daf = (struct intr_map_data_fdt *)data;
755                 if (gic_map_fdt(dev, daf->ncells, daf->cells, &irq, &pol,
756                     &trig) != 0)
757                         return (EINVAL);
758                 KASSERT(irq >= sc->nirqs ||
759                     (sc->gic_irqs[irq].gi_flags & GI_FLAG_MSI) == 0,
760                     ("%s: Attempting to map a MSI interrupt from FDT",
761                     __func__));
762                 break;
763 #endif
764 #ifdef DEV_ACPI
765         case INTR_MAP_DATA_ACPI:
766                 daa = (struct intr_map_data_acpi *)data;
767                 irq = daa->irq;
768                 pol = daa->pol;
769                 trig = daa->trig;
770                 break;
771 #endif
772         case INTR_MAP_DATA_MSI:
773                 /* Non-GICv2m MSI */
774                 dam = (struct intr_map_data_msi *)data;
775                 if (gic_map_msi(dev, dam, &irq, &pol, &trig) != 0)
776                         return (EINVAL);
777                 break;
778         default:
779                 return (ENOTSUP);
780         }
781
782         if (irq >= sc->nirqs)
783                 return (EINVAL);
784         if (pol != INTR_POLARITY_CONFORM && pol != INTR_POLARITY_LOW &&
785             pol != INTR_POLARITY_HIGH)
786                 return (EINVAL);
787         if (trig != INTR_TRIGGER_CONFORM && trig != INTR_TRIGGER_EDGE &&
788             trig != INTR_TRIGGER_LEVEL)
789                 return (EINVAL);
790
791         *irqp = irq;
792         if (polp != NULL)
793                 *polp = pol;
794         if (trigp != NULL)
795                 *trigp = trig;
796         return (0);
797 }
798
799 static int
800 arm_gic_map_intr(device_t dev, struct intr_map_data *data,
801     struct intr_irqsrc **isrcp)
802 {
803         int error;
804         u_int irq;
805         struct arm_gic_softc *sc;
806
807         error = gic_map_intr(dev, data, &irq, NULL, NULL);
808         if (error == 0) {
809                 sc = device_get_softc(dev);
810                 *isrcp = GIC_INTR_ISRC(sc, irq);
811         }
812         return (error);
813 }
814
815 static int
816 arm_gic_setup_intr(device_t dev, struct intr_irqsrc *isrc,
817     struct resource *res, struct intr_map_data *data)
818 {
819         struct arm_gic_softc *sc = device_get_softc(dev);
820         struct gic_irqsrc *gi = (struct gic_irqsrc *)isrc;
821         enum intr_trigger trig;
822         enum intr_polarity pol;
823
824         if ((gi->gi_flags & GI_FLAG_MSI) == GI_FLAG_MSI) {
825                 /* GICv2m MSI */
826                 pol = gi->gi_pol;
827                 trig = gi->gi_trig;
828                 KASSERT(pol == INTR_POLARITY_HIGH,
829                     ("%s: MSI interrupts must be active-high", __func__));
830                 KASSERT(trig == INTR_TRIGGER_EDGE,
831                     ("%s: MSI interrupts must be edge triggered", __func__));
832         } else if (data != NULL) {
833                 u_int irq;
834
835                 /* Get config for resource. */
836                 if (gic_map_intr(dev, data, &irq, &pol, &trig) ||
837                     gi->gi_irq != irq)
838                         return (EINVAL);
839         } else {
840                 pol = INTR_POLARITY_CONFORM;
841                 trig = INTR_TRIGGER_CONFORM;
842         }
843
844         /* Compare config if this is not first setup. */
845         if (isrc->isrc_handlers != 0) {
846                 if ((pol != INTR_POLARITY_CONFORM && pol != gi->gi_pol) ||
847                     (trig != INTR_TRIGGER_CONFORM && trig != gi->gi_trig))
848                         return (EINVAL);
849                 else
850                         return (0);
851         }
852
853         /* For MSI/MSI-X we should have already configured these */
854         if ((gi->gi_flags & GI_FLAG_MSI) == 0) {
855                 if (pol == INTR_POLARITY_CONFORM)
856                         pol = INTR_POLARITY_LOW;        /* just pick some */
857                 if (trig == INTR_TRIGGER_CONFORM)
858                         trig = INTR_TRIGGER_EDGE;       /* just pick some */
859
860                 gi->gi_pol = pol;
861                 gi->gi_trig = trig;
862
863                 /* Edge triggered interrupts need an early EOI sent */
864                 if (gi->gi_trig == INTR_TRIGGER_EDGE)
865                         gi->gi_flags |= GI_FLAG_EARLY_EOI;
866         }
867
868         /*
869          * XXX - In case that per CPU interrupt is going to be enabled in time
870          *       when SMP is already started, we need some IPI call which
871          *       enables it on others CPUs. Further, it's more complicated as
872          *       pic_enable_source() and pic_disable_source() should act on
873          *       per CPU basis only. Thus, it should be solved here somehow.
874          */
875         if (isrc->isrc_flags & INTR_ISRCF_PPI)
876                 CPU_SET(PCPU_GET(cpuid), &isrc->isrc_cpu);
877
878         gic_config(sc, gi->gi_irq, gi->gi_trig, gi->gi_pol);
879         arm_gic_bind_intr(dev, isrc);
880         return (0);
881 }
882
883 static int
884 arm_gic_teardown_intr(device_t dev, struct intr_irqsrc *isrc,
885     struct resource *res, struct intr_map_data *data)
886 {
887         struct gic_irqsrc *gi = (struct gic_irqsrc *)isrc;
888
889         if (isrc->isrc_handlers == 0 && (gi->gi_flags & GI_FLAG_MSI) == 0) {
890                 gi->gi_pol = INTR_POLARITY_CONFORM;
891                 gi->gi_trig = INTR_TRIGGER_CONFORM;
892         }
893         return (0);
894 }
895
896 static void
897 arm_gic_enable_intr(device_t dev, struct intr_irqsrc *isrc)
898 {
899         struct arm_gic_softc *sc = device_get_softc(dev);
900         struct gic_irqsrc *gi = (struct gic_irqsrc *)isrc;
901
902         arm_irq_memory_barrier(gi->gi_irq);
903         gic_irq_unmask(sc, gi->gi_irq);
904 }
905
906 static void
907 arm_gic_disable_intr(device_t dev, struct intr_irqsrc *isrc)
908 {
909         struct arm_gic_softc *sc = device_get_softc(dev);
910         struct gic_irqsrc *gi = (struct gic_irqsrc *)isrc;
911
912         gic_irq_mask(sc, gi->gi_irq);
913 }
914
915 static void
916 arm_gic_pre_ithread(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         arm_gic_disable_intr(dev, isrc);
922         gic_c_write_4(sc, GICC_EOIR, gi->gi_irq);
923 }
924
925 static void
926 arm_gic_post_ithread(device_t dev, struct intr_irqsrc *isrc)
927 {
928
929         arm_irq_memory_barrier(0);
930         arm_gic_enable_intr(dev, isrc);
931 }
932
933 static void
934 arm_gic_post_filter(device_t dev, struct intr_irqsrc *isrc)
935 {
936         struct arm_gic_softc *sc = device_get_softc(dev);
937         struct gic_irqsrc *gi = (struct gic_irqsrc *)isrc;
938
939         /* EOI for edge-triggered done earlier. */
940         if ((gi->gi_flags & GI_FLAG_EARLY_EOI) == GI_FLAG_EARLY_EOI)
941                 return;
942
943         arm_irq_memory_barrier(0);
944         gic_c_write_4(sc, GICC_EOIR, gi->gi_irq);
945 }
946
947 static int
948 arm_gic_bind_intr(device_t dev, struct intr_irqsrc *isrc)
949 {
950         struct arm_gic_softc *sc = device_get_softc(dev);
951         struct gic_irqsrc *gi = (struct gic_irqsrc *)isrc;
952
953         if (gi->gi_irq < GIC_FIRST_SPI)
954                 return (EINVAL);
955
956         if (CPU_EMPTY(&isrc->isrc_cpu)) {
957                 gic_irq_cpu = intr_irq_next_cpu(gic_irq_cpu, &all_cpus);
958                 CPU_SETOF(gic_irq_cpu, &isrc->isrc_cpu);
959         }
960         return (gic_bind(sc, gi->gi_irq, &isrc->isrc_cpu));
961 }
962
963 #ifdef SMP
964 static void
965 arm_gic_ipi_send(device_t dev, struct intr_irqsrc *isrc, cpuset_t cpus,
966     u_int ipi)
967 {
968         struct arm_gic_softc *sc = device_get_softc(dev);
969         struct gic_irqsrc *gi = (struct gic_irqsrc *)isrc;
970         uint32_t val = 0, i;
971
972         for (i = 0; i < MAXCPU; i++)
973                 if (CPU_ISSET(i, &cpus))
974                         val |= arm_gic_map[i] << GICD_SGI_TARGET_SHIFT;
975
976         gic_d_write_4(sc, GICD_SGIR, val | gi->gi_irq);
977 }
978
979 static int
980 arm_gic_ipi_setup(device_t dev, u_int ipi, struct intr_irqsrc **isrcp)
981 {
982         struct intr_irqsrc *isrc;
983         struct arm_gic_softc *sc = device_get_softc(dev);
984
985         if (sgi_first_unused > GIC_LAST_SGI)
986                 return (ENOSPC);
987
988         isrc = GIC_INTR_ISRC(sc, sgi_first_unused);
989         sgi_to_ipi[sgi_first_unused++] = ipi;
990
991         CPU_SET(PCPU_GET(cpuid), &isrc->isrc_cpu);
992
993         *isrcp = isrc;
994         return (0);
995 }
996 #endif
997
998 static device_method_t arm_gic_methods[] = {
999         /* Bus interface */
1000         DEVMETHOD(bus_print_child,      arm_gic_print_child),
1001         DEVMETHOD(bus_add_child,        bus_generic_add_child),
1002         DEVMETHOD(bus_alloc_resource,   arm_gic_alloc_resource),
1003         DEVMETHOD(bus_release_resource, bus_generic_release_resource),
1004         DEVMETHOD(bus_activate_resource,bus_generic_activate_resource),
1005         DEVMETHOD(bus_read_ivar,        arm_gic_read_ivar),
1006
1007         /* Interrupt controller interface */
1008         DEVMETHOD(pic_disable_intr,     arm_gic_disable_intr),
1009         DEVMETHOD(pic_enable_intr,      arm_gic_enable_intr),
1010         DEVMETHOD(pic_map_intr,         arm_gic_map_intr),
1011         DEVMETHOD(pic_setup_intr,       arm_gic_setup_intr),
1012         DEVMETHOD(pic_teardown_intr,    arm_gic_teardown_intr),
1013         DEVMETHOD(pic_post_filter,      arm_gic_post_filter),
1014         DEVMETHOD(pic_post_ithread,     arm_gic_post_ithread),
1015         DEVMETHOD(pic_pre_ithread,      arm_gic_pre_ithread),
1016 #ifdef SMP
1017         DEVMETHOD(pic_bind_intr,        arm_gic_bind_intr),
1018         DEVMETHOD(pic_init_secondary,   arm_gic_init_secondary),
1019         DEVMETHOD(pic_ipi_send,         arm_gic_ipi_send),
1020         DEVMETHOD(pic_ipi_setup,        arm_gic_ipi_setup),
1021 #endif
1022         { 0, 0 }
1023 };
1024
1025 DEFINE_CLASS_0(gic, arm_gic_driver, arm_gic_methods,
1026     sizeof(struct arm_gic_softc));
1027
1028 /*
1029  * GICv2m support -- the GICv2 MSI/MSI-X controller.
1030  */
1031
1032 #define GICV2M_MSI_TYPER        0x008
1033 #define  MSI_TYPER_SPI_BASE(x)  (((x) >> 16) & 0x3ff)
1034 #define  MSI_TYPER_SPI_COUNT(x) (((x) >> 0) & 0x3ff)
1035 #define GICv2M_MSI_SETSPI_NS    0x040
1036 #define GICV2M_MSI_IIDR         0xFCC
1037
1038 int
1039 arm_gicv2m_attach(device_t dev)
1040 {
1041         struct arm_gicv2m_softc *sc;
1042         uint32_t typer;
1043         int rid;
1044
1045         sc = device_get_softc(dev);
1046
1047         rid = 0;
1048         sc->sc_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
1049             RF_ACTIVE);
1050         if (sc->sc_mem == NULL) {
1051                 device_printf(dev, "Unable to allocate resources\n");
1052                 return (ENXIO);
1053         }
1054
1055         typer = bus_read_4(sc->sc_mem, GICV2M_MSI_TYPER);
1056         sc->sc_spi_start = MSI_TYPER_SPI_BASE(typer);
1057         sc->sc_spi_count = MSI_TYPER_SPI_COUNT(typer);
1058         sc->sc_spi_end = sc->sc_spi_start + sc->sc_spi_count;
1059
1060         /* Reserve these interrupts for MSI/MSI-X use */
1061         arm_gic_reserve_msi_range(device_get_parent(dev), sc->sc_spi_start,
1062             sc->sc_spi_count);
1063
1064         mtx_init(&sc->sc_mutex, "GICv2m lock", NULL, MTX_DEF);
1065
1066         intr_msi_register(dev, sc->sc_xref);
1067
1068         if (bootverbose)
1069                 device_printf(dev, "using spi %u to %u\n", sc->sc_spi_start,
1070                     sc->sc_spi_start + sc->sc_spi_count - 1);
1071
1072         return (0);
1073 }
1074
1075 static int
1076 arm_gicv2m_alloc_msi(device_t dev, device_t child, int count, int maxcount,
1077     device_t *pic, struct intr_irqsrc **srcs)
1078 {
1079         struct arm_gic_softc *psc;
1080         struct arm_gicv2m_softc *sc;
1081         int i, irq, end_irq;
1082         bool found;
1083
1084         KASSERT(powerof2(count), ("%s: bad count", __func__));
1085         KASSERT(powerof2(maxcount), ("%s: bad maxcount", __func__));
1086
1087         psc = device_get_softc(device_get_parent(dev));
1088         sc = device_get_softc(dev);
1089
1090         mtx_lock(&sc->sc_mutex);
1091
1092         found = false;
1093         for (irq = sc->sc_spi_start; irq < sc->sc_spi_end; irq++) {
1094                 /* Start on an aligned interrupt */
1095                 if ((irq & (maxcount - 1)) != 0)
1096                         continue;
1097
1098                 /* Assume we found a valid range until shown otherwise */
1099                 found = true;
1100
1101                 /* Check this range is valid */
1102                 for (end_irq = irq; end_irq != irq + count; end_irq++) {
1103                         /* No free interrupts */
1104                         if (end_irq == sc->sc_spi_end) {
1105                                 found = false;
1106                                 break;
1107                         }
1108
1109                         KASSERT((psc->gic_irqs[end_irq].gi_flags & GI_FLAG_MSI)!= 0,
1110                             ("%s: Non-MSI interrupt found", __func__));
1111
1112                         /* This is already used */
1113                         if ((psc->gic_irqs[end_irq].gi_flags & GI_FLAG_MSI_USED) ==
1114                             GI_FLAG_MSI_USED) {
1115                                 found = false;
1116                                 break;
1117                         }
1118                 }
1119                 if (found)
1120                         break;
1121         }
1122
1123         /* Not enough interrupts were found */
1124         if (!found || irq == sc->sc_spi_end) {
1125                 mtx_unlock(&sc->sc_mutex);
1126                 return (ENXIO);
1127         }
1128
1129         for (i = 0; i < count; i++) {
1130                 /* Mark the interrupt as used */
1131                 psc->gic_irqs[irq + i].gi_flags |= GI_FLAG_MSI_USED;
1132         }
1133         mtx_unlock(&sc->sc_mutex);
1134
1135         for (i = 0; i < count; i++)
1136                 srcs[i] = (struct intr_irqsrc *)&psc->gic_irqs[irq + i];
1137         *pic = device_get_parent(dev);
1138
1139         return (0);
1140 }
1141
1142 static int
1143 arm_gicv2m_release_msi(device_t dev, device_t child, int count,
1144     struct intr_irqsrc **isrc)
1145 {
1146         struct arm_gicv2m_softc *sc;
1147         struct gic_irqsrc *gi;
1148         int i;
1149
1150         sc = device_get_softc(dev);
1151
1152         mtx_lock(&sc->sc_mutex);
1153         for (i = 0; i < count; i++) {
1154                 gi = (struct gic_irqsrc *)isrc[i];
1155
1156                 KASSERT((gi->gi_flags & GI_FLAG_MSI_USED) == GI_FLAG_MSI_USED,
1157                     ("%s: Trying to release an unused MSI-X interrupt",
1158                     __func__));
1159
1160                 gi->gi_flags &= ~GI_FLAG_MSI_USED;
1161         }
1162         mtx_unlock(&sc->sc_mutex);
1163
1164         return (0);
1165 }
1166
1167 static int
1168 arm_gicv2m_alloc_msix(device_t dev, device_t child, device_t *pic,
1169     struct intr_irqsrc **isrcp)
1170 {
1171         struct arm_gicv2m_softc *sc;
1172         struct arm_gic_softc *psc;
1173         int irq;
1174
1175         psc = device_get_softc(device_get_parent(dev));
1176         sc = device_get_softc(dev);
1177
1178         mtx_lock(&sc->sc_mutex);
1179         /* Find an unused interrupt */
1180         for (irq = sc->sc_spi_start; irq < sc->sc_spi_end; irq++) {
1181                 KASSERT((psc->gic_irqs[irq].gi_flags & GI_FLAG_MSI) != 0,
1182                     ("%s: Non-MSI interrupt found", __func__));
1183                 if ((psc->gic_irqs[irq].gi_flags & GI_FLAG_MSI_USED) == 0)
1184                         break;
1185         }
1186         /* No free interrupt was found */
1187         if (irq == sc->sc_spi_end) {
1188                 mtx_unlock(&sc->sc_mutex);
1189                 return (ENXIO);
1190         }
1191
1192         /* Mark the interrupt as used */
1193         psc->gic_irqs[irq].gi_flags |= GI_FLAG_MSI_USED;
1194         mtx_unlock(&sc->sc_mutex);
1195
1196         *isrcp = (struct intr_irqsrc *)&psc->gic_irqs[irq];
1197         *pic = device_get_parent(dev);
1198
1199         return (0);
1200 }
1201
1202 static int
1203 arm_gicv2m_release_msix(device_t dev, device_t child, struct intr_irqsrc *isrc)
1204 {
1205         struct arm_gicv2m_softc *sc;
1206         struct gic_irqsrc *gi;
1207
1208         sc = device_get_softc(dev);
1209         gi = (struct gic_irqsrc *)isrc;
1210
1211         KASSERT((gi->gi_flags & GI_FLAG_MSI_USED) == GI_FLAG_MSI_USED,
1212             ("%s: Trying to release an unused MSI-X interrupt", __func__));
1213
1214         mtx_lock(&sc->sc_mutex);
1215         gi->gi_flags &= ~GI_FLAG_MSI_USED;
1216         mtx_unlock(&sc->sc_mutex);
1217
1218         return (0);
1219 }
1220
1221 static int
1222 arm_gicv2m_map_msi(device_t dev, device_t child, struct intr_irqsrc *isrc,
1223     uint64_t *addr, uint32_t *data)
1224 {
1225         struct arm_gicv2m_softc *sc = device_get_softc(dev);
1226         struct gic_irqsrc *gi = (struct gic_irqsrc *)isrc;
1227
1228         *addr = vtophys(rman_get_virtual(sc->sc_mem)) + GICv2M_MSI_SETSPI_NS;
1229         *data = gi->gi_irq;
1230
1231         return (0);
1232 }
1233
1234 static device_method_t arm_gicv2m_methods[] = {
1235         /* Device interface */
1236         DEVMETHOD(device_attach,        arm_gicv2m_attach),
1237
1238         /* MSI/MSI-X */
1239         DEVMETHOD(msi_alloc_msi,        arm_gicv2m_alloc_msi),
1240         DEVMETHOD(msi_release_msi,      arm_gicv2m_release_msi),
1241         DEVMETHOD(msi_alloc_msix,       arm_gicv2m_alloc_msix),
1242         DEVMETHOD(msi_release_msix,     arm_gicv2m_release_msix),
1243         DEVMETHOD(msi_map_msi,          arm_gicv2m_map_msi),
1244
1245         /* End */
1246         DEVMETHOD_END
1247 };
1248
1249 DEFINE_CLASS_0(gicv2m, arm_gicv2m_driver, arm_gicv2m_methods,
1250     sizeof(struct arm_gicv2m_softc));