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