omap_remoteproc.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. * OMAP Remote Processor driver
  3. *
  4. * Copyright (C) 2011 Texas Instruments, Inc.
  5. * Copyright (C) 2011 Google, Inc.
  6. *
  7. * Ohad Ben-Cohen <ohad@wizery.com>
  8. * Brian Swetland <swetland@google.com>
  9. * Fernando Guzman Lugo <fernando.lugo@ti.com>
  10. * Mark Grosen <mgrosen@ti.com>
  11. * Suman Anna <s-anna@ti.com>
  12. * Hari Kanigeri <h-kanigeri2@ti.com>
  13. *
  14. * This program is free software; you can redistribute it and/or
  15. * modify it under the terms of the GNU General Public License
  16. * version 2 as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. */
  23. #include <linux/kernel.h>
  24. #include <linux/module.h>
  25. #include <linux/err.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/dma-mapping.h>
  28. #include <linux/remoteproc.h>
  29. #include <linux/mailbox_client.h>
  30. #include <linux/omap-mailbox.h>
  31. #include <linux/platform_data/remoteproc-omap.h>
  32. #include "omap_remoteproc.h"
  33. #include "remoteproc_internal.h"
  34. /**
  35. * struct omap_rproc - omap remote processor state
  36. * @mbox: mailbox channel handle
  37. * @client: mailbox client to request the mailbox channel
  38. * @rproc: rproc handle
  39. */
  40. struct omap_rproc {
  41. struct mbox_chan *mbox;
  42. struct mbox_client client;
  43. struct rproc *rproc;
  44. };
  45. /**
  46. * omap_rproc_mbox_callback() - inbound mailbox message handler
  47. * @client: mailbox client pointer used for requesting the mailbox channel
  48. * @data: mailbox payload
  49. *
  50. * This handler is invoked by omap's mailbox driver whenever a mailbox
  51. * message is received. Usually, the mailbox payload simply contains
  52. * the index of the virtqueue that is kicked by the remote processor,
  53. * and we let remoteproc core handle it.
  54. *
  55. * In addition to virtqueue indices, we also have some out-of-band values
  56. * that indicates different events. Those values are deliberately very
  57. * big so they don't coincide with virtqueue indices.
  58. */
  59. static void omap_rproc_mbox_callback(struct mbox_client *client, void *data)
  60. {
  61. struct omap_rproc *oproc = container_of(client, struct omap_rproc,
  62. client);
  63. struct device *dev = oproc->rproc->dev.parent;
  64. const char *name = oproc->rproc->name;
  65. u32 msg = (u32)data;
  66. dev_dbg(dev, "mbox msg: 0x%x\n", msg);
  67. switch (msg) {
  68. case RP_MBOX_CRASH:
  69. /* just log this for now. later, we'll also do recovery */
  70. dev_err(dev, "omap rproc %s crashed\n", name);
  71. break;
  72. case RP_MBOX_ECHO_REPLY:
  73. dev_info(dev, "received echo reply from %s\n", name);
  74. break;
  75. default:
  76. /* msg contains the index of the triggered vring */
  77. if (rproc_vq_interrupt(oproc->rproc, msg) == IRQ_NONE)
  78. dev_dbg(dev, "no message was found in vqid %d\n", msg);
  79. }
  80. }
  81. /* kick a virtqueue */
  82. static void omap_rproc_kick(struct rproc *rproc, int vqid)
  83. {
  84. struct omap_rproc *oproc = rproc->priv;
  85. struct device *dev = rproc->dev.parent;
  86. int ret;
  87. /* send the index of the triggered virtqueue in the mailbox payload */
  88. ret = mbox_send_message(oproc->mbox, (void *)vqid);
  89. if (ret < 0)
  90. dev_err(dev, "omap_mbox_msg_send failed: %d\n", ret);
  91. }
  92. /*
  93. * Power up the remote processor.
  94. *
  95. * This function will be invoked only after the firmware for this rproc
  96. * was loaded, parsed successfully, and all of its resource requirements
  97. * were met.
  98. */
  99. static int omap_rproc_start(struct rproc *rproc)
  100. {
  101. struct omap_rproc *oproc = rproc->priv;
  102. struct device *dev = rproc->dev.parent;
  103. struct platform_device *pdev = to_platform_device(dev);
  104. struct omap_rproc_pdata *pdata = pdev->dev.platform_data;
  105. int ret;
  106. struct mbox_client *client = &oproc->client;
  107. if (pdata->set_bootaddr)
  108. pdata->set_bootaddr(rproc->bootaddr);
  109. client->dev = dev;
  110. client->tx_done = NULL;
  111. client->rx_callback = omap_rproc_mbox_callback;
  112. client->tx_block = false;
  113. client->knows_txdone = false;
  114. oproc->mbox = omap_mbox_request_channel(client, pdata->mbox_name);
  115. if (IS_ERR(oproc->mbox)) {
  116. ret = -EBUSY;
  117. dev_err(dev, "mbox_request_channel failed: %ld\n",
  118. PTR_ERR(oproc->mbox));
  119. return ret;
  120. }
  121. /*
  122. * Ping the remote processor. this is only for sanity-sake;
  123. * there is no functional effect whatsoever.
  124. *
  125. * Note that the reply will _not_ arrive immediately: this message
  126. * will wait in the mailbox fifo until the remote processor is booted.
  127. */
  128. ret = mbox_send_message(oproc->mbox, (void *)RP_MBOX_ECHO_REQUEST);
  129. if (ret < 0) {
  130. dev_err(dev, "mbox_send_message failed: %d\n", ret);
  131. goto put_mbox;
  132. }
  133. ret = pdata->device_enable(pdev);
  134. if (ret) {
  135. dev_err(dev, "omap_device_enable failed: %d\n", ret);
  136. goto put_mbox;
  137. }
  138. return 0;
  139. put_mbox:
  140. mbox_free_channel(oproc->mbox);
  141. return ret;
  142. }
  143. /* power off the remote processor */
  144. static int omap_rproc_stop(struct rproc *rproc)
  145. {
  146. struct device *dev = rproc->dev.parent;
  147. struct platform_device *pdev = to_platform_device(dev);
  148. struct omap_rproc_pdata *pdata = pdev->dev.platform_data;
  149. struct omap_rproc *oproc = rproc->priv;
  150. int ret;
  151. ret = pdata->device_shutdown(pdev);
  152. if (ret)
  153. return ret;
  154. mbox_free_channel(oproc->mbox);
  155. return 0;
  156. }
  157. static struct rproc_ops omap_rproc_ops = {
  158. .start = omap_rproc_start,
  159. .stop = omap_rproc_stop,
  160. .kick = omap_rproc_kick,
  161. };
  162. static int omap_rproc_probe(struct platform_device *pdev)
  163. {
  164. struct omap_rproc_pdata *pdata = pdev->dev.platform_data;
  165. struct omap_rproc *oproc;
  166. struct rproc *rproc;
  167. int ret;
  168. ret = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
  169. if (ret) {
  170. dev_err(&pdev->dev, "dma_set_coherent_mask: %d\n", ret);
  171. return ret;
  172. }
  173. rproc = rproc_alloc(&pdev->dev, pdata->name, &omap_rproc_ops,
  174. pdata->firmware, sizeof(*oproc));
  175. if (!rproc)
  176. return -ENOMEM;
  177. oproc = rproc->priv;
  178. oproc->rproc = rproc;
  179. /* All existing OMAP IPU and DSP processors have an MMU */
  180. rproc->has_iommu = true;
  181. platform_set_drvdata(pdev, rproc);
  182. ret = rproc_add(rproc);
  183. if (ret)
  184. goto free_rproc;
  185. return 0;
  186. free_rproc:
  187. rproc_put(rproc);
  188. return ret;
  189. }
  190. static int omap_rproc_remove(struct platform_device *pdev)
  191. {
  192. struct rproc *rproc = platform_get_drvdata(pdev);
  193. rproc_del(rproc);
  194. rproc_put(rproc);
  195. return 0;
  196. }
  197. static struct platform_driver omap_rproc_driver = {
  198. .probe = omap_rproc_probe,
  199. .remove = omap_rproc_remove,
  200. .driver = {
  201. .name = "omap-rproc",
  202. },
  203. };
  204. module_platform_driver(omap_rproc_driver);
  205. MODULE_LICENSE("GPL v2");
  206. MODULE_DESCRIPTION("OMAP Remote Processor control driver");