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