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