xref: /openbmc/linux/sound/soc/sof/pm.c (revision 94d964e5)
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  */
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 (!runtime_resume && !sof_ops(sdev)->resume)
94 		return 0;
95 
96 	if (runtime_resume && !sof_ops(sdev)->runtime_resume)
97 		return 0;
98 
99 	/* DSP was never successfully started, nothing to resume */
100 	if (sdev->first_boot)
101 		return 0;
102 
103 	/*
104 	 * if the runtime_resume flag is set, call the runtime_resume routine
105 	 * or else call the system resume routine
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 	 * Nothing further to be done for platforms that support the low power
119 	 * D0 substate.
120 	 */
121 	if (!runtime_resume && sof_ops(sdev)->set_power_state &&
122 	    old_state == SOF_DSP_PM_D0)
123 		return 0;
124 
125 	sof_set_fw_state(sdev, SOF_FW_BOOT_PREPARE);
126 
127 	/* load the firmware */
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 		sof_set_fw_state(sdev, SOF_FW_BOOT_FAILED);
134 		return ret;
135 	}
136 
137 	sof_set_fw_state(sdev, SOF_FW_BOOT_IN_PROGRESS);
138 
139 	/*
140 	 * Boot the firmware. The FW boot status will be modified
141 	 * in snd_sof_run_firmware() depending on the outcome.
142 	 */
143 	ret = snd_sof_run_firmware(sdev);
144 	if (ret < 0) {
145 		dev_err(sdev->dev,
146 			"error: failed to boot DSP firmware after resume %d\n",
147 			ret);
148 		sof_set_fw_state(sdev, SOF_FW_BOOT_FAILED);
149 		return ret;
150 	}
151 
152 	/* resume DMA trace, only need send ipc */
153 	ret = snd_sof_init_trace_ipc(sdev);
154 	if (ret < 0) {
155 		/* non fatal */
156 		dev_warn(sdev->dev,
157 			 "warning: failed to init trace after resume %d\n",
158 			 ret);
159 	}
160 
161 	/* restore pipelines */
162 	ret = sof_set_up_pipelines(sdev, false);
163 	if (ret < 0) {
164 		dev_err(sdev->dev,
165 			"error: failed to restore pipeline after resume %d\n",
166 			ret);
167 		return ret;
168 	}
169 
170 	/* notify DSP of system resume */
171 	ret = sof_send_pm_ctx_ipc(sdev, SOF_IPC_PM_CTX_RESTORE);
172 	if (ret < 0)
173 		dev_err(sdev->dev,
174 			"error: ctx_restore ipc error during resume %d\n",
175 			ret);
176 
177 	return ret;
178 }
179 
180 static int sof_suspend(struct device *dev, bool runtime_suspend)
181 {
182 	struct snd_sof_dev *sdev = dev_get_drvdata(dev);
183 	u32 target_state = 0;
184 	int ret;
185 
186 	/* do nothing if dsp suspend callback is not set */
187 	if (!runtime_suspend && !sof_ops(sdev)->suspend)
188 		return 0;
189 
190 	if (runtime_suspend && !sof_ops(sdev)->runtime_suspend)
191 		return 0;
192 
193 	if (sdev->fw_state != SOF_FW_BOOT_COMPLETE)
194 		goto suspend;
195 
196 	/* prepare for streams to be resumed properly upon resume */
197 	if (!runtime_suspend) {
198 		ret = sof_set_hw_params_upon_resume(sdev->dev);
199 		if (ret < 0) {
200 			dev_err(sdev->dev,
201 				"error: setting hw_params flag during suspend %d\n",
202 				ret);
203 			return ret;
204 		}
205 	}
206 
207 	target_state = snd_sof_dsp_power_target(sdev);
208 
209 	/* Skip to platform-specific suspend if DSP is entering D0 */
210 	if (target_state == SOF_DSP_PM_D0)
211 		goto suspend;
212 
213 	sof_tear_down_pipelines(sdev, false);
214 
215 	/* release trace */
216 	snd_sof_release_trace(sdev);
217 
218 #if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_ENABLE_DEBUGFS_CACHE)
219 	/* cache debugfs contents during runtime suspend */
220 	if (runtime_suspend)
221 		sof_cache_debugfs(sdev);
222 #endif
223 	/* notify DSP of upcoming power down */
224 	ret = sof_send_pm_ctx_ipc(sdev, SOF_IPC_PM_CTX_SAVE);
225 	if (ret == -EBUSY || ret == -EAGAIN) {
226 		/*
227 		 * runtime PM has logic to handle -EBUSY/-EAGAIN so
228 		 * pass these errors up
229 		 */
230 		dev_err(sdev->dev,
231 			"error: ctx_save ipc error during suspend %d\n",
232 			ret);
233 		return ret;
234 	} else if (ret < 0) {
235 		/* FW in unexpected state, continue to power down */
236 		dev_warn(sdev->dev,
237 			 "ctx_save ipc error %d, proceeding with suspend\n",
238 			 ret);
239 	}
240 
241 suspend:
242 
243 	/* return if the DSP was not probed successfully */
244 	if (sdev->fw_state == SOF_FW_BOOT_NOT_STARTED)
245 		return 0;
246 
247 	/* platform-specific suspend */
248 	if (runtime_suspend)
249 		ret = snd_sof_dsp_runtime_suspend(sdev);
250 	else
251 		ret = snd_sof_dsp_suspend(sdev, target_state);
252 	if (ret < 0)
253 		dev_err(sdev->dev,
254 			"error: failed to power down DSP during suspend %d\n",
255 			ret);
256 
257 	/* Do not reset FW state if DSP is in D0 */
258 	if (target_state == SOF_DSP_PM_D0)
259 		return ret;
260 
261 	/* reset FW state */
262 	sof_set_fw_state(sdev, SOF_FW_BOOT_NOT_STARTED);
263 	sdev->enabled_cores_mask = 0;
264 
265 	return ret;
266 }
267 
268 int snd_sof_dsp_power_down_notify(struct snd_sof_dev *sdev)
269 {
270 	/* Notify DSP of upcoming power down */
271 	if (sof_ops(sdev)->remove)
272 		return sof_send_pm_ctx_ipc(sdev, SOF_IPC_PM_CTX_SAVE);
273 
274 	return 0;
275 }
276 
277 int snd_sof_runtime_suspend(struct device *dev)
278 {
279 	return sof_suspend(dev, true);
280 }
281 EXPORT_SYMBOL(snd_sof_runtime_suspend);
282 
283 int snd_sof_runtime_idle(struct device *dev)
284 {
285 	struct snd_sof_dev *sdev = dev_get_drvdata(dev);
286 
287 	return snd_sof_dsp_runtime_idle(sdev);
288 }
289 EXPORT_SYMBOL(snd_sof_runtime_idle);
290 
291 int snd_sof_runtime_resume(struct device *dev)
292 {
293 	return sof_resume(dev, true);
294 }
295 EXPORT_SYMBOL(snd_sof_runtime_resume);
296 
297 int snd_sof_resume(struct device *dev)
298 {
299 	return sof_resume(dev, false);
300 }
301 EXPORT_SYMBOL(snd_sof_resume);
302 
303 int snd_sof_suspend(struct device *dev)
304 {
305 	return sof_suspend(dev, false);
306 }
307 EXPORT_SYMBOL(snd_sof_suspend);
308 
309 int snd_sof_prepare(struct device *dev)
310 {
311 	struct snd_sof_dev *sdev = dev_get_drvdata(dev);
312 	const struct sof_dev_desc *desc = sdev->pdata->desc;
313 
314 	/* will suspend to S3 by default */
315 	sdev->system_suspend_target = SOF_SUSPEND_S3;
316 
317 	/*
318 	 * if the firmware is crashed or boot failed then we try to aim for S3
319 	 * to reboot the firmware
320 	 */
321 	if (sdev->fw_state == SOF_FW_CRASHED ||
322 	    sdev->fw_state == SOF_FW_BOOT_FAILED)
323 		return 0;
324 
325 	if (!desc->use_acpi_target_states)
326 		return 0;
327 
328 #if defined(CONFIG_ACPI)
329 	if (acpi_target_system_state() == ACPI_STATE_S0)
330 		sdev->system_suspend_target = SOF_SUSPEND_S0IX;
331 #endif
332 
333 	return 0;
334 }
335 EXPORT_SYMBOL(snd_sof_prepare);
336 
337 void snd_sof_complete(struct device *dev)
338 {
339 	struct snd_sof_dev *sdev = dev_get_drvdata(dev);
340 
341 	sdev->system_suspend_target = SOF_SUSPEND_NONE;
342 }
343 EXPORT_SYMBOL(snd_sof_complete);
344