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/DocBook/genericirq
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>
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 zalloc_cpumask_var(&irq_default_affinity, GFP_NOWAIT);
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 comandline masks
35 cpumask_set_cpu(smp_processor_id(), irq_default_affinity);
38 __setup("irqaffinity=", irq_affinity_setup);
40 static void __init init_irq_default_affinity(void)
42 #ifdef CONFIG_CPUMASK_OFFSTACK
43 if (!irq_default_affinity)
44 zalloc_cpumask_var(&irq_default_affinity, GFP_NOWAIT);
46 if (cpumask_empty(irq_default_affinity))
47 cpumask_setall(irq_default_affinity);
50 static void __init init_irq_default_affinity(void)
56 static int alloc_masks(struct irq_desc *desc, gfp_t gfp, int node)
58 if (!zalloc_cpumask_var_node(&desc->irq_common_data.affinity,
62 #ifdef CONFIG_GENERIC_PENDING_IRQ
63 if (!zalloc_cpumask_var_node(&desc->pending_mask, gfp, node)) {
64 free_cpumask_var(desc->irq_common_data.affinity);
71 static void desc_smp_init(struct irq_desc *desc, int node,
72 const struct cpumask *affinity)
75 affinity = irq_default_affinity;
76 cpumask_copy(desc->irq_common_data.affinity, affinity);
78 #ifdef CONFIG_GENERIC_PENDING_IRQ
79 cpumask_clear(desc->pending_mask);
82 desc->irq_common_data.node = node;
88 alloc_masks(struct irq_desc *desc, gfp_t gfp, int node) { return 0; }
90 desc_smp_init(struct irq_desc *desc, int node, const struct cpumask *affinity) { }
93 static void desc_set_defaults(unsigned int irq, struct irq_desc *desc, int node,
94 const struct cpumask *affinity, struct module *owner)
98 desc->irq_common_data.handler_data = NULL;
99 desc->irq_common_data.msi_desc = NULL;
101 desc->irq_data.common = &desc->irq_common_data;
102 desc->irq_data.irq = irq;
103 desc->irq_data.chip = &no_irq_chip;
104 desc->irq_data.chip_data = NULL;
105 irq_settings_clr_and_set(desc, ~0, _IRQ_DEFAULT_INIT_FLAGS);
106 irqd_set(&desc->irq_data, IRQD_IRQ_DISABLED);
107 desc->handle_irq = handle_bad_irq;
110 desc->irqs_unhandled = 0;
113 for_each_possible_cpu(cpu)
114 *per_cpu_ptr(desc->kstat_irqs, cpu) = 0;
115 desc_smp_init(desc, node, affinity);
118 int nr_irqs = NR_IRQS;
119 EXPORT_SYMBOL_GPL(nr_irqs);
121 static DEFINE_MUTEX(sparse_irq_lock);
122 static DECLARE_BITMAP(allocated_irqs, IRQ_BITMAP_BITS);
124 #ifdef CONFIG_SPARSE_IRQ
126 static RADIX_TREE(irq_desc_tree, GFP_KERNEL);
128 static void irq_insert_desc(unsigned int irq, struct irq_desc *desc)
130 radix_tree_insert(&irq_desc_tree, irq, desc);
133 struct irq_desc *irq_to_desc(unsigned int irq)
135 return radix_tree_lookup(&irq_desc_tree, irq);
137 EXPORT_SYMBOL(irq_to_desc);
139 static void delete_irq_desc(unsigned int irq)
141 radix_tree_delete(&irq_desc_tree, irq);
145 static void free_masks(struct irq_desc *desc)
147 #ifdef CONFIG_GENERIC_PENDING_IRQ
148 free_cpumask_var(desc->pending_mask);
150 free_cpumask_var(desc->irq_common_data.affinity);
153 static inline void free_masks(struct irq_desc *desc) { }
156 void irq_lock_sparse(void)
158 mutex_lock(&sparse_irq_lock);
161 void irq_unlock_sparse(void)
163 mutex_unlock(&sparse_irq_lock);
166 static struct irq_desc *alloc_desc(int irq, int node, unsigned int flags,
167 const struct cpumask *affinity,
168 struct module *owner)
170 struct irq_desc *desc;
171 gfp_t gfp = GFP_KERNEL;
173 desc = kzalloc_node(sizeof(*desc), gfp, node);
176 /* allocate based on nr_cpu_ids */
177 desc->kstat_irqs = alloc_percpu(unsigned int);
178 if (!desc->kstat_irqs)
181 if (alloc_masks(desc, gfp, node))
184 raw_spin_lock_init(&desc->lock);
185 lockdep_set_class(&desc->lock, &irq_desc_lock_class);
186 init_rcu_head(&desc->rcu);
188 desc_set_defaults(irq, desc, node, affinity, owner);
189 irqd_set(&desc->irq_data, flags);
194 free_percpu(desc->kstat_irqs);
200 static void delayed_free_desc(struct rcu_head *rhp)
202 struct irq_desc *desc = container_of(rhp, struct irq_desc, rcu);
205 free_percpu(desc->kstat_irqs);
209 static void free_desc(unsigned int irq)
211 struct irq_desc *desc = irq_to_desc(irq);
213 unregister_irq_proc(irq, desc);
216 * sparse_irq_lock protects also show_interrupts() and
217 * kstat_irq_usr(). Once we deleted the descriptor from the
218 * sparse tree we can free it. Access in proc will fail to
219 * lookup the descriptor.
221 mutex_lock(&sparse_irq_lock);
222 delete_irq_desc(irq);
223 mutex_unlock(&sparse_irq_lock);
226 * We free the descriptor, masks and stat fields via RCU. That
227 * allows demultiplex interrupts to do rcu based management of
228 * the child interrupts.
230 call_rcu(&desc->rcu, delayed_free_desc);
233 static int alloc_descs(unsigned int start, unsigned int cnt, int node,
234 const struct cpumask *affinity, struct module *owner)
236 const struct cpumask *mask = NULL;
237 struct irq_desc *desc;
241 if (affinity && cpumask_empty(affinity))
244 flags = affinity ? IRQD_AFFINITY_MANAGED : 0;
246 for (i = 0; i < cnt; i++) {
248 cpu = cpumask_next(cpu, affinity);
249 if (cpu >= nr_cpu_ids)
250 cpu = cpumask_first(affinity);
251 node = cpu_to_node(cpu);
254 * For single allocations we use the caller provided
255 * mask otherwise we use the mask of the target cpu
257 mask = cnt == 1 ? affinity : cpumask_of(cpu);
259 desc = alloc_desc(start + i, node, flags, mask, owner);
262 mutex_lock(&sparse_irq_lock);
263 irq_insert_desc(start + i, desc);
264 mutex_unlock(&sparse_irq_lock);
269 for (i--; i >= 0; i--)
270 free_desc(start + i);
272 mutex_lock(&sparse_irq_lock);
273 bitmap_clear(allocated_irqs, start, cnt);
274 mutex_unlock(&sparse_irq_lock);
278 static int irq_expand_nr_irqs(unsigned int nr)
280 if (nr > IRQ_BITMAP_BITS)
286 int __init early_irq_init(void)
288 int i, initcnt, node = first_online_node;
289 struct irq_desc *desc;
291 init_irq_default_affinity();
293 /* Let arch update nr_irqs and return the nr of preallocated irqs */
294 initcnt = arch_probe_nr_irqs();
295 printk(KERN_INFO "NR_IRQS:%d nr_irqs:%d %d\n", NR_IRQS, nr_irqs, initcnt);
297 if (WARN_ON(nr_irqs > IRQ_BITMAP_BITS))
298 nr_irqs = IRQ_BITMAP_BITS;
300 if (WARN_ON(initcnt > IRQ_BITMAP_BITS))
301 initcnt = IRQ_BITMAP_BITS;
303 if (initcnt > nr_irqs)
306 for (i = 0; i < initcnt; i++) {
307 desc = alloc_desc(i, node, 0, NULL, NULL);
308 set_bit(i, allocated_irqs);
309 irq_insert_desc(i, desc);
311 return arch_early_irq_init();
314 #else /* !CONFIG_SPARSE_IRQ */
316 struct irq_desc irq_desc[NR_IRQS] __cacheline_aligned_in_smp = {
317 [0 ... NR_IRQS-1] = {
318 .handle_irq = handle_bad_irq,
320 .lock = __RAW_SPIN_LOCK_UNLOCKED(irq_desc->lock),
324 int __init early_irq_init(void)
326 int count, i, node = first_online_node;
327 struct irq_desc *desc;
329 init_irq_default_affinity();
331 printk(KERN_INFO "NR_IRQS:%d\n", NR_IRQS);
334 count = ARRAY_SIZE(irq_desc);
336 for (i = 0; i < count; i++) {
337 desc[i].kstat_irqs = alloc_percpu(unsigned int);
338 alloc_masks(&desc[i], GFP_KERNEL, node);
339 raw_spin_lock_init(&desc[i].lock);
340 lockdep_set_class(&desc[i].lock, &irq_desc_lock_class);
341 desc_set_defaults(i, &desc[i], node, NULL, NULL);
343 return arch_early_irq_init();
346 struct irq_desc *irq_to_desc(unsigned int irq)
348 return (irq < NR_IRQS) ? irq_desc + irq : NULL;
350 EXPORT_SYMBOL(irq_to_desc);
352 static void free_desc(unsigned int irq)
354 struct irq_desc *desc = irq_to_desc(irq);
357 raw_spin_lock_irqsave(&desc->lock, flags);
358 desc_set_defaults(irq, desc, irq_desc_get_node(desc), NULL, NULL);
359 raw_spin_unlock_irqrestore(&desc->lock, flags);
362 static inline int alloc_descs(unsigned int start, unsigned int cnt, int node,
363 const struct cpumask *affinity,
364 struct module *owner)
368 for (i = 0; i < cnt; i++) {
369 struct irq_desc *desc = irq_to_desc(start + i);
376 static int irq_expand_nr_irqs(unsigned int nr)
381 void irq_mark_irq(unsigned int irq)
383 mutex_lock(&sparse_irq_lock);
384 bitmap_set(allocated_irqs, irq, 1);
385 mutex_unlock(&sparse_irq_lock);
388 #ifdef CONFIG_GENERIC_IRQ_LEGACY
389 void irq_init_desc(unsigned int irq)
395 #endif /* !CONFIG_SPARSE_IRQ */
398 * generic_handle_irq - Invoke the handler for a particular irq
399 * @irq: The irq number to handle
402 int generic_handle_irq(unsigned int irq)
404 struct irq_desc *desc = irq_to_desc(irq);
408 generic_handle_irq_desc(desc);
411 EXPORT_SYMBOL_GPL(generic_handle_irq);
413 #ifdef CONFIG_HANDLE_DOMAIN_IRQ
415 * __handle_domain_irq - Invoke the handler for a HW irq belonging to a domain
416 * @domain: The domain where to perform the lookup
417 * @hwirq: The HW irq number to convert to a logical one
418 * @lookup: Whether to perform the domain lookup or not
419 * @regs: Register file coming from the low-level handling code
421 * Returns: 0 on success, or -EINVAL if conversion has failed
423 int __handle_domain_irq(struct irq_domain *domain, unsigned int hwirq,
424 bool lookup, struct pt_regs *regs)
426 struct pt_regs *old_regs = set_irq_regs(regs);
427 unsigned int irq = hwirq;
432 #ifdef CONFIG_IRQ_DOMAIN
434 irq = irq_find_mapping(domain, hwirq);
438 * Some hardware gives randomly wrong interrupts. Rather
439 * than crashing, do something sensible.
441 if (unlikely(!irq || irq >= nr_irqs)) {
445 generic_handle_irq(irq);
449 set_irq_regs(old_regs);
454 /* Dynamic interrupt handling */
457 * irq_free_descs - free irq descriptors
458 * @from: Start of descriptor range
459 * @cnt: Number of consecutive irqs to free
461 void irq_free_descs(unsigned int from, unsigned int cnt)
465 if (from >= nr_irqs || (from + cnt) > nr_irqs)
468 for (i = 0; i < cnt; i++)
471 mutex_lock(&sparse_irq_lock);
472 bitmap_clear(allocated_irqs, from, cnt);
473 mutex_unlock(&sparse_irq_lock);
475 EXPORT_SYMBOL_GPL(irq_free_descs);
478 * irq_alloc_descs - allocate and initialize a range of irq descriptors
479 * @irq: Allocate for specific irq number if irq >= 0
480 * @from: Start the search from this irq number
481 * @cnt: Number of consecutive irqs to allocate.
482 * @node: Preferred node on which the irq descriptor should be allocated
483 * @owner: Owning module (can be NULL)
484 * @affinity: Optional pointer to an affinity mask which hints where the
485 * irq descriptors should be allocated and which default
488 * Returns the first irq number or error code
491 __irq_alloc_descs(int irq, unsigned int from, unsigned int cnt, int node,
492 struct module *owner, const struct cpumask *affinity)
505 * For interrupts which are freely allocated the
506 * architecture can force a lower bound to the @from
507 * argument. x86 uses this to exclude the GSI space.
509 from = arch_dynirq_lower_bound(from);
512 mutex_lock(&sparse_irq_lock);
514 start = bitmap_find_next_zero_area(allocated_irqs, IRQ_BITMAP_BITS,
517 if (irq >=0 && start != irq)
520 if (start + cnt > nr_irqs) {
521 ret = irq_expand_nr_irqs(start + cnt);
526 bitmap_set(allocated_irqs, start, cnt);
527 mutex_unlock(&sparse_irq_lock);
528 return alloc_descs(start, cnt, node, affinity, owner);
531 mutex_unlock(&sparse_irq_lock);
534 EXPORT_SYMBOL_GPL(__irq_alloc_descs);
536 #ifdef CONFIG_GENERIC_IRQ_LEGACY_ALLOC_HWIRQ
538 * irq_alloc_hwirqs - Allocate an irq descriptor and initialize the hardware
539 * @cnt: number of interrupts to allocate
540 * @node: node on which to allocate
542 * Returns an interrupt number > 0 or 0, if the allocation fails.
544 unsigned int irq_alloc_hwirqs(int cnt, int node)
546 int i, irq = __irq_alloc_descs(-1, 0, cnt, node, NULL, NULL);
551 for (i = irq; cnt > 0; i++, cnt--) {
552 if (arch_setup_hwirq(i, node))
554 irq_clear_status_flags(i, _IRQ_NOREQUEST);
559 for (i--; i >= irq; i--) {
560 irq_set_status_flags(i, _IRQ_NOREQUEST | _IRQ_NOPROBE);
561 arch_teardown_hwirq(i);
563 irq_free_descs(irq, cnt);
566 EXPORT_SYMBOL_GPL(irq_alloc_hwirqs);
569 * irq_free_hwirqs - Free irq descriptor and cleanup the hardware
570 * @from: Free from irq number
571 * @cnt: number of interrupts to free
574 void irq_free_hwirqs(unsigned int from, int cnt)
578 for (i = from, j = cnt; j > 0; i++, j--) {
579 irq_set_status_flags(i, _IRQ_NOREQUEST | _IRQ_NOPROBE);
580 arch_teardown_hwirq(i);
582 irq_free_descs(from, cnt);
584 EXPORT_SYMBOL_GPL(irq_free_hwirqs);
588 * irq_get_next_irq - get next allocated irq number
589 * @offset: where to start the search
591 * Returns next irq number after offset or nr_irqs if none is found.
593 unsigned int irq_get_next_irq(unsigned int offset)
595 return find_next_bit(allocated_irqs, nr_irqs, offset);
599 __irq_get_desc_lock(unsigned int irq, unsigned long *flags, bool bus,
602 struct irq_desc *desc = irq_to_desc(irq);
605 if (check & _IRQ_DESC_CHECK) {
606 if ((check & _IRQ_DESC_PERCPU) &&
607 !irq_settings_is_per_cpu_devid(desc))
610 if (!(check & _IRQ_DESC_PERCPU) &&
611 irq_settings_is_per_cpu_devid(desc))
617 raw_spin_lock_irqsave(&desc->lock, *flags);
622 void __irq_put_desc_unlock(struct irq_desc *desc, unsigned long flags, bool bus)
624 raw_spin_unlock_irqrestore(&desc->lock, flags);
626 chip_bus_sync_unlock(desc);
629 int irq_set_percpu_devid_partition(unsigned int irq,
630 const struct cpumask *affinity)
632 struct irq_desc *desc = irq_to_desc(irq);
637 if (desc->percpu_enabled)
640 desc->percpu_enabled = kzalloc(sizeof(*desc->percpu_enabled), GFP_KERNEL);
642 if (!desc->percpu_enabled)
646 desc->percpu_affinity = affinity;
648 desc->percpu_affinity = cpu_possible_mask;
650 irq_set_percpu_devid_flags(irq);
654 int irq_set_percpu_devid(unsigned int irq)
656 return irq_set_percpu_devid_partition(irq, NULL);
659 int irq_get_percpu_devid_partition(unsigned int irq, struct cpumask *affinity)
661 struct irq_desc *desc = irq_to_desc(irq);
663 if (!desc || !desc->percpu_enabled)
667 cpumask_copy(affinity, desc->percpu_affinity);
672 void kstat_incr_irq_this_cpu(unsigned int irq)
674 kstat_incr_irqs_this_cpu(irq_to_desc(irq));
678 * kstat_irqs_cpu - Get the statistics for an interrupt on a cpu
679 * @irq: The interrupt number
680 * @cpu: The cpu number
682 * Returns the sum of interrupt counts on @cpu since boot for
683 * @irq. The caller must ensure that the interrupt is not removed
686 unsigned int kstat_irqs_cpu(unsigned int irq, int cpu)
688 struct irq_desc *desc = irq_to_desc(irq);
690 return desc && desc->kstat_irqs ?
691 *per_cpu_ptr(desc->kstat_irqs, cpu) : 0;
695 * kstat_irqs - Get the statistics for an interrupt
696 * @irq: The interrupt number
698 * Returns the sum of interrupt counts on all cpus since boot for
699 * @irq. The caller must ensure that the interrupt is not removed
702 unsigned int kstat_irqs(unsigned int irq)
704 struct irq_desc *desc = irq_to_desc(irq);
706 unsigned int sum = 0;
708 if (!desc || !desc->kstat_irqs)
710 for_each_possible_cpu(cpu)
711 sum += *per_cpu_ptr(desc->kstat_irqs, cpu);
716 * kstat_irqs_usr - Get the statistics for an interrupt
717 * @irq: The interrupt number
719 * Returns the sum of interrupt counts on all cpus since boot for
720 * @irq. Contrary to kstat_irqs() this can be called from any
721 * preemptible context. It's protected against concurrent removal of
722 * an interrupt descriptor when sparse irqs are enabled.
724 unsigned int kstat_irqs_usr(unsigned int irq)
729 sum = kstat_irqs(irq);