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