xref: /linux/sound/soc/sof/intel/hda-dsp.c (revision 404bec4c8f6c38ae5fa208344f1086d38026e93d)
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 // Authors: Liam Girdwood <liam.r.girdwood@linux.intel.com>
9 //	    Ranjani Sridharan <ranjani.sridharan@linux.intel.com>
10 //	    Rander Wang <rander.wang@intel.com>
11 //          Keyon Jie <yang.jie@linux.intel.com>
12 //
13 
14 /*
15  * Hardware interface for generic Intel audio DSP HDA IP
16  */
17 
18 #include <linux/module.h>
19 #include <sound/hdaudio_ext.h>
20 #include <sound/hda_register.h>
21 #include "../sof-audio.h"
22 #include "../ops.h"
23 #include "hda.h"
24 #include "hda-ipc.h"
25 
26 static bool hda_enable_trace_D0I3_S0;
27 #if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG)
28 module_param_named(enable_trace_D0I3_S0, hda_enable_trace_D0I3_S0, bool, 0444);
29 MODULE_PARM_DESC(enable_trace_D0I3_S0,
30 		 "SOF HDA enable trace when the DSP is in D0I3 in S0");
31 #endif
32 
33 /*
34  * DSP Core control.
35  */
36 
37 static int hda_dsp_core_reset_enter(struct snd_sof_dev *sdev, unsigned int core_mask)
38 {
39 	u32 adspcs;
40 	u32 reset;
41 	int ret;
42 
43 	/* set reset bits for cores */
44 	reset = HDA_DSP_ADSPCS_CRST_MASK(core_mask);
45 	snd_sof_dsp_update_bits_unlocked(sdev, HDA_DSP_BAR,
46 					 HDA_DSP_REG_ADSPCS,
47 					 reset, reset);
48 
49 	/* poll with timeout to check if operation successful */
50 	ret = snd_sof_dsp_read_poll_timeout(sdev, HDA_DSP_BAR,
51 					HDA_DSP_REG_ADSPCS, adspcs,
52 					((adspcs & reset) == reset),
53 					HDA_DSP_REG_POLL_INTERVAL_US,
54 					HDA_DSP_RESET_TIMEOUT_US);
55 	if (ret < 0) {
56 		dev_err(sdev->dev,
57 			"error: %s: timeout on HDA_DSP_REG_ADSPCS read\n",
58 			__func__);
59 		return ret;
60 	}
61 
62 	/* has core entered reset ? */
63 	adspcs = snd_sof_dsp_read(sdev, HDA_DSP_BAR,
64 				  HDA_DSP_REG_ADSPCS);
65 	if ((adspcs & HDA_DSP_ADSPCS_CRST_MASK(core_mask)) !=
66 		HDA_DSP_ADSPCS_CRST_MASK(core_mask)) {
67 		dev_err(sdev->dev,
68 			"error: reset enter failed: core_mask %x adspcs 0x%x\n",
69 			core_mask, adspcs);
70 		ret = -EIO;
71 	}
72 
73 	return ret;
74 }
75 
76 static int hda_dsp_core_reset_leave(struct snd_sof_dev *sdev, unsigned int core_mask)
77 {
78 	unsigned int crst;
79 	u32 adspcs;
80 	int ret;
81 
82 	/* clear reset bits for cores */
83 	snd_sof_dsp_update_bits_unlocked(sdev, HDA_DSP_BAR,
84 					 HDA_DSP_REG_ADSPCS,
85 					 HDA_DSP_ADSPCS_CRST_MASK(core_mask),
86 					 0);
87 
88 	/* poll with timeout to check if operation successful */
89 	crst = HDA_DSP_ADSPCS_CRST_MASK(core_mask);
90 	ret = snd_sof_dsp_read_poll_timeout(sdev, HDA_DSP_BAR,
91 					    HDA_DSP_REG_ADSPCS, adspcs,
92 					    !(adspcs & crst),
93 					    HDA_DSP_REG_POLL_INTERVAL_US,
94 					    HDA_DSP_RESET_TIMEOUT_US);
95 
96 	if (ret < 0) {
97 		dev_err(sdev->dev,
98 			"error: %s: timeout on HDA_DSP_REG_ADSPCS read\n",
99 			__func__);
100 		return ret;
101 	}
102 
103 	/* has core left reset ? */
104 	adspcs = snd_sof_dsp_read(sdev, HDA_DSP_BAR,
105 				  HDA_DSP_REG_ADSPCS);
106 	if ((adspcs & HDA_DSP_ADSPCS_CRST_MASK(core_mask)) != 0) {
107 		dev_err(sdev->dev,
108 			"error: reset leave failed: core_mask %x adspcs 0x%x\n",
109 			core_mask, adspcs);
110 		ret = -EIO;
111 	}
112 
113 	return ret;
114 }
115 
116 static int hda_dsp_core_stall_reset(struct snd_sof_dev *sdev, unsigned int core_mask)
117 {
118 	/* stall core */
119 	snd_sof_dsp_update_bits_unlocked(sdev, HDA_DSP_BAR,
120 					 HDA_DSP_REG_ADSPCS,
121 					 HDA_DSP_ADSPCS_CSTALL_MASK(core_mask),
122 					 HDA_DSP_ADSPCS_CSTALL_MASK(core_mask));
123 
124 	/* set reset state */
125 	return hda_dsp_core_reset_enter(sdev, core_mask);
126 }
127 
128 static bool hda_dsp_core_is_enabled(struct snd_sof_dev *sdev, unsigned int core_mask)
129 {
130 	int val;
131 	bool is_enable;
132 
133 	val = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_REG_ADSPCS);
134 
135 #define MASK_IS_EQUAL(v, m, field) ({	\
136 	u32 _m = field(m);		\
137 	((v) & _m) == _m;		\
138 })
139 
140 	is_enable = MASK_IS_EQUAL(val, core_mask, HDA_DSP_ADSPCS_CPA_MASK) &&
141 		MASK_IS_EQUAL(val, core_mask, HDA_DSP_ADSPCS_SPA_MASK) &&
142 		!(val & HDA_DSP_ADSPCS_CRST_MASK(core_mask)) &&
143 		!(val & HDA_DSP_ADSPCS_CSTALL_MASK(core_mask));
144 
145 #undef MASK_IS_EQUAL
146 
147 	dev_dbg(sdev->dev, "DSP core(s) enabled? %d : core_mask %x\n",
148 		is_enable, core_mask);
149 
150 	return is_enable;
151 }
152 
153 int hda_dsp_core_run(struct snd_sof_dev *sdev, unsigned int core_mask)
154 {
155 	int ret;
156 
157 	/* leave reset state */
158 	ret = hda_dsp_core_reset_leave(sdev, core_mask);
159 	if (ret < 0)
160 		return ret;
161 
162 	/* run core */
163 	dev_dbg(sdev->dev, "unstall/run core: core_mask = %x\n", core_mask);
164 	snd_sof_dsp_update_bits_unlocked(sdev, HDA_DSP_BAR,
165 					 HDA_DSP_REG_ADSPCS,
166 					 HDA_DSP_ADSPCS_CSTALL_MASK(core_mask),
167 					 0);
168 
169 	/* is core now running ? */
170 	if (!hda_dsp_core_is_enabled(sdev, core_mask)) {
171 		hda_dsp_core_stall_reset(sdev, core_mask);
172 		dev_err(sdev->dev, "error: DSP start core failed: core_mask %x\n",
173 			core_mask);
174 		ret = -EIO;
175 	}
176 
177 	return ret;
178 }
179 
180 /*
181  * Power Management.
182  */
183 
184 int hda_dsp_core_power_up(struct snd_sof_dev *sdev, unsigned int core_mask)
185 {
186 	struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata;
187 	const struct sof_intel_dsp_desc *chip = hda->desc;
188 	unsigned int cpa;
189 	u32 adspcs;
190 	int ret;
191 
192 	/* restrict core_mask to host managed cores mask */
193 	core_mask &= chip->host_managed_cores_mask;
194 	/* return if core_mask is not valid */
195 	if (!core_mask)
196 		return 0;
197 
198 	/* update bits */
199 	snd_sof_dsp_update_bits(sdev, HDA_DSP_BAR, HDA_DSP_REG_ADSPCS,
200 				HDA_DSP_ADSPCS_SPA_MASK(core_mask),
201 				HDA_DSP_ADSPCS_SPA_MASK(core_mask));
202 
203 	/* poll with timeout to check if operation successful */
204 	cpa = HDA_DSP_ADSPCS_CPA_MASK(core_mask);
205 	ret = snd_sof_dsp_read_poll_timeout(sdev, HDA_DSP_BAR,
206 					    HDA_DSP_REG_ADSPCS, adspcs,
207 					    (adspcs & cpa) == cpa,
208 					    HDA_DSP_REG_POLL_INTERVAL_US,
209 					    HDA_DSP_RESET_TIMEOUT_US);
210 	if (ret < 0) {
211 		dev_err(sdev->dev,
212 			"error: %s: timeout on HDA_DSP_REG_ADSPCS read\n",
213 			__func__);
214 		return ret;
215 	}
216 
217 	/* did core power up ? */
218 	adspcs = snd_sof_dsp_read(sdev, HDA_DSP_BAR,
219 				  HDA_DSP_REG_ADSPCS);
220 	if ((adspcs & HDA_DSP_ADSPCS_CPA_MASK(core_mask)) !=
221 		HDA_DSP_ADSPCS_CPA_MASK(core_mask)) {
222 		dev_err(sdev->dev,
223 			"error: power up core failed core_mask %xadspcs 0x%x\n",
224 			core_mask, adspcs);
225 		ret = -EIO;
226 	}
227 
228 	return ret;
229 }
230 
231 static int hda_dsp_core_power_down(struct snd_sof_dev *sdev, unsigned int core_mask)
232 {
233 	u32 adspcs;
234 	int ret;
235 
236 	/* update bits */
237 	snd_sof_dsp_update_bits_unlocked(sdev, HDA_DSP_BAR,
238 					 HDA_DSP_REG_ADSPCS,
239 					 HDA_DSP_ADSPCS_SPA_MASK(core_mask), 0);
240 
241 	ret = snd_sof_dsp_read_poll_timeout(sdev, HDA_DSP_BAR,
242 				HDA_DSP_REG_ADSPCS, adspcs,
243 				!(adspcs & HDA_DSP_ADSPCS_CPA_MASK(core_mask)),
244 				HDA_DSP_REG_POLL_INTERVAL_US,
245 				HDA_DSP_PD_TIMEOUT * USEC_PER_MSEC);
246 	if (ret < 0)
247 		dev_err(sdev->dev,
248 			"error: %s: timeout on HDA_DSP_REG_ADSPCS read\n",
249 			__func__);
250 
251 	return ret;
252 }
253 
254 int hda_dsp_enable_core(struct snd_sof_dev *sdev, unsigned int core_mask)
255 {
256 	struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata;
257 	const struct sof_intel_dsp_desc *chip = hda->desc;
258 	int ret;
259 
260 	/* restrict core_mask to host managed cores mask */
261 	core_mask &= chip->host_managed_cores_mask;
262 
263 	/* return if core_mask is not valid or cores are already enabled */
264 	if (!core_mask || hda_dsp_core_is_enabled(sdev, core_mask))
265 		return 0;
266 
267 	/* power up */
268 	ret = hda_dsp_core_power_up(sdev, core_mask);
269 	if (ret < 0) {
270 		dev_err(sdev->dev, "error: dsp core power up failed: core_mask %x\n",
271 			core_mask);
272 		return ret;
273 	}
274 
275 	return hda_dsp_core_run(sdev, core_mask);
276 }
277 
278 int hda_dsp_core_reset_power_down(struct snd_sof_dev *sdev,
279 				  unsigned int core_mask)
280 {
281 	struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata;
282 	const struct sof_intel_dsp_desc *chip = hda->desc;
283 	int ret;
284 
285 	/* restrict core_mask to host managed cores mask */
286 	core_mask &= chip->host_managed_cores_mask;
287 
288 	/* return if core_mask is not valid */
289 	if (!core_mask)
290 		return 0;
291 
292 	/* place core in reset prior to power down */
293 	ret = hda_dsp_core_stall_reset(sdev, core_mask);
294 	if (ret < 0) {
295 		dev_err(sdev->dev, "error: dsp core reset failed: core_mask %x\n",
296 			core_mask);
297 		return ret;
298 	}
299 
300 	/* power down core */
301 	ret = hda_dsp_core_power_down(sdev, core_mask);
302 	if (ret < 0) {
303 		dev_err(sdev->dev, "error: dsp core power down fail mask %x: %d\n",
304 			core_mask, ret);
305 		return ret;
306 	}
307 
308 	/* make sure we are in OFF state */
309 	if (hda_dsp_core_is_enabled(sdev, core_mask)) {
310 		dev_err(sdev->dev, "error: dsp core disable fail mask %x: %d\n",
311 			core_mask, ret);
312 		ret = -EIO;
313 	}
314 
315 	return ret;
316 }
317 
318 void hda_dsp_ipc_int_enable(struct snd_sof_dev *sdev)
319 {
320 	struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata;
321 	const struct sof_intel_dsp_desc *chip = hda->desc;
322 
323 	/* enable IPC DONE and BUSY interrupts */
324 	snd_sof_dsp_update_bits(sdev, HDA_DSP_BAR, chip->ipc_ctl,
325 			HDA_DSP_REG_HIPCCTL_DONE | HDA_DSP_REG_HIPCCTL_BUSY,
326 			HDA_DSP_REG_HIPCCTL_DONE | HDA_DSP_REG_HIPCCTL_BUSY);
327 
328 	/* enable IPC interrupt */
329 	snd_sof_dsp_update_bits(sdev, HDA_DSP_BAR, HDA_DSP_REG_ADSPIC,
330 				HDA_DSP_ADSPIC_IPC, HDA_DSP_ADSPIC_IPC);
331 }
332 
333 void hda_dsp_ipc_int_disable(struct snd_sof_dev *sdev)
334 {
335 	struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata;
336 	const struct sof_intel_dsp_desc *chip = hda->desc;
337 
338 	/* disable IPC interrupt */
339 	snd_sof_dsp_update_bits(sdev, HDA_DSP_BAR, HDA_DSP_REG_ADSPIC,
340 				HDA_DSP_ADSPIC_IPC, 0);
341 
342 	/* disable IPC BUSY and DONE interrupt */
343 	snd_sof_dsp_update_bits(sdev, HDA_DSP_BAR, chip->ipc_ctl,
344 			HDA_DSP_REG_HIPCCTL_BUSY | HDA_DSP_REG_HIPCCTL_DONE, 0);
345 }
346 
347 static int hda_dsp_wait_d0i3c_done(struct snd_sof_dev *sdev)
348 {
349 	struct hdac_bus *bus = sof_to_bus(sdev);
350 	int retry = HDA_DSP_REG_POLL_RETRY_COUNT;
351 
352 	while (snd_hdac_chip_readb(bus, VS_D0I3C) & SOF_HDA_VS_D0I3C_CIP) {
353 		if (!retry--)
354 			return -ETIMEDOUT;
355 		usleep_range(10, 15);
356 	}
357 
358 	return 0;
359 }
360 
361 static int hda_dsp_send_pm_gate_ipc(struct snd_sof_dev *sdev, u32 flags)
362 {
363 	struct sof_ipc_pm_gate pm_gate;
364 	struct sof_ipc_reply reply;
365 
366 	memset(&pm_gate, 0, sizeof(pm_gate));
367 
368 	/* configure pm_gate ipc message */
369 	pm_gate.hdr.size = sizeof(pm_gate);
370 	pm_gate.hdr.cmd = SOF_IPC_GLB_PM_MSG | SOF_IPC_PM_GATE;
371 	pm_gate.flags = flags;
372 
373 	/* send pm_gate ipc to dsp */
374 	return sof_ipc_tx_message_no_pm(sdev->ipc, &pm_gate, sizeof(pm_gate),
375 					&reply, sizeof(reply));
376 }
377 
378 static int hda_dsp_update_d0i3c_register(struct snd_sof_dev *sdev, u8 value)
379 {
380 	struct hdac_bus *bus = sof_to_bus(sdev);
381 	int ret;
382 
383 	/* Write to D0I3C after Command-In-Progress bit is cleared */
384 	ret = hda_dsp_wait_d0i3c_done(sdev);
385 	if (ret < 0) {
386 		dev_err(bus->dev, "CIP timeout before D0I3C update!\n");
387 		return ret;
388 	}
389 
390 	/* Update D0I3C register */
391 	snd_hdac_chip_updateb(bus, VS_D0I3C, SOF_HDA_VS_D0I3C_I3, value);
392 
393 	/* Wait for cmd in progress to be cleared before exiting the function */
394 	ret = hda_dsp_wait_d0i3c_done(sdev);
395 	if (ret < 0) {
396 		dev_err(bus->dev, "CIP timeout after D0I3C update!\n");
397 		return ret;
398 	}
399 
400 	dev_vdbg(bus->dev, "D0I3C updated, register = 0x%x\n",
401 		 snd_hdac_chip_readb(bus, VS_D0I3C));
402 
403 	return 0;
404 }
405 
406 static int hda_dsp_set_D0_state(struct snd_sof_dev *sdev,
407 				const struct sof_dsp_power_state *target_state)
408 {
409 	u32 flags = 0;
410 	int ret;
411 	u8 value = 0;
412 
413 	/*
414 	 * Sanity check for illegal state transitions
415 	 * The only allowed transitions are:
416 	 * 1. D3 -> D0I0
417 	 * 2. D0I0 -> D0I3
418 	 * 3. D0I3 -> D0I0
419 	 */
420 	switch (sdev->dsp_power_state.state) {
421 	case SOF_DSP_PM_D0:
422 		/* Follow the sequence below for D0 substate transitions */
423 		break;
424 	case SOF_DSP_PM_D3:
425 		/* Follow regular flow for D3 -> D0 transition */
426 		return 0;
427 	default:
428 		dev_err(sdev->dev, "error: transition from %d to %d not allowed\n",
429 			sdev->dsp_power_state.state, target_state->state);
430 		return -EINVAL;
431 	}
432 
433 	/* Set flags and register value for D0 target substate */
434 	if (target_state->substate == SOF_HDA_DSP_PM_D0I3) {
435 		value = SOF_HDA_VS_D0I3C_I3;
436 
437 		/*
438 		 * Trace DMA need to be disabled when the DSP enters
439 		 * D0I3 for S0Ix suspend, but it can be kept enabled
440 		 * when the DSP enters D0I3 while the system is in S0
441 		 * for debug purpose.
442 		 */
443 		if (!sdev->fw_trace_is_supported ||
444 		    !hda_enable_trace_D0I3_S0 ||
445 		    sdev->system_suspend_target != SOF_SUSPEND_NONE)
446 			flags = HDA_PM_NO_DMA_TRACE;
447 	} else {
448 		/* prevent power gating in D0I0 */
449 		flags = HDA_PM_PPG;
450 	}
451 
452 	/* update D0I3C register */
453 	ret = hda_dsp_update_d0i3c_register(sdev, value);
454 	if (ret < 0)
455 		return ret;
456 
457 	/*
458 	 * Notify the DSP of the state change.
459 	 * If this IPC fails, revert the D0I3C register update in order
460 	 * to prevent partial state change.
461 	 */
462 	ret = hda_dsp_send_pm_gate_ipc(sdev, flags);
463 	if (ret < 0) {
464 		dev_err(sdev->dev,
465 			"error: PM_GATE ipc error %d\n", ret);
466 		goto revert;
467 	}
468 
469 	return ret;
470 
471 revert:
472 	/* fallback to the previous register value */
473 	value = value ? 0 : SOF_HDA_VS_D0I3C_I3;
474 
475 	/*
476 	 * This can fail but return the IPC error to signal that
477 	 * the state change failed.
478 	 */
479 	hda_dsp_update_d0i3c_register(sdev, value);
480 
481 	return ret;
482 }
483 
484 /* helper to log DSP state */
485 static void hda_dsp_state_log(struct snd_sof_dev *sdev)
486 {
487 	switch (sdev->dsp_power_state.state) {
488 	case SOF_DSP_PM_D0:
489 		switch (sdev->dsp_power_state.substate) {
490 		case SOF_HDA_DSP_PM_D0I0:
491 			dev_dbg(sdev->dev, "Current DSP power state: D0I0\n");
492 			break;
493 		case SOF_HDA_DSP_PM_D0I3:
494 			dev_dbg(sdev->dev, "Current DSP power state: D0I3\n");
495 			break;
496 		default:
497 			dev_dbg(sdev->dev, "Unknown DSP D0 substate: %d\n",
498 				sdev->dsp_power_state.substate);
499 			break;
500 		}
501 		break;
502 	case SOF_DSP_PM_D1:
503 		dev_dbg(sdev->dev, "Current DSP power state: D1\n");
504 		break;
505 	case SOF_DSP_PM_D2:
506 		dev_dbg(sdev->dev, "Current DSP power state: D2\n");
507 		break;
508 	case SOF_DSP_PM_D3:
509 		dev_dbg(sdev->dev, "Current DSP power state: D3\n");
510 		break;
511 	default:
512 		dev_dbg(sdev->dev, "Unknown DSP power state: %d\n",
513 			sdev->dsp_power_state.state);
514 		break;
515 	}
516 }
517 
518 /*
519  * All DSP power state transitions are initiated by the driver.
520  * If the requested state change fails, the error is simply returned.
521  * Further state transitions are attempted only when the set_power_save() op
522  * is called again either because of a new IPC sent to the DSP or
523  * during system suspend/resume.
524  */
525 int hda_dsp_set_power_state(struct snd_sof_dev *sdev,
526 			    const struct sof_dsp_power_state *target_state)
527 {
528 	int ret = 0;
529 
530 	/*
531 	 * When the DSP is already in D0I3 and the target state is D0I3,
532 	 * it could be the case that the DSP is in D0I3 during S0
533 	 * and the system is suspending to S0Ix. Therefore,
534 	 * hda_dsp_set_D0_state() must be called to disable trace DMA
535 	 * by sending the PM_GATE IPC to the FW.
536 	 */
537 	if (target_state->substate == SOF_HDA_DSP_PM_D0I3 &&
538 	    sdev->system_suspend_target == SOF_SUSPEND_S0IX)
539 		goto set_state;
540 
541 	/*
542 	 * For all other cases, return without doing anything if
543 	 * the DSP is already in the target state.
544 	 */
545 	if (target_state->state == sdev->dsp_power_state.state &&
546 	    target_state->substate == sdev->dsp_power_state.substate)
547 		return 0;
548 
549 set_state:
550 	switch (target_state->state) {
551 	case SOF_DSP_PM_D0:
552 		ret = hda_dsp_set_D0_state(sdev, target_state);
553 		break;
554 	case SOF_DSP_PM_D3:
555 		/* The only allowed transition is: D0I0 -> D3 */
556 		if (sdev->dsp_power_state.state == SOF_DSP_PM_D0 &&
557 		    sdev->dsp_power_state.substate == SOF_HDA_DSP_PM_D0I0)
558 			break;
559 
560 		dev_err(sdev->dev,
561 			"error: transition from %d to %d not allowed\n",
562 			sdev->dsp_power_state.state, target_state->state);
563 		return -EINVAL;
564 	default:
565 		dev_err(sdev->dev, "error: target state unsupported %d\n",
566 			target_state->state);
567 		return -EINVAL;
568 	}
569 	if (ret < 0) {
570 		dev_err(sdev->dev,
571 			"failed to set requested target DSP state %d substate %d\n",
572 			target_state->state, target_state->substate);
573 		return ret;
574 	}
575 
576 	sdev->dsp_power_state = *target_state;
577 	hda_dsp_state_log(sdev);
578 	return ret;
579 }
580 
581 /*
582  * Audio DSP states may transform as below:-
583  *
584  *                                         Opportunistic D0I3 in S0
585  *     Runtime    +---------------------+  Delayed D0i3 work timeout
586  *     suspend    |                     +--------------------+
587  *   +------------+       D0I0(active)  |                    |
588  *   |            |                     <---------------+    |
589  *   |   +-------->                     |    New IPC	|    |
590  *   |   |Runtime +--^--+---------^--+--+ (via mailbox)	|    |
591  *   |   |resume     |  |         |  |			|    |
592  *   |   |           |  |         |  |			|    |
593  *   |   |     System|  |         |  |			|    |
594  *   |   |     resume|  | S3/S0IX |  |                  |    |
595  *   |   |	     |  | suspend |  | S0IX             |    |
596  *   |   |           |  |         |  |suspend           |    |
597  *   |   |           |  |         |  |                  |    |
598  *   |   |           |  |         |  |                  |    |
599  * +-v---+-----------+--v-------+ |  |           +------+----v----+
600  * |                            | |  +----------->                |
601  * |       D3 (suspended)       | |              |      D0I3      |
602  * |                            | +--------------+                |
603  * |                            |  System resume |                |
604  * +----------------------------+		 +----------------+
605  *
606  * S0IX suspend: The DSP is in D0I3 if any D0I3-compatible streams
607  *		 ignored the suspend trigger. Otherwise the DSP
608  *		 is in D3.
609  */
610 
611 static int hda_suspend(struct snd_sof_dev *sdev, bool runtime_suspend)
612 {
613 	struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata;
614 	const struct sof_intel_dsp_desc *chip = hda->desc;
615 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
616 	struct hdac_bus *bus = sof_to_bus(sdev);
617 #endif
618 	int ret, j;
619 
620 	/*
621 	 * The memory used for IMR boot loses its content in deeper than S3 state
622 	 * We must not try IMR boot on next power up (as it will fail).
623 	 *
624 	 * In case of firmware crash or boot failure set the skip_imr_boot to true
625 	 * as well in order to try to re-load the firmware to do a 'cold' boot.
626 	 */
627 	if (sdev->system_suspend_target > SOF_SUSPEND_S3 ||
628 	    sdev->fw_state == SOF_FW_CRASHED ||
629 	    sdev->fw_state == SOF_FW_BOOT_FAILED)
630 		hda->skip_imr_boot = true;
631 
632 	hda_sdw_int_enable(sdev, false);
633 
634 	/* disable IPC interrupts */
635 	hda_dsp_ipc_int_disable(sdev);
636 
637 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
638 	hda_codec_jack_wake_enable(sdev, runtime_suspend);
639 
640 	/* power down all hda link */
641 	snd_hdac_ext_bus_link_power_down_all(bus);
642 #endif
643 
644 	/* power down DSP */
645 	ret = hda_dsp_core_reset_power_down(sdev, chip->host_managed_cores_mask);
646 	if (ret < 0) {
647 		dev_err(sdev->dev,
648 			"error: failed to power down core during suspend\n");
649 		return ret;
650 	}
651 
652 	/* reset ref counts for all cores */
653 	for (j = 0; j < chip->cores_num; j++)
654 		sdev->dsp_core_ref_count[j] = 0;
655 
656 	/* disable ppcap interrupt */
657 	hda_dsp_ctrl_ppcap_enable(sdev, false);
658 	hda_dsp_ctrl_ppcap_int_enable(sdev, false);
659 
660 	/* disable hda bus irq and streams */
661 	hda_dsp_ctrl_stop_chip(sdev);
662 
663 	/* disable LP retention mode */
664 	snd_sof_pci_update_bits(sdev, PCI_PGCTL,
665 				PCI_PGCTL_LSRMD_MASK, PCI_PGCTL_LSRMD_MASK);
666 
667 	/* reset controller */
668 	ret = hda_dsp_ctrl_link_reset(sdev, true);
669 	if (ret < 0) {
670 		dev_err(sdev->dev,
671 			"error: failed to reset controller during suspend\n");
672 		return ret;
673 	}
674 
675 	/* display codec can powered off after link reset */
676 	hda_codec_i915_display_power(sdev, false);
677 
678 	return 0;
679 }
680 
681 static int hda_resume(struct snd_sof_dev *sdev, bool runtime_resume)
682 {
683 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
684 	struct hdac_bus *bus = sof_to_bus(sdev);
685 	struct hdac_ext_link *hlink = NULL;
686 #endif
687 	int ret;
688 
689 	/* display codec must be powered before link reset */
690 	hda_codec_i915_display_power(sdev, true);
691 
692 	/*
693 	 * clear TCSEL to clear playback on some HD Audio
694 	 * codecs. PCI TCSEL is defined in the Intel manuals.
695 	 */
696 	snd_sof_pci_update_bits(sdev, PCI_TCSEL, 0x07, 0);
697 
698 	/* reset and start hda controller */
699 	ret = hda_dsp_ctrl_init_chip(sdev, true);
700 	if (ret < 0) {
701 		dev_err(sdev->dev,
702 			"error: failed to start controller after resume\n");
703 		goto cleanup;
704 	}
705 
706 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
707 	/* check jack status */
708 	if (runtime_resume) {
709 		hda_codec_jack_wake_enable(sdev, false);
710 		if (sdev->system_suspend_target == SOF_SUSPEND_NONE)
711 			hda_codec_jack_check(sdev);
712 	}
713 
714 	/* turn off the links that were off before suspend */
715 	list_for_each_entry(hlink, &bus->hlink_list, list) {
716 		if (!hlink->ref_count)
717 			snd_hdac_ext_bus_link_power_down(hlink);
718 	}
719 
720 	/* check dma status and clean up CORB/RIRB buffers */
721 	if (!bus->cmd_dma_state)
722 		snd_hdac_bus_stop_cmd_io(bus);
723 #endif
724 
725 	/* enable ppcap interrupt */
726 	hda_dsp_ctrl_ppcap_enable(sdev, true);
727 	hda_dsp_ctrl_ppcap_int_enable(sdev, true);
728 
729 cleanup:
730 	/* display codec can powered off after controller init */
731 	hda_codec_i915_display_power(sdev, false);
732 
733 	return 0;
734 }
735 
736 int hda_dsp_resume(struct snd_sof_dev *sdev)
737 {
738 	struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata;
739 	struct pci_dev *pci = to_pci_dev(sdev->dev);
740 	const struct sof_dsp_power_state target_state = {
741 		.state = SOF_DSP_PM_D0,
742 		.substate = SOF_HDA_DSP_PM_D0I0,
743 	};
744 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
745 	struct hdac_bus *bus = sof_to_bus(sdev);
746 	struct hdac_ext_link *hlink = NULL;
747 #endif
748 	int ret;
749 
750 	/* resume from D0I3 */
751 	if (sdev->dsp_power_state.state == SOF_DSP_PM_D0) {
752 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
753 		/* power up links that were active before suspend */
754 		list_for_each_entry(hlink, &bus->hlink_list, list) {
755 			if (hlink->ref_count) {
756 				ret = snd_hdac_ext_bus_link_power_up(hlink);
757 				if (ret < 0) {
758 					dev_err(sdev->dev,
759 						"error %d in %s: failed to power up links",
760 						ret, __func__);
761 					return ret;
762 				}
763 			}
764 		}
765 
766 		/* set up CORB/RIRB buffers if was on before suspend */
767 		if (bus->cmd_dma_state)
768 			snd_hdac_bus_init_cmd_io(bus);
769 #endif
770 
771 		/* Set DSP power state */
772 		ret = snd_sof_dsp_set_power_state(sdev, &target_state);
773 		if (ret < 0) {
774 			dev_err(sdev->dev, "error: setting dsp state %d substate %d\n",
775 				target_state.state, target_state.substate);
776 			return ret;
777 		}
778 
779 		/* restore L1SEN bit */
780 		if (hda->l1_support_changed)
781 			snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR,
782 						HDA_VS_INTEL_EM2,
783 						HDA_VS_INTEL_EM2_L1SEN, 0);
784 
785 		/* restore and disable the system wakeup */
786 		pci_restore_state(pci);
787 		disable_irq_wake(pci->irq);
788 		return 0;
789 	}
790 
791 	/* init hda controller. DSP cores will be powered up during fw boot */
792 	ret = hda_resume(sdev, false);
793 	if (ret < 0)
794 		return ret;
795 
796 	return snd_sof_dsp_set_power_state(sdev, &target_state);
797 }
798 
799 int hda_dsp_runtime_resume(struct snd_sof_dev *sdev)
800 {
801 	const struct sof_dsp_power_state target_state = {
802 		.state = SOF_DSP_PM_D0,
803 	};
804 	int ret;
805 
806 	/* init hda controller. DSP cores will be powered up during fw boot */
807 	ret = hda_resume(sdev, true);
808 	if (ret < 0)
809 		return ret;
810 
811 	return snd_sof_dsp_set_power_state(sdev, &target_state);
812 }
813 
814 int hda_dsp_runtime_idle(struct snd_sof_dev *sdev)
815 {
816 	struct hdac_bus *hbus = sof_to_bus(sdev);
817 
818 	if (hbus->codec_powered) {
819 		dev_dbg(sdev->dev, "some codecs still powered (%08X), not idle\n",
820 			(unsigned int)hbus->codec_powered);
821 		return -EBUSY;
822 	}
823 
824 	return 0;
825 }
826 
827 int hda_dsp_runtime_suspend(struct snd_sof_dev *sdev)
828 {
829 	struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata;
830 	const struct sof_dsp_power_state target_state = {
831 		.state = SOF_DSP_PM_D3,
832 	};
833 	int ret;
834 
835 	/* cancel any attempt for DSP D0I3 */
836 	cancel_delayed_work_sync(&hda->d0i3_work);
837 
838 	/* stop hda controller and power dsp off */
839 	ret = hda_suspend(sdev, true);
840 	if (ret < 0)
841 		return ret;
842 
843 	return snd_sof_dsp_set_power_state(sdev, &target_state);
844 }
845 
846 int hda_dsp_suspend(struct snd_sof_dev *sdev, u32 target_state)
847 {
848 	struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata;
849 	struct hdac_bus *bus = sof_to_bus(sdev);
850 	struct pci_dev *pci = to_pci_dev(sdev->dev);
851 	const struct sof_dsp_power_state target_dsp_state = {
852 		.state = target_state,
853 		.substate = target_state == SOF_DSP_PM_D0 ?
854 				SOF_HDA_DSP_PM_D0I3 : 0,
855 	};
856 	int ret;
857 
858 	/* cancel any attempt for DSP D0I3 */
859 	cancel_delayed_work_sync(&hda->d0i3_work);
860 
861 	if (target_state == SOF_DSP_PM_D0) {
862 		/* Set DSP power state */
863 		ret = snd_sof_dsp_set_power_state(sdev, &target_dsp_state);
864 		if (ret < 0) {
865 			dev_err(sdev->dev, "error: setting dsp state %d substate %d\n",
866 				target_dsp_state.state,
867 				target_dsp_state.substate);
868 			return ret;
869 		}
870 
871 		/* enable L1SEN to make sure the system can enter S0Ix */
872 		hda->l1_support_changed =
873 			snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR,
874 						HDA_VS_INTEL_EM2,
875 						HDA_VS_INTEL_EM2_L1SEN,
876 						HDA_VS_INTEL_EM2_L1SEN);
877 
878 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
879 		/* stop the CORB/RIRB DMA if it is On */
880 		if (bus->cmd_dma_state)
881 			snd_hdac_bus_stop_cmd_io(bus);
882 
883 		/* no link can be powered in s0ix state */
884 		ret = snd_hdac_ext_bus_link_power_down_all(bus);
885 		if (ret < 0) {
886 			dev_err(sdev->dev,
887 				"error %d in %s: failed to power down links",
888 				ret, __func__);
889 			return ret;
890 		}
891 #endif
892 
893 		/* enable the system waking up via IPC IRQ */
894 		enable_irq_wake(pci->irq);
895 		pci_save_state(pci);
896 		return 0;
897 	}
898 
899 	/* stop hda controller and power dsp off */
900 	ret = hda_suspend(sdev, false);
901 	if (ret < 0) {
902 		dev_err(bus->dev, "error: suspending dsp\n");
903 		return ret;
904 	}
905 
906 	return snd_sof_dsp_set_power_state(sdev, &target_dsp_state);
907 }
908 
909 int hda_dsp_shutdown(struct snd_sof_dev *sdev)
910 {
911 	sdev->system_suspend_target = SOF_SUSPEND_S3;
912 	return snd_sof_suspend(sdev->dev);
913 }
914 
915 int hda_dsp_set_hw_params_upon_resume(struct snd_sof_dev *sdev)
916 {
917 	int ret;
918 
919 	/* make sure all DAI resources are freed */
920 	ret = hda_dsp_dais_suspend(sdev);
921 	if (ret < 0)
922 		dev_warn(sdev->dev, "%s: failure in hda_dsp_dais_suspend\n", __func__);
923 
924 	return ret;
925 }
926 
927 void hda_dsp_d0i3_work(struct work_struct *work)
928 {
929 	struct sof_intel_hda_dev *hdev = container_of(work,
930 						      struct sof_intel_hda_dev,
931 						      d0i3_work.work);
932 	struct hdac_bus *bus = &hdev->hbus.core;
933 	struct snd_sof_dev *sdev = dev_get_drvdata(bus->dev);
934 	struct sof_dsp_power_state target_state = {
935 		.state = SOF_DSP_PM_D0,
936 		.substate = SOF_HDA_DSP_PM_D0I3,
937 	};
938 	int ret;
939 
940 	/* DSP can enter D0I3 iff only D0I3-compatible streams are active */
941 	if (!snd_sof_dsp_only_d0i3_compatible_stream_active(sdev))
942 		/* remain in D0I0 */
943 		return;
944 
945 	/* This can fail but error cannot be propagated */
946 	ret = snd_sof_dsp_set_power_state(sdev, &target_state);
947 	if (ret < 0)
948 		dev_err_ratelimited(sdev->dev,
949 				    "error: failed to set DSP state %d substate %d\n",
950 				    target_state.state, target_state.substate);
951 }
952 
953 int hda_dsp_core_get(struct snd_sof_dev *sdev, int core)
954 {
955 	const struct sof_ipc_pm_ops *pm_ops = sdev->ipc->ops->pm;
956 	int ret, ret1;
957 
958 	/* power up core */
959 	ret = hda_dsp_enable_core(sdev, BIT(core));
960 	if (ret < 0) {
961 		dev_err(sdev->dev, "failed to power up core %d with err: %d\n",
962 			core, ret);
963 		return ret;
964 	}
965 
966 	/* No need to send IPC for primary core or if FW boot is not complete */
967 	if (sdev->fw_state != SOF_FW_BOOT_COMPLETE || core == SOF_DSP_PRIMARY_CORE)
968 		return 0;
969 
970 	/* No need to continue the set_core_state ops is not available */
971 	if (!pm_ops->set_core_state)
972 		return 0;
973 
974 	/* Now notify DSP for secondary cores */
975 	ret = pm_ops->set_core_state(sdev, core, true);
976 	if (ret < 0) {
977 		dev_err(sdev->dev, "failed to enable secondary core '%d' failed with %d\n",
978 			core, ret);
979 		goto power_down;
980 	}
981 
982 	return ret;
983 
984 power_down:
985 	/* power down core if it is host managed and return the original error if this fails too */
986 	ret1 = hda_dsp_core_reset_power_down(sdev, BIT(core));
987 	if (ret1 < 0)
988 		dev_err(sdev->dev, "failed to power down core: %d with err: %d\n", core, ret1);
989 
990 	return ret;
991 }
992