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