airq.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Support for adapter interruptions
  4. *
  5. * Copyright IBM Corp. 1999, 2007
  6. * Author(s): Ingo Adlung <adlung@de.ibm.com>
  7. * Cornelia Huck <cornelia.huck@de.ibm.com>
  8. * Arnd Bergmann <arndb@de.ibm.com>
  9. * Peter Oberparleiter <peter.oberparleiter@de.ibm.com>
  10. */
  11. #include <linux/init.h>
  12. #include <linux/irq.h>
  13. #include <linux/kernel_stat.h>
  14. #include <linux/module.h>
  15. #include <linux/mutex.h>
  16. #include <linux/rculist.h>
  17. #include <linux/slab.h>
  18. #include <asm/airq.h>
  19. #include <asm/isc.h>
  20. #include "cio.h"
  21. #include "cio_debug.h"
  22. #include "ioasm.h"
  23. static DEFINE_SPINLOCK(airq_lists_lock);
  24. static struct hlist_head airq_lists[MAX_ISC+1];
  25. /**
  26. * register_adapter_interrupt() - register adapter interrupt handler
  27. * @airq: pointer to adapter interrupt descriptor
  28. *
  29. * Returns 0 on success, or -EINVAL.
  30. */
  31. int register_adapter_interrupt(struct airq_struct *airq)
  32. {
  33. char dbf_txt[32];
  34. if (!airq->handler || airq->isc > MAX_ISC)
  35. return -EINVAL;
  36. if (!airq->lsi_ptr) {
  37. airq->lsi_ptr = kzalloc(1, GFP_KERNEL);
  38. if (!airq->lsi_ptr)
  39. return -ENOMEM;
  40. airq->flags |= AIRQ_PTR_ALLOCATED;
  41. }
  42. if (!airq->lsi_mask)
  43. airq->lsi_mask = 0xff;
  44. snprintf(dbf_txt, sizeof(dbf_txt), "rairq:%p", airq);
  45. CIO_TRACE_EVENT(4, dbf_txt);
  46. isc_register(airq->isc);
  47. spin_lock(&airq_lists_lock);
  48. hlist_add_head_rcu(&airq->list, &airq_lists[airq->isc]);
  49. spin_unlock(&airq_lists_lock);
  50. return 0;
  51. }
  52. EXPORT_SYMBOL(register_adapter_interrupt);
  53. /**
  54. * unregister_adapter_interrupt - unregister adapter interrupt handler
  55. * @airq: pointer to adapter interrupt descriptor
  56. */
  57. void unregister_adapter_interrupt(struct airq_struct *airq)
  58. {
  59. char dbf_txt[32];
  60. if (hlist_unhashed(&airq->list))
  61. return;
  62. snprintf(dbf_txt, sizeof(dbf_txt), "urairq:%p", airq);
  63. CIO_TRACE_EVENT(4, dbf_txt);
  64. spin_lock(&airq_lists_lock);
  65. hlist_del_rcu(&airq->list);
  66. spin_unlock(&airq_lists_lock);
  67. synchronize_rcu();
  68. isc_unregister(airq->isc);
  69. if (airq->flags & AIRQ_PTR_ALLOCATED) {
  70. kfree(airq->lsi_ptr);
  71. airq->lsi_ptr = NULL;
  72. airq->flags &= ~AIRQ_PTR_ALLOCATED;
  73. }
  74. }
  75. EXPORT_SYMBOL(unregister_adapter_interrupt);
  76. static irqreturn_t do_airq_interrupt(int irq, void *dummy)
  77. {
  78. struct tpi_info *tpi_info;
  79. struct airq_struct *airq;
  80. struct hlist_head *head;
  81. set_cpu_flag(CIF_NOHZ_DELAY);
  82. tpi_info = (struct tpi_info *) &get_irq_regs()->int_code;
  83. trace_s390_cio_adapter_int(tpi_info);
  84. head = &airq_lists[tpi_info->isc];
  85. rcu_read_lock();
  86. hlist_for_each_entry_rcu(airq, head, list)
  87. if ((*airq->lsi_ptr & airq->lsi_mask) != 0)
  88. airq->handler(airq);
  89. rcu_read_unlock();
  90. return IRQ_HANDLED;
  91. }
  92. static struct irqaction airq_interrupt = {
  93. .name = "AIO",
  94. .handler = do_airq_interrupt,
  95. };
  96. void __init init_airq_interrupts(void)
  97. {
  98. irq_set_chip_and_handler(THIN_INTERRUPT,
  99. &dummy_irq_chip, handle_percpu_irq);
  100. setup_irq(THIN_INTERRUPT, &airq_interrupt);
  101. }
  102. /**
  103. * airq_iv_create - create an interrupt vector
  104. * @bits: number of bits in the interrupt vector
  105. * @flags: allocation flags
  106. *
  107. * Returns a pointer to an interrupt vector structure
  108. */
  109. struct airq_iv *airq_iv_create(unsigned long bits, unsigned long flags)
  110. {
  111. struct airq_iv *iv;
  112. unsigned long size;
  113. iv = kzalloc(sizeof(*iv), GFP_KERNEL);
  114. if (!iv)
  115. goto out;
  116. iv->bits = bits;
  117. size = BITS_TO_LONGS(bits) * sizeof(unsigned long);
  118. iv->vector = kzalloc(size, GFP_KERNEL);
  119. if (!iv->vector)
  120. goto out_free;
  121. if (flags & AIRQ_IV_ALLOC) {
  122. iv->avail = kmalloc(size, GFP_KERNEL);
  123. if (!iv->avail)
  124. goto out_free;
  125. memset(iv->avail, 0xff, size);
  126. iv->end = 0;
  127. } else
  128. iv->end = bits;
  129. if (flags & AIRQ_IV_BITLOCK) {
  130. iv->bitlock = kzalloc(size, GFP_KERNEL);
  131. if (!iv->bitlock)
  132. goto out_free;
  133. }
  134. if (flags & AIRQ_IV_PTR) {
  135. size = bits * sizeof(unsigned long);
  136. iv->ptr = kzalloc(size, GFP_KERNEL);
  137. if (!iv->ptr)
  138. goto out_free;
  139. }
  140. if (flags & AIRQ_IV_DATA) {
  141. size = bits * sizeof(unsigned int);
  142. iv->data = kzalloc(size, GFP_KERNEL);
  143. if (!iv->data)
  144. goto out_free;
  145. }
  146. spin_lock_init(&iv->lock);
  147. return iv;
  148. out_free:
  149. kfree(iv->ptr);
  150. kfree(iv->bitlock);
  151. kfree(iv->avail);
  152. kfree(iv->vector);
  153. kfree(iv);
  154. out:
  155. return NULL;
  156. }
  157. EXPORT_SYMBOL(airq_iv_create);
  158. /**
  159. * airq_iv_release - release an interrupt vector
  160. * @iv: pointer to interrupt vector structure
  161. */
  162. void airq_iv_release(struct airq_iv *iv)
  163. {
  164. kfree(iv->data);
  165. kfree(iv->ptr);
  166. kfree(iv->bitlock);
  167. kfree(iv->vector);
  168. kfree(iv->avail);
  169. kfree(iv);
  170. }
  171. EXPORT_SYMBOL(airq_iv_release);
  172. /**
  173. * airq_iv_alloc - allocate irq bits from an interrupt vector
  174. * @iv: pointer to an interrupt vector structure
  175. * @num: number of consecutive irq bits to allocate
  176. *
  177. * Returns the bit number of the first irq in the allocated block of irqs,
  178. * or -1UL if no bit is available or the AIRQ_IV_ALLOC flag has not been
  179. * specified
  180. */
  181. unsigned long airq_iv_alloc(struct airq_iv *iv, unsigned long num)
  182. {
  183. unsigned long bit, i, flags;
  184. if (!iv->avail || num == 0)
  185. return -1UL;
  186. spin_lock_irqsave(&iv->lock, flags);
  187. bit = find_first_bit_inv(iv->avail, iv->bits);
  188. while (bit + num <= iv->bits) {
  189. for (i = 1; i < num; i++)
  190. if (!test_bit_inv(bit + i, iv->avail))
  191. break;
  192. if (i >= num) {
  193. /* Found a suitable block of irqs */
  194. for (i = 0; i < num; i++)
  195. clear_bit_inv(bit + i, iv->avail);
  196. if (bit + num >= iv->end)
  197. iv->end = bit + num + 1;
  198. break;
  199. }
  200. bit = find_next_bit_inv(iv->avail, iv->bits, bit + i + 1);
  201. }
  202. if (bit + num > iv->bits)
  203. bit = -1UL;
  204. spin_unlock_irqrestore(&iv->lock, flags);
  205. return bit;
  206. }
  207. EXPORT_SYMBOL(airq_iv_alloc);
  208. /**
  209. * airq_iv_free - free irq bits of an interrupt vector
  210. * @iv: pointer to interrupt vector structure
  211. * @bit: number of the first irq bit to free
  212. * @num: number of consecutive irq bits to free
  213. */
  214. void airq_iv_free(struct airq_iv *iv, unsigned long bit, unsigned long num)
  215. {
  216. unsigned long i, flags;
  217. if (!iv->avail || num == 0)
  218. return;
  219. spin_lock_irqsave(&iv->lock, flags);
  220. for (i = 0; i < num; i++) {
  221. /* Clear (possibly left over) interrupt bit */
  222. clear_bit_inv(bit + i, iv->vector);
  223. /* Make the bit positions available again */
  224. set_bit_inv(bit + i, iv->avail);
  225. }
  226. if (bit + num >= iv->end) {
  227. /* Find new end of bit-field */
  228. while (iv->end > 0 && !test_bit_inv(iv->end - 1, iv->avail))
  229. iv->end--;
  230. }
  231. spin_unlock_irqrestore(&iv->lock, flags);
  232. }
  233. EXPORT_SYMBOL(airq_iv_free);
  234. /**
  235. * airq_iv_scan - scan interrupt vector for non-zero bits
  236. * @iv: pointer to interrupt vector structure
  237. * @start: bit number to start the search
  238. * @end: bit number to end the search
  239. *
  240. * Returns the bit number of the next non-zero interrupt bit, or
  241. * -1UL if the scan completed without finding any more any non-zero bits.
  242. */
  243. unsigned long airq_iv_scan(struct airq_iv *iv, unsigned long start,
  244. unsigned long end)
  245. {
  246. unsigned long bit;
  247. /* Find non-zero bit starting from 'ivs->next'. */
  248. bit = find_next_bit_inv(iv->vector, end, start);
  249. if (bit >= end)
  250. return -1UL;
  251. clear_bit_inv(bit, iv->vector);
  252. return bit;
  253. }
  254. EXPORT_SYMBOL(airq_iv_scan);