blob: c78485e2a15c89a298e479fdaf3faea66693d569 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// SPDX-License-Identifier: GPL-2.0
2
3#define pr_fmt(fmt) "irq: " fmt
4
5#include <linux/acpi.h>
6#include <linux/debugfs.h>
7#include <linux/hardirq.h>
8#include <linux/interrupt.h>
9#include <linux/irq.h>
10#include <linux/irqdesc.h>
11#include <linux/irqdomain.h>
12#include <linux/module.h>
13#include <linux/mutex.h>
14#include <linux/of.h>
15#include <linux/of_address.h>
16#include <linux/of_irq.h>
17#include <linux/topology.h>
18#include <linux/seq_file.h>
19#include <linux/slab.h>
20#include <linux/smp.h>
21#include <linux/fs.h>
22
23static LIST_HEAD(irq_domain_list);
24static DEFINE_MUTEX(irq_domain_mutex);
25
26static struct irq_domain *irq_default_domain;
27
28static int irq_domain_alloc_irqs_locked(struct irq_domain *domain, int irq_base,
29 unsigned int nr_irqs, int node, void *arg,
30 bool realloc, const struct irq_affinity_desc *affinity);
31static void irq_domain_check_hierarchy(struct irq_domain *domain);
32
33struct irqchip_fwid {
34 struct fwnode_handle fwnode;
35 unsigned int type;
36 char *name;
37 phys_addr_t *pa;
38};
39
40#ifdef CONFIG_GENERIC_IRQ_DEBUGFS
41static void debugfs_add_domain_dir(struct irq_domain *d);
42static void debugfs_remove_domain_dir(struct irq_domain *d);
43#else
44static inline void debugfs_add_domain_dir(struct irq_domain *d) { }
45static inline void debugfs_remove_domain_dir(struct irq_domain *d) { }
46#endif
47
48const struct fwnode_operations irqchip_fwnode_ops;
49EXPORT_SYMBOL_GPL(irqchip_fwnode_ops);
50
51/**
52 * irq_domain_alloc_fwnode - Allocate a fwnode_handle suitable for
53 * identifying an irq domain
54 * @type: Type of irqchip_fwnode. See linux/irqdomain.h
55 * @name: Optional user provided domain name
56 * @id: Optional user provided id if name != NULL
57 * @pa: Optional user-provided physical address
58 *
59 * Allocate a struct irqchip_fwid, and return a poiner to the embedded
60 * fwnode_handle (or NULL on failure).
61 *
62 * Note: The types IRQCHIP_FWNODE_NAMED and IRQCHIP_FWNODE_NAMED_ID are
63 * solely to transport name information to irqdomain creation code. The
64 * node is not stored. For other types the pointer is kept in the irq
65 * domain struct.
66 */
67struct fwnode_handle *__irq_domain_alloc_fwnode(unsigned int type, int id,
68 const char *name,
69 phys_addr_t *pa)
70{
71 struct irqchip_fwid *fwid;
72 char *n;
73
74 fwid = kzalloc(sizeof(*fwid), GFP_KERNEL);
75
76 switch (type) {
77 case IRQCHIP_FWNODE_NAMED:
78 n = kasprintf(GFP_KERNEL, "%s", name);
79 break;
80 case IRQCHIP_FWNODE_NAMED_ID:
81 n = kasprintf(GFP_KERNEL, "%s-%d", name, id);
82 break;
83 default:
84 n = kasprintf(GFP_KERNEL, "irqchip@%pa", pa);
85 break;
86 }
87
88 if (!fwid || !n) {
89 kfree(fwid);
90 kfree(n);
91 return NULL;
92 }
93
94 fwid->type = type;
95 fwid->name = n;
96 fwid->pa = pa;
97 fwid->fwnode.ops = &irqchip_fwnode_ops;
98 return &fwid->fwnode;
99}
100EXPORT_SYMBOL_GPL(__irq_domain_alloc_fwnode);
101
102/**
103 * irq_domain_free_fwnode - Free a non-OF-backed fwnode_handle
104 *
105 * Free a fwnode_handle allocated with irq_domain_alloc_fwnode.
106 */
107void irq_domain_free_fwnode(struct fwnode_handle *fwnode)
108{
109 struct irqchip_fwid *fwid;
110
111 if (WARN_ON(!is_fwnode_irqchip(fwnode)))
112 return;
113
114 fwid = container_of(fwnode, struct irqchip_fwid, fwnode);
115 kfree(fwid->name);
116 kfree(fwid);
117}
118EXPORT_SYMBOL_GPL(irq_domain_free_fwnode);
119
120static struct irq_domain *__irq_domain_create(struct fwnode_handle *fwnode,
121 unsigned int size,
122 irq_hw_number_t hwirq_max,
123 int direct_max,
124 const struct irq_domain_ops *ops,
125 void *host_data)
126{
127 struct device_node *of_node = to_of_node(fwnode);
128 struct irqchip_fwid *fwid;
129 struct irq_domain *domain;
130
131 static atomic_t unknown_domains;
132
133 domain = kzalloc_node(sizeof(*domain) + (sizeof(unsigned int) * size),
134 GFP_KERNEL, of_node_to_nid(of_node));
135 if (!domain)
136 return NULL;
137
138 if (fwnode && is_fwnode_irqchip(fwnode)) {
139 fwid = container_of(fwnode, struct irqchip_fwid, fwnode);
140
141 switch (fwid->type) {
142 case IRQCHIP_FWNODE_NAMED:
143 case IRQCHIP_FWNODE_NAMED_ID:
144 domain->fwnode = fwnode;
145 domain->name = kstrdup(fwid->name, GFP_KERNEL);
146 if (!domain->name) {
147 kfree(domain);
148 return NULL;
149 }
150 domain->flags |= IRQ_DOMAIN_NAME_ALLOCATED;
151 break;
152 default:
153 domain->fwnode = fwnode;
154 domain->name = fwid->name;
155 break;
156 }
157#ifdef CONFIG_ACPI
158 } else if (is_acpi_device_node(fwnode)) {
159 struct acpi_buffer buf = {
160 .length = ACPI_ALLOCATE_BUFFER,
161 };
162 acpi_handle handle;
163
164 handle = acpi_device_handle(to_acpi_device_node(fwnode));
165 if (acpi_get_name(handle, ACPI_FULL_PATHNAME, &buf) == AE_OK) {
166 domain->name = buf.pointer;
167 domain->flags |= IRQ_DOMAIN_NAME_ALLOCATED;
168 }
169
170 domain->fwnode = fwnode;
171#endif
172 } else if (of_node) {
173 char *name;
174
175 /*
176 * DT paths contain '/', which debugfs is legitimately
177 * unhappy about. Replace them with ':', which does
178 * the trick and is not as offensive as '\'...
179 */
180 name = kasprintf(GFP_KERNEL, "%pOF", of_node);
181 if (!name) {
182 kfree(domain);
183 return NULL;
184 }
185
186 strreplace(name, '/', ':');
187
188 domain->name = name;
189 domain->fwnode = fwnode;
190 domain->flags |= IRQ_DOMAIN_NAME_ALLOCATED;
191 }
192
193 if (!domain->name) {
194 if (fwnode)
195 pr_err("Invalid fwnode type for irqdomain\n");
196 domain->name = kasprintf(GFP_KERNEL, "unknown-%d",
197 atomic_inc_return(&unknown_domains));
198 if (!domain->name) {
199 kfree(domain);
200 return NULL;
201 }
202 domain->flags |= IRQ_DOMAIN_NAME_ALLOCATED;
203 }
204
205 of_node_get(of_node);
206
207 /* Fill structure */
208 INIT_RADIX_TREE(&domain->revmap_tree, GFP_KERNEL);
209 mutex_init(&domain->revmap_tree_mutex);
210 domain->ops = ops;
211 domain->host_data = host_data;
212 domain->hwirq_max = hwirq_max;
213 domain->revmap_size = size;
214 domain->revmap_direct_max_irq = direct_max;
215 irq_domain_check_hierarchy(domain);
216
217 return domain;
218}
219
220static void __irq_domain_publish(struct irq_domain *domain)
221{
222 mutex_lock(&irq_domain_mutex);
223 debugfs_add_domain_dir(domain);
224 list_add(&domain->link, &irq_domain_list);
225 mutex_unlock(&irq_domain_mutex);
226
227 pr_debug("Added domain %s\n", domain->name);
228}
229
230/**
231 * __irq_domain_add() - Allocate a new irq_domain data structure
232 * @fwnode: firmware node for the interrupt controller
233 * @size: Size of linear map; 0 for radix mapping only
234 * @hwirq_max: Maximum number of interrupts supported by controller
235 * @direct_max: Maximum value of direct maps; Use ~0 for no limit; 0 for no
236 * direct mapping
237 * @ops: domain callbacks
238 * @host_data: Controller private data pointer
239 *
240 * Allocates and initializes an irq_domain structure.
241 * Returns pointer to IRQ domain, or NULL on failure.
242 */
243struct irq_domain *__irq_domain_add(struct fwnode_handle *fwnode, unsigned int size,
244 irq_hw_number_t hwirq_max, int direct_max,
245 const struct irq_domain_ops *ops,
246 void *host_data)
247{
248 struct irq_domain *domain;
249
250 domain = __irq_domain_create(fwnode, size, hwirq_max, direct_max,
251 ops, host_data);
252 if (domain)
253 __irq_domain_publish(domain);
254
255 return domain;
256}
257EXPORT_SYMBOL_GPL(__irq_domain_add);
258
259/**
260 * irq_domain_remove() - Remove an irq domain.
261 * @domain: domain to remove
262 *
263 * This routine is used to remove an irq domain. The caller must ensure
264 * that all mappings within the domain have been disposed of prior to
265 * use, depending on the revmap type.
266 */
267void irq_domain_remove(struct irq_domain *domain)
268{
269 mutex_lock(&irq_domain_mutex);
270 debugfs_remove_domain_dir(domain);
271
272 WARN_ON(!radix_tree_empty(&domain->revmap_tree));
273
274 list_del(&domain->link);
275
276 /*
277 * If the going away domain is the default one, reset it.
278 */
279 if (unlikely(irq_default_domain == domain))
280 irq_set_default_host(NULL);
281
282 mutex_unlock(&irq_domain_mutex);
283
284 pr_debug("Removed domain %s\n", domain->name);
285
286 of_node_put(irq_domain_get_of_node(domain));
287 if (domain->flags & IRQ_DOMAIN_NAME_ALLOCATED)
288 kfree(domain->name);
289 kfree(domain);
290}
291EXPORT_SYMBOL_GPL(irq_domain_remove);
292
293void irq_domain_update_bus_token(struct irq_domain *domain,
294 enum irq_domain_bus_token bus_token)
295{
296 char *name;
297
298 if (domain->bus_token == bus_token)
299 return;
300
301 mutex_lock(&irq_domain_mutex);
302
303 domain->bus_token = bus_token;
304
305 name = kasprintf(GFP_KERNEL, "%s-%d", domain->name, bus_token);
306 if (!name) {
307 mutex_unlock(&irq_domain_mutex);
308 return;
309 }
310
311 debugfs_remove_domain_dir(domain);
312
313 if (domain->flags & IRQ_DOMAIN_NAME_ALLOCATED)
314 kfree(domain->name);
315 else
316 domain->flags |= IRQ_DOMAIN_NAME_ALLOCATED;
317
318 domain->name = name;
319 debugfs_add_domain_dir(domain);
320
321 mutex_unlock(&irq_domain_mutex);
322}
323EXPORT_SYMBOL_GPL(irq_domain_update_bus_token);
324
325/**
326 * irq_domain_add_simple() - Register an irq_domain and optionally map a range of irqs
327 * @of_node: pointer to interrupt controller's device tree node.
328 * @size: total number of irqs in mapping
329 * @first_irq: first number of irq block assigned to the domain,
330 * pass zero to assign irqs on-the-fly. If first_irq is non-zero, then
331 * pre-map all of the irqs in the domain to virqs starting at first_irq.
332 * @ops: domain callbacks
333 * @host_data: Controller private data pointer
334 *
335 * Allocates an irq_domain, and optionally if first_irq is positive then also
336 * allocate irq_descs and map all of the hwirqs to virqs starting at first_irq.
337 *
338 * This is intended to implement the expected behaviour for most
339 * interrupt controllers. If device tree is used, then first_irq will be 0 and
340 * irqs get mapped dynamically on the fly. However, if the controller requires
341 * static virq assignments (non-DT boot) then it will set that up correctly.
342 */
343struct irq_domain *irq_domain_add_simple(struct device_node *of_node,
344 unsigned int size,
345 unsigned int first_irq,
346 const struct irq_domain_ops *ops,
347 void *host_data)
348{
349 struct irq_domain *domain;
350
351 domain = __irq_domain_add(of_node_to_fwnode(of_node), size, size, 0, ops, host_data);
352 if (!domain)
353 return NULL;
354
355 if (first_irq > 0) {
356 if (IS_ENABLED(CONFIG_SPARSE_IRQ)) {
357 /* attempt to allocated irq_descs */
358 int rc = irq_alloc_descs(first_irq, first_irq, size,
359 of_node_to_nid(of_node));
360 if (rc < 0)
361 pr_info("Cannot allocate irq_descs @ IRQ%d, assuming pre-allocated\n",
362 first_irq);
363 }
364 irq_domain_associate_many(domain, first_irq, 0, size);
365 }
366
367 return domain;
368}
369EXPORT_SYMBOL_GPL(irq_domain_add_simple);
370
371/**
372 * irq_domain_add_legacy() - Allocate and register a legacy revmap irq_domain.
373 * @of_node: pointer to interrupt controller's device tree node.
374 * @size: total number of irqs in legacy mapping
375 * @first_irq: first number of irq block assigned to the domain
376 * @first_hwirq: first hwirq number to use for the translation. Should normally
377 * be '0', but a positive integer can be used if the effective
378 * hwirqs numbering does not begin at zero.
379 * @ops: map/unmap domain callbacks
380 * @host_data: Controller private data pointer
381 *
382 * Note: the map() callback will be called before this function returns
383 * for all legacy interrupts except 0 (which is always the invalid irq for
384 * a legacy controller).
385 */
386struct irq_domain *irq_domain_add_legacy(struct device_node *of_node,
387 unsigned int size,
388 unsigned int first_irq,
389 irq_hw_number_t first_hwirq,
390 const struct irq_domain_ops *ops,
391 void *host_data)
392{
393 struct irq_domain *domain;
394
395 domain = __irq_domain_add(of_node_to_fwnode(of_node), first_hwirq + size,
396 first_hwirq + size, 0, ops, host_data);
397 if (domain)
398 irq_domain_associate_many(domain, first_irq, first_hwirq, size);
399
400 return domain;
401}
402EXPORT_SYMBOL_GPL(irq_domain_add_legacy);
403
404/**
405 * irq_find_matching_fwspec() - Locates a domain for a given fwspec
406 * @fwspec: FW specifier for an interrupt
407 * @bus_token: domain-specific data
408 */
409struct irq_domain *irq_find_matching_fwspec(struct irq_fwspec *fwspec,
410 enum irq_domain_bus_token bus_token)
411{
412 struct irq_domain *h, *found = NULL;
413 struct fwnode_handle *fwnode = fwspec->fwnode;
414 int rc;
415
416 /* We might want to match the legacy controller last since
417 * it might potentially be set to match all interrupts in
418 * the absence of a device node. This isn't a problem so far
419 * yet though...
420 *
421 * bus_token == DOMAIN_BUS_ANY matches any domain, any other
422 * values must generate an exact match for the domain to be
423 * selected.
424 */
425 mutex_lock(&irq_domain_mutex);
426 list_for_each_entry(h, &irq_domain_list, link) {
427 if (h->ops->select && fwspec->param_count)
428 rc = h->ops->select(h, fwspec, bus_token);
429 else if (h->ops->match)
430 rc = h->ops->match(h, to_of_node(fwnode), bus_token);
431 else
432 rc = ((fwnode != NULL) && (h->fwnode == fwnode) &&
433 ((bus_token == DOMAIN_BUS_ANY) ||
434 (h->bus_token == bus_token)));
435
436 if (rc) {
437 found = h;
438 break;
439 }
440 }
441 mutex_unlock(&irq_domain_mutex);
442 return found;
443}
444EXPORT_SYMBOL_GPL(irq_find_matching_fwspec);
445
446/**
447 * irq_domain_check_msi_remap - Check whether all MSI irq domains implement
448 * IRQ remapping
449 *
450 * Return: false if any MSI irq domain does not support IRQ remapping,
451 * true otherwise (including if there is no MSI irq domain)
452 */
453bool irq_domain_check_msi_remap(void)
454{
455 struct irq_domain *h;
456 bool ret = true;
457
458 mutex_lock(&irq_domain_mutex);
459 list_for_each_entry(h, &irq_domain_list, link) {
460 if (irq_domain_is_msi(h) &&
461 !irq_domain_hierarchical_is_msi_remap(h)) {
462 ret = false;
463 break;
464 }
465 }
466 mutex_unlock(&irq_domain_mutex);
467 return ret;
468}
469EXPORT_SYMBOL_GPL(irq_domain_check_msi_remap);
470
471/**
472 * irq_set_default_host() - Set a "default" irq domain
473 * @domain: default domain pointer
474 *
475 * For convenience, it's possible to set a "default" domain that will be used
476 * whenever NULL is passed to irq_create_mapping(). It makes life easier for
477 * platforms that want to manipulate a few hard coded interrupt numbers that
478 * aren't properly represented in the device-tree.
479 */
480void irq_set_default_host(struct irq_domain *domain)
481{
482 pr_debug("Default domain set to @0x%p\n", domain);
483
484 irq_default_domain = domain;
485}
486EXPORT_SYMBOL_GPL(irq_set_default_host);
487
488/**
489 * irq_get_default_host() - Retrieve the "default" irq domain
490 *
491 * Returns: the default domain, if any.
492 *
493 * Modern code should never use this. This should only be used on
494 * systems that cannot implement a firmware->fwnode mapping (which
495 * both DT and ACPI provide).
496 */
497struct irq_domain *irq_get_default_host(void)
498{
499 return irq_default_domain;
500}
501
502static void irq_domain_clear_mapping(struct irq_domain *domain,
503 irq_hw_number_t hwirq)
504{
505 if (hwirq < domain->revmap_size) {
506 domain->linear_revmap[hwirq] = 0;
507 } else {
508 mutex_lock(&domain->revmap_tree_mutex);
509 radix_tree_delete(&domain->revmap_tree, hwirq);
510 mutex_unlock(&domain->revmap_tree_mutex);
511 }
512}
513
514static void irq_domain_set_mapping(struct irq_domain *domain,
515 irq_hw_number_t hwirq,
516 struct irq_data *irq_data)
517{
518 if (hwirq < domain->revmap_size) {
519 domain->linear_revmap[hwirq] = irq_data->irq;
520 } else {
521 mutex_lock(&domain->revmap_tree_mutex);
522 radix_tree_insert(&domain->revmap_tree, hwirq, irq_data);
523 mutex_unlock(&domain->revmap_tree_mutex);
524 }
525}
526
527void irq_domain_disassociate(struct irq_domain *domain, unsigned int irq)
528{
529 struct irq_data *irq_data = irq_get_irq_data(irq);
530 irq_hw_number_t hwirq;
531
532 if (WARN(!irq_data || irq_data->domain != domain,
533 "virq%i doesn't exist; cannot disassociate\n", irq))
534 return;
535
536 hwirq = irq_data->hwirq;
537
538 mutex_lock(&irq_domain_mutex);
539
540 irq_set_status_flags(irq, IRQ_NOREQUEST);
541
542 /* remove chip and handler */
543 irq_set_chip_and_handler(irq, NULL, NULL);
544
545 /* Make sure it's completed */
546 synchronize_irq(irq);
547
548 /* Tell the PIC about it */
549 if (domain->ops->unmap)
550 domain->ops->unmap(domain, irq);
551 smp_mb();
552
553 irq_data->domain = NULL;
554 irq_data->hwirq = 0;
555 domain->mapcount--;
556
557 /* Clear reverse map for this hwirq */
558 irq_domain_clear_mapping(domain, hwirq);
559
560 mutex_unlock(&irq_domain_mutex);
561}
562
563static int irq_domain_associate_locked(struct irq_domain *domain, unsigned int virq,
564 irq_hw_number_t hwirq)
565{
566 struct irq_data *irq_data = irq_get_irq_data(virq);
567 int ret;
568
569 if (WARN(hwirq >= domain->hwirq_max,
570 "error: hwirq 0x%x is too large for %s\n", (int)hwirq, domain->name))
571 return -EINVAL;
572 if (WARN(!irq_data, "error: virq%i is not allocated", virq))
573 return -EINVAL;
574 if (WARN(irq_data->domain, "error: virq%i is already associated", virq))
575 return -EINVAL;
576
577 irq_data->hwirq = hwirq;
578 irq_data->domain = domain;
579 if (domain->ops->map) {
580 ret = domain->ops->map(domain, virq, hwirq);
581 if (ret != 0) {
582 /*
583 * If map() returns -EPERM, this interrupt is protected
584 * by the firmware or some other service and shall not
585 * be mapped. Don't bother telling the user about it.
586 */
587 if (ret != -EPERM) {
588 pr_info("%s didn't like hwirq-0x%lx to VIRQ%i mapping (rc=%d)\n",
589 domain->name, hwirq, virq, ret);
590 }
591 irq_data->domain = NULL;
592 irq_data->hwirq = 0;
593 return ret;
594 }
595
596 /* If not already assigned, give the domain the chip's name */
597 if (!domain->name && irq_data->chip)
598 domain->name = irq_data->chip->name;
599 }
600
601 domain->mapcount++;
602 irq_domain_set_mapping(domain, hwirq, irq_data);
603
604 irq_clear_status_flags(virq, IRQ_NOREQUEST);
605
606 return 0;
607}
608
609int irq_domain_associate(struct irq_domain *domain, unsigned int virq,
610 irq_hw_number_t hwirq)
611{
612 int ret;
613
614 mutex_lock(&irq_domain_mutex);
615 ret = irq_domain_associate_locked(domain, virq, hwirq);
616 mutex_unlock(&irq_domain_mutex);
617
618 return ret;
619}
620EXPORT_SYMBOL_GPL(irq_domain_associate);
621
622void irq_domain_associate_many(struct irq_domain *domain, unsigned int irq_base,
623 irq_hw_number_t hwirq_base, int count)
624{
625 struct device_node *of_node;
626 int i;
627
628 of_node = irq_domain_get_of_node(domain);
629 pr_debug("%s(%s, irqbase=%i, hwbase=%i, count=%i)\n", __func__,
630 of_node_full_name(of_node), irq_base, (int)hwirq_base, count);
631
632 for (i = 0; i < count; i++) {
633 irq_domain_associate(domain, irq_base + i, hwirq_base + i);
634 }
635}
636EXPORT_SYMBOL_GPL(irq_domain_associate_many);
637
638/**
639 * irq_create_direct_mapping() - Allocate an irq for direct mapping
640 * @domain: domain to allocate the irq for or NULL for default domain
641 *
642 * This routine is used for irq controllers which can choose the hardware
643 * interrupt numbers they generate. In such a case it's simplest to use
644 * the linux irq as the hardware interrupt number. It still uses the linear
645 * or radix tree to store the mapping, but the irq controller can optimize
646 * the revmap path by using the hwirq directly.
647 */
648unsigned int irq_create_direct_mapping(struct irq_domain *domain)
649{
650 struct device_node *of_node;
651 unsigned int virq;
652
653 if (domain == NULL)
654 domain = irq_default_domain;
655
656 of_node = irq_domain_get_of_node(domain);
657 virq = irq_alloc_desc_from(1, of_node_to_nid(of_node));
658 if (!virq) {
659 pr_debug("create_direct virq allocation failed\n");
660 return 0;
661 }
662 if (virq >= domain->revmap_direct_max_irq) {
663 pr_err("ERROR: no free irqs available below %i maximum\n",
664 domain->revmap_direct_max_irq);
665 irq_free_desc(virq);
666 return 0;
667 }
668 pr_debug("create_direct obtained virq %d\n", virq);
669
670 if (irq_domain_associate(domain, virq, virq)) {
671 irq_free_desc(virq);
672 return 0;
673 }
674
675 return virq;
676}
677EXPORT_SYMBOL_GPL(irq_create_direct_mapping);
678
679static unsigned int irq_create_mapping_affinity_locked(struct irq_domain *domain,
680 irq_hw_number_t hwirq,
681 const struct irq_affinity_desc *affinity)
682{
683 struct device_node *of_node = irq_domain_get_of_node(domain);
684 int virq;
685
686 pr_debug("irq_create_mapping(0x%p, 0x%lx)\n", domain, hwirq);
687
688 /* Allocate a virtual interrupt number */
689 virq = irq_domain_alloc_descs(-1, 1, hwirq, of_node_to_nid(of_node),
690 affinity);
691 if (virq <= 0) {
692 pr_debug("-> virq allocation failed\n");
693 return 0;
694 }
695
696 if (irq_domain_associate_locked(domain, virq, hwirq)) {
697 irq_free_desc(virq);
698 return 0;
699 }
700
701 pr_debug("irq %lu on domain %s mapped to virtual irq %u\n",
702 hwirq, of_node_full_name(of_node), virq);
703
704 return virq;
705}
706
707/**
708 * irq_create_mapping_affinity() - Map a hardware interrupt into linux irq space
709 * @domain: domain owning this hardware interrupt or NULL for default domain
710 * @hwirq: hardware irq number in that domain space
711 * @affinity: irq affinity
712 *
713 * Only one mapping per hardware interrupt is permitted. Returns a linux
714 * irq number.
715 * If the sense/trigger is to be specified, set_irq_type() should be called
716 * on the number returned from that call.
717 */
718unsigned int irq_create_mapping_affinity(struct irq_domain *domain,
719 irq_hw_number_t hwirq,
720 const struct irq_affinity_desc *affinity)
721{
722 int virq;
723
724 /* Look for default domain if necessary */
725 if (domain == NULL)
726 domain = irq_default_domain;
727 if (domain == NULL) {
728 WARN(1, "%s(, %lx) called with NULL domain\n", __func__, hwirq);
729 return 0;
730 }
731
732 mutex_lock(&irq_domain_mutex);
733
734 /* Check if mapping already exists */
735 virq = irq_find_mapping(domain, hwirq);
736 if (virq) {
737 pr_debug("existing mapping on virq %d\n", virq);
738 goto out;
739 }
740
741 virq = irq_create_mapping_affinity_locked(domain, hwirq, affinity);
742out:
743 mutex_unlock(&irq_domain_mutex);
744
745 return virq;
746}
747EXPORT_SYMBOL_GPL(irq_create_mapping_affinity);
748
749/**
750 * irq_create_strict_mappings() - Map a range of hw irqs to fixed linux irqs
751 * @domain: domain owning the interrupt range
752 * @irq_base: beginning of linux IRQ range
753 * @hwirq_base: beginning of hardware IRQ range
754 * @count: Number of interrupts to map
755 *
756 * This routine is used for allocating and mapping a range of hardware
757 * irqs to linux irqs where the linux irq numbers are at pre-defined
758 * locations. For use by controllers that already have static mappings
759 * to insert in to the domain.
760 *
761 * Non-linear users can use irq_create_identity_mapping() for IRQ-at-a-time
762 * domain insertion.
763 *
764 * 0 is returned upon success, while any failure to establish a static
765 * mapping is treated as an error.
766 */
767int irq_create_strict_mappings(struct irq_domain *domain, unsigned int irq_base,
768 irq_hw_number_t hwirq_base, int count)
769{
770 struct device_node *of_node;
771 int ret;
772
773 of_node = irq_domain_get_of_node(domain);
774 ret = irq_alloc_descs(irq_base, irq_base, count,
775 of_node_to_nid(of_node));
776 if (unlikely(ret < 0))
777 return ret;
778
779 irq_domain_associate_many(domain, irq_base, hwirq_base, count);
780 return 0;
781}
782EXPORT_SYMBOL_GPL(irq_create_strict_mappings);
783
784static int irq_domain_translate(struct irq_domain *d,
785 struct irq_fwspec *fwspec,
786 irq_hw_number_t *hwirq, unsigned int *type)
787{
788#ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
789 if (d->ops->translate)
790 return d->ops->translate(d, fwspec, hwirq, type);
791#endif
792 if (d->ops->xlate)
793 return d->ops->xlate(d, to_of_node(fwspec->fwnode),
794 fwspec->param, fwspec->param_count,
795 hwirq, type);
796
797 /* If domain has no translation, then we assume interrupt line */
798 *hwirq = fwspec->param[0];
799 return 0;
800}
801
802static void of_phandle_args_to_fwspec(struct device_node *np, const u32 *args,
803 unsigned int count,
804 struct irq_fwspec *fwspec)
805{
806 int i;
807
808 fwspec->fwnode = np ? &np->fwnode : NULL;
809 fwspec->param_count = count;
810
811 for (i = 0; i < count; i++)
812 fwspec->param[i] = args[i];
813}
814
815unsigned int irq_create_fwspec_mapping(struct irq_fwspec *fwspec)
816{
817 struct irq_domain *domain;
818 struct irq_data *irq_data;
819 irq_hw_number_t hwirq;
820 unsigned int type = IRQ_TYPE_NONE;
821 int virq;
822
823 if (fwspec->fwnode) {
824 domain = irq_find_matching_fwspec(fwspec, DOMAIN_BUS_WIRED);
825 if (!domain)
826 domain = irq_find_matching_fwspec(fwspec, DOMAIN_BUS_ANY);
827 } else {
828 domain = irq_default_domain;
829 }
830
831 if (!domain) {
832 pr_warn("no irq domain found for %s !\n",
833 of_node_full_name(to_of_node(fwspec->fwnode)));
834 return 0;
835 }
836
837 if (irq_domain_translate(domain, fwspec, &hwirq, &type))
838 return 0;
839
840 /*
841 * WARN if the irqchip returns a type with bits
842 * outside the sense mask set and clear these bits.
843 */
844 if (WARN_ON(type & ~IRQ_TYPE_SENSE_MASK))
845 type &= IRQ_TYPE_SENSE_MASK;
846
847 mutex_lock(&irq_domain_mutex);
848
849 /*
850 * If we've already configured this interrupt,
851 * don't do it again, or hell will break loose.
852 */
853 virq = irq_find_mapping(domain, hwirq);
854 if (virq) {
855 /*
856 * If the trigger type is not specified or matches the
857 * current trigger type then we are done so return the
858 * interrupt number.
859 */
860 if (type == IRQ_TYPE_NONE || type == irq_get_trigger_type(virq))
861 goto out;
862
863 /*
864 * If the trigger type has not been set yet, then set
865 * it now and return the interrupt number.
866 */
867 if (irq_get_trigger_type(virq) == IRQ_TYPE_NONE) {
868 irq_data = irq_get_irq_data(virq);
869 if (!irq_data) {
870 virq = 0;
871 goto out;
872 }
873
874 irqd_set_trigger_type(irq_data, type);
875 goto out;
876 }
877
878 pr_warn("type mismatch, failed to map hwirq-%lu for %s!\n",
879 hwirq, of_node_full_name(to_of_node(fwspec->fwnode)));
880 virq = 0;
881 goto out;
882 }
883
884 if (irq_domain_is_hierarchy(domain)) {
885 virq = irq_domain_alloc_irqs_locked(domain, -1, 1, NUMA_NO_NODE,
886 fwspec, false, NULL);
887 if (virq <= 0) {
888 virq = 0;
889 goto out;
890 }
891 } else {
892 /* Create mapping */
893 virq = irq_create_mapping_affinity_locked(domain, hwirq, NULL);
894 if (!virq)
895 goto out;
896 }
897
898 irq_data = irq_get_irq_data(virq);
899 if (WARN_ON(!irq_data)) {
900 virq = 0;
901 goto out;
902 }
903
904 /* Store trigger type */
905 irqd_set_trigger_type(irq_data, type);
906out:
907 mutex_unlock(&irq_domain_mutex);
908
909 return virq;
910}
911EXPORT_SYMBOL_GPL(irq_create_fwspec_mapping);
912
913unsigned int irq_create_of_mapping(struct of_phandle_args *irq_data)
914{
915 struct irq_fwspec fwspec;
916
917 of_phandle_args_to_fwspec(irq_data->np, irq_data->args,
918 irq_data->args_count, &fwspec);
919
920 return irq_create_fwspec_mapping(&fwspec);
921}
922EXPORT_SYMBOL_GPL(irq_create_of_mapping);
923
924/**
925 * irq_dispose_mapping() - Unmap an interrupt
926 * @virq: linux irq number of the interrupt to unmap
927 */
928void irq_dispose_mapping(unsigned int virq)
929{
930 struct irq_data *irq_data = irq_get_irq_data(virq);
931 struct irq_domain *domain;
932
933 if (!virq || !irq_data)
934 return;
935
936 domain = irq_data->domain;
937 if (WARN_ON(domain == NULL))
938 return;
939
940 if (irq_domain_is_hierarchy(domain)) {
941 irq_domain_free_irqs(virq, 1);
942 } else {
943 irq_domain_disassociate(domain, virq);
944 irq_free_desc(virq);
945 }
946}
947EXPORT_SYMBOL_GPL(irq_dispose_mapping);
948
949/**
950 * irq_find_mapping() - Find a linux irq from a hw irq number.
951 * @domain: domain owning this hardware interrupt
952 * @hwirq: hardware irq number in that domain space
953 */
954unsigned int irq_find_mapping(struct irq_domain *domain,
955 irq_hw_number_t hwirq)
956{
957 struct irq_data *data;
958
959 /* Look for default domain if nececssary */
960 if (domain == NULL)
961 domain = irq_default_domain;
962 if (domain == NULL)
963 return 0;
964
965 if (hwirq < domain->revmap_direct_max_irq) {
966 data = irq_domain_get_irq_data(domain, hwirq);
967 if (data && data->hwirq == hwirq)
968 return hwirq;
969 }
970
971 /* Check if the hwirq is in the linear revmap. */
972 if (hwirq < domain->revmap_size)
973 return domain->linear_revmap[hwirq];
974
975 rcu_read_lock();
976 data = radix_tree_lookup(&domain->revmap_tree, hwirq);
977 rcu_read_unlock();
978 return data ? data->irq : 0;
979}
980EXPORT_SYMBOL_GPL(irq_find_mapping);
981
982/**
983 * irq_domain_xlate_onecell() - Generic xlate for direct one cell bindings
984 *
985 * Device Tree IRQ specifier translation function which works with one cell
986 * bindings where the cell value maps directly to the hwirq number.
987 */
988int irq_domain_xlate_onecell(struct irq_domain *d, struct device_node *ctrlr,
989 const u32 *intspec, unsigned int intsize,
990 unsigned long *out_hwirq, unsigned int *out_type)
991{
992 if (WARN_ON(intsize < 1))
993 return -EINVAL;
994 *out_hwirq = intspec[0];
995 *out_type = IRQ_TYPE_NONE;
996 return 0;
997}
998EXPORT_SYMBOL_GPL(irq_domain_xlate_onecell);
999
1000/**
1001 * irq_domain_xlate_twocell() - Generic xlate for direct two cell bindings
1002 *
1003 * Device Tree IRQ specifier translation function which works with two cell
1004 * bindings where the cell values map directly to the hwirq number
1005 * and linux irq flags.
1006 */
1007int irq_domain_xlate_twocell(struct irq_domain *d, struct device_node *ctrlr,
1008 const u32 *intspec, unsigned int intsize,
1009 irq_hw_number_t *out_hwirq, unsigned int *out_type)
1010{
1011 struct irq_fwspec fwspec;
1012
1013 of_phandle_args_to_fwspec(ctrlr, intspec, intsize, &fwspec);
1014 return irq_domain_translate_twocell(d, &fwspec, out_hwirq, out_type);
1015}
1016EXPORT_SYMBOL_GPL(irq_domain_xlate_twocell);
1017
1018/**
1019 * irq_domain_xlate_onetwocell() - Generic xlate for one or two cell bindings
1020 *
1021 * Device Tree IRQ specifier translation function which works with either one
1022 * or two cell bindings where the cell values map directly to the hwirq number
1023 * and linux irq flags.
1024 *
1025 * Note: don't use this function unless your interrupt controller explicitly
1026 * supports both one and two cell bindings. For the majority of controllers
1027 * the _onecell() or _twocell() variants above should be used.
1028 */
1029int irq_domain_xlate_onetwocell(struct irq_domain *d,
1030 struct device_node *ctrlr,
1031 const u32 *intspec, unsigned int intsize,
1032 unsigned long *out_hwirq, unsigned int *out_type)
1033{
1034 if (WARN_ON(intsize < 1))
1035 return -EINVAL;
1036 *out_hwirq = intspec[0];
1037 if (intsize > 1)
1038 *out_type = intspec[1] & IRQ_TYPE_SENSE_MASK;
1039 else
1040 *out_type = IRQ_TYPE_NONE;
1041 return 0;
1042}
1043EXPORT_SYMBOL_GPL(irq_domain_xlate_onetwocell);
1044
1045const struct irq_domain_ops irq_domain_simple_ops = {
1046 .xlate = irq_domain_xlate_onetwocell,
1047};
1048EXPORT_SYMBOL_GPL(irq_domain_simple_ops);
1049
1050/**
1051 * irq_domain_translate_twocell() - Generic translate for direct two cell
1052 * bindings
1053 *
1054 * Device Tree IRQ specifier translation function which works with two cell
1055 * bindings where the cell values map directly to the hwirq number
1056 * and linux irq flags.
1057 */
1058int irq_domain_translate_twocell(struct irq_domain *d,
1059 struct irq_fwspec *fwspec,
1060 unsigned long *out_hwirq,
1061 unsigned int *out_type)
1062{
1063 if (WARN_ON(fwspec->param_count < 2))
1064 return -EINVAL;
1065 *out_hwirq = fwspec->param[0];
1066 *out_type = fwspec->param[1] & IRQ_TYPE_SENSE_MASK;
1067 return 0;
1068}
1069EXPORT_SYMBOL_GPL(irq_domain_translate_twocell);
1070
1071int irq_domain_alloc_descs(int virq, unsigned int cnt, irq_hw_number_t hwirq,
1072 int node, const struct irq_affinity_desc *affinity)
1073{
1074 unsigned int hint;
1075
1076 if (virq >= 0) {
1077 virq = __irq_alloc_descs(virq, virq, cnt, node, THIS_MODULE,
1078 affinity);
1079 } else {
1080 hint = hwirq % nr_irqs;
1081 if (hint == 0)
1082 hint++;
1083 virq = __irq_alloc_descs(-1, hint, cnt, node, THIS_MODULE,
1084 affinity);
1085 if (virq <= 0 && hint > 1) {
1086 virq = __irq_alloc_descs(-1, 1, cnt, node, THIS_MODULE,
1087 affinity);
1088 }
1089 }
1090
1091 return virq;
1092}
1093
1094#ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
1095/**
1096 * irq_domain_create_hierarchy - Add a irqdomain into the hierarchy
1097 * @parent: Parent irq domain to associate with the new domain
1098 * @flags: Irq domain flags associated to the domain
1099 * @size: Size of the domain. See below
1100 * @fwnode: Optional fwnode of the interrupt controller
1101 * @ops: Pointer to the interrupt domain callbacks
1102 * @host_data: Controller private data pointer
1103 *
1104 * If @size is 0 a tree domain is created, otherwise a linear domain.
1105 *
1106 * If successful the parent is associated to the new domain and the
1107 * domain flags are set.
1108 * Returns pointer to IRQ domain, or NULL on failure.
1109 */
1110struct irq_domain *irq_domain_create_hierarchy(struct irq_domain *parent,
1111 unsigned int flags,
1112 unsigned int size,
1113 struct fwnode_handle *fwnode,
1114 const struct irq_domain_ops *ops,
1115 void *host_data)
1116{
1117 struct irq_domain *domain;
1118
1119 if (size)
1120 domain = __irq_domain_create(fwnode, size, size, 0, ops, host_data);
1121 else
1122 domain = __irq_domain_create(fwnode, 0, ~0, 0, ops, host_data);
1123
1124 if (domain) {
1125 domain->parent = parent;
1126 domain->flags |= flags;
1127
1128 __irq_domain_publish(domain);
1129 }
1130
1131 return domain;
1132}
1133EXPORT_SYMBOL_GPL(irq_domain_create_hierarchy);
1134
1135static void irq_domain_insert_irq(int virq)
1136{
1137 struct irq_data *data;
1138
1139 for (data = irq_get_irq_data(virq); data; data = data->parent_data) {
1140 struct irq_domain *domain = data->domain;
1141
1142 domain->mapcount++;
1143 irq_domain_set_mapping(domain, data->hwirq, data);
1144
1145 /* If not already assigned, give the domain the chip's name */
1146 if (!domain->name && data->chip)
1147 domain->name = data->chip->name;
1148 }
1149
1150 irq_clear_status_flags(virq, IRQ_NOREQUEST);
1151}
1152
1153static void irq_domain_remove_irq(int virq)
1154{
1155 struct irq_data *data;
1156
1157 irq_set_status_flags(virq, IRQ_NOREQUEST);
1158 irq_set_chip_and_handler(virq, NULL, NULL);
1159 synchronize_irq(virq);
1160 smp_mb();
1161
1162 for (data = irq_get_irq_data(virq); data; data = data->parent_data) {
1163 struct irq_domain *domain = data->domain;
1164 irq_hw_number_t hwirq = data->hwirq;
1165
1166 domain->mapcount--;
1167 irq_domain_clear_mapping(domain, hwirq);
1168 }
1169}
1170
1171static struct irq_data *irq_domain_insert_irq_data(struct irq_domain *domain,
1172 struct irq_data *child)
1173{
1174 struct irq_data *irq_data;
1175
1176 irq_data = kzalloc_node(sizeof(*irq_data), GFP_KERNEL,
1177 irq_data_get_node(child));
1178 if (irq_data) {
1179 child->parent_data = irq_data;
1180 irq_data->irq = child->irq;
1181 irq_data->common = child->common;
1182 irq_data->domain = domain;
1183 }
1184
1185 return irq_data;
1186}
1187
1188static void irq_domain_free_irq_data(unsigned int virq, unsigned int nr_irqs)
1189{
1190 struct irq_data *irq_data, *tmp;
1191 int i;
1192
1193 for (i = 0; i < nr_irqs; i++) {
1194 irq_data = irq_get_irq_data(virq + i);
1195 tmp = irq_data->parent_data;
1196 irq_data->parent_data = NULL;
1197 irq_data->domain = NULL;
1198
1199 while (tmp) {
1200 irq_data = tmp;
1201 tmp = tmp->parent_data;
1202 kfree(irq_data);
1203 }
1204 }
1205}
1206
1207static int irq_domain_alloc_irq_data(struct irq_domain *domain,
1208 unsigned int virq, unsigned int nr_irqs)
1209{
1210 struct irq_data *irq_data;
1211 struct irq_domain *parent;
1212 int i;
1213
1214 /* The outermost irq_data is embedded in struct irq_desc */
1215 for (i = 0; i < nr_irqs; i++) {
1216 irq_data = irq_get_irq_data(virq + i);
1217 irq_data->domain = domain;
1218
1219 for (parent = domain->parent; parent; parent = parent->parent) {
1220 irq_data = irq_domain_insert_irq_data(parent, irq_data);
1221 if (!irq_data) {
1222 irq_domain_free_irq_data(virq, i + 1);
1223 return -ENOMEM;
1224 }
1225 }
1226 }
1227
1228 return 0;
1229}
1230
1231/**
1232 * irq_domain_get_irq_data - Get irq_data associated with @virq and @domain
1233 * @domain: domain to match
1234 * @virq: IRQ number to get irq_data
1235 */
1236struct irq_data *irq_domain_get_irq_data(struct irq_domain *domain,
1237 unsigned int virq)
1238{
1239 struct irq_data *irq_data;
1240
1241 for (irq_data = irq_get_irq_data(virq); irq_data;
1242 irq_data = irq_data->parent_data)
1243 if (irq_data->domain == domain)
1244 return irq_data;
1245
1246 return NULL;
1247}
1248EXPORT_SYMBOL_GPL(irq_domain_get_irq_data);
1249
1250/**
1251 * irq_domain_set_hwirq_and_chip - Set hwirq and irqchip of @virq at @domain
1252 * @domain: Interrupt domain to match
1253 * @virq: IRQ number
1254 * @hwirq: The hwirq number
1255 * @chip: The associated interrupt chip
1256 * @chip_data: The associated chip data
1257 */
1258int irq_domain_set_hwirq_and_chip(struct irq_domain *domain, unsigned int virq,
1259 irq_hw_number_t hwirq, struct irq_chip *chip,
1260 void *chip_data)
1261{
1262 struct irq_data *irq_data = irq_domain_get_irq_data(domain, virq);
1263
1264 if (!irq_data)
1265 return -ENOENT;
1266
1267 irq_data->hwirq = hwirq;
1268 irq_data->chip = chip ? chip : &no_irq_chip;
1269 irq_data->chip_data = chip_data;
1270
1271 return 0;
1272}
1273EXPORT_SYMBOL_GPL(irq_domain_set_hwirq_and_chip);
1274
1275/**
1276 * irq_domain_set_info - Set the complete data for a @virq in @domain
1277 * @domain: Interrupt domain to match
1278 * @virq: IRQ number
1279 * @hwirq: The hardware interrupt number
1280 * @chip: The associated interrupt chip
1281 * @chip_data: The associated interrupt chip data
1282 * @handler: The interrupt flow handler
1283 * @handler_data: The interrupt flow handler data
1284 * @handler_name: The interrupt handler name
1285 */
1286void irq_domain_set_info(struct irq_domain *domain, unsigned int virq,
1287 irq_hw_number_t hwirq, struct irq_chip *chip,
1288 void *chip_data, irq_flow_handler_t handler,
1289 void *handler_data, const char *handler_name)
1290{
1291 irq_domain_set_hwirq_and_chip(domain, virq, hwirq, chip, chip_data);
1292 __irq_set_handler(virq, handler, 0, handler_name);
1293 irq_set_handler_data(virq, handler_data);
1294}
1295EXPORT_SYMBOL(irq_domain_set_info);
1296
1297/**
1298 * irq_domain_reset_irq_data - Clear hwirq, chip and chip_data in @irq_data
1299 * @irq_data: The pointer to irq_data
1300 */
1301void irq_domain_reset_irq_data(struct irq_data *irq_data)
1302{
1303 irq_data->hwirq = 0;
1304 irq_data->chip = &no_irq_chip;
1305 irq_data->chip_data = NULL;
1306}
1307EXPORT_SYMBOL_GPL(irq_domain_reset_irq_data);
1308
1309/**
1310 * irq_domain_free_irqs_common - Clear irq_data and free the parent
1311 * @domain: Interrupt domain to match
1312 * @virq: IRQ number to start with
1313 * @nr_irqs: The number of irqs to free
1314 */
1315void irq_domain_free_irqs_common(struct irq_domain *domain, unsigned int virq,
1316 unsigned int nr_irqs)
1317{
1318 struct irq_data *irq_data;
1319 int i;
1320
1321 for (i = 0; i < nr_irqs; i++) {
1322 irq_data = irq_domain_get_irq_data(domain, virq + i);
1323 if (irq_data)
1324 irq_domain_reset_irq_data(irq_data);
1325 }
1326 irq_domain_free_irqs_parent(domain, virq, nr_irqs);
1327}
1328EXPORT_SYMBOL_GPL(irq_domain_free_irqs_common);
1329
1330/**
1331 * irq_domain_free_irqs_top - Clear handler and handler data, clear irqdata and free parent
1332 * @domain: Interrupt domain to match
1333 * @virq: IRQ number to start with
1334 * @nr_irqs: The number of irqs to free
1335 */
1336void irq_domain_free_irqs_top(struct irq_domain *domain, unsigned int virq,
1337 unsigned int nr_irqs)
1338{
1339 int i;
1340
1341 for (i = 0; i < nr_irqs; i++) {
1342 irq_set_handler_data(virq + i, NULL);
1343 irq_set_handler(virq + i, NULL);
1344 }
1345 irq_domain_free_irqs_common(domain, virq, nr_irqs);
1346}
1347
1348static void irq_domain_free_irqs_hierarchy(struct irq_domain *domain,
1349 unsigned int irq_base,
1350 unsigned int nr_irqs)
1351{
1352 unsigned int i;
1353
1354 if (!domain->ops->free)
1355 return;
1356
1357 for (i = 0; i < nr_irqs; i++) {
1358 if (irq_domain_get_irq_data(domain, irq_base + i))
1359 domain->ops->free(domain, irq_base + i, 1);
1360 }
1361}
1362
1363int irq_domain_alloc_irqs_hierarchy(struct irq_domain *domain,
1364 unsigned int irq_base,
1365 unsigned int nr_irqs, void *arg)
1366{
1367 if (!domain->ops->alloc) {
1368 pr_debug("domain->ops->alloc() is NULL\n");
1369 return -ENOSYS;
1370 }
1371
1372 return domain->ops->alloc(domain, irq_base, nr_irqs, arg);
1373}
1374
1375static int irq_domain_alloc_irqs_locked(struct irq_domain *domain, int irq_base,
1376 unsigned int nr_irqs, int node, void *arg,
1377 bool realloc, const struct irq_affinity_desc *affinity)
1378{
1379 int i, ret, virq;
1380
1381 if (realloc && irq_base >= 0) {
1382 virq = irq_base;
1383 } else {
1384 virq = irq_domain_alloc_descs(irq_base, nr_irqs, 0, node,
1385 affinity);
1386 if (virq < 0) {
1387 pr_debug("cannot allocate IRQ(base %d, count %d)\n",
1388 irq_base, nr_irqs);
1389 return virq;
1390 }
1391 }
1392
1393 if (irq_domain_alloc_irq_data(domain, virq, nr_irqs)) {
1394 pr_debug("cannot allocate memory for IRQ%d\n", virq);
1395 ret = -ENOMEM;
1396 goto out_free_desc;
1397 }
1398
1399 ret = irq_domain_alloc_irqs_hierarchy(domain, virq, nr_irqs, arg);
1400 if (ret < 0)
1401 goto out_free_irq_data;
1402 for (i = 0; i < nr_irqs; i++)
1403 irq_domain_insert_irq(virq + i);
1404
1405 return virq;
1406
1407out_free_irq_data:
1408 irq_domain_free_irq_data(virq, nr_irqs);
1409out_free_desc:
1410 irq_free_descs(virq, nr_irqs);
1411 return ret;
1412}
1413
1414/**
1415 * __irq_domain_alloc_irqs - Allocate IRQs from domain
1416 * @domain: domain to allocate from
1417 * @irq_base: allocate specified IRQ number if irq_base >= 0
1418 * @nr_irqs: number of IRQs to allocate
1419 * @node: NUMA node id for memory allocation
1420 * @arg: domain specific argument
1421 * @realloc: IRQ descriptors have already been allocated if true
1422 * @affinity: Optional irq affinity mask for multiqueue devices
1423 *
1424 * Allocate IRQ numbers and initialized all data structures to support
1425 * hierarchy IRQ domains.
1426 * Parameter @realloc is mainly to support legacy IRQs.
1427 * Returns error code or allocated IRQ number
1428 *
1429 * The whole process to setup an IRQ has been split into two steps.
1430 * The first step, __irq_domain_alloc_irqs(), is to allocate IRQ
1431 * descriptor and required hardware resources. The second step,
1432 * irq_domain_activate_irq(), is to program hardwares with preallocated
1433 * resources. In this way, it's easier to rollback when failing to
1434 * allocate resources.
1435 */
1436int __irq_domain_alloc_irqs(struct irq_domain *domain, int irq_base,
1437 unsigned int nr_irqs, int node, void *arg,
1438 bool realloc, const struct irq_affinity_desc *affinity)
1439{
1440 int ret;
1441
1442 if (domain == NULL) {
1443 domain = irq_default_domain;
1444 if (WARN(!domain, "domain is NULL; cannot allocate IRQ\n"))
1445 return -EINVAL;
1446 }
1447
1448 mutex_lock(&irq_domain_mutex);
1449 ret = irq_domain_alloc_irqs_locked(domain, irq_base, nr_irqs, node, arg,
1450 realloc, affinity);
1451 mutex_unlock(&irq_domain_mutex);
1452
1453 return ret;
1454}
1455
1456/* The irq_data was moved, fix the revmap to refer to the new location */
1457static void irq_domain_fix_revmap(struct irq_data *d)
1458{
1459 void __rcu **slot;
1460
1461 if (d->hwirq < d->domain->revmap_size)
1462 return; /* Not using radix tree. */
1463
1464 /* Fix up the revmap. */
1465 mutex_lock(&d->domain->revmap_tree_mutex);
1466 slot = radix_tree_lookup_slot(&d->domain->revmap_tree, d->hwirq);
1467 if (slot)
1468 radix_tree_replace_slot(&d->domain->revmap_tree, slot, d);
1469 mutex_unlock(&d->domain->revmap_tree_mutex);
1470}
1471
1472/**
1473 * irq_domain_push_irq() - Push a domain in to the top of a hierarchy.
1474 * @domain: Domain to push.
1475 * @virq: Irq to push the domain in to.
1476 * @arg: Passed to the irq_domain_ops alloc() function.
1477 *
1478 * For an already existing irqdomain hierarchy, as might be obtained
1479 * via a call to pci_enable_msix(), add an additional domain to the
1480 * head of the processing chain. Must be called before request_irq()
1481 * has been called.
1482 */
1483int irq_domain_push_irq(struct irq_domain *domain, int virq, void *arg)
1484{
1485 struct irq_data *child_irq_data;
1486 struct irq_data *root_irq_data = irq_get_irq_data(virq);
1487 struct irq_desc *desc;
1488 int rv = 0;
1489
1490 /*
1491 * Check that no action has been set, which indicates the virq
1492 * is in a state where this function doesn't have to deal with
1493 * races between interrupt handling and maintaining the
1494 * hierarchy. This will catch gross misuse. Attempting to
1495 * make the check race free would require holding locks across
1496 * calls to struct irq_domain_ops->alloc(), which could lead
1497 * to deadlock, so we just do a simple check before starting.
1498 */
1499 desc = irq_to_desc(virq);
1500 if (!desc)
1501 return -EINVAL;
1502 if (WARN_ON(desc->action))
1503 return -EBUSY;
1504
1505 if (domain == NULL)
1506 return -EINVAL;
1507
1508 if (WARN_ON(!irq_domain_is_hierarchy(domain)))
1509 return -EINVAL;
1510
1511 if (!root_irq_data)
1512 return -EINVAL;
1513
1514 if (domain->parent != root_irq_data->domain)
1515 return -EINVAL;
1516
1517 child_irq_data = kzalloc_node(sizeof(*child_irq_data), GFP_KERNEL,
1518 irq_data_get_node(root_irq_data));
1519 if (!child_irq_data)
1520 return -ENOMEM;
1521
1522 mutex_lock(&irq_domain_mutex);
1523
1524 /* Copy the original irq_data. */
1525 *child_irq_data = *root_irq_data;
1526
1527 /*
1528 * Overwrite the root_irq_data, which is embedded in struct
1529 * irq_desc, with values for this domain.
1530 */
1531 root_irq_data->parent_data = child_irq_data;
1532 root_irq_data->domain = domain;
1533 root_irq_data->mask = 0;
1534 root_irq_data->hwirq = 0;
1535 root_irq_data->chip = NULL;
1536 root_irq_data->chip_data = NULL;
1537
1538 /* May (probably does) set hwirq, chip, etc. */
1539 rv = irq_domain_alloc_irqs_hierarchy(domain, virq, 1, arg);
1540 if (rv) {
1541 /* Restore the original irq_data. */
1542 *root_irq_data = *child_irq_data;
1543 kfree(child_irq_data);
1544 goto error;
1545 }
1546
1547 irq_domain_fix_revmap(child_irq_data);
1548 irq_domain_set_mapping(domain, root_irq_data->hwirq, root_irq_data);
1549
1550error:
1551 mutex_unlock(&irq_domain_mutex);
1552
1553 return rv;
1554}
1555EXPORT_SYMBOL_GPL(irq_domain_push_irq);
1556
1557/**
1558 * irq_domain_pop_irq() - Remove a domain from the top of a hierarchy.
1559 * @domain: Domain to remove.
1560 * @virq: Irq to remove the domain from.
1561 *
1562 * Undo the effects of a call to irq_domain_push_irq(). Must be
1563 * called either before request_irq() or after free_irq().
1564 */
1565int irq_domain_pop_irq(struct irq_domain *domain, int virq)
1566{
1567 struct irq_data *root_irq_data = irq_get_irq_data(virq);
1568 struct irq_data *child_irq_data;
1569 struct irq_data *tmp_irq_data;
1570 struct irq_desc *desc;
1571
1572 /*
1573 * Check that no action is set, which indicates the virq is in
1574 * a state where this function doesn't have to deal with races
1575 * between interrupt handling and maintaining the hierarchy.
1576 * This will catch gross misuse. Attempting to make the check
1577 * race free would require holding locks across calls to
1578 * struct irq_domain_ops->free(), which could lead to
1579 * deadlock, so we just do a simple check before starting.
1580 */
1581 desc = irq_to_desc(virq);
1582 if (!desc)
1583 return -EINVAL;
1584 if (WARN_ON(desc->action))
1585 return -EBUSY;
1586
1587 if (domain == NULL)
1588 return -EINVAL;
1589
1590 if (!root_irq_data)
1591 return -EINVAL;
1592
1593 tmp_irq_data = irq_domain_get_irq_data(domain, virq);
1594
1595 /* We can only "pop" if this domain is at the top of the list */
1596 if (WARN_ON(root_irq_data != tmp_irq_data))
1597 return -EINVAL;
1598
1599 if (WARN_ON(root_irq_data->domain != domain))
1600 return -EINVAL;
1601
1602 child_irq_data = root_irq_data->parent_data;
1603 if (WARN_ON(!child_irq_data))
1604 return -EINVAL;
1605
1606 mutex_lock(&irq_domain_mutex);
1607
1608 root_irq_data->parent_data = NULL;
1609
1610 irq_domain_clear_mapping(domain, root_irq_data->hwirq);
1611 irq_domain_free_irqs_hierarchy(domain, virq, 1);
1612
1613 /* Restore the original irq_data. */
1614 *root_irq_data = *child_irq_data;
1615
1616 irq_domain_fix_revmap(root_irq_data);
1617
1618 mutex_unlock(&irq_domain_mutex);
1619
1620 kfree(child_irq_data);
1621
1622 return 0;
1623}
1624EXPORT_SYMBOL_GPL(irq_domain_pop_irq);
1625
1626/**
1627 * irq_domain_free_irqs - Free IRQ number and associated data structures
1628 * @virq: base IRQ number
1629 * @nr_irqs: number of IRQs to free
1630 */
1631void irq_domain_free_irqs(unsigned int virq, unsigned int nr_irqs)
1632{
1633 struct irq_data *data = irq_get_irq_data(virq);
1634 int i;
1635
1636 if (WARN(!data || !data->domain || !data->domain->ops->free,
1637 "NULL pointer, cannot free irq\n"))
1638 return;
1639
1640 mutex_lock(&irq_domain_mutex);
1641 for (i = 0; i < nr_irqs; i++)
1642 irq_domain_remove_irq(virq + i);
1643 irq_domain_free_irqs_hierarchy(data->domain, virq, nr_irqs);
1644 mutex_unlock(&irq_domain_mutex);
1645
1646 irq_domain_free_irq_data(virq, nr_irqs);
1647 irq_free_descs(virq, nr_irqs);
1648}
1649
1650/**
1651 * irq_domain_alloc_irqs_parent - Allocate interrupts from parent domain
1652 * @irq_base: Base IRQ number
1653 * @nr_irqs: Number of IRQs to allocate
1654 * @arg: Allocation data (arch/domain specific)
1655 *
1656 * Check whether the domain has been setup recursive. If not allocate
1657 * through the parent domain.
1658 */
1659int irq_domain_alloc_irqs_parent(struct irq_domain *domain,
1660 unsigned int irq_base, unsigned int nr_irqs,
1661 void *arg)
1662{
1663 if (!domain->parent)
1664 return -ENOSYS;
1665
1666 return irq_domain_alloc_irqs_hierarchy(domain->parent, irq_base,
1667 nr_irqs, arg);
1668}
1669EXPORT_SYMBOL_GPL(irq_domain_alloc_irqs_parent);
1670
1671/**
1672 * irq_domain_free_irqs_parent - Free interrupts from parent domain
1673 * @irq_base: Base IRQ number
1674 * @nr_irqs: Number of IRQs to free
1675 *
1676 * Check whether the domain has been setup recursive. If not free
1677 * through the parent domain.
1678 */
1679void irq_domain_free_irqs_parent(struct irq_domain *domain,
1680 unsigned int irq_base, unsigned int nr_irqs)
1681{
1682 if (!domain->parent)
1683 return;
1684
1685 irq_domain_free_irqs_hierarchy(domain->parent, irq_base, nr_irqs);
1686}
1687EXPORT_SYMBOL_GPL(irq_domain_free_irqs_parent);
1688
1689static void __irq_domain_deactivate_irq(struct irq_data *irq_data)
1690{
1691 if (irq_data && irq_data->domain) {
1692 struct irq_domain *domain = irq_data->domain;
1693
1694 if (domain->ops->deactivate)
1695 domain->ops->deactivate(domain, irq_data);
1696 if (irq_data->parent_data)
1697 __irq_domain_deactivate_irq(irq_data->parent_data);
1698 }
1699}
1700
1701static int __irq_domain_activate_irq(struct irq_data *irqd, bool reserve)
1702{
1703 int ret = 0;
1704
1705 if (irqd && irqd->domain) {
1706 struct irq_domain *domain = irqd->domain;
1707
1708 if (irqd->parent_data)
1709 ret = __irq_domain_activate_irq(irqd->parent_data,
1710 reserve);
1711 if (!ret && domain->ops->activate) {
1712 ret = domain->ops->activate(domain, irqd, reserve);
1713 /* Rollback in case of error */
1714 if (ret && irqd->parent_data)
1715 __irq_domain_deactivate_irq(irqd->parent_data);
1716 }
1717 }
1718 return ret;
1719}
1720
1721/**
1722 * irq_domain_activate_irq - Call domain_ops->activate recursively to activate
1723 * interrupt
1724 * @irq_data: Outermost irq_data associated with interrupt
1725 * @reserve: If set only reserve an interrupt vector instead of assigning one
1726 *
1727 * This is the second step to call domain_ops->activate to program interrupt
1728 * controllers, so the interrupt could actually get delivered.
1729 */
1730int irq_domain_activate_irq(struct irq_data *irq_data, bool reserve)
1731{
1732 int ret = 0;
1733
1734 if (!irqd_is_activated(irq_data))
1735 ret = __irq_domain_activate_irq(irq_data, reserve);
1736 if (!ret)
1737 irqd_set_activated(irq_data);
1738 return ret;
1739}
1740
1741/**
1742 * irq_domain_deactivate_irq - Call domain_ops->deactivate recursively to
1743 * deactivate interrupt
1744 * @irq_data: outermost irq_data associated with interrupt
1745 *
1746 * It calls domain_ops->deactivate to program interrupt controllers to disable
1747 * interrupt delivery.
1748 */
1749void irq_domain_deactivate_irq(struct irq_data *irq_data)
1750{
1751 if (irqd_is_activated(irq_data)) {
1752 __irq_domain_deactivate_irq(irq_data);
1753 irqd_clr_activated(irq_data);
1754 }
1755}
1756
1757static void irq_domain_check_hierarchy(struct irq_domain *domain)
1758{
1759 /* Hierarchy irq_domains must implement callback alloc() */
1760 if (domain->ops->alloc)
1761 domain->flags |= IRQ_DOMAIN_FLAG_HIERARCHY;
1762}
1763
1764/**
1765 * irq_domain_hierarchical_is_msi_remap - Check if the domain or any
1766 * parent has MSI remapping support
1767 * @domain: domain pointer
1768 */
1769bool irq_domain_hierarchical_is_msi_remap(struct irq_domain *domain)
1770{
1771 for (; domain; domain = domain->parent) {
1772 if (irq_domain_is_msi_remap(domain))
1773 return true;
1774 }
1775 return false;
1776}
1777#else /* CONFIG_IRQ_DOMAIN_HIERARCHY */
1778/**
1779 * irq_domain_get_irq_data - Get irq_data associated with @virq and @domain
1780 * @domain: domain to match
1781 * @virq: IRQ number to get irq_data
1782 */
1783struct irq_data *irq_domain_get_irq_data(struct irq_domain *domain,
1784 unsigned int virq)
1785{
1786 struct irq_data *irq_data = irq_get_irq_data(virq);
1787
1788 return (irq_data && irq_data->domain == domain) ? irq_data : NULL;
1789}
1790EXPORT_SYMBOL_GPL(irq_domain_get_irq_data);
1791
1792/**
1793 * irq_domain_set_info - Set the complete data for a @virq in @domain
1794 * @domain: Interrupt domain to match
1795 * @virq: IRQ number
1796 * @hwirq: The hardware interrupt number
1797 * @chip: The associated interrupt chip
1798 * @chip_data: The associated interrupt chip data
1799 * @handler: The interrupt flow handler
1800 * @handler_data: The interrupt flow handler data
1801 * @handler_name: The interrupt handler name
1802 */
1803void irq_domain_set_info(struct irq_domain *domain, unsigned int virq,
1804 irq_hw_number_t hwirq, struct irq_chip *chip,
1805 void *chip_data, irq_flow_handler_t handler,
1806 void *handler_data, const char *handler_name)
1807{
1808 irq_set_chip_and_handler_name(virq, chip, handler, handler_name);
1809 irq_set_chip_data(virq, chip_data);
1810 irq_set_handler_data(virq, handler_data);
1811}
1812
1813static int irq_domain_alloc_irqs_locked(struct irq_domain *domain, int irq_base,
1814 unsigned int nr_irqs, int node, void *arg,
1815 bool realloc, const struct irq_affinity_desc *affinity)
1816{
1817 return -EINVAL;
1818}
1819
1820static void irq_domain_check_hierarchy(struct irq_domain *domain)
1821{
1822}
1823#endif /* CONFIG_IRQ_DOMAIN_HIERARCHY */
1824
1825#ifdef CONFIG_GENERIC_IRQ_DEBUGFS
1826static struct dentry *domain_dir;
1827
1828static void
1829irq_domain_debug_show_one(struct seq_file *m, struct irq_domain *d, int ind)
1830{
1831 seq_printf(m, "%*sname: %s\n", ind, "", d->name);
1832 seq_printf(m, "%*ssize: %u\n", ind + 1, "",
1833 d->revmap_size + d->revmap_direct_max_irq);
1834 seq_printf(m, "%*smapped: %u\n", ind + 1, "", d->mapcount);
1835 seq_printf(m, "%*sflags: 0x%08x\n", ind +1 , "", d->flags);
1836 if (d->ops && d->ops->debug_show)
1837 d->ops->debug_show(m, d, NULL, ind + 1);
1838#ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
1839 if (!d->parent)
1840 return;
1841 seq_printf(m, "%*sparent: %s\n", ind + 1, "", d->parent->name);
1842 irq_domain_debug_show_one(m, d->parent, ind + 4);
1843#endif
1844}
1845
1846static int irq_domain_debug_show(struct seq_file *m, void *p)
1847{
1848 struct irq_domain *d = m->private;
1849
1850 /* Default domain? Might be NULL */
1851 if (!d) {
1852 if (!irq_default_domain)
1853 return 0;
1854 d = irq_default_domain;
1855 }
1856 irq_domain_debug_show_one(m, d, 0);
1857 return 0;
1858}
1859DEFINE_SHOW_ATTRIBUTE(irq_domain_debug);
1860
1861static void debugfs_add_domain_dir(struct irq_domain *d)
1862{
1863 if (!d->name || !domain_dir || d->debugfs_file)
1864 return;
1865 d->debugfs_file = debugfs_create_file(d->name, 0444, domain_dir, d,
1866 &irq_domain_debug_fops);
1867}
1868
1869static void debugfs_remove_domain_dir(struct irq_domain *d)
1870{
1871 debugfs_remove(d->debugfs_file);
1872 d->debugfs_file = NULL;
1873}
1874
1875void __init irq_domain_debugfs_init(struct dentry *root)
1876{
1877 struct irq_domain *d;
1878
1879 domain_dir = debugfs_create_dir("domains", root);
1880
1881 debugfs_create_file("default", 0444, domain_dir, NULL,
1882 &irq_domain_debug_fops);
1883 mutex_lock(&irq_domain_mutex);
1884 list_for_each_entry(d, &irq_domain_list, link)
1885 debugfs_add_domain_dir(d);
1886 mutex_unlock(&irq_domain_mutex);
1887}
1888#endif