2 * Dynamic IRQ management
4 * Copyright (C) 2010 Paul Mundt
6 * Modelled after arch/x86/kernel/apic/io_apic.c
8 * This file is subject to the terms and conditions of the GNU General Public
9 * License. See the file "COPYING" in the main directory of this archive
12 #define pr_fmt(fmt) "intc: " fmt
14 #include <linux/irq.h>
15 #include <linux/bitmap.h>
16 #include <linux/spinlock.h>
17 #include "internals.h" /* only for activate_irq() damage.. */
20 * The intc_irq_map provides a global map of bound IRQ vectors for a
21 * given platform. Allocation of IRQs are either static through the CPU
22 * vector map, or dynamic in the case of board mux vectors or MSI.
24 * As this is a central point for all IRQ controllers on the system,
25 * each of the available sources are mapped out here. This combined with
26 * sparseirq makes it quite trivial to keep the vector map tightly packed
27 * when dynamically creating IRQs, as well as tying in to otherwise
28 * unused irq_desc positions in the sparse array.
30 static DECLARE_BITMAP(intc_irq_map, NR_IRQS);
31 static DEFINE_RAW_SPINLOCK(vector_lock);
34 * Dynamic IRQ allocation and deallocation
36 unsigned int create_irq_nr(unsigned int irq_want, int node)
38 unsigned int irq = 0, new;
41 raw_spin_lock_irqsave(&vector_lock, flags);
44 * First try the wanted IRQ
46 if (test_and_set_bit(irq_want, intc_irq_map) == 0) {
49 /* .. then fall back to scanning. */
50 new = find_first_zero_bit(intc_irq_map, nr_irqs);
51 if (unlikely(new == nr_irqs))
54 __set_bit(new, intc_irq_map);
57 raw_spin_unlock_irqrestore(&vector_lock, flags);
59 irq = irq_alloc_desc_at(new, node);
60 if (unlikely(irq != new)) {
61 pr_err("can't get irq_desc for %d\n", new);
69 raw_spin_unlock_irqrestore(&vector_lock, flags);
75 int nid = cpu_to_node(smp_processor_id());
78 irq = create_irq_nr(NR_IRQS_LEGACY, nid);
85 void destroy_irq(unsigned int irq)
91 raw_spin_lock_irqsave(&vector_lock, flags);
92 __clear_bit(irq, intc_irq_map);
93 raw_spin_unlock_irqrestore(&vector_lock, flags);
96 int reserve_irq_vector(unsigned int irq)
101 raw_spin_lock_irqsave(&vector_lock, flags);
102 if (test_and_set_bit(irq, intc_irq_map))
104 raw_spin_unlock_irqrestore(&vector_lock, flags);
109 void reserve_intc_vectors(struct intc_vect *vectors, unsigned int nr_vecs)
114 raw_spin_lock_irqsave(&vector_lock, flags);
115 for (i = 0; i < nr_vecs; i++)
116 __set_bit(evt2irq(vectors[i].vect), intc_irq_map);
117 raw_spin_unlock_irqrestore(&vector_lock, flags);
120 void reserve_irq_legacy(void)
125 raw_spin_lock_irqsave(&vector_lock, flags);
126 j = find_first_bit(intc_irq_map, nr_irqs);
127 for (i = 0; i < j; i++)
128 __set_bit(i, intc_irq_map);
129 raw_spin_unlock_irqrestore(&vector_lock, flags);