airq.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. static irqreturn_t do_airq_interrupt(int irq, void *dummy)
  76. {
  77. struct tpi_info *tpi_info;
  78. struct airq_struct *airq;
  79. struct hlist_head *head;
  80. __this_cpu_write(s390_idle.nohz_delay, 1);
  81. tpi_info = (struct tpi_info *) &get_irq_regs()->int_code;
  82. head = &airq_lists[tpi_info->isc];
  83. rcu_read_lock();
  84. hlist_for_each_entry_rcu(airq, head, list)
  85. if ((*airq->lsi_ptr & airq->lsi_mask) != 0)
  86. airq->handler(airq);
  87. rcu_read_unlock();
  88. return IRQ_HANDLED;
  89. }
  90. static struct irqaction airq_interrupt = {
  91. .name = "AIO",
  92. .handler = do_airq_interrupt,
  93. };
  94. void __init init_airq_interrupts(void)
  95. {
  96. irq_set_chip_and_handler(THIN_INTERRUPT,
  97. &dummy_irq_chip, handle_percpu_irq);
  98. setup_irq(THIN_INTERRUPT, &airq_interrupt);
  99. }
  100. /**
  101. * airq_iv_create - create an interrupt vector
  102. * @bits: number of bits in the interrupt vector
  103. * @flags: allocation flags
  104. *
  105. * Returns a pointer to an interrupt vector structure
  106. */
  107. struct airq_iv *airq_iv_create(unsigned long bits, unsigned long flags)
  108. {
  109. struct airq_iv *iv;
  110. unsigned long size;
  111. iv = kzalloc(sizeof(*iv), GFP_KERNEL);
  112. if (!iv)
  113. goto out;
  114. iv->bits = bits;
  115. size = BITS_TO_LONGS(bits) * sizeof(unsigned long);
  116. iv->vector = kzalloc(size, GFP_KERNEL);
  117. if (!iv->vector)
  118. goto out_free;
  119. if (flags & AIRQ_IV_ALLOC) {
  120. iv->avail = kmalloc(size, GFP_KERNEL);
  121. if (!iv->avail)
  122. goto out_free;
  123. memset(iv->avail, 0xff, size);
  124. iv->end = 0;
  125. } else
  126. iv->end = bits;
  127. if (flags & AIRQ_IV_BITLOCK) {
  128. iv->bitlock = kzalloc(size, GFP_KERNEL);
  129. if (!iv->bitlock)
  130. goto out_free;
  131. }
  132. if (flags & AIRQ_IV_PTR) {
  133. size = bits * sizeof(unsigned long);
  134. iv->ptr = kzalloc(size, GFP_KERNEL);
  135. if (!iv->ptr)
  136. goto out_free;
  137. }
  138. if (flags & AIRQ_IV_DATA) {
  139. size = bits * sizeof(unsigned int);
  140. iv->data = kzalloc(size, GFP_KERNEL);
  141. if (!iv->data)
  142. goto out_free;
  143. }
  144. spin_lock_init(&iv->lock);
  145. return iv;
  146. out_free:
  147. kfree(iv->ptr);
  148. kfree(iv->bitlock);
  149. kfree(iv->avail);
  150. kfree(iv->vector);
  151. kfree(iv);
  152. out:
  153. return NULL;
  154. }
  155. EXPORT_SYMBOL(airq_iv_create);
  156. /**
  157. * airq_iv_release - release an interrupt vector
  158. * @iv: pointer to interrupt vector structure
  159. */
  160. void airq_iv_release(struct airq_iv *iv)
  161. {
  162. kfree(iv->data);
  163. kfree(iv->ptr);
  164. kfree(iv->bitlock);
  165. kfree(iv->vector);
  166. kfree(iv->avail);
  167. kfree(iv);
  168. }
  169. EXPORT_SYMBOL(airq_iv_release);
  170. /**
  171. * airq_iv_alloc_bit - allocate an irq bit from an interrupt vector
  172. * @iv: pointer to an interrupt vector structure
  173. *
  174. * Returns the bit number of the allocated irq, or -1UL if no bit
  175. * is available or the AIRQ_IV_ALLOC flag has not been specified
  176. */
  177. unsigned long airq_iv_alloc_bit(struct airq_iv *iv)
  178. {
  179. const unsigned long be_to_le = BITS_PER_LONG - 1;
  180. unsigned long bit;
  181. if (!iv->avail)
  182. return -1UL;
  183. spin_lock(&iv->lock);
  184. bit = find_first_bit_left(iv->avail, iv->bits);
  185. if (bit < iv->bits) {
  186. clear_bit(bit ^ be_to_le, iv->avail);
  187. if (bit >= iv->end)
  188. iv->end = bit + 1;
  189. } else
  190. bit = -1UL;
  191. spin_unlock(&iv->lock);
  192. return bit;
  193. }
  194. EXPORT_SYMBOL(airq_iv_alloc_bit);
  195. /**
  196. * airq_iv_free_bit - free an irq bit of an interrupt vector
  197. * @iv: pointer to interrupt vector structure
  198. * @bit: number of the irq bit to free
  199. */
  200. void airq_iv_free_bit(struct airq_iv *iv, unsigned long bit)
  201. {
  202. const unsigned long be_to_le = BITS_PER_LONG - 1;
  203. if (!iv->avail)
  204. return;
  205. spin_lock(&iv->lock);
  206. /* Clear (possibly left over) interrupt bit */
  207. clear_bit(bit ^ be_to_le, iv->vector);
  208. /* Make the bit position available again */
  209. set_bit(bit ^ be_to_le, iv->avail);
  210. if (bit == iv->end - 1) {
  211. /* Find new end of bit-field */
  212. while (--iv->end > 0)
  213. if (!test_bit((iv->end - 1) ^ be_to_le, iv->avail))
  214. break;
  215. }
  216. spin_unlock(&iv->lock);
  217. }
  218. EXPORT_SYMBOL(airq_iv_free_bit);
  219. /**
  220. * airq_iv_scan - scan interrupt vector for non-zero bits
  221. * @iv: pointer to interrupt vector structure
  222. * @start: bit number to start the search
  223. * @end: bit number to end the search
  224. *
  225. * Returns the bit number of the next non-zero interrupt bit, or
  226. * -1UL if the scan completed without finding any more any non-zero bits.
  227. */
  228. unsigned long airq_iv_scan(struct airq_iv *iv, unsigned long start,
  229. unsigned long end)
  230. {
  231. const unsigned long be_to_le = BITS_PER_LONG - 1;
  232. unsigned long bit;
  233. /* Find non-zero bit starting from 'ivs->next'. */
  234. bit = find_next_bit_left(iv->vector, end, start);
  235. if (bit >= end)
  236. return -1UL;
  237. /* Clear interrupt bit (find left uses big-endian bit numbers) */
  238. clear_bit(bit ^ be_to_le, iv->vector);
  239. return bit;
  240. }
  241. EXPORT_SYMBOL(airq_iv_scan);