qcom_glink_rpm.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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 "rpmsg_internal.h"
  20. #include "qcom_glink_native.h"
  21. #define RPM_TOC_SIZE 256
  22. #define RPM_TOC_MAGIC 0x67727430 /* grt0 */
  23. #define RPM_TOC_MAX_ENTRIES ((RPM_TOC_SIZE - sizeof(struct rpm_toc)) / \
  24. sizeof(struct rpm_toc_entry))
  25. #define RPM_TX_FIFO_ID 0x61703272 /* ap2r */
  26. #define RPM_RX_FIFO_ID 0x72326170 /* r2ap */
  27. #define to_rpm_pipe(p) container_of(p, struct glink_rpm_pipe, native)
  28. struct rpm_toc_entry {
  29. __le32 id;
  30. __le32 offset;
  31. __le32 size;
  32. } __packed;
  33. struct rpm_toc {
  34. __le32 magic;
  35. __le32 count;
  36. struct rpm_toc_entry entries[];
  37. } __packed;
  38. struct glink_rpm_pipe {
  39. struct qcom_glink_pipe native;
  40. void __iomem *tail;
  41. void __iomem *head;
  42. void __iomem *fifo;
  43. };
  44. static size_t glink_rpm_rx_avail(struct qcom_glink_pipe *glink_pipe)
  45. {
  46. struct glink_rpm_pipe *pipe = to_rpm_pipe(glink_pipe);
  47. unsigned int head;
  48. unsigned int tail;
  49. head = readl(pipe->head);
  50. tail = readl(pipe->tail);
  51. if (head < tail)
  52. return pipe->native.length - tail + head;
  53. else
  54. return head - tail;
  55. }
  56. static void glink_rpm_rx_peak(struct qcom_glink_pipe *glink_pipe,
  57. void *data, unsigned int offset, size_t count)
  58. {
  59. struct glink_rpm_pipe *pipe = to_rpm_pipe(glink_pipe);
  60. unsigned int tail;
  61. size_t len;
  62. tail = readl(pipe->tail);
  63. tail += offset;
  64. if (tail >= pipe->native.length)
  65. tail -= pipe->native.length;
  66. len = min_t(size_t, count, pipe->native.length - tail);
  67. if (len) {
  68. __ioread32_copy(data, pipe->fifo + tail,
  69. len / sizeof(u32));
  70. }
  71. if (len != count) {
  72. __ioread32_copy(data + len, pipe->fifo,
  73. (count - len) / sizeof(u32));
  74. }
  75. }
  76. static void glink_rpm_rx_advance(struct qcom_glink_pipe *glink_pipe,
  77. size_t count)
  78. {
  79. struct glink_rpm_pipe *pipe = to_rpm_pipe(glink_pipe);
  80. unsigned int tail;
  81. tail = readl(pipe->tail);
  82. tail += count;
  83. if (tail >= pipe->native.length)
  84. tail -= pipe->native.length;
  85. writel(tail, pipe->tail);
  86. }
  87. static size_t glink_rpm_tx_avail(struct qcom_glink_pipe *glink_pipe)
  88. {
  89. struct glink_rpm_pipe *pipe = to_rpm_pipe(glink_pipe);
  90. unsigned int head;
  91. unsigned int tail;
  92. head = readl(pipe->head);
  93. tail = readl(pipe->tail);
  94. if (tail <= head)
  95. return pipe->native.length - head + tail;
  96. else
  97. return tail - head;
  98. }
  99. static unsigned int glink_rpm_tx_write_one(struct glink_rpm_pipe *pipe,
  100. unsigned int head,
  101. const void *data, size_t count)
  102. {
  103. size_t len;
  104. len = min_t(size_t, count, pipe->native.length - head);
  105. if (len) {
  106. __iowrite32_copy(pipe->fifo + head, data,
  107. len / sizeof(u32));
  108. }
  109. if (len != count) {
  110. __iowrite32_copy(pipe->fifo, data + len,
  111. (count - len) / sizeof(u32));
  112. }
  113. head += count;
  114. if (head >= pipe->native.length)
  115. head -= pipe->native.length;
  116. return head;
  117. }
  118. static void glink_rpm_tx_write(struct qcom_glink_pipe *glink_pipe,
  119. const void *hdr, size_t hlen,
  120. const void *data, size_t dlen)
  121. {
  122. struct glink_rpm_pipe *pipe = to_rpm_pipe(glink_pipe);
  123. size_t tlen = hlen + dlen;
  124. size_t aligned_dlen;
  125. unsigned int head;
  126. char padding[8] = {0};
  127. size_t pad;
  128. /* Header length comes from glink native and is always 4 byte aligned */
  129. if (WARN(hlen % 4, "Glink Header length must be 4 bytes aligned\n"))
  130. return;
  131. /*
  132. * Move the unaligned tail of the message to the padding chunk, to
  133. * ensure word aligned accesses
  134. */
  135. aligned_dlen = ALIGN_DOWN(dlen, 4);
  136. if (aligned_dlen != dlen)
  137. memcpy(padding, data + aligned_dlen, dlen - aligned_dlen);
  138. head = readl(pipe->head);
  139. head = glink_rpm_tx_write_one(pipe, head, hdr, hlen);
  140. head = glink_rpm_tx_write_one(pipe, head, data, aligned_dlen);
  141. pad = ALIGN(tlen, 8) - ALIGN_DOWN(tlen, 4);
  142. if (pad)
  143. head = glink_rpm_tx_write_one(pipe, head, padding, pad);
  144. writel(head, pipe->head);
  145. }
  146. static int glink_rpm_parse_toc(struct device *dev,
  147. void __iomem *msg_ram,
  148. size_t msg_ram_size,
  149. struct glink_rpm_pipe *rx,
  150. struct glink_rpm_pipe *tx)
  151. {
  152. struct rpm_toc *toc;
  153. int num_entries;
  154. unsigned int id;
  155. size_t offset;
  156. size_t size;
  157. void *buf;
  158. int i;
  159. buf = kzalloc(RPM_TOC_SIZE, GFP_KERNEL);
  160. if (!buf)
  161. return -ENOMEM;
  162. __ioread32_copy(buf, msg_ram + msg_ram_size - RPM_TOC_SIZE,
  163. RPM_TOC_SIZE / sizeof(u32));
  164. toc = buf;
  165. if (le32_to_cpu(toc->magic) != RPM_TOC_MAGIC) {
  166. dev_err(dev, "RPM TOC has invalid magic\n");
  167. goto err_inval;
  168. }
  169. num_entries = le32_to_cpu(toc->count);
  170. if (num_entries > RPM_TOC_MAX_ENTRIES) {
  171. dev_err(dev, "Invalid number of toc entries\n");
  172. goto err_inval;
  173. }
  174. for (i = 0; i < num_entries; i++) {
  175. id = le32_to_cpu(toc->entries[i].id);
  176. offset = le32_to_cpu(toc->entries[i].offset);
  177. size = le32_to_cpu(toc->entries[i].size);
  178. if (offset > msg_ram_size || offset + size > msg_ram_size) {
  179. dev_err(dev, "TOC entry with invalid size\n");
  180. continue;
  181. }
  182. switch (id) {
  183. case RPM_RX_FIFO_ID:
  184. rx->native.length = size;
  185. rx->tail = msg_ram + offset;
  186. rx->head = msg_ram + offset + sizeof(u32);
  187. rx->fifo = msg_ram + offset + 2 * sizeof(u32);
  188. break;
  189. case RPM_TX_FIFO_ID:
  190. tx->native.length = size;
  191. tx->tail = msg_ram + offset;
  192. tx->head = msg_ram + offset + sizeof(u32);
  193. tx->fifo = msg_ram + offset + 2 * sizeof(u32);
  194. break;
  195. }
  196. }
  197. if (!rx->fifo || !tx->fifo) {
  198. dev_err(dev, "Unable to find rx and tx descriptors\n");
  199. goto err_inval;
  200. }
  201. kfree(buf);
  202. return 0;
  203. err_inval:
  204. kfree(buf);
  205. return -EINVAL;
  206. }
  207. static int glink_rpm_probe(struct platform_device *pdev)
  208. {
  209. struct qcom_glink *glink;
  210. struct glink_rpm_pipe *rx_pipe;
  211. struct glink_rpm_pipe *tx_pipe;
  212. struct device_node *np;
  213. void __iomem *msg_ram;
  214. size_t msg_ram_size;
  215. struct device *dev = &pdev->dev;
  216. struct resource r;
  217. int ret;
  218. rx_pipe = devm_kzalloc(&pdev->dev, sizeof(*rx_pipe), GFP_KERNEL);
  219. tx_pipe = devm_kzalloc(&pdev->dev, sizeof(*tx_pipe), GFP_KERNEL);
  220. if (!rx_pipe || !tx_pipe)
  221. return -ENOMEM;
  222. np = of_parse_phandle(dev->of_node, "qcom,rpm-msg-ram", 0);
  223. ret = of_address_to_resource(np, 0, &r);
  224. of_node_put(np);
  225. if (ret)
  226. return ret;
  227. msg_ram = devm_ioremap(dev, r.start, resource_size(&r));
  228. msg_ram_size = resource_size(&r);
  229. if (!msg_ram)
  230. return -ENOMEM;
  231. ret = glink_rpm_parse_toc(dev, msg_ram, msg_ram_size,
  232. rx_pipe, tx_pipe);
  233. if (ret)
  234. return ret;
  235. /* Pipe specific accessors */
  236. rx_pipe->native.avail = glink_rpm_rx_avail;
  237. rx_pipe->native.peak = glink_rpm_rx_peak;
  238. rx_pipe->native.advance = glink_rpm_rx_advance;
  239. tx_pipe->native.avail = glink_rpm_tx_avail;
  240. tx_pipe->native.write = glink_rpm_tx_write;
  241. writel(0, tx_pipe->head);
  242. writel(0, rx_pipe->tail);
  243. glink = qcom_glink_native_probe(&pdev->dev,
  244. 0,
  245. &rx_pipe->native,
  246. &tx_pipe->native,
  247. true);
  248. if (IS_ERR(glink))
  249. return PTR_ERR(glink);
  250. platform_set_drvdata(pdev, glink);
  251. return 0;
  252. }
  253. static int glink_rpm_remove(struct platform_device *pdev)
  254. {
  255. struct qcom_glink *glink = platform_get_drvdata(pdev);
  256. qcom_glink_native_remove(glink);
  257. return 0;
  258. }
  259. static const struct of_device_id glink_rpm_of_match[] = {
  260. { .compatible = "qcom,glink-rpm" },
  261. {}
  262. };
  263. MODULE_DEVICE_TABLE(of, glink_rpm_of_match);
  264. static struct platform_driver glink_rpm_driver = {
  265. .probe = glink_rpm_probe,
  266. .remove = glink_rpm_remove,
  267. .driver = {
  268. .name = "qcom_glink_rpm",
  269. .of_match_table = glink_rpm_of_match,
  270. },
  271. };
  272. static int __init glink_rpm_init(void)
  273. {
  274. return platform_driver_register(&glink_rpm_driver);
  275. }
  276. subsys_initcall(glink_rpm_init);
  277. static void __exit glink_rpm_exit(void)
  278. {
  279. platform_driver_unregister(&glink_rpm_driver);
  280. }
  281. module_exit(glink_rpm_exit);
  282. MODULE_AUTHOR("Bjorn Andersson <bjorn.andersson@linaro.org>");
  283. MODULE_DESCRIPTION("Qualcomm GLINK RPM driver");
  284. MODULE_LICENSE("GPL v2");