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));
944 intr_activate_irq(device_t dev, struct resource *res)
948 struct intr_map_data *data;
949 struct intr_irqsrc *isrc;
953 KASSERT(rman_get_start(res) == rman_get_end(res),
954 ("%s: more interrupts in resource", __func__));
956 res_id = (u_int)rman_get_start(res);
957 if (intr_map_get_isrc(res_id) != NULL)
958 panic("Attempt to double activation of resource id: %u\n",
960 intr_map_copy_map_data(res_id, &map_dev, &map_xref, &data);
961 error = intr_resolve_irq(map_dev, map_xref, data, &isrc);
963 free(data, M_INTRNG);
964 /* XXX TODO DISCONECTED PICs */
965 /* if (error == EINVAL) return(0); */
968 intr_map_set_isrc(res_id, isrc);
969 rman_set_virtual(res, data);
970 return (PIC_ACTIVATE_INTR(isrc->isrc_dev, isrc, res, data));
974 intr_deactivate_irq(device_t dev, struct resource *res)
976 struct intr_map_data *data;
977 struct intr_irqsrc *isrc;
981 KASSERT(rman_get_start(res) == rman_get_end(res),
982 ("%s: more interrupts in resource", __func__));
984 res_id = (u_int)rman_get_start(res);
985 isrc = intr_map_get_isrc(res_id);
987 panic("Attempt to deactivate non-active resource id: %u\n",
990 data = rman_get_virtual(res);
991 error = PIC_DEACTIVATE_INTR(isrc->isrc_dev, isrc, res, data);
992 intr_map_set_isrc(res_id, NULL);
993 rman_set_virtual(res, NULL);
994 free(data, M_INTRNG);
999 intr_setup_irq(device_t dev, struct resource *res, driver_filter_t filt,
1000 driver_intr_t hand, void *arg, int flags, void **cookiep)
1003 struct intr_map_data *data;
1004 struct intr_irqsrc *isrc;
1008 KASSERT(rman_get_start(res) == rman_get_end(res),
1009 ("%s: more interrupts in resource", __func__));
1011 res_id = (u_int)rman_get_start(res);
1012 isrc = intr_map_get_isrc(res_id);
1014 /* XXX TODO DISCONECTED PICs */
1018 data = rman_get_virtual(res);
1019 name = device_get_nameunit(dev);
1023 * Standard handling is done through MI interrupt framework. However,
1024 * some interrupts could request solely own special handling. This
1025 * non standard handling can be used for interrupt controllers without
1026 * handler (filter only), so in case that interrupt controllers are
1027 * chained, MI interrupt framework is called only in leaf controller.
1029 * Note that root interrupt controller routine is served as well,
1030 * however in intr_irq_handler(), i.e. main system dispatch routine.
1032 if (flags & INTR_SOLO && hand != NULL) {
1033 debugf("irq %u cannot solo on %s\n", irq, name);
1037 if (flags & INTR_SOLO) {
1038 error = iscr_setup_filter(isrc, name, (intr_irq_filter_t *)filt,
1040 debugf("irq %u setup filter error %d on %s\n", isrc->isrc_irq, error,
1045 error = isrc_add_handler(isrc, name, filt, hand, arg, flags,
1047 debugf("irq %u add handler error %d on %s\n", isrc->isrc_irq, error, name);
1052 mtx_lock(&isrc_table_lock);
1053 error = PIC_SETUP_INTR(isrc->isrc_dev, isrc, res, data);
1055 isrc->isrc_handlers++;
1056 if (isrc->isrc_handlers == 1)
1057 PIC_ENABLE_INTR(isrc->isrc_dev, isrc);
1059 mtx_unlock(&isrc_table_lock);
1061 intr_event_remove_handler(*cookiep);
1066 intr_teardown_irq(device_t dev, struct resource *res, void *cookie)
1069 struct intr_map_data *data;
1070 struct intr_irqsrc *isrc;
1073 KASSERT(rman_get_start(res) == rman_get_end(res),
1074 ("%s: more interrupts in resource", __func__));
1076 res_id = (u_int)rman_get_start(res);
1077 isrc = intr_map_get_isrc(res_id);
1078 if (isrc == NULL || isrc->isrc_handlers == 0)
1081 data = rman_get_virtual(res);
1084 if (isrc->isrc_filter != NULL) {
1088 mtx_lock(&isrc_table_lock);
1089 isrc->isrc_filter = NULL;
1090 isrc->isrc_arg = NULL;
1091 isrc->isrc_handlers = 0;
1092 PIC_DISABLE_INTR(isrc->isrc_dev, isrc);
1093 PIC_TEARDOWN_INTR(isrc->isrc_dev, isrc, res, data);
1094 isrc_update_name(isrc, NULL);
1095 mtx_unlock(&isrc_table_lock);
1099 if (isrc != intr_handler_source(cookie))
1102 error = intr_event_remove_handler(cookie);
1104 mtx_lock(&isrc_table_lock);
1105 isrc->isrc_handlers--;
1106 if (isrc->isrc_handlers == 0)
1107 PIC_DISABLE_INTR(isrc->isrc_dev, isrc);
1108 PIC_TEARDOWN_INTR(isrc->isrc_dev, isrc, res, data);
1109 intrcnt_updatename(isrc);
1110 mtx_unlock(&isrc_table_lock);
1116 intr_describe_irq(device_t dev, struct resource *res, void *cookie,
1120 struct intr_irqsrc *isrc;
1123 KASSERT(rman_get_start(res) == rman_get_end(res),
1124 ("%s: more interrupts in resource", __func__));
1126 res_id = (u_int)rman_get_start(res);
1127 isrc = intr_map_get_isrc(res_id);
1128 if (isrc == NULL || isrc->isrc_handlers == 0)
1131 if (isrc->isrc_filter != NULL) {
1135 mtx_lock(&isrc_table_lock);
1136 isrc_update_name(isrc, descr);
1137 mtx_unlock(&isrc_table_lock);
1141 error = intr_event_describe_handler(isrc->isrc_event, cookie, descr);
1143 mtx_lock(&isrc_table_lock);
1144 intrcnt_updatename(isrc);
1145 mtx_unlock(&isrc_table_lock);
1152 intr_bind_irq(device_t dev, struct resource *res, int cpu)
1154 struct intr_irqsrc *isrc;
1157 KASSERT(rman_get_start(res) == rman_get_end(res),
1158 ("%s: more interrupts in resource", __func__));
1160 res_id = (u_int)rman_get_start(res);
1161 isrc = intr_map_get_isrc(res_id);
1162 if (isrc == NULL || isrc->isrc_handlers == 0)
1165 if (isrc->isrc_filter != NULL)
1166 return (intr_isrc_assign_cpu(isrc, cpu));
1168 return (intr_event_bind(isrc->isrc_event, cpu));
1172 * Return the CPU that the next interrupt source should use.
1173 * For now just returns the next CPU according to round-robin.
1176 intr_irq_next_cpu(u_int last_cpu, cpuset_t *cpumask)
1180 KASSERT(!CPU_EMPTY(cpumask), ("%s: Empty CPU mask", __func__));
1181 if (!irq_assign_cpu || mp_ncpus == 1) {
1182 cpu = PCPU_GET(cpuid);
1184 if (CPU_ISSET(cpu, cpumask))
1187 return (CPU_FFS(cpumask) - 1);
1192 if (last_cpu > mp_maxid)
1194 } while (!CPU_ISSET(last_cpu, cpumask));
1198 #ifndef EARLY_AP_STARTUP
1200 * Distribute all the interrupt sources among the available
1201 * CPUs once the AP's have been launched.
1204 intr_irq_shuffle(void *arg __unused)
1206 struct intr_irqsrc *isrc;
1212 mtx_lock(&isrc_table_lock);
1213 irq_assign_cpu = true;
1214 for (i = 0; i < NIRQ; i++) {
1215 isrc = irq_sources[i];
1216 if (isrc == NULL || isrc->isrc_handlers == 0 ||
1217 isrc->isrc_flags & (INTR_ISRCF_PPI | INTR_ISRCF_IPI))
1220 if (isrc->isrc_event != NULL &&
1221 isrc->isrc_flags & INTR_ISRCF_BOUND &&
1222 isrc->isrc_event->ie_cpu != CPU_FFS(&isrc->isrc_cpu) - 1)
1223 panic("%s: CPU inconsistency", __func__);
1225 if ((isrc->isrc_flags & INTR_ISRCF_BOUND) == 0)
1226 CPU_ZERO(&isrc->isrc_cpu); /* start again */
1229 * We are in wicked position here if the following call fails
1230 * for bound ISRC. The best thing we can do is to clear
1231 * isrc_cpu so inconsistency with ie_cpu will be detectable.
1233 if (PIC_BIND_INTR(isrc->isrc_dev, isrc) != 0)
1234 CPU_ZERO(&isrc->isrc_cpu);
1236 mtx_unlock(&isrc_table_lock);
1238 SYSINIT(intr_irq_shuffle, SI_SUB_SMP, SI_ORDER_SECOND, intr_irq_shuffle, NULL);
1239 #endif /* !EARLY_AP_STARTUP */
1243 intr_irq_next_cpu(u_int current_cpu, cpuset_t *cpumask)
1246 return (PCPU_GET(cpuid));
1251 * Allocate memory for new intr_map_data structure.
1252 * Initialize common fields.
1254 struct intr_map_data *
1255 intr_alloc_map_data(enum intr_map_data_type type, size_t len, int flags)
1257 struct intr_map_data *data;
1259 data = malloc(len, M_INTRNG, flags);
1265 void intr_free_intr_map_data(struct intr_map_data *data)
1268 free(data, M_INTRNG);
1272 * Register a MSI/MSI-X interrupt controller
1275 intr_msi_register(device_t dev, intptr_t xref)
1277 struct intr_pic *pic;
1281 pic = pic_create(dev, xref, FLAG_MSI);
1285 debugf("PIC %p registered for %s <dev %p, xref %jx>\n", pic,
1286 device_get_nameunit(dev), dev, (uintmax_t)xref);
1291 intr_alloc_msi(device_t pci, device_t child, intptr_t xref, int count,
1292 int maxcount, int *irqs)
1294 struct intr_irqsrc **isrc;
1295 struct intr_pic *pic;
1297 struct intr_map_data_msi *msi;
1300 pic = pic_lookup(NULL, xref, FLAG_MSI);
1304 KASSERT((pic->pic_flags & FLAG_TYPE_MASK) == FLAG_MSI,
1305 ("%s: Found a non-MSI controller: %s", __func__,
1306 device_get_name(pic->pic_dev)));
1308 isrc = malloc(sizeof(*isrc) * count, M_INTRNG, M_WAITOK);
1309 err = MSI_ALLOC_MSI(pic->pic_dev, child, count, maxcount, &pdev, isrc);
1311 free(isrc, M_INTRNG);
1315 for (i = 0; i < count; i++) {
1316 msi = (struct intr_map_data_msi *)intr_alloc_map_data(
1317 INTR_MAP_DATA_MSI, sizeof(*msi), M_WAITOK | M_ZERO);
1318 msi-> isrc = isrc[i];
1319 irqs[i] = intr_map_irq(pic->pic_dev, xref,
1320 (struct intr_map_data *)msi);
1323 free(isrc, M_INTRNG);
1329 intr_release_msi(device_t pci, device_t child, intptr_t xref, int count,
1332 struct intr_irqsrc **isrc;
1333 struct intr_pic *pic;
1334 struct intr_map_data_msi *msi;
1337 pic = pic_lookup(NULL, xref, FLAG_MSI);
1341 KASSERT((pic->pic_flags & FLAG_TYPE_MASK) == FLAG_MSI,
1342 ("%s: Found a non-MSI controller: %s", __func__,
1343 device_get_name(pic->pic_dev)));
1345 isrc = malloc(sizeof(*isrc) * count, M_INTRNG, M_WAITOK);
1347 for (i = 0; i < count; i++) {
1348 msi = (struct intr_map_data_msi *)
1349 intr_map_get_map_data(irqs[i]);
1350 KASSERT(msi->hdr.type == INTR_MAP_DATA_MSI,
1351 ("%s: irq %d map data is not MSI", __func__,
1353 isrc[i] = msi->isrc;
1356 err = MSI_RELEASE_MSI(pic->pic_dev, child, count, isrc);
1358 for (i = 0; i < count; i++) {
1359 if (isrc[i] != NULL)
1360 intr_unmap_irq(irqs[i]);
1363 free(isrc, M_INTRNG);
1368 intr_alloc_msix(device_t pci, device_t child, intptr_t xref, int *irq)
1370 struct intr_irqsrc *isrc;
1371 struct intr_pic *pic;
1373 struct intr_map_data_msi *msi;
1376 pic = pic_lookup(NULL, xref, FLAG_MSI);
1380 KASSERT((pic->pic_flags & FLAG_TYPE_MASK) == FLAG_MSI,
1381 ("%s: Found a non-MSI controller: %s", __func__,
1382 device_get_name(pic->pic_dev)));
1384 err = MSI_ALLOC_MSIX(pic->pic_dev, child, &pdev, &isrc);
1388 msi = (struct intr_map_data_msi *)intr_alloc_map_data(
1389 INTR_MAP_DATA_MSI, sizeof(*msi), M_WAITOK | M_ZERO);
1391 *irq = intr_map_irq(pic->pic_dev, xref, (struct intr_map_data *)msi);
1396 intr_release_msix(device_t pci, device_t child, intptr_t xref, int irq)
1398 struct intr_irqsrc *isrc;
1399 struct intr_pic *pic;
1400 struct intr_map_data_msi *msi;
1403 pic = pic_lookup(NULL, xref, FLAG_MSI);
1407 KASSERT((pic->pic_flags & FLAG_TYPE_MASK) == FLAG_MSI,
1408 ("%s: Found a non-MSI controller: %s", __func__,
1409 device_get_name(pic->pic_dev)));
1411 msi = (struct intr_map_data_msi *)
1412 intr_map_get_map_data(irq);
1413 KASSERT(msi->hdr.type == INTR_MAP_DATA_MSI,
1414 ("%s: irq %d map data is not MSI", __func__,
1418 intr_unmap_irq(irq);
1422 err = MSI_RELEASE_MSIX(pic->pic_dev, child, isrc);
1423 intr_unmap_irq(irq);
1429 intr_map_msi(device_t pci, device_t child, intptr_t xref, int irq,
1430 uint64_t *addr, uint32_t *data)
1432 struct intr_irqsrc *isrc;
1433 struct intr_pic *pic;
1436 pic = pic_lookup(NULL, xref, FLAG_MSI);
1440 KASSERT((pic->pic_flags & FLAG_TYPE_MASK) == FLAG_MSI,
1441 ("%s: Found a non-MSI controller: %s", __func__,
1442 device_get_name(pic->pic_dev)));
1444 isrc = intr_map_get_isrc(irq);
1448 err = MSI_MAP_MSI(pic->pic_dev, child, isrc, addr, data);
1452 void dosoftints(void);
1460 * Init interrupt controller on another CPU.
1463 intr_pic_init_secondary(void)
1467 * QQQ: Only root PIC is aware of other CPUs ???
1469 KASSERT(intr_irq_root_dev != NULL, ("%s: no root attached", __func__));
1471 //mtx_lock(&isrc_table_lock);
1472 PIC_INIT_SECONDARY(intr_irq_root_dev);
1473 //mtx_unlock(&isrc_table_lock);
1478 DB_SHOW_COMMAND(irqs, db_show_irqs)
1482 struct intr_irqsrc *isrc;
1484 for (irqsum = 0, i = 0; i < NIRQ; i++) {
1485 isrc = irq_sources[i];
1489 num = isrc->isrc_count != NULL ? isrc->isrc_count[0] : 0;
1490 db_printf("irq%-3u <%s>: cpu %02lx%s cnt %lu\n", i,
1491 isrc->isrc_name, isrc->isrc_cpu.__bits[0],
1492 isrc->isrc_flags & INTR_ISRCF_BOUND ? " (bound)" : "", num);
1495 db_printf("irq total %u\n", irqsum);
1500 * Interrupt mapping table functions.
1502 * Please, keep this part separately, it can be transformed to
1503 * extension of standard resources.
1505 struct intr_map_entry
1509 struct intr_map_data *map_data;
1510 struct intr_irqsrc *isrc;
1511 /* XXX TODO DISCONECTED PICs */
1515 /* XXX Convert irq_map[] to dynamicaly expandable one. */
1516 static struct intr_map_entry *irq_map[2 * NIRQ];
1517 static int irq_map_count = nitems(irq_map);
1518 static int irq_map_first_free_idx;
1519 static struct mtx irq_map_lock;
1521 static struct intr_irqsrc *
1522 intr_map_get_isrc(u_int res_id)
1524 struct intr_irqsrc *isrc;
1527 mtx_lock(&irq_map_lock);
1528 if (res_id < irq_map_count && irq_map[res_id] != NULL)
1529 isrc = irq_map[res_id]->isrc;
1530 mtx_unlock(&irq_map_lock);
1536 intr_map_set_isrc(u_int res_id, struct intr_irqsrc *isrc)
1539 mtx_lock(&irq_map_lock);
1540 if (res_id < irq_map_count && irq_map[res_id] != NULL)
1541 irq_map[res_id]->isrc = isrc;
1542 mtx_unlock(&irq_map_lock);
1546 * Get a copy of intr_map_entry data
1548 static struct intr_map_data *
1549 intr_map_get_map_data(u_int res_id)
1551 struct intr_map_data *data;
1554 mtx_lock(&irq_map_lock);
1555 if (res_id >= irq_map_count || irq_map[res_id] == NULL)
1556 panic("Attempt to copy invalid resource id: %u\n", res_id);
1557 data = irq_map[res_id]->map_data;
1558 mtx_unlock(&irq_map_lock);
1564 * Get a copy of intr_map_entry data
1567 intr_map_copy_map_data(u_int res_id, device_t *map_dev, intptr_t *map_xref,
1568 struct intr_map_data **data)
1573 mtx_lock(&irq_map_lock);
1574 if (res_id >= irq_map_count || irq_map[res_id] == NULL)
1575 panic("Attempt to copy invalid resource id: %u\n", res_id);
1576 if (irq_map[res_id]->map_data != NULL)
1577 len = irq_map[res_id]->map_data->len;
1578 mtx_unlock(&irq_map_lock);
1583 *data = malloc(len, M_INTRNG, M_WAITOK | M_ZERO);
1584 mtx_lock(&irq_map_lock);
1585 if (irq_map[res_id] == NULL)
1586 panic("Attempt to copy invalid resource id: %u\n", res_id);
1588 if (len != irq_map[res_id]->map_data->len)
1589 panic("Resource id: %u has changed.\n", res_id);
1590 memcpy(*data, irq_map[res_id]->map_data, len);
1592 *map_dev = irq_map[res_id]->dev;
1593 *map_xref = irq_map[res_id]->xref;
1594 mtx_unlock(&irq_map_lock);
1598 * Allocate and fill new entry in irq_map table.
1601 intr_map_irq(device_t dev, intptr_t xref, struct intr_map_data *data)
1604 struct intr_map_entry *entry;
1606 /* Prepare new entry first. */
1607 entry = malloc(sizeof(*entry), M_INTRNG, M_WAITOK | M_ZERO);
1611 entry->map_data = data;
1614 mtx_lock(&irq_map_lock);
1615 for (i = irq_map_first_free_idx; i < irq_map_count; i++) {
1616 if (irq_map[i] == NULL) {
1618 irq_map_first_free_idx = i + 1;
1619 mtx_unlock(&irq_map_lock);
1623 mtx_unlock(&irq_map_lock);
1625 /* XXX Expand irq_map table */
1626 panic("IRQ mapping table is full.");
1630 * Remove and free mapping entry.
1633 intr_unmap_irq(u_int res_id)
1635 struct intr_map_entry *entry;
1637 mtx_lock(&irq_map_lock);
1638 if ((res_id >= irq_map_count) || (irq_map[res_id] == NULL))
1639 panic("Attempt to unmap invalid resource id: %u\n", res_id);
1640 entry = irq_map[res_id];
1641 irq_map[res_id] = NULL;
1642 irq_map_first_free_idx = res_id;
1643 mtx_unlock(&irq_map_lock);
1644 intr_free_intr_map_data(entry->map_data);
1645 free(entry, M_INTRNG);
1649 * Clone mapping entry.
1652 intr_map_clone_irq(u_int old_res_id)
1656 struct intr_map_data *data;
1658 intr_map_copy_map_data(old_res_id, &map_dev, &map_xref, &data);
1659 return (intr_map_irq(map_dev, map_xref, data));
1663 intr_map_init(void *dummy __unused)
1666 mtx_init(&irq_map_lock, "intr map table", NULL, MTX_DEF);
1668 SYSINIT(intr_map_init, SI_SUB_INTR, SI_ORDER_FIRST, intr_map_init, NULL);