Home | History | Annotate | Line # | Download | only in cortex
gicv3.c revision 1.13.4.3
      1  1.13.4.3    martin /* $NetBSD: gicv3.c,v 1.13.4.3 2020/04/08 14:07:28 martin Exp $ */
      2  1.13.4.2  christos 
      3  1.13.4.2  christos /*-
      4  1.13.4.2  christos  * Copyright (c) 2018 Jared McNeill <jmcneill (at) invisible.ca>
      5  1.13.4.2  christos  * All rights reserved.
      6  1.13.4.2  christos  *
      7  1.13.4.2  christos  * Redistribution and use in source and binary forms, with or without
      8  1.13.4.2  christos  * modification, are permitted provided that the following conditions
      9  1.13.4.2  christos  * are met:
     10  1.13.4.2  christos  * 1. Redistributions of source code must retain the above copyright
     11  1.13.4.2  christos  *    notice, this list of conditions and the following disclaimer.
     12  1.13.4.2  christos  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.13.4.2  christos  *    notice, this list of conditions and the following disclaimer in the
     14  1.13.4.2  christos  *    documentation and/or other materials provided with the distribution.
     15  1.13.4.2  christos  *
     16  1.13.4.2  christos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  1.13.4.2  christos  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  1.13.4.2  christos  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  1.13.4.2  christos  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  1.13.4.2  christos  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21  1.13.4.2  christos  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22  1.13.4.2  christos  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23  1.13.4.2  christos  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24  1.13.4.2  christos  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  1.13.4.2  christos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  1.13.4.2  christos  * SUCH DAMAGE.
     27  1.13.4.2  christos  */
     28  1.13.4.2  christos 
     29  1.13.4.2  christos #include "opt_multiprocessor.h"
     30  1.13.4.2  christos 
     31  1.13.4.2  christos #define	_INTR_PRIVATE
     32  1.13.4.2  christos 
     33  1.13.4.2  christos #include <sys/cdefs.h>
     34  1.13.4.3    martin __KERNEL_RCSID(0, "$NetBSD: gicv3.c,v 1.13.4.3 2020/04/08 14:07:28 martin Exp $");
     35  1.13.4.2  christos 
     36  1.13.4.2  christos #include <sys/param.h>
     37  1.13.4.2  christos #include <sys/kernel.h>
     38  1.13.4.2  christos #include <sys/bus.h>
     39  1.13.4.2  christos #include <sys/device.h>
     40  1.13.4.2  christos #include <sys/intr.h>
     41  1.13.4.2  christos #include <sys/systm.h>
     42  1.13.4.2  christos #include <sys/cpu.h>
     43  1.13.4.3    martin #include <sys/vmem.h>
     44  1.13.4.2  christos 
     45  1.13.4.2  christos #include <arm/locore.h>
     46  1.13.4.2  christos #include <arm/armreg.h>
     47  1.13.4.2  christos 
     48  1.13.4.2  christos #include <arm/cortex/gicv3.h>
     49  1.13.4.2  christos #include <arm/cortex/gic_reg.h>
     50  1.13.4.2  christos 
     51  1.13.4.2  christos #define	PICTOSOFTC(pic)	\
     52  1.13.4.2  christos 	((void *)((uintptr_t)(pic) - offsetof(struct gicv3_softc, sc_pic)))
     53  1.13.4.2  christos #define	LPITOSOFTC(lpi) \
     54  1.13.4.2  christos 	((void *)((uintptr_t)(lpi) - offsetof(struct gicv3_softc, sc_lpi)))
     55  1.13.4.2  christos 
     56  1.13.4.2  christos #define	IPL_TO_PRIORITY(ipl)	((IPL_HIGH - (ipl)) << 4)
     57  1.13.4.2  christos 
     58  1.13.4.2  christos static struct gicv3_softc *gicv3_softc;
     59  1.13.4.2  christos 
     60  1.13.4.2  christos static inline uint32_t
     61  1.13.4.2  christos gicd_read_4(struct gicv3_softc *sc, bus_size_t reg)
     62  1.13.4.2  christos {
     63  1.13.4.2  christos 	return bus_space_read_4(sc->sc_bst, sc->sc_bsh_d, reg);
     64  1.13.4.2  christos }
     65  1.13.4.2  christos 
     66  1.13.4.2  christos static inline void
     67  1.13.4.2  christos gicd_write_4(struct gicv3_softc *sc, bus_size_t reg, uint32_t val)
     68  1.13.4.2  christos {
     69  1.13.4.2  christos 	bus_space_write_4(sc->sc_bst, sc->sc_bsh_d, reg, val);
     70  1.13.4.2  christos }
     71  1.13.4.2  christos 
     72  1.13.4.2  christos static inline uint64_t
     73  1.13.4.2  christos gicd_read_8(struct gicv3_softc *sc, bus_size_t reg)
     74  1.13.4.2  christos {
     75  1.13.4.2  christos 	return bus_space_read_8(sc->sc_bst, sc->sc_bsh_d, reg);
     76  1.13.4.2  christos }
     77  1.13.4.2  christos 
     78  1.13.4.2  christos static inline void
     79  1.13.4.2  christos gicd_write_8(struct gicv3_softc *sc, bus_size_t reg, uint64_t val)
     80  1.13.4.2  christos {
     81  1.13.4.2  christos 	bus_space_write_8(sc->sc_bst, sc->sc_bsh_d, reg, val);
     82  1.13.4.2  christos }
     83  1.13.4.2  christos 
     84  1.13.4.2  christos static inline uint32_t
     85  1.13.4.2  christos gicr_read_4(struct gicv3_softc *sc, u_int index, bus_size_t reg)
     86  1.13.4.2  christos {
     87  1.13.4.2  christos 	KASSERT(index < sc->sc_bsh_r_count);
     88  1.13.4.2  christos 	return bus_space_read_4(sc->sc_bst, sc->sc_bsh_r[index], reg);
     89  1.13.4.2  christos }
     90  1.13.4.2  christos 
     91  1.13.4.2  christos static inline void
     92  1.13.4.2  christos gicr_write_4(struct gicv3_softc *sc, u_int index, bus_size_t reg, uint32_t val)
     93  1.13.4.2  christos {
     94  1.13.4.2  christos 	KASSERT(index < sc->sc_bsh_r_count);
     95  1.13.4.2  christos 	bus_space_write_4(sc->sc_bst, sc->sc_bsh_r[index], reg, val);
     96  1.13.4.2  christos }
     97  1.13.4.2  christos 
     98  1.13.4.2  christos static inline uint64_t
     99  1.13.4.2  christos gicr_read_8(struct gicv3_softc *sc, u_int index, bus_size_t reg)
    100  1.13.4.2  christos {
    101  1.13.4.2  christos 	KASSERT(index < sc->sc_bsh_r_count);
    102  1.13.4.2  christos 	return bus_space_read_8(sc->sc_bst, sc->sc_bsh_r[index], reg);
    103  1.13.4.2  christos }
    104  1.13.4.2  christos 
    105  1.13.4.2  christos static inline void
    106  1.13.4.2  christos gicr_write_8(struct gicv3_softc *sc, u_int index, bus_size_t reg, uint64_t val)
    107  1.13.4.2  christos {
    108  1.13.4.2  christos 	KASSERT(index < sc->sc_bsh_r_count);
    109  1.13.4.2  christos 	bus_space_write_8(sc->sc_bst, sc->sc_bsh_r[index], reg, val);
    110  1.13.4.2  christos }
    111  1.13.4.2  christos 
    112  1.13.4.2  christos static void
    113  1.13.4.2  christos gicv3_unblock_irqs(struct pic_softc *pic, size_t irqbase, uint32_t mask)
    114  1.13.4.2  christos {
    115  1.13.4.2  christos 	struct gicv3_softc * const sc = PICTOSOFTC(pic);
    116  1.13.4.2  christos 	struct cpu_info * const ci = curcpu();
    117  1.13.4.2  christos 	const u_int group = irqbase / 32;
    118  1.13.4.2  christos 
    119  1.13.4.2  christos 	if (group == 0) {
    120  1.13.4.2  christos 		sc->sc_enabled_sgippi |= mask;
    121  1.13.4.2  christos 		gicr_write_4(sc, ci->ci_gic_redist, GICR_ISENABLER0, mask);
    122  1.13.4.2  christos 		while (gicr_read_4(sc, ci->ci_gic_redist, GICR_CTLR) & GICR_CTLR_RWP)
    123  1.13.4.2  christos 			;
    124  1.13.4.2  christos 	} else {
    125  1.13.4.2  christos 		gicd_write_4(sc, GICD_ISENABLERn(group), mask);
    126  1.13.4.2  christos 		while (gicd_read_4(sc, GICD_CTRL) & GICD_CTRL_RWP)
    127  1.13.4.2  christos 			;
    128  1.13.4.2  christos 	}
    129  1.13.4.2  christos }
    130  1.13.4.2  christos 
    131  1.13.4.2  christos static void
    132  1.13.4.2  christos gicv3_block_irqs(struct pic_softc *pic, size_t irqbase, uint32_t mask)
    133  1.13.4.2  christos {
    134  1.13.4.2  christos 	struct gicv3_softc * const sc = PICTOSOFTC(pic);
    135  1.13.4.2  christos 	struct cpu_info * const ci = curcpu();
    136  1.13.4.2  christos 	const u_int group = irqbase / 32;
    137  1.13.4.2  christos 
    138  1.13.4.2  christos 	if (group == 0) {
    139  1.13.4.2  christos 		sc->sc_enabled_sgippi &= ~mask;
    140  1.13.4.2  christos 		gicr_write_4(sc, ci->ci_gic_redist, GICR_ICENABLER0, mask);
    141  1.13.4.2  christos 		while (gicr_read_4(sc, ci->ci_gic_redist, GICR_CTLR) & GICR_CTLR_RWP)
    142  1.13.4.2  christos 			;
    143  1.13.4.2  christos 	} else {
    144  1.13.4.2  christos 		gicd_write_4(sc, GICD_ICENABLERn(group), mask);
    145  1.13.4.2  christos 		while (gicd_read_4(sc, GICD_CTRL) & GICD_CTRL_RWP)
    146  1.13.4.2  christos 			;
    147  1.13.4.2  christos 	}
    148  1.13.4.2  christos }
    149  1.13.4.2  christos 
    150  1.13.4.2  christos static void
    151  1.13.4.2  christos gicv3_establish_irq(struct pic_softc *pic, struct intrsource *is)
    152  1.13.4.2  christos {
    153  1.13.4.2  christos 	struct gicv3_softc * const sc = PICTOSOFTC(pic);
    154  1.13.4.2  christos 	const u_int group = is->is_irq / 32;
    155  1.13.4.2  christos 	uint32_t ipriority, icfg;
    156  1.13.4.2  christos 	uint64_t irouter;
    157  1.13.4.2  christos 	u_int n;
    158  1.13.4.2  christos 
    159  1.13.4.2  christos 	const u_int ipriority_val = 0x80 | IPL_TO_PRIORITY(is->is_ipl);
    160  1.13.4.2  christos 	const u_int ipriority_shift = (is->is_irq & 0x3) * 8;
    161  1.13.4.2  christos 	const u_int icfg_shift = (is->is_irq & 0xf) * 2;
    162  1.13.4.2  christos 
    163  1.13.4.2  christos 	if (group == 0) {
    164  1.13.4.2  christos 		/* SGIs and PPIs are always MP-safe */
    165  1.13.4.2  christos 		is->is_mpsafe = true;
    166  1.13.4.2  christos 
    167  1.13.4.2  christos 		/* Update interrupt configuration and priority on all redistributors */
    168  1.13.4.2  christos 		for (n = 0; n < sc->sc_bsh_r_count; n++) {
    169  1.13.4.2  christos 			icfg = gicr_read_4(sc, n, GICR_ICFGRn(is->is_irq / 16));
    170  1.13.4.2  christos 			if (is->is_type == IST_LEVEL)
    171  1.13.4.2  christos 				icfg &= ~(0x2 << icfg_shift);
    172  1.13.4.2  christos 			if (is->is_type == IST_EDGE)
    173  1.13.4.2  christos 				icfg |= (0x2 << icfg_shift);
    174  1.13.4.2  christos 			gicr_write_4(sc, n, GICR_ICFGRn(is->is_irq / 16), icfg);
    175  1.13.4.2  christos 
    176  1.13.4.2  christos 			ipriority = gicr_read_4(sc, n, GICR_IPRIORITYRn(is->is_irq / 4));
    177  1.13.4.2  christos 			ipriority &= ~(0xff << ipriority_shift);
    178  1.13.4.2  christos 			ipriority |= (ipriority_val << ipriority_shift);
    179  1.13.4.2  christos 			gicr_write_4(sc, n, GICR_IPRIORITYRn(is->is_irq / 4), ipriority);
    180  1.13.4.2  christos 		}
    181  1.13.4.2  christos 	} else {
    182  1.13.4.2  christos 		if (is->is_mpsafe) {
    183  1.13.4.2  christos 			/* Route MP-safe interrupts to all participating PEs */
    184  1.13.4.2  christos 			irouter = GICD_IROUTER_Interrupt_Routing_mode;
    185  1.13.4.2  christos 		} else {
    186  1.13.4.2  christos 			/* Route non-MP-safe interrupts to the primary PE only */
    187  1.13.4.2  christos 			irouter = sc->sc_irouter[0];
    188  1.13.4.2  christos 		}
    189  1.13.4.2  christos 		gicd_write_8(sc, GICD_IROUTER(is->is_irq), irouter);
    190  1.13.4.2  christos 
    191  1.13.4.2  christos 		/* Update interrupt configuration */
    192  1.13.4.2  christos 		icfg = gicd_read_4(sc, GICD_ICFGRn(is->is_irq / 16));
    193  1.13.4.2  christos 		if (is->is_type == IST_LEVEL)
    194  1.13.4.2  christos 			icfg &= ~(0x2 << icfg_shift);
    195  1.13.4.2  christos 		if (is->is_type == IST_EDGE)
    196  1.13.4.2  christos 			icfg |= (0x2 << icfg_shift);
    197  1.13.4.2  christos 		gicd_write_4(sc, GICD_ICFGRn(is->is_irq / 16), icfg);
    198  1.13.4.2  christos 
    199  1.13.4.2  christos 		/* Update interrupt priority */
    200  1.13.4.2  christos 		ipriority = gicd_read_4(sc, GICD_IPRIORITYRn(is->is_irq / 4));
    201  1.13.4.2  christos 		ipriority &= ~(0xff << ipriority_shift);
    202  1.13.4.2  christos 		ipriority |= (ipriority_val << ipriority_shift);
    203  1.13.4.2  christos 		gicd_write_4(sc, GICD_IPRIORITYRn(is->is_irq / 4), ipriority);
    204  1.13.4.2  christos 	}
    205  1.13.4.2  christos }
    206  1.13.4.2  christos 
    207  1.13.4.2  christos static void
    208  1.13.4.2  christos gicv3_set_priority(struct pic_softc *pic, int ipl)
    209  1.13.4.2  christos {
    210  1.13.4.2  christos 	icc_pmr_write(IPL_TO_PRIORITY(ipl) << 1);
    211  1.13.4.2  christos }
    212  1.13.4.2  christos 
    213  1.13.4.2  christos static void
    214  1.13.4.2  christos gicv3_dist_enable(struct gicv3_softc *sc)
    215  1.13.4.2  christos {
    216  1.13.4.2  christos 	uint32_t gicd_ctrl;
    217  1.13.4.2  christos 	u_int n;
    218  1.13.4.2  christos 
    219  1.13.4.2  christos 	/* Disable the distributor */
    220  1.13.4.2  christos 	gicd_write_4(sc, GICD_CTRL, 0);
    221  1.13.4.2  christos 
    222  1.13.4.2  christos 	/* Wait for register write to complete */
    223  1.13.4.2  christos 	while (gicd_read_4(sc, GICD_CTRL) & GICD_CTRL_RWP)
    224  1.13.4.2  christos 		;
    225  1.13.4.2  christos 
    226  1.13.4.2  christos 	/* Clear all INTID enable bits */
    227  1.13.4.2  christos 	for (n = 32; n < sc->sc_pic.pic_maxsources; n += 32)
    228  1.13.4.2  christos 		gicd_write_4(sc, GICD_ICENABLERn(n / 32), ~0);
    229  1.13.4.2  christos 
    230  1.13.4.2  christos 	/* Set default priorities to lowest */
    231  1.13.4.2  christos 	for (n = 32; n < sc->sc_pic.pic_maxsources; n += 4)
    232  1.13.4.2  christos 		gicd_write_4(sc, GICD_IPRIORITYRn(n / 4), ~0);
    233  1.13.4.2  christos 
    234  1.13.4.2  christos 	/* Set all interrupts to G1NS */
    235  1.13.4.2  christos 	for (n = 32; n < sc->sc_pic.pic_maxsources; n += 32) {
    236  1.13.4.2  christos 		gicd_write_4(sc, GICD_IGROUPRn(n / 32), ~0);
    237  1.13.4.2  christos 		gicd_write_4(sc, GICD_IGRPMODRn(n / 32), 0);
    238  1.13.4.2  christos 	}
    239  1.13.4.2  christos 
    240  1.13.4.2  christos 	/* Set all interrupts level-sensitive by default */
    241  1.13.4.2  christos 	for (n = 32; n < sc->sc_pic.pic_maxsources; n += 16)
    242  1.13.4.2  christos 		gicd_write_4(sc, GICD_ICFGRn(n / 16), 0);
    243  1.13.4.2  christos 
    244  1.13.4.2  christos 	/* Wait for register writes to complete */
    245  1.13.4.2  christos 	while (gicd_read_4(sc, GICD_CTRL) & GICD_CTRL_RWP)
    246  1.13.4.2  christos 		;
    247  1.13.4.2  christos 
    248  1.13.4.2  christos 	/* Enable Affinity routing and G1NS interrupts */
    249  1.13.4.2  christos 	gicd_ctrl = GICD_CTRL_EnableGrp1A | GICD_CTRL_Enable | GICD_CTRL_ARE_NS;
    250  1.13.4.2  christos 	gicd_write_4(sc, GICD_CTRL, gicd_ctrl);
    251  1.13.4.2  christos }
    252  1.13.4.2  christos 
    253  1.13.4.2  christos static void
    254  1.13.4.2  christos gicv3_redist_enable(struct gicv3_softc *sc, struct cpu_info *ci)
    255  1.13.4.2  christos {
    256  1.13.4.2  christos 	uint32_t icfg;
    257  1.13.4.2  christos 	u_int n, o;
    258  1.13.4.2  christos 
    259  1.13.4.2  christos 	/* Clear INTID enable bits */
    260  1.13.4.2  christos 	gicr_write_4(sc, ci->ci_gic_redist, GICR_ICENABLER0, ~0);
    261  1.13.4.2  christos 
    262  1.13.4.2  christos 	/* Wait for register write to complete */
    263  1.13.4.2  christos 	while (gicr_read_4(sc, ci->ci_gic_redist, GICR_CTLR) & GICR_CTLR_RWP)
    264  1.13.4.2  christos 		;
    265  1.13.4.2  christos 
    266  1.13.4.2  christos 	/* Set default priorities */
    267  1.13.4.2  christos 	for (n = 0; n < 32; n += 4) {
    268  1.13.4.2  christos 		uint32_t priority = 0;
    269  1.13.4.2  christos 		size_t byte_shift = 0;
    270  1.13.4.2  christos 		for (o = 0; o < 4; o++, byte_shift += 8) {
    271  1.13.4.2  christos 			struct intrsource * const is = sc->sc_pic.pic_sources[n + o];
    272  1.13.4.2  christos 			if (is == NULL)
    273  1.13.4.2  christos 				priority |= 0xff << byte_shift;
    274  1.13.4.2  christos 			else {
    275  1.13.4.2  christos 				const u_int ipriority_val = 0x80 | IPL_TO_PRIORITY(is->is_ipl);
    276  1.13.4.2  christos 				priority |= ipriority_val << byte_shift;
    277  1.13.4.2  christos 			}
    278  1.13.4.2  christos 		}
    279  1.13.4.2  christos 		gicr_write_4(sc, ci->ci_gic_redist, GICR_IPRIORITYRn(n / 4), priority);
    280  1.13.4.2  christos 	}
    281  1.13.4.2  christos 
    282  1.13.4.2  christos 	/* Set all interrupts to G1NS */
    283  1.13.4.2  christos 	gicr_write_4(sc, ci->ci_gic_redist, GICR_IGROUPR0, ~0);
    284  1.13.4.2  christos 	gicr_write_4(sc, ci->ci_gic_redist, GICR_IGRPMODR0, 0);
    285  1.13.4.2  christos 
    286  1.13.4.2  christos 	/* Restore PPI configs */
    287  1.13.4.2  christos 	for (n = 0, icfg = 0; n < 16; n++) {
    288  1.13.4.2  christos 		struct intrsource * const is = sc->sc_pic.pic_sources[16 + n];
    289  1.13.4.2  christos 		if (is != NULL && is->is_type == IST_EDGE)
    290  1.13.4.2  christos 			icfg |= (0x2 << (n * 2));
    291  1.13.4.2  christos 	}
    292  1.13.4.2  christos 	gicr_write_4(sc, ci->ci_gic_redist, GICR_ICFGRn(1), icfg);
    293  1.13.4.2  christos 
    294  1.13.4.2  christos 	/* Restore current enable bits */
    295  1.13.4.2  christos 	gicr_write_4(sc, ci->ci_gic_redist, GICR_ISENABLER0, sc->sc_enabled_sgippi);
    296  1.13.4.2  christos 
    297  1.13.4.2  christos 	/* Wait for register write to complete */
    298  1.13.4.2  christos 	while (gicr_read_4(sc, ci->ci_gic_redist, GICR_CTLR) & GICR_CTLR_RWP)
    299  1.13.4.2  christos 		;
    300  1.13.4.2  christos }
    301  1.13.4.2  christos 
    302  1.13.4.2  christos static uint64_t
    303  1.13.4.2  christos gicv3_cpu_identity(void)
    304  1.13.4.2  christos {
    305  1.13.4.2  christos 	u_int aff3, aff2, aff1, aff0;
    306  1.13.4.2  christos 
    307  1.13.4.2  christos #ifdef __aarch64__
    308  1.13.4.2  christos 	const register_t mpidr = reg_mpidr_el1_read();
    309  1.13.4.2  christos 	aff0 = __SHIFTOUT(mpidr, MPIDR_AFF0);
    310  1.13.4.2  christos 	aff1 = __SHIFTOUT(mpidr, MPIDR_AFF1);
    311  1.13.4.2  christos 	aff2 = __SHIFTOUT(mpidr, MPIDR_AFF2);
    312  1.13.4.2  christos 	aff3 = __SHIFTOUT(mpidr, MPIDR_AFF3);
    313  1.13.4.2  christos #else
    314  1.13.4.2  christos 	const register_t mpidr = armreg_mpidr_read();
    315  1.13.4.2  christos 	aff0 = __SHIFTOUT(mpidr, MPIDR_AFF0);
    316  1.13.4.2  christos 	aff1 = __SHIFTOUT(mpidr, MPIDR_AFF1);
    317  1.13.4.2  christos 	aff2 = __SHIFTOUT(mpidr, MPIDR_AFF2);
    318  1.13.4.2  christos 	aff3 = 0;
    319  1.13.4.2  christos #endif
    320  1.13.4.2  christos 
    321  1.13.4.2  christos 	return __SHIFTIN(aff0, GICR_TYPER_Affinity_Value_Aff0) |
    322  1.13.4.2  christos 	       __SHIFTIN(aff1, GICR_TYPER_Affinity_Value_Aff1) |
    323  1.13.4.2  christos 	       __SHIFTIN(aff2, GICR_TYPER_Affinity_Value_Aff2) |
    324  1.13.4.2  christos 	       __SHIFTIN(aff3, GICR_TYPER_Affinity_Value_Aff3);
    325  1.13.4.2  christos }
    326  1.13.4.2  christos 
    327  1.13.4.2  christos static u_int
    328  1.13.4.2  christos gicv3_find_redist(struct gicv3_softc *sc)
    329  1.13.4.2  christos {
    330  1.13.4.2  christos 	uint64_t gicr_typer;
    331  1.13.4.2  christos 	u_int n;
    332  1.13.4.2  christos 
    333  1.13.4.2  christos 	const uint64_t cpu_identity = gicv3_cpu_identity();
    334  1.13.4.2  christos 
    335  1.13.4.2  christos 	for (n = 0; n < sc->sc_bsh_r_count; n++) {
    336  1.13.4.2  christos 		gicr_typer = gicr_read_8(sc, n, GICR_TYPER);
    337  1.13.4.2  christos 		if ((gicr_typer & GICR_TYPER_Affinity_Value) == cpu_identity)
    338  1.13.4.2  christos 			return n;
    339  1.13.4.2  christos 	}
    340  1.13.4.2  christos 
    341  1.13.4.2  christos 	const u_int aff0 = __SHIFTOUT(cpu_identity, GICR_TYPER_Affinity_Value_Aff0);
    342  1.13.4.2  christos 	const u_int aff1 = __SHIFTOUT(cpu_identity, GICR_TYPER_Affinity_Value_Aff1);
    343  1.13.4.2  christos 	const u_int aff2 = __SHIFTOUT(cpu_identity, GICR_TYPER_Affinity_Value_Aff2);
    344  1.13.4.2  christos 	const u_int aff3 = __SHIFTOUT(cpu_identity, GICR_TYPER_Affinity_Value_Aff3);
    345  1.13.4.2  christos 
    346  1.13.4.2  christos 	panic("%s: could not find GICv3 redistributor for cpu %d.%d.%d.%d",
    347  1.13.4.2  christos 	    cpu_name(curcpu()), aff3, aff2, aff1, aff0);
    348  1.13.4.2  christos }
    349  1.13.4.2  christos 
    350  1.13.4.2  christos static uint64_t
    351  1.13.4.2  christos gicv3_sgir(struct gicv3_softc *sc)
    352  1.13.4.2  christos {
    353  1.13.4.3    martin 	const uint64_t cpu_identity = gicv3_cpu_identity();
    354  1.13.4.2  christos 
    355  1.13.4.2  christos 	const u_int aff0 = __SHIFTOUT(cpu_identity, GICR_TYPER_Affinity_Value_Aff0);
    356  1.13.4.2  christos 	const u_int aff1 = __SHIFTOUT(cpu_identity, GICR_TYPER_Affinity_Value_Aff1);
    357  1.13.4.2  christos 	const u_int aff2 = __SHIFTOUT(cpu_identity, GICR_TYPER_Affinity_Value_Aff2);
    358  1.13.4.2  christos 	const u_int aff3 = __SHIFTOUT(cpu_identity, GICR_TYPER_Affinity_Value_Aff3);
    359  1.13.4.2  christos 
    360  1.13.4.2  christos 	return __SHIFTIN(__BIT(aff0), ICC_SGIR_EL1_TargetList) |
    361  1.13.4.2  christos 	       __SHIFTIN(aff1, ICC_SGIR_EL1_Aff1) |
    362  1.13.4.2  christos 	       __SHIFTIN(aff2, ICC_SGIR_EL1_Aff2) |
    363  1.13.4.3    martin 	       __SHIFTIN(aff3, ICC_SGIR_EL1_Aff3);
    364  1.13.4.2  christos }
    365  1.13.4.2  christos 
    366  1.13.4.2  christos static void
    367  1.13.4.2  christos gicv3_cpu_init(struct pic_softc *pic, struct cpu_info *ci)
    368  1.13.4.2  christos {
    369  1.13.4.2  christos 	struct gicv3_softc * const sc = PICTOSOFTC(pic);
    370  1.13.4.2  christos 	uint32_t icc_sre, icc_ctlr, gicr_waker;
    371  1.13.4.2  christos 
    372  1.13.4.2  christos 	ci->ci_gic_redist = gicv3_find_redist(sc);
    373  1.13.4.2  christos 	ci->ci_gic_sgir = gicv3_sgir(sc);
    374  1.13.4.2  christos 
    375  1.13.4.2  christos 	/* Store route to CPU for SPIs */
    376  1.13.4.2  christos 	const uint64_t cpu_identity = gicv3_cpu_identity();
    377  1.13.4.2  christos 	const u_int aff0 = __SHIFTOUT(cpu_identity, GICR_TYPER_Affinity_Value_Aff0);
    378  1.13.4.2  christos 	const u_int aff1 = __SHIFTOUT(cpu_identity, GICR_TYPER_Affinity_Value_Aff1);
    379  1.13.4.2  christos 	const u_int aff2 = __SHIFTOUT(cpu_identity, GICR_TYPER_Affinity_Value_Aff2);
    380  1.13.4.2  christos 	const u_int aff3 = __SHIFTOUT(cpu_identity, GICR_TYPER_Affinity_Value_Aff3);
    381  1.13.4.2  christos 	sc->sc_irouter[cpu_index(ci)] =
    382  1.13.4.2  christos 	    __SHIFTIN(aff0, GICD_IROUTER_Aff0) |
    383  1.13.4.2  christos 	    __SHIFTIN(aff1, GICD_IROUTER_Aff1) |
    384  1.13.4.2  christos 	    __SHIFTIN(aff2, GICD_IROUTER_Aff2) |
    385  1.13.4.2  christos 	    __SHIFTIN(aff3, GICD_IROUTER_Aff3);
    386  1.13.4.2  christos 
    387  1.13.4.2  christos 	/* Enable System register access and disable IRQ/FIQ bypass */
    388  1.13.4.2  christos 	icc_sre = ICC_SRE_EL1_SRE | ICC_SRE_EL1_DFB | ICC_SRE_EL1_DIB;
    389  1.13.4.2  christos 	icc_sre_write(icc_sre);
    390  1.13.4.2  christos 
    391  1.13.4.2  christos 	/* Mark the connected PE as being awake */
    392  1.13.4.2  christos 	gicr_waker = gicr_read_4(sc, ci->ci_gic_redist, GICR_WAKER);
    393  1.13.4.2  christos 	gicr_waker &= ~GICR_WAKER_ProcessorSleep;
    394  1.13.4.2  christos 	gicr_write_4(sc, ci->ci_gic_redist, GICR_WAKER, gicr_waker);
    395  1.13.4.2  christos 	while (gicr_read_4(sc, ci->ci_gic_redist, GICR_WAKER) & GICR_WAKER_ChildrenAsleep)
    396  1.13.4.2  christos 		;
    397  1.13.4.2  christos 
    398  1.13.4.2  christos 	/* Set initial priority mask */
    399  1.13.4.2  christos 	gicv3_set_priority(pic, IPL_HIGH);
    400  1.13.4.2  christos 
    401  1.13.4.2  christos 	/* Set the binary point field to the minimum value */
    402  1.13.4.2  christos 	icc_bpr1_write(0);
    403  1.13.4.2  christos 
    404  1.13.4.2  christos 	/* Enable group 1 interrupt signaling */
    405  1.13.4.2  christos 	icc_igrpen1_write(ICC_IGRPEN_EL1_Enable);
    406  1.13.4.2  christos 
    407  1.13.4.2  christos 	/* Set EOI mode */
    408  1.13.4.2  christos 	icc_ctlr = icc_ctlr_read();
    409  1.13.4.2  christos 	icc_ctlr &= ~ICC_CTLR_EL1_EOImode;
    410  1.13.4.2  christos 	icc_ctlr_write(icc_ctlr);
    411  1.13.4.2  christos 
    412  1.13.4.2  christos 	/* Enable redistributor */
    413  1.13.4.2  christos 	gicv3_redist_enable(sc, ci);
    414  1.13.4.2  christos 
    415  1.13.4.2  christos 	/* Allow IRQ exceptions */
    416  1.13.4.2  christos 	cpsie(I32_bit);
    417  1.13.4.2  christos }
    418  1.13.4.2  christos 
    419  1.13.4.2  christos #ifdef MULTIPROCESSOR
    420  1.13.4.2  christos static void
    421  1.13.4.2  christos gicv3_ipi_send(struct pic_softc *pic, const kcpuset_t *kcp, u_long ipi)
    422  1.13.4.2  christos {
    423  1.13.4.2  christos 	CPU_INFO_ITERATOR cii;
    424  1.13.4.2  christos 	struct cpu_info *ci;
    425  1.13.4.2  christos 	uint64_t intid, aff, targets;
    426  1.13.4.2  christos 
    427  1.13.4.2  christos 	intid = __SHIFTIN(ipi, ICC_SGIR_EL1_INTID);
    428  1.13.4.2  christos 	if (kcp == NULL) {
    429  1.13.4.2  christos 		/* Interrupts routed to all PEs, excluding "self" */
    430  1.13.4.2  christos 		if (ncpu == 1)
    431  1.13.4.2  christos 			return;
    432  1.13.4.2  christos 		icc_sgi1r_write(intid | ICC_SGIR_EL1_IRM);
    433  1.13.4.2  christos 	} else {
    434  1.13.4.2  christos 		/* Interrupts routed to specific PEs */
    435  1.13.4.2  christos 		aff = 0;
    436  1.13.4.2  christos 		targets = 0;
    437  1.13.4.2  christos 		for (CPU_INFO_FOREACH(cii, ci)) {
    438  1.13.4.2  christos 			if (!kcpuset_isset(kcp, cpu_index(ci)))
    439  1.13.4.2  christos 				continue;
    440  1.13.4.2  christos 			if ((ci->ci_gic_sgir & ICC_SGIR_EL1_Aff) != aff) {
    441  1.13.4.2  christos 				if (targets != 0) {
    442  1.13.4.2  christos 					icc_sgi1r_write(intid | aff | targets);
    443  1.13.4.2  christos 					targets = 0;
    444  1.13.4.2  christos 				}
    445  1.13.4.2  christos 				aff = (ci->ci_gic_sgir & ICC_SGIR_EL1_Aff);
    446  1.13.4.2  christos 			}
    447  1.13.4.2  christos 			targets |= (ci->ci_gic_sgir & ICC_SGIR_EL1_TargetList);
    448  1.13.4.2  christos 		}
    449  1.13.4.2  christos 		if (targets != 0)
    450  1.13.4.2  christos 			icc_sgi1r_write(intid | aff | targets);
    451  1.13.4.2  christos 	}
    452  1.13.4.2  christos }
    453  1.13.4.2  christos 
    454  1.13.4.2  christos static void
    455  1.13.4.2  christos gicv3_get_affinity(struct pic_softc *pic, size_t irq, kcpuset_t *affinity)
    456  1.13.4.2  christos {
    457  1.13.4.2  christos 	struct gicv3_softc * const sc = PICTOSOFTC(pic);
    458  1.13.4.2  christos 	const size_t group = irq / 32;
    459  1.13.4.2  christos 	int n;
    460  1.13.4.2  christos 
    461  1.13.4.2  christos 	kcpuset_zero(affinity);
    462  1.13.4.2  christos 	if (group == 0) {
    463  1.13.4.2  christos 		/* All CPUs are targets for group 0 (SGI/PPI) */
    464  1.13.4.2  christos 		for (n = 0; n < ncpu; n++) {
    465  1.13.4.2  christos 			if (sc->sc_irouter[n] != UINT64_MAX)
    466  1.13.4.2  christos 				kcpuset_set(affinity, n);
    467  1.13.4.2  christos 		}
    468  1.13.4.2  christos 	} else {
    469  1.13.4.2  christos 		/* Find distributor targets (SPI) */
    470  1.13.4.2  christos 		const uint64_t irouter = gicd_read_8(sc, GICD_IROUTER(irq));
    471  1.13.4.2  christos 		for (n = 0; n < ncpu; n++) {
    472  1.13.4.2  christos 			if (irouter == GICD_IROUTER_Interrupt_Routing_mode ||
    473  1.13.4.2  christos 			    irouter == sc->sc_irouter[n])
    474  1.13.4.2  christos 				kcpuset_set(affinity, n);
    475  1.13.4.2  christos 		}
    476  1.13.4.2  christos 	}
    477  1.13.4.2  christos }
    478  1.13.4.2  christos 
    479  1.13.4.2  christos static int
    480  1.13.4.2  christos gicv3_set_affinity(struct pic_softc *pic, size_t irq, const kcpuset_t *affinity)
    481  1.13.4.2  christos {
    482  1.13.4.2  christos 	struct gicv3_softc * const sc = PICTOSOFTC(pic);
    483  1.13.4.2  christos 	const size_t group = irq / 32;
    484  1.13.4.2  christos 	uint64_t irouter;
    485  1.13.4.2  christos 
    486  1.13.4.2  christos 	if (group == 0)
    487  1.13.4.2  christos 		return EINVAL;
    488  1.13.4.2  christos 
    489  1.13.4.2  christos 	const int set = kcpuset_countset(affinity);
    490  1.13.4.2  christos 	if (set == ncpu)
    491  1.13.4.2  christos 		irouter = GICD_IROUTER_Interrupt_Routing_mode;
    492  1.13.4.2  christos 	else if (set == 1)
    493  1.13.4.2  christos 		irouter = sc->sc_irouter[kcpuset_ffs(affinity) - 1];
    494  1.13.4.2  christos 	else
    495  1.13.4.2  christos 		return EINVAL;
    496  1.13.4.2  christos 
    497  1.13.4.2  christos 	gicd_write_8(sc, GICD_IROUTER(irq), irouter);
    498  1.13.4.2  christos 
    499  1.13.4.2  christos 	return 0;
    500  1.13.4.2  christos }
    501  1.13.4.2  christos #endif
    502  1.13.4.2  christos 
    503  1.13.4.2  christos static const struct pic_ops gicv3_picops = {
    504  1.13.4.2  christos 	.pic_unblock_irqs = gicv3_unblock_irqs,
    505  1.13.4.2  christos 	.pic_block_irqs = gicv3_block_irqs,
    506  1.13.4.2  christos 	.pic_establish_irq = gicv3_establish_irq,
    507  1.13.4.2  christos 	.pic_set_priority = gicv3_set_priority,
    508  1.13.4.2  christos #ifdef MULTIPROCESSOR
    509  1.13.4.2  christos 	.pic_cpu_init = gicv3_cpu_init,
    510  1.13.4.2  christos 	.pic_ipi_send = gicv3_ipi_send,
    511  1.13.4.2  christos 	.pic_get_affinity = gicv3_get_affinity,
    512  1.13.4.2  christos 	.pic_set_affinity = gicv3_set_affinity,
    513  1.13.4.2  christos #endif
    514  1.13.4.2  christos };
    515  1.13.4.2  christos 
    516  1.13.4.2  christos static void
    517  1.13.4.2  christos gicv3_lpi_unblock_irqs(struct pic_softc *pic, size_t irqbase, uint32_t mask)
    518  1.13.4.2  christos {
    519  1.13.4.2  christos 	struct gicv3_softc * const sc = LPITOSOFTC(pic);
    520  1.13.4.2  christos 	int bit;
    521  1.13.4.2  christos 
    522  1.13.4.2  christos 	while ((bit = ffs(mask)) != 0) {
    523  1.13.4.2  christos 		sc->sc_lpiconf.base[irqbase + bit - 1] |= GIC_LPICONF_Enable;
    524  1.13.4.2  christos 		mask &= ~__BIT(bit - 1);
    525  1.13.4.2  christos 	}
    526  1.13.4.2  christos 
    527  1.13.4.2  christos 	bus_dmamap_sync(sc->sc_dmat, sc->sc_lpiconf.map, irqbase, 32, BUS_DMASYNC_PREWRITE);
    528  1.13.4.2  christos }
    529  1.13.4.2  christos 
    530  1.13.4.2  christos static void
    531  1.13.4.2  christos gicv3_lpi_block_irqs(struct pic_softc *pic, size_t irqbase, uint32_t mask)
    532  1.13.4.2  christos {
    533  1.13.4.2  christos 	struct gicv3_softc * const sc = LPITOSOFTC(pic);
    534  1.13.4.2  christos 	int bit;
    535  1.13.4.2  christos 
    536  1.13.4.2  christos 	while ((bit = ffs(mask)) != 0) {
    537  1.13.4.2  christos 		sc->sc_lpiconf.base[irqbase + bit - 1] &= ~GIC_LPICONF_Enable;
    538  1.13.4.2  christos 		mask &= ~__BIT(bit - 1);
    539  1.13.4.2  christos 	}
    540  1.13.4.2  christos 
    541  1.13.4.2  christos 	bus_dmamap_sync(sc->sc_dmat, sc->sc_lpiconf.map, irqbase, 32, BUS_DMASYNC_PREWRITE);
    542  1.13.4.2  christos }
    543  1.13.4.2  christos 
    544  1.13.4.2  christos static void
    545  1.13.4.2  christos gicv3_lpi_establish_irq(struct pic_softc *pic, struct intrsource *is)
    546  1.13.4.2  christos {
    547  1.13.4.2  christos 	struct gicv3_softc * const sc = LPITOSOFTC(pic);
    548  1.13.4.2  christos 
    549  1.13.4.2  christos 	sc->sc_lpiconf.base[is->is_irq] = 0x80 | IPL_TO_PRIORITY(is->is_ipl) | GIC_LPICONF_Res1;
    550  1.13.4.2  christos 
    551  1.13.4.2  christos 	bus_dmamap_sync(sc->sc_dmat, sc->sc_lpiconf.map, is->is_irq, 1, BUS_DMASYNC_PREWRITE);
    552  1.13.4.2  christos }
    553  1.13.4.2  christos 
    554  1.13.4.2  christos static void
    555  1.13.4.2  christos gicv3_lpi_cpu_init(struct pic_softc *pic, struct cpu_info *ci)
    556  1.13.4.2  christos {
    557  1.13.4.2  christos 	struct gicv3_softc * const sc = LPITOSOFTC(pic);
    558  1.13.4.2  christos 	struct gicv3_lpi_callback *cb;
    559  1.13.4.2  christos 	uint32_t ctlr;
    560  1.13.4.2  christos 
    561  1.13.4.2  christos 	/* If physical LPIs are not supported on this redistributor, just return. */
    562  1.13.4.2  christos 	const uint64_t typer = gicr_read_8(sc, ci->ci_gic_redist, GICR_TYPER);
    563  1.13.4.2  christos 	if ((typer & GICR_TYPER_PLPIS) == 0)
    564  1.13.4.2  christos 		return;
    565  1.13.4.2  christos 
    566  1.13.4.2  christos 	/* Interrupt target address for this CPU, used by ITS when GITS_TYPER.PTA == 0 */
    567  1.13.4.2  christos 	sc->sc_processor_id[cpu_index(ci)] = __SHIFTOUT(typer, GICR_TYPER_Processor_Number);
    568  1.13.4.2  christos 
    569  1.13.4.2  christos 	/* Disable LPIs before making changes */
    570  1.13.4.2  christos 	ctlr = gicr_read_4(sc, ci->ci_gic_redist, GICR_CTLR);
    571  1.13.4.2  christos 	ctlr &= ~GICR_CTLR_Enable_LPIs;
    572  1.13.4.2  christos 	gicr_write_4(sc, ci->ci_gic_redist, GICR_CTLR, ctlr);
    573  1.13.4.2  christos 	arm_dsb();
    574  1.13.4.2  christos 
    575  1.13.4.2  christos 	/* Setup the LPI configuration table */
    576  1.13.4.2  christos 	const uint64_t propbase = sc->sc_lpiconf.segs[0].ds_addr |
    577  1.13.4.2  christos 	    __SHIFTIN(ffs(pic->pic_maxsources) - 1, GICR_PROPBASER_IDbits) |
    578  1.13.4.2  christos 	    __SHIFTIN(GICR_Shareability_NS, GICR_PROPBASER_Shareability) |
    579  1.13.4.2  christos 	    __SHIFTIN(GICR_Cache_NORMAL_NC, GICR_PROPBASER_InnerCache);
    580  1.13.4.2  christos 	gicr_write_8(sc, ci->ci_gic_redist, GICR_PROPBASER, propbase);
    581  1.13.4.2  christos 
    582  1.13.4.2  christos 	/* Setup the LPI pending table */
    583  1.13.4.2  christos 	const uint64_t pendbase = sc->sc_lpipend[cpu_index(ci)].segs[0].ds_addr |
    584  1.13.4.2  christos 	    __SHIFTIN(GICR_Shareability_NS, GICR_PENDBASER_Shareability) |
    585  1.13.4.2  christos 	    __SHIFTIN(GICR_Cache_NORMAL_NC, GICR_PENDBASER_InnerCache) |
    586  1.13.4.2  christos 	    GICR_PENDBASER_PTZ;
    587  1.13.4.2  christos 	gicr_write_8(sc, ci->ci_gic_redist, GICR_PENDBASER, pendbase);
    588  1.13.4.2  christos 
    589  1.13.4.2  christos 	/* Enable LPIs */
    590  1.13.4.2  christos 	ctlr = gicr_read_4(sc, ci->ci_gic_redist, GICR_CTLR);
    591  1.13.4.2  christos 	ctlr |= GICR_CTLR_Enable_LPIs;
    592  1.13.4.2  christos 	gicr_write_4(sc, ci->ci_gic_redist, GICR_CTLR, ctlr);
    593  1.13.4.2  christos 	arm_dsb();
    594  1.13.4.2  christos 
    595  1.13.4.2  christos 	/* Setup ITS if present */
    596  1.13.4.2  christos 	LIST_FOREACH(cb, &sc->sc_lpi_callbacks, list)
    597  1.13.4.2  christos 		cb->cpu_init(cb->priv, ci);
    598  1.13.4.2  christos }
    599  1.13.4.2  christos 
    600  1.13.4.2  christos #ifdef MULTIPROCESSOR
    601  1.13.4.2  christos static void
    602  1.13.4.2  christos gicv3_lpi_get_affinity(struct pic_softc *pic, size_t irq, kcpuset_t *affinity)
    603  1.13.4.2  christos {
    604  1.13.4.2  christos 	struct gicv3_softc * const sc = LPITOSOFTC(pic);
    605  1.13.4.2  christos 	struct gicv3_lpi_callback *cb;
    606  1.13.4.2  christos 
    607  1.13.4.3    martin 	kcpuset_zero(affinity);
    608  1.13.4.2  christos 	LIST_FOREACH(cb, &sc->sc_lpi_callbacks, list)
    609  1.13.4.2  christos 		cb->get_affinity(cb->priv, irq, affinity);
    610  1.13.4.2  christos }
    611  1.13.4.2  christos 
    612  1.13.4.2  christos static int
    613  1.13.4.2  christos gicv3_lpi_set_affinity(struct pic_softc *pic, size_t irq, const kcpuset_t *affinity)
    614  1.13.4.2  christos {
    615  1.13.4.2  christos 	struct gicv3_softc * const sc = LPITOSOFTC(pic);
    616  1.13.4.2  christos 	struct gicv3_lpi_callback *cb;
    617  1.13.4.2  christos 	int error = EINVAL;
    618  1.13.4.2  christos 
    619  1.13.4.2  christos 	LIST_FOREACH(cb, &sc->sc_lpi_callbacks, list) {
    620  1.13.4.2  christos 		error = cb->set_affinity(cb->priv, irq, affinity);
    621  1.13.4.3    martin 		if (error != EPASSTHROUGH)
    622  1.13.4.2  christos 			return error;
    623  1.13.4.2  christos 	}
    624  1.13.4.2  christos 
    625  1.13.4.3    martin 	return EINVAL;
    626  1.13.4.2  christos }
    627  1.13.4.2  christos #endif
    628  1.13.4.2  christos 
    629  1.13.4.2  christos static const struct pic_ops gicv3_lpiops = {
    630  1.13.4.2  christos 	.pic_unblock_irqs = gicv3_lpi_unblock_irqs,
    631  1.13.4.2  christos 	.pic_block_irqs = gicv3_lpi_block_irqs,
    632  1.13.4.2  christos 	.pic_establish_irq = gicv3_lpi_establish_irq,
    633  1.13.4.2  christos #ifdef MULTIPROCESSOR
    634  1.13.4.2  christos 	.pic_cpu_init = gicv3_lpi_cpu_init,
    635  1.13.4.2  christos 	.pic_get_affinity = gicv3_lpi_get_affinity,
    636  1.13.4.2  christos 	.pic_set_affinity = gicv3_lpi_set_affinity,
    637  1.13.4.2  christos #endif
    638  1.13.4.2  christos };
    639  1.13.4.2  christos 
    640  1.13.4.2  christos void
    641  1.13.4.2  christos gicv3_dma_alloc(struct gicv3_softc *sc, struct gicv3_dma *dma, bus_size_t len, bus_size_t align)
    642  1.13.4.2  christos {
    643  1.13.4.2  christos 	int nsegs, error;
    644  1.13.4.2  christos 
    645  1.13.4.2  christos 	dma->len = len;
    646  1.13.4.2  christos 	error = bus_dmamem_alloc(sc->sc_dmat, dma->len, align, 0, dma->segs, 1, &nsegs, BUS_DMA_WAITOK);
    647  1.13.4.2  christos 	if (error)
    648  1.13.4.2  christos 		panic("bus_dmamem_alloc failed: %d", error);
    649  1.13.4.2  christos 	error = bus_dmamem_map(sc->sc_dmat, dma->segs, nsegs, len, (void **)&dma->base, BUS_DMA_WAITOK);
    650  1.13.4.2  christos 	if (error)
    651  1.13.4.2  christos 		panic("bus_dmamem_map failed: %d", error);
    652  1.13.4.2  christos 	error = bus_dmamap_create(sc->sc_dmat, len, 1, len, 0, BUS_DMA_WAITOK, &dma->map);
    653  1.13.4.2  christos 	if (error)
    654  1.13.4.2  christos 		panic("bus_dmamap_create failed: %d", error);
    655  1.13.4.2  christos 	error = bus_dmamap_load(sc->sc_dmat, dma->map, dma->base, dma->len, NULL, BUS_DMA_WAITOK);
    656  1.13.4.2  christos 	if (error)
    657  1.13.4.2  christos 		panic("bus_dmamap_load failed: %d", error);
    658  1.13.4.2  christos 
    659  1.13.4.2  christos 	memset(dma->base, 0, dma->len);
    660  1.13.4.2  christos 	bus_dmamap_sync(sc->sc_dmat, dma->map, 0, dma->len, BUS_DMASYNC_PREWRITE);
    661  1.13.4.2  christos }
    662  1.13.4.2  christos 
    663  1.13.4.2  christos static void
    664  1.13.4.2  christos gicv3_lpi_init(struct gicv3_softc *sc)
    665  1.13.4.2  christos {
    666  1.13.4.2  christos 	/*
    667  1.13.4.2  christos 	 * Allocate LPI configuration table
    668  1.13.4.2  christos 	 */
    669  1.13.4.2  christos 	gicv3_dma_alloc(sc, &sc->sc_lpiconf, sc->sc_lpi.pic_maxsources, 0x1000);
    670  1.13.4.2  christos 	KASSERT((sc->sc_lpiconf.segs[0].ds_addr & ~GICR_PROPBASER_Physical_Address) == 0);
    671  1.13.4.2  christos 
    672  1.13.4.2  christos 	/*
    673  1.13.4.2  christos 	 * Allocate LPI pending tables
    674  1.13.4.2  christos 	 */
    675  1.13.4.2  christos 	const bus_size_t lpipend_sz = sc->sc_lpi.pic_maxsources / NBBY;
    676  1.13.4.2  christos 	for (int cpuindex = 0; cpuindex < ncpu; cpuindex++) {
    677  1.13.4.2  christos 		gicv3_dma_alloc(sc, &sc->sc_lpipend[cpuindex], lpipend_sz, 0x10000);
    678  1.13.4.2  christos 		KASSERT((sc->sc_lpipend[cpuindex].segs[0].ds_addr & ~GICR_PENDBASER_Physical_Address) == 0);
    679  1.13.4.2  christos 	}
    680  1.13.4.2  christos }
    681  1.13.4.2  christos 
    682  1.13.4.2  christos void
    683  1.13.4.2  christos gicv3_irq_handler(void *frame)
    684  1.13.4.2  christos {
    685  1.13.4.2  christos 	struct cpu_info * const ci = curcpu();
    686  1.13.4.2  christos 	struct gicv3_softc * const sc = gicv3_softc;
    687  1.13.4.2  christos 	struct pic_softc *pic;
    688  1.13.4.2  christos 	const int oldipl = ci->ci_cpl;
    689  1.13.4.2  christos 
    690  1.13.4.2  christos 	ci->ci_data.cpu_nintr++;
    691  1.13.4.2  christos 
    692  1.13.4.2  christos 	for (;;) {
    693  1.13.4.2  christos 		const uint32_t iar = icc_iar1_read();
    694  1.13.4.2  christos 		const uint32_t irq = __SHIFTOUT(iar, ICC_IAR_INTID);
    695  1.13.4.2  christos 		if (irq == ICC_IAR_INTID_SPURIOUS)
    696  1.13.4.2  christos 			break;
    697  1.13.4.2  christos 
    698  1.13.4.2  christos 		pic = irq >= GIC_LPI_BASE ? &sc->sc_lpi : &sc->sc_pic;
    699  1.13.4.2  christos 		if (irq - pic->pic_irqbase >= pic->pic_maxsources)
    700  1.13.4.2  christos 			continue;
    701  1.13.4.2  christos 
    702  1.13.4.2  christos 		struct intrsource * const is = pic->pic_sources[irq - pic->pic_irqbase];
    703  1.13.4.2  christos 		KASSERT(is != NULL);
    704  1.13.4.2  christos 
    705  1.13.4.2  christos 		const int ipl = is->is_ipl;
    706  1.13.4.2  christos 		if (ci->ci_cpl < ipl)
    707  1.13.4.2  christos 			pic_set_priority(ci, ipl);
    708  1.13.4.2  christos 
    709  1.13.4.2  christos 		cpsie(I32_bit);
    710  1.13.4.2  christos 		pic_dispatch(is, frame);
    711  1.13.4.2  christos 		cpsid(I32_bit);
    712  1.13.4.2  christos 
    713  1.13.4.2  christos 		icc_eoi1r_write(iar);
    714  1.13.4.2  christos 	}
    715  1.13.4.2  christos 
    716  1.13.4.2  christos 	if (ci->ci_cpl != oldipl)
    717  1.13.4.2  christos 		pic_set_priority(ci, oldipl);
    718  1.13.4.2  christos }
    719  1.13.4.2  christos 
    720  1.13.4.2  christos int
    721  1.13.4.2  christos gicv3_init(struct gicv3_softc *sc)
    722  1.13.4.2  christos {
    723  1.13.4.2  christos 	const uint32_t gicd_typer = gicd_read_4(sc, GICD_TYPER);
    724  1.13.4.2  christos 	int n;
    725  1.13.4.2  christos 
    726  1.13.4.2  christos 	KASSERT(CPU_IS_PRIMARY(curcpu()));
    727  1.13.4.2  christos 
    728  1.13.4.2  christos 	LIST_INIT(&sc->sc_lpi_callbacks);
    729  1.13.4.2  christos 
    730  1.13.4.2  christos 	for (n = 0; n < MAXCPUS; n++)
    731  1.13.4.2  christos 		sc->sc_irouter[n] = UINT64_MAX;
    732  1.13.4.2  christos 
    733  1.13.4.2  christos 	sc->sc_pic.pic_ops = &gicv3_picops;
    734  1.13.4.2  christos 	sc->sc_pic.pic_maxsources = GICD_TYPER_LINES(gicd_typer);
    735  1.13.4.2  christos 	snprintf(sc->sc_pic.pic_name, sizeof(sc->sc_pic.pic_name), "gicv3");
    736  1.13.4.2  christos #ifdef MULTIPROCESSOR
    737  1.13.4.2  christos 	sc->sc_pic.pic_cpus = kcpuset_running;
    738  1.13.4.2  christos #endif
    739  1.13.4.2  christos 	pic_add(&sc->sc_pic, 0);
    740  1.13.4.2  christos 
    741  1.13.4.2  christos 	if ((gicd_typer & GICD_TYPER_LPIS) != 0) {
    742  1.13.4.2  christos 		sc->sc_lpi.pic_ops = &gicv3_lpiops;
    743  1.13.4.2  christos 		sc->sc_lpi.pic_maxsources = 8192;	/* Min. required by GICv3 spec */
    744  1.13.4.2  christos 		snprintf(sc->sc_lpi.pic_name, sizeof(sc->sc_lpi.pic_name), "gicv3-lpi");
    745  1.13.4.2  christos 		pic_add(&sc->sc_lpi, GIC_LPI_BASE);
    746  1.13.4.2  christos 
    747  1.13.4.3    martin 		sc->sc_lpi_pool = vmem_create("gicv3-lpi", 0, sc->sc_lpi.pic_maxsources,
    748  1.13.4.3    martin 		    1, NULL, NULL, NULL, 0, VM_SLEEP, IPL_HIGH);
    749  1.13.4.3    martin 		if (sc->sc_lpi_pool == NULL)
    750  1.13.4.3    martin 			panic("failed to create gicv3 lpi pool\n");
    751  1.13.4.3    martin 
    752  1.13.4.2  christos 		gicv3_lpi_init(sc);
    753  1.13.4.2  christos 	}
    754  1.13.4.2  christos 
    755  1.13.4.2  christos 	KASSERT(gicv3_softc == NULL);
    756  1.13.4.2  christos 	gicv3_softc = sc;
    757  1.13.4.2  christos 
    758  1.13.4.2  christos 	for (int i = 0; i < sc->sc_bsh_r_count; i++) {
    759  1.13.4.2  christos 		const uint64_t gicr_typer = gicr_read_8(sc, i, GICR_TYPER);
    760  1.13.4.2  christos 		const u_int aff0 = __SHIFTOUT(gicr_typer, GICR_TYPER_Affinity_Value_Aff0);
    761  1.13.4.2  christos 		const u_int aff1 = __SHIFTOUT(gicr_typer, GICR_TYPER_Affinity_Value_Aff1);
    762  1.13.4.2  christos 		const u_int aff2 = __SHIFTOUT(gicr_typer, GICR_TYPER_Affinity_Value_Aff2);
    763  1.13.4.2  christos 		const u_int aff3 = __SHIFTOUT(gicr_typer, GICR_TYPER_Affinity_Value_Aff3);
    764  1.13.4.2  christos 
    765  1.13.4.2  christos 		aprint_debug_dev(sc->sc_dev, "redist %d: cpu %d.%d.%d.%d\n",
    766  1.13.4.2  christos 		    i, aff3, aff2, aff1, aff0);
    767  1.13.4.2  christos 	}
    768  1.13.4.2  christos 
    769  1.13.4.2  christos 	gicv3_dist_enable(sc);
    770  1.13.4.2  christos 
    771  1.13.4.2  christos 	gicv3_cpu_init(&sc->sc_pic, curcpu());
    772  1.13.4.2  christos 	if ((gicd_typer & GICD_TYPER_LPIS) != 0)
    773  1.13.4.2  christos 		gicv3_lpi_cpu_init(&sc->sc_lpi, curcpu());
    774  1.13.4.2  christos 
    775  1.13.4.2  christos #ifdef __HAVE_PIC_FAST_SOFTINTS
    776  1.13.4.2  christos 	intr_establish_xname(SOFTINT_BIO, IPL_SOFTBIO, IST_MPSAFE | IST_EDGE, pic_handle_softint, (void *)SOFTINT_BIO, "softint bio");
    777  1.13.4.2  christos 	intr_establish_xname(SOFTINT_CLOCK, IPL_SOFTCLOCK, IST_MPSAFE | IST_EDGE, pic_handle_softint, (void *)SOFTINT_CLOCK, "softint clock");
    778  1.13.4.2  christos 	intr_establish_xname(SOFTINT_NET, IPL_SOFTNET, IST_MPSAFE | IST_EDGE, pic_handle_softint, (void *)SOFTINT_NET, "softint net");
    779  1.13.4.2  christos 	intr_establish_xname(SOFTINT_SERIAL, IPL_SOFTSERIAL, IST_MPSAFE | IST_EDGE, pic_handle_softint, (void *)SOFTINT_SERIAL, "softint serial");
    780  1.13.4.2  christos #endif
    781  1.13.4.2  christos 
    782  1.13.4.2  christos #ifdef MULTIPROCESSOR
    783  1.13.4.2  christos 	intr_establish_xname(IPI_AST, IPL_VM, IST_MPSAFE | IST_EDGE, pic_ipi_ast, (void *)-1, "IPI ast");
    784  1.13.4.2  christos 	intr_establish_xname(IPI_XCALL, IPL_HIGH, IST_MPSAFE | IST_EDGE, pic_ipi_xcall, (void *)-1, "IPI xcall");
    785  1.13.4.2  christos 	intr_establish_xname(IPI_GENERIC, IPL_HIGH, IST_MPSAFE | IST_EDGE, pic_ipi_generic, (void *)-1, "IPI generic");
    786  1.13.4.2  christos 	intr_establish_xname(IPI_NOP, IPL_VM, IST_MPSAFE | IST_EDGE, pic_ipi_nop, (void *)-1, "IPI nop");
    787  1.13.4.2  christos 	intr_establish_xname(IPI_SHOOTDOWN, IPL_SCHED, IST_MPSAFE | IST_EDGE, pic_ipi_shootdown, (void *)-1, "IPI shootdown");
    788  1.13.4.2  christos #ifdef DDB
    789  1.13.4.2  christos 	intr_establish_xname(IPI_DDB, IPL_HIGH, IST_MPSAFE | IST_EDGE, pic_ipi_ddb, NULL, "IPI ddb");
    790  1.13.4.2  christos #endif
    791  1.13.4.2  christos #ifdef __HAVE_PREEMPTION
    792  1.13.4.2  christos 	intr_establish_xname(IPI_KPREEMPT, IPL_VM, IST_MPSAFE | IST_EDGE, pic_ipi_kpreempt, (void *)-1, "IPI kpreempt");
    793  1.13.4.2  christos #endif
    794  1.13.4.2  christos #endif
    795  1.13.4.2  christos 
    796  1.13.4.2  christos 	return 0;
    797  1.13.4.2  christos }
    798