Home | History | Annotate | Line # | Download | only in cortex
gicv3.c revision 1.54.12.1
      1  1.54.12.1    martin /* $NetBSD: gicv3.c,v 1.54.12.1 2025/09/05 09:21:33 martin Exp $ */
      2        1.1  jmcneill 
      3        1.1  jmcneill /*-
      4        1.1  jmcneill  * Copyright (c) 2018 Jared McNeill <jmcneill (at) invisible.ca>
      5        1.1  jmcneill  * All rights reserved.
      6        1.1  jmcneill  *
      7        1.1  jmcneill  * Redistribution and use in source and binary forms, with or without
      8        1.1  jmcneill  * modification, are permitted provided that the following conditions
      9        1.1  jmcneill  * are met:
     10        1.1  jmcneill  * 1. Redistributions of source code must retain the above copyright
     11        1.1  jmcneill  *    notice, this list of conditions and the following disclaimer.
     12        1.1  jmcneill  * 2. Redistributions in binary form must reproduce the above copyright
     13        1.1  jmcneill  *    notice, this list of conditions and the following disclaimer in the
     14        1.1  jmcneill  *    documentation and/or other materials provided with the distribution.
     15        1.1  jmcneill  *
     16        1.1  jmcneill  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17        1.1  jmcneill  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18        1.1  jmcneill  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19        1.1  jmcneill  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20        1.1  jmcneill  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21        1.1  jmcneill  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22        1.1  jmcneill  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23        1.1  jmcneill  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24        1.1  jmcneill  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25        1.1  jmcneill  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26        1.1  jmcneill  * SUCH DAMAGE.
     27        1.1  jmcneill  */
     28        1.1  jmcneill 
     29        1.1  jmcneill #include "opt_multiprocessor.h"
     30        1.1  jmcneill 
     31        1.1  jmcneill #define	_INTR_PRIVATE
     32        1.1  jmcneill 
     33        1.1  jmcneill #include <sys/cdefs.h>
     34  1.54.12.1    martin __KERNEL_RCSID(0, "$NetBSD: gicv3.c,v 1.54.12.1 2025/09/05 09:21:33 martin Exp $");
     35        1.1  jmcneill 
     36        1.1  jmcneill #include <sys/param.h>
     37        1.1  jmcneill #include <sys/kernel.h>
     38        1.1  jmcneill #include <sys/bus.h>
     39        1.1  jmcneill #include <sys/device.h>
     40        1.1  jmcneill #include <sys/intr.h>
     41        1.1  jmcneill #include <sys/systm.h>
     42        1.1  jmcneill #include <sys/cpu.h>
     43       1.23  jmcneill #include <sys/vmem.h>
     44       1.39  jmcneill #include <sys/kmem.h>
     45       1.32  jmcneill #include <sys/atomic.h>
     46        1.1  jmcneill 
     47       1.20  jmcneill #include <machine/cpufunc.h>
     48       1.20  jmcneill 
     49        1.1  jmcneill #include <arm/locore.h>
     50        1.1  jmcneill #include <arm/armreg.h>
     51        1.1  jmcneill 
     52        1.1  jmcneill #include <arm/cortex/gicv3.h>
     53        1.1  jmcneill #include <arm/cortex/gic_reg.h>
     54       1.46  jmcneill 
     55        1.1  jmcneill #define	PICTOSOFTC(pic)	\
     56       1.50  riastrad 	container_of(pic, struct gicv3_softc, sc_pic)
     57        1.5  jmcneill #define	LPITOSOFTC(lpi) \
     58       1.50  riastrad 	container_of(lpi, struct gicv3_softc, sc_lpi)
     59        1.1  jmcneill 
     60  1.54.12.1    martin #define	IPL_TO_PRIORITY(sc, ipl)	(((0xffU - (ipl)) << (sc)->sc_priority_shift) & 0xffU)
     61  1.54.12.1    martin #define	IPL_TO_PMR(sc, ipl)		(((0xffU - (ipl)) << (sc)->sc_pmr_shift) & 0xffU)
     62       1.53  jmcneill #define	IPL_TO_HWPL(ipl)		((ipl) >= IPL_VM ? (ipl) : IPL_NONE)
     63       1.35  jmcneill 
     64       1.36  jmcneill #define	GIC_SUPPORTS_1OFN(sc)		(((sc)->sc_gicd_typer & GICD_TYPER_No1N) == 0)
     65       1.36  jmcneill 
     66       1.35  jmcneill #define	GIC_PRIO_SHIFT_NS		4
     67       1.35  jmcneill #define	GIC_PRIO_SHIFT_S		3
     68        1.1  jmcneill 
     69       1.44  jmcneill /*
     70       1.44  jmcneill  * Set to true if you want to use 1 of N interrupt distribution for SPIs
     71       1.44  jmcneill  * when available. Disabled by default because it causes issues with the
     72       1.44  jmcneill  * USB stack.
     73       1.44  jmcneill  */
     74       1.44  jmcneill bool gicv3_use_1ofn = false;
     75       1.44  jmcneill 
     76        1.1  jmcneill static struct gicv3_softc *gicv3_softc;
     77        1.1  jmcneill 
     78        1.1  jmcneill static inline uint32_t
     79        1.1  jmcneill gicd_read_4(struct gicv3_softc *sc, bus_size_t reg)
     80        1.1  jmcneill {
     81        1.1  jmcneill 	return bus_space_read_4(sc->sc_bst, sc->sc_bsh_d, reg);
     82        1.1  jmcneill }
     83        1.1  jmcneill 
     84        1.1  jmcneill static inline void
     85        1.1  jmcneill gicd_write_4(struct gicv3_softc *sc, bus_size_t reg, uint32_t val)
     86        1.1  jmcneill {
     87        1.1  jmcneill 	bus_space_write_4(sc->sc_bst, sc->sc_bsh_d, reg, val);
     88        1.1  jmcneill }
     89        1.1  jmcneill 
     90       1.41       ryo #ifdef MULTIPROCESSOR
     91        1.6  jmcneill static inline uint64_t
     92        1.6  jmcneill gicd_read_8(struct gicv3_softc *sc, bus_size_t reg)
     93        1.6  jmcneill {
     94        1.6  jmcneill 	return bus_space_read_8(sc->sc_bst, sc->sc_bsh_d, reg);
     95        1.6  jmcneill }
     96       1.41       ryo #endif
     97        1.6  jmcneill 
     98        1.1  jmcneill static inline void
     99        1.1  jmcneill gicd_write_8(struct gicv3_softc *sc, bus_size_t reg, uint64_t val)
    100        1.1  jmcneill {
    101        1.1  jmcneill 	bus_space_write_8(sc->sc_bst, sc->sc_bsh_d, reg, val);
    102        1.1  jmcneill }
    103        1.1  jmcneill 
    104        1.1  jmcneill static inline uint32_t
    105        1.1  jmcneill gicr_read_4(struct gicv3_softc *sc, u_int index, bus_size_t reg)
    106        1.1  jmcneill {
    107        1.1  jmcneill 	KASSERT(index < sc->sc_bsh_r_count);
    108        1.1  jmcneill 	return bus_space_read_4(sc->sc_bst, sc->sc_bsh_r[index], reg);
    109        1.1  jmcneill }
    110        1.1  jmcneill 
    111        1.1  jmcneill static inline void
    112        1.1  jmcneill gicr_write_4(struct gicv3_softc *sc, u_int index, bus_size_t reg, uint32_t val)
    113        1.1  jmcneill {
    114        1.1  jmcneill 	KASSERT(index < sc->sc_bsh_r_count);
    115        1.1  jmcneill 	bus_space_write_4(sc->sc_bst, sc->sc_bsh_r[index], reg, val);
    116        1.1  jmcneill }
    117        1.1  jmcneill 
    118        1.1  jmcneill static inline uint64_t
    119        1.1  jmcneill gicr_read_8(struct gicv3_softc *sc, u_int index, bus_size_t reg)
    120        1.1  jmcneill {
    121        1.1  jmcneill 	KASSERT(index < sc->sc_bsh_r_count);
    122        1.1  jmcneill 	return bus_space_read_8(sc->sc_bst, sc->sc_bsh_r[index], reg);
    123        1.1  jmcneill }
    124        1.1  jmcneill 
    125        1.1  jmcneill static inline void
    126        1.1  jmcneill gicr_write_8(struct gicv3_softc *sc, u_int index, bus_size_t reg, uint64_t val)
    127        1.1  jmcneill {
    128        1.1  jmcneill 	KASSERT(index < sc->sc_bsh_r_count);
    129        1.1  jmcneill 	bus_space_write_8(sc->sc_bst, sc->sc_bsh_r[index], reg, val);
    130        1.1  jmcneill }
    131        1.1  jmcneill 
    132        1.1  jmcneill static void
    133        1.1  jmcneill gicv3_unblock_irqs(struct pic_softc *pic, size_t irqbase, uint32_t mask)
    134        1.1  jmcneill {
    135        1.1  jmcneill 	struct gicv3_softc * const sc = PICTOSOFTC(pic);
    136        1.1  jmcneill 	struct cpu_info * const ci = curcpu();
    137        1.1  jmcneill 	const u_int group = irqbase / 32;
    138        1.1  jmcneill 
    139        1.1  jmcneill 	if (group == 0) {
    140       1.32  jmcneill 		atomic_or_32(&sc->sc_enabled_sgippi, mask);
    141        1.1  jmcneill 		gicr_write_4(sc, ci->ci_gic_redist, GICR_ISENABLER0, mask);
    142        1.5  jmcneill 		while (gicr_read_4(sc, ci->ci_gic_redist, GICR_CTLR) & GICR_CTLR_RWP)
    143        1.1  jmcneill 			;
    144        1.1  jmcneill 	} else {
    145        1.1  jmcneill 		gicd_write_4(sc, GICD_ISENABLERn(group), mask);
    146        1.1  jmcneill 		while (gicd_read_4(sc, GICD_CTRL) & GICD_CTRL_RWP)
    147        1.1  jmcneill 			;
    148        1.1  jmcneill 	}
    149        1.1  jmcneill }
    150        1.1  jmcneill 
    151        1.1  jmcneill static void
    152        1.1  jmcneill gicv3_block_irqs(struct pic_softc *pic, size_t irqbase, uint32_t mask)
    153        1.1  jmcneill {
    154        1.1  jmcneill 	struct gicv3_softc * const sc = PICTOSOFTC(pic);
    155        1.1  jmcneill 	struct cpu_info * const ci = curcpu();
    156        1.1  jmcneill 	const u_int group = irqbase / 32;
    157        1.1  jmcneill 
    158        1.1  jmcneill 	if (group == 0) {
    159       1.32  jmcneill 		atomic_and_32(&sc->sc_enabled_sgippi, ~mask);
    160        1.1  jmcneill 		gicr_write_4(sc, ci->ci_gic_redist, GICR_ICENABLER0, mask);
    161        1.5  jmcneill 		while (gicr_read_4(sc, ci->ci_gic_redist, GICR_CTLR) & GICR_CTLR_RWP)
    162        1.1  jmcneill 			;
    163        1.1  jmcneill 	} else {
    164        1.1  jmcneill 		gicd_write_4(sc, GICD_ICENABLERn(group), mask);
    165        1.1  jmcneill 		while (gicd_read_4(sc, GICD_CTRL) & GICD_CTRL_RWP)
    166        1.1  jmcneill 			;
    167        1.1  jmcneill 	}
    168        1.1  jmcneill }
    169        1.1  jmcneill 
    170        1.1  jmcneill static void
    171        1.1  jmcneill gicv3_establish_irq(struct pic_softc *pic, struct intrsource *is)
    172        1.1  jmcneill {
    173        1.1  jmcneill 	struct gicv3_softc * const sc = PICTOSOFTC(pic);
    174        1.1  jmcneill 	const u_int group = is->is_irq / 32;
    175        1.1  jmcneill 	uint32_t ipriority, icfg;
    176        1.1  jmcneill 	uint64_t irouter;
    177        1.1  jmcneill 	u_int n;
    178        1.1  jmcneill 
    179       1.18  jmcneill 	const u_int ipriority_val = IPL_TO_PRIORITY(sc, is->is_ipl);
    180        1.1  jmcneill 	const u_int ipriority_shift = (is->is_irq & 0x3) * 8;
    181        1.1  jmcneill 	const u_int icfg_shift = (is->is_irq & 0xf) * 2;
    182        1.1  jmcneill 
    183        1.1  jmcneill 	if (group == 0) {
    184       1.48  jmcneill 		/* SGIs and PPIs are per-CPU and always MP-safe */
    185        1.1  jmcneill 		is->is_mpsafe = true;
    186       1.48  jmcneill 		is->is_percpu = true;
    187        1.1  jmcneill 
    188        1.1  jmcneill 		/* Update interrupt configuration and priority on all redistributors */
    189        1.1  jmcneill 		for (n = 0; n < sc->sc_bsh_r_count; n++) {
    190        1.1  jmcneill 			icfg = gicr_read_4(sc, n, GICR_ICFGRn(is->is_irq / 16));
    191        1.1  jmcneill 			if (is->is_type == IST_LEVEL)
    192  1.54.12.1    martin 				icfg &= ~(0x2U << icfg_shift);
    193        1.1  jmcneill 			if (is->is_type == IST_EDGE)
    194  1.54.12.1    martin 				icfg |= (0x2U << icfg_shift);
    195        1.1  jmcneill 			gicr_write_4(sc, n, GICR_ICFGRn(is->is_irq / 16), icfg);
    196        1.1  jmcneill 
    197        1.1  jmcneill 			ipriority = gicr_read_4(sc, n, GICR_IPRIORITYRn(is->is_irq / 4));
    198       1.25  jmcneill 			ipriority &= ~(0xffU << ipriority_shift);
    199        1.2  jmcneill 			ipriority |= (ipriority_val << ipriority_shift);
    200        1.1  jmcneill 			gicr_write_4(sc, n, GICR_IPRIORITYRn(is->is_irq / 4), ipriority);
    201        1.1  jmcneill 		}
    202        1.1  jmcneill 	} else {
    203       1.36  jmcneill 		/*
    204       1.36  jmcneill 		 * If 1 of N SPI routing is supported, route MP-safe interrupts to all
    205       1.36  jmcneill 		 * participating PEs. Otherwise, just route to the primary PE.
    206       1.36  jmcneill 		 */
    207       1.44  jmcneill 		if (is->is_mpsafe && GIC_SUPPORTS_1OFN(sc) && gicv3_use_1ofn) {
    208        1.1  jmcneill 			irouter = GICD_IROUTER_Interrupt_Routing_mode;
    209        1.1  jmcneill 		} else {
    210        1.6  jmcneill 			irouter = sc->sc_irouter[0];
    211        1.1  jmcneill 		}
    212        1.1  jmcneill 		gicd_write_8(sc, GICD_IROUTER(is->is_irq), irouter);
    213        1.1  jmcneill 
    214        1.1  jmcneill 		/* Update interrupt configuration */
    215        1.1  jmcneill 		icfg = gicd_read_4(sc, GICD_ICFGRn(is->is_irq / 16));
    216        1.1  jmcneill 		if (is->is_type == IST_LEVEL)
    217  1.54.12.1    martin 			icfg &= ~(0x2U << icfg_shift);
    218        1.1  jmcneill 		if (is->is_type == IST_EDGE)
    219  1.54.12.1    martin 			icfg |= (0x2U << icfg_shift);
    220        1.1  jmcneill 		gicd_write_4(sc, GICD_ICFGRn(is->is_irq / 16), icfg);
    221        1.1  jmcneill 
    222        1.1  jmcneill 		/* Update interrupt priority */
    223        1.1  jmcneill 		ipriority = gicd_read_4(sc, GICD_IPRIORITYRn(is->is_irq / 4));
    224       1.25  jmcneill 		ipriority &= ~(0xffU << ipriority_shift);
    225        1.2  jmcneill 		ipriority |= (ipriority_val << ipriority_shift);
    226        1.1  jmcneill 		gicd_write_4(sc, GICD_IPRIORITYRn(is->is_irq / 4), ipriority);
    227        1.1  jmcneill 	}
    228        1.1  jmcneill }
    229        1.1  jmcneill 
    230        1.1  jmcneill static void
    231        1.1  jmcneill gicv3_set_priority(struct pic_softc *pic, int ipl)
    232        1.1  jmcneill {
    233       1.18  jmcneill 	struct gicv3_softc * const sc = PICTOSOFTC(pic);
    234       1.42  jmcneill 	struct cpu_info * const ci = curcpu();
    235       1.53  jmcneill 	const int hwpl = IPL_TO_HWPL(ipl);
    236       1.18  jmcneill 
    237       1.53  jmcneill 	while (hwpl < ci->ci_hwpl) {
    238       1.40  jmcneill 		/* Lowering priority mask */
    239       1.53  jmcneill 		ci->ci_hwpl = hwpl;
    240       1.51  jmcneill 		__insn_barrier();
    241       1.53  jmcneill 		icc_pmr_write(IPL_TO_PMR(sc, hwpl));
    242       1.40  jmcneill 	}
    243       1.51  jmcneill 	__insn_barrier();
    244       1.51  jmcneill 	ci->ci_cpl = ipl;
    245        1.1  jmcneill }
    246        1.1  jmcneill 
    247        1.1  jmcneill static void
    248        1.1  jmcneill gicv3_dist_enable(struct gicv3_softc *sc)
    249        1.1  jmcneill {
    250        1.1  jmcneill 	uint32_t gicd_ctrl;
    251        1.1  jmcneill 	u_int n;
    252        1.1  jmcneill 
    253        1.1  jmcneill 	/* Disable the distributor */
    254       1.35  jmcneill 	gicd_ctrl = gicd_read_4(sc, GICD_CTRL);
    255       1.35  jmcneill 	gicd_ctrl &= ~(GICD_CTRL_EnableGrp1A | GICD_CTRL_ARE_NS);
    256       1.35  jmcneill 	gicd_write_4(sc, GICD_CTRL, gicd_ctrl);
    257        1.1  jmcneill 
    258        1.1  jmcneill 	/* Wait for register write to complete */
    259        1.1  jmcneill 	while (gicd_read_4(sc, GICD_CTRL) & GICD_CTRL_RWP)
    260        1.1  jmcneill 		;
    261        1.1  jmcneill 
    262        1.1  jmcneill 	/* Clear all INTID enable bits */
    263        1.1  jmcneill 	for (n = 32; n < sc->sc_pic.pic_maxsources; n += 32)
    264        1.1  jmcneill 		gicd_write_4(sc, GICD_ICENABLERn(n / 32), ~0);
    265        1.1  jmcneill 
    266        1.1  jmcneill 	/* Set default priorities to lowest */
    267        1.1  jmcneill 	for (n = 32; n < sc->sc_pic.pic_maxsources; n += 4)
    268        1.1  jmcneill 		gicd_write_4(sc, GICD_IPRIORITYRn(n / 4), ~0);
    269        1.1  jmcneill 
    270        1.1  jmcneill 	/* Set all interrupts to G1NS */
    271        1.1  jmcneill 	for (n = 32; n < sc->sc_pic.pic_maxsources; n += 32) {
    272        1.1  jmcneill 		gicd_write_4(sc, GICD_IGROUPRn(n / 32), ~0);
    273        1.1  jmcneill 		gicd_write_4(sc, GICD_IGRPMODRn(n / 32), 0);
    274        1.1  jmcneill 	}
    275        1.1  jmcneill 
    276        1.1  jmcneill 	/* Set all interrupts level-sensitive by default */
    277        1.1  jmcneill 	for (n = 32; n < sc->sc_pic.pic_maxsources; n += 16)
    278        1.1  jmcneill 		gicd_write_4(sc, GICD_ICFGRn(n / 16), 0);
    279        1.1  jmcneill 
    280        1.1  jmcneill 	/* Wait for register writes to complete */
    281        1.1  jmcneill 	while (gicd_read_4(sc, GICD_CTRL) & GICD_CTRL_RWP)
    282        1.1  jmcneill 		;
    283        1.1  jmcneill 
    284        1.1  jmcneill 	/* Enable Affinity routing and G1NS interrupts */
    285       1.19  jmcneill 	gicd_ctrl = GICD_CTRL_EnableGrp1A | GICD_CTRL_ARE_NS;
    286        1.1  jmcneill 	gicd_write_4(sc, GICD_CTRL, gicd_ctrl);
    287        1.1  jmcneill }
    288        1.1  jmcneill 
    289        1.1  jmcneill static void
    290        1.1  jmcneill gicv3_redist_enable(struct gicv3_softc *sc, struct cpu_info *ci)
    291        1.1  jmcneill {
    292        1.1  jmcneill 	uint32_t icfg;
    293        1.1  jmcneill 	u_int n, o;
    294        1.1  jmcneill 
    295        1.1  jmcneill 	/* Clear INTID enable bits */
    296        1.1  jmcneill 	gicr_write_4(sc, ci->ci_gic_redist, GICR_ICENABLER0, ~0);
    297        1.1  jmcneill 
    298        1.1  jmcneill 	/* Wait for register write to complete */
    299        1.5  jmcneill 	while (gicr_read_4(sc, ci->ci_gic_redist, GICR_CTLR) & GICR_CTLR_RWP)
    300        1.1  jmcneill 		;
    301        1.1  jmcneill 
    302        1.1  jmcneill 	/* Set default priorities */
    303        1.1  jmcneill 	for (n = 0; n < 32; n += 4) {
    304        1.1  jmcneill 		uint32_t priority = 0;
    305        1.1  jmcneill 		size_t byte_shift = 0;
    306        1.1  jmcneill 		for (o = 0; o < 4; o++, byte_shift += 8) {
    307        1.1  jmcneill 			struct intrsource * const is = sc->sc_pic.pic_sources[n + o];
    308        1.1  jmcneill 			if (is == NULL)
    309       1.25  jmcneill 				priority |= (0xffU << byte_shift);
    310        1.2  jmcneill 			else {
    311       1.18  jmcneill 				const u_int ipriority_val = IPL_TO_PRIORITY(sc, is->is_ipl);
    312        1.2  jmcneill 				priority |= ipriority_val << byte_shift;
    313        1.2  jmcneill 			}
    314        1.1  jmcneill 		}
    315        1.1  jmcneill 		gicr_write_4(sc, ci->ci_gic_redist, GICR_IPRIORITYRn(n / 4), priority);
    316        1.1  jmcneill 	}
    317        1.1  jmcneill 
    318        1.1  jmcneill 	/* Set all interrupts to G1NS */
    319        1.1  jmcneill 	gicr_write_4(sc, ci->ci_gic_redist, GICR_IGROUPR0, ~0);
    320        1.1  jmcneill 	gicr_write_4(sc, ci->ci_gic_redist, GICR_IGRPMODR0, 0);
    321        1.1  jmcneill 
    322        1.1  jmcneill 	/* Restore PPI configs */
    323        1.1  jmcneill 	for (n = 0, icfg = 0; n < 16; n++) {
    324        1.1  jmcneill 		struct intrsource * const is = sc->sc_pic.pic_sources[16 + n];
    325        1.1  jmcneill 		if (is != NULL && is->is_type == IST_EDGE)
    326  1.54.12.1    martin 			icfg |= (0x2U << (n * 2));
    327        1.1  jmcneill 	}
    328        1.1  jmcneill 	gicr_write_4(sc, ci->ci_gic_redist, GICR_ICFGRn(1), icfg);
    329        1.1  jmcneill 
    330        1.1  jmcneill 	/* Restore current enable bits */
    331        1.1  jmcneill 	gicr_write_4(sc, ci->ci_gic_redist, GICR_ISENABLER0, sc->sc_enabled_sgippi);
    332        1.1  jmcneill 
    333        1.1  jmcneill 	/* Wait for register write to complete */
    334        1.5  jmcneill 	while (gicr_read_4(sc, ci->ci_gic_redist, GICR_CTLR) & GICR_CTLR_RWP)
    335        1.1  jmcneill 		;
    336        1.1  jmcneill }
    337        1.1  jmcneill 
    338        1.1  jmcneill static uint64_t
    339        1.1  jmcneill gicv3_cpu_identity(void)
    340        1.1  jmcneill {
    341        1.1  jmcneill 	u_int aff3, aff2, aff1, aff0;
    342        1.1  jmcneill 
    343       1.18  jmcneill 	const register_t mpidr = cpu_mpidr_aff_read();
    344        1.1  jmcneill 	aff0 = __SHIFTOUT(mpidr, MPIDR_AFF0);
    345        1.1  jmcneill 	aff1 = __SHIFTOUT(mpidr, MPIDR_AFF1);
    346        1.1  jmcneill 	aff2 = __SHIFTOUT(mpidr, MPIDR_AFF2);
    347        1.1  jmcneill 	aff3 = __SHIFTOUT(mpidr, MPIDR_AFF3);
    348        1.1  jmcneill 
    349        1.1  jmcneill 	return __SHIFTIN(aff0, GICR_TYPER_Affinity_Value_Aff0) |
    350        1.1  jmcneill 	       __SHIFTIN(aff1, GICR_TYPER_Affinity_Value_Aff1) |
    351        1.1  jmcneill 	       __SHIFTIN(aff2, GICR_TYPER_Affinity_Value_Aff2) |
    352        1.1  jmcneill 	       __SHIFTIN(aff3, GICR_TYPER_Affinity_Value_Aff3);
    353        1.1  jmcneill }
    354        1.1  jmcneill 
    355        1.1  jmcneill static u_int
    356        1.1  jmcneill gicv3_find_redist(struct gicv3_softc *sc)
    357        1.1  jmcneill {
    358        1.1  jmcneill 	uint64_t gicr_typer;
    359        1.1  jmcneill 	u_int n;
    360        1.1  jmcneill 
    361        1.1  jmcneill 	const uint64_t cpu_identity = gicv3_cpu_identity();
    362        1.1  jmcneill 
    363        1.1  jmcneill 	for (n = 0; n < sc->sc_bsh_r_count; n++) {
    364        1.1  jmcneill 		gicr_typer = gicr_read_8(sc, n, GICR_TYPER);
    365        1.1  jmcneill 		if ((gicr_typer & GICR_TYPER_Affinity_Value) == cpu_identity)
    366        1.1  jmcneill 			return n;
    367        1.1  jmcneill 	}
    368        1.1  jmcneill 
    369        1.1  jmcneill 	const u_int aff0 = __SHIFTOUT(cpu_identity, GICR_TYPER_Affinity_Value_Aff0);
    370        1.1  jmcneill 	const u_int aff1 = __SHIFTOUT(cpu_identity, GICR_TYPER_Affinity_Value_Aff1);
    371        1.1  jmcneill 	const u_int aff2 = __SHIFTOUT(cpu_identity, GICR_TYPER_Affinity_Value_Aff2);
    372        1.1  jmcneill 	const u_int aff3 = __SHIFTOUT(cpu_identity, GICR_TYPER_Affinity_Value_Aff3);
    373        1.1  jmcneill 
    374        1.1  jmcneill 	panic("%s: could not find GICv3 redistributor for cpu %d.%d.%d.%d",
    375        1.1  jmcneill 	    cpu_name(curcpu()), aff3, aff2, aff1, aff0);
    376        1.1  jmcneill }
    377        1.1  jmcneill 
    378        1.1  jmcneill static uint64_t
    379        1.1  jmcneill gicv3_sgir(struct gicv3_softc *sc)
    380        1.1  jmcneill {
    381       1.22     skrll 	const uint64_t cpu_identity = gicv3_cpu_identity();
    382        1.1  jmcneill 
    383        1.1  jmcneill 	const u_int aff0 = __SHIFTOUT(cpu_identity, GICR_TYPER_Affinity_Value_Aff0);
    384        1.1  jmcneill 	const u_int aff1 = __SHIFTOUT(cpu_identity, GICR_TYPER_Affinity_Value_Aff1);
    385        1.1  jmcneill 	const u_int aff2 = __SHIFTOUT(cpu_identity, GICR_TYPER_Affinity_Value_Aff2);
    386        1.1  jmcneill 	const u_int aff3 = __SHIFTOUT(cpu_identity, GICR_TYPER_Affinity_Value_Aff3);
    387        1.1  jmcneill 
    388        1.1  jmcneill 	return __SHIFTIN(__BIT(aff0), ICC_SGIR_EL1_TargetList) |
    389        1.1  jmcneill 	       __SHIFTIN(aff1, ICC_SGIR_EL1_Aff1) |
    390        1.1  jmcneill 	       __SHIFTIN(aff2, ICC_SGIR_EL1_Aff2) |
    391       1.22     skrll 	       __SHIFTIN(aff3, ICC_SGIR_EL1_Aff3);
    392        1.1  jmcneill }
    393        1.1  jmcneill 
    394        1.1  jmcneill static void
    395        1.1  jmcneill gicv3_cpu_init(struct pic_softc *pic, struct cpu_info *ci)
    396        1.1  jmcneill {
    397        1.1  jmcneill 	struct gicv3_softc * const sc = PICTOSOFTC(pic);
    398        1.1  jmcneill 	uint32_t icc_sre, icc_ctlr, gicr_waker;
    399        1.1  jmcneill 
    400       1.33  jmcneill 	evcnt_attach_dynamic(&ci->ci_intr_preempt, EVCNT_TYPE_MISC, NULL,
    401       1.33  jmcneill 	    ci->ci_cpuname, "intr preempt");
    402       1.33  jmcneill 
    403        1.1  jmcneill 	ci->ci_gic_redist = gicv3_find_redist(sc);
    404        1.1  jmcneill 	ci->ci_gic_sgir = gicv3_sgir(sc);
    405        1.1  jmcneill 
    406        1.1  jmcneill 	/* Enable System register access and disable IRQ/FIQ bypass */
    407        1.1  jmcneill 	icc_sre = ICC_SRE_EL1_SRE | ICC_SRE_EL1_DFB | ICC_SRE_EL1_DIB;
    408        1.1  jmcneill 	icc_sre_write(icc_sre);
    409        1.1  jmcneill 
    410        1.1  jmcneill 	/* Mark the connected PE as being awake */
    411        1.1  jmcneill 	gicr_waker = gicr_read_4(sc, ci->ci_gic_redist, GICR_WAKER);
    412        1.1  jmcneill 	gicr_waker &= ~GICR_WAKER_ProcessorSleep;
    413        1.1  jmcneill 	gicr_write_4(sc, ci->ci_gic_redist, GICR_WAKER, gicr_waker);
    414        1.1  jmcneill 	while (gicr_read_4(sc, ci->ci_gic_redist, GICR_WAKER) & GICR_WAKER_ChildrenAsleep)
    415        1.1  jmcneill 		;
    416        1.1  jmcneill 
    417        1.1  jmcneill 	/* Set initial priority mask */
    418       1.45  jmcneill 	ci->ci_hwpl = IPL_HIGH;
    419       1.45  jmcneill 	icc_pmr_write(IPL_TO_PMR(sc, IPL_HIGH));
    420        1.1  jmcneill 
    421       1.10  jmcneill 	/* Set the binary point field to the minimum value */
    422       1.10  jmcneill 	icc_bpr1_write(0);
    423        1.1  jmcneill 
    424        1.1  jmcneill 	/* Enable group 1 interrupt signaling */
    425        1.1  jmcneill 	icc_igrpen1_write(ICC_IGRPEN_EL1_Enable);
    426        1.1  jmcneill 
    427        1.1  jmcneill 	/* Set EOI mode */
    428        1.1  jmcneill 	icc_ctlr = icc_ctlr_read();
    429        1.1  jmcneill 	icc_ctlr &= ~ICC_CTLR_EL1_EOImode;
    430        1.1  jmcneill 	icc_ctlr_write(icc_ctlr);
    431        1.1  jmcneill 
    432        1.1  jmcneill 	/* Enable redistributor */
    433        1.1  jmcneill 	gicv3_redist_enable(sc, ci);
    434        1.1  jmcneill 
    435        1.1  jmcneill 	/* Allow IRQ exceptions */
    436       1.40  jmcneill 	ENABLE_INTERRUPT();
    437        1.1  jmcneill }
    438        1.1  jmcneill 
    439        1.1  jmcneill #ifdef MULTIPROCESSOR
    440        1.1  jmcneill static void
    441        1.1  jmcneill gicv3_ipi_send(struct pic_softc *pic, const kcpuset_t *kcp, u_long ipi)
    442        1.1  jmcneill {
    443        1.1  jmcneill 	struct cpu_info *ci;
    444       1.27  jmcneill 	uint64_t sgir;
    445        1.1  jmcneill 
    446       1.27  jmcneill 	sgir = __SHIFTIN(ipi, ICC_SGIR_EL1_INTID);
    447        1.1  jmcneill 	if (kcp == NULL) {
    448        1.1  jmcneill 		/* Interrupts routed to all PEs, excluding "self" */
    449        1.1  jmcneill 		if (ncpu == 1)
    450        1.1  jmcneill 			return;
    451       1.27  jmcneill 		sgir |= ICC_SGIR_EL1_IRM;
    452        1.1  jmcneill 	} else {
    453       1.27  jmcneill 		/* Interrupt to exactly one PE */
    454       1.27  jmcneill 		ci = cpu_lookup(kcpuset_ffs(kcp) - 1);
    455       1.27  jmcneill 		if (ci == curcpu())
    456       1.27  jmcneill 			return;
    457       1.27  jmcneill 		sgir |= ci->ci_gic_sgir;
    458        1.1  jmcneill 	}
    459       1.27  jmcneill 	icc_sgi1r_write(sgir);
    460       1.30  jmcneill 	isb();
    461        1.1  jmcneill }
    462        1.6  jmcneill 
    463        1.6  jmcneill static void
    464        1.6  jmcneill gicv3_get_affinity(struct pic_softc *pic, size_t irq, kcpuset_t *affinity)
    465        1.6  jmcneill {
    466        1.6  jmcneill 	struct gicv3_softc * const sc = PICTOSOFTC(pic);
    467        1.6  jmcneill 	const size_t group = irq / 32;
    468        1.6  jmcneill 	int n;
    469        1.6  jmcneill 
    470        1.6  jmcneill 	kcpuset_zero(affinity);
    471        1.6  jmcneill 	if (group == 0) {
    472        1.6  jmcneill 		/* All CPUs are targets for group 0 (SGI/PPI) */
    473        1.6  jmcneill 		for (n = 0; n < ncpu; n++) {
    474       1.47  jmcneill 			kcpuset_set(affinity, n);
    475        1.6  jmcneill 		}
    476        1.6  jmcneill 	} else {
    477        1.6  jmcneill 		/* Find distributor targets (SPI) */
    478        1.6  jmcneill 		const uint64_t irouter = gicd_read_8(sc, GICD_IROUTER(irq));
    479        1.6  jmcneill 		for (n = 0; n < ncpu; n++) {
    480        1.6  jmcneill 			if (irouter == GICD_IROUTER_Interrupt_Routing_mode ||
    481        1.6  jmcneill 			    irouter == sc->sc_irouter[n])
    482        1.6  jmcneill 				kcpuset_set(affinity, n);
    483        1.6  jmcneill 		}
    484        1.6  jmcneill 	}
    485        1.6  jmcneill }
    486        1.6  jmcneill 
    487        1.6  jmcneill static int
    488        1.6  jmcneill gicv3_set_affinity(struct pic_softc *pic, size_t irq, const kcpuset_t *affinity)
    489        1.6  jmcneill {
    490        1.6  jmcneill 	struct gicv3_softc * const sc = PICTOSOFTC(pic);
    491        1.6  jmcneill 	const size_t group = irq / 32;
    492        1.6  jmcneill 	uint64_t irouter;
    493        1.6  jmcneill 
    494        1.6  jmcneill 	if (group == 0)
    495        1.6  jmcneill 		return EINVAL;
    496        1.6  jmcneill 
    497        1.6  jmcneill 	const int set = kcpuset_countset(affinity);
    498       1.36  jmcneill 	if (set == 1) {
    499       1.36  jmcneill 		irouter = sc->sc_irouter[kcpuset_ffs(affinity) - 1];
    500       1.44  jmcneill 	} else if (set == ncpu && GIC_SUPPORTS_1OFN(sc) && gicv3_use_1ofn) {
    501        1.6  jmcneill 		irouter = GICD_IROUTER_Interrupt_Routing_mode;
    502       1.36  jmcneill 	} else {
    503        1.6  jmcneill 		return EINVAL;
    504       1.36  jmcneill 	}
    505        1.6  jmcneill 
    506        1.6  jmcneill 	gicd_write_8(sc, GICD_IROUTER(irq), irouter);
    507        1.6  jmcneill 
    508        1.6  jmcneill 	return 0;
    509        1.6  jmcneill }
    510        1.1  jmcneill #endif
    511        1.1  jmcneill 
    512        1.1  jmcneill static const struct pic_ops gicv3_picops = {
    513        1.1  jmcneill 	.pic_unblock_irqs = gicv3_unblock_irqs,
    514        1.1  jmcneill 	.pic_block_irqs = gicv3_block_irqs,
    515        1.1  jmcneill 	.pic_establish_irq = gicv3_establish_irq,
    516        1.1  jmcneill 	.pic_set_priority = gicv3_set_priority,
    517        1.1  jmcneill #ifdef MULTIPROCESSOR
    518        1.1  jmcneill 	.pic_cpu_init = gicv3_cpu_init,
    519        1.1  jmcneill 	.pic_ipi_send = gicv3_ipi_send,
    520        1.6  jmcneill 	.pic_get_affinity = gicv3_get_affinity,
    521        1.6  jmcneill 	.pic_set_affinity = gicv3_set_affinity,
    522        1.1  jmcneill #endif
    523        1.1  jmcneill };
    524        1.1  jmcneill 
    525        1.5  jmcneill static void
    526       1.38  jmcneill gicv3_dcache_wb_range(vaddr_t va, vsize_t len)
    527       1.38  jmcneill {
    528       1.38  jmcneill 	cpu_dcache_wb_range(va, len);
    529       1.38  jmcneill 	dsb(sy);
    530       1.38  jmcneill }
    531       1.38  jmcneill 
    532       1.38  jmcneill static void
    533        1.5  jmcneill gicv3_lpi_unblock_irqs(struct pic_softc *pic, size_t irqbase, uint32_t mask)
    534        1.5  jmcneill {
    535        1.5  jmcneill 	struct gicv3_softc * const sc = LPITOSOFTC(pic);
    536        1.5  jmcneill 	int bit;
    537        1.5  jmcneill 
    538        1.5  jmcneill 	while ((bit = ffs(mask)) != 0) {
    539        1.5  jmcneill 		sc->sc_lpiconf.base[irqbase + bit - 1] |= GIC_LPICONF_Enable;
    540       1.20  jmcneill 		if (sc->sc_lpiconf_flush)
    541       1.38  jmcneill 			gicv3_dcache_wb_range((vaddr_t)&sc->sc_lpiconf.base[irqbase + bit - 1], 1);
    542        1.5  jmcneill 		mask &= ~__BIT(bit - 1);
    543        1.5  jmcneill 	}
    544        1.5  jmcneill 
    545       1.20  jmcneill 	if (!sc->sc_lpiconf_flush)
    546       1.26     skrll 		dsb(ishst);
    547        1.5  jmcneill }
    548        1.5  jmcneill 
    549        1.5  jmcneill static void
    550        1.5  jmcneill gicv3_lpi_block_irqs(struct pic_softc *pic, size_t irqbase, uint32_t mask)
    551        1.5  jmcneill {
    552        1.5  jmcneill 	struct gicv3_softc * const sc = LPITOSOFTC(pic);
    553        1.5  jmcneill 	int bit;
    554        1.5  jmcneill 
    555        1.5  jmcneill 	while ((bit = ffs(mask)) != 0) {
    556       1.13  jmcneill 		sc->sc_lpiconf.base[irqbase + bit - 1] &= ~GIC_LPICONF_Enable;
    557       1.20  jmcneill 		if (sc->sc_lpiconf_flush)
    558       1.38  jmcneill 			gicv3_dcache_wb_range((vaddr_t)&sc->sc_lpiconf.base[irqbase + bit - 1], 1);
    559        1.5  jmcneill 		mask &= ~__BIT(bit - 1);
    560        1.5  jmcneill 	}
    561        1.5  jmcneill 
    562       1.20  jmcneill 	if (!sc->sc_lpiconf_flush)
    563       1.26     skrll 		dsb(ishst);
    564        1.5  jmcneill }
    565        1.5  jmcneill 
    566        1.5  jmcneill static void
    567        1.5  jmcneill gicv3_lpi_establish_irq(struct pic_softc *pic, struct intrsource *is)
    568        1.5  jmcneill {
    569        1.5  jmcneill 	struct gicv3_softc * const sc = LPITOSOFTC(pic);
    570        1.5  jmcneill 
    571       1.35  jmcneill 	sc->sc_lpiconf.base[is->is_irq] = IPL_TO_PRIORITY(sc, is->is_ipl) | GIC_LPICONF_Res1;
    572        1.5  jmcneill 
    573       1.20  jmcneill 	if (sc->sc_lpiconf_flush)
    574       1.38  jmcneill 		gicv3_dcache_wb_range((vaddr_t)&sc->sc_lpiconf.base[is->is_irq], 1);
    575       1.20  jmcneill 	else
    576       1.26     skrll 		dsb(ishst);
    577        1.5  jmcneill }
    578        1.5  jmcneill 
    579        1.5  jmcneill static void
    580        1.5  jmcneill gicv3_lpi_cpu_init(struct pic_softc *pic, struct cpu_info *ci)
    581        1.5  jmcneill {
    582        1.5  jmcneill 	struct gicv3_softc * const sc = LPITOSOFTC(pic);
    583        1.7  jmcneill 	struct gicv3_lpi_callback *cb;
    584       1.20  jmcneill 	uint64_t propbase, pendbase;
    585        1.5  jmcneill 	uint32_t ctlr;
    586        1.5  jmcneill 
    587        1.5  jmcneill 	/* If physical LPIs are not supported on this redistributor, just return. */
    588        1.5  jmcneill 	const uint64_t typer = gicr_read_8(sc, ci->ci_gic_redist, GICR_TYPER);
    589        1.5  jmcneill 	if ((typer & GICR_TYPER_PLPIS) == 0)
    590        1.5  jmcneill 		return;
    591        1.5  jmcneill 
    592        1.5  jmcneill 	/* Interrupt target address for this CPU, used by ITS when GITS_TYPER.PTA == 0 */
    593        1.5  jmcneill 	sc->sc_processor_id[cpu_index(ci)] = __SHIFTOUT(typer, GICR_TYPER_Processor_Number);
    594        1.5  jmcneill 
    595        1.5  jmcneill 	/* Disable LPIs before making changes */
    596        1.5  jmcneill 	ctlr = gicr_read_4(sc, ci->ci_gic_redist, GICR_CTLR);
    597        1.5  jmcneill 	ctlr &= ~GICR_CTLR_Enable_LPIs;
    598        1.5  jmcneill 	gicr_write_4(sc, ci->ci_gic_redist, GICR_CTLR, ctlr);
    599       1.26     skrll 	dsb(sy);
    600        1.5  jmcneill 
    601        1.5  jmcneill 	/* Setup the LPI configuration table */
    602       1.20  jmcneill 	propbase = sc->sc_lpiconf.segs[0].ds_addr |
    603        1.5  jmcneill 	    __SHIFTIN(ffs(pic->pic_maxsources) - 1, GICR_PROPBASER_IDbits) |
    604       1.20  jmcneill 	    __SHIFTIN(GICR_Shareability_IS, GICR_PROPBASER_Shareability) |
    605       1.20  jmcneill 	    __SHIFTIN(GICR_Cache_NORMAL_RA_WA_WB, GICR_PROPBASER_InnerCache);
    606        1.5  jmcneill 	gicr_write_8(sc, ci->ci_gic_redist, GICR_PROPBASER, propbase);
    607       1.20  jmcneill 	propbase = gicr_read_8(sc, ci->ci_gic_redist, GICR_PROPBASER);
    608       1.20  jmcneill 	if (__SHIFTOUT(propbase, GICR_PROPBASER_Shareability) != GICR_Shareability_IS) {
    609       1.20  jmcneill 		if (__SHIFTOUT(propbase, GICR_PROPBASER_Shareability) == GICR_Shareability_NS) {
    610       1.20  jmcneill 			propbase &= ~GICR_PROPBASER_Shareability;
    611       1.20  jmcneill 			propbase |= __SHIFTIN(GICR_Shareability_NS, GICR_PROPBASER_Shareability);
    612       1.20  jmcneill 			propbase &= ~GICR_PROPBASER_InnerCache;
    613       1.20  jmcneill 			propbase |= __SHIFTIN(GICR_Cache_NORMAL_NC, GICR_PROPBASER_InnerCache);
    614       1.20  jmcneill 			gicr_write_8(sc, ci->ci_gic_redist, GICR_PROPBASER, propbase);
    615       1.20  jmcneill 		}
    616       1.20  jmcneill 		sc->sc_lpiconf_flush = true;
    617       1.20  jmcneill 	}
    618        1.5  jmcneill 
    619        1.5  jmcneill 	/* Setup the LPI pending table */
    620       1.20  jmcneill 	pendbase = sc->sc_lpipend[cpu_index(ci)].segs[0].ds_addr |
    621       1.20  jmcneill 	    __SHIFTIN(GICR_Shareability_IS, GICR_PENDBASER_Shareability) |
    622       1.20  jmcneill 	    __SHIFTIN(GICR_Cache_NORMAL_RA_WA_WB, GICR_PENDBASER_InnerCache);
    623        1.5  jmcneill 	gicr_write_8(sc, ci->ci_gic_redist, GICR_PENDBASER, pendbase);
    624       1.20  jmcneill 	pendbase = gicr_read_8(sc, ci->ci_gic_redist, GICR_PENDBASER);
    625       1.20  jmcneill 	if (__SHIFTOUT(pendbase, GICR_PENDBASER_Shareability) == GICR_Shareability_NS) {
    626       1.20  jmcneill 		pendbase &= ~GICR_PENDBASER_Shareability;
    627       1.20  jmcneill 		pendbase |= __SHIFTIN(GICR_Shareability_NS, GICR_PENDBASER_Shareability);
    628       1.20  jmcneill 		pendbase &= ~GICR_PENDBASER_InnerCache;
    629       1.20  jmcneill 		pendbase |= __SHIFTIN(GICR_Cache_NORMAL_NC, GICR_PENDBASER_InnerCache);
    630       1.20  jmcneill 		gicr_write_8(sc, ci->ci_gic_redist, GICR_PENDBASER, pendbase);
    631       1.20  jmcneill 	}
    632        1.5  jmcneill 
    633        1.5  jmcneill 	/* Enable LPIs */
    634        1.5  jmcneill 	ctlr = gicr_read_4(sc, ci->ci_gic_redist, GICR_CTLR);
    635        1.5  jmcneill 	ctlr |= GICR_CTLR_Enable_LPIs;
    636        1.5  jmcneill 	gicr_write_4(sc, ci->ci_gic_redist, GICR_CTLR, ctlr);
    637       1.26     skrll 	dsb(sy);
    638        1.5  jmcneill 
    639        1.5  jmcneill 	/* Setup ITS if present */
    640        1.7  jmcneill 	LIST_FOREACH(cb, &sc->sc_lpi_callbacks, list)
    641        1.7  jmcneill 		cb->cpu_init(cb->priv, ci);
    642        1.5  jmcneill }
    643        1.5  jmcneill 
    644        1.7  jmcneill #ifdef MULTIPROCESSOR
    645        1.7  jmcneill static void
    646        1.7  jmcneill gicv3_lpi_get_affinity(struct pic_softc *pic, size_t irq, kcpuset_t *affinity)
    647        1.7  jmcneill {
    648        1.7  jmcneill 	struct gicv3_softc * const sc = LPITOSOFTC(pic);
    649        1.7  jmcneill 	struct gicv3_lpi_callback *cb;
    650        1.7  jmcneill 
    651       1.24  jmcneill 	kcpuset_zero(affinity);
    652        1.7  jmcneill 	LIST_FOREACH(cb, &sc->sc_lpi_callbacks, list)
    653        1.7  jmcneill 		cb->get_affinity(cb->priv, irq, affinity);
    654        1.7  jmcneill }
    655        1.7  jmcneill 
    656        1.7  jmcneill static int
    657        1.7  jmcneill gicv3_lpi_set_affinity(struct pic_softc *pic, size_t irq, const kcpuset_t *affinity)
    658        1.7  jmcneill {
    659        1.7  jmcneill 	struct gicv3_softc * const sc = LPITOSOFTC(pic);
    660        1.7  jmcneill 	struct gicv3_lpi_callback *cb;
    661        1.7  jmcneill 	int error = EINVAL;
    662        1.7  jmcneill 
    663        1.7  jmcneill 	LIST_FOREACH(cb, &sc->sc_lpi_callbacks, list) {
    664        1.7  jmcneill 		error = cb->set_affinity(cb->priv, irq, affinity);
    665       1.24  jmcneill 		if (error != EPASSTHROUGH)
    666        1.7  jmcneill 			return error;
    667        1.7  jmcneill 	}
    668        1.7  jmcneill 
    669       1.24  jmcneill 	return EINVAL;
    670        1.7  jmcneill }
    671        1.7  jmcneill #endif
    672        1.7  jmcneill 
    673        1.5  jmcneill static const struct pic_ops gicv3_lpiops = {
    674        1.5  jmcneill 	.pic_unblock_irqs = gicv3_lpi_unblock_irqs,
    675        1.5  jmcneill 	.pic_block_irqs = gicv3_lpi_block_irqs,
    676        1.5  jmcneill 	.pic_establish_irq = gicv3_lpi_establish_irq,
    677        1.5  jmcneill #ifdef MULTIPROCESSOR
    678        1.5  jmcneill 	.pic_cpu_init = gicv3_lpi_cpu_init,
    679        1.7  jmcneill 	.pic_get_affinity = gicv3_lpi_get_affinity,
    680        1.7  jmcneill 	.pic_set_affinity = gicv3_lpi_set_affinity,
    681        1.5  jmcneill #endif
    682        1.5  jmcneill };
    683        1.5  jmcneill 
    684        1.5  jmcneill void
    685        1.5  jmcneill gicv3_dma_alloc(struct gicv3_softc *sc, struct gicv3_dma *dma, bus_size_t len, bus_size_t align)
    686        1.5  jmcneill {
    687        1.5  jmcneill 	int nsegs, error;
    688        1.5  jmcneill 
    689        1.5  jmcneill 	dma->len = len;
    690        1.5  jmcneill 	error = bus_dmamem_alloc(sc->sc_dmat, dma->len, align, 0, dma->segs, 1, &nsegs, BUS_DMA_WAITOK);
    691        1.5  jmcneill 	if (error)
    692        1.5  jmcneill 		panic("bus_dmamem_alloc failed: %d", error);
    693        1.5  jmcneill 	error = bus_dmamem_map(sc->sc_dmat, dma->segs, nsegs, len, (void **)&dma->base, BUS_DMA_WAITOK);
    694        1.5  jmcneill 	if (error)
    695        1.5  jmcneill 		panic("bus_dmamem_map failed: %d", error);
    696        1.5  jmcneill 	error = bus_dmamap_create(sc->sc_dmat, len, 1, len, 0, BUS_DMA_WAITOK, &dma->map);
    697        1.5  jmcneill 	if (error)
    698        1.5  jmcneill 		panic("bus_dmamap_create failed: %d", error);
    699        1.5  jmcneill 	error = bus_dmamap_load(sc->sc_dmat, dma->map, dma->base, dma->len, NULL, BUS_DMA_WAITOK);
    700        1.5  jmcneill 	if (error)
    701        1.5  jmcneill 		panic("bus_dmamap_load failed: %d", error);
    702        1.5  jmcneill 
    703        1.5  jmcneill 	memset(dma->base, 0, dma->len);
    704        1.5  jmcneill 	bus_dmamap_sync(sc->sc_dmat, dma->map, 0, dma->len, BUS_DMASYNC_PREWRITE);
    705        1.5  jmcneill }
    706        1.5  jmcneill 
    707        1.5  jmcneill static void
    708        1.5  jmcneill gicv3_lpi_init(struct gicv3_softc *sc)
    709        1.5  jmcneill {
    710        1.5  jmcneill 	/*
    711        1.5  jmcneill 	 * Allocate LPI configuration table
    712        1.5  jmcneill 	 */
    713        1.5  jmcneill 	gicv3_dma_alloc(sc, &sc->sc_lpiconf, sc->sc_lpi.pic_maxsources, 0x1000);
    714        1.5  jmcneill 	KASSERT((sc->sc_lpiconf.segs[0].ds_addr & ~GICR_PROPBASER_Physical_Address) == 0);
    715        1.5  jmcneill 
    716        1.5  jmcneill 	/*
    717        1.5  jmcneill 	 * Allocate LPI pending tables
    718        1.5  jmcneill 	 */
    719       1.20  jmcneill 	const bus_size_t lpipend_sz = (8192 + sc->sc_lpi.pic_maxsources) / NBBY;
    720        1.8  jmcneill 	for (int cpuindex = 0; cpuindex < ncpu; cpuindex++) {
    721        1.5  jmcneill 		gicv3_dma_alloc(sc, &sc->sc_lpipend[cpuindex], lpipend_sz, 0x10000);
    722        1.5  jmcneill 		KASSERT((sc->sc_lpipend[cpuindex].segs[0].ds_addr & ~GICR_PENDBASER_Physical_Address) == 0);
    723        1.5  jmcneill 	}
    724        1.5  jmcneill }
    725        1.5  jmcneill 
    726        1.1  jmcneill void
    727        1.1  jmcneill gicv3_irq_handler(void *frame)
    728        1.1  jmcneill {
    729        1.1  jmcneill 	struct cpu_info * const ci = curcpu();
    730        1.1  jmcneill 	struct gicv3_softc * const sc = gicv3_softc;
    731        1.5  jmcneill 	struct pic_softc *pic;
    732        1.1  jmcneill 	const int oldipl = ci->ci_cpl;
    733       1.53  jmcneill 	const int oldhwpl = IPL_TO_HWPL(oldipl);
    734        1.1  jmcneill 
    735        1.1  jmcneill 	ci->ci_data.cpu_nintr++;
    736        1.1  jmcneill 
    737       1.53  jmcneill 	if (ci->ci_hwpl != oldhwpl) {
    738       1.53  jmcneill 		ci->ci_hwpl = oldhwpl;
    739       1.53  jmcneill 		icc_pmr_write(IPL_TO_PMR(sc, oldhwpl));
    740       1.53  jmcneill 		if (oldhwpl == IPL_HIGH) {
    741       1.43  jmcneill 			return;
    742       1.43  jmcneill 		}
    743       1.40  jmcneill 	}
    744       1.40  jmcneill 
    745        1.1  jmcneill 	for (;;) {
    746        1.1  jmcneill 		const uint32_t iar = icc_iar1_read();
    747       1.26     skrll 		dsb(sy);
    748        1.1  jmcneill 		const uint32_t irq = __SHIFTOUT(iar, ICC_IAR_INTID);
    749        1.1  jmcneill 		if (irq == ICC_IAR_INTID_SPURIOUS)
    750        1.1  jmcneill 			break;
    751        1.1  jmcneill 
    752        1.5  jmcneill 		pic = irq >= GIC_LPI_BASE ? &sc->sc_lpi : &sc->sc_pic;
    753        1.5  jmcneill 		if (irq - pic->pic_irqbase >= pic->pic_maxsources)
    754        1.1  jmcneill 			continue;
    755        1.1  jmcneill 
    756        1.5  jmcneill 		struct intrsource * const is = pic->pic_sources[irq - pic->pic_irqbase];
    757        1.1  jmcneill 		KASSERT(is != NULL);
    758        1.1  jmcneill 
    759       1.21  jmcneill 		const bool early_eoi = irq < GIC_LPI_BASE && is->is_type == IST_EDGE;
    760       1.21  jmcneill 
    761        1.1  jmcneill 		const int ipl = is->is_ipl;
    762       1.21  jmcneill 		if (__predict_false(ipl < ci->ci_cpl)) {
    763       1.21  jmcneill 			pic_do_pending_ints(I32_bit, ipl, frame);
    764       1.28  jmcneill 		} else if (ci->ci_cpl != ipl) {
    765       1.53  jmcneill 			KASSERT(ipl >= IPL_VM);
    766       1.40  jmcneill 			icc_pmr_write(IPL_TO_PMR(sc, ipl));
    767       1.42  jmcneill 			ci->ci_hwpl = ci->ci_cpl = ipl;
    768       1.21  jmcneill 		}
    769       1.21  jmcneill 
    770       1.21  jmcneill 		if (early_eoi) {
    771       1.21  jmcneill 			icc_eoi1r_write(iar);
    772       1.26     skrll 			isb();
    773       1.21  jmcneill 		}
    774        1.1  jmcneill 
    775       1.33  jmcneill 		const int64_t nintr = ci->ci_data.cpu_nintr;
    776       1.33  jmcneill 
    777       1.40  jmcneill 		ENABLE_INTERRUPT();
    778        1.1  jmcneill 		pic_dispatch(is, frame);
    779       1.40  jmcneill 		DISABLE_INTERRUPT();
    780        1.1  jmcneill 
    781       1.33  jmcneill 		if (nintr != ci->ci_data.cpu_nintr)
    782       1.33  jmcneill 			ci->ci_intr_preempt.ev_count++;
    783       1.33  jmcneill 
    784       1.21  jmcneill 		if (!early_eoi) {
    785       1.21  jmcneill 			icc_eoi1r_write(iar);
    786       1.26     skrll 			isb();
    787       1.21  jmcneill 		}
    788        1.1  jmcneill 	}
    789        1.1  jmcneill 
    790       1.21  jmcneill 	pic_do_pending_ints(I32_bit, oldipl, frame);
    791        1.1  jmcneill }
    792        1.1  jmcneill 
    793       1.34  jmcneill static bool
    794       1.35  jmcneill gicv3_cpuif_is_nonsecure(struct gicv3_softc *sc)
    795       1.34  jmcneill {
    796       1.35  jmcneill 	/*
    797       1.35  jmcneill 	 * Write 0 to bit7 and see if it sticks. This is only possible if
    798       1.35  jmcneill 	 * we have a non-secure view of the PMR register.
    799       1.35  jmcneill 	 */
    800       1.35  jmcneill 	const uint32_t opmr = icc_pmr_read();
    801       1.35  jmcneill 	icc_pmr_write(0);
    802       1.35  jmcneill 	const uint32_t npmr = icc_pmr_read();
    803       1.35  jmcneill 	icc_pmr_write(opmr);
    804       1.34  jmcneill 
    805       1.35  jmcneill 	return (npmr & GICC_PMR_NONSECURE) == 0;
    806       1.34  jmcneill }
    807       1.34  jmcneill 
    808       1.35  jmcneill static bool
    809       1.35  jmcneill gicv3_dist_is_nonsecure(struct gicv3_softc *sc)
    810       1.19  jmcneill {
    811       1.35  jmcneill 	const uint32_t gicd_ctrl = gicd_read_4(sc, GICD_CTRL);
    812       1.19  jmcneill 
    813       1.35  jmcneill 	/*
    814       1.35  jmcneill 	 * If security is enabled, we have a non-secure view of the IPRIORITYRn
    815       1.35  jmcneill 	 * registers and LPI configuration priority fields.
    816       1.35  jmcneill 	 */
    817       1.35  jmcneill 	return (gicd_ctrl & GICD_CTRL_DS) == 0;
    818       1.19  jmcneill }
    819       1.19  jmcneill 
    820       1.35  jmcneill /*
    821       1.35  jmcneill  * Rockchip RK3399 provides a different view of int priority registers
    822       1.35  jmcneill  * depending on which firmware is in use. This is hard to detect in
    823       1.35  jmcneill  * a way that could possibly break other boards, so only do this
    824       1.35  jmcneill  * detection if we know we are on a RK3399 SoC.
    825       1.35  jmcneill  */
    826       1.35  jmcneill static void
    827       1.35  jmcneill gicv3_quirk_rockchip_rk3399(struct gicv3_softc *sc)
    828       1.19  jmcneill {
    829       1.35  jmcneill 	/* Detect the number of supported PMR bits */
    830       1.35  jmcneill 	icc_pmr_write(0xff);
    831       1.35  jmcneill 	const uint8_t pmrbits = icc_pmr_read();
    832       1.19  jmcneill 
    833       1.35  jmcneill 	/* Detect the number of supported IPRIORITYRn bits */
    834       1.35  jmcneill 	const uint32_t oiprio = gicd_read_4(sc, GICD_IPRIORITYRn(8));
    835       1.35  jmcneill 	gicd_write_4(sc, GICD_IPRIORITYRn(8), oiprio | 0xff);
    836       1.35  jmcneill 	const uint8_t pribits = gicd_read_4(sc, GICD_IPRIORITYRn(8)) & 0xff;
    837       1.35  jmcneill 	gicd_write_4(sc, GICD_IPRIORITYRn(8), oiprio);
    838       1.35  jmcneill 
    839       1.35  jmcneill 	/*
    840       1.35  jmcneill 	 * If we see fewer PMR bits than IPRIORITYRn bits here, it means
    841       1.35  jmcneill 	 * we have a secure view of IPRIORITYRn (this is not supposed to
    842       1.49     skrll 	 * happen!).
    843       1.35  jmcneill 	 */
    844       1.35  jmcneill 	if (pmrbits < pribits) {
    845       1.35  jmcneill 		aprint_verbose_dev(sc->sc_dev,
    846       1.35  jmcneill 		    "buggy RK3399 firmware detected; applying workaround\n");
    847       1.35  jmcneill 		sc->sc_priority_shift = GIC_PRIO_SHIFT_S;
    848       1.35  jmcneill 	}
    849       1.19  jmcneill }
    850       1.19  jmcneill 
    851        1.1  jmcneill int
    852        1.1  jmcneill gicv3_init(struct gicv3_softc *sc)
    853        1.1  jmcneill {
    854       1.47  jmcneill 	CPU_INFO_ITERATOR cii;
    855       1.47  jmcneill 	struct cpu_info *ci;
    856        1.1  jmcneill 
    857        1.1  jmcneill 	KASSERT(CPU_IS_PRIMARY(curcpu()));
    858        1.1  jmcneill 
    859        1.7  jmcneill 	LIST_INIT(&sc->sc_lpi_callbacks);
    860        1.5  jmcneill 
    861       1.47  jmcneill 	/* Store route to CPU for SPIs */
    862       1.39  jmcneill 	sc->sc_irouter = kmem_zalloc(sizeof(*sc->sc_irouter) * ncpu, KM_SLEEP);
    863       1.47  jmcneill 	for (CPU_INFO_FOREACH(cii, ci)) {
    864       1.47  jmcneill 		KASSERT(cpu_index(ci) < ncpu);
    865       1.47  jmcneill 		sc->sc_irouter[cpu_index(ci)] = ci->ci_cpuid;
    866       1.47  jmcneill 	}
    867        1.6  jmcneill 
    868       1.36  jmcneill 	sc->sc_gicd_typer = gicd_read_4(sc, GICD_TYPER);
    869       1.36  jmcneill 
    870       1.35  jmcneill 	/*
    871       1.37  jmcneill 	 * We don't always have a consistent view of priorities between the
    872       1.35  jmcneill 	 * CPU interface (ICC_PMR_EL1) and the GICD/GICR registers. Detect
    873       1.35  jmcneill 	 * if we are making secure or non-secure accesses to each, and adjust
    874       1.35  jmcneill 	 * the values that we write to each accordingly.
    875       1.35  jmcneill 	 */
    876       1.35  jmcneill 	const bool dist_ns = gicv3_dist_is_nonsecure(sc);
    877       1.35  jmcneill 	sc->sc_priority_shift = dist_ns ? GIC_PRIO_SHIFT_NS : GIC_PRIO_SHIFT_S;
    878       1.35  jmcneill 	const bool cpuif_ns = gicv3_cpuif_is_nonsecure(sc);
    879       1.35  jmcneill 	sc->sc_pmr_shift = cpuif_ns ? GIC_PRIO_SHIFT_NS : GIC_PRIO_SHIFT_S;
    880       1.34  jmcneill 
    881       1.35  jmcneill 	if ((sc->sc_quirks & GICV3_QUIRK_RK3399) != 0)
    882       1.35  jmcneill 		gicv3_quirk_rockchip_rk3399(sc);
    883       1.19  jmcneill 
    884       1.34  jmcneill 	aprint_verbose_dev(sc->sc_dev,
    885       1.35  jmcneill 	    "iidr 0x%08x, cpuif %ssecure, dist %ssecure, "
    886       1.35  jmcneill 	    "priority shift %d, pmr shift %d, quirks %#x\n",
    887       1.35  jmcneill 	    gicd_read_4(sc, GICD_IIDR),
    888       1.35  jmcneill 	    cpuif_ns ? "non-" : "",
    889       1.35  jmcneill 	    dist_ns ? "non-" : "",
    890       1.35  jmcneill 	    sc->sc_priority_shift,
    891       1.35  jmcneill 	    sc->sc_pmr_shift,
    892       1.35  jmcneill 	    sc->sc_quirks);
    893       1.18  jmcneill 
    894        1.1  jmcneill 	sc->sc_pic.pic_ops = &gicv3_picops;
    895       1.36  jmcneill 	sc->sc_pic.pic_maxsources = GICD_TYPER_LINES(sc->sc_gicd_typer);
    896        1.1  jmcneill 	snprintf(sc->sc_pic.pic_name, sizeof(sc->sc_pic.pic_name), "gicv3");
    897        1.1  jmcneill #ifdef MULTIPROCESSOR
    898        1.1  jmcneill 	sc->sc_pic.pic_cpus = kcpuset_running;
    899        1.1  jmcneill #endif
    900        1.1  jmcneill 	pic_add(&sc->sc_pic, 0);
    901        1.1  jmcneill 
    902       1.36  jmcneill 	if ((sc->sc_gicd_typer & GICD_TYPER_LPIS) != 0) {
    903       1.39  jmcneill 		sc->sc_lpipend = kmem_zalloc(sizeof(*sc->sc_lpipend) * ncpu, KM_SLEEP);
    904       1.39  jmcneill 		sc->sc_processor_id = kmem_zalloc(sizeof(*sc->sc_processor_id) * ncpu, KM_SLEEP);
    905       1.39  jmcneill 
    906        1.5  jmcneill 		sc->sc_lpi.pic_ops = &gicv3_lpiops;
    907        1.5  jmcneill 		sc->sc_lpi.pic_maxsources = 8192;	/* Min. required by GICv3 spec */
    908        1.5  jmcneill 		snprintf(sc->sc_lpi.pic_name, sizeof(sc->sc_lpi.pic_name), "gicv3-lpi");
    909        1.5  jmcneill 		pic_add(&sc->sc_lpi, GIC_LPI_BASE);
    910        1.5  jmcneill 
    911       1.23  jmcneill 		sc->sc_lpi_pool = vmem_create("gicv3-lpi", 0, sc->sc_lpi.pic_maxsources,
    912       1.23  jmcneill 		    1, NULL, NULL, NULL, 0, VM_SLEEP, IPL_HIGH);
    913       1.23  jmcneill 		if (sc->sc_lpi_pool == NULL)
    914       1.23  jmcneill 			panic("failed to create gicv3 lpi pool\n");
    915       1.23  jmcneill 
    916        1.5  jmcneill 		gicv3_lpi_init(sc);
    917        1.5  jmcneill 	}
    918        1.5  jmcneill 
    919        1.1  jmcneill 	KASSERT(gicv3_softc == NULL);
    920        1.1  jmcneill 	gicv3_softc = sc;
    921        1.1  jmcneill 
    922        1.1  jmcneill 	for (int i = 0; i < sc->sc_bsh_r_count; i++) {
    923        1.1  jmcneill 		const uint64_t gicr_typer = gicr_read_8(sc, i, GICR_TYPER);
    924        1.1  jmcneill 		const u_int aff0 = __SHIFTOUT(gicr_typer, GICR_TYPER_Affinity_Value_Aff0);
    925        1.1  jmcneill 		const u_int aff1 = __SHIFTOUT(gicr_typer, GICR_TYPER_Affinity_Value_Aff1);
    926        1.1  jmcneill 		const u_int aff2 = __SHIFTOUT(gicr_typer, GICR_TYPER_Affinity_Value_Aff2);
    927        1.1  jmcneill 		const u_int aff3 = __SHIFTOUT(gicr_typer, GICR_TYPER_Affinity_Value_Aff3);
    928        1.1  jmcneill 
    929        1.1  jmcneill 		aprint_debug_dev(sc->sc_dev, "redist %d: cpu %d.%d.%d.%d\n",
    930        1.1  jmcneill 		    i, aff3, aff2, aff1, aff0);
    931        1.1  jmcneill 	}
    932        1.1  jmcneill 
    933        1.1  jmcneill 	gicv3_dist_enable(sc);
    934        1.1  jmcneill 
    935        1.1  jmcneill 	gicv3_cpu_init(&sc->sc_pic, curcpu());
    936       1.36  jmcneill 	if ((sc->sc_gicd_typer & GICD_TYPER_LPIS) != 0)
    937        1.5  jmcneill 		gicv3_lpi_cpu_init(&sc->sc_lpi, curcpu());
    938        1.1  jmcneill 
    939        1.1  jmcneill #ifdef MULTIPROCESSOR
    940       1.11  jmcneill 	intr_establish_xname(IPI_AST, IPL_VM, IST_MPSAFE | IST_EDGE, pic_ipi_ast, (void *)-1, "IPI ast");
    941       1.11  jmcneill 	intr_establish_xname(IPI_XCALL, IPL_HIGH, IST_MPSAFE | IST_EDGE, pic_ipi_xcall, (void *)-1, "IPI xcall");
    942       1.11  jmcneill 	intr_establish_xname(IPI_GENERIC, IPL_HIGH, IST_MPSAFE | IST_EDGE, pic_ipi_generic, (void *)-1, "IPI generic");
    943       1.11  jmcneill 	intr_establish_xname(IPI_NOP, IPL_VM, IST_MPSAFE | IST_EDGE, pic_ipi_nop, (void *)-1, "IPI nop");
    944       1.11  jmcneill 	intr_establish_xname(IPI_SHOOTDOWN, IPL_SCHED, IST_MPSAFE | IST_EDGE, pic_ipi_shootdown, (void *)-1, "IPI shootdown");
    945        1.1  jmcneill #ifdef DDB
    946       1.11  jmcneill 	intr_establish_xname(IPI_DDB, IPL_HIGH, IST_MPSAFE | IST_EDGE, pic_ipi_ddb, NULL, "IPI ddb");
    947        1.1  jmcneill #endif
    948        1.1  jmcneill #ifdef __HAVE_PREEMPTION
    949       1.11  jmcneill 	intr_establish_xname(IPI_KPREEMPT, IPL_VM, IST_MPSAFE | IST_EDGE, pic_ipi_kpreempt, (void *)-1, "IPI kpreempt");
    950        1.1  jmcneill #endif
    951        1.1  jmcneill #endif
    952        1.1  jmcneill 
    953        1.1  jmcneill 	return 0;
    954        1.1  jmcneill }
    955