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/qcom_aoss.h> 14 #include <linux/soc/qcom/smem.h> 15 #include <linux/soc/qcom/smem_state.h> 16 #include <linux/remoteproc.h> 17 #include "qcom_common.h" 18 #include "qcom_q6v5.h" 19 20 #define Q6V5_LOAD_STATE_MSG_LEN 64 21 #define Q6V5_PANIC_DELAY_MS 200 22 23 static int q6v5_load_state_toggle(struct qcom_q6v5 *q6v5, bool enable) 24 { 25 char buf[Q6V5_LOAD_STATE_MSG_LEN]; 26 int ret; 27 28 if (!q6v5->qmp) 29 return 0; 30 31 ret = snprintf(buf, sizeof(buf), 32 "{class: image, res: load_state, name: %s, val: %s}", 33 q6v5->load_state, enable ? "on" : "off"); 34 35 WARN_ON(ret >= Q6V5_LOAD_STATE_MSG_LEN); 36 37 ret = qmp_send(q6v5->qmp, buf, sizeof(buf)); 38 if (ret) 39 dev_err(q6v5->dev, "failed to toggle load state\n"); 40 41 return ret; 42 } 43 44 /** 45 * qcom_q6v5_prepare() - reinitialize the qcom_q6v5 context before start 46 * @q6v5: reference to qcom_q6v5 context to be reinitialized 47 * 48 * Return: 0 on success, negative errno on failure 49 */ 50 int qcom_q6v5_prepare(struct qcom_q6v5 *q6v5) 51 { 52 int ret; 53 54 ret = q6v5_load_state_toggle(q6v5, true); 55 if (ret) 56 return ret; 57 58 reinit_completion(&q6v5->start_done); 59 reinit_completion(&q6v5->stop_done); 60 61 q6v5->running = true; 62 q6v5->handover_issued = false; 63 64 enable_irq(q6v5->handover_irq); 65 66 return 0; 67 } 68 EXPORT_SYMBOL_GPL(qcom_q6v5_prepare); 69 70 /** 71 * qcom_q6v5_unprepare() - unprepare the qcom_q6v5 context after stop 72 * @q6v5: reference to qcom_q6v5 context to be unprepared 73 * 74 * Return: 0 on success, 1 if handover hasn't yet been called 75 */ 76 int qcom_q6v5_unprepare(struct qcom_q6v5 *q6v5) 77 { 78 disable_irq(q6v5->handover_irq); 79 q6v5_load_state_toggle(q6v5, false); 80 81 return !q6v5->handover_issued; 82 } 83 EXPORT_SYMBOL_GPL(qcom_q6v5_unprepare); 84 85 static irqreturn_t q6v5_wdog_interrupt(int irq, void *data) 86 { 87 struct qcom_q6v5 *q6v5 = data; 88 size_t len; 89 char *msg; 90 91 /* Sometimes the stop triggers a watchdog rather than a stop-ack */ 92 if (!q6v5->running) { 93 complete(&q6v5->stop_done); 94 return IRQ_HANDLED; 95 } 96 97 msg = qcom_smem_get(QCOM_SMEM_HOST_ANY, q6v5->crash_reason, &len); 98 if (!IS_ERR(msg) && len > 0 && msg[0]) 99 dev_err(q6v5->dev, "watchdog received: %s\n", msg); 100 else 101 dev_err(q6v5->dev, "watchdog without message\n"); 102 103 rproc_report_crash(q6v5->rproc, RPROC_WATCHDOG); 104 105 return IRQ_HANDLED; 106 } 107 108 static irqreturn_t q6v5_fatal_interrupt(int irq, void *data) 109 { 110 struct qcom_q6v5 *q6v5 = data; 111 size_t len; 112 char *msg; 113 114 msg = qcom_smem_get(QCOM_SMEM_HOST_ANY, q6v5->crash_reason, &len); 115 if (!IS_ERR(msg) && len > 0 && msg[0]) 116 dev_err(q6v5->dev, "fatal error received: %s\n", msg); 117 else 118 dev_err(q6v5->dev, "fatal error without message\n"); 119 120 q6v5->running = false; 121 rproc_report_crash(q6v5->rproc, RPROC_FATAL_ERROR); 122 123 return IRQ_HANDLED; 124 } 125 126 static irqreturn_t q6v5_ready_interrupt(int irq, void *data) 127 { 128 struct qcom_q6v5 *q6v5 = data; 129 130 complete(&q6v5->start_done); 131 132 return IRQ_HANDLED; 133 } 134 135 /** 136 * qcom_q6v5_wait_for_start() - wait for remote processor start signal 137 * @q6v5: reference to qcom_q6v5 context 138 * @timeout: timeout to wait for the event, in jiffies 139 * 140 * qcom_q6v5_unprepare() should not be called when this function fails. 141 * 142 * Return: 0 on success, -ETIMEDOUT on timeout 143 */ 144 int qcom_q6v5_wait_for_start(struct qcom_q6v5 *q6v5, int timeout) 145 { 146 int ret; 147 148 ret = wait_for_completion_timeout(&q6v5->start_done, timeout); 149 if (!ret) 150 disable_irq(q6v5->handover_irq); 151 152 return !ret ? -ETIMEDOUT : 0; 153 } 154 EXPORT_SYMBOL_GPL(qcom_q6v5_wait_for_start); 155 156 static irqreturn_t q6v5_handover_interrupt(int irq, void *data) 157 { 158 struct qcom_q6v5 *q6v5 = data; 159 160 if (q6v5->handover) 161 q6v5->handover(q6v5); 162 163 q6v5->handover_issued = true; 164 165 return IRQ_HANDLED; 166 } 167 168 static irqreturn_t q6v5_stop_interrupt(int irq, void *data) 169 { 170 struct qcom_q6v5 *q6v5 = data; 171 172 complete(&q6v5->stop_done); 173 174 return IRQ_HANDLED; 175 } 176 177 /** 178 * qcom_q6v5_request_stop() - request the remote processor to stop 179 * @q6v5: reference to qcom_q6v5 context 180 * @sysmon: reference to the remote's sysmon instance, or NULL 181 * 182 * Return: 0 on success, negative errno on failure 183 */ 184 int qcom_q6v5_request_stop(struct qcom_q6v5 *q6v5, struct qcom_sysmon *sysmon) 185 { 186 int ret; 187 188 q6v5->running = false; 189 190 /* Don't perform SMP2P dance if sysmon already shut down the remote */ 191 if (qcom_sysmon_shutdown_acked(sysmon)) 192 return 0; 193 194 qcom_smem_state_update_bits(q6v5->state, 195 BIT(q6v5->stop_bit), BIT(q6v5->stop_bit)); 196 197 ret = wait_for_completion_timeout(&q6v5->stop_done, 5 * HZ); 198 199 qcom_smem_state_update_bits(q6v5->state, BIT(q6v5->stop_bit), 0); 200 201 return ret == 0 ? -ETIMEDOUT : 0; 202 } 203 EXPORT_SYMBOL_GPL(qcom_q6v5_request_stop); 204 205 /** 206 * qcom_q6v5_panic() - panic handler to invoke a stop on the remote 207 * @q6v5: reference to qcom_q6v5 context 208 * 209 * Set the stop bit and sleep in order to allow the remote processor to flush 210 * its caches etc for post mortem debugging. 211 * 212 * Return: 200ms 213 */ 214 unsigned long qcom_q6v5_panic(struct qcom_q6v5 *q6v5) 215 { 216 qcom_smem_state_update_bits(q6v5->state, 217 BIT(q6v5->stop_bit), BIT(q6v5->stop_bit)); 218 219 return Q6V5_PANIC_DELAY_MS; 220 } 221 EXPORT_SYMBOL_GPL(qcom_q6v5_panic); 222 223 /** 224 * qcom_q6v5_init() - initializer of the q6v5 common struct 225 * @q6v5: handle to be initialized 226 * @pdev: platform_device reference for acquiring resources 227 * @rproc: associated remoteproc instance 228 * @crash_reason: SMEM id for crash reason string, or 0 if none 229 * @load_state: load state resource string 230 * @handover: function to be called when proxy resources should be released 231 * 232 * Return: 0 on success, negative errno on failure 233 */ 234 int qcom_q6v5_init(struct qcom_q6v5 *q6v5, struct platform_device *pdev, 235 struct rproc *rproc, int crash_reason, const char *load_state, 236 void (*handover)(struct qcom_q6v5 *q6v5)) 237 { 238 int ret; 239 240 q6v5->rproc = rproc; 241 q6v5->dev = &pdev->dev; 242 q6v5->crash_reason = crash_reason; 243 q6v5->handover = handover; 244 245 init_completion(&q6v5->start_done); 246 init_completion(&q6v5->stop_done); 247 248 q6v5->wdog_irq = platform_get_irq_byname(pdev, "wdog"); 249 if (q6v5->wdog_irq < 0) 250 return q6v5->wdog_irq; 251 252 ret = devm_request_threaded_irq(&pdev->dev, q6v5->wdog_irq, 253 NULL, q6v5_wdog_interrupt, 254 IRQF_TRIGGER_RISING | IRQF_ONESHOT, 255 "q6v5 wdog", q6v5); 256 if (ret) { 257 dev_err(&pdev->dev, "failed to acquire wdog IRQ\n"); 258 return ret; 259 } 260 261 q6v5->fatal_irq = platform_get_irq_byname(pdev, "fatal"); 262 if (q6v5->fatal_irq < 0) 263 return q6v5->fatal_irq; 264 265 ret = devm_request_threaded_irq(&pdev->dev, q6v5->fatal_irq, 266 NULL, q6v5_fatal_interrupt, 267 IRQF_TRIGGER_RISING | IRQF_ONESHOT, 268 "q6v5 fatal", q6v5); 269 if (ret) { 270 dev_err(&pdev->dev, "failed to acquire fatal IRQ\n"); 271 return ret; 272 } 273 274 q6v5->ready_irq = platform_get_irq_byname(pdev, "ready"); 275 if (q6v5->ready_irq < 0) 276 return q6v5->ready_irq; 277 278 ret = devm_request_threaded_irq(&pdev->dev, q6v5->ready_irq, 279 NULL, q6v5_ready_interrupt, 280 IRQF_TRIGGER_RISING | IRQF_ONESHOT, 281 "q6v5 ready", q6v5); 282 if (ret) { 283 dev_err(&pdev->dev, "failed to acquire ready IRQ\n"); 284 return ret; 285 } 286 287 q6v5->handover_irq = platform_get_irq_byname(pdev, "handover"); 288 if (q6v5->handover_irq < 0) 289 return q6v5->handover_irq; 290 291 ret = devm_request_threaded_irq(&pdev->dev, q6v5->handover_irq, 292 NULL, q6v5_handover_interrupt, 293 IRQF_TRIGGER_RISING | IRQF_ONESHOT, 294 "q6v5 handover", q6v5); 295 if (ret) { 296 dev_err(&pdev->dev, "failed to acquire handover IRQ\n"); 297 return ret; 298 } 299 disable_irq(q6v5->handover_irq); 300 301 q6v5->stop_irq = platform_get_irq_byname(pdev, "stop-ack"); 302 if (q6v5->stop_irq < 0) 303 return q6v5->stop_irq; 304 305 ret = devm_request_threaded_irq(&pdev->dev, q6v5->stop_irq, 306 NULL, q6v5_stop_interrupt, 307 IRQF_TRIGGER_RISING | IRQF_ONESHOT, 308 "q6v5 stop", q6v5); 309 if (ret) { 310 dev_err(&pdev->dev, "failed to acquire stop-ack IRQ\n"); 311 return ret; 312 } 313 314 q6v5->state = devm_qcom_smem_state_get(&pdev->dev, "stop", &q6v5->stop_bit); 315 if (IS_ERR(q6v5->state)) { 316 dev_err(&pdev->dev, "failed to acquire stop state\n"); 317 return PTR_ERR(q6v5->state); 318 } 319 320 q6v5->load_state = devm_kstrdup_const(&pdev->dev, load_state, GFP_KERNEL); 321 q6v5->qmp = qmp_get(&pdev->dev); 322 if (IS_ERR(q6v5->qmp)) { 323 if (PTR_ERR(q6v5->qmp) != -ENODEV) 324 return dev_err_probe(&pdev->dev, PTR_ERR(q6v5->qmp), 325 "failed to acquire load state\n"); 326 q6v5->qmp = NULL; 327 } else if (!q6v5->load_state) { 328 if (!load_state) 329 dev_err(&pdev->dev, "load state resource string empty\n"); 330 331 qmp_put(q6v5->qmp); 332 return load_state ? -ENOMEM : -EINVAL; 333 } 334 335 return 0; 336 } 337 EXPORT_SYMBOL_GPL(qcom_q6v5_init); 338 339 /** 340 * qcom_q6v5_deinit() - deinitialize the q6v5 common struct 341 * @q6v5: reference to qcom_q6v5 context to be deinitialized 342 */ 343 void qcom_q6v5_deinit(struct qcom_q6v5 *q6v5) 344 { 345 qmp_put(q6v5->qmp); 346 } 347 EXPORT_SYMBOL_GPL(qcom_q6v5_deinit); 348 349 MODULE_LICENSE("GPL v2"); 350 MODULE_DESCRIPTION("Qualcomm Peripheral Image Loader for Q6V5"); 351