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