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