glink_ssr.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * Copyright (c) 2014-2017, The Linux Foundation. All rights reserved.
  3. * Copyright (c) 2017, Linaro Ltd.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 and
  7. * only version 2 as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include <linux/completion.h>
  15. #include <linux/module.h>
  16. #include <linux/notifier.h>
  17. #include <linux/rpmsg.h>
  18. #include <linux/remoteproc/qcom_rproc.h>
  19. /**
  20. * struct do_cleanup_msg - The data structure for an SSR do_cleanup message
  21. * version: The G-Link SSR protocol version
  22. * command: The G-Link SSR command - do_cleanup
  23. * seq_num: Sequence number
  24. * name_len: Length of the name of the subsystem being restarted
  25. * name: G-Link edge name of the subsystem being restarted
  26. */
  27. struct do_cleanup_msg {
  28. __le32 version;
  29. __le32 command;
  30. __le32 seq_num;
  31. __le32 name_len;
  32. char name[32];
  33. };
  34. /**
  35. * struct cleanup_done_msg - The data structure for an SSR cleanup_done message
  36. * version: The G-Link SSR protocol version
  37. * response: The G-Link SSR response to a do_cleanup command, cleanup_done
  38. * seq_num: Sequence number
  39. */
  40. struct cleanup_done_msg {
  41. __le32 version;
  42. __le32 response;
  43. __le32 seq_num;
  44. };
  45. /**
  46. * G-Link SSR protocol commands
  47. */
  48. #define GLINK_SSR_DO_CLEANUP 0
  49. #define GLINK_SSR_CLEANUP_DONE 1
  50. struct glink_ssr {
  51. struct device *dev;
  52. struct rpmsg_endpoint *ept;
  53. struct notifier_block nb;
  54. u32 seq_num;
  55. struct completion completion;
  56. };
  57. static int qcom_glink_ssr_callback(struct rpmsg_device *rpdev,
  58. void *data, int len, void *priv, u32 addr)
  59. {
  60. struct cleanup_done_msg *msg = data;
  61. struct glink_ssr *ssr = dev_get_drvdata(&rpdev->dev);
  62. if (len < sizeof(*msg)) {
  63. dev_err(ssr->dev, "message too short\n");
  64. return -EINVAL;
  65. }
  66. if (le32_to_cpu(msg->version) != 0)
  67. return -EINVAL;
  68. if (le32_to_cpu(msg->response) != GLINK_SSR_CLEANUP_DONE)
  69. return 0;
  70. if (le32_to_cpu(msg->seq_num) != ssr->seq_num) {
  71. dev_err(ssr->dev, "invalid sequence number of response\n");
  72. return -EINVAL;
  73. }
  74. complete(&ssr->completion);
  75. return 0;
  76. }
  77. static int qcom_glink_ssr_notify(struct notifier_block *nb, unsigned long event,
  78. void *data)
  79. {
  80. struct glink_ssr *ssr = container_of(nb, struct glink_ssr, nb);
  81. struct do_cleanup_msg msg;
  82. char *ssr_name = data;
  83. int ret;
  84. ssr->seq_num++;
  85. reinit_completion(&ssr->completion);
  86. memset(&msg, 0, sizeof(msg));
  87. msg.command = cpu_to_le32(GLINK_SSR_DO_CLEANUP);
  88. msg.seq_num = cpu_to_le32(ssr->seq_num);
  89. msg.name_len = cpu_to_le32(strlen(ssr_name));
  90. strlcpy(msg.name, ssr_name, sizeof(msg.name));
  91. ret = rpmsg_send(ssr->ept, &msg, sizeof(msg));
  92. if (ret < 0)
  93. dev_err(ssr->dev, "failed to send cleanup message\n");
  94. ret = wait_for_completion_timeout(&ssr->completion, HZ);
  95. if (!ret)
  96. dev_err(ssr->dev, "timeout waiting for cleanup done message\n");
  97. return NOTIFY_DONE;
  98. }
  99. static int qcom_glink_ssr_probe(struct rpmsg_device *rpdev)
  100. {
  101. struct glink_ssr *ssr;
  102. ssr = devm_kzalloc(&rpdev->dev, sizeof(*ssr), GFP_KERNEL);
  103. if (!ssr)
  104. return -ENOMEM;
  105. init_completion(&ssr->completion);
  106. ssr->dev = &rpdev->dev;
  107. ssr->ept = rpdev->ept;
  108. ssr->nb.notifier_call = qcom_glink_ssr_notify;
  109. dev_set_drvdata(&rpdev->dev, ssr);
  110. return qcom_register_ssr_notifier(&ssr->nb);
  111. }
  112. static void qcom_glink_ssr_remove(struct rpmsg_device *rpdev)
  113. {
  114. struct glink_ssr *ssr = dev_get_drvdata(&rpdev->dev);
  115. qcom_unregister_ssr_notifier(&ssr->nb);
  116. }
  117. static const struct rpmsg_device_id qcom_glink_ssr_match[] = {
  118. { "glink_ssr" },
  119. {}
  120. };
  121. static struct rpmsg_driver qcom_glink_ssr_driver = {
  122. .probe = qcom_glink_ssr_probe,
  123. .remove = qcom_glink_ssr_remove,
  124. .callback = qcom_glink_ssr_callback,
  125. .id_table = qcom_glink_ssr_match,
  126. .drv = {
  127. .name = "qcom_glink_ssr",
  128. },
  129. };
  130. module_rpmsg_driver(qcom_glink_ssr_driver);
  131. MODULE_ALIAS("rpmsg:glink_ssr");
  132. MODULE_DESCRIPTION("Qualcomm GLINK SSR notifier");
  133. MODULE_LICENSE("GPL v2");