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