opal-async.c 4.7 KB

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