omap_remoteproc.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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;
  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. int ret;
  87. /* send the index of the triggered virtqueue in the mailbox payload */
  88. ret = omap_mbox_msg_send(oproc->mbox, vqid);
  89. if (ret)
  90. dev_err(rproc->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 platform_device *pdev = to_platform_device(rproc->dev);
  103. struct omap_rproc_pdata *pdata = pdev->dev.platform_data;
  104. int ret;
  105. oproc->nb.notifier_call = omap_rproc_mbox_callback;
  106. /* every omap rproc is assigned a mailbox instance for messaging */
  107. oproc->mbox = omap_mbox_get(pdata->mbox_name, &oproc->nb);
  108. if (IS_ERR(oproc->mbox)) {
  109. ret = PTR_ERR(oproc->mbox);
  110. dev_err(rproc->dev, "omap_mbox_get failed: %d\n", ret);
  111. return ret;
  112. }
  113. /*
  114. * Ping the remote processor. this is only for sanity-sake;
  115. * there is no functional effect whatsoever.
  116. *
  117. * Note that the reply will _not_ arrive immediately: this message
  118. * will wait in the mailbox fifo until the remote processor is booted.
  119. */
  120. ret = omap_mbox_msg_send(oproc->mbox, RP_MBOX_ECHO_REQUEST);
  121. if (ret) {
  122. dev_err(rproc->dev, "omap_mbox_get failed: %d\n", ret);
  123. goto put_mbox;
  124. }
  125. ret = pdata->device_enable(pdev);
  126. if (ret) {
  127. dev_err(rproc->dev, "omap_device_enable failed: %d\n", ret);
  128. goto put_mbox;
  129. }
  130. return 0;
  131. put_mbox:
  132. omap_mbox_put(oproc->mbox, &oproc->nb);
  133. return ret;
  134. }
  135. /* power off the remote processor */
  136. static int omap_rproc_stop(struct rproc *rproc)
  137. {
  138. struct platform_device *pdev = to_platform_device(rproc->dev);
  139. struct omap_rproc_pdata *pdata = pdev->dev.platform_data;
  140. struct omap_rproc *oproc = rproc->priv;
  141. int ret;
  142. ret = pdata->device_shutdown(pdev);
  143. if (ret)
  144. return ret;
  145. omap_mbox_put(oproc->mbox, &oproc->nb);
  146. return 0;
  147. }
  148. static struct rproc_ops omap_rproc_ops = {
  149. .start = omap_rproc_start,
  150. .stop = omap_rproc_stop,
  151. .kick = omap_rproc_kick,
  152. };
  153. static int __devinit omap_rproc_probe(struct platform_device *pdev)
  154. {
  155. struct omap_rproc_pdata *pdata = pdev->dev.platform_data;
  156. struct omap_rproc *oproc;
  157. struct rproc *rproc;
  158. int ret;
  159. ret = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
  160. if (ret) {
  161. dev_err(&pdev->dev, "dma_set_coherent_mask: %d\n", ret);
  162. return ret;
  163. }
  164. rproc = rproc_alloc(&pdev->dev, pdata->name, &omap_rproc_ops,
  165. pdata->firmware, sizeof(*oproc));
  166. if (!rproc)
  167. return -ENOMEM;
  168. oproc = rproc->priv;
  169. oproc->rproc = rproc;
  170. platform_set_drvdata(pdev, rproc);
  171. ret = rproc_register(rproc);
  172. if (ret)
  173. goto free_rproc;
  174. return 0;
  175. free_rproc:
  176. rproc_free(rproc);
  177. return ret;
  178. }
  179. static int __devexit omap_rproc_remove(struct platform_device *pdev)
  180. {
  181. struct rproc *rproc = platform_get_drvdata(pdev);
  182. return rproc_unregister(rproc);
  183. }
  184. static struct platform_driver omap_rproc_driver = {
  185. .probe = omap_rproc_probe,
  186. .remove = __devexit_p(omap_rproc_remove),
  187. .driver = {
  188. .name = "omap-rproc",
  189. .owner = THIS_MODULE,
  190. },
  191. };
  192. module_platform_driver(omap_rproc_driver);
  193. MODULE_LICENSE("GPL v2");
  194. MODULE_DESCRIPTION("OMAP Remote Processor control driver");