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