blob: ac8ae6e422a788239765b3a44a6c3498d85b6980 [file] [log] [blame]
Pierre-Louis Bossarte149ca22020-05-01 09:58:50 -05001// SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
Liam Girdwood8920153c2019-04-12 11:05:16 -05002//
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
11#include "ops.h"
12#include "sof-priv.h"
Ranjani Sridharanee1e79b2019-12-04 15:15:51 -060013#include "sof-audio.h"
Liam Girdwood8920153c2019-04-12 11:05:16 -050014
Ranjani Sridharan700d1672020-01-29 16:07:21 -060015/*
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.
20 */
21static u32 snd_sof_dsp_power_target(struct snd_sof_dev *sdev)
22{
23 u32 target_dsp_state;
24
25 switch (sdev->system_suspend_target) {
26 case SOF_SUSPEND_S3:
27 /* DSP should be in D3 if the system is suspending to S3 */
28 target_dsp_state = SOF_DSP_PM_D3;
29 break;
30 case SOF_SUSPEND_S0IX:
31 /*
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.
36 */
37 if (snd_sof_stream_suspend_ignored(sdev))
38 target_dsp_state = SOF_DSP_PM_D0;
39 else
40 target_dsp_state = SOF_DSP_PM_D3;
41 break;
42 default:
43 /* This case would be during runtime suspend */
44 target_dsp_state = SOF_DSP_PM_D3;
45 break;
46 }
47
48 return target_dsp_state;
49}
50
Keyon Jie7c7eba22019-10-25 17:41:08 -050051static int sof_send_pm_ctx_ipc(struct snd_sof_dev *sdev, int cmd)
Liam Girdwood8920153c2019-04-12 11:05:16 -050052{
53 struct sof_ipc_pm_ctx pm_ctx;
54 struct sof_ipc_reply reply;
55
56 memset(&pm_ctx, 0, sizeof(pm_ctx));
57
58 /* configure ctx save ipc message */
59 pm_ctx.hdr.size = sizeof(pm_ctx);
60 pm_ctx.hdr.cmd = SOF_IPC_GLB_PM_MSG | cmd;
61
62 /* send ctx save ipc to dsp */
63 return sof_ipc_tx_message(sdev->ipc, pm_ctx.hdr.cmd, &pm_ctx,
64 sizeof(pm_ctx), &reply, sizeof(reply));
65}
66
Liam Girdwood8920153c2019-04-12 11:05:16 -050067#if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_ENABLE_DEBUGFS_CACHE)
68static void sof_cache_debugfs(struct snd_sof_dev *sdev)
69{
70 struct snd_sof_dfsentry *dfse;
71
72 list_for_each_entry(dfse, &sdev->dfsentry_list, list) {
73
74 /* nothing to do if debugfs buffer is not IO mem */
75 if (dfse->type == SOF_DFSENTRY_TYPE_BUF)
76 continue;
77
78 /* cache memory that is only accessible in D0 */
79 if (dfse->access_type == SOF_DEBUGFS_ACCESS_D0_ONLY)
80 memcpy_fromio(dfse->cache_buf, dfse->io_mem,
81 dfse->size);
82 }
83}
84#endif
85
86static int sof_resume(struct device *dev, bool runtime_resume)
87{
88 struct snd_sof_dev *sdev = dev_get_drvdata(dev);
Ranjani Sridharan61e285c2020-01-29 16:07:22 -060089 u32 old_state = sdev->dsp_power_state.state;
Liam Girdwood8920153c2019-04-12 11:05:16 -050090 int ret;
91
92 /* do nothing if dsp resume callbacks are not set */
Daniel Balutac26fde32020-05-15 16:59:51 +030093 if (!runtime_resume && !sof_ops(sdev)->resume)
94 return 0;
95
96 if (runtime_resume && !sof_ops(sdev)->runtime_resume)
Liam Girdwood8920153c2019-04-12 11:05:16 -050097 return 0;
98
Pierre-Louis Bossart410e5e52020-01-24 15:36:21 -060099 /* DSP was never successfully started, nothing to resume */
100 if (sdev->first_boot)
101 return 0;
102
Liam Girdwood8920153c2019-04-12 11:05:16 -0500103 /*
104 * if the runtime_resume flag is set, call the runtime_resume routine
105 * or else call the system resume routine
106 */
107 if (runtime_resume)
108 ret = snd_sof_dsp_runtime_resume(sdev);
109 else
110 ret = snd_sof_dsp_resume(sdev);
111 if (ret < 0) {
112 dev_err(sdev->dev,
113 "error: failed to power up DSP after resume\n");
114 return ret;
115 }
116
Ranjani Sridharanfc907cc2020-05-26 15:36:34 -0500117 /*
118 * Nothing further to be done for platforms that support the low power
119 * D0 substate.
120 */
121 if (!runtime_resume && sof_ops(sdev)->set_power_state &&
122 old_state == SOF_DSP_PM_D0)
Ranjani Sridharanfb9a8112020-01-29 16:07:19 -0600123 return 0;
124
Peter Ujfalusi58a5c9a2021-10-06 14:06:40 +0300125 sof_set_fw_state(sdev, SOF_FW_BOOT_PREPARE);
Ranjani Sridharan6ca5cec2019-12-17 18:26:09 -0600126
Liam Girdwood8920153c2019-04-12 11:05:16 -0500127 /* load the firmware */
128 ret = snd_sof_load_firmware(sdev);
129 if (ret < 0) {
130 dev_err(sdev->dev,
131 "error: failed to load DSP firmware after resume %d\n",
132 ret);
133 return ret;
134 }
135
Peter Ujfalusi58a5c9a2021-10-06 14:06:40 +0300136 sof_set_fw_state(sdev, SOF_FW_BOOT_IN_PROGRESS);
Ranjani Sridharan6ca5cec2019-12-17 18:26:09 -0600137
138 /*
139 * Boot the firmware. The FW boot status will be modified
140 * in snd_sof_run_firmware() depending on the outcome.
141 */
Liam Girdwood8920153c2019-04-12 11:05:16 -0500142 ret = snd_sof_run_firmware(sdev);
143 if (ret < 0) {
144 dev_err(sdev->dev,
145 "error: failed to boot DSP firmware after resume %d\n",
146 ret);
147 return ret;
148 }
149
150 /* resume DMA trace, only need send ipc */
151 ret = snd_sof_init_trace_ipc(sdev);
152 if (ret < 0) {
153 /* non fatal */
154 dev_warn(sdev->dev,
155 "warning: failed to init trace after resume %d\n",
156 ret);
157 }
158
159 /* restore pipelines */
Peter Ujfalusid8a15e52021-10-06 14:16:51 +0300160 ret = sof_set_up_pipelines(sdev, false);
Liam Girdwood8920153c2019-04-12 11:05:16 -0500161 if (ret < 0) {
162 dev_err(sdev->dev,
163 "error: failed to restore pipeline after resume %d\n",
164 ret);
165 return ret;
166 }
167
168 /* notify DSP of system resume */
Keyon Jie7c7eba22019-10-25 17:41:08 -0500169 ret = sof_send_pm_ctx_ipc(sdev, SOF_IPC_PM_CTX_RESTORE);
Liam Girdwood8920153c2019-04-12 11:05:16 -0500170 if (ret < 0)
171 dev_err(sdev->dev,
172 "error: ctx_restore ipc error during resume %d\n",
173 ret);
174
175 return ret;
176}
177
178static int sof_suspend(struct device *dev, bool runtime_suspend)
179{
180 struct snd_sof_dev *sdev = dev_get_drvdata(dev);
Ranjani Sridharan61e285c2020-01-29 16:07:22 -0600181 u32 target_state = 0;
Liam Girdwood8920153c2019-04-12 11:05:16 -0500182 int ret;
183
184 /* do nothing if dsp suspend callback is not set */
Daniel Balutac26fde32020-05-15 16:59:51 +0300185 if (!runtime_suspend && !sof_ops(sdev)->suspend)
186 return 0;
187
188 if (runtime_suspend && !sof_ops(sdev)->runtime_suspend)
Liam Girdwood8920153c2019-04-12 11:05:16 -0500189 return 0;
190
Ranjani Sridharan6ca5cec2019-12-17 18:26:09 -0600191 if (sdev->fw_state != SOF_FW_BOOT_COMPLETE)
Ranjani Sridharanfb9a8112020-01-29 16:07:19 -0600192 goto suspend;
Liam Girdwood8920153c2019-04-12 11:05:16 -0500193
Ranjani Sridharana1ce6e42021-09-28 10:40:30 +0300194 /* prepare for streams to be resumed properly upon resume */
Ranjani Sridharan7077a072019-06-12 12:23:38 -0500195 if (!runtime_suspend) {
Ranjani Sridharanee1e79b2019-12-04 15:15:51 -0600196 ret = sof_set_hw_params_upon_resume(sdev->dev);
Ranjani Sridharan7077a072019-06-12 12:23:38 -0500197 if (ret < 0) {
198 dev_err(sdev->dev,
199 "error: setting hw_params flag during suspend %d\n",
200 ret);
201 return ret;
202 }
203 }
Liam Girdwood8920153c2019-04-12 11:05:16 -0500204
Ranjani Sridharan61e285c2020-01-29 16:07:22 -0600205 target_state = snd_sof_dsp_power_target(sdev);
Ranjani Sridharanfb9a8112020-01-29 16:07:19 -0600206
Ranjani Sridharan61e285c2020-01-29 16:07:22 -0600207 /* Skip to platform-specific suspend if DSP is entering D0 */
208 if (target_state == SOF_DSP_PM_D0)
Ranjani Sridharanfb9a8112020-01-29 16:07:19 -0600209 goto suspend;
Ranjani Sridharanfb9a8112020-01-29 16:07:19 -0600210
Peter Ujfalusid8a15e52021-10-06 14:16:51 +0300211 sof_tear_down_pipelines(sdev, false);
Ranjani Sridharan0a2dea12021-09-27 15:05:11 +0300212
Ranjani Sridharanfb9a8112020-01-29 16:07:19 -0600213 /* release trace */
214 snd_sof_release_trace(sdev);
215
Liam Girdwood8920153c2019-04-12 11:05:16 -0500216#if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_ENABLE_DEBUGFS_CACHE)
217 /* cache debugfs contents during runtime suspend */
218 if (runtime_suspend)
219 sof_cache_debugfs(sdev);
220#endif
221 /* notify DSP of upcoming power down */
Keyon Jie7c7eba22019-10-25 17:41:08 -0500222 ret = sof_send_pm_ctx_ipc(sdev, SOF_IPC_PM_CTX_SAVE);
Kai Vehmanene2eba552019-06-12 11:57:04 -0500223 if (ret == -EBUSY || ret == -EAGAIN) {
224 /*
225 * runtime PM has logic to handle -EBUSY/-EAGAIN so
226 * pass these errors up
227 */
Liam Girdwood8920153c2019-04-12 11:05:16 -0500228 dev_err(sdev->dev,
229 "error: ctx_save ipc error during suspend %d\n",
230 ret);
231 return ret;
Kai Vehmanene2eba552019-06-12 11:57:04 -0500232 } else if (ret < 0) {
233 /* FW in unexpected state, continue to power down */
234 dev_warn(sdev->dev,
235 "ctx_save ipc error %d, proceeding with suspend\n",
236 ret);
Liam Girdwood8920153c2019-04-12 11:05:16 -0500237 }
238
Ranjani Sridharanfb9a8112020-01-29 16:07:19 -0600239suspend:
Ranjani Sridharan6ca5cec2019-12-17 18:26:09 -0600240
241 /* return if the DSP was not probed successfully */
242 if (sdev->fw_state == SOF_FW_BOOT_NOT_STARTED)
243 return 0;
244
Ranjani Sridharanfb9a8112020-01-29 16:07:19 -0600245 /* platform-specific suspend */
Liam Girdwood8920153c2019-04-12 11:05:16 -0500246 if (runtime_suspend)
Fred Oh1c38c922019-07-22 09:13:50 -0500247 ret = snd_sof_dsp_runtime_suspend(sdev);
Liam Girdwood8920153c2019-04-12 11:05:16 -0500248 else
Ranjani Sridharan61e285c2020-01-29 16:07:22 -0600249 ret = snd_sof_dsp_suspend(sdev, target_state);
Liam Girdwood8920153c2019-04-12 11:05:16 -0500250 if (ret < 0)
251 dev_err(sdev->dev,
252 "error: failed to power down DSP during suspend %d\n",
253 ret);
254
Ranjani Sridharan61e285c2020-01-29 16:07:22 -0600255 /* Do not reset FW state if DSP is in D0 */
256 if (target_state == SOF_DSP_PM_D0)
Ranjani Sridharanfb9a8112020-01-29 16:07:19 -0600257 return ret;
258
Ranjani Sridharan6ca5cec2019-12-17 18:26:09 -0600259 /* reset FW state */
Peter Ujfalusi58a5c9a2021-10-06 14:06:40 +0300260 sof_set_fw_state(sdev, SOF_FW_BOOT_NOT_STARTED);
Kai Vehmanenb640e8a2021-05-28 17:43:30 +0300261 sdev->enabled_cores_mask = 0;
Ranjani Sridharan6ca5cec2019-12-17 18:26:09 -0600262
Liam Girdwood8920153c2019-04-12 11:05:16 -0500263 return ret;
264}
265
Marcin Rajwa3541aef2020-05-15 16:59:52 +0300266int snd_sof_dsp_power_down_notify(struct snd_sof_dev *sdev)
267{
268 /* Notify DSP of upcoming power down */
269 if (sof_ops(sdev)->remove)
270 return sof_send_pm_ctx_ipc(sdev, SOF_IPC_PM_CTX_SAVE);
271
272 return 0;
273}
274
Liam Girdwood8920153c2019-04-12 11:05:16 -0500275int snd_sof_runtime_suspend(struct device *dev)
276{
277 return sof_suspend(dev, true);
278}
279EXPORT_SYMBOL(snd_sof_runtime_suspend);
280
Kai Vehmanen62fde972019-07-02 16:24:27 +0300281int snd_sof_runtime_idle(struct device *dev)
282{
283 struct snd_sof_dev *sdev = dev_get_drvdata(dev);
284
285 return snd_sof_dsp_runtime_idle(sdev);
286}
287EXPORT_SYMBOL(snd_sof_runtime_idle);
288
Liam Girdwood8920153c2019-04-12 11:05:16 -0500289int snd_sof_runtime_resume(struct device *dev)
290{
291 return sof_resume(dev, true);
292}
293EXPORT_SYMBOL(snd_sof_runtime_resume);
294
Liam Girdwood8920153c2019-04-12 11:05:16 -0500295int snd_sof_resume(struct device *dev)
296{
297 return sof_resume(dev, false);
298}
299EXPORT_SYMBOL(snd_sof_resume);
300
301int snd_sof_suspend(struct device *dev)
302{
303 return sof_suspend(dev, false);
304}
305EXPORT_SYMBOL(snd_sof_suspend);
Keyon Jie0b50b3b2019-10-25 17:41:17 -0500306
307int snd_sof_prepare(struct device *dev)
308{
309 struct snd_sof_dev *sdev = dev_get_drvdata(dev);
Daniel Baluta43437d02020-09-21 13:50:38 +0300310 const struct sof_dev_desc *desc = sdev->pdata->desc;
311
312 /* will suspend to S3 by default */
313 sdev->system_suspend_target = SOF_SUSPEND_S3;
314
315 if (!desc->use_acpi_target_states)
316 return 0;
Keyon Jie0b50b3b2019-10-25 17:41:17 -0500317
318#if defined(CONFIG_ACPI)
Ranjani Sridharan043ae132020-01-29 16:07:20 -0600319 if (acpi_target_system_state() == ACPI_STATE_S0)
320 sdev->system_suspend_target = SOF_SUSPEND_S0IX;
Keyon Jie0b50b3b2019-10-25 17:41:17 -0500321#endif
322
323 return 0;
324}
325EXPORT_SYMBOL(snd_sof_prepare);
326
327void snd_sof_complete(struct device *dev)
328{
329 struct snd_sof_dev *sdev = dev_get_drvdata(dev);
330
Ranjani Sridharan043ae132020-01-29 16:07:20 -0600331 sdev->system_suspend_target = SOF_SUSPEND_NONE;
Keyon Jie0b50b3b2019-10-25 17:41:17 -0500332}
333EXPORT_SYMBOL(snd_sof_complete);