2 * Support for adapter interruptions
4 * Copyright IBM Corp. 1999, 2007
11 #include <linux/init.h>
12 #include <linux/irq.h>
13 #include <linux/kernel_stat.h>
14 #include <linux/module.h>
15 #include <linux/mutex.h>
16 #include <linux/rculist.h>
17 #include <linux/slab.h>
23 #include "cio_debug.h"
26 static DEFINE_SPINLOCK(airq_lists_lock);
27 static struct hlist_head airq_lists[MAX_ISC+1];
30 * register_adapter_interrupt() - register adapter interrupt handler
31 * @airq: pointer to adapter interrupt descriptor
33 * Returns 0 on success, or -EINVAL.
35 int register_adapter_interrupt(struct airq_struct *airq)
39 if (!airq->handler || airq->isc > MAX_ISC)
42 airq->lsi_ptr = kzalloc(1, GFP_KERNEL);
45 airq->flags |= AIRQ_PTR_ALLOCATED;
48 airq->lsi_mask = 0xff;
49 snprintf(dbf_txt, sizeof(dbf_txt), "rairq:%p", airq);
50 CIO_TRACE_EVENT(4, dbf_txt);
51 isc_register(airq->isc);
52 spin_lock(&airq_lists_lock);
53 hlist_add_head_rcu(&airq->list, &airq_lists[airq->isc]);
54 spin_unlock(&airq_lists_lock);
57 EXPORT_SYMBOL(register_adapter_interrupt);
60 * unregister_adapter_interrupt - unregister adapter interrupt handler
61 * @airq: pointer to adapter interrupt descriptor
63 void unregister_adapter_interrupt(struct airq_struct *airq)
67 if (hlist_unhashed(&airq->list))
69 snprintf(dbf_txt, sizeof(dbf_txt), "urairq:%p", airq);
70 CIO_TRACE_EVENT(4, dbf_txt);
71 spin_lock(&airq_lists_lock);
72 hlist_del_rcu(&airq->list);
73 spin_unlock(&airq_lists_lock);
75 isc_unregister(airq->isc);
76 if (airq->flags & AIRQ_PTR_ALLOCATED) {
79 airq->flags &= ~AIRQ_PTR_ALLOCATED;
82 EXPORT_SYMBOL(unregister_adapter_interrupt);
84 static irqreturn_t do_airq_interrupt(int irq, void *dummy)
86 struct tpi_info *tpi_info;
87 struct airq_struct *airq;
88 struct hlist_head *head;
90 set_cpu_flag(CIF_NOHZ_DELAY);
91 tpi_info = (struct tpi_info *) &get_irq_regs()->int_code;
92 head = &airq_lists[tpi_info->isc];
94 hlist_for_each_entry_rcu(airq, head, list)
95 if ((*airq->lsi_ptr & airq->lsi_mask) != 0)
102 static struct irqaction airq_interrupt = {
104 .handler = do_airq_interrupt,
107 void __init init_airq_interrupts(void)
109 irq_set_chip_and_handler(THIN_INTERRUPT,
110 &dummy_irq_chip, handle_percpu_irq);
111 setup_irq(THIN_INTERRUPT, &airq_interrupt);
115 * airq_iv_create - create an interrupt vector
116 * @bits: number of bits in the interrupt vector
117 * @flags: allocation flags
119 * Returns a pointer to an interrupt vector structure
121 struct airq_iv *airq_iv_create(unsigned long bits, unsigned long flags)
126 iv = kzalloc(sizeof(*iv), GFP_KERNEL);
130 size = BITS_TO_LONGS(bits) * sizeof(unsigned long);
131 iv->vector = kzalloc(size, GFP_KERNEL);
134 if (flags & AIRQ_IV_ALLOC) {
135 iv->avail = kmalloc(size, GFP_KERNEL);
138 memset(iv->avail, 0xff, size);
142 if (flags & AIRQ_IV_BITLOCK) {
143 iv->bitlock = kzalloc(size, GFP_KERNEL);
147 if (flags & AIRQ_IV_PTR) {
148 size = bits * sizeof(unsigned long);
149 iv->ptr = kzalloc(size, GFP_KERNEL);
153 if (flags & AIRQ_IV_DATA) {
154 size = bits * sizeof(unsigned int);
155 iv->data = kzalloc(size, GFP_KERNEL);
159 spin_lock_init(&iv->lock);
171 EXPORT_SYMBOL(airq_iv_create);
174 * airq_iv_release - release an interrupt vector
175 * @iv: pointer to interrupt vector structure
177 void airq_iv_release(struct airq_iv *iv)
186 EXPORT_SYMBOL(airq_iv_release);
189 * airq_iv_alloc - allocate irq bits from an interrupt vector
190 * @iv: pointer to an interrupt vector structure
191 * @num: number of consecutive irq bits to allocate
193 * Returns the bit number of the first irq in the allocated block of irqs,
194 * or -1UL if no bit is available or the AIRQ_IV_ALLOC flag has not been
197 unsigned long airq_iv_alloc(struct airq_iv *iv, unsigned long num)
199 unsigned long bit, i, flags;
201 if (!iv->avail || num == 0)
203 spin_lock_irqsave(&iv->lock, flags);
204 bit = find_first_bit_inv(iv->avail, iv->bits);
205 while (bit + num <= iv->bits) {
206 for (i = 1; i < num; i++)
207 if (!test_bit_inv(bit + i, iv->avail))
210 /* Found a suitable block of irqs */
211 for (i = 0; i < num; i++)
212 clear_bit_inv(bit + i, iv->avail);
213 if (bit + num >= iv->end)
214 iv->end = bit + num + 1;
217 bit = find_next_bit_inv(iv->avail, iv->bits, bit + i + 1);
219 if (bit + num > iv->bits)
221 spin_unlock_irqrestore(&iv->lock, flags);
224 EXPORT_SYMBOL(airq_iv_alloc);
227 * airq_iv_free - free irq bits of an interrupt vector
228 * @iv: pointer to interrupt vector structure
229 * @bit: number of the first irq bit to free
230 * @num: number of consecutive irq bits to free
232 void airq_iv_free(struct airq_iv *iv, unsigned long bit, unsigned long num)
234 unsigned long i, flags;
236 if (!iv->avail || num == 0)
238 spin_lock_irqsave(&iv->lock, flags);
239 for (i = 0; i < num; i++) {
240 /* Clear (possibly left over) interrupt bit */
241 clear_bit_inv(bit + i, iv->vector);
242 /* Make the bit positions available again */
243 set_bit_inv(bit + i, iv->avail);
245 if (bit + num >= iv->end) {
246 /* Find new end of bit-field */
247 while (iv->end > 0 && !test_bit_inv(iv->end - 1, iv->avail))
250 spin_unlock_irqrestore(&iv->lock, flags);
252 EXPORT_SYMBOL(airq_iv_free);
255 * airq_iv_scan - scan interrupt vector for non-zero bits
256 * @iv: pointer to interrupt vector structure
257 * @start: bit number to start the search
258 * @end: bit number to end the search
260 * Returns the bit number of the next non-zero interrupt bit, or
261 * -1UL if the scan completed without finding any more any non-zero bits.
263 unsigned long airq_iv_scan(struct airq_iv *iv, unsigned long start,
268 /* Find non-zero bit starting from 'ivs->next'. */
269 bit = find_next_bit_inv(iv->vector, end, start);
272 clear_bit_inv(bit, iv->vector);
275 EXPORT_SYMBOL(airq_iv_scan);