Home | History | Annotate | Line # | Download | only in cortex
gic.c revision 1.5.2.2
      1  1.5.2.2  matt /*	$NetBSD: gic.c,v 1.5.2.2 2014/02/15 16:18:36 matt Exp $	*/
      2  1.5.2.2  matt /*-
      3  1.5.2.2  matt  * Copyright (c) 2012 The NetBSD Foundation, Inc.
      4  1.5.2.2  matt  * All rights reserved.
      5  1.5.2.2  matt  *
      6  1.5.2.2  matt  * This code is derived from software contributed to The NetBSD Foundation
      7  1.5.2.2  matt  * by Matt Thomas of 3am Software Foundry.
      8  1.5.2.2  matt  *
      9  1.5.2.2  matt  * Redistribution and use in source and binary forms, with or without
     10  1.5.2.2  matt  * modification, are permitted provided that the following conditions
     11  1.5.2.2  matt  * are met:
     12  1.5.2.2  matt  * 1. Redistributions of source code must retain the above copyright
     13  1.5.2.2  matt  *    notice, this list of conditions and the following disclaimer.
     14  1.5.2.2  matt  * 2. Redistributions in binary form must reproduce the above copyright
     15  1.5.2.2  matt  *    notice, this list of conditions and the following disclaimer in the
     16  1.5.2.2  matt  *    documentation and/or other materials provided with the distribution.
     17  1.5.2.2  matt  *
     18  1.5.2.2  matt  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     19  1.5.2.2  matt  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     20  1.5.2.2  matt  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     21  1.5.2.2  matt  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     22  1.5.2.2  matt  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     23  1.5.2.2  matt  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     24  1.5.2.2  matt  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     25  1.5.2.2  matt  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     26  1.5.2.2  matt  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     27  1.5.2.2  matt  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     28  1.5.2.2  matt  * POSSIBILITY OF SUCH DAMAGE.
     29  1.5.2.2  matt  */
     30  1.5.2.2  matt 
     31  1.5.2.2  matt #define _INTR_PRIVATE
     32  1.5.2.2  matt 
     33  1.5.2.2  matt #include <sys/cdefs.h>
     34  1.5.2.2  matt __KERNEL_RCSID(0, "$NetBSD: gic.c,v 1.5.2.2 2014/02/15 16:18:36 matt Exp $");
     35  1.5.2.2  matt 
     36  1.5.2.2  matt #include <sys/param.h>
     37  1.5.2.2  matt #include <sys/bus.h>
     38  1.5.2.2  matt #include <sys/device.h>
     39  1.5.2.2  matt #include <sys/evcnt.h>
     40  1.5.2.2  matt #include <sys/intr.h>
     41  1.5.2.2  matt #include <sys/proc.h>
     42  1.5.2.2  matt #include <sys/xcall.h>		/* for xc_ipi_handler */
     43  1.5.2.2  matt 
     44  1.5.2.2  matt #include <arm/armreg.h>
     45  1.5.2.2  matt #include <arm/cpufunc.h>
     46  1.5.2.2  matt #include <arm/atomic.h>
     47  1.5.2.2  matt 
     48  1.5.2.2  matt #include <arm/cortex/gic_reg.h>
     49  1.5.2.2  matt #include <arm/cortex/mpcore_var.h>
     50  1.5.2.2  matt 
     51  1.5.2.2  matt #define	ARMGIC_SGI_IPIBASE	(16 - NIPI)
     52  1.5.2.2  matt 
     53  1.5.2.2  matt static int armgic_match(device_t, cfdata_t, void *);
     54  1.5.2.2  matt static void armgic_attach(device_t, device_t, void *);
     55  1.5.2.2  matt 
     56  1.5.2.2  matt static void armgic_set_priority(struct pic_softc *, int);
     57  1.5.2.2  matt static void armgic_unblock_irqs(struct pic_softc *, size_t, uint32_t);
     58  1.5.2.2  matt static void armgic_block_irqs(struct pic_softc *, size_t, uint32_t);
     59  1.5.2.2  matt static void armgic_establish_irq(struct pic_softc *, struct intrsource *);
     60  1.5.2.2  matt #if 0
     61  1.5.2.2  matt static void armgic_source_name(struct pic_softc *, int, char *, size_t);
     62  1.5.2.2  matt #endif
     63  1.5.2.2  matt 
     64  1.5.2.2  matt #ifdef MULTIPROCESSOR
     65  1.5.2.2  matt static void armgic_cpu_init(struct pic_softc *, struct cpu_info *);
     66  1.5.2.2  matt static void armgic_ipi_send(struct pic_softc *, const kcpuset_t *, u_long);
     67  1.5.2.2  matt #endif
     68  1.5.2.2  matt 
     69  1.5.2.2  matt static const struct pic_ops armgic_picops = {
     70  1.5.2.2  matt 	.pic_unblock_irqs = armgic_unblock_irqs,
     71  1.5.2.2  matt 	.pic_block_irqs = armgic_block_irqs,
     72  1.5.2.2  matt 	.pic_establish_irq = armgic_establish_irq,
     73  1.5.2.2  matt #if 0
     74  1.5.2.2  matt 	.pic_source_name = armgic_source_name,
     75  1.5.2.2  matt #endif
     76  1.5.2.2  matt 	.pic_set_priority = armgic_set_priority,
     77  1.5.2.2  matt #ifdef MULTIPROCESSOR
     78  1.5.2.2  matt 	.pic_cpu_init = armgic_cpu_init,
     79  1.5.2.2  matt 	.pic_ipi_send = armgic_ipi_send,
     80  1.5.2.2  matt #endif
     81  1.5.2.2  matt };
     82  1.5.2.2  matt 
     83  1.5.2.2  matt #define	PICTOSOFTC(pic)		((struct armgic_softc *)(pic))
     84  1.5.2.2  matt 
     85  1.5.2.2  matt static struct armgic_softc {
     86  1.5.2.2  matt 	struct pic_softc sc_pic;
     87  1.5.2.2  matt 	device_t sc_dev;
     88  1.5.2.2  matt 	bus_space_tag_t sc_memt;
     89  1.5.2.2  matt 	bus_space_handle_t sc_gicch;
     90  1.5.2.2  matt 	bus_space_handle_t sc_gicdh;
     91  1.5.2.2  matt 	size_t sc_gic_lines;
     92  1.5.2.2  matt 	uint32_t sc_gic_type;
     93  1.5.2.2  matt 	uint32_t sc_gic_valid_lines[1024/32];
     94  1.5.2.2  matt 	uint32_t sc_enabled_local;
     95  1.5.2.2  matt } armgic_softc = {
     96  1.5.2.2  matt 	.sc_pic = {
     97  1.5.2.2  matt 		.pic_ops = &armgic_picops,
     98  1.5.2.2  matt 		.pic_name = "armgic",
     99  1.5.2.2  matt 	},
    100  1.5.2.2  matt };
    101  1.5.2.2  matt 
    102  1.5.2.2  matt static struct intrsource armgic_dummy_source;
    103  1.5.2.2  matt 
    104  1.5.2.2  matt __CTASSERT(NIPL == 8);
    105  1.5.2.2  matt 
    106  1.5.2.2  matt /*
    107  1.5.2.2  matt  * GIC register are always in little-endian.
    108  1.5.2.2  matt  */
    109  1.5.2.2  matt static inline uint32_t
    110  1.5.2.2  matt gicc_read(struct armgic_softc *sc, bus_size_t o)
    111  1.5.2.2  matt {
    112  1.5.2.2  matt 	uint32_t v = bus_space_read_4(sc->sc_memt, sc->sc_gicch, o);
    113  1.5.2.2  matt 	return le32toh(v);
    114  1.5.2.2  matt }
    115  1.5.2.2  matt 
    116  1.5.2.2  matt static inline void
    117  1.5.2.2  matt gicc_write(struct armgic_softc *sc, bus_size_t o, uint32_t v)
    118  1.5.2.2  matt {
    119  1.5.2.2  matt 	v = htole32(v);
    120  1.5.2.2  matt 	bus_space_write_4(sc->sc_memt, sc->sc_gicch, o, v);
    121  1.5.2.2  matt }
    122  1.5.2.2  matt 
    123  1.5.2.2  matt static inline uint32_t
    124  1.5.2.2  matt gicd_read(struct armgic_softc *sc, bus_size_t o)
    125  1.5.2.2  matt {
    126  1.5.2.2  matt 	uint32_t v = bus_space_read_4(sc->sc_memt, sc->sc_gicdh, o);
    127  1.5.2.2  matt 	return le32toh(v);
    128  1.5.2.2  matt }
    129  1.5.2.2  matt 
    130  1.5.2.2  matt static inline void
    131  1.5.2.2  matt gicd_write(struct armgic_softc *sc, bus_size_t o, uint32_t v)
    132  1.5.2.2  matt {
    133  1.5.2.2  matt 	v = htole32(v);
    134  1.5.2.2  matt 	bus_space_write_4(sc->sc_memt, sc->sc_gicdh, o, v);
    135  1.5.2.2  matt }
    136  1.5.2.2  matt 
    137  1.5.2.2  matt /*
    138  1.5.2.2  matt  * In the GIC prioritization scheme, lower numbers have higher priority.
    139  1.5.2.2  matt  */
    140  1.5.2.2  matt static inline uint32_t
    141  1.5.2.2  matt armgic_ipl_to_priority(int ipl)
    142  1.5.2.2  matt {
    143  1.5.2.2  matt 	return (IPL_HIGH - ipl) * GICC_PMR_PRIORITIES / NIPL;
    144  1.5.2.2  matt }
    145  1.5.2.2  matt 
    146  1.5.2.2  matt #if 0
    147  1.5.2.2  matt static inline int
    148  1.5.2.2  matt armgic_priority_to_ipl(uint32_t priority)
    149  1.5.2.2  matt {
    150  1.5.2.2  matt 	return IPL_HIGH - priority * NIPL / GICC_PMR_PRIORITIES;
    151  1.5.2.2  matt }
    152  1.5.2.2  matt #endif
    153  1.5.2.2  matt 
    154  1.5.2.2  matt static void
    155  1.5.2.2  matt armgic_unblock_irqs(struct pic_softc *pic, size_t irq_base, uint32_t irq_mask)
    156  1.5.2.2  matt {
    157  1.5.2.2  matt 	struct armgic_softc * const sc = PICTOSOFTC(pic);
    158  1.5.2.2  matt 	const size_t group = irq_base / 32;
    159  1.5.2.2  matt 
    160  1.5.2.2  matt 	if (group == 0)
    161  1.5.2.2  matt 		sc->sc_enabled_local |= irq_mask;
    162  1.5.2.2  matt 
    163  1.5.2.2  matt 	gicd_write(sc, GICD_ISENABLERn(group), irq_mask);
    164  1.5.2.2  matt }
    165  1.5.2.2  matt 
    166  1.5.2.2  matt static void
    167  1.5.2.2  matt armgic_block_irqs(struct pic_softc *pic, size_t irq_base, uint32_t irq_mask)
    168  1.5.2.2  matt {
    169  1.5.2.2  matt 	struct armgic_softc * const sc = PICTOSOFTC(pic);
    170  1.5.2.2  matt 	const size_t group = irq_base / 32;
    171  1.5.2.2  matt 
    172  1.5.2.2  matt 	if (group == 0)
    173  1.5.2.2  matt 		sc->sc_enabled_local &= ~irq_mask;
    174  1.5.2.2  matt 
    175  1.5.2.2  matt 	gicd_write(sc, GICD_ICENABLERn(group), irq_mask);
    176  1.5.2.2  matt }
    177  1.5.2.2  matt 
    178  1.5.2.2  matt static uint32_t armgic_last_priority;
    179  1.5.2.2  matt 
    180  1.5.2.2  matt static void
    181  1.5.2.2  matt armgic_set_priority(struct pic_softc *pic, int ipl)
    182  1.5.2.2  matt {
    183  1.5.2.2  matt 	struct armgic_softc * const sc = PICTOSOFTC(pic);
    184  1.5.2.2  matt 
    185  1.5.2.2  matt 	const uint32_t priority = armgic_ipl_to_priority(ipl);
    186  1.5.2.2  matt 	gicc_write(sc, GICC_PMR, priority);
    187  1.5.2.2  matt 	armgic_last_priority = priority;
    188  1.5.2.2  matt }
    189  1.5.2.2  matt 
    190  1.5.2.2  matt #ifdef __HAVE_PIC_FAST_SOFTINTS
    191  1.5.2.2  matt void
    192  1.5.2.2  matt softint_init_md(lwp_t *l, u_int level, uintptr_t *machdep_p)
    193  1.5.2.2  matt {
    194  1.5.2.2  matt 	lwp_t **lp = &l->l_cpu->ci_softlwps[level];
    195  1.5.2.2  matt 	KASSERT(*lp == NULL || *lp == l);
    196  1.5.2.2  matt 	*lp = l;
    197  1.5.2.2  matt 	/*
    198  1.5.2.2  matt 	 * Really easy.  Just tell it to trigger the local CPU.
    199  1.5.2.2  matt 	 */
    200  1.5.2.2  matt 	*machdep_p = GICD_SGIR_TargetListFilter_Me
    201  1.5.2.2  matt 	    | __SHIFTIN(level, GICD_SGIR_SGIINTID);
    202  1.5.2.2  matt }
    203  1.5.2.2  matt 
    204  1.5.2.2  matt void
    205  1.5.2.2  matt softint_trigger(uintptr_t machdep)
    206  1.5.2.2  matt {
    207  1.5.2.2  matt 
    208  1.5.2.2  matt 	gicd_write(&armgic_softc, GICD_SGIR, machdep);
    209  1.5.2.2  matt }
    210  1.5.2.2  matt #endif
    211  1.5.2.2  matt 
    212  1.5.2.2  matt void
    213  1.5.2.2  matt armgic_irq_handler(void *tf)
    214  1.5.2.2  matt {
    215  1.5.2.2  matt 	struct cpu_info * const ci = curcpu();
    216  1.5.2.2  matt 	struct armgic_softc * const sc = &armgic_softc;
    217  1.5.2.2  matt 	const int old_ipl = ci->ci_cpl;
    218  1.5.2.2  matt #ifdef DIAGNOSTIC
    219  1.5.2.2  matt 	const int old_mtx_count = ci->ci_mtx_count;
    220  1.5.2.2  matt 	const int old_l_biglocks = ci->ci_curlwp->l_biglocks;
    221  1.5.2.2  matt #endif
    222  1.5.2.2  matt #ifdef DEBUG
    223  1.5.2.2  matt 	size_t n = 0;
    224  1.5.2.2  matt #endif
    225  1.5.2.2  matt 
    226  1.5.2.2  matt 	ci->ci_data.cpu_nintr++;
    227  1.5.2.2  matt 
    228  1.5.2.2  matt 	KASSERTMSG(old_ipl != IPL_HIGH, "old_ipl %d pmr %#x hppir %#x",
    229  1.5.2.2  matt 	    old_ipl, gicc_read(sc, GICC_PMR), gicc_read(sc, GICC_HPPIR));
    230  1.5.2.2  matt #if 0
    231  1.5.2.2  matt 	printf("%s(enter): %s: pmr=%u hppir=%u\n",
    232  1.5.2.2  matt 	    __func__, ci->ci_data.cpu_name,
    233  1.5.2.2  matt 	    gicc_read(sc, GICC_PMR),
    234  1.5.2.2  matt 	    gicc_read(sc, GICC_HPPIR));
    235  1.5.2.2  matt #elif 0
    236  1.5.2.2  matt 	printf("(%u:%d", ci->ci_index, old_ipl);
    237  1.5.2.2  matt #endif
    238  1.5.2.2  matt 
    239  1.5.2.2  matt 	for (;;) {
    240  1.5.2.2  matt 		uint32_t iar = gicc_read(sc, GICC_IAR);
    241  1.5.2.2  matt 		uint32_t irq = __SHIFTOUT(iar, GICC_IAR_IRQ);
    242  1.5.2.2  matt 		//printf(".%u", irq);
    243  1.5.2.2  matt 		if (irq == GICC_IAR_IRQ_SPURIOUS) {
    244  1.5.2.2  matt 			iar = gicc_read(sc, GICC_IAR);
    245  1.5.2.2  matt 			irq = __SHIFTOUT(iar, GICC_IAR_IRQ);
    246  1.5.2.2  matt 			if (irq == GICC_IAR_IRQ_SPURIOUS)
    247  1.5.2.2  matt 				break;
    248  1.5.2.2  matt 			//printf(".%u", irq);
    249  1.5.2.2  matt 		}
    250  1.5.2.2  matt 
    251  1.5.2.2  matt 		//const uint32_t cpuid = __SHIFTOUT(iar, GICC_IAR_CPUID_MASK);
    252  1.5.2.2  matt 		struct intrsource * const is = sc->sc_pic.pic_sources[irq];
    253  1.5.2.2  matt 		KASSERT(is != &armgic_dummy_source);
    254  1.5.2.2  matt 
    255  1.5.2.2  matt 		/*
    256  1.5.2.2  matt 		 * GIC has asserted IPL for us so we can just update ci_cpl.
    257  1.5.2.2  matt 		 *
    258  1.5.2.2  matt 		 * But it's not that simple.  We may have already bumped ci_cpl
    259  1.5.2.2  matt 		 * due to a high priority interrupt and now we are about to
    260  1.5.2.2  matt 		 * dispatch one lower than the previous.  It's possible for
    261  1.5.2.2  matt 		 * that previous interrupt to have deferred some interrupts
    262  1.5.2.2  matt 		 * so we need deal with those when lowering to the current
    263  1.5.2.2  matt 		 * interrupt's ipl.
    264  1.5.2.2  matt 		 *
    265  1.5.2.2  matt 		 * However, if are just raising ipl, we can just update ci_cpl.
    266  1.5.2.2  matt 		 */
    267  1.5.2.2  matt #if 0
    268  1.5.2.2  matt 		const int ipl = armgic_priority_to_ipl(gicc_read(sc, GICC_RPR));
    269  1.5.2.2  matt 		KASSERTMSG(panicstr != NULL || ipl == is->is_ipl,
    270  1.5.2.2  matt 		    "%s: irq %d: running ipl %d != source ipl %u",
    271  1.5.2.2  matt 		    ci->ci_data.cpu_name, irq, ipl, is->is_ipl);
    272  1.5.2.2  matt #else
    273  1.5.2.2  matt 		const int ipl = is->is_ipl;
    274  1.5.2.2  matt #endif
    275  1.5.2.2  matt 		if (__predict_false(ipl < ci->ci_cpl)) {
    276  1.5.2.2  matt 			//printf("<");
    277  1.5.2.2  matt 			pic_do_pending_ints(I32_bit, ipl, tf);
    278  1.5.2.2  matt 			KASSERT(ci->ci_cpl == ipl);
    279  1.5.2.2  matt 		} else {
    280  1.5.2.2  matt 			KASSERTMSG(ipl > ci->ci_cpl, "ipl %d cpl %d hw-ipl %#x",
    281  1.5.2.2  matt 			    ipl, ci->ci_cpl,
    282  1.5.2.2  matt 			    gicc_read(sc, GICC_PMR));
    283  1.5.2.2  matt 			//printf(">");
    284  1.5.2.2  matt 			gicc_write(sc, GICC_PMR, armgic_ipl_to_priority(ipl));
    285  1.5.2.2  matt 			ci->ci_cpl = ipl;
    286  1.5.2.2  matt 		}
    287  1.5.2.2  matt 		//printf("$");
    288  1.5.2.2  matt 		cpsie(I32_bit);
    289  1.5.2.2  matt 		pic_dispatch(is, tf);
    290  1.5.2.2  matt 		cpsid(I32_bit);
    291  1.5.2.2  matt 		gicc_write(sc, GICC_EOIR, iar);
    292  1.5.2.2  matt #ifdef DEBUG
    293  1.5.2.2  matt 		n++;
    294  1.5.2.2  matt 		KDASSERTMSG(n < 5, "%s: processed too many (%zu)",
    295  1.5.2.2  matt 		    ci->ci_data.cpu_name, n);
    296  1.5.2.2  matt #endif
    297  1.5.2.2  matt 	}
    298  1.5.2.2  matt 
    299  1.5.2.2  matt 	// printf("%s(%p): exit (%zu dispatched)\n", __func__, tf, n);
    300  1.5.2.2  matt 	/*
    301  1.5.2.2  matt 	 * Now handle any pending ints.
    302  1.5.2.2  matt 	 */
    303  1.5.2.2  matt 	//printf("!");
    304  1.5.2.2  matt 	KASSERT(old_ipl != IPL_HIGH);
    305  1.5.2.2  matt 	pic_do_pending_ints(I32_bit, old_ipl, tf);
    306  1.5.2.2  matt 	KASSERTMSG(ci->ci_cpl == old_ipl, "ci_cpl %d old_ipl %d", ci->ci_cpl, old_ipl);
    307  1.5.2.2  matt 	KASSERT(old_mtx_count == ci->ci_mtx_count);
    308  1.5.2.2  matt 	KASSERT(old_l_biglocks == ci->ci_curlwp->l_biglocks);
    309  1.5.2.2  matt #if 0
    310  1.5.2.2  matt 	printf("%s(exit): %s(%d): pmr=%u hppir=%u\n",
    311  1.5.2.2  matt 	    __func__, ci->ci_data.cpu_name, ci->ci_cpl,
    312  1.5.2.2  matt 	    gicc_read(sc, GICC_PMR),
    313  1.5.2.2  matt 	    gicc_read(sc, GICC_HPPIR));
    314  1.5.2.2  matt #elif 0
    315  1.5.2.2  matt 	printf("->%#x)", ((struct trapframe *)tf)->tf_pc);
    316  1.5.2.2  matt #endif
    317  1.5.2.2  matt }
    318  1.5.2.2  matt 
    319  1.5.2.2  matt void
    320  1.5.2.2  matt armgic_establish_irq(struct pic_softc *pic, struct intrsource *is)
    321  1.5.2.2  matt {
    322  1.5.2.2  matt 	struct armgic_softc * const sc = PICTOSOFTC(pic);
    323  1.5.2.2  matt 	const size_t group = is->is_irq / 32;
    324  1.5.2.2  matt 	const u_int irq = is->is_irq & 31;
    325  1.5.2.2  matt 	const u_int byte_shift = 8 * (irq & 3);
    326  1.5.2.2  matt 	const u_int twopair_shift = 2 * (irq & 15);
    327  1.5.2.2  matt 
    328  1.5.2.2  matt 	KASSERTMSG(sc->sc_gic_valid_lines[group] & __BIT(irq),
    329  1.5.2.2  matt 	    "irq %u: not valid (group[%zu]=0x%08x [0x%08x])",
    330  1.5.2.2  matt 	    is->is_irq, group, sc->sc_gic_valid_lines[group],
    331  1.5.2.2  matt 	    (uint32_t)__BIT(irq));
    332  1.5.2.2  matt 
    333  1.5.2.2  matt 	KASSERTMSG(is->is_type == IST_LEVEL || is->is_type == IST_EDGE,
    334  1.5.2.2  matt 	    "irq %u: type %u unsupported", is->is_irq, is->is_type);
    335  1.5.2.2  matt 
    336  1.5.2.2  matt 	const bus_size_t targets_reg = GICD_ITARGETSRn(is->is_irq / 4);
    337  1.5.2.2  matt 	const bus_size_t cfg_reg = GICD_ICFGRn(is->is_irq / 16);
    338  1.5.2.2  matt 	uint32_t targets = gicd_read(sc, targets_reg);
    339  1.5.2.2  matt 	uint32_t cfg = gicd_read(sc, cfg_reg);
    340  1.5.2.2  matt 
    341  1.5.2.2  matt 	if (group > 0) {
    342  1.5.2.2  matt 		/*
    343  1.5.2.2  matt 		 * There are 4 irqs per TARGETS register.  For now bind
    344  1.5.2.2  matt 		 * to the primary cpu.
    345  1.5.2.2  matt 		 */
    346  1.5.2.2  matt 		targets &= ~(0xff << byte_shift);
    347  1.5.2.2  matt 		targets |= 1 << byte_shift;
    348  1.5.2.2  matt 		gicd_write(sc, targets_reg, targets);
    349  1.5.2.2  matt 
    350  1.5.2.2  matt 		/*
    351  1.5.2.2  matt 		 * There are 16 irqs per CFG register.  10=EDGE 00=LEVEL
    352  1.5.2.2  matt 		 */
    353  1.5.2.2  matt 		uint32_t new_cfg = cfg;
    354  1.5.2.2  matt 		uint32_t old_cfg = (cfg >> twopair_shift) & 3;
    355  1.5.2.2  matt 		if (is->is_type == IST_LEVEL && (old_cfg & 2) != 0) {
    356  1.5.2.2  matt 			new_cfg &= ~(3 << twopair_shift);
    357  1.5.2.2  matt 		} else if (is->is_type == IST_EDGE && (old_cfg & 2) == 0) {
    358  1.5.2.2  matt 			new_cfg |= 2 << twopair_shift;
    359  1.5.2.2  matt 		}
    360  1.5.2.2  matt 		if (new_cfg != cfg) {
    361  1.5.2.2  matt 			gicd_write(sc, cfg_reg, cfg);
    362  1.5.2.2  matt #if 0
    363  1.5.2.2  matt 			printf("%s: irq %u: cfg changed from %#x to %#x\n",
    364  1.5.2.2  matt 			    pic->pic_name, is->is_irq, cfg, new_cfg);
    365  1.5.2.2  matt #endif
    366  1.5.2.2  matt 		}
    367  1.5.2.2  matt 	}
    368  1.5.2.2  matt 
    369  1.5.2.2  matt 	/*
    370  1.5.2.2  matt 	 * There are 4 irqs per PRIORITY register.  Map the IPL
    371  1.5.2.2  matt 	 * to GIC priority.
    372  1.5.2.2  matt 	 */
    373  1.5.2.2  matt 	const bus_size_t priority_reg = GICD_IPRIORITYRn(is->is_irq / 4);
    374  1.5.2.2  matt 	uint32_t priority = gicd_read(sc, priority_reg);
    375  1.5.2.2  matt 	priority &= ~(0xff << byte_shift);
    376  1.5.2.2  matt 	priority |= armgic_ipl_to_priority(is->is_ipl) << byte_shift;
    377  1.5.2.2  matt 	gicd_write(sc, priority_reg, priority);
    378  1.5.2.2  matt 
    379  1.5.2.2  matt #if 0
    380  1.5.2.2  matt 	printf("%s: irq %u: target %#x cfg %u priority %#x (%u)\n",
    381  1.5.2.2  matt 	    pic->pic_name, is->is_irq, (targets >> byte_shift) & 0xff,
    382  1.5.2.2  matt 	    (cfg >> twopair_shift) & 3, (priority >> byte_shift) & 0xff,
    383  1.5.2.2  matt 	    is->is_ipl);
    384  1.5.2.2  matt #endif
    385  1.5.2.2  matt }
    386  1.5.2.2  matt 
    387  1.5.2.2  matt #ifdef MULTIPROCESSOR
    388  1.5.2.2  matt static void
    389  1.5.2.2  matt armgic_cpu_init_priorities(struct armgic_softc *sc)
    390  1.5.2.2  matt {
    391  1.5.2.2  matt 	uint32_t enabled = sc->sc_enabled_local;
    392  1.5.2.2  matt 	for (size_t i = 0; i < 32; i += 4, enabled >>= 4) {
    393  1.5.2.2  matt 		/*
    394  1.5.2.2  matt 		 * If there are no enabled interrupts for the priority register,
    395  1.5.2.2  matt 		 * don't bother changing it.
    396  1.5.2.2  matt 		 */
    397  1.5.2.2  matt 		if ((enabled & 0x0f) == 0)
    398  1.5.2.2  matt 			continue;
    399  1.5.2.2  matt 		/*
    400  1.5.2.2  matt 		 * Since priorities are in 3210 order, it'
    401  1.5.2.2  matt 		 */
    402  1.5.2.2  matt 		const bus_size_t priority_reg = GICD_IPRIORITYRn(i / 4);
    403  1.5.2.2  matt 		uint32_t priority = gicd_read(sc, priority_reg);
    404  1.5.2.2  matt 		uint32_t byte_mask = 0xff;
    405  1.5.2.2  matt 		size_t byte_shift = 0;
    406  1.5.2.2  matt 		for (size_t j = 0; j < 4; j++, byte_mask <<= 8, byte_shift += 8) {
    407  1.5.2.2  matt 			struct intrsource * const is = sc->sc_pic.pic_sources[i+j];
    408  1.5.2.2  matt 			if (is == NULL || is == &armgic_dummy_source)
    409  1.5.2.2  matt 				continue;
    410  1.5.2.2  matt 			priority &= ~byte_mask;
    411  1.5.2.2  matt 			priority |= armgic_ipl_to_priority(is->is_ipl) << byte_shift;
    412  1.5.2.2  matt 		}
    413  1.5.2.2  matt 		gicd_write(sc, priority_reg, priority);
    414  1.5.2.2  matt 	}
    415  1.5.2.2  matt }
    416  1.5.2.2  matt 
    417  1.5.2.2  matt void
    418  1.5.2.2  matt armgic_cpu_init(struct pic_softc *pic, struct cpu_info *ci)
    419  1.5.2.2  matt {
    420  1.5.2.2  matt 	struct armgic_softc * const sc = PICTOSOFTC(pic);
    421  1.5.2.2  matt 	if (!CPU_IS_PRIMARY(ci) && sc->sc_enabled_local) {
    422  1.5.2.2  matt 		armgic_cpu_init_priorities(sc);
    423  1.5.2.2  matt 	}
    424  1.5.2.2  matt 	KASSERTMSG(ci->ci_cpl == IPL_HIGH, "ipl %d not IPL_HIGH", ci->ci_cpl);
    425  1.5.2.2  matt 	gicc_write(sc, GICC_PMR, armgic_ipl_to_priority(ci->ci_cpl));	// set PMR
    426  1.5.2.2  matt 	gicc_write(sc, GICC_CTRL, GICC_CTRL_V1_Enable);	// enable interrupt
    427  1.5.2.2  matt 	if (!CPU_IS_PRIMARY(ci) && sc->sc_enabled_local)
    428  1.5.2.2  matt 		gicd_write(sc, GICD_ISENABLERn(0), sc->sc_enabled_local);
    429  1.5.2.2  matt 	cpsie(I32_bit);					// allow IRQ exceptions
    430  1.5.2.2  matt }
    431  1.5.2.2  matt 
    432  1.5.2.2  matt void
    433  1.5.2.2  matt armgic_ipi_send(struct pic_softc *pic, const kcpuset_t *kcp, u_long ipi)
    434  1.5.2.2  matt {
    435  1.5.2.2  matt 	struct armgic_softc * const sc = PICTOSOFTC(pic);
    436  1.5.2.2  matt 
    437  1.5.2.2  matt 	if (ipi == IPI_NOP) {
    438  1.5.2.2  matt 		__asm __volatile("sev");
    439  1.5.2.2  matt 		return;
    440  1.5.2.2  matt 	}
    441  1.5.2.2  matt 
    442  1.5.2.2  matt 	uint32_t targets;
    443  1.5.2.2  matt 	kcpuset_export_u32(kcp, &targets, sizeof(targets));
    444  1.5.2.2  matt 	uint32_t sgir = __SHIFTOUT(ARMGIC_SGI_IPIBASE + ipi, GICD_SGIR_SGIINTID);
    445  1.5.2.2  matt 	sgir |= __SHIFTOUT(targets, GICD_SGIR_TargetList);
    446  1.5.2.2  matt 
    447  1.5.2.2  matt 	printf("%s: %s: %#x", __func__, curcpu()->ci_data.cpu_name, sgir);
    448  1.5.2.2  matt 	gicd_write(sc, GICD_SGIR, sgir);
    449  1.5.2.2  matt 	printf("\n");
    450  1.5.2.2  matt }
    451  1.5.2.2  matt #endif
    452  1.5.2.2  matt 
    453  1.5.2.2  matt int
    454  1.5.2.2  matt armgic_match(device_t parent, cfdata_t cf, void *aux)
    455  1.5.2.2  matt {
    456  1.5.2.2  matt 	struct mpcore_attach_args * const mpcaa = aux;
    457  1.5.2.2  matt 
    458  1.5.2.2  matt 	if (strcmp(cf->cf_name, mpcaa->mpcaa_name) != 0)
    459  1.5.2.2  matt 		return 0;
    460  1.5.2.2  matt 	if (!CPU_ID_CORTEX_P(cputype) || CPU_ID_CORTEX_A8_P(cputype))
    461  1.5.2.2  matt 		return 0;
    462  1.5.2.2  matt 
    463  1.5.2.2  matt 	return 1;
    464  1.5.2.2  matt }
    465  1.5.2.2  matt 
    466  1.5.2.2  matt void
    467  1.5.2.2  matt armgic_attach(device_t parent, device_t self, void *aux)
    468  1.5.2.2  matt {
    469  1.5.2.2  matt 	struct armgic_softc * const sc = &armgic_softc;
    470  1.5.2.2  matt 	struct mpcore_attach_args * const mpcaa = aux;
    471  1.5.2.2  matt 
    472  1.5.2.2  matt 	sc->sc_dev = self;
    473  1.5.2.2  matt 	self->dv_private = sc;
    474  1.5.2.2  matt 
    475  1.5.2.2  matt 	sc->sc_memt = mpcaa->mpcaa_memt;	/* provided for us */
    476  1.5.2.2  matt 	bus_space_subregion(sc->sc_memt, mpcaa->mpcaa_memh, mpcaa->mpcaa_off1,
    477  1.5.2.2  matt 	    4096, &sc->sc_gicdh);
    478  1.5.2.2  matt 	bus_space_subregion(sc->sc_memt, mpcaa->mpcaa_memh, mpcaa->mpcaa_off2,
    479  1.5.2.2  matt 	    4096, &sc->sc_gicch);
    480  1.5.2.2  matt 
    481  1.5.2.2  matt 	sc->sc_gic_type = gicd_read(sc, GICD_TYPER);
    482  1.5.2.2  matt 	sc->sc_pic.pic_maxsources = GICD_TYPER_LINES(sc->sc_gic_type);
    483  1.5.2.2  matt 
    484  1.5.2.2  matt 	gicc_write(sc, GICC_CTRL, 0);	/* disable all interrupts */
    485  1.5.2.2  matt 	gicd_write(sc, GICD_CTRL, 0);	/* disable all interrupts */
    486  1.5.2.2  matt 
    487  1.5.2.2  matt 	gicc_write(sc, GICC_PMR, 0xff);
    488  1.5.2.2  matt 	uint32_t pmr = gicc_read(sc, GICC_PMR);
    489  1.5.2.2  matt 	u_int priorities = 1 << popcount32(pmr);
    490  1.5.2.2  matt 
    491  1.5.2.2  matt 	/*
    492  1.5.2.2  matt 	 * Let's find out how many real sources we have.
    493  1.5.2.2  matt 	 */
    494  1.5.2.2  matt 	for (size_t i = 0, group = 0;
    495  1.5.2.2  matt 	     i < sc->sc_pic.pic_maxsources;
    496  1.5.2.2  matt 	     i += 32, group++) {
    497  1.5.2.2  matt 		/*
    498  1.5.2.2  matt 		 * To figure what sources are real, one enables all interrupts
    499  1.5.2.2  matt 		 * and then reads back the enable mask so which ones really
    500  1.5.2.2  matt 		 * got enabled.
    501  1.5.2.2  matt 		 */
    502  1.5.2.2  matt 		gicd_write(sc, GICD_ISENABLERn(group), 0xffffffff);
    503  1.5.2.2  matt 		uint32_t valid = gicd_read(sc, GICD_ISENABLERn(group));
    504  1.5.2.2  matt 
    505  1.5.2.2  matt 		/*
    506  1.5.2.2  matt 		 * Now disable (clear enable) them again.
    507  1.5.2.2  matt 		 */
    508  1.5.2.2  matt 		gicd_write(sc, GICD_ICENABLERn(group), valid);
    509  1.5.2.2  matt 
    510  1.5.2.2  matt 		/*
    511  1.5.2.2  matt 		 * Count how many are valid.
    512  1.5.2.2  matt 		 */
    513  1.5.2.2  matt 		sc->sc_gic_lines += popcount32(valid);
    514  1.5.2.2  matt 		sc->sc_gic_valid_lines[group] = valid;
    515  1.5.2.2  matt 	}
    516  1.5.2.2  matt 
    517  1.5.2.2  matt 	pic_add(&sc->sc_pic, 0);
    518  1.5.2.2  matt 
    519  1.5.2.2  matt 	/*
    520  1.5.2.2  matt 	 * Force the GICD to IPL_HIGH and then enable interrupts.
    521  1.5.2.2  matt 	 */
    522  1.5.2.2  matt 	struct cpu_info * const ci = curcpu();
    523  1.5.2.2  matt 	KASSERTMSG(ci->ci_cpl == IPL_HIGH, "ipl %d not IPL_HIGH", ci->ci_cpl);
    524  1.5.2.2  matt 	armgic_set_priority(&sc->sc_pic, ci->ci_cpl);	// set PMR
    525  1.5.2.2  matt 	gicd_write(sc, GICD_CTRL, GICD_CTRL_Enable);	// enable Distributer
    526  1.5.2.2  matt 	gicc_write(sc, GICC_CTRL, GICC_CTRL_V1_Enable);	// enable CPU interrupts
    527  1.5.2.2  matt 	cpsie(I32_bit);					// allow interrupt exceptions
    528  1.5.2.2  matt 
    529  1.5.2.2  matt 	/*
    530  1.5.2.2  matt 	 * For each line that isn't valid, we set the intrsource for it to
    531  1.5.2.2  matt 	 * point at a dummy source so that pic_intr_establish will fail for it.
    532  1.5.2.2  matt 	 */
    533  1.5.2.2  matt 	for (size_t i = 0, group = 0;
    534  1.5.2.2  matt 	     i < sc->sc_pic.pic_maxsources;
    535  1.5.2.2  matt 	     i += 32, group++) {
    536  1.5.2.2  matt 		uint32_t invalid = ~sc->sc_gic_valid_lines[group];
    537  1.5.2.2  matt 		for (size_t j = 0; invalid && j < 32; j++, invalid >>= 1) {
    538  1.5.2.2  matt 			if (invalid & 1) {
    539  1.5.2.2  matt 				sc->sc_pic.pic_sources[i + j] =
    540  1.5.2.2  matt 				     &armgic_dummy_source;
    541  1.5.2.2  matt 			}
    542  1.5.2.2  matt 		}
    543  1.5.2.2  matt 	}
    544  1.5.2.2  matt #ifdef __HAVE_PIC_FAST_SOFTINTS
    545  1.5.2.2  matt 	intr_establish(SOFTINT_BIO, IPL_SOFTBIO, IST_EDGE,
    546  1.5.2.2  matt 	    pic_handle_softint, (void *)SOFTINT_BIO);
    547  1.5.2.2  matt 	intr_establish(SOFTINT_CLOCK, IPL_SOFTCLOCK, IST_EDGE,
    548  1.5.2.2  matt 	    pic_handle_softint, (void *)SOFTINT_CLOCK);
    549  1.5.2.2  matt 	intr_establish(SOFTINT_NET, IPL_SOFTNET, IST_EDGE,
    550  1.5.2.2  matt 	    pic_handle_softint, (void *)SOFTINT_NET);
    551  1.5.2.2  matt 	intr_establish(SOFTINT_SERIAL, IPL_SOFTSERIAL, IST_EDGE,
    552  1.5.2.2  matt 	    pic_handle_softint, (void *)SOFTINT_SERIAL);
    553  1.5.2.2  matt #endif
    554  1.5.2.2  matt #ifdef MULTIPROCESSOR
    555  1.5.2.2  matt 	intr_establish(ARMGIC_SGI_IPIBASE + IPI_AST, IPL_VM, IST_EDGE,
    556  1.5.2.2  matt 	    pic_ipi_nop, (void *)-1);
    557  1.5.2.2  matt 	intr_establish(ARMGIC_SGI_IPIBASE + IPI_XCALL, IPL_VM, IST_EDGE,
    558  1.5.2.2  matt 	    pic_ipi_xcall, (void *)-1);
    559  1.5.2.2  matt #if 0	/* Not needed */
    560  1.5.2.2  matt 	intr_establish(ARMGIC_SGI_IPIBASE + IPI_NOP, IPL_VM, IST_EDGE,
    561  1.5.2.2  matt 	    pic_ipi_nop, (void *)-1);
    562  1.5.2.2  matt #endif
    563  1.5.2.2  matt #ifdef __HAVE_PREEMPTION
    564  1.5.2.2  matt 	intr_establish(ARMGIC_SGI_IPIBASE + IPI_KPREEMPT, IPL_VM, IST_EDGE,
    565  1.5.2.2  matt 	    pic_ipi_nop, (void *)-1);
    566  1.5.2.2  matt #endif
    567  1.5.2.2  matt 	armgic_cpu_init(&sc->sc_pic, curcpu());
    568  1.5.2.2  matt #endif
    569  1.5.2.2  matt 
    570  1.5.2.2  matt 	aprint_normal(": Generic Interrupt Controller, "
    571  1.5.2.2  matt 	    "%zu sources (%zu valid)\n",
    572  1.5.2.2  matt 	    sc->sc_pic.pic_maxsources, sc->sc_gic_lines);
    573  1.5.2.2  matt 
    574  1.5.2.2  matt 	const u_int ppis = popcount32(sc->sc_gic_valid_lines[0] >> 16);
    575  1.5.2.2  matt 	const u_int sgis = popcount32(sc->sc_gic_valid_lines[0] & 0xffff);
    576  1.5.2.2  matt 	aprint_normal_dev(sc->sc_dev, "%u Priorities, %zu SPIs, %u PPIs, %u SGIs\n",
    577  1.5.2.2  matt 	    priorities, sc->sc_gic_lines - ppis - sgis, ppis, sgis);
    578  1.5.2.2  matt }
    579  1.5.2.2  matt 
    580  1.5.2.2  matt CFATTACH_DECL_NEW(armgic, 0,
    581  1.5.2.2  matt     armgic_match, armgic_attach, NULL, NULL);
    582