2 * Copyright (C) 1992, 1998-2006 Linus Torvalds, Ingo Molnar
3 * Copyright (C) 2005-2006, Thomas Gleixner, Russell King
5 * This file contains the interrupt descriptor management code
7 * Detailed information is available in Documentation/core-api/genericirq.rst
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/radix-tree.h>
16 #include <linux/bitmap.h>
17 #include <linux/irqdomain.h>
18 #include <linux/sysfs.h>
20 #include "internals.h"
23 * lockdep: we want to handle all irq_desc locks as a single lock-class:
25 static struct lock_class_key irq_desc_lock_class;
27 #if defined(CONFIG_SMP)
28 static int __init irq_affinity_setup(char *str)
30 zalloc_cpumask_var(&irq_default_affinity, GFP_NOWAIT);
31 cpulist_parse(str, irq_default_affinity);
33 * Set at least the boot cpu. We don't want to end up with
34 * bugreports caused by random comandline masks
36 cpumask_set_cpu(smp_processor_id(), irq_default_affinity);
39 __setup("irqaffinity=", irq_affinity_setup);
41 static void __init init_irq_default_affinity(void)
43 #ifdef CONFIG_CPUMASK_OFFSTACK
44 if (!irq_default_affinity)
45 zalloc_cpumask_var(&irq_default_affinity, GFP_NOWAIT);
47 if (cpumask_empty(irq_default_affinity))
48 cpumask_setall(irq_default_affinity);
51 static void __init init_irq_default_affinity(void)
57 static int alloc_masks(struct irq_desc *desc, int node)
59 if (!zalloc_cpumask_var_node(&desc->irq_common_data.affinity,
63 #ifdef CONFIG_GENERIC_IRQ_EFFECTIVE_AFF_MASK
64 if (!zalloc_cpumask_var_node(&desc->irq_common_data.effective_affinity,
66 free_cpumask_var(desc->irq_common_data.affinity);
71 #ifdef CONFIG_GENERIC_PENDING_IRQ
72 if (!zalloc_cpumask_var_node(&desc->pending_mask, GFP_KERNEL, node)) {
73 #ifdef CONFIG_GENERIC_IRQ_EFFECTIVE_AFF_MASK
74 free_cpumask_var(desc->irq_common_data.effective_affinity);
76 free_cpumask_var(desc->irq_common_data.affinity);
83 static void desc_smp_init(struct irq_desc *desc, int node,
84 const struct cpumask *affinity)
87 affinity = irq_default_affinity;
88 cpumask_copy(desc->irq_common_data.affinity, affinity);
90 #ifdef CONFIG_GENERIC_PENDING_IRQ
91 cpumask_clear(desc->pending_mask);
94 desc->irq_common_data.node = node;
100 alloc_masks(struct irq_desc *desc, int node) { return 0; }
102 desc_smp_init(struct irq_desc *desc, int node, const struct cpumask *affinity) { }
105 static void desc_set_defaults(unsigned int irq, struct irq_desc *desc, int node,
106 const struct cpumask *affinity, struct module *owner)
110 desc->irq_common_data.handler_data = NULL;
111 desc->irq_common_data.msi_desc = NULL;
113 desc->irq_data.common = &desc->irq_common_data;
114 desc->irq_data.irq = irq;
115 desc->irq_data.chip = &no_irq_chip;
116 desc->irq_data.chip_data = NULL;
117 irq_settings_clr_and_set(desc, ~0, _IRQ_DEFAULT_INIT_FLAGS);
118 irqd_set(&desc->irq_data, IRQD_IRQ_DISABLED);
119 irqd_set(&desc->irq_data, IRQD_IRQ_MASKED);
120 desc->handle_irq = handle_bad_irq;
123 desc->irqs_unhandled = 0;
126 for_each_possible_cpu(cpu)
127 *per_cpu_ptr(desc->kstat_irqs, cpu) = 0;
128 desc_smp_init(desc, node, affinity);
131 int nr_irqs = NR_IRQS;
132 EXPORT_SYMBOL_GPL(nr_irqs);
134 static DEFINE_MUTEX(sparse_irq_lock);
135 static DECLARE_BITMAP(allocated_irqs, IRQ_BITMAP_BITS);
137 #ifdef CONFIG_SPARSE_IRQ
139 static void irq_kobj_release(struct kobject *kobj);
142 static struct kobject *irq_kobj_base;
144 #define IRQ_ATTR_RO(_name) \
145 static struct kobj_attribute _name##_attr = __ATTR_RO(_name)
147 static ssize_t per_cpu_count_show(struct kobject *kobj,
148 struct kobj_attribute *attr, char *buf)
150 struct irq_desc *desc = container_of(kobj, struct irq_desc, kobj);
151 int cpu, irq = desc->irq_data.irq;
155 for_each_possible_cpu(cpu) {
156 unsigned int c = kstat_irqs_cpu(irq, cpu);
158 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "%s%u", p, c);
162 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "\n");
165 IRQ_ATTR_RO(per_cpu_count);
167 static ssize_t chip_name_show(struct kobject *kobj,
168 struct kobj_attribute *attr, char *buf)
170 struct irq_desc *desc = container_of(kobj, struct irq_desc, kobj);
173 raw_spin_lock_irq(&desc->lock);
174 if (desc->irq_data.chip && desc->irq_data.chip->name) {
175 ret = scnprintf(buf, PAGE_SIZE, "%s\n",
176 desc->irq_data.chip->name);
178 raw_spin_unlock_irq(&desc->lock);
182 IRQ_ATTR_RO(chip_name);
184 static ssize_t hwirq_show(struct kobject *kobj,
185 struct kobj_attribute *attr, char *buf)
187 struct irq_desc *desc = container_of(kobj, struct irq_desc, kobj);
190 raw_spin_lock_irq(&desc->lock);
191 if (desc->irq_data.domain)
192 ret = sprintf(buf, "%d\n", (int)desc->irq_data.hwirq);
193 raw_spin_unlock_irq(&desc->lock);
199 static ssize_t type_show(struct kobject *kobj,
200 struct kobj_attribute *attr, char *buf)
202 struct irq_desc *desc = container_of(kobj, struct irq_desc, kobj);
205 raw_spin_lock_irq(&desc->lock);
206 ret = sprintf(buf, "%s\n",
207 irqd_is_level_type(&desc->irq_data) ? "level" : "edge");
208 raw_spin_unlock_irq(&desc->lock);
215 static ssize_t name_show(struct kobject *kobj,
216 struct kobj_attribute *attr, char *buf)
218 struct irq_desc *desc = container_of(kobj, struct irq_desc, kobj);
221 raw_spin_lock_irq(&desc->lock);
223 ret = scnprintf(buf, PAGE_SIZE, "%s\n", desc->name);
224 raw_spin_unlock_irq(&desc->lock);
230 static ssize_t actions_show(struct kobject *kobj,
231 struct kobj_attribute *attr, char *buf)
233 struct irq_desc *desc = container_of(kobj, struct irq_desc, kobj);
234 struct irqaction *action;
238 raw_spin_lock_irq(&desc->lock);
239 for (action = desc->action; action != NULL; action = action->next) {
240 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "%s%s",
244 raw_spin_unlock_irq(&desc->lock);
247 ret += scnprintf(buf + ret, PAGE_SIZE - ret, "\n");
251 IRQ_ATTR_RO(actions);
253 static struct attribute *irq_attrs[] = {
254 &per_cpu_count_attr.attr,
255 &chip_name_attr.attr,
263 static struct kobj_type irq_kobj_type = {
264 .release = irq_kobj_release,
265 .sysfs_ops = &kobj_sysfs_ops,
266 .default_attrs = irq_attrs,
269 static void irq_sysfs_add(int irq, struct irq_desc *desc)
273 * Continue even in case of failure as this is nothing
276 if (kobject_add(&desc->kobj, irq_kobj_base, "%d", irq))
277 pr_warn("Failed to add kobject for irq %d\n", irq);
281 static int __init irq_sysfs_init(void)
283 struct irq_desc *desc;
286 /* Prevent concurrent irq alloc/free */
289 irq_kobj_base = kobject_create_and_add("irq", kernel_kobj);
290 if (!irq_kobj_base) {
295 /* Add the already allocated interrupts */
296 for_each_irq_desc(irq, desc)
297 irq_sysfs_add(irq, desc);
302 postcore_initcall(irq_sysfs_init);
304 #else /* !CONFIG_SYSFS */
306 static struct kobj_type irq_kobj_type = {
307 .release = irq_kobj_release,
310 static void irq_sysfs_add(int irq, struct irq_desc *desc) {}
312 #endif /* CONFIG_SYSFS */
314 static RADIX_TREE(irq_desc_tree, GFP_KERNEL);
316 static void irq_insert_desc(unsigned int irq, struct irq_desc *desc)
318 radix_tree_insert(&irq_desc_tree, irq, desc);
321 struct irq_desc *irq_to_desc(unsigned int irq)
323 return radix_tree_lookup(&irq_desc_tree, irq);
325 EXPORT_SYMBOL(irq_to_desc);
327 static void delete_irq_desc(unsigned int irq)
329 radix_tree_delete(&irq_desc_tree, irq);
333 static void free_masks(struct irq_desc *desc)
335 #ifdef CONFIG_GENERIC_PENDING_IRQ
336 free_cpumask_var(desc->pending_mask);
338 free_cpumask_var(desc->irq_common_data.affinity);
339 #ifdef CONFIG_GENERIC_IRQ_EFFECTIVE_AFF_MASK
340 free_cpumask_var(desc->irq_common_data.effective_affinity);
344 static inline void free_masks(struct irq_desc *desc) { }
347 void irq_lock_sparse(void)
349 mutex_lock(&sparse_irq_lock);
352 void irq_unlock_sparse(void)
354 mutex_unlock(&sparse_irq_lock);
357 static struct irq_desc *alloc_desc(int irq, int node, unsigned int flags,
358 const struct cpumask *affinity,
359 struct module *owner)
361 struct irq_desc *desc;
363 desc = kzalloc_node(sizeof(*desc), GFP_KERNEL, node);
366 /* allocate based on nr_cpu_ids */
367 desc->kstat_irqs = alloc_percpu(unsigned int);
368 if (!desc->kstat_irqs)
371 if (alloc_masks(desc, node))
374 raw_spin_lock_init(&desc->lock);
375 lockdep_set_class(&desc->lock, &irq_desc_lock_class);
376 mutex_init(&desc->request_mutex);
377 init_rcu_head(&desc->rcu);
379 desc_set_defaults(irq, desc, node, affinity, owner);
380 irqd_set(&desc->irq_data, flags);
381 kobject_init(&desc->kobj, &irq_kobj_type);
386 free_percpu(desc->kstat_irqs);
392 static void irq_kobj_release(struct kobject *kobj)
394 struct irq_desc *desc = container_of(kobj, struct irq_desc, kobj);
397 free_percpu(desc->kstat_irqs);
401 static void delayed_free_desc(struct rcu_head *rhp)
403 struct irq_desc *desc = container_of(rhp, struct irq_desc, rcu);
405 kobject_put(&desc->kobj);
408 static void free_desc(unsigned int irq)
410 struct irq_desc *desc = irq_to_desc(irq);
412 irq_remove_debugfs_entry(desc);
413 unregister_irq_proc(irq, desc);
416 * sparse_irq_lock protects also show_interrupts() and
417 * kstat_irq_usr(). Once we deleted the descriptor from the
418 * sparse tree we can free it. Access in proc will fail to
419 * lookup the descriptor.
421 * The sysfs entry must be serialized against a concurrent
422 * irq_sysfs_init() as well.
424 kobject_del(&desc->kobj);
425 delete_irq_desc(irq);
428 * We free the descriptor, masks and stat fields via RCU. That
429 * allows demultiplex interrupts to do rcu based management of
430 * the child interrupts.
432 call_rcu(&desc->rcu, delayed_free_desc);
435 static int alloc_descs(unsigned int start, unsigned int cnt, int node,
436 const struct cpumask *affinity, struct module *owner)
438 const struct cpumask *mask = NULL;
439 struct irq_desc *desc;
443 /* Validate affinity mask(s) */
445 for (i = 0, mask = affinity; i < cnt; i++, mask++) {
446 if (cpumask_empty(mask))
451 flags = affinity ? IRQD_AFFINITY_MANAGED : 0;
454 for (i = 0; i < cnt; i++) {
456 node = cpu_to_node(cpumask_first(affinity));
460 desc = alloc_desc(start + i, node, flags, mask, owner);
463 irq_insert_desc(start + i, desc);
464 irq_sysfs_add(start + i, desc);
466 bitmap_set(allocated_irqs, start, cnt);
470 for (i--; i >= 0; i--)
471 free_desc(start + i);
475 static int irq_expand_nr_irqs(unsigned int nr)
477 if (nr > IRQ_BITMAP_BITS)
483 int __init early_irq_init(void)
485 int i, initcnt, node = first_online_node;
486 struct irq_desc *desc;
488 init_irq_default_affinity();
490 /* Let arch update nr_irqs and return the nr of preallocated irqs */
491 initcnt = arch_probe_nr_irqs();
492 printk(KERN_INFO "NR_IRQS: %d, nr_irqs: %d, preallocated irqs: %d\n",
493 NR_IRQS, nr_irqs, initcnt);
495 if (WARN_ON(nr_irqs > IRQ_BITMAP_BITS))
496 nr_irqs = IRQ_BITMAP_BITS;
498 if (WARN_ON(initcnt > IRQ_BITMAP_BITS))
499 initcnt = IRQ_BITMAP_BITS;
501 if (initcnt > nr_irqs)
504 for (i = 0; i < initcnt; i++) {
505 desc = alloc_desc(i, node, 0, NULL, NULL);
506 set_bit(i, allocated_irqs);
507 irq_insert_desc(i, desc);
509 return arch_early_irq_init();
512 #else /* !CONFIG_SPARSE_IRQ */
514 struct irq_desc irq_desc[NR_IRQS] __cacheline_aligned_in_smp = {
515 [0 ... NR_IRQS-1] = {
516 .handle_irq = handle_bad_irq,
518 .lock = __RAW_SPIN_LOCK_UNLOCKED(irq_desc->lock),
522 int __init early_irq_init(void)
524 int count, i, node = first_online_node;
525 struct irq_desc *desc;
527 init_irq_default_affinity();
529 printk(KERN_INFO "NR_IRQS: %d\n", NR_IRQS);
532 count = ARRAY_SIZE(irq_desc);
534 for (i = 0; i < count; i++) {
535 desc[i].kstat_irqs = alloc_percpu(unsigned int);
536 alloc_masks(&desc[i], node);
537 raw_spin_lock_init(&desc[i].lock);
538 lockdep_set_class(&desc[i].lock, &irq_desc_lock_class);
539 desc_set_defaults(i, &desc[i], node, NULL, NULL);
541 return arch_early_irq_init();
544 struct irq_desc *irq_to_desc(unsigned int irq)
546 return (irq < NR_IRQS) ? irq_desc + irq : NULL;
548 EXPORT_SYMBOL(irq_to_desc);
550 static void free_desc(unsigned int irq)
552 struct irq_desc *desc = irq_to_desc(irq);
555 raw_spin_lock_irqsave(&desc->lock, flags);
556 desc_set_defaults(irq, desc, irq_desc_get_node(desc), NULL, NULL);
557 raw_spin_unlock_irqrestore(&desc->lock, flags);
560 static inline int alloc_descs(unsigned int start, unsigned int cnt, int node,
561 const struct cpumask *affinity,
562 struct module *owner)
566 for (i = 0; i < cnt; i++) {
567 struct irq_desc *desc = irq_to_desc(start + i);
571 bitmap_set(allocated_irqs, start, cnt);
575 static int irq_expand_nr_irqs(unsigned int nr)
580 void irq_mark_irq(unsigned int irq)
582 mutex_lock(&sparse_irq_lock);
583 bitmap_set(allocated_irqs, irq, 1);
584 mutex_unlock(&sparse_irq_lock);
587 #ifdef CONFIG_GENERIC_IRQ_LEGACY
588 void irq_init_desc(unsigned int irq)
594 #endif /* !CONFIG_SPARSE_IRQ */
597 * generic_handle_irq - Invoke the handler for a particular irq
598 * @irq: The irq number to handle
601 int generic_handle_irq(unsigned int irq)
603 struct irq_desc *desc = irq_to_desc(irq);
607 generic_handle_irq_desc(desc);
610 EXPORT_SYMBOL_GPL(generic_handle_irq);
612 #ifdef CONFIG_HANDLE_DOMAIN_IRQ
614 * __handle_domain_irq - Invoke the handler for a HW irq belonging to a domain
615 * @domain: The domain where to perform the lookup
616 * @hwirq: The HW irq number to convert to a logical one
617 * @lookup: Whether to perform the domain lookup or not
618 * @regs: Register file coming from the low-level handling code
620 * Returns: 0 on success, or -EINVAL if conversion has failed
622 int __handle_domain_irq(struct irq_domain *domain, unsigned int hwirq,
623 bool lookup, struct pt_regs *regs)
625 struct pt_regs *old_regs = set_irq_regs(regs);
626 unsigned int irq = hwirq;
631 #ifdef CONFIG_IRQ_DOMAIN
633 irq = irq_find_mapping(domain, hwirq);
637 * Some hardware gives randomly wrong interrupts. Rather
638 * than crashing, do something sensible.
640 if (unlikely(!irq || irq >= nr_irqs)) {
644 generic_handle_irq(irq);
648 set_irq_regs(old_regs);
653 /* Dynamic interrupt handling */
656 * irq_free_descs - free irq descriptors
657 * @from: Start of descriptor range
658 * @cnt: Number of consecutive irqs to free
660 void irq_free_descs(unsigned int from, unsigned int cnt)
664 if (from >= nr_irqs || (from + cnt) > nr_irqs)
667 mutex_lock(&sparse_irq_lock);
668 for (i = 0; i < cnt; i++)
671 bitmap_clear(allocated_irqs, from, cnt);
672 mutex_unlock(&sparse_irq_lock);
674 EXPORT_SYMBOL_GPL(irq_free_descs);
677 * irq_alloc_descs - allocate and initialize a range of irq descriptors
678 * @irq: Allocate for specific irq number if irq >= 0
679 * @from: Start the search from this irq number
680 * @cnt: Number of consecutive irqs to allocate.
681 * @node: Preferred node on which the irq descriptor should be allocated
682 * @owner: Owning module (can be NULL)
683 * @affinity: Optional pointer to an affinity mask array of size @cnt which
684 * hints where the irq descriptors should be allocated and which
685 * default affinities to use
687 * Returns the first irq number or error code
690 __irq_alloc_descs(int irq, unsigned int from, unsigned int cnt, int node,
691 struct module *owner, const struct cpumask *affinity)
704 * For interrupts which are freely allocated the
705 * architecture can force a lower bound to the @from
706 * argument. x86 uses this to exclude the GSI space.
708 from = arch_dynirq_lower_bound(from);
711 mutex_lock(&sparse_irq_lock);
713 start = bitmap_find_next_zero_area(allocated_irqs, IRQ_BITMAP_BITS,
716 if (irq >=0 && start != irq)
719 if (start + cnt > nr_irqs) {
720 ret = irq_expand_nr_irqs(start + cnt);
724 ret = alloc_descs(start, cnt, node, affinity, owner);
726 mutex_unlock(&sparse_irq_lock);
729 EXPORT_SYMBOL_GPL(__irq_alloc_descs);
731 #ifdef CONFIG_GENERIC_IRQ_LEGACY_ALLOC_HWIRQ
733 * irq_alloc_hwirqs - Allocate an irq descriptor and initialize the hardware
734 * @cnt: number of interrupts to allocate
735 * @node: node on which to allocate
737 * Returns an interrupt number > 0 or 0, if the allocation fails.
739 unsigned int irq_alloc_hwirqs(int cnt, int node)
741 int i, irq = __irq_alloc_descs(-1, 0, cnt, node, NULL, NULL);
746 for (i = irq; cnt > 0; i++, cnt--) {
747 if (arch_setup_hwirq(i, node))
749 irq_clear_status_flags(i, _IRQ_NOREQUEST);
754 for (i--; i >= irq; i--) {
755 irq_set_status_flags(i, _IRQ_NOREQUEST | _IRQ_NOPROBE);
756 arch_teardown_hwirq(i);
758 irq_free_descs(irq, cnt);
761 EXPORT_SYMBOL_GPL(irq_alloc_hwirqs);
764 * irq_free_hwirqs - Free irq descriptor and cleanup the hardware
765 * @from: Free from irq number
766 * @cnt: number of interrupts to free
769 void irq_free_hwirqs(unsigned int from, int cnt)
773 for (i = from, j = cnt; j > 0; i++, j--) {
774 irq_set_status_flags(i, _IRQ_NOREQUEST | _IRQ_NOPROBE);
775 arch_teardown_hwirq(i);
777 irq_free_descs(from, cnt);
779 EXPORT_SYMBOL_GPL(irq_free_hwirqs);
783 * irq_get_next_irq - get next allocated irq number
784 * @offset: where to start the search
786 * Returns next irq number after offset or nr_irqs if none is found.
788 unsigned int irq_get_next_irq(unsigned int offset)
790 return find_next_bit(allocated_irqs, nr_irqs, offset);
794 __irq_get_desc_lock(unsigned int irq, unsigned long *flags, bool bus,
797 struct irq_desc *desc = irq_to_desc(irq);
800 if (check & _IRQ_DESC_CHECK) {
801 if ((check & _IRQ_DESC_PERCPU) &&
802 !irq_settings_is_per_cpu_devid(desc))
805 if (!(check & _IRQ_DESC_PERCPU) &&
806 irq_settings_is_per_cpu_devid(desc))
812 raw_spin_lock_irqsave(&desc->lock, *flags);
817 void __irq_put_desc_unlock(struct irq_desc *desc, unsigned long flags, bool bus)
819 raw_spin_unlock_irqrestore(&desc->lock, flags);
821 chip_bus_sync_unlock(desc);
824 int irq_set_percpu_devid_partition(unsigned int irq,
825 const struct cpumask *affinity)
827 struct irq_desc *desc = irq_to_desc(irq);
832 if (desc->percpu_enabled)
835 desc->percpu_enabled = kzalloc(sizeof(*desc->percpu_enabled), GFP_KERNEL);
837 if (!desc->percpu_enabled)
841 desc->percpu_affinity = affinity;
843 desc->percpu_affinity = cpu_possible_mask;
845 irq_set_percpu_devid_flags(irq);
849 int irq_set_percpu_devid(unsigned int irq)
851 return irq_set_percpu_devid_partition(irq, NULL);
854 int irq_get_percpu_devid_partition(unsigned int irq, struct cpumask *affinity)
856 struct irq_desc *desc = irq_to_desc(irq);
858 if (!desc || !desc->percpu_enabled)
862 cpumask_copy(affinity, desc->percpu_affinity);
867 void kstat_incr_irq_this_cpu(unsigned int irq)
869 kstat_incr_irqs_this_cpu(irq_to_desc(irq));
873 * kstat_irqs_cpu - Get the statistics for an interrupt on a cpu
874 * @irq: The interrupt number
875 * @cpu: The cpu number
877 * Returns the sum of interrupt counts on @cpu since boot for
878 * @irq. The caller must ensure that the interrupt is not removed
881 unsigned int kstat_irqs_cpu(unsigned int irq, int cpu)
883 struct irq_desc *desc = irq_to_desc(irq);
885 return desc && desc->kstat_irqs ?
886 *per_cpu_ptr(desc->kstat_irqs, cpu) : 0;
890 * kstat_irqs - Get the statistics for an interrupt
891 * @irq: The interrupt number
893 * Returns the sum of interrupt counts on all cpus since boot for
894 * @irq. The caller must ensure that the interrupt is not removed
897 unsigned int kstat_irqs(unsigned int irq)
899 struct irq_desc *desc = irq_to_desc(irq);
901 unsigned int sum = 0;
903 if (!desc || !desc->kstat_irqs)
905 for_each_possible_cpu(cpu)
906 sum += *per_cpu_ptr(desc->kstat_irqs, cpu);
911 * kstat_irqs_usr - Get the statistics for an interrupt
912 * @irq: The interrupt number
914 * Returns the sum of interrupt counts on all cpus since boot for
915 * @irq. Contrary to kstat_irqs() this can be called from any
916 * preemptible context. It's protected against concurrent removal of
917 * an interrupt descriptor when sparse irqs are enabled.
919 unsigned int kstat_irqs_usr(unsigned int irq)
924 sum = kstat_irqs(irq);