airq.c 6.8 KB

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