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