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