blob: 5b186bceedb91084cb96a3757eb303b730097ca0 [file] [log] [blame]
Liam Girdwood8920153c2019-04-12 11:05:16 -05001// SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
2//
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
Keyon Jie7c7eba22019-10-25 17:41:08 -050015static int sof_send_pm_ctx_ipc(struct snd_sof_dev *sdev, int cmd)
Liam Girdwood8920153c2019-04-12 11:05:16 -050016{
17 struct sof_ipc_pm_ctx pm_ctx;
18 struct sof_ipc_reply reply;
19
20 memset(&pm_ctx, 0, sizeof(pm_ctx));
21
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;
25
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));
29}
30
Liam Girdwood8920153c2019-04-12 11:05:16 -050031#if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_ENABLE_DEBUGFS_CACHE)
32static void sof_cache_debugfs(struct snd_sof_dev *sdev)
33{
34 struct snd_sof_dfsentry *dfse;
35
36 list_for_each_entry(dfse, &sdev->dfsentry_list, list) {
37
38 /* nothing to do if debugfs buffer is not IO mem */
39 if (dfse->type == SOF_DFSENTRY_TYPE_BUF)
40 continue;
41
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,
45 dfse->size);
46 }
47}
48#endif
49
50static int sof_resume(struct device *dev, bool runtime_resume)
51{
52 struct snd_sof_dev *sdev = dev_get_drvdata(dev);
Ranjani Sridharanfb9a8112020-01-29 16:07:19 -060053 enum sof_d0_substate old_d0_substate = sdev->d0_substate;
Liam Girdwood8920153c2019-04-12 11:05:16 -050054 int ret;
55
56 /* do nothing if dsp resume callbacks are not set */
57 if (!sof_ops(sdev)->resume || !sof_ops(sdev)->runtime_resume)
58 return 0;
59
Pierre-Louis Bossart410e5e52020-01-24 15:36:21 -060060 /* DSP was never successfully started, nothing to resume */
61 if (sdev->first_boot)
62 return 0;
63
Ranjani Sridharanfb9a8112020-01-29 16:07:19 -060064 /* 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) {
68 dev_err(sdev->dev,
69 "error: failed to resume from D0I3 %d\n",
70 ret);
71 return ret;
72 }
73 }
74
Liam Girdwood8920153c2019-04-12 11:05:16 -050075 /*
76 * if the runtime_resume flag is set, call the runtime_resume routine
77 * or else call the system resume routine
78 */
79 if (runtime_resume)
80 ret = snd_sof_dsp_runtime_resume(sdev);
81 else
82 ret = snd_sof_dsp_resume(sdev);
83 if (ret < 0) {
84 dev_err(sdev->dev,
85 "error: failed to power up DSP after resume\n");
86 return ret;
87 }
88
Ranjani Sridharanfb9a8112020-01-29 16:07:19 -060089 /* Nothing further to do if resuming from D0I3 */
90 if (!runtime_resume && old_d0_substate == SOF_DSP_D0I3)
91 return 0;
92
Ranjani Sridharan6ca5cec2019-12-17 18:26:09 -060093 sdev->fw_state = SOF_FW_BOOT_PREPARE;
94
Liam Girdwood8920153c2019-04-12 11:05:16 -050095 /* load the firmware */
96 ret = snd_sof_load_firmware(sdev);
97 if (ret < 0) {
98 dev_err(sdev->dev,
99 "error: failed to load DSP firmware after resume %d\n",
100 ret);
101 return ret;
102 }
103
Ranjani Sridharan6ca5cec2019-12-17 18:26:09 -0600104 sdev->fw_state = SOF_FW_BOOT_IN_PROGRESS;
105
106 /*
107 * Boot the firmware. The FW boot status will be modified
108 * in snd_sof_run_firmware() depending on the outcome.
109 */
Liam Girdwood8920153c2019-04-12 11:05:16 -0500110 ret = snd_sof_run_firmware(sdev);
111 if (ret < 0) {
112 dev_err(sdev->dev,
113 "error: failed to boot DSP firmware after resume %d\n",
114 ret);
115 return ret;
116 }
117
118 /* resume DMA trace, only need send ipc */
119 ret = snd_sof_init_trace_ipc(sdev);
120 if (ret < 0) {
121 /* non fatal */
122 dev_warn(sdev->dev,
123 "warning: failed to init trace after resume %d\n",
124 ret);
125 }
126
127 /* restore pipelines */
Ranjani Sridharanee1e79b2019-12-04 15:15:51 -0600128 ret = sof_restore_pipelines(sdev->dev);
Liam Girdwood8920153c2019-04-12 11:05:16 -0500129 if (ret < 0) {
130 dev_err(sdev->dev,
131 "error: failed to restore pipeline after resume %d\n",
132 ret);
133 return ret;
134 }
135
136 /* notify DSP of system resume */
Keyon Jie7c7eba22019-10-25 17:41:08 -0500137 ret = sof_send_pm_ctx_ipc(sdev, SOF_IPC_PM_CTX_RESTORE);
Liam Girdwood8920153c2019-04-12 11:05:16 -0500138 if (ret < 0)
139 dev_err(sdev->dev,
140 "error: ctx_restore ipc error during resume %d\n",
141 ret);
142
Keyon Jie09fe6b52019-10-25 17:40:58 -0500143 /* initialize default D0 sub-state */
144 sdev->d0_substate = SOF_DSP_D0I0;
145
Liam Girdwood8920153c2019-04-12 11:05:16 -0500146 return ret;
147}
148
149static int sof_suspend(struct device *dev, bool runtime_suspend)
150{
151 struct snd_sof_dev *sdev = dev_get_drvdata(dev);
152 int ret;
153
154 /* do nothing if dsp suspend callback is not set */
155 if (!sof_ops(sdev)->suspend)
156 return 0;
157
Ranjani Sridharan6ca5cec2019-12-17 18:26:09 -0600158 if (sdev->fw_state != SOF_FW_BOOT_COMPLETE)
Ranjani Sridharanfb9a8112020-01-29 16:07:19 -0600159 goto suspend;
Liam Girdwood8920153c2019-04-12 11:05:16 -0500160
161 /* set restore_stream for all streams during system suspend */
Ranjani Sridharan7077a072019-06-12 12:23:38 -0500162 if (!runtime_suspend) {
Ranjani Sridharanee1e79b2019-12-04 15:15:51 -0600163 ret = sof_set_hw_params_upon_resume(sdev->dev);
Ranjani Sridharan7077a072019-06-12 12:23:38 -0500164 if (ret < 0) {
165 dev_err(sdev->dev,
166 "error: setting hw_params flag during suspend %d\n",
167 ret);
168 return ret;
169 }
170 }
Liam Girdwood8920153c2019-04-12 11:05:16 -0500171
Ranjani Sridharanfb9a8112020-01-29 16:07:19 -0600172 if (snd_sof_dsp_d0i3_on_suspend(sdev)) {
173 /* suspend to D0i3 */
174 ret = snd_sof_set_d0_substate(sdev, SOF_DSP_D0I3);
175 if (ret < 0) {
176 dev_err(sdev->dev, "error: failed to enter D0I3, %d\n",
177 ret);
178 return ret;
179 }
180
181 /* Skip to platform-specific suspend if DSP is entering D0I3 */
182 goto suspend;
183 }
184
185 /* release trace */
186 snd_sof_release_trace(sdev);
187
Liam Girdwood8920153c2019-04-12 11:05:16 -0500188#if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_ENABLE_DEBUGFS_CACHE)
189 /* cache debugfs contents during runtime suspend */
190 if (runtime_suspend)
191 sof_cache_debugfs(sdev);
192#endif
193 /* notify DSP of upcoming power down */
Keyon Jie7c7eba22019-10-25 17:41:08 -0500194 ret = sof_send_pm_ctx_ipc(sdev, SOF_IPC_PM_CTX_SAVE);
Kai Vehmanene2eba552019-06-12 11:57:04 -0500195 if (ret == -EBUSY || ret == -EAGAIN) {
196 /*
197 * runtime PM has logic to handle -EBUSY/-EAGAIN so
198 * pass these errors up
199 */
Liam Girdwood8920153c2019-04-12 11:05:16 -0500200 dev_err(sdev->dev,
201 "error: ctx_save ipc error during suspend %d\n",
202 ret);
203 return ret;
Kai Vehmanene2eba552019-06-12 11:57:04 -0500204 } else if (ret < 0) {
205 /* FW in unexpected state, continue to power down */
206 dev_warn(sdev->dev,
207 "ctx_save ipc error %d, proceeding with suspend\n",
208 ret);
Liam Girdwood8920153c2019-04-12 11:05:16 -0500209 }
210
Ranjani Sridharanfb9a8112020-01-29 16:07:19 -0600211suspend:
Ranjani Sridharan6ca5cec2019-12-17 18:26:09 -0600212
213 /* return if the DSP was not probed successfully */
214 if (sdev->fw_state == SOF_FW_BOOT_NOT_STARTED)
215 return 0;
216
Ranjani Sridharanfb9a8112020-01-29 16:07:19 -0600217 /* platform-specific suspend */
Liam Girdwood8920153c2019-04-12 11:05:16 -0500218 if (runtime_suspend)
Fred Oh1c38c922019-07-22 09:13:50 -0500219 ret = snd_sof_dsp_runtime_suspend(sdev);
Liam Girdwood8920153c2019-04-12 11:05:16 -0500220 else
Fred Oh1c38c922019-07-22 09:13:50 -0500221 ret = snd_sof_dsp_suspend(sdev);
Liam Girdwood8920153c2019-04-12 11:05:16 -0500222 if (ret < 0)
223 dev_err(sdev->dev,
224 "error: failed to power down DSP during suspend %d\n",
225 ret);
226
Ranjani Sridharanfb9a8112020-01-29 16:07:19 -0600227 /* Do not reset FW state if DSP is in D0I3 */
228 if (sdev->d0_substate == SOF_DSP_D0I3)
229 return ret;
230
Ranjani Sridharan6ca5cec2019-12-17 18:26:09 -0600231 /* reset FW state */
232 sdev->fw_state = SOF_FW_BOOT_NOT_STARTED;
233
Liam Girdwood8920153c2019-04-12 11:05:16 -0500234 return ret;
235}
236
237int snd_sof_runtime_suspend(struct device *dev)
238{
239 return sof_suspend(dev, true);
240}
241EXPORT_SYMBOL(snd_sof_runtime_suspend);
242
Kai Vehmanen62fde972019-07-02 16:24:27 +0300243int snd_sof_runtime_idle(struct device *dev)
244{
245 struct snd_sof_dev *sdev = dev_get_drvdata(dev);
246
247 return snd_sof_dsp_runtime_idle(sdev);
248}
249EXPORT_SYMBOL(snd_sof_runtime_idle);
250
Liam Girdwood8920153c2019-04-12 11:05:16 -0500251int snd_sof_runtime_resume(struct device *dev)
252{
253 return sof_resume(dev, true);
254}
255EXPORT_SYMBOL(snd_sof_runtime_resume);
256
Keyon Jie60125282019-10-25 17:41:13 -0500257int snd_sof_set_d0_substate(struct snd_sof_dev *sdev,
258 enum sof_d0_substate d0_substate)
259{
260 int ret;
261
Keyon Jie58a972e2019-11-11 16:33:41 -0600262 if (sdev->d0_substate == d0_substate)
263 return 0;
264
Keyon Jie60125282019-10-25 17:41:13 -0500265 /* do platform specific set_state */
266 ret = snd_sof_dsp_set_power_state(sdev, d0_substate);
267 if (ret < 0)
268 return ret;
269
270 /* update dsp D0 sub-state */
271 sdev->d0_substate = d0_substate;
272
273 return 0;
274}
275EXPORT_SYMBOL(snd_sof_set_d0_substate);
276
Keyon Jie74b4dd02019-11-11 16:33:40 -0600277/*
278 * Audio DSP states may transform as below:-
279 *
280 * D0I3 compatible stream
281 * Runtime +---------------------+ opened only, timeout
282 * suspend | +--------------------+
283 * +------------+ D0(active) | |
284 * | | <---------------+ |
285 * | +--------> | | |
286 * | |Runtime +--^--+---------^--+--+ The last | |
287 * | |resume | | | | opened D0I3 | |
288 * | | | | | | compatible | |
289 * | | resume| | | | stream closed | |
290 * | | from | | D3 | | | |
291 * | | D3 | |suspend | | d0i3 | |
292 * | | | | | |suspend | |
293 * | | | | | | | |
294 * | | | | | | | |
295 * +-v---+-----------+--v-------+ | | +------+----v----+
296 * | | | +-----------> |
297 * | D3 (suspended) | | | D0I3 +-----+
298 * | | +--------------+ | |
299 * | | resume from | | |
300 * +-------------------^--------+ d0i3 suspend +----------------+ |
301 * | |
302 * | D3 suspend |
303 * +------------------------------------------------+
304 *
305 * d0i3_suspend = s0_suspend && D0I3 stream opened,
306 * D3 suspend = !d0i3_suspend,
307 */
308
Liam Girdwood8920153c2019-04-12 11:05:16 -0500309int snd_sof_resume(struct device *dev)
310{
311 return sof_resume(dev, false);
312}
313EXPORT_SYMBOL(snd_sof_resume);
314
315int snd_sof_suspend(struct device *dev)
316{
317 return sof_suspend(dev, false);
318}
319EXPORT_SYMBOL(snd_sof_suspend);
Keyon Jie0b50b3b2019-10-25 17:41:17 -0500320
321int snd_sof_prepare(struct device *dev)
322{
323 struct snd_sof_dev *sdev = dev_get_drvdata(dev);
324
325#if defined(CONFIG_ACPI)
326 sdev->s0_suspend = acpi_target_system_state() == ACPI_STATE_S0;
327#else
328 /* will suspend to S3 by default */
329 sdev->s0_suspend = false;
330#endif
331
332 return 0;
333}
334EXPORT_SYMBOL(snd_sof_prepare);
335
336void snd_sof_complete(struct device *dev)
337{
338 struct snd_sof_dev *sdev = dev_get_drvdata(dev);
339
340 sdev->s0_suspend = false;
341}
342EXPORT_SYMBOL(snd_sof_complete);