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