152a65ff5SThomas Gleixner // SPDX-License-Identifier: GPL-2.0
252a65ff5SThomas Gleixner
354a90588SPaul Mundt #define pr_fmt(fmt) "irq: " fmt
454a90588SPaul Mundt
5c5c601c4SMarc Zyngier #include <linux/acpi.h>
6cc79ca69SGrant Likely #include <linux/debugfs.h>
7cc79ca69SGrant Likely #include <linux/hardirq.h>
8cc79ca69SGrant Likely #include <linux/interrupt.h>
908a543adSGrant Likely #include <linux/irq.h>
10cc79ca69SGrant Likely #include <linux/irqdesc.h>
1108a543adSGrant Likely #include <linux/irqdomain.h>
1208a543adSGrant Likely #include <linux/module.h>
1308a543adSGrant Likely #include <linux/mutex.h>
1408a543adSGrant Likely #include <linux/of.h>
157e713301SGrant Likely #include <linux/of_address.h>
1664be38abSRashika Kheria #include <linux/of_irq.h>
175ca4db61SPaul Mundt #include <linux/topology.h>
18cc79ca69SGrant Likely #include <linux/seq_file.h>
197e713301SGrant Likely #include <linux/slab.h>
20cc79ca69SGrant Likely #include <linux/smp.h>
21cc79ca69SGrant Likely #include <linux/fs.h>
2208a543adSGrant Likely
2308a543adSGrant Likely static LIST_HEAD(irq_domain_list);
2408a543adSGrant Likely static DEFINE_MUTEX(irq_domain_mutex);
2508a543adSGrant Likely
2668700650SGrant Likely static struct irq_domain *irq_default_domain;
27cc79ca69SGrant Likely
28601363ccSJohan Hovold static int irq_domain_alloc_irqs_locked(struct irq_domain *domain, int irq_base,
29601363ccSJohan Hovold unsigned int nr_irqs, int node, void *arg,
30601363ccSJohan Hovold bool realloc, const struct irq_affinity_desc *affinity);
31f8264e34SJiang Liu static void irq_domain_check_hierarchy(struct irq_domain *domain);
32f8264e34SJiang Liu
33b145dcc4SMarc Zyngier struct irqchip_fwid {
34b145dcc4SMarc Zyngier struct fwnode_handle fwnode;
35d59f6617SThomas Gleixner unsigned int type;
36b145dcc4SMarc Zyngier char *name;
37b977fcf4SMarc Zyngier phys_addr_t *pa;
38b145dcc4SMarc Zyngier };
39b145dcc4SMarc Zyngier
40087cdfb6SThomas Gleixner #ifdef CONFIG_GENERIC_IRQ_DEBUGFS
41087cdfb6SThomas Gleixner static void debugfs_add_domain_dir(struct irq_domain *d);
42087cdfb6SThomas Gleixner static void debugfs_remove_domain_dir(struct irq_domain *d);
43087cdfb6SThomas Gleixner #else
debugfs_add_domain_dir(struct irq_domain * d)44087cdfb6SThomas Gleixner static inline void debugfs_add_domain_dir(struct irq_domain *d) { }
debugfs_remove_domain_dir(struct irq_domain * d)45087cdfb6SThomas Gleixner static inline void debugfs_remove_domain_dir(struct irq_domain *d) { }
46087cdfb6SThomas Gleixner #endif
47087cdfb6SThomas Gleixner
irqchip_fwnode_get_name(const struct fwnode_handle * fwnode)482cbd5a45SDavid Woodhouse static const char *irqchip_fwnode_get_name(const struct fwnode_handle *fwnode)
492cbd5a45SDavid Woodhouse {
502cbd5a45SDavid Woodhouse struct irqchip_fwid *fwid = container_of(fwnode, struct irqchip_fwid, fwnode);
512cbd5a45SDavid Woodhouse
522cbd5a45SDavid Woodhouse return fwid->name;
532cbd5a45SDavid Woodhouse }
542cbd5a45SDavid Woodhouse
552cbd5a45SDavid Woodhouse const struct fwnode_operations irqchip_fwnode_ops = {
562cbd5a45SDavid Woodhouse .get_name = irqchip_fwnode_get_name,
572cbd5a45SDavid Woodhouse };
58b6eb66fdSArnd Bergmann EXPORT_SYMBOL_GPL(irqchip_fwnode_ops);
59db3e50f3SSakari Ailus
60b145dcc4SMarc Zyngier /**
61b513df67Sluanshi * __irq_domain_alloc_fwnode - Allocate a fwnode_handle suitable for
62b145dcc4SMarc Zyngier * identifying an irq domain
63d59f6617SThomas Gleixner * @type: Type of irqchip_fwnode. See linux/irqdomain.h
64d59f6617SThomas Gleixner * @id: Optional user provided id if name != NULL
65b513df67Sluanshi * @name: Optional user provided domain name
660ed9ca25SYi Wang * @pa: Optional user-provided physical address
67b145dcc4SMarc Zyngier *
68a359f757SIngo Molnar * Allocate a struct irqchip_fwid, and return a pointer to the embedded
69b145dcc4SMarc Zyngier * fwnode_handle (or NULL on failure).
70d59f6617SThomas Gleixner *
71d59f6617SThomas Gleixner * Note: The types IRQCHIP_FWNODE_NAMED and IRQCHIP_FWNODE_NAMED_ID are
72d59f6617SThomas Gleixner * solely to transport name information to irqdomain creation code. The
73d59f6617SThomas Gleixner * node is not stored. For other types the pointer is kept in the irq
74d59f6617SThomas Gleixner * domain struct.
75b145dcc4SMarc Zyngier */
__irq_domain_alloc_fwnode(unsigned int type,int id,const char * name,phys_addr_t * pa)76d59f6617SThomas Gleixner struct fwnode_handle *__irq_domain_alloc_fwnode(unsigned int type, int id,
77b977fcf4SMarc Zyngier const char *name,
78b977fcf4SMarc Zyngier phys_addr_t *pa)
79b145dcc4SMarc Zyngier {
80b145dcc4SMarc Zyngier struct irqchip_fwid *fwid;
81d59f6617SThomas Gleixner char *n;
82b145dcc4SMarc Zyngier
83b145dcc4SMarc Zyngier fwid = kzalloc(sizeof(*fwid), GFP_KERNEL);
84b145dcc4SMarc Zyngier
85d59f6617SThomas Gleixner switch (type) {
86d59f6617SThomas Gleixner case IRQCHIP_FWNODE_NAMED:
87d59f6617SThomas Gleixner n = kasprintf(GFP_KERNEL, "%s", name);
88d59f6617SThomas Gleixner break;
89d59f6617SThomas Gleixner case IRQCHIP_FWNODE_NAMED_ID:
90d59f6617SThomas Gleixner n = kasprintf(GFP_KERNEL, "%s-%d", name, id);
91d59f6617SThomas Gleixner break;
92d59f6617SThomas Gleixner default:
93b977fcf4SMarc Zyngier n = kasprintf(GFP_KERNEL, "irqchip@%pa", pa);
94d59f6617SThomas Gleixner break;
95d59f6617SThomas Gleixner }
96d59f6617SThomas Gleixner
97d59f6617SThomas Gleixner if (!fwid || !n) {
98b145dcc4SMarc Zyngier kfree(fwid);
99d59f6617SThomas Gleixner kfree(n);
100b145dcc4SMarc Zyngier return NULL;
101b145dcc4SMarc Zyngier }
102b145dcc4SMarc Zyngier
103d59f6617SThomas Gleixner fwid->type = type;
104d59f6617SThomas Gleixner fwid->name = n;
105b977fcf4SMarc Zyngier fwid->pa = pa;
10601bb86b3SSaravana Kannan fwnode_init(&fwid->fwnode, &irqchip_fwnode_ops);
107b145dcc4SMarc Zyngier return &fwid->fwnode;
108b145dcc4SMarc Zyngier }
109d59f6617SThomas Gleixner EXPORT_SYMBOL_GPL(__irq_domain_alloc_fwnode);
110b145dcc4SMarc Zyngier
111b145dcc4SMarc Zyngier /**
112b145dcc4SMarc Zyngier * irq_domain_free_fwnode - Free a non-OF-backed fwnode_handle
113b145dcc4SMarc Zyngier *
114b145dcc4SMarc Zyngier * Free a fwnode_handle allocated with irq_domain_alloc_fwnode.
115b145dcc4SMarc Zyngier */
irq_domain_free_fwnode(struct fwnode_handle * fwnode)116b145dcc4SMarc Zyngier void irq_domain_free_fwnode(struct fwnode_handle *fwnode)
117b145dcc4SMarc Zyngier {
118b145dcc4SMarc Zyngier struct irqchip_fwid *fwid;
119b145dcc4SMarc Zyngier
120ac8f29aeSJason Gunthorpe if (!fwnode || WARN_ON(!is_fwnode_irqchip(fwnode)))
121b145dcc4SMarc Zyngier return;
122b145dcc4SMarc Zyngier
123b145dcc4SMarc Zyngier fwid = container_of(fwnode, struct irqchip_fwid, fwnode);
124b145dcc4SMarc Zyngier kfree(fwid->name);
125b145dcc4SMarc Zyngier kfree(fwid);
126b145dcc4SMarc Zyngier }
127a4289dc2SJake Oshins EXPORT_SYMBOL_GPL(irq_domain_free_fwnode);
128b145dcc4SMarc Zyngier
__irq_domain_create(struct fwnode_handle * fwnode,unsigned int size,irq_hw_number_t hwirq_max,int direct_max,const struct irq_domain_ops * ops,void * host_data)1298932c32cSMarc Zyngier static struct irq_domain *__irq_domain_create(struct fwnode_handle *fwnode,
1308932c32cSMarc Zyngier unsigned int size,
1318932c32cSMarc Zyngier irq_hw_number_t hwirq_max,
1328932c32cSMarc Zyngier int direct_max,
133a18dc81bSGrant Likely const struct irq_domain_ops *ops,
134a8db8cf0SGrant Likely void *host_data)
135cc79ca69SGrant Likely {
136d59f6617SThomas Gleixner struct irqchip_fwid *fwid;
137a8db8cf0SGrant Likely struct irq_domain *domain;
138cc79ca69SGrant Likely
139d59f6617SThomas Gleixner static atomic_t unknown_domains;
140d59f6617SThomas Gleixner
1414f86a06eSMarc Zyngier if (WARN_ON((size && direct_max) ||
142ef50cd57SXu Qiang (!IS_ENABLED(CONFIG_IRQ_DOMAIN_NOMAP) && direct_max) ||
143ef50cd57SXu Qiang (direct_max && (direct_max != hwirq_max))))
1444f86a06eSMarc Zyngier return NULL;
1454f86a06eSMarc Zyngier
146426fa316SMarc Zyngier domain = kzalloc_node(struct_size(domain, revmap, size),
147181e9d4eSAndy Shevchenko GFP_KERNEL, of_node_to_nid(to_of_node(fwnode)));
14843b98d87SGeert Uytterhoeven if (!domain)
149cc79ca69SGrant Likely return NULL;
150cc79ca69SGrant Likely
15145e9504fSZenghui Yu if (is_fwnode_irqchip(fwnode)) {
152d59f6617SThomas Gleixner fwid = container_of(fwnode, struct irqchip_fwid, fwnode);
153d59f6617SThomas Gleixner
154d59f6617SThomas Gleixner switch (fwid->type) {
155d59f6617SThomas Gleixner case IRQCHIP_FWNODE_NAMED:
156d59f6617SThomas Gleixner case IRQCHIP_FWNODE_NAMED_ID:
157d59f6617SThomas Gleixner domain->name = kstrdup(fwid->name, GFP_KERNEL);
158d59f6617SThomas Gleixner if (!domain->name) {
159d59f6617SThomas Gleixner kfree(domain);
160d59f6617SThomas Gleixner return NULL;
161d59f6617SThomas Gleixner }
162d59f6617SThomas Gleixner domain->flags |= IRQ_DOMAIN_NAME_ALLOCATED;
163d59f6617SThomas Gleixner break;
164d59f6617SThomas Gleixner default:
165d59f6617SThomas Gleixner domain->name = fwid->name;
166d59f6617SThomas Gleixner break;
167d59f6617SThomas Gleixner }
1689ed78b05SAndy Shevchenko } else if (is_of_node(fwnode) || is_acpi_device_node(fwnode) ||
1699ed78b05SAndy Shevchenko is_software_node(fwnode)) {
170d59f6617SThomas Gleixner char *name;
171d59f6617SThomas Gleixner
172d59f6617SThomas Gleixner /*
173181e9d4eSAndy Shevchenko * fwnode paths contain '/', which debugfs is legitimately
174d59f6617SThomas Gleixner * unhappy about. Replace them with ':', which does
175d59f6617SThomas Gleixner * the trick and is not as offensive as '\'...
176d59f6617SThomas Gleixner */
177181e9d4eSAndy Shevchenko name = kasprintf(GFP_KERNEL, "%pfw", fwnode);
178d59f6617SThomas Gleixner if (!name) {
179d59f6617SThomas Gleixner kfree(domain);
180d59f6617SThomas Gleixner return NULL;
181d59f6617SThomas Gleixner }
182d59f6617SThomas Gleixner
18367a4e1a3SAndy Shevchenko domain->name = strreplace(name, '/', ':');
184d59f6617SThomas Gleixner domain->flags |= IRQ_DOMAIN_NAME_ALLOCATED;
185d59f6617SThomas Gleixner }
186d59f6617SThomas Gleixner
187d59f6617SThomas Gleixner if (!domain->name) {
188db3e50f3SSakari Ailus if (fwnode)
189db3e50f3SSakari Ailus pr_err("Invalid fwnode type for irqdomain\n");
190d59f6617SThomas Gleixner domain->name = kasprintf(GFP_KERNEL, "unknown-%d",
191d59f6617SThomas Gleixner atomic_inc_return(&unknown_domains));
192d59f6617SThomas Gleixner if (!domain->name) {
193d59f6617SThomas Gleixner kfree(domain);
194d59f6617SThomas Gleixner return NULL;
195d59f6617SThomas Gleixner }
196d59f6617SThomas Gleixner domain->flags |= IRQ_DOMAIN_NAME_ALLOCATED;
197d59f6617SThomas Gleixner }
198d59f6617SThomas Gleixner
199*8b0e8b33SHerve Codina domain->fwnode = fwnode_handle_get(fwnode);
200*8b0e8b33SHerve Codina fwnode_dev_initialized(domain->fwnode, true);
201f110711aSMarc Zyngier
202cc79ca69SGrant Likely /* Fill structure */
2031aa0dd94SGrant Likely INIT_RADIX_TREE(&domain->revmap_tree, GFP_KERNEL);
20468700650SGrant Likely domain->ops = ops;
205a8db8cf0SGrant Likely domain->host_data = host_data;
206ddaf144cSGrant Likely domain->hwirq_max = hwirq_max;
2074f86a06eSMarc Zyngier
2084e0d86dfSJohan Hovold if (direct_max)
2094f86a06eSMarc Zyngier domain->flags |= IRQ_DOMAIN_FLAG_NO_MAP;
2104f86a06eSMarc Zyngier
2111aa0dd94SGrant Likely domain->revmap_size = size;
2124f86a06eSMarc Zyngier
2139dbb8e34SJohan Hovold /*
2149dbb8e34SJohan Hovold * Hierarchical domains use the domain lock of the root domain
2159dbb8e34SJohan Hovold * (innermost domain).
2169dbb8e34SJohan Hovold *
2179dbb8e34SJohan Hovold * For non-hierarchical domains (as for root domains), the root
2189dbb8e34SJohan Hovold * pointer is set to the domain itself so that &domain->root->mutex
2199dbb8e34SJohan Hovold * always points to the right lock.
2209dbb8e34SJohan Hovold */
2219dbb8e34SJohan Hovold mutex_init(&domain->mutex);
2229dbb8e34SJohan Hovold domain->root = domain;
2239dbb8e34SJohan Hovold
224f8264e34SJiang Liu irq_domain_check_hierarchy(domain);
225cc79ca69SGrant Likely
2268932c32cSMarc Zyngier return domain;
2278932c32cSMarc Zyngier }
2288932c32cSMarc Zyngier
__irq_domain_publish(struct irq_domain * domain)2298932c32cSMarc Zyngier static void __irq_domain_publish(struct irq_domain *domain)
2308932c32cSMarc Zyngier {
231a8db8cf0SGrant Likely mutex_lock(&irq_domain_mutex);
232087cdfb6SThomas Gleixner debugfs_add_domain_dir(domain);
233a8db8cf0SGrant Likely list_add(&domain->link, &irq_domain_list);
234a8db8cf0SGrant Likely mutex_unlock(&irq_domain_mutex);
235fa40f377SGrant Likely
2361aa0dd94SGrant Likely pr_debug("Added domain %s\n", domain->name);
2378932c32cSMarc Zyngier }
2388932c32cSMarc Zyngier
2398932c32cSMarc Zyngier /**
2408932c32cSMarc Zyngier * __irq_domain_add() - Allocate a new irq_domain data structure
2418932c32cSMarc Zyngier * @fwnode: firmware node for the interrupt controller
2428932c32cSMarc Zyngier * @size: Size of linear map; 0 for radix mapping only
2438932c32cSMarc Zyngier * @hwirq_max: Maximum number of interrupts supported by controller
2448932c32cSMarc Zyngier * @direct_max: Maximum value of direct maps; Use ~0 for no limit; 0 for no
2458932c32cSMarc Zyngier * direct mapping
2468932c32cSMarc Zyngier * @ops: domain callbacks
2478932c32cSMarc Zyngier * @host_data: Controller private data pointer
2488932c32cSMarc Zyngier *
2498932c32cSMarc Zyngier * Allocates and initializes an irq_domain structure.
2508932c32cSMarc Zyngier * Returns pointer to IRQ domain, or NULL on failure.
2518932c32cSMarc Zyngier */
__irq_domain_add(struct fwnode_handle * fwnode,unsigned int size,irq_hw_number_t hwirq_max,int direct_max,const struct irq_domain_ops * ops,void * host_data)2528932c32cSMarc Zyngier struct irq_domain *__irq_domain_add(struct fwnode_handle *fwnode, unsigned int size,
2538932c32cSMarc Zyngier irq_hw_number_t hwirq_max, int direct_max,
2548932c32cSMarc Zyngier const struct irq_domain_ops *ops,
2558932c32cSMarc Zyngier void *host_data)
2568932c32cSMarc Zyngier {
2578932c32cSMarc Zyngier struct irq_domain *domain;
2588932c32cSMarc Zyngier
2598932c32cSMarc Zyngier domain = __irq_domain_create(fwnode, size, hwirq_max, direct_max,
2608932c32cSMarc Zyngier ops, host_data);
2618932c32cSMarc Zyngier if (domain)
2628932c32cSMarc Zyngier __irq_domain_publish(domain);
2638932c32cSMarc Zyngier
264fa40f377SGrant Likely return domain;
265a8db8cf0SGrant Likely }
266fa40f377SGrant Likely EXPORT_SYMBOL_GPL(__irq_domain_add);
267a8db8cf0SGrant Likely
26858ee99adSPaul Mundt /**
26958ee99adSPaul Mundt * irq_domain_remove() - Remove an irq domain.
27058ee99adSPaul Mundt * @domain: domain to remove
27158ee99adSPaul Mundt *
27258ee99adSPaul Mundt * This routine is used to remove an irq domain. The caller must ensure
27358ee99adSPaul Mundt * that all mappings within the domain have been disposed of prior to
27458ee99adSPaul Mundt * use, depending on the revmap type.
27558ee99adSPaul Mundt */
irq_domain_remove(struct irq_domain * domain)27658ee99adSPaul Mundt void irq_domain_remove(struct irq_domain *domain)
27758ee99adSPaul Mundt {
27858ee99adSPaul Mundt mutex_lock(&irq_domain_mutex);
279087cdfb6SThomas Gleixner debugfs_remove_domain_dir(domain);
28058ee99adSPaul Mundt
281e9256efcSMatthew Wilcox WARN_ON(!radix_tree_empty(&domain->revmap_tree));
28258ee99adSPaul Mundt
28358ee99adSPaul Mundt list_del(&domain->link);
28458ee99adSPaul Mundt
28558ee99adSPaul Mundt /*
28658ee99adSPaul Mundt * If the going away domain is the default one, reset it.
28758ee99adSPaul Mundt */
28858ee99adSPaul Mundt if (unlikely(irq_default_domain == domain))
28958ee99adSPaul Mundt irq_set_default_host(NULL);
29058ee99adSPaul Mundt
29158ee99adSPaul Mundt mutex_unlock(&irq_domain_mutex);
29258ee99adSPaul Mundt
2931aa0dd94SGrant Likely pr_debug("Removed domain %s\n", domain->name);
29458ee99adSPaul Mundt
295ed1054a0SSaravana Kannan fwnode_dev_initialized(domain->fwnode, false);
296181e9d4eSAndy Shevchenko fwnode_handle_put(domain->fwnode);
297d59f6617SThomas Gleixner if (domain->flags & IRQ_DOMAIN_NAME_ALLOCATED)
298d59f6617SThomas Gleixner kfree(domain->name);
299fa40f377SGrant Likely kfree(domain);
30058ee99adSPaul Mundt }
301ecd84eb2SPaul Mundt EXPORT_SYMBOL_GPL(irq_domain_remove);
30258ee99adSPaul Mundt
irq_domain_update_bus_token(struct irq_domain * domain,enum irq_domain_bus_token bus_token)30361d0a000SMarc Zyngier void irq_domain_update_bus_token(struct irq_domain *domain,
30461d0a000SMarc Zyngier enum irq_domain_bus_token bus_token)
30561d0a000SMarc Zyngier {
30661d0a000SMarc Zyngier char *name;
30761d0a000SMarc Zyngier
30861d0a000SMarc Zyngier if (domain->bus_token == bus_token)
30961d0a000SMarc Zyngier return;
31061d0a000SMarc Zyngier
31161d0a000SMarc Zyngier mutex_lock(&irq_domain_mutex);
31261d0a000SMarc Zyngier
31361d0a000SMarc Zyngier domain->bus_token = bus_token;
31461d0a000SMarc Zyngier
31561d0a000SMarc Zyngier name = kasprintf(GFP_KERNEL, "%s-%d", domain->name, bus_token);
31661d0a000SMarc Zyngier if (!name) {
31761d0a000SMarc Zyngier mutex_unlock(&irq_domain_mutex);
31861d0a000SMarc Zyngier return;
31961d0a000SMarc Zyngier }
32061d0a000SMarc Zyngier
32161d0a000SMarc Zyngier debugfs_remove_domain_dir(domain);
32261d0a000SMarc Zyngier
32361d0a000SMarc Zyngier if (domain->flags & IRQ_DOMAIN_NAME_ALLOCATED)
32461d0a000SMarc Zyngier kfree(domain->name);
32561d0a000SMarc Zyngier else
32661d0a000SMarc Zyngier domain->flags |= IRQ_DOMAIN_NAME_ALLOCATED;
32761d0a000SMarc Zyngier
32861d0a000SMarc Zyngier domain->name = name;
32961d0a000SMarc Zyngier debugfs_add_domain_dir(domain);
33061d0a000SMarc Zyngier
33161d0a000SMarc Zyngier mutex_unlock(&irq_domain_mutex);
33261d0a000SMarc Zyngier }
3338a667928SJohn Stultz EXPORT_SYMBOL_GPL(irq_domain_update_bus_token);
33461d0a000SMarc Zyngier
335a8db8cf0SGrant Likely /**
33667196feaSAndy Shevchenko * irq_domain_create_simple() - Register an irq_domain and optionally map a range of irqs
33767196feaSAndy Shevchenko * @fwnode: firmware node for the interrupt controller
338781d0f46SMark Brown * @size: total number of irqs in mapping
33994a63da0SLinus Walleij * @first_irq: first number of irq block assigned to the domain,
340fa40f377SGrant Likely * pass zero to assign irqs on-the-fly. If first_irq is non-zero, then
341fa40f377SGrant Likely * pre-map all of the irqs in the domain to virqs starting at first_irq.
342f8264e34SJiang Liu * @ops: domain callbacks
343781d0f46SMark Brown * @host_data: Controller private data pointer
344781d0f46SMark Brown *
345fa40f377SGrant Likely * Allocates an irq_domain, and optionally if first_irq is positive then also
346fa40f377SGrant Likely * allocate irq_descs and map all of the hwirqs to virqs starting at first_irq.
347781d0f46SMark Brown *
348781d0f46SMark Brown * This is intended to implement the expected behaviour for most
349fa40f377SGrant Likely * interrupt controllers. If device tree is used, then first_irq will be 0 and
350fa40f377SGrant Likely * irqs get mapped dynamically on the fly. However, if the controller requires
351fa40f377SGrant Likely * static virq assignments (non-DT boot) then it will set that up correctly.
352781d0f46SMark Brown */
irq_domain_create_simple(struct fwnode_handle * fwnode,unsigned int size,unsigned int first_irq,const struct irq_domain_ops * ops,void * host_data)35367196feaSAndy Shevchenko struct irq_domain *irq_domain_create_simple(struct fwnode_handle *fwnode,
354781d0f46SMark Brown unsigned int size,
355781d0f46SMark Brown unsigned int first_irq,
356781d0f46SMark Brown const struct irq_domain_ops *ops,
357781d0f46SMark Brown void *host_data)
358781d0f46SMark Brown {
359fa40f377SGrant Likely struct irq_domain *domain;
3602854d167SLinus Walleij
36167196feaSAndy Shevchenko domain = __irq_domain_add(fwnode, size, size, 0, ops, host_data);
362fa40f377SGrant Likely if (!domain)
363fa40f377SGrant Likely return NULL;
364fa40f377SGrant Likely
365fa40f377SGrant Likely if (first_irq > 0) {
3662854d167SLinus Walleij if (IS_ENABLED(CONFIG_SPARSE_IRQ)) {
367fa40f377SGrant Likely /* attempt to allocated irq_descs */
368fa40f377SGrant Likely int rc = irq_alloc_descs(first_irq, first_irq, size,
36967196feaSAndy Shevchenko of_node_to_nid(to_of_node(fwnode)));
370fa40f377SGrant Likely if (rc < 0)
371d202b7b9SLinus Walleij pr_info("Cannot allocate irq_descs @ IRQ%d, assuming pre-allocated\n",
3722854d167SLinus Walleij first_irq);
3732854d167SLinus Walleij }
374ddaf144cSGrant Likely irq_domain_associate_many(domain, first_irq, 0, size);
3752854d167SLinus Walleij }
3762854d167SLinus Walleij
377fa40f377SGrant Likely return domain;
378781d0f46SMark Brown }
37967196feaSAndy Shevchenko EXPORT_SYMBOL_GPL(irq_domain_create_simple);
380781d0f46SMark Brown
381781d0f46SMark Brown /**
382a8db8cf0SGrant Likely * irq_domain_add_legacy() - Allocate and register a legacy revmap irq_domain.
383a8db8cf0SGrant Likely * @of_node: pointer to interrupt controller's device tree node.
3841bc04f2cSGrant Likely * @size: total number of irqs in legacy mapping
3851bc04f2cSGrant Likely * @first_irq: first number of irq block assigned to the domain
3861bc04f2cSGrant Likely * @first_hwirq: first hwirq number to use for the translation. Should normally
3871bc04f2cSGrant Likely * be '0', but a positive integer can be used if the effective
3881bc04f2cSGrant Likely * hwirqs numbering does not begin at zero.
389a8db8cf0SGrant Likely * @ops: map/unmap domain callbacks
390a8db8cf0SGrant Likely * @host_data: Controller private data pointer
391a8db8cf0SGrant Likely *
392a8db8cf0SGrant Likely * Note: the map() callback will be called before this function returns
393a8db8cf0SGrant Likely * for all legacy interrupts except 0 (which is always the invalid irq for
394a8db8cf0SGrant Likely * a legacy controller).
395a8db8cf0SGrant Likely */
irq_domain_add_legacy(struct device_node * of_node,unsigned int size,unsigned int first_irq,irq_hw_number_t first_hwirq,const struct irq_domain_ops * ops,void * host_data)396a8db8cf0SGrant Likely struct irq_domain *irq_domain_add_legacy(struct device_node *of_node,
3971bc04f2cSGrant Likely unsigned int size,
3981bc04f2cSGrant Likely unsigned int first_irq,
3991bc04f2cSGrant Likely irq_hw_number_t first_hwirq,
400a18dc81bSGrant Likely const struct irq_domain_ops *ops,
401a8db8cf0SGrant Likely void *host_data)
402a8db8cf0SGrant Likely {
403b6e95788SAndy Shevchenko return irq_domain_create_legacy(of_node_to_fwnode(of_node), size,
404b6e95788SAndy Shevchenko first_irq, first_hwirq, ops, host_data);
405b6e95788SAndy Shevchenko }
406b6e95788SAndy Shevchenko EXPORT_SYMBOL_GPL(irq_domain_add_legacy);
407b6e95788SAndy Shevchenko
irq_domain_create_legacy(struct fwnode_handle * fwnode,unsigned int size,unsigned int first_irq,irq_hw_number_t first_hwirq,const struct irq_domain_ops * ops,void * host_data)408b6e95788SAndy Shevchenko struct irq_domain *irq_domain_create_legacy(struct fwnode_handle *fwnode,
409b6e95788SAndy Shevchenko unsigned int size,
410b6e95788SAndy Shevchenko unsigned int first_irq,
411b6e95788SAndy Shevchenko irq_hw_number_t first_hwirq,
412b6e95788SAndy Shevchenko const struct irq_domain_ops *ops,
413b6e95788SAndy Shevchenko void *host_data)
414b6e95788SAndy Shevchenko {
4151bc04f2cSGrant Likely struct irq_domain *domain;
416a8db8cf0SGrant Likely
417b6e95788SAndy Shevchenko domain = __irq_domain_add(fwnode, first_hwirq + size, first_hwirq + size, 0, ops, host_data);
418f8264e34SJiang Liu if (domain)
419ddaf144cSGrant Likely irq_domain_associate_many(domain, first_irq, first_hwirq, size);
4201bc04f2cSGrant Likely
421a8db8cf0SGrant Likely return domain;
422cc79ca69SGrant Likely }
423b6e95788SAndy Shevchenko EXPORT_SYMBOL_GPL(irq_domain_create_legacy);
424cc79ca69SGrant Likely
425a8db8cf0SGrant Likely /**
426651e8b54SMarc Zyngier * irq_find_matching_fwspec() - Locates a domain for a given fwspec
427651e8b54SMarc Zyngier * @fwspec: FW specifier for an interrupt
428ad3aedfbSMarc Zyngier * @bus_token: domain-specific data
429cc79ca69SGrant Likely */
irq_find_matching_fwspec(struct irq_fwspec * fwspec,enum irq_domain_bus_token bus_token)430651e8b54SMarc Zyngier struct irq_domain *irq_find_matching_fwspec(struct irq_fwspec *fwspec,
431ad3aedfbSMarc Zyngier enum irq_domain_bus_token bus_token)
432cc79ca69SGrant Likely {
433cc79ca69SGrant Likely struct irq_domain *h, *found = NULL;
434651e8b54SMarc Zyngier struct fwnode_handle *fwnode = fwspec->fwnode;
435a18dc81bSGrant Likely int rc;
436cc79ca69SGrant Likely
437cc79ca69SGrant Likely /* We might want to match the legacy controller last since
438cc79ca69SGrant Likely * it might potentially be set to match all interrupts in
439cc79ca69SGrant Likely * the absence of a device node. This isn't a problem so far
440cc79ca69SGrant Likely * yet though...
441ad3aedfbSMarc Zyngier *
442ad3aedfbSMarc Zyngier * bus_token == DOMAIN_BUS_ANY matches any domain, any other
443ad3aedfbSMarc Zyngier * values must generate an exact match for the domain to be
444ad3aedfbSMarc Zyngier * selected.
445cc79ca69SGrant Likely */
446cc79ca69SGrant Likely mutex_lock(&irq_domain_mutex);
447a18dc81bSGrant Likely list_for_each_entry(h, &irq_domain_list, link) {
448651e8b54SMarc Zyngier if (h->ops->select && fwspec->param_count)
449651e8b54SMarc Zyngier rc = h->ops->select(h, fwspec, bus_token);
450651e8b54SMarc Zyngier else if (h->ops->match)
451130b8c6cSMarc Zyngier rc = h->ops->match(h, to_of_node(fwnode), bus_token);
452a18dc81bSGrant Likely else
453130b8c6cSMarc Zyngier rc = ((fwnode != NULL) && (h->fwnode == fwnode) &&
454ad3aedfbSMarc Zyngier ((bus_token == DOMAIN_BUS_ANY) ||
455ad3aedfbSMarc Zyngier (h->bus_token == bus_token)));
456a18dc81bSGrant Likely
457a18dc81bSGrant Likely if (rc) {
458cc79ca69SGrant Likely found = h;
459cc79ca69SGrant Likely break;
460cc79ca69SGrant Likely }
461a18dc81bSGrant Likely }
462cc79ca69SGrant Likely mutex_unlock(&irq_domain_mutex);
463cc79ca69SGrant Likely return found;
464cc79ca69SGrant Likely }
465651e8b54SMarc Zyngier EXPORT_SYMBOL_GPL(irq_find_matching_fwspec);
466cc79ca69SGrant Likely
467cc79ca69SGrant Likely /**
468cc79ca69SGrant Likely * irq_set_default_host() - Set a "default" irq domain
46968700650SGrant Likely * @domain: default domain pointer
470cc79ca69SGrant Likely *
471cc79ca69SGrant Likely * For convenience, it's possible to set a "default" domain that will be used
472cc79ca69SGrant Likely * whenever NULL is passed to irq_create_mapping(). It makes life easier for
473cc79ca69SGrant Likely * platforms that want to manipulate a few hard coded interrupt numbers that
474cc79ca69SGrant Likely * aren't properly represented in the device-tree.
475cc79ca69SGrant Likely */
irq_set_default_host(struct irq_domain * domain)47668700650SGrant Likely void irq_set_default_host(struct irq_domain *domain)
477cc79ca69SGrant Likely {
47854a90588SPaul Mundt pr_debug("Default domain set to @0x%p\n", domain);
479cc79ca69SGrant Likely
48068700650SGrant Likely irq_default_domain = domain;
481cc79ca69SGrant Likely }
482ecd84eb2SPaul Mundt EXPORT_SYMBOL_GPL(irq_set_default_host);
483cc79ca69SGrant Likely
4849f199dd3SMarc Zyngier /**
4859f199dd3SMarc Zyngier * irq_get_default_host() - Retrieve the "default" irq domain
4869f199dd3SMarc Zyngier *
4879f199dd3SMarc Zyngier * Returns: the default domain, if any.
4889f199dd3SMarc Zyngier *
4899f199dd3SMarc Zyngier * Modern code should never use this. This should only be used on
4909f199dd3SMarc Zyngier * systems that cannot implement a firmware->fwnode mapping (which
4919f199dd3SMarc Zyngier * both DT and ACPI provide).
4929f199dd3SMarc Zyngier */
irq_get_default_host(void)4939f199dd3SMarc Zyngier struct irq_domain *irq_get_default_host(void)
4949f199dd3SMarc Zyngier {
4959f199dd3SMarc Zyngier return irq_default_domain;
4969f199dd3SMarc Zyngier }
49751be9e51SCédric Le Goater EXPORT_SYMBOL_GPL(irq_get_default_host);
4989f199dd3SMarc Zyngier
irq_domain_is_nomap(struct irq_domain * domain)4994f86a06eSMarc Zyngier static bool irq_domain_is_nomap(struct irq_domain *domain)
5004f86a06eSMarc Zyngier {
5014f86a06eSMarc Zyngier return IS_ENABLED(CONFIG_IRQ_DOMAIN_NOMAP) &&
5024f86a06eSMarc Zyngier (domain->flags & IRQ_DOMAIN_FLAG_NO_MAP);
5034f86a06eSMarc Zyngier }
5044f86a06eSMarc Zyngier
irq_domain_clear_mapping(struct irq_domain * domain,irq_hw_number_t hwirq)505b526adfeSDavid Daney static void irq_domain_clear_mapping(struct irq_domain *domain,
506b526adfeSDavid Daney irq_hw_number_t hwirq)
507b526adfeSDavid Daney {
5089dbb8e34SJohan Hovold lockdep_assert_held(&domain->root->mutex);
50947d1932fSJohan Hovold
5104f86a06eSMarc Zyngier if (irq_domain_is_nomap(domain))
5114f86a06eSMarc Zyngier return;
5124f86a06eSMarc Zyngier
513d4a45c68SMarc Zyngier if (hwirq < domain->revmap_size)
514d4a45c68SMarc Zyngier rcu_assign_pointer(domain->revmap[hwirq], NULL);
515d4a45c68SMarc Zyngier else
516b526adfeSDavid Daney radix_tree_delete(&domain->revmap_tree, hwirq);
517b526adfeSDavid Daney }
518b526adfeSDavid Daney
irq_domain_set_mapping(struct irq_domain * domain,irq_hw_number_t hwirq,struct irq_data * irq_data)519b526adfeSDavid Daney static void irq_domain_set_mapping(struct irq_domain *domain,
520b526adfeSDavid Daney irq_hw_number_t hwirq,
521b526adfeSDavid Daney struct irq_data *irq_data)
522b526adfeSDavid Daney {
5239dbb8e34SJohan Hovold /*
5249dbb8e34SJohan Hovold * This also makes sure that all domains point to the same root when
5259dbb8e34SJohan Hovold * called from irq_domain_insert_irq() for each domain in a hierarchy.
5269dbb8e34SJohan Hovold */
5279dbb8e34SJohan Hovold lockdep_assert_held(&domain->root->mutex);
52847d1932fSJohan Hovold
5294f86a06eSMarc Zyngier if (irq_domain_is_nomap(domain))
5304f86a06eSMarc Zyngier return;
5314f86a06eSMarc Zyngier
532d4a45c68SMarc Zyngier if (hwirq < domain->revmap_size)
533d4a45c68SMarc Zyngier rcu_assign_pointer(domain->revmap[hwirq], irq_data);
534d4a45c68SMarc Zyngier else
535b526adfeSDavid Daney radix_tree_insert(&domain->revmap_tree, hwirq, irq_data);
536b526adfeSDavid Daney }
537b526adfeSDavid Daney
irq_domain_disassociate(struct irq_domain * domain,unsigned int irq)538e906a546SThomas Gleixner static void irq_domain_disassociate(struct irq_domain *domain, unsigned int irq)
539913af207SGrant Likely {
540913af207SGrant Likely struct irq_data *irq_data = irq_get_irq_data(irq);
541275e31b1SChen Gang irq_hw_number_t hwirq;
542913af207SGrant Likely
543ddaf144cSGrant Likely if (WARN(!irq_data || irq_data->domain != domain,
544ddaf144cSGrant Likely "virq%i doesn't exist; cannot disassociate\n", irq))
545ddaf144cSGrant Likely return;
546913af207SGrant Likely
547275e31b1SChen Gang hwirq = irq_data->hwirq;
5483f883c38SJohan Hovold
5499dbb8e34SJohan Hovold mutex_lock(&domain->root->mutex);
5503f883c38SJohan Hovold
551913af207SGrant Likely irq_set_status_flags(irq, IRQ_NOREQUEST);
552913af207SGrant Likely
553913af207SGrant Likely /* remove chip and handler */
554913af207SGrant Likely irq_set_chip_and_handler(irq, NULL, NULL);
555913af207SGrant Likely
556913af207SGrant Likely /* Make sure it's completed */
557913af207SGrant Likely synchronize_irq(irq);
558913af207SGrant Likely
559913af207SGrant Likely /* Tell the PIC about it */
560913af207SGrant Likely if (domain->ops->unmap)
561913af207SGrant Likely domain->ops->unmap(domain, irq);
562913af207SGrant Likely smp_mb();
563913af207SGrant Likely
564913af207SGrant Likely irq_data->domain = NULL;
565913af207SGrant Likely irq_data->hwirq = 0;
5669dc6be3dSThomas Gleixner domain->mapcount--;
567913af207SGrant Likely
568cef5075cSGrant Likely /* Clear reverse map for this hwirq */
569b526adfeSDavid Daney irq_domain_clear_mapping(domain, hwirq);
5703f883c38SJohan Hovold
5719dbb8e34SJohan Hovold mutex_unlock(&domain->root->mutex);
572913af207SGrant Likely }
573913af207SGrant Likely
irq_domain_associate_locked(struct irq_domain * domain,unsigned int virq,irq_hw_number_t hwirq)574b06730a5SJohan Hovold static int irq_domain_associate_locked(struct irq_domain *domain, unsigned int virq,
575ddaf144cSGrant Likely irq_hw_number_t hwirq)
576cc79ca69SGrant Likely {
577cc79ca69SGrant Likely struct irq_data *irq_data = irq_get_irq_data(virq);
578ddaf144cSGrant Likely int ret;
579cc79ca69SGrant Likely
580ddaf144cSGrant Likely if (WARN(hwirq >= domain->hwirq_max,
581ddaf144cSGrant Likely "error: hwirq 0x%x is too large for %s\n", (int)hwirq, domain->name))
582ddaf144cSGrant Likely return -EINVAL;
583ddaf144cSGrant Likely if (WARN(!irq_data, "error: virq%i is not allocated", virq))
584ddaf144cSGrant Likely return -EINVAL;
585ddaf144cSGrant Likely if (WARN(irq_data->domain, "error: virq%i is already associated", virq))
586ddaf144cSGrant Likely return -EINVAL;
587ddaf144cSGrant Likely
588cc79ca69SGrant Likely irq_data->hwirq = hwirq;
58968700650SGrant Likely irq_data->domain = domain;
590f5a1ad05SMark Brown if (domain->ops->map) {
591f5a1ad05SMark Brown ret = domain->ops->map(domain, virq, hwirq);
592f5a1ad05SMark Brown if (ret != 0) {
5935fe0c1f2SBenjamin Herrenschmidt /*
5945fe0c1f2SBenjamin Herrenschmidt * If map() returns -EPERM, this interrupt is protected
5955fe0c1f2SBenjamin Herrenschmidt * by the firmware or some other service and shall not
5965e1cda5bSGrant Likely * be mapped. Don't bother telling the user about it.
5975fe0c1f2SBenjamin Herrenschmidt */
5985fe0c1f2SBenjamin Herrenschmidt if (ret != -EPERM) {
5995e1cda5bSGrant Likely pr_info("%s didn't like hwirq-0x%lx to VIRQ%i mapping (rc=%d)\n",
6000bb4afb4SGrant Likely domain->name, hwirq, virq, ret);
6015fe0c1f2SBenjamin Herrenschmidt }
602cc79ca69SGrant Likely irq_data->domain = NULL;
603cc79ca69SGrant Likely irq_data->hwirq = 0;
604ddaf144cSGrant Likely return ret;
605f5a1ad05SMark Brown }
6062a71a1a9SGrant Likely }
6072a71a1a9SGrant Likely
6089dc6be3dSThomas Gleixner domain->mapcount++;
609b526adfeSDavid Daney irq_domain_set_mapping(domain, hwirq, irq_data);
610cc79ca69SGrant Likely
611cc79ca69SGrant Likely irq_clear_status_flags(virq, IRQ_NOREQUEST);
61268700650SGrant Likely
613cc79ca69SGrant Likely return 0;
614cc79ca69SGrant Likely }
615b06730a5SJohan Hovold
irq_domain_associate(struct irq_domain * domain,unsigned int virq,irq_hw_number_t hwirq)616b06730a5SJohan Hovold int irq_domain_associate(struct irq_domain *domain, unsigned int virq,
617b06730a5SJohan Hovold irq_hw_number_t hwirq)
618b06730a5SJohan Hovold {
619b06730a5SJohan Hovold int ret;
620b06730a5SJohan Hovold
6219dbb8e34SJohan Hovold mutex_lock(&domain->root->mutex);
622b06730a5SJohan Hovold ret = irq_domain_associate_locked(domain, virq, hwirq);
6239dbb8e34SJohan Hovold mutex_unlock(&domain->root->mutex);
624b06730a5SJohan Hovold
625b06730a5SJohan Hovold return ret;
626b06730a5SJohan Hovold }
627ddaf144cSGrant Likely EXPORT_SYMBOL_GPL(irq_domain_associate);
628cc79ca69SGrant Likely
irq_domain_associate_many(struct irq_domain * domain,unsigned int irq_base,irq_hw_number_t hwirq_base,int count)629ddaf144cSGrant Likely void irq_domain_associate_many(struct irq_domain *domain, unsigned int irq_base,
630ddaf144cSGrant Likely irq_hw_number_t hwirq_base, int count)
631ddaf144cSGrant Likely {
6325d4c9bc7SMarc Zyngier struct device_node *of_node;
633ddaf144cSGrant Likely int i;
634ddaf144cSGrant Likely
6355d4c9bc7SMarc Zyngier of_node = irq_domain_get_of_node(domain);
636ddaf144cSGrant Likely pr_debug("%s(%s, irqbase=%i, hwbase=%i, count=%i)\n", __func__,
6375d4c9bc7SMarc Zyngier of_node_full_name(of_node), irq_base, (int)hwirq_base, count);
638ddaf144cSGrant Likely
6394e0d86dfSJohan Hovold for (i = 0; i < count; i++)
640ddaf144cSGrant Likely irq_domain_associate(domain, irq_base + i, hwirq_base + i);
641ddaf144cSGrant Likely }
64298aa468eSGrant Likely EXPORT_SYMBOL_GPL(irq_domain_associate_many);
643cc79ca69SGrant Likely
644e37af801SMarc Zyngier #ifdef CONFIG_IRQ_DOMAIN_NOMAP
645cc79ca69SGrant Likely /**
646cc79ca69SGrant Likely * irq_create_direct_mapping() - Allocate an irq for direct mapping
647cc79ca69SGrant Likely * @domain: domain to allocate the irq for or NULL for default domain
648cc79ca69SGrant Likely *
649cc79ca69SGrant Likely * This routine is used for irq controllers which can choose the hardware
650cc79ca69SGrant Likely * interrupt numbers they generate. In such a case it's simplest to use
6511aa0dd94SGrant Likely * the linux irq as the hardware interrupt number. It still uses the linear
6521aa0dd94SGrant Likely * or radix tree to store the mapping, but the irq controller can optimize
6531aa0dd94SGrant Likely * the revmap path by using the hwirq directly.
654cc79ca69SGrant Likely */
irq_create_direct_mapping(struct irq_domain * domain)655cc79ca69SGrant Likely unsigned int irq_create_direct_mapping(struct irq_domain *domain)
656cc79ca69SGrant Likely {
6575d4c9bc7SMarc Zyngier struct device_node *of_node;
658cc79ca69SGrant Likely unsigned int virq;
659cc79ca69SGrant Likely
66068700650SGrant Likely if (domain == NULL)
66168700650SGrant Likely domain = irq_default_domain;
662cc79ca69SGrant Likely
6635d4c9bc7SMarc Zyngier of_node = irq_domain_get_of_node(domain);
6645d4c9bc7SMarc Zyngier virq = irq_alloc_desc_from(1, of_node_to_nid(of_node));
66503848373SGrant Likely if (!virq) {
66654a90588SPaul Mundt pr_debug("create_direct virq allocation failed\n");
66703848373SGrant Likely return 0;
668cc79ca69SGrant Likely }
669ef50cd57SXu Qiang if (virq >= domain->hwirq_max) {
670ef50cd57SXu Qiang pr_err("ERROR: no free irqs available below %lu maximum\n",
671ef50cd57SXu Qiang domain->hwirq_max);
672cc79ca69SGrant Likely irq_free_desc(virq);
673cc79ca69SGrant Likely return 0;
674cc79ca69SGrant Likely }
67554a90588SPaul Mundt pr_debug("create_direct obtained virq %d\n", virq);
676cc79ca69SGrant Likely
67798aa468eSGrant Likely if (irq_domain_associate(domain, virq, virq)) {
678cc79ca69SGrant Likely irq_free_desc(virq);
67903848373SGrant Likely return 0;
680cc79ca69SGrant Likely }
681cc79ca69SGrant Likely
682cc79ca69SGrant Likely return virq;
683cc79ca69SGrant Likely }
684ecd84eb2SPaul Mundt EXPORT_SYMBOL_GPL(irq_create_direct_mapping);
685e37af801SMarc Zyngier #endif
686cc79ca69SGrant Likely
irq_create_mapping_affinity_locked(struct irq_domain * domain,irq_hw_number_t hwirq,const struct irq_affinity_desc * affinity)687601363ccSJohan Hovold static unsigned int irq_create_mapping_affinity_locked(struct irq_domain *domain,
688bb4c6910SLaurent Vivier irq_hw_number_t hwirq,
689bb4c6910SLaurent Vivier const struct irq_affinity_desc *affinity)
690cc79ca69SGrant Likely {
6916e6f75c9SJohan Hovold struct device_node *of_node = irq_domain_get_of_node(domain);
6925b7526e3SDavid Daney int virq;
693cc79ca69SGrant Likely
69454a90588SPaul Mundt pr_debug("irq_create_mapping(0x%p, 0x%lx)\n", domain, hwirq);
695cc79ca69SGrant Likely
696cc79ca69SGrant Likely /* Allocate a virtual interrupt number */
697bb4c6910SLaurent Vivier virq = irq_domain_alloc_descs(-1, 1, hwirq, of_node_to_nid(of_node),
698bb4c6910SLaurent Vivier affinity);
6995b7526e3SDavid Daney if (virq <= 0) {
70054a90588SPaul Mundt pr_debug("-> virq allocation failed\n");
70103848373SGrant Likely return 0;
702cc79ca69SGrant Likely }
703cc79ca69SGrant Likely
704601363ccSJohan Hovold if (irq_domain_associate_locked(domain, virq, hwirq)) {
705cc79ca69SGrant Likely irq_free_desc(virq);
70603848373SGrant Likely return 0;
707cc79ca69SGrant Likely }
708cc79ca69SGrant Likely
70954a90588SPaul Mundt pr_debug("irq %lu on domain %s mapped to virtual irq %u\n",
7105d4c9bc7SMarc Zyngier hwirq, of_node_full_name(of_node), virq);
711cc79ca69SGrant Likely
712cc79ca69SGrant Likely return virq;
713cc79ca69SGrant Likely }
7146e6f75c9SJohan Hovold
715cc79ca69SGrant Likely /**
716cc79ca69SGrant Likely * irq_create_mapping_affinity() - Map a hardware interrupt into linux irq space
717cc79ca69SGrant Likely * @domain: domain owning this hardware interrupt or NULL for default domain
718cc79ca69SGrant Likely * @hwirq: hardware irq number in that domain space
719cc79ca69SGrant Likely * @affinity: irq affinity
720cc79ca69SGrant Likely *
721cc79ca69SGrant Likely * Only one mapping per hardware interrupt is permitted. Returns a linux
722cc79ca69SGrant Likely * irq number.
723cc79ca69SGrant Likely * If the sense/trigger is to be specified, set_irq_type() should be called
724cc79ca69SGrant Likely * on the number returned from that call.
725cc79ca69SGrant Likely */
irq_create_mapping_affinity(struct irq_domain * domain,irq_hw_number_t hwirq,const struct irq_affinity_desc * affinity)726cc79ca69SGrant Likely unsigned int irq_create_mapping_affinity(struct irq_domain *domain,
727cc79ca69SGrant Likely irq_hw_number_t hwirq,
728cc79ca69SGrant Likely const struct irq_affinity_desc *affinity)
729cc79ca69SGrant Likely {
730cc79ca69SGrant Likely int virq;
731cc79ca69SGrant Likely
732cc79ca69SGrant Likely /* Look for default domain if necessary */
733cc79ca69SGrant Likely if (domain == NULL)
734cc79ca69SGrant Likely domain = irq_default_domain;
735cc79ca69SGrant Likely if (domain == NULL) {
736cc79ca69SGrant Likely WARN(1, "%s(, %lx) called with NULL domain\n", __func__, hwirq);
737cc79ca69SGrant Likely return 0;
738cc79ca69SGrant Likely }
739cc79ca69SGrant Likely
7409dbb8e34SJohan Hovold mutex_lock(&domain->root->mutex);
741cc79ca69SGrant Likely
742cc79ca69SGrant Likely /* Check if mapping already exists */
743cc79ca69SGrant Likely virq = irq_find_mapping(domain, hwirq);
744cc79ca69SGrant Likely if (virq) {
7456e6f75c9SJohan Hovold pr_debug("existing mapping on virq %d\n", virq);
746601363ccSJohan Hovold goto out;
747cc79ca69SGrant Likely }
748cc79ca69SGrant Likely
749601363ccSJohan Hovold virq = irq_create_mapping_affinity_locked(domain, hwirq, affinity);
750601363ccSJohan Hovold out:
7519dbb8e34SJohan Hovold mutex_unlock(&domain->root->mutex);
752cc79ca69SGrant Likely
753cc79ca69SGrant Likely return virq;
754cc79ca69SGrant Likely }
755bb4c6910SLaurent Vivier EXPORT_SYMBOL_GPL(irq_create_mapping_affinity);
756cc79ca69SGrant Likely
irq_domain_translate(struct irq_domain * d,struct irq_fwspec * fwspec,irq_hw_number_t * hwirq,unsigned int * type)75711e4438eSMarc Zyngier static int irq_domain_translate(struct irq_domain *d,
75811e4438eSMarc Zyngier struct irq_fwspec *fwspec,
75911e4438eSMarc Zyngier irq_hw_number_t *hwirq, unsigned int *type)
76011e4438eSMarc Zyngier {
76111e4438eSMarc Zyngier #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
76211e4438eSMarc Zyngier if (d->ops->translate)
76311e4438eSMarc Zyngier return d->ops->translate(d, fwspec, hwirq, type);
76411e4438eSMarc Zyngier #endif
76511e4438eSMarc Zyngier if (d->ops->xlate)
76611e4438eSMarc Zyngier return d->ops->xlate(d, to_of_node(fwspec->fwnode),
76711e4438eSMarc Zyngier fwspec->param, fwspec->param_count,
76811e4438eSMarc Zyngier hwirq, type);
76911e4438eSMarc Zyngier
77011e4438eSMarc Zyngier /* If domain has no translation, then we assume interrupt line */
77111e4438eSMarc Zyngier *hwirq = fwspec->param[0];
77211e4438eSMarc Zyngier return 0;
77311e4438eSMarc Zyngier }
77411e4438eSMarc Zyngier
of_phandle_args_to_fwspec(struct device_node * np,const u32 * args,unsigned int count,struct irq_fwspec * fwspec)7750ab8d0f6SMarc Zyngier void of_phandle_args_to_fwspec(struct device_node *np, const u32 *args,
7760ab8d0f6SMarc Zyngier unsigned int count, struct irq_fwspec *fwspec)
77711e4438eSMarc Zyngier {
77811e4438eSMarc Zyngier int i;
77911e4438eSMarc Zyngier
780c3a877feSAndy Shevchenko fwspec->fwnode = of_node_to_fwnode(np);
781b5c231d8SBrian Masney fwspec->param_count = count;
78211e4438eSMarc Zyngier
783b5c231d8SBrian Masney for (i = 0; i < count; i++)
784b5c231d8SBrian Masney fwspec->param[i] = args[i];
78511e4438eSMarc Zyngier }
7860ab8d0f6SMarc Zyngier EXPORT_SYMBOL_GPL(of_phandle_args_to_fwspec);
78711e4438eSMarc Zyngier
irq_create_fwspec_mapping(struct irq_fwspec * fwspec)788c0131f09SMarc Zyngier unsigned int irq_create_fwspec_mapping(struct irq_fwspec *fwspec)
789cc79ca69SGrant Likely {
79068700650SGrant Likely struct irq_domain *domain;
7911e2a7d78SJon Hunter struct irq_data *irq_data;
792cc79ca69SGrant Likely irq_hw_number_t hwirq;
793cc79ca69SGrant Likely unsigned int type = IRQ_TYPE_NONE;
794f8264e34SJiang Liu int virq;
795cc79ca69SGrant Likely
796530cbe10SMarc Zyngier if (fwspec->fwnode) {
797651e8b54SMarc Zyngier domain = irq_find_matching_fwspec(fwspec, DOMAIN_BUS_WIRED);
798530cbe10SMarc Zyngier if (!domain)
799651e8b54SMarc Zyngier domain = irq_find_matching_fwspec(fwspec, DOMAIN_BUS_ANY);
800530cbe10SMarc Zyngier } else {
80111e4438eSMarc Zyngier domain = irq_default_domain;
802530cbe10SMarc Zyngier }
80311e4438eSMarc Zyngier
80468700650SGrant Likely if (!domain) {
805798f0fd1SKefeng Wang pr_warn("no irq domain found for %s !\n",
806c0131f09SMarc Zyngier of_node_full_name(to_of_node(fwspec->fwnode)));
80703848373SGrant Likely return 0;
808cc79ca69SGrant Likely }
809cc79ca69SGrant Likely
810c0131f09SMarc Zyngier if (irq_domain_translate(domain, fwspec, &hwirq, &type))
81103848373SGrant Likely return 0;
812cc79ca69SGrant Likely
813b62b2cf5SJon Hunter /*
814b62b2cf5SJon Hunter * WARN if the irqchip returns a type with bits
815b62b2cf5SJon Hunter * outside the sense mask set and clear these bits.
816b62b2cf5SJon Hunter */
817b62b2cf5SJon Hunter if (WARN_ON(type & ~IRQ_TYPE_SENSE_MASK))
818b62b2cf5SJon Hunter type &= IRQ_TYPE_SENSE_MASK;
819b62b2cf5SJon Hunter
8209dbb8e34SJohan Hovold mutex_lock(&domain->root->mutex);
821601363ccSJohan Hovold
8220cc01abaSYingjoe Chen /*
8230cc01abaSYingjoe Chen * If we've already configured this interrupt,
8240cc01abaSYingjoe Chen * don't do it again, or hell will break loose.
8250cc01abaSYingjoe Chen */
8260cc01abaSYingjoe Chen virq = irq_find_mapping(domain, hwirq);
827b62b2cf5SJon Hunter if (virq) {
828b62b2cf5SJon Hunter /*
829b62b2cf5SJon Hunter * If the trigger type is not specified or matches the
830b62b2cf5SJon Hunter * current trigger type then we are done so return the
831b62b2cf5SJon Hunter * interrupt number.
832b62b2cf5SJon Hunter */
833b62b2cf5SJon Hunter if (type == IRQ_TYPE_NONE || type == irq_get_trigger_type(virq))
834601363ccSJohan Hovold goto out;
8350cc01abaSYingjoe Chen
836b62b2cf5SJon Hunter /*
837b62b2cf5SJon Hunter * If the trigger type has not been set yet, then set
838b62b2cf5SJon Hunter * it now and return the interrupt number.
839b62b2cf5SJon Hunter */
840b62b2cf5SJon Hunter if (irq_get_trigger_type(virq) == IRQ_TYPE_NONE) {
8411e2a7d78SJon Hunter irq_data = irq_get_irq_data(virq);
842601363ccSJohan Hovold if (!irq_data) {
843601363ccSJohan Hovold virq = 0;
844601363ccSJohan Hovold goto out;
845601363ccSJohan Hovold }
8461e2a7d78SJon Hunter
8471e2a7d78SJon Hunter irqd_set_trigger_type(irq_data, type);
848601363ccSJohan Hovold goto out;
849b62b2cf5SJon Hunter }
850b62b2cf5SJon Hunter
851b62b2cf5SJon Hunter pr_warn("type mismatch, failed to map hwirq-%lu for %s!\n",
852b62b2cf5SJon Hunter hwirq, of_node_full_name(to_of_node(fwspec->fwnode)));
853601363ccSJohan Hovold virq = 0;
854601363ccSJohan Hovold goto out;
855b62b2cf5SJon Hunter }
856b62b2cf5SJon Hunter
857b62b2cf5SJon Hunter if (irq_domain_is_hierarchy(domain)) {
858601363ccSJohan Hovold virq = irq_domain_alloc_irqs_locked(domain, -1, 1, NUMA_NO_NODE,
859601363ccSJohan Hovold fwspec, false, NULL);
860601363ccSJohan Hovold if (virq <= 0) {
861601363ccSJohan Hovold virq = 0;
862601363ccSJohan Hovold goto out;
863601363ccSJohan Hovold }
8640cc01abaSYingjoe Chen } else {
865cc79ca69SGrant Likely /* Create mapping */
866601363ccSJohan Hovold virq = irq_create_mapping_affinity_locked(domain, hwirq, NULL);
86703848373SGrant Likely if (!virq)
868601363ccSJohan Hovold goto out;
8690cc01abaSYingjoe Chen }
870cc79ca69SGrant Likely
8711e2a7d78SJon Hunter irq_data = irq_get_irq_data(virq);
872601363ccSJohan Hovold if (WARN_ON(!irq_data)) {
873601363ccSJohan Hovold virq = 0;
874601363ccSJohan Hovold goto out;
8751e2a7d78SJon Hunter }
8761e2a7d78SJon Hunter
8771e2a7d78SJon Hunter /* Store trigger type */
8781e2a7d78SJon Hunter irqd_set_trigger_type(irq_data, type);
879601363ccSJohan Hovold out:
8809dbb8e34SJohan Hovold mutex_unlock(&domain->root->mutex);
8811e2a7d78SJon Hunter
882cc79ca69SGrant Likely return virq;
883cc79ca69SGrant Likely }
884c0131f09SMarc Zyngier EXPORT_SYMBOL_GPL(irq_create_fwspec_mapping);
885c0131f09SMarc Zyngier
irq_create_of_mapping(struct of_phandle_args * irq_data)886c0131f09SMarc Zyngier unsigned int irq_create_of_mapping(struct of_phandle_args *irq_data)
887c0131f09SMarc Zyngier {
888c0131f09SMarc Zyngier struct irq_fwspec fwspec;
889c0131f09SMarc Zyngier
890b5c231d8SBrian Masney of_phandle_args_to_fwspec(irq_data->np, irq_data->args,
891b5c231d8SBrian Masney irq_data->args_count, &fwspec);
892b5c231d8SBrian Masney
893c0131f09SMarc Zyngier return irq_create_fwspec_mapping(&fwspec);
894c0131f09SMarc Zyngier }
895cc79ca69SGrant Likely EXPORT_SYMBOL_GPL(irq_create_of_mapping);
896cc79ca69SGrant Likely
897cc79ca69SGrant Likely /**
898cc79ca69SGrant Likely * irq_dispose_mapping() - Unmap an interrupt
899cc79ca69SGrant Likely * @virq: linux irq number of the interrupt to unmap
900cc79ca69SGrant Likely */
irq_dispose_mapping(unsigned int virq)901cc79ca69SGrant Likely void irq_dispose_mapping(unsigned int virq)
902cc79ca69SGrant Likely {
903cc79ca69SGrant Likely struct irq_data *irq_data = irq_get_irq_data(virq);
90468700650SGrant Likely struct irq_domain *domain;
905cc79ca69SGrant Likely
90603848373SGrant Likely if (!virq || !irq_data)
907cc79ca69SGrant Likely return;
908cc79ca69SGrant Likely
90968700650SGrant Likely domain = irq_data->domain;
91068700650SGrant Likely if (WARN_ON(domain == NULL))
911cc79ca69SGrant Likely return;
912cc79ca69SGrant Likely
913d16dcd3dSJon Hunter if (irq_domain_is_hierarchy(domain)) {
914d16dcd3dSJon Hunter irq_domain_free_irqs(virq, 1);
915d16dcd3dSJon Hunter } else {
916ddaf144cSGrant Likely irq_domain_disassociate(domain, virq);
917cc79ca69SGrant Likely irq_free_desc(virq);
918cc79ca69SGrant Likely }
919d16dcd3dSJon Hunter }
920cc79ca69SGrant Likely EXPORT_SYMBOL_GPL(irq_dispose_mapping);
921cc79ca69SGrant Likely
922cc79ca69SGrant Likely /**
923d22558ddSMarc Zyngier * __irq_resolve_mapping() - Find a linux irq from a hw irq number.
92468700650SGrant Likely * @domain: domain owning this hardware interrupt
92568700650SGrant Likely * @hwirq: hardware irq number in that domain space
926d22558ddSMarc Zyngier * @irq: optional pointer to return the Linux irq if required
927d22558ddSMarc Zyngier *
928d22558ddSMarc Zyngier * Returns the interrupt descriptor.
929cc79ca69SGrant Likely */
__irq_resolve_mapping(struct irq_domain * domain,irq_hw_number_t hwirq,unsigned int * irq)930d22558ddSMarc Zyngier struct irq_desc *__irq_resolve_mapping(struct irq_domain *domain,
931d22558ddSMarc Zyngier irq_hw_number_t hwirq,
932d22558ddSMarc Zyngier unsigned int *irq)
933cc79ca69SGrant Likely {
934d22558ddSMarc Zyngier struct irq_desc *desc = NULL;
9354c0946c4SGrant Likely struct irq_data *data;
936cc79ca69SGrant Likely
937a359f757SIngo Molnar /* Look for default domain if necessary */
93868700650SGrant Likely if (domain == NULL)
93968700650SGrant Likely domain = irq_default_domain;
94068700650SGrant Likely if (domain == NULL)
941d22558ddSMarc Zyngier return desc;
942cc79ca69SGrant Likely
9434f86a06eSMarc Zyngier if (irq_domain_is_nomap(domain)) {
944ef50cd57SXu Qiang if (hwirq < domain->hwirq_max) {
945f8264e34SJiang Liu data = irq_domain_get_irq_data(domain, hwirq);
946f8264e34SJiang Liu if (data && data->hwirq == hwirq)
947d22558ddSMarc Zyngier desc = irq_data_to_desc(data);
9486f194c99SXu Qiang if (irq && desc)
9496f194c99SXu Qiang *irq = hwirq;
9504c0946c4SGrant Likely }
9514c0946c4SGrant Likely
952d22558ddSMarc Zyngier return desc;
9534f86a06eSMarc Zyngier }
9544f86a06eSMarc Zyngier
955d4a45c68SMarc Zyngier rcu_read_lock();
956d3dcb436SGrant Likely /* Check if the hwirq is in the linear revmap. */
957d3dcb436SGrant Likely if (hwirq < domain->revmap_size)
958d4a45c68SMarc Zyngier data = rcu_dereference(domain->revmap[hwirq]);
959d4a45c68SMarc Zyngier else
9601aa0dd94SGrant Likely data = radix_tree_lookup(&domain->revmap_tree, hwirq);
961d22558ddSMarc Zyngier
962d22558ddSMarc Zyngier if (likely(data)) {
963d22558ddSMarc Zyngier desc = irq_data_to_desc(data);
964d22558ddSMarc Zyngier if (irq)
965d22558ddSMarc Zyngier *irq = data->irq;
966cc79ca69SGrant Likely }
967d22558ddSMarc Zyngier
968d22558ddSMarc Zyngier rcu_read_unlock();
969d22558ddSMarc Zyngier return desc;
970d22558ddSMarc Zyngier }
971d22558ddSMarc Zyngier EXPORT_SYMBOL_GPL(__irq_resolve_mapping);
972cc79ca69SGrant Likely
97316b2e6e2SGrant Likely /**
97416b2e6e2SGrant Likely * irq_domain_xlate_onecell() - Generic xlate for direct one cell bindings
97516b2e6e2SGrant Likely *
97616b2e6e2SGrant Likely * Device Tree IRQ specifier translation function which works with one cell
97716b2e6e2SGrant Likely * bindings where the cell value maps directly to the hwirq number.
97816b2e6e2SGrant Likely */
irq_domain_xlate_onecell(struct irq_domain * d,struct device_node * ctrlr,const u32 * intspec,unsigned int intsize,unsigned long * out_hwirq,unsigned int * out_type)97916b2e6e2SGrant Likely int irq_domain_xlate_onecell(struct irq_domain *d, struct device_node *ctrlr,
9807e713301SGrant Likely const u32 *intspec, unsigned int intsize,
9817e713301SGrant Likely unsigned long *out_hwirq, unsigned int *out_type)
9827e713301SGrant Likely {
98316b2e6e2SGrant Likely if (WARN_ON(intsize < 1))
9847e713301SGrant Likely return -EINVAL;
9857e713301SGrant Likely *out_hwirq = intspec[0];
9867e713301SGrant Likely *out_type = IRQ_TYPE_NONE;
98716b2e6e2SGrant Likely return 0;
98816b2e6e2SGrant Likely }
98916b2e6e2SGrant Likely EXPORT_SYMBOL_GPL(irq_domain_xlate_onecell);
99016b2e6e2SGrant Likely
99116b2e6e2SGrant Likely /**
99216b2e6e2SGrant Likely * irq_domain_xlate_twocell() - Generic xlate for direct two cell bindings
99316b2e6e2SGrant Likely *
99416b2e6e2SGrant Likely * Device Tree IRQ specifier translation function which works with two cell
99516b2e6e2SGrant Likely * bindings where the cell values map directly to the hwirq number
99616b2e6e2SGrant Likely * and linux irq flags.
99716b2e6e2SGrant Likely */
irq_domain_xlate_twocell(struct irq_domain * d,struct device_node * ctrlr,const u32 * intspec,unsigned int intsize,irq_hw_number_t * out_hwirq,unsigned int * out_type)99816b2e6e2SGrant Likely int irq_domain_xlate_twocell(struct irq_domain *d, struct device_node *ctrlr,
99916b2e6e2SGrant Likely const u32 *intspec, unsigned int intsize,
100016b2e6e2SGrant Likely irq_hw_number_t *out_hwirq, unsigned int *out_type)
100116b2e6e2SGrant Likely {
1002b5c231d8SBrian Masney struct irq_fwspec fwspec;
1003b5c231d8SBrian Masney
1004b5c231d8SBrian Masney of_phandle_args_to_fwspec(ctrlr, intspec, intsize, &fwspec);
1005b5c231d8SBrian Masney return irq_domain_translate_twocell(d, &fwspec, out_hwirq, out_type);
10067e713301SGrant Likely }
100716b2e6e2SGrant Likely EXPORT_SYMBOL_GPL(irq_domain_xlate_twocell);
100816b2e6e2SGrant Likely
100916b2e6e2SGrant Likely /**
101016b2e6e2SGrant Likely * irq_domain_xlate_onetwocell() - Generic xlate for one or two cell bindings
101116b2e6e2SGrant Likely *
101216b2e6e2SGrant Likely * Device Tree IRQ specifier translation function which works with either one
101316b2e6e2SGrant Likely * or two cell bindings where the cell values map directly to the hwirq number
101416b2e6e2SGrant Likely * and linux irq flags.
101516b2e6e2SGrant Likely *
101616b2e6e2SGrant Likely * Note: don't use this function unless your interrupt controller explicitly
101716b2e6e2SGrant Likely * supports both one and two cell bindings. For the majority of controllers
101816b2e6e2SGrant Likely * the _onecell() or _twocell() variants above should be used.
101916b2e6e2SGrant Likely */
irq_domain_xlate_onetwocell(struct irq_domain * d,struct device_node * ctrlr,const u32 * intspec,unsigned int intsize,unsigned long * out_hwirq,unsigned int * out_type)102016b2e6e2SGrant Likely int irq_domain_xlate_onetwocell(struct irq_domain *d,
102116b2e6e2SGrant Likely struct device_node *ctrlr,
102216b2e6e2SGrant Likely const u32 *intspec, unsigned int intsize,
102316b2e6e2SGrant Likely unsigned long *out_hwirq, unsigned int *out_type)
102416b2e6e2SGrant Likely {
102516b2e6e2SGrant Likely if (WARN_ON(intsize < 1))
102616b2e6e2SGrant Likely return -EINVAL;
102716b2e6e2SGrant Likely *out_hwirq = intspec[0];
10280c228919SSebastian Frias if (intsize > 1)
10290c228919SSebastian Frias *out_type = intspec[1] & IRQ_TYPE_SENSE_MASK;
10300c228919SSebastian Frias else
10310c228919SSebastian Frias *out_type = IRQ_TYPE_NONE;
103216b2e6e2SGrant Likely return 0;
103316b2e6e2SGrant Likely }
103416b2e6e2SGrant Likely EXPORT_SYMBOL_GPL(irq_domain_xlate_onetwocell);
10357e713301SGrant Likely
1036a18dc81bSGrant Likely const struct irq_domain_ops irq_domain_simple_ops = {
103716b2e6e2SGrant Likely .xlate = irq_domain_xlate_onetwocell,
103875294957SGrant Likely };
103975294957SGrant Likely EXPORT_SYMBOL_GPL(irq_domain_simple_ops);
1040f8264e34SJiang Liu
1041b5c231d8SBrian Masney /**
1042b01ecceaSYash Shah * irq_domain_translate_onecell() - Generic translate for direct one cell
1043b01ecceaSYash Shah * bindings
1044b01ecceaSYash Shah */
irq_domain_translate_onecell(struct irq_domain * d,struct irq_fwspec * fwspec,unsigned long * out_hwirq,unsigned int * out_type)1045b01ecceaSYash Shah int irq_domain_translate_onecell(struct irq_domain *d,
1046b01ecceaSYash Shah struct irq_fwspec *fwspec,
1047b01ecceaSYash Shah unsigned long *out_hwirq,
1048b01ecceaSYash Shah unsigned int *out_type)
1049b01ecceaSYash Shah {
1050b01ecceaSYash Shah if (WARN_ON(fwspec->param_count < 1))
1051b01ecceaSYash Shah return -EINVAL;
1052b01ecceaSYash Shah *out_hwirq = fwspec->param[0];
1053b01ecceaSYash Shah *out_type = IRQ_TYPE_NONE;
1054b01ecceaSYash Shah return 0;
1055b01ecceaSYash Shah }
1056b01ecceaSYash Shah EXPORT_SYMBOL_GPL(irq_domain_translate_onecell);
1057b01ecceaSYash Shah
1058b01ecceaSYash Shah /**
1059b5c231d8SBrian Masney * irq_domain_translate_twocell() - Generic translate for direct two cell
1060b5c231d8SBrian Masney * bindings
1061b5c231d8SBrian Masney *
1062b5c231d8SBrian Masney * Device Tree IRQ specifier translation function which works with two cell
1063b5c231d8SBrian Masney * bindings where the cell values map directly to the hwirq number
1064b5c231d8SBrian Masney * and linux irq flags.
1065b5c231d8SBrian Masney */
irq_domain_translate_twocell(struct irq_domain * d,struct irq_fwspec * fwspec,unsigned long * out_hwirq,unsigned int * out_type)1066b5c231d8SBrian Masney int irq_domain_translate_twocell(struct irq_domain *d,
1067b5c231d8SBrian Masney struct irq_fwspec *fwspec,
1068b5c231d8SBrian Masney unsigned long *out_hwirq,
1069b5c231d8SBrian Masney unsigned int *out_type)
1070b5c231d8SBrian Masney {
1071b5c231d8SBrian Masney if (WARN_ON(fwspec->param_count < 2))
1072b5c231d8SBrian Masney return -EINVAL;
1073b5c231d8SBrian Masney *out_hwirq = fwspec->param[0];
1074b5c231d8SBrian Masney *out_type = fwspec->param[1] & IRQ_TYPE_SENSE_MASK;
1075b5c231d8SBrian Masney return 0;
1076b5c231d8SBrian Masney }
1077b5c231d8SBrian Masney EXPORT_SYMBOL_GPL(irq_domain_translate_twocell);
1078b5c231d8SBrian Masney
irq_domain_alloc_descs(int virq,unsigned int cnt,irq_hw_number_t hwirq,int node,const struct irq_affinity_desc * affinity)1079ac0a0cd2SQais Yousef int irq_domain_alloc_descs(int virq, unsigned int cnt, irq_hw_number_t hwirq,
1080bec04037SDou Liyang int node, const struct irq_affinity_desc *affinity)
1081f8264e34SJiang Liu {
1082f8264e34SJiang Liu unsigned int hint;
1083f8264e34SJiang Liu
1084f8264e34SJiang Liu if (virq >= 0) {
108506ee6d57SThomas Gleixner virq = __irq_alloc_descs(virq, virq, cnt, node, THIS_MODULE,
108606ee6d57SThomas Gleixner affinity);
1087f8264e34SJiang Liu } else {
1088f8264e34SJiang Liu hint = hwirq % nr_irqs;
1089f8264e34SJiang Liu if (hint == 0)
1090f8264e34SJiang Liu hint++;
109106ee6d57SThomas Gleixner virq = __irq_alloc_descs(-1, hint, cnt, node, THIS_MODULE,
109206ee6d57SThomas Gleixner affinity);
109306ee6d57SThomas Gleixner if (virq <= 0 && hint > 1) {
109406ee6d57SThomas Gleixner virq = __irq_alloc_descs(-1, 1, cnt, node, THIS_MODULE,
109506ee6d57SThomas Gleixner affinity);
109606ee6d57SThomas Gleixner }
1097f8264e34SJiang Liu }
1098f8264e34SJiang Liu
1099f8264e34SJiang Liu return virq;
1100f8264e34SJiang Liu }
1101f8264e34SJiang Liu
11025c8f77a2SBartosz Golaszewski /**
11035c8f77a2SBartosz Golaszewski * irq_domain_reset_irq_data - Clear hwirq, chip and chip_data in @irq_data
11045c8f77a2SBartosz Golaszewski * @irq_data: The pointer to irq_data
11055c8f77a2SBartosz Golaszewski */
irq_domain_reset_irq_data(struct irq_data * irq_data)11065c8f77a2SBartosz Golaszewski void irq_domain_reset_irq_data(struct irq_data *irq_data)
11075c8f77a2SBartosz Golaszewski {
11085c8f77a2SBartosz Golaszewski irq_data->hwirq = 0;
11095c8f77a2SBartosz Golaszewski irq_data->chip = &no_irq_chip;
11105c8f77a2SBartosz Golaszewski irq_data->chip_data = NULL;
11115c8f77a2SBartosz Golaszewski }
11125c8f77a2SBartosz Golaszewski EXPORT_SYMBOL_GPL(irq_domain_reset_irq_data);
11135c8f77a2SBartosz Golaszewski
1114f8264e34SJiang Liu #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
1115afb7da83SJiang Liu /**
11162a5e9a07SMarc Zyngier * irq_domain_create_hierarchy - Add a irqdomain into the hierarchy
1117afb7da83SJiang Liu * @parent: Parent irq domain to associate with the new domain
1118afb7da83SJiang Liu * @flags: Irq domain flags associated to the domain
1119afb7da83SJiang Liu * @size: Size of the domain. See below
11202a5e9a07SMarc Zyngier * @fwnode: Optional fwnode of the interrupt controller
1121afb7da83SJiang Liu * @ops: Pointer to the interrupt domain callbacks
1122afb7da83SJiang Liu * @host_data: Controller private data pointer
1123afb7da83SJiang Liu *
1124afb7da83SJiang Liu * If @size is 0 a tree domain is created, otherwise a linear domain.
1125afb7da83SJiang Liu *
1126afb7da83SJiang Liu * If successful the parent is associated to the new domain and the
1127afb7da83SJiang Liu * domain flags are set.
1128afb7da83SJiang Liu * Returns pointer to IRQ domain, or NULL on failure.
1129afb7da83SJiang Liu */
irq_domain_create_hierarchy(struct irq_domain * parent,unsigned int flags,unsigned int size,struct fwnode_handle * fwnode,const struct irq_domain_ops * ops,void * host_data)11302a5e9a07SMarc Zyngier struct irq_domain *irq_domain_create_hierarchy(struct irq_domain *parent,
1131afb7da83SJiang Liu unsigned int flags,
1132afb7da83SJiang Liu unsigned int size,
11332a5e9a07SMarc Zyngier struct fwnode_handle *fwnode,
1134afb7da83SJiang Liu const struct irq_domain_ops *ops,
1135afb7da83SJiang Liu void *host_data)
1136afb7da83SJiang Liu {
1137afb7da83SJiang Liu struct irq_domain *domain;
1138afb7da83SJiang Liu
1139afb7da83SJiang Liu if (size)
11408932c32cSMarc Zyngier domain = __irq_domain_create(fwnode, size, size, 0, ops, host_data);
1141afb7da83SJiang Liu else
11428932c32cSMarc Zyngier domain = __irq_domain_create(fwnode, 0, ~0, 0, ops, host_data);
11438932c32cSMarc Zyngier
1144afb7da83SJiang Liu if (domain) {
1145ad32ab96SJuergen Gross if (parent)
11469dbb8e34SJohan Hovold domain->root = parent->root;
1147afb7da83SJiang Liu domain->parent = parent;
1148afb7da83SJiang Liu domain->flags |= flags;
11498932c32cSMarc Zyngier
11508932c32cSMarc Zyngier __irq_domain_publish(domain);
1151afb7da83SJiang Liu }
1152afb7da83SJiang Liu
1153afb7da83SJiang Liu return domain;
1154afb7da83SJiang Liu }
115552b2a05fSQuan Nguyen EXPORT_SYMBOL_GPL(irq_domain_create_hierarchy);
1156afb7da83SJiang Liu
irq_domain_insert_irq(int virq)1157f8264e34SJiang Liu static void irq_domain_insert_irq(int virq)
1158f8264e34SJiang Liu {
1159f8264e34SJiang Liu struct irq_data *data;
1160f8264e34SJiang Liu
1161f8264e34SJiang Liu for (data = irq_get_irq_data(virq); data; data = data->parent_data) {
1162f8264e34SJiang Liu struct irq_domain *domain = data->domain;
1163f8264e34SJiang Liu
11649dc6be3dSThomas Gleixner domain->mapcount++;
1165b526adfeSDavid Daney irq_domain_set_mapping(domain, data->hwirq, data);
1166f8264e34SJiang Liu }
1167f8264e34SJiang Liu
1168f8264e34SJiang Liu irq_clear_status_flags(virq, IRQ_NOREQUEST);
1169f8264e34SJiang Liu }
1170f8264e34SJiang Liu
irq_domain_remove_irq(int virq)1171f8264e34SJiang Liu static void irq_domain_remove_irq(int virq)
1172f8264e34SJiang Liu {
1173f8264e34SJiang Liu struct irq_data *data;
1174f8264e34SJiang Liu
1175f8264e34SJiang Liu irq_set_status_flags(virq, IRQ_NOREQUEST);
1176f8264e34SJiang Liu irq_set_chip_and_handler(virq, NULL, NULL);
1177f8264e34SJiang Liu synchronize_irq(virq);
1178f8264e34SJiang Liu smp_mb();
1179f8264e34SJiang Liu
1180f8264e34SJiang Liu for (data = irq_get_irq_data(virq); data; data = data->parent_data) {
1181f8264e34SJiang Liu struct irq_domain *domain = data->domain;
1182f8264e34SJiang Liu irq_hw_number_t hwirq = data->hwirq;
1183f8264e34SJiang Liu
11849dc6be3dSThomas Gleixner domain->mapcount--;
1185b526adfeSDavid Daney irq_domain_clear_mapping(domain, hwirq);
1186f8264e34SJiang Liu }
1187f8264e34SJiang Liu }
1188f8264e34SJiang Liu
irq_domain_insert_irq_data(struct irq_domain * domain,struct irq_data * child)1189f8264e34SJiang Liu static struct irq_data *irq_domain_insert_irq_data(struct irq_domain *domain,
1190f8264e34SJiang Liu struct irq_data *child)
1191f8264e34SJiang Liu {
1192f8264e34SJiang Liu struct irq_data *irq_data;
1193f8264e34SJiang Liu
11946783011bSJiang Liu irq_data = kzalloc_node(sizeof(*irq_data), GFP_KERNEL,
11956783011bSJiang Liu irq_data_get_node(child));
1196f8264e34SJiang Liu if (irq_data) {
1197f8264e34SJiang Liu child->parent_data = irq_data;
1198f8264e34SJiang Liu irq_data->irq = child->irq;
11990d0b4c86SJiang Liu irq_data->common = child->common;
1200f8264e34SJiang Liu irq_data->domain = domain;
1201f8264e34SJiang Liu }
1202f8264e34SJiang Liu
1203f8264e34SJiang Liu return irq_data;
1204f8264e34SJiang Liu }
1205f8264e34SJiang Liu
__irq_domain_free_hierarchy(struct irq_data * irq_data)120655567976SMarc Zyngier static void __irq_domain_free_hierarchy(struct irq_data *irq_data)
120755567976SMarc Zyngier {
120855567976SMarc Zyngier struct irq_data *tmp;
120955567976SMarc Zyngier
121055567976SMarc Zyngier while (irq_data) {
121155567976SMarc Zyngier tmp = irq_data;
121255567976SMarc Zyngier irq_data = irq_data->parent_data;
121355567976SMarc Zyngier kfree(tmp);
121455567976SMarc Zyngier }
121555567976SMarc Zyngier }
121655567976SMarc Zyngier
irq_domain_free_irq_data(unsigned int virq,unsigned int nr_irqs)1217f8264e34SJiang Liu static void irq_domain_free_irq_data(unsigned int virq, unsigned int nr_irqs)
1218f8264e34SJiang Liu {
1219f8264e34SJiang Liu struct irq_data *irq_data, *tmp;
1220f8264e34SJiang Liu int i;
1221f8264e34SJiang Liu
1222f8264e34SJiang Liu for (i = 0; i < nr_irqs; i++) {
1223f8264e34SJiang Liu irq_data = irq_get_irq_data(virq + i);
1224f8264e34SJiang Liu tmp = irq_data->parent_data;
1225f8264e34SJiang Liu irq_data->parent_data = NULL;
1226f8264e34SJiang Liu irq_data->domain = NULL;
1227f8264e34SJiang Liu
122855567976SMarc Zyngier __irq_domain_free_hierarchy(tmp);
1229f8264e34SJiang Liu }
1230f8264e34SJiang Liu }
123155567976SMarc Zyngier
123255567976SMarc Zyngier /**
123355567976SMarc Zyngier * irq_domain_disconnect_hierarchy - Mark the first unused level of a hierarchy
123455567976SMarc Zyngier * @domain: IRQ domain from which the hierarchy is to be disconnected
123555567976SMarc Zyngier * @virq: IRQ number where the hierarchy is to be trimmed
123655567976SMarc Zyngier *
123755567976SMarc Zyngier * Marks the @virq level belonging to @domain as disconnected.
123855567976SMarc Zyngier * Returns -EINVAL if @virq doesn't have a valid irq_data pointing
123955567976SMarc Zyngier * to @domain.
124055567976SMarc Zyngier *
124155567976SMarc Zyngier * Its only use is to be able to trim levels of hierarchy that do not
124255567976SMarc Zyngier * have any real meaning for this interrupt, and that the driver marks
124355567976SMarc Zyngier * as such from its .alloc() callback.
124455567976SMarc Zyngier */
irq_domain_disconnect_hierarchy(struct irq_domain * domain,unsigned int virq)124555567976SMarc Zyngier int irq_domain_disconnect_hierarchy(struct irq_domain *domain,
124655567976SMarc Zyngier unsigned int virq)
124755567976SMarc Zyngier {
124855567976SMarc Zyngier struct irq_data *irqd;
124955567976SMarc Zyngier
125055567976SMarc Zyngier irqd = irq_domain_get_irq_data(domain, virq);
125155567976SMarc Zyngier if (!irqd)
125255567976SMarc Zyngier return -EINVAL;
125355567976SMarc Zyngier
125455567976SMarc Zyngier irqd->chip = ERR_PTR(-ENOTCONN);
125555567976SMarc Zyngier return 0;
125655567976SMarc Zyngier }
1257131d326bSMaulik Shah EXPORT_SYMBOL_GPL(irq_domain_disconnect_hierarchy);
125855567976SMarc Zyngier
irq_domain_trim_hierarchy(unsigned int virq)125955567976SMarc Zyngier static int irq_domain_trim_hierarchy(unsigned int virq)
126055567976SMarc Zyngier {
126155567976SMarc Zyngier struct irq_data *tail, *irqd, *irq_data;
126255567976SMarc Zyngier
126355567976SMarc Zyngier irq_data = irq_get_irq_data(virq);
126455567976SMarc Zyngier tail = NULL;
126555567976SMarc Zyngier
126655567976SMarc Zyngier /* The first entry must have a valid irqchip */
126755567976SMarc Zyngier if (!irq_data->chip || IS_ERR(irq_data->chip))
126855567976SMarc Zyngier return -EINVAL;
126955567976SMarc Zyngier
127055567976SMarc Zyngier /*
127155567976SMarc Zyngier * Validate that the irq_data chain is sane in the presence of
127255567976SMarc Zyngier * a hierarchy trimming marker.
127355567976SMarc Zyngier */
127455567976SMarc Zyngier for (irqd = irq_data->parent_data; irqd; irq_data = irqd, irqd = irqd->parent_data) {
127555567976SMarc Zyngier /* Can't have a valid irqchip after a trim marker */
127655567976SMarc Zyngier if (irqd->chip && tail)
127755567976SMarc Zyngier return -EINVAL;
127855567976SMarc Zyngier
127955567976SMarc Zyngier /* Can't have an empty irqchip before a trim marker */
128055567976SMarc Zyngier if (!irqd->chip && !tail)
128155567976SMarc Zyngier return -EINVAL;
128255567976SMarc Zyngier
128355567976SMarc Zyngier if (IS_ERR(irqd->chip)) {
128455567976SMarc Zyngier /* Only -ENOTCONN is a valid trim marker */
128555567976SMarc Zyngier if (PTR_ERR(irqd->chip) != -ENOTCONN)
128655567976SMarc Zyngier return -EINVAL;
128755567976SMarc Zyngier
128855567976SMarc Zyngier tail = irq_data;
128955567976SMarc Zyngier }
129055567976SMarc Zyngier }
129155567976SMarc Zyngier
129255567976SMarc Zyngier /* No trim marker, nothing to do */
129355567976SMarc Zyngier if (!tail)
129455567976SMarc Zyngier return 0;
129555567976SMarc Zyngier
129655567976SMarc Zyngier pr_info("IRQ%d: trimming hierarchy from %s\n",
129755567976SMarc Zyngier virq, tail->parent_data->domain->name);
129855567976SMarc Zyngier
129955567976SMarc Zyngier /* Sever the inner part of the hierarchy... */
130055567976SMarc Zyngier irqd = tail;
130155567976SMarc Zyngier tail = tail->parent_data;
130255567976SMarc Zyngier irqd->parent_data = NULL;
130355567976SMarc Zyngier __irq_domain_free_hierarchy(tail);
130455567976SMarc Zyngier
130555567976SMarc Zyngier return 0;
1306f8264e34SJiang Liu }
1307f8264e34SJiang Liu
irq_domain_alloc_irq_data(struct irq_domain * domain,unsigned int virq,unsigned int nr_irqs)1308f8264e34SJiang Liu static int irq_domain_alloc_irq_data(struct irq_domain *domain,
1309f8264e34SJiang Liu unsigned int virq, unsigned int nr_irqs)
1310f8264e34SJiang Liu {
1311f8264e34SJiang Liu struct irq_data *irq_data;
1312f8264e34SJiang Liu struct irq_domain *parent;
1313f8264e34SJiang Liu int i;
1314f8264e34SJiang Liu
1315f8264e34SJiang Liu /* The outermost irq_data is embedded in struct irq_desc */
1316f8264e34SJiang Liu for (i = 0; i < nr_irqs; i++) {
1317f8264e34SJiang Liu irq_data = irq_get_irq_data(virq + i);
1318f8264e34SJiang Liu irq_data->domain = domain;
1319f8264e34SJiang Liu
1320f8264e34SJiang Liu for (parent = domain->parent; parent; parent = parent->parent) {
1321f8264e34SJiang Liu irq_data = irq_domain_insert_irq_data(parent, irq_data);
1322f8264e34SJiang Liu if (!irq_data) {
1323f8264e34SJiang Liu irq_domain_free_irq_data(virq, i + 1);
1324f8264e34SJiang Liu return -ENOMEM;
1325f8264e34SJiang Liu }
1326f8264e34SJiang Liu }
1327f8264e34SJiang Liu }
1328f8264e34SJiang Liu
1329f8264e34SJiang Liu return 0;
1330f8264e34SJiang Liu }
1331f8264e34SJiang Liu
1332f8264e34SJiang Liu /**
1333f8264e34SJiang Liu * irq_domain_get_irq_data - Get irq_data associated with @virq and @domain
1334f8264e34SJiang Liu * @domain: domain to match
1335f8264e34SJiang Liu * @virq: IRQ number to get irq_data
1336f8264e34SJiang Liu */
irq_domain_get_irq_data(struct irq_domain * domain,unsigned int virq)1337f8264e34SJiang Liu struct irq_data *irq_domain_get_irq_data(struct irq_domain *domain,
1338f8264e34SJiang Liu unsigned int virq)
1339f8264e34SJiang Liu {
1340f8264e34SJiang Liu struct irq_data *irq_data;
1341f8264e34SJiang Liu
1342f8264e34SJiang Liu for (irq_data = irq_get_irq_data(virq); irq_data;
1343f8264e34SJiang Liu irq_data = irq_data->parent_data)
1344f8264e34SJiang Liu if (irq_data->domain == domain)
1345f8264e34SJiang Liu return irq_data;
1346f8264e34SJiang Liu
1347f8264e34SJiang Liu return NULL;
1348f8264e34SJiang Liu }
1349a4289dc2SJake Oshins EXPORT_SYMBOL_GPL(irq_domain_get_irq_data);
1350f8264e34SJiang Liu
1351f8264e34SJiang Liu /**
1352f8264e34SJiang Liu * irq_domain_set_hwirq_and_chip - Set hwirq and irqchip of @virq at @domain
1353f8264e34SJiang Liu * @domain: Interrupt domain to match
1354f8264e34SJiang Liu * @virq: IRQ number
1355f8264e34SJiang Liu * @hwirq: The hwirq number
1356f8264e34SJiang Liu * @chip: The associated interrupt chip
1357f8264e34SJiang Liu * @chip_data: The associated chip data
1358f8264e34SJiang Liu */
irq_domain_set_hwirq_and_chip(struct irq_domain * domain,unsigned int virq,irq_hw_number_t hwirq,const struct irq_chip * chip,void * chip_data)1359f8264e34SJiang Liu int irq_domain_set_hwirq_and_chip(struct irq_domain *domain, unsigned int virq,
136045ec846cSMarc Zyngier irq_hw_number_t hwirq,
136145ec846cSMarc Zyngier const struct irq_chip *chip,
1362f8264e34SJiang Liu void *chip_data)
1363f8264e34SJiang Liu {
1364f8264e34SJiang Liu struct irq_data *irq_data = irq_domain_get_irq_data(domain, virq);
1365f8264e34SJiang Liu
1366f8264e34SJiang Liu if (!irq_data)
1367f8264e34SJiang Liu return -ENOENT;
1368f8264e34SJiang Liu
1369f8264e34SJiang Liu irq_data->hwirq = hwirq;
137045ec846cSMarc Zyngier irq_data->chip = (struct irq_chip *)(chip ? chip : &no_irq_chip);
1371f8264e34SJiang Liu irq_data->chip_data = chip_data;
1372f8264e34SJiang Liu
1373f8264e34SJiang Liu return 0;
1374f8264e34SJiang Liu }
137552b2a05fSQuan Nguyen EXPORT_SYMBOL_GPL(irq_domain_set_hwirq_and_chip);
1376f8264e34SJiang Liu
1377f8264e34SJiang Liu /**
13781b537708SJiang Liu * irq_domain_set_info - Set the complete data for a @virq in @domain
13791b537708SJiang Liu * @domain: Interrupt domain to match
13801b537708SJiang Liu * @virq: IRQ number
13811b537708SJiang Liu * @hwirq: The hardware interrupt number
13821b537708SJiang Liu * @chip: The associated interrupt chip
13831b537708SJiang Liu * @chip_data: The associated interrupt chip data
13841b537708SJiang Liu * @handler: The interrupt flow handler
13851b537708SJiang Liu * @handler_data: The interrupt flow handler data
13861b537708SJiang Liu * @handler_name: The interrupt handler name
13871b537708SJiang Liu */
irq_domain_set_info(struct irq_domain * domain,unsigned int virq,irq_hw_number_t hwirq,const struct irq_chip * chip,void * chip_data,irq_flow_handler_t handler,void * handler_data,const char * handler_name)13881b537708SJiang Liu void irq_domain_set_info(struct irq_domain *domain, unsigned int virq,
138945ec846cSMarc Zyngier irq_hw_number_t hwirq, const struct irq_chip *chip,
13901b537708SJiang Liu void *chip_data, irq_flow_handler_t handler,
13911b537708SJiang Liu void *handler_data, const char *handler_name)
13921b537708SJiang Liu {
13931b537708SJiang Liu irq_domain_set_hwirq_and_chip(domain, virq, hwirq, chip, chip_data);
13941b537708SJiang Liu __irq_set_handler(virq, handler, 0, handler_name);
13951b537708SJiang Liu irq_set_handler_data(virq, handler_data);
13961b537708SJiang Liu }
139764bce3e8SKeith Busch EXPORT_SYMBOL(irq_domain_set_info);
13981b537708SJiang Liu
13991b537708SJiang Liu /**
1400f8264e34SJiang Liu * irq_domain_free_irqs_common - Clear irq_data and free the parent
1401f8264e34SJiang Liu * @domain: Interrupt domain to match
1402f8264e34SJiang Liu * @virq: IRQ number to start with
1403f8264e34SJiang Liu * @nr_irqs: The number of irqs to free
1404f8264e34SJiang Liu */
irq_domain_free_irqs_common(struct irq_domain * domain,unsigned int virq,unsigned int nr_irqs)1405f8264e34SJiang Liu void irq_domain_free_irqs_common(struct irq_domain *domain, unsigned int virq,
1406f8264e34SJiang Liu unsigned int nr_irqs)
1407f8264e34SJiang Liu {
1408f8264e34SJiang Liu struct irq_data *irq_data;
1409f8264e34SJiang Liu int i;
1410f8264e34SJiang Liu
1411f8264e34SJiang Liu for (i = 0; i < nr_irqs; i++) {
1412f8264e34SJiang Liu irq_data = irq_domain_get_irq_data(domain, virq + i);
1413f8264e34SJiang Liu if (irq_data)
1414f8264e34SJiang Liu irq_domain_reset_irq_data(irq_data);
1415f8264e34SJiang Liu }
1416f8264e34SJiang Liu irq_domain_free_irqs_parent(domain, virq, nr_irqs);
1417f8264e34SJiang Liu }
141863cc787eSAxel Lin EXPORT_SYMBOL_GPL(irq_domain_free_irqs_common);
1419f8264e34SJiang Liu
1420f8264e34SJiang Liu /**
1421f8264e34SJiang Liu * irq_domain_free_irqs_top - Clear handler and handler data, clear irqdata and free parent
1422f8264e34SJiang Liu * @domain: Interrupt domain to match
1423f8264e34SJiang Liu * @virq: IRQ number to start with
1424f8264e34SJiang Liu * @nr_irqs: The number of irqs to free
1425f8264e34SJiang Liu */
irq_domain_free_irqs_top(struct irq_domain * domain,unsigned int virq,unsigned int nr_irqs)1426f8264e34SJiang Liu void irq_domain_free_irqs_top(struct irq_domain *domain, unsigned int virq,
1427f8264e34SJiang Liu unsigned int nr_irqs)
1428f8264e34SJiang Liu {
1429f8264e34SJiang Liu int i;
1430f8264e34SJiang Liu
1431f8264e34SJiang Liu for (i = 0; i < nr_irqs; i++) {
1432f8264e34SJiang Liu irq_set_handler_data(virq + i, NULL);
1433f8264e34SJiang Liu irq_set_handler(virq + i, NULL);
1434f8264e34SJiang Liu }
1435f8264e34SJiang Liu irq_domain_free_irqs_common(domain, virq, nr_irqs);
1436f8264e34SJiang Liu }
1437f8264e34SJiang Liu
irq_domain_free_irqs_hierarchy(struct irq_domain * domain,unsigned int irq_base,unsigned int nr_irqs)14386a6544e5SMarc Zyngier static void irq_domain_free_irqs_hierarchy(struct irq_domain *domain,
143936d72731SJiang Liu unsigned int irq_base,
144036d72731SJiang Liu unsigned int nr_irqs)
144136d72731SJiang Liu {
14424615fbc3SMarc Zyngier unsigned int i;
14434615fbc3SMarc Zyngier
14444615fbc3SMarc Zyngier if (!domain->ops->free)
14454615fbc3SMarc Zyngier return;
14464615fbc3SMarc Zyngier
14474615fbc3SMarc Zyngier for (i = 0; i < nr_irqs; i++) {
14484615fbc3SMarc Zyngier if (irq_domain_get_irq_data(domain, irq_base + i))
14494615fbc3SMarc Zyngier domain->ops->free(domain, irq_base + i, 1);
14504615fbc3SMarc Zyngier }
145136d72731SJiang Liu }
145236d72731SJiang Liu
irq_domain_alloc_irqs_hierarchy(struct irq_domain * domain,unsigned int irq_base,unsigned int nr_irqs,void * arg)14536a6544e5SMarc Zyngier int irq_domain_alloc_irqs_hierarchy(struct irq_domain *domain,
145436d72731SJiang Liu unsigned int irq_base,
145536d72731SJiang Liu unsigned int nr_irqs, void *arg)
145636d72731SJiang Liu {
145787f2d1c6SAlexander Sverdlin if (!domain->ops->alloc) {
145887f2d1c6SAlexander Sverdlin pr_debug("domain->ops->alloc() is NULL\n");
145987f2d1c6SAlexander Sverdlin return -ENOSYS;
146087f2d1c6SAlexander Sverdlin }
146187f2d1c6SAlexander Sverdlin
14626a6544e5SMarc Zyngier return domain->ops->alloc(domain, irq_base, nr_irqs, arg);
146336d72731SJiang Liu }
146436d72731SJiang Liu
irq_domain_alloc_irqs_locked(struct irq_domain * domain,int irq_base,unsigned int nr_irqs,int node,void * arg,bool realloc,const struct irq_affinity_desc * affinity)1465d55f7f4cSJohan Hovold static int irq_domain_alloc_irqs_locked(struct irq_domain *domain, int irq_base,
1466d55f7f4cSJohan Hovold unsigned int nr_irqs, int node, void *arg,
1467d55f7f4cSJohan Hovold bool realloc, const struct irq_affinity_desc *affinity)
1468d55f7f4cSJohan Hovold {
1469d55f7f4cSJohan Hovold int i, ret, virq;
1470d55f7f4cSJohan Hovold
1471d55f7f4cSJohan Hovold if (realloc && irq_base >= 0) {
1472d55f7f4cSJohan Hovold virq = irq_base;
1473d55f7f4cSJohan Hovold } else {
1474d55f7f4cSJohan Hovold virq = irq_domain_alloc_descs(irq_base, nr_irqs, 0, node,
1475d55f7f4cSJohan Hovold affinity);
1476d55f7f4cSJohan Hovold if (virq < 0) {
1477d55f7f4cSJohan Hovold pr_debug("cannot allocate IRQ(base %d, count %d)\n",
1478d55f7f4cSJohan Hovold irq_base, nr_irqs);
1479d55f7f4cSJohan Hovold return virq;
1480d55f7f4cSJohan Hovold }
1481d55f7f4cSJohan Hovold }
1482d55f7f4cSJohan Hovold
1483d55f7f4cSJohan Hovold if (irq_domain_alloc_irq_data(domain, virq, nr_irqs)) {
1484d55f7f4cSJohan Hovold pr_debug("cannot allocate memory for IRQ%d\n", virq);
1485d55f7f4cSJohan Hovold ret = -ENOMEM;
1486d55f7f4cSJohan Hovold goto out_free_desc;
1487d55f7f4cSJohan Hovold }
1488d55f7f4cSJohan Hovold
1489d55f7f4cSJohan Hovold ret = irq_domain_alloc_irqs_hierarchy(domain, virq, nr_irqs, arg);
1490d55f7f4cSJohan Hovold if (ret < 0)
1491d55f7f4cSJohan Hovold goto out_free_irq_data;
1492d55f7f4cSJohan Hovold
1493d55f7f4cSJohan Hovold for (i = 0; i < nr_irqs; i++) {
1494d55f7f4cSJohan Hovold ret = irq_domain_trim_hierarchy(virq + i);
1495d55f7f4cSJohan Hovold if (ret)
1496d55f7f4cSJohan Hovold goto out_free_irq_data;
1497d55f7f4cSJohan Hovold }
1498d55f7f4cSJohan Hovold
1499d55f7f4cSJohan Hovold for (i = 0; i < nr_irqs; i++)
1500d55f7f4cSJohan Hovold irq_domain_insert_irq(virq + i);
1501d55f7f4cSJohan Hovold
1502d55f7f4cSJohan Hovold return virq;
1503d55f7f4cSJohan Hovold
1504d55f7f4cSJohan Hovold out_free_irq_data:
1505d55f7f4cSJohan Hovold irq_domain_free_irq_data(virq, nr_irqs);
1506d55f7f4cSJohan Hovold out_free_desc:
1507d55f7f4cSJohan Hovold irq_free_descs(virq, nr_irqs);
1508d55f7f4cSJohan Hovold return ret;
1509d55f7f4cSJohan Hovold }
1510d55f7f4cSJohan Hovold
1511f8264e34SJiang Liu /**
1512f8264e34SJiang Liu * __irq_domain_alloc_irqs - Allocate IRQs from domain
1513f8264e34SJiang Liu * @domain: domain to allocate from
151408970ecfSJulien Grall * @irq_base: allocate specified IRQ number if irq_base >= 0
1515f8264e34SJiang Liu * @nr_irqs: number of IRQs to allocate
1516f8264e34SJiang Liu * @node: NUMA node id for memory allocation
1517f8264e34SJiang Liu * @arg: domain specific argument
1518f8264e34SJiang Liu * @realloc: IRQ descriptors have already been allocated if true
151906ee6d57SThomas Gleixner * @affinity: Optional irq affinity mask for multiqueue devices
1520f8264e34SJiang Liu *
1521f8264e34SJiang Liu * Allocate IRQ numbers and initialized all data structures to support
1522f8264e34SJiang Liu * hierarchy IRQ domains.
1523f8264e34SJiang Liu * Parameter @realloc is mainly to support legacy IRQs.
1524f8264e34SJiang Liu * Returns error code or allocated IRQ number
1525f8264e34SJiang Liu *
1526f8264e34SJiang Liu * The whole process to setup an IRQ has been split into two steps.
1527f8264e34SJiang Liu * The first step, __irq_domain_alloc_irqs(), is to allocate IRQ
1528f8264e34SJiang Liu * descriptor and required hardware resources. The second step,
1529a359f757SIngo Molnar * irq_domain_activate_irq(), is to program the hardware with preallocated
1530f8264e34SJiang Liu * resources. In this way, it's easier to rollback when failing to
1531f8264e34SJiang Liu * allocate resources.
1532f8264e34SJiang Liu */
__irq_domain_alloc_irqs(struct irq_domain * domain,int irq_base,unsigned int nr_irqs,int node,void * arg,bool realloc,const struct irq_affinity_desc * affinity)1533f8264e34SJiang Liu int __irq_domain_alloc_irqs(struct irq_domain *domain, int irq_base,
1534f8264e34SJiang Liu unsigned int nr_irqs, int node, void *arg,
1535bec04037SDou Liyang bool realloc, const struct irq_affinity_desc *affinity)
1536f8264e34SJiang Liu {
1537d55f7f4cSJohan Hovold int ret;
1538f8264e34SJiang Liu
1539f8264e34SJiang Liu if (domain == NULL) {
1540f8264e34SJiang Liu domain = irq_default_domain;
1541f8264e34SJiang Liu if (WARN(!domain, "domain is NULL; cannot allocate IRQ\n"))
1542f8264e34SJiang Liu return -EINVAL;
1543f8264e34SJiang Liu }
1544f8264e34SJiang Liu
15459dbb8e34SJohan Hovold mutex_lock(&domain->root->mutex);
1546d55f7f4cSJohan Hovold ret = irq_domain_alloc_irqs_locked(domain, irq_base, nr_irqs, node, arg,
1547d55f7f4cSJohan Hovold realloc, affinity);
15489dbb8e34SJohan Hovold mutex_unlock(&domain->root->mutex);
1549f8264e34SJiang Liu
1550f8264e34SJiang Liu return ret;
1551f8264e34SJiang Liu }
1552d8fcbe52SMarc Zyngier EXPORT_SYMBOL_GPL(__irq_domain_alloc_irqs);
1553f8264e34SJiang Liu
1554495c38d3SDavid Daney /* The irq_data was moved, fix the revmap to refer to the new location */
irq_domain_fix_revmap(struct irq_data * d)1555495c38d3SDavid Daney static void irq_domain_fix_revmap(struct irq_data *d)
1556495c38d3SDavid Daney {
1557d03cc2d8SMasahiro Yamada void __rcu **slot;
1558495c38d3SDavid Daney
15599dbb8e34SJohan Hovold lockdep_assert_held(&d->domain->root->mutex);
156047d1932fSJohan Hovold
156148b15a79SMarc Zyngier if (irq_domain_is_nomap(d->domain))
156248b15a79SMarc Zyngier return;
156348b15a79SMarc Zyngier
1564d4a45c68SMarc Zyngier /* Fix up the revmap. */
156548b15a79SMarc Zyngier if (d->hwirq < d->domain->revmap_size) {
156648b15a79SMarc Zyngier /* Not using radix tree */
1567d4a45c68SMarc Zyngier rcu_assign_pointer(d->domain->revmap[d->hwirq], d);
1568d4a45c68SMarc Zyngier } else {
1569495c38d3SDavid Daney slot = radix_tree_lookup_slot(&d->domain->revmap_tree, d->hwirq);
1570495c38d3SDavid Daney if (slot)
1571495c38d3SDavid Daney radix_tree_replace_slot(&d->domain->revmap_tree, slot, d);
1572d4a45c68SMarc Zyngier }
1573495c38d3SDavid Daney }
1574495c38d3SDavid Daney
1575495c38d3SDavid Daney /**
1576495c38d3SDavid Daney * irq_domain_push_irq() - Push a domain in to the top of a hierarchy.
1577495c38d3SDavid Daney * @domain: Domain to push.
1578495c38d3SDavid Daney * @virq: Irq to push the domain in to.
1579495c38d3SDavid Daney * @arg: Passed to the irq_domain_ops alloc() function.
1580495c38d3SDavid Daney *
1581495c38d3SDavid Daney * For an already existing irqdomain hierarchy, as might be obtained
1582495c38d3SDavid Daney * via a call to pci_enable_msix(), add an additional domain to the
1583495c38d3SDavid Daney * head of the processing chain. Must be called before request_irq()
1584495c38d3SDavid Daney * has been called.
1585495c38d3SDavid Daney */
irq_domain_push_irq(struct irq_domain * domain,int virq,void * arg)1586495c38d3SDavid Daney int irq_domain_push_irq(struct irq_domain *domain, int virq, void *arg)
1587495c38d3SDavid Daney {
1588930a1bbbSJohan Hovold struct irq_data *irq_data = irq_get_irq_data(virq);
1589930a1bbbSJohan Hovold struct irq_data *parent_irq_data;
1590495c38d3SDavid Daney struct irq_desc *desc;
1591495c38d3SDavid Daney int rv = 0;
1592495c38d3SDavid Daney
1593495c38d3SDavid Daney /*
1594495c38d3SDavid Daney * Check that no action has been set, which indicates the virq
1595495c38d3SDavid Daney * is in a state where this function doesn't have to deal with
1596495c38d3SDavid Daney * races between interrupt handling and maintaining the
1597495c38d3SDavid Daney * hierarchy. This will catch gross misuse. Attempting to
1598495c38d3SDavid Daney * make the check race free would require holding locks across
1599495c38d3SDavid Daney * calls to struct irq_domain_ops->alloc(), which could lead
1600495c38d3SDavid Daney * to deadlock, so we just do a simple check before starting.
1601495c38d3SDavid Daney */
1602495c38d3SDavid Daney desc = irq_to_desc(virq);
1603495c38d3SDavid Daney if (!desc)
1604495c38d3SDavid Daney return -EINVAL;
1605495c38d3SDavid Daney if (WARN_ON(desc->action))
1606495c38d3SDavid Daney return -EBUSY;
1607495c38d3SDavid Daney
1608495c38d3SDavid Daney if (domain == NULL)
1609495c38d3SDavid Daney return -EINVAL;
1610495c38d3SDavid Daney
1611495c38d3SDavid Daney if (WARN_ON(!irq_domain_is_hierarchy(domain)))
1612495c38d3SDavid Daney return -EINVAL;
1613495c38d3SDavid Daney
1614930a1bbbSJohan Hovold if (!irq_data)
1615495c38d3SDavid Daney return -EINVAL;
1616495c38d3SDavid Daney
1617930a1bbbSJohan Hovold if (domain->parent != irq_data->domain)
1618495c38d3SDavid Daney return -EINVAL;
1619495c38d3SDavid Daney
1620930a1bbbSJohan Hovold parent_irq_data = kzalloc_node(sizeof(*parent_irq_data), GFP_KERNEL,
1621930a1bbbSJohan Hovold irq_data_get_node(irq_data));
1622930a1bbbSJohan Hovold if (!parent_irq_data)
1623495c38d3SDavid Daney return -ENOMEM;
1624495c38d3SDavid Daney
16259dbb8e34SJohan Hovold mutex_lock(&domain->root->mutex);
1626495c38d3SDavid Daney
1627495c38d3SDavid Daney /* Copy the original irq_data. */
1628930a1bbbSJohan Hovold *parent_irq_data = *irq_data;
1629495c38d3SDavid Daney
1630495c38d3SDavid Daney /*
1631930a1bbbSJohan Hovold * Overwrite the irq_data, which is embedded in struct irq_desc, with
1632930a1bbbSJohan Hovold * values for this domain.
1633495c38d3SDavid Daney */
1634930a1bbbSJohan Hovold irq_data->parent_data = parent_irq_data;
1635930a1bbbSJohan Hovold irq_data->domain = domain;
1636930a1bbbSJohan Hovold irq_data->mask = 0;
1637930a1bbbSJohan Hovold irq_data->hwirq = 0;
1638930a1bbbSJohan Hovold irq_data->chip = NULL;
1639930a1bbbSJohan Hovold irq_data->chip_data = NULL;
1640495c38d3SDavid Daney
1641495c38d3SDavid Daney /* May (probably does) set hwirq, chip, etc. */
1642495c38d3SDavid Daney rv = irq_domain_alloc_irqs_hierarchy(domain, virq, 1, arg);
1643495c38d3SDavid Daney if (rv) {
1644495c38d3SDavid Daney /* Restore the original irq_data. */
1645930a1bbbSJohan Hovold *irq_data = *parent_irq_data;
1646930a1bbbSJohan Hovold kfree(parent_irq_data);
1647495c38d3SDavid Daney goto error;
1648495c38d3SDavid Daney }
1649495c38d3SDavid Daney
1650930a1bbbSJohan Hovold irq_domain_fix_revmap(parent_irq_data);
1651930a1bbbSJohan Hovold irq_domain_set_mapping(domain, irq_data->hwirq, irq_data);
1652495c38d3SDavid Daney error:
16539dbb8e34SJohan Hovold mutex_unlock(&domain->root->mutex);
1654495c38d3SDavid Daney
1655495c38d3SDavid Daney return rv;
1656495c38d3SDavid Daney }
1657495c38d3SDavid Daney EXPORT_SYMBOL_GPL(irq_domain_push_irq);
1658495c38d3SDavid Daney
1659495c38d3SDavid Daney /**
1660495c38d3SDavid Daney * irq_domain_pop_irq() - Remove a domain from the top of a hierarchy.
1661495c38d3SDavid Daney * @domain: Domain to remove.
1662495c38d3SDavid Daney * @virq: Irq to remove the domain from.
1663495c38d3SDavid Daney *
1664495c38d3SDavid Daney * Undo the effects of a call to irq_domain_push_irq(). Must be
1665495c38d3SDavid Daney * called either before request_irq() or after free_irq().
1666495c38d3SDavid Daney */
irq_domain_pop_irq(struct irq_domain * domain,int virq)1667495c38d3SDavid Daney int irq_domain_pop_irq(struct irq_domain *domain, int virq)
1668495c38d3SDavid Daney {
1669930a1bbbSJohan Hovold struct irq_data *irq_data = irq_get_irq_data(virq);
1670930a1bbbSJohan Hovold struct irq_data *parent_irq_data;
1671495c38d3SDavid Daney struct irq_data *tmp_irq_data;
1672495c38d3SDavid Daney struct irq_desc *desc;
1673495c38d3SDavid Daney
1674495c38d3SDavid Daney /*
1675495c38d3SDavid Daney * Check that no action is set, which indicates the virq is in
1676495c38d3SDavid Daney * a state where this function doesn't have to deal with races
1677495c38d3SDavid Daney * between interrupt handling and maintaining the hierarchy.
1678495c38d3SDavid Daney * This will catch gross misuse. Attempting to make the check
1679495c38d3SDavid Daney * race free would require holding locks across calls to
1680495c38d3SDavid Daney * struct irq_domain_ops->free(), which could lead to
1681495c38d3SDavid Daney * deadlock, so we just do a simple check before starting.
1682495c38d3SDavid Daney */
1683495c38d3SDavid Daney desc = irq_to_desc(virq);
1684495c38d3SDavid Daney if (!desc)
1685495c38d3SDavid Daney return -EINVAL;
1686495c38d3SDavid Daney if (WARN_ON(desc->action))
1687495c38d3SDavid Daney return -EBUSY;
1688495c38d3SDavid Daney
1689495c38d3SDavid Daney if (domain == NULL)
1690495c38d3SDavid Daney return -EINVAL;
1691495c38d3SDavid Daney
1692930a1bbbSJohan Hovold if (!irq_data)
1693495c38d3SDavid Daney return -EINVAL;
1694495c38d3SDavid Daney
1695495c38d3SDavid Daney tmp_irq_data = irq_domain_get_irq_data(domain, virq);
1696495c38d3SDavid Daney
1697495c38d3SDavid Daney /* We can only "pop" if this domain is at the top of the list */
1698930a1bbbSJohan Hovold if (WARN_ON(irq_data != tmp_irq_data))
1699495c38d3SDavid Daney return -EINVAL;
1700495c38d3SDavid Daney
1701930a1bbbSJohan Hovold if (WARN_ON(irq_data->domain != domain))
1702495c38d3SDavid Daney return -EINVAL;
1703495c38d3SDavid Daney
1704930a1bbbSJohan Hovold parent_irq_data = irq_data->parent_data;
1705930a1bbbSJohan Hovold if (WARN_ON(!parent_irq_data))
1706495c38d3SDavid Daney return -EINVAL;
1707495c38d3SDavid Daney
17089dbb8e34SJohan Hovold mutex_lock(&domain->root->mutex);
1709495c38d3SDavid Daney
1710930a1bbbSJohan Hovold irq_data->parent_data = NULL;
1711495c38d3SDavid Daney
1712930a1bbbSJohan Hovold irq_domain_clear_mapping(domain, irq_data->hwirq);
1713495c38d3SDavid Daney irq_domain_free_irqs_hierarchy(domain, virq, 1);
1714495c38d3SDavid Daney
1715495c38d3SDavid Daney /* Restore the original irq_data. */
1716930a1bbbSJohan Hovold *irq_data = *parent_irq_data;
1717495c38d3SDavid Daney
1718930a1bbbSJohan Hovold irq_domain_fix_revmap(irq_data);
1719495c38d3SDavid Daney
17209dbb8e34SJohan Hovold mutex_unlock(&domain->root->mutex);
1721495c38d3SDavid Daney
1722930a1bbbSJohan Hovold kfree(parent_irq_data);
1723495c38d3SDavid Daney
1724495c38d3SDavid Daney return 0;
1725495c38d3SDavid Daney }
1726495c38d3SDavid Daney EXPORT_SYMBOL_GPL(irq_domain_pop_irq);
1727495c38d3SDavid Daney
1728f8264e34SJiang Liu /**
1729f8264e34SJiang Liu * irq_domain_free_irqs - Free IRQ number and associated data structures
1730f8264e34SJiang Liu * @virq: base IRQ number
1731f8264e34SJiang Liu * @nr_irqs: number of IRQs to free
1732f8264e34SJiang Liu */
irq_domain_free_irqs(unsigned int virq,unsigned int nr_irqs)1733f8264e34SJiang Liu void irq_domain_free_irqs(unsigned int virq, unsigned int nr_irqs)
1734f8264e34SJiang Liu {
1735f8264e34SJiang Liu struct irq_data *data = irq_get_irq_data(virq);
17369dbb8e34SJohan Hovold struct irq_domain *domain;
1737f8264e34SJiang Liu int i;
1738f8264e34SJiang Liu
1739f8264e34SJiang Liu if (WARN(!data || !data->domain || !data->domain->ops->free,
1740f8264e34SJiang Liu "NULL pointer, cannot free irq\n"))
1741f8264e34SJiang Liu return;
1742f8264e34SJiang Liu
17439dbb8e34SJohan Hovold domain = data->domain;
17449dbb8e34SJohan Hovold
17459dbb8e34SJohan Hovold mutex_lock(&domain->root->mutex);
1746f8264e34SJiang Liu for (i = 0; i < nr_irqs; i++)
1747f8264e34SJiang Liu irq_domain_remove_irq(virq + i);
17489dbb8e34SJohan Hovold irq_domain_free_irqs_hierarchy(domain, virq, nr_irqs);
17499dbb8e34SJohan Hovold mutex_unlock(&domain->root->mutex);
1750f8264e34SJiang Liu
1751f8264e34SJiang Liu irq_domain_free_irq_data(virq, nr_irqs);
1752f8264e34SJiang Liu irq_free_descs(virq, nr_irqs);
1753f8264e34SJiang Liu }
1754f8264e34SJiang Liu
1755f8264e34SJiang Liu /**
175636d72731SJiang Liu * irq_domain_alloc_irqs_parent - Allocate interrupts from parent domain
1757817aad5dSMarc Zyngier * @domain: Domain below which interrupts must be allocated
175836d72731SJiang Liu * @irq_base: Base IRQ number
175936d72731SJiang Liu * @nr_irqs: Number of IRQs to allocate
176036d72731SJiang Liu * @arg: Allocation data (arch/domain specific)
176136d72731SJiang Liu */
irq_domain_alloc_irqs_parent(struct irq_domain * domain,unsigned int irq_base,unsigned int nr_irqs,void * arg)176236d72731SJiang Liu int irq_domain_alloc_irqs_parent(struct irq_domain *domain,
176336d72731SJiang Liu unsigned int irq_base, unsigned int nr_irqs,
176436d72731SJiang Liu void *arg)
176536d72731SJiang Liu {
17666a6544e5SMarc Zyngier if (!domain->parent)
176736d72731SJiang Liu return -ENOSYS;
17686a6544e5SMarc Zyngier
17696a6544e5SMarc Zyngier return irq_domain_alloc_irqs_hierarchy(domain->parent, irq_base,
17706a6544e5SMarc Zyngier nr_irqs, arg);
177136d72731SJiang Liu }
177252b2a05fSQuan Nguyen EXPORT_SYMBOL_GPL(irq_domain_alloc_irqs_parent);
177336d72731SJiang Liu
177436d72731SJiang Liu /**
177536d72731SJiang Liu * irq_domain_free_irqs_parent - Free interrupts from parent domain
1776817aad5dSMarc Zyngier * @domain: Domain below which interrupts must be freed
177736d72731SJiang Liu * @irq_base: Base IRQ number
177836d72731SJiang Liu * @nr_irqs: Number of IRQs to free
177936d72731SJiang Liu */
irq_domain_free_irqs_parent(struct irq_domain * domain,unsigned int irq_base,unsigned int nr_irqs)178036d72731SJiang Liu void irq_domain_free_irqs_parent(struct irq_domain *domain,
178136d72731SJiang Liu unsigned int irq_base, unsigned int nr_irqs)
178236d72731SJiang Liu {
17836a6544e5SMarc Zyngier if (!domain->parent)
17846a6544e5SMarc Zyngier return;
17856a6544e5SMarc Zyngier
17866a6544e5SMarc Zyngier irq_domain_free_irqs_hierarchy(domain->parent, irq_base, nr_irqs);
178736d72731SJiang Liu }
178852b2a05fSQuan Nguyen EXPORT_SYMBOL_GPL(irq_domain_free_irqs_parent);
178936d72731SJiang Liu
__irq_domain_deactivate_irq(struct irq_data * irq_data)179008d85f3eSMarc Zyngier static void __irq_domain_deactivate_irq(struct irq_data *irq_data)
179108d85f3eSMarc Zyngier {
179208d85f3eSMarc Zyngier if (irq_data && irq_data->domain) {
179308d85f3eSMarc Zyngier struct irq_domain *domain = irq_data->domain;
179408d85f3eSMarc Zyngier
179508d85f3eSMarc Zyngier if (domain->ops->deactivate)
179608d85f3eSMarc Zyngier domain->ops->deactivate(domain, irq_data);
179708d85f3eSMarc Zyngier if (irq_data->parent_data)
179808d85f3eSMarc Zyngier __irq_domain_deactivate_irq(irq_data->parent_data);
179908d85f3eSMarc Zyngier }
180008d85f3eSMarc Zyngier }
180108d85f3eSMarc Zyngier
__irq_domain_activate_irq(struct irq_data * irqd,bool reserve)1802702cb0a0SThomas Gleixner static int __irq_domain_activate_irq(struct irq_data *irqd, bool reserve)
1803bb9b428aSThomas Gleixner {
1804bb9b428aSThomas Gleixner int ret = 0;
1805bb9b428aSThomas Gleixner
1806bb9b428aSThomas Gleixner if (irqd && irqd->domain) {
1807bb9b428aSThomas Gleixner struct irq_domain *domain = irqd->domain;
1808bb9b428aSThomas Gleixner
1809bb9b428aSThomas Gleixner if (irqd->parent_data)
181042e1cc2dSThomas Gleixner ret = __irq_domain_activate_irq(irqd->parent_data,
1811702cb0a0SThomas Gleixner reserve);
1812bb9b428aSThomas Gleixner if (!ret && domain->ops->activate) {
1813702cb0a0SThomas Gleixner ret = domain->ops->activate(domain, irqd, reserve);
1814bb9b428aSThomas Gleixner /* Rollback in case of error */
1815bb9b428aSThomas Gleixner if (ret && irqd->parent_data)
1816bb9b428aSThomas Gleixner __irq_domain_deactivate_irq(irqd->parent_data);
1817bb9b428aSThomas Gleixner }
1818bb9b428aSThomas Gleixner }
1819bb9b428aSThomas Gleixner return ret;
1820bb9b428aSThomas Gleixner }
1821bb9b428aSThomas Gleixner
182236d72731SJiang Liu /**
1823f8264e34SJiang Liu * irq_domain_activate_irq - Call domain_ops->activate recursively to activate
1824f8264e34SJiang Liu * interrupt
1825702cb0a0SThomas Gleixner * @irq_data: Outermost irq_data associated with interrupt
1826702cb0a0SThomas Gleixner * @reserve: If set only reserve an interrupt vector instead of assigning one
1827f8264e34SJiang Liu *
1828f8264e34SJiang Liu * This is the second step to call domain_ops->activate to program interrupt
1829f8264e34SJiang Liu * controllers, so the interrupt could actually get delivered.
1830f8264e34SJiang Liu */
irq_domain_activate_irq(struct irq_data * irq_data,bool reserve)1831702cb0a0SThomas Gleixner int irq_domain_activate_irq(struct irq_data *irq_data, bool reserve)
1832f8264e34SJiang Liu {
1833bb9b428aSThomas Gleixner int ret = 0;
1834bb9b428aSThomas Gleixner
1835bb9b428aSThomas Gleixner if (!irqd_is_activated(irq_data))
1836702cb0a0SThomas Gleixner ret = __irq_domain_activate_irq(irq_data, reserve);
1837bb9b428aSThomas Gleixner if (!ret)
183808d85f3eSMarc Zyngier irqd_set_activated(irq_data);
1839bb9b428aSThomas Gleixner return ret;
1840f8264e34SJiang Liu }
1841f8264e34SJiang Liu
1842f8264e34SJiang Liu /**
1843f8264e34SJiang Liu * irq_domain_deactivate_irq - Call domain_ops->deactivate recursively to
1844f8264e34SJiang Liu * deactivate interrupt
1845f8264e34SJiang Liu * @irq_data: outermost irq_data associated with interrupt
1846f8264e34SJiang Liu *
1847f8264e34SJiang Liu * It calls domain_ops->deactivate to program interrupt controllers to disable
1848f8264e34SJiang Liu * interrupt delivery.
1849f8264e34SJiang Liu */
irq_domain_deactivate_irq(struct irq_data * irq_data)1850f8264e34SJiang Liu void irq_domain_deactivate_irq(struct irq_data *irq_data)
1851f8264e34SJiang Liu {
185208d85f3eSMarc Zyngier if (irqd_is_activated(irq_data)) {
185308d85f3eSMarc Zyngier __irq_domain_deactivate_irq(irq_data);
185408d85f3eSMarc Zyngier irqd_clr_activated(irq_data);
1855f8264e34SJiang Liu }
1856f8264e34SJiang Liu }
1857f8264e34SJiang Liu
irq_domain_check_hierarchy(struct irq_domain * domain)1858f8264e34SJiang Liu static void irq_domain_check_hierarchy(struct irq_domain *domain)
1859f8264e34SJiang Liu {
1860f8264e34SJiang Liu /* Hierarchy irq_domains must implement callback alloc() */
1861f8264e34SJiang Liu if (domain->ops->alloc)
1862f8264e34SJiang Liu domain->flags |= IRQ_DOMAIN_FLAG_HIERARCHY;
1863f8264e34SJiang Liu }
1864f8264e34SJiang Liu #else /* CONFIG_IRQ_DOMAIN_HIERARCHY */
1865f8264e34SJiang Liu /**
1866f8264e34SJiang Liu * irq_domain_get_irq_data - Get irq_data associated with @virq and @domain
1867f8264e34SJiang Liu * @domain: domain to match
1868f8264e34SJiang Liu * @virq: IRQ number to get irq_data
1869f8264e34SJiang Liu */
irq_domain_get_irq_data(struct irq_domain * domain,unsigned int virq)1870f8264e34SJiang Liu struct irq_data *irq_domain_get_irq_data(struct irq_domain *domain,
1871f8264e34SJiang Liu unsigned int virq)
1872f8264e34SJiang Liu {
1873f8264e34SJiang Liu struct irq_data *irq_data = irq_get_irq_data(virq);
1874f8264e34SJiang Liu
1875f8264e34SJiang Liu return (irq_data && irq_data->domain == domain) ? irq_data : NULL;
1876f8264e34SJiang Liu }
1877a4289dc2SJake Oshins EXPORT_SYMBOL_GPL(irq_domain_get_irq_data);
1878f8264e34SJiang Liu
18795f22f5c6SStefan Agner /**
18805f22f5c6SStefan Agner * irq_domain_set_info - Set the complete data for a @virq in @domain
18815f22f5c6SStefan Agner * @domain: Interrupt domain to match
18825f22f5c6SStefan Agner * @virq: IRQ number
18835f22f5c6SStefan Agner * @hwirq: The hardware interrupt number
18845f22f5c6SStefan Agner * @chip: The associated interrupt chip
18855f22f5c6SStefan Agner * @chip_data: The associated interrupt chip data
18865f22f5c6SStefan Agner * @handler: The interrupt flow handler
18875f22f5c6SStefan Agner * @handler_data: The interrupt flow handler data
18885f22f5c6SStefan Agner * @handler_name: The interrupt handler name
18895f22f5c6SStefan Agner */
irq_domain_set_info(struct irq_domain * domain,unsigned int virq,irq_hw_number_t hwirq,const struct irq_chip * chip,void * chip_data,irq_flow_handler_t handler,void * handler_data,const char * handler_name)18905f22f5c6SStefan Agner void irq_domain_set_info(struct irq_domain *domain, unsigned int virq,
189145ec846cSMarc Zyngier irq_hw_number_t hwirq, const struct irq_chip *chip,
18925f22f5c6SStefan Agner void *chip_data, irq_flow_handler_t handler,
18935f22f5c6SStefan Agner void *handler_data, const char *handler_name)
18945f22f5c6SStefan Agner {
18955f22f5c6SStefan Agner irq_set_chip_and_handler_name(virq, chip, handler, handler_name);
18965f22f5c6SStefan Agner irq_set_chip_data(virq, chip_data);
18975f22f5c6SStefan Agner irq_set_handler_data(virq, handler_data);
18985f22f5c6SStefan Agner }
18995f22f5c6SStefan Agner
irq_domain_alloc_irqs_locked(struct irq_domain * domain,int irq_base,unsigned int nr_irqs,int node,void * arg,bool realloc,const struct irq_affinity_desc * affinity)1900601363ccSJohan Hovold static int irq_domain_alloc_irqs_locked(struct irq_domain *domain, int irq_base,
1901601363ccSJohan Hovold unsigned int nr_irqs, int node, void *arg,
1902601363ccSJohan Hovold bool realloc, const struct irq_affinity_desc *affinity)
1903601363ccSJohan Hovold {
1904601363ccSJohan Hovold return -EINVAL;
1905601363ccSJohan Hovold }
1906601363ccSJohan Hovold
irq_domain_check_hierarchy(struct irq_domain * domain)1907f8264e34SJiang Liu static void irq_domain_check_hierarchy(struct irq_domain *domain)
1908f8264e34SJiang Liu {
1909f8264e34SJiang Liu }
1910f8264e34SJiang Liu #endif /* CONFIG_IRQ_DOMAIN_HIERARCHY */
1911087cdfb6SThomas Gleixner
1912087cdfb6SThomas Gleixner #ifdef CONFIG_GENERIC_IRQ_DEBUGFS
19138091f56eSArnd Bergmann #include "internals.h"
19148091f56eSArnd Bergmann
1915087cdfb6SThomas Gleixner static struct dentry *domain_dir;
1916087cdfb6SThomas Gleixner
1917087cdfb6SThomas Gleixner static void
irq_domain_debug_show_one(struct seq_file * m,struct irq_domain * d,int ind)1918087cdfb6SThomas Gleixner irq_domain_debug_show_one(struct seq_file *m, struct irq_domain *d, int ind)
1919087cdfb6SThomas Gleixner {
1920087cdfb6SThomas Gleixner seq_printf(m, "%*sname: %s\n", ind, "", d->name);
19214f86a06eSMarc Zyngier seq_printf(m, "%*ssize: %u\n", ind + 1, "", d->revmap_size);
1922087cdfb6SThomas Gleixner seq_printf(m, "%*smapped: %u\n", ind + 1, "", d->mapcount);
1923087cdfb6SThomas Gleixner seq_printf(m, "%*sflags: 0x%08x\n", ind +1 , "", d->flags);
1924c3e7239aSThomas Gleixner if (d->ops && d->ops->debug_show)
1925c3e7239aSThomas Gleixner d->ops->debug_show(m, d, NULL, ind + 1);
1926087cdfb6SThomas Gleixner #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
1927087cdfb6SThomas Gleixner if (!d->parent)
1928087cdfb6SThomas Gleixner return;
1929087cdfb6SThomas Gleixner seq_printf(m, "%*sparent: %s\n", ind + 1, "", d->parent->name);
1930087cdfb6SThomas Gleixner irq_domain_debug_show_one(m, d->parent, ind + 4);
1931087cdfb6SThomas Gleixner #endif
1932087cdfb6SThomas Gleixner }
1933087cdfb6SThomas Gleixner
irq_domain_debug_show(struct seq_file * m,void * p)1934087cdfb6SThomas Gleixner static int irq_domain_debug_show(struct seq_file *m, void *p)
1935087cdfb6SThomas Gleixner {
1936087cdfb6SThomas Gleixner struct irq_domain *d = m->private;
1937087cdfb6SThomas Gleixner
1938087cdfb6SThomas Gleixner /* Default domain? Might be NULL */
1939087cdfb6SThomas Gleixner if (!d) {
1940087cdfb6SThomas Gleixner if (!irq_default_domain)
1941087cdfb6SThomas Gleixner return 0;
1942087cdfb6SThomas Gleixner d = irq_default_domain;
1943087cdfb6SThomas Gleixner }
1944087cdfb6SThomas Gleixner irq_domain_debug_show_one(m, d, 0);
1945087cdfb6SThomas Gleixner return 0;
1946087cdfb6SThomas Gleixner }
19470b24a0bbSAndy Shevchenko DEFINE_SHOW_ATTRIBUTE(irq_domain_debug);
1948087cdfb6SThomas Gleixner
debugfs_add_domain_dir(struct irq_domain * d)1949087cdfb6SThomas Gleixner static void debugfs_add_domain_dir(struct irq_domain *d)
1950087cdfb6SThomas Gleixner {
195169dd4503SGreg Kroah-Hartman if (!d->name || !domain_dir)
1952087cdfb6SThomas Gleixner return;
195369dd4503SGreg Kroah-Hartman debugfs_create_file(d->name, 0444, domain_dir, d,
19540b24a0bbSAndy Shevchenko &irq_domain_debug_fops);
1955087cdfb6SThomas Gleixner }
1956087cdfb6SThomas Gleixner
debugfs_remove_domain_dir(struct irq_domain * d)1957087cdfb6SThomas Gleixner static void debugfs_remove_domain_dir(struct irq_domain *d)
1958087cdfb6SThomas Gleixner {
1959d83d7ed2SGreg Kroah-Hartman debugfs_lookup_and_remove(d->name, domain_dir);
1960087cdfb6SThomas Gleixner }
1961087cdfb6SThomas Gleixner
irq_domain_debugfs_init(struct dentry * root)1962087cdfb6SThomas Gleixner void __init irq_domain_debugfs_init(struct dentry *root)
1963087cdfb6SThomas Gleixner {
1964087cdfb6SThomas Gleixner struct irq_domain *d;
1965087cdfb6SThomas Gleixner
1966087cdfb6SThomas Gleixner domain_dir = debugfs_create_dir("domains", root);
1967087cdfb6SThomas Gleixner
19680b24a0bbSAndy Shevchenko debugfs_create_file("default", 0444, domain_dir, NULL,
19690b24a0bbSAndy Shevchenko &irq_domain_debug_fops);
1970087cdfb6SThomas Gleixner mutex_lock(&irq_domain_mutex);
1971087cdfb6SThomas Gleixner list_for_each_entry(d, &irq_domain_list, link)
1972087cdfb6SThomas Gleixner debugfs_add_domain_dir(d);
1973087cdfb6SThomas Gleixner mutex_unlock(&irq_domain_mutex);
1974087cdfb6SThomas Gleixner }
1975087cdfb6SThomas Gleixner #endif
1976