Home | History | Annotate | Line # | Download | only in pic
pic.c revision 1.69.2.1
      1 /*	$NetBSD: pic.c,v 1.69.2.1 2021/04/03 21:44:41 thorpej Exp $	*/
      2 /*-
      3  * Copyright (c) 2008 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.
      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 #define _INTR_PRIVATE
     32 #include "opt_ddb.h"
     33 #include "opt_multiprocessor.h"
     34 
     35 #include <sys/cdefs.h>
     36 __KERNEL_RCSID(0, "$NetBSD: pic.c,v 1.69.2.1 2021/04/03 21:44:41 thorpej Exp $");
     37 
     38 #include <sys/param.h>
     39 #include <sys/atomic.h>
     40 #include <sys/cpu.h>
     41 #include <sys/evcnt.h>
     42 #include <sys/interrupt.h>
     43 #include <sys/intr.h>
     44 #include <sys/ipi.h>
     45 #include <sys/kernel.h>
     46 #include <sys/kmem.h>
     47 #include <sys/mutex.h>
     48 #include <sys/once.h>
     49 #include <sys/xcall.h>
     50 
     51 #include <arm/armreg.h>
     52 #include <arm/cpufunc.h>
     53 #include <arm/locore.h>	/* for compat aarch64 */
     54 
     55 #ifdef DDB
     56 #include <arm/db_machdep.h>
     57 #endif
     58 
     59 #include <arm/pic/picvar.h>
     60 
     61 #if defined(__HAVE_PIC_PENDING_INTRS)
     62 /*
     63  * This implementation of pending interrupts on a MULTIPROCESSOR system makes
     64  * the assumption that a PIC (pic_softc) shall only have all its interrupts
     65  * come from the same CPU.  In other words, interrupts from a single PIC will
     66  * not be distributed among multiple CPUs.
     67  */
     68 struct pic_pending {
     69 	volatile uint32_t blocked_pics;
     70 	volatile uint32_t pending_pics;
     71 	volatile uint32_t pending_ipls;
     72 };
     73 static uint32_t
     74 	pic_find_pending_irqs_by_ipl(struct pic_softc *, size_t, uint32_t, int);
     75 static struct pic_softc *
     76 	pic_list_find_pic_by_pending_ipl(struct pic_pending *, uint32_t);
     77 static void
     78 	pic_deliver_irqs(struct pic_pending *, struct pic_softc *, int, void *);
     79 static void
     80 	pic_list_deliver_irqs(struct pic_pending *, register_t, int, void *);
     81 
     82 #ifdef MULTIPROCESSOR
     83 percpu_t *pic_pending_percpu;
     84 static struct pic_pending *
     85 pic_pending_get(void)
     86 {
     87 	return percpu_getref(pic_pending_percpu);
     88 }
     89 static void
     90 pic_pending_put(struct pic_pending *pend)
     91 {
     92 	percpu_putref(pic_pending_percpu);
     93 }
     94 #else
     95 struct pic_pending pic_pending;
     96 #define	pic_pending_get()	(&pic_pending)
     97 #define	pic_pending_put(pend)	__nothing
     98 #endif /* MULTIPROCESSOR */
     99 #endif /* __HAVE_PIC_PENDING_INTRS */
    100 
    101 struct pic_softc *pic_list[PIC_MAXPICS];
    102 #if PIC_MAXPICS > 32
    103 #error PIC_MAXPICS > 32 not supported
    104 #endif
    105 struct intrsource *pic_sources[PIC_MAXMAXSOURCES];
    106 struct intrsource *pic__iplsources[PIC_MAXMAXSOURCES];
    107 struct intrsource **pic_iplsource[NIPL] = {
    108 	[0 ... NIPL-1] = pic__iplsources,
    109 };
    110 size_t pic_ipl_offset[NIPL+1];
    111 
    112 static kmutex_t pic_lock;
    113 static size_t pic_sourcebase;
    114 static int pic_lastbase;
    115 static struct evcnt pic_deferral_ev =
    116     EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "deferred", "intr");
    117 EVCNT_ATTACH_STATIC(pic_deferral_ev);
    118 
    119 static int pic_init(void);
    120 
    121 #ifdef __HAVE_PIC_SET_PRIORITY
    122 void
    123 pic_set_priority(struct cpu_info *ci, int newipl)
    124 {
    125 	register_t psw = DISABLE_INTERRUPT_SAVE();
    126 	if (pic_list[0] != NULL)
    127 		(pic_list[0]->pic_ops->pic_set_priority)(pic_list[0], newipl);
    128 	ci->ci_cpl = newipl;
    129 	if ((psw & I32_bit) == 0) {
    130 		ENABLE_INTERRUPT();
    131 	}
    132 }
    133 #endif
    134 
    135 #ifdef MULTIPROCESSOR
    136 int
    137 pic_ipi_ast(void *arg)
    138 {
    139 	setsoftast(curcpu());
    140 	return 1;
    141 }
    142 
    143 int
    144 pic_ipi_nop(void *arg)
    145 {
    146 	/* do nothing */
    147 	return 1;
    148 }
    149 
    150 int
    151 pic_ipi_xcall(void *arg)
    152 {
    153 	xc_ipi_handler();
    154 	return 1;
    155 }
    156 
    157 int
    158 pic_ipi_generic(void *arg)
    159 {
    160 	ipi_cpu_handler();
    161 	return 1;
    162 }
    163 
    164 #ifdef DDB
    165 int
    166 pic_ipi_ddb(void *arg)
    167 {
    168 //	printf("%s: %s: tf=%p\n", __func__, curcpu()->ci_cpuname, arg);
    169 	kdb_trap(-1, arg);
    170 	return 1;
    171 }
    172 #endif /* DDB */
    173 
    174 #ifdef __HAVE_PREEMPTION
    175 int
    176 pic_ipi_kpreempt(void *arg)
    177 {
    178 	atomic_or_uint(&curcpu()->ci_astpending, __BIT(1));
    179 	return 1;
    180 }
    181 #endif /* __HAVE_PREEMPTION */
    182 
    183 void
    184 intr_cpu_init(struct cpu_info *ci)
    185 {
    186 	for (size_t slot = 0; slot < PIC_MAXPICS; slot++) {
    187 		struct pic_softc * const pic = pic_list[slot];
    188 		if (pic != NULL && pic->pic_ops->pic_cpu_init != NULL) {
    189 			(*pic->pic_ops->pic_cpu_init)(pic, ci);
    190 		}
    191 	}
    192 }
    193 
    194 typedef void (*pic_ipi_send_func_t)(struct pic_softc *, u_long);
    195 
    196 void
    197 intr_ipi_send(const kcpuset_t *kcp, u_long ipi)
    198 {
    199 	struct cpu_info * const ci = curcpu();
    200 	KASSERT(ipi < NIPI);
    201 	KASSERT(kcp == NULL || kcpuset_countset(kcp) == 1);
    202 	bool __diagused sent_p = false;
    203 	for (size_t slot = 0; slot < PIC_MAXPICS; slot++) {
    204 		struct pic_softc * const pic = pic_list[slot];
    205 		if (pic == NULL || pic->pic_cpus == NULL)
    206 			continue;
    207 		if (kcp == NULL || kcpuset_intersecting_p(kcp, pic->pic_cpus)) {
    208 			/*
    209 			 * Never send to ourself.
    210 			 *
    211 			 * This test uses pointer comparison for systems
    212 			 * that have a pic per cpu, e.g. RPI[23].  GIC sets
    213 			 * pic_cpus to kcpuset_running and handles "not for
    214 			 * self" internally.
    215 			 */
    216 			if (pic->pic_cpus == ci->ci_kcpuset)
    217 				continue;
    218 
    219 			(*pic->pic_ops->pic_ipi_send)(pic, kcp, ipi);
    220 
    221 			/*
    222 			 * If we were targeting a single CPU or this pic
    223 			 * handles all cpus, we're done.
    224 			 */
    225 			if (kcp != NULL || pic->pic_cpus == kcpuset_running)
    226 				return;
    227 			sent_p = true;
    228 		}
    229 	}
    230 	KASSERTMSG(cold || sent_p || ncpu <= 1, "cold %d sent_p %d ncpu %d",
    231 	    cold, sent_p, ncpu);
    232 }
    233 #endif /* MULTIPROCESSOR */
    234 
    235 #ifdef __HAVE_PIC_FAST_SOFTINTS
    236 int
    237 pic_handle_softint(void *arg)
    238 {
    239 	void softint_switch(lwp_t *, int);
    240 	struct cpu_info * const ci = curcpu();
    241 	const size_t softint = (size_t) arg;
    242 	int s = splhigh();
    243 	ci->ci_intr_depth--;	// don't count these as interrupts
    244 	softint_switch(ci->ci_softlwps[softint], s);
    245 	ci->ci_intr_depth++;
    246 	splx(s);
    247 	return 1;
    248 }
    249 #endif
    250 
    251 int
    252 pic_handle_intr(void *arg)
    253 {
    254 	struct pic_softc * const pic = arg;
    255 	int rv;
    256 
    257 	rv = (*pic->pic_ops->pic_find_pending_irqs)(pic);
    258 
    259 	return rv > 0;
    260 }
    261 
    262 #if defined(__HAVE_PIC_PENDING_INTRS)
    263 void
    264 pic_mark_pending_source(struct pic_softc *pic, struct intrsource *is)
    265 {
    266 	const uint32_t ipl_mask = __BIT(is->is_ipl);
    267 
    268 	atomic_or_32(&pic->pic_pending_irqs[is->is_irq >> 5],
    269 	    __BIT(is->is_irq & 0x1f));
    270 
    271 	atomic_or_32(&pic->pic_pending_ipls, ipl_mask);
    272 	struct pic_pending *pend = pic_pending_get();
    273 	atomic_or_32(&pend->pending_ipls, ipl_mask);
    274 	atomic_or_32(&pend->pending_pics, __BIT(pic->pic_id));
    275 	pic_pending_put(pend);
    276 }
    277 
    278 void
    279 pic_mark_pending(struct pic_softc *pic, int irq)
    280 {
    281 	struct intrsource * const is = pic->pic_sources[irq];
    282 
    283 	KASSERT(irq < pic->pic_maxsources);
    284 	KASSERT(is != NULL);
    285 
    286 	pic_mark_pending_source(pic, is);
    287 }
    288 
    289 uint32_t
    290 pic_mark_pending_sources(struct pic_softc *pic, size_t irq_base,
    291 	uint32_t pending)
    292 {
    293 	struct intrsource ** const isbase = &pic->pic_sources[irq_base];
    294 	struct intrsource *is;
    295 	volatile uint32_t *ipending = &pic->pic_pending_irqs[irq_base >> 5];
    296 	uint32_t ipl_mask = 0;
    297 
    298 	if (pending == 0)
    299 		return ipl_mask;
    300 
    301 	KASSERT((irq_base & 31) == 0);
    302 
    303 	(*pic->pic_ops->pic_block_irqs)(pic, irq_base, pending);
    304 
    305 	atomic_or_32(ipending, pending);
    306 	while (pending != 0) {
    307 		int n = ffs(pending);
    308 		if (n-- == 0)
    309 			break;
    310 		is = isbase[n];
    311 		KASSERT(is != NULL);
    312 		KASSERT(irq_base <= is->is_irq && is->is_irq < irq_base + 32);
    313 		pending &= ~__BIT(n);
    314 		ipl_mask |= __BIT(is->is_ipl);
    315 	}
    316 
    317 	atomic_or_32(&pic->pic_pending_ipls, ipl_mask);
    318 	struct pic_pending *pend = pic_pending_get();
    319 	atomic_or_32(&pend->pending_ipls, ipl_mask);
    320 	atomic_or_32(&pend->pending_pics, __BIT(pic->pic_id));
    321 	pic_pending_put(pend);
    322 	return ipl_mask;
    323 }
    324 
    325 uint32_t
    326 pic_find_pending_irqs_by_ipl(struct pic_softc *pic, size_t irq_base,
    327 	uint32_t pending, int ipl)
    328 {
    329 	uint32_t ipl_irq_mask = 0;
    330 	uint32_t irq_mask;
    331 
    332 	for (;;) {
    333 		int irq = ffs(pending);
    334 		if (irq-- == 0)
    335 			return ipl_irq_mask;
    336 
    337 		irq_mask = __BIT(irq);
    338 #if 1
    339     		KASSERTMSG(pic->pic_sources[irq_base + irq] != NULL,
    340 		   "%s: irq_base %zu irq %d\n", __func__, irq_base, irq);
    341 #else
    342 		if (pic->pic_sources[irq_base + irq] == NULL) {
    343 			aprint_error("stray interrupt? irq_base=%zu irq=%d\n",
    344 			    irq_base, irq);
    345 		} else
    346 #endif
    347 		if (pic->pic_sources[irq_base + irq]->is_ipl == ipl)
    348 			ipl_irq_mask |= irq_mask;
    349 
    350 		pending &= ~irq_mask;
    351 	}
    352 }
    353 #endif /* __HAVE_PIC_PENDING_INTRS */
    354 
    355 void
    356 pic_dispatch(struct intrsource *is, void *frame)
    357 {
    358 	int (*func)(void *) = is->is_func;
    359 	void *arg = is->is_arg;
    360 
    361 	if (__predict_false(arg == NULL)) {
    362 		if (__predict_false(frame == NULL)) {
    363 			pic_deferral_ev.ev_count++;
    364 			return;
    365 		}
    366 		arg = frame;
    367 	}
    368 
    369 #ifdef MULTIPROCESSOR
    370 	if (!is->is_mpsafe) {
    371 		KERNEL_LOCK(1, NULL);
    372 		const u_int ci_blcnt __diagused = curcpu()->ci_biglock_count;
    373 		const u_int l_blcnt __diagused = curlwp->l_blcnt;
    374 		(void)(*func)(arg);
    375 		KASSERT(ci_blcnt == curcpu()->ci_biglock_count);
    376 		KASSERT(l_blcnt == curlwp->l_blcnt);
    377 		KERNEL_UNLOCK_ONE(NULL);
    378 	} else
    379 #endif
    380 		(void)(*func)(arg);
    381 
    382 	struct pic_percpu * const pcpu = percpu_getref(is->is_pic->pic_percpu);
    383 	KASSERT(pcpu->pcpu_magic == PICPERCPU_MAGIC);
    384 	pcpu->pcpu_evs[is->is_irq].ev_count++;
    385 	percpu_putref(is->is_pic->pic_percpu);
    386 }
    387 
    388 #if defined(__HAVE_PIC_PENDING_INTRS)
    389 void
    390 pic_deliver_irqs(struct pic_pending *pend, struct pic_softc *pic, int ipl,
    391     void *frame)
    392 {
    393 	const uint32_t ipl_mask = __BIT(ipl);
    394 	struct intrsource *is;
    395 	volatile uint32_t *ipending = pic->pic_pending_irqs;
    396 	volatile uint32_t *iblocked = pic->pic_blocked_irqs;
    397 	size_t irq_base;
    398 #if PIC_MAXSOURCES > 32
    399 	size_t irq_count;
    400 	int poi = 0;		/* Possibility of interrupting */
    401 #endif
    402 	uint32_t pending_irqs;
    403 	uint32_t blocked_irqs;
    404 	int irq;
    405 	bool progress __diagused = false;
    406 
    407 	KASSERT(pic->pic_pending_ipls & ipl_mask);
    408 
    409 	irq_base = 0;
    410 #if PIC_MAXSOURCES > 32
    411 	irq_count = 0;
    412 #endif
    413 
    414 	for (;;) {
    415 		pending_irqs = pic_find_pending_irqs_by_ipl(pic, irq_base,
    416 		    *ipending, ipl);
    417 		KASSERT((pending_irqs & *ipending) == pending_irqs);
    418 		KASSERT((pending_irqs & ~(*ipending)) == 0);
    419 		if (pending_irqs == 0) {
    420 #if PIC_MAXSOURCES > 32
    421 			irq_count += 32;
    422 			if (__predict_true(irq_count >= pic->pic_maxsources)) {
    423 				if (!poi)
    424 					/*Interrupt at this level was handled.*/
    425 					break;
    426 				irq_base = 0;
    427 				irq_count = 0;
    428 				poi = 0;
    429 				ipending = pic->pic_pending_irqs;
    430 				iblocked = pic->pic_blocked_irqs;
    431 			} else {
    432 				irq_base += 32;
    433 				ipending++;
    434 				iblocked++;
    435 				KASSERT(irq_base <= pic->pic_maxsources);
    436 			}
    437 			continue;
    438 #else
    439 			break;
    440 #endif
    441 		}
    442 		progress = true;
    443 		blocked_irqs = 0;
    444 		do {
    445 			irq = ffs(pending_irqs) - 1;
    446 			KASSERT(irq >= 0);
    447 
    448 			atomic_and_32(ipending, ~__BIT(irq));
    449 			is = pic->pic_sources[irq_base + irq];
    450 			if (is != NULL) {
    451 				ENABLE_INTERRUPT();
    452 				pic_dispatch(is, frame);
    453 				DISABLE_INTERRUPT();
    454 #if PIC_MAXSOURCES > 32
    455 				/*
    456 				 * There is a possibility of interrupting
    457 				 * from ENABLE_INTERRUPT() to
    458 				 * DISABLE_INTERRUPT().
    459 				 */
    460 				poi = 1;
    461 #endif
    462 				blocked_irqs |= __BIT(irq);
    463 			} else {
    464 				KASSERT(0);
    465 			}
    466 			pending_irqs = pic_find_pending_irqs_by_ipl(pic,
    467 			    irq_base, *ipending, ipl);
    468 		} while (pending_irqs);
    469 		if (blocked_irqs) {
    470 			atomic_or_32(iblocked, blocked_irqs);
    471 			atomic_or_32(&pend->blocked_pics, __BIT(pic->pic_id));
    472 		}
    473 	}
    474 
    475 	KASSERT(progress);
    476 	/*
    477 	 * Since interrupts are disabled, we don't have to be too careful
    478 	 * about these.
    479 	 */
    480 	if (atomic_and_32_nv(&pic->pic_pending_ipls, ~ipl_mask) == 0)
    481 		atomic_and_32(&pend->pending_pics, ~__BIT(pic->pic_id));
    482 }
    483 
    484 static void
    485 pic_list_unblock_irqs(struct pic_pending *pend)
    486 {
    487 	uint32_t blocked_pics = pend->blocked_pics;
    488 
    489 	pend->blocked_pics = 0;
    490 
    491 	for (;;) {
    492 		struct pic_softc *pic;
    493 #if PIC_MAXSOURCES > 32
    494 		volatile uint32_t *iblocked;
    495 		uint32_t blocked;
    496 		size_t irq_base;
    497 #endif
    498 
    499 		int pic_id = ffs(blocked_pics);
    500 		if (pic_id-- == 0)
    501 			return;
    502 
    503 		pic = pic_list[pic_id];
    504 		KASSERT(pic != NULL);
    505 #if PIC_MAXSOURCES > 32
    506 		for (irq_base = 0, iblocked = pic->pic_blocked_irqs;
    507 		     irq_base < pic->pic_maxsources;
    508 		     irq_base += 32, iblocked++) {
    509 			if ((blocked = *iblocked) != 0) {
    510 				(*pic->pic_ops->pic_unblock_irqs)(pic,
    511 				    irq_base, blocked);
    512 				atomic_and_32(iblocked, ~blocked);
    513 			}
    514 		}
    515 #else
    516 		KASSERT(pic->pic_blocked_irqs[0] != 0);
    517 		(*pic->pic_ops->pic_unblock_irqs)(pic,
    518 		    0, pic->pic_blocked_irqs[0]);
    519 		pic->pic_blocked_irqs[0] = 0;
    520 #endif
    521 		blocked_pics &= ~__BIT(pic_id);
    522 	}
    523 }
    524 
    525 struct pic_softc *
    526 pic_list_find_pic_by_pending_ipl(struct pic_pending *pend, uint32_t ipl_mask)
    527 {
    528 	uint32_t pending_pics = pend->pending_pics;
    529 	struct pic_softc *pic;
    530 
    531 	for (;;) {
    532 		int pic_id = ffs(pending_pics);
    533 		if (pic_id-- == 0)
    534 			return NULL;
    535 
    536 		pic = pic_list[pic_id];
    537 		KASSERT(pic != NULL);
    538 		if (pic->pic_pending_ipls & ipl_mask)
    539 			return pic;
    540 		pending_pics &= ~__BIT(pic_id);
    541 	}
    542 }
    543 
    544 void
    545 pic_list_deliver_irqs(struct pic_pending *pend, register_t psw, int ipl,
    546     void *frame)
    547 {
    548 	const uint32_t ipl_mask = __BIT(ipl);
    549 	struct pic_softc *pic;
    550 
    551 	while ((pic = pic_list_find_pic_by_pending_ipl(pend, ipl_mask)) != NULL) {
    552 		pic_deliver_irqs(pend, pic, ipl, frame);
    553 		KASSERT((pic->pic_pending_ipls & ipl_mask) == 0);
    554 	}
    555 	atomic_and_32(&pend->pending_ipls, ~ipl_mask);
    556 }
    557 #endif /* __HAVE_PIC_PENDING_INTRS */
    558 
    559 void
    560 pic_do_pending_ints(register_t psw, int newipl, void *frame)
    561 {
    562 	struct cpu_info * const ci = curcpu();
    563 	if (__predict_false(newipl == IPL_HIGH)) {
    564 		KASSERTMSG(ci->ci_cpl == IPL_HIGH, "cpl %d", ci->ci_cpl);
    565 		return;
    566 	}
    567 #if defined(__HAVE_PIC_PENDING_INTRS)
    568 	struct pic_pending *pend = pic_pending_get();
    569 	while ((pend->pending_ipls & ~__BIT(newipl)) > __BIT(newipl)) {
    570 		KASSERT(pend->pending_ipls < __BIT(NIPL));
    571 		for (;;) {
    572 			int ipl = 31 - __builtin_clz(pend->pending_ipls);
    573 			KASSERT(ipl < NIPL);
    574 			if (ipl <= newipl)
    575 				break;
    576 
    577 			pic_set_priority(ci, ipl);
    578 			pic_list_deliver_irqs(pend, psw, ipl, frame);
    579 			pic_list_unblock_irqs(pend);
    580 		}
    581 	}
    582 	pic_pending_put(pend);
    583 #endif /* __HAVE_PIC_PENDING_INTRS */
    584 #ifdef __HAVE_PREEMPTION
    585 	if (newipl == IPL_NONE && (ci->ci_astpending & __BIT(1))) {
    586 		pic_set_priority(ci, IPL_SCHED);
    587 		kpreempt(0);
    588 	}
    589 #endif
    590 	if (ci->ci_cpl != newipl)
    591 		pic_set_priority(ci, newipl);
    592 }
    593 
    594 static void
    595 pic_percpu_allocate(void *v0, void *v1, struct cpu_info *ci)
    596 {
    597 	struct pic_percpu * const pcpu = v0;
    598 	struct pic_softc * const pic = v1;
    599 
    600 	pcpu->pcpu_evs = kmem_zalloc(pic->pic_maxsources * sizeof(pcpu->pcpu_evs[0]),
    601 	    KM_SLEEP);
    602 	KASSERT(pcpu->pcpu_evs != NULL);
    603 
    604 #define	PCPU_NAMELEN	32
    605 #ifdef DIAGNOSTIC
    606 	const size_t namelen = strlen(pic->pic_name) + 4 + strlen(ci->ci_data.cpu_name);
    607 #endif
    608 
    609 	KASSERT(namelen < PCPU_NAMELEN);
    610 	pcpu->pcpu_name = kmem_alloc(PCPU_NAMELEN, KM_SLEEP);
    611 #ifdef MULTIPROCESSOR
    612 	snprintf(pcpu->pcpu_name, PCPU_NAMELEN,
    613 	    "%s (%s)", pic->pic_name, ci->ci_data.cpu_name);
    614 #else
    615 	strlcpy(pcpu->pcpu_name, pic->pic_name, PCPU_NAMELEN);
    616 #endif
    617 	pcpu->pcpu_magic = PICPERCPU_MAGIC;
    618 #if 0
    619 	printf("%s: %s %s: <%s>\n",
    620 	    __func__, ci->ci_data.cpu_name, pic->pic_name,
    621 	    pcpu->pcpu_name);
    622 #endif
    623 }
    624 
    625 static int
    626 pic_init(void)
    627 {
    628 
    629 	mutex_init(&pic_lock, MUTEX_DEFAULT, IPL_HIGH);
    630 
    631 	return 0;
    632 }
    633 
    634 int
    635 pic_add(struct pic_softc *pic, int irqbase)
    636 {
    637 	int slot, maybe_slot = -1;
    638 	size_t sourcebase;
    639 	static ONCE_DECL(pic_once);
    640 
    641 	RUN_ONCE(&pic_once, pic_init);
    642 
    643 	KASSERT(strlen(pic->pic_name) > 0);
    644 
    645 #if defined(__HAVE_PIC_PENDING_INTRS) && defined(MULTIPROCESSOR)
    646 	if (__predict_false(pic_pending_percpu == NULL))
    647 		pic_pending_percpu = percpu_alloc(sizeof(struct pic_pending));
    648 #endif /* __HAVE_PIC_PENDING_INTRS && MULTIPROCESSOR */
    649 
    650 	mutex_enter(&pic_lock);
    651 	if (irqbase == PIC_IRQBASE_ALLOC) {
    652 		irqbase = pic_lastbase;
    653 	}
    654 	for (slot = 0; slot < PIC_MAXPICS; slot++) {
    655 		struct pic_softc * const xpic = pic_list[slot];
    656 		if (xpic == NULL) {
    657 			if (maybe_slot < 0)
    658 				maybe_slot = slot;
    659 			if (irqbase < 0)
    660 				break;
    661 			continue;
    662 		}
    663 		if (irqbase < 0 || xpic->pic_irqbase < 0)
    664 			continue;
    665 		if (irqbase >= xpic->pic_irqbase + xpic->pic_maxsources)
    666 			continue;
    667 		if (irqbase + pic->pic_maxsources <= xpic->pic_irqbase)
    668 			continue;
    669 		panic("pic_add: pic %s (%zu sources @ irq %u) conflicts"
    670 		    " with pic %s (%zu sources @ irq %u)",
    671 		    pic->pic_name, pic->pic_maxsources, irqbase,
    672 		    xpic->pic_name, xpic->pic_maxsources, xpic->pic_irqbase);
    673 	}
    674 	slot = maybe_slot;
    675 #if 0
    676 	printf("%s: pic_sourcebase=%zu pic_maxsources=%zu\n",
    677 	    pic->pic_name, pic_sourcebase, pic->pic_maxsources);
    678 #endif
    679 	KASSERTMSG(pic->pic_maxsources <= PIC_MAXSOURCES, "%zu",
    680 	    pic->pic_maxsources);
    681 	KASSERT(pic_sourcebase + pic->pic_maxsources <= PIC_MAXMAXSOURCES);
    682 	sourcebase = pic_sourcebase;
    683 	pic_sourcebase += pic->pic_maxsources;
    684         if (pic_lastbase < irqbase + pic->pic_maxsources)
    685                 pic_lastbase = irqbase + pic->pic_maxsources;
    686 	mutex_exit(&pic_lock);
    687 
    688 	/*
    689 	 * Allocate a pointer to each cpu's evcnts and then, for each cpu,
    690 	 * allocate its evcnts and then attach an evcnt for each pin.
    691 	 * We can't allocate the evcnt structures directly since
    692 	 * percpu will move the contents of percpu memory around and
    693 	 * corrupt the pointers in the evcnts themselves.  Remember, any
    694 	 * problem can be solved with sufficient indirection.
    695 	 */
    696 	pic->pic_percpu = percpu_create(sizeof(struct pic_percpu),
    697 	    pic_percpu_allocate, NULL, pic);
    698 
    699 	pic->pic_sources = &pic_sources[sourcebase];
    700 	pic->pic_irqbase = irqbase;
    701 	pic->pic_id = slot;
    702 #ifdef __HAVE_PIC_SET_PRIORITY
    703 	KASSERT((slot == 0) == (pic->pic_ops->pic_set_priority != NULL));
    704 #endif
    705 #ifdef MULTIPROCESSOR
    706 	KASSERT((pic->pic_cpus != NULL) == (pic->pic_ops->pic_ipi_send != NULL));
    707 #endif
    708 	pic_list[slot] = pic;
    709 
    710 	return irqbase;
    711 }
    712 
    713 int
    714 pic_alloc_irq(struct pic_softc *pic)
    715 {
    716 	int irq;
    717 
    718 	for (irq = 0; irq < pic->pic_maxsources; irq++) {
    719 		if (pic->pic_sources[irq] == NULL)
    720 			return irq;
    721 	}
    722 
    723 	return -1;
    724 }
    725 
    726 static void
    727 pic_percpu_evcnt_attach(void *v0, void *v1, struct cpu_info *ci)
    728 {
    729 	struct pic_percpu * const pcpu = v0;
    730 	struct intrsource * const is = v1;
    731 
    732 	KASSERT(pcpu->pcpu_magic == PICPERCPU_MAGIC);
    733 	evcnt_attach_dynamic(&pcpu->pcpu_evs[is->is_irq], EVCNT_TYPE_INTR, NULL,
    734 	    pcpu->pcpu_name, is->is_source);
    735 }
    736 
    737 void *
    738 pic_establish_intr(struct pic_softc *pic, int irq, int ipl, int type,
    739 	int (*func)(void *), void *arg, const char *xname)
    740 {
    741 	struct intrsource *is;
    742 	int off, nipl;
    743 
    744 	if (pic->pic_sources[irq]) {
    745 		printf("pic_establish_intr: pic %s irq %d already present\n",
    746 		    pic->pic_name, irq);
    747 		return NULL;
    748 	}
    749 
    750 	is = kmem_zalloc(sizeof(*is), KM_SLEEP);
    751 	is->is_pic = pic;
    752 	is->is_irq = irq;
    753 	is->is_ipl = ipl;
    754 	is->is_type = type & 0xff;
    755 	is->is_func = func;
    756 	is->is_arg = arg;
    757 #ifdef MULTIPROCESSOR
    758 	is->is_mpsafe = (type & IST_MPSAFE) || ipl != IPL_VM;
    759 #endif
    760 
    761 	if (pic->pic_ops->pic_source_name)
    762 		(*pic->pic_ops->pic_source_name)(pic, irq, is->is_source,
    763 		    sizeof(is->is_source));
    764 	else
    765 		snprintf(is->is_source, sizeof(is->is_source), "irq %d", irq);
    766 
    767 	/*
    768 	 * Now attach the per-cpu evcnts.
    769 	 */
    770 	percpu_foreach(pic->pic_percpu, pic_percpu_evcnt_attach, is);
    771 
    772 	pic->pic_sources[irq] = is;
    773 
    774 	/*
    775 	 * First try to use an existing slot which is empty.
    776 	 */
    777 	for (off = pic_ipl_offset[ipl]; off < pic_ipl_offset[ipl+1]; off++) {
    778 		if (pic__iplsources[off] == NULL) {
    779 			is->is_iplidx = off - pic_ipl_offset[ipl];
    780 			pic__iplsources[off] = is;
    781 			goto unblock;
    782 		}
    783 	}
    784 
    785 	/*
    786 	 * Move up all the sources by one.
    787  	 */
    788 	if (ipl < NIPL) {
    789 		off = pic_ipl_offset[ipl+1];
    790 		memmove(&pic__iplsources[off+1], &pic__iplsources[off],
    791 		    sizeof(pic__iplsources[0]) * (pic_ipl_offset[NIPL] - off));
    792 	}
    793 
    794 	/*
    795 	 * Advance the offset of all IPLs higher than this.  Include an
    796 	 * extra one as well.  Thus the number of sources per ipl is
    797 	 * pic_ipl_offset[ipl+1] - pic_ipl_offset[ipl].
    798 	 */
    799 	for (nipl = ipl + 1; nipl <= NIPL; nipl++)
    800 		pic_ipl_offset[nipl]++;
    801 
    802 	/*
    803 	 * Insert into the previously made position at the end of this IPL's
    804 	 * sources.
    805 	 */
    806 	off = pic_ipl_offset[ipl + 1] - 1;
    807 	is->is_iplidx = off - pic_ipl_offset[ipl];
    808 	pic__iplsources[off] = is;
    809 
    810 	(*pic->pic_ops->pic_establish_irq)(pic, is);
    811 
    812 unblock:
    813 	(*pic->pic_ops->pic_unblock_irqs)(pic, is->is_irq & ~0x1f,
    814 	    __BIT(is->is_irq & 0x1f));
    815 
    816 	if (xname) {
    817 		if (is->is_xname == NULL)
    818 			is->is_xname = kmem_zalloc(INTRDEVNAMEBUF, KM_SLEEP);
    819 		if (is->is_xname[0] != '\0')
    820 			strlcat(is->is_xname, ", ", INTRDEVNAMEBUF);
    821 		strlcat(is->is_xname, xname, INTRDEVNAMEBUF);
    822 	}
    823 
    824 	/* We're done. */
    825 	return is;
    826 }
    827 
    828 static void
    829 pic_percpu_evcnt_deattach(void *v0, void *v1, struct cpu_info *ci)
    830 {
    831 	struct pic_percpu * const pcpu = v0;
    832 	struct intrsource * const is = v1;
    833 
    834 	KASSERT(pcpu->pcpu_magic == PICPERCPU_MAGIC);
    835 	evcnt_detach(&pcpu->pcpu_evs[is->is_irq]);
    836 }
    837 
    838 void
    839 pic_disestablish_source(struct intrsource *is)
    840 {
    841 	struct pic_softc * const pic = is->is_pic;
    842 	const int irq = is->is_irq;
    843 
    844 	KASSERT(is == pic->pic_sources[irq]);
    845 
    846 	(*pic->pic_ops->pic_block_irqs)(pic, irq & ~0x1f, __BIT(irq & 0x1f));
    847 	pic->pic_sources[irq] = NULL;
    848 	pic__iplsources[pic_ipl_offset[is->is_ipl] + is->is_iplidx] = NULL;
    849 	if (is->is_xname != NULL) {
    850 		kmem_free(is->is_xname, INTRDEVNAMEBUF);
    851 		is->is_xname = NULL;
    852 	}
    853 	/*
    854 	 * Now detach the per-cpu evcnts.
    855 	 */
    856 	percpu_foreach(pic->pic_percpu, pic_percpu_evcnt_deattach, is);
    857 
    858 	kmem_free(is, sizeof(*is));
    859 }
    860 
    861 void *
    862 intr_establish(int irq, int ipl, int type, int (*func)(void *), void *arg)
    863 {
    864 	return intr_establish_xname(irq, ipl, type, func, arg, NULL);
    865 }
    866 
    867 void *
    868 intr_establish_xname(int irq, int ipl, int type, int (*func)(void *), void *arg,
    869     const char *xname)
    870 {
    871 	KASSERT(!cpu_intr_p());
    872 	KASSERT(!cpu_softintr_p());
    873 
    874 	for (size_t slot = 0; slot < PIC_MAXPICS; slot++) {
    875 		struct pic_softc * const pic = pic_list[slot];
    876 		if (pic == NULL || pic->pic_irqbase < 0)
    877 			continue;
    878 		if (pic->pic_irqbase <= irq
    879 		    && irq < pic->pic_irqbase + pic->pic_maxsources) {
    880 			return pic_establish_intr(pic, irq - pic->pic_irqbase,
    881 			    ipl, type, func, arg, xname);
    882 		}
    883 	}
    884 
    885 	return NULL;
    886 }
    887 
    888 void
    889 intr_disestablish(void *ih)
    890 {
    891 	struct intrsource * const is = ih;
    892 
    893 	KASSERT(!cpu_intr_p());
    894 	KASSERT(!cpu_softintr_p());
    895 
    896 	pic_disestablish_source(is);
    897 }
    898 
    899 void
    900 intr_mask(void *ih)
    901 {
    902 	struct intrsource * const is = ih;
    903 	struct pic_softc * const pic = is->is_pic;
    904 	const int irq = is->is_irq;
    905 
    906 	if (atomic_inc_32_nv(&is->is_mask_count) == 1)
    907 		(*pic->pic_ops->pic_block_irqs)(pic, irq & ~0x1f, __BIT(irq & 0x1f));
    908 }
    909 
    910 void
    911 intr_unmask(void *ih)
    912 {
    913 	struct intrsource * const is = ih;
    914 	struct pic_softc * const pic = is->is_pic;
    915 	const int irq = is->is_irq;
    916 
    917 	if (atomic_dec_32_nv(&is->is_mask_count) == 0)
    918 		(*pic->pic_ops->pic_unblock_irqs)(pic, irq & ~0x1f, __BIT(irq & 0x1f));
    919 }
    920 
    921 const char *
    922 intr_string(intr_handle_t irq, char *buf, size_t len)
    923 {
    924 	for (size_t slot = 0; slot < PIC_MAXPICS; slot++) {
    925 		struct pic_softc * const pic = pic_list[slot];
    926 		if (pic == NULL || pic->pic_irqbase < 0)
    927 			continue;
    928 		if (pic->pic_irqbase <= irq
    929 		    && irq < pic->pic_irqbase + pic->pic_maxsources) {
    930 			struct intrsource * const is = pic->pic_sources[irq - pic->pic_irqbase];
    931 			snprintf(buf, len, "%s %s", pic->pic_name, is->is_source);
    932 			return buf;
    933 		}
    934 	}
    935 
    936 	return NULL;
    937 }
    938 
    939 static struct intrsource *
    940 intr_get_source(const char *intrid)
    941 {
    942 	struct intrsource *is;
    943 	intrid_t buf;
    944 	size_t slot;
    945 	int irq;
    946 
    947 	KASSERT(mutex_owned(&cpu_lock));
    948 
    949 	for (slot = 0; slot < PIC_MAXPICS; slot++) {
    950 		struct pic_softc * const pic = pic_list[slot];
    951 		if (pic == NULL || pic->pic_irqbase < 0)
    952 			continue;
    953 		for (irq = 0; irq < pic->pic_maxsources; irq++) {
    954 			is = pic->pic_sources[irq];
    955 			if (is == NULL || is->is_source[0] == '\0')
    956 				continue;
    957 
    958 			snprintf(buf, sizeof(buf), "%s %s", pic->pic_name, is->is_source);
    959 			if (strcmp(buf, intrid) == 0)
    960 				return is;
    961 		}
    962 	}
    963 
    964 	return NULL;
    965 }
    966 
    967 struct intrids_handler *
    968 interrupt_construct_intrids(const kcpuset_t *cpuset)
    969 {
    970 	struct intrids_handler *iih;
    971 	struct intrsource *is;
    972 	int count, irq, n;
    973 	size_t slot;
    974 
    975 	if (kcpuset_iszero(cpuset))
    976 		return NULL;
    977 
    978 	count = 0;
    979 	for (slot = 0; slot < PIC_MAXPICS; slot++) {
    980 		struct pic_softc * const pic = pic_list[slot];
    981 		if (pic != NULL && pic->pic_irqbase >= 0) {
    982 			for (irq = 0; irq < pic->pic_maxsources; irq++) {
    983 				is = pic->pic_sources[irq];
    984 				if (is && is->is_source[0] != '\0')
    985 					count++;
    986 			}
    987 		}
    988 	}
    989 
    990 	iih = kmem_zalloc(sizeof(int) + sizeof(intrid_t) * count, KM_SLEEP);
    991 	iih->iih_nids = count;
    992 
    993 	for (n = 0, slot = 0; n < count && slot < PIC_MAXPICS; slot++) {
    994 		struct pic_softc * const pic = pic_list[slot];
    995 		if (pic == NULL || pic->pic_irqbase < 0)
    996 			continue;
    997 		for (irq = 0; irq < pic->pic_maxsources; irq++) {
    998 			is = pic->pic_sources[irq];
    999 			if (is == NULL || is->is_source[0] == '\0')
   1000 				continue;
   1001 
   1002 			snprintf(iih->iih_intrids[n++], sizeof(intrid_t), "%s %s",
   1003 			    pic->pic_name, is->is_source);
   1004 		}
   1005 	}
   1006 
   1007 	return iih;
   1008 }
   1009 
   1010 void
   1011 interrupt_destruct_intrids(struct intrids_handler *iih)
   1012 {
   1013 	if (iih == NULL)
   1014 		return;
   1015 
   1016 	kmem_free(iih, sizeof(int) + sizeof(intrid_t) * iih->iih_nids);
   1017 }
   1018 
   1019 void
   1020 interrupt_get_available(kcpuset_t *cpuset)
   1021 {
   1022 	CPU_INFO_ITERATOR cii;
   1023 	struct cpu_info *ci;
   1024 
   1025 	kcpuset_zero(cpuset);
   1026 
   1027 	mutex_enter(&cpu_lock);
   1028 	for (CPU_INFO_FOREACH(cii, ci)) {
   1029 		if ((ci->ci_schedstate.spc_flags & SPCF_NOINTR) == 0)
   1030 			kcpuset_set(cpuset, cpu_index(ci));
   1031 	}
   1032 	mutex_exit(&cpu_lock);
   1033 }
   1034 
   1035 void
   1036 interrupt_get_devname(const char *intrid, char *buf, size_t len)
   1037 {
   1038 	struct intrsource *is;
   1039 
   1040 	mutex_enter(&cpu_lock);
   1041 	is = intr_get_source(intrid);
   1042 	if (is == NULL || is->is_xname == NULL)
   1043 		buf[0] = '\0';
   1044 	else
   1045 		strlcpy(buf, is->is_xname, len);
   1046 	mutex_exit(&cpu_lock);
   1047 }
   1048 
   1049 struct interrupt_get_count_arg {
   1050 	struct intrsource *is;
   1051 	uint64_t count;
   1052 	u_int cpu_idx;
   1053 };
   1054 
   1055 static void
   1056 interrupt_get_count_cb(void *v0, void *v1, struct cpu_info *ci)
   1057 {
   1058 	struct pic_percpu * const pcpu = v0;
   1059 	struct interrupt_get_count_arg * const arg = v1;
   1060 
   1061 	if (arg->cpu_idx != cpu_index(ci))
   1062 		return;
   1063 
   1064 	arg->count = pcpu->pcpu_evs[arg->is->is_irq].ev_count;
   1065 }
   1066 
   1067 uint64_t
   1068 interrupt_get_count(const char *intrid, u_int cpu_idx)
   1069 {
   1070 	struct interrupt_get_count_arg arg;
   1071 	struct intrsource *is;
   1072 	uint64_t count;
   1073 
   1074 	count = 0;
   1075 
   1076 	mutex_enter(&cpu_lock);
   1077 	is = intr_get_source(intrid);
   1078 	if (is != NULL && is->is_pic != NULL) {
   1079 		arg.is = is;
   1080 		arg.count = 0;
   1081 		arg.cpu_idx = cpu_idx;
   1082 		percpu_foreach(is->is_pic->pic_percpu, interrupt_get_count_cb, &arg);
   1083 		count = arg.count;
   1084 	}
   1085 	mutex_exit(&cpu_lock);
   1086 
   1087 	return count;
   1088 }
   1089 
   1090 #ifdef MULTIPROCESSOR
   1091 void
   1092 interrupt_get_assigned(const char *intrid, kcpuset_t *cpuset)
   1093 {
   1094 	struct intrsource *is;
   1095 	struct pic_softc *pic;
   1096 
   1097 	kcpuset_zero(cpuset);
   1098 
   1099 	mutex_enter(&cpu_lock);
   1100 	is = intr_get_source(intrid);
   1101 	if (is != NULL) {
   1102 		pic = is->is_pic;
   1103 		if (pic && pic->pic_ops->pic_get_affinity)
   1104 			pic->pic_ops->pic_get_affinity(pic, is->is_irq, cpuset);
   1105 	}
   1106 	mutex_exit(&cpu_lock);
   1107 }
   1108 
   1109 int
   1110 interrupt_distribute_handler(const char *intrid, const kcpuset_t *newset,
   1111     kcpuset_t *oldset)
   1112 {
   1113 	struct intrsource *is;
   1114 	int error;
   1115 
   1116 	mutex_enter(&cpu_lock);
   1117 	is = intr_get_source(intrid);
   1118 	if (is == NULL) {
   1119 		error = ENOENT;
   1120 	} else {
   1121 		error = interrupt_distribute(is, newset, oldset);
   1122 	}
   1123 	mutex_exit(&cpu_lock);
   1124 
   1125 	return error;
   1126 }
   1127 
   1128 int
   1129 interrupt_distribute(void *ih, const kcpuset_t *newset, kcpuset_t *oldset)
   1130 {
   1131 	struct intrsource * const is = ih;
   1132 	struct pic_softc * const pic = is->is_pic;
   1133 
   1134 	if (pic == NULL)
   1135 		return EOPNOTSUPP;
   1136 	if (pic->pic_ops->pic_set_affinity == NULL ||
   1137 	    pic->pic_ops->pic_get_affinity == NULL)
   1138 		return EOPNOTSUPP;
   1139 
   1140 	if (!is->is_mpsafe)
   1141 		return EINVAL;
   1142 
   1143 	if (oldset != NULL)
   1144 		pic->pic_ops->pic_get_affinity(pic, is->is_irq, oldset);
   1145 
   1146 	return pic->pic_ops->pic_set_affinity(pic, is->is_irq, newset);
   1147 }
   1148 #endif
   1149