qcom_q6v5.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Qualcomm Peripheral Image Loader for Q6V5
  4. *
  5. * Copyright (C) 2016-2018 Linaro Ltd.
  6. * Copyright (C) 2014 Sony Mobile Communications AB
  7. * Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/module.h>
  13. #include <linux/soc/qcom/smem.h>
  14. #include <linux/soc/qcom/smem_state.h>
  15. #include <linux/remoteproc.h>
  16. #include "qcom_q6v5.h"
  17. /**
  18. * qcom_q6v5_prepare() - reinitialize the qcom_q6v5 context before start
  19. * @q6v5: reference to qcom_q6v5 context to be reinitialized
  20. *
  21. * Return: 0 on success, negative errno on failure
  22. */
  23. int qcom_q6v5_prepare(struct qcom_q6v5 *q6v5)
  24. {
  25. reinit_completion(&q6v5->start_done);
  26. reinit_completion(&q6v5->stop_done);
  27. q6v5->running = true;
  28. q6v5->handover_issued = false;
  29. enable_irq(q6v5->handover_irq);
  30. return 0;
  31. }
  32. EXPORT_SYMBOL_GPL(qcom_q6v5_prepare);
  33. /**
  34. * qcom_q6v5_unprepare() - unprepare the qcom_q6v5 context after stop
  35. * @q6v5: reference to qcom_q6v5 context to be unprepared
  36. *
  37. * Return: 0 on success, 1 if handover hasn't yet been called
  38. */
  39. int qcom_q6v5_unprepare(struct qcom_q6v5 *q6v5)
  40. {
  41. disable_irq(q6v5->handover_irq);
  42. return !q6v5->handover_issued;
  43. }
  44. EXPORT_SYMBOL_GPL(qcom_q6v5_unprepare);
  45. static irqreturn_t q6v5_wdog_interrupt(int irq, void *data)
  46. {
  47. struct qcom_q6v5 *q6v5 = data;
  48. size_t len;
  49. char *msg;
  50. /* Sometimes the stop triggers a watchdog rather than a stop-ack */
  51. if (!q6v5->running) {
  52. complete(&q6v5->stop_done);
  53. return IRQ_HANDLED;
  54. }
  55. msg = qcom_smem_get(QCOM_SMEM_HOST_ANY, q6v5->crash_reason, &len);
  56. if (!IS_ERR(msg) && len > 0 && msg[0])
  57. dev_err(q6v5->dev, "watchdog received: %s\n", msg);
  58. else
  59. dev_err(q6v5->dev, "watchdog without message\n");
  60. rproc_report_crash(q6v5->rproc, RPROC_WATCHDOG);
  61. return IRQ_HANDLED;
  62. }
  63. static irqreturn_t q6v5_fatal_interrupt(int irq, void *data)
  64. {
  65. struct qcom_q6v5 *q6v5 = data;
  66. size_t len;
  67. char *msg;
  68. msg = qcom_smem_get(QCOM_SMEM_HOST_ANY, q6v5->crash_reason, &len);
  69. if (!IS_ERR(msg) && len > 0 && msg[0])
  70. dev_err(q6v5->dev, "fatal error received: %s\n", msg);
  71. else
  72. dev_err(q6v5->dev, "fatal error without message\n");
  73. q6v5->running = false;
  74. rproc_report_crash(q6v5->rproc, RPROC_FATAL_ERROR);
  75. return IRQ_HANDLED;
  76. }
  77. static irqreturn_t q6v5_ready_interrupt(int irq, void *data)
  78. {
  79. struct qcom_q6v5 *q6v5 = data;
  80. complete(&q6v5->start_done);
  81. return IRQ_HANDLED;
  82. }
  83. /**
  84. * qcom_q6v5_wait_for_start() - wait for remote processor start signal
  85. * @q6v5: reference to qcom_q6v5 context
  86. * @timeout: timeout to wait for the event, in jiffies
  87. *
  88. * qcom_q6v5_unprepare() should not be called when this function fails.
  89. *
  90. * Return: 0 on success, -ETIMEDOUT on timeout
  91. */
  92. int qcom_q6v5_wait_for_start(struct qcom_q6v5 *q6v5, int timeout)
  93. {
  94. int ret;
  95. ret = wait_for_completion_timeout(&q6v5->start_done, timeout);
  96. if (!ret)
  97. disable_irq(q6v5->handover_irq);
  98. return !ret ? -ETIMEDOUT : 0;
  99. }
  100. EXPORT_SYMBOL_GPL(qcom_q6v5_wait_for_start);
  101. static irqreturn_t q6v5_handover_interrupt(int irq, void *data)
  102. {
  103. struct qcom_q6v5 *q6v5 = data;
  104. if (q6v5->handover)
  105. q6v5->handover(q6v5);
  106. q6v5->handover_issued = true;
  107. return IRQ_HANDLED;
  108. }
  109. static irqreturn_t q6v5_stop_interrupt(int irq, void *data)
  110. {
  111. struct qcom_q6v5 *q6v5 = data;
  112. complete(&q6v5->stop_done);
  113. return IRQ_HANDLED;
  114. }
  115. /**
  116. * qcom_q6v5_request_stop() - request the remote processor to stop
  117. * @q6v5: reference to qcom_q6v5 context
  118. *
  119. * Return: 0 on success, negative errno on failure
  120. */
  121. int qcom_q6v5_request_stop(struct qcom_q6v5 *q6v5)
  122. {
  123. int ret;
  124. q6v5->running = false;
  125. qcom_smem_state_update_bits(q6v5->state,
  126. BIT(q6v5->stop_bit), BIT(q6v5->stop_bit));
  127. ret = wait_for_completion_timeout(&q6v5->stop_done, 5 * HZ);
  128. qcom_smem_state_update_bits(q6v5->state, BIT(q6v5->stop_bit), 0);
  129. return ret == 0 ? -ETIMEDOUT : 0;
  130. }
  131. EXPORT_SYMBOL_GPL(qcom_q6v5_request_stop);
  132. /**
  133. * qcom_q6v5_init() - initializer of the q6v5 common struct
  134. * @q6v5: handle to be initialized
  135. * @pdev: platform_device reference for acquiring resources
  136. * @rproc: associated remoteproc instance
  137. * @crash_reason: SMEM id for crash reason string, or 0 if none
  138. * @handover: function to be called when proxy resources should be released
  139. *
  140. * Return: 0 on success, negative errno on failure
  141. */
  142. int qcom_q6v5_init(struct qcom_q6v5 *q6v5, struct platform_device *pdev,
  143. struct rproc *rproc, int crash_reason,
  144. void (*handover)(struct qcom_q6v5 *q6v5))
  145. {
  146. int ret;
  147. q6v5->rproc = rproc;
  148. q6v5->dev = &pdev->dev;
  149. q6v5->crash_reason = crash_reason;
  150. q6v5->handover = handover;
  151. init_completion(&q6v5->start_done);
  152. init_completion(&q6v5->stop_done);
  153. q6v5->wdog_irq = platform_get_irq_byname(pdev, "wdog");
  154. if (q6v5->wdog_irq < 0)
  155. return q6v5->wdog_irq;
  156. ret = devm_request_threaded_irq(&pdev->dev, q6v5->wdog_irq,
  157. NULL, q6v5_wdog_interrupt,
  158. IRQF_TRIGGER_RISING | IRQF_ONESHOT,
  159. "q6v5 wdog", q6v5);
  160. if (ret) {
  161. dev_err(&pdev->dev, "failed to acquire wdog IRQ\n");
  162. return ret;
  163. }
  164. q6v5->fatal_irq = platform_get_irq_byname(pdev, "fatal");
  165. if (q6v5->fatal_irq < 0)
  166. return q6v5->fatal_irq;
  167. ret = devm_request_threaded_irq(&pdev->dev, q6v5->fatal_irq,
  168. NULL, q6v5_fatal_interrupt,
  169. IRQF_TRIGGER_RISING | IRQF_ONESHOT,
  170. "q6v5 fatal", q6v5);
  171. if (ret) {
  172. dev_err(&pdev->dev, "failed to acquire fatal IRQ\n");
  173. return ret;
  174. }
  175. q6v5->ready_irq = platform_get_irq_byname(pdev, "ready");
  176. if (q6v5->ready_irq < 0)
  177. return q6v5->ready_irq;
  178. ret = devm_request_threaded_irq(&pdev->dev, q6v5->ready_irq,
  179. NULL, q6v5_ready_interrupt,
  180. IRQF_TRIGGER_RISING | IRQF_ONESHOT,
  181. "q6v5 ready", q6v5);
  182. if (ret) {
  183. dev_err(&pdev->dev, "failed to acquire ready IRQ\n");
  184. return ret;
  185. }
  186. q6v5->handover_irq = platform_get_irq_byname(pdev, "handover");
  187. if (q6v5->handover_irq < 0)
  188. return q6v5->handover_irq;
  189. ret = devm_request_threaded_irq(&pdev->dev, q6v5->handover_irq,
  190. NULL, q6v5_handover_interrupt,
  191. IRQF_TRIGGER_RISING | IRQF_ONESHOT,
  192. "q6v5 handover", q6v5);
  193. if (ret) {
  194. dev_err(&pdev->dev, "failed to acquire handover IRQ\n");
  195. return ret;
  196. }
  197. disable_irq(q6v5->handover_irq);
  198. q6v5->stop_irq = platform_get_irq_byname(pdev, "stop-ack");
  199. if (q6v5->stop_irq < 0)
  200. return q6v5->stop_irq;
  201. ret = devm_request_threaded_irq(&pdev->dev, q6v5->stop_irq,
  202. NULL, q6v5_stop_interrupt,
  203. IRQF_TRIGGER_RISING | IRQF_ONESHOT,
  204. "q6v5 stop", q6v5);
  205. if (ret) {
  206. dev_err(&pdev->dev, "failed to acquire stop-ack IRQ\n");
  207. return ret;
  208. }
  209. q6v5->state = qcom_smem_state_get(&pdev->dev, "stop", &q6v5->stop_bit);
  210. if (IS_ERR(q6v5->state)) {
  211. dev_err(&pdev->dev, "failed to acquire stop state\n");
  212. return PTR_ERR(q6v5->state);
  213. }
  214. return 0;
  215. }
  216. EXPORT_SYMBOL_GPL(qcom_q6v5_init);
  217. MODULE_LICENSE("GPL v2");
  218. MODULE_DESCRIPTION("Qualcomm Peripheral Image Loader for Q6V5");