bpmp-tegra186.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2018, NVIDIA CORPORATION.
  4. */
  5. #include <linux/genalloc.h>
  6. #include <linux/io.h>
  7. #include <linux/mailbox_client.h>
  8. #include <linux/of_reserved_mem.h>
  9. #include <linux/platform_device.h>
  10. #include <soc/tegra/bpmp.h>
  11. #include <soc/tegra/bpmp-abi.h>
  12. #include <soc/tegra/ivc.h>
  13. #include "bpmp-private.h"
  14. struct tegra186_bpmp {
  15. struct tegra_bpmp *parent;
  16. struct {
  17. struct gen_pool *pool;
  18. union {
  19. void __iomem *sram;
  20. void *dram;
  21. };
  22. dma_addr_t phys;
  23. } tx, rx;
  24. struct {
  25. struct mbox_client client;
  26. struct mbox_chan *channel;
  27. } mbox;
  28. };
  29. static inline struct tegra_bpmp *
  30. mbox_client_to_bpmp(struct mbox_client *client)
  31. {
  32. struct tegra186_bpmp *priv;
  33. priv = container_of(client, struct tegra186_bpmp, mbox.client);
  34. return priv->parent;
  35. }
  36. static bool tegra186_bpmp_is_message_ready(struct tegra_bpmp_channel *channel)
  37. {
  38. int err;
  39. err = tegra_ivc_read_get_next_frame(channel->ivc, &channel->ib);
  40. if (err) {
  41. iosys_map_clear(&channel->ib);
  42. return false;
  43. }
  44. return true;
  45. }
  46. static bool tegra186_bpmp_is_channel_free(struct tegra_bpmp_channel *channel)
  47. {
  48. int err;
  49. err = tegra_ivc_write_get_next_frame(channel->ivc, &channel->ob);
  50. if (err) {
  51. iosys_map_clear(&channel->ob);
  52. return false;
  53. }
  54. return true;
  55. }
  56. static int tegra186_bpmp_ack_message(struct tegra_bpmp_channel *channel)
  57. {
  58. return tegra_ivc_read_advance(channel->ivc);
  59. }
  60. static int tegra186_bpmp_post_message(struct tegra_bpmp_channel *channel)
  61. {
  62. return tegra_ivc_write_advance(channel->ivc);
  63. }
  64. static int tegra186_bpmp_ring_doorbell(struct tegra_bpmp *bpmp)
  65. {
  66. struct tegra186_bpmp *priv = bpmp->priv;
  67. int err;
  68. err = mbox_send_message(priv->mbox.channel, NULL);
  69. if (err < 0)
  70. return err;
  71. mbox_client_txdone(priv->mbox.channel, 0);
  72. return 0;
  73. }
  74. static void tegra186_bpmp_ivc_notify(struct tegra_ivc *ivc, void *data)
  75. {
  76. struct tegra_bpmp *bpmp = data;
  77. struct tegra186_bpmp *priv = bpmp->priv;
  78. if (WARN_ON(priv->mbox.channel == NULL))
  79. return;
  80. tegra186_bpmp_ring_doorbell(bpmp);
  81. }
  82. static int tegra186_bpmp_channel_init(struct tegra_bpmp_channel *channel,
  83. struct tegra_bpmp *bpmp,
  84. unsigned int index)
  85. {
  86. struct tegra186_bpmp *priv = bpmp->priv;
  87. size_t message_size, queue_size;
  88. struct iosys_map rx, tx;
  89. unsigned int offset;
  90. int err;
  91. channel->ivc = devm_kzalloc(bpmp->dev, sizeof(*channel->ivc),
  92. GFP_KERNEL);
  93. if (!channel->ivc)
  94. return -ENOMEM;
  95. message_size = tegra_ivc_align(MSG_MIN_SZ);
  96. queue_size = tegra_ivc_total_queue_size(message_size);
  97. offset = queue_size * index;
  98. if (priv->rx.pool) {
  99. iosys_map_set_vaddr_iomem(&rx, priv->rx.sram + offset);
  100. iosys_map_set_vaddr_iomem(&tx, priv->tx.sram + offset);
  101. } else {
  102. iosys_map_set_vaddr(&rx, priv->rx.dram + offset);
  103. iosys_map_set_vaddr(&tx, priv->tx.dram + offset);
  104. }
  105. err = tegra_ivc_init(channel->ivc, NULL, &rx, priv->rx.phys + offset, &tx,
  106. priv->tx.phys + offset, 1, message_size, tegra186_bpmp_ivc_notify,
  107. bpmp);
  108. if (err < 0) {
  109. dev_err(bpmp->dev, "failed to setup IVC for channel %u: %d\n",
  110. index, err);
  111. return err;
  112. }
  113. init_completion(&channel->completion);
  114. channel->bpmp = bpmp;
  115. return 0;
  116. }
  117. static void tegra186_bpmp_channel_reset(struct tegra_bpmp_channel *channel)
  118. {
  119. /* reset the channel state */
  120. tegra_ivc_reset(channel->ivc);
  121. /* sync the channel state with BPMP */
  122. while (tegra_ivc_notified(channel->ivc))
  123. ;
  124. }
  125. static void tegra186_bpmp_channel_cleanup(struct tegra_bpmp_channel *channel)
  126. {
  127. tegra_ivc_cleanup(channel->ivc);
  128. }
  129. static void mbox_handle_rx(struct mbox_client *client, void *data)
  130. {
  131. struct tegra_bpmp *bpmp = mbox_client_to_bpmp(client);
  132. tegra_bpmp_handle_rx(bpmp);
  133. }
  134. static void tegra186_bpmp_teardown_channels(struct tegra_bpmp *bpmp)
  135. {
  136. struct tegra186_bpmp *priv = bpmp->priv;
  137. unsigned int i;
  138. for (i = 0; i < bpmp->threaded.count; i++) {
  139. if (!bpmp->threaded_channels[i].bpmp)
  140. continue;
  141. tegra186_bpmp_channel_cleanup(&bpmp->threaded_channels[i]);
  142. }
  143. tegra186_bpmp_channel_cleanup(bpmp->rx_channel);
  144. tegra186_bpmp_channel_cleanup(bpmp->tx_channel);
  145. if (priv->tx.pool) {
  146. gen_pool_free(priv->tx.pool, (unsigned long)priv->tx.sram, 4096);
  147. gen_pool_free(priv->rx.pool, (unsigned long)priv->rx.sram, 4096);
  148. }
  149. }
  150. static int tegra186_bpmp_dram_init(struct tegra_bpmp *bpmp)
  151. {
  152. struct tegra186_bpmp *priv = bpmp->priv;
  153. struct resource res;
  154. size_t size;
  155. int err;
  156. err = of_reserved_mem_region_to_resource(bpmp->dev->of_node, 0, &res);
  157. if (err < 0) {
  158. if (err != -ENODEV)
  159. dev_warn(bpmp->dev,
  160. "failed to parse memory region: %d\n", err);
  161. return err;
  162. }
  163. size = resource_size(&res);
  164. if (size < SZ_8K) {
  165. dev_warn(bpmp->dev, "DRAM region must be larger than 8 KiB\n");
  166. return -EINVAL;
  167. }
  168. priv->tx.phys = res.start;
  169. priv->rx.phys = res.start + SZ_4K;
  170. priv->tx.dram = devm_memremap(bpmp->dev, priv->tx.phys, size,
  171. MEMREMAP_WC);
  172. if (IS_ERR(priv->tx.dram)) {
  173. err = PTR_ERR(priv->tx.dram);
  174. dev_warn(bpmp->dev, "failed to map DRAM region: %d\n", err);
  175. return err;
  176. }
  177. priv->rx.dram = priv->tx.dram + SZ_4K;
  178. return 0;
  179. }
  180. static int tegra186_bpmp_sram_init(struct tegra_bpmp *bpmp)
  181. {
  182. struct tegra186_bpmp *priv = bpmp->priv;
  183. int err;
  184. priv->tx.pool = of_gen_pool_get(bpmp->dev->of_node, "shmem", 0);
  185. if (!priv->tx.pool) {
  186. dev_err(bpmp->dev, "TX shmem pool not found\n");
  187. return -EPROBE_DEFER;
  188. }
  189. priv->tx.sram = (void __iomem *)gen_pool_dma_alloc(priv->tx.pool, 4096,
  190. &priv->tx.phys);
  191. if (!priv->tx.sram) {
  192. dev_err(bpmp->dev, "failed to allocate from TX pool\n");
  193. return -ENOMEM;
  194. }
  195. priv->rx.pool = of_gen_pool_get(bpmp->dev->of_node, "shmem", 1);
  196. if (!priv->rx.pool) {
  197. dev_err(bpmp->dev, "RX shmem pool not found\n");
  198. err = -EPROBE_DEFER;
  199. goto free_tx;
  200. }
  201. priv->rx.sram = (void __iomem *)gen_pool_dma_alloc(priv->rx.pool, 4096,
  202. &priv->rx.phys);
  203. if (!priv->rx.sram) {
  204. dev_err(bpmp->dev, "failed to allocate from RX pool\n");
  205. err = -ENOMEM;
  206. goto free_tx;
  207. }
  208. return 0;
  209. free_tx:
  210. gen_pool_free(priv->tx.pool, (unsigned long)priv->tx.sram, 4096);
  211. return err;
  212. }
  213. static int tegra186_bpmp_setup_channels(struct tegra_bpmp *bpmp)
  214. {
  215. unsigned int i;
  216. int err;
  217. err = tegra186_bpmp_dram_init(bpmp);
  218. if (err == -ENODEV) {
  219. err = tegra186_bpmp_sram_init(bpmp);
  220. if (err < 0)
  221. return err;
  222. }
  223. err = tegra186_bpmp_channel_init(bpmp->tx_channel, bpmp,
  224. bpmp->soc->channels.cpu_tx.offset);
  225. if (err < 0)
  226. return err;
  227. err = tegra186_bpmp_channel_init(bpmp->rx_channel, bpmp,
  228. bpmp->soc->channels.cpu_rx.offset);
  229. if (err < 0) {
  230. tegra186_bpmp_channel_cleanup(bpmp->tx_channel);
  231. return err;
  232. }
  233. for (i = 0; i < bpmp->threaded.count; i++) {
  234. unsigned int index = bpmp->soc->channels.thread.offset + i;
  235. err = tegra186_bpmp_channel_init(&bpmp->threaded_channels[i],
  236. bpmp, index);
  237. if (err < 0)
  238. break;
  239. }
  240. if (err < 0)
  241. tegra186_bpmp_teardown_channels(bpmp);
  242. return err;
  243. }
  244. static void tegra186_bpmp_reset_channels(struct tegra_bpmp *bpmp)
  245. {
  246. unsigned int i;
  247. /* reset message channels */
  248. tegra186_bpmp_channel_reset(bpmp->tx_channel);
  249. tegra186_bpmp_channel_reset(bpmp->rx_channel);
  250. for (i = 0; i < bpmp->threaded.count; i++)
  251. tegra186_bpmp_channel_reset(&bpmp->threaded_channels[i]);
  252. }
  253. static int tegra186_bpmp_init(struct tegra_bpmp *bpmp)
  254. {
  255. struct tegra186_bpmp *priv;
  256. int err;
  257. priv = devm_kzalloc(bpmp->dev, sizeof(*priv), GFP_KERNEL);
  258. if (!priv)
  259. return -ENOMEM;
  260. priv->parent = bpmp;
  261. bpmp->priv = priv;
  262. err = tegra186_bpmp_setup_channels(bpmp);
  263. if (err < 0)
  264. return err;
  265. /* mbox registration */
  266. priv->mbox.client.dev = bpmp->dev;
  267. priv->mbox.client.rx_callback = mbox_handle_rx;
  268. priv->mbox.client.tx_block = false;
  269. priv->mbox.client.knows_txdone = false;
  270. priv->mbox.channel = mbox_request_channel(&priv->mbox.client, 0);
  271. if (IS_ERR(priv->mbox.channel)) {
  272. err = PTR_ERR(priv->mbox.channel);
  273. dev_err(bpmp->dev, "failed to get HSP mailbox: %d\n", err);
  274. tegra186_bpmp_teardown_channels(bpmp);
  275. return err;
  276. }
  277. tegra186_bpmp_reset_channels(bpmp);
  278. return 0;
  279. }
  280. static void tegra186_bpmp_deinit(struct tegra_bpmp *bpmp)
  281. {
  282. struct tegra186_bpmp *priv = bpmp->priv;
  283. mbox_free_channel(priv->mbox.channel);
  284. tegra186_bpmp_teardown_channels(bpmp);
  285. }
  286. static int tegra186_bpmp_resume(struct tegra_bpmp *bpmp)
  287. {
  288. tegra186_bpmp_reset_channels(bpmp);
  289. return 0;
  290. }
  291. const struct tegra_bpmp_ops tegra186_bpmp_ops = {
  292. .init = tegra186_bpmp_init,
  293. .deinit = tegra186_bpmp_deinit,
  294. .is_response_ready = tegra186_bpmp_is_message_ready,
  295. .is_request_ready = tegra186_bpmp_is_message_ready,
  296. .ack_response = tegra186_bpmp_ack_message,
  297. .ack_request = tegra186_bpmp_ack_message,
  298. .is_response_channel_free = tegra186_bpmp_is_channel_free,
  299. .is_request_channel_free = tegra186_bpmp_is_channel_free,
  300. .post_response = tegra186_bpmp_post_message,
  301. .post_request = tegra186_bpmp_post_message,
  302. .ring_doorbell = tegra186_bpmp_ring_doorbell,
  303. .resume = tegra186_bpmp_resume,
  304. };