1
2
3
4
5
6
7
8
9
10
11#include "ops.h"
12#include "sof-priv.h"
13#include "sof-audio.h"
14
15
16
17
18
19
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
28 target_dsp_state = SOF_DSP_PM_D3;
29 break;
30 case SOF_SUSPEND_S0IX:
31
32
33
34
35
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
44 target_dsp_state = SOF_DSP_PM_D3;
45 break;
46 }
47
48 return target_dsp_state;
49}
50
51static int sof_send_pm_ctx_ipc(struct snd_sof_dev *sdev, int cmd)
52{
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
59 pm_ctx.hdr.size = sizeof(pm_ctx);
60 pm_ctx.hdr.cmd = SOF_IPC_GLB_PM_MSG | cmd;
61
62
63 return sof_ipc_tx_message(sdev->ipc, pm_ctx.hdr.cmd, &pm_ctx,
64 sizeof(pm_ctx), &reply, sizeof(reply));
65}
66
67#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
75 if (dfse->type == SOF_DFSENTRY_TYPE_BUF)
76 continue;
77
78
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);
89 u32 old_state = sdev->dsp_power_state.state;
90 int ret;
91
92
93 if (!runtime_resume && !sof_ops(sdev)->resume)
94 return 0;
95
96 if (runtime_resume && !sof_ops(sdev)->runtime_resume)
97 return 0;
98
99
100 if (sdev->first_boot)
101 return 0;
102
103
104
105
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
117
118
119
120
121 if (!runtime_resume && sof_ops(sdev)->set_power_state &&
122 old_state == SOF_DSP_PM_D0)
123 return 0;
124
125 sdev->fw_state = SOF_FW_BOOT_PREPARE;
126
127
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
136 sdev->fw_state = SOF_FW_BOOT_IN_PROGRESS;
137
138
139
140
141
142 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
151 ret = snd_sof_init_trace_ipc(sdev);
152 if (ret < 0) {
153
154 dev_warn(sdev->dev,
155 "warning: failed to init trace after resume %d\n",
156 ret);
157 }
158
159
160 ret = sof_restore_pipelines(sdev->dev);
161 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
169 ret = sof_send_pm_ctx_ipc(sdev, SOF_IPC_PM_CTX_RESTORE);
170 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);
181 u32 target_state = 0;
182 int ret;
183
184
185 if (!runtime_suspend && !sof_ops(sdev)->suspend)
186 return 0;
187
188 if (runtime_suspend && !sof_ops(sdev)->runtime_suspend)
189 return 0;
190
191 if (sdev->fw_state != SOF_FW_BOOT_COMPLETE)
192 goto suspend;
193
194
195 if (!runtime_suspend) {
196 ret = sof_set_hw_params_upon_resume(sdev->dev);
197 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 }
204
205 target_state = snd_sof_dsp_power_target(sdev);
206
207
208 if (target_state == SOF_DSP_PM_D0)
209 goto suspend;
210
211
212 snd_sof_release_trace(sdev);
213
214#if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_ENABLE_DEBUGFS_CACHE)
215
216 if (runtime_suspend)
217 sof_cache_debugfs(sdev);
218#endif
219
220 ret = sof_send_pm_ctx_ipc(sdev, SOF_IPC_PM_CTX_SAVE);
221 if (ret == -EBUSY || ret == -EAGAIN) {
222
223
224
225
226 dev_err(sdev->dev,
227 "error: ctx_save ipc error during suspend %d\n",
228 ret);
229 return ret;
230 } else if (ret < 0) {
231
232 dev_warn(sdev->dev,
233 "ctx_save ipc error %d, proceeding with suspend\n",
234 ret);
235 }
236
237suspend:
238
239
240 if (sdev->fw_state == SOF_FW_BOOT_NOT_STARTED)
241 return 0;
242
243
244 if (runtime_suspend)
245 ret = snd_sof_dsp_runtime_suspend(sdev);
246 else
247 ret = snd_sof_dsp_suspend(sdev, target_state);
248 if (ret < 0)
249 dev_err(sdev->dev,
250 "error: failed to power down DSP during suspend %d\n",
251 ret);
252
253
254 if (target_state == SOF_DSP_PM_D0)
255 return ret;
256
257
258 sdev->fw_state = SOF_FW_BOOT_NOT_STARTED;
259
260 return ret;
261}
262
263int snd_sof_dsp_power_down_notify(struct snd_sof_dev *sdev)
264{
265
266 if (sof_ops(sdev)->remove)
267 return sof_send_pm_ctx_ipc(sdev, SOF_IPC_PM_CTX_SAVE);
268
269 return 0;
270}
271
272int snd_sof_runtime_suspend(struct device *dev)
273{
274 return sof_suspend(dev, true);
275}
276EXPORT_SYMBOL(snd_sof_runtime_suspend);
277
278int snd_sof_runtime_idle(struct device *dev)
279{
280 struct snd_sof_dev *sdev = dev_get_drvdata(dev);
281
282 return snd_sof_dsp_runtime_idle(sdev);
283}
284EXPORT_SYMBOL(snd_sof_runtime_idle);
285
286int snd_sof_runtime_resume(struct device *dev)
287{
288 return sof_resume(dev, true);
289}
290EXPORT_SYMBOL(snd_sof_runtime_resume);
291
292int snd_sof_resume(struct device *dev)
293{
294 return sof_resume(dev, false);
295}
296EXPORT_SYMBOL(snd_sof_resume);
297
298int snd_sof_suspend(struct device *dev)
299{
300 return sof_suspend(dev, false);
301}
302EXPORT_SYMBOL(snd_sof_suspend);
303
304int snd_sof_prepare(struct device *dev)
305{
306 struct snd_sof_dev *sdev = dev_get_drvdata(dev);
307
308#if defined(CONFIG_ACPI)
309 if (acpi_target_system_state() == ACPI_STATE_S0)
310 sdev->system_suspend_target = SOF_SUSPEND_S0IX;
311 else
312 sdev->system_suspend_target = SOF_SUSPEND_S3;
313#else
314
315 sdev->system_suspend_target = SOF_SUSPEND_S3;
316#endif
317
318 return 0;
319}
320EXPORT_SYMBOL(snd_sof_prepare);
321
322void snd_sof_complete(struct device *dev)
323{
324 struct snd_sof_dev *sdev = dev_get_drvdata(dev);
325
326 sdev->system_suspend_target = SOF_SUSPEND_NONE;
327}
328EXPORT_SYMBOL(snd_sof_complete);
329