1 // SPDX-License-Identifier: GPL-2.0
3 * Support for adapter interruptions
5 * Copyright IBM Corp. 1999, 2007
6 * Author(s): Ingo Adlung <adlung@de.ibm.com>
7 * Cornelia Huck <cornelia.huck@de.ibm.com>
8 * Arnd Bergmann <arndb@de.ibm.com>
9 * Peter Oberparleiter <peter.oberparleiter@de.ibm.com>
12 #include <linux/init.h>
13 #include <linux/irq.h>
14 #include <linux/kernel_stat.h>
15 #include <linux/module.h>
16 #include <linux/mutex.h>
17 #include <linux/rculist.h>
18 #include <linux/slab.h>
19 #include <linux/dmapool.h>
26 #include "cio_debug.h"
29 static DEFINE_SPINLOCK(airq_lists_lock
);
30 static struct hlist_head airq_lists
[MAX_ISC
+1];
32 static struct dma_pool
*airq_iv_cache
;
35 * register_adapter_interrupt() - register adapter interrupt handler
36 * @airq: pointer to adapter interrupt descriptor
38 * Returns 0 on success, or -EINVAL.
40 int register_adapter_interrupt(struct airq_struct
*airq
)
44 if (!airq
->handler
|| airq
->isc
> MAX_ISC
)
47 airq
->lsi_ptr
= kzalloc(1, GFP_KERNEL
);
50 airq
->flags
|= AIRQ_PTR_ALLOCATED
;
53 airq
->lsi_mask
= 0xff;
54 snprintf(dbf_txt
, sizeof(dbf_txt
), "rairq:%p", airq
);
55 CIO_TRACE_EVENT(4, dbf_txt
);
56 isc_register(airq
->isc
);
57 spin_lock(&airq_lists_lock
);
58 hlist_add_head_rcu(&airq
->list
, &airq_lists
[airq
->isc
]);
59 spin_unlock(&airq_lists_lock
);
62 EXPORT_SYMBOL(register_adapter_interrupt
);
65 * unregister_adapter_interrupt - unregister adapter interrupt handler
66 * @airq: pointer to adapter interrupt descriptor
68 void unregister_adapter_interrupt(struct airq_struct
*airq
)
72 if (hlist_unhashed(&airq
->list
))
74 snprintf(dbf_txt
, sizeof(dbf_txt
), "urairq:%p", airq
);
75 CIO_TRACE_EVENT(4, dbf_txt
);
76 spin_lock(&airq_lists_lock
);
77 hlist_del_rcu(&airq
->list
);
78 spin_unlock(&airq_lists_lock
);
80 isc_unregister(airq
->isc
);
81 if (airq
->flags
& AIRQ_PTR_ALLOCATED
) {
84 airq
->flags
&= ~AIRQ_PTR_ALLOCATED
;
87 EXPORT_SYMBOL(unregister_adapter_interrupt
);
89 static irqreturn_t
do_airq_interrupt(int irq
, void *dummy
)
91 struct tpi_info
*tpi_info
;
92 struct airq_struct
*airq
;
93 struct hlist_head
*head
;
95 set_cpu_flag(CIF_NOHZ_DELAY
);
96 tpi_info
= (struct tpi_info
*) &get_irq_regs()->int_code
;
97 trace_s390_cio_adapter_int(tpi_info
);
98 head
= &airq_lists
[tpi_info
->isc
];
100 hlist_for_each_entry_rcu(airq
, head
, list
)
101 if ((*airq
->lsi_ptr
& airq
->lsi_mask
) != 0)
102 airq
->handler(airq
, !tpi_info
->directed_irq
);
108 void __init
init_airq_interrupts(void)
110 irq_set_chip_and_handler(THIN_INTERRUPT
,
111 &dummy_irq_chip
, handle_percpu_irq
);
112 if (request_irq(THIN_INTERRUPT
, do_airq_interrupt
, 0, "AIO", NULL
))
113 panic("Failed to register AIO interrupt\n");
116 static inline unsigned long iv_size(unsigned long bits
)
118 return BITS_TO_LONGS(bits
) * sizeof(unsigned long);
122 * airq_iv_create - create an interrupt vector
123 * @bits: number of bits in the interrupt vector
124 * @flags: allocation flags
126 * Returns a pointer to an interrupt vector structure
128 struct airq_iv
*airq_iv_create(unsigned long bits
, unsigned long flags
)
133 iv
= kzalloc(sizeof(*iv
), GFP_KERNEL
);
138 size
= iv_size(bits
);
140 if (flags
& AIRQ_IV_CACHELINE
) {
141 if ((cache_line_size() * BITS_PER_BYTE
) < bits
145 iv
->vector
= dma_pool_zalloc(airq_iv_cache
, GFP_KERNEL
,
150 iv
->vector
= cio_dma_zalloc(size
);
154 if (flags
& AIRQ_IV_ALLOC
) {
155 iv
->avail
= kmalloc(size
, GFP_KERNEL
);
158 memset(iv
->avail
, 0xff, size
);
162 if (flags
& AIRQ_IV_BITLOCK
) {
163 iv
->bitlock
= kzalloc(size
, GFP_KERNEL
);
167 if (flags
& AIRQ_IV_PTR
) {
168 size
= bits
* sizeof(unsigned long);
169 iv
->ptr
= kzalloc(size
, GFP_KERNEL
);
173 if (flags
& AIRQ_IV_DATA
) {
174 size
= bits
* sizeof(unsigned int);
175 iv
->data
= kzalloc(size
, GFP_KERNEL
);
179 spin_lock_init(&iv
->lock
);
186 if (iv
->flags
& AIRQ_IV_CACHELINE
&& iv
->vector
)
187 dma_pool_free(airq_iv_cache
, iv
->vector
, iv
->vector_dma
);
189 cio_dma_free(iv
->vector
, size
);
194 EXPORT_SYMBOL(airq_iv_create
);
197 * airq_iv_release - release an interrupt vector
198 * @iv: pointer to interrupt vector structure
200 void airq_iv_release(struct airq_iv
*iv
)
205 if (iv
->flags
& AIRQ_IV_CACHELINE
)
206 dma_pool_free(airq_iv_cache
, iv
->vector
, iv
->vector_dma
);
208 cio_dma_free(iv
->vector
, iv_size(iv
->bits
));
212 EXPORT_SYMBOL(airq_iv_release
);
215 * airq_iv_alloc - allocate irq bits from an interrupt vector
216 * @iv: pointer to an interrupt vector structure
217 * @num: number of consecutive irq bits to allocate
219 * Returns the bit number of the first irq in the allocated block of irqs,
220 * or -1UL if no bit is available or the AIRQ_IV_ALLOC flag has not been
223 unsigned long airq_iv_alloc(struct airq_iv
*iv
, unsigned long num
)
225 unsigned long bit
, i
, flags
;
227 if (!iv
->avail
|| num
== 0)
229 spin_lock_irqsave(&iv
->lock
, flags
);
230 bit
= find_first_bit_inv(iv
->avail
, iv
->bits
);
231 while (bit
+ num
<= iv
->bits
) {
232 for (i
= 1; i
< num
; i
++)
233 if (!test_bit_inv(bit
+ i
, iv
->avail
))
236 /* Found a suitable block of irqs */
237 for (i
= 0; i
< num
; i
++)
238 clear_bit_inv(bit
+ i
, iv
->avail
);
239 if (bit
+ num
>= iv
->end
)
240 iv
->end
= bit
+ num
+ 1;
243 bit
= find_next_bit_inv(iv
->avail
, iv
->bits
, bit
+ i
+ 1);
245 if (bit
+ num
> iv
->bits
)
247 spin_unlock_irqrestore(&iv
->lock
, flags
);
250 EXPORT_SYMBOL(airq_iv_alloc
);
253 * airq_iv_free - free irq bits of an interrupt vector
254 * @iv: pointer to interrupt vector structure
255 * @bit: number of the first irq bit to free
256 * @num: number of consecutive irq bits to free
258 void airq_iv_free(struct airq_iv
*iv
, unsigned long bit
, unsigned long num
)
260 unsigned long i
, flags
;
262 if (!iv
->avail
|| num
== 0)
264 spin_lock_irqsave(&iv
->lock
, flags
);
265 for (i
= 0; i
< num
; i
++) {
266 /* Clear (possibly left over) interrupt bit */
267 clear_bit_inv(bit
+ i
, iv
->vector
);
268 /* Make the bit positions available again */
269 set_bit_inv(bit
+ i
, iv
->avail
);
271 if (bit
+ num
>= iv
->end
) {
272 /* Find new end of bit-field */
273 while (iv
->end
> 0 && !test_bit_inv(iv
->end
- 1, iv
->avail
))
276 spin_unlock_irqrestore(&iv
->lock
, flags
);
278 EXPORT_SYMBOL(airq_iv_free
);
281 * airq_iv_scan - scan interrupt vector for non-zero bits
282 * @iv: pointer to interrupt vector structure
283 * @start: bit number to start the search
284 * @end: bit number to end the search
286 * Returns the bit number of the next non-zero interrupt bit, or
287 * -1UL if the scan completed without finding any more any non-zero bits.
289 unsigned long airq_iv_scan(struct airq_iv
*iv
, unsigned long start
,
294 /* Find non-zero bit starting from 'ivs->next'. */
295 bit
= find_next_bit_inv(iv
->vector
, end
, start
);
298 clear_bit_inv(bit
, iv
->vector
);
301 EXPORT_SYMBOL(airq_iv_scan
);
303 int __init
airq_init(void)
305 airq_iv_cache
= dma_pool_create("airq_iv_cache", cio_get_dma_css_dev(),
307 cache_line_size(), PAGE_SIZE
);