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