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