drm/panthor: Don't add write fences to the shared BOs
[drm/drm-misc.git] / kernel / irq / irqdesc.c
blob1dee88ba0ae449a7c83d5cfa8fe6b69f83b3b6d6
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (C) 1992, 1998-2006 Linus Torvalds, Ingo Molnar
4 * Copyright (C) 2005-2006, Thomas Gleixner, Russell King
6 * This file contains the interrupt descriptor management code. Detailed
7 * information is available in Documentation/core-api/genericirq.rst
9 */
10 #include <linux/irq.h>
11 #include <linux/slab.h>
12 #include <linux/export.h>
13 #include <linux/interrupt.h>
14 #include <linux/kernel_stat.h>
15 #include <linux/maple_tree.h>
16 #include <linux/irqdomain.h>
17 #include <linux/sysfs.h>
19 #include "internals.h"
22 * lockdep: we want to handle all irq_desc locks as a single lock-class:
24 static struct lock_class_key irq_desc_lock_class;
26 #if defined(CONFIG_SMP)
27 static int __init irq_affinity_setup(char *str)
29 alloc_bootmem_cpumask_var(&irq_default_affinity);
30 cpulist_parse(str, irq_default_affinity);
32 * Set at least the boot cpu. We don't want to end up with
33 * bugreports caused by random commandline masks
35 cpumask_set_cpu(smp_processor_id(), irq_default_affinity);
36 return 1;
38 __setup("irqaffinity=", irq_affinity_setup);
40 static void __init init_irq_default_affinity(void)
42 if (!cpumask_available(irq_default_affinity))
43 zalloc_cpumask_var(&irq_default_affinity, GFP_NOWAIT);
44 if (cpumask_empty(irq_default_affinity))
45 cpumask_setall(irq_default_affinity);
47 #else
48 static void __init init_irq_default_affinity(void)
51 #endif
53 #ifdef CONFIG_SMP
54 static int alloc_masks(struct irq_desc *desc, int node)
56 if (!zalloc_cpumask_var_node(&desc->irq_common_data.affinity,
57 GFP_KERNEL, node))
58 return -ENOMEM;
60 #ifdef CONFIG_GENERIC_IRQ_EFFECTIVE_AFF_MASK
61 if (!zalloc_cpumask_var_node(&desc->irq_common_data.effective_affinity,
62 GFP_KERNEL, node)) {
63 free_cpumask_var(desc->irq_common_data.affinity);
64 return -ENOMEM;
66 #endif
68 #ifdef CONFIG_GENERIC_PENDING_IRQ
69 if (!zalloc_cpumask_var_node(&desc->pending_mask, GFP_KERNEL, node)) {
70 #ifdef CONFIG_GENERIC_IRQ_EFFECTIVE_AFF_MASK
71 free_cpumask_var(desc->irq_common_data.effective_affinity);
72 #endif
73 free_cpumask_var(desc->irq_common_data.affinity);
74 return -ENOMEM;
76 #endif
77 return 0;
80 static void desc_smp_init(struct irq_desc *desc, int node,
81 const struct cpumask *affinity)
83 if (!affinity)
84 affinity = irq_default_affinity;
85 cpumask_copy(desc->irq_common_data.affinity, affinity);
87 #ifdef CONFIG_GENERIC_PENDING_IRQ
88 cpumask_clear(desc->pending_mask);
89 #endif
90 #ifdef CONFIG_NUMA
91 desc->irq_common_data.node = node;
92 #endif
95 static void free_masks(struct irq_desc *desc)
97 #ifdef CONFIG_GENERIC_PENDING_IRQ
98 free_cpumask_var(desc->pending_mask);
99 #endif
100 free_cpumask_var(desc->irq_common_data.affinity);
101 #ifdef CONFIG_GENERIC_IRQ_EFFECTIVE_AFF_MASK
102 free_cpumask_var(desc->irq_common_data.effective_affinity);
103 #endif
106 #else
107 static inline int
108 alloc_masks(struct irq_desc *desc, int node) { return 0; }
109 static inline void
110 desc_smp_init(struct irq_desc *desc, int node, const struct cpumask *affinity) { }
111 static inline void free_masks(struct irq_desc *desc) { }
112 #endif
114 static void desc_set_defaults(unsigned int irq, struct irq_desc *desc, int node,
115 const struct cpumask *affinity, struct module *owner)
117 int cpu;
119 desc->irq_common_data.handler_data = NULL;
120 desc->irq_common_data.msi_desc = NULL;
122 desc->irq_data.common = &desc->irq_common_data;
123 desc->irq_data.irq = irq;
124 desc->irq_data.chip = &no_irq_chip;
125 desc->irq_data.chip_data = NULL;
126 irq_settings_clr_and_set(desc, ~0, _IRQ_DEFAULT_INIT_FLAGS);
127 irqd_set(&desc->irq_data, IRQD_IRQ_DISABLED);
128 irqd_set(&desc->irq_data, IRQD_IRQ_MASKED);
129 desc->handle_irq = handle_bad_irq;
130 desc->depth = 1;
131 desc->irq_count = 0;
132 desc->irqs_unhandled = 0;
133 desc->tot_count = 0;
134 desc->name = NULL;
135 desc->owner = owner;
136 for_each_possible_cpu(cpu)
137 *per_cpu_ptr(desc->kstat_irqs, cpu) = (struct irqstat) { };
138 desc_smp_init(desc, node, affinity);
141 int nr_irqs = NR_IRQS;
142 EXPORT_SYMBOL_GPL(nr_irqs);
144 static DEFINE_MUTEX(sparse_irq_lock);
145 static struct maple_tree sparse_irqs = MTREE_INIT_EXT(sparse_irqs,
146 MT_FLAGS_ALLOC_RANGE |
147 MT_FLAGS_LOCK_EXTERN |
148 MT_FLAGS_USE_RCU,
149 sparse_irq_lock);
151 static int irq_find_free_area(unsigned int from, unsigned int cnt)
153 MA_STATE(mas, &sparse_irqs, 0, 0);
155 if (mas_empty_area(&mas, from, MAX_SPARSE_IRQS, cnt))
156 return -ENOSPC;
157 return mas.index;
160 static unsigned int irq_find_at_or_after(unsigned int offset)
162 unsigned long index = offset;
163 struct irq_desc *desc;
165 guard(rcu)();
166 desc = mt_find(&sparse_irqs, &index, nr_irqs);
168 return desc ? irq_desc_get_irq(desc) : nr_irqs;
171 static void irq_insert_desc(unsigned int irq, struct irq_desc *desc)
173 MA_STATE(mas, &sparse_irqs, irq, irq);
174 WARN_ON(mas_store_gfp(&mas, desc, GFP_KERNEL) != 0);
177 static void delete_irq_desc(unsigned int irq)
179 MA_STATE(mas, &sparse_irqs, irq, irq);
180 mas_erase(&mas);
183 #ifdef CONFIG_SPARSE_IRQ
184 static const struct kobj_type irq_kobj_type;
185 #endif
187 static int init_desc(struct irq_desc *desc, int irq, int node,
188 unsigned int flags,
189 const struct cpumask *affinity,
190 struct module *owner)
192 desc->kstat_irqs = alloc_percpu(struct irqstat);
193 if (!desc->kstat_irqs)
194 return -ENOMEM;
196 if (alloc_masks(desc, node)) {
197 free_percpu(desc->kstat_irqs);
198 return -ENOMEM;
201 raw_spin_lock_init(&desc->lock);
202 lockdep_set_class(&desc->lock, &irq_desc_lock_class);
203 mutex_init(&desc->request_mutex);
204 init_waitqueue_head(&desc->wait_for_threads);
205 desc_set_defaults(irq, desc, node, affinity, owner);
206 irqd_set(&desc->irq_data, flags);
207 irq_resend_init(desc);
208 #ifdef CONFIG_SPARSE_IRQ
209 kobject_init(&desc->kobj, &irq_kobj_type);
210 init_rcu_head(&desc->rcu);
211 #endif
213 return 0;
216 #ifdef CONFIG_SPARSE_IRQ
218 static void irq_kobj_release(struct kobject *kobj);
220 #ifdef CONFIG_SYSFS
221 static struct kobject *irq_kobj_base;
223 #define IRQ_ATTR_RO(_name) \
224 static struct kobj_attribute _name##_attr = __ATTR_RO(_name)
226 static ssize_t per_cpu_count_show(struct kobject *kobj,
227 struct kobj_attribute *attr, char *buf)
229 struct irq_desc *desc = container_of(kobj, struct irq_desc, kobj);
230 ssize_t ret = 0;
231 char *p = "";
232 int cpu;
234 for_each_possible_cpu(cpu) {
235 unsigned int c = irq_desc_kstat_cpu(desc, cpu);
237 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "%s%u", p, c);
238 p = ",";
241 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "\n");
242 return ret;
244 IRQ_ATTR_RO(per_cpu_count);
246 static ssize_t chip_name_show(struct kobject *kobj,
247 struct kobj_attribute *attr, char *buf)
249 struct irq_desc *desc = container_of(kobj, struct irq_desc, kobj);
250 ssize_t ret = 0;
252 raw_spin_lock_irq(&desc->lock);
253 if (desc->irq_data.chip && desc->irq_data.chip->name) {
254 ret = scnprintf(buf, PAGE_SIZE, "%s\n",
255 desc->irq_data.chip->name);
257 raw_spin_unlock_irq(&desc->lock);
259 return ret;
261 IRQ_ATTR_RO(chip_name);
263 static ssize_t hwirq_show(struct kobject *kobj,
264 struct kobj_attribute *attr, char *buf)
266 struct irq_desc *desc = container_of(kobj, struct irq_desc, kobj);
267 ssize_t ret = 0;
269 raw_spin_lock_irq(&desc->lock);
270 if (desc->irq_data.domain)
271 ret = sprintf(buf, "%lu\n", desc->irq_data.hwirq);
272 raw_spin_unlock_irq(&desc->lock);
274 return ret;
276 IRQ_ATTR_RO(hwirq);
278 static ssize_t type_show(struct kobject *kobj,
279 struct kobj_attribute *attr, char *buf)
281 struct irq_desc *desc = container_of(kobj, struct irq_desc, kobj);
282 ssize_t ret = 0;
284 raw_spin_lock_irq(&desc->lock);
285 ret = sprintf(buf, "%s\n",
286 irqd_is_level_type(&desc->irq_data) ? "level" : "edge");
287 raw_spin_unlock_irq(&desc->lock);
289 return ret;
292 IRQ_ATTR_RO(type);
294 static ssize_t wakeup_show(struct kobject *kobj,
295 struct kobj_attribute *attr, char *buf)
297 struct irq_desc *desc = container_of(kobj, struct irq_desc, kobj);
298 ssize_t ret = 0;
300 raw_spin_lock_irq(&desc->lock);
301 ret = sprintf(buf, "%s\n",
302 irqd_is_wakeup_set(&desc->irq_data) ? "enabled" : "disabled");
303 raw_spin_unlock_irq(&desc->lock);
305 return ret;
308 IRQ_ATTR_RO(wakeup);
310 static ssize_t name_show(struct kobject *kobj,
311 struct kobj_attribute *attr, char *buf)
313 struct irq_desc *desc = container_of(kobj, struct irq_desc, kobj);
314 ssize_t ret = 0;
316 raw_spin_lock_irq(&desc->lock);
317 if (desc->name)
318 ret = scnprintf(buf, PAGE_SIZE, "%s\n", desc->name);
319 raw_spin_unlock_irq(&desc->lock);
321 return ret;
323 IRQ_ATTR_RO(name);
325 static ssize_t actions_show(struct kobject *kobj,
326 struct kobj_attribute *attr, char *buf)
328 struct irq_desc *desc = container_of(kobj, struct irq_desc, kobj);
329 struct irqaction *action;
330 ssize_t ret = 0;
331 char *p = "";
333 raw_spin_lock_irq(&desc->lock);
334 for_each_action_of_desc(desc, action) {
335 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "%s%s",
336 p, action->name);
337 p = ",";
339 raw_spin_unlock_irq(&desc->lock);
341 if (ret)
342 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "\n");
344 return ret;
346 IRQ_ATTR_RO(actions);
348 static struct attribute *irq_attrs[] = {
349 &per_cpu_count_attr.attr,
350 &chip_name_attr.attr,
351 &hwirq_attr.attr,
352 &type_attr.attr,
353 &wakeup_attr.attr,
354 &name_attr.attr,
355 &actions_attr.attr,
356 NULL
358 ATTRIBUTE_GROUPS(irq);
360 static const struct kobj_type irq_kobj_type = {
361 .release = irq_kobj_release,
362 .sysfs_ops = &kobj_sysfs_ops,
363 .default_groups = irq_groups,
366 static void irq_sysfs_add(int irq, struct irq_desc *desc)
368 if (irq_kobj_base) {
370 * Continue even in case of failure as this is nothing
371 * crucial and failures in the late irq_sysfs_init()
372 * cannot be rolled back.
374 if (kobject_add(&desc->kobj, irq_kobj_base, "%d", irq))
375 pr_warn("Failed to add kobject for irq %d\n", irq);
376 else
377 desc->istate |= IRQS_SYSFS;
381 static void irq_sysfs_del(struct irq_desc *desc)
384 * Only invoke kobject_del() when kobject_add() was successfully
385 * invoked for the descriptor. This covers both early boot, where
386 * sysfs is not initialized yet, and the case of a failed
387 * kobject_add() invocation.
389 if (desc->istate & IRQS_SYSFS)
390 kobject_del(&desc->kobj);
393 static int __init irq_sysfs_init(void)
395 struct irq_desc *desc;
396 int irq;
398 /* Prevent concurrent irq alloc/free */
399 irq_lock_sparse();
401 irq_kobj_base = kobject_create_and_add("irq", kernel_kobj);
402 if (!irq_kobj_base) {
403 irq_unlock_sparse();
404 return -ENOMEM;
407 /* Add the already allocated interrupts */
408 for_each_irq_desc(irq, desc)
409 irq_sysfs_add(irq, desc);
410 irq_unlock_sparse();
412 return 0;
414 postcore_initcall(irq_sysfs_init);
416 #else /* !CONFIG_SYSFS */
418 static const struct kobj_type irq_kobj_type = {
419 .release = irq_kobj_release,
422 static void irq_sysfs_add(int irq, struct irq_desc *desc) {}
423 static void irq_sysfs_del(struct irq_desc *desc) {}
425 #endif /* CONFIG_SYSFS */
427 struct irq_desc *irq_to_desc(unsigned int irq)
429 return mtree_load(&sparse_irqs, irq);
431 #ifdef CONFIG_KVM_BOOK3S_64_HV_MODULE
432 EXPORT_SYMBOL_GPL(irq_to_desc);
433 #endif
435 void irq_lock_sparse(void)
437 mutex_lock(&sparse_irq_lock);
440 void irq_unlock_sparse(void)
442 mutex_unlock(&sparse_irq_lock);
445 static struct irq_desc *alloc_desc(int irq, int node, unsigned int flags,
446 const struct cpumask *affinity,
447 struct module *owner)
449 struct irq_desc *desc;
450 int ret;
452 desc = kzalloc_node(sizeof(*desc), GFP_KERNEL, node);
453 if (!desc)
454 return NULL;
456 ret = init_desc(desc, irq, node, flags, affinity, owner);
457 if (unlikely(ret)) {
458 kfree(desc);
459 return NULL;
462 return desc;
465 static void irq_kobj_release(struct kobject *kobj)
467 struct irq_desc *desc = container_of(kobj, struct irq_desc, kobj);
469 free_masks(desc);
470 free_percpu(desc->kstat_irqs);
471 kfree(desc);
474 static void delayed_free_desc(struct rcu_head *rhp)
476 struct irq_desc *desc = container_of(rhp, struct irq_desc, rcu);
478 kobject_put(&desc->kobj);
481 static void free_desc(unsigned int irq)
483 struct irq_desc *desc = irq_to_desc(irq);
485 irq_remove_debugfs_entry(desc);
486 unregister_irq_proc(irq, desc);
489 * sparse_irq_lock protects also show_interrupts() and
490 * kstat_irq_usr(). Once we deleted the descriptor from the
491 * sparse tree we can free it. Access in proc will fail to
492 * lookup the descriptor.
494 * The sysfs entry must be serialized against a concurrent
495 * irq_sysfs_init() as well.
497 irq_sysfs_del(desc);
498 delete_irq_desc(irq);
501 * We free the descriptor, masks and stat fields via RCU. That
502 * allows demultiplex interrupts to do rcu based management of
503 * the child interrupts.
504 * This also allows us to use rcu in kstat_irqs_usr().
506 call_rcu(&desc->rcu, delayed_free_desc);
509 static int alloc_descs(unsigned int start, unsigned int cnt, int node,
510 const struct irq_affinity_desc *affinity,
511 struct module *owner)
513 struct irq_desc *desc;
514 int i;
516 /* Validate affinity mask(s) */
517 if (affinity) {
518 for (i = 0; i < cnt; i++) {
519 if (cpumask_empty(&affinity[i].mask))
520 return -EINVAL;
524 for (i = 0; i < cnt; i++) {
525 const struct cpumask *mask = NULL;
526 unsigned int flags = 0;
528 if (affinity) {
529 if (affinity->is_managed) {
530 flags = IRQD_AFFINITY_MANAGED |
531 IRQD_MANAGED_SHUTDOWN;
533 flags |= IRQD_AFFINITY_SET;
534 mask = &affinity->mask;
535 node = cpu_to_node(cpumask_first(mask));
536 affinity++;
539 desc = alloc_desc(start + i, node, flags, mask, owner);
540 if (!desc)
541 goto err;
542 irq_insert_desc(start + i, desc);
543 irq_sysfs_add(start + i, desc);
544 irq_add_debugfs_entry(start + i, desc);
546 return start;
548 err:
549 for (i--; i >= 0; i--)
550 free_desc(start + i);
551 return -ENOMEM;
554 static int irq_expand_nr_irqs(unsigned int nr)
556 if (nr > MAX_SPARSE_IRQS)
557 return -ENOMEM;
558 nr_irqs = nr;
559 return 0;
562 int __init early_irq_init(void)
564 int i, initcnt, node = first_online_node;
565 struct irq_desc *desc;
567 init_irq_default_affinity();
569 /* Let arch update nr_irqs and return the nr of preallocated irqs */
570 initcnt = arch_probe_nr_irqs();
571 printk(KERN_INFO "NR_IRQS: %d, nr_irqs: %d, preallocated irqs: %d\n",
572 NR_IRQS, nr_irqs, initcnt);
574 if (WARN_ON(nr_irqs > MAX_SPARSE_IRQS))
575 nr_irqs = MAX_SPARSE_IRQS;
577 if (WARN_ON(initcnt > MAX_SPARSE_IRQS))
578 initcnt = MAX_SPARSE_IRQS;
580 if (initcnt > nr_irqs)
581 nr_irqs = initcnt;
583 for (i = 0; i < initcnt; i++) {
584 desc = alloc_desc(i, node, 0, NULL, NULL);
585 irq_insert_desc(i, desc);
587 return arch_early_irq_init();
590 #else /* !CONFIG_SPARSE_IRQ */
592 struct irq_desc irq_desc[NR_IRQS] __cacheline_aligned_in_smp = {
593 [0 ... NR_IRQS-1] = {
594 .handle_irq = handle_bad_irq,
595 .depth = 1,
596 .lock = __RAW_SPIN_LOCK_UNLOCKED(irq_desc->lock),
600 int __init early_irq_init(void)
602 int count, i, node = first_online_node;
603 int ret;
605 init_irq_default_affinity();
607 printk(KERN_INFO "NR_IRQS: %d\n", NR_IRQS);
609 count = ARRAY_SIZE(irq_desc);
611 for (i = 0; i < count; i++) {
612 ret = init_desc(irq_desc + i, i, node, 0, NULL, NULL);
613 if (unlikely(ret))
614 goto __free_desc_res;
617 return arch_early_irq_init();
619 __free_desc_res:
620 while (--i >= 0) {
621 free_masks(irq_desc + i);
622 free_percpu(irq_desc[i].kstat_irqs);
625 return ret;
628 struct irq_desc *irq_to_desc(unsigned int irq)
630 return (irq < NR_IRQS) ? irq_desc + irq : NULL;
632 EXPORT_SYMBOL(irq_to_desc);
634 static void free_desc(unsigned int irq)
636 struct irq_desc *desc = irq_to_desc(irq);
637 unsigned long flags;
639 raw_spin_lock_irqsave(&desc->lock, flags);
640 desc_set_defaults(irq, desc, irq_desc_get_node(desc), NULL, NULL);
641 raw_spin_unlock_irqrestore(&desc->lock, flags);
642 delete_irq_desc(irq);
645 static inline int alloc_descs(unsigned int start, unsigned int cnt, int node,
646 const struct irq_affinity_desc *affinity,
647 struct module *owner)
649 u32 i;
651 for (i = 0; i < cnt; i++) {
652 struct irq_desc *desc = irq_to_desc(start + i);
654 desc->owner = owner;
655 irq_insert_desc(start + i, desc);
657 return start;
660 static int irq_expand_nr_irqs(unsigned int nr)
662 return -ENOMEM;
665 void irq_mark_irq(unsigned int irq)
667 mutex_lock(&sparse_irq_lock);
668 irq_insert_desc(irq, irq_desc + irq);
669 mutex_unlock(&sparse_irq_lock);
672 #ifdef CONFIG_GENERIC_IRQ_LEGACY
673 void irq_init_desc(unsigned int irq)
675 free_desc(irq);
677 #endif
679 #endif /* !CONFIG_SPARSE_IRQ */
681 int handle_irq_desc(struct irq_desc *desc)
683 struct irq_data *data;
685 if (!desc)
686 return -EINVAL;
688 data = irq_desc_get_irq_data(desc);
689 if (WARN_ON_ONCE(!in_hardirq() && handle_enforce_irqctx(data)))
690 return -EPERM;
692 generic_handle_irq_desc(desc);
693 return 0;
697 * generic_handle_irq - Invoke the handler for a particular irq
698 * @irq: The irq number to handle
700 * Returns: 0 on success, or -EINVAL if conversion has failed
702 * This function must be called from an IRQ context with irq regs
703 * initialized.
705 int generic_handle_irq(unsigned int irq)
707 return handle_irq_desc(irq_to_desc(irq));
709 EXPORT_SYMBOL_GPL(generic_handle_irq);
712 * generic_handle_irq_safe - Invoke the handler for a particular irq from any
713 * context.
714 * @irq: The irq number to handle
716 * Returns: 0 on success, a negative value on error.
718 * This function can be called from any context (IRQ or process context). It
719 * will report an error if not invoked from IRQ context and the irq has been
720 * marked to enforce IRQ-context only.
722 int generic_handle_irq_safe(unsigned int irq)
724 unsigned long flags;
725 int ret;
727 local_irq_save(flags);
728 ret = handle_irq_desc(irq_to_desc(irq));
729 local_irq_restore(flags);
730 return ret;
732 EXPORT_SYMBOL_GPL(generic_handle_irq_safe);
734 #ifdef CONFIG_IRQ_DOMAIN
736 * generic_handle_domain_irq - Invoke the handler for a HW irq belonging
737 * to a domain.
738 * @domain: The domain where to perform the lookup
739 * @hwirq: The HW irq number to convert to a logical one
741 * Returns: 0 on success, or -EINVAL if conversion has failed
743 * This function must be called from an IRQ context with irq regs
744 * initialized.
746 int generic_handle_domain_irq(struct irq_domain *domain, unsigned int hwirq)
748 return handle_irq_desc(irq_resolve_mapping(domain, hwirq));
750 EXPORT_SYMBOL_GPL(generic_handle_domain_irq);
753 * generic_handle_irq_safe - Invoke the handler for a HW irq belonging
754 * to a domain from any context.
755 * @domain: The domain where to perform the lookup
756 * @hwirq: The HW irq number to convert to a logical one
758 * Returns: 0 on success, a negative value on error.
760 * This function can be called from any context (IRQ or process
761 * context). If the interrupt is marked as 'enforce IRQ-context only' then
762 * the function must be invoked from hard interrupt context.
764 int generic_handle_domain_irq_safe(struct irq_domain *domain, unsigned int hwirq)
766 unsigned long flags;
767 int ret;
769 local_irq_save(flags);
770 ret = handle_irq_desc(irq_resolve_mapping(domain, hwirq));
771 local_irq_restore(flags);
772 return ret;
774 EXPORT_SYMBOL_GPL(generic_handle_domain_irq_safe);
777 * generic_handle_domain_nmi - Invoke the handler for a HW nmi belonging
778 * to a domain.
779 * @domain: The domain where to perform the lookup
780 * @hwirq: The HW irq number to convert to a logical one
782 * Returns: 0 on success, or -EINVAL if conversion has failed
784 * This function must be called from an NMI context with irq regs
785 * initialized.
787 int generic_handle_domain_nmi(struct irq_domain *domain, unsigned int hwirq)
789 WARN_ON_ONCE(!in_nmi());
790 return handle_irq_desc(irq_resolve_mapping(domain, hwirq));
792 #endif
794 /* Dynamic interrupt handling */
797 * irq_free_descs - free irq descriptors
798 * @from: Start of descriptor range
799 * @cnt: Number of consecutive irqs to free
801 void irq_free_descs(unsigned int from, unsigned int cnt)
803 int i;
805 if (from >= nr_irqs || (from + cnt) > nr_irqs)
806 return;
808 mutex_lock(&sparse_irq_lock);
809 for (i = 0; i < cnt; i++)
810 free_desc(from + i);
812 mutex_unlock(&sparse_irq_lock);
814 EXPORT_SYMBOL_GPL(irq_free_descs);
817 * __irq_alloc_descs - allocate and initialize a range of irq descriptors
818 * @irq: Allocate for specific irq number if irq >= 0
819 * @from: Start the search from this irq number
820 * @cnt: Number of consecutive irqs to allocate.
821 * @node: Preferred node on which the irq descriptor should be allocated
822 * @owner: Owning module (can be NULL)
823 * @affinity: Optional pointer to an affinity mask array of size @cnt which
824 * hints where the irq descriptors should be allocated and which
825 * default affinities to use
827 * Returns the first irq number or error code
829 int __ref
830 __irq_alloc_descs(int irq, unsigned int from, unsigned int cnt, int node,
831 struct module *owner, const struct irq_affinity_desc *affinity)
833 int start, ret;
835 if (!cnt)
836 return -EINVAL;
838 if (irq >= 0) {
839 if (from > irq)
840 return -EINVAL;
841 from = irq;
842 } else {
844 * For interrupts which are freely allocated the
845 * architecture can force a lower bound to the @from
846 * argument. x86 uses this to exclude the GSI space.
848 from = arch_dynirq_lower_bound(from);
851 mutex_lock(&sparse_irq_lock);
853 start = irq_find_free_area(from, cnt);
854 ret = -EEXIST;
855 if (irq >=0 && start != irq)
856 goto unlock;
858 if (start + cnt > nr_irqs) {
859 ret = irq_expand_nr_irqs(start + cnt);
860 if (ret)
861 goto unlock;
863 ret = alloc_descs(start, cnt, node, affinity, owner);
864 unlock:
865 mutex_unlock(&sparse_irq_lock);
866 return ret;
868 EXPORT_SYMBOL_GPL(__irq_alloc_descs);
871 * irq_get_next_irq - get next allocated irq number
872 * @offset: where to start the search
874 * Returns next irq number after offset or nr_irqs if none is found.
876 unsigned int irq_get_next_irq(unsigned int offset)
878 return irq_find_at_or_after(offset);
881 struct irq_desc *
882 __irq_get_desc_lock(unsigned int irq, unsigned long *flags, bool bus,
883 unsigned int check)
885 struct irq_desc *desc = irq_to_desc(irq);
887 if (desc) {
888 if (check & _IRQ_DESC_CHECK) {
889 if ((check & _IRQ_DESC_PERCPU) &&
890 !irq_settings_is_per_cpu_devid(desc))
891 return NULL;
893 if (!(check & _IRQ_DESC_PERCPU) &&
894 irq_settings_is_per_cpu_devid(desc))
895 return NULL;
898 if (bus)
899 chip_bus_lock(desc);
900 raw_spin_lock_irqsave(&desc->lock, *flags);
902 return desc;
905 void __irq_put_desc_unlock(struct irq_desc *desc, unsigned long flags, bool bus)
906 __releases(&desc->lock)
908 raw_spin_unlock_irqrestore(&desc->lock, flags);
909 if (bus)
910 chip_bus_sync_unlock(desc);
913 int irq_set_percpu_devid_partition(unsigned int irq,
914 const struct cpumask *affinity)
916 struct irq_desc *desc = irq_to_desc(irq);
918 if (!desc || desc->percpu_enabled)
919 return -EINVAL;
921 desc->percpu_enabled = kzalloc(sizeof(*desc->percpu_enabled), GFP_KERNEL);
923 if (!desc->percpu_enabled)
924 return -ENOMEM;
926 desc->percpu_affinity = affinity ? : cpu_possible_mask;
928 irq_set_percpu_devid_flags(irq);
929 return 0;
932 int irq_set_percpu_devid(unsigned int irq)
934 return irq_set_percpu_devid_partition(irq, NULL);
937 int irq_get_percpu_devid_partition(unsigned int irq, struct cpumask *affinity)
939 struct irq_desc *desc = irq_to_desc(irq);
941 if (!desc || !desc->percpu_enabled)
942 return -EINVAL;
944 if (affinity)
945 cpumask_copy(affinity, desc->percpu_affinity);
947 return 0;
949 EXPORT_SYMBOL_GPL(irq_get_percpu_devid_partition);
951 void kstat_incr_irq_this_cpu(unsigned int irq)
953 kstat_incr_irqs_this_cpu(irq_to_desc(irq));
957 * kstat_irqs_cpu - Get the statistics for an interrupt on a cpu
958 * @irq: The interrupt number
959 * @cpu: The cpu number
961 * Returns the sum of interrupt counts on @cpu since boot for
962 * @irq. The caller must ensure that the interrupt is not removed
963 * concurrently.
965 unsigned int kstat_irqs_cpu(unsigned int irq, int cpu)
967 struct irq_desc *desc = irq_to_desc(irq);
969 return desc && desc->kstat_irqs ? per_cpu(desc->kstat_irqs->cnt, cpu) : 0;
972 unsigned int kstat_irqs_desc(struct irq_desc *desc, const struct cpumask *cpumask)
974 unsigned int sum = 0;
975 int cpu;
977 if (!irq_settings_is_per_cpu_devid(desc) &&
978 !irq_settings_is_per_cpu(desc) &&
979 !irq_is_nmi(desc))
980 return data_race(desc->tot_count);
982 for_each_cpu(cpu, cpumask)
983 sum += data_race(per_cpu(desc->kstat_irqs->cnt, cpu));
984 return sum;
987 static unsigned int kstat_irqs(unsigned int irq)
989 struct irq_desc *desc = irq_to_desc(irq);
991 if (!desc || !desc->kstat_irqs)
992 return 0;
993 return kstat_irqs_desc(desc, cpu_possible_mask);
996 #ifdef CONFIG_GENERIC_IRQ_STAT_SNAPSHOT
998 void kstat_snapshot_irqs(void)
1000 struct irq_desc *desc;
1001 unsigned int irq;
1003 for_each_irq_desc(irq, desc) {
1004 if (!desc->kstat_irqs)
1005 continue;
1006 this_cpu_write(desc->kstat_irqs->ref, this_cpu_read(desc->kstat_irqs->cnt));
1010 unsigned int kstat_get_irq_since_snapshot(unsigned int irq)
1012 struct irq_desc *desc = irq_to_desc(irq);
1014 if (!desc || !desc->kstat_irqs)
1015 return 0;
1016 return this_cpu_read(desc->kstat_irqs->cnt) - this_cpu_read(desc->kstat_irqs->ref);
1019 #endif
1022 * kstat_irqs_usr - Get the statistics for an interrupt from thread context
1023 * @irq: The interrupt number
1025 * Returns the sum of interrupt counts on all cpus since boot for @irq.
1027 * It uses rcu to protect the access since a concurrent removal of an
1028 * interrupt descriptor is observing an rcu grace period before
1029 * delayed_free_desc()/irq_kobj_release().
1031 unsigned int kstat_irqs_usr(unsigned int irq)
1033 unsigned int sum;
1035 rcu_read_lock();
1036 sum = kstat_irqs(irq);
1037 rcu_read_unlock();
1038 return sum;
1041 #ifdef CONFIG_LOCKDEP
1042 void __irq_set_lockdep_class(unsigned int irq, struct lock_class_key *lock_class,
1043 struct lock_class_key *request_class)
1045 struct irq_desc *desc = irq_to_desc(irq);
1047 if (desc) {
1048 lockdep_set_class(&desc->lock, lock_class);
1049 lockdep_set_class(&desc->request_mutex, request_class);
1052 EXPORT_SYMBOL_GPL(__irq_set_lockdep_class);
1053 #endif