aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/irq
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/irq')
-rw-r--r--kernel/irq/irqdomain.c600
1 files changed, 600 insertions, 0 deletions
diff --git a/kernel/irq/irqdomain.c b/kernel/irq/irqdomain.c
index 509adb8762d7..f551bc1d3167 100644
--- a/kernel/irq/irqdomain.c
+++ b/kernel/irq/irqdomain.c
@@ -1,14 +1,612 @@
1#include <linux/debugfs.h>
2#include <linux/hardirq.h>
3#include <linux/interrupt.h>
1#include <linux/irq.h> 4#include <linux/irq.h>
5#include <linux/irqdesc.h>
2#include <linux/irqdomain.h> 6#include <linux/irqdomain.h>
3#include <linux/module.h> 7#include <linux/module.h>
4#include <linux/mutex.h> 8#include <linux/mutex.h>
5#include <linux/of.h> 9#include <linux/of.h>
6#include <linux/of_address.h> 10#include <linux/of_address.h>
11#include <linux/seq_file.h>
7#include <linux/slab.h> 12#include <linux/slab.h>
13#include <linux/smp.h>
14#include <linux/fs.h>
8 15
9static LIST_HEAD(irq_domain_list); 16static LIST_HEAD(irq_domain_list);
10static DEFINE_MUTEX(irq_domain_mutex); 17static DEFINE_MUTEX(irq_domain_mutex);
11 18
19#ifdef CONFIG_PPC
20static DEFINE_MUTEX(revmap_trees_mutex);
21static unsigned int irq_virq_count = NR_IRQS;
22static struct irq_domain *irq_default_host;
23
24static int default_irq_host_match(struct irq_domain *h, struct device_node *np)
25{
26 return h->of_node != NULL && h->of_node == np;
27}
28
29/**
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
36 *
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).
44 */
45struct 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)
50{
51 struct irq_domain *host, *h;
52 unsigned int size = sizeof(struct irq_domain);
53 unsigned int i;
54 unsigned int *rmap;
55
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);
60 if (host == NULL)
61 return NULL;
62
63 /* Fill structure */
64 host->revmap_type = revmap_type;
65 host->inval_irq = inval_irq;
66 host->ops = ops;
67 host->of_node = of_node_get(of_node);
68
69 if (host->ops->match == NULL)
70 host->ops->match = default_irq_host_match;
71
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);
79 kfree(host);
80 return NULL;
81 }
82 }
83 }
84 list_add(&host->link, &irq_domain_list);
85 mutex_unlock(&irq_domain_mutex);
86
87 /* Additional setups per revmap type */
88 switch(revmap_type) {
89 case IRQ_DOMAIN_MAP_LEGACY:
90 /* 0 is always the invalid number for legacy */
91 host->inval_irq = 0;
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);
95 irq_data->hwirq = i;
96 irq_data->domain = host;
97
98 /* Legacy flags are left to default at this point,
99 * one can then use irq_create_mapping() to
100 * explicitly change them
101 */
102 ops->map(host, i, i);
103
104 /* Clear norequest flags */
105 irq_clear_status_flags(i, IRQ_NOREQUEST);
106 }
107 break;
108 case IRQ_DOMAIN_MAP_LINEAR:
109 rmap = (unsigned int *)(host + 1);
110 for (i = 0; i < revmap_arg; i++)
111 rmap[i] = NO_IRQ;
112 host->revmap_data.linear.size = revmap_arg;
113 host->revmap_data.linear.revmap = rmap;
114 break;
115 case IRQ_DOMAIN_MAP_TREE:
116 INIT_RADIX_TREE(&host->revmap_data.tree, GFP_KERNEL);
117 break;
118 default:
119 break;
120 }
121
122 pr_debug("irq: Allocated host of type %d @0x%p\n", revmap_type, host);
123
124 return host;
125}
126
127/**
128 * irq_find_host() - Locates a domain for a given device node
129 * @node: device-tree node of the interrupt controller
130 */
131struct irq_domain *irq_find_host(struct device_node *node)
132{
133 struct irq_domain *h, *found = NULL;
134
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
138 * yet though...
139 */
140 mutex_lock(&irq_domain_mutex);
141 list_for_each_entry(h, &irq_domain_list, link)
142 if (h->ops->match(h, node)) {
143 found = h;
144 break;
145 }
146 mutex_unlock(&irq_domain_mutex);
147 return found;
148}
149EXPORT_SYMBOL_GPL(irq_find_host);
150
151/**
152 * irq_set_default_host() - Set a "default" irq domain
153 * @host: default host pointer
154 *
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.
159 */
160void irq_set_default_host(struct irq_domain *host)
161{
162 pr_debug("irq: Default host set to @0x%p\n", host);
163
164 irq_default_host = host;
165}
166
167/**
168 * irq_set_virq_count() - Set the maximum number of linux irqs
169 * @count: number of linux irqs, capped with NR_IRQS
170 *
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
173 */
174void irq_set_virq_count(unsigned int count)
175{
176 pr_debug("irq: Trying to set virq count to %d\n", count);
177
178 BUG_ON(count < NUM_ISA_INTERRUPTS);
179 if (count < NR_IRQS)
180 irq_virq_count = count;
181}
182
183static int irq_setup_virq(struct irq_domain *host, unsigned int virq,
184 irq_hw_number_t hwirq)
185{
186 struct irq_data *irq_data = irq_get_irq_data(virq);
187
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;
193 irq_data->hwirq = 0;
194 return -1;
195 }
196
197 irq_clear_status_flags(virq, IRQ_NOREQUEST);
198
199 return 0;
200}
201
202/**
203 * irq_create_direct_mapping() - Allocate an irq for direct mapping
204 * @host: domain to allocate the irq for or NULL for default host
205 *
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.
209 */
210unsigned int irq_create_direct_mapping(struct irq_domain *host)
211{
212 unsigned int virq;
213
214 if (host == NULL)
215 host = irq_default_host;
216
217 BUG_ON(host == NULL);
218 WARN_ON(host->revmap_type != IRQ_DOMAIN_MAP_NOMAP);
219
220 virq = irq_alloc_desc_from(1, 0);
221 if (virq == NO_IRQ) {
222 pr_debug("irq: create_direct virq allocation failed\n");
223 return NO_IRQ;
224 }
225 if (virq >= irq_virq_count) {
226 pr_err("ERROR: no free irqs available below %i maximum\n",
227 irq_virq_count);
228 irq_free_desc(virq);
229 return 0;
230 }
231
232 pr_debug("irq: create_direct obtained virq %d\n", virq);
233
234 if (irq_setup_virq(host, virq, virq)) {
235 irq_free_desc(virq);
236 return NO_IRQ;
237 }
238
239 return virq;
240}
241
242/**
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
246 *
247 * Only one mapping per hardware interrupt is permitted. Returns a linux
248 * irq number.
249 * If the sense/trigger is to be specified, set_irq_type() should be called
250 * on the number returned from that call.
251 */
252unsigned int irq_create_mapping(struct irq_domain *host,
253 irq_hw_number_t hwirq)
254{
255 unsigned int virq, hint;
256
257 pr_debug("irq: irq_create_mapping(0x%p, 0x%lx)\n", host, hwirq);
258
259 /* Look for default host if nececssary */
260 if (host == NULL)
261 host = irq_default_host;
262 if (host == NULL) {
263 printk(KERN_WARNING "irq_create_mapping called for"
264 " NULL host, hwirq=%lx\n", hwirq);
265 WARN_ON(1);
266 return NO_IRQ;
267 }
268 pr_debug("irq: -> using host @%p\n", host);
269
270 /* Check if mapping already exists */
271 virq = irq_find_mapping(host, hwirq);
272 if (virq != NO_IRQ) {
273 pr_debug("irq: -> existing mapping on virq %d\n", virq);
274 return virq;
275 }
276
277 /* Get a virtual interrupt number */
278 if (host->revmap_type == IRQ_DOMAIN_MAP_LEGACY) {
279 /* Handle legacy */
280 virq = (unsigned int)hwirq;
281 if (virq == 0 || virq >= NUM_ISA_INTERRUPTS)
282 return NO_IRQ;
283 return virq;
284 } else {
285 /* Allocate a virtual interrupt number */
286 hint = hwirq % irq_virq_count;
287 if (hint == 0)
288 hint++;
289 virq = irq_alloc_desc_from(hint, 0);
290 if (!virq)
291 virq = irq_alloc_desc_from(1, 0);
292 if (virq == NO_IRQ) {
293 pr_debug("irq: -> virq allocation failed\n");
294 return NO_IRQ;
295 }
296 }
297
298 if (irq_setup_virq(host, virq, hwirq)) {
299 if (host->revmap_type != IRQ_DOMAIN_MAP_LEGACY)
300 irq_free_desc(virq);
301 return NO_IRQ;
302 }
303
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);
306
307 return virq;
308}
309EXPORT_SYMBOL_GPL(irq_create_mapping);
310
311unsigned int irq_create_of_mapping(struct device_node *controller,
312 const u32 *intspec, unsigned int intsize)
313{
314 struct irq_domain *host;
315 irq_hw_number_t hwirq;
316 unsigned int type = IRQ_TYPE_NONE;
317 unsigned int virq;
318
319 if (controller == NULL)
320 host = irq_default_host;
321 else
322 host = irq_find_host(controller);
323 if (host == NULL) {
324 printk(KERN_WARNING "irq: no irq host found for %s !\n",
325 controller->full_name);
326 return NO_IRQ;
327 }
328
329 /* If host has no translation, then we assume interrupt line */
330 if (host->ops->xlate == NULL)
331 hwirq = intspec[0];
332 else {
333 if (host->ops->xlate(host, controller, intspec, intsize,
334 &hwirq, &type))
335 return NO_IRQ;
336 }
337
338 /* Create mapping */
339 virq = irq_create_mapping(host, hwirq);
340 if (virq == NO_IRQ)
341 return virq;
342
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);
347 return virq;
348}
349EXPORT_SYMBOL_GPL(irq_create_of_mapping);
350
351/**
352 * irq_dispose_mapping() - Unmap an interrupt
353 * @virq: linux irq number of the interrupt to unmap
354 */
355void irq_dispose_mapping(unsigned int virq)
356{
357 struct irq_data *irq_data = irq_get_irq_data(virq);
358 struct irq_domain *host;
359 irq_hw_number_t hwirq;
360
361 if (virq == NO_IRQ || !irq_data)
362 return;
363
364 host = irq_data->domain;
365 if (WARN_ON(host == NULL))
366 return;
367
368 /* Never unmap legacy interrupts */
369 if (host->revmap_type == IRQ_DOMAIN_MAP_LEGACY)
370 return;
371
372 irq_set_status_flags(virq, IRQ_NOREQUEST);
373
374 /* remove chip and handler */
375 irq_set_chip_and_handler(virq, NULL, NULL);
376
377 /* Make sure it's completed */
378 synchronize_irq(virq);
379
380 /* Tell the PIC about it */
381 if (host->ops->unmap)
382 host->ops->unmap(host, virq);
383 smp_mb();
384
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] = NO_IRQ;
391 break;
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);
396 break;
397 }
398
399 /* Destroy map */
400 irq_data->hwirq = host->inval_irq;
401
402 irq_free_desc(virq);
403}
404EXPORT_SYMBOL_GPL(irq_dispose_mapping);
405
406/**
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
410 *
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
413 * mapping function.
414 */
415unsigned int irq_find_mapping(struct irq_domain *host,
416 irq_hw_number_t hwirq)
417{
418 unsigned int i;
419 unsigned int hint = hwirq % irq_virq_count;
420
421 /* Look for default host if nececssary */
422 if (host == NULL)
423 host = irq_default_host;
424 if (host == NULL)
425 return NO_IRQ;
426
427 /* legacy -> bail early */
428 if (host->revmap_type == IRQ_DOMAIN_MAP_LEGACY)
429 return hwirq;
430
431 /* Slow path does a linear search of the map */
432 if (hint == 0)
433 hint = 1;
434 i = hint;
435 do {
436 struct irq_data *data = irq_get_irq_data(i);
437 if (data && (data->domain == host) && (data->hwirq == hwirq))
438 return i;
439 i++;
440 if (i >= irq_virq_count)
441 i = 1;
442 } while(i != hint);
443 return NO_IRQ;
444}
445EXPORT_SYMBOL_GPL(irq_find_mapping);
446
447/**
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
451 *
452 * This is a fast path, for use by irq controller code that uses radix tree
453 * revmaps
454 */
455unsigned int irq_radix_revmap_lookup(struct irq_domain *host,
456 irq_hw_number_t hwirq)
457{
458 struct irq_data *irq_data;
459
460 if (WARN_ON_ONCE(host->revmap_type != IRQ_DOMAIN_MAP_TREE))
461 return irq_find_mapping(host, hwirq);
462
463 /*
464 * Freeing an irq can delete nodes along the path to
465 * do the lookup via call_rcu.
466 */
467 rcu_read_lock();
468 irq_data = radix_tree_lookup(&host->revmap_data.tree, hwirq);
469 rcu_read_unlock();
470
471 /*
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.
475 */
476 return irq_data ? irq_data->irq : irq_find_mapping(host, hwirq);
477}
478
479/**
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
484 *
485 * This is for use by irq controllers that use a radix tree reverse
486 * mapping for fast lookup.
487 */
488void irq_radix_revmap_insert(struct irq_domain *host, unsigned int virq,
489 irq_hw_number_t hwirq)
490{
491 struct irq_data *irq_data = irq_get_irq_data(virq);
492
493 if (WARN_ON(host->revmap_type != IRQ_DOMAIN_MAP_TREE))
494 return;
495
496 if (virq != NO_IRQ) {
497 mutex_lock(&revmap_trees_mutex);
498 radix_tree_insert(&host->revmap_data.tree, hwirq, irq_data);
499 mutex_unlock(&revmap_trees_mutex);
500 }
501}
502
503/**
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
507 *
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
511 */
512unsigned int irq_linear_revmap(struct irq_domain *host,
513 irq_hw_number_t hwirq)
514{
515 unsigned int *revmap;
516
517 if (WARN_ON_ONCE(host->revmap_type != IRQ_DOMAIN_MAP_LINEAR))
518 return irq_find_mapping(host, hwirq);
519
520 /* Check revmap bounds */
521 if (unlikely(hwirq >= host->revmap_data.linear.size))
522 return irq_find_mapping(host, hwirq);
523
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);
528
529 /* Fill up revmap with slow path if no mapping found */
530 if (unlikely(revmap[hwirq] == NO_IRQ))
531 revmap[hwirq] = irq_find_mapping(host, hwirq);
532
533 return revmap[hwirq];
534}
535
536#ifdef CONFIG_VIRQ_DEBUG
537static int virq_debug_show(struct seq_file *m, void *private)
538{
539 unsigned long flags;
540 struct irq_desc *desc;
541 const char *p;
542 static const char none[] = "none";
543 void *data;
544 int i;
545
546 seq_printf(m, "%-5s %-7s %-15s %-18s %s\n", "virq", "hwirq",
547 "chip name", "chip data", "host name");
548
549 for (i = 1; i < nr_irqs; i++) {
550 desc = irq_to_desc(i);
551 if (!desc)
552 continue;
553
554 raw_spin_lock_irqsave(&desc->lock, flags);
555
556 if (desc->action && desc->action->handler) {
557 struct irq_chip *chip;
558
559 seq_printf(m, "%5d ", i);
560 seq_printf(m, "0x%05lx ", desc->irq_data.hwirq);
561
562 chip = irq_desc_get_chip(desc);
563 if (chip && chip->name)
564 p = chip->name;
565 else
566 p = none;
567 seq_printf(m, "%-15s ", p);
568
569 data = irq_desc_get_chip_data(desc);
570 seq_printf(m, "0x%16p ", data);
571
572 if (desc->irq_data.domain->of_node)
573 p = desc->irq_data.domain->of_node->full_name;
574 else
575 p = none;
576 seq_printf(m, "%s\n", p);
577 }
578
579 raw_spin_unlock_irqrestore(&desc->lock, flags);
580 }
581
582 return 0;
583}
584
585static int virq_debug_open(struct inode *inode, struct file *file)
586{
587 return single_open(file, virq_debug_show, inode->i_private);
588}
589
590static const struct file_operations virq_debug_fops = {
591 .open = virq_debug_open,
592 .read = seq_read,
593 .llseek = seq_lseek,
594 .release = single_release,
595};
596
597static int __init irq_debugfs_init(void)
598{
599 if (debugfs_create_file("virq_mapping", S_IRUGO, powerpc_debugfs_root,
600 NULL, &virq_debug_fops) == NULL)
601 return -ENOMEM;
602
603 return 0;
604}
605__initcall(irq_debugfs_init);
606#endif /* CONFIG_VIRQ_DEBUG */
607
608#else /* CONFIG_PPC */
609
12/** 610/**
13 * irq_domain_add() - Register an irq_domain 611 * irq_domain_add() - Register an irq_domain
14 * @domain: ptr to initialized irq_domain structure 612 * @domain: ptr to initialized irq_domain structure
@@ -185,3 +783,5 @@ struct irq_domain_ops irq_domain_simple_ops = {
185#endif /* CONFIG_OF_IRQ */ 783#endif /* CONFIG_OF_IRQ */
186}; 784};
187EXPORT_SYMBOL_GPL(irq_domain_simple_ops); 785EXPORT_SYMBOL_GPL(irq_domain_simple_ops);
786
787#endif /* !CONFIG_PPC */