1 // SPDX-License-Identifier: (GPL-2.0 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"
15 static int sof_send_pm_ctx_ipc(struct snd_sof_dev *sdev, int cmd)
17 struct sof_ipc_pm_ctx pm_ctx;
18 struct sof_ipc_reply reply;
20 memset(&pm_ctx, 0, sizeof(pm_ctx));
22 /* configure ctx save ipc message */
23 pm_ctx.hdr.size = sizeof(pm_ctx);
24 pm_ctx.hdr.cmd = SOF_IPC_GLB_PM_MSG | cmd;
26 /* send ctx save ipc to dsp */
27 return sof_ipc_tx_message(sdev->ipc, pm_ctx.hdr.cmd, &pm_ctx,
28 sizeof(pm_ctx), &reply, sizeof(reply));
31 #if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_ENABLE_DEBUGFS_CACHE)
32 static void sof_cache_debugfs(struct snd_sof_dev *sdev)
34 struct snd_sof_dfsentry *dfse;
36 list_for_each_entry(dfse, &sdev->dfsentry_list, list) {
38 /* nothing to do if debugfs buffer is not IO mem */
39 if (dfse->type == SOF_DFSENTRY_TYPE_BUF)
42 /* cache memory that is only accessible in D0 */
43 if (dfse->access_type == SOF_DEBUGFS_ACCESS_D0_ONLY)
44 memcpy_fromio(dfse->cache_buf, dfse->io_mem,
50 static int sof_resume(struct device *dev, bool runtime_resume)
52 struct snd_sof_dev *sdev = dev_get_drvdata(dev);
53 enum sof_d0_substate old_d0_substate = sdev->d0_substate;
56 /* do nothing if dsp resume callbacks are not set */
57 if (!sof_ops(sdev)->resume || !sof_ops(sdev)->runtime_resume)
60 /* DSP was never successfully started, nothing to resume */
64 /* resume from D0I3 */
65 if (!runtime_resume && old_d0_substate == SOF_DSP_D0I3) {
66 ret = snd_sof_set_d0_substate(sdev, SOF_DSP_D0I0);
67 if (ret < 0 && ret != -ENOTSUPP) {
69 "error: failed to resume from D0I3 %d\n",
76 * if the runtime_resume flag is set, call the runtime_resume routine
77 * or else call the system resume routine
80 ret = snd_sof_dsp_runtime_resume(sdev);
82 ret = snd_sof_dsp_resume(sdev);
85 "error: failed to power up DSP after resume\n");
89 /* Nothing further to do if resuming from D0I3 */
90 if (!runtime_resume && old_d0_substate == SOF_DSP_D0I3)
93 sdev->fw_state = SOF_FW_BOOT_PREPARE;
95 /* load the firmware */
96 ret = snd_sof_load_firmware(sdev);
99 "error: failed to load DSP firmware after resume %d\n",
104 sdev->fw_state = SOF_FW_BOOT_IN_PROGRESS;
107 * Boot the firmware. The FW boot status will be modified
108 * in snd_sof_run_firmware() depending on the outcome.
110 ret = snd_sof_run_firmware(sdev);
113 "error: failed to boot DSP firmware after resume %d\n",
118 /* resume DMA trace, only need send ipc */
119 ret = snd_sof_init_trace_ipc(sdev);
123 "warning: failed to init trace after resume %d\n",
127 /* restore pipelines */
128 ret = sof_restore_pipelines(sdev->dev);
131 "error: failed to restore pipeline after resume %d\n",
136 /* notify DSP of system resume */
137 ret = sof_send_pm_ctx_ipc(sdev, SOF_IPC_PM_CTX_RESTORE);
140 "error: ctx_restore ipc error during resume %d\n",
143 /* initialize default D0 sub-state */
144 sdev->d0_substate = SOF_DSP_D0I0;
149 static int sof_suspend(struct device *dev, bool runtime_suspend)
151 struct snd_sof_dev *sdev = dev_get_drvdata(dev);
154 /* do nothing if dsp suspend callback is not set */
155 if (!sof_ops(sdev)->suspend)
158 if (sdev->fw_state != SOF_FW_BOOT_COMPLETE)
161 /* set restore_stream for all streams during system suspend */
162 if (!runtime_suspend) {
163 ret = sof_set_hw_params_upon_resume(sdev->dev);
166 "error: setting hw_params flag during suspend %d\n",
172 if (snd_sof_dsp_d0i3_on_suspend(sdev)) {
173 /* suspend to D0i3 */
174 ret = snd_sof_set_d0_substate(sdev, SOF_DSP_D0I3);
176 dev_err(sdev->dev, "error: failed to enter D0I3, %d\n",
181 /* Skip to platform-specific suspend if DSP is entering D0I3 */
186 snd_sof_release_trace(sdev);
188 #if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_ENABLE_DEBUGFS_CACHE)
189 /* cache debugfs contents during runtime suspend */
191 sof_cache_debugfs(sdev);
193 /* notify DSP of upcoming power down */
194 ret = sof_send_pm_ctx_ipc(sdev, SOF_IPC_PM_CTX_SAVE);
195 if (ret == -EBUSY || ret == -EAGAIN) {
197 * runtime PM has logic to handle -EBUSY/-EAGAIN so
198 * pass these errors up
201 "error: ctx_save ipc error during suspend %d\n",
204 } else if (ret < 0) {
205 /* FW in unexpected state, continue to power down */
207 "ctx_save ipc error %d, proceeding with suspend\n",
213 /* return if the DSP was not probed successfully */
214 if (sdev->fw_state == SOF_FW_BOOT_NOT_STARTED)
217 /* platform-specific suspend */
219 ret = snd_sof_dsp_runtime_suspend(sdev);
221 ret = snd_sof_dsp_suspend(sdev);
224 "error: failed to power down DSP during suspend %d\n",
227 /* Do not reset FW state if DSP is in D0I3 */
228 if (sdev->d0_substate == SOF_DSP_D0I3)
232 sdev->fw_state = SOF_FW_BOOT_NOT_STARTED;
237 int snd_sof_runtime_suspend(struct device *dev)
239 return sof_suspend(dev, true);
241 EXPORT_SYMBOL(snd_sof_runtime_suspend);
243 int snd_sof_runtime_idle(struct device *dev)
245 struct snd_sof_dev *sdev = dev_get_drvdata(dev);
247 return snd_sof_dsp_runtime_idle(sdev);
249 EXPORT_SYMBOL(snd_sof_runtime_idle);
251 int snd_sof_runtime_resume(struct device *dev)
253 return sof_resume(dev, true);
255 EXPORT_SYMBOL(snd_sof_runtime_resume);
257 int snd_sof_set_d0_substate(struct snd_sof_dev *sdev,
258 enum sof_d0_substate d0_substate)
262 if (sdev->d0_substate == d0_substate)
265 /* do platform specific set_state */
266 ret = snd_sof_dsp_set_power_state(sdev, d0_substate);
270 /* update dsp D0 sub-state */
271 sdev->d0_substate = d0_substate;
275 EXPORT_SYMBOL(snd_sof_set_d0_substate);
278 * Audio DSP states may transform as below:-
280 * D0I3 compatible stream
281 * Runtime +---------------------+ opened only, timeout
282 * suspend | +--------------------+
283 * +------------+ D0(active) | |
284 * | | <---------------+ |
286 * | |Runtime +--^--+---------^--+--+ The last | |
287 * | |resume | | | | opened D0I3 | |
288 * | | | | | | compatible | |
289 * | | resume| | | | stream closed | |
290 * | | from | | D3 | | | |
291 * | | D3 | |suspend | | d0i3 | |
292 * | | | | | |suspend | |
295 * +-v---+-----------+--v-------+ | | +------+----v----+
296 * | | | +-----------> |
297 * | D3 (suspended) | | | D0I3 +-----+
298 * | | +--------------+ | |
299 * | | resume from | | |
300 * +-------------------^--------+ d0i3 suspend +----------------+ |
303 * +------------------------------------------------+
305 * d0i3_suspend = s0_suspend && D0I3 stream opened,
306 * D3 suspend = !d0i3_suspend,
309 int snd_sof_resume(struct device *dev)
311 return sof_resume(dev, false);
313 EXPORT_SYMBOL(snd_sof_resume);
315 int snd_sof_suspend(struct device *dev)
317 return sof_suspend(dev, false);
319 EXPORT_SYMBOL(snd_sof_suspend);
321 int snd_sof_prepare(struct device *dev)
323 struct snd_sof_dev *sdev = dev_get_drvdata(dev);
325 #if defined(CONFIG_ACPI)
326 if (acpi_target_system_state() == ACPI_STATE_S0)
327 sdev->system_suspend_target = SOF_SUSPEND_S0IX;
329 sdev->system_suspend_target = SOF_SUSPEND_S3;
331 /* will suspend to S3 by default */
332 sdev->system_suspend_target = SOF_SUSPEND_S3;
337 EXPORT_SYMBOL(snd_sof_prepare);
339 void snd_sof_complete(struct device *dev)
341 struct snd_sof_dev *sdev = dev_get_drvdata(dev);
343 sdev->system_suspend_target = SOF_SUSPEND_NONE;
345 EXPORT_SYMBOL(snd_sof_complete);