airq.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. * Support for adapter interruptions
  3. *
  4. * Copyright IBM Corp. 1999, 2007
  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>
  9. */
  10. #include <linux/init.h>
  11. #include <linux/irq.h>
  12. #include <linux/kernel_stat.h>
  13. #include <linux/module.h>
  14. #include <linux/mutex.h>
  15. #include <linux/rculist.h>
  16. #include <linux/slab.h>
  17. #include <asm/airq.h>
  18. #include <asm/isc.h>
  19. #include "cio.h"
  20. #include "cio_debug.h"
  21. #include "ioasm.h"
  22. static DEFINE_SPINLOCK(airq_lists_lock);
  23. static struct hlist_head airq_lists[MAX_ISC+1];
  24. /**
  25. * register_adapter_interrupt() - register adapter interrupt handler
  26. * @airq: pointer to adapter interrupt descriptor
  27. *
  28. * Returns 0 on success, or -EINVAL.
  29. */
  30. int register_adapter_interrupt(struct airq_struct *airq)
  31. {
  32. char dbf_txt[32];
  33. if (!airq->handler || airq->isc > MAX_ISC)
  34. return -EINVAL;
  35. if (!airq->lsi_ptr) {
  36. airq->lsi_ptr = kzalloc(1, GFP_KERNEL);
  37. if (!airq->lsi_ptr)
  38. return -ENOMEM;
  39. airq->flags |= AIRQ_PTR_ALLOCATED;
  40. }
  41. if (!airq->lsi_mask)
  42. airq->lsi_mask = 0xff;
  43. snprintf(dbf_txt, sizeof(dbf_txt), "rairq:%p", airq);
  44. CIO_TRACE_EVENT(4, dbf_txt);
  45. isc_register(airq->isc);
  46. spin_lock(&airq_lists_lock);
  47. hlist_add_head_rcu(&airq->list, &airq_lists[airq->isc]);
  48. spin_unlock(&airq_lists_lock);
  49. return 0;
  50. }
  51. EXPORT_SYMBOL(register_adapter_interrupt);
  52. /**
  53. * unregister_adapter_interrupt - unregister adapter interrupt handler
  54. * @airq: pointer to adapter interrupt descriptor
  55. */
  56. void unregister_adapter_interrupt(struct airq_struct *airq)
  57. {
  58. char dbf_txt[32];
  59. if (hlist_unhashed(&airq->list))
  60. return;
  61. snprintf(dbf_txt, sizeof(dbf_txt), "urairq:%p", airq);
  62. CIO_TRACE_EVENT(4, dbf_txt);
  63. spin_lock(&airq_lists_lock);
  64. hlist_del_rcu(&airq->list);
  65. spin_unlock(&airq_lists_lock);
  66. synchronize_rcu();
  67. isc_unregister(airq->isc);
  68. if (airq->flags & AIRQ_PTR_ALLOCATED) {
  69. kfree(airq->lsi_ptr);
  70. airq->lsi_ptr = NULL;
  71. airq->flags &= ~AIRQ_PTR_ALLOCATED;
  72. }
  73. }
  74. EXPORT_SYMBOL(unregister_adapter_interrupt);
  75. void do_adapter_IO(u8 isc)
  76. {
  77. struct airq_struct *airq;
  78. struct hlist_head *head;
  79. head = &airq_lists[isc];
  80. rcu_read_lock();
  81. hlist_for_each_entry_rcu(airq, head, list)
  82. if ((*airq->lsi_ptr & airq->lsi_mask) != 0)
  83. airq->handler(airq);
  84. rcu_read_unlock();
  85. }
  86. /**
  87. * airq_iv_create - create an interrupt vector
  88. * @bits: number of bits in the interrupt vector
  89. * @flags: allocation flags
  90. *
  91. * Returns a pointer to an interrupt vector structure
  92. */
  93. struct airq_iv *airq_iv_create(unsigned long bits, unsigned long flags)
  94. {
  95. struct airq_iv *iv;
  96. unsigned long size;
  97. iv = kzalloc(sizeof(*iv), GFP_KERNEL);
  98. if (!iv)
  99. goto out;
  100. iv->bits = bits;
  101. size = BITS_TO_LONGS(bits) * sizeof(unsigned long);
  102. iv->vector = kzalloc(size, GFP_KERNEL);
  103. if (!iv->vector)
  104. goto out_free;
  105. if (flags & AIRQ_IV_ALLOC) {
  106. iv->avail = kmalloc(size, GFP_KERNEL);
  107. if (!iv->avail)
  108. goto out_free;
  109. memset(iv->avail, 0xff, size);
  110. iv->end = 0;
  111. } else
  112. iv->end = bits;
  113. if (flags & AIRQ_IV_BITLOCK) {
  114. iv->bitlock = kzalloc(size, GFP_KERNEL);
  115. if (!iv->bitlock)
  116. goto out_free;
  117. }
  118. if (flags & AIRQ_IV_PTR) {
  119. size = bits * sizeof(unsigned long);
  120. iv->ptr = kzalloc(size, GFP_KERNEL);
  121. if (!iv->ptr)
  122. goto out_free;
  123. }
  124. if (flags & AIRQ_IV_DATA) {
  125. size = bits * sizeof(unsigned int);
  126. iv->data = kzalloc(size, GFP_KERNEL);
  127. if (!iv->data)
  128. goto out_free;
  129. }
  130. spin_lock_init(&iv->lock);
  131. return iv;
  132. out_free:
  133. kfree(iv->ptr);
  134. kfree(iv->bitlock);
  135. kfree(iv->avail);
  136. kfree(iv->vector);
  137. kfree(iv);
  138. out:
  139. return NULL;
  140. }
  141. EXPORT_SYMBOL(airq_iv_create);
  142. /**
  143. * airq_iv_release - release an interrupt vector
  144. * @iv: pointer to interrupt vector structure
  145. */
  146. void airq_iv_release(struct airq_iv *iv)
  147. {
  148. kfree(iv->data);
  149. kfree(iv->ptr);
  150. kfree(iv->bitlock);
  151. kfree(iv->vector);
  152. kfree(iv->avail);
  153. kfree(iv);
  154. }
  155. EXPORT_SYMBOL(airq_iv_release);
  156. /**
  157. * airq_iv_alloc_bit - allocate an irq bit from an interrupt vector
  158. * @iv: pointer to an interrupt vector structure
  159. *
  160. * Returns the bit number of the allocated irq, or -1UL if no bit
  161. * is available or the AIRQ_IV_ALLOC flag has not been specified
  162. */
  163. unsigned long airq_iv_alloc_bit(struct airq_iv *iv)
  164. {
  165. const unsigned long be_to_le = BITS_PER_LONG - 1;
  166. unsigned long bit;
  167. if (!iv->avail)
  168. return -1UL;
  169. spin_lock(&iv->lock);
  170. bit = find_first_bit_left(iv->avail, iv->bits);
  171. if (bit < iv->bits) {
  172. clear_bit(bit ^ be_to_le, iv->avail);
  173. if (bit >= iv->end)
  174. iv->end = bit + 1;
  175. } else
  176. bit = -1UL;
  177. spin_unlock(&iv->lock);
  178. return bit;
  179. }
  180. EXPORT_SYMBOL(airq_iv_alloc_bit);
  181. /**
  182. * airq_iv_free_bit - free an irq bit of an interrupt vector
  183. * @iv: pointer to interrupt vector structure
  184. * @bit: number of the irq bit to free
  185. */
  186. void airq_iv_free_bit(struct airq_iv *iv, unsigned long bit)
  187. {
  188. const unsigned long be_to_le = BITS_PER_LONG - 1;
  189. if (!iv->avail)
  190. return;
  191. spin_lock(&iv->lock);
  192. /* Clear (possibly left over) interrupt bit */
  193. clear_bit(bit ^ be_to_le, iv->vector);
  194. /* Make the bit position available again */
  195. set_bit(bit ^ be_to_le, iv->avail);
  196. if (bit == iv->end - 1) {
  197. /* Find new end of bit-field */
  198. while (--iv->end > 0)
  199. if (!test_bit((iv->end - 1) ^ be_to_le, iv->avail))
  200. break;
  201. }
  202. spin_unlock(&iv->lock);
  203. }
  204. EXPORT_SYMBOL(airq_iv_free_bit);
  205. /**
  206. * airq_iv_scan - scan interrupt vector for non-zero bits
  207. * @iv: pointer to interrupt vector structure
  208. * @start: bit number to start the search
  209. * @end: bit number to end the search
  210. *
  211. * Returns the bit number of the next non-zero interrupt bit, or
  212. * -1UL if the scan completed without finding any more any non-zero bits.
  213. */
  214. unsigned long airq_iv_scan(struct airq_iv *iv, unsigned long start,
  215. unsigned long end)
  216. {
  217. const unsigned long be_to_le = BITS_PER_LONG - 1;
  218. unsigned long bit;
  219. /* Find non-zero bit starting from 'ivs->next'. */
  220. bit = find_next_bit_left(iv->vector, end, start);
  221. if (bit >= end)
  222. return -1UL;
  223. /* Clear interrupt bit (find left uses big-endian bit numbers) */
  224. clear_bit(bit ^ be_to_le, iv->vector);
  225. return bit;
  226. }
  227. EXPORT_SYMBOL(airq_iv_scan);