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