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