2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 2011 The FreeBSD Foundation
7 * Developed by Damjan Marion <damjan.marion@gmail.com>
9 * Based on OMAP4 GIC code by Ben Gray
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
40 #include "opt_platform.h"
42 #include <sys/param.h>
43 #include <sys/systm.h>
45 #include <sys/kernel.h>
47 #include <sys/module.h>
48 #include <sys/malloc.h>
52 #include <sys/cpuset.h>
54 #include <sys/mutex.h>
56 #include <sys/sched.h>
61 #include <machine/bus.h>
62 #include <machine/intr.h>
63 #include <machine/smp.h>
66 #include <dev/fdt/fdt_intr.h>
67 #include <dev/ofw/ofw_bus_subr.h>
71 #include <contrib/dev/acpica/include/acpi.h>
72 #include <dev/acpica/acpivar.h>
75 #include <arm/arm/gic.h>
76 #include <arm/arm/gic_common.h>
82 /* We are using GICv2 register naming */
84 /* Distributor Registers */
87 #define GICC_CTLR 0x0000 /* v1 ICCICR */
88 #define GICC_PMR 0x0004 /* v1 ICCPMR */
89 #define GICC_BPR 0x0008 /* v1 ICCBPR */
90 #define GICC_IAR 0x000C /* v1 ICCIAR */
91 #define GICC_EOIR 0x0010 /* v1 ICCEOIR */
92 #define GICC_RPR 0x0014 /* v1 ICCRPR */
93 #define GICC_HPPIR 0x0018 /* v1 ICCHPIR */
94 #define GICC_ABPR 0x001C /* v1 ICCABPR */
95 #define GICC_IIDR 0x00FC /* v1 ICCIIDR*/
98 #define GICD_TYPER_SECURITYEXT 0x400
99 #define GIC_SUPPORT_SECEXT(_sc) \
100 ((_sc->typer & GICD_TYPER_SECURITYEXT) == GICD_TYPER_SECURITYEXT)
102 #ifndef GIC_DEFAULT_ICFGR_INIT
103 #define GIC_DEFAULT_ICFGR_INIT 0x00000000
107 struct intr_irqsrc gi_isrc;
109 enum intr_polarity gi_pol;
110 enum intr_trigger gi_trig;
111 #define GI_FLAG_EARLY_EOI (1 << 0)
112 #define GI_FLAG_MSI (1 << 1) /* This interrupt source should only */
113 /* be used for MSI/MSI-X interrupts */
114 #define GI_FLAG_MSI_USED (1 << 2) /* This irq is already allocated */
115 /* for a MSI/MSI-X interrupt */
119 static u_int gic_irq_cpu;
120 static int arm_gic_bind_intr(device_t dev, struct intr_irqsrc *isrc);
123 static u_int sgi_to_ipi[GIC_LAST_SGI - GIC_FIRST_SGI + 1];
124 static u_int sgi_first_unused = GIC_FIRST_SGI;
127 #define GIC_INTR_ISRC(sc, irq) (&sc->gic_irqs[irq].gi_isrc)
129 static struct resource_spec arm_gic_spec[] = {
130 { SYS_RES_MEMORY, 0, RF_ACTIVE }, /* Distributor registers */
131 { SYS_RES_MEMORY, 1, RF_ACTIVE }, /* CPU Interrupt Intf. registers */
132 { SYS_RES_IRQ, 0, RF_ACTIVE | RF_OPTIONAL }, /* Parent interrupt */
136 #if defined(__arm__) && defined(INVARIANTS)
137 static int gic_debug_spurious = 1;
139 static int gic_debug_spurious = 0;
141 TUNABLE_INT("hw.gic.debug_spurious", &gic_debug_spurious);
143 static u_int arm_gic_map[MAXCPU];
145 static struct arm_gic_softc *gic_sc = NULL;
148 #define gic_c_read_4(_sc, _reg) \
149 bus_read_4((_sc)->gic_res[GIC_RES_CPU], (_reg))
150 #define gic_c_write_4(_sc, _reg, _val) \
151 bus_write_4((_sc)->gic_res[GIC_RES_CPU], (_reg), (_val))
152 /* Distributor Interface */
153 #define gic_d_read_4(_sc, _reg) \
154 bus_read_4((_sc)->gic_res[GIC_RES_DIST], (_reg))
155 #define gic_d_write_1(_sc, _reg, _val) \
156 bus_write_1((_sc)->gic_res[GIC_RES_DIST], (_reg), (_val))
157 #define gic_d_write_4(_sc, _reg, _val) \
158 bus_write_4((_sc)->gic_res[GIC_RES_DIST], (_reg), (_val))
161 gic_irq_unmask(struct arm_gic_softc *sc, u_int irq)
164 gic_d_write_4(sc, GICD_ISENABLER(irq), GICD_I_MASK(irq));
168 gic_irq_mask(struct arm_gic_softc *sc, u_int irq)
171 gic_d_write_4(sc, GICD_ICENABLER(irq), GICD_I_MASK(irq));
175 gic_cpu_mask(struct arm_gic_softc *sc)
180 /* Read the current cpuid mask by reading ITARGETSR{0..7} */
181 for (i = 0; i < 8; i++) {
182 mask = gic_d_read_4(sc, GICD_ITARGETSR(4 * i));
186 /* No mask found, assume we are on CPU interface 0 */
190 /* Collect the mask in the lower byte */
199 arm_gic_init_secondary(device_t dev)
201 struct arm_gic_softc *sc = device_get_softc(dev);
204 /* Set the mask so we can find this CPU to send it IPIs */
205 cpu = PCPU_GET(cpuid);
206 arm_gic_map[cpu] = gic_cpu_mask(sc);
208 for (irq = 0; irq < sc->nirqs; irq += 4)
209 gic_d_write_4(sc, GICD_IPRIORITYR(irq), 0);
211 /* Set all the interrupts to be in Group 0 (secure) */
212 for (irq = 0; GIC_SUPPORT_SECEXT(sc) && irq < sc->nirqs; irq += 32) {
213 gic_d_write_4(sc, GICD_IGROUPR(irq), 0);
216 /* Enable CPU interface */
217 gic_c_write_4(sc, GICC_CTLR, 1);
219 /* Set priority mask register. */
220 gic_c_write_4(sc, GICC_PMR, 0xff);
222 /* Enable interrupt distribution */
223 gic_d_write_4(sc, GICD_CTLR, 0x01);
225 /* Unmask attached SGI interrupts. */
226 for (irq = GIC_FIRST_SGI; irq <= GIC_LAST_SGI; irq++)
227 if (intr_isrc_init_on_cpu(GIC_INTR_ISRC(sc, irq), cpu))
228 gic_irq_unmask(sc, irq);
230 /* Unmask attached PPI interrupts. */
231 for (irq = GIC_FIRST_PPI; irq <= GIC_LAST_PPI; irq++)
232 if (intr_isrc_init_on_cpu(GIC_INTR_ISRC(sc, irq), cpu))
233 gic_irq_unmask(sc, irq);
238 arm_gic_register_isrcs(struct arm_gic_softc *sc, uint32_t num)
242 struct gic_irqsrc *irqs;
243 struct intr_irqsrc *isrc;
246 irqs = malloc(num * sizeof(struct gic_irqsrc), M_DEVBUF,
249 name = device_get_nameunit(sc->gic_dev);
250 for (irq = 0; irq < num; irq++) {
251 irqs[irq].gi_irq = irq;
252 irqs[irq].gi_pol = INTR_POLARITY_CONFORM;
253 irqs[irq].gi_trig = INTR_TRIGGER_CONFORM;
255 isrc = &irqs[irq].gi_isrc;
256 if (irq <= GIC_LAST_SGI) {
257 error = intr_isrc_register(isrc, sc->gic_dev,
258 INTR_ISRCF_IPI, "%s,i%u", name, irq - GIC_FIRST_SGI);
259 } else if (irq <= GIC_LAST_PPI) {
260 error = intr_isrc_register(isrc, sc->gic_dev,
261 INTR_ISRCF_PPI, "%s,p%u", name, irq - GIC_FIRST_PPI);
263 error = intr_isrc_register(isrc, sc->gic_dev, 0,
264 "%s,s%u", name, irq - GIC_FIRST_SPI);
267 /* XXX call intr_isrc_deregister() */
268 free(irqs, M_DEVBUF);
278 arm_gic_reserve_msi_range(device_t dev, u_int start, u_int count)
280 struct arm_gic_softc *sc;
283 sc = device_get_softc(dev);
285 KASSERT((start + count) < sc->nirqs,
286 ("%s: Trying to allocate too many MSI IRQs: %d + %d > %d", __func__,
287 start, count, sc->nirqs));
288 for (i = 0; i < count; i++) {
289 KASSERT(sc->gic_irqs[start + i].gi_isrc.isrc_handlers == 0,
290 ("%s: MSI interrupt %d already has a handler", __func__,
292 KASSERT(sc->gic_irqs[start + i].gi_pol == INTR_POLARITY_CONFORM,
293 ("%s: MSI interrupt %d already has a polarity", __func__,
295 KASSERT(sc->gic_irqs[start + i].gi_trig == INTR_TRIGGER_CONFORM,
296 ("%s: MSI interrupt %d already has a trigger", __func__,
298 sc->gic_irqs[start + i].gi_pol = INTR_POLARITY_HIGH;
299 sc->gic_irqs[start + i].gi_trig = INTR_TRIGGER_EDGE;
300 sc->gic_irqs[start + i].gi_flags |= GI_FLAG_MSI;
305 arm_gic_attach(device_t dev)
307 struct arm_gic_softc *sc;
309 uint32_t icciidr, mask, nirqs;
314 sc = device_get_softc(dev);
316 if (bus_alloc_resources(dev, arm_gic_spec, sc->gic_res)) {
317 device_printf(dev, "could not allocate resources\n");
324 /* Initialize mutex */
325 mtx_init(&sc->mutex, "GIC lock", NULL, MTX_SPIN);
327 /* Disable interrupt forwarding to the CPU interface */
328 gic_d_write_4(sc, GICD_CTLR, 0x00);
330 /* Get the number of interrupts */
331 sc->typer = gic_d_read_4(sc, GICD_TYPER);
332 nirqs = GICD_TYPER_I_NUM(sc->typer);
334 if (arm_gic_register_isrcs(sc, nirqs)) {
335 device_printf(dev, "could not register irqs\n");
339 icciidr = gic_c_read_4(sc, GICC_IIDR);
341 "pn 0x%x, arch 0x%x, rev 0x%x, implementer 0x%x irqs %u\n",
342 GICD_IIDR_PROD(icciidr), GICD_IIDR_VAR(icciidr),
343 GICD_IIDR_REV(icciidr), GICD_IIDR_IMPL(icciidr), sc->nirqs);
344 sc->gic_iidr = icciidr;
346 /* Set all global interrupts to be level triggered, active low. */
347 for (i = 32; i < sc->nirqs; i += 16) {
348 gic_d_write_4(sc, GICD_ICFGR(i), GIC_DEFAULT_ICFGR_INIT);
351 /* Disable all interrupts. */
352 for (i = 32; i < sc->nirqs; i += 32) {
353 gic_d_write_4(sc, GICD_ICENABLER(i), 0xFFFFFFFF);
356 /* Find the current cpu mask */
357 mask = gic_cpu_mask(sc);
358 /* Set the mask so we can find this CPU to send it IPIs */
359 arm_gic_map[PCPU_GET(cpuid)] = mask;
360 /* Set all four targets to this cpu */
364 for (i = 0; i < sc->nirqs; i += 4) {
365 gic_d_write_4(sc, GICD_IPRIORITYR(i), 0);
367 gic_d_write_4(sc, GICD_ITARGETSR(i), mask);
371 /* Set all the interrupts to be in Group 0 (secure) */
372 for (i = 0; GIC_SUPPORT_SECEXT(sc) && i < sc->nirqs; i += 32) {
373 gic_d_write_4(sc, GICD_IGROUPR(i), 0);
376 /* Enable CPU interface */
377 gic_c_write_4(sc, GICC_CTLR, 1);
379 /* Set priority mask register. */
380 gic_c_write_4(sc, GICC_PMR, 0xff);
382 /* Enable interrupt distribution */
383 gic_d_write_4(sc, GICD_CTLR, 0x01);
392 arm_gic_detach(device_t dev)
394 struct arm_gic_softc *sc;
396 sc = device_get_softc(dev);
398 if (sc->gic_irqs != NULL)
399 free(sc->gic_irqs, M_DEVBUF);
401 bus_release_resources(dev, arm_gic_spec, sc->gic_res);
407 arm_gic_print_child(device_t bus, device_t child)
409 struct resource_list *rl;
412 rv = bus_print_child_header(bus, child);
414 rl = BUS_GET_RESOURCE_LIST(bus, child);
416 rv += resource_list_print_type(rl, "mem", SYS_RES_MEMORY,
418 rv += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%jd");
421 rv += bus_print_child_footer(bus, child);
426 static struct resource *
427 arm_gic_alloc_resource(device_t bus, device_t child, int type, int *rid,
428 rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
430 struct arm_gic_softc *sc;
431 struct resource_list_entry *rle;
432 struct resource_list *rl;
435 KASSERT(type == SYS_RES_MEMORY, ("Invalid resoure type %x", type));
437 sc = device_get_softc(bus);
440 * Request for the default allocation with a given rid: use resource
441 * list stored in the local device info.
443 if (RMAN_IS_DEFAULT_RANGE(start, end)) {
444 rl = BUS_GET_RESOURCE_LIST(bus, child);
446 if (type == SYS_RES_IOPORT)
447 type = SYS_RES_MEMORY;
449 rle = resource_list_find(rl, type, *rid);
452 device_printf(bus, "no default resources for "
453 "rid = %d, type = %d\n", *rid, type);
461 /* Remap through ranges property */
462 for (j = 0; j < sc->nranges; j++) {
463 if (start >= sc->ranges[j].bus && end <
464 sc->ranges[j].bus + sc->ranges[j].size) {
465 start -= sc->ranges[j].bus;
466 start += sc->ranges[j].host;
467 end -= sc->ranges[j].bus;
468 end += sc->ranges[j].host;
472 if (j == sc->nranges && sc->nranges != 0) {
474 device_printf(bus, "Could not map resource "
475 "%#jx-%#jx\n", (uintmax_t)start, (uintmax_t)end);
480 return (bus_generic_alloc_resource(bus, child, type, rid, start, end,
485 arm_gic_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
487 struct arm_gic_softc *sc;
489 sc = device_get_softc(dev);
492 case GIC_IVAR_HW_REV:
493 KASSERT(GICD_IIDR_VAR(sc->gic_iidr) < 3,
494 ("arm_gic_read_ivar: Unknown IIDR revision %u (%.08x)",
495 GICD_IIDR_VAR(sc->gic_iidr), sc->gic_iidr));
496 *result = GICD_IIDR_VAR(sc->gic_iidr);
499 KASSERT(sc->gic_bus != GIC_BUS_UNKNOWN,
500 ("arm_gic_read_ivar: Unknown bus type"));
501 KASSERT(sc->gic_bus <= GIC_BUS_MAX,
502 ("arm_gic_read_ivar: Invalid bus type %u", sc->gic_bus));
503 *result = sc->gic_bus;
511 arm_gic_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
514 case GIC_IVAR_HW_REV:
523 arm_gic_intr(void *arg)
525 struct arm_gic_softc *sc = arg;
526 struct gic_irqsrc *gi;
527 uint32_t irq_active_reg, irq;
528 struct trapframe *tf;
530 irq_active_reg = gic_c_read_4(sc, GICC_IAR);
531 irq = irq_active_reg & 0x3FF;
534 * 1. We do EOI here because recent read value from active interrupt
535 * register must be used for it. Another approach is to save this
536 * value into associated interrupt source.
537 * 2. EOI must be done on same CPU where interrupt has fired. Thus
538 * we must ensure that interrupted thread does not migrate to
540 * 3. EOI cannot be delayed by any preemption which could happen on
541 * critical_exit() used in MI intr code, when interrupt thread is
542 * scheduled. See next point.
543 * 4. IPI_RENDEZVOUS assumes that no preemption is permitted during
544 * an action and any use of critical_exit() could break this
545 * assumption. See comments within smp_rendezvous_action().
546 * 5. We always return FILTER_HANDLED as this is an interrupt
547 * controller dispatch function. Otherwise, in cascaded interrupt
548 * case, the whole interrupt subtree would be masked.
551 if (irq >= sc->nirqs) {
552 if (gic_debug_spurious)
553 device_printf(sc->gic_dev,
554 "Spurious interrupt detected: last irq: %d on CPU%d\n",
555 sc->last_irq[PCPU_GET(cpuid)], PCPU_GET(cpuid));
556 return (FILTER_HANDLED);
559 tf = curthread->td_intr_frame;
561 gi = sc->gic_irqs + irq;
563 * Note that GIC_FIRST_SGI is zero and is not used in 'if' statement
564 * as compiler complains that comparing u_int >= 0 is always true.
566 if (irq <= GIC_LAST_SGI) {
568 /* Call EOI for all IPI before dispatch. */
569 gic_c_write_4(sc, GICC_EOIR, irq_active_reg);
570 intr_ipi_dispatch(sgi_to_ipi[gi->gi_irq], tf);
573 device_printf(sc->gic_dev, "SGI %u on UP system detected\n",
574 irq - GIC_FIRST_SGI);
575 gic_c_write_4(sc, GICC_EOIR, irq_active_reg);
580 if (gic_debug_spurious)
581 sc->last_irq[PCPU_GET(cpuid)] = irq;
582 if ((gi->gi_flags & GI_FLAG_EARLY_EOI) == GI_FLAG_EARLY_EOI)
583 gic_c_write_4(sc, GICC_EOIR, irq_active_reg);
585 if (intr_isrc_dispatch(&gi->gi_isrc, tf) != 0) {
586 gic_irq_mask(sc, irq);
587 if ((gi->gi_flags & GI_FLAG_EARLY_EOI) != GI_FLAG_EARLY_EOI)
588 gic_c_write_4(sc, GICC_EOIR, irq_active_reg);
589 device_printf(sc->gic_dev, "Stray irq %u disabled\n", irq);
593 arm_irq_memory_barrier(irq);
594 irq_active_reg = gic_c_read_4(sc, GICC_IAR);
595 irq = irq_active_reg & 0x3FF;
599 return (FILTER_HANDLED);
603 gic_config(struct arm_gic_softc *sc, u_int irq, enum intr_trigger trig,
604 enum intr_polarity pol)
609 if (irq < GIC_FIRST_SPI)
612 mtx_lock_spin(&sc->mutex);
614 reg = gic_d_read_4(sc, GICD_ICFGR(irq));
615 mask = (reg >> 2*(irq % 16)) & 0x3;
617 if (pol == INTR_POLARITY_LOW) {
618 mask &= ~GICD_ICFGR_POL_MASK;
619 mask |= GICD_ICFGR_POL_LOW;
620 } else if (pol == INTR_POLARITY_HIGH) {
621 mask &= ~GICD_ICFGR_POL_MASK;
622 mask |= GICD_ICFGR_POL_HIGH;
625 if (trig == INTR_TRIGGER_LEVEL) {
626 mask &= ~GICD_ICFGR_TRIG_MASK;
627 mask |= GICD_ICFGR_TRIG_LVL;
628 } else if (trig == INTR_TRIGGER_EDGE) {
629 mask &= ~GICD_ICFGR_TRIG_MASK;
630 mask |= GICD_ICFGR_TRIG_EDGE;
634 reg = reg & ~(0x3 << 2*(irq % 16));
635 reg = reg | (mask << 2*(irq % 16));
636 gic_d_write_4(sc, GICD_ICFGR(irq), reg);
638 mtx_unlock_spin(&sc->mutex);
642 gic_bind(struct arm_gic_softc *sc, u_int irq, cpuset_t *cpus)
644 uint32_t cpu, end, mask;
646 end = min(mp_ncpus, 8);
647 for (cpu = end; cpu < MAXCPU; cpu++)
648 if (CPU_ISSET(cpu, cpus))
651 for (mask = 0, cpu = 0; cpu < end; cpu++)
652 if (CPU_ISSET(cpu, cpus))
653 mask |= arm_gic_map[cpu];
655 gic_d_write_1(sc, GICD_ITARGETSR(0) + irq, mask);
661 gic_map_fdt(device_t dev, u_int ncells, pcell_t *cells, u_int *irqp,
662 enum intr_polarity *polp, enum intr_trigger *trigp)
667 *polp = INTR_POLARITY_CONFORM;
668 *trigp = INTR_TRIGGER_CONFORM;
675 * The 1st cell is the interrupt type:
678 * The 2nd cell contains the interrupt number:
681 * The 3rd cell is the flags, encoded as follows:
682 * bits[3:0] trigger type and level flags
683 * 1 = low-to-high edge triggered
684 * 2 = high-to-low edge triggered
685 * 4 = active high level-sensitive
686 * 8 = active low level-sensitive
687 * bits[15:8] PPI interrupt cpu mask
688 * Each bit corresponds to each of the 8 possible cpus
689 * attached to the GIC. A bit set to '1' indicated
690 * the interrupt is wired to that CPU.
694 irq = GIC_FIRST_SPI + cells[1];
695 /* SPI irq is checked later. */
698 irq = GIC_FIRST_PPI + cells[1];
699 if (irq > GIC_LAST_PPI) {
700 device_printf(dev, "unsupported PPI interrupt "
701 "number %u\n", cells[1]);
706 device_printf(dev, "unsupported interrupt type "
707 "configuration %u\n", cells[0]);
711 tripol = cells[2] & 0xff;
712 if (tripol & 0xf0 || (tripol & FDT_INTR_LOW_MASK &&
714 device_printf(dev, "unsupported trigger/polarity "
715 "configuration 0x%02x\n", tripol);
718 *polp = INTR_POLARITY_CONFORM;
719 *trigp = tripol & FDT_INTR_EDGE_MASK ?
720 INTR_TRIGGER_EDGE : INTR_TRIGGER_LEVEL;
728 gic_map_msi(device_t dev, struct intr_map_data_msi *msi_data, u_int *irqp,
729 enum intr_polarity *polp, enum intr_trigger *trigp)
731 struct gic_irqsrc *gi;
733 /* Map a non-GICv2m MSI */
734 gi = (struct gic_irqsrc *)msi_data->isrc;
740 /* MSI/MSI-X interrupts are always edge triggered with high polarity */
741 *polp = INTR_POLARITY_HIGH;
742 *trigp = INTR_TRIGGER_EDGE;
748 gic_map_intr(device_t dev, struct intr_map_data *data, u_int *irqp,
749 enum intr_polarity *polp, enum intr_trigger *trigp)
752 enum intr_polarity pol;
753 enum intr_trigger trig;
754 struct arm_gic_softc *sc;
755 struct intr_map_data_msi *dam;
757 struct intr_map_data_fdt *daf;
760 struct intr_map_data_acpi *daa;
763 sc = device_get_softc(dev);
764 switch (data->type) {
766 case INTR_MAP_DATA_FDT:
767 daf = (struct intr_map_data_fdt *)data;
768 if (gic_map_fdt(dev, daf->ncells, daf->cells, &irq, &pol,
771 KASSERT(irq >= sc->nirqs ||
772 (sc->gic_irqs[irq].gi_flags & GI_FLAG_MSI) == 0,
773 ("%s: Attempting to map a MSI interrupt from FDT",
778 case INTR_MAP_DATA_ACPI:
779 daa = (struct intr_map_data_acpi *)data;
785 case INTR_MAP_DATA_MSI:
787 dam = (struct intr_map_data_msi *)data;
788 if (gic_map_msi(dev, dam, &irq, &pol, &trig) != 0)
795 if (irq >= sc->nirqs)
797 if (pol != INTR_POLARITY_CONFORM && pol != INTR_POLARITY_LOW &&
798 pol != INTR_POLARITY_HIGH)
800 if (trig != INTR_TRIGGER_CONFORM && trig != INTR_TRIGGER_EDGE &&
801 trig != INTR_TRIGGER_LEVEL)
813 arm_gic_map_intr(device_t dev, struct intr_map_data *data,
814 struct intr_irqsrc **isrcp)
818 struct arm_gic_softc *sc;
820 error = gic_map_intr(dev, data, &irq, NULL, NULL);
822 sc = device_get_softc(dev);
823 *isrcp = GIC_INTR_ISRC(sc, irq);
829 arm_gic_setup_intr(device_t dev, struct intr_irqsrc *isrc,
830 struct resource *res, struct intr_map_data *data)
832 struct arm_gic_softc *sc = device_get_softc(dev);
833 struct gic_irqsrc *gi = (struct gic_irqsrc *)isrc;
834 enum intr_trigger trig;
835 enum intr_polarity pol;
837 if ((gi->gi_flags & GI_FLAG_MSI) == GI_FLAG_MSI) {
841 KASSERT(pol == INTR_POLARITY_HIGH,
842 ("%s: MSI interrupts must be active-high", __func__));
843 KASSERT(trig == INTR_TRIGGER_EDGE,
844 ("%s: MSI interrupts must be edge triggered", __func__));
845 } else if (data != NULL) {
848 /* Get config for resource. */
849 if (gic_map_intr(dev, data, &irq, &pol, &trig) ||
853 pol = INTR_POLARITY_CONFORM;
854 trig = INTR_TRIGGER_CONFORM;
857 /* Compare config if this is not first setup. */
858 if (isrc->isrc_handlers != 0) {
859 if ((pol != INTR_POLARITY_CONFORM && pol != gi->gi_pol) ||
860 (trig != INTR_TRIGGER_CONFORM && trig != gi->gi_trig))
866 /* For MSI/MSI-X we should have already configured these */
867 if ((gi->gi_flags & GI_FLAG_MSI) == 0) {
868 if (pol == INTR_POLARITY_CONFORM)
869 pol = INTR_POLARITY_LOW; /* just pick some */
870 if (trig == INTR_TRIGGER_CONFORM)
871 trig = INTR_TRIGGER_EDGE; /* just pick some */
876 /* Edge triggered interrupts need an early EOI sent */
877 if (gi->gi_trig == INTR_TRIGGER_EDGE)
878 gi->gi_flags |= GI_FLAG_EARLY_EOI;
882 * XXX - In case that per CPU interrupt is going to be enabled in time
883 * when SMP is already started, we need some IPI call which
884 * enables it on others CPUs. Further, it's more complicated as
885 * pic_enable_source() and pic_disable_source() should act on
886 * per CPU basis only. Thus, it should be solved here somehow.
888 if (isrc->isrc_flags & INTR_ISRCF_PPI)
889 CPU_SET(PCPU_GET(cpuid), &isrc->isrc_cpu);
891 gic_config(sc, gi->gi_irq, gi->gi_trig, gi->gi_pol);
892 arm_gic_bind_intr(dev, isrc);
897 arm_gic_teardown_intr(device_t dev, struct intr_irqsrc *isrc,
898 struct resource *res, struct intr_map_data *data)
900 struct gic_irqsrc *gi = (struct gic_irqsrc *)isrc;
902 if (isrc->isrc_handlers == 0 && (gi->gi_flags & GI_FLAG_MSI) == 0) {
903 gi->gi_pol = INTR_POLARITY_CONFORM;
904 gi->gi_trig = INTR_TRIGGER_CONFORM;
910 arm_gic_enable_intr(device_t dev, struct intr_irqsrc *isrc)
912 struct arm_gic_softc *sc = device_get_softc(dev);
913 struct gic_irqsrc *gi = (struct gic_irqsrc *)isrc;
915 arm_irq_memory_barrier(gi->gi_irq);
916 gic_irq_unmask(sc, gi->gi_irq);
920 arm_gic_disable_intr(device_t dev, struct intr_irqsrc *isrc)
922 struct arm_gic_softc *sc = device_get_softc(dev);
923 struct gic_irqsrc *gi = (struct gic_irqsrc *)isrc;
925 gic_irq_mask(sc, gi->gi_irq);
929 arm_gic_pre_ithread(device_t dev, struct intr_irqsrc *isrc)
931 struct arm_gic_softc *sc = device_get_softc(dev);
932 struct gic_irqsrc *gi = (struct gic_irqsrc *)isrc;
934 arm_gic_disable_intr(dev, isrc);
935 gic_c_write_4(sc, GICC_EOIR, gi->gi_irq);
939 arm_gic_post_ithread(device_t dev, struct intr_irqsrc *isrc)
942 arm_irq_memory_barrier(0);
943 arm_gic_enable_intr(dev, isrc);
947 arm_gic_post_filter(device_t dev, struct intr_irqsrc *isrc)
949 struct arm_gic_softc *sc = device_get_softc(dev);
950 struct gic_irqsrc *gi = (struct gic_irqsrc *)isrc;
952 /* EOI for edge-triggered done earlier. */
953 if ((gi->gi_flags & GI_FLAG_EARLY_EOI) == GI_FLAG_EARLY_EOI)
956 arm_irq_memory_barrier(0);
957 gic_c_write_4(sc, GICC_EOIR, gi->gi_irq);
961 arm_gic_bind_intr(device_t dev, struct intr_irqsrc *isrc)
963 struct arm_gic_softc *sc = device_get_softc(dev);
964 struct gic_irqsrc *gi = (struct gic_irqsrc *)isrc;
966 if (gi->gi_irq < GIC_FIRST_SPI)
969 if (CPU_EMPTY(&isrc->isrc_cpu)) {
970 gic_irq_cpu = intr_irq_next_cpu(gic_irq_cpu, &all_cpus);
971 CPU_SETOF(gic_irq_cpu, &isrc->isrc_cpu);
973 return (gic_bind(sc, gi->gi_irq, &isrc->isrc_cpu));
978 arm_gic_ipi_send(device_t dev, struct intr_irqsrc *isrc, cpuset_t cpus,
981 struct arm_gic_softc *sc = device_get_softc(dev);
982 struct gic_irqsrc *gi = (struct gic_irqsrc *)isrc;
985 for (i = 0; i < MAXCPU; i++)
986 if (CPU_ISSET(i, &cpus))
987 val |= arm_gic_map[i] << GICD_SGI_TARGET_SHIFT;
989 gic_d_write_4(sc, GICD_SGIR, val | gi->gi_irq);
993 arm_gic_ipi_setup(device_t dev, u_int ipi, struct intr_irqsrc **isrcp)
995 struct intr_irqsrc *isrc;
996 struct arm_gic_softc *sc = device_get_softc(dev);
998 if (sgi_first_unused > GIC_LAST_SGI)
1001 isrc = GIC_INTR_ISRC(sc, sgi_first_unused);
1002 sgi_to_ipi[sgi_first_unused++] = ipi;
1004 CPU_SET(PCPU_GET(cpuid), &isrc->isrc_cpu);
1012 arm_gic_alloc_msi(device_t dev, u_int mbi_start, u_int mbi_count, int count,
1013 int maxcount, struct intr_irqsrc **isrc)
1015 struct arm_gic_softc *sc;
1016 int i, irq, end_irq;
1019 KASSERT(powerof2(count), ("%s: bad count", __func__));
1020 KASSERT(powerof2(maxcount), ("%s: bad maxcount", __func__));
1022 sc = device_get_softc(dev);
1024 mtx_lock_spin(&sc->mutex);
1027 for (irq = mbi_start; irq < mbi_start + mbi_count; irq++) {
1028 /* Start on an aligned interrupt */
1029 if ((irq & (maxcount - 1)) != 0)
1032 /* Assume we found a valid range until shown otherwise */
1035 /* Check this range is valid */
1036 for (end_irq = irq; end_irq != irq + count; end_irq++) {
1037 /* No free interrupts */
1038 if (end_irq == mbi_start + mbi_count) {
1043 KASSERT((sc->gic_irqs[end_irq].gi_flags & GI_FLAG_MSI)!= 0,
1044 ("%s: Non-MSI interrupt found", __func__));
1046 /* This is already used */
1047 if ((sc->gic_irqs[end_irq].gi_flags & GI_FLAG_MSI_USED) ==
1057 /* Not enough interrupts were found */
1058 if (!found || irq == mbi_start + mbi_count) {
1059 mtx_unlock_spin(&sc->mutex);
1063 for (i = 0; i < count; i++) {
1064 /* Mark the interrupt as used */
1065 sc->gic_irqs[irq + i].gi_flags |= GI_FLAG_MSI_USED;
1067 mtx_unlock_spin(&sc->mutex);
1069 for (i = 0; i < count; i++)
1070 isrc[i] = (struct intr_irqsrc *)&sc->gic_irqs[irq + i];
1076 arm_gic_release_msi(device_t dev, int count, struct intr_irqsrc **isrc)
1078 struct arm_gic_softc *sc;
1079 struct gic_irqsrc *gi;
1082 sc = device_get_softc(dev);
1084 mtx_lock_spin(&sc->mutex);
1085 for (i = 0; i < count; i++) {
1086 gi = (struct gic_irqsrc *)isrc[i];
1088 KASSERT((gi->gi_flags & GI_FLAG_MSI_USED) == GI_FLAG_MSI_USED,
1089 ("%s: Trying to release an unused MSI-X interrupt",
1092 gi->gi_flags &= ~GI_FLAG_MSI_USED;
1094 mtx_unlock_spin(&sc->mutex);
1100 arm_gic_alloc_msix(device_t dev, u_int mbi_start, u_int mbi_count,
1101 struct intr_irqsrc **isrc)
1103 struct arm_gic_softc *sc;
1106 sc = device_get_softc(dev);
1108 mtx_lock_spin(&sc->mutex);
1109 /* Find an unused interrupt */
1110 for (irq = mbi_start; irq < mbi_start + mbi_count; irq++) {
1111 KASSERT((sc->gic_irqs[irq].gi_flags & GI_FLAG_MSI) != 0,
1112 ("%s: Non-MSI interrupt found", __func__));
1113 if ((sc->gic_irqs[irq].gi_flags & GI_FLAG_MSI_USED) == 0)
1116 /* No free interrupt was found */
1117 if (irq == mbi_start + mbi_count) {
1118 mtx_unlock_spin(&sc->mutex);
1122 /* Mark the interrupt as used */
1123 sc->gic_irqs[irq].gi_flags |= GI_FLAG_MSI_USED;
1124 mtx_unlock_spin(&sc->mutex);
1126 *isrc = (struct intr_irqsrc *)&sc->gic_irqs[irq];
1132 arm_gic_release_msix(device_t dev, struct intr_irqsrc *isrc)
1134 struct arm_gic_softc *sc;
1135 struct gic_irqsrc *gi;
1137 sc = device_get_softc(dev);
1138 gi = (struct gic_irqsrc *)isrc;
1140 KASSERT((gi->gi_flags & GI_FLAG_MSI_USED) == GI_FLAG_MSI_USED,
1141 ("%s: Trying to release an unused MSI-X interrupt", __func__));
1143 mtx_lock_spin(&sc->mutex);
1144 gi->gi_flags &= ~GI_FLAG_MSI_USED;
1145 mtx_unlock_spin(&sc->mutex);
1150 static device_method_t arm_gic_methods[] = {
1152 DEVMETHOD(bus_print_child, arm_gic_print_child),
1153 DEVMETHOD(bus_add_child, bus_generic_add_child),
1154 DEVMETHOD(bus_alloc_resource, arm_gic_alloc_resource),
1155 DEVMETHOD(bus_release_resource, bus_generic_release_resource),
1156 DEVMETHOD(bus_activate_resource,bus_generic_activate_resource),
1157 DEVMETHOD(bus_read_ivar, arm_gic_read_ivar),
1158 DEVMETHOD(bus_write_ivar, arm_gic_write_ivar),
1160 /* Interrupt controller interface */
1161 DEVMETHOD(pic_disable_intr, arm_gic_disable_intr),
1162 DEVMETHOD(pic_enable_intr, arm_gic_enable_intr),
1163 DEVMETHOD(pic_map_intr, arm_gic_map_intr),
1164 DEVMETHOD(pic_setup_intr, arm_gic_setup_intr),
1165 DEVMETHOD(pic_teardown_intr, arm_gic_teardown_intr),
1166 DEVMETHOD(pic_post_filter, arm_gic_post_filter),
1167 DEVMETHOD(pic_post_ithread, arm_gic_post_ithread),
1168 DEVMETHOD(pic_pre_ithread, arm_gic_pre_ithread),
1170 DEVMETHOD(pic_bind_intr, arm_gic_bind_intr),
1171 DEVMETHOD(pic_init_secondary, arm_gic_init_secondary),
1172 DEVMETHOD(pic_ipi_send, arm_gic_ipi_send),
1173 DEVMETHOD(pic_ipi_setup, arm_gic_ipi_setup),
1177 DEVMETHOD(gic_reserve_msi_range, arm_gic_reserve_msi_range),
1178 DEVMETHOD(gic_alloc_msi, arm_gic_alloc_msi),
1179 DEVMETHOD(gic_release_msi, arm_gic_release_msi),
1180 DEVMETHOD(gic_alloc_msix, arm_gic_alloc_msix),
1181 DEVMETHOD(gic_release_msix, arm_gic_release_msix),
1186 DEFINE_CLASS_0(gic, arm_gic_driver, arm_gic_methods,
1187 sizeof(struct arm_gic_softc));
1190 * GICv2m support -- the GICv2 MSI/MSI-X controller.
1193 #define GICV2M_MSI_TYPER 0x008
1194 #define MSI_TYPER_SPI_BASE(x) (((x) >> 16) & 0x3ff)
1195 #define MSI_TYPER_SPI_COUNT(x) (((x) >> 0) & 0x3ff)
1196 #define GICv2M_MSI_SETSPI_NS 0x040
1197 #define GICV2M_MSI_IIDR 0xFCC
1200 arm_gicv2m_attach(device_t dev)
1202 struct arm_gicv2m_softc *sc;
1206 sc = device_get_softc(dev);
1209 sc->sc_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
1211 if (sc->sc_mem == NULL) {
1212 device_printf(dev, "Unable to allocate resources\n");
1216 typer = bus_read_4(sc->sc_mem, GICV2M_MSI_TYPER);
1217 sc->sc_spi_start = MSI_TYPER_SPI_BASE(typer);
1218 sc->sc_spi_count = MSI_TYPER_SPI_COUNT(typer);
1220 /* Reserve these interrupts for MSI/MSI-X use */
1221 GIC_RESERVE_MSI_RANGE(device_get_parent(dev), sc->sc_spi_start,
1224 intr_msi_register(dev, sc->sc_xref);
1227 device_printf(dev, "using spi %u to %u\n", sc->sc_spi_start,
1228 sc->sc_spi_start + sc->sc_spi_count - 1);
1234 arm_gicv2m_alloc_msi(device_t dev, device_t child, int count, int maxcount,
1235 device_t *pic, struct intr_irqsrc **srcs)
1237 struct arm_gicv2m_softc *sc;
1240 sc = device_get_softc(dev);
1241 error = GIC_ALLOC_MSI(device_get_parent(dev), sc->sc_spi_start,
1242 sc->sc_spi_count, count, maxcount, srcs);
1251 arm_gicv2m_release_msi(device_t dev, device_t child, int count,
1252 struct intr_irqsrc **isrc)
1254 return (GIC_RELEASE_MSI(device_get_parent(dev), count, isrc));
1258 arm_gicv2m_alloc_msix(device_t dev, device_t child, device_t *pic,
1259 struct intr_irqsrc **isrcp)
1261 struct arm_gicv2m_softc *sc;
1264 sc = device_get_softc(dev);
1265 error = GIC_ALLOC_MSIX(device_get_parent(dev), sc->sc_spi_start,
1266 sc->sc_spi_count, isrcp);
1275 arm_gicv2m_release_msix(device_t dev, device_t child, struct intr_irqsrc *isrc)
1277 return (GIC_RELEASE_MSIX(device_get_parent(dev), isrc));
1281 arm_gicv2m_map_msi(device_t dev, device_t child, struct intr_irqsrc *isrc,
1282 uint64_t *addr, uint32_t *data)
1284 struct arm_gicv2m_softc *sc = device_get_softc(dev);
1285 struct gic_irqsrc *gi = (struct gic_irqsrc *)isrc;
1287 *addr = vtophys(rman_get_virtual(sc->sc_mem)) + GICv2M_MSI_SETSPI_NS;
1293 static device_method_t arm_gicv2m_methods[] = {
1294 /* Device interface */
1295 DEVMETHOD(device_attach, arm_gicv2m_attach),
1298 DEVMETHOD(msi_alloc_msi, arm_gicv2m_alloc_msi),
1299 DEVMETHOD(msi_release_msi, arm_gicv2m_release_msi),
1300 DEVMETHOD(msi_alloc_msix, arm_gicv2m_alloc_msix),
1301 DEVMETHOD(msi_release_msix, arm_gicv2m_release_msix),
1302 DEVMETHOD(msi_map_msi, arm_gicv2m_map_msi),
1308 DEFINE_CLASS_0(gicv2m, arm_gicv2m_driver, arm_gicv2m_methods,
1309 sizeof(struct arm_gicv2m_softc));