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