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