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) {
28 /* DSP should be in D3 if the system is suspending to S3+ */
30 /* DSP should be in D3 if the system is suspending to S3 */
31 target_dsp_state = SOF_DSP_PM_D3;
33 case SOF_SUSPEND_S0IX:
35 * Currently, the only criterion for retaining the DSP in D0
36 * is that there are streams that ignored the suspend trigger.
37 * Additional criteria such Soundwire clock-stop mode and
38 * device suspend latency considerations will be added later.
40 if (snd_sof_stream_suspend_ignored(sdev))
41 target_dsp_state = SOF_DSP_PM_D0;
43 target_dsp_state = SOF_DSP_PM_D3;
46 /* This case would be during runtime suspend */
47 target_dsp_state = SOF_DSP_PM_D3;
51 return target_dsp_state;
54 #if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_ENABLE_DEBUGFS_CACHE)
55 static void sof_cache_debugfs(struct snd_sof_dev *sdev)
57 struct snd_sof_dfsentry *dfse;
59 list_for_each_entry(dfse, &sdev->dfsentry_list, list) {
61 /* nothing to do if debugfs buffer is not IO mem */
62 if (dfse->type == SOF_DFSENTRY_TYPE_BUF)
65 /* cache memory that is only accessible in D0 */
66 if (dfse->access_type == SOF_DEBUGFS_ACCESS_D0_ONLY)
67 memcpy_fromio(dfse->cache_buf, dfse->io_mem,
73 static int sof_resume(struct device *dev, bool runtime_resume)
75 struct snd_sof_dev *sdev = dev_get_drvdata(dev);
76 const struct sof_ipc_pm_ops *pm_ops = sof_ipc_get_ops(sdev, pm);
77 const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg);
78 u32 old_state = sdev->dsp_power_state.state;
81 /* do nothing if dsp resume callbacks are not set */
82 if (!runtime_resume && !sof_ops(sdev)->resume)
85 if (runtime_resume && !sof_ops(sdev)->runtime_resume)
88 /* DSP was never successfully started, nothing to resume */
93 * if the runtime_resume flag is set, call the runtime_resume routine
94 * or else call the system resume routine
97 ret = snd_sof_dsp_runtime_resume(sdev);
99 ret = snd_sof_dsp_resume(sdev);
102 "error: failed to power up DSP after resume\n");
107 * Nothing further to be done for platforms that support the low power
108 * D0 substate. Resume trace and return when resuming from
109 * low-power D0 substate
111 if (!runtime_resume && sof_ops(sdev)->set_power_state &&
112 old_state == SOF_DSP_PM_D0) {
113 ret = sof_fw_trace_resume(sdev);
117 "failed to enable trace after resume %d\n", ret);
121 sof_set_fw_state(sdev, SOF_FW_BOOT_PREPARE);
123 /* load the firmware */
124 ret = snd_sof_load_firmware(sdev);
127 "error: failed to load DSP firmware after resume %d\n",
129 sof_set_fw_state(sdev, SOF_FW_BOOT_FAILED);
133 sof_set_fw_state(sdev, SOF_FW_BOOT_IN_PROGRESS);
136 * Boot the firmware. The FW boot status will be modified
137 * in snd_sof_run_firmware() depending on the outcome.
139 ret = snd_sof_run_firmware(sdev);
142 "error: failed to boot DSP firmware after resume %d\n",
144 sof_set_fw_state(sdev, SOF_FW_BOOT_FAILED);
148 /* resume DMA trace */
149 ret = sof_fw_trace_resume(sdev);
153 "warning: failed to init trace after resume %d\n",
157 /* restore pipelines */
158 if (tplg_ops && tplg_ops->set_up_all_pipelines) {
159 ret = tplg_ops->set_up_all_pipelines(sdev, false);
161 dev_err(sdev->dev, "Failed to restore pipeline after resume %d\n", ret);
166 /* Notify clients not managed by pm framework about core resume */
167 sof_resume_clients(sdev);
169 /* notify DSP of system resume */
170 if (pm_ops && pm_ops->ctx_restore) {
171 ret = pm_ops->ctx_restore(sdev);
173 dev_err(sdev->dev, "ctx_restore IPC error during resume: %d\n", ret);
179 static int sof_suspend(struct device *dev, bool runtime_suspend)
181 struct snd_sof_dev *sdev = dev_get_drvdata(dev);
182 const struct sof_ipc_pm_ops *pm_ops = sof_ipc_get_ops(sdev, pm);
183 const struct sof_ipc_tplg_ops *tplg_ops = sof_ipc_get_ops(sdev, tplg);
184 pm_message_t pm_state;
185 u32 target_state = snd_sof_dsp_power_target(sdev);
188 /* do nothing if dsp suspend callback is not set */
189 if (!runtime_suspend && !sof_ops(sdev)->suspend)
192 if (runtime_suspend && !sof_ops(sdev)->runtime_suspend)
195 if (tplg_ops && tplg_ops->tear_down_all_pipelines)
196 tplg_ops->tear_down_all_pipelines(sdev, false);
198 if (sdev->fw_state != SOF_FW_BOOT_COMPLETE)
201 /* prepare for streams to be resumed properly upon resume */
202 if (!runtime_suspend) {
203 ret = snd_sof_dsp_hw_params_upon_resume(sdev);
206 "error: setting hw_params flag during suspend %d\n",
212 pm_state.event = target_state;
214 /* Skip to platform-specific suspend if DSP is entering D0 */
215 if (target_state == SOF_DSP_PM_D0) {
216 sof_fw_trace_suspend(sdev, pm_state);
217 /* Notify clients not managed by pm framework about core suspend */
218 sof_suspend_clients(sdev, pm_state);
222 /* suspend DMA trace */
223 sof_fw_trace_suspend(sdev, pm_state);
225 /* Notify clients not managed by pm framework about core suspend */
226 sof_suspend_clients(sdev, pm_state);
228 #if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_ENABLE_DEBUGFS_CACHE)
229 /* cache debugfs contents during runtime suspend */
231 sof_cache_debugfs(sdev);
233 /* notify DSP of upcoming power down */
234 if (pm_ops && pm_ops->ctx_save) {
235 ret = pm_ops->ctx_save(sdev);
236 if (ret == -EBUSY || ret == -EAGAIN) {
238 * runtime PM has logic to handle -EBUSY/-EAGAIN so
239 * pass these errors up
241 dev_err(sdev->dev, "ctx_save IPC error during suspend: %d\n", ret);
243 } else if (ret < 0) {
244 /* FW in unexpected state, continue to power down */
245 dev_warn(sdev->dev, "ctx_save IPC error: %d, proceeding with suspend\n",
252 /* return if the DSP was not probed successfully */
253 if (sdev->fw_state == SOF_FW_BOOT_NOT_STARTED)
256 /* platform-specific suspend */
258 ret = snd_sof_dsp_runtime_suspend(sdev);
260 ret = snd_sof_dsp_suspend(sdev, target_state);
263 "error: failed to power down DSP during suspend %d\n",
266 /* Do not reset FW state if DSP is in D0 */
267 if (target_state == SOF_DSP_PM_D0)
271 sof_set_fw_state(sdev, SOF_FW_BOOT_NOT_STARTED);
272 sdev->enabled_cores_mask = 0;
277 int snd_sof_dsp_power_down_notify(struct snd_sof_dev *sdev)
279 const struct sof_ipc_pm_ops *pm_ops = sof_ipc_get_ops(sdev, pm);
281 /* Notify DSP of upcoming power down */
282 if (sof_ops(sdev)->remove && pm_ops && pm_ops->ctx_save)
283 return pm_ops->ctx_save(sdev);
288 int snd_sof_runtime_suspend(struct device *dev)
290 return sof_suspend(dev, true);
292 EXPORT_SYMBOL(snd_sof_runtime_suspend);
294 int snd_sof_runtime_idle(struct device *dev)
296 struct snd_sof_dev *sdev = dev_get_drvdata(dev);
298 return snd_sof_dsp_runtime_idle(sdev);
300 EXPORT_SYMBOL(snd_sof_runtime_idle);
302 int snd_sof_runtime_resume(struct device *dev)
304 return sof_resume(dev, true);
306 EXPORT_SYMBOL(snd_sof_runtime_resume);
308 int snd_sof_resume(struct device *dev)
310 return sof_resume(dev, false);
312 EXPORT_SYMBOL(snd_sof_resume);
314 int snd_sof_suspend(struct device *dev)
316 return sof_suspend(dev, false);
318 EXPORT_SYMBOL(snd_sof_suspend);
320 int snd_sof_prepare(struct device *dev)
322 struct snd_sof_dev *sdev = dev_get_drvdata(dev);
323 const struct sof_dev_desc *desc = sdev->pdata->desc;
325 /* will suspend to S3 by default */
326 sdev->system_suspend_target = SOF_SUSPEND_S3;
329 * if the firmware is crashed or boot failed then we try to aim for S3
330 * to reboot the firmware
332 if (sdev->fw_state == SOF_FW_CRASHED ||
333 sdev->fw_state == SOF_FW_BOOT_FAILED)
336 if (!desc->use_acpi_target_states)
339 #if defined(CONFIG_ACPI)
340 switch (acpi_target_system_state()) {
342 sdev->system_suspend_target = SOF_SUSPEND_S0IX;
347 sdev->system_suspend_target = SOF_SUSPEND_S3;
350 sdev->system_suspend_target = SOF_SUSPEND_S4;
353 sdev->system_suspend_target = SOF_SUSPEND_S5;
362 EXPORT_SYMBOL(snd_sof_prepare);
364 void snd_sof_complete(struct device *dev)
366 struct snd_sof_dev *sdev = dev_get_drvdata(dev);
368 sdev->system_suspend_target = SOF_SUSPEND_NONE;
370 EXPORT_SYMBOL(snd_sof_complete);