airq.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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 <[email protected]>
  7. * Cornelia Huck <[email protected]>
  8. * Arnd Bergmann <[email protected]>
  9. * Peter Oberparleiter <[email protected]>
  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 <linux/dmapool.h>
  19. #include <asm/airq.h>
  20. #include <asm/isc.h>
  21. #include <asm/cio.h>
  22. #include "cio.h"
  23. #include "cio_debug.h"
  24. #include "ioasm.h"
  25. static DEFINE_SPINLOCK(airq_lists_lock);
  26. static struct hlist_head airq_lists[MAX_ISC+1];
  27. static struct dma_pool *airq_iv_cache;
  28. /**
  29. * register_adapter_interrupt() - register adapter interrupt handler
  30. * @airq: pointer to adapter interrupt descriptor
  31. *
  32. * Returns 0 on success, or -EINVAL.
  33. */
  34. int register_adapter_interrupt(struct airq_struct *airq)
  35. {
  36. char dbf_txt[32];
  37. if (!airq->handler || airq->isc > MAX_ISC)
  38. return -EINVAL;
  39. if (!airq->lsi_ptr) {
  40. airq->lsi_ptr = cio_dma_zalloc(1);
  41. if (!airq->lsi_ptr)
  42. return -ENOMEM;
  43. airq->flags |= AIRQ_PTR_ALLOCATED;
  44. }
  45. if (!airq->lsi_mask)
  46. airq->lsi_mask = 0xff;
  47. snprintf(dbf_txt, sizeof(dbf_txt), "rairq:%p", airq);
  48. CIO_TRACE_EVENT(4, dbf_txt);
  49. isc_register(airq->isc);
  50. spin_lock(&airq_lists_lock);
  51. hlist_add_head_rcu(&airq->list, &airq_lists[airq->isc]);
  52. spin_unlock(&airq_lists_lock);
  53. return 0;
  54. }
  55. EXPORT_SYMBOL(register_adapter_interrupt);
  56. /**
  57. * unregister_adapter_interrupt - unregister adapter interrupt handler
  58. * @airq: pointer to adapter interrupt descriptor
  59. */
  60. void unregister_adapter_interrupt(struct airq_struct *airq)
  61. {
  62. char dbf_txt[32];
  63. if (hlist_unhashed(&airq->list))
  64. return;
  65. snprintf(dbf_txt, sizeof(dbf_txt), "urairq:%p", airq);
  66. CIO_TRACE_EVENT(4, dbf_txt);
  67. spin_lock(&airq_lists_lock);
  68. hlist_del_rcu(&airq->list);
  69. spin_unlock(&airq_lists_lock);
  70. synchronize_rcu();
  71. isc_unregister(airq->isc);
  72. if (airq->flags & AIRQ_PTR_ALLOCATED) {
  73. cio_dma_free(airq->lsi_ptr, 1);
  74. airq->lsi_ptr = NULL;
  75. airq->flags &= ~AIRQ_PTR_ALLOCATED;
  76. }
  77. }
  78. EXPORT_SYMBOL(unregister_adapter_interrupt);
  79. static irqreturn_t do_airq_interrupt(int irq, void *dummy)
  80. {
  81. struct tpi_info *tpi_info;
  82. struct airq_struct *airq;
  83. struct hlist_head *head;
  84. set_cpu_flag(CIF_NOHZ_DELAY);
  85. tpi_info = &get_irq_regs()->tpi_info;
  86. trace_s390_cio_adapter_int(tpi_info);
  87. head = &airq_lists[tpi_info->isc];
  88. rcu_read_lock();
  89. hlist_for_each_entry_rcu(airq, head, list)
  90. if ((*airq->lsi_ptr & airq->lsi_mask) != 0)
  91. airq->handler(airq, tpi_info);
  92. rcu_read_unlock();
  93. return IRQ_HANDLED;
  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. if (request_irq(THIN_INTERRUPT, do_airq_interrupt, 0, "AIO", NULL))
  100. panic("Failed to register AIO interrupt\n");
  101. }
  102. static inline unsigned long iv_size(unsigned long bits)
  103. {
  104. return BITS_TO_LONGS(bits) * sizeof(unsigned long);
  105. }
  106. /**
  107. * airq_iv_create - create an interrupt vector
  108. * @bits: number of bits in the interrupt vector
  109. * @flags: allocation flags
  110. * @vec: pointer to pinned guest memory if AIRQ_IV_GUESTVEC
  111. *
  112. * Returns a pointer to an interrupt vector structure
  113. */
  114. struct airq_iv *airq_iv_create(unsigned long bits, unsigned long flags,
  115. unsigned long *vec)
  116. {
  117. struct airq_iv *iv;
  118. unsigned long size;
  119. iv = kzalloc(sizeof(*iv), GFP_KERNEL);
  120. if (!iv)
  121. goto out;
  122. iv->bits = bits;
  123. iv->flags = flags;
  124. size = iv_size(bits);
  125. if (flags & AIRQ_IV_CACHELINE) {
  126. if ((cache_line_size() * BITS_PER_BYTE) < bits
  127. || !airq_iv_cache)
  128. goto out_free;
  129. iv->vector = dma_pool_zalloc(airq_iv_cache, GFP_KERNEL,
  130. &iv->vector_dma);
  131. if (!iv->vector)
  132. goto out_free;
  133. } else if (flags & AIRQ_IV_GUESTVEC) {
  134. iv->vector = vec;
  135. } else {
  136. iv->vector = cio_dma_zalloc(size);
  137. if (!iv->vector)
  138. goto out_free;
  139. }
  140. if (flags & AIRQ_IV_ALLOC) {
  141. iv->avail = kmalloc(size, GFP_KERNEL);
  142. if (!iv->avail)
  143. goto out_free;
  144. memset(iv->avail, 0xff, size);
  145. iv->end = 0;
  146. } else
  147. iv->end = bits;
  148. if (flags & AIRQ_IV_BITLOCK) {
  149. iv->bitlock = kzalloc(size, GFP_KERNEL);
  150. if (!iv->bitlock)
  151. goto out_free;
  152. }
  153. if (flags & AIRQ_IV_PTR) {
  154. size = bits * sizeof(unsigned long);
  155. iv->ptr = kzalloc(size, GFP_KERNEL);
  156. if (!iv->ptr)
  157. goto out_free;
  158. }
  159. if (flags & AIRQ_IV_DATA) {
  160. size = bits * sizeof(unsigned int);
  161. iv->data = kzalloc(size, GFP_KERNEL);
  162. if (!iv->data)
  163. goto out_free;
  164. }
  165. spin_lock_init(&iv->lock);
  166. return iv;
  167. out_free:
  168. kfree(iv->ptr);
  169. kfree(iv->bitlock);
  170. kfree(iv->avail);
  171. if (iv->flags & AIRQ_IV_CACHELINE && iv->vector)
  172. dma_pool_free(airq_iv_cache, iv->vector, iv->vector_dma);
  173. else if (!(iv->flags & AIRQ_IV_GUESTVEC))
  174. cio_dma_free(iv->vector, size);
  175. kfree(iv);
  176. out:
  177. return NULL;
  178. }
  179. EXPORT_SYMBOL(airq_iv_create);
  180. /**
  181. * airq_iv_release - release an interrupt vector
  182. * @iv: pointer to interrupt vector structure
  183. */
  184. void airq_iv_release(struct airq_iv *iv)
  185. {
  186. kfree(iv->data);
  187. kfree(iv->ptr);
  188. kfree(iv->bitlock);
  189. if (iv->flags & AIRQ_IV_CACHELINE)
  190. dma_pool_free(airq_iv_cache, iv->vector, iv->vector_dma);
  191. else if (!(iv->flags & AIRQ_IV_GUESTVEC))
  192. cio_dma_free(iv->vector, iv_size(iv->bits));
  193. kfree(iv->avail);
  194. kfree(iv);
  195. }
  196. EXPORT_SYMBOL(airq_iv_release);
  197. /**
  198. * airq_iv_alloc - allocate irq bits from an interrupt vector
  199. * @iv: pointer to an interrupt vector structure
  200. * @num: number of consecutive irq bits to allocate
  201. *
  202. * Returns the bit number of the first irq in the allocated block of irqs,
  203. * or -1UL if no bit is available or the AIRQ_IV_ALLOC flag has not been
  204. * specified
  205. */
  206. unsigned long airq_iv_alloc(struct airq_iv *iv, unsigned long num)
  207. {
  208. unsigned long bit, i, flags;
  209. if (!iv->avail || num == 0)
  210. return -1UL;
  211. spin_lock_irqsave(&iv->lock, flags);
  212. bit = find_first_bit_inv(iv->avail, iv->bits);
  213. while (bit + num <= iv->bits) {
  214. for (i = 1; i < num; i++)
  215. if (!test_bit_inv(bit + i, iv->avail))
  216. break;
  217. if (i >= num) {
  218. /* Found a suitable block of irqs */
  219. for (i = 0; i < num; i++)
  220. clear_bit_inv(bit + i, iv->avail);
  221. if (bit + num >= iv->end)
  222. iv->end = bit + num + 1;
  223. break;
  224. }
  225. bit = find_next_bit_inv(iv->avail, iv->bits, bit + i + 1);
  226. }
  227. if (bit + num > iv->bits)
  228. bit = -1UL;
  229. spin_unlock_irqrestore(&iv->lock, flags);
  230. return bit;
  231. }
  232. EXPORT_SYMBOL(airq_iv_alloc);
  233. /**
  234. * airq_iv_free - free irq bits of an interrupt vector
  235. * @iv: pointer to interrupt vector structure
  236. * @bit: number of the first irq bit to free
  237. * @num: number of consecutive irq bits to free
  238. */
  239. void airq_iv_free(struct airq_iv *iv, unsigned long bit, unsigned long num)
  240. {
  241. unsigned long i, flags;
  242. if (!iv->avail || num == 0)
  243. return;
  244. spin_lock_irqsave(&iv->lock, flags);
  245. for (i = 0; i < num; i++) {
  246. /* Clear (possibly left over) interrupt bit */
  247. clear_bit_inv(bit + i, iv->vector);
  248. /* Make the bit positions available again */
  249. set_bit_inv(bit + i, iv->avail);
  250. }
  251. if (bit + num >= iv->end) {
  252. /* Find new end of bit-field */
  253. while (iv->end > 0 && !test_bit_inv(iv->end - 1, iv->avail))
  254. iv->end--;
  255. }
  256. spin_unlock_irqrestore(&iv->lock, flags);
  257. }
  258. EXPORT_SYMBOL(airq_iv_free);
  259. /**
  260. * airq_iv_scan - scan interrupt vector for non-zero bits
  261. * @iv: pointer to interrupt vector structure
  262. * @start: bit number to start the search
  263. * @end: bit number to end the search
  264. *
  265. * Returns the bit number of the next non-zero interrupt bit, or
  266. * -1UL if the scan completed without finding any more any non-zero bits.
  267. */
  268. unsigned long airq_iv_scan(struct airq_iv *iv, unsigned long start,
  269. unsigned long end)
  270. {
  271. unsigned long bit;
  272. /* Find non-zero bit starting from 'ivs->next'. */
  273. bit = find_next_bit_inv(iv->vector, end, start);
  274. if (bit >= end)
  275. return -1UL;
  276. clear_bit_inv(bit, iv->vector);
  277. return bit;
  278. }
  279. EXPORT_SYMBOL(airq_iv_scan);
  280. int __init airq_init(void)
  281. {
  282. airq_iv_cache = dma_pool_create("airq_iv_cache", cio_get_dma_css_dev(),
  283. cache_line_size(),
  284. cache_line_size(), PAGE_SIZE);
  285. if (!airq_iv_cache)
  286. return -ENOMEM;
  287. return 0;
  288. }