Home | History | Annotate | Line # | Download | only in cortex
gic.c revision 1.29
      1 /*	$NetBSD: gic.c,v 1.29 2017/06/28 20:46:35 skrll 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.29 2017/06/28 20:46:35 skrll 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 	uint32_t sc_bptargets;
    104 } armgic_softc = {
    105 	.sc_pic = {
    106 		.pic_ops = &armgic_picops,
    107 		.pic_name = "armgic",
    108 	},
    109 };
    110 
    111 static struct intrsource armgic_dummy_source;
    112 
    113 __CTASSERT(NIPL == 8);
    114 
    115 /*
    116  * GIC register are always in little-endian.  It is assumed the bus_space
    117  * will do any endian conversion required.
    118  */
    119 static inline uint32_t
    120 gicc_read(struct armgic_softc *sc, bus_size_t o)
    121 {
    122 	return bus_space_read_4(sc->sc_memt, sc->sc_gicch, o);
    123 }
    124 
    125 static inline void
    126 gicc_write(struct armgic_softc *sc, bus_size_t o, uint32_t v)
    127 {
    128 	bus_space_write_4(sc->sc_memt, sc->sc_gicch, o, v);
    129 }
    130 
    131 static inline uint32_t
    132 gicd_read(struct armgic_softc *sc, bus_size_t o)
    133 {
    134 	return bus_space_read_4(sc->sc_memt, sc->sc_gicdh, o);
    135 }
    136 
    137 static inline void
    138 gicd_write(struct armgic_softc *sc, bus_size_t o, uint32_t v)
    139 {
    140 	bus_space_write_4(sc->sc_memt, sc->sc_gicdh, o, v);
    141 }
    142 
    143 static uint32_t
    144 gicd_find_targets(struct armgic_softc *sc)
    145 {
    146 	uint32_t targets = 0;
    147 
    148 	/*
    149 	 * GICD_ITARGETSR0 through 7 are read-only, and each field returns
    150 	 * a value that corresponds only to the processor reading the
    151 	 * register. Use this to determine the current processor's
    152 	 * CPU interface number.
    153 	 */
    154 	for (int i = 0; i < 8; i++) {
    155 		targets = gicd_read(sc, GICD_ITARGETSRn(i));
    156 		if (targets != 0)
    157 			break;
    158 	}
    159 	targets |= (targets >> 16);
    160 	targets |= (targets >> 8);
    161 	targets &= 0xff;
    162 
    163 	return targets ? targets : 1;
    164 }
    165 
    166 /*
    167  * In the GIC prioritization scheme, lower numbers have higher priority.
    168  * Only write priorities that could be non-secure.
    169  */
    170 static inline uint32_t
    171 armgic_ipl_to_priority(int ipl)
    172 {
    173 	return GICC_PMR_NONSECURE
    174 	    | ((IPL_HIGH - ipl) * GICC_PMR_NS_PRIORITIES / NIPL);
    175 }
    176 
    177 #if 0
    178 static inline int
    179 armgic_priority_to_ipl(uint32_t priority)
    180 {
    181 	return IPL_HIGH
    182 	    - (priority & ~GICC_PMR_NONSECURE) * NIPL / GICC_PMR_NS_PRIORITIES;
    183 }
    184 #endif
    185 
    186 static void
    187 armgic_unblock_irqs(struct pic_softc *pic, size_t irq_base, uint32_t irq_mask)
    188 {
    189 	struct armgic_softc * const sc = PICTOSOFTC(pic);
    190 	const size_t group = irq_base / 32;
    191 
    192 	if (group == 0)
    193 		sc->sc_enabled_local |= irq_mask;
    194 
    195 	gicd_write(sc, GICD_ISENABLERn(group), irq_mask);
    196 }
    197 
    198 static void
    199 armgic_block_irqs(struct pic_softc *pic, size_t irq_base, uint32_t irq_mask)
    200 {
    201 	struct armgic_softc * const sc = PICTOSOFTC(pic);
    202 	const size_t group = irq_base / 32;
    203 
    204 	if (group == 0)
    205 		sc->sc_enabled_local &= ~irq_mask;
    206 
    207 	gicd_write(sc, GICD_ICENABLERn(group), irq_mask);
    208 }
    209 
    210 static void
    211 armgic_set_priority(struct pic_softc *pic, int ipl)
    212 {
    213 	struct armgic_softc * const sc = PICTOSOFTC(pic);
    214 
    215 	const uint32_t priority = armgic_ipl_to_priority(ipl);
    216 	gicc_write(sc, GICC_PMR, priority);
    217 }
    218 
    219 #ifdef __HAVE_PIC_FAST_SOFTINTS
    220 void
    221 softint_init_md(lwp_t *l, u_int level, uintptr_t *machdep_p)
    222 {
    223 	lwp_t **lp = &l->l_cpu->ci_softlwps[level];
    224 	KASSERT(*lp == NULL || *lp == l);
    225 	*lp = l;
    226 	/*
    227 	 * Really easy.  Just tell it to trigger the local CPU.
    228 	 */
    229 	*machdep_p = GICD_SGIR_TargetListFilter_Me
    230 	    | __SHIFTIN(level, GICD_SGIR_SGIINTID);
    231 }
    232 
    233 void
    234 softint_trigger(uintptr_t machdep)
    235 {
    236 
    237 	gicd_write(&armgic_softc, GICD_SGIR, machdep);
    238 }
    239 #endif
    240 
    241 void
    242 armgic_irq_handler(void *tf)
    243 {
    244 	struct cpu_info * const ci = curcpu();
    245 	struct armgic_softc * const sc = &armgic_softc;
    246 	const int old_ipl = ci->ci_cpl;
    247 #ifdef DIAGNOSTIC
    248 	const int old_mtx_count = ci->ci_mtx_count;
    249 	const int old_l_biglocks = ci->ci_curlwp->l_biglocks;
    250 #endif
    251 #ifdef DEBUG
    252 	size_t n = 0;
    253 #endif
    254 
    255 	ci->ci_data.cpu_nintr++;
    256 
    257 	KASSERTMSG(old_ipl != IPL_HIGH, "old_ipl %d pmr %#x hppir %#x",
    258 	    old_ipl, gicc_read(sc, GICC_PMR), gicc_read(sc, GICC_HPPIR));
    259 
    260 	for (;;) {
    261 		uint32_t iar = gicc_read(sc, GICC_IAR);
    262 		uint32_t irq = __SHIFTOUT(iar, GICC_IAR_IRQ);
    263 
    264 		if (irq == GICC_IAR_IRQ_SPURIOUS ||
    265 		    irq == GICC_IAR_IRQ_SSPURIOUS) {
    266 			iar = gicc_read(sc, GICC_IAR);
    267 			irq = __SHIFTOUT(iar, GICC_IAR_IRQ);
    268 			if (irq == GICC_IAR_IRQ_SPURIOUS)
    269 				break;
    270 			if (irq == GICC_IAR_IRQ_SSPURIOUS) {
    271 				break;
    272 			}
    273 		}
    274 
    275 		//const uint32_t cpuid = __SHIFTOUT(iar, GICC_IAR_CPUID_MASK);
    276 		struct intrsource * const is = sc->sc_pic.pic_sources[irq];
    277 		KASSERT(is != &armgic_dummy_source);
    278 
    279 		/*
    280 		 * GIC has asserted IPL for us so we can just update ci_cpl.
    281 		 *
    282 		 * But it's not that simple.  We may have already bumped ci_cpl
    283 		 * due to a high priority interrupt and now we are about to
    284 		 * dispatch one lower than the previous.  It's possible for
    285 		 * that previous interrupt to have deferred some interrupts
    286 		 * so we need deal with those when lowering to the current
    287 		 * interrupt's ipl.
    288 		 *
    289 		 * However, if are just raising ipl, we can just update ci_cpl.
    290 		 */
    291 		const int ipl = is->is_ipl;
    292 		if (__predict_false(ipl < ci->ci_cpl)) {
    293 			pic_do_pending_ints(I32_bit, ipl, tf);
    294 			KASSERT(ci->ci_cpl == ipl);
    295 		} else {
    296 			KASSERTMSG(ipl > ci->ci_cpl, "ipl %d cpl %d hw-ipl %#x",
    297 			    ipl, ci->ci_cpl,
    298 			    gicc_read(sc, GICC_PMR));
    299 			gicc_write(sc, GICC_PMR, armgic_ipl_to_priority(ipl));
    300 			ci->ci_cpl = ipl;
    301 		}
    302 		cpsie(I32_bit);
    303 		pic_dispatch(is, tf);
    304 		cpsid(I32_bit);
    305 		gicc_write(sc, GICC_EOIR, iar);
    306 #ifdef DEBUG
    307 		n++;
    308 		KDASSERTMSG(n < 5, "%s: processed too many (%zu)",
    309 		    ci->ci_data.cpu_name, n);
    310 #endif
    311 	}
    312 
    313 	/*
    314 	 * Now handle any pending ints.
    315 	 */
    316 	KASSERT(old_ipl != IPL_HIGH);
    317 	pic_do_pending_ints(I32_bit, old_ipl, tf);
    318 	KASSERTMSG(ci->ci_cpl == old_ipl, "ci_cpl %d old_ipl %d", ci->ci_cpl, old_ipl);
    319 	KASSERT(old_mtx_count == ci->ci_mtx_count);
    320 	KASSERT(old_l_biglocks == ci->ci_curlwp->l_biglocks);
    321 }
    322 
    323 void
    324 armgic_establish_irq(struct pic_softc *pic, struct intrsource *is)
    325 {
    326 	struct armgic_softc * const sc = PICTOSOFTC(pic);
    327 	const size_t group = is->is_irq / 32;
    328 	const u_int irq = is->is_irq & 31;
    329 	const u_int byte_shift = 8 * (irq & 3);
    330 	const u_int twopair_shift = 2 * (irq & 15);
    331 
    332 	KASSERTMSG(sc->sc_gic_valid_lines[group] & __BIT(irq),
    333 	    "irq %u: not valid (group[%zu]=0x%08x [0x%08x])",
    334 	    is->is_irq, group, sc->sc_gic_valid_lines[group],
    335 	    (uint32_t)__BIT(irq));
    336 
    337 	KASSERTMSG(is->is_type == IST_LEVEL || is->is_type == IST_EDGE,
    338 	    "irq %u: type %u unsupported", is->is_irq, is->is_type);
    339 
    340 	const bus_size_t targets_reg = GICD_ITARGETSRn(is->is_irq / 4);
    341 	const bus_size_t cfg_reg = GICD_ICFGRn(is->is_irq / 16);
    342 	uint32_t targets = gicd_read(sc, targets_reg);
    343 	uint32_t cfg = gicd_read(sc, cfg_reg);
    344 
    345 	if (group > 0) {
    346 		/*
    347 		 * There are 4 irqs per TARGETS register.  For now bind
    348 		 * to the primary cpu.
    349 		 */
    350 		targets &= ~(0xff << byte_shift);
    351 #if 0
    352 #ifdef MULTIPROCESSOR
    353 		if (is->is_mpsafe) {
    354 			targets |= sc->sc_mptargets << byte_shift;
    355 		} else
    356 #endif
    357 #endif
    358 		targets |= sc->sc_bptargets << byte_shift;
    359 		gicd_write(sc, targets_reg, targets);
    360 
    361 		/*
    362 		 * There are 16 irqs per CFG register.  10=EDGE 00=LEVEL
    363 		 */
    364 		uint32_t new_cfg = cfg;
    365 		uint32_t old_cfg = (cfg >> twopair_shift) & 3;
    366 		if (is->is_type == IST_LEVEL && (old_cfg & 2) != 0) {
    367 			new_cfg &= ~(3 << twopair_shift);
    368 		} else if (is->is_type == IST_EDGE && (old_cfg & 2) == 0) {
    369 			new_cfg |= 2 << twopair_shift;
    370 		}
    371 		if (new_cfg != cfg) {
    372 			gicd_write(sc, cfg_reg, new_cfg);
    373 		}
    374 #ifdef MULTIPROCESSOR
    375 	} else {
    376 		/*
    377 		 * All group 0 interrupts are per processor and MPSAFE by
    378 		 * default.
    379 		 */
    380 		is->is_mpsafe = true;
    381 #endif
    382 	}
    383 
    384 	/*
    385 	 * There are 4 irqs per PRIORITY register.  Map the IPL
    386 	 * to GIC priority.
    387 	 */
    388 	const bus_size_t priority_reg = GICD_IPRIORITYRn(is->is_irq / 4);
    389 	uint32_t priority = gicd_read(sc, priority_reg);
    390 	priority &= ~(0xff << byte_shift);
    391 	priority |= armgic_ipl_to_priority(is->is_ipl) << byte_shift;
    392 	gicd_write(sc, priority_reg, priority);
    393 }
    394 
    395 #ifdef MULTIPROCESSOR
    396 static void
    397 armgic_cpu_init_priorities(struct armgic_softc *sc)
    398 {
    399 	/* Set lowest priority, i.e. disable interrupts */
    400 	for (size_t i = 0; i < 32; i += 4) {
    401 		const bus_size_t priority_reg = GICD_IPRIORITYRn(i / 4);
    402 		gicd_write(sc, priority_reg, ~0);
    403 	}
    404 }
    405 
    406 static void
    407 armgic_cpu_update_priorities(struct armgic_softc *sc)
    408 {
    409 	uint32_t enabled = sc->sc_enabled_local;
    410 	for (size_t i = 0; i < 32; i += 4, enabled >>= 4) {
    411 		const bus_size_t priority_reg = GICD_IPRIORITYRn(i / 4);
    412 		uint32_t priority = gicd_read(sc, priority_reg);
    413 		uint32_t byte_mask = 0xff;
    414 		size_t byte_shift = 0;
    415 		for (size_t j = 0; j < 4; j++, byte_mask <<= 8, byte_shift += 8) {
    416 			struct intrsource * const is = sc->sc_pic.pic_sources[i+j];
    417 			priority |= byte_mask;
    418 			if (is == NULL || is == &armgic_dummy_source)
    419 				continue;
    420 			priority &= ~byte_mask;
    421 			priority |= armgic_ipl_to_priority(is->is_ipl) << byte_shift;
    422 		}
    423 		gicd_write(sc, priority_reg, priority);
    424 	}
    425 }
    426 
    427 static void
    428 armgic_cpu_init_targets(struct armgic_softc *sc)
    429 {
    430 	/*
    431 	 * Update the mpsafe targets
    432 	 */
    433 	for (size_t irq = 32; irq < sc->sc_pic.pic_maxsources; irq++) {
    434 		struct intrsource * const is = sc->sc_pic.pic_sources[irq];
    435 		const bus_size_t targets_reg = GICD_ITARGETSRn(irq / 4);
    436 		if (is != NULL && is->is_mpsafe) {
    437 			const u_int byte_shift = 8 * (irq & 3);
    438 			uint32_t targets = gicd_read(sc, targets_reg);
    439 			targets |= sc->sc_mptargets << byte_shift;
    440 			gicd_write(sc, targets_reg, targets);
    441 		}
    442 	}
    443 }
    444 
    445 void
    446 armgic_cpu_init(struct pic_softc *pic, struct cpu_info *ci)
    447 {
    448 	struct armgic_softc * const sc = PICTOSOFTC(pic);
    449 	sc->sc_mptargets |= gicd_find_targets(sc);
    450 	KASSERTMSG(ci->ci_cpl == IPL_HIGH, "ipl %d not IPL_HIGH", ci->ci_cpl);
    451 	armgic_cpu_init_priorities(sc);
    452 	if (!CPU_IS_PRIMARY(ci)) {
    453 		if (popcount(sc->sc_mptargets) != 1) {
    454 			armgic_cpu_init_targets(sc);
    455 		}
    456 		if (sc->sc_enabled_local) {
    457 			armgic_cpu_update_priorities(sc);
    458 			gicd_write(sc, GICD_ISENABLERn(0),
    459 			    sc->sc_enabled_local);
    460 		}
    461 	}
    462 	gicc_write(sc, GICC_PMR, armgic_ipl_to_priority(ci->ci_cpl));	// set PMR
    463 	gicc_write(sc, GICC_CTRL, GICC_CTRL_V1_Enable);	// enable interrupt
    464 	cpsie(I32_bit);					// allow IRQ exceptions
    465 }
    466 
    467 void
    468 armgic_ipi_send(struct pic_softc *pic, const kcpuset_t *kcp, u_long ipi)
    469 {
    470 	struct armgic_softc * const sc = PICTOSOFTC(pic);
    471 
    472 #if 0
    473 	if (ipi == IPI_NOP) {
    474 		__asm __volatile("sev");
    475 		return;
    476 	}
    477 #endif
    478 
    479 	uint32_t sgir = __SHIFTIN(ARMGIC_SGI_IPIBASE + ipi, GICD_SGIR_SGIINTID);
    480 	if (kcp != NULL) {
    481 		uint32_t targets;
    482 		kcpuset_export_u32(kcp, &targets, sizeof(targets));
    483 		sgir |= __SHIFTIN(targets, GICD_SGIR_TargetList);
    484 		sgir |= GICD_SGIR_TargetListFilter_List;
    485 	} else {
    486 		if (ncpu == 1)
    487 			return;
    488 		sgir |= GICD_SGIR_TargetListFilter_NotMe;
    489 	}
    490 
    491 	gicd_write(sc, GICD_SGIR, sgir);
    492 }
    493 #endif
    494 
    495 int
    496 armgic_match(device_t parent, cfdata_t cf, void *aux)
    497 {
    498 	struct mpcore_attach_args * const mpcaa = aux;
    499 
    500 	if (strcmp(cf->cf_name, mpcaa->mpcaa_name) != 0)
    501 		return 0;
    502 	if (!CPU_ID_CORTEX_P(cputype) || CPU_ID_CORTEX_A8_P(cputype))
    503 		return 0;
    504 
    505 	return 1;
    506 }
    507 
    508 void
    509 armgic_attach(device_t parent, device_t self, void *aux)
    510 {
    511 	struct armgic_softc * const sc = &armgic_softc;
    512 	struct mpcore_attach_args * const mpcaa = aux;
    513 
    514 	sc->sc_dev = self;
    515 	self->dv_private = sc;
    516 
    517 	sc->sc_memt = mpcaa->mpcaa_memt;	/* provided for us */
    518 	bus_space_subregion(sc->sc_memt, mpcaa->mpcaa_memh, mpcaa->mpcaa_off1,
    519 	    4096, &sc->sc_gicdh);
    520 	bus_space_subregion(sc->sc_memt, mpcaa->mpcaa_memh, mpcaa->mpcaa_off2,
    521 	    4096, &sc->sc_gicch);
    522 
    523 	sc->sc_gic_type = gicd_read(sc, GICD_TYPER);
    524 	sc->sc_pic.pic_maxsources = GICD_TYPER_LINES(sc->sc_gic_type);
    525 
    526 	gicc_write(sc, GICC_CTRL, 0);	/* disable all interrupts */
    527 	gicd_write(sc, GICD_CTRL, 0);	/* disable all interrupts */
    528 
    529 	gicc_write(sc, GICC_PMR, 0xff);
    530 	uint32_t pmr = gicc_read(sc, GICC_PMR);
    531 	u_int priorities = 1 << popcount32(pmr);
    532 
    533 	const uint32_t iidr = gicc_read(sc, GICC_IIDR);
    534 	const int iidr_prod = __SHIFTOUT(iidr, GICC_IIDR_ProductID);
    535 	const int iidr_arch = __SHIFTOUT(iidr, GICC_IIDR_ArchVersion);
    536 	const int iidr_rev = __SHIFTOUT(iidr, GICC_IIDR_Revision);
    537 	const int iidr_imp = __SHIFTOUT(iidr, GICC_IIDR_Implementer);
    538 
    539 	/*
    540 	 * Find the boot processor's CPU interface number.
    541 	 */
    542 	sc->sc_bptargets = gicd_find_targets(sc);
    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 	aprint_debug_dev(sc->sc_dev, "Architecture version %d"
    574 	    " (0x%x:%d rev %d)\n", iidr_arch, iidr_imp, iidr_prod,
    575 	    iidr_rev);
    576 
    577 #ifdef MULTIPROCESSOR
    578 	sc->sc_pic.pic_cpus = kcpuset_running;
    579 #endif
    580 	pic_add(&sc->sc_pic, 0);
    581 
    582 	/*
    583 	 * Force the GICD to IPL_HIGH and then enable interrupts.
    584 	 */
    585 	struct cpu_info * const ci = curcpu();
    586 	KASSERTMSG(ci->ci_cpl == IPL_HIGH, "ipl %d not IPL_HIGH", ci->ci_cpl);
    587 	armgic_set_priority(&sc->sc_pic, ci->ci_cpl);	// set PMR
    588 	gicd_write(sc, GICD_CTRL, GICD_CTRL_Enable);	// enable Distributer
    589 	gicc_write(sc, GICC_CTRL, GICC_CTRL_V1_Enable);	// enable CPU interrupts
    590 	cpsie(I32_bit);					// allow interrupt exceptions
    591 
    592 	/*
    593 	 * For each line that isn't valid, we set the intrsource for it to
    594 	 * point at a dummy source so that pic_intr_establish will fail for it.
    595 	 */
    596 	for (size_t i = 0, group = 0;
    597 	     i < sc->sc_pic.pic_maxsources;
    598 	     i += 32, group++) {
    599 		uint32_t invalid = ~sc->sc_gic_valid_lines[group];
    600 		for (size_t j = 0; invalid && j < 32; j++, invalid >>= 1) {
    601 			if (invalid & 1) {
    602 				sc->sc_pic.pic_sources[i + j] =
    603 				     &armgic_dummy_source;
    604 			}
    605 		}
    606 	}
    607 #ifdef __HAVE_PIC_FAST_SOFTINTS
    608 	intr_establish(SOFTINT_BIO, IPL_SOFTBIO, IST_MPSAFE | IST_EDGE,
    609 	    pic_handle_softint, (void *)SOFTINT_BIO);
    610 	intr_establish(SOFTINT_CLOCK, IPL_SOFTCLOCK, IST_MPSAFE | IST_EDGE,
    611 	    pic_handle_softint, (void *)SOFTINT_CLOCK);
    612 	intr_establish(SOFTINT_NET, IPL_SOFTNET, IST_MPSAFE | IST_EDGE,
    613 	    pic_handle_softint, (void *)SOFTINT_NET);
    614 	intr_establish(SOFTINT_SERIAL, IPL_SOFTSERIAL, IST_MPSAFE | IST_EDGE,
    615 	    pic_handle_softint, (void *)SOFTINT_SERIAL);
    616 #endif
    617 #ifdef MULTIPROCESSOR
    618 	armgic_cpu_init(&sc->sc_pic, curcpu());
    619 
    620 	intr_establish(ARMGIC_SGI_IPIBASE + IPI_AST, IPL_VM,
    621 	    IST_MPSAFE | IST_EDGE, pic_ipi_ast, (void *)-1);
    622 	intr_establish(ARMGIC_SGI_IPIBASE + IPI_XCALL, IPL_HIGH,
    623 	    IST_MPSAFE | IST_EDGE, pic_ipi_xcall, (void *)-1);
    624 	intr_establish(ARMGIC_SGI_IPIBASE + IPI_GENERIC, IPL_HIGH,
    625 	    IST_MPSAFE | IST_EDGE, pic_ipi_generic, (void *)-1);
    626 	intr_establish(ARMGIC_SGI_IPIBASE + IPI_NOP, IPL_VM,
    627 	    IST_MPSAFE | IST_EDGE, pic_ipi_nop, (void *)-1);
    628 	intr_establish(ARMGIC_SGI_IPIBASE + IPI_SHOOTDOWN, IPL_SCHED,
    629 	    IST_MPSAFE | IST_EDGE, pic_ipi_shootdown, (void *)-1);
    630 #ifdef DDB
    631 	intr_establish(ARMGIC_SGI_IPIBASE + IPI_DDB, IPL_HIGH,
    632 	    IST_MPSAFE | IST_EDGE, pic_ipi_ddb, NULL);
    633 #endif
    634 #ifdef __HAVE_PREEMPTION
    635 	intr_establish(ARMGIC_SGI_IPIBASE + IPI_KPREEMPT, IPL_VM,
    636 	    IST_MPSAFE | IST_EDGE, pic_ipi_kpreempt, (void *)-1);
    637 #endif
    638 #endif
    639 
    640 	const u_int ppis = popcount32(sc->sc_gic_valid_lines[0] >> 16);
    641 	const u_int sgis = popcount32(sc->sc_gic_valid_lines[0] & 0xffff);
    642 	aprint_normal_dev(sc->sc_dev, "%u Priorities, %zu SPIs, %u PPIs, "
    643 	    "%u SGIs\n",  priorities, sc->sc_gic_lines - ppis - sgis, ppis,
    644 	    sgis);
    645 }
    646 
    647 CFATTACH_DECL_NEW(armgic, 0,
    648     armgic_match, armgic_attach, NULL, NULL);
    649