ASoC: SOF: Add system_suspend_target field to struct snd_sof_dev
[linux-2.6-microblaze.git] / sound / soc / sof / pm.c
1 // 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"
13 #include "sof-audio.h"
14
15 static int sof_send_pm_ctx_ipc(struct snd_sof_dev *sdev, int cmd)
16 {
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
31 #if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_ENABLE_DEBUGFS_CACHE)
32 static 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
50 static int sof_resume(struct device *dev, bool runtime_resume)
51 {
52         struct snd_sof_dev *sdev = dev_get_drvdata(dev);
53         enum sof_d0_substate old_d0_substate = sdev->d0_substate;
54         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
60         /* DSP was never successfully started, nothing to resume */
61         if (sdev->first_boot)
62                 return 0;
63
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) {
68                         dev_err(sdev->dev,
69                                 "error: failed to resume from D0I3 %d\n",
70                                 ret);
71                         return ret;
72                 }
73         }
74
75         /*
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
89         /* Nothing further to do if resuming from D0I3 */
90         if (!runtime_resume && old_d0_substate == SOF_DSP_D0I3)
91                 return 0;
92
93         sdev->fw_state = SOF_FW_BOOT_PREPARE;
94
95         /* 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
104         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          */
110         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 */
128         ret = sof_restore_pipelines(sdev->dev);
129         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 */
137         ret = sof_send_pm_ctx_ipc(sdev, SOF_IPC_PM_CTX_RESTORE);
138         if (ret < 0)
139                 dev_err(sdev->dev,
140                         "error: ctx_restore ipc error during resume %d\n",
141                         ret);
142
143         /* initialize default D0 sub-state */
144         sdev->d0_substate = SOF_DSP_D0I0;
145
146         return ret;
147 }
148
149 static 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
158         if (sdev->fw_state != SOF_FW_BOOT_COMPLETE)
159                 goto suspend;
160
161         /* set restore_stream for all streams during system suspend */
162         if (!runtime_suspend) {
163                 ret = sof_set_hw_params_upon_resume(sdev->dev);
164                 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         }
171
172         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
188 #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 */
194         ret = sof_send_pm_ctx_ipc(sdev, SOF_IPC_PM_CTX_SAVE);
195         if (ret == -EBUSY || ret == -EAGAIN) {
196                 /*
197                  * runtime PM has logic to handle -EBUSY/-EAGAIN so
198                  * pass these errors up
199                  */
200                 dev_err(sdev->dev,
201                         "error: ctx_save ipc error during suspend %d\n",
202                         ret);
203                 return ret;
204         } 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);
209         }
210
211 suspend:
212
213         /* return if the DSP was not probed successfully */
214         if (sdev->fw_state == SOF_FW_BOOT_NOT_STARTED)
215                 return 0;
216
217         /* platform-specific suspend */
218         if (runtime_suspend)
219                 ret = snd_sof_dsp_runtime_suspend(sdev);
220         else
221                 ret = snd_sof_dsp_suspend(sdev);
222         if (ret < 0)
223                 dev_err(sdev->dev,
224                         "error: failed to power down DSP during suspend %d\n",
225                         ret);
226
227         /* Do not reset FW state if DSP is in D0I3 */
228         if (sdev->d0_substate == SOF_DSP_D0I3)
229                 return ret;
230
231         /* reset FW state */
232         sdev->fw_state = SOF_FW_BOOT_NOT_STARTED;
233
234         return ret;
235 }
236
237 int snd_sof_runtime_suspend(struct device *dev)
238 {
239         return sof_suspend(dev, true);
240 }
241 EXPORT_SYMBOL(snd_sof_runtime_suspend);
242
243 int 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 }
249 EXPORT_SYMBOL(snd_sof_runtime_idle);
250
251 int snd_sof_runtime_resume(struct device *dev)
252 {
253         return sof_resume(dev, true);
254 }
255 EXPORT_SYMBOL(snd_sof_runtime_resume);
256
257 int snd_sof_set_d0_substate(struct snd_sof_dev *sdev,
258                             enum sof_d0_substate d0_substate)
259 {
260         int ret;
261
262         if (sdev->d0_substate == d0_substate)
263                 return 0;
264
265         /* 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 }
275 EXPORT_SYMBOL(snd_sof_set_d0_substate);
276
277 /*
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
309 int snd_sof_resume(struct device *dev)
310 {
311         return sof_resume(dev, false);
312 }
313 EXPORT_SYMBOL(snd_sof_resume);
314
315 int snd_sof_suspend(struct device *dev)
316 {
317         return sof_suspend(dev, false);
318 }
319 EXPORT_SYMBOL(snd_sof_suspend);
320
321 int snd_sof_prepare(struct device *dev)
322 {
323         struct snd_sof_dev *sdev = dev_get_drvdata(dev);
324
325 #if defined(CONFIG_ACPI)
326         if (acpi_target_system_state() == ACPI_STATE_S0)
327                 sdev->system_suspend_target = SOF_SUSPEND_S0IX;
328         else
329                 sdev->system_suspend_target = SOF_SUSPEND_S3;
330 #else
331         /* will suspend to S3 by default */
332         sdev->system_suspend_target = SOF_SUSPEND_S3;
333 #endif
334
335         return 0;
336 }
337 EXPORT_SYMBOL(snd_sof_prepare);
338
339 void snd_sof_complete(struct device *dev)
340 {
341         struct snd_sof_dev *sdev = dev_get_drvdata(dev);
342
343         sdev->system_suspend_target = SOF_SUSPEND_NONE;
344 }
345 EXPORT_SYMBOL(snd_sof_complete);