Home | History | Annotate | Line # | Download | only in pic
pic.c revision 1.55
      1 /*	$NetBSD: pic.c,v 1.55 2020/02/01 12:55:26 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.55 2020/02/01 12:55:26 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 static struct pic_pending *
     85 pic_pending_get(void)
     86 {
     87 	return percpu_getref(pic_pending_percpu);
     88 }
     89 static void
     90 pic_pending_put(struct pic_pending *pend)
     91 {
     92 	percpu_putref(pic_pending_percpu);
     93 }
     94 #else
     95 struct pic_pending pic_pending;
     96 #define	pic_pending_get()	(&pic_pending)
     97 #define	pic_pending_put(pend)	__nothing
     98 #endif /* MULTIPROCESSOR */
     99 #endif /* __HAVE_PIC_PENDING_INTRS */
    100 
    101 struct pic_softc *pic_list[PIC_MAXPICS];
    102 #if PIC_MAXPICS > 32
    103 #error PIC_MAXPICS > 32 not supported
    104 #endif
    105 struct intrsource *pic_sources[PIC_MAXMAXSOURCES];
    106 struct intrsource *pic__iplsources[PIC_MAXMAXSOURCES];
    107 struct intrsource **pic_iplsource[NIPL] = {
    108 	[0 ... NIPL-1] = pic__iplsources,
    109 };
    110 size_t pic_ipl_offset[NIPL+1];
    111 
    112 static kmutex_t pic_lock;
    113 static size_t pic_sourcebase;
    114 static int pic_lastbase;
    115 static struct evcnt pic_deferral_ev =
    116     EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "deferred", "intr");
    117 EVCNT_ATTACH_STATIC(pic_deferral_ev);
    118 
    119 static int pic_init(void);
    120 
    121 #ifdef __HAVE_PIC_SET_PRIORITY
    122 void
    123 pic_set_priority(struct cpu_info *ci, int newipl)
    124 {
    125 	register_t psw = cpsid(I32_bit);
    126 	if (pic_list[0] != NULL)
    127 		(pic_list[0]->pic_ops->pic_set_priority)(pic_list[0], newipl);
    128 	ci->ci_cpl = newipl;
    129 	if ((psw & I32_bit) == 0)
    130 		cpsie(I32_bit);
    131 }
    132 #endif
    133 
    134 #ifdef MULTIPROCESSOR
    135 int
    136 pic_ipi_ast(void *arg)
    137 {
    138 	setsoftast(curcpu());
    139 	return 1;
    140 }
    141 
    142 int
    143 pic_ipi_nop(void *arg)
    144 {
    145 	/* do nothing */
    146 	return 1;
    147 }
    148 
    149 int
    150 pic_ipi_xcall(void *arg)
    151 {
    152 	xc_ipi_handler();
    153 	return 1;
    154 }
    155 
    156 int
    157 pic_ipi_generic(void *arg)
    158 {
    159 	ipi_cpu_handler();
    160 	return 1;
    161 }
    162 
    163 #ifdef DDB
    164 int
    165 pic_ipi_ddb(void *arg)
    166 {
    167 //	printf("%s: %s: tf=%p\n", __func__, curcpu()->ci_cpuname, arg);
    168 	kdb_trap(-1, arg);
    169 	return 1;
    170 }
    171 #endif /* DDB */
    172 
    173 #ifdef __HAVE_PREEMPTION
    174 int
    175 pic_ipi_kpreempt(void *arg)
    176 {
    177 	atomic_or_uint(&curcpu()->ci_astpending, __BIT(1));
    178 	return 1;
    179 }
    180 #endif /* __HAVE_PREEMPTION */
    181 
    182 void
    183 intr_cpu_init(struct cpu_info *ci)
    184 {
    185 	for (size_t slot = 0; slot < PIC_MAXPICS; slot++) {
    186 		struct pic_softc * const pic = pic_list[slot];
    187 		if (pic != NULL && pic->pic_ops->pic_cpu_init != NULL) {
    188 			(*pic->pic_ops->pic_cpu_init)(pic, ci);
    189 		}
    190 	}
    191 }
    192 
    193 typedef void (*pic_ipi_send_func_t)(struct pic_softc *, u_long);
    194 
    195 void
    196 intr_ipi_send(const kcpuset_t *kcp, u_long ipi)
    197 {
    198 	struct cpu_info * const ci = curcpu();
    199 	KASSERT(ipi < NIPI);
    200 	bool __diagused sent_p = false;
    201 	for (size_t slot = 0; slot < PIC_MAXPICS; slot++) {
    202 		struct pic_softc * const pic = pic_list[slot];
    203 		if (pic == NULL || pic->pic_cpus == NULL)
    204 			continue;
    205 		if (kcp == NULL || kcpuset_intersecting_p(kcp, pic->pic_cpus)) {
    206 			// never send to ourself
    207 			if (pic->pic_cpus == ci->ci_kcpuset)
    208 				continue;
    209 
    210 			(*pic->pic_ops->pic_ipi_send)(pic, kcp, ipi);
    211 			// If we were targeting a single CPU or this pic
    212 			// handles all cpus, we're done.
    213 			if (kcp != NULL || pic->pic_cpus == kcpuset_running)
    214 				return;
    215 			sent_p = true;
    216 		}
    217 	}
    218 	KASSERT(cold || sent_p || ncpu <= 1);
    219 }
    220 #endif /* MULTIPROCESSOR */
    221 
    222 #ifdef __HAVE_PIC_FAST_SOFTINTS
    223 int
    224 pic_handle_softint(void *arg)
    225 {
    226 	void softint_switch(lwp_t *, int);
    227 	struct cpu_info * const ci = curcpu();
    228 	const size_t softint = (size_t) arg;
    229 	int s = splhigh();
    230 	ci->ci_intr_depth--;	// don't count these as interrupts
    231 	softint_switch(ci->ci_softlwps[softint], s);
    232 	ci->ci_intr_depth++;
    233 	splx(s);
    234 	return 1;
    235 }
    236 #endif
    237 
    238 int
    239 pic_handle_intr(void *arg)
    240 {
    241 	struct pic_softc * const pic = arg;
    242 	int rv;
    243 
    244 	rv = (*pic->pic_ops->pic_find_pending_irqs)(pic);
    245 
    246 	return rv > 0;
    247 }
    248 
    249 #if defined(__HAVE_PIC_PENDING_INTRS)
    250 void
    251 pic_mark_pending_source(struct pic_softc *pic, struct intrsource *is)
    252 {
    253 	const uint32_t ipl_mask = __BIT(is->is_ipl);
    254 
    255 	atomic_or_32(&pic->pic_pending_irqs[is->is_irq >> 5],
    256 	    __BIT(is->is_irq & 0x1f));
    257 
    258 	atomic_or_32(&pic->pic_pending_ipls, ipl_mask);
    259 	struct pic_pending *pend = pic_pending_get();
    260 	atomic_or_32(&pend->pending_ipls, ipl_mask);
    261 	atomic_or_32(&pend->pending_pics, __BIT(pic->pic_id));
    262 	pic_pending_put(pend);
    263 }
    264 
    265 void
    266 pic_mark_pending(struct pic_softc *pic, int irq)
    267 {
    268 	struct intrsource * const is = pic->pic_sources[irq];
    269 
    270 	KASSERT(irq < pic->pic_maxsources);
    271 	KASSERT(is != NULL);
    272 
    273 	pic_mark_pending_source(pic, is);
    274 }
    275 
    276 uint32_t
    277 pic_mark_pending_sources(struct pic_softc *pic, size_t irq_base,
    278 	uint32_t pending)
    279 {
    280 	struct intrsource ** const isbase = &pic->pic_sources[irq_base];
    281 	struct intrsource *is;
    282 	volatile uint32_t *ipending = &pic->pic_pending_irqs[irq_base >> 5];
    283 	uint32_t ipl_mask = 0;
    284 
    285 	if (pending == 0)
    286 		return ipl_mask;
    287 
    288 	KASSERT((irq_base & 31) == 0);
    289 
    290 	(*pic->pic_ops->pic_block_irqs)(pic, irq_base, pending);
    291 
    292 	atomic_or_32(ipending, pending);
    293 	while (pending != 0) {
    294 		int n = ffs(pending);
    295 		if (n-- == 0)
    296 			break;
    297 		is = isbase[n];
    298 		KASSERT(is != NULL);
    299 		KASSERT(irq_base <= is->is_irq && is->is_irq < irq_base + 32);
    300 		pending &= ~__BIT(n);
    301 		ipl_mask |= __BIT(is->is_ipl);
    302 	}
    303 
    304 	atomic_or_32(&pic->pic_pending_ipls, ipl_mask);
    305 	struct pic_pending *pend = pic_pending_get();
    306 	atomic_or_32(&pend->pending_ipls, ipl_mask);
    307 	atomic_or_32(&pend->pending_pics, __BIT(pic->pic_id));
    308 	pic_pending_put(pend);
    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 	struct pic_pending *pend = pic_pending_get();
    557 	while ((pend->pending_ipls & ~__BIT(newipl)) > __BIT(newipl)) {
    558 		KASSERT(pend->pending_ipls < __BIT(NIPL));
    559 		for (;;) {
    560 			int ipl = 31 - __builtin_clz(pend->pending_ipls);
    561 			KASSERT(ipl < NIPL);
    562 			if (ipl <= newipl)
    563 				break;
    564 
    565 			pic_set_priority(ci, ipl);
    566 			pic_list_deliver_irqs(pend, psw, ipl, frame);
    567 			pic_list_unblock_irqs(pend);
    568 		}
    569 	}
    570 	pic_pending_put(pend);
    571 #endif /* __HAVE_PIC_PENDING_INTRS */
    572 #ifdef __HAVE_PREEMPTION
    573 	if (newipl == IPL_NONE && (ci->ci_astpending & __BIT(1))) {
    574 		pic_set_priority(ci, IPL_SCHED);
    575 		kpreempt(0);
    576 	}
    577 #endif
    578 	if (ci->ci_cpl != newipl)
    579 		pic_set_priority(ci, newipl);
    580 }
    581 
    582 static void
    583 pic_percpu_allocate(void *v0, void *v1, struct cpu_info *ci)
    584 {
    585 	struct pic_percpu * const pcpu = v0;
    586 	struct pic_softc * const pic = v1;
    587 
    588 	pcpu->pcpu_evs = kmem_zalloc(pic->pic_maxsources * sizeof(pcpu->pcpu_evs[0]),
    589 	    KM_SLEEP);
    590 	KASSERT(pcpu->pcpu_evs != NULL);
    591 
    592 #define	PCPU_NAMELEN	32
    593 #ifdef DIAGNOSTIC
    594 	const size_t namelen = strlen(pic->pic_name) + 4 + strlen(ci->ci_data.cpu_name);
    595 #endif
    596 
    597 	KASSERT(namelen < PCPU_NAMELEN);
    598 	pcpu->pcpu_name = kmem_alloc(PCPU_NAMELEN, KM_SLEEP);
    599 #ifdef MULTIPROCESSOR
    600 	snprintf(pcpu->pcpu_name, PCPU_NAMELEN,
    601 	    "%s (%s)", pic->pic_name, ci->ci_data.cpu_name);
    602 #else
    603 	strlcpy(pcpu->pcpu_name, pic->pic_name, PCPU_NAMELEN);
    604 #endif
    605 	pcpu->pcpu_magic = PICPERCPU_MAGIC;
    606 #if 0
    607 	printf("%s: %s %s: <%s>\n",
    608 	    __func__, ci->ci_data.cpu_name, pic->pic_name,
    609 	    pcpu->pcpu_name);
    610 #endif
    611 }
    612 
    613 static int
    614 pic_init(void)
    615 {
    616 
    617 	mutex_init(&pic_lock, MUTEX_DEFAULT, IPL_HIGH);
    618 
    619 	return 0;
    620 }
    621 
    622 int
    623 pic_add(struct pic_softc *pic, int irqbase)
    624 {
    625 	int slot, maybe_slot = -1;
    626 	size_t sourcebase;
    627 	static ONCE_DECL(pic_once);
    628 
    629 	RUN_ONCE(&pic_once, pic_init);
    630 
    631 	KASSERT(strlen(pic->pic_name) > 0);
    632 
    633 #if defined(__HAVE_PIC_PENDING_INTRS) && defined(MULTIPROCESSOR)
    634 	if (__predict_false(pic_pending_percpu == NULL))
    635 		pic_pending_percpu = percpu_alloc(sizeof(struct pic_pending));
    636 #endif /* __HAVE_PIC_PENDING_INTRS && MULTIPROCESSOR */
    637 
    638 	mutex_enter(&pic_lock);
    639 	if (irqbase == PIC_IRQBASE_ALLOC) {
    640 		irqbase = pic_lastbase;
    641 	}
    642 	for (slot = 0; slot < PIC_MAXPICS; slot++) {
    643 		struct pic_softc * const xpic = pic_list[slot];
    644 		if (xpic == NULL) {
    645 			if (maybe_slot < 0)
    646 				maybe_slot = slot;
    647 			if (irqbase < 0)
    648 				break;
    649 			continue;
    650 		}
    651 		if (irqbase < 0 || xpic->pic_irqbase < 0)
    652 			continue;
    653 		if (irqbase >= xpic->pic_irqbase + xpic->pic_maxsources)
    654 			continue;
    655 		if (irqbase + pic->pic_maxsources <= xpic->pic_irqbase)
    656 			continue;
    657 		panic("pic_add: pic %s (%zu sources @ irq %u) conflicts"
    658 		    " with pic %s (%zu sources @ irq %u)",
    659 		    pic->pic_name, pic->pic_maxsources, irqbase,
    660 		    xpic->pic_name, xpic->pic_maxsources, xpic->pic_irqbase);
    661 	}
    662 	slot = maybe_slot;
    663 #if 0
    664 	printf("%s: pic_sourcebase=%zu pic_maxsources=%zu\n",
    665 	    pic->pic_name, pic_sourcebase, pic->pic_maxsources);
    666 #endif
    667 	KASSERTMSG(pic->pic_maxsources <= PIC_MAXSOURCES, "%zu",
    668 	    pic->pic_maxsources);
    669 	KASSERT(pic_sourcebase + pic->pic_maxsources <= PIC_MAXMAXSOURCES);
    670 	sourcebase = pic_sourcebase;
    671 	pic_sourcebase += pic->pic_maxsources;
    672         if (pic_lastbase < irqbase + pic->pic_maxsources)
    673                 pic_lastbase = irqbase + pic->pic_maxsources;
    674 	mutex_exit(&pic_lock);
    675 
    676 	/*
    677 	 * Allocate a pointer to each cpu's evcnts and then, for each cpu,
    678 	 * allocate its evcnts and then attach an evcnt for each pin.
    679 	 * We can't allocate the evcnt structures directly since
    680 	 * percpu will move the contents of percpu memory around and
    681 	 * corrupt the pointers in the evcnts themselves.  Remember, any
    682 	 * problem can be solved with sufficient indirection.
    683 	 */
    684 	pic->pic_percpu = percpu_create(sizeof(struct pic_percpu),
    685 	    pic_percpu_allocate, NULL, pic);
    686 
    687 	pic->pic_sources = &pic_sources[sourcebase];
    688 	pic->pic_irqbase = irqbase;
    689 	pic->pic_id = slot;
    690 #ifdef __HAVE_PIC_SET_PRIORITY
    691 	KASSERT((slot == 0) == (pic->pic_ops->pic_set_priority != NULL));
    692 #endif
    693 #ifdef MULTIPROCESSOR
    694 	KASSERT((pic->pic_cpus != NULL) == (pic->pic_ops->pic_ipi_send != NULL));
    695 #endif
    696 	pic_list[slot] = pic;
    697 
    698 	return irqbase;
    699 }
    700 
    701 int
    702 pic_alloc_irq(struct pic_softc *pic)
    703 {
    704 	int irq;
    705 
    706 	for (irq = 0; irq < pic->pic_maxsources; irq++) {
    707 		if (pic->pic_sources[irq] == NULL)
    708 			return irq;
    709 	}
    710 
    711 	return -1;
    712 }
    713 
    714 static void
    715 pic_percpu_evcnt_attach(void *v0, void *v1, struct cpu_info *ci)
    716 {
    717 	struct pic_percpu * const pcpu = v0;
    718 	struct intrsource * const is = v1;
    719 
    720 	KASSERT(pcpu->pcpu_magic == PICPERCPU_MAGIC);
    721 	evcnt_attach_dynamic(&pcpu->pcpu_evs[is->is_irq], EVCNT_TYPE_INTR, NULL,
    722 	    pcpu->pcpu_name, is->is_source);
    723 }
    724 
    725 void *
    726 pic_establish_intr(struct pic_softc *pic, int irq, int ipl, int type,
    727 	int (*func)(void *), void *arg, const char *xname)
    728 {
    729 	struct intrsource *is;
    730 	int off, nipl;
    731 
    732 	if (pic->pic_sources[irq]) {
    733 		printf("pic_establish_intr: pic %s irq %d already present\n",
    734 		    pic->pic_name, irq);
    735 		return NULL;
    736 	}
    737 
    738 	is = kmem_zalloc(sizeof(*is), KM_SLEEP);
    739 	is->is_pic = pic;
    740 	is->is_irq = irq;
    741 	is->is_ipl = ipl;
    742 	is->is_type = type & 0xff;
    743 	is->is_func = func;
    744 	is->is_arg = arg;
    745 #ifdef MULTIPROCESSOR
    746 	is->is_mpsafe = (type & IST_MPSAFE) || ipl != IPL_VM;
    747 #endif
    748 
    749 	if (pic->pic_ops->pic_source_name)
    750 		(*pic->pic_ops->pic_source_name)(pic, irq, is->is_source,
    751 		    sizeof(is->is_source));
    752 	else
    753 		snprintf(is->is_source, sizeof(is->is_source), "irq %d", irq);
    754 
    755 	/*
    756 	 * Now attach the per-cpu evcnts.
    757 	 */
    758 	percpu_foreach(pic->pic_percpu, pic_percpu_evcnt_attach, is);
    759 
    760 	pic->pic_sources[irq] = is;
    761 
    762 	/*
    763 	 * First try to use an existing slot which is empty.
    764 	 */
    765 	for (off = pic_ipl_offset[ipl]; off < pic_ipl_offset[ipl+1]; off++) {
    766 		if (pic__iplsources[off] == NULL) {
    767 			is->is_iplidx = off - pic_ipl_offset[ipl];
    768 			pic__iplsources[off] = is;
    769 			goto unblock;
    770 		}
    771 	}
    772 
    773 	/*
    774 	 * Move up all the sources by one.
    775  	 */
    776 	if (ipl < NIPL) {
    777 		off = pic_ipl_offset[ipl+1];
    778 		memmove(&pic__iplsources[off+1], &pic__iplsources[off],
    779 		    sizeof(pic__iplsources[0]) * (pic_ipl_offset[NIPL] - off));
    780 	}
    781 
    782 	/*
    783 	 * Advance the offset of all IPLs higher than this.  Include an
    784 	 * extra one as well.  Thus the number of sources per ipl is
    785 	 * pic_ipl_offset[ipl+1] - pic_ipl_offset[ipl].
    786 	 */
    787 	for (nipl = ipl + 1; nipl <= NIPL; nipl++)
    788 		pic_ipl_offset[nipl]++;
    789 
    790 	/*
    791 	 * Insert into the previously made position at the end of this IPL's
    792 	 * sources.
    793 	 */
    794 	off = pic_ipl_offset[ipl + 1] - 1;
    795 	is->is_iplidx = off - pic_ipl_offset[ipl];
    796 	pic__iplsources[off] = is;
    797 
    798 	(*pic->pic_ops->pic_establish_irq)(pic, is);
    799 
    800 unblock:
    801 	(*pic->pic_ops->pic_unblock_irqs)(pic, is->is_irq & ~0x1f,
    802 	    __BIT(is->is_irq & 0x1f));
    803 
    804 	if (xname) {
    805 		if (is->is_xname == NULL)
    806 			is->is_xname = kmem_zalloc(INTRDEVNAMEBUF, KM_SLEEP);
    807 		if (is->is_xname[0] != '\0')
    808 			strlcat(is->is_xname, ", ", INTRDEVNAMEBUF);
    809 		strlcat(is->is_xname, xname, INTRDEVNAMEBUF);
    810 	}
    811 
    812 	/* We're done. */
    813 	return is;
    814 }
    815 
    816 static void
    817 pic_percpu_evcnt_deattach(void *v0, void *v1, struct cpu_info *ci)
    818 {
    819 	struct pic_percpu * const pcpu = v0;
    820 	struct intrsource * const is = v1;
    821 
    822 	KASSERT(pcpu->pcpu_magic == PICPERCPU_MAGIC);
    823 	evcnt_detach(&pcpu->pcpu_evs[is->is_irq]);
    824 }
    825 
    826 void
    827 pic_disestablish_source(struct intrsource *is)
    828 {
    829 	struct pic_softc * const pic = is->is_pic;
    830 	const int irq = is->is_irq;
    831 
    832 	KASSERT(is == pic->pic_sources[irq]);
    833 
    834 	(*pic->pic_ops->pic_block_irqs)(pic, irq & ~0x1f, __BIT(irq & 0x1f));
    835 	pic->pic_sources[irq] = NULL;
    836 	pic__iplsources[pic_ipl_offset[is->is_ipl] + is->is_iplidx] = NULL;
    837 	if (is->is_xname != NULL) {
    838 		kmem_free(is->is_xname, INTRDEVNAMEBUF);
    839 		is->is_xname = NULL;
    840 	}
    841 	/*
    842 	 * Now detach the per-cpu evcnts.
    843 	 */
    844 	percpu_foreach(pic->pic_percpu, pic_percpu_evcnt_deattach, is);
    845 
    846 	kmem_free(is, sizeof(*is));
    847 }
    848 
    849 void *
    850 intr_establish(int irq, int ipl, int type, int (*func)(void *), void *arg)
    851 {
    852 	return intr_establish_xname(irq, ipl, type, func, arg, NULL);
    853 }
    854 
    855 void *
    856 intr_establish_xname(int irq, int ipl, int type, int (*func)(void *), void *arg,
    857     const char *xname)
    858 {
    859 	KASSERT(!cpu_intr_p());
    860 	KASSERT(!cpu_softintr_p());
    861 
    862 	for (size_t slot = 0; slot < PIC_MAXPICS; slot++) {
    863 		struct pic_softc * const pic = pic_list[slot];
    864 		if (pic == NULL || pic->pic_irqbase < 0)
    865 			continue;
    866 		if (pic->pic_irqbase <= irq
    867 		    && irq < pic->pic_irqbase + pic->pic_maxsources) {
    868 			return pic_establish_intr(pic, irq - pic->pic_irqbase,
    869 			    ipl, type, func, arg, xname);
    870 		}
    871 	}
    872 
    873 	return NULL;
    874 }
    875 
    876 void
    877 intr_disestablish(void *ih)
    878 {
    879 	struct intrsource * const is = ih;
    880 
    881 	KASSERT(!cpu_intr_p());
    882 	KASSERT(!cpu_softintr_p());
    883 
    884 	pic_disestablish_source(is);
    885 }
    886 
    887 void
    888 intr_mask(void *ih)
    889 {
    890 	struct intrsource * const is = ih;
    891 	struct pic_softc * const pic = is->is_pic;
    892 	const int irq = is->is_irq;
    893 
    894 	if (atomic_inc_32_nv(&is->is_mask_count) == 1)
    895 		(*pic->pic_ops->pic_block_irqs)(pic, irq & ~0x1f, __BIT(irq & 0x1f));
    896 }
    897 
    898 void
    899 intr_unmask(void *ih)
    900 {
    901 	struct intrsource * const is = ih;
    902 	struct pic_softc * const pic = is->is_pic;
    903 	const int irq = is->is_irq;
    904 
    905 	if (atomic_dec_32_nv(&is->is_mask_count) == 0)
    906 		(*pic->pic_ops->pic_unblock_irqs)(pic, irq & ~0x1f, __BIT(irq & 0x1f));
    907 }
    908 
    909 const char *
    910 intr_string(intr_handle_t irq, char *buf, size_t len)
    911 {
    912 	for (size_t slot = 0; slot < PIC_MAXPICS; slot++) {
    913 		struct pic_softc * const pic = pic_list[slot];
    914 		if (pic == NULL || pic->pic_irqbase < 0)
    915 			continue;
    916 		if (pic->pic_irqbase <= irq
    917 		    && irq < pic->pic_irqbase + pic->pic_maxsources) {
    918 			struct intrsource * const is = pic->pic_sources[irq - pic->pic_irqbase];
    919 			snprintf(buf, len, "%s %s", pic->pic_name, is->is_source);
    920 			return buf;
    921 		}
    922 	}
    923 
    924 	return NULL;
    925 }
    926 
    927 static struct intrsource *
    928 intr_get_source(const char *intrid)
    929 {
    930 	struct intrsource *is;
    931 	intrid_t buf;
    932 	size_t slot;
    933 	int irq;
    934 
    935 	KASSERT(mutex_owned(&cpu_lock));
    936 
    937 	for (slot = 0; slot < PIC_MAXPICS; slot++) {
    938 		struct pic_softc * const pic = pic_list[slot];
    939 		if (pic == NULL || pic->pic_irqbase < 0)
    940 			continue;
    941 		for (irq = 0; irq < pic->pic_maxsources; irq++) {
    942 			is = pic->pic_sources[irq];
    943 			if (is == NULL || is->is_source[0] == '\0')
    944 				continue;
    945 
    946 			snprintf(buf, sizeof(buf), "%s %s", pic->pic_name, is->is_source);
    947 			if (strcmp(buf, intrid) == 0)
    948 				return is;
    949 		}
    950 	}
    951 
    952 	return NULL;
    953 }
    954 
    955 struct intrids_handler *
    956 interrupt_construct_intrids(const kcpuset_t *cpuset)
    957 {
    958 	struct intrids_handler *iih;
    959 	struct intrsource *is;
    960 	int count, irq, n;
    961 	size_t slot;
    962 
    963 	if (kcpuset_iszero(cpuset))
    964 		return NULL;
    965 
    966 	count = 0;
    967 	for (slot = 0; slot < PIC_MAXPICS; slot++) {
    968 		struct pic_softc * const pic = pic_list[slot];
    969 		if (pic != NULL && pic->pic_irqbase >= 0) {
    970 			for (irq = 0; irq < pic->pic_maxsources; irq++) {
    971 				is = pic->pic_sources[irq];
    972 				if (is && is->is_source[0] != '\0')
    973 					count++;
    974 			}
    975 		}
    976 	}
    977 
    978 	iih = kmem_zalloc(sizeof(int) + sizeof(intrid_t) * count, KM_SLEEP);
    979 	iih->iih_nids = count;
    980 
    981 	for (n = 0, slot = 0; n < count && slot < PIC_MAXPICS; slot++) {
    982 		struct pic_softc * const pic = pic_list[slot];
    983 		if (pic == NULL || pic->pic_irqbase < 0)
    984 			continue;
    985 		for (irq = 0; irq < pic->pic_maxsources; irq++) {
    986 			is = pic->pic_sources[irq];
    987 			if (is == NULL || is->is_source[0] == '\0')
    988 				continue;
    989 
    990 			snprintf(iih->iih_intrids[n++], sizeof(intrid_t), "%s %s",
    991 			    pic->pic_name, is->is_source);
    992 		}
    993 	}
    994 
    995 	return iih;
    996 }
    997 
    998 void
    999 interrupt_destruct_intrids(struct intrids_handler *iih)
   1000 {
   1001 	if (iih == NULL)
   1002 		return;
   1003 
   1004 	kmem_free(iih, sizeof(int) + sizeof(intrid_t) * iih->iih_nids);
   1005 }
   1006 
   1007 void
   1008 interrupt_get_available(kcpuset_t *cpuset)
   1009 {
   1010 	CPU_INFO_ITERATOR cii;
   1011 	struct cpu_info *ci;
   1012 
   1013 	kcpuset_zero(cpuset);
   1014 
   1015 	mutex_enter(&cpu_lock);
   1016 	for (CPU_INFO_FOREACH(cii, ci)) {
   1017 		if ((ci->ci_schedstate.spc_flags & SPCF_NOINTR) == 0)
   1018 			kcpuset_set(cpuset, cpu_index(ci));
   1019 	}
   1020 	mutex_exit(&cpu_lock);
   1021 }
   1022 
   1023 void
   1024 interrupt_get_devname(const char *intrid, char *buf, size_t len)
   1025 {
   1026 	struct intrsource *is;
   1027 
   1028 	mutex_enter(&cpu_lock);
   1029 	is = intr_get_source(intrid);
   1030 	if (is == NULL || is->is_xname == NULL)
   1031 		buf[0] = '\0';
   1032 	else
   1033 		strlcpy(buf, is->is_xname, len);
   1034 	mutex_exit(&cpu_lock);
   1035 }
   1036 
   1037 struct interrupt_get_count_arg {
   1038 	struct intrsource *is;
   1039 	uint64_t count;
   1040 	u_int cpu_idx;
   1041 };
   1042 
   1043 static void
   1044 interrupt_get_count_cb(void *v0, void *v1, struct cpu_info *ci)
   1045 {
   1046 	struct pic_percpu * const pcpu = v0;
   1047 	struct interrupt_get_count_arg * const arg = v1;
   1048 
   1049 	if (arg->cpu_idx != cpu_index(ci))
   1050 		return;
   1051 
   1052 	arg->count = pcpu->pcpu_evs[arg->is->is_irq].ev_count;
   1053 }
   1054 
   1055 uint64_t
   1056 interrupt_get_count(const char *intrid, u_int cpu_idx)
   1057 {
   1058 	struct interrupt_get_count_arg arg;
   1059 	struct intrsource *is;
   1060 	uint64_t count;
   1061 
   1062 	count = 0;
   1063 
   1064 	mutex_enter(&cpu_lock);
   1065 	is = intr_get_source(intrid);
   1066 	if (is != NULL && is->is_pic != NULL) {
   1067 		arg.is = is;
   1068 		arg.count = 0;
   1069 		arg.cpu_idx = cpu_idx;
   1070 		percpu_foreach(is->is_pic->pic_percpu, interrupt_get_count_cb, &arg);
   1071 		count = arg.count;
   1072 	}
   1073 	mutex_exit(&cpu_lock);
   1074 
   1075 	return count;
   1076 }
   1077 
   1078 #ifdef MULTIPROCESSOR
   1079 void
   1080 interrupt_get_assigned(const char *intrid, kcpuset_t *cpuset)
   1081 {
   1082 	struct intrsource *is;
   1083 	struct pic_softc *pic;
   1084 
   1085 	kcpuset_zero(cpuset);
   1086 
   1087 	mutex_enter(&cpu_lock);
   1088 	is = intr_get_source(intrid);
   1089 	if (is != NULL) {
   1090 		pic = is->is_pic;
   1091 		if (pic && pic->pic_ops->pic_get_affinity)
   1092 			pic->pic_ops->pic_get_affinity(pic, is->is_irq, cpuset);
   1093 	}
   1094 	mutex_exit(&cpu_lock);
   1095 }
   1096 
   1097 int
   1098 interrupt_distribute_handler(const char *intrid, const kcpuset_t *newset,
   1099     kcpuset_t *oldset)
   1100 {
   1101 	struct intrsource *is;
   1102 	int error;
   1103 
   1104 	mutex_enter(&cpu_lock);
   1105 	is = intr_get_source(intrid);
   1106 	if (is == NULL) {
   1107 		error = ENOENT;
   1108 	} else {
   1109 		error = interrupt_distribute(is, newset, oldset);
   1110 	}
   1111 	mutex_exit(&cpu_lock);
   1112 
   1113 	return error;
   1114 }
   1115 
   1116 int
   1117 interrupt_distribute(void *ih, const kcpuset_t *newset, kcpuset_t *oldset)
   1118 {
   1119 	struct intrsource * const is = ih;
   1120 	struct pic_softc * const pic = is->is_pic;
   1121 
   1122 	if (pic == NULL)
   1123 		return EOPNOTSUPP;
   1124 	if (pic->pic_ops->pic_set_affinity == NULL ||
   1125 	    pic->pic_ops->pic_get_affinity == NULL)
   1126 		return EOPNOTSUPP;
   1127 
   1128 	if (!is->is_mpsafe)
   1129 		return EINVAL;
   1130 
   1131 	if (oldset != NULL)
   1132 		pic->pic_ops->pic_get_affinity(pic, is->is_irq, oldset);
   1133 
   1134 	return pic->pic_ops->pic_set_affinity(pic, is->is_irq, newset);
   1135 }
   1136 #endif
   1137