opal-async.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*
  2. * PowerNV OPAL asynchronous completion interfaces
  3. *
  4. * Copyright 2013-2017 IBM Corp.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #undef DEBUG
  12. #include <linux/kernel.h>
  13. #include <linux/init.h>
  14. #include <linux/slab.h>
  15. #include <linux/sched.h>
  16. #include <linux/semaphore.h>
  17. #include <linux/spinlock.h>
  18. #include <linux/wait.h>
  19. #include <linux/gfp.h>
  20. #include <linux/of.h>
  21. #include <asm/machdep.h>
  22. #include <asm/opal.h>
  23. enum opal_async_token_state {
  24. ASYNC_TOKEN_UNALLOCATED = 0,
  25. ASYNC_TOKEN_ALLOCATED,
  26. ASYNC_TOKEN_DISPATCHED,
  27. ASYNC_TOKEN_ABANDONED,
  28. ASYNC_TOKEN_COMPLETED
  29. };
  30. struct opal_async_token {
  31. enum opal_async_token_state state;
  32. struct opal_msg response;
  33. };
  34. static DECLARE_WAIT_QUEUE_HEAD(opal_async_wait);
  35. static DEFINE_SPINLOCK(opal_async_comp_lock);
  36. static struct semaphore opal_async_sem;
  37. static unsigned int opal_max_async_tokens;
  38. static struct opal_async_token *opal_async_tokens;
  39. static int __opal_async_get_token(void)
  40. {
  41. unsigned long flags;
  42. int i, token = -EBUSY;
  43. spin_lock_irqsave(&opal_async_comp_lock, flags);
  44. for (i = 0; i < opal_max_async_tokens; i++) {
  45. if (opal_async_tokens[i].state == ASYNC_TOKEN_UNALLOCATED) {
  46. opal_async_tokens[i].state = ASYNC_TOKEN_ALLOCATED;
  47. token = i;
  48. break;
  49. }
  50. }
  51. spin_unlock_irqrestore(&opal_async_comp_lock, flags);
  52. return token;
  53. }
  54. /*
  55. * Note: If the returned token is used in an opal call and opal returns
  56. * OPAL_ASYNC_COMPLETION you MUST call one of opal_async_wait_response() or
  57. * opal_async_wait_response_interruptible() at least once before calling another
  58. * opal_async_* function
  59. */
  60. int opal_async_get_token_interruptible(void)
  61. {
  62. int token;
  63. /* Wait until a token is available */
  64. if (down_interruptible(&opal_async_sem))
  65. return -ERESTARTSYS;
  66. token = __opal_async_get_token();
  67. if (token < 0)
  68. up(&opal_async_sem);
  69. return token;
  70. }
  71. EXPORT_SYMBOL_GPL(opal_async_get_token_interruptible);
  72. static int __opal_async_release_token(int token)
  73. {
  74. unsigned long flags;
  75. int rc;
  76. if (token < 0 || token >= opal_max_async_tokens) {
  77. pr_err("%s: Passed token is out of range, token %d\n",
  78. __func__, token);
  79. return -EINVAL;
  80. }
  81. spin_lock_irqsave(&opal_async_comp_lock, flags);
  82. switch (opal_async_tokens[token].state) {
  83. case ASYNC_TOKEN_COMPLETED:
  84. case ASYNC_TOKEN_ALLOCATED:
  85. opal_async_tokens[token].state = ASYNC_TOKEN_UNALLOCATED;
  86. rc = 0;
  87. break;
  88. /*
  89. * DISPATCHED and ABANDONED tokens must wait for OPAL to respond.
  90. * Mark a DISPATCHED token as ABANDONED so that the response handling
  91. * code knows no one cares and that it can free it then.
  92. */
  93. case ASYNC_TOKEN_DISPATCHED:
  94. opal_async_tokens[token].state = ASYNC_TOKEN_ABANDONED;
  95. /* Fall through */
  96. default:
  97. rc = 1;
  98. }
  99. spin_unlock_irqrestore(&opal_async_comp_lock, flags);
  100. return rc;
  101. }
  102. int opal_async_release_token(int token)
  103. {
  104. int ret;
  105. ret = __opal_async_release_token(token);
  106. if (!ret)
  107. up(&opal_async_sem);
  108. return ret;
  109. }
  110. EXPORT_SYMBOL_GPL(opal_async_release_token);
  111. int opal_async_wait_response(uint64_t token, struct opal_msg *msg)
  112. {
  113. if (token >= opal_max_async_tokens) {
  114. pr_err("%s: Invalid token passed\n", __func__);
  115. return -EINVAL;
  116. }
  117. if (!msg) {
  118. pr_err("%s: Invalid message pointer passed\n", __func__);
  119. return -EINVAL;
  120. }
  121. /*
  122. * There is no need to mark the token as dispatched, wait_event()
  123. * will block until the token completes.
  124. *
  125. * Wakeup the poller before we wait for events to speed things
  126. * up on platforms or simulators where the interrupts aren't
  127. * functional.
  128. */
  129. opal_wake_poller();
  130. wait_event(opal_async_wait, opal_async_tokens[token].state
  131. == ASYNC_TOKEN_COMPLETED);
  132. memcpy(msg, &opal_async_tokens[token].response, sizeof(*msg));
  133. return 0;
  134. }
  135. EXPORT_SYMBOL_GPL(opal_async_wait_response);
  136. int opal_async_wait_response_interruptible(uint64_t token, struct opal_msg *msg)
  137. {
  138. unsigned long flags;
  139. int ret;
  140. if (token >= opal_max_async_tokens) {
  141. pr_err("%s: Invalid token passed\n", __func__);
  142. return -EINVAL;
  143. }
  144. if (!msg) {
  145. pr_err("%s: Invalid message pointer passed\n", __func__);
  146. return -EINVAL;
  147. }
  148. /*
  149. * The first time this gets called we mark the token as DISPATCHED
  150. * so that if wait_event_interruptible() returns not zero and the
  151. * caller frees the token, we know not to actually free the token
  152. * until the response comes.
  153. *
  154. * Only change if the token is ALLOCATED - it may have been
  155. * completed even before the caller gets around to calling this
  156. * the first time.
  157. *
  158. * There is also a dirty great comment at the token allocation
  159. * function that if the opal call returns OPAL_ASYNC_COMPLETION to
  160. * the caller then the caller *must* call this or the not
  161. * interruptible version before doing anything else with the
  162. * token.
  163. */
  164. if (opal_async_tokens[token].state == ASYNC_TOKEN_ALLOCATED) {
  165. spin_lock_irqsave(&opal_async_comp_lock, flags);
  166. if (opal_async_tokens[token].state == ASYNC_TOKEN_ALLOCATED)
  167. opal_async_tokens[token].state = ASYNC_TOKEN_DISPATCHED;
  168. spin_unlock_irqrestore(&opal_async_comp_lock, flags);
  169. }
  170. /*
  171. * Wakeup the poller before we wait for events to speed things
  172. * up on platforms or simulators where the interrupts aren't
  173. * functional.
  174. */
  175. opal_wake_poller();
  176. ret = wait_event_interruptible(opal_async_wait,
  177. opal_async_tokens[token].state ==
  178. ASYNC_TOKEN_COMPLETED);
  179. if (!ret)
  180. memcpy(msg, &opal_async_tokens[token].response, sizeof(*msg));
  181. return ret;
  182. }
  183. EXPORT_SYMBOL_GPL(opal_async_wait_response_interruptible);
  184. /* Called from interrupt context */
  185. static int opal_async_comp_event(struct notifier_block *nb,
  186. unsigned long msg_type, void *msg)
  187. {
  188. struct opal_msg *comp_msg = msg;
  189. enum opal_async_token_state state;
  190. unsigned long flags;
  191. uint64_t token;
  192. if (msg_type != OPAL_MSG_ASYNC_COMP)
  193. return 0;
  194. token = be64_to_cpu(comp_msg->params[0]);
  195. spin_lock_irqsave(&opal_async_comp_lock, flags);
  196. state = opal_async_tokens[token].state;
  197. opal_async_tokens[token].state = ASYNC_TOKEN_COMPLETED;
  198. spin_unlock_irqrestore(&opal_async_comp_lock, flags);
  199. if (state == ASYNC_TOKEN_ABANDONED) {
  200. /* Free the token, no one else will */
  201. opal_async_release_token(token);
  202. return 0;
  203. }
  204. memcpy(&opal_async_tokens[token].response, comp_msg, sizeof(*comp_msg));
  205. wake_up(&opal_async_wait);
  206. return 0;
  207. }
  208. static struct notifier_block opal_async_comp_nb = {
  209. .notifier_call = opal_async_comp_event,
  210. .next = NULL,
  211. .priority = 0,
  212. };
  213. int __init opal_async_comp_init(void)
  214. {
  215. struct device_node *opal_node;
  216. const __be32 *async;
  217. int err;
  218. opal_node = of_find_node_by_path("/ibm,opal");
  219. if (!opal_node) {
  220. pr_err("%s: Opal node not found\n", __func__);
  221. err = -ENOENT;
  222. goto out;
  223. }
  224. async = of_get_property(opal_node, "opal-msg-async-num", NULL);
  225. if (!async) {
  226. pr_err("%s: %pOF has no opal-msg-async-num\n",
  227. __func__, opal_node);
  228. err = -ENOENT;
  229. goto out_opal_node;
  230. }
  231. opal_max_async_tokens = be32_to_cpup(async);
  232. opal_async_tokens = kcalloc(opal_max_async_tokens,
  233. sizeof(*opal_async_tokens), GFP_KERNEL);
  234. if (!opal_async_tokens) {
  235. err = -ENOMEM;
  236. goto out_opal_node;
  237. }
  238. err = opal_message_notifier_register(OPAL_MSG_ASYNC_COMP,
  239. &opal_async_comp_nb);
  240. if (err) {
  241. pr_err("%s: Can't register OPAL event notifier (%d)\n",
  242. __func__, err);
  243. kfree(opal_async_tokens);
  244. goto out_opal_node;
  245. }
  246. sema_init(&opal_async_sem, opal_max_async_tokens);
  247. out_opal_node:
  248. of_node_put(opal_node);
  249. out:
  250. return err;
  251. }