1 #include <linux/debugfs.h>
2 #include <linux/hardirq.h>
3 #include <linux/interrupt.h>
5 #include <linux/irqdesc.h>
6 #include <linux/irqdomain.h>
7 #include <linux/module.h>
8 #include <linux/mutex.h>
10 #include <linux/of_address.h>
11 #include <linux/seq_file.h>
12 #include <linux/slab.h>
13 #include <linux/smp.h>
16 #define IRQ_DOMAIN_MAP_LEGACY 0 /* driver allocated fixed range of irqs.
17 * ie. legacy 8259, gets irqs 1..15 */
18 #define IRQ_DOMAIN_MAP_NOMAP 1 /* no fast reverse mapping */
19 #define IRQ_DOMAIN_MAP_LINEAR 2 /* linear map of interrupts */
20 #define IRQ_DOMAIN_MAP_TREE 3 /* radix tree */
22 static LIST_HEAD(irq_domain_list
);
23 static DEFINE_MUTEX(irq_domain_mutex
);
25 static DEFINE_MUTEX(revmap_trees_mutex
);
26 static struct irq_domain
*irq_default_domain
;
29 * irq_domain_alloc() - Allocate a new irq_domain data structure
30 * @of_node: optional device-tree node of the interrupt controller
31 * @revmap_type: type of reverse mapping to use
32 * @ops: map/unmap domain callbacks
33 * @host_data: Controller private data pointer
35 * Allocates and initialize and irq_domain structure. Caller is expected to
36 * register allocated irq_domain with irq_domain_register(). Returns pointer
37 * to IRQ domain, or NULL on failure.
39 static struct irq_domain
*irq_domain_alloc(struct device_node
*of_node
,
40 unsigned int revmap_type
,
41 const struct irq_domain_ops
*ops
,
44 struct irq_domain
*domain
;
46 domain
= kzalloc(sizeof(*domain
), GFP_KERNEL
);
51 domain
->revmap_type
= revmap_type
;
53 domain
->host_data
= host_data
;
54 domain
->of_node
= of_node_get(of_node
);
59 static void irq_domain_add(struct irq_domain
*domain
)
61 mutex_lock(&irq_domain_mutex
);
62 list_add(&domain
->link
, &irq_domain_list
);
63 mutex_unlock(&irq_domain_mutex
);
64 pr_debug("irq: Allocated domain of type %d @0x%p\n",
65 domain
->revmap_type
, domain
);
68 static unsigned int irq_domain_legacy_revmap(struct irq_domain
*domain
,
69 irq_hw_number_t hwirq
)
71 irq_hw_number_t first_hwirq
= domain
->revmap_data
.legacy
.first_hwirq
;
72 int size
= domain
->revmap_data
.legacy
.size
;
74 if (WARN_ON(hwirq
< first_hwirq
|| hwirq
>= first_hwirq
+ size
))
76 return hwirq
- first_hwirq
+ domain
->revmap_data
.legacy
.first_irq
;
80 * irq_domain_add_legacy() - Allocate and register a legacy revmap irq_domain.
81 * @of_node: pointer to interrupt controller's device tree node.
82 * @size: total number of irqs in legacy mapping
83 * @first_irq: first number of irq block assigned to the domain
84 * @first_hwirq: first hwirq number to use for the translation. Should normally
85 * be '0', but a positive integer can be used if the effective
86 * hwirqs numbering does not begin at zero.
87 * @ops: map/unmap domain callbacks
88 * @host_data: Controller private data pointer
90 * Note: the map() callback will be called before this function returns
91 * for all legacy interrupts except 0 (which is always the invalid irq for
92 * a legacy controller).
94 struct irq_domain
*irq_domain_add_legacy(struct device_node
*of_node
,
96 unsigned int first_irq
,
97 irq_hw_number_t first_hwirq
,
98 const struct irq_domain_ops
*ops
,
101 struct irq_domain
*domain
;
104 domain
= irq_domain_alloc(of_node
, IRQ_DOMAIN_MAP_LEGACY
, ops
, host_data
);
108 domain
->revmap_data
.legacy
.first_irq
= first_irq
;
109 domain
->revmap_data
.legacy
.first_hwirq
= first_hwirq
;
110 domain
->revmap_data
.legacy
.size
= size
;
112 mutex_lock(&irq_domain_mutex
);
113 /* Verify that all the irqs are available */
114 for (i
= 0; i
< size
; i
++) {
115 int irq
= first_irq
+ i
;
116 struct irq_data
*irq_data
= irq_get_irq_data(irq
);
118 if (WARN_ON(!irq_data
|| irq_data
->domain
)) {
119 mutex_unlock(&irq_domain_mutex
);
120 of_node_put(domain
->of_node
);
126 /* Claim all of the irqs before registering a legacy domain */
127 for (i
= 0; i
< size
; i
++) {
128 struct irq_data
*irq_data
= irq_get_irq_data(first_irq
+ i
);
129 irq_data
->hwirq
= first_hwirq
+ i
;
130 irq_data
->domain
= domain
;
132 mutex_unlock(&irq_domain_mutex
);
134 for (i
= 0; i
< size
; i
++) {
135 int irq
= first_irq
+ i
;
136 int hwirq
= first_hwirq
+ i
;
138 /* IRQ0 gets ignored */
142 /* Legacy flags are left to default at this point,
143 * one can then use irq_create_mapping() to
144 * explicitly change them
146 ops
->map(domain
, irq
, hwirq
);
148 /* Clear norequest flags */
149 irq_clear_status_flags(irq
, IRQ_NOREQUEST
);
152 irq_domain_add(domain
);
157 * irq_domain_add_linear() - Allocate and register a legacy revmap irq_domain.
158 * @of_node: pointer to interrupt controller's device tree node.
159 * @ops: map/unmap domain callbacks
160 * @host_data: Controller private data pointer
162 struct irq_domain
*irq_domain_add_linear(struct device_node
*of_node
,
164 const struct irq_domain_ops
*ops
,
167 struct irq_domain
*domain
;
168 unsigned int *revmap
;
170 revmap
= kzalloc(sizeof(*revmap
) * size
, GFP_KERNEL
);
171 if (WARN_ON(!revmap
))
174 domain
= irq_domain_alloc(of_node
, IRQ_DOMAIN_MAP_LINEAR
, ops
, host_data
);
179 domain
->revmap_data
.linear
.size
= size
;
180 domain
->revmap_data
.linear
.revmap
= revmap
;
181 irq_domain_add(domain
);
185 struct irq_domain
*irq_domain_add_nomap(struct device_node
*of_node
,
186 unsigned int max_irq
,
187 const struct irq_domain_ops
*ops
,
190 struct irq_domain
*domain
= irq_domain_alloc(of_node
,
191 IRQ_DOMAIN_MAP_NOMAP
, ops
, host_data
);
193 domain
->revmap_data
.nomap
.max_irq
= max_irq
? max_irq
: ~0;
194 irq_domain_add(domain
);
200 * irq_domain_add_tree()
201 * @of_node: pointer to interrupt controller's device tree node.
202 * @ops: map/unmap domain callbacks
204 * Note: The radix tree will be allocated later during boot automatically
205 * (the reverse mapping will use the slow path until that happens).
207 struct irq_domain
*irq_domain_add_tree(struct device_node
*of_node
,
208 const struct irq_domain_ops
*ops
,
211 struct irq_domain
*domain
= irq_domain_alloc(of_node
,
212 IRQ_DOMAIN_MAP_TREE
, ops
, host_data
);
214 INIT_RADIX_TREE(&domain
->revmap_data
.tree
, GFP_KERNEL
);
215 irq_domain_add(domain
);
221 * irq_find_host() - Locates a domain for a given device node
222 * @node: device-tree node of the interrupt controller
224 struct irq_domain
*irq_find_host(struct device_node
*node
)
226 struct irq_domain
*h
, *found
= NULL
;
229 /* We might want to match the legacy controller last since
230 * it might potentially be set to match all interrupts in
231 * the absence of a device node. This isn't a problem so far
234 mutex_lock(&irq_domain_mutex
);
235 list_for_each_entry(h
, &irq_domain_list
, link
) {
237 rc
= h
->ops
->match(h
, node
);
239 rc
= (h
->of_node
!= NULL
) && (h
->of_node
== node
);
246 mutex_unlock(&irq_domain_mutex
);
249 EXPORT_SYMBOL_GPL(irq_find_host
);
252 * irq_set_default_host() - Set a "default" irq domain
253 * @domain: default domain pointer
255 * For convenience, it's possible to set a "default" domain that will be used
256 * whenever NULL is passed to irq_create_mapping(). It makes life easier for
257 * platforms that want to manipulate a few hard coded interrupt numbers that
258 * aren't properly represented in the device-tree.
260 void irq_set_default_host(struct irq_domain
*domain
)
262 pr_debug("irq: Default domain set to @0x%p\n", domain
);
264 irq_default_domain
= domain
;
267 static int irq_setup_virq(struct irq_domain
*domain
, unsigned int virq
,
268 irq_hw_number_t hwirq
)
270 struct irq_data
*irq_data
= irq_get_irq_data(virq
);
272 irq_data
->hwirq
= hwirq
;
273 irq_data
->domain
= domain
;
274 if (domain
->ops
->map(domain
, virq
, hwirq
)) {
275 pr_debug("irq: -> mapping failed, freeing\n");
276 irq_data
->domain
= NULL
;
281 irq_clear_status_flags(virq
, IRQ_NOREQUEST
);
287 * irq_create_direct_mapping() - Allocate an irq for direct mapping
288 * @domain: domain to allocate the irq for or NULL for default domain
290 * This routine is used for irq controllers which can choose the hardware
291 * interrupt numbers they generate. In such a case it's simplest to use
292 * the linux irq as the hardware interrupt number.
294 unsigned int irq_create_direct_mapping(struct irq_domain
*domain
)
299 domain
= irq_default_domain
;
301 BUG_ON(domain
== NULL
);
302 WARN_ON(domain
->revmap_type
!= IRQ_DOMAIN_MAP_NOMAP
);
304 virq
= irq_alloc_desc_from(1, 0);
306 pr_debug("irq: create_direct virq allocation failed\n");
309 if (virq
>= domain
->revmap_data
.nomap
.max_irq
) {
310 pr_err("ERROR: no free irqs available below %i maximum\n",
311 domain
->revmap_data
.nomap
.max_irq
);
315 pr_debug("irq: create_direct obtained virq %d\n", virq
);
317 if (irq_setup_virq(domain
, virq
, virq
)) {
326 * irq_create_mapping() - Map a hardware interrupt into linux irq space
327 * @domain: domain owning this hardware interrupt or NULL for default domain
328 * @hwirq: hardware irq number in that domain space
330 * Only one mapping per hardware interrupt is permitted. Returns a linux
332 * If the sense/trigger is to be specified, set_irq_type() should be called
333 * on the number returned from that call.
335 unsigned int irq_create_mapping(struct irq_domain
*domain
,
336 irq_hw_number_t hwirq
)
341 pr_debug("irq: irq_create_mapping(0x%p, 0x%lx)\n", domain
, hwirq
);
343 /* Look for default domain if nececssary */
345 domain
= irq_default_domain
;
346 if (domain
== NULL
) {
347 printk(KERN_WARNING
"irq_create_mapping called for"
348 " NULL domain, hwirq=%lx\n", hwirq
);
352 pr_debug("irq: -> using domain @%p\n", domain
);
354 /* Check if mapping already exists */
355 virq
= irq_find_mapping(domain
, hwirq
);
357 pr_debug("irq: -> existing mapping on virq %d\n", virq
);
361 /* Get a virtual interrupt number */
362 if (domain
->revmap_type
== IRQ_DOMAIN_MAP_LEGACY
)
363 return irq_domain_legacy_revmap(domain
, hwirq
);
365 /* Allocate a virtual interrupt number */
366 hint
= hwirq
% nr_irqs
;
369 virq
= irq_alloc_desc_from(hint
, 0);
371 virq
= irq_alloc_desc_from(1, 0);
373 pr_debug("irq: -> virq allocation failed\n");
377 if (irq_setup_virq(domain
, virq
, hwirq
)) {
378 if (domain
->revmap_type
!= IRQ_DOMAIN_MAP_LEGACY
)
383 pr_debug("irq: irq %lu on domain %s mapped to virtual irq %u\n",
384 hwirq
, domain
->of_node
? domain
->of_node
->full_name
: "null", virq
);
388 EXPORT_SYMBOL_GPL(irq_create_mapping
);
390 unsigned int irq_create_of_mapping(struct device_node
*controller
,
391 const u32
*intspec
, unsigned int intsize
)
393 struct irq_domain
*domain
;
394 irq_hw_number_t hwirq
;
395 unsigned int type
= IRQ_TYPE_NONE
;
398 domain
= controller
? irq_find_host(controller
) : irq_default_domain
;
402 * Workaround to avoid breaking interrupt controller drivers
403 * that don't yet register an irq_domain. This is temporary
404 * code. ~~~gcl, Feb 24, 2012
406 * Scheduled for removal in Linux v3.6. That should be enough
412 printk(KERN_WARNING
"irq: no irq domain found for %s !\n",
413 controller
->full_name
);
417 /* If domain has no translation, then we assume interrupt line */
418 if (domain
->ops
->xlate
== NULL
)
421 if (domain
->ops
->xlate(domain
, controller
, intspec
, intsize
,
427 virq
= irq_create_mapping(domain
, hwirq
);
431 /* Set type if specified and different than the current one */
432 if (type
!= IRQ_TYPE_NONE
&&
433 type
!= (irqd_get_trigger_type(irq_get_irq_data(virq
))))
434 irq_set_irq_type(virq
, type
);
437 EXPORT_SYMBOL_GPL(irq_create_of_mapping
);
440 * irq_dispose_mapping() - Unmap an interrupt
441 * @virq: linux irq number of the interrupt to unmap
443 void irq_dispose_mapping(unsigned int virq
)
445 struct irq_data
*irq_data
= irq_get_irq_data(virq
);
446 struct irq_domain
*domain
;
447 irq_hw_number_t hwirq
;
449 if (!virq
|| !irq_data
)
452 domain
= irq_data
->domain
;
453 if (WARN_ON(domain
== NULL
))
456 /* Never unmap legacy interrupts */
457 if (domain
->revmap_type
== IRQ_DOMAIN_MAP_LEGACY
)
460 irq_set_status_flags(virq
, IRQ_NOREQUEST
);
462 /* remove chip and handler */
463 irq_set_chip_and_handler(virq
, NULL
, NULL
);
465 /* Make sure it's completed */
466 synchronize_irq(virq
);
468 /* Tell the PIC about it */
469 if (domain
->ops
->unmap
)
470 domain
->ops
->unmap(domain
, virq
);
473 /* Clear reverse map */
474 hwirq
= irq_data
->hwirq
;
475 switch(domain
->revmap_type
) {
476 case IRQ_DOMAIN_MAP_LINEAR
:
477 if (hwirq
< domain
->revmap_data
.linear
.size
)
478 domain
->revmap_data
.linear
.revmap
[hwirq
] = 0;
480 case IRQ_DOMAIN_MAP_TREE
:
481 mutex_lock(&revmap_trees_mutex
);
482 radix_tree_delete(&domain
->revmap_data
.tree
, hwirq
);
483 mutex_unlock(&revmap_trees_mutex
);
489 EXPORT_SYMBOL_GPL(irq_dispose_mapping
);
492 * irq_find_mapping() - Find a linux irq from an hw irq number.
493 * @domain: domain owning this hardware interrupt
494 * @hwirq: hardware irq number in that domain space
496 * This is a slow path, for use by generic code. It's expected that an
497 * irq controller implementation directly calls the appropriate low level
500 unsigned int irq_find_mapping(struct irq_domain
*domain
,
501 irq_hw_number_t hwirq
)
504 unsigned int hint
= hwirq
% nr_irqs
;
506 /* Look for default domain if nececssary */
508 domain
= irq_default_domain
;
512 /* legacy -> bail early */
513 if (domain
->revmap_type
== IRQ_DOMAIN_MAP_LEGACY
)
514 return irq_domain_legacy_revmap(domain
, hwirq
);
516 /* Slow path does a linear search of the map */
521 struct irq_data
*data
= irq_get_irq_data(i
);
522 if (data
&& (data
->domain
== domain
) && (data
->hwirq
== hwirq
))
530 EXPORT_SYMBOL_GPL(irq_find_mapping
);
533 * irq_radix_revmap_lookup() - Find a linux irq from a hw irq number.
534 * @domain: domain owning this hardware interrupt
535 * @hwirq: hardware irq number in that domain space
537 * This is a fast path, for use by irq controller code that uses radix tree
540 unsigned int irq_radix_revmap_lookup(struct irq_domain
*domain
,
541 irq_hw_number_t hwirq
)
543 struct irq_data
*irq_data
;
545 if (WARN_ON_ONCE(domain
->revmap_type
!= IRQ_DOMAIN_MAP_TREE
))
546 return irq_find_mapping(domain
, hwirq
);
549 * Freeing an irq can delete nodes along the path to
550 * do the lookup via call_rcu.
553 irq_data
= radix_tree_lookup(&domain
->revmap_data
.tree
, hwirq
);
557 * If found in radix tree, then fine.
558 * Else fallback to linear lookup - this should not happen in practice
559 * as it means that we failed to insert the node in the radix tree.
561 return irq_data
? irq_data
->irq
: irq_find_mapping(domain
, hwirq
);
565 * irq_radix_revmap_insert() - Insert a hw irq to linux irq number mapping.
566 * @domain: domain owning this hardware interrupt
567 * @virq: linux irq number
568 * @hwirq: hardware irq number in that domain space
570 * This is for use by irq controllers that use a radix tree reverse
571 * mapping for fast lookup.
573 void irq_radix_revmap_insert(struct irq_domain
*domain
, unsigned int virq
,
574 irq_hw_number_t hwirq
)
576 struct irq_data
*irq_data
= irq_get_irq_data(virq
);
578 if (WARN_ON(domain
->revmap_type
!= IRQ_DOMAIN_MAP_TREE
))
582 mutex_lock(&revmap_trees_mutex
);
583 radix_tree_insert(&domain
->revmap_data
.tree
, hwirq
, irq_data
);
584 mutex_unlock(&revmap_trees_mutex
);
589 * irq_linear_revmap() - Find a linux irq from a hw irq number.
590 * @domain: domain owning this hardware interrupt
591 * @hwirq: hardware irq number in that domain space
593 * This is a fast path, for use by irq controller code that uses linear
594 * revmaps. It does fallback to the slow path if the revmap doesn't exist
595 * yet and will create the revmap entry with appropriate locking
597 unsigned int irq_linear_revmap(struct irq_domain
*domain
,
598 irq_hw_number_t hwirq
)
600 unsigned int *revmap
;
602 if (WARN_ON_ONCE(domain
->revmap_type
!= IRQ_DOMAIN_MAP_LINEAR
))
603 return irq_find_mapping(domain
, hwirq
);
605 /* Check revmap bounds */
606 if (unlikely(hwirq
>= domain
->revmap_data
.linear
.size
))
607 return irq_find_mapping(domain
, hwirq
);
609 /* Check if revmap was allocated */
610 revmap
= domain
->revmap_data
.linear
.revmap
;
611 if (unlikely(revmap
== NULL
))
612 return irq_find_mapping(domain
, hwirq
);
614 /* Fill up revmap with slow path if no mapping found */
615 if (unlikely(!revmap
[hwirq
]))
616 revmap
[hwirq
] = irq_find_mapping(domain
, hwirq
);
618 return revmap
[hwirq
];
621 #ifdef CONFIG_IRQ_DOMAIN_DEBUG
622 static int virq_debug_show(struct seq_file
*m
, void *private)
625 struct irq_desc
*desc
;
627 static const char none
[] = "none";
631 seq_printf(m
, "%-5s %-7s %-15s %-*s %s\n", "irq", "hwirq",
632 "chip name", (int)(2 * sizeof(void *) + 2), "chip data",
635 for (i
= 1; i
< nr_irqs
; i
++) {
636 desc
= irq_to_desc(i
);
640 raw_spin_lock_irqsave(&desc
->lock
, flags
);
642 if (desc
->action
&& desc
->action
->handler
) {
643 struct irq_chip
*chip
;
645 seq_printf(m
, "%5d ", i
);
646 seq_printf(m
, "0x%05lx ", desc
->irq_data
.hwirq
);
648 chip
= irq_desc_get_chip(desc
);
649 if (chip
&& chip
->name
)
653 seq_printf(m
, "%-15s ", p
);
655 data
= irq_desc_get_chip_data(desc
);
656 seq_printf(m
, data
? "0x%p " : " %p ", data
);
658 if (desc
->irq_data
.domain
&& desc
->irq_data
.domain
->of_node
)
659 p
= desc
->irq_data
.domain
->of_node
->full_name
;
662 seq_printf(m
, "%s\n", p
);
665 raw_spin_unlock_irqrestore(&desc
->lock
, flags
);
671 static int virq_debug_open(struct inode
*inode
, struct file
*file
)
673 return single_open(file
, virq_debug_show
, inode
->i_private
);
676 static const struct file_operations virq_debug_fops
= {
677 .open
= virq_debug_open
,
680 .release
= single_release
,
683 static int __init
irq_debugfs_init(void)
685 if (debugfs_create_file("irq_domain_mapping", S_IRUGO
, NULL
,
686 NULL
, &virq_debug_fops
) == NULL
)
691 __initcall(irq_debugfs_init
);
692 #endif /* CONFIG_IRQ_DOMAIN_DEBUG */
694 int irq_domain_simple_map(struct irq_domain
*d
, unsigned int irq
,
695 irq_hw_number_t hwirq
)
701 * irq_domain_xlate_onecell() - Generic xlate for direct one cell bindings
703 * Device Tree IRQ specifier translation function which works with one cell
704 * bindings where the cell value maps directly to the hwirq number.
706 int irq_domain_xlate_onecell(struct irq_domain
*d
, struct device_node
*ctrlr
,
707 const u32
*intspec
, unsigned int intsize
,
708 unsigned long *out_hwirq
, unsigned int *out_type
)
710 if (WARN_ON(intsize
< 1))
712 *out_hwirq
= intspec
[0];
713 *out_type
= IRQ_TYPE_NONE
;
716 EXPORT_SYMBOL_GPL(irq_domain_xlate_onecell
);
719 * irq_domain_xlate_twocell() - Generic xlate for direct two cell bindings
721 * Device Tree IRQ specifier translation function which works with two cell
722 * bindings where the cell values map directly to the hwirq number
723 * and linux irq flags.
725 int irq_domain_xlate_twocell(struct irq_domain
*d
, struct device_node
*ctrlr
,
726 const u32
*intspec
, unsigned int intsize
,
727 irq_hw_number_t
*out_hwirq
, unsigned int *out_type
)
729 if (WARN_ON(intsize
< 2))
731 *out_hwirq
= intspec
[0];
732 *out_type
= intspec
[1] & IRQ_TYPE_SENSE_MASK
;
735 EXPORT_SYMBOL_GPL(irq_domain_xlate_twocell
);
738 * irq_domain_xlate_onetwocell() - Generic xlate for one or two cell bindings
740 * Device Tree IRQ specifier translation function which works with either one
741 * or two cell bindings where the cell values map directly to the hwirq number
742 * and linux irq flags.
744 * Note: don't use this function unless your interrupt controller explicitly
745 * supports both one and two cell bindings. For the majority of controllers
746 * the _onecell() or _twocell() variants above should be used.
748 int irq_domain_xlate_onetwocell(struct irq_domain
*d
,
749 struct device_node
*ctrlr
,
750 const u32
*intspec
, unsigned int intsize
,
751 unsigned long *out_hwirq
, unsigned int *out_type
)
753 if (WARN_ON(intsize
< 1))
755 *out_hwirq
= intspec
[0];
756 *out_type
= (intsize
> 1) ? intspec
[1] : IRQ_TYPE_NONE
;
759 EXPORT_SYMBOL_GPL(irq_domain_xlate_onetwocell
);
761 const struct irq_domain_ops irq_domain_simple_ops
= {
762 .map
= irq_domain_simple_map
,
763 .xlate
= irq_domain_xlate_onetwocell
,
765 EXPORT_SYMBOL_GPL(irq_domain_simple_ops
);
768 void irq_domain_generate_simple(const struct of_device_id
*match
,
769 u64 phys_base
, unsigned int irq_start
)
771 struct device_node
*node
;
772 pr_debug("looking for phys_base=%llx, irq_start=%i\n",
773 (unsigned long long) phys_base
, (int) irq_start
);
774 node
= of_find_matching_node_by_address(NULL
, match
, phys_base
);
776 irq_domain_add_legacy(node
, 32, irq_start
, 0,
777 &irq_domain_simple_ops
, NULL
);
779 EXPORT_SYMBOL_GPL(irq_domain_generate_simple
);