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