Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Peter Oberparleiter | 4e8e56c | 2008-01-26 14:10:44 +0100 | [diff] [blame] | 2 | * Support for adapter interruptions |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
Heiko Carstens | a53c8fa | 2012-07-20 11:15:04 +0200 | [diff] [blame] | 4 | * Copyright IBM Corp. 1999, 2007 |
Peter Oberparleiter | 4e8e56c | 2008-01-26 14:10:44 +0100 | [diff] [blame] | 5 | * 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #include <linux/init.h> |
Martin Schwidefsky | f4eae94 | 2013-06-24 10:30:41 +0200 | [diff] [blame] | 12 | #include <linux/irq.h> |
| 13 | #include <linux/kernel_stat.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/module.h> |
Martin Schwidefsky | f4eae94 | 2013-06-24 10:30:41 +0200 | [diff] [blame] | 15 | #include <linux/mutex.h> |
| 16 | #include <linux/rculist.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | |
Peter Oberparleiter | 4e8e56c | 2008-01-26 14:10:44 +0100 | [diff] [blame] | 19 | #include <asm/airq.h> |
Cornelia Huck | da7c5af | 2008-07-14 09:58:59 +0200 | [diff] [blame] | 20 | #include <asm/isc.h> |
Peter Oberparleiter | 4e8e56c | 2008-01-26 14:10:44 +0100 | [diff] [blame] | 21 | |
| 22 | #include "cio.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include "cio_debug.h" |
Martin Schwidefsky | f4eae94 | 2013-06-24 10:30:41 +0200 | [diff] [blame] | 24 | #include "ioasm.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | |
Martin Schwidefsky | f4eae94 | 2013-06-24 10:30:41 +0200 | [diff] [blame] | 26 | static DEFINE_SPINLOCK(airq_lists_lock); |
| 27 | static struct hlist_head airq_lists[MAX_ISC+1]; |
Peter Oberparleiter | 4e8e56c | 2008-01-26 14:10:44 +0100 | [diff] [blame] | 28 | |
| 29 | /** |
Martin Schwidefsky | f4eae94 | 2013-06-24 10:30:41 +0200 | [diff] [blame] | 30 | * register_adapter_interrupt() - register adapter interrupt handler |
| 31 | * @airq: pointer to adapter interrupt descriptor |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | * |
Martin Schwidefsky | f4eae94 | 2013-06-24 10:30:41 +0200 | [diff] [blame] | 33 | * Returns 0 on success, or -EINVAL. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | */ |
Martin Schwidefsky | f4eae94 | 2013-06-24 10:30:41 +0200 | [diff] [blame] | 35 | int register_adapter_interrupt(struct airq_struct *airq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | { |
Martin Schwidefsky | f4eae94 | 2013-06-24 10:30:41 +0200 | [diff] [blame] | 37 | char dbf_txt[32]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | |
Martin Schwidefsky | f4eae94 | 2013-06-24 10:30:41 +0200 | [diff] [blame] | 39 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | } |
Martin Schwidefsky | f4eae94 | 2013-06-24 10:30:41 +0200 | [diff] [blame] | 47 | if (!airq->lsi_mask) |
| 48 | airq->lsi_mask = 0xff; |
| 49 | snprintf(dbf_txt, sizeof(dbf_txt), "rairq:%p", airq); |
Peter Oberparleiter | 4e8e56c | 2008-01-26 14:10:44 +0100 | [diff] [blame] | 50 | CIO_TRACE_EVENT(4, dbf_txt); |
Martin Schwidefsky | f4eae94 | 2013-06-24 10:30:41 +0200 | [diff] [blame] | 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); |
| 55 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | } |
Martin Schwidefsky | f4eae94 | 2013-06-24 10:30:41 +0200 | [diff] [blame] | 57 | EXPORT_SYMBOL(register_adapter_interrupt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | |
Peter Oberparleiter | 4e8e56c | 2008-01-26 14:10:44 +0100 | [diff] [blame] | 59 | /** |
Martin Schwidefsky | f4eae94 | 2013-06-24 10:30:41 +0200 | [diff] [blame] | 60 | * unregister_adapter_interrupt - unregister adapter interrupt handler |
| 61 | * @airq: pointer to adapter interrupt descriptor |
Peter Oberparleiter | 4e8e56c | 2008-01-26 14:10:44 +0100 | [diff] [blame] | 62 | */ |
Martin Schwidefsky | f4eae94 | 2013-06-24 10:30:41 +0200 | [diff] [blame] | 63 | void unregister_adapter_interrupt(struct airq_struct *airq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | { |
Martin Schwidefsky | f4eae94 | 2013-06-24 10:30:41 +0200 | [diff] [blame] | 65 | char dbf_txt[32]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | |
Martin Schwidefsky | f4eae94 | 2013-06-24 10:30:41 +0200 | [diff] [blame] | 67 | if (hlist_unhashed(&airq->list)) |
| 68 | return; |
| 69 | snprintf(dbf_txt, sizeof(dbf_txt), "urairq:%p", airq); |
Peter Oberparleiter | 4e8e56c | 2008-01-26 14:10:44 +0100 | [diff] [blame] | 70 | CIO_TRACE_EVENT(4, dbf_txt); |
Martin Schwidefsky | f4eae94 | 2013-06-24 10:30:41 +0200 | [diff] [blame] | 71 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | } |
Martin Schwidefsky | f4eae94 | 2013-06-24 10:30:41 +0200 | [diff] [blame] | 82 | EXPORT_SYMBOL(unregister_adapter_interrupt); |
Peter Oberparleiter | 4e8e56c | 2008-01-26 14:10:44 +0100 | [diff] [blame] | 83 | |
Martin Schwidefsky | 1f44a22 | 2013-06-27 09:01:09 +0200 | [diff] [blame] | 84 | static irqreturn_t do_airq_interrupt(int irq, void *dummy) |
Peter Oberparleiter | 4e8e56c | 2008-01-26 14:10:44 +0100 | [diff] [blame] | 85 | { |
Martin Schwidefsky | 1f44a22 | 2013-06-27 09:01:09 +0200 | [diff] [blame] | 86 | struct tpi_info *tpi_info; |
Martin Schwidefsky | f4eae94 | 2013-06-24 10:30:41 +0200 | [diff] [blame] | 87 | struct airq_struct *airq; |
| 88 | struct hlist_head *head; |
Peter Oberparleiter | 4e8e56c | 2008-01-26 14:10:44 +0100 | [diff] [blame] | 89 | |
Martin Schwidefsky | fe0f497 | 2014-09-30 17:37:52 +0200 | [diff] [blame] | 90 | set_cpu_flag(CIF_NOHZ_DELAY); |
Martin Schwidefsky | 1f44a22 | 2013-06-27 09:01:09 +0200 | [diff] [blame] | 91 | tpi_info = (struct tpi_info *) &get_irq_regs()->int_code; |
Peter Oberparleiter | 4224897 | 2015-12-18 12:59:36 +0100 | [diff] [blame] | 92 | trace_s390_cio_adapter_int(tpi_info); |
Martin Schwidefsky | 1f44a22 | 2013-06-27 09:01:09 +0200 | [diff] [blame] | 93 | head = &airq_lists[tpi_info->isc]; |
Martin Schwidefsky | f4eae94 | 2013-06-24 10:30:41 +0200 | [diff] [blame] | 94 | rcu_read_lock(); |
| 95 | hlist_for_each_entry_rcu(airq, head, list) |
| 96 | if ((*airq->lsi_ptr & airq->lsi_mask) != 0) |
| 97 | airq->handler(airq); |
| 98 | rcu_read_unlock(); |
Martin Schwidefsky | 1f44a22 | 2013-06-27 09:01:09 +0200 | [diff] [blame] | 99 | |
| 100 | return IRQ_HANDLED; |
| 101 | } |
| 102 | |
| 103 | static struct irqaction airq_interrupt = { |
| 104 | .name = "AIO", |
| 105 | .handler = do_airq_interrupt, |
| 106 | }; |
| 107 | |
| 108 | void __init init_airq_interrupts(void) |
| 109 | { |
| 110 | irq_set_chip_and_handler(THIN_INTERRUPT, |
| 111 | &dummy_irq_chip, handle_percpu_irq); |
| 112 | setup_irq(THIN_INTERRUPT, &airq_interrupt); |
Peter Oberparleiter | 4e8e56c | 2008-01-26 14:10:44 +0100 | [diff] [blame] | 113 | } |
Martin Schwidefsky | a9a6f03 | 2013-06-25 14:17:57 +0200 | [diff] [blame] | 114 | |
| 115 | /** |
| 116 | * airq_iv_create - create an interrupt vector |
| 117 | * @bits: number of bits in the interrupt vector |
| 118 | * @flags: allocation flags |
| 119 | * |
| 120 | * Returns a pointer to an interrupt vector structure |
| 121 | */ |
| 122 | struct airq_iv *airq_iv_create(unsigned long bits, unsigned long flags) |
| 123 | { |
| 124 | struct airq_iv *iv; |
| 125 | unsigned long size; |
| 126 | |
| 127 | iv = kzalloc(sizeof(*iv), GFP_KERNEL); |
| 128 | if (!iv) |
| 129 | goto out; |
| 130 | iv->bits = bits; |
| 131 | size = BITS_TO_LONGS(bits) * sizeof(unsigned long); |
| 132 | iv->vector = kzalloc(size, GFP_KERNEL); |
| 133 | if (!iv->vector) |
| 134 | goto out_free; |
| 135 | if (flags & AIRQ_IV_ALLOC) { |
| 136 | iv->avail = kmalloc(size, GFP_KERNEL); |
| 137 | if (!iv->avail) |
| 138 | goto out_free; |
| 139 | memset(iv->avail, 0xff, size); |
| 140 | iv->end = 0; |
| 141 | } else |
| 142 | iv->end = bits; |
| 143 | if (flags & AIRQ_IV_BITLOCK) { |
| 144 | iv->bitlock = kzalloc(size, GFP_KERNEL); |
| 145 | if (!iv->bitlock) |
| 146 | goto out_free; |
| 147 | } |
| 148 | if (flags & AIRQ_IV_PTR) { |
| 149 | size = bits * sizeof(unsigned long); |
| 150 | iv->ptr = kzalloc(size, GFP_KERNEL); |
| 151 | if (!iv->ptr) |
| 152 | goto out_free; |
| 153 | } |
| 154 | if (flags & AIRQ_IV_DATA) { |
| 155 | size = bits * sizeof(unsigned int); |
| 156 | iv->data = kzalloc(size, GFP_KERNEL); |
| 157 | if (!iv->data) |
| 158 | goto out_free; |
| 159 | } |
| 160 | spin_lock_init(&iv->lock); |
| 161 | return iv; |
| 162 | |
| 163 | out_free: |
| 164 | kfree(iv->ptr); |
| 165 | kfree(iv->bitlock); |
| 166 | kfree(iv->avail); |
| 167 | kfree(iv->vector); |
| 168 | kfree(iv); |
| 169 | out: |
| 170 | return NULL; |
| 171 | } |
| 172 | EXPORT_SYMBOL(airq_iv_create); |
| 173 | |
| 174 | /** |
| 175 | * airq_iv_release - release an interrupt vector |
| 176 | * @iv: pointer to interrupt vector structure |
| 177 | */ |
| 178 | void airq_iv_release(struct airq_iv *iv) |
| 179 | { |
| 180 | kfree(iv->data); |
| 181 | kfree(iv->ptr); |
| 182 | kfree(iv->bitlock); |
| 183 | kfree(iv->vector); |
| 184 | kfree(iv->avail); |
| 185 | kfree(iv); |
| 186 | } |
| 187 | EXPORT_SYMBOL(airq_iv_release); |
| 188 | |
| 189 | /** |
Martin Schwidefsky | fe7c30a | 2014-02-13 13:02:32 +0100 | [diff] [blame] | 190 | * airq_iv_alloc - allocate irq bits from an interrupt vector |
Martin Schwidefsky | a9a6f03 | 2013-06-25 14:17:57 +0200 | [diff] [blame] | 191 | * @iv: pointer to an interrupt vector structure |
Martin Schwidefsky | fe7c30a | 2014-02-13 13:02:32 +0100 | [diff] [blame] | 192 | * @num: number of consecutive irq bits to allocate |
Martin Schwidefsky | a9a6f03 | 2013-06-25 14:17:57 +0200 | [diff] [blame] | 193 | * |
Martin Schwidefsky | fe7c30a | 2014-02-13 13:02:32 +0100 | [diff] [blame] | 194 | * Returns the bit number of the first irq in the allocated block of irqs, |
| 195 | * or -1UL if no bit is available or the AIRQ_IV_ALLOC flag has not been |
| 196 | * specified |
Martin Schwidefsky | a9a6f03 | 2013-06-25 14:17:57 +0200 | [diff] [blame] | 197 | */ |
Martin Schwidefsky | fe7c30a | 2014-02-13 13:02:32 +0100 | [diff] [blame] | 198 | unsigned long airq_iv_alloc(struct airq_iv *iv, unsigned long num) |
Martin Schwidefsky | a9a6f03 | 2013-06-25 14:17:57 +0200 | [diff] [blame] | 199 | { |
Sebastian Ott | 0eb69a0 | 2014-06-05 14:30:19 +0200 | [diff] [blame] | 200 | unsigned long bit, i, flags; |
Martin Schwidefsky | a9a6f03 | 2013-06-25 14:17:57 +0200 | [diff] [blame] | 201 | |
Martin Schwidefsky | fe7c30a | 2014-02-13 13:02:32 +0100 | [diff] [blame] | 202 | if (!iv->avail || num == 0) |
Martin Schwidefsky | a9a6f03 | 2013-06-25 14:17:57 +0200 | [diff] [blame] | 203 | return -1UL; |
Sebastian Ott | 0eb69a0 | 2014-06-05 14:30:19 +0200 | [diff] [blame] | 204 | spin_lock_irqsave(&iv->lock, flags); |
Heiko Carstens | 7d7c7b2 | 2013-09-23 12:01:44 +0200 | [diff] [blame] | 205 | bit = find_first_bit_inv(iv->avail, iv->bits); |
Martin Schwidefsky | fe7c30a | 2014-02-13 13:02:32 +0100 | [diff] [blame] | 206 | while (bit + num <= iv->bits) { |
| 207 | for (i = 1; i < num; i++) |
| 208 | if (!test_bit_inv(bit + i, iv->avail)) |
| 209 | break; |
| 210 | if (i >= num) { |
| 211 | /* Found a suitable block of irqs */ |
| 212 | for (i = 0; i < num; i++) |
| 213 | clear_bit_inv(bit + i, iv->avail); |
| 214 | if (bit + num >= iv->end) |
| 215 | iv->end = bit + num + 1; |
| 216 | break; |
| 217 | } |
| 218 | bit = find_next_bit_inv(iv->avail, iv->bits, bit + i + 1); |
| 219 | } |
| 220 | if (bit + num > iv->bits) |
Martin Schwidefsky | a9a6f03 | 2013-06-25 14:17:57 +0200 | [diff] [blame] | 221 | bit = -1UL; |
Sebastian Ott | 0eb69a0 | 2014-06-05 14:30:19 +0200 | [diff] [blame] | 222 | spin_unlock_irqrestore(&iv->lock, flags); |
Martin Schwidefsky | a9a6f03 | 2013-06-25 14:17:57 +0200 | [diff] [blame] | 223 | return bit; |
Martin Schwidefsky | a9a6f03 | 2013-06-25 14:17:57 +0200 | [diff] [blame] | 224 | } |
Martin Schwidefsky | fe7c30a | 2014-02-13 13:02:32 +0100 | [diff] [blame] | 225 | EXPORT_SYMBOL(airq_iv_alloc); |
Martin Schwidefsky | a9a6f03 | 2013-06-25 14:17:57 +0200 | [diff] [blame] | 226 | |
| 227 | /** |
Martin Schwidefsky | fe7c30a | 2014-02-13 13:02:32 +0100 | [diff] [blame] | 228 | * airq_iv_free - free irq bits of an interrupt vector |
Martin Schwidefsky | a9a6f03 | 2013-06-25 14:17:57 +0200 | [diff] [blame] | 229 | * @iv: pointer to interrupt vector structure |
Martin Schwidefsky | fe7c30a | 2014-02-13 13:02:32 +0100 | [diff] [blame] | 230 | * @bit: number of the first irq bit to free |
| 231 | * @num: number of consecutive irq bits to free |
Martin Schwidefsky | a9a6f03 | 2013-06-25 14:17:57 +0200 | [diff] [blame] | 232 | */ |
Martin Schwidefsky | fe7c30a | 2014-02-13 13:02:32 +0100 | [diff] [blame] | 233 | void airq_iv_free(struct airq_iv *iv, unsigned long bit, unsigned long num) |
Martin Schwidefsky | a9a6f03 | 2013-06-25 14:17:57 +0200 | [diff] [blame] | 234 | { |
Sebastian Ott | 0eb69a0 | 2014-06-05 14:30:19 +0200 | [diff] [blame] | 235 | unsigned long i, flags; |
Martin Schwidefsky | fe7c30a | 2014-02-13 13:02:32 +0100 | [diff] [blame] | 236 | |
| 237 | if (!iv->avail || num == 0) |
Martin Schwidefsky | a9a6f03 | 2013-06-25 14:17:57 +0200 | [diff] [blame] | 238 | return; |
Sebastian Ott | 0eb69a0 | 2014-06-05 14:30:19 +0200 | [diff] [blame] | 239 | spin_lock_irqsave(&iv->lock, flags); |
Martin Schwidefsky | fe7c30a | 2014-02-13 13:02:32 +0100 | [diff] [blame] | 240 | 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 Schwidefsky | a9a6f03 | 2013-06-25 14:17:57 +0200 | [diff] [blame] | 247 | /* Find new end of bit-field */ |
Martin Schwidefsky | fe7c30a | 2014-02-13 13:02:32 +0100 | [diff] [blame] | 248 | while (iv->end > 0 && !test_bit_inv(iv->end - 1, iv->avail)) |
| 249 | iv->end--; |
Martin Schwidefsky | a9a6f03 | 2013-06-25 14:17:57 +0200 | [diff] [blame] | 250 | } |
Sebastian Ott | 0eb69a0 | 2014-06-05 14:30:19 +0200 | [diff] [blame] | 251 | spin_unlock_irqrestore(&iv->lock, flags); |
Martin Schwidefsky | a9a6f03 | 2013-06-25 14:17:57 +0200 | [diff] [blame] | 252 | } |
Martin Schwidefsky | fe7c30a | 2014-02-13 13:02:32 +0100 | [diff] [blame] | 253 | EXPORT_SYMBOL(airq_iv_free); |
Martin Schwidefsky | a9a6f03 | 2013-06-25 14:17:57 +0200 | [diff] [blame] | 254 | |
| 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 | */ |
| 264 | unsigned long airq_iv_scan(struct airq_iv *iv, unsigned long start, |
| 265 | unsigned long end) |
| 266 | { |
Martin Schwidefsky | a9a6f03 | 2013-06-25 14:17:57 +0200 | [diff] [blame] | 267 | unsigned long bit; |
| 268 | |
| 269 | /* Find non-zero bit starting from 'ivs->next'. */ |
Heiko Carstens | 7d7c7b2 | 2013-09-23 12:01:44 +0200 | [diff] [blame] | 270 | bit = find_next_bit_inv(iv->vector, end, start); |
Martin Schwidefsky | a9a6f03 | 2013-06-25 14:17:57 +0200 | [diff] [blame] | 271 | if (bit >= end) |
| 272 | return -1UL; |
Heiko Carstens | 7d7c7b2 | 2013-09-23 12:01:44 +0200 | [diff] [blame] | 273 | clear_bit_inv(bit, iv->vector); |
Martin Schwidefsky | a9a6f03 | 2013-06-25 14:17:57 +0200 | [diff] [blame] | 274 | return bit; |
| 275 | } |
| 276 | EXPORT_SYMBOL(airq_iv_scan); |