LCOV - code coverage report
Current view: top level - kernel/irq - irqdomain.c (source / functions) Hit Total Coverage
Test: landlock.info Lines: 165 679 24.3 %
Date: 2021-04-22 12:43:58 Functions: 16 60 26.7 %

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

Generated by: LCOV version 1.14