• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: (GPL-2.0-only 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  */
snd_sof_dsp_power_target(struct snd_sof_dev * sdev)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_S5:
27 	case SOF_SUSPEND_S4:
28 		/* DSP should be in D3 if the system is suspending to S3+ */
29 	case SOF_SUSPEND_S3:
30 		/* DSP should be in D3 if the system is suspending to S3 */
31 		target_dsp_state = SOF_DSP_PM_D3;
32 		break;
33 	case SOF_SUSPEND_S0IX:
34 		/*
35 		 * Currently, the only criterion for retaining the DSP in D0
36 		 * is that there are streams that ignored the suspend trigger.
37 		 * Additional criteria such Soundwire clock-stop mode and
38 		 * device suspend latency considerations will be added later.
39 		 */
40 		if (snd_sof_stream_suspend_ignored(sdev))
41 			target_dsp_state = SOF_DSP_PM_D0;
42 		else
43 			target_dsp_state = SOF_DSP_PM_D3;
44 		break;
45 	default:
46 		/* This case would be during runtime suspend */
47 		target_dsp_state = SOF_DSP_PM_D3;
48 		break;
49 	}
50 
51 	return target_dsp_state;
52 }
53 
54 #if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_ENABLE_DEBUGFS_CACHE)
sof_cache_debugfs(struct snd_sof_dev * sdev)55 static void sof_cache_debugfs(struct snd_sof_dev *sdev)
56 {
57 	struct snd_sof_dfsentry *dfse;
58 
59 	list_for_each_entry(dfse, &sdev->dfsentry_list, list) {
60 
61 		/* nothing to do if debugfs buffer is not IO mem */
62 		if (dfse->type == SOF_DFSENTRY_TYPE_BUF)
63 			continue;
64 
65 		/* cache memory that is only accessible in D0 */
66 		if (dfse->access_type == SOF_DEBUGFS_ACCESS_D0_ONLY)
67 			memcpy_fromio(dfse->cache_buf, dfse->io_mem,
68 				      dfse->size);
69 	}
70 }
71 #endif
72 
sof_resume(struct device * dev,bool runtime_resume)73 static int sof_resume(struct device *dev, bool runtime_resume)
74 {
75 	struct snd_sof_dev *sdev = dev_get_drvdata(dev);
76 	const struct sof_ipc_pm_ops *pm_ops = sdev->ipc->ops->pm;
77 	const struct sof_ipc_tplg_ops *tplg_ops = sdev->ipc->ops->tplg;
78 	u32 old_state = sdev->dsp_power_state.state;
79 	int ret;
80 
81 	/* do nothing if dsp resume callbacks are not set */
82 	if (!runtime_resume && !sof_ops(sdev)->resume)
83 		return 0;
84 
85 	if (runtime_resume && !sof_ops(sdev)->runtime_resume)
86 		return 0;
87 
88 	/* DSP was never successfully started, nothing to resume */
89 	if (sdev->first_boot)
90 		return 0;
91 
92 	/*
93 	 * if the runtime_resume flag is set, call the runtime_resume routine
94 	 * or else call the system resume routine
95 	 */
96 	if (runtime_resume)
97 		ret = snd_sof_dsp_runtime_resume(sdev);
98 	else
99 		ret = snd_sof_dsp_resume(sdev);
100 	if (ret < 0) {
101 		dev_err(sdev->dev,
102 			"error: failed to power up DSP after resume\n");
103 		return ret;
104 	}
105 
106 	/*
107 	 * Nothing further to be done for platforms that support the low power
108 	 * D0 substate. Resume trace and return when resuming from
109 	 * low-power D0 substate
110 	 */
111 	if (!runtime_resume && sof_ops(sdev)->set_power_state &&
112 	    old_state == SOF_DSP_PM_D0) {
113 		ret = sof_fw_trace_resume(sdev);
114 		if (ret < 0)
115 			/* non fatal */
116 			dev_warn(sdev->dev,
117 				 "failed to enable trace after resume %d\n", ret);
118 		return 0;
119 	}
120 
121 	sof_set_fw_state(sdev, SOF_FW_BOOT_PREPARE);
122 
123 	/* load the firmware */
124 	ret = snd_sof_load_firmware(sdev);
125 	if (ret < 0) {
126 		dev_err(sdev->dev,
127 			"error: failed to load DSP firmware after resume %d\n",
128 			ret);
129 		sof_set_fw_state(sdev, SOF_FW_BOOT_FAILED);
130 		return ret;
131 	}
132 
133 	sof_set_fw_state(sdev, SOF_FW_BOOT_IN_PROGRESS);
134 
135 	/*
136 	 * Boot the firmware. The FW boot status will be modified
137 	 * in snd_sof_run_firmware() depending on the outcome.
138 	 */
139 	ret = snd_sof_run_firmware(sdev);
140 	if (ret < 0) {
141 		dev_err(sdev->dev,
142 			"error: failed to boot DSP firmware after resume %d\n",
143 			ret);
144 		sof_set_fw_state(sdev, SOF_FW_BOOT_FAILED);
145 		return ret;
146 	}
147 
148 	/* resume DMA trace */
149 	ret = sof_fw_trace_resume(sdev);
150 	if (ret < 0) {
151 		/* non fatal */
152 		dev_warn(sdev->dev,
153 			 "warning: failed to init trace after resume %d\n",
154 			 ret);
155 	}
156 
157 	/* restore pipelines */
158 	if (tplg_ops->set_up_all_pipelines) {
159 		ret = tplg_ops->set_up_all_pipelines(sdev, false);
160 		if (ret < 0) {
161 			dev_err(sdev->dev, "Failed to restore pipeline after resume %d\n", ret);
162 			goto setup_fail;
163 		}
164 	}
165 
166 	/* Notify clients not managed by pm framework about core resume */
167 	sof_resume_clients(sdev);
168 
169 	/* notify DSP of system resume */
170 	if (pm_ops && pm_ops->ctx_restore) {
171 		ret = pm_ops->ctx_restore(sdev);
172 		if (ret < 0)
173 			dev_err(sdev->dev, "ctx_restore IPC error during resume: %d\n", ret);
174 	}
175 
176 setup_fail:
177 #if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_ENABLE_DEBUGFS_CACHE)
178 	if (ret < 0) {
179 		/*
180 		 * Debugfs cannot be read in runtime suspend, so cache
181 		 * the contents upon failure. This allows to capture
182 		 * possible DSP coredump information.
183 		 */
184 		sof_cache_debugfs(sdev);
185 	}
186 #endif
187 
188 	return ret;
189 }
190 
sof_suspend(struct device * dev,bool runtime_suspend)191 static int sof_suspend(struct device *dev, bool runtime_suspend)
192 {
193 	struct snd_sof_dev *sdev = dev_get_drvdata(dev);
194 	const struct sof_ipc_pm_ops *pm_ops = sdev->ipc->ops->pm;
195 	const struct sof_ipc_tplg_ops *tplg_ops = sdev->ipc->ops->tplg;
196 	pm_message_t pm_state;
197 	u32 target_state = snd_sof_dsp_power_target(sdev);
198 	u32 old_state = sdev->dsp_power_state.state;
199 	int ret;
200 
201 	/* do nothing if dsp suspend callback is not set */
202 	if (!runtime_suspend && !sof_ops(sdev)->suspend)
203 		return 0;
204 
205 	if (runtime_suspend && !sof_ops(sdev)->runtime_suspend)
206 		return 0;
207 
208 	/* we need to tear down pipelines only if the DSP hardware is
209 	 * active, which happens for PCI devices. if the device is
210 	 * suspended, it is brought back to full power and then
211 	 * suspended again
212 	 */
213 	if (tplg_ops && tplg_ops->tear_down_all_pipelines && (old_state == SOF_DSP_PM_D0))
214 		tplg_ops->tear_down_all_pipelines(sdev, false);
215 
216 	if (sdev->fw_state != SOF_FW_BOOT_COMPLETE)
217 		goto suspend;
218 
219 	/* prepare for streams to be resumed properly upon resume */
220 	if (!runtime_suspend) {
221 		ret = snd_sof_dsp_hw_params_upon_resume(sdev);
222 		if (ret < 0) {
223 			dev_err(sdev->dev,
224 				"error: setting hw_params flag during suspend %d\n",
225 				ret);
226 			return ret;
227 		}
228 	}
229 
230 	pm_state.event = target_state;
231 
232 	/* Skip to platform-specific suspend if DSP is entering D0 */
233 	if (target_state == SOF_DSP_PM_D0) {
234 		sof_fw_trace_suspend(sdev, pm_state);
235 		/* Notify clients not managed by pm framework about core suspend */
236 		sof_suspend_clients(sdev, pm_state);
237 		goto suspend;
238 	}
239 
240 	/* suspend DMA trace */
241 	sof_fw_trace_suspend(sdev, pm_state);
242 
243 	/* Notify clients not managed by pm framework about core suspend */
244 	sof_suspend_clients(sdev, pm_state);
245 
246 #if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_ENABLE_DEBUGFS_CACHE)
247 	/* cache debugfs contents during runtime suspend */
248 	if (runtime_suspend)
249 		sof_cache_debugfs(sdev);
250 #endif
251 	/* notify DSP of upcoming power down */
252 	if (pm_ops && pm_ops->ctx_save) {
253 		ret = pm_ops->ctx_save(sdev);
254 		if (ret == -EBUSY || ret == -EAGAIN) {
255 			/*
256 			 * runtime PM has logic to handle -EBUSY/-EAGAIN so
257 			 * pass these errors up
258 			 */
259 			dev_err(sdev->dev, "ctx_save IPC error during suspend: %d\n", ret);
260 			return ret;
261 		} else if (ret < 0) {
262 			/* FW in unexpected state, continue to power down */
263 			dev_warn(sdev->dev, "ctx_save IPC error: %d, proceeding with suspend\n",
264 				 ret);
265 		}
266 	}
267 
268 suspend:
269 
270 	/* return if the DSP was not probed successfully */
271 	if (sdev->fw_state == SOF_FW_BOOT_NOT_STARTED)
272 		return 0;
273 
274 	/* platform-specific suspend */
275 	if (runtime_suspend)
276 		ret = snd_sof_dsp_runtime_suspend(sdev);
277 	else
278 		ret = snd_sof_dsp_suspend(sdev, target_state);
279 	if (ret < 0)
280 		dev_err(sdev->dev,
281 			"error: failed to power down DSP during suspend %d\n",
282 			ret);
283 
284 	/* Do not reset FW state if DSP is in D0 */
285 	if (target_state == SOF_DSP_PM_D0)
286 		return ret;
287 
288 	/* reset FW state */
289 	sof_set_fw_state(sdev, SOF_FW_BOOT_NOT_STARTED);
290 	sdev->enabled_cores_mask = 0;
291 
292 	return ret;
293 }
294 
snd_sof_dsp_power_down_notify(struct snd_sof_dev * sdev)295 int snd_sof_dsp_power_down_notify(struct snd_sof_dev *sdev)
296 {
297 	const struct sof_ipc_pm_ops *pm_ops = sdev->ipc->ops->pm;
298 
299 	/* Notify DSP of upcoming power down */
300 	if (sof_ops(sdev)->remove && pm_ops && pm_ops->ctx_save)
301 		return pm_ops->ctx_save(sdev);
302 
303 	return 0;
304 }
305 
snd_sof_runtime_suspend(struct device * dev)306 int snd_sof_runtime_suspend(struct device *dev)
307 {
308 	return sof_suspend(dev, true);
309 }
310 EXPORT_SYMBOL(snd_sof_runtime_suspend);
311 
snd_sof_runtime_idle(struct device * dev)312 int snd_sof_runtime_idle(struct device *dev)
313 {
314 	struct snd_sof_dev *sdev = dev_get_drvdata(dev);
315 
316 	return snd_sof_dsp_runtime_idle(sdev);
317 }
318 EXPORT_SYMBOL(snd_sof_runtime_idle);
319 
snd_sof_runtime_resume(struct device * dev)320 int snd_sof_runtime_resume(struct device *dev)
321 {
322 	return sof_resume(dev, true);
323 }
324 EXPORT_SYMBOL(snd_sof_runtime_resume);
325 
snd_sof_resume(struct device * dev)326 int snd_sof_resume(struct device *dev)
327 {
328 	return sof_resume(dev, false);
329 }
330 EXPORT_SYMBOL(snd_sof_resume);
331 
snd_sof_suspend(struct device * dev)332 int snd_sof_suspend(struct device *dev)
333 {
334 	return sof_suspend(dev, false);
335 }
336 EXPORT_SYMBOL(snd_sof_suspend);
337 
snd_sof_prepare(struct device * dev)338 int snd_sof_prepare(struct device *dev)
339 {
340 	struct snd_sof_dev *sdev = dev_get_drvdata(dev);
341 	const struct sof_dev_desc *desc = sdev->pdata->desc;
342 
343 	/* will suspend to S3 by default */
344 	sdev->system_suspend_target = SOF_SUSPEND_S3;
345 
346 	/*
347 	 * if the firmware is crashed or boot failed then we try to aim for S3
348 	 * to reboot the firmware
349 	 */
350 	if (sdev->fw_state == SOF_FW_CRASHED ||
351 	    sdev->fw_state == SOF_FW_BOOT_FAILED)
352 		return 0;
353 
354 	if (!desc->use_acpi_target_states)
355 		return 0;
356 
357 #if defined(CONFIG_ACPI)
358 	switch (acpi_target_system_state()) {
359 	case ACPI_STATE_S0:
360 		sdev->system_suspend_target = SOF_SUSPEND_S0IX;
361 		break;
362 	case ACPI_STATE_S1:
363 	case ACPI_STATE_S2:
364 	case ACPI_STATE_S3:
365 		sdev->system_suspend_target = SOF_SUSPEND_S3;
366 		break;
367 	case ACPI_STATE_S4:
368 		sdev->system_suspend_target = SOF_SUSPEND_S4;
369 		break;
370 	case ACPI_STATE_S5:
371 		sdev->system_suspend_target = SOF_SUSPEND_S5;
372 		break;
373 	default:
374 		break;
375 	}
376 #endif
377 
378 	return 0;
379 }
380 EXPORT_SYMBOL(snd_sof_prepare);
381 
snd_sof_complete(struct device * dev)382 void snd_sof_complete(struct device *dev)
383 {
384 	struct snd_sof_dev *sdev = dev_get_drvdata(dev);
385 
386 	sdev->system_suspend_target = SOF_SUSPEND_NONE;
387 }
388 EXPORT_SYMBOL(snd_sof_complete);
389