2 * Copyright (c) 2015-2016 The FreeBSD Foundation
4 * This software was developed by Andrew Turner under
5 * the sponsorship of the FreeBSD Foundation.
7 * This software was developed by Semihalf under
8 * the sponsorship of the FreeBSD Foundation.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include "opt_platform.h"
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/bitstring.h>
42 #include <sys/kernel.h>
44 #include <sys/malloc.h>
45 #include <sys/module.h>
49 #include <sys/cpuset.h>
51 #include <sys/mutex.h>
53 #include <sys/interrupt.h>
58 #include <machine/bus.h>
59 #include <machine/cpu.h>
60 #include <machine/intr.h>
63 #include <dev/fdt/fdt_intr.h>
64 #include <dev/ofw/ofw_bus_subr.h>
68 #include <contrib/dev/acpica/include/acpi.h>
69 #include <dev/acpica/acpivar.h>
75 #include <arm/arm/gic_common.h>
76 #include "gic_v3_reg.h"
77 #include "gic_v3_var.h"
79 static bus_print_child_t gic_v3_print_child;
80 static bus_get_domain_t gic_v3_get_domain;
81 static bus_read_ivar_t gic_v3_read_ivar;
83 static pic_disable_intr_t gic_v3_disable_intr;
84 static pic_enable_intr_t gic_v3_enable_intr;
85 static pic_map_intr_t gic_v3_map_intr;
86 static pic_setup_intr_t gic_v3_setup_intr;
87 static pic_teardown_intr_t gic_v3_teardown_intr;
88 static pic_post_filter_t gic_v3_post_filter;
89 static pic_post_ithread_t gic_v3_post_ithread;
90 static pic_pre_ithread_t gic_v3_pre_ithread;
91 static pic_bind_intr_t gic_v3_bind_intr;
93 static pic_init_secondary_t gic_v3_init_secondary;
94 static pic_ipi_send_t gic_v3_ipi_send;
95 static pic_ipi_setup_t gic_v3_ipi_setup;
98 static msi_alloc_msi_t gic_v3_alloc_msi;
99 static msi_release_msi_t gic_v3_release_msi;
100 static msi_alloc_msix_t gic_v3_alloc_msix;
101 static msi_release_msix_t gic_v3_release_msix;
102 static msi_map_msi_t gic_v3_map_msi;
104 static u_int gic_irq_cpu;
106 static u_int sgi_to_ipi[GIC_LAST_SGI - GIC_FIRST_SGI + 1];
107 static u_int sgi_first_unused = GIC_FIRST_SGI;
110 static device_method_t gic_v3_methods[] = {
111 /* Device interface */
112 DEVMETHOD(device_detach, gic_v3_detach),
115 DEVMETHOD(bus_print_child, gic_v3_print_child),
116 DEVMETHOD(bus_get_domain, gic_v3_get_domain),
117 DEVMETHOD(bus_read_ivar, gic_v3_read_ivar),
119 /* Interrupt controller interface */
120 DEVMETHOD(pic_disable_intr, gic_v3_disable_intr),
121 DEVMETHOD(pic_enable_intr, gic_v3_enable_intr),
122 DEVMETHOD(pic_map_intr, gic_v3_map_intr),
123 DEVMETHOD(pic_setup_intr, gic_v3_setup_intr),
124 DEVMETHOD(pic_teardown_intr, gic_v3_teardown_intr),
125 DEVMETHOD(pic_post_filter, gic_v3_post_filter),
126 DEVMETHOD(pic_post_ithread, gic_v3_post_ithread),
127 DEVMETHOD(pic_pre_ithread, gic_v3_pre_ithread),
129 DEVMETHOD(pic_bind_intr, gic_v3_bind_intr),
130 DEVMETHOD(pic_init_secondary, gic_v3_init_secondary),
131 DEVMETHOD(pic_ipi_send, gic_v3_ipi_send),
132 DEVMETHOD(pic_ipi_setup, gic_v3_ipi_setup),
136 DEVMETHOD(msi_alloc_msi, gic_v3_alloc_msi),
137 DEVMETHOD(msi_release_msi, gic_v3_release_msi),
138 DEVMETHOD(msi_alloc_msix, gic_v3_alloc_msix),
139 DEVMETHOD(msi_release_msix, gic_v3_release_msix),
140 DEVMETHOD(msi_map_msi, gic_v3_map_msi),
146 DEFINE_CLASS_0(gic, gic_v3_driver, gic_v3_methods,
147 sizeof(struct gic_v3_softc));
150 * Driver-specific definitions.
152 MALLOC_DEFINE(M_GIC_V3, "GICv3", GIC_V3_DEVSTR);
155 * Helper functions and definitions.
157 /* Destination registers, either Distributor or Re-Distributor */
163 struct gic_v3_irqsrc {
164 struct intr_irqsrc gi_isrc;
166 enum intr_polarity gi_pol;
167 enum intr_trigger gi_trig;
168 #define GI_FLAG_MSI (1 << 1) /* This interrupt source should only */
169 /* be used for MSI/MSI-X interrupts */
170 #define GI_FLAG_MSI_USED (1 << 2) /* This irq is already allocated */
171 /* for a MSI/MSI-X interrupt */
175 /* Helper routines starting with gic_v3_ */
176 static int gic_v3_dist_init(struct gic_v3_softc *);
177 static int gic_v3_redist_alloc(struct gic_v3_softc *);
178 static int gic_v3_redist_find(struct gic_v3_softc *);
179 static int gic_v3_redist_init(struct gic_v3_softc *);
180 static int gic_v3_cpu_init(struct gic_v3_softc *);
181 static void gic_v3_wait_for_rwp(struct gic_v3_softc *, enum gic_v3_xdist);
183 /* A sequence of init functions for primary (boot) CPU */
184 typedef int (*gic_v3_initseq_t) (struct gic_v3_softc *);
185 /* Primary CPU initialization sequence */
186 static gic_v3_initseq_t gic_v3_primary_init[] = {
195 /* Secondary CPU initialization sequence */
196 static gic_v3_initseq_t gic_v3_secondary_init[] = {
204 gic_r_read_4(device_t dev, bus_size_t offset)
206 struct gic_v3_softc *sc;
207 struct resource *rdist;
209 sc = device_get_softc(dev);
210 rdist = &sc->gic_redists.pcpu[PCPU_GET(cpuid)]->res;
211 return (bus_read_4(rdist, offset));
215 gic_r_read_8(device_t dev, bus_size_t offset)
217 struct gic_v3_softc *sc;
218 struct resource *rdist;
220 sc = device_get_softc(dev);
221 rdist = &sc->gic_redists.pcpu[PCPU_GET(cpuid)]->res;
222 return (bus_read_8(rdist, offset));
226 gic_r_write_4(device_t dev, bus_size_t offset, uint32_t val)
228 struct gic_v3_softc *sc;
229 struct resource *rdist;
231 sc = device_get_softc(dev);
232 rdist = &sc->gic_redists.pcpu[PCPU_GET(cpuid)]->res;
233 bus_write_4(rdist, offset, val);
237 gic_r_write_8(device_t dev, bus_size_t offset, uint64_t val)
239 struct gic_v3_softc *sc;
240 struct resource *rdist;
242 sc = device_get_softc(dev);
243 rdist = &sc->gic_redists.pcpu[PCPU_GET(cpuid)]->res;
244 bus_write_8(rdist, offset, val);
251 gic_v3_attach(device_t dev)
253 struct gic_v3_softc *sc;
254 gic_v3_initseq_t *init_func;
262 sc = device_get_softc(dev);
263 sc->gic_registered = FALSE;
267 /* Initialize mutex */
268 mtx_init(&sc->gic_mtx, "GICv3 lock", NULL, MTX_SPIN);
271 * Allocate array of struct resource.
272 * One entry for Distributor and all remaining for Re-Distributor.
274 sc->gic_res = malloc(
275 sizeof(*sc->gic_res) * (sc->gic_redists.nregions + 1),
278 /* Now allocate corresponding resources */
279 for (i = 0, rid = 0; i < (sc->gic_redists.nregions + 1); i++, rid++) {
280 sc->gic_res[rid] = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
282 if (sc->gic_res[rid] == NULL)
287 * Distributor interface
289 sc->gic_dist = sc->gic_res[0];
292 * Re-Dristributor interface
294 /* Allocate space under region descriptions */
295 sc->gic_redists.regions = malloc(
296 sizeof(*sc->gic_redists.regions) * sc->gic_redists.nregions,
299 /* Fill-up bus_space information for each region. */
300 for (i = 0, rid = 1; i < sc->gic_redists.nregions; i++, rid++)
301 sc->gic_redists.regions[i] = sc->gic_res[rid];
303 /* Get the number of supported SPI interrupts */
304 typer = gic_d_read(sc, 4, GICD_TYPER);
305 sc->gic_nirqs = GICD_TYPER_I_NUM(typer);
306 if (sc->gic_nirqs > GIC_I_NUM_MAX)
307 sc->gic_nirqs = GIC_I_NUM_MAX;
309 sc->gic_irqs = malloc(sizeof(*sc->gic_irqs) * sc->gic_nirqs,
310 M_GIC_V3, M_WAITOK | M_ZERO);
311 name = device_get_nameunit(dev);
312 for (irq = 0; irq < sc->gic_nirqs; irq++) {
313 struct intr_irqsrc *isrc;
315 sc->gic_irqs[irq].gi_irq = irq;
316 sc->gic_irqs[irq].gi_pol = INTR_POLARITY_CONFORM;
317 sc->gic_irqs[irq].gi_trig = INTR_TRIGGER_CONFORM;
319 isrc = &sc->gic_irqs[irq].gi_isrc;
320 if (irq <= GIC_LAST_SGI) {
321 err = intr_isrc_register(isrc, sc->dev,
322 INTR_ISRCF_IPI, "%s,i%u", name, irq - GIC_FIRST_SGI);
323 } else if (irq <= GIC_LAST_PPI) {
324 err = intr_isrc_register(isrc, sc->dev,
325 INTR_ISRCF_PPI, "%s,p%u", name, irq - GIC_FIRST_PPI);
327 err = intr_isrc_register(isrc, sc->dev, 0,
328 "%s,s%u", name, irq - GIC_FIRST_SPI);
331 /* XXX call intr_isrc_deregister() */
332 free(sc->gic_irqs, M_DEVBUF);
337 if (sc->gic_mbi_start > 0) {
338 /* Reserve these interrupts for MSI/MSI-X use */
339 for (irq = sc->gic_mbi_start; irq <= sc->gic_mbi_end; irq++) {
340 sc->gic_irqs[irq].gi_pol = INTR_POLARITY_HIGH;
341 sc->gic_irqs[irq].gi_trig = INTR_TRIGGER_EDGE;
342 sc->gic_irqs[irq].gi_flags |= GI_FLAG_MSI;
345 mtx_init(&sc->gic_mbi_mtx, "GICv3 mbi lock", NULL, MTX_DEF);
348 device_printf(dev, "using spi %u to %u\n", sc->gic_mbi_start,
354 * Read the Peripheral ID2 register. This is an implementation
355 * defined register, but seems to be implemented in all GICv3
356 * parts and Linux expects it to be there.
358 sc->gic_pidr2 = gic_d_read(sc, 4, GICD_PIDR2);
360 /* Get the number of supported interrupt identifier bits */
361 sc->gic_idbits = GICD_TYPER_IDBITS(typer);
364 device_printf(dev, "SPIs: %u, IDs: %u\n",
365 sc->gic_nirqs, (1 << sc->gic_idbits) - 1);
368 /* Train init sequence for boot CPU */
369 for (init_func = gic_v3_primary_init; *init_func != NULL; init_func++) {
370 err = (*init_func)(sc);
379 gic_v3_detach(device_t dev)
381 struct gic_v3_softc *sc;
385 sc = device_get_softc(dev);
387 if (device_is_attached(dev)) {
389 * XXX: We should probably deregister PIC
391 if (sc->gic_registered)
392 panic("Trying to detach registered PIC");
394 for (rid = 0; rid < (sc->gic_redists.nregions + 1); rid++)
395 bus_release_resource(dev, SYS_RES_MEMORY, rid, sc->gic_res[rid]);
397 for (i = 0; i <= mp_maxid; i++)
398 free(sc->gic_redists.pcpu[i], M_GIC_V3);
400 free(sc->gic_res, M_GIC_V3);
401 free(sc->gic_redists.regions, M_GIC_V3);
407 gic_v3_print_child(device_t bus, device_t child)
409 struct resource_list *rl;
412 rl = BUS_GET_RESOURCE_LIST(bus, child);
413 KASSERT(rl != NULL, ("%s: No resource list", __func__));
414 retval += bus_print_child_header(bus, child);
415 retval += resource_list_print_type(rl, "mem", SYS_RES_MEMORY, "%#jx");
416 retval += bus_print_child_footer(bus, child);
422 gic_v3_get_domain(device_t dev, device_t child, int *domain)
424 struct gic_v3_devinfo *di;
426 di = device_get_ivars(child);
427 if (di->gic_domain < 0)
430 *domain = di->gic_domain;
435 gic_v3_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
437 struct gic_v3_softc *sc;
439 sc = device_get_softc(dev);
442 case GICV3_IVAR_NIRQS:
443 *result = (intr_nirq - sc->gic_nirqs) / sc->gic_nchildren;
445 case GICV3_IVAR_REDIST:
446 *result = (uintptr_t)sc->gic_redists.pcpu[PCPU_GET(cpuid)];
448 case GIC_IVAR_HW_REV:
450 GICR_PIDR2_ARCH(sc->gic_pidr2) == GICR_PIDR2_ARCH_GICv3 ||
451 GICR_PIDR2_ARCH(sc->gic_pidr2) == GICR_PIDR2_ARCH_GICv4,
452 ("gic_v3_read_ivar: Invalid GIC architecture: %d (%.08X)",
453 GICR_PIDR2_ARCH(sc->gic_pidr2), sc->gic_pidr2));
454 *result = GICR_PIDR2_ARCH(sc->gic_pidr2);
457 KASSERT(sc->gic_bus != GIC_BUS_UNKNOWN,
458 ("gic_v3_read_ivar: Unknown bus type"));
459 KASSERT(sc->gic_bus <= GIC_BUS_MAX,
460 ("gic_v3_read_ivar: Invalid bus type %u", sc->gic_bus));
461 *result = sc->gic_bus;
469 arm_gic_v3_intr(void *arg)
471 struct gic_v3_softc *sc = arg;
472 struct gic_v3_irqsrc *gi;
473 struct intr_pic *pic;
475 struct trapframe *tf;
480 if (CPU_MATCH_ERRATA_CAVIUM_THUNDERX_1_1) {
482 * Hardware: Cavium ThunderX
483 * Chip revision: Pass 1.0 (early version)
484 * Pass 1.1 (production)
485 * ERRATUM: 22978, 23154
488 "nop;nop;nop;nop;nop;nop;nop;nop; \n"
489 "mrs %0, ICC_IAR1_EL1 \n"
490 "nop;nop;nop;nop; \n"
492 : "=&r" (active_irq));
494 active_irq = gic_icc_read(IAR1);
497 if (active_irq >= GIC_FIRST_LPI) {
498 intr_child_irq_handler(pic, active_irq);
502 if (__predict_false(active_irq >= sc->gic_nirqs))
503 return (FILTER_HANDLED);
505 tf = curthread->td_intr_frame;
506 gi = &sc->gic_irqs[active_irq];
507 if (active_irq <= GIC_LAST_SGI) {
508 /* Call EOI for all IPI before dispatch. */
509 gic_icc_write(EOIR1, (uint64_t)active_irq);
511 intr_ipi_dispatch(sgi_to_ipi[gi->gi_irq], tf);
513 device_printf(sc->dev, "SGI %ju on UP system detected\n",
514 (uintmax_t)(active_irq - GIC_FIRST_SGI));
516 } else if (active_irq >= GIC_FIRST_PPI &&
517 active_irq <= GIC_LAST_SPI) {
518 if (gi->gi_trig == INTR_TRIGGER_EDGE)
519 gic_icc_write(EOIR1, gi->gi_irq);
521 if (intr_isrc_dispatch(&gi->gi_isrc, tf) != 0) {
522 if (gi->gi_trig != INTR_TRIGGER_EDGE)
523 gic_icc_write(EOIR1, gi->gi_irq);
524 gic_v3_disable_intr(sc->dev, &gi->gi_isrc);
525 device_printf(sc->dev,
526 "Stray irq %lu disabled\n", active_irq);
534 gic_map_fdt(device_t dev, u_int ncells, pcell_t *cells, u_int *irqp,
535 enum intr_polarity *polp, enum intr_trigger *trigp)
543 * The 1st cell is the interrupt type:
546 * The 2nd cell contains the interrupt number:
549 * The 3rd cell is the flags, encoded as follows:
550 * bits[3:0] trigger type and level flags
552 * 2 = edge triggered (PPI only)
553 * 4 = level-sensitive
554 * 8 = level-sensitive (PPI only)
558 irq = GIC_FIRST_SPI + cells[1];
559 /* SPI irq is checked later. */
562 irq = GIC_FIRST_PPI + cells[1];
563 if (irq > GIC_LAST_PPI) {
564 device_printf(dev, "unsupported PPI interrupt "
565 "number %u\n", cells[1]);
570 device_printf(dev, "unsupported interrupt type "
571 "configuration %u\n", cells[0]);
575 switch (cells[2] & FDT_INTR_MASK) {
576 case FDT_INTR_EDGE_RISING:
577 *trigp = INTR_TRIGGER_EDGE;
578 *polp = INTR_POLARITY_HIGH;
580 case FDT_INTR_EDGE_FALLING:
581 *trigp = INTR_TRIGGER_EDGE;
582 *polp = INTR_POLARITY_LOW;
584 case FDT_INTR_LEVEL_HIGH:
585 *trigp = INTR_TRIGGER_LEVEL;
586 *polp = INTR_POLARITY_HIGH;
588 case FDT_INTR_LEVEL_LOW:
589 *trigp = INTR_TRIGGER_LEVEL;
590 *polp = INTR_POLARITY_LOW;
593 device_printf(dev, "unsupported trigger/polarity "
594 "configuration 0x%02x\n", cells[2]);
598 /* Check the interrupt is valid */
599 if (irq >= GIC_FIRST_SPI && *polp != INTR_POLARITY_HIGH)
608 gic_map_msi(device_t dev, struct intr_map_data_msi *msi_data, u_int *irqp,
609 enum intr_polarity *polp, enum intr_trigger *trigp)
611 struct gic_v3_irqsrc *gi;
614 gi = (struct gic_v3_irqsrc *)msi_data->isrc;
620 /* MSI/MSI-X interrupts are always edge triggered with high polarity */
621 *polp = INTR_POLARITY_HIGH;
622 *trigp = INTR_TRIGGER_EDGE;
628 do_gic_v3_map_intr(device_t dev, struct intr_map_data *data, u_int *irqp,
629 enum intr_polarity *polp, enum intr_trigger *trigp)
631 struct gic_v3_softc *sc;
632 enum intr_polarity pol;
633 enum intr_trigger trig;
634 struct intr_map_data_msi *dam;
636 struct intr_map_data_fdt *daf;
639 struct intr_map_data_acpi *daa;
643 sc = device_get_softc(dev);
645 switch (data->type) {
647 case INTR_MAP_DATA_FDT:
648 daf = (struct intr_map_data_fdt *)data;
649 if (gic_map_fdt(dev, daf->ncells, daf->cells, &irq, &pol,
655 case INTR_MAP_DATA_ACPI:
656 daa = (struct intr_map_data_acpi *)data;
662 case INTR_MAP_DATA_MSI:
664 dam = (struct intr_map_data_msi *)data;
665 if (gic_map_msi(dev, dam, &irq, &pol, &trig) != 0)
672 if (irq >= sc->gic_nirqs)
675 case INTR_POLARITY_CONFORM:
676 case INTR_POLARITY_LOW:
677 case INTR_POLARITY_HIGH:
683 case INTR_TRIGGER_CONFORM:
684 case INTR_TRIGGER_EDGE:
685 case INTR_TRIGGER_LEVEL:
700 gic_v3_map_intr(device_t dev, struct intr_map_data *data,
701 struct intr_irqsrc **isrcp)
703 struct gic_v3_softc *sc;
707 error = do_gic_v3_map_intr(dev, data, &irq, NULL, NULL);
709 sc = device_get_softc(dev);
710 *isrcp = GIC_INTR_ISRC(sc, irq);
716 gic_v3_setup_intr(device_t dev, struct intr_irqsrc *isrc,
717 struct resource *res, struct intr_map_data *data)
719 struct gic_v3_softc *sc = device_get_softc(dev);
720 struct gic_v3_irqsrc *gi = (struct gic_v3_irqsrc *)isrc;
721 enum intr_trigger trig;
722 enum intr_polarity pol;
730 error = do_gic_v3_map_intr(dev, data, &irq, &pol, &trig);
734 if (gi->gi_irq != irq || pol == INTR_POLARITY_CONFORM ||
735 trig == INTR_TRIGGER_CONFORM)
738 /* Compare config if this is not first setup. */
739 if (isrc->isrc_handlers != 0) {
740 if (pol != gi->gi_pol || trig != gi->gi_trig)
746 /* For MSI/MSI-X we should have already configured these */
747 if ((gi->gi_flags & GI_FLAG_MSI) == 0) {
753 * XXX - In case that per CPU interrupt is going to be enabled in time
754 * when SMP is already started, we need some IPI call which
755 * enables it on others CPUs. Further, it's more complicated as
756 * pic_enable_source() and pic_disable_source() should act on
757 * per CPU basis only. Thus, it should be solved here somehow.
759 if (isrc->isrc_flags & INTR_ISRCF_PPI)
760 CPU_SET(PCPU_GET(cpuid), &isrc->isrc_cpu);
762 if (irq >= GIC_FIRST_PPI && irq <= GIC_LAST_SPI) {
763 mtx_lock_spin(&sc->gic_mtx);
765 /* Set the trigger and polarity */
766 if (irq <= GIC_LAST_PPI)
767 reg = gic_r_read(sc, 4,
768 GICR_SGI_BASE_SIZE + GICD_ICFGR(irq));
770 reg = gic_d_read(sc, 4, GICD_ICFGR(irq));
771 if (trig == INTR_TRIGGER_LEVEL)
772 reg &= ~(2 << ((irq % 16) * 2));
774 reg |= 2 << ((irq % 16) * 2);
776 if (irq <= GIC_LAST_PPI) {
778 GICR_SGI_BASE_SIZE + GICD_ICFGR(irq), reg);
779 gic_v3_wait_for_rwp(sc, REDIST);
781 gic_d_write(sc, 4, GICD_ICFGR(irq), reg);
782 gic_v3_wait_for_rwp(sc, DIST);
785 mtx_unlock_spin(&sc->gic_mtx);
787 gic_v3_bind_intr(dev, isrc);
794 gic_v3_teardown_intr(device_t dev, struct intr_irqsrc *isrc,
795 struct resource *res, struct intr_map_data *data)
797 struct gic_v3_irqsrc *gi = (struct gic_v3_irqsrc *)isrc;
799 if (isrc->isrc_handlers == 0 && (gi->gi_flags & GI_FLAG_MSI) == 0) {
800 gi->gi_pol = INTR_POLARITY_CONFORM;
801 gi->gi_trig = INTR_TRIGGER_CONFORM;
808 gic_v3_disable_intr(device_t dev, struct intr_irqsrc *isrc)
810 struct gic_v3_softc *sc;
811 struct gic_v3_irqsrc *gi;
814 sc = device_get_softc(dev);
815 gi = (struct gic_v3_irqsrc *)isrc;
818 if (irq <= GIC_LAST_PPI) {
819 /* SGIs and PPIs in corresponding Re-Distributor */
820 gic_r_write(sc, 4, GICR_SGI_BASE_SIZE + GICD_ICENABLER(irq),
822 gic_v3_wait_for_rwp(sc, REDIST);
823 } else if (irq >= GIC_FIRST_SPI && irq <= GIC_LAST_SPI) {
824 /* SPIs in distributor */
825 gic_d_write(sc, 4, GICD_ICENABLER(irq), GICD_I_MASK(irq));
826 gic_v3_wait_for_rwp(sc, DIST);
828 panic("%s: Unsupported IRQ %u", __func__, irq);
832 gic_v3_enable_intr(device_t dev, struct intr_irqsrc *isrc)
834 struct gic_v3_softc *sc;
835 struct gic_v3_irqsrc *gi;
838 sc = device_get_softc(dev);
839 gi = (struct gic_v3_irqsrc *)isrc;
842 if (irq <= GIC_LAST_PPI) {
843 /* SGIs and PPIs in corresponding Re-Distributor */
844 gic_r_write(sc, 4, GICR_SGI_BASE_SIZE + GICD_ISENABLER(irq),
846 gic_v3_wait_for_rwp(sc, REDIST);
847 } else if (irq >= GIC_FIRST_SPI && irq <= GIC_LAST_SPI) {
848 /* SPIs in distributor */
849 gic_d_write(sc, 4, GICD_ISENABLER(irq), GICD_I_MASK(irq));
850 gic_v3_wait_for_rwp(sc, DIST);
852 panic("%s: Unsupported IRQ %u", __func__, irq);
856 gic_v3_pre_ithread(device_t dev, struct intr_irqsrc *isrc)
858 struct gic_v3_irqsrc *gi = (struct gic_v3_irqsrc *)isrc;
860 gic_v3_disable_intr(dev, isrc);
861 gic_icc_write(EOIR1, gi->gi_irq);
865 gic_v3_post_ithread(device_t dev, struct intr_irqsrc *isrc)
868 gic_v3_enable_intr(dev, isrc);
872 gic_v3_post_filter(device_t dev, struct intr_irqsrc *isrc)
874 struct gic_v3_irqsrc *gi = (struct gic_v3_irqsrc *)isrc;
876 if (gi->gi_trig == INTR_TRIGGER_EDGE)
879 gic_icc_write(EOIR1, gi->gi_irq);
883 gic_v3_bind_intr(device_t dev, struct intr_irqsrc *isrc)
885 struct gic_v3_softc *sc;
886 struct gic_v3_irqsrc *gi;
889 gi = (struct gic_v3_irqsrc *)isrc;
890 if (gi->gi_irq <= GIC_LAST_PPI)
893 KASSERT(gi->gi_irq >= GIC_FIRST_SPI && gi->gi_irq <= GIC_LAST_SPI,
894 ("%s: Attempting to bind an invalid IRQ", __func__));
896 sc = device_get_softc(dev);
898 if (CPU_EMPTY(&isrc->isrc_cpu)) {
899 gic_irq_cpu = intr_irq_next_cpu(gic_irq_cpu, &all_cpus);
900 CPU_SETOF(gic_irq_cpu, &isrc->isrc_cpu);
901 gic_d_write(sc, 8, GICD_IROUTER(gi->gi_irq),
902 CPU_AFFINITY(gic_irq_cpu));
905 * We can only bind to a single CPU so select
906 * the first CPU found.
908 cpu = CPU_FFS(&isrc->isrc_cpu) - 1;
909 gic_d_write(sc, 8, GICD_IROUTER(gi->gi_irq), CPU_AFFINITY(cpu));
917 gic_v3_init_secondary(device_t dev)
920 struct gic_v3_softc *sc;
921 gic_v3_initseq_t *init_func;
922 struct intr_irqsrc *isrc;
926 sc = device_get_softc(dev);
927 cpu = PCPU_GET(cpuid);
929 /* Train init sequence for boot CPU */
930 for (init_func = gic_v3_secondary_init; *init_func != NULL;
932 err = (*init_func)(sc);
935 "Could not initialize GIC for CPU%u\n", cpu);
940 /* Unmask attached SGI interrupts. */
941 for (irq = GIC_FIRST_SGI; irq <= GIC_LAST_SGI; irq++) {
942 isrc = GIC_INTR_ISRC(sc, irq);
943 if (intr_isrc_init_on_cpu(isrc, cpu))
944 gic_v3_enable_intr(dev, isrc);
947 /* Unmask attached PPI interrupts. */
948 for (irq = GIC_FIRST_PPI; irq <= GIC_LAST_PPI; irq++) {
949 isrc = GIC_INTR_ISRC(sc, irq);
950 if (intr_isrc_init_on_cpu(isrc, cpu))
951 gic_v3_enable_intr(dev, isrc);
954 for (i = 0; i < sc->gic_nchildren; i++) {
955 child = sc->gic_children[i];
956 PIC_INIT_SECONDARY(child);
961 gic_v3_ipi_send(device_t dev, struct intr_irqsrc *isrc, cpuset_t cpus,
964 struct gic_v3_irqsrc *gi = (struct gic_v3_irqsrc *)isrc;
965 uint64_t aff, val, irq;
968 #define GIC_AFF_MASK (CPU_AFF3_MASK | CPU_AFF2_MASK | CPU_AFF1_MASK)
969 #define GIC_AFFINITY(i) (CPU_AFFINITY(i) & GIC_AFF_MASK)
970 aff = GIC_AFFINITY(0);
974 /* Iterate through all CPUs in set */
975 for (i = 0; i <= mp_maxid; i++) {
976 /* Move to the next affinity group */
977 if (aff != GIC_AFFINITY(i)) {
980 gic_icc_write(SGI1R, val);
983 aff = GIC_AFFINITY(i);
986 /* Send the IPI to this cpu */
987 if (CPU_ISSET(i, &cpus)) {
988 #define ICC_SGI1R_AFFINITY(aff) \
989 (((uint64_t)CPU_AFF3(aff) << ICC_SGI1R_EL1_AFF3_SHIFT) | \
990 ((uint64_t)CPU_AFF2(aff) << ICC_SGI1R_EL1_AFF2_SHIFT) | \
991 ((uint64_t)CPU_AFF1(aff) << ICC_SGI1R_EL1_AFF1_SHIFT))
992 /* Set the affinity when the first at this level */
994 val = ICC_SGI1R_AFFINITY(aff) |
995 irq << ICC_SGI1R_EL1_SGIID_SHIFT;
996 /* Set the bit to send the IPI to te CPU */
997 val |= 1 << CPU_AFF0(CPU_AFFINITY(i));
1001 /* Send the IPI to the last cpu affinity group */
1003 gic_icc_write(SGI1R, val);
1009 gic_v3_ipi_setup(device_t dev, u_int ipi, struct intr_irqsrc **isrcp)
1011 struct intr_irqsrc *isrc;
1012 struct gic_v3_softc *sc = device_get_softc(dev);
1014 if (sgi_first_unused > GIC_LAST_SGI)
1017 isrc = GIC_INTR_ISRC(sc, sgi_first_unused);
1018 sgi_to_ipi[sgi_first_unused++] = ipi;
1020 CPU_SET(PCPU_GET(cpuid), &isrc->isrc_cpu);
1031 gic_v3_wait_for_rwp(struct gic_v3_softc *sc, enum gic_v3_xdist xdist)
1033 struct resource *res;
1035 size_t us_left = 1000000;
1037 cpuid = PCPU_GET(cpuid);
1044 res = &sc->gic_redists.pcpu[cpuid]->res;
1047 KASSERT(0, ("%s: Attempt to wait for unknown RWP", __func__));
1051 while ((bus_read_4(res, GICD_CTLR) & GICD_CTLR_RWP) != 0) {
1054 panic("GICD Register write pending for too long");
1058 /* CPU interface. */
1059 static __inline void
1060 gic_v3_cpu_priority(uint64_t mask)
1063 /* Set prority mask */
1064 gic_icc_write(PMR, mask & ICC_PMR_EL1_PRIO_MASK);
1068 gic_v3_cpu_enable_sre(struct gic_v3_softc *sc)
1073 cpuid = PCPU_GET(cpuid);
1075 * Set the SRE bit to enable access to GIC CPU interface
1076 * via system registers.
1078 sre = READ_SPECIALREG(icc_sre_el1);
1079 sre |= ICC_SRE_EL1_SRE;
1080 WRITE_SPECIALREG(icc_sre_el1, sre);
1083 * Now ensure that the bit is set.
1085 sre = READ_SPECIALREG(icc_sre_el1);
1086 if ((sre & ICC_SRE_EL1_SRE) == 0) {
1087 /* We are done. This was disabled in EL2 */
1088 device_printf(sc->dev, "ERROR: CPU%u cannot enable CPU interface "
1089 "via system registers\n", cpuid);
1091 } else if (bootverbose) {
1092 device_printf(sc->dev,
1093 "CPU%u enabled CPU interface via system registers\n",
1101 gic_v3_cpu_init(struct gic_v3_softc *sc)
1105 /* Enable access to CPU interface via system registers */
1106 err = gic_v3_cpu_enable_sre(sc);
1109 /* Priority mask to minimum - accept all interrupts */
1110 gic_v3_cpu_priority(GIC_PRIORITY_MIN);
1111 /* Disable EOI mode */
1112 gic_icc_clear(CTLR, ICC_CTLR_EL1_EOIMODE);
1113 /* Enable group 1 (insecure) interrups */
1114 gic_icc_set(IGRPEN1, ICC_IGRPEN0_EL1_EN);
1121 gic_v3_dist_init(struct gic_v3_softc *sc)
1127 * 1. Disable the Distributor
1129 gic_d_write(sc, 4, GICD_CTLR, 0);
1130 gic_v3_wait_for_rwp(sc, DIST);
1133 * 2. Configure the Distributor
1135 /* Set all SPIs to be Group 1 Non-secure */
1136 for (i = GIC_FIRST_SPI; i < sc->gic_nirqs; i += GICD_I_PER_IGROUPRn)
1137 gic_d_write(sc, 4, GICD_IGROUPR(i), 0xFFFFFFFF);
1139 /* Set all global interrupts to be level triggered, active low. */
1140 for (i = GIC_FIRST_SPI; i < sc->gic_nirqs; i += GICD_I_PER_ICFGRn)
1141 gic_d_write(sc, 4, GICD_ICFGR(i), 0x00000000);
1143 /* Set priority to all shared interrupts */
1144 for (i = GIC_FIRST_SPI;
1145 i < sc->gic_nirqs; i += GICD_I_PER_IPRIORITYn) {
1146 /* Set highest priority */
1147 gic_d_write(sc, 4, GICD_IPRIORITYR(i), GIC_PRIORITY_MAX);
1151 * Disable all interrupts. Leave PPI and SGIs as they are enabled in
1152 * Re-Distributor registers.
1154 for (i = GIC_FIRST_SPI; i < sc->gic_nirqs; i += GICD_I_PER_ISENABLERn)
1155 gic_d_write(sc, 4, GICD_ICENABLER(i), 0xFFFFFFFF);
1157 gic_v3_wait_for_rwp(sc, DIST);
1160 * 3. Enable Distributor
1162 /* Enable Distributor with ARE, Group 1 */
1163 gic_d_write(sc, 4, GICD_CTLR, GICD_CTLR_ARE_NS | GICD_CTLR_G1A |
1167 * 4. Route all interrupts to boot CPU.
1169 aff = CPU_AFFINITY(0);
1170 for (i = GIC_FIRST_SPI; i < sc->gic_nirqs; i++)
1171 gic_d_write(sc, 8, GICD_IROUTER(i), aff);
1176 /* Re-Distributor */
1178 gic_v3_redist_alloc(struct gic_v3_softc *sc)
1182 /* Allocate struct resource for all CPU's Re-Distributor registers */
1183 for (cpuid = 0; cpuid <= mp_maxid; cpuid++)
1184 if (CPU_ISSET(cpuid, &all_cpus) != 0)
1185 sc->gic_redists.pcpu[cpuid] =
1186 malloc(sizeof(*sc->gic_redists.pcpu[0]),
1187 M_GIC_V3, M_WAITOK);
1189 sc->gic_redists.pcpu[cpuid] = NULL;
1194 gic_v3_redist_find(struct gic_v3_softc *sc)
1196 struct resource r_res;
1197 bus_space_handle_t r_bsh;
1204 cpuid = PCPU_GET(cpuid);
1206 aff = CPU_AFFINITY(cpuid);
1207 /* Affinity in format for comparison with typer */
1208 aff = (CPU_AFF3(aff) << 24) | (CPU_AFF2(aff) << 16) |
1209 (CPU_AFF1(aff) << 8) | CPU_AFF0(aff);
1212 device_printf(sc->dev,
1213 "Start searching for Re-Distributor\n");
1215 /* Iterate through Re-Distributor regions */
1216 for (i = 0; i < sc->gic_redists.nregions; i++) {
1217 /* Take a copy of the region's resource */
1218 r_res = *sc->gic_redists.regions[i];
1219 r_bsh = rman_get_bushandle(&r_res);
1221 pidr2 = bus_read_4(&r_res, GICR_PIDR2);
1222 switch (GICR_PIDR2_ARCH(pidr2)) {
1223 case GICR_PIDR2_ARCH_GICv3: /* fall through */
1224 case GICR_PIDR2_ARCH_GICv4:
1227 device_printf(sc->dev,
1228 "No Re-Distributor found for CPU%u\n", cpuid);
1233 typer = bus_read_8(&r_res, GICR_TYPER);
1234 if ((typer >> GICR_TYPER_AFF_SHIFT) == aff) {
1235 KASSERT(sc->gic_redists.pcpu[cpuid] != NULL,
1236 ("Invalid pointer to per-CPU redistributor"));
1237 /* Copy res contents to its final destination */
1238 sc->gic_redists.pcpu[cpuid]->res = r_res;
1239 sc->gic_redists.pcpu[cpuid]->lpi_enabled = false;
1241 device_printf(sc->dev,
1242 "CPU%u Re-Distributor has been found\n",
1248 r_bsh += (GICR_RD_BASE_SIZE + GICR_SGI_BASE_SIZE);
1249 if ((typer & GICR_TYPER_VLPIS) != 0) {
1251 (GICR_VLPI_BASE_SIZE + GICR_RESERVED_SIZE);
1254 rman_set_bushandle(&r_res, r_bsh);
1255 } while ((typer & GICR_TYPER_LAST) == 0);
1258 device_printf(sc->dev, "No Re-Distributor found for CPU%u\n", cpuid);
1263 gic_v3_redist_wake(struct gic_v3_softc *sc)
1266 size_t us_left = 1000000;
1268 waker = gic_r_read(sc, 4, GICR_WAKER);
1269 /* Wake up Re-Distributor for this CPU */
1270 waker &= ~GICR_WAKER_PS;
1271 gic_r_write(sc, 4, GICR_WAKER, waker);
1273 * When clearing ProcessorSleep bit it is required to wait for
1274 * ChildrenAsleep to become zero following the processor power-on.
1276 while ((gic_r_read(sc, 4, GICR_WAKER) & GICR_WAKER_CA) != 0) {
1278 if (us_left-- == 0) {
1279 panic("Could not wake Re-Distributor for CPU%u",
1285 device_printf(sc->dev, "CPU%u Re-Distributor woke up\n",
1293 gic_v3_redist_init(struct gic_v3_softc *sc)
1298 err = gic_v3_redist_find(sc);
1302 err = gic_v3_redist_wake(sc);
1306 /* Configure SGIs and PPIs to be Group1 Non-secure */
1307 gic_r_write(sc, 4, GICR_SGI_BASE_SIZE + GICR_IGROUPR0,
1311 gic_r_write(sc, 4, GICR_SGI_BASE_SIZE + GICR_ICENABLER0,
1312 GICR_I_ENABLER_PPI_MASK);
1314 gic_r_write(sc, 4, GICR_SGI_BASE_SIZE + GICR_ISENABLER0,
1315 GICR_I_ENABLER_SGI_MASK);
1317 /* Set priority for SGIs and PPIs */
1318 for (i = 0; i <= GIC_LAST_PPI; i += GICR_I_PER_IPRIORITYn) {
1319 gic_r_write(sc, 4, GICR_SGI_BASE_SIZE + GICD_IPRIORITYR(i),
1323 gic_v3_wait_for_rwp(sc, REDIST);
1329 * SPI-mapped Message Based Interrupts -- a GICv3 MSI/MSI-X controller.
1333 gic_v3_alloc_msi(device_t dev, device_t child, int count, int maxcount,
1334 device_t *pic, struct intr_irqsrc **srcs)
1336 struct gic_v3_softc *sc;
1337 int i, irq, end_irq;
1340 KASSERT(powerof2(count), ("%s: bad count", __func__));
1341 KASSERT(powerof2(maxcount), ("%s: bad maxcount", __func__));
1343 sc = device_get_softc(dev);
1345 mtx_lock(&sc->gic_mbi_mtx);
1348 for (irq = sc->gic_mbi_start; irq < sc->gic_mbi_end; irq++) {
1349 /* Start on an aligned interrupt */
1350 if ((irq & (maxcount - 1)) != 0)
1353 /* Assume we found a valid range until shown otherwise */
1356 /* Check this range is valid */
1357 for (end_irq = irq; end_irq != irq + count; end_irq++) {
1358 /* No free interrupts */
1359 if (end_irq == sc->gic_mbi_end) {
1364 KASSERT((sc->gic_irqs[end_irq].gi_flags & GI_FLAG_MSI)!= 0,
1365 ("%s: Non-MSI interrupt found", __func__));
1367 /* This is already used */
1368 if ((sc->gic_irqs[end_irq].gi_flags & GI_FLAG_MSI_USED) ==
1378 /* Not enough interrupts were found */
1379 if (!found || irq == sc->gic_mbi_end) {
1380 mtx_unlock(&sc->gic_mbi_mtx);
1384 for (i = 0; i < count; i++) {
1385 /* Mark the interrupt as used */
1386 sc->gic_irqs[irq + i].gi_flags |= GI_FLAG_MSI_USED;
1388 mtx_unlock(&sc->gic_mbi_mtx);
1390 for (i = 0; i < count; i++)
1391 srcs[i] = (struct intr_irqsrc *)&sc->gic_irqs[irq + i];
1398 gic_v3_release_msi(device_t dev, device_t child, int count,
1399 struct intr_irqsrc **isrc)
1401 struct gic_v3_softc *sc;
1402 struct gic_v3_irqsrc *gi;
1405 sc = device_get_softc(dev);
1407 mtx_lock(&sc->gic_mbi_mtx);
1408 for (i = 0; i < count; i++) {
1409 gi = (struct gic_v3_irqsrc *)isrc[i];
1411 KASSERT((gi->gi_flags & GI_FLAG_MSI_USED) == GI_FLAG_MSI_USED,
1412 ("%s: Trying to release an unused MSI-X interrupt",
1415 gi->gi_flags &= ~GI_FLAG_MSI_USED;
1417 mtx_unlock(&sc->gic_mbi_mtx);
1423 gic_v3_alloc_msix(device_t dev, device_t child, device_t *pic,
1424 struct intr_irqsrc **isrcp)
1426 struct gic_v3_softc *sc;
1429 sc = device_get_softc(dev);
1431 mtx_lock(&sc->gic_mbi_mtx);
1432 /* Find an unused interrupt */
1433 for (irq = sc->gic_mbi_start; irq < sc->gic_mbi_end; irq++) {
1434 KASSERT((sc->gic_irqs[irq].gi_flags & GI_FLAG_MSI) != 0,
1435 ("%s: Non-MSI interrupt found", __func__));
1436 if ((sc->gic_irqs[irq].gi_flags & GI_FLAG_MSI_USED) == 0)
1439 /* No free interrupt was found */
1440 if (irq == sc->gic_mbi_end) {
1441 mtx_unlock(&sc->gic_mbi_mtx);
1445 /* Mark the interrupt as used */
1446 sc->gic_irqs[irq].gi_flags |= GI_FLAG_MSI_USED;
1447 mtx_unlock(&sc->gic_mbi_mtx);
1449 *isrcp = (struct intr_irqsrc *)&sc->gic_irqs[irq];
1456 gic_v3_release_msix(device_t dev, device_t child, struct intr_irqsrc *isrc)
1458 struct gic_v3_softc *sc;
1459 struct gic_v3_irqsrc *gi;
1461 sc = device_get_softc(dev);
1462 gi = (struct gic_v3_irqsrc *)isrc;
1464 KASSERT((gi->gi_flags & GI_FLAG_MSI_USED) == GI_FLAG_MSI_USED,
1465 ("%s: Trying to release an unused MSI-X interrupt", __func__));
1467 mtx_lock(&sc->gic_mbi_mtx);
1468 gi->gi_flags &= ~GI_FLAG_MSI_USED;
1469 mtx_unlock(&sc->gic_mbi_mtx);
1475 gic_v3_map_msi(device_t dev, device_t child, struct intr_irqsrc *isrc,
1476 uint64_t *addr, uint32_t *data)
1478 struct gic_v3_softc *sc = device_get_softc(dev);
1479 struct gic_v3_irqsrc *gi = (struct gic_v3_irqsrc *)isrc;
1481 *addr = vtophys(rman_get_virtual(sc->gic_dist)) + GICD_SETSPI_NSR;