2 * Copyright (c) 2015-2016 Svatopluk Kraus
3 * Copyright (c) 2015-2016 Michal Meloun
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
32 * New-style Interrupt Framework
34 * TODO: - add support for disconnected PICs.
35 * - to support IPI (PPI) enabling on other CPUs if already started.
36 * - to complete things for removable PICs.
40 #include "opt_hwpmc_hooks.h"
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
45 #include <sys/syslog.h>
46 #include <sys/malloc.h>
48 #include <sys/queue.h>
50 #include <sys/interrupt.h>
52 #include <sys/cpuset.h>
54 #include <sys/sched.h>
56 #include <sys/vmmeter.h>
58 #include <sys/pmckern.h>
61 #include <machine/atomic.h>
62 #include <machine/intr.h>
63 #include <machine/cpu.h>
64 #include <machine/smp.h>
65 #include <machine/stdarg.h>
74 #define INTRNAME_LEN (2*MAXCOMLEN + 1)
77 #define debugf(fmt, args...) do { printf("%s(): ", __func__); \
78 printf(fmt,##args); } while (0)
80 #define debugf(fmt, args...)
83 MALLOC_DECLARE(M_INTRNG);
84 MALLOC_DEFINE(M_INTRNG, "intr", "intr interrupt handling");
86 /* Main interrupt handler called from assembler -> 'hidden' for C code. */
87 void intr_irq_handler(struct trapframe *tf);
89 /* Root interrupt controller stuff. */
90 device_t intr_irq_root_dev;
91 static intr_irq_filter_t *irq_root_filter;
92 static void *irq_root_arg;
93 static u_int irq_root_ipicount;
95 struct intr_pic_child {
96 SLIST_ENTRY(intr_pic_child) pc_next;
97 struct intr_pic *pc_pic;
98 intr_child_irq_filter_t *pc_filter;
104 /* Interrupt controller definition. */
106 SLIST_ENTRY(intr_pic) pic_next;
107 intptr_t pic_xref; /* hardware identification */
109 /* Only one of FLAG_PIC or FLAG_MSI may be set */
110 #define FLAG_PIC (1 << 0)
111 #define FLAG_MSI (1 << 1)
112 #define FLAG_TYPE_MASK (FLAG_PIC | FLAG_MSI)
114 struct mtx pic_child_lock;
115 SLIST_HEAD(, intr_pic_child) pic_children;
118 static struct mtx pic_list_lock;
119 static SLIST_HEAD(, intr_pic) pic_list;
121 static struct intr_pic *pic_lookup(device_t dev, intptr_t xref, int flags);
123 /* Interrupt source definition. */
124 static struct mtx isrc_table_lock;
125 static struct intr_irqsrc *irq_sources[NIRQ];
129 static boolean_t irq_assign_cpu = FALSE;
133 * - 2 counters for each I/O interrupt.
134 * - MAXCPU counters for each IPI counters for SMP.
137 #define INTRCNT_COUNT (NIRQ * 2 + INTR_IPI_COUNT * MAXCPU)
139 #define INTRCNT_COUNT (NIRQ * 2)
142 /* Data for MI statistics reporting. */
143 u_long intrcnt[INTRCNT_COUNT];
144 char intrnames[INTRCNT_COUNT * INTRNAME_LEN];
145 size_t sintrcnt = sizeof(intrcnt);
146 size_t sintrnames = sizeof(intrnames);
147 static u_int intrcnt_index;
149 static struct intr_irqsrc *intr_map_get_isrc(u_int res_id);
150 static void intr_map_set_isrc(u_int res_id, struct intr_irqsrc *isrc);
151 static struct intr_map_data * intr_map_get_map_data(u_int res_id);
152 static void intr_map_copy_map_data(u_int res_id, device_t *dev, intptr_t *xref,
153 struct intr_map_data **data);
156 * Interrupt framework initialization routine.
159 intr_irq_init(void *dummy __unused)
162 SLIST_INIT(&pic_list);
163 mtx_init(&pic_list_lock, "intr pic list", NULL, MTX_DEF);
165 mtx_init(&isrc_table_lock, "intr isrc table", NULL, MTX_DEF);
167 SYSINIT(intr_irq_init, SI_SUB_INTR, SI_ORDER_FIRST, intr_irq_init, NULL);
170 intrcnt_setname(const char *name, int index)
173 snprintf(intrnames + INTRNAME_LEN * index, INTRNAME_LEN, "%-*s",
174 INTRNAME_LEN - 1, name);
178 * Update name for interrupt source with interrupt event.
181 intrcnt_updatename(struct intr_irqsrc *isrc)
184 /* QQQ: What about stray counter name? */
185 mtx_assert(&isrc_table_lock, MA_OWNED);
186 intrcnt_setname(isrc->isrc_event->ie_fullname, isrc->isrc_index);
190 * Virtualization for interrupt source interrupt counter increment.
193 isrc_increment_count(struct intr_irqsrc *isrc)
196 if (isrc->isrc_flags & INTR_ISRCF_PPI)
197 atomic_add_long(&isrc->isrc_count[0], 1);
199 isrc->isrc_count[0]++;
203 * Virtualization for interrupt source interrupt stray counter increment.
206 isrc_increment_straycount(struct intr_irqsrc *isrc)
209 isrc->isrc_count[1]++;
213 * Virtualization for interrupt source interrupt name update.
216 isrc_update_name(struct intr_irqsrc *isrc, const char *name)
218 char str[INTRNAME_LEN];
220 mtx_assert(&isrc_table_lock, MA_OWNED);
223 snprintf(str, INTRNAME_LEN, "%s: %s", isrc->isrc_name, name);
224 intrcnt_setname(str, isrc->isrc_index);
225 snprintf(str, INTRNAME_LEN, "stray %s: %s", isrc->isrc_name,
227 intrcnt_setname(str, isrc->isrc_index + 1);
229 snprintf(str, INTRNAME_LEN, "%s:", isrc->isrc_name);
230 intrcnt_setname(str, isrc->isrc_index);
231 snprintf(str, INTRNAME_LEN, "stray %s:", isrc->isrc_name);
232 intrcnt_setname(str, isrc->isrc_index + 1);
237 * Virtualization for interrupt source interrupt counters setup.
240 isrc_setup_counters(struct intr_irqsrc *isrc)
245 * XXX - it does not work well with removable controllers and
246 * interrupt sources !!!
248 index = atomic_fetchadd_int(&intrcnt_index, 2);
249 isrc->isrc_index = index;
250 isrc->isrc_count = &intrcnt[index];
251 isrc_update_name(isrc, NULL);
255 * Virtualization for interrupt source interrupt counters release.
258 isrc_release_counters(struct intr_irqsrc *isrc)
261 panic("%s: not implemented", __func__);
266 * Virtualization for interrupt source IPI counters setup.
269 intr_ipi_setup_counters(const char *name)
272 char str[INTRNAME_LEN];
274 index = atomic_fetchadd_int(&intrcnt_index, MAXCPU);
275 for (i = 0; i < MAXCPU; i++) {
276 snprintf(str, INTRNAME_LEN, "cpu%d:%s", i, name);
277 intrcnt_setname(str, index + i);
279 return (&intrcnt[index]);
284 * Main interrupt dispatch handler. It's called straight
285 * from the assembler, where CPU interrupt is served.
288 intr_irq_handler(struct trapframe *tf)
290 struct trapframe * oldframe;
293 KASSERT(irq_root_filter != NULL, ("%s: no filter", __func__));
295 PCPU_INC(cnt.v_intr);
298 oldframe = td->td_intr_frame;
299 td->td_intr_frame = tf;
300 irq_root_filter(irq_root_arg);
301 td->td_intr_frame = oldframe;
304 if (pmc_hook && TRAPF_USERMODE(tf) &&
305 (PCPU_GET(curthread)->td_pflags & TDP_CALLCHAIN))
306 pmc_hook(PCPU_GET(curthread), PMC_FN_USER_CALLCHAIN, tf);
311 intr_child_irq_handler(struct intr_pic *parent, uintptr_t irq)
313 struct intr_pic_child *child;
317 mtx_lock_spin(&parent->pic_child_lock);
318 SLIST_FOREACH(child, &parent->pic_children, pc_next) {
319 if (child->pc_start <= irq &&
320 irq < (child->pc_start + child->pc_length)) {
325 mtx_unlock_spin(&parent->pic_child_lock);
328 return (child->pc_filter(child->pc_filter_arg, irq));
330 return (FILTER_STRAY);
334 * interrupt controller dispatch function for interrupts. It should
335 * be called straight from the interrupt controller, when associated interrupt
339 intr_isrc_dispatch(struct intr_irqsrc *isrc, struct trapframe *tf)
342 KASSERT(isrc != NULL, ("%s: no source", __func__));
344 isrc_increment_count(isrc);
347 if (isrc->isrc_filter != NULL) {
349 error = isrc->isrc_filter(isrc->isrc_arg, tf);
350 PIC_POST_FILTER(isrc->isrc_dev, isrc);
351 if (error == FILTER_HANDLED)
355 if (isrc->isrc_event != NULL) {
356 if (intr_event_handle(isrc->isrc_event, tf) == 0)
360 isrc_increment_straycount(isrc);
365 * Alloc unique interrupt number (resource handle) for interrupt source.
367 * There could be various strategies how to allocate free interrupt number
368 * (resource handle) for new interrupt source.
370 * 1. Handles are always allocated forward, so handles are not recycled
371 * immediately. However, if only one free handle left which is reused
375 isrc_alloc_irq(struct intr_irqsrc *isrc)
379 mtx_assert(&isrc_table_lock, MA_OWNED);
381 maxirqs = nitems(irq_sources);
382 if (irq_next_free >= maxirqs)
385 for (irq = irq_next_free; irq < maxirqs; irq++) {
386 if (irq_sources[irq] == NULL)
389 for (irq = 0; irq < irq_next_free; irq++) {
390 if (irq_sources[irq] == NULL)
394 irq_next_free = maxirqs;
398 isrc->isrc_irq = irq;
399 irq_sources[irq] = isrc;
401 irq_next_free = irq + 1;
402 if (irq_next_free >= maxirqs)
408 * Free unique interrupt number (resource handle) from interrupt source.
411 isrc_free_irq(struct intr_irqsrc *isrc)
414 mtx_assert(&isrc_table_lock, MA_OWNED);
416 if (isrc->isrc_irq >= nitems(irq_sources))
418 if (irq_sources[isrc->isrc_irq] != isrc)
421 irq_sources[isrc->isrc_irq] = NULL;
422 isrc->isrc_irq = INTR_IRQ_INVALID; /* just to be safe */
427 * Initialize interrupt source and register it into global interrupt table.
430 intr_isrc_register(struct intr_irqsrc *isrc, device_t dev, u_int flags,
431 const char *fmt, ...)
436 bzero(isrc, sizeof(struct intr_irqsrc));
437 isrc->isrc_dev = dev;
438 isrc->isrc_irq = INTR_IRQ_INVALID; /* just to be safe */
439 isrc->isrc_flags = flags;
442 vsnprintf(isrc->isrc_name, INTR_ISRC_NAMELEN, fmt, ap);
445 mtx_lock(&isrc_table_lock);
446 error = isrc_alloc_irq(isrc);
448 mtx_unlock(&isrc_table_lock);
452 * Setup interrupt counters, but not for IPI sources. Those are setup
453 * later and only for used ones (up to INTR_IPI_COUNT) to not exhaust
456 if ((isrc->isrc_flags & INTR_ISRCF_IPI) == 0)
457 isrc_setup_counters(isrc);
458 mtx_unlock(&isrc_table_lock);
463 * Deregister interrupt source from global interrupt table.
466 intr_isrc_deregister(struct intr_irqsrc *isrc)
470 mtx_lock(&isrc_table_lock);
471 if ((isrc->isrc_flags & INTR_ISRCF_IPI) == 0)
472 isrc_release_counters(isrc);
473 error = isrc_free_irq(isrc);
474 mtx_unlock(&isrc_table_lock);
480 * A support function for a PIC to decide if provided ISRC should be inited
481 * on given cpu. The logic of INTR_ISRCF_BOUND flag and isrc_cpu member of
482 * struct intr_irqsrc is the following:
484 * If INTR_ISRCF_BOUND is set, the ISRC should be inited only on cpus
485 * set in isrc_cpu. If not, the ISRC should be inited on every cpu and
486 * isrc_cpu is kept consistent with it. Thus isrc_cpu is always correct.
489 intr_isrc_init_on_cpu(struct intr_irqsrc *isrc, u_int cpu)
492 if (isrc->isrc_handlers == 0)
494 if ((isrc->isrc_flags & (INTR_ISRCF_PPI | INTR_ISRCF_IPI)) == 0)
496 if (isrc->isrc_flags & INTR_ISRCF_BOUND)
497 return (CPU_ISSET(cpu, &isrc->isrc_cpu));
499 CPU_SET(cpu, &isrc->isrc_cpu);
506 * Setup filter into interrupt source.
509 iscr_setup_filter(struct intr_irqsrc *isrc, const char *name,
510 intr_irq_filter_t *filter, void *arg, void **cookiep)
516 mtx_lock(&isrc_table_lock);
518 * Make sure that we do not mix the two ways
519 * how we handle interrupt sources.
521 if (isrc->isrc_filter != NULL || isrc->isrc_event != NULL) {
522 mtx_unlock(&isrc_table_lock);
525 isrc->isrc_filter = filter;
526 isrc->isrc_arg = arg;
527 isrc_update_name(isrc, name);
528 mtx_unlock(&isrc_table_lock);
536 * Interrupt source pre_ithread method for MI interrupt framework.
539 intr_isrc_pre_ithread(void *arg)
541 struct intr_irqsrc *isrc = arg;
543 PIC_PRE_ITHREAD(isrc->isrc_dev, isrc);
547 * Interrupt source post_ithread method for MI interrupt framework.
550 intr_isrc_post_ithread(void *arg)
552 struct intr_irqsrc *isrc = arg;
554 PIC_POST_ITHREAD(isrc->isrc_dev, isrc);
558 * Interrupt source post_filter method for MI interrupt framework.
561 intr_isrc_post_filter(void *arg)
563 struct intr_irqsrc *isrc = arg;
565 PIC_POST_FILTER(isrc->isrc_dev, isrc);
569 * Interrupt source assign_cpu method for MI interrupt framework.
572 intr_isrc_assign_cpu(void *arg, int cpu)
575 struct intr_irqsrc *isrc = arg;
578 if (isrc->isrc_dev != intr_irq_root_dev)
581 mtx_lock(&isrc_table_lock);
583 CPU_ZERO(&isrc->isrc_cpu);
584 isrc->isrc_flags &= ~INTR_ISRCF_BOUND;
586 CPU_SETOF(cpu, &isrc->isrc_cpu);
587 isrc->isrc_flags |= INTR_ISRCF_BOUND;
591 * In NOCPU case, it's up to PIC to either leave ISRC on same CPU or
592 * re-balance it to another CPU or enable it on more CPUs. However,
593 * PIC is expected to change isrc_cpu appropriately to keep us well
594 * informed if the call is successful.
596 if (irq_assign_cpu) {
597 error = PIC_BIND_INTR(isrc->isrc_dev, isrc);
599 CPU_ZERO(&isrc->isrc_cpu);
600 mtx_unlock(&isrc_table_lock);
604 mtx_unlock(&isrc_table_lock);
612 * Create interrupt event for interrupt source.
615 isrc_event_create(struct intr_irqsrc *isrc)
617 struct intr_event *ie;
620 error = intr_event_create(&ie, isrc, 0, isrc->isrc_irq,
621 intr_isrc_pre_ithread, intr_isrc_post_ithread, intr_isrc_post_filter,
622 intr_isrc_assign_cpu, "%s:", isrc->isrc_name);
626 mtx_lock(&isrc_table_lock);
628 * Make sure that we do not mix the two ways
629 * how we handle interrupt sources. Let contested event wins.
632 if (isrc->isrc_filter != NULL || isrc->isrc_event != NULL) {
634 if (isrc->isrc_event != NULL) {
636 mtx_unlock(&isrc_table_lock);
637 intr_event_destroy(ie);
638 return (isrc->isrc_event != NULL ? EBUSY : 0);
640 isrc->isrc_event = ie;
641 mtx_unlock(&isrc_table_lock);
647 * Destroy interrupt event for interrupt source.
650 isrc_event_destroy(struct intr_irqsrc *isrc)
652 struct intr_event *ie;
654 mtx_lock(&isrc_table_lock);
655 ie = isrc->isrc_event;
656 isrc->isrc_event = NULL;
657 mtx_unlock(&isrc_table_lock);
660 intr_event_destroy(ie);
664 * Add handler to interrupt source.
667 isrc_add_handler(struct intr_irqsrc *isrc, const char *name,
668 driver_filter_t filter, driver_intr_t handler, void *arg,
669 enum intr_type flags, void **cookiep)
673 if (isrc->isrc_event == NULL) {
674 error = isrc_event_create(isrc);
679 error = intr_event_add_handler(isrc->isrc_event, name, filter, handler,
680 arg, intr_priority(flags), flags, cookiep);
682 mtx_lock(&isrc_table_lock);
683 intrcnt_updatename(isrc);
684 mtx_unlock(&isrc_table_lock);
691 * Lookup interrupt controller locked.
693 static inline struct intr_pic *
694 pic_lookup_locked(device_t dev, intptr_t xref, int flags)
696 struct intr_pic *pic;
698 mtx_assert(&pic_list_lock, MA_OWNED);
700 if (dev == NULL && xref == 0)
703 /* Note that pic->pic_dev is never NULL on registered PIC. */
704 SLIST_FOREACH(pic, &pic_list, pic_next) {
705 if ((pic->pic_flags & FLAG_TYPE_MASK) !=
706 (flags & FLAG_TYPE_MASK))
710 if (xref == pic->pic_xref)
712 } else if (xref == 0 || pic->pic_xref == 0) {
713 if (dev == pic->pic_dev)
715 } else if (xref == pic->pic_xref && dev == pic->pic_dev)
722 * Lookup interrupt controller.
724 static struct intr_pic *
725 pic_lookup(device_t dev, intptr_t xref, int flags)
727 struct intr_pic *pic;
729 mtx_lock(&pic_list_lock);
730 pic = pic_lookup_locked(dev, xref, flags);
731 mtx_unlock(&pic_list_lock);
736 * Create interrupt controller.
738 static struct intr_pic *
739 pic_create(device_t dev, intptr_t xref, int flags)
741 struct intr_pic *pic;
743 mtx_lock(&pic_list_lock);
744 pic = pic_lookup_locked(dev, xref, flags);
746 mtx_unlock(&pic_list_lock);
749 pic = malloc(sizeof(*pic), M_INTRNG, M_NOWAIT | M_ZERO);
751 mtx_unlock(&pic_list_lock);
754 pic->pic_xref = xref;
756 pic->pic_flags = flags;
757 mtx_init(&pic->pic_child_lock, "pic child lock", NULL, MTX_SPIN);
758 SLIST_INSERT_HEAD(&pic_list, pic, pic_next);
759 mtx_unlock(&pic_list_lock);
765 * Destroy interrupt controller.
768 pic_destroy(device_t dev, intptr_t xref, int flags)
770 struct intr_pic *pic;
772 mtx_lock(&pic_list_lock);
773 pic = pic_lookup_locked(dev, xref, flags);
775 mtx_unlock(&pic_list_lock);
778 SLIST_REMOVE(&pic_list, pic, intr_pic, pic_next);
779 mtx_unlock(&pic_list_lock);
785 * Register interrupt controller.
788 intr_pic_register(device_t dev, intptr_t xref)
790 struct intr_pic *pic;
794 pic = pic_create(dev, xref, FLAG_PIC);
798 debugf("PIC %p registered for %s <dev %p, xref %x>\n", pic,
799 device_get_nameunit(dev), dev, xref);
804 * Unregister interrupt controller.
807 intr_pic_deregister(device_t dev, intptr_t xref)
810 panic("%s: not implemented", __func__);
814 * Mark interrupt controller (itself) as a root one.
816 * Note that only an interrupt controller can really know its position
817 * in interrupt controller's tree. So root PIC must claim itself as a root.
819 * In FDT case, according to ePAPR approved version 1.1 from 08 April 2011,
821 * "The root of the interrupt tree is determined when traversal
822 * of the interrupt tree reaches an interrupt controller node without
823 * an interrupts property and thus no explicit interrupt parent."
826 intr_pic_claim_root(device_t dev, intptr_t xref, intr_irq_filter_t *filter,
827 void *arg, u_int ipicount)
829 struct intr_pic *pic;
831 pic = pic_lookup(dev, xref, FLAG_PIC);
833 device_printf(dev, "not registered\n");
837 KASSERT((pic->pic_flags & FLAG_TYPE_MASK) == FLAG_PIC,
838 ("%s: Found a non-PIC controller: %s", __func__,
839 device_get_name(pic->pic_dev)));
841 if (filter == NULL) {
842 device_printf(dev, "filter missing\n");
847 * Only one interrupt controllers could be on the root for now.
848 * Note that we further suppose that there is not threaded interrupt
849 * routine (handler) on the root. See intr_irq_handler().
851 if (intr_irq_root_dev != NULL) {
852 device_printf(dev, "another root already set\n");
856 intr_irq_root_dev = dev;
857 irq_root_filter = filter;
859 irq_root_ipicount = ipicount;
861 debugf("irq root set to %s\n", device_get_nameunit(dev));
866 * Add a handler to manage a sub range of a parents interrupts.
869 intr_pic_add_handler(device_t parent, struct intr_pic *pic,
870 intr_child_irq_filter_t *filter, void *arg, uintptr_t start,
873 struct intr_pic *parent_pic;
874 struct intr_pic_child *newchild;
876 struct intr_pic_child *child;
879 /* Find the parent PIC */
880 parent_pic = pic_lookup(parent, 0, FLAG_PIC);
881 if (parent_pic == NULL)
884 newchild = malloc(sizeof(*newchild), M_INTRNG, M_WAITOK | M_ZERO);
885 newchild->pc_pic = pic;
886 newchild->pc_filter = filter;
887 newchild->pc_filter_arg = arg;
888 newchild->pc_start = start;
889 newchild->pc_length = length;
891 mtx_lock_spin(&parent_pic->pic_child_lock);
893 SLIST_FOREACH(child, &parent_pic->pic_children, pc_next) {
894 KASSERT(child->pc_pic != pic, ("%s: Adding a child PIC twice",
898 SLIST_INSERT_HEAD(&parent_pic->pic_children, newchild, pc_next);
899 mtx_unlock_spin(&parent_pic->pic_child_lock);
905 intr_resolve_irq(device_t dev, intptr_t xref, struct intr_map_data *data,
906 struct intr_irqsrc **isrc)
908 struct intr_pic *pic;
909 struct intr_map_data_msi *msi;
914 pic = pic_lookup(dev, xref,
915 (data->type == INTR_MAP_DATA_MSI) ? FLAG_MSI : FLAG_PIC);
919 switch (data->type) {
920 case INTR_MAP_DATA_MSI:
921 KASSERT((pic->pic_flags & FLAG_TYPE_MASK) == FLAG_MSI,
922 ("%s: Found a non-MSI controller: %s", __func__,
923 device_get_name(pic->pic_dev)));
924 msi = (struct intr_map_data_msi *)data;
929 KASSERT((pic->pic_flags & FLAG_TYPE_MASK) == FLAG_PIC,
930 ("%s: Found a non-PIC controller: %s", __func__,
931 device_get_name(pic->pic_dev)));
932 return (PIC_MAP_INTR(pic->pic_dev, data, isrc));
938 intr_activate_irq(device_t dev, struct resource *res)
942 struct intr_map_data *data;
943 struct intr_irqsrc *isrc;
947 KASSERT(rman_get_start(res) == rman_get_end(res),
948 ("%s: more interrupts in resource", __func__));
950 res_id = (u_int)rman_get_start(res);
951 if (intr_map_get_isrc(res_id) != NULL)
952 panic("Attempt to double activation of resource id: %u\n",
954 intr_map_copy_map_data(res_id, &map_dev, &map_xref, &data);
955 error = intr_resolve_irq(map_dev, map_xref, data, &isrc);
957 free(data, M_INTRNG);
958 /* XXX TODO DISCONECTED PICs */
959 /* if (error == EINVAL) return(0); */
962 intr_map_set_isrc(res_id, isrc);
963 rman_set_virtual(res, data);
964 return (PIC_ACTIVATE_INTR(isrc->isrc_dev, isrc, res, data));
968 intr_deactivate_irq(device_t dev, struct resource *res)
970 struct intr_map_data *data;
971 struct intr_irqsrc *isrc;
975 KASSERT(rman_get_start(res) == rman_get_end(res),
976 ("%s: more interrupts in resource", __func__));
978 res_id = (u_int)rman_get_start(res);
979 isrc = intr_map_get_isrc(res_id);
981 panic("Attempt to deactivate non-active resource id: %u\n",
984 data = rman_get_virtual(res);
985 error = PIC_DEACTIVATE_INTR(isrc->isrc_dev, isrc, res, data);
986 intr_map_set_isrc(res_id, NULL);
987 rman_set_virtual(res, NULL);
988 free(data, M_INTRNG);
993 intr_setup_irq(device_t dev, struct resource *res, driver_filter_t filt,
994 driver_intr_t hand, void *arg, int flags, void **cookiep)
997 struct intr_map_data *data;
998 struct intr_irqsrc *isrc;
1002 KASSERT(rman_get_start(res) == rman_get_end(res),
1003 ("%s: more interrupts in resource", __func__));
1005 res_id = (u_int)rman_get_start(res);
1006 isrc = intr_map_get_isrc(res_id);
1008 /* XXX TODO DISCONECTED PICs */
1012 data = rman_get_virtual(res);
1013 name = device_get_nameunit(dev);
1017 * Standard handling is done through MI interrupt framework. However,
1018 * some interrupts could request solely own special handling. This
1019 * non standard handling can be used for interrupt controllers without
1020 * handler (filter only), so in case that interrupt controllers are
1021 * chained, MI interrupt framework is called only in leaf controller.
1023 * Note that root interrupt controller routine is served as well,
1024 * however in intr_irq_handler(), i.e. main system dispatch routine.
1026 if (flags & INTR_SOLO && hand != NULL) {
1027 debugf("irq %u cannot solo on %s\n", irq, name);
1031 if (flags & INTR_SOLO) {
1032 error = iscr_setup_filter(isrc, name, (intr_irq_filter_t *)filt,
1034 debugf("irq %u setup filter error %d on %s\n", irq, error,
1039 error = isrc_add_handler(isrc, name, filt, hand, arg, flags,
1041 debugf("irq %u add handler error %d on %s\n", irq, error, name);
1046 mtx_lock(&isrc_table_lock);
1047 error = PIC_SETUP_INTR(isrc->isrc_dev, isrc, res, data);
1049 isrc->isrc_handlers++;
1050 if (isrc->isrc_handlers == 1)
1051 PIC_ENABLE_INTR(isrc->isrc_dev, isrc);
1053 mtx_unlock(&isrc_table_lock);
1055 intr_event_remove_handler(*cookiep);
1060 intr_teardown_irq(device_t dev, struct resource *res, void *cookie)
1063 struct intr_map_data *data;
1064 struct intr_irqsrc *isrc;
1067 KASSERT(rman_get_start(res) == rman_get_end(res),
1068 ("%s: more interrupts in resource", __func__));
1070 res_id = (u_int)rman_get_start(res);
1071 isrc = intr_map_get_isrc(res_id);
1072 if (isrc == NULL || isrc->isrc_handlers == 0)
1075 data = rman_get_virtual(res);
1078 if (isrc->isrc_filter != NULL) {
1082 mtx_lock(&isrc_table_lock);
1083 isrc->isrc_filter = NULL;
1084 isrc->isrc_arg = NULL;
1085 isrc->isrc_handlers = 0;
1086 PIC_DISABLE_INTR(isrc->isrc_dev, isrc);
1087 PIC_TEARDOWN_INTR(isrc->isrc_dev, isrc, res, data);
1088 isrc_update_name(isrc, NULL);
1089 mtx_unlock(&isrc_table_lock);
1093 if (isrc != intr_handler_source(cookie))
1096 error = intr_event_remove_handler(cookie);
1098 mtx_lock(&isrc_table_lock);
1099 isrc->isrc_handlers--;
1100 if (isrc->isrc_handlers == 0)
1101 PIC_DISABLE_INTR(isrc->isrc_dev, isrc);
1102 PIC_TEARDOWN_INTR(isrc->isrc_dev, isrc, res, data);
1103 intrcnt_updatename(isrc);
1104 mtx_unlock(&isrc_table_lock);
1110 intr_describe_irq(device_t dev, struct resource *res, void *cookie,
1114 struct intr_irqsrc *isrc;
1117 KASSERT(rman_get_start(res) == rman_get_end(res),
1118 ("%s: more interrupts in resource", __func__));
1120 res_id = (u_int)rman_get_start(res);
1121 isrc = intr_map_get_isrc(res_id);
1122 if (isrc == NULL || isrc->isrc_handlers == 0)
1125 if (isrc->isrc_filter != NULL) {
1129 mtx_lock(&isrc_table_lock);
1130 isrc_update_name(isrc, descr);
1131 mtx_unlock(&isrc_table_lock);
1135 error = intr_event_describe_handler(isrc->isrc_event, cookie, descr);
1137 mtx_lock(&isrc_table_lock);
1138 intrcnt_updatename(isrc);
1139 mtx_unlock(&isrc_table_lock);
1146 intr_bind_irq(device_t dev, struct resource *res, int cpu)
1148 struct intr_irqsrc *isrc;
1151 KASSERT(rman_get_start(res) == rman_get_end(res),
1152 ("%s: more interrupts in resource", __func__));
1154 res_id = (u_int)rman_get_start(res);
1155 isrc = intr_map_get_isrc(res_id);
1156 if (isrc == NULL || isrc->isrc_handlers == 0)
1159 if (isrc->isrc_filter != NULL)
1160 return (intr_isrc_assign_cpu(isrc, cpu));
1162 return (intr_event_bind(isrc->isrc_event, cpu));
1166 * Return the CPU that the next interrupt source should use.
1167 * For now just returns the next CPU according to round-robin.
1170 intr_irq_next_cpu(u_int last_cpu, cpuset_t *cpumask)
1173 if (!irq_assign_cpu || mp_ncpus == 1)
1174 return (PCPU_GET(cpuid));
1178 if (last_cpu > mp_maxid)
1180 } while (!CPU_ISSET(last_cpu, cpumask));
1185 * Distribute all the interrupt sources among the available
1186 * CPUs once the AP's have been launched.
1189 intr_irq_shuffle(void *arg __unused)
1191 struct intr_irqsrc *isrc;
1197 mtx_lock(&isrc_table_lock);
1198 irq_assign_cpu = TRUE;
1199 for (i = 0; i < NIRQ; i++) {
1200 isrc = irq_sources[i];
1201 if (isrc == NULL || isrc->isrc_handlers == 0 ||
1202 isrc->isrc_flags & (INTR_ISRCF_PPI | INTR_ISRCF_IPI))
1205 if (isrc->isrc_event != NULL &&
1206 isrc->isrc_flags & INTR_ISRCF_BOUND &&
1207 isrc->isrc_event->ie_cpu != CPU_FFS(&isrc->isrc_cpu) - 1)
1208 panic("%s: CPU inconsistency", __func__);
1210 if ((isrc->isrc_flags & INTR_ISRCF_BOUND) == 0)
1211 CPU_ZERO(&isrc->isrc_cpu); /* start again */
1214 * We are in wicked position here if the following call fails
1215 * for bound ISRC. The best thing we can do is to clear
1216 * isrc_cpu so inconsistency with ie_cpu will be detectable.
1218 if (PIC_BIND_INTR(isrc->isrc_dev, isrc) != 0)
1219 CPU_ZERO(&isrc->isrc_cpu);
1221 mtx_unlock(&isrc_table_lock);
1223 SYSINIT(intr_irq_shuffle, SI_SUB_SMP, SI_ORDER_SECOND, intr_irq_shuffle, NULL);
1227 intr_irq_next_cpu(u_int current_cpu, cpuset_t *cpumask)
1230 return (PCPU_GET(cpuid));
1235 * Allocate memory for new intr_map_data structure.
1236 * Initialize common fields.
1238 struct intr_map_data *
1239 intr_alloc_map_data(enum intr_map_data_type type, size_t len, int flags)
1241 struct intr_map_data *data;
1243 data = malloc(len, M_INTRNG, flags);
1249 void intr_free_intr_map_data(struct intr_map_data *data)
1252 free(data, M_INTRNG);
1257 * Register a MSI/MSI-X interrupt controller
1260 intr_msi_register(device_t dev, intptr_t xref)
1262 struct intr_pic *pic;
1266 pic = pic_create(dev, xref, FLAG_MSI);
1270 debugf("PIC %p registered for %s <dev %p, xref %jx>\n", pic,
1271 device_get_nameunit(dev), dev, (uintmax_t)xref);
1276 intr_alloc_msi(device_t pci, device_t child, intptr_t xref, int count,
1277 int maxcount, int *irqs)
1279 struct intr_irqsrc **isrc;
1280 struct intr_pic *pic;
1282 struct intr_map_data_msi *msi;
1285 pic = pic_lookup(NULL, xref, FLAG_MSI);
1289 KASSERT((pic->pic_flags & FLAG_TYPE_MASK) == FLAG_MSI,
1290 ("%s: Found a non-MSI controller: %s", __func__,
1291 device_get_name(pic->pic_dev)));
1293 isrc = malloc(sizeof(*isrc) * count, M_INTRNG, M_WAITOK);
1294 err = MSI_ALLOC_MSI(pic->pic_dev, child, count, maxcount, &pdev, isrc);
1296 free(isrc, M_INTRNG);
1300 for (i = 0; i < count; i++) {
1301 msi = (struct intr_map_data_msi *)intr_alloc_map_data(
1302 INTR_MAP_DATA_MSI, sizeof(*msi), M_WAITOK | M_ZERO);
1303 msi-> isrc = isrc[i];
1304 irqs[i] = intr_map_irq(pic->pic_dev, xref,
1305 (struct intr_map_data *)msi);
1308 free(isrc, M_INTRNG);
1314 intr_release_msi(device_t pci, device_t child, intptr_t xref, int count,
1317 struct intr_irqsrc **isrc;
1318 struct intr_pic *pic;
1319 struct intr_map_data_msi *msi;
1322 pic = pic_lookup(NULL, xref, FLAG_MSI);
1326 KASSERT((pic->pic_flags & FLAG_TYPE_MASK) == FLAG_MSI,
1327 ("%s: Found a non-MSI controller: %s", __func__,
1328 device_get_name(pic->pic_dev)));
1330 isrc = malloc(sizeof(*isrc) * count, M_INTRNG, M_WAITOK);
1332 for (i = 0; i < count; i++) {
1333 msi = (struct intr_map_data_msi *)
1334 intr_map_get_map_data(irqs[i]);
1335 KASSERT(msi->hdr.type == INTR_MAP_DATA_MSI,
1336 ("%s: irq %d map data is not MSI", __func__,
1338 isrc[i] = msi->isrc;
1341 err = MSI_RELEASE_MSI(pic->pic_dev, child, count, isrc);
1343 for (i = 0; i < count; i++) {
1344 if (isrc[i] != NULL)
1345 intr_unmap_irq(irqs[i]);
1348 free(isrc, M_INTRNG);
1353 intr_alloc_msix(device_t pci, device_t child, intptr_t xref, int *irq)
1355 struct intr_irqsrc *isrc;
1356 struct intr_pic *pic;
1358 struct intr_map_data_msi *msi;
1361 pic = pic_lookup(NULL, xref, FLAG_MSI);
1365 KASSERT((pic->pic_flags & FLAG_TYPE_MASK) == FLAG_MSI,
1366 ("%s: Found a non-MSI controller: %s", __func__,
1367 device_get_name(pic->pic_dev)));
1370 err = MSI_ALLOC_MSIX(pic->pic_dev, child, &pdev, &isrc);
1374 msi = (struct intr_map_data_msi *)intr_alloc_map_data(
1375 INTR_MAP_DATA_MSI, sizeof(*msi), M_WAITOK | M_ZERO);
1377 *irq = intr_map_irq(pic->pic_dev, xref, (struct intr_map_data *)msi);
1382 intr_release_msix(device_t pci, device_t child, intptr_t xref, int irq)
1384 struct intr_irqsrc *isrc;
1385 struct intr_pic *pic;
1386 struct intr_map_data_msi *msi;
1389 pic = pic_lookup(NULL, xref, FLAG_MSI);
1393 KASSERT((pic->pic_flags & FLAG_TYPE_MASK) == FLAG_MSI,
1394 ("%s: Found a non-MSI controller: %s", __func__,
1395 device_get_name(pic->pic_dev)));
1397 msi = (struct intr_map_data_msi *)
1398 intr_map_get_map_data(irq);
1399 KASSERT(msi->hdr.type == INTR_MAP_DATA_MSI,
1400 ("%s: irq %d map data is not MSI", __func__,
1404 intr_unmap_irq(irq);
1408 err = MSI_RELEASE_MSIX(pic->pic_dev, child, isrc);
1409 intr_unmap_irq(irq);
1415 intr_map_msi(device_t pci, device_t child, intptr_t xref, int irq,
1416 uint64_t *addr, uint32_t *data)
1418 struct intr_irqsrc *isrc;
1419 struct intr_pic *pic;
1422 pic = pic_lookup(NULL, xref, FLAG_MSI);
1426 KASSERT((pic->pic_flags & FLAG_TYPE_MASK) == FLAG_MSI,
1427 ("%s: Found a non-MSI controller: %s", __func__,
1428 device_get_name(pic->pic_dev)));
1430 isrc = intr_map_get_isrc(irq);
1434 err = MSI_MAP_MSI(pic->pic_dev, child, isrc, addr, data);
1439 void dosoftints(void);
1447 * Init interrupt controller on another CPU.
1450 intr_pic_init_secondary(void)
1454 * QQQ: Only root PIC is aware of other CPUs ???
1456 KASSERT(intr_irq_root_dev != NULL, ("%s: no root attached", __func__));
1458 //mtx_lock(&isrc_table_lock);
1459 PIC_INIT_SECONDARY(intr_irq_root_dev);
1460 //mtx_unlock(&isrc_table_lock);
1465 DB_SHOW_COMMAND(irqs, db_show_irqs)
1469 struct intr_irqsrc *isrc;
1471 for (irqsum = 0, i = 0; i < NIRQ; i++) {
1472 isrc = irq_sources[i];
1476 num = isrc->isrc_count != NULL ? isrc->isrc_count[0] : 0;
1477 db_printf("irq%-3u <%s>: cpu %02lx%s cnt %lu\n", i,
1478 isrc->isrc_name, isrc->isrc_cpu.__bits[0],
1479 isrc->isrc_flags & INTR_ISRCF_BOUND ? " (bound)" : "", num);
1482 db_printf("irq total %u\n", irqsum);
1487 * Interrupt mapping table functions.
1489 * Please, keep this part separately, it can be transformed to
1490 * extension of standard resources.
1492 struct intr_map_entry
1496 struct intr_map_data *map_data;
1497 struct intr_irqsrc *isrc;
1498 /* XXX TODO DISCONECTED PICs */
1502 /* XXX Convert irq_map[] to dynamicaly expandable one. */
1503 static struct intr_map_entry *irq_map[2 * NIRQ];
1504 static int irq_map_count = nitems(irq_map);
1505 static int irq_map_first_free_idx;
1506 static struct mtx irq_map_lock;
1508 static struct intr_irqsrc *
1509 intr_map_get_isrc(u_int res_id)
1511 struct intr_irqsrc *isrc;
1513 mtx_lock(&irq_map_lock);
1514 if ((res_id >= irq_map_count) || (irq_map[res_id] == NULL)) {
1515 mtx_unlock(&irq_map_lock);
1518 isrc = irq_map[res_id]->isrc;
1519 mtx_unlock(&irq_map_lock);
1524 intr_map_set_isrc(u_int res_id, struct intr_irqsrc *isrc)
1527 mtx_lock(&irq_map_lock);
1528 if ((res_id >= irq_map_count) || (irq_map[res_id] == NULL)) {
1529 mtx_unlock(&irq_map_lock);
1532 irq_map[res_id]->isrc = isrc;
1533 mtx_unlock(&irq_map_lock);
1537 * Get a copy of intr_map_entry data
1539 static struct intr_map_data *
1540 intr_map_get_map_data(u_int res_id)
1542 struct intr_map_data *data;
1545 mtx_lock(&irq_map_lock);
1546 if (res_id >= irq_map_count || irq_map[res_id] == NULL)
1547 panic("Attempt to copy invalid resource id: %u\n", res_id);
1548 data = irq_map[res_id]->map_data;
1549 mtx_unlock(&irq_map_lock);
1555 * Get a copy of intr_map_entry data
1558 intr_map_copy_map_data(u_int res_id, device_t *map_dev, intptr_t *map_xref,
1559 struct intr_map_data **data)
1564 mtx_lock(&irq_map_lock);
1565 if (res_id >= irq_map_count || irq_map[res_id] == NULL)
1566 panic("Attempt to copy invalid resource id: %u\n", res_id);
1567 if (irq_map[res_id]->map_data != NULL)
1568 len = irq_map[res_id]->map_data->len;
1569 mtx_unlock(&irq_map_lock);
1574 *data = malloc(len, M_INTRNG, M_WAITOK | M_ZERO);
1575 mtx_lock(&irq_map_lock);
1576 if (irq_map[res_id] == NULL)
1577 panic("Attempt to copy invalid resource id: %u\n", res_id);
1579 if (len != irq_map[res_id]->map_data->len)
1580 panic("Resource id: %u has changed.\n", res_id);
1581 memcpy(*data, irq_map[res_id]->map_data, len);
1583 *map_dev = irq_map[res_id]->dev;
1584 *map_xref = irq_map[res_id]->xref;
1585 mtx_unlock(&irq_map_lock);
1590 * Allocate and fill new entry in irq_map table.
1593 intr_map_irq(device_t dev, intptr_t xref, struct intr_map_data *data)
1596 struct intr_map_entry *entry;
1598 /* Prepare new entry first. */
1599 entry = malloc(sizeof(*entry), M_INTRNG, M_WAITOK | M_ZERO);
1603 entry->map_data = data;
1606 mtx_lock(&irq_map_lock);
1607 for (i = irq_map_first_free_idx; i < irq_map_count; i++) {
1608 if (irq_map[i] == NULL) {
1610 irq_map_first_free_idx = i + 1;
1611 mtx_unlock(&irq_map_lock);
1615 mtx_unlock(&irq_map_lock);
1617 /* XXX Expand irq_map table */
1618 panic("IRQ mapping table is full.");
1622 * Remove and free mapping entry.
1625 intr_unmap_irq(u_int res_id)
1627 struct intr_map_entry *entry;
1629 mtx_lock(&irq_map_lock);
1630 if ((res_id >= irq_map_count) || (irq_map[res_id] == NULL))
1631 panic("Attempt to unmap invalid resource id: %u\n", res_id);
1632 entry = irq_map[res_id];
1633 irq_map[res_id] = NULL;
1634 irq_map_first_free_idx = res_id;
1635 mtx_unlock(&irq_map_lock);
1636 intr_free_intr_map_data(entry->map_data);
1637 free(entry, M_INTRNG);
1641 * Clone mapping entry.
1644 intr_map_clone_irq(u_int old_res_id)
1648 struct intr_map_data *data;
1650 intr_map_copy_map_data(old_res_id, &map_dev, &map_xref, &data);
1651 return (intr_map_irq(map_dev, map_xref, data));
1655 intr_map_init(void *dummy __unused)
1658 mtx_init(&irq_map_lock, "intr map table", NULL, MTX_DEF);
1660 SYSINIT(intr_map_init, SI_SUB_INTR, SI_ORDER_FIRST, intr_map_init, NULL);