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