xref: /openbmc/linux/sound/soc/sof/pm.c (revision 06b72824)
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 /*
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  */
21 static 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 
51 static 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 	/* 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 
67 #if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_ENABLE_DEBUGFS_CACHE)
68 static 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 
86 static 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 	/* do nothing if dsp resume callbacks are not set */
93 	if (!sof_ops(sdev)->resume || !sof_ops(sdev)->runtime_resume)
94 		return 0;
95 
96 	/* DSP was never successfully started, nothing to resume */
97 	if (sdev->first_boot)
98 		return 0;
99 
100 	/*
101 	 * if the runtime_resume flag is set, call the runtime_resume routine
102 	 * or else call the system resume routine
103 	 */
104 	if (runtime_resume)
105 		ret = snd_sof_dsp_runtime_resume(sdev);
106 	else
107 		ret = snd_sof_dsp_resume(sdev);
108 	if (ret < 0) {
109 		dev_err(sdev->dev,
110 			"error: failed to power up DSP after resume\n");
111 		return ret;
112 	}
113 
114 	/* Nothing further to do if resuming from a low-power D0 substate */
115 	if (!runtime_resume && old_state == SOF_DSP_PM_D0)
116 		return 0;
117 
118 	sdev->fw_state = SOF_FW_BOOT_PREPARE;
119 
120 	/* load the firmware */
121 	ret = snd_sof_load_firmware(sdev);
122 	if (ret < 0) {
123 		dev_err(sdev->dev,
124 			"error: failed to load DSP firmware after resume %d\n",
125 			ret);
126 		return ret;
127 	}
128 
129 	sdev->fw_state = SOF_FW_BOOT_IN_PROGRESS;
130 
131 	/*
132 	 * Boot the firmware. The FW boot status will be modified
133 	 * in snd_sof_run_firmware() depending on the outcome.
134 	 */
135 	ret = snd_sof_run_firmware(sdev);
136 	if (ret < 0) {
137 		dev_err(sdev->dev,
138 			"error: failed to boot DSP firmware after resume %d\n",
139 			ret);
140 		return ret;
141 	}
142 
143 	/* resume DMA trace, only need send ipc */
144 	ret = snd_sof_init_trace_ipc(sdev);
145 	if (ret < 0) {
146 		/* non fatal */
147 		dev_warn(sdev->dev,
148 			 "warning: failed to init trace after resume %d\n",
149 			 ret);
150 	}
151 
152 	/* restore pipelines */
153 	ret = sof_restore_pipelines(sdev->dev);
154 	if (ret < 0) {
155 		dev_err(sdev->dev,
156 			"error: failed to restore pipeline after resume %d\n",
157 			ret);
158 		return ret;
159 	}
160 
161 	/* notify DSP of system resume */
162 	ret = sof_send_pm_ctx_ipc(sdev, SOF_IPC_PM_CTX_RESTORE);
163 	if (ret < 0)
164 		dev_err(sdev->dev,
165 			"error: ctx_restore ipc error during resume %d\n",
166 			ret);
167 
168 	return ret;
169 }
170 
171 static int sof_suspend(struct device *dev, bool runtime_suspend)
172 {
173 	struct snd_sof_dev *sdev = dev_get_drvdata(dev);
174 	u32 target_state = 0;
175 	int ret;
176 
177 	/* do nothing if dsp suspend callback is not set */
178 	if (!sof_ops(sdev)->suspend)
179 		return 0;
180 
181 	if (sdev->fw_state != SOF_FW_BOOT_COMPLETE)
182 		goto suspend;
183 
184 	/* set restore_stream for all streams during system suspend */
185 	if (!runtime_suspend) {
186 		ret = sof_set_hw_params_upon_resume(sdev->dev);
187 		if (ret < 0) {
188 			dev_err(sdev->dev,
189 				"error: setting hw_params flag during suspend %d\n",
190 				ret);
191 			return ret;
192 		}
193 	}
194 
195 	target_state = snd_sof_dsp_power_target(sdev);
196 
197 	/* Skip to platform-specific suspend if DSP is entering D0 */
198 	if (target_state == SOF_DSP_PM_D0)
199 		goto suspend;
200 
201 	/* release trace */
202 	snd_sof_release_trace(sdev);
203 
204 #if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_ENABLE_DEBUGFS_CACHE)
205 	/* cache debugfs contents during runtime suspend */
206 	if (runtime_suspend)
207 		sof_cache_debugfs(sdev);
208 #endif
209 	/* notify DSP of upcoming power down */
210 	ret = sof_send_pm_ctx_ipc(sdev, SOF_IPC_PM_CTX_SAVE);
211 	if (ret == -EBUSY || ret == -EAGAIN) {
212 		/*
213 		 * runtime PM has logic to handle -EBUSY/-EAGAIN so
214 		 * pass these errors up
215 		 */
216 		dev_err(sdev->dev,
217 			"error: ctx_save ipc error during suspend %d\n",
218 			ret);
219 		return ret;
220 	} else if (ret < 0) {
221 		/* FW in unexpected state, continue to power down */
222 		dev_warn(sdev->dev,
223 			 "ctx_save ipc error %d, proceeding with suspend\n",
224 			 ret);
225 	}
226 
227 suspend:
228 
229 	/* return if the DSP was not probed successfully */
230 	if (sdev->fw_state == SOF_FW_BOOT_NOT_STARTED)
231 		return 0;
232 
233 	/* platform-specific suspend */
234 	if (runtime_suspend)
235 		ret = snd_sof_dsp_runtime_suspend(sdev);
236 	else
237 		ret = snd_sof_dsp_suspend(sdev, target_state);
238 	if (ret < 0)
239 		dev_err(sdev->dev,
240 			"error: failed to power down DSP during suspend %d\n",
241 			ret);
242 
243 	/* Do not reset FW state if DSP is in D0 */
244 	if (target_state == SOF_DSP_PM_D0)
245 		return ret;
246 
247 	/* reset FW state */
248 	sdev->fw_state = SOF_FW_BOOT_NOT_STARTED;
249 
250 	return ret;
251 }
252 
253 int snd_sof_runtime_suspend(struct device *dev)
254 {
255 	return sof_suspend(dev, true);
256 }
257 EXPORT_SYMBOL(snd_sof_runtime_suspend);
258 
259 int snd_sof_runtime_idle(struct device *dev)
260 {
261 	struct snd_sof_dev *sdev = dev_get_drvdata(dev);
262 
263 	return snd_sof_dsp_runtime_idle(sdev);
264 }
265 EXPORT_SYMBOL(snd_sof_runtime_idle);
266 
267 int snd_sof_runtime_resume(struct device *dev)
268 {
269 	return sof_resume(dev, true);
270 }
271 EXPORT_SYMBOL(snd_sof_runtime_resume);
272 
273 int snd_sof_resume(struct device *dev)
274 {
275 	return sof_resume(dev, false);
276 }
277 EXPORT_SYMBOL(snd_sof_resume);
278 
279 int snd_sof_suspend(struct device *dev)
280 {
281 	return sof_suspend(dev, false);
282 }
283 EXPORT_SYMBOL(snd_sof_suspend);
284 
285 int snd_sof_prepare(struct device *dev)
286 {
287 	struct snd_sof_dev *sdev = dev_get_drvdata(dev);
288 
289 #if defined(CONFIG_ACPI)
290 	if (acpi_target_system_state() == ACPI_STATE_S0)
291 		sdev->system_suspend_target = SOF_SUSPEND_S0IX;
292 	else
293 		sdev->system_suspend_target = SOF_SUSPEND_S3;
294 #else
295 	/* will suspend to S3 by default */
296 	sdev->system_suspend_target = SOF_SUSPEND_S3;
297 #endif
298 
299 	return 0;
300 }
301 EXPORT_SYMBOL(snd_sof_prepare);
302 
303 void snd_sof_complete(struct device *dev)
304 {
305 	struct snd_sof_dev *sdev = dev_get_drvdata(dev);
306 
307 	sdev->system_suspend_target = SOF_SUSPEND_NONE;
308 }
309 EXPORT_SYMBOL(snd_sof_complete);
310