1*4882a593Smuzhiyun // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
2*4882a593Smuzhiyun //
3*4882a593Smuzhiyun // This file is provided under a dual BSD/GPLv2 license. When using or
4*4882a593Smuzhiyun // redistributing this file, you may do so under either license.
5*4882a593Smuzhiyun //
6*4882a593Smuzhiyun // Copyright(c) 2018 Intel Corporation. All rights reserved.
7*4882a593Smuzhiyun //
8*4882a593Smuzhiyun // Author: Liam Girdwood <liam.r.girdwood@linux.intel.com>
9*4882a593Smuzhiyun //
10*4882a593Smuzhiyun
11*4882a593Smuzhiyun #include "ops.h"
12*4882a593Smuzhiyun #include "sof-priv.h"
13*4882a593Smuzhiyun #include "sof-audio.h"
14*4882a593Smuzhiyun
15*4882a593Smuzhiyun /*
16*4882a593Smuzhiyun * Helper function to determine the target DSP state during
17*4882a593Smuzhiyun * system suspend. This function only cares about the device
18*4882a593Smuzhiyun * D-states. Platform-specific substates, if any, should be
19*4882a593Smuzhiyun * handled by the platform-specific parts.
20*4882a593Smuzhiyun */
snd_sof_dsp_power_target(struct snd_sof_dev * sdev)21*4882a593Smuzhiyun static u32 snd_sof_dsp_power_target(struct snd_sof_dev *sdev)
22*4882a593Smuzhiyun {
23*4882a593Smuzhiyun u32 target_dsp_state;
24*4882a593Smuzhiyun
25*4882a593Smuzhiyun switch (sdev->system_suspend_target) {
26*4882a593Smuzhiyun case SOF_SUSPEND_S3:
27*4882a593Smuzhiyun /* DSP should be in D3 if the system is suspending to S3 */
28*4882a593Smuzhiyun target_dsp_state = SOF_DSP_PM_D3;
29*4882a593Smuzhiyun break;
30*4882a593Smuzhiyun case SOF_SUSPEND_S0IX:
31*4882a593Smuzhiyun /*
32*4882a593Smuzhiyun * Currently, the only criterion for retaining the DSP in D0
33*4882a593Smuzhiyun * is that there are streams that ignored the suspend trigger.
34*4882a593Smuzhiyun * Additional criteria such Soundwire clock-stop mode and
35*4882a593Smuzhiyun * device suspend latency considerations will be added later.
36*4882a593Smuzhiyun */
37*4882a593Smuzhiyun if (snd_sof_stream_suspend_ignored(sdev))
38*4882a593Smuzhiyun target_dsp_state = SOF_DSP_PM_D0;
39*4882a593Smuzhiyun else
40*4882a593Smuzhiyun target_dsp_state = SOF_DSP_PM_D3;
41*4882a593Smuzhiyun break;
42*4882a593Smuzhiyun default:
43*4882a593Smuzhiyun /* This case would be during runtime suspend */
44*4882a593Smuzhiyun target_dsp_state = SOF_DSP_PM_D3;
45*4882a593Smuzhiyun break;
46*4882a593Smuzhiyun }
47*4882a593Smuzhiyun
48*4882a593Smuzhiyun return target_dsp_state;
49*4882a593Smuzhiyun }
50*4882a593Smuzhiyun
sof_send_pm_ctx_ipc(struct snd_sof_dev * sdev,int cmd)51*4882a593Smuzhiyun static int sof_send_pm_ctx_ipc(struct snd_sof_dev *sdev, int cmd)
52*4882a593Smuzhiyun {
53*4882a593Smuzhiyun struct sof_ipc_pm_ctx pm_ctx;
54*4882a593Smuzhiyun struct sof_ipc_reply reply;
55*4882a593Smuzhiyun
56*4882a593Smuzhiyun memset(&pm_ctx, 0, sizeof(pm_ctx));
57*4882a593Smuzhiyun
58*4882a593Smuzhiyun /* configure ctx save ipc message */
59*4882a593Smuzhiyun pm_ctx.hdr.size = sizeof(pm_ctx);
60*4882a593Smuzhiyun pm_ctx.hdr.cmd = SOF_IPC_GLB_PM_MSG | cmd;
61*4882a593Smuzhiyun
62*4882a593Smuzhiyun /* send ctx save ipc to dsp */
63*4882a593Smuzhiyun return sof_ipc_tx_message(sdev->ipc, pm_ctx.hdr.cmd, &pm_ctx,
64*4882a593Smuzhiyun sizeof(pm_ctx), &reply, sizeof(reply));
65*4882a593Smuzhiyun }
66*4882a593Smuzhiyun
67*4882a593Smuzhiyun #if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_ENABLE_DEBUGFS_CACHE)
sof_cache_debugfs(struct snd_sof_dev * sdev)68*4882a593Smuzhiyun static void sof_cache_debugfs(struct snd_sof_dev *sdev)
69*4882a593Smuzhiyun {
70*4882a593Smuzhiyun struct snd_sof_dfsentry *dfse;
71*4882a593Smuzhiyun
72*4882a593Smuzhiyun list_for_each_entry(dfse, &sdev->dfsentry_list, list) {
73*4882a593Smuzhiyun
74*4882a593Smuzhiyun /* nothing to do if debugfs buffer is not IO mem */
75*4882a593Smuzhiyun if (dfse->type == SOF_DFSENTRY_TYPE_BUF)
76*4882a593Smuzhiyun continue;
77*4882a593Smuzhiyun
78*4882a593Smuzhiyun /* cache memory that is only accessible in D0 */
79*4882a593Smuzhiyun if (dfse->access_type == SOF_DEBUGFS_ACCESS_D0_ONLY)
80*4882a593Smuzhiyun memcpy_fromio(dfse->cache_buf, dfse->io_mem,
81*4882a593Smuzhiyun dfse->size);
82*4882a593Smuzhiyun }
83*4882a593Smuzhiyun }
84*4882a593Smuzhiyun #endif
85*4882a593Smuzhiyun
sof_resume(struct device * dev,bool runtime_resume)86*4882a593Smuzhiyun static int sof_resume(struct device *dev, bool runtime_resume)
87*4882a593Smuzhiyun {
88*4882a593Smuzhiyun struct snd_sof_dev *sdev = dev_get_drvdata(dev);
89*4882a593Smuzhiyun u32 old_state = sdev->dsp_power_state.state;
90*4882a593Smuzhiyun int ret;
91*4882a593Smuzhiyun
92*4882a593Smuzhiyun /* do nothing if dsp resume callbacks are not set */
93*4882a593Smuzhiyun if (!runtime_resume && !sof_ops(sdev)->resume)
94*4882a593Smuzhiyun return 0;
95*4882a593Smuzhiyun
96*4882a593Smuzhiyun if (runtime_resume && !sof_ops(sdev)->runtime_resume)
97*4882a593Smuzhiyun return 0;
98*4882a593Smuzhiyun
99*4882a593Smuzhiyun /* DSP was never successfully started, nothing to resume */
100*4882a593Smuzhiyun if (sdev->first_boot)
101*4882a593Smuzhiyun return 0;
102*4882a593Smuzhiyun
103*4882a593Smuzhiyun /*
104*4882a593Smuzhiyun * if the runtime_resume flag is set, call the runtime_resume routine
105*4882a593Smuzhiyun * or else call the system resume routine
106*4882a593Smuzhiyun */
107*4882a593Smuzhiyun if (runtime_resume)
108*4882a593Smuzhiyun ret = snd_sof_dsp_runtime_resume(sdev);
109*4882a593Smuzhiyun else
110*4882a593Smuzhiyun ret = snd_sof_dsp_resume(sdev);
111*4882a593Smuzhiyun if (ret < 0) {
112*4882a593Smuzhiyun dev_err(sdev->dev,
113*4882a593Smuzhiyun "error: failed to power up DSP after resume\n");
114*4882a593Smuzhiyun return ret;
115*4882a593Smuzhiyun }
116*4882a593Smuzhiyun
117*4882a593Smuzhiyun /*
118*4882a593Smuzhiyun * Nothing further to be done for platforms that support the low power
119*4882a593Smuzhiyun * D0 substate.
120*4882a593Smuzhiyun */
121*4882a593Smuzhiyun if (!runtime_resume && sof_ops(sdev)->set_power_state &&
122*4882a593Smuzhiyun old_state == SOF_DSP_PM_D0)
123*4882a593Smuzhiyun return 0;
124*4882a593Smuzhiyun
125*4882a593Smuzhiyun sdev->fw_state = SOF_FW_BOOT_PREPARE;
126*4882a593Smuzhiyun
127*4882a593Smuzhiyun /* load the firmware */
128*4882a593Smuzhiyun ret = snd_sof_load_firmware(sdev);
129*4882a593Smuzhiyun if (ret < 0) {
130*4882a593Smuzhiyun dev_err(sdev->dev,
131*4882a593Smuzhiyun "error: failed to load DSP firmware after resume %d\n",
132*4882a593Smuzhiyun ret);
133*4882a593Smuzhiyun return ret;
134*4882a593Smuzhiyun }
135*4882a593Smuzhiyun
136*4882a593Smuzhiyun sdev->fw_state = SOF_FW_BOOT_IN_PROGRESS;
137*4882a593Smuzhiyun
138*4882a593Smuzhiyun /*
139*4882a593Smuzhiyun * Boot the firmware. The FW boot status will be modified
140*4882a593Smuzhiyun * in snd_sof_run_firmware() depending on the outcome.
141*4882a593Smuzhiyun */
142*4882a593Smuzhiyun ret = snd_sof_run_firmware(sdev);
143*4882a593Smuzhiyun if (ret < 0) {
144*4882a593Smuzhiyun dev_err(sdev->dev,
145*4882a593Smuzhiyun "error: failed to boot DSP firmware after resume %d\n",
146*4882a593Smuzhiyun ret);
147*4882a593Smuzhiyun return ret;
148*4882a593Smuzhiyun }
149*4882a593Smuzhiyun
150*4882a593Smuzhiyun /* resume DMA trace, only need send ipc */
151*4882a593Smuzhiyun ret = snd_sof_init_trace_ipc(sdev);
152*4882a593Smuzhiyun if (ret < 0) {
153*4882a593Smuzhiyun /* non fatal */
154*4882a593Smuzhiyun dev_warn(sdev->dev,
155*4882a593Smuzhiyun "warning: failed to init trace after resume %d\n",
156*4882a593Smuzhiyun ret);
157*4882a593Smuzhiyun }
158*4882a593Smuzhiyun
159*4882a593Smuzhiyun /* restore pipelines */
160*4882a593Smuzhiyun ret = sof_restore_pipelines(sdev->dev);
161*4882a593Smuzhiyun if (ret < 0) {
162*4882a593Smuzhiyun dev_err(sdev->dev,
163*4882a593Smuzhiyun "error: failed to restore pipeline after resume %d\n",
164*4882a593Smuzhiyun ret);
165*4882a593Smuzhiyun return ret;
166*4882a593Smuzhiyun }
167*4882a593Smuzhiyun
168*4882a593Smuzhiyun /* notify DSP of system resume */
169*4882a593Smuzhiyun ret = sof_send_pm_ctx_ipc(sdev, SOF_IPC_PM_CTX_RESTORE);
170*4882a593Smuzhiyun if (ret < 0)
171*4882a593Smuzhiyun dev_err(sdev->dev,
172*4882a593Smuzhiyun "error: ctx_restore ipc error during resume %d\n",
173*4882a593Smuzhiyun ret);
174*4882a593Smuzhiyun
175*4882a593Smuzhiyun return ret;
176*4882a593Smuzhiyun }
177*4882a593Smuzhiyun
sof_suspend(struct device * dev,bool runtime_suspend)178*4882a593Smuzhiyun static int sof_suspend(struct device *dev, bool runtime_suspend)
179*4882a593Smuzhiyun {
180*4882a593Smuzhiyun struct snd_sof_dev *sdev = dev_get_drvdata(dev);
181*4882a593Smuzhiyun u32 target_state = 0;
182*4882a593Smuzhiyun int ret;
183*4882a593Smuzhiyun
184*4882a593Smuzhiyun /* do nothing if dsp suspend callback is not set */
185*4882a593Smuzhiyun if (!runtime_suspend && !sof_ops(sdev)->suspend)
186*4882a593Smuzhiyun return 0;
187*4882a593Smuzhiyun
188*4882a593Smuzhiyun if (runtime_suspend && !sof_ops(sdev)->runtime_suspend)
189*4882a593Smuzhiyun return 0;
190*4882a593Smuzhiyun
191*4882a593Smuzhiyun if (sdev->fw_state != SOF_FW_BOOT_COMPLETE)
192*4882a593Smuzhiyun goto suspend;
193*4882a593Smuzhiyun
194*4882a593Smuzhiyun /* set restore_stream for all streams during system suspend */
195*4882a593Smuzhiyun if (!runtime_suspend) {
196*4882a593Smuzhiyun ret = sof_set_hw_params_upon_resume(sdev->dev);
197*4882a593Smuzhiyun if (ret < 0) {
198*4882a593Smuzhiyun dev_err(sdev->dev,
199*4882a593Smuzhiyun "error: setting hw_params flag during suspend %d\n",
200*4882a593Smuzhiyun ret);
201*4882a593Smuzhiyun return ret;
202*4882a593Smuzhiyun }
203*4882a593Smuzhiyun }
204*4882a593Smuzhiyun
205*4882a593Smuzhiyun target_state = snd_sof_dsp_power_target(sdev);
206*4882a593Smuzhiyun
207*4882a593Smuzhiyun /* Skip to platform-specific suspend if DSP is entering D0 */
208*4882a593Smuzhiyun if (target_state == SOF_DSP_PM_D0)
209*4882a593Smuzhiyun goto suspend;
210*4882a593Smuzhiyun
211*4882a593Smuzhiyun /* release trace */
212*4882a593Smuzhiyun snd_sof_release_trace(sdev);
213*4882a593Smuzhiyun
214*4882a593Smuzhiyun #if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_ENABLE_DEBUGFS_CACHE)
215*4882a593Smuzhiyun /* cache debugfs contents during runtime suspend */
216*4882a593Smuzhiyun if (runtime_suspend)
217*4882a593Smuzhiyun sof_cache_debugfs(sdev);
218*4882a593Smuzhiyun #endif
219*4882a593Smuzhiyun /* notify DSP of upcoming power down */
220*4882a593Smuzhiyun ret = sof_send_pm_ctx_ipc(sdev, SOF_IPC_PM_CTX_SAVE);
221*4882a593Smuzhiyun if (ret == -EBUSY || ret == -EAGAIN) {
222*4882a593Smuzhiyun /*
223*4882a593Smuzhiyun * runtime PM has logic to handle -EBUSY/-EAGAIN so
224*4882a593Smuzhiyun * pass these errors up
225*4882a593Smuzhiyun */
226*4882a593Smuzhiyun dev_err(sdev->dev,
227*4882a593Smuzhiyun "error: ctx_save ipc error during suspend %d\n",
228*4882a593Smuzhiyun ret);
229*4882a593Smuzhiyun return ret;
230*4882a593Smuzhiyun } else if (ret < 0) {
231*4882a593Smuzhiyun /* FW in unexpected state, continue to power down */
232*4882a593Smuzhiyun dev_warn(sdev->dev,
233*4882a593Smuzhiyun "ctx_save ipc error %d, proceeding with suspend\n",
234*4882a593Smuzhiyun ret);
235*4882a593Smuzhiyun }
236*4882a593Smuzhiyun
237*4882a593Smuzhiyun suspend:
238*4882a593Smuzhiyun
239*4882a593Smuzhiyun /* return if the DSP was not probed successfully */
240*4882a593Smuzhiyun if (sdev->fw_state == SOF_FW_BOOT_NOT_STARTED)
241*4882a593Smuzhiyun return 0;
242*4882a593Smuzhiyun
243*4882a593Smuzhiyun /* platform-specific suspend */
244*4882a593Smuzhiyun if (runtime_suspend)
245*4882a593Smuzhiyun ret = snd_sof_dsp_runtime_suspend(sdev);
246*4882a593Smuzhiyun else
247*4882a593Smuzhiyun ret = snd_sof_dsp_suspend(sdev, target_state);
248*4882a593Smuzhiyun if (ret < 0)
249*4882a593Smuzhiyun dev_err(sdev->dev,
250*4882a593Smuzhiyun "error: failed to power down DSP during suspend %d\n",
251*4882a593Smuzhiyun ret);
252*4882a593Smuzhiyun
253*4882a593Smuzhiyun /* Do not reset FW state if DSP is in D0 */
254*4882a593Smuzhiyun if (target_state == SOF_DSP_PM_D0)
255*4882a593Smuzhiyun return ret;
256*4882a593Smuzhiyun
257*4882a593Smuzhiyun /* reset FW state */
258*4882a593Smuzhiyun sdev->fw_state = SOF_FW_BOOT_NOT_STARTED;
259*4882a593Smuzhiyun sdev->enabled_cores_mask = 0;
260*4882a593Smuzhiyun
261*4882a593Smuzhiyun return ret;
262*4882a593Smuzhiyun }
263*4882a593Smuzhiyun
snd_sof_dsp_power_down_notify(struct snd_sof_dev * sdev)264*4882a593Smuzhiyun int snd_sof_dsp_power_down_notify(struct snd_sof_dev *sdev)
265*4882a593Smuzhiyun {
266*4882a593Smuzhiyun /* Notify DSP of upcoming power down */
267*4882a593Smuzhiyun if (sof_ops(sdev)->remove)
268*4882a593Smuzhiyun return sof_send_pm_ctx_ipc(sdev, SOF_IPC_PM_CTX_SAVE);
269*4882a593Smuzhiyun
270*4882a593Smuzhiyun return 0;
271*4882a593Smuzhiyun }
272*4882a593Smuzhiyun
snd_sof_runtime_suspend(struct device * dev)273*4882a593Smuzhiyun int snd_sof_runtime_suspend(struct device *dev)
274*4882a593Smuzhiyun {
275*4882a593Smuzhiyun return sof_suspend(dev, true);
276*4882a593Smuzhiyun }
277*4882a593Smuzhiyun EXPORT_SYMBOL(snd_sof_runtime_suspend);
278*4882a593Smuzhiyun
snd_sof_runtime_idle(struct device * dev)279*4882a593Smuzhiyun int snd_sof_runtime_idle(struct device *dev)
280*4882a593Smuzhiyun {
281*4882a593Smuzhiyun struct snd_sof_dev *sdev = dev_get_drvdata(dev);
282*4882a593Smuzhiyun
283*4882a593Smuzhiyun return snd_sof_dsp_runtime_idle(sdev);
284*4882a593Smuzhiyun }
285*4882a593Smuzhiyun EXPORT_SYMBOL(snd_sof_runtime_idle);
286*4882a593Smuzhiyun
snd_sof_runtime_resume(struct device * dev)287*4882a593Smuzhiyun int snd_sof_runtime_resume(struct device *dev)
288*4882a593Smuzhiyun {
289*4882a593Smuzhiyun return sof_resume(dev, true);
290*4882a593Smuzhiyun }
291*4882a593Smuzhiyun EXPORT_SYMBOL(snd_sof_runtime_resume);
292*4882a593Smuzhiyun
snd_sof_resume(struct device * dev)293*4882a593Smuzhiyun int snd_sof_resume(struct device *dev)
294*4882a593Smuzhiyun {
295*4882a593Smuzhiyun return sof_resume(dev, false);
296*4882a593Smuzhiyun }
297*4882a593Smuzhiyun EXPORT_SYMBOL(snd_sof_resume);
298*4882a593Smuzhiyun
snd_sof_suspend(struct device * dev)299*4882a593Smuzhiyun int snd_sof_suspend(struct device *dev)
300*4882a593Smuzhiyun {
301*4882a593Smuzhiyun return sof_suspend(dev, false);
302*4882a593Smuzhiyun }
303*4882a593Smuzhiyun EXPORT_SYMBOL(snd_sof_suspend);
304*4882a593Smuzhiyun
snd_sof_prepare(struct device * dev)305*4882a593Smuzhiyun int snd_sof_prepare(struct device *dev)
306*4882a593Smuzhiyun {
307*4882a593Smuzhiyun struct snd_sof_dev *sdev = dev_get_drvdata(dev);
308*4882a593Smuzhiyun const struct sof_dev_desc *desc = sdev->pdata->desc;
309*4882a593Smuzhiyun
310*4882a593Smuzhiyun /* will suspend to S3 by default */
311*4882a593Smuzhiyun sdev->system_suspend_target = SOF_SUSPEND_S3;
312*4882a593Smuzhiyun
313*4882a593Smuzhiyun if (!desc->use_acpi_target_states)
314*4882a593Smuzhiyun return 0;
315*4882a593Smuzhiyun
316*4882a593Smuzhiyun #if defined(CONFIG_ACPI)
317*4882a593Smuzhiyun if (acpi_target_system_state() == ACPI_STATE_S0)
318*4882a593Smuzhiyun sdev->system_suspend_target = SOF_SUSPEND_S0IX;
319*4882a593Smuzhiyun #endif
320*4882a593Smuzhiyun
321*4882a593Smuzhiyun return 0;
322*4882a593Smuzhiyun }
323*4882a593Smuzhiyun EXPORT_SYMBOL(snd_sof_prepare);
324*4882a593Smuzhiyun
snd_sof_complete(struct device * dev)325*4882a593Smuzhiyun void snd_sof_complete(struct device *dev)
326*4882a593Smuzhiyun {
327*4882a593Smuzhiyun struct snd_sof_dev *sdev = dev_get_drvdata(dev);
328*4882a593Smuzhiyun
329*4882a593Smuzhiyun sdev->system_suspend_target = SOF_SUSPEND_NONE;
330*4882a593Smuzhiyun }
331*4882a593Smuzhiyun EXPORT_SYMBOL(snd_sof_complete);
332