xref: /freebsd/sys/kern/subr_intr.c (revision 4be58cba48cc944c9c122f9d4e8f50a05fc53370)
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"
732b3ad188SAdrian Chadd 
742b3ad188SAdrian Chadd #define	INTRNAME_LEN	(2*MAXCOMLEN + 1)
752b3ad188SAdrian Chadd 
762b3ad188SAdrian Chadd #ifdef DEBUG
772b3ad188SAdrian Chadd #define debugf(fmt, args...) do { printf("%s(): ", __func__);	\
782b3ad188SAdrian Chadd     printf(fmt,##args); } while (0)
792b3ad188SAdrian Chadd #else
802b3ad188SAdrian Chadd #define debugf(fmt, args...)
812b3ad188SAdrian Chadd #endif
822b3ad188SAdrian Chadd 
832b3ad188SAdrian Chadd MALLOC_DECLARE(M_INTRNG);
842b3ad188SAdrian Chadd MALLOC_DEFINE(M_INTRNG, "intr", "intr interrupt handling");
852b3ad188SAdrian Chadd 
862b3ad188SAdrian Chadd /* Main interrupt handler called from assembler -> 'hidden' for C code. */
872b3ad188SAdrian Chadd void intr_irq_handler(struct trapframe *tf);
882b3ad188SAdrian Chadd 
892b3ad188SAdrian Chadd /* Root interrupt controller stuff. */
905b70c08cSSvatopluk Kraus device_t intr_irq_root_dev;
912b3ad188SAdrian Chadd static intr_irq_filter_t *irq_root_filter;
922b3ad188SAdrian Chadd static void *irq_root_arg;
932b3ad188SAdrian Chadd static u_int irq_root_ipicount;
942b3ad188SAdrian Chadd 
952b3ad188SAdrian Chadd /* Interrupt controller definition. */
962b3ad188SAdrian Chadd struct intr_pic {
972b3ad188SAdrian Chadd 	SLIST_ENTRY(intr_pic)	pic_next;
982b3ad188SAdrian Chadd 	intptr_t		pic_xref;	/* hardware identification */
992b3ad188SAdrian Chadd 	device_t		pic_dev;
1002b3ad188SAdrian Chadd };
1012b3ad188SAdrian Chadd 
1022b3ad188SAdrian Chadd static struct mtx pic_list_lock;
1032b3ad188SAdrian Chadd static SLIST_HEAD(, intr_pic) pic_list;
1042b3ad188SAdrian Chadd 
1052b3ad188SAdrian Chadd static struct intr_pic *pic_lookup(device_t dev, intptr_t xref);
1062b3ad188SAdrian Chadd 
1072b3ad188SAdrian Chadd /* Interrupt source definition. */
1082b3ad188SAdrian Chadd static struct mtx isrc_table_lock;
1092b3ad188SAdrian Chadd static struct intr_irqsrc *irq_sources[NIRQ];
1102b3ad188SAdrian Chadd u_int irq_next_free;
1112b3ad188SAdrian Chadd 
1122b3ad188SAdrian Chadd #define IRQ_INVALID	nitems(irq_sources)
1132b3ad188SAdrian Chadd 
114bff6be3eSSvatopluk Kraus /*
115bff6be3eSSvatopluk Kraus  *  XXX - All stuff around struct intr_dev_data is considered as temporary
116bff6be3eSSvatopluk Kraus  *  until better place for storing struct intr_map_data will be find.
117bff6be3eSSvatopluk Kraus  *
118bff6be3eSSvatopluk Kraus  *  For now, there are two global interrupt numbers spaces:
119bff6be3eSSvatopluk Kraus  *  <0, NIRQ)                      ... interrupts without config data
120bff6be3eSSvatopluk Kraus  *                                     managed in irq_sources[]
121bff6be3eSSvatopluk Kraus  *  IRQ_DDATA_BASE + <0, 2 * NIRQ) ... interrupts with config data
122bff6be3eSSvatopluk Kraus  *                                     managed in intr_ddata_tab[]
123bff6be3eSSvatopluk Kraus  *
124bff6be3eSSvatopluk Kraus  *  Read intr_ddata_lookup() to see how these spaces are worked with.
125bff6be3eSSvatopluk Kraus  *  Note that each interrupt number from second space duplicates some number
126bff6be3eSSvatopluk Kraus  *  from first space at this moment. An interrupt number from first space can
127bff6be3eSSvatopluk Kraus  *  be duplicated even multiple times in second space.
128bff6be3eSSvatopluk Kraus  */
129bff6be3eSSvatopluk Kraus struct intr_dev_data {
130bff6be3eSSvatopluk Kraus 	device_t		idd_dev;
131bff6be3eSSvatopluk Kraus 	intptr_t		idd_xref;
132bff6be3eSSvatopluk Kraus 	u_int			idd_irq;
133bff6be3eSSvatopluk Kraus 	struct intr_map_data	idd_data;
134bff6be3eSSvatopluk Kraus 	struct intr_irqsrc *	idd_isrc;
135bff6be3eSSvatopluk Kraus };
136bff6be3eSSvatopluk Kraus 
137bff6be3eSSvatopluk Kraus static struct intr_dev_data *intr_ddata_tab[2 * NIRQ];
138bff6be3eSSvatopluk Kraus static u_int intr_ddata_first_unused;
139bff6be3eSSvatopluk Kraus 
140bff6be3eSSvatopluk Kraus #define IRQ_DDATA_BASE	10000
141bff6be3eSSvatopluk Kraus CTASSERT(IRQ_DDATA_BASE > IRQ_INVALID);
142bff6be3eSSvatopluk Kraus 
1432b3ad188SAdrian Chadd #ifdef SMP
1442b3ad188SAdrian Chadd static boolean_t irq_assign_cpu = FALSE;
1452b3ad188SAdrian Chadd #endif
1462b3ad188SAdrian Chadd 
1472b3ad188SAdrian Chadd /*
1482b3ad188SAdrian Chadd  * - 2 counters for each I/O interrupt.
1492b3ad188SAdrian Chadd  * - MAXCPU counters for each IPI counters for SMP.
1502b3ad188SAdrian Chadd  */
1512b3ad188SAdrian Chadd #ifdef SMP
1522b3ad188SAdrian Chadd #define INTRCNT_COUNT   (NIRQ * 2 + INTR_IPI_COUNT * MAXCPU)
1532b3ad188SAdrian Chadd #else
1542b3ad188SAdrian Chadd #define INTRCNT_COUNT   (NIRQ * 2)
1552b3ad188SAdrian Chadd #endif
1562b3ad188SAdrian Chadd 
1572b3ad188SAdrian Chadd /* Data for MI statistics reporting. */
1582b3ad188SAdrian Chadd u_long intrcnt[INTRCNT_COUNT];
1592b3ad188SAdrian Chadd char intrnames[INTRCNT_COUNT * INTRNAME_LEN];
1602b3ad188SAdrian Chadd size_t sintrcnt = sizeof(intrcnt);
1612b3ad188SAdrian Chadd size_t sintrnames = sizeof(intrnames);
1622b3ad188SAdrian Chadd static u_int intrcnt_index;
1632b3ad188SAdrian Chadd 
1642b3ad188SAdrian Chadd /*
1652b3ad188SAdrian Chadd  *  Interrupt framework initialization routine.
1662b3ad188SAdrian Chadd  */
1672b3ad188SAdrian Chadd static void
1682b3ad188SAdrian Chadd intr_irq_init(void *dummy __unused)
1692b3ad188SAdrian Chadd {
1702b3ad188SAdrian Chadd 
1712b3ad188SAdrian Chadd 	SLIST_INIT(&pic_list);
1722b3ad188SAdrian Chadd 	mtx_init(&pic_list_lock, "intr pic list", NULL, MTX_DEF);
1732b3ad188SAdrian Chadd 	mtx_init(&isrc_table_lock, "intr isrc table", NULL, MTX_DEF);
1742b3ad188SAdrian Chadd }
1752b3ad188SAdrian Chadd SYSINIT(intr_irq_init, SI_SUB_INTR, SI_ORDER_FIRST, intr_irq_init, NULL);
1762b3ad188SAdrian Chadd 
1772b3ad188SAdrian Chadd static void
1782b3ad188SAdrian Chadd intrcnt_setname(const char *name, int index)
1792b3ad188SAdrian Chadd {
1802b3ad188SAdrian Chadd 
1812b3ad188SAdrian Chadd 	snprintf(intrnames + INTRNAME_LEN * index, INTRNAME_LEN, "%-*s",
1822b3ad188SAdrian Chadd 	    INTRNAME_LEN - 1, name);
1832b3ad188SAdrian Chadd }
1842b3ad188SAdrian Chadd 
1852b3ad188SAdrian Chadd /*
1862b3ad188SAdrian Chadd  *  Update name for interrupt source with interrupt event.
1872b3ad188SAdrian Chadd  */
1882b3ad188SAdrian Chadd static void
1892b3ad188SAdrian Chadd intrcnt_updatename(struct intr_irqsrc *isrc)
1902b3ad188SAdrian Chadd {
1912b3ad188SAdrian Chadd 
1922b3ad188SAdrian Chadd 	/* QQQ: What about stray counter name? */
1932b3ad188SAdrian Chadd 	mtx_assert(&isrc_table_lock, MA_OWNED);
1942b3ad188SAdrian Chadd 	intrcnt_setname(isrc->isrc_event->ie_fullname, isrc->isrc_index);
1952b3ad188SAdrian Chadd }
1962b3ad188SAdrian Chadd 
1972b3ad188SAdrian Chadd /*
1982b3ad188SAdrian Chadd  *  Virtualization for interrupt source interrupt counter increment.
1992b3ad188SAdrian Chadd  */
2002b3ad188SAdrian Chadd static inline void
2012b3ad188SAdrian Chadd isrc_increment_count(struct intr_irqsrc *isrc)
2022b3ad188SAdrian Chadd {
2032b3ad188SAdrian Chadd 
204bff6be3eSSvatopluk Kraus 	if (isrc->isrc_flags & INTR_ISRCF_PPI)
205bff6be3eSSvatopluk Kraus 		atomic_add_long(&isrc->isrc_count[0], 1);
206bff6be3eSSvatopluk Kraus 	else
2072b3ad188SAdrian Chadd 		isrc->isrc_count[0]++;
2082b3ad188SAdrian Chadd }
2092b3ad188SAdrian Chadd 
2102b3ad188SAdrian Chadd /*
2112b3ad188SAdrian Chadd  *  Virtualization for interrupt source interrupt stray counter increment.
2122b3ad188SAdrian Chadd  */
2132b3ad188SAdrian Chadd static inline void
2142b3ad188SAdrian Chadd isrc_increment_straycount(struct intr_irqsrc *isrc)
2152b3ad188SAdrian Chadd {
2162b3ad188SAdrian Chadd 
2172b3ad188SAdrian Chadd 	isrc->isrc_count[1]++;
2182b3ad188SAdrian Chadd }
2192b3ad188SAdrian Chadd 
2202b3ad188SAdrian Chadd /*
2212b3ad188SAdrian Chadd  *  Virtualization for interrupt source interrupt name update.
2222b3ad188SAdrian Chadd  */
2232b3ad188SAdrian Chadd static void
2242b3ad188SAdrian Chadd isrc_update_name(struct intr_irqsrc *isrc, const char *name)
2252b3ad188SAdrian Chadd {
2262b3ad188SAdrian Chadd 	char str[INTRNAME_LEN];
2272b3ad188SAdrian Chadd 
2282b3ad188SAdrian Chadd 	mtx_assert(&isrc_table_lock, MA_OWNED);
2292b3ad188SAdrian Chadd 
2302b3ad188SAdrian Chadd 	if (name != NULL) {
2312b3ad188SAdrian Chadd 		snprintf(str, INTRNAME_LEN, "%s: %s", isrc->isrc_name, name);
2322b3ad188SAdrian Chadd 		intrcnt_setname(str, isrc->isrc_index);
2332b3ad188SAdrian Chadd 		snprintf(str, INTRNAME_LEN, "stray %s: %s", isrc->isrc_name,
2342b3ad188SAdrian Chadd 		    name);
2352b3ad188SAdrian Chadd 		intrcnt_setname(str, isrc->isrc_index + 1);
2362b3ad188SAdrian Chadd 	} else {
2372b3ad188SAdrian Chadd 		snprintf(str, INTRNAME_LEN, "%s:", isrc->isrc_name);
2382b3ad188SAdrian Chadd 		intrcnt_setname(str, isrc->isrc_index);
2392b3ad188SAdrian Chadd 		snprintf(str, INTRNAME_LEN, "stray %s:", isrc->isrc_name);
2402b3ad188SAdrian Chadd 		intrcnt_setname(str, isrc->isrc_index + 1);
2412b3ad188SAdrian Chadd 	}
2422b3ad188SAdrian Chadd }
2432b3ad188SAdrian Chadd 
2442b3ad188SAdrian Chadd /*
2452b3ad188SAdrian Chadd  *  Virtualization for interrupt source interrupt counters setup.
2462b3ad188SAdrian Chadd  */
2472b3ad188SAdrian Chadd static void
2482b3ad188SAdrian Chadd isrc_setup_counters(struct intr_irqsrc *isrc)
2492b3ad188SAdrian Chadd {
2502b3ad188SAdrian Chadd 	u_int index;
2512b3ad188SAdrian Chadd 
2522b3ad188SAdrian Chadd 	/*
2532b3ad188SAdrian Chadd 	 *  XXX - it does not work well with removable controllers and
2542b3ad188SAdrian Chadd 	 *        interrupt sources !!!
2552b3ad188SAdrian Chadd 	 */
2562b3ad188SAdrian Chadd 	index = atomic_fetchadd_int(&intrcnt_index, 2);
2572b3ad188SAdrian Chadd 	isrc->isrc_index = index;
2582b3ad188SAdrian Chadd 	isrc->isrc_count = &intrcnt[index];
2592b3ad188SAdrian Chadd 	isrc_update_name(isrc, NULL);
2602b3ad188SAdrian Chadd }
2612b3ad188SAdrian Chadd 
262bff6be3eSSvatopluk Kraus /*
263bff6be3eSSvatopluk Kraus  *  Virtualization for interrupt source interrupt counters release.
264bff6be3eSSvatopluk Kraus  */
265bff6be3eSSvatopluk Kraus static void
266bff6be3eSSvatopluk Kraus isrc_release_counters(struct intr_irqsrc *isrc)
267bff6be3eSSvatopluk Kraus {
268bff6be3eSSvatopluk Kraus 
269bff6be3eSSvatopluk Kraus 	panic("%s: not implemented", __func__);
270bff6be3eSSvatopluk Kraus }
271bff6be3eSSvatopluk Kraus 
2722b3ad188SAdrian Chadd #ifdef SMP
2732b3ad188SAdrian Chadd /*
2742b3ad188SAdrian Chadd  *  Virtualization for interrupt source IPI counters setup.
2752b3ad188SAdrian Chadd  */
2765b70c08cSSvatopluk Kraus u_long *
2775b70c08cSSvatopluk Kraus intr_ipi_setup_counters(const char *name)
2782b3ad188SAdrian Chadd {
2792b3ad188SAdrian Chadd 	u_int index, i;
2802b3ad188SAdrian Chadd 	char str[INTRNAME_LEN];
2812b3ad188SAdrian Chadd 
2822b3ad188SAdrian Chadd 	index = atomic_fetchadd_int(&intrcnt_index, MAXCPU);
2832b3ad188SAdrian Chadd 	for (i = 0; i < MAXCPU; i++) {
2842b3ad188SAdrian Chadd 		snprintf(str, INTRNAME_LEN, "cpu%d:%s", i, name);
2852b3ad188SAdrian Chadd 		intrcnt_setname(str, index + i);
2862b3ad188SAdrian Chadd 	}
2875b70c08cSSvatopluk Kraus 	return (&intrcnt[index]);
2882b3ad188SAdrian Chadd }
2892b3ad188SAdrian Chadd #endif
2902b3ad188SAdrian Chadd 
2912b3ad188SAdrian Chadd /*
2922b3ad188SAdrian Chadd  *  Main interrupt dispatch handler. It's called straight
2932b3ad188SAdrian Chadd  *  from the assembler, where CPU interrupt is served.
2942b3ad188SAdrian Chadd  */
2952b3ad188SAdrian Chadd void
2962b3ad188SAdrian Chadd intr_irq_handler(struct trapframe *tf)
2972b3ad188SAdrian Chadd {
2982b3ad188SAdrian Chadd 	struct trapframe * oldframe;
2992b3ad188SAdrian Chadd 	struct thread * td;
3002b3ad188SAdrian Chadd 
3012b3ad188SAdrian Chadd 	KASSERT(irq_root_filter != NULL, ("%s: no filter", __func__));
3022b3ad188SAdrian Chadd 
3032b3ad188SAdrian Chadd 	PCPU_INC(cnt.v_intr);
3042b3ad188SAdrian Chadd 	critical_enter();
3052b3ad188SAdrian Chadd 	td = curthread;
3062b3ad188SAdrian Chadd 	oldframe = td->td_intr_frame;
3072b3ad188SAdrian Chadd 	td->td_intr_frame = tf;
3082b3ad188SAdrian Chadd 	irq_root_filter(irq_root_arg);
3092b3ad188SAdrian Chadd 	td->td_intr_frame = oldframe;
3102b3ad188SAdrian Chadd 	critical_exit();
3112b3ad188SAdrian Chadd }
3122b3ad188SAdrian Chadd 
3132b3ad188SAdrian Chadd /*
3142b3ad188SAdrian Chadd  *  interrupt controller dispatch function for interrupts. It should
3152b3ad188SAdrian Chadd  *  be called straight from the interrupt controller, when associated interrupt
3162b3ad188SAdrian Chadd  *  source is learned.
3172b3ad188SAdrian Chadd  */
318bff6be3eSSvatopluk Kraus int
319bff6be3eSSvatopluk Kraus intr_isrc_dispatch(struct intr_irqsrc *isrc, struct trapframe *tf)
3202b3ad188SAdrian Chadd {
3212b3ad188SAdrian Chadd 
3222b3ad188SAdrian Chadd 	KASSERT(isrc != NULL, ("%s: no source", __func__));
3232b3ad188SAdrian Chadd 
3242b3ad188SAdrian Chadd 	isrc_increment_count(isrc);
3252b3ad188SAdrian Chadd 
3262b3ad188SAdrian Chadd #ifdef INTR_SOLO
3272b3ad188SAdrian Chadd 	if (isrc->isrc_filter != NULL) {
3282b3ad188SAdrian Chadd 		int error;
3292b3ad188SAdrian Chadd 		error = isrc->isrc_filter(isrc->isrc_arg, tf);
3302b3ad188SAdrian Chadd 		PIC_POST_FILTER(isrc->isrc_dev, isrc);
3312b3ad188SAdrian Chadd 		if (error == FILTER_HANDLED)
332bff6be3eSSvatopluk Kraus 			return (0);
3332b3ad188SAdrian Chadd 	} else
3342b3ad188SAdrian Chadd #endif
3352b3ad188SAdrian Chadd 	if (isrc->isrc_event != NULL) {
3362b3ad188SAdrian Chadd 		if (intr_event_handle(isrc->isrc_event, tf) == 0)
337bff6be3eSSvatopluk Kraus 			return (0);
3382b3ad188SAdrian Chadd 	}
3392b3ad188SAdrian Chadd 
3402b3ad188SAdrian Chadd 	isrc_increment_straycount(isrc);
341bff6be3eSSvatopluk Kraus 	return (EINVAL);
3422b3ad188SAdrian Chadd }
3432b3ad188SAdrian Chadd 
3442b3ad188SAdrian Chadd /*
3452b3ad188SAdrian Chadd  *  Alloc unique interrupt number (resource handle) for interrupt source.
3462b3ad188SAdrian Chadd  *
3472b3ad188SAdrian Chadd  *  There could be various strategies how to allocate free interrupt number
3482b3ad188SAdrian Chadd  *  (resource handle) for new interrupt source.
3492b3ad188SAdrian Chadd  *
3502b3ad188SAdrian Chadd  *  1. Handles are always allocated forward, so handles are not recycled
3512b3ad188SAdrian Chadd  *     immediately. However, if only one free handle left which is reused
3522b3ad188SAdrian Chadd  *     constantly...
3532b3ad188SAdrian Chadd  */
354bff6be3eSSvatopluk Kraus static inline int
355bff6be3eSSvatopluk Kraus isrc_alloc_irq(struct intr_irqsrc *isrc)
3562b3ad188SAdrian Chadd {
3572b3ad188SAdrian Chadd 	u_int maxirqs, irq;
3582b3ad188SAdrian Chadd 
3592b3ad188SAdrian Chadd 	mtx_assert(&isrc_table_lock, MA_OWNED);
3602b3ad188SAdrian Chadd 
3612b3ad188SAdrian Chadd 	maxirqs = nitems(irq_sources);
3622b3ad188SAdrian Chadd 	if (irq_next_free >= maxirqs)
3632b3ad188SAdrian Chadd 		return (ENOSPC);
3642b3ad188SAdrian Chadd 
3652b3ad188SAdrian Chadd 	for (irq = irq_next_free; irq < maxirqs; irq++) {
3662b3ad188SAdrian Chadd 		if (irq_sources[irq] == NULL)
3672b3ad188SAdrian Chadd 			goto found;
3682b3ad188SAdrian Chadd 	}
3692b3ad188SAdrian Chadd 	for (irq = 0; irq < irq_next_free; irq++) {
3702b3ad188SAdrian Chadd 		if (irq_sources[irq] == NULL)
3712b3ad188SAdrian Chadd 			goto found;
3722b3ad188SAdrian Chadd 	}
3732b3ad188SAdrian Chadd 
3742b3ad188SAdrian Chadd 	irq_next_free = maxirqs;
3752b3ad188SAdrian Chadd 	return (ENOSPC);
3762b3ad188SAdrian Chadd 
3772b3ad188SAdrian Chadd found:
3782b3ad188SAdrian Chadd 	isrc->isrc_irq = irq;
3792b3ad188SAdrian Chadd 	irq_sources[irq] = isrc;
3802b3ad188SAdrian Chadd 
3812b3ad188SAdrian Chadd 	irq_next_free = irq + 1;
3822b3ad188SAdrian Chadd 	if (irq_next_free >= maxirqs)
3832b3ad188SAdrian Chadd 		irq_next_free = 0;
3842b3ad188SAdrian Chadd 	return (0);
3852b3ad188SAdrian Chadd }
386bff6be3eSSvatopluk Kraus 
3872b3ad188SAdrian Chadd /*
3882b3ad188SAdrian Chadd  *  Free unique interrupt number (resource handle) from interrupt source.
3892b3ad188SAdrian Chadd  */
390bff6be3eSSvatopluk Kraus static inline int
3912b3ad188SAdrian Chadd isrc_free_irq(struct intr_irqsrc *isrc)
3922b3ad188SAdrian Chadd {
3932b3ad188SAdrian Chadd 
394bff6be3eSSvatopluk Kraus 	mtx_assert(&isrc_table_lock, MA_OWNED);
3952b3ad188SAdrian Chadd 
396bff6be3eSSvatopluk Kraus 	if (isrc->isrc_irq >= nitems(irq_sources))
3972b3ad188SAdrian Chadd 		return (EINVAL);
398bff6be3eSSvatopluk Kraus 	if (irq_sources[isrc->isrc_irq] != isrc)
3992b3ad188SAdrian Chadd 		return (EINVAL);
4002b3ad188SAdrian Chadd 
4012b3ad188SAdrian Chadd 	irq_sources[isrc->isrc_irq] = NULL;
4022b3ad188SAdrian Chadd 	isrc->isrc_irq = IRQ_INVALID;	/* just to be safe */
4032b3ad188SAdrian Chadd 	return (0);
4042b3ad188SAdrian Chadd }
405bff6be3eSSvatopluk Kraus 
4062b3ad188SAdrian Chadd /*
4072b3ad188SAdrian Chadd  *  Lookup interrupt source by interrupt number (resource handle).
4082b3ad188SAdrian Chadd  */
409bff6be3eSSvatopluk Kraus static inline struct intr_irqsrc *
4102b3ad188SAdrian Chadd isrc_lookup(u_int irq)
4112b3ad188SAdrian Chadd {
4122b3ad188SAdrian Chadd 
4132b3ad188SAdrian Chadd 	if (irq < nitems(irq_sources))
4142b3ad188SAdrian Chadd 		return (irq_sources[irq]);
4152b3ad188SAdrian Chadd 	return (NULL);
4162b3ad188SAdrian Chadd }
4172b3ad188SAdrian Chadd 
4182b3ad188SAdrian Chadd /*
419bff6be3eSSvatopluk Kraus  *  Initialize interrupt source and register it into global interrupt table.
4202b3ad188SAdrian Chadd  */
421bff6be3eSSvatopluk Kraus int
422bff6be3eSSvatopluk Kraus intr_isrc_register(struct intr_irqsrc *isrc, device_t dev, u_int flags,
423bff6be3eSSvatopluk Kraus     const char *fmt, ...)
4242b3ad188SAdrian Chadd {
425bff6be3eSSvatopluk Kraus 	int error;
426bff6be3eSSvatopluk Kraus 	va_list ap;
4272b3ad188SAdrian Chadd 
428bff6be3eSSvatopluk Kraus 	bzero(isrc, sizeof(struct intr_irqsrc));
429bff6be3eSSvatopluk Kraus 	isrc->isrc_dev = dev;
430bff6be3eSSvatopluk Kraus 	isrc->isrc_irq = IRQ_INVALID;	/* just to be safe */
431bff6be3eSSvatopluk Kraus 	isrc->isrc_flags = flags;
4322b3ad188SAdrian Chadd 
433bff6be3eSSvatopluk Kraus 	va_start(ap, fmt);
434bff6be3eSSvatopluk Kraus 	vsnprintf(isrc->isrc_name, INTR_ISRC_NAMELEN, fmt, ap);
435bff6be3eSSvatopluk Kraus 	va_end(ap);
436bff6be3eSSvatopluk Kraus 
437bff6be3eSSvatopluk Kraus 	mtx_lock(&isrc_table_lock);
438bff6be3eSSvatopluk Kraus 	error = isrc_alloc_irq(isrc);
439bff6be3eSSvatopluk Kraus 	if (error != 0) {
440bff6be3eSSvatopluk Kraus 		mtx_unlock(&isrc_table_lock);
441bff6be3eSSvatopluk Kraus 		return (error);
4422b3ad188SAdrian Chadd 	}
443bff6be3eSSvatopluk Kraus 	/*
444bff6be3eSSvatopluk Kraus 	 * Setup interrupt counters, but not for IPI sources. Those are setup
445bff6be3eSSvatopluk Kraus 	 * later and only for used ones (up to INTR_IPI_COUNT) to not exhaust
446bff6be3eSSvatopluk Kraus 	 * our counter pool.
447bff6be3eSSvatopluk Kraus 	 */
448bff6be3eSSvatopluk Kraus 	if ((isrc->isrc_flags & INTR_ISRCF_IPI) == 0)
449bff6be3eSSvatopluk Kraus 		isrc_setup_counters(isrc);
450bff6be3eSSvatopluk Kraus 	mtx_unlock(&isrc_table_lock);
451bff6be3eSSvatopluk Kraus 	return (0);
4522b3ad188SAdrian Chadd }
4532b3ad188SAdrian Chadd 
4542b3ad188SAdrian Chadd /*
455bff6be3eSSvatopluk Kraus  *  Deregister interrupt source from global interrupt table.
456bff6be3eSSvatopluk Kraus  */
457bff6be3eSSvatopluk Kraus int
458bff6be3eSSvatopluk Kraus intr_isrc_deregister(struct intr_irqsrc *isrc)
459bff6be3eSSvatopluk Kraus {
460bff6be3eSSvatopluk Kraus 	int error;
461bff6be3eSSvatopluk Kraus 
462bff6be3eSSvatopluk Kraus 	mtx_lock(&isrc_table_lock);
463bff6be3eSSvatopluk Kraus 	if ((isrc->isrc_flags & INTR_ISRCF_IPI) == 0)
464bff6be3eSSvatopluk Kraus 		isrc_release_counters(isrc);
465bff6be3eSSvatopluk Kraus 	error = isrc_free_irq(isrc);
466bff6be3eSSvatopluk Kraus 	mtx_unlock(&isrc_table_lock);
467bff6be3eSSvatopluk Kraus 	return (error);
468bff6be3eSSvatopluk Kraus }
469bff6be3eSSvatopluk Kraus 
470bff6be3eSSvatopluk Kraus static struct intr_dev_data *
471bff6be3eSSvatopluk Kraus intr_ddata_alloc(u_int extsize)
472bff6be3eSSvatopluk Kraus {
473bff6be3eSSvatopluk Kraus 	struct intr_dev_data *ddata;
474bff6be3eSSvatopluk Kraus 
475bff6be3eSSvatopluk Kraus 	ddata = malloc(sizeof(*ddata) + extsize, M_INTRNG, M_WAITOK | M_ZERO);
476bff6be3eSSvatopluk Kraus 
477bff6be3eSSvatopluk Kraus 	mtx_lock(&isrc_table_lock);
478bff6be3eSSvatopluk Kraus 	if (intr_ddata_first_unused >= nitems(intr_ddata_tab)) {
479bff6be3eSSvatopluk Kraus 		mtx_unlock(&isrc_table_lock);
480bff6be3eSSvatopluk Kraus 		free(ddata, M_INTRNG);
481bff6be3eSSvatopluk Kraus 		return (NULL);
482bff6be3eSSvatopluk Kraus 	}
483bff6be3eSSvatopluk Kraus 	intr_ddata_tab[intr_ddata_first_unused] = ddata;
484bff6be3eSSvatopluk Kraus 	ddata->idd_irq = IRQ_DDATA_BASE + intr_ddata_first_unused++;
485bff6be3eSSvatopluk Kraus 	mtx_unlock(&isrc_table_lock);
486bff6be3eSSvatopluk Kraus 	return (ddata);
487bff6be3eSSvatopluk Kraus }
488bff6be3eSSvatopluk Kraus 
489bff6be3eSSvatopluk Kraus static struct intr_irqsrc *
490bff6be3eSSvatopluk Kraus intr_ddata_lookup(u_int irq, struct intr_map_data **datap)
491bff6be3eSSvatopluk Kraus {
492bff6be3eSSvatopluk Kraus 	int error;
493bff6be3eSSvatopluk Kraus 	struct intr_irqsrc *isrc;
494bff6be3eSSvatopluk Kraus 	struct intr_dev_data *ddata;
495bff6be3eSSvatopluk Kraus 
496bff6be3eSSvatopluk Kraus 	isrc = isrc_lookup(irq);
497bff6be3eSSvatopluk Kraus 	if (isrc != NULL) {
498bff6be3eSSvatopluk Kraus 		if (datap != NULL)
499bff6be3eSSvatopluk Kraus 			*datap = NULL;
500bff6be3eSSvatopluk Kraus 		return (isrc);
501bff6be3eSSvatopluk Kraus 	}
502bff6be3eSSvatopluk Kraus 
503bff6be3eSSvatopluk Kraus 	if (irq < IRQ_DDATA_BASE)
504bff6be3eSSvatopluk Kraus 		return (NULL);
505bff6be3eSSvatopluk Kraus 
506bff6be3eSSvatopluk Kraus 	irq -= IRQ_DDATA_BASE;
507bff6be3eSSvatopluk Kraus 	if (irq >= nitems(intr_ddata_tab))
508bff6be3eSSvatopluk Kraus 		return (NULL);
509bff6be3eSSvatopluk Kraus 
510bff6be3eSSvatopluk Kraus 	ddata = intr_ddata_tab[irq];
511bff6be3eSSvatopluk Kraus 	if (ddata->idd_isrc == NULL) {
512bff6be3eSSvatopluk Kraus 		error = intr_map_irq(ddata->idd_dev, ddata->idd_xref,
513bff6be3eSSvatopluk Kraus 		    &ddata->idd_data, &irq);
514bff6be3eSSvatopluk Kraus 		if (error != 0)
515bff6be3eSSvatopluk Kraus 			return (NULL);
516bff6be3eSSvatopluk Kraus 		ddata->idd_isrc = isrc_lookup(irq);
517bff6be3eSSvatopluk Kraus 	}
518bff6be3eSSvatopluk Kraus 	if (datap != NULL)
519bff6be3eSSvatopluk Kraus 		*datap = &ddata->idd_data;
520bff6be3eSSvatopluk Kraus 	return (ddata->idd_isrc);
521bff6be3eSSvatopluk Kraus }
522bff6be3eSSvatopluk Kraus 
523bff6be3eSSvatopluk Kraus #ifdef DEV_ACPI
524bff6be3eSSvatopluk Kraus /*
525bff6be3eSSvatopluk Kraus  *  Map interrupt source according to ACPI info into framework. If such mapping
5262b3ad188SAdrian Chadd  *  does not exist, create it. Return unique interrupt number (resource handle)
5272b3ad188SAdrian Chadd  *  associated with mapped interrupt source.
5282b3ad188SAdrian Chadd  */
5292b3ad188SAdrian Chadd u_int
530bff6be3eSSvatopluk Kraus intr_acpi_map_irq(device_t dev, u_int irq, enum intr_polarity pol,
531bff6be3eSSvatopluk Kraus     enum intr_trigger trig)
5322b3ad188SAdrian Chadd {
533bff6be3eSSvatopluk Kraus 	struct intr_dev_data *ddata;
5342b3ad188SAdrian Chadd 
535bff6be3eSSvatopluk Kraus 	ddata = intr_ddata_alloc(0);
536bff6be3eSSvatopluk Kraus 	if (ddata == NULL)
537bff6be3eSSvatopluk Kraus 		return (0xFFFFFFFF);	/* no space left */
5382b3ad188SAdrian Chadd 
539bff6be3eSSvatopluk Kraus 	ddata->idd_dev = dev;
540bff6be3eSSvatopluk Kraus 	ddata->idd_data.type = INTR_MAP_DATA_ACPI;
541bff6be3eSSvatopluk Kraus 	ddata->idd_data.acpi.irq = irq;
542bff6be3eSSvatopluk Kraus 	ddata->idd_data.acpi.pol = pol;
543bff6be3eSSvatopluk Kraus 	ddata->idd_data.acpi.trig = trig;
544bff6be3eSSvatopluk Kraus 	return (ddata->idd_irq);
5452b3ad188SAdrian Chadd }
546bff6be3eSSvatopluk Kraus #endif
5472b3ad188SAdrian Chadd #ifdef FDT
5482b3ad188SAdrian Chadd /*
5492b3ad188SAdrian Chadd  *  Map interrupt source according to FDT data into framework. If such mapping
5502b3ad188SAdrian Chadd  *  does not exist, create it. Return unique interrupt number (resource handle)
5512b3ad188SAdrian Chadd  *  associated with mapped interrupt source.
5522b3ad188SAdrian Chadd  */
5532b3ad188SAdrian Chadd u_int
5542b3ad188SAdrian Chadd intr_fdt_map_irq(phandle_t node, pcell_t *cells, u_int ncells)
5552b3ad188SAdrian Chadd {
556bff6be3eSSvatopluk Kraus 	struct intr_dev_data *ddata;
5572b3ad188SAdrian Chadd 	u_int cellsize;
5582b3ad188SAdrian Chadd 
5592b3ad188SAdrian Chadd 	cellsize = ncells * sizeof(*cells);
560bff6be3eSSvatopluk Kraus 	ddata = intr_ddata_alloc(cellsize);
561bff6be3eSSvatopluk Kraus 	if (ddata == NULL)
562bff6be3eSSvatopluk Kraus 		return (0xFFFFFFFF);	/* no space left */
5632b3ad188SAdrian Chadd 
564bff6be3eSSvatopluk Kraus 	ddata->idd_xref = (intptr_t)node;
565bff6be3eSSvatopluk Kraus 	ddata->idd_data.type = INTR_MAP_DATA_FDT;
566bff6be3eSSvatopluk Kraus 	ddata->idd_data.fdt.ncells = ncells;
567bff6be3eSSvatopluk Kraus 	ddata->idd_data.fdt.cells = (pcell_t *)(ddata + 1);
568bff6be3eSSvatopluk Kraus 	memcpy(ddata->idd_data.fdt.cells, cells, cellsize);
569bff6be3eSSvatopluk Kraus 	return (ddata->idd_irq);
5702b3ad188SAdrian Chadd }
5712b3ad188SAdrian Chadd #endif
5722b3ad188SAdrian Chadd 
5732b3ad188SAdrian Chadd #ifdef INTR_SOLO
5742b3ad188SAdrian Chadd /*
5752b3ad188SAdrian Chadd  *  Setup filter into interrupt source.
5762b3ad188SAdrian Chadd  */
5772b3ad188SAdrian Chadd static int
5782b3ad188SAdrian Chadd iscr_setup_filter(struct intr_irqsrc *isrc, const char *name,
5792b3ad188SAdrian Chadd     intr_irq_filter_t *filter, void *arg, void **cookiep)
5802b3ad188SAdrian Chadd {
5812b3ad188SAdrian Chadd 
5822b3ad188SAdrian Chadd 	if (filter == NULL)
5832b3ad188SAdrian Chadd 		return (EINVAL);
5842b3ad188SAdrian Chadd 
5852b3ad188SAdrian Chadd 	mtx_lock(&isrc_table_lock);
5862b3ad188SAdrian Chadd 	/*
5872b3ad188SAdrian Chadd 	 * Make sure that we do not mix the two ways
5882b3ad188SAdrian Chadd 	 * how we handle interrupt sources.
5892b3ad188SAdrian Chadd 	 */
5902b3ad188SAdrian Chadd 	if (isrc->isrc_filter != NULL || isrc->isrc_event != NULL) {
5912b3ad188SAdrian Chadd 		mtx_unlock(&isrc_table_lock);
5922b3ad188SAdrian Chadd 		return (EBUSY);
5932b3ad188SAdrian Chadd 	}
5942b3ad188SAdrian Chadd 	isrc->isrc_filter = filter;
5952b3ad188SAdrian Chadd 	isrc->isrc_arg = arg;
5962b3ad188SAdrian Chadd 	isrc_update_name(isrc, name);
5972b3ad188SAdrian Chadd 	mtx_unlock(&isrc_table_lock);
5982b3ad188SAdrian Chadd 
5992b3ad188SAdrian Chadd 	*cookiep = isrc;
6002b3ad188SAdrian Chadd 	return (0);
6012b3ad188SAdrian Chadd }
6022b3ad188SAdrian Chadd #endif
6032b3ad188SAdrian Chadd 
6042b3ad188SAdrian Chadd /*
6052b3ad188SAdrian Chadd  *  Interrupt source pre_ithread method for MI interrupt framework.
6062b3ad188SAdrian Chadd  */
6072b3ad188SAdrian Chadd static void
6082b3ad188SAdrian Chadd intr_isrc_pre_ithread(void *arg)
6092b3ad188SAdrian Chadd {
6102b3ad188SAdrian Chadd 	struct intr_irqsrc *isrc = arg;
6112b3ad188SAdrian Chadd 
6122b3ad188SAdrian Chadd 	PIC_PRE_ITHREAD(isrc->isrc_dev, isrc);
6132b3ad188SAdrian Chadd }
6142b3ad188SAdrian Chadd 
6152b3ad188SAdrian Chadd /*
6162b3ad188SAdrian Chadd  *  Interrupt source post_ithread method for MI interrupt framework.
6172b3ad188SAdrian Chadd  */
6182b3ad188SAdrian Chadd static void
6192b3ad188SAdrian Chadd intr_isrc_post_ithread(void *arg)
6202b3ad188SAdrian Chadd {
6212b3ad188SAdrian Chadd 	struct intr_irqsrc *isrc = arg;
6222b3ad188SAdrian Chadd 
6232b3ad188SAdrian Chadd 	PIC_POST_ITHREAD(isrc->isrc_dev, isrc);
6242b3ad188SAdrian Chadd }
6252b3ad188SAdrian Chadd 
6262b3ad188SAdrian Chadd /*
6272b3ad188SAdrian Chadd  *  Interrupt source post_filter method for MI interrupt framework.
6282b3ad188SAdrian Chadd  */
6292b3ad188SAdrian Chadd static void
6302b3ad188SAdrian Chadd intr_isrc_post_filter(void *arg)
6312b3ad188SAdrian Chadd {
6322b3ad188SAdrian Chadd 	struct intr_irqsrc *isrc = arg;
6332b3ad188SAdrian Chadd 
6342b3ad188SAdrian Chadd 	PIC_POST_FILTER(isrc->isrc_dev, isrc);
6352b3ad188SAdrian Chadd }
6362b3ad188SAdrian Chadd 
6372b3ad188SAdrian Chadd /*
6382b3ad188SAdrian Chadd  *  Interrupt source assign_cpu method for MI interrupt framework.
6392b3ad188SAdrian Chadd  */
6402b3ad188SAdrian Chadd static int
6412b3ad188SAdrian Chadd intr_isrc_assign_cpu(void *arg, int cpu)
6422b3ad188SAdrian Chadd {
6432b3ad188SAdrian Chadd #ifdef SMP
6442b3ad188SAdrian Chadd 	struct intr_irqsrc *isrc = arg;
6452b3ad188SAdrian Chadd 	int error;
6462b3ad188SAdrian Chadd 
6475b70c08cSSvatopluk Kraus 	if (isrc->isrc_dev != intr_irq_root_dev)
6482b3ad188SAdrian Chadd 		return (EINVAL);
6492b3ad188SAdrian Chadd 
6502b3ad188SAdrian Chadd 	mtx_lock(&isrc_table_lock);
6512b3ad188SAdrian Chadd 	if (cpu == NOCPU) {
6522b3ad188SAdrian Chadd 		CPU_ZERO(&isrc->isrc_cpu);
6532b3ad188SAdrian Chadd 		isrc->isrc_flags &= ~INTR_ISRCF_BOUND;
6542b3ad188SAdrian Chadd 	} else {
6552b3ad188SAdrian Chadd 		CPU_SETOF(cpu, &isrc->isrc_cpu);
6562b3ad188SAdrian Chadd 		isrc->isrc_flags |= INTR_ISRCF_BOUND;
6572b3ad188SAdrian Chadd 	}
6582b3ad188SAdrian Chadd 
6592b3ad188SAdrian Chadd 	/*
6602b3ad188SAdrian Chadd 	 * In NOCPU case, it's up to PIC to either leave ISRC on same CPU or
6612b3ad188SAdrian Chadd 	 * re-balance it to another CPU or enable it on more CPUs. However,
6622b3ad188SAdrian Chadd 	 * PIC is expected to change isrc_cpu appropriately to keep us well
6632b3ad188SAdrian Chadd 	 * informed if the call is successfull.
6642b3ad188SAdrian Chadd 	 */
6652b3ad188SAdrian Chadd 	if (irq_assign_cpu) {
666bff6be3eSSvatopluk Kraus 		error = PIC_BIND_INTR(isrc->isrc_dev, isrc);
6672b3ad188SAdrian Chadd 		if (error) {
6682b3ad188SAdrian Chadd 			CPU_ZERO(&isrc->isrc_cpu);
6692b3ad188SAdrian Chadd 			mtx_unlock(&isrc_table_lock);
6702b3ad188SAdrian Chadd 			return (error);
6712b3ad188SAdrian Chadd 		}
6722b3ad188SAdrian Chadd 	}
6732b3ad188SAdrian Chadd 	mtx_unlock(&isrc_table_lock);
6742b3ad188SAdrian Chadd 	return (0);
6752b3ad188SAdrian Chadd #else
6762b3ad188SAdrian Chadd 	return (EOPNOTSUPP);
6772b3ad188SAdrian Chadd #endif
6782b3ad188SAdrian Chadd }
6792b3ad188SAdrian Chadd 
6802b3ad188SAdrian Chadd /*
6812b3ad188SAdrian Chadd  *  Create interrupt event for interrupt source.
6822b3ad188SAdrian Chadd  */
6832b3ad188SAdrian Chadd static int
6842b3ad188SAdrian Chadd isrc_event_create(struct intr_irqsrc *isrc)
6852b3ad188SAdrian Chadd {
6862b3ad188SAdrian Chadd 	struct intr_event *ie;
6872b3ad188SAdrian Chadd 	int error;
6882b3ad188SAdrian Chadd 
6892b3ad188SAdrian Chadd 	error = intr_event_create(&ie, isrc, 0, isrc->isrc_irq,
6902b3ad188SAdrian Chadd 	    intr_isrc_pre_ithread, intr_isrc_post_ithread, intr_isrc_post_filter,
6912b3ad188SAdrian Chadd 	    intr_isrc_assign_cpu, "%s:", isrc->isrc_name);
6922b3ad188SAdrian Chadd 	if (error)
6932b3ad188SAdrian Chadd 		return (error);
6942b3ad188SAdrian Chadd 
6952b3ad188SAdrian Chadd 	mtx_lock(&isrc_table_lock);
6962b3ad188SAdrian Chadd 	/*
6972b3ad188SAdrian Chadd 	 * Make sure that we do not mix the two ways
6982b3ad188SAdrian Chadd 	 * how we handle interrupt sources. Let contested event wins.
6992b3ad188SAdrian Chadd 	 */
700169e6abdSSvatopluk Kraus #ifdef INTR_SOLO
7012b3ad188SAdrian Chadd 	if (isrc->isrc_filter != NULL || isrc->isrc_event != NULL) {
702169e6abdSSvatopluk Kraus #else
703169e6abdSSvatopluk Kraus 	if (isrc->isrc_event != NULL) {
704169e6abdSSvatopluk Kraus #endif
7052b3ad188SAdrian Chadd 		mtx_unlock(&isrc_table_lock);
7062b3ad188SAdrian Chadd 		intr_event_destroy(ie);
7072b3ad188SAdrian Chadd 		return (isrc->isrc_event != NULL ? EBUSY : 0);
7082b3ad188SAdrian Chadd 	}
7092b3ad188SAdrian Chadd 	isrc->isrc_event = ie;
7102b3ad188SAdrian Chadd 	mtx_unlock(&isrc_table_lock);
7112b3ad188SAdrian Chadd 
7122b3ad188SAdrian Chadd 	return (0);
7132b3ad188SAdrian Chadd }
7142b3ad188SAdrian Chadd #ifdef notyet
7152b3ad188SAdrian Chadd /*
7162b3ad188SAdrian Chadd  *  Destroy interrupt event for interrupt source.
7172b3ad188SAdrian Chadd  */
7182b3ad188SAdrian Chadd static void
7192b3ad188SAdrian Chadd isrc_event_destroy(struct intr_irqsrc *isrc)
7202b3ad188SAdrian Chadd {
7212b3ad188SAdrian Chadd 	struct intr_event *ie;
7222b3ad188SAdrian Chadd 
7232b3ad188SAdrian Chadd 	mtx_lock(&isrc_table_lock);
7242b3ad188SAdrian Chadd 	ie = isrc->isrc_event;
7252b3ad188SAdrian Chadd 	isrc->isrc_event = NULL;
7262b3ad188SAdrian Chadd 	mtx_unlock(&isrc_table_lock);
7272b3ad188SAdrian Chadd 
7282b3ad188SAdrian Chadd 	if (ie != NULL)
7292b3ad188SAdrian Chadd 		intr_event_destroy(ie);
7302b3ad188SAdrian Chadd }
7312b3ad188SAdrian Chadd #endif
7322b3ad188SAdrian Chadd /*
7332b3ad188SAdrian Chadd  *  Add handler to interrupt source.
7342b3ad188SAdrian Chadd  */
7352b3ad188SAdrian Chadd static int
7362b3ad188SAdrian Chadd isrc_add_handler(struct intr_irqsrc *isrc, const char *name,
7372b3ad188SAdrian Chadd     driver_filter_t filter, driver_intr_t handler, void *arg,
7382b3ad188SAdrian Chadd     enum intr_type flags, void **cookiep)
7392b3ad188SAdrian Chadd {
7402b3ad188SAdrian Chadd 	int error;
7412b3ad188SAdrian Chadd 
7422b3ad188SAdrian Chadd 	if (isrc->isrc_event == NULL) {
7432b3ad188SAdrian Chadd 		error = isrc_event_create(isrc);
7442b3ad188SAdrian Chadd 		if (error)
7452b3ad188SAdrian Chadd 			return (error);
7462b3ad188SAdrian Chadd 	}
7472b3ad188SAdrian Chadd 
7482b3ad188SAdrian Chadd 	error = intr_event_add_handler(isrc->isrc_event, name, filter, handler,
7492b3ad188SAdrian Chadd 	    arg, intr_priority(flags), flags, cookiep);
7502b3ad188SAdrian Chadd 	if (error == 0) {
7512b3ad188SAdrian Chadd 		mtx_lock(&isrc_table_lock);
7522b3ad188SAdrian Chadd 		intrcnt_updatename(isrc);
7532b3ad188SAdrian Chadd 		mtx_unlock(&isrc_table_lock);
7542b3ad188SAdrian Chadd 	}
7552b3ad188SAdrian Chadd 
7562b3ad188SAdrian Chadd 	return (error);
7572b3ad188SAdrian Chadd }
7582b3ad188SAdrian Chadd 
7592b3ad188SAdrian Chadd /*
7602b3ad188SAdrian Chadd  *  Lookup interrupt controller locked.
7612b3ad188SAdrian Chadd  */
762bff6be3eSSvatopluk Kraus static inline struct intr_pic *
7632b3ad188SAdrian Chadd pic_lookup_locked(device_t dev, intptr_t xref)
7642b3ad188SAdrian Chadd {
7652b3ad188SAdrian Chadd 	struct intr_pic *pic;
7662b3ad188SAdrian Chadd 
7672b3ad188SAdrian Chadd 	mtx_assert(&pic_list_lock, MA_OWNED);
7682b3ad188SAdrian Chadd 
769*4be58cbaSSvatopluk Kraus 	if (dev == NULL && xref == 0)
770*4be58cbaSSvatopluk Kraus 		return (NULL);
771*4be58cbaSSvatopluk Kraus 
772*4be58cbaSSvatopluk Kraus 	/* Note that pic->pic_dev is never NULL on registered PIC. */
7732b3ad188SAdrian Chadd 	SLIST_FOREACH(pic, &pic_list, pic_next) {
774*4be58cbaSSvatopluk Kraus 		if (dev == NULL) {
775*4be58cbaSSvatopluk Kraus 			if (xref == pic->pic_xref)
776*4be58cbaSSvatopluk Kraus 				return (pic);
777*4be58cbaSSvatopluk Kraus 		} else if (xref == 0 || pic->pic_xref == 0) {
778*4be58cbaSSvatopluk Kraus 			if (dev == pic->pic_dev)
779*4be58cbaSSvatopluk Kraus 				return (pic);
780*4be58cbaSSvatopluk Kraus 		} else if (xref == pic->pic_xref && dev == pic->pic_dev)
7812b3ad188SAdrian Chadd 				return (pic);
7822b3ad188SAdrian Chadd 	}
7832b3ad188SAdrian Chadd 	return (NULL);
7842b3ad188SAdrian Chadd }
7852b3ad188SAdrian Chadd 
7862b3ad188SAdrian Chadd /*
7872b3ad188SAdrian Chadd  *  Lookup interrupt controller.
7882b3ad188SAdrian Chadd  */
7892b3ad188SAdrian Chadd static struct intr_pic *
7902b3ad188SAdrian Chadd pic_lookup(device_t dev, intptr_t xref)
7912b3ad188SAdrian Chadd {
7922b3ad188SAdrian Chadd 	struct intr_pic *pic;
7932b3ad188SAdrian Chadd 
7942b3ad188SAdrian Chadd 	mtx_lock(&pic_list_lock);
7952b3ad188SAdrian Chadd 	pic = pic_lookup_locked(dev, xref);
7962b3ad188SAdrian Chadd 	mtx_unlock(&pic_list_lock);
7972b3ad188SAdrian Chadd 	return (pic);
7982b3ad188SAdrian Chadd }
7992b3ad188SAdrian Chadd 
8002b3ad188SAdrian Chadd /*
8012b3ad188SAdrian Chadd  *  Create interrupt controller.
8022b3ad188SAdrian Chadd  */
8032b3ad188SAdrian Chadd static struct intr_pic *
8042b3ad188SAdrian Chadd pic_create(device_t dev, intptr_t xref)
8052b3ad188SAdrian Chadd {
8062b3ad188SAdrian Chadd 	struct intr_pic *pic;
8072b3ad188SAdrian Chadd 
8082b3ad188SAdrian Chadd 	mtx_lock(&pic_list_lock);
8092b3ad188SAdrian Chadd 	pic = pic_lookup_locked(dev, xref);
8102b3ad188SAdrian Chadd 	if (pic != NULL) {
8112b3ad188SAdrian Chadd 		mtx_unlock(&pic_list_lock);
8122b3ad188SAdrian Chadd 		return (pic);
8132b3ad188SAdrian Chadd 	}
8142b3ad188SAdrian Chadd 	pic = malloc(sizeof(*pic), M_INTRNG, M_NOWAIT | M_ZERO);
8152b3ad188SAdrian Chadd 	pic->pic_xref = xref;
8162b3ad188SAdrian Chadd 	pic->pic_dev = dev;
8172b3ad188SAdrian Chadd 	SLIST_INSERT_HEAD(&pic_list, pic, pic_next);
8182b3ad188SAdrian Chadd 	mtx_unlock(&pic_list_lock);
8192b3ad188SAdrian Chadd 
8202b3ad188SAdrian Chadd 	return (pic);
8212b3ad188SAdrian Chadd }
8222b3ad188SAdrian Chadd #ifdef notyet
8232b3ad188SAdrian Chadd /*
8242b3ad188SAdrian Chadd  *  Destroy interrupt controller.
8252b3ad188SAdrian Chadd  */
8262b3ad188SAdrian Chadd static void
8272b3ad188SAdrian Chadd pic_destroy(device_t dev, intptr_t xref)
8282b3ad188SAdrian Chadd {
8292b3ad188SAdrian Chadd 	struct intr_pic *pic;
8302b3ad188SAdrian Chadd 
8312b3ad188SAdrian Chadd 	mtx_lock(&pic_list_lock);
8322b3ad188SAdrian Chadd 	pic = pic_lookup_locked(dev, xref);
8332b3ad188SAdrian Chadd 	if (pic == NULL) {
8342b3ad188SAdrian Chadd 		mtx_unlock(&pic_list_lock);
8352b3ad188SAdrian Chadd 		return;
8362b3ad188SAdrian Chadd 	}
8372b3ad188SAdrian Chadd 	SLIST_REMOVE(&pic_list, pic, intr_pic, pic_next);
8382b3ad188SAdrian Chadd 	mtx_unlock(&pic_list_lock);
8392b3ad188SAdrian Chadd 
8402b3ad188SAdrian Chadd 	free(pic, M_INTRNG);
8412b3ad188SAdrian Chadd }
8422b3ad188SAdrian Chadd #endif
8432b3ad188SAdrian Chadd /*
8442b3ad188SAdrian Chadd  *  Register interrupt controller.
8452b3ad188SAdrian Chadd  */
8462b3ad188SAdrian Chadd int
8472b3ad188SAdrian Chadd intr_pic_register(device_t dev, intptr_t xref)
8482b3ad188SAdrian Chadd {
8492b3ad188SAdrian Chadd 	struct intr_pic *pic;
8502b3ad188SAdrian Chadd 
851*4be58cbaSSvatopluk Kraus 	if (dev == NULL)
852*4be58cbaSSvatopluk Kraus 		return (EINVAL);
8532b3ad188SAdrian Chadd 	pic = pic_create(dev, xref);
8542b3ad188SAdrian Chadd 	if (pic == NULL)
8552b3ad188SAdrian Chadd 		return (ENOMEM);
8562b3ad188SAdrian Chadd 
857*4be58cbaSSvatopluk Kraus 	debugf("PIC %p registered for %s <dev %p, xref %x>\n", pic,
858*4be58cbaSSvatopluk Kraus 	    device_get_nameunit(dev), dev, xref);
8592b3ad188SAdrian Chadd 	return (0);
8602b3ad188SAdrian Chadd }
8612b3ad188SAdrian Chadd 
8622b3ad188SAdrian Chadd /*
8632b3ad188SAdrian Chadd  *  Unregister interrupt controller.
8642b3ad188SAdrian Chadd  */
8652b3ad188SAdrian Chadd int
866bff6be3eSSvatopluk Kraus intr_pic_deregister(device_t dev, intptr_t xref)
8672b3ad188SAdrian Chadd {
8682b3ad188SAdrian Chadd 
8692b3ad188SAdrian Chadd 	panic("%s: not implemented", __func__);
8702b3ad188SAdrian Chadd }
8712b3ad188SAdrian Chadd 
8722b3ad188SAdrian Chadd /*
8732b3ad188SAdrian Chadd  *  Mark interrupt controller (itself) as a root one.
8742b3ad188SAdrian Chadd  *
8752b3ad188SAdrian Chadd  *  Note that only an interrupt controller can really know its position
8762b3ad188SAdrian Chadd  *  in interrupt controller's tree. So root PIC must claim itself as a root.
8772b3ad188SAdrian Chadd  *
8782b3ad188SAdrian Chadd  *  In FDT case, according to ePAPR approved version 1.1 from 08 April 2011,
8792b3ad188SAdrian Chadd  *  page 30:
8802b3ad188SAdrian Chadd  *    "The root of the interrupt tree is determined when traversal
8812b3ad188SAdrian Chadd  *     of the interrupt tree reaches an interrupt controller node without
8822b3ad188SAdrian Chadd  *     an interrupts property and thus no explicit interrupt parent."
8832b3ad188SAdrian Chadd  */
8842b3ad188SAdrian Chadd int
8852b3ad188SAdrian Chadd intr_pic_claim_root(device_t dev, intptr_t xref, intr_irq_filter_t *filter,
8862b3ad188SAdrian Chadd     void *arg, u_int ipicount)
8872b3ad188SAdrian Chadd {
8882b3ad188SAdrian Chadd 
8892b3ad188SAdrian Chadd 	if (pic_lookup(dev, xref) == NULL) {
8902b3ad188SAdrian Chadd 		device_printf(dev, "not registered\n");
8912b3ad188SAdrian Chadd 		return (EINVAL);
8922b3ad188SAdrian Chadd 	}
8932b3ad188SAdrian Chadd 	if (filter == NULL) {
8942b3ad188SAdrian Chadd 		device_printf(dev, "filter missing\n");
8952b3ad188SAdrian Chadd 		return (EINVAL);
8962b3ad188SAdrian Chadd 	}
8972b3ad188SAdrian Chadd 
8982b3ad188SAdrian Chadd 	/*
8992b3ad188SAdrian Chadd 	 * Only one interrupt controllers could be on the root for now.
9002b3ad188SAdrian Chadd 	 * Note that we further suppose that there is not threaded interrupt
9012b3ad188SAdrian Chadd 	 * routine (handler) on the root. See intr_irq_handler().
9022b3ad188SAdrian Chadd 	 */
9035b70c08cSSvatopluk Kraus 	if (intr_irq_root_dev != NULL) {
9042b3ad188SAdrian Chadd 		device_printf(dev, "another root already set\n");
9052b3ad188SAdrian Chadd 		return (EBUSY);
9062b3ad188SAdrian Chadd 	}
9072b3ad188SAdrian Chadd 
9085b70c08cSSvatopluk Kraus 	intr_irq_root_dev = dev;
9092b3ad188SAdrian Chadd 	irq_root_filter = filter;
9102b3ad188SAdrian Chadd 	irq_root_arg = arg;
9112b3ad188SAdrian Chadd 	irq_root_ipicount = ipicount;
9122b3ad188SAdrian Chadd 
9132b3ad188SAdrian Chadd 	debugf("irq root set to %s\n", device_get_nameunit(dev));
9142b3ad188SAdrian Chadd 	return (0);
9152b3ad188SAdrian Chadd }
9162b3ad188SAdrian Chadd 
9172b3ad188SAdrian Chadd int
918bff6be3eSSvatopluk Kraus intr_map_irq(device_t dev, intptr_t xref, struct intr_map_data *data,
919bff6be3eSSvatopluk Kraus     u_int *irqp)
9202b3ad188SAdrian Chadd {
9212b3ad188SAdrian Chadd 	int error;
922bff6be3eSSvatopluk Kraus 	struct intr_irqsrc *isrc;
923bff6be3eSSvatopluk Kraus 	struct intr_pic *pic;
924bff6be3eSSvatopluk Kraus 
925bff6be3eSSvatopluk Kraus 	if (data == NULL)
926bff6be3eSSvatopluk Kraus 		return (EINVAL);
927bff6be3eSSvatopluk Kraus 
928bff6be3eSSvatopluk Kraus 	pic = pic_lookup(dev, xref);
929bff6be3eSSvatopluk Kraus 	if (pic == NULL || pic->pic_dev == NULL)
930bff6be3eSSvatopluk Kraus 		return (ESRCH);
931bff6be3eSSvatopluk Kraus 
932bff6be3eSSvatopluk Kraus 	error = PIC_MAP_INTR(pic->pic_dev, data, &isrc);
933bff6be3eSSvatopluk Kraus 	if (error == 0)
934bff6be3eSSvatopluk Kraus 		*irqp = isrc->isrc_irq;
935bff6be3eSSvatopluk Kraus 	return (error);
936bff6be3eSSvatopluk Kraus }
937bff6be3eSSvatopluk Kraus 
938bff6be3eSSvatopluk Kraus int
939bff6be3eSSvatopluk Kraus intr_alloc_irq(device_t dev, struct resource *res)
940bff6be3eSSvatopluk Kraus {
941bff6be3eSSvatopluk Kraus 	struct intr_map_data *data;
942bff6be3eSSvatopluk Kraus 	struct intr_irqsrc *isrc;
943bff6be3eSSvatopluk Kraus 
944bff6be3eSSvatopluk Kraus 	KASSERT(rman_get_start(res) == rman_get_end(res),
945bff6be3eSSvatopluk Kraus 	    ("%s: more interrupts in resource", __func__));
946bff6be3eSSvatopluk Kraus 
947bff6be3eSSvatopluk Kraus 	isrc = intr_ddata_lookup(rman_get_start(res), &data);
948bff6be3eSSvatopluk Kraus 	if (isrc == NULL)
949bff6be3eSSvatopluk Kraus 		return (EINVAL);
950bff6be3eSSvatopluk Kraus 
951bff6be3eSSvatopluk Kraus 	return (PIC_ALLOC_INTR(isrc->isrc_dev, isrc, res, data));
952bff6be3eSSvatopluk Kraus }
953bff6be3eSSvatopluk Kraus 
954bff6be3eSSvatopluk Kraus int
955bff6be3eSSvatopluk Kraus intr_release_irq(device_t dev, struct resource *res)
956bff6be3eSSvatopluk Kraus {
957bff6be3eSSvatopluk Kraus 	struct intr_map_data *data;
958bff6be3eSSvatopluk Kraus 	struct intr_irqsrc *isrc;
959bff6be3eSSvatopluk Kraus 
960bff6be3eSSvatopluk Kraus 	KASSERT(rman_get_start(res) == rman_get_end(res),
961bff6be3eSSvatopluk Kraus 	    ("%s: more interrupts in resource", __func__));
962bff6be3eSSvatopluk Kraus 
963bff6be3eSSvatopluk Kraus 	isrc = intr_ddata_lookup(rman_get_start(res), &data);
964bff6be3eSSvatopluk Kraus 	if (isrc == NULL)
965bff6be3eSSvatopluk Kraus 		return (EINVAL);
966bff6be3eSSvatopluk Kraus 
967bff6be3eSSvatopluk Kraus 	return (PIC_RELEASE_INTR(isrc->isrc_dev, isrc, res, data));
968bff6be3eSSvatopluk Kraus }
969bff6be3eSSvatopluk Kraus 
970bff6be3eSSvatopluk Kraus int
971bff6be3eSSvatopluk Kraus intr_setup_irq(device_t dev, struct resource *res, driver_filter_t filt,
972bff6be3eSSvatopluk Kraus     driver_intr_t hand, void *arg, int flags, void **cookiep)
973bff6be3eSSvatopluk Kraus {
974bff6be3eSSvatopluk Kraus 	int error;
975bff6be3eSSvatopluk Kraus 	struct intr_map_data *data;
976bff6be3eSSvatopluk Kraus 	struct intr_irqsrc *isrc;
977bff6be3eSSvatopluk Kraus 	const char *name;
978bff6be3eSSvatopluk Kraus 
979bff6be3eSSvatopluk Kraus 	KASSERT(rman_get_start(res) == rman_get_end(res),
980bff6be3eSSvatopluk Kraus 	    ("%s: more interrupts in resource", __func__));
981bff6be3eSSvatopluk Kraus 
982bff6be3eSSvatopluk Kraus 	isrc = intr_ddata_lookup(rman_get_start(res), &data);
983bff6be3eSSvatopluk Kraus 	if (isrc == NULL)
984bff6be3eSSvatopluk Kraus 		return (EINVAL);
9852b3ad188SAdrian Chadd 
9862b3ad188SAdrian Chadd 	name = device_get_nameunit(dev);
9872b3ad188SAdrian Chadd 
9882b3ad188SAdrian Chadd #ifdef INTR_SOLO
9892b3ad188SAdrian Chadd 	/*
9902b3ad188SAdrian Chadd 	 * Standard handling is done thru MI interrupt framework. However,
9912b3ad188SAdrian Chadd 	 * some interrupts could request solely own special handling. This
9922b3ad188SAdrian Chadd 	 * non standard handling can be used for interrupt controllers without
9932b3ad188SAdrian Chadd 	 * handler (filter only), so in case that interrupt controllers are
9942b3ad188SAdrian Chadd 	 * chained, MI interrupt framework is called only in leaf controller.
9952b3ad188SAdrian Chadd 	 *
9962b3ad188SAdrian Chadd 	 * Note that root interrupt controller routine is served as well,
9972b3ad188SAdrian Chadd 	 * however in intr_irq_handler(), i.e. main system dispatch routine.
9982b3ad188SAdrian Chadd 	 */
9992b3ad188SAdrian Chadd 	if (flags & INTR_SOLO && hand != NULL) {
10002b3ad188SAdrian Chadd 		debugf("irq %u cannot solo on %s\n", irq, name);
10012b3ad188SAdrian Chadd 		return (EINVAL);
10022b3ad188SAdrian Chadd 	}
10032b3ad188SAdrian Chadd 
10042b3ad188SAdrian Chadd 	if (flags & INTR_SOLO) {
10052b3ad188SAdrian Chadd 		error = iscr_setup_filter(isrc, name, (intr_irq_filter_t *)filt,
10062b3ad188SAdrian Chadd 		    arg, cookiep);
10072b3ad188SAdrian Chadd 		debugf("irq %u setup filter error %d on %s\n", irq, error,
10082b3ad188SAdrian Chadd 		    name);
10092b3ad188SAdrian Chadd 	} else
10102b3ad188SAdrian Chadd #endif
10112b3ad188SAdrian Chadd 		{
10122b3ad188SAdrian Chadd 		error = isrc_add_handler(isrc, name, filt, hand, arg, flags,
10132b3ad188SAdrian Chadd 		    cookiep);
10142b3ad188SAdrian Chadd 		debugf("irq %u add handler error %d on %s\n", irq, error, name);
10152b3ad188SAdrian Chadd 	}
10162b3ad188SAdrian Chadd 	if (error != 0)
10172b3ad188SAdrian Chadd 		return (error);
10182b3ad188SAdrian Chadd 
10192b3ad188SAdrian Chadd 	mtx_lock(&isrc_table_lock);
1020bff6be3eSSvatopluk Kraus 	error = PIC_SETUP_INTR(isrc->isrc_dev, isrc, res, data);
1021bff6be3eSSvatopluk Kraus 	if (error == 0) {
10222b3ad188SAdrian Chadd 		isrc->isrc_handlers++;
1023bff6be3eSSvatopluk Kraus 		if (isrc->isrc_handlers == 1)
10242b3ad188SAdrian Chadd 			PIC_ENABLE_INTR(isrc->isrc_dev, isrc);
10252b3ad188SAdrian Chadd 	}
10262b3ad188SAdrian Chadd 	mtx_unlock(&isrc_table_lock);
1027bff6be3eSSvatopluk Kraus 	if (error != 0)
1028bff6be3eSSvatopluk Kraus 		intr_event_remove_handler(*cookiep);
1029bff6be3eSSvatopluk Kraus 	return (error);
10302b3ad188SAdrian Chadd }
10312b3ad188SAdrian Chadd 
10322b3ad188SAdrian Chadd int
1033bff6be3eSSvatopluk Kraus intr_teardown_irq(device_t dev, struct resource *res, void *cookie)
10342b3ad188SAdrian Chadd {
10352b3ad188SAdrian Chadd 	int error;
1036bff6be3eSSvatopluk Kraus 	struct intr_map_data *data;
1037bff6be3eSSvatopluk Kraus 	struct intr_irqsrc *isrc;
10382b3ad188SAdrian Chadd 
1039bff6be3eSSvatopluk Kraus 	KASSERT(rman_get_start(res) == rman_get_end(res),
1040bff6be3eSSvatopluk Kraus 	    ("%s: more interrupts in resource", __func__));
1041bff6be3eSSvatopluk Kraus 
1042bff6be3eSSvatopluk Kraus 	isrc = intr_ddata_lookup(rman_get_start(res), &data);
10432b3ad188SAdrian Chadd 	if (isrc == NULL || isrc->isrc_handlers == 0)
10442b3ad188SAdrian Chadd 		return (EINVAL);
1045bff6be3eSSvatopluk Kraus 
1046169e6abdSSvatopluk Kraus #ifdef INTR_SOLO
10472b3ad188SAdrian Chadd 	if (isrc->isrc_filter != NULL) {
10482b3ad188SAdrian Chadd 		if (isrc != cookie)
10492b3ad188SAdrian Chadd 			return (EINVAL);
10502b3ad188SAdrian Chadd 
10512b3ad188SAdrian Chadd 		mtx_lock(&isrc_table_lock);
10522b3ad188SAdrian Chadd 		isrc->isrc_filter = NULL;
10532b3ad188SAdrian Chadd 		isrc->isrc_arg = NULL;
10542b3ad188SAdrian Chadd 		isrc->isrc_handlers = 0;
10552b3ad188SAdrian Chadd 		PIC_DISABLE_INTR(isrc->isrc_dev, isrc);
1056bff6be3eSSvatopluk Kraus 		PIC_TEARDOWN_INTR(isrc->isrc_dev, isrc, res, data);
10572b3ad188SAdrian Chadd 		isrc_update_name(isrc, NULL);
10582b3ad188SAdrian Chadd 		mtx_unlock(&isrc_table_lock);
10592b3ad188SAdrian Chadd 		return (0);
10602b3ad188SAdrian Chadd 	}
1061169e6abdSSvatopluk Kraus #endif
10622b3ad188SAdrian Chadd 	if (isrc != intr_handler_source(cookie))
10632b3ad188SAdrian Chadd 		return (EINVAL);
10642b3ad188SAdrian Chadd 
10652b3ad188SAdrian Chadd 	error = intr_event_remove_handler(cookie);
10662b3ad188SAdrian Chadd 	if (error == 0) {
10672b3ad188SAdrian Chadd 		mtx_lock(&isrc_table_lock);
10682b3ad188SAdrian Chadd 		isrc->isrc_handlers--;
1069bff6be3eSSvatopluk Kraus 		if (isrc->isrc_handlers == 0)
10702b3ad188SAdrian Chadd 			PIC_DISABLE_INTR(isrc->isrc_dev, isrc);
1071bff6be3eSSvatopluk Kraus 		PIC_TEARDOWN_INTR(isrc->isrc_dev, isrc, res, data);
10722b3ad188SAdrian Chadd 		intrcnt_updatename(isrc);
10732b3ad188SAdrian Chadd 		mtx_unlock(&isrc_table_lock);
10742b3ad188SAdrian Chadd 	}
10752b3ad188SAdrian Chadd 	return (error);
10762b3ad188SAdrian Chadd }
10772b3ad188SAdrian Chadd 
10782b3ad188SAdrian Chadd int
1079bff6be3eSSvatopluk Kraus intr_describe_irq(device_t dev, struct resource *res, void *cookie,
1080bff6be3eSSvatopluk Kraus     const char *descr)
10812b3ad188SAdrian Chadd {
10822b3ad188SAdrian Chadd 	int error;
1083bff6be3eSSvatopluk Kraus 	struct intr_irqsrc *isrc;
10842b3ad188SAdrian Chadd 
1085bff6be3eSSvatopluk Kraus 	KASSERT(rman_get_start(res) == rman_get_end(res),
1086bff6be3eSSvatopluk Kraus 	    ("%s: more interrupts in resource", __func__));
1087bff6be3eSSvatopluk Kraus 
1088bff6be3eSSvatopluk Kraus 	isrc = intr_ddata_lookup(rman_get_start(res), NULL);
10892b3ad188SAdrian Chadd 	if (isrc == NULL || isrc->isrc_handlers == 0)
10902b3ad188SAdrian Chadd 		return (EINVAL);
1091169e6abdSSvatopluk Kraus #ifdef INTR_SOLO
10922b3ad188SAdrian Chadd 	if (isrc->isrc_filter != NULL) {
10932b3ad188SAdrian Chadd 		if (isrc != cookie)
10942b3ad188SAdrian Chadd 			return (EINVAL);
10952b3ad188SAdrian Chadd 
10962b3ad188SAdrian Chadd 		mtx_lock(&isrc_table_lock);
10972b3ad188SAdrian Chadd 		isrc_update_name(isrc, descr);
10982b3ad188SAdrian Chadd 		mtx_unlock(&isrc_table_lock);
10992b3ad188SAdrian Chadd 		return (0);
11002b3ad188SAdrian Chadd 	}
1101169e6abdSSvatopluk Kraus #endif
11022b3ad188SAdrian Chadd 	error = intr_event_describe_handler(isrc->isrc_event, cookie, descr);
11032b3ad188SAdrian Chadd 	if (error == 0) {
11042b3ad188SAdrian Chadd 		mtx_lock(&isrc_table_lock);
11052b3ad188SAdrian Chadd 		intrcnt_updatename(isrc);
11062b3ad188SAdrian Chadd 		mtx_unlock(&isrc_table_lock);
11072b3ad188SAdrian Chadd 	}
11082b3ad188SAdrian Chadd 	return (error);
11092b3ad188SAdrian Chadd }
11102b3ad188SAdrian Chadd 
11112b3ad188SAdrian Chadd #ifdef SMP
11122b3ad188SAdrian Chadd int
1113bff6be3eSSvatopluk Kraus intr_bind_irq(device_t dev, struct resource *res, int cpu)
11142b3ad188SAdrian Chadd {
11152b3ad188SAdrian Chadd 	struct intr_irqsrc *isrc;
11162b3ad188SAdrian Chadd 
1117bff6be3eSSvatopluk Kraus 	KASSERT(rman_get_start(res) == rman_get_end(res),
1118bff6be3eSSvatopluk Kraus 	    ("%s: more interrupts in resource", __func__));
1119bff6be3eSSvatopluk Kraus 
1120bff6be3eSSvatopluk Kraus 	isrc = intr_ddata_lookup(rman_get_start(res), NULL);
11212b3ad188SAdrian Chadd 	if (isrc == NULL || isrc->isrc_handlers == 0)
11222b3ad188SAdrian Chadd 		return (EINVAL);
1123169e6abdSSvatopluk Kraus #ifdef INTR_SOLO
11242b3ad188SAdrian Chadd 	if (isrc->isrc_filter != NULL)
11252b3ad188SAdrian Chadd 		return (intr_isrc_assign_cpu(isrc, cpu));
1126169e6abdSSvatopluk Kraus #endif
11272b3ad188SAdrian Chadd 	return (intr_event_bind(isrc->isrc_event, cpu));
11282b3ad188SAdrian Chadd }
11292b3ad188SAdrian Chadd 
11302b3ad188SAdrian Chadd /*
11312b3ad188SAdrian Chadd  * Return the CPU that the next interrupt source should use.
11322b3ad188SAdrian Chadd  * For now just returns the next CPU according to round-robin.
11332b3ad188SAdrian Chadd  */
11342b3ad188SAdrian Chadd u_int
11352b3ad188SAdrian Chadd intr_irq_next_cpu(u_int last_cpu, cpuset_t *cpumask)
11362b3ad188SAdrian Chadd {
11372b3ad188SAdrian Chadd 
11382b3ad188SAdrian Chadd 	if (!irq_assign_cpu || mp_ncpus == 1)
11392b3ad188SAdrian Chadd 		return (PCPU_GET(cpuid));
11402b3ad188SAdrian Chadd 
11412b3ad188SAdrian Chadd 	do {
11422b3ad188SAdrian Chadd 		last_cpu++;
11432b3ad188SAdrian Chadd 		if (last_cpu > mp_maxid)
11442b3ad188SAdrian Chadd 			last_cpu = 0;
11452b3ad188SAdrian Chadd 	} while (!CPU_ISSET(last_cpu, cpumask));
11462b3ad188SAdrian Chadd 	return (last_cpu);
11472b3ad188SAdrian Chadd }
11482b3ad188SAdrian Chadd 
11492b3ad188SAdrian Chadd /*
11502b3ad188SAdrian Chadd  *  Distribute all the interrupt sources among the available
11512b3ad188SAdrian Chadd  *  CPUs once the AP's have been launched.
11522b3ad188SAdrian Chadd  */
11532b3ad188SAdrian Chadd static void
11542b3ad188SAdrian Chadd intr_irq_shuffle(void *arg __unused)
11552b3ad188SAdrian Chadd {
11562b3ad188SAdrian Chadd 	struct intr_irqsrc *isrc;
11572b3ad188SAdrian Chadd 	u_int i;
11582b3ad188SAdrian Chadd 
11592b3ad188SAdrian Chadd 	if (mp_ncpus == 1)
11602b3ad188SAdrian Chadd 		return;
11612b3ad188SAdrian Chadd 
11622b3ad188SAdrian Chadd 	mtx_lock(&isrc_table_lock);
11632b3ad188SAdrian Chadd 	irq_assign_cpu = TRUE;
11642b3ad188SAdrian Chadd 	for (i = 0; i < NIRQ; i++) {
11652b3ad188SAdrian Chadd 		isrc = irq_sources[i];
11662b3ad188SAdrian Chadd 		if (isrc == NULL || isrc->isrc_handlers == 0 ||
1167bff6be3eSSvatopluk Kraus 		    isrc->isrc_flags & INTR_ISRCF_PPI)
11682b3ad188SAdrian Chadd 			continue;
11692b3ad188SAdrian Chadd 
11702b3ad188SAdrian Chadd 		if (isrc->isrc_event != NULL &&
11712b3ad188SAdrian Chadd 		    isrc->isrc_flags & INTR_ISRCF_BOUND &&
11722b3ad188SAdrian Chadd 		    isrc->isrc_event->ie_cpu != CPU_FFS(&isrc->isrc_cpu) - 1)
11732b3ad188SAdrian Chadd 			panic("%s: CPU inconsistency", __func__);
11742b3ad188SAdrian Chadd 
11752b3ad188SAdrian Chadd 		if ((isrc->isrc_flags & INTR_ISRCF_BOUND) == 0)
11762b3ad188SAdrian Chadd 			CPU_ZERO(&isrc->isrc_cpu); /* start again */
11772b3ad188SAdrian Chadd 
11782b3ad188SAdrian Chadd 		/*
11792b3ad188SAdrian Chadd 		 * We are in wicked position here if the following call fails
11802b3ad188SAdrian Chadd 		 * for bound ISRC. The best thing we can do is to clear
11812b3ad188SAdrian Chadd 		 * isrc_cpu so inconsistency with ie_cpu will be detectable.
11822b3ad188SAdrian Chadd 		 */
1183bff6be3eSSvatopluk Kraus 		if (PIC_BIND_INTR(isrc->isrc_dev, isrc) != 0)
11842b3ad188SAdrian Chadd 			CPU_ZERO(&isrc->isrc_cpu);
11852b3ad188SAdrian Chadd 	}
11862b3ad188SAdrian Chadd 	mtx_unlock(&isrc_table_lock);
11872b3ad188SAdrian Chadd }
11882b3ad188SAdrian Chadd SYSINIT(intr_irq_shuffle, SI_SUB_SMP, SI_ORDER_SECOND, intr_irq_shuffle, NULL);
11892b3ad188SAdrian Chadd 
11902b3ad188SAdrian Chadd #else
11912b3ad188SAdrian Chadd u_int
11922b3ad188SAdrian Chadd intr_irq_next_cpu(u_int current_cpu, cpuset_t *cpumask)
11932b3ad188SAdrian Chadd {
11942b3ad188SAdrian Chadd 
11952b3ad188SAdrian Chadd 	return (PCPU_GET(cpuid));
11962b3ad188SAdrian Chadd }
11972b3ad188SAdrian Chadd #endif
11982b3ad188SAdrian Chadd 
11992b3ad188SAdrian Chadd void dosoftints(void);
12002b3ad188SAdrian Chadd void
12012b3ad188SAdrian Chadd dosoftints(void)
12022b3ad188SAdrian Chadd {
12032b3ad188SAdrian Chadd }
12042b3ad188SAdrian Chadd 
12052b3ad188SAdrian Chadd #ifdef SMP
12062b3ad188SAdrian Chadd /*
12072b3ad188SAdrian Chadd  *  Init interrupt controller on another CPU.
12082b3ad188SAdrian Chadd  */
12092b3ad188SAdrian Chadd void
12102b3ad188SAdrian Chadd intr_pic_init_secondary(void)
12112b3ad188SAdrian Chadd {
12122b3ad188SAdrian Chadd 
12132b3ad188SAdrian Chadd 	/*
12142b3ad188SAdrian Chadd 	 * QQQ: Only root PIC is aware of other CPUs ???
12152b3ad188SAdrian Chadd 	 */
12165b70c08cSSvatopluk Kraus 	KASSERT(intr_irq_root_dev != NULL, ("%s: no root attached", __func__));
12172b3ad188SAdrian Chadd 
12182b3ad188SAdrian Chadd 	//mtx_lock(&isrc_table_lock);
12195b70c08cSSvatopluk Kraus 	PIC_INIT_SECONDARY(intr_irq_root_dev);
12202b3ad188SAdrian Chadd 	//mtx_unlock(&isrc_table_lock);
12212b3ad188SAdrian Chadd }
12222b3ad188SAdrian Chadd #endif
12232b3ad188SAdrian Chadd 
12242b3ad188SAdrian Chadd #ifdef DDB
12252b3ad188SAdrian Chadd DB_SHOW_COMMAND(irqs, db_show_irqs)
12262b3ad188SAdrian Chadd {
12272b3ad188SAdrian Chadd 	u_int i, irqsum;
1228bff6be3eSSvatopluk Kraus 	u_long num;
12292b3ad188SAdrian Chadd 	struct intr_irqsrc *isrc;
12302b3ad188SAdrian Chadd 
12312b3ad188SAdrian Chadd 	for (irqsum = 0, i = 0; i < NIRQ; i++) {
12322b3ad188SAdrian Chadd 		isrc = irq_sources[i];
12332b3ad188SAdrian Chadd 		if (isrc == NULL)
12342b3ad188SAdrian Chadd 			continue;
12352b3ad188SAdrian Chadd 
1236bff6be3eSSvatopluk Kraus 		num = isrc->isrc_count != NULL ? isrc->isrc_count[0] : 0;
12372b3ad188SAdrian Chadd 		db_printf("irq%-3u <%s>: cpu %02lx%s cnt %lu\n", i,
12382b3ad188SAdrian Chadd 		    isrc->isrc_name, isrc->isrc_cpu.__bits[0],
1239bff6be3eSSvatopluk Kraus 		    isrc->isrc_flags & INTR_ISRCF_BOUND ? " (bound)" : "", num);
1240bff6be3eSSvatopluk Kraus 		irqsum += num;
12412b3ad188SAdrian Chadd 	}
12422b3ad188SAdrian Chadd 	db_printf("irq total %u\n", irqsum);
12432b3ad188SAdrian Chadd }
12442b3ad188SAdrian Chadd #endif
1245