]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - kernel/irq/irqdomain.c
irq_domain: correct the debugfs file name
[mirror_ubuntu-zesty-kernel.git] / kernel / irq / irqdomain.c
CommitLineData
cc79ca69
GL
1#include <linux/debugfs.h>
2#include <linux/hardirq.h>
3#include <linux/interrupt.h>
08a543ad 4#include <linux/irq.h>
cc79ca69 5#include <linux/irqdesc.h>
08a543ad
GL
6#include <linux/irqdomain.h>
7#include <linux/module.h>
8#include <linux/mutex.h>
9#include <linux/of.h>
7e713301 10#include <linux/of_address.h>
cc79ca69 11#include <linux/seq_file.h>
7e713301 12#include <linux/slab.h>
cc79ca69
GL
13#include <linux/smp.h>
14#include <linux/fs.h>
08a543ad 15
1bc04f2c
GL
16#define IRQ_DOMAIN_MAP_LEGACY 0 /* driver allocated fixed range of irqs.
17 * ie. legacy 8259, gets irqs 1..15 */
a8db8cf0
GL
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 */
21
08a543ad
GL
22static LIST_HEAD(irq_domain_list);
23static DEFINE_MUTEX(irq_domain_mutex);
24
cc79ca69
GL
25static DEFINE_MUTEX(revmap_trees_mutex);
26static unsigned int irq_virq_count = NR_IRQS;
68700650 27static struct irq_domain *irq_default_domain;
cc79ca69 28
cc79ca69 29/**
a8db8cf0 30 * irq_domain_alloc() - Allocate a new irq_domain data structure
cc79ca69
GL
31 * @of_node: optional device-tree node of the interrupt controller
32 * @revmap_type: type of reverse mapping to use
68700650 33 * @ops: map/unmap domain callbacks
a8db8cf0 34 * @host_data: Controller private data pointer
cc79ca69 35 *
a8db8cf0
GL
36 * Allocates and initialize and irq_domain structure. Caller is expected to
37 * register allocated irq_domain with irq_domain_register(). Returns pointer
38 * to IRQ domain, or NULL on failure.
cc79ca69 39 */
a8db8cf0
GL
40static struct irq_domain *irq_domain_alloc(struct device_node *of_node,
41 unsigned int revmap_type,
a18dc81b 42 const struct irq_domain_ops *ops,
a8db8cf0 43 void *host_data)
cc79ca69 44{
a8db8cf0 45 struct irq_domain *domain;
cc79ca69 46
a8db8cf0
GL
47 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
48 if (WARN_ON(!domain))
cc79ca69
GL
49 return NULL;
50
51 /* Fill structure */
68700650 52 domain->revmap_type = revmap_type;
68700650 53 domain->ops = ops;
a8db8cf0 54 domain->host_data = host_data;
68700650 55 domain->of_node = of_node_get(of_node);
cc79ca69 56
a8db8cf0
GL
57 return domain;
58}
59
60static void irq_domain_add(struct irq_domain *domain)
61{
62 mutex_lock(&irq_domain_mutex);
63 list_add(&domain->link, &irq_domain_list);
64 mutex_unlock(&irq_domain_mutex);
65 pr_debug("irq: Allocated domain of type %d @0x%p\n",
66 domain->revmap_type, domain);
67}
68
1bc04f2c
GL
69static unsigned int irq_domain_legacy_revmap(struct irq_domain *domain,
70 irq_hw_number_t hwirq)
71{
72 irq_hw_number_t first_hwirq = domain->revmap_data.legacy.first_hwirq;
73 int size = domain->revmap_data.legacy.size;
74
75 if (WARN_ON(hwirq < first_hwirq || hwirq >= first_hwirq + size))
76 return 0;
77 return hwirq - first_hwirq + domain->revmap_data.legacy.first_irq;
78}
79
a8db8cf0
GL
80/**
81 * irq_domain_add_legacy() - Allocate and register a legacy revmap irq_domain.
82 * @of_node: pointer to interrupt controller's device tree node.
1bc04f2c
GL
83 * @size: total number of irqs in legacy mapping
84 * @first_irq: first number of irq block assigned to the domain
85 * @first_hwirq: first hwirq number to use for the translation. Should normally
86 * be '0', but a positive integer can be used if the effective
87 * hwirqs numbering does not begin at zero.
a8db8cf0
GL
88 * @ops: map/unmap domain callbacks
89 * @host_data: Controller private data pointer
90 *
91 * Note: the map() callback will be called before this function returns
92 * for all legacy interrupts except 0 (which is always the invalid irq for
93 * a legacy controller).
94 */
95struct irq_domain *irq_domain_add_legacy(struct device_node *of_node,
1bc04f2c
GL
96 unsigned int size,
97 unsigned int first_irq,
98 irq_hw_number_t first_hwirq,
a18dc81b 99 const struct irq_domain_ops *ops,
a8db8cf0
GL
100 void *host_data)
101{
1bc04f2c 102 struct irq_domain *domain;
a8db8cf0
GL
103 unsigned int i;
104
105 domain = irq_domain_alloc(of_node, IRQ_DOMAIN_MAP_LEGACY, ops, host_data);
106 if (!domain)
107 return NULL;
108
1bc04f2c
GL
109 domain->revmap_data.legacy.first_irq = first_irq;
110 domain->revmap_data.legacy.first_hwirq = first_hwirq;
111 domain->revmap_data.legacy.size = size;
112
cc79ca69 113 mutex_lock(&irq_domain_mutex);
1bc04f2c
GL
114 /* Verify that all the irqs are available */
115 for (i = 0; i < size; i++) {
116 int irq = first_irq + i;
117 struct irq_data *irq_data = irq_get_irq_data(irq);
118
119 if (WARN_ON(!irq_data || irq_data->domain)) {
a8db8cf0
GL
120 mutex_unlock(&irq_domain_mutex);
121 of_node_put(domain->of_node);
122 kfree(domain);
123 return NULL;
cc79ca69
GL
124 }
125 }
cc79ca69 126
1bc04f2c
GL
127 /* Claim all of the irqs before registering a legacy domain */
128 for (i = 0; i < size; i++) {
129 struct irq_data *irq_data = irq_get_irq_data(first_irq + i);
130 irq_data->hwirq = first_hwirq + i;
a8db8cf0 131 irq_data->domain = domain;
1bc04f2c
GL
132 }
133 mutex_unlock(&irq_domain_mutex);
134
135 for (i = 0; i < size; i++) {
136 int irq = first_irq + i;
137 int hwirq = first_hwirq + i;
138
139 /* IRQ0 gets ignored */
140 if (!irq)
141 continue;
a8db8cf0
GL
142
143 /* Legacy flags are left to default at this point,
144 * one can then use irq_create_mapping() to
145 * explicitly change them
146 */
1bc04f2c 147 ops->map(domain, irq, hwirq);
a8db8cf0
GL
148
149 /* Clear norequest flags */
1bc04f2c 150 irq_clear_status_flags(irq, IRQ_NOREQUEST);
cc79ca69 151 }
1bc04f2c
GL
152
153 irq_domain_add(domain);
a8db8cf0
GL
154 return domain;
155}
156
157/**
158 * irq_domain_add_linear() - Allocate and register a legacy revmap irq_domain.
159 * @of_node: pointer to interrupt controller's device tree node.
160 * @ops: map/unmap domain callbacks
161 * @host_data: Controller private data pointer
162 */
163struct irq_domain *irq_domain_add_linear(struct device_node *of_node,
164 unsigned int size,
a18dc81b 165 const struct irq_domain_ops *ops,
a8db8cf0
GL
166 void *host_data)
167{
168 struct irq_domain *domain;
169 unsigned int *revmap;
cc79ca69 170
a8db8cf0
GL
171 revmap = kzalloc(sizeof(*revmap) * size, GFP_KERNEL);
172 if (WARN_ON(!revmap))
173 return NULL;
cc79ca69 174
a8db8cf0
GL
175 domain = irq_domain_alloc(of_node, IRQ_DOMAIN_MAP_LINEAR, ops, host_data);
176 if (!domain) {
177 kfree(revmap);
178 return NULL;
179 }
180 domain->revmap_data.linear.size = size;
181 domain->revmap_data.linear.revmap = revmap;
182 irq_domain_add(domain);
183 return domain;
184}
185
186struct irq_domain *irq_domain_add_nomap(struct device_node *of_node,
a18dc81b 187 const struct irq_domain_ops *ops,
a8db8cf0
GL
188 void *host_data)
189{
190 struct irq_domain *domain = irq_domain_alloc(of_node,
191 IRQ_DOMAIN_MAP_NOMAP, ops, host_data);
192 if (domain)
193 irq_domain_add(domain);
194 return domain;
195}
196
197/**
198 * irq_domain_add_tree()
199 * @of_node: pointer to interrupt controller's device tree node.
200 * @ops: map/unmap domain callbacks
201 *
202 * Note: The radix tree will be allocated later during boot automatically
203 * (the reverse mapping will use the slow path until that happens).
204 */
205struct irq_domain *irq_domain_add_tree(struct device_node *of_node,
a18dc81b 206 const struct irq_domain_ops *ops,
a8db8cf0
GL
207 void *host_data)
208{
209 struct irq_domain *domain = irq_domain_alloc(of_node,
210 IRQ_DOMAIN_MAP_TREE, ops, host_data);
211 if (domain) {
212 INIT_RADIX_TREE(&domain->revmap_data.tree, GFP_KERNEL);
213 irq_domain_add(domain);
214 }
68700650 215 return domain;
cc79ca69
GL
216}
217
218/**
219 * irq_find_host() - Locates a domain for a given device node
220 * @node: device-tree node of the interrupt controller
221 */
222struct irq_domain *irq_find_host(struct device_node *node)
223{
224 struct irq_domain *h, *found = NULL;
a18dc81b 225 int rc;
cc79ca69
GL
226
227 /* We might want to match the legacy controller last since
228 * it might potentially be set to match all interrupts in
229 * the absence of a device node. This isn't a problem so far
230 * yet though...
231 */
232 mutex_lock(&irq_domain_mutex);
a18dc81b
GL
233 list_for_each_entry(h, &irq_domain_list, link) {
234 if (h->ops->match)
235 rc = h->ops->match(h, node);
236 else
237 rc = (h->of_node != NULL) && (h->of_node == node);
238
239 if (rc) {
cc79ca69
GL
240 found = h;
241 break;
242 }
a18dc81b 243 }
cc79ca69
GL
244 mutex_unlock(&irq_domain_mutex);
245 return found;
246}
247EXPORT_SYMBOL_GPL(irq_find_host);
248
249/**
250 * irq_set_default_host() - Set a "default" irq domain
68700650 251 * @domain: default domain pointer
cc79ca69
GL
252 *
253 * For convenience, it's possible to set a "default" domain that will be used
254 * whenever NULL is passed to irq_create_mapping(). It makes life easier for
255 * platforms that want to manipulate a few hard coded interrupt numbers that
256 * aren't properly represented in the device-tree.
257 */
68700650 258void irq_set_default_host(struct irq_domain *domain)
cc79ca69 259{
68700650 260 pr_debug("irq: Default domain set to @0x%p\n", domain);
cc79ca69 261
68700650 262 irq_default_domain = domain;
cc79ca69
GL
263}
264
265/**
266 * irq_set_virq_count() - Set the maximum number of linux irqs
267 * @count: number of linux irqs, capped with NR_IRQS
268 *
269 * This is mainly for use by platforms like iSeries who want to program
270 * the virtual irq number in the controller to avoid the reverse mapping
271 */
272void irq_set_virq_count(unsigned int count)
273{
274 pr_debug("irq: Trying to set virq count to %d\n", count);
275
276 BUG_ON(count < NUM_ISA_INTERRUPTS);
277 if (count < NR_IRQS)
278 irq_virq_count = count;
279}
280
68700650 281static int irq_setup_virq(struct irq_domain *domain, unsigned int virq,
cc79ca69
GL
282 irq_hw_number_t hwirq)
283{
284 struct irq_data *irq_data = irq_get_irq_data(virq);
285
286 irq_data->hwirq = hwirq;
68700650
GL
287 irq_data->domain = domain;
288 if (domain->ops->map(domain, virq, hwirq)) {
cc79ca69
GL
289 pr_debug("irq: -> mapping failed, freeing\n");
290 irq_data->domain = NULL;
291 irq_data->hwirq = 0;
292 return -1;
293 }
294
295 irq_clear_status_flags(virq, IRQ_NOREQUEST);
296
297 return 0;
298}
299
300/**
301 * irq_create_direct_mapping() - Allocate an irq for direct mapping
68700650 302 * @domain: domain to allocate the irq for or NULL for default domain
cc79ca69
GL
303 *
304 * This routine is used for irq controllers which can choose the hardware
305 * interrupt numbers they generate. In such a case it's simplest to use
306 * the linux irq as the hardware interrupt number.
307 */
68700650 308unsigned int irq_create_direct_mapping(struct irq_domain *domain)
cc79ca69
GL
309{
310 unsigned int virq;
311
68700650
GL
312 if (domain == NULL)
313 domain = irq_default_domain;
cc79ca69 314
68700650
GL
315 BUG_ON(domain == NULL);
316 WARN_ON(domain->revmap_type != IRQ_DOMAIN_MAP_NOMAP);
cc79ca69
GL
317
318 virq = irq_alloc_desc_from(1, 0);
03848373 319 if (!virq) {
cc79ca69 320 pr_debug("irq: create_direct virq allocation failed\n");
03848373 321 return 0;
cc79ca69
GL
322 }
323 if (virq >= irq_virq_count) {
324 pr_err("ERROR: no free irqs available below %i maximum\n",
325 irq_virq_count);
326 irq_free_desc(virq);
327 return 0;
328 }
329
330 pr_debug("irq: create_direct obtained virq %d\n", virq);
331
68700650 332 if (irq_setup_virq(domain, virq, virq)) {
cc79ca69 333 irq_free_desc(virq);
03848373 334 return 0;
cc79ca69
GL
335 }
336
337 return virq;
338}
339
340/**
341 * irq_create_mapping() - Map a hardware interrupt into linux irq space
68700650
GL
342 * @domain: domain owning this hardware interrupt or NULL for default domain
343 * @hwirq: hardware irq number in that domain space
cc79ca69
GL
344 *
345 * Only one mapping per hardware interrupt is permitted. Returns a linux
346 * irq number.
347 * If the sense/trigger is to be specified, set_irq_type() should be called
348 * on the number returned from that call.
349 */
68700650 350unsigned int irq_create_mapping(struct irq_domain *domain,
cc79ca69
GL
351 irq_hw_number_t hwirq)
352{
5b7526e3
DD
353 unsigned int hint;
354 int virq;
cc79ca69 355
68700650 356 pr_debug("irq: irq_create_mapping(0x%p, 0x%lx)\n", domain, hwirq);
cc79ca69 357
68700650
GL
358 /* Look for default domain if nececssary */
359 if (domain == NULL)
360 domain = irq_default_domain;
361 if (domain == NULL) {
cc79ca69 362 printk(KERN_WARNING "irq_create_mapping called for"
68700650 363 " NULL domain, hwirq=%lx\n", hwirq);
cc79ca69 364 WARN_ON(1);
03848373 365 return 0;
cc79ca69 366 }
68700650 367 pr_debug("irq: -> using domain @%p\n", domain);
cc79ca69
GL
368
369 /* Check if mapping already exists */
68700650 370 virq = irq_find_mapping(domain, hwirq);
03848373 371 if (virq) {
cc79ca69
GL
372 pr_debug("irq: -> existing mapping on virq %d\n", virq);
373 return virq;
374 }
375
376 /* Get a virtual interrupt number */
1bc04f2c
GL
377 if (domain->revmap_type == IRQ_DOMAIN_MAP_LEGACY)
378 return irq_domain_legacy_revmap(domain, hwirq);
379
380 /* Allocate a virtual interrupt number */
381 hint = hwirq % irq_virq_count;
382 if (hint == 0)
383 hint++;
384 virq = irq_alloc_desc_from(hint, 0);
5b7526e3 385 if (virq <= 0)
1bc04f2c 386 virq = irq_alloc_desc_from(1, 0);
5b7526e3 387 if (virq <= 0) {
1bc04f2c
GL
388 pr_debug("irq: -> virq allocation failed\n");
389 return 0;
cc79ca69
GL
390 }
391
68700650
GL
392 if (irq_setup_virq(domain, virq, hwirq)) {
393 if (domain->revmap_type != IRQ_DOMAIN_MAP_LEGACY)
cc79ca69 394 irq_free_desc(virq);
03848373 395 return 0;
cc79ca69
GL
396 }
397
68700650
GL
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);
cc79ca69
GL
400
401 return virq;
402}
403EXPORT_SYMBOL_GPL(irq_create_mapping);
404
405unsigned int irq_create_of_mapping(struct device_node *controller,
406 const u32 *intspec, unsigned int intsize)
407{
68700650 408 struct irq_domain *domain;
cc79ca69
GL
409 irq_hw_number_t hwirq;
410 unsigned int type = IRQ_TYPE_NONE;
411 unsigned int virq;
412
68700650
GL
413 domain = controller ? irq_find_host(controller) : irq_default_domain;
414 if (!domain) {
abd2363f
GL
415#ifdef CONFIG_MIPS
416 /*
417 * Workaround to avoid breaking interrupt controller drivers
418 * that don't yet register an irq_domain. This is temporary
419 * code. ~~~gcl, Feb 24, 2012
420 *
421 * Scheduled for removal in Linux v3.6. That should be enough
422 * time.
423 */
424 if (intsize > 0)
425 return intspec[0];
426#endif
68700650 427 printk(KERN_WARNING "irq: no irq domain found for %s !\n",
cc79ca69 428 controller->full_name);
03848373 429 return 0;
cc79ca69
GL
430 }
431
68700650
GL
432 /* If domain has no translation, then we assume interrupt line */
433 if (domain->ops->xlate == NULL)
cc79ca69
GL
434 hwirq = intspec[0];
435 else {
68700650 436 if (domain->ops->xlate(domain, controller, intspec, intsize,
cc79ca69 437 &hwirq, &type))
03848373 438 return 0;
cc79ca69
GL
439 }
440
441 /* Create mapping */
68700650 442 virq = irq_create_mapping(domain, hwirq);
03848373 443 if (!virq)
cc79ca69
GL
444 return virq;
445
446 /* Set type if specified and different than the current one */
447 if (type != IRQ_TYPE_NONE &&
448 type != (irqd_get_trigger_type(irq_get_irq_data(virq))))
449 irq_set_irq_type(virq, type);
450 return virq;
451}
452EXPORT_SYMBOL_GPL(irq_create_of_mapping);
453
454/**
455 * irq_dispose_mapping() - Unmap an interrupt
456 * @virq: linux irq number of the interrupt to unmap
457 */
458void irq_dispose_mapping(unsigned int virq)
459{
460 struct irq_data *irq_data = irq_get_irq_data(virq);
68700650 461 struct irq_domain *domain;
cc79ca69
GL
462 irq_hw_number_t hwirq;
463
03848373 464 if (!virq || !irq_data)
cc79ca69
GL
465 return;
466
68700650
GL
467 domain = irq_data->domain;
468 if (WARN_ON(domain == NULL))
cc79ca69
GL
469 return;
470
471 /* Never unmap legacy interrupts */
68700650 472 if (domain->revmap_type == IRQ_DOMAIN_MAP_LEGACY)
cc79ca69
GL
473 return;
474
475 irq_set_status_flags(virq, IRQ_NOREQUEST);
476
477 /* remove chip and handler */
478 irq_set_chip_and_handler(virq, NULL, NULL);
479
480 /* Make sure it's completed */
481 synchronize_irq(virq);
482
483 /* Tell the PIC about it */
68700650
GL
484 if (domain->ops->unmap)
485 domain->ops->unmap(domain, virq);
cc79ca69
GL
486 smp_mb();
487
488 /* Clear reverse map */
489 hwirq = irq_data->hwirq;
68700650 490 switch(domain->revmap_type) {
cc79ca69 491 case IRQ_DOMAIN_MAP_LINEAR:
68700650
GL
492 if (hwirq < domain->revmap_data.linear.size)
493 domain->revmap_data.linear.revmap[hwirq] = 0;
cc79ca69
GL
494 break;
495 case IRQ_DOMAIN_MAP_TREE:
496 mutex_lock(&revmap_trees_mutex);
68700650 497 radix_tree_delete(&domain->revmap_data.tree, hwirq);
cc79ca69
GL
498 mutex_unlock(&revmap_trees_mutex);
499 break;
500 }
501
cc79ca69
GL
502 irq_free_desc(virq);
503}
504EXPORT_SYMBOL_GPL(irq_dispose_mapping);
505
506/**
507 * irq_find_mapping() - Find a linux irq from an hw irq number.
68700650
GL
508 * @domain: domain owning this hardware interrupt
509 * @hwirq: hardware irq number in that domain space
cc79ca69
GL
510 *
511 * This is a slow path, for use by generic code. It's expected that an
512 * irq controller implementation directly calls the appropriate low level
513 * mapping function.
514 */
68700650 515unsigned int irq_find_mapping(struct irq_domain *domain,
cc79ca69
GL
516 irq_hw_number_t hwirq)
517{
518 unsigned int i;
519 unsigned int hint = hwirq % irq_virq_count;
520
68700650
GL
521 /* Look for default domain if nececssary */
522 if (domain == NULL)
523 domain = irq_default_domain;
524 if (domain == NULL)
03848373 525 return 0;
cc79ca69
GL
526
527 /* legacy -> bail early */
68700650 528 if (domain->revmap_type == IRQ_DOMAIN_MAP_LEGACY)
1bc04f2c 529 return irq_domain_legacy_revmap(domain, hwirq);
cc79ca69
GL
530
531 /* Slow path does a linear search of the map */
532 if (hint == 0)
533 hint = 1;
534 i = hint;
535 do {
536 struct irq_data *data = irq_get_irq_data(i);
68700650 537 if (data && (data->domain == domain) && (data->hwirq == hwirq))
cc79ca69
GL
538 return i;
539 i++;
540 if (i >= irq_virq_count)
541 i = 1;
542 } while(i != hint);
03848373 543 return 0;
cc79ca69
GL
544}
545EXPORT_SYMBOL_GPL(irq_find_mapping);
546
547/**
548 * irq_radix_revmap_lookup() - Find a linux irq from a hw irq number.
68700650
GL
549 * @domain: domain owning this hardware interrupt
550 * @hwirq: hardware irq number in that domain space
cc79ca69
GL
551 *
552 * This is a fast path, for use by irq controller code that uses radix tree
553 * revmaps
554 */
68700650 555unsigned int irq_radix_revmap_lookup(struct irq_domain *domain,
cc79ca69
GL
556 irq_hw_number_t hwirq)
557{
558 struct irq_data *irq_data;
559
68700650
GL
560 if (WARN_ON_ONCE(domain->revmap_type != IRQ_DOMAIN_MAP_TREE))
561 return irq_find_mapping(domain, hwirq);
cc79ca69
GL
562
563 /*
564 * Freeing an irq can delete nodes along the path to
565 * do the lookup via call_rcu.
566 */
567 rcu_read_lock();
68700650 568 irq_data = radix_tree_lookup(&domain->revmap_data.tree, hwirq);
cc79ca69
GL
569 rcu_read_unlock();
570
571 /*
572 * If found in radix tree, then fine.
573 * Else fallback to linear lookup - this should not happen in practice
574 * as it means that we failed to insert the node in the radix tree.
575 */
68700650 576 return irq_data ? irq_data->irq : irq_find_mapping(domain, hwirq);
cc79ca69
GL
577}
578
579/**
580 * irq_radix_revmap_insert() - Insert a hw irq to linux irq number mapping.
68700650 581 * @domain: domain owning this hardware interrupt
cc79ca69 582 * @virq: linux irq number
68700650 583 * @hwirq: hardware irq number in that domain space
cc79ca69
GL
584 *
585 * This is for use by irq controllers that use a radix tree reverse
586 * mapping for fast lookup.
587 */
68700650 588void irq_radix_revmap_insert(struct irq_domain *domain, unsigned int virq,
cc79ca69
GL
589 irq_hw_number_t hwirq)
590{
591 struct irq_data *irq_data = irq_get_irq_data(virq);
592
68700650 593 if (WARN_ON(domain->revmap_type != IRQ_DOMAIN_MAP_TREE))
cc79ca69
GL
594 return;
595
03848373 596 if (virq) {
cc79ca69 597 mutex_lock(&revmap_trees_mutex);
68700650 598 radix_tree_insert(&domain->revmap_data.tree, hwirq, irq_data);
cc79ca69
GL
599 mutex_unlock(&revmap_trees_mutex);
600 }
601}
602
603/**
604 * irq_linear_revmap() - Find a linux irq from a hw irq number.
68700650
GL
605 * @domain: domain owning this hardware interrupt
606 * @hwirq: hardware irq number in that domain space
cc79ca69
GL
607 *
608 * This is a fast path, for use by irq controller code that uses linear
609 * revmaps. It does fallback to the slow path if the revmap doesn't exist
610 * yet and will create the revmap entry with appropriate locking
611 */
68700650 612unsigned int irq_linear_revmap(struct irq_domain *domain,
cc79ca69
GL
613 irq_hw_number_t hwirq)
614{
615 unsigned int *revmap;
616
68700650
GL
617 if (WARN_ON_ONCE(domain->revmap_type != IRQ_DOMAIN_MAP_LINEAR))
618 return irq_find_mapping(domain, hwirq);
cc79ca69
GL
619
620 /* Check revmap bounds */
68700650
GL
621 if (unlikely(hwirq >= domain->revmap_data.linear.size))
622 return irq_find_mapping(domain, hwirq);
cc79ca69
GL
623
624 /* Check if revmap was allocated */
68700650 625 revmap = domain->revmap_data.linear.revmap;
cc79ca69 626 if (unlikely(revmap == NULL))
68700650 627 return irq_find_mapping(domain, hwirq);
cc79ca69
GL
628
629 /* Fill up revmap with slow path if no mapping found */
03848373 630 if (unlikely(!revmap[hwirq]))
68700650 631 revmap[hwirq] = irq_find_mapping(domain, hwirq);
cc79ca69
GL
632
633 return revmap[hwirq];
634}
635
092b2fb0 636#ifdef CONFIG_IRQ_DOMAIN_DEBUG
cc79ca69
GL
637static int virq_debug_show(struct seq_file *m, void *private)
638{
639 unsigned long flags;
640 struct irq_desc *desc;
641 const char *p;
642 static const char none[] = "none";
643 void *data;
644 int i;
645
646 seq_printf(m, "%-5s %-7s %-15s %-18s %s\n", "virq", "hwirq",
68700650 647 "chip name", "chip data", "domain name");
cc79ca69
GL
648
649 for (i = 1; i < nr_irqs; i++) {
650 desc = irq_to_desc(i);
651 if (!desc)
652 continue;
653
654 raw_spin_lock_irqsave(&desc->lock, flags);
655
656 if (desc->action && desc->action->handler) {
657 struct irq_chip *chip;
658
659 seq_printf(m, "%5d ", i);
660 seq_printf(m, "0x%05lx ", desc->irq_data.hwirq);
661
662 chip = irq_desc_get_chip(desc);
663 if (chip && chip->name)
664 p = chip->name;
665 else
666 p = none;
667 seq_printf(m, "%-15s ", p);
668
669 data = irq_desc_get_chip_data(desc);
670 seq_printf(m, "0x%16p ", data);
671
092b2fb0 672 if (desc->irq_data.domain && desc->irq_data.domain->of_node)
cc79ca69
GL
673 p = desc->irq_data.domain->of_node->full_name;
674 else
675 p = none;
676 seq_printf(m, "%s\n", p);
677 }
678
679 raw_spin_unlock_irqrestore(&desc->lock, flags);
680 }
681
682 return 0;
683}
684
685static int virq_debug_open(struct inode *inode, struct file *file)
686{
687 return single_open(file, virq_debug_show, inode->i_private);
688}
689
690static const struct file_operations virq_debug_fops = {
691 .open = virq_debug_open,
692 .read = seq_read,
693 .llseek = seq_lseek,
694 .release = single_release,
695};
696
697static int __init irq_debugfs_init(void)
698{
092b2fb0 699 if (debugfs_create_file("irq_domain_mapping", S_IRUGO, NULL,
cc79ca69
GL
700 NULL, &virq_debug_fops) == NULL)
701 return -ENOMEM;
702
703 return 0;
704}
705__initcall(irq_debugfs_init);
092b2fb0 706#endif /* CONFIG_IRQ_DOMAIN_DEBUG */
cc79ca69 707
75294957
GL
708int irq_domain_simple_map(struct irq_domain *d, unsigned int irq,
709 irq_hw_number_t hwirq)
08a543ad 710{
75294957 711 return 0;
08a543ad 712}
7e713301 713
16b2e6e2
GL
714/**
715 * irq_domain_xlate_onecell() - Generic xlate for direct one cell bindings
716 *
717 * Device Tree IRQ specifier translation function which works with one cell
718 * bindings where the cell value maps directly to the hwirq number.
719 */
720int irq_domain_xlate_onecell(struct irq_domain *d, struct device_node *ctrlr,
721 const u32 *intspec, unsigned int intsize,
722 unsigned long *out_hwirq, unsigned int *out_type)
7e713301 723{
16b2e6e2 724 if (WARN_ON(intsize < 1))
7e713301 725 return -EINVAL;
7e713301
GL
726 *out_hwirq = intspec[0];
727 *out_type = IRQ_TYPE_NONE;
7e713301
GL
728 return 0;
729}
16b2e6e2
GL
730EXPORT_SYMBOL_GPL(irq_domain_xlate_onecell);
731
732/**
733 * irq_domain_xlate_twocell() - Generic xlate for direct two cell bindings
734 *
735 * Device Tree IRQ specifier translation function which works with two cell
736 * bindings where the cell values map directly to the hwirq number
737 * and linux irq flags.
738 */
739int irq_domain_xlate_twocell(struct irq_domain *d, struct device_node *ctrlr,
740 const u32 *intspec, unsigned int intsize,
741 irq_hw_number_t *out_hwirq, unsigned int *out_type)
742{
743 if (WARN_ON(intsize < 2))
744 return -EINVAL;
745 *out_hwirq = intspec[0];
746 *out_type = intspec[1] & IRQ_TYPE_SENSE_MASK;
747 return 0;
748}
749EXPORT_SYMBOL_GPL(irq_domain_xlate_twocell);
750
751/**
752 * irq_domain_xlate_onetwocell() - Generic xlate for one or two cell bindings
753 *
754 * Device Tree IRQ specifier translation function which works with either one
755 * or two cell bindings where the cell values map directly to the hwirq number
756 * and linux irq flags.
757 *
758 * Note: don't use this function unless your interrupt controller explicitly
759 * supports both one and two cell bindings. For the majority of controllers
760 * the _onecell() or _twocell() variants above should be used.
761 */
762int irq_domain_xlate_onetwocell(struct irq_domain *d,
763 struct device_node *ctrlr,
764 const u32 *intspec, unsigned int intsize,
765 unsigned long *out_hwirq, unsigned int *out_type)
766{
767 if (WARN_ON(intsize < 1))
768 return -EINVAL;
769 *out_hwirq = intspec[0];
770 *out_type = (intsize > 1) ? intspec[1] : IRQ_TYPE_NONE;
771 return 0;
772}
773EXPORT_SYMBOL_GPL(irq_domain_xlate_onetwocell);
7e713301 774
a18dc81b 775const struct irq_domain_ops irq_domain_simple_ops = {
75294957 776 .map = irq_domain_simple_map,
16b2e6e2 777 .xlate = irq_domain_xlate_onetwocell,
75294957
GL
778};
779EXPORT_SYMBOL_GPL(irq_domain_simple_ops);
780
781#ifdef CONFIG_OF_IRQ
7e713301
GL
782void irq_domain_generate_simple(const struct of_device_id *match,
783 u64 phys_base, unsigned int irq_start)
784{
785 struct device_node *node;
e1964c50 786 pr_debug("looking for phys_base=%llx, irq_start=%i\n",
7e713301
GL
787 (unsigned long long) phys_base, (int) irq_start);
788 node = of_find_matching_node_by_address(NULL, match, phys_base);
789 if (node)
6b783f7c
GL
790 irq_domain_add_legacy(node, 32, irq_start, 0,
791 &irq_domain_simple_ops, NULL);
7e713301
GL
792}
793EXPORT_SYMBOL_GPL(irq_domain_generate_simple);
75294957 794#endif