smd-rpm.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2015, Sony Mobile Communications AB.
  4. * Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
  5. */
  6. #include <linux/module.h>
  7. #include <linux/platform_device.h>
  8. #include <linux/of_platform.h>
  9. #include <linux/io.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/slab.h>
  12. #include <linux/rpmsg.h>
  13. #include <linux/soc/qcom/smd-rpm.h>
  14. #define RPM_REQUEST_TIMEOUT (5 * HZ)
  15. /**
  16. * struct qcom_smd_rpm - state of the rpm device driver
  17. * @rpm_channel: reference to the smd channel
  18. * @icc: interconnect proxy device
  19. * @dev: rpm device
  20. * @ack: completion for acks
  21. * @lock: mutual exclusion around the send/complete pair
  22. * @ack_status: result of the rpm request
  23. */
  24. struct qcom_smd_rpm {
  25. struct rpmsg_endpoint *rpm_channel;
  26. struct platform_device *icc;
  27. struct device *dev;
  28. struct completion ack;
  29. struct mutex lock;
  30. int ack_status;
  31. };
  32. /**
  33. * struct qcom_rpm_header - header for all rpm requests and responses
  34. * @service_type: identifier of the service
  35. * @length: length of the payload
  36. */
  37. struct qcom_rpm_header {
  38. __le32 service_type;
  39. __le32 length;
  40. };
  41. /**
  42. * struct qcom_rpm_request - request message to the rpm
  43. * @msg_id: identifier of the outgoing message
  44. * @flags: active/sleep state flags
  45. * @type: resource type
  46. * @id: resource id
  47. * @data_len: length of the payload following this header
  48. */
  49. struct qcom_rpm_request {
  50. __le32 msg_id;
  51. __le32 flags;
  52. __le32 type;
  53. __le32 id;
  54. __le32 data_len;
  55. };
  56. /**
  57. * struct qcom_rpm_message - response message from the rpm
  58. * @msg_type: indicator of the type of message
  59. * @length: the size of this message, including the message header
  60. * @msg_id: message id
  61. * @message: textual message from the rpm
  62. *
  63. * Multiple of these messages can be stacked in an rpm message.
  64. */
  65. struct qcom_rpm_message {
  66. __le32 msg_type;
  67. __le32 length;
  68. union {
  69. __le32 msg_id;
  70. u8 message[0];
  71. };
  72. };
  73. #define RPM_SERVICE_TYPE_REQUEST 0x00716572 /* "req\0" */
  74. #define RPM_MSG_TYPE_ERR 0x00727265 /* "err\0" */
  75. #define RPM_MSG_TYPE_MSG_ID 0x2367736d /* "msg#" */
  76. /**
  77. * qcom_rpm_smd_write - write @buf to @type:@id
  78. * @rpm: rpm handle
  79. * @state: active/sleep state flags
  80. * @type: resource type
  81. * @id: resource identifier
  82. * @buf: the data to be written
  83. * @count: number of bytes in @buf
  84. */
  85. int qcom_rpm_smd_write(struct qcom_smd_rpm *rpm,
  86. int state,
  87. u32 type, u32 id,
  88. void *buf,
  89. size_t count)
  90. {
  91. static unsigned msg_id = 1;
  92. int left;
  93. int ret;
  94. struct {
  95. struct qcom_rpm_header hdr;
  96. struct qcom_rpm_request req;
  97. u8 payload[];
  98. } *pkt;
  99. size_t size = sizeof(*pkt) + count;
  100. /* SMD packets to the RPM may not exceed 256 bytes */
  101. if (WARN_ON(size >= 256))
  102. return -EINVAL;
  103. pkt = kmalloc(size, GFP_KERNEL);
  104. if (!pkt)
  105. return -ENOMEM;
  106. mutex_lock(&rpm->lock);
  107. pkt->hdr.service_type = cpu_to_le32(RPM_SERVICE_TYPE_REQUEST);
  108. pkt->hdr.length = cpu_to_le32(sizeof(struct qcom_rpm_request) + count);
  109. pkt->req.msg_id = cpu_to_le32(msg_id++);
  110. pkt->req.flags = cpu_to_le32(state);
  111. pkt->req.type = cpu_to_le32(type);
  112. pkt->req.id = cpu_to_le32(id);
  113. pkt->req.data_len = cpu_to_le32(count);
  114. memcpy(pkt->payload, buf, count);
  115. ret = rpmsg_send(rpm->rpm_channel, pkt, size);
  116. if (ret)
  117. goto out;
  118. left = wait_for_completion_timeout(&rpm->ack, RPM_REQUEST_TIMEOUT);
  119. if (!left)
  120. ret = -ETIMEDOUT;
  121. else
  122. ret = rpm->ack_status;
  123. out:
  124. kfree(pkt);
  125. mutex_unlock(&rpm->lock);
  126. return ret;
  127. }
  128. EXPORT_SYMBOL(qcom_rpm_smd_write);
  129. static int qcom_smd_rpm_callback(struct rpmsg_device *rpdev,
  130. void *data,
  131. int count,
  132. void *priv,
  133. u32 addr)
  134. {
  135. const struct qcom_rpm_header *hdr = data;
  136. size_t hdr_length = le32_to_cpu(hdr->length);
  137. const struct qcom_rpm_message *msg;
  138. struct qcom_smd_rpm *rpm = dev_get_drvdata(&rpdev->dev);
  139. const u8 *buf = data + sizeof(struct qcom_rpm_header);
  140. const u8 *end = buf + hdr_length;
  141. char msgbuf[32];
  142. int status = 0;
  143. u32 len, msg_length;
  144. if (le32_to_cpu(hdr->service_type) != RPM_SERVICE_TYPE_REQUEST ||
  145. hdr_length < sizeof(struct qcom_rpm_message)) {
  146. dev_err(rpm->dev, "invalid request\n");
  147. return 0;
  148. }
  149. while (buf < end) {
  150. msg = (struct qcom_rpm_message *)buf;
  151. msg_length = le32_to_cpu(msg->length);
  152. switch (le32_to_cpu(msg->msg_type)) {
  153. case RPM_MSG_TYPE_MSG_ID:
  154. break;
  155. case RPM_MSG_TYPE_ERR:
  156. len = min_t(u32, ALIGN(msg_length, 4), sizeof(msgbuf));
  157. memcpy_fromio(msgbuf, msg->message, len);
  158. msgbuf[len - 1] = 0;
  159. if (!strcmp(msgbuf, "resource does not exist"))
  160. status = -ENXIO;
  161. else
  162. status = -EINVAL;
  163. break;
  164. }
  165. buf = PTR_ALIGN(buf + 2 * sizeof(u32) + msg_length, 4);
  166. }
  167. rpm->ack_status = status;
  168. complete(&rpm->ack);
  169. return 0;
  170. }
  171. static int qcom_smd_rpm_probe(struct rpmsg_device *rpdev)
  172. {
  173. struct qcom_smd_rpm *rpm;
  174. int ret;
  175. rpm = devm_kzalloc(&rpdev->dev, sizeof(*rpm), GFP_KERNEL);
  176. if (!rpm)
  177. return -ENOMEM;
  178. mutex_init(&rpm->lock);
  179. init_completion(&rpm->ack);
  180. rpm->dev = &rpdev->dev;
  181. rpm->rpm_channel = rpdev->ept;
  182. dev_set_drvdata(&rpdev->dev, rpm);
  183. rpm->icc = platform_device_register_data(&rpdev->dev, "icc_smd_rpm", -1,
  184. NULL, 0);
  185. if (IS_ERR(rpm->icc))
  186. return PTR_ERR(rpm->icc);
  187. ret = of_platform_populate(rpdev->dev.of_node, NULL, NULL, &rpdev->dev);
  188. if (ret)
  189. platform_device_unregister(rpm->icc);
  190. return ret;
  191. }
  192. static void qcom_smd_rpm_remove(struct rpmsg_device *rpdev)
  193. {
  194. struct qcom_smd_rpm *rpm = dev_get_drvdata(&rpdev->dev);
  195. platform_device_unregister(rpm->icc);
  196. of_platform_depopulate(&rpdev->dev);
  197. }
  198. static const struct of_device_id qcom_smd_rpm_of_match[] = {
  199. { .compatible = "qcom,rpm-apq8084" },
  200. { .compatible = "qcom,rpm-ipq6018" },
  201. { .compatible = "qcom,rpm-msm8916" },
  202. { .compatible = "qcom,rpm-msm8936" },
  203. { .compatible = "qcom,rpm-msm8974" },
  204. { .compatible = "qcom,rpm-msm8976" },
  205. { .compatible = "qcom,rpm-msm8994" },
  206. { .compatible = "qcom,rpm-msm8996" },
  207. { .compatible = "qcom,rpm-msm8998" },
  208. { .compatible = "qcom,rpm-sdm660" },
  209. { .compatible = "qcom,rpm-qcs404" },
  210. {}
  211. };
  212. MODULE_DEVICE_TABLE(of, qcom_smd_rpm_of_match);
  213. static struct rpmsg_driver qcom_smd_rpm_driver = {
  214. .probe = qcom_smd_rpm_probe,
  215. .remove = qcom_smd_rpm_remove,
  216. .callback = qcom_smd_rpm_callback,
  217. .drv = {
  218. .name = "qcom_smd_rpm",
  219. .of_match_table = qcom_smd_rpm_of_match,
  220. },
  221. };
  222. static int __init qcom_smd_rpm_init(void)
  223. {
  224. return register_rpmsg_driver(&qcom_smd_rpm_driver);
  225. }
  226. arch_initcall(qcom_smd_rpm_init);
  227. static void __exit qcom_smd_rpm_exit(void)
  228. {
  229. unregister_rpmsg_driver(&qcom_smd_rpm_driver);
  230. }
  231. module_exit(qcom_smd_rpm_exit);
  232. MODULE_AUTHOR("Bjorn Andersson <bjorn.andersson@sonymobile.com>");
  233. MODULE_DESCRIPTION("Qualcomm SMD backed RPM driver");
  234. MODULE_LICENSE("GPL v2");