2 * Copyright (c) 2015-2016 Svatopluk Kraus
3 * Copyright (c) 2015-2016 Michal Meloun
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
32 * New-style Interrupt Framework
34 * TODO: - add support for disconnected PICs.
35 * - to support IPI (PPI) enabling on other CPUs if already started.
36 * - to complete things for removable PICs.
40 #include "opt_hwpmc_hooks.h"
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
45 #include <sys/syslog.h>
46 #include <sys/malloc.h>
48 #include <sys/queue.h>
50 #include <sys/interrupt.h>
52 #include <sys/cpuset.h>
54 #include <sys/sched.h>
56 #include <sys/vmmeter.h>
58 #include <sys/pmckern.h>
61 #include <machine/atomic.h>
62 #include <machine/intr.h>
63 #include <machine/cpu.h>
64 #include <machine/smp.h>
65 #include <machine/stdarg.h>
74 #define INTRNAME_LEN (2*MAXCOMLEN + 1)
77 #define debugf(fmt, args...) do { printf("%s(): ", __func__); \
78 printf(fmt,##args); } while (0)
80 #define debugf(fmt, args...)
83 MALLOC_DECLARE(M_INTRNG);
84 MALLOC_DEFINE(M_INTRNG, "intr", "intr interrupt handling");
86 /* Main interrupt handler called from assembler -> 'hidden' for C code. */
87 void intr_irq_handler(struct trapframe *tf);
89 /* Root interrupt controller stuff. */
90 device_t intr_irq_root_dev;
91 static intr_irq_filter_t *irq_root_filter;
92 static void *irq_root_arg;
93 static u_int irq_root_ipicount;
95 struct intr_pic_child {
96 SLIST_ENTRY(intr_pic_child) pc_next;
97 struct intr_pic *pc_pic;
98 intr_child_irq_filter_t *pc_filter;
104 /* Interrupt controller definition. */
106 SLIST_ENTRY(intr_pic) pic_next;
107 intptr_t pic_xref; /* hardware identification */
109 #define FLAG_PIC (1 << 0)
110 #define FLAG_MSI (1 << 1)
112 struct mtx pic_child_lock;
113 SLIST_HEAD(, intr_pic_child) pic_children;
116 static struct mtx pic_list_lock;
117 static SLIST_HEAD(, intr_pic) pic_list;
119 static struct intr_pic *pic_lookup(device_t dev, intptr_t xref);
121 /* Interrupt source definition. */
122 static struct mtx isrc_table_lock;
123 static struct intr_irqsrc *irq_sources[NIRQ];
127 static boolean_t irq_assign_cpu = FALSE;
131 * - 2 counters for each I/O interrupt.
132 * - MAXCPU counters for each IPI counters for SMP.
135 #define INTRCNT_COUNT (NIRQ * 2 + INTR_IPI_COUNT * MAXCPU)
137 #define INTRCNT_COUNT (NIRQ * 2)
140 /* Data for MI statistics reporting. */
141 u_long intrcnt[INTRCNT_COUNT];
142 char intrnames[INTRCNT_COUNT * INTRNAME_LEN];
143 size_t sintrcnt = sizeof(intrcnt);
144 size_t sintrnames = sizeof(intrnames);
145 static u_int intrcnt_index;
147 static struct intr_irqsrc *intr_map_get_isrc(u_int res_id);
148 static void intr_map_set_isrc(u_int res_id, struct intr_irqsrc *isrc);
149 static struct intr_map_data * intr_map_get_map_data(u_int res_id);
150 static void intr_map_copy_map_data(u_int res_id, device_t *dev, intptr_t *xref,
151 struct intr_map_data **data);
154 * Interrupt framework initialization routine.
157 intr_irq_init(void *dummy __unused)
160 SLIST_INIT(&pic_list);
161 mtx_init(&pic_list_lock, "intr pic list", NULL, MTX_DEF);
163 mtx_init(&isrc_table_lock, "intr isrc table", NULL, MTX_DEF);
165 SYSINIT(intr_irq_init, SI_SUB_INTR, SI_ORDER_FIRST, intr_irq_init, NULL);
168 intrcnt_setname(const char *name, int index)
171 snprintf(intrnames + INTRNAME_LEN * index, INTRNAME_LEN, "%-*s",
172 INTRNAME_LEN - 1, name);
176 * Update name for interrupt source with interrupt event.
179 intrcnt_updatename(struct intr_irqsrc *isrc)
182 /* QQQ: What about stray counter name? */
183 mtx_assert(&isrc_table_lock, MA_OWNED);
184 intrcnt_setname(isrc->isrc_event->ie_fullname, isrc->isrc_index);
188 * Virtualization for interrupt source interrupt counter increment.
191 isrc_increment_count(struct intr_irqsrc *isrc)
194 if (isrc->isrc_flags & INTR_ISRCF_PPI)
195 atomic_add_long(&isrc->isrc_count[0], 1);
197 isrc->isrc_count[0]++;
201 * Virtualization for interrupt source interrupt stray counter increment.
204 isrc_increment_straycount(struct intr_irqsrc *isrc)
207 isrc->isrc_count[1]++;
211 * Virtualization for interrupt source interrupt name update.
214 isrc_update_name(struct intr_irqsrc *isrc, const char *name)
216 char str[INTRNAME_LEN];
218 mtx_assert(&isrc_table_lock, MA_OWNED);
221 snprintf(str, INTRNAME_LEN, "%s: %s", isrc->isrc_name, name);
222 intrcnt_setname(str, isrc->isrc_index);
223 snprintf(str, INTRNAME_LEN, "stray %s: %s", isrc->isrc_name,
225 intrcnt_setname(str, isrc->isrc_index + 1);
227 snprintf(str, INTRNAME_LEN, "%s:", isrc->isrc_name);
228 intrcnt_setname(str, isrc->isrc_index);
229 snprintf(str, INTRNAME_LEN, "stray %s:", isrc->isrc_name);
230 intrcnt_setname(str, isrc->isrc_index + 1);
235 * Virtualization for interrupt source interrupt counters setup.
238 isrc_setup_counters(struct intr_irqsrc *isrc)
243 * XXX - it does not work well with removable controllers and
244 * interrupt sources !!!
246 index = atomic_fetchadd_int(&intrcnt_index, 2);
247 isrc->isrc_index = index;
248 isrc->isrc_count = &intrcnt[index];
249 isrc_update_name(isrc, NULL);
253 * Virtualization for interrupt source interrupt counters release.
256 isrc_release_counters(struct intr_irqsrc *isrc)
259 panic("%s: not implemented", __func__);
264 * Virtualization for interrupt source IPI counters setup.
267 intr_ipi_setup_counters(const char *name)
270 char str[INTRNAME_LEN];
272 index = atomic_fetchadd_int(&intrcnt_index, MAXCPU);
273 for (i = 0; i < MAXCPU; i++) {
274 snprintf(str, INTRNAME_LEN, "cpu%d:%s", i, name);
275 intrcnt_setname(str, index + i);
277 return (&intrcnt[index]);
282 * Main interrupt dispatch handler. It's called straight
283 * from the assembler, where CPU interrupt is served.
286 intr_irq_handler(struct trapframe *tf)
288 struct trapframe * oldframe;
291 KASSERT(irq_root_filter != NULL, ("%s: no filter", __func__));
293 PCPU_INC(cnt.v_intr);
296 oldframe = td->td_intr_frame;
297 td->td_intr_frame = tf;
298 irq_root_filter(irq_root_arg);
299 td->td_intr_frame = oldframe;
302 if (pmc_hook && TRAPF_USERMODE(tf) &&
303 (PCPU_GET(curthread)->td_pflags & TDP_CALLCHAIN))
304 pmc_hook(PCPU_GET(curthread), PMC_FN_USER_CALLCHAIN, tf);
309 intr_child_irq_handler(struct intr_pic *parent, uintptr_t irq)
311 struct intr_pic_child *child;
315 mtx_lock_spin(&parent->pic_child_lock);
316 SLIST_FOREACH(child, &parent->pic_children, pc_next) {
317 if (child->pc_start <= irq &&
318 irq < (child->pc_start + child->pc_length)) {
323 mtx_unlock_spin(&parent->pic_child_lock);
326 return (child->pc_filter(child->pc_filter_arg, irq));
328 return (FILTER_STRAY);
332 * interrupt controller dispatch function for interrupts. It should
333 * be called straight from the interrupt controller, when associated interrupt
337 intr_isrc_dispatch(struct intr_irqsrc *isrc, struct trapframe *tf)
340 KASSERT(isrc != NULL, ("%s: no source", __func__));
342 isrc_increment_count(isrc);
345 if (isrc->isrc_filter != NULL) {
347 error = isrc->isrc_filter(isrc->isrc_arg, tf);
348 PIC_POST_FILTER(isrc->isrc_dev, isrc);
349 if (error == FILTER_HANDLED)
353 if (isrc->isrc_event != NULL) {
354 if (intr_event_handle(isrc->isrc_event, tf) == 0)
358 isrc_increment_straycount(isrc);
363 * Alloc unique interrupt number (resource handle) for interrupt source.
365 * There could be various strategies how to allocate free interrupt number
366 * (resource handle) for new interrupt source.
368 * 1. Handles are always allocated forward, so handles are not recycled
369 * immediately. However, if only one free handle left which is reused
373 isrc_alloc_irq(struct intr_irqsrc *isrc)
377 mtx_assert(&isrc_table_lock, MA_OWNED);
379 maxirqs = nitems(irq_sources);
380 if (irq_next_free >= maxirqs)
383 for (irq = irq_next_free; irq < maxirqs; irq++) {
384 if (irq_sources[irq] == NULL)
387 for (irq = 0; irq < irq_next_free; irq++) {
388 if (irq_sources[irq] == NULL)
392 irq_next_free = maxirqs;
396 isrc->isrc_irq = irq;
397 irq_sources[irq] = isrc;
399 irq_next_free = irq + 1;
400 if (irq_next_free >= maxirqs)
406 * Free unique interrupt number (resource handle) from interrupt source.
409 isrc_free_irq(struct intr_irqsrc *isrc)
412 mtx_assert(&isrc_table_lock, MA_OWNED);
414 if (isrc->isrc_irq >= nitems(irq_sources))
416 if (irq_sources[isrc->isrc_irq] != isrc)
419 irq_sources[isrc->isrc_irq] = NULL;
420 isrc->isrc_irq = INTR_IRQ_INVALID; /* just to be safe */
425 * Initialize interrupt source and register it into global interrupt table.
428 intr_isrc_register(struct intr_irqsrc *isrc, device_t dev, u_int flags,
429 const char *fmt, ...)
434 bzero(isrc, sizeof(struct intr_irqsrc));
435 isrc->isrc_dev = dev;
436 isrc->isrc_irq = INTR_IRQ_INVALID; /* just to be safe */
437 isrc->isrc_flags = flags;
440 vsnprintf(isrc->isrc_name, INTR_ISRC_NAMELEN, fmt, ap);
443 mtx_lock(&isrc_table_lock);
444 error = isrc_alloc_irq(isrc);
446 mtx_unlock(&isrc_table_lock);
450 * Setup interrupt counters, but not for IPI sources. Those are setup
451 * later and only for used ones (up to INTR_IPI_COUNT) to not exhaust
454 if ((isrc->isrc_flags & INTR_ISRCF_IPI) == 0)
455 isrc_setup_counters(isrc);
456 mtx_unlock(&isrc_table_lock);
461 * Deregister interrupt source from global interrupt table.
464 intr_isrc_deregister(struct intr_irqsrc *isrc)
468 mtx_lock(&isrc_table_lock);
469 if ((isrc->isrc_flags & INTR_ISRCF_IPI) == 0)
470 isrc_release_counters(isrc);
471 error = isrc_free_irq(isrc);
472 mtx_unlock(&isrc_table_lock);
478 * A support function for a PIC to decide if provided ISRC should be inited
479 * on given cpu. The logic of INTR_ISRCF_BOUND flag and isrc_cpu member of
480 * struct intr_irqsrc is the following:
482 * If INTR_ISRCF_BOUND is set, the ISRC should be inited only on cpus
483 * set in isrc_cpu. If not, the ISRC should be inited on every cpu and
484 * isrc_cpu is kept consistent with it. Thus isrc_cpu is always correct.
487 intr_isrc_init_on_cpu(struct intr_irqsrc *isrc, u_int cpu)
490 if (isrc->isrc_handlers == 0)
492 if ((isrc->isrc_flags & (INTR_ISRCF_PPI | INTR_ISRCF_IPI)) == 0)
494 if (isrc->isrc_flags & INTR_ISRCF_BOUND)
495 return (CPU_ISSET(cpu, &isrc->isrc_cpu));
497 CPU_SET(cpu, &isrc->isrc_cpu);
504 * Setup filter into interrupt source.
507 iscr_setup_filter(struct intr_irqsrc *isrc, const char *name,
508 intr_irq_filter_t *filter, void *arg, void **cookiep)
514 mtx_lock(&isrc_table_lock);
516 * Make sure that we do not mix the two ways
517 * how we handle interrupt sources.
519 if (isrc->isrc_filter != NULL || isrc->isrc_event != NULL) {
520 mtx_unlock(&isrc_table_lock);
523 isrc->isrc_filter = filter;
524 isrc->isrc_arg = arg;
525 isrc_update_name(isrc, name);
526 mtx_unlock(&isrc_table_lock);
534 * Interrupt source pre_ithread method for MI interrupt framework.
537 intr_isrc_pre_ithread(void *arg)
539 struct intr_irqsrc *isrc = arg;
541 PIC_PRE_ITHREAD(isrc->isrc_dev, isrc);
545 * Interrupt source post_ithread method for MI interrupt framework.
548 intr_isrc_post_ithread(void *arg)
550 struct intr_irqsrc *isrc = arg;
552 PIC_POST_ITHREAD(isrc->isrc_dev, isrc);
556 * Interrupt source post_filter method for MI interrupt framework.
559 intr_isrc_post_filter(void *arg)
561 struct intr_irqsrc *isrc = arg;
563 PIC_POST_FILTER(isrc->isrc_dev, isrc);
567 * Interrupt source assign_cpu method for MI interrupt framework.
570 intr_isrc_assign_cpu(void *arg, int cpu)
573 struct intr_irqsrc *isrc = arg;
576 if (isrc->isrc_dev != intr_irq_root_dev)
579 mtx_lock(&isrc_table_lock);
581 CPU_ZERO(&isrc->isrc_cpu);
582 isrc->isrc_flags &= ~INTR_ISRCF_BOUND;
584 CPU_SETOF(cpu, &isrc->isrc_cpu);
585 isrc->isrc_flags |= INTR_ISRCF_BOUND;
589 * In NOCPU case, it's up to PIC to either leave ISRC on same CPU or
590 * re-balance it to another CPU or enable it on more CPUs. However,
591 * PIC is expected to change isrc_cpu appropriately to keep us well
592 * informed if the call is successful.
594 if (irq_assign_cpu) {
595 error = PIC_BIND_INTR(isrc->isrc_dev, isrc);
597 CPU_ZERO(&isrc->isrc_cpu);
598 mtx_unlock(&isrc_table_lock);
602 mtx_unlock(&isrc_table_lock);
610 * Create interrupt event for interrupt source.
613 isrc_event_create(struct intr_irqsrc *isrc)
615 struct intr_event *ie;
618 error = intr_event_create(&ie, isrc, 0, isrc->isrc_irq,
619 intr_isrc_pre_ithread, intr_isrc_post_ithread, intr_isrc_post_filter,
620 intr_isrc_assign_cpu, "%s:", isrc->isrc_name);
624 mtx_lock(&isrc_table_lock);
626 * Make sure that we do not mix the two ways
627 * how we handle interrupt sources. Let contested event wins.
630 if (isrc->isrc_filter != NULL || isrc->isrc_event != NULL) {
632 if (isrc->isrc_event != NULL) {
634 mtx_unlock(&isrc_table_lock);
635 intr_event_destroy(ie);
636 return (isrc->isrc_event != NULL ? EBUSY : 0);
638 isrc->isrc_event = ie;
639 mtx_unlock(&isrc_table_lock);
645 * Destroy interrupt event for interrupt source.
648 isrc_event_destroy(struct intr_irqsrc *isrc)
650 struct intr_event *ie;
652 mtx_lock(&isrc_table_lock);
653 ie = isrc->isrc_event;
654 isrc->isrc_event = NULL;
655 mtx_unlock(&isrc_table_lock);
658 intr_event_destroy(ie);
662 * Add handler to interrupt source.
665 isrc_add_handler(struct intr_irqsrc *isrc, const char *name,
666 driver_filter_t filter, driver_intr_t handler, void *arg,
667 enum intr_type flags, void **cookiep)
671 if (isrc->isrc_event == NULL) {
672 error = isrc_event_create(isrc);
677 error = intr_event_add_handler(isrc->isrc_event, name, filter, handler,
678 arg, intr_priority(flags), flags, cookiep);
680 mtx_lock(&isrc_table_lock);
681 intrcnt_updatename(isrc);
682 mtx_unlock(&isrc_table_lock);
689 * Lookup interrupt controller locked.
691 static inline struct intr_pic *
692 pic_lookup_locked(device_t dev, intptr_t xref)
694 struct intr_pic *pic;
696 mtx_assert(&pic_list_lock, MA_OWNED);
698 if (dev == NULL && xref == 0)
701 /* Note that pic->pic_dev is never NULL on registered PIC. */
702 SLIST_FOREACH(pic, &pic_list, pic_next) {
704 if (xref == pic->pic_xref)
706 } else if (xref == 0 || pic->pic_xref == 0) {
707 if (dev == pic->pic_dev)
709 } else if (xref == pic->pic_xref && dev == pic->pic_dev)
716 * Lookup interrupt controller.
718 static struct intr_pic *
719 pic_lookup(device_t dev, intptr_t xref)
721 struct intr_pic *pic;
723 mtx_lock(&pic_list_lock);
724 pic = pic_lookup_locked(dev, xref);
725 mtx_unlock(&pic_list_lock);
730 * Create interrupt controller.
732 static struct intr_pic *
733 pic_create(device_t dev, intptr_t xref)
735 struct intr_pic *pic;
737 mtx_lock(&pic_list_lock);
738 pic = pic_lookup_locked(dev, xref);
740 mtx_unlock(&pic_list_lock);
743 pic = malloc(sizeof(*pic), M_INTRNG, M_NOWAIT | M_ZERO);
745 mtx_unlock(&pic_list_lock);
748 pic->pic_xref = xref;
750 mtx_init(&pic->pic_child_lock, "pic child lock", NULL, MTX_SPIN);
751 SLIST_INSERT_HEAD(&pic_list, pic, pic_next);
752 mtx_unlock(&pic_list_lock);
758 * Destroy interrupt controller.
761 pic_destroy(device_t dev, intptr_t xref)
763 struct intr_pic *pic;
765 mtx_lock(&pic_list_lock);
766 pic = pic_lookup_locked(dev, xref);
768 mtx_unlock(&pic_list_lock);
771 SLIST_REMOVE(&pic_list, pic, intr_pic, pic_next);
772 mtx_unlock(&pic_list_lock);
778 * Register interrupt controller.
781 intr_pic_register(device_t dev, intptr_t xref)
783 struct intr_pic *pic;
787 pic = pic_create(dev, xref);
791 pic->pic_flags |= FLAG_PIC;
793 debugf("PIC %p registered for %s <dev %p, xref %x>\n", pic,
794 device_get_nameunit(dev), dev, xref);
799 * Unregister interrupt controller.
802 intr_pic_deregister(device_t dev, intptr_t xref)
805 panic("%s: not implemented", __func__);
809 * Mark interrupt controller (itself) as a root one.
811 * Note that only an interrupt controller can really know its position
812 * in interrupt controller's tree. So root PIC must claim itself as a root.
814 * In FDT case, according to ePAPR approved version 1.1 from 08 April 2011,
816 * "The root of the interrupt tree is determined when traversal
817 * of the interrupt tree reaches an interrupt controller node without
818 * an interrupts property and thus no explicit interrupt parent."
821 intr_pic_claim_root(device_t dev, intptr_t xref, intr_irq_filter_t *filter,
822 void *arg, u_int ipicount)
824 struct intr_pic *pic;
826 pic = pic_lookup(dev, xref);
828 device_printf(dev, "not registered\n");
832 KASSERT((pic->pic_flags & FLAG_PIC) != 0,
833 ("%s: Found a non-PIC controller: %s", __func__,
834 device_get_name(pic->pic_dev)));
836 if (filter == NULL) {
837 device_printf(dev, "filter missing\n");
842 * Only one interrupt controllers could be on the root for now.
843 * Note that we further suppose that there is not threaded interrupt
844 * routine (handler) on the root. See intr_irq_handler().
846 if (intr_irq_root_dev != NULL) {
847 device_printf(dev, "another root already set\n");
851 intr_irq_root_dev = dev;
852 irq_root_filter = filter;
854 irq_root_ipicount = ipicount;
856 debugf("irq root set to %s\n", device_get_nameunit(dev));
861 * Add a handler to manage a sub range of a parents interrupts.
864 intr_pic_add_handler(device_t parent, struct intr_pic *pic,
865 intr_child_irq_filter_t *filter, void *arg, uintptr_t start,
868 struct intr_pic *parent_pic;
869 struct intr_pic_child *newchild;
871 struct intr_pic_child *child;
874 parent_pic = pic_lookup(parent, 0);
875 if (parent_pic == NULL)
878 newchild = malloc(sizeof(*newchild), M_INTRNG, M_WAITOK | M_ZERO);
879 newchild->pc_pic = pic;
880 newchild->pc_filter = filter;
881 newchild->pc_filter_arg = arg;
882 newchild->pc_start = start;
883 newchild->pc_length = length;
885 mtx_lock_spin(&parent_pic->pic_child_lock);
887 SLIST_FOREACH(child, &parent_pic->pic_children, pc_next) {
888 KASSERT(child->pc_pic != pic, ("%s: Adding a child PIC twice",
892 SLIST_INSERT_HEAD(&parent_pic->pic_children, newchild, pc_next);
893 mtx_unlock_spin(&parent_pic->pic_child_lock);
899 intr_resolve_irq(device_t dev, intptr_t xref, struct intr_map_data *data,
900 struct intr_irqsrc **isrc)
902 struct intr_pic *pic;
903 struct intr_map_data_msi *msi;
908 pic = pic_lookup(dev, xref);
912 switch (data->type) {
913 case INTR_MAP_DATA_MSI:
914 KASSERT((pic->pic_flags & FLAG_MSI) != 0,
915 ("%s: Found a non-MSI controller: %s", __func__,
916 device_get_name(pic->pic_dev)));
917 msi = (struct intr_map_data_msi *)data;
922 KASSERT((pic->pic_flags & FLAG_PIC) != 0,
923 ("%s: Found a non-PIC controller: %s", __func__,
924 device_get_name(pic->pic_dev)));
925 return (PIC_MAP_INTR(pic->pic_dev, data, isrc));
931 intr_activate_irq(device_t dev, struct resource *res)
935 struct intr_map_data *data;
936 struct intr_irqsrc *isrc;
940 KASSERT(rman_get_start(res) == rman_get_end(res),
941 ("%s: more interrupts in resource", __func__));
943 res_id = (u_int)rman_get_start(res);
944 if (intr_map_get_isrc(res_id) != NULL)
945 panic("Attempt to double activation of resource id: %u\n",
947 intr_map_copy_map_data(res_id, &map_dev, &map_xref, &data);
948 error = intr_resolve_irq(map_dev, map_xref, data, &isrc);
950 free(data, M_INTRNG);
951 /* XXX TODO DISCONECTED PICs */
952 /* if (error == EINVAL) return(0); */
955 intr_map_set_isrc(res_id, isrc);
956 rman_set_virtual(res, data);
957 return (PIC_ACTIVATE_INTR(isrc->isrc_dev, isrc, res, data));
961 intr_deactivate_irq(device_t dev, struct resource *res)
963 struct intr_map_data *data;
964 struct intr_irqsrc *isrc;
968 KASSERT(rman_get_start(res) == rman_get_end(res),
969 ("%s: more interrupts in resource", __func__));
971 res_id = (u_int)rman_get_start(res);
972 isrc = intr_map_get_isrc(res_id);
974 panic("Attempt to deactivate non-active resource id: %u\n",
977 data = rman_get_virtual(res);
978 error = PIC_DEACTIVATE_INTR(isrc->isrc_dev, isrc, res, data);
979 intr_map_set_isrc(res_id, NULL);
980 rman_set_virtual(res, NULL);
981 free(data, M_INTRNG);
986 intr_setup_irq(device_t dev, struct resource *res, driver_filter_t filt,
987 driver_intr_t hand, void *arg, int flags, void **cookiep)
990 struct intr_map_data *data;
991 struct intr_irqsrc *isrc;
995 KASSERT(rman_get_start(res) == rman_get_end(res),
996 ("%s: more interrupts in resource", __func__));
998 res_id = (u_int)rman_get_start(res);
999 isrc = intr_map_get_isrc(res_id);
1001 /* XXX TODO DISCONECTED PICs */
1005 data = rman_get_virtual(res);
1006 name = device_get_nameunit(dev);
1010 * Standard handling is done through MI interrupt framework. However,
1011 * some interrupts could request solely own special handling. This
1012 * non standard handling can be used for interrupt controllers without
1013 * handler (filter only), so in case that interrupt controllers are
1014 * chained, MI interrupt framework is called only in leaf controller.
1016 * Note that root interrupt controller routine is served as well,
1017 * however in intr_irq_handler(), i.e. main system dispatch routine.
1019 if (flags & INTR_SOLO && hand != NULL) {
1020 debugf("irq %u cannot solo on %s\n", irq, name);
1024 if (flags & INTR_SOLO) {
1025 error = iscr_setup_filter(isrc, name, (intr_irq_filter_t *)filt,
1027 debugf("irq %u setup filter error %d on %s\n", isrc->isrc_irq, error,
1032 error = isrc_add_handler(isrc, name, filt, hand, arg, flags,
1034 debugf("irq %u add handler error %d on %s\n", isrc->isrc_irq, error, name);
1039 mtx_lock(&isrc_table_lock);
1040 error = PIC_SETUP_INTR(isrc->isrc_dev, isrc, res, data);
1042 isrc->isrc_handlers++;
1043 if (isrc->isrc_handlers == 1)
1044 PIC_ENABLE_INTR(isrc->isrc_dev, isrc);
1046 mtx_unlock(&isrc_table_lock);
1048 intr_event_remove_handler(*cookiep);
1053 intr_teardown_irq(device_t dev, struct resource *res, void *cookie)
1056 struct intr_map_data *data;
1057 struct intr_irqsrc *isrc;
1060 KASSERT(rman_get_start(res) == rman_get_end(res),
1061 ("%s: more interrupts in resource", __func__));
1063 res_id = (u_int)rman_get_start(res);
1064 isrc = intr_map_get_isrc(res_id);
1065 if (isrc == NULL || isrc->isrc_handlers == 0)
1068 data = rman_get_virtual(res);
1071 if (isrc->isrc_filter != NULL) {
1075 mtx_lock(&isrc_table_lock);
1076 isrc->isrc_filter = NULL;
1077 isrc->isrc_arg = NULL;
1078 isrc->isrc_handlers = 0;
1079 PIC_DISABLE_INTR(isrc->isrc_dev, isrc);
1080 PIC_TEARDOWN_INTR(isrc->isrc_dev, isrc, res, data);
1081 isrc_update_name(isrc, NULL);
1082 mtx_unlock(&isrc_table_lock);
1086 if (isrc != intr_handler_source(cookie))
1089 error = intr_event_remove_handler(cookie);
1091 mtx_lock(&isrc_table_lock);
1092 isrc->isrc_handlers--;
1093 if (isrc->isrc_handlers == 0)
1094 PIC_DISABLE_INTR(isrc->isrc_dev, isrc);
1095 PIC_TEARDOWN_INTR(isrc->isrc_dev, isrc, res, data);
1096 intrcnt_updatename(isrc);
1097 mtx_unlock(&isrc_table_lock);
1103 intr_describe_irq(device_t dev, struct resource *res, void *cookie,
1107 struct intr_irqsrc *isrc;
1110 KASSERT(rman_get_start(res) == rman_get_end(res),
1111 ("%s: more interrupts in resource", __func__));
1113 res_id = (u_int)rman_get_start(res);
1114 isrc = intr_map_get_isrc(res_id);
1115 if (isrc == NULL || isrc->isrc_handlers == 0)
1118 if (isrc->isrc_filter != NULL) {
1122 mtx_lock(&isrc_table_lock);
1123 isrc_update_name(isrc, descr);
1124 mtx_unlock(&isrc_table_lock);
1128 error = intr_event_describe_handler(isrc->isrc_event, cookie, descr);
1130 mtx_lock(&isrc_table_lock);
1131 intrcnt_updatename(isrc);
1132 mtx_unlock(&isrc_table_lock);
1139 intr_bind_irq(device_t dev, struct resource *res, int cpu)
1141 struct intr_irqsrc *isrc;
1144 KASSERT(rman_get_start(res) == rman_get_end(res),
1145 ("%s: more interrupts in resource", __func__));
1147 res_id = (u_int)rman_get_start(res);
1148 isrc = intr_map_get_isrc(res_id);
1149 if (isrc == NULL || isrc->isrc_handlers == 0)
1152 if (isrc->isrc_filter != NULL)
1153 return (intr_isrc_assign_cpu(isrc, cpu));
1155 return (intr_event_bind(isrc->isrc_event, cpu));
1159 * Return the CPU that the next interrupt source should use.
1160 * For now just returns the next CPU according to round-robin.
1163 intr_irq_next_cpu(u_int last_cpu, cpuset_t *cpumask)
1166 if (!irq_assign_cpu || mp_ncpus == 1)
1167 return (PCPU_GET(cpuid));
1171 if (last_cpu > mp_maxid)
1173 } while (!CPU_ISSET(last_cpu, cpumask));
1178 * Distribute all the interrupt sources among the available
1179 * CPUs once the AP's have been launched.
1182 intr_irq_shuffle(void *arg __unused)
1184 struct intr_irqsrc *isrc;
1190 mtx_lock(&isrc_table_lock);
1191 irq_assign_cpu = TRUE;
1192 for (i = 0; i < NIRQ; i++) {
1193 isrc = irq_sources[i];
1194 if (isrc == NULL || isrc->isrc_handlers == 0 ||
1195 isrc->isrc_flags & (INTR_ISRCF_PPI | INTR_ISRCF_IPI))
1198 if (isrc->isrc_event != NULL &&
1199 isrc->isrc_flags & INTR_ISRCF_BOUND &&
1200 isrc->isrc_event->ie_cpu != CPU_FFS(&isrc->isrc_cpu) - 1)
1201 panic("%s: CPU inconsistency", __func__);
1203 if ((isrc->isrc_flags & INTR_ISRCF_BOUND) == 0)
1204 CPU_ZERO(&isrc->isrc_cpu); /* start again */
1207 * We are in wicked position here if the following call fails
1208 * for bound ISRC. The best thing we can do is to clear
1209 * isrc_cpu so inconsistency with ie_cpu will be detectable.
1211 if (PIC_BIND_INTR(isrc->isrc_dev, isrc) != 0)
1212 CPU_ZERO(&isrc->isrc_cpu);
1214 mtx_unlock(&isrc_table_lock);
1216 SYSINIT(intr_irq_shuffle, SI_SUB_SMP, SI_ORDER_SECOND, intr_irq_shuffle, NULL);
1220 intr_irq_next_cpu(u_int current_cpu, cpuset_t *cpumask)
1223 return (PCPU_GET(cpuid));
1228 * Allocate memory for new intr_map_data structure.
1229 * Initialize common fields.
1231 struct intr_map_data *
1232 intr_alloc_map_data(enum intr_map_data_type type, size_t len, int flags)
1234 struct intr_map_data *data;
1236 data = malloc(len, M_INTRNG, flags);
1242 void intr_free_intr_map_data(struct intr_map_data *data)
1245 free(data, M_INTRNG);
1250 * Register a MSI/MSI-X interrupt controller
1253 intr_msi_register(device_t dev, intptr_t xref)
1255 struct intr_pic *pic;
1259 pic = pic_create(dev, xref);
1263 pic->pic_flags |= FLAG_MSI;
1265 debugf("PIC %p registered for %s <dev %p, xref %jx>\n", pic,
1266 device_get_nameunit(dev), dev, (uintmax_t)xref);
1271 intr_alloc_msi(device_t pci, device_t child, intptr_t xref, int count,
1272 int maxcount, int *irqs)
1274 struct intr_irqsrc **isrc;
1275 struct intr_pic *pic;
1277 struct intr_map_data_msi *msi;
1280 pic = pic_lookup(NULL, xref);
1284 KASSERT((pic->pic_flags & FLAG_MSI) != 0,
1285 ("%s: Found a non-MSI controller: %s", __func__,
1286 device_get_name(pic->pic_dev)));
1288 isrc = malloc(sizeof(*isrc) * count, M_INTRNG, M_WAITOK);
1289 err = MSI_ALLOC_MSI(pic->pic_dev, child, count, maxcount, &pdev, isrc);
1291 free(isrc, M_INTRNG);
1295 for (i = 0; i < count; i++) {
1296 msi = (struct intr_map_data_msi *)intr_alloc_map_data(
1297 INTR_MAP_DATA_MSI, sizeof(*msi), M_WAITOK | M_ZERO);
1298 msi-> isrc = isrc[i];
1299 irqs[i] = intr_map_irq(pic->pic_dev, xref,
1300 (struct intr_map_data *)msi);
1303 free(isrc, M_INTRNG);
1309 intr_release_msi(device_t pci, device_t child, intptr_t xref, int count,
1312 struct intr_irqsrc **isrc;
1313 struct intr_pic *pic;
1314 struct intr_map_data_msi *msi;
1317 pic = pic_lookup(NULL, xref);
1321 KASSERT((pic->pic_flags & FLAG_MSI) != 0,
1322 ("%s: Found a non-MSI controller: %s", __func__,
1323 device_get_name(pic->pic_dev)));
1325 isrc = malloc(sizeof(*isrc) * count, M_INTRNG, M_WAITOK);
1327 for (i = 0; i < count; i++) {
1328 msi = (struct intr_map_data_msi *)
1329 intr_map_get_map_data(irqs[i]);
1330 KASSERT(msi->hdr.type == INTR_MAP_DATA_MSI,
1331 ("%s: irq %d map data is not MSI", __func__,
1333 isrc[i] = msi->isrc;
1336 err = MSI_RELEASE_MSI(pic->pic_dev, child, count, isrc);
1338 for (i = 0; i < count; i++) {
1339 if (isrc[i] != NULL)
1340 intr_unmap_irq(irqs[i]);
1343 free(isrc, M_INTRNG);
1348 intr_alloc_msix(device_t pci, device_t child, intptr_t xref, int *irq)
1350 struct intr_irqsrc *isrc;
1351 struct intr_pic *pic;
1353 struct intr_map_data_msi *msi;
1356 pic = pic_lookup(NULL, xref);
1360 KASSERT((pic->pic_flags & FLAG_MSI) != 0,
1361 ("%s: Found a non-MSI controller: %s", __func__,
1362 device_get_name(pic->pic_dev)));
1365 err = MSI_ALLOC_MSIX(pic->pic_dev, child, &pdev, &isrc);
1369 msi = (struct intr_map_data_msi *)intr_alloc_map_data(
1370 INTR_MAP_DATA_MSI, sizeof(*msi), M_WAITOK | M_ZERO);
1372 *irq = intr_map_irq(pic->pic_dev, xref, (struct intr_map_data *)msi);
1377 intr_release_msix(device_t pci, device_t child, intptr_t xref, int irq)
1379 struct intr_irqsrc *isrc;
1380 struct intr_pic *pic;
1381 struct intr_map_data_msi *msi;
1384 pic = pic_lookup(NULL, xref);
1388 KASSERT((pic->pic_flags & FLAG_MSI) != 0,
1389 ("%s: Found a non-MSI controller: %s", __func__,
1390 device_get_name(pic->pic_dev)));
1392 msi = (struct intr_map_data_msi *)
1393 intr_map_get_map_data(irq);
1394 KASSERT(msi->hdr.type == INTR_MAP_DATA_MSI,
1395 ("%s: irq %d map data is not MSI", __func__,
1399 intr_unmap_irq(irq);
1403 err = MSI_RELEASE_MSIX(pic->pic_dev, child, isrc);
1404 intr_unmap_irq(irq);
1410 intr_map_msi(device_t pci, device_t child, intptr_t xref, int irq,
1411 uint64_t *addr, uint32_t *data)
1413 struct intr_irqsrc *isrc;
1414 struct intr_pic *pic;
1417 pic = pic_lookup(NULL, xref);
1421 KASSERT((pic->pic_flags & FLAG_MSI) != 0,
1422 ("%s: Found a non-MSI controller: %s", __func__,
1423 device_get_name(pic->pic_dev)));
1425 isrc = intr_map_get_isrc(irq);
1429 err = MSI_MAP_MSI(pic->pic_dev, child, isrc, addr, data);
1434 void dosoftints(void);
1442 * Init interrupt controller on another CPU.
1445 intr_pic_init_secondary(void)
1449 * QQQ: Only root PIC is aware of other CPUs ???
1451 KASSERT(intr_irq_root_dev != NULL, ("%s: no root attached", __func__));
1453 //mtx_lock(&isrc_table_lock);
1454 PIC_INIT_SECONDARY(intr_irq_root_dev);
1455 //mtx_unlock(&isrc_table_lock);
1460 DB_SHOW_COMMAND(irqs, db_show_irqs)
1464 struct intr_irqsrc *isrc;
1466 for (irqsum = 0, i = 0; i < NIRQ; i++) {
1467 isrc = irq_sources[i];
1471 num = isrc->isrc_count != NULL ? isrc->isrc_count[0] : 0;
1472 db_printf("irq%-3u <%s>: cpu %02lx%s cnt %lu\n", i,
1473 isrc->isrc_name, isrc->isrc_cpu.__bits[0],
1474 isrc->isrc_flags & INTR_ISRCF_BOUND ? " (bound)" : "", num);
1477 db_printf("irq total %u\n", irqsum);
1482 * Interrupt mapping table functions.
1484 * Please, keep this part separately, it can be transformed to
1485 * extension of standard resources.
1487 struct intr_map_entry
1491 struct intr_map_data *map_data;
1492 struct intr_irqsrc *isrc;
1493 /* XXX TODO DISCONECTED PICs */
1497 /* XXX Convert irq_map[] to dynamicaly expandable one. */
1498 static struct intr_map_entry *irq_map[2 * NIRQ];
1499 static int irq_map_count = nitems(irq_map);
1500 static int irq_map_first_free_idx;
1501 static struct mtx irq_map_lock;
1503 static struct intr_irqsrc *
1504 intr_map_get_isrc(u_int res_id)
1506 struct intr_irqsrc *isrc;
1508 mtx_lock(&irq_map_lock);
1509 if ((res_id >= irq_map_count) || (irq_map[res_id] == NULL)) {
1510 mtx_unlock(&irq_map_lock);
1513 isrc = irq_map[res_id]->isrc;
1514 mtx_unlock(&irq_map_lock);
1519 intr_map_set_isrc(u_int res_id, struct intr_irqsrc *isrc)
1522 mtx_lock(&irq_map_lock);
1523 if ((res_id >= irq_map_count) || (irq_map[res_id] == NULL)) {
1524 mtx_unlock(&irq_map_lock);
1527 irq_map[res_id]->isrc = isrc;
1528 mtx_unlock(&irq_map_lock);
1532 * Get a copy of intr_map_entry data
1534 static struct intr_map_data *
1535 intr_map_get_map_data(u_int res_id)
1537 struct intr_map_data *data;
1540 mtx_lock(&irq_map_lock);
1541 if (res_id >= irq_map_count || irq_map[res_id] == NULL)
1542 panic("Attempt to copy invalid resource id: %u\n", res_id);
1543 data = irq_map[res_id]->map_data;
1544 mtx_unlock(&irq_map_lock);
1550 * Get a copy of intr_map_entry data
1553 intr_map_copy_map_data(u_int res_id, device_t *map_dev, intptr_t *map_xref,
1554 struct intr_map_data **data)
1559 mtx_lock(&irq_map_lock);
1560 if (res_id >= irq_map_count || irq_map[res_id] == NULL)
1561 panic("Attempt to copy invalid resource id: %u\n", res_id);
1562 if (irq_map[res_id]->map_data != NULL)
1563 len = irq_map[res_id]->map_data->len;
1564 mtx_unlock(&irq_map_lock);
1569 *data = malloc(len, M_INTRNG, M_WAITOK | M_ZERO);
1570 mtx_lock(&irq_map_lock);
1571 if (irq_map[res_id] == NULL)
1572 panic("Attempt to copy invalid resource id: %u\n", res_id);
1574 if (len != irq_map[res_id]->map_data->len)
1575 panic("Resource id: %u has changed.\n", res_id);
1576 memcpy(*data, irq_map[res_id]->map_data, len);
1578 *map_dev = irq_map[res_id]->dev;
1579 *map_xref = irq_map[res_id]->xref;
1580 mtx_unlock(&irq_map_lock);
1585 * Allocate and fill new entry in irq_map table.
1588 intr_map_irq(device_t dev, intptr_t xref, struct intr_map_data *data)
1591 struct intr_map_entry *entry;
1593 /* Prepare new entry first. */
1594 entry = malloc(sizeof(*entry), M_INTRNG, M_WAITOK | M_ZERO);
1598 entry->map_data = data;
1601 mtx_lock(&irq_map_lock);
1602 for (i = irq_map_first_free_idx; i < irq_map_count; i++) {
1603 if (irq_map[i] == NULL) {
1605 irq_map_first_free_idx = i + 1;
1606 mtx_unlock(&irq_map_lock);
1610 mtx_unlock(&irq_map_lock);
1612 /* XXX Expand irq_map table */
1613 panic("IRQ mapping table is full.");
1617 * Remove and free mapping entry.
1620 intr_unmap_irq(u_int res_id)
1622 struct intr_map_entry *entry;
1624 mtx_lock(&irq_map_lock);
1625 if ((res_id >= irq_map_count) || (irq_map[res_id] == NULL))
1626 panic("Attempt to unmap invalid resource id: %u\n", res_id);
1627 entry = irq_map[res_id];
1628 irq_map[res_id] = NULL;
1629 irq_map_first_free_idx = res_id;
1630 mtx_unlock(&irq_map_lock);
1631 intr_free_intr_map_data(entry->map_data);
1632 free(entry, M_INTRNG);
1636 * Clone mapping entry.
1639 intr_map_clone_irq(u_int old_res_id)
1643 struct intr_map_data *data;
1645 intr_map_copy_map_data(old_res_id, &map_dev, &map_xref, &data);
1646 return (intr_map_irq(map_dev, map_xref, data));
1650 intr_map_init(void *dummy __unused)
1653 mtx_init(&irq_map_lock, "intr map table", NULL, MTX_DEF);
1655 SYSINIT(intr_map_init, SI_SUB_INTR, SI_ORDER_FIRST, intr_map_init, NULL);