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>
57 #include <sys/pmckern.h>
60 #include <machine/atomic.h>
61 #include <machine/intr.h>
62 #include <machine/cpu.h>
63 #include <machine/smp.h>
64 #include <machine/stdarg.h>
73 #define INTRNAME_LEN (2*MAXCOMLEN + 1)
76 #define debugf(fmt, args...) do { printf("%s(): ", __func__); \
77 printf(fmt,##args); } while (0)
79 #define debugf(fmt, args...)
82 MALLOC_DECLARE(M_INTRNG);
83 MALLOC_DEFINE(M_INTRNG, "intr", "intr interrupt handling");
85 /* Main interrupt handler called from assembler -> 'hidden' for C code. */
86 void intr_irq_handler(struct trapframe *tf);
88 /* Root interrupt controller stuff. */
89 device_t intr_irq_root_dev;
90 static intr_irq_filter_t *irq_root_filter;
91 static void *irq_root_arg;
92 static u_int irq_root_ipicount;
94 struct intr_pic_child {
95 SLIST_ENTRY(intr_pic_child) pc_next;
96 struct intr_pic *pc_pic;
97 intr_child_irq_filter_t *pc_filter;
103 /* Interrupt controller definition. */
105 SLIST_ENTRY(intr_pic) pic_next;
106 intptr_t pic_xref; /* hardware identification */
108 #define FLAG_PIC (1 << 0)
109 #define FLAG_MSI (1 << 1)
111 struct mtx pic_child_lock;
112 SLIST_HEAD(, intr_pic_child) pic_children;
115 static struct mtx pic_list_lock;
116 static SLIST_HEAD(, intr_pic) pic_list;
118 static struct intr_pic *pic_lookup(device_t dev, intptr_t xref);
120 /* Interrupt source definition. */
121 static struct mtx isrc_table_lock;
122 static struct intr_irqsrc *irq_sources[NIRQ];
126 static boolean_t irq_assign_cpu = FALSE;
130 * - 2 counters for each I/O interrupt.
131 * - MAXCPU counters for each IPI counters for SMP.
134 #define INTRCNT_COUNT (NIRQ * 2 + INTR_IPI_COUNT * MAXCPU)
136 #define INTRCNT_COUNT (NIRQ * 2)
139 /* Data for MI statistics reporting. */
140 u_long intrcnt[INTRCNT_COUNT];
141 char intrnames[INTRCNT_COUNT * INTRNAME_LEN];
142 size_t sintrcnt = sizeof(intrcnt);
143 size_t sintrnames = sizeof(intrnames);
144 static u_int intrcnt_index;
146 static struct intr_irqsrc *intr_map_get_isrc(u_int res_id);
147 static void intr_map_set_isrc(u_int res_id, struct intr_irqsrc *isrc);
148 static void intr_map_copy_map_data(u_int res_id, device_t *dev, intptr_t *xref,
149 struct intr_map_data **data);
152 * Interrupt framework initialization routine.
155 intr_irq_init(void *dummy __unused)
158 SLIST_INIT(&pic_list);
159 mtx_init(&pic_list_lock, "intr pic list", NULL, MTX_DEF);
161 mtx_init(&isrc_table_lock, "intr isrc table", NULL, MTX_DEF);
163 SYSINIT(intr_irq_init, SI_SUB_INTR, SI_ORDER_FIRST, intr_irq_init, NULL);
166 intrcnt_setname(const char *name, int index)
169 snprintf(intrnames + INTRNAME_LEN * index, INTRNAME_LEN, "%-*s",
170 INTRNAME_LEN - 1, name);
174 * Update name for interrupt source with interrupt event.
177 intrcnt_updatename(struct intr_irqsrc *isrc)
180 /* QQQ: What about stray counter name? */
181 mtx_assert(&isrc_table_lock, MA_OWNED);
182 intrcnt_setname(isrc->isrc_event->ie_fullname, isrc->isrc_index);
186 * Virtualization for interrupt source interrupt counter increment.
189 isrc_increment_count(struct intr_irqsrc *isrc)
192 if (isrc->isrc_flags & INTR_ISRCF_PPI)
193 atomic_add_long(&isrc->isrc_count[0], 1);
195 isrc->isrc_count[0]++;
199 * Virtualization for interrupt source interrupt stray counter increment.
202 isrc_increment_straycount(struct intr_irqsrc *isrc)
205 isrc->isrc_count[1]++;
209 * Virtualization for interrupt source interrupt name update.
212 isrc_update_name(struct intr_irqsrc *isrc, const char *name)
214 char str[INTRNAME_LEN];
216 mtx_assert(&isrc_table_lock, MA_OWNED);
219 snprintf(str, INTRNAME_LEN, "%s: %s", isrc->isrc_name, name);
220 intrcnt_setname(str, isrc->isrc_index);
221 snprintf(str, INTRNAME_LEN, "stray %s: %s", isrc->isrc_name,
223 intrcnt_setname(str, isrc->isrc_index + 1);
225 snprintf(str, INTRNAME_LEN, "%s:", isrc->isrc_name);
226 intrcnt_setname(str, isrc->isrc_index);
227 snprintf(str, INTRNAME_LEN, "stray %s:", isrc->isrc_name);
228 intrcnt_setname(str, isrc->isrc_index + 1);
233 * Virtualization for interrupt source interrupt counters setup.
236 isrc_setup_counters(struct intr_irqsrc *isrc)
241 * XXX - it does not work well with removable controllers and
242 * interrupt sources !!!
244 index = atomic_fetchadd_int(&intrcnt_index, 2);
245 isrc->isrc_index = index;
246 isrc->isrc_count = &intrcnt[index];
247 isrc_update_name(isrc, NULL);
251 * Virtualization for interrupt source interrupt counters release.
254 isrc_release_counters(struct intr_irqsrc *isrc)
257 panic("%s: not implemented", __func__);
262 * Virtualization for interrupt source IPI counters setup.
265 intr_ipi_setup_counters(const char *name)
268 char str[INTRNAME_LEN];
270 index = atomic_fetchadd_int(&intrcnt_index, MAXCPU);
271 for (i = 0; i < MAXCPU; i++) {
272 snprintf(str, INTRNAME_LEN, "cpu%d:%s", i, name);
273 intrcnt_setname(str, index + i);
275 return (&intrcnt[index]);
280 * Main interrupt dispatch handler. It's called straight
281 * from the assembler, where CPU interrupt is served.
284 intr_irq_handler(struct trapframe *tf)
286 struct trapframe * oldframe;
289 KASSERT(irq_root_filter != NULL, ("%s: no filter", __func__));
291 PCPU_INC(cnt.v_intr);
294 oldframe = td->td_intr_frame;
295 td->td_intr_frame = tf;
296 irq_root_filter(irq_root_arg);
297 td->td_intr_frame = oldframe;
300 if (pmc_hook && TRAPF_USERMODE(tf) &&
301 (PCPU_GET(curthread)->td_pflags & TDP_CALLCHAIN))
302 pmc_hook(PCPU_GET(curthread), PMC_FN_USER_CALLCHAIN, tf);
307 intr_child_irq_handler(struct intr_pic *parent, uintptr_t irq)
309 struct intr_pic_child *child;
313 mtx_lock_spin(&parent->pic_child_lock);
314 SLIST_FOREACH(child, &parent->pic_children, pc_next) {
315 if (child->pc_start <= irq &&
316 irq < (child->pc_start + child->pc_length)) {
321 mtx_unlock_spin(&parent->pic_child_lock);
324 return (child->pc_filter(child->pc_filter_arg, irq));
326 return (FILTER_STRAY);
330 * interrupt controller dispatch function for interrupts. It should
331 * be called straight from the interrupt controller, when associated interrupt
335 intr_isrc_dispatch(struct intr_irqsrc *isrc, struct trapframe *tf)
338 KASSERT(isrc != NULL, ("%s: no source", __func__));
340 isrc_increment_count(isrc);
343 if (isrc->isrc_filter != NULL) {
345 error = isrc->isrc_filter(isrc->isrc_arg, tf);
346 PIC_POST_FILTER(isrc->isrc_dev, isrc);
347 if (error == FILTER_HANDLED)
351 if (isrc->isrc_event != NULL) {
352 if (intr_event_handle(isrc->isrc_event, tf) == 0)
356 isrc_increment_straycount(isrc);
361 * Alloc unique interrupt number (resource handle) for interrupt source.
363 * There could be various strategies how to allocate free interrupt number
364 * (resource handle) for new interrupt source.
366 * 1. Handles are always allocated forward, so handles are not recycled
367 * immediately. However, if only one free handle left which is reused
371 isrc_alloc_irq(struct intr_irqsrc *isrc)
375 mtx_assert(&isrc_table_lock, MA_OWNED);
377 maxirqs = nitems(irq_sources);
378 if (irq_next_free >= maxirqs)
381 for (irq = irq_next_free; irq < maxirqs; irq++) {
382 if (irq_sources[irq] == NULL)
385 for (irq = 0; irq < irq_next_free; irq++) {
386 if (irq_sources[irq] == NULL)
390 irq_next_free = maxirqs;
394 isrc->isrc_irq = irq;
395 irq_sources[irq] = isrc;
397 irq_next_free = irq + 1;
398 if (irq_next_free >= maxirqs)
404 * Free unique interrupt number (resource handle) from interrupt source.
407 isrc_free_irq(struct intr_irqsrc *isrc)
410 mtx_assert(&isrc_table_lock, MA_OWNED);
412 if (isrc->isrc_irq >= nitems(irq_sources))
414 if (irq_sources[isrc->isrc_irq] != isrc)
417 irq_sources[isrc->isrc_irq] = NULL;
418 isrc->isrc_irq = INTR_IRQ_INVALID; /* just to be safe */
423 * Initialize interrupt source and register it into global interrupt table.
426 intr_isrc_register(struct intr_irqsrc *isrc, device_t dev, u_int flags,
427 const char *fmt, ...)
432 bzero(isrc, sizeof(struct intr_irqsrc));
433 isrc->isrc_dev = dev;
434 isrc->isrc_irq = INTR_IRQ_INVALID; /* just to be safe */
435 isrc->isrc_flags = flags;
438 vsnprintf(isrc->isrc_name, INTR_ISRC_NAMELEN, fmt, ap);
441 mtx_lock(&isrc_table_lock);
442 error = isrc_alloc_irq(isrc);
444 mtx_unlock(&isrc_table_lock);
448 * Setup interrupt counters, but not for IPI sources. Those are setup
449 * later and only for used ones (up to INTR_IPI_COUNT) to not exhaust
452 if ((isrc->isrc_flags & INTR_ISRCF_IPI) == 0)
453 isrc_setup_counters(isrc);
454 mtx_unlock(&isrc_table_lock);
459 * Deregister interrupt source from global interrupt table.
462 intr_isrc_deregister(struct intr_irqsrc *isrc)
466 mtx_lock(&isrc_table_lock);
467 if ((isrc->isrc_flags & INTR_ISRCF_IPI) == 0)
468 isrc_release_counters(isrc);
469 error = isrc_free_irq(isrc);
470 mtx_unlock(&isrc_table_lock);
476 * A support function for a PIC to decide if provided ISRC should be inited
477 * on given cpu. The logic of INTR_ISRCF_BOUND flag and isrc_cpu member of
478 * struct intr_irqsrc is the following:
480 * If INTR_ISRCF_BOUND is set, the ISRC should be inited only on cpus
481 * set in isrc_cpu. If not, the ISRC should be inited on every cpu and
482 * isrc_cpu is kept consistent with it. Thus isrc_cpu is always correct.
485 intr_isrc_init_on_cpu(struct intr_irqsrc *isrc, u_int cpu)
488 if (isrc->isrc_handlers == 0)
490 if ((isrc->isrc_flags & (INTR_ISRCF_PPI | INTR_ISRCF_IPI)) == 0)
492 if (isrc->isrc_flags & INTR_ISRCF_BOUND)
493 return (CPU_ISSET(cpu, &isrc->isrc_cpu));
495 CPU_SET(cpu, &isrc->isrc_cpu);
502 * Setup filter into interrupt source.
505 iscr_setup_filter(struct intr_irqsrc *isrc, const char *name,
506 intr_irq_filter_t *filter, void *arg, void **cookiep)
512 mtx_lock(&isrc_table_lock);
514 * Make sure that we do not mix the two ways
515 * how we handle interrupt sources.
517 if (isrc->isrc_filter != NULL || isrc->isrc_event != NULL) {
518 mtx_unlock(&isrc_table_lock);
521 isrc->isrc_filter = filter;
522 isrc->isrc_arg = arg;
523 isrc_update_name(isrc, name);
524 mtx_unlock(&isrc_table_lock);
532 * Interrupt source pre_ithread method for MI interrupt framework.
535 intr_isrc_pre_ithread(void *arg)
537 struct intr_irqsrc *isrc = arg;
539 PIC_PRE_ITHREAD(isrc->isrc_dev, isrc);
543 * Interrupt source post_ithread method for MI interrupt framework.
546 intr_isrc_post_ithread(void *arg)
548 struct intr_irqsrc *isrc = arg;
550 PIC_POST_ITHREAD(isrc->isrc_dev, isrc);
554 * Interrupt source post_filter method for MI interrupt framework.
557 intr_isrc_post_filter(void *arg)
559 struct intr_irqsrc *isrc = arg;
561 PIC_POST_FILTER(isrc->isrc_dev, isrc);
565 * Interrupt source assign_cpu method for MI interrupt framework.
568 intr_isrc_assign_cpu(void *arg, int cpu)
571 struct intr_irqsrc *isrc = arg;
574 if (isrc->isrc_dev != intr_irq_root_dev)
577 mtx_lock(&isrc_table_lock);
579 CPU_ZERO(&isrc->isrc_cpu);
580 isrc->isrc_flags &= ~INTR_ISRCF_BOUND;
582 CPU_SETOF(cpu, &isrc->isrc_cpu);
583 isrc->isrc_flags |= INTR_ISRCF_BOUND;
587 * In NOCPU case, it's up to PIC to either leave ISRC on same CPU or
588 * re-balance it to another CPU or enable it on more CPUs. However,
589 * PIC is expected to change isrc_cpu appropriately to keep us well
590 * informed if the call is successful.
592 if (irq_assign_cpu) {
593 error = PIC_BIND_INTR(isrc->isrc_dev, isrc);
595 CPU_ZERO(&isrc->isrc_cpu);
596 mtx_unlock(&isrc_table_lock);
600 mtx_unlock(&isrc_table_lock);
608 * Create interrupt event for interrupt source.
611 isrc_event_create(struct intr_irqsrc *isrc)
613 struct intr_event *ie;
616 error = intr_event_create(&ie, isrc, 0, isrc->isrc_irq,
617 intr_isrc_pre_ithread, intr_isrc_post_ithread, intr_isrc_post_filter,
618 intr_isrc_assign_cpu, "%s:", isrc->isrc_name);
622 mtx_lock(&isrc_table_lock);
624 * Make sure that we do not mix the two ways
625 * how we handle interrupt sources. Let contested event wins.
628 if (isrc->isrc_filter != NULL || isrc->isrc_event != NULL) {
630 if (isrc->isrc_event != NULL) {
632 mtx_unlock(&isrc_table_lock);
633 intr_event_destroy(ie);
634 return (isrc->isrc_event != NULL ? EBUSY : 0);
636 isrc->isrc_event = ie;
637 mtx_unlock(&isrc_table_lock);
643 * Destroy interrupt event for interrupt source.
646 isrc_event_destroy(struct intr_irqsrc *isrc)
648 struct intr_event *ie;
650 mtx_lock(&isrc_table_lock);
651 ie = isrc->isrc_event;
652 isrc->isrc_event = NULL;
653 mtx_unlock(&isrc_table_lock);
656 intr_event_destroy(ie);
660 * Add handler to interrupt source.
663 isrc_add_handler(struct intr_irqsrc *isrc, const char *name,
664 driver_filter_t filter, driver_intr_t handler, void *arg,
665 enum intr_type flags, void **cookiep)
669 if (isrc->isrc_event == NULL) {
670 error = isrc_event_create(isrc);
675 error = intr_event_add_handler(isrc->isrc_event, name, filter, handler,
676 arg, intr_priority(flags), flags, cookiep);
678 mtx_lock(&isrc_table_lock);
679 intrcnt_updatename(isrc);
680 mtx_unlock(&isrc_table_lock);
687 * Lookup interrupt controller locked.
689 static inline struct intr_pic *
690 pic_lookup_locked(device_t dev, intptr_t xref)
692 struct intr_pic *pic;
694 mtx_assert(&pic_list_lock, MA_OWNED);
696 if (dev == NULL && xref == 0)
699 /* Note that pic->pic_dev is never NULL on registered PIC. */
700 SLIST_FOREACH(pic, &pic_list, pic_next) {
702 if (xref == pic->pic_xref)
704 } else if (xref == 0 || pic->pic_xref == 0) {
705 if (dev == pic->pic_dev)
707 } else if (xref == pic->pic_xref && dev == pic->pic_dev)
714 * Lookup interrupt controller.
716 static struct intr_pic *
717 pic_lookup(device_t dev, intptr_t xref)
719 struct intr_pic *pic;
721 mtx_lock(&pic_list_lock);
722 pic = pic_lookup_locked(dev, xref);
723 mtx_unlock(&pic_list_lock);
728 * Create interrupt controller.
730 static struct intr_pic *
731 pic_create(device_t dev, intptr_t xref)
733 struct intr_pic *pic;
735 mtx_lock(&pic_list_lock);
736 pic = pic_lookup_locked(dev, xref);
738 mtx_unlock(&pic_list_lock);
741 pic = malloc(sizeof(*pic), M_INTRNG, M_NOWAIT | M_ZERO);
743 mtx_unlock(&pic_list_lock);
746 pic->pic_xref = xref;
748 mtx_init(&pic->pic_child_lock, "pic child lock", NULL, MTX_SPIN);
749 SLIST_INSERT_HEAD(&pic_list, pic, pic_next);
750 mtx_unlock(&pic_list_lock);
756 * Destroy interrupt controller.
759 pic_destroy(device_t dev, intptr_t xref)
761 struct intr_pic *pic;
763 mtx_lock(&pic_list_lock);
764 pic = pic_lookup_locked(dev, xref);
766 mtx_unlock(&pic_list_lock);
769 SLIST_REMOVE(&pic_list, pic, intr_pic, pic_next);
770 mtx_unlock(&pic_list_lock);
776 * Register interrupt controller.
779 intr_pic_register(device_t dev, intptr_t xref)
781 struct intr_pic *pic;
785 pic = pic_create(dev, xref);
789 pic->pic_flags |= FLAG_PIC;
791 debugf("PIC %p registered for %s <dev %p, xref %x>\n", pic,
792 device_get_nameunit(dev), dev, xref);
797 * Unregister interrupt controller.
800 intr_pic_deregister(device_t dev, intptr_t xref)
803 panic("%s: not implemented", __func__);
807 * Mark interrupt controller (itself) as a root one.
809 * Note that only an interrupt controller can really know its position
810 * in interrupt controller's tree. So root PIC must claim itself as a root.
812 * In FDT case, according to ePAPR approved version 1.1 from 08 April 2011,
814 * "The root of the interrupt tree is determined when traversal
815 * of the interrupt tree reaches an interrupt controller node without
816 * an interrupts property and thus no explicit interrupt parent."
819 intr_pic_claim_root(device_t dev, intptr_t xref, intr_irq_filter_t *filter,
820 void *arg, u_int ipicount)
822 struct intr_pic *pic;
824 pic = pic_lookup(dev, xref);
826 device_printf(dev, "not registered\n");
830 KASSERT((pic->pic_flags & FLAG_PIC) != 0,
831 ("%s: Found a non-PIC controller: %s", __func__,
832 device_get_name(pic->pic_dev)));
834 if (filter == NULL) {
835 device_printf(dev, "filter missing\n");
840 * Only one interrupt controllers could be on the root for now.
841 * Note that we further suppose that there is not threaded interrupt
842 * routine (handler) on the root. See intr_irq_handler().
844 if (intr_irq_root_dev != NULL) {
845 device_printf(dev, "another root already set\n");
849 intr_irq_root_dev = dev;
850 irq_root_filter = filter;
852 irq_root_ipicount = ipicount;
854 debugf("irq root set to %s\n", device_get_nameunit(dev));
859 * Add a handler to manage a sub range of a parents interrupts.
862 intr_pic_add_handler(device_t parent, struct intr_pic *pic,
863 intr_child_irq_filter_t *filter, void *arg, uintptr_t start,
866 struct intr_pic *parent_pic;
867 struct intr_pic_child *newchild;
869 struct intr_pic_child *child;
872 parent_pic = pic_lookup(parent, 0);
873 if (parent_pic == NULL)
876 newchild = malloc(sizeof(*newchild), M_INTRNG, M_WAITOK | M_ZERO);
877 newchild->pc_pic = pic;
878 newchild->pc_filter = filter;
879 newchild->pc_filter_arg = arg;
880 newchild->pc_start = start;
881 newchild->pc_length = length;
883 mtx_lock_spin(&parent_pic->pic_child_lock);
885 SLIST_FOREACH(child, &parent_pic->pic_children, pc_next) {
886 KASSERT(child->pc_pic != pic, ("%s: Adding a child PIC twice",
890 SLIST_INSERT_HEAD(&parent_pic->pic_children, newchild, pc_next);
891 mtx_unlock_spin(&parent_pic->pic_child_lock);
897 intr_resolve_irq(device_t dev, intptr_t xref, struct intr_map_data *data,
898 struct intr_irqsrc **isrc)
900 struct intr_pic *pic;
901 struct intr_map_data_msi *msi;
906 pic = pic_lookup(dev, xref);
910 switch (data->type) {
911 case INTR_MAP_DATA_MSI:
912 KASSERT((pic->pic_flags & FLAG_MSI) != 0,
913 ("%s: Found a non-MSI controller: %s", __func__,
914 device_get_name(pic->pic_dev)));
915 msi = (struct intr_map_data_msi *)data;
920 KASSERT((pic->pic_flags & FLAG_PIC) != 0,
921 ("%s: Found a non-PIC controller: %s", __func__,
922 device_get_name(pic->pic_dev)));
923 return (PIC_MAP_INTR(pic->pic_dev, data, isrc));
929 intr_activate_irq(device_t dev, struct resource *res)
933 struct intr_map_data *data;
934 struct intr_irqsrc *isrc;
938 KASSERT(rman_get_start(res) == rman_get_end(res),
939 ("%s: more interrupts in resource", __func__));
941 res_id = (u_int)rman_get_start(res);
942 if (intr_map_get_isrc(res_id) != NULL)
943 panic("Attempt to double activation of resource id: %u\n",
945 intr_map_copy_map_data(res_id, &map_dev, &map_xref, &data);
946 error = intr_resolve_irq(map_dev, map_xref, data, &isrc);
948 free(data, M_INTRNG);
949 /* XXX TODO DISCONECTED PICs */
950 /* if (error == EINVAL) return(0); */
953 intr_map_set_isrc(res_id, isrc);
954 rman_set_virtual(res, data);
955 return (PIC_ACTIVATE_INTR(isrc->isrc_dev, isrc, res, data));
959 intr_deactivate_irq(device_t dev, struct resource *res)
961 struct intr_map_data *data;
962 struct intr_irqsrc *isrc;
966 KASSERT(rman_get_start(res) == rman_get_end(res),
967 ("%s: more interrupts in resource", __func__));
969 res_id = (u_int)rman_get_start(res);
970 isrc = intr_map_get_isrc(res_id);
972 panic("Attempt to deactivate non-active resource id: %u\n",
975 data = rman_get_virtual(res);
976 error = PIC_DEACTIVATE_INTR(isrc->isrc_dev, isrc, res, data);
977 intr_map_set_isrc(res_id, NULL);
978 rman_set_virtual(res, NULL);
979 free(data, M_INTRNG);
984 intr_setup_irq(device_t dev, struct resource *res, driver_filter_t filt,
985 driver_intr_t hand, void *arg, int flags, void **cookiep)
988 struct intr_map_data *data;
989 struct intr_irqsrc *isrc;
993 KASSERT(rman_get_start(res) == rman_get_end(res),
994 ("%s: more interrupts in resource", __func__));
996 res_id = (u_int)rman_get_start(res);
997 isrc = intr_map_get_isrc(res_id);
999 /* XXX TODO DISCONECTED PICs */
1003 data = rman_get_virtual(res);
1004 name = device_get_nameunit(dev);
1008 * Standard handling is done through MI interrupt framework. However,
1009 * some interrupts could request solely own special handling. This
1010 * non standard handling can be used for interrupt controllers without
1011 * handler (filter only), so in case that interrupt controllers are
1012 * chained, MI interrupt framework is called only in leaf controller.
1014 * Note that root interrupt controller routine is served as well,
1015 * however in intr_irq_handler(), i.e. main system dispatch routine.
1017 if (flags & INTR_SOLO && hand != NULL) {
1018 debugf("irq %u cannot solo on %s\n", irq, name);
1022 if (flags & INTR_SOLO) {
1023 error = iscr_setup_filter(isrc, name, (intr_irq_filter_t *)filt,
1025 debugf("irq %u setup filter error %d on %s\n", irq, error,
1030 error = isrc_add_handler(isrc, name, filt, hand, arg, flags,
1032 debugf("irq %u add handler error %d on %s\n", irq, error, name);
1037 mtx_lock(&isrc_table_lock);
1038 error = PIC_SETUP_INTR(isrc->isrc_dev, isrc, res, data);
1040 isrc->isrc_handlers++;
1041 if (isrc->isrc_handlers == 1)
1042 PIC_ENABLE_INTR(isrc->isrc_dev, isrc);
1044 mtx_unlock(&isrc_table_lock);
1046 intr_event_remove_handler(*cookiep);
1051 intr_teardown_irq(device_t dev, struct resource *res, void *cookie)
1054 struct intr_map_data *data;
1055 struct intr_irqsrc *isrc;
1058 KASSERT(rman_get_start(res) == rman_get_end(res),
1059 ("%s: more interrupts in resource", __func__));
1061 res_id = (u_int)rman_get_start(res);
1062 isrc = intr_map_get_isrc(res_id);
1063 if (isrc == NULL || isrc->isrc_handlers == 0)
1066 data = rman_get_virtual(res);
1069 if (isrc->isrc_filter != NULL) {
1073 mtx_lock(&isrc_table_lock);
1074 isrc->isrc_filter = NULL;
1075 isrc->isrc_arg = NULL;
1076 isrc->isrc_handlers = 0;
1077 PIC_DISABLE_INTR(isrc->isrc_dev, isrc);
1078 PIC_TEARDOWN_INTR(isrc->isrc_dev, isrc, res, data);
1079 isrc_update_name(isrc, NULL);
1080 mtx_unlock(&isrc_table_lock);
1084 if (isrc != intr_handler_source(cookie))
1087 error = intr_event_remove_handler(cookie);
1089 mtx_lock(&isrc_table_lock);
1090 isrc->isrc_handlers--;
1091 if (isrc->isrc_handlers == 0)
1092 PIC_DISABLE_INTR(isrc->isrc_dev, isrc);
1093 PIC_TEARDOWN_INTR(isrc->isrc_dev, isrc, res, data);
1094 intrcnt_updatename(isrc);
1095 mtx_unlock(&isrc_table_lock);
1101 intr_describe_irq(device_t dev, struct resource *res, void *cookie,
1105 struct intr_irqsrc *isrc;
1108 KASSERT(rman_get_start(res) == rman_get_end(res),
1109 ("%s: more interrupts in resource", __func__));
1111 res_id = (u_int)rman_get_start(res);
1112 isrc = intr_map_get_isrc(res_id);
1113 if (isrc == NULL || isrc->isrc_handlers == 0)
1116 if (isrc->isrc_filter != NULL) {
1120 mtx_lock(&isrc_table_lock);
1121 isrc_update_name(isrc, descr);
1122 mtx_unlock(&isrc_table_lock);
1126 error = intr_event_describe_handler(isrc->isrc_event, cookie, descr);
1128 mtx_lock(&isrc_table_lock);
1129 intrcnt_updatename(isrc);
1130 mtx_unlock(&isrc_table_lock);
1137 intr_bind_irq(device_t dev, struct resource *res, int cpu)
1139 struct intr_irqsrc *isrc;
1142 KASSERT(rman_get_start(res) == rman_get_end(res),
1143 ("%s: more interrupts in resource", __func__));
1145 res_id = (u_int)rman_get_start(res);
1146 isrc = intr_map_get_isrc(res_id);
1147 if (isrc == NULL || isrc->isrc_handlers == 0)
1150 if (isrc->isrc_filter != NULL)
1151 return (intr_isrc_assign_cpu(isrc, cpu));
1153 return (intr_event_bind(isrc->isrc_event, cpu));
1157 * Return the CPU that the next interrupt source should use.
1158 * For now just returns the next CPU according to round-robin.
1161 intr_irq_next_cpu(u_int last_cpu, cpuset_t *cpumask)
1164 if (!irq_assign_cpu || mp_ncpus == 1)
1165 return (PCPU_GET(cpuid));
1169 if (last_cpu > mp_maxid)
1171 } while (!CPU_ISSET(last_cpu, cpumask));
1176 * Distribute all the interrupt sources among the available
1177 * CPUs once the AP's have been launched.
1180 intr_irq_shuffle(void *arg __unused)
1182 struct intr_irqsrc *isrc;
1188 mtx_lock(&isrc_table_lock);
1189 irq_assign_cpu = TRUE;
1190 for (i = 0; i < NIRQ; i++) {
1191 isrc = irq_sources[i];
1192 if (isrc == NULL || isrc->isrc_handlers == 0 ||
1193 isrc->isrc_flags & (INTR_ISRCF_PPI | INTR_ISRCF_IPI))
1196 if (isrc->isrc_event != NULL &&
1197 isrc->isrc_flags & INTR_ISRCF_BOUND &&
1198 isrc->isrc_event->ie_cpu != CPU_FFS(&isrc->isrc_cpu) - 1)
1199 panic("%s: CPU inconsistency", __func__);
1201 if ((isrc->isrc_flags & INTR_ISRCF_BOUND) == 0)
1202 CPU_ZERO(&isrc->isrc_cpu); /* start again */
1205 * We are in wicked position here if the following call fails
1206 * for bound ISRC. The best thing we can do is to clear
1207 * isrc_cpu so inconsistency with ie_cpu will be detectable.
1209 if (PIC_BIND_INTR(isrc->isrc_dev, isrc) != 0)
1210 CPU_ZERO(&isrc->isrc_cpu);
1212 mtx_unlock(&isrc_table_lock);
1214 SYSINIT(intr_irq_shuffle, SI_SUB_SMP, SI_ORDER_SECOND, intr_irq_shuffle, NULL);
1218 intr_irq_next_cpu(u_int current_cpu, cpuset_t *cpumask)
1221 return (PCPU_GET(cpuid));
1226 * Allocate memory for new intr_map_data structure.
1227 * Initialize common fields.
1229 struct intr_map_data *
1230 intr_alloc_map_data(enum intr_map_data_type type, size_t len, int flags)
1232 struct intr_map_data *data;
1234 data = malloc(len, M_INTRNG, flags);
1240 void intr_free_intr_map_data(struct intr_map_data *data)
1243 free(data, M_INTRNG);
1248 * Register a MSI/MSI-X interrupt controller
1251 intr_msi_register(device_t dev, intptr_t xref)
1253 struct intr_pic *pic;
1257 pic = pic_create(dev, xref);
1261 pic->pic_flags |= FLAG_MSI;
1263 debugf("PIC %p registered for %s <dev %p, xref %jx>\n", pic,
1264 device_get_nameunit(dev), dev, (uintmax_t)xref);
1269 intr_alloc_msi(device_t pci, device_t child, intptr_t xref, int count,
1270 int maxcount, int *irqs)
1272 struct intr_irqsrc **isrc;
1273 struct intr_pic *pic;
1275 struct intr_map_data_msi *msi;
1278 pic = pic_lookup(NULL, xref);
1282 KASSERT((pic->pic_flags & FLAG_MSI) != 0,
1283 ("%s: Found a non-MSI controller: %s", __func__,
1284 device_get_name(pic->pic_dev)));
1286 isrc = malloc(sizeof(*isrc) * count, M_INTRNG, M_WAITOK);
1287 err = MSI_ALLOC_MSI(pic->pic_dev, child, count, maxcount, &pdev, isrc);
1289 free(isrc, M_INTRNG);
1293 for (i = 0; i < count; i++) {
1294 msi = (struct intr_map_data_msi *)intr_alloc_map_data(
1295 INTR_MAP_DATA_MSI, sizeof(*msi), M_WAITOK | M_ZERO);
1296 msi-> isrc = isrc[i];
1297 irqs[i] = intr_map_irq(pic->pic_dev, xref,
1298 (struct intr_map_data *)msi);
1301 free(isrc, M_INTRNG);
1307 intr_release_msi(device_t pci, device_t child, intptr_t xref, int count,
1310 struct intr_irqsrc **isrc;
1311 struct intr_pic *pic;
1314 pic = pic_lookup(NULL, xref);
1318 KASSERT((pic->pic_flags & FLAG_MSI) != 0,
1319 ("%s: Found a non-MSI controller: %s", __func__,
1320 device_get_name(pic->pic_dev)));
1322 isrc = malloc(sizeof(*isrc) * count, M_INTRNG, M_WAITOK);
1324 for (i = 0; i < count; i++)
1325 isrc[i] = intr_map_get_isrc(irqs[i]);
1327 err = MSI_RELEASE_MSI(pic->pic_dev, child, count, isrc);
1329 for (i = 0; i < count; i++) {
1330 if (isrc[i] != NULL)
1331 intr_unmap_irq(irqs[i]);
1334 free(isrc, M_INTRNG);
1339 intr_alloc_msix(device_t pci, device_t child, intptr_t xref, int *irq)
1341 struct intr_irqsrc *isrc;
1342 struct intr_pic *pic;
1344 struct intr_map_data_msi *msi;
1347 pic = pic_lookup(NULL, xref);
1351 KASSERT((pic->pic_flags & FLAG_MSI) != 0,
1352 ("%s: Found a non-MSI controller: %s", __func__,
1353 device_get_name(pic->pic_dev)));
1356 err = MSI_ALLOC_MSIX(pic->pic_dev, child, &pdev, &isrc);
1360 msi = (struct intr_map_data_msi *)intr_alloc_map_data(
1361 INTR_MAP_DATA_MSI, sizeof(*msi), M_WAITOK | M_ZERO);
1363 *irq = intr_map_irq(pic->pic_dev, xref, (struct intr_map_data *)msi);
1368 intr_release_msix(device_t pci, device_t child, intptr_t xref, int irq)
1370 struct intr_irqsrc *isrc;
1371 struct intr_pic *pic;
1374 pic = pic_lookup(NULL, xref);
1378 KASSERT((pic->pic_flags & FLAG_MSI) != 0,
1379 ("%s: Found a non-MSI controller: %s", __func__,
1380 device_get_name(pic->pic_dev)));
1382 isrc = intr_map_get_isrc(irq);
1384 intr_unmap_irq(irq);
1388 err = MSI_RELEASE_MSIX(pic->pic_dev, child, isrc);
1389 intr_unmap_irq(irq);
1395 intr_map_msi(device_t pci, device_t child, intptr_t xref, int irq,
1396 uint64_t *addr, uint32_t *data)
1398 struct intr_irqsrc *isrc;
1399 struct intr_pic *pic;
1402 pic = pic_lookup(NULL, xref);
1406 KASSERT((pic->pic_flags & FLAG_MSI) != 0,
1407 ("%s: Found a non-MSI controller: %s", __func__,
1408 device_get_name(pic->pic_dev)));
1410 isrc = intr_map_get_isrc(irq);
1414 err = MSI_MAP_MSI(pic->pic_dev, child, isrc, addr, data);
1419 void dosoftints(void);
1427 * Init interrupt controller on another CPU.
1430 intr_pic_init_secondary(void)
1434 * QQQ: Only root PIC is aware of other CPUs ???
1436 KASSERT(intr_irq_root_dev != NULL, ("%s: no root attached", __func__));
1438 //mtx_lock(&isrc_table_lock);
1439 PIC_INIT_SECONDARY(intr_irq_root_dev);
1440 //mtx_unlock(&isrc_table_lock);
1445 DB_SHOW_COMMAND(irqs, db_show_irqs)
1449 struct intr_irqsrc *isrc;
1451 for (irqsum = 0, i = 0; i < NIRQ; i++) {
1452 isrc = irq_sources[i];
1456 num = isrc->isrc_count != NULL ? isrc->isrc_count[0] : 0;
1457 db_printf("irq%-3u <%s>: cpu %02lx%s cnt %lu\n", i,
1458 isrc->isrc_name, isrc->isrc_cpu.__bits[0],
1459 isrc->isrc_flags & INTR_ISRCF_BOUND ? " (bound)" : "", num);
1462 db_printf("irq total %u\n", irqsum);
1467 * Interrupt mapping table functions.
1469 * Please, keep this part separately, it can be transformed to
1470 * extension of standard resources.
1472 struct intr_map_entry
1476 struct intr_map_data *map_data;
1477 struct intr_irqsrc *isrc;
1478 /* XXX TODO DISCONECTED PICs */
1482 /* XXX Convert irq_map[] to dynamicaly expandable one. */
1483 static struct intr_map_entry *irq_map[2 * NIRQ];
1484 static int irq_map_count = nitems(irq_map);
1485 static int irq_map_first_free_idx;
1486 static struct mtx irq_map_lock;
1488 static struct intr_irqsrc *
1489 intr_map_get_isrc(u_int res_id)
1491 struct intr_irqsrc *isrc;
1493 mtx_lock(&irq_map_lock);
1494 if ((res_id >= irq_map_count) || (irq_map[res_id] == NULL)) {
1495 mtx_unlock(&irq_map_lock);
1498 isrc = irq_map[res_id]->isrc;
1499 mtx_unlock(&irq_map_lock);
1504 intr_map_set_isrc(u_int res_id, struct intr_irqsrc *isrc)
1507 mtx_lock(&irq_map_lock);
1508 if ((res_id >= irq_map_count) || (irq_map[res_id] == NULL)) {
1509 mtx_unlock(&irq_map_lock);
1512 irq_map[res_id]->isrc = isrc;
1513 mtx_unlock(&irq_map_lock);
1517 * Get a copy of intr_map_entry data
1520 intr_map_copy_map_data(u_int res_id, device_t *map_dev, intptr_t *map_xref,
1521 struct intr_map_data **data)
1526 mtx_lock(&irq_map_lock);
1527 if (res_id >= irq_map_count || irq_map[res_id] == NULL)
1528 panic("Attempt to copy invalid resource id: %u\n", res_id);
1529 if (irq_map[res_id]->map_data != NULL)
1530 len = irq_map[res_id]->map_data->len;
1531 mtx_unlock(&irq_map_lock);
1536 *data = malloc(len, M_INTRNG, M_WAITOK | M_ZERO);
1537 mtx_lock(&irq_map_lock);
1538 if (irq_map[res_id] == NULL)
1539 panic("Attempt to copy invalid resource id: %u\n", res_id);
1541 if (len != irq_map[res_id]->map_data->len)
1542 panic("Resource id: %u has changed.\n", res_id);
1543 memcpy(*data, irq_map[res_id]->map_data, len);
1545 *map_dev = irq_map[res_id]->dev;
1546 *map_xref = irq_map[res_id]->xref;
1547 mtx_unlock(&irq_map_lock);
1552 * Allocate and fill new entry in irq_map table.
1555 intr_map_irq(device_t dev, intptr_t xref, struct intr_map_data *data)
1558 struct intr_map_entry *entry;
1560 /* Prepare new entry first. */
1561 entry = malloc(sizeof(*entry), M_INTRNG, M_WAITOK | M_ZERO);
1565 entry->map_data = data;
1568 mtx_lock(&irq_map_lock);
1569 for (i = irq_map_first_free_idx; i < irq_map_count; i++) {
1570 if (irq_map[i] == NULL) {
1572 irq_map_first_free_idx = i + 1;
1573 mtx_unlock(&irq_map_lock);
1577 mtx_unlock(&irq_map_lock);
1579 /* XXX Expand irq_map table */
1580 panic("IRQ mapping table is full.");
1584 * Remove and free mapping entry.
1587 intr_unmap_irq(u_int res_id)
1589 struct intr_map_entry *entry;
1591 mtx_lock(&irq_map_lock);
1592 if ((res_id >= irq_map_count) || (irq_map[res_id] == NULL))
1593 panic("Attempt to unmap invalid resource id: %u\n", res_id);
1594 entry = irq_map[res_id];
1595 irq_map[res_id] = NULL;
1596 irq_map_first_free_idx = res_id;
1597 mtx_unlock(&irq_map_lock);
1598 intr_free_intr_map_data(entry->map_data);
1599 free(entry, M_INTRNG);
1603 * Clone mapping entry.
1606 intr_map_clone_irq(u_int old_res_id)
1610 struct intr_map_data *data;
1612 intr_map_copy_map_data(old_res_id, &map_dev, &map_xref, &data);
1613 return (intr_map_irq(map_dev, map_xref, data));
1617 intr_map_init(void *dummy __unused)
1620 mtx_init(&irq_map_lock, "intr map table", NULL, MTX_DEF);
1622 SYSINIT(intr_map_init, SI_SUB_INTR, SI_ORDER_FIRST, intr_map_init, NULL);