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>
46 #include <sys/mutex.h>
47 #include <sys/syslog.h>
48 #include <sys/malloc.h>
50 #include <sys/queue.h>
52 #include <sys/interrupt.h>
54 #include <sys/cpuset.h>
56 #include <sys/sched.h>
58 #include <sys/vmmeter.h>
60 #include <sys/pmckern.h>
63 #include <machine/atomic.h>
64 #include <machine/intr.h>
65 #include <machine/cpu.h>
66 #include <machine/smp.h>
67 #include <machine/stdarg.h>
76 #define INTRNAME_LEN (2*MAXCOMLEN + 1)
79 #define debugf(fmt, args...) do { printf("%s(): ", __func__); \
80 printf(fmt,##args); } while (0)
82 #define debugf(fmt, args...)
85 MALLOC_DECLARE(M_INTRNG);
86 MALLOC_DEFINE(M_INTRNG, "intr", "intr interrupt handling");
88 /* Main interrupt handler called from assembler -> 'hidden' for C code. */
89 void intr_irq_handler(struct trapframe *tf);
91 /* Root interrupt controller stuff. */
92 device_t intr_irq_root_dev;
93 static intr_irq_filter_t *irq_root_filter;
94 static void *irq_root_arg;
95 static u_int irq_root_ipicount;
97 struct intr_pic_child {
98 SLIST_ENTRY(intr_pic_child) pc_next;
99 struct intr_pic *pc_pic;
100 intr_child_irq_filter_t *pc_filter;
106 /* Interrupt controller definition. */
108 SLIST_ENTRY(intr_pic) pic_next;
109 intptr_t pic_xref; /* hardware identification */
111 /* Only one of FLAG_PIC or FLAG_MSI may be set */
112 #define FLAG_PIC (1 << 0)
113 #define FLAG_MSI (1 << 1)
114 #define FLAG_TYPE_MASK (FLAG_PIC | FLAG_MSI)
116 struct mtx pic_child_lock;
117 SLIST_HEAD(, intr_pic_child) pic_children;
120 static struct mtx pic_list_lock;
121 static SLIST_HEAD(, intr_pic) pic_list;
123 static struct intr_pic *pic_lookup(device_t dev, intptr_t xref, int flags);
125 /* Interrupt source definition. */
126 static struct mtx isrc_table_lock;
127 static struct intr_irqsrc *irq_sources[NIRQ];
131 static boolean_t irq_assign_cpu = FALSE;
135 * - 2 counters for each I/O interrupt.
136 * - MAXCPU counters for each IPI counters for SMP.
139 #define INTRCNT_COUNT (NIRQ * 2 + INTR_IPI_COUNT * MAXCPU)
141 #define INTRCNT_COUNT (NIRQ * 2)
144 /* Data for MI statistics reporting. */
145 u_long intrcnt[INTRCNT_COUNT];
146 char intrnames[INTRCNT_COUNT * INTRNAME_LEN];
147 size_t sintrcnt = sizeof(intrcnt);
148 size_t sintrnames = sizeof(intrnames);
149 static u_int intrcnt_index;
151 static struct intr_irqsrc *intr_map_get_isrc(u_int res_id);
152 static void intr_map_set_isrc(u_int res_id, struct intr_irqsrc *isrc);
153 static struct intr_map_data * intr_map_get_map_data(u_int res_id);
154 static void intr_map_copy_map_data(u_int res_id, device_t *dev, intptr_t *xref,
155 struct intr_map_data **data);
158 * Interrupt framework initialization routine.
161 intr_irq_init(void *dummy __unused)
164 SLIST_INIT(&pic_list);
165 mtx_init(&pic_list_lock, "intr pic list", NULL, MTX_DEF);
167 mtx_init(&isrc_table_lock, "intr isrc table", NULL, MTX_DEF);
169 SYSINIT(intr_irq_init, SI_SUB_INTR, SI_ORDER_FIRST, intr_irq_init, NULL);
172 intrcnt_setname(const char *name, int index)
175 snprintf(intrnames + INTRNAME_LEN * index, INTRNAME_LEN, "%-*s",
176 INTRNAME_LEN - 1, name);
180 * Update name for interrupt source with interrupt event.
183 intrcnt_updatename(struct intr_irqsrc *isrc)
186 /* QQQ: What about stray counter name? */
187 mtx_assert(&isrc_table_lock, MA_OWNED);
188 intrcnt_setname(isrc->isrc_event->ie_fullname, isrc->isrc_index);
192 * Virtualization for interrupt source interrupt counter increment.
195 isrc_increment_count(struct intr_irqsrc *isrc)
198 if (isrc->isrc_flags & INTR_ISRCF_PPI)
199 atomic_add_long(&isrc->isrc_count[0], 1);
201 isrc->isrc_count[0]++;
205 * Virtualization for interrupt source interrupt stray counter increment.
208 isrc_increment_straycount(struct intr_irqsrc *isrc)
211 isrc->isrc_count[1]++;
215 * Virtualization for interrupt source interrupt name update.
218 isrc_update_name(struct intr_irqsrc *isrc, const char *name)
220 char str[INTRNAME_LEN];
222 mtx_assert(&isrc_table_lock, MA_OWNED);
225 snprintf(str, INTRNAME_LEN, "%s: %s", isrc->isrc_name, name);
226 intrcnt_setname(str, isrc->isrc_index);
227 snprintf(str, INTRNAME_LEN, "stray %s: %s", isrc->isrc_name,
229 intrcnt_setname(str, isrc->isrc_index + 1);
231 snprintf(str, INTRNAME_LEN, "%s:", isrc->isrc_name);
232 intrcnt_setname(str, isrc->isrc_index);
233 snprintf(str, INTRNAME_LEN, "stray %s:", isrc->isrc_name);
234 intrcnt_setname(str, isrc->isrc_index + 1);
239 * Virtualization for interrupt source interrupt counters setup.
242 isrc_setup_counters(struct intr_irqsrc *isrc)
247 * XXX - it does not work well with removable controllers and
248 * interrupt sources !!!
250 index = atomic_fetchadd_int(&intrcnt_index, 2);
251 isrc->isrc_index = index;
252 isrc->isrc_count = &intrcnt[index];
253 isrc_update_name(isrc, NULL);
257 * Virtualization for interrupt source interrupt counters release.
260 isrc_release_counters(struct intr_irqsrc *isrc)
263 panic("%s: not implemented", __func__);
268 * Virtualization for interrupt source IPI counters setup.
271 intr_ipi_setup_counters(const char *name)
274 char str[INTRNAME_LEN];
276 index = atomic_fetchadd_int(&intrcnt_index, MAXCPU);
277 for (i = 0; i < MAXCPU; i++) {
278 snprintf(str, INTRNAME_LEN, "cpu%d:%s", i, name);
279 intrcnt_setname(str, index + i);
281 return (&intrcnt[index]);
286 * Main interrupt dispatch handler. It's called straight
287 * from the assembler, where CPU interrupt is served.
290 intr_irq_handler(struct trapframe *tf)
292 struct trapframe * oldframe;
295 KASSERT(irq_root_filter != NULL, ("%s: no filter", __func__));
300 oldframe = td->td_intr_frame;
301 td->td_intr_frame = tf;
302 irq_root_filter(irq_root_arg);
303 td->td_intr_frame = oldframe;
306 if (pmc_hook && TRAPF_USERMODE(tf) &&
307 (PCPU_GET(curthread)->td_pflags & TDP_CALLCHAIN))
308 pmc_hook(PCPU_GET(curthread), PMC_FN_USER_CALLCHAIN, tf);
313 intr_child_irq_handler(struct intr_pic *parent, uintptr_t irq)
315 struct intr_pic_child *child;
319 mtx_lock_spin(&parent->pic_child_lock);
320 SLIST_FOREACH(child, &parent->pic_children, pc_next) {
321 if (child->pc_start <= irq &&
322 irq < (child->pc_start + child->pc_length)) {
327 mtx_unlock_spin(&parent->pic_child_lock);
330 return (child->pc_filter(child->pc_filter_arg, irq));
332 return (FILTER_STRAY);
336 * interrupt controller dispatch function for interrupts. It should
337 * be called straight from the interrupt controller, when associated interrupt
341 intr_isrc_dispatch(struct intr_irqsrc *isrc, struct trapframe *tf)
344 KASSERT(isrc != NULL, ("%s: no source", __func__));
346 isrc_increment_count(isrc);
349 if (isrc->isrc_filter != NULL) {
351 error = isrc->isrc_filter(isrc->isrc_arg, tf);
352 PIC_POST_FILTER(isrc->isrc_dev, isrc);
353 if (error == FILTER_HANDLED)
357 if (isrc->isrc_event != NULL) {
358 if (intr_event_handle(isrc->isrc_event, tf) == 0)
362 isrc_increment_straycount(isrc);
367 * Alloc unique interrupt number (resource handle) for interrupt source.
369 * There could be various strategies how to allocate free interrupt number
370 * (resource handle) for new interrupt source.
372 * 1. Handles are always allocated forward, so handles are not recycled
373 * immediately. However, if only one free handle left which is reused
377 isrc_alloc_irq(struct intr_irqsrc *isrc)
381 mtx_assert(&isrc_table_lock, MA_OWNED);
383 maxirqs = nitems(irq_sources);
384 if (irq_next_free >= maxirqs)
387 for (irq = irq_next_free; irq < maxirqs; irq++) {
388 if (irq_sources[irq] == NULL)
391 for (irq = 0; irq < irq_next_free; irq++) {
392 if (irq_sources[irq] == NULL)
396 irq_next_free = maxirqs;
400 isrc->isrc_irq = irq;
401 irq_sources[irq] = isrc;
403 irq_next_free = irq + 1;
404 if (irq_next_free >= maxirqs)
410 * Free unique interrupt number (resource handle) from interrupt source.
413 isrc_free_irq(struct intr_irqsrc *isrc)
416 mtx_assert(&isrc_table_lock, MA_OWNED);
418 if (isrc->isrc_irq >= nitems(irq_sources))
420 if (irq_sources[isrc->isrc_irq] != isrc)
423 irq_sources[isrc->isrc_irq] = NULL;
424 isrc->isrc_irq = INTR_IRQ_INVALID; /* just to be safe */
429 * Initialize interrupt source and register it into global interrupt table.
432 intr_isrc_register(struct intr_irqsrc *isrc, device_t dev, u_int flags,
433 const char *fmt, ...)
438 bzero(isrc, sizeof(struct intr_irqsrc));
439 isrc->isrc_dev = dev;
440 isrc->isrc_irq = INTR_IRQ_INVALID; /* just to be safe */
441 isrc->isrc_flags = flags;
444 vsnprintf(isrc->isrc_name, INTR_ISRC_NAMELEN, fmt, ap);
447 mtx_lock(&isrc_table_lock);
448 error = isrc_alloc_irq(isrc);
450 mtx_unlock(&isrc_table_lock);
454 * Setup interrupt counters, but not for IPI sources. Those are setup
455 * later and only for used ones (up to INTR_IPI_COUNT) to not exhaust
458 if ((isrc->isrc_flags & INTR_ISRCF_IPI) == 0)
459 isrc_setup_counters(isrc);
460 mtx_unlock(&isrc_table_lock);
465 * Deregister interrupt source from global interrupt table.
468 intr_isrc_deregister(struct intr_irqsrc *isrc)
472 mtx_lock(&isrc_table_lock);
473 if ((isrc->isrc_flags & INTR_ISRCF_IPI) == 0)
474 isrc_release_counters(isrc);
475 error = isrc_free_irq(isrc);
476 mtx_unlock(&isrc_table_lock);
482 * A support function for a PIC to decide if provided ISRC should be inited
483 * on given cpu. The logic of INTR_ISRCF_BOUND flag and isrc_cpu member of
484 * struct intr_irqsrc is the following:
486 * If INTR_ISRCF_BOUND is set, the ISRC should be inited only on cpus
487 * set in isrc_cpu. If not, the ISRC should be inited on every cpu and
488 * isrc_cpu is kept consistent with it. Thus isrc_cpu is always correct.
491 intr_isrc_init_on_cpu(struct intr_irqsrc *isrc, u_int cpu)
494 if (isrc->isrc_handlers == 0)
496 if ((isrc->isrc_flags & (INTR_ISRCF_PPI | INTR_ISRCF_IPI)) == 0)
498 if (isrc->isrc_flags & INTR_ISRCF_BOUND)
499 return (CPU_ISSET(cpu, &isrc->isrc_cpu));
501 CPU_SET(cpu, &isrc->isrc_cpu);
508 * Setup filter into interrupt source.
511 iscr_setup_filter(struct intr_irqsrc *isrc, const char *name,
512 intr_irq_filter_t *filter, void *arg, void **cookiep)
518 mtx_lock(&isrc_table_lock);
520 * Make sure that we do not mix the two ways
521 * how we handle interrupt sources.
523 if (isrc->isrc_filter != NULL || isrc->isrc_event != NULL) {
524 mtx_unlock(&isrc_table_lock);
527 isrc->isrc_filter = filter;
528 isrc->isrc_arg = arg;
529 isrc_update_name(isrc, name);
530 mtx_unlock(&isrc_table_lock);
538 * Interrupt source pre_ithread method for MI interrupt framework.
541 intr_isrc_pre_ithread(void *arg)
543 struct intr_irqsrc *isrc = arg;
545 PIC_PRE_ITHREAD(isrc->isrc_dev, isrc);
549 * Interrupt source post_ithread method for MI interrupt framework.
552 intr_isrc_post_ithread(void *arg)
554 struct intr_irqsrc *isrc = arg;
556 PIC_POST_ITHREAD(isrc->isrc_dev, isrc);
560 * Interrupt source post_filter method for MI interrupt framework.
563 intr_isrc_post_filter(void *arg)
565 struct intr_irqsrc *isrc = arg;
567 PIC_POST_FILTER(isrc->isrc_dev, isrc);
571 * Interrupt source assign_cpu method for MI interrupt framework.
574 intr_isrc_assign_cpu(void *arg, int cpu)
577 struct intr_irqsrc *isrc = arg;
580 if (isrc->isrc_dev != intr_irq_root_dev)
583 mtx_lock(&isrc_table_lock);
585 CPU_ZERO(&isrc->isrc_cpu);
586 isrc->isrc_flags &= ~INTR_ISRCF_BOUND;
588 CPU_SETOF(cpu, &isrc->isrc_cpu);
589 isrc->isrc_flags |= INTR_ISRCF_BOUND;
593 * In NOCPU case, it's up to PIC to either leave ISRC on same CPU or
594 * re-balance it to another CPU or enable it on more CPUs. However,
595 * PIC is expected to change isrc_cpu appropriately to keep us well
596 * informed if the call is successful.
598 if (irq_assign_cpu) {
599 error = PIC_BIND_INTR(isrc->isrc_dev, isrc);
601 CPU_ZERO(&isrc->isrc_cpu);
602 mtx_unlock(&isrc_table_lock);
606 mtx_unlock(&isrc_table_lock);
614 * Create interrupt event for interrupt source.
617 isrc_event_create(struct intr_irqsrc *isrc)
619 struct intr_event *ie;
622 error = intr_event_create(&ie, isrc, 0, isrc->isrc_irq,
623 intr_isrc_pre_ithread, intr_isrc_post_ithread, intr_isrc_post_filter,
624 intr_isrc_assign_cpu, "%s:", isrc->isrc_name);
628 mtx_lock(&isrc_table_lock);
630 * Make sure that we do not mix the two ways
631 * how we handle interrupt sources. Let contested event wins.
634 if (isrc->isrc_filter != NULL || isrc->isrc_event != NULL) {
636 if (isrc->isrc_event != NULL) {
638 mtx_unlock(&isrc_table_lock);
639 intr_event_destroy(ie);
640 return (isrc->isrc_event != NULL ? EBUSY : 0);
642 isrc->isrc_event = ie;
643 mtx_unlock(&isrc_table_lock);
649 * Destroy interrupt event for interrupt source.
652 isrc_event_destroy(struct intr_irqsrc *isrc)
654 struct intr_event *ie;
656 mtx_lock(&isrc_table_lock);
657 ie = isrc->isrc_event;
658 isrc->isrc_event = NULL;
659 mtx_unlock(&isrc_table_lock);
662 intr_event_destroy(ie);
666 * Add handler to interrupt source.
669 isrc_add_handler(struct intr_irqsrc *isrc, const char *name,
670 driver_filter_t filter, driver_intr_t handler, void *arg,
671 enum intr_type flags, void **cookiep)
675 if (isrc->isrc_event == NULL) {
676 error = isrc_event_create(isrc);
681 error = intr_event_add_handler(isrc->isrc_event, name, filter, handler,
682 arg, intr_priority(flags), flags, cookiep);
684 mtx_lock(&isrc_table_lock);
685 intrcnt_updatename(isrc);
686 mtx_unlock(&isrc_table_lock);
693 * Lookup interrupt controller locked.
695 static inline struct intr_pic *
696 pic_lookup_locked(device_t dev, intptr_t xref, int flags)
698 struct intr_pic *pic;
700 mtx_assert(&pic_list_lock, MA_OWNED);
702 if (dev == NULL && xref == 0)
705 /* Note that pic->pic_dev is never NULL on registered PIC. */
706 SLIST_FOREACH(pic, &pic_list, pic_next) {
707 if ((pic->pic_flags & FLAG_TYPE_MASK) !=
708 (flags & FLAG_TYPE_MASK))
712 if (xref == pic->pic_xref)
714 } else if (xref == 0 || pic->pic_xref == 0) {
715 if (dev == pic->pic_dev)
717 } else if (xref == pic->pic_xref && dev == pic->pic_dev)
724 * Lookup interrupt controller.
726 static struct intr_pic *
727 pic_lookup(device_t dev, intptr_t xref, int flags)
729 struct intr_pic *pic;
731 mtx_lock(&pic_list_lock);
732 pic = pic_lookup_locked(dev, xref, flags);
733 mtx_unlock(&pic_list_lock);
738 * Create interrupt controller.
740 static struct intr_pic *
741 pic_create(device_t dev, intptr_t xref, int flags)
743 struct intr_pic *pic;
745 mtx_lock(&pic_list_lock);
746 pic = pic_lookup_locked(dev, xref, flags);
748 mtx_unlock(&pic_list_lock);
751 pic = malloc(sizeof(*pic), M_INTRNG, M_NOWAIT | M_ZERO);
753 mtx_unlock(&pic_list_lock);
756 pic->pic_xref = xref;
758 pic->pic_flags = flags;
759 mtx_init(&pic->pic_child_lock, "pic child lock", NULL, MTX_SPIN);
760 SLIST_INSERT_HEAD(&pic_list, pic, pic_next);
761 mtx_unlock(&pic_list_lock);
767 * Destroy interrupt controller.
770 pic_destroy(device_t dev, intptr_t xref, int flags)
772 struct intr_pic *pic;
774 mtx_lock(&pic_list_lock);
775 pic = pic_lookup_locked(dev, xref, flags);
777 mtx_unlock(&pic_list_lock);
780 SLIST_REMOVE(&pic_list, pic, intr_pic, pic_next);
781 mtx_unlock(&pic_list_lock);
787 * Register interrupt controller.
790 intr_pic_register(device_t dev, intptr_t xref)
792 struct intr_pic *pic;
796 pic = pic_create(dev, xref, FLAG_PIC);
800 debugf("PIC %p registered for %s <dev %p, xref %x>\n", pic,
801 device_get_nameunit(dev), dev, xref);
806 * Unregister interrupt controller.
809 intr_pic_deregister(device_t dev, intptr_t xref)
812 panic("%s: not implemented", __func__);
816 * Mark interrupt controller (itself) as a root one.
818 * Note that only an interrupt controller can really know its position
819 * in interrupt controller's tree. So root PIC must claim itself as a root.
821 * In FDT case, according to ePAPR approved version 1.1 from 08 April 2011,
823 * "The root of the interrupt tree is determined when traversal
824 * of the interrupt tree reaches an interrupt controller node without
825 * an interrupts property and thus no explicit interrupt parent."
828 intr_pic_claim_root(device_t dev, intptr_t xref, intr_irq_filter_t *filter,
829 void *arg, u_int ipicount)
831 struct intr_pic *pic;
833 pic = pic_lookup(dev, xref, FLAG_PIC);
835 device_printf(dev, "not registered\n");
839 KASSERT((pic->pic_flags & FLAG_TYPE_MASK) == FLAG_PIC,
840 ("%s: Found a non-PIC controller: %s", __func__,
841 device_get_name(pic->pic_dev)));
843 if (filter == NULL) {
844 device_printf(dev, "filter missing\n");
849 * Only one interrupt controllers could be on the root for now.
850 * Note that we further suppose that there is not threaded interrupt
851 * routine (handler) on the root. See intr_irq_handler().
853 if (intr_irq_root_dev != NULL) {
854 device_printf(dev, "another root already set\n");
858 intr_irq_root_dev = dev;
859 irq_root_filter = filter;
861 irq_root_ipicount = ipicount;
863 debugf("irq root set to %s\n", device_get_nameunit(dev));
868 * Add a handler to manage a sub range of a parents interrupts.
871 intr_pic_add_handler(device_t parent, struct intr_pic *pic,
872 intr_child_irq_filter_t *filter, void *arg, uintptr_t start,
875 struct intr_pic *parent_pic;
876 struct intr_pic_child *newchild;
878 struct intr_pic_child *child;
881 /* Find the parent PIC */
882 parent_pic = pic_lookup(parent, 0, FLAG_PIC);
883 if (parent_pic == NULL)
886 newchild = malloc(sizeof(*newchild), M_INTRNG, M_WAITOK | M_ZERO);
887 newchild->pc_pic = pic;
888 newchild->pc_filter = filter;
889 newchild->pc_filter_arg = arg;
890 newchild->pc_start = start;
891 newchild->pc_length = length;
893 mtx_lock_spin(&parent_pic->pic_child_lock);
895 SLIST_FOREACH(child, &parent_pic->pic_children, pc_next) {
896 KASSERT(child->pc_pic != pic, ("%s: Adding a child PIC twice",
900 SLIST_INSERT_HEAD(&parent_pic->pic_children, newchild, pc_next);
901 mtx_unlock_spin(&parent_pic->pic_child_lock);
907 intr_resolve_irq(device_t dev, intptr_t xref, struct intr_map_data *data,
908 struct intr_irqsrc **isrc)
910 struct intr_pic *pic;
911 struct intr_map_data_msi *msi;
916 pic = pic_lookup(dev, xref,
917 (data->type == INTR_MAP_DATA_MSI) ? FLAG_MSI : FLAG_PIC);
921 switch (data->type) {
922 case INTR_MAP_DATA_MSI:
923 KASSERT((pic->pic_flags & FLAG_TYPE_MASK) == FLAG_MSI,
924 ("%s: Found a non-MSI controller: %s", __func__,
925 device_get_name(pic->pic_dev)));
926 msi = (struct intr_map_data_msi *)data;
931 KASSERT((pic->pic_flags & FLAG_TYPE_MASK) == FLAG_PIC,
932 ("%s: Found a non-PIC controller: %s", __func__,
933 device_get_name(pic->pic_dev)));
934 return (PIC_MAP_INTR(pic->pic_dev, data, isrc));
940 intr_activate_irq(device_t dev, struct resource *res)
944 struct intr_map_data *data;
945 struct intr_irqsrc *isrc;
949 KASSERT(rman_get_start(res) == rman_get_end(res),
950 ("%s: more interrupts in resource", __func__));
952 res_id = (u_int)rman_get_start(res);
953 if (intr_map_get_isrc(res_id) != NULL)
954 panic("Attempt to double activation of resource id: %u\n",
956 intr_map_copy_map_data(res_id, &map_dev, &map_xref, &data);
957 error = intr_resolve_irq(map_dev, map_xref, data, &isrc);
959 free(data, M_INTRNG);
960 /* XXX TODO DISCONECTED PICs */
961 /* if (error == EINVAL) return(0); */
964 intr_map_set_isrc(res_id, isrc);
965 rman_set_virtual(res, data);
966 return (PIC_ACTIVATE_INTR(isrc->isrc_dev, isrc, res, data));
970 intr_deactivate_irq(device_t dev, struct resource *res)
972 struct intr_map_data *data;
973 struct intr_irqsrc *isrc;
977 KASSERT(rman_get_start(res) == rman_get_end(res),
978 ("%s: more interrupts in resource", __func__));
980 res_id = (u_int)rman_get_start(res);
981 isrc = intr_map_get_isrc(res_id);
983 panic("Attempt to deactivate non-active resource id: %u\n",
986 data = rman_get_virtual(res);
987 error = PIC_DEACTIVATE_INTR(isrc->isrc_dev, isrc, res, data);
988 intr_map_set_isrc(res_id, NULL);
989 rman_set_virtual(res, NULL);
990 free(data, M_INTRNG);
995 intr_setup_irq(device_t dev, struct resource *res, driver_filter_t filt,
996 driver_intr_t hand, void *arg, int flags, void **cookiep)
999 struct intr_map_data *data;
1000 struct intr_irqsrc *isrc;
1004 KASSERT(rman_get_start(res) == rman_get_end(res),
1005 ("%s: more interrupts in resource", __func__));
1007 res_id = (u_int)rman_get_start(res);
1008 isrc = intr_map_get_isrc(res_id);
1010 /* XXX TODO DISCONECTED PICs */
1014 data = rman_get_virtual(res);
1015 name = device_get_nameunit(dev);
1019 * Standard handling is done through MI interrupt framework. However,
1020 * some interrupts could request solely own special handling. This
1021 * non standard handling can be used for interrupt controllers without
1022 * handler (filter only), so in case that interrupt controllers are
1023 * chained, MI interrupt framework is called only in leaf controller.
1025 * Note that root interrupt controller routine is served as well,
1026 * however in intr_irq_handler(), i.e. main system dispatch routine.
1028 if (flags & INTR_SOLO && hand != NULL) {
1029 debugf("irq %u cannot solo on %s\n", irq, name);
1033 if (flags & INTR_SOLO) {
1034 error = iscr_setup_filter(isrc, name, (intr_irq_filter_t *)filt,
1036 debugf("irq %u setup filter error %d on %s\n", isrc->isrc_irq, error,
1041 error = isrc_add_handler(isrc, name, filt, hand, arg, flags,
1043 debugf("irq %u add handler error %d on %s\n", isrc->isrc_irq, error, name);
1048 mtx_lock(&isrc_table_lock);
1049 error = PIC_SETUP_INTR(isrc->isrc_dev, isrc, res, data);
1051 isrc->isrc_handlers++;
1052 if (isrc->isrc_handlers == 1)
1053 PIC_ENABLE_INTR(isrc->isrc_dev, isrc);
1055 mtx_unlock(&isrc_table_lock);
1057 intr_event_remove_handler(*cookiep);
1062 intr_teardown_irq(device_t dev, struct resource *res, void *cookie)
1065 struct intr_map_data *data;
1066 struct intr_irqsrc *isrc;
1069 KASSERT(rman_get_start(res) == rman_get_end(res),
1070 ("%s: more interrupts in resource", __func__));
1072 res_id = (u_int)rman_get_start(res);
1073 isrc = intr_map_get_isrc(res_id);
1074 if (isrc == NULL || isrc->isrc_handlers == 0)
1077 data = rman_get_virtual(res);
1080 if (isrc->isrc_filter != NULL) {
1084 mtx_lock(&isrc_table_lock);
1085 isrc->isrc_filter = NULL;
1086 isrc->isrc_arg = NULL;
1087 isrc->isrc_handlers = 0;
1088 PIC_DISABLE_INTR(isrc->isrc_dev, isrc);
1089 PIC_TEARDOWN_INTR(isrc->isrc_dev, isrc, res, data);
1090 isrc_update_name(isrc, NULL);
1091 mtx_unlock(&isrc_table_lock);
1095 if (isrc != intr_handler_source(cookie))
1098 error = intr_event_remove_handler(cookie);
1100 mtx_lock(&isrc_table_lock);
1101 isrc->isrc_handlers--;
1102 if (isrc->isrc_handlers == 0)
1103 PIC_DISABLE_INTR(isrc->isrc_dev, isrc);
1104 PIC_TEARDOWN_INTR(isrc->isrc_dev, isrc, res, data);
1105 intrcnt_updatename(isrc);
1106 mtx_unlock(&isrc_table_lock);
1112 intr_describe_irq(device_t dev, struct resource *res, void *cookie,
1116 struct intr_irqsrc *isrc;
1119 KASSERT(rman_get_start(res) == rman_get_end(res),
1120 ("%s: more interrupts in resource", __func__));
1122 res_id = (u_int)rman_get_start(res);
1123 isrc = intr_map_get_isrc(res_id);
1124 if (isrc == NULL || isrc->isrc_handlers == 0)
1127 if (isrc->isrc_filter != NULL) {
1131 mtx_lock(&isrc_table_lock);
1132 isrc_update_name(isrc, descr);
1133 mtx_unlock(&isrc_table_lock);
1137 error = intr_event_describe_handler(isrc->isrc_event, cookie, descr);
1139 mtx_lock(&isrc_table_lock);
1140 intrcnt_updatename(isrc);
1141 mtx_unlock(&isrc_table_lock);
1148 intr_bind_irq(device_t dev, struct resource *res, int cpu)
1150 struct intr_irqsrc *isrc;
1153 KASSERT(rman_get_start(res) == rman_get_end(res),
1154 ("%s: more interrupts in resource", __func__));
1156 res_id = (u_int)rman_get_start(res);
1157 isrc = intr_map_get_isrc(res_id);
1158 if (isrc == NULL || isrc->isrc_handlers == 0)
1161 if (isrc->isrc_filter != NULL)
1162 return (intr_isrc_assign_cpu(isrc, cpu));
1164 return (intr_event_bind(isrc->isrc_event, cpu));
1168 * Return the CPU that the next interrupt source should use.
1169 * For now just returns the next CPU according to round-robin.
1172 intr_irq_next_cpu(u_int last_cpu, cpuset_t *cpumask)
1176 KASSERT(!CPU_EMPTY(cpumask), ("%s: Empty CPU mask", __func__));
1177 if (!irq_assign_cpu || mp_ncpus == 1) {
1178 cpu = PCPU_GET(cpuid);
1180 if (CPU_ISSET(cpu, cpumask))
1183 return (CPU_FFS(cpumask) - 1);
1188 if (last_cpu > mp_maxid)
1190 } while (!CPU_ISSET(last_cpu, cpumask));
1195 * Distribute all the interrupt sources among the available
1196 * CPUs once the AP's have been launched.
1199 intr_irq_shuffle(void *arg __unused)
1201 struct intr_irqsrc *isrc;
1207 mtx_lock(&isrc_table_lock);
1208 irq_assign_cpu = TRUE;
1209 for (i = 0; i < NIRQ; i++) {
1210 isrc = irq_sources[i];
1211 if (isrc == NULL || isrc->isrc_handlers == 0 ||
1212 isrc->isrc_flags & (INTR_ISRCF_PPI | INTR_ISRCF_IPI))
1215 if (isrc->isrc_event != NULL &&
1216 isrc->isrc_flags & INTR_ISRCF_BOUND &&
1217 isrc->isrc_event->ie_cpu != CPU_FFS(&isrc->isrc_cpu) - 1)
1218 panic("%s: CPU inconsistency", __func__);
1220 if ((isrc->isrc_flags & INTR_ISRCF_BOUND) == 0)
1221 CPU_ZERO(&isrc->isrc_cpu); /* start again */
1224 * We are in wicked position here if the following call fails
1225 * for bound ISRC. The best thing we can do is to clear
1226 * isrc_cpu so inconsistency with ie_cpu will be detectable.
1228 if (PIC_BIND_INTR(isrc->isrc_dev, isrc) != 0)
1229 CPU_ZERO(&isrc->isrc_cpu);
1231 mtx_unlock(&isrc_table_lock);
1233 SYSINIT(intr_irq_shuffle, SI_SUB_SMP, SI_ORDER_SECOND, intr_irq_shuffle, NULL);
1237 intr_irq_next_cpu(u_int current_cpu, cpuset_t *cpumask)
1240 return (PCPU_GET(cpuid));
1245 * Allocate memory for new intr_map_data structure.
1246 * Initialize common fields.
1248 struct intr_map_data *
1249 intr_alloc_map_data(enum intr_map_data_type type, size_t len, int flags)
1251 struct intr_map_data *data;
1253 data = malloc(len, M_INTRNG, flags);
1259 void intr_free_intr_map_data(struct intr_map_data *data)
1262 free(data, M_INTRNG);
1267 * Register a MSI/MSI-X interrupt controller
1270 intr_msi_register(device_t dev, intptr_t xref)
1272 struct intr_pic *pic;
1276 pic = pic_create(dev, xref, FLAG_MSI);
1280 debugf("PIC %p registered for %s <dev %p, xref %jx>\n", pic,
1281 device_get_nameunit(dev), dev, (uintmax_t)xref);
1286 intr_alloc_msi(device_t pci, device_t child, intptr_t xref, int count,
1287 int maxcount, int *irqs)
1289 struct intr_irqsrc **isrc;
1290 struct intr_pic *pic;
1292 struct intr_map_data_msi *msi;
1295 pic = pic_lookup(NULL, xref, FLAG_MSI);
1299 KASSERT((pic->pic_flags & FLAG_TYPE_MASK) == FLAG_MSI,
1300 ("%s: Found a non-MSI controller: %s", __func__,
1301 device_get_name(pic->pic_dev)));
1303 isrc = malloc(sizeof(*isrc) * count, M_INTRNG, M_WAITOK);
1304 err = MSI_ALLOC_MSI(pic->pic_dev, child, count, maxcount, &pdev, isrc);
1306 free(isrc, M_INTRNG);
1310 for (i = 0; i < count; i++) {
1311 msi = (struct intr_map_data_msi *)intr_alloc_map_data(
1312 INTR_MAP_DATA_MSI, sizeof(*msi), M_WAITOK | M_ZERO);
1313 msi-> isrc = isrc[i];
1314 irqs[i] = intr_map_irq(pic->pic_dev, xref,
1315 (struct intr_map_data *)msi);
1318 free(isrc, M_INTRNG);
1324 intr_release_msi(device_t pci, device_t child, intptr_t xref, int count,
1327 struct intr_irqsrc **isrc;
1328 struct intr_pic *pic;
1329 struct intr_map_data_msi *msi;
1332 pic = pic_lookup(NULL, xref, FLAG_MSI);
1336 KASSERT((pic->pic_flags & FLAG_TYPE_MASK) == FLAG_MSI,
1337 ("%s: Found a non-MSI controller: %s", __func__,
1338 device_get_name(pic->pic_dev)));
1340 isrc = malloc(sizeof(*isrc) * count, M_INTRNG, M_WAITOK);
1342 for (i = 0; i < count; i++) {
1343 msi = (struct intr_map_data_msi *)
1344 intr_map_get_map_data(irqs[i]);
1345 KASSERT(msi->hdr.type == INTR_MAP_DATA_MSI,
1346 ("%s: irq %d map data is not MSI", __func__,
1348 isrc[i] = msi->isrc;
1351 err = MSI_RELEASE_MSI(pic->pic_dev, child, count, isrc);
1353 for (i = 0; i < count; i++) {
1354 if (isrc[i] != NULL)
1355 intr_unmap_irq(irqs[i]);
1358 free(isrc, M_INTRNG);
1363 intr_alloc_msix(device_t pci, device_t child, intptr_t xref, int *irq)
1365 struct intr_irqsrc *isrc;
1366 struct intr_pic *pic;
1368 struct intr_map_data_msi *msi;
1371 pic = pic_lookup(NULL, xref, FLAG_MSI);
1375 KASSERT((pic->pic_flags & FLAG_TYPE_MASK) == FLAG_MSI,
1376 ("%s: Found a non-MSI controller: %s", __func__,
1377 device_get_name(pic->pic_dev)));
1380 err = MSI_ALLOC_MSIX(pic->pic_dev, child, &pdev, &isrc);
1384 msi = (struct intr_map_data_msi *)intr_alloc_map_data(
1385 INTR_MAP_DATA_MSI, sizeof(*msi), M_WAITOK | M_ZERO);
1387 *irq = intr_map_irq(pic->pic_dev, xref, (struct intr_map_data *)msi);
1392 intr_release_msix(device_t pci, device_t child, intptr_t xref, int irq)
1394 struct intr_irqsrc *isrc;
1395 struct intr_pic *pic;
1396 struct intr_map_data_msi *msi;
1399 pic = pic_lookup(NULL, xref, FLAG_MSI);
1403 KASSERT((pic->pic_flags & FLAG_TYPE_MASK) == FLAG_MSI,
1404 ("%s: Found a non-MSI controller: %s", __func__,
1405 device_get_name(pic->pic_dev)));
1407 msi = (struct intr_map_data_msi *)
1408 intr_map_get_map_data(irq);
1409 KASSERT(msi->hdr.type == INTR_MAP_DATA_MSI,
1410 ("%s: irq %d map data is not MSI", __func__,
1414 intr_unmap_irq(irq);
1418 err = MSI_RELEASE_MSIX(pic->pic_dev, child, isrc);
1419 intr_unmap_irq(irq);
1425 intr_map_msi(device_t pci, device_t child, intptr_t xref, int irq,
1426 uint64_t *addr, uint32_t *data)
1428 struct intr_irqsrc *isrc;
1429 struct intr_pic *pic;
1432 pic = pic_lookup(NULL, xref, FLAG_MSI);
1436 KASSERT((pic->pic_flags & FLAG_TYPE_MASK) == FLAG_MSI,
1437 ("%s: Found a non-MSI controller: %s", __func__,
1438 device_get_name(pic->pic_dev)));
1440 isrc = intr_map_get_isrc(irq);
1444 err = MSI_MAP_MSI(pic->pic_dev, child, isrc, addr, data);
1449 void dosoftints(void);
1457 * Init interrupt controller on another CPU.
1460 intr_pic_init_secondary(void)
1464 * QQQ: Only root PIC is aware of other CPUs ???
1466 KASSERT(intr_irq_root_dev != NULL, ("%s: no root attached", __func__));
1468 //mtx_lock(&isrc_table_lock);
1469 PIC_INIT_SECONDARY(intr_irq_root_dev);
1470 //mtx_unlock(&isrc_table_lock);
1475 DB_SHOW_COMMAND(irqs, db_show_irqs)
1479 struct intr_irqsrc *isrc;
1481 for (irqsum = 0, i = 0; i < NIRQ; i++) {
1482 isrc = irq_sources[i];
1486 num = isrc->isrc_count != NULL ? isrc->isrc_count[0] : 0;
1487 db_printf("irq%-3u <%s>: cpu %02lx%s cnt %lu\n", i,
1488 isrc->isrc_name, isrc->isrc_cpu.__bits[0],
1489 isrc->isrc_flags & INTR_ISRCF_BOUND ? " (bound)" : "", num);
1492 db_printf("irq total %u\n", irqsum);
1497 * Interrupt mapping table functions.
1499 * Please, keep this part separately, it can be transformed to
1500 * extension of standard resources.
1502 struct intr_map_entry
1506 struct intr_map_data *map_data;
1507 struct intr_irqsrc *isrc;
1508 /* XXX TODO DISCONECTED PICs */
1512 /* XXX Convert irq_map[] to dynamicaly expandable one. */
1513 static struct intr_map_entry *irq_map[2 * NIRQ];
1514 static int irq_map_count = nitems(irq_map);
1515 static int irq_map_first_free_idx;
1516 static struct mtx irq_map_lock;
1518 static struct intr_irqsrc *
1519 intr_map_get_isrc(u_int res_id)
1521 struct intr_irqsrc *isrc;
1523 mtx_lock(&irq_map_lock);
1524 if ((res_id >= irq_map_count) || (irq_map[res_id] == NULL)) {
1525 mtx_unlock(&irq_map_lock);
1528 isrc = irq_map[res_id]->isrc;
1529 mtx_unlock(&irq_map_lock);
1534 intr_map_set_isrc(u_int res_id, struct intr_irqsrc *isrc)
1537 mtx_lock(&irq_map_lock);
1538 if ((res_id >= irq_map_count) || (irq_map[res_id] == NULL)) {
1539 mtx_unlock(&irq_map_lock);
1542 irq_map[res_id]->isrc = isrc;
1543 mtx_unlock(&irq_map_lock);
1547 * Get a copy of intr_map_entry data
1549 static struct intr_map_data *
1550 intr_map_get_map_data(u_int res_id)
1552 struct intr_map_data *data;
1555 mtx_lock(&irq_map_lock);
1556 if (res_id >= irq_map_count || irq_map[res_id] == NULL)
1557 panic("Attempt to copy invalid resource id: %u\n", res_id);
1558 data = irq_map[res_id]->map_data;
1559 mtx_unlock(&irq_map_lock);
1565 * Get a copy of intr_map_entry data
1568 intr_map_copy_map_data(u_int res_id, device_t *map_dev, intptr_t *map_xref,
1569 struct intr_map_data **data)
1574 mtx_lock(&irq_map_lock);
1575 if (res_id >= irq_map_count || irq_map[res_id] == NULL)
1576 panic("Attempt to copy invalid resource id: %u\n", res_id);
1577 if (irq_map[res_id]->map_data != NULL)
1578 len = irq_map[res_id]->map_data->len;
1579 mtx_unlock(&irq_map_lock);
1584 *data = malloc(len, M_INTRNG, M_WAITOK | M_ZERO);
1585 mtx_lock(&irq_map_lock);
1586 if (irq_map[res_id] == NULL)
1587 panic("Attempt to copy invalid resource id: %u\n", res_id);
1589 if (len != irq_map[res_id]->map_data->len)
1590 panic("Resource id: %u has changed.\n", res_id);
1591 memcpy(*data, irq_map[res_id]->map_data, len);
1593 *map_dev = irq_map[res_id]->dev;
1594 *map_xref = irq_map[res_id]->xref;
1595 mtx_unlock(&irq_map_lock);
1600 * Allocate and fill new entry in irq_map table.
1603 intr_map_irq(device_t dev, intptr_t xref, struct intr_map_data *data)
1606 struct intr_map_entry *entry;
1608 /* Prepare new entry first. */
1609 entry = malloc(sizeof(*entry), M_INTRNG, M_WAITOK | M_ZERO);
1613 entry->map_data = data;
1616 mtx_lock(&irq_map_lock);
1617 for (i = irq_map_first_free_idx; i < irq_map_count; i++) {
1618 if (irq_map[i] == NULL) {
1620 irq_map_first_free_idx = i + 1;
1621 mtx_unlock(&irq_map_lock);
1625 mtx_unlock(&irq_map_lock);
1627 /* XXX Expand irq_map table */
1628 panic("IRQ mapping table is full.");
1632 * Remove and free mapping entry.
1635 intr_unmap_irq(u_int res_id)
1637 struct intr_map_entry *entry;
1639 mtx_lock(&irq_map_lock);
1640 if ((res_id >= irq_map_count) || (irq_map[res_id] == NULL))
1641 panic("Attempt to unmap invalid resource id: %u\n", res_id);
1642 entry = irq_map[res_id];
1643 irq_map[res_id] = NULL;
1644 irq_map_first_free_idx = res_id;
1645 mtx_unlock(&irq_map_lock);
1646 intr_free_intr_map_data(entry->map_data);
1647 free(entry, M_INTRNG);
1651 * Clone mapping entry.
1654 intr_map_clone_irq(u_int old_res_id)
1658 struct intr_map_data *data;
1660 intr_map_copy_map_data(old_res_id, &map_dev, &map_xref, &data);
1661 return (intr_map_irq(map_dev, map_xref, data));
1665 intr_map_init(void *dummy __unused)
1668 mtx_init(&irq_map_lock, "intr map table", NULL, MTX_DEF);
1670 SYSINIT(intr_map_init, SI_SUB_INTR, SI_ORDER_FIRST, intr_map_init, NULL);