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 /* legacy 8259, gets irqs 1..15 */
17 #define IRQ_DOMAIN_MAP_NOMAP 1 /* no fast reverse mapping */
18 #define IRQ_DOMAIN_MAP_LINEAR 2 /* linear map of interrupts */
19 #define IRQ_DOMAIN_MAP_TREE 3 /* radix tree */
21 static LIST_HEAD(irq_domain_list);
22 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);
78 * irq_domain_add_legacy() - Allocate and register a legacy revmap irq_domain.
79 * @of_node: pointer to interrupt controller's device tree node.
80 * @ops: map/unmap domain callbacks
81 * @host_data: Controller private data pointer
83 * Note: the map() callback will be called before this function returns
84 * for all legacy interrupts except 0 (which is always the invalid irq for
85 * a legacy controller).
87 struct irq_domain *irq_domain_add_legacy(struct device_node *of_node,
88 struct irq_domain_ops *ops,
91 struct irq_domain *domain, *h;
94 domain = irq_domain_alloc(of_node, IRQ_DOMAIN_MAP_LEGACY, ops, host_data);
98 mutex_lock(&irq_domain_mutex);
99 /* Make sure only one legacy controller can be created */
100 list_for_each_entry(h, &irq_domain_list, link) {
101 if (WARN_ON(h->revmap_type == IRQ_DOMAIN_MAP_LEGACY)) {
102 mutex_unlock(&irq_domain_mutex);
103 of_node_put(domain->of_node);
108 list_add(&domain->link, &irq_domain_list);
109 mutex_unlock(&irq_domain_mutex);
111 /* setup us as the domain for all legacy interrupts */
112 for (i = 1; i < NUM_ISA_INTERRUPTS; i++) {
113 struct irq_data *irq_data = irq_get_irq_data(i);
115 irq_data->domain = domain;
117 /* Legacy flags are left to default at this point,
118 * one can then use irq_create_mapping() to
119 * explicitly change them
121 ops->map(domain, i, i);
123 /* Clear norequest flags */
124 irq_clear_status_flags(i, IRQ_NOREQUEST);
130 * irq_domain_add_linear() - Allocate and register a legacy revmap irq_domain.
131 * @of_node: pointer to interrupt controller's device tree node.
132 * @ops: map/unmap domain callbacks
133 * @host_data: Controller private data pointer
135 struct irq_domain *irq_domain_add_linear(struct device_node *of_node,
137 struct irq_domain_ops *ops,
140 struct irq_domain *domain;
141 unsigned int *revmap;
143 revmap = kzalloc(sizeof(*revmap) * size, GFP_KERNEL);
144 if (WARN_ON(!revmap))
147 domain = irq_domain_alloc(of_node, IRQ_DOMAIN_MAP_LINEAR, ops, host_data);
152 domain->revmap_data.linear.size = size;
153 domain->revmap_data.linear.revmap = revmap;
154 irq_domain_add(domain);
158 struct irq_domain *irq_domain_add_nomap(struct device_node *of_node,
159 struct irq_domain_ops *ops,
162 struct irq_domain *domain = irq_domain_alloc(of_node,
163 IRQ_DOMAIN_MAP_NOMAP, ops, host_data);
165 irq_domain_add(domain);
170 * irq_domain_add_tree()
171 * @of_node: pointer to interrupt controller's device tree node.
172 * @ops: map/unmap domain callbacks
174 * Note: The radix tree will be allocated later during boot automatically
175 * (the reverse mapping will use the slow path until that happens).
177 struct irq_domain *irq_domain_add_tree(struct device_node *of_node,
178 struct irq_domain_ops *ops,
181 struct irq_domain *domain = irq_domain_alloc(of_node,
182 IRQ_DOMAIN_MAP_TREE, ops, host_data);
184 INIT_RADIX_TREE(&domain->revmap_data.tree, GFP_KERNEL);
185 irq_domain_add(domain);
191 * irq_find_host() - Locates a domain for a given device node
192 * @node: device-tree node of the interrupt controller
194 struct irq_domain *irq_find_host(struct device_node *node)
196 struct irq_domain *h, *found = NULL;
198 /* We might want to match the legacy controller last since
199 * it might potentially be set to match all interrupts in
200 * the absence of a device node. This isn't a problem so far
203 mutex_lock(&irq_domain_mutex);
204 list_for_each_entry(h, &irq_domain_list, link)
205 if (h->ops->match(h, node)) {
209 mutex_unlock(&irq_domain_mutex);
212 EXPORT_SYMBOL_GPL(irq_find_host);
215 * irq_set_default_host() - Set a "default" irq domain
216 * @domain: default domain pointer
218 * For convenience, it's possible to set a "default" domain that will be used
219 * whenever NULL is passed to irq_create_mapping(). It makes life easier for
220 * platforms that want to manipulate a few hard coded interrupt numbers that
221 * aren't properly represented in the device-tree.
223 void irq_set_default_host(struct irq_domain *domain)
225 pr_debug("irq: Default domain set to @0x%p\n", domain);
227 irq_default_domain = domain;
231 * irq_set_virq_count() - Set the maximum number of linux irqs
232 * @count: number of linux irqs, capped with NR_IRQS
234 * This is mainly for use by platforms like iSeries who want to program
235 * the virtual irq number in the controller to avoid the reverse mapping
237 void irq_set_virq_count(unsigned int count)
239 pr_debug("irq: Trying to set virq count to %d\n", count);
241 BUG_ON(count < NUM_ISA_INTERRUPTS);
243 irq_virq_count = count;
246 static int irq_setup_virq(struct irq_domain *domain, unsigned int virq,
247 irq_hw_number_t hwirq)
249 struct irq_data *irq_data = irq_get_irq_data(virq);
251 irq_data->hwirq = hwirq;
252 irq_data->domain = domain;
253 if (domain->ops->map(domain, virq, hwirq)) {
254 pr_debug("irq: -> mapping failed, freeing\n");
255 irq_data->domain = NULL;
260 irq_clear_status_flags(virq, IRQ_NOREQUEST);
266 * irq_create_direct_mapping() - Allocate an irq for direct mapping
267 * @domain: domain to allocate the irq for or NULL for default domain
269 * This routine is used for irq controllers which can choose the hardware
270 * interrupt numbers they generate. In such a case it's simplest to use
271 * the linux irq as the hardware interrupt number.
273 unsigned int irq_create_direct_mapping(struct irq_domain *domain)
278 domain = irq_default_domain;
280 BUG_ON(domain == NULL);
281 WARN_ON(domain->revmap_type != IRQ_DOMAIN_MAP_NOMAP);
283 virq = irq_alloc_desc_from(1, 0);
285 pr_debug("irq: create_direct virq allocation failed\n");
288 if (virq >= irq_virq_count) {
289 pr_err("ERROR: no free irqs available below %i maximum\n",
295 pr_debug("irq: create_direct obtained virq %d\n", virq);
297 if (irq_setup_virq(domain, virq, virq)) {
306 * irq_create_mapping() - Map a hardware interrupt into linux irq space
307 * @domain: domain owning this hardware interrupt or NULL for default domain
308 * @hwirq: hardware irq number in that domain space
310 * Only one mapping per hardware interrupt is permitted. Returns a linux
312 * If the sense/trigger is to be specified, set_irq_type() should be called
313 * on the number returned from that call.
315 unsigned int irq_create_mapping(struct irq_domain *domain,
316 irq_hw_number_t hwirq)
318 unsigned int virq, hint;
320 pr_debug("irq: irq_create_mapping(0x%p, 0x%lx)\n", domain, hwirq);
322 /* Look for default domain if nececssary */
324 domain = irq_default_domain;
325 if (domain == NULL) {
326 printk(KERN_WARNING "irq_create_mapping called for"
327 " NULL domain, hwirq=%lx\n", hwirq);
331 pr_debug("irq: -> using domain @%p\n", domain);
333 /* Check if mapping already exists */
334 virq = irq_find_mapping(domain, hwirq);
336 pr_debug("irq: -> existing mapping on virq %d\n", virq);
340 /* Get a virtual interrupt number */
341 if (domain->revmap_type == IRQ_DOMAIN_MAP_LEGACY) {
343 virq = (unsigned int)hwirq;
344 if (virq == 0 || virq >= NUM_ISA_INTERRUPTS)
348 /* Allocate a virtual interrupt number */
349 hint = hwirq % irq_virq_count;
352 virq = irq_alloc_desc_from(hint, 0);
354 virq = irq_alloc_desc_from(1, 0);
356 pr_debug("irq: -> virq allocation failed\n");
361 if (irq_setup_virq(domain, virq, hwirq)) {
362 if (domain->revmap_type != IRQ_DOMAIN_MAP_LEGACY)
367 pr_debug("irq: irq %lu on domain %s mapped to virtual irq %u\n",
368 hwirq, domain->of_node ? domain->of_node->full_name : "null", virq);
372 EXPORT_SYMBOL_GPL(irq_create_mapping);
374 unsigned int irq_create_of_mapping(struct device_node *controller,
375 const u32 *intspec, unsigned int intsize)
377 struct irq_domain *domain;
378 irq_hw_number_t hwirq;
379 unsigned int type = IRQ_TYPE_NONE;
382 domain = controller ? irq_find_host(controller) : irq_default_domain;
384 printk(KERN_WARNING "irq: no irq domain found for %s !\n",
385 controller->full_name);
389 /* If domain has no translation, then we assume interrupt line */
390 if (domain->ops->xlate == NULL)
393 if (domain->ops->xlate(domain, controller, intspec, intsize,
399 virq = irq_create_mapping(domain, hwirq);
403 /* Set type if specified and different than the current one */
404 if (type != IRQ_TYPE_NONE &&
405 type != (irqd_get_trigger_type(irq_get_irq_data(virq))))
406 irq_set_irq_type(virq, type);
409 EXPORT_SYMBOL_GPL(irq_create_of_mapping);
412 * irq_dispose_mapping() - Unmap an interrupt
413 * @virq: linux irq number of the interrupt to unmap
415 void irq_dispose_mapping(unsigned int virq)
417 struct irq_data *irq_data = irq_get_irq_data(virq);
418 struct irq_domain *domain;
419 irq_hw_number_t hwirq;
421 if (!virq || !irq_data)
424 domain = irq_data->domain;
425 if (WARN_ON(domain == NULL))
428 /* Never unmap legacy interrupts */
429 if (domain->revmap_type == IRQ_DOMAIN_MAP_LEGACY)
432 irq_set_status_flags(virq, IRQ_NOREQUEST);
434 /* remove chip and handler */
435 irq_set_chip_and_handler(virq, NULL, NULL);
437 /* Make sure it's completed */
438 synchronize_irq(virq);
440 /* Tell the PIC about it */
441 if (domain->ops->unmap)
442 domain->ops->unmap(domain, virq);
445 /* Clear reverse map */
446 hwirq = irq_data->hwirq;
447 switch(domain->revmap_type) {
448 case IRQ_DOMAIN_MAP_LINEAR:
449 if (hwirq < domain->revmap_data.linear.size)
450 domain->revmap_data.linear.revmap[hwirq] = 0;
452 case IRQ_DOMAIN_MAP_TREE:
453 mutex_lock(&revmap_trees_mutex);
454 radix_tree_delete(&domain->revmap_data.tree, hwirq);
455 mutex_unlock(&revmap_trees_mutex);
461 EXPORT_SYMBOL_GPL(irq_dispose_mapping);
464 * irq_find_mapping() - Find a linux irq from an hw irq number.
465 * @domain: domain owning this hardware interrupt
466 * @hwirq: hardware irq number in that domain space
468 * This is a slow path, for use by generic code. It's expected that an
469 * irq controller implementation directly calls the appropriate low level
472 unsigned int irq_find_mapping(struct irq_domain *domain,
473 irq_hw_number_t hwirq)
476 unsigned int hint = hwirq % irq_virq_count;
478 /* Look for default domain if nececssary */
480 domain = irq_default_domain;
484 /* legacy -> bail early */
485 if (domain->revmap_type == IRQ_DOMAIN_MAP_LEGACY)
488 /* Slow path does a linear search of the map */
493 struct irq_data *data = irq_get_irq_data(i);
494 if (data && (data->domain == domain) && (data->hwirq == hwirq))
497 if (i >= irq_virq_count)
502 EXPORT_SYMBOL_GPL(irq_find_mapping);
505 * irq_radix_revmap_lookup() - Find a linux irq from a hw irq number.
506 * @domain: domain owning this hardware interrupt
507 * @hwirq: hardware irq number in that domain space
509 * This is a fast path, for use by irq controller code that uses radix tree
512 unsigned int irq_radix_revmap_lookup(struct irq_domain *domain,
513 irq_hw_number_t hwirq)
515 struct irq_data *irq_data;
517 if (WARN_ON_ONCE(domain->revmap_type != IRQ_DOMAIN_MAP_TREE))
518 return irq_find_mapping(domain, hwirq);
521 * Freeing an irq can delete nodes along the path to
522 * do the lookup via call_rcu.
525 irq_data = radix_tree_lookup(&domain->revmap_data.tree, hwirq);
529 * If found in radix tree, then fine.
530 * Else fallback to linear lookup - this should not happen in practice
531 * as it means that we failed to insert the node in the radix tree.
533 return irq_data ? irq_data->irq : irq_find_mapping(domain, hwirq);
537 * irq_radix_revmap_insert() - Insert a hw irq to linux irq number mapping.
538 * @domain: domain owning this hardware interrupt
539 * @virq: linux irq number
540 * @hwirq: hardware irq number in that domain space
542 * This is for use by irq controllers that use a radix tree reverse
543 * mapping for fast lookup.
545 void irq_radix_revmap_insert(struct irq_domain *domain, unsigned int virq,
546 irq_hw_number_t hwirq)
548 struct irq_data *irq_data = irq_get_irq_data(virq);
550 if (WARN_ON(domain->revmap_type != IRQ_DOMAIN_MAP_TREE))
554 mutex_lock(&revmap_trees_mutex);
555 radix_tree_insert(&domain->revmap_data.tree, hwirq, irq_data);
556 mutex_unlock(&revmap_trees_mutex);
561 * irq_linear_revmap() - Find a linux irq from a hw irq number.
562 * @domain: domain owning this hardware interrupt
563 * @hwirq: hardware irq number in that domain space
565 * This is a fast path, for use by irq controller code that uses linear
566 * revmaps. It does fallback to the slow path if the revmap doesn't exist
567 * yet and will create the revmap entry with appropriate locking
569 unsigned int irq_linear_revmap(struct irq_domain *domain,
570 irq_hw_number_t hwirq)
572 unsigned int *revmap;
574 if (WARN_ON_ONCE(domain->revmap_type != IRQ_DOMAIN_MAP_LINEAR))
575 return irq_find_mapping(domain, hwirq);
577 /* Check revmap bounds */
578 if (unlikely(hwirq >= domain->revmap_data.linear.size))
579 return irq_find_mapping(domain, hwirq);
581 /* Check if revmap was allocated */
582 revmap = domain->revmap_data.linear.revmap;
583 if (unlikely(revmap == NULL))
584 return irq_find_mapping(domain, hwirq);
586 /* Fill up revmap with slow path if no mapping found */
587 if (unlikely(!revmap[hwirq]))
588 revmap[hwirq] = irq_find_mapping(domain, hwirq);
590 return revmap[hwirq];
593 #ifdef CONFIG_VIRQ_DEBUG
594 static int virq_debug_show(struct seq_file *m, void *private)
597 struct irq_desc *desc;
599 static const char none[] = "none";
603 seq_printf(m, "%-5s %-7s %-15s %-18s %s\n", "virq", "hwirq",
604 "chip name", "chip data", "domain name");
606 for (i = 1; i < nr_irqs; i++) {
607 desc = irq_to_desc(i);
611 raw_spin_lock_irqsave(&desc->lock, flags);
613 if (desc->action && desc->action->handler) {
614 struct irq_chip *chip;
616 seq_printf(m, "%5d ", i);
617 seq_printf(m, "0x%05lx ", desc->irq_data.hwirq);
619 chip = irq_desc_get_chip(desc);
620 if (chip && chip->name)
624 seq_printf(m, "%-15s ", p);
626 data = irq_desc_get_chip_data(desc);
627 seq_printf(m, "0x%16p ", data);
629 if (desc->irq_data.domain->of_node)
630 p = desc->irq_data.domain->of_node->full_name;
633 seq_printf(m, "%s\n", p);
636 raw_spin_unlock_irqrestore(&desc->lock, flags);
642 static int virq_debug_open(struct inode *inode, struct file *file)
644 return single_open(file, virq_debug_show, inode->i_private);
647 static const struct file_operations virq_debug_fops = {
648 .open = virq_debug_open,
651 .release = single_release,
654 static int __init irq_debugfs_init(void)
656 if (debugfs_create_file("virq_mapping", S_IRUGO, powerpc_debugfs_root,
657 NULL, &virq_debug_fops) == NULL)
662 __initcall(irq_debugfs_init);
663 #endif /* CONFIG_VIRQ_DEBUG */
665 #else /* CONFIG_PPC */
668 * irq_domain_add() - Register an irq_domain
669 * @domain: ptr to initialized irq_domain structure
671 * Registers an irq_domain structure. The irq_domain must at a minimum be
672 * initialized with an ops structure pointer, and either a ->to_irq hook or
673 * a valid irq_base value. Everything else is optional.
675 void irq_domain_add(struct irq_domain *domain)
681 * This assumes that the irq_domain owner has already allocated
682 * the irq_descs. This block will be removed when support for dynamic
683 * allocation of irq_descs is added to irq_domain.
685 irq_domain_for_each_irq(domain, hwirq, irq) {
686 d = irq_get_irq_data(irq);
688 WARN(1, "error: assigning domain to non existant irq_desc");
692 /* things are broken; just report, don't clean up */
693 WARN(1, "error: irq_desc already assigned to a domain");
700 mutex_lock(&irq_domain_mutex);
701 list_add(&domain->link, &irq_domain_list);
702 mutex_unlock(&irq_domain_mutex);
706 * irq_domain_del() - Unregister an irq_domain
707 * @domain: ptr to registered irq_domain.
709 void irq_domain_del(struct irq_domain *domain)
714 mutex_lock(&irq_domain_mutex);
715 list_del(&domain->link);
716 mutex_unlock(&irq_domain_mutex);
718 /* Clear the irq_domain assignments */
719 irq_domain_for_each_irq(domain, hwirq, irq) {
720 d = irq_get_irq_data(irq);
725 #if defined(CONFIG_OF_IRQ)
727 * irq_create_of_mapping() - Map a linux irq number from a DT interrupt spec
729 * Used by the device tree interrupt mapping code to translate a device tree
730 * interrupt specifier to a valid linux irq number. Returns either a valid
731 * linux IRQ number or 0.
733 * When the caller no longer need the irq number returned by this function it
734 * should arrange to call irq_dispose_mapping().
736 unsigned int irq_create_of_mapping(struct device_node *controller,
737 const u32 *intspec, unsigned int intsize)
739 struct irq_domain *domain;
741 unsigned int irq, type;
744 /* Find a domain which can translate the irq spec */
745 mutex_lock(&irq_domain_mutex);
746 list_for_each_entry(domain, &irq_domain_list, link) {
747 if (!domain->ops->xlate)
749 rc = domain->ops->xlate(domain, controller,
750 intspec, intsize, &hwirq, &type);
754 mutex_unlock(&irq_domain_mutex);
759 irq = irq_domain_to_irq(domain, hwirq);
760 if (type != IRQ_TYPE_NONE)
761 irq_set_irq_type(irq, type);
762 pr_debug("%s: mapped hwirq=%i to irq=%i, flags=%x\n",
763 controller->full_name, (int)hwirq, irq, type);
766 EXPORT_SYMBOL_GPL(irq_create_of_mapping);
769 * irq_dispose_mapping() - Discard a mapping created by irq_create_of_mapping()
770 * @irq: linux irq number to be discarded
772 * Calling this function indicates the caller no longer needs a reference to
773 * the linux irq number returned by a prior call to irq_create_of_mapping().
775 void irq_dispose_mapping(unsigned int irq)
778 * nothing yet; will be filled when support for dynamic allocation of
779 * irq_descs is added to irq_domain
782 EXPORT_SYMBOL_GPL(irq_dispose_mapping);
784 int irq_domain_simple_xlate(struct irq_domain *d,
785 struct device_node *controller,
786 const u32 *intspec, unsigned int intsize,
787 unsigned long *out_hwirq, unsigned int *out_type)
789 if (d->of_node != controller)
793 if (d->nr_irq && ((intspec[0] < d->hwirq_base) ||
794 (intspec[0] >= d->hwirq_base + d->nr_irq)))
797 *out_hwirq = intspec[0];
798 *out_type = IRQ_TYPE_NONE;
800 *out_type = intspec[1] & IRQ_TYPE_SENSE_MASK;
805 * irq_domain_create_simple() - Set up a 'simple' translation range
807 void irq_domain_add_simple(struct device_node *controller, int irq_base)
809 struct irq_domain *domain;
811 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
817 domain->irq_base = irq_base;
818 domain->of_node = of_node_get(controller);
819 domain->ops = &irq_domain_simple_ops;
820 irq_domain_add(domain);
822 EXPORT_SYMBOL_GPL(irq_domain_add_simple);
824 void irq_domain_generate_simple(const struct of_device_id *match,
825 u64 phys_base, unsigned int irq_start)
827 struct device_node *node;
828 pr_debug("looking for phys_base=%llx, irq_start=%i\n",
829 (unsigned long long) phys_base, (int) irq_start);
830 node = of_find_matching_node_by_address(NULL, match, phys_base);
832 irq_domain_add_simple(node, irq_start);
834 EXPORT_SYMBOL_GPL(irq_domain_generate_simple);
835 #endif /* CONFIG_OF_IRQ */
837 struct irq_domain_ops irq_domain_simple_ops = {
839 .xlate = irq_domain_simple_xlate,
840 #endif /* CONFIG_OF_IRQ */
842 EXPORT_SYMBOL_GPL(irq_domain_simple_ops);
844 #endif /* !CONFIG_PPC */