bpmp-tegra186.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2018, NVIDIA CORPORATION.
  4. */
  5. #include <linux/genalloc.h>
  6. #include <linux/mailbox_client.h>
  7. #include <linux/platform_device.h>
  8. #include <soc/tegra/bpmp.h>
  9. #include <soc/tegra/bpmp-abi.h>
  10. #include <soc/tegra/ivc.h>
  11. #include "bpmp-private.h"
  12. struct tegra186_bpmp {
  13. struct tegra_bpmp *parent;
  14. struct {
  15. struct gen_pool *pool;
  16. dma_addr_t phys;
  17. void *virt;
  18. } tx, rx;
  19. struct {
  20. struct mbox_client client;
  21. struct mbox_chan *channel;
  22. } mbox;
  23. };
  24. static inline struct tegra_bpmp *
  25. mbox_client_to_bpmp(struct mbox_client *client)
  26. {
  27. struct tegra186_bpmp *priv;
  28. priv = container_of(client, struct tegra186_bpmp, mbox.client);
  29. return priv->parent;
  30. }
  31. static bool tegra186_bpmp_is_message_ready(struct tegra_bpmp_channel *channel)
  32. {
  33. void *frame;
  34. frame = tegra_ivc_read_get_next_frame(channel->ivc);
  35. if (IS_ERR(frame)) {
  36. channel->ib = NULL;
  37. return false;
  38. }
  39. channel->ib = frame;
  40. return true;
  41. }
  42. static bool tegra186_bpmp_is_channel_free(struct tegra_bpmp_channel *channel)
  43. {
  44. void *frame;
  45. frame = tegra_ivc_write_get_next_frame(channel->ivc);
  46. if (IS_ERR(frame)) {
  47. channel->ob = NULL;
  48. return false;
  49. }
  50. channel->ob = frame;
  51. return true;
  52. }
  53. static int tegra186_bpmp_ack_message(struct tegra_bpmp_channel *channel)
  54. {
  55. return tegra_ivc_read_advance(channel->ivc);
  56. }
  57. static int tegra186_bpmp_post_message(struct tegra_bpmp_channel *channel)
  58. {
  59. return tegra_ivc_write_advance(channel->ivc);
  60. }
  61. static int tegra186_bpmp_ring_doorbell(struct tegra_bpmp *bpmp)
  62. {
  63. struct tegra186_bpmp *priv = bpmp->priv;
  64. int err;
  65. err = mbox_send_message(priv->mbox.channel, NULL);
  66. if (err < 0)
  67. return err;
  68. mbox_client_txdone(priv->mbox.channel, 0);
  69. return 0;
  70. }
  71. static void tegra186_bpmp_ivc_notify(struct tegra_ivc *ivc, void *data)
  72. {
  73. struct tegra_bpmp *bpmp = data;
  74. struct tegra186_bpmp *priv = bpmp->priv;
  75. if (WARN_ON(priv->mbox.channel == NULL))
  76. return;
  77. tegra186_bpmp_ring_doorbell(bpmp);
  78. }
  79. static int tegra186_bpmp_channel_init(struct tegra_bpmp_channel *channel,
  80. struct tegra_bpmp *bpmp,
  81. unsigned int index)
  82. {
  83. struct tegra186_bpmp *priv = bpmp->priv;
  84. size_t message_size, queue_size;
  85. unsigned int offset;
  86. int err;
  87. channel->ivc = devm_kzalloc(bpmp->dev, sizeof(*channel->ivc),
  88. GFP_KERNEL);
  89. if (!channel->ivc)
  90. return -ENOMEM;
  91. message_size = tegra_ivc_align(MSG_MIN_SZ);
  92. queue_size = tegra_ivc_total_queue_size(message_size);
  93. offset = queue_size * index;
  94. err = tegra_ivc_init(channel->ivc, NULL,
  95. priv->rx.virt + offset, priv->rx.phys + offset,
  96. priv->tx.virt + offset, priv->tx.phys + offset,
  97. 1, message_size, tegra186_bpmp_ivc_notify,
  98. bpmp);
  99. if (err < 0) {
  100. dev_err(bpmp->dev, "failed to setup IVC for channel %u: %d\n",
  101. index, err);
  102. return err;
  103. }
  104. init_completion(&channel->completion);
  105. channel->bpmp = bpmp;
  106. return 0;
  107. }
  108. static void tegra186_bpmp_channel_reset(struct tegra_bpmp_channel *channel)
  109. {
  110. /* reset the channel state */
  111. tegra_ivc_reset(channel->ivc);
  112. /* sync the channel state with BPMP */
  113. while (tegra_ivc_notified(channel->ivc))
  114. ;
  115. }
  116. static void tegra186_bpmp_channel_cleanup(struct tegra_bpmp_channel *channel)
  117. {
  118. tegra_ivc_cleanup(channel->ivc);
  119. }
  120. static void mbox_handle_rx(struct mbox_client *client, void *data)
  121. {
  122. struct tegra_bpmp *bpmp = mbox_client_to_bpmp(client);
  123. tegra_bpmp_handle_rx(bpmp);
  124. }
  125. static int tegra186_bpmp_init(struct tegra_bpmp *bpmp)
  126. {
  127. struct tegra186_bpmp *priv;
  128. unsigned int i;
  129. int err;
  130. priv = devm_kzalloc(bpmp->dev, sizeof(*priv), GFP_KERNEL);
  131. if (!priv)
  132. return -ENOMEM;
  133. bpmp->priv = priv;
  134. priv->parent = bpmp;
  135. priv->tx.pool = of_gen_pool_get(bpmp->dev->of_node, "shmem", 0);
  136. if (!priv->tx.pool) {
  137. dev_err(bpmp->dev, "TX shmem pool not found\n");
  138. return -EPROBE_DEFER;
  139. }
  140. priv->tx.virt = gen_pool_dma_alloc(priv->tx.pool, 4096, &priv->tx.phys);
  141. if (!priv->tx.virt) {
  142. dev_err(bpmp->dev, "failed to allocate from TX pool\n");
  143. return -ENOMEM;
  144. }
  145. priv->rx.pool = of_gen_pool_get(bpmp->dev->of_node, "shmem", 1);
  146. if (!priv->rx.pool) {
  147. dev_err(bpmp->dev, "RX shmem pool not found\n");
  148. err = -EPROBE_DEFER;
  149. goto free_tx;
  150. }
  151. priv->rx.virt = gen_pool_dma_alloc(priv->rx.pool, 4096, &priv->rx.phys);
  152. if (!priv->rx.virt) {
  153. dev_err(bpmp->dev, "failed to allocate from RX pool\n");
  154. err = -ENOMEM;
  155. goto free_tx;
  156. }
  157. err = tegra186_bpmp_channel_init(bpmp->tx_channel, bpmp,
  158. bpmp->soc->channels.cpu_tx.offset);
  159. if (err < 0)
  160. goto free_rx;
  161. err = tegra186_bpmp_channel_init(bpmp->rx_channel, bpmp,
  162. bpmp->soc->channels.cpu_rx.offset);
  163. if (err < 0)
  164. goto cleanup_tx_channel;
  165. for (i = 0; i < bpmp->threaded.count; i++) {
  166. unsigned int index = bpmp->soc->channels.thread.offset + i;
  167. err = tegra186_bpmp_channel_init(&bpmp->threaded_channels[i],
  168. bpmp, index);
  169. if (err < 0)
  170. goto cleanup_channels;
  171. }
  172. /* mbox registration */
  173. priv->mbox.client.dev = bpmp->dev;
  174. priv->mbox.client.rx_callback = mbox_handle_rx;
  175. priv->mbox.client.tx_block = false;
  176. priv->mbox.client.knows_txdone = false;
  177. priv->mbox.channel = mbox_request_channel(&priv->mbox.client, 0);
  178. if (IS_ERR(priv->mbox.channel)) {
  179. err = PTR_ERR(priv->mbox.channel);
  180. dev_err(bpmp->dev, "failed to get HSP mailbox: %d\n", err);
  181. goto cleanup_channels;
  182. }
  183. tegra186_bpmp_channel_reset(bpmp->tx_channel);
  184. tegra186_bpmp_channel_reset(bpmp->rx_channel);
  185. for (i = 0; i < bpmp->threaded.count; i++)
  186. tegra186_bpmp_channel_reset(&bpmp->threaded_channels[i]);
  187. return 0;
  188. cleanup_channels:
  189. for (i = 0; i < bpmp->threaded.count; i++) {
  190. if (!bpmp->threaded_channels[i].bpmp)
  191. continue;
  192. tegra186_bpmp_channel_cleanup(&bpmp->threaded_channels[i]);
  193. }
  194. tegra186_bpmp_channel_cleanup(bpmp->rx_channel);
  195. cleanup_tx_channel:
  196. tegra186_bpmp_channel_cleanup(bpmp->tx_channel);
  197. free_rx:
  198. gen_pool_free(priv->rx.pool, (unsigned long)priv->rx.virt, 4096);
  199. free_tx:
  200. gen_pool_free(priv->tx.pool, (unsigned long)priv->tx.virt, 4096);
  201. return err;
  202. }
  203. static void tegra186_bpmp_deinit(struct tegra_bpmp *bpmp)
  204. {
  205. struct tegra186_bpmp *priv = bpmp->priv;
  206. unsigned int i;
  207. mbox_free_channel(priv->mbox.channel);
  208. for (i = 0; i < bpmp->threaded.count; i++)
  209. tegra186_bpmp_channel_cleanup(&bpmp->threaded_channels[i]);
  210. tegra186_bpmp_channel_cleanup(bpmp->rx_channel);
  211. tegra186_bpmp_channel_cleanup(bpmp->tx_channel);
  212. gen_pool_free(priv->rx.pool, (unsigned long)priv->rx.virt, 4096);
  213. gen_pool_free(priv->tx.pool, (unsigned long)priv->tx.virt, 4096);
  214. }
  215. static int tegra186_bpmp_resume(struct tegra_bpmp *bpmp)
  216. {
  217. unsigned int i;
  218. /* reset message channels */
  219. tegra186_bpmp_channel_reset(bpmp->tx_channel);
  220. tegra186_bpmp_channel_reset(bpmp->rx_channel);
  221. for (i = 0; i < bpmp->threaded.count; i++)
  222. tegra186_bpmp_channel_reset(&bpmp->threaded_channels[i]);
  223. return 0;
  224. }
  225. const struct tegra_bpmp_ops tegra186_bpmp_ops = {
  226. .init = tegra186_bpmp_init,
  227. .deinit = tegra186_bpmp_deinit,
  228. .is_response_ready = tegra186_bpmp_is_message_ready,
  229. .is_request_ready = tegra186_bpmp_is_message_ready,
  230. .ack_response = tegra186_bpmp_ack_message,
  231. .ack_request = tegra186_bpmp_ack_message,
  232. .is_response_channel_free = tegra186_bpmp_is_channel_free,
  233. .is_request_channel_free = tegra186_bpmp_is_channel_free,
  234. .post_response = tegra186_bpmp_post_message,
  235. .post_request = tegra186_bpmp_post_message,
  236. .ring_doorbell = tegra186_bpmp_ring_doorbell,
  237. .resume = tegra186_bpmp_resume,
  238. };