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