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