2 * Copyright (c) 2015-2016 Svatopluk Kraus
3 * Copyright (c) 2015-2016 Michal Meloun
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
32 * New-style Interrupt Framework
34 * TODO: - add support for disconnected PICs.
35 * - to support IPI (PPI) enabling on other CPUs if already started.
36 * - to complete things for removable PICs.
40 #include "opt_hwpmc_hooks.h"
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
46 #include <sys/mutex.h>
47 #include <sys/syslog.h>
48 #include <sys/malloc.h>
50 #include <sys/queue.h>
52 #include <sys/interrupt.h>
54 #include <sys/cpuset.h>
56 #include <sys/sched.h>
58 #include <sys/vmmeter.h>
60 #include <sys/pmckern.h>
63 #include <machine/atomic.h>
64 #include <machine/intr.h>
65 #include <machine/cpu.h>
66 #include <machine/smp.h>
67 #include <machine/stdarg.h>
76 #define INTRNAME_LEN (2*MAXCOMLEN + 1)
79 #define debugf(fmt, args...) do { printf("%s(): ", __func__); \
80 printf(fmt,##args); } while (0)
82 #define debugf(fmt, args...)
85 MALLOC_DECLARE(M_INTRNG);
86 MALLOC_DEFINE(M_INTRNG, "intr", "intr interrupt handling");
88 /* Main interrupt handler called from assembler -> 'hidden' for C code. */
89 void intr_irq_handler(struct trapframe *tf);
91 /* Root interrupt controller stuff. */
92 device_t intr_irq_root_dev;
93 static intr_irq_filter_t *irq_root_filter;
94 static void *irq_root_arg;
95 static u_int irq_root_ipicount;
97 struct intr_pic_child {
98 SLIST_ENTRY(intr_pic_child) pc_next;
99 struct intr_pic *pc_pic;
100 intr_child_irq_filter_t *pc_filter;
106 /* Interrupt controller definition. */
108 SLIST_ENTRY(intr_pic) pic_next;
109 intptr_t pic_xref; /* hardware identification */
111 /* Only one of FLAG_PIC or FLAG_MSI may be set */
112 #define FLAG_PIC (1 << 0)
113 #define FLAG_MSI (1 << 1)
114 #define FLAG_TYPE_MASK (FLAG_PIC | FLAG_MSI)
116 struct mtx pic_child_lock;
117 SLIST_HEAD(, intr_pic_child) pic_children;
120 static struct mtx pic_list_lock;
121 static SLIST_HEAD(, intr_pic) pic_list;
123 static struct intr_pic *pic_lookup(device_t dev, intptr_t xref, int flags);
125 /* Interrupt source definition. */
126 static struct mtx isrc_table_lock;
127 static struct intr_irqsrc *irq_sources[NIRQ];
131 #ifdef EARLY_AP_STARTUP
132 static bool irq_assign_cpu = true;
134 static bool irq_assign_cpu = false;
139 * - 2 counters for each I/O interrupt.
140 * - MAXCPU counters for each IPI counters for SMP.
143 #define INTRCNT_COUNT (NIRQ * 2 + INTR_IPI_COUNT * MAXCPU)
145 #define INTRCNT_COUNT (NIRQ * 2)
148 /* Data for MI statistics reporting. */
149 u_long intrcnt[INTRCNT_COUNT];
150 char intrnames[INTRCNT_COUNT * INTRNAME_LEN];
151 size_t sintrcnt = sizeof(intrcnt);
152 size_t sintrnames = sizeof(intrnames);
153 static u_int intrcnt_index;
155 static struct intr_irqsrc *intr_map_get_isrc(u_int res_id);
156 static void intr_map_set_isrc(u_int res_id, struct intr_irqsrc *isrc);
157 static struct intr_map_data * intr_map_get_map_data(u_int res_id);
158 static void intr_map_copy_map_data(u_int res_id, device_t *dev, intptr_t *xref,
159 struct intr_map_data **data);
162 * Interrupt framework initialization routine.
165 intr_irq_init(void *dummy __unused)
168 SLIST_INIT(&pic_list);
169 mtx_init(&pic_list_lock, "intr pic list", NULL, MTX_DEF);
171 mtx_init(&isrc_table_lock, "intr isrc table", NULL, MTX_DEF);
173 SYSINIT(intr_irq_init, SI_SUB_INTR, SI_ORDER_FIRST, intr_irq_init, NULL);
176 intrcnt_setname(const char *name, int index)
179 snprintf(intrnames + INTRNAME_LEN * index, INTRNAME_LEN, "%-*s",
180 INTRNAME_LEN - 1, name);
184 * Update name for interrupt source with interrupt event.
187 intrcnt_updatename(struct intr_irqsrc *isrc)
190 /* QQQ: What about stray counter name? */
191 mtx_assert(&isrc_table_lock, MA_OWNED);
192 intrcnt_setname(isrc->isrc_event->ie_fullname, isrc->isrc_index);
196 * Virtualization for interrupt source interrupt counter increment.
199 isrc_increment_count(struct intr_irqsrc *isrc)
202 if (isrc->isrc_flags & INTR_ISRCF_PPI)
203 atomic_add_long(&isrc->isrc_count[0], 1);
205 isrc->isrc_count[0]++;
209 * Virtualization for interrupt source interrupt stray counter increment.
212 isrc_increment_straycount(struct intr_irqsrc *isrc)
215 isrc->isrc_count[1]++;
219 * Virtualization for interrupt source interrupt name update.
222 isrc_update_name(struct intr_irqsrc *isrc, const char *name)
224 char str[INTRNAME_LEN];
226 mtx_assert(&isrc_table_lock, MA_OWNED);
229 snprintf(str, INTRNAME_LEN, "%s: %s", isrc->isrc_name, name);
230 intrcnt_setname(str, isrc->isrc_index);
231 snprintf(str, INTRNAME_LEN, "stray %s: %s", isrc->isrc_name,
233 intrcnt_setname(str, isrc->isrc_index + 1);
235 snprintf(str, INTRNAME_LEN, "%s:", isrc->isrc_name);
236 intrcnt_setname(str, isrc->isrc_index);
237 snprintf(str, INTRNAME_LEN, "stray %s:", isrc->isrc_name);
238 intrcnt_setname(str, isrc->isrc_index + 1);
243 * Virtualization for interrupt source interrupt counters setup.
246 isrc_setup_counters(struct intr_irqsrc *isrc)
251 * XXX - it does not work well with removable controllers and
252 * interrupt sources !!!
254 index = atomic_fetchadd_int(&intrcnt_index, 2);
255 isrc->isrc_index = index;
256 isrc->isrc_count = &intrcnt[index];
257 isrc_update_name(isrc, NULL);
261 * Virtualization for interrupt source interrupt counters release.
264 isrc_release_counters(struct intr_irqsrc *isrc)
267 panic("%s: not implemented", __func__);
272 * Virtualization for interrupt source IPI counters setup.
275 intr_ipi_setup_counters(const char *name)
278 char str[INTRNAME_LEN];
280 index = atomic_fetchadd_int(&intrcnt_index, MAXCPU);
281 for (i = 0; i < MAXCPU; i++) {
282 snprintf(str, INTRNAME_LEN, "cpu%d:%s", i, name);
283 intrcnt_setname(str, index + i);
285 return (&intrcnt[index]);
290 * Main interrupt dispatch handler. It's called straight
291 * from the assembler, where CPU interrupt is served.
294 intr_irq_handler(struct trapframe *tf)
296 struct trapframe * oldframe;
299 KASSERT(irq_root_filter != NULL, ("%s: no filter", __func__));
304 oldframe = td->td_intr_frame;
305 td->td_intr_frame = tf;
306 irq_root_filter(irq_root_arg);
307 td->td_intr_frame = oldframe;
310 if (pmc_hook && TRAPF_USERMODE(tf) &&
311 (PCPU_GET(curthread)->td_pflags & TDP_CALLCHAIN))
312 pmc_hook(PCPU_GET(curthread), PMC_FN_USER_CALLCHAIN, tf);
317 intr_child_irq_handler(struct intr_pic *parent, uintptr_t irq)
319 struct intr_pic_child *child;
323 mtx_lock_spin(&parent->pic_child_lock);
324 SLIST_FOREACH(child, &parent->pic_children, pc_next) {
325 if (child->pc_start <= irq &&
326 irq < (child->pc_start + child->pc_length)) {
331 mtx_unlock_spin(&parent->pic_child_lock);
334 return (child->pc_filter(child->pc_filter_arg, irq));
336 return (FILTER_STRAY);
340 * interrupt controller dispatch function for interrupts. It should
341 * be called straight from the interrupt controller, when associated interrupt
345 intr_isrc_dispatch(struct intr_irqsrc *isrc, struct trapframe *tf)
348 KASSERT(isrc != NULL, ("%s: no source", __func__));
350 isrc_increment_count(isrc);
353 if (isrc->isrc_filter != NULL) {
355 error = isrc->isrc_filter(isrc->isrc_arg, tf);
356 PIC_POST_FILTER(isrc->isrc_dev, isrc);
357 if (error == FILTER_HANDLED)
361 if (isrc->isrc_event != NULL) {
362 if (intr_event_handle(isrc->isrc_event, tf) == 0)
366 isrc_increment_straycount(isrc);
371 * Alloc unique interrupt number (resource handle) for interrupt source.
373 * There could be various strategies how to allocate free interrupt number
374 * (resource handle) for new interrupt source.
376 * 1. Handles are always allocated forward, so handles are not recycled
377 * immediately. However, if only one free handle left which is reused
381 isrc_alloc_irq(struct intr_irqsrc *isrc)
385 mtx_assert(&isrc_table_lock, MA_OWNED);
387 maxirqs = nitems(irq_sources);
388 if (irq_next_free >= maxirqs)
391 for (irq = irq_next_free; irq < maxirqs; irq++) {
392 if (irq_sources[irq] == NULL)
395 for (irq = 0; irq < irq_next_free; irq++) {
396 if (irq_sources[irq] == NULL)
400 irq_next_free = maxirqs;
404 isrc->isrc_irq = irq;
405 irq_sources[irq] = isrc;
407 irq_next_free = irq + 1;
408 if (irq_next_free >= maxirqs)
414 * Free unique interrupt number (resource handle) from interrupt source.
417 isrc_free_irq(struct intr_irqsrc *isrc)
420 mtx_assert(&isrc_table_lock, MA_OWNED);
422 if (isrc->isrc_irq >= nitems(irq_sources))
424 if (irq_sources[isrc->isrc_irq] != isrc)
427 irq_sources[isrc->isrc_irq] = NULL;
428 isrc->isrc_irq = INTR_IRQ_INVALID; /* just to be safe */
433 * Initialize interrupt source and register it into global interrupt table.
436 intr_isrc_register(struct intr_irqsrc *isrc, device_t dev, u_int flags,
437 const char *fmt, ...)
442 bzero(isrc, sizeof(struct intr_irqsrc));
443 isrc->isrc_dev = dev;
444 isrc->isrc_irq = INTR_IRQ_INVALID; /* just to be safe */
445 isrc->isrc_flags = flags;
448 vsnprintf(isrc->isrc_name, INTR_ISRC_NAMELEN, fmt, ap);
451 mtx_lock(&isrc_table_lock);
452 error = isrc_alloc_irq(isrc);
454 mtx_unlock(&isrc_table_lock);
458 * Setup interrupt counters, but not for IPI sources. Those are setup
459 * later and only for used ones (up to INTR_IPI_COUNT) to not exhaust
462 if ((isrc->isrc_flags & INTR_ISRCF_IPI) == 0)
463 isrc_setup_counters(isrc);
464 mtx_unlock(&isrc_table_lock);
469 * Deregister interrupt source from global interrupt table.
472 intr_isrc_deregister(struct intr_irqsrc *isrc)
476 mtx_lock(&isrc_table_lock);
477 if ((isrc->isrc_flags & INTR_ISRCF_IPI) == 0)
478 isrc_release_counters(isrc);
479 error = isrc_free_irq(isrc);
480 mtx_unlock(&isrc_table_lock);
486 * A support function for a PIC to decide if provided ISRC should be inited
487 * on given cpu. The logic of INTR_ISRCF_BOUND flag and isrc_cpu member of
488 * struct intr_irqsrc is the following:
490 * If INTR_ISRCF_BOUND is set, the ISRC should be inited only on cpus
491 * set in isrc_cpu. If not, the ISRC should be inited on every cpu and
492 * isrc_cpu is kept consistent with it. Thus isrc_cpu is always correct.
495 intr_isrc_init_on_cpu(struct intr_irqsrc *isrc, u_int cpu)
498 if (isrc->isrc_handlers == 0)
500 if ((isrc->isrc_flags & (INTR_ISRCF_PPI | INTR_ISRCF_IPI)) == 0)
502 if (isrc->isrc_flags & INTR_ISRCF_BOUND)
503 return (CPU_ISSET(cpu, &isrc->isrc_cpu));
505 CPU_SET(cpu, &isrc->isrc_cpu);
512 * Setup filter into interrupt source.
515 iscr_setup_filter(struct intr_irqsrc *isrc, const char *name,
516 intr_irq_filter_t *filter, void *arg, void **cookiep)
522 mtx_lock(&isrc_table_lock);
524 * Make sure that we do not mix the two ways
525 * how we handle interrupt sources.
527 if (isrc->isrc_filter != NULL || isrc->isrc_event != NULL) {
528 mtx_unlock(&isrc_table_lock);
531 isrc->isrc_filter = filter;
532 isrc->isrc_arg = arg;
533 isrc_update_name(isrc, name);
534 mtx_unlock(&isrc_table_lock);
542 * Interrupt source pre_ithread method for MI interrupt framework.
545 intr_isrc_pre_ithread(void *arg)
547 struct intr_irqsrc *isrc = arg;
549 PIC_PRE_ITHREAD(isrc->isrc_dev, isrc);
553 * Interrupt source post_ithread method for MI interrupt framework.
556 intr_isrc_post_ithread(void *arg)
558 struct intr_irqsrc *isrc = arg;
560 PIC_POST_ITHREAD(isrc->isrc_dev, isrc);
564 * Interrupt source post_filter method for MI interrupt framework.
567 intr_isrc_post_filter(void *arg)
569 struct intr_irqsrc *isrc = arg;
571 PIC_POST_FILTER(isrc->isrc_dev, isrc);
575 * Interrupt source assign_cpu method for MI interrupt framework.
578 intr_isrc_assign_cpu(void *arg, int cpu)
581 struct intr_irqsrc *isrc = arg;
584 if (isrc->isrc_dev != intr_irq_root_dev)
587 mtx_lock(&isrc_table_lock);
589 CPU_ZERO(&isrc->isrc_cpu);
590 isrc->isrc_flags &= ~INTR_ISRCF_BOUND;
592 CPU_SETOF(cpu, &isrc->isrc_cpu);
593 isrc->isrc_flags |= INTR_ISRCF_BOUND;
597 * In NOCPU case, it's up to PIC to either leave ISRC on same CPU or
598 * re-balance it to another CPU or enable it on more CPUs. However,
599 * PIC is expected to change isrc_cpu appropriately to keep us well
600 * informed if the call is successful.
602 if (irq_assign_cpu) {
603 error = PIC_BIND_INTR(isrc->isrc_dev, isrc);
605 CPU_ZERO(&isrc->isrc_cpu);
606 mtx_unlock(&isrc_table_lock);
610 mtx_unlock(&isrc_table_lock);
618 * Create interrupt event for interrupt source.
621 isrc_event_create(struct intr_irqsrc *isrc)
623 struct intr_event *ie;
626 error = intr_event_create(&ie, isrc, 0, isrc->isrc_irq,
627 intr_isrc_pre_ithread, intr_isrc_post_ithread, intr_isrc_post_filter,
628 intr_isrc_assign_cpu, "%s:", isrc->isrc_name);
632 mtx_lock(&isrc_table_lock);
634 * Make sure that we do not mix the two ways
635 * how we handle interrupt sources. Let contested event wins.
638 if (isrc->isrc_filter != NULL || isrc->isrc_event != NULL) {
640 if (isrc->isrc_event != NULL) {
642 mtx_unlock(&isrc_table_lock);
643 intr_event_destroy(ie);
644 return (isrc->isrc_event != NULL ? EBUSY : 0);
646 isrc->isrc_event = ie;
647 mtx_unlock(&isrc_table_lock);
653 * Destroy interrupt event for interrupt source.
656 isrc_event_destroy(struct intr_irqsrc *isrc)
658 struct intr_event *ie;
660 mtx_lock(&isrc_table_lock);
661 ie = isrc->isrc_event;
662 isrc->isrc_event = NULL;
663 mtx_unlock(&isrc_table_lock);
666 intr_event_destroy(ie);
670 * Add handler to interrupt source.
673 isrc_add_handler(struct intr_irqsrc *isrc, const char *name,
674 driver_filter_t filter, driver_intr_t handler, void *arg,
675 enum intr_type flags, void **cookiep)
679 if (isrc->isrc_event == NULL) {
680 error = isrc_event_create(isrc);
685 error = intr_event_add_handler(isrc->isrc_event, name, filter, handler,
686 arg, intr_priority(flags), flags, cookiep);
688 mtx_lock(&isrc_table_lock);
689 intrcnt_updatename(isrc);
690 mtx_unlock(&isrc_table_lock);
697 * Lookup interrupt controller locked.
699 static inline struct intr_pic *
700 pic_lookup_locked(device_t dev, intptr_t xref, int flags)
702 struct intr_pic *pic;
704 mtx_assert(&pic_list_lock, MA_OWNED);
706 if (dev == NULL && xref == 0)
709 /* Note that pic->pic_dev is never NULL on registered PIC. */
710 SLIST_FOREACH(pic, &pic_list, pic_next) {
711 if ((pic->pic_flags & FLAG_TYPE_MASK) !=
712 (flags & FLAG_TYPE_MASK))
716 if (xref == pic->pic_xref)
718 } else if (xref == 0 || pic->pic_xref == 0) {
719 if (dev == pic->pic_dev)
721 } else if (xref == pic->pic_xref && dev == pic->pic_dev)
728 * Lookup interrupt controller.
730 static struct intr_pic *
731 pic_lookup(device_t dev, intptr_t xref, int flags)
733 struct intr_pic *pic;
735 mtx_lock(&pic_list_lock);
736 pic = pic_lookup_locked(dev, xref, flags);
737 mtx_unlock(&pic_list_lock);
742 * Create interrupt controller.
744 static struct intr_pic *
745 pic_create(device_t dev, intptr_t xref, int flags)
747 struct intr_pic *pic;
749 mtx_lock(&pic_list_lock);
750 pic = pic_lookup_locked(dev, xref, flags);
752 mtx_unlock(&pic_list_lock);
755 pic = malloc(sizeof(*pic), M_INTRNG, M_NOWAIT | M_ZERO);
757 mtx_unlock(&pic_list_lock);
760 pic->pic_xref = xref;
762 pic->pic_flags = flags;
763 mtx_init(&pic->pic_child_lock, "pic child lock", NULL, MTX_SPIN);
764 SLIST_INSERT_HEAD(&pic_list, pic, pic_next);
765 mtx_unlock(&pic_list_lock);
771 * Destroy interrupt controller.
774 pic_destroy(device_t dev, intptr_t xref, int flags)
776 struct intr_pic *pic;
778 mtx_lock(&pic_list_lock);
779 pic = pic_lookup_locked(dev, xref, flags);
781 mtx_unlock(&pic_list_lock);
784 SLIST_REMOVE(&pic_list, pic, intr_pic, pic_next);
785 mtx_unlock(&pic_list_lock);
791 * Register interrupt controller.
794 intr_pic_register(device_t dev, intptr_t xref)
796 struct intr_pic *pic;
800 pic = pic_create(dev, xref, FLAG_PIC);
804 debugf("PIC %p registered for %s <dev %p, xref %jx>\n", pic,
805 device_get_nameunit(dev), dev, (uintmax_t)xref);
810 * Unregister interrupt controller.
813 intr_pic_deregister(device_t dev, intptr_t xref)
816 panic("%s: not implemented", __func__);
820 * Mark interrupt controller (itself) as a root one.
822 * Note that only an interrupt controller can really know its position
823 * in interrupt controller's tree. So root PIC must claim itself as a root.
825 * In FDT case, according to ePAPR approved version 1.1 from 08 April 2011,
827 * "The root of the interrupt tree is determined when traversal
828 * of the interrupt tree reaches an interrupt controller node without
829 * an interrupts property and thus no explicit interrupt parent."
832 intr_pic_claim_root(device_t dev, intptr_t xref, intr_irq_filter_t *filter,
833 void *arg, u_int ipicount)
835 struct intr_pic *pic;
837 pic = pic_lookup(dev, xref, FLAG_PIC);
839 device_printf(dev, "not registered\n");
843 KASSERT((pic->pic_flags & FLAG_TYPE_MASK) == FLAG_PIC,
844 ("%s: Found a non-PIC controller: %s", __func__,
845 device_get_name(pic->pic_dev)));
847 if (filter == NULL) {
848 device_printf(dev, "filter missing\n");
853 * Only one interrupt controllers could be on the root for now.
854 * Note that we further suppose that there is not threaded interrupt
855 * routine (handler) on the root. See intr_irq_handler().
857 if (intr_irq_root_dev != NULL) {
858 device_printf(dev, "another root already set\n");
862 intr_irq_root_dev = dev;
863 irq_root_filter = filter;
865 irq_root_ipicount = ipicount;
867 debugf("irq root set to %s\n", device_get_nameunit(dev));
872 * Add a handler to manage a sub range of a parents interrupts.
875 intr_pic_add_handler(device_t parent, struct intr_pic *pic,
876 intr_child_irq_filter_t *filter, void *arg, uintptr_t start,
879 struct intr_pic *parent_pic;
880 struct intr_pic_child *newchild;
882 struct intr_pic_child *child;
885 /* Find the parent PIC */
886 parent_pic = pic_lookup(parent, 0, FLAG_PIC);
887 if (parent_pic == NULL)
890 newchild = malloc(sizeof(*newchild), M_INTRNG, M_WAITOK | M_ZERO);
891 newchild->pc_pic = pic;
892 newchild->pc_filter = filter;
893 newchild->pc_filter_arg = arg;
894 newchild->pc_start = start;
895 newchild->pc_length = length;
897 mtx_lock_spin(&parent_pic->pic_child_lock);
899 SLIST_FOREACH(child, &parent_pic->pic_children, pc_next) {
900 KASSERT(child->pc_pic != pic, ("%s: Adding a child PIC twice",
904 SLIST_INSERT_HEAD(&parent_pic->pic_children, newchild, pc_next);
905 mtx_unlock_spin(&parent_pic->pic_child_lock);
911 intr_resolve_irq(device_t dev, intptr_t xref, struct intr_map_data *data,
912 struct intr_irqsrc **isrc)
914 struct intr_pic *pic;
915 struct intr_map_data_msi *msi;
920 pic = pic_lookup(dev, xref,
921 (data->type == INTR_MAP_DATA_MSI) ? FLAG_MSI : FLAG_PIC);
925 switch (data->type) {
926 case INTR_MAP_DATA_MSI:
927 KASSERT((pic->pic_flags & FLAG_TYPE_MASK) == FLAG_MSI,
928 ("%s: Found a non-MSI controller: %s", __func__,
929 device_get_name(pic->pic_dev)));
930 msi = (struct intr_map_data_msi *)data;
935 KASSERT((pic->pic_flags & FLAG_TYPE_MASK) == FLAG_PIC,
936 ("%s: Found a non-PIC controller: %s", __func__,
937 device_get_name(pic->pic_dev)));
938 return (PIC_MAP_INTR(pic->pic_dev, data, isrc));
943 intr_activate_irq(device_t dev, struct resource *res)
947 struct intr_map_data *data;
948 struct intr_irqsrc *isrc;
952 KASSERT(rman_get_start(res) == rman_get_end(res),
953 ("%s: more interrupts in resource", __func__));
955 res_id = (u_int)rman_get_start(res);
956 if (intr_map_get_isrc(res_id) != NULL)
957 panic("Attempt to double activation of resource id: %u\n",
959 intr_map_copy_map_data(res_id, &map_dev, &map_xref, &data);
960 error = intr_resolve_irq(map_dev, map_xref, data, &isrc);
962 free(data, M_INTRNG);
963 /* XXX TODO DISCONECTED PICs */
964 /* if (error == EINVAL) return(0); */
967 intr_map_set_isrc(res_id, isrc);
968 rman_set_virtual(res, data);
969 return (PIC_ACTIVATE_INTR(isrc->isrc_dev, isrc, res, data));
973 intr_deactivate_irq(device_t dev, struct resource *res)
975 struct intr_map_data *data;
976 struct intr_irqsrc *isrc;
980 KASSERT(rman_get_start(res) == rman_get_end(res),
981 ("%s: more interrupts in resource", __func__));
983 res_id = (u_int)rman_get_start(res);
984 isrc = intr_map_get_isrc(res_id);
986 panic("Attempt to deactivate non-active resource id: %u\n",
989 data = rman_get_virtual(res);
990 error = PIC_DEACTIVATE_INTR(isrc->isrc_dev, isrc, res, data);
991 intr_map_set_isrc(res_id, NULL);
992 rman_set_virtual(res, NULL);
993 free(data, M_INTRNG);
998 intr_setup_irq(device_t dev, struct resource *res, driver_filter_t filt,
999 driver_intr_t hand, void *arg, int flags, void **cookiep)
1002 struct intr_map_data *data;
1003 struct intr_irqsrc *isrc;
1007 KASSERT(rman_get_start(res) == rman_get_end(res),
1008 ("%s: more interrupts in resource", __func__));
1010 res_id = (u_int)rman_get_start(res);
1011 isrc = intr_map_get_isrc(res_id);
1013 /* XXX TODO DISCONECTED PICs */
1017 data = rman_get_virtual(res);
1018 name = device_get_nameunit(dev);
1022 * Standard handling is done through MI interrupt framework. However,
1023 * some interrupts could request solely own special handling. This
1024 * non standard handling can be used for interrupt controllers without
1025 * handler (filter only), so in case that interrupt controllers are
1026 * chained, MI interrupt framework is called only in leaf controller.
1028 * Note that root interrupt controller routine is served as well,
1029 * however in intr_irq_handler(), i.e. main system dispatch routine.
1031 if (flags & INTR_SOLO && hand != NULL) {
1032 debugf("irq %u cannot solo on %s\n", irq, name);
1036 if (flags & INTR_SOLO) {
1037 error = iscr_setup_filter(isrc, name, (intr_irq_filter_t *)filt,
1039 debugf("irq %u setup filter error %d on %s\n", isrc->isrc_irq, error,
1044 error = isrc_add_handler(isrc, name, filt, hand, arg, flags,
1046 debugf("irq %u add handler error %d on %s\n", isrc->isrc_irq, error, name);
1051 mtx_lock(&isrc_table_lock);
1052 error = PIC_SETUP_INTR(isrc->isrc_dev, isrc, res, data);
1054 isrc->isrc_handlers++;
1055 if (isrc->isrc_handlers == 1)
1056 PIC_ENABLE_INTR(isrc->isrc_dev, isrc);
1058 mtx_unlock(&isrc_table_lock);
1060 intr_event_remove_handler(*cookiep);
1065 intr_teardown_irq(device_t dev, struct resource *res, void *cookie)
1068 struct intr_map_data *data;
1069 struct intr_irqsrc *isrc;
1072 KASSERT(rman_get_start(res) == rman_get_end(res),
1073 ("%s: more interrupts in resource", __func__));
1075 res_id = (u_int)rman_get_start(res);
1076 isrc = intr_map_get_isrc(res_id);
1077 if (isrc == NULL || isrc->isrc_handlers == 0)
1080 data = rman_get_virtual(res);
1083 if (isrc->isrc_filter != NULL) {
1087 mtx_lock(&isrc_table_lock);
1088 isrc->isrc_filter = NULL;
1089 isrc->isrc_arg = NULL;
1090 isrc->isrc_handlers = 0;
1091 PIC_DISABLE_INTR(isrc->isrc_dev, isrc);
1092 PIC_TEARDOWN_INTR(isrc->isrc_dev, isrc, res, data);
1093 isrc_update_name(isrc, NULL);
1094 mtx_unlock(&isrc_table_lock);
1098 if (isrc != intr_handler_source(cookie))
1101 error = intr_event_remove_handler(cookie);
1103 mtx_lock(&isrc_table_lock);
1104 isrc->isrc_handlers--;
1105 if (isrc->isrc_handlers == 0)
1106 PIC_DISABLE_INTR(isrc->isrc_dev, isrc);
1107 PIC_TEARDOWN_INTR(isrc->isrc_dev, isrc, res, data);
1108 intrcnt_updatename(isrc);
1109 mtx_unlock(&isrc_table_lock);
1115 intr_describe_irq(device_t dev, struct resource *res, void *cookie,
1119 struct intr_irqsrc *isrc;
1122 KASSERT(rman_get_start(res) == rman_get_end(res),
1123 ("%s: more interrupts in resource", __func__));
1125 res_id = (u_int)rman_get_start(res);
1126 isrc = intr_map_get_isrc(res_id);
1127 if (isrc == NULL || isrc->isrc_handlers == 0)
1130 if (isrc->isrc_filter != NULL) {
1134 mtx_lock(&isrc_table_lock);
1135 isrc_update_name(isrc, descr);
1136 mtx_unlock(&isrc_table_lock);
1140 error = intr_event_describe_handler(isrc->isrc_event, cookie, descr);
1142 mtx_lock(&isrc_table_lock);
1143 intrcnt_updatename(isrc);
1144 mtx_unlock(&isrc_table_lock);
1151 intr_bind_irq(device_t dev, struct resource *res, int cpu)
1153 struct intr_irqsrc *isrc;
1156 KASSERT(rman_get_start(res) == rman_get_end(res),
1157 ("%s: more interrupts in resource", __func__));
1159 res_id = (u_int)rman_get_start(res);
1160 isrc = intr_map_get_isrc(res_id);
1161 if (isrc == NULL || isrc->isrc_handlers == 0)
1164 if (isrc->isrc_filter != NULL)
1165 return (intr_isrc_assign_cpu(isrc, cpu));
1167 return (intr_event_bind(isrc->isrc_event, cpu));
1171 * Return the CPU that the next interrupt source should use.
1172 * For now just returns the next CPU according to round-robin.
1175 intr_irq_next_cpu(u_int last_cpu, cpuset_t *cpumask)
1179 KASSERT(!CPU_EMPTY(cpumask), ("%s: Empty CPU mask", __func__));
1180 if (!irq_assign_cpu || mp_ncpus == 1) {
1181 cpu = PCPU_GET(cpuid);
1183 if (CPU_ISSET(cpu, cpumask))
1186 return (CPU_FFS(cpumask) - 1);
1191 if (last_cpu > mp_maxid)
1193 } while (!CPU_ISSET(last_cpu, cpumask));
1197 #ifndef EARLY_AP_STARTUP
1199 * Distribute all the interrupt sources among the available
1200 * CPUs once the AP's have been launched.
1203 intr_irq_shuffle(void *arg __unused)
1205 struct intr_irqsrc *isrc;
1211 mtx_lock(&isrc_table_lock);
1212 irq_assign_cpu = true;
1213 for (i = 0; i < NIRQ; i++) {
1214 isrc = irq_sources[i];
1215 if (isrc == NULL || isrc->isrc_handlers == 0 ||
1216 isrc->isrc_flags & (INTR_ISRCF_PPI | INTR_ISRCF_IPI))
1219 if (isrc->isrc_event != NULL &&
1220 isrc->isrc_flags & INTR_ISRCF_BOUND &&
1221 isrc->isrc_event->ie_cpu != CPU_FFS(&isrc->isrc_cpu) - 1)
1222 panic("%s: CPU inconsistency", __func__);
1224 if ((isrc->isrc_flags & INTR_ISRCF_BOUND) == 0)
1225 CPU_ZERO(&isrc->isrc_cpu); /* start again */
1228 * We are in wicked position here if the following call fails
1229 * for bound ISRC. The best thing we can do is to clear
1230 * isrc_cpu so inconsistency with ie_cpu will be detectable.
1232 if (PIC_BIND_INTR(isrc->isrc_dev, isrc) != 0)
1233 CPU_ZERO(&isrc->isrc_cpu);
1235 mtx_unlock(&isrc_table_lock);
1237 SYSINIT(intr_irq_shuffle, SI_SUB_SMP, SI_ORDER_SECOND, intr_irq_shuffle, NULL);
1238 #endif /* !EARLY_AP_STARTUP */
1242 intr_irq_next_cpu(u_int current_cpu, cpuset_t *cpumask)
1245 return (PCPU_GET(cpuid));
1250 * Allocate memory for new intr_map_data structure.
1251 * Initialize common fields.
1253 struct intr_map_data *
1254 intr_alloc_map_data(enum intr_map_data_type type, size_t len, int flags)
1256 struct intr_map_data *data;
1258 data = malloc(len, M_INTRNG, flags);
1264 void intr_free_intr_map_data(struct intr_map_data *data)
1267 free(data, M_INTRNG);
1271 * Register a MSI/MSI-X interrupt controller
1274 intr_msi_register(device_t dev, intptr_t xref)
1276 struct intr_pic *pic;
1280 pic = pic_create(dev, xref, FLAG_MSI);
1284 debugf("PIC %p registered for %s <dev %p, xref %jx>\n", pic,
1285 device_get_nameunit(dev), dev, (uintmax_t)xref);
1290 intr_alloc_msi(device_t pci, device_t child, intptr_t xref, int count,
1291 int maxcount, int *irqs)
1293 struct intr_irqsrc **isrc;
1294 struct intr_pic *pic;
1296 struct intr_map_data_msi *msi;
1299 pic = pic_lookup(NULL, xref, FLAG_MSI);
1303 KASSERT((pic->pic_flags & FLAG_TYPE_MASK) == FLAG_MSI,
1304 ("%s: Found a non-MSI controller: %s", __func__,
1305 device_get_name(pic->pic_dev)));
1307 isrc = malloc(sizeof(*isrc) * count, M_INTRNG, M_WAITOK);
1308 err = MSI_ALLOC_MSI(pic->pic_dev, child, count, maxcount, &pdev, isrc);
1310 free(isrc, M_INTRNG);
1314 for (i = 0; i < count; i++) {
1315 msi = (struct intr_map_data_msi *)intr_alloc_map_data(
1316 INTR_MAP_DATA_MSI, sizeof(*msi), M_WAITOK | M_ZERO);
1317 msi-> isrc = isrc[i];
1318 irqs[i] = intr_map_irq(pic->pic_dev, xref,
1319 (struct intr_map_data *)msi);
1321 free(isrc, M_INTRNG);
1327 intr_release_msi(device_t pci, device_t child, intptr_t xref, int count,
1330 struct intr_irqsrc **isrc;
1331 struct intr_pic *pic;
1332 struct intr_map_data_msi *msi;
1335 pic = pic_lookup(NULL, xref, FLAG_MSI);
1339 KASSERT((pic->pic_flags & FLAG_TYPE_MASK) == FLAG_MSI,
1340 ("%s: Found a non-MSI controller: %s", __func__,
1341 device_get_name(pic->pic_dev)));
1343 isrc = malloc(sizeof(*isrc) * count, M_INTRNG, M_WAITOK);
1345 for (i = 0; i < count; i++) {
1346 msi = (struct intr_map_data_msi *)
1347 intr_map_get_map_data(irqs[i]);
1348 KASSERT(msi->hdr.type == INTR_MAP_DATA_MSI,
1349 ("%s: irq %d map data is not MSI", __func__,
1351 isrc[i] = msi->isrc;
1354 err = MSI_RELEASE_MSI(pic->pic_dev, child, count, isrc);
1356 for (i = 0; i < count; i++) {
1357 if (isrc[i] != NULL)
1358 intr_unmap_irq(irqs[i]);
1361 free(isrc, M_INTRNG);
1366 intr_alloc_msix(device_t pci, device_t child, intptr_t xref, int *irq)
1368 struct intr_irqsrc *isrc;
1369 struct intr_pic *pic;
1371 struct intr_map_data_msi *msi;
1374 pic = pic_lookup(NULL, xref, FLAG_MSI);
1378 KASSERT((pic->pic_flags & FLAG_TYPE_MASK) == FLAG_MSI,
1379 ("%s: Found a non-MSI controller: %s", __func__,
1380 device_get_name(pic->pic_dev)));
1382 err = MSI_ALLOC_MSIX(pic->pic_dev, child, &pdev, &isrc);
1386 msi = (struct intr_map_data_msi *)intr_alloc_map_data(
1387 INTR_MAP_DATA_MSI, sizeof(*msi), M_WAITOK | M_ZERO);
1389 *irq = intr_map_irq(pic->pic_dev, xref, (struct intr_map_data *)msi);
1394 intr_release_msix(device_t pci, device_t child, intptr_t xref, int irq)
1396 struct intr_irqsrc *isrc;
1397 struct intr_pic *pic;
1398 struct intr_map_data_msi *msi;
1401 pic = pic_lookup(NULL, xref, FLAG_MSI);
1405 KASSERT((pic->pic_flags & FLAG_TYPE_MASK) == FLAG_MSI,
1406 ("%s: Found a non-MSI controller: %s", __func__,
1407 device_get_name(pic->pic_dev)));
1409 msi = (struct intr_map_data_msi *)
1410 intr_map_get_map_data(irq);
1411 KASSERT(msi->hdr.type == INTR_MAP_DATA_MSI,
1412 ("%s: irq %d map data is not MSI", __func__,
1416 intr_unmap_irq(irq);
1420 err = MSI_RELEASE_MSIX(pic->pic_dev, child, isrc);
1421 intr_unmap_irq(irq);
1427 intr_map_msi(device_t pci, device_t child, intptr_t xref, int irq,
1428 uint64_t *addr, uint32_t *data)
1430 struct intr_irqsrc *isrc;
1431 struct intr_pic *pic;
1434 pic = pic_lookup(NULL, xref, FLAG_MSI);
1438 KASSERT((pic->pic_flags & FLAG_TYPE_MASK) == FLAG_MSI,
1439 ("%s: Found a non-MSI controller: %s", __func__,
1440 device_get_name(pic->pic_dev)));
1442 isrc = intr_map_get_isrc(irq);
1446 err = MSI_MAP_MSI(pic->pic_dev, child, isrc, addr, data);
1450 void dosoftints(void);
1458 * Init interrupt controller on another CPU.
1461 intr_pic_init_secondary(void)
1465 * QQQ: Only root PIC is aware of other CPUs ???
1467 KASSERT(intr_irq_root_dev != NULL, ("%s: no root attached", __func__));
1469 //mtx_lock(&isrc_table_lock);
1470 PIC_INIT_SECONDARY(intr_irq_root_dev);
1471 //mtx_unlock(&isrc_table_lock);
1476 DB_SHOW_COMMAND(irqs, db_show_irqs)
1480 struct intr_irqsrc *isrc;
1482 for (irqsum = 0, i = 0; i < NIRQ; i++) {
1483 isrc = irq_sources[i];
1487 num = isrc->isrc_count != NULL ? isrc->isrc_count[0] : 0;
1488 db_printf("irq%-3u <%s>: cpu %02lx%s cnt %lu\n", i,
1489 isrc->isrc_name, isrc->isrc_cpu.__bits[0],
1490 isrc->isrc_flags & INTR_ISRCF_BOUND ? " (bound)" : "", num);
1493 db_printf("irq total %u\n", irqsum);
1498 * Interrupt mapping table functions.
1500 * Please, keep this part separately, it can be transformed to
1501 * extension of standard resources.
1503 struct intr_map_entry
1507 struct intr_map_data *map_data;
1508 struct intr_irqsrc *isrc;
1509 /* XXX TODO DISCONECTED PICs */
1513 /* XXX Convert irq_map[] to dynamicaly expandable one. */
1514 static struct intr_map_entry *irq_map[2 * NIRQ];
1515 static int irq_map_count = nitems(irq_map);
1516 static int irq_map_first_free_idx;
1517 static struct mtx irq_map_lock;
1519 static struct intr_irqsrc *
1520 intr_map_get_isrc(u_int res_id)
1522 struct intr_irqsrc *isrc;
1525 mtx_lock(&irq_map_lock);
1526 if (res_id < irq_map_count && irq_map[res_id] != NULL)
1527 isrc = irq_map[res_id]->isrc;
1528 mtx_unlock(&irq_map_lock);
1534 intr_map_set_isrc(u_int res_id, struct intr_irqsrc *isrc)
1537 mtx_lock(&irq_map_lock);
1538 if (res_id < irq_map_count && irq_map[res_id] != NULL)
1539 irq_map[res_id]->isrc = isrc;
1540 mtx_unlock(&irq_map_lock);
1544 * Get a copy of intr_map_entry data
1546 static struct intr_map_data *
1547 intr_map_get_map_data(u_int res_id)
1549 struct intr_map_data *data;
1552 mtx_lock(&irq_map_lock);
1553 if (res_id >= irq_map_count || irq_map[res_id] == NULL)
1554 panic("Attempt to copy invalid resource id: %u\n", res_id);
1555 data = irq_map[res_id]->map_data;
1556 mtx_unlock(&irq_map_lock);
1562 * Get a copy of intr_map_entry data
1565 intr_map_copy_map_data(u_int res_id, device_t *map_dev, intptr_t *map_xref,
1566 struct intr_map_data **data)
1571 mtx_lock(&irq_map_lock);
1572 if (res_id >= irq_map_count || irq_map[res_id] == NULL)
1573 panic("Attempt to copy invalid resource id: %u\n", res_id);
1574 if (irq_map[res_id]->map_data != NULL)
1575 len = irq_map[res_id]->map_data->len;
1576 mtx_unlock(&irq_map_lock);
1581 *data = malloc(len, M_INTRNG, M_WAITOK | M_ZERO);
1582 mtx_lock(&irq_map_lock);
1583 if (irq_map[res_id] == NULL)
1584 panic("Attempt to copy invalid resource id: %u\n", res_id);
1586 if (len != irq_map[res_id]->map_data->len)
1587 panic("Resource id: %u has changed.\n", res_id);
1588 memcpy(*data, irq_map[res_id]->map_data, len);
1590 *map_dev = irq_map[res_id]->dev;
1591 *map_xref = irq_map[res_id]->xref;
1592 mtx_unlock(&irq_map_lock);
1596 * Allocate and fill new entry in irq_map table.
1599 intr_map_irq(device_t dev, intptr_t xref, struct intr_map_data *data)
1602 struct intr_map_entry *entry;
1604 /* Prepare new entry first. */
1605 entry = malloc(sizeof(*entry), M_INTRNG, M_WAITOK | M_ZERO);
1609 entry->map_data = data;
1612 mtx_lock(&irq_map_lock);
1613 for (i = irq_map_first_free_idx; i < irq_map_count; i++) {
1614 if (irq_map[i] == NULL) {
1616 irq_map_first_free_idx = i + 1;
1617 mtx_unlock(&irq_map_lock);
1621 mtx_unlock(&irq_map_lock);
1623 /* XXX Expand irq_map table */
1624 panic("IRQ mapping table is full.");
1628 * Remove and free mapping entry.
1631 intr_unmap_irq(u_int res_id)
1633 struct intr_map_entry *entry;
1635 mtx_lock(&irq_map_lock);
1636 if ((res_id >= irq_map_count) || (irq_map[res_id] == NULL))
1637 panic("Attempt to unmap invalid resource id: %u\n", res_id);
1638 entry = irq_map[res_id];
1639 irq_map[res_id] = NULL;
1640 irq_map_first_free_idx = res_id;
1641 mtx_unlock(&irq_map_lock);
1642 intr_free_intr_map_data(entry->map_data);
1643 free(entry, M_INTRNG);
1647 * Clone mapping entry.
1650 intr_map_clone_irq(u_int old_res_id)
1654 struct intr_map_data *data;
1656 intr_map_copy_map_data(old_res_id, &map_dev, &map_xref, &data);
1657 return (intr_map_irq(map_dev, map_xref, data));
1661 intr_map_init(void *dummy __unused)
1664 mtx_init(&irq_map_lock, "intr map table", NULL, MTX_DEF);
1666 SYSINIT(intr_map_init, SI_SUB_INTR, SI_ORDER_FIRST, intr_map_init, NULL);