opal-async.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. wait_event(opal_async_wait, test_bit(token, opal_async_complete_map));
  97. memcpy(msg, &opal_async_responses[token], sizeof(*msg));
  98. return 0;
  99. }
  100. EXPORT_SYMBOL_GPL(opal_async_wait_response);
  101. static int opal_async_comp_event(struct notifier_block *nb,
  102. unsigned long msg_type, void *msg)
  103. {
  104. struct opal_msg *comp_msg = msg;
  105. unsigned long flags;
  106. uint64_t token;
  107. if (msg_type != OPAL_MSG_ASYNC_COMP)
  108. return 0;
  109. token = be64_to_cpu(comp_msg->params[0]);
  110. memcpy(&opal_async_responses[token], comp_msg, sizeof(*comp_msg));
  111. spin_lock_irqsave(&opal_async_comp_lock, flags);
  112. __set_bit(token, opal_async_complete_map);
  113. spin_unlock_irqrestore(&opal_async_comp_lock, flags);
  114. wake_up(&opal_async_wait);
  115. return 0;
  116. }
  117. static struct notifier_block opal_async_comp_nb = {
  118. .notifier_call = opal_async_comp_event,
  119. .next = NULL,
  120. .priority = 0,
  121. };
  122. int __init opal_async_comp_init(void)
  123. {
  124. struct device_node *opal_node;
  125. const __be32 *async;
  126. int err;
  127. opal_node = of_find_node_by_path("/ibm,opal");
  128. if (!opal_node) {
  129. pr_err("%s: Opal node not found\n", __func__);
  130. err = -ENOENT;
  131. goto out;
  132. }
  133. async = of_get_property(opal_node, "opal-msg-async-num", NULL);
  134. if (!async) {
  135. pr_err("%s: %s has no opal-msg-async-num\n",
  136. __func__, opal_node->full_name);
  137. err = -ENOENT;
  138. goto out_opal_node;
  139. }
  140. opal_max_async_tokens = be32_to_cpup(async);
  141. if (opal_max_async_tokens > N_ASYNC_COMPLETIONS)
  142. opal_max_async_tokens = N_ASYNC_COMPLETIONS;
  143. err = opal_message_notifier_register(OPAL_MSG_ASYNC_COMP,
  144. &opal_async_comp_nb);
  145. if (err) {
  146. pr_err("%s: Can't register OPAL event notifier (%d)\n",
  147. __func__, err);
  148. goto out_opal_node;
  149. }
  150. opal_async_responses = kzalloc(
  151. sizeof(*opal_async_responses) * opal_max_async_tokens,
  152. GFP_KERNEL);
  153. if (!opal_async_responses) {
  154. pr_err("%s: Out of memory, failed to do asynchronous "
  155. "completion init\n", __func__);
  156. err = -ENOMEM;
  157. goto out_opal_node;
  158. }
  159. /* Initialize to 1 less than the maximum tokens available, as we may
  160. * require to pop one during emergency through synchronous call to
  161. * __opal_async_get_token()
  162. */
  163. sema_init(&opal_async_sem, opal_max_async_tokens - 1);
  164. out_opal_node:
  165. of_node_put(opal_node);
  166. out:
  167. return err;
  168. }