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