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.
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);
55 /* do nothing if dsp resume callbacks are not set */
56 if (!sof_ops(sdev)->resume || !sof_ops(sdev)->runtime_resume)
59 /* DSP was never successfully started, nothing to resume */
64 * if the runtime_resume flag is set, call the runtime_resume routine
65 * or else call the system resume routine
68 ret = snd_sof_dsp_runtime_resume(sdev);
70 ret = snd_sof_dsp_resume(sdev);
73 "error: failed to power up DSP after resume\n");
77 sdev->fw_state = SOF_FW_BOOT_PREPARE;
79 /* load the firmware */
80 ret = snd_sof_load_firmware(sdev);
83 "error: failed to load DSP firmware after resume %d\n",
88 sdev->fw_state = SOF_FW_BOOT_IN_PROGRESS;
91 * Boot the firmware. The FW boot status will be modified
92 * in snd_sof_run_firmware() depending on the outcome.
94 ret = snd_sof_run_firmware(sdev);
97 "error: failed to boot DSP firmware after resume %d\n",
102 /* resume DMA trace, only need send ipc */
103 ret = snd_sof_init_trace_ipc(sdev);
107 "warning: failed to init trace after resume %d\n",
111 /* restore pipelines */
112 ret = sof_restore_pipelines(sdev->dev);
115 "error: failed to restore pipeline after resume %d\n",
120 /* notify DSP of system resume */
121 ret = sof_send_pm_ctx_ipc(sdev, SOF_IPC_PM_CTX_RESTORE);
124 "error: ctx_restore ipc error during resume %d\n",
127 /* initialize default D0 sub-state */
128 sdev->d0_substate = SOF_DSP_D0I0;
133 static int sof_suspend(struct device *dev, bool runtime_suspend)
135 struct snd_sof_dev *sdev = dev_get_drvdata(dev);
138 /* do nothing if dsp suspend callback is not set */
139 if (!sof_ops(sdev)->suspend)
142 if (sdev->fw_state != SOF_FW_BOOT_COMPLETE)
146 snd_sof_release_trace(sdev);
148 /* set restore_stream for all streams during system suspend */
149 if (!runtime_suspend) {
150 ret = sof_set_hw_params_upon_resume(sdev->dev);
153 "error: setting hw_params flag during suspend %d\n",
159 #if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_ENABLE_DEBUGFS_CACHE)
160 /* cache debugfs contents during runtime suspend */
162 sof_cache_debugfs(sdev);
164 /* notify DSP of upcoming power down */
165 ret = sof_send_pm_ctx_ipc(sdev, SOF_IPC_PM_CTX_SAVE);
166 if (ret == -EBUSY || ret == -EAGAIN) {
168 * runtime PM has logic to handle -EBUSY/-EAGAIN so
169 * pass these errors up
172 "error: ctx_save ipc error during suspend %d\n",
175 } else if (ret < 0) {
176 /* FW in unexpected state, continue to power down */
178 "ctx_save ipc error %d, proceeding with suspend\n",
184 /* return if the DSP was not probed successfully */
185 if (sdev->fw_state == SOF_FW_BOOT_NOT_STARTED)
188 /* power down all DSP cores */
190 ret = snd_sof_dsp_runtime_suspend(sdev);
192 ret = snd_sof_dsp_suspend(sdev);
195 "error: failed to power down DSP during suspend %d\n",
199 sdev->fw_state = SOF_FW_BOOT_NOT_STARTED;
204 int snd_sof_runtime_suspend(struct device *dev)
206 return sof_suspend(dev, true);
208 EXPORT_SYMBOL(snd_sof_runtime_suspend);
210 int snd_sof_runtime_idle(struct device *dev)
212 struct snd_sof_dev *sdev = dev_get_drvdata(dev);
214 return snd_sof_dsp_runtime_idle(sdev);
216 EXPORT_SYMBOL(snd_sof_runtime_idle);
218 int snd_sof_runtime_resume(struct device *dev)
220 return sof_resume(dev, true);
222 EXPORT_SYMBOL(snd_sof_runtime_resume);
224 int snd_sof_set_d0_substate(struct snd_sof_dev *sdev,
225 enum sof_d0_substate d0_substate)
229 if (sdev->d0_substate == d0_substate)
232 /* do platform specific set_state */
233 ret = snd_sof_dsp_set_power_state(sdev, d0_substate);
237 /* update dsp D0 sub-state */
238 sdev->d0_substate = d0_substate;
242 EXPORT_SYMBOL(snd_sof_set_d0_substate);
245 * Audio DSP states may transform as below:-
247 * D0I3 compatible stream
248 * Runtime +---------------------+ opened only, timeout
249 * suspend | +--------------------+
250 * +------------+ D0(active) | |
251 * | | <---------------+ |
253 * | |Runtime +--^--+---------^--+--+ The last | |
254 * | |resume | | | | opened D0I3 | |
255 * | | | | | | compatible | |
256 * | | resume| | | | stream closed | |
257 * | | from | | D3 | | | |
258 * | | D3 | |suspend | | d0i3 | |
259 * | | | | | |suspend | |
262 * +-v---+-----------+--v-------+ | | +------+----v----+
263 * | | | +-----------> |
264 * | D3 (suspended) | | | D0I3 +-----+
265 * | | +--------------+ | |
266 * | | resume from | | |
267 * +-------------------^--------+ d0i3 suspend +----------------+ |
270 * +------------------------------------------------+
272 * d0i3_suspend = s0_suspend && D0I3 stream opened,
273 * D3 suspend = !d0i3_suspend,
276 int snd_sof_resume(struct device *dev)
278 struct snd_sof_dev *sdev = dev_get_drvdata(dev);
281 if (snd_sof_dsp_d0i3_on_suspend(sdev)) {
282 /* resume from D0I3 */
283 dev_dbg(sdev->dev, "DSP will exit from D0i3...\n");
284 ret = snd_sof_set_d0_substate(sdev, SOF_DSP_D0I0);
285 if (ret == -ENOTSUPP) {
286 /* fallback to resume from D3 */
287 dev_dbg(sdev->dev, "D0i3 not supported, fall back to resume from D3...\n");
289 } else if (ret < 0) {
290 dev_err(sdev->dev, "error: failed to exit from D0I3 %d\n",
295 /* platform-specific resume from D0i3 */
296 return snd_sof_dsp_resume(sdev);
301 return sof_resume(dev, false);
303 EXPORT_SYMBOL(snd_sof_resume);
305 int snd_sof_suspend(struct device *dev)
307 struct snd_sof_dev *sdev = dev_get_drvdata(dev);
310 if (snd_sof_dsp_d0i3_on_suspend(sdev)) {
311 /* suspend to D0i3 */
312 dev_dbg(sdev->dev, "DSP is trying to enter D0i3...\n");
313 ret = snd_sof_set_d0_substate(sdev, SOF_DSP_D0I3);
314 if (ret == -ENOTSUPP) {
315 /* fallback to D3 suspend */
316 dev_dbg(sdev->dev, "D0i3 not supported, fall back to D3...\n");
318 } else if (ret < 0) {
319 dev_err(sdev->dev, "error: failed to enter D0I3, %d\n",
324 /* platform-specific suspend to D0i3 */
325 return snd_sof_dsp_suspend(sdev);
330 return sof_suspend(dev, false);
332 EXPORT_SYMBOL(snd_sof_suspend);
334 int snd_sof_prepare(struct device *dev)
336 struct snd_sof_dev *sdev = dev_get_drvdata(dev);
338 #if defined(CONFIG_ACPI)
339 sdev->s0_suspend = acpi_target_system_state() == ACPI_STATE_S0;
341 /* will suspend to S3 by default */
342 sdev->s0_suspend = false;
347 EXPORT_SYMBOL(snd_sof_prepare);
349 void snd_sof_complete(struct device *dev)
351 struct snd_sof_dev *sdev = dev_get_drvdata(dev);
353 sdev->s0_suspend = false;
355 EXPORT_SYMBOL(snd_sof_complete);