irqbypass.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /*
  2. * IRQ offload/bypass manager
  3. *
  4. * Copyright (C) 2015 Red Hat, Inc.
  5. * Copyright (c) 2015 Linaro Ltd.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * Various virtualization hardware acceleration techniques allow bypassing or
  12. * offloading interrupts received from devices around the host kernel. Posted
  13. * Interrupts on Intel VT-d systems can allow interrupts to be received
  14. * directly by a virtual machine. ARM IRQ Forwarding allows forwarded physical
  15. * interrupts to be directly deactivated by the guest. This manager allows
  16. * interrupt producers and consumers to find each other to enable this sort of
  17. * bypass.
  18. */
  19. #include <linux/irqbypass.h>
  20. #include <linux/list.h>
  21. #include <linux/module.h>
  22. #include <linux/mutex.h>
  23. MODULE_LICENSE("GPL v2");
  24. MODULE_DESCRIPTION("IRQ bypass manager utility module");
  25. static LIST_HEAD(producers);
  26. static LIST_HEAD(consumers);
  27. static DEFINE_MUTEX(lock);
  28. /* @lock must be held when calling connect */
  29. static int __connect(struct irq_bypass_producer *prod,
  30. struct irq_bypass_consumer *cons)
  31. {
  32. int ret = 0;
  33. if (prod->stop)
  34. prod->stop(prod);
  35. if (cons->stop)
  36. cons->stop(cons);
  37. if (prod->add_consumer)
  38. ret = prod->add_consumer(prod, cons);
  39. if (!ret) {
  40. ret = cons->add_producer(cons, prod);
  41. if (ret && prod->del_consumer)
  42. prod->del_consumer(prod, cons);
  43. }
  44. if (cons->start)
  45. cons->start(cons);
  46. if (prod->start)
  47. prod->start(prod);
  48. return ret;
  49. }
  50. /* @lock must be held when calling disconnect */
  51. static void __disconnect(struct irq_bypass_producer *prod,
  52. struct irq_bypass_consumer *cons)
  53. {
  54. if (prod->stop)
  55. prod->stop(prod);
  56. if (cons->stop)
  57. cons->stop(cons);
  58. cons->del_producer(cons, prod);
  59. if (prod->del_consumer)
  60. prod->del_consumer(prod, cons);
  61. if (cons->start)
  62. cons->start(cons);
  63. if (prod->start)
  64. prod->start(prod);
  65. }
  66. /**
  67. * irq_bypass_register_producer - register IRQ bypass producer
  68. * @producer: pointer to producer structure
  69. *
  70. * Add the provided IRQ producer to the list of producers and connect
  71. * with any matching token found on the IRQ consumers list.
  72. */
  73. int irq_bypass_register_producer(struct irq_bypass_producer *producer)
  74. {
  75. struct irq_bypass_producer *tmp;
  76. struct irq_bypass_consumer *consumer;
  77. if (!producer->token)
  78. return -EINVAL;
  79. might_sleep();
  80. if (!try_module_get(THIS_MODULE))
  81. return -ENODEV;
  82. mutex_lock(&lock);
  83. list_for_each_entry(tmp, &producers, node) {
  84. if (tmp->token == producer->token) {
  85. mutex_unlock(&lock);
  86. module_put(THIS_MODULE);
  87. return -EBUSY;
  88. }
  89. }
  90. list_for_each_entry(consumer, &consumers, node) {
  91. if (consumer->token == producer->token) {
  92. int ret = __connect(producer, consumer);
  93. if (ret) {
  94. mutex_unlock(&lock);
  95. module_put(THIS_MODULE);
  96. return ret;
  97. }
  98. break;
  99. }
  100. }
  101. list_add(&producer->node, &producers);
  102. mutex_unlock(&lock);
  103. return 0;
  104. }
  105. EXPORT_SYMBOL_GPL(irq_bypass_register_producer);
  106. /**
  107. * irq_bypass_unregister_producer - unregister IRQ bypass producer
  108. * @producer: pointer to producer structure
  109. *
  110. * Remove a previously registered IRQ producer from the list of producers
  111. * and disconnect it from any connected IRQ consumer.
  112. */
  113. void irq_bypass_unregister_producer(struct irq_bypass_producer *producer)
  114. {
  115. struct irq_bypass_producer *tmp;
  116. struct irq_bypass_consumer *consumer;
  117. if (!producer->token)
  118. return;
  119. might_sleep();
  120. if (!try_module_get(THIS_MODULE))
  121. return; /* nothing in the list anyway */
  122. mutex_lock(&lock);
  123. list_for_each_entry(tmp, &producers, node) {
  124. if (tmp->token != producer->token)
  125. continue;
  126. list_for_each_entry(consumer, &consumers, node) {
  127. if (consumer->token == producer->token) {
  128. __disconnect(producer, consumer);
  129. break;
  130. }
  131. }
  132. list_del(&producer->node);
  133. module_put(THIS_MODULE);
  134. break;
  135. }
  136. mutex_unlock(&lock);
  137. module_put(THIS_MODULE);
  138. }
  139. EXPORT_SYMBOL_GPL(irq_bypass_unregister_producer);
  140. /**
  141. * irq_bypass_register_consumer - register IRQ bypass consumer
  142. * @consumer: pointer to consumer structure
  143. *
  144. * Add the provided IRQ consumer to the list of consumers and connect
  145. * with any matching token found on the IRQ producer list.
  146. */
  147. int irq_bypass_register_consumer(struct irq_bypass_consumer *consumer)
  148. {
  149. struct irq_bypass_consumer *tmp;
  150. struct irq_bypass_producer *producer;
  151. if (!consumer->token ||
  152. !consumer->add_producer || !consumer->del_producer)
  153. return -EINVAL;
  154. might_sleep();
  155. if (!try_module_get(THIS_MODULE))
  156. return -ENODEV;
  157. mutex_lock(&lock);
  158. list_for_each_entry(tmp, &consumers, node) {
  159. if (tmp->token == consumer->token || tmp == consumer) {
  160. mutex_unlock(&lock);
  161. module_put(THIS_MODULE);
  162. return -EBUSY;
  163. }
  164. }
  165. list_for_each_entry(producer, &producers, node) {
  166. if (producer->token == consumer->token) {
  167. int ret = __connect(producer, consumer);
  168. if (ret) {
  169. mutex_unlock(&lock);
  170. module_put(THIS_MODULE);
  171. return ret;
  172. }
  173. break;
  174. }
  175. }
  176. list_add(&consumer->node, &consumers);
  177. mutex_unlock(&lock);
  178. return 0;
  179. }
  180. EXPORT_SYMBOL_GPL(irq_bypass_register_consumer);
  181. /**
  182. * irq_bypass_unregister_consumer - unregister IRQ bypass consumer
  183. * @consumer: pointer to consumer structure
  184. *
  185. * Remove a previously registered IRQ consumer from the list of consumers
  186. * and disconnect it from any connected IRQ producer.
  187. */
  188. void irq_bypass_unregister_consumer(struct irq_bypass_consumer *consumer)
  189. {
  190. struct irq_bypass_consumer *tmp;
  191. struct irq_bypass_producer *producer;
  192. if (!consumer->token)
  193. return;
  194. might_sleep();
  195. if (!try_module_get(THIS_MODULE))
  196. return; /* nothing in the list anyway */
  197. mutex_lock(&lock);
  198. list_for_each_entry(tmp, &consumers, node) {
  199. if (tmp != consumer)
  200. continue;
  201. list_for_each_entry(producer, &producers, node) {
  202. if (producer->token == consumer->token) {
  203. __disconnect(producer, consumer);
  204. break;
  205. }
  206. }
  207. list_del(&consumer->node);
  208. module_put(THIS_MODULE);
  209. break;
  210. }
  211. mutex_unlock(&lock);
  212. module_put(THIS_MODULE);
  213. }
  214. EXPORT_SYMBOL_GPL(irq_bypass_unregister_consumer);