pm.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
  2. //
  3. // This file is provided under a dual BSD/GPLv2 license. When using or
  4. // redistributing this file, you may do so under either license.
  5. //
  6. // Copyright(c) 2018 Intel Corporation. All rights reserved.
  7. //
  8. // Author: Liam Girdwood <liam.r.girdwood@linux.intel.com>
  9. //
  10. #include "ops.h"
  11. #include "sof-priv.h"
  12. #include "sof-audio.h"
  13. /*
  14. * Helper function to determine the target DSP state during
  15. * system suspend. This function only cares about the device
  16. * D-states. Platform-specific substates, if any, should be
  17. * handled by the platform-specific parts.
  18. */
  19. static u32 snd_sof_dsp_power_target(struct snd_sof_dev *sdev)
  20. {
  21. u32 target_dsp_state;
  22. switch (sdev->system_suspend_target) {
  23. case SOF_SUSPEND_S3:
  24. /* DSP should be in D3 if the system is suspending to S3 */
  25. target_dsp_state = SOF_DSP_PM_D3;
  26. break;
  27. case SOF_SUSPEND_S0IX:
  28. /*
  29. * Currently, the only criterion for retaining the DSP in D0
  30. * is that there are streams that ignored the suspend trigger.
  31. * Additional criteria such Soundwire clock-stop mode and
  32. * device suspend latency considerations will be added later.
  33. */
  34. if (snd_sof_stream_suspend_ignored(sdev))
  35. target_dsp_state = SOF_DSP_PM_D0;
  36. else
  37. target_dsp_state = SOF_DSP_PM_D3;
  38. break;
  39. default:
  40. /* This case would be during runtime suspend */
  41. target_dsp_state = SOF_DSP_PM_D3;
  42. break;
  43. }
  44. return target_dsp_state;
  45. }
  46. static int sof_send_pm_ctx_ipc(struct snd_sof_dev *sdev, int cmd)
  47. {
  48. struct sof_ipc_pm_ctx pm_ctx;
  49. struct sof_ipc_reply reply;
  50. memset(&pm_ctx, 0, sizeof(pm_ctx));
  51. /* configure ctx save ipc message */
  52. pm_ctx.hdr.size = sizeof(pm_ctx);
  53. pm_ctx.hdr.cmd = SOF_IPC_GLB_PM_MSG | cmd;
  54. /* send ctx save ipc to dsp */
  55. return sof_ipc_tx_message(sdev->ipc, pm_ctx.hdr.cmd, &pm_ctx,
  56. sizeof(pm_ctx), &reply, sizeof(reply));
  57. }
  58. #if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_ENABLE_DEBUGFS_CACHE)
  59. static void sof_cache_debugfs(struct snd_sof_dev *sdev)
  60. {
  61. struct snd_sof_dfsentry *dfse;
  62. list_for_each_entry(dfse, &sdev->dfsentry_list, list) {
  63. /* nothing to do if debugfs buffer is not IO mem */
  64. if (dfse->type == SOF_DFSENTRY_TYPE_BUF)
  65. continue;
  66. /* cache memory that is only accessible in D0 */
  67. if (dfse->access_type == SOF_DEBUGFS_ACCESS_D0_ONLY)
  68. memcpy_fromio(dfse->cache_buf, dfse->io_mem,
  69. dfse->size);
  70. }
  71. }
  72. #endif
  73. static int sof_resume(struct device *dev, bool runtime_resume)
  74. {
  75. struct snd_sof_dev *sdev = dev_get_drvdata(dev);
  76. u32 old_state = sdev->dsp_power_state.state;
  77. int ret;
  78. /* do nothing if dsp resume callbacks are not set */
  79. if (!runtime_resume && !sof_ops(sdev)->resume)
  80. return 0;
  81. if (runtime_resume && !sof_ops(sdev)->runtime_resume)
  82. return 0;
  83. /* DSP was never successfully started, nothing to resume */
  84. if (sdev->first_boot)
  85. return 0;
  86. /*
  87. * if the runtime_resume flag is set, call the runtime_resume routine
  88. * or else call the system resume routine
  89. */
  90. if (runtime_resume)
  91. ret = snd_sof_dsp_runtime_resume(sdev);
  92. else
  93. ret = snd_sof_dsp_resume(sdev);
  94. if (ret < 0) {
  95. dev_err(sdev->dev,
  96. "error: failed to power up DSP after resume\n");
  97. return ret;
  98. }
  99. /*
  100. * Nothing further to be done for platforms that support the low power
  101. * D0 substate.
  102. */
  103. if (!runtime_resume && sof_ops(sdev)->set_power_state &&
  104. old_state == SOF_DSP_PM_D0)
  105. return 0;
  106. sdev->fw_state = SOF_FW_BOOT_PREPARE;
  107. /* load the firmware */
  108. ret = snd_sof_load_firmware(sdev);
  109. if (ret < 0) {
  110. dev_err(sdev->dev,
  111. "error: failed to load DSP firmware after resume %d\n",
  112. ret);
  113. return ret;
  114. }
  115. sdev->fw_state = SOF_FW_BOOT_IN_PROGRESS;
  116. /*
  117. * Boot the firmware. The FW boot status will be modified
  118. * in snd_sof_run_firmware() depending on the outcome.
  119. */
  120. ret = snd_sof_run_firmware(sdev);
  121. if (ret < 0) {
  122. dev_err(sdev->dev,
  123. "error: failed to boot DSP firmware after resume %d\n",
  124. ret);
  125. return ret;
  126. }
  127. /* resume DMA trace, only need send ipc */
  128. ret = snd_sof_init_trace_ipc(sdev);
  129. if (ret < 0) {
  130. /* non fatal */
  131. dev_warn(sdev->dev,
  132. "warning: failed to init trace after resume %d\n",
  133. ret);
  134. }
  135. /* restore pipelines */
  136. ret = sof_restore_pipelines(sdev->dev);
  137. if (ret < 0) {
  138. dev_err(sdev->dev,
  139. "error: failed to restore pipeline after resume %d\n",
  140. ret);
  141. return ret;
  142. }
  143. /* notify DSP of system resume */
  144. ret = sof_send_pm_ctx_ipc(sdev, SOF_IPC_PM_CTX_RESTORE);
  145. if (ret < 0)
  146. dev_err(sdev->dev,
  147. "error: ctx_restore ipc error during resume %d\n",
  148. ret);
  149. return ret;
  150. }
  151. static int sof_suspend(struct device *dev, bool runtime_suspend)
  152. {
  153. struct snd_sof_dev *sdev = dev_get_drvdata(dev);
  154. u32 target_state = 0;
  155. int ret;
  156. /* do nothing if dsp suspend callback is not set */
  157. if (!runtime_suspend && !sof_ops(sdev)->suspend)
  158. return 0;
  159. if (runtime_suspend && !sof_ops(sdev)->runtime_suspend)
  160. return 0;
  161. if (sdev->fw_state != SOF_FW_BOOT_COMPLETE)
  162. goto suspend;
  163. /* set restore_stream for all streams during system suspend */
  164. if (!runtime_suspend) {
  165. ret = sof_set_hw_params_upon_resume(sdev->dev);
  166. if (ret < 0) {
  167. dev_err(sdev->dev,
  168. "error: setting hw_params flag during suspend %d\n",
  169. ret);
  170. return ret;
  171. }
  172. }
  173. target_state = snd_sof_dsp_power_target(sdev);
  174. /* Skip to platform-specific suspend if DSP is entering D0 */
  175. if (target_state == SOF_DSP_PM_D0)
  176. goto suspend;
  177. /* release trace */
  178. snd_sof_release_trace(sdev);
  179. #if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_ENABLE_DEBUGFS_CACHE)
  180. /* cache debugfs contents during runtime suspend */
  181. if (runtime_suspend)
  182. sof_cache_debugfs(sdev);
  183. #endif
  184. /* notify DSP of upcoming power down */
  185. ret = sof_send_pm_ctx_ipc(sdev, SOF_IPC_PM_CTX_SAVE);
  186. if (ret == -EBUSY || ret == -EAGAIN) {
  187. /*
  188. * runtime PM has logic to handle -EBUSY/-EAGAIN so
  189. * pass these errors up
  190. */
  191. dev_err(sdev->dev,
  192. "error: ctx_save ipc error during suspend %d\n",
  193. ret);
  194. return ret;
  195. } else if (ret < 0) {
  196. /* FW in unexpected state, continue to power down */
  197. dev_warn(sdev->dev,
  198. "ctx_save ipc error %d, proceeding with suspend\n",
  199. ret);
  200. }
  201. suspend:
  202. /* return if the DSP was not probed successfully */
  203. if (sdev->fw_state == SOF_FW_BOOT_NOT_STARTED)
  204. return 0;
  205. /* platform-specific suspend */
  206. if (runtime_suspend)
  207. ret = snd_sof_dsp_runtime_suspend(sdev);
  208. else
  209. ret = snd_sof_dsp_suspend(sdev, target_state);
  210. if (ret < 0)
  211. dev_err(sdev->dev,
  212. "error: failed to power down DSP during suspend %d\n",
  213. ret);
  214. /* Do not reset FW state if DSP is in D0 */
  215. if (target_state == SOF_DSP_PM_D0)
  216. return ret;
  217. /* reset FW state */
  218. sdev->fw_state = SOF_FW_BOOT_NOT_STARTED;
  219. sdev->enabled_cores_mask = 0;
  220. return ret;
  221. }
  222. int snd_sof_dsp_power_down_notify(struct snd_sof_dev *sdev)
  223. {
  224. /* Notify DSP of upcoming power down */
  225. if (sof_ops(sdev)->remove)
  226. return sof_send_pm_ctx_ipc(sdev, SOF_IPC_PM_CTX_SAVE);
  227. return 0;
  228. }
  229. int snd_sof_runtime_suspend(struct device *dev)
  230. {
  231. return sof_suspend(dev, true);
  232. }
  233. EXPORT_SYMBOL(snd_sof_runtime_suspend);
  234. int snd_sof_runtime_idle(struct device *dev)
  235. {
  236. struct snd_sof_dev *sdev = dev_get_drvdata(dev);
  237. return snd_sof_dsp_runtime_idle(sdev);
  238. }
  239. EXPORT_SYMBOL(snd_sof_runtime_idle);
  240. int snd_sof_runtime_resume(struct device *dev)
  241. {
  242. return sof_resume(dev, true);
  243. }
  244. EXPORT_SYMBOL(snd_sof_runtime_resume);
  245. int snd_sof_resume(struct device *dev)
  246. {
  247. return sof_resume(dev, false);
  248. }
  249. EXPORT_SYMBOL(snd_sof_resume);
  250. int snd_sof_suspend(struct device *dev)
  251. {
  252. return sof_suspend(dev, false);
  253. }
  254. EXPORT_SYMBOL(snd_sof_suspend);
  255. int snd_sof_prepare(struct device *dev)
  256. {
  257. struct snd_sof_dev *sdev = dev_get_drvdata(dev);
  258. const struct sof_dev_desc *desc = sdev->pdata->desc;
  259. /* will suspend to S3 by default */
  260. sdev->system_suspend_target = SOF_SUSPEND_S3;
  261. if (!desc->use_acpi_target_states)
  262. return 0;
  263. #if defined(CONFIG_ACPI)
  264. if (acpi_target_system_state() == ACPI_STATE_S0)
  265. sdev->system_suspend_target = SOF_SUSPEND_S0IX;
  266. #endif
  267. return 0;
  268. }
  269. EXPORT_SYMBOL(snd_sof_prepare);
  270. void snd_sof_complete(struct device *dev)
  271. {
  272. struct snd_sof_dev *sdev = dev_get_drvdata(dev);
  273. sdev->system_suspend_target = SOF_SUSPEND_NONE;
  274. }
  275. EXPORT_SYMBOL(snd_sof_complete);