qcom_glink_rpm.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2016-2017, Linaro Ltd
  4. */
  5. #include <linux/idr.h>
  6. #include <linux/interrupt.h>
  7. #include <linux/io.h>
  8. #include <linux/list.h>
  9. #include <linux/mfd/syscon.h>
  10. #include <linux/module.h>
  11. #include <linux/of.h>
  12. #include <linux/of_address.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/regmap.h>
  15. #include <linux/rpmsg.h>
  16. #include <linux/slab.h>
  17. #include <linux/workqueue.h>
  18. #include <linux/mailbox_client.h>
  19. #include <linux/ipc_logging.h>
  20. #include "rpmsg_internal.h"
  21. #include "qcom_glink_native.h"
  22. #define RPM_TOC_SIZE 256
  23. #define RPM_TOC_MAGIC 0x67727430 /* grt0 */
  24. #define RPM_TOC_MAX_ENTRIES ((RPM_TOC_SIZE - sizeof(struct rpm_toc)) / \
  25. sizeof(struct rpm_toc_entry))
  26. #define RPM_TX_FIFO_ID 0x61703272 /* ap2r */
  27. #define RPM_RX_FIFO_ID 0x72326170 /* r2ap */
  28. /* Define IPC Logging Macros */
  29. #define GLINK_RPM_IPC_LOG_PAGE_CNT 8
  30. static void *glink_ilctxt;
  31. #define GLINK_RPM_INFO(x, ...) \
  32. ipc_log_string(glink_ilctxt, "[%s]: "x, __func__, ##__VA_ARGS__)
  33. #define to_rpm_pipe(p) container_of(p, struct glink_rpm_pipe, native)
  34. struct rpm_toc_entry {
  35. __le32 id;
  36. __le32 offset;
  37. __le32 size;
  38. } __packed;
  39. struct rpm_toc {
  40. __le32 magic;
  41. __le32 count;
  42. struct rpm_toc_entry entries[];
  43. } __packed;
  44. struct glink_rpm_pipe {
  45. struct qcom_glink_pipe native;
  46. void __iomem *tail;
  47. void __iomem *head;
  48. void __iomem *fifo;
  49. };
  50. static size_t glink_rpm_rx_avail(struct qcom_glink_pipe *glink_pipe)
  51. {
  52. struct glink_rpm_pipe *pipe = to_rpm_pipe(glink_pipe);
  53. unsigned int head;
  54. unsigned int tail;
  55. head = readl(pipe->head);
  56. tail = readl(pipe->tail);
  57. GLINK_RPM_INFO("RX: head:0X%X tail:0X%X\n", head, tail);
  58. if (head < tail)
  59. return pipe->native.length - tail + head;
  60. else
  61. return head - tail;
  62. }
  63. static void glink_rpm_rx_peak(struct qcom_glink_pipe *glink_pipe,
  64. void *data, unsigned int offset, size_t count)
  65. {
  66. struct glink_rpm_pipe *pipe = to_rpm_pipe(glink_pipe);
  67. unsigned int tail;
  68. size_t len;
  69. tail = readl(pipe->tail);
  70. tail += offset;
  71. if (tail >= pipe->native.length)
  72. tail -= pipe->native.length;
  73. len = min_t(size_t, count, pipe->native.length - tail);
  74. if (len) {
  75. __ioread32_copy(data, pipe->fifo + tail,
  76. len / sizeof(u32));
  77. }
  78. if (len != count) {
  79. __ioread32_copy(data + len, pipe->fifo,
  80. (count - len) / sizeof(u32));
  81. }
  82. }
  83. static void glink_rpm_rx_advance(struct qcom_glink_pipe *glink_pipe,
  84. size_t count)
  85. {
  86. struct glink_rpm_pipe *pipe = to_rpm_pipe(glink_pipe);
  87. unsigned int tail;
  88. tail = readl(pipe->tail);
  89. GLINK_RPM_INFO("RX: tail:0X%X tail+Count:0X%X\n", tail, tail+count);
  90. tail += count;
  91. if (tail >= pipe->native.length)
  92. tail -= pipe->native.length;
  93. writel(tail, pipe->tail);
  94. }
  95. static size_t glink_rpm_tx_avail(struct qcom_glink_pipe *glink_pipe)
  96. {
  97. struct glink_rpm_pipe *pipe = to_rpm_pipe(glink_pipe);
  98. unsigned int head;
  99. unsigned int tail;
  100. head = readl(pipe->head);
  101. tail = readl(pipe->tail);
  102. if (tail <= head)
  103. return pipe->native.length - head + tail;
  104. else
  105. return tail - head;
  106. }
  107. static unsigned int glink_rpm_tx_write_one(struct glink_rpm_pipe *pipe,
  108. unsigned int head,
  109. const void *data, size_t count)
  110. {
  111. size_t len;
  112. len = min_t(size_t, count, pipe->native.length - head);
  113. if (len) {
  114. __iowrite32_copy(pipe->fifo + head, data,
  115. len / sizeof(u32));
  116. }
  117. if (len != count) {
  118. __iowrite32_copy(pipe->fifo, data + len,
  119. (count - len) / sizeof(u32));
  120. }
  121. head += count;
  122. if (head >= pipe->native.length)
  123. head -= pipe->native.length;
  124. return head;
  125. }
  126. static void glink_rpm_tx_write(struct qcom_glink_pipe *glink_pipe,
  127. const void *hdr, size_t hlen,
  128. const void *data, size_t dlen)
  129. {
  130. struct glink_rpm_pipe *pipe = to_rpm_pipe(glink_pipe);
  131. size_t tlen = hlen + dlen;
  132. size_t aligned_dlen;
  133. unsigned int head;
  134. char padding[8] = {0};
  135. size_t pad;
  136. /* Header length comes from glink native and is always 4 byte aligned */
  137. if (WARN(hlen % 4, "Glink Header length must be 4 bytes aligned\n"))
  138. return;
  139. /*
  140. * Move the unaligned tail of the message to the padding chunk, to
  141. * ensure word aligned accesses
  142. */
  143. aligned_dlen = ALIGN_DOWN(dlen, 4);
  144. if (aligned_dlen != dlen)
  145. memcpy(padding, data + aligned_dlen, dlen - aligned_dlen);
  146. head = readl(pipe->head);
  147. head = glink_rpm_tx_write_one(pipe, head, hdr, hlen);
  148. head = glink_rpm_tx_write_one(pipe, head, data, aligned_dlen);
  149. pad = ALIGN(tlen, 8) - ALIGN_DOWN(tlen, 4);
  150. if (pad)
  151. head = glink_rpm_tx_write_one(pipe, head, padding, pad);
  152. writel(head, pipe->head);
  153. }
  154. static int glink_rpm_parse_toc(struct device *dev,
  155. void __iomem *msg_ram,
  156. size_t msg_ram_size,
  157. struct glink_rpm_pipe *rx,
  158. struct glink_rpm_pipe *tx)
  159. {
  160. struct rpm_toc *toc;
  161. int num_entries;
  162. unsigned int id;
  163. size_t offset;
  164. size_t size;
  165. void *buf;
  166. int i;
  167. buf = kzalloc(RPM_TOC_SIZE, GFP_KERNEL);
  168. if (!buf)
  169. return -ENOMEM;
  170. __ioread32_copy(buf, msg_ram + msg_ram_size - RPM_TOC_SIZE,
  171. RPM_TOC_SIZE / sizeof(u32));
  172. toc = buf;
  173. if (le32_to_cpu(toc->magic) != RPM_TOC_MAGIC) {
  174. dev_err(dev, "RPM TOC has invalid magic\n");
  175. goto err_inval;
  176. }
  177. num_entries = le32_to_cpu(toc->count);
  178. if (num_entries > RPM_TOC_MAX_ENTRIES) {
  179. dev_err(dev, "Invalid number of toc entries\n");
  180. goto err_inval;
  181. }
  182. for (i = 0; i < num_entries; i++) {
  183. id = le32_to_cpu(toc->entries[i].id);
  184. offset = le32_to_cpu(toc->entries[i].offset);
  185. size = le32_to_cpu(toc->entries[i].size);
  186. if (offset > msg_ram_size || offset + size > msg_ram_size) {
  187. dev_err(dev, "TOC entry with invalid size\n");
  188. continue;
  189. }
  190. switch (id) {
  191. case RPM_RX_FIFO_ID:
  192. rx->native.length = size;
  193. rx->tail = msg_ram + offset;
  194. rx->head = msg_ram + offset + sizeof(u32);
  195. rx->fifo = msg_ram + offset + 2 * sizeof(u32);
  196. break;
  197. case RPM_TX_FIFO_ID:
  198. tx->native.length = size;
  199. tx->tail = msg_ram + offset;
  200. tx->head = msg_ram + offset + sizeof(u32);
  201. tx->fifo = msg_ram + offset + 2 * sizeof(u32);
  202. break;
  203. }
  204. }
  205. if (!rx->fifo || !tx->fifo) {
  206. dev_err(dev, "Unable to find rx and tx descriptors\n");
  207. goto err_inval;
  208. }
  209. kfree(buf);
  210. return 0;
  211. err_inval:
  212. kfree(buf);
  213. return -EINVAL;
  214. }
  215. static void glink_rpm_release(struct device *dev)
  216. {
  217. kfree(dev);
  218. }
  219. struct qcom_glink *glink_rpm_register(struct device *parent,
  220. struct device_node *node)
  221. {
  222. struct qcom_glink *glink;
  223. struct glink_rpm_pipe *rx_pipe;
  224. struct glink_rpm_pipe *tx_pipe;
  225. struct device_node *np;
  226. void __iomem *msg_ram;
  227. size_t msg_ram_size;
  228. struct device *dev;
  229. struct resource r;
  230. int ret;
  231. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  232. if (!dev)
  233. return ERR_PTR(-ENOMEM);
  234. dev->parent = parent;
  235. dev->of_node = node;
  236. dev->release = glink_rpm_release;
  237. dev_set_name(dev, "%s:%pKOFn", dev_name(parent->parent), node);
  238. ret = device_register(dev);
  239. if (ret) {
  240. pr_err("failed to register glink edge\n");
  241. put_device(dev);
  242. return ERR_PTR(ret);
  243. }
  244. rx_pipe = devm_kzalloc(dev, sizeof(*rx_pipe), GFP_KERNEL);
  245. tx_pipe = devm_kzalloc(dev, sizeof(*tx_pipe), GFP_KERNEL);
  246. if (!rx_pipe || !tx_pipe) {
  247. ret = -ENOMEM;
  248. goto err_put_dev;
  249. }
  250. np = of_parse_phandle(dev->of_node, "qcom,rpm-msg-ram", 0);
  251. ret = of_address_to_resource(np, 0, &r);
  252. of_node_put(np);
  253. if (ret)
  254. goto err_put_dev;
  255. msg_ram = devm_ioremap(dev, r.start, resource_size(&r));
  256. msg_ram_size = resource_size(&r);
  257. if (!msg_ram) {
  258. ret = -ENOMEM;
  259. goto err_put_dev;
  260. }
  261. ret = glink_rpm_parse_toc(dev, msg_ram, msg_ram_size,
  262. rx_pipe, tx_pipe);
  263. if (ret)
  264. goto err_put_dev;
  265. /* Pipe specific accessors */
  266. rx_pipe->native.avail = glink_rpm_rx_avail;
  267. rx_pipe->native.peak = glink_rpm_rx_peak;
  268. rx_pipe->native.advance = glink_rpm_rx_advance;
  269. tx_pipe->native.avail = glink_rpm_tx_avail;
  270. tx_pipe->native.write = glink_rpm_tx_write;
  271. writel(0, tx_pipe->head);
  272. writel(0, rx_pipe->tail);
  273. glink = qcom_glink_native_probe(dev,
  274. 0,
  275. &rx_pipe->native,
  276. &tx_pipe->native,
  277. true);
  278. if (IS_ERR(glink)) {
  279. ret = PTR_ERR(glink);
  280. goto err_put_dev;
  281. }
  282. if (!glink_ilctxt)
  283. glink_ilctxt = ipc_log_context_create(GLINK_RPM_IPC_LOG_PAGE_CNT,
  284. "glink_rpm", 0);
  285. return glink;
  286. err_put_dev:
  287. device_unregister(dev);
  288. return ERR_PTR(ret);
  289. }
  290. static int glink_rpm_probe(struct platform_device *pdev)
  291. {
  292. struct qcom_glink *glink;
  293. int ret;
  294. glink = glink_rpm_register(&pdev->dev, pdev->dev.of_node);
  295. if (IS_ERR(glink)) {
  296. ret = PTR_ERR(glink);
  297. return ret;
  298. }
  299. ret = qcom_glink_native_start(glink);
  300. if (ret)
  301. pr_err("Failed to register glink as chrdev\n");
  302. platform_set_drvdata(pdev, glink);
  303. return 0;
  304. }
  305. static int glink_rpm_unregister(struct device *dev)
  306. {
  307. struct qcom_glink *glink = dev_get_drvdata(dev);
  308. qcom_glink_native_remove(glink);
  309. qcom_glink_native_unregister(glink);
  310. return 0;
  311. }
  312. static int glink_rpm_remove(struct platform_device *pdev)
  313. {
  314. glink_rpm_unregister(&pdev->dev);
  315. return 0;
  316. }
  317. static const struct of_device_id glink_rpm_of_match[] = {
  318. { .compatible = "qcom,glink-rpm" },
  319. {}
  320. };
  321. MODULE_DEVICE_TABLE(of, glink_rpm_of_match);
  322. static struct platform_driver glink_rpm_driver = {
  323. .probe = glink_rpm_probe,
  324. .remove = glink_rpm_remove,
  325. .driver = {
  326. .name = "qcom_glink_rpm",
  327. .of_match_table = glink_rpm_of_match,
  328. .pm = &glink_native_pm_ops,
  329. },
  330. };
  331. static int __init glink_rpm_init(void)
  332. {
  333. return platform_driver_register(&glink_rpm_driver);
  334. }
  335. subsys_initcall(glink_rpm_init);
  336. static void __exit glink_rpm_exit(void)
  337. {
  338. platform_driver_unregister(&glink_rpm_driver);
  339. }
  340. module_exit(glink_rpm_exit);
  341. MODULE_AUTHOR("Bjorn Andersson <[email protected]>");
  342. MODULE_DESCRIPTION("Qualcomm GLINK RPM driver");
  343. MODULE_LICENSE("GPL v2");