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_common.h" 17 #include "qcom_q6v5.h" 18 19 #define Q6V5_LOAD_STATE_MSG_LEN 64 20 #define Q6V5_PANIC_DELAY_MS 200 21 22 static 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 43 /** 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 */ 49 int qcom_q6v5_prepare(struct qcom_q6v5 *q6v5) 50 { 51 int ret; 52 53 ret = q6v5_load_state_toggle(q6v5, true); 54 if (ret) 55 return ret; 56 57 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 } 67 EXPORT_SYMBOL_GPL(qcom_q6v5_prepare); 68 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 */ 75 int qcom_q6v5_unprepare(struct qcom_q6v5 *q6v5) 76 { 77 disable_irq(q6v5->handover_irq); 78 q6v5_load_state_toggle(q6v5, false); 79 80 return !q6v5->handover_issued; 81 } 82 EXPORT_SYMBOL_GPL(qcom_q6v5_unprepare); 83 84 static 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 107 static 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 119 q6v5->running = false; 120 rproc_report_crash(q6v5->rproc, RPROC_FATAL_ERROR); 121 122 return IRQ_HANDLED; 123 } 124 125 static 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 */ 143 int 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 } 153 EXPORT_SYMBOL_GPL(qcom_q6v5_wait_for_start); 154 155 static 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 167 static 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 179 * @sysmon: reference to the remote's sysmon instance, or NULL 180 * 181 * Return: 0 on success, negative errno on failure 182 */ 183 int qcom_q6v5_request_stop(struct qcom_q6v5 *q6v5, struct qcom_sysmon *sysmon) 184 { 185 int ret; 186 187 q6v5->running = false; 188 189 /* Don't perform SMP2P dance if sysmon already shut down the remote */ 190 if (qcom_sysmon_shutdown_acked(sysmon)) 191 return 0; 192 193 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 } 202 EXPORT_SYMBOL_GPL(qcom_q6v5_request_stop); 203 204 /** 205 * 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 */ 213 unsigned 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 } 220 EXPORT_SYMBOL_GPL(qcom_q6v5_panic); 221 222 /** 223 * 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 228 * @load_state: load state resource string 229 * @handover: function to be called when proxy resources should be released 230 * 231 * Return: 0 on success, negative errno on failure 232 */ 233 int qcom_q6v5_init(struct qcom_q6v5 *q6v5, struct platform_device *pdev, 234 struct rproc *rproc, int crash_reason, const char *load_state, 235 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"); 248 if (q6v5->wdog_irq < 0) 249 return q6v5->wdog_irq; 250 251 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"); 261 if (q6v5->fatal_irq < 0) 262 return q6v5->fatal_irq; 263 264 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"); 274 if (q6v5->ready_irq < 0) 275 return q6v5->ready_irq; 276 277 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"); 287 if (q6v5->handover_irq < 0) 288 return q6v5->handover_irq; 289 290 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"); 301 if (q6v5->stop_irq < 0) 302 return q6v5->stop_irq; 303 304 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 313 q6v5->state = devm_qcom_smem_state_get(&pdev->dev, "stop", &q6v5->stop_bit); 314 if (IS_ERR(q6v5->state)) { 315 dev_err(&pdev->dev, "failed to acquire stop state\n"); 316 return PTR_ERR(q6v5->state); 317 } 318 319 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 334 return 0; 335 } 336 EXPORT_SYMBOL_GPL(qcom_q6v5_init); 337 338 /** 339 * qcom_q6v5_deinit() - deinitialize the q6v5 common struct 340 * @q6v5: reference to qcom_q6v5 context to be deinitialized 341 */ 342 void qcom_q6v5_deinit(struct qcom_q6v5 *q6v5) 343 { 344 qmp_put(q6v5->qmp); 345 } 346 EXPORT_SYMBOL_GPL(qcom_q6v5_deinit); 347 348 MODULE_LICENSE("GPL v2"); 349 MODULE_DESCRIPTION("Qualcomm Peripheral Image Loader for Q6V5"); 350