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