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;
40 struct irq_desc *desc;
42 raw_spin_lock_irqsave(&vector_lock, flags);
45 * First try the wanted IRQ
47 if (test_and_set_bit(irq_want, intc_irq_map) == 0) {
50 /* .. then fall back to scanning. */
51 new = find_first_zero_bit(intc_irq_map, nr_irqs);
52 if (unlikely(new == nr_irqs))
55 __set_bit(new, intc_irq_map);
58 desc = irq_to_desc_alloc_node(new, node);
59 if (unlikely(!desc)) {
60 pr_err("can't get irq_desc for %d\n", new);
64 desc = move_irq_desc(desc, node);
68 raw_spin_unlock_irqrestore(&vector_lock, flags);
71 dynamic_irq_init(irq);
80 int nid = cpu_to_node(smp_processor_id());
83 irq = create_irq_nr(NR_IRQS_LEGACY, nid);
90 void destroy_irq(unsigned int irq)
94 dynamic_irq_cleanup(irq);
96 raw_spin_lock_irqsave(&vector_lock, flags);
97 __clear_bit(irq, intc_irq_map);
98 raw_spin_unlock_irqrestore(&vector_lock, flags);
101 int reserve_irq_vector(unsigned int irq)
106 raw_spin_lock_irqsave(&vector_lock, flags);
107 if (test_and_set_bit(irq, intc_irq_map))
109 raw_spin_unlock_irqrestore(&vector_lock, flags);
114 void reserve_intc_vectors(struct intc_vect *vectors, unsigned int nr_vecs)
119 raw_spin_lock_irqsave(&vector_lock, flags);
120 for (i = 0; i < nr_vecs; i++)
121 __set_bit(evt2irq(vectors[i].vect), intc_irq_map);
122 raw_spin_unlock_irqrestore(&vector_lock, flags);
125 void reserve_irq_legacy(void)
130 raw_spin_lock_irqsave(&vector_lock, flags);
131 j = find_first_bit(intc_irq_map, nr_irqs);
132 for (i = 0; i < j; i++)
133 __set_bit(i, intc_irq_map);
134 raw_spin_unlock_irqrestore(&vector_lock, flags);