airq.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. 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 - allocate irq bits from an interrupt vector
  172. * @iv: pointer to an interrupt vector structure
  173. * @num: number of consecutive irq bits to allocate
  174. *
  175. * Returns the bit number of the first irq in the allocated block of irqs,
  176. * or -1UL if no bit is available or the AIRQ_IV_ALLOC flag has not been
  177. * specified
  178. */
  179. unsigned long airq_iv_alloc(struct airq_iv *iv, unsigned long num)
  180. {
  181. unsigned long bit, i, flags;
  182. if (!iv->avail || num == 0)
  183. return -1UL;
  184. spin_lock_irqsave(&iv->lock, flags);
  185. bit = find_first_bit_inv(iv->avail, iv->bits);
  186. while (bit + num <= iv->bits) {
  187. for (i = 1; i < num; i++)
  188. if (!test_bit_inv(bit + i, iv->avail))
  189. break;
  190. if (i >= num) {
  191. /* Found a suitable block of irqs */
  192. for (i = 0; i < num; i++)
  193. clear_bit_inv(bit + i, iv->avail);
  194. if (bit + num >= iv->end)
  195. iv->end = bit + num + 1;
  196. break;
  197. }
  198. bit = find_next_bit_inv(iv->avail, iv->bits, bit + i + 1);
  199. }
  200. if (bit + num > iv->bits)
  201. bit = -1UL;
  202. spin_unlock_irqrestore(&iv->lock, flags);
  203. return bit;
  204. }
  205. EXPORT_SYMBOL(airq_iv_alloc);
  206. /**
  207. * airq_iv_free - free irq bits of an interrupt vector
  208. * @iv: pointer to interrupt vector structure
  209. * @bit: number of the first irq bit to free
  210. * @num: number of consecutive irq bits to free
  211. */
  212. void airq_iv_free(struct airq_iv *iv, unsigned long bit, unsigned long num)
  213. {
  214. unsigned long i, flags;
  215. if (!iv->avail || num == 0)
  216. return;
  217. spin_lock_irqsave(&iv->lock, flags);
  218. for (i = 0; i < num; i++) {
  219. /* Clear (possibly left over) interrupt bit */
  220. clear_bit_inv(bit + i, iv->vector);
  221. /* Make the bit positions available again */
  222. set_bit_inv(bit + i, iv->avail);
  223. }
  224. if (bit + num >= iv->end) {
  225. /* Find new end of bit-field */
  226. while (iv->end > 0 && !test_bit_inv(iv->end - 1, iv->avail))
  227. iv->end--;
  228. }
  229. spin_unlock_irqrestore(&iv->lock, flags);
  230. }
  231. EXPORT_SYMBOL(airq_iv_free);
  232. /**
  233. * airq_iv_scan - scan interrupt vector for non-zero bits
  234. * @iv: pointer to interrupt vector structure
  235. * @start: bit number to start the search
  236. * @end: bit number to end the search
  237. *
  238. * Returns the bit number of the next non-zero interrupt bit, or
  239. * -1UL if the scan completed without finding any more any non-zero bits.
  240. */
  241. unsigned long airq_iv_scan(struct airq_iv *iv, unsigned long start,
  242. unsigned long end)
  243. {
  244. unsigned long bit;
  245. /* Find non-zero bit starting from 'ivs->next'. */
  246. bit = find_next_bit_inv(iv->vector, end, start);
  247. if (bit >= end)
  248. return -1UL;
  249. clear_bit_inv(bit, iv->vector);
  250. return bit;
  251. }
  252. EXPORT_SYMBOL(airq_iv_scan);