1 #define pr_fmt(fmt) "irq: " fmt 2 3 #include <linux/debugfs.h> 4 #include <linux/hardirq.h> 5 #include <linux/interrupt.h> 6 #include <linux/irq.h> 7 #include <linux/irqdesc.h> 8 #include <linux/irqdomain.h> 9 #include <linux/module.h> 10 #include <linux/mutex.h> 11 #include <linux/of.h> 12 #include <linux/of_address.h> 13 #include <linux/of_irq.h> 14 #include <linux/topology.h> 15 #include <linux/seq_file.h> 16 #include <linux/slab.h> 17 #include <linux/smp.h> 18 #include <linux/fs.h> 19 20 static LIST_HEAD(irq_domain_list); 21 static DEFINE_MUTEX(irq_domain_mutex); 22 23 static DEFINE_MUTEX(revmap_trees_mutex); 24 static struct irq_domain *irq_default_domain; 25 26 static void irq_domain_check_hierarchy(struct irq_domain *domain); 27 28 struct irqchip_fwid { 29 struct fwnode_handle fwnode; 30 unsigned int type; 31 char *name; 32 void *data; 33 }; 34 35 #ifdef CONFIG_GENERIC_IRQ_DEBUGFS 36 static void debugfs_add_domain_dir(struct irq_domain *d); 37 static void debugfs_remove_domain_dir(struct irq_domain *d); 38 #else 39 static inline void debugfs_add_domain_dir(struct irq_domain *d) { } 40 static inline void debugfs_remove_domain_dir(struct irq_domain *d) { } 41 #endif 42 43 /** 44 * irq_domain_alloc_fwnode - Allocate a fwnode_handle suitable for 45 * identifying an irq domain 46 * @type: Type of irqchip_fwnode. See linux/irqdomain.h 47 * @name: Optional user provided domain name 48 * @id: Optional user provided id if name != NULL 49 * @data: Optional user-provided data 50 * 51 * Allocate a struct irqchip_fwid, and return a poiner to the embedded 52 * fwnode_handle (or NULL on failure). 53 * 54 * Note: The types IRQCHIP_FWNODE_NAMED and IRQCHIP_FWNODE_NAMED_ID are 55 * solely to transport name information to irqdomain creation code. The 56 * node is not stored. For other types the pointer is kept in the irq 57 * domain struct. 58 */ 59 struct fwnode_handle *__irq_domain_alloc_fwnode(unsigned int type, int id, 60 const char *name, void *data) 61 { 62 struct irqchip_fwid *fwid; 63 char *n; 64 65 fwid = kzalloc(sizeof(*fwid), GFP_KERNEL); 66 67 switch (type) { 68 case IRQCHIP_FWNODE_NAMED: 69 n = kasprintf(GFP_KERNEL, "%s", name); 70 break; 71 case IRQCHIP_FWNODE_NAMED_ID: 72 n = kasprintf(GFP_KERNEL, "%s-%d", name, id); 73 break; 74 default: 75 n = kasprintf(GFP_KERNEL, "irqchip@%p", data); 76 break; 77 } 78 79 if (!fwid || !n) { 80 kfree(fwid); 81 kfree(n); 82 return NULL; 83 } 84 85 fwid->type = type; 86 fwid->name = n; 87 fwid->data = data; 88 fwid->fwnode.type = FWNODE_IRQCHIP; 89 return &fwid->fwnode; 90 } 91 EXPORT_SYMBOL_GPL(__irq_domain_alloc_fwnode); 92 93 /** 94 * irq_domain_free_fwnode - Free a non-OF-backed fwnode_handle 95 * 96 * Free a fwnode_handle allocated with irq_domain_alloc_fwnode. 97 */ 98 void irq_domain_free_fwnode(struct fwnode_handle *fwnode) 99 { 100 struct irqchip_fwid *fwid; 101 102 if (WARN_ON(!is_fwnode_irqchip(fwnode))) 103 return; 104 105 fwid = container_of(fwnode, struct irqchip_fwid, fwnode); 106 kfree(fwid->name); 107 kfree(fwid); 108 } 109 EXPORT_SYMBOL_GPL(irq_domain_free_fwnode); 110 111 /** 112 * __irq_domain_add() - Allocate a new irq_domain data structure 113 * @fwnode: firmware node for the interrupt controller 114 * @size: Size of linear map; 0 for radix mapping only 115 * @hwirq_max: Maximum number of interrupts supported by controller 116 * @direct_max: Maximum value of direct maps; Use ~0 for no limit; 0 for no 117 * direct mapping 118 * @ops: domain callbacks 119 * @host_data: Controller private data pointer 120 * 121 * Allocates and initialize and irq_domain structure. 122 * Returns pointer to IRQ domain, or NULL on failure. 123 */ 124 struct irq_domain *__irq_domain_add(struct fwnode_handle *fwnode, int size, 125 irq_hw_number_t hwirq_max, int direct_max, 126 const struct irq_domain_ops *ops, 127 void *host_data) 128 { 129 struct device_node *of_node = to_of_node(fwnode); 130 struct irqchip_fwid *fwid; 131 struct irq_domain *domain; 132 133 static atomic_t unknown_domains; 134 135 domain = kzalloc_node(sizeof(*domain) + (sizeof(unsigned int) * size), 136 GFP_KERNEL, of_node_to_nid(of_node)); 137 if (WARN_ON(!domain)) 138 return NULL; 139 140 if (fwnode && is_fwnode_irqchip(fwnode)) { 141 fwid = container_of(fwnode, struct irqchip_fwid, fwnode); 142 143 switch (fwid->type) { 144 case IRQCHIP_FWNODE_NAMED: 145 case IRQCHIP_FWNODE_NAMED_ID: 146 domain->name = kstrdup(fwid->name, GFP_KERNEL); 147 if (!domain->name) { 148 kfree(domain); 149 return NULL; 150 } 151 domain->flags |= IRQ_DOMAIN_NAME_ALLOCATED; 152 break; 153 default: 154 domain->fwnode = fwnode; 155 domain->name = fwid->name; 156 break; 157 } 158 } else if (of_node) { 159 char *name; 160 161 /* 162 * DT paths contain '/', which debugfs is legitimately 163 * unhappy about. Replace them with ':', which does 164 * the trick and is not as offensive as '\'... 165 */ 166 name = kstrdup(of_node_full_name(of_node), GFP_KERNEL); 167 if (!name) { 168 kfree(domain); 169 return NULL; 170 } 171 172 strreplace(name, '/', ':'); 173 174 domain->name = name; 175 domain->fwnode = fwnode; 176 domain->flags |= IRQ_DOMAIN_NAME_ALLOCATED; 177 } 178 179 if (!domain->name) { 180 if (fwnode) { 181 pr_err("Invalid fwnode type (%d) for irqdomain\n", 182 fwnode->type); 183 } 184 domain->name = kasprintf(GFP_KERNEL, "unknown-%d", 185 atomic_inc_return(&unknown_domains)); 186 if (!domain->name) { 187 kfree(domain); 188 return NULL; 189 } 190 domain->flags |= IRQ_DOMAIN_NAME_ALLOCATED; 191 } 192 193 of_node_get(of_node); 194 195 /* Fill structure */ 196 INIT_RADIX_TREE(&domain->revmap_tree, GFP_KERNEL); 197 domain->ops = ops; 198 domain->host_data = host_data; 199 domain->hwirq_max = hwirq_max; 200 domain->revmap_size = size; 201 domain->revmap_direct_max_irq = direct_max; 202 irq_domain_check_hierarchy(domain); 203 204 mutex_lock(&irq_domain_mutex); 205 debugfs_add_domain_dir(domain); 206 list_add(&domain->link, &irq_domain_list); 207 mutex_unlock(&irq_domain_mutex); 208 209 pr_debug("Added domain %s\n", domain->name); 210 return domain; 211 } 212 EXPORT_SYMBOL_GPL(__irq_domain_add); 213 214 /** 215 * irq_domain_remove() - Remove an irq domain. 216 * @domain: domain to remove 217 * 218 * This routine is used to remove an irq domain. The caller must ensure 219 * that all mappings within the domain have been disposed of prior to 220 * use, depending on the revmap type. 221 */ 222 void irq_domain_remove(struct irq_domain *domain) 223 { 224 mutex_lock(&irq_domain_mutex); 225 debugfs_remove_domain_dir(domain); 226 227 WARN_ON(!radix_tree_empty(&domain->revmap_tree)); 228 229 list_del(&domain->link); 230 231 /* 232 * If the going away domain is the default one, reset it. 233 */ 234 if (unlikely(irq_default_domain == domain)) 235 irq_set_default_host(NULL); 236 237 mutex_unlock(&irq_domain_mutex); 238 239 pr_debug("Removed domain %s\n", domain->name); 240 241 of_node_put(irq_domain_get_of_node(domain)); 242 if (domain->flags & IRQ_DOMAIN_NAME_ALLOCATED) 243 kfree(domain->name); 244 kfree(domain); 245 } 246 EXPORT_SYMBOL_GPL(irq_domain_remove); 247 248 void irq_domain_update_bus_token(struct irq_domain *domain, 249 enum irq_domain_bus_token bus_token) 250 { 251 char *name; 252 253 if (domain->bus_token == bus_token) 254 return; 255 256 mutex_lock(&irq_domain_mutex); 257 258 domain->bus_token = bus_token; 259 260 name = kasprintf(GFP_KERNEL, "%s-%d", domain->name, bus_token); 261 if (!name) { 262 mutex_unlock(&irq_domain_mutex); 263 return; 264 } 265 266 debugfs_remove_domain_dir(domain); 267 268 if (domain->flags & IRQ_DOMAIN_NAME_ALLOCATED) 269 kfree(domain->name); 270 else 271 domain->flags |= IRQ_DOMAIN_NAME_ALLOCATED; 272 273 domain->name = name; 274 debugfs_add_domain_dir(domain); 275 276 mutex_unlock(&irq_domain_mutex); 277 } 278 279 /** 280 * irq_domain_add_simple() - Register an irq_domain and optionally map a range of irqs 281 * @of_node: pointer to interrupt controller's device tree node. 282 * @size: total number of irqs in mapping 283 * @first_irq: first number of irq block assigned to the domain, 284 * pass zero to assign irqs on-the-fly. If first_irq is non-zero, then 285 * pre-map all of the irqs in the domain to virqs starting at first_irq. 286 * @ops: domain callbacks 287 * @host_data: Controller private data pointer 288 * 289 * Allocates an irq_domain, and optionally if first_irq is positive then also 290 * allocate irq_descs and map all of the hwirqs to virqs starting at first_irq. 291 * 292 * This is intended to implement the expected behaviour for most 293 * interrupt controllers. If device tree is used, then first_irq will be 0 and 294 * irqs get mapped dynamically on the fly. However, if the controller requires 295 * static virq assignments (non-DT boot) then it will set that up correctly. 296 */ 297 struct irq_domain *irq_domain_add_simple(struct device_node *of_node, 298 unsigned int size, 299 unsigned int first_irq, 300 const struct irq_domain_ops *ops, 301 void *host_data) 302 { 303 struct irq_domain *domain; 304 305 domain = __irq_domain_add(of_node_to_fwnode(of_node), size, size, 0, ops, host_data); 306 if (!domain) 307 return NULL; 308 309 if (first_irq > 0) { 310 if (IS_ENABLED(CONFIG_SPARSE_IRQ)) { 311 /* attempt to allocated irq_descs */ 312 int rc = irq_alloc_descs(first_irq, first_irq, size, 313 of_node_to_nid(of_node)); 314 if (rc < 0) 315 pr_info("Cannot allocate irq_descs @ IRQ%d, assuming pre-allocated\n", 316 first_irq); 317 } 318 irq_domain_associate_many(domain, first_irq, 0, size); 319 } 320 321 return domain; 322 } 323 EXPORT_SYMBOL_GPL(irq_domain_add_simple); 324 325 /** 326 * irq_domain_add_legacy() - Allocate and register a legacy revmap irq_domain. 327 * @of_node: pointer to interrupt controller's device tree node. 328 * @size: total number of irqs in legacy mapping 329 * @first_irq: first number of irq block assigned to the domain 330 * @first_hwirq: first hwirq number to use for the translation. Should normally 331 * be '0', but a positive integer can be used if the effective 332 * hwirqs numbering does not begin at zero. 333 * @ops: map/unmap domain callbacks 334 * @host_data: Controller private data pointer 335 * 336 * Note: the map() callback will be called before this function returns 337 * for all legacy interrupts except 0 (which is always the invalid irq for 338 * a legacy controller). 339 */ 340 struct irq_domain *irq_domain_add_legacy(struct device_node *of_node, 341 unsigned int size, 342 unsigned int first_irq, 343 irq_hw_number_t first_hwirq, 344 const struct irq_domain_ops *ops, 345 void *host_data) 346 { 347 struct irq_domain *domain; 348 349 domain = __irq_domain_add(of_node_to_fwnode(of_node), first_hwirq + size, 350 first_hwirq + size, 0, ops, host_data); 351 if (domain) 352 irq_domain_associate_many(domain, first_irq, first_hwirq, size); 353 354 return domain; 355 } 356 EXPORT_SYMBOL_GPL(irq_domain_add_legacy); 357 358 /** 359 * irq_find_matching_fwspec() - Locates a domain for a given fwspec 360 * @fwspec: FW specifier for an interrupt 361 * @bus_token: domain-specific data 362 */ 363 struct irq_domain *irq_find_matching_fwspec(struct irq_fwspec *fwspec, 364 enum irq_domain_bus_token bus_token) 365 { 366 struct irq_domain *h, *found = NULL; 367 struct fwnode_handle *fwnode = fwspec->fwnode; 368 int rc; 369 370 /* We might want to match the legacy controller last since 371 * it might potentially be set to match all interrupts in 372 * the absence of a device node. This isn't a problem so far 373 * yet though... 374 * 375 * bus_token == DOMAIN_BUS_ANY matches any domain, any other 376 * values must generate an exact match for the domain to be 377 * selected. 378 */ 379 mutex_lock(&irq_domain_mutex); 380 list_for_each_entry(h, &irq_domain_list, link) { 381 if (h->ops->select && fwspec->param_count) 382 rc = h->ops->select(h, fwspec, bus_token); 383 else if (h->ops->match) 384 rc = h->ops->match(h, to_of_node(fwnode), bus_token); 385 else 386 rc = ((fwnode != NULL) && (h->fwnode == fwnode) && 387 ((bus_token == DOMAIN_BUS_ANY) || 388 (h->bus_token == bus_token))); 389 390 if (rc) { 391 found = h; 392 break; 393 } 394 } 395 mutex_unlock(&irq_domain_mutex); 396 return found; 397 } 398 EXPORT_SYMBOL_GPL(irq_find_matching_fwspec); 399 400 /** 401 * irq_domain_check_msi_remap - Check whether all MSI irq domains implement 402 * IRQ remapping 403 * 404 * Return: false if any MSI irq domain does not support IRQ remapping, 405 * true otherwise (including if there is no MSI irq domain) 406 */ 407 bool irq_domain_check_msi_remap(void) 408 { 409 struct irq_domain *h; 410 bool ret = true; 411 412 mutex_lock(&irq_domain_mutex); 413 list_for_each_entry(h, &irq_domain_list, link) { 414 if (irq_domain_is_msi(h) && 415 !irq_domain_hierarchical_is_msi_remap(h)) { 416 ret = false; 417 break; 418 } 419 } 420 mutex_unlock(&irq_domain_mutex); 421 return ret; 422 } 423 EXPORT_SYMBOL_GPL(irq_domain_check_msi_remap); 424 425 /** 426 * irq_set_default_host() - Set a "default" irq domain 427 * @domain: default domain pointer 428 * 429 * For convenience, it's possible to set a "default" domain that will be used 430 * whenever NULL is passed to irq_create_mapping(). It makes life easier for 431 * platforms that want to manipulate a few hard coded interrupt numbers that 432 * aren't properly represented in the device-tree. 433 */ 434 void irq_set_default_host(struct irq_domain *domain) 435 { 436 pr_debug("Default domain set to @0x%p\n", domain); 437 438 irq_default_domain = domain; 439 } 440 EXPORT_SYMBOL_GPL(irq_set_default_host); 441 442 void irq_domain_disassociate(struct irq_domain *domain, unsigned int irq) 443 { 444 struct irq_data *irq_data = irq_get_irq_data(irq); 445 irq_hw_number_t hwirq; 446 447 if (WARN(!irq_data || irq_data->domain != domain, 448 "virq%i doesn't exist; cannot disassociate\n", irq)) 449 return; 450 451 hwirq = irq_data->hwirq; 452 irq_set_status_flags(irq, IRQ_NOREQUEST); 453 454 /* remove chip and handler */ 455 irq_set_chip_and_handler(irq, NULL, NULL); 456 457 /* Make sure it's completed */ 458 synchronize_irq(irq); 459 460 /* Tell the PIC about it */ 461 if (domain->ops->unmap) 462 domain->ops->unmap(domain, irq); 463 smp_mb(); 464 465 irq_data->domain = NULL; 466 irq_data->hwirq = 0; 467 domain->mapcount--; 468 469 /* Clear reverse map for this hwirq */ 470 if (hwirq < domain->revmap_size) { 471 domain->linear_revmap[hwirq] = 0; 472 } else { 473 mutex_lock(&revmap_trees_mutex); 474 radix_tree_delete(&domain->revmap_tree, hwirq); 475 mutex_unlock(&revmap_trees_mutex); 476 } 477 } 478 479 int irq_domain_associate(struct irq_domain *domain, unsigned int virq, 480 irq_hw_number_t hwirq) 481 { 482 struct irq_data *irq_data = irq_get_irq_data(virq); 483 int ret; 484 485 if (WARN(hwirq >= domain->hwirq_max, 486 "error: hwirq 0x%x is too large for %s\n", (int)hwirq, domain->name)) 487 return -EINVAL; 488 if (WARN(!irq_data, "error: virq%i is not allocated", virq)) 489 return -EINVAL; 490 if (WARN(irq_data->domain, "error: virq%i is already associated", virq)) 491 return -EINVAL; 492 493 mutex_lock(&irq_domain_mutex); 494 irq_data->hwirq = hwirq; 495 irq_data->domain = domain; 496 if (domain->ops->map) { 497 ret = domain->ops->map(domain, virq, hwirq); 498 if (ret != 0) { 499 /* 500 * If map() returns -EPERM, this interrupt is protected 501 * by the firmware or some other service and shall not 502 * be mapped. Don't bother telling the user about it. 503 */ 504 if (ret != -EPERM) { 505 pr_info("%s didn't like hwirq-0x%lx to VIRQ%i mapping (rc=%d)\n", 506 domain->name, hwirq, virq, ret); 507 } 508 irq_data->domain = NULL; 509 irq_data->hwirq = 0; 510 mutex_unlock(&irq_domain_mutex); 511 return ret; 512 } 513 514 /* If not already assigned, give the domain the chip's name */ 515 if (!domain->name && irq_data->chip) 516 domain->name = irq_data->chip->name; 517 } 518 519 domain->mapcount++; 520 if (hwirq < domain->revmap_size) { 521 domain->linear_revmap[hwirq] = virq; 522 } else { 523 mutex_lock(&revmap_trees_mutex); 524 radix_tree_insert(&domain->revmap_tree, hwirq, irq_data); 525 mutex_unlock(&revmap_trees_mutex); 526 } 527 mutex_unlock(&irq_domain_mutex); 528 529 irq_clear_status_flags(virq, IRQ_NOREQUEST); 530 531 return 0; 532 } 533 EXPORT_SYMBOL_GPL(irq_domain_associate); 534 535 void irq_domain_associate_many(struct irq_domain *domain, unsigned int irq_base, 536 irq_hw_number_t hwirq_base, int count) 537 { 538 struct device_node *of_node; 539 int i; 540 541 of_node = irq_domain_get_of_node(domain); 542 pr_debug("%s(%s, irqbase=%i, hwbase=%i, count=%i)\n", __func__, 543 of_node_full_name(of_node), irq_base, (int)hwirq_base, count); 544 545 for (i = 0; i < count; i++) { 546 irq_domain_associate(domain, irq_base + i, hwirq_base + i); 547 } 548 } 549 EXPORT_SYMBOL_GPL(irq_domain_associate_many); 550 551 /** 552 * irq_create_direct_mapping() - Allocate an irq for direct mapping 553 * @domain: domain to allocate the irq for or NULL for default domain 554 * 555 * This routine is used for irq controllers which can choose the hardware 556 * interrupt numbers they generate. In such a case it's simplest to use 557 * the linux irq as the hardware interrupt number. It still uses the linear 558 * or radix tree to store the mapping, but the irq controller can optimize 559 * the revmap path by using the hwirq directly. 560 */ 561 unsigned int irq_create_direct_mapping(struct irq_domain *domain) 562 { 563 struct device_node *of_node; 564 unsigned int virq; 565 566 if (domain == NULL) 567 domain = irq_default_domain; 568 569 of_node = irq_domain_get_of_node(domain); 570 virq = irq_alloc_desc_from(1, of_node_to_nid(of_node)); 571 if (!virq) { 572 pr_debug("create_direct virq allocation failed\n"); 573 return 0; 574 } 575 if (virq >= domain->revmap_direct_max_irq) { 576 pr_err("ERROR: no free irqs available below %i maximum\n", 577 domain->revmap_direct_max_irq); 578 irq_free_desc(virq); 579 return 0; 580 } 581 pr_debug("create_direct obtained virq %d\n", virq); 582 583 if (irq_domain_associate(domain, virq, virq)) { 584 irq_free_desc(virq); 585 return 0; 586 } 587 588 return virq; 589 } 590 EXPORT_SYMBOL_GPL(irq_create_direct_mapping); 591 592 /** 593 * irq_create_mapping() - Map a hardware interrupt into linux irq space 594 * @domain: domain owning this hardware interrupt or NULL for default domain 595 * @hwirq: hardware irq number in that domain space 596 * 597 * Only one mapping per hardware interrupt is permitted. Returns a linux 598 * irq number. 599 * If the sense/trigger is to be specified, set_irq_type() should be called 600 * on the number returned from that call. 601 */ 602 unsigned int irq_create_mapping(struct irq_domain *domain, 603 irq_hw_number_t hwirq) 604 { 605 struct device_node *of_node; 606 int virq; 607 608 pr_debug("irq_create_mapping(0x%p, 0x%lx)\n", domain, hwirq); 609 610 /* Look for default domain if nececssary */ 611 if (domain == NULL) 612 domain = irq_default_domain; 613 if (domain == NULL) { 614 WARN(1, "%s(, %lx) called with NULL domain\n", __func__, hwirq); 615 return 0; 616 } 617 pr_debug("-> using domain @%p\n", domain); 618 619 of_node = irq_domain_get_of_node(domain); 620 621 /* Check if mapping already exists */ 622 virq = irq_find_mapping(domain, hwirq); 623 if (virq) { 624 pr_debug("-> existing mapping on virq %d\n", virq); 625 return virq; 626 } 627 628 /* Allocate a virtual interrupt number */ 629 virq = irq_domain_alloc_descs(-1, 1, hwirq, of_node_to_nid(of_node), NULL); 630 if (virq <= 0) { 631 pr_debug("-> virq allocation failed\n"); 632 return 0; 633 } 634 635 if (irq_domain_associate(domain, virq, hwirq)) { 636 irq_free_desc(virq); 637 return 0; 638 } 639 640 pr_debug("irq %lu on domain %s mapped to virtual irq %u\n", 641 hwirq, of_node_full_name(of_node), virq); 642 643 return virq; 644 } 645 EXPORT_SYMBOL_GPL(irq_create_mapping); 646 647 /** 648 * irq_create_strict_mappings() - Map a range of hw irqs to fixed linux irqs 649 * @domain: domain owning the interrupt range 650 * @irq_base: beginning of linux IRQ range 651 * @hwirq_base: beginning of hardware IRQ range 652 * @count: Number of interrupts to map 653 * 654 * This routine is used for allocating and mapping a range of hardware 655 * irqs to linux irqs where the linux irq numbers are at pre-defined 656 * locations. For use by controllers that already have static mappings 657 * to insert in to the domain. 658 * 659 * Non-linear users can use irq_create_identity_mapping() for IRQ-at-a-time 660 * domain insertion. 661 * 662 * 0 is returned upon success, while any failure to establish a static 663 * mapping is treated as an error. 664 */ 665 int irq_create_strict_mappings(struct irq_domain *domain, unsigned int irq_base, 666 irq_hw_number_t hwirq_base, int count) 667 { 668 struct device_node *of_node; 669 int ret; 670 671 of_node = irq_domain_get_of_node(domain); 672 ret = irq_alloc_descs(irq_base, irq_base, count, 673 of_node_to_nid(of_node)); 674 if (unlikely(ret < 0)) 675 return ret; 676 677 irq_domain_associate_many(domain, irq_base, hwirq_base, count); 678 return 0; 679 } 680 EXPORT_SYMBOL_GPL(irq_create_strict_mappings); 681 682 static int irq_domain_translate(struct irq_domain *d, 683 struct irq_fwspec *fwspec, 684 irq_hw_number_t *hwirq, unsigned int *type) 685 { 686 #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY 687 if (d->ops->translate) 688 return d->ops->translate(d, fwspec, hwirq, type); 689 #endif 690 if (d->ops->xlate) 691 return d->ops->xlate(d, to_of_node(fwspec->fwnode), 692 fwspec->param, fwspec->param_count, 693 hwirq, type); 694 695 /* If domain has no translation, then we assume interrupt line */ 696 *hwirq = fwspec->param[0]; 697 return 0; 698 } 699 700 static void of_phandle_args_to_fwspec(struct of_phandle_args *irq_data, 701 struct irq_fwspec *fwspec) 702 { 703 int i; 704 705 fwspec->fwnode = irq_data->np ? &irq_data->np->fwnode : NULL; 706 fwspec->param_count = irq_data->args_count; 707 708 for (i = 0; i < irq_data->args_count; i++) 709 fwspec->param[i] = irq_data->args[i]; 710 } 711 712 unsigned int irq_create_fwspec_mapping(struct irq_fwspec *fwspec) 713 { 714 struct irq_domain *domain; 715 struct irq_data *irq_data; 716 irq_hw_number_t hwirq; 717 unsigned int type = IRQ_TYPE_NONE; 718 int virq; 719 720 if (fwspec->fwnode) { 721 domain = irq_find_matching_fwspec(fwspec, DOMAIN_BUS_WIRED); 722 if (!domain) 723 domain = irq_find_matching_fwspec(fwspec, DOMAIN_BUS_ANY); 724 } else { 725 domain = irq_default_domain; 726 } 727 728 if (!domain) { 729 pr_warn("no irq domain found for %s !\n", 730 of_node_full_name(to_of_node(fwspec->fwnode))); 731 return 0; 732 } 733 734 if (irq_domain_translate(domain, fwspec, &hwirq, &type)) 735 return 0; 736 737 /* 738 * WARN if the irqchip returns a type with bits 739 * outside the sense mask set and clear these bits. 740 */ 741 if (WARN_ON(type & ~IRQ_TYPE_SENSE_MASK)) 742 type &= IRQ_TYPE_SENSE_MASK; 743 744 /* 745 * If we've already configured this interrupt, 746 * don't do it again, or hell will break loose. 747 */ 748 virq = irq_find_mapping(domain, hwirq); 749 if (virq) { 750 /* 751 * If the trigger type is not specified or matches the 752 * current trigger type then we are done so return the 753 * interrupt number. 754 */ 755 if (type == IRQ_TYPE_NONE || type == irq_get_trigger_type(virq)) 756 return virq; 757 758 /* 759 * If the trigger type has not been set yet, then set 760 * it now and return the interrupt number. 761 */ 762 if (irq_get_trigger_type(virq) == IRQ_TYPE_NONE) { 763 irq_data = irq_get_irq_data(virq); 764 if (!irq_data) 765 return 0; 766 767 irqd_set_trigger_type(irq_data, type); 768 return virq; 769 } 770 771 pr_warn("type mismatch, failed to map hwirq-%lu for %s!\n", 772 hwirq, of_node_full_name(to_of_node(fwspec->fwnode))); 773 return 0; 774 } 775 776 if (irq_domain_is_hierarchy(domain)) { 777 virq = irq_domain_alloc_irqs(domain, 1, NUMA_NO_NODE, fwspec); 778 if (virq <= 0) 779 return 0; 780 } else { 781 /* Create mapping */ 782 virq = irq_create_mapping(domain, hwirq); 783 if (!virq) 784 return virq; 785 } 786 787 irq_data = irq_get_irq_data(virq); 788 if (!irq_data) { 789 if (irq_domain_is_hierarchy(domain)) 790 irq_domain_free_irqs(virq, 1); 791 else 792 irq_dispose_mapping(virq); 793 return 0; 794 } 795 796 /* Store trigger type */ 797 irqd_set_trigger_type(irq_data, type); 798 799 return virq; 800 } 801 EXPORT_SYMBOL_GPL(irq_create_fwspec_mapping); 802 803 unsigned int irq_create_of_mapping(struct of_phandle_args *irq_data) 804 { 805 struct irq_fwspec fwspec; 806 807 of_phandle_args_to_fwspec(irq_data, &fwspec); 808 return irq_create_fwspec_mapping(&fwspec); 809 } 810 EXPORT_SYMBOL_GPL(irq_create_of_mapping); 811 812 /** 813 * irq_dispose_mapping() - Unmap an interrupt 814 * @virq: linux irq number of the interrupt to unmap 815 */ 816 void irq_dispose_mapping(unsigned int virq) 817 { 818 struct irq_data *irq_data = irq_get_irq_data(virq); 819 struct irq_domain *domain; 820 821 if (!virq || !irq_data) 822 return; 823 824 domain = irq_data->domain; 825 if (WARN_ON(domain == NULL)) 826 return; 827 828 if (irq_domain_is_hierarchy(domain)) { 829 irq_domain_free_irqs(virq, 1); 830 } else { 831 irq_domain_disassociate(domain, virq); 832 irq_free_desc(virq); 833 } 834 } 835 EXPORT_SYMBOL_GPL(irq_dispose_mapping); 836 837 /** 838 * irq_find_mapping() - Find a linux irq from an hw irq number. 839 * @domain: domain owning this hardware interrupt 840 * @hwirq: hardware irq number in that domain space 841 */ 842 unsigned int irq_find_mapping(struct irq_domain *domain, 843 irq_hw_number_t hwirq) 844 { 845 struct irq_data *data; 846 847 /* Look for default domain if nececssary */ 848 if (domain == NULL) 849 domain = irq_default_domain; 850 if (domain == NULL) 851 return 0; 852 853 if (hwirq < domain->revmap_direct_max_irq) { 854 data = irq_domain_get_irq_data(domain, hwirq); 855 if (data && data->hwirq == hwirq) 856 return hwirq; 857 } 858 859 /* Check if the hwirq is in the linear revmap. */ 860 if (hwirq < domain->revmap_size) 861 return domain->linear_revmap[hwirq]; 862 863 rcu_read_lock(); 864 data = radix_tree_lookup(&domain->revmap_tree, hwirq); 865 rcu_read_unlock(); 866 return data ? data->irq : 0; 867 } 868 EXPORT_SYMBOL_GPL(irq_find_mapping); 869 870 #ifdef CONFIG_IRQ_DOMAIN_DEBUG 871 static void virq_debug_show_one(struct seq_file *m, struct irq_desc *desc) 872 { 873 struct irq_domain *domain; 874 struct irq_data *data; 875 876 domain = desc->irq_data.domain; 877 data = &desc->irq_data; 878 879 while (domain) { 880 unsigned int irq = data->irq; 881 unsigned long hwirq = data->hwirq; 882 struct irq_chip *chip; 883 bool direct; 884 885 if (data == &desc->irq_data) 886 seq_printf(m, "%5d ", irq); 887 else 888 seq_printf(m, "%5d+ ", irq); 889 seq_printf(m, "0x%05lx ", hwirq); 890 891 chip = irq_data_get_irq_chip(data); 892 seq_printf(m, "%-15s ", (chip && chip->name) ? chip->name : "none"); 893 894 seq_printf(m, data ? "0x%p " : " %p ", 895 irq_data_get_irq_chip_data(data)); 896 897 seq_printf(m, " %c ", (desc->action && desc->action->handler) ? '*' : ' '); 898 direct = (irq == hwirq) && (irq < domain->revmap_direct_max_irq); 899 seq_printf(m, "%6s%-8s ", 900 (hwirq < domain->revmap_size) ? "LINEAR" : "RADIX", 901 direct ? "(DIRECT)" : ""); 902 seq_printf(m, "%s\n", domain->name); 903 #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY 904 domain = domain->parent; 905 data = data->parent_data; 906 #else 907 domain = NULL; 908 #endif 909 } 910 } 911 912 static int virq_debug_show(struct seq_file *m, void *private) 913 { 914 unsigned long flags; 915 struct irq_desc *desc; 916 struct irq_domain *domain; 917 struct radix_tree_iter iter; 918 void **slot; 919 int i; 920 921 seq_printf(m, " %-16s %-6s %-10s %-10s %s\n", 922 "name", "mapped", "linear-max", "direct-max", "devtree-node"); 923 mutex_lock(&irq_domain_mutex); 924 list_for_each_entry(domain, &irq_domain_list, link) { 925 struct device_node *of_node; 926 const char *name; 927 928 int count = 0; 929 930 of_node = irq_domain_get_of_node(domain); 931 if (of_node) 932 name = of_node_full_name(of_node); 933 else if (is_fwnode_irqchip(domain->fwnode)) 934 name = container_of(domain->fwnode, struct irqchip_fwid, 935 fwnode)->name; 936 else 937 name = ""; 938 939 radix_tree_for_each_slot(slot, &domain->revmap_tree, &iter, 0) 940 count++; 941 seq_printf(m, "%c%-16s %6u %10u %10u %s\n", 942 domain == irq_default_domain ? '*' : ' ', domain->name, 943 domain->revmap_size + count, domain->revmap_size, 944 domain->revmap_direct_max_irq, 945 name); 946 } 947 mutex_unlock(&irq_domain_mutex); 948 949 seq_printf(m, "%-5s %-7s %-15s %-*s %6s %-14s %s\n", "irq", "hwirq", 950 "chip name", (int)(2 * sizeof(void *) + 2), "chip data", 951 "active", "type", "domain"); 952 953 for (i = 1; i < nr_irqs; i++) { 954 desc = irq_to_desc(i); 955 if (!desc) 956 continue; 957 958 raw_spin_lock_irqsave(&desc->lock, flags); 959 virq_debug_show_one(m, desc); 960 raw_spin_unlock_irqrestore(&desc->lock, flags); 961 } 962 963 return 0; 964 } 965 966 static int virq_debug_open(struct inode *inode, struct file *file) 967 { 968 return single_open(file, virq_debug_show, inode->i_private); 969 } 970 971 static const struct file_operations virq_debug_fops = { 972 .open = virq_debug_open, 973 .read = seq_read, 974 .llseek = seq_lseek, 975 .release = single_release, 976 }; 977 978 static int __init irq_debugfs_init(void) 979 { 980 if (debugfs_create_file("irq_domain_mapping", S_IRUGO, NULL, 981 NULL, &virq_debug_fops) == NULL) 982 return -ENOMEM; 983 984 return 0; 985 } 986 __initcall(irq_debugfs_init); 987 #endif /* CONFIG_IRQ_DOMAIN_DEBUG */ 988 989 /** 990 * irq_domain_xlate_onecell() - Generic xlate for direct one cell bindings 991 * 992 * Device Tree IRQ specifier translation function which works with one cell 993 * bindings where the cell value maps directly to the hwirq number. 994 */ 995 int irq_domain_xlate_onecell(struct irq_domain *d, struct device_node *ctrlr, 996 const u32 *intspec, unsigned int intsize, 997 unsigned long *out_hwirq, unsigned int *out_type) 998 { 999 if (WARN_ON(intsize < 1)) 1000 return -EINVAL; 1001 *out_hwirq = intspec[0]; 1002 *out_type = IRQ_TYPE_NONE; 1003 return 0; 1004 } 1005 EXPORT_SYMBOL_GPL(irq_domain_xlate_onecell); 1006 1007 /** 1008 * irq_domain_xlate_twocell() - Generic xlate for direct two cell bindings 1009 * 1010 * Device Tree IRQ specifier translation function which works with two cell 1011 * bindings where the cell values map directly to the hwirq number 1012 * and linux irq flags. 1013 */ 1014 int irq_domain_xlate_twocell(struct irq_domain *d, struct device_node *ctrlr, 1015 const u32 *intspec, unsigned int intsize, 1016 irq_hw_number_t *out_hwirq, unsigned int *out_type) 1017 { 1018 if (WARN_ON(intsize < 2)) 1019 return -EINVAL; 1020 *out_hwirq = intspec[0]; 1021 *out_type = intspec[1] & IRQ_TYPE_SENSE_MASK; 1022 return 0; 1023 } 1024 EXPORT_SYMBOL_GPL(irq_domain_xlate_twocell); 1025 1026 /** 1027 * irq_domain_xlate_onetwocell() - Generic xlate for one or two cell bindings 1028 * 1029 * Device Tree IRQ specifier translation function which works with either one 1030 * or two cell bindings where the cell values map directly to the hwirq number 1031 * and linux irq flags. 1032 * 1033 * Note: don't use this function unless your interrupt controller explicitly 1034 * supports both one and two cell bindings. For the majority of controllers 1035 * the _onecell() or _twocell() variants above should be used. 1036 */ 1037 int irq_domain_xlate_onetwocell(struct irq_domain *d, 1038 struct device_node *ctrlr, 1039 const u32 *intspec, unsigned int intsize, 1040 unsigned long *out_hwirq, unsigned int *out_type) 1041 { 1042 if (WARN_ON(intsize < 1)) 1043 return -EINVAL; 1044 *out_hwirq = intspec[0]; 1045 if (intsize > 1) 1046 *out_type = intspec[1] & IRQ_TYPE_SENSE_MASK; 1047 else 1048 *out_type = IRQ_TYPE_NONE; 1049 return 0; 1050 } 1051 EXPORT_SYMBOL_GPL(irq_domain_xlate_onetwocell); 1052 1053 const struct irq_domain_ops irq_domain_simple_ops = { 1054 .xlate = irq_domain_xlate_onetwocell, 1055 }; 1056 EXPORT_SYMBOL_GPL(irq_domain_simple_ops); 1057 1058 int irq_domain_alloc_descs(int virq, unsigned int cnt, irq_hw_number_t hwirq, 1059 int node, const struct cpumask *affinity) 1060 { 1061 unsigned int hint; 1062 1063 if (virq >= 0) { 1064 virq = __irq_alloc_descs(virq, virq, cnt, node, THIS_MODULE, 1065 affinity); 1066 } else { 1067 hint = hwirq % nr_irqs; 1068 if (hint == 0) 1069 hint++; 1070 virq = __irq_alloc_descs(-1, hint, cnt, node, THIS_MODULE, 1071 affinity); 1072 if (virq <= 0 && hint > 1) { 1073 virq = __irq_alloc_descs(-1, 1, cnt, node, THIS_MODULE, 1074 affinity); 1075 } 1076 } 1077 1078 return virq; 1079 } 1080 1081 #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY 1082 /** 1083 * irq_domain_create_hierarchy - Add a irqdomain into the hierarchy 1084 * @parent: Parent irq domain to associate with the new domain 1085 * @flags: Irq domain flags associated to the domain 1086 * @size: Size of the domain. See below 1087 * @fwnode: Optional fwnode of the interrupt controller 1088 * @ops: Pointer to the interrupt domain callbacks 1089 * @host_data: Controller private data pointer 1090 * 1091 * If @size is 0 a tree domain is created, otherwise a linear domain. 1092 * 1093 * If successful the parent is associated to the new domain and the 1094 * domain flags are set. 1095 * Returns pointer to IRQ domain, or NULL on failure. 1096 */ 1097 struct irq_domain *irq_domain_create_hierarchy(struct irq_domain *parent, 1098 unsigned int flags, 1099 unsigned int size, 1100 struct fwnode_handle *fwnode, 1101 const struct irq_domain_ops *ops, 1102 void *host_data) 1103 { 1104 struct irq_domain *domain; 1105 1106 if (size) 1107 domain = irq_domain_create_linear(fwnode, size, ops, host_data); 1108 else 1109 domain = irq_domain_create_tree(fwnode, ops, host_data); 1110 if (domain) { 1111 domain->parent = parent; 1112 domain->flags |= flags; 1113 } 1114 1115 return domain; 1116 } 1117 EXPORT_SYMBOL_GPL(irq_domain_create_hierarchy); 1118 1119 static void irq_domain_insert_irq(int virq) 1120 { 1121 struct irq_data *data; 1122 1123 for (data = irq_get_irq_data(virq); data; data = data->parent_data) { 1124 struct irq_domain *domain = data->domain; 1125 irq_hw_number_t hwirq = data->hwirq; 1126 1127 domain->mapcount++; 1128 if (hwirq < domain->revmap_size) { 1129 domain->linear_revmap[hwirq] = virq; 1130 } else { 1131 mutex_lock(&revmap_trees_mutex); 1132 radix_tree_insert(&domain->revmap_tree, hwirq, data); 1133 mutex_unlock(&revmap_trees_mutex); 1134 } 1135 1136 /* If not already assigned, give the domain the chip's name */ 1137 if (!domain->name && data->chip) 1138 domain->name = data->chip->name; 1139 } 1140 1141 irq_clear_status_flags(virq, IRQ_NOREQUEST); 1142 } 1143 1144 static void irq_domain_remove_irq(int virq) 1145 { 1146 struct irq_data *data; 1147 1148 irq_set_status_flags(virq, IRQ_NOREQUEST); 1149 irq_set_chip_and_handler(virq, NULL, NULL); 1150 synchronize_irq(virq); 1151 smp_mb(); 1152 1153 for (data = irq_get_irq_data(virq); data; data = data->parent_data) { 1154 struct irq_domain *domain = data->domain; 1155 irq_hw_number_t hwirq = data->hwirq; 1156 1157 domain->mapcount--; 1158 if (hwirq < domain->revmap_size) { 1159 domain->linear_revmap[hwirq] = 0; 1160 } else { 1161 mutex_lock(&revmap_trees_mutex); 1162 radix_tree_delete(&domain->revmap_tree, hwirq); 1163 mutex_unlock(&revmap_trees_mutex); 1164 } 1165 } 1166 } 1167 1168 static struct irq_data *irq_domain_insert_irq_data(struct irq_domain *domain, 1169 struct irq_data *child) 1170 { 1171 struct irq_data *irq_data; 1172 1173 irq_data = kzalloc_node(sizeof(*irq_data), GFP_KERNEL, 1174 irq_data_get_node(child)); 1175 if (irq_data) { 1176 child->parent_data = irq_data; 1177 irq_data->irq = child->irq; 1178 irq_data->common = child->common; 1179 irq_data->domain = domain; 1180 } 1181 1182 return irq_data; 1183 } 1184 1185 static void irq_domain_free_irq_data(unsigned int virq, unsigned int nr_irqs) 1186 { 1187 struct irq_data *irq_data, *tmp; 1188 int i; 1189 1190 for (i = 0; i < nr_irqs; i++) { 1191 irq_data = irq_get_irq_data(virq + i); 1192 tmp = irq_data->parent_data; 1193 irq_data->parent_data = NULL; 1194 irq_data->domain = NULL; 1195 1196 while (tmp) { 1197 irq_data = tmp; 1198 tmp = tmp->parent_data; 1199 kfree(irq_data); 1200 } 1201 } 1202 } 1203 1204 static int irq_domain_alloc_irq_data(struct irq_domain *domain, 1205 unsigned int virq, unsigned int nr_irqs) 1206 { 1207 struct irq_data *irq_data; 1208 struct irq_domain *parent; 1209 int i; 1210 1211 /* The outermost irq_data is embedded in struct irq_desc */ 1212 for (i = 0; i < nr_irqs; i++) { 1213 irq_data = irq_get_irq_data(virq + i); 1214 irq_data->domain = domain; 1215 1216 for (parent = domain->parent; parent; parent = parent->parent) { 1217 irq_data = irq_domain_insert_irq_data(parent, irq_data); 1218 if (!irq_data) { 1219 irq_domain_free_irq_data(virq, i + 1); 1220 return -ENOMEM; 1221 } 1222 } 1223 } 1224 1225 return 0; 1226 } 1227 1228 /** 1229 * irq_domain_get_irq_data - Get irq_data associated with @virq and @domain 1230 * @domain: domain to match 1231 * @virq: IRQ number to get irq_data 1232 */ 1233 struct irq_data *irq_domain_get_irq_data(struct irq_domain *domain, 1234 unsigned int virq) 1235 { 1236 struct irq_data *irq_data; 1237 1238 for (irq_data = irq_get_irq_data(virq); irq_data; 1239 irq_data = irq_data->parent_data) 1240 if (irq_data->domain == domain) 1241 return irq_data; 1242 1243 return NULL; 1244 } 1245 EXPORT_SYMBOL_GPL(irq_domain_get_irq_data); 1246 1247 /** 1248 * irq_domain_set_hwirq_and_chip - Set hwirq and irqchip of @virq at @domain 1249 * @domain: Interrupt domain to match 1250 * @virq: IRQ number 1251 * @hwirq: The hwirq number 1252 * @chip: The associated interrupt chip 1253 * @chip_data: The associated chip data 1254 */ 1255 int irq_domain_set_hwirq_and_chip(struct irq_domain *domain, unsigned int virq, 1256 irq_hw_number_t hwirq, struct irq_chip *chip, 1257 void *chip_data) 1258 { 1259 struct irq_data *irq_data = irq_domain_get_irq_data(domain, virq); 1260 1261 if (!irq_data) 1262 return -ENOENT; 1263 1264 irq_data->hwirq = hwirq; 1265 irq_data->chip = chip ? chip : &no_irq_chip; 1266 irq_data->chip_data = chip_data; 1267 1268 return 0; 1269 } 1270 EXPORT_SYMBOL_GPL(irq_domain_set_hwirq_and_chip); 1271 1272 /** 1273 * irq_domain_set_info - Set the complete data for a @virq in @domain 1274 * @domain: Interrupt domain to match 1275 * @virq: IRQ number 1276 * @hwirq: The hardware interrupt number 1277 * @chip: The associated interrupt chip 1278 * @chip_data: The associated interrupt chip data 1279 * @handler: The interrupt flow handler 1280 * @handler_data: The interrupt flow handler data 1281 * @handler_name: The interrupt handler name 1282 */ 1283 void irq_domain_set_info(struct irq_domain *domain, unsigned int virq, 1284 irq_hw_number_t hwirq, struct irq_chip *chip, 1285 void *chip_data, irq_flow_handler_t handler, 1286 void *handler_data, const char *handler_name) 1287 { 1288 irq_domain_set_hwirq_and_chip(domain, virq, hwirq, chip, chip_data); 1289 __irq_set_handler(virq, handler, 0, handler_name); 1290 irq_set_handler_data(virq, handler_data); 1291 } 1292 EXPORT_SYMBOL(irq_domain_set_info); 1293 1294 /** 1295 * irq_domain_reset_irq_data - Clear hwirq, chip and chip_data in @irq_data 1296 * @irq_data: The pointer to irq_data 1297 */ 1298 void irq_domain_reset_irq_data(struct irq_data *irq_data) 1299 { 1300 irq_data->hwirq = 0; 1301 irq_data->chip = &no_irq_chip; 1302 irq_data->chip_data = NULL; 1303 } 1304 EXPORT_SYMBOL_GPL(irq_domain_reset_irq_data); 1305 1306 /** 1307 * irq_domain_free_irqs_common - Clear irq_data and free the parent 1308 * @domain: Interrupt domain to match 1309 * @virq: IRQ number to start with 1310 * @nr_irqs: The number of irqs to free 1311 */ 1312 void irq_domain_free_irqs_common(struct irq_domain *domain, unsigned int virq, 1313 unsigned int nr_irqs) 1314 { 1315 struct irq_data *irq_data; 1316 int i; 1317 1318 for (i = 0; i < nr_irqs; i++) { 1319 irq_data = irq_domain_get_irq_data(domain, virq + i); 1320 if (irq_data) 1321 irq_domain_reset_irq_data(irq_data); 1322 } 1323 irq_domain_free_irqs_parent(domain, virq, nr_irqs); 1324 } 1325 EXPORT_SYMBOL_GPL(irq_domain_free_irqs_common); 1326 1327 /** 1328 * irq_domain_free_irqs_top - Clear handler and handler data, clear irqdata and free parent 1329 * @domain: Interrupt domain to match 1330 * @virq: IRQ number to start with 1331 * @nr_irqs: The number of irqs to free 1332 */ 1333 void irq_domain_free_irqs_top(struct irq_domain *domain, unsigned int virq, 1334 unsigned int nr_irqs) 1335 { 1336 int i; 1337 1338 for (i = 0; i < nr_irqs; i++) { 1339 irq_set_handler_data(virq + i, NULL); 1340 irq_set_handler(virq + i, NULL); 1341 } 1342 irq_domain_free_irqs_common(domain, virq, nr_irqs); 1343 } 1344 1345 static void irq_domain_free_irqs_hierarchy(struct irq_domain *domain, 1346 unsigned int irq_base, 1347 unsigned int nr_irqs) 1348 { 1349 domain->ops->free(domain, irq_base, nr_irqs); 1350 } 1351 1352 int irq_domain_alloc_irqs_hierarchy(struct irq_domain *domain, 1353 unsigned int irq_base, 1354 unsigned int nr_irqs, void *arg) 1355 { 1356 return domain->ops->alloc(domain, irq_base, nr_irqs, arg); 1357 } 1358 1359 /** 1360 * __irq_domain_alloc_irqs - Allocate IRQs from domain 1361 * @domain: domain to allocate from 1362 * @irq_base: allocate specified IRQ nubmer if irq_base >= 0 1363 * @nr_irqs: number of IRQs to allocate 1364 * @node: NUMA node id for memory allocation 1365 * @arg: domain specific argument 1366 * @realloc: IRQ descriptors have already been allocated if true 1367 * @affinity: Optional irq affinity mask for multiqueue devices 1368 * 1369 * Allocate IRQ numbers and initialized all data structures to support 1370 * hierarchy IRQ domains. 1371 * Parameter @realloc is mainly to support legacy IRQs. 1372 * Returns error code or allocated IRQ number 1373 * 1374 * The whole process to setup an IRQ has been split into two steps. 1375 * The first step, __irq_domain_alloc_irqs(), is to allocate IRQ 1376 * descriptor and required hardware resources. The second step, 1377 * irq_domain_activate_irq(), is to program hardwares with preallocated 1378 * resources. In this way, it's easier to rollback when failing to 1379 * allocate resources. 1380 */ 1381 int __irq_domain_alloc_irqs(struct irq_domain *domain, int irq_base, 1382 unsigned int nr_irqs, int node, void *arg, 1383 bool realloc, const struct cpumask *affinity) 1384 { 1385 int i, ret, virq; 1386 1387 if (domain == NULL) { 1388 domain = irq_default_domain; 1389 if (WARN(!domain, "domain is NULL; cannot allocate IRQ\n")) 1390 return -EINVAL; 1391 } 1392 1393 if (!domain->ops->alloc) { 1394 pr_debug("domain->ops->alloc() is NULL\n"); 1395 return -ENOSYS; 1396 } 1397 1398 if (realloc && irq_base >= 0) { 1399 virq = irq_base; 1400 } else { 1401 virq = irq_domain_alloc_descs(irq_base, nr_irqs, 0, node, 1402 affinity); 1403 if (virq < 0) { 1404 pr_debug("cannot allocate IRQ(base %d, count %d)\n", 1405 irq_base, nr_irqs); 1406 return virq; 1407 } 1408 } 1409 1410 if (irq_domain_alloc_irq_data(domain, virq, nr_irqs)) { 1411 pr_debug("cannot allocate memory for IRQ%d\n", virq); 1412 ret = -ENOMEM; 1413 goto out_free_desc; 1414 } 1415 1416 mutex_lock(&irq_domain_mutex); 1417 ret = irq_domain_alloc_irqs_hierarchy(domain, virq, nr_irqs, arg); 1418 if (ret < 0) { 1419 mutex_unlock(&irq_domain_mutex); 1420 goto out_free_irq_data; 1421 } 1422 for (i = 0; i < nr_irqs; i++) 1423 irq_domain_insert_irq(virq + i); 1424 mutex_unlock(&irq_domain_mutex); 1425 1426 return virq; 1427 1428 out_free_irq_data: 1429 irq_domain_free_irq_data(virq, nr_irqs); 1430 out_free_desc: 1431 irq_free_descs(virq, nr_irqs); 1432 return ret; 1433 } 1434 1435 /** 1436 * irq_domain_free_irqs - Free IRQ number and associated data structures 1437 * @virq: base IRQ number 1438 * @nr_irqs: number of IRQs to free 1439 */ 1440 void irq_domain_free_irqs(unsigned int virq, unsigned int nr_irqs) 1441 { 1442 struct irq_data *data = irq_get_irq_data(virq); 1443 int i; 1444 1445 if (WARN(!data || !data->domain || !data->domain->ops->free, 1446 "NULL pointer, cannot free irq\n")) 1447 return; 1448 1449 mutex_lock(&irq_domain_mutex); 1450 for (i = 0; i < nr_irqs; i++) 1451 irq_domain_remove_irq(virq + i); 1452 irq_domain_free_irqs_hierarchy(data->domain, virq, nr_irqs); 1453 mutex_unlock(&irq_domain_mutex); 1454 1455 irq_domain_free_irq_data(virq, nr_irqs); 1456 irq_free_descs(virq, nr_irqs); 1457 } 1458 1459 /** 1460 * irq_domain_alloc_irqs_parent - Allocate interrupts from parent domain 1461 * @irq_base: Base IRQ number 1462 * @nr_irqs: Number of IRQs to allocate 1463 * @arg: Allocation data (arch/domain specific) 1464 * 1465 * Check whether the domain has been setup recursive. If not allocate 1466 * through the parent domain. 1467 */ 1468 int irq_domain_alloc_irqs_parent(struct irq_domain *domain, 1469 unsigned int irq_base, unsigned int nr_irqs, 1470 void *arg) 1471 { 1472 if (!domain->parent) 1473 return -ENOSYS; 1474 1475 return irq_domain_alloc_irqs_hierarchy(domain->parent, irq_base, 1476 nr_irqs, arg); 1477 } 1478 EXPORT_SYMBOL_GPL(irq_domain_alloc_irqs_parent); 1479 1480 /** 1481 * irq_domain_free_irqs_parent - Free interrupts from parent domain 1482 * @irq_base: Base IRQ number 1483 * @nr_irqs: Number of IRQs to free 1484 * 1485 * Check whether the domain has been setup recursive. If not free 1486 * through the parent domain. 1487 */ 1488 void irq_domain_free_irqs_parent(struct irq_domain *domain, 1489 unsigned int irq_base, unsigned int nr_irqs) 1490 { 1491 if (!domain->parent) 1492 return; 1493 1494 irq_domain_free_irqs_hierarchy(domain->parent, irq_base, nr_irqs); 1495 } 1496 EXPORT_SYMBOL_GPL(irq_domain_free_irqs_parent); 1497 1498 static void __irq_domain_activate_irq(struct irq_data *irq_data) 1499 { 1500 if (irq_data && irq_data->domain) { 1501 struct irq_domain *domain = irq_data->domain; 1502 1503 if (irq_data->parent_data) 1504 __irq_domain_activate_irq(irq_data->parent_data); 1505 if (domain->ops->activate) 1506 domain->ops->activate(domain, irq_data); 1507 } 1508 } 1509 1510 static void __irq_domain_deactivate_irq(struct irq_data *irq_data) 1511 { 1512 if (irq_data && irq_data->domain) { 1513 struct irq_domain *domain = irq_data->domain; 1514 1515 if (domain->ops->deactivate) 1516 domain->ops->deactivate(domain, irq_data); 1517 if (irq_data->parent_data) 1518 __irq_domain_deactivate_irq(irq_data->parent_data); 1519 } 1520 } 1521 1522 /** 1523 * irq_domain_activate_irq - Call domain_ops->activate recursively to activate 1524 * interrupt 1525 * @irq_data: outermost irq_data associated with interrupt 1526 * 1527 * This is the second step to call domain_ops->activate to program interrupt 1528 * controllers, so the interrupt could actually get delivered. 1529 */ 1530 void irq_domain_activate_irq(struct irq_data *irq_data) 1531 { 1532 if (!irqd_is_activated(irq_data)) { 1533 __irq_domain_activate_irq(irq_data); 1534 irqd_set_activated(irq_data); 1535 } 1536 } 1537 1538 /** 1539 * irq_domain_deactivate_irq - Call domain_ops->deactivate recursively to 1540 * deactivate interrupt 1541 * @irq_data: outermost irq_data associated with interrupt 1542 * 1543 * It calls domain_ops->deactivate to program interrupt controllers to disable 1544 * interrupt delivery. 1545 */ 1546 void irq_domain_deactivate_irq(struct irq_data *irq_data) 1547 { 1548 if (irqd_is_activated(irq_data)) { 1549 __irq_domain_deactivate_irq(irq_data); 1550 irqd_clr_activated(irq_data); 1551 } 1552 } 1553 1554 static void irq_domain_check_hierarchy(struct irq_domain *domain) 1555 { 1556 /* Hierarchy irq_domains must implement callback alloc() */ 1557 if (domain->ops->alloc) 1558 domain->flags |= IRQ_DOMAIN_FLAG_HIERARCHY; 1559 } 1560 1561 /** 1562 * irq_domain_hierarchical_is_msi_remap - Check if the domain or any 1563 * parent has MSI remapping support 1564 * @domain: domain pointer 1565 */ 1566 bool irq_domain_hierarchical_is_msi_remap(struct irq_domain *domain) 1567 { 1568 for (; domain; domain = domain->parent) { 1569 if (irq_domain_is_msi_remap(domain)) 1570 return true; 1571 } 1572 return false; 1573 } 1574 #else /* CONFIG_IRQ_DOMAIN_HIERARCHY */ 1575 /** 1576 * irq_domain_get_irq_data - Get irq_data associated with @virq and @domain 1577 * @domain: domain to match 1578 * @virq: IRQ number to get irq_data 1579 */ 1580 struct irq_data *irq_domain_get_irq_data(struct irq_domain *domain, 1581 unsigned int virq) 1582 { 1583 struct irq_data *irq_data = irq_get_irq_data(virq); 1584 1585 return (irq_data && irq_data->domain == domain) ? irq_data : NULL; 1586 } 1587 EXPORT_SYMBOL_GPL(irq_domain_get_irq_data); 1588 1589 /** 1590 * irq_domain_set_info - Set the complete data for a @virq in @domain 1591 * @domain: Interrupt domain to match 1592 * @virq: IRQ number 1593 * @hwirq: The hardware interrupt number 1594 * @chip: The associated interrupt chip 1595 * @chip_data: The associated interrupt chip data 1596 * @handler: The interrupt flow handler 1597 * @handler_data: The interrupt flow handler data 1598 * @handler_name: The interrupt handler name 1599 */ 1600 void irq_domain_set_info(struct irq_domain *domain, unsigned int virq, 1601 irq_hw_number_t hwirq, struct irq_chip *chip, 1602 void *chip_data, irq_flow_handler_t handler, 1603 void *handler_data, const char *handler_name) 1604 { 1605 irq_set_chip_and_handler_name(virq, chip, handler, handler_name); 1606 irq_set_chip_data(virq, chip_data); 1607 irq_set_handler_data(virq, handler_data); 1608 } 1609 1610 static void irq_domain_check_hierarchy(struct irq_domain *domain) 1611 { 1612 } 1613 #endif /* CONFIG_IRQ_DOMAIN_HIERARCHY */ 1614 1615 #ifdef CONFIG_GENERIC_IRQ_DEBUGFS 1616 static struct dentry *domain_dir; 1617 1618 static void 1619 irq_domain_debug_show_one(struct seq_file *m, struct irq_domain *d, int ind) 1620 { 1621 seq_printf(m, "%*sname: %s\n", ind, "", d->name); 1622 seq_printf(m, "%*ssize: %u\n", ind + 1, "", 1623 d->revmap_size + d->revmap_direct_max_irq); 1624 seq_printf(m, "%*smapped: %u\n", ind + 1, "", d->mapcount); 1625 seq_printf(m, "%*sflags: 0x%08x\n", ind +1 , "", d->flags); 1626 #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY 1627 if (!d->parent) 1628 return; 1629 seq_printf(m, "%*sparent: %s\n", ind + 1, "", d->parent->name); 1630 irq_domain_debug_show_one(m, d->parent, ind + 4); 1631 #endif 1632 } 1633 1634 static int irq_domain_debug_show(struct seq_file *m, void *p) 1635 { 1636 struct irq_domain *d = m->private; 1637 1638 /* Default domain? Might be NULL */ 1639 if (!d) { 1640 if (!irq_default_domain) 1641 return 0; 1642 d = irq_default_domain; 1643 } 1644 irq_domain_debug_show_one(m, d, 0); 1645 return 0; 1646 } 1647 1648 static int irq_domain_debug_open(struct inode *inode, struct file *file) 1649 { 1650 return single_open(file, irq_domain_debug_show, inode->i_private); 1651 } 1652 1653 static const struct file_operations dfs_domain_ops = { 1654 .open = irq_domain_debug_open, 1655 .read = seq_read, 1656 .llseek = seq_lseek, 1657 .release = single_release, 1658 }; 1659 1660 static void debugfs_add_domain_dir(struct irq_domain *d) 1661 { 1662 if (!d->name || !domain_dir || d->debugfs_file) 1663 return; 1664 d->debugfs_file = debugfs_create_file(d->name, 0444, domain_dir, d, 1665 &dfs_domain_ops); 1666 } 1667 1668 static void debugfs_remove_domain_dir(struct irq_domain *d) 1669 { 1670 if (d->debugfs_file) 1671 debugfs_remove(d->debugfs_file); 1672 } 1673 1674 void __init irq_domain_debugfs_init(struct dentry *root) 1675 { 1676 struct irq_domain *d; 1677 1678 domain_dir = debugfs_create_dir("domains", root); 1679 if (!domain_dir) 1680 return; 1681 1682 debugfs_create_file("default", 0444, domain_dir, NULL, &dfs_domain_ops); 1683 mutex_lock(&irq_domain_mutex); 1684 list_for_each_entry(d, &irq_domain_list, link) 1685 debugfs_add_domain_dir(d); 1686 mutex_unlock(&irq_domain_mutex); 1687 } 1688 #endif 1689