blob: 445564c790f65ddc587d2a8185468c28b9e711a7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Peter Oberparleiter4e8e56c2008-01-26 14:10:44 +01002 * Support for adapter interruptions
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Heiko Carstensa53c8fa2012-07-20 11:15:04 +02004 * Copyright IBM Corp. 1999, 2007
Peter Oberparleiter4e8e56c2008-01-26 14:10:44 +01005 * Author(s): Ingo Adlung <adlung@de.ibm.com>
6 * Cornelia Huck <cornelia.huck@de.ibm.com>
7 * Arnd Bergmann <arndb@de.ibm.com>
8 * Peter Oberparleiter <peter.oberparleiter@de.ibm.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
10
11#include <linux/init.h>
Martin Schwidefskyf4eae942013-06-24 10:30:41 +020012#include <linux/irq.h>
13#include <linux/kernel_stat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/module.h>
Martin Schwidefskyf4eae942013-06-24 10:30:41 +020015#include <linux/mutex.h>
16#include <linux/rculist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
Peter Oberparleiter4e8e56c2008-01-26 14:10:44 +010019#include <asm/airq.h>
Cornelia Huckda7c5af2008-07-14 09:58:59 +020020#include <asm/isc.h>
Peter Oberparleiter4e8e56c2008-01-26 14:10:44 +010021
22#include "cio.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include "cio_debug.h"
Martin Schwidefskyf4eae942013-06-24 10:30:41 +020024#include "ioasm.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Martin Schwidefskyf4eae942013-06-24 10:30:41 +020026static DEFINE_SPINLOCK(airq_lists_lock);
27static struct hlist_head airq_lists[MAX_ISC+1];
Peter Oberparleiter4e8e56c2008-01-26 14:10:44 +010028
29/**
Martin Schwidefskyf4eae942013-06-24 10:30:41 +020030 * register_adapter_interrupt() - register adapter interrupt handler
31 * @airq: pointer to adapter interrupt descriptor
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 *
Martin Schwidefskyf4eae942013-06-24 10:30:41 +020033 * Returns 0 on success, or -EINVAL.
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 */
Martin Schwidefskyf4eae942013-06-24 10:30:41 +020035int register_adapter_interrupt(struct airq_struct *airq)
Linus Torvalds1da177e2005-04-16 15:20:36 -070036{
Martin Schwidefskyf4eae942013-06-24 10:30:41 +020037 char dbf_txt[32];
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Martin Schwidefskyf4eae942013-06-24 10:30:41 +020039 if (!airq->handler || airq->isc > MAX_ISC)
40 return -EINVAL;
41 if (!airq->lsi_ptr) {
42 airq->lsi_ptr = kzalloc(1, GFP_KERNEL);
43 if (!airq->lsi_ptr)
44 return -ENOMEM;
45 airq->flags |= AIRQ_PTR_ALLOCATED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 }
Martin Schwidefskyf4eae942013-06-24 10:30:41 +020047 if (!airq->lsi_mask)
48 airq->lsi_mask = 0xff;
49 snprintf(dbf_txt, sizeof(dbf_txt), "rairq:%p", airq);
Peter Oberparleiter4e8e56c2008-01-26 14:10:44 +010050 CIO_TRACE_EVENT(4, dbf_txt);
Martin Schwidefskyf4eae942013-06-24 10:30:41 +020051 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);
55 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056}
Martin Schwidefskyf4eae942013-06-24 10:30:41 +020057EXPORT_SYMBOL(register_adapter_interrupt);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Peter Oberparleiter4e8e56c2008-01-26 14:10:44 +010059/**
Martin Schwidefskyf4eae942013-06-24 10:30:41 +020060 * unregister_adapter_interrupt - unregister adapter interrupt handler
61 * @airq: pointer to adapter interrupt descriptor
Peter Oberparleiter4e8e56c2008-01-26 14:10:44 +010062 */
Martin Schwidefskyf4eae942013-06-24 10:30:41 +020063void unregister_adapter_interrupt(struct airq_struct *airq)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064{
Martin Schwidefskyf4eae942013-06-24 10:30:41 +020065 char dbf_txt[32];
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Martin Schwidefskyf4eae942013-06-24 10:30:41 +020067 if (hlist_unhashed(&airq->list))
68 return;
69 snprintf(dbf_txt, sizeof(dbf_txt), "urairq:%p", airq);
Peter Oberparleiter4e8e56c2008-01-26 14:10:44 +010070 CIO_TRACE_EVENT(4, dbf_txt);
Martin Schwidefskyf4eae942013-06-24 10:30:41 +020071 spin_lock(&airq_lists_lock);
72 hlist_del_rcu(&airq->list);
73 spin_unlock(&airq_lists_lock);
74 synchronize_rcu();
75 isc_unregister(airq->isc);
76 if (airq->flags & AIRQ_PTR_ALLOCATED) {
77 kfree(airq->lsi_ptr);
78 airq->lsi_ptr = NULL;
79 airq->flags &= ~AIRQ_PTR_ALLOCATED;
80 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070081}
Martin Schwidefskyf4eae942013-06-24 10:30:41 +020082EXPORT_SYMBOL(unregister_adapter_interrupt);
Peter Oberparleiter4e8e56c2008-01-26 14:10:44 +010083
Martin Schwidefsky1f44a222013-06-27 09:01:09 +020084static irqreturn_t do_airq_interrupt(int irq, void *dummy)
Peter Oberparleiter4e8e56c2008-01-26 14:10:44 +010085{
Martin Schwidefsky1f44a222013-06-27 09:01:09 +020086 struct tpi_info *tpi_info;
Martin Schwidefskyf4eae942013-06-24 10:30:41 +020087 struct airq_struct *airq;
88 struct hlist_head *head;
Peter Oberparleiter4e8e56c2008-01-26 14:10:44 +010089
Martin Schwidefsky1f44a222013-06-27 09:01:09 +020090 __this_cpu_write(s390_idle.nohz_delay, 1);
91 tpi_info = (struct tpi_info *) &get_irq_regs()->int_code;
92 head = &airq_lists[tpi_info->isc];
Martin Schwidefskyf4eae942013-06-24 10:30:41 +020093 rcu_read_lock();
94 hlist_for_each_entry_rcu(airq, head, list)
95 if ((*airq->lsi_ptr & airq->lsi_mask) != 0)
96 airq->handler(airq);
97 rcu_read_unlock();
Martin Schwidefsky1f44a222013-06-27 09:01:09 +020098
99 return IRQ_HANDLED;
100}
101
102static struct irqaction airq_interrupt = {
103 .name = "AIO",
104 .handler = do_airq_interrupt,
105};
106
107void __init init_airq_interrupts(void)
108{
109 irq_set_chip_and_handler(THIN_INTERRUPT,
110 &dummy_irq_chip, handle_percpu_irq);
111 setup_irq(THIN_INTERRUPT, &airq_interrupt);
Peter Oberparleiter4e8e56c2008-01-26 14:10:44 +0100112}
Martin Schwidefskya9a6f032013-06-25 14:17:57 +0200113
114/**
115 * airq_iv_create - create an interrupt vector
116 * @bits: number of bits in the interrupt vector
117 * @flags: allocation flags
118 *
119 * Returns a pointer to an interrupt vector structure
120 */
121struct airq_iv *airq_iv_create(unsigned long bits, unsigned long flags)
122{
123 struct airq_iv *iv;
124 unsigned long size;
125
126 iv = kzalloc(sizeof(*iv), GFP_KERNEL);
127 if (!iv)
128 goto out;
129 iv->bits = bits;
130 size = BITS_TO_LONGS(bits) * sizeof(unsigned long);
131 iv->vector = kzalloc(size, GFP_KERNEL);
132 if (!iv->vector)
133 goto out_free;
134 if (flags & AIRQ_IV_ALLOC) {
135 iv->avail = kmalloc(size, GFP_KERNEL);
136 if (!iv->avail)
137 goto out_free;
138 memset(iv->avail, 0xff, size);
139 iv->end = 0;
140 } else
141 iv->end = bits;
142 if (flags & AIRQ_IV_BITLOCK) {
143 iv->bitlock = kzalloc(size, GFP_KERNEL);
144 if (!iv->bitlock)
145 goto out_free;
146 }
147 if (flags & AIRQ_IV_PTR) {
148 size = bits * sizeof(unsigned long);
149 iv->ptr = kzalloc(size, GFP_KERNEL);
150 if (!iv->ptr)
151 goto out_free;
152 }
153 if (flags & AIRQ_IV_DATA) {
154 size = bits * sizeof(unsigned int);
155 iv->data = kzalloc(size, GFP_KERNEL);
156 if (!iv->data)
157 goto out_free;
158 }
159 spin_lock_init(&iv->lock);
160 return iv;
161
162out_free:
163 kfree(iv->ptr);
164 kfree(iv->bitlock);
165 kfree(iv->avail);
166 kfree(iv->vector);
167 kfree(iv);
168out:
169 return NULL;
170}
171EXPORT_SYMBOL(airq_iv_create);
172
173/**
174 * airq_iv_release - release an interrupt vector
175 * @iv: pointer to interrupt vector structure
176 */
177void airq_iv_release(struct airq_iv *iv)
178{
179 kfree(iv->data);
180 kfree(iv->ptr);
181 kfree(iv->bitlock);
182 kfree(iv->vector);
183 kfree(iv->avail);
184 kfree(iv);
185}
186EXPORT_SYMBOL(airq_iv_release);
187
188/**
Martin Schwidefskyfe7c30a2014-02-13 13:02:32 +0100189 * airq_iv_alloc - allocate irq bits from an interrupt vector
Martin Schwidefskya9a6f032013-06-25 14:17:57 +0200190 * @iv: pointer to an interrupt vector structure
Martin Schwidefskyfe7c30a2014-02-13 13:02:32 +0100191 * @num: number of consecutive irq bits to allocate
Martin Schwidefskya9a6f032013-06-25 14:17:57 +0200192 *
Martin Schwidefskyfe7c30a2014-02-13 13:02:32 +0100193 * 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
195 * specified
Martin Schwidefskya9a6f032013-06-25 14:17:57 +0200196 */
Martin Schwidefskyfe7c30a2014-02-13 13:02:32 +0100197unsigned long airq_iv_alloc(struct airq_iv *iv, unsigned long num)
Martin Schwidefskya9a6f032013-06-25 14:17:57 +0200198{
Martin Schwidefskyfe7c30a2014-02-13 13:02:32 +0100199 unsigned long bit, i;
Martin Schwidefskya9a6f032013-06-25 14:17:57 +0200200
Martin Schwidefskyfe7c30a2014-02-13 13:02:32 +0100201 if (!iv->avail || num == 0)
Martin Schwidefskya9a6f032013-06-25 14:17:57 +0200202 return -1UL;
203 spin_lock(&iv->lock);
Heiko Carstens7d7c7b22013-09-23 12:01:44 +0200204 bit = find_first_bit_inv(iv->avail, iv->bits);
Martin Schwidefskyfe7c30a2014-02-13 13:02:32 +0100205 while (bit + num <= iv->bits) {
206 for (i = 1; i < num; i++)
207 if (!test_bit_inv(bit + i, iv->avail))
208 break;
209 if (i >= num) {
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;
215 break;
216 }
217 bit = find_next_bit_inv(iv->avail, iv->bits, bit + i + 1);
218 }
219 if (bit + num > iv->bits)
Martin Schwidefskya9a6f032013-06-25 14:17:57 +0200220 bit = -1UL;
221 spin_unlock(&iv->lock);
222 return bit;
223
224}
Martin Schwidefskyfe7c30a2014-02-13 13:02:32 +0100225EXPORT_SYMBOL(airq_iv_alloc);
Martin Schwidefskya9a6f032013-06-25 14:17:57 +0200226
227/**
Martin Schwidefskyfe7c30a2014-02-13 13:02:32 +0100228 * airq_iv_free - free irq bits of an interrupt vector
Martin Schwidefskya9a6f032013-06-25 14:17:57 +0200229 * @iv: pointer to interrupt vector structure
Martin Schwidefskyfe7c30a2014-02-13 13:02:32 +0100230 * @bit: number of the first irq bit to free
231 * @num: number of consecutive irq bits to free
Martin Schwidefskya9a6f032013-06-25 14:17:57 +0200232 */
Martin Schwidefskyfe7c30a2014-02-13 13:02:32 +0100233void airq_iv_free(struct airq_iv *iv, unsigned long bit, unsigned long num)
Martin Schwidefskya9a6f032013-06-25 14:17:57 +0200234{
Martin Schwidefskyfe7c30a2014-02-13 13:02:32 +0100235 unsigned long i;
236
237 if (!iv->avail || num == 0)
Martin Schwidefskya9a6f032013-06-25 14:17:57 +0200238 return;
239 spin_lock(&iv->lock);
Martin Schwidefskyfe7c30a2014-02-13 13:02:32 +0100240 for (i = 0; i < num; i++) {
241 /* Clear (possibly left over) interrupt bit */
242 clear_bit_inv(bit + i, iv->vector);
243 /* Make the bit positions available again */
244 set_bit_inv(bit + i, iv->avail);
245 }
246 if (bit + num >= iv->end) {
Martin Schwidefskya9a6f032013-06-25 14:17:57 +0200247 /* Find new end of bit-field */
Martin Schwidefskyfe7c30a2014-02-13 13:02:32 +0100248 while (iv->end > 0 && !test_bit_inv(iv->end - 1, iv->avail))
249 iv->end--;
Martin Schwidefskya9a6f032013-06-25 14:17:57 +0200250 }
251 spin_unlock(&iv->lock);
252}
Martin Schwidefskyfe7c30a2014-02-13 13:02:32 +0100253EXPORT_SYMBOL(airq_iv_free);
Martin Schwidefskya9a6f032013-06-25 14:17:57 +0200254
255/**
256 * airq_iv_scan - scan interrupt vector for non-zero bits
257 * @iv: pointer to interrupt vector structure
258 * @start: bit number to start the search
259 * @end: bit number to end the search
260 *
261 * Returns the bit number of the next non-zero interrupt bit, or
262 * -1UL if the scan completed without finding any more any non-zero bits.
263 */
264unsigned long airq_iv_scan(struct airq_iv *iv, unsigned long start,
265 unsigned long end)
266{
Martin Schwidefskya9a6f032013-06-25 14:17:57 +0200267 unsigned long bit;
268
269 /* Find non-zero bit starting from 'ivs->next'. */
Heiko Carstens7d7c7b22013-09-23 12:01:44 +0200270 bit = find_next_bit_inv(iv->vector, end, start);
Martin Schwidefskya9a6f032013-06-25 14:17:57 +0200271 if (bit >= end)
272 return -1UL;
Heiko Carstens7d7c7b22013-09-23 12:01:44 +0200273 clear_bit_inv(bit, iv->vector);
Martin Schwidefskya9a6f032013-06-25 14:17:57 +0200274 return bit;
275}
276EXPORT_SYMBOL(airq_iv_scan);