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