irqdomain: Use irq_domain_get_of_node() instead of direct field access
[linux-drm-fsl-dcu.git] / kernel / irq / irqdomain.c
1 #define pr_fmt(fmt)  "irq: " fmt
2
3 #include <linux/debugfs.h>
4 #include <linux/hardirq.h>
5 #include <linux/interrupt.h>
6 #include <linux/irq.h>
7 #include <linux/irqdesc.h>
8 #include <linux/irqdomain.h>
9 #include <linux/module.h>
10 #include <linux/mutex.h>
11 #include <linux/of.h>
12 #include <linux/of_address.h>
13 #include <linux/of_irq.h>
14 #include <linux/topology.h>
15 #include <linux/seq_file.h>
16 #include <linux/slab.h>
17 #include <linux/smp.h>
18 #include <linux/fs.h>
19
20 static LIST_HEAD(irq_domain_list);
21 static DEFINE_MUTEX(irq_domain_mutex);
22
23 static DEFINE_MUTEX(revmap_trees_mutex);
24 static struct irq_domain *irq_default_domain;
25
26 static int irq_domain_alloc_descs(int virq, unsigned int nr_irqs,
27                                   irq_hw_number_t hwirq, int node);
28 static void irq_domain_check_hierarchy(struct irq_domain *domain);
29
30 /**
31  * __irq_domain_add() - Allocate a new irq_domain data structure
32  * @of_node: optional device-tree node of the interrupt controller
33  * @size: Size of linear map; 0 for radix mapping only
34  * @hwirq_max: Maximum number of interrupts supported by controller
35  * @direct_max: Maximum value of direct maps; Use ~0 for no limit; 0 for no
36  *              direct mapping
37  * @ops: domain callbacks
38  * @host_data: Controller private data pointer
39  *
40  * Allocates and initialize and irq_domain structure.
41  * Returns pointer to IRQ domain, or NULL on failure.
42  */
43 struct irq_domain *__irq_domain_add(struct device_node *of_node, int size,
44                                     irq_hw_number_t hwirq_max, int direct_max,
45                                     const struct irq_domain_ops *ops,
46                                     void *host_data)
47 {
48         struct irq_domain *domain;
49
50         domain = kzalloc_node(sizeof(*domain) + (sizeof(unsigned int) * size),
51                               GFP_KERNEL, of_node_to_nid(of_node));
52         if (WARN_ON(!domain))
53                 return NULL;
54
55         /* Fill structure */
56         INIT_RADIX_TREE(&domain->revmap_tree, GFP_KERNEL);
57         domain->ops = ops;
58         domain->host_data = host_data;
59         domain->of_node = of_node_get(of_node);
60         domain->hwirq_max = hwirq_max;
61         domain->revmap_size = size;
62         domain->revmap_direct_max_irq = direct_max;
63         irq_domain_check_hierarchy(domain);
64
65         mutex_lock(&irq_domain_mutex);
66         list_add(&domain->link, &irq_domain_list);
67         mutex_unlock(&irq_domain_mutex);
68
69         pr_debug("Added domain %s\n", domain->name);
70         return domain;
71 }
72 EXPORT_SYMBOL_GPL(__irq_domain_add);
73
74 /**
75  * irq_domain_remove() - Remove an irq domain.
76  * @domain: domain to remove
77  *
78  * This routine is used to remove an irq domain. The caller must ensure
79  * that all mappings within the domain have been disposed of prior to
80  * use, depending on the revmap type.
81  */
82 void irq_domain_remove(struct irq_domain *domain)
83 {
84         mutex_lock(&irq_domain_mutex);
85
86         /*
87          * radix_tree_delete() takes care of destroying the root
88          * node when all entries are removed. Shout if there are
89          * any mappings left.
90          */
91         WARN_ON(domain->revmap_tree.height);
92
93         list_del(&domain->link);
94
95         /*
96          * If the going away domain is the default one, reset it.
97          */
98         if (unlikely(irq_default_domain == domain))
99                 irq_set_default_host(NULL);
100
101         mutex_unlock(&irq_domain_mutex);
102
103         pr_debug("Removed domain %s\n", domain->name);
104
105         of_node_put(irq_domain_get_of_node(domain));
106         kfree(domain);
107 }
108 EXPORT_SYMBOL_GPL(irq_domain_remove);
109
110 /**
111  * irq_domain_add_simple() - Register an irq_domain and optionally map a range of irqs
112  * @of_node: pointer to interrupt controller's device tree node.
113  * @size: total number of irqs in mapping
114  * @first_irq: first number of irq block assigned to the domain,
115  *      pass zero to assign irqs on-the-fly. If first_irq is non-zero, then
116  *      pre-map all of the irqs in the domain to virqs starting at first_irq.
117  * @ops: domain callbacks
118  * @host_data: Controller private data pointer
119  *
120  * Allocates an irq_domain, and optionally if first_irq is positive then also
121  * allocate irq_descs and map all of the hwirqs to virqs starting at first_irq.
122  *
123  * This is intended to implement the expected behaviour for most
124  * interrupt controllers. If device tree is used, then first_irq will be 0 and
125  * irqs get mapped dynamically on the fly. However, if the controller requires
126  * static virq assignments (non-DT boot) then it will set that up correctly.
127  */
128 struct irq_domain *irq_domain_add_simple(struct device_node *of_node,
129                                          unsigned int size,
130                                          unsigned int first_irq,
131                                          const struct irq_domain_ops *ops,
132                                          void *host_data)
133 {
134         struct irq_domain *domain;
135
136         domain = __irq_domain_add(of_node, size, size, 0, ops, host_data);
137         if (!domain)
138                 return NULL;
139
140         if (first_irq > 0) {
141                 if (IS_ENABLED(CONFIG_SPARSE_IRQ)) {
142                         /* attempt to allocated irq_descs */
143                         int rc = irq_alloc_descs(first_irq, first_irq, size,
144                                                  of_node_to_nid(of_node));
145                         if (rc < 0)
146                                 pr_info("Cannot allocate irq_descs @ IRQ%d, assuming pre-allocated\n",
147                                         first_irq);
148                 }
149                 irq_domain_associate_many(domain, first_irq, 0, size);
150         }
151
152         return domain;
153 }
154 EXPORT_SYMBOL_GPL(irq_domain_add_simple);
155
156 /**
157  * irq_domain_add_legacy() - Allocate and register a legacy revmap irq_domain.
158  * @of_node: pointer to interrupt controller's device tree node.
159  * @size: total number of irqs in legacy mapping
160  * @first_irq: first number of irq block assigned to the domain
161  * @first_hwirq: first hwirq number to use for the translation. Should normally
162  *               be '0', but a positive integer can be used if the effective
163  *               hwirqs numbering does not begin at zero.
164  * @ops: map/unmap domain callbacks
165  * @host_data: Controller private data pointer
166  *
167  * Note: the map() callback will be called before this function returns
168  * for all legacy interrupts except 0 (which is always the invalid irq for
169  * a legacy controller).
170  */
171 struct irq_domain *irq_domain_add_legacy(struct device_node *of_node,
172                                          unsigned int size,
173                                          unsigned int first_irq,
174                                          irq_hw_number_t first_hwirq,
175                                          const struct irq_domain_ops *ops,
176                                          void *host_data)
177 {
178         struct irq_domain *domain;
179
180         domain = __irq_domain_add(of_node, first_hwirq + size,
181                                   first_hwirq + size, 0, ops, host_data);
182         if (domain)
183                 irq_domain_associate_many(domain, first_irq, first_hwirq, size);
184
185         return domain;
186 }
187 EXPORT_SYMBOL_GPL(irq_domain_add_legacy);
188
189 /**
190  * irq_find_matching_host() - Locates a domain for a given device node
191  * @node: device-tree node of the interrupt controller
192  * @bus_token: domain-specific data
193  */
194 struct irq_domain *irq_find_matching_host(struct device_node *node,
195                                           enum irq_domain_bus_token bus_token)
196 {
197         struct irq_domain *h, *found = NULL;
198         int rc;
199
200         /* We might want to match the legacy controller last since
201          * it might potentially be set to match all interrupts in
202          * the absence of a device node. This isn't a problem so far
203          * yet though...
204          *
205          * bus_token == DOMAIN_BUS_ANY matches any domain, any other
206          * values must generate an exact match for the domain to be
207          * selected.
208          */
209         mutex_lock(&irq_domain_mutex);
210         list_for_each_entry(h, &irq_domain_list, link) {
211                 struct device_node *of_node;
212                 of_node = irq_domain_get_of_node(h);
213                 if (h->ops->match)
214                         rc = h->ops->match(h, node, bus_token);
215                 else
216                         rc = ((of_node != NULL) && (of_node == node) &&
217                               ((bus_token == DOMAIN_BUS_ANY) ||
218                                (h->bus_token == bus_token)));
219
220                 if (rc) {
221                         found = h;
222                         break;
223                 }
224         }
225         mutex_unlock(&irq_domain_mutex);
226         return found;
227 }
228 EXPORT_SYMBOL_GPL(irq_find_matching_host);
229
230 /**
231  * irq_set_default_host() - Set a "default" irq domain
232  * @domain: default domain pointer
233  *
234  * For convenience, it's possible to set a "default" domain that will be used
235  * whenever NULL is passed to irq_create_mapping(). It makes life easier for
236  * platforms that want to manipulate a few hard coded interrupt numbers that
237  * aren't properly represented in the device-tree.
238  */
239 void irq_set_default_host(struct irq_domain *domain)
240 {
241         pr_debug("Default domain set to @0x%p\n", domain);
242
243         irq_default_domain = domain;
244 }
245 EXPORT_SYMBOL_GPL(irq_set_default_host);
246
247 void irq_domain_disassociate(struct irq_domain *domain, unsigned int irq)
248 {
249         struct irq_data *irq_data = irq_get_irq_data(irq);
250         irq_hw_number_t hwirq;
251
252         if (WARN(!irq_data || irq_data->domain != domain,
253                  "virq%i doesn't exist; cannot disassociate\n", irq))
254                 return;
255
256         hwirq = irq_data->hwirq;
257         irq_set_status_flags(irq, IRQ_NOREQUEST);
258
259         /* remove chip and handler */
260         irq_set_chip_and_handler(irq, NULL, NULL);
261
262         /* Make sure it's completed */
263         synchronize_irq(irq);
264
265         /* Tell the PIC about it */
266         if (domain->ops->unmap)
267                 domain->ops->unmap(domain, irq);
268         smp_mb();
269
270         irq_data->domain = NULL;
271         irq_data->hwirq = 0;
272
273         /* Clear reverse map for this hwirq */
274         if (hwirq < domain->revmap_size) {
275                 domain->linear_revmap[hwirq] = 0;
276         } else {
277                 mutex_lock(&revmap_trees_mutex);
278                 radix_tree_delete(&domain->revmap_tree, hwirq);
279                 mutex_unlock(&revmap_trees_mutex);
280         }
281 }
282
283 int irq_domain_associate(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         int ret;
288
289         if (WARN(hwirq >= domain->hwirq_max,
290                  "error: hwirq 0x%x is too large for %s\n", (int)hwirq, domain->name))
291                 return -EINVAL;
292         if (WARN(!irq_data, "error: virq%i is not allocated", virq))
293                 return -EINVAL;
294         if (WARN(irq_data->domain, "error: virq%i is already associated", virq))
295                 return -EINVAL;
296
297         mutex_lock(&irq_domain_mutex);
298         irq_data->hwirq = hwirq;
299         irq_data->domain = domain;
300         if (domain->ops->map) {
301                 ret = domain->ops->map(domain, virq, hwirq);
302                 if (ret != 0) {
303                         /*
304                          * If map() returns -EPERM, this interrupt is protected
305                          * by the firmware or some other service and shall not
306                          * be mapped. Don't bother telling the user about it.
307                          */
308                         if (ret != -EPERM) {
309                                 pr_info("%s didn't like hwirq-0x%lx to VIRQ%i mapping (rc=%d)\n",
310                                        domain->name, hwirq, virq, ret);
311                         }
312                         irq_data->domain = NULL;
313                         irq_data->hwirq = 0;
314                         mutex_unlock(&irq_domain_mutex);
315                         return ret;
316                 }
317
318                 /* If not already assigned, give the domain the chip's name */
319                 if (!domain->name && irq_data->chip)
320                         domain->name = irq_data->chip->name;
321         }
322
323         if (hwirq < domain->revmap_size) {
324                 domain->linear_revmap[hwirq] = virq;
325         } else {
326                 mutex_lock(&revmap_trees_mutex);
327                 radix_tree_insert(&domain->revmap_tree, hwirq, irq_data);
328                 mutex_unlock(&revmap_trees_mutex);
329         }
330         mutex_unlock(&irq_domain_mutex);
331
332         irq_clear_status_flags(virq, IRQ_NOREQUEST);
333
334         return 0;
335 }
336 EXPORT_SYMBOL_GPL(irq_domain_associate);
337
338 void irq_domain_associate_many(struct irq_domain *domain, unsigned int irq_base,
339                                irq_hw_number_t hwirq_base, int count)
340 {
341         struct device_node *of_node;
342         int i;
343
344         of_node = irq_domain_get_of_node(domain);
345         pr_debug("%s(%s, irqbase=%i, hwbase=%i, count=%i)\n", __func__,
346                 of_node_full_name(of_node), irq_base, (int)hwirq_base, count);
347
348         for (i = 0; i < count; i++) {
349                 irq_domain_associate(domain, irq_base + i, hwirq_base + i);
350         }
351 }
352 EXPORT_SYMBOL_GPL(irq_domain_associate_many);
353
354 /**
355  * irq_create_direct_mapping() - Allocate an irq for direct mapping
356  * @domain: domain to allocate the irq for or NULL for default domain
357  *
358  * This routine is used for irq controllers which can choose the hardware
359  * interrupt numbers they generate. In such a case it's simplest to use
360  * the linux irq as the hardware interrupt number. It still uses the linear
361  * or radix tree to store the mapping, but the irq controller can optimize
362  * the revmap path by using the hwirq directly.
363  */
364 unsigned int irq_create_direct_mapping(struct irq_domain *domain)
365 {
366         struct device_node *of_node;
367         unsigned int virq;
368
369         if (domain == NULL)
370                 domain = irq_default_domain;
371
372         of_node = irq_domain_get_of_node(domain);
373         virq = irq_alloc_desc_from(1, of_node_to_nid(of_node));
374         if (!virq) {
375                 pr_debug("create_direct virq allocation failed\n");
376                 return 0;
377         }
378         if (virq >= domain->revmap_direct_max_irq) {
379                 pr_err("ERROR: no free irqs available below %i maximum\n",
380                         domain->revmap_direct_max_irq);
381                 irq_free_desc(virq);
382                 return 0;
383         }
384         pr_debug("create_direct obtained virq %d\n", virq);
385
386         if (irq_domain_associate(domain, virq, virq)) {
387                 irq_free_desc(virq);
388                 return 0;
389         }
390
391         return virq;
392 }
393 EXPORT_SYMBOL_GPL(irq_create_direct_mapping);
394
395 /**
396  * irq_create_mapping() - Map a hardware interrupt into linux irq space
397  * @domain: domain owning this hardware interrupt or NULL for default domain
398  * @hwirq: hardware irq number in that domain space
399  *
400  * Only one mapping per hardware interrupt is permitted. Returns a linux
401  * irq number.
402  * If the sense/trigger is to be specified, set_irq_type() should be called
403  * on the number returned from that call.
404  */
405 unsigned int irq_create_mapping(struct irq_domain *domain,
406                                 irq_hw_number_t hwirq)
407 {
408         struct device_node *of_node;
409         int virq;
410
411         pr_debug("irq_create_mapping(0x%p, 0x%lx)\n", domain, hwirq);
412
413         /* Look for default domain if nececssary */
414         if (domain == NULL)
415                 domain = irq_default_domain;
416         if (domain == NULL) {
417                 WARN(1, "%s(, %lx) called with NULL domain\n", __func__, hwirq);
418                 return 0;
419         }
420         pr_debug("-> using domain @%p\n", domain);
421
422         of_node = irq_domain_get_of_node(domain);
423
424         /* Check if mapping already exists */
425         virq = irq_find_mapping(domain, hwirq);
426         if (virq) {
427                 pr_debug("-> existing mapping on virq %d\n", virq);
428                 return virq;
429         }
430
431         /* Allocate a virtual interrupt number */
432         virq = irq_domain_alloc_descs(-1, 1, hwirq, of_node_to_nid(of_node));
433         if (virq <= 0) {
434                 pr_debug("-> virq allocation failed\n");
435                 return 0;
436         }
437
438         if (irq_domain_associate(domain, virq, hwirq)) {
439                 irq_free_desc(virq);
440                 return 0;
441         }
442
443         pr_debug("irq %lu on domain %s mapped to virtual irq %u\n",
444                 hwirq, of_node_full_name(of_node), virq);
445
446         return virq;
447 }
448 EXPORT_SYMBOL_GPL(irq_create_mapping);
449
450 /**
451  * irq_create_strict_mappings() - Map a range of hw irqs to fixed linux irqs
452  * @domain: domain owning the interrupt range
453  * @irq_base: beginning of linux IRQ range
454  * @hwirq_base: beginning of hardware IRQ range
455  * @count: Number of interrupts to map
456  *
457  * This routine is used for allocating and mapping a range of hardware
458  * irqs to linux irqs where the linux irq numbers are at pre-defined
459  * locations. For use by controllers that already have static mappings
460  * to insert in to the domain.
461  *
462  * Non-linear users can use irq_create_identity_mapping() for IRQ-at-a-time
463  * domain insertion.
464  *
465  * 0 is returned upon success, while any failure to establish a static
466  * mapping is treated as an error.
467  */
468 int irq_create_strict_mappings(struct irq_domain *domain, unsigned int irq_base,
469                                irq_hw_number_t hwirq_base, int count)
470 {
471         struct device_node *of_node;
472         int ret;
473
474         of_node = irq_domain_get_of_node(domain);
475         ret = irq_alloc_descs(irq_base, irq_base, count,
476                               of_node_to_nid(of_node));
477         if (unlikely(ret < 0))
478                 return ret;
479
480         irq_domain_associate_many(domain, irq_base, hwirq_base, count);
481         return 0;
482 }
483 EXPORT_SYMBOL_GPL(irq_create_strict_mappings);
484
485 unsigned int irq_create_of_mapping(struct of_phandle_args *irq_data)
486 {
487         struct irq_domain *domain;
488         irq_hw_number_t hwirq;
489         unsigned int type = IRQ_TYPE_NONE;
490         int virq;
491
492         domain = irq_data->np ? irq_find_host(irq_data->np) : irq_default_domain;
493         if (!domain) {
494                 pr_warn("no irq domain found for %s !\n",
495                         of_node_full_name(irq_data->np));
496                 return 0;
497         }
498
499         /* If domain has no translation, then we assume interrupt line */
500         if (domain->ops->xlate == NULL)
501                 hwirq = irq_data->args[0];
502         else {
503                 if (domain->ops->xlate(domain, irq_data->np, irq_data->args,
504                                         irq_data->args_count, &hwirq, &type))
505                         return 0;
506         }
507
508         if (irq_domain_is_hierarchy(domain)) {
509                 /*
510                  * If we've already configured this interrupt,
511                  * don't do it again, or hell will break loose.
512                  */
513                 virq = irq_find_mapping(domain, hwirq);
514                 if (virq)
515                         return virq;
516
517                 virq = irq_domain_alloc_irqs(domain, 1, NUMA_NO_NODE, irq_data);
518                 if (virq <= 0)
519                         return 0;
520         } else {
521                 /* Create mapping */
522                 virq = irq_create_mapping(domain, hwirq);
523                 if (!virq)
524                         return virq;
525         }
526
527         /* Set type if specified and different than the current one */
528         if (type != IRQ_TYPE_NONE &&
529             type != irq_get_trigger_type(virq))
530                 irq_set_irq_type(virq, type);
531         return virq;
532 }
533 EXPORT_SYMBOL_GPL(irq_create_of_mapping);
534
535 /**
536  * irq_dispose_mapping() - Unmap an interrupt
537  * @virq: linux irq number of the interrupt to unmap
538  */
539 void irq_dispose_mapping(unsigned int virq)
540 {
541         struct irq_data *irq_data = irq_get_irq_data(virq);
542         struct irq_domain *domain;
543
544         if (!virq || !irq_data)
545                 return;
546
547         domain = irq_data->domain;
548         if (WARN_ON(domain == NULL))
549                 return;
550
551         irq_domain_disassociate(domain, virq);
552         irq_free_desc(virq);
553 }
554 EXPORT_SYMBOL_GPL(irq_dispose_mapping);
555
556 /**
557  * irq_find_mapping() - Find a linux irq from an hw irq number.
558  * @domain: domain owning this hardware interrupt
559  * @hwirq: hardware irq number in that domain space
560  */
561 unsigned int irq_find_mapping(struct irq_domain *domain,
562                               irq_hw_number_t hwirq)
563 {
564         struct irq_data *data;
565
566         /* Look for default domain if nececssary */
567         if (domain == NULL)
568                 domain = irq_default_domain;
569         if (domain == NULL)
570                 return 0;
571
572         if (hwirq < domain->revmap_direct_max_irq) {
573                 data = irq_domain_get_irq_data(domain, hwirq);
574                 if (data && data->hwirq == hwirq)
575                         return hwirq;
576         }
577
578         /* Check if the hwirq is in the linear revmap. */
579         if (hwirq < domain->revmap_size)
580                 return domain->linear_revmap[hwirq];
581
582         rcu_read_lock();
583         data = radix_tree_lookup(&domain->revmap_tree, hwirq);
584         rcu_read_unlock();
585         return data ? data->irq : 0;
586 }
587 EXPORT_SYMBOL_GPL(irq_find_mapping);
588
589 #ifdef CONFIG_IRQ_DOMAIN_DEBUG
590 static int virq_debug_show(struct seq_file *m, void *private)
591 {
592         unsigned long flags;
593         struct irq_desc *desc;
594         struct irq_domain *domain;
595         struct radix_tree_iter iter;
596         void *data, **slot;
597         int i;
598
599         seq_printf(m, " %-16s  %-6s  %-10s  %-10s  %s\n",
600                    "name", "mapped", "linear-max", "direct-max", "devtree-node");
601         mutex_lock(&irq_domain_mutex);
602         list_for_each_entry(domain, &irq_domain_list, link) {
603                 struct device_node *of_node;
604                 int count = 0;
605                 of_node = irq_domain_get_of_node(domain);
606                 radix_tree_for_each_slot(slot, &domain->revmap_tree, &iter, 0)
607                         count++;
608                 seq_printf(m, "%c%-16s  %6u  %10u  %10u  %s\n",
609                            domain == irq_default_domain ? '*' : ' ', domain->name,
610                            domain->revmap_size + count, domain->revmap_size,
611                            domain->revmap_direct_max_irq,
612                            of_node ? of_node_full_name(of_node) : "");
613         }
614         mutex_unlock(&irq_domain_mutex);
615
616         seq_printf(m, "%-5s  %-7s  %-15s  %-*s  %6s  %-14s  %s\n", "irq", "hwirq",
617                       "chip name", (int)(2 * sizeof(void *) + 2), "chip data",
618                       "active", "type", "domain");
619
620         for (i = 1; i < nr_irqs; i++) {
621                 desc = irq_to_desc(i);
622                 if (!desc)
623                         continue;
624
625                 raw_spin_lock_irqsave(&desc->lock, flags);
626                 domain = desc->irq_data.domain;
627
628                 if (domain) {
629                         struct irq_chip *chip;
630                         int hwirq = desc->irq_data.hwirq;
631                         bool direct;
632
633                         seq_printf(m, "%5d  ", i);
634                         seq_printf(m, "0x%05x  ", hwirq);
635
636                         chip = irq_desc_get_chip(desc);
637                         seq_printf(m, "%-15s  ", (chip && chip->name) ? chip->name : "none");
638
639                         data = irq_desc_get_chip_data(desc);
640                         seq_printf(m, data ? "0x%p  " : "  %p  ", data);
641
642                         seq_printf(m, "   %c    ", (desc->action && desc->action->handler) ? '*' : ' ');
643                         direct = (i == hwirq) && (i < domain->revmap_direct_max_irq);
644                         seq_printf(m, "%6s%-8s  ",
645                                    (hwirq < domain->revmap_size) ? "LINEAR" : "RADIX",
646                                    direct ? "(DIRECT)" : "");
647                         seq_printf(m, "%s\n", desc->irq_data.domain->name);
648                 }
649
650                 raw_spin_unlock_irqrestore(&desc->lock, flags);
651         }
652
653         return 0;
654 }
655
656 static int virq_debug_open(struct inode *inode, struct file *file)
657 {
658         return single_open(file, virq_debug_show, inode->i_private);
659 }
660
661 static const struct file_operations virq_debug_fops = {
662         .open = virq_debug_open,
663         .read = seq_read,
664         .llseek = seq_lseek,
665         .release = single_release,
666 };
667
668 static int __init irq_debugfs_init(void)
669 {
670         if (debugfs_create_file("irq_domain_mapping", S_IRUGO, NULL,
671                                  NULL, &virq_debug_fops) == NULL)
672                 return -ENOMEM;
673
674         return 0;
675 }
676 __initcall(irq_debugfs_init);
677 #endif /* CONFIG_IRQ_DOMAIN_DEBUG */
678
679 /**
680  * irq_domain_xlate_onecell() - Generic xlate for direct one cell bindings
681  *
682  * Device Tree IRQ specifier translation function which works with one cell
683  * bindings where the cell value maps directly to the hwirq number.
684  */
685 int irq_domain_xlate_onecell(struct irq_domain *d, struct device_node *ctrlr,
686                              const u32 *intspec, unsigned int intsize,
687                              unsigned long *out_hwirq, unsigned int *out_type)
688 {
689         if (WARN_ON(intsize < 1))
690                 return -EINVAL;
691         *out_hwirq = intspec[0];
692         *out_type = IRQ_TYPE_NONE;
693         return 0;
694 }
695 EXPORT_SYMBOL_GPL(irq_domain_xlate_onecell);
696
697 /**
698  * irq_domain_xlate_twocell() - Generic xlate for direct two cell bindings
699  *
700  * Device Tree IRQ specifier translation function which works with two cell
701  * bindings where the cell values map directly to the hwirq number
702  * and linux irq flags.
703  */
704 int irq_domain_xlate_twocell(struct irq_domain *d, struct device_node *ctrlr,
705                         const u32 *intspec, unsigned int intsize,
706                         irq_hw_number_t *out_hwirq, unsigned int *out_type)
707 {
708         if (WARN_ON(intsize < 2))
709                 return -EINVAL;
710         *out_hwirq = intspec[0];
711         *out_type = intspec[1] & IRQ_TYPE_SENSE_MASK;
712         return 0;
713 }
714 EXPORT_SYMBOL_GPL(irq_domain_xlate_twocell);
715
716 /**
717  * irq_domain_xlate_onetwocell() - Generic xlate for one or two cell bindings
718  *
719  * Device Tree IRQ specifier translation function which works with either one
720  * or two cell bindings where the cell values map directly to the hwirq number
721  * and linux irq flags.
722  *
723  * Note: don't use this function unless your interrupt controller explicitly
724  * supports both one and two cell bindings.  For the majority of controllers
725  * the _onecell() or _twocell() variants above should be used.
726  */
727 int irq_domain_xlate_onetwocell(struct irq_domain *d,
728                                 struct device_node *ctrlr,
729                                 const u32 *intspec, unsigned int intsize,
730                                 unsigned long *out_hwirq, unsigned int *out_type)
731 {
732         if (WARN_ON(intsize < 1))
733                 return -EINVAL;
734         *out_hwirq = intspec[0];
735         *out_type = (intsize > 1) ? intspec[1] : IRQ_TYPE_NONE;
736         return 0;
737 }
738 EXPORT_SYMBOL_GPL(irq_domain_xlate_onetwocell);
739
740 const struct irq_domain_ops irq_domain_simple_ops = {
741         .xlate = irq_domain_xlate_onetwocell,
742 };
743 EXPORT_SYMBOL_GPL(irq_domain_simple_ops);
744
745 static int irq_domain_alloc_descs(int virq, unsigned int cnt,
746                                   irq_hw_number_t hwirq, int node)
747 {
748         unsigned int hint;
749
750         if (virq >= 0) {
751                 virq = irq_alloc_descs(virq, virq, cnt, node);
752         } else {
753                 hint = hwirq % nr_irqs;
754                 if (hint == 0)
755                         hint++;
756                 virq = irq_alloc_descs_from(hint, cnt, node);
757                 if (virq <= 0 && hint > 1)
758                         virq = irq_alloc_descs_from(1, cnt, node);
759         }
760
761         return virq;
762 }
763
764 #ifdef  CONFIG_IRQ_DOMAIN_HIERARCHY
765 /**
766  * irq_domain_add_hierarchy - Add a irqdomain into the hierarchy
767  * @parent:     Parent irq domain to associate with the new domain
768  * @flags:      Irq domain flags associated to the domain
769  * @size:       Size of the domain. See below
770  * @node:       Optional device-tree node of the interrupt controller
771  * @ops:        Pointer to the interrupt domain callbacks
772  * @host_data:  Controller private data pointer
773  *
774  * If @size is 0 a tree domain is created, otherwise a linear domain.
775  *
776  * If successful the parent is associated to the new domain and the
777  * domain flags are set.
778  * Returns pointer to IRQ domain, or NULL on failure.
779  */
780 struct irq_domain *irq_domain_add_hierarchy(struct irq_domain *parent,
781                                             unsigned int flags,
782                                             unsigned int size,
783                                             struct device_node *node,
784                                             const struct irq_domain_ops *ops,
785                                             void *host_data)
786 {
787         struct irq_domain *domain;
788
789         if (size)
790                 domain = irq_domain_add_linear(node, size, ops, host_data);
791         else
792                 domain = irq_domain_add_tree(node, ops, host_data);
793         if (domain) {
794                 domain->parent = parent;
795                 domain->flags |= flags;
796         }
797
798         return domain;
799 }
800
801 static void irq_domain_insert_irq(int virq)
802 {
803         struct irq_data *data;
804
805         for (data = irq_get_irq_data(virq); data; data = data->parent_data) {
806                 struct irq_domain *domain = data->domain;
807                 irq_hw_number_t hwirq = data->hwirq;
808
809                 if (hwirq < domain->revmap_size) {
810                         domain->linear_revmap[hwirq] = virq;
811                 } else {
812                         mutex_lock(&revmap_trees_mutex);
813                         radix_tree_insert(&domain->revmap_tree, hwirq, data);
814                         mutex_unlock(&revmap_trees_mutex);
815                 }
816
817                 /* If not already assigned, give the domain the chip's name */
818                 if (!domain->name && data->chip)
819                         domain->name = data->chip->name;
820         }
821
822         irq_clear_status_flags(virq, IRQ_NOREQUEST);
823 }
824
825 static void irq_domain_remove_irq(int virq)
826 {
827         struct irq_data *data;
828
829         irq_set_status_flags(virq, IRQ_NOREQUEST);
830         irq_set_chip_and_handler(virq, NULL, NULL);
831         synchronize_irq(virq);
832         smp_mb();
833
834         for (data = irq_get_irq_data(virq); data; data = data->parent_data) {
835                 struct irq_domain *domain = data->domain;
836                 irq_hw_number_t hwirq = data->hwirq;
837
838                 if (hwirq < domain->revmap_size) {
839                         domain->linear_revmap[hwirq] = 0;
840                 } else {
841                         mutex_lock(&revmap_trees_mutex);
842                         radix_tree_delete(&domain->revmap_tree, hwirq);
843                         mutex_unlock(&revmap_trees_mutex);
844                 }
845         }
846 }
847
848 static struct irq_data *irq_domain_insert_irq_data(struct irq_domain *domain,
849                                                    struct irq_data *child)
850 {
851         struct irq_data *irq_data;
852
853         irq_data = kzalloc_node(sizeof(*irq_data), GFP_KERNEL,
854                                 irq_data_get_node(child));
855         if (irq_data) {
856                 child->parent_data = irq_data;
857                 irq_data->irq = child->irq;
858                 irq_data->common = child->common;
859                 irq_data->domain = domain;
860         }
861
862         return irq_data;
863 }
864
865 static void irq_domain_free_irq_data(unsigned int virq, unsigned int nr_irqs)
866 {
867         struct irq_data *irq_data, *tmp;
868         int i;
869
870         for (i = 0; i < nr_irqs; i++) {
871                 irq_data = irq_get_irq_data(virq + i);
872                 tmp = irq_data->parent_data;
873                 irq_data->parent_data = NULL;
874                 irq_data->domain = NULL;
875
876                 while (tmp) {
877                         irq_data = tmp;
878                         tmp = tmp->parent_data;
879                         kfree(irq_data);
880                 }
881         }
882 }
883
884 static int irq_domain_alloc_irq_data(struct irq_domain *domain,
885                                      unsigned int virq, unsigned int nr_irqs)
886 {
887         struct irq_data *irq_data;
888         struct irq_domain *parent;
889         int i;
890
891         /* The outermost irq_data is embedded in struct irq_desc */
892         for (i = 0; i < nr_irqs; i++) {
893                 irq_data = irq_get_irq_data(virq + i);
894                 irq_data->domain = domain;
895
896                 for (parent = domain->parent; parent; parent = parent->parent) {
897                         irq_data = irq_domain_insert_irq_data(parent, irq_data);
898                         if (!irq_data) {
899                                 irq_domain_free_irq_data(virq, i + 1);
900                                 return -ENOMEM;
901                         }
902                 }
903         }
904
905         return 0;
906 }
907
908 /**
909  * irq_domain_get_irq_data - Get irq_data associated with @virq and @domain
910  * @domain:     domain to match
911  * @virq:       IRQ number to get irq_data
912  */
913 struct irq_data *irq_domain_get_irq_data(struct irq_domain *domain,
914                                          unsigned int virq)
915 {
916         struct irq_data *irq_data;
917
918         for (irq_data = irq_get_irq_data(virq); irq_data;
919              irq_data = irq_data->parent_data)
920                 if (irq_data->domain == domain)
921                         return irq_data;
922
923         return NULL;
924 }
925
926 /**
927  * irq_domain_set_hwirq_and_chip - Set hwirq and irqchip of @virq at @domain
928  * @domain:     Interrupt domain to match
929  * @virq:       IRQ number
930  * @hwirq:      The hwirq number
931  * @chip:       The associated interrupt chip
932  * @chip_data:  The associated chip data
933  */
934 int irq_domain_set_hwirq_and_chip(struct irq_domain *domain, unsigned int virq,
935                                   irq_hw_number_t hwirq, struct irq_chip *chip,
936                                   void *chip_data)
937 {
938         struct irq_data *irq_data = irq_domain_get_irq_data(domain, virq);
939
940         if (!irq_data)
941                 return -ENOENT;
942
943         irq_data->hwirq = hwirq;
944         irq_data->chip = chip ? chip : &no_irq_chip;
945         irq_data->chip_data = chip_data;
946
947         return 0;
948 }
949
950 /**
951  * irq_domain_set_info - Set the complete data for a @virq in @domain
952  * @domain:             Interrupt domain to match
953  * @virq:               IRQ number
954  * @hwirq:              The hardware interrupt number
955  * @chip:               The associated interrupt chip
956  * @chip_data:          The associated interrupt chip data
957  * @handler:            The interrupt flow handler
958  * @handler_data:       The interrupt flow handler data
959  * @handler_name:       The interrupt handler name
960  */
961 void irq_domain_set_info(struct irq_domain *domain, unsigned int virq,
962                          irq_hw_number_t hwirq, struct irq_chip *chip,
963                          void *chip_data, irq_flow_handler_t handler,
964                          void *handler_data, const char *handler_name)
965 {
966         irq_domain_set_hwirq_and_chip(domain, virq, hwirq, chip, chip_data);
967         __irq_set_handler(virq, handler, 0, handler_name);
968         irq_set_handler_data(virq, handler_data);
969 }
970
971 /**
972  * irq_domain_reset_irq_data - Clear hwirq, chip and chip_data in @irq_data
973  * @irq_data:   The pointer to irq_data
974  */
975 void irq_domain_reset_irq_data(struct irq_data *irq_data)
976 {
977         irq_data->hwirq = 0;
978         irq_data->chip = &no_irq_chip;
979         irq_data->chip_data = NULL;
980 }
981
982 /**
983  * irq_domain_free_irqs_common - Clear irq_data and free the parent
984  * @domain:     Interrupt domain to match
985  * @virq:       IRQ number to start with
986  * @nr_irqs:    The number of irqs to free
987  */
988 void irq_domain_free_irqs_common(struct irq_domain *domain, unsigned int virq,
989                                  unsigned int nr_irqs)
990 {
991         struct irq_data *irq_data;
992         int i;
993
994         for (i = 0; i < nr_irqs; i++) {
995                 irq_data = irq_domain_get_irq_data(domain, virq + i);
996                 if (irq_data)
997                         irq_domain_reset_irq_data(irq_data);
998         }
999         irq_domain_free_irqs_parent(domain, virq, nr_irqs);
1000 }
1001
1002 /**
1003  * irq_domain_free_irqs_top - Clear handler and handler data, clear irqdata and free parent
1004  * @domain:     Interrupt domain to match
1005  * @virq:       IRQ number to start with
1006  * @nr_irqs:    The number of irqs to free
1007  */
1008 void irq_domain_free_irqs_top(struct irq_domain *domain, unsigned int virq,
1009                               unsigned int nr_irqs)
1010 {
1011         int i;
1012
1013         for (i = 0; i < nr_irqs; i++) {
1014                 irq_set_handler_data(virq + i, NULL);
1015                 irq_set_handler(virq + i, NULL);
1016         }
1017         irq_domain_free_irqs_common(domain, virq, nr_irqs);
1018 }
1019
1020 static bool irq_domain_is_auto_recursive(struct irq_domain *domain)
1021 {
1022         return domain->flags & IRQ_DOMAIN_FLAG_AUTO_RECURSIVE;
1023 }
1024
1025 static void irq_domain_free_irqs_recursive(struct irq_domain *domain,
1026                                            unsigned int irq_base,
1027                                            unsigned int nr_irqs)
1028 {
1029         domain->ops->free(domain, irq_base, nr_irqs);
1030         if (irq_domain_is_auto_recursive(domain)) {
1031                 BUG_ON(!domain->parent);
1032                 irq_domain_free_irqs_recursive(domain->parent, irq_base,
1033                                                nr_irqs);
1034         }
1035 }
1036
1037 static int irq_domain_alloc_irqs_recursive(struct irq_domain *domain,
1038                                            unsigned int irq_base,
1039                                            unsigned int nr_irqs, void *arg)
1040 {
1041         int ret = 0;
1042         struct irq_domain *parent = domain->parent;
1043         bool recursive = irq_domain_is_auto_recursive(domain);
1044
1045         BUG_ON(recursive && !parent);
1046         if (recursive)
1047                 ret = irq_domain_alloc_irqs_recursive(parent, irq_base,
1048                                                       nr_irqs, arg);
1049         if (ret >= 0)
1050                 ret = domain->ops->alloc(domain, irq_base, nr_irqs, arg);
1051         if (ret < 0 && recursive)
1052                 irq_domain_free_irqs_recursive(parent, irq_base, nr_irqs);
1053
1054         return ret;
1055 }
1056
1057 /**
1058  * __irq_domain_alloc_irqs - Allocate IRQs from domain
1059  * @domain:     domain to allocate from
1060  * @irq_base:   allocate specified IRQ nubmer if irq_base >= 0
1061  * @nr_irqs:    number of IRQs to allocate
1062  * @node:       NUMA node id for memory allocation
1063  * @arg:        domain specific argument
1064  * @realloc:    IRQ descriptors have already been allocated if true
1065  *
1066  * Allocate IRQ numbers and initialized all data structures to support
1067  * hierarchy IRQ domains.
1068  * Parameter @realloc is mainly to support legacy IRQs.
1069  * Returns error code or allocated IRQ number
1070  *
1071  * The whole process to setup an IRQ has been split into two steps.
1072  * The first step, __irq_domain_alloc_irqs(), is to allocate IRQ
1073  * descriptor and required hardware resources. The second step,
1074  * irq_domain_activate_irq(), is to program hardwares with preallocated
1075  * resources. In this way, it's easier to rollback when failing to
1076  * allocate resources.
1077  */
1078 int __irq_domain_alloc_irqs(struct irq_domain *domain, int irq_base,
1079                             unsigned int nr_irqs, int node, void *arg,
1080                             bool realloc)
1081 {
1082         int i, ret, virq;
1083
1084         if (domain == NULL) {
1085                 domain = irq_default_domain;
1086                 if (WARN(!domain, "domain is NULL; cannot allocate IRQ\n"))
1087                         return -EINVAL;
1088         }
1089
1090         if (!domain->ops->alloc) {
1091                 pr_debug("domain->ops->alloc() is NULL\n");
1092                 return -ENOSYS;
1093         }
1094
1095         if (realloc && irq_base >= 0) {
1096                 virq = irq_base;
1097         } else {
1098                 virq = irq_domain_alloc_descs(irq_base, nr_irqs, 0, node);
1099                 if (virq < 0) {
1100                         pr_debug("cannot allocate IRQ(base %d, count %d)\n",
1101                                  irq_base, nr_irqs);
1102                         return virq;
1103                 }
1104         }
1105
1106         if (irq_domain_alloc_irq_data(domain, virq, nr_irqs)) {
1107                 pr_debug("cannot allocate memory for IRQ%d\n", virq);
1108                 ret = -ENOMEM;
1109                 goto out_free_desc;
1110         }
1111
1112         mutex_lock(&irq_domain_mutex);
1113         ret = irq_domain_alloc_irqs_recursive(domain, virq, nr_irqs, arg);
1114         if (ret < 0) {
1115                 mutex_unlock(&irq_domain_mutex);
1116                 goto out_free_irq_data;
1117         }
1118         for (i = 0; i < nr_irqs; i++)
1119                 irq_domain_insert_irq(virq + i);
1120         mutex_unlock(&irq_domain_mutex);
1121
1122         return virq;
1123
1124 out_free_irq_data:
1125         irq_domain_free_irq_data(virq, nr_irqs);
1126 out_free_desc:
1127         irq_free_descs(virq, nr_irqs);
1128         return ret;
1129 }
1130
1131 /**
1132  * irq_domain_free_irqs - Free IRQ number and associated data structures
1133  * @virq:       base IRQ number
1134  * @nr_irqs:    number of IRQs to free
1135  */
1136 void irq_domain_free_irqs(unsigned int virq, unsigned int nr_irqs)
1137 {
1138         struct irq_data *data = irq_get_irq_data(virq);
1139         int i;
1140
1141         if (WARN(!data || !data->domain || !data->domain->ops->free,
1142                  "NULL pointer, cannot free irq\n"))
1143                 return;
1144
1145         mutex_lock(&irq_domain_mutex);
1146         for (i = 0; i < nr_irqs; i++)
1147                 irq_domain_remove_irq(virq + i);
1148         irq_domain_free_irqs_recursive(data->domain, virq, nr_irqs);
1149         mutex_unlock(&irq_domain_mutex);
1150
1151         irq_domain_free_irq_data(virq, nr_irqs);
1152         irq_free_descs(virq, nr_irqs);
1153 }
1154
1155 /**
1156  * irq_domain_alloc_irqs_parent - Allocate interrupts from parent domain
1157  * @irq_base:   Base IRQ number
1158  * @nr_irqs:    Number of IRQs to allocate
1159  * @arg:        Allocation data (arch/domain specific)
1160  *
1161  * Check whether the domain has been setup recursive. If not allocate
1162  * through the parent domain.
1163  */
1164 int irq_domain_alloc_irqs_parent(struct irq_domain *domain,
1165                                  unsigned int irq_base, unsigned int nr_irqs,
1166                                  void *arg)
1167 {
1168         /* irq_domain_alloc_irqs_recursive() has called parent's alloc() */
1169         if (irq_domain_is_auto_recursive(domain))
1170                 return 0;
1171
1172         domain = domain->parent;
1173         if (domain)
1174                 return irq_domain_alloc_irqs_recursive(domain, irq_base,
1175                                                        nr_irqs, arg);
1176         return -ENOSYS;
1177 }
1178
1179 /**
1180  * irq_domain_free_irqs_parent - Free interrupts from parent domain
1181  * @irq_base:   Base IRQ number
1182  * @nr_irqs:    Number of IRQs to free
1183  *
1184  * Check whether the domain has been setup recursive. If not free
1185  * through the parent domain.
1186  */
1187 void irq_domain_free_irqs_parent(struct irq_domain *domain,
1188                                  unsigned int irq_base, unsigned int nr_irqs)
1189 {
1190         /* irq_domain_free_irqs_recursive() will call parent's free */
1191         if (!irq_domain_is_auto_recursive(domain) && domain->parent)
1192                 irq_domain_free_irqs_recursive(domain->parent, irq_base,
1193                                                nr_irqs);
1194 }
1195
1196 /**
1197  * irq_domain_activate_irq - Call domain_ops->activate recursively to activate
1198  *                           interrupt
1199  * @irq_data:   outermost irq_data associated with interrupt
1200  *
1201  * This is the second step to call domain_ops->activate to program interrupt
1202  * controllers, so the interrupt could actually get delivered.
1203  */
1204 void irq_domain_activate_irq(struct irq_data *irq_data)
1205 {
1206         if (irq_data && irq_data->domain) {
1207                 struct irq_domain *domain = irq_data->domain;
1208
1209                 if (irq_data->parent_data)
1210                         irq_domain_activate_irq(irq_data->parent_data);
1211                 if (domain->ops->activate)
1212                         domain->ops->activate(domain, irq_data);
1213         }
1214 }
1215
1216 /**
1217  * irq_domain_deactivate_irq - Call domain_ops->deactivate recursively to
1218  *                             deactivate interrupt
1219  * @irq_data: outermost irq_data associated with interrupt
1220  *
1221  * It calls domain_ops->deactivate to program interrupt controllers to disable
1222  * interrupt delivery.
1223  */
1224 void irq_domain_deactivate_irq(struct irq_data *irq_data)
1225 {
1226         if (irq_data && irq_data->domain) {
1227                 struct irq_domain *domain = irq_data->domain;
1228
1229                 if (domain->ops->deactivate)
1230                         domain->ops->deactivate(domain, irq_data);
1231                 if (irq_data->parent_data)
1232                         irq_domain_deactivate_irq(irq_data->parent_data);
1233         }
1234 }
1235
1236 static void irq_domain_check_hierarchy(struct irq_domain *domain)
1237 {
1238         /* Hierarchy irq_domains must implement callback alloc() */
1239         if (domain->ops->alloc)
1240                 domain->flags |= IRQ_DOMAIN_FLAG_HIERARCHY;
1241 }
1242 #else   /* CONFIG_IRQ_DOMAIN_HIERARCHY */
1243 /**
1244  * irq_domain_get_irq_data - Get irq_data associated with @virq and @domain
1245  * @domain:     domain to match
1246  * @virq:       IRQ number to get irq_data
1247  */
1248 struct irq_data *irq_domain_get_irq_data(struct irq_domain *domain,
1249                                          unsigned int virq)
1250 {
1251         struct irq_data *irq_data = irq_get_irq_data(virq);
1252
1253         return (irq_data && irq_data->domain == domain) ? irq_data : NULL;
1254 }
1255
1256 /**
1257  * irq_domain_set_info - Set the complete data for a @virq in @domain
1258  * @domain:             Interrupt domain to match
1259  * @virq:               IRQ number
1260  * @hwirq:              The hardware interrupt number
1261  * @chip:               The associated interrupt chip
1262  * @chip_data:          The associated interrupt chip data
1263  * @handler:            The interrupt flow handler
1264  * @handler_data:       The interrupt flow handler data
1265  * @handler_name:       The interrupt handler name
1266  */
1267 void irq_domain_set_info(struct irq_domain *domain, unsigned int virq,
1268                          irq_hw_number_t hwirq, struct irq_chip *chip,
1269                          void *chip_data, irq_flow_handler_t handler,
1270                          void *handler_data, const char *handler_name)
1271 {
1272         irq_set_chip_and_handler_name(virq, chip, handler, handler_name);
1273         irq_set_chip_data(virq, chip_data);
1274         irq_set_handler_data(virq, handler_data);
1275 }
1276
1277 static void irq_domain_check_hierarchy(struct irq_domain *domain)
1278 {
1279 }
1280 #endif  /* CONFIG_IRQ_DOMAIN_HIERARCHY */