Home | History | Annotate | Line # | Download | only in pic
pic.c revision 1.23
      1  1.23     skrll /*	$NetBSD: pic.c,v 1.23 2014/10/13 09:21:06 skrll 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.21      matt 
     34   1.2      matt #include <sys/cdefs.h>
     35  1.23     skrll __KERNEL_RCSID(0, "$NetBSD: pic.c,v 1.23 2014/10/13 09:21:06 skrll Exp $");
     36   1.2      matt 
     37   1.2      matt #include <sys/param.h>
     38  1.13      matt #include <sys/atomic.h>
     39  1.13      matt #include <sys/cpu.h>
     40   1.2      matt #include <sys/evcnt.h>
     41  1.13      matt #include <sys/intr.h>
     42  1.13      matt #include <sys/kernel.h>
     43  1.11      matt #include <sys/kmem.h>
     44  1.13      matt #include <sys/xcall.h>
     45  1.22     rmind #include <sys/ipi.h>
     46   1.2      matt 
     47   1.2      matt #include <arm/armreg.h>
     48   1.2      matt #include <arm/cpufunc.h>
     49   1.2      matt 
     50  1.21      matt #ifdef DDB
     51  1.21      matt #include <arm/db_machdep.h>
     52  1.21      matt #endif
     53  1.21      matt 
     54   1.2      matt #include <arm/pic/picvar.h>
     55   1.2      matt 
     56   1.2      matt static uint32_t
     57   1.2      matt 	pic_find_pending_irqs_by_ipl(struct pic_softc *, size_t, uint32_t, int);
     58   1.2      matt static struct pic_softc *
     59   1.2      matt 	pic_list_find_pic_by_pending_ipl(uint32_t);
     60   1.2      matt static void
     61   1.2      matt 	pic_deliver_irqs(struct pic_softc *, int, void *);
     62   1.2      matt static void
     63   1.2      matt 	pic_list_deliver_irqs(register_t, int, void *);
     64   1.2      matt 
     65   1.2      matt struct pic_softc *pic_list[PIC_MAXPICS];
     66   1.2      matt #if PIC_MAXPICS > 32
     67   1.2      matt #error PIC_MAXPICS > 32 not supported
     68   1.2      matt #endif
     69   1.2      matt volatile uint32_t pic_blocked_pics;
     70   1.2      matt volatile uint32_t pic_pending_pics;
     71   1.2      matt volatile uint32_t pic_pending_ipls;
     72   1.2      matt struct intrsource *pic_sources[PIC_MAXMAXSOURCES];
     73   1.2      matt struct intrsource *pic__iplsources[PIC_MAXMAXSOURCES];
     74   1.2      matt struct intrsource **pic_iplsource[NIPL] = {
     75   1.2      matt 	[0 ... NIPL-1] = pic__iplsources,
     76   1.2      matt };
     77   1.2      matt size_t pic_ipl_offset[NIPL+1];
     78   1.2      matt size_t pic_sourcebase;
     79   1.2      matt static struct evcnt pic_deferral_ev =
     80   1.2      matt     EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "deferred", "intr");
     81   1.2      matt EVCNT_ATTACH_STATIC(pic_deferral_ev);
     82   1.2      matt 
     83  1.11      matt #ifdef __HAVE_PIC_SET_PRIORITY
     84  1.11      matt void
     85  1.11      matt pic_set_priority(struct cpu_info *ci, int newipl)
     86  1.11      matt {
     87  1.13      matt 	register_t psw = cpsid(I32_bit);
     88  1.13      matt 	if (pic_list[0] != NULL)
     89  1.13      matt 		(pic_list[0]->pic_ops->pic_set_priority)(pic_list[0], newipl);
     90  1.11      matt 	ci->ci_cpl = newipl;
     91  1.13      matt 	if ((psw & I32_bit) == 0)
     92  1.13      matt 		cpsie(I32_bit);
     93  1.13      matt }
     94  1.13      matt #endif
     95  1.13      matt 
     96  1.13      matt #ifdef MULTIPROCESSOR
     97  1.13      matt int
     98  1.13      matt pic_ipi_nop(void *arg)
     99  1.13      matt {
    100  1.13      matt 	/* do nothing */
    101  1.13      matt 	return 1;
    102  1.13      matt }
    103  1.13      matt 
    104  1.13      matt int
    105  1.13      matt pic_ipi_xcall(void *arg)
    106  1.13      matt {
    107  1.13      matt 	xc_ipi_handler();
    108  1.13      matt 	return 1;
    109  1.13      matt }
    110  1.13      matt 
    111  1.22     rmind int
    112  1.22     rmind pic_ipi_generic(void *arg)
    113  1.22     rmind {
    114  1.22     rmind 	ipi_cpu_handler();
    115  1.22     rmind 	return 1;
    116  1.22     rmind }
    117  1.22     rmind 
    118  1.21      matt #ifdef DDB
    119  1.21      matt int
    120  1.21      matt pic_ipi_ddb(void *arg)
    121  1.21      matt {
    122  1.23     skrll //	printf("%s: %s: tf=%p\n", __func__, curcpu()->ci_cpuname, arg);
    123  1.21      matt 	kdb_trap(-1, arg);
    124  1.21      matt 	return 1;
    125  1.21      matt }
    126  1.21      matt #endif
    127  1.21      matt 
    128  1.13      matt void
    129  1.13      matt intr_cpu_init(struct cpu_info *ci)
    130  1.13      matt {
    131  1.13      matt 	for (size_t slot = 0; slot < PIC_MAXPICS; slot++) {
    132  1.13      matt 		struct pic_softc * const pic = pic_list[slot];
    133  1.13      matt 		if (pic != NULL && pic->pic_ops->pic_cpu_init != NULL) {
    134  1.13      matt 			(*pic->pic_ops->pic_cpu_init)(pic, ci);
    135  1.13      matt 		}
    136  1.13      matt 	}
    137  1.13      matt }
    138  1.13      matt 
    139  1.13      matt typedef void (*pic_ipi_send_func_t)(struct pic_softc *, u_long);
    140  1.13      matt 
    141  1.13      matt static struct pic_softc *
    142  1.13      matt pic_ipi_sender(void)
    143  1.13      matt {
    144  1.13      matt 	for (size_t slot = 0; slot < PIC_MAXPICS; slot++) {
    145  1.13      matt 		struct pic_softc * const pic = pic_list[slot];
    146  1.13      matt 		if (pic != NULL && pic->pic_ops->pic_ipi_send != NULL) {
    147  1.13      matt 			return pic;
    148  1.13      matt 		}
    149  1.13      matt 	}
    150  1.13      matt 	return NULL;
    151  1.13      matt }
    152  1.13      matt 
    153  1.13      matt void
    154  1.13      matt intr_ipi_send(const kcpuset_t *kcp, u_long ipi)
    155  1.13      matt {
    156  1.13      matt 	struct pic_softc * const pic = pic_ipi_sender();
    157  1.13      matt 	KASSERT(ipi < NIPI);
    158  1.13      matt 	if (cold && pic == NULL)
    159  1.13      matt 		return;
    160  1.13      matt 	KASSERT(pic != NULL);
    161  1.13      matt 	(*pic->pic_ops->pic_ipi_send)(pic, kcp, ipi);
    162  1.13      matt }
    163  1.13      matt #endif /* MULTIPROCESSOR */
    164  1.13      matt 
    165  1.13      matt #ifdef __HAVE_PIC_FAST_SOFTINTS
    166  1.13      matt int
    167  1.13      matt pic_handle_softint(void *arg)
    168  1.13      matt {
    169  1.13      matt 	void softint_switch(lwp_t *, int);
    170  1.13      matt 	struct cpu_info * const ci = curcpu();
    171  1.13      matt 	const size_t softint = (size_t) arg;
    172  1.13      matt 	int s = splhigh();
    173  1.13      matt 	ci->ci_intr_depth--;	// don't count these as interrupts
    174  1.13      matt 	softint_switch(ci->ci_softlwps[softint], s);
    175  1.13      matt 	ci->ci_intr_depth++;
    176  1.13      matt 	splx(s);
    177  1.13      matt 	return 1;
    178  1.11      matt }
    179  1.11      matt #endif
    180   1.2      matt 
    181   1.2      matt int
    182   1.2      matt pic_handle_intr(void *arg)
    183   1.2      matt {
    184   1.2      matt 	struct pic_softc * const pic = arg;
    185   1.2      matt 	int rv;
    186   1.2      matt 
    187   1.2      matt 	rv = (*pic->pic_ops->pic_find_pending_irqs)(pic);
    188   1.2      matt 
    189   1.2      matt 	return rv > 0;
    190   1.2      matt }
    191   1.2      matt 
    192   1.2      matt void
    193   1.2      matt pic_mark_pending_source(struct pic_softc *pic, struct intrsource *is)
    194   1.2      matt {
    195   1.2      matt 	const uint32_t ipl_mask = __BIT(is->is_ipl);
    196   1.2      matt 
    197   1.4      matt 	atomic_or_32(&pic->pic_pending_irqs[is->is_irq >> 5],
    198   1.4      matt 	    __BIT(is->is_irq & 0x1f));
    199   1.2      matt 
    200   1.4      matt 	atomic_or_32(&pic->pic_pending_ipls, ipl_mask);
    201   1.4      matt 	atomic_or_32(&pic_pending_ipls, ipl_mask);
    202   1.4      matt 	atomic_or_32(&pic_pending_pics, __BIT(pic->pic_id));
    203   1.2      matt }
    204   1.2      matt 
    205   1.2      matt void
    206   1.2      matt pic_mark_pending(struct pic_softc *pic, int irq)
    207   1.2      matt {
    208   1.2      matt 	struct intrsource * const is = pic->pic_sources[irq];
    209   1.2      matt 
    210   1.2      matt 	KASSERT(irq < pic->pic_maxsources);
    211   1.2      matt 	KASSERT(is != NULL);
    212   1.2      matt 
    213   1.2      matt 	pic_mark_pending_source(pic, is);
    214   1.2      matt }
    215   1.2      matt 
    216   1.2      matt uint32_t
    217   1.2      matt pic_mark_pending_sources(struct pic_softc *pic, size_t irq_base,
    218   1.2      matt 	uint32_t pending)
    219   1.2      matt {
    220   1.2      matt 	struct intrsource ** const isbase = &pic->pic_sources[irq_base];
    221   1.2      matt 	struct intrsource *is;
    222   1.4      matt 	volatile uint32_t *ipending = &pic->pic_pending_irqs[irq_base >> 5];
    223   1.2      matt 	uint32_t ipl_mask = 0;
    224   1.2      matt 
    225   1.2      matt 	if (pending == 0)
    226   1.2      matt 		return ipl_mask;
    227   1.2      matt 
    228   1.2      matt 	KASSERT((irq_base & 31) == 0);
    229   1.2      matt 
    230   1.2      matt 	(*pic->pic_ops->pic_block_irqs)(pic, irq_base, pending);
    231   1.2      matt 
    232   1.4      matt 	atomic_or_32(ipending, pending);
    233   1.2      matt         while (pending != 0) {
    234   1.2      matt 		int n = ffs(pending);
    235   1.2      matt 		if (n-- == 0)
    236   1.2      matt 			break;
    237   1.2      matt 		is = isbase[n];
    238   1.2      matt 		KASSERT(is != NULL);
    239   1.2      matt 		KASSERT(irq_base <= is->is_irq && is->is_irq < irq_base + 32);
    240   1.2      matt 		pending &= ~__BIT(n);
    241   1.2      matt 		ipl_mask |= __BIT(is->is_ipl);
    242   1.2      matt 	}
    243   1.2      matt 
    244   1.4      matt 	atomic_or_32(&pic->pic_pending_ipls, ipl_mask);
    245   1.4      matt 	atomic_or_32(&pic_pending_ipls, ipl_mask);
    246   1.4      matt 	atomic_or_32(&pic_pending_pics, __BIT(pic->pic_id));
    247   1.2      matt 
    248   1.2      matt 	return ipl_mask;
    249   1.2      matt }
    250   1.2      matt 
    251   1.2      matt uint32_t
    252   1.2      matt pic_find_pending_irqs_by_ipl(struct pic_softc *pic, size_t irq_base,
    253   1.2      matt 	uint32_t pending, int ipl)
    254   1.2      matt {
    255   1.2      matt 	uint32_t ipl_irq_mask = 0;
    256   1.2      matt 	uint32_t irq_mask;
    257   1.2      matt 
    258   1.2      matt 	for (;;) {
    259   1.2      matt 		int irq = ffs(pending);
    260   1.2      matt 		if (irq-- == 0)
    261   1.2      matt 			return ipl_irq_mask;
    262   1.2      matt 
    263   1.2      matt 		irq_mask = __BIT(irq);
    264   1.8       bsh #if 1
    265  1.10     skrll     		KASSERTMSG(pic->pic_sources[irq_base + irq] != NULL,
    266  1.10     skrll 		   "%s: irq_base %zu irq %d\n", __func__, irq_base, irq);
    267   1.8       bsh #else
    268   1.8       bsh 		if (pic->pic_sources[irq_base + irq] == NULL) {
    269   1.8       bsh 			aprint_error("stray interrupt? irq_base=%zu irq=%d\n",
    270   1.8       bsh 			    irq_base, irq);
    271   1.8       bsh 		} else
    272   1.8       bsh #endif
    273   1.2      matt 		if (pic->pic_sources[irq_base + irq]->is_ipl == ipl)
    274   1.2      matt 			ipl_irq_mask |= irq_mask;
    275   1.2      matt 
    276   1.2      matt 		pending &= ~irq_mask;
    277   1.2      matt 	}
    278   1.2      matt }
    279   1.2      matt 
    280   1.2      matt void
    281   1.2      matt pic_dispatch(struct intrsource *is, void *frame)
    282   1.2      matt {
    283  1.20      matt 	int (*func)(void *) = is->is_func;
    284  1.20      matt 	void *arg = is->is_arg;
    285   1.2      matt 
    286  1.20      matt 	if (__predict_false(arg == NULL)) {
    287  1.20      matt 		if (__predict_false(frame == NULL)) {
    288  1.20      matt 			pic_deferral_ev.ev_count++;
    289  1.20      matt 			return;
    290  1.20      matt 		}
    291  1.20      matt 		arg = frame;
    292   1.2      matt 	}
    293  1.13      matt 
    294  1.20      matt #ifdef MULTIPROCESSOR
    295  1.20      matt 	if (!is->is_mpsafe) {
    296  1.20      matt 		KERNEL_LOCK(1, NULL);
    297  1.21      matt 		const u_int ci_blcnt __diagused = curcpu()->ci_biglock_count;
    298  1.21      matt 		const u_int l_blcnt __diagused = curlwp->l_blcnt;
    299  1.20      matt 		(void)(*func)(arg);
    300  1.21      matt 		KASSERT(ci_blcnt == curcpu()->ci_biglock_count);
    301  1.21      matt 		KASSERT(l_blcnt == curlwp->l_blcnt);
    302  1.20      matt 		KERNEL_UNLOCK_ONE(NULL);
    303  1.20      matt 	} else
    304  1.20      matt #endif
    305  1.20      matt 		(void)(*func)(arg);
    306  1.20      matt 
    307  1.20      matt 
    308  1.13      matt 	struct pic_percpu * const pcpu = percpu_getref(is->is_pic->pic_percpu);
    309  1.13      matt 	KASSERT(pcpu->pcpu_magic == PICPERCPU_MAGIC);
    310  1.13      matt 	pcpu->pcpu_evs[is->is_irq].ev_count++;
    311  1.13      matt 	percpu_putref(is->is_pic->pic_percpu);
    312   1.2      matt }
    313   1.2      matt 
    314   1.2      matt void
    315   1.2      matt pic_deliver_irqs(struct pic_softc *pic, int ipl, void *frame)
    316   1.2      matt {
    317   1.2      matt 	const uint32_t ipl_mask = __BIT(ipl);
    318   1.2      matt 	struct intrsource *is;
    319   1.4      matt 	volatile uint32_t *ipending = pic->pic_pending_irqs;
    320   1.4      matt 	volatile uint32_t *iblocked = pic->pic_blocked_irqs;
    321   1.2      matt 	size_t irq_base;
    322   1.2      matt #if PIC_MAXSOURCES > 32
    323   1.2      matt 	size_t irq_count;
    324   1.6  kiyohara 	int poi = 0;		/* Possibility of interrupting */
    325   1.2      matt #endif
    326   1.2      matt 	uint32_t pending_irqs;
    327   1.2      matt 	uint32_t blocked_irqs;
    328   1.2      matt 	int irq;
    329  1.19    martin 	bool progress __diagused = false;
    330   1.2      matt 
    331   1.2      matt 	KASSERT(pic->pic_pending_ipls & ipl_mask);
    332   1.2      matt 
    333   1.2      matt 	irq_base = 0;
    334   1.2      matt #if PIC_MAXSOURCES > 32
    335   1.2      matt 	irq_count = 0;
    336   1.2      matt #endif
    337   1.2      matt 
    338   1.2      matt 	for (;;) {
    339   1.2      matt 		pending_irqs = pic_find_pending_irqs_by_ipl(pic, irq_base,
    340   1.2      matt 		    *ipending, ipl);
    341   1.2      matt 		KASSERT((pending_irqs & *ipending) == pending_irqs);
    342   1.2      matt 		KASSERT((pending_irqs & ~(*ipending)) == 0);
    343   1.2      matt 		if (pending_irqs == 0) {
    344   1.2      matt #if PIC_MAXSOURCES > 32
    345   1.2      matt 			irq_count += 32;
    346   1.6  kiyohara 			if (__predict_true(irq_count >= pic->pic_maxsources)) {
    347   1.6  kiyohara 				if (!poi)
    348   1.6  kiyohara 					/*Interrupt at this level was handled.*/
    349   1.6  kiyohara 					break;
    350   1.6  kiyohara 				irq_base = 0;
    351   1.6  kiyohara 				irq_count = 0;
    352   1.6  kiyohara 				poi = 0;
    353   1.2      matt 				ipending = pic->pic_pending_irqs;
    354   1.2      matt 				iblocked = pic->pic_blocked_irqs;
    355   1.6  kiyohara 			} else {
    356   1.6  kiyohara 				irq_base += 32;
    357   1.6  kiyohara 				ipending++;
    358   1.6  kiyohara 				iblocked++;
    359   1.6  kiyohara 				KASSERT(irq_base <= pic->pic_maxsources);
    360   1.2      matt 			}
    361   1.2      matt 			continue;
    362   1.2      matt #else
    363   1.2      matt 			break;
    364   1.2      matt #endif
    365   1.2      matt 		}
    366   1.2      matt 		progress = true;
    367   1.5  kiyohara 		blocked_irqs = 0;
    368   1.2      matt 		do {
    369   1.2      matt 			irq = ffs(pending_irqs) - 1;
    370   1.2      matt 			KASSERT(irq >= 0);
    371   1.2      matt 
    372   1.4      matt 			atomic_and_32(ipending, ~__BIT(irq));
    373   1.2      matt 			is = pic->pic_sources[irq_base + irq];
    374   1.2      matt 			if (is != NULL) {
    375   1.2      matt 				cpsie(I32_bit);
    376   1.2      matt 				pic_dispatch(is, frame);
    377   1.2      matt 				cpsid(I32_bit);
    378   1.6  kiyohara #if PIC_MAXSOURCES > 32
    379   1.6  kiyohara 				/*
    380   1.6  kiyohara 				 * There is a possibility of interrupting
    381   1.6  kiyohara 				 * from cpsie() to cpsid().
    382   1.6  kiyohara 				 */
    383   1.6  kiyohara 				poi = 1;
    384   1.6  kiyohara #endif
    385   1.5  kiyohara 				blocked_irqs |= __BIT(irq);
    386   1.2      matt 			} else {
    387   1.2      matt 				KASSERT(0);
    388   1.2      matt 			}
    389   1.2      matt 			pending_irqs = pic_find_pending_irqs_by_ipl(pic,
    390   1.2      matt 			    irq_base, *ipending, ipl);
    391   1.2      matt 		} while (pending_irqs);
    392   1.2      matt 		if (blocked_irqs) {
    393   1.4      matt 			atomic_or_32(iblocked, blocked_irqs);
    394   1.4      matt 			atomic_or_32(&pic_blocked_pics, __BIT(pic->pic_id));
    395   1.2      matt 		}
    396   1.2      matt 	}
    397   1.2      matt 
    398   1.2      matt 	KASSERT(progress);
    399   1.2      matt 	/*
    400   1.2      matt 	 * Since interrupts are disabled, we don't have to be too careful
    401   1.2      matt 	 * about these.
    402   1.2      matt 	 */
    403   1.4      matt 	if (atomic_and_32_nv(&pic->pic_pending_ipls, ~ipl_mask) == 0)
    404   1.4      matt 		atomic_and_32(&pic_pending_pics, ~__BIT(pic->pic_id));
    405   1.2      matt }
    406   1.2      matt 
    407   1.2      matt static void
    408   1.2      matt pic_list_unblock_irqs(void)
    409   1.2      matt {
    410   1.4      matt 	uint32_t blocked_pics = pic_blocked_pics;
    411   1.2      matt 
    412   1.2      matt 	pic_blocked_pics = 0;
    413   1.2      matt 	for (;;) {
    414   1.2      matt 		struct pic_softc *pic;
    415   1.2      matt #if PIC_MAXSOURCES > 32
    416   1.4      matt 		volatile uint32_t *iblocked;
    417   1.4      matt 		uint32_t blocked;
    418   1.2      matt 		size_t irq_base;
    419   1.2      matt #endif
    420   1.2      matt 
    421   1.4      matt 		int pic_id = ffs(blocked_pics);
    422   1.2      matt 		if (pic_id-- == 0)
    423   1.2      matt 			return;
    424   1.2      matt 
    425   1.2      matt 		pic = pic_list[pic_id];
    426   1.2      matt 		KASSERT(pic != NULL);
    427   1.2      matt #if PIC_MAXSOURCES > 32
    428   1.2      matt 		for (irq_base = 0, iblocked = pic->pic_blocked_irqs;
    429   1.2      matt 		     irq_base < pic->pic_maxsources;
    430   1.2      matt 		     irq_base += 32, iblocked++) {
    431   1.4      matt 			if ((blocked = *iblocked) != 0) {
    432   1.2      matt 				(*pic->pic_ops->pic_unblock_irqs)(pic,
    433   1.4      matt 				    irq_base, blocked);
    434   1.4      matt 				atomic_and_32(iblocked, ~blocked);
    435   1.2      matt 			}
    436   1.2      matt 		}
    437   1.2      matt #else
    438   1.2      matt 		KASSERT(pic->pic_blocked_irqs[0] != 0);
    439   1.2      matt 		(*pic->pic_ops->pic_unblock_irqs)(pic,
    440   1.2      matt 		    0, pic->pic_blocked_irqs[0]);
    441   1.4      matt 		pic->pic_blocked_irqs[0] = 0;
    442   1.2      matt #endif
    443   1.4      matt 		blocked_pics &= ~__BIT(pic_id);
    444   1.2      matt 	}
    445   1.2      matt }
    446   1.2      matt 
    447   1.2      matt 
    448   1.2      matt struct pic_softc *
    449   1.2      matt pic_list_find_pic_by_pending_ipl(uint32_t ipl_mask)
    450   1.2      matt {
    451   1.4      matt 	uint32_t pending_pics = pic_pending_pics;
    452   1.2      matt 	struct pic_softc *pic;
    453   1.2      matt 
    454   1.2      matt 	for (;;) {
    455   1.4      matt 		int pic_id = ffs(pending_pics);
    456   1.2      matt 		if (pic_id-- == 0)
    457   1.2      matt 			return NULL;
    458   1.2      matt 
    459   1.2      matt 		pic = pic_list[pic_id];
    460   1.2      matt 		KASSERT(pic != NULL);
    461   1.2      matt 		if (pic->pic_pending_ipls & ipl_mask)
    462   1.2      matt 			return pic;
    463   1.4      matt 		pending_pics &= ~__BIT(pic_id);
    464   1.2      matt 	}
    465   1.2      matt }
    466   1.2      matt 
    467   1.2      matt void
    468   1.2      matt pic_list_deliver_irqs(register_t psw, int ipl, void *frame)
    469   1.2      matt {
    470   1.2      matt 	const uint32_t ipl_mask = __BIT(ipl);
    471   1.2      matt 	struct pic_softc *pic;
    472   1.2      matt 
    473   1.2      matt 	while ((pic = pic_list_find_pic_by_pending_ipl(ipl_mask)) != NULL) {
    474   1.2      matt 		pic_deliver_irqs(pic, ipl, frame);
    475   1.2      matt 		KASSERT((pic->pic_pending_ipls & ipl_mask) == 0);
    476   1.2      matt 	}
    477   1.4      matt 	atomic_and_32(&pic_pending_ipls, ~ipl_mask);
    478   1.2      matt }
    479   1.2      matt 
    480   1.2      matt void
    481   1.2      matt pic_do_pending_ints(register_t psw, int newipl, void *frame)
    482   1.2      matt {
    483   1.2      matt 	struct cpu_info * const ci = curcpu();
    484  1.13      matt 	if (__predict_false(newipl == IPL_HIGH)) {
    485  1.13      matt 		KASSERTMSG(ci->ci_cpl == IPL_HIGH, "cpl %d", ci->ci_cpl);
    486   1.2      matt 		return;
    487  1.13      matt 	}
    488   1.2      matt 	while ((pic_pending_ipls & ~__BIT(newipl)) > __BIT(newipl)) {
    489   1.2      matt 		KASSERT(pic_pending_ipls < __BIT(NIPL));
    490   1.2      matt 		for (;;) {
    491   1.2      matt 			int ipl = 31 - __builtin_clz(pic_pending_ipls);
    492   1.2      matt 			KASSERT(ipl < NIPL);
    493   1.2      matt 			if (ipl <= newipl)
    494   1.2      matt 				break;
    495   1.2      matt 
    496  1.12      matt 			pic_set_priority(ci, ipl);
    497   1.2      matt 			pic_list_deliver_irqs(psw, ipl, frame);
    498   1.2      matt 			pic_list_unblock_irqs();
    499   1.2      matt 		}
    500   1.2      matt 	}
    501   1.2      matt 	if (ci->ci_cpl != newipl)
    502  1.11      matt 		pic_set_priority(ci, newipl);
    503  1.13      matt }
    504  1.13      matt 
    505  1.13      matt static void
    506  1.13      matt pic_percpu_allocate(void *v0, void *v1, struct cpu_info *ci)
    507  1.13      matt {
    508  1.13      matt 	struct pic_percpu * const pcpu = v0;
    509  1.13      matt 	struct pic_softc * const pic = v1;
    510  1.13      matt 
    511  1.13      matt 	pcpu->pcpu_evs = kmem_zalloc(pic->pic_maxsources * sizeof(pcpu->pcpu_evs[0]),
    512  1.13      matt 	    KM_SLEEP);
    513  1.13      matt 	KASSERT(pcpu->pcpu_evs != NULL);
    514  1.13      matt 
    515  1.13      matt #define	PCPU_NAMELEN	32
    516  1.14      matt #ifdef DIAGNOSTIC
    517  1.13      matt 	const size_t namelen = strlen(pic->pic_name) + 4 + strlen(ci->ci_data.cpu_name);
    518  1.14      matt #endif
    519  1.13      matt 
    520  1.13      matt 	KASSERT(namelen < PCPU_NAMELEN);
    521  1.13      matt 	pcpu->pcpu_name = kmem_alloc(PCPU_NAMELEN, KM_SLEEP);
    522  1.13      matt #ifdef MULTIPROCESSOR
    523  1.13      matt 	snprintf(pcpu->pcpu_name, PCPU_NAMELEN,
    524  1.13      matt 	    "%s (%s)", pic->pic_name, ci->ci_data.cpu_name);
    525  1.13      matt #else
    526  1.13      matt 	strlcpy(pcpu->pcpu_name, pic->pic_name, PCPU_NAMELEN);
    527  1.13      matt #endif
    528  1.13      matt 	pcpu->pcpu_magic = PICPERCPU_MAGIC;
    529  1.13      matt #if 0
    530  1.13      matt 	printf("%s: %s %s: <%s>\n",
    531  1.13      matt 	    __func__, ci->ci_data.cpu_name, pic->pic_name,
    532  1.13      matt 	    pcpu->pcpu_name);
    533   1.2      matt #endif
    534   1.2      matt }
    535   1.2      matt 
    536   1.2      matt void
    537   1.2      matt pic_add(struct pic_softc *pic, int irqbase)
    538   1.2      matt {
    539   1.2      matt 	int slot, maybe_slot = -1;
    540   1.2      matt 
    541  1.13      matt 	KASSERT(strlen(pic->pic_name) > 0);
    542  1.13      matt 
    543   1.2      matt 	for (slot = 0; slot < PIC_MAXPICS; slot++) {
    544   1.2      matt 		struct pic_softc * const xpic = pic_list[slot];
    545   1.2      matt 		if (xpic == NULL) {
    546   1.2      matt 			if (maybe_slot < 0)
    547   1.2      matt 				maybe_slot = slot;
    548   1.2      matt 			if (irqbase < 0)
    549   1.2      matt 				break;
    550   1.2      matt 			continue;
    551   1.2      matt 		}
    552   1.2      matt 		if (irqbase < 0 || xpic->pic_irqbase < 0)
    553   1.2      matt 			continue;
    554   1.2      matt 		if (irqbase >= xpic->pic_irqbase + xpic->pic_maxsources)
    555   1.2      matt 			continue;
    556   1.2      matt 		if (irqbase + pic->pic_maxsources <= xpic->pic_irqbase)
    557   1.2      matt 			continue;
    558   1.2      matt 		panic("pic_add: pic %s (%zu sources @ irq %u) conflicts"
    559   1.2      matt 		    " with pic %s (%zu sources @ irq %u)",
    560   1.2      matt 		    pic->pic_name, pic->pic_maxsources, irqbase,
    561   1.2      matt 		    xpic->pic_name, xpic->pic_maxsources, xpic->pic_irqbase);
    562   1.2      matt 	}
    563   1.2      matt 	slot = maybe_slot;
    564   1.2      matt #if 0
    565   1.2      matt 	printf("%s: pic_sourcebase=%zu pic_maxsources=%zu\n",
    566   1.2      matt 	    pic->pic_name, pic_sourcebase, pic->pic_maxsources);
    567   1.2      matt #endif
    568  1.17      matt 	KASSERTMSG(pic->pic_maxsources <= PIC_MAXSOURCES, "%zu",
    569  1.17      matt 	    pic->pic_maxsources);
    570   1.2      matt 	KASSERT(pic_sourcebase + pic->pic_maxsources <= PIC_MAXMAXSOURCES);
    571   1.2      matt 
    572  1.13      matt 	/*
    573  1.13      matt 	 * Allocate a pointer to each cpu's evcnts and then, for each cpu,
    574  1.13      matt 	 * allocate its evcnts and then attach an evcnt for each pin.
    575  1.13      matt 	 * We can't allocate the evcnt structures directly since
    576  1.13      matt 	 * percpu will move the contents of percpu memory around and
    577  1.13      matt 	 * corrupt the pointers in the evcnts themselves.  Remember, any
    578  1.13      matt 	 * problem can be solved with sufficient indirection.
    579  1.13      matt 	 */
    580  1.13      matt 	pic->pic_percpu = percpu_alloc(sizeof(struct pic_percpu));
    581  1.13      matt 	KASSERT(pic->pic_percpu != NULL);
    582  1.13      matt 
    583  1.13      matt 	/*
    584  1.13      matt 	 * Now allocate the per-cpu evcnts.
    585  1.13      matt 	 */
    586  1.13      matt 	percpu_foreach(pic->pic_percpu, pic_percpu_allocate, pic);
    587  1.13      matt 
    588   1.2      matt 	pic->pic_sources = &pic_sources[pic_sourcebase];
    589   1.2      matt 	pic->pic_irqbase = irqbase;
    590   1.2      matt 	pic_sourcebase += pic->pic_maxsources;
    591   1.2      matt 	pic->pic_id = slot;
    592  1.13      matt #ifdef __HAVE_PIC_SET_PRIORITY
    593  1.13      matt 	KASSERT((slot == 0) == (pic->pic_ops->pic_set_priority != NULL));
    594  1.13      matt #endif
    595  1.13      matt #ifdef MULTIPROCESSOR
    596  1.13      matt 	KASSERT((slot == 0) == (pic->pic_ops->pic_ipi_send != NULL));
    597  1.13      matt #endif
    598   1.2      matt 	pic_list[slot] = pic;
    599   1.2      matt }
    600   1.2      matt 
    601   1.2      matt int
    602   1.2      matt pic_alloc_irq(struct pic_softc *pic)
    603   1.2      matt {
    604   1.2      matt 	int irq;
    605   1.2      matt 
    606   1.2      matt 	for (irq = 0; irq < pic->pic_maxsources; irq++) {
    607   1.2      matt 		if (pic->pic_sources[irq] == NULL)
    608   1.2      matt 			return irq;
    609   1.2      matt 	}
    610   1.2      matt 
    611   1.2      matt 	return -1;
    612   1.2      matt }
    613   1.2      matt 
    614  1.13      matt static void
    615  1.13      matt pic_percpu_evcnt_attach(void *v0, void *v1, struct cpu_info *ci)
    616  1.13      matt {
    617  1.13      matt 	struct pic_percpu * const pcpu = v0;
    618  1.13      matt 	struct intrsource * const is = v1;
    619  1.13      matt 
    620  1.13      matt 	KASSERT(pcpu->pcpu_magic == PICPERCPU_MAGIC);
    621  1.13      matt 	evcnt_attach_dynamic(&pcpu->pcpu_evs[is->is_irq], EVCNT_TYPE_INTR, NULL,
    622  1.13      matt 	    pcpu->pcpu_name, is->is_source);
    623  1.13      matt }
    624  1.13      matt 
    625   1.2      matt void *
    626   1.2      matt pic_establish_intr(struct pic_softc *pic, int irq, int ipl, int type,
    627   1.2      matt 	int (*func)(void *), void *arg)
    628   1.2      matt {
    629   1.2      matt 	struct intrsource *is;
    630   1.2      matt 	int off, nipl;
    631   1.2      matt 
    632   1.2      matt 	if (pic->pic_sources[irq]) {
    633   1.2      matt 		printf("pic_establish_intr: pic %s irq %d already present\n",
    634   1.2      matt 		    pic->pic_name, irq);
    635   1.2      matt 		return NULL;
    636   1.2      matt 	}
    637   1.2      matt 
    638  1.11      matt 	is = kmem_zalloc(sizeof(*is), KM_SLEEP);
    639   1.2      matt 	if (is == NULL)
    640   1.2      matt 		return NULL;
    641   1.2      matt 
    642   1.2      matt 	is->is_pic = pic;
    643   1.2      matt 	is->is_irq = irq;
    644   1.2      matt 	is->is_ipl = ipl;
    645  1.21      matt 	is->is_type = type & 0xff;
    646   1.2      matt 	is->is_func = func;
    647   1.2      matt 	is->is_arg = arg;
    648  1.20      matt #ifdef MULTIPROCESSOR
    649  1.21      matt 	is->is_mpsafe = (type & IST_MPSAFE);
    650  1.20      matt #endif
    651  1.13      matt 
    652   1.2      matt 	if (pic->pic_ops->pic_source_name)
    653   1.2      matt 		(*pic->pic_ops->pic_source_name)(pic, irq, is->is_source,
    654   1.2      matt 		    sizeof(is->is_source));
    655   1.2      matt 	else
    656   1.2      matt 		snprintf(is->is_source, sizeof(is->is_source), "irq %d", irq);
    657   1.2      matt 
    658  1.13      matt 	/*
    659  1.13      matt 	 * Now attach the per-cpu evcnts.
    660  1.13      matt 	 */
    661  1.13      matt 	percpu_foreach(pic->pic_percpu, pic_percpu_evcnt_attach, is);
    662   1.2      matt 
    663   1.2      matt 	pic->pic_sources[irq] = is;
    664   1.2      matt 
    665   1.2      matt 	/*
    666   1.2      matt 	 * First try to use an existing slot which is empty.
    667   1.2      matt 	 */
    668   1.2      matt 	for (off = pic_ipl_offset[ipl]; off < pic_ipl_offset[ipl+1]; off++) {
    669   1.2      matt 		if (pic__iplsources[off] == NULL) {
    670   1.2      matt 			is->is_iplidx = off - pic_ipl_offset[ipl];
    671   1.2      matt 			pic__iplsources[off] = is;
    672   1.2      matt 			return is;
    673   1.2      matt 		}
    674   1.2      matt 	}
    675   1.2      matt 
    676   1.2      matt 	/*
    677   1.2      matt 	 * Move up all the sources by one.
    678   1.2      matt  	 */
    679   1.2      matt 	if (ipl < NIPL) {
    680   1.2      matt 		off = pic_ipl_offset[ipl+1];
    681   1.2      matt 		memmove(&pic__iplsources[off+1], &pic__iplsources[off],
    682   1.2      matt 		    sizeof(pic__iplsources[0]) * (pic_ipl_offset[NIPL] - off));
    683   1.2      matt 	}
    684   1.2      matt 
    685   1.2      matt 	/*
    686   1.2      matt 	 * Advance the offset of all IPLs higher than this.  Include an
    687   1.2      matt 	 * extra one as well.  Thus the number of sources per ipl is
    688   1.2      matt 	 * pic_ipl_offset[ipl+1] - pic_ipl_offset[ipl].
    689   1.2      matt 	 */
    690   1.2      matt 	for (nipl = ipl + 1; nipl <= NIPL; nipl++)
    691   1.2      matt 		pic_ipl_offset[nipl]++;
    692   1.2      matt 
    693   1.2      matt 	/*
    694   1.2      matt 	 * Insert into the previously made position at the end of this IPL's
    695   1.2      matt 	 * sources.
    696   1.2      matt 	 */
    697   1.2      matt 	off = pic_ipl_offset[ipl + 1] - 1;
    698   1.2      matt 	is->is_iplidx = off - pic_ipl_offset[ipl];
    699   1.2      matt 	pic__iplsources[off] = is;
    700   1.2      matt 
    701   1.2      matt 	(*pic->pic_ops->pic_establish_irq)(pic, is);
    702   1.2      matt 
    703   1.2      matt 	(*pic->pic_ops->pic_unblock_irqs)(pic, is->is_irq & ~0x1f,
    704   1.2      matt 	    __BIT(is->is_irq & 0x1f));
    705   1.2      matt 
    706   1.2      matt 	/* We're done. */
    707   1.2      matt 	return is;
    708   1.2      matt }
    709   1.2      matt 
    710  1.13      matt static void
    711  1.13      matt pic_percpu_evcnt_deattach(void *v0, void *v1, struct cpu_info *ci)
    712  1.13      matt {
    713  1.13      matt 	struct pic_percpu * const pcpu = v0;
    714  1.13      matt 	struct intrsource * const is = v1;
    715  1.13      matt 
    716  1.13      matt 	KASSERT(pcpu->pcpu_magic == PICPERCPU_MAGIC);
    717  1.13      matt 	evcnt_detach(&pcpu->pcpu_evs[is->is_irq]);
    718  1.13      matt }
    719  1.13      matt 
    720   1.2      matt void
    721   1.2      matt pic_disestablish_source(struct intrsource *is)
    722   1.2      matt {
    723   1.2      matt 	struct pic_softc * const pic = is->is_pic;
    724   1.2      matt 	const int irq = is->is_irq;
    725   1.2      matt 
    726  1.13      matt 	KASSERT(is == pic->pic_sources[irq]);
    727  1.13      matt 
    728  1.15   msaitoh 	(*pic->pic_ops->pic_block_irqs)(pic, irq & ~0x1f, __BIT(irq & 0x1f));
    729   1.2      matt 	pic->pic_sources[irq] = NULL;
    730   1.2      matt 	pic__iplsources[pic_ipl_offset[is->is_ipl] + is->is_iplidx] = NULL;
    731  1.13      matt 	/*
    732  1.13      matt 	 * Now detach the per-cpu evcnts.
    733  1.13      matt 	 */
    734  1.13      matt 	percpu_foreach(pic->pic_percpu, pic_percpu_evcnt_deattach, is);
    735   1.2      matt 
    736  1.11      matt 	kmem_free(is, sizeof(*is));
    737   1.2      matt }
    738   1.2      matt 
    739   1.2      matt void *
    740   1.2      matt intr_establish(int irq, int ipl, int type, int (*func)(void *), void *arg)
    741   1.2      matt {
    742  1.11      matt 	KASSERT(!cpu_intr_p());
    743  1.11      matt 	KASSERT(!cpu_softintr_p());
    744  1.11      matt 
    745  1.13      matt 	for (size_t slot = 0; slot < PIC_MAXPICS; slot++) {
    746   1.2      matt 		struct pic_softc * const pic = pic_list[slot];
    747   1.2      matt 		if (pic == NULL || pic->pic_irqbase < 0)
    748   1.2      matt 			continue;
    749   1.2      matt 		if (pic->pic_irqbase <= irq
    750   1.2      matt 		    && irq < pic->pic_irqbase + pic->pic_maxsources) {
    751   1.2      matt 			return pic_establish_intr(pic, irq - pic->pic_irqbase,
    752   1.2      matt 			    ipl, type, func, arg);
    753   1.2      matt 		}
    754   1.2      matt 	}
    755   1.2      matt 
    756   1.2      matt 	return NULL;
    757   1.2      matt }
    758   1.2      matt 
    759   1.2      matt void
    760   1.2      matt intr_disestablish(void *ih)
    761   1.2      matt {
    762   1.2      matt 	struct intrsource * const is = ih;
    763  1.13      matt 
    764  1.13      matt 	KASSERT(!cpu_intr_p());
    765  1.13      matt 	KASSERT(!cpu_softintr_p());
    766  1.13      matt 
    767   1.2      matt 	pic_disestablish_source(is);
    768   1.2      matt }
    769