blob: a453e229f99caf404bf93575abff805b56c4d3d5 [file] [log] [blame]
Thomas Gleixner52a65ff2018-03-14 22:15:19 +01001// SPDX-License-Identifier: GPL-2.0
2
Paul Mundt54a90582012-05-19 15:11:47 +09003#define pr_fmt(fmt) "irq: " fmt
4
Marc Zyngierc5c601c2017-07-07 09:39:59 +01005#include <linux/acpi.h>
Grant Likelycc79ca62012-02-16 01:37:49 -07006#include <linux/debugfs.h>
7#include <linux/hardirq.h>
8#include <linux/interrupt.h>
Grant Likely08a543a2011-07-26 03:19:06 -06009#include <linux/irq.h>
Grant Likelycc79ca62012-02-16 01:37:49 -070010#include <linux/irqdesc.h>
Grant Likely08a543a2011-07-26 03:19:06 -060011#include <linux/irqdomain.h>
12#include <linux/module.h>
13#include <linux/mutex.h>
14#include <linux/of.h>
Grant Likely7e713302011-07-26 03:19:06 -060015#include <linux/of_address.h>
Rashika Kheria64be38a2014-02-27 17:10:12 +053016#include <linux/of_irq.h>
Paul Mundt5ca4db62012-06-03 22:04:34 -070017#include <linux/topology.h>
Grant Likelycc79ca62012-02-16 01:37:49 -070018#include <linux/seq_file.h>
Grant Likely7e713302011-07-26 03:19:06 -060019#include <linux/slab.h>
Grant Likelycc79ca62012-02-16 01:37:49 -070020#include <linux/smp.h>
21#include <linux/fs.h>
Grant Likely08a543a2011-07-26 03:19:06 -060022
23static LIST_HEAD(irq_domain_list);
24static DEFINE_MUTEX(irq_domain_mutex);
25
Grant Likely68700652012-02-14 14:06:53 -070026static struct irq_domain *irq_default_domain;
Grant Likelycc79ca62012-02-16 01:37:49 -070027
Jiang Liuf8264e32014-11-06 22:20:14 +080028static void irq_domain_check_hierarchy(struct irq_domain *domain);
29
Marc Zyngierb145dcc2015-10-13 12:51:36 +010030struct irqchip_fwid {
Thomas Gleixnerd59f6612017-06-20 01:37:05 +020031 struct fwnode_handle fwnode;
32 unsigned int type;
33 char *name;
Thomas Gleixner087cdfb2017-06-20 01:37:17 +020034 void *data;
Marc Zyngierb145dcc2015-10-13 12:51:36 +010035};
36
Thomas Gleixner087cdfb2017-06-20 01:37:17 +020037#ifdef CONFIG_GENERIC_IRQ_DEBUGFS
38static void debugfs_add_domain_dir(struct irq_domain *d);
39static void debugfs_remove_domain_dir(struct irq_domain *d);
40#else
41static inline void debugfs_add_domain_dir(struct irq_domain *d) { }
42static inline void debugfs_remove_domain_dir(struct irq_domain *d) { }
43#endif
44
Sakari Ailusdb3e50f2017-07-21 14:39:31 +030045const struct fwnode_operations irqchip_fwnode_ops;
Arnd Bergmannb6eb66f2017-07-26 02:19:35 +020046EXPORT_SYMBOL_GPL(irqchip_fwnode_ops);
Sakari Ailusdb3e50f2017-07-21 14:39:31 +030047
Marc Zyngierb145dcc2015-10-13 12:51:36 +010048/**
49 * irq_domain_alloc_fwnode - Allocate a fwnode_handle suitable for
50 * identifying an irq domain
Thomas Gleixnerd59f6612017-06-20 01:37:05 +020051 * @type: Type of irqchip_fwnode. See linux/irqdomain.h
52 * @name: Optional user provided domain name
53 * @id: Optional user provided id if name != NULL
54 * @data: Optional user-provided data
Marc Zyngierb145dcc2015-10-13 12:51:36 +010055 *
Thomas Gleixnerd59f6612017-06-20 01:37:05 +020056 * Allocate a struct irqchip_fwid, and return a poiner to the embedded
Marc Zyngierb145dcc2015-10-13 12:51:36 +010057 * fwnode_handle (or NULL on failure).
Thomas Gleixnerd59f6612017-06-20 01:37:05 +020058 *
59 * Note: The types IRQCHIP_FWNODE_NAMED and IRQCHIP_FWNODE_NAMED_ID are
60 * solely to transport name information to irqdomain creation code. The
61 * node is not stored. For other types the pointer is kept in the irq
62 * domain struct.
Marc Zyngierb145dcc2015-10-13 12:51:36 +010063 */
Thomas Gleixnerd59f6612017-06-20 01:37:05 +020064struct fwnode_handle *__irq_domain_alloc_fwnode(unsigned int type, int id,
65 const char *name, void *data)
Marc Zyngierb145dcc2015-10-13 12:51:36 +010066{
67 struct irqchip_fwid *fwid;
Thomas Gleixnerd59f6612017-06-20 01:37:05 +020068 char *n;
Marc Zyngierb145dcc2015-10-13 12:51:36 +010069
70 fwid = kzalloc(sizeof(*fwid), GFP_KERNEL);
Marc Zyngierb145dcc2015-10-13 12:51:36 +010071
Thomas Gleixnerd59f6612017-06-20 01:37:05 +020072 switch (type) {
73 case IRQCHIP_FWNODE_NAMED:
74 n = kasprintf(GFP_KERNEL, "%s", name);
75 break;
76 case IRQCHIP_FWNODE_NAMED_ID:
77 n = kasprintf(GFP_KERNEL, "%s-%d", name, id);
78 break;
79 default:
80 n = kasprintf(GFP_KERNEL, "irqchip@%p", data);
81 break;
82 }
83
84 if (!fwid || !n) {
Marc Zyngierb145dcc2015-10-13 12:51:36 +010085 kfree(fwid);
Thomas Gleixnerd59f6612017-06-20 01:37:05 +020086 kfree(n);
Marc Zyngierb145dcc2015-10-13 12:51:36 +010087 return NULL;
88 }
89
Thomas Gleixnerd59f6612017-06-20 01:37:05 +020090 fwid->type = type;
91 fwid->name = n;
Marc Zyngierb145dcc2015-10-13 12:51:36 +010092 fwid->data = data;
Sakari Ailusdb3e50f2017-07-21 14:39:31 +030093 fwid->fwnode.ops = &irqchip_fwnode_ops;
Marc Zyngierb145dcc2015-10-13 12:51:36 +010094 return &fwid->fwnode;
95}
Thomas Gleixnerd59f6612017-06-20 01:37:05 +020096EXPORT_SYMBOL_GPL(__irq_domain_alloc_fwnode);
Marc Zyngierb145dcc2015-10-13 12:51:36 +010097
98/**
99 * irq_domain_free_fwnode - Free a non-OF-backed fwnode_handle
100 *
101 * Free a fwnode_handle allocated with irq_domain_alloc_fwnode.
102 */
103void irq_domain_free_fwnode(struct fwnode_handle *fwnode)
104{
105 struct irqchip_fwid *fwid;
106
Suravee Suthikulpanit75aba7b2015-12-10 08:55:28 -0800107 if (WARN_ON(!is_fwnode_irqchip(fwnode)))
Marc Zyngierb145dcc2015-10-13 12:51:36 +0100108 return;
109
110 fwid = container_of(fwnode, struct irqchip_fwid, fwnode);
111 kfree(fwid->name);
112 kfree(fwid);
113}
Jake Oshinsa4289dc2015-12-10 17:52:59 +0000114EXPORT_SYMBOL_GPL(irq_domain_free_fwnode);
Marc Zyngierb145dcc2015-10-13 12:51:36 +0100115
Grant Likelycc79ca62012-02-16 01:37:49 -0700116/**
Grant Likelyfa40f372013-06-08 12:57:40 +0100117 * __irq_domain_add() - Allocate a new irq_domain data structure
Punit Agrawal545d5d62016-05-31 13:56:48 +0100118 * @fwnode: firmware node for the interrupt controller
Grant Likelyfa40f372013-06-08 12:57:40 +0100119 * @size: Size of linear map; 0 for radix mapping only
Jiang Liua2579542014-05-27 16:07:37 +0800120 * @hwirq_max: Maximum number of interrupts supported by controller
Grant Likelyfa40f372013-06-08 12:57:40 +0100121 * @direct_max: Maximum value of direct maps; Use ~0 for no limit; 0 for no
122 * direct mapping
Jiang Liuf8264e32014-11-06 22:20:14 +0800123 * @ops: domain callbacks
Grant Likelya8db8cf2012-02-14 14:06:54 -0700124 * @host_data: Controller private data pointer
Grant Likelycc79ca62012-02-16 01:37:49 -0700125 *
Jiang Liua2579542014-05-27 16:07:37 +0800126 * Allocates and initialize and irq_domain structure.
127 * Returns pointer to IRQ domain, or NULL on failure.
Grant Likelycc79ca62012-02-16 01:37:49 -0700128 */
Marc Zyngier1bf4ddc2015-10-13 12:51:35 +0100129struct irq_domain *__irq_domain_add(struct fwnode_handle *fwnode, int size,
Grant Likelyddaf1442013-06-10 01:06:02 +0100130 irq_hw_number_t hwirq_max, int direct_max,
Grant Likelyfa40f372013-06-08 12:57:40 +0100131 const struct irq_domain_ops *ops,
132 void *host_data)
Grant Likelycc79ca62012-02-16 01:37:49 -0700133{
Punit Agrawal545d5d62016-05-31 13:56:48 +0100134 struct device_node *of_node = to_of_node(fwnode);
Thomas Gleixnerd59f6612017-06-20 01:37:05 +0200135 struct irqchip_fwid *fwid;
Grant Likelya8db8cf2012-02-14 14:06:54 -0700136 struct irq_domain *domain;
Grant Likelycc79ca62012-02-16 01:37:49 -0700137
Thomas Gleixnerd59f6612017-06-20 01:37:05 +0200138 static atomic_t unknown_domains;
139
Grant Likelycef50752012-07-11 17:24:31 +0100140 domain = kzalloc_node(sizeof(*domain) + (sizeof(unsigned int) * size),
141 GFP_KERNEL, of_node_to_nid(of_node));
Grant Likelya8db8cf2012-02-14 14:06:54 -0700142 if (WARN_ON(!domain))
Grant Likelycc79ca62012-02-16 01:37:49 -0700143 return NULL;
144
Thomas Gleixnerd59f6612017-06-20 01:37:05 +0200145 if (fwnode && is_fwnode_irqchip(fwnode)) {
146 fwid = container_of(fwnode, struct irqchip_fwid, fwnode);
147
148 switch (fwid->type) {
149 case IRQCHIP_FWNODE_NAMED:
150 case IRQCHIP_FWNODE_NAMED_ID:
151 domain->name = kstrdup(fwid->name, GFP_KERNEL);
152 if (!domain->name) {
153 kfree(domain);
154 return NULL;
155 }
156 domain->flags |= IRQ_DOMAIN_NAME_ALLOCATED;
157 break;
158 default:
159 domain->fwnode = fwnode;
160 domain->name = fwid->name;
161 break;
162 }
Marc Zyngierc5c601c2017-07-07 09:39:59 +0100163#ifdef CONFIG_ACPI
164 } else if (is_acpi_device_node(fwnode)) {
165 struct acpi_buffer buf = {
166 .length = ACPI_ALLOCATE_BUFFER,
167 };
168 acpi_handle handle;
169
170 handle = acpi_device_handle(to_acpi_device_node(fwnode));
171 if (acpi_get_name(handle, ACPI_FULL_PATHNAME, &buf) == AE_OK) {
172 domain->name = buf.pointer;
173 domain->flags |= IRQ_DOMAIN_NAME_ALLOCATED;
174 }
175
176 domain->fwnode = fwnode;
177#endif
Thomas Gleixnerd59f6612017-06-20 01:37:05 +0200178 } else if (of_node) {
179 char *name;
180
181 /*
182 * DT paths contain '/', which debugfs is legitimately
183 * unhappy about. Replace them with ':', which does
184 * the trick and is not as offensive as '\'...
185 */
Marc Zyngier94967b52018-10-01 11:05:22 +0100186 name = kasprintf(GFP_KERNEL, "%pOF", of_node);
Thomas Gleixnerd59f6612017-06-20 01:37:05 +0200187 if (!name) {
188 kfree(domain);
189 return NULL;
190 }
191
192 strreplace(name, '/', ':');
193
194 domain->name = name;
195 domain->fwnode = fwnode;
196 domain->flags |= IRQ_DOMAIN_NAME_ALLOCATED;
197 }
198
199 if (!domain->name) {
Sakari Ailusdb3e50f2017-07-21 14:39:31 +0300200 if (fwnode)
201 pr_err("Invalid fwnode type for irqdomain\n");
Thomas Gleixnerd59f6612017-06-20 01:37:05 +0200202 domain->name = kasprintf(GFP_KERNEL, "unknown-%d",
203 atomic_inc_return(&unknown_domains));
204 if (!domain->name) {
205 kfree(domain);
206 return NULL;
207 }
208 domain->flags |= IRQ_DOMAIN_NAME_ALLOCATED;
209 }
210
Marc Zyngierf1107112015-10-13 12:51:30 +0100211 of_node_get(of_node);
Marc Zyngierf1107112015-10-13 12:51:30 +0100212
Grant Likelycc79ca62012-02-16 01:37:49 -0700213 /* Fill structure */
Grant Likely1aa0dd92013-06-08 12:03:59 +0100214 INIT_RADIX_TREE(&domain->revmap_tree, GFP_KERNEL);
Masahiro Yamadaf1d78352017-10-05 10:44:54 +0900215 mutex_init(&domain->revmap_tree_mutex);
Grant Likely68700652012-02-14 14:06:53 -0700216 domain->ops = ops;
Grant Likelya8db8cf2012-02-14 14:06:54 -0700217 domain->host_data = host_data;
Grant Likelyddaf1442013-06-10 01:06:02 +0100218 domain->hwirq_max = hwirq_max;
Grant Likely1aa0dd92013-06-08 12:03:59 +0100219 domain->revmap_size = size;
Grant Likelyfa40f372013-06-08 12:57:40 +0100220 domain->revmap_direct_max_irq = direct_max;
Jiang Liuf8264e32014-11-06 22:20:14 +0800221 irq_domain_check_hierarchy(domain);
Grant Likelycc79ca62012-02-16 01:37:49 -0700222
Grant Likelya8db8cf2012-02-14 14:06:54 -0700223 mutex_lock(&irq_domain_mutex);
Thomas Gleixner087cdfb2017-06-20 01:37:17 +0200224 debugfs_add_domain_dir(domain);
Grant Likelya8db8cf2012-02-14 14:06:54 -0700225 list_add(&domain->link, &irq_domain_list);
226 mutex_unlock(&irq_domain_mutex);
Grant Likelyfa40f372013-06-08 12:57:40 +0100227
Grant Likely1aa0dd92013-06-08 12:03:59 +0100228 pr_debug("Added domain %s\n", domain->name);
Grant Likelyfa40f372013-06-08 12:57:40 +0100229 return domain;
Grant Likelya8db8cf2012-02-14 14:06:54 -0700230}
Grant Likelyfa40f372013-06-08 12:57:40 +0100231EXPORT_SYMBOL_GPL(__irq_domain_add);
Grant Likelya8db8cf2012-02-14 14:06:54 -0700232
Paul Mundt58ee99a2012-05-19 15:11:41 +0900233/**
234 * irq_domain_remove() - Remove an irq domain.
235 * @domain: domain to remove
236 *
237 * This routine is used to remove an irq domain. The caller must ensure
238 * that all mappings within the domain have been disposed of prior to
239 * use, depending on the revmap type.
240 */
241void irq_domain_remove(struct irq_domain *domain)
242{
243 mutex_lock(&irq_domain_mutex);
Thomas Gleixner087cdfb2017-06-20 01:37:17 +0200244 debugfs_remove_domain_dir(domain);
Paul Mundt58ee99a2012-05-19 15:11:41 +0900245
Matthew Wilcoxe9256ef2016-05-20 17:01:33 -0700246 WARN_ON(!radix_tree_empty(&domain->revmap_tree));
Paul Mundt58ee99a2012-05-19 15:11:41 +0900247
248 list_del(&domain->link);
249
250 /*
251 * If the going away domain is the default one, reset it.
252 */
253 if (unlikely(irq_default_domain == domain))
254 irq_set_default_host(NULL);
255
256 mutex_unlock(&irq_domain_mutex);
257
Grant Likely1aa0dd92013-06-08 12:03:59 +0100258 pr_debug("Removed domain %s\n", domain->name);
Paul Mundt58ee99a2012-05-19 15:11:41 +0900259
Marc Zyngier5d4c9bc2015-10-13 12:51:29 +0100260 of_node_put(irq_domain_get_of_node(domain));
Thomas Gleixnerd59f6612017-06-20 01:37:05 +0200261 if (domain->flags & IRQ_DOMAIN_NAME_ALLOCATED)
262 kfree(domain->name);
Grant Likelyfa40f372013-06-08 12:57:40 +0100263 kfree(domain);
Paul Mundt58ee99a2012-05-19 15:11:41 +0900264}
Paul Mundtecd84eb2012-05-19 15:11:42 +0900265EXPORT_SYMBOL_GPL(irq_domain_remove);
Paul Mundt58ee99a2012-05-19 15:11:41 +0900266
Marc Zyngier61d0a002017-06-22 11:34:57 +0100267void irq_domain_update_bus_token(struct irq_domain *domain,
268 enum irq_domain_bus_token bus_token)
269{
270 char *name;
271
272 if (domain->bus_token == bus_token)
273 return;
274
275 mutex_lock(&irq_domain_mutex);
276
277 domain->bus_token = bus_token;
278
279 name = kasprintf(GFP_KERNEL, "%s-%d", domain->name, bus_token);
280 if (!name) {
281 mutex_unlock(&irq_domain_mutex);
282 return;
283 }
284
285 debugfs_remove_domain_dir(domain);
286
287 if (domain->flags & IRQ_DOMAIN_NAME_ALLOCATED)
288 kfree(domain->name);
289 else
290 domain->flags |= IRQ_DOMAIN_NAME_ALLOCATED;
291
292 domain->name = name;
293 debugfs_add_domain_dir(domain);
294
295 mutex_unlock(&irq_domain_mutex);
296}
297
Grant Likelya8db8cf2012-02-14 14:06:54 -0700298/**
Grant Likelyfa40f372013-06-08 12:57:40 +0100299 * irq_domain_add_simple() - Register an irq_domain and optionally map a range of irqs
Mark Brown781d0f42012-07-05 12:19:19 +0100300 * @of_node: pointer to interrupt controller's device tree node.
301 * @size: total number of irqs in mapping
Linus Walleij94a63da2013-06-06 12:10:23 +0100302 * @first_irq: first number of irq block assigned to the domain,
Grant Likelyfa40f372013-06-08 12:57:40 +0100303 * pass zero to assign irqs on-the-fly. If first_irq is non-zero, then
304 * pre-map all of the irqs in the domain to virqs starting at first_irq.
Jiang Liuf8264e32014-11-06 22:20:14 +0800305 * @ops: domain callbacks
Mark Brown781d0f42012-07-05 12:19:19 +0100306 * @host_data: Controller private data pointer
307 *
Grant Likelyfa40f372013-06-08 12:57:40 +0100308 * Allocates an irq_domain, and optionally if first_irq is positive then also
309 * allocate irq_descs and map all of the hwirqs to virqs starting at first_irq.
Mark Brown781d0f42012-07-05 12:19:19 +0100310 *
311 * This is intended to implement the expected behaviour for most
Grant Likelyfa40f372013-06-08 12:57:40 +0100312 * interrupt controllers. If device tree is used, then first_irq will be 0 and
313 * irqs get mapped dynamically on the fly. However, if the controller requires
314 * static virq assignments (non-DT boot) then it will set that up correctly.
Mark Brown781d0f42012-07-05 12:19:19 +0100315 */
316struct irq_domain *irq_domain_add_simple(struct device_node *of_node,
317 unsigned int size,
318 unsigned int first_irq,
319 const struct irq_domain_ops *ops,
320 void *host_data)
321{
Grant Likelyfa40f372013-06-08 12:57:40 +0100322 struct irq_domain *domain;
Linus Walleij2854d162012-09-27 14:59:39 +0200323
Marc Zyngier1bf4ddc2015-10-13 12:51:35 +0100324 domain = __irq_domain_add(of_node_to_fwnode(of_node), size, size, 0, ops, host_data);
Grant Likelyfa40f372013-06-08 12:57:40 +0100325 if (!domain)
326 return NULL;
327
328 if (first_irq > 0) {
Linus Walleij2854d162012-09-27 14:59:39 +0200329 if (IS_ENABLED(CONFIG_SPARSE_IRQ)) {
Grant Likelyfa40f372013-06-08 12:57:40 +0100330 /* attempt to allocated irq_descs */
331 int rc = irq_alloc_descs(first_irq, first_irq, size,
332 of_node_to_nid(of_node));
333 if (rc < 0)
Linus Walleijd202b7b2012-11-27 01:20:32 +0100334 pr_info("Cannot allocate irq_descs @ IRQ%d, assuming pre-allocated\n",
335 first_irq);
Grant Likelyfa40f372013-06-08 12:57:40 +0100336 }
Grant Likelyddaf1442013-06-10 01:06:02 +0100337 irq_domain_associate_many(domain, first_irq, 0, size);
Linus Walleij2854d162012-09-27 14:59:39 +0200338 }
339
Grant Likelyfa40f372013-06-08 12:57:40 +0100340 return domain;
Mark Brown781d0f42012-07-05 12:19:19 +0100341}
Arnd Bergmann346dbb72013-04-25 19:28:54 +0200342EXPORT_SYMBOL_GPL(irq_domain_add_simple);
Mark Brown781d0f42012-07-05 12:19:19 +0100343
344/**
Grant Likelya8db8cf2012-02-14 14:06:54 -0700345 * irq_domain_add_legacy() - Allocate and register a legacy revmap irq_domain.
346 * @of_node: pointer to interrupt controller's device tree node.
Grant Likely1bc04f22012-02-14 14:06:55 -0700347 * @size: total number of irqs in legacy mapping
348 * @first_irq: first number of irq block assigned to the domain
349 * @first_hwirq: first hwirq number to use for the translation. Should normally
350 * be '0', but a positive integer can be used if the effective
351 * hwirqs numbering does not begin at zero.
Grant Likelya8db8cf2012-02-14 14:06:54 -0700352 * @ops: map/unmap domain callbacks
353 * @host_data: Controller private data pointer
354 *
355 * Note: the map() callback will be called before this function returns
356 * for all legacy interrupts except 0 (which is always the invalid irq for
357 * a legacy controller).
358 */
359struct irq_domain *irq_domain_add_legacy(struct device_node *of_node,
Grant Likely1bc04f22012-02-14 14:06:55 -0700360 unsigned int size,
361 unsigned int first_irq,
362 irq_hw_number_t first_hwirq,
Grant Likelya18dc812012-01-26 12:12:14 -0700363 const struct irq_domain_ops *ops,
Grant Likelya8db8cf2012-02-14 14:06:54 -0700364 void *host_data)
365{
Grant Likely1bc04f22012-02-14 14:06:55 -0700366 struct irq_domain *domain;
Grant Likelya8db8cf2012-02-14 14:06:54 -0700367
Marc Zyngier1bf4ddc2015-10-13 12:51:35 +0100368 domain = __irq_domain_add(of_node_to_fwnode(of_node), first_hwirq + size,
Grant Likelyddaf1442013-06-10 01:06:02 +0100369 first_hwirq + size, 0, ops, host_data);
Jiang Liuf8264e32014-11-06 22:20:14 +0800370 if (domain)
371 irq_domain_associate_many(domain, first_irq, first_hwirq, size);
Grant Likely1bc04f22012-02-14 14:06:55 -0700372
Grant Likelya8db8cf2012-02-14 14:06:54 -0700373 return domain;
374}
Paul Mundtecd84eb2012-05-19 15:11:42 +0900375EXPORT_SYMBOL_GPL(irq_domain_add_legacy);
Grant Likelycc79ca62012-02-16 01:37:49 -0700376
Grant Likelya8db8cf2012-02-14 14:06:54 -0700377/**
Marc Zyngier651e8b52016-04-11 09:57:51 +0100378 * irq_find_matching_fwspec() - Locates a domain for a given fwspec
379 * @fwspec: FW specifier for an interrupt
Marc Zyngierad3aedf2015-07-28 14:46:08 +0100380 * @bus_token: domain-specific data
Grant Likelycc79ca62012-02-16 01:37:49 -0700381 */
Marc Zyngier651e8b52016-04-11 09:57:51 +0100382struct irq_domain *irq_find_matching_fwspec(struct irq_fwspec *fwspec,
Marc Zyngier130b8c62015-10-13 12:51:31 +0100383 enum irq_domain_bus_token bus_token)
Grant Likelycc79ca62012-02-16 01:37:49 -0700384{
385 struct irq_domain *h, *found = NULL;
Marc Zyngier651e8b52016-04-11 09:57:51 +0100386 struct fwnode_handle *fwnode = fwspec->fwnode;
Grant Likelya18dc812012-01-26 12:12:14 -0700387 int rc;
Grant Likelycc79ca62012-02-16 01:37:49 -0700388
389 /* We might want to match the legacy controller last since
390 * it might potentially be set to match all interrupts in
391 * the absence of a device node. This isn't a problem so far
392 * yet though...
Marc Zyngierad3aedf2015-07-28 14:46:08 +0100393 *
394 * bus_token == DOMAIN_BUS_ANY matches any domain, any other
395 * values must generate an exact match for the domain to be
396 * selected.
Grant Likelycc79ca62012-02-16 01:37:49 -0700397 */
398 mutex_lock(&irq_domain_mutex);
Grant Likelya18dc812012-01-26 12:12:14 -0700399 list_for_each_entry(h, &irq_domain_list, link) {
Marc Zyngier651e8b52016-04-11 09:57:51 +0100400 if (h->ops->select && fwspec->param_count)
401 rc = h->ops->select(h, fwspec, bus_token);
402 else if (h->ops->match)
Marc Zyngier130b8c62015-10-13 12:51:31 +0100403 rc = h->ops->match(h, to_of_node(fwnode), bus_token);
Grant Likelya18dc812012-01-26 12:12:14 -0700404 else
Marc Zyngier130b8c62015-10-13 12:51:31 +0100405 rc = ((fwnode != NULL) && (h->fwnode == fwnode) &&
Marc Zyngierad3aedf2015-07-28 14:46:08 +0100406 ((bus_token == DOMAIN_BUS_ANY) ||
407 (h->bus_token == bus_token)));
Grant Likelya18dc812012-01-26 12:12:14 -0700408
409 if (rc) {
Grant Likelycc79ca62012-02-16 01:37:49 -0700410 found = h;
411 break;
412 }
Grant Likelya18dc812012-01-26 12:12:14 -0700413 }
Grant Likelycc79ca62012-02-16 01:37:49 -0700414 mutex_unlock(&irq_domain_mutex);
415 return found;
416}
Marc Zyngier651e8b52016-04-11 09:57:51 +0100417EXPORT_SYMBOL_GPL(irq_find_matching_fwspec);
Grant Likelycc79ca62012-02-16 01:37:49 -0700418
419/**
Eric Augerc7b41f02017-01-19 20:57:59 +0000420 * irq_domain_check_msi_remap - Check whether all MSI irq domains implement
421 * IRQ remapping
422 *
423 * Return: false if any MSI irq domain does not support IRQ remapping,
424 * true otherwise (including if there is no MSI irq domain)
425 */
426bool irq_domain_check_msi_remap(void)
427{
428 struct irq_domain *h;
429 bool ret = true;
430
431 mutex_lock(&irq_domain_mutex);
432 list_for_each_entry(h, &irq_domain_list, link) {
433 if (irq_domain_is_msi(h) &&
434 !irq_domain_hierarchical_is_msi_remap(h)) {
435 ret = false;
436 break;
437 }
438 }
439 mutex_unlock(&irq_domain_mutex);
440 return ret;
441}
442EXPORT_SYMBOL_GPL(irq_domain_check_msi_remap);
443
444/**
Grant Likelycc79ca62012-02-16 01:37:49 -0700445 * irq_set_default_host() - Set a "default" irq domain
Grant Likely68700652012-02-14 14:06:53 -0700446 * @domain: default domain pointer
Grant Likelycc79ca62012-02-16 01:37:49 -0700447 *
448 * For convenience, it's possible to set a "default" domain that will be used
449 * whenever NULL is passed to irq_create_mapping(). It makes life easier for
450 * platforms that want to manipulate a few hard coded interrupt numbers that
451 * aren't properly represented in the device-tree.
452 */
Grant Likely68700652012-02-14 14:06:53 -0700453void irq_set_default_host(struct irq_domain *domain)
Grant Likelycc79ca62012-02-16 01:37:49 -0700454{
Paul Mundt54a90582012-05-19 15:11:47 +0900455 pr_debug("Default domain set to @0x%p\n", domain);
Grant Likelycc79ca62012-02-16 01:37:49 -0700456
Grant Likely68700652012-02-14 14:06:53 -0700457 irq_default_domain = domain;
Grant Likelycc79ca62012-02-16 01:37:49 -0700458}
Paul Mundtecd84eb2012-05-19 15:11:42 +0900459EXPORT_SYMBOL_GPL(irq_set_default_host);
Grant Likelycc79ca62012-02-16 01:37:49 -0700460
Marc Zyngier9f199dd2019-02-20 08:59:23 +0000461/**
462 * irq_get_default_host() - Retrieve the "default" irq domain
463 *
464 * Returns: the default domain, if any.
465 *
466 * Modern code should never use this. This should only be used on
467 * systems that cannot implement a firmware->fwnode mapping (which
468 * both DT and ACPI provide).
469 */
470struct irq_domain *irq_get_default_host(void)
471{
472 return irq_default_domain;
473}
474
David Daneyb526adf2017-08-17 17:53:32 -0700475static void irq_domain_clear_mapping(struct irq_domain *domain,
476 irq_hw_number_t hwirq)
477{
478 if (hwirq < domain->revmap_size) {
479 domain->linear_revmap[hwirq] = 0;
480 } else {
Masahiro Yamadaf1d78352017-10-05 10:44:54 +0900481 mutex_lock(&domain->revmap_tree_mutex);
David Daneyb526adf2017-08-17 17:53:32 -0700482 radix_tree_delete(&domain->revmap_tree, hwirq);
Masahiro Yamadaf1d78352017-10-05 10:44:54 +0900483 mutex_unlock(&domain->revmap_tree_mutex);
David Daneyb526adf2017-08-17 17:53:32 -0700484 }
485}
486
487static void irq_domain_set_mapping(struct irq_domain *domain,
488 irq_hw_number_t hwirq,
489 struct irq_data *irq_data)
490{
491 if (hwirq < domain->revmap_size) {
492 domain->linear_revmap[hwirq] = irq_data->irq;
493 } else {
Masahiro Yamadaf1d78352017-10-05 10:44:54 +0900494 mutex_lock(&domain->revmap_tree_mutex);
David Daneyb526adf2017-08-17 17:53:32 -0700495 radix_tree_insert(&domain->revmap_tree, hwirq, irq_data);
Masahiro Yamadaf1d78352017-10-05 10:44:54 +0900496 mutex_unlock(&domain->revmap_tree_mutex);
David Daneyb526adf2017-08-17 17:53:32 -0700497 }
498}
499
Jiang Liu43a77592014-06-09 16:20:05 +0800500void irq_domain_disassociate(struct irq_domain *domain, unsigned int irq)
Grant Likely913af202012-06-03 22:04:35 -0700501{
Grant Likelyddaf1442013-06-10 01:06:02 +0100502 struct irq_data *irq_data = irq_get_irq_data(irq);
503 irq_hw_number_t hwirq;
Grant Likely913af202012-06-03 22:04:35 -0700504
Grant Likelyddaf1442013-06-10 01:06:02 +0100505 if (WARN(!irq_data || irq_data->domain != domain,
506 "virq%i doesn't exist; cannot disassociate\n", irq))
507 return;
Grant Likely913af202012-06-03 22:04:35 -0700508
Grant Likelyddaf1442013-06-10 01:06:02 +0100509 hwirq = irq_data->hwirq;
510 irq_set_status_flags(irq, IRQ_NOREQUEST);
Grant Likely913af202012-06-03 22:04:35 -0700511
Grant Likelyddaf1442013-06-10 01:06:02 +0100512 /* remove chip and handler */
513 irq_set_chip_and_handler(irq, NULL, NULL);
Grant Likely913af202012-06-03 22:04:35 -0700514
Grant Likelyddaf1442013-06-10 01:06:02 +0100515 /* Make sure it's completed */
516 synchronize_irq(irq);
Grant Likely913af202012-06-03 22:04:35 -0700517
Grant Likelyddaf1442013-06-10 01:06:02 +0100518 /* Tell the PIC about it */
519 if (domain->ops->unmap)
520 domain->ops->unmap(domain, irq);
521 smp_mb();
Grant Likely913af202012-06-03 22:04:35 -0700522
Grant Likelyddaf1442013-06-10 01:06:02 +0100523 irq_data->domain = NULL;
524 irq_data->hwirq = 0;
Thomas Gleixner9dc6be32017-06-20 01:37:16 +0200525 domain->mapcount--;
Grant Likely913af202012-06-03 22:04:35 -0700526
Grant Likelyddaf1442013-06-10 01:06:02 +0100527 /* Clear reverse map for this hwirq */
David Daneyb526adf2017-08-17 17:53:32 -0700528 irq_domain_clear_mapping(domain, hwirq);
Grant Likely913af202012-06-03 22:04:35 -0700529}
530
Grant Likelyddaf1442013-06-10 01:06:02 +0100531int irq_domain_associate(struct irq_domain *domain, unsigned int virq,
532 irq_hw_number_t hwirq)
Grant Likelycc79ca62012-02-16 01:37:49 -0700533{
Grant Likelyddaf1442013-06-10 01:06:02 +0100534 struct irq_data *irq_data = irq_get_irq_data(virq);
535 int ret;
536
537 if (WARN(hwirq >= domain->hwirq_max,
538 "error: hwirq 0x%x is too large for %s\n", (int)hwirq, domain->name))
539 return -EINVAL;
540 if (WARN(!irq_data, "error: virq%i is not allocated", virq))
541 return -EINVAL;
542 if (WARN(irq_data->domain, "error: virq%i is already associated", virq))
543 return -EINVAL;
544
545 mutex_lock(&irq_domain_mutex);
546 irq_data->hwirq = hwirq;
547 irq_data->domain = domain;
548 if (domain->ops->map) {
549 ret = domain->ops->map(domain, virq, hwirq);
550 if (ret != 0) {
551 /*
552 * If map() returns -EPERM, this interrupt is protected
553 * by the firmware or some other service and shall not
554 * be mapped. Don't bother telling the user about it.
555 */
556 if (ret != -EPERM) {
557 pr_info("%s didn't like hwirq-0x%lx to VIRQ%i mapping (rc=%d)\n",
558 domain->name, hwirq, virq, ret);
559 }
560 irq_data->domain = NULL;
561 irq_data->hwirq = 0;
562 mutex_unlock(&irq_domain_mutex);
563 return ret;
564 }
565
566 /* If not already assigned, give the domain the chip's name */
567 if (!domain->name && irq_data->chip)
568 domain->name = irq_data->chip->name;
569 }
570
Thomas Gleixner9dc6be32017-06-20 01:37:16 +0200571 domain->mapcount++;
David Daneyb526adf2017-08-17 17:53:32 -0700572 irq_domain_set_mapping(domain, hwirq, irq_data);
Grant Likelyddaf1442013-06-10 01:06:02 +0100573 mutex_unlock(&irq_domain_mutex);
574
575 irq_clear_status_flags(virq, IRQ_NOREQUEST);
576
577 return 0;
578}
579EXPORT_SYMBOL_GPL(irq_domain_associate);
580
581void irq_domain_associate_many(struct irq_domain *domain, unsigned int irq_base,
582 irq_hw_number_t hwirq_base, int count)
583{
Marc Zyngier5d4c9bc2015-10-13 12:51:29 +0100584 struct device_node *of_node;
Grant Likelyddaf1442013-06-10 01:06:02 +0100585 int i;
Grant Likelycc79ca62012-02-16 01:37:49 -0700586
Marc Zyngier5d4c9bc2015-10-13 12:51:29 +0100587 of_node = irq_domain_get_of_node(domain);
Grant Likely98aa4682012-06-17 16:17:04 -0600588 pr_debug("%s(%s, irqbase=%i, hwbase=%i, count=%i)\n", __func__,
Marc Zyngier5d4c9bc2015-10-13 12:51:29 +0100589 of_node_full_name(of_node), irq_base, (int)hwirq_base, count);
Grant Likely98aa4682012-06-17 16:17:04 -0600590
591 for (i = 0; i < count; i++) {
Grant Likelyddaf1442013-06-10 01:06:02 +0100592 irq_domain_associate(domain, irq_base + i, hwirq_base + i);
Grant Likelycc79ca62012-02-16 01:37:49 -0700593 }
Grant Likelycc79ca62012-02-16 01:37:49 -0700594}
Grant Likely98aa4682012-06-17 16:17:04 -0600595EXPORT_SYMBOL_GPL(irq_domain_associate_many);
Grant Likelycc79ca62012-02-16 01:37:49 -0700596
597/**
598 * irq_create_direct_mapping() - Allocate an irq for direct mapping
Grant Likely68700652012-02-14 14:06:53 -0700599 * @domain: domain to allocate the irq for or NULL for default domain
Grant Likelycc79ca62012-02-16 01:37:49 -0700600 *
601 * This routine is used for irq controllers which can choose the hardware
602 * interrupt numbers they generate. In such a case it's simplest to use
Grant Likely1aa0dd92013-06-08 12:03:59 +0100603 * the linux irq as the hardware interrupt number. It still uses the linear
604 * or radix tree to store the mapping, but the irq controller can optimize
605 * the revmap path by using the hwirq directly.
Grant Likelycc79ca62012-02-16 01:37:49 -0700606 */
Grant Likely68700652012-02-14 14:06:53 -0700607unsigned int irq_create_direct_mapping(struct irq_domain *domain)
Grant Likelycc79ca62012-02-16 01:37:49 -0700608{
Marc Zyngier5d4c9bc2015-10-13 12:51:29 +0100609 struct device_node *of_node;
Grant Likelycc79ca62012-02-16 01:37:49 -0700610 unsigned int virq;
611
Grant Likely68700652012-02-14 14:06:53 -0700612 if (domain == NULL)
613 domain = irq_default_domain;
Grant Likelycc79ca62012-02-16 01:37:49 -0700614
Marc Zyngier5d4c9bc2015-10-13 12:51:29 +0100615 of_node = irq_domain_get_of_node(domain);
616 virq = irq_alloc_desc_from(1, of_node_to_nid(of_node));
Grant Likely03848372012-02-14 14:06:52 -0700617 if (!virq) {
Paul Mundt54a90582012-05-19 15:11:47 +0900618 pr_debug("create_direct virq allocation failed\n");
Grant Likely03848372012-02-14 14:06:52 -0700619 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700620 }
Grant Likely1aa0dd92013-06-08 12:03:59 +0100621 if (virq >= domain->revmap_direct_max_irq) {
Grant Likelycc79ca62012-02-16 01:37:49 -0700622 pr_err("ERROR: no free irqs available below %i maximum\n",
Grant Likely1aa0dd92013-06-08 12:03:59 +0100623 domain->revmap_direct_max_irq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700624 irq_free_desc(virq);
625 return 0;
626 }
Paul Mundt54a90582012-05-19 15:11:47 +0900627 pr_debug("create_direct obtained virq %d\n", virq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700628
Grant Likely98aa4682012-06-17 16:17:04 -0600629 if (irq_domain_associate(domain, virq, virq)) {
Grant Likelycc79ca62012-02-16 01:37:49 -0700630 irq_free_desc(virq);
Grant Likely03848372012-02-14 14:06:52 -0700631 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700632 }
633
634 return virq;
635}
Paul Mundtecd84eb2012-05-19 15:11:42 +0900636EXPORT_SYMBOL_GPL(irq_create_direct_mapping);
Grant Likelycc79ca62012-02-16 01:37:49 -0700637
638/**
639 * irq_create_mapping() - Map a hardware interrupt into linux irq space
Grant Likely68700652012-02-14 14:06:53 -0700640 * @domain: domain owning this hardware interrupt or NULL for default domain
641 * @hwirq: hardware irq number in that domain space
Grant Likelycc79ca62012-02-16 01:37:49 -0700642 *
643 * Only one mapping per hardware interrupt is permitted. Returns a linux
644 * irq number.
645 * If the sense/trigger is to be specified, set_irq_type() should be called
646 * on the number returned from that call.
647 */
Grant Likely68700652012-02-14 14:06:53 -0700648unsigned int irq_create_mapping(struct irq_domain *domain,
Grant Likelycc79ca62012-02-16 01:37:49 -0700649 irq_hw_number_t hwirq)
650{
Marc Zyngier5d4c9bc2015-10-13 12:51:29 +0100651 struct device_node *of_node;
David Daney5b7526e2012-04-05 16:52:13 -0700652 int virq;
Grant Likelycc79ca62012-02-16 01:37:49 -0700653
Paul Mundt54a90582012-05-19 15:11:47 +0900654 pr_debug("irq_create_mapping(0x%p, 0x%lx)\n", domain, hwirq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700655
Grant Likely68700652012-02-14 14:06:53 -0700656 /* Look for default domain if nececssary */
657 if (domain == NULL)
658 domain = irq_default_domain;
659 if (domain == NULL) {
Kefeng Wang798f0fd2013-06-06 19:20:27 +0800660 WARN(1, "%s(, %lx) called with NULL domain\n", __func__, hwirq);
Grant Likely03848372012-02-14 14:06:52 -0700661 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700662 }
Paul Mundt54a90582012-05-19 15:11:47 +0900663 pr_debug("-> using domain @%p\n", domain);
Grant Likelycc79ca62012-02-16 01:37:49 -0700664
Marc Zyngier5d4c9bc2015-10-13 12:51:29 +0100665 of_node = irq_domain_get_of_node(domain);
666
Grant Likelycc79ca62012-02-16 01:37:49 -0700667 /* Check if mapping already exists */
Grant Likely68700652012-02-14 14:06:53 -0700668 virq = irq_find_mapping(domain, hwirq);
Grant Likely03848372012-02-14 14:06:52 -0700669 if (virq) {
Paul Mundt54a90582012-05-19 15:11:47 +0900670 pr_debug("-> existing mapping on virq %d\n", virq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700671 return virq;
672 }
673
Grant Likely1bc04f22012-02-14 14:06:55 -0700674 /* Allocate a virtual interrupt number */
Thomas Gleixner06ee6d52016-07-04 17:39:24 +0900675 virq = irq_domain_alloc_descs(-1, 1, hwirq, of_node_to_nid(of_node), NULL);
David Daney5b7526e2012-04-05 16:52:13 -0700676 if (virq <= 0) {
Paul Mundt54a90582012-05-19 15:11:47 +0900677 pr_debug("-> virq allocation failed\n");
Grant Likely1bc04f22012-02-14 14:06:55 -0700678 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700679 }
680
Grant Likely98aa4682012-06-17 16:17:04 -0600681 if (irq_domain_associate(domain, virq, hwirq)) {
Grant Likely73255702012-06-03 22:04:35 -0700682 irq_free_desc(virq);
Grant Likely03848372012-02-14 14:06:52 -0700683 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700684 }
685
Paul Mundt54a90582012-05-19 15:11:47 +0900686 pr_debug("irq %lu on domain %s mapped to virtual irq %u\n",
Marc Zyngier5d4c9bc2015-10-13 12:51:29 +0100687 hwirq, of_node_full_name(of_node), virq);
Grant Likelycc79ca62012-02-16 01:37:49 -0700688
689 return virq;
690}
691EXPORT_SYMBOL_GPL(irq_create_mapping);
692
Grant Likely98aa4682012-06-17 16:17:04 -0600693/**
694 * irq_create_strict_mappings() - Map a range of hw irqs to fixed linux irqs
695 * @domain: domain owning the interrupt range
696 * @irq_base: beginning of linux IRQ range
697 * @hwirq_base: beginning of hardware IRQ range
698 * @count: Number of interrupts to map
699 *
700 * This routine is used for allocating and mapping a range of hardware
701 * irqs to linux irqs where the linux irq numbers are at pre-defined
702 * locations. For use by controllers that already have static mappings
703 * to insert in to the domain.
704 *
705 * Non-linear users can use irq_create_identity_mapping() for IRQ-at-a-time
706 * domain insertion.
707 *
708 * 0 is returned upon success, while any failure to establish a static
709 * mapping is treated as an error.
710 */
711int irq_create_strict_mappings(struct irq_domain *domain, unsigned int irq_base,
712 irq_hw_number_t hwirq_base, int count)
713{
Marc Zyngier5d4c9bc2015-10-13 12:51:29 +0100714 struct device_node *of_node;
Grant Likely98aa4682012-06-17 16:17:04 -0600715 int ret;
716
Marc Zyngier5d4c9bc2015-10-13 12:51:29 +0100717 of_node = irq_domain_get_of_node(domain);
Grant Likely98aa4682012-06-17 16:17:04 -0600718 ret = irq_alloc_descs(irq_base, irq_base, count,
Marc Zyngier5d4c9bc2015-10-13 12:51:29 +0100719 of_node_to_nid(of_node));
Grant Likely98aa4682012-06-17 16:17:04 -0600720 if (unlikely(ret < 0))
721 return ret;
722
Grant Likelyddaf1442013-06-10 01:06:02 +0100723 irq_domain_associate_many(domain, irq_base, hwirq_base, count);
Grant Likely98aa4682012-06-17 16:17:04 -0600724 return 0;
725}
726EXPORT_SYMBOL_GPL(irq_create_strict_mappings);
727
Marc Zyngier11e4438e2015-10-13 12:51:32 +0100728static int irq_domain_translate(struct irq_domain *d,
729 struct irq_fwspec *fwspec,
730 irq_hw_number_t *hwirq, unsigned int *type)
731{
732#ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
733 if (d->ops->translate)
734 return d->ops->translate(d, fwspec, hwirq, type);
735#endif
736 if (d->ops->xlate)
737 return d->ops->xlate(d, to_of_node(fwspec->fwnode),
738 fwspec->param, fwspec->param_count,
739 hwirq, type);
740
741 /* If domain has no translation, then we assume interrupt line */
742 *hwirq = fwspec->param[0];
743 return 0;
744}
745
Brian Masneyb5c231d2019-02-07 21:16:22 -0500746static void of_phandle_args_to_fwspec(struct device_node *np, const u32 *args,
747 unsigned int count,
Marc Zyngier11e4438e2015-10-13 12:51:32 +0100748 struct irq_fwspec *fwspec)
749{
750 int i;
751
Brian Masneyb5c231d2019-02-07 21:16:22 -0500752 fwspec->fwnode = np ? &np->fwnode : NULL;
753 fwspec->param_count = count;
Marc Zyngier11e4438e2015-10-13 12:51:32 +0100754
Brian Masneyb5c231d2019-02-07 21:16:22 -0500755 for (i = 0; i < count; i++)
756 fwspec->param[i] = args[i];
Marc Zyngier11e4438e2015-10-13 12:51:32 +0100757}
758
Marc Zyngierc0131f02015-10-13 12:51:34 +0100759unsigned int irq_create_fwspec_mapping(struct irq_fwspec *fwspec)
Grant Likelycc79ca62012-02-16 01:37:49 -0700760{
Grant Likely68700652012-02-14 14:06:53 -0700761 struct irq_domain *domain;
Jon Hunter1e2a7d72016-06-07 16:12:28 +0100762 struct irq_data *irq_data;
Grant Likelycc79ca62012-02-16 01:37:49 -0700763 irq_hw_number_t hwirq;
764 unsigned int type = IRQ_TYPE_NONE;
Jiang Liuf8264e32014-11-06 22:20:14 +0800765 int virq;
Grant Likelycc79ca62012-02-16 01:37:49 -0700766
Marc Zyngier530cbe12016-01-26 13:52:25 +0000767 if (fwspec->fwnode) {
Marc Zyngier651e8b52016-04-11 09:57:51 +0100768 domain = irq_find_matching_fwspec(fwspec, DOMAIN_BUS_WIRED);
Marc Zyngier530cbe12016-01-26 13:52:25 +0000769 if (!domain)
Marc Zyngier651e8b52016-04-11 09:57:51 +0100770 domain = irq_find_matching_fwspec(fwspec, DOMAIN_BUS_ANY);
Marc Zyngier530cbe12016-01-26 13:52:25 +0000771 } else {
Marc Zyngier11e4438e2015-10-13 12:51:32 +0100772 domain = irq_default_domain;
Marc Zyngier530cbe12016-01-26 13:52:25 +0000773 }
Marc Zyngier11e4438e2015-10-13 12:51:32 +0100774
Grant Likely68700652012-02-14 14:06:53 -0700775 if (!domain) {
Kefeng Wang798f0fd2013-06-06 19:20:27 +0800776 pr_warn("no irq domain found for %s !\n",
Marc Zyngierc0131f02015-10-13 12:51:34 +0100777 of_node_full_name(to_of_node(fwspec->fwnode)));
Grant Likely03848372012-02-14 14:06:52 -0700778 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700779 }
780
Marc Zyngierc0131f02015-10-13 12:51:34 +0100781 if (irq_domain_translate(domain, fwspec, &hwirq, &type))
Marc Zyngier11e4438e2015-10-13 12:51:32 +0100782 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700783
Jon Hunterb62b2cf2016-06-07 16:12:26 +0100784 /*
785 * WARN if the irqchip returns a type with bits
786 * outside the sense mask set and clear these bits.
787 */
788 if (WARN_ON(type & ~IRQ_TYPE_SENSE_MASK))
789 type &= IRQ_TYPE_SENSE_MASK;
790
791 /*
792 * If we've already configured this interrupt,
793 * don't do it again, or hell will break loose.
794 */
795 virq = irq_find_mapping(domain, hwirq);
796 if (virq) {
Yingjoe Chen0cc01ab2014-11-06 22:20:15 +0800797 /*
Jon Hunterb62b2cf2016-06-07 16:12:26 +0100798 * If the trigger type is not specified or matches the
799 * current trigger type then we are done so return the
800 * interrupt number.
Yingjoe Chen0cc01ab2014-11-06 22:20:15 +0800801 */
Jon Hunterb62b2cf2016-06-07 16:12:26 +0100802 if (type == IRQ_TYPE_NONE || type == irq_get_trigger_type(virq))
Yingjoe Chen0cc01ab2014-11-06 22:20:15 +0800803 return virq;
804
Jon Hunterb62b2cf2016-06-07 16:12:26 +0100805 /*
806 * If the trigger type has not been set yet, then set
807 * it now and return the interrupt number.
808 */
809 if (irq_get_trigger_type(virq) == IRQ_TYPE_NONE) {
Jon Hunter1e2a7d72016-06-07 16:12:28 +0100810 irq_data = irq_get_irq_data(virq);
811 if (!irq_data)
812 return 0;
813
814 irqd_set_trigger_type(irq_data, type);
Jon Hunterb62b2cf2016-06-07 16:12:26 +0100815 return virq;
816 }
817
818 pr_warn("type mismatch, failed to map hwirq-%lu for %s!\n",
819 hwirq, of_node_full_name(to_of_node(fwspec->fwnode)));
820 return 0;
821 }
822
823 if (irq_domain_is_hierarchy(domain)) {
Marc Zyngierc0131f02015-10-13 12:51:34 +0100824 virq = irq_domain_alloc_irqs(domain, 1, NUMA_NO_NODE, fwspec);
Yingjoe Chen0cc01ab2014-11-06 22:20:15 +0800825 if (virq <= 0)
826 return 0;
827 } else {
828 /* Create mapping */
829 virq = irq_create_mapping(domain, hwirq);
830 if (!virq)
831 return virq;
832 }
Grant Likelycc79ca62012-02-16 01:37:49 -0700833
Jon Hunter1e2a7d72016-06-07 16:12:28 +0100834 irq_data = irq_get_irq_data(virq);
835 if (!irq_data) {
836 if (irq_domain_is_hierarchy(domain))
837 irq_domain_free_irqs(virq, 1);
838 else
839 irq_dispose_mapping(virq);
840 return 0;
841 }
842
843 /* Store trigger type */
844 irqd_set_trigger_type(irq_data, type);
845
Grant Likelycc79ca62012-02-16 01:37:49 -0700846 return virq;
847}
Marc Zyngierc0131f02015-10-13 12:51:34 +0100848EXPORT_SYMBOL_GPL(irq_create_fwspec_mapping);
849
850unsigned int irq_create_of_mapping(struct of_phandle_args *irq_data)
851{
852 struct irq_fwspec fwspec;
853
Brian Masneyb5c231d2019-02-07 21:16:22 -0500854 of_phandle_args_to_fwspec(irq_data->np, irq_data->args,
855 irq_data->args_count, &fwspec);
856
Marc Zyngierc0131f02015-10-13 12:51:34 +0100857 return irq_create_fwspec_mapping(&fwspec);
858}
Grant Likelycc79ca62012-02-16 01:37:49 -0700859EXPORT_SYMBOL_GPL(irq_create_of_mapping);
860
861/**
862 * irq_dispose_mapping() - Unmap an interrupt
863 * @virq: linux irq number of the interrupt to unmap
864 */
865void irq_dispose_mapping(unsigned int virq)
866{
867 struct irq_data *irq_data = irq_get_irq_data(virq);
Grant Likely68700652012-02-14 14:06:53 -0700868 struct irq_domain *domain;
Grant Likelycc79ca62012-02-16 01:37:49 -0700869
Grant Likely03848372012-02-14 14:06:52 -0700870 if (!virq || !irq_data)
Grant Likelycc79ca62012-02-16 01:37:49 -0700871 return;
872
Grant Likely68700652012-02-14 14:06:53 -0700873 domain = irq_data->domain;
874 if (WARN_ON(domain == NULL))
Grant Likelycc79ca62012-02-16 01:37:49 -0700875 return;
876
Jon Hunterd16dcd3d2016-06-21 10:23:22 +0100877 if (irq_domain_is_hierarchy(domain)) {
878 irq_domain_free_irqs(virq, 1);
879 } else {
880 irq_domain_disassociate(domain, virq);
881 irq_free_desc(virq);
882 }
Grant Likelycc79ca62012-02-16 01:37:49 -0700883}
884EXPORT_SYMBOL_GPL(irq_dispose_mapping);
885
886/**
Geert Uytterhoevenb8d62f32018-10-08 13:17:26 +0200887 * irq_find_mapping() - Find a linux irq from a hw irq number.
Grant Likely68700652012-02-14 14:06:53 -0700888 * @domain: domain owning this hardware interrupt
889 * @hwirq: hardware irq number in that domain space
Grant Likelycc79ca62012-02-16 01:37:49 -0700890 */
Grant Likely68700652012-02-14 14:06:53 -0700891unsigned int irq_find_mapping(struct irq_domain *domain,
Grant Likelycc79ca62012-02-16 01:37:49 -0700892 irq_hw_number_t hwirq)
893{
Grant Likely4c0946c2012-06-03 22:04:39 -0700894 struct irq_data *data;
Grant Likelycc79ca62012-02-16 01:37:49 -0700895
Grant Likely68700652012-02-14 14:06:53 -0700896 /* Look for default domain if nececssary */
897 if (domain == NULL)
898 domain = irq_default_domain;
899 if (domain == NULL)
Grant Likely03848372012-02-14 14:06:52 -0700900 return 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700901
Grant Likely1aa0dd92013-06-08 12:03:59 +0100902 if (hwirq < domain->revmap_direct_max_irq) {
Jiang Liuf8264e32014-11-06 22:20:14 +0800903 data = irq_domain_get_irq_data(domain, hwirq);
904 if (data && data->hwirq == hwirq)
Grant Likely4c0946c2012-06-03 22:04:39 -0700905 return hwirq;
Grant Likely4c0946c2012-06-03 22:04:39 -0700906 }
907
Grant Likelyd3dcb432013-06-10 12:19:17 +0100908 /* Check if the hwirq is in the linear revmap. */
909 if (hwirq < domain->revmap_size)
910 return domain->linear_revmap[hwirq];
911
912 rcu_read_lock();
913 data = radix_tree_lookup(&domain->revmap_tree, hwirq);
914 rcu_read_unlock();
915 return data ? data->irq : 0;
Grant Likelycc79ca62012-02-16 01:37:49 -0700916}
917EXPORT_SYMBOL_GPL(irq_find_mapping);
918
Grant Likely16b2e6e2012-01-26 11:26:52 -0700919/**
920 * irq_domain_xlate_onecell() - Generic xlate for direct one cell bindings
921 *
922 * Device Tree IRQ specifier translation function which works with one cell
923 * bindings where the cell value maps directly to the hwirq number.
924 */
925int irq_domain_xlate_onecell(struct irq_domain *d, struct device_node *ctrlr,
926 const u32 *intspec, unsigned int intsize,
927 unsigned long *out_hwirq, unsigned int *out_type)
Grant Likely7e713302011-07-26 03:19:06 -0600928{
Grant Likely16b2e6e2012-01-26 11:26:52 -0700929 if (WARN_ON(intsize < 1))
Grant Likely7e713302011-07-26 03:19:06 -0600930 return -EINVAL;
Grant Likely7e713302011-07-26 03:19:06 -0600931 *out_hwirq = intspec[0];
932 *out_type = IRQ_TYPE_NONE;
Grant Likely7e713302011-07-26 03:19:06 -0600933 return 0;
934}
Grant Likely16b2e6e2012-01-26 11:26:52 -0700935EXPORT_SYMBOL_GPL(irq_domain_xlate_onecell);
936
937/**
938 * irq_domain_xlate_twocell() - Generic xlate for direct two cell bindings
939 *
940 * Device Tree IRQ specifier translation function which works with two cell
941 * bindings where the cell values map directly to the hwirq number
942 * and linux irq flags.
943 */
944int irq_domain_xlate_twocell(struct irq_domain *d, struct device_node *ctrlr,
945 const u32 *intspec, unsigned int intsize,
946 irq_hw_number_t *out_hwirq, unsigned int *out_type)
947{
Brian Masneyb5c231d2019-02-07 21:16:22 -0500948 struct irq_fwspec fwspec;
949
950 of_phandle_args_to_fwspec(ctrlr, intspec, intsize, &fwspec);
951 return irq_domain_translate_twocell(d, &fwspec, out_hwirq, out_type);
Grant Likely16b2e6e2012-01-26 11:26:52 -0700952}
953EXPORT_SYMBOL_GPL(irq_domain_xlate_twocell);
954
955/**
956 * irq_domain_xlate_onetwocell() - Generic xlate for one or two cell bindings
957 *
958 * Device Tree IRQ specifier translation function which works with either one
959 * or two cell bindings where the cell values map directly to the hwirq number
960 * and linux irq flags.
961 *
962 * Note: don't use this function unless your interrupt controller explicitly
963 * supports both one and two cell bindings. For the majority of controllers
964 * the _onecell() or _twocell() variants above should be used.
965 */
966int irq_domain_xlate_onetwocell(struct irq_domain *d,
967 struct device_node *ctrlr,
968 const u32 *intspec, unsigned int intsize,
969 unsigned long *out_hwirq, unsigned int *out_type)
970{
971 if (WARN_ON(intsize < 1))
972 return -EINVAL;
973 *out_hwirq = intspec[0];
Sebastian Frias0c228912016-08-02 10:52:45 +0200974 if (intsize > 1)
975 *out_type = intspec[1] & IRQ_TYPE_SENSE_MASK;
976 else
977 *out_type = IRQ_TYPE_NONE;
Grant Likely16b2e6e2012-01-26 11:26:52 -0700978 return 0;
979}
980EXPORT_SYMBOL_GPL(irq_domain_xlate_onetwocell);
Grant Likely7e713302011-07-26 03:19:06 -0600981
Grant Likelya18dc812012-01-26 12:12:14 -0700982const struct irq_domain_ops irq_domain_simple_ops = {
Grant Likely16b2e6e2012-01-26 11:26:52 -0700983 .xlate = irq_domain_xlate_onetwocell,
Grant Likely75294952012-02-14 14:06:57 -0700984};
985EXPORT_SYMBOL_GPL(irq_domain_simple_ops);
Jiang Liuf8264e32014-11-06 22:20:14 +0800986
Brian Masneyb5c231d2019-02-07 21:16:22 -0500987/**
988 * irq_domain_translate_twocell() - Generic translate for direct two cell
989 * bindings
990 *
991 * Device Tree IRQ specifier translation function which works with two cell
992 * bindings where the cell values map directly to the hwirq number
993 * and linux irq flags.
994 */
995int irq_domain_translate_twocell(struct irq_domain *d,
996 struct irq_fwspec *fwspec,
997 unsigned long *out_hwirq,
998 unsigned int *out_type)
999{
1000 if (WARN_ON(fwspec->param_count < 2))
1001 return -EINVAL;
1002 *out_hwirq = fwspec->param[0];
1003 *out_type = fwspec->param[1] & IRQ_TYPE_SENSE_MASK;
1004 return 0;
1005}
1006EXPORT_SYMBOL_GPL(irq_domain_translate_twocell);
1007
Qais Yousefac0a0cd2015-12-08 13:20:18 +00001008int irq_domain_alloc_descs(int virq, unsigned int cnt, irq_hw_number_t hwirq,
Dou Liyangbec04032018-12-04 23:51:20 +08001009 int node, const struct irq_affinity_desc *affinity)
Jiang Liuf8264e32014-11-06 22:20:14 +08001010{
1011 unsigned int hint;
1012
1013 if (virq >= 0) {
Thomas Gleixner06ee6d52016-07-04 17:39:24 +09001014 virq = __irq_alloc_descs(virq, virq, cnt, node, THIS_MODULE,
1015 affinity);
Jiang Liuf8264e32014-11-06 22:20:14 +08001016 } else {
1017 hint = hwirq % nr_irqs;
1018 if (hint == 0)
1019 hint++;
Thomas Gleixner06ee6d52016-07-04 17:39:24 +09001020 virq = __irq_alloc_descs(-1, hint, cnt, node, THIS_MODULE,
1021 affinity);
1022 if (virq <= 0 && hint > 1) {
1023 virq = __irq_alloc_descs(-1, 1, cnt, node, THIS_MODULE,
1024 affinity);
1025 }
Jiang Liuf8264e32014-11-06 22:20:14 +08001026 }
1027
1028 return virq;
1029}
1030
1031#ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
Jiang Liuafb7da82014-11-15 22:24:02 +08001032/**
Marc Zyngier2a5e9a02015-10-13 12:51:43 +01001033 * irq_domain_create_hierarchy - Add a irqdomain into the hierarchy
Jiang Liuafb7da82014-11-15 22:24:02 +08001034 * @parent: Parent irq domain to associate with the new domain
1035 * @flags: Irq domain flags associated to the domain
1036 * @size: Size of the domain. See below
Marc Zyngier2a5e9a02015-10-13 12:51:43 +01001037 * @fwnode: Optional fwnode of the interrupt controller
Jiang Liuafb7da82014-11-15 22:24:02 +08001038 * @ops: Pointer to the interrupt domain callbacks
1039 * @host_data: Controller private data pointer
1040 *
1041 * If @size is 0 a tree domain is created, otherwise a linear domain.
1042 *
1043 * If successful the parent is associated to the new domain and the
1044 * domain flags are set.
1045 * Returns pointer to IRQ domain, or NULL on failure.
1046 */
Marc Zyngier2a5e9a02015-10-13 12:51:43 +01001047struct irq_domain *irq_domain_create_hierarchy(struct irq_domain *parent,
Jiang Liuafb7da82014-11-15 22:24:02 +08001048 unsigned int flags,
1049 unsigned int size,
Marc Zyngier2a5e9a02015-10-13 12:51:43 +01001050 struct fwnode_handle *fwnode,
Jiang Liuafb7da82014-11-15 22:24:02 +08001051 const struct irq_domain_ops *ops,
1052 void *host_data)
1053{
1054 struct irq_domain *domain;
1055
1056 if (size)
Marc Zyngier2a5e9a02015-10-13 12:51:43 +01001057 domain = irq_domain_create_linear(fwnode, size, ops, host_data);
Jiang Liuafb7da82014-11-15 22:24:02 +08001058 else
Marc Zyngier2a5e9a02015-10-13 12:51:43 +01001059 domain = irq_domain_create_tree(fwnode, ops, host_data);
Jiang Liuafb7da82014-11-15 22:24:02 +08001060 if (domain) {
1061 domain->parent = parent;
1062 domain->flags |= flags;
1063 }
1064
1065 return domain;
1066}
Quan Nguyen52b2a052016-03-03 21:56:52 +07001067EXPORT_SYMBOL_GPL(irq_domain_create_hierarchy);
Jiang Liuafb7da82014-11-15 22:24:02 +08001068
Jiang Liuf8264e32014-11-06 22:20:14 +08001069static void irq_domain_insert_irq(int virq)
1070{
1071 struct irq_data *data;
1072
1073 for (data = irq_get_irq_data(virq); data; data = data->parent_data) {
1074 struct irq_domain *domain = data->domain;
Jiang Liuf8264e32014-11-06 22:20:14 +08001075
Thomas Gleixner9dc6be32017-06-20 01:37:16 +02001076 domain->mapcount++;
David Daneyb526adf2017-08-17 17:53:32 -07001077 irq_domain_set_mapping(domain, data->hwirq, data);
Jiang Liuf8264e32014-11-06 22:20:14 +08001078
1079 /* If not already assigned, give the domain the chip's name */
1080 if (!domain->name && data->chip)
1081 domain->name = data->chip->name;
1082 }
1083
1084 irq_clear_status_flags(virq, IRQ_NOREQUEST);
1085}
1086
1087static void irq_domain_remove_irq(int virq)
1088{
1089 struct irq_data *data;
1090
1091 irq_set_status_flags(virq, IRQ_NOREQUEST);
1092 irq_set_chip_and_handler(virq, NULL, NULL);
1093 synchronize_irq(virq);
1094 smp_mb();
1095
1096 for (data = irq_get_irq_data(virq); data; data = data->parent_data) {
1097 struct irq_domain *domain = data->domain;
1098 irq_hw_number_t hwirq = data->hwirq;
1099
Thomas Gleixner9dc6be32017-06-20 01:37:16 +02001100 domain->mapcount--;
David Daneyb526adf2017-08-17 17:53:32 -07001101 irq_domain_clear_mapping(domain, hwirq);
Jiang Liuf8264e32014-11-06 22:20:14 +08001102 }
1103}
1104
1105static struct irq_data *irq_domain_insert_irq_data(struct irq_domain *domain,
1106 struct irq_data *child)
1107{
1108 struct irq_data *irq_data;
1109
Jiang Liu67830112015-06-01 16:05:13 +08001110 irq_data = kzalloc_node(sizeof(*irq_data), GFP_KERNEL,
1111 irq_data_get_node(child));
Jiang Liuf8264e32014-11-06 22:20:14 +08001112 if (irq_data) {
1113 child->parent_data = irq_data;
1114 irq_data->irq = child->irq;
Jiang Liu0d0b4c82015-06-01 16:05:12 +08001115 irq_data->common = child->common;
Jiang Liuf8264e32014-11-06 22:20:14 +08001116 irq_data->domain = domain;
1117 }
1118
1119 return irq_data;
1120}
1121
1122static void irq_domain_free_irq_data(unsigned int virq, unsigned int nr_irqs)
1123{
1124 struct irq_data *irq_data, *tmp;
1125 int i;
1126
1127 for (i = 0; i < nr_irqs; i++) {
1128 irq_data = irq_get_irq_data(virq + i);
1129 tmp = irq_data->parent_data;
1130 irq_data->parent_data = NULL;
1131 irq_data->domain = NULL;
1132
1133 while (tmp) {
1134 irq_data = tmp;
1135 tmp = tmp->parent_data;
1136 kfree(irq_data);
1137 }
1138 }
1139}
1140
1141static int irq_domain_alloc_irq_data(struct irq_domain *domain,
1142 unsigned int virq, unsigned int nr_irqs)
1143{
1144 struct irq_data *irq_data;
1145 struct irq_domain *parent;
1146 int i;
1147
1148 /* The outermost irq_data is embedded in struct irq_desc */
1149 for (i = 0; i < nr_irqs; i++) {
1150 irq_data = irq_get_irq_data(virq + i);
1151 irq_data->domain = domain;
1152
1153 for (parent = domain->parent; parent; parent = parent->parent) {
1154 irq_data = irq_domain_insert_irq_data(parent, irq_data);
1155 if (!irq_data) {
1156 irq_domain_free_irq_data(virq, i + 1);
1157 return -ENOMEM;
1158 }
1159 }
1160 }
1161
1162 return 0;
1163}
1164
1165/**
1166 * irq_domain_get_irq_data - Get irq_data associated with @virq and @domain
1167 * @domain: domain to match
1168 * @virq: IRQ number to get irq_data
1169 */
1170struct irq_data *irq_domain_get_irq_data(struct irq_domain *domain,
1171 unsigned int virq)
1172{
1173 struct irq_data *irq_data;
1174
1175 for (irq_data = irq_get_irq_data(virq); irq_data;
1176 irq_data = irq_data->parent_data)
1177 if (irq_data->domain == domain)
1178 return irq_data;
1179
1180 return NULL;
1181}
Jake Oshinsa4289dc2015-12-10 17:52:59 +00001182EXPORT_SYMBOL_GPL(irq_domain_get_irq_data);
Jiang Liuf8264e32014-11-06 22:20:14 +08001183
1184/**
1185 * irq_domain_set_hwirq_and_chip - Set hwirq and irqchip of @virq at @domain
1186 * @domain: Interrupt domain to match
1187 * @virq: IRQ number
1188 * @hwirq: The hwirq number
1189 * @chip: The associated interrupt chip
1190 * @chip_data: The associated chip data
1191 */
1192int irq_domain_set_hwirq_and_chip(struct irq_domain *domain, unsigned int virq,
1193 irq_hw_number_t hwirq, struct irq_chip *chip,
1194 void *chip_data)
1195{
1196 struct irq_data *irq_data = irq_domain_get_irq_data(domain, virq);
1197
1198 if (!irq_data)
1199 return -ENOENT;
1200
1201 irq_data->hwirq = hwirq;
1202 irq_data->chip = chip ? chip : &no_irq_chip;
1203 irq_data->chip_data = chip_data;
1204
1205 return 0;
1206}
Quan Nguyen52b2a052016-03-03 21:56:52 +07001207EXPORT_SYMBOL_GPL(irq_domain_set_hwirq_and_chip);
Jiang Liuf8264e32014-11-06 22:20:14 +08001208
1209/**
Jiang Liu1b537702014-11-09 23:10:24 +08001210 * irq_domain_set_info - Set the complete data for a @virq in @domain
1211 * @domain: Interrupt domain to match
1212 * @virq: IRQ number
1213 * @hwirq: The hardware interrupt number
1214 * @chip: The associated interrupt chip
1215 * @chip_data: The associated interrupt chip data
1216 * @handler: The interrupt flow handler
1217 * @handler_data: The interrupt flow handler data
1218 * @handler_name: The interrupt handler name
1219 */
1220void irq_domain_set_info(struct irq_domain *domain, unsigned int virq,
1221 irq_hw_number_t hwirq, struct irq_chip *chip,
1222 void *chip_data, irq_flow_handler_t handler,
1223 void *handler_data, const char *handler_name)
1224{
1225 irq_domain_set_hwirq_and_chip(domain, virq, hwirq, chip, chip_data);
1226 __irq_set_handler(virq, handler, 0, handler_name);
1227 irq_set_handler_data(virq, handler_data);
1228}
Keith Busch64bce3e2016-01-12 13:18:07 -07001229EXPORT_SYMBOL(irq_domain_set_info);
Jiang Liu1b537702014-11-09 23:10:24 +08001230
1231/**
Jiang Liuf8264e32014-11-06 22:20:14 +08001232 * irq_domain_reset_irq_data - Clear hwirq, chip and chip_data in @irq_data
1233 * @irq_data: The pointer to irq_data
1234 */
1235void irq_domain_reset_irq_data(struct irq_data *irq_data)
1236{
1237 irq_data->hwirq = 0;
1238 irq_data->chip = &no_irq_chip;
1239 irq_data->chip_data = NULL;
1240}
Quan Nguyen52b2a052016-03-03 21:56:52 +07001241EXPORT_SYMBOL_GPL(irq_domain_reset_irq_data);
Jiang Liuf8264e32014-11-06 22:20:14 +08001242
1243/**
1244 * irq_domain_free_irqs_common - Clear irq_data and free the parent
1245 * @domain: Interrupt domain to match
1246 * @virq: IRQ number to start with
1247 * @nr_irqs: The number of irqs to free
1248 */
1249void irq_domain_free_irqs_common(struct irq_domain *domain, unsigned int virq,
1250 unsigned int nr_irqs)
1251{
1252 struct irq_data *irq_data;
1253 int i;
1254
1255 for (i = 0; i < nr_irqs; i++) {
1256 irq_data = irq_domain_get_irq_data(domain, virq + i);
1257 if (irq_data)
1258 irq_domain_reset_irq_data(irq_data);
1259 }
1260 irq_domain_free_irqs_parent(domain, virq, nr_irqs);
1261}
Axel Lin63cc7872016-03-17 12:00:31 +08001262EXPORT_SYMBOL_GPL(irq_domain_free_irqs_common);
Jiang Liuf8264e32014-11-06 22:20:14 +08001263
1264/**
1265 * irq_domain_free_irqs_top - Clear handler and handler data, clear irqdata and free parent
1266 * @domain: Interrupt domain to match
1267 * @virq: IRQ number to start with
1268 * @nr_irqs: The number of irqs to free
1269 */
1270void irq_domain_free_irqs_top(struct irq_domain *domain, unsigned int virq,
1271 unsigned int nr_irqs)
1272{
1273 int i;
1274
1275 for (i = 0; i < nr_irqs; i++) {
1276 irq_set_handler_data(virq + i, NULL);
1277 irq_set_handler(virq + i, NULL);
1278 }
1279 irq_domain_free_irqs_common(domain, virq, nr_irqs);
1280}
1281
Marc Zyngier6a6544e2017-06-20 22:17:44 +01001282static void irq_domain_free_irqs_hierarchy(struct irq_domain *domain,
Jiang Liu36d72732014-11-15 22:24:01 +08001283 unsigned int irq_base,
1284 unsigned int nr_irqs)
1285{
David Daney0d12ec02017-08-17 17:53:33 -07001286 if (domain->ops->free)
1287 domain->ops->free(domain, irq_base, nr_irqs);
Jiang Liu36d72732014-11-15 22:24:01 +08001288}
1289
Marc Zyngier6a6544e2017-06-20 22:17:44 +01001290int irq_domain_alloc_irqs_hierarchy(struct irq_domain *domain,
Marc Zyngierc4665952015-11-23 08:26:04 +00001291 unsigned int irq_base,
1292 unsigned int nr_irqs, void *arg)
Jiang Liu36d72732014-11-15 22:24:01 +08001293{
Marc Zyngier6a6544e2017-06-20 22:17:44 +01001294 return domain->ops->alloc(domain, irq_base, nr_irqs, arg);
Jiang Liu36d72732014-11-15 22:24:01 +08001295}
1296
Jiang Liuf8264e32014-11-06 22:20:14 +08001297/**
1298 * __irq_domain_alloc_irqs - Allocate IRQs from domain
1299 * @domain: domain to allocate from
Julien Grall08970ec2019-04-18 16:54:01 +01001300 * @irq_base: allocate specified IRQ number if irq_base >= 0
Jiang Liuf8264e32014-11-06 22:20:14 +08001301 * @nr_irqs: number of IRQs to allocate
1302 * @node: NUMA node id for memory allocation
1303 * @arg: domain specific argument
1304 * @realloc: IRQ descriptors have already been allocated if true
Thomas Gleixner06ee6d52016-07-04 17:39:24 +09001305 * @affinity: Optional irq affinity mask for multiqueue devices
Jiang Liuf8264e32014-11-06 22:20:14 +08001306 *
1307 * Allocate IRQ numbers and initialized all data structures to support
1308 * hierarchy IRQ domains.
1309 * Parameter @realloc is mainly to support legacy IRQs.
1310 * Returns error code or allocated IRQ number
1311 *
1312 * The whole process to setup an IRQ has been split into two steps.
1313 * The first step, __irq_domain_alloc_irqs(), is to allocate IRQ
1314 * descriptor and required hardware resources. The second step,
1315 * irq_domain_activate_irq(), is to program hardwares with preallocated
1316 * resources. In this way, it's easier to rollback when failing to
1317 * allocate resources.
1318 */
1319int __irq_domain_alloc_irqs(struct irq_domain *domain, int irq_base,
1320 unsigned int nr_irqs, int node, void *arg,
Dou Liyangbec04032018-12-04 23:51:20 +08001321 bool realloc, const struct irq_affinity_desc *affinity)
Jiang Liuf8264e32014-11-06 22:20:14 +08001322{
1323 int i, ret, virq;
1324
1325 if (domain == NULL) {
1326 domain = irq_default_domain;
1327 if (WARN(!domain, "domain is NULL; cannot allocate IRQ\n"))
1328 return -EINVAL;
1329 }
1330
1331 if (!domain->ops->alloc) {
1332 pr_debug("domain->ops->alloc() is NULL\n");
1333 return -ENOSYS;
1334 }
1335
1336 if (realloc && irq_base >= 0) {
1337 virq = irq_base;
1338 } else {
Thomas Gleixner06ee6d52016-07-04 17:39:24 +09001339 virq = irq_domain_alloc_descs(irq_base, nr_irqs, 0, node,
1340 affinity);
Jiang Liuf8264e32014-11-06 22:20:14 +08001341 if (virq < 0) {
1342 pr_debug("cannot allocate IRQ(base %d, count %d)\n",
1343 irq_base, nr_irqs);
1344 return virq;
1345 }
1346 }
1347
1348 if (irq_domain_alloc_irq_data(domain, virq, nr_irqs)) {
1349 pr_debug("cannot allocate memory for IRQ%d\n", virq);
1350 ret = -ENOMEM;
1351 goto out_free_desc;
1352 }
1353
1354 mutex_lock(&irq_domain_mutex);
Marc Zyngier6a6544e2017-06-20 22:17:44 +01001355 ret = irq_domain_alloc_irqs_hierarchy(domain, virq, nr_irqs, arg);
Jiang Liuf8264e32014-11-06 22:20:14 +08001356 if (ret < 0) {
1357 mutex_unlock(&irq_domain_mutex);
1358 goto out_free_irq_data;
1359 }
1360 for (i = 0; i < nr_irqs; i++)
1361 irq_domain_insert_irq(virq + i);
1362 mutex_unlock(&irq_domain_mutex);
1363
1364 return virq;
1365
1366out_free_irq_data:
1367 irq_domain_free_irq_data(virq, nr_irqs);
1368out_free_desc:
1369 irq_free_descs(virq, nr_irqs);
1370 return ret;
1371}
1372
David Daney495c38d2017-08-17 17:53:34 -07001373/* The irq_data was moved, fix the revmap to refer to the new location */
1374static void irq_domain_fix_revmap(struct irq_data *d)
1375{
Masahiro Yamadad03cc2d2017-09-22 21:20:41 +09001376 void __rcu **slot;
David Daney495c38d2017-08-17 17:53:34 -07001377
1378 if (d->hwirq < d->domain->revmap_size)
1379 return; /* Not using radix tree. */
1380
1381 /* Fix up the revmap. */
Masahiro Yamadaf1d78352017-10-05 10:44:54 +09001382 mutex_lock(&d->domain->revmap_tree_mutex);
David Daney495c38d2017-08-17 17:53:34 -07001383 slot = radix_tree_lookup_slot(&d->domain->revmap_tree, d->hwirq);
1384 if (slot)
1385 radix_tree_replace_slot(&d->domain->revmap_tree, slot, d);
Masahiro Yamadaf1d78352017-10-05 10:44:54 +09001386 mutex_unlock(&d->domain->revmap_tree_mutex);
David Daney495c38d2017-08-17 17:53:34 -07001387}
1388
1389/**
1390 * irq_domain_push_irq() - Push a domain in to the top of a hierarchy.
1391 * @domain: Domain to push.
1392 * @virq: Irq to push the domain in to.
1393 * @arg: Passed to the irq_domain_ops alloc() function.
1394 *
1395 * For an already existing irqdomain hierarchy, as might be obtained
1396 * via a call to pci_enable_msix(), add an additional domain to the
1397 * head of the processing chain. Must be called before request_irq()
1398 * has been called.
1399 */
1400int irq_domain_push_irq(struct irq_domain *domain, int virq, void *arg)
1401{
1402 struct irq_data *child_irq_data;
1403 struct irq_data *root_irq_data = irq_get_irq_data(virq);
1404 struct irq_desc *desc;
1405 int rv = 0;
1406
1407 /*
1408 * Check that no action has been set, which indicates the virq
1409 * is in a state where this function doesn't have to deal with
1410 * races between interrupt handling and maintaining the
1411 * hierarchy. This will catch gross misuse. Attempting to
1412 * make the check race free would require holding locks across
1413 * calls to struct irq_domain_ops->alloc(), which could lead
1414 * to deadlock, so we just do a simple check before starting.
1415 */
1416 desc = irq_to_desc(virq);
1417 if (!desc)
1418 return -EINVAL;
1419 if (WARN_ON(desc->action))
1420 return -EBUSY;
1421
1422 if (domain == NULL)
1423 return -EINVAL;
1424
1425 if (WARN_ON(!irq_domain_is_hierarchy(domain)))
1426 return -EINVAL;
1427
Dan Carpenter20c4d492017-08-25 15:14:09 +03001428 if (!root_irq_data)
David Daney495c38d2017-08-17 17:53:34 -07001429 return -EINVAL;
1430
Dan Carpenter20c4d492017-08-25 15:14:09 +03001431 if (domain->parent != root_irq_data->domain)
David Daney495c38d2017-08-17 17:53:34 -07001432 return -EINVAL;
1433
1434 child_irq_data = kzalloc_node(sizeof(*child_irq_data), GFP_KERNEL,
1435 irq_data_get_node(root_irq_data));
1436 if (!child_irq_data)
1437 return -ENOMEM;
1438
1439 mutex_lock(&irq_domain_mutex);
1440
1441 /* Copy the original irq_data. */
1442 *child_irq_data = *root_irq_data;
1443
1444 /*
1445 * Overwrite the root_irq_data, which is embedded in struct
1446 * irq_desc, with values for this domain.
1447 */
1448 root_irq_data->parent_data = child_irq_data;
1449 root_irq_data->domain = domain;
1450 root_irq_data->mask = 0;
1451 root_irq_data->hwirq = 0;
1452 root_irq_data->chip = NULL;
1453 root_irq_data->chip_data = NULL;
1454
1455 /* May (probably does) set hwirq, chip, etc. */
1456 rv = irq_domain_alloc_irqs_hierarchy(domain, virq, 1, arg);
1457 if (rv) {
1458 /* Restore the original irq_data. */
1459 *root_irq_data = *child_irq_data;
1460 goto error;
1461 }
1462
1463 irq_domain_fix_revmap(child_irq_data);
1464 irq_domain_set_mapping(domain, root_irq_data->hwirq, root_irq_data);
1465
1466error:
1467 mutex_unlock(&irq_domain_mutex);
1468
1469 return rv;
1470}
1471EXPORT_SYMBOL_GPL(irq_domain_push_irq);
1472
1473/**
1474 * irq_domain_pop_irq() - Remove a domain from the top of a hierarchy.
1475 * @domain: Domain to remove.
1476 * @virq: Irq to remove the domain from.
1477 *
1478 * Undo the effects of a call to irq_domain_push_irq(). Must be
1479 * called either before request_irq() or after free_irq().
1480 */
1481int irq_domain_pop_irq(struct irq_domain *domain, int virq)
1482{
1483 struct irq_data *root_irq_data = irq_get_irq_data(virq);
1484 struct irq_data *child_irq_data;
1485 struct irq_data *tmp_irq_data;
1486 struct irq_desc *desc;
1487
1488 /*
1489 * Check that no action is set, which indicates the virq is in
1490 * a state where this function doesn't have to deal with races
1491 * between interrupt handling and maintaining the hierarchy.
1492 * This will catch gross misuse. Attempting to make the check
1493 * race free would require holding locks across calls to
1494 * struct irq_domain_ops->free(), which could lead to
1495 * deadlock, so we just do a simple check before starting.
1496 */
1497 desc = irq_to_desc(virq);
1498 if (!desc)
1499 return -EINVAL;
1500 if (WARN_ON(desc->action))
1501 return -EBUSY;
1502
1503 if (domain == NULL)
1504 return -EINVAL;
1505
1506 if (!root_irq_data)
1507 return -EINVAL;
1508
1509 tmp_irq_data = irq_domain_get_irq_data(domain, virq);
1510
1511 /* We can only "pop" if this domain is at the top of the list */
1512 if (WARN_ON(root_irq_data != tmp_irq_data))
1513 return -EINVAL;
1514
1515 if (WARN_ON(root_irq_data->domain != domain))
1516 return -EINVAL;
1517
1518 child_irq_data = root_irq_data->parent_data;
1519 if (WARN_ON(!child_irq_data))
1520 return -EINVAL;
1521
1522 mutex_lock(&irq_domain_mutex);
1523
1524 root_irq_data->parent_data = NULL;
1525
1526 irq_domain_clear_mapping(domain, root_irq_data->hwirq);
1527 irq_domain_free_irqs_hierarchy(domain, virq, 1);
1528
1529 /* Restore the original irq_data. */
1530 *root_irq_data = *child_irq_data;
1531
1532 irq_domain_fix_revmap(root_irq_data);
1533
1534 mutex_unlock(&irq_domain_mutex);
1535
1536 kfree(child_irq_data);
1537
1538 return 0;
1539}
1540EXPORT_SYMBOL_GPL(irq_domain_pop_irq);
1541
Jiang Liuf8264e32014-11-06 22:20:14 +08001542/**
1543 * irq_domain_free_irqs - Free IRQ number and associated data structures
1544 * @virq: base IRQ number
1545 * @nr_irqs: number of IRQs to free
1546 */
1547void irq_domain_free_irqs(unsigned int virq, unsigned int nr_irqs)
1548{
1549 struct irq_data *data = irq_get_irq_data(virq);
1550 int i;
1551
1552 if (WARN(!data || !data->domain || !data->domain->ops->free,
1553 "NULL pointer, cannot free irq\n"))
1554 return;
1555
1556 mutex_lock(&irq_domain_mutex);
1557 for (i = 0; i < nr_irqs; i++)
1558 irq_domain_remove_irq(virq + i);
Marc Zyngier6a6544e2017-06-20 22:17:44 +01001559 irq_domain_free_irqs_hierarchy(data->domain, virq, nr_irqs);
Jiang Liuf8264e32014-11-06 22:20:14 +08001560 mutex_unlock(&irq_domain_mutex);
1561
1562 irq_domain_free_irq_data(virq, nr_irqs);
1563 irq_free_descs(virq, nr_irqs);
1564}
1565
1566/**
Jiang Liu36d72732014-11-15 22:24:01 +08001567 * irq_domain_alloc_irqs_parent - Allocate interrupts from parent domain
1568 * @irq_base: Base IRQ number
1569 * @nr_irqs: Number of IRQs to allocate
1570 * @arg: Allocation data (arch/domain specific)
1571 *
1572 * Check whether the domain has been setup recursive. If not allocate
1573 * through the parent domain.
1574 */
1575int irq_domain_alloc_irqs_parent(struct irq_domain *domain,
1576 unsigned int irq_base, unsigned int nr_irqs,
1577 void *arg)
1578{
Marc Zyngier6a6544e2017-06-20 22:17:44 +01001579 if (!domain->parent)
1580 return -ENOSYS;
Jiang Liu36d72732014-11-15 22:24:01 +08001581
Marc Zyngier6a6544e2017-06-20 22:17:44 +01001582 return irq_domain_alloc_irqs_hierarchy(domain->parent, irq_base,
1583 nr_irqs, arg);
Jiang Liu36d72732014-11-15 22:24:01 +08001584}
Quan Nguyen52b2a052016-03-03 21:56:52 +07001585EXPORT_SYMBOL_GPL(irq_domain_alloc_irqs_parent);
Jiang Liu36d72732014-11-15 22:24:01 +08001586
1587/**
1588 * irq_domain_free_irqs_parent - Free interrupts from parent domain
1589 * @irq_base: Base IRQ number
1590 * @nr_irqs: Number of IRQs to free
1591 *
1592 * Check whether the domain has been setup recursive. If not free
1593 * through the parent domain.
1594 */
1595void irq_domain_free_irqs_parent(struct irq_domain *domain,
1596 unsigned int irq_base, unsigned int nr_irqs)
1597{
Marc Zyngier6a6544e2017-06-20 22:17:44 +01001598 if (!domain->parent)
1599 return;
1600
1601 irq_domain_free_irqs_hierarchy(domain->parent, irq_base, nr_irqs);
Jiang Liu36d72732014-11-15 22:24:01 +08001602}
Quan Nguyen52b2a052016-03-03 21:56:52 +07001603EXPORT_SYMBOL_GPL(irq_domain_free_irqs_parent);
Jiang Liu36d72732014-11-15 22:24:01 +08001604
Marc Zyngier08d85f32017-01-17 16:00:48 +00001605static void __irq_domain_deactivate_irq(struct irq_data *irq_data)
1606{
1607 if (irq_data && irq_data->domain) {
1608 struct irq_domain *domain = irq_data->domain;
1609
1610 if (domain->ops->deactivate)
1611 domain->ops->deactivate(domain, irq_data);
1612 if (irq_data->parent_data)
1613 __irq_domain_deactivate_irq(irq_data->parent_data);
1614 }
1615}
1616
Thomas Gleixner702cb0a2017-12-29 16:59:06 +01001617static int __irq_domain_activate_irq(struct irq_data *irqd, bool reserve)
Thomas Gleixnerbb9b4282017-09-13 23:29:11 +02001618{
1619 int ret = 0;
1620
1621 if (irqd && irqd->domain) {
1622 struct irq_domain *domain = irqd->domain;
1623
1624 if (irqd->parent_data)
Thomas Gleixner42e1cc22017-09-13 23:29:12 +02001625 ret = __irq_domain_activate_irq(irqd->parent_data,
Thomas Gleixner702cb0a2017-12-29 16:59:06 +01001626 reserve);
Thomas Gleixnerbb9b4282017-09-13 23:29:11 +02001627 if (!ret && domain->ops->activate) {
Thomas Gleixner702cb0a2017-12-29 16:59:06 +01001628 ret = domain->ops->activate(domain, irqd, reserve);
Thomas Gleixnerbb9b4282017-09-13 23:29:11 +02001629 /* Rollback in case of error */
1630 if (ret && irqd->parent_data)
1631 __irq_domain_deactivate_irq(irqd->parent_data);
1632 }
1633 }
1634 return ret;
1635}
1636
Jiang Liu36d72732014-11-15 22:24:01 +08001637/**
Jiang Liuf8264e32014-11-06 22:20:14 +08001638 * irq_domain_activate_irq - Call domain_ops->activate recursively to activate
1639 * interrupt
Thomas Gleixner702cb0a2017-12-29 16:59:06 +01001640 * @irq_data: Outermost irq_data associated with interrupt
1641 * @reserve: If set only reserve an interrupt vector instead of assigning one
Jiang Liuf8264e32014-11-06 22:20:14 +08001642 *
1643 * This is the second step to call domain_ops->activate to program interrupt
1644 * controllers, so the interrupt could actually get delivered.
1645 */
Thomas Gleixner702cb0a2017-12-29 16:59:06 +01001646int irq_domain_activate_irq(struct irq_data *irq_data, bool reserve)
Jiang Liuf8264e32014-11-06 22:20:14 +08001647{
Thomas Gleixnerbb9b4282017-09-13 23:29:11 +02001648 int ret = 0;
1649
1650 if (!irqd_is_activated(irq_data))
Thomas Gleixner702cb0a2017-12-29 16:59:06 +01001651 ret = __irq_domain_activate_irq(irq_data, reserve);
Thomas Gleixnerbb9b4282017-09-13 23:29:11 +02001652 if (!ret)
Marc Zyngier08d85f32017-01-17 16:00:48 +00001653 irqd_set_activated(irq_data);
Thomas Gleixnerbb9b4282017-09-13 23:29:11 +02001654 return ret;
Jiang Liuf8264e32014-11-06 22:20:14 +08001655}
1656
1657/**
1658 * irq_domain_deactivate_irq - Call domain_ops->deactivate recursively to
1659 * deactivate interrupt
1660 * @irq_data: outermost irq_data associated with interrupt
1661 *
1662 * It calls domain_ops->deactivate to program interrupt controllers to disable
1663 * interrupt delivery.
1664 */
1665void irq_domain_deactivate_irq(struct irq_data *irq_data)
1666{
Marc Zyngier08d85f32017-01-17 16:00:48 +00001667 if (irqd_is_activated(irq_data)) {
1668 __irq_domain_deactivate_irq(irq_data);
1669 irqd_clr_activated(irq_data);
Jiang Liuf8264e32014-11-06 22:20:14 +08001670 }
1671}
1672
1673static void irq_domain_check_hierarchy(struct irq_domain *domain)
1674{
1675 /* Hierarchy irq_domains must implement callback alloc() */
1676 if (domain->ops->alloc)
1677 domain->flags |= IRQ_DOMAIN_FLAG_HIERARCHY;
1678}
Eric Auger631a9632017-01-19 20:57:57 +00001679
1680/**
1681 * irq_domain_hierarchical_is_msi_remap - Check if the domain or any
1682 * parent has MSI remapping support
1683 * @domain: domain pointer
1684 */
1685bool irq_domain_hierarchical_is_msi_remap(struct irq_domain *domain)
1686{
1687 for (; domain; domain = domain->parent) {
1688 if (irq_domain_is_msi_remap(domain))
1689 return true;
1690 }
1691 return false;
1692}
Jiang Liuf8264e32014-11-06 22:20:14 +08001693#else /* CONFIG_IRQ_DOMAIN_HIERARCHY */
1694/**
1695 * irq_domain_get_irq_data - Get irq_data associated with @virq and @domain
1696 * @domain: domain to match
1697 * @virq: IRQ number to get irq_data
1698 */
1699struct irq_data *irq_domain_get_irq_data(struct irq_domain *domain,
1700 unsigned int virq)
1701{
1702 struct irq_data *irq_data = irq_get_irq_data(virq);
1703
1704 return (irq_data && irq_data->domain == domain) ? irq_data : NULL;
1705}
Jake Oshinsa4289dc2015-12-10 17:52:59 +00001706EXPORT_SYMBOL_GPL(irq_domain_get_irq_data);
Jiang Liuf8264e32014-11-06 22:20:14 +08001707
Stefan Agner5f22f5c2015-05-16 11:44:13 +02001708/**
1709 * irq_domain_set_info - Set the complete data for a @virq in @domain
1710 * @domain: Interrupt domain to match
1711 * @virq: IRQ number
1712 * @hwirq: The hardware interrupt number
1713 * @chip: The associated interrupt chip
1714 * @chip_data: The associated interrupt chip data
1715 * @handler: The interrupt flow handler
1716 * @handler_data: The interrupt flow handler data
1717 * @handler_name: The interrupt handler name
1718 */
1719void irq_domain_set_info(struct irq_domain *domain, unsigned int virq,
1720 irq_hw_number_t hwirq, struct irq_chip *chip,
1721 void *chip_data, irq_flow_handler_t handler,
1722 void *handler_data, const char *handler_name)
1723{
1724 irq_set_chip_and_handler_name(virq, chip, handler, handler_name);
1725 irq_set_chip_data(virq, chip_data);
1726 irq_set_handler_data(virq, handler_data);
1727}
1728
Jiang Liuf8264e32014-11-06 22:20:14 +08001729static void irq_domain_check_hierarchy(struct irq_domain *domain)
1730{
1731}
1732#endif /* CONFIG_IRQ_DOMAIN_HIERARCHY */
Thomas Gleixner087cdfb2017-06-20 01:37:17 +02001733
1734#ifdef CONFIG_GENERIC_IRQ_DEBUGFS
1735static struct dentry *domain_dir;
1736
1737static void
1738irq_domain_debug_show_one(struct seq_file *m, struct irq_domain *d, int ind)
1739{
1740 seq_printf(m, "%*sname: %s\n", ind, "", d->name);
1741 seq_printf(m, "%*ssize: %u\n", ind + 1, "",
1742 d->revmap_size + d->revmap_direct_max_irq);
1743 seq_printf(m, "%*smapped: %u\n", ind + 1, "", d->mapcount);
1744 seq_printf(m, "%*sflags: 0x%08x\n", ind +1 , "", d->flags);
Thomas Gleixnerc3e72392017-09-13 23:29:06 +02001745 if (d->ops && d->ops->debug_show)
1746 d->ops->debug_show(m, d, NULL, ind + 1);
Thomas Gleixner087cdfb2017-06-20 01:37:17 +02001747#ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
1748 if (!d->parent)
1749 return;
1750 seq_printf(m, "%*sparent: %s\n", ind + 1, "", d->parent->name);
1751 irq_domain_debug_show_one(m, d->parent, ind + 4);
1752#endif
1753}
1754
1755static int irq_domain_debug_show(struct seq_file *m, void *p)
1756{
1757 struct irq_domain *d = m->private;
1758
1759 /* Default domain? Might be NULL */
1760 if (!d) {
1761 if (!irq_default_domain)
1762 return 0;
1763 d = irq_default_domain;
1764 }
1765 irq_domain_debug_show_one(m, d, 0);
1766 return 0;
1767}
Andy Shevchenko0b24a0b2018-02-14 17:47:35 +02001768DEFINE_SHOW_ATTRIBUTE(irq_domain_debug);
Thomas Gleixner087cdfb2017-06-20 01:37:17 +02001769
1770static void debugfs_add_domain_dir(struct irq_domain *d)
1771{
1772 if (!d->name || !domain_dir || d->debugfs_file)
1773 return;
1774 d->debugfs_file = debugfs_create_file(d->name, 0444, domain_dir, d,
Andy Shevchenko0b24a0b2018-02-14 17:47:35 +02001775 &irq_domain_debug_fops);
Thomas Gleixner087cdfb2017-06-20 01:37:17 +02001776}
1777
1778static void debugfs_remove_domain_dir(struct irq_domain *d)
1779{
Thomas Gleixnerf610c9d2017-07-07 08:57:57 +02001780 debugfs_remove(d->debugfs_file);
Marc Zyngier513145e2018-10-01 11:05:21 +01001781 d->debugfs_file = NULL;
Thomas Gleixner087cdfb2017-06-20 01:37:17 +02001782}
1783
1784void __init irq_domain_debugfs_init(struct dentry *root)
1785{
1786 struct irq_domain *d;
1787
1788 domain_dir = debugfs_create_dir("domains", root);
Thomas Gleixner087cdfb2017-06-20 01:37:17 +02001789
Andy Shevchenko0b24a0b2018-02-14 17:47:35 +02001790 debugfs_create_file("default", 0444, domain_dir, NULL,
1791 &irq_domain_debug_fops);
Thomas Gleixner087cdfb2017-06-20 01:37:17 +02001792 mutex_lock(&irq_domain_mutex);
1793 list_for_each_entry(d, &irq_domain_list, link)
1794 debugfs_add_domain_dir(d);
1795 mutex_unlock(&irq_domain_mutex);
1796}
1797#endif