qcom_q6v5.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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, 2020-2021, The Linux Foundation. All rights reserved.
  8. * Copyright (c) 2023-2024 Qualcomm Innovation Center, Inc. All rights reserved.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/module.h>
  14. #include <linux/soc/qcom/smem.h>
  15. #include <linux/soc/qcom/smem_state.h>
  16. #include <linux/remoteproc.h>
  17. #include <linux/delay.h>
  18. #include "qcom_common.h"
  19. #include "qcom_q6v5.h"
  20. #include <trace/events/rproc_qcom.h>
  21. #if IS_ENABLED(CONFIG_SEC_SENSORS_SSC)
  22. #include <linux/adsp/ssc_ssr_reason.h>
  23. #endif
  24. #if IS_ENABLED(CONFIG_SND_SOC_SAMSUNG_AUDIO)
  25. #include <sound/samsung/sec_audio_sysfs.h>
  26. #include <sound/samsung/snd_debug_proc.h>
  27. #endif
  28. #define Q6V5_PANIC_DELAY_MS 200
  29. #define SEC_DETAILED_CRASH_REASON
  30. /**
  31. * qcom_q6v5_prepare() - reinitialize the qcom_q6v5 context before start
  32. * @q6v5: reference to qcom_q6v5 context to be reinitialized
  33. *
  34. * Return: 0 on success, negative errno on failure
  35. */
  36. int qcom_q6v5_prepare(struct qcom_q6v5 *q6v5)
  37. {
  38. reinit_completion(&q6v5->start_done);
  39. reinit_completion(&q6v5->stop_done);
  40. q6v5->running = true;
  41. q6v5->handover_issued = false;
  42. enable_irq(q6v5->handover_irq);
  43. return 0;
  44. }
  45. EXPORT_SYMBOL_GPL(qcom_q6v5_prepare);
  46. /**
  47. * qcom_q6v5_unprepare() - unprepare the qcom_q6v5 context after stop
  48. * @q6v5: reference to qcom_q6v5 context to be unprepared
  49. *
  50. * Return: 0 on success, 1 if handover hasn't yet been called
  51. */
  52. int qcom_q6v5_unprepare(struct qcom_q6v5 *q6v5)
  53. {
  54. disable_irq(q6v5->handover_irq);
  55. return !q6v5->handover_issued;
  56. }
  57. EXPORT_SYMBOL_GPL(qcom_q6v5_unprepare);
  58. void qcom_q6v5_register_ssr_subdev(struct qcom_q6v5 *q6v5, struct rproc_subdev *ssr_subdev)
  59. {
  60. q6v5->ssr_subdev = ssr_subdev;
  61. }
  62. EXPORT_SYMBOL(qcom_q6v5_register_ssr_subdev);
  63. static void qcom_q6v5_crash_handler_work(struct work_struct *work)
  64. {
  65. struct qcom_q6v5 *q6v5 = container_of(work, struct qcom_q6v5, crash_handler);
  66. struct rproc *rproc = q6v5->rproc;
  67. struct rproc_subdev *subdev;
  68. int votes;
  69. #ifdef SEC_DETAILED_CRASH_REASON
  70. char *msg;
  71. size_t len;
  72. #endif
  73. if (atomic_read(&q6v5->ssr_in_prog) != 0) {
  74. dev_err(q6v5->dev, "skip crash handling\n");
  75. return;
  76. }
  77. mutex_lock(&rproc->lock);
  78. rproc->state = RPROC_CRASHED;
  79. votes = atomic_xchg(&rproc->power, 0);
  80. /* if votes are zero, rproc has already been shutdown */
  81. if (votes == 0) {
  82. mutex_unlock(&rproc->lock);
  83. return;
  84. }
  85. list_for_each_entry_reverse(subdev, &rproc->subdevs, node) {
  86. if (subdev->stop)
  87. subdev->stop(subdev, true);
  88. }
  89. mutex_unlock(&rproc->lock);
  90. /*
  91. * Temporary workaround until ramdump userspace application calls
  92. * sync() and fclose() on attempting the dump.
  93. */
  94. msleep(100);
  95. #ifdef SEC_DETAILED_CRASH_REASON
  96. msg = qcom_smem_get(QCOM_SMEM_HOST_ANY, q6v5->crash_reason, &len);
  97. if (!IS_ERR(msg) && len > 0 && msg[0]) {
  98. /*
  99. * From this code in BL,
  100. * snprintf_rc(buf, 150, "Panic Msg : %s", summary->apss->excp.panic_msg
  101. * we can use only 150 - 12(Panic Msg : )
  102. */
  103. char fatal_msg[150-12];
  104. char *lined_msg = fatal_msg;
  105. char *remoteproc_name;
  106. /* for long device name, condense rproc name */
  107. remoteproc_name = strstr(q6v5->rproc->name, "remoteproc-");
  108. if (remoteproc_name != NULL)
  109. remoteproc_name += 11;
  110. else
  111. remoteproc_name = (char *)q6v5->rproc->name;
  112. len = snprintf(fatal_msg, sizeof(fatal_msg)-1,
  113. "FATAL %s, %s", remoteproc_name, msg);
  114. if (len >= sizeof(fatal_msg))
  115. len = sizeof(fatal_msg) - 2;
  116. /* '\n' -> ' ', except the last one */
  117. while (len--) {
  118. if (*lined_msg == '\n') *lined_msg = ' ';
  119. lined_msg++;
  120. }
  121. *lined_msg++ = '\n';
  122. *lined_msg = '\0';
  123. panic(fatal_msg);
  124. } else
  125. #endif
  126. panic("Panicking, remoteproc %s crashed\n", q6v5->rproc->name);
  127. }
  128. static irqreturn_t q6v5_wdog_interrupt(int irq, void *data)
  129. {
  130. struct qcom_q6v5 *q6v5 = data;
  131. size_t len;
  132. char *msg;
  133. #if IS_ENABLED(CONFIG_SEC_SENSORS_SSC) || IS_ENABLED(CONFIG_SND_SOC_SAMSUNG_AUDIO)
  134. char *chk_name = NULL;
  135. #endif
  136. /* Sometimes the stop triggers a watchdog rather than a stop-ack */
  137. if (!q6v5->running) {
  138. dev_info(q6v5->dev, "received wdog irq while q6 is offline\n");
  139. complete(&q6v5->stop_done);
  140. return IRQ_HANDLED;
  141. }
  142. msg = qcom_smem_get(QCOM_SMEM_HOST_ANY, q6v5->crash_reason, &len);
  143. if (!IS_ERR(msg) && len > 0 && msg[0]) {
  144. dev_err(q6v5->dev, "watchdog received: %s\n", msg);
  145. trace_rproc_qcom_event(dev_name(q6v5->dev), "q6v5_wdog", msg);
  146. #if IS_ENABLED(CONFIG_SEC_SENSORS_SSC)
  147. chk_name = strstr(q6v5->rproc->name, "adsp");
  148. if (chk_name != NULL)
  149. ssr_reason_call_back(msg, len);
  150. #endif
  151. #if IS_ENABLED(CONFIG_SND_SOC_SAMSUNG_AUDIO)
  152. chk_name = strstr(q6v5->rproc->name, "adsp");
  153. if (chk_name != NULL) {
  154. sdp_info_print("watchdog received: %s\n", msg);
  155. send_adsp_silent_reset_ev();
  156. }
  157. #endif
  158. } else
  159. dev_err(q6v5->dev, "watchdog without message\n");
  160. q6v5->running = false;
  161. dev_err(q6v5->dev, "rproc recovery state: %s\n",
  162. q6v5->rproc->recovery_disabled ?
  163. "disabled and lead to device crash" :
  164. "enabled and kick reovery process");
  165. if (q6v5->ssr_subdev)
  166. qcom_notify_early_ssr_clients(q6v5->ssr_subdev);
  167. if (q6v5->rproc->recovery_disabled)
  168. schedule_work(&q6v5->crash_handler);
  169. else
  170. rproc_report_crash(q6v5->rproc, RPROC_WATCHDOG);
  171. return IRQ_HANDLED;
  172. }
  173. static irqreturn_t q6v5_fatal_interrupt(int irq, void *data)
  174. {
  175. struct qcom_q6v5 *q6v5 = data;
  176. size_t len;
  177. char *msg;
  178. #if IS_ENABLED(CONFIG_SEC_SENSORS_SSC) || IS_ENABLED(CONFIG_SND_SOC_SAMSUNG_AUDIO)
  179. char *chk_name = NULL;
  180. #endif
  181. if (!q6v5->running) {
  182. dev_info(q6v5->dev, "received fatal irq while q6 is offline\n");
  183. return IRQ_HANDLED;
  184. }
  185. msg = qcom_smem_get(QCOM_SMEM_HOST_ANY, q6v5->crash_reason, &len);
  186. if (!IS_ERR(msg) && len > 0 && msg[0]) {
  187. dev_err(q6v5->dev, "fatal error received: %s\n", msg);
  188. trace_rproc_qcom_event(dev_name(q6v5->dev), "q6v5_fatal", msg);
  189. #if IS_ENABLED(CONFIG_SEC_SENSORS_SSC)
  190. chk_name = strstr(q6v5->rproc->name, "adsp");
  191. if (chk_name != NULL) {
  192. ssr_reason_call_back(msg, len);
  193. if (strstr(msg, "IPLSREVOCER")
  194. #if IS_ENABLED(CONFIG_SEC_SENSORS_RECOVERY)
  195. || strstr(msg, "qsh_process")
  196. #endif
  197. || strstr(msg, "PMUDRSS")) {
  198. q6v5->rproc->fssr = true;
  199. q6v5->rproc->prev_recovery_disabled =
  200. q6v5->rproc->recovery_disabled;
  201. q6v5->rproc->recovery_disabled = false;
  202. if (strstr(msg, "PMUDRSS"))
  203. q6v5->rproc->fssr_dump = true;
  204. } else {
  205. q6v5->rproc->fssr = false;
  206. q6v5->rproc->fssr_dump = false;
  207. }
  208. dev_info(q6v5->dev, "recovery:%d,%d\n",
  209. (int)q6v5->rproc->prev_recovery_disabled,
  210. (int)q6v5->rproc->recovery_disabled);
  211. }
  212. #endif
  213. #if IS_ENABLED(CONFIG_SND_SOC_SAMSUNG_AUDIO)
  214. chk_name = strstr(q6v5->rproc->name, "adsp");
  215. if (chk_name != NULL) {
  216. sdp_info_print("fatal error received: %s\n", msg);
  217. send_adsp_silent_reset_ev();
  218. }
  219. #endif
  220. } else
  221. dev_err(q6v5->dev, "fatal error without message\n");
  222. q6v5->running = false;
  223. dev_err(q6v5->dev, "rproc recovery state: %s\n",
  224. q6v5->rproc->recovery_disabled ? "disabled and lead to device crash" :
  225. "enabled and kick reovery process");
  226. if (q6v5->ssr_subdev) {
  227. int silent_ssr_in_progress;
  228. spin_lock(&q6v5->silent_ssr_lock);
  229. silent_ssr_in_progress = atomic_read(&q6v5->ssr_in_prog);
  230. spin_unlock(&q6v5->silent_ssr_lock);
  231. if (silent_ssr_in_progress) {
  232. dev_err(q6v5->dev, "silent ssr is ongoing. Return\n");
  233. return IRQ_HANDLED;
  234. }
  235. qcom_notify_early_ssr_clients(q6v5->ssr_subdev);
  236. }
  237. if (q6v5->rproc->recovery_disabled)
  238. schedule_work(&q6v5->crash_handler);
  239. else
  240. rproc_report_crash(q6v5->rproc, RPROC_FATAL_ERROR);
  241. return IRQ_HANDLED;
  242. }
  243. static irqreturn_t q6v5_ready_interrupt(int irq, void *data)
  244. {
  245. struct qcom_q6v5 *q6v5 = data;
  246. complete(&q6v5->start_done);
  247. return IRQ_HANDLED;
  248. }
  249. /**
  250. * qcom_q6v5_wait_for_start() - wait for remote processor start signal
  251. * @q6v5: reference to qcom_q6v5 context
  252. * @timeout: timeout to wait for the event, in jiffies
  253. *
  254. * qcom_q6v5_unprepare() should not be called when this function fails.
  255. *
  256. * Return: 0 on success, -ETIMEDOUT on timeout
  257. */
  258. int qcom_q6v5_wait_for_start(struct qcom_q6v5 *q6v5, int timeout)
  259. {
  260. int ret;
  261. ret = wait_for_completion_timeout(&q6v5->start_done, timeout);
  262. if (!ret)
  263. disable_irq(q6v5->handover_irq);
  264. return !ret ? -ETIMEDOUT : 0;
  265. }
  266. EXPORT_SYMBOL_GPL(qcom_q6v5_wait_for_start);
  267. static irqreturn_t q6v5_handover_interrupt(int irq, void *data)
  268. {
  269. struct qcom_q6v5 *q6v5 = data;
  270. if (q6v5->handover)
  271. q6v5->handover(q6v5);
  272. q6v5->handover_issued = true;
  273. return IRQ_HANDLED;
  274. }
  275. static irqreturn_t q6v5_stop_interrupt(int irq, void *data)
  276. {
  277. struct qcom_q6v5 *q6v5 = data;
  278. complete(&q6v5->stop_done);
  279. return IRQ_HANDLED;
  280. }
  281. /**
  282. * qcom_q6v5_request_stop() - request the remote processor to stop
  283. * @q6v5: reference to qcom_q6v5 context
  284. * @sysmon: reference to the remote's sysmon instance, or NULL
  285. *
  286. * Return: 0 on success, negative errno on failure
  287. */
  288. int qcom_q6v5_request_stop(struct qcom_q6v5 *q6v5, struct qcom_sysmon *sysmon)
  289. {
  290. int ret;
  291. q6v5->running = false;
  292. /* Don't perform SMP2P dance if sysmon already shut
  293. * down the remote or if it isn't running
  294. */
  295. if (q6v5->rproc->state != RPROC_RUNNING || qcom_sysmon_shutdown_acked(sysmon))
  296. return 0;
  297. qcom_smem_state_update_bits(q6v5->state,
  298. BIT(q6v5->stop_bit), BIT(q6v5->stop_bit));
  299. ret = wait_for_completion_timeout(&q6v5->stop_done, 5 * HZ);
  300. qcom_smem_state_update_bits(q6v5->state, BIT(q6v5->stop_bit), 0);
  301. return ret == 0 ? -ETIMEDOUT : 0;
  302. }
  303. EXPORT_SYMBOL_GPL(qcom_q6v5_request_stop);
  304. /**
  305. * qcom_q6v5_panic() - panic handler to invoke a stop on the remote
  306. * @q6v5: reference to qcom_q6v5 context
  307. *
  308. * Set the stop bit and sleep in order to allow the remote processor to flush
  309. * its caches etc for post mortem debugging.
  310. *
  311. * Return: 200ms
  312. */
  313. unsigned long qcom_q6v5_panic(struct qcom_q6v5 *q6v5)
  314. {
  315. qcom_smem_state_update_bits(q6v5->state,
  316. BIT(q6v5->stop_bit), BIT(q6v5->stop_bit));
  317. return Q6V5_PANIC_DELAY_MS;
  318. }
  319. EXPORT_SYMBOL_GPL(qcom_q6v5_panic);
  320. /**
  321. * qcom_q6v5_init() - initializer of the q6v5 common struct
  322. * @q6v5: handle to be initialized
  323. * @pdev: platform_device reference for acquiring resources
  324. * @rproc: associated remoteproc instance
  325. * @crash_reason: SMEM id for crash reason string, or 0 if none
  326. * @handover: function to be called when proxy resources should be released
  327. *
  328. * Return: 0 on success, negative errno on failure
  329. */
  330. int qcom_q6v5_init(struct qcom_q6v5 *q6v5, struct platform_device *pdev,
  331. struct rproc *rproc, int crash_reason,
  332. void (*handover)(struct qcom_q6v5 *q6v5))
  333. {
  334. int ret;
  335. struct resource *res;
  336. q6v5->rproc = rproc;
  337. q6v5->dev = &pdev->dev;
  338. q6v5->crash_reason = crash_reason;
  339. q6v5->handover = handover;
  340. q6v5->ssr_subdev = NULL;
  341. atomic_set(&q6v5->ssr_in_prog, 0);
  342. init_completion(&q6v5->start_done);
  343. init_completion(&q6v5->stop_done);
  344. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  345. if (res) {
  346. q6v5->rmb_base = devm_ioremap_resource(&pdev->dev, res);
  347. if (IS_ERR(q6v5->rmb_base))
  348. q6v5->rmb_base = NULL;
  349. } else
  350. q6v5->rmb_base = NULL;
  351. q6v5->wdog_irq = platform_get_irq_byname(pdev, "wdog");
  352. if (q6v5->wdog_irq < 0)
  353. return q6v5->wdog_irq;
  354. ret = devm_request_threaded_irq(&pdev->dev, q6v5->wdog_irq,
  355. NULL, q6v5_wdog_interrupt,
  356. IRQF_ONESHOT,
  357. "q6v5 wdog", q6v5);
  358. if (ret) {
  359. dev_err(&pdev->dev, "failed to acquire wdog IRQ\n");
  360. return ret;
  361. }
  362. q6v5->fatal_irq = platform_get_irq_byname(pdev, "fatal");
  363. if (q6v5->fatal_irq < 0)
  364. return q6v5->fatal_irq;
  365. ret = devm_request_threaded_irq(&pdev->dev, q6v5->fatal_irq,
  366. NULL, q6v5_fatal_interrupt,
  367. IRQF_TRIGGER_RISING | IRQF_ONESHOT,
  368. "q6v5 fatal", q6v5);
  369. if (ret) {
  370. dev_err(&pdev->dev, "failed to acquire fatal IRQ\n");
  371. return ret;
  372. }
  373. q6v5->ready_irq = platform_get_irq_byname(pdev, "ready");
  374. if (q6v5->ready_irq < 0)
  375. return q6v5->ready_irq;
  376. ret = devm_request_threaded_irq(&pdev->dev, q6v5->ready_irq,
  377. NULL, q6v5_ready_interrupt,
  378. IRQF_TRIGGER_RISING | IRQF_ONESHOT,
  379. "q6v5 ready", q6v5);
  380. if (ret) {
  381. dev_err(&pdev->dev, "failed to acquire ready IRQ\n");
  382. return ret;
  383. }
  384. q6v5->handover_irq = platform_get_irq_byname(pdev, "handover");
  385. if (q6v5->handover_irq < 0)
  386. return q6v5->handover_irq;
  387. ret = devm_request_threaded_irq(&pdev->dev, q6v5->handover_irq,
  388. NULL, q6v5_handover_interrupt,
  389. IRQF_TRIGGER_RISING | IRQF_ONESHOT,
  390. "q6v5 handover", q6v5);
  391. if (ret) {
  392. dev_err(&pdev->dev, "failed to acquire handover IRQ\n");
  393. return ret;
  394. }
  395. disable_irq(q6v5->handover_irq);
  396. q6v5->stop_irq = platform_get_irq_byname(pdev, "stop-ack");
  397. if (q6v5->stop_irq < 0)
  398. return q6v5->stop_irq;
  399. ret = devm_request_threaded_irq(&pdev->dev, q6v5->stop_irq,
  400. NULL, q6v5_stop_interrupt,
  401. IRQF_TRIGGER_RISING | IRQF_ONESHOT,
  402. "q6v5 stop", q6v5);
  403. if (ret) {
  404. dev_err(&pdev->dev, "failed to acquire stop-ack IRQ\n");
  405. return ret;
  406. }
  407. q6v5->state = qcom_smem_state_get(&pdev->dev, "stop", &q6v5->stop_bit);
  408. if (IS_ERR(q6v5->state)) {
  409. dev_err(&pdev->dev, "failed to acquire stop state\n");
  410. return PTR_ERR(q6v5->state);
  411. }
  412. INIT_WORK(&q6v5->crash_handler, qcom_q6v5_crash_handler_work);
  413. spin_lock_init(&q6v5->silent_ssr_lock);
  414. return 0;
  415. }
  416. EXPORT_SYMBOL_GPL(qcom_q6v5_init);
  417. MODULE_LICENSE("GPL v2");
  418. MODULE_DESCRIPTION("Qualcomm Peripheral Image Loader for Q6V5");