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>
30 * New-style Interrupt Framework
32 * TODO: - add support for disconnected PICs.
33 * - to support IPI (PPI) enabling on other CPUs if already started.
34 * - to complete things for removable PICs.
38 #include "opt_hwpmc_hooks.h"
39 #include "opt_iommu.h"
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/bitstring.h>
46 #include <sys/cpuset.h>
47 #include <sys/interrupt.h>
48 #include <sys/kernel.h>
50 #include <sys/malloc.h>
51 #include <sys/mutex.h>
53 #include <sys/queue.h>
55 #include <sys/sched.h>
57 #include <sys/sysctl.h>
58 #include <sys/syslog.h>
59 #include <sys/taskqueue.h>
61 #include <sys/vmmeter.h>
63 #include <sys/pmckern.h>
66 #include <machine/atomic.h>
67 #include <machine/cpu.h>
68 #include <machine/intr.h>
69 #include <machine/smp.h>
70 #include <machine/stdarg.h>
77 #include <dev/iommu/iommu_msi.h>
83 #define INTRNAME_LEN (2*MAXCOMLEN + 1)
86 #define debugf(fmt, args...) do { printf("%s(): ", __func__); \
87 printf(fmt,##args); } while (0)
89 #define debugf(fmt, args...)
92 MALLOC_DECLARE(M_INTRNG);
93 MALLOC_DEFINE(M_INTRNG, "intr", "intr interrupt handling");
95 /* Main interrupt handler called from assembler -> 'hidden' for C code. */
96 void intr_irq_handler(struct trapframe *tf);
98 /* Root interrupt controller stuff. */
99 device_t intr_irq_root_dev;
100 static intr_irq_filter_t *irq_root_filter;
101 static void *irq_root_arg;
102 static u_int irq_root_ipicount;
104 struct intr_pic_child {
105 SLIST_ENTRY(intr_pic_child) pc_next;
106 struct intr_pic *pc_pic;
107 intr_child_irq_filter_t *pc_filter;
113 /* Interrupt controller definition. */
115 SLIST_ENTRY(intr_pic) pic_next;
116 intptr_t pic_xref; /* hardware identification */
118 /* Only one of FLAG_PIC or FLAG_MSI may be set */
119 #define FLAG_PIC (1 << 0)
120 #define FLAG_MSI (1 << 1)
121 #define FLAG_TYPE_MASK (FLAG_PIC | FLAG_MSI)
123 struct mtx pic_child_lock;
124 SLIST_HEAD(, intr_pic_child) pic_children;
127 static struct mtx pic_list_lock;
128 static SLIST_HEAD(, intr_pic) pic_list;
130 static struct intr_pic *pic_lookup(device_t dev, intptr_t xref, int flags);
132 /* Interrupt source definition. */
133 static struct mtx isrc_table_lock;
134 static struct intr_irqsrc **irq_sources;
138 #ifdef EARLY_AP_STARTUP
139 static bool irq_assign_cpu = true;
141 static bool irq_assign_cpu = false;
145 u_int intr_nirq = NIRQ;
146 SYSCTL_UINT(_machdep, OID_AUTO, nirq, CTLFLAG_RDTUN, &intr_nirq, 0,
149 /* Data for MI statistics reporting. */
155 static bitstr_t *intrcnt_bitmap;
157 static struct intr_irqsrc *intr_map_get_isrc(u_int res_id);
158 static void intr_map_set_isrc(u_int res_id, struct intr_irqsrc *isrc);
159 static struct intr_map_data * intr_map_get_map_data(u_int res_id);
160 static void intr_map_copy_map_data(u_int res_id, device_t *dev, intptr_t *xref,
161 struct intr_map_data **data);
164 * Interrupt framework initialization routine.
167 intr_irq_init(void *dummy __unused)
170 SLIST_INIT(&pic_list);
171 mtx_init(&pic_list_lock, "intr pic list", NULL, MTX_DEF);
173 mtx_init(&isrc_table_lock, "intr isrc table", NULL, MTX_DEF);
176 * - 2 counters for each I/O interrupt.
177 * - MAXCPU counters for each IPI counters for SMP.
179 nintrcnt = intr_nirq * 2;
181 nintrcnt += INTR_IPI_COUNT * MAXCPU;
184 intrcnt = mallocarray(nintrcnt, sizeof(u_long), M_INTRNG,
186 intrnames = mallocarray(nintrcnt, INTRNAME_LEN, M_INTRNG,
188 sintrcnt = nintrcnt * sizeof(u_long);
189 sintrnames = nintrcnt * INTRNAME_LEN;
191 /* Allocate the bitmap tracking counter allocations. */
192 intrcnt_bitmap = bit_alloc(nintrcnt, M_INTRNG, M_WAITOK | M_ZERO);
194 irq_sources = mallocarray(intr_nirq, sizeof(struct intr_irqsrc*),
195 M_INTRNG, M_WAITOK | M_ZERO);
197 SYSINIT(intr_irq_init, SI_SUB_INTR, SI_ORDER_FIRST, intr_irq_init, NULL);
200 intrcnt_setname(const char *name, int index)
203 snprintf(intrnames + INTRNAME_LEN * index, INTRNAME_LEN, "%-*s",
204 INTRNAME_LEN - 1, name);
208 * Update name for interrupt source with interrupt event.
211 intrcnt_updatename(struct intr_irqsrc *isrc)
214 /* QQQ: What about stray counter name? */
215 mtx_assert(&isrc_table_lock, MA_OWNED);
216 intrcnt_setname(isrc->isrc_event->ie_fullname, isrc->isrc_index);
220 * Virtualization for interrupt source interrupt counter increment.
223 isrc_increment_count(struct intr_irqsrc *isrc)
226 if (isrc->isrc_flags & INTR_ISRCF_PPI)
227 atomic_add_long(&isrc->isrc_count[0], 1);
229 isrc->isrc_count[0]++;
233 * Virtualization for interrupt source interrupt stray counter increment.
236 isrc_increment_straycount(struct intr_irqsrc *isrc)
239 isrc->isrc_count[1]++;
243 * Virtualization for interrupt source interrupt name update.
246 isrc_update_name(struct intr_irqsrc *isrc, const char *name)
248 char str[INTRNAME_LEN];
250 mtx_assert(&isrc_table_lock, MA_OWNED);
253 snprintf(str, INTRNAME_LEN, "%s: %s", isrc->isrc_name, name);
254 intrcnt_setname(str, isrc->isrc_index);
255 snprintf(str, INTRNAME_LEN, "stray %s: %s", isrc->isrc_name,
257 intrcnt_setname(str, isrc->isrc_index + 1);
259 snprintf(str, INTRNAME_LEN, "%s:", isrc->isrc_name);
260 intrcnt_setname(str, isrc->isrc_index);
261 snprintf(str, INTRNAME_LEN, "stray %s:", isrc->isrc_name);
262 intrcnt_setname(str, isrc->isrc_index + 1);
267 * Virtualization for interrupt source interrupt counters setup.
270 isrc_setup_counters(struct intr_irqsrc *isrc)
274 mtx_assert(&isrc_table_lock, MA_OWNED);
277 * Allocate two counter values, the second tracking "stray" interrupts.
279 bit_ffc_area(intrcnt_bitmap, nintrcnt, 2, &index);
281 panic("Failed to allocate 2 counters. Array exhausted?");
282 bit_nset(intrcnt_bitmap, index, index + 1);
283 isrc->isrc_index = index;
284 isrc->isrc_count = &intrcnt[index];
285 isrc_update_name(isrc, NULL);
289 * Virtualization for interrupt source interrupt counters release.
292 isrc_release_counters(struct intr_irqsrc *isrc)
294 int idx = isrc->isrc_index;
296 mtx_assert(&isrc_table_lock, MA_OWNED);
298 bit_nclear(intrcnt_bitmap, idx, idx + 1);
303 * Virtualization for interrupt source IPI counters setup.
306 intr_ipi_setup_counters(const char *name)
309 char str[INTRNAME_LEN];
311 mtx_lock(&isrc_table_lock);
314 * We should never have a problem finding MAXCPU contiguous counters,
315 * in practice. Interrupts will be allocated sequentially during boot,
316 * so the array should fill from low to high index. Once reserved, the
317 * IPI counters will never be released. Similarly, we will not need to
318 * allocate more IPIs once the system is running.
320 bit_ffc_area(intrcnt_bitmap, nintrcnt, MAXCPU, &index);
322 panic("Failed to allocate %d counters. Array exhausted?",
324 bit_nset(intrcnt_bitmap, index, index + MAXCPU - 1);
325 for (i = 0; i < MAXCPU; i++) {
326 snprintf(str, INTRNAME_LEN, "cpu%d:%s", i, name);
327 intrcnt_setname(str, index + i);
329 mtx_unlock(&isrc_table_lock);
330 return (&intrcnt[index]);
335 * Main interrupt dispatch handler. It's called straight
336 * from the assembler, where CPU interrupt is served.
339 intr_irq_handler(struct trapframe *tf)
341 struct trapframe * oldframe;
344 KASSERT(irq_root_filter != NULL, ("%s: no filter", __func__));
349 oldframe = td->td_intr_frame;
350 td->td_intr_frame = tf;
351 irq_root_filter(irq_root_arg);
352 td->td_intr_frame = oldframe;
355 if (pmc_hook && TRAPF_USERMODE(tf) &&
356 (PCPU_GET(curthread)->td_pflags & TDP_CALLCHAIN))
357 pmc_hook(PCPU_GET(curthread), PMC_FN_USER_CALLCHAIN, tf);
362 intr_child_irq_handler(struct intr_pic *parent, uintptr_t irq)
364 struct intr_pic_child *child;
368 mtx_lock_spin(&parent->pic_child_lock);
369 SLIST_FOREACH(child, &parent->pic_children, pc_next) {
370 if (child->pc_start <= irq &&
371 irq < (child->pc_start + child->pc_length)) {
376 mtx_unlock_spin(&parent->pic_child_lock);
379 return (child->pc_filter(child->pc_filter_arg, irq));
381 return (FILTER_STRAY);
385 * interrupt controller dispatch function for interrupts. It should
386 * be called straight from the interrupt controller, when associated interrupt
390 intr_isrc_dispatch(struct intr_irqsrc *isrc, struct trapframe *tf)
393 KASSERT(isrc != NULL, ("%s: no source", __func__));
395 isrc_increment_count(isrc);
398 if (isrc->isrc_filter != NULL) {
400 error = isrc->isrc_filter(isrc->isrc_arg, tf);
401 PIC_POST_FILTER(isrc->isrc_dev, isrc);
402 if (error == FILTER_HANDLED)
406 if (isrc->isrc_event != NULL) {
407 if (intr_event_handle(isrc->isrc_event, tf) == 0)
411 isrc_increment_straycount(isrc);
416 * Alloc unique interrupt number (resource handle) for interrupt source.
418 * There could be various strategies how to allocate free interrupt number
419 * (resource handle) for new interrupt source.
421 * 1. Handles are always allocated forward, so handles are not recycled
422 * immediately. However, if only one free handle left which is reused
426 isrc_alloc_irq(struct intr_irqsrc *isrc)
430 mtx_assert(&isrc_table_lock, MA_OWNED);
432 if (irq_next_free >= intr_nirq)
435 for (irq = irq_next_free; irq < intr_nirq; irq++) {
436 if (irq_sources[irq] == NULL)
439 for (irq = 0; irq < irq_next_free; irq++) {
440 if (irq_sources[irq] == NULL)
444 irq_next_free = intr_nirq;
448 isrc->isrc_irq = irq;
449 irq_sources[irq] = isrc;
451 irq_next_free = irq + 1;
452 if (irq_next_free >= intr_nirq)
458 * Free unique interrupt number (resource handle) from interrupt source.
461 isrc_free_irq(struct intr_irqsrc *isrc)
464 mtx_assert(&isrc_table_lock, MA_OWNED);
466 if (isrc->isrc_irq >= intr_nirq)
468 if (irq_sources[isrc->isrc_irq] != isrc)
471 irq_sources[isrc->isrc_irq] = NULL;
472 isrc->isrc_irq = INTR_IRQ_INVALID; /* just to be safe */
475 * If we are recovering from the state irq_sources table is full,
476 * then the following allocation should check the entire table. This
477 * will ensure maximum separation of allocation order from release
480 if (irq_next_free >= intr_nirq)
487 * Initialize interrupt source and register it into global interrupt table.
490 intr_isrc_register(struct intr_irqsrc *isrc, device_t dev, u_int flags,
491 const char *fmt, ...)
496 bzero(isrc, sizeof(struct intr_irqsrc));
497 isrc->isrc_dev = dev;
498 isrc->isrc_irq = INTR_IRQ_INVALID; /* just to be safe */
499 isrc->isrc_flags = flags;
502 vsnprintf(isrc->isrc_name, INTR_ISRC_NAMELEN, fmt, ap);
505 mtx_lock(&isrc_table_lock);
506 error = isrc_alloc_irq(isrc);
508 mtx_unlock(&isrc_table_lock);
512 * Setup interrupt counters, but not for IPI sources. Those are setup
513 * later and only for used ones (up to INTR_IPI_COUNT) to not exhaust
516 if ((isrc->isrc_flags & INTR_ISRCF_IPI) == 0)
517 isrc_setup_counters(isrc);
518 mtx_unlock(&isrc_table_lock);
523 * Deregister interrupt source from global interrupt table.
526 intr_isrc_deregister(struct intr_irqsrc *isrc)
530 mtx_lock(&isrc_table_lock);
531 if ((isrc->isrc_flags & INTR_ISRCF_IPI) == 0)
532 isrc_release_counters(isrc);
533 error = isrc_free_irq(isrc);
534 mtx_unlock(&isrc_table_lock);
540 * A support function for a PIC to decide if provided ISRC should be inited
541 * on given cpu. The logic of INTR_ISRCF_BOUND flag and isrc_cpu member of
542 * struct intr_irqsrc is the following:
544 * If INTR_ISRCF_BOUND is set, the ISRC should be inited only on cpus
545 * set in isrc_cpu. If not, the ISRC should be inited on every cpu and
546 * isrc_cpu is kept consistent with it. Thus isrc_cpu is always correct.
549 intr_isrc_init_on_cpu(struct intr_irqsrc *isrc, u_int cpu)
552 if (isrc->isrc_handlers == 0)
554 if ((isrc->isrc_flags & (INTR_ISRCF_PPI | INTR_ISRCF_IPI)) == 0)
556 if (isrc->isrc_flags & INTR_ISRCF_BOUND)
557 return (CPU_ISSET(cpu, &isrc->isrc_cpu));
559 CPU_SET(cpu, &isrc->isrc_cpu);
566 * Setup filter into interrupt source.
569 iscr_setup_filter(struct intr_irqsrc *isrc, const char *name,
570 intr_irq_filter_t *filter, void *arg, void **cookiep)
576 mtx_lock(&isrc_table_lock);
578 * Make sure that we do not mix the two ways
579 * how we handle interrupt sources.
581 if (isrc->isrc_filter != NULL || isrc->isrc_event != NULL) {
582 mtx_unlock(&isrc_table_lock);
585 isrc->isrc_filter = filter;
586 isrc->isrc_arg = arg;
587 isrc_update_name(isrc, name);
588 mtx_unlock(&isrc_table_lock);
596 * Interrupt source pre_ithread method for MI interrupt framework.
599 intr_isrc_pre_ithread(void *arg)
601 struct intr_irqsrc *isrc = arg;
603 PIC_PRE_ITHREAD(isrc->isrc_dev, isrc);
607 * Interrupt source post_ithread method for MI interrupt framework.
610 intr_isrc_post_ithread(void *arg)
612 struct intr_irqsrc *isrc = arg;
614 PIC_POST_ITHREAD(isrc->isrc_dev, isrc);
618 * Interrupt source post_filter method for MI interrupt framework.
621 intr_isrc_post_filter(void *arg)
623 struct intr_irqsrc *isrc = arg;
625 PIC_POST_FILTER(isrc->isrc_dev, isrc);
629 * Interrupt source assign_cpu method for MI interrupt framework.
632 intr_isrc_assign_cpu(void *arg, int cpu)
635 struct intr_irqsrc *isrc = arg;
638 mtx_lock(&isrc_table_lock);
640 CPU_ZERO(&isrc->isrc_cpu);
641 isrc->isrc_flags &= ~INTR_ISRCF_BOUND;
643 CPU_SETOF(cpu, &isrc->isrc_cpu);
644 isrc->isrc_flags |= INTR_ISRCF_BOUND;
648 * In NOCPU case, it's up to PIC to either leave ISRC on same CPU or
649 * re-balance it to another CPU or enable it on more CPUs. However,
650 * PIC is expected to change isrc_cpu appropriately to keep us well
651 * informed if the call is successful.
653 if (irq_assign_cpu) {
654 error = PIC_BIND_INTR(isrc->isrc_dev, isrc);
656 CPU_ZERO(&isrc->isrc_cpu);
657 mtx_unlock(&isrc_table_lock);
661 mtx_unlock(&isrc_table_lock);
669 * Create interrupt event for interrupt source.
672 isrc_event_create(struct intr_irqsrc *isrc)
674 struct intr_event *ie;
677 error = intr_event_create(&ie, isrc, 0, isrc->isrc_irq,
678 intr_isrc_pre_ithread, intr_isrc_post_ithread, intr_isrc_post_filter,
679 intr_isrc_assign_cpu, "%s:", isrc->isrc_name);
683 mtx_lock(&isrc_table_lock);
685 * Make sure that we do not mix the two ways
686 * how we handle interrupt sources. Let contested event wins.
689 if (isrc->isrc_filter != NULL || isrc->isrc_event != NULL) {
691 if (isrc->isrc_event != NULL) {
693 mtx_unlock(&isrc_table_lock);
694 intr_event_destroy(ie);
695 return (isrc->isrc_event != NULL ? EBUSY : 0);
697 isrc->isrc_event = ie;
698 mtx_unlock(&isrc_table_lock);
704 * Destroy interrupt event for interrupt source.
707 isrc_event_destroy(struct intr_irqsrc *isrc)
709 struct intr_event *ie;
711 mtx_lock(&isrc_table_lock);
712 ie = isrc->isrc_event;
713 isrc->isrc_event = NULL;
714 mtx_unlock(&isrc_table_lock);
717 intr_event_destroy(ie);
721 * Add handler to interrupt source.
724 isrc_add_handler(struct intr_irqsrc *isrc, const char *name,
725 driver_filter_t filter, driver_intr_t handler, void *arg,
726 enum intr_type flags, void **cookiep)
730 if (isrc->isrc_event == NULL) {
731 error = isrc_event_create(isrc);
736 error = intr_event_add_handler(isrc->isrc_event, name, filter, handler,
737 arg, intr_priority(flags), flags, cookiep);
739 mtx_lock(&isrc_table_lock);
740 intrcnt_updatename(isrc);
741 mtx_unlock(&isrc_table_lock);
748 * Lookup interrupt controller locked.
750 static inline struct intr_pic *
751 pic_lookup_locked(device_t dev, intptr_t xref, int flags)
753 struct intr_pic *pic;
755 mtx_assert(&pic_list_lock, MA_OWNED);
757 if (dev == NULL && xref == 0)
760 /* Note that pic->pic_dev is never NULL on registered PIC. */
761 SLIST_FOREACH(pic, &pic_list, pic_next) {
762 if ((pic->pic_flags & FLAG_TYPE_MASK) !=
763 (flags & FLAG_TYPE_MASK))
767 if (xref == pic->pic_xref)
769 } else if (xref == 0 || pic->pic_xref == 0) {
770 if (dev == pic->pic_dev)
772 } else if (xref == pic->pic_xref && dev == pic->pic_dev)
779 * Lookup interrupt controller.
781 static struct intr_pic *
782 pic_lookup(device_t dev, intptr_t xref, int flags)
784 struct intr_pic *pic;
786 mtx_lock(&pic_list_lock);
787 pic = pic_lookup_locked(dev, xref, flags);
788 mtx_unlock(&pic_list_lock);
793 * Create interrupt controller.
795 static struct intr_pic *
796 pic_create(device_t dev, intptr_t xref, int flags)
798 struct intr_pic *pic;
800 mtx_lock(&pic_list_lock);
801 pic = pic_lookup_locked(dev, xref, flags);
803 mtx_unlock(&pic_list_lock);
806 pic = malloc(sizeof(*pic), M_INTRNG, M_NOWAIT | M_ZERO);
808 mtx_unlock(&pic_list_lock);
811 pic->pic_xref = xref;
813 pic->pic_flags = flags;
814 mtx_init(&pic->pic_child_lock, "pic child lock", NULL, MTX_SPIN);
815 SLIST_INSERT_HEAD(&pic_list, pic, pic_next);
816 mtx_unlock(&pic_list_lock);
822 * Destroy interrupt controller.
825 pic_destroy(device_t dev, intptr_t xref, int flags)
827 struct intr_pic *pic;
829 mtx_lock(&pic_list_lock);
830 pic = pic_lookup_locked(dev, xref, flags);
832 mtx_unlock(&pic_list_lock);
835 SLIST_REMOVE(&pic_list, pic, intr_pic, pic_next);
836 mtx_unlock(&pic_list_lock);
842 * Register interrupt controller.
845 intr_pic_register(device_t dev, intptr_t xref)
847 struct intr_pic *pic;
851 pic = pic_create(dev, xref, FLAG_PIC);
855 debugf("PIC %p registered for %s <dev %p, xref %jx>\n", pic,
856 device_get_nameunit(dev), dev, (uintmax_t)xref);
861 * Unregister interrupt controller.
864 intr_pic_deregister(device_t dev, intptr_t xref)
867 panic("%s: not implemented", __func__);
871 * Mark interrupt controller (itself) as a root one.
873 * Note that only an interrupt controller can really know its position
874 * in interrupt controller's tree. So root PIC must claim itself as a root.
876 * In FDT case, according to ePAPR approved version 1.1 from 08 April 2011,
878 * "The root of the interrupt tree is determined when traversal
879 * of the interrupt tree reaches an interrupt controller node without
880 * an interrupts property and thus no explicit interrupt parent."
883 intr_pic_claim_root(device_t dev, intptr_t xref, intr_irq_filter_t *filter,
884 void *arg, u_int ipicount)
886 struct intr_pic *pic;
888 pic = pic_lookup(dev, xref, FLAG_PIC);
890 device_printf(dev, "not registered\n");
894 KASSERT((pic->pic_flags & FLAG_TYPE_MASK) == FLAG_PIC,
895 ("%s: Found a non-PIC controller: %s", __func__,
896 device_get_name(pic->pic_dev)));
898 if (filter == NULL) {
899 device_printf(dev, "filter missing\n");
904 * Only one interrupt controllers could be on the root for now.
905 * Note that we further suppose that there is not threaded interrupt
906 * routine (handler) on the root. See intr_irq_handler().
908 if (intr_irq_root_dev != NULL) {
909 device_printf(dev, "another root already set\n");
913 intr_irq_root_dev = dev;
914 irq_root_filter = filter;
916 irq_root_ipicount = ipicount;
918 debugf("irq root set to %s\n", device_get_nameunit(dev));
923 * Add a handler to manage a sub range of a parents interrupts.
926 intr_pic_add_handler(device_t parent, struct intr_pic *pic,
927 intr_child_irq_filter_t *filter, void *arg, uintptr_t start,
930 struct intr_pic *parent_pic;
931 struct intr_pic_child *newchild;
933 struct intr_pic_child *child;
936 /* Find the parent PIC */
937 parent_pic = pic_lookup(parent, 0, FLAG_PIC);
938 if (parent_pic == NULL)
941 newchild = malloc(sizeof(*newchild), M_INTRNG, M_WAITOK | M_ZERO);
942 newchild->pc_pic = pic;
943 newchild->pc_filter = filter;
944 newchild->pc_filter_arg = arg;
945 newchild->pc_start = start;
946 newchild->pc_length = length;
948 mtx_lock_spin(&parent_pic->pic_child_lock);
950 SLIST_FOREACH(child, &parent_pic->pic_children, pc_next) {
951 KASSERT(child->pc_pic != pic, ("%s: Adding a child PIC twice",
955 SLIST_INSERT_HEAD(&parent_pic->pic_children, newchild, pc_next);
956 mtx_unlock_spin(&parent_pic->pic_child_lock);
962 intr_resolve_irq(device_t dev, intptr_t xref, struct intr_map_data *data,
963 struct intr_irqsrc **isrc)
965 struct intr_pic *pic;
966 struct intr_map_data_msi *msi;
971 pic = pic_lookup(dev, xref,
972 (data->type == INTR_MAP_DATA_MSI) ? FLAG_MSI : FLAG_PIC);
976 switch (data->type) {
977 case INTR_MAP_DATA_MSI:
978 KASSERT((pic->pic_flags & FLAG_TYPE_MASK) == FLAG_MSI,
979 ("%s: Found a non-MSI controller: %s", __func__,
980 device_get_name(pic->pic_dev)));
981 msi = (struct intr_map_data_msi *)data;
986 KASSERT((pic->pic_flags & FLAG_TYPE_MASK) == FLAG_PIC,
987 ("%s: Found a non-PIC controller: %s", __func__,
988 device_get_name(pic->pic_dev)));
989 return (PIC_MAP_INTR(pic->pic_dev, data, isrc));
994 intr_is_per_cpu(struct resource *res)
997 struct intr_irqsrc *isrc;
999 res_id = (u_int)rman_get_start(res);
1000 isrc = intr_map_get_isrc(res_id);
1003 panic("Attempt to get isrc for non-active resource id: %u\n",
1005 return ((isrc->isrc_flags & INTR_ISRCF_PPI) != 0);
1009 intr_activate_irq(device_t dev, struct resource *res)
1013 struct intr_map_data *data;
1014 struct intr_irqsrc *isrc;
1018 KASSERT(rman_get_start(res) == rman_get_end(res),
1019 ("%s: more interrupts in resource", __func__));
1021 res_id = (u_int)rman_get_start(res);
1022 if (intr_map_get_isrc(res_id) != NULL)
1023 panic("Attempt to double activation of resource id: %u\n",
1025 intr_map_copy_map_data(res_id, &map_dev, &map_xref, &data);
1026 error = intr_resolve_irq(map_dev, map_xref, data, &isrc);
1028 free(data, M_INTRNG);
1029 /* XXX TODO DISCONECTED PICs */
1030 /* if (error == EINVAL) return(0); */
1033 intr_map_set_isrc(res_id, isrc);
1034 rman_set_virtual(res, data);
1035 return (PIC_ACTIVATE_INTR(isrc->isrc_dev, isrc, res, data));
1039 intr_deactivate_irq(device_t dev, struct resource *res)
1041 struct intr_map_data *data;
1042 struct intr_irqsrc *isrc;
1046 KASSERT(rman_get_start(res) == rman_get_end(res),
1047 ("%s: more interrupts in resource", __func__));
1049 res_id = (u_int)rman_get_start(res);
1050 isrc = intr_map_get_isrc(res_id);
1052 panic("Attempt to deactivate non-active resource id: %u\n",
1055 data = rman_get_virtual(res);
1056 error = PIC_DEACTIVATE_INTR(isrc->isrc_dev, isrc, res, data);
1057 intr_map_set_isrc(res_id, NULL);
1058 rman_set_virtual(res, NULL);
1059 free(data, M_INTRNG);
1064 intr_setup_irq(device_t dev, struct resource *res, driver_filter_t filt,
1065 driver_intr_t hand, void *arg, int flags, void **cookiep)
1068 struct intr_map_data *data;
1069 struct intr_irqsrc *isrc;
1073 KASSERT(rman_get_start(res) == rman_get_end(res),
1074 ("%s: more interrupts in resource", __func__));
1076 res_id = (u_int)rman_get_start(res);
1077 isrc = intr_map_get_isrc(res_id);
1079 /* XXX TODO DISCONECTED PICs */
1083 data = rman_get_virtual(res);
1084 name = device_get_nameunit(dev);
1088 * Standard handling is done through MI interrupt framework. However,
1089 * some interrupts could request solely own special handling. This
1090 * non standard handling can be used for interrupt controllers without
1091 * handler (filter only), so in case that interrupt controllers are
1092 * chained, MI interrupt framework is called only in leaf controller.
1094 * Note that root interrupt controller routine is served as well,
1095 * however in intr_irq_handler(), i.e. main system dispatch routine.
1097 if (flags & INTR_SOLO && hand != NULL) {
1098 debugf("irq %u cannot solo on %s\n", irq, name);
1102 if (flags & INTR_SOLO) {
1103 error = iscr_setup_filter(isrc, name, (intr_irq_filter_t *)filt,
1105 debugf("irq %u setup filter error %d on %s\n", isrc->isrc_irq, error,
1110 error = isrc_add_handler(isrc, name, filt, hand, arg, flags,
1112 debugf("irq %u add handler error %d on %s\n", isrc->isrc_irq, error, name);
1117 mtx_lock(&isrc_table_lock);
1118 error = PIC_SETUP_INTR(isrc->isrc_dev, isrc, res, data);
1120 isrc->isrc_handlers++;
1121 if (isrc->isrc_handlers == 1)
1122 PIC_ENABLE_INTR(isrc->isrc_dev, isrc);
1124 mtx_unlock(&isrc_table_lock);
1126 intr_event_remove_handler(*cookiep);
1131 intr_teardown_irq(device_t dev, struct resource *res, void *cookie)
1134 struct intr_map_data *data;
1135 struct intr_irqsrc *isrc;
1138 KASSERT(rman_get_start(res) == rman_get_end(res),
1139 ("%s: more interrupts in resource", __func__));
1141 res_id = (u_int)rman_get_start(res);
1142 isrc = intr_map_get_isrc(res_id);
1143 if (isrc == NULL || isrc->isrc_handlers == 0)
1146 data = rman_get_virtual(res);
1149 if (isrc->isrc_filter != NULL) {
1153 mtx_lock(&isrc_table_lock);
1154 isrc->isrc_filter = NULL;
1155 isrc->isrc_arg = NULL;
1156 isrc->isrc_handlers = 0;
1157 PIC_DISABLE_INTR(isrc->isrc_dev, isrc);
1158 PIC_TEARDOWN_INTR(isrc->isrc_dev, isrc, res, data);
1159 isrc_update_name(isrc, NULL);
1160 mtx_unlock(&isrc_table_lock);
1164 if (isrc != intr_handler_source(cookie))
1167 error = intr_event_remove_handler(cookie);
1169 mtx_lock(&isrc_table_lock);
1170 isrc->isrc_handlers--;
1171 if (isrc->isrc_handlers == 0)
1172 PIC_DISABLE_INTR(isrc->isrc_dev, isrc);
1173 PIC_TEARDOWN_INTR(isrc->isrc_dev, isrc, res, data);
1174 intrcnt_updatename(isrc);
1175 mtx_unlock(&isrc_table_lock);
1181 intr_describe_irq(device_t dev, struct resource *res, void *cookie,
1185 struct intr_irqsrc *isrc;
1188 KASSERT(rman_get_start(res) == rman_get_end(res),
1189 ("%s: more interrupts in resource", __func__));
1191 res_id = (u_int)rman_get_start(res);
1192 isrc = intr_map_get_isrc(res_id);
1193 if (isrc == NULL || isrc->isrc_handlers == 0)
1196 if (isrc->isrc_filter != NULL) {
1200 mtx_lock(&isrc_table_lock);
1201 isrc_update_name(isrc, descr);
1202 mtx_unlock(&isrc_table_lock);
1206 error = intr_event_describe_handler(isrc->isrc_event, cookie, descr);
1208 mtx_lock(&isrc_table_lock);
1209 intrcnt_updatename(isrc);
1210 mtx_unlock(&isrc_table_lock);
1217 intr_bind_irq(device_t dev, struct resource *res, int cpu)
1219 struct intr_irqsrc *isrc;
1222 KASSERT(rman_get_start(res) == rman_get_end(res),
1223 ("%s: more interrupts in resource", __func__));
1225 res_id = (u_int)rman_get_start(res);
1226 isrc = intr_map_get_isrc(res_id);
1227 if (isrc == NULL || isrc->isrc_handlers == 0)
1230 if (isrc->isrc_filter != NULL)
1231 return (intr_isrc_assign_cpu(isrc, cpu));
1233 return (intr_event_bind(isrc->isrc_event, cpu));
1237 * Return the CPU that the next interrupt source should use.
1238 * For now just returns the next CPU according to round-robin.
1241 intr_irq_next_cpu(u_int last_cpu, cpuset_t *cpumask)
1245 KASSERT(!CPU_EMPTY(cpumask), ("%s: Empty CPU mask", __func__));
1246 if (!irq_assign_cpu || mp_ncpus == 1) {
1247 cpu = PCPU_GET(cpuid);
1249 if (CPU_ISSET(cpu, cpumask))
1252 return (CPU_FFS(cpumask) - 1);
1257 if (last_cpu > mp_maxid)
1259 } while (!CPU_ISSET(last_cpu, cpumask));
1263 #ifndef EARLY_AP_STARTUP
1265 * Distribute all the interrupt sources among the available
1266 * CPUs once the AP's have been launched.
1269 intr_irq_shuffle(void *arg __unused)
1271 struct intr_irqsrc *isrc;
1277 mtx_lock(&isrc_table_lock);
1278 irq_assign_cpu = true;
1279 for (i = 0; i < intr_nirq; i++) {
1280 isrc = irq_sources[i];
1281 if (isrc == NULL || isrc->isrc_handlers == 0 ||
1282 isrc->isrc_flags & (INTR_ISRCF_PPI | INTR_ISRCF_IPI))
1285 if (isrc->isrc_event != NULL &&
1286 isrc->isrc_flags & INTR_ISRCF_BOUND &&
1287 isrc->isrc_event->ie_cpu != CPU_FFS(&isrc->isrc_cpu) - 1)
1288 panic("%s: CPU inconsistency", __func__);
1290 if ((isrc->isrc_flags & INTR_ISRCF_BOUND) == 0)
1291 CPU_ZERO(&isrc->isrc_cpu); /* start again */
1294 * We are in wicked position here if the following call fails
1295 * for bound ISRC. The best thing we can do is to clear
1296 * isrc_cpu so inconsistency with ie_cpu will be detectable.
1298 if (PIC_BIND_INTR(isrc->isrc_dev, isrc) != 0)
1299 CPU_ZERO(&isrc->isrc_cpu);
1301 mtx_unlock(&isrc_table_lock);
1303 SYSINIT(intr_irq_shuffle, SI_SUB_SMP, SI_ORDER_SECOND, intr_irq_shuffle, NULL);
1304 #endif /* !EARLY_AP_STARTUP */
1308 intr_irq_next_cpu(u_int current_cpu, cpuset_t *cpumask)
1311 return (PCPU_GET(cpuid));
1316 * Allocate memory for new intr_map_data structure.
1317 * Initialize common fields.
1319 struct intr_map_data *
1320 intr_alloc_map_data(enum intr_map_data_type type, size_t len, int flags)
1322 struct intr_map_data *data;
1324 data = malloc(len, M_INTRNG, flags);
1330 void intr_free_intr_map_data(struct intr_map_data *data)
1333 free(data, M_INTRNG);
1337 * Register a MSI/MSI-X interrupt controller
1340 intr_msi_register(device_t dev, intptr_t xref)
1342 struct intr_pic *pic;
1346 pic = pic_create(dev, xref, FLAG_MSI);
1350 debugf("PIC %p registered for %s <dev %p, xref %jx>\n", pic,
1351 device_get_nameunit(dev), dev, (uintmax_t)xref);
1356 intr_alloc_msi(device_t pci, device_t child, intptr_t xref, int count,
1357 int maxcount, int *irqs)
1359 struct iommu_domain *domain;
1360 struct intr_irqsrc **isrc;
1361 struct intr_pic *pic;
1363 struct intr_map_data_msi *msi;
1366 pic = pic_lookup(NULL, xref, FLAG_MSI);
1370 KASSERT((pic->pic_flags & FLAG_TYPE_MASK) == FLAG_MSI,
1371 ("%s: Found a non-MSI controller: %s", __func__,
1372 device_get_name(pic->pic_dev)));
1375 * If this is the first time we have used this context ask the
1376 * interrupt controller to map memory the msi source will need.
1378 err = MSI_IOMMU_INIT(pic->pic_dev, child, &domain);
1382 isrc = malloc(sizeof(*isrc) * count, M_INTRNG, M_WAITOK);
1383 err = MSI_ALLOC_MSI(pic->pic_dev, child, count, maxcount, &pdev, isrc);
1385 free(isrc, M_INTRNG);
1389 for (i = 0; i < count; i++) {
1390 isrc[i]->isrc_iommu = domain;
1391 msi = (struct intr_map_data_msi *)intr_alloc_map_data(
1392 INTR_MAP_DATA_MSI, sizeof(*msi), M_WAITOK | M_ZERO);
1393 msi-> isrc = isrc[i];
1395 irqs[i] = intr_map_irq(pic->pic_dev, xref,
1396 (struct intr_map_data *)msi);
1398 free(isrc, M_INTRNG);
1404 intr_release_msi(device_t pci, device_t child, intptr_t xref, int count,
1407 struct intr_irqsrc **isrc;
1408 struct intr_pic *pic;
1409 struct intr_map_data_msi *msi;
1412 pic = pic_lookup(NULL, xref, FLAG_MSI);
1416 KASSERT((pic->pic_flags & FLAG_TYPE_MASK) == FLAG_MSI,
1417 ("%s: Found a non-MSI controller: %s", __func__,
1418 device_get_name(pic->pic_dev)));
1420 isrc = malloc(sizeof(*isrc) * count, M_INTRNG, M_WAITOK);
1422 for (i = 0; i < count; i++) {
1423 msi = (struct intr_map_data_msi *)
1424 intr_map_get_map_data(irqs[i]);
1425 KASSERT(msi->hdr.type == INTR_MAP_DATA_MSI,
1426 ("%s: irq %d map data is not MSI", __func__,
1428 isrc[i] = msi->isrc;
1431 MSI_IOMMU_DEINIT(pic->pic_dev, child);
1433 err = MSI_RELEASE_MSI(pic->pic_dev, child, count, isrc);
1435 for (i = 0; i < count; i++) {
1436 if (isrc[i] != NULL)
1437 intr_unmap_irq(irqs[i]);
1440 free(isrc, M_INTRNG);
1445 intr_alloc_msix(device_t pci, device_t child, intptr_t xref, int *irq)
1447 struct iommu_domain *domain;
1448 struct intr_irqsrc *isrc;
1449 struct intr_pic *pic;
1451 struct intr_map_data_msi *msi;
1454 pic = pic_lookup(NULL, xref, FLAG_MSI);
1458 KASSERT((pic->pic_flags & FLAG_TYPE_MASK) == FLAG_MSI,
1459 ("%s: Found a non-MSI controller: %s", __func__,
1460 device_get_name(pic->pic_dev)));
1463 * If this is the first time we have used this context ask the
1464 * interrupt controller to map memory the msi source will need.
1466 err = MSI_IOMMU_INIT(pic->pic_dev, child, &domain);
1470 err = MSI_ALLOC_MSIX(pic->pic_dev, child, &pdev, &isrc);
1474 isrc->isrc_iommu = domain;
1475 msi = (struct intr_map_data_msi *)intr_alloc_map_data(
1476 INTR_MAP_DATA_MSI, sizeof(*msi), M_WAITOK | M_ZERO);
1478 *irq = intr_map_irq(pic->pic_dev, xref, (struct intr_map_data *)msi);
1483 intr_release_msix(device_t pci, device_t child, intptr_t xref, int irq)
1485 struct intr_irqsrc *isrc;
1486 struct intr_pic *pic;
1487 struct intr_map_data_msi *msi;
1490 pic = pic_lookup(NULL, xref, FLAG_MSI);
1494 KASSERT((pic->pic_flags & FLAG_TYPE_MASK) == FLAG_MSI,
1495 ("%s: Found a non-MSI controller: %s", __func__,
1496 device_get_name(pic->pic_dev)));
1498 msi = (struct intr_map_data_msi *)
1499 intr_map_get_map_data(irq);
1500 KASSERT(msi->hdr.type == INTR_MAP_DATA_MSI,
1501 ("%s: irq %d map data is not MSI", __func__,
1505 intr_unmap_irq(irq);
1509 MSI_IOMMU_DEINIT(pic->pic_dev, child);
1511 err = MSI_RELEASE_MSIX(pic->pic_dev, child, isrc);
1512 intr_unmap_irq(irq);
1518 intr_map_msi(device_t pci, device_t child, intptr_t xref, int irq,
1519 uint64_t *addr, uint32_t *data)
1521 struct intr_irqsrc *isrc;
1522 struct intr_pic *pic;
1525 pic = pic_lookup(NULL, xref, FLAG_MSI);
1529 KASSERT((pic->pic_flags & FLAG_TYPE_MASK) == FLAG_MSI,
1530 ("%s: Found a non-MSI controller: %s", __func__,
1531 device_get_name(pic->pic_dev)));
1533 isrc = intr_map_get_isrc(irq);
1537 err = MSI_MAP_MSI(pic->pic_dev, child, isrc, addr, data);
1540 if (isrc->isrc_iommu != NULL)
1541 iommu_translate_msi(isrc->isrc_iommu, addr);
1547 void dosoftints(void);
1555 * Init interrupt controller on another CPU.
1558 intr_pic_init_secondary(void)
1562 * QQQ: Only root PIC is aware of other CPUs ???
1564 KASSERT(intr_irq_root_dev != NULL, ("%s: no root attached", __func__));
1566 //mtx_lock(&isrc_table_lock);
1567 PIC_INIT_SECONDARY(intr_irq_root_dev);
1568 //mtx_unlock(&isrc_table_lock);
1573 DB_SHOW_COMMAND(irqs, db_show_irqs)
1577 struct intr_irqsrc *isrc;
1579 for (irqsum = 0, i = 0; i < intr_nirq; i++) {
1580 isrc = irq_sources[i];
1584 num = isrc->isrc_count != NULL ? isrc->isrc_count[0] : 0;
1585 db_printf("irq%-3u <%s>: cpu %02lx%s cnt %lu\n", i,
1586 isrc->isrc_name, isrc->isrc_cpu.__bits[0],
1587 isrc->isrc_flags & INTR_ISRCF_BOUND ? " (bound)" : "", num);
1590 db_printf("irq total %u\n", irqsum);
1595 * Interrupt mapping table functions.
1597 * Please, keep this part separately, it can be transformed to
1598 * extension of standard resources.
1600 struct intr_map_entry
1604 struct intr_map_data *map_data;
1605 struct intr_irqsrc *isrc;
1606 /* XXX TODO DISCONECTED PICs */
1610 /* XXX Convert irq_map[] to dynamicaly expandable one. */
1611 static struct intr_map_entry **irq_map;
1612 static u_int irq_map_count;
1613 static u_int irq_map_first_free_idx;
1614 static struct mtx irq_map_lock;
1616 static struct intr_irqsrc *
1617 intr_map_get_isrc(u_int res_id)
1619 struct intr_irqsrc *isrc;
1622 mtx_lock(&irq_map_lock);
1623 if (res_id < irq_map_count && irq_map[res_id] != NULL)
1624 isrc = irq_map[res_id]->isrc;
1625 mtx_unlock(&irq_map_lock);
1631 intr_map_set_isrc(u_int res_id, struct intr_irqsrc *isrc)
1634 mtx_lock(&irq_map_lock);
1635 if (res_id < irq_map_count && irq_map[res_id] != NULL)
1636 irq_map[res_id]->isrc = isrc;
1637 mtx_unlock(&irq_map_lock);
1641 * Get a copy of intr_map_entry data
1643 static struct intr_map_data *
1644 intr_map_get_map_data(u_int res_id)
1646 struct intr_map_data *data;
1649 mtx_lock(&irq_map_lock);
1650 if (res_id >= irq_map_count || irq_map[res_id] == NULL)
1651 panic("Attempt to copy invalid resource id: %u\n", res_id);
1652 data = irq_map[res_id]->map_data;
1653 mtx_unlock(&irq_map_lock);
1659 * Get a copy of intr_map_entry data
1662 intr_map_copy_map_data(u_int res_id, device_t *map_dev, intptr_t *map_xref,
1663 struct intr_map_data **data)
1668 mtx_lock(&irq_map_lock);
1669 if (res_id >= irq_map_count || irq_map[res_id] == NULL)
1670 panic("Attempt to copy invalid resource id: %u\n", res_id);
1671 if (irq_map[res_id]->map_data != NULL)
1672 len = irq_map[res_id]->map_data->len;
1673 mtx_unlock(&irq_map_lock);
1678 *data = malloc(len, M_INTRNG, M_WAITOK | M_ZERO);
1679 mtx_lock(&irq_map_lock);
1680 if (irq_map[res_id] == NULL)
1681 panic("Attempt to copy invalid resource id: %u\n", res_id);
1683 if (len != irq_map[res_id]->map_data->len)
1684 panic("Resource id: %u has changed.\n", res_id);
1685 memcpy(*data, irq_map[res_id]->map_data, len);
1687 *map_dev = irq_map[res_id]->dev;
1688 *map_xref = irq_map[res_id]->xref;
1689 mtx_unlock(&irq_map_lock);
1693 * Allocate and fill new entry in irq_map table.
1696 intr_map_irq(device_t dev, intptr_t xref, struct intr_map_data *data)
1699 struct intr_map_entry *entry;
1701 /* Prepare new entry first. */
1702 entry = malloc(sizeof(*entry), M_INTRNG, M_WAITOK | M_ZERO);
1706 entry->map_data = data;
1709 mtx_lock(&irq_map_lock);
1710 for (i = irq_map_first_free_idx; i < irq_map_count; i++) {
1711 if (irq_map[i] == NULL) {
1713 irq_map_first_free_idx = i + 1;
1714 mtx_unlock(&irq_map_lock);
1718 for (i = 0; i < irq_map_first_free_idx; i++) {
1719 if (irq_map[i] == NULL) {
1721 irq_map_first_free_idx = i + 1;
1722 mtx_unlock(&irq_map_lock);
1726 mtx_unlock(&irq_map_lock);
1728 /* XXX Expand irq_map table */
1729 panic("IRQ mapping table is full.");
1733 * Remove and free mapping entry.
1736 intr_unmap_irq(u_int res_id)
1738 struct intr_map_entry *entry;
1740 mtx_lock(&irq_map_lock);
1741 if ((res_id >= irq_map_count) || (irq_map[res_id] == NULL))
1742 panic("Attempt to unmap invalid resource id: %u\n", res_id);
1743 entry = irq_map[res_id];
1744 irq_map[res_id] = NULL;
1745 irq_map_first_free_idx = res_id;
1746 mtx_unlock(&irq_map_lock);
1747 intr_free_intr_map_data(entry->map_data);
1748 free(entry, M_INTRNG);
1752 * Clone mapping entry.
1755 intr_map_clone_irq(u_int old_res_id)
1759 struct intr_map_data *data;
1761 intr_map_copy_map_data(old_res_id, &map_dev, &map_xref, &data);
1762 return (intr_map_irq(map_dev, map_xref, data));
1766 intr_map_init(void *dummy __unused)
1769 mtx_init(&irq_map_lock, "intr map table", NULL, MTX_DEF);
1771 irq_map_count = 2 * intr_nirq;
1772 irq_map = mallocarray(irq_map_count, sizeof(struct intr_map_entry*),
1773 M_INTRNG, M_WAITOK | M_ZERO);
1775 SYSINIT(intr_map_init, SI_SUB_INTR, SI_ORDER_FIRST, intr_map_init, NULL);