ste_modem_rproc.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /*
  2. * Copyright (C) ST-Ericsson AB 2012
  3. * Author: Sjur Brændeland <sjur.brandeland@stericsson.com>
  4. * License terms: GNU General Public License (GPL), version 2
  5. */
  6. #include <linux/module.h>
  7. #include <linux/dma-mapping.h>
  8. #include <linux/remoteproc.h>
  9. #include <linux/ste_modem_shm.h>
  10. #include "remoteproc_internal.h"
  11. #define SPROC_FW_SIZE (50 * 4096)
  12. #define SPROC_MAX_TOC_ENTRIES 32
  13. #define SPROC_MAX_NOTIFY_ID 14
  14. #define SPROC_RESOURCE_NAME "rsc-table"
  15. #define SPROC_MODEM_NAME "ste-modem"
  16. #define SPROC_MODEM_FIRMWARE SPROC_MODEM_NAME "-fw.bin"
  17. #define sproc_dbg(sproc, fmt, ...) \
  18. dev_dbg(&sproc->mdev->pdev.dev, fmt, ##__VA_ARGS__)
  19. #define sproc_err(sproc, fmt, ...) \
  20. dev_err(&sproc->mdev->pdev.dev, fmt, ##__VA_ARGS__)
  21. /* STE-modem control structure */
  22. struct sproc {
  23. struct rproc *rproc;
  24. struct ste_modem_device *mdev;
  25. int error;
  26. void *fw_addr;
  27. size_t fw_size;
  28. dma_addr_t fw_dma_addr;
  29. };
  30. /* STE-Modem firmware entry */
  31. struct ste_toc_entry {
  32. __le32 start;
  33. __le32 size;
  34. __le32 flags;
  35. __le32 entry_point;
  36. __le32 load_addr;
  37. char name[12];
  38. };
  39. /*
  40. * The Table Of Content is located at the start of the firmware image and
  41. * at offset zero in the shared memory region. The resource table typically
  42. * contains the initial boot image (boot strap) and other information elements
  43. * such as remoteproc resource table. Each entry is identified by a unique
  44. * name.
  45. */
  46. struct ste_toc {
  47. struct ste_toc_entry table[SPROC_MAX_TOC_ENTRIES];
  48. };
  49. /* Loads the firmware to shared memory. */
  50. static int sproc_load_segments(struct rproc *rproc, const struct firmware *fw)
  51. {
  52. struct sproc *sproc = rproc->priv;
  53. memcpy(sproc->fw_addr, fw->data, fw->size);
  54. return 0;
  55. }
  56. /* Find the entry for resource table in the Table of Content */
  57. static const struct ste_toc_entry *sproc_find_rsc_entry(const void *data)
  58. {
  59. int i;
  60. const struct ste_toc *toc;
  61. toc = data;
  62. /* Search the table for the resource table */
  63. for (i = 0; i < SPROC_MAX_TOC_ENTRIES &&
  64. toc->table[i].start != 0xffffffff; i++) {
  65. if (!strncmp(toc->table[i].name, SPROC_RESOURCE_NAME,
  66. sizeof(toc->table[i].name)))
  67. return &toc->table[i];
  68. }
  69. return NULL;
  70. }
  71. /* Find the resource table inside the remote processor's firmware. */
  72. static struct resource_table *
  73. sproc_find_rsc_table(struct rproc *rproc, const struct firmware *fw,
  74. int *tablesz)
  75. {
  76. struct sproc *sproc = rproc->priv;
  77. struct resource_table *table;
  78. const struct ste_toc_entry *entry;
  79. if (!fw)
  80. return NULL;
  81. entry = sproc_find_rsc_entry(fw->data);
  82. if (!entry) {
  83. sproc_err(sproc, "resource table not found in fw\n");
  84. return NULL;
  85. }
  86. table = (void *)(fw->data + entry->start);
  87. /* sanity check size and offset of resource table */
  88. if (entry->start > SPROC_FW_SIZE ||
  89. entry->size > SPROC_FW_SIZE ||
  90. fw->size > SPROC_FW_SIZE ||
  91. entry->start + entry->size > fw->size ||
  92. sizeof(struct resource_table) > entry->size) {
  93. sproc_err(sproc, "bad size of fw or resource table\n");
  94. return NULL;
  95. }
  96. /* we don't support any version beyond the first */
  97. if (table->ver != 1) {
  98. sproc_err(sproc, "unsupported fw ver: %d\n", table->ver);
  99. return NULL;
  100. }
  101. /* make sure reserved bytes are zeroes */
  102. if (table->reserved[0] || table->reserved[1]) {
  103. sproc_err(sproc, "non zero reserved bytes\n");
  104. return NULL;
  105. }
  106. /* make sure the offsets array isn't truncated */
  107. if (table->num > SPROC_MAX_TOC_ENTRIES ||
  108. table->num * sizeof(table->offset[0]) +
  109. sizeof(struct resource_table) > entry->size) {
  110. sproc_err(sproc, "resource table incomplete\n");
  111. return NULL;
  112. }
  113. /* If the fw size has grown, release the previous fw allocation */
  114. if (SPROC_FW_SIZE < fw->size) {
  115. sproc_err(sproc, "Insufficient space for fw (%d < %zd)\n",
  116. SPROC_FW_SIZE, fw->size);
  117. return NULL;
  118. }
  119. sproc->fw_size = fw->size;
  120. *tablesz = entry->size;
  121. return table;
  122. }
  123. /* Find the resource table inside the remote processor's firmware. */
  124. static struct resource_table *
  125. sproc_find_loaded_rsc_table(struct rproc *rproc, const struct firmware *fw)
  126. {
  127. struct sproc *sproc = rproc->priv;
  128. const struct ste_toc_entry *entry;
  129. if (!fw || !sproc->fw_addr)
  130. return NULL;
  131. entry = sproc_find_rsc_entry(sproc->fw_addr);
  132. if (!entry) {
  133. sproc_err(sproc, "resource table not found in fw\n");
  134. return NULL;
  135. }
  136. return sproc->fw_addr + entry->start;
  137. }
  138. /* STE modem firmware handler operations */
  139. const struct rproc_fw_ops sproc_fw_ops = {
  140. .load = sproc_load_segments,
  141. .find_rsc_table = sproc_find_rsc_table,
  142. .find_loaded_rsc_table = sproc_find_loaded_rsc_table,
  143. };
  144. /* Kick the modem with specified notification id */
  145. static void sproc_kick(struct rproc *rproc, int vqid)
  146. {
  147. struct sproc *sproc = rproc->priv;
  148. sproc_dbg(sproc, "kick vqid:%d\n", vqid);
  149. /*
  150. * We need different notification IDs for RX and TX so add
  151. * an offset on TX notification IDs.
  152. */
  153. sproc->mdev->ops.kick(sproc->mdev, vqid + SPROC_MAX_NOTIFY_ID);
  154. }
  155. /* Received a kick from a modem, kick the virtqueue */
  156. static void sproc_kick_callback(struct ste_modem_device *mdev, int vqid)
  157. {
  158. struct sproc *sproc = mdev->drv_data;
  159. if (rproc_vq_interrupt(sproc->rproc, vqid) == IRQ_NONE)
  160. sproc_dbg(sproc, "no message was found in vqid %d\n", vqid);
  161. }
  162. struct ste_modem_dev_cb sproc_dev_cb = {
  163. .kick = sproc_kick_callback,
  164. };
  165. /* Start the STE modem */
  166. static int sproc_start(struct rproc *rproc)
  167. {
  168. struct sproc *sproc = rproc->priv;
  169. int i, err;
  170. sproc_dbg(sproc, "start ste-modem\n");
  171. /* Sanity test the max_notifyid */
  172. if (rproc->max_notifyid > SPROC_MAX_NOTIFY_ID) {
  173. sproc_err(sproc, "Notification IDs too high:%d\n",
  174. rproc->max_notifyid);
  175. return -EINVAL;
  176. }
  177. /* Subscribe to notifications */
  178. for (i = 0; i <= rproc->max_notifyid; i++) {
  179. err = sproc->mdev->ops.kick_subscribe(sproc->mdev, i);
  180. if (err) {
  181. sproc_err(sproc,
  182. "subscription of kicks failed:%d\n", err);
  183. return err;
  184. }
  185. }
  186. /* Request modem start-up*/
  187. return sproc->mdev->ops.power(sproc->mdev, true);
  188. }
  189. /* Stop the STE modem */
  190. static int sproc_stop(struct rproc *rproc)
  191. {
  192. struct sproc *sproc = rproc->priv;
  193. sproc_dbg(sproc, "stop ste-modem\n");
  194. return sproc->mdev->ops.power(sproc->mdev, false);
  195. }
  196. static struct rproc_ops sproc_ops = {
  197. .start = sproc_start,
  198. .stop = sproc_stop,
  199. .kick = sproc_kick,
  200. };
  201. /* STE modem device is unregistered */
  202. static int sproc_drv_remove(struct platform_device *pdev)
  203. {
  204. struct ste_modem_device *mdev =
  205. container_of(pdev, struct ste_modem_device, pdev);
  206. struct sproc *sproc = mdev->drv_data;
  207. sproc_dbg(sproc, "remove ste-modem\n");
  208. /* Reset device callback functions */
  209. sproc->mdev->ops.setup(sproc->mdev, NULL);
  210. /* Unregister as remoteproc device */
  211. rproc_del(sproc->rproc);
  212. dma_free_coherent(sproc->rproc->dev.parent, SPROC_FW_SIZE,
  213. sproc->fw_addr, sproc->fw_dma_addr);
  214. rproc_put(sproc->rproc);
  215. mdev->drv_data = NULL;
  216. return 0;
  217. }
  218. /* Handle probe of a modem device */
  219. static int sproc_probe(struct platform_device *pdev)
  220. {
  221. struct ste_modem_device *mdev =
  222. container_of(pdev, struct ste_modem_device, pdev);
  223. struct sproc *sproc;
  224. struct rproc *rproc;
  225. int err;
  226. dev_dbg(&mdev->pdev.dev, "probe ste-modem\n");
  227. if (!mdev->ops.setup || !mdev->ops.kick || !mdev->ops.kick_subscribe ||
  228. !mdev->ops.power) {
  229. dev_err(&mdev->pdev.dev, "invalid mdev ops\n");
  230. return -EINVAL;
  231. }
  232. rproc = rproc_alloc(&mdev->pdev.dev, mdev->pdev.name, &sproc_ops,
  233. SPROC_MODEM_FIRMWARE, sizeof(*sproc));
  234. if (!rproc)
  235. return -ENOMEM;
  236. sproc = rproc->priv;
  237. sproc->mdev = mdev;
  238. sproc->rproc = rproc;
  239. mdev->drv_data = sproc;
  240. /* Provide callback functions to modem device */
  241. sproc->mdev->ops.setup(sproc->mdev, &sproc_dev_cb);
  242. /* Set the STE-modem specific firmware handler */
  243. rproc->fw_ops = &sproc_fw_ops;
  244. /*
  245. * STE-modem requires the firmware to be located
  246. * at the start of the shared memory region. So we need to
  247. * reserve space for firmware at the start.
  248. */
  249. sproc->fw_addr = dma_alloc_coherent(rproc->dev.parent, SPROC_FW_SIZE,
  250. &sproc->fw_dma_addr,
  251. GFP_KERNEL);
  252. if (!sproc->fw_addr) {
  253. sproc_err(sproc, "Cannot allocate memory for fw\n");
  254. err = -ENOMEM;
  255. goto free_rproc;
  256. }
  257. /* Register as a remoteproc device */
  258. err = rproc_add(rproc);
  259. if (err)
  260. goto free_mem;
  261. return 0;
  262. free_mem:
  263. dma_free_coherent(rproc->dev.parent, SPROC_FW_SIZE,
  264. sproc->fw_addr, sproc->fw_dma_addr);
  265. free_rproc:
  266. /* Reset device data upon error */
  267. mdev->drv_data = NULL;
  268. rproc_put(rproc);
  269. return err;
  270. }
  271. static struct platform_driver sproc_driver = {
  272. .driver = {
  273. .name = SPROC_MODEM_NAME,
  274. .owner = THIS_MODULE,
  275. },
  276. .probe = sproc_probe,
  277. .remove = sproc_drv_remove,
  278. };
  279. module_platform_driver(sproc_driver);
  280. MODULE_LICENSE("GPL v2");
  281. MODULE_DESCRIPTION("STE Modem driver using the Remote Processor Framework");