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"
41 #include "opt_iommu.h"
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
47 #include <sys/mutex.h>
48 #include <sys/syslog.h>
49 #include <sys/malloc.h>
51 #include <sys/queue.h>
53 #include <sys/interrupt.h>
54 #include <sys/taskqueue.h>
57 #include <sys/cpuset.h>
59 #include <sys/sched.h>
61 #include <sys/sysctl.h>
62 #include <sys/vmmeter.h>
64 #include <sys/pmckern.h>
67 #include <machine/atomic.h>
68 #include <machine/intr.h>
69 #include <machine/cpu.h>
70 #include <machine/smp.h>
71 #include <machine/stdarg.h>
78 #include <dev/iommu/iommu_msi.h>
84 #define INTRNAME_LEN (2*MAXCOMLEN + 1)
87 #define debugf(fmt, args...) do { printf("%s(): ", __func__); \
88 printf(fmt,##args); } while (0)
90 #define debugf(fmt, args...)
93 MALLOC_DECLARE(M_INTRNG);
94 MALLOC_DEFINE(M_INTRNG, "intr", "intr interrupt handling");
96 /* Main interrupt handler called from assembler -> 'hidden' for C code. */
97 void intr_irq_handler(struct trapframe *tf);
99 /* Root interrupt controller stuff. */
100 device_t intr_irq_root_dev;
101 static intr_irq_filter_t *irq_root_filter;
102 static void *irq_root_arg;
103 static u_int irq_root_ipicount;
105 struct intr_pic_child {
106 SLIST_ENTRY(intr_pic_child) pc_next;
107 struct intr_pic *pc_pic;
108 intr_child_irq_filter_t *pc_filter;
114 /* Interrupt controller definition. */
116 SLIST_ENTRY(intr_pic) pic_next;
117 intptr_t pic_xref; /* hardware identification */
119 /* Only one of FLAG_PIC or FLAG_MSI may be set */
120 #define FLAG_PIC (1 << 0)
121 #define FLAG_MSI (1 << 1)
122 #define FLAG_TYPE_MASK (FLAG_PIC | FLAG_MSI)
124 struct mtx pic_child_lock;
125 SLIST_HEAD(, intr_pic_child) pic_children;
128 static struct mtx pic_list_lock;
129 static SLIST_HEAD(, intr_pic) pic_list;
131 static struct intr_pic *pic_lookup(device_t dev, intptr_t xref, int flags);
133 /* Interrupt source definition. */
134 static struct mtx isrc_table_lock;
135 static struct intr_irqsrc **irq_sources;
139 #ifdef EARLY_AP_STARTUP
140 static bool irq_assign_cpu = true;
142 static bool irq_assign_cpu = false;
146 u_int intr_nirq = NIRQ;
147 SYSCTL_UINT(_machdep, OID_AUTO, nirq, CTLFLAG_RDTUN, &intr_nirq, 0,
150 /* Data for MI statistics reporting. */
155 static u_int intrcnt_index;
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)
171 SLIST_INIT(&pic_list);
172 mtx_init(&pic_list_lock, "intr pic list", NULL, MTX_DEF);
174 mtx_init(&isrc_table_lock, "intr isrc table", NULL, MTX_DEF);
177 * - 2 counters for each I/O interrupt.
178 * - MAXCPU counters for each IPI counters for SMP.
180 intrcnt_count = intr_nirq * 2;
182 intrcnt_count += INTR_IPI_COUNT * MAXCPU;
185 intrcnt = mallocarray(intrcnt_count, sizeof(u_long), M_INTRNG,
187 intrnames = mallocarray(intrcnt_count, INTRNAME_LEN, M_INTRNG,
189 sintrcnt = intrcnt_count * sizeof(u_long);
190 sintrnames = intrcnt_count * INTRNAME_LEN;
191 irq_sources = mallocarray(intr_nirq, sizeof(struct intr_irqsrc*),
192 M_INTRNG, M_WAITOK | M_ZERO);
194 SYSINIT(intr_irq_init, SI_SUB_INTR, SI_ORDER_FIRST, intr_irq_init, NULL);
197 intrcnt_setname(const char *name, int index)
200 snprintf(intrnames + INTRNAME_LEN * index, INTRNAME_LEN, "%-*s",
201 INTRNAME_LEN - 1, name);
205 * Update name for interrupt source with interrupt event.
208 intrcnt_updatename(struct intr_irqsrc *isrc)
211 /* QQQ: What about stray counter name? */
212 mtx_assert(&isrc_table_lock, MA_OWNED);
213 intrcnt_setname(isrc->isrc_event->ie_fullname, isrc->isrc_index);
217 * Virtualization for interrupt source interrupt counter increment.
220 isrc_increment_count(struct intr_irqsrc *isrc)
223 if (isrc->isrc_flags & INTR_ISRCF_PPI)
224 atomic_add_long(&isrc->isrc_count[0], 1);
226 isrc->isrc_count[0]++;
230 * Virtualization for interrupt source interrupt stray counter increment.
233 isrc_increment_straycount(struct intr_irqsrc *isrc)
236 isrc->isrc_count[1]++;
240 * Virtualization for interrupt source interrupt name update.
243 isrc_update_name(struct intr_irqsrc *isrc, const char *name)
245 char str[INTRNAME_LEN];
247 mtx_assert(&isrc_table_lock, MA_OWNED);
250 snprintf(str, INTRNAME_LEN, "%s: %s", isrc->isrc_name, name);
251 intrcnt_setname(str, isrc->isrc_index);
252 snprintf(str, INTRNAME_LEN, "stray %s: %s", isrc->isrc_name,
254 intrcnt_setname(str, isrc->isrc_index + 1);
256 snprintf(str, INTRNAME_LEN, "%s:", isrc->isrc_name);
257 intrcnt_setname(str, isrc->isrc_index);
258 snprintf(str, INTRNAME_LEN, "stray %s:", isrc->isrc_name);
259 intrcnt_setname(str, isrc->isrc_index + 1);
264 * Virtualization for interrupt source interrupt counters setup.
267 isrc_setup_counters(struct intr_irqsrc *isrc)
272 * XXX - it does not work well with removable controllers and
273 * interrupt sources !!!
275 index = atomic_fetchadd_int(&intrcnt_index, 2);
276 isrc->isrc_index = index;
277 isrc->isrc_count = &intrcnt[index];
278 isrc_update_name(isrc, NULL);
282 * Virtualization for interrupt source interrupt counters release.
285 isrc_release_counters(struct intr_irqsrc *isrc)
288 panic("%s: not implemented", __func__);
293 * Virtualization for interrupt source IPI counters setup.
296 intr_ipi_setup_counters(const char *name)
299 char str[INTRNAME_LEN];
301 index = atomic_fetchadd_int(&intrcnt_index, MAXCPU);
302 for (i = 0; i < MAXCPU; i++) {
303 snprintf(str, INTRNAME_LEN, "cpu%d:%s", i, name);
304 intrcnt_setname(str, index + i);
306 return (&intrcnt[index]);
311 * Main interrupt dispatch handler. It's called straight
312 * from the assembler, where CPU interrupt is served.
315 intr_irq_handler(struct trapframe *tf)
317 struct trapframe * oldframe;
320 KASSERT(irq_root_filter != NULL, ("%s: no filter", __func__));
325 oldframe = td->td_intr_frame;
326 td->td_intr_frame = tf;
327 irq_root_filter(irq_root_arg);
328 td->td_intr_frame = oldframe;
331 if (pmc_hook && TRAPF_USERMODE(tf) &&
332 (PCPU_GET(curthread)->td_pflags & TDP_CALLCHAIN))
333 pmc_hook(PCPU_GET(curthread), PMC_FN_USER_CALLCHAIN, tf);
338 intr_child_irq_handler(struct intr_pic *parent, uintptr_t irq)
340 struct intr_pic_child *child;
344 mtx_lock_spin(&parent->pic_child_lock);
345 SLIST_FOREACH(child, &parent->pic_children, pc_next) {
346 if (child->pc_start <= irq &&
347 irq < (child->pc_start + child->pc_length)) {
352 mtx_unlock_spin(&parent->pic_child_lock);
355 return (child->pc_filter(child->pc_filter_arg, irq));
357 return (FILTER_STRAY);
361 * interrupt controller dispatch function for interrupts. It should
362 * be called straight from the interrupt controller, when associated interrupt
366 intr_isrc_dispatch(struct intr_irqsrc *isrc, struct trapframe *tf)
369 KASSERT(isrc != NULL, ("%s: no source", __func__));
371 isrc_increment_count(isrc);
374 if (isrc->isrc_filter != NULL) {
376 error = isrc->isrc_filter(isrc->isrc_arg, tf);
377 PIC_POST_FILTER(isrc->isrc_dev, isrc);
378 if (error == FILTER_HANDLED)
382 if (isrc->isrc_event != NULL) {
383 if (intr_event_handle(isrc->isrc_event, tf) == 0)
387 isrc_increment_straycount(isrc);
392 * Alloc unique interrupt number (resource handle) for interrupt source.
394 * There could be various strategies how to allocate free interrupt number
395 * (resource handle) for new interrupt source.
397 * 1. Handles are always allocated forward, so handles are not recycled
398 * immediately. However, if only one free handle left which is reused
402 isrc_alloc_irq(struct intr_irqsrc *isrc)
406 mtx_assert(&isrc_table_lock, MA_OWNED);
409 if (irq_next_free >= maxirqs)
412 for (irq = irq_next_free; irq < maxirqs; irq++) {
413 if (irq_sources[irq] == NULL)
416 for (irq = 0; irq < irq_next_free; irq++) {
417 if (irq_sources[irq] == NULL)
421 irq_next_free = maxirqs;
425 isrc->isrc_irq = irq;
426 irq_sources[irq] = isrc;
428 irq_next_free = irq + 1;
429 if (irq_next_free >= maxirqs)
435 * Free unique interrupt number (resource handle) from interrupt source.
438 isrc_free_irq(struct intr_irqsrc *isrc)
441 mtx_assert(&isrc_table_lock, MA_OWNED);
443 if (isrc->isrc_irq >= intr_nirq)
445 if (irq_sources[isrc->isrc_irq] != isrc)
448 irq_sources[isrc->isrc_irq] = NULL;
449 isrc->isrc_irq = INTR_IRQ_INVALID; /* just to be safe */
454 * Initialize interrupt source and register it into global interrupt table.
457 intr_isrc_register(struct intr_irqsrc *isrc, device_t dev, u_int flags,
458 const char *fmt, ...)
463 bzero(isrc, sizeof(struct intr_irqsrc));
464 isrc->isrc_dev = dev;
465 isrc->isrc_irq = INTR_IRQ_INVALID; /* just to be safe */
466 isrc->isrc_flags = flags;
469 vsnprintf(isrc->isrc_name, INTR_ISRC_NAMELEN, fmt, ap);
472 mtx_lock(&isrc_table_lock);
473 error = isrc_alloc_irq(isrc);
475 mtx_unlock(&isrc_table_lock);
479 * Setup interrupt counters, but not for IPI sources. Those are setup
480 * later and only for used ones (up to INTR_IPI_COUNT) to not exhaust
483 if ((isrc->isrc_flags & INTR_ISRCF_IPI) == 0)
484 isrc_setup_counters(isrc);
485 mtx_unlock(&isrc_table_lock);
490 * Deregister interrupt source from global interrupt table.
493 intr_isrc_deregister(struct intr_irqsrc *isrc)
497 mtx_lock(&isrc_table_lock);
498 if ((isrc->isrc_flags & INTR_ISRCF_IPI) == 0)
499 isrc_release_counters(isrc);
500 error = isrc_free_irq(isrc);
501 mtx_unlock(&isrc_table_lock);
507 * A support function for a PIC to decide if provided ISRC should be inited
508 * on given cpu. The logic of INTR_ISRCF_BOUND flag and isrc_cpu member of
509 * struct intr_irqsrc is the following:
511 * If INTR_ISRCF_BOUND is set, the ISRC should be inited only on cpus
512 * set in isrc_cpu. If not, the ISRC should be inited on every cpu and
513 * isrc_cpu is kept consistent with it. Thus isrc_cpu is always correct.
516 intr_isrc_init_on_cpu(struct intr_irqsrc *isrc, u_int cpu)
519 if (isrc->isrc_handlers == 0)
521 if ((isrc->isrc_flags & (INTR_ISRCF_PPI | INTR_ISRCF_IPI)) == 0)
523 if (isrc->isrc_flags & INTR_ISRCF_BOUND)
524 return (CPU_ISSET(cpu, &isrc->isrc_cpu));
526 CPU_SET(cpu, &isrc->isrc_cpu);
533 * Setup filter into interrupt source.
536 iscr_setup_filter(struct intr_irqsrc *isrc, const char *name,
537 intr_irq_filter_t *filter, void *arg, void **cookiep)
543 mtx_lock(&isrc_table_lock);
545 * Make sure that we do not mix the two ways
546 * how we handle interrupt sources.
548 if (isrc->isrc_filter != NULL || isrc->isrc_event != NULL) {
549 mtx_unlock(&isrc_table_lock);
552 isrc->isrc_filter = filter;
553 isrc->isrc_arg = arg;
554 isrc_update_name(isrc, name);
555 mtx_unlock(&isrc_table_lock);
563 * Interrupt source pre_ithread method for MI interrupt framework.
566 intr_isrc_pre_ithread(void *arg)
568 struct intr_irqsrc *isrc = arg;
570 PIC_PRE_ITHREAD(isrc->isrc_dev, isrc);
574 * Interrupt source post_ithread method for MI interrupt framework.
577 intr_isrc_post_ithread(void *arg)
579 struct intr_irqsrc *isrc = arg;
581 PIC_POST_ITHREAD(isrc->isrc_dev, isrc);
585 * Interrupt source post_filter method for MI interrupt framework.
588 intr_isrc_post_filter(void *arg)
590 struct intr_irqsrc *isrc = arg;
592 PIC_POST_FILTER(isrc->isrc_dev, isrc);
596 * Interrupt source assign_cpu method for MI interrupt framework.
599 intr_isrc_assign_cpu(void *arg, int cpu)
602 struct intr_irqsrc *isrc = arg;
605 mtx_lock(&isrc_table_lock);
607 CPU_ZERO(&isrc->isrc_cpu);
608 isrc->isrc_flags &= ~INTR_ISRCF_BOUND;
610 CPU_SETOF(cpu, &isrc->isrc_cpu);
611 isrc->isrc_flags |= INTR_ISRCF_BOUND;
615 * In NOCPU case, it's up to PIC to either leave ISRC on same CPU or
616 * re-balance it to another CPU or enable it on more CPUs. However,
617 * PIC is expected to change isrc_cpu appropriately to keep us well
618 * informed if the call is successful.
620 if (irq_assign_cpu) {
621 error = PIC_BIND_INTR(isrc->isrc_dev, isrc);
623 CPU_ZERO(&isrc->isrc_cpu);
624 mtx_unlock(&isrc_table_lock);
628 mtx_unlock(&isrc_table_lock);
636 * Create interrupt event for interrupt source.
639 isrc_event_create(struct intr_irqsrc *isrc)
641 struct intr_event *ie;
644 error = intr_event_create(&ie, isrc, 0, isrc->isrc_irq,
645 intr_isrc_pre_ithread, intr_isrc_post_ithread, intr_isrc_post_filter,
646 intr_isrc_assign_cpu, "%s:", isrc->isrc_name);
650 mtx_lock(&isrc_table_lock);
652 * Make sure that we do not mix the two ways
653 * how we handle interrupt sources. Let contested event wins.
656 if (isrc->isrc_filter != NULL || isrc->isrc_event != NULL) {
658 if (isrc->isrc_event != NULL) {
660 mtx_unlock(&isrc_table_lock);
661 intr_event_destroy(ie);
662 return (isrc->isrc_event != NULL ? EBUSY : 0);
664 isrc->isrc_event = ie;
665 mtx_unlock(&isrc_table_lock);
671 * Destroy interrupt event for interrupt source.
674 isrc_event_destroy(struct intr_irqsrc *isrc)
676 struct intr_event *ie;
678 mtx_lock(&isrc_table_lock);
679 ie = isrc->isrc_event;
680 isrc->isrc_event = NULL;
681 mtx_unlock(&isrc_table_lock);
684 intr_event_destroy(ie);
688 * Add handler to interrupt source.
691 isrc_add_handler(struct intr_irqsrc *isrc, const char *name,
692 driver_filter_t filter, driver_intr_t handler, void *arg,
693 enum intr_type flags, void **cookiep)
697 if (isrc->isrc_event == NULL) {
698 error = isrc_event_create(isrc);
703 error = intr_event_add_handler(isrc->isrc_event, name, filter, handler,
704 arg, intr_priority(flags), flags, cookiep);
706 mtx_lock(&isrc_table_lock);
707 intrcnt_updatename(isrc);
708 mtx_unlock(&isrc_table_lock);
715 * Lookup interrupt controller locked.
717 static inline struct intr_pic *
718 pic_lookup_locked(device_t dev, intptr_t xref, int flags)
720 struct intr_pic *pic;
722 mtx_assert(&pic_list_lock, MA_OWNED);
724 if (dev == NULL && xref == 0)
727 /* Note that pic->pic_dev is never NULL on registered PIC. */
728 SLIST_FOREACH(pic, &pic_list, pic_next) {
729 if ((pic->pic_flags & FLAG_TYPE_MASK) !=
730 (flags & FLAG_TYPE_MASK))
734 if (xref == pic->pic_xref)
736 } else if (xref == 0 || pic->pic_xref == 0) {
737 if (dev == pic->pic_dev)
739 } else if (xref == pic->pic_xref && dev == pic->pic_dev)
746 * Lookup interrupt controller.
748 static struct intr_pic *
749 pic_lookup(device_t dev, intptr_t xref, int flags)
751 struct intr_pic *pic;
753 mtx_lock(&pic_list_lock);
754 pic = pic_lookup_locked(dev, xref, flags);
755 mtx_unlock(&pic_list_lock);
760 * Create interrupt controller.
762 static struct intr_pic *
763 pic_create(device_t dev, intptr_t xref, int flags)
765 struct intr_pic *pic;
767 mtx_lock(&pic_list_lock);
768 pic = pic_lookup_locked(dev, xref, flags);
770 mtx_unlock(&pic_list_lock);
773 pic = malloc(sizeof(*pic), M_INTRNG, M_NOWAIT | M_ZERO);
775 mtx_unlock(&pic_list_lock);
778 pic->pic_xref = xref;
780 pic->pic_flags = flags;
781 mtx_init(&pic->pic_child_lock, "pic child lock", NULL, MTX_SPIN);
782 SLIST_INSERT_HEAD(&pic_list, pic, pic_next);
783 mtx_unlock(&pic_list_lock);
789 * Destroy interrupt controller.
792 pic_destroy(device_t dev, intptr_t xref, int flags)
794 struct intr_pic *pic;
796 mtx_lock(&pic_list_lock);
797 pic = pic_lookup_locked(dev, xref, flags);
799 mtx_unlock(&pic_list_lock);
802 SLIST_REMOVE(&pic_list, pic, intr_pic, pic_next);
803 mtx_unlock(&pic_list_lock);
809 * Register interrupt controller.
812 intr_pic_register(device_t dev, intptr_t xref)
814 struct intr_pic *pic;
818 pic = pic_create(dev, xref, FLAG_PIC);
822 debugf("PIC %p registered for %s <dev %p, xref %jx>\n", pic,
823 device_get_nameunit(dev), dev, (uintmax_t)xref);
828 * Unregister interrupt controller.
831 intr_pic_deregister(device_t dev, intptr_t xref)
834 panic("%s: not implemented", __func__);
838 * Mark interrupt controller (itself) as a root one.
840 * Note that only an interrupt controller can really know its position
841 * in interrupt controller's tree. So root PIC must claim itself as a root.
843 * In FDT case, according to ePAPR approved version 1.1 from 08 April 2011,
845 * "The root of the interrupt tree is determined when traversal
846 * of the interrupt tree reaches an interrupt controller node without
847 * an interrupts property and thus no explicit interrupt parent."
850 intr_pic_claim_root(device_t dev, intptr_t xref, intr_irq_filter_t *filter,
851 void *arg, u_int ipicount)
853 struct intr_pic *pic;
855 pic = pic_lookup(dev, xref, FLAG_PIC);
857 device_printf(dev, "not registered\n");
861 KASSERT((pic->pic_flags & FLAG_TYPE_MASK) == FLAG_PIC,
862 ("%s: Found a non-PIC controller: %s", __func__,
863 device_get_name(pic->pic_dev)));
865 if (filter == NULL) {
866 device_printf(dev, "filter missing\n");
871 * Only one interrupt controllers could be on the root for now.
872 * Note that we further suppose that there is not threaded interrupt
873 * routine (handler) on the root. See intr_irq_handler().
875 if (intr_irq_root_dev != NULL) {
876 device_printf(dev, "another root already set\n");
880 intr_irq_root_dev = dev;
881 irq_root_filter = filter;
883 irq_root_ipicount = ipicount;
885 debugf("irq root set to %s\n", device_get_nameunit(dev));
890 * Add a handler to manage a sub range of a parents interrupts.
893 intr_pic_add_handler(device_t parent, struct intr_pic *pic,
894 intr_child_irq_filter_t *filter, void *arg, uintptr_t start,
897 struct intr_pic *parent_pic;
898 struct intr_pic_child *newchild;
900 struct intr_pic_child *child;
903 /* Find the parent PIC */
904 parent_pic = pic_lookup(parent, 0, FLAG_PIC);
905 if (parent_pic == NULL)
908 newchild = malloc(sizeof(*newchild), M_INTRNG, M_WAITOK | M_ZERO);
909 newchild->pc_pic = pic;
910 newchild->pc_filter = filter;
911 newchild->pc_filter_arg = arg;
912 newchild->pc_start = start;
913 newchild->pc_length = length;
915 mtx_lock_spin(&parent_pic->pic_child_lock);
917 SLIST_FOREACH(child, &parent_pic->pic_children, pc_next) {
918 KASSERT(child->pc_pic != pic, ("%s: Adding a child PIC twice",
922 SLIST_INSERT_HEAD(&parent_pic->pic_children, newchild, pc_next);
923 mtx_unlock_spin(&parent_pic->pic_child_lock);
929 intr_resolve_irq(device_t dev, intptr_t xref, struct intr_map_data *data,
930 struct intr_irqsrc **isrc)
932 struct intr_pic *pic;
933 struct intr_map_data_msi *msi;
938 pic = pic_lookup(dev, xref,
939 (data->type == INTR_MAP_DATA_MSI) ? FLAG_MSI : FLAG_PIC);
943 switch (data->type) {
944 case INTR_MAP_DATA_MSI:
945 KASSERT((pic->pic_flags & FLAG_TYPE_MASK) == FLAG_MSI,
946 ("%s: Found a non-MSI controller: %s", __func__,
947 device_get_name(pic->pic_dev)));
948 msi = (struct intr_map_data_msi *)data;
953 KASSERT((pic->pic_flags & FLAG_TYPE_MASK) == FLAG_PIC,
954 ("%s: Found a non-PIC controller: %s", __func__,
955 device_get_name(pic->pic_dev)));
956 return (PIC_MAP_INTR(pic->pic_dev, data, isrc));
961 intr_is_per_cpu(struct resource *res)
964 struct intr_irqsrc *isrc;
966 res_id = (u_int)rman_get_start(res);
967 isrc = intr_map_get_isrc(res_id);
970 panic("Attempt to get isrc for non-active resource id: %u\n",
972 return ((isrc->isrc_flags & INTR_ISRCF_PPI) != 0);
976 intr_activate_irq(device_t dev, struct resource *res)
980 struct intr_map_data *data;
981 struct intr_irqsrc *isrc;
985 KASSERT(rman_get_start(res) == rman_get_end(res),
986 ("%s: more interrupts in resource", __func__));
988 res_id = (u_int)rman_get_start(res);
989 if (intr_map_get_isrc(res_id) != NULL)
990 panic("Attempt to double activation of resource id: %u\n",
992 intr_map_copy_map_data(res_id, &map_dev, &map_xref, &data);
993 error = intr_resolve_irq(map_dev, map_xref, data, &isrc);
995 free(data, M_INTRNG);
996 /* XXX TODO DISCONECTED PICs */
997 /* if (error == EINVAL) return(0); */
1000 intr_map_set_isrc(res_id, isrc);
1001 rman_set_virtual(res, data);
1002 return (PIC_ACTIVATE_INTR(isrc->isrc_dev, isrc, res, data));
1006 intr_deactivate_irq(device_t dev, struct resource *res)
1008 struct intr_map_data *data;
1009 struct intr_irqsrc *isrc;
1013 KASSERT(rman_get_start(res) == rman_get_end(res),
1014 ("%s: more interrupts in resource", __func__));
1016 res_id = (u_int)rman_get_start(res);
1017 isrc = intr_map_get_isrc(res_id);
1019 panic("Attempt to deactivate non-active resource id: %u\n",
1022 data = rman_get_virtual(res);
1023 error = PIC_DEACTIVATE_INTR(isrc->isrc_dev, isrc, res, data);
1024 intr_map_set_isrc(res_id, NULL);
1025 rman_set_virtual(res, NULL);
1026 free(data, M_INTRNG);
1031 intr_setup_irq(device_t dev, struct resource *res, driver_filter_t filt,
1032 driver_intr_t hand, void *arg, int flags, void **cookiep)
1035 struct intr_map_data *data;
1036 struct intr_irqsrc *isrc;
1040 KASSERT(rman_get_start(res) == rman_get_end(res),
1041 ("%s: more interrupts in resource", __func__));
1043 res_id = (u_int)rman_get_start(res);
1044 isrc = intr_map_get_isrc(res_id);
1046 /* XXX TODO DISCONECTED PICs */
1050 data = rman_get_virtual(res);
1051 name = device_get_nameunit(dev);
1055 * Standard handling is done through MI interrupt framework. However,
1056 * some interrupts could request solely own special handling. This
1057 * non standard handling can be used for interrupt controllers without
1058 * handler (filter only), so in case that interrupt controllers are
1059 * chained, MI interrupt framework is called only in leaf controller.
1061 * Note that root interrupt controller routine is served as well,
1062 * however in intr_irq_handler(), i.e. main system dispatch routine.
1064 if (flags & INTR_SOLO && hand != NULL) {
1065 debugf("irq %u cannot solo on %s\n", irq, name);
1069 if (flags & INTR_SOLO) {
1070 error = iscr_setup_filter(isrc, name, (intr_irq_filter_t *)filt,
1072 debugf("irq %u setup filter error %d on %s\n", isrc->isrc_irq, error,
1077 error = isrc_add_handler(isrc, name, filt, hand, arg, flags,
1079 debugf("irq %u add handler error %d on %s\n", isrc->isrc_irq, error, name);
1084 mtx_lock(&isrc_table_lock);
1085 error = PIC_SETUP_INTR(isrc->isrc_dev, isrc, res, data);
1087 isrc->isrc_handlers++;
1088 if (isrc->isrc_handlers == 1)
1089 PIC_ENABLE_INTR(isrc->isrc_dev, isrc);
1091 mtx_unlock(&isrc_table_lock);
1093 intr_event_remove_handler(*cookiep);
1098 intr_teardown_irq(device_t dev, struct resource *res, void *cookie)
1101 struct intr_map_data *data;
1102 struct intr_irqsrc *isrc;
1105 KASSERT(rman_get_start(res) == rman_get_end(res),
1106 ("%s: more interrupts in resource", __func__));
1108 res_id = (u_int)rman_get_start(res);
1109 isrc = intr_map_get_isrc(res_id);
1110 if (isrc == NULL || isrc->isrc_handlers == 0)
1113 data = rman_get_virtual(res);
1116 if (isrc->isrc_filter != NULL) {
1120 mtx_lock(&isrc_table_lock);
1121 isrc->isrc_filter = NULL;
1122 isrc->isrc_arg = NULL;
1123 isrc->isrc_handlers = 0;
1124 PIC_DISABLE_INTR(isrc->isrc_dev, isrc);
1125 PIC_TEARDOWN_INTR(isrc->isrc_dev, isrc, res, data);
1126 isrc_update_name(isrc, NULL);
1127 mtx_unlock(&isrc_table_lock);
1131 if (isrc != intr_handler_source(cookie))
1134 error = intr_event_remove_handler(cookie);
1136 mtx_lock(&isrc_table_lock);
1137 isrc->isrc_handlers--;
1138 if (isrc->isrc_handlers == 0)
1139 PIC_DISABLE_INTR(isrc->isrc_dev, isrc);
1140 PIC_TEARDOWN_INTR(isrc->isrc_dev, isrc, res, data);
1141 intrcnt_updatename(isrc);
1142 mtx_unlock(&isrc_table_lock);
1148 intr_describe_irq(device_t dev, struct resource *res, void *cookie,
1152 struct intr_irqsrc *isrc;
1155 KASSERT(rman_get_start(res) == rman_get_end(res),
1156 ("%s: more interrupts in resource", __func__));
1158 res_id = (u_int)rman_get_start(res);
1159 isrc = intr_map_get_isrc(res_id);
1160 if (isrc == NULL || isrc->isrc_handlers == 0)
1163 if (isrc->isrc_filter != NULL) {
1167 mtx_lock(&isrc_table_lock);
1168 isrc_update_name(isrc, descr);
1169 mtx_unlock(&isrc_table_lock);
1173 error = intr_event_describe_handler(isrc->isrc_event, cookie, descr);
1175 mtx_lock(&isrc_table_lock);
1176 intrcnt_updatename(isrc);
1177 mtx_unlock(&isrc_table_lock);
1184 intr_bind_irq(device_t dev, struct resource *res, int cpu)
1186 struct intr_irqsrc *isrc;
1189 KASSERT(rman_get_start(res) == rman_get_end(res),
1190 ("%s: more interrupts in resource", __func__));
1192 res_id = (u_int)rman_get_start(res);
1193 isrc = intr_map_get_isrc(res_id);
1194 if (isrc == NULL || isrc->isrc_handlers == 0)
1197 if (isrc->isrc_filter != NULL)
1198 return (intr_isrc_assign_cpu(isrc, cpu));
1200 return (intr_event_bind(isrc->isrc_event, cpu));
1204 * Return the CPU that the next interrupt source should use.
1205 * For now just returns the next CPU according to round-robin.
1208 intr_irq_next_cpu(u_int last_cpu, cpuset_t *cpumask)
1212 KASSERT(!CPU_EMPTY(cpumask), ("%s: Empty CPU mask", __func__));
1213 if (!irq_assign_cpu || mp_ncpus == 1) {
1214 cpu = PCPU_GET(cpuid);
1216 if (CPU_ISSET(cpu, cpumask))
1219 return (CPU_FFS(cpumask) - 1);
1224 if (last_cpu > mp_maxid)
1226 } while (!CPU_ISSET(last_cpu, cpumask));
1230 #ifndef EARLY_AP_STARTUP
1232 * Distribute all the interrupt sources among the available
1233 * CPUs once the AP's have been launched.
1236 intr_irq_shuffle(void *arg __unused)
1238 struct intr_irqsrc *isrc;
1244 mtx_lock(&isrc_table_lock);
1245 irq_assign_cpu = true;
1246 for (i = 0; i < intr_nirq; i++) {
1247 isrc = irq_sources[i];
1248 if (isrc == NULL || isrc->isrc_handlers == 0 ||
1249 isrc->isrc_flags & (INTR_ISRCF_PPI | INTR_ISRCF_IPI))
1252 if (isrc->isrc_event != NULL &&
1253 isrc->isrc_flags & INTR_ISRCF_BOUND &&
1254 isrc->isrc_event->ie_cpu != CPU_FFS(&isrc->isrc_cpu) - 1)
1255 panic("%s: CPU inconsistency", __func__);
1257 if ((isrc->isrc_flags & INTR_ISRCF_BOUND) == 0)
1258 CPU_ZERO(&isrc->isrc_cpu); /* start again */
1261 * We are in wicked position here if the following call fails
1262 * for bound ISRC. The best thing we can do is to clear
1263 * isrc_cpu so inconsistency with ie_cpu will be detectable.
1265 if (PIC_BIND_INTR(isrc->isrc_dev, isrc) != 0)
1266 CPU_ZERO(&isrc->isrc_cpu);
1268 mtx_unlock(&isrc_table_lock);
1270 SYSINIT(intr_irq_shuffle, SI_SUB_SMP, SI_ORDER_SECOND, intr_irq_shuffle, NULL);
1271 #endif /* !EARLY_AP_STARTUP */
1275 intr_irq_next_cpu(u_int current_cpu, cpuset_t *cpumask)
1278 return (PCPU_GET(cpuid));
1283 * Allocate memory for new intr_map_data structure.
1284 * Initialize common fields.
1286 struct intr_map_data *
1287 intr_alloc_map_data(enum intr_map_data_type type, size_t len, int flags)
1289 struct intr_map_data *data;
1291 data = malloc(len, M_INTRNG, flags);
1297 void intr_free_intr_map_data(struct intr_map_data *data)
1300 free(data, M_INTRNG);
1304 * Register a MSI/MSI-X interrupt controller
1307 intr_msi_register(device_t dev, intptr_t xref)
1309 struct intr_pic *pic;
1313 pic = pic_create(dev, xref, FLAG_MSI);
1317 debugf("PIC %p registered for %s <dev %p, xref %jx>\n", pic,
1318 device_get_nameunit(dev), dev, (uintmax_t)xref);
1323 intr_alloc_msi(device_t pci, device_t child, intptr_t xref, int count,
1324 int maxcount, int *irqs)
1326 struct iommu_domain *domain;
1327 struct intr_irqsrc **isrc;
1328 struct intr_pic *pic;
1330 struct intr_map_data_msi *msi;
1333 pic = pic_lookup(NULL, xref, FLAG_MSI);
1337 KASSERT((pic->pic_flags & FLAG_TYPE_MASK) == FLAG_MSI,
1338 ("%s: Found a non-MSI controller: %s", __func__,
1339 device_get_name(pic->pic_dev)));
1342 * If this is the first time we have used this context ask the
1343 * interrupt controller to map memory the msi source will need.
1345 err = MSI_IOMMU_INIT(pic->pic_dev, child, &domain);
1349 isrc = malloc(sizeof(*isrc) * count, M_INTRNG, M_WAITOK);
1350 err = MSI_ALLOC_MSI(pic->pic_dev, child, count, maxcount, &pdev, isrc);
1352 free(isrc, M_INTRNG);
1356 for (i = 0; i < count; i++) {
1357 isrc[i]->isrc_iommu = domain;
1358 msi = (struct intr_map_data_msi *)intr_alloc_map_data(
1359 INTR_MAP_DATA_MSI, sizeof(*msi), M_WAITOK | M_ZERO);
1360 msi-> isrc = isrc[i];
1362 irqs[i] = intr_map_irq(pic->pic_dev, xref,
1363 (struct intr_map_data *)msi);
1365 free(isrc, M_INTRNG);
1371 intr_release_msi(device_t pci, device_t child, intptr_t xref, int count,
1374 struct intr_irqsrc **isrc;
1375 struct intr_pic *pic;
1376 struct intr_map_data_msi *msi;
1379 pic = pic_lookup(NULL, xref, FLAG_MSI);
1383 KASSERT((pic->pic_flags & FLAG_TYPE_MASK) == FLAG_MSI,
1384 ("%s: Found a non-MSI controller: %s", __func__,
1385 device_get_name(pic->pic_dev)));
1387 isrc = malloc(sizeof(*isrc) * count, M_INTRNG, M_WAITOK);
1389 for (i = 0; i < count; i++) {
1390 msi = (struct intr_map_data_msi *)
1391 intr_map_get_map_data(irqs[i]);
1392 KASSERT(msi->hdr.type == INTR_MAP_DATA_MSI,
1393 ("%s: irq %d map data is not MSI", __func__,
1395 isrc[i] = msi->isrc;
1398 MSI_IOMMU_DEINIT(pic->pic_dev, child);
1400 err = MSI_RELEASE_MSI(pic->pic_dev, child, count, isrc);
1402 for (i = 0; i < count; i++) {
1403 if (isrc[i] != NULL)
1404 intr_unmap_irq(irqs[i]);
1407 free(isrc, M_INTRNG);
1412 intr_alloc_msix(device_t pci, device_t child, intptr_t xref, int *irq)
1414 struct iommu_domain *domain;
1415 struct intr_irqsrc *isrc;
1416 struct intr_pic *pic;
1418 struct intr_map_data_msi *msi;
1421 pic = pic_lookup(NULL, xref, FLAG_MSI);
1425 KASSERT((pic->pic_flags & FLAG_TYPE_MASK) == FLAG_MSI,
1426 ("%s: Found a non-MSI controller: %s", __func__,
1427 device_get_name(pic->pic_dev)));
1430 * If this is the first time we have used this context ask the
1431 * interrupt controller to map memory the msi source will need.
1433 err = MSI_IOMMU_INIT(pic->pic_dev, child, &domain);
1437 err = MSI_ALLOC_MSIX(pic->pic_dev, child, &pdev, &isrc);
1441 isrc->isrc_iommu = domain;
1442 msi = (struct intr_map_data_msi *)intr_alloc_map_data(
1443 INTR_MAP_DATA_MSI, sizeof(*msi), M_WAITOK | M_ZERO);
1445 *irq = intr_map_irq(pic->pic_dev, xref, (struct intr_map_data *)msi);
1450 intr_release_msix(device_t pci, device_t child, intptr_t xref, int irq)
1452 struct intr_irqsrc *isrc;
1453 struct intr_pic *pic;
1454 struct intr_map_data_msi *msi;
1457 pic = pic_lookup(NULL, xref, FLAG_MSI);
1461 KASSERT((pic->pic_flags & FLAG_TYPE_MASK) == FLAG_MSI,
1462 ("%s: Found a non-MSI controller: %s", __func__,
1463 device_get_name(pic->pic_dev)));
1465 msi = (struct intr_map_data_msi *)
1466 intr_map_get_map_data(irq);
1467 KASSERT(msi->hdr.type == INTR_MAP_DATA_MSI,
1468 ("%s: irq %d map data is not MSI", __func__,
1472 intr_unmap_irq(irq);
1476 MSI_IOMMU_DEINIT(pic->pic_dev, child);
1478 err = MSI_RELEASE_MSIX(pic->pic_dev, child, isrc);
1479 intr_unmap_irq(irq);
1485 intr_map_msi(device_t pci, device_t child, intptr_t xref, int irq,
1486 uint64_t *addr, uint32_t *data)
1488 struct intr_irqsrc *isrc;
1489 struct intr_pic *pic;
1492 pic = pic_lookup(NULL, xref, FLAG_MSI);
1496 KASSERT((pic->pic_flags & FLAG_TYPE_MASK) == FLAG_MSI,
1497 ("%s: Found a non-MSI controller: %s", __func__,
1498 device_get_name(pic->pic_dev)));
1500 isrc = intr_map_get_isrc(irq);
1504 err = MSI_MAP_MSI(pic->pic_dev, child, isrc, addr, data);
1507 if (isrc->isrc_iommu != NULL)
1508 iommu_translate_msi(isrc->isrc_iommu, addr);
1514 void dosoftints(void);
1522 * Init interrupt controller on another CPU.
1525 intr_pic_init_secondary(void)
1529 * QQQ: Only root PIC is aware of other CPUs ???
1531 KASSERT(intr_irq_root_dev != NULL, ("%s: no root attached", __func__));
1533 //mtx_lock(&isrc_table_lock);
1534 PIC_INIT_SECONDARY(intr_irq_root_dev);
1535 //mtx_unlock(&isrc_table_lock);
1540 DB_SHOW_COMMAND(irqs, db_show_irqs)
1544 struct intr_irqsrc *isrc;
1546 for (irqsum = 0, i = 0; i < intr_nirq; i++) {
1547 isrc = irq_sources[i];
1551 num = isrc->isrc_count != NULL ? isrc->isrc_count[0] : 0;
1552 db_printf("irq%-3u <%s>: cpu %02lx%s cnt %lu\n", i,
1553 isrc->isrc_name, isrc->isrc_cpu.__bits[0],
1554 isrc->isrc_flags & INTR_ISRCF_BOUND ? " (bound)" : "", num);
1557 db_printf("irq total %u\n", irqsum);
1562 * Interrupt mapping table functions.
1564 * Please, keep this part separately, it can be transformed to
1565 * extension of standard resources.
1567 struct intr_map_entry
1571 struct intr_map_data *map_data;
1572 struct intr_irqsrc *isrc;
1573 /* XXX TODO DISCONECTED PICs */
1577 /* XXX Convert irq_map[] to dynamicaly expandable one. */
1578 static struct intr_map_entry **irq_map;
1579 static u_int irq_map_count;
1580 static u_int irq_map_first_free_idx;
1581 static struct mtx irq_map_lock;
1583 static struct intr_irqsrc *
1584 intr_map_get_isrc(u_int res_id)
1586 struct intr_irqsrc *isrc;
1589 mtx_lock(&irq_map_lock);
1590 if (res_id < irq_map_count && irq_map[res_id] != NULL)
1591 isrc = irq_map[res_id]->isrc;
1592 mtx_unlock(&irq_map_lock);
1598 intr_map_set_isrc(u_int res_id, struct intr_irqsrc *isrc)
1601 mtx_lock(&irq_map_lock);
1602 if (res_id < irq_map_count && irq_map[res_id] != NULL)
1603 irq_map[res_id]->isrc = isrc;
1604 mtx_unlock(&irq_map_lock);
1608 * Get a copy of intr_map_entry data
1610 static struct intr_map_data *
1611 intr_map_get_map_data(u_int res_id)
1613 struct intr_map_data *data;
1616 mtx_lock(&irq_map_lock);
1617 if (res_id >= irq_map_count || irq_map[res_id] == NULL)
1618 panic("Attempt to copy invalid resource id: %u\n", res_id);
1619 data = irq_map[res_id]->map_data;
1620 mtx_unlock(&irq_map_lock);
1626 * Get a copy of intr_map_entry data
1629 intr_map_copy_map_data(u_int res_id, device_t *map_dev, intptr_t *map_xref,
1630 struct intr_map_data **data)
1635 mtx_lock(&irq_map_lock);
1636 if (res_id >= irq_map_count || irq_map[res_id] == NULL)
1637 panic("Attempt to copy invalid resource id: %u\n", res_id);
1638 if (irq_map[res_id]->map_data != NULL)
1639 len = irq_map[res_id]->map_data->len;
1640 mtx_unlock(&irq_map_lock);
1645 *data = malloc(len, M_INTRNG, M_WAITOK | M_ZERO);
1646 mtx_lock(&irq_map_lock);
1647 if (irq_map[res_id] == NULL)
1648 panic("Attempt to copy invalid resource id: %u\n", res_id);
1650 if (len != irq_map[res_id]->map_data->len)
1651 panic("Resource id: %u has changed.\n", res_id);
1652 memcpy(*data, irq_map[res_id]->map_data, len);
1654 *map_dev = irq_map[res_id]->dev;
1655 *map_xref = irq_map[res_id]->xref;
1656 mtx_unlock(&irq_map_lock);
1660 * Allocate and fill new entry in irq_map table.
1663 intr_map_irq(device_t dev, intptr_t xref, struct intr_map_data *data)
1666 struct intr_map_entry *entry;
1668 /* Prepare new entry first. */
1669 entry = malloc(sizeof(*entry), M_INTRNG, M_WAITOK | M_ZERO);
1673 entry->map_data = data;
1676 mtx_lock(&irq_map_lock);
1677 for (i = irq_map_first_free_idx; i < irq_map_count; i++) {
1678 if (irq_map[i] == NULL) {
1680 irq_map_first_free_idx = i + 1;
1681 mtx_unlock(&irq_map_lock);
1685 mtx_unlock(&irq_map_lock);
1687 /* XXX Expand irq_map table */
1688 panic("IRQ mapping table is full.");
1692 * Remove and free mapping entry.
1695 intr_unmap_irq(u_int res_id)
1697 struct intr_map_entry *entry;
1699 mtx_lock(&irq_map_lock);
1700 if ((res_id >= irq_map_count) || (irq_map[res_id] == NULL))
1701 panic("Attempt to unmap invalid resource id: %u\n", res_id);
1702 entry = irq_map[res_id];
1703 irq_map[res_id] = NULL;
1704 irq_map_first_free_idx = res_id;
1705 mtx_unlock(&irq_map_lock);
1706 intr_free_intr_map_data(entry->map_data);
1707 free(entry, M_INTRNG);
1711 * Clone mapping entry.
1714 intr_map_clone_irq(u_int old_res_id)
1718 struct intr_map_data *data;
1720 intr_map_copy_map_data(old_res_id, &map_dev, &map_xref, &data);
1721 return (intr_map_irq(map_dev, map_xref, data));
1725 intr_map_init(void *dummy __unused)
1728 mtx_init(&irq_map_lock, "intr map table", NULL, MTX_DEF);
1730 irq_map_count = 2 * intr_nirq;
1731 irq_map = mallocarray(irq_map_count, sizeof(struct intr_map_entry*),
1732 M_INTRNG, M_WAITOK | M_ZERO);
1734 SYSINIT(intr_map_init, SI_SUB_INTR, SI_ORDER_FIRST, intr_map_init, NULL);