ste_modem_rproc.c 8.6 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 = data;
  61. /* Search the table for the resource table */
  62. for (i = 0; i < SPROC_MAX_TOC_ENTRIES &&
  63. toc->table[i].start != 0xffffffff; i++) {
  64. if (!strncmp(toc->table[i].name, SPROC_RESOURCE_NAME,
  65. sizeof(toc->table[i].name)))
  66. return &toc->table[i];
  67. }
  68. return NULL;
  69. }
  70. /* Find the resource table inside the remote processor's firmware. */
  71. static struct resource_table *
  72. sproc_find_rsc_table(struct rproc *rproc, const struct firmware *fw,
  73. int *tablesz)
  74. {
  75. struct sproc *sproc = rproc->priv;
  76. struct resource_table *table;
  77. const struct ste_toc_entry *entry;
  78. if (!fw)
  79. return NULL;
  80. entry = sproc_find_rsc_entry(fw->data);
  81. if (!entry) {
  82. sproc_err(sproc, "resource table not found in fw\n");
  83. return NULL;
  84. }
  85. table = (void *)(fw->data + entry->start);
  86. /* sanity check size and offset of resource table */
  87. if (entry->start > SPROC_FW_SIZE ||
  88. entry->size > SPROC_FW_SIZE ||
  89. fw->size > SPROC_FW_SIZE ||
  90. entry->start + entry->size > fw->size ||
  91. sizeof(struct resource_table) > entry->size) {
  92. sproc_err(sproc, "bad size of fw or resource table\n");
  93. return NULL;
  94. }
  95. /* we don't support any version beyond the first */
  96. if (table->ver != 1) {
  97. sproc_err(sproc, "unsupported fw ver: %d\n", table->ver);
  98. return NULL;
  99. }
  100. /* make sure reserved bytes are zeroes */
  101. if (table->reserved[0] || table->reserved[1]) {
  102. sproc_err(sproc, "non zero reserved bytes\n");
  103. return NULL;
  104. }
  105. /* make sure the offsets array isn't truncated */
  106. if (table->num > SPROC_MAX_TOC_ENTRIES ||
  107. table->num * sizeof(table->offset[0]) +
  108. sizeof(struct resource_table) > entry->size) {
  109. sproc_err(sproc, "resource table incomplete\n");
  110. return NULL;
  111. }
  112. /* If the fw size has grown, release the previous fw allocation */
  113. if (SPROC_FW_SIZE < fw->size) {
  114. sproc_err(sproc, "Insufficient space for fw (%d < %zd)\n",
  115. SPROC_FW_SIZE, fw->size);
  116. return NULL;
  117. }
  118. sproc->fw_size = fw->size;
  119. *tablesz = entry->size;
  120. return table;
  121. }
  122. /* Find the resource table inside the remote processor's firmware. */
  123. static struct resource_table *
  124. sproc_find_loaded_rsc_table(struct rproc *rproc, const struct firmware *fw)
  125. {
  126. struct sproc *sproc = rproc->priv;
  127. const struct ste_toc_entry *entry;
  128. if (!fw || !sproc->fw_addr)
  129. return NULL;
  130. entry = sproc_find_rsc_entry(sproc->fw_addr);
  131. if (!entry) {
  132. sproc_err(sproc, "resource table not found in fw\n");
  133. return NULL;
  134. }
  135. return sproc->fw_addr + entry->start;
  136. }
  137. /* STE modem firmware handler operations */
  138. static const struct rproc_fw_ops sproc_fw_ops = {
  139. .load = sproc_load_segments,
  140. .find_rsc_table = sproc_find_rsc_table,
  141. .find_loaded_rsc_table = sproc_find_loaded_rsc_table,
  142. };
  143. /* Kick the modem with specified notification id */
  144. static void sproc_kick(struct rproc *rproc, int vqid)
  145. {
  146. struct sproc *sproc = rproc->priv;
  147. sproc_dbg(sproc, "kick vqid:%d\n", vqid);
  148. /*
  149. * We need different notification IDs for RX and TX so add
  150. * an offset on TX notification IDs.
  151. */
  152. sproc->mdev->ops.kick(sproc->mdev, vqid + SPROC_MAX_NOTIFY_ID);
  153. }
  154. /* Received a kick from a modem, kick the virtqueue */
  155. static void sproc_kick_callback(struct ste_modem_device *mdev, int vqid)
  156. {
  157. struct sproc *sproc = mdev->drv_data;
  158. if (rproc_vq_interrupt(sproc->rproc, vqid) == IRQ_NONE)
  159. sproc_dbg(sproc, "no message was found in vqid %d\n", vqid);
  160. }
  161. static struct ste_modem_dev_cb sproc_dev_cb = {
  162. .kick = sproc_kick_callback,
  163. };
  164. /* Start the STE modem */
  165. static int sproc_start(struct rproc *rproc)
  166. {
  167. struct sproc *sproc = rproc->priv;
  168. int i, err;
  169. sproc_dbg(sproc, "start ste-modem\n");
  170. /* Sanity test the max_notifyid */
  171. if (rproc->max_notifyid > SPROC_MAX_NOTIFY_ID) {
  172. sproc_err(sproc, "Notification IDs too high:%d\n",
  173. rproc->max_notifyid);
  174. return -EINVAL;
  175. }
  176. /* Subscribe to notifications */
  177. for (i = 0; i <= rproc->max_notifyid; i++) {
  178. err = sproc->mdev->ops.kick_subscribe(sproc->mdev, i);
  179. if (err) {
  180. sproc_err(sproc,
  181. "subscription of kicks failed:%d\n", err);
  182. return err;
  183. }
  184. }
  185. /* Request modem start-up*/
  186. return sproc->mdev->ops.power(sproc->mdev, true);
  187. }
  188. /* Stop the STE modem */
  189. static int sproc_stop(struct rproc *rproc)
  190. {
  191. struct sproc *sproc = rproc->priv;
  192. sproc_dbg(sproc, "stop ste-modem\n");
  193. return sproc->mdev->ops.power(sproc->mdev, false);
  194. }
  195. static struct rproc_ops sproc_ops = {
  196. .start = sproc_start,
  197. .stop = sproc_stop,
  198. .kick = sproc_kick,
  199. };
  200. /* STE modem device is unregistered */
  201. static int sproc_drv_remove(struct platform_device *pdev)
  202. {
  203. struct ste_modem_device *mdev =
  204. container_of(pdev, struct ste_modem_device, pdev);
  205. struct sproc *sproc = mdev->drv_data;
  206. sproc_dbg(sproc, "remove ste-modem\n");
  207. /* Reset device callback functions */
  208. sproc->mdev->ops.setup(sproc->mdev, NULL);
  209. /* Unregister as remoteproc device */
  210. rproc_del(sproc->rproc);
  211. dma_free_coherent(sproc->rproc->dev.parent, SPROC_FW_SIZE,
  212. sproc->fw_addr, sproc->fw_dma_addr);
  213. rproc_put(sproc->rproc);
  214. mdev->drv_data = NULL;
  215. return 0;
  216. }
  217. /* Handle probe of a modem device */
  218. static int sproc_probe(struct platform_device *pdev)
  219. {
  220. struct ste_modem_device *mdev =
  221. container_of(pdev, struct ste_modem_device, pdev);
  222. struct sproc *sproc;
  223. struct rproc *rproc;
  224. int err;
  225. dev_dbg(&mdev->pdev.dev, "probe ste-modem\n");
  226. if (!mdev->ops.setup || !mdev->ops.kick || !mdev->ops.kick_subscribe ||
  227. !mdev->ops.power) {
  228. dev_err(&mdev->pdev.dev, "invalid mdev ops\n");
  229. return -EINVAL;
  230. }
  231. rproc = rproc_alloc(&mdev->pdev.dev, mdev->pdev.name, &sproc_ops,
  232. SPROC_MODEM_FIRMWARE, sizeof(*sproc));
  233. if (!rproc)
  234. return -ENOMEM;
  235. sproc = rproc->priv;
  236. sproc->mdev = mdev;
  237. sproc->rproc = rproc;
  238. rproc->has_iommu = false;
  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. },
  275. .probe = sproc_probe,
  276. .remove = sproc_drv_remove,
  277. };
  278. module_platform_driver(sproc_driver);
  279. MODULE_LICENSE("GPL v2");
  280. MODULE_DESCRIPTION("STE Modem driver using the Remote Processor Framework");