xref: /openbmc/linux/sound/soc/sof/intel/hda-dsp.c (revision 57f93492410942355b5a6eacbbe977176ffe5110)
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>
21d272b657SBard Liao #include <trace/events/sof_intel.h>
2263e51fd3SRanjani Sridharan #include "../sof-audio.h"
23747503b1SLiam Girdwood #include "../ops.h"
24747503b1SLiam Girdwood #include "hda.h"
25534037fdSKeyon Jie #include "hda-ipc.h"
26747503b1SLiam Girdwood 
27851fd873SRanjani Sridharan static bool hda_enable_trace_D0I3_S0;
28851fd873SRanjani Sridharan #if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG)
29851fd873SRanjani Sridharan module_param_named(enable_trace_D0I3_S0, hda_enable_trace_D0I3_S0, bool, 0444);
30851fd873SRanjani Sridharan MODULE_PARM_DESC(enable_trace_D0I3_S0,
31851fd873SRanjani Sridharan 		 "SOF HDA enable trace when the DSP is in D0I3 in S0");
32851fd873SRanjani Sridharan #endif
33851fd873SRanjani Sridharan 
34747503b1SLiam Girdwood /*
35747503b1SLiam Girdwood  * DSP Core control.
36747503b1SLiam Girdwood  */
37747503b1SLiam Girdwood 
38189bf1deSPeter Ujfalusi static int hda_dsp_core_reset_enter(struct snd_sof_dev *sdev, unsigned int core_mask)
39747503b1SLiam Girdwood {
40747503b1SLiam Girdwood 	u32 adspcs;
41747503b1SLiam Girdwood 	u32 reset;
42747503b1SLiam Girdwood 	int ret;
43747503b1SLiam Girdwood 
44747503b1SLiam Girdwood 	/* set reset bits for cores */
45747503b1SLiam Girdwood 	reset = HDA_DSP_ADSPCS_CRST_MASK(core_mask);
46747503b1SLiam Girdwood 	snd_sof_dsp_update_bits_unlocked(sdev, HDA_DSP_BAR,
47747503b1SLiam Girdwood 					 HDA_DSP_REG_ADSPCS,
48bed5ed64SJulia Lawall 					 reset, reset);
49747503b1SLiam Girdwood 
50747503b1SLiam Girdwood 	/* poll with timeout to check if operation successful */
51747503b1SLiam Girdwood 	ret = snd_sof_dsp_read_poll_timeout(sdev, HDA_DSP_BAR,
52747503b1SLiam Girdwood 					HDA_DSP_REG_ADSPCS, adspcs,
53747503b1SLiam Girdwood 					((adspcs & reset) == reset),
54747503b1SLiam Girdwood 					HDA_DSP_REG_POLL_INTERVAL_US,
55747503b1SLiam Girdwood 					HDA_DSP_RESET_TIMEOUT_US);
566a414489SPierre-Louis Bossart 	if (ret < 0) {
576a414489SPierre-Louis Bossart 		dev_err(sdev->dev,
586a414489SPierre-Louis Bossart 			"error: %s: timeout on HDA_DSP_REG_ADSPCS read\n",
596a414489SPierre-Louis Bossart 			__func__);
606a414489SPierre-Louis Bossart 		return ret;
616a414489SPierre-Louis Bossart 	}
62747503b1SLiam Girdwood 
63747503b1SLiam Girdwood 	/* has core entered reset ? */
64747503b1SLiam Girdwood 	adspcs = snd_sof_dsp_read(sdev, HDA_DSP_BAR,
65747503b1SLiam Girdwood 				  HDA_DSP_REG_ADSPCS);
66747503b1SLiam Girdwood 	if ((adspcs & HDA_DSP_ADSPCS_CRST_MASK(core_mask)) !=
67747503b1SLiam Girdwood 		HDA_DSP_ADSPCS_CRST_MASK(core_mask)) {
68747503b1SLiam Girdwood 		dev_err(sdev->dev,
69747503b1SLiam Girdwood 			"error: reset enter failed: core_mask %x adspcs 0x%x\n",
70747503b1SLiam Girdwood 			core_mask, adspcs);
71747503b1SLiam Girdwood 		ret = -EIO;
72747503b1SLiam Girdwood 	}
73747503b1SLiam Girdwood 
74747503b1SLiam Girdwood 	return ret;
75747503b1SLiam Girdwood }
76747503b1SLiam Girdwood 
77189bf1deSPeter Ujfalusi static int hda_dsp_core_reset_leave(struct snd_sof_dev *sdev, unsigned int core_mask)
78747503b1SLiam Girdwood {
79747503b1SLiam Girdwood 	unsigned int crst;
80747503b1SLiam Girdwood 	u32 adspcs;
81747503b1SLiam Girdwood 	int ret;
82747503b1SLiam Girdwood 
83747503b1SLiam Girdwood 	/* clear reset bits for cores */
84747503b1SLiam Girdwood 	snd_sof_dsp_update_bits_unlocked(sdev, HDA_DSP_BAR,
85747503b1SLiam Girdwood 					 HDA_DSP_REG_ADSPCS,
86747503b1SLiam Girdwood 					 HDA_DSP_ADSPCS_CRST_MASK(core_mask),
87747503b1SLiam Girdwood 					 0);
88747503b1SLiam Girdwood 
89747503b1SLiam Girdwood 	/* poll with timeout to check if operation successful */
90747503b1SLiam Girdwood 	crst = HDA_DSP_ADSPCS_CRST_MASK(core_mask);
91747503b1SLiam Girdwood 	ret = snd_sof_dsp_read_poll_timeout(sdev, HDA_DSP_BAR,
92747503b1SLiam Girdwood 					    HDA_DSP_REG_ADSPCS, adspcs,
93747503b1SLiam Girdwood 					    !(adspcs & crst),
94747503b1SLiam Girdwood 					    HDA_DSP_REG_POLL_INTERVAL_US,
95747503b1SLiam Girdwood 					    HDA_DSP_RESET_TIMEOUT_US);
96747503b1SLiam Girdwood 
976a414489SPierre-Louis Bossart 	if (ret < 0) {
986a414489SPierre-Louis Bossart 		dev_err(sdev->dev,
996a414489SPierre-Louis Bossart 			"error: %s: timeout on HDA_DSP_REG_ADSPCS read\n",
1006a414489SPierre-Louis Bossart 			__func__);
1016a414489SPierre-Louis Bossart 		return ret;
1026a414489SPierre-Louis Bossart 	}
1036a414489SPierre-Louis Bossart 
104747503b1SLiam Girdwood 	/* has core left reset ? */
105747503b1SLiam Girdwood 	adspcs = snd_sof_dsp_read(sdev, HDA_DSP_BAR,
106747503b1SLiam Girdwood 				  HDA_DSP_REG_ADSPCS);
107747503b1SLiam Girdwood 	if ((adspcs & HDA_DSP_ADSPCS_CRST_MASK(core_mask)) != 0) {
108747503b1SLiam Girdwood 		dev_err(sdev->dev,
109747503b1SLiam Girdwood 			"error: reset leave failed: core_mask %x adspcs 0x%x\n",
110747503b1SLiam Girdwood 			core_mask, adspcs);
111747503b1SLiam Girdwood 		ret = -EIO;
112747503b1SLiam Girdwood 	}
113747503b1SLiam Girdwood 
114747503b1SLiam Girdwood 	return ret;
115747503b1SLiam Girdwood }
116747503b1SLiam Girdwood 
117556eb416SPierre-Louis Bossart int hda_dsp_core_stall_reset(struct snd_sof_dev *sdev, unsigned int core_mask)
118747503b1SLiam Girdwood {
119747503b1SLiam Girdwood 	/* stall core */
120747503b1SLiam Girdwood 	snd_sof_dsp_update_bits_unlocked(sdev, HDA_DSP_BAR,
121747503b1SLiam Girdwood 					 HDA_DSP_REG_ADSPCS,
122747503b1SLiam Girdwood 					 HDA_DSP_ADSPCS_CSTALL_MASK(core_mask),
123747503b1SLiam Girdwood 					 HDA_DSP_ADSPCS_CSTALL_MASK(core_mask));
124747503b1SLiam Girdwood 
125747503b1SLiam Girdwood 	/* set reset state */
126747503b1SLiam Girdwood 	return hda_dsp_core_reset_enter(sdev, core_mask);
127747503b1SLiam Girdwood }
128747503b1SLiam Girdwood 
129556eb416SPierre-Louis Bossart bool hda_dsp_core_is_enabled(struct snd_sof_dev *sdev, unsigned int core_mask)
130189bf1deSPeter Ujfalusi {
131189bf1deSPeter Ujfalusi 	int val;
132189bf1deSPeter Ujfalusi 	bool is_enable;
133189bf1deSPeter Ujfalusi 
134189bf1deSPeter Ujfalusi 	val = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_REG_ADSPCS);
135189bf1deSPeter Ujfalusi 
136189bf1deSPeter Ujfalusi #define MASK_IS_EQUAL(v, m, field) ({	\
137189bf1deSPeter Ujfalusi 	u32 _m = field(m);		\
138189bf1deSPeter Ujfalusi 	((v) & _m) == _m;		\
139189bf1deSPeter Ujfalusi })
140189bf1deSPeter Ujfalusi 
141189bf1deSPeter Ujfalusi 	is_enable = MASK_IS_EQUAL(val, core_mask, HDA_DSP_ADSPCS_CPA_MASK) &&
142189bf1deSPeter Ujfalusi 		MASK_IS_EQUAL(val, core_mask, HDA_DSP_ADSPCS_SPA_MASK) &&
143189bf1deSPeter Ujfalusi 		!(val & HDA_DSP_ADSPCS_CRST_MASK(core_mask)) &&
144189bf1deSPeter Ujfalusi 		!(val & HDA_DSP_ADSPCS_CSTALL_MASK(core_mask));
145189bf1deSPeter Ujfalusi 
146189bf1deSPeter Ujfalusi #undef MASK_IS_EQUAL
147189bf1deSPeter Ujfalusi 
148189bf1deSPeter Ujfalusi 	dev_dbg(sdev->dev, "DSP core(s) enabled? %d : core_mask %x\n",
149189bf1deSPeter Ujfalusi 		is_enable, core_mask);
150189bf1deSPeter Ujfalusi 
151189bf1deSPeter Ujfalusi 	return is_enable;
152189bf1deSPeter Ujfalusi }
153189bf1deSPeter Ujfalusi 
154747503b1SLiam Girdwood int hda_dsp_core_run(struct snd_sof_dev *sdev, unsigned int core_mask)
155747503b1SLiam Girdwood {
156747503b1SLiam Girdwood 	int ret;
157747503b1SLiam Girdwood 
158747503b1SLiam Girdwood 	/* leave reset state */
159747503b1SLiam Girdwood 	ret = hda_dsp_core_reset_leave(sdev, core_mask);
160747503b1SLiam Girdwood 	if (ret < 0)
161747503b1SLiam Girdwood 		return ret;
162747503b1SLiam Girdwood 
163747503b1SLiam Girdwood 	/* run core */
164747503b1SLiam Girdwood 	dev_dbg(sdev->dev, "unstall/run core: core_mask = %x\n", core_mask);
165747503b1SLiam Girdwood 	snd_sof_dsp_update_bits_unlocked(sdev, HDA_DSP_BAR,
166747503b1SLiam Girdwood 					 HDA_DSP_REG_ADSPCS,
167747503b1SLiam Girdwood 					 HDA_DSP_ADSPCS_CSTALL_MASK(core_mask),
168747503b1SLiam Girdwood 					 0);
169747503b1SLiam Girdwood 
170747503b1SLiam Girdwood 	/* is core now running ? */
171747503b1SLiam Girdwood 	if (!hda_dsp_core_is_enabled(sdev, core_mask)) {
172747503b1SLiam Girdwood 		hda_dsp_core_stall_reset(sdev, core_mask);
173747503b1SLiam Girdwood 		dev_err(sdev->dev, "error: DSP start core failed: core_mask %x\n",
174747503b1SLiam Girdwood 			core_mask);
175747503b1SLiam Girdwood 		ret = -EIO;
176747503b1SLiam Girdwood 	}
177747503b1SLiam Girdwood 
178747503b1SLiam Girdwood 	return ret;
179747503b1SLiam Girdwood }
180747503b1SLiam Girdwood 
181747503b1SLiam Girdwood /*
182747503b1SLiam Girdwood  * Power Management.
183747503b1SLiam Girdwood  */
184747503b1SLiam Girdwood 
185537b4a0cSPeter Ujfalusi int hda_dsp_core_power_up(struct snd_sof_dev *sdev, unsigned int core_mask)
186747503b1SLiam Girdwood {
187537b4a0cSPeter Ujfalusi 	struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata;
188537b4a0cSPeter Ujfalusi 	const struct sof_intel_dsp_desc *chip = hda->desc;
189747503b1SLiam Girdwood 	unsigned int cpa;
190747503b1SLiam Girdwood 	u32 adspcs;
191747503b1SLiam Girdwood 	int ret;
192747503b1SLiam Girdwood 
193537b4a0cSPeter Ujfalusi 	/* restrict core_mask to host managed cores mask */
194537b4a0cSPeter Ujfalusi 	core_mask &= chip->host_managed_cores_mask;
195537b4a0cSPeter Ujfalusi 	/* return if core_mask is not valid */
196537b4a0cSPeter Ujfalusi 	if (!core_mask)
197537b4a0cSPeter Ujfalusi 		return 0;
198537b4a0cSPeter Ujfalusi 
199747503b1SLiam Girdwood 	/* update bits */
200747503b1SLiam Girdwood 	snd_sof_dsp_update_bits(sdev, HDA_DSP_BAR, HDA_DSP_REG_ADSPCS,
201747503b1SLiam Girdwood 				HDA_DSP_ADSPCS_SPA_MASK(core_mask),
202747503b1SLiam Girdwood 				HDA_DSP_ADSPCS_SPA_MASK(core_mask));
203747503b1SLiam Girdwood 
204747503b1SLiam Girdwood 	/* poll with timeout to check if operation successful */
205747503b1SLiam Girdwood 	cpa = HDA_DSP_ADSPCS_CPA_MASK(core_mask);
206747503b1SLiam Girdwood 	ret = snd_sof_dsp_read_poll_timeout(sdev, HDA_DSP_BAR,
207747503b1SLiam Girdwood 					    HDA_DSP_REG_ADSPCS, adspcs,
208747503b1SLiam Girdwood 					    (adspcs & cpa) == cpa,
209747503b1SLiam Girdwood 					    HDA_DSP_REG_POLL_INTERVAL_US,
210747503b1SLiam Girdwood 					    HDA_DSP_RESET_TIMEOUT_US);
2116a414489SPierre-Louis Bossart 	if (ret < 0) {
2126a414489SPierre-Louis Bossart 		dev_err(sdev->dev,
2136a414489SPierre-Louis Bossart 			"error: %s: timeout on HDA_DSP_REG_ADSPCS read\n",
2146a414489SPierre-Louis Bossart 			__func__);
2156a414489SPierre-Louis Bossart 		return ret;
2166a414489SPierre-Louis Bossart 	}
217747503b1SLiam Girdwood 
218747503b1SLiam Girdwood 	/* did core power up ? */
219747503b1SLiam Girdwood 	adspcs = snd_sof_dsp_read(sdev, HDA_DSP_BAR,
220747503b1SLiam Girdwood 				  HDA_DSP_REG_ADSPCS);
221747503b1SLiam Girdwood 	if ((adspcs & HDA_DSP_ADSPCS_CPA_MASK(core_mask)) !=
222747503b1SLiam Girdwood 		HDA_DSP_ADSPCS_CPA_MASK(core_mask)) {
223747503b1SLiam Girdwood 		dev_err(sdev->dev,
224747503b1SLiam Girdwood 			"error: power up core failed core_mask %xadspcs 0x%x\n",
225747503b1SLiam Girdwood 			core_mask, adspcs);
226747503b1SLiam Girdwood 		ret = -EIO;
227747503b1SLiam Girdwood 	}
228747503b1SLiam Girdwood 
229747503b1SLiam Girdwood 	return ret;
230747503b1SLiam Girdwood }
231747503b1SLiam Girdwood 
232189bf1deSPeter Ujfalusi static int hda_dsp_core_power_down(struct snd_sof_dev *sdev, unsigned int core_mask)
233747503b1SLiam Girdwood {
234747503b1SLiam Girdwood 	u32 adspcs;
2356a414489SPierre-Louis Bossart 	int ret;
236747503b1SLiam Girdwood 
237747503b1SLiam Girdwood 	/* update bits */
238747503b1SLiam Girdwood 	snd_sof_dsp_update_bits_unlocked(sdev, HDA_DSP_BAR,
239747503b1SLiam Girdwood 					 HDA_DSP_REG_ADSPCS,
240747503b1SLiam Girdwood 					 HDA_DSP_ADSPCS_SPA_MASK(core_mask), 0);
241747503b1SLiam Girdwood 
2426a414489SPierre-Louis Bossart 	ret = snd_sof_dsp_read_poll_timeout(sdev, HDA_DSP_BAR,
243747503b1SLiam Girdwood 				HDA_DSP_REG_ADSPCS, adspcs,
244fd829918SPan Xiuli 				!(adspcs & HDA_DSP_ADSPCS_CPA_MASK(core_mask)),
245747503b1SLiam Girdwood 				HDA_DSP_REG_POLL_INTERVAL_US,
246747503b1SLiam Girdwood 				HDA_DSP_PD_TIMEOUT * USEC_PER_MSEC);
2476a414489SPierre-Louis Bossart 	if (ret < 0)
2486a414489SPierre-Louis Bossart 		dev_err(sdev->dev,
2496a414489SPierre-Louis Bossart 			"error: %s: timeout on HDA_DSP_REG_ADSPCS read\n",
2506a414489SPierre-Louis Bossart 			__func__);
2516a414489SPierre-Louis Bossart 
2526a414489SPierre-Louis Bossart 	return ret;
253747503b1SLiam Girdwood }
254747503b1SLiam Girdwood 
255747503b1SLiam Girdwood int hda_dsp_enable_core(struct snd_sof_dev *sdev, unsigned int core_mask)
256747503b1SLiam Girdwood {
257914fab3bSRanjani Sridharan 	struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata;
258914fab3bSRanjani Sridharan 	const struct sof_intel_dsp_desc *chip = hda->desc;
259747503b1SLiam Girdwood 	int ret;
260747503b1SLiam Girdwood 
261914fab3bSRanjani Sridharan 	/* restrict core_mask to host managed cores mask */
262914fab3bSRanjani Sridharan 	core_mask &= chip->host_managed_cores_mask;
263914fab3bSRanjani Sridharan 
264914fab3bSRanjani Sridharan 	/* return if core_mask is not valid or cores are already enabled */
265914fab3bSRanjani Sridharan 	if (!core_mask || hda_dsp_core_is_enabled(sdev, core_mask))
266747503b1SLiam Girdwood 		return 0;
267747503b1SLiam Girdwood 
268747503b1SLiam Girdwood 	/* power up */
269747503b1SLiam Girdwood 	ret = hda_dsp_core_power_up(sdev, core_mask);
270747503b1SLiam Girdwood 	if (ret < 0) {
271747503b1SLiam Girdwood 		dev_err(sdev->dev, "error: dsp core power up failed: core_mask %x\n",
272747503b1SLiam Girdwood 			core_mask);
273747503b1SLiam Girdwood 		return ret;
274747503b1SLiam Girdwood 	}
275747503b1SLiam Girdwood 
276747503b1SLiam Girdwood 	return hda_dsp_core_run(sdev, core_mask);
277747503b1SLiam Girdwood }
278747503b1SLiam Girdwood 
279747503b1SLiam Girdwood int hda_dsp_core_reset_power_down(struct snd_sof_dev *sdev,
280747503b1SLiam Girdwood 				  unsigned int core_mask)
281747503b1SLiam Girdwood {
282914fab3bSRanjani Sridharan 	struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata;
283914fab3bSRanjani Sridharan 	const struct sof_intel_dsp_desc *chip = hda->desc;
284747503b1SLiam Girdwood 	int ret;
285747503b1SLiam Girdwood 
286914fab3bSRanjani Sridharan 	/* restrict core_mask to host managed cores mask */
287914fab3bSRanjani Sridharan 	core_mask &= chip->host_managed_cores_mask;
288914fab3bSRanjani Sridharan 
289914fab3bSRanjani Sridharan 	/* return if core_mask is not valid */
290914fab3bSRanjani Sridharan 	if (!core_mask)
291914fab3bSRanjani Sridharan 		return 0;
292914fab3bSRanjani Sridharan 
293747503b1SLiam Girdwood 	/* place core in reset prior to power down */
294747503b1SLiam Girdwood 	ret = hda_dsp_core_stall_reset(sdev, core_mask);
295747503b1SLiam Girdwood 	if (ret < 0) {
296747503b1SLiam Girdwood 		dev_err(sdev->dev, "error: dsp core reset failed: core_mask %x\n",
297747503b1SLiam Girdwood 			core_mask);
298747503b1SLiam Girdwood 		return ret;
299747503b1SLiam Girdwood 	}
300747503b1SLiam Girdwood 
301747503b1SLiam Girdwood 	/* power down core */
302747503b1SLiam Girdwood 	ret = hda_dsp_core_power_down(sdev, core_mask);
303747503b1SLiam Girdwood 	if (ret < 0) {
304747503b1SLiam Girdwood 		dev_err(sdev->dev, "error: dsp core power down fail mask %x: %d\n",
305747503b1SLiam Girdwood 			core_mask, ret);
306747503b1SLiam Girdwood 		return ret;
307747503b1SLiam Girdwood 	}
308747503b1SLiam Girdwood 
309747503b1SLiam Girdwood 	/* make sure we are in OFF state */
310747503b1SLiam Girdwood 	if (hda_dsp_core_is_enabled(sdev, core_mask)) {
311747503b1SLiam Girdwood 		dev_err(sdev->dev, "error: dsp core disable fail mask %x: %d\n",
312747503b1SLiam Girdwood 			core_mask, ret);
313747503b1SLiam Girdwood 		ret = -EIO;
314747503b1SLiam Girdwood 	}
315747503b1SLiam Girdwood 
316747503b1SLiam Girdwood 	return ret;
317747503b1SLiam Girdwood }
318747503b1SLiam Girdwood 
319747503b1SLiam Girdwood void hda_dsp_ipc_int_enable(struct snd_sof_dev *sdev)
320747503b1SLiam Girdwood {
321747503b1SLiam Girdwood 	struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata;
322747503b1SLiam Girdwood 	const struct sof_intel_dsp_desc *chip = hda->desc;
323747503b1SLiam Girdwood 
324747503b1SLiam Girdwood 	/* enable IPC DONE and BUSY interrupts */
325747503b1SLiam Girdwood 	snd_sof_dsp_update_bits(sdev, HDA_DSP_BAR, chip->ipc_ctl,
326747503b1SLiam Girdwood 			HDA_DSP_REG_HIPCCTL_DONE | HDA_DSP_REG_HIPCCTL_BUSY,
327747503b1SLiam Girdwood 			HDA_DSP_REG_HIPCCTL_DONE | HDA_DSP_REG_HIPCCTL_BUSY);
328747503b1SLiam Girdwood 
329747503b1SLiam Girdwood 	/* enable IPC interrupt */
330747503b1SLiam Girdwood 	snd_sof_dsp_update_bits(sdev, HDA_DSP_BAR, HDA_DSP_REG_ADSPIC,
331747503b1SLiam Girdwood 				HDA_DSP_ADSPIC_IPC, HDA_DSP_ADSPIC_IPC);
332747503b1SLiam Girdwood }
333747503b1SLiam Girdwood 
334747503b1SLiam Girdwood void hda_dsp_ipc_int_disable(struct snd_sof_dev *sdev)
335747503b1SLiam Girdwood {
336747503b1SLiam Girdwood 	struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata;
337747503b1SLiam Girdwood 	const struct sof_intel_dsp_desc *chip = hda->desc;
338747503b1SLiam Girdwood 
339747503b1SLiam Girdwood 	/* disable IPC interrupt */
340747503b1SLiam Girdwood 	snd_sof_dsp_update_bits(sdev, HDA_DSP_BAR, HDA_DSP_REG_ADSPIC,
341747503b1SLiam Girdwood 				HDA_DSP_ADSPIC_IPC, 0);
342747503b1SLiam Girdwood 
343747503b1SLiam Girdwood 	/* disable IPC BUSY and DONE interrupt */
344747503b1SLiam Girdwood 	snd_sof_dsp_update_bits(sdev, HDA_DSP_BAR, chip->ipc_ctl,
345747503b1SLiam Girdwood 			HDA_DSP_REG_HIPCCTL_BUSY | HDA_DSP_REG_HIPCCTL_DONE, 0);
346747503b1SLiam Girdwood }
347747503b1SLiam Girdwood 
34865c56f5dSRanjani Sridharan static int hda_dsp_wait_d0i3c_done(struct snd_sof_dev *sdev)
34962f8f766SKeyon Jie {
35065c56f5dSRanjani Sridharan 	int retry = HDA_DSP_REG_POLL_RETRY_COUNT;
351*57f93492SRander Wang 	struct snd_sof_pdata *pdata = sdev->pdata;
352*57f93492SRander Wang 	const struct sof_intel_dsp_desc *chip;
35362f8f766SKeyon Jie 
354*57f93492SRander Wang 	chip = get_chip_info(pdata);
355*57f93492SRander Wang 	while (snd_sof_dsp_read8(sdev, HDA_DSP_HDA_BAR, chip->d0i3_offset) &
356*57f93492SRander Wang 		SOF_HDA_VS_D0I3C_CIP) {
35762f8f766SKeyon Jie 		if (!retry--)
35862f8f766SKeyon Jie 			return -ETIMEDOUT;
35962f8f766SKeyon Jie 		usleep_range(10, 15);
36062f8f766SKeyon Jie 	}
36162f8f766SKeyon Jie 
36262f8f766SKeyon Jie 	return 0;
36362f8f766SKeyon Jie }
36462f8f766SKeyon Jie 
365534037fdSKeyon Jie static int hda_dsp_send_pm_gate_ipc(struct snd_sof_dev *sdev, u32 flags)
366534037fdSKeyon Jie {
367534037fdSKeyon Jie 	struct sof_ipc_pm_gate pm_gate;
368534037fdSKeyon Jie 	struct sof_ipc_reply reply;
369534037fdSKeyon Jie 
370534037fdSKeyon Jie 	memset(&pm_gate, 0, sizeof(pm_gate));
371534037fdSKeyon Jie 
372534037fdSKeyon Jie 	/* configure pm_gate ipc message */
373534037fdSKeyon Jie 	pm_gate.hdr.size = sizeof(pm_gate);
374534037fdSKeyon Jie 	pm_gate.hdr.cmd = SOF_IPC_GLB_PM_MSG | SOF_IPC_PM_GATE;
375534037fdSKeyon Jie 	pm_gate.flags = flags;
376534037fdSKeyon Jie 
377534037fdSKeyon Jie 	/* send pm_gate ipc to dsp */
3782a51c0f8SPeter Ujfalusi 	return sof_ipc_tx_message_no_pm(sdev->ipc, &pm_gate, sizeof(pm_gate),
3792a51c0f8SPeter Ujfalusi 					&reply, sizeof(reply));
380534037fdSKeyon Jie }
381534037fdSKeyon Jie 
38261e285caSRanjani Sridharan static int hda_dsp_update_d0i3c_register(struct snd_sof_dev *sdev, u8 value)
38362f8f766SKeyon Jie {
384*57f93492SRander Wang 	struct snd_sof_pdata *pdata = sdev->pdata;
385*57f93492SRander Wang 	const struct sof_intel_dsp_desc *chip;
38662f8f766SKeyon Jie 	int ret;
38733ac4ca7SPierre-Louis Bossart 	u8 reg;
38862f8f766SKeyon Jie 
389*57f93492SRander Wang 	chip = get_chip_info(pdata);
390*57f93492SRander 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) {
394*57f93492SRander Wang 		dev_err(sdev->dev, "CIP timeout before D0I3C update!\n");
39562f8f766SKeyon Jie 		return ret;
39662f8f766SKeyon Jie 	}
39762f8f766SKeyon Jie 
39862f8f766SKeyon Jie 	/* Update D0I3C register */
399*57f93492SRander Wang 	snd_sof_dsp_update8(sdev, HDA_DSP_HDA_BAR, chip->d0i3_offset,
400*57f93492SRander Wang 			    SOF_HDA_VS_D0I3C_I3, value);
40162f8f766SKeyon Jie 
40262f8f766SKeyon Jie 	/* Wait for cmd in progress to be cleared before exiting the function */
40365c56f5dSRanjani Sridharan 	ret = hda_dsp_wait_d0i3c_done(sdev);
40462f8f766SKeyon Jie 	if (ret < 0) {
405*57f93492SRander Wang 		dev_err(sdev->dev, "CIP timeout after D0I3C update!\n");
40662f8f766SKeyon Jie 		return ret;
40762f8f766SKeyon Jie 	}
40862f8f766SKeyon Jie 
409*57f93492SRander Wang 	reg = snd_sof_dsp_read8(sdev, HDA_DSP_HDA_BAR, chip->d0i3_offset);
41033ac4ca7SPierre-Louis Bossart 	trace_sof_intel_D0I3C_updated(sdev, reg);
41162f8f766SKeyon Jie 
41261e285caSRanjani Sridharan 	return 0;
41361e285caSRanjani Sridharan }
414534037fdSKeyon Jie 
41561e285caSRanjani Sridharan static int hda_dsp_set_D0_state(struct snd_sof_dev *sdev,
41661e285caSRanjani Sridharan 				const struct sof_dsp_power_state *target_state)
41761e285caSRanjani Sridharan {
41861e285caSRanjani Sridharan 	u32 flags = 0;
41961e285caSRanjani Sridharan 	int ret;
42061e285caSRanjani Sridharan 	u8 value = 0;
42161e285caSRanjani Sridharan 
42261e285caSRanjani Sridharan 	/*
42361e285caSRanjani Sridharan 	 * Sanity check for illegal state transitions
42461e285caSRanjani Sridharan 	 * The only allowed transitions are:
42561e285caSRanjani Sridharan 	 * 1. D3 -> D0I0
42661e285caSRanjani Sridharan 	 * 2. D0I0 -> D0I3
42761e285caSRanjani Sridharan 	 * 3. D0I3 -> D0I0
42861e285caSRanjani Sridharan 	 */
42961e285caSRanjani Sridharan 	switch (sdev->dsp_power_state.state) {
43061e285caSRanjani Sridharan 	case SOF_DSP_PM_D0:
43161e285caSRanjani Sridharan 		/* Follow the sequence below for D0 substate transitions */
43261e285caSRanjani Sridharan 		break;
43361e285caSRanjani Sridharan 	case SOF_DSP_PM_D3:
43461e285caSRanjani Sridharan 		/* Follow regular flow for D3 -> D0 transition */
43561e285caSRanjani Sridharan 		return 0;
43661e285caSRanjani Sridharan 	default:
43761e285caSRanjani Sridharan 		dev_err(sdev->dev, "error: transition from %d to %d not allowed\n",
43861e285caSRanjani Sridharan 			sdev->dsp_power_state.state, target_state->state);
43961e285caSRanjani Sridharan 		return -EINVAL;
44061e285caSRanjani Sridharan 	}
44161e285caSRanjani Sridharan 
44261e285caSRanjani Sridharan 	/* Set flags and register value for D0 target substate */
44361e285caSRanjani Sridharan 	if (target_state->substate == SOF_HDA_DSP_PM_D0I3) {
44461e285caSRanjani Sridharan 		value = SOF_HDA_VS_D0I3C_I3;
44561e285caSRanjani Sridharan 
446851fd873SRanjani Sridharan 		/*
44779560b8aSMarcin Rajwa 		 * Trace DMA need to be disabled when the DSP enters
44879560b8aSMarcin Rajwa 		 * D0I3 for S0Ix suspend, but it can be kept enabled
44979560b8aSMarcin Rajwa 		 * when the DSP enters D0I3 while the system is in S0
45079560b8aSMarcin Rajwa 		 * for debug purpose.
451851fd873SRanjani Sridharan 		 */
45225b17da6SPeter Ujfalusi 		if (!sdev->fw_trace_is_supported ||
45379560b8aSMarcin Rajwa 		    !hda_enable_trace_D0I3_S0 ||
454851fd873SRanjani Sridharan 		    sdev->system_suspend_target != SOF_SUSPEND_NONE)
45561e285caSRanjani Sridharan 			flags = HDA_PM_NO_DMA_TRACE;
45661e285caSRanjani Sridharan 	} else {
45761e285caSRanjani Sridharan 		/* prevent power gating in D0I0 */
45861e285caSRanjani Sridharan 		flags = HDA_PM_PPG;
45961e285caSRanjani Sridharan 	}
46061e285caSRanjani Sridharan 
46161e285caSRanjani Sridharan 	/* update D0I3C register */
46261e285caSRanjani Sridharan 	ret = hda_dsp_update_d0i3c_register(sdev, value);
463534037fdSKeyon Jie 	if (ret < 0)
46461e285caSRanjani Sridharan 		return ret;
46561e285caSRanjani Sridharan 
46661e285caSRanjani Sridharan 	/*
46761e285caSRanjani Sridharan 	 * Notify the DSP of the state change.
46861e285caSRanjani Sridharan 	 * If this IPC fails, revert the D0I3C register update in order
46961e285caSRanjani Sridharan 	 * to prevent partial state change.
47061e285caSRanjani Sridharan 	 */
47161e285caSRanjani Sridharan 	ret = hda_dsp_send_pm_gate_ipc(sdev, flags);
47261e285caSRanjani Sridharan 	if (ret < 0) {
473534037fdSKeyon Jie 		dev_err(sdev->dev,
474534037fdSKeyon Jie 			"error: PM_GATE ipc error %d\n", ret);
47561e285caSRanjani Sridharan 		goto revert;
47661e285caSRanjani Sridharan 	}
47761e285caSRanjani Sridharan 
47861e285caSRanjani Sridharan 	return ret;
47961e285caSRanjani Sridharan 
48061e285caSRanjani Sridharan revert:
48161e285caSRanjani Sridharan 	/* fallback to the previous register value */
48261e285caSRanjani Sridharan 	value = value ? 0 : SOF_HDA_VS_D0I3C_I3;
48361e285caSRanjani Sridharan 
48461e285caSRanjani Sridharan 	/*
48561e285caSRanjani Sridharan 	 * This can fail but return the IPC error to signal that
48661e285caSRanjani Sridharan 	 * the state change failed.
48761e285caSRanjani Sridharan 	 */
48861e285caSRanjani Sridharan 	hda_dsp_update_d0i3c_register(sdev, value);
489534037fdSKeyon Jie 
490534037fdSKeyon Jie 	return ret;
49162f8f766SKeyon Jie }
49262f8f766SKeyon Jie 
49366de6bebSRanjani Sridharan /* helper to log DSP state */
49466de6bebSRanjani Sridharan static void hda_dsp_state_log(struct snd_sof_dev *sdev)
49566de6bebSRanjani Sridharan {
49666de6bebSRanjani Sridharan 	switch (sdev->dsp_power_state.state) {
49766de6bebSRanjani Sridharan 	case SOF_DSP_PM_D0:
49866de6bebSRanjani Sridharan 		switch (sdev->dsp_power_state.substate) {
49966de6bebSRanjani Sridharan 		case SOF_HDA_DSP_PM_D0I0:
50066de6bebSRanjani Sridharan 			dev_dbg(sdev->dev, "Current DSP power state: D0I0\n");
50166de6bebSRanjani Sridharan 			break;
50266de6bebSRanjani Sridharan 		case SOF_HDA_DSP_PM_D0I3:
50366de6bebSRanjani Sridharan 			dev_dbg(sdev->dev, "Current DSP power state: D0I3\n");
50466de6bebSRanjani Sridharan 			break;
50566de6bebSRanjani Sridharan 		default:
50666de6bebSRanjani Sridharan 			dev_dbg(sdev->dev, "Unknown DSP D0 substate: %d\n",
50766de6bebSRanjani Sridharan 				sdev->dsp_power_state.substate);
50866de6bebSRanjani Sridharan 			break;
50966de6bebSRanjani Sridharan 		}
51066de6bebSRanjani Sridharan 		break;
51166de6bebSRanjani Sridharan 	case SOF_DSP_PM_D1:
51266de6bebSRanjani Sridharan 		dev_dbg(sdev->dev, "Current DSP power state: D1\n");
51366de6bebSRanjani Sridharan 		break;
51466de6bebSRanjani Sridharan 	case SOF_DSP_PM_D2:
51566de6bebSRanjani Sridharan 		dev_dbg(sdev->dev, "Current DSP power state: D2\n");
51666de6bebSRanjani Sridharan 		break;
51766de6bebSRanjani Sridharan 	case SOF_DSP_PM_D3:
51866de6bebSRanjani Sridharan 		dev_dbg(sdev->dev, "Current DSP power state: D3\n");
51966de6bebSRanjani Sridharan 		break;
52066de6bebSRanjani Sridharan 	default:
52166de6bebSRanjani Sridharan 		dev_dbg(sdev->dev, "Unknown DSP power state: %d\n",
52266de6bebSRanjani Sridharan 			sdev->dsp_power_state.state);
52366de6bebSRanjani Sridharan 		break;
52466de6bebSRanjani Sridharan 	}
52566de6bebSRanjani Sridharan }
52666de6bebSRanjani Sridharan 
52761e285caSRanjani Sridharan /*
52861e285caSRanjani Sridharan  * All DSP power state transitions are initiated by the driver.
52961e285caSRanjani Sridharan  * If the requested state change fails, the error is simply returned.
53061e285caSRanjani Sridharan  * Further state transitions are attempted only when the set_power_save() op
53161e285caSRanjani Sridharan  * is called again either because of a new IPC sent to the DSP or
53261e285caSRanjani Sridharan  * during system suspend/resume.
53361e285caSRanjani Sridharan  */
53461e285caSRanjani Sridharan int hda_dsp_set_power_state(struct snd_sof_dev *sdev,
53561e285caSRanjani Sridharan 			    const struct sof_dsp_power_state *target_state)
53661e285caSRanjani Sridharan {
53761e285caSRanjani Sridharan 	int ret = 0;
53861e285caSRanjani Sridharan 
539851fd873SRanjani Sridharan 	/*
540851fd873SRanjani Sridharan 	 * When the DSP is already in D0I3 and the target state is D0I3,
541851fd873SRanjani Sridharan 	 * it could be the case that the DSP is in D0I3 during S0
542851fd873SRanjani Sridharan 	 * and the system is suspending to S0Ix. Therefore,
543851fd873SRanjani Sridharan 	 * hda_dsp_set_D0_state() must be called to disable trace DMA
544851fd873SRanjani Sridharan 	 * by sending the PM_GATE IPC to the FW.
545851fd873SRanjani Sridharan 	 */
546851fd873SRanjani Sridharan 	if (target_state->substate == SOF_HDA_DSP_PM_D0I3 &&
547851fd873SRanjani Sridharan 	    sdev->system_suspend_target == SOF_SUSPEND_S0IX)
548851fd873SRanjani Sridharan 		goto set_state;
549851fd873SRanjani Sridharan 
550851fd873SRanjani Sridharan 	/*
551851fd873SRanjani Sridharan 	 * For all other cases, return without doing anything if
552851fd873SRanjani Sridharan 	 * the DSP is already in the target state.
553851fd873SRanjani Sridharan 	 */
55461e285caSRanjani Sridharan 	if (target_state->state == sdev->dsp_power_state.state &&
55561e285caSRanjani Sridharan 	    target_state->substate == sdev->dsp_power_state.substate)
55661e285caSRanjani Sridharan 		return 0;
55761e285caSRanjani Sridharan 
558851fd873SRanjani Sridharan set_state:
55961e285caSRanjani Sridharan 	switch (target_state->state) {
56061e285caSRanjani Sridharan 	case SOF_DSP_PM_D0:
56161e285caSRanjani Sridharan 		ret = hda_dsp_set_D0_state(sdev, target_state);
56261e285caSRanjani Sridharan 		break;
56361e285caSRanjani Sridharan 	case SOF_DSP_PM_D3:
56461e285caSRanjani Sridharan 		/* The only allowed transition is: D0I0 -> D3 */
56561e285caSRanjani Sridharan 		if (sdev->dsp_power_state.state == SOF_DSP_PM_D0 &&
56661e285caSRanjani Sridharan 		    sdev->dsp_power_state.substate == SOF_HDA_DSP_PM_D0I0)
56761e285caSRanjani Sridharan 			break;
56861e285caSRanjani Sridharan 
56961e285caSRanjani Sridharan 		dev_err(sdev->dev,
57061e285caSRanjani Sridharan 			"error: transition from %d to %d not allowed\n",
57161e285caSRanjani Sridharan 			sdev->dsp_power_state.state, target_state->state);
57261e285caSRanjani Sridharan 		return -EINVAL;
57361e285caSRanjani Sridharan 	default:
57461e285caSRanjani Sridharan 		dev_err(sdev->dev, "error: target state unsupported %d\n",
57561e285caSRanjani Sridharan 			target_state->state);
57661e285caSRanjani Sridharan 		return -EINVAL;
57761e285caSRanjani Sridharan 	}
57861e285caSRanjani Sridharan 	if (ret < 0) {
57961e285caSRanjani Sridharan 		dev_err(sdev->dev,
58061e285caSRanjani Sridharan 			"failed to set requested target DSP state %d substate %d\n",
58161e285caSRanjani Sridharan 			target_state->state, target_state->substate);
58261e285caSRanjani Sridharan 		return ret;
58361e285caSRanjani Sridharan 	}
58461e285caSRanjani Sridharan 
58561e285caSRanjani Sridharan 	sdev->dsp_power_state = *target_state;
58666de6bebSRanjani Sridharan 	hda_dsp_state_log(sdev);
58761e285caSRanjani Sridharan 	return ret;
58861e285caSRanjani Sridharan }
58961e285caSRanjani Sridharan 
59061e285caSRanjani Sridharan /*
59161e285caSRanjani Sridharan  * Audio DSP states may transform as below:-
59261e285caSRanjani Sridharan  *
593207bf12fSRanjani Sridharan  *                                         Opportunistic D0I3 in S0
594207bf12fSRanjani Sridharan  *     Runtime    +---------------------+  Delayed D0i3 work timeout
59561e285caSRanjani Sridharan  *     suspend    |                     +--------------------+
596207bf12fSRanjani Sridharan  *   +------------+       D0I0(active)  |                    |
59761e285caSRanjani Sridharan  *   |            |                     <---------------+    |
598207bf12fSRanjani Sridharan  *   |   +-------->                     |    New IPC	|    |
599207bf12fSRanjani Sridharan  *   |   |Runtime +--^--+---------^--+--+ (via mailbox)	|    |
600207bf12fSRanjani Sridharan  *   |   |resume     |  |         |  |			|    |
601207bf12fSRanjani Sridharan  *   |   |           |  |         |  |			|    |
602207bf12fSRanjani Sridharan  *   |   |     System|  |         |  |			|    |
603207bf12fSRanjani Sridharan  *   |   |     resume|  | S3/S0IX |  |                  |    |
604207bf12fSRanjani Sridharan  *   |   |	     |  | suspend |  | S0IX             |    |
60561e285caSRanjani Sridharan  *   |   |           |  |         |  |suspend           |    |
60661e285caSRanjani Sridharan  *   |   |           |  |         |  |                  |    |
60761e285caSRanjani Sridharan  *   |   |           |  |         |  |                  |    |
60861e285caSRanjani Sridharan  * +-v---+-----------+--v-------+ |  |           +------+----v----+
60961e285caSRanjani Sridharan  * |                            | |  +----------->                |
610207bf12fSRanjani Sridharan  * |       D3 (suspended)       | |              |      D0I3      |
611207bf12fSRanjani Sridharan  * |                            | +--------------+                |
612207bf12fSRanjani Sridharan  * |                            |  System resume |                |
613207bf12fSRanjani Sridharan  * +----------------------------+		 +----------------+
61461e285caSRanjani Sridharan  *
615207bf12fSRanjani Sridharan  * S0IX suspend: The DSP is in D0I3 if any D0I3-compatible streams
616207bf12fSRanjani Sridharan  *		 ignored the suspend trigger. Otherwise the DSP
617207bf12fSRanjani Sridharan  *		 is in D3.
61861e285caSRanjani Sridharan  */
61961e285caSRanjani Sridharan 
6201c38c922SFred Oh static int hda_suspend(struct snd_sof_dev *sdev, bool runtime_suspend)
621747503b1SLiam Girdwood {
622747503b1SLiam Girdwood 	struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata;
623747503b1SLiam Girdwood 	const struct sof_intel_dsp_desc *chip = hda->desc;
624747503b1SLiam Girdwood 	struct hdac_bus *bus = sof_to_bus(sdev);
625d4165199SRanjani Sridharan 	int ret, j;
626747503b1SLiam Girdwood 
62757724db1SPeter Ujfalusi 	/*
62857724db1SPeter Ujfalusi 	 * The memory used for IMR boot loses its content in deeper than S3 state
62957724db1SPeter Ujfalusi 	 * We must not try IMR boot on next power up (as it will fail).
6303b99852fSPeter Ujfalusi 	 *
6313b99852fSPeter Ujfalusi 	 * In case of firmware crash or boot failure set the skip_imr_boot to true
6323b99852fSPeter Ujfalusi 	 * as well in order to try to re-load the firmware to do a 'cold' boot.
63357724db1SPeter Ujfalusi 	 */
6343b99852fSPeter Ujfalusi 	if (sdev->system_suspend_target > SOF_SUSPEND_S3 ||
6353b99852fSPeter Ujfalusi 	    sdev->fw_state == SOF_FW_CRASHED ||
6363b99852fSPeter Ujfalusi 	    sdev->fw_state == SOF_FW_BOOT_FAILED)
63757724db1SPeter Ujfalusi 		hda->skip_imr_boot = true;
63857724db1SPeter Ujfalusi 
6390fbd539fSRanjani Sridharan 	ret = chip->disable_interrupts(sdev);
6400fbd539fSRanjani Sridharan 	if (ret < 0)
6410fbd539fSRanjani Sridharan 		return ret;
642747503b1SLiam Girdwood 
643fd572393SKai Vehmanen 	hda_codec_jack_wake_enable(sdev, runtime_suspend);
644fd15f2f5SRander Wang 
645f402a974SPierre-Louis Bossart 	/* power down all hda links */
646f402a974SPierre-Louis Bossart 	hda_bus_ml_suspend(bus);
647747503b1SLiam Girdwood 
6480fbd539fSRanjani Sridharan 	ret = chip->power_down_dsp(sdev);
649747503b1SLiam Girdwood 	if (ret < 0) {
6500fbd539fSRanjani Sridharan 		dev_err(sdev->dev, "failed to power down DSP during suspend\n");
651747503b1SLiam Girdwood 		return ret;
652747503b1SLiam Girdwood 	}
653747503b1SLiam Girdwood 
654d4165199SRanjani Sridharan 	/* reset ref counts for all cores */
655d4165199SRanjani Sridharan 	for (j = 0; j < chip->cores_num; j++)
656d4165199SRanjani Sridharan 		sdev->dsp_core_ref_count[j] = 0;
657d4165199SRanjani Sridharan 
658747503b1SLiam Girdwood 	/* disable ppcap interrupt */
659747503b1SLiam Girdwood 	hda_dsp_ctrl_ppcap_enable(sdev, false);
660747503b1SLiam Girdwood 	hda_dsp_ctrl_ppcap_int_enable(sdev, false);
661747503b1SLiam Girdwood 
6629a50ee58SZhu Yingjiang 	/* disable hda bus irq and streams */
6639a50ee58SZhu Yingjiang 	hda_dsp_ctrl_stop_chip(sdev);
664747503b1SLiam Girdwood 
665747503b1SLiam Girdwood 	/* disable LP retention mode */
666747503b1SLiam Girdwood 	snd_sof_pci_update_bits(sdev, PCI_PGCTL,
667747503b1SLiam Girdwood 				PCI_PGCTL_LSRMD_MASK, PCI_PGCTL_LSRMD_MASK);
668747503b1SLiam Girdwood 
669747503b1SLiam Girdwood 	/* reset controller */
670747503b1SLiam Girdwood 	ret = hda_dsp_ctrl_link_reset(sdev, true);
671747503b1SLiam Girdwood 	if (ret < 0) {
672747503b1SLiam Girdwood 		dev_err(sdev->dev,
673747503b1SLiam Girdwood 			"error: failed to reset controller during suspend\n");
674747503b1SLiam Girdwood 		return ret;
675747503b1SLiam Girdwood 	}
676747503b1SLiam Girdwood 
677816938b2SKai Vehmanen 	/* display codec can powered off after link reset */
678816938b2SKai Vehmanen 	hda_codec_i915_display_power(sdev, false);
679816938b2SKai Vehmanen 
680747503b1SLiam Girdwood 	return 0;
681747503b1SLiam Girdwood }
682747503b1SLiam Girdwood 
683fd15f2f5SRander Wang static int hda_resume(struct snd_sof_dev *sdev, bool runtime_resume)
684747503b1SLiam Girdwood {
685747503b1SLiam Girdwood 	int ret;
686747503b1SLiam Girdwood 
687816938b2SKai Vehmanen 	/* display codec must be powered before link reset */
688816938b2SKai Vehmanen 	hda_codec_i915_display_power(sdev, true);
689816938b2SKai Vehmanen 
690747503b1SLiam Girdwood 	/*
691747503b1SLiam Girdwood 	 * clear TCSEL to clear playback on some HD Audio
692747503b1SLiam Girdwood 	 * codecs. PCI TCSEL is defined in the Intel manuals.
693747503b1SLiam Girdwood 	 */
694747503b1SLiam Girdwood 	snd_sof_pci_update_bits(sdev, PCI_TCSEL, 0x07, 0);
695747503b1SLiam Girdwood 
696747503b1SLiam Girdwood 	/* reset and start hda controller */
697b48b77d8SPierre-Louis Bossart 	ret = hda_dsp_ctrl_init_chip(sdev);
698747503b1SLiam Girdwood 	if (ret < 0) {
699747503b1SLiam Girdwood 		dev_err(sdev->dev,
700747503b1SLiam Girdwood 			"error: failed to start controller after resume\n");
7011372c768SKai Vehmanen 		goto cleanup;
702747503b1SLiam Girdwood 	}
703747503b1SLiam Girdwood 
704fd15f2f5SRander Wang 	/* check jack status */
70531ba0c07SKai-Heng Feng 	if (runtime_resume) {
70631ba0c07SKai-Heng Feng 		hda_codec_jack_wake_enable(sdev, false);
707ef4d764cSKai-Heng Feng 		if (sdev->system_suspend_target == SOF_SUSPEND_NONE)
708fd15f2f5SRander Wang 			hda_codec_jack_check(sdev);
70931ba0c07SKai-Heng Feng 	}
710747503b1SLiam Girdwood 
711747503b1SLiam Girdwood 	/* enable ppcap interrupt */
712747503b1SLiam Girdwood 	hda_dsp_ctrl_ppcap_enable(sdev, true);
713747503b1SLiam Girdwood 	hda_dsp_ctrl_ppcap_int_enable(sdev, true);
714747503b1SLiam Girdwood 
7151372c768SKai Vehmanen cleanup:
7161372c768SKai Vehmanen 	/* display codec can powered off after controller init */
7171372c768SKai Vehmanen 	hda_codec_i915_display_power(sdev, false);
7181372c768SKai Vehmanen 
719747503b1SLiam Girdwood 	return 0;
720747503b1SLiam Girdwood }
721747503b1SLiam Girdwood 
722747503b1SLiam Girdwood int hda_dsp_resume(struct snd_sof_dev *sdev)
723747503b1SLiam Girdwood {
72416299326SKeyon Jie 	struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata;
725f402a974SPierre-Louis Bossart 	struct hdac_bus *bus = sof_to_bus(sdev);
72666e40876SKeyon Jie 	struct pci_dev *pci = to_pci_dev(sdev->dev);
72761e285caSRanjani Sridharan 	const struct sof_dsp_power_state target_state = {
72861e285caSRanjani Sridharan 		.state = SOF_DSP_PM_D0,
72961e285caSRanjani Sridharan 		.substate = SOF_HDA_DSP_PM_D0I0,
73061e285caSRanjani Sridharan 	};
73161e285caSRanjani Sridharan 	int ret;
73266e40876SKeyon Jie 
73361e285caSRanjani Sridharan 	/* resume from D0I3 */
73461e285caSRanjani Sridharan 	if (sdev->dsp_power_state.state == SOF_DSP_PM_D0) {
735f402a974SPierre-Louis Bossart 		ret = hda_bus_ml_resume(bus);
736195f1019SMarcin Rajwa 		if (ret < 0) {
7376d5e37b0SPierre-Louis Bossart 			dev_err(sdev->dev,
738ce1f55baSCurtis Malainey 				"error %d in %s: failed to power up links",
739195f1019SMarcin Rajwa 				ret, __func__);
740195f1019SMarcin Rajwa 			return ret;
741195f1019SMarcin Rajwa 		}
742195f1019SMarcin Rajwa 
743195f1019SMarcin Rajwa 		/* set up CORB/RIRB buffers if was on before suspend */
7443400afcfSPierre-Louis Bossart 		hda_codec_resume_cmd_io(sdev);
745195f1019SMarcin Rajwa 
74661e285caSRanjani Sridharan 		/* Set DSP power state */
747787c5214SRanjani Sridharan 		ret = snd_sof_dsp_set_power_state(sdev, &target_state);
74861e285caSRanjani Sridharan 		if (ret < 0) {
74961e285caSRanjani Sridharan 			dev_err(sdev->dev, "error: setting dsp state %d substate %d\n",
75061e285caSRanjani Sridharan 				target_state.state, target_state.substate);
75161e285caSRanjani Sridharan 			return ret;
75261e285caSRanjani Sridharan 		}
75361e285caSRanjani Sridharan 
75416299326SKeyon Jie 		/* restore L1SEN bit */
75516299326SKeyon Jie 		if (hda->l1_support_changed)
75616299326SKeyon Jie 			snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR,
75716299326SKeyon Jie 						HDA_VS_INTEL_EM2,
75816299326SKeyon Jie 						HDA_VS_INTEL_EM2_L1SEN, 0);
75916299326SKeyon Jie 
76066e40876SKeyon Jie 		/* restore and disable the system wakeup */
76166e40876SKeyon Jie 		pci_restore_state(pci);
76266e40876SKeyon Jie 		disable_irq_wake(pci->irq);
76366e40876SKeyon Jie 		return 0;
76466e40876SKeyon Jie 	}
76566e40876SKeyon Jie 
766747503b1SLiam Girdwood 	/* init hda controller. DSP cores will be powered up during fw boot */
76761e285caSRanjani Sridharan 	ret = hda_resume(sdev, false);
76861e285caSRanjani Sridharan 	if (ret < 0)
76961e285caSRanjani Sridharan 		return ret;
77061e285caSRanjani Sridharan 
771787c5214SRanjani Sridharan 	return snd_sof_dsp_set_power_state(sdev, &target_state);
772747503b1SLiam Girdwood }
773747503b1SLiam Girdwood 
774747503b1SLiam Girdwood int hda_dsp_runtime_resume(struct snd_sof_dev *sdev)
775747503b1SLiam Girdwood {
77661e285caSRanjani Sridharan 	const struct sof_dsp_power_state target_state = {
77761e285caSRanjani Sridharan 		.state = SOF_DSP_PM_D0,
77861e285caSRanjani Sridharan 	};
77961e285caSRanjani Sridharan 	int ret;
78061e285caSRanjani Sridharan 
781747503b1SLiam Girdwood 	/* init hda controller. DSP cores will be powered up during fw boot */
78261e285caSRanjani Sridharan 	ret = hda_resume(sdev, true);
78361e285caSRanjani Sridharan 	if (ret < 0)
78461e285caSRanjani Sridharan 		return ret;
78561e285caSRanjani Sridharan 
786787c5214SRanjani Sridharan 	return snd_sof_dsp_set_power_state(sdev, &target_state);
787747503b1SLiam Girdwood }
788747503b1SLiam Girdwood 
78987a6fe80SKai Vehmanen int hda_dsp_runtime_idle(struct snd_sof_dev *sdev)
79087a6fe80SKai Vehmanen {
79187a6fe80SKai Vehmanen 	struct hdac_bus *hbus = sof_to_bus(sdev);
79287a6fe80SKai Vehmanen 
79387a6fe80SKai Vehmanen 	if (hbus->codec_powered) {
79487a6fe80SKai Vehmanen 		dev_dbg(sdev->dev, "some codecs still powered (%08X), not idle\n",
79587a6fe80SKai Vehmanen 			(unsigned int)hbus->codec_powered);
79687a6fe80SKai Vehmanen 		return -EBUSY;
79787a6fe80SKai Vehmanen 	}
79887a6fe80SKai Vehmanen 
79987a6fe80SKai Vehmanen 	return 0;
80087a6fe80SKai Vehmanen }
80187a6fe80SKai Vehmanen 
8021c38c922SFred Oh int hda_dsp_runtime_suspend(struct snd_sof_dev *sdev)
803747503b1SLiam Girdwood {
8040084364dSRanjani Sridharan 	struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata;
80561e285caSRanjani Sridharan 	const struct sof_dsp_power_state target_state = {
80661e285caSRanjani Sridharan 		.state = SOF_DSP_PM_D3,
80761e285caSRanjani Sridharan 	};
80861e285caSRanjani Sridharan 	int ret;
80961e285caSRanjani Sridharan 
8100084364dSRanjani Sridharan 	/* cancel any attempt for DSP D0I3 */
8110084364dSRanjani Sridharan 	cancel_delayed_work_sync(&hda->d0i3_work);
8120084364dSRanjani Sridharan 
813747503b1SLiam Girdwood 	/* stop hda controller and power dsp off */
81461e285caSRanjani Sridharan 	ret = hda_suspend(sdev, true);
81561e285caSRanjani Sridharan 	if (ret < 0)
81661e285caSRanjani Sridharan 		return ret;
81761e285caSRanjani Sridharan 
818787c5214SRanjani Sridharan 	return snd_sof_dsp_set_power_state(sdev, &target_state);
819747503b1SLiam Girdwood }
820747503b1SLiam Girdwood 
82161e285caSRanjani Sridharan int hda_dsp_suspend(struct snd_sof_dev *sdev, u32 target_state)
822747503b1SLiam Girdwood {
82316299326SKeyon Jie 	struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata;
824747503b1SLiam Girdwood 	struct hdac_bus *bus = sof_to_bus(sdev);
82566e40876SKeyon Jie 	struct pci_dev *pci = to_pci_dev(sdev->dev);
82661e285caSRanjani Sridharan 	const struct sof_dsp_power_state target_dsp_state = {
82761e285caSRanjani Sridharan 		.state = target_state,
82861e285caSRanjani Sridharan 		.substate = target_state == SOF_DSP_PM_D0 ?
82961e285caSRanjani Sridharan 				SOF_HDA_DSP_PM_D0I3 : 0,
83061e285caSRanjani Sridharan 	};
831747503b1SLiam Girdwood 	int ret;
832747503b1SLiam Girdwood 
83363e51fd3SRanjani Sridharan 	/* cancel any attempt for DSP D0I3 */
83463e51fd3SRanjani Sridharan 	cancel_delayed_work_sync(&hda->d0i3_work);
83563e51fd3SRanjani Sridharan 
83661e285caSRanjani Sridharan 	if (target_state == SOF_DSP_PM_D0) {
83761e285caSRanjani Sridharan 		/* Set DSP power state */
838787c5214SRanjani Sridharan 		ret = snd_sof_dsp_set_power_state(sdev, &target_dsp_state);
83961e285caSRanjani Sridharan 		if (ret < 0) {
84061e285caSRanjani Sridharan 			dev_err(sdev->dev, "error: setting dsp state %d substate %d\n",
84161e285caSRanjani Sridharan 				target_dsp_state.state,
84261e285caSRanjani Sridharan 				target_dsp_state.substate);
84361e285caSRanjani Sridharan 			return ret;
84461e285caSRanjani Sridharan 		}
84561e285caSRanjani Sridharan 
84616299326SKeyon Jie 		/* enable L1SEN to make sure the system can enter S0Ix */
84716299326SKeyon Jie 		hda->l1_support_changed =
84816299326SKeyon Jie 			snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR,
84916299326SKeyon Jie 						HDA_VS_INTEL_EM2,
85016299326SKeyon Jie 						HDA_VS_INTEL_EM2_L1SEN,
85116299326SKeyon Jie 						HDA_VS_INTEL_EM2_L1SEN);
85216299326SKeyon Jie 
853195f1019SMarcin Rajwa 		/* stop the CORB/RIRB DMA if it is On */
8543400afcfSPierre-Louis Bossart 		hda_codec_suspend_cmd_io(sdev);
855195f1019SMarcin Rajwa 
856195f1019SMarcin Rajwa 		/* no link can be powered in s0ix state */
857f402a974SPierre-Louis Bossart 		ret = hda_bus_ml_suspend(bus);
858195f1019SMarcin Rajwa 		if (ret < 0) {
8596d5e37b0SPierre-Louis Bossart 			dev_err(sdev->dev,
860195f1019SMarcin Rajwa 				"error %d in %s: failed to power down links",
861195f1019SMarcin Rajwa 				ret, __func__);
862195f1019SMarcin Rajwa 			return ret;
863195f1019SMarcin Rajwa 		}
864195f1019SMarcin Rajwa 
86566e40876SKeyon Jie 		/* enable the system waking up via IPC IRQ */
86666e40876SKeyon Jie 		enable_irq_wake(pci->irq);
86766e40876SKeyon Jie 		pci_save_state(pci);
86866e40876SKeyon Jie 		return 0;
86966e40876SKeyon Jie 	}
87066e40876SKeyon Jie 
871747503b1SLiam Girdwood 	/* stop hda controller and power dsp off */
8721c38c922SFred Oh 	ret = hda_suspend(sdev, false);
873747503b1SLiam Girdwood 	if (ret < 0) {
874747503b1SLiam Girdwood 		dev_err(bus->dev, "error: suspending dsp\n");
875747503b1SLiam Girdwood 		return ret;
876747503b1SLiam Girdwood 	}
877747503b1SLiam Girdwood 
878787c5214SRanjani Sridharan 	return snd_sof_dsp_set_power_state(sdev, &target_dsp_state);
879747503b1SLiam Girdwood }
880ed3baacdSRanjani Sridharan 
88122aa9e02SLibin Yang int hda_dsp_shutdown(struct snd_sof_dev *sdev)
88222aa9e02SLibin Yang {
88322aa9e02SLibin Yang 	sdev->system_suspend_target = SOF_SUSPEND_S3;
88422aa9e02SLibin Yang 	return snd_sof_suspend(sdev->dev);
88522aa9e02SLibin Yang }
88622aa9e02SLibin Yang 
8877077a07aSRanjani Sridharan int hda_dsp_set_hw_params_upon_resume(struct snd_sof_dev *sdev)
888ed3baacdSRanjani Sridharan {
889f09e9284SPierre-Louis Bossart 	int ret;
8907077a07aSRanjani Sridharan 
891f09e9284SPierre-Louis Bossart 	/* make sure all DAI resources are freed */
892f09e9284SPierre-Louis Bossart 	ret = hda_dsp_dais_suspend(sdev);
893f09e9284SPierre-Louis Bossart 	if (ret < 0)
894f09e9284SPierre-Louis Bossart 		dev_warn(sdev->dev, "%s: failure in hda_dsp_dais_suspend\n", __func__);
895a3ebccb5SKai Vehmanen 
896f09e9284SPierre-Louis Bossart 	return ret;
897ed3baacdSRanjani Sridharan }
89863e51fd3SRanjani Sridharan 
89963e51fd3SRanjani Sridharan void hda_dsp_d0i3_work(struct work_struct *work)
90063e51fd3SRanjani Sridharan {
90163e51fd3SRanjani Sridharan 	struct sof_intel_hda_dev *hdev = container_of(work,
90263e51fd3SRanjani Sridharan 						      struct sof_intel_hda_dev,
90363e51fd3SRanjani Sridharan 						      d0i3_work.work);
90463e51fd3SRanjani Sridharan 	struct hdac_bus *bus = &hdev->hbus.core;
90563e51fd3SRanjani Sridharan 	struct snd_sof_dev *sdev = dev_get_drvdata(bus->dev);
906f1bb0235SGuennadi Liakhovetski 	struct sof_dsp_power_state target_state = {
907f1bb0235SGuennadi Liakhovetski 		.state = SOF_DSP_PM_D0,
908f1bb0235SGuennadi Liakhovetski 		.substate = SOF_HDA_DSP_PM_D0I3,
909f1bb0235SGuennadi Liakhovetski 	};
91063e51fd3SRanjani Sridharan 	int ret;
91163e51fd3SRanjani Sridharan 
91263e51fd3SRanjani Sridharan 	/* DSP can enter D0I3 iff only D0I3-compatible streams are active */
913f1bb0235SGuennadi Liakhovetski 	if (!snd_sof_dsp_only_d0i3_compatible_stream_active(sdev))
91463e51fd3SRanjani Sridharan 		/* remain in D0I0 */
91563e51fd3SRanjani Sridharan 		return;
91663e51fd3SRanjani Sridharan 
91763e51fd3SRanjani Sridharan 	/* This can fail but error cannot be propagated */
918787c5214SRanjani Sridharan 	ret = snd_sof_dsp_set_power_state(sdev, &target_state);
91963e51fd3SRanjani Sridharan 	if (ret < 0)
92063e51fd3SRanjani Sridharan 		dev_err_ratelimited(sdev->dev,
92163e51fd3SRanjani Sridharan 				    "error: failed to set DSP state %d substate %d\n",
92263e51fd3SRanjani Sridharan 				    target_state.state, target_state.substate);
92363e51fd3SRanjani Sridharan }
9249cdcbc9fSRanjani Sridharan 
9259cdcbc9fSRanjani Sridharan int hda_dsp_core_get(struct snd_sof_dev *sdev, int core)
9269cdcbc9fSRanjani Sridharan {
9277a567740SPeter Ujfalusi 	const struct sof_ipc_pm_ops *pm_ops = sdev->ipc->ops->pm;
9289cdcbc9fSRanjani Sridharan 	int ret, ret1;
9299cdcbc9fSRanjani Sridharan 
9309cdcbc9fSRanjani Sridharan 	/* power up core */
9319cdcbc9fSRanjani Sridharan 	ret = hda_dsp_enable_core(sdev, BIT(core));
9329cdcbc9fSRanjani Sridharan 	if (ret < 0) {
9339cdcbc9fSRanjani Sridharan 		dev_err(sdev->dev, "failed to power up core %d with err: %d\n",
9349cdcbc9fSRanjani Sridharan 			core, ret);
9359cdcbc9fSRanjani Sridharan 		return ret;
9369cdcbc9fSRanjani Sridharan 	}
9379cdcbc9fSRanjani Sridharan 
9389cdcbc9fSRanjani Sridharan 	/* No need to send IPC for primary core or if FW boot is not complete */
9399cdcbc9fSRanjani Sridharan 	if (sdev->fw_state != SOF_FW_BOOT_COMPLETE || core == SOF_DSP_PRIMARY_CORE)
9409cdcbc9fSRanjani Sridharan 		return 0;
9419cdcbc9fSRanjani Sridharan 
9427a567740SPeter Ujfalusi 	/* No need to continue the set_core_state ops is not available */
9437a567740SPeter Ujfalusi 	if (!pm_ops->set_core_state)
9447a567740SPeter Ujfalusi 		return 0;
9457a567740SPeter Ujfalusi 
9469cdcbc9fSRanjani Sridharan 	/* Now notify DSP for secondary cores */
9477a567740SPeter Ujfalusi 	ret = pm_ops->set_core_state(sdev, core, true);
9489cdcbc9fSRanjani Sridharan 	if (ret < 0) {
9499cdcbc9fSRanjani Sridharan 		dev_err(sdev->dev, "failed to enable secondary core '%d' failed with %d\n",
9509cdcbc9fSRanjani Sridharan 			core, ret);
9519cdcbc9fSRanjani Sridharan 		goto power_down;
9529cdcbc9fSRanjani Sridharan 	}
9539cdcbc9fSRanjani Sridharan 
9549cdcbc9fSRanjani Sridharan 	return ret;
9559cdcbc9fSRanjani Sridharan 
9569cdcbc9fSRanjani Sridharan power_down:
9579cdcbc9fSRanjani Sridharan 	/* power down core if it is host managed and return the original error if this fails too */
9589cdcbc9fSRanjani Sridharan 	ret1 = hda_dsp_core_reset_power_down(sdev, BIT(core));
9599cdcbc9fSRanjani Sridharan 	if (ret1 < 0)
9609cdcbc9fSRanjani Sridharan 		dev_err(sdev->dev, "failed to power down core: %d with err: %d\n", core, ret1);
9619cdcbc9fSRanjani Sridharan 
9629cdcbc9fSRanjani Sridharan 	return ret;
9639cdcbc9fSRanjani Sridharan }
964b2520dbcSRanjani Sridharan 
965b2520dbcSRanjani Sridharan int hda_dsp_disable_interrupts(struct snd_sof_dev *sdev)
966b2520dbcSRanjani Sridharan {
967b2520dbcSRanjani Sridharan 	hda_sdw_int_enable(sdev, false);
968b2520dbcSRanjani Sridharan 	hda_dsp_ipc_int_disable(sdev);
969b2520dbcSRanjani Sridharan 
970b2520dbcSRanjani Sridharan 	return 0;
971b2520dbcSRanjani Sridharan }
972