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 static LIST_HEAD(irq_domain_list);
17 static DEFINE_MUTEX(irq_domain_mutex);
20 static DEFINE_MUTEX(revmap_trees_mutex);
21 static unsigned int irq_virq_count = NR_IRQS;
22 static struct irq_domain *irq_default_host;
24 static int default_irq_host_match(struct irq_domain *h, struct device_node *np)
26 return h->of_node != NULL && h->of_node == np;
30 * irq_alloc_host() - Allocate a new irq_domain data structure
31 * @of_node: optional device-tree node of the interrupt controller
32 * @revmap_type: type of reverse mapping to use
33 * @revmap_arg: for IRQ_DOMAIN_MAP_LINEAR linear only: size of the map
34 * @ops: map/unmap host callbacks
35 * @inval_irq: provide a hw number in that host space that is always invalid
37 * Allocates and initialize and irq_domain structure. Note that in the case of
38 * IRQ_DOMAIN_MAP_LEGACY, the map() callback will be called before this returns
39 * for all legacy interrupts except 0 (which is always the invalid irq for
40 * a legacy controller). For a IRQ_DOMAIN_MAP_LINEAR, the map is allocated by
41 * this call as well. For a IRQ_DOMAIN_MAP_TREE, the radix tree will be
42 * allocated later during boot automatically (the reverse mapping will use the
43 * slow path until that happens).
45 struct irq_domain *irq_alloc_host(struct device_node *of_node,
46 unsigned int revmap_type,
47 unsigned int revmap_arg,
48 struct irq_domain_ops *ops,
49 irq_hw_number_t inval_irq)
51 struct irq_domain *host, *h;
52 unsigned int size = sizeof(struct irq_domain);
56 /* Allocate structure and revmap table if using linear mapping */
57 if (revmap_type == IRQ_DOMAIN_MAP_LINEAR)
58 size += revmap_arg * sizeof(unsigned int);
59 host = kzalloc(size, GFP_KERNEL);
64 host->revmap_type = revmap_type;
65 host->inval_irq = inval_irq;
67 host->of_node = of_node_get(of_node);
69 if (host->ops->match == NULL)
70 host->ops->match = default_irq_host_match;
72 mutex_lock(&irq_domain_mutex);
73 /* Make sure only one legacy controller can be created */
74 if (revmap_type == IRQ_DOMAIN_MAP_LEGACY) {
75 list_for_each_entry(h, &irq_domain_list, link) {
76 if (WARN_ON(h->revmap_type == IRQ_DOMAIN_MAP_LEGACY)) {
77 mutex_unlock(&irq_domain_mutex);
78 of_node_put(host->of_node);
84 list_add(&host->link, &irq_domain_list);
85 mutex_unlock(&irq_domain_mutex);
87 /* Additional setups per revmap type */
89 case IRQ_DOMAIN_MAP_LEGACY:
90 /* 0 is always the invalid number for legacy */
92 /* setup us as the host for all legacy interrupts */
93 for (i = 1; i < NUM_ISA_INTERRUPTS; i++) {
94 struct irq_data *irq_data = irq_get_irq_data(i);
96 irq_data->domain = host;
98 /* Legacy flags are left to default at this point,
99 * one can then use irq_create_mapping() to
100 * explicitly change them
102 ops->map(host, i, i);
104 /* Clear norequest flags */
105 irq_clear_status_flags(i, IRQ_NOREQUEST);
108 case IRQ_DOMAIN_MAP_LINEAR:
109 rmap = (unsigned int *)(host + 1);
110 for (i = 0; i < revmap_arg; i++)
112 host->revmap_data.linear.size = revmap_arg;
113 host->revmap_data.linear.revmap = rmap;
115 case IRQ_DOMAIN_MAP_TREE:
116 INIT_RADIX_TREE(&host->revmap_data.tree, GFP_KERNEL);
122 pr_debug("irq: Allocated host of type %d @0x%p\n", revmap_type, host);
128 * irq_find_host() - Locates a domain for a given device node
129 * @node: device-tree node of the interrupt controller
131 struct irq_domain *irq_find_host(struct device_node *node)
133 struct irq_domain *h, *found = NULL;
135 /* We might want to match the legacy controller last since
136 * it might potentially be set to match all interrupts in
137 * the absence of a device node. This isn't a problem so far
140 mutex_lock(&irq_domain_mutex);
141 list_for_each_entry(h, &irq_domain_list, link)
142 if (h->ops->match(h, node)) {
146 mutex_unlock(&irq_domain_mutex);
149 EXPORT_SYMBOL_GPL(irq_find_host);
152 * irq_set_default_host() - Set a "default" irq domain
153 * @host: default host pointer
155 * For convenience, it's possible to set a "default" domain that will be used
156 * whenever NULL is passed to irq_create_mapping(). It makes life easier for
157 * platforms that want to manipulate a few hard coded interrupt numbers that
158 * aren't properly represented in the device-tree.
160 void irq_set_default_host(struct irq_domain *host)
162 pr_debug("irq: Default host set to @0x%p\n", host);
164 irq_default_host = host;
168 * irq_set_virq_count() - Set the maximum number of linux irqs
169 * @count: number of linux irqs, capped with NR_IRQS
171 * This is mainly for use by platforms like iSeries who want to program
172 * the virtual irq number in the controller to avoid the reverse mapping
174 void irq_set_virq_count(unsigned int count)
176 pr_debug("irq: Trying to set virq count to %d\n", count);
178 BUG_ON(count < NUM_ISA_INTERRUPTS);
180 irq_virq_count = count;
183 static int irq_setup_virq(struct irq_domain *host, unsigned int virq,
184 irq_hw_number_t hwirq)
186 struct irq_data *irq_data = irq_get_irq_data(virq);
188 irq_data->hwirq = hwirq;
189 irq_data->domain = host;
190 if (host->ops->map(host, virq, hwirq)) {
191 pr_debug("irq: -> mapping failed, freeing\n");
192 irq_data->domain = NULL;
197 irq_clear_status_flags(virq, IRQ_NOREQUEST);
203 * irq_create_direct_mapping() - Allocate an irq for direct mapping
204 * @host: domain to allocate the irq for or NULL for default host
206 * This routine is used for irq controllers which can choose the hardware
207 * interrupt numbers they generate. In such a case it's simplest to use
208 * the linux irq as the hardware interrupt number.
210 unsigned int irq_create_direct_mapping(struct irq_domain *host)
215 host = irq_default_host;
217 BUG_ON(host == NULL);
218 WARN_ON(host->revmap_type != IRQ_DOMAIN_MAP_NOMAP);
220 virq = irq_alloc_desc_from(1, 0);
222 pr_debug("irq: create_direct virq allocation failed\n");
225 if (virq >= irq_virq_count) {
226 pr_err("ERROR: no free irqs available below %i maximum\n",
232 pr_debug("irq: create_direct obtained virq %d\n", virq);
234 if (irq_setup_virq(host, virq, virq)) {
243 * irq_create_mapping() - Map a hardware interrupt into linux irq space
244 * @host: host owning this hardware interrupt or NULL for default host
245 * @hwirq: hardware irq number in that host space
247 * Only one mapping per hardware interrupt is permitted. Returns a linux
249 * If the sense/trigger is to be specified, set_irq_type() should be called
250 * on the number returned from that call.
252 unsigned int irq_create_mapping(struct irq_domain *host,
253 irq_hw_number_t hwirq)
255 unsigned int virq, hint;
257 pr_debug("irq: irq_create_mapping(0x%p, 0x%lx)\n", host, hwirq);
259 /* Look for default host if nececssary */
261 host = irq_default_host;
263 printk(KERN_WARNING "irq_create_mapping called for"
264 " NULL host, hwirq=%lx\n", hwirq);
268 pr_debug("irq: -> using host @%p\n", host);
270 /* Check if mapping already exists */
271 virq = irq_find_mapping(host, hwirq);
273 pr_debug("irq: -> existing mapping on virq %d\n", virq);
277 /* Get a virtual interrupt number */
278 if (host->revmap_type == IRQ_DOMAIN_MAP_LEGACY) {
280 virq = (unsigned int)hwirq;
281 if (virq == 0 || virq >= NUM_ISA_INTERRUPTS)
285 /* Allocate a virtual interrupt number */
286 hint = hwirq % irq_virq_count;
289 virq = irq_alloc_desc_from(hint, 0);
291 virq = irq_alloc_desc_from(1, 0);
293 pr_debug("irq: -> virq allocation failed\n");
298 if (irq_setup_virq(host, virq, hwirq)) {
299 if (host->revmap_type != IRQ_DOMAIN_MAP_LEGACY)
304 pr_debug("irq: irq %lu on host %s mapped to virtual irq %u\n",
305 hwirq, host->of_node ? host->of_node->full_name : "null", virq);
309 EXPORT_SYMBOL_GPL(irq_create_mapping);
311 unsigned int irq_create_of_mapping(struct device_node *controller,
312 const u32 *intspec, unsigned int intsize)
314 struct irq_domain *host;
315 irq_hw_number_t hwirq;
316 unsigned int type = IRQ_TYPE_NONE;
319 if (controller == NULL)
320 host = irq_default_host;
322 host = irq_find_host(controller);
324 printk(KERN_WARNING "irq: no irq host found for %s !\n",
325 controller->full_name);
329 /* If host has no translation, then we assume interrupt line */
330 if (host->ops->xlate == NULL)
333 if (host->ops->xlate(host, controller, intspec, intsize,
339 virq = irq_create_mapping(host, hwirq);
343 /* Set type if specified and different than the current one */
344 if (type != IRQ_TYPE_NONE &&
345 type != (irqd_get_trigger_type(irq_get_irq_data(virq))))
346 irq_set_irq_type(virq, type);
349 EXPORT_SYMBOL_GPL(irq_create_of_mapping);
352 * irq_dispose_mapping() - Unmap an interrupt
353 * @virq: linux irq number of the interrupt to unmap
355 void irq_dispose_mapping(unsigned int virq)
357 struct irq_data *irq_data = irq_get_irq_data(virq);
358 struct irq_domain *host;
359 irq_hw_number_t hwirq;
361 if (!virq || !irq_data)
364 host = irq_data->domain;
365 if (WARN_ON(host == NULL))
368 /* Never unmap legacy interrupts */
369 if (host->revmap_type == IRQ_DOMAIN_MAP_LEGACY)
372 irq_set_status_flags(virq, IRQ_NOREQUEST);
374 /* remove chip and handler */
375 irq_set_chip_and_handler(virq, NULL, NULL);
377 /* Make sure it's completed */
378 synchronize_irq(virq);
380 /* Tell the PIC about it */
381 if (host->ops->unmap)
382 host->ops->unmap(host, virq);
385 /* Clear reverse map */
386 hwirq = irq_data->hwirq;
387 switch(host->revmap_type) {
388 case IRQ_DOMAIN_MAP_LINEAR:
389 if (hwirq < host->revmap_data.linear.size)
390 host->revmap_data.linear.revmap[hwirq] = 0;
392 case IRQ_DOMAIN_MAP_TREE:
393 mutex_lock(&revmap_trees_mutex);
394 radix_tree_delete(&host->revmap_data.tree, hwirq);
395 mutex_unlock(&revmap_trees_mutex);
400 irq_data->hwirq = host->inval_irq;
404 EXPORT_SYMBOL_GPL(irq_dispose_mapping);
407 * irq_find_mapping() - Find a linux irq from an hw irq number.
408 * @host: domain owning this hardware interrupt
409 * @hwirq: hardware irq number in that host space
411 * This is a slow path, for use by generic code. It's expected that an
412 * irq controller implementation directly calls the appropriate low level
415 unsigned int irq_find_mapping(struct irq_domain *host,
416 irq_hw_number_t hwirq)
419 unsigned int hint = hwirq % irq_virq_count;
421 /* Look for default host if nececssary */
423 host = irq_default_host;
427 /* legacy -> bail early */
428 if (host->revmap_type == IRQ_DOMAIN_MAP_LEGACY)
431 /* Slow path does a linear search of the map */
436 struct irq_data *data = irq_get_irq_data(i);
437 if (data && (data->domain == host) && (data->hwirq == hwirq))
440 if (i >= irq_virq_count)
445 EXPORT_SYMBOL_GPL(irq_find_mapping);
448 * irq_radix_revmap_lookup() - Find a linux irq from a hw irq number.
449 * @host: host owning this hardware interrupt
450 * @hwirq: hardware irq number in that host space
452 * This is a fast path, for use by irq controller code that uses radix tree
455 unsigned int irq_radix_revmap_lookup(struct irq_domain *host,
456 irq_hw_number_t hwirq)
458 struct irq_data *irq_data;
460 if (WARN_ON_ONCE(host->revmap_type != IRQ_DOMAIN_MAP_TREE))
461 return irq_find_mapping(host, hwirq);
464 * Freeing an irq can delete nodes along the path to
465 * do the lookup via call_rcu.
468 irq_data = radix_tree_lookup(&host->revmap_data.tree, hwirq);
472 * If found in radix tree, then fine.
473 * Else fallback to linear lookup - this should not happen in practice
474 * as it means that we failed to insert the node in the radix tree.
476 return irq_data ? irq_data->irq : irq_find_mapping(host, hwirq);
480 * irq_radix_revmap_insert() - Insert a hw irq to linux irq number mapping.
481 * @host: host owning this hardware interrupt
482 * @virq: linux irq number
483 * @hwirq: hardware irq number in that host space
485 * This is for use by irq controllers that use a radix tree reverse
486 * mapping for fast lookup.
488 void irq_radix_revmap_insert(struct irq_domain *host, unsigned int virq,
489 irq_hw_number_t hwirq)
491 struct irq_data *irq_data = irq_get_irq_data(virq);
493 if (WARN_ON(host->revmap_type != IRQ_DOMAIN_MAP_TREE))
497 mutex_lock(&revmap_trees_mutex);
498 radix_tree_insert(&host->revmap_data.tree, hwirq, irq_data);
499 mutex_unlock(&revmap_trees_mutex);
504 * irq_linear_revmap() - Find a linux irq from a hw irq number.
505 * @host: host owning this hardware interrupt
506 * @hwirq: hardware irq number in that host space
508 * This is a fast path, for use by irq controller code that uses linear
509 * revmaps. It does fallback to the slow path if the revmap doesn't exist
510 * yet and will create the revmap entry with appropriate locking
512 unsigned int irq_linear_revmap(struct irq_domain *host,
513 irq_hw_number_t hwirq)
515 unsigned int *revmap;
517 if (WARN_ON_ONCE(host->revmap_type != IRQ_DOMAIN_MAP_LINEAR))
518 return irq_find_mapping(host, hwirq);
520 /* Check revmap bounds */
521 if (unlikely(hwirq >= host->revmap_data.linear.size))
522 return irq_find_mapping(host, hwirq);
524 /* Check if revmap was allocated */
525 revmap = host->revmap_data.linear.revmap;
526 if (unlikely(revmap == NULL))
527 return irq_find_mapping(host, hwirq);
529 /* Fill up revmap with slow path if no mapping found */
530 if (unlikely(!revmap[hwirq]))
531 revmap[hwirq] = irq_find_mapping(host, hwirq);
533 return revmap[hwirq];
536 #ifdef CONFIG_VIRQ_DEBUG
537 static int virq_debug_show(struct seq_file *m, void *private)
540 struct irq_desc *desc;
542 static const char none[] = "none";
546 seq_printf(m, "%-5s %-7s %-15s %-18s %s\n", "virq", "hwirq",
547 "chip name", "chip data", "host name");
549 for (i = 1; i < nr_irqs; i++) {
550 desc = irq_to_desc(i);
554 raw_spin_lock_irqsave(&desc->lock, flags);
556 if (desc->action && desc->action->handler) {
557 struct irq_chip *chip;
559 seq_printf(m, "%5d ", i);
560 seq_printf(m, "0x%05lx ", desc->irq_data.hwirq);
562 chip = irq_desc_get_chip(desc);
563 if (chip && chip->name)
567 seq_printf(m, "%-15s ", p);
569 data = irq_desc_get_chip_data(desc);
570 seq_printf(m, "0x%16p ", data);
572 if (desc->irq_data.domain->of_node)
573 p = desc->irq_data.domain->of_node->full_name;
576 seq_printf(m, "%s\n", p);
579 raw_spin_unlock_irqrestore(&desc->lock, flags);
585 static int virq_debug_open(struct inode *inode, struct file *file)
587 return single_open(file, virq_debug_show, inode->i_private);
590 static const struct file_operations virq_debug_fops = {
591 .open = virq_debug_open,
594 .release = single_release,
597 static int __init irq_debugfs_init(void)
599 if (debugfs_create_file("virq_mapping", S_IRUGO, powerpc_debugfs_root,
600 NULL, &virq_debug_fops) == NULL)
605 __initcall(irq_debugfs_init);
606 #endif /* CONFIG_VIRQ_DEBUG */
608 #else /* CONFIG_PPC */
611 * irq_domain_add() - Register an irq_domain
612 * @domain: ptr to initialized irq_domain structure
614 * Registers an irq_domain structure. The irq_domain must at a minimum be
615 * initialized with an ops structure pointer, and either a ->to_irq hook or
616 * a valid irq_base value. Everything else is optional.
618 void irq_domain_add(struct irq_domain *domain)
624 * This assumes that the irq_domain owner has already allocated
625 * the irq_descs. This block will be removed when support for dynamic
626 * allocation of irq_descs is added to irq_domain.
628 irq_domain_for_each_irq(domain, hwirq, irq) {
629 d = irq_get_irq_data(irq);
631 WARN(1, "error: assigning domain to non existant irq_desc");
635 /* things are broken; just report, don't clean up */
636 WARN(1, "error: irq_desc already assigned to a domain");
643 mutex_lock(&irq_domain_mutex);
644 list_add(&domain->link, &irq_domain_list);
645 mutex_unlock(&irq_domain_mutex);
649 * irq_domain_del() - Unregister an irq_domain
650 * @domain: ptr to registered irq_domain.
652 void irq_domain_del(struct irq_domain *domain)
657 mutex_lock(&irq_domain_mutex);
658 list_del(&domain->link);
659 mutex_unlock(&irq_domain_mutex);
661 /* Clear the irq_domain assignments */
662 irq_domain_for_each_irq(domain, hwirq, irq) {
663 d = irq_get_irq_data(irq);
668 #if defined(CONFIG_OF_IRQ)
670 * irq_create_of_mapping() - Map a linux irq number from a DT interrupt spec
672 * Used by the device tree interrupt mapping code to translate a device tree
673 * interrupt specifier to a valid linux irq number. Returns either a valid
674 * linux IRQ number or 0.
676 * When the caller no longer need the irq number returned by this function it
677 * should arrange to call irq_dispose_mapping().
679 unsigned int irq_create_of_mapping(struct device_node *controller,
680 const u32 *intspec, unsigned int intsize)
682 struct irq_domain *domain;
684 unsigned int irq, type;
687 /* Find a domain which can translate the irq spec */
688 mutex_lock(&irq_domain_mutex);
689 list_for_each_entry(domain, &irq_domain_list, link) {
690 if (!domain->ops->xlate)
692 rc = domain->ops->xlate(domain, controller,
693 intspec, intsize, &hwirq, &type);
697 mutex_unlock(&irq_domain_mutex);
702 irq = irq_domain_to_irq(domain, hwirq);
703 if (type != IRQ_TYPE_NONE)
704 irq_set_irq_type(irq, type);
705 pr_debug("%s: mapped hwirq=%i to irq=%i, flags=%x\n",
706 controller->full_name, (int)hwirq, irq, type);
709 EXPORT_SYMBOL_GPL(irq_create_of_mapping);
712 * irq_dispose_mapping() - Discard a mapping created by irq_create_of_mapping()
713 * @irq: linux irq number to be discarded
715 * Calling this function indicates the caller no longer needs a reference to
716 * the linux irq number returned by a prior call to irq_create_of_mapping().
718 void irq_dispose_mapping(unsigned int irq)
721 * nothing yet; will be filled when support for dynamic allocation of
722 * irq_descs is added to irq_domain
725 EXPORT_SYMBOL_GPL(irq_dispose_mapping);
727 int irq_domain_simple_xlate(struct irq_domain *d,
728 struct device_node *controller,
729 const u32 *intspec, unsigned int intsize,
730 unsigned long *out_hwirq, unsigned int *out_type)
732 if (d->of_node != controller)
736 if (d->nr_irq && ((intspec[0] < d->hwirq_base) ||
737 (intspec[0] >= d->hwirq_base + d->nr_irq)))
740 *out_hwirq = intspec[0];
741 *out_type = IRQ_TYPE_NONE;
743 *out_type = intspec[1] & IRQ_TYPE_SENSE_MASK;
748 * irq_domain_create_simple() - Set up a 'simple' translation range
750 void irq_domain_add_simple(struct device_node *controller, int irq_base)
752 struct irq_domain *domain;
754 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
760 domain->irq_base = irq_base;
761 domain->of_node = of_node_get(controller);
762 domain->ops = &irq_domain_simple_ops;
763 irq_domain_add(domain);
765 EXPORT_SYMBOL_GPL(irq_domain_add_simple);
767 void irq_domain_generate_simple(const struct of_device_id *match,
768 u64 phys_base, unsigned int irq_start)
770 struct device_node *node;
771 pr_debug("looking for phys_base=%llx, irq_start=%i\n",
772 (unsigned long long) phys_base, (int) irq_start);
773 node = of_find_matching_node_by_address(NULL, match, phys_base);
775 irq_domain_add_simple(node, irq_start);
777 EXPORT_SYMBOL_GPL(irq_domain_generate_simple);
778 #endif /* CONFIG_OF_IRQ */
780 struct irq_domain_ops irq_domain_simple_ops = {
782 .xlate = irq_domain_simple_xlate,
783 #endif /* CONFIG_OF_IRQ */
785 EXPORT_SYMBOL_GPL(irq_domain_simple_ops);
787 #endif /* !CONFIG_PPC */