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