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