opal-async.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * PowerNV OPAL asynchronous completion interfaces
  3. *
  4. * Copyright 2013 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. #define N_ASYNC_COMPLETIONS 64
  24. static DECLARE_BITMAP(opal_async_complete_map, N_ASYNC_COMPLETIONS) = {~0UL};
  25. static DECLARE_BITMAP(opal_async_token_map, N_ASYNC_COMPLETIONS);
  26. static DECLARE_WAIT_QUEUE_HEAD(opal_async_wait);
  27. static DEFINE_SPINLOCK(opal_async_comp_lock);
  28. static struct semaphore opal_async_sem;
  29. static struct opal_msg *opal_async_responses;
  30. static unsigned int opal_max_async_tokens;
  31. int __opal_async_get_token(void)
  32. {
  33. unsigned long flags;
  34. int token;
  35. spin_lock_irqsave(&opal_async_comp_lock, flags);
  36. token = find_first_zero_bit(opal_async_token_map, opal_max_async_tokens);
  37. if (token >= opal_max_async_tokens) {
  38. token = -EBUSY;
  39. goto out;
  40. }
  41. if (!__test_and_clear_bit(token, opal_async_complete_map)) {
  42. token = -EBUSY;
  43. goto out;
  44. }
  45. __set_bit(token, opal_async_token_map);
  46. out:
  47. spin_unlock_irqrestore(&opal_async_comp_lock, flags);
  48. return token;
  49. }
  50. int opal_async_get_token_interruptible(void)
  51. {
  52. int token;
  53. /* Wait until a token is available */
  54. if (down_interruptible(&opal_async_sem))
  55. return -ERESTARTSYS;
  56. token = __opal_async_get_token();
  57. if (token < 0)
  58. up(&opal_async_sem);
  59. return token;
  60. }
  61. EXPORT_SYMBOL_GPL(opal_async_get_token_interruptible);
  62. int __opal_async_release_token(int token)
  63. {
  64. unsigned long flags;
  65. if (token < 0 || token >= opal_max_async_tokens) {
  66. pr_err("%s: Passed token is out of range, token %d\n",
  67. __func__, token);
  68. return -EINVAL;
  69. }
  70. spin_lock_irqsave(&opal_async_comp_lock, flags);
  71. __set_bit(token, opal_async_complete_map);
  72. __clear_bit(token, opal_async_token_map);
  73. spin_unlock_irqrestore(&opal_async_comp_lock, flags);
  74. return 0;
  75. }
  76. int opal_async_release_token(int token)
  77. {
  78. int ret;
  79. ret = __opal_async_release_token(token);
  80. if (ret)
  81. return ret;
  82. up(&opal_async_sem);
  83. return 0;
  84. }
  85. EXPORT_SYMBOL_GPL(opal_async_release_token);
  86. int opal_async_wait_response(uint64_t token, struct opal_msg *msg)
  87. {
  88. if (token >= opal_max_async_tokens) {
  89. pr_err("%s: Invalid token passed\n", __func__);
  90. return -EINVAL;
  91. }
  92. if (!msg) {
  93. pr_err("%s: Invalid message pointer passed\n", __func__);
  94. return -EINVAL;
  95. }
  96. /* Wakeup the poller before we wait for events to speed things
  97. * up on platforms or simulators where the interrupts aren't
  98. * functional.
  99. */
  100. opal_wake_poller();
  101. wait_event(opal_async_wait, test_bit(token, opal_async_complete_map));
  102. memcpy(msg, &opal_async_responses[token], sizeof(*msg));
  103. return 0;
  104. }
  105. EXPORT_SYMBOL_GPL(opal_async_wait_response);
  106. static int opal_async_comp_event(struct notifier_block *nb,
  107. unsigned long msg_type, void *msg)
  108. {
  109. struct opal_msg *comp_msg = msg;
  110. unsigned long flags;
  111. uint64_t token;
  112. if (msg_type != OPAL_MSG_ASYNC_COMP)
  113. return 0;
  114. token = be64_to_cpu(comp_msg->params[0]);
  115. memcpy(&opal_async_responses[token], comp_msg, sizeof(*comp_msg));
  116. spin_lock_irqsave(&opal_async_comp_lock, flags);
  117. __set_bit(token, opal_async_complete_map);
  118. spin_unlock_irqrestore(&opal_async_comp_lock, flags);
  119. wake_up(&opal_async_wait);
  120. return 0;
  121. }
  122. static struct notifier_block opal_async_comp_nb = {
  123. .notifier_call = opal_async_comp_event,
  124. .next = NULL,
  125. .priority = 0,
  126. };
  127. int __init opal_async_comp_init(void)
  128. {
  129. struct device_node *opal_node;
  130. const __be32 *async;
  131. int err;
  132. opal_node = of_find_node_by_path("/ibm,opal");
  133. if (!opal_node) {
  134. pr_err("%s: Opal node not found\n", __func__);
  135. err = -ENOENT;
  136. goto out;
  137. }
  138. async = of_get_property(opal_node, "opal-msg-async-num", NULL);
  139. if (!async) {
  140. pr_err("%s: %s has no opal-msg-async-num\n",
  141. __func__, opal_node->full_name);
  142. err = -ENOENT;
  143. goto out_opal_node;
  144. }
  145. opal_max_async_tokens = be32_to_cpup(async);
  146. if (opal_max_async_tokens > N_ASYNC_COMPLETIONS)
  147. opal_max_async_tokens = N_ASYNC_COMPLETIONS;
  148. err = opal_message_notifier_register(OPAL_MSG_ASYNC_COMP,
  149. &opal_async_comp_nb);
  150. if (err) {
  151. pr_err("%s: Can't register OPAL event notifier (%d)\n",
  152. __func__, err);
  153. goto out_opal_node;
  154. }
  155. opal_async_responses = kzalloc(
  156. sizeof(*opal_async_responses) * opal_max_async_tokens,
  157. GFP_KERNEL);
  158. if (!opal_async_responses) {
  159. pr_err("%s: Out of memory, failed to do asynchronous "
  160. "completion init\n", __func__);
  161. err = -ENOMEM;
  162. goto out_opal_node;
  163. }
  164. /* Initialize to 1 less than the maximum tokens available, as we may
  165. * require to pop one during emergency through synchronous call to
  166. * __opal_async_get_token()
  167. */
  168. sema_init(&opal_async_sem, opal_max_async_tokens - 1);
  169. out_opal_node:
  170. of_node_put(opal_node);
  171. out:
  172. return err;
  173. }