2 * Copyright (c) 2011 The FreeBSD Foundation
5 * Developed by Damjan Marion <damjan.marion@gmail.com>
7 * Based on OMAP4 GIC code by Ben Gray
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include "opt_platform.h"
39 #include <sys/param.h>
40 #include <sys/systm.h>
42 #include <sys/kernel.h>
44 #include <sys/module.h>
45 #include <sys/malloc.h>
49 #include <sys/cpuset.h>
51 #include <sys/mutex.h>
54 #include <sys/sched.h>
56 #include <machine/bus.h>
57 #include <machine/intr.h>
58 #include <machine/smp.h>
60 #include <dev/fdt/fdt_common.h>
61 #include <dev/ofw/openfirm.h>
62 #include <dev/ofw/ofw_bus.h>
63 #include <dev/ofw/ofw_bus_subr.h>
69 #define GIC_DEBUG_SPURIOUS
71 /* We are using GICv2 register naming */
73 /* Distributor Registers */
74 #define GICD_CTLR 0x000 /* v1 ICDDCR */
75 #define GICD_TYPER 0x004 /* v1 ICDICTR */
76 #define GICD_IIDR 0x008 /* v1 ICDIIDR */
77 #define GICD_IGROUPR(n) (0x0080 + ((n) * 4)) /* v1 ICDISER */
78 #define GICD_ISENABLER(n) (0x0100 + ((n) * 4)) /* v1 ICDISER */
79 #define GICD_ICENABLER(n) (0x0180 + ((n) * 4)) /* v1 ICDICER */
80 #define GICD_ISPENDR(n) (0x0200 + ((n) * 4)) /* v1 ICDISPR */
81 #define GICD_ICPENDR(n) (0x0280 + ((n) * 4)) /* v1 ICDICPR */
82 #define GICD_ICACTIVER(n) (0x0380 + ((n) * 4)) /* v1 ICDABR */
83 #define GICD_IPRIORITYR(n) (0x0400 + ((n) * 4)) /* v1 ICDIPR */
84 #define GICD_ITARGETSR(n) (0x0800 + ((n) * 4)) /* v1 ICDIPTR */
85 #define GICD_ICFGR(n) (0x0C00 + ((n) * 4)) /* v1 ICDICFR */
86 #define GICD_SGIR(n) (0x0F00 + ((n) * 4)) /* v1 ICDSGIR */
87 #define GICD_SGI_TARGET_SHIFT 16
90 #define GICC_CTLR 0x0000 /* v1 ICCICR */
91 #define GICC_PMR 0x0004 /* v1 ICCPMR */
92 #define GICC_BPR 0x0008 /* v1 ICCBPR */
93 #define GICC_IAR 0x000C /* v1 ICCIAR */
94 #define GICC_EOIR 0x0010 /* v1 ICCEOIR */
95 #define GICC_RPR 0x0014 /* v1 ICCRPR */
96 #define GICC_HPPIR 0x0018 /* v1 ICCHPIR */
97 #define GICC_ABPR 0x001C /* v1 ICCABPR */
98 #define GICC_IIDR 0x00FC /* v1 ICCIIDR*/
100 #define GIC_FIRST_SGI 0 /* Irqs 0-15 are SGIs/IPIs. */
101 #define GIC_LAST_SGI 15
102 #define GIC_FIRST_PPI 16 /* Irqs 16-31 are private (per */
103 #define GIC_LAST_PPI 31 /* core) peripheral interrupts. */
104 #define GIC_FIRST_SPI 32 /* Irqs 32+ are shared peripherals. */
106 /* First bit is a polarity bit (0 - low, 1 - high) */
107 #define GICD_ICFGR_POL_LOW (0 << 0)
108 #define GICD_ICFGR_POL_HIGH (1 << 0)
109 #define GICD_ICFGR_POL_MASK 0x1
110 /* Second bit is a trigger bit (0 - level, 1 - edge) */
111 #define GICD_ICFGR_TRIG_LVL (0 << 1)
112 #define GICD_ICFGR_TRIG_EDGE (1 << 1)
113 #define GICD_ICFGR_TRIG_MASK 0x2
115 #ifndef GIC_DEFAULT_ICFGR_INIT
116 #define GIC_DEFAULT_ICFGR_INIT 0x00000000
121 struct intr_irqsrc gi_isrc;
123 enum intr_polarity gi_pol;
124 enum intr_trigger gi_trig;
127 static u_int gic_irq_cpu;
128 static int arm_gic_intr(void *);
129 static int arm_gic_bind_intr(device_t dev, struct intr_irqsrc *isrc);
132 u_int sgi_to_ipi[GIC_LAST_SGI - GIC_FIRST_SGI + 1];
133 u_int sgi_first_unused = GIC_FIRST_SGI;
137 struct arm_gic_softc {
141 struct gic_irqsrc * gic_irqs;
143 struct resource * gic_res[3];
144 bus_space_tag_t gic_c_bst;
145 bus_space_tag_t gic_d_bst;
146 bus_space_handle_t gic_c_bsh;
147 bus_space_handle_t gic_d_bsh;
151 #ifdef GIC_DEBUG_SPURIOUS
152 uint32_t last_irq[MAXCPU];
157 #define GIC_INTR_ISRC(sc, irq) (&sc->gic_irqs[irq].gi_isrc)
160 static struct resource_spec arm_gic_spec[] = {
161 { SYS_RES_MEMORY, 0, RF_ACTIVE }, /* Distributor registers */
162 { SYS_RES_MEMORY, 1, RF_ACTIVE }, /* CPU Interrupt Intf. registers */
164 { SYS_RES_IRQ, 0, RF_ACTIVE | RF_OPTIONAL }, /* Parent interrupt */
169 static u_int arm_gic_map[MAXCPU];
171 static struct arm_gic_softc *gic_sc = NULL;
173 #define gic_c_read_4(_sc, _reg) \
174 bus_space_read_4((_sc)->gic_c_bst, (_sc)->gic_c_bsh, (_reg))
175 #define gic_c_write_4(_sc, _reg, _val) \
176 bus_space_write_4((_sc)->gic_c_bst, (_sc)->gic_c_bsh, (_reg), (_val))
177 #define gic_d_read_4(_sc, _reg) \
178 bus_space_read_4((_sc)->gic_d_bst, (_sc)->gic_d_bsh, (_reg))
179 #define gic_d_write_1(_sc, _reg, _val) \
180 bus_space_write_1((_sc)->gic_d_bst, (_sc)->gic_d_bsh, (_reg), (_val))
181 #define gic_d_write_4(_sc, _reg, _val) \
182 bus_space_write_4((_sc)->gic_d_bst, (_sc)->gic_d_bsh, (_reg), (_val))
185 static int gic_config_irq(int irq, enum intr_trigger trig,
186 enum intr_polarity pol);
187 static void gic_post_filter(void *);
190 static struct ofw_compat_data compat_data[] = {
191 {"arm,gic", true}, /* Non-standard, used in FreeBSD dts. */
192 {"arm,gic-400", true},
193 {"arm,cortex-a15-gic", true},
194 {"arm,cortex-a9-gic", true},
195 {"arm,cortex-a7-gic", true},
196 {"arm,arm11mp-gic", true},
197 {"brcm,brahma-b15-gic", true},
202 arm_gic_probe(device_t dev)
205 if (!ofw_bus_status_okay(dev))
208 if (!ofw_bus_search_compatible(dev, compat_data)->ocd_data)
210 device_set_desc(dev, "ARM Generic Interrupt Controller");
211 return (BUS_PROBE_DEFAULT);
216 gic_irq_unmask(struct arm_gic_softc *sc, u_int irq)
219 gic_d_write_4(sc, GICD_ISENABLER(irq >> 5), (1UL << (irq & 0x1F)));
223 gic_irq_mask(struct arm_gic_softc *sc, u_int irq)
226 gic_d_write_4(sc, GICD_ICENABLER(irq >> 5), (1UL << (irq & 0x1F)));
231 gic_cpu_mask(struct arm_gic_softc *sc)
236 /* Read the current cpuid mask by reading ITARGETSR{0..7} */
237 for (i = 0; i < 8; i++) {
238 mask = gic_d_read_4(sc, GICD_ITARGETSR(i));
242 /* No mask found, assume we are on CPU interface 0 */
246 /* Collect the mask in the lower byte */
256 arm_gic_init_secondary(device_t dev)
258 struct arm_gic_softc *sc = device_get_softc(dev);
261 /* Set the mask so we can find this CPU to send it IPIs */
262 cpu = PCPU_GET(cpuid);
263 arm_gic_map[cpu] = gic_cpu_mask(sc);
265 for (irq = 0; irq < sc->nirqs; irq += 4)
266 gic_d_write_4(sc, GICD_IPRIORITYR(irq >> 2), 0);
268 /* Set all the interrupts to be in Group 0 (secure) */
269 for (irq = 0; irq < sc->nirqs; irq += 32) {
270 gic_d_write_4(sc, GICD_IGROUPR(irq >> 5), 0);
273 /* Enable CPU interface */
274 gic_c_write_4(sc, GICC_CTLR, 1);
276 /* Set priority mask register. */
277 gic_c_write_4(sc, GICC_PMR, 0xff);
279 /* Enable interrupt distribution */
280 gic_d_write_4(sc, GICD_CTLR, 0x01);
282 /* Unmask attached SGI interrupts. */
283 for (irq = GIC_FIRST_SGI; irq <= GIC_LAST_SGI; irq++)
284 if (intr_isrc_init_on_cpu(GIC_INTR_ISRC(sc, irq), cpu))
285 gic_irq_unmask(sc, irq);
287 /* Unmask attached PPI interrupts. */
288 for (irq = GIC_FIRST_PPI; irq <= GIC_LAST_PPI; irq++)
289 if (intr_isrc_init_on_cpu(GIC_INTR_ISRC(sc, irq), cpu))
290 gic_irq_unmask(sc, irq);
294 arm_gic_init_secondary(device_t dev)
296 struct arm_gic_softc *sc = device_get_softc(dev);
299 /* Set the mask so we can find this CPU to send it IPIs */
300 arm_gic_map[PCPU_GET(cpuid)] = gic_cpu_mask(sc);
302 for (i = 0; i < sc->nirqs; i += 4)
303 gic_d_write_4(sc, GICD_IPRIORITYR(i >> 2), 0);
305 /* Set all the interrupts to be in Group 0 (secure) */
306 for (i = 0; i < sc->nirqs; i += 32) {
307 gic_d_write_4(sc, GICD_IGROUPR(i >> 5), 0);
310 /* Enable CPU interface */
311 gic_c_write_4(sc, GICC_CTLR, 1);
313 /* Set priority mask register. */
314 gic_c_write_4(sc, GICC_PMR, 0xff);
316 /* Enable interrupt distribution */
317 gic_d_write_4(sc, GICD_CTLR, 0x01);
320 * Activate the timer interrupts: virtual, secure, and non-secure.
322 gic_d_write_4(sc, GICD_ISENABLER(27 >> 5), (1UL << (27 & 0x1F)));
323 gic_d_write_4(sc, GICD_ISENABLER(29 >> 5), (1UL << (29 & 0x1F)));
324 gic_d_write_4(sc, GICD_ISENABLER(30 >> 5), (1UL << (30 & 0x1F)));
326 #endif /* ARM_INTRNG */
331 gic_decode_fdt(phandle_t iparent, pcell_t *intr, int *interrupt,
334 static u_int num_intr_cells;
335 static phandle_t self;
336 struct ofw_compat_data *ocd;
339 for (ocd = compat_data; ocd->ocd_str != NULL; ocd++) {
340 if (fdt_is_compatible(iparent, ocd->ocd_str)) {
349 if (num_intr_cells == 0) {
350 if (OF_searchencprop(OF_node_from_xref(iparent),
351 "#interrupt-cells", &num_intr_cells,
352 sizeof(num_intr_cells)) == -1) {
357 if (num_intr_cells == 1) {
358 *interrupt = fdt32_to_cpu(intr[0]);
359 *trig = INTR_TRIGGER_CONFORM;
360 *pol = INTR_POLARITY_CONFORM;
362 if (fdt32_to_cpu(intr[0]) == 0)
363 *interrupt = fdt32_to_cpu(intr[1]) + GIC_FIRST_SPI;
365 *interrupt = fdt32_to_cpu(intr[1]) + GIC_FIRST_PPI;
367 * In intr[2], bits[3:0] are trigger type and level flags.
368 * 1 = low-to-high edge triggered
369 * 2 = high-to-low edge triggered
370 * 4 = active high level-sensitive
371 * 8 = active low level-sensitive
372 * The hardware only supports active-high-level or rising-edge
375 if (*interrupt >= GIC_FIRST_SPI &&
376 fdt32_to_cpu(intr[2]) & 0x0a) {
377 printf("unsupported trigger/polarity configuration "
378 "0x%02x\n", fdt32_to_cpu(intr[2]) & 0x0f);
380 *pol = INTR_POLARITY_CONFORM;
381 if (fdt32_to_cpu(intr[2]) & 0x03)
382 *trig = INTR_TRIGGER_EDGE;
384 *trig = INTR_TRIGGER_LEVEL;
391 static inline intptr_t
392 gic_xref(device_t dev)
395 return (OF_xref_from_node(ofw_bus_get_node(dev)));
402 arm_gic_register_isrcs(struct arm_gic_softc *sc, uint32_t num)
406 struct gic_irqsrc *irqs;
407 struct intr_irqsrc *isrc;
410 irqs = malloc(num * sizeof(struct gic_irqsrc), M_DEVBUF,
413 name = device_get_nameunit(sc->gic_dev);
414 for (irq = 0; irq < num; irq++) {
415 irqs[irq].gi_irq = irq;
416 irqs[irq].gi_pol = INTR_POLARITY_CONFORM;
417 irqs[irq].gi_trig = INTR_TRIGGER_CONFORM;
419 isrc = &irqs[irq].gi_isrc;
420 if (irq <= GIC_LAST_SGI) {
421 error = intr_isrc_register(isrc, sc->gic_dev,
422 INTR_ISRCF_IPI, "%s,i%u", name, irq - GIC_FIRST_SGI);
423 } else if (irq <= GIC_LAST_PPI) {
424 error = intr_isrc_register(isrc, sc->gic_dev,
425 INTR_ISRCF_PPI, "%s,p%u", name, irq - GIC_FIRST_PPI);
427 error = intr_isrc_register(isrc, sc->gic_dev, 0,
428 "%s,s%u", name, irq - GIC_FIRST_SPI);
431 /* XXX call intr_isrc_deregister() */
432 free(irqs, M_DEVBUF);
443 arm_gic_attach(device_t dev)
445 struct arm_gic_softc *sc;
447 uint32_t icciidr, mask, nirqs;
450 intptr_t xref = gic_xref(dev);
456 sc = device_get_softc(dev);
458 if (bus_alloc_resources(dev, arm_gic_spec, sc->gic_res)) {
459 device_printf(dev, "could not allocate resources\n");
466 /* Initialize mutex */
467 mtx_init(&sc->mutex, "GIC lock", "", MTX_SPIN);
469 /* Distributor Interface */
470 sc->gic_d_bst = rman_get_bustag(sc->gic_res[0]);
471 sc->gic_d_bsh = rman_get_bushandle(sc->gic_res[0]);
474 sc->gic_c_bst = rman_get_bustag(sc->gic_res[1]);
475 sc->gic_c_bsh = rman_get_bushandle(sc->gic_res[1]);
477 /* Disable interrupt forwarding to the CPU interface */
478 gic_d_write_4(sc, GICD_CTLR, 0x00);
480 /* Get the number of interrupts */
481 nirqs = gic_d_read_4(sc, GICD_TYPER);
482 nirqs = 32 * ((nirqs & 0x1f) + 1);
485 if (arm_gic_register_isrcs(sc, nirqs)) {
486 device_printf(dev, "could not register irqs\n");
492 /* Set up function pointers */
493 arm_post_filter = gic_post_filter;
494 arm_config_irq = gic_config_irq;
497 icciidr = gic_c_read_4(sc, GICC_IIDR);
498 device_printf(dev,"pn 0x%x, arch 0x%x, rev 0x%x, implementer 0x%x irqs %u\n",
499 icciidr>>20, (icciidr>>16) & 0xF, (icciidr>>12) & 0xf,
500 (icciidr & 0xfff), sc->nirqs);
502 /* Set all global interrupts to be level triggered, active low. */
503 for (i = 32; i < sc->nirqs; i += 16) {
504 gic_d_write_4(sc, GICD_ICFGR(i >> 4), GIC_DEFAULT_ICFGR_INIT);
507 /* Disable all interrupts. */
508 for (i = 32; i < sc->nirqs; i += 32) {
509 gic_d_write_4(sc, GICD_ICENABLER(i >> 5), 0xFFFFFFFF);
512 /* Find the current cpu mask */
513 mask = gic_cpu_mask(sc);
514 /* Set the mask so we can find this CPU to send it IPIs */
515 arm_gic_map[PCPU_GET(cpuid)] = mask;
516 /* Set all four targets to this cpu */
520 for (i = 0; i < sc->nirqs; i += 4) {
521 gic_d_write_4(sc, GICD_IPRIORITYR(i >> 2), 0);
523 gic_d_write_4(sc, GICD_ITARGETSR(i >> 2), mask);
527 /* Set all the interrupts to be in Group 0 (secure) */
528 for (i = 0; i < sc->nirqs; i += 32) {
529 gic_d_write_4(sc, GICD_IGROUPR(i >> 5), 0);
532 /* Enable CPU interface */
533 gic_c_write_4(sc, GICC_CTLR, 1);
535 /* Set priority mask register. */
536 gic_c_write_4(sc, GICC_PMR, 0xff);
538 /* Enable interrupt distribution */
539 gic_d_write_4(sc, GICD_CTLR, 0x01);
544 * Now, when everything is initialized, it's right time to
545 * register interrupt controller to interrupt framefork.
547 if (intr_pic_register(dev, xref) != 0) {
548 device_printf(dev, "could not register PIC\n");
553 * Controller is root if:
554 * - doesn't have interrupt parent
555 * - his interrupt parent is this controller
557 pxref = ofw_bus_find_iparent(ofw_bus_get_node(dev));
558 if (pxref == 0 || xref == pxref) {
559 if (intr_pic_claim_root(dev, xref, arm_gic_intr, sc,
560 GIC_LAST_SGI - GIC_FIRST_SGI + 1) != 0) {
561 device_printf(dev, "could not set PIC as a root\n");
562 intr_pic_deregister(dev, xref);
566 if (sc->gic_res[2] == NULL) {
568 "not root PIC must have defined interrupt\n");
569 intr_pic_deregister(dev, xref);
572 if (bus_setup_intr(dev, sc->gic_res[2], INTR_TYPE_CLK,
573 arm_gic_intr, NULL, sc, &sc->gic_intrhand)) {
574 device_printf(dev, "could not setup irq handler\n");
575 intr_pic_deregister(dev, xref);
580 OF_device_register_xref(xref, dev);
585 * XXX - not implemented arm_gic_detach() should be called !
587 if (sc->gic_irqs != NULL)
588 free(sc->gic_irqs, M_DEVBUF);
589 bus_release_resources(dev, arm_gic_spec, sc->gic_res);
596 arm_gic_intr(void *arg)
598 struct arm_gic_softc *sc = arg;
599 struct gic_irqsrc *gi;
600 uint32_t irq_active_reg, irq;
601 struct trapframe *tf;
603 irq_active_reg = gic_c_read_4(sc, GICC_IAR);
604 irq = irq_active_reg & 0x3FF;
607 * 1. We do EOI here because recent read value from active interrupt
608 * register must be used for it. Another approach is to save this
609 * value into associated interrupt source.
610 * 2. EOI must be done on same CPU where interrupt has fired. Thus
611 * we must ensure that interrupted thread does not migrate to
613 * 3. EOI cannot be delayed by any preemption which could happen on
614 * critical_exit() used in MI intr code, when interrupt thread is
615 * scheduled. See next point.
616 * 4. IPI_RENDEZVOUS assumes that no preemption is permitted during
617 * an action and any use of critical_exit() could break this
618 * assumption. See comments within smp_rendezvous_action().
619 * 5. We always return FILTER_HANDLED as this is an interrupt
620 * controller dispatch function. Otherwise, in cascaded interrupt
621 * case, the whole interrupt subtree would be masked.
624 if (irq >= sc->nirqs) {
625 #ifdef GIC_DEBUG_SPURIOUS
626 device_printf(sc->gic_dev,
627 "Spurious interrupt detected: last irq: %d on CPU%d\n",
628 sc->last_irq[PCPU_GET(cpuid)], PCPU_GET(cpuid));
630 return (FILTER_HANDLED);
633 tf = curthread->td_intr_frame;
635 gi = sc->gic_irqs + irq;
637 * Note that GIC_FIRST_SGI is zero and is not used in 'if' statement
638 * as compiler complains that comparing u_int >= 0 is always true.
640 if (irq <= GIC_LAST_SGI) {
642 /* Call EOI for all IPI before dispatch. */
643 gic_c_write_4(sc, GICC_EOIR, irq_active_reg);
644 intr_ipi_dispatch(sgi_to_ipi[gi->gi_irq], tf);
647 device_printf(sc->gic_dev, "SGI %u on UP system detected\n",
648 irq - GIC_FIRST_SGI);
649 gic_c_write_4(sc, GICC_EOIR, irq_active_reg);
654 #ifdef GIC_DEBUG_SPURIOUS
655 sc->last_irq[PCPU_GET(cpuid)] = irq;
657 if (gi->gi_trig == INTR_TRIGGER_EDGE)
658 gic_c_write_4(sc, GICC_EOIR, irq_active_reg);
660 if (intr_isrc_dispatch(&gi->gi_isrc, tf) != 0) {
661 gic_irq_mask(sc, irq);
662 if (gi->gi_trig != INTR_TRIGGER_EDGE)
663 gic_c_write_4(sc, GICC_EOIR, irq_active_reg);
664 device_printf(sc->gic_dev, "Stray irq %u disabled\n", irq);
668 arm_irq_memory_barrier(irq);
669 irq_active_reg = gic_c_read_4(sc, GICC_IAR);
670 irq = irq_active_reg & 0x3FF;
674 return (FILTER_HANDLED);
678 gic_config(struct arm_gic_softc *sc, u_int irq, enum intr_trigger trig,
679 enum intr_polarity pol)
684 if (irq < GIC_FIRST_SPI)
687 mtx_lock_spin(&sc->mutex);
689 reg = gic_d_read_4(sc, GICD_ICFGR(irq >> 4));
690 mask = (reg >> 2*(irq % 16)) & 0x3;
692 if (pol == INTR_POLARITY_LOW) {
693 mask &= ~GICD_ICFGR_POL_MASK;
694 mask |= GICD_ICFGR_POL_LOW;
695 } else if (pol == INTR_POLARITY_HIGH) {
696 mask &= ~GICD_ICFGR_POL_MASK;
697 mask |= GICD_ICFGR_POL_HIGH;
700 if (trig == INTR_TRIGGER_LEVEL) {
701 mask &= ~GICD_ICFGR_TRIG_MASK;
702 mask |= GICD_ICFGR_TRIG_LVL;
703 } else if (trig == INTR_TRIGGER_EDGE) {
704 mask &= ~GICD_ICFGR_TRIG_MASK;
705 mask |= GICD_ICFGR_TRIG_EDGE;
709 reg = reg & ~(0x3 << 2*(irq % 16));
710 reg = reg | (mask << 2*(irq % 16));
711 gic_d_write_4(sc, GICD_ICFGR(irq >> 4), reg);
713 mtx_unlock_spin(&sc->mutex);
717 gic_bind(struct arm_gic_softc *sc, u_int irq, cpuset_t *cpus)
719 uint32_t cpu, end, mask;
721 end = min(mp_ncpus, 8);
722 for (cpu = end; cpu < MAXCPU; cpu++)
723 if (CPU_ISSET(cpu, cpus))
726 for (mask = 0, cpu = 0; cpu < end; cpu++)
727 if (CPU_ISSET(cpu, cpus))
730 gic_d_write_1(sc, GICD_ITARGETSR(0) + irq, mask);
736 gic_map_fdt(device_t dev, u_int ncells, pcell_t *cells, u_int *irqp,
737 enum intr_polarity *polp, enum intr_trigger *trigp)
742 *polp = INTR_POLARITY_CONFORM;
743 *trigp = INTR_TRIGGER_CONFORM;
750 * The 1st cell is the interrupt type:
753 * The 2nd cell contains the interrupt number:
756 * The 3rd cell is the flags, encoded as follows:
757 * bits[3:0] trigger type and level flags
758 * 1 = low-to-high edge triggered
759 * 2 = high-to-low edge triggered
760 * 4 = active high level-sensitive
761 * 8 = active low level-sensitive
762 * bits[15:8] PPI interrupt cpu mask
763 * Each bit corresponds to each of the 8 possible cpus
764 * attached to the GIC. A bit set to '1' indicated
765 * the interrupt is wired to that CPU.
769 irq = GIC_FIRST_SPI + cells[1];
770 /* SPI irq is checked later. */
773 irq = GIC_FIRST_PPI + cells[1];
774 if (irq > GIC_LAST_PPI) {
775 device_printf(dev, "unsupported PPI interrupt "
776 "number %u\n", cells[1]);
781 device_printf(dev, "unsupported interrupt type "
782 "configuration %u\n", cells[0]);
786 tripol = cells[2] & 0xff;
787 if (tripol & 0xf0 || (tripol & 0x0a && cells[0] == 0))
788 device_printf(dev, "unsupported trigger/polarity "
789 "configuration 0x%02x\n", tripol);
792 *polp = INTR_POLARITY_CONFORM;
793 *trigp = tripol & 0x03 ? INTR_TRIGGER_EDGE : INTR_TRIGGER_LEVEL;
801 gic_map_intr(device_t dev, struct intr_map_data *data, u_int *irqp,
802 enum intr_polarity *polp, enum intr_trigger *trigp)
805 enum intr_polarity pol;
806 enum intr_trigger trig;
807 struct arm_gic_softc *sc;
809 sc = device_get_softc(dev);
810 switch (data->type) {
812 case INTR_MAP_DATA_FDT:
813 if (gic_map_fdt(dev, data->fdt.ncells, data->fdt.cells, &irq,
822 if (irq >= sc->nirqs)
824 if (pol != INTR_POLARITY_CONFORM && pol != INTR_POLARITY_LOW &&
825 pol != INTR_POLARITY_HIGH)
827 if (trig != INTR_TRIGGER_CONFORM && trig != INTR_TRIGGER_EDGE &&
828 trig != INTR_TRIGGER_LEVEL)
840 arm_gic_map_intr(device_t dev, struct intr_map_data *data,
841 struct intr_irqsrc **isrcp)
845 struct arm_gic_softc *sc;
847 error = gic_map_intr(dev, data, &irq, NULL, NULL);
849 sc = device_get_softc(dev);
850 *isrcp = GIC_INTR_ISRC(sc, irq);
856 arm_gic_setup_intr(device_t dev, struct intr_irqsrc *isrc,
857 struct resource *res, struct intr_map_data *data)
859 struct arm_gic_softc *sc = device_get_softc(dev);
860 struct gic_irqsrc *gi = (struct gic_irqsrc *)isrc;
862 enum intr_trigger trig;
863 enum intr_polarity pol;
868 /* Get config for resource. */
869 if (gic_map_intr(dev, data, &irq, &pol, &trig))
872 if (gi->gi_irq != irq)
875 /* Compare config if this is not first setup. */
876 if (isrc->isrc_handlers != 0) {
877 if ((pol != INTR_POLARITY_CONFORM && pol != gi->gi_pol) ||
878 (trig != INTR_TRIGGER_CONFORM && trig != gi->gi_trig))
884 if (pol == INTR_POLARITY_CONFORM)
885 pol = INTR_POLARITY_LOW; /* just pick some */
886 if (trig == INTR_TRIGGER_CONFORM)
887 trig = INTR_TRIGGER_EDGE; /* just pick some */
893 * XXX - In case that per CPU interrupt is going to be enabled in time
894 * when SMP is already started, we need some IPI call which
895 * enables it on others CPUs. Further, it's more complicated as
896 * pic_enable_source() and pic_disable_source() should act on
897 * per CPU basis only. Thus, it should be solved here somehow.
899 if (isrc->isrc_flags & INTR_ISRCF_PPI)
900 CPU_SET(PCPU_GET(cpuid), &isrc->isrc_cpu);
902 gic_config(sc, gi->gi_irq, trig, pol);
903 arm_gic_bind_intr(dev, isrc);
908 arm_gic_teardown_intr(device_t dev, struct intr_irqsrc *isrc,
909 struct resource *res, struct intr_map_data *data)
911 struct gic_irqsrc *gi = (struct gic_irqsrc *)isrc;
913 if (isrc->isrc_handlers == 0) {
914 gi->gi_pol = INTR_POLARITY_CONFORM;
915 gi->gi_trig = INTR_TRIGGER_CONFORM;
921 arm_gic_enable_intr(device_t dev, struct intr_irqsrc *isrc)
923 struct arm_gic_softc *sc = device_get_softc(dev);
924 struct gic_irqsrc *gi = (struct gic_irqsrc *)isrc;
926 arm_irq_memory_barrier(gi->gi_irq);
927 gic_irq_unmask(sc, gi->gi_irq);
931 arm_gic_disable_intr(device_t dev, struct intr_irqsrc *isrc)
933 struct arm_gic_softc *sc = device_get_softc(dev);
934 struct gic_irqsrc *gi = (struct gic_irqsrc *)isrc;
936 gic_irq_mask(sc, gi->gi_irq);
940 arm_gic_pre_ithread(device_t dev, struct intr_irqsrc *isrc)
942 struct arm_gic_softc *sc = device_get_softc(dev);
943 struct gic_irqsrc *gi = (struct gic_irqsrc *)isrc;
945 arm_gic_disable_intr(dev, isrc);
946 gic_c_write_4(sc, GICC_EOIR, gi->gi_irq);
950 arm_gic_post_ithread(device_t dev, struct intr_irqsrc *isrc)
953 arm_irq_memory_barrier(0);
954 arm_gic_enable_intr(dev, isrc);
958 arm_gic_post_filter(device_t dev, struct intr_irqsrc *isrc)
960 struct arm_gic_softc *sc = device_get_softc(dev);
961 struct gic_irqsrc *gi = (struct gic_irqsrc *)isrc;
963 /* EOI for edge-triggered done earlier. */
964 if (gi->gi_trig == INTR_TRIGGER_EDGE)
967 arm_irq_memory_barrier(0);
968 gic_c_write_4(sc, GICC_EOIR, gi->gi_irq);
972 arm_gic_bind_intr(device_t dev, struct intr_irqsrc *isrc)
974 struct arm_gic_softc *sc = device_get_softc(dev);
975 struct gic_irqsrc *gi = (struct gic_irqsrc *)isrc;
977 if (gi->gi_irq < GIC_FIRST_SPI)
980 if (CPU_EMPTY(&isrc->isrc_cpu)) {
981 gic_irq_cpu = intr_irq_next_cpu(gic_irq_cpu, &all_cpus);
982 CPU_SETOF(gic_irq_cpu, &isrc->isrc_cpu);
984 return (gic_bind(sc, gi->gi_irq, &isrc->isrc_cpu));
989 arm_gic_ipi_send(device_t dev, struct intr_irqsrc *isrc, cpuset_t cpus,
992 struct arm_gic_softc *sc = device_get_softc(dev);
993 struct gic_irqsrc *gi = (struct gic_irqsrc *)isrc;
996 for (i = 0; i < MAXCPU; i++)
997 if (CPU_ISSET(i, &cpus))
998 val |= arm_gic_map[i] << GICD_SGI_TARGET_SHIFT;
1000 gic_d_write_4(sc, GICD_SGIR(0), val | gi->gi_irq);
1004 arm_gic_ipi_setup(device_t dev, u_int ipi, struct intr_irqsrc **isrcp)
1006 struct arm_gic_softc *sc = device_get_softc(dev);
1008 if (sgi_first_unused > GIC_LAST_SGI)
1011 *isrcp = GIC_INTR_ISRC(sc, sgi_first_unused);
1012 sgi_to_ipi[sgi_first_unused++] = ipi;
1018 arm_gic_next_irq(struct arm_gic_softc *sc, int last_irq)
1020 uint32_t active_irq;
1022 active_irq = gic_c_read_4(sc, GICC_IAR);
1025 * Immediatly EOIR the SGIs, because doing so requires the other
1026 * bits (ie CPU number), not just the IRQ number, and we do not
1027 * have this information later.
1029 if ((active_irq & 0x3ff) <= GIC_LAST_SGI)
1030 gic_c_write_4(sc, GICC_EOIR, active_irq);
1031 active_irq &= 0x3FF;
1033 if (active_irq == 0x3FF) {
1035 device_printf(sc->gic_dev,
1036 "Spurious interrupt detected\n");
1044 arm_gic_config(device_t dev, int irq, enum intr_trigger trig,
1045 enum intr_polarity pol)
1047 struct arm_gic_softc *sc = device_get_softc(dev);
1051 /* Function is public-accessible, so validate input arguments */
1052 if ((irq < 0) || (irq >= sc->nirqs))
1054 if ((trig != INTR_TRIGGER_EDGE) && (trig != INTR_TRIGGER_LEVEL) &&
1055 (trig != INTR_TRIGGER_CONFORM))
1057 if ((pol != INTR_POLARITY_HIGH) && (pol != INTR_POLARITY_LOW) &&
1058 (pol != INTR_POLARITY_CONFORM))
1061 mtx_lock_spin(&sc->mutex);
1063 reg = gic_d_read_4(sc, GICD_ICFGR(irq >> 4));
1064 mask = (reg >> 2*(irq % 16)) & 0x3;
1066 if (pol == INTR_POLARITY_LOW) {
1067 mask &= ~GICD_ICFGR_POL_MASK;
1068 mask |= GICD_ICFGR_POL_LOW;
1069 } else if (pol == INTR_POLARITY_HIGH) {
1070 mask &= ~GICD_ICFGR_POL_MASK;
1071 mask |= GICD_ICFGR_POL_HIGH;
1074 if (trig == INTR_TRIGGER_LEVEL) {
1075 mask &= ~GICD_ICFGR_TRIG_MASK;
1076 mask |= GICD_ICFGR_TRIG_LVL;
1077 } else if (trig == INTR_TRIGGER_EDGE) {
1078 mask &= ~GICD_ICFGR_TRIG_MASK;
1079 mask |= GICD_ICFGR_TRIG_EDGE;
1083 reg = reg & ~(0x3 << 2*(irq % 16));
1084 reg = reg | (mask << 2*(irq % 16));
1085 gic_d_write_4(sc, GICD_ICFGR(irq >> 4), reg);
1087 mtx_unlock_spin(&sc->mutex);
1092 device_printf(dev, "gic_config_irg, invalid parameters\n");
1098 arm_gic_mask(device_t dev, int irq)
1100 struct arm_gic_softc *sc = device_get_softc(dev);
1102 gic_d_write_4(sc, GICD_ICENABLER(irq >> 5), (1UL << (irq & 0x1F)));
1103 gic_c_write_4(sc, GICC_EOIR, irq); /* XXX - not allowed */
1107 arm_gic_unmask(device_t dev, int irq)
1109 struct arm_gic_softc *sc = device_get_softc(dev);
1111 if (irq > GIC_LAST_SGI)
1112 arm_irq_memory_barrier(irq);
1114 gic_d_write_4(sc, GICD_ISENABLER(irq >> 5), (1UL << (irq & 0x1F)));
1119 arm_gic_ipi_send(device_t dev, cpuset_t cpus, u_int ipi)
1121 struct arm_gic_softc *sc = device_get_softc(dev);
1122 uint32_t val = 0, i;
1124 for (i = 0; i < MAXCPU; i++)
1125 if (CPU_ISSET(i, &cpus))
1126 val |= arm_gic_map[i] << GICD_SGI_TARGET_SHIFT;
1128 gic_d_write_4(sc, GICD_SGIR(0), val | ipi);
1132 arm_gic_ipi_read(device_t dev, int i)
1137 * The intr code will automagically give the frame pointer
1138 * if the interrupt argument is 0.
1140 if ((unsigned int)i > 16)
1149 arm_gic_ipi_clear(device_t dev, int ipi)
1156 gic_post_filter(void *arg)
1158 struct arm_gic_softc *sc = gic_sc;
1159 uintptr_t irq = (uintptr_t) arg;
1161 if (irq > GIC_LAST_SGI)
1162 arm_irq_memory_barrier(irq);
1163 gic_c_write_4(sc, GICC_EOIR, irq);
1167 gic_config_irq(int irq, enum intr_trigger trig, enum intr_polarity pol)
1170 return (arm_gic_config(gic_sc->gic_dev, irq, trig, pol));
1174 arm_mask_irq(uintptr_t nb)
1177 arm_gic_mask(gic_sc->gic_dev, nb);
1181 arm_unmask_irq(uintptr_t nb)
1184 arm_gic_unmask(gic_sc->gic_dev, nb);
1188 arm_get_next_irq(int last_irq)
1191 return (arm_gic_next_irq(gic_sc, last_irq));
1196 intr_pic_init_secondary(void)
1199 arm_gic_init_secondary(gic_sc->gic_dev);
1203 pic_ipi_send(cpuset_t cpus, u_int ipi)
1206 arm_gic_ipi_send(gic_sc->gic_dev, cpus, ipi);
1213 return (arm_gic_ipi_read(gic_sc->gic_dev, i));
1217 pic_ipi_clear(int ipi)
1220 arm_gic_ipi_clear(gic_sc->gic_dev, ipi);
1223 #endif /* ARM_INTRNG */
1225 static device_method_t arm_gic_methods[] = {
1226 /* Device interface */
1227 DEVMETHOD(device_probe, arm_gic_probe),
1228 DEVMETHOD(device_attach, arm_gic_attach),
1230 /* Interrupt controller interface */
1231 DEVMETHOD(pic_disable_intr, arm_gic_disable_intr),
1232 DEVMETHOD(pic_enable_intr, arm_gic_enable_intr),
1233 DEVMETHOD(pic_map_intr, arm_gic_map_intr),
1234 DEVMETHOD(pic_setup_intr, arm_gic_setup_intr),
1235 DEVMETHOD(pic_teardown_intr, arm_gic_teardown_intr),
1236 DEVMETHOD(pic_post_filter, arm_gic_post_filter),
1237 DEVMETHOD(pic_post_ithread, arm_gic_post_ithread),
1238 DEVMETHOD(pic_pre_ithread, arm_gic_pre_ithread),
1240 DEVMETHOD(pic_bind_intr, arm_gic_bind_intr),
1241 DEVMETHOD(pic_init_secondary, arm_gic_init_secondary),
1242 DEVMETHOD(pic_ipi_send, arm_gic_ipi_send),
1243 DEVMETHOD(pic_ipi_setup, arm_gic_ipi_setup),
1249 static driver_t arm_gic_driver = {
1252 sizeof(struct arm_gic_softc),
1255 static devclass_t arm_gic_devclass;
1257 EARLY_DRIVER_MODULE(gic, simplebus, arm_gic_driver, arm_gic_devclass, 0, 0,
1258 BUS_PASS_INTERRUPT + BUS_PASS_ORDER_MIDDLE);
1259 EARLY_DRIVER_MODULE(gic, ofwbus, arm_gic_driver, arm_gic_devclass, 0, 0,
1260 BUS_PASS_INTERRUPT + BUS_PASS_ORDER_MIDDLE);