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