Merge tag 'i3c/for-5.3' of git://git.kernel.org/pub/scm/linux/kernel/git/i3c/linux
[linux-2.6-block.git] / kernel / irq / irqdomain.c
CommitLineData
52a65ff5
TG
1// SPDX-License-Identifier: GPL-2.0
2
54a90588
PM
3#define pr_fmt(fmt) "irq: " fmt
4
c5c601c4 5#include <linux/acpi.h>
cc79ca69
GL
6#include <linux/debugfs.h>
7#include <linux/hardirq.h>
8#include <linux/interrupt.h>
08a543ad 9#include <linux/irq.h>
cc79ca69 10#include <linux/irqdesc.h>
08a543ad
GL
11#include <linux/irqdomain.h>
12#include <linux/module.h>
13#include <linux/mutex.h>
14#include <linux/of.h>
7e713301 15#include <linux/of_address.h>
64be38ab 16#include <linux/of_irq.h>
5ca4db61 17#include <linux/topology.h>
cc79ca69 18#include <linux/seq_file.h>
7e713301 19#include <linux/slab.h>
cc79ca69
GL
20#include <linux/smp.h>
21#include <linux/fs.h>
08a543ad
GL
22
23static LIST_HEAD(irq_domain_list);
24static DEFINE_MUTEX(irq_domain_mutex);
25
68700650 26static struct irq_domain *irq_default_domain;
cc79ca69 27
f8264e34
JL
28static void irq_domain_check_hierarchy(struct irq_domain *domain);
29
b145dcc4 30struct irqchip_fwid {
d59f6617
TG
31 struct fwnode_handle fwnode;
32 unsigned int type;
33 char *name;
087cdfb6 34 void *data;
b145dcc4
MZ
35};
36
087cdfb6
TG
37#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
db3e50f3 45const struct fwnode_operations irqchip_fwnode_ops;
b6eb66fd 46EXPORT_SYMBOL_GPL(irqchip_fwnode_ops);
db3e50f3 47
b145dcc4
MZ
48/**
49 * irq_domain_alloc_fwnode - Allocate a fwnode_handle suitable for
50 * identifying an irq domain
d59f6617
TG
51 * @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
b145dcc4 55 *
d59f6617 56 * Allocate a struct irqchip_fwid, and return a poiner to the embedded
b145dcc4 57 * fwnode_handle (or NULL on failure).
d59f6617
TG
58 *
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.
b145dcc4 63 */
d59f6617
TG
64struct fwnode_handle *__irq_domain_alloc_fwnode(unsigned int type, int id,
65 const char *name, void *data)
b145dcc4
MZ
66{
67 struct irqchip_fwid *fwid;
d59f6617 68 char *n;
b145dcc4
MZ
69
70 fwid = kzalloc(sizeof(*fwid), GFP_KERNEL);
b145dcc4 71
d59f6617
TG
72 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) {
b145dcc4 85 kfree(fwid);
d59f6617 86 kfree(n);
b145dcc4
MZ
87 return NULL;
88 }
89
d59f6617
TG
90 fwid->type = type;
91 fwid->name = n;
b145dcc4 92 fwid->data = data;
db3e50f3 93 fwid->fwnode.ops = &irqchip_fwnode_ops;
b145dcc4
MZ
94 return &fwid->fwnode;
95}
d59f6617 96EXPORT_SYMBOL_GPL(__irq_domain_alloc_fwnode);
b145dcc4
MZ
97
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
75aba7b0 107 if (WARN_ON(!is_fwnode_irqchip(fwnode)))
b145dcc4
MZ
108 return;
109
110 fwid = container_of(fwnode, struct irqchip_fwid, fwnode);
111 kfree(fwid->name);
112 kfree(fwid);
113}
a4289dc2 114EXPORT_SYMBOL_GPL(irq_domain_free_fwnode);
b145dcc4 115
cc79ca69 116/**
fa40f377 117 * __irq_domain_add() - Allocate a new irq_domain data structure
545d5d65 118 * @fwnode: firmware node for the interrupt controller
fa40f377 119 * @size: Size of linear map; 0 for radix mapping only
a257954b 120 * @hwirq_max: Maximum number of interrupts supported by controller
fa40f377
GL
121 * @direct_max: Maximum value of direct maps; Use ~0 for no limit; 0 for no
122 * direct mapping
f8264e34 123 * @ops: domain callbacks
a8db8cf0 124 * @host_data: Controller private data pointer
cc79ca69 125 *
3a1d24ca 126 * Allocates and initializes an irq_domain structure.
a257954b 127 * Returns pointer to IRQ domain, or NULL on failure.
cc79ca69 128 */
1bf4ddc4 129struct irq_domain *__irq_domain_add(struct fwnode_handle *fwnode, int size,
ddaf144c 130 irq_hw_number_t hwirq_max, int direct_max,
fa40f377
GL
131 const struct irq_domain_ops *ops,
132 void *host_data)
cc79ca69 133{
545d5d65 134 struct device_node *of_node = to_of_node(fwnode);
d59f6617 135 struct irqchip_fwid *fwid;
a8db8cf0 136 struct irq_domain *domain;
cc79ca69 137
d59f6617
TG
138 static atomic_t unknown_domains;
139
cef5075c
GL
140 domain = kzalloc_node(sizeof(*domain) + (sizeof(unsigned int) * size),
141 GFP_KERNEL, of_node_to_nid(of_node));
43b98d87 142 if (!domain)
cc79ca69
GL
143 return NULL;
144
d59f6617
TG
145 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 }
c5c601c4
MZ
163#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
d59f6617
TG
178 } 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 */
94967b55 186 name = kasprintf(GFP_KERNEL, "%pOF", of_node);
d59f6617
TG
187 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) {
db3e50f3
SA
200 if (fwnode)
201 pr_err("Invalid fwnode type for irqdomain\n");
d59f6617
TG
202 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
f110711a 211 of_node_get(of_node);
f110711a 212
cc79ca69 213 /* Fill structure */
1aa0dd94 214 INIT_RADIX_TREE(&domain->revmap_tree, GFP_KERNEL);
f1d78358 215 mutex_init(&domain->revmap_tree_mutex);
68700650 216 domain->ops = ops;
a8db8cf0 217 domain->host_data = host_data;
ddaf144c 218 domain->hwirq_max = hwirq_max;
1aa0dd94 219 domain->revmap_size = size;
fa40f377 220 domain->revmap_direct_max_irq = direct_max;
f8264e34 221 irq_domain_check_hierarchy(domain);
cc79ca69 222
a8db8cf0 223 mutex_lock(&irq_domain_mutex);
087cdfb6 224 debugfs_add_domain_dir(domain);
a8db8cf0
GL
225 list_add(&domain->link, &irq_domain_list);
226 mutex_unlock(&irq_domain_mutex);
fa40f377 227
1aa0dd94 228 pr_debug("Added domain %s\n", domain->name);
fa40f377 229 return domain;
a8db8cf0 230}
fa40f377 231EXPORT_SYMBOL_GPL(__irq_domain_add);
a8db8cf0 232
58ee99ad
PM
233/**
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);
087cdfb6 244 debugfs_remove_domain_dir(domain);
58ee99ad 245
e9256efc 246 WARN_ON(!radix_tree_empty(&domain->revmap_tree));
58ee99ad
PM
247
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
1aa0dd94 258 pr_debug("Removed domain %s\n", domain->name);
58ee99ad 259
5d4c9bc7 260 of_node_put(irq_domain_get_of_node(domain));
d59f6617
TG
261 if (domain->flags & IRQ_DOMAIN_NAME_ALLOCATED)
262 kfree(domain->name);
fa40f377 263 kfree(domain);
58ee99ad 264}
ecd84eb2 265EXPORT_SYMBOL_GPL(irq_domain_remove);
58ee99ad 266
61d0a000
MZ
267void 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
781d0f46 298/**
fa40f377 299 * irq_domain_add_simple() - Register an irq_domain and optionally map a range of irqs
781d0f46
MB
300 * @of_node: pointer to interrupt controller's device tree node.
301 * @size: total number of irqs in mapping
94a63da0 302 * @first_irq: first number of irq block assigned to the domain,
fa40f377
GL
303 * 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.
f8264e34 305 * @ops: domain callbacks
781d0f46
MB
306 * @host_data: Controller private data pointer
307 *
fa40f377
GL
308 * 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.
781d0f46
MB
310 *
311 * This is intended to implement the expected behaviour for most
fa40f377
GL
312 * 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.
781d0f46
MB
315 */
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{
fa40f377
GL
322 struct irq_domain *domain;
323
1bf4ddc4 324 domain = __irq_domain_add(of_node_to_fwnode(of_node), size, size, 0, ops, host_data);
fa40f377
GL
325 if (!domain)
326 return NULL;
2854d167 327
fa40f377 328 if (first_irq > 0) {
2854d167 329 if (IS_ENABLED(CONFIG_SPARSE_IRQ)) {
fa40f377
GL
330 /* 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)
d202b7b9
LW
334 pr_info("Cannot allocate irq_descs @ IRQ%d, assuming pre-allocated\n",
335 first_irq);
fa40f377 336 }
ddaf144c 337 irq_domain_associate_many(domain, first_irq, 0, size);
2854d167
LW
338 }
339
fa40f377 340 return domain;
781d0f46 341}
346dbb79 342EXPORT_SYMBOL_GPL(irq_domain_add_simple);
781d0f46 343
a8db8cf0
GL
344/**
345 * irq_domain_add_legacy() - Allocate and register a legacy revmap irq_domain.
346 * @of_node: pointer to interrupt controller's device tree node.
1bc04f2c
GL
347 * @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.
a8db8cf0
GL
352 * @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,
1bc04f2c
GL
360 unsigned int size,
361 unsigned int first_irq,
362 irq_hw_number_t first_hwirq,
a18dc81b 363 const struct irq_domain_ops *ops,
a8db8cf0
GL
364 void *host_data)
365{
1bc04f2c 366 struct irq_domain *domain;
a8db8cf0 367
1bf4ddc4 368 domain = __irq_domain_add(of_node_to_fwnode(of_node), first_hwirq + size,
ddaf144c 369 first_hwirq + size, 0, ops, host_data);
f8264e34
JL
370 if (domain)
371 irq_domain_associate_many(domain, first_irq, first_hwirq, size);
1bc04f2c 372
a8db8cf0
GL
373 return domain;
374}
ecd84eb2 375EXPORT_SYMBOL_GPL(irq_domain_add_legacy);
a8db8cf0 376
cc79ca69 377/**
651e8b54
MZ
378 * irq_find_matching_fwspec() - Locates a domain for a given fwspec
379 * @fwspec: FW specifier for an interrupt
ad3aedfb 380 * @bus_token: domain-specific data
cc79ca69 381 */
651e8b54 382struct irq_domain *irq_find_matching_fwspec(struct irq_fwspec *fwspec,
130b8c6c 383 enum irq_domain_bus_token bus_token)
cc79ca69
GL
384{
385 struct irq_domain *h, *found = NULL;
651e8b54 386 struct fwnode_handle *fwnode = fwspec->fwnode;
a18dc81b 387 int rc;
cc79ca69
GL
388
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...
ad3aedfb
MZ
393 *
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.
cc79ca69
GL
397 */
398 mutex_lock(&irq_domain_mutex);
a18dc81b 399 list_for_each_entry(h, &irq_domain_list, link) {
651e8b54
MZ
400 if (h->ops->select && fwspec->param_count)
401 rc = h->ops->select(h, fwspec, bus_token);
402 else if (h->ops->match)
130b8c6c 403 rc = h->ops->match(h, to_of_node(fwnode), bus_token);
a18dc81b 404 else
130b8c6c 405 rc = ((fwnode != NULL) && (h->fwnode == fwnode) &&
ad3aedfb
MZ
406 ((bus_token == DOMAIN_BUS_ANY) ||
407 (h->bus_token == bus_token)));
a18dc81b
GL
408
409 if (rc) {
cc79ca69
GL
410 found = h;
411 break;
412 }
a18dc81b 413 }
cc79ca69
GL
414 mutex_unlock(&irq_domain_mutex);
415 return found;
416}
651e8b54 417EXPORT_SYMBOL_GPL(irq_find_matching_fwspec);
cc79ca69 418
c7b41f0a
EA
419/**
420 * 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
cc79ca69
GL
444/**
445 * irq_set_default_host() - Set a "default" irq domain
68700650 446 * @domain: default domain pointer
cc79ca69
GL
447 *
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 */
68700650 453void irq_set_default_host(struct irq_domain *domain)
cc79ca69 454{
54a90588 455 pr_debug("Default domain set to @0x%p\n", domain);
cc79ca69 456
68700650 457 irq_default_domain = domain;
cc79ca69 458}
ecd84eb2 459EXPORT_SYMBOL_GPL(irq_set_default_host);
cc79ca69 460
9f199dd3
MZ
461/**
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
b526adfe
DD
475static 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 {
f1d78358 481 mutex_lock(&domain->revmap_tree_mutex);
b526adfe 482 radix_tree_delete(&domain->revmap_tree, hwirq);
f1d78358 483 mutex_unlock(&domain->revmap_tree_mutex);
b526adfe
DD
484 }
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 {
f1d78358 494 mutex_lock(&domain->revmap_tree_mutex);
b526adfe 495 radix_tree_insert(&domain->revmap_tree, hwirq, irq_data);
f1d78358 496 mutex_unlock(&domain->revmap_tree_mutex);
b526adfe
DD
497 }
498}
499
43a77591 500void irq_domain_disassociate(struct irq_domain *domain, unsigned int irq)
913af207 501{
ddaf144c
GL
502 struct irq_data *irq_data = irq_get_irq_data(irq);
503 irq_hw_number_t hwirq;
913af207 504
ddaf144c
GL
505 if (WARN(!irq_data || irq_data->domain != domain,
506 "virq%i doesn't exist; cannot disassociate\n", irq))
507 return;
913af207 508
ddaf144c
GL
509 hwirq = irq_data->hwirq;
510 irq_set_status_flags(irq, IRQ_NOREQUEST);
913af207 511
ddaf144c
GL
512 /* remove chip and handler */
513 irq_set_chip_and_handler(irq, NULL, NULL);
913af207 514
ddaf144c
GL
515 /* Make sure it's completed */
516 synchronize_irq(irq);
913af207 517
ddaf144c
GL
518 /* Tell the PIC about it */
519 if (domain->ops->unmap)
520 domain->ops->unmap(domain, irq);
521 smp_mb();
913af207 522
ddaf144c
GL
523 irq_data->domain = NULL;
524 irq_data->hwirq = 0;
9dc6be3d 525 domain->mapcount--;
913af207 526
ddaf144c 527 /* Clear reverse map for this hwirq */
b526adfe 528 irq_domain_clear_mapping(domain, hwirq);
913af207
GL
529}
530
ddaf144c
GL
531int irq_domain_associate(struct irq_domain *domain, unsigned int virq,
532 irq_hw_number_t hwirq)
cc79ca69 533{
ddaf144c
GL
534 struct irq_data *irq_data = irq_get_irq_data(virq);
535 int ret;
cc79ca69 536
ddaf144c
GL
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;
cc79ca69 544
ddaf144c
GL
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);
f5a1ad05 559 }
ddaf144c
GL
560 irq_data->domain = NULL;
561 irq_data->hwirq = 0;
562 mutex_unlock(&irq_domain_mutex);
563 return ret;
98aa468e
GL
564 }
565
ddaf144c
GL
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 }
2a71a1a9 570
9dc6be3d 571 domain->mapcount++;
b526adfe 572 irq_domain_set_mapping(domain, hwirq, irq_data);
ddaf144c
GL
573 mutex_unlock(&irq_domain_mutex);
574
575 irq_clear_status_flags(virq, IRQ_NOREQUEST);
cc79ca69
GL
576
577 return 0;
578}
ddaf144c 579EXPORT_SYMBOL_GPL(irq_domain_associate);
98aa468e 580
ddaf144c
GL
581void irq_domain_associate_many(struct irq_domain *domain, unsigned int irq_base,
582 irq_hw_number_t hwirq_base, int count)
583{
5d4c9bc7 584 struct device_node *of_node;
ddaf144c
GL
585 int i;
586
5d4c9bc7 587 of_node = irq_domain_get_of_node(domain);
ddaf144c 588 pr_debug("%s(%s, irqbase=%i, hwbase=%i, count=%i)\n", __func__,
5d4c9bc7 589 of_node_full_name(of_node), irq_base, (int)hwirq_base, count);
ddaf144c
GL
590
591 for (i = 0; i < count; i++) {
592 irq_domain_associate(domain, irq_base + i, hwirq_base + i);
593 }
cc79ca69 594}
98aa468e 595EXPORT_SYMBOL_GPL(irq_domain_associate_many);
cc79ca69
GL
596
597/**
598 * irq_create_direct_mapping() - Allocate an irq for direct mapping
68700650 599 * @domain: domain to allocate the irq for or NULL for default domain
cc79ca69
GL
600 *
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
1aa0dd94
GL
603 * 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.
cc79ca69 606 */
68700650 607unsigned int irq_create_direct_mapping(struct irq_domain *domain)
cc79ca69 608{
5d4c9bc7 609 struct device_node *of_node;
cc79ca69
GL
610 unsigned int virq;
611
68700650
GL
612 if (domain == NULL)
613 domain = irq_default_domain;
cc79ca69 614
5d4c9bc7
MZ
615 of_node = irq_domain_get_of_node(domain);
616 virq = irq_alloc_desc_from(1, of_node_to_nid(of_node));
03848373 617 if (!virq) {
54a90588 618 pr_debug("create_direct virq allocation failed\n");
03848373 619 return 0;
cc79ca69 620 }
1aa0dd94 621 if (virq >= domain->revmap_direct_max_irq) {
cc79ca69 622 pr_err("ERROR: no free irqs available below %i maximum\n",
1aa0dd94 623 domain->revmap_direct_max_irq);
cc79ca69
GL
624 irq_free_desc(virq);
625 return 0;
626 }
54a90588 627 pr_debug("create_direct obtained virq %d\n", virq);
cc79ca69 628
98aa468e 629 if (irq_domain_associate(domain, virq, virq)) {
cc79ca69 630 irq_free_desc(virq);
03848373 631 return 0;
cc79ca69
GL
632 }
633
634 return virq;
635}
ecd84eb2 636EXPORT_SYMBOL_GPL(irq_create_direct_mapping);
cc79ca69
GL
637
638/**
639 * irq_create_mapping() - Map a hardware interrupt into linux irq space
68700650
GL
640 * @domain: domain owning this hardware interrupt or NULL for default domain
641 * @hwirq: hardware irq number in that domain space
cc79ca69
GL
642 *
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 */
68700650 648unsigned int irq_create_mapping(struct irq_domain *domain,
cc79ca69
GL
649 irq_hw_number_t hwirq)
650{
5d4c9bc7 651 struct device_node *of_node;
5b7526e3 652 int virq;
cc79ca69 653
54a90588 654 pr_debug("irq_create_mapping(0x%p, 0x%lx)\n", domain, hwirq);
cc79ca69 655
68700650
GL
656 /* Look for default domain if nececssary */
657 if (domain == NULL)
658 domain = irq_default_domain;
659 if (domain == NULL) {
798f0fd1 660 WARN(1, "%s(, %lx) called with NULL domain\n", __func__, hwirq);
03848373 661 return 0;
cc79ca69 662 }
54a90588 663 pr_debug("-> using domain @%p\n", domain);
cc79ca69 664
5d4c9bc7
MZ
665 of_node = irq_domain_get_of_node(domain);
666
cc79ca69 667 /* Check if mapping already exists */
68700650 668 virq = irq_find_mapping(domain, hwirq);
03848373 669 if (virq) {
54a90588 670 pr_debug("-> existing mapping on virq %d\n", virq);
cc79ca69
GL
671 return virq;
672 }
673
1bc04f2c 674 /* Allocate a virtual interrupt number */
06ee6d57 675 virq = irq_domain_alloc_descs(-1, 1, hwirq, of_node_to_nid(of_node), NULL);
5b7526e3 676 if (virq <= 0) {
54a90588 677 pr_debug("-> virq allocation failed\n");
1bc04f2c 678 return 0;
cc79ca69
GL
679 }
680
98aa468e 681 if (irq_domain_associate(domain, virq, hwirq)) {
73255704 682 irq_free_desc(virq);
03848373 683 return 0;
cc79ca69
GL
684 }
685
54a90588 686 pr_debug("irq %lu on domain %s mapped to virtual irq %u\n",
5d4c9bc7 687 hwirq, of_node_full_name(of_node), virq);
cc79ca69
GL
688
689 return virq;
690}
691EXPORT_SYMBOL_GPL(irq_create_mapping);
692
98aa468e
GL
693/**
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{
5d4c9bc7 714 struct device_node *of_node;
98aa468e
GL
715 int ret;
716
5d4c9bc7 717 of_node = irq_domain_get_of_node(domain);
98aa468e 718 ret = irq_alloc_descs(irq_base, irq_base, count,
5d4c9bc7 719 of_node_to_nid(of_node));
98aa468e
GL
720 if (unlikely(ret < 0))
721 return ret;
722
ddaf144c 723 irq_domain_associate_many(domain, irq_base, hwirq_base, count);
98aa468e
GL
724 return 0;
725}
726EXPORT_SYMBOL_GPL(irq_create_strict_mappings);
727
11e4438e
MZ
728static 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
b5c231d8
BM
746static void of_phandle_args_to_fwspec(struct device_node *np, const u32 *args,
747 unsigned int count,
11e4438e
MZ
748 struct irq_fwspec *fwspec)
749{
750 int i;
751
b5c231d8
BM
752 fwspec->fwnode = np ? &np->fwnode : NULL;
753 fwspec->param_count = count;
11e4438e 754
b5c231d8
BM
755 for (i = 0; i < count; i++)
756 fwspec->param[i] = args[i];
11e4438e
MZ
757}
758
c0131f09 759unsigned int irq_create_fwspec_mapping(struct irq_fwspec *fwspec)
cc79ca69 760{
68700650 761 struct irq_domain *domain;
1e2a7d78 762 struct irq_data *irq_data;
cc79ca69
GL
763 irq_hw_number_t hwirq;
764 unsigned int type = IRQ_TYPE_NONE;
f8264e34 765 int virq;
cc79ca69 766
530cbe10 767 if (fwspec->fwnode) {
651e8b54 768 domain = irq_find_matching_fwspec(fwspec, DOMAIN_BUS_WIRED);
530cbe10 769 if (!domain)
651e8b54 770 domain = irq_find_matching_fwspec(fwspec, DOMAIN_BUS_ANY);
530cbe10 771 } else {
11e4438e 772 domain = irq_default_domain;
530cbe10 773 }
11e4438e 774
68700650 775 if (!domain) {
798f0fd1 776 pr_warn("no irq domain found for %s !\n",
c0131f09 777 of_node_full_name(to_of_node(fwspec->fwnode)));
03848373 778 return 0;
cc79ca69
GL
779 }
780
c0131f09 781 if (irq_domain_translate(domain, fwspec, &hwirq, &type))
11e4438e 782 return 0;
cc79ca69 783
b62b2cf5
JH
784 /*
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) {
0cc01aba 797 /*
b62b2cf5
JH
798 * If the trigger type is not specified or matches the
799 * current trigger type then we are done so return the
800 * interrupt number.
0cc01aba 801 */
b62b2cf5 802 if (type == IRQ_TYPE_NONE || type == irq_get_trigger_type(virq))
0cc01aba
YC
803 return virq;
804
b62b2cf5
JH
805 /*
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) {
1e2a7d78
JH
810 irq_data = irq_get_irq_data(virq);
811 if (!irq_data)
812 return 0;
813
814 irqd_set_trigger_type(irq_data, type);
b62b2cf5
JH
815 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)) {
c0131f09 824 virq = irq_domain_alloc_irqs(domain, 1, NUMA_NO_NODE, fwspec);
0cc01aba
YC
825 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 }
cc79ca69 833
1e2a7d78
JH
834 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
cc79ca69
GL
846 return virq;
847}
c0131f09
MZ
848EXPORT_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
b5c231d8
BM
854 of_phandle_args_to_fwspec(irq_data->np, irq_data->args,
855 irq_data->args_count, &fwspec);
856
c0131f09
MZ
857 return irq_create_fwspec_mapping(&fwspec);
858}
cc79ca69
GL
859EXPORT_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);
68700650 868 struct irq_domain *domain;
cc79ca69 869
03848373 870 if (!virq || !irq_data)
cc79ca69
GL
871 return;
872
68700650
GL
873 domain = irq_data->domain;
874 if (WARN_ON(domain == NULL))
cc79ca69
GL
875 return;
876
d16dcd3d
JH
877 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 }
cc79ca69
GL
883}
884EXPORT_SYMBOL_GPL(irq_dispose_mapping);
885
886/**
b8d62f33 887 * irq_find_mapping() - Find a linux irq from a hw irq number.
68700650
GL
888 * @domain: domain owning this hardware interrupt
889 * @hwirq: hardware irq number in that domain space
cc79ca69 890 */
68700650 891unsigned int irq_find_mapping(struct irq_domain *domain,
cc79ca69
GL
892 irq_hw_number_t hwirq)
893{
4c0946c4 894 struct irq_data *data;
cc79ca69 895
68700650
GL
896 /* Look for default domain if nececssary */
897 if (domain == NULL)
898 domain = irq_default_domain;
899 if (domain == NULL)
03848373 900 return 0;
cc79ca69 901
1aa0dd94 902 if (hwirq < domain->revmap_direct_max_irq) {
f8264e34
JL
903 data = irq_domain_get_irq_data(domain, hwirq);
904 if (data && data->hwirq == hwirq)
4c0946c4 905 return hwirq;
4c0946c4
GL
906 }
907
d3dcb436
GL
908 /* Check if the hwirq is in the linear revmap. */
909 if (hwirq < domain->revmap_size)
910 return domain->linear_revmap[hwirq];
cc79ca69 911
d3dcb436
GL
912 rcu_read_lock();
913 data = radix_tree_lookup(&domain->revmap_tree, hwirq);
914 rcu_read_unlock();
915 return data ? data->irq : 0;
cc79ca69 916}
cc79ca69 917EXPORT_SYMBOL_GPL(irq_find_mapping);
cc79ca69 918
16b2e6e2
GL
919/**
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)
7e713301 928{
16b2e6e2 929 if (WARN_ON(intsize < 1))
7e713301 930 return -EINVAL;
7e713301
GL
931 *out_hwirq = intspec[0];
932 *out_type = IRQ_TYPE_NONE;
7e713301
GL
933 return 0;
934}
16b2e6e2
GL
935EXPORT_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{
b5c231d8
BM
948 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);
16b2e6e2
GL
952}
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];
0c228919
SF
974 if (intsize > 1)
975 *out_type = intspec[1] & IRQ_TYPE_SENSE_MASK;
976 else
977 *out_type = IRQ_TYPE_NONE;
16b2e6e2
GL
978 return 0;
979}
980EXPORT_SYMBOL_GPL(irq_domain_xlate_onetwocell);
7e713301 981
a18dc81b 982const struct irq_domain_ops irq_domain_simple_ops = {
16b2e6e2 983 .xlate = irq_domain_xlate_onetwocell,
75294957
GL
984};
985EXPORT_SYMBOL_GPL(irq_domain_simple_ops);
f8264e34 986
b5c231d8
BM
987/**
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
ac0a0cd2 1008int irq_domain_alloc_descs(int virq, unsigned int cnt, irq_hw_number_t hwirq,
bec04037 1009 int node, const struct irq_affinity_desc *affinity)
f8264e34
JL
1010{
1011 unsigned int hint;
1012
1013 if (virq >= 0) {
06ee6d57
TG
1014 virq = __irq_alloc_descs(virq, virq, cnt, node, THIS_MODULE,
1015 affinity);
f8264e34
JL
1016 } else {
1017 hint = hwirq % nr_irqs;
1018 if (hint == 0)
1019 hint++;
06ee6d57
TG
1020 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 }
f8264e34
JL
1026 }
1027
1028 return virq;
1029}
1030
1031#ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
afb7da83 1032/**
2a5e9a07 1033 * irq_domain_create_hierarchy - Add a irqdomain into the hierarchy
afb7da83
JL
1034 * @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
2a5e9a07 1037 * @fwnode: Optional fwnode of the interrupt controller
afb7da83
JL
1038 * @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 */
2a5e9a07 1047struct irq_domain *irq_domain_create_hierarchy(struct irq_domain *parent,
afb7da83
JL
1048 unsigned int flags,
1049 unsigned int size,
2a5e9a07 1050 struct fwnode_handle *fwnode,
afb7da83
JL
1051 const struct irq_domain_ops *ops,
1052 void *host_data)
1053{
1054 struct irq_domain *domain;
1055
1056 if (size)
2a5e9a07 1057 domain = irq_domain_create_linear(fwnode, size, ops, host_data);
afb7da83 1058 else
2a5e9a07 1059 domain = irq_domain_create_tree(fwnode, ops, host_data);
afb7da83
JL
1060 if (domain) {
1061 domain->parent = parent;
1062 domain->flags |= flags;
1063 }
1064
1065 return domain;
1066}
52b2a05f 1067EXPORT_SYMBOL_GPL(irq_domain_create_hierarchy);
afb7da83 1068
f8264e34
JL
1069static 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;
f8264e34 1075
9dc6be3d 1076 domain->mapcount++;
b526adfe 1077 irq_domain_set_mapping(domain, data->hwirq, data);
f8264e34
JL
1078
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
9dc6be3d 1100 domain->mapcount--;
b526adfe 1101 irq_domain_clear_mapping(domain, hwirq);
f8264e34
JL
1102 }
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
6783011b
JL
1110 irq_data = kzalloc_node(sizeof(*irq_data), GFP_KERNEL,
1111 irq_data_get_node(child));
f8264e34
JL
1112 if (irq_data) {
1113 child->parent_data = irq_data;
1114 irq_data->irq = child->irq;
0d0b4c86 1115 irq_data->common = child->common;
f8264e34
JL
1116 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}
a4289dc2 1182EXPORT_SYMBOL_GPL(irq_domain_get_irq_data);
f8264e34
JL
1183
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}
52b2a05f 1207EXPORT_SYMBOL_GPL(irq_domain_set_hwirq_and_chip);
f8264e34 1208
1b537708
JL
1209/**
1210 * 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}
64bce3e8 1229EXPORT_SYMBOL(irq_domain_set_info);
1b537708 1230
f8264e34
JL
1231/**
1232 * 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}
52b2a05f 1241EXPORT_SYMBOL_GPL(irq_domain_reset_irq_data);
f8264e34
JL
1242
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}
63cc787e 1262EXPORT_SYMBOL_GPL(irq_domain_free_irqs_common);
f8264e34
JL
1263
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
6a6544e5 1282static void irq_domain_free_irqs_hierarchy(struct irq_domain *domain,
36d72731
JL
1283 unsigned int irq_base,
1284 unsigned int nr_irqs)
1285{
0d12ec07
DD
1286 if (domain->ops->free)
1287 domain->ops->free(domain, irq_base, nr_irqs);
36d72731
JL
1288}
1289
6a6544e5 1290int irq_domain_alloc_irqs_hierarchy(struct irq_domain *domain,
c466595c
MZ
1291 unsigned int irq_base,
1292 unsigned int nr_irqs, void *arg)
36d72731 1293{
6a6544e5 1294 return domain->ops->alloc(domain, irq_base, nr_irqs, arg);
36d72731
JL
1295}
1296
f8264e34
JL
1297/**
1298 * __irq_domain_alloc_irqs - Allocate IRQs from domain
1299 * @domain: domain to allocate from
08970ecf 1300 * @irq_base: allocate specified IRQ number if irq_base >= 0
f8264e34
JL
1301 * @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
06ee6d57 1305 * @affinity: Optional irq affinity mask for multiqueue devices
f8264e34
JL
1306 *
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,
bec04037 1321 bool realloc, const struct irq_affinity_desc *affinity)
f8264e34
JL
1322{
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 {
06ee6d57
TG
1339 virq = irq_domain_alloc_descs(irq_base, nr_irqs, 0, node,
1340 affinity);
f8264e34
JL
1341 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);
6a6544e5 1355 ret = irq_domain_alloc_irqs_hierarchy(domain, virq, nr_irqs, arg);
f8264e34
JL
1356 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
495c38d3
DD
1373/* 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{
d03cc2d8 1376 void __rcu **slot;
495c38d3
DD
1377
1378 if (d->hwirq < d->domain->revmap_size)
1379 return; /* Not using radix tree. */
1380
1381 /* Fix up the revmap. */
f1d78358 1382 mutex_lock(&d->domain->revmap_tree_mutex);
495c38d3
DD
1383 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);
f1d78358 1386 mutex_unlock(&d->domain->revmap_tree_mutex);
495c38d3
DD
1387}
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
20c4d49c 1428 if (!root_irq_data)
495c38d3
DD
1429 return -EINVAL;
1430
20c4d49c 1431 if (domain->parent != root_irq_data->domain)
495c38d3
DD
1432 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
f8264e34
JL
1542/**
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);
6a6544e5 1559 irq_domain_free_irqs_hierarchy(data->domain, virq, nr_irqs);
f8264e34
JL
1560 mutex_unlock(&irq_domain_mutex);
1561
1562 irq_domain_free_irq_data(virq, nr_irqs);
1563 irq_free_descs(virq, nr_irqs);
1564}
1565
36d72731
JL
1566/**
1567 * 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{
6a6544e5
MZ
1579 if (!domain->parent)
1580 return -ENOSYS;
36d72731 1581
6a6544e5
MZ
1582 return irq_domain_alloc_irqs_hierarchy(domain->parent, irq_base,
1583 nr_irqs, arg);
36d72731 1584}
52b2a05f 1585EXPORT_SYMBOL_GPL(irq_domain_alloc_irqs_parent);
36d72731
JL
1586
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{
6a6544e5
MZ
1598 if (!domain->parent)
1599 return;
1600
1601 irq_domain_free_irqs_hierarchy(domain->parent, irq_base, nr_irqs);
36d72731 1602}
52b2a05f 1603EXPORT_SYMBOL_GPL(irq_domain_free_irqs_parent);
36d72731 1604
08d85f3e
MZ
1605static 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
702cb0a0 1617static int __irq_domain_activate_irq(struct irq_data *irqd, bool reserve)
bb9b428a
TG
1618{
1619 int ret = 0;
1620
1621 if (irqd && irqd->domain) {
1622 struct irq_domain *domain = irqd->domain;
1623
1624 if (irqd->parent_data)
42e1cc2d 1625 ret = __irq_domain_activate_irq(irqd->parent_data,
702cb0a0 1626 reserve);
bb9b428a 1627 if (!ret && domain->ops->activate) {
702cb0a0 1628 ret = domain->ops->activate(domain, irqd, reserve);
bb9b428a
TG
1629 /* 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
f8264e34
JL
1637/**
1638 * irq_domain_activate_irq - Call domain_ops->activate recursively to activate
1639 * interrupt
702cb0a0
TG
1640 * @irq_data: Outermost irq_data associated with interrupt
1641 * @reserve: If set only reserve an interrupt vector instead of assigning one
f8264e34
JL
1642 *
1643 * This is the second step to call domain_ops->activate to program interrupt
1644 * controllers, so the interrupt could actually get delivered.
1645 */
702cb0a0 1646int irq_domain_activate_irq(struct irq_data *irq_data, bool reserve)
f8264e34 1647{
bb9b428a
TG
1648 int ret = 0;
1649
1650 if (!irqd_is_activated(irq_data))
702cb0a0 1651 ret = __irq_domain_activate_irq(irq_data, reserve);
bb9b428a 1652 if (!ret)
08d85f3e 1653 irqd_set_activated(irq_data);
bb9b428a 1654 return ret;
f8264e34
JL
1655}
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{
08d85f3e
MZ
1667 if (irqd_is_activated(irq_data)) {
1668 __irq_domain_deactivate_irq(irq_data);
1669 irqd_clr_activated(irq_data);
f8264e34
JL
1670 }
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}
631a9639
EA
1679
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}
f8264e34
JL
1693#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}
a4289dc2 1706EXPORT_SYMBOL_GPL(irq_domain_get_irq_data);
f8264e34 1707
5f22f5c6
SA
1708/**
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
f8264e34
JL
1729static void irq_domain_check_hierarchy(struct irq_domain *domain)
1730{
1731}
1732#endif /* CONFIG_IRQ_DOMAIN_HIERARCHY */
087cdfb6
TG
1733
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);
c3e7239a
TG
1745 if (d->ops && d->ops->debug_show)
1746 d->ops->debug_show(m, d, NULL, ind + 1);
087cdfb6
TG
1747#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}
0b24a0bb 1768DEFINE_SHOW_ATTRIBUTE(irq_domain_debug);
087cdfb6
TG
1769
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,
0b24a0bb 1775 &irq_domain_debug_fops);
087cdfb6
TG
1776}
1777
1778static void debugfs_remove_domain_dir(struct irq_domain *d)
1779{
f610c9d6 1780 debugfs_remove(d->debugfs_file);
513145ea 1781 d->debugfs_file = NULL;
087cdfb6
TG
1782}
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);
087cdfb6 1789
0b24a0bb
AS
1790 debugfs_create_file("default", 0444, domain_dir, NULL,
1791 &irq_domain_debug_fops);
087cdfb6
TG
1792 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