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