1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
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.
6 // Copyright(c) 2018 Intel Corporation. All rights reserved.
8 // Author: Liam Girdwood <liam.r.girdwood@linux.intel.com>
13 #include "sof-audio.h"
16 * Helper function to determine the target DSP state during
17 * system suspend. This function only cares about the device
18 * D-states. Platform-specific substates, if any, should be
19 * handled by the platform-specific parts.
21 static u32 snd_sof_dsp_power_target(struct snd_sof_dev *sdev)
25 switch (sdev->system_suspend_target) {
27 /* DSP should be in D3 if the system is suspending to S3 */
28 target_dsp_state = SOF_DSP_PM_D3;
30 case SOF_SUSPEND_S0IX:
32 * Currently, the only criterion for retaining the DSP in D0
33 * is that there are streams that ignored the suspend trigger.
34 * Additional criteria such Soundwire clock-stop mode and
35 * device suspend latency considerations will be added later.
37 if (snd_sof_stream_suspend_ignored(sdev))
38 target_dsp_state = SOF_DSP_PM_D0;
40 target_dsp_state = SOF_DSP_PM_D3;
43 /* This case would be during runtime suspend */
44 target_dsp_state = SOF_DSP_PM_D3;
48 return target_dsp_state;
51 #if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_ENABLE_DEBUGFS_CACHE)
52 static void sof_cache_debugfs(struct snd_sof_dev *sdev)
54 struct snd_sof_dfsentry *dfse;
56 list_for_each_entry(dfse, &sdev->dfsentry_list, list) {
58 /* nothing to do if debugfs buffer is not IO mem */
59 if (dfse->type == SOF_DFSENTRY_TYPE_BUF)
62 /* cache memory that is only accessible in D0 */
63 if (dfse->access_type == SOF_DEBUGFS_ACCESS_D0_ONLY)
64 memcpy_fromio(dfse->cache_buf, dfse->io_mem,
70 static int sof_resume(struct device *dev, bool runtime_resume)
72 struct snd_sof_dev *sdev = dev_get_drvdata(dev);
73 const struct sof_ipc_pm_ops *pm_ops = sdev->ipc->ops->pm;
74 const struct sof_ipc_tplg_ops *tplg_ops = sdev->ipc->ops->tplg;
75 u32 old_state = sdev->dsp_power_state.state;
78 /* do nothing if dsp resume callbacks are not set */
79 if (!runtime_resume && !sof_ops(sdev)->resume)
82 if (runtime_resume && !sof_ops(sdev)->runtime_resume)
85 /* DSP was never successfully started, nothing to resume */
90 * if the runtime_resume flag is set, call the runtime_resume routine
91 * or else call the system resume routine
94 ret = snd_sof_dsp_runtime_resume(sdev);
96 ret = snd_sof_dsp_resume(sdev);
99 "error: failed to power up DSP after resume\n");
104 * Nothing further to be done for platforms that support the low power
105 * D0 substate. Resume trace and return when resuming from
106 * low-power D0 substate
108 if (!runtime_resume && sof_ops(sdev)->set_power_state &&
109 old_state == SOF_DSP_PM_D0) {
110 ret = snd_sof_trace_resume(sdev);
114 "failed to enable trace after resume %d\n", ret);
118 sof_set_fw_state(sdev, SOF_FW_BOOT_PREPARE);
120 /* load the firmware */
121 ret = snd_sof_load_firmware(sdev);
124 "error: failed to load DSP firmware after resume %d\n",
126 sof_set_fw_state(sdev, SOF_FW_BOOT_FAILED);
130 sof_set_fw_state(sdev, SOF_FW_BOOT_IN_PROGRESS);
133 * Boot the firmware. The FW boot status will be modified
134 * in snd_sof_run_firmware() depending on the outcome.
136 ret = snd_sof_run_firmware(sdev);
139 "error: failed to boot DSP firmware after resume %d\n",
141 sof_set_fw_state(sdev, SOF_FW_BOOT_FAILED);
145 /* resume DMA trace */
146 ret = snd_sof_trace_resume(sdev);
150 "warning: failed to init trace after resume %d\n",
154 /* restore pipelines */
155 if (tplg_ops->set_up_all_pipelines) {
156 ret = tplg_ops->set_up_all_pipelines(sdev, false);
158 dev_err(sdev->dev, "Failed to restore pipeline after resume %d\n", ret);
163 /* Notify clients not managed by pm framework about core resume */
164 sof_resume_clients(sdev);
166 /* notify DSP of system resume */
167 if (pm_ops && pm_ops->ctx_restore) {
168 ret = pm_ops->ctx_restore(sdev);
170 dev_err(sdev->dev, "ctx_restore IPC error during resume: %d\n", ret);
176 static int sof_suspend(struct device *dev, bool runtime_suspend)
178 struct snd_sof_dev *sdev = dev_get_drvdata(dev);
179 const struct sof_ipc_pm_ops *pm_ops = sdev->ipc->ops->pm;
180 const struct sof_ipc_tplg_ops *tplg_ops = sdev->ipc->ops->tplg;
181 pm_message_t pm_state;
182 u32 target_state = 0;
185 /* do nothing if dsp suspend callback is not set */
186 if (!runtime_suspend && !sof_ops(sdev)->suspend)
189 if (runtime_suspend && !sof_ops(sdev)->runtime_suspend)
192 if (sdev->fw_state != SOF_FW_BOOT_COMPLETE)
195 /* prepare for streams to be resumed properly upon resume */
196 if (!runtime_suspend) {
197 ret = snd_sof_dsp_hw_params_upon_resume(sdev);
200 "error: setting hw_params flag during suspend %d\n",
206 target_state = snd_sof_dsp_power_target(sdev);
207 pm_state.event = target_state;
209 /* Skip to platform-specific suspend if DSP is entering D0 */
210 if (target_state == SOF_DSP_PM_D0) {
211 snd_sof_trace_suspend(sdev, pm_state);
212 /* Notify clients not managed by pm framework about core suspend */
213 sof_suspend_clients(sdev, pm_state);
217 if (tplg_ops->tear_down_all_pipelines)
218 tplg_ops->tear_down_all_pipelines(sdev, false);
220 /* suspend DMA trace */
221 snd_sof_trace_suspend(sdev, pm_state);
223 /* Notify clients not managed by pm framework about core suspend */
224 sof_suspend_clients(sdev, pm_state);
226 #if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_ENABLE_DEBUGFS_CACHE)
227 /* cache debugfs contents during runtime suspend */
229 sof_cache_debugfs(sdev);
231 /* notify DSP of upcoming power down */
232 if (pm_ops && pm_ops->ctx_save) {
233 ret = pm_ops->ctx_save(sdev);
234 if (ret == -EBUSY || ret == -EAGAIN) {
236 * runtime PM has logic to handle -EBUSY/-EAGAIN so
237 * pass these errors up
239 dev_err(sdev->dev, "ctx_save IPC error during suspend: %d\n", ret);
241 } else if (ret < 0) {
242 /* FW in unexpected state, continue to power down */
243 dev_warn(sdev->dev, "ctx_save IPC error: %d, proceeding with suspend\n",
250 /* return if the DSP was not probed successfully */
251 if (sdev->fw_state == SOF_FW_BOOT_NOT_STARTED)
254 /* platform-specific suspend */
256 ret = snd_sof_dsp_runtime_suspend(sdev);
258 ret = snd_sof_dsp_suspend(sdev, target_state);
261 "error: failed to power down DSP during suspend %d\n",
264 /* Do not reset FW state if DSP is in D0 */
265 if (target_state == SOF_DSP_PM_D0)
269 sof_set_fw_state(sdev, SOF_FW_BOOT_NOT_STARTED);
270 sdev->enabled_cores_mask = 0;
275 int snd_sof_dsp_power_down_notify(struct snd_sof_dev *sdev)
277 const struct sof_ipc_pm_ops *pm_ops = sdev->ipc->ops->pm;
279 /* Notify DSP of upcoming power down */
280 if (sof_ops(sdev)->remove && pm_ops && pm_ops->ctx_save)
281 return pm_ops->ctx_save(sdev);
286 int snd_sof_runtime_suspend(struct device *dev)
288 return sof_suspend(dev, true);
290 EXPORT_SYMBOL(snd_sof_runtime_suspend);
292 int snd_sof_runtime_idle(struct device *dev)
294 struct snd_sof_dev *sdev = dev_get_drvdata(dev);
296 return snd_sof_dsp_runtime_idle(sdev);
298 EXPORT_SYMBOL(snd_sof_runtime_idle);
300 int snd_sof_runtime_resume(struct device *dev)
302 return sof_resume(dev, true);
304 EXPORT_SYMBOL(snd_sof_runtime_resume);
306 int snd_sof_resume(struct device *dev)
308 return sof_resume(dev, false);
310 EXPORT_SYMBOL(snd_sof_resume);
312 int snd_sof_suspend(struct device *dev)
314 return sof_suspend(dev, false);
316 EXPORT_SYMBOL(snd_sof_suspend);
318 int snd_sof_prepare(struct device *dev)
320 struct snd_sof_dev *sdev = dev_get_drvdata(dev);
321 const struct sof_dev_desc *desc = sdev->pdata->desc;
323 /* will suspend to S3 by default */
324 sdev->system_suspend_target = SOF_SUSPEND_S3;
327 * if the firmware is crashed or boot failed then we try to aim for S3
328 * to reboot the firmware
330 if (sdev->fw_state == SOF_FW_CRASHED ||
331 sdev->fw_state == SOF_FW_BOOT_FAILED)
334 if (!desc->use_acpi_target_states)
337 #if defined(CONFIG_ACPI)
338 if (acpi_target_system_state() == ACPI_STATE_S0)
339 sdev->system_suspend_target = SOF_SUSPEND_S0IX;
344 EXPORT_SYMBOL(snd_sof_prepare);
346 void snd_sof_complete(struct device *dev)
348 struct snd_sof_dev *sdev = dev_get_drvdata(dev);
350 sdev->system_suspend_target = SOF_SUSPEND_NONE;
352 EXPORT_SYMBOL(snd_sof_complete);