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