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/interconnect.h>
12 #include <linux/interrupt.h>
13 #include <linux/module.h>
14 #include <linux/soc/qcom/qcom_aoss.h>
15 #include <linux/soc/qcom/smem.h>
16 #include <linux/soc/qcom/smem_state.h>
17 #include <linux/remoteproc.h>
18 #include "qcom_common.h"
19 #include "qcom_q6v5.h"
21 #define Q6V5_LOAD_STATE_MSG_LEN 64
22 #define Q6V5_PANIC_DELAY_MS 200
24 static int q6v5_load_state_toggle(struct qcom_q6v5 *q6v5, bool enable)
31 ret = qmp_send(q6v5->qmp, "{class: image, res: load_state, name: %s, val: %s}",
32 q6v5->load_state, enable ? "on" : "off");
34 dev_err(q6v5->dev, "failed to toggle load state\n");
40 * qcom_q6v5_prepare() - reinitialize the qcom_q6v5 context before start
41 * @q6v5: reference to qcom_q6v5 context to be reinitialized
43 * Return: 0 on success, negative errno on failure
45 int qcom_q6v5_prepare(struct qcom_q6v5 *q6v5)
49 ret = icc_set_bw(q6v5->path, 0, UINT_MAX);
51 dev_err(q6v5->dev, "failed to set bandwidth request\n");
55 ret = q6v5_load_state_toggle(q6v5, true);
57 icc_set_bw(q6v5->path, 0, 0);
61 reinit_completion(&q6v5->start_done);
62 reinit_completion(&q6v5->stop_done);
65 q6v5->handover_issued = false;
67 enable_irq(q6v5->handover_irq);
71 EXPORT_SYMBOL_GPL(qcom_q6v5_prepare);
74 * qcom_q6v5_unprepare() - unprepare the qcom_q6v5 context after stop
75 * @q6v5: reference to qcom_q6v5 context to be unprepared
77 * Return: 0 on success, 1 if handover hasn't yet been called
79 int qcom_q6v5_unprepare(struct qcom_q6v5 *q6v5)
81 disable_irq(q6v5->handover_irq);
82 q6v5_load_state_toggle(q6v5, false);
84 /* Disable interconnect vote, in case handover never happened */
85 icc_set_bw(q6v5->path, 0, 0);
87 return !q6v5->handover_issued;
89 EXPORT_SYMBOL_GPL(qcom_q6v5_unprepare);
91 static irqreturn_t q6v5_wdog_interrupt(int irq, void *data)
93 struct qcom_q6v5 *q6v5 = data;
97 /* Sometimes the stop triggers a watchdog rather than a stop-ack */
99 complete(&q6v5->stop_done);
103 msg = qcom_smem_get(QCOM_SMEM_HOST_ANY, q6v5->crash_reason, &len);
104 if (!IS_ERR(msg) && len > 0 && msg[0])
105 dev_err(q6v5->dev, "watchdog received: %s\n", msg);
107 dev_err(q6v5->dev, "watchdog without message\n");
109 q6v5->running = false;
110 rproc_report_crash(q6v5->rproc, RPROC_WATCHDOG);
115 static irqreturn_t q6v5_fatal_interrupt(int irq, void *data)
117 struct qcom_q6v5 *q6v5 = data;
124 msg = qcom_smem_get(QCOM_SMEM_HOST_ANY, q6v5->crash_reason, &len);
125 if (!IS_ERR(msg) && len > 0 && msg[0])
126 dev_err(q6v5->dev, "fatal error received: %s\n", msg);
128 dev_err(q6v5->dev, "fatal error without message\n");
130 q6v5->running = false;
131 rproc_report_crash(q6v5->rproc, RPROC_FATAL_ERROR);
136 static irqreturn_t q6v5_ready_interrupt(int irq, void *data)
138 struct qcom_q6v5 *q6v5 = data;
140 complete(&q6v5->start_done);
146 * qcom_q6v5_wait_for_start() - wait for remote processor start signal
147 * @q6v5: reference to qcom_q6v5 context
148 * @timeout: timeout to wait for the event, in jiffies
150 * qcom_q6v5_unprepare() should not be called when this function fails.
152 * Return: 0 on success, -ETIMEDOUT on timeout
154 int qcom_q6v5_wait_for_start(struct qcom_q6v5 *q6v5, int timeout)
158 ret = wait_for_completion_timeout(&q6v5->start_done, timeout);
160 disable_irq(q6v5->handover_irq);
162 return !ret ? -ETIMEDOUT : 0;
164 EXPORT_SYMBOL_GPL(qcom_q6v5_wait_for_start);
166 static irqreturn_t q6v5_handover_interrupt(int irq, void *data)
168 struct qcom_q6v5 *q6v5 = data;
171 q6v5->handover(q6v5);
173 icc_set_bw(q6v5->path, 0, 0);
175 q6v5->handover_issued = true;
180 static irqreturn_t q6v5_stop_interrupt(int irq, void *data)
182 struct qcom_q6v5 *q6v5 = data;
184 complete(&q6v5->stop_done);
190 * qcom_q6v5_request_stop() - request the remote processor to stop
191 * @q6v5: reference to qcom_q6v5 context
192 * @sysmon: reference to the remote's sysmon instance, or NULL
194 * Return: 0 on success, negative errno on failure
196 int qcom_q6v5_request_stop(struct qcom_q6v5 *q6v5, struct qcom_sysmon *sysmon)
200 q6v5->running = false;
202 /* Don't perform SMP2P dance if remote isn't running */
203 if (q6v5->rproc->state != RPROC_RUNNING || qcom_sysmon_shutdown_acked(sysmon))
206 qcom_smem_state_update_bits(q6v5->state,
207 BIT(q6v5->stop_bit), BIT(q6v5->stop_bit));
209 ret = wait_for_completion_timeout(&q6v5->stop_done, 5 * HZ);
211 qcom_smem_state_update_bits(q6v5->state, BIT(q6v5->stop_bit), 0);
213 return ret == 0 ? -ETIMEDOUT : 0;
215 EXPORT_SYMBOL_GPL(qcom_q6v5_request_stop);
218 * qcom_q6v5_panic() - panic handler to invoke a stop on the remote
219 * @q6v5: reference to qcom_q6v5 context
221 * Set the stop bit and sleep in order to allow the remote processor to flush
222 * its caches etc for post mortem debugging.
226 unsigned long qcom_q6v5_panic(struct qcom_q6v5 *q6v5)
228 qcom_smem_state_update_bits(q6v5->state,
229 BIT(q6v5->stop_bit), BIT(q6v5->stop_bit));
231 return Q6V5_PANIC_DELAY_MS;
233 EXPORT_SYMBOL_GPL(qcom_q6v5_panic);
236 * qcom_q6v5_init() - initializer of the q6v5 common struct
237 * @q6v5: handle to be initialized
238 * @pdev: platform_device reference for acquiring resources
239 * @rproc: associated remoteproc instance
240 * @crash_reason: SMEM id for crash reason string, or 0 if none
241 * @load_state: load state resource string
242 * @handover: function to be called when proxy resources should be released
244 * Return: 0 on success, negative errno on failure
246 int qcom_q6v5_init(struct qcom_q6v5 *q6v5, struct platform_device *pdev,
247 struct rproc *rproc, int crash_reason, const char *load_state,
248 void (*handover)(struct qcom_q6v5 *q6v5))
253 q6v5->dev = &pdev->dev;
254 q6v5->crash_reason = crash_reason;
255 q6v5->handover = handover;
257 init_completion(&q6v5->start_done);
258 init_completion(&q6v5->stop_done);
260 q6v5->wdog_irq = platform_get_irq_byname(pdev, "wdog");
261 if (q6v5->wdog_irq < 0)
262 return q6v5->wdog_irq;
264 ret = devm_request_threaded_irq(&pdev->dev, q6v5->wdog_irq,
265 NULL, q6v5_wdog_interrupt,
266 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
269 dev_err(&pdev->dev, "failed to acquire wdog IRQ\n");
273 q6v5->fatal_irq = platform_get_irq_byname(pdev, "fatal");
274 if (q6v5->fatal_irq < 0)
275 return q6v5->fatal_irq;
277 ret = devm_request_threaded_irq(&pdev->dev, q6v5->fatal_irq,
278 NULL, q6v5_fatal_interrupt,
279 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
282 dev_err(&pdev->dev, "failed to acquire fatal IRQ\n");
286 q6v5->ready_irq = platform_get_irq_byname(pdev, "ready");
287 if (q6v5->ready_irq < 0)
288 return q6v5->ready_irq;
290 ret = devm_request_threaded_irq(&pdev->dev, q6v5->ready_irq,
291 NULL, q6v5_ready_interrupt,
292 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
295 dev_err(&pdev->dev, "failed to acquire ready IRQ\n");
299 q6v5->handover_irq = platform_get_irq_byname(pdev, "handover");
300 if (q6v5->handover_irq < 0)
301 return q6v5->handover_irq;
303 ret = devm_request_threaded_irq(&pdev->dev, q6v5->handover_irq,
304 NULL, q6v5_handover_interrupt,
305 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
306 "q6v5 handover", q6v5);
308 dev_err(&pdev->dev, "failed to acquire handover IRQ\n");
311 disable_irq(q6v5->handover_irq);
313 q6v5->stop_irq = platform_get_irq_byname(pdev, "stop-ack");
314 if (q6v5->stop_irq < 0)
315 return q6v5->stop_irq;
317 ret = devm_request_threaded_irq(&pdev->dev, q6v5->stop_irq,
318 NULL, q6v5_stop_interrupt,
319 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
322 dev_err(&pdev->dev, "failed to acquire stop-ack IRQ\n");
326 q6v5->state = devm_qcom_smem_state_get(&pdev->dev, "stop", &q6v5->stop_bit);
327 if (IS_ERR(q6v5->state)) {
328 dev_err(&pdev->dev, "failed to acquire stop state\n");
329 return PTR_ERR(q6v5->state);
332 q6v5->load_state = devm_kstrdup_const(&pdev->dev, load_state, GFP_KERNEL);
333 q6v5->qmp = qmp_get(&pdev->dev);
334 if (IS_ERR(q6v5->qmp)) {
335 if (PTR_ERR(q6v5->qmp) != -ENODEV)
336 return dev_err_probe(&pdev->dev, PTR_ERR(q6v5->qmp),
337 "failed to acquire load state\n");
339 } else if (!q6v5->load_state) {
341 dev_err(&pdev->dev, "load state resource string empty\n");
344 return load_state ? -ENOMEM : -EINVAL;
347 q6v5->path = devm_of_icc_get(&pdev->dev, NULL);
348 if (IS_ERR(q6v5->path))
349 return dev_err_probe(&pdev->dev, PTR_ERR(q6v5->path),
350 "failed to acquire interconnect path\n");
354 EXPORT_SYMBOL_GPL(qcom_q6v5_init);
357 * qcom_q6v5_deinit() - deinitialize the q6v5 common struct
358 * @q6v5: reference to qcom_q6v5 context to be deinitialized
360 void qcom_q6v5_deinit(struct qcom_q6v5 *q6v5)
364 EXPORT_SYMBOL_GPL(qcom_q6v5_deinit);
366 MODULE_LICENSE("GPL v2");
367 MODULE_DESCRIPTION("Qualcomm Peripheral Image Loader for Q6V5");