genirq: generic chip: Use DIV_ROUND_UP to calculate numchips
[platform/adaptation/renesas_rcar/renesas_kernel.git] / kernel / irq / irqdomain.c
1 #define pr_fmt(fmt)  "irq: " fmt
2
3 #include <linux/debugfs.h>
4 #include <linux/hardirq.h>
5 #include <linux/interrupt.h>
6 #include <linux/irq.h>
7 #include <linux/irqdesc.h>
8 #include <linux/irqdomain.h>
9 #include <linux/module.h>
10 #include <linux/mutex.h>
11 #include <linux/of.h>
12 #include <linux/of_address.h>
13 #include <linux/topology.h>
14 #include <linux/seq_file.h>
15 #include <linux/slab.h>
16 #include <linux/smp.h>
17 #include <linux/fs.h>
18
19 static LIST_HEAD(irq_domain_list);
20 static DEFINE_MUTEX(irq_domain_mutex);
21
22 static DEFINE_MUTEX(revmap_trees_mutex);
23 static struct irq_domain *irq_default_domain;
24
25 /**
26  * irq_domain_alloc() - Allocate a new irq_domain data structure
27  * @of_node: optional device-tree node of the interrupt controller
28  * @revmap_type: type of reverse mapping to use
29  * @ops: map/unmap domain callbacks
30  * @host_data: Controller private data pointer
31  *
32  * Allocates and initialize and irq_domain structure.  Caller is expected to
33  * register allocated irq_domain with irq_domain_register().  Returns pointer
34  * to IRQ domain, or NULL on failure.
35  */
36 static struct irq_domain *irq_domain_alloc(struct device_node *of_node,
37                                            unsigned int revmap_type,
38                                            const struct irq_domain_ops *ops,
39                                            void *host_data)
40 {
41         struct irq_domain *domain;
42
43         domain = kzalloc_node(sizeof(*domain), GFP_KERNEL,
44                               of_node_to_nid(of_node));
45         if (WARN_ON(!domain))
46                 return NULL;
47
48         /* Fill structure */
49         domain->revmap_type = revmap_type;
50         domain->ops = ops;
51         domain->host_data = host_data;
52         domain->of_node = of_node_get(of_node);
53
54         return domain;
55 }
56
57 static void irq_domain_free(struct irq_domain *domain)
58 {
59         of_node_put(domain->of_node);
60         kfree(domain);
61 }
62
63 static void irq_domain_add(struct irq_domain *domain)
64 {
65         mutex_lock(&irq_domain_mutex);
66         list_add(&domain->link, &irq_domain_list);
67         mutex_unlock(&irq_domain_mutex);
68         pr_debug("Allocated domain of type %d @0x%p\n",
69                  domain->revmap_type, domain);
70 }
71
72 /**
73  * irq_domain_remove() - Remove an irq domain.
74  * @domain: domain to remove
75  *
76  * This routine is used to remove an irq domain. The caller must ensure
77  * that all mappings within the domain have been disposed of prior to
78  * use, depending on the revmap type.
79  */
80 void irq_domain_remove(struct irq_domain *domain)
81 {
82         mutex_lock(&irq_domain_mutex);
83
84         switch (domain->revmap_type) {
85         case IRQ_DOMAIN_MAP_LEGACY:
86                 /*
87                  * Legacy domains don't manage their own irq_desc
88                  * allocations, we expect the caller to handle irq_desc
89                  * freeing on their own.
90                  */
91                 break;
92         case IRQ_DOMAIN_MAP_TREE:
93                 /*
94                  * radix_tree_delete() takes care of destroying the root
95                  * node when all entries are removed. Shout if there are
96                  * any mappings left.
97                  */
98                 WARN_ON(domain->revmap_data.tree.height);
99                 break;
100         case IRQ_DOMAIN_MAP_LINEAR:
101                 kfree(domain->revmap_data.linear.revmap);
102                 domain->revmap_data.linear.size = 0;
103                 break;
104         case IRQ_DOMAIN_MAP_NOMAP:
105                 break;
106         }
107
108         list_del(&domain->link);
109
110         /*
111          * If the going away domain is the default one, reset it.
112          */
113         if (unlikely(irq_default_domain == domain))
114                 irq_set_default_host(NULL);
115
116         mutex_unlock(&irq_domain_mutex);
117
118         pr_debug("Removed domain of type %d @0x%p\n",
119                  domain->revmap_type, domain);
120
121         irq_domain_free(domain);
122 }
123 EXPORT_SYMBOL_GPL(irq_domain_remove);
124
125 static unsigned int irq_domain_legacy_revmap(struct irq_domain *domain,
126                                              irq_hw_number_t hwirq)
127 {
128         irq_hw_number_t first_hwirq = domain->revmap_data.legacy.first_hwirq;
129         int size = domain->revmap_data.legacy.size;
130
131         if (WARN_ON(hwirq < first_hwirq || hwirq >= first_hwirq + size))
132                 return 0;
133         return hwirq - first_hwirq + domain->revmap_data.legacy.first_irq;
134 }
135
136 /**
137  * irq_domain_add_simple() - Allocate and register a simple irq_domain.
138  * @of_node: pointer to interrupt controller's device tree node.
139  * @size: total number of irqs in mapping
140  * @first_irq: first number of irq block assigned to the domain
141  * @ops: map/unmap domain callbacks
142  * @host_data: Controller private data pointer
143  *
144  * Allocates a legacy irq_domain if irq_base is positive or a linear
145  * domain otherwise. For the legacy domain, IRQ descriptors will also
146  * be allocated.
147  *
148  * This is intended to implement the expected behaviour for most
149  * interrupt controllers which is that a linear mapping should
150  * normally be used unless the system requires a legacy mapping in
151  * order to support supplying interrupt numbers during non-DT
152  * registration of devices.
153  */
154 struct irq_domain *irq_domain_add_simple(struct device_node *of_node,
155                                          unsigned int size,
156                                          unsigned int first_irq,
157                                          const struct irq_domain_ops *ops,
158                                          void *host_data)
159 {
160         if (first_irq > 0) {
161                 int irq_base;
162
163                 if (IS_ENABLED(CONFIG_SPARSE_IRQ)) {
164                         /*
165                          * Set the descriptor allocator to search for a
166                          * 1-to-1 mapping, such as irq_alloc_desc_at().
167                          * Use of_node_to_nid() which is defined to
168                          * numa_node_id() on platforms that have no custom
169                          * implementation.
170                          */
171                         irq_base = irq_alloc_descs(first_irq, first_irq, size,
172                                                    of_node_to_nid(of_node));
173                         if (irq_base < 0) {
174                                 pr_info("Cannot allocate irq_descs @ IRQ%d, assuming pre-allocated\n",
175                                         first_irq);
176                                 irq_base = first_irq;
177                         }
178                 } else
179                         irq_base = first_irq;
180
181                 return irq_domain_add_legacy(of_node, size, irq_base, 0,
182                                              ops, host_data);
183         }
184
185         /* A linear domain is the default */
186         return irq_domain_add_linear(of_node, size, ops, host_data);
187 }
188
189 /**
190  * irq_domain_add_legacy() - Allocate and register a legacy revmap irq_domain.
191  * @of_node: pointer to interrupt controller's device tree node.
192  * @size: total number of irqs in legacy mapping
193  * @first_irq: first number of irq block assigned to the domain
194  * @first_hwirq: first hwirq number to use for the translation. Should normally
195  *               be '0', but a positive integer can be used if the effective
196  *               hwirqs numbering does not begin at zero.
197  * @ops: map/unmap domain callbacks
198  * @host_data: Controller private data pointer
199  *
200  * Note: the map() callback will be called before this function returns
201  * for all legacy interrupts except 0 (which is always the invalid irq for
202  * a legacy controller).
203  */
204 struct irq_domain *irq_domain_add_legacy(struct device_node *of_node,
205                                          unsigned int size,
206                                          unsigned int first_irq,
207                                          irq_hw_number_t first_hwirq,
208                                          const struct irq_domain_ops *ops,
209                                          void *host_data)
210 {
211         struct irq_domain *domain;
212         unsigned int i;
213
214         domain = irq_domain_alloc(of_node, IRQ_DOMAIN_MAP_LEGACY, ops, host_data);
215         if (!domain)
216                 return NULL;
217
218         domain->revmap_data.legacy.first_irq = first_irq;
219         domain->revmap_data.legacy.first_hwirq = first_hwirq;
220         domain->revmap_data.legacy.size = size;
221
222         mutex_lock(&irq_domain_mutex);
223         /* Verify that all the irqs are available */
224         for (i = 0; i < size; i++) {
225                 int irq = first_irq + i;
226                 struct irq_data *irq_data = irq_get_irq_data(irq);
227
228                 if (WARN_ON(!irq_data || irq_data->domain)) {
229                         mutex_unlock(&irq_domain_mutex);
230                         irq_domain_free(domain);
231                         return NULL;
232                 }
233         }
234
235         /* Claim all of the irqs before registering a legacy domain */
236         for (i = 0; i < size; i++) {
237                 struct irq_data *irq_data = irq_get_irq_data(first_irq + i);
238                 irq_data->hwirq = first_hwirq + i;
239                 irq_data->domain = domain;
240         }
241         mutex_unlock(&irq_domain_mutex);
242
243         for (i = 0; i < size; i++) {
244                 int irq = first_irq + i;
245                 int hwirq = first_hwirq + i;
246
247                 /* IRQ0 gets ignored */
248                 if (!irq)
249                         continue;
250
251                 /* Legacy flags are left to default at this point,
252                  * one can then use irq_create_mapping() to
253                  * explicitly change them
254                  */
255                 if (ops->map)
256                         ops->map(domain, irq, hwirq);
257
258                 /* Clear norequest flags */
259                 irq_clear_status_flags(irq, IRQ_NOREQUEST);
260         }
261
262         irq_domain_add(domain);
263         return domain;
264 }
265 EXPORT_SYMBOL_GPL(irq_domain_add_legacy);
266
267 /**
268  * irq_domain_add_linear() - Allocate and register a linear revmap irq_domain.
269  * @of_node: pointer to interrupt controller's device tree node.
270  * @size: Number of interrupts in the domain.
271  * @ops: map/unmap domain callbacks
272  * @host_data: Controller private data pointer
273  */
274 struct irq_domain *irq_domain_add_linear(struct device_node *of_node,
275                                          unsigned int size,
276                                          const struct irq_domain_ops *ops,
277                                          void *host_data)
278 {
279         struct irq_domain *domain;
280         unsigned int *revmap;
281
282         revmap = kzalloc_node(sizeof(*revmap) * size, GFP_KERNEL,
283                               of_node_to_nid(of_node));
284         if (WARN_ON(!revmap))
285                 return NULL;
286
287         domain = irq_domain_alloc(of_node, IRQ_DOMAIN_MAP_LINEAR, ops, host_data);
288         if (!domain) {
289                 kfree(revmap);
290                 return NULL;
291         }
292         domain->revmap_data.linear.size = size;
293         domain->revmap_data.linear.revmap = revmap;
294         irq_domain_add(domain);
295         return domain;
296 }
297 EXPORT_SYMBOL_GPL(irq_domain_add_linear);
298
299 struct irq_domain *irq_domain_add_nomap(struct device_node *of_node,
300                                          unsigned int max_irq,
301                                          const struct irq_domain_ops *ops,
302                                          void *host_data)
303 {
304         struct irq_domain *domain = irq_domain_alloc(of_node,
305                                         IRQ_DOMAIN_MAP_NOMAP, ops, host_data);
306         if (domain) {
307                 domain->revmap_data.nomap.max_irq = max_irq ? max_irq : ~0;
308                 irq_domain_add(domain);
309         }
310         return domain;
311 }
312 EXPORT_SYMBOL_GPL(irq_domain_add_nomap);
313
314 /**
315  * irq_domain_add_tree()
316  * @of_node: pointer to interrupt controller's device tree node.
317  * @ops: map/unmap domain callbacks
318  *
319  * Note: The radix tree will be allocated later during boot automatically
320  * (the reverse mapping will use the slow path until that happens).
321  */
322 struct irq_domain *irq_domain_add_tree(struct device_node *of_node,
323                                          const struct irq_domain_ops *ops,
324                                          void *host_data)
325 {
326         struct irq_domain *domain = irq_domain_alloc(of_node,
327                                         IRQ_DOMAIN_MAP_TREE, ops, host_data);
328         if (domain) {
329                 INIT_RADIX_TREE(&domain->revmap_data.tree, GFP_KERNEL);
330                 irq_domain_add(domain);
331         }
332         return domain;
333 }
334 EXPORT_SYMBOL_GPL(irq_domain_add_tree);
335
336 /**
337  * irq_find_host() - Locates a domain for a given device node
338  * @node: device-tree node of the interrupt controller
339  */
340 struct irq_domain *irq_find_host(struct device_node *node)
341 {
342         struct irq_domain *h, *found = NULL;
343         int rc;
344
345         /* We might want to match the legacy controller last since
346          * it might potentially be set to match all interrupts in
347          * the absence of a device node. This isn't a problem so far
348          * yet though...
349          */
350         mutex_lock(&irq_domain_mutex);
351         list_for_each_entry(h, &irq_domain_list, link) {
352                 if (h->ops->match)
353                         rc = h->ops->match(h, node);
354                 else
355                         rc = (h->of_node != NULL) && (h->of_node == node);
356
357                 if (rc) {
358                         found = h;
359                         break;
360                 }
361         }
362         mutex_unlock(&irq_domain_mutex);
363         return found;
364 }
365 EXPORT_SYMBOL_GPL(irq_find_host);
366
367 /**
368  * irq_set_default_host() - Set a "default" irq domain
369  * @domain: default domain pointer
370  *
371  * For convenience, it's possible to set a "default" domain that will be used
372  * whenever NULL is passed to irq_create_mapping(). It makes life easier for
373  * platforms that want to manipulate a few hard coded interrupt numbers that
374  * aren't properly represented in the device-tree.
375  */
376 void irq_set_default_host(struct irq_domain *domain)
377 {
378         pr_debug("Default domain set to @0x%p\n", domain);
379
380         irq_default_domain = domain;
381 }
382 EXPORT_SYMBOL_GPL(irq_set_default_host);
383
384 static void irq_domain_disassociate_many(struct irq_domain *domain,
385                                          unsigned int irq_base, int count)
386 {
387         /*
388          * disassociate in reverse order;
389          * not strictly necessary, but nice for unwinding
390          */
391         while (count--) {
392                 int irq = irq_base + count;
393                 struct irq_data *irq_data = irq_get_irq_data(irq);
394                 irq_hw_number_t hwirq = irq_data->hwirq;
395
396                 if (WARN_ON(!irq_data || irq_data->domain != domain))
397                         continue;
398
399                 irq_set_status_flags(irq, IRQ_NOREQUEST);
400
401                 /* remove chip and handler */
402                 irq_set_chip_and_handler(irq, NULL, NULL);
403
404                 /* Make sure it's completed */
405                 synchronize_irq(irq);
406
407                 /* Tell the PIC about it */
408                 if (domain->ops->unmap)
409                         domain->ops->unmap(domain, irq);
410                 smp_mb();
411
412                 irq_data->domain = NULL;
413                 irq_data->hwirq = 0;
414
415                 /* Clear reverse map */
416                 switch(domain->revmap_type) {
417                 case IRQ_DOMAIN_MAP_LINEAR:
418                         if (hwirq < domain->revmap_data.linear.size)
419                                 domain->revmap_data.linear.revmap[hwirq] = 0;
420                         break;
421                 case IRQ_DOMAIN_MAP_TREE:
422                         mutex_lock(&revmap_trees_mutex);
423                         radix_tree_delete(&domain->revmap_data.tree, hwirq);
424                         mutex_unlock(&revmap_trees_mutex);
425                         break;
426                 }
427         }
428 }
429
430 int irq_domain_associate_many(struct irq_domain *domain, unsigned int irq_base,
431                               irq_hw_number_t hwirq_base, int count)
432 {
433         unsigned int virq = irq_base;
434         irq_hw_number_t hwirq = hwirq_base;
435         int i, ret;
436
437         pr_debug("%s(%s, irqbase=%i, hwbase=%i, count=%i)\n", __func__,
438                 of_node_full_name(domain->of_node), irq_base, (int)hwirq_base, count);
439
440         for (i = 0; i < count; i++) {
441                 struct irq_data *irq_data = irq_get_irq_data(virq + i);
442
443                 if (WARN(!irq_data, "error: irq_desc not allocated; "
444                          "irq=%i hwirq=0x%x\n", virq + i, (int)hwirq + i))
445                         return -EINVAL;
446                 if (WARN(irq_data->domain, "error: irq_desc already associated; "
447                          "irq=%i hwirq=0x%x\n", virq + i, (int)hwirq + i))
448                         return -EINVAL;
449         };
450
451         for (i = 0; i < count; i++, virq++, hwirq++) {
452                 struct irq_data *irq_data = irq_get_irq_data(virq);
453
454                 irq_data->hwirq = hwirq;
455                 irq_data->domain = domain;
456                 if (domain->ops->map) {
457                         ret = domain->ops->map(domain, virq, hwirq);
458                         if (ret != 0) {
459                                 /*
460                                  * If map() returns -EPERM, this interrupt is protected
461                                  * by the firmware or some other service and shall not
462                                  * be mapped.
463                                  *
464                                  * Since on some platforms we blindly try to map everything
465                                  * we end up with a log full of backtraces.
466                                  *
467                                  * So instead, we silently fail on -EPERM, it is the
468                                  * responsibility of the PIC driver to display a relevant
469                                  * message if needed.
470                                  */
471                                 if (ret != -EPERM) {
472                                         pr_err("irq-%i==>hwirq-0x%lx mapping failed: %d\n",
473                                                virq, hwirq, ret);
474                                         WARN_ON(1);
475                                 }
476                                 irq_data->domain = NULL;
477                                 irq_data->hwirq = 0;
478                                 goto err_unmap;
479                         }
480                 }
481
482                 switch (domain->revmap_type) {
483                 case IRQ_DOMAIN_MAP_LINEAR:
484                         if (hwirq < domain->revmap_data.linear.size)
485                                 domain->revmap_data.linear.revmap[hwirq] = virq;
486                         break;
487                 case IRQ_DOMAIN_MAP_TREE:
488                         mutex_lock(&revmap_trees_mutex);
489                         radix_tree_insert(&domain->revmap_data.tree, hwirq, irq_data);
490                         mutex_unlock(&revmap_trees_mutex);
491                         break;
492                 }
493
494                 irq_clear_status_flags(virq, IRQ_NOREQUEST);
495         }
496
497         return 0;
498
499  err_unmap:
500         irq_domain_disassociate_many(domain, irq_base, i);
501         return -EINVAL;
502 }
503 EXPORT_SYMBOL_GPL(irq_domain_associate_many);
504
505 /**
506  * irq_create_direct_mapping() - Allocate an irq for direct mapping
507  * @domain: domain to allocate the irq for or NULL for default domain
508  *
509  * This routine is used for irq controllers which can choose the hardware
510  * interrupt numbers they generate. In such a case it's simplest to use
511  * the linux irq as the hardware interrupt number.
512  */
513 unsigned int irq_create_direct_mapping(struct irq_domain *domain)
514 {
515         unsigned int virq;
516
517         if (domain == NULL)
518                 domain = irq_default_domain;
519
520         if (WARN_ON(!domain || domain->revmap_type != IRQ_DOMAIN_MAP_NOMAP))
521                 return 0;
522
523         virq = irq_alloc_desc_from(1, of_node_to_nid(domain->of_node));
524         if (!virq) {
525                 pr_debug("create_direct virq allocation failed\n");
526                 return 0;
527         }
528         if (virq >= domain->revmap_data.nomap.max_irq) {
529                 pr_err("ERROR: no free irqs available below %i maximum\n",
530                         domain->revmap_data.nomap.max_irq);
531                 irq_free_desc(virq);
532                 return 0;
533         }
534         pr_debug("create_direct obtained virq %d\n", virq);
535
536         if (irq_domain_associate(domain, virq, virq)) {
537                 irq_free_desc(virq);
538                 return 0;
539         }
540
541         return virq;
542 }
543 EXPORT_SYMBOL_GPL(irq_create_direct_mapping);
544
545 /**
546  * irq_create_mapping() - Map a hardware interrupt into linux irq space
547  * @domain: domain owning this hardware interrupt or NULL for default domain
548  * @hwirq: hardware irq number in that domain space
549  *
550  * Only one mapping per hardware interrupt is permitted. Returns a linux
551  * irq number.
552  * If the sense/trigger is to be specified, set_irq_type() should be called
553  * on the number returned from that call.
554  */
555 unsigned int irq_create_mapping(struct irq_domain *domain,
556                                 irq_hw_number_t hwirq)
557 {
558         unsigned int hint;
559         int virq;
560
561         pr_debug("irq_create_mapping(0x%p, 0x%lx)\n", domain, hwirq);
562
563         /* Look for default domain if nececssary */
564         if (domain == NULL)
565                 domain = irq_default_domain;
566         if (domain == NULL) {
567                 pr_warning("irq_create_mapping called for"
568                            " NULL domain, hwirq=%lx\n", hwirq);
569                 WARN_ON(1);
570                 return 0;
571         }
572         pr_debug("-> using domain @%p\n", domain);
573
574         /* Check if mapping already exists */
575         virq = irq_find_mapping(domain, hwirq);
576         if (virq) {
577                 pr_debug("-> existing mapping on virq %d\n", virq);
578                 return virq;
579         }
580
581         /* Get a virtual interrupt number */
582         if (domain->revmap_type == IRQ_DOMAIN_MAP_LEGACY)
583                 return irq_domain_legacy_revmap(domain, hwirq);
584
585         /* Allocate a virtual interrupt number */
586         hint = hwirq % nr_irqs;
587         if (hint == 0)
588                 hint++;
589         virq = irq_alloc_desc_from(hint, of_node_to_nid(domain->of_node));
590         if (virq <= 0)
591                 virq = irq_alloc_desc_from(1, of_node_to_nid(domain->of_node));
592         if (virq <= 0) {
593                 pr_debug("-> virq allocation failed\n");
594                 return 0;
595         }
596
597         if (irq_domain_associate(domain, virq, hwirq)) {
598                 irq_free_desc(virq);
599                 return 0;
600         }
601
602         pr_debug("irq %lu on domain %s mapped to virtual irq %u\n",
603                 hwirq, of_node_full_name(domain->of_node), virq);
604
605         return virq;
606 }
607 EXPORT_SYMBOL_GPL(irq_create_mapping);
608
609 /**
610  * irq_create_strict_mappings() - Map a range of hw irqs to fixed linux irqs
611  * @domain: domain owning the interrupt range
612  * @irq_base: beginning of linux IRQ range
613  * @hwirq_base: beginning of hardware IRQ range
614  * @count: Number of interrupts to map
615  *
616  * This routine is used for allocating and mapping a range of hardware
617  * irqs to linux irqs where the linux irq numbers are at pre-defined
618  * locations. For use by controllers that already have static mappings
619  * to insert in to the domain.
620  *
621  * Non-linear users can use irq_create_identity_mapping() for IRQ-at-a-time
622  * domain insertion.
623  *
624  * 0 is returned upon success, while any failure to establish a static
625  * mapping is treated as an error.
626  */
627 int irq_create_strict_mappings(struct irq_domain *domain, unsigned int irq_base,
628                                irq_hw_number_t hwirq_base, int count)
629 {
630         int ret;
631
632         ret = irq_alloc_descs(irq_base, irq_base, count,
633                               of_node_to_nid(domain->of_node));
634         if (unlikely(ret < 0))
635                 return ret;
636
637         ret = irq_domain_associate_many(domain, irq_base, hwirq_base, count);
638         if (unlikely(ret < 0)) {
639                 irq_free_descs(irq_base, count);
640                 return ret;
641         }
642
643         return 0;
644 }
645 EXPORT_SYMBOL_GPL(irq_create_strict_mappings);
646
647 unsigned int irq_create_of_mapping(struct device_node *controller,
648                                    const u32 *intspec, unsigned int intsize)
649 {
650         struct irq_domain *domain;
651         irq_hw_number_t hwirq;
652         unsigned int type = IRQ_TYPE_NONE;
653         unsigned int virq;
654
655         domain = controller ? irq_find_host(controller) : irq_default_domain;
656         if (!domain) {
657 #ifdef CONFIG_MIPS
658                 /*
659                  * Workaround to avoid breaking interrupt controller drivers
660                  * that don't yet register an irq_domain.  This is temporary
661                  * code. ~~~gcl, Feb 24, 2012
662                  *
663                  * Scheduled for removal in Linux v3.6.  That should be enough
664                  * time.
665                  */
666                 if (intsize > 0)
667                         return intspec[0];
668 #endif
669                 pr_warning("no irq domain found for %s !\n",
670                            of_node_full_name(controller));
671                 return 0;
672         }
673
674         /* If domain has no translation, then we assume interrupt line */
675         if (domain->ops->xlate == NULL)
676                 hwirq = intspec[0];
677         else {
678                 if (domain->ops->xlate(domain, controller, intspec, intsize,
679                                      &hwirq, &type))
680                         return 0;
681         }
682
683         /* Create mapping */
684         virq = irq_create_mapping(domain, hwirq);
685         if (!virq)
686                 return virq;
687
688         /* Set type if specified and different than the current one */
689         if (type != IRQ_TYPE_NONE &&
690             type != irq_get_trigger_type(virq))
691                 irq_set_irq_type(virq, type);
692         return virq;
693 }
694 EXPORT_SYMBOL_GPL(irq_create_of_mapping);
695
696 /**
697  * irq_dispose_mapping() - Unmap an interrupt
698  * @virq: linux irq number of the interrupt to unmap
699  */
700 void irq_dispose_mapping(unsigned int virq)
701 {
702         struct irq_data *irq_data = irq_get_irq_data(virq);
703         struct irq_domain *domain;
704
705         if (!virq || !irq_data)
706                 return;
707
708         domain = irq_data->domain;
709         if (WARN_ON(domain == NULL))
710                 return;
711
712         /* Never unmap legacy interrupts */
713         if (domain->revmap_type == IRQ_DOMAIN_MAP_LEGACY)
714                 return;
715
716         irq_domain_disassociate_many(domain, virq, 1);
717         irq_free_desc(virq);
718 }
719 EXPORT_SYMBOL_GPL(irq_dispose_mapping);
720
721 /**
722  * irq_find_mapping() - Find a linux irq from an hw irq number.
723  * @domain: domain owning this hardware interrupt
724  * @hwirq: hardware irq number in that domain space
725  */
726 unsigned int irq_find_mapping(struct irq_domain *domain,
727                               irq_hw_number_t hwirq)
728 {
729         struct irq_data *data;
730
731         /* Look for default domain if nececssary */
732         if (domain == NULL)
733                 domain = irq_default_domain;
734         if (domain == NULL)
735                 return 0;
736
737         switch (domain->revmap_type) {
738         case IRQ_DOMAIN_MAP_LEGACY:
739                 return irq_domain_legacy_revmap(domain, hwirq);
740         case IRQ_DOMAIN_MAP_LINEAR:
741                 return irq_linear_revmap(domain, hwirq);
742         case IRQ_DOMAIN_MAP_TREE:
743                 rcu_read_lock();
744                 data = radix_tree_lookup(&domain->revmap_data.tree, hwirq);
745                 rcu_read_unlock();
746                 if (data)
747                         return data->irq;
748                 break;
749         case IRQ_DOMAIN_MAP_NOMAP:
750                 data = irq_get_irq_data(hwirq);
751                 if (data && (data->domain == domain) && (data->hwirq == hwirq))
752                         return hwirq;
753                 break;
754         }
755
756         return 0;
757 }
758 EXPORT_SYMBOL_GPL(irq_find_mapping);
759
760 /**
761  * irq_linear_revmap() - Find a linux irq from a hw irq number.
762  * @domain: domain owning this hardware interrupt
763  * @hwirq: hardware irq number in that domain space
764  *
765  * This is a fast path that can be called directly by irq controller code to
766  * save a handful of instructions.
767  */
768 unsigned int irq_linear_revmap(struct irq_domain *domain,
769                                irq_hw_number_t hwirq)
770 {
771         BUG_ON(domain->revmap_type != IRQ_DOMAIN_MAP_LINEAR);
772
773         /* Check revmap bounds; complain if exceeded */
774         if (WARN_ON(hwirq >= domain->revmap_data.linear.size))
775                 return 0;
776
777         return domain->revmap_data.linear.revmap[hwirq];
778 }
779 EXPORT_SYMBOL_GPL(irq_linear_revmap);
780
781 #ifdef CONFIG_IRQ_DOMAIN_DEBUG
782 static int virq_debug_show(struct seq_file *m, void *private)
783 {
784         unsigned long flags;
785         struct irq_desc *desc;
786         const char *p;
787         static const char none[] = "none";
788         void *data;
789         int i;
790
791         seq_printf(m, "%-5s  %-7s  %-15s  %-*s  %s\n", "irq", "hwirq",
792                       "chip name", (int)(2 * sizeof(void *) + 2), "chip data",
793                       "domain name");
794
795         for (i = 1; i < nr_irqs; i++) {
796                 desc = irq_to_desc(i);
797                 if (!desc)
798                         continue;
799
800                 raw_spin_lock_irqsave(&desc->lock, flags);
801
802                 if (desc->action && desc->action->handler) {
803                         struct irq_chip *chip;
804
805                         seq_printf(m, "%5d  ", i);
806                         seq_printf(m, "0x%05lx  ", desc->irq_data.hwirq);
807
808                         chip = irq_desc_get_chip(desc);
809                         if (chip && chip->name)
810                                 p = chip->name;
811                         else
812                                 p = none;
813                         seq_printf(m, "%-15s  ", p);
814
815                         data = irq_desc_get_chip_data(desc);
816                         seq_printf(m, data ? "0x%p  " : "  %p  ", data);
817
818                         if (desc->irq_data.domain)
819                                 p = of_node_full_name(desc->irq_data.domain->of_node);
820                         else
821                                 p = none;
822                         seq_printf(m, "%s\n", p);
823                 }
824
825                 raw_spin_unlock_irqrestore(&desc->lock, flags);
826         }
827
828         return 0;
829 }
830
831 static int virq_debug_open(struct inode *inode, struct file *file)
832 {
833         return single_open(file, virq_debug_show, inode->i_private);
834 }
835
836 static const struct file_operations virq_debug_fops = {
837         .open = virq_debug_open,
838         .read = seq_read,
839         .llseek = seq_lseek,
840         .release = single_release,
841 };
842
843 static int __init irq_debugfs_init(void)
844 {
845         if (debugfs_create_file("irq_domain_mapping", S_IRUGO, NULL,
846                                  NULL, &virq_debug_fops) == NULL)
847                 return -ENOMEM;
848
849         return 0;
850 }
851 __initcall(irq_debugfs_init);
852 #endif /* CONFIG_IRQ_DOMAIN_DEBUG */
853
854 /**
855  * irq_domain_xlate_onecell() - Generic xlate for direct one cell bindings
856  *
857  * Device Tree IRQ specifier translation function which works with one cell
858  * bindings where the cell value maps directly to the hwirq number.
859  */
860 int irq_domain_xlate_onecell(struct irq_domain *d, struct device_node *ctrlr,
861                              const u32 *intspec, unsigned int intsize,
862                              unsigned long *out_hwirq, unsigned int *out_type)
863 {
864         if (WARN_ON(intsize < 1))
865                 return -EINVAL;
866         *out_hwirq = intspec[0];
867         *out_type = IRQ_TYPE_NONE;
868         return 0;
869 }
870 EXPORT_SYMBOL_GPL(irq_domain_xlate_onecell);
871
872 /**
873  * irq_domain_xlate_twocell() - Generic xlate for direct two cell bindings
874  *
875  * Device Tree IRQ specifier translation function which works with two cell
876  * bindings where the cell values map directly to the hwirq number
877  * and linux irq flags.
878  */
879 int irq_domain_xlate_twocell(struct irq_domain *d, struct device_node *ctrlr,
880                         const u32 *intspec, unsigned int intsize,
881                         irq_hw_number_t *out_hwirq, unsigned int *out_type)
882 {
883         if (WARN_ON(intsize < 2))
884                 return -EINVAL;
885         *out_hwirq = intspec[0];
886         *out_type = intspec[1] & IRQ_TYPE_SENSE_MASK;
887         return 0;
888 }
889 EXPORT_SYMBOL_GPL(irq_domain_xlate_twocell);
890
891 /**
892  * irq_domain_xlate_onetwocell() - Generic xlate for one or two cell bindings
893  *
894  * Device Tree IRQ specifier translation function which works with either one
895  * or two cell bindings where the cell values map directly to the hwirq number
896  * and linux irq flags.
897  *
898  * Note: don't use this function unless your interrupt controller explicitly
899  * supports both one and two cell bindings.  For the majority of controllers
900  * the _onecell() or _twocell() variants above should be used.
901  */
902 int irq_domain_xlate_onetwocell(struct irq_domain *d,
903                                 struct device_node *ctrlr,
904                                 const u32 *intspec, unsigned int intsize,
905                                 unsigned long *out_hwirq, unsigned int *out_type)
906 {
907         if (WARN_ON(intsize < 1))
908                 return -EINVAL;
909         *out_hwirq = intspec[0];
910         *out_type = (intsize > 1) ? intspec[1] : IRQ_TYPE_NONE;
911         return 0;
912 }
913 EXPORT_SYMBOL_GPL(irq_domain_xlate_onetwocell);
914
915 const struct irq_domain_ops irq_domain_simple_ops = {
916         .xlate = irq_domain_xlate_onetwocell,
917 };
918 EXPORT_SYMBOL_GPL(irq_domain_simple_ops);
919
920 #ifdef CONFIG_OF_IRQ
921 void irq_domain_generate_simple(const struct of_device_id *match,
922                                 u64 phys_base, unsigned int irq_start)
923 {
924         struct device_node *node;
925         pr_debug("looking for phys_base=%llx, irq_start=%i\n",
926                 (unsigned long long) phys_base, (int) irq_start);
927         node = of_find_matching_node_by_address(NULL, match, phys_base);
928         if (node)
929                 irq_domain_add_legacy(node, 32, irq_start, 0,
930                                       &irq_domain_simple_ops, NULL);
931 }
932 EXPORT_SYMBOL_GPL(irq_domain_generate_simple);
933 #endif