omap_remoteproc.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 <plat/mailbox.h>
  30. #include <plat/remoteproc.h>
  31. #include "omap_remoteproc.h"
  32. #include "remoteproc_internal.h"
  33. /**
  34. * struct omap_rproc - omap remote processor state
  35. * @mbox: omap mailbox handle
  36. * @nb: notifier block that will be invoked on inbound mailbox messages
  37. * @rproc: rproc handle
  38. */
  39. struct omap_rproc {
  40. struct omap_mbox *mbox;
  41. struct notifier_block nb;
  42. struct rproc *rproc;
  43. };
  44. /**
  45. * omap_rproc_mbox_callback() - inbound mailbox message handler
  46. * @this: notifier block
  47. * @index: unused
  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 int omap_rproc_mbox_callback(struct notifier_block *this,
  60. unsigned long index, void *data)
  61. {
  62. mbox_msg_t msg = (mbox_msg_t) data;
  63. struct omap_rproc *oproc = container_of(this, struct omap_rproc, nb);
  64. struct device *dev = oproc->rproc->dev.parent;
  65. const char *name = oproc->rproc->name;
  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. return NOTIFY_DONE;
  81. }
  82. /* kick a virtqueue */
  83. static void omap_rproc_kick(struct rproc *rproc, int vqid)
  84. {
  85. struct omap_rproc *oproc = rproc->priv;
  86. struct device *dev = rproc->dev.parent;
  87. int ret;
  88. /* send the index of the triggered virtqueue in the mailbox payload */
  89. ret = omap_mbox_msg_send(oproc->mbox, vqid);
  90. if (ret)
  91. dev_err(dev, "omap_mbox_msg_send failed: %d\n", ret);
  92. }
  93. /*
  94. * Power up the remote processor.
  95. *
  96. * This function will be invoked only after the firmware for this rproc
  97. * was loaded, parsed successfully, and all of its resource requirements
  98. * were met.
  99. */
  100. static int omap_rproc_start(struct rproc *rproc)
  101. {
  102. struct omap_rproc *oproc = rproc->priv;
  103. struct device *dev = rproc->dev.parent;
  104. struct platform_device *pdev = to_platform_device(dev);
  105. struct omap_rproc_pdata *pdata = pdev->dev.platform_data;
  106. int ret;
  107. oproc->nb.notifier_call = omap_rproc_mbox_callback;
  108. /* every omap rproc is assigned a mailbox instance for messaging */
  109. oproc->mbox = omap_mbox_get(pdata->mbox_name, &oproc->nb);
  110. if (IS_ERR(oproc->mbox)) {
  111. ret = PTR_ERR(oproc->mbox);
  112. dev_err(dev, "omap_mbox_get failed: %d\n", ret);
  113. return ret;
  114. }
  115. /*
  116. * Ping the remote processor. this is only for sanity-sake;
  117. * there is no functional effect whatsoever.
  118. *
  119. * Note that the reply will _not_ arrive immediately: this message
  120. * will wait in the mailbox fifo until the remote processor is booted.
  121. */
  122. ret = omap_mbox_msg_send(oproc->mbox, RP_MBOX_ECHO_REQUEST);
  123. if (ret) {
  124. dev_err(dev, "omap_mbox_get failed: %d\n", ret);
  125. goto put_mbox;
  126. }
  127. ret = pdata->device_enable(pdev);
  128. if (ret) {
  129. dev_err(dev, "omap_device_enable failed: %d\n", ret);
  130. goto put_mbox;
  131. }
  132. return 0;
  133. put_mbox:
  134. omap_mbox_put(oproc->mbox, &oproc->nb);
  135. return ret;
  136. }
  137. /* power off the remote processor */
  138. static int omap_rproc_stop(struct rproc *rproc)
  139. {
  140. struct device *dev = rproc->dev.parent;
  141. struct platform_device *pdev = to_platform_device(dev);
  142. struct omap_rproc_pdata *pdata = pdev->dev.platform_data;
  143. struct omap_rproc *oproc = rproc->priv;
  144. int ret;
  145. ret = pdata->device_shutdown(pdev);
  146. if (ret)
  147. return ret;
  148. omap_mbox_put(oproc->mbox, &oproc->nb);
  149. return 0;
  150. }
  151. static struct rproc_ops omap_rproc_ops = {
  152. .start = omap_rproc_start,
  153. .stop = omap_rproc_stop,
  154. .kick = omap_rproc_kick,
  155. };
  156. static int __devinit omap_rproc_probe(struct platform_device *pdev)
  157. {
  158. struct omap_rproc_pdata *pdata = pdev->dev.platform_data;
  159. struct omap_rproc *oproc;
  160. struct rproc *rproc;
  161. int ret;
  162. ret = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
  163. if (ret) {
  164. dev_err(&pdev->dev, "dma_set_coherent_mask: %d\n", ret);
  165. return ret;
  166. }
  167. rproc = rproc_alloc(&pdev->dev, pdata->name, &omap_rproc_ops,
  168. pdata->firmware, sizeof(*oproc));
  169. if (!rproc)
  170. return -ENOMEM;
  171. oproc = rproc->priv;
  172. oproc->rproc = rproc;
  173. platform_set_drvdata(pdev, rproc);
  174. ret = rproc_add(rproc);
  175. if (ret)
  176. goto free_rproc;
  177. return 0;
  178. free_rproc:
  179. rproc_put(rproc);
  180. return ret;
  181. }
  182. static int __devexit omap_rproc_remove(struct platform_device *pdev)
  183. {
  184. struct rproc *rproc = platform_get_drvdata(pdev);
  185. rproc_del(rproc);
  186. rproc_put(rproc);
  187. return 0;
  188. }
  189. static struct platform_driver omap_rproc_driver = {
  190. .probe = omap_rproc_probe,
  191. .remove = __devexit_p(omap_rproc_remove),
  192. .driver = {
  193. .name = "omap-rproc",
  194. .owner = THIS_MODULE,
  195. },
  196. };
  197. module_platform_driver(omap_rproc_driver);
  198. MODULE_LICENSE("GPL v2");
  199. MODULE_DESCRIPTION("OMAP Remote Processor control driver");