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 unsigned int irq_virq_count = NR_IRQS;
27 static struct irq_domain *irq_default_domain;
29 static int default_irq_domain_match(struct irq_domain *d, struct device_node *np)
31 return d->of_node != NULL && d->of_node == np;
35 * irq_domain_alloc() - Allocate a new irq_domain data structure
36 * @of_node: optional device-tree node of the interrupt controller
37 * @revmap_type: type of reverse mapping to use
38 * @ops: map/unmap domain callbacks
39 * @host_data: Controller private data pointer
41 * Allocates and initialize and irq_domain structure. Caller is expected to
42 * register allocated irq_domain with irq_domain_register(). Returns pointer
43 * to IRQ domain, or NULL on failure.
45 static struct irq_domain *irq_domain_alloc(struct device_node *of_node,
46 unsigned int revmap_type,
47 struct irq_domain_ops *ops,
50 struct irq_domain *domain;
52 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
57 domain->revmap_type = revmap_type;
59 domain->host_data = host_data;
60 domain->of_node = of_node_get(of_node);
62 if (domain->ops->match == NULL)
63 domain->ops->match = default_irq_domain_match;
68 static void irq_domain_add(struct irq_domain *domain)
70 mutex_lock(&irq_domain_mutex);
71 list_add(&domain->link, &irq_domain_list);
72 mutex_unlock(&irq_domain_mutex);
73 pr_debug("irq: Allocated domain of type %d @0x%p\n",
74 domain->revmap_type, domain);
77 static unsigned int irq_domain_legacy_revmap(struct irq_domain *domain,
78 irq_hw_number_t hwirq)
80 irq_hw_number_t first_hwirq = domain->revmap_data.legacy.first_hwirq;
81 int size = domain->revmap_data.legacy.size;
83 if (WARN_ON(hwirq < first_hwirq || hwirq >= first_hwirq + size))
85 return hwirq - first_hwirq + domain->revmap_data.legacy.first_irq;
89 * irq_domain_add_legacy() - Allocate and register a legacy revmap irq_domain.
90 * @of_node: pointer to interrupt controller's device tree node.
91 * @size: total number of irqs in legacy mapping
92 * @first_irq: first number of irq block assigned to the domain
93 * @first_hwirq: first hwirq number to use for the translation. Should normally
94 * be '0', but a positive integer can be used if the effective
95 * hwirqs numbering does not begin at zero.
96 * @ops: map/unmap domain callbacks
97 * @host_data: Controller private data pointer
99 * Note: the map() callback will be called before this function returns
100 * for all legacy interrupts except 0 (which is always the invalid irq for
101 * a legacy controller).
103 struct irq_domain *irq_domain_add_legacy(struct device_node *of_node,
105 unsigned int first_irq,
106 irq_hw_number_t first_hwirq,
107 struct irq_domain_ops *ops,
110 struct irq_domain *domain;
113 domain = irq_domain_alloc(of_node, IRQ_DOMAIN_MAP_LEGACY, ops, host_data);
117 domain->revmap_data.legacy.first_irq = first_irq;
118 domain->revmap_data.legacy.first_hwirq = first_hwirq;
119 domain->revmap_data.legacy.size = size;
121 mutex_lock(&irq_domain_mutex);
122 /* Verify that all the irqs are available */
123 for (i = 0; i < size; i++) {
124 int irq = first_irq + i;
125 struct irq_data *irq_data = irq_get_irq_data(irq);
127 if (WARN_ON(!irq_data || irq_data->domain)) {
128 mutex_unlock(&irq_domain_mutex);
129 of_node_put(domain->of_node);
135 /* Claim all of the irqs before registering a legacy domain */
136 for (i = 0; i < size; i++) {
137 struct irq_data *irq_data = irq_get_irq_data(first_irq + i);
138 irq_data->hwirq = first_hwirq + i;
139 irq_data->domain = domain;
141 mutex_unlock(&irq_domain_mutex);
143 for (i = 0; i < size; i++) {
144 int irq = first_irq + i;
145 int hwirq = first_hwirq + i;
147 /* IRQ0 gets ignored */
151 /* Legacy flags are left to default at this point,
152 * one can then use irq_create_mapping() to
153 * explicitly change them
155 ops->map(domain, irq, hwirq);
157 /* Clear norequest flags */
158 irq_clear_status_flags(irq, IRQ_NOREQUEST);
161 irq_domain_add(domain);
166 * irq_domain_add_linear() - Allocate and register a legacy revmap irq_domain.
167 * @of_node: pointer to interrupt controller's device tree node.
168 * @ops: map/unmap domain callbacks
169 * @host_data: Controller private data pointer
171 struct irq_domain *irq_domain_add_linear(struct device_node *of_node,
173 struct irq_domain_ops *ops,
176 struct irq_domain *domain;
177 unsigned int *revmap;
179 revmap = kzalloc(sizeof(*revmap) * size, GFP_KERNEL);
180 if (WARN_ON(!revmap))
183 domain = irq_domain_alloc(of_node, IRQ_DOMAIN_MAP_LINEAR, ops, host_data);
188 domain->revmap_data.linear.size = size;
189 domain->revmap_data.linear.revmap = revmap;
190 irq_domain_add(domain);
194 struct irq_domain *irq_domain_add_nomap(struct device_node *of_node,
195 struct irq_domain_ops *ops,
198 struct irq_domain *domain = irq_domain_alloc(of_node,
199 IRQ_DOMAIN_MAP_NOMAP, ops, host_data);
201 irq_domain_add(domain);
206 * irq_domain_add_tree()
207 * @of_node: pointer to interrupt controller's device tree node.
208 * @ops: map/unmap domain callbacks
210 * Note: The radix tree will be allocated later during boot automatically
211 * (the reverse mapping will use the slow path until that happens).
213 struct irq_domain *irq_domain_add_tree(struct device_node *of_node,
214 struct irq_domain_ops *ops,
217 struct irq_domain *domain = irq_domain_alloc(of_node,
218 IRQ_DOMAIN_MAP_TREE, ops, host_data);
220 INIT_RADIX_TREE(&domain->revmap_data.tree, GFP_KERNEL);
221 irq_domain_add(domain);
227 * irq_find_host() - Locates a domain for a given device node
228 * @node: device-tree node of the interrupt controller
230 struct irq_domain *irq_find_host(struct device_node *node)
232 struct irq_domain *h, *found = NULL;
234 /* We might want to match the legacy controller last since
235 * it might potentially be set to match all interrupts in
236 * the absence of a device node. This isn't a problem so far
239 mutex_lock(&irq_domain_mutex);
240 list_for_each_entry(h, &irq_domain_list, link)
241 if (h->ops->match(h, node)) {
245 mutex_unlock(&irq_domain_mutex);
248 EXPORT_SYMBOL_GPL(irq_find_host);
251 * irq_set_default_host() - Set a "default" irq domain
252 * @domain: default domain pointer
254 * For convenience, it's possible to set a "default" domain that will be used
255 * whenever NULL is passed to irq_create_mapping(). It makes life easier for
256 * platforms that want to manipulate a few hard coded interrupt numbers that
257 * aren't properly represented in the device-tree.
259 void irq_set_default_host(struct irq_domain *domain)
261 pr_debug("irq: Default domain set to @0x%p\n", domain);
263 irq_default_domain = domain;
267 * irq_set_virq_count() - Set the maximum number of linux irqs
268 * @count: number of linux irqs, capped with NR_IRQS
270 * This is mainly for use by platforms like iSeries who want to program
271 * the virtual irq number in the controller to avoid the reverse mapping
273 void irq_set_virq_count(unsigned int count)
275 pr_debug("irq: Trying to set virq count to %d\n", count);
277 BUG_ON(count < NUM_ISA_INTERRUPTS);
279 irq_virq_count = count;
282 static int irq_setup_virq(struct irq_domain *domain, unsigned int virq,
283 irq_hw_number_t hwirq)
285 struct irq_data *irq_data = irq_get_irq_data(virq);
287 irq_data->hwirq = hwirq;
288 irq_data->domain = domain;
289 if (domain->ops->map(domain, virq, hwirq)) {
290 pr_debug("irq: -> mapping failed, freeing\n");
291 irq_data->domain = NULL;
296 irq_clear_status_flags(virq, IRQ_NOREQUEST);
302 * irq_create_direct_mapping() - Allocate an irq for direct mapping
303 * @domain: domain to allocate the irq for or NULL for default domain
305 * This routine is used for irq controllers which can choose the hardware
306 * interrupt numbers they generate. In such a case it's simplest to use
307 * the linux irq as the hardware interrupt number.
309 unsigned int irq_create_direct_mapping(struct irq_domain *domain)
314 domain = irq_default_domain;
316 BUG_ON(domain == NULL);
317 WARN_ON(domain->revmap_type != IRQ_DOMAIN_MAP_NOMAP);
319 virq = irq_alloc_desc_from(1, 0);
321 pr_debug("irq: create_direct virq allocation failed\n");
324 if (virq >= irq_virq_count) {
325 pr_err("ERROR: no free irqs available below %i maximum\n",
331 pr_debug("irq: create_direct obtained virq %d\n", virq);
333 if (irq_setup_virq(domain, virq, virq)) {
342 * irq_create_mapping() - Map a hardware interrupt into linux irq space
343 * @domain: domain owning this hardware interrupt or NULL for default domain
344 * @hwirq: hardware irq number in that domain space
346 * Only one mapping per hardware interrupt is permitted. Returns a linux
348 * If the sense/trigger is to be specified, set_irq_type() should be called
349 * on the number returned from that call.
351 unsigned int irq_create_mapping(struct irq_domain *domain,
352 irq_hw_number_t hwirq)
354 unsigned int virq, hint;
356 pr_debug("irq: irq_create_mapping(0x%p, 0x%lx)\n", domain, hwirq);
358 /* Look for default domain if nececssary */
360 domain = irq_default_domain;
361 if (domain == NULL) {
362 printk(KERN_WARNING "irq_create_mapping called for"
363 " NULL domain, hwirq=%lx\n", hwirq);
367 pr_debug("irq: -> using domain @%p\n", domain);
369 /* Check if mapping already exists */
370 virq = irq_find_mapping(domain, hwirq);
372 pr_debug("irq: -> existing mapping on virq %d\n", virq);
376 /* Get a virtual interrupt number */
377 if (domain->revmap_type == IRQ_DOMAIN_MAP_LEGACY)
378 return irq_domain_legacy_revmap(domain, hwirq);
380 /* Allocate a virtual interrupt number */
381 hint = hwirq % irq_virq_count;
384 virq = irq_alloc_desc_from(hint, 0);
386 virq = irq_alloc_desc_from(1, 0);
388 pr_debug("irq: -> virq allocation failed\n");
392 if (irq_setup_virq(domain, virq, hwirq)) {
393 if (domain->revmap_type != IRQ_DOMAIN_MAP_LEGACY)
398 pr_debug("irq: irq %lu on domain %s mapped to virtual irq %u\n",
399 hwirq, domain->of_node ? domain->of_node->full_name : "null", virq);
403 EXPORT_SYMBOL_GPL(irq_create_mapping);
405 unsigned int irq_create_of_mapping(struct device_node *controller,
406 const u32 *intspec, unsigned int intsize)
408 struct irq_domain *domain;
409 irq_hw_number_t hwirq;
410 unsigned int type = IRQ_TYPE_NONE;
413 domain = controller ? irq_find_host(controller) : irq_default_domain;
415 printk(KERN_WARNING "irq: no irq domain found for %s !\n",
416 controller->full_name);
420 /* If domain has no translation, then we assume interrupt line */
421 if (domain->ops->xlate == NULL)
424 if (domain->ops->xlate(domain, controller, intspec, intsize,
430 virq = irq_create_mapping(domain, hwirq);
434 /* Set type if specified and different than the current one */
435 if (type != IRQ_TYPE_NONE &&
436 type != (irqd_get_trigger_type(irq_get_irq_data(virq))))
437 irq_set_irq_type(virq, type);
440 EXPORT_SYMBOL_GPL(irq_create_of_mapping);
443 * irq_dispose_mapping() - Unmap an interrupt
444 * @virq: linux irq number of the interrupt to unmap
446 void irq_dispose_mapping(unsigned int virq)
448 struct irq_data *irq_data = irq_get_irq_data(virq);
449 struct irq_domain *domain;
450 irq_hw_number_t hwirq;
452 if (!virq || !irq_data)
455 domain = irq_data->domain;
456 if (WARN_ON(domain == NULL))
459 /* Never unmap legacy interrupts */
460 if (domain->revmap_type == IRQ_DOMAIN_MAP_LEGACY)
463 irq_set_status_flags(virq, IRQ_NOREQUEST);
465 /* remove chip and handler */
466 irq_set_chip_and_handler(virq, NULL, NULL);
468 /* Make sure it's completed */
469 synchronize_irq(virq);
471 /* Tell the PIC about it */
472 if (domain->ops->unmap)
473 domain->ops->unmap(domain, virq);
476 /* Clear reverse map */
477 hwirq = irq_data->hwirq;
478 switch(domain->revmap_type) {
479 case IRQ_DOMAIN_MAP_LINEAR:
480 if (hwirq < domain->revmap_data.linear.size)
481 domain->revmap_data.linear.revmap[hwirq] = 0;
483 case IRQ_DOMAIN_MAP_TREE:
484 mutex_lock(&revmap_trees_mutex);
485 radix_tree_delete(&domain->revmap_data.tree, hwirq);
486 mutex_unlock(&revmap_trees_mutex);
492 EXPORT_SYMBOL_GPL(irq_dispose_mapping);
495 * irq_find_mapping() - Find a linux irq from an hw irq number.
496 * @domain: domain owning this hardware interrupt
497 * @hwirq: hardware irq number in that domain space
499 * This is a slow path, for use by generic code. It's expected that an
500 * irq controller implementation directly calls the appropriate low level
503 unsigned int irq_find_mapping(struct irq_domain *domain,
504 irq_hw_number_t hwirq)
507 unsigned int hint = hwirq % irq_virq_count;
509 /* Look for default domain if nececssary */
511 domain = irq_default_domain;
515 /* legacy -> bail early */
516 if (domain->revmap_type == IRQ_DOMAIN_MAP_LEGACY)
517 return irq_domain_legacy_revmap(domain, hwirq);
519 /* Slow path does a linear search of the map */
524 struct irq_data *data = irq_get_irq_data(i);
525 if (data && (data->domain == domain) && (data->hwirq == hwirq))
528 if (i >= irq_virq_count)
533 EXPORT_SYMBOL_GPL(irq_find_mapping);
536 * irq_radix_revmap_lookup() - Find a linux irq from a hw irq number.
537 * @domain: domain owning this hardware interrupt
538 * @hwirq: hardware irq number in that domain space
540 * This is a fast path, for use by irq controller code that uses radix tree
543 unsigned int irq_radix_revmap_lookup(struct irq_domain *domain,
544 irq_hw_number_t hwirq)
546 struct irq_data *irq_data;
548 if (WARN_ON_ONCE(domain->revmap_type != IRQ_DOMAIN_MAP_TREE))
549 return irq_find_mapping(domain, hwirq);
552 * Freeing an irq can delete nodes along the path to
553 * do the lookup via call_rcu.
556 irq_data = radix_tree_lookup(&domain->revmap_data.tree, hwirq);
560 * If found in radix tree, then fine.
561 * Else fallback to linear lookup - this should not happen in practice
562 * as it means that we failed to insert the node in the radix tree.
564 return irq_data ? irq_data->irq : irq_find_mapping(domain, hwirq);
568 * irq_radix_revmap_insert() - Insert a hw irq to linux irq number mapping.
569 * @domain: domain owning this hardware interrupt
570 * @virq: linux irq number
571 * @hwirq: hardware irq number in that domain space
573 * This is for use by irq controllers that use a radix tree reverse
574 * mapping for fast lookup.
576 void irq_radix_revmap_insert(struct irq_domain *domain, unsigned int virq,
577 irq_hw_number_t hwirq)
579 struct irq_data *irq_data = irq_get_irq_data(virq);
581 if (WARN_ON(domain->revmap_type != IRQ_DOMAIN_MAP_TREE))
585 mutex_lock(&revmap_trees_mutex);
586 radix_tree_insert(&domain->revmap_data.tree, hwirq, irq_data);
587 mutex_unlock(&revmap_trees_mutex);
592 * irq_linear_revmap() - Find a linux irq from a hw irq number.
593 * @domain: domain owning this hardware interrupt
594 * @hwirq: hardware irq number in that domain space
596 * This is a fast path, for use by irq controller code that uses linear
597 * revmaps. It does fallback to the slow path if the revmap doesn't exist
598 * yet and will create the revmap entry with appropriate locking
600 unsigned int irq_linear_revmap(struct irq_domain *domain,
601 irq_hw_number_t hwirq)
603 unsigned int *revmap;
605 if (WARN_ON_ONCE(domain->revmap_type != IRQ_DOMAIN_MAP_LINEAR))
606 return irq_find_mapping(domain, hwirq);
608 /* Check revmap bounds */
609 if (unlikely(hwirq >= domain->revmap_data.linear.size))
610 return irq_find_mapping(domain, hwirq);
612 /* Check if revmap was allocated */
613 revmap = domain->revmap_data.linear.revmap;
614 if (unlikely(revmap == NULL))
615 return irq_find_mapping(domain, hwirq);
617 /* Fill up revmap with slow path if no mapping found */
618 if (unlikely(!revmap[hwirq]))
619 revmap[hwirq] = irq_find_mapping(domain, hwirq);
621 return revmap[hwirq];
624 #ifdef CONFIG_VIRQ_DEBUG
625 static int virq_debug_show(struct seq_file *m, void *private)
628 struct irq_desc *desc;
630 static const char none[] = "none";
634 seq_printf(m, "%-5s %-7s %-15s %-18s %s\n", "virq", "hwirq",
635 "chip name", "chip data", "domain name");
637 for (i = 1; i < nr_irqs; i++) {
638 desc = irq_to_desc(i);
642 raw_spin_lock_irqsave(&desc->lock, flags);
644 if (desc->action && desc->action->handler) {
645 struct irq_chip *chip;
647 seq_printf(m, "%5d ", i);
648 seq_printf(m, "0x%05lx ", desc->irq_data.hwirq);
650 chip = irq_desc_get_chip(desc);
651 if (chip && chip->name)
655 seq_printf(m, "%-15s ", p);
657 data = irq_desc_get_chip_data(desc);
658 seq_printf(m, "0x%16p ", data);
660 if (desc->irq_data.domain->of_node)
661 p = desc->irq_data.domain->of_node->full_name;
664 seq_printf(m, "%s\n", p);
667 raw_spin_unlock_irqrestore(&desc->lock, flags);
673 static int virq_debug_open(struct inode *inode, struct file *file)
675 return single_open(file, virq_debug_show, inode->i_private);
678 static const struct file_operations virq_debug_fops = {
679 .open = virq_debug_open,
682 .release = single_release,
685 static int __init irq_debugfs_init(void)
687 if (debugfs_create_file("virq_mapping", S_IRUGO, powerpc_debugfs_root,
688 NULL, &virq_debug_fops) == NULL)
693 __initcall(irq_debugfs_init);
694 #endif /* CONFIG_VIRQ_DEBUG */
696 int irq_domain_simple_map(struct irq_domain *d, unsigned int irq,
697 irq_hw_number_t hwirq)
702 int irq_domain_simple_xlate(struct irq_domain *d,
703 struct device_node *controller,
704 const u32 *intspec, unsigned int intsize,
705 unsigned long *out_hwirq, unsigned int *out_type)
707 if (d->of_node != controller)
711 *out_hwirq = intspec[0];
712 *out_type = IRQ_TYPE_NONE;
714 *out_type = intspec[1] & IRQ_TYPE_SENSE_MASK;
718 struct irq_domain_ops irq_domain_simple_ops = {
719 .map = irq_domain_simple_map,
720 .xlate = irq_domain_simple_xlate,
722 EXPORT_SYMBOL_GPL(irq_domain_simple_ops);
725 void irq_domain_generate_simple(const struct of_device_id *match,
726 u64 phys_base, unsigned int irq_start)
728 struct device_node *node;
729 pr_debug("looking for phys_base=%llx, irq_start=%i\n",
730 (unsigned long long) phys_base, (int) irq_start);
731 node = of_find_matching_node_by_address(NULL, match, phys_base);
733 irq_domain_add_legacy(node, 32, irq_start, 0,
734 &irq_domain_simple_ops, NULL);
736 EXPORT_SYMBOL_GPL(irq_domain_generate_simple);