blob: eada7e34f3af51d7f5b0da137b8626d146bfc957 [file] [log] [blame]
Bjorn Andersson3b415c82018-06-04 13:30:36 -07001// 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>
Arnd Bergmannded79d02018-07-06 14:57:53 +020012#include <linux/module.h>
Bjorn Andersson3b415c82018-06-04 13:30:36 -070013#include <linux/soc/qcom/smem.h>
14#include <linux/soc/qcom/smem_state.h>
15#include <linux/remoteproc.h>
Bjorn Anderssoned5da802020-11-21 21:41:34 -080016#include "qcom_common.h"
Bjorn Andersson3b415c82018-06-04 13:30:36 -070017#include "qcom_q6v5.h"
18
Sibi Sankarc1fe10d2021-09-16 19:29:21 +053019#define Q6V5_LOAD_STATE_MSG_LEN 64
Bjorn Anderssone9142f52020-03-23 22:29:03 -070020#define Q6V5_PANIC_DELAY_MS 200
21
Sibi Sankarc1fe10d2021-09-16 19:29:21 +053022static int q6v5_load_state_toggle(struct qcom_q6v5 *q6v5, bool enable)
23{
24 char buf[Q6V5_LOAD_STATE_MSG_LEN];
25 int ret;
26
27 if (!q6v5->qmp)
28 return 0;
29
30 ret = snprintf(buf, sizeof(buf),
31 "{class: image, res: load_state, name: %s, val: %s}",
32 q6v5->load_state, enable ? "on" : "off");
33
34 WARN_ON(ret >= Q6V5_LOAD_STATE_MSG_LEN);
35
36 ret = qmp_send(q6v5->qmp, buf, sizeof(buf));
37 if (ret)
38 dev_err(q6v5->dev, "failed to toggle load state\n");
39
40 return ret;
41}
42
Bjorn Andersson3b415c82018-06-04 13:30:36 -070043/**
44 * qcom_q6v5_prepare() - reinitialize the qcom_q6v5 context before start
45 * @q6v5: reference to qcom_q6v5 context to be reinitialized
46 *
47 * Return: 0 on success, negative errno on failure
48 */
49int qcom_q6v5_prepare(struct qcom_q6v5 *q6v5)
50{
Sibi Sankarc1fe10d2021-09-16 19:29:21 +053051 int ret;
52
53 ret = q6v5_load_state_toggle(q6v5, true);
54 if (ret)
55 return ret;
56
Bjorn Andersson3b415c82018-06-04 13:30:36 -070057 reinit_completion(&q6v5->start_done);
58 reinit_completion(&q6v5->stop_done);
59
60 q6v5->running = true;
61 q6v5->handover_issued = false;
62
63 enable_irq(q6v5->handover_irq);
64
65 return 0;
66}
Arnd Bergmannded79d02018-07-06 14:57:53 +020067EXPORT_SYMBOL_GPL(qcom_q6v5_prepare);
Bjorn Andersson3b415c82018-06-04 13:30:36 -070068
69/**
70 * qcom_q6v5_unprepare() - unprepare the qcom_q6v5 context after stop
71 * @q6v5: reference to qcom_q6v5 context to be unprepared
72 *
73 * Return: 0 on success, 1 if handover hasn't yet been called
74 */
75int qcom_q6v5_unprepare(struct qcom_q6v5 *q6v5)
76{
77 disable_irq(q6v5->handover_irq);
Sibi Sankarc1fe10d2021-09-16 19:29:21 +053078 q6v5_load_state_toggle(q6v5, false);
Bjorn Andersson3b415c82018-06-04 13:30:36 -070079
80 return !q6v5->handover_issued;
81}
Arnd Bergmannded79d02018-07-06 14:57:53 +020082EXPORT_SYMBOL_GPL(qcom_q6v5_unprepare);
Bjorn Andersson3b415c82018-06-04 13:30:36 -070083
84static irqreturn_t q6v5_wdog_interrupt(int irq, void *data)
85{
86 struct qcom_q6v5 *q6v5 = data;
87 size_t len;
88 char *msg;
89
90 /* Sometimes the stop triggers a watchdog rather than a stop-ack */
91 if (!q6v5->running) {
92 complete(&q6v5->stop_done);
93 return IRQ_HANDLED;
94 }
95
96 msg = qcom_smem_get(QCOM_SMEM_HOST_ANY, q6v5->crash_reason, &len);
97 if (!IS_ERR(msg) && len > 0 && msg[0])
98 dev_err(q6v5->dev, "watchdog received: %s\n", msg);
99 else
100 dev_err(q6v5->dev, "watchdog without message\n");
101
102 rproc_report_crash(q6v5->rproc, RPROC_WATCHDOG);
103
104 return IRQ_HANDLED;
105}
106
107static irqreturn_t q6v5_fatal_interrupt(int irq, void *data)
108{
109 struct qcom_q6v5 *q6v5 = data;
110 size_t len;
111 char *msg;
112
113 msg = qcom_smem_get(QCOM_SMEM_HOST_ANY, q6v5->crash_reason, &len);
114 if (!IS_ERR(msg) && len > 0 && msg[0])
115 dev_err(q6v5->dev, "fatal error received: %s\n", msg);
116 else
117 dev_err(q6v5->dev, "fatal error without message\n");
118
Sibi Sankard3ae96c2018-10-01 19:55:00 +0530119 q6v5->running = false;
Bjorn Andersson3b415c82018-06-04 13:30:36 -0700120 rproc_report_crash(q6v5->rproc, RPROC_FATAL_ERROR);
121
122 return IRQ_HANDLED;
123}
124
125static irqreturn_t q6v5_ready_interrupt(int irq, void *data)
126{
127 struct qcom_q6v5 *q6v5 = data;
128
129 complete(&q6v5->start_done);
130
131 return IRQ_HANDLED;
132}
133
134/**
135 * qcom_q6v5_wait_for_start() - wait for remote processor start signal
136 * @q6v5: reference to qcom_q6v5 context
137 * @timeout: timeout to wait for the event, in jiffies
138 *
139 * qcom_q6v5_unprepare() should not be called when this function fails.
140 *
141 * Return: 0 on success, -ETIMEDOUT on timeout
142 */
143int qcom_q6v5_wait_for_start(struct qcom_q6v5 *q6v5, int timeout)
144{
145 int ret;
146
147 ret = wait_for_completion_timeout(&q6v5->start_done, timeout);
148 if (!ret)
149 disable_irq(q6v5->handover_irq);
150
151 return !ret ? -ETIMEDOUT : 0;
152}
Arnd Bergmannded79d02018-07-06 14:57:53 +0200153EXPORT_SYMBOL_GPL(qcom_q6v5_wait_for_start);
Bjorn Andersson3b415c82018-06-04 13:30:36 -0700154
155static irqreturn_t q6v5_handover_interrupt(int irq, void *data)
156{
157 struct qcom_q6v5 *q6v5 = data;
158
159 if (q6v5->handover)
160 q6v5->handover(q6v5);
161
162 q6v5->handover_issued = true;
163
164 return IRQ_HANDLED;
165}
166
167static irqreturn_t q6v5_stop_interrupt(int irq, void *data)
168{
169 struct qcom_q6v5 *q6v5 = data;
170
171 complete(&q6v5->stop_done);
172
173 return IRQ_HANDLED;
174}
175
176/**
177 * qcom_q6v5_request_stop() - request the remote processor to stop
178 * @q6v5: reference to qcom_q6v5 context
Bjorn Anderssoned5da802020-11-21 21:41:34 -0800179 * @sysmon: reference to the remote's sysmon instance, or NULL
Bjorn Andersson3b415c82018-06-04 13:30:36 -0700180 *
181 * Return: 0 on success, negative errno on failure
182 */
Bjorn Anderssoned5da802020-11-21 21:41:34 -0800183int qcom_q6v5_request_stop(struct qcom_q6v5 *q6v5, struct qcom_sysmon *sysmon)
Bjorn Andersson3b415c82018-06-04 13:30:36 -0700184{
185 int ret;
186
Sibi Sankar5b7be882020-06-02 22:02:56 +0530187 q6v5->running = false;
188
Bjorn Anderssoned5da802020-11-21 21:41:34 -0800189 /* Don't perform SMP2P dance if sysmon already shut down the remote */
190 if (qcom_sysmon_shutdown_acked(sysmon))
191 return 0;
192
Bjorn Andersson3b415c82018-06-04 13:30:36 -0700193 qcom_smem_state_update_bits(q6v5->state,
194 BIT(q6v5->stop_bit), BIT(q6v5->stop_bit));
195
196 ret = wait_for_completion_timeout(&q6v5->stop_done, 5 * HZ);
197
198 qcom_smem_state_update_bits(q6v5->state, BIT(q6v5->stop_bit), 0);
199
200 return ret == 0 ? -ETIMEDOUT : 0;
201}
Arnd Bergmannded79d02018-07-06 14:57:53 +0200202EXPORT_SYMBOL_GPL(qcom_q6v5_request_stop);
Bjorn Andersson3b415c82018-06-04 13:30:36 -0700203
204/**
Bjorn Anderssone9142f52020-03-23 22:29:03 -0700205 * qcom_q6v5_panic() - panic handler to invoke a stop on the remote
206 * @q6v5: reference to qcom_q6v5 context
207 *
208 * Set the stop bit and sleep in order to allow the remote processor to flush
209 * its caches etc for post mortem debugging.
210 *
211 * Return: 200ms
212 */
213unsigned long qcom_q6v5_panic(struct qcom_q6v5 *q6v5)
214{
215 qcom_smem_state_update_bits(q6v5->state,
216 BIT(q6v5->stop_bit), BIT(q6v5->stop_bit));
217
218 return Q6V5_PANIC_DELAY_MS;
219}
220EXPORT_SYMBOL_GPL(qcom_q6v5_panic);
221
222/**
Bjorn Andersson3b415c82018-06-04 13:30:36 -0700223 * qcom_q6v5_init() - initializer of the q6v5 common struct
224 * @q6v5: handle to be initialized
225 * @pdev: platform_device reference for acquiring resources
226 * @rproc: associated remoteproc instance
227 * @crash_reason: SMEM id for crash reason string, or 0 if none
Sibi Sankarc1fe10d2021-09-16 19:29:21 +0530228 * @load_state: load state resource string
Bjorn Andersson3b415c82018-06-04 13:30:36 -0700229 * @handover: function to be called when proxy resources should be released
230 *
231 * Return: 0 on success, negative errno on failure
232 */
233int qcom_q6v5_init(struct qcom_q6v5 *q6v5, struct platform_device *pdev,
Sibi Sankarc1fe10d2021-09-16 19:29:21 +0530234 struct rproc *rproc, int crash_reason, const char *load_state,
Bjorn Andersson3b415c82018-06-04 13:30:36 -0700235 void (*handover)(struct qcom_q6v5 *q6v5))
236{
237 int ret;
238
239 q6v5->rproc = rproc;
240 q6v5->dev = &pdev->dev;
241 q6v5->crash_reason = crash_reason;
242 q6v5->handover = handover;
243
244 init_completion(&q6v5->start_done);
245 init_completion(&q6v5->stop_done);
246
247 q6v5->wdog_irq = platform_get_irq_byname(pdev, "wdog");
Stephen Boydd4466092019-07-30 11:15:38 -0700248 if (q6v5->wdog_irq < 0)
Brian Norris1e2517d2018-10-09 15:25:27 -0700249 return q6v5->wdog_irq;
Brian Norris1e2517d2018-10-09 15:25:27 -0700250
Bjorn Andersson3b415c82018-06-04 13:30:36 -0700251 ret = devm_request_threaded_irq(&pdev->dev, q6v5->wdog_irq,
252 NULL, q6v5_wdog_interrupt,
253 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
254 "q6v5 wdog", q6v5);
255 if (ret) {
256 dev_err(&pdev->dev, "failed to acquire wdog IRQ\n");
257 return ret;
258 }
259
260 q6v5->fatal_irq = platform_get_irq_byname(pdev, "fatal");
Stephen Boydd4466092019-07-30 11:15:38 -0700261 if (q6v5->fatal_irq < 0)
Brian Norris1e2517d2018-10-09 15:25:27 -0700262 return q6v5->fatal_irq;
Bjorn Anderssond5269c42018-09-19 18:51:51 -0700263
Bjorn Andersson3b415c82018-06-04 13:30:36 -0700264 ret = devm_request_threaded_irq(&pdev->dev, q6v5->fatal_irq,
265 NULL, q6v5_fatal_interrupt,
266 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
267 "q6v5 fatal", q6v5);
268 if (ret) {
269 dev_err(&pdev->dev, "failed to acquire fatal IRQ\n");
270 return ret;
271 }
272
273 q6v5->ready_irq = platform_get_irq_byname(pdev, "ready");
Stephen Boydd4466092019-07-30 11:15:38 -0700274 if (q6v5->ready_irq < 0)
Brian Norris1e2517d2018-10-09 15:25:27 -0700275 return q6v5->ready_irq;
Bjorn Anderssond5269c42018-09-19 18:51:51 -0700276
Bjorn Andersson3b415c82018-06-04 13:30:36 -0700277 ret = devm_request_threaded_irq(&pdev->dev, q6v5->ready_irq,
278 NULL, q6v5_ready_interrupt,
279 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
280 "q6v5 ready", q6v5);
281 if (ret) {
282 dev_err(&pdev->dev, "failed to acquire ready IRQ\n");
283 return ret;
284 }
285
286 q6v5->handover_irq = platform_get_irq_byname(pdev, "handover");
Stephen Boydd4466092019-07-30 11:15:38 -0700287 if (q6v5->handover_irq < 0)
Brian Norris1e2517d2018-10-09 15:25:27 -0700288 return q6v5->handover_irq;
Bjorn Anderssond5269c42018-09-19 18:51:51 -0700289
Bjorn Andersson3b415c82018-06-04 13:30:36 -0700290 ret = devm_request_threaded_irq(&pdev->dev, q6v5->handover_irq,
291 NULL, q6v5_handover_interrupt,
292 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
293 "q6v5 handover", q6v5);
294 if (ret) {
295 dev_err(&pdev->dev, "failed to acquire handover IRQ\n");
296 return ret;
297 }
298 disable_irq(q6v5->handover_irq);
299
300 q6v5->stop_irq = platform_get_irq_byname(pdev, "stop-ack");
Stephen Boydd4466092019-07-30 11:15:38 -0700301 if (q6v5->stop_irq < 0)
Brian Norris1e2517d2018-10-09 15:25:27 -0700302 return q6v5->stop_irq;
Bjorn Anderssond5269c42018-09-19 18:51:51 -0700303
Bjorn Andersson3b415c82018-06-04 13:30:36 -0700304 ret = devm_request_threaded_irq(&pdev->dev, q6v5->stop_irq,
305 NULL, q6v5_stop_interrupt,
306 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
307 "q6v5 stop", q6v5);
308 if (ret) {
309 dev_err(&pdev->dev, "failed to acquire stop-ack IRQ\n");
310 return ret;
311 }
312
Stephan Gerhold632f1ca2021-06-18 13:15:55 +0200313 q6v5->state = devm_qcom_smem_state_get(&pdev->dev, "stop", &q6v5->stop_bit);
Bjorn Andersson3b415c82018-06-04 13:30:36 -0700314 if (IS_ERR(q6v5->state)) {
315 dev_err(&pdev->dev, "failed to acquire stop state\n");
316 return PTR_ERR(q6v5->state);
317 }
318
Sibi Sankarc1fe10d2021-09-16 19:29:21 +0530319 q6v5->load_state = devm_kstrdup_const(&pdev->dev, load_state, GFP_KERNEL);
320 q6v5->qmp = qmp_get(&pdev->dev);
321 if (IS_ERR(q6v5->qmp)) {
322 if (PTR_ERR(q6v5->qmp) != -ENODEV)
323 return dev_err_probe(&pdev->dev, PTR_ERR(q6v5->qmp),
324 "failed to acquire load state\n");
325 q6v5->qmp = NULL;
326 } else if (!q6v5->load_state) {
327 if (!load_state)
328 dev_err(&pdev->dev, "load state resource string empty\n");
329
330 qmp_put(q6v5->qmp);
331 return load_state ? -ENOMEM : -EINVAL;
332 }
333
Bjorn Andersson3b415c82018-06-04 13:30:36 -0700334 return 0;
335}
Arnd Bergmannded79d02018-07-06 14:57:53 +0200336EXPORT_SYMBOL_GPL(qcom_q6v5_init);
337
Sibi Sankarc1fe10d2021-09-16 19:29:21 +0530338/**
339 * qcom_q6v5_deinit() - deinitialize the q6v5 common struct
340 * @q6v5: reference to qcom_q6v5 context to be deinitialized
341 */
342void qcom_q6v5_deinit(struct qcom_q6v5 *q6v5)
343{
344 qmp_put(q6v5->qmp);
345}
346EXPORT_SYMBOL_GPL(qcom_q6v5_deinit);
347
Arnd Bergmannded79d02018-07-06 14:57:53 +0200348MODULE_LICENSE("GPL v2");
349MODULE_DESCRIPTION("Qualcomm Peripheral Image Loader for Q6V5");