1 /*- 2 * Copyright (c) 2015-2016 Svatopluk Kraus 3 * Copyright (c) 2015-2016 Michal Meloun 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 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. 14 * 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 25 * SUCH DAMAGE. 26 */ 27 28 #include <sys/cdefs.h> 29 __FBSDID("$FreeBSD$"); 30 31 /* 32 * New-style Interrupt Framework 33 * 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. 37 */ 38 39 #include "opt_ddb.h" 40 #include "opt_hwpmc_hooks.h" 41 42 #include <sys/param.h> 43 #include <sys/systm.h> 44 #include <sys/kernel.h> 45 #include <sys/lock.h> 46 #include <sys/mutex.h> 47 #include <sys/syslog.h> 48 #include <sys/malloc.h> 49 #include <sys/proc.h> 50 #include <sys/queue.h> 51 #include <sys/bus.h> 52 #include <sys/interrupt.h> 53 #include <sys/conf.h> 54 #include <sys/cpuset.h> 55 #include <sys/rman.h> 56 #include <sys/sched.h> 57 #include <sys/smp.h> 58 #include <sys/vmmeter.h> 59 #ifdef HWPMC_HOOKS 60 #include <sys/pmckern.h> 61 #endif 62 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> 68 69 #ifdef DDB 70 #include <ddb/ddb.h> 71 #endif 72 73 #include "pic_if.h" 74 #include "msi_if.h" 75 76 #define INTRNAME_LEN (2*MAXCOMLEN + 1) 77 78 #ifdef DEBUG 79 #define debugf(fmt, args...) do { printf("%s(): ", __func__); \ 80 printf(fmt,##args); } while (0) 81 #else 82 #define debugf(fmt, args...) 83 #endif 84 85 MALLOC_DECLARE(M_INTRNG); 86 MALLOC_DEFINE(M_INTRNG, "intr", "intr interrupt handling"); 87 88 /* Main interrupt handler called from assembler -> 'hidden' for C code. */ 89 void intr_irq_handler(struct trapframe *tf); 90 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; 96 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; 101 void *pc_filter_arg; 102 uintptr_t pc_start; 103 uintptr_t pc_length; 104 }; 105 106 /* Interrupt controller definition. */ 107 struct intr_pic { 108 SLIST_ENTRY(intr_pic) pic_next; 109 intptr_t pic_xref; /* hardware identification */ 110 device_t pic_dev; 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) 115 u_int pic_flags; 116 struct mtx pic_child_lock; 117 SLIST_HEAD(, intr_pic_child) pic_children; 118 }; 119 120 static struct mtx pic_list_lock; 121 static SLIST_HEAD(, intr_pic) pic_list; 122 123 static struct intr_pic *pic_lookup(device_t dev, intptr_t xref, int flags); 124 125 /* Interrupt source definition. */ 126 static struct mtx isrc_table_lock; 127 static struct intr_irqsrc *irq_sources[NIRQ]; 128 u_int irq_next_free; 129 130 #ifdef SMP 131 #ifdef EARLY_AP_STARTUP 132 static bool irq_assign_cpu = true; 133 #else 134 static bool irq_assign_cpu = false; 135 #endif 136 #endif 137 138 /* 139 * - 2 counters for each I/O interrupt. 140 * - MAXCPU counters for each IPI counters for SMP. 141 */ 142 #ifdef SMP 143 #define INTRCNT_COUNT (NIRQ * 2 + INTR_IPI_COUNT * MAXCPU) 144 #else 145 #define INTRCNT_COUNT (NIRQ * 2) 146 #endif 147 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; 154 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); 160 161 /* 162 * Interrupt framework initialization routine. 163 */ 164 static void 165 intr_irq_init(void *dummy __unused) 166 { 167 168 SLIST_INIT(&pic_list); 169 mtx_init(&pic_list_lock, "intr pic list", NULL, MTX_DEF); 170 171 mtx_init(&isrc_table_lock, "intr isrc table", NULL, MTX_DEF); 172 } 173 SYSINIT(intr_irq_init, SI_SUB_INTR, SI_ORDER_FIRST, intr_irq_init, NULL); 174 175 static void 176 intrcnt_setname(const char *name, int index) 177 { 178 179 snprintf(intrnames + INTRNAME_LEN * index, INTRNAME_LEN, "%-*s", 180 INTRNAME_LEN - 1, name); 181 } 182 183 /* 184 * Update name for interrupt source with interrupt event. 185 */ 186 static void 187 intrcnt_updatename(struct intr_irqsrc *isrc) 188 { 189 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); 193 } 194 195 /* 196 * Virtualization for interrupt source interrupt counter increment. 197 */ 198 static inline void 199 isrc_increment_count(struct intr_irqsrc *isrc) 200 { 201 202 if (isrc->isrc_flags & INTR_ISRCF_PPI) 203 atomic_add_long(&isrc->isrc_count[0], 1); 204 else 205 isrc->isrc_count[0]++; 206 } 207 208 /* 209 * Virtualization for interrupt source interrupt stray counter increment. 210 */ 211 static inline void 212 isrc_increment_straycount(struct intr_irqsrc *isrc) 213 { 214 215 isrc->isrc_count[1]++; 216 } 217 218 /* 219 * Virtualization for interrupt source interrupt name update. 220 */ 221 static void 222 isrc_update_name(struct intr_irqsrc *isrc, const char *name) 223 { 224 char str[INTRNAME_LEN]; 225 226 mtx_assert(&isrc_table_lock, MA_OWNED); 227 228 if (name != NULL) { 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, 232 name); 233 intrcnt_setname(str, isrc->isrc_index + 1); 234 } else { 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); 239 } 240 } 241 242 /* 243 * Virtualization for interrupt source interrupt counters setup. 244 */ 245 static void 246 isrc_setup_counters(struct intr_irqsrc *isrc) 247 { 248 u_int index; 249 250 /* 251 * XXX - it does not work well with removable controllers and 252 * interrupt sources !!! 253 */ 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); 258 } 259 260 /* 261 * Virtualization for interrupt source interrupt counters release. 262 */ 263 static void 264 isrc_release_counters(struct intr_irqsrc *isrc) 265 { 266 267 panic("%s: not implemented", __func__); 268 } 269 270 #ifdef SMP 271 /* 272 * Virtualization for interrupt source IPI counters setup. 273 */ 274 u_long * 275 intr_ipi_setup_counters(const char *name) 276 { 277 u_int index, i; 278 char str[INTRNAME_LEN]; 279 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); 284 } 285 return (&intrcnt[index]); 286 } 287 #endif 288 289 /* 290 * Main interrupt dispatch handler. It's called straight 291 * from the assembler, where CPU interrupt is served. 292 */ 293 void 294 intr_irq_handler(struct trapframe *tf) 295 { 296 struct trapframe * oldframe; 297 struct thread * td; 298 299 KASSERT(irq_root_filter != NULL, ("%s: no filter", __func__)); 300 301 VM_CNT_INC(v_intr); 302 critical_enter(); 303 td = curthread; 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; 308 critical_exit(); 309 #ifdef HWPMC_HOOKS 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); 313 #endif 314 } 315 316 int 317 intr_child_irq_handler(struct intr_pic *parent, uintptr_t irq) 318 { 319 struct intr_pic_child *child; 320 bool found; 321 322 found = false; 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)) { 327 found = true; 328 break; 329 } 330 } 331 mtx_unlock_spin(&parent->pic_child_lock); 332 333 if (found) 334 return (child->pc_filter(child->pc_filter_arg, irq)); 335 336 return (FILTER_STRAY); 337 } 338 339 /* 340 * interrupt controller dispatch function for interrupts. It should 341 * be called straight from the interrupt controller, when associated interrupt 342 * source is learned. 343 */ 344 int 345 intr_isrc_dispatch(struct intr_irqsrc *isrc, struct trapframe *tf) 346 { 347 348 KASSERT(isrc != NULL, ("%s: no source", __func__)); 349 350 isrc_increment_count(isrc); 351 352 #ifdef INTR_SOLO 353 if (isrc->isrc_filter != NULL) { 354 int error; 355 error = isrc->isrc_filter(isrc->isrc_arg, tf); 356 PIC_POST_FILTER(isrc->isrc_dev, isrc); 357 if (error == FILTER_HANDLED) 358 return (0); 359 } else 360 #endif 361 if (isrc->isrc_event != NULL) { 362 if (intr_event_handle(isrc->isrc_event, tf) == 0) 363 return (0); 364 } 365 366 isrc_increment_straycount(isrc); 367 return (EINVAL); 368 } 369 370 /* 371 * Alloc unique interrupt number (resource handle) for interrupt source. 372 * 373 * There could be various strategies how to allocate free interrupt number 374 * (resource handle) for new interrupt source. 375 * 376 * 1. Handles are always allocated forward, so handles are not recycled 377 * immediately. However, if only one free handle left which is reused 378 * constantly... 379 */ 380 static inline int 381 isrc_alloc_irq(struct intr_irqsrc *isrc) 382 { 383 u_int maxirqs, irq; 384 385 mtx_assert(&isrc_table_lock, MA_OWNED); 386 387 maxirqs = nitems(irq_sources); 388 if (irq_next_free >= maxirqs) 389 return (ENOSPC); 390 391 for (irq = irq_next_free; irq < maxirqs; irq++) { 392 if (irq_sources[irq] == NULL) 393 goto found; 394 } 395 for (irq = 0; irq < irq_next_free; irq++) { 396 if (irq_sources[irq] == NULL) 397 goto found; 398 } 399 400 irq_next_free = maxirqs; 401 return (ENOSPC); 402 403 found: 404 isrc->isrc_irq = irq; 405 irq_sources[irq] = isrc; 406 407 irq_next_free = irq + 1; 408 if (irq_next_free >= maxirqs) 409 irq_next_free = 0; 410 return (0); 411 } 412 413 /* 414 * Free unique interrupt number (resource handle) from interrupt source. 415 */ 416 static inline int 417 isrc_free_irq(struct intr_irqsrc *isrc) 418 { 419 420 mtx_assert(&isrc_table_lock, MA_OWNED); 421 422 if (isrc->isrc_irq >= nitems(irq_sources)) 423 return (EINVAL); 424 if (irq_sources[isrc->isrc_irq] != isrc) 425 return (EINVAL); 426 427 irq_sources[isrc->isrc_irq] = NULL; 428 isrc->isrc_irq = INTR_IRQ_INVALID; /* just to be safe */ 429 return (0); 430 } 431 432 /* 433 * Initialize interrupt source and register it into global interrupt table. 434 */ 435 int 436 intr_isrc_register(struct intr_irqsrc *isrc, device_t dev, u_int flags, 437 const char *fmt, ...) 438 { 439 int error; 440 va_list ap; 441 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; 446 447 va_start(ap, fmt); 448 vsnprintf(isrc->isrc_name, INTR_ISRC_NAMELEN, fmt, ap); 449 va_end(ap); 450 451 mtx_lock(&isrc_table_lock); 452 error = isrc_alloc_irq(isrc); 453 if (error != 0) { 454 mtx_unlock(&isrc_table_lock); 455 return (error); 456 } 457 /* 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 460 * our counter pool. 461 */ 462 if ((isrc->isrc_flags & INTR_ISRCF_IPI) == 0) 463 isrc_setup_counters(isrc); 464 mtx_unlock(&isrc_table_lock); 465 return (0); 466 } 467 468 /* 469 * Deregister interrupt source from global interrupt table. 470 */ 471 int 472 intr_isrc_deregister(struct intr_irqsrc *isrc) 473 { 474 int error; 475 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); 481 return (error); 482 } 483 484 #ifdef SMP 485 /* 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: 489 * 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. 493 */ 494 bool 495 intr_isrc_init_on_cpu(struct intr_irqsrc *isrc, u_int cpu) 496 { 497 498 if (isrc->isrc_handlers == 0) 499 return (false); 500 if ((isrc->isrc_flags & (INTR_ISRCF_PPI | INTR_ISRCF_IPI)) == 0) 501 return (false); 502 if (isrc->isrc_flags & INTR_ISRCF_BOUND) 503 return (CPU_ISSET(cpu, &isrc->isrc_cpu)); 504 505 CPU_SET(cpu, &isrc->isrc_cpu); 506 return (true); 507 } 508 #endif 509 510 #ifdef INTR_SOLO 511 /* 512 * Setup filter into interrupt source. 513 */ 514 static int 515 iscr_setup_filter(struct intr_irqsrc *isrc, const char *name, 516 intr_irq_filter_t *filter, void *arg, void **cookiep) 517 { 518 519 if (filter == NULL) 520 return (EINVAL); 521 522 mtx_lock(&isrc_table_lock); 523 /* 524 * Make sure that we do not mix the two ways 525 * how we handle interrupt sources. 526 */ 527 if (isrc->isrc_filter != NULL || isrc->isrc_event != NULL) { 528 mtx_unlock(&isrc_table_lock); 529 return (EBUSY); 530 } 531 isrc->isrc_filter = filter; 532 isrc->isrc_arg = arg; 533 isrc_update_name(isrc, name); 534 mtx_unlock(&isrc_table_lock); 535 536 *cookiep = isrc; 537 return (0); 538 } 539 #endif 540 541 /* 542 * Interrupt source pre_ithread method for MI interrupt framework. 543 */ 544 static void 545 intr_isrc_pre_ithread(void *arg) 546 { 547 struct intr_irqsrc *isrc = arg; 548 549 PIC_PRE_ITHREAD(isrc->isrc_dev, isrc); 550 } 551 552 /* 553 * Interrupt source post_ithread method for MI interrupt framework. 554 */ 555 static void 556 intr_isrc_post_ithread(void *arg) 557 { 558 struct intr_irqsrc *isrc = arg; 559 560 PIC_POST_ITHREAD(isrc->isrc_dev, isrc); 561 } 562 563 /* 564 * Interrupt source post_filter method for MI interrupt framework. 565 */ 566 static void 567 intr_isrc_post_filter(void *arg) 568 { 569 struct intr_irqsrc *isrc = arg; 570 571 PIC_POST_FILTER(isrc->isrc_dev, isrc); 572 } 573 574 /* 575 * Interrupt source assign_cpu method for MI interrupt framework. 576 */ 577 static int 578 intr_isrc_assign_cpu(void *arg, int cpu) 579 { 580 #ifdef SMP 581 struct intr_irqsrc *isrc = arg; 582 int error; 583 584 if (isrc->isrc_dev != intr_irq_root_dev) 585 return (EINVAL); 586 587 mtx_lock(&isrc_table_lock); 588 if (cpu == NOCPU) { 589 CPU_ZERO(&isrc->isrc_cpu); 590 isrc->isrc_flags &= ~INTR_ISRCF_BOUND; 591 } else { 592 CPU_SETOF(cpu, &isrc->isrc_cpu); 593 isrc->isrc_flags |= INTR_ISRCF_BOUND; 594 } 595 596 /* 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. 601 */ 602 if (irq_assign_cpu) { 603 error = PIC_BIND_INTR(isrc->isrc_dev, isrc); 604 if (error) { 605 CPU_ZERO(&isrc->isrc_cpu); 606 mtx_unlock(&isrc_table_lock); 607 return (error); 608 } 609 } 610 mtx_unlock(&isrc_table_lock); 611 return (0); 612 #else 613 return (EOPNOTSUPP); 614 #endif 615 } 616 617 /* 618 * Create interrupt event for interrupt source. 619 */ 620 static int 621 isrc_event_create(struct intr_irqsrc *isrc) 622 { 623 struct intr_event *ie; 624 int error; 625 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); 629 if (error) 630 return (error); 631 632 mtx_lock(&isrc_table_lock); 633 /* 634 * Make sure that we do not mix the two ways 635 * how we handle interrupt sources. Let contested event wins. 636 */ 637 #ifdef INTR_SOLO 638 if (isrc->isrc_filter != NULL || isrc->isrc_event != NULL) { 639 #else 640 if (isrc->isrc_event != NULL) { 641 #endif 642 mtx_unlock(&isrc_table_lock); 643 intr_event_destroy(ie); 644 return (isrc->isrc_event != NULL ? EBUSY : 0); 645 } 646 isrc->isrc_event = ie; 647 mtx_unlock(&isrc_table_lock); 648 649 return (0); 650 } 651 #ifdef notyet 652 /* 653 * Destroy interrupt event for interrupt source. 654 */ 655 static void 656 isrc_event_destroy(struct intr_irqsrc *isrc) 657 { 658 struct intr_event *ie; 659 660 mtx_lock(&isrc_table_lock); 661 ie = isrc->isrc_event; 662 isrc->isrc_event = NULL; 663 mtx_unlock(&isrc_table_lock); 664 665 if (ie != NULL) 666 intr_event_destroy(ie); 667 } 668 #endif 669 /* 670 * Add handler to interrupt source. 671 */ 672 static int 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) 676 { 677 int error; 678 679 if (isrc->isrc_event == NULL) { 680 error = isrc_event_create(isrc); 681 if (error) 682 return (error); 683 } 684 685 error = intr_event_add_handler(isrc->isrc_event, name, filter, handler, 686 arg, intr_priority(flags), flags, cookiep); 687 if (error == 0) { 688 mtx_lock(&isrc_table_lock); 689 intrcnt_updatename(isrc); 690 mtx_unlock(&isrc_table_lock); 691 } 692 693 return (error); 694 } 695 696 /* 697 * Lookup interrupt controller locked. 698 */ 699 static inline struct intr_pic * 700 pic_lookup_locked(device_t dev, intptr_t xref, int flags) 701 { 702 struct intr_pic *pic; 703 704 mtx_assert(&pic_list_lock, MA_OWNED); 705 706 if (dev == NULL && xref == 0) 707 return (NULL); 708 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)) 713 continue; 714 715 if (dev == NULL) { 716 if (xref == pic->pic_xref) 717 return (pic); 718 } else if (xref == 0 || pic->pic_xref == 0) { 719 if (dev == pic->pic_dev) 720 return (pic); 721 } else if (xref == pic->pic_xref && dev == pic->pic_dev) 722 return (pic); 723 } 724 return (NULL); 725 } 726 727 /* 728 * Lookup interrupt controller. 729 */ 730 static struct intr_pic * 731 pic_lookup(device_t dev, intptr_t xref, int flags) 732 { 733 struct intr_pic *pic; 734 735 mtx_lock(&pic_list_lock); 736 pic = pic_lookup_locked(dev, xref, flags); 737 mtx_unlock(&pic_list_lock); 738 return (pic); 739 } 740 741 /* 742 * Create interrupt controller. 743 */ 744 static struct intr_pic * 745 pic_create(device_t dev, intptr_t xref, int flags) 746 { 747 struct intr_pic *pic; 748 749 mtx_lock(&pic_list_lock); 750 pic = pic_lookup_locked(dev, xref, flags); 751 if (pic != NULL) { 752 mtx_unlock(&pic_list_lock); 753 return (pic); 754 } 755 pic = malloc(sizeof(*pic), M_INTRNG, M_NOWAIT | M_ZERO); 756 if (pic == NULL) { 757 mtx_unlock(&pic_list_lock); 758 return (NULL); 759 } 760 pic->pic_xref = xref; 761 pic->pic_dev = dev; 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); 766 767 return (pic); 768 } 769 #ifdef notyet 770 /* 771 * Destroy interrupt controller. 772 */ 773 static void 774 pic_destroy(device_t dev, intptr_t xref, int flags) 775 { 776 struct intr_pic *pic; 777 778 mtx_lock(&pic_list_lock); 779 pic = pic_lookup_locked(dev, xref, flags); 780 if (pic == NULL) { 781 mtx_unlock(&pic_list_lock); 782 return; 783 } 784 SLIST_REMOVE(&pic_list, pic, intr_pic, pic_next); 785 mtx_unlock(&pic_list_lock); 786 787 free(pic, M_INTRNG); 788 } 789 #endif 790 /* 791 * Register interrupt controller. 792 */ 793 struct intr_pic * 794 intr_pic_register(device_t dev, intptr_t xref) 795 { 796 struct intr_pic *pic; 797 798 if (dev == NULL) 799 return (NULL); 800 pic = pic_create(dev, xref, FLAG_PIC); 801 if (pic == NULL) 802 return (NULL); 803 804 debugf("PIC %p registered for %s <dev %p, xref %jx>\n", pic, 805 device_get_nameunit(dev), dev, (uintmax_t)xref); 806 return (pic); 807 } 808 809 /* 810 * Unregister interrupt controller. 811 */ 812 int 813 intr_pic_deregister(device_t dev, intptr_t xref) 814 { 815 816 panic("%s: not implemented", __func__); 817 } 818 819 /* 820 * Mark interrupt controller (itself) as a root one. 821 * 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. 824 * 825 * In FDT case, according to ePAPR approved version 1.1 from 08 April 2011, 826 * page 30: 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." 830 */ 831 int 832 intr_pic_claim_root(device_t dev, intptr_t xref, intr_irq_filter_t *filter, 833 void *arg, u_int ipicount) 834 { 835 struct intr_pic *pic; 836 837 pic = pic_lookup(dev, xref, FLAG_PIC); 838 if (pic == NULL) { 839 device_printf(dev, "not registered\n"); 840 return (EINVAL); 841 } 842 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))); 846 847 if (filter == NULL) { 848 device_printf(dev, "filter missing\n"); 849 return (EINVAL); 850 } 851 852 /* 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(). 856 */ 857 if (intr_irq_root_dev != NULL) { 858 device_printf(dev, "another root already set\n"); 859 return (EBUSY); 860 } 861 862 intr_irq_root_dev = dev; 863 irq_root_filter = filter; 864 irq_root_arg = arg; 865 irq_root_ipicount = ipicount; 866 867 debugf("irq root set to %s\n", device_get_nameunit(dev)); 868 return (0); 869 } 870 871 /* 872 * Add a handler to manage a sub range of a parents interrupts. 873 */ 874 struct intr_pic * 875 intr_pic_add_handler(device_t parent, struct intr_pic *pic, 876 intr_child_irq_filter_t *filter, void *arg, uintptr_t start, 877 uintptr_t length) 878 { 879 struct intr_pic *parent_pic; 880 struct intr_pic_child *newchild; 881 #ifdef INVARIANTS 882 struct intr_pic_child *child; 883 #endif 884 885 /* Find the parent PIC */ 886 parent_pic = pic_lookup(parent, 0, FLAG_PIC); 887 if (parent_pic == NULL) 888 return (NULL); 889 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; 896 897 mtx_lock_spin(&parent_pic->pic_child_lock); 898 #ifdef INVARIANTS 899 SLIST_FOREACH(child, &parent_pic->pic_children, pc_next) { 900 KASSERT(child->pc_pic != pic, ("%s: Adding a child PIC twice", 901 __func__)); 902 } 903 #endif 904 SLIST_INSERT_HEAD(&parent_pic->pic_children, newchild, pc_next); 905 mtx_unlock_spin(&parent_pic->pic_child_lock); 906 907 return (pic); 908 } 909 910 static int 911 intr_resolve_irq(device_t dev, intptr_t xref, struct intr_map_data *data, 912 struct intr_irqsrc **isrc) 913 { 914 struct intr_pic *pic; 915 struct intr_map_data_msi *msi; 916 917 if (data == NULL) 918 return (EINVAL); 919 920 pic = pic_lookup(dev, xref, 921 (data->type == INTR_MAP_DATA_MSI) ? FLAG_MSI : FLAG_PIC); 922 if (pic == NULL) 923 return (ESRCH); 924 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; 931 *isrc = msi->isrc; 932 return (0); 933 934 default: 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)); 939 } 940 } 941 942 int 943 intr_activate_irq(device_t dev, struct resource *res) 944 { 945 device_t map_dev; 946 intptr_t map_xref; 947 struct intr_map_data *data; 948 struct intr_irqsrc *isrc; 949 u_int res_id; 950 int error; 951 952 KASSERT(rman_get_start(res) == rman_get_end(res), 953 ("%s: more interrupts in resource", __func__)); 954 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", 958 res_id); 959 intr_map_copy_map_data(res_id, &map_dev, &map_xref, &data); 960 error = intr_resolve_irq(map_dev, map_xref, data, &isrc); 961 if (error != 0) { 962 free(data, M_INTRNG); 963 /* XXX TODO DISCONECTED PICs */ 964 /* if (error == EINVAL) return(0); */ 965 return (error); 966 } 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)); 970 } 971 972 int 973 intr_deactivate_irq(device_t dev, struct resource *res) 974 { 975 struct intr_map_data *data; 976 struct intr_irqsrc *isrc; 977 u_int res_id; 978 int error; 979 980 KASSERT(rman_get_start(res) == rman_get_end(res), 981 ("%s: more interrupts in resource", __func__)); 982 983 res_id = (u_int)rman_get_start(res); 984 isrc = intr_map_get_isrc(res_id); 985 if (isrc == NULL) 986 panic("Attempt to deactivate non-active resource id: %u\n", 987 res_id); 988 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); 994 return (error); 995 } 996 997 int 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) 1000 { 1001 int error; 1002 struct intr_map_data *data; 1003 struct intr_irqsrc *isrc; 1004 const char *name; 1005 u_int res_id; 1006 1007 KASSERT(rman_get_start(res) == rman_get_end(res), 1008 ("%s: more interrupts in resource", __func__)); 1009 1010 res_id = (u_int)rman_get_start(res); 1011 isrc = intr_map_get_isrc(res_id); 1012 if (isrc == NULL) { 1013 /* XXX TODO DISCONECTED PICs */ 1014 return (EINVAL); 1015 } 1016 1017 data = rman_get_virtual(res); 1018 name = device_get_nameunit(dev); 1019 1020 #ifdef INTR_SOLO 1021 /* 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. 1027 * 1028 * Note that root interrupt controller routine is served as well, 1029 * however in intr_irq_handler(), i.e. main system dispatch routine. 1030 */ 1031 if (flags & INTR_SOLO && hand != NULL) { 1032 debugf("irq %u cannot solo on %s\n", irq, name); 1033 return (EINVAL); 1034 } 1035 1036 if (flags & INTR_SOLO) { 1037 error = iscr_setup_filter(isrc, name, (intr_irq_filter_t *)filt, 1038 arg, cookiep); 1039 debugf("irq %u setup filter error %d on %s\n", isrc->isrc_irq, error, 1040 name); 1041 } else 1042 #endif 1043 { 1044 error = isrc_add_handler(isrc, name, filt, hand, arg, flags, 1045 cookiep); 1046 debugf("irq %u add handler error %d on %s\n", isrc->isrc_irq, error, name); 1047 } 1048 if (error != 0) 1049 return (error); 1050 1051 mtx_lock(&isrc_table_lock); 1052 error = PIC_SETUP_INTR(isrc->isrc_dev, isrc, res, data); 1053 if (error == 0) { 1054 isrc->isrc_handlers++; 1055 if (isrc->isrc_handlers == 1) 1056 PIC_ENABLE_INTR(isrc->isrc_dev, isrc); 1057 } 1058 mtx_unlock(&isrc_table_lock); 1059 if (error != 0) 1060 intr_event_remove_handler(*cookiep); 1061 return (error); 1062 } 1063 1064 int 1065 intr_teardown_irq(device_t dev, struct resource *res, void *cookie) 1066 { 1067 int error; 1068 struct intr_map_data *data; 1069 struct intr_irqsrc *isrc; 1070 u_int res_id; 1071 1072 KASSERT(rman_get_start(res) == rman_get_end(res), 1073 ("%s: more interrupts in resource", __func__)); 1074 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) 1078 return (EINVAL); 1079 1080 data = rman_get_virtual(res); 1081 1082 #ifdef INTR_SOLO 1083 if (isrc->isrc_filter != NULL) { 1084 if (isrc != cookie) 1085 return (EINVAL); 1086 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); 1095 return (0); 1096 } 1097 #endif 1098 if (isrc != intr_handler_source(cookie)) 1099 return (EINVAL); 1100 1101 error = intr_event_remove_handler(cookie); 1102 if (error == 0) { 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); 1110 } 1111 return (error); 1112 } 1113 1114 int 1115 intr_describe_irq(device_t dev, struct resource *res, void *cookie, 1116 const char *descr) 1117 { 1118 int error; 1119 struct intr_irqsrc *isrc; 1120 u_int res_id; 1121 1122 KASSERT(rman_get_start(res) == rman_get_end(res), 1123 ("%s: more interrupts in resource", __func__)); 1124 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) 1128 return (EINVAL); 1129 #ifdef INTR_SOLO 1130 if (isrc->isrc_filter != NULL) { 1131 if (isrc != cookie) 1132 return (EINVAL); 1133 1134 mtx_lock(&isrc_table_lock); 1135 isrc_update_name(isrc, descr); 1136 mtx_unlock(&isrc_table_lock); 1137 return (0); 1138 } 1139 #endif 1140 error = intr_event_describe_handler(isrc->isrc_event, cookie, descr); 1141 if (error == 0) { 1142 mtx_lock(&isrc_table_lock); 1143 intrcnt_updatename(isrc); 1144 mtx_unlock(&isrc_table_lock); 1145 } 1146 return (error); 1147 } 1148 1149 #ifdef SMP 1150 int 1151 intr_bind_irq(device_t dev, struct resource *res, int cpu) 1152 { 1153 struct intr_irqsrc *isrc; 1154 u_int res_id; 1155 1156 KASSERT(rman_get_start(res) == rman_get_end(res), 1157 ("%s: more interrupts in resource", __func__)); 1158 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) 1162 return (EINVAL); 1163 #ifdef INTR_SOLO 1164 if (isrc->isrc_filter != NULL) 1165 return (intr_isrc_assign_cpu(isrc, cpu)); 1166 #endif 1167 return (intr_event_bind(isrc->isrc_event, cpu)); 1168 } 1169 1170 /* 1171 * Return the CPU that the next interrupt source should use. 1172 * For now just returns the next CPU according to round-robin. 1173 */ 1174 u_int 1175 intr_irq_next_cpu(u_int last_cpu, cpuset_t *cpumask) 1176 { 1177 u_int cpu; 1178 1179 KASSERT(!CPU_EMPTY(cpumask), ("%s: Empty CPU mask", __func__)); 1180 if (!irq_assign_cpu || mp_ncpus == 1) { 1181 cpu = PCPU_GET(cpuid); 1182 1183 if (CPU_ISSET(cpu, cpumask)) 1184 return (curcpu); 1185 1186 return (CPU_FFS(cpumask) - 1); 1187 } 1188 1189 do { 1190 last_cpu++; 1191 if (last_cpu > mp_maxid) 1192 last_cpu = 0; 1193 } while (!CPU_ISSET(last_cpu, cpumask)); 1194 return (last_cpu); 1195 } 1196 1197 #ifndef EARLY_AP_STARTUP 1198 /* 1199 * Distribute all the interrupt sources among the available 1200 * CPUs once the AP's have been launched. 1201 */ 1202 static void 1203 intr_irq_shuffle(void *arg __unused) 1204 { 1205 struct intr_irqsrc *isrc; 1206 u_int i; 1207 1208 if (mp_ncpus == 1) 1209 return; 1210 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)) 1217 continue; 1218 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__); 1223 1224 if ((isrc->isrc_flags & INTR_ISRCF_BOUND) == 0) 1225 CPU_ZERO(&isrc->isrc_cpu); /* start again */ 1226 1227 /* 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. 1231 */ 1232 if (PIC_BIND_INTR(isrc->isrc_dev, isrc) != 0) 1233 CPU_ZERO(&isrc->isrc_cpu); 1234 } 1235 mtx_unlock(&isrc_table_lock); 1236 } 1237 SYSINIT(intr_irq_shuffle, SI_SUB_SMP, SI_ORDER_SECOND, intr_irq_shuffle, NULL); 1238 #endif /* !EARLY_AP_STARTUP */ 1239 1240 #else 1241 u_int 1242 intr_irq_next_cpu(u_int current_cpu, cpuset_t *cpumask) 1243 { 1244 1245 return (PCPU_GET(cpuid)); 1246 } 1247 #endif /* SMP */ 1248 1249 /* 1250 * Allocate memory for new intr_map_data structure. 1251 * Initialize common fields. 1252 */ 1253 struct intr_map_data * 1254 intr_alloc_map_data(enum intr_map_data_type type, size_t len, int flags) 1255 { 1256 struct intr_map_data *data; 1257 1258 data = malloc(len, M_INTRNG, flags); 1259 data->type = type; 1260 data->len = len; 1261 return (data); 1262 } 1263 1264 void intr_free_intr_map_data(struct intr_map_data *data) 1265 { 1266 1267 free(data, M_INTRNG); 1268 } 1269 1270 /* 1271 * Register a MSI/MSI-X interrupt controller 1272 */ 1273 int 1274 intr_msi_register(device_t dev, intptr_t xref) 1275 { 1276 struct intr_pic *pic; 1277 1278 if (dev == NULL) 1279 return (EINVAL); 1280 pic = pic_create(dev, xref, FLAG_MSI); 1281 if (pic == NULL) 1282 return (ENOMEM); 1283 1284 debugf("PIC %p registered for %s <dev %p, xref %jx>\n", pic, 1285 device_get_nameunit(dev), dev, (uintmax_t)xref); 1286 return (0); 1287 } 1288 1289 int 1290 intr_alloc_msi(device_t pci, device_t child, intptr_t xref, int count, 1291 int maxcount, int *irqs) 1292 { 1293 struct intr_irqsrc **isrc; 1294 struct intr_pic *pic; 1295 device_t pdev; 1296 struct intr_map_data_msi *msi; 1297 int err, i; 1298 1299 pic = pic_lookup(NULL, xref, FLAG_MSI); 1300 if (pic == NULL) 1301 return (ESRCH); 1302 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))); 1306 1307 isrc = malloc(sizeof(*isrc) * count, M_INTRNG, M_WAITOK); 1308 err = MSI_ALLOC_MSI(pic->pic_dev, child, count, maxcount, &pdev, isrc); 1309 if (err != 0) { 1310 free(isrc, M_INTRNG); 1311 return (err); 1312 } 1313 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); 1320 } 1321 free(isrc, M_INTRNG); 1322 1323 return (err); 1324 } 1325 1326 int 1327 intr_release_msi(device_t pci, device_t child, intptr_t xref, int count, 1328 int *irqs) 1329 { 1330 struct intr_irqsrc **isrc; 1331 struct intr_pic *pic; 1332 struct intr_map_data_msi *msi; 1333 int i, err; 1334 1335 pic = pic_lookup(NULL, xref, FLAG_MSI); 1336 if (pic == NULL) 1337 return (ESRCH); 1338 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))); 1342 1343 isrc = malloc(sizeof(*isrc) * count, M_INTRNG, M_WAITOK); 1344 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__, 1350 irqs[i])); 1351 isrc[i] = msi->isrc; 1352 } 1353 1354 err = MSI_RELEASE_MSI(pic->pic_dev, child, count, isrc); 1355 1356 for (i = 0; i < count; i++) { 1357 if (isrc[i] != NULL) 1358 intr_unmap_irq(irqs[i]); 1359 } 1360 1361 free(isrc, M_INTRNG); 1362 return (err); 1363 } 1364 1365 int 1366 intr_alloc_msix(device_t pci, device_t child, intptr_t xref, int *irq) 1367 { 1368 struct intr_irqsrc *isrc; 1369 struct intr_pic *pic; 1370 device_t pdev; 1371 struct intr_map_data_msi *msi; 1372 int err; 1373 1374 pic = pic_lookup(NULL, xref, FLAG_MSI); 1375 if (pic == NULL) 1376 return (ESRCH); 1377 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))); 1381 1382 err = MSI_ALLOC_MSIX(pic->pic_dev, child, &pdev, &isrc); 1383 if (err != 0) 1384 return (err); 1385 1386 msi = (struct intr_map_data_msi *)intr_alloc_map_data( 1387 INTR_MAP_DATA_MSI, sizeof(*msi), M_WAITOK | M_ZERO); 1388 msi->isrc = isrc; 1389 *irq = intr_map_irq(pic->pic_dev, xref, (struct intr_map_data *)msi); 1390 return (0); 1391 } 1392 1393 int 1394 intr_release_msix(device_t pci, device_t child, intptr_t xref, int irq) 1395 { 1396 struct intr_irqsrc *isrc; 1397 struct intr_pic *pic; 1398 struct intr_map_data_msi *msi; 1399 int err; 1400 1401 pic = pic_lookup(NULL, xref, FLAG_MSI); 1402 if (pic == NULL) 1403 return (ESRCH); 1404 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))); 1408 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__, 1413 irq)); 1414 isrc = msi->isrc; 1415 if (isrc == NULL) { 1416 intr_unmap_irq(irq); 1417 return (EINVAL); 1418 } 1419 1420 err = MSI_RELEASE_MSIX(pic->pic_dev, child, isrc); 1421 intr_unmap_irq(irq); 1422 1423 return (err); 1424 } 1425 1426 int 1427 intr_map_msi(device_t pci, device_t child, intptr_t xref, int irq, 1428 uint64_t *addr, uint32_t *data) 1429 { 1430 struct intr_irqsrc *isrc; 1431 struct intr_pic *pic; 1432 int err; 1433 1434 pic = pic_lookup(NULL, xref, FLAG_MSI); 1435 if (pic == NULL) 1436 return (ESRCH); 1437 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))); 1441 1442 isrc = intr_map_get_isrc(irq); 1443 if (isrc == NULL) 1444 return (EINVAL); 1445 1446 err = MSI_MAP_MSI(pic->pic_dev, child, isrc, addr, data); 1447 return (err); 1448 } 1449 1450 void dosoftints(void); 1451 void 1452 dosoftints(void) 1453 { 1454 } 1455 1456 #ifdef SMP 1457 /* 1458 * Init interrupt controller on another CPU. 1459 */ 1460 void 1461 intr_pic_init_secondary(void) 1462 { 1463 1464 /* 1465 * QQQ: Only root PIC is aware of other CPUs ??? 1466 */ 1467 KASSERT(intr_irq_root_dev != NULL, ("%s: no root attached", __func__)); 1468 1469 //mtx_lock(&isrc_table_lock); 1470 PIC_INIT_SECONDARY(intr_irq_root_dev); 1471 //mtx_unlock(&isrc_table_lock); 1472 } 1473 #endif 1474 1475 #ifdef DDB 1476 DB_SHOW_COMMAND(irqs, db_show_irqs) 1477 { 1478 u_int i, irqsum; 1479 u_long num; 1480 struct intr_irqsrc *isrc; 1481 1482 for (irqsum = 0, i = 0; i < NIRQ; i++) { 1483 isrc = irq_sources[i]; 1484 if (isrc == NULL) 1485 continue; 1486 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); 1491 irqsum += num; 1492 } 1493 db_printf("irq total %u\n", irqsum); 1494 } 1495 #endif 1496 1497 /* 1498 * Interrupt mapping table functions. 1499 * 1500 * Please, keep this part separately, it can be transformed to 1501 * extension of standard resources. 1502 */ 1503 struct intr_map_entry 1504 { 1505 device_t dev; 1506 intptr_t xref; 1507 struct intr_map_data *map_data; 1508 struct intr_irqsrc *isrc; 1509 /* XXX TODO DISCONECTED PICs */ 1510 /*int flags */ 1511 }; 1512 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; 1518 1519 static struct intr_irqsrc * 1520 intr_map_get_isrc(u_int res_id) 1521 { 1522 struct intr_irqsrc *isrc; 1523 1524 isrc = NULL; 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); 1529 1530 return (isrc); 1531 } 1532 1533 static void 1534 intr_map_set_isrc(u_int res_id, struct intr_irqsrc *isrc) 1535 { 1536 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); 1541 } 1542 1543 /* 1544 * Get a copy of intr_map_entry data 1545 */ 1546 static struct intr_map_data * 1547 intr_map_get_map_data(u_int res_id) 1548 { 1549 struct intr_map_data *data; 1550 1551 data = NULL; 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); 1557 1558 return (data); 1559 } 1560 1561 /* 1562 * Get a copy of intr_map_entry data 1563 */ 1564 static void 1565 intr_map_copy_map_data(u_int res_id, device_t *map_dev, intptr_t *map_xref, 1566 struct intr_map_data **data) 1567 { 1568 size_t len; 1569 1570 len = 0; 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); 1577 1578 if (len == 0) 1579 *data = NULL; 1580 else 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); 1585 if (len != 0) { 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); 1589 } 1590 *map_dev = irq_map[res_id]->dev; 1591 *map_xref = irq_map[res_id]->xref; 1592 mtx_unlock(&irq_map_lock); 1593 } 1594 1595 /* 1596 * Allocate and fill new entry in irq_map table. 1597 */ 1598 u_int 1599 intr_map_irq(device_t dev, intptr_t xref, struct intr_map_data *data) 1600 { 1601 u_int i; 1602 struct intr_map_entry *entry; 1603 1604 /* Prepare new entry first. */ 1605 entry = malloc(sizeof(*entry), M_INTRNG, M_WAITOK | M_ZERO); 1606 1607 entry->dev = dev; 1608 entry->xref = xref; 1609 entry->map_data = data; 1610 entry->isrc = NULL; 1611 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) { 1615 irq_map[i] = entry; 1616 irq_map_first_free_idx = i + 1; 1617 mtx_unlock(&irq_map_lock); 1618 return (i); 1619 } 1620 } 1621 mtx_unlock(&irq_map_lock); 1622 1623 /* XXX Expand irq_map table */ 1624 panic("IRQ mapping table is full."); 1625 } 1626 1627 /* 1628 * Remove and free mapping entry. 1629 */ 1630 void 1631 intr_unmap_irq(u_int res_id) 1632 { 1633 struct intr_map_entry *entry; 1634 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); 1644 } 1645 1646 /* 1647 * Clone mapping entry. 1648 */ 1649 u_int 1650 intr_map_clone_irq(u_int old_res_id) 1651 { 1652 device_t map_dev; 1653 intptr_t map_xref; 1654 struct intr_map_data *data; 1655 1656 intr_map_copy_map_data(old_res_id, &map_dev, &map_xref, &data); 1657 return (intr_map_irq(map_dev, map_xref, data)); 1658 } 1659 1660 static void 1661 intr_map_init(void *dummy __unused) 1662 { 1663 1664 mtx_init(&irq_map_lock, "intr map table", NULL, MTX_DEF); 1665 } 1666 SYSINIT(intr_map_init, SI_SUB_INTR, SI_ORDER_FIRST, intr_map_init, NULL); 1667