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