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 | 1f44a22 | 2013-06-27 09:01:09 +0200 | [diff] [blame^] | 90 | __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 Schwidefsky | f4eae94 | 2013-06-24 10:30:41 +0200 | [diff] [blame] | 93 | 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 Schwidefsky | 1f44a22 | 2013-06-27 09:01:09 +0200 | [diff] [blame^] | 98 | |
| 99 | return IRQ_HANDLED; |
| 100 | } |
| 101 | |
| 102 | static struct irqaction airq_interrupt = { |
| 103 | .name = "AIO", |
| 104 | .handler = do_airq_interrupt, |
| 105 | }; |
| 106 | |
| 107 | void __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 Oberparleiter | 4e8e56c | 2008-01-26 14:10:44 +0100 | [diff] [blame] | 112 | } |
Martin Schwidefsky | a9a6f03 | 2013-06-25 14:17:57 +0200 | [diff] [blame] | 113 | |
| 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 | */ |
| 121 | struct 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 | |
| 162 | out_free: |
| 163 | kfree(iv->ptr); |
| 164 | kfree(iv->bitlock); |
| 165 | kfree(iv->avail); |
| 166 | kfree(iv->vector); |
| 167 | kfree(iv); |
| 168 | out: |
| 169 | return NULL; |
| 170 | } |
| 171 | EXPORT_SYMBOL(airq_iv_create); |
| 172 | |
| 173 | /** |
| 174 | * airq_iv_release - release an interrupt vector |
| 175 | * @iv: pointer to interrupt vector structure |
| 176 | */ |
| 177 | void 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 | } |
| 186 | EXPORT_SYMBOL(airq_iv_release); |
| 187 | |
| 188 | /** |
| 189 | * airq_iv_alloc_bit - allocate an irq bit from an interrupt vector |
| 190 | * @iv: pointer to an interrupt vector structure |
| 191 | * |
| 192 | * Returns the bit number of the allocated irq, or -1UL if no bit |
| 193 | * is available or the AIRQ_IV_ALLOC flag has not been specified |
| 194 | */ |
| 195 | unsigned long airq_iv_alloc_bit(struct airq_iv *iv) |
| 196 | { |
| 197 | const unsigned long be_to_le = BITS_PER_LONG - 1; |
| 198 | unsigned long bit; |
| 199 | |
| 200 | if (!iv->avail) |
| 201 | return -1UL; |
| 202 | spin_lock(&iv->lock); |
| 203 | bit = find_first_bit_left(iv->avail, iv->bits); |
| 204 | if (bit < iv->bits) { |
| 205 | clear_bit(bit ^ be_to_le, iv->avail); |
| 206 | if (bit >= iv->end) |
| 207 | iv->end = bit + 1; |
| 208 | } else |
| 209 | bit = -1UL; |
| 210 | spin_unlock(&iv->lock); |
| 211 | return bit; |
| 212 | |
| 213 | } |
| 214 | EXPORT_SYMBOL(airq_iv_alloc_bit); |
| 215 | |
| 216 | /** |
| 217 | * airq_iv_free_bit - free an irq bit of an interrupt vector |
| 218 | * @iv: pointer to interrupt vector structure |
| 219 | * @bit: number of the irq bit to free |
| 220 | */ |
| 221 | void airq_iv_free_bit(struct airq_iv *iv, unsigned long bit) |
| 222 | { |
| 223 | const unsigned long be_to_le = BITS_PER_LONG - 1; |
| 224 | |
| 225 | if (!iv->avail) |
| 226 | return; |
| 227 | spin_lock(&iv->lock); |
| 228 | /* Clear (possibly left over) interrupt bit */ |
| 229 | clear_bit(bit ^ be_to_le, iv->vector); |
| 230 | /* Make the bit position available again */ |
| 231 | set_bit(bit ^ be_to_le, iv->avail); |
| 232 | if (bit == iv->end - 1) { |
| 233 | /* Find new end of bit-field */ |
| 234 | while (--iv->end > 0) |
| 235 | if (!test_bit((iv->end - 1) ^ be_to_le, iv->avail)) |
| 236 | break; |
| 237 | } |
| 238 | spin_unlock(&iv->lock); |
| 239 | } |
| 240 | EXPORT_SYMBOL(airq_iv_free_bit); |
| 241 | |
| 242 | /** |
| 243 | * airq_iv_scan - scan interrupt vector for non-zero bits |
| 244 | * @iv: pointer to interrupt vector structure |
| 245 | * @start: bit number to start the search |
| 246 | * @end: bit number to end the search |
| 247 | * |
| 248 | * Returns the bit number of the next non-zero interrupt bit, or |
| 249 | * -1UL if the scan completed without finding any more any non-zero bits. |
| 250 | */ |
| 251 | unsigned long airq_iv_scan(struct airq_iv *iv, unsigned long start, |
| 252 | unsigned long end) |
| 253 | { |
| 254 | const unsigned long be_to_le = BITS_PER_LONG - 1; |
| 255 | unsigned long bit; |
| 256 | |
| 257 | /* Find non-zero bit starting from 'ivs->next'. */ |
| 258 | bit = find_next_bit_left(iv->vector, end, start); |
| 259 | if (bit >= end) |
| 260 | return -1UL; |
| 261 | /* Clear interrupt bit (find left uses big-endian bit numbers) */ |
| 262 | clear_bit(bit ^ be_to_le, iv->vector); |
| 263 | return bit; |
| 264 | } |
| 265 | EXPORT_SYMBOL(airq_iv_scan); |