1 // SPDX-License-Identifier: GPL-2.0
3 * Qualcomm Peripheral Image Loader for Q6V5
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.
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_common.h"
17 #include "qcom_q6v5.h"
19 #define Q6V5_PANIC_DELAY_MS 200
22 * qcom_q6v5_prepare() - reinitialize the qcom_q6v5 context before start
23 * @q6v5: reference to qcom_q6v5 context to be reinitialized
25 * Return: 0 on success, negative errno on failure
27 int qcom_q6v5_prepare(struct qcom_q6v5 *q6v5)
29 reinit_completion(&q6v5->start_done);
30 reinit_completion(&q6v5->stop_done);
33 q6v5->handover_issued = false;
35 enable_irq(q6v5->handover_irq);
39 EXPORT_SYMBOL_GPL(qcom_q6v5_prepare);
42 * qcom_q6v5_unprepare() - unprepare the qcom_q6v5 context after stop
43 * @q6v5: reference to qcom_q6v5 context to be unprepared
45 * Return: 0 on success, 1 if handover hasn't yet been called
47 int qcom_q6v5_unprepare(struct qcom_q6v5 *q6v5)
49 disable_irq(q6v5->handover_irq);
51 return !q6v5->handover_issued;
53 EXPORT_SYMBOL_GPL(qcom_q6v5_unprepare);
55 static irqreturn_t q6v5_wdog_interrupt(int irq, void *data)
57 struct qcom_q6v5 *q6v5 = data;
61 /* Sometimes the stop triggers a watchdog rather than a stop-ack */
63 complete(&q6v5->stop_done);
67 msg = qcom_smem_get(QCOM_SMEM_HOST_ANY, q6v5->crash_reason, &len);
68 if (!IS_ERR(msg) && len > 0 && msg[0])
69 dev_err(q6v5->dev, "watchdog received: %s\n", msg);
71 dev_err(q6v5->dev, "watchdog without message\n");
73 rproc_report_crash(q6v5->rproc, RPROC_WATCHDOG);
78 static irqreturn_t q6v5_fatal_interrupt(int irq, void *data)
80 struct qcom_q6v5 *q6v5 = data;
84 msg = qcom_smem_get(QCOM_SMEM_HOST_ANY, q6v5->crash_reason, &len);
85 if (!IS_ERR(msg) && len > 0 && msg[0])
86 dev_err(q6v5->dev, "fatal error received: %s\n", msg);
88 dev_err(q6v5->dev, "fatal error without message\n");
90 q6v5->running = false;
91 rproc_report_crash(q6v5->rproc, RPROC_FATAL_ERROR);
96 static irqreturn_t q6v5_ready_interrupt(int irq, void *data)
98 struct qcom_q6v5 *q6v5 = data;
100 complete(&q6v5->start_done);
106 * qcom_q6v5_wait_for_start() - wait for remote processor start signal
107 * @q6v5: reference to qcom_q6v5 context
108 * @timeout: timeout to wait for the event, in jiffies
110 * qcom_q6v5_unprepare() should not be called when this function fails.
112 * Return: 0 on success, -ETIMEDOUT on timeout
114 int qcom_q6v5_wait_for_start(struct qcom_q6v5 *q6v5, int timeout)
118 ret = wait_for_completion_timeout(&q6v5->start_done, timeout);
120 disable_irq(q6v5->handover_irq);
122 return !ret ? -ETIMEDOUT : 0;
124 EXPORT_SYMBOL_GPL(qcom_q6v5_wait_for_start);
126 static irqreturn_t q6v5_handover_interrupt(int irq, void *data)
128 struct qcom_q6v5 *q6v5 = data;
131 q6v5->handover(q6v5);
133 q6v5->handover_issued = true;
138 static irqreturn_t q6v5_stop_interrupt(int irq, void *data)
140 struct qcom_q6v5 *q6v5 = data;
142 complete(&q6v5->stop_done);
148 * qcom_q6v5_request_stop() - request the remote processor to stop
149 * @q6v5: reference to qcom_q6v5 context
150 * @sysmon: reference to the remote's sysmon instance, or NULL
152 * Return: 0 on success, negative errno on failure
154 int qcom_q6v5_request_stop(struct qcom_q6v5 *q6v5, struct qcom_sysmon *sysmon)
158 q6v5->running = false;
160 /* Don't perform SMP2P dance if sysmon already shut down the remote */
161 if (qcom_sysmon_shutdown_acked(sysmon))
164 qcom_smem_state_update_bits(q6v5->state,
165 BIT(q6v5->stop_bit), BIT(q6v5->stop_bit));
167 ret = wait_for_completion_timeout(&q6v5->stop_done, 5 * HZ);
169 qcom_smem_state_update_bits(q6v5->state, BIT(q6v5->stop_bit), 0);
171 return ret == 0 ? -ETIMEDOUT : 0;
173 EXPORT_SYMBOL_GPL(qcom_q6v5_request_stop);
176 * qcom_q6v5_panic() - panic handler to invoke a stop on the remote
177 * @q6v5: reference to qcom_q6v5 context
179 * Set the stop bit and sleep in order to allow the remote processor to flush
180 * its caches etc for post mortem debugging.
184 unsigned long qcom_q6v5_panic(struct qcom_q6v5 *q6v5)
186 qcom_smem_state_update_bits(q6v5->state,
187 BIT(q6v5->stop_bit), BIT(q6v5->stop_bit));
189 return Q6V5_PANIC_DELAY_MS;
191 EXPORT_SYMBOL_GPL(qcom_q6v5_panic);
194 * qcom_q6v5_init() - initializer of the q6v5 common struct
195 * @q6v5: handle to be initialized
196 * @pdev: platform_device reference for acquiring resources
197 * @rproc: associated remoteproc instance
198 * @crash_reason: SMEM id for crash reason string, or 0 if none
199 * @handover: function to be called when proxy resources should be released
201 * Return: 0 on success, negative errno on failure
203 int qcom_q6v5_init(struct qcom_q6v5 *q6v5, struct platform_device *pdev,
204 struct rproc *rproc, int crash_reason,
205 void (*handover)(struct qcom_q6v5 *q6v5))
210 q6v5->dev = &pdev->dev;
211 q6v5->crash_reason = crash_reason;
212 q6v5->handover = handover;
214 init_completion(&q6v5->start_done);
215 init_completion(&q6v5->stop_done);
217 q6v5->wdog_irq = platform_get_irq_byname(pdev, "wdog");
218 if (q6v5->wdog_irq < 0)
219 return q6v5->wdog_irq;
221 ret = devm_request_threaded_irq(&pdev->dev, q6v5->wdog_irq,
222 NULL, q6v5_wdog_interrupt,
223 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
226 dev_err(&pdev->dev, "failed to acquire wdog IRQ\n");
230 q6v5->fatal_irq = platform_get_irq_byname(pdev, "fatal");
231 if (q6v5->fatal_irq < 0)
232 return q6v5->fatal_irq;
234 ret = devm_request_threaded_irq(&pdev->dev, q6v5->fatal_irq,
235 NULL, q6v5_fatal_interrupt,
236 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
239 dev_err(&pdev->dev, "failed to acquire fatal IRQ\n");
243 q6v5->ready_irq = platform_get_irq_byname(pdev, "ready");
244 if (q6v5->ready_irq < 0)
245 return q6v5->ready_irq;
247 ret = devm_request_threaded_irq(&pdev->dev, q6v5->ready_irq,
248 NULL, q6v5_ready_interrupt,
249 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
252 dev_err(&pdev->dev, "failed to acquire ready IRQ\n");
256 q6v5->handover_irq = platform_get_irq_byname(pdev, "handover");
257 if (q6v5->handover_irq < 0)
258 return q6v5->handover_irq;
260 ret = devm_request_threaded_irq(&pdev->dev, q6v5->handover_irq,
261 NULL, q6v5_handover_interrupt,
262 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
263 "q6v5 handover", q6v5);
265 dev_err(&pdev->dev, "failed to acquire handover IRQ\n");
268 disable_irq(q6v5->handover_irq);
270 q6v5->stop_irq = platform_get_irq_byname(pdev, "stop-ack");
271 if (q6v5->stop_irq < 0)
272 return q6v5->stop_irq;
274 ret = devm_request_threaded_irq(&pdev->dev, q6v5->stop_irq,
275 NULL, q6v5_stop_interrupt,
276 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
279 dev_err(&pdev->dev, "failed to acquire stop-ack IRQ\n");
283 q6v5->state = qcom_smem_state_get(&pdev->dev, "stop", &q6v5->stop_bit);
284 if (IS_ERR(q6v5->state)) {
285 dev_err(&pdev->dev, "failed to acquire stop state\n");
286 return PTR_ERR(q6v5->state);
291 EXPORT_SYMBOL_GPL(qcom_q6v5_init);
293 MODULE_LICENSE("GPL v2");
294 MODULE_DESCRIPTION("Qualcomm Peripheral Image Loader for Q6V5");