xref: /freebsd/sys/kern/subr_intr.c (revision df7a2251ccf99f2fa7e1f4b978da34ee3f2a0553)
12b3ad188SAdrian Chadd /*-
2bff6be3eSSvatopluk Kraus  * Copyright (c) 2015-2016 Svatopluk Kraus
3bff6be3eSSvatopluk Kraus  * Copyright (c) 2015-2016 Michal Meloun
42b3ad188SAdrian Chadd  * All rights reserved.
52b3ad188SAdrian Chadd  *
62b3ad188SAdrian Chadd  * Redistribution and use in source and binary forms, with or without
72b3ad188SAdrian Chadd  * modification, are permitted provided that the following conditions
82b3ad188SAdrian Chadd  * are met:
92b3ad188SAdrian Chadd  * 1. Redistributions of source code must retain the above copyright
102b3ad188SAdrian Chadd  *    notice, this list of conditions and the following disclaimer.
112b3ad188SAdrian Chadd  * 2. Redistributions in binary form must reproduce the above copyright
122b3ad188SAdrian Chadd  *    notice, this list of conditions and the following disclaimer in the
132b3ad188SAdrian Chadd  *    documentation and/or other materials provided with the distribution.
142b3ad188SAdrian Chadd  *
152b3ad188SAdrian Chadd  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
162b3ad188SAdrian Chadd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
172b3ad188SAdrian Chadd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
182b3ad188SAdrian Chadd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
192b3ad188SAdrian Chadd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
202b3ad188SAdrian Chadd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
212b3ad188SAdrian Chadd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
222b3ad188SAdrian Chadd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
232b3ad188SAdrian Chadd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
242b3ad188SAdrian Chadd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
252b3ad188SAdrian Chadd  * SUCH DAMAGE.
262b3ad188SAdrian Chadd  */
272b3ad188SAdrian Chadd 
282b3ad188SAdrian Chadd #include <sys/cdefs.h>
292b3ad188SAdrian Chadd __FBSDID("$FreeBSD$");
302b3ad188SAdrian Chadd 
312b3ad188SAdrian Chadd /*
322b3ad188SAdrian Chadd  *	New-style Interrupt Framework
332b3ad188SAdrian Chadd  *
342b3ad188SAdrian Chadd  *  TODO: - to support IPI (PPI) enabling on other CPUs if already started
352b3ad188SAdrian Chadd  *        - to complete things for removable PICs
362b3ad188SAdrian Chadd  */
372b3ad188SAdrian Chadd 
38bff6be3eSSvatopluk Kraus #include "opt_acpi.h"
392b3ad188SAdrian Chadd #include "opt_ddb.h"
40*df7a2251SAndrew Turner #include "opt_hwpmc_hooks.h"
412b3ad188SAdrian Chadd #include "opt_platform.h"
422b3ad188SAdrian Chadd 
432b3ad188SAdrian Chadd #include <sys/param.h>
442b3ad188SAdrian Chadd #include <sys/systm.h>
452b3ad188SAdrian Chadd #include <sys/kernel.h>
462b3ad188SAdrian Chadd #include <sys/syslog.h>
472b3ad188SAdrian Chadd #include <sys/malloc.h>
482b3ad188SAdrian Chadd #include <sys/proc.h>
492b3ad188SAdrian Chadd #include <sys/queue.h>
502b3ad188SAdrian Chadd #include <sys/bus.h>
512b3ad188SAdrian Chadd #include <sys/interrupt.h>
522b3ad188SAdrian Chadd #include <sys/conf.h>
532b3ad188SAdrian Chadd #include <sys/cpuset.h>
546b42a1f4SAndrew Turner #include <sys/rman.h>
552b3ad188SAdrian Chadd #include <sys/sched.h>
562b3ad188SAdrian Chadd #include <sys/smp.h>
57*df7a2251SAndrew Turner #ifdef HWPMC_HOOKS
58*df7a2251SAndrew Turner #include <sys/pmckern.h>
59*df7a2251SAndrew Turner #endif
60*df7a2251SAndrew Turner 
612b3ad188SAdrian Chadd #include <machine/atomic.h>
622b3ad188SAdrian Chadd #include <machine/intr.h>
632b3ad188SAdrian Chadd #include <machine/cpu.h>
642b3ad188SAdrian Chadd #include <machine/smp.h>
652b3ad188SAdrian Chadd #include <machine/stdarg.h>
662b3ad188SAdrian Chadd 
670cc5515aSAdrian Chadd #ifdef FDT
682b3ad188SAdrian Chadd #include <dev/ofw/openfirm.h>
692b3ad188SAdrian Chadd #include <dev/ofw/ofw_bus.h>
702b3ad188SAdrian Chadd #include <dev/ofw/ofw_bus_subr.h>
710cc5515aSAdrian Chadd #endif
722b3ad188SAdrian Chadd 
732b3ad188SAdrian Chadd #ifdef DDB
742b3ad188SAdrian Chadd #include <ddb/ddb.h>
752b3ad188SAdrian Chadd #endif
762b3ad188SAdrian Chadd 
772b3ad188SAdrian Chadd #include "pic_if.h"
783fc155dcSAndrew Turner #include "msi_if.h"
792b3ad188SAdrian Chadd 
802b3ad188SAdrian Chadd #define	INTRNAME_LEN	(2*MAXCOMLEN + 1)
812b3ad188SAdrian Chadd 
822b3ad188SAdrian Chadd #ifdef DEBUG
832b3ad188SAdrian Chadd #define debugf(fmt, args...) do { printf("%s(): ", __func__);	\
842b3ad188SAdrian Chadd     printf(fmt,##args); } while (0)
852b3ad188SAdrian Chadd #else
862b3ad188SAdrian Chadd #define debugf(fmt, args...)
872b3ad188SAdrian Chadd #endif
882b3ad188SAdrian Chadd 
892b3ad188SAdrian Chadd MALLOC_DECLARE(M_INTRNG);
902b3ad188SAdrian Chadd MALLOC_DEFINE(M_INTRNG, "intr", "intr interrupt handling");
912b3ad188SAdrian Chadd 
922b3ad188SAdrian Chadd /* Main interrupt handler called from assembler -> 'hidden' for C code. */
932b3ad188SAdrian Chadd void intr_irq_handler(struct trapframe *tf);
942b3ad188SAdrian Chadd 
952b3ad188SAdrian Chadd /* Root interrupt controller stuff. */
965b70c08cSSvatopluk Kraus device_t intr_irq_root_dev;
972b3ad188SAdrian Chadd static intr_irq_filter_t *irq_root_filter;
982b3ad188SAdrian Chadd static void *irq_root_arg;
992b3ad188SAdrian Chadd static u_int irq_root_ipicount;
1002b3ad188SAdrian Chadd 
1012b3ad188SAdrian Chadd /* Interrupt controller definition. */
1022b3ad188SAdrian Chadd struct intr_pic {
1032b3ad188SAdrian Chadd 	SLIST_ENTRY(intr_pic)	pic_next;
1042b3ad188SAdrian Chadd 	intptr_t		pic_xref;	/* hardware identification */
1052b3ad188SAdrian Chadd 	device_t		pic_dev;
1063fc155dcSAndrew Turner #define	FLAG_PIC	(1 << 0)
1073fc155dcSAndrew Turner #define	FLAG_MSI	(1 << 1)
1083fc155dcSAndrew Turner 	u_int			pic_flags;
1092b3ad188SAdrian Chadd };
1102b3ad188SAdrian Chadd 
1112b3ad188SAdrian Chadd static struct mtx pic_list_lock;
1122b3ad188SAdrian Chadd static SLIST_HEAD(, intr_pic) pic_list;
1132b3ad188SAdrian Chadd 
1142b3ad188SAdrian Chadd static struct intr_pic *pic_lookup(device_t dev, intptr_t xref);
1152b3ad188SAdrian Chadd 
1162b3ad188SAdrian Chadd /* Interrupt source definition. */
1172b3ad188SAdrian Chadd static struct mtx isrc_table_lock;
1182b3ad188SAdrian Chadd static struct intr_irqsrc *irq_sources[NIRQ];
1192b3ad188SAdrian Chadd u_int irq_next_free;
1202b3ad188SAdrian Chadd 
121bff6be3eSSvatopluk Kraus /*
122bff6be3eSSvatopluk Kraus  *  XXX - All stuff around struct intr_dev_data is considered as temporary
123bff6be3eSSvatopluk Kraus  *  until better place for storing struct intr_map_data will be find.
124bff6be3eSSvatopluk Kraus  *
125bff6be3eSSvatopluk Kraus  *  For now, there are two global interrupt numbers spaces:
126bff6be3eSSvatopluk Kraus  *  <0, NIRQ)                      ... interrupts without config data
127bff6be3eSSvatopluk Kraus  *                                     managed in irq_sources[]
128bff6be3eSSvatopluk Kraus  *  IRQ_DDATA_BASE + <0, 2 * NIRQ) ... interrupts with config data
129bff6be3eSSvatopluk Kraus  *                                     managed in intr_ddata_tab[]
130bff6be3eSSvatopluk Kraus  *
131bff6be3eSSvatopluk Kraus  *  Read intr_ddata_lookup() to see how these spaces are worked with.
132bff6be3eSSvatopluk Kraus  *  Note that each interrupt number from second space duplicates some number
133bff6be3eSSvatopluk Kraus  *  from first space at this moment. An interrupt number from first space can
134bff6be3eSSvatopluk Kraus  *  be duplicated even multiple times in second space.
135bff6be3eSSvatopluk Kraus  */
136bff6be3eSSvatopluk Kraus struct intr_dev_data {
137bff6be3eSSvatopluk Kraus 	device_t		idd_dev;
138bff6be3eSSvatopluk Kraus 	intptr_t		idd_xref;
139bff6be3eSSvatopluk Kraus 	u_int			idd_irq;
140cd642c88SSvatopluk Kraus 	struct intr_map_data *	idd_data;
141bff6be3eSSvatopluk Kraus 	struct intr_irqsrc *	idd_isrc;
142bff6be3eSSvatopluk Kraus };
143bff6be3eSSvatopluk Kraus 
144bff6be3eSSvatopluk Kraus static struct intr_dev_data *intr_ddata_tab[2 * NIRQ];
145bff6be3eSSvatopluk Kraus static u_int intr_ddata_first_unused;
146bff6be3eSSvatopluk Kraus 
147bff6be3eSSvatopluk Kraus #define IRQ_DDATA_BASE	10000
1488442087fSMichal Meloun CTASSERT(IRQ_DDATA_BASE > nitems(irq_sources));
149bff6be3eSSvatopluk Kraus 
1502b3ad188SAdrian Chadd #ifdef SMP
1512b3ad188SAdrian Chadd static boolean_t irq_assign_cpu = FALSE;
1522b3ad188SAdrian Chadd #endif
1532b3ad188SAdrian Chadd 
1542b3ad188SAdrian Chadd /*
1552b3ad188SAdrian Chadd  * - 2 counters for each I/O interrupt.
1562b3ad188SAdrian Chadd  * - MAXCPU counters for each IPI counters for SMP.
1572b3ad188SAdrian Chadd  */
1582b3ad188SAdrian Chadd #ifdef SMP
1592b3ad188SAdrian Chadd #define INTRCNT_COUNT   (NIRQ * 2 + INTR_IPI_COUNT * MAXCPU)
1602b3ad188SAdrian Chadd #else
1612b3ad188SAdrian Chadd #define INTRCNT_COUNT   (NIRQ * 2)
1622b3ad188SAdrian Chadd #endif
1632b3ad188SAdrian Chadd 
1642b3ad188SAdrian Chadd /* Data for MI statistics reporting. */
1652b3ad188SAdrian Chadd u_long intrcnt[INTRCNT_COUNT];
1662b3ad188SAdrian Chadd char intrnames[INTRCNT_COUNT * INTRNAME_LEN];
1672b3ad188SAdrian Chadd size_t sintrcnt = sizeof(intrcnt);
1682b3ad188SAdrian Chadd size_t sintrnames = sizeof(intrnames);
1692b3ad188SAdrian Chadd static u_int intrcnt_index;
1702b3ad188SAdrian Chadd 
1712b3ad188SAdrian Chadd /*
1722b3ad188SAdrian Chadd  *  Interrupt framework initialization routine.
1732b3ad188SAdrian Chadd  */
1742b3ad188SAdrian Chadd static void
1752b3ad188SAdrian Chadd intr_irq_init(void *dummy __unused)
1762b3ad188SAdrian Chadd {
1772b3ad188SAdrian Chadd 
1782b3ad188SAdrian Chadd 	SLIST_INIT(&pic_list);
1792b3ad188SAdrian Chadd 	mtx_init(&pic_list_lock, "intr pic list", NULL, MTX_DEF);
1803fc155dcSAndrew Turner 
1812b3ad188SAdrian Chadd 	mtx_init(&isrc_table_lock, "intr isrc table", NULL, MTX_DEF);
1822b3ad188SAdrian Chadd }
1832b3ad188SAdrian Chadd SYSINIT(intr_irq_init, SI_SUB_INTR, SI_ORDER_FIRST, intr_irq_init, NULL);
1842b3ad188SAdrian Chadd 
1852b3ad188SAdrian Chadd static void
1862b3ad188SAdrian Chadd intrcnt_setname(const char *name, int index)
1872b3ad188SAdrian Chadd {
1882b3ad188SAdrian Chadd 
1892b3ad188SAdrian Chadd 	snprintf(intrnames + INTRNAME_LEN * index, INTRNAME_LEN, "%-*s",
1902b3ad188SAdrian Chadd 	    INTRNAME_LEN - 1, name);
1912b3ad188SAdrian Chadd }
1922b3ad188SAdrian Chadd 
1932b3ad188SAdrian Chadd /*
1942b3ad188SAdrian Chadd  *  Update name for interrupt source with interrupt event.
1952b3ad188SAdrian Chadd  */
1962b3ad188SAdrian Chadd static void
1972b3ad188SAdrian Chadd intrcnt_updatename(struct intr_irqsrc *isrc)
1982b3ad188SAdrian Chadd {
1992b3ad188SAdrian Chadd 
2002b3ad188SAdrian Chadd 	/* QQQ: What about stray counter name? */
2012b3ad188SAdrian Chadd 	mtx_assert(&isrc_table_lock, MA_OWNED);
2022b3ad188SAdrian Chadd 	intrcnt_setname(isrc->isrc_event->ie_fullname, isrc->isrc_index);
2032b3ad188SAdrian Chadd }
2042b3ad188SAdrian Chadd 
2052b3ad188SAdrian Chadd /*
2062b3ad188SAdrian Chadd  *  Virtualization for interrupt source interrupt counter increment.
2072b3ad188SAdrian Chadd  */
2082b3ad188SAdrian Chadd static inline void
2092b3ad188SAdrian Chadd isrc_increment_count(struct intr_irqsrc *isrc)
2102b3ad188SAdrian Chadd {
2112b3ad188SAdrian Chadd 
212bff6be3eSSvatopluk Kraus 	if (isrc->isrc_flags & INTR_ISRCF_PPI)
213bff6be3eSSvatopluk Kraus 		atomic_add_long(&isrc->isrc_count[0], 1);
214bff6be3eSSvatopluk Kraus 	else
2152b3ad188SAdrian Chadd 		isrc->isrc_count[0]++;
2162b3ad188SAdrian Chadd }
2172b3ad188SAdrian Chadd 
2182b3ad188SAdrian Chadd /*
2192b3ad188SAdrian Chadd  *  Virtualization for interrupt source interrupt stray counter increment.
2202b3ad188SAdrian Chadd  */
2212b3ad188SAdrian Chadd static inline void
2222b3ad188SAdrian Chadd isrc_increment_straycount(struct intr_irqsrc *isrc)
2232b3ad188SAdrian Chadd {
2242b3ad188SAdrian Chadd 
2252b3ad188SAdrian Chadd 	isrc->isrc_count[1]++;
2262b3ad188SAdrian Chadd }
2272b3ad188SAdrian Chadd 
2282b3ad188SAdrian Chadd /*
2292b3ad188SAdrian Chadd  *  Virtualization for interrupt source interrupt name update.
2302b3ad188SAdrian Chadd  */
2312b3ad188SAdrian Chadd static void
2322b3ad188SAdrian Chadd isrc_update_name(struct intr_irqsrc *isrc, const char *name)
2332b3ad188SAdrian Chadd {
2342b3ad188SAdrian Chadd 	char str[INTRNAME_LEN];
2352b3ad188SAdrian Chadd 
2362b3ad188SAdrian Chadd 	mtx_assert(&isrc_table_lock, MA_OWNED);
2372b3ad188SAdrian Chadd 
2382b3ad188SAdrian Chadd 	if (name != NULL) {
2392b3ad188SAdrian Chadd 		snprintf(str, INTRNAME_LEN, "%s: %s", isrc->isrc_name, name);
2402b3ad188SAdrian Chadd 		intrcnt_setname(str, isrc->isrc_index);
2412b3ad188SAdrian Chadd 		snprintf(str, INTRNAME_LEN, "stray %s: %s", isrc->isrc_name,
2422b3ad188SAdrian Chadd 		    name);
2432b3ad188SAdrian Chadd 		intrcnt_setname(str, isrc->isrc_index + 1);
2442b3ad188SAdrian Chadd 	} else {
2452b3ad188SAdrian Chadd 		snprintf(str, INTRNAME_LEN, "%s:", isrc->isrc_name);
2462b3ad188SAdrian Chadd 		intrcnt_setname(str, isrc->isrc_index);
2472b3ad188SAdrian Chadd 		snprintf(str, INTRNAME_LEN, "stray %s:", isrc->isrc_name);
2482b3ad188SAdrian Chadd 		intrcnt_setname(str, isrc->isrc_index + 1);
2492b3ad188SAdrian Chadd 	}
2502b3ad188SAdrian Chadd }
2512b3ad188SAdrian Chadd 
2522b3ad188SAdrian Chadd /*
2532b3ad188SAdrian Chadd  *  Virtualization for interrupt source interrupt counters setup.
2542b3ad188SAdrian Chadd  */
2552b3ad188SAdrian Chadd static void
2562b3ad188SAdrian Chadd isrc_setup_counters(struct intr_irqsrc *isrc)
2572b3ad188SAdrian Chadd {
2582b3ad188SAdrian Chadd 	u_int index;
2592b3ad188SAdrian Chadd 
2602b3ad188SAdrian Chadd 	/*
2612b3ad188SAdrian Chadd 	 *  XXX - it does not work well with removable controllers and
2622b3ad188SAdrian Chadd 	 *        interrupt sources !!!
2632b3ad188SAdrian Chadd 	 */
2642b3ad188SAdrian Chadd 	index = atomic_fetchadd_int(&intrcnt_index, 2);
2652b3ad188SAdrian Chadd 	isrc->isrc_index = index;
2662b3ad188SAdrian Chadd 	isrc->isrc_count = &intrcnt[index];
2672b3ad188SAdrian Chadd 	isrc_update_name(isrc, NULL);
2682b3ad188SAdrian Chadd }
2692b3ad188SAdrian Chadd 
270bff6be3eSSvatopluk Kraus /*
271bff6be3eSSvatopluk Kraus  *  Virtualization for interrupt source interrupt counters release.
272bff6be3eSSvatopluk Kraus  */
273bff6be3eSSvatopluk Kraus static void
274bff6be3eSSvatopluk Kraus isrc_release_counters(struct intr_irqsrc *isrc)
275bff6be3eSSvatopluk Kraus {
276bff6be3eSSvatopluk Kraus 
277bff6be3eSSvatopluk Kraus 	panic("%s: not implemented", __func__);
278bff6be3eSSvatopluk Kraus }
279bff6be3eSSvatopluk Kraus 
2802b3ad188SAdrian Chadd #ifdef SMP
2812b3ad188SAdrian Chadd /*
2822b3ad188SAdrian Chadd  *  Virtualization for interrupt source IPI counters setup.
2832b3ad188SAdrian Chadd  */
2845b70c08cSSvatopluk Kraus u_long *
2855b70c08cSSvatopluk Kraus intr_ipi_setup_counters(const char *name)
2862b3ad188SAdrian Chadd {
2872b3ad188SAdrian Chadd 	u_int index, i;
2882b3ad188SAdrian Chadd 	char str[INTRNAME_LEN];
2892b3ad188SAdrian Chadd 
2902b3ad188SAdrian Chadd 	index = atomic_fetchadd_int(&intrcnt_index, MAXCPU);
2912b3ad188SAdrian Chadd 	for (i = 0; i < MAXCPU; i++) {
2922b3ad188SAdrian Chadd 		snprintf(str, INTRNAME_LEN, "cpu%d:%s", i, name);
2932b3ad188SAdrian Chadd 		intrcnt_setname(str, index + i);
2942b3ad188SAdrian Chadd 	}
2955b70c08cSSvatopluk Kraus 	return (&intrcnt[index]);
2962b3ad188SAdrian Chadd }
2972b3ad188SAdrian Chadd #endif
2982b3ad188SAdrian Chadd 
2992b3ad188SAdrian Chadd /*
3002b3ad188SAdrian Chadd  *  Main interrupt dispatch handler. It's called straight
3012b3ad188SAdrian Chadd  *  from the assembler, where CPU interrupt is served.
3022b3ad188SAdrian Chadd  */
3032b3ad188SAdrian Chadd void
3042b3ad188SAdrian Chadd intr_irq_handler(struct trapframe *tf)
3052b3ad188SAdrian Chadd {
3062b3ad188SAdrian Chadd 	struct trapframe * oldframe;
3072b3ad188SAdrian Chadd 	struct thread * td;
3082b3ad188SAdrian Chadd 
3092b3ad188SAdrian Chadd 	KASSERT(irq_root_filter != NULL, ("%s: no filter", __func__));
3102b3ad188SAdrian Chadd 
3112b3ad188SAdrian Chadd 	PCPU_INC(cnt.v_intr);
3122b3ad188SAdrian Chadd 	critical_enter();
3132b3ad188SAdrian Chadd 	td = curthread;
3142b3ad188SAdrian Chadd 	oldframe = td->td_intr_frame;
3152b3ad188SAdrian Chadd 	td->td_intr_frame = tf;
3162b3ad188SAdrian Chadd 	irq_root_filter(irq_root_arg);
3172b3ad188SAdrian Chadd 	td->td_intr_frame = oldframe;
3182b3ad188SAdrian Chadd 	critical_exit();
319*df7a2251SAndrew Turner #ifdef HWPMC_HOOKS
320*df7a2251SAndrew Turner 	if (pmc_hook && (PCPU_GET(curthread)->td_pflags & TDP_CALLCHAIN))
321*df7a2251SAndrew Turner 		pmc_hook(PCPU_GET(curthread), PMC_FN_USER_CALLCHAIN, tf);
322*df7a2251SAndrew Turner #endif
3232b3ad188SAdrian Chadd }
3242b3ad188SAdrian Chadd 
3252b3ad188SAdrian Chadd /*
3262b3ad188SAdrian Chadd  *  interrupt controller dispatch function for interrupts. It should
3272b3ad188SAdrian Chadd  *  be called straight from the interrupt controller, when associated interrupt
3282b3ad188SAdrian Chadd  *  source is learned.
3292b3ad188SAdrian Chadd  */
330bff6be3eSSvatopluk Kraus int
331bff6be3eSSvatopluk Kraus intr_isrc_dispatch(struct intr_irqsrc *isrc, struct trapframe *tf)
3322b3ad188SAdrian Chadd {
3332b3ad188SAdrian Chadd 
3342b3ad188SAdrian Chadd 	KASSERT(isrc != NULL, ("%s: no source", __func__));
3352b3ad188SAdrian Chadd 
3362b3ad188SAdrian Chadd 	isrc_increment_count(isrc);
3372b3ad188SAdrian Chadd 
3382b3ad188SAdrian Chadd #ifdef INTR_SOLO
3392b3ad188SAdrian Chadd 	if (isrc->isrc_filter != NULL) {
3402b3ad188SAdrian Chadd 		int error;
3412b3ad188SAdrian Chadd 		error = isrc->isrc_filter(isrc->isrc_arg, tf);
3422b3ad188SAdrian Chadd 		PIC_POST_FILTER(isrc->isrc_dev, isrc);
3432b3ad188SAdrian Chadd 		if (error == FILTER_HANDLED)
344bff6be3eSSvatopluk Kraus 			return (0);
3452b3ad188SAdrian Chadd 	} else
3462b3ad188SAdrian Chadd #endif
3472b3ad188SAdrian Chadd 	if (isrc->isrc_event != NULL) {
3482b3ad188SAdrian Chadd 		if (intr_event_handle(isrc->isrc_event, tf) == 0)
349bff6be3eSSvatopluk Kraus 			return (0);
3502b3ad188SAdrian Chadd 	}
3512b3ad188SAdrian Chadd 
3522b3ad188SAdrian Chadd 	isrc_increment_straycount(isrc);
353bff6be3eSSvatopluk Kraus 	return (EINVAL);
3542b3ad188SAdrian Chadd }
3552b3ad188SAdrian Chadd 
3562b3ad188SAdrian Chadd /*
3572b3ad188SAdrian Chadd  *  Alloc unique interrupt number (resource handle) for interrupt source.
3582b3ad188SAdrian Chadd  *
3592b3ad188SAdrian Chadd  *  There could be various strategies how to allocate free interrupt number
3602b3ad188SAdrian Chadd  *  (resource handle) for new interrupt source.
3612b3ad188SAdrian Chadd  *
3622b3ad188SAdrian Chadd  *  1. Handles are always allocated forward, so handles are not recycled
3632b3ad188SAdrian Chadd  *     immediately. However, if only one free handle left which is reused
3642b3ad188SAdrian Chadd  *     constantly...
3652b3ad188SAdrian Chadd  */
366bff6be3eSSvatopluk Kraus static inline int
367bff6be3eSSvatopluk Kraus isrc_alloc_irq(struct intr_irqsrc *isrc)
3682b3ad188SAdrian Chadd {
3692b3ad188SAdrian Chadd 	u_int maxirqs, irq;
3702b3ad188SAdrian Chadd 
3712b3ad188SAdrian Chadd 	mtx_assert(&isrc_table_lock, MA_OWNED);
3722b3ad188SAdrian Chadd 
3732b3ad188SAdrian Chadd 	maxirqs = nitems(irq_sources);
3742b3ad188SAdrian Chadd 	if (irq_next_free >= maxirqs)
3752b3ad188SAdrian Chadd 		return (ENOSPC);
3762b3ad188SAdrian Chadd 
3772b3ad188SAdrian Chadd 	for (irq = irq_next_free; irq < maxirqs; irq++) {
3782b3ad188SAdrian Chadd 		if (irq_sources[irq] == NULL)
3792b3ad188SAdrian Chadd 			goto found;
3802b3ad188SAdrian Chadd 	}
3812b3ad188SAdrian Chadd 	for (irq = 0; irq < irq_next_free; irq++) {
3822b3ad188SAdrian Chadd 		if (irq_sources[irq] == NULL)
3832b3ad188SAdrian Chadd 			goto found;
3842b3ad188SAdrian Chadd 	}
3852b3ad188SAdrian Chadd 
3862b3ad188SAdrian Chadd 	irq_next_free = maxirqs;
3872b3ad188SAdrian Chadd 	return (ENOSPC);
3882b3ad188SAdrian Chadd 
3892b3ad188SAdrian Chadd found:
3902b3ad188SAdrian Chadd 	isrc->isrc_irq = irq;
3912b3ad188SAdrian Chadd 	irq_sources[irq] = isrc;
3922b3ad188SAdrian Chadd 
3932b3ad188SAdrian Chadd 	irq_next_free = irq + 1;
3942b3ad188SAdrian Chadd 	if (irq_next_free >= maxirqs)
3952b3ad188SAdrian Chadd 		irq_next_free = 0;
3962b3ad188SAdrian Chadd 	return (0);
3972b3ad188SAdrian Chadd }
398bff6be3eSSvatopluk Kraus 
3992b3ad188SAdrian Chadd /*
4002b3ad188SAdrian Chadd  *  Free unique interrupt number (resource handle) from interrupt source.
4012b3ad188SAdrian Chadd  */
402bff6be3eSSvatopluk Kraus static inline int
4032b3ad188SAdrian Chadd isrc_free_irq(struct intr_irqsrc *isrc)
4042b3ad188SAdrian Chadd {
4052b3ad188SAdrian Chadd 
406bff6be3eSSvatopluk Kraus 	mtx_assert(&isrc_table_lock, MA_OWNED);
4072b3ad188SAdrian Chadd 
408bff6be3eSSvatopluk Kraus 	if (isrc->isrc_irq >= nitems(irq_sources))
4092b3ad188SAdrian Chadd 		return (EINVAL);
410bff6be3eSSvatopluk Kraus 	if (irq_sources[isrc->isrc_irq] != isrc)
4112b3ad188SAdrian Chadd 		return (EINVAL);
4122b3ad188SAdrian Chadd 
4132b3ad188SAdrian Chadd 	irq_sources[isrc->isrc_irq] = NULL;
4148442087fSMichal Meloun 	isrc->isrc_irq = INTR_IRQ_INVALID;	/* just to be safe */
4152b3ad188SAdrian Chadd 	return (0);
4162b3ad188SAdrian Chadd }
417bff6be3eSSvatopluk Kraus 
4182b3ad188SAdrian Chadd /*
4192b3ad188SAdrian Chadd  *  Lookup interrupt source by interrupt number (resource handle).
4202b3ad188SAdrian Chadd  */
421bff6be3eSSvatopluk Kraus static inline struct intr_irqsrc *
4222b3ad188SAdrian Chadd isrc_lookup(u_int irq)
4232b3ad188SAdrian Chadd {
4242b3ad188SAdrian Chadd 
4252b3ad188SAdrian Chadd 	if (irq < nitems(irq_sources))
4262b3ad188SAdrian Chadd 		return (irq_sources[irq]);
4272b3ad188SAdrian Chadd 	return (NULL);
4282b3ad188SAdrian Chadd }
4292b3ad188SAdrian Chadd 
4302b3ad188SAdrian Chadd /*
431bff6be3eSSvatopluk Kraus  *  Initialize interrupt source and register it into global interrupt table.
4322b3ad188SAdrian Chadd  */
433bff6be3eSSvatopluk Kraus int
434bff6be3eSSvatopluk Kraus intr_isrc_register(struct intr_irqsrc *isrc, device_t dev, u_int flags,
435bff6be3eSSvatopluk Kraus     const char *fmt, ...)
4362b3ad188SAdrian Chadd {
437bff6be3eSSvatopluk Kraus 	int error;
438bff6be3eSSvatopluk Kraus 	va_list ap;
4392b3ad188SAdrian Chadd 
440bff6be3eSSvatopluk Kraus 	bzero(isrc, sizeof(struct intr_irqsrc));
441bff6be3eSSvatopluk Kraus 	isrc->isrc_dev = dev;
4428442087fSMichal Meloun 	isrc->isrc_irq = INTR_IRQ_INVALID;	/* just to be safe */
443bff6be3eSSvatopluk Kraus 	isrc->isrc_flags = flags;
4442b3ad188SAdrian Chadd 
445bff6be3eSSvatopluk Kraus 	va_start(ap, fmt);
446bff6be3eSSvatopluk Kraus 	vsnprintf(isrc->isrc_name, INTR_ISRC_NAMELEN, fmt, ap);
447bff6be3eSSvatopluk Kraus 	va_end(ap);
448bff6be3eSSvatopluk Kraus 
449bff6be3eSSvatopluk Kraus 	mtx_lock(&isrc_table_lock);
450bff6be3eSSvatopluk Kraus 	error = isrc_alloc_irq(isrc);
451bff6be3eSSvatopluk Kraus 	if (error != 0) {
452bff6be3eSSvatopluk Kraus 		mtx_unlock(&isrc_table_lock);
453bff6be3eSSvatopluk Kraus 		return (error);
4542b3ad188SAdrian Chadd 	}
455bff6be3eSSvatopluk Kraus 	/*
456bff6be3eSSvatopluk Kraus 	 * Setup interrupt counters, but not for IPI sources. Those are setup
457bff6be3eSSvatopluk Kraus 	 * later and only for used ones (up to INTR_IPI_COUNT) to not exhaust
458bff6be3eSSvatopluk Kraus 	 * our counter pool.
459bff6be3eSSvatopluk Kraus 	 */
460bff6be3eSSvatopluk Kraus 	if ((isrc->isrc_flags & INTR_ISRCF_IPI) == 0)
461bff6be3eSSvatopluk Kraus 		isrc_setup_counters(isrc);
462bff6be3eSSvatopluk Kraus 	mtx_unlock(&isrc_table_lock);
463bff6be3eSSvatopluk Kraus 	return (0);
4642b3ad188SAdrian Chadd }
4652b3ad188SAdrian Chadd 
4662b3ad188SAdrian Chadd /*
467bff6be3eSSvatopluk Kraus  *  Deregister interrupt source from global interrupt table.
468bff6be3eSSvatopluk Kraus  */
469bff6be3eSSvatopluk Kraus int
470bff6be3eSSvatopluk Kraus intr_isrc_deregister(struct intr_irqsrc *isrc)
471bff6be3eSSvatopluk Kraus {
472bff6be3eSSvatopluk Kraus 	int error;
473bff6be3eSSvatopluk Kraus 
474bff6be3eSSvatopluk Kraus 	mtx_lock(&isrc_table_lock);
475bff6be3eSSvatopluk Kraus 	if ((isrc->isrc_flags & INTR_ISRCF_IPI) == 0)
476bff6be3eSSvatopluk Kraus 		isrc_release_counters(isrc);
477bff6be3eSSvatopluk Kraus 	error = isrc_free_irq(isrc);
478bff6be3eSSvatopluk Kraus 	mtx_unlock(&isrc_table_lock);
479bff6be3eSSvatopluk Kraus 	return (error);
480bff6be3eSSvatopluk Kraus }
481bff6be3eSSvatopluk Kraus 
4825b613c19SSvatopluk Kraus #ifdef SMP
4835b613c19SSvatopluk Kraus /*
4845b613c19SSvatopluk Kraus  *  A support function for a PIC to decide if provided ISRC should be inited
4855b613c19SSvatopluk Kraus  *  on given cpu. The logic of INTR_ISRCF_BOUND flag and isrc_cpu member of
4865b613c19SSvatopluk Kraus  *  struct intr_irqsrc is the following:
4875b613c19SSvatopluk Kraus  *
4885b613c19SSvatopluk Kraus  *     If INTR_ISRCF_BOUND is set, the ISRC should be inited only on cpus
4895b613c19SSvatopluk Kraus  *     set in isrc_cpu. If not, the ISRC should be inited on every cpu and
4905b613c19SSvatopluk Kraus  *     isrc_cpu is kept consistent with it. Thus isrc_cpu is always correct.
4915b613c19SSvatopluk Kraus  */
4925b613c19SSvatopluk Kraus bool
4935b613c19SSvatopluk Kraus intr_isrc_init_on_cpu(struct intr_irqsrc *isrc, u_int cpu)
4945b613c19SSvatopluk Kraus {
4955b613c19SSvatopluk Kraus 
4965b613c19SSvatopluk Kraus 	if (isrc->isrc_handlers == 0)
4975b613c19SSvatopluk Kraus 		return (false);
4985b613c19SSvatopluk Kraus 	if ((isrc->isrc_flags & (INTR_ISRCF_PPI | INTR_ISRCF_IPI)) == 0)
4995b613c19SSvatopluk Kraus 		return (false);
5005b613c19SSvatopluk Kraus 	if (isrc->isrc_flags & INTR_ISRCF_BOUND)
5015b613c19SSvatopluk Kraus 		return (CPU_ISSET(cpu, &isrc->isrc_cpu));
5025b613c19SSvatopluk Kraus 
5035b613c19SSvatopluk Kraus 	CPU_SET(cpu, &isrc->isrc_cpu);
5045b613c19SSvatopluk Kraus 	return (true);
5055b613c19SSvatopluk Kraus }
5065b613c19SSvatopluk Kraus #endif
5075b613c19SSvatopluk Kraus 
508bff6be3eSSvatopluk Kraus static struct intr_dev_data *
509bff6be3eSSvatopluk Kraus intr_ddata_alloc(u_int extsize)
510bff6be3eSSvatopluk Kraus {
511bff6be3eSSvatopluk Kraus 	struct intr_dev_data *ddata;
512cd642c88SSvatopluk Kraus 	size_t size;
513bff6be3eSSvatopluk Kraus 
514cd642c88SSvatopluk Kraus 	size = sizeof(*ddata);
515cd642c88SSvatopluk Kraus 	ddata = malloc(size + extsize, M_INTRNG, M_WAITOK | M_ZERO);
516bff6be3eSSvatopluk Kraus 
517bff6be3eSSvatopluk Kraus 	mtx_lock(&isrc_table_lock);
518bff6be3eSSvatopluk Kraus 	if (intr_ddata_first_unused >= nitems(intr_ddata_tab)) {
519bff6be3eSSvatopluk Kraus 		mtx_unlock(&isrc_table_lock);
520bff6be3eSSvatopluk Kraus 		free(ddata, M_INTRNG);
521bff6be3eSSvatopluk Kraus 		return (NULL);
522bff6be3eSSvatopluk Kraus 	}
523bff6be3eSSvatopluk Kraus 	intr_ddata_tab[intr_ddata_first_unused] = ddata;
524bff6be3eSSvatopluk Kraus 	ddata->idd_irq = IRQ_DDATA_BASE + intr_ddata_first_unused++;
525bff6be3eSSvatopluk Kraus 	mtx_unlock(&isrc_table_lock);
526cd642c88SSvatopluk Kraus 
527cd642c88SSvatopluk Kraus 	ddata->idd_data = (struct intr_map_data *)((uintptr_t)ddata + size);
528a100280eSSvatopluk Kraus 	ddata->idd_data->size = extsize;
529bff6be3eSSvatopluk Kraus 	return (ddata);
530bff6be3eSSvatopluk Kraus }
531bff6be3eSSvatopluk Kraus 
532bff6be3eSSvatopluk Kraus static struct intr_irqsrc *
533bff6be3eSSvatopluk Kraus intr_ddata_lookup(u_int irq, struct intr_map_data **datap)
534bff6be3eSSvatopluk Kraus {
535bff6be3eSSvatopluk Kraus 	int error;
536bff6be3eSSvatopluk Kraus 	struct intr_irqsrc *isrc;
537bff6be3eSSvatopluk Kraus 	struct intr_dev_data *ddata;
538bff6be3eSSvatopluk Kraus 
539bff6be3eSSvatopluk Kraus 	isrc = isrc_lookup(irq);
540bff6be3eSSvatopluk Kraus 	if (isrc != NULL) {
541bff6be3eSSvatopluk Kraus 		if (datap != NULL)
542bff6be3eSSvatopluk Kraus 			*datap = NULL;
543bff6be3eSSvatopluk Kraus 		return (isrc);
544bff6be3eSSvatopluk Kraus 	}
545bff6be3eSSvatopluk Kraus 
546bff6be3eSSvatopluk Kraus 	if (irq < IRQ_DDATA_BASE)
547bff6be3eSSvatopluk Kraus 		return (NULL);
548bff6be3eSSvatopluk Kraus 
549bff6be3eSSvatopluk Kraus 	irq -= IRQ_DDATA_BASE;
550bff6be3eSSvatopluk Kraus 	if (irq >= nitems(intr_ddata_tab))
551bff6be3eSSvatopluk Kraus 		return (NULL);
552bff6be3eSSvatopluk Kraus 
553bff6be3eSSvatopluk Kraus 	ddata = intr_ddata_tab[irq];
554bff6be3eSSvatopluk Kraus 	if (ddata->idd_isrc == NULL) {
555bff6be3eSSvatopluk Kraus 		error = intr_map_irq(ddata->idd_dev, ddata->idd_xref,
556cd642c88SSvatopluk Kraus 		    ddata->idd_data, &irq);
557bff6be3eSSvatopluk Kraus 		if (error != 0)
558bff6be3eSSvatopluk Kraus 			return (NULL);
559bff6be3eSSvatopluk Kraus 		ddata->idd_isrc = isrc_lookup(irq);
560bff6be3eSSvatopluk Kraus 	}
561bff6be3eSSvatopluk Kraus 	if (datap != NULL)
562cd642c88SSvatopluk Kraus 		*datap = ddata->idd_data;
563bff6be3eSSvatopluk Kraus 	return (ddata->idd_isrc);
564bff6be3eSSvatopluk Kraus }
565bff6be3eSSvatopluk Kraus 
566bff6be3eSSvatopluk Kraus #ifdef DEV_ACPI
567bff6be3eSSvatopluk Kraus /*
568bff6be3eSSvatopluk Kraus  *  Map interrupt source according to ACPI info into framework. If such mapping
5692b3ad188SAdrian Chadd  *  does not exist, create it. Return unique interrupt number (resource handle)
5702b3ad188SAdrian Chadd  *  associated with mapped interrupt source.
5712b3ad188SAdrian Chadd  */
5722b3ad188SAdrian Chadd u_int
573bff6be3eSSvatopluk Kraus intr_acpi_map_irq(device_t dev, u_int irq, enum intr_polarity pol,
574bff6be3eSSvatopluk Kraus     enum intr_trigger trig)
5752b3ad188SAdrian Chadd {
576cd642c88SSvatopluk Kraus 	struct intr_map_data_acpi *daa;
577bff6be3eSSvatopluk Kraus 	struct intr_dev_data *ddata;
5782b3ad188SAdrian Chadd 
579cd642c88SSvatopluk Kraus 	ddata = intr_ddata_alloc(sizeof(struct intr_map_data_acpi));
580bff6be3eSSvatopluk Kraus 	if (ddata == NULL)
5818442087fSMichal Meloun 		return (INTR_IRQ_INVALID);	/* no space left */
5822b3ad188SAdrian Chadd 
583bff6be3eSSvatopluk Kraus 	ddata->idd_dev = dev;
584cd642c88SSvatopluk Kraus 	ddata->idd_data->type = INTR_MAP_DATA_ACPI;
585cd642c88SSvatopluk Kraus 
586cd642c88SSvatopluk Kraus 	daa = (struct intr_map_data_acpi *)ddata->idd_data;
587cd642c88SSvatopluk Kraus 	daa->irq = irq;
588cd642c88SSvatopluk Kraus 	daa->pol = pol;
589cd642c88SSvatopluk Kraus 	daa->trig = trig;
590cd642c88SSvatopluk Kraus 
591bff6be3eSSvatopluk Kraus 	return (ddata->idd_irq);
5922b3ad188SAdrian Chadd }
593bff6be3eSSvatopluk Kraus #endif
5942b3ad188SAdrian Chadd #ifdef FDT
5952b3ad188SAdrian Chadd /*
5962b3ad188SAdrian Chadd  *  Map interrupt source according to FDT data into framework. If such mapping
5972b3ad188SAdrian Chadd  *  does not exist, create it. Return unique interrupt number (resource handle)
5982b3ad188SAdrian Chadd  *  associated with mapped interrupt source.
5992b3ad188SAdrian Chadd  */
6002b3ad188SAdrian Chadd u_int
6012b3ad188SAdrian Chadd intr_fdt_map_irq(phandle_t node, pcell_t *cells, u_int ncells)
6022b3ad188SAdrian Chadd {
603cd642c88SSvatopluk Kraus 	size_t cellsize;
604bff6be3eSSvatopluk Kraus 	struct intr_dev_data *ddata;
605cd642c88SSvatopluk Kraus 	struct intr_map_data_fdt *daf;
6062b3ad188SAdrian Chadd 
6072b3ad188SAdrian Chadd 	cellsize = ncells * sizeof(*cells);
608cd642c88SSvatopluk Kraus 	ddata = intr_ddata_alloc(sizeof(struct intr_map_data_fdt) + cellsize);
609bff6be3eSSvatopluk Kraus 	if (ddata == NULL)
6108442087fSMichal Meloun 		return (INTR_IRQ_INVALID);	/* no space left */
6112b3ad188SAdrian Chadd 
612bff6be3eSSvatopluk Kraus 	ddata->idd_xref = (intptr_t)node;
613cd642c88SSvatopluk Kraus 	ddata->idd_data->type = INTR_MAP_DATA_FDT;
614cd642c88SSvatopluk Kraus 
615cd642c88SSvatopluk Kraus 	daf = (struct intr_map_data_fdt *)ddata->idd_data;
616cd642c88SSvatopluk Kraus 	daf->ncells = ncells;
617cd642c88SSvatopluk Kraus 	memcpy(daf->cells, cells, cellsize);
618bff6be3eSSvatopluk Kraus 	return (ddata->idd_irq);
6192b3ad188SAdrian Chadd }
6202b3ad188SAdrian Chadd #endif
6212b3ad188SAdrian Chadd 
62239f6c1bdSMichal Meloun /*
62339f6c1bdSMichal Meloun  *  Store GPIO interrupt decription in framework and return unique interrupt
62439f6c1bdSMichal Meloun  *  number (resource handle) associated with it.
62539f6c1bdSMichal Meloun  */
62639f6c1bdSMichal Meloun u_int
62739f6c1bdSMichal Meloun intr_gpio_map_irq(device_t dev, u_int pin_num, u_int pin_flags, u_int intr_mode)
62839f6c1bdSMichal Meloun {
62939f6c1bdSMichal Meloun 	struct intr_dev_data *ddata;
630cd642c88SSvatopluk Kraus 	struct intr_map_data_gpio *dag;
63139f6c1bdSMichal Meloun 
632cd642c88SSvatopluk Kraus 	ddata = intr_ddata_alloc(sizeof(struct intr_map_data_gpio));
63339f6c1bdSMichal Meloun 	if (ddata == NULL)
6348442087fSMichal Meloun 		return (INTR_IRQ_INVALID);	/* no space left */
63539f6c1bdSMichal Meloun 
63639f6c1bdSMichal Meloun 	ddata->idd_dev = dev;
637cd642c88SSvatopluk Kraus 	ddata->idd_data->type = INTR_MAP_DATA_GPIO;
638cd642c88SSvatopluk Kraus 
639cd642c88SSvatopluk Kraus 	dag = (struct intr_map_data_gpio *)ddata->idd_data;
640cd642c88SSvatopluk Kraus 	dag->gpio_pin_num = pin_num;
641cd642c88SSvatopluk Kraus 	dag->gpio_pin_flags = pin_flags;
642cd642c88SSvatopluk Kraus 	dag->gpio_intr_mode = intr_mode;
64339f6c1bdSMichal Meloun 	return (ddata->idd_irq);
64439f6c1bdSMichal Meloun }
64539f6c1bdSMichal Meloun 
6462b3ad188SAdrian Chadd #ifdef INTR_SOLO
6472b3ad188SAdrian Chadd /*
6482b3ad188SAdrian Chadd  *  Setup filter into interrupt source.
6492b3ad188SAdrian Chadd  */
6502b3ad188SAdrian Chadd static int
6512b3ad188SAdrian Chadd iscr_setup_filter(struct intr_irqsrc *isrc, const char *name,
6522b3ad188SAdrian Chadd     intr_irq_filter_t *filter, void *arg, void **cookiep)
6532b3ad188SAdrian Chadd {
6542b3ad188SAdrian Chadd 
6552b3ad188SAdrian Chadd 	if (filter == NULL)
6562b3ad188SAdrian Chadd 		return (EINVAL);
6572b3ad188SAdrian Chadd 
6582b3ad188SAdrian Chadd 	mtx_lock(&isrc_table_lock);
6592b3ad188SAdrian Chadd 	/*
6602b3ad188SAdrian Chadd 	 * Make sure that we do not mix the two ways
6612b3ad188SAdrian Chadd 	 * how we handle interrupt sources.
6622b3ad188SAdrian Chadd 	 */
6632b3ad188SAdrian Chadd 	if (isrc->isrc_filter != NULL || isrc->isrc_event != NULL) {
6642b3ad188SAdrian Chadd 		mtx_unlock(&isrc_table_lock);
6652b3ad188SAdrian Chadd 		return (EBUSY);
6662b3ad188SAdrian Chadd 	}
6672b3ad188SAdrian Chadd 	isrc->isrc_filter = filter;
6682b3ad188SAdrian Chadd 	isrc->isrc_arg = arg;
6692b3ad188SAdrian Chadd 	isrc_update_name(isrc, name);
6702b3ad188SAdrian Chadd 	mtx_unlock(&isrc_table_lock);
6712b3ad188SAdrian Chadd 
6722b3ad188SAdrian Chadd 	*cookiep = isrc;
6732b3ad188SAdrian Chadd 	return (0);
6742b3ad188SAdrian Chadd }
6752b3ad188SAdrian Chadd #endif
6762b3ad188SAdrian Chadd 
6772b3ad188SAdrian Chadd /*
6782b3ad188SAdrian Chadd  *  Interrupt source pre_ithread method for MI interrupt framework.
6792b3ad188SAdrian Chadd  */
6802b3ad188SAdrian Chadd static void
6812b3ad188SAdrian Chadd intr_isrc_pre_ithread(void *arg)
6822b3ad188SAdrian Chadd {
6832b3ad188SAdrian Chadd 	struct intr_irqsrc *isrc = arg;
6842b3ad188SAdrian Chadd 
6852b3ad188SAdrian Chadd 	PIC_PRE_ITHREAD(isrc->isrc_dev, isrc);
6862b3ad188SAdrian Chadd }
6872b3ad188SAdrian Chadd 
6882b3ad188SAdrian Chadd /*
6892b3ad188SAdrian Chadd  *  Interrupt source post_ithread method for MI interrupt framework.
6902b3ad188SAdrian Chadd  */
6912b3ad188SAdrian Chadd static void
6922b3ad188SAdrian Chadd intr_isrc_post_ithread(void *arg)
6932b3ad188SAdrian Chadd {
6942b3ad188SAdrian Chadd 	struct intr_irqsrc *isrc = arg;
6952b3ad188SAdrian Chadd 
6962b3ad188SAdrian Chadd 	PIC_POST_ITHREAD(isrc->isrc_dev, isrc);
6972b3ad188SAdrian Chadd }
6982b3ad188SAdrian Chadd 
6992b3ad188SAdrian Chadd /*
7002b3ad188SAdrian Chadd  *  Interrupt source post_filter method for MI interrupt framework.
7012b3ad188SAdrian Chadd  */
7022b3ad188SAdrian Chadd static void
7032b3ad188SAdrian Chadd intr_isrc_post_filter(void *arg)
7042b3ad188SAdrian Chadd {
7052b3ad188SAdrian Chadd 	struct intr_irqsrc *isrc = arg;
7062b3ad188SAdrian Chadd 
7072b3ad188SAdrian Chadd 	PIC_POST_FILTER(isrc->isrc_dev, isrc);
7082b3ad188SAdrian Chadd }
7092b3ad188SAdrian Chadd 
7102b3ad188SAdrian Chadd /*
7112b3ad188SAdrian Chadd  *  Interrupt source assign_cpu method for MI interrupt framework.
7122b3ad188SAdrian Chadd  */
7132b3ad188SAdrian Chadd static int
7142b3ad188SAdrian Chadd intr_isrc_assign_cpu(void *arg, int cpu)
7152b3ad188SAdrian Chadd {
7162b3ad188SAdrian Chadd #ifdef SMP
7172b3ad188SAdrian Chadd 	struct intr_irqsrc *isrc = arg;
7182b3ad188SAdrian Chadd 	int error;
7192b3ad188SAdrian Chadd 
7205b70c08cSSvatopluk Kraus 	if (isrc->isrc_dev != intr_irq_root_dev)
7212b3ad188SAdrian Chadd 		return (EINVAL);
7222b3ad188SAdrian Chadd 
7232b3ad188SAdrian Chadd 	mtx_lock(&isrc_table_lock);
7242b3ad188SAdrian Chadd 	if (cpu == NOCPU) {
7252b3ad188SAdrian Chadd 		CPU_ZERO(&isrc->isrc_cpu);
7262b3ad188SAdrian Chadd 		isrc->isrc_flags &= ~INTR_ISRCF_BOUND;
7272b3ad188SAdrian Chadd 	} else {
7282b3ad188SAdrian Chadd 		CPU_SETOF(cpu, &isrc->isrc_cpu);
7292b3ad188SAdrian Chadd 		isrc->isrc_flags |= INTR_ISRCF_BOUND;
7302b3ad188SAdrian Chadd 	}
7312b3ad188SAdrian Chadd 
7322b3ad188SAdrian Chadd 	/*
7332b3ad188SAdrian Chadd 	 * In NOCPU case, it's up to PIC to either leave ISRC on same CPU or
7342b3ad188SAdrian Chadd 	 * re-balance it to another CPU or enable it on more CPUs. However,
7352b3ad188SAdrian Chadd 	 * PIC is expected to change isrc_cpu appropriately to keep us well
736e3043798SPedro F. Giffuni 	 * informed if the call is successful.
7372b3ad188SAdrian Chadd 	 */
7382b3ad188SAdrian Chadd 	if (irq_assign_cpu) {
739bff6be3eSSvatopluk Kraus 		error = PIC_BIND_INTR(isrc->isrc_dev, isrc);
7402b3ad188SAdrian Chadd 		if (error) {
7412b3ad188SAdrian Chadd 			CPU_ZERO(&isrc->isrc_cpu);
7422b3ad188SAdrian Chadd 			mtx_unlock(&isrc_table_lock);
7432b3ad188SAdrian Chadd 			return (error);
7442b3ad188SAdrian Chadd 		}
7452b3ad188SAdrian Chadd 	}
7462b3ad188SAdrian Chadd 	mtx_unlock(&isrc_table_lock);
7472b3ad188SAdrian Chadd 	return (0);
7482b3ad188SAdrian Chadd #else
7492b3ad188SAdrian Chadd 	return (EOPNOTSUPP);
7502b3ad188SAdrian Chadd #endif
7512b3ad188SAdrian Chadd }
7522b3ad188SAdrian Chadd 
7532b3ad188SAdrian Chadd /*
7542b3ad188SAdrian Chadd  *  Create interrupt event for interrupt source.
7552b3ad188SAdrian Chadd  */
7562b3ad188SAdrian Chadd static int
7572b3ad188SAdrian Chadd isrc_event_create(struct intr_irqsrc *isrc)
7582b3ad188SAdrian Chadd {
7592b3ad188SAdrian Chadd 	struct intr_event *ie;
7602b3ad188SAdrian Chadd 	int error;
7612b3ad188SAdrian Chadd 
7622b3ad188SAdrian Chadd 	error = intr_event_create(&ie, isrc, 0, isrc->isrc_irq,
7632b3ad188SAdrian Chadd 	    intr_isrc_pre_ithread, intr_isrc_post_ithread, intr_isrc_post_filter,
7642b3ad188SAdrian Chadd 	    intr_isrc_assign_cpu, "%s:", isrc->isrc_name);
7652b3ad188SAdrian Chadd 	if (error)
7662b3ad188SAdrian Chadd 		return (error);
7672b3ad188SAdrian Chadd 
7682b3ad188SAdrian Chadd 	mtx_lock(&isrc_table_lock);
7692b3ad188SAdrian Chadd 	/*
7702b3ad188SAdrian Chadd 	 * Make sure that we do not mix the two ways
7712b3ad188SAdrian Chadd 	 * how we handle interrupt sources. Let contested event wins.
7722b3ad188SAdrian Chadd 	 */
773169e6abdSSvatopluk Kraus #ifdef INTR_SOLO
7742b3ad188SAdrian Chadd 	if (isrc->isrc_filter != NULL || isrc->isrc_event != NULL) {
775169e6abdSSvatopluk Kraus #else
776169e6abdSSvatopluk Kraus 	if (isrc->isrc_event != NULL) {
777169e6abdSSvatopluk Kraus #endif
7782b3ad188SAdrian Chadd 		mtx_unlock(&isrc_table_lock);
7792b3ad188SAdrian Chadd 		intr_event_destroy(ie);
7802b3ad188SAdrian Chadd 		return (isrc->isrc_event != NULL ? EBUSY : 0);
7812b3ad188SAdrian Chadd 	}
7822b3ad188SAdrian Chadd 	isrc->isrc_event = ie;
7832b3ad188SAdrian Chadd 	mtx_unlock(&isrc_table_lock);
7842b3ad188SAdrian Chadd 
7852b3ad188SAdrian Chadd 	return (0);
7862b3ad188SAdrian Chadd }
7872b3ad188SAdrian Chadd #ifdef notyet
7882b3ad188SAdrian Chadd /*
7892b3ad188SAdrian Chadd  *  Destroy interrupt event for interrupt source.
7902b3ad188SAdrian Chadd  */
7912b3ad188SAdrian Chadd static void
7922b3ad188SAdrian Chadd isrc_event_destroy(struct intr_irqsrc *isrc)
7932b3ad188SAdrian Chadd {
7942b3ad188SAdrian Chadd 	struct intr_event *ie;
7952b3ad188SAdrian Chadd 
7962b3ad188SAdrian Chadd 	mtx_lock(&isrc_table_lock);
7972b3ad188SAdrian Chadd 	ie = isrc->isrc_event;
7982b3ad188SAdrian Chadd 	isrc->isrc_event = NULL;
7992b3ad188SAdrian Chadd 	mtx_unlock(&isrc_table_lock);
8002b3ad188SAdrian Chadd 
8012b3ad188SAdrian Chadd 	if (ie != NULL)
8022b3ad188SAdrian Chadd 		intr_event_destroy(ie);
8032b3ad188SAdrian Chadd }
8042b3ad188SAdrian Chadd #endif
8052b3ad188SAdrian Chadd /*
8062b3ad188SAdrian Chadd  *  Add handler to interrupt source.
8072b3ad188SAdrian Chadd  */
8082b3ad188SAdrian Chadd static int
8092b3ad188SAdrian Chadd isrc_add_handler(struct intr_irqsrc *isrc, const char *name,
8102b3ad188SAdrian Chadd     driver_filter_t filter, driver_intr_t handler, void *arg,
8112b3ad188SAdrian Chadd     enum intr_type flags, void **cookiep)
8122b3ad188SAdrian Chadd {
8132b3ad188SAdrian Chadd 	int error;
8142b3ad188SAdrian Chadd 
8152b3ad188SAdrian Chadd 	if (isrc->isrc_event == NULL) {
8162b3ad188SAdrian Chadd 		error = isrc_event_create(isrc);
8172b3ad188SAdrian Chadd 		if (error)
8182b3ad188SAdrian Chadd 			return (error);
8192b3ad188SAdrian Chadd 	}
8202b3ad188SAdrian Chadd 
8212b3ad188SAdrian Chadd 	error = intr_event_add_handler(isrc->isrc_event, name, filter, handler,
8222b3ad188SAdrian Chadd 	    arg, intr_priority(flags), flags, cookiep);
8232b3ad188SAdrian Chadd 	if (error == 0) {
8242b3ad188SAdrian Chadd 		mtx_lock(&isrc_table_lock);
8252b3ad188SAdrian Chadd 		intrcnt_updatename(isrc);
8262b3ad188SAdrian Chadd 		mtx_unlock(&isrc_table_lock);
8272b3ad188SAdrian Chadd 	}
8282b3ad188SAdrian Chadd 
8292b3ad188SAdrian Chadd 	return (error);
8302b3ad188SAdrian Chadd }
8312b3ad188SAdrian Chadd 
8322b3ad188SAdrian Chadd /*
8332b3ad188SAdrian Chadd  *  Lookup interrupt controller locked.
8342b3ad188SAdrian Chadd  */
835bff6be3eSSvatopluk Kraus static inline struct intr_pic *
8362b3ad188SAdrian Chadd pic_lookup_locked(device_t dev, intptr_t xref)
8372b3ad188SAdrian Chadd {
8382b3ad188SAdrian Chadd 	struct intr_pic *pic;
8392b3ad188SAdrian Chadd 
8402b3ad188SAdrian Chadd 	mtx_assert(&pic_list_lock, MA_OWNED);
8412b3ad188SAdrian Chadd 
8424be58cbaSSvatopluk Kraus 	if (dev == NULL && xref == 0)
8434be58cbaSSvatopluk Kraus 		return (NULL);
8444be58cbaSSvatopluk Kraus 
8454be58cbaSSvatopluk Kraus 	/* Note that pic->pic_dev is never NULL on registered PIC. */
8462b3ad188SAdrian Chadd 	SLIST_FOREACH(pic, &pic_list, pic_next) {
8474be58cbaSSvatopluk Kraus 		if (dev == NULL) {
8484be58cbaSSvatopluk Kraus 			if (xref == pic->pic_xref)
8494be58cbaSSvatopluk Kraus 				return (pic);
8504be58cbaSSvatopluk Kraus 		} else if (xref == 0 || pic->pic_xref == 0) {
8514be58cbaSSvatopluk Kraus 			if (dev == pic->pic_dev)
8524be58cbaSSvatopluk Kraus 				return (pic);
8534be58cbaSSvatopluk Kraus 		} else if (xref == pic->pic_xref && dev == pic->pic_dev)
8542b3ad188SAdrian Chadd 				return (pic);
8552b3ad188SAdrian Chadd 	}
8562b3ad188SAdrian Chadd 	return (NULL);
8572b3ad188SAdrian Chadd }
8582b3ad188SAdrian Chadd 
8592b3ad188SAdrian Chadd /*
8602b3ad188SAdrian Chadd  *  Lookup interrupt controller.
8612b3ad188SAdrian Chadd  */
8622b3ad188SAdrian Chadd static struct intr_pic *
8632b3ad188SAdrian Chadd pic_lookup(device_t dev, intptr_t xref)
8642b3ad188SAdrian Chadd {
8652b3ad188SAdrian Chadd 	struct intr_pic *pic;
8662b3ad188SAdrian Chadd 
8672b3ad188SAdrian Chadd 	mtx_lock(&pic_list_lock);
8682b3ad188SAdrian Chadd 	pic = pic_lookup_locked(dev, xref);
8692b3ad188SAdrian Chadd 	mtx_unlock(&pic_list_lock);
8702b3ad188SAdrian Chadd 	return (pic);
8712b3ad188SAdrian Chadd }
8722b3ad188SAdrian Chadd 
8732b3ad188SAdrian Chadd /*
8742b3ad188SAdrian Chadd  *  Create interrupt controller.
8752b3ad188SAdrian Chadd  */
8762b3ad188SAdrian Chadd static struct intr_pic *
8772b3ad188SAdrian Chadd pic_create(device_t dev, intptr_t xref)
8782b3ad188SAdrian Chadd {
8792b3ad188SAdrian Chadd 	struct intr_pic *pic;
8802b3ad188SAdrian Chadd 
8812b3ad188SAdrian Chadd 	mtx_lock(&pic_list_lock);
8822b3ad188SAdrian Chadd 	pic = pic_lookup_locked(dev, xref);
8832b3ad188SAdrian Chadd 	if (pic != NULL) {
8842b3ad188SAdrian Chadd 		mtx_unlock(&pic_list_lock);
8852b3ad188SAdrian Chadd 		return (pic);
8862b3ad188SAdrian Chadd 	}
8872b3ad188SAdrian Chadd 	pic = malloc(sizeof(*pic), M_INTRNG, M_NOWAIT | M_ZERO);
888b48c6083SAndrew Turner 	if (pic == NULL) {
889b48c6083SAndrew Turner 		mtx_unlock(&pic_list_lock);
890b48c6083SAndrew Turner 		return (NULL);
891b48c6083SAndrew Turner 	}
8922b3ad188SAdrian Chadd 	pic->pic_xref = xref;
8932b3ad188SAdrian Chadd 	pic->pic_dev = dev;
8942b3ad188SAdrian Chadd 	SLIST_INSERT_HEAD(&pic_list, pic, pic_next);
8952b3ad188SAdrian Chadd 	mtx_unlock(&pic_list_lock);
8962b3ad188SAdrian Chadd 
8972b3ad188SAdrian Chadd 	return (pic);
8982b3ad188SAdrian Chadd }
8992b3ad188SAdrian Chadd #ifdef notyet
9002b3ad188SAdrian Chadd /*
9012b3ad188SAdrian Chadd  *  Destroy interrupt controller.
9022b3ad188SAdrian Chadd  */
9032b3ad188SAdrian Chadd static void
9042b3ad188SAdrian Chadd pic_destroy(device_t dev, intptr_t xref)
9052b3ad188SAdrian Chadd {
9062b3ad188SAdrian Chadd 	struct intr_pic *pic;
9072b3ad188SAdrian Chadd 
9082b3ad188SAdrian Chadd 	mtx_lock(&pic_list_lock);
9092b3ad188SAdrian Chadd 	pic = pic_lookup_locked(dev, xref);
9102b3ad188SAdrian Chadd 	if (pic == NULL) {
9112b3ad188SAdrian Chadd 		mtx_unlock(&pic_list_lock);
9122b3ad188SAdrian Chadd 		return;
9132b3ad188SAdrian Chadd 	}
9142b3ad188SAdrian Chadd 	SLIST_REMOVE(&pic_list, pic, intr_pic, pic_next);
9152b3ad188SAdrian Chadd 	mtx_unlock(&pic_list_lock);
9162b3ad188SAdrian Chadd 
9172b3ad188SAdrian Chadd 	free(pic, M_INTRNG);
9182b3ad188SAdrian Chadd }
9192b3ad188SAdrian Chadd #endif
9202b3ad188SAdrian Chadd /*
9212b3ad188SAdrian Chadd  *  Register interrupt controller.
9222b3ad188SAdrian Chadd  */
9239346e913SAndrew Turner struct intr_pic *
9242b3ad188SAdrian Chadd intr_pic_register(device_t dev, intptr_t xref)
9252b3ad188SAdrian Chadd {
9262b3ad188SAdrian Chadd 	struct intr_pic *pic;
9272b3ad188SAdrian Chadd 
9284be58cbaSSvatopluk Kraus 	if (dev == NULL)
9299346e913SAndrew Turner 		return (NULL);
9302b3ad188SAdrian Chadd 	pic = pic_create(dev, xref);
9312b3ad188SAdrian Chadd 	if (pic == NULL)
9329346e913SAndrew Turner 		return (NULL);
9332b3ad188SAdrian Chadd 
9343fc155dcSAndrew Turner 	pic->pic_flags |= FLAG_PIC;
9353fc155dcSAndrew Turner 
9364be58cbaSSvatopluk Kraus 	debugf("PIC %p registered for %s <dev %p, xref %x>\n", pic,
9374be58cbaSSvatopluk Kraus 	    device_get_nameunit(dev), dev, xref);
9389346e913SAndrew Turner 	return (pic);
9392b3ad188SAdrian Chadd }
9402b3ad188SAdrian Chadd 
9412b3ad188SAdrian Chadd /*
9422b3ad188SAdrian Chadd  *  Unregister interrupt controller.
9432b3ad188SAdrian Chadd  */
9442b3ad188SAdrian Chadd int
945bff6be3eSSvatopluk Kraus intr_pic_deregister(device_t dev, intptr_t xref)
9462b3ad188SAdrian Chadd {
9472b3ad188SAdrian Chadd 
9482b3ad188SAdrian Chadd 	panic("%s: not implemented", __func__);
9492b3ad188SAdrian Chadd }
9502b3ad188SAdrian Chadd 
9512b3ad188SAdrian Chadd /*
9522b3ad188SAdrian Chadd  *  Mark interrupt controller (itself) as a root one.
9532b3ad188SAdrian Chadd  *
9542b3ad188SAdrian Chadd  *  Note that only an interrupt controller can really know its position
9552b3ad188SAdrian Chadd  *  in interrupt controller's tree. So root PIC must claim itself as a root.
9562b3ad188SAdrian Chadd  *
9572b3ad188SAdrian Chadd  *  In FDT case, according to ePAPR approved version 1.1 from 08 April 2011,
9582b3ad188SAdrian Chadd  *  page 30:
9592b3ad188SAdrian Chadd  *    "The root of the interrupt tree is determined when traversal
9602b3ad188SAdrian Chadd  *     of the interrupt tree reaches an interrupt controller node without
9612b3ad188SAdrian Chadd  *     an interrupts property and thus no explicit interrupt parent."
9622b3ad188SAdrian Chadd  */
9632b3ad188SAdrian Chadd int
9642b3ad188SAdrian Chadd intr_pic_claim_root(device_t dev, intptr_t xref, intr_irq_filter_t *filter,
9652b3ad188SAdrian Chadd     void *arg, u_int ipicount)
9662b3ad188SAdrian Chadd {
9673fc155dcSAndrew Turner 	struct intr_pic *pic;
9682b3ad188SAdrian Chadd 
9693fc155dcSAndrew Turner 	pic = pic_lookup(dev, xref);
9703fc155dcSAndrew Turner 	if (pic == NULL) {
9712b3ad188SAdrian Chadd 		device_printf(dev, "not registered\n");
9722b3ad188SAdrian Chadd 		return (EINVAL);
9732b3ad188SAdrian Chadd 	}
9743fc155dcSAndrew Turner 
9753fc155dcSAndrew Turner 	KASSERT((pic->pic_flags & FLAG_PIC) != 0,
9763fc155dcSAndrew Turner 	    ("%s: Found a non-PIC controller: %s", __func__,
9773fc155dcSAndrew Turner 	     device_get_name(pic->pic_dev)));
9783fc155dcSAndrew Turner 
9792b3ad188SAdrian Chadd 	if (filter == NULL) {
9802b3ad188SAdrian Chadd 		device_printf(dev, "filter missing\n");
9812b3ad188SAdrian Chadd 		return (EINVAL);
9822b3ad188SAdrian Chadd 	}
9832b3ad188SAdrian Chadd 
9842b3ad188SAdrian Chadd 	/*
9852b3ad188SAdrian Chadd 	 * Only one interrupt controllers could be on the root for now.
9862b3ad188SAdrian Chadd 	 * Note that we further suppose that there is not threaded interrupt
9872b3ad188SAdrian Chadd 	 * routine (handler) on the root. See intr_irq_handler().
9882b3ad188SAdrian Chadd 	 */
9895b70c08cSSvatopluk Kraus 	if (intr_irq_root_dev != NULL) {
9902b3ad188SAdrian Chadd 		device_printf(dev, "another root already set\n");
9912b3ad188SAdrian Chadd 		return (EBUSY);
9922b3ad188SAdrian Chadd 	}
9932b3ad188SAdrian Chadd 
9945b70c08cSSvatopluk Kraus 	intr_irq_root_dev = dev;
9952b3ad188SAdrian Chadd 	irq_root_filter = filter;
9962b3ad188SAdrian Chadd 	irq_root_arg = arg;
9972b3ad188SAdrian Chadd 	irq_root_ipicount = ipicount;
9982b3ad188SAdrian Chadd 
9992b3ad188SAdrian Chadd 	debugf("irq root set to %s\n", device_get_nameunit(dev));
10002b3ad188SAdrian Chadd 	return (0);
10012b3ad188SAdrian Chadd }
10022b3ad188SAdrian Chadd 
10032b3ad188SAdrian Chadd int
1004bff6be3eSSvatopluk Kraus intr_map_irq(device_t dev, intptr_t xref, struct intr_map_data *data,
1005bff6be3eSSvatopluk Kraus     u_int *irqp)
10062b3ad188SAdrian Chadd {
10072b3ad188SAdrian Chadd 	int error;
1008bff6be3eSSvatopluk Kraus 	struct intr_irqsrc *isrc;
1009bff6be3eSSvatopluk Kraus 	struct intr_pic *pic;
1010bff6be3eSSvatopluk Kraus 
1011bff6be3eSSvatopluk Kraus 	if (data == NULL)
1012bff6be3eSSvatopluk Kraus 		return (EINVAL);
1013bff6be3eSSvatopluk Kraus 
1014bff6be3eSSvatopluk Kraus 	pic = pic_lookup(dev, xref);
101515adccc6SSvatopluk Kraus 	if (pic == NULL)
1016bff6be3eSSvatopluk Kraus 		return (ESRCH);
1017bff6be3eSSvatopluk Kraus 
10183fc155dcSAndrew Turner 	KASSERT((pic->pic_flags & FLAG_PIC) != 0,
10193fc155dcSAndrew Turner 	    ("%s: Found a non-PIC controller: %s", __func__,
10203fc155dcSAndrew Turner 	     device_get_name(pic->pic_dev)));
10213fc155dcSAndrew Turner 
1022bff6be3eSSvatopluk Kraus 	error = PIC_MAP_INTR(pic->pic_dev, data, &isrc);
1023bff6be3eSSvatopluk Kraus 	if (error == 0)
1024bff6be3eSSvatopluk Kraus 		*irqp = isrc->isrc_irq;
1025bff6be3eSSvatopluk Kraus 	return (error);
1026bff6be3eSSvatopluk Kraus }
1027bff6be3eSSvatopluk Kraus 
1028bff6be3eSSvatopluk Kraus int
1029bff6be3eSSvatopluk Kraus intr_alloc_irq(device_t dev, struct resource *res)
1030bff6be3eSSvatopluk Kraus {
1031bff6be3eSSvatopluk Kraus 	struct intr_map_data *data;
1032bff6be3eSSvatopluk Kraus 	struct intr_irqsrc *isrc;
1033bff6be3eSSvatopluk Kraus 
1034bff6be3eSSvatopluk Kraus 	KASSERT(rman_get_start(res) == rman_get_end(res),
1035bff6be3eSSvatopluk Kraus 	    ("%s: more interrupts in resource", __func__));
1036bff6be3eSSvatopluk Kraus 
1037bff6be3eSSvatopluk Kraus 	isrc = intr_ddata_lookup(rman_get_start(res), &data);
1038bff6be3eSSvatopluk Kraus 	if (isrc == NULL)
1039bff6be3eSSvatopluk Kraus 		return (EINVAL);
1040bff6be3eSSvatopluk Kraus 
1041bff6be3eSSvatopluk Kraus 	return (PIC_ALLOC_INTR(isrc->isrc_dev, isrc, res, data));
1042bff6be3eSSvatopluk Kraus }
1043bff6be3eSSvatopluk Kraus 
1044bff6be3eSSvatopluk Kraus int
1045bff6be3eSSvatopluk Kraus intr_release_irq(device_t dev, struct resource *res)
1046bff6be3eSSvatopluk Kraus {
1047bff6be3eSSvatopluk Kraus 	struct intr_map_data *data;
1048bff6be3eSSvatopluk Kraus 	struct intr_irqsrc *isrc;
1049bff6be3eSSvatopluk Kraus 
1050bff6be3eSSvatopluk Kraus 	KASSERT(rman_get_start(res) == rman_get_end(res),
1051bff6be3eSSvatopluk Kraus 	    ("%s: more interrupts in resource", __func__));
1052bff6be3eSSvatopluk Kraus 
1053bff6be3eSSvatopluk Kraus 	isrc = intr_ddata_lookup(rman_get_start(res), &data);
1054bff6be3eSSvatopluk Kraus 	if (isrc == NULL)
1055bff6be3eSSvatopluk Kraus 		return (EINVAL);
1056bff6be3eSSvatopluk Kraus 
1057bff6be3eSSvatopluk Kraus 	return (PIC_RELEASE_INTR(isrc->isrc_dev, isrc, res, data));
1058bff6be3eSSvatopluk Kraus }
1059bff6be3eSSvatopluk Kraus 
1060bff6be3eSSvatopluk Kraus int
1061bff6be3eSSvatopluk Kraus intr_setup_irq(device_t dev, struct resource *res, driver_filter_t filt,
1062bff6be3eSSvatopluk Kraus     driver_intr_t hand, void *arg, int flags, void **cookiep)
1063bff6be3eSSvatopluk Kraus {
1064bff6be3eSSvatopluk Kraus 	int error;
1065bff6be3eSSvatopluk Kraus 	struct intr_map_data *data;
1066bff6be3eSSvatopluk Kraus 	struct intr_irqsrc *isrc;
1067bff6be3eSSvatopluk Kraus 	const char *name;
1068bff6be3eSSvatopluk Kraus 
1069bff6be3eSSvatopluk Kraus 	KASSERT(rman_get_start(res) == rman_get_end(res),
1070bff6be3eSSvatopluk Kraus 	    ("%s: more interrupts in resource", __func__));
1071bff6be3eSSvatopluk Kraus 
1072bff6be3eSSvatopluk Kraus 	isrc = intr_ddata_lookup(rman_get_start(res), &data);
1073bff6be3eSSvatopluk Kraus 	if (isrc == NULL)
1074bff6be3eSSvatopluk Kraus 		return (EINVAL);
10752b3ad188SAdrian Chadd 
10762b3ad188SAdrian Chadd 	name = device_get_nameunit(dev);
10772b3ad188SAdrian Chadd 
10782b3ad188SAdrian Chadd #ifdef INTR_SOLO
10792b3ad188SAdrian Chadd 	/*
1080e3043798SPedro F. Giffuni 	 * Standard handling is done through MI interrupt framework. However,
10812b3ad188SAdrian Chadd 	 * some interrupts could request solely own special handling. This
10822b3ad188SAdrian Chadd 	 * non standard handling can be used for interrupt controllers without
10832b3ad188SAdrian Chadd 	 * handler (filter only), so in case that interrupt controllers are
10842b3ad188SAdrian Chadd 	 * chained, MI interrupt framework is called only in leaf controller.
10852b3ad188SAdrian Chadd 	 *
10862b3ad188SAdrian Chadd 	 * Note that root interrupt controller routine is served as well,
10872b3ad188SAdrian Chadd 	 * however in intr_irq_handler(), i.e. main system dispatch routine.
10882b3ad188SAdrian Chadd 	 */
10892b3ad188SAdrian Chadd 	if (flags & INTR_SOLO && hand != NULL) {
10902b3ad188SAdrian Chadd 		debugf("irq %u cannot solo on %s\n", irq, name);
10912b3ad188SAdrian Chadd 		return (EINVAL);
10922b3ad188SAdrian Chadd 	}
10932b3ad188SAdrian Chadd 
10942b3ad188SAdrian Chadd 	if (flags & INTR_SOLO) {
10952b3ad188SAdrian Chadd 		error = iscr_setup_filter(isrc, name, (intr_irq_filter_t *)filt,
10962b3ad188SAdrian Chadd 		    arg, cookiep);
10972b3ad188SAdrian Chadd 		debugf("irq %u setup filter error %d on %s\n", irq, error,
10982b3ad188SAdrian Chadd 		    name);
10992b3ad188SAdrian Chadd 	} else
11002b3ad188SAdrian Chadd #endif
11012b3ad188SAdrian Chadd 		{
11022b3ad188SAdrian Chadd 		error = isrc_add_handler(isrc, name, filt, hand, arg, flags,
11032b3ad188SAdrian Chadd 		    cookiep);
11042b3ad188SAdrian Chadd 		debugf("irq %u add handler error %d on %s\n", irq, error, name);
11052b3ad188SAdrian Chadd 	}
11062b3ad188SAdrian Chadd 	if (error != 0)
11072b3ad188SAdrian Chadd 		return (error);
11082b3ad188SAdrian Chadd 
11092b3ad188SAdrian Chadd 	mtx_lock(&isrc_table_lock);
1110bff6be3eSSvatopluk Kraus 	error = PIC_SETUP_INTR(isrc->isrc_dev, isrc, res, data);
1111bff6be3eSSvatopluk Kraus 	if (error == 0) {
11122b3ad188SAdrian Chadd 		isrc->isrc_handlers++;
1113bff6be3eSSvatopluk Kraus 		if (isrc->isrc_handlers == 1)
11142b3ad188SAdrian Chadd 			PIC_ENABLE_INTR(isrc->isrc_dev, isrc);
11152b3ad188SAdrian Chadd 	}
11162b3ad188SAdrian Chadd 	mtx_unlock(&isrc_table_lock);
1117bff6be3eSSvatopluk Kraus 	if (error != 0)
1118bff6be3eSSvatopluk Kraus 		intr_event_remove_handler(*cookiep);
1119bff6be3eSSvatopluk Kraus 	return (error);
11202b3ad188SAdrian Chadd }
11212b3ad188SAdrian Chadd 
11222b3ad188SAdrian Chadd int
1123bff6be3eSSvatopluk Kraus intr_teardown_irq(device_t dev, struct resource *res, void *cookie)
11242b3ad188SAdrian Chadd {
11252b3ad188SAdrian Chadd 	int error;
1126bff6be3eSSvatopluk Kraus 	struct intr_map_data *data;
1127bff6be3eSSvatopluk Kraus 	struct intr_irqsrc *isrc;
11282b3ad188SAdrian Chadd 
1129bff6be3eSSvatopluk Kraus 	KASSERT(rman_get_start(res) == rman_get_end(res),
1130bff6be3eSSvatopluk Kraus 	    ("%s: more interrupts in resource", __func__));
1131bff6be3eSSvatopluk Kraus 
1132bff6be3eSSvatopluk Kraus 	isrc = intr_ddata_lookup(rman_get_start(res), &data);
11332b3ad188SAdrian Chadd 	if (isrc == NULL || isrc->isrc_handlers == 0)
11342b3ad188SAdrian Chadd 		return (EINVAL);
1135bff6be3eSSvatopluk Kraus 
1136169e6abdSSvatopluk Kraus #ifdef INTR_SOLO
11372b3ad188SAdrian Chadd 	if (isrc->isrc_filter != NULL) {
11382b3ad188SAdrian Chadd 		if (isrc != cookie)
11392b3ad188SAdrian Chadd 			return (EINVAL);
11402b3ad188SAdrian Chadd 
11412b3ad188SAdrian Chadd 		mtx_lock(&isrc_table_lock);
11422b3ad188SAdrian Chadd 		isrc->isrc_filter = NULL;
11432b3ad188SAdrian Chadd 		isrc->isrc_arg = NULL;
11442b3ad188SAdrian Chadd 		isrc->isrc_handlers = 0;
11452b3ad188SAdrian Chadd 		PIC_DISABLE_INTR(isrc->isrc_dev, isrc);
1146bff6be3eSSvatopluk Kraus 		PIC_TEARDOWN_INTR(isrc->isrc_dev, isrc, res, data);
11472b3ad188SAdrian Chadd 		isrc_update_name(isrc, NULL);
11482b3ad188SAdrian Chadd 		mtx_unlock(&isrc_table_lock);
11492b3ad188SAdrian Chadd 		return (0);
11502b3ad188SAdrian Chadd 	}
1151169e6abdSSvatopluk Kraus #endif
11522b3ad188SAdrian Chadd 	if (isrc != intr_handler_source(cookie))
11532b3ad188SAdrian Chadd 		return (EINVAL);
11542b3ad188SAdrian Chadd 
11552b3ad188SAdrian Chadd 	error = intr_event_remove_handler(cookie);
11562b3ad188SAdrian Chadd 	if (error == 0) {
11572b3ad188SAdrian Chadd 		mtx_lock(&isrc_table_lock);
11582b3ad188SAdrian Chadd 		isrc->isrc_handlers--;
1159bff6be3eSSvatopluk Kraus 		if (isrc->isrc_handlers == 0)
11602b3ad188SAdrian Chadd 			PIC_DISABLE_INTR(isrc->isrc_dev, isrc);
1161bff6be3eSSvatopluk Kraus 		PIC_TEARDOWN_INTR(isrc->isrc_dev, isrc, res, data);
11622b3ad188SAdrian Chadd 		intrcnt_updatename(isrc);
11632b3ad188SAdrian Chadd 		mtx_unlock(&isrc_table_lock);
11642b3ad188SAdrian Chadd 	}
11652b3ad188SAdrian Chadd 	return (error);
11662b3ad188SAdrian Chadd }
11672b3ad188SAdrian Chadd 
11682b3ad188SAdrian Chadd int
1169bff6be3eSSvatopluk Kraus intr_describe_irq(device_t dev, struct resource *res, void *cookie,
1170bff6be3eSSvatopluk Kraus     const char *descr)
11712b3ad188SAdrian Chadd {
11722b3ad188SAdrian Chadd 	int error;
1173bff6be3eSSvatopluk Kraus 	struct intr_irqsrc *isrc;
11742b3ad188SAdrian Chadd 
1175bff6be3eSSvatopluk Kraus 	KASSERT(rman_get_start(res) == rman_get_end(res),
1176bff6be3eSSvatopluk Kraus 	    ("%s: more interrupts in resource", __func__));
1177bff6be3eSSvatopluk Kraus 
1178bff6be3eSSvatopluk Kraus 	isrc = intr_ddata_lookup(rman_get_start(res), NULL);
11792b3ad188SAdrian Chadd 	if (isrc == NULL || isrc->isrc_handlers == 0)
11802b3ad188SAdrian Chadd 		return (EINVAL);
1181169e6abdSSvatopluk Kraus #ifdef INTR_SOLO
11822b3ad188SAdrian Chadd 	if (isrc->isrc_filter != NULL) {
11832b3ad188SAdrian Chadd 		if (isrc != cookie)
11842b3ad188SAdrian Chadd 			return (EINVAL);
11852b3ad188SAdrian Chadd 
11862b3ad188SAdrian Chadd 		mtx_lock(&isrc_table_lock);
11872b3ad188SAdrian Chadd 		isrc_update_name(isrc, descr);
11882b3ad188SAdrian Chadd 		mtx_unlock(&isrc_table_lock);
11892b3ad188SAdrian Chadd 		return (0);
11902b3ad188SAdrian Chadd 	}
1191169e6abdSSvatopluk Kraus #endif
11922b3ad188SAdrian Chadd 	error = intr_event_describe_handler(isrc->isrc_event, cookie, descr);
11932b3ad188SAdrian Chadd 	if (error == 0) {
11942b3ad188SAdrian Chadd 		mtx_lock(&isrc_table_lock);
11952b3ad188SAdrian Chadd 		intrcnt_updatename(isrc);
11962b3ad188SAdrian Chadd 		mtx_unlock(&isrc_table_lock);
11972b3ad188SAdrian Chadd 	}
11982b3ad188SAdrian Chadd 	return (error);
11992b3ad188SAdrian Chadd }
12002b3ad188SAdrian Chadd 
12012b3ad188SAdrian Chadd #ifdef SMP
12022b3ad188SAdrian Chadd int
1203bff6be3eSSvatopluk Kraus intr_bind_irq(device_t dev, struct resource *res, int cpu)
12042b3ad188SAdrian Chadd {
12052b3ad188SAdrian Chadd 	struct intr_irqsrc *isrc;
12062b3ad188SAdrian Chadd 
1207bff6be3eSSvatopluk Kraus 	KASSERT(rman_get_start(res) == rman_get_end(res),
1208bff6be3eSSvatopluk Kraus 	    ("%s: more interrupts in resource", __func__));
1209bff6be3eSSvatopluk Kraus 
1210bff6be3eSSvatopluk Kraus 	isrc = intr_ddata_lookup(rman_get_start(res), NULL);
12112b3ad188SAdrian Chadd 	if (isrc == NULL || isrc->isrc_handlers == 0)
12122b3ad188SAdrian Chadd 		return (EINVAL);
1213169e6abdSSvatopluk Kraus #ifdef INTR_SOLO
12142b3ad188SAdrian Chadd 	if (isrc->isrc_filter != NULL)
12152b3ad188SAdrian Chadd 		return (intr_isrc_assign_cpu(isrc, cpu));
1216169e6abdSSvatopluk Kraus #endif
12172b3ad188SAdrian Chadd 	return (intr_event_bind(isrc->isrc_event, cpu));
12182b3ad188SAdrian Chadd }
12192b3ad188SAdrian Chadd 
12202b3ad188SAdrian Chadd /*
12212b3ad188SAdrian Chadd  * Return the CPU that the next interrupt source should use.
12222b3ad188SAdrian Chadd  * For now just returns the next CPU according to round-robin.
12232b3ad188SAdrian Chadd  */
12242b3ad188SAdrian Chadd u_int
12252b3ad188SAdrian Chadd intr_irq_next_cpu(u_int last_cpu, cpuset_t *cpumask)
12262b3ad188SAdrian Chadd {
12272b3ad188SAdrian Chadd 
12282b3ad188SAdrian Chadd 	if (!irq_assign_cpu || mp_ncpus == 1)
12292b3ad188SAdrian Chadd 		return (PCPU_GET(cpuid));
12302b3ad188SAdrian Chadd 
12312b3ad188SAdrian Chadd 	do {
12322b3ad188SAdrian Chadd 		last_cpu++;
12332b3ad188SAdrian Chadd 		if (last_cpu > mp_maxid)
12342b3ad188SAdrian Chadd 			last_cpu = 0;
12352b3ad188SAdrian Chadd 	} while (!CPU_ISSET(last_cpu, cpumask));
12362b3ad188SAdrian Chadd 	return (last_cpu);
12372b3ad188SAdrian Chadd }
12382b3ad188SAdrian Chadd 
12392b3ad188SAdrian Chadd /*
12402b3ad188SAdrian Chadd  *  Distribute all the interrupt sources among the available
12412b3ad188SAdrian Chadd  *  CPUs once the AP's have been launched.
12422b3ad188SAdrian Chadd  */
12432b3ad188SAdrian Chadd static void
12442b3ad188SAdrian Chadd intr_irq_shuffle(void *arg __unused)
12452b3ad188SAdrian Chadd {
12462b3ad188SAdrian Chadd 	struct intr_irqsrc *isrc;
12472b3ad188SAdrian Chadd 	u_int i;
12482b3ad188SAdrian Chadd 
12492b3ad188SAdrian Chadd 	if (mp_ncpus == 1)
12502b3ad188SAdrian Chadd 		return;
12512b3ad188SAdrian Chadd 
12522b3ad188SAdrian Chadd 	mtx_lock(&isrc_table_lock);
12532b3ad188SAdrian Chadd 	irq_assign_cpu = TRUE;
12542b3ad188SAdrian Chadd 	for (i = 0; i < NIRQ; i++) {
12552b3ad188SAdrian Chadd 		isrc = irq_sources[i];
12562b3ad188SAdrian Chadd 		if (isrc == NULL || isrc->isrc_handlers == 0 ||
1257cf55df9fSSvatopluk Kraus 		    isrc->isrc_flags & (INTR_ISRCF_PPI | INTR_ISRCF_IPI))
12582b3ad188SAdrian Chadd 			continue;
12592b3ad188SAdrian Chadd 
12602b3ad188SAdrian Chadd 		if (isrc->isrc_event != NULL &&
12612b3ad188SAdrian Chadd 		    isrc->isrc_flags & INTR_ISRCF_BOUND &&
12622b3ad188SAdrian Chadd 		    isrc->isrc_event->ie_cpu != CPU_FFS(&isrc->isrc_cpu) - 1)
12632b3ad188SAdrian Chadd 			panic("%s: CPU inconsistency", __func__);
12642b3ad188SAdrian Chadd 
12652b3ad188SAdrian Chadd 		if ((isrc->isrc_flags & INTR_ISRCF_BOUND) == 0)
12662b3ad188SAdrian Chadd 			CPU_ZERO(&isrc->isrc_cpu); /* start again */
12672b3ad188SAdrian Chadd 
12682b3ad188SAdrian Chadd 		/*
12692b3ad188SAdrian Chadd 		 * We are in wicked position here if the following call fails
12702b3ad188SAdrian Chadd 		 * for bound ISRC. The best thing we can do is to clear
12712b3ad188SAdrian Chadd 		 * isrc_cpu so inconsistency with ie_cpu will be detectable.
12722b3ad188SAdrian Chadd 		 */
1273bff6be3eSSvatopluk Kraus 		if (PIC_BIND_INTR(isrc->isrc_dev, isrc) != 0)
12742b3ad188SAdrian Chadd 			CPU_ZERO(&isrc->isrc_cpu);
12752b3ad188SAdrian Chadd 	}
12762b3ad188SAdrian Chadd 	mtx_unlock(&isrc_table_lock);
12772b3ad188SAdrian Chadd }
12782b3ad188SAdrian Chadd SYSINIT(intr_irq_shuffle, SI_SUB_SMP, SI_ORDER_SECOND, intr_irq_shuffle, NULL);
12792b3ad188SAdrian Chadd 
12802b3ad188SAdrian Chadd #else
12812b3ad188SAdrian Chadd u_int
12822b3ad188SAdrian Chadd intr_irq_next_cpu(u_int current_cpu, cpuset_t *cpumask)
12832b3ad188SAdrian Chadd {
12842b3ad188SAdrian Chadd 
12852b3ad188SAdrian Chadd 	return (PCPU_GET(cpuid));
12862b3ad188SAdrian Chadd }
12872b3ad188SAdrian Chadd #endif
12882b3ad188SAdrian Chadd 
12893fc155dcSAndrew Turner /*
12903fc155dcSAndrew Turner  *  Register a MSI/MSI-X interrupt controller
12913fc155dcSAndrew Turner  */
12923fc155dcSAndrew Turner int
12933fc155dcSAndrew Turner intr_msi_register(device_t dev, intptr_t xref)
12943fc155dcSAndrew Turner {
12953fc155dcSAndrew Turner 	struct intr_pic *pic;
12963fc155dcSAndrew Turner 
12973fc155dcSAndrew Turner 	if (dev == NULL)
12983fc155dcSAndrew Turner 		return (EINVAL);
12993fc155dcSAndrew Turner 	pic = pic_create(dev, xref);
13003fc155dcSAndrew Turner 	if (pic == NULL)
13013fc155dcSAndrew Turner 		return (ENOMEM);
13023fc155dcSAndrew Turner 
13033fc155dcSAndrew Turner 	pic->pic_flags |= FLAG_MSI;
13043fc155dcSAndrew Turner 
13053fc155dcSAndrew Turner 	debugf("PIC %p registered for %s <dev %p, xref %jx>\n", pic,
13063fc155dcSAndrew Turner 	    device_get_nameunit(dev), dev, (uintmax_t)xref);
13073fc155dcSAndrew Turner 	return (0);
13083fc155dcSAndrew Turner }
13093fc155dcSAndrew Turner 
13103fc155dcSAndrew Turner int
13113fc155dcSAndrew Turner intr_alloc_msi(device_t pci, device_t child, intptr_t xref, int count,
13123fc155dcSAndrew Turner     int maxcount, int *irqs)
13133fc155dcSAndrew Turner {
13143fc155dcSAndrew Turner 	struct intr_irqsrc **isrc;
13153fc155dcSAndrew Turner 	struct intr_pic *pic;
13163fc155dcSAndrew Turner 	device_t pdev;
13173fc155dcSAndrew Turner 	int err, i;
13183fc155dcSAndrew Turner 
13193fc155dcSAndrew Turner 	pic = pic_lookup(NULL, xref);
13203fc155dcSAndrew Turner 	if (pic == NULL)
13213fc155dcSAndrew Turner 		return (ESRCH);
13223fc155dcSAndrew Turner 
13233fc155dcSAndrew Turner 	KASSERT((pic->pic_flags & FLAG_MSI) != 0,
13243fc155dcSAndrew Turner 	    ("%s: Found a non-MSI controller: %s", __func__,
13253fc155dcSAndrew Turner 	     device_get_name(pic->pic_dev)));
13263fc155dcSAndrew Turner 
13273fc155dcSAndrew Turner 	isrc = malloc(sizeof(*isrc) * count, M_INTRNG, M_WAITOK);
13283fc155dcSAndrew Turner 	err = MSI_ALLOC_MSI(pic->pic_dev, child, count, maxcount, &pdev, isrc);
13293fc155dcSAndrew Turner 	if (err == 0) {
13303fc155dcSAndrew Turner 		for (i = 0; i < count; i++) {
13313fc155dcSAndrew Turner 			irqs[i] = isrc[i]->isrc_irq;
13323fc155dcSAndrew Turner 		}
13333fc155dcSAndrew Turner 	}
13343fc155dcSAndrew Turner 
13353fc155dcSAndrew Turner 	free(isrc, M_INTRNG);
13363fc155dcSAndrew Turner 
13373fc155dcSAndrew Turner 	return (err);
13383fc155dcSAndrew Turner }
13393fc155dcSAndrew Turner 
13403fc155dcSAndrew Turner int
13413fc155dcSAndrew Turner intr_release_msi(device_t pci, device_t child, intptr_t xref, int count,
13423fc155dcSAndrew Turner     int *irqs)
13433fc155dcSAndrew Turner {
13443fc155dcSAndrew Turner 	struct intr_irqsrc **isrc;
13453fc155dcSAndrew Turner 	struct intr_pic *pic;
13463fc155dcSAndrew Turner 	int i, err;
13473fc155dcSAndrew Turner 
13483fc155dcSAndrew Turner 	pic = pic_lookup(NULL, xref);
13493fc155dcSAndrew Turner 	if (pic == NULL)
13503fc155dcSAndrew Turner 		return (ESRCH);
13513fc155dcSAndrew Turner 
13523fc155dcSAndrew Turner 	KASSERT((pic->pic_flags & FLAG_MSI) != 0,
13533fc155dcSAndrew Turner 	    ("%s: Found a non-MSI controller: %s", __func__,
13543fc155dcSAndrew Turner 	     device_get_name(pic->pic_dev)));
13553fc155dcSAndrew Turner 
13563fc155dcSAndrew Turner 	isrc = malloc(sizeof(*isrc) * count, M_INTRNG, M_WAITOK);
13573fc155dcSAndrew Turner 
13583fc155dcSAndrew Turner 	for (i = 0; i < count; i++) {
13593fc155dcSAndrew Turner 		isrc[i] = isrc_lookup(irqs[i]);
13603fc155dcSAndrew Turner 		if (isrc == NULL) {
13613fc155dcSAndrew Turner 			free(isrc, M_INTRNG);
13623fc155dcSAndrew Turner 			return (EINVAL);
13633fc155dcSAndrew Turner 		}
13643fc155dcSAndrew Turner 	}
13653fc155dcSAndrew Turner 
13663fc155dcSAndrew Turner 	err = MSI_RELEASE_MSI(pic->pic_dev, child, count, isrc);
13673fc155dcSAndrew Turner 	free(isrc, M_INTRNG);
13683fc155dcSAndrew Turner 	return (err);
13693fc155dcSAndrew Turner }
13703fc155dcSAndrew Turner 
13713fc155dcSAndrew Turner int
13723fc155dcSAndrew Turner intr_alloc_msix(device_t pci, device_t child, intptr_t xref, int *irq)
13733fc155dcSAndrew Turner {
13743fc155dcSAndrew Turner 	struct intr_irqsrc *isrc;
13753fc155dcSAndrew Turner 	struct intr_pic *pic;
13763fc155dcSAndrew Turner 	device_t pdev;
13773fc155dcSAndrew Turner 	int err;
13783fc155dcSAndrew Turner 
13793fc155dcSAndrew Turner 	pic = pic_lookup(NULL, xref);
13803fc155dcSAndrew Turner 	if (pic == NULL)
13813fc155dcSAndrew Turner 		return (ESRCH);
13823fc155dcSAndrew Turner 
13833fc155dcSAndrew Turner 	KASSERT((pic->pic_flags & FLAG_MSI) != 0,
13843fc155dcSAndrew Turner 	    ("%s: Found a non-MSI controller: %s", __func__,
13853fc155dcSAndrew Turner 	     device_get_name(pic->pic_dev)));
13863fc155dcSAndrew Turner 
13873fc155dcSAndrew Turner 	err = MSI_ALLOC_MSIX(pic->pic_dev, child, &pdev, &isrc);
13883fc155dcSAndrew Turner 	if (err != 0)
13893fc155dcSAndrew Turner 		return (err);
13903fc155dcSAndrew Turner 
13913fc155dcSAndrew Turner 	*irq = isrc->isrc_irq;
13923fc155dcSAndrew Turner 	return (0);
13933fc155dcSAndrew Turner }
13943fc155dcSAndrew Turner 
13953fc155dcSAndrew Turner int
13963fc155dcSAndrew Turner intr_release_msix(device_t pci, device_t child, intptr_t xref, int irq)
13973fc155dcSAndrew Turner {
13983fc155dcSAndrew Turner 	struct intr_irqsrc *isrc;
13993fc155dcSAndrew Turner 	struct intr_pic *pic;
14003fc155dcSAndrew Turner 	int err;
14013fc155dcSAndrew Turner 
14023fc155dcSAndrew Turner 	pic = pic_lookup(NULL, xref);
14033fc155dcSAndrew Turner 	if (pic == NULL)
14043fc155dcSAndrew Turner 		return (ESRCH);
14053fc155dcSAndrew Turner 
14063fc155dcSAndrew Turner 	KASSERT((pic->pic_flags & FLAG_MSI) != 0,
14073fc155dcSAndrew Turner 	    ("%s: Found a non-MSI controller: %s", __func__,
14083fc155dcSAndrew Turner 	     device_get_name(pic->pic_dev)));
14093fc155dcSAndrew Turner 
14103fc155dcSAndrew Turner 	isrc = isrc_lookup(irq);
14113fc155dcSAndrew Turner 	if (isrc == NULL)
14123fc155dcSAndrew Turner 		return (EINVAL);
14133fc155dcSAndrew Turner 
14143fc155dcSAndrew Turner 	err = MSI_RELEASE_MSIX(pic->pic_dev, child, isrc);
14153fc155dcSAndrew Turner 	return (err);
14163fc155dcSAndrew Turner }
14173fc155dcSAndrew Turner 
14183fc155dcSAndrew Turner int
14193fc155dcSAndrew Turner intr_map_msi(device_t pci, device_t child, intptr_t xref, int irq,
14203fc155dcSAndrew Turner     uint64_t *addr, uint32_t *data)
14213fc155dcSAndrew Turner {
14223fc155dcSAndrew Turner 	struct intr_irqsrc *isrc;
14233fc155dcSAndrew Turner 	struct intr_pic *pic;
14243fc155dcSAndrew Turner 	int err;
14253fc155dcSAndrew Turner 
14263fc155dcSAndrew Turner 	pic = pic_lookup(NULL, xref);
14273fc155dcSAndrew Turner 	if (pic == NULL)
14283fc155dcSAndrew Turner 		return (ESRCH);
14293fc155dcSAndrew Turner 
14303fc155dcSAndrew Turner 	KASSERT((pic->pic_flags & FLAG_MSI) != 0,
14313fc155dcSAndrew Turner 	    ("%s: Found a non-MSI controller: %s", __func__,
14323fc155dcSAndrew Turner 	     device_get_name(pic->pic_dev)));
14333fc155dcSAndrew Turner 
14343fc155dcSAndrew Turner 	isrc = isrc_lookup(irq);
14353fc155dcSAndrew Turner 	if (isrc == NULL)
14363fc155dcSAndrew Turner 		return (EINVAL);
14373fc155dcSAndrew Turner 
14383fc155dcSAndrew Turner 	err = MSI_MAP_MSI(pic->pic_dev, child, isrc, addr, data);
14393fc155dcSAndrew Turner 	return (err);
14403fc155dcSAndrew Turner }
14413fc155dcSAndrew Turner 
14423fc155dcSAndrew Turner 
14432b3ad188SAdrian Chadd void dosoftints(void);
14442b3ad188SAdrian Chadd void
14452b3ad188SAdrian Chadd dosoftints(void)
14462b3ad188SAdrian Chadd {
14472b3ad188SAdrian Chadd }
14482b3ad188SAdrian Chadd 
14492b3ad188SAdrian Chadd #ifdef SMP
14502b3ad188SAdrian Chadd /*
14512b3ad188SAdrian Chadd  *  Init interrupt controller on another CPU.
14522b3ad188SAdrian Chadd  */
14532b3ad188SAdrian Chadd void
14542b3ad188SAdrian Chadd intr_pic_init_secondary(void)
14552b3ad188SAdrian Chadd {
14562b3ad188SAdrian Chadd 
14572b3ad188SAdrian Chadd 	/*
14582b3ad188SAdrian Chadd 	 * QQQ: Only root PIC is aware of other CPUs ???
14592b3ad188SAdrian Chadd 	 */
14605b70c08cSSvatopluk Kraus 	KASSERT(intr_irq_root_dev != NULL, ("%s: no root attached", __func__));
14612b3ad188SAdrian Chadd 
14622b3ad188SAdrian Chadd 	//mtx_lock(&isrc_table_lock);
14635b70c08cSSvatopluk Kraus 	PIC_INIT_SECONDARY(intr_irq_root_dev);
14642b3ad188SAdrian Chadd 	//mtx_unlock(&isrc_table_lock);
14652b3ad188SAdrian Chadd }
14662b3ad188SAdrian Chadd #endif
14672b3ad188SAdrian Chadd 
14682b3ad188SAdrian Chadd #ifdef DDB
14692b3ad188SAdrian Chadd DB_SHOW_COMMAND(irqs, db_show_irqs)
14702b3ad188SAdrian Chadd {
14712b3ad188SAdrian Chadd 	u_int i, irqsum;
1472bff6be3eSSvatopluk Kraus 	u_long num;
14732b3ad188SAdrian Chadd 	struct intr_irqsrc *isrc;
14742b3ad188SAdrian Chadd 
14752b3ad188SAdrian Chadd 	for (irqsum = 0, i = 0; i < NIRQ; i++) {
14762b3ad188SAdrian Chadd 		isrc = irq_sources[i];
14772b3ad188SAdrian Chadd 		if (isrc == NULL)
14782b3ad188SAdrian Chadd 			continue;
14792b3ad188SAdrian Chadd 
1480bff6be3eSSvatopluk Kraus 		num = isrc->isrc_count != NULL ? isrc->isrc_count[0] : 0;
14812b3ad188SAdrian Chadd 		db_printf("irq%-3u <%s>: cpu %02lx%s cnt %lu\n", i,
14822b3ad188SAdrian Chadd 		    isrc->isrc_name, isrc->isrc_cpu.__bits[0],
1483bff6be3eSSvatopluk Kraus 		    isrc->isrc_flags & INTR_ISRCF_BOUND ? " (bound)" : "", num);
1484bff6be3eSSvatopluk Kraus 		irqsum += num;
14852b3ad188SAdrian Chadd 	}
14862b3ad188SAdrian Chadd 	db_printf("irq total %u\n", irqsum);
14872b3ad188SAdrian Chadd }
14882b3ad188SAdrian Chadd #endif
1489