xref: /freebsd/sys/kern/subr_intr.c (revision 9346e9130d7ae72b1a6fadb36de85c835074b883)
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"
402b3ad188SAdrian Chadd #include "opt_platform.h"
412b3ad188SAdrian Chadd 
422b3ad188SAdrian Chadd #include <sys/param.h>
432b3ad188SAdrian Chadd #include <sys/systm.h>
442b3ad188SAdrian Chadd #include <sys/kernel.h>
452b3ad188SAdrian Chadd #include <sys/syslog.h>
462b3ad188SAdrian Chadd #include <sys/malloc.h>
472b3ad188SAdrian Chadd #include <sys/proc.h>
482b3ad188SAdrian Chadd #include <sys/queue.h>
492b3ad188SAdrian Chadd #include <sys/bus.h>
502b3ad188SAdrian Chadd #include <sys/interrupt.h>
512b3ad188SAdrian Chadd #include <sys/conf.h>
522b3ad188SAdrian Chadd #include <sys/cpuset.h>
536b42a1f4SAndrew Turner #include <sys/rman.h>
542b3ad188SAdrian Chadd #include <sys/sched.h>
552b3ad188SAdrian Chadd #include <sys/smp.h>
562b3ad188SAdrian Chadd #include <machine/atomic.h>
572b3ad188SAdrian Chadd #include <machine/intr.h>
582b3ad188SAdrian Chadd #include <machine/cpu.h>
592b3ad188SAdrian Chadd #include <machine/smp.h>
602b3ad188SAdrian Chadd #include <machine/stdarg.h>
612b3ad188SAdrian Chadd 
620cc5515aSAdrian Chadd #ifdef FDT
632b3ad188SAdrian Chadd #include <dev/ofw/openfirm.h>
642b3ad188SAdrian Chadd #include <dev/ofw/ofw_bus.h>
652b3ad188SAdrian Chadd #include <dev/ofw/ofw_bus_subr.h>
660cc5515aSAdrian Chadd #endif
672b3ad188SAdrian Chadd 
682b3ad188SAdrian Chadd #ifdef DDB
692b3ad188SAdrian Chadd #include <ddb/ddb.h>
702b3ad188SAdrian Chadd #endif
712b3ad188SAdrian Chadd 
722b3ad188SAdrian Chadd #include "pic_if.h"
733fc155dcSAndrew Turner #include "msi_if.h"
742b3ad188SAdrian Chadd 
752b3ad188SAdrian Chadd #define	INTRNAME_LEN	(2*MAXCOMLEN + 1)
762b3ad188SAdrian Chadd 
772b3ad188SAdrian Chadd #ifdef DEBUG
782b3ad188SAdrian Chadd #define debugf(fmt, args...) do { printf("%s(): ", __func__);	\
792b3ad188SAdrian Chadd     printf(fmt,##args); } while (0)
802b3ad188SAdrian Chadd #else
812b3ad188SAdrian Chadd #define debugf(fmt, args...)
822b3ad188SAdrian Chadd #endif
832b3ad188SAdrian Chadd 
842b3ad188SAdrian Chadd MALLOC_DECLARE(M_INTRNG);
852b3ad188SAdrian Chadd MALLOC_DEFINE(M_INTRNG, "intr", "intr interrupt handling");
862b3ad188SAdrian Chadd 
872b3ad188SAdrian Chadd /* Main interrupt handler called from assembler -> 'hidden' for C code. */
882b3ad188SAdrian Chadd void intr_irq_handler(struct trapframe *tf);
892b3ad188SAdrian Chadd 
902b3ad188SAdrian Chadd /* Root interrupt controller stuff. */
915b70c08cSSvatopluk Kraus device_t intr_irq_root_dev;
922b3ad188SAdrian Chadd static intr_irq_filter_t *irq_root_filter;
932b3ad188SAdrian Chadd static void *irq_root_arg;
942b3ad188SAdrian Chadd static u_int irq_root_ipicount;
952b3ad188SAdrian Chadd 
962b3ad188SAdrian Chadd /* Interrupt controller definition. */
972b3ad188SAdrian Chadd struct intr_pic {
982b3ad188SAdrian Chadd 	SLIST_ENTRY(intr_pic)	pic_next;
992b3ad188SAdrian Chadd 	intptr_t		pic_xref;	/* hardware identification */
1002b3ad188SAdrian Chadd 	device_t		pic_dev;
1013fc155dcSAndrew Turner #define	FLAG_PIC	(1 << 0)
1023fc155dcSAndrew Turner #define	FLAG_MSI	(1 << 1)
1033fc155dcSAndrew Turner 	u_int			pic_flags;
1042b3ad188SAdrian Chadd };
1052b3ad188SAdrian Chadd 
1062b3ad188SAdrian Chadd static struct mtx pic_list_lock;
1072b3ad188SAdrian Chadd static SLIST_HEAD(, intr_pic) pic_list;
1082b3ad188SAdrian Chadd 
1092b3ad188SAdrian Chadd static struct intr_pic *pic_lookup(device_t dev, intptr_t xref);
1102b3ad188SAdrian Chadd 
1112b3ad188SAdrian Chadd /* Interrupt source definition. */
1122b3ad188SAdrian Chadd static struct mtx isrc_table_lock;
1132b3ad188SAdrian Chadd static struct intr_irqsrc *irq_sources[NIRQ];
1142b3ad188SAdrian Chadd u_int irq_next_free;
1152b3ad188SAdrian Chadd 
116bff6be3eSSvatopluk Kraus /*
117bff6be3eSSvatopluk Kraus  *  XXX - All stuff around struct intr_dev_data is considered as temporary
118bff6be3eSSvatopluk Kraus  *  until better place for storing struct intr_map_data will be find.
119bff6be3eSSvatopluk Kraus  *
120bff6be3eSSvatopluk Kraus  *  For now, there are two global interrupt numbers spaces:
121bff6be3eSSvatopluk Kraus  *  <0, NIRQ)                      ... interrupts without config data
122bff6be3eSSvatopluk Kraus  *                                     managed in irq_sources[]
123bff6be3eSSvatopluk Kraus  *  IRQ_DDATA_BASE + <0, 2 * NIRQ) ... interrupts with config data
124bff6be3eSSvatopluk Kraus  *                                     managed in intr_ddata_tab[]
125bff6be3eSSvatopluk Kraus  *
126bff6be3eSSvatopluk Kraus  *  Read intr_ddata_lookup() to see how these spaces are worked with.
127bff6be3eSSvatopluk Kraus  *  Note that each interrupt number from second space duplicates some number
128bff6be3eSSvatopluk Kraus  *  from first space at this moment. An interrupt number from first space can
129bff6be3eSSvatopluk Kraus  *  be duplicated even multiple times in second space.
130bff6be3eSSvatopluk Kraus  */
131bff6be3eSSvatopluk Kraus struct intr_dev_data {
132bff6be3eSSvatopluk Kraus 	device_t		idd_dev;
133bff6be3eSSvatopluk Kraus 	intptr_t		idd_xref;
134bff6be3eSSvatopluk Kraus 	u_int			idd_irq;
135cd642c88SSvatopluk Kraus 	struct intr_map_data *	idd_data;
136bff6be3eSSvatopluk Kraus 	struct intr_irqsrc *	idd_isrc;
137bff6be3eSSvatopluk Kraus };
138bff6be3eSSvatopluk Kraus 
139bff6be3eSSvatopluk Kraus static struct intr_dev_data *intr_ddata_tab[2 * NIRQ];
140bff6be3eSSvatopluk Kraus static u_int intr_ddata_first_unused;
141bff6be3eSSvatopluk Kraus 
142bff6be3eSSvatopluk Kraus #define IRQ_DDATA_BASE	10000
1438442087fSMichal Meloun CTASSERT(IRQ_DDATA_BASE > nitems(irq_sources));
144bff6be3eSSvatopluk Kraus 
1452b3ad188SAdrian Chadd #ifdef SMP
1462b3ad188SAdrian Chadd static boolean_t irq_assign_cpu = FALSE;
1472b3ad188SAdrian Chadd #endif
1482b3ad188SAdrian Chadd 
1492b3ad188SAdrian Chadd /*
1502b3ad188SAdrian Chadd  * - 2 counters for each I/O interrupt.
1512b3ad188SAdrian Chadd  * - MAXCPU counters for each IPI counters for SMP.
1522b3ad188SAdrian Chadd  */
1532b3ad188SAdrian Chadd #ifdef SMP
1542b3ad188SAdrian Chadd #define INTRCNT_COUNT   (NIRQ * 2 + INTR_IPI_COUNT * MAXCPU)
1552b3ad188SAdrian Chadd #else
1562b3ad188SAdrian Chadd #define INTRCNT_COUNT   (NIRQ * 2)
1572b3ad188SAdrian Chadd #endif
1582b3ad188SAdrian Chadd 
1592b3ad188SAdrian Chadd /* Data for MI statistics reporting. */
1602b3ad188SAdrian Chadd u_long intrcnt[INTRCNT_COUNT];
1612b3ad188SAdrian Chadd char intrnames[INTRCNT_COUNT * INTRNAME_LEN];
1622b3ad188SAdrian Chadd size_t sintrcnt = sizeof(intrcnt);
1632b3ad188SAdrian Chadd size_t sintrnames = sizeof(intrnames);
1642b3ad188SAdrian Chadd static u_int intrcnt_index;
1652b3ad188SAdrian Chadd 
1662b3ad188SAdrian Chadd /*
1672b3ad188SAdrian Chadd  *  Interrupt framework initialization routine.
1682b3ad188SAdrian Chadd  */
1692b3ad188SAdrian Chadd static void
1702b3ad188SAdrian Chadd intr_irq_init(void *dummy __unused)
1712b3ad188SAdrian Chadd {
1722b3ad188SAdrian Chadd 
1732b3ad188SAdrian Chadd 	SLIST_INIT(&pic_list);
1742b3ad188SAdrian Chadd 	mtx_init(&pic_list_lock, "intr pic list", NULL, MTX_DEF);
1753fc155dcSAndrew Turner 
1762b3ad188SAdrian Chadd 	mtx_init(&isrc_table_lock, "intr isrc table", NULL, MTX_DEF);
1772b3ad188SAdrian Chadd }
1782b3ad188SAdrian Chadd SYSINIT(intr_irq_init, SI_SUB_INTR, SI_ORDER_FIRST, intr_irq_init, NULL);
1792b3ad188SAdrian Chadd 
1802b3ad188SAdrian Chadd static void
1812b3ad188SAdrian Chadd intrcnt_setname(const char *name, int index)
1822b3ad188SAdrian Chadd {
1832b3ad188SAdrian Chadd 
1842b3ad188SAdrian Chadd 	snprintf(intrnames + INTRNAME_LEN * index, INTRNAME_LEN, "%-*s",
1852b3ad188SAdrian Chadd 	    INTRNAME_LEN - 1, name);
1862b3ad188SAdrian Chadd }
1872b3ad188SAdrian Chadd 
1882b3ad188SAdrian Chadd /*
1892b3ad188SAdrian Chadd  *  Update name for interrupt source with interrupt event.
1902b3ad188SAdrian Chadd  */
1912b3ad188SAdrian Chadd static void
1922b3ad188SAdrian Chadd intrcnt_updatename(struct intr_irqsrc *isrc)
1932b3ad188SAdrian Chadd {
1942b3ad188SAdrian Chadd 
1952b3ad188SAdrian Chadd 	/* QQQ: What about stray counter name? */
1962b3ad188SAdrian Chadd 	mtx_assert(&isrc_table_lock, MA_OWNED);
1972b3ad188SAdrian Chadd 	intrcnt_setname(isrc->isrc_event->ie_fullname, isrc->isrc_index);
1982b3ad188SAdrian Chadd }
1992b3ad188SAdrian Chadd 
2002b3ad188SAdrian Chadd /*
2012b3ad188SAdrian Chadd  *  Virtualization for interrupt source interrupt counter increment.
2022b3ad188SAdrian Chadd  */
2032b3ad188SAdrian Chadd static inline void
2042b3ad188SAdrian Chadd isrc_increment_count(struct intr_irqsrc *isrc)
2052b3ad188SAdrian Chadd {
2062b3ad188SAdrian Chadd 
207bff6be3eSSvatopluk Kraus 	if (isrc->isrc_flags & INTR_ISRCF_PPI)
208bff6be3eSSvatopluk Kraus 		atomic_add_long(&isrc->isrc_count[0], 1);
209bff6be3eSSvatopluk Kraus 	else
2102b3ad188SAdrian Chadd 		isrc->isrc_count[0]++;
2112b3ad188SAdrian Chadd }
2122b3ad188SAdrian Chadd 
2132b3ad188SAdrian Chadd /*
2142b3ad188SAdrian Chadd  *  Virtualization for interrupt source interrupt stray counter increment.
2152b3ad188SAdrian Chadd  */
2162b3ad188SAdrian Chadd static inline void
2172b3ad188SAdrian Chadd isrc_increment_straycount(struct intr_irqsrc *isrc)
2182b3ad188SAdrian Chadd {
2192b3ad188SAdrian Chadd 
2202b3ad188SAdrian Chadd 	isrc->isrc_count[1]++;
2212b3ad188SAdrian Chadd }
2222b3ad188SAdrian Chadd 
2232b3ad188SAdrian Chadd /*
2242b3ad188SAdrian Chadd  *  Virtualization for interrupt source interrupt name update.
2252b3ad188SAdrian Chadd  */
2262b3ad188SAdrian Chadd static void
2272b3ad188SAdrian Chadd isrc_update_name(struct intr_irqsrc *isrc, const char *name)
2282b3ad188SAdrian Chadd {
2292b3ad188SAdrian Chadd 	char str[INTRNAME_LEN];
2302b3ad188SAdrian Chadd 
2312b3ad188SAdrian Chadd 	mtx_assert(&isrc_table_lock, MA_OWNED);
2322b3ad188SAdrian Chadd 
2332b3ad188SAdrian Chadd 	if (name != NULL) {
2342b3ad188SAdrian Chadd 		snprintf(str, INTRNAME_LEN, "%s: %s", isrc->isrc_name, name);
2352b3ad188SAdrian Chadd 		intrcnt_setname(str, isrc->isrc_index);
2362b3ad188SAdrian Chadd 		snprintf(str, INTRNAME_LEN, "stray %s: %s", isrc->isrc_name,
2372b3ad188SAdrian Chadd 		    name);
2382b3ad188SAdrian Chadd 		intrcnt_setname(str, isrc->isrc_index + 1);
2392b3ad188SAdrian Chadd 	} else {
2402b3ad188SAdrian Chadd 		snprintf(str, INTRNAME_LEN, "%s:", isrc->isrc_name);
2412b3ad188SAdrian Chadd 		intrcnt_setname(str, isrc->isrc_index);
2422b3ad188SAdrian Chadd 		snprintf(str, INTRNAME_LEN, "stray %s:", isrc->isrc_name);
2432b3ad188SAdrian Chadd 		intrcnt_setname(str, isrc->isrc_index + 1);
2442b3ad188SAdrian Chadd 	}
2452b3ad188SAdrian Chadd }
2462b3ad188SAdrian Chadd 
2472b3ad188SAdrian Chadd /*
2482b3ad188SAdrian Chadd  *  Virtualization for interrupt source interrupt counters setup.
2492b3ad188SAdrian Chadd  */
2502b3ad188SAdrian Chadd static void
2512b3ad188SAdrian Chadd isrc_setup_counters(struct intr_irqsrc *isrc)
2522b3ad188SAdrian Chadd {
2532b3ad188SAdrian Chadd 	u_int index;
2542b3ad188SAdrian Chadd 
2552b3ad188SAdrian Chadd 	/*
2562b3ad188SAdrian Chadd 	 *  XXX - it does not work well with removable controllers and
2572b3ad188SAdrian Chadd 	 *        interrupt sources !!!
2582b3ad188SAdrian Chadd 	 */
2592b3ad188SAdrian Chadd 	index = atomic_fetchadd_int(&intrcnt_index, 2);
2602b3ad188SAdrian Chadd 	isrc->isrc_index = index;
2612b3ad188SAdrian Chadd 	isrc->isrc_count = &intrcnt[index];
2622b3ad188SAdrian Chadd 	isrc_update_name(isrc, NULL);
2632b3ad188SAdrian Chadd }
2642b3ad188SAdrian Chadd 
265bff6be3eSSvatopluk Kraus /*
266bff6be3eSSvatopluk Kraus  *  Virtualization for interrupt source interrupt counters release.
267bff6be3eSSvatopluk Kraus  */
268bff6be3eSSvatopluk Kraus static void
269bff6be3eSSvatopluk Kraus isrc_release_counters(struct intr_irqsrc *isrc)
270bff6be3eSSvatopluk Kraus {
271bff6be3eSSvatopluk Kraus 
272bff6be3eSSvatopluk Kraus 	panic("%s: not implemented", __func__);
273bff6be3eSSvatopluk Kraus }
274bff6be3eSSvatopluk Kraus 
2752b3ad188SAdrian Chadd #ifdef SMP
2762b3ad188SAdrian Chadd /*
2772b3ad188SAdrian Chadd  *  Virtualization for interrupt source IPI counters setup.
2782b3ad188SAdrian Chadd  */
2795b70c08cSSvatopluk Kraus u_long *
2805b70c08cSSvatopluk Kraus intr_ipi_setup_counters(const char *name)
2812b3ad188SAdrian Chadd {
2822b3ad188SAdrian Chadd 	u_int index, i;
2832b3ad188SAdrian Chadd 	char str[INTRNAME_LEN];
2842b3ad188SAdrian Chadd 
2852b3ad188SAdrian Chadd 	index = atomic_fetchadd_int(&intrcnt_index, MAXCPU);
2862b3ad188SAdrian Chadd 	for (i = 0; i < MAXCPU; i++) {
2872b3ad188SAdrian Chadd 		snprintf(str, INTRNAME_LEN, "cpu%d:%s", i, name);
2882b3ad188SAdrian Chadd 		intrcnt_setname(str, index + i);
2892b3ad188SAdrian Chadd 	}
2905b70c08cSSvatopluk Kraus 	return (&intrcnt[index]);
2912b3ad188SAdrian Chadd }
2922b3ad188SAdrian Chadd #endif
2932b3ad188SAdrian Chadd 
2942b3ad188SAdrian Chadd /*
2952b3ad188SAdrian Chadd  *  Main interrupt dispatch handler. It's called straight
2962b3ad188SAdrian Chadd  *  from the assembler, where CPU interrupt is served.
2972b3ad188SAdrian Chadd  */
2982b3ad188SAdrian Chadd void
2992b3ad188SAdrian Chadd intr_irq_handler(struct trapframe *tf)
3002b3ad188SAdrian Chadd {
3012b3ad188SAdrian Chadd 	struct trapframe * oldframe;
3022b3ad188SAdrian Chadd 	struct thread * td;
3032b3ad188SAdrian Chadd 
3042b3ad188SAdrian Chadd 	KASSERT(irq_root_filter != NULL, ("%s: no filter", __func__));
3052b3ad188SAdrian Chadd 
3062b3ad188SAdrian Chadd 	PCPU_INC(cnt.v_intr);
3072b3ad188SAdrian Chadd 	critical_enter();
3082b3ad188SAdrian Chadd 	td = curthread;
3092b3ad188SAdrian Chadd 	oldframe = td->td_intr_frame;
3102b3ad188SAdrian Chadd 	td->td_intr_frame = tf;
3112b3ad188SAdrian Chadd 	irq_root_filter(irq_root_arg);
3122b3ad188SAdrian Chadd 	td->td_intr_frame = oldframe;
3132b3ad188SAdrian Chadd 	critical_exit();
3142b3ad188SAdrian Chadd }
3152b3ad188SAdrian Chadd 
3162b3ad188SAdrian Chadd /*
3172b3ad188SAdrian Chadd  *  interrupt controller dispatch function for interrupts. It should
3182b3ad188SAdrian Chadd  *  be called straight from the interrupt controller, when associated interrupt
3192b3ad188SAdrian Chadd  *  source is learned.
3202b3ad188SAdrian Chadd  */
321bff6be3eSSvatopluk Kraus int
322bff6be3eSSvatopluk Kraus intr_isrc_dispatch(struct intr_irqsrc *isrc, struct trapframe *tf)
3232b3ad188SAdrian Chadd {
3242b3ad188SAdrian Chadd 
3252b3ad188SAdrian Chadd 	KASSERT(isrc != NULL, ("%s: no source", __func__));
3262b3ad188SAdrian Chadd 
3272b3ad188SAdrian Chadd 	isrc_increment_count(isrc);
3282b3ad188SAdrian Chadd 
3292b3ad188SAdrian Chadd #ifdef INTR_SOLO
3302b3ad188SAdrian Chadd 	if (isrc->isrc_filter != NULL) {
3312b3ad188SAdrian Chadd 		int error;
3322b3ad188SAdrian Chadd 		error = isrc->isrc_filter(isrc->isrc_arg, tf);
3332b3ad188SAdrian Chadd 		PIC_POST_FILTER(isrc->isrc_dev, isrc);
3342b3ad188SAdrian Chadd 		if (error == FILTER_HANDLED)
335bff6be3eSSvatopluk Kraus 			return (0);
3362b3ad188SAdrian Chadd 	} else
3372b3ad188SAdrian Chadd #endif
3382b3ad188SAdrian Chadd 	if (isrc->isrc_event != NULL) {
3392b3ad188SAdrian Chadd 		if (intr_event_handle(isrc->isrc_event, tf) == 0)
340bff6be3eSSvatopluk Kraus 			return (0);
3412b3ad188SAdrian Chadd 	}
3422b3ad188SAdrian Chadd 
3432b3ad188SAdrian Chadd 	isrc_increment_straycount(isrc);
344bff6be3eSSvatopluk Kraus 	return (EINVAL);
3452b3ad188SAdrian Chadd }
3462b3ad188SAdrian Chadd 
3472b3ad188SAdrian Chadd /*
3482b3ad188SAdrian Chadd  *  Alloc unique interrupt number (resource handle) for interrupt source.
3492b3ad188SAdrian Chadd  *
3502b3ad188SAdrian Chadd  *  There could be various strategies how to allocate free interrupt number
3512b3ad188SAdrian Chadd  *  (resource handle) for new interrupt source.
3522b3ad188SAdrian Chadd  *
3532b3ad188SAdrian Chadd  *  1. Handles are always allocated forward, so handles are not recycled
3542b3ad188SAdrian Chadd  *     immediately. However, if only one free handle left which is reused
3552b3ad188SAdrian Chadd  *     constantly...
3562b3ad188SAdrian Chadd  */
357bff6be3eSSvatopluk Kraus static inline int
358bff6be3eSSvatopluk Kraus isrc_alloc_irq(struct intr_irqsrc *isrc)
3592b3ad188SAdrian Chadd {
3602b3ad188SAdrian Chadd 	u_int maxirqs, irq;
3612b3ad188SAdrian Chadd 
3622b3ad188SAdrian Chadd 	mtx_assert(&isrc_table_lock, MA_OWNED);
3632b3ad188SAdrian Chadd 
3642b3ad188SAdrian Chadd 	maxirqs = nitems(irq_sources);
3652b3ad188SAdrian Chadd 	if (irq_next_free >= maxirqs)
3662b3ad188SAdrian Chadd 		return (ENOSPC);
3672b3ad188SAdrian Chadd 
3682b3ad188SAdrian Chadd 	for (irq = irq_next_free; irq < maxirqs; irq++) {
3692b3ad188SAdrian Chadd 		if (irq_sources[irq] == NULL)
3702b3ad188SAdrian Chadd 			goto found;
3712b3ad188SAdrian Chadd 	}
3722b3ad188SAdrian Chadd 	for (irq = 0; irq < irq_next_free; irq++) {
3732b3ad188SAdrian Chadd 		if (irq_sources[irq] == NULL)
3742b3ad188SAdrian Chadd 			goto found;
3752b3ad188SAdrian Chadd 	}
3762b3ad188SAdrian Chadd 
3772b3ad188SAdrian Chadd 	irq_next_free = maxirqs;
3782b3ad188SAdrian Chadd 	return (ENOSPC);
3792b3ad188SAdrian Chadd 
3802b3ad188SAdrian Chadd found:
3812b3ad188SAdrian Chadd 	isrc->isrc_irq = irq;
3822b3ad188SAdrian Chadd 	irq_sources[irq] = isrc;
3832b3ad188SAdrian Chadd 
3842b3ad188SAdrian Chadd 	irq_next_free = irq + 1;
3852b3ad188SAdrian Chadd 	if (irq_next_free >= maxirqs)
3862b3ad188SAdrian Chadd 		irq_next_free = 0;
3872b3ad188SAdrian Chadd 	return (0);
3882b3ad188SAdrian Chadd }
389bff6be3eSSvatopluk Kraus 
3902b3ad188SAdrian Chadd /*
3912b3ad188SAdrian Chadd  *  Free unique interrupt number (resource handle) from interrupt source.
3922b3ad188SAdrian Chadd  */
393bff6be3eSSvatopluk Kraus static inline int
3942b3ad188SAdrian Chadd isrc_free_irq(struct intr_irqsrc *isrc)
3952b3ad188SAdrian Chadd {
3962b3ad188SAdrian Chadd 
397bff6be3eSSvatopluk Kraus 	mtx_assert(&isrc_table_lock, MA_OWNED);
3982b3ad188SAdrian Chadd 
399bff6be3eSSvatopluk Kraus 	if (isrc->isrc_irq >= nitems(irq_sources))
4002b3ad188SAdrian Chadd 		return (EINVAL);
401bff6be3eSSvatopluk Kraus 	if (irq_sources[isrc->isrc_irq] != isrc)
4022b3ad188SAdrian Chadd 		return (EINVAL);
4032b3ad188SAdrian Chadd 
4042b3ad188SAdrian Chadd 	irq_sources[isrc->isrc_irq] = NULL;
4058442087fSMichal Meloun 	isrc->isrc_irq = INTR_IRQ_INVALID;	/* just to be safe */
4062b3ad188SAdrian Chadd 	return (0);
4072b3ad188SAdrian Chadd }
408bff6be3eSSvatopluk Kraus 
4092b3ad188SAdrian Chadd /*
4102b3ad188SAdrian Chadd  *  Lookup interrupt source by interrupt number (resource handle).
4112b3ad188SAdrian Chadd  */
412bff6be3eSSvatopluk Kraus static inline struct intr_irqsrc *
4132b3ad188SAdrian Chadd isrc_lookup(u_int irq)
4142b3ad188SAdrian Chadd {
4152b3ad188SAdrian Chadd 
4162b3ad188SAdrian Chadd 	if (irq < nitems(irq_sources))
4172b3ad188SAdrian Chadd 		return (irq_sources[irq]);
4182b3ad188SAdrian Chadd 	return (NULL);
4192b3ad188SAdrian Chadd }
4202b3ad188SAdrian Chadd 
4212b3ad188SAdrian Chadd /*
422bff6be3eSSvatopluk Kraus  *  Initialize interrupt source and register it into global interrupt table.
4232b3ad188SAdrian Chadd  */
424bff6be3eSSvatopluk Kraus int
425bff6be3eSSvatopluk Kraus intr_isrc_register(struct intr_irqsrc *isrc, device_t dev, u_int flags,
426bff6be3eSSvatopluk Kraus     const char *fmt, ...)
4272b3ad188SAdrian Chadd {
428bff6be3eSSvatopluk Kraus 	int error;
429bff6be3eSSvatopluk Kraus 	va_list ap;
4302b3ad188SAdrian Chadd 
431bff6be3eSSvatopluk Kraus 	bzero(isrc, sizeof(struct intr_irqsrc));
432bff6be3eSSvatopluk Kraus 	isrc->isrc_dev = dev;
4338442087fSMichal Meloun 	isrc->isrc_irq = INTR_IRQ_INVALID;	/* just to be safe */
434bff6be3eSSvatopluk Kraus 	isrc->isrc_flags = flags;
4352b3ad188SAdrian Chadd 
436bff6be3eSSvatopluk Kraus 	va_start(ap, fmt);
437bff6be3eSSvatopluk Kraus 	vsnprintf(isrc->isrc_name, INTR_ISRC_NAMELEN, fmt, ap);
438bff6be3eSSvatopluk Kraus 	va_end(ap);
439bff6be3eSSvatopluk Kraus 
440bff6be3eSSvatopluk Kraus 	mtx_lock(&isrc_table_lock);
441bff6be3eSSvatopluk Kraus 	error = isrc_alloc_irq(isrc);
442bff6be3eSSvatopluk Kraus 	if (error != 0) {
443bff6be3eSSvatopluk Kraus 		mtx_unlock(&isrc_table_lock);
444bff6be3eSSvatopluk Kraus 		return (error);
4452b3ad188SAdrian Chadd 	}
446bff6be3eSSvatopluk Kraus 	/*
447bff6be3eSSvatopluk Kraus 	 * Setup interrupt counters, but not for IPI sources. Those are setup
448bff6be3eSSvatopluk Kraus 	 * later and only for used ones (up to INTR_IPI_COUNT) to not exhaust
449bff6be3eSSvatopluk Kraus 	 * our counter pool.
450bff6be3eSSvatopluk Kraus 	 */
451bff6be3eSSvatopluk Kraus 	if ((isrc->isrc_flags & INTR_ISRCF_IPI) == 0)
452bff6be3eSSvatopluk Kraus 		isrc_setup_counters(isrc);
453bff6be3eSSvatopluk Kraus 	mtx_unlock(&isrc_table_lock);
454bff6be3eSSvatopluk Kraus 	return (0);
4552b3ad188SAdrian Chadd }
4562b3ad188SAdrian Chadd 
4572b3ad188SAdrian Chadd /*
458bff6be3eSSvatopluk Kraus  *  Deregister interrupt source from global interrupt table.
459bff6be3eSSvatopluk Kraus  */
460bff6be3eSSvatopluk Kraus int
461bff6be3eSSvatopluk Kraus intr_isrc_deregister(struct intr_irqsrc *isrc)
462bff6be3eSSvatopluk Kraus {
463bff6be3eSSvatopluk Kraus 	int error;
464bff6be3eSSvatopluk Kraus 
465bff6be3eSSvatopluk Kraus 	mtx_lock(&isrc_table_lock);
466bff6be3eSSvatopluk Kraus 	if ((isrc->isrc_flags & INTR_ISRCF_IPI) == 0)
467bff6be3eSSvatopluk Kraus 		isrc_release_counters(isrc);
468bff6be3eSSvatopluk Kraus 	error = isrc_free_irq(isrc);
469bff6be3eSSvatopluk Kraus 	mtx_unlock(&isrc_table_lock);
470bff6be3eSSvatopluk Kraus 	return (error);
471bff6be3eSSvatopluk Kraus }
472bff6be3eSSvatopluk Kraus 
4735b613c19SSvatopluk Kraus #ifdef SMP
4745b613c19SSvatopluk Kraus /*
4755b613c19SSvatopluk Kraus  *  A support function for a PIC to decide if provided ISRC should be inited
4765b613c19SSvatopluk Kraus  *  on given cpu. The logic of INTR_ISRCF_BOUND flag and isrc_cpu member of
4775b613c19SSvatopluk Kraus  *  struct intr_irqsrc is the following:
4785b613c19SSvatopluk Kraus  *
4795b613c19SSvatopluk Kraus  *     If INTR_ISRCF_BOUND is set, the ISRC should be inited only on cpus
4805b613c19SSvatopluk Kraus  *     set in isrc_cpu. If not, the ISRC should be inited on every cpu and
4815b613c19SSvatopluk Kraus  *     isrc_cpu is kept consistent with it. Thus isrc_cpu is always correct.
4825b613c19SSvatopluk Kraus  */
4835b613c19SSvatopluk Kraus bool
4845b613c19SSvatopluk Kraus intr_isrc_init_on_cpu(struct intr_irqsrc *isrc, u_int cpu)
4855b613c19SSvatopluk Kraus {
4865b613c19SSvatopluk Kraus 
4875b613c19SSvatopluk Kraus 	if (isrc->isrc_handlers == 0)
4885b613c19SSvatopluk Kraus 		return (false);
4895b613c19SSvatopluk Kraus 	if ((isrc->isrc_flags & (INTR_ISRCF_PPI | INTR_ISRCF_IPI)) == 0)
4905b613c19SSvatopluk Kraus 		return (false);
4915b613c19SSvatopluk Kraus 	if (isrc->isrc_flags & INTR_ISRCF_BOUND)
4925b613c19SSvatopluk Kraus 		return (CPU_ISSET(cpu, &isrc->isrc_cpu));
4935b613c19SSvatopluk Kraus 
4945b613c19SSvatopluk Kraus 	CPU_SET(cpu, &isrc->isrc_cpu);
4955b613c19SSvatopluk Kraus 	return (true);
4965b613c19SSvatopluk Kraus }
4975b613c19SSvatopluk Kraus #endif
4985b613c19SSvatopluk Kraus 
499bff6be3eSSvatopluk Kraus static struct intr_dev_data *
500bff6be3eSSvatopluk Kraus intr_ddata_alloc(u_int extsize)
501bff6be3eSSvatopluk Kraus {
502bff6be3eSSvatopluk Kraus 	struct intr_dev_data *ddata;
503cd642c88SSvatopluk Kraus 	size_t size;
504bff6be3eSSvatopluk Kraus 
505cd642c88SSvatopluk Kraus 	size = sizeof(*ddata);
506cd642c88SSvatopluk Kraus 	ddata = malloc(size + extsize, M_INTRNG, M_WAITOK | M_ZERO);
507bff6be3eSSvatopluk Kraus 
508bff6be3eSSvatopluk Kraus 	mtx_lock(&isrc_table_lock);
509bff6be3eSSvatopluk Kraus 	if (intr_ddata_first_unused >= nitems(intr_ddata_tab)) {
510bff6be3eSSvatopluk Kraus 		mtx_unlock(&isrc_table_lock);
511bff6be3eSSvatopluk Kraus 		free(ddata, M_INTRNG);
512bff6be3eSSvatopluk Kraus 		return (NULL);
513bff6be3eSSvatopluk Kraus 	}
514bff6be3eSSvatopluk Kraus 	intr_ddata_tab[intr_ddata_first_unused] = ddata;
515bff6be3eSSvatopluk Kraus 	ddata->idd_irq = IRQ_DDATA_BASE + intr_ddata_first_unused++;
516bff6be3eSSvatopluk Kraus 	mtx_unlock(&isrc_table_lock);
517cd642c88SSvatopluk Kraus 
518cd642c88SSvatopluk Kraus 	ddata->idd_data = (struct intr_map_data *)((uintptr_t)ddata + size);
519a100280eSSvatopluk Kraus 	ddata->idd_data->size = extsize;
520bff6be3eSSvatopluk Kraus 	return (ddata);
521bff6be3eSSvatopluk Kraus }
522bff6be3eSSvatopluk Kraus 
523bff6be3eSSvatopluk Kraus static struct intr_irqsrc *
524bff6be3eSSvatopluk Kraus intr_ddata_lookup(u_int irq, struct intr_map_data **datap)
525bff6be3eSSvatopluk Kraus {
526bff6be3eSSvatopluk Kraus 	int error;
527bff6be3eSSvatopluk Kraus 	struct intr_irqsrc *isrc;
528bff6be3eSSvatopluk Kraus 	struct intr_dev_data *ddata;
529bff6be3eSSvatopluk Kraus 
530bff6be3eSSvatopluk Kraus 	isrc = isrc_lookup(irq);
531bff6be3eSSvatopluk Kraus 	if (isrc != NULL) {
532bff6be3eSSvatopluk Kraus 		if (datap != NULL)
533bff6be3eSSvatopluk Kraus 			*datap = NULL;
534bff6be3eSSvatopluk Kraus 		return (isrc);
535bff6be3eSSvatopluk Kraus 	}
536bff6be3eSSvatopluk Kraus 
537bff6be3eSSvatopluk Kraus 	if (irq < IRQ_DDATA_BASE)
538bff6be3eSSvatopluk Kraus 		return (NULL);
539bff6be3eSSvatopluk Kraus 
540bff6be3eSSvatopluk Kraus 	irq -= IRQ_DDATA_BASE;
541bff6be3eSSvatopluk Kraus 	if (irq >= nitems(intr_ddata_tab))
542bff6be3eSSvatopluk Kraus 		return (NULL);
543bff6be3eSSvatopluk Kraus 
544bff6be3eSSvatopluk Kraus 	ddata = intr_ddata_tab[irq];
545bff6be3eSSvatopluk Kraus 	if (ddata->idd_isrc == NULL) {
546bff6be3eSSvatopluk Kraus 		error = intr_map_irq(ddata->idd_dev, ddata->idd_xref,
547cd642c88SSvatopluk Kraus 		    ddata->idd_data, &irq);
548bff6be3eSSvatopluk Kraus 		if (error != 0)
549bff6be3eSSvatopluk Kraus 			return (NULL);
550bff6be3eSSvatopluk Kraus 		ddata->idd_isrc = isrc_lookup(irq);
551bff6be3eSSvatopluk Kraus 	}
552bff6be3eSSvatopluk Kraus 	if (datap != NULL)
553cd642c88SSvatopluk Kraus 		*datap = ddata->idd_data;
554bff6be3eSSvatopluk Kraus 	return (ddata->idd_isrc);
555bff6be3eSSvatopluk Kraus }
556bff6be3eSSvatopluk Kraus 
557bff6be3eSSvatopluk Kraus #ifdef DEV_ACPI
558bff6be3eSSvatopluk Kraus /*
559bff6be3eSSvatopluk Kraus  *  Map interrupt source according to ACPI info into framework. If such mapping
5602b3ad188SAdrian Chadd  *  does not exist, create it. Return unique interrupt number (resource handle)
5612b3ad188SAdrian Chadd  *  associated with mapped interrupt source.
5622b3ad188SAdrian Chadd  */
5632b3ad188SAdrian Chadd u_int
564bff6be3eSSvatopluk Kraus intr_acpi_map_irq(device_t dev, u_int irq, enum intr_polarity pol,
565bff6be3eSSvatopluk Kraus     enum intr_trigger trig)
5662b3ad188SAdrian Chadd {
567cd642c88SSvatopluk Kraus 	struct intr_map_data_acpi *daa;
568bff6be3eSSvatopluk Kraus 	struct intr_dev_data *ddata;
5692b3ad188SAdrian Chadd 
570cd642c88SSvatopluk Kraus 	ddata = intr_ddata_alloc(sizeof(struct intr_map_data_acpi));
571bff6be3eSSvatopluk Kraus 	if (ddata == NULL)
5728442087fSMichal Meloun 		return (INTR_IRQ_INVALID);	/* no space left */
5732b3ad188SAdrian Chadd 
574bff6be3eSSvatopluk Kraus 	ddata->idd_dev = dev;
575cd642c88SSvatopluk Kraus 	ddata->idd_data->type = INTR_MAP_DATA_ACPI;
576cd642c88SSvatopluk Kraus 
577cd642c88SSvatopluk Kraus 	daa = (struct intr_map_data_acpi *)ddata->idd_data;
578cd642c88SSvatopluk Kraus 	daa->irq = irq;
579cd642c88SSvatopluk Kraus 	daa->pol = pol;
580cd642c88SSvatopluk Kraus 	daa->trig = trig;
581cd642c88SSvatopluk Kraus 
582bff6be3eSSvatopluk Kraus 	return (ddata->idd_irq);
5832b3ad188SAdrian Chadd }
584bff6be3eSSvatopluk Kraus #endif
5852b3ad188SAdrian Chadd #ifdef FDT
5862b3ad188SAdrian Chadd /*
5872b3ad188SAdrian Chadd  *  Map interrupt source according to FDT data into framework. If such mapping
5882b3ad188SAdrian Chadd  *  does not exist, create it. Return unique interrupt number (resource handle)
5892b3ad188SAdrian Chadd  *  associated with mapped interrupt source.
5902b3ad188SAdrian Chadd  */
5912b3ad188SAdrian Chadd u_int
5922b3ad188SAdrian Chadd intr_fdt_map_irq(phandle_t node, pcell_t *cells, u_int ncells)
5932b3ad188SAdrian Chadd {
594cd642c88SSvatopluk Kraus 	size_t cellsize;
595bff6be3eSSvatopluk Kraus 	struct intr_dev_data *ddata;
596cd642c88SSvatopluk Kraus 	struct intr_map_data_fdt *daf;
5972b3ad188SAdrian Chadd 
5982b3ad188SAdrian Chadd 	cellsize = ncells * sizeof(*cells);
599cd642c88SSvatopluk Kraus 	ddata = intr_ddata_alloc(sizeof(struct intr_map_data_fdt) + cellsize);
600bff6be3eSSvatopluk Kraus 	if (ddata == NULL)
6018442087fSMichal Meloun 		return (INTR_IRQ_INVALID);	/* no space left */
6022b3ad188SAdrian Chadd 
603bff6be3eSSvatopluk Kraus 	ddata->idd_xref = (intptr_t)node;
604cd642c88SSvatopluk Kraus 	ddata->idd_data->type = INTR_MAP_DATA_FDT;
605cd642c88SSvatopluk Kraus 
606cd642c88SSvatopluk Kraus 	daf = (struct intr_map_data_fdt *)ddata->idd_data;
607cd642c88SSvatopluk Kraus 	daf->ncells = ncells;
608cd642c88SSvatopluk Kraus 	memcpy(daf->cells, cells, cellsize);
609bff6be3eSSvatopluk Kraus 	return (ddata->idd_irq);
6102b3ad188SAdrian Chadd }
6112b3ad188SAdrian Chadd #endif
6122b3ad188SAdrian Chadd 
61339f6c1bdSMichal Meloun /*
61439f6c1bdSMichal Meloun  *  Store GPIO interrupt decription in framework and return unique interrupt
61539f6c1bdSMichal Meloun  *  number (resource handle) associated with it.
61639f6c1bdSMichal Meloun  */
61739f6c1bdSMichal Meloun u_int
61839f6c1bdSMichal Meloun intr_gpio_map_irq(device_t dev, u_int pin_num, u_int pin_flags, u_int intr_mode)
61939f6c1bdSMichal Meloun {
62039f6c1bdSMichal Meloun 	struct intr_dev_data *ddata;
621cd642c88SSvatopluk Kraus 	struct intr_map_data_gpio *dag;
62239f6c1bdSMichal Meloun 
623cd642c88SSvatopluk Kraus 	ddata = intr_ddata_alloc(sizeof(struct intr_map_data_gpio));
62439f6c1bdSMichal Meloun 	if (ddata == NULL)
6258442087fSMichal Meloun 		return (INTR_IRQ_INVALID);	/* no space left */
62639f6c1bdSMichal Meloun 
62739f6c1bdSMichal Meloun 	ddata->idd_dev = dev;
628cd642c88SSvatopluk Kraus 	ddata->idd_data->type = INTR_MAP_DATA_GPIO;
629cd642c88SSvatopluk Kraus 
630cd642c88SSvatopluk Kraus 	dag = (struct intr_map_data_gpio *)ddata->idd_data;
631cd642c88SSvatopluk Kraus 	dag->gpio_pin_num = pin_num;
632cd642c88SSvatopluk Kraus 	dag->gpio_pin_flags = pin_flags;
633cd642c88SSvatopluk Kraus 	dag->gpio_intr_mode = intr_mode;
63439f6c1bdSMichal Meloun 	return (ddata->idd_irq);
63539f6c1bdSMichal Meloun }
63639f6c1bdSMichal Meloun 
6372b3ad188SAdrian Chadd #ifdef INTR_SOLO
6382b3ad188SAdrian Chadd /*
6392b3ad188SAdrian Chadd  *  Setup filter into interrupt source.
6402b3ad188SAdrian Chadd  */
6412b3ad188SAdrian Chadd static int
6422b3ad188SAdrian Chadd iscr_setup_filter(struct intr_irqsrc *isrc, const char *name,
6432b3ad188SAdrian Chadd     intr_irq_filter_t *filter, void *arg, void **cookiep)
6442b3ad188SAdrian Chadd {
6452b3ad188SAdrian Chadd 
6462b3ad188SAdrian Chadd 	if (filter == NULL)
6472b3ad188SAdrian Chadd 		return (EINVAL);
6482b3ad188SAdrian Chadd 
6492b3ad188SAdrian Chadd 	mtx_lock(&isrc_table_lock);
6502b3ad188SAdrian Chadd 	/*
6512b3ad188SAdrian Chadd 	 * Make sure that we do not mix the two ways
6522b3ad188SAdrian Chadd 	 * how we handle interrupt sources.
6532b3ad188SAdrian Chadd 	 */
6542b3ad188SAdrian Chadd 	if (isrc->isrc_filter != NULL || isrc->isrc_event != NULL) {
6552b3ad188SAdrian Chadd 		mtx_unlock(&isrc_table_lock);
6562b3ad188SAdrian Chadd 		return (EBUSY);
6572b3ad188SAdrian Chadd 	}
6582b3ad188SAdrian Chadd 	isrc->isrc_filter = filter;
6592b3ad188SAdrian Chadd 	isrc->isrc_arg = arg;
6602b3ad188SAdrian Chadd 	isrc_update_name(isrc, name);
6612b3ad188SAdrian Chadd 	mtx_unlock(&isrc_table_lock);
6622b3ad188SAdrian Chadd 
6632b3ad188SAdrian Chadd 	*cookiep = isrc;
6642b3ad188SAdrian Chadd 	return (0);
6652b3ad188SAdrian Chadd }
6662b3ad188SAdrian Chadd #endif
6672b3ad188SAdrian Chadd 
6682b3ad188SAdrian Chadd /*
6692b3ad188SAdrian Chadd  *  Interrupt source pre_ithread method for MI interrupt framework.
6702b3ad188SAdrian Chadd  */
6712b3ad188SAdrian Chadd static void
6722b3ad188SAdrian Chadd intr_isrc_pre_ithread(void *arg)
6732b3ad188SAdrian Chadd {
6742b3ad188SAdrian Chadd 	struct intr_irqsrc *isrc = arg;
6752b3ad188SAdrian Chadd 
6762b3ad188SAdrian Chadd 	PIC_PRE_ITHREAD(isrc->isrc_dev, isrc);
6772b3ad188SAdrian Chadd }
6782b3ad188SAdrian Chadd 
6792b3ad188SAdrian Chadd /*
6802b3ad188SAdrian Chadd  *  Interrupt source post_ithread method for MI interrupt framework.
6812b3ad188SAdrian Chadd  */
6822b3ad188SAdrian Chadd static void
6832b3ad188SAdrian Chadd intr_isrc_post_ithread(void *arg)
6842b3ad188SAdrian Chadd {
6852b3ad188SAdrian Chadd 	struct intr_irqsrc *isrc = arg;
6862b3ad188SAdrian Chadd 
6872b3ad188SAdrian Chadd 	PIC_POST_ITHREAD(isrc->isrc_dev, isrc);
6882b3ad188SAdrian Chadd }
6892b3ad188SAdrian Chadd 
6902b3ad188SAdrian Chadd /*
6912b3ad188SAdrian Chadd  *  Interrupt source post_filter method for MI interrupt framework.
6922b3ad188SAdrian Chadd  */
6932b3ad188SAdrian Chadd static void
6942b3ad188SAdrian Chadd intr_isrc_post_filter(void *arg)
6952b3ad188SAdrian Chadd {
6962b3ad188SAdrian Chadd 	struct intr_irqsrc *isrc = arg;
6972b3ad188SAdrian Chadd 
6982b3ad188SAdrian Chadd 	PIC_POST_FILTER(isrc->isrc_dev, isrc);
6992b3ad188SAdrian Chadd }
7002b3ad188SAdrian Chadd 
7012b3ad188SAdrian Chadd /*
7022b3ad188SAdrian Chadd  *  Interrupt source assign_cpu method for MI interrupt framework.
7032b3ad188SAdrian Chadd  */
7042b3ad188SAdrian Chadd static int
7052b3ad188SAdrian Chadd intr_isrc_assign_cpu(void *arg, int cpu)
7062b3ad188SAdrian Chadd {
7072b3ad188SAdrian Chadd #ifdef SMP
7082b3ad188SAdrian Chadd 	struct intr_irqsrc *isrc = arg;
7092b3ad188SAdrian Chadd 	int error;
7102b3ad188SAdrian Chadd 
7115b70c08cSSvatopluk Kraus 	if (isrc->isrc_dev != intr_irq_root_dev)
7122b3ad188SAdrian Chadd 		return (EINVAL);
7132b3ad188SAdrian Chadd 
7142b3ad188SAdrian Chadd 	mtx_lock(&isrc_table_lock);
7152b3ad188SAdrian Chadd 	if (cpu == NOCPU) {
7162b3ad188SAdrian Chadd 		CPU_ZERO(&isrc->isrc_cpu);
7172b3ad188SAdrian Chadd 		isrc->isrc_flags &= ~INTR_ISRCF_BOUND;
7182b3ad188SAdrian Chadd 	} else {
7192b3ad188SAdrian Chadd 		CPU_SETOF(cpu, &isrc->isrc_cpu);
7202b3ad188SAdrian Chadd 		isrc->isrc_flags |= INTR_ISRCF_BOUND;
7212b3ad188SAdrian Chadd 	}
7222b3ad188SAdrian Chadd 
7232b3ad188SAdrian Chadd 	/*
7242b3ad188SAdrian Chadd 	 * In NOCPU case, it's up to PIC to either leave ISRC on same CPU or
7252b3ad188SAdrian Chadd 	 * re-balance it to another CPU or enable it on more CPUs. However,
7262b3ad188SAdrian Chadd 	 * PIC is expected to change isrc_cpu appropriately to keep us well
727e3043798SPedro F. Giffuni 	 * informed if the call is successful.
7282b3ad188SAdrian Chadd 	 */
7292b3ad188SAdrian Chadd 	if (irq_assign_cpu) {
730bff6be3eSSvatopluk Kraus 		error = PIC_BIND_INTR(isrc->isrc_dev, isrc);
7312b3ad188SAdrian Chadd 		if (error) {
7322b3ad188SAdrian Chadd 			CPU_ZERO(&isrc->isrc_cpu);
7332b3ad188SAdrian Chadd 			mtx_unlock(&isrc_table_lock);
7342b3ad188SAdrian Chadd 			return (error);
7352b3ad188SAdrian Chadd 		}
7362b3ad188SAdrian Chadd 	}
7372b3ad188SAdrian Chadd 	mtx_unlock(&isrc_table_lock);
7382b3ad188SAdrian Chadd 	return (0);
7392b3ad188SAdrian Chadd #else
7402b3ad188SAdrian Chadd 	return (EOPNOTSUPP);
7412b3ad188SAdrian Chadd #endif
7422b3ad188SAdrian Chadd }
7432b3ad188SAdrian Chadd 
7442b3ad188SAdrian Chadd /*
7452b3ad188SAdrian Chadd  *  Create interrupt event for interrupt source.
7462b3ad188SAdrian Chadd  */
7472b3ad188SAdrian Chadd static int
7482b3ad188SAdrian Chadd isrc_event_create(struct intr_irqsrc *isrc)
7492b3ad188SAdrian Chadd {
7502b3ad188SAdrian Chadd 	struct intr_event *ie;
7512b3ad188SAdrian Chadd 	int error;
7522b3ad188SAdrian Chadd 
7532b3ad188SAdrian Chadd 	error = intr_event_create(&ie, isrc, 0, isrc->isrc_irq,
7542b3ad188SAdrian Chadd 	    intr_isrc_pre_ithread, intr_isrc_post_ithread, intr_isrc_post_filter,
7552b3ad188SAdrian Chadd 	    intr_isrc_assign_cpu, "%s:", isrc->isrc_name);
7562b3ad188SAdrian Chadd 	if (error)
7572b3ad188SAdrian Chadd 		return (error);
7582b3ad188SAdrian Chadd 
7592b3ad188SAdrian Chadd 	mtx_lock(&isrc_table_lock);
7602b3ad188SAdrian Chadd 	/*
7612b3ad188SAdrian Chadd 	 * Make sure that we do not mix the two ways
7622b3ad188SAdrian Chadd 	 * how we handle interrupt sources. Let contested event wins.
7632b3ad188SAdrian Chadd 	 */
764169e6abdSSvatopluk Kraus #ifdef INTR_SOLO
7652b3ad188SAdrian Chadd 	if (isrc->isrc_filter != NULL || isrc->isrc_event != NULL) {
766169e6abdSSvatopluk Kraus #else
767169e6abdSSvatopluk Kraus 	if (isrc->isrc_event != NULL) {
768169e6abdSSvatopluk Kraus #endif
7692b3ad188SAdrian Chadd 		mtx_unlock(&isrc_table_lock);
7702b3ad188SAdrian Chadd 		intr_event_destroy(ie);
7712b3ad188SAdrian Chadd 		return (isrc->isrc_event != NULL ? EBUSY : 0);
7722b3ad188SAdrian Chadd 	}
7732b3ad188SAdrian Chadd 	isrc->isrc_event = ie;
7742b3ad188SAdrian Chadd 	mtx_unlock(&isrc_table_lock);
7752b3ad188SAdrian Chadd 
7762b3ad188SAdrian Chadd 	return (0);
7772b3ad188SAdrian Chadd }
7782b3ad188SAdrian Chadd #ifdef notyet
7792b3ad188SAdrian Chadd /*
7802b3ad188SAdrian Chadd  *  Destroy interrupt event for interrupt source.
7812b3ad188SAdrian Chadd  */
7822b3ad188SAdrian Chadd static void
7832b3ad188SAdrian Chadd isrc_event_destroy(struct intr_irqsrc *isrc)
7842b3ad188SAdrian Chadd {
7852b3ad188SAdrian Chadd 	struct intr_event *ie;
7862b3ad188SAdrian Chadd 
7872b3ad188SAdrian Chadd 	mtx_lock(&isrc_table_lock);
7882b3ad188SAdrian Chadd 	ie = isrc->isrc_event;
7892b3ad188SAdrian Chadd 	isrc->isrc_event = NULL;
7902b3ad188SAdrian Chadd 	mtx_unlock(&isrc_table_lock);
7912b3ad188SAdrian Chadd 
7922b3ad188SAdrian Chadd 	if (ie != NULL)
7932b3ad188SAdrian Chadd 		intr_event_destroy(ie);
7942b3ad188SAdrian Chadd }
7952b3ad188SAdrian Chadd #endif
7962b3ad188SAdrian Chadd /*
7972b3ad188SAdrian Chadd  *  Add handler to interrupt source.
7982b3ad188SAdrian Chadd  */
7992b3ad188SAdrian Chadd static int
8002b3ad188SAdrian Chadd isrc_add_handler(struct intr_irqsrc *isrc, const char *name,
8012b3ad188SAdrian Chadd     driver_filter_t filter, driver_intr_t handler, void *arg,
8022b3ad188SAdrian Chadd     enum intr_type flags, void **cookiep)
8032b3ad188SAdrian Chadd {
8042b3ad188SAdrian Chadd 	int error;
8052b3ad188SAdrian Chadd 
8062b3ad188SAdrian Chadd 	if (isrc->isrc_event == NULL) {
8072b3ad188SAdrian Chadd 		error = isrc_event_create(isrc);
8082b3ad188SAdrian Chadd 		if (error)
8092b3ad188SAdrian Chadd 			return (error);
8102b3ad188SAdrian Chadd 	}
8112b3ad188SAdrian Chadd 
8122b3ad188SAdrian Chadd 	error = intr_event_add_handler(isrc->isrc_event, name, filter, handler,
8132b3ad188SAdrian Chadd 	    arg, intr_priority(flags), flags, cookiep);
8142b3ad188SAdrian Chadd 	if (error == 0) {
8152b3ad188SAdrian Chadd 		mtx_lock(&isrc_table_lock);
8162b3ad188SAdrian Chadd 		intrcnt_updatename(isrc);
8172b3ad188SAdrian Chadd 		mtx_unlock(&isrc_table_lock);
8182b3ad188SAdrian Chadd 	}
8192b3ad188SAdrian Chadd 
8202b3ad188SAdrian Chadd 	return (error);
8212b3ad188SAdrian Chadd }
8222b3ad188SAdrian Chadd 
8232b3ad188SAdrian Chadd /*
8242b3ad188SAdrian Chadd  *  Lookup interrupt controller locked.
8252b3ad188SAdrian Chadd  */
826bff6be3eSSvatopluk Kraus static inline struct intr_pic *
8272b3ad188SAdrian Chadd pic_lookup_locked(device_t dev, intptr_t xref)
8282b3ad188SAdrian Chadd {
8292b3ad188SAdrian Chadd 	struct intr_pic *pic;
8302b3ad188SAdrian Chadd 
8312b3ad188SAdrian Chadd 	mtx_assert(&pic_list_lock, MA_OWNED);
8322b3ad188SAdrian Chadd 
8334be58cbaSSvatopluk Kraus 	if (dev == NULL && xref == 0)
8344be58cbaSSvatopluk Kraus 		return (NULL);
8354be58cbaSSvatopluk Kraus 
8364be58cbaSSvatopluk Kraus 	/* Note that pic->pic_dev is never NULL on registered PIC. */
8372b3ad188SAdrian Chadd 	SLIST_FOREACH(pic, &pic_list, pic_next) {
8384be58cbaSSvatopluk Kraus 		if (dev == NULL) {
8394be58cbaSSvatopluk Kraus 			if (xref == pic->pic_xref)
8404be58cbaSSvatopluk Kraus 				return (pic);
8414be58cbaSSvatopluk Kraus 		} else if (xref == 0 || pic->pic_xref == 0) {
8424be58cbaSSvatopluk Kraus 			if (dev == pic->pic_dev)
8434be58cbaSSvatopluk Kraus 				return (pic);
8444be58cbaSSvatopluk Kraus 		} else if (xref == pic->pic_xref && dev == pic->pic_dev)
8452b3ad188SAdrian Chadd 				return (pic);
8462b3ad188SAdrian Chadd 	}
8472b3ad188SAdrian Chadd 	return (NULL);
8482b3ad188SAdrian Chadd }
8492b3ad188SAdrian Chadd 
8502b3ad188SAdrian Chadd /*
8512b3ad188SAdrian Chadd  *  Lookup interrupt controller.
8522b3ad188SAdrian Chadd  */
8532b3ad188SAdrian Chadd static struct intr_pic *
8542b3ad188SAdrian Chadd pic_lookup(device_t dev, intptr_t xref)
8552b3ad188SAdrian Chadd {
8562b3ad188SAdrian Chadd 	struct intr_pic *pic;
8572b3ad188SAdrian Chadd 
8582b3ad188SAdrian Chadd 	mtx_lock(&pic_list_lock);
8592b3ad188SAdrian Chadd 	pic = pic_lookup_locked(dev, xref);
8602b3ad188SAdrian Chadd 	mtx_unlock(&pic_list_lock);
8612b3ad188SAdrian Chadd 	return (pic);
8622b3ad188SAdrian Chadd }
8632b3ad188SAdrian Chadd 
8642b3ad188SAdrian Chadd /*
8652b3ad188SAdrian Chadd  *  Create interrupt controller.
8662b3ad188SAdrian Chadd  */
8672b3ad188SAdrian Chadd static struct intr_pic *
8682b3ad188SAdrian Chadd pic_create(device_t dev, intptr_t xref)
8692b3ad188SAdrian Chadd {
8702b3ad188SAdrian Chadd 	struct intr_pic *pic;
8712b3ad188SAdrian Chadd 
8722b3ad188SAdrian Chadd 	mtx_lock(&pic_list_lock);
8732b3ad188SAdrian Chadd 	pic = pic_lookup_locked(dev, xref);
8742b3ad188SAdrian Chadd 	if (pic != NULL) {
8752b3ad188SAdrian Chadd 		mtx_unlock(&pic_list_lock);
8762b3ad188SAdrian Chadd 		return (pic);
8772b3ad188SAdrian Chadd 	}
8782b3ad188SAdrian Chadd 	pic = malloc(sizeof(*pic), M_INTRNG, M_NOWAIT | M_ZERO);
879b48c6083SAndrew Turner 	if (pic == NULL) {
880b48c6083SAndrew Turner 		mtx_unlock(&pic_list_lock);
881b48c6083SAndrew Turner 		return (NULL);
882b48c6083SAndrew Turner 	}
8832b3ad188SAdrian Chadd 	pic->pic_xref = xref;
8842b3ad188SAdrian Chadd 	pic->pic_dev = dev;
8852b3ad188SAdrian Chadd 	SLIST_INSERT_HEAD(&pic_list, pic, pic_next);
8862b3ad188SAdrian Chadd 	mtx_unlock(&pic_list_lock);
8872b3ad188SAdrian Chadd 
8882b3ad188SAdrian Chadd 	return (pic);
8892b3ad188SAdrian Chadd }
8902b3ad188SAdrian Chadd #ifdef notyet
8912b3ad188SAdrian Chadd /*
8922b3ad188SAdrian Chadd  *  Destroy interrupt controller.
8932b3ad188SAdrian Chadd  */
8942b3ad188SAdrian Chadd static void
8952b3ad188SAdrian Chadd pic_destroy(device_t dev, intptr_t xref)
8962b3ad188SAdrian Chadd {
8972b3ad188SAdrian Chadd 	struct intr_pic *pic;
8982b3ad188SAdrian Chadd 
8992b3ad188SAdrian Chadd 	mtx_lock(&pic_list_lock);
9002b3ad188SAdrian Chadd 	pic = pic_lookup_locked(dev, xref);
9012b3ad188SAdrian Chadd 	if (pic == NULL) {
9022b3ad188SAdrian Chadd 		mtx_unlock(&pic_list_lock);
9032b3ad188SAdrian Chadd 		return;
9042b3ad188SAdrian Chadd 	}
9052b3ad188SAdrian Chadd 	SLIST_REMOVE(&pic_list, pic, intr_pic, pic_next);
9062b3ad188SAdrian Chadd 	mtx_unlock(&pic_list_lock);
9072b3ad188SAdrian Chadd 
9082b3ad188SAdrian Chadd 	free(pic, M_INTRNG);
9092b3ad188SAdrian Chadd }
9102b3ad188SAdrian Chadd #endif
9112b3ad188SAdrian Chadd /*
9122b3ad188SAdrian Chadd  *  Register interrupt controller.
9132b3ad188SAdrian Chadd  */
914*9346e913SAndrew Turner struct intr_pic *
9152b3ad188SAdrian Chadd intr_pic_register(device_t dev, intptr_t xref)
9162b3ad188SAdrian Chadd {
9172b3ad188SAdrian Chadd 	struct intr_pic *pic;
9182b3ad188SAdrian Chadd 
9194be58cbaSSvatopluk Kraus 	if (dev == NULL)
920*9346e913SAndrew Turner 		return (NULL);
9212b3ad188SAdrian Chadd 	pic = pic_create(dev, xref);
9222b3ad188SAdrian Chadd 	if (pic == NULL)
923*9346e913SAndrew Turner 		return (NULL);
9242b3ad188SAdrian Chadd 
9253fc155dcSAndrew Turner 	pic->pic_flags |= FLAG_PIC;
9263fc155dcSAndrew Turner 
9274be58cbaSSvatopluk Kraus 	debugf("PIC %p registered for %s <dev %p, xref %x>\n", pic,
9284be58cbaSSvatopluk Kraus 	    device_get_nameunit(dev), dev, xref);
929*9346e913SAndrew Turner 	return (pic);
9302b3ad188SAdrian Chadd }
9312b3ad188SAdrian Chadd 
9322b3ad188SAdrian Chadd /*
9332b3ad188SAdrian Chadd  *  Unregister interrupt controller.
9342b3ad188SAdrian Chadd  */
9352b3ad188SAdrian Chadd int
936bff6be3eSSvatopluk Kraus intr_pic_deregister(device_t dev, intptr_t xref)
9372b3ad188SAdrian Chadd {
9382b3ad188SAdrian Chadd 
9392b3ad188SAdrian Chadd 	panic("%s: not implemented", __func__);
9402b3ad188SAdrian Chadd }
9412b3ad188SAdrian Chadd 
9422b3ad188SAdrian Chadd /*
9432b3ad188SAdrian Chadd  *  Mark interrupt controller (itself) as a root one.
9442b3ad188SAdrian Chadd  *
9452b3ad188SAdrian Chadd  *  Note that only an interrupt controller can really know its position
9462b3ad188SAdrian Chadd  *  in interrupt controller's tree. So root PIC must claim itself as a root.
9472b3ad188SAdrian Chadd  *
9482b3ad188SAdrian Chadd  *  In FDT case, according to ePAPR approved version 1.1 from 08 April 2011,
9492b3ad188SAdrian Chadd  *  page 30:
9502b3ad188SAdrian Chadd  *    "The root of the interrupt tree is determined when traversal
9512b3ad188SAdrian Chadd  *     of the interrupt tree reaches an interrupt controller node without
9522b3ad188SAdrian Chadd  *     an interrupts property and thus no explicit interrupt parent."
9532b3ad188SAdrian Chadd  */
9542b3ad188SAdrian Chadd int
9552b3ad188SAdrian Chadd intr_pic_claim_root(device_t dev, intptr_t xref, intr_irq_filter_t *filter,
9562b3ad188SAdrian Chadd     void *arg, u_int ipicount)
9572b3ad188SAdrian Chadd {
9583fc155dcSAndrew Turner 	struct intr_pic *pic;
9592b3ad188SAdrian Chadd 
9603fc155dcSAndrew Turner 	pic = pic_lookup(dev, xref);
9613fc155dcSAndrew Turner 	if (pic == NULL) {
9622b3ad188SAdrian Chadd 		device_printf(dev, "not registered\n");
9632b3ad188SAdrian Chadd 		return (EINVAL);
9642b3ad188SAdrian Chadd 	}
9653fc155dcSAndrew Turner 
9663fc155dcSAndrew Turner 	KASSERT((pic->pic_flags & FLAG_PIC) != 0,
9673fc155dcSAndrew Turner 	    ("%s: Found a non-PIC controller: %s", __func__,
9683fc155dcSAndrew Turner 	     device_get_name(pic->pic_dev)));
9693fc155dcSAndrew Turner 
9702b3ad188SAdrian Chadd 	if (filter == NULL) {
9712b3ad188SAdrian Chadd 		device_printf(dev, "filter missing\n");
9722b3ad188SAdrian Chadd 		return (EINVAL);
9732b3ad188SAdrian Chadd 	}
9742b3ad188SAdrian Chadd 
9752b3ad188SAdrian Chadd 	/*
9762b3ad188SAdrian Chadd 	 * Only one interrupt controllers could be on the root for now.
9772b3ad188SAdrian Chadd 	 * Note that we further suppose that there is not threaded interrupt
9782b3ad188SAdrian Chadd 	 * routine (handler) on the root. See intr_irq_handler().
9792b3ad188SAdrian Chadd 	 */
9805b70c08cSSvatopluk Kraus 	if (intr_irq_root_dev != NULL) {
9812b3ad188SAdrian Chadd 		device_printf(dev, "another root already set\n");
9822b3ad188SAdrian Chadd 		return (EBUSY);
9832b3ad188SAdrian Chadd 	}
9842b3ad188SAdrian Chadd 
9855b70c08cSSvatopluk Kraus 	intr_irq_root_dev = dev;
9862b3ad188SAdrian Chadd 	irq_root_filter = filter;
9872b3ad188SAdrian Chadd 	irq_root_arg = arg;
9882b3ad188SAdrian Chadd 	irq_root_ipicount = ipicount;
9892b3ad188SAdrian Chadd 
9902b3ad188SAdrian Chadd 	debugf("irq root set to %s\n", device_get_nameunit(dev));
9912b3ad188SAdrian Chadd 	return (0);
9922b3ad188SAdrian Chadd }
9932b3ad188SAdrian Chadd 
9942b3ad188SAdrian Chadd int
995bff6be3eSSvatopluk Kraus intr_map_irq(device_t dev, intptr_t xref, struct intr_map_data *data,
996bff6be3eSSvatopluk Kraus     u_int *irqp)
9972b3ad188SAdrian Chadd {
9982b3ad188SAdrian Chadd 	int error;
999bff6be3eSSvatopluk Kraus 	struct intr_irqsrc *isrc;
1000bff6be3eSSvatopluk Kraus 	struct intr_pic *pic;
1001bff6be3eSSvatopluk Kraus 
1002bff6be3eSSvatopluk Kraus 	if (data == NULL)
1003bff6be3eSSvatopluk Kraus 		return (EINVAL);
1004bff6be3eSSvatopluk Kraus 
1005bff6be3eSSvatopluk Kraus 	pic = pic_lookup(dev, xref);
100615adccc6SSvatopluk Kraus 	if (pic == NULL)
1007bff6be3eSSvatopluk Kraus 		return (ESRCH);
1008bff6be3eSSvatopluk Kraus 
10093fc155dcSAndrew Turner 	KASSERT((pic->pic_flags & FLAG_PIC) != 0,
10103fc155dcSAndrew Turner 	    ("%s: Found a non-PIC controller: %s", __func__,
10113fc155dcSAndrew Turner 	     device_get_name(pic->pic_dev)));
10123fc155dcSAndrew Turner 
1013bff6be3eSSvatopluk Kraus 	error = PIC_MAP_INTR(pic->pic_dev, data, &isrc);
1014bff6be3eSSvatopluk Kraus 	if (error == 0)
1015bff6be3eSSvatopluk Kraus 		*irqp = isrc->isrc_irq;
1016bff6be3eSSvatopluk Kraus 	return (error);
1017bff6be3eSSvatopluk Kraus }
1018bff6be3eSSvatopluk Kraus 
1019bff6be3eSSvatopluk Kraus int
1020bff6be3eSSvatopluk Kraus intr_alloc_irq(device_t dev, struct resource *res)
1021bff6be3eSSvatopluk Kraus {
1022bff6be3eSSvatopluk Kraus 	struct intr_map_data *data;
1023bff6be3eSSvatopluk Kraus 	struct intr_irqsrc *isrc;
1024bff6be3eSSvatopluk Kraus 
1025bff6be3eSSvatopluk Kraus 	KASSERT(rman_get_start(res) == rman_get_end(res),
1026bff6be3eSSvatopluk Kraus 	    ("%s: more interrupts in resource", __func__));
1027bff6be3eSSvatopluk Kraus 
1028bff6be3eSSvatopluk Kraus 	isrc = intr_ddata_lookup(rman_get_start(res), &data);
1029bff6be3eSSvatopluk Kraus 	if (isrc == NULL)
1030bff6be3eSSvatopluk Kraus 		return (EINVAL);
1031bff6be3eSSvatopluk Kraus 
1032bff6be3eSSvatopluk Kraus 	return (PIC_ALLOC_INTR(isrc->isrc_dev, isrc, res, data));
1033bff6be3eSSvatopluk Kraus }
1034bff6be3eSSvatopluk Kraus 
1035bff6be3eSSvatopluk Kraus int
1036bff6be3eSSvatopluk Kraus intr_release_irq(device_t dev, struct resource *res)
1037bff6be3eSSvatopluk Kraus {
1038bff6be3eSSvatopluk Kraus 	struct intr_map_data *data;
1039bff6be3eSSvatopluk Kraus 	struct intr_irqsrc *isrc;
1040bff6be3eSSvatopluk Kraus 
1041bff6be3eSSvatopluk Kraus 	KASSERT(rman_get_start(res) == rman_get_end(res),
1042bff6be3eSSvatopluk Kraus 	    ("%s: more interrupts in resource", __func__));
1043bff6be3eSSvatopluk Kraus 
1044bff6be3eSSvatopluk Kraus 	isrc = intr_ddata_lookup(rman_get_start(res), &data);
1045bff6be3eSSvatopluk Kraus 	if (isrc == NULL)
1046bff6be3eSSvatopluk Kraus 		return (EINVAL);
1047bff6be3eSSvatopluk Kraus 
1048bff6be3eSSvatopluk Kraus 	return (PIC_RELEASE_INTR(isrc->isrc_dev, isrc, res, data));
1049bff6be3eSSvatopluk Kraus }
1050bff6be3eSSvatopluk Kraus 
1051bff6be3eSSvatopluk Kraus int
1052bff6be3eSSvatopluk Kraus intr_setup_irq(device_t dev, struct resource *res, driver_filter_t filt,
1053bff6be3eSSvatopluk Kraus     driver_intr_t hand, void *arg, int flags, void **cookiep)
1054bff6be3eSSvatopluk Kraus {
1055bff6be3eSSvatopluk Kraus 	int error;
1056bff6be3eSSvatopluk Kraus 	struct intr_map_data *data;
1057bff6be3eSSvatopluk Kraus 	struct intr_irqsrc *isrc;
1058bff6be3eSSvatopluk Kraus 	const char *name;
1059bff6be3eSSvatopluk Kraus 
1060bff6be3eSSvatopluk Kraus 	KASSERT(rman_get_start(res) == rman_get_end(res),
1061bff6be3eSSvatopluk Kraus 	    ("%s: more interrupts in resource", __func__));
1062bff6be3eSSvatopluk Kraus 
1063bff6be3eSSvatopluk Kraus 	isrc = intr_ddata_lookup(rman_get_start(res), &data);
1064bff6be3eSSvatopluk Kraus 	if (isrc == NULL)
1065bff6be3eSSvatopluk Kraus 		return (EINVAL);
10662b3ad188SAdrian Chadd 
10672b3ad188SAdrian Chadd 	name = device_get_nameunit(dev);
10682b3ad188SAdrian Chadd 
10692b3ad188SAdrian Chadd #ifdef INTR_SOLO
10702b3ad188SAdrian Chadd 	/*
1071e3043798SPedro F. Giffuni 	 * Standard handling is done through MI interrupt framework. However,
10722b3ad188SAdrian Chadd 	 * some interrupts could request solely own special handling. This
10732b3ad188SAdrian Chadd 	 * non standard handling can be used for interrupt controllers without
10742b3ad188SAdrian Chadd 	 * handler (filter only), so in case that interrupt controllers are
10752b3ad188SAdrian Chadd 	 * chained, MI interrupt framework is called only in leaf controller.
10762b3ad188SAdrian Chadd 	 *
10772b3ad188SAdrian Chadd 	 * Note that root interrupt controller routine is served as well,
10782b3ad188SAdrian Chadd 	 * however in intr_irq_handler(), i.e. main system dispatch routine.
10792b3ad188SAdrian Chadd 	 */
10802b3ad188SAdrian Chadd 	if (flags & INTR_SOLO && hand != NULL) {
10812b3ad188SAdrian Chadd 		debugf("irq %u cannot solo on %s\n", irq, name);
10822b3ad188SAdrian Chadd 		return (EINVAL);
10832b3ad188SAdrian Chadd 	}
10842b3ad188SAdrian Chadd 
10852b3ad188SAdrian Chadd 	if (flags & INTR_SOLO) {
10862b3ad188SAdrian Chadd 		error = iscr_setup_filter(isrc, name, (intr_irq_filter_t *)filt,
10872b3ad188SAdrian Chadd 		    arg, cookiep);
10882b3ad188SAdrian Chadd 		debugf("irq %u setup filter error %d on %s\n", irq, error,
10892b3ad188SAdrian Chadd 		    name);
10902b3ad188SAdrian Chadd 	} else
10912b3ad188SAdrian Chadd #endif
10922b3ad188SAdrian Chadd 		{
10932b3ad188SAdrian Chadd 		error = isrc_add_handler(isrc, name, filt, hand, arg, flags,
10942b3ad188SAdrian Chadd 		    cookiep);
10952b3ad188SAdrian Chadd 		debugf("irq %u add handler error %d on %s\n", irq, error, name);
10962b3ad188SAdrian Chadd 	}
10972b3ad188SAdrian Chadd 	if (error != 0)
10982b3ad188SAdrian Chadd 		return (error);
10992b3ad188SAdrian Chadd 
11002b3ad188SAdrian Chadd 	mtx_lock(&isrc_table_lock);
1101bff6be3eSSvatopluk Kraus 	error = PIC_SETUP_INTR(isrc->isrc_dev, isrc, res, data);
1102bff6be3eSSvatopluk Kraus 	if (error == 0) {
11032b3ad188SAdrian Chadd 		isrc->isrc_handlers++;
1104bff6be3eSSvatopluk Kraus 		if (isrc->isrc_handlers == 1)
11052b3ad188SAdrian Chadd 			PIC_ENABLE_INTR(isrc->isrc_dev, isrc);
11062b3ad188SAdrian Chadd 	}
11072b3ad188SAdrian Chadd 	mtx_unlock(&isrc_table_lock);
1108bff6be3eSSvatopluk Kraus 	if (error != 0)
1109bff6be3eSSvatopluk Kraus 		intr_event_remove_handler(*cookiep);
1110bff6be3eSSvatopluk Kraus 	return (error);
11112b3ad188SAdrian Chadd }
11122b3ad188SAdrian Chadd 
11132b3ad188SAdrian Chadd int
1114bff6be3eSSvatopluk Kraus intr_teardown_irq(device_t dev, struct resource *res, void *cookie)
11152b3ad188SAdrian Chadd {
11162b3ad188SAdrian Chadd 	int error;
1117bff6be3eSSvatopluk Kraus 	struct intr_map_data *data;
1118bff6be3eSSvatopluk Kraus 	struct intr_irqsrc *isrc;
11192b3ad188SAdrian Chadd 
1120bff6be3eSSvatopluk Kraus 	KASSERT(rman_get_start(res) == rman_get_end(res),
1121bff6be3eSSvatopluk Kraus 	    ("%s: more interrupts in resource", __func__));
1122bff6be3eSSvatopluk Kraus 
1123bff6be3eSSvatopluk Kraus 	isrc = intr_ddata_lookup(rman_get_start(res), &data);
11242b3ad188SAdrian Chadd 	if (isrc == NULL || isrc->isrc_handlers == 0)
11252b3ad188SAdrian Chadd 		return (EINVAL);
1126bff6be3eSSvatopluk Kraus 
1127169e6abdSSvatopluk Kraus #ifdef INTR_SOLO
11282b3ad188SAdrian Chadd 	if (isrc->isrc_filter != NULL) {
11292b3ad188SAdrian Chadd 		if (isrc != cookie)
11302b3ad188SAdrian Chadd 			return (EINVAL);
11312b3ad188SAdrian Chadd 
11322b3ad188SAdrian Chadd 		mtx_lock(&isrc_table_lock);
11332b3ad188SAdrian Chadd 		isrc->isrc_filter = NULL;
11342b3ad188SAdrian Chadd 		isrc->isrc_arg = NULL;
11352b3ad188SAdrian Chadd 		isrc->isrc_handlers = 0;
11362b3ad188SAdrian Chadd 		PIC_DISABLE_INTR(isrc->isrc_dev, isrc);
1137bff6be3eSSvatopluk Kraus 		PIC_TEARDOWN_INTR(isrc->isrc_dev, isrc, res, data);
11382b3ad188SAdrian Chadd 		isrc_update_name(isrc, NULL);
11392b3ad188SAdrian Chadd 		mtx_unlock(&isrc_table_lock);
11402b3ad188SAdrian Chadd 		return (0);
11412b3ad188SAdrian Chadd 	}
1142169e6abdSSvatopluk Kraus #endif
11432b3ad188SAdrian Chadd 	if (isrc != intr_handler_source(cookie))
11442b3ad188SAdrian Chadd 		return (EINVAL);
11452b3ad188SAdrian Chadd 
11462b3ad188SAdrian Chadd 	error = intr_event_remove_handler(cookie);
11472b3ad188SAdrian Chadd 	if (error == 0) {
11482b3ad188SAdrian Chadd 		mtx_lock(&isrc_table_lock);
11492b3ad188SAdrian Chadd 		isrc->isrc_handlers--;
1150bff6be3eSSvatopluk Kraus 		if (isrc->isrc_handlers == 0)
11512b3ad188SAdrian Chadd 			PIC_DISABLE_INTR(isrc->isrc_dev, isrc);
1152bff6be3eSSvatopluk Kraus 		PIC_TEARDOWN_INTR(isrc->isrc_dev, isrc, res, data);
11532b3ad188SAdrian Chadd 		intrcnt_updatename(isrc);
11542b3ad188SAdrian Chadd 		mtx_unlock(&isrc_table_lock);
11552b3ad188SAdrian Chadd 	}
11562b3ad188SAdrian Chadd 	return (error);
11572b3ad188SAdrian Chadd }
11582b3ad188SAdrian Chadd 
11592b3ad188SAdrian Chadd int
1160bff6be3eSSvatopluk Kraus intr_describe_irq(device_t dev, struct resource *res, void *cookie,
1161bff6be3eSSvatopluk Kraus     const char *descr)
11622b3ad188SAdrian Chadd {
11632b3ad188SAdrian Chadd 	int error;
1164bff6be3eSSvatopluk Kraus 	struct intr_irqsrc *isrc;
11652b3ad188SAdrian Chadd 
1166bff6be3eSSvatopluk Kraus 	KASSERT(rman_get_start(res) == rman_get_end(res),
1167bff6be3eSSvatopluk Kraus 	    ("%s: more interrupts in resource", __func__));
1168bff6be3eSSvatopluk Kraus 
1169bff6be3eSSvatopluk Kraus 	isrc = intr_ddata_lookup(rman_get_start(res), NULL);
11702b3ad188SAdrian Chadd 	if (isrc == NULL || isrc->isrc_handlers == 0)
11712b3ad188SAdrian Chadd 		return (EINVAL);
1172169e6abdSSvatopluk Kraus #ifdef INTR_SOLO
11732b3ad188SAdrian Chadd 	if (isrc->isrc_filter != NULL) {
11742b3ad188SAdrian Chadd 		if (isrc != cookie)
11752b3ad188SAdrian Chadd 			return (EINVAL);
11762b3ad188SAdrian Chadd 
11772b3ad188SAdrian Chadd 		mtx_lock(&isrc_table_lock);
11782b3ad188SAdrian Chadd 		isrc_update_name(isrc, descr);
11792b3ad188SAdrian Chadd 		mtx_unlock(&isrc_table_lock);
11802b3ad188SAdrian Chadd 		return (0);
11812b3ad188SAdrian Chadd 	}
1182169e6abdSSvatopluk Kraus #endif
11832b3ad188SAdrian Chadd 	error = intr_event_describe_handler(isrc->isrc_event, cookie, descr);
11842b3ad188SAdrian Chadd 	if (error == 0) {
11852b3ad188SAdrian Chadd 		mtx_lock(&isrc_table_lock);
11862b3ad188SAdrian Chadd 		intrcnt_updatename(isrc);
11872b3ad188SAdrian Chadd 		mtx_unlock(&isrc_table_lock);
11882b3ad188SAdrian Chadd 	}
11892b3ad188SAdrian Chadd 	return (error);
11902b3ad188SAdrian Chadd }
11912b3ad188SAdrian Chadd 
11922b3ad188SAdrian Chadd #ifdef SMP
11932b3ad188SAdrian Chadd int
1194bff6be3eSSvatopluk Kraus intr_bind_irq(device_t dev, struct resource *res, int cpu)
11952b3ad188SAdrian Chadd {
11962b3ad188SAdrian Chadd 	struct intr_irqsrc *isrc;
11972b3ad188SAdrian Chadd 
1198bff6be3eSSvatopluk Kraus 	KASSERT(rman_get_start(res) == rman_get_end(res),
1199bff6be3eSSvatopluk Kraus 	    ("%s: more interrupts in resource", __func__));
1200bff6be3eSSvatopluk Kraus 
1201bff6be3eSSvatopluk Kraus 	isrc = intr_ddata_lookup(rman_get_start(res), NULL);
12022b3ad188SAdrian Chadd 	if (isrc == NULL || isrc->isrc_handlers == 0)
12032b3ad188SAdrian Chadd 		return (EINVAL);
1204169e6abdSSvatopluk Kraus #ifdef INTR_SOLO
12052b3ad188SAdrian Chadd 	if (isrc->isrc_filter != NULL)
12062b3ad188SAdrian Chadd 		return (intr_isrc_assign_cpu(isrc, cpu));
1207169e6abdSSvatopluk Kraus #endif
12082b3ad188SAdrian Chadd 	return (intr_event_bind(isrc->isrc_event, cpu));
12092b3ad188SAdrian Chadd }
12102b3ad188SAdrian Chadd 
12112b3ad188SAdrian Chadd /*
12122b3ad188SAdrian Chadd  * Return the CPU that the next interrupt source should use.
12132b3ad188SAdrian Chadd  * For now just returns the next CPU according to round-robin.
12142b3ad188SAdrian Chadd  */
12152b3ad188SAdrian Chadd u_int
12162b3ad188SAdrian Chadd intr_irq_next_cpu(u_int last_cpu, cpuset_t *cpumask)
12172b3ad188SAdrian Chadd {
12182b3ad188SAdrian Chadd 
12192b3ad188SAdrian Chadd 	if (!irq_assign_cpu || mp_ncpus == 1)
12202b3ad188SAdrian Chadd 		return (PCPU_GET(cpuid));
12212b3ad188SAdrian Chadd 
12222b3ad188SAdrian Chadd 	do {
12232b3ad188SAdrian Chadd 		last_cpu++;
12242b3ad188SAdrian Chadd 		if (last_cpu > mp_maxid)
12252b3ad188SAdrian Chadd 			last_cpu = 0;
12262b3ad188SAdrian Chadd 	} while (!CPU_ISSET(last_cpu, cpumask));
12272b3ad188SAdrian Chadd 	return (last_cpu);
12282b3ad188SAdrian Chadd }
12292b3ad188SAdrian Chadd 
12302b3ad188SAdrian Chadd /*
12312b3ad188SAdrian Chadd  *  Distribute all the interrupt sources among the available
12322b3ad188SAdrian Chadd  *  CPUs once the AP's have been launched.
12332b3ad188SAdrian Chadd  */
12342b3ad188SAdrian Chadd static void
12352b3ad188SAdrian Chadd intr_irq_shuffle(void *arg __unused)
12362b3ad188SAdrian Chadd {
12372b3ad188SAdrian Chadd 	struct intr_irqsrc *isrc;
12382b3ad188SAdrian Chadd 	u_int i;
12392b3ad188SAdrian Chadd 
12402b3ad188SAdrian Chadd 	if (mp_ncpus == 1)
12412b3ad188SAdrian Chadd 		return;
12422b3ad188SAdrian Chadd 
12432b3ad188SAdrian Chadd 	mtx_lock(&isrc_table_lock);
12442b3ad188SAdrian Chadd 	irq_assign_cpu = TRUE;
12452b3ad188SAdrian Chadd 	for (i = 0; i < NIRQ; i++) {
12462b3ad188SAdrian Chadd 		isrc = irq_sources[i];
12472b3ad188SAdrian Chadd 		if (isrc == NULL || isrc->isrc_handlers == 0 ||
1248cf55df9fSSvatopluk Kraus 		    isrc->isrc_flags & (INTR_ISRCF_PPI | INTR_ISRCF_IPI))
12492b3ad188SAdrian Chadd 			continue;
12502b3ad188SAdrian Chadd 
12512b3ad188SAdrian Chadd 		if (isrc->isrc_event != NULL &&
12522b3ad188SAdrian Chadd 		    isrc->isrc_flags & INTR_ISRCF_BOUND &&
12532b3ad188SAdrian Chadd 		    isrc->isrc_event->ie_cpu != CPU_FFS(&isrc->isrc_cpu) - 1)
12542b3ad188SAdrian Chadd 			panic("%s: CPU inconsistency", __func__);
12552b3ad188SAdrian Chadd 
12562b3ad188SAdrian Chadd 		if ((isrc->isrc_flags & INTR_ISRCF_BOUND) == 0)
12572b3ad188SAdrian Chadd 			CPU_ZERO(&isrc->isrc_cpu); /* start again */
12582b3ad188SAdrian Chadd 
12592b3ad188SAdrian Chadd 		/*
12602b3ad188SAdrian Chadd 		 * We are in wicked position here if the following call fails
12612b3ad188SAdrian Chadd 		 * for bound ISRC. The best thing we can do is to clear
12622b3ad188SAdrian Chadd 		 * isrc_cpu so inconsistency with ie_cpu will be detectable.
12632b3ad188SAdrian Chadd 		 */
1264bff6be3eSSvatopluk Kraus 		if (PIC_BIND_INTR(isrc->isrc_dev, isrc) != 0)
12652b3ad188SAdrian Chadd 			CPU_ZERO(&isrc->isrc_cpu);
12662b3ad188SAdrian Chadd 	}
12672b3ad188SAdrian Chadd 	mtx_unlock(&isrc_table_lock);
12682b3ad188SAdrian Chadd }
12692b3ad188SAdrian Chadd SYSINIT(intr_irq_shuffle, SI_SUB_SMP, SI_ORDER_SECOND, intr_irq_shuffle, NULL);
12702b3ad188SAdrian Chadd 
12712b3ad188SAdrian Chadd #else
12722b3ad188SAdrian Chadd u_int
12732b3ad188SAdrian Chadd intr_irq_next_cpu(u_int current_cpu, cpuset_t *cpumask)
12742b3ad188SAdrian Chadd {
12752b3ad188SAdrian Chadd 
12762b3ad188SAdrian Chadd 	return (PCPU_GET(cpuid));
12772b3ad188SAdrian Chadd }
12782b3ad188SAdrian Chadd #endif
12792b3ad188SAdrian Chadd 
12803fc155dcSAndrew Turner /*
12813fc155dcSAndrew Turner  *  Register a MSI/MSI-X interrupt controller
12823fc155dcSAndrew Turner  */
12833fc155dcSAndrew Turner int
12843fc155dcSAndrew Turner intr_msi_register(device_t dev, intptr_t xref)
12853fc155dcSAndrew Turner {
12863fc155dcSAndrew Turner 	struct intr_pic *pic;
12873fc155dcSAndrew Turner 
12883fc155dcSAndrew Turner 	if (dev == NULL)
12893fc155dcSAndrew Turner 		return (EINVAL);
12903fc155dcSAndrew Turner 	pic = pic_create(dev, xref);
12913fc155dcSAndrew Turner 	if (pic == NULL)
12923fc155dcSAndrew Turner 		return (ENOMEM);
12933fc155dcSAndrew Turner 
12943fc155dcSAndrew Turner 	pic->pic_flags |= FLAG_MSI;
12953fc155dcSAndrew Turner 
12963fc155dcSAndrew Turner 	debugf("PIC %p registered for %s <dev %p, xref %jx>\n", pic,
12973fc155dcSAndrew Turner 	    device_get_nameunit(dev), dev, (uintmax_t)xref);
12983fc155dcSAndrew Turner 	return (0);
12993fc155dcSAndrew Turner }
13003fc155dcSAndrew Turner 
13013fc155dcSAndrew Turner int
13023fc155dcSAndrew Turner intr_alloc_msi(device_t pci, device_t child, intptr_t xref, int count,
13033fc155dcSAndrew Turner     int maxcount, int *irqs)
13043fc155dcSAndrew Turner {
13053fc155dcSAndrew Turner 	struct intr_irqsrc **isrc;
13063fc155dcSAndrew Turner 	struct intr_pic *pic;
13073fc155dcSAndrew Turner 	device_t pdev;
13083fc155dcSAndrew Turner 	int err, i;
13093fc155dcSAndrew Turner 
13103fc155dcSAndrew Turner 	pic = pic_lookup(NULL, xref);
13113fc155dcSAndrew Turner 	if (pic == NULL)
13123fc155dcSAndrew Turner 		return (ESRCH);
13133fc155dcSAndrew Turner 
13143fc155dcSAndrew Turner 	KASSERT((pic->pic_flags & FLAG_MSI) != 0,
13153fc155dcSAndrew Turner 	    ("%s: Found a non-MSI controller: %s", __func__,
13163fc155dcSAndrew Turner 	     device_get_name(pic->pic_dev)));
13173fc155dcSAndrew Turner 
13183fc155dcSAndrew Turner 	isrc = malloc(sizeof(*isrc) * count, M_INTRNG, M_WAITOK);
13193fc155dcSAndrew Turner 	err = MSI_ALLOC_MSI(pic->pic_dev, child, count, maxcount, &pdev, isrc);
13203fc155dcSAndrew Turner 	if (err == 0) {
13213fc155dcSAndrew Turner 		for (i = 0; i < count; i++) {
13223fc155dcSAndrew Turner 			irqs[i] = isrc[i]->isrc_irq;
13233fc155dcSAndrew Turner 		}
13243fc155dcSAndrew Turner 	}
13253fc155dcSAndrew Turner 
13263fc155dcSAndrew Turner 	free(isrc, M_INTRNG);
13273fc155dcSAndrew Turner 
13283fc155dcSAndrew Turner 	return (err);
13293fc155dcSAndrew Turner }
13303fc155dcSAndrew Turner 
13313fc155dcSAndrew Turner int
13323fc155dcSAndrew Turner intr_release_msi(device_t pci, device_t child, intptr_t xref, int count,
13333fc155dcSAndrew Turner     int *irqs)
13343fc155dcSAndrew Turner {
13353fc155dcSAndrew Turner 	struct intr_irqsrc **isrc;
13363fc155dcSAndrew Turner 	struct intr_pic *pic;
13373fc155dcSAndrew Turner 	int i, err;
13383fc155dcSAndrew Turner 
13393fc155dcSAndrew Turner 	pic = pic_lookup(NULL, xref);
13403fc155dcSAndrew Turner 	if (pic == NULL)
13413fc155dcSAndrew Turner 		return (ESRCH);
13423fc155dcSAndrew Turner 
13433fc155dcSAndrew Turner 	KASSERT((pic->pic_flags & FLAG_MSI) != 0,
13443fc155dcSAndrew Turner 	    ("%s: Found a non-MSI controller: %s", __func__,
13453fc155dcSAndrew Turner 	     device_get_name(pic->pic_dev)));
13463fc155dcSAndrew Turner 
13473fc155dcSAndrew Turner 	isrc = malloc(sizeof(*isrc) * count, M_INTRNG, M_WAITOK);
13483fc155dcSAndrew Turner 
13493fc155dcSAndrew Turner 	for (i = 0; i < count; i++) {
13503fc155dcSAndrew Turner 		isrc[i] = isrc_lookup(irqs[i]);
13513fc155dcSAndrew Turner 		if (isrc == NULL) {
13523fc155dcSAndrew Turner 			free(isrc, M_INTRNG);
13533fc155dcSAndrew Turner 			return (EINVAL);
13543fc155dcSAndrew Turner 		}
13553fc155dcSAndrew Turner 	}
13563fc155dcSAndrew Turner 
13573fc155dcSAndrew Turner 	err = MSI_RELEASE_MSI(pic->pic_dev, child, count, isrc);
13583fc155dcSAndrew Turner 	free(isrc, M_INTRNG);
13593fc155dcSAndrew Turner 	return (err);
13603fc155dcSAndrew Turner }
13613fc155dcSAndrew Turner 
13623fc155dcSAndrew Turner int
13633fc155dcSAndrew Turner intr_alloc_msix(device_t pci, device_t child, intptr_t xref, int *irq)
13643fc155dcSAndrew Turner {
13653fc155dcSAndrew Turner 	struct intr_irqsrc *isrc;
13663fc155dcSAndrew Turner 	struct intr_pic *pic;
13673fc155dcSAndrew Turner 	device_t pdev;
13683fc155dcSAndrew Turner 	int err;
13693fc155dcSAndrew Turner 
13703fc155dcSAndrew Turner 	pic = pic_lookup(NULL, xref);
13713fc155dcSAndrew Turner 	if (pic == NULL)
13723fc155dcSAndrew Turner 		return (ESRCH);
13733fc155dcSAndrew Turner 
13743fc155dcSAndrew Turner 	KASSERT((pic->pic_flags & FLAG_MSI) != 0,
13753fc155dcSAndrew Turner 	    ("%s: Found a non-MSI controller: %s", __func__,
13763fc155dcSAndrew Turner 	     device_get_name(pic->pic_dev)));
13773fc155dcSAndrew Turner 
13783fc155dcSAndrew Turner 	err = MSI_ALLOC_MSIX(pic->pic_dev, child, &pdev, &isrc);
13793fc155dcSAndrew Turner 	if (err != 0)
13803fc155dcSAndrew Turner 		return (err);
13813fc155dcSAndrew Turner 
13823fc155dcSAndrew Turner 	*irq = isrc->isrc_irq;
13833fc155dcSAndrew Turner 	return (0);
13843fc155dcSAndrew Turner }
13853fc155dcSAndrew Turner 
13863fc155dcSAndrew Turner int
13873fc155dcSAndrew Turner intr_release_msix(device_t pci, device_t child, intptr_t xref, int irq)
13883fc155dcSAndrew Turner {
13893fc155dcSAndrew Turner 	struct intr_irqsrc *isrc;
13903fc155dcSAndrew Turner 	struct intr_pic *pic;
13913fc155dcSAndrew Turner 	int err;
13923fc155dcSAndrew Turner 
13933fc155dcSAndrew Turner 	pic = pic_lookup(NULL, xref);
13943fc155dcSAndrew Turner 	if (pic == NULL)
13953fc155dcSAndrew Turner 		return (ESRCH);
13963fc155dcSAndrew Turner 
13973fc155dcSAndrew Turner 	KASSERT((pic->pic_flags & FLAG_MSI) != 0,
13983fc155dcSAndrew Turner 	    ("%s: Found a non-MSI controller: %s", __func__,
13993fc155dcSAndrew Turner 	     device_get_name(pic->pic_dev)));
14003fc155dcSAndrew Turner 
14013fc155dcSAndrew Turner 	isrc = isrc_lookup(irq);
14023fc155dcSAndrew Turner 	if (isrc == NULL)
14033fc155dcSAndrew Turner 		return (EINVAL);
14043fc155dcSAndrew Turner 
14053fc155dcSAndrew Turner 	err = MSI_RELEASE_MSIX(pic->pic_dev, child, isrc);
14063fc155dcSAndrew Turner 	return (err);
14073fc155dcSAndrew Turner }
14083fc155dcSAndrew Turner 
14093fc155dcSAndrew Turner int
14103fc155dcSAndrew Turner intr_map_msi(device_t pci, device_t child, intptr_t xref, int irq,
14113fc155dcSAndrew Turner     uint64_t *addr, uint32_t *data)
14123fc155dcSAndrew Turner {
14133fc155dcSAndrew Turner 	struct intr_irqsrc *isrc;
14143fc155dcSAndrew Turner 	struct intr_pic *pic;
14153fc155dcSAndrew Turner 	int err;
14163fc155dcSAndrew Turner 
14173fc155dcSAndrew Turner 	pic = pic_lookup(NULL, xref);
14183fc155dcSAndrew Turner 	if (pic == NULL)
14193fc155dcSAndrew Turner 		return (ESRCH);
14203fc155dcSAndrew Turner 
14213fc155dcSAndrew Turner 	KASSERT((pic->pic_flags & FLAG_MSI) != 0,
14223fc155dcSAndrew Turner 	    ("%s: Found a non-MSI controller: %s", __func__,
14233fc155dcSAndrew Turner 	     device_get_name(pic->pic_dev)));
14243fc155dcSAndrew Turner 
14253fc155dcSAndrew Turner 	isrc = isrc_lookup(irq);
14263fc155dcSAndrew Turner 	if (isrc == NULL)
14273fc155dcSAndrew Turner 		return (EINVAL);
14283fc155dcSAndrew Turner 
14293fc155dcSAndrew Turner 	err = MSI_MAP_MSI(pic->pic_dev, child, isrc, addr, data);
14303fc155dcSAndrew Turner 	return (err);
14313fc155dcSAndrew Turner }
14323fc155dcSAndrew Turner 
14333fc155dcSAndrew Turner 
14342b3ad188SAdrian Chadd void dosoftints(void);
14352b3ad188SAdrian Chadd void
14362b3ad188SAdrian Chadd dosoftints(void)
14372b3ad188SAdrian Chadd {
14382b3ad188SAdrian Chadd }
14392b3ad188SAdrian Chadd 
14402b3ad188SAdrian Chadd #ifdef SMP
14412b3ad188SAdrian Chadd /*
14422b3ad188SAdrian Chadd  *  Init interrupt controller on another CPU.
14432b3ad188SAdrian Chadd  */
14442b3ad188SAdrian Chadd void
14452b3ad188SAdrian Chadd intr_pic_init_secondary(void)
14462b3ad188SAdrian Chadd {
14472b3ad188SAdrian Chadd 
14482b3ad188SAdrian Chadd 	/*
14492b3ad188SAdrian Chadd 	 * QQQ: Only root PIC is aware of other CPUs ???
14502b3ad188SAdrian Chadd 	 */
14515b70c08cSSvatopluk Kraus 	KASSERT(intr_irq_root_dev != NULL, ("%s: no root attached", __func__));
14522b3ad188SAdrian Chadd 
14532b3ad188SAdrian Chadd 	//mtx_lock(&isrc_table_lock);
14545b70c08cSSvatopluk Kraus 	PIC_INIT_SECONDARY(intr_irq_root_dev);
14552b3ad188SAdrian Chadd 	//mtx_unlock(&isrc_table_lock);
14562b3ad188SAdrian Chadd }
14572b3ad188SAdrian Chadd #endif
14582b3ad188SAdrian Chadd 
14592b3ad188SAdrian Chadd #ifdef DDB
14602b3ad188SAdrian Chadd DB_SHOW_COMMAND(irqs, db_show_irqs)
14612b3ad188SAdrian Chadd {
14622b3ad188SAdrian Chadd 	u_int i, irqsum;
1463bff6be3eSSvatopluk Kraus 	u_long num;
14642b3ad188SAdrian Chadd 	struct intr_irqsrc *isrc;
14652b3ad188SAdrian Chadd 
14662b3ad188SAdrian Chadd 	for (irqsum = 0, i = 0; i < NIRQ; i++) {
14672b3ad188SAdrian Chadd 		isrc = irq_sources[i];
14682b3ad188SAdrian Chadd 		if (isrc == NULL)
14692b3ad188SAdrian Chadd 			continue;
14702b3ad188SAdrian Chadd 
1471bff6be3eSSvatopluk Kraus 		num = isrc->isrc_count != NULL ? isrc->isrc_count[0] : 0;
14722b3ad188SAdrian Chadd 		db_printf("irq%-3u <%s>: cpu %02lx%s cnt %lu\n", i,
14732b3ad188SAdrian Chadd 		    isrc->isrc_name, isrc->isrc_cpu.__bits[0],
1474bff6be3eSSvatopluk Kraus 		    isrc->isrc_flags & INTR_ISRCF_BOUND ? " (bound)" : "", num);
1475bff6be3eSSvatopluk Kraus 		irqsum += num;
14762b3ad188SAdrian Chadd 	}
14772b3ad188SAdrian Chadd 	db_printf("irq total %u\n", irqsum);
14782b3ad188SAdrian Chadd }
14792b3ad188SAdrian Chadd #endif
1480