xref: /openbmc/linux/sound/soc/sof/intel/hda-dsp.c (revision 26d0dfbb16fcb17d128a79dc70f3020ea6992af0)
1e149ca29SPierre-Louis Bossart // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
2747503b1SLiam Girdwood //
3747503b1SLiam Girdwood // This file is provided under a dual BSD/GPLv2 license.  When using or
4747503b1SLiam Girdwood // redistributing this file, you may do so under either license.
5747503b1SLiam Girdwood //
6747503b1SLiam Girdwood // Copyright(c) 2018 Intel Corporation. All rights reserved.
7747503b1SLiam Girdwood //
8747503b1SLiam Girdwood // Authors: Liam Girdwood <liam.r.girdwood@linux.intel.com>
9747503b1SLiam Girdwood //	    Ranjani Sridharan <ranjani.sridharan@linux.intel.com>
10747503b1SLiam Girdwood //	    Rander Wang <rander.wang@intel.com>
11747503b1SLiam Girdwood //          Keyon Jie <yang.jie@linux.intel.com>
12747503b1SLiam Girdwood //
13747503b1SLiam Girdwood 
14747503b1SLiam Girdwood /*
15747503b1SLiam Girdwood  * Hardware interface for generic Intel audio DSP HDA IP
16747503b1SLiam Girdwood  */
17747503b1SLiam Girdwood 
18851fd873SRanjani Sridharan #include <linux/module.h>
19747503b1SLiam Girdwood #include <sound/hdaudio_ext.h>
20747503b1SLiam Girdwood #include <sound/hda_register.h>
2118227585SPierre-Louis Bossart #include <sound/hda-mlink.h>
22d272b657SBard Liao #include <trace/events/sof_intel.h>
2363e51fd3SRanjani Sridharan #include "../sof-audio.h"
24747503b1SLiam Girdwood #include "../ops.h"
25747503b1SLiam Girdwood #include "hda.h"
26534037fdSKeyon Jie #include "hda-ipc.h"
27747503b1SLiam Girdwood 
28851fd873SRanjani Sridharan static bool hda_enable_trace_D0I3_S0;
29851fd873SRanjani Sridharan #if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG)
30851fd873SRanjani Sridharan module_param_named(enable_trace_D0I3_S0, hda_enable_trace_D0I3_S0, bool, 0444);
31851fd873SRanjani Sridharan MODULE_PARM_DESC(enable_trace_D0I3_S0,
32851fd873SRanjani Sridharan 		 "SOF HDA enable trace when the DSP is in D0I3 in S0");
33851fd873SRanjani Sridharan #endif
34851fd873SRanjani Sridharan 
35747503b1SLiam Girdwood /*
36747503b1SLiam Girdwood  * DSP Core control.
37747503b1SLiam Girdwood  */
38747503b1SLiam Girdwood 
hda_dsp_core_reset_enter(struct snd_sof_dev * sdev,unsigned int core_mask)39189bf1deSPeter Ujfalusi static int hda_dsp_core_reset_enter(struct snd_sof_dev *sdev, unsigned int core_mask)
40747503b1SLiam Girdwood {
41747503b1SLiam Girdwood 	u32 adspcs;
42747503b1SLiam Girdwood 	u32 reset;
43747503b1SLiam Girdwood 	int ret;
44747503b1SLiam Girdwood 
45747503b1SLiam Girdwood 	/* set reset bits for cores */
46747503b1SLiam Girdwood 	reset = HDA_DSP_ADSPCS_CRST_MASK(core_mask);
47747503b1SLiam Girdwood 	snd_sof_dsp_update_bits_unlocked(sdev, HDA_DSP_BAR,
48747503b1SLiam Girdwood 					 HDA_DSP_REG_ADSPCS,
49bed5ed64SJulia Lawall 					 reset, reset);
50747503b1SLiam Girdwood 
51747503b1SLiam Girdwood 	/* poll with timeout to check if operation successful */
52747503b1SLiam Girdwood 	ret = snd_sof_dsp_read_poll_timeout(sdev, HDA_DSP_BAR,
53747503b1SLiam Girdwood 					HDA_DSP_REG_ADSPCS, adspcs,
54747503b1SLiam Girdwood 					((adspcs & reset) == reset),
55747503b1SLiam Girdwood 					HDA_DSP_REG_POLL_INTERVAL_US,
56747503b1SLiam Girdwood 					HDA_DSP_RESET_TIMEOUT_US);
576a414489SPierre-Louis Bossart 	if (ret < 0) {
586a414489SPierre-Louis Bossart 		dev_err(sdev->dev,
596a414489SPierre-Louis Bossart 			"error: %s: timeout on HDA_DSP_REG_ADSPCS read\n",
606a414489SPierre-Louis Bossart 			__func__);
616a414489SPierre-Louis Bossart 		return ret;
626a414489SPierre-Louis Bossart 	}
63747503b1SLiam Girdwood 
64747503b1SLiam Girdwood 	/* has core entered reset ? */
65747503b1SLiam Girdwood 	adspcs = snd_sof_dsp_read(sdev, HDA_DSP_BAR,
66747503b1SLiam Girdwood 				  HDA_DSP_REG_ADSPCS);
67747503b1SLiam Girdwood 	if ((adspcs & HDA_DSP_ADSPCS_CRST_MASK(core_mask)) !=
68747503b1SLiam Girdwood 		HDA_DSP_ADSPCS_CRST_MASK(core_mask)) {
69747503b1SLiam Girdwood 		dev_err(sdev->dev,
70747503b1SLiam Girdwood 			"error: reset enter failed: core_mask %x adspcs 0x%x\n",
71747503b1SLiam Girdwood 			core_mask, adspcs);
72747503b1SLiam Girdwood 		ret = -EIO;
73747503b1SLiam Girdwood 	}
74747503b1SLiam Girdwood 
75747503b1SLiam Girdwood 	return ret;
76747503b1SLiam Girdwood }
77747503b1SLiam Girdwood 
hda_dsp_core_reset_leave(struct snd_sof_dev * sdev,unsigned int core_mask)78189bf1deSPeter Ujfalusi static int hda_dsp_core_reset_leave(struct snd_sof_dev *sdev, unsigned int core_mask)
79747503b1SLiam Girdwood {
80747503b1SLiam Girdwood 	unsigned int crst;
81747503b1SLiam Girdwood 	u32 adspcs;
82747503b1SLiam Girdwood 	int ret;
83747503b1SLiam Girdwood 
84747503b1SLiam Girdwood 	/* clear reset bits for cores */
85747503b1SLiam Girdwood 	snd_sof_dsp_update_bits_unlocked(sdev, HDA_DSP_BAR,
86747503b1SLiam Girdwood 					 HDA_DSP_REG_ADSPCS,
87747503b1SLiam Girdwood 					 HDA_DSP_ADSPCS_CRST_MASK(core_mask),
88747503b1SLiam Girdwood 					 0);
89747503b1SLiam Girdwood 
90747503b1SLiam Girdwood 	/* poll with timeout to check if operation successful */
91747503b1SLiam Girdwood 	crst = HDA_DSP_ADSPCS_CRST_MASK(core_mask);
92747503b1SLiam Girdwood 	ret = snd_sof_dsp_read_poll_timeout(sdev, HDA_DSP_BAR,
93747503b1SLiam Girdwood 					    HDA_DSP_REG_ADSPCS, adspcs,
94747503b1SLiam Girdwood 					    !(adspcs & crst),
95747503b1SLiam Girdwood 					    HDA_DSP_REG_POLL_INTERVAL_US,
96747503b1SLiam Girdwood 					    HDA_DSP_RESET_TIMEOUT_US);
97747503b1SLiam Girdwood 
986a414489SPierre-Louis Bossart 	if (ret < 0) {
996a414489SPierre-Louis Bossart 		dev_err(sdev->dev,
1006a414489SPierre-Louis Bossart 			"error: %s: timeout on HDA_DSP_REG_ADSPCS read\n",
1016a414489SPierre-Louis Bossart 			__func__);
1026a414489SPierre-Louis Bossart 		return ret;
1036a414489SPierre-Louis Bossart 	}
1046a414489SPierre-Louis Bossart 
105747503b1SLiam Girdwood 	/* has core left reset ? */
106747503b1SLiam Girdwood 	adspcs = snd_sof_dsp_read(sdev, HDA_DSP_BAR,
107747503b1SLiam Girdwood 				  HDA_DSP_REG_ADSPCS);
108747503b1SLiam Girdwood 	if ((adspcs & HDA_DSP_ADSPCS_CRST_MASK(core_mask)) != 0) {
109747503b1SLiam Girdwood 		dev_err(sdev->dev,
110747503b1SLiam Girdwood 			"error: reset leave failed: core_mask %x adspcs 0x%x\n",
111747503b1SLiam Girdwood 			core_mask, adspcs);
112747503b1SLiam Girdwood 		ret = -EIO;
113747503b1SLiam Girdwood 	}
114747503b1SLiam Girdwood 
115747503b1SLiam Girdwood 	return ret;
116747503b1SLiam Girdwood }
117747503b1SLiam Girdwood 
hda_dsp_core_stall_reset(struct snd_sof_dev * sdev,unsigned int core_mask)118556eb416SPierre-Louis Bossart int hda_dsp_core_stall_reset(struct snd_sof_dev *sdev, unsigned int core_mask)
119747503b1SLiam Girdwood {
120747503b1SLiam Girdwood 	/* stall core */
121747503b1SLiam Girdwood 	snd_sof_dsp_update_bits_unlocked(sdev, HDA_DSP_BAR,
122747503b1SLiam Girdwood 					 HDA_DSP_REG_ADSPCS,
123747503b1SLiam Girdwood 					 HDA_DSP_ADSPCS_CSTALL_MASK(core_mask),
124747503b1SLiam Girdwood 					 HDA_DSP_ADSPCS_CSTALL_MASK(core_mask));
125747503b1SLiam Girdwood 
126747503b1SLiam Girdwood 	/* set reset state */
127747503b1SLiam Girdwood 	return hda_dsp_core_reset_enter(sdev, core_mask);
128747503b1SLiam Girdwood }
129747503b1SLiam Girdwood 
hda_dsp_core_is_enabled(struct snd_sof_dev * sdev,unsigned int core_mask)130556eb416SPierre-Louis Bossart bool hda_dsp_core_is_enabled(struct snd_sof_dev *sdev, unsigned int core_mask)
131189bf1deSPeter Ujfalusi {
132189bf1deSPeter Ujfalusi 	int val;
133189bf1deSPeter Ujfalusi 	bool is_enable;
134189bf1deSPeter Ujfalusi 
135189bf1deSPeter Ujfalusi 	val = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_REG_ADSPCS);
136189bf1deSPeter Ujfalusi 
137189bf1deSPeter Ujfalusi #define MASK_IS_EQUAL(v, m, field) ({	\
138189bf1deSPeter Ujfalusi 	u32 _m = field(m);		\
139189bf1deSPeter Ujfalusi 	((v) & _m) == _m;		\
140189bf1deSPeter Ujfalusi })
141189bf1deSPeter Ujfalusi 
142189bf1deSPeter Ujfalusi 	is_enable = MASK_IS_EQUAL(val, core_mask, HDA_DSP_ADSPCS_CPA_MASK) &&
143189bf1deSPeter Ujfalusi 		MASK_IS_EQUAL(val, core_mask, HDA_DSP_ADSPCS_SPA_MASK) &&
144189bf1deSPeter Ujfalusi 		!(val & HDA_DSP_ADSPCS_CRST_MASK(core_mask)) &&
145189bf1deSPeter Ujfalusi 		!(val & HDA_DSP_ADSPCS_CSTALL_MASK(core_mask));
146189bf1deSPeter Ujfalusi 
147189bf1deSPeter Ujfalusi #undef MASK_IS_EQUAL
148189bf1deSPeter Ujfalusi 
149189bf1deSPeter Ujfalusi 	dev_dbg(sdev->dev, "DSP core(s) enabled? %d : core_mask %x\n",
150189bf1deSPeter Ujfalusi 		is_enable, core_mask);
151189bf1deSPeter Ujfalusi 
152189bf1deSPeter Ujfalusi 	return is_enable;
153189bf1deSPeter Ujfalusi }
154189bf1deSPeter Ujfalusi 
hda_dsp_core_run(struct snd_sof_dev * sdev,unsigned int core_mask)155747503b1SLiam Girdwood int hda_dsp_core_run(struct snd_sof_dev *sdev, unsigned int core_mask)
156747503b1SLiam Girdwood {
157747503b1SLiam Girdwood 	int ret;
158747503b1SLiam Girdwood 
159747503b1SLiam Girdwood 	/* leave reset state */
160747503b1SLiam Girdwood 	ret = hda_dsp_core_reset_leave(sdev, core_mask);
161747503b1SLiam Girdwood 	if (ret < 0)
162747503b1SLiam Girdwood 		return ret;
163747503b1SLiam Girdwood 
164747503b1SLiam Girdwood 	/* run core */
165747503b1SLiam Girdwood 	dev_dbg(sdev->dev, "unstall/run core: core_mask = %x\n", core_mask);
166747503b1SLiam Girdwood 	snd_sof_dsp_update_bits_unlocked(sdev, HDA_DSP_BAR,
167747503b1SLiam Girdwood 					 HDA_DSP_REG_ADSPCS,
168747503b1SLiam Girdwood 					 HDA_DSP_ADSPCS_CSTALL_MASK(core_mask),
169747503b1SLiam Girdwood 					 0);
170747503b1SLiam Girdwood 
171747503b1SLiam Girdwood 	/* is core now running ? */
172747503b1SLiam Girdwood 	if (!hda_dsp_core_is_enabled(sdev, core_mask)) {
173747503b1SLiam Girdwood 		hda_dsp_core_stall_reset(sdev, core_mask);
174747503b1SLiam Girdwood 		dev_err(sdev->dev, "error: DSP start core failed: core_mask %x\n",
175747503b1SLiam Girdwood 			core_mask);
176747503b1SLiam Girdwood 		ret = -EIO;
177747503b1SLiam Girdwood 	}
178747503b1SLiam Girdwood 
179747503b1SLiam Girdwood 	return ret;
180747503b1SLiam Girdwood }
181747503b1SLiam Girdwood 
182747503b1SLiam Girdwood /*
183747503b1SLiam Girdwood  * Power Management.
184747503b1SLiam Girdwood  */
185747503b1SLiam Girdwood 
hda_dsp_core_power_up(struct snd_sof_dev * sdev,unsigned int core_mask)186537b4a0cSPeter Ujfalusi int hda_dsp_core_power_up(struct snd_sof_dev *sdev, unsigned int core_mask)
187747503b1SLiam Girdwood {
188537b4a0cSPeter Ujfalusi 	struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata;
189537b4a0cSPeter Ujfalusi 	const struct sof_intel_dsp_desc *chip = hda->desc;
190747503b1SLiam Girdwood 	unsigned int cpa;
191747503b1SLiam Girdwood 	u32 adspcs;
192747503b1SLiam Girdwood 	int ret;
193747503b1SLiam Girdwood 
194537b4a0cSPeter Ujfalusi 	/* restrict core_mask to host managed cores mask */
195537b4a0cSPeter Ujfalusi 	core_mask &= chip->host_managed_cores_mask;
196537b4a0cSPeter Ujfalusi 	/* return if core_mask is not valid */
197537b4a0cSPeter Ujfalusi 	if (!core_mask)
198537b4a0cSPeter Ujfalusi 		return 0;
199537b4a0cSPeter Ujfalusi 
200747503b1SLiam Girdwood 	/* update bits */
201747503b1SLiam Girdwood 	snd_sof_dsp_update_bits(sdev, HDA_DSP_BAR, HDA_DSP_REG_ADSPCS,
202747503b1SLiam Girdwood 				HDA_DSP_ADSPCS_SPA_MASK(core_mask),
203747503b1SLiam Girdwood 				HDA_DSP_ADSPCS_SPA_MASK(core_mask));
204747503b1SLiam Girdwood 
205747503b1SLiam Girdwood 	/* poll with timeout to check if operation successful */
206747503b1SLiam Girdwood 	cpa = HDA_DSP_ADSPCS_CPA_MASK(core_mask);
207747503b1SLiam Girdwood 	ret = snd_sof_dsp_read_poll_timeout(sdev, HDA_DSP_BAR,
208747503b1SLiam Girdwood 					    HDA_DSP_REG_ADSPCS, adspcs,
209747503b1SLiam Girdwood 					    (adspcs & cpa) == cpa,
210747503b1SLiam Girdwood 					    HDA_DSP_REG_POLL_INTERVAL_US,
211747503b1SLiam Girdwood 					    HDA_DSP_RESET_TIMEOUT_US);
2126a414489SPierre-Louis Bossart 	if (ret < 0) {
2136a414489SPierre-Louis Bossart 		dev_err(sdev->dev,
2146a414489SPierre-Louis Bossart 			"error: %s: timeout on HDA_DSP_REG_ADSPCS read\n",
2156a414489SPierre-Louis Bossart 			__func__);
2166a414489SPierre-Louis Bossart 		return ret;
2176a414489SPierre-Louis Bossart 	}
218747503b1SLiam Girdwood 
219747503b1SLiam Girdwood 	/* did core power up ? */
220747503b1SLiam Girdwood 	adspcs = snd_sof_dsp_read(sdev, HDA_DSP_BAR,
221747503b1SLiam Girdwood 				  HDA_DSP_REG_ADSPCS);
222747503b1SLiam Girdwood 	if ((adspcs & HDA_DSP_ADSPCS_CPA_MASK(core_mask)) !=
223747503b1SLiam Girdwood 		HDA_DSP_ADSPCS_CPA_MASK(core_mask)) {
224747503b1SLiam Girdwood 		dev_err(sdev->dev,
225747503b1SLiam Girdwood 			"error: power up core failed core_mask %xadspcs 0x%x\n",
226747503b1SLiam Girdwood 			core_mask, adspcs);
227747503b1SLiam Girdwood 		ret = -EIO;
228747503b1SLiam Girdwood 	}
229747503b1SLiam Girdwood 
230747503b1SLiam Girdwood 	return ret;
231747503b1SLiam Girdwood }
232747503b1SLiam Girdwood 
hda_dsp_core_power_down(struct snd_sof_dev * sdev,unsigned int core_mask)233189bf1deSPeter Ujfalusi static int hda_dsp_core_power_down(struct snd_sof_dev *sdev, unsigned int core_mask)
234747503b1SLiam Girdwood {
235747503b1SLiam Girdwood 	u32 adspcs;
2366a414489SPierre-Louis Bossart 	int ret;
237747503b1SLiam Girdwood 
238747503b1SLiam Girdwood 	/* update bits */
239747503b1SLiam Girdwood 	snd_sof_dsp_update_bits_unlocked(sdev, HDA_DSP_BAR,
240747503b1SLiam Girdwood 					 HDA_DSP_REG_ADSPCS,
241747503b1SLiam Girdwood 					 HDA_DSP_ADSPCS_SPA_MASK(core_mask), 0);
242747503b1SLiam Girdwood 
2436a414489SPierre-Louis Bossart 	ret = snd_sof_dsp_read_poll_timeout(sdev, HDA_DSP_BAR,
244747503b1SLiam Girdwood 				HDA_DSP_REG_ADSPCS, adspcs,
245fd829918SPan Xiuli 				!(adspcs & HDA_DSP_ADSPCS_CPA_MASK(core_mask)),
246747503b1SLiam Girdwood 				HDA_DSP_REG_POLL_INTERVAL_US,
247747503b1SLiam Girdwood 				HDA_DSP_PD_TIMEOUT * USEC_PER_MSEC);
2486a414489SPierre-Louis Bossart 	if (ret < 0)
2496a414489SPierre-Louis Bossart 		dev_err(sdev->dev,
2506a414489SPierre-Louis Bossart 			"error: %s: timeout on HDA_DSP_REG_ADSPCS read\n",
2516a414489SPierre-Louis Bossart 			__func__);
2526a414489SPierre-Louis Bossart 
2536a414489SPierre-Louis Bossart 	return ret;
254747503b1SLiam Girdwood }
255747503b1SLiam Girdwood 
hda_dsp_enable_core(struct snd_sof_dev * sdev,unsigned int core_mask)256747503b1SLiam Girdwood int hda_dsp_enable_core(struct snd_sof_dev *sdev, unsigned int core_mask)
257747503b1SLiam Girdwood {
258914fab3bSRanjani Sridharan 	struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata;
259914fab3bSRanjani Sridharan 	const struct sof_intel_dsp_desc *chip = hda->desc;
260747503b1SLiam Girdwood 	int ret;
261747503b1SLiam Girdwood 
262914fab3bSRanjani Sridharan 	/* restrict core_mask to host managed cores mask */
263914fab3bSRanjani Sridharan 	core_mask &= chip->host_managed_cores_mask;
264914fab3bSRanjani Sridharan 
265914fab3bSRanjani Sridharan 	/* return if core_mask is not valid or cores are already enabled */
266914fab3bSRanjani Sridharan 	if (!core_mask || hda_dsp_core_is_enabled(sdev, core_mask))
267747503b1SLiam Girdwood 		return 0;
268747503b1SLiam Girdwood 
269747503b1SLiam Girdwood 	/* power up */
270747503b1SLiam Girdwood 	ret = hda_dsp_core_power_up(sdev, core_mask);
271747503b1SLiam Girdwood 	if (ret < 0) {
272747503b1SLiam Girdwood 		dev_err(sdev->dev, "error: dsp core power up failed: core_mask %x\n",
273747503b1SLiam Girdwood 			core_mask);
274747503b1SLiam Girdwood 		return ret;
275747503b1SLiam Girdwood 	}
276747503b1SLiam Girdwood 
277747503b1SLiam Girdwood 	return hda_dsp_core_run(sdev, core_mask);
278747503b1SLiam Girdwood }
279747503b1SLiam Girdwood 
hda_dsp_core_reset_power_down(struct snd_sof_dev * sdev,unsigned int core_mask)280747503b1SLiam Girdwood int hda_dsp_core_reset_power_down(struct snd_sof_dev *sdev,
281747503b1SLiam Girdwood 				  unsigned int core_mask)
282747503b1SLiam Girdwood {
283914fab3bSRanjani Sridharan 	struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata;
284914fab3bSRanjani Sridharan 	const struct sof_intel_dsp_desc *chip = hda->desc;
285747503b1SLiam Girdwood 	int ret;
286747503b1SLiam Girdwood 
287914fab3bSRanjani Sridharan 	/* restrict core_mask to host managed cores mask */
288914fab3bSRanjani Sridharan 	core_mask &= chip->host_managed_cores_mask;
289914fab3bSRanjani Sridharan 
290914fab3bSRanjani Sridharan 	/* return if core_mask is not valid */
291914fab3bSRanjani Sridharan 	if (!core_mask)
292914fab3bSRanjani Sridharan 		return 0;
293914fab3bSRanjani Sridharan 
294747503b1SLiam Girdwood 	/* place core in reset prior to power down */
295747503b1SLiam Girdwood 	ret = hda_dsp_core_stall_reset(sdev, core_mask);
296747503b1SLiam Girdwood 	if (ret < 0) {
297747503b1SLiam Girdwood 		dev_err(sdev->dev, "error: dsp core reset failed: core_mask %x\n",
298747503b1SLiam Girdwood 			core_mask);
299747503b1SLiam Girdwood 		return ret;
300747503b1SLiam Girdwood 	}
301747503b1SLiam Girdwood 
302747503b1SLiam Girdwood 	/* power down core */
303747503b1SLiam Girdwood 	ret = hda_dsp_core_power_down(sdev, core_mask);
304747503b1SLiam Girdwood 	if (ret < 0) {
305747503b1SLiam Girdwood 		dev_err(sdev->dev, "error: dsp core power down fail mask %x: %d\n",
306747503b1SLiam Girdwood 			core_mask, ret);
307747503b1SLiam Girdwood 		return ret;
308747503b1SLiam Girdwood 	}
309747503b1SLiam Girdwood 
310747503b1SLiam Girdwood 	/* make sure we are in OFF state */
311747503b1SLiam Girdwood 	if (hda_dsp_core_is_enabled(sdev, core_mask)) {
312747503b1SLiam Girdwood 		dev_err(sdev->dev, "error: dsp core disable fail mask %x: %d\n",
313747503b1SLiam Girdwood 			core_mask, ret);
314747503b1SLiam Girdwood 		ret = -EIO;
315747503b1SLiam Girdwood 	}
316747503b1SLiam Girdwood 
317747503b1SLiam Girdwood 	return ret;
318747503b1SLiam Girdwood }
319747503b1SLiam Girdwood 
hda_dsp_ipc_int_enable(struct snd_sof_dev * sdev)320747503b1SLiam Girdwood void hda_dsp_ipc_int_enable(struct snd_sof_dev *sdev)
321747503b1SLiam Girdwood {
322747503b1SLiam Girdwood 	struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata;
323747503b1SLiam Girdwood 	const struct sof_intel_dsp_desc *chip = hda->desc;
324747503b1SLiam Girdwood 
3259fc6786fSPierre-Louis Bossart 	if (sdev->dspless_mode_selected)
3269fc6786fSPierre-Louis Bossart 		return;
3279fc6786fSPierre-Louis Bossart 
328747503b1SLiam Girdwood 	/* enable IPC DONE and BUSY interrupts */
329747503b1SLiam Girdwood 	snd_sof_dsp_update_bits(sdev, HDA_DSP_BAR, chip->ipc_ctl,
330747503b1SLiam Girdwood 			HDA_DSP_REG_HIPCCTL_DONE | HDA_DSP_REG_HIPCCTL_BUSY,
331747503b1SLiam Girdwood 			HDA_DSP_REG_HIPCCTL_DONE | HDA_DSP_REG_HIPCCTL_BUSY);
332747503b1SLiam Girdwood 
333747503b1SLiam Girdwood 	/* enable IPC interrupt */
334747503b1SLiam Girdwood 	snd_sof_dsp_update_bits(sdev, HDA_DSP_BAR, HDA_DSP_REG_ADSPIC,
335747503b1SLiam Girdwood 				HDA_DSP_ADSPIC_IPC, HDA_DSP_ADSPIC_IPC);
336747503b1SLiam Girdwood }
337747503b1SLiam Girdwood 
hda_dsp_ipc_int_disable(struct snd_sof_dev * sdev)338747503b1SLiam Girdwood void hda_dsp_ipc_int_disable(struct snd_sof_dev *sdev)
339747503b1SLiam Girdwood {
340747503b1SLiam Girdwood 	struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata;
341747503b1SLiam Girdwood 	const struct sof_intel_dsp_desc *chip = hda->desc;
342747503b1SLiam Girdwood 
3439fc6786fSPierre-Louis Bossart 	if (sdev->dspless_mode_selected)
3449fc6786fSPierre-Louis Bossart 		return;
3459fc6786fSPierre-Louis Bossart 
346747503b1SLiam Girdwood 	/* disable IPC interrupt */
347747503b1SLiam Girdwood 	snd_sof_dsp_update_bits(sdev, HDA_DSP_BAR, HDA_DSP_REG_ADSPIC,
348747503b1SLiam Girdwood 				HDA_DSP_ADSPIC_IPC, 0);
349747503b1SLiam Girdwood 
350747503b1SLiam Girdwood 	/* disable IPC BUSY and DONE interrupt */
351747503b1SLiam Girdwood 	snd_sof_dsp_update_bits(sdev, HDA_DSP_BAR, chip->ipc_ctl,
352747503b1SLiam Girdwood 			HDA_DSP_REG_HIPCCTL_BUSY | HDA_DSP_REG_HIPCCTL_DONE, 0);
353747503b1SLiam Girdwood }
354747503b1SLiam Girdwood 
hda_dsp_wait_d0i3c_done(struct snd_sof_dev * sdev)35565c56f5dSRanjani Sridharan static int hda_dsp_wait_d0i3c_done(struct snd_sof_dev *sdev)
35662f8f766SKeyon Jie {
35765c56f5dSRanjani Sridharan 	int retry = HDA_DSP_REG_POLL_RETRY_COUNT;
35857f93492SRander Wang 	struct snd_sof_pdata *pdata = sdev->pdata;
35957f93492SRander Wang 	const struct sof_intel_dsp_desc *chip;
36062f8f766SKeyon Jie 
36157f93492SRander Wang 	chip = get_chip_info(pdata);
36257f93492SRander Wang 	while (snd_sof_dsp_read8(sdev, HDA_DSP_HDA_BAR, chip->d0i3_offset) &
36357f93492SRander Wang 		SOF_HDA_VS_D0I3C_CIP) {
36462f8f766SKeyon Jie 		if (!retry--)
36562f8f766SKeyon Jie 			return -ETIMEDOUT;
36662f8f766SKeyon Jie 		usleep_range(10, 15);
36762f8f766SKeyon Jie 	}
36862f8f766SKeyon Jie 
36962f8f766SKeyon Jie 	return 0;
37062f8f766SKeyon Jie }
37162f8f766SKeyon Jie 
hda_dsp_send_pm_gate_ipc(struct snd_sof_dev * sdev,u32 flags)372534037fdSKeyon Jie static int hda_dsp_send_pm_gate_ipc(struct snd_sof_dev *sdev, u32 flags)
373534037fdSKeyon Jie {
3743c168838SRander Wang 	const struct sof_ipc_pm_ops *pm_ops = sof_ipc_get_ops(sdev, pm);
375534037fdSKeyon Jie 
3763c168838SRander Wang 	if (pm_ops && pm_ops->set_pm_gate)
3773c168838SRander Wang 		return pm_ops->set_pm_gate(sdev, flags);
378534037fdSKeyon Jie 
3793c168838SRander Wang 	return 0;
380534037fdSKeyon Jie }
381534037fdSKeyon Jie 
hda_dsp_update_d0i3c_register(struct snd_sof_dev * sdev,u8 value)38261e285caSRanjani Sridharan static int hda_dsp_update_d0i3c_register(struct snd_sof_dev *sdev, u8 value)
38362f8f766SKeyon Jie {
38457f93492SRander Wang 	struct snd_sof_pdata *pdata = sdev->pdata;
38557f93492SRander Wang 	const struct sof_intel_dsp_desc *chip;
38662f8f766SKeyon Jie 	int ret;
38733ac4ca7SPierre-Louis Bossart 	u8 reg;
38862f8f766SKeyon Jie 
38957f93492SRander Wang 	chip = get_chip_info(pdata);
39057f93492SRander Wang 
39162f8f766SKeyon Jie 	/* Write to D0I3C after Command-In-Progress bit is cleared */
39265c56f5dSRanjani Sridharan 	ret = hda_dsp_wait_d0i3c_done(sdev);
39362f8f766SKeyon Jie 	if (ret < 0) {
39457f93492SRander Wang 		dev_err(sdev->dev, "CIP timeout before D0I3C update!\n");
39562f8f766SKeyon Jie 		return ret;
39662f8f766SKeyon Jie 	}
39762f8f766SKeyon Jie 
39862f8f766SKeyon Jie 	/* Update D0I3C register */
39957f93492SRander Wang 	snd_sof_dsp_update8(sdev, HDA_DSP_HDA_BAR, chip->d0i3_offset,
40057f93492SRander Wang 			    SOF_HDA_VS_D0I3C_I3, value);
40162f8f766SKeyon Jie 
40252a55779SRander Wang 	/*
40352a55779SRander Wang 	 * The value written to the D0I3C::I3 bit may not be taken into account immediately.
40452a55779SRander Wang 	 * A delay is recommended before checking if D0I3C::CIP is cleared
40552a55779SRander Wang 	 */
40652a55779SRander Wang 	usleep_range(30, 40);
40752a55779SRander Wang 
40862f8f766SKeyon Jie 	/* Wait for cmd in progress to be cleared before exiting the function */
40965c56f5dSRanjani Sridharan 	ret = hda_dsp_wait_d0i3c_done(sdev);
41062f8f766SKeyon Jie 	if (ret < 0) {
41157f93492SRander Wang 		dev_err(sdev->dev, "CIP timeout after D0I3C update!\n");
41262f8f766SKeyon Jie 		return ret;
41362f8f766SKeyon Jie 	}
41462f8f766SKeyon Jie 
41557f93492SRander Wang 	reg = snd_sof_dsp_read8(sdev, HDA_DSP_HDA_BAR, chip->d0i3_offset);
41652a55779SRander Wang 	/* Confirm d0i3 state changed with paranoia check */
41752a55779SRander Wang 	if ((reg ^ value) & SOF_HDA_VS_D0I3C_I3) {
41852a55779SRander Wang 		dev_err(sdev->dev, "failed to update D0I3C!\n");
41952a55779SRander Wang 		return -EIO;
42052a55779SRander Wang 	}
42152a55779SRander Wang 
42233ac4ca7SPierre-Louis Bossart 	trace_sof_intel_D0I3C_updated(sdev, reg);
42362f8f766SKeyon Jie 
42461e285caSRanjani Sridharan 	return 0;
42561e285caSRanjani Sridharan }
426534037fdSKeyon Jie 
4276611b975SRander Wang /*
4286611b975SRander Wang  * d0i3 streaming is enabled if all the active streams can
4296611b975SRander Wang  * work in d0i3 state and playback is enabled
4306611b975SRander Wang  */
hda_dsp_d0i3_streaming_applicable(struct snd_sof_dev * sdev)4316611b975SRander Wang static bool hda_dsp_d0i3_streaming_applicable(struct snd_sof_dev *sdev)
4326611b975SRander Wang {
4336611b975SRander Wang 	struct snd_pcm_substream *substream;
4346611b975SRander Wang 	struct snd_sof_pcm *spcm;
4356611b975SRander Wang 	bool playback_active = false;
4366611b975SRander Wang 	int dir;
4376611b975SRander Wang 
4386611b975SRander Wang 	list_for_each_entry(spcm, &sdev->pcm_list, list) {
4396611b975SRander Wang 		for_each_pcm_streams(dir) {
4406611b975SRander Wang 			substream = spcm->stream[dir].substream;
4416611b975SRander Wang 			if (!substream || !substream->runtime)
4426611b975SRander Wang 				continue;
4436611b975SRander Wang 
4446611b975SRander Wang 			if (!spcm->stream[dir].d0i3_compatible)
4456611b975SRander Wang 				return false;
4466611b975SRander Wang 
4476611b975SRander Wang 			if (dir == SNDRV_PCM_STREAM_PLAYBACK)
4486611b975SRander Wang 				playback_active = true;
4496611b975SRander Wang 		}
4506611b975SRander Wang 	}
4516611b975SRander Wang 
4526611b975SRander Wang 	return playback_active;
4536611b975SRander Wang }
4546611b975SRander Wang 
hda_dsp_set_D0_state(struct snd_sof_dev * sdev,const struct sof_dsp_power_state * target_state)45561e285caSRanjani Sridharan static int hda_dsp_set_D0_state(struct snd_sof_dev *sdev,
45661e285caSRanjani Sridharan 				const struct sof_dsp_power_state *target_state)
45761e285caSRanjani Sridharan {
45861e285caSRanjani Sridharan 	u32 flags = 0;
45961e285caSRanjani Sridharan 	int ret;
46061e285caSRanjani Sridharan 	u8 value = 0;
46161e285caSRanjani Sridharan 
46261e285caSRanjani Sridharan 	/*
46361e285caSRanjani Sridharan 	 * Sanity check for illegal state transitions
46461e285caSRanjani Sridharan 	 * The only allowed transitions are:
46561e285caSRanjani Sridharan 	 * 1. D3 -> D0I0
46661e285caSRanjani Sridharan 	 * 2. D0I0 -> D0I3
46761e285caSRanjani Sridharan 	 * 3. D0I3 -> D0I0
46861e285caSRanjani Sridharan 	 */
46961e285caSRanjani Sridharan 	switch (sdev->dsp_power_state.state) {
47061e285caSRanjani Sridharan 	case SOF_DSP_PM_D0:
47161e285caSRanjani Sridharan 		/* Follow the sequence below for D0 substate transitions */
47261e285caSRanjani Sridharan 		break;
47361e285caSRanjani Sridharan 	case SOF_DSP_PM_D3:
47461e285caSRanjani Sridharan 		/* Follow regular flow for D3 -> D0 transition */
47561e285caSRanjani Sridharan 		return 0;
47661e285caSRanjani Sridharan 	default:
47761e285caSRanjani Sridharan 		dev_err(sdev->dev, "error: transition from %d to %d not allowed\n",
47861e285caSRanjani Sridharan 			sdev->dsp_power_state.state, target_state->state);
47961e285caSRanjani Sridharan 		return -EINVAL;
48061e285caSRanjani Sridharan 	}
48161e285caSRanjani Sridharan 
48261e285caSRanjani Sridharan 	/* Set flags and register value for D0 target substate */
48361e285caSRanjani Sridharan 	if (target_state->substate == SOF_HDA_DSP_PM_D0I3) {
48461e285caSRanjani Sridharan 		value = SOF_HDA_VS_D0I3C_I3;
48561e285caSRanjani Sridharan 
486851fd873SRanjani Sridharan 		/*
48779560b8aSMarcin Rajwa 		 * Trace DMA need to be disabled when the DSP enters
48879560b8aSMarcin Rajwa 		 * D0I3 for S0Ix suspend, but it can be kept enabled
48979560b8aSMarcin Rajwa 		 * when the DSP enters D0I3 while the system is in S0
49079560b8aSMarcin Rajwa 		 * for debug purpose.
491851fd873SRanjani Sridharan 		 */
49225b17da6SPeter Ujfalusi 		if (!sdev->fw_trace_is_supported ||
49379560b8aSMarcin Rajwa 		    !hda_enable_trace_D0I3_S0 ||
494851fd873SRanjani Sridharan 		    sdev->system_suspend_target != SOF_SUSPEND_NONE)
49561e285caSRanjani Sridharan 			flags = HDA_PM_NO_DMA_TRACE;
4966611b975SRander Wang 
4976611b975SRander Wang 		if (hda_dsp_d0i3_streaming_applicable(sdev))
4986611b975SRander Wang 			flags |= HDA_PM_PG_STREAMING;
49961e285caSRanjani Sridharan 	} else {
50061e285caSRanjani Sridharan 		/* prevent power gating in D0I0 */
50161e285caSRanjani Sridharan 		flags = HDA_PM_PPG;
50261e285caSRanjani Sridharan 	}
50361e285caSRanjani Sridharan 
50461e285caSRanjani Sridharan 	/* update D0I3C register */
50561e285caSRanjani Sridharan 	ret = hda_dsp_update_d0i3c_register(sdev, value);
506534037fdSKeyon Jie 	if (ret < 0)
50761e285caSRanjani Sridharan 		return ret;
50861e285caSRanjani Sridharan 
50961e285caSRanjani Sridharan 	/*
51061e285caSRanjani Sridharan 	 * Notify the DSP of the state change.
51161e285caSRanjani Sridharan 	 * If this IPC fails, revert the D0I3C register update in order
51261e285caSRanjani Sridharan 	 * to prevent partial state change.
51361e285caSRanjani Sridharan 	 */
51461e285caSRanjani Sridharan 	ret = hda_dsp_send_pm_gate_ipc(sdev, flags);
51561e285caSRanjani Sridharan 	if (ret < 0) {
516534037fdSKeyon Jie 		dev_err(sdev->dev,
517534037fdSKeyon Jie 			"error: PM_GATE ipc error %d\n", ret);
51861e285caSRanjani Sridharan 		goto revert;
51961e285caSRanjani Sridharan 	}
52061e285caSRanjani Sridharan 
52161e285caSRanjani Sridharan 	return ret;
52261e285caSRanjani Sridharan 
52361e285caSRanjani Sridharan revert:
52461e285caSRanjani Sridharan 	/* fallback to the previous register value */
52561e285caSRanjani Sridharan 	value = value ? 0 : SOF_HDA_VS_D0I3C_I3;
52661e285caSRanjani Sridharan 
52761e285caSRanjani Sridharan 	/*
52861e285caSRanjani Sridharan 	 * This can fail but return the IPC error to signal that
52961e285caSRanjani Sridharan 	 * the state change failed.
53061e285caSRanjani Sridharan 	 */
53161e285caSRanjani Sridharan 	hda_dsp_update_d0i3c_register(sdev, value);
532534037fdSKeyon Jie 
533534037fdSKeyon Jie 	return ret;
53462f8f766SKeyon Jie }
53562f8f766SKeyon Jie 
53666de6bebSRanjani Sridharan /* helper to log DSP state */
hda_dsp_state_log(struct snd_sof_dev * sdev)53766de6bebSRanjani Sridharan static void hda_dsp_state_log(struct snd_sof_dev *sdev)
53866de6bebSRanjani Sridharan {
53966de6bebSRanjani Sridharan 	switch (sdev->dsp_power_state.state) {
54066de6bebSRanjani Sridharan 	case SOF_DSP_PM_D0:
54166de6bebSRanjani Sridharan 		switch (sdev->dsp_power_state.substate) {
54266de6bebSRanjani Sridharan 		case SOF_HDA_DSP_PM_D0I0:
54366de6bebSRanjani Sridharan 			dev_dbg(sdev->dev, "Current DSP power state: D0I0\n");
54466de6bebSRanjani Sridharan 			break;
54566de6bebSRanjani Sridharan 		case SOF_HDA_DSP_PM_D0I3:
54666de6bebSRanjani Sridharan 			dev_dbg(sdev->dev, "Current DSP power state: D0I3\n");
54766de6bebSRanjani Sridharan 			break;
54866de6bebSRanjani Sridharan 		default:
54966de6bebSRanjani Sridharan 			dev_dbg(sdev->dev, "Unknown DSP D0 substate: %d\n",
55066de6bebSRanjani Sridharan 				sdev->dsp_power_state.substate);
55166de6bebSRanjani Sridharan 			break;
55266de6bebSRanjani Sridharan 		}
55366de6bebSRanjani Sridharan 		break;
55466de6bebSRanjani Sridharan 	case SOF_DSP_PM_D1:
55566de6bebSRanjani Sridharan 		dev_dbg(sdev->dev, "Current DSP power state: D1\n");
55666de6bebSRanjani Sridharan 		break;
55766de6bebSRanjani Sridharan 	case SOF_DSP_PM_D2:
55866de6bebSRanjani Sridharan 		dev_dbg(sdev->dev, "Current DSP power state: D2\n");
55966de6bebSRanjani Sridharan 		break;
56066de6bebSRanjani Sridharan 	case SOF_DSP_PM_D3:
56166de6bebSRanjani Sridharan 		dev_dbg(sdev->dev, "Current DSP power state: D3\n");
56266de6bebSRanjani Sridharan 		break;
56366de6bebSRanjani Sridharan 	default:
56466de6bebSRanjani Sridharan 		dev_dbg(sdev->dev, "Unknown DSP power state: %d\n",
56566de6bebSRanjani Sridharan 			sdev->dsp_power_state.state);
56666de6bebSRanjani Sridharan 		break;
56766de6bebSRanjani Sridharan 	}
56866de6bebSRanjani Sridharan }
56966de6bebSRanjani Sridharan 
57061e285caSRanjani Sridharan /*
57161e285caSRanjani Sridharan  * All DSP power state transitions are initiated by the driver.
57261e285caSRanjani Sridharan  * If the requested state change fails, the error is simply returned.
57361e285caSRanjani Sridharan  * Further state transitions are attempted only when the set_power_save() op
57461e285caSRanjani Sridharan  * is called again either because of a new IPC sent to the DSP or
57561e285caSRanjani Sridharan  * during system suspend/resume.
57661e285caSRanjani Sridharan  */
hda_dsp_set_power_state(struct snd_sof_dev * sdev,const struct sof_dsp_power_state * target_state)577996b07efSRanjani Sridharan static int hda_dsp_set_power_state(struct snd_sof_dev *sdev,
57861e285caSRanjani Sridharan 				   const struct sof_dsp_power_state *target_state)
57961e285caSRanjani Sridharan {
58061e285caSRanjani Sridharan 	int ret = 0;
58161e285caSRanjani Sridharan 
58261e285caSRanjani Sridharan 	switch (target_state->state) {
58361e285caSRanjani Sridharan 	case SOF_DSP_PM_D0:
58461e285caSRanjani Sridharan 		ret = hda_dsp_set_D0_state(sdev, target_state);
58561e285caSRanjani Sridharan 		break;
58661e285caSRanjani Sridharan 	case SOF_DSP_PM_D3:
58761e285caSRanjani Sridharan 		/* The only allowed transition is: D0I0 -> D3 */
58861e285caSRanjani Sridharan 		if (sdev->dsp_power_state.state == SOF_DSP_PM_D0 &&
58961e285caSRanjani Sridharan 		    sdev->dsp_power_state.substate == SOF_HDA_DSP_PM_D0I0)
59061e285caSRanjani Sridharan 			break;
59161e285caSRanjani Sridharan 
59261e285caSRanjani Sridharan 		dev_err(sdev->dev,
59361e285caSRanjani Sridharan 			"error: transition from %d to %d not allowed\n",
59461e285caSRanjani Sridharan 			sdev->dsp_power_state.state, target_state->state);
59561e285caSRanjani Sridharan 		return -EINVAL;
59661e285caSRanjani Sridharan 	default:
59761e285caSRanjani Sridharan 		dev_err(sdev->dev, "error: target state unsupported %d\n",
59861e285caSRanjani Sridharan 			target_state->state);
59961e285caSRanjani Sridharan 		return -EINVAL;
60061e285caSRanjani Sridharan 	}
60161e285caSRanjani Sridharan 	if (ret < 0) {
60261e285caSRanjani Sridharan 		dev_err(sdev->dev,
60361e285caSRanjani Sridharan 			"failed to set requested target DSP state %d substate %d\n",
60461e285caSRanjani Sridharan 			target_state->state, target_state->substate);
60561e285caSRanjani Sridharan 		return ret;
60661e285caSRanjani Sridharan 	}
60761e285caSRanjani Sridharan 
60861e285caSRanjani Sridharan 	sdev->dsp_power_state = *target_state;
60966de6bebSRanjani Sridharan 	hda_dsp_state_log(sdev);
61061e285caSRanjani Sridharan 	return ret;
61161e285caSRanjani Sridharan }
61261e285caSRanjani Sridharan 
hda_dsp_set_power_state_ipc3(struct snd_sof_dev * sdev,const struct sof_dsp_power_state * target_state)613996b07efSRanjani Sridharan int hda_dsp_set_power_state_ipc3(struct snd_sof_dev *sdev,
614996b07efSRanjani Sridharan 				 const struct sof_dsp_power_state *target_state)
615996b07efSRanjani Sridharan {
616996b07efSRanjani Sridharan 	/*
617996b07efSRanjani Sridharan 	 * When the DSP is already in D0I3 and the target state is D0I3,
618996b07efSRanjani Sridharan 	 * it could be the case that the DSP is in D0I3 during S0
619996b07efSRanjani Sridharan 	 * and the system is suspending to S0Ix. Therefore,
620996b07efSRanjani Sridharan 	 * hda_dsp_set_D0_state() must be called to disable trace DMA
621996b07efSRanjani Sridharan 	 * by sending the PM_GATE IPC to the FW.
622996b07efSRanjani Sridharan 	 */
623996b07efSRanjani Sridharan 	if (target_state->substate == SOF_HDA_DSP_PM_D0I3 &&
624996b07efSRanjani Sridharan 	    sdev->system_suspend_target == SOF_SUSPEND_S0IX)
625996b07efSRanjani Sridharan 		return hda_dsp_set_power_state(sdev, target_state);
626996b07efSRanjani Sridharan 
627996b07efSRanjani Sridharan 	/*
628996b07efSRanjani Sridharan 	 * For all other cases, return without doing anything if
629996b07efSRanjani Sridharan 	 * the DSP is already in the target state.
630996b07efSRanjani Sridharan 	 */
631996b07efSRanjani Sridharan 	if (target_state->state == sdev->dsp_power_state.state &&
632996b07efSRanjani Sridharan 	    target_state->substate == sdev->dsp_power_state.substate)
633996b07efSRanjani Sridharan 		return 0;
634996b07efSRanjani Sridharan 
635996b07efSRanjani Sridharan 	return hda_dsp_set_power_state(sdev, target_state);
636996b07efSRanjani Sridharan }
637996b07efSRanjani Sridharan 
hda_dsp_set_power_state_ipc4(struct snd_sof_dev * sdev,const struct sof_dsp_power_state * target_state)638996b07efSRanjani Sridharan int hda_dsp_set_power_state_ipc4(struct snd_sof_dev *sdev,
639996b07efSRanjani Sridharan 				 const struct sof_dsp_power_state *target_state)
640996b07efSRanjani Sridharan {
641996b07efSRanjani Sridharan 	/* Return without doing anything if the DSP is already in the target state */
642996b07efSRanjani Sridharan 	if (target_state->state == sdev->dsp_power_state.state &&
643996b07efSRanjani Sridharan 	    target_state->substate == sdev->dsp_power_state.substate)
644996b07efSRanjani Sridharan 		return 0;
645996b07efSRanjani Sridharan 
646996b07efSRanjani Sridharan 	return hda_dsp_set_power_state(sdev, target_state);
647996b07efSRanjani Sridharan }
648996b07efSRanjani Sridharan 
64961e285caSRanjani Sridharan /*
65061e285caSRanjani Sridharan  * Audio DSP states may transform as below:-
65161e285caSRanjani Sridharan  *
652207bf12fSRanjani Sridharan  *                                         Opportunistic D0I3 in S0
653207bf12fSRanjani Sridharan  *     Runtime    +---------------------+  Delayed D0i3 work timeout
65461e285caSRanjani Sridharan  *     suspend    |                     +--------------------+
655207bf12fSRanjani Sridharan  *   +------------+       D0I0(active)  |                    |
65661e285caSRanjani Sridharan  *   |            |                     <---------------+    |
657207bf12fSRanjani Sridharan  *   |   +-------->                     |    New IPC	|    |
658207bf12fSRanjani Sridharan  *   |   |Runtime +--^--+---------^--+--+ (via mailbox)	|    |
659207bf12fSRanjani Sridharan  *   |   |resume     |  |         |  |			|    |
660207bf12fSRanjani Sridharan  *   |   |           |  |         |  |			|    |
661207bf12fSRanjani Sridharan  *   |   |     System|  |         |  |			|    |
662207bf12fSRanjani Sridharan  *   |   |     resume|  | S3/S0IX |  |                  |    |
663207bf12fSRanjani Sridharan  *   |   |	     |  | suspend |  | S0IX             |    |
66461e285caSRanjani Sridharan  *   |   |           |  |         |  |suspend           |    |
66561e285caSRanjani Sridharan  *   |   |           |  |         |  |                  |    |
66661e285caSRanjani Sridharan  *   |   |           |  |         |  |                  |    |
66761e285caSRanjani Sridharan  * +-v---+-----------+--v-------+ |  |           +------+----v----+
66861e285caSRanjani Sridharan  * |                            | |  +----------->                |
669207bf12fSRanjani Sridharan  * |       D3 (suspended)       | |              |      D0I3      |
670207bf12fSRanjani Sridharan  * |                            | +--------------+                |
671207bf12fSRanjani Sridharan  * |                            |  System resume |                |
672207bf12fSRanjani Sridharan  * +----------------------------+		 +----------------+
67361e285caSRanjani Sridharan  *
674207bf12fSRanjani Sridharan  * S0IX suspend: The DSP is in D0I3 if any D0I3-compatible streams
675207bf12fSRanjani Sridharan  *		 ignored the suspend trigger. Otherwise the DSP
676207bf12fSRanjani Sridharan  *		 is in D3.
67761e285caSRanjani Sridharan  */
67861e285caSRanjani Sridharan 
hda_suspend(struct snd_sof_dev * sdev,bool runtime_suspend)6791c38c922SFred Oh static int hda_suspend(struct snd_sof_dev *sdev, bool runtime_suspend)
680747503b1SLiam Girdwood {
681747503b1SLiam Girdwood 	struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata;
682747503b1SLiam Girdwood 	const struct sof_intel_dsp_desc *chip = hda->desc;
683747503b1SLiam Girdwood 	struct hdac_bus *bus = sof_to_bus(sdev);
684de2a108dSPeter Ujfalusi 	bool imr_lost = false;
685d4165199SRanjani Sridharan 	int ret, j;
686747503b1SLiam Girdwood 
68757724db1SPeter Ujfalusi 	/*
688de2a108dSPeter Ujfalusi 	 * The memory used for IMR boot loses its content in deeper than S3
689de2a108dSPeter Ujfalusi 	 * state on CAVS platforms.
690de2a108dSPeter Ujfalusi 	 * On ACE platforms due to the system architecture the IMR content is
691de2a108dSPeter Ujfalusi 	 * lost at S3 state already, they are tailored for s2idle use.
692de2a108dSPeter Ujfalusi 	 * We must not try IMR boot on next power up in these cases as it will
693de2a108dSPeter Ujfalusi 	 * fail.
694de2a108dSPeter Ujfalusi 	 */
695de2a108dSPeter Ujfalusi 	if (sdev->system_suspend_target > SOF_SUSPEND_S3 ||
696de2a108dSPeter Ujfalusi 	    (chip->hw_ip_version >= SOF_INTEL_ACE_1_0 &&
697de2a108dSPeter Ujfalusi 	     sdev->system_suspend_target == SOF_SUSPEND_S3))
698de2a108dSPeter Ujfalusi 		imr_lost = true;
699de2a108dSPeter Ujfalusi 
700de2a108dSPeter Ujfalusi 	/*
7013b99852fSPeter Ujfalusi 	 * In case of firmware crash or boot failure set the skip_imr_boot to true
7023b99852fSPeter Ujfalusi 	 * as well in order to try to re-load the firmware to do a 'cold' boot.
70357724db1SPeter Ujfalusi 	 */
704de2a108dSPeter Ujfalusi 	if (imr_lost || sdev->fw_state == SOF_FW_CRASHED ||
7053b99852fSPeter Ujfalusi 	    sdev->fw_state == SOF_FW_BOOT_FAILED)
70657724db1SPeter Ujfalusi 		hda->skip_imr_boot = true;
70757724db1SPeter Ujfalusi 
7080fbd539fSRanjani Sridharan 	ret = chip->disable_interrupts(sdev);
7090fbd539fSRanjani Sridharan 	if (ret < 0)
7100fbd539fSRanjani Sridharan 		return ret;
711747503b1SLiam Girdwood 
712*3b2f3606SPeter Ujfalusi 	/* make sure that no irq handler is pending before shutdown */
713*3b2f3606SPeter Ujfalusi 	synchronize_irq(sdev->ipc_irq);
714*3b2f3606SPeter Ujfalusi 
715fd572393SKai Vehmanen 	hda_codec_jack_wake_enable(sdev, runtime_suspend);
716fd15f2f5SRander Wang 
717f402a974SPierre-Louis Bossart 	/* power down all hda links */
718f402a974SPierre-Louis Bossart 	hda_bus_ml_suspend(bus);
719747503b1SLiam Girdwood 
7209fc6786fSPierre-Louis Bossart 	if (sdev->dspless_mode_selected)
7219fc6786fSPierre-Louis Bossart 		goto skip_dsp;
7229fc6786fSPierre-Louis Bossart 
7230fbd539fSRanjani Sridharan 	ret = chip->power_down_dsp(sdev);
724747503b1SLiam Girdwood 	if (ret < 0) {
7250fbd539fSRanjani Sridharan 		dev_err(sdev->dev, "failed to power down DSP during suspend\n");
726747503b1SLiam Girdwood 		return ret;
727747503b1SLiam Girdwood 	}
728747503b1SLiam Girdwood 
729d4165199SRanjani Sridharan 	/* reset ref counts for all cores */
730d4165199SRanjani Sridharan 	for (j = 0; j < chip->cores_num; j++)
731d4165199SRanjani Sridharan 		sdev->dsp_core_ref_count[j] = 0;
732d4165199SRanjani Sridharan 
733747503b1SLiam Girdwood 	/* disable ppcap interrupt */
734747503b1SLiam Girdwood 	hda_dsp_ctrl_ppcap_enable(sdev, false);
735747503b1SLiam Girdwood 	hda_dsp_ctrl_ppcap_int_enable(sdev, false);
7369fc6786fSPierre-Louis Bossart skip_dsp:
737747503b1SLiam Girdwood 
7389a50ee58SZhu Yingjiang 	/* disable hda bus irq and streams */
7399a50ee58SZhu Yingjiang 	hda_dsp_ctrl_stop_chip(sdev);
740747503b1SLiam Girdwood 
741747503b1SLiam Girdwood 	/* disable LP retention mode */
742747503b1SLiam Girdwood 	snd_sof_pci_update_bits(sdev, PCI_PGCTL,
743747503b1SLiam Girdwood 				PCI_PGCTL_LSRMD_MASK, PCI_PGCTL_LSRMD_MASK);
744747503b1SLiam Girdwood 
745747503b1SLiam Girdwood 	/* reset controller */
746747503b1SLiam Girdwood 	ret = hda_dsp_ctrl_link_reset(sdev, true);
747747503b1SLiam Girdwood 	if (ret < 0) {
748747503b1SLiam Girdwood 		dev_err(sdev->dev,
749747503b1SLiam Girdwood 			"error: failed to reset controller during suspend\n");
750747503b1SLiam Girdwood 		return ret;
751747503b1SLiam Girdwood 	}
752747503b1SLiam Girdwood 
753816938b2SKai Vehmanen 	/* display codec can powered off after link reset */
754816938b2SKai Vehmanen 	hda_codec_i915_display_power(sdev, false);
755816938b2SKai Vehmanen 
756747503b1SLiam Girdwood 	return 0;
757747503b1SLiam Girdwood }
758747503b1SLiam Girdwood 
hda_resume(struct snd_sof_dev * sdev,bool runtime_resume)759fd15f2f5SRander Wang static int hda_resume(struct snd_sof_dev *sdev, bool runtime_resume)
760747503b1SLiam Girdwood {
761747503b1SLiam Girdwood 	int ret;
762747503b1SLiam Girdwood 
763816938b2SKai Vehmanen 	/* display codec must be powered before link reset */
764816938b2SKai Vehmanen 	hda_codec_i915_display_power(sdev, true);
765816938b2SKai Vehmanen 
766747503b1SLiam Girdwood 	/*
767747503b1SLiam Girdwood 	 * clear TCSEL to clear playback on some HD Audio
768747503b1SLiam Girdwood 	 * codecs. PCI TCSEL is defined in the Intel manuals.
769747503b1SLiam Girdwood 	 */
770747503b1SLiam Girdwood 	snd_sof_pci_update_bits(sdev, PCI_TCSEL, 0x07, 0);
771747503b1SLiam Girdwood 
772747503b1SLiam Girdwood 	/* reset and start hda controller */
773b48b77d8SPierre-Louis Bossart 	ret = hda_dsp_ctrl_init_chip(sdev);
774747503b1SLiam Girdwood 	if (ret < 0) {
775747503b1SLiam Girdwood 		dev_err(sdev->dev,
776747503b1SLiam Girdwood 			"error: failed to start controller after resume\n");
7771372c768SKai Vehmanen 		goto cleanup;
778747503b1SLiam Girdwood 	}
779747503b1SLiam Girdwood 
780fd15f2f5SRander Wang 	/* check jack status */
78131ba0c07SKai-Heng Feng 	if (runtime_resume) {
78231ba0c07SKai-Heng Feng 		hda_codec_jack_wake_enable(sdev, false);
783ef4d764cSKai-Heng Feng 		if (sdev->system_suspend_target == SOF_SUSPEND_NONE)
784fd15f2f5SRander Wang 			hda_codec_jack_check(sdev);
78531ba0c07SKai-Heng Feng 	}
786747503b1SLiam Girdwood 
7879fc6786fSPierre-Louis Bossart 	if (!sdev->dspless_mode_selected) {
788747503b1SLiam Girdwood 		/* enable ppcap interrupt */
789747503b1SLiam Girdwood 		hda_dsp_ctrl_ppcap_enable(sdev, true);
790747503b1SLiam Girdwood 		hda_dsp_ctrl_ppcap_int_enable(sdev, true);
7919fc6786fSPierre-Louis Bossart 	}
792747503b1SLiam Girdwood 
7931372c768SKai Vehmanen cleanup:
7941372c768SKai Vehmanen 	/* display codec can powered off after controller init */
7951372c768SKai Vehmanen 	hda_codec_i915_display_power(sdev, false);
7961372c768SKai Vehmanen 
797747503b1SLiam Girdwood 	return 0;
798747503b1SLiam Girdwood }
799747503b1SLiam Girdwood 
hda_dsp_resume(struct snd_sof_dev * sdev)800747503b1SLiam Girdwood int hda_dsp_resume(struct snd_sof_dev *sdev)
801747503b1SLiam Girdwood {
80216299326SKeyon Jie 	struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata;
803f402a974SPierre-Louis Bossart 	struct hdac_bus *bus = sof_to_bus(sdev);
80466e40876SKeyon Jie 	struct pci_dev *pci = to_pci_dev(sdev->dev);
80561e285caSRanjani Sridharan 	const struct sof_dsp_power_state target_state = {
80661e285caSRanjani Sridharan 		.state = SOF_DSP_PM_D0,
80761e285caSRanjani Sridharan 		.substate = SOF_HDA_DSP_PM_D0I0,
80861e285caSRanjani Sridharan 	};
80961e285caSRanjani Sridharan 	int ret;
81066e40876SKeyon Jie 
81161e285caSRanjani Sridharan 	/* resume from D0I3 */
81261e285caSRanjani Sridharan 	if (sdev->dsp_power_state.state == SOF_DSP_PM_D0) {
813f402a974SPierre-Louis Bossart 		ret = hda_bus_ml_resume(bus);
814195f1019SMarcin Rajwa 		if (ret < 0) {
8156d5e37b0SPierre-Louis Bossart 			dev_err(sdev->dev,
816ce1f55baSCurtis Malainey 				"error %d in %s: failed to power up links",
817195f1019SMarcin Rajwa 				ret, __func__);
818195f1019SMarcin Rajwa 			return ret;
819195f1019SMarcin Rajwa 		}
820195f1019SMarcin Rajwa 
821195f1019SMarcin Rajwa 		/* set up CORB/RIRB buffers if was on before suspend */
8223400afcfSPierre-Louis Bossart 		hda_codec_resume_cmd_io(sdev);
823195f1019SMarcin Rajwa 
82461e285caSRanjani Sridharan 		/* Set DSP power state */
825787c5214SRanjani Sridharan 		ret = snd_sof_dsp_set_power_state(sdev, &target_state);
82661e285caSRanjani Sridharan 		if (ret < 0) {
82761e285caSRanjani Sridharan 			dev_err(sdev->dev, "error: setting dsp state %d substate %d\n",
82861e285caSRanjani Sridharan 				target_state.state, target_state.substate);
82961e285caSRanjani Sridharan 			return ret;
83061e285caSRanjani Sridharan 		}
83161e285caSRanjani Sridharan 
83216299326SKeyon Jie 		/* restore L1SEN bit */
833ae9db908SRanjani Sridharan 		if (hda->l1_disabled)
83416299326SKeyon Jie 			snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR,
83516299326SKeyon Jie 						HDA_VS_INTEL_EM2,
83616299326SKeyon Jie 						HDA_VS_INTEL_EM2_L1SEN, 0);
83716299326SKeyon Jie 
83866e40876SKeyon Jie 		/* restore and disable the system wakeup */
83966e40876SKeyon Jie 		pci_restore_state(pci);
84066e40876SKeyon Jie 		disable_irq_wake(pci->irq);
84166e40876SKeyon Jie 		return 0;
84266e40876SKeyon Jie 	}
84366e40876SKeyon Jie 
844747503b1SLiam Girdwood 	/* init hda controller. DSP cores will be powered up during fw boot */
84561e285caSRanjani Sridharan 	ret = hda_resume(sdev, false);
84661e285caSRanjani Sridharan 	if (ret < 0)
84761e285caSRanjani Sridharan 		return ret;
84861e285caSRanjani Sridharan 
849787c5214SRanjani Sridharan 	return snd_sof_dsp_set_power_state(sdev, &target_state);
850747503b1SLiam Girdwood }
851747503b1SLiam Girdwood 
hda_dsp_runtime_resume(struct snd_sof_dev * sdev)852747503b1SLiam Girdwood int hda_dsp_runtime_resume(struct snd_sof_dev *sdev)
853747503b1SLiam Girdwood {
85461e285caSRanjani Sridharan 	const struct sof_dsp_power_state target_state = {
85561e285caSRanjani Sridharan 		.state = SOF_DSP_PM_D0,
85661e285caSRanjani Sridharan 	};
85761e285caSRanjani Sridharan 	int ret;
85861e285caSRanjani Sridharan 
859747503b1SLiam Girdwood 	/* init hda controller. DSP cores will be powered up during fw boot */
86061e285caSRanjani Sridharan 	ret = hda_resume(sdev, true);
86161e285caSRanjani Sridharan 	if (ret < 0)
86261e285caSRanjani Sridharan 		return ret;
86361e285caSRanjani Sridharan 
864787c5214SRanjani Sridharan 	return snd_sof_dsp_set_power_state(sdev, &target_state);
865747503b1SLiam Girdwood }
866747503b1SLiam Girdwood 
hda_dsp_runtime_idle(struct snd_sof_dev * sdev)86787a6fe80SKai Vehmanen int hda_dsp_runtime_idle(struct snd_sof_dev *sdev)
86887a6fe80SKai Vehmanen {
86987a6fe80SKai Vehmanen 	struct hdac_bus *hbus = sof_to_bus(sdev);
87087a6fe80SKai Vehmanen 
87187a6fe80SKai Vehmanen 	if (hbus->codec_powered) {
87287a6fe80SKai Vehmanen 		dev_dbg(sdev->dev, "some codecs still powered (%08X), not idle\n",
87387a6fe80SKai Vehmanen 			(unsigned int)hbus->codec_powered);
87487a6fe80SKai Vehmanen 		return -EBUSY;
87587a6fe80SKai Vehmanen 	}
87687a6fe80SKai Vehmanen 
87787a6fe80SKai Vehmanen 	return 0;
87887a6fe80SKai Vehmanen }
87987a6fe80SKai Vehmanen 
hda_dsp_runtime_suspend(struct snd_sof_dev * sdev)8801c38c922SFred Oh int hda_dsp_runtime_suspend(struct snd_sof_dev *sdev)
881747503b1SLiam Girdwood {
8820084364dSRanjani Sridharan 	struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata;
88361e285caSRanjani Sridharan 	const struct sof_dsp_power_state target_state = {
88461e285caSRanjani Sridharan 		.state = SOF_DSP_PM_D3,
88561e285caSRanjani Sridharan 	};
88661e285caSRanjani Sridharan 	int ret;
88761e285caSRanjani Sridharan 
8889fc6786fSPierre-Louis Bossart 	if (!sdev->dspless_mode_selected) {
8890084364dSRanjani Sridharan 		/* cancel any attempt for DSP D0I3 */
8900084364dSRanjani Sridharan 		cancel_delayed_work_sync(&hda->d0i3_work);
8919fc6786fSPierre-Louis Bossart 	}
8920084364dSRanjani Sridharan 
893747503b1SLiam Girdwood 	/* stop hda controller and power dsp off */
89461e285caSRanjani Sridharan 	ret = hda_suspend(sdev, true);
89561e285caSRanjani Sridharan 	if (ret < 0)
89661e285caSRanjani Sridharan 		return ret;
89761e285caSRanjani Sridharan 
898787c5214SRanjani Sridharan 	return snd_sof_dsp_set_power_state(sdev, &target_state);
899747503b1SLiam Girdwood }
900747503b1SLiam Girdwood 
hda_dsp_suspend(struct snd_sof_dev * sdev,u32 target_state)90161e285caSRanjani Sridharan int hda_dsp_suspend(struct snd_sof_dev *sdev, u32 target_state)
902747503b1SLiam Girdwood {
90316299326SKeyon Jie 	struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata;
904747503b1SLiam Girdwood 	struct hdac_bus *bus = sof_to_bus(sdev);
90566e40876SKeyon Jie 	struct pci_dev *pci = to_pci_dev(sdev->dev);
90661e285caSRanjani Sridharan 	const struct sof_dsp_power_state target_dsp_state = {
90761e285caSRanjani Sridharan 		.state = target_state,
90861e285caSRanjani Sridharan 		.substate = target_state == SOF_DSP_PM_D0 ?
90961e285caSRanjani Sridharan 				SOF_HDA_DSP_PM_D0I3 : 0,
91061e285caSRanjani Sridharan 	};
911747503b1SLiam Girdwood 	int ret;
912747503b1SLiam Girdwood 
9139fc6786fSPierre-Louis Bossart 	if (!sdev->dspless_mode_selected) {
91463e51fd3SRanjani Sridharan 		/* cancel any attempt for DSP D0I3 */
91563e51fd3SRanjani Sridharan 		cancel_delayed_work_sync(&hda->d0i3_work);
9169fc6786fSPierre-Louis Bossart 	}
91763e51fd3SRanjani Sridharan 
91861e285caSRanjani Sridharan 	if (target_state == SOF_DSP_PM_D0) {
91961e285caSRanjani Sridharan 		/* Set DSP power state */
920787c5214SRanjani Sridharan 		ret = snd_sof_dsp_set_power_state(sdev, &target_dsp_state);
92161e285caSRanjani Sridharan 		if (ret < 0) {
92261e285caSRanjani Sridharan 			dev_err(sdev->dev, "error: setting dsp state %d substate %d\n",
92361e285caSRanjani Sridharan 				target_dsp_state.state,
92461e285caSRanjani Sridharan 				target_dsp_state.substate);
92561e285caSRanjani Sridharan 			return ret;
92661e285caSRanjani Sridharan 		}
92761e285caSRanjani Sridharan 
92816299326SKeyon Jie 		/* enable L1SEN to make sure the system can enter S0Ix */
929ae9db908SRanjani Sridharan 		if (hda->l1_disabled)
930ae9db908SRanjani Sridharan 			snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR, HDA_VS_INTEL_EM2,
931ae9db908SRanjani Sridharan 						HDA_VS_INTEL_EM2_L1SEN, HDA_VS_INTEL_EM2_L1SEN);
93216299326SKeyon Jie 
933195f1019SMarcin Rajwa 		/* stop the CORB/RIRB DMA if it is On */
9343400afcfSPierre-Louis Bossart 		hda_codec_suspend_cmd_io(sdev);
935195f1019SMarcin Rajwa 
936195f1019SMarcin Rajwa 		/* no link can be powered in s0ix state */
937f402a974SPierre-Louis Bossart 		ret = hda_bus_ml_suspend(bus);
938195f1019SMarcin Rajwa 		if (ret < 0) {
9396d5e37b0SPierre-Louis Bossart 			dev_err(sdev->dev,
940195f1019SMarcin Rajwa 				"error %d in %s: failed to power down links",
941195f1019SMarcin Rajwa 				ret, __func__);
942195f1019SMarcin Rajwa 			return ret;
943195f1019SMarcin Rajwa 		}
944195f1019SMarcin Rajwa 
94566e40876SKeyon Jie 		/* enable the system waking up via IPC IRQ */
94666e40876SKeyon Jie 		enable_irq_wake(pci->irq);
94766e40876SKeyon Jie 		pci_save_state(pci);
94866e40876SKeyon Jie 		return 0;
94966e40876SKeyon Jie 	}
95066e40876SKeyon Jie 
951747503b1SLiam Girdwood 	/* stop hda controller and power dsp off */
9521c38c922SFred Oh 	ret = hda_suspend(sdev, false);
953747503b1SLiam Girdwood 	if (ret < 0) {
954747503b1SLiam Girdwood 		dev_err(bus->dev, "error: suspending dsp\n");
955747503b1SLiam Girdwood 		return ret;
956747503b1SLiam Girdwood 	}
957747503b1SLiam Girdwood 
958787c5214SRanjani Sridharan 	return snd_sof_dsp_set_power_state(sdev, &target_dsp_state);
959747503b1SLiam Girdwood }
960ed3baacdSRanjani Sridharan 
hda_dsp_check_for_dma_streams(struct snd_sof_dev * sdev)9612aa2a5eaSKai Vehmanen static unsigned int hda_dsp_check_for_dma_streams(struct snd_sof_dev *sdev)
9622aa2a5eaSKai Vehmanen {
9632aa2a5eaSKai Vehmanen 	struct hdac_bus *bus = sof_to_bus(sdev);
9642aa2a5eaSKai Vehmanen 	struct hdac_stream *s;
9652aa2a5eaSKai Vehmanen 	unsigned int active_streams = 0;
9662aa2a5eaSKai Vehmanen 	int sd_offset;
9672aa2a5eaSKai Vehmanen 	u32 val;
9682aa2a5eaSKai Vehmanen 
9692aa2a5eaSKai Vehmanen 	list_for_each_entry(s, &bus->stream_list, list) {
9702aa2a5eaSKai Vehmanen 		sd_offset = SOF_STREAM_SD_OFFSET(s);
9712aa2a5eaSKai Vehmanen 		val = snd_sof_dsp_read(sdev, HDA_DSP_HDA_BAR,
9722aa2a5eaSKai Vehmanen 				       sd_offset);
9732aa2a5eaSKai Vehmanen 		if (val & SOF_HDA_SD_CTL_DMA_START)
9742aa2a5eaSKai Vehmanen 			active_streams |= BIT(s->index);
9752aa2a5eaSKai Vehmanen 	}
9762aa2a5eaSKai Vehmanen 
9772aa2a5eaSKai Vehmanen 	return active_streams;
9782aa2a5eaSKai Vehmanen }
9792aa2a5eaSKai Vehmanen 
hda_dsp_s5_quirk(struct snd_sof_dev * sdev)9802aa2a5eaSKai Vehmanen static int hda_dsp_s5_quirk(struct snd_sof_dev *sdev)
9812aa2a5eaSKai Vehmanen {
9822aa2a5eaSKai Vehmanen 	int ret;
9832aa2a5eaSKai Vehmanen 
9842aa2a5eaSKai Vehmanen 	/*
9852aa2a5eaSKai Vehmanen 	 * Do not assume a certain timing between the prior
9862aa2a5eaSKai Vehmanen 	 * suspend flow, and running of this quirk function.
9872aa2a5eaSKai Vehmanen 	 * This is needed if the controller was just put
9882aa2a5eaSKai Vehmanen 	 * to reset before calling this function.
9892aa2a5eaSKai Vehmanen 	 */
9902aa2a5eaSKai Vehmanen 	usleep_range(500, 1000);
9912aa2a5eaSKai Vehmanen 
9922aa2a5eaSKai Vehmanen 	/*
9932aa2a5eaSKai Vehmanen 	 * Take controller out of reset to flush DMA
9942aa2a5eaSKai Vehmanen 	 * transactions.
9952aa2a5eaSKai Vehmanen 	 */
9962aa2a5eaSKai Vehmanen 	ret = hda_dsp_ctrl_link_reset(sdev, false);
9972aa2a5eaSKai Vehmanen 	if (ret < 0)
9982aa2a5eaSKai Vehmanen 		return ret;
9992aa2a5eaSKai Vehmanen 
10002aa2a5eaSKai Vehmanen 	usleep_range(500, 1000);
10012aa2a5eaSKai Vehmanen 
10022aa2a5eaSKai Vehmanen 	/* Restore state for shutdown, back to reset */
10032aa2a5eaSKai Vehmanen 	ret = hda_dsp_ctrl_link_reset(sdev, true);
10042aa2a5eaSKai Vehmanen 	if (ret < 0)
10052aa2a5eaSKai Vehmanen 		return ret;
10062aa2a5eaSKai Vehmanen 
10072aa2a5eaSKai Vehmanen 	return ret;
10082aa2a5eaSKai Vehmanen }
10092aa2a5eaSKai Vehmanen 
hda_dsp_shutdown_dma_flush(struct snd_sof_dev * sdev)10102aa2a5eaSKai Vehmanen int hda_dsp_shutdown_dma_flush(struct snd_sof_dev *sdev)
10112aa2a5eaSKai Vehmanen {
10122aa2a5eaSKai Vehmanen 	unsigned int active_streams;
10132aa2a5eaSKai Vehmanen 	int ret, ret2;
10142aa2a5eaSKai Vehmanen 
10152aa2a5eaSKai Vehmanen 	/* check if DMA cleanup has been successful */
10162aa2a5eaSKai Vehmanen 	active_streams = hda_dsp_check_for_dma_streams(sdev);
10172aa2a5eaSKai Vehmanen 
10182aa2a5eaSKai Vehmanen 	sdev->system_suspend_target = SOF_SUSPEND_S3;
10192aa2a5eaSKai Vehmanen 	ret = snd_sof_suspend(sdev->dev);
10202aa2a5eaSKai Vehmanen 
10212aa2a5eaSKai Vehmanen 	if (active_streams) {
10222aa2a5eaSKai Vehmanen 		dev_warn(sdev->dev,
10232aa2a5eaSKai Vehmanen 			 "There were active DSP streams (%#x) at shutdown, trying to recover\n",
10242aa2a5eaSKai Vehmanen 			 active_streams);
10252aa2a5eaSKai Vehmanen 		ret2 = hda_dsp_s5_quirk(sdev);
10262aa2a5eaSKai Vehmanen 		if (ret2 < 0)
10272aa2a5eaSKai Vehmanen 			dev_err(sdev->dev, "shutdown recovery failed (%d)\n", ret2);
10282aa2a5eaSKai Vehmanen 	}
10292aa2a5eaSKai Vehmanen 
10302aa2a5eaSKai Vehmanen 	return ret;
10312aa2a5eaSKai Vehmanen }
10322aa2a5eaSKai Vehmanen 
hda_dsp_shutdown(struct snd_sof_dev * sdev)103322aa9e02SLibin Yang int hda_dsp_shutdown(struct snd_sof_dev *sdev)
103422aa9e02SLibin Yang {
103522aa9e02SLibin Yang 	sdev->system_suspend_target = SOF_SUSPEND_S3;
103622aa9e02SLibin Yang 	return snd_sof_suspend(sdev->dev);
103722aa9e02SLibin Yang }
103822aa9e02SLibin Yang 
hda_dsp_set_hw_params_upon_resume(struct snd_sof_dev * sdev)10397077a07aSRanjani Sridharan int hda_dsp_set_hw_params_upon_resume(struct snd_sof_dev *sdev)
1040ed3baacdSRanjani Sridharan {
1041f09e9284SPierre-Louis Bossart 	int ret;
10427077a07aSRanjani Sridharan 
1043f09e9284SPierre-Louis Bossart 	/* make sure all DAI resources are freed */
1044f09e9284SPierre-Louis Bossart 	ret = hda_dsp_dais_suspend(sdev);
1045f09e9284SPierre-Louis Bossart 	if (ret < 0)
1046f09e9284SPierre-Louis Bossart 		dev_warn(sdev->dev, "%s: failure in hda_dsp_dais_suspend\n", __func__);
1047a3ebccb5SKai Vehmanen 
1048f09e9284SPierre-Louis Bossart 	return ret;
1049ed3baacdSRanjani Sridharan }
105063e51fd3SRanjani Sridharan 
hda_dsp_d0i3_work(struct work_struct * work)105163e51fd3SRanjani Sridharan void hda_dsp_d0i3_work(struct work_struct *work)
105263e51fd3SRanjani Sridharan {
105363e51fd3SRanjani Sridharan 	struct sof_intel_hda_dev *hdev = container_of(work,
105463e51fd3SRanjani Sridharan 						      struct sof_intel_hda_dev,
105563e51fd3SRanjani Sridharan 						      d0i3_work.work);
105663e51fd3SRanjani Sridharan 	struct hdac_bus *bus = &hdev->hbus.core;
105763e51fd3SRanjani Sridharan 	struct snd_sof_dev *sdev = dev_get_drvdata(bus->dev);
1058f1bb0235SGuennadi Liakhovetski 	struct sof_dsp_power_state target_state = {
1059f1bb0235SGuennadi Liakhovetski 		.state = SOF_DSP_PM_D0,
1060f1bb0235SGuennadi Liakhovetski 		.substate = SOF_HDA_DSP_PM_D0I3,
1061f1bb0235SGuennadi Liakhovetski 	};
106263e51fd3SRanjani Sridharan 	int ret;
106363e51fd3SRanjani Sridharan 
106463e51fd3SRanjani Sridharan 	/* DSP can enter D0I3 iff only D0I3-compatible streams are active */
1065f1bb0235SGuennadi Liakhovetski 	if (!snd_sof_dsp_only_d0i3_compatible_stream_active(sdev))
106663e51fd3SRanjani Sridharan 		/* remain in D0I0 */
106763e51fd3SRanjani Sridharan 		return;
106863e51fd3SRanjani Sridharan 
106963e51fd3SRanjani Sridharan 	/* This can fail but error cannot be propagated */
1070787c5214SRanjani Sridharan 	ret = snd_sof_dsp_set_power_state(sdev, &target_state);
107163e51fd3SRanjani Sridharan 	if (ret < 0)
107263e51fd3SRanjani Sridharan 		dev_err_ratelimited(sdev->dev,
107363e51fd3SRanjani Sridharan 				    "error: failed to set DSP state %d substate %d\n",
107463e51fd3SRanjani Sridharan 				    target_state.state, target_state.substate);
107563e51fd3SRanjani Sridharan }
10769cdcbc9fSRanjani Sridharan 
hda_dsp_core_get(struct snd_sof_dev * sdev,int core)10779cdcbc9fSRanjani Sridharan int hda_dsp_core_get(struct snd_sof_dev *sdev, int core)
10789cdcbc9fSRanjani Sridharan {
10797a567740SPeter Ujfalusi 	const struct sof_ipc_pm_ops *pm_ops = sdev->ipc->ops->pm;
10809cdcbc9fSRanjani Sridharan 	int ret, ret1;
10819cdcbc9fSRanjani Sridharan 
10829cdcbc9fSRanjani Sridharan 	/* power up core */
10839cdcbc9fSRanjani Sridharan 	ret = hda_dsp_enable_core(sdev, BIT(core));
10849cdcbc9fSRanjani Sridharan 	if (ret < 0) {
10859cdcbc9fSRanjani Sridharan 		dev_err(sdev->dev, "failed to power up core %d with err: %d\n",
10869cdcbc9fSRanjani Sridharan 			core, ret);
10879cdcbc9fSRanjani Sridharan 		return ret;
10889cdcbc9fSRanjani Sridharan 	}
10899cdcbc9fSRanjani Sridharan 
10909cdcbc9fSRanjani Sridharan 	/* No need to send IPC for primary core or if FW boot is not complete */
10919cdcbc9fSRanjani Sridharan 	if (sdev->fw_state != SOF_FW_BOOT_COMPLETE || core == SOF_DSP_PRIMARY_CORE)
10929cdcbc9fSRanjani Sridharan 		return 0;
10939cdcbc9fSRanjani Sridharan 
10947a567740SPeter Ujfalusi 	/* No need to continue the set_core_state ops is not available */
10957a567740SPeter Ujfalusi 	if (!pm_ops->set_core_state)
10967a567740SPeter Ujfalusi 		return 0;
10977a567740SPeter Ujfalusi 
10989cdcbc9fSRanjani Sridharan 	/* Now notify DSP for secondary cores */
10997a567740SPeter Ujfalusi 	ret = pm_ops->set_core_state(sdev, core, true);
11009cdcbc9fSRanjani Sridharan 	if (ret < 0) {
11019cdcbc9fSRanjani Sridharan 		dev_err(sdev->dev, "failed to enable secondary core '%d' failed with %d\n",
11029cdcbc9fSRanjani Sridharan 			core, ret);
11039cdcbc9fSRanjani Sridharan 		goto power_down;
11049cdcbc9fSRanjani Sridharan 	}
11059cdcbc9fSRanjani Sridharan 
11069cdcbc9fSRanjani Sridharan 	return ret;
11079cdcbc9fSRanjani Sridharan 
11089cdcbc9fSRanjani Sridharan power_down:
11099cdcbc9fSRanjani Sridharan 	/* power down core if it is host managed and return the original error if this fails too */
11109cdcbc9fSRanjani Sridharan 	ret1 = hda_dsp_core_reset_power_down(sdev, BIT(core));
11119cdcbc9fSRanjani Sridharan 	if (ret1 < 0)
11129cdcbc9fSRanjani Sridharan 		dev_err(sdev->dev, "failed to power down core: %d with err: %d\n", core, ret1);
11139cdcbc9fSRanjani Sridharan 
11149cdcbc9fSRanjani Sridharan 	return ret;
11159cdcbc9fSRanjani Sridharan }
1116b2520dbcSRanjani Sridharan 
hda_dsp_disable_interrupts(struct snd_sof_dev * sdev)1117b2520dbcSRanjani Sridharan int hda_dsp_disable_interrupts(struct snd_sof_dev *sdev)
1118b2520dbcSRanjani Sridharan {
1119b2520dbcSRanjani Sridharan 	hda_sdw_int_enable(sdev, false);
1120b2520dbcSRanjani Sridharan 	hda_dsp_ipc_int_disable(sdev);
1121b2520dbcSRanjani Sridharan 
1122b2520dbcSRanjani Sridharan 	return 0;
1123b2520dbcSRanjani Sridharan }
1124