opal-async.c 4.8 KB

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