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