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