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>
24 #include "cio_debug.h"
27 static DEFINE_SPINLOCK(airq_lists_lock
);
28 static struct hlist_head airq_lists
[MAX_ISC
+1];
31 * register_adapter_interrupt() - register adapter interrupt handler
32 * @airq: pointer to adapter interrupt descriptor
34 * Returns 0 on success, or -EINVAL.
36 int register_adapter_interrupt(struct airq_struct
*airq
)
40 if (!airq
->handler
|| airq
->isc
> MAX_ISC
)
43 airq
->lsi_ptr
= kzalloc(1, GFP_KERNEL
);
46 airq
->flags
|= AIRQ_PTR_ALLOCATED
;
49 airq
->lsi_mask
= 0xff;
50 snprintf(dbf_txt
, sizeof(dbf_txt
), "rairq:%p", airq
);
51 CIO_TRACE_EVENT(4, dbf_txt
);
52 isc_register(airq
->isc
);
53 spin_lock(&airq_lists_lock
);
54 hlist_add_head_rcu(&airq
->list
, &airq_lists
[airq
->isc
]);
55 spin_unlock(&airq_lists_lock
);
58 EXPORT_SYMBOL(register_adapter_interrupt
);
61 * unregister_adapter_interrupt - unregister adapter interrupt handler
62 * @airq: pointer to adapter interrupt descriptor
64 void unregister_adapter_interrupt(struct airq_struct
*airq
)
68 if (hlist_unhashed(&airq
->list
))
70 snprintf(dbf_txt
, sizeof(dbf_txt
), "urairq:%p", airq
);
71 CIO_TRACE_EVENT(4, dbf_txt
);
72 spin_lock(&airq_lists_lock
);
73 hlist_del_rcu(&airq
->list
);
74 spin_unlock(&airq_lists_lock
);
76 isc_unregister(airq
->isc
);
77 if (airq
->flags
& AIRQ_PTR_ALLOCATED
) {
80 airq
->flags
&= ~AIRQ_PTR_ALLOCATED
;
83 EXPORT_SYMBOL(unregister_adapter_interrupt
);
85 static irqreturn_t
do_airq_interrupt(int irq
, void *dummy
)
87 struct tpi_info
*tpi_info
;
88 struct airq_struct
*airq
;
89 struct hlist_head
*head
;
91 set_cpu_flag(CIF_NOHZ_DELAY
);
92 tpi_info
= (struct tpi_info
*) &get_irq_regs()->int_code
;
93 trace_s390_cio_adapter_int(tpi_info
);
94 head
= &airq_lists
[tpi_info
->isc
];
96 hlist_for_each_entry_rcu(airq
, head
, list
)
97 if ((*airq
->lsi_ptr
& airq
->lsi_mask
) != 0)
104 static struct irqaction airq_interrupt
= {
106 .handler
= do_airq_interrupt
,
109 void __init
init_airq_interrupts(void)
111 irq_set_chip_and_handler(THIN_INTERRUPT
,
112 &dummy_irq_chip
, handle_percpu_irq
);
113 setup_irq(THIN_INTERRUPT
, &airq_interrupt
);
117 * airq_iv_create - create an interrupt vector
118 * @bits: number of bits in the interrupt vector
119 * @flags: allocation flags
121 * Returns a pointer to an interrupt vector structure
123 struct airq_iv
*airq_iv_create(unsigned long bits
, unsigned long flags
)
128 iv
= kzalloc(sizeof(*iv
), GFP_KERNEL
);
132 size
= BITS_TO_LONGS(bits
) * sizeof(unsigned long);
133 iv
->vector
= kzalloc(size
, GFP_KERNEL
);
136 if (flags
& AIRQ_IV_ALLOC
) {
137 iv
->avail
= kmalloc(size
, GFP_KERNEL
);
140 memset(iv
->avail
, 0xff, size
);
144 if (flags
& AIRQ_IV_BITLOCK
) {
145 iv
->bitlock
= kzalloc(size
, GFP_KERNEL
);
149 if (flags
& AIRQ_IV_PTR
) {
150 size
= bits
* sizeof(unsigned long);
151 iv
->ptr
= kzalloc(size
, GFP_KERNEL
);
155 if (flags
& AIRQ_IV_DATA
) {
156 size
= bits
* sizeof(unsigned int);
157 iv
->data
= kzalloc(size
, GFP_KERNEL
);
161 spin_lock_init(&iv
->lock
);
173 EXPORT_SYMBOL(airq_iv_create
);
176 * airq_iv_release - release an interrupt vector
177 * @iv: pointer to interrupt vector structure
179 void airq_iv_release(struct airq_iv
*iv
)
188 EXPORT_SYMBOL(airq_iv_release
);
191 * airq_iv_alloc - allocate irq bits from an interrupt vector
192 * @iv: pointer to an interrupt vector structure
193 * @num: number of consecutive irq bits to allocate
195 * Returns the bit number of the first irq in the allocated block of irqs,
196 * or -1UL if no bit is available or the AIRQ_IV_ALLOC flag has not been
199 unsigned long airq_iv_alloc(struct airq_iv
*iv
, unsigned long num
)
201 unsigned long bit
, i
, flags
;
203 if (!iv
->avail
|| num
== 0)
205 spin_lock_irqsave(&iv
->lock
, flags
);
206 bit
= find_first_bit_inv(iv
->avail
, iv
->bits
);
207 while (bit
+ num
<= iv
->bits
) {
208 for (i
= 1; i
< num
; i
++)
209 if (!test_bit_inv(bit
+ i
, iv
->avail
))
212 /* Found a suitable block of irqs */
213 for (i
= 0; i
< num
; i
++)
214 clear_bit_inv(bit
+ i
, iv
->avail
);
215 if (bit
+ num
>= iv
->end
)
216 iv
->end
= bit
+ num
+ 1;
219 bit
= find_next_bit_inv(iv
->avail
, iv
->bits
, bit
+ i
+ 1);
221 if (bit
+ num
> iv
->bits
)
223 spin_unlock_irqrestore(&iv
->lock
, flags
);
226 EXPORT_SYMBOL(airq_iv_alloc
);
229 * airq_iv_free - free irq bits of an interrupt vector
230 * @iv: pointer to interrupt vector structure
231 * @bit: number of the first irq bit to free
232 * @num: number of consecutive irq bits to free
234 void airq_iv_free(struct airq_iv
*iv
, unsigned long bit
, unsigned long num
)
236 unsigned long i
, flags
;
238 if (!iv
->avail
|| num
== 0)
240 spin_lock_irqsave(&iv
->lock
, flags
);
241 for (i
= 0; i
< num
; i
++) {
242 /* Clear (possibly left over) interrupt bit */
243 clear_bit_inv(bit
+ i
, iv
->vector
);
244 /* Make the bit positions available again */
245 set_bit_inv(bit
+ i
, iv
->avail
);
247 if (bit
+ num
>= iv
->end
) {
248 /* Find new end of bit-field */
249 while (iv
->end
> 0 && !test_bit_inv(iv
->end
- 1, iv
->avail
))
252 spin_unlock_irqrestore(&iv
->lock
, flags
);
254 EXPORT_SYMBOL(airq_iv_free
);
257 * airq_iv_scan - scan interrupt vector for non-zero bits
258 * @iv: pointer to interrupt vector structure
259 * @start: bit number to start the search
260 * @end: bit number to end the search
262 * Returns the bit number of the next non-zero interrupt bit, or
263 * -1UL if the scan completed without finding any more any non-zero bits.
265 unsigned long airq_iv_scan(struct airq_iv
*iv
, unsigned long start
,
270 /* Find non-zero bit starting from 'ivs->next'. */
271 bit
= find_next_bit_inv(iv
->vector
, end
, start
);
274 clear_bit_inv(bit
, iv
->vector
);
277 EXPORT_SYMBOL(airq_iv_scan
);