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