genirq: Introduce helper irq_domain_set_info() to reduce duplicated code
[firefly-linux-kernel-4.4.55.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(domain->of_node);
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_host() - Locates a domain for a given device node
191  * @node: device-tree node of the interrupt controller
192  */
193 struct irq_domain *irq_find_host(struct device_node *node)
194 {
195         struct irq_domain *h, *found = NULL;
196         int rc;
197
198         /* We might want to match the legacy controller last since
199          * it might potentially be set to match all interrupts in
200          * the absence of a device node. This isn't a problem so far
201          * yet though...
202          */
203         mutex_lock(&irq_domain_mutex);
204         list_for_each_entry(h, &irq_domain_list, link) {
205                 if (h->ops->match)
206                         rc = h->ops->match(h, node);
207                 else
208                         rc = (h->of_node != NULL) && (h->of_node == node);
209
210                 if (rc) {
211                         found = h;
212                         break;
213                 }
214         }
215         mutex_unlock(&irq_domain_mutex);
216         return found;
217 }
218 EXPORT_SYMBOL_GPL(irq_find_host);
219
220 /**
221  * irq_set_default_host() - Set a "default" irq domain
222  * @domain: default domain pointer
223  *
224  * For convenience, it's possible to set a "default" domain that will be used
225  * whenever NULL is passed to irq_create_mapping(). It makes life easier for
226  * platforms that want to manipulate a few hard coded interrupt numbers that
227  * aren't properly represented in the device-tree.
228  */
229 void irq_set_default_host(struct irq_domain *domain)
230 {
231         pr_debug("Default domain set to @0x%p\n", domain);
232
233         irq_default_domain = domain;
234 }
235 EXPORT_SYMBOL_GPL(irq_set_default_host);
236
237 void irq_domain_disassociate(struct irq_domain *domain, unsigned int irq)
238 {
239         struct irq_data *irq_data = irq_get_irq_data(irq);
240         irq_hw_number_t hwirq;
241
242         if (WARN(!irq_data || irq_data->domain != domain,
243                  "virq%i doesn't exist; cannot disassociate\n", irq))
244                 return;
245
246         hwirq = irq_data->hwirq;
247         irq_set_status_flags(irq, IRQ_NOREQUEST);
248
249         /* remove chip and handler */
250         irq_set_chip_and_handler(irq, NULL, NULL);
251
252         /* Make sure it's completed */
253         synchronize_irq(irq);
254
255         /* Tell the PIC about it */
256         if (domain->ops->unmap)
257                 domain->ops->unmap(domain, irq);
258         smp_mb();
259
260         irq_data->domain = NULL;
261         irq_data->hwirq = 0;
262
263         /* Clear reverse map for this hwirq */
264         if (hwirq < domain->revmap_size) {
265                 domain->linear_revmap[hwirq] = 0;
266         } else {
267                 mutex_lock(&revmap_trees_mutex);
268                 radix_tree_delete(&domain->revmap_tree, hwirq);
269                 mutex_unlock(&revmap_trees_mutex);
270         }
271 }
272
273 int irq_domain_associate(struct irq_domain *domain, unsigned int virq,
274                          irq_hw_number_t hwirq)
275 {
276         struct irq_data *irq_data = irq_get_irq_data(virq);
277         int ret;
278
279         if (WARN(hwirq >= domain->hwirq_max,
280                  "error: hwirq 0x%x is too large for %s\n", (int)hwirq, domain->name))
281                 return -EINVAL;
282         if (WARN(!irq_data, "error: virq%i is not allocated", virq))
283                 return -EINVAL;
284         if (WARN(irq_data->domain, "error: virq%i is already associated", virq))
285                 return -EINVAL;
286
287         mutex_lock(&irq_domain_mutex);
288         irq_data->hwirq = hwirq;
289         irq_data->domain = domain;
290         if (domain->ops->map) {
291                 ret = domain->ops->map(domain, virq, hwirq);
292                 if (ret != 0) {
293                         /*
294                          * If map() returns -EPERM, this interrupt is protected
295                          * by the firmware or some other service and shall not
296                          * be mapped. Don't bother telling the user about it.
297                          */
298                         if (ret != -EPERM) {
299                                 pr_info("%s didn't like hwirq-0x%lx to VIRQ%i mapping (rc=%d)\n",
300                                        domain->name, hwirq, virq, ret);
301                         }
302                         irq_data->domain = NULL;
303                         irq_data->hwirq = 0;
304                         mutex_unlock(&irq_domain_mutex);
305                         return ret;
306                 }
307
308                 /* If not already assigned, give the domain the chip's name */
309                 if (!domain->name && irq_data->chip)
310                         domain->name = irq_data->chip->name;
311         }
312
313         if (hwirq < domain->revmap_size) {
314                 domain->linear_revmap[hwirq] = virq;
315         } else {
316                 mutex_lock(&revmap_trees_mutex);
317                 radix_tree_insert(&domain->revmap_tree, hwirq, irq_data);
318                 mutex_unlock(&revmap_trees_mutex);
319         }
320         mutex_unlock(&irq_domain_mutex);
321
322         irq_clear_status_flags(virq, IRQ_NOREQUEST);
323
324         return 0;
325 }
326 EXPORT_SYMBOL_GPL(irq_domain_associate);
327
328 void irq_domain_associate_many(struct irq_domain *domain, unsigned int irq_base,
329                                irq_hw_number_t hwirq_base, int count)
330 {
331         int i;
332
333         pr_debug("%s(%s, irqbase=%i, hwbase=%i, count=%i)\n", __func__,
334                 of_node_full_name(domain->of_node), irq_base, (int)hwirq_base, count);
335
336         for (i = 0; i < count; i++) {
337                 irq_domain_associate(domain, irq_base + i, hwirq_base + i);
338         }
339 }
340 EXPORT_SYMBOL_GPL(irq_domain_associate_many);
341
342 /**
343  * irq_create_direct_mapping() - Allocate an irq for direct mapping
344  * @domain: domain to allocate the irq for or NULL for default domain
345  *
346  * This routine is used for irq controllers which can choose the hardware
347  * interrupt numbers they generate. In such a case it's simplest to use
348  * the linux irq as the hardware interrupt number. It still uses the linear
349  * or radix tree to store the mapping, but the irq controller can optimize
350  * the revmap path by using the hwirq directly.
351  */
352 unsigned int irq_create_direct_mapping(struct irq_domain *domain)
353 {
354         unsigned int virq;
355
356         if (domain == NULL)
357                 domain = irq_default_domain;
358
359         virq = irq_alloc_desc_from(1, of_node_to_nid(domain->of_node));
360         if (!virq) {
361                 pr_debug("create_direct virq allocation failed\n");
362                 return 0;
363         }
364         if (virq >= domain->revmap_direct_max_irq) {
365                 pr_err("ERROR: no free irqs available below %i maximum\n",
366                         domain->revmap_direct_max_irq);
367                 irq_free_desc(virq);
368                 return 0;
369         }
370         pr_debug("create_direct obtained virq %d\n", virq);
371
372         if (irq_domain_associate(domain, virq, virq)) {
373                 irq_free_desc(virq);
374                 return 0;
375         }
376
377         return virq;
378 }
379 EXPORT_SYMBOL_GPL(irq_create_direct_mapping);
380
381 /**
382  * irq_create_mapping() - Map a hardware interrupt into linux irq space
383  * @domain: domain owning this hardware interrupt or NULL for default domain
384  * @hwirq: hardware irq number in that domain space
385  *
386  * Only one mapping per hardware interrupt is permitted. Returns a linux
387  * irq number.
388  * If the sense/trigger is to be specified, set_irq_type() should be called
389  * on the number returned from that call.
390  */
391 unsigned int irq_create_mapping(struct irq_domain *domain,
392                                 irq_hw_number_t hwirq)
393 {
394         int virq;
395
396         pr_debug("irq_create_mapping(0x%p, 0x%lx)\n", domain, hwirq);
397
398         /* Look for default domain if nececssary */
399         if (domain == NULL)
400                 domain = irq_default_domain;
401         if (domain == NULL) {
402                 WARN(1, "%s(, %lx) called with NULL domain\n", __func__, hwirq);
403                 return 0;
404         }
405         pr_debug("-> using domain @%p\n", domain);
406
407         /* Check if mapping already exists */
408         virq = irq_find_mapping(domain, hwirq);
409         if (virq) {
410                 pr_debug("-> existing mapping on virq %d\n", virq);
411                 return virq;
412         }
413
414         /* Allocate a virtual interrupt number */
415         virq = irq_domain_alloc_descs(-1, 1, hwirq,
416                                       of_node_to_nid(domain->of_node));
417         if (virq <= 0) {
418                 pr_debug("-> virq allocation failed\n");
419                 return 0;
420         }
421
422         if (irq_domain_associate(domain, virq, hwirq)) {
423                 irq_free_desc(virq);
424                 return 0;
425         }
426
427         pr_debug("irq %lu on domain %s mapped to virtual irq %u\n",
428                 hwirq, of_node_full_name(domain->of_node), virq);
429
430         return virq;
431 }
432 EXPORT_SYMBOL_GPL(irq_create_mapping);
433
434 /**
435  * irq_create_strict_mappings() - Map a range of hw irqs to fixed linux irqs
436  * @domain: domain owning the interrupt range
437  * @irq_base: beginning of linux IRQ range
438  * @hwirq_base: beginning of hardware IRQ range
439  * @count: Number of interrupts to map
440  *
441  * This routine is used for allocating and mapping a range of hardware
442  * irqs to linux irqs where the linux irq numbers are at pre-defined
443  * locations. For use by controllers that already have static mappings
444  * to insert in to the domain.
445  *
446  * Non-linear users can use irq_create_identity_mapping() for IRQ-at-a-time
447  * domain insertion.
448  *
449  * 0 is returned upon success, while any failure to establish a static
450  * mapping is treated as an error.
451  */
452 int irq_create_strict_mappings(struct irq_domain *domain, unsigned int irq_base,
453                                irq_hw_number_t hwirq_base, int count)
454 {
455         int ret;
456
457         ret = irq_alloc_descs(irq_base, irq_base, count,
458                               of_node_to_nid(domain->of_node));
459         if (unlikely(ret < 0))
460                 return ret;
461
462         irq_domain_associate_many(domain, irq_base, hwirq_base, count);
463         return 0;
464 }
465 EXPORT_SYMBOL_GPL(irq_create_strict_mappings);
466
467 unsigned int irq_create_of_mapping(struct of_phandle_args *irq_data)
468 {
469         struct irq_domain *domain;
470         irq_hw_number_t hwirq;
471         unsigned int type = IRQ_TYPE_NONE;
472         int virq;
473
474         domain = irq_data->np ? irq_find_host(irq_data->np) : irq_default_domain;
475         if (!domain) {
476                 pr_warn("no irq domain found for %s !\n",
477                         of_node_full_name(irq_data->np));
478                 return 0;
479         }
480
481         /* If domain has no translation, then we assume interrupt line */
482         if (domain->ops->xlate == NULL)
483                 hwirq = irq_data->args[0];
484         else {
485                 if (domain->ops->xlate(domain, irq_data->np, irq_data->args,
486                                         irq_data->args_count, &hwirq, &type))
487                         return 0;
488         }
489
490         if (irq_domain_is_hierarchy(domain)) {
491                 /*
492                  * If we've already configured this interrupt,
493                  * don't do it again, or hell will break loose.
494                  */
495                 virq = irq_find_mapping(domain, hwirq);
496                 if (virq)
497                         return virq;
498
499                 virq = irq_domain_alloc_irqs(domain, 1, NUMA_NO_NODE, irq_data);
500                 if (virq <= 0)
501                         return 0;
502         } else {
503                 /* Create mapping */
504                 virq = irq_create_mapping(domain, hwirq);
505                 if (!virq)
506                         return virq;
507         }
508
509         /* Set type if specified and different than the current one */
510         if (type != IRQ_TYPE_NONE &&
511             type != irq_get_trigger_type(virq))
512                 irq_set_irq_type(virq, type);
513         return virq;
514 }
515 EXPORT_SYMBOL_GPL(irq_create_of_mapping);
516
517 /**
518  * irq_dispose_mapping() - Unmap an interrupt
519  * @virq: linux irq number of the interrupt to unmap
520  */
521 void irq_dispose_mapping(unsigned int virq)
522 {
523         struct irq_data *irq_data = irq_get_irq_data(virq);
524         struct irq_domain *domain;
525
526         if (!virq || !irq_data)
527                 return;
528
529         domain = irq_data->domain;
530         if (WARN_ON(domain == NULL))
531                 return;
532
533         irq_domain_disassociate(domain, virq);
534         irq_free_desc(virq);
535 }
536 EXPORT_SYMBOL_GPL(irq_dispose_mapping);
537
538 /**
539  * irq_find_mapping() - Find a linux irq from an hw irq number.
540  * @domain: domain owning this hardware interrupt
541  * @hwirq: hardware irq number in that domain space
542  */
543 unsigned int irq_find_mapping(struct irq_domain *domain,
544                               irq_hw_number_t hwirq)
545 {
546         struct irq_data *data;
547
548         /* Look for default domain if nececssary */
549         if (domain == NULL)
550                 domain = irq_default_domain;
551         if (domain == NULL)
552                 return 0;
553
554         if (hwirq < domain->revmap_direct_max_irq) {
555                 data = irq_domain_get_irq_data(domain, hwirq);
556                 if (data && data->hwirq == hwirq)
557                         return hwirq;
558         }
559
560         /* Check if the hwirq is in the linear revmap. */
561         if (hwirq < domain->revmap_size)
562                 return domain->linear_revmap[hwirq];
563
564         rcu_read_lock();
565         data = radix_tree_lookup(&domain->revmap_tree, hwirq);
566         rcu_read_unlock();
567         return data ? data->irq : 0;
568 }
569 EXPORT_SYMBOL_GPL(irq_find_mapping);
570
571 #ifdef CONFIG_IRQ_DOMAIN_DEBUG
572 static int virq_debug_show(struct seq_file *m, void *private)
573 {
574         unsigned long flags;
575         struct irq_desc *desc;
576         struct irq_domain *domain;
577         struct radix_tree_iter iter;
578         void *data, **slot;
579         int i;
580
581         seq_printf(m, " %-16s  %-6s  %-10s  %-10s  %s\n",
582                    "name", "mapped", "linear-max", "direct-max", "devtree-node");
583         mutex_lock(&irq_domain_mutex);
584         list_for_each_entry(domain, &irq_domain_list, link) {
585                 int count = 0;
586                 radix_tree_for_each_slot(slot, &domain->revmap_tree, &iter, 0)
587                         count++;
588                 seq_printf(m, "%c%-16s  %6u  %10u  %10u  %s\n",
589                            domain == irq_default_domain ? '*' : ' ', domain->name,
590                            domain->revmap_size + count, domain->revmap_size,
591                            domain->revmap_direct_max_irq,
592                            domain->of_node ? of_node_full_name(domain->of_node) : "");
593         }
594         mutex_unlock(&irq_domain_mutex);
595
596         seq_printf(m, "%-5s  %-7s  %-15s  %-*s  %6s  %-14s  %s\n", "irq", "hwirq",
597                       "chip name", (int)(2 * sizeof(void *) + 2), "chip data",
598                       "active", "type", "domain");
599
600         for (i = 1; i < nr_irqs; i++) {
601                 desc = irq_to_desc(i);
602                 if (!desc)
603                         continue;
604
605                 raw_spin_lock_irqsave(&desc->lock, flags);
606                 domain = desc->irq_data.domain;
607
608                 if (domain) {
609                         struct irq_chip *chip;
610                         int hwirq = desc->irq_data.hwirq;
611                         bool direct;
612
613                         seq_printf(m, "%5d  ", i);
614                         seq_printf(m, "0x%05x  ", hwirq);
615
616                         chip = irq_desc_get_chip(desc);
617                         seq_printf(m, "%-15s  ", (chip && chip->name) ? chip->name : "none");
618
619                         data = irq_desc_get_chip_data(desc);
620                         seq_printf(m, data ? "0x%p  " : "  %p  ", data);
621
622                         seq_printf(m, "   %c    ", (desc->action && desc->action->handler) ? '*' : ' ');
623                         direct = (i == hwirq) && (i < domain->revmap_direct_max_irq);
624                         seq_printf(m, "%6s%-8s  ",
625                                    (hwirq < domain->revmap_size) ? "LINEAR" : "RADIX",
626                                    direct ? "(DIRECT)" : "");
627                         seq_printf(m, "%s\n", desc->irq_data.domain->name);
628                 }
629
630                 raw_spin_unlock_irqrestore(&desc->lock, flags);
631         }
632
633         return 0;
634 }
635
636 static int virq_debug_open(struct inode *inode, struct file *file)
637 {
638         return single_open(file, virq_debug_show, inode->i_private);
639 }
640
641 static const struct file_operations virq_debug_fops = {
642         .open = virq_debug_open,
643         .read = seq_read,
644         .llseek = seq_lseek,
645         .release = single_release,
646 };
647
648 static int __init irq_debugfs_init(void)
649 {
650         if (debugfs_create_file("irq_domain_mapping", S_IRUGO, NULL,
651                                  NULL, &virq_debug_fops) == NULL)
652                 return -ENOMEM;
653
654         return 0;
655 }
656 __initcall(irq_debugfs_init);
657 #endif /* CONFIG_IRQ_DOMAIN_DEBUG */
658
659 /**
660  * irq_domain_xlate_onecell() - Generic xlate for direct one cell bindings
661  *
662  * Device Tree IRQ specifier translation function which works with one cell
663  * bindings where the cell value maps directly to the hwirq number.
664  */
665 int irq_domain_xlate_onecell(struct irq_domain *d, struct device_node *ctrlr,
666                              const u32 *intspec, unsigned int intsize,
667                              unsigned long *out_hwirq, unsigned int *out_type)
668 {
669         if (WARN_ON(intsize < 1))
670                 return -EINVAL;
671         *out_hwirq = intspec[0];
672         *out_type = IRQ_TYPE_NONE;
673         return 0;
674 }
675 EXPORT_SYMBOL_GPL(irq_domain_xlate_onecell);
676
677 /**
678  * irq_domain_xlate_twocell() - Generic xlate for direct two cell bindings
679  *
680  * Device Tree IRQ specifier translation function which works with two cell
681  * bindings where the cell values map directly to the hwirq number
682  * and linux irq flags.
683  */
684 int irq_domain_xlate_twocell(struct irq_domain *d, struct device_node *ctrlr,
685                         const u32 *intspec, unsigned int intsize,
686                         irq_hw_number_t *out_hwirq, unsigned int *out_type)
687 {
688         if (WARN_ON(intsize < 2))
689                 return -EINVAL;
690         *out_hwirq = intspec[0];
691         *out_type = intspec[1] & IRQ_TYPE_SENSE_MASK;
692         return 0;
693 }
694 EXPORT_SYMBOL_GPL(irq_domain_xlate_twocell);
695
696 /**
697  * irq_domain_xlate_onetwocell() - Generic xlate for one or two cell bindings
698  *
699  * Device Tree IRQ specifier translation function which works with either one
700  * or two cell bindings where the cell values map directly to the hwirq number
701  * and linux irq flags.
702  *
703  * Note: don't use this function unless your interrupt controller explicitly
704  * supports both one and two cell bindings.  For the majority of controllers
705  * the _onecell() or _twocell() variants above should be used.
706  */
707 int irq_domain_xlate_onetwocell(struct irq_domain *d,
708                                 struct device_node *ctrlr,
709                                 const u32 *intspec, unsigned int intsize,
710                                 unsigned long *out_hwirq, unsigned int *out_type)
711 {
712         if (WARN_ON(intsize < 1))
713                 return -EINVAL;
714         *out_hwirq = intspec[0];
715         *out_type = (intsize > 1) ? intspec[1] : IRQ_TYPE_NONE;
716         return 0;
717 }
718 EXPORT_SYMBOL_GPL(irq_domain_xlate_onetwocell);
719
720 const struct irq_domain_ops irq_domain_simple_ops = {
721         .xlate = irq_domain_xlate_onetwocell,
722 };
723 EXPORT_SYMBOL_GPL(irq_domain_simple_ops);
724
725 static int irq_domain_alloc_descs(int virq, unsigned int cnt,
726                                   irq_hw_number_t hwirq, int node)
727 {
728         unsigned int hint;
729
730         if (virq >= 0) {
731                 virq = irq_alloc_descs(virq, virq, cnt, node);
732         } else {
733                 hint = hwirq % nr_irqs;
734                 if (hint == 0)
735                         hint++;
736                 virq = irq_alloc_descs_from(hint, cnt, node);
737                 if (virq <= 0 && hint > 1)
738                         virq = irq_alloc_descs_from(1, cnt, node);
739         }
740
741         return virq;
742 }
743
744 #ifdef  CONFIG_IRQ_DOMAIN_HIERARCHY
745 static void irq_domain_insert_irq(int virq)
746 {
747         struct irq_data *data;
748
749         for (data = irq_get_irq_data(virq); data; data = data->parent_data) {
750                 struct irq_domain *domain = data->domain;
751                 irq_hw_number_t hwirq = data->hwirq;
752
753                 if (hwirq < domain->revmap_size) {
754                         domain->linear_revmap[hwirq] = virq;
755                 } else {
756                         mutex_lock(&revmap_trees_mutex);
757                         radix_tree_insert(&domain->revmap_tree, hwirq, data);
758                         mutex_unlock(&revmap_trees_mutex);
759                 }
760
761                 /* If not already assigned, give the domain the chip's name */
762                 if (!domain->name && data->chip)
763                         domain->name = data->chip->name;
764         }
765
766         irq_clear_status_flags(virq, IRQ_NOREQUEST);
767 }
768
769 static void irq_domain_remove_irq(int virq)
770 {
771         struct irq_data *data;
772
773         irq_set_status_flags(virq, IRQ_NOREQUEST);
774         irq_set_chip_and_handler(virq, NULL, NULL);
775         synchronize_irq(virq);
776         smp_mb();
777
778         for (data = irq_get_irq_data(virq); data; data = data->parent_data) {
779                 struct irq_domain *domain = data->domain;
780                 irq_hw_number_t hwirq = data->hwirq;
781
782                 if (hwirq < domain->revmap_size) {
783                         domain->linear_revmap[hwirq] = 0;
784                 } else {
785                         mutex_lock(&revmap_trees_mutex);
786                         radix_tree_delete(&domain->revmap_tree, hwirq);
787                         mutex_unlock(&revmap_trees_mutex);
788                 }
789         }
790 }
791
792 static struct irq_data *irq_domain_insert_irq_data(struct irq_domain *domain,
793                                                    struct irq_data *child)
794 {
795         struct irq_data *irq_data;
796
797         irq_data = kzalloc_node(sizeof(*irq_data), GFP_KERNEL, child->node);
798         if (irq_data) {
799                 child->parent_data = irq_data;
800                 irq_data->irq = child->irq;
801                 irq_data->node = child->node;
802                 irq_data->domain = domain;
803         }
804
805         return irq_data;
806 }
807
808 static void irq_domain_free_irq_data(unsigned int virq, unsigned int nr_irqs)
809 {
810         struct irq_data *irq_data, *tmp;
811         int i;
812
813         for (i = 0; i < nr_irqs; i++) {
814                 irq_data = irq_get_irq_data(virq + i);
815                 tmp = irq_data->parent_data;
816                 irq_data->parent_data = NULL;
817                 irq_data->domain = NULL;
818
819                 while (tmp) {
820                         irq_data = tmp;
821                         tmp = tmp->parent_data;
822                         kfree(irq_data);
823                 }
824         }
825 }
826
827 static int irq_domain_alloc_irq_data(struct irq_domain *domain,
828                                      unsigned int virq, unsigned int nr_irqs)
829 {
830         struct irq_data *irq_data;
831         struct irq_domain *parent;
832         int i;
833
834         /* The outermost irq_data is embedded in struct irq_desc */
835         for (i = 0; i < nr_irqs; i++) {
836                 irq_data = irq_get_irq_data(virq + i);
837                 irq_data->domain = domain;
838
839                 for (parent = domain->parent; parent; parent = parent->parent) {
840                         irq_data = irq_domain_insert_irq_data(parent, irq_data);
841                         if (!irq_data) {
842                                 irq_domain_free_irq_data(virq, i + 1);
843                                 return -ENOMEM;
844                         }
845                 }
846         }
847
848         return 0;
849 }
850
851 /**
852  * irq_domain_get_irq_data - Get irq_data associated with @virq and @domain
853  * @domain:     domain to match
854  * @virq:       IRQ number to get irq_data
855  */
856 struct irq_data *irq_domain_get_irq_data(struct irq_domain *domain,
857                                          unsigned int virq)
858 {
859         struct irq_data *irq_data;
860
861         for (irq_data = irq_get_irq_data(virq); irq_data;
862              irq_data = irq_data->parent_data)
863                 if (irq_data->domain == domain)
864                         return irq_data;
865
866         return NULL;
867 }
868
869 /**
870  * irq_domain_set_hwirq_and_chip - Set hwirq and irqchip of @virq at @domain
871  * @domain:     Interrupt domain to match
872  * @virq:       IRQ number
873  * @hwirq:      The hwirq number
874  * @chip:       The associated interrupt chip
875  * @chip_data:  The associated chip data
876  */
877 int irq_domain_set_hwirq_and_chip(struct irq_domain *domain, unsigned int virq,
878                                   irq_hw_number_t hwirq, struct irq_chip *chip,
879                                   void *chip_data)
880 {
881         struct irq_data *irq_data = irq_domain_get_irq_data(domain, virq);
882
883         if (!irq_data)
884                 return -ENOENT;
885
886         irq_data->hwirq = hwirq;
887         irq_data->chip = chip ? chip : &no_irq_chip;
888         irq_data->chip_data = chip_data;
889
890         return 0;
891 }
892
893 /**
894  * irq_domain_set_info - Set the complete data for a @virq in @domain
895  * @domain:             Interrupt domain to match
896  * @virq:               IRQ number
897  * @hwirq:              The hardware interrupt number
898  * @chip:               The associated interrupt chip
899  * @chip_data:          The associated interrupt chip data
900  * @handler:            The interrupt flow handler
901  * @handler_data:       The interrupt flow handler data
902  * @handler_name:       The interrupt handler name
903  */
904 void irq_domain_set_info(struct irq_domain *domain, unsigned int virq,
905                          irq_hw_number_t hwirq, struct irq_chip *chip,
906                          void *chip_data, irq_flow_handler_t handler,
907                          void *handler_data, const char *handler_name)
908 {
909         irq_domain_set_hwirq_and_chip(domain, virq, hwirq, chip, chip_data);
910         __irq_set_handler(virq, handler, 0, handler_name);
911         irq_set_handler_data(virq, handler_data);
912 }
913
914 /**
915  * irq_domain_reset_irq_data - Clear hwirq, chip and chip_data in @irq_data
916  * @irq_data:   The pointer to irq_data
917  */
918 void irq_domain_reset_irq_data(struct irq_data *irq_data)
919 {
920         irq_data->hwirq = 0;
921         irq_data->chip = &no_irq_chip;
922         irq_data->chip_data = NULL;
923 }
924
925 /**
926  * irq_domain_free_irqs_common - Clear irq_data and free the parent
927  * @domain:     Interrupt domain to match
928  * @virq:       IRQ number to start with
929  * @nr_irqs:    The number of irqs to free
930  */
931 void irq_domain_free_irqs_common(struct irq_domain *domain, unsigned int virq,
932                                  unsigned int nr_irqs)
933 {
934         struct irq_data *irq_data;
935         int i;
936
937         for (i = 0; i < nr_irqs; i++) {
938                 irq_data = irq_domain_get_irq_data(domain, virq + i);
939                 if (irq_data)
940                         irq_domain_reset_irq_data(irq_data);
941         }
942         irq_domain_free_irqs_parent(domain, virq, nr_irqs);
943 }
944
945 /**
946  * irq_domain_free_irqs_top - Clear handler and handler data, clear irqdata and free parent
947  * @domain:     Interrupt domain to match
948  * @virq:       IRQ number to start with
949  * @nr_irqs:    The number of irqs to free
950  */
951 void irq_domain_free_irqs_top(struct irq_domain *domain, unsigned int virq,
952                               unsigned int nr_irqs)
953 {
954         int i;
955
956         for (i = 0; i < nr_irqs; i++) {
957                 irq_set_handler_data(virq + i, NULL);
958                 irq_set_handler(virq + i, NULL);
959         }
960         irq_domain_free_irqs_common(domain, virq, nr_irqs);
961 }
962
963 /**
964  * __irq_domain_alloc_irqs - Allocate IRQs from domain
965  * @domain:     domain to allocate from
966  * @irq_base:   allocate specified IRQ nubmer if irq_base >= 0
967  * @nr_irqs:    number of IRQs to allocate
968  * @node:       NUMA node id for memory allocation
969  * @arg:        domain specific argument
970  * @realloc:    IRQ descriptors have already been allocated if true
971  *
972  * Allocate IRQ numbers and initialized all data structures to support
973  * hierarchy IRQ domains.
974  * Parameter @realloc is mainly to support legacy IRQs.
975  * Returns error code or allocated IRQ number
976  *
977  * The whole process to setup an IRQ has been split into two steps.
978  * The first step, __irq_domain_alloc_irqs(), is to allocate IRQ
979  * descriptor and required hardware resources. The second step,
980  * irq_domain_activate_irq(), is to program hardwares with preallocated
981  * resources. In this way, it's easier to rollback when failing to
982  * allocate resources.
983  */
984 int __irq_domain_alloc_irqs(struct irq_domain *domain, int irq_base,
985                             unsigned int nr_irqs, int node, void *arg,
986                             bool realloc)
987 {
988         int i, ret, virq;
989
990         if (domain == NULL) {
991                 domain = irq_default_domain;
992                 if (WARN(!domain, "domain is NULL; cannot allocate IRQ\n"))
993                         return -EINVAL;
994         }
995
996         if (!domain->ops->alloc) {
997                 pr_debug("domain->ops->alloc() is NULL\n");
998                 return -ENOSYS;
999         }
1000
1001         if (realloc && irq_base >= 0) {
1002                 virq = irq_base;
1003         } else {
1004                 virq = irq_domain_alloc_descs(irq_base, nr_irqs, 0, node);
1005                 if (virq < 0) {
1006                         pr_debug("cannot allocate IRQ(base %d, count %d)\n",
1007                                  irq_base, nr_irqs);
1008                         return virq;
1009                 }
1010         }
1011
1012         if (irq_domain_alloc_irq_data(domain, virq, nr_irqs)) {
1013                 pr_debug("cannot allocate memory for IRQ%d\n", virq);
1014                 ret = -ENOMEM;
1015                 goto out_free_desc;
1016         }
1017
1018         mutex_lock(&irq_domain_mutex);
1019         ret = domain->ops->alloc(domain, virq, nr_irqs, arg);
1020         if (ret < 0) {
1021                 mutex_unlock(&irq_domain_mutex);
1022                 goto out_free_irq_data;
1023         }
1024         for (i = 0; i < nr_irqs; i++)
1025                 irq_domain_insert_irq(virq + i);
1026         mutex_unlock(&irq_domain_mutex);
1027
1028         return virq;
1029
1030 out_free_irq_data:
1031         irq_domain_free_irq_data(virq, nr_irqs);
1032 out_free_desc:
1033         irq_free_descs(virq, nr_irqs);
1034         return ret;
1035 }
1036
1037 /**
1038  * irq_domain_free_irqs - Free IRQ number and associated data structures
1039  * @virq:       base IRQ number
1040  * @nr_irqs:    number of IRQs to free
1041  */
1042 void irq_domain_free_irqs(unsigned int virq, unsigned int nr_irqs)
1043 {
1044         struct irq_data *data = irq_get_irq_data(virq);
1045         int i;
1046
1047         if (WARN(!data || !data->domain || !data->domain->ops->free,
1048                  "NULL pointer, cannot free irq\n"))
1049                 return;
1050
1051         mutex_lock(&irq_domain_mutex);
1052         for (i = 0; i < nr_irqs; i++)
1053                 irq_domain_remove_irq(virq + i);
1054         data->domain->ops->free(data->domain, virq, nr_irqs);
1055         mutex_unlock(&irq_domain_mutex);
1056
1057         irq_domain_free_irq_data(virq, nr_irqs);
1058         irq_free_descs(virq, nr_irqs);
1059 }
1060
1061 /**
1062  * irq_domain_activate_irq - Call domain_ops->activate recursively to activate
1063  *                           interrupt
1064  * @irq_data:   outermost irq_data associated with interrupt
1065  *
1066  * This is the second step to call domain_ops->activate to program interrupt
1067  * controllers, so the interrupt could actually get delivered.
1068  */
1069 void irq_domain_activate_irq(struct irq_data *irq_data)
1070 {
1071         if (irq_data && irq_data->domain) {
1072                 struct irq_domain *domain = irq_data->domain;
1073
1074                 if (irq_data->parent_data)
1075                         irq_domain_activate_irq(irq_data->parent_data);
1076                 if (domain->ops->activate)
1077                         domain->ops->activate(domain, irq_data);
1078         }
1079 }
1080
1081 /**
1082  * irq_domain_deactivate_irq - Call domain_ops->deactivate recursively to
1083  *                             deactivate interrupt
1084  * @irq_data: outermost irq_data associated with interrupt
1085  *
1086  * It calls domain_ops->deactivate to program interrupt controllers to disable
1087  * interrupt delivery.
1088  */
1089 void irq_domain_deactivate_irq(struct irq_data *irq_data)
1090 {
1091         if (irq_data && irq_data->domain) {
1092                 struct irq_domain *domain = irq_data->domain;
1093
1094                 if (domain->ops->deactivate)
1095                         domain->ops->deactivate(domain, irq_data);
1096                 if (irq_data->parent_data)
1097                         irq_domain_deactivate_irq(irq_data->parent_data);
1098         }
1099 }
1100
1101 static void irq_domain_check_hierarchy(struct irq_domain *domain)
1102 {
1103         /* Hierarchy irq_domains must implement callback alloc() */
1104         if (domain->ops->alloc)
1105                 domain->flags |= IRQ_DOMAIN_FLAG_HIERARCHY;
1106 }
1107 #else   /* CONFIG_IRQ_DOMAIN_HIERARCHY */
1108 /**
1109  * irq_domain_get_irq_data - Get irq_data associated with @virq and @domain
1110  * @domain:     domain to match
1111  * @virq:       IRQ number to get irq_data
1112  */
1113 struct irq_data *irq_domain_get_irq_data(struct irq_domain *domain,
1114                                          unsigned int virq)
1115 {
1116         struct irq_data *irq_data = irq_get_irq_data(virq);
1117
1118         return (irq_data && irq_data->domain == domain) ? irq_data : NULL;
1119 }
1120
1121 static void irq_domain_check_hierarchy(struct irq_domain *domain)
1122 {
1123 }
1124 #endif  /* CONFIG_IRQ_DOMAIN_HIERARCHY */