smd-rpm.c 6.0 KB

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