xref: /openbmc/linux/sound/soc/sof/intel/hda-dsp.c (revision 816938b272b0ac0203e25ce50483bd284ea4a2db)
1747503b1SLiam Girdwood // SPDX-License-Identifier: (GPL-2.0 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 
18747503b1SLiam Girdwood #include <sound/hdaudio_ext.h>
19747503b1SLiam Girdwood #include <sound/hda_register.h>
20747503b1SLiam Girdwood #include "../ops.h"
21747503b1SLiam Girdwood #include "hda.h"
22534037fdSKeyon Jie #include "hda-ipc.h"
23747503b1SLiam Girdwood 
24747503b1SLiam Girdwood /*
25747503b1SLiam Girdwood  * DSP Core control.
26747503b1SLiam Girdwood  */
27747503b1SLiam Girdwood 
28747503b1SLiam Girdwood int hda_dsp_core_reset_enter(struct snd_sof_dev *sdev, unsigned int core_mask)
29747503b1SLiam Girdwood {
30747503b1SLiam Girdwood 	u32 adspcs;
31747503b1SLiam Girdwood 	u32 reset;
32747503b1SLiam Girdwood 	int ret;
33747503b1SLiam Girdwood 
34747503b1SLiam Girdwood 	/* set reset bits for cores */
35747503b1SLiam Girdwood 	reset = HDA_DSP_ADSPCS_CRST_MASK(core_mask);
36747503b1SLiam Girdwood 	snd_sof_dsp_update_bits_unlocked(sdev, HDA_DSP_BAR,
37747503b1SLiam Girdwood 					 HDA_DSP_REG_ADSPCS,
38747503b1SLiam Girdwood 					 reset, reset),
39747503b1SLiam Girdwood 
40747503b1SLiam Girdwood 	/* poll with timeout to check if operation successful */
41747503b1SLiam Girdwood 	ret = snd_sof_dsp_read_poll_timeout(sdev, HDA_DSP_BAR,
42747503b1SLiam Girdwood 					HDA_DSP_REG_ADSPCS, adspcs,
43747503b1SLiam Girdwood 					((adspcs & reset) == reset),
44747503b1SLiam Girdwood 					HDA_DSP_REG_POLL_INTERVAL_US,
45747503b1SLiam Girdwood 					HDA_DSP_RESET_TIMEOUT_US);
466a414489SPierre-Louis Bossart 	if (ret < 0) {
476a414489SPierre-Louis Bossart 		dev_err(sdev->dev,
486a414489SPierre-Louis Bossart 			"error: %s: timeout on HDA_DSP_REG_ADSPCS read\n",
496a414489SPierre-Louis Bossart 			__func__);
506a414489SPierre-Louis Bossart 		return ret;
516a414489SPierre-Louis Bossart 	}
52747503b1SLiam Girdwood 
53747503b1SLiam Girdwood 	/* has core entered reset ? */
54747503b1SLiam Girdwood 	adspcs = snd_sof_dsp_read(sdev, HDA_DSP_BAR,
55747503b1SLiam Girdwood 				  HDA_DSP_REG_ADSPCS);
56747503b1SLiam Girdwood 	if ((adspcs & HDA_DSP_ADSPCS_CRST_MASK(core_mask)) !=
57747503b1SLiam Girdwood 		HDA_DSP_ADSPCS_CRST_MASK(core_mask)) {
58747503b1SLiam Girdwood 		dev_err(sdev->dev,
59747503b1SLiam Girdwood 			"error: reset enter failed: core_mask %x adspcs 0x%x\n",
60747503b1SLiam Girdwood 			core_mask, adspcs);
61747503b1SLiam Girdwood 		ret = -EIO;
62747503b1SLiam Girdwood 	}
63747503b1SLiam Girdwood 
64747503b1SLiam Girdwood 	return ret;
65747503b1SLiam Girdwood }
66747503b1SLiam Girdwood 
67747503b1SLiam Girdwood int hda_dsp_core_reset_leave(struct snd_sof_dev *sdev, unsigned int core_mask)
68747503b1SLiam Girdwood {
69747503b1SLiam Girdwood 	unsigned int crst;
70747503b1SLiam Girdwood 	u32 adspcs;
71747503b1SLiam Girdwood 	int ret;
72747503b1SLiam Girdwood 
73747503b1SLiam Girdwood 	/* clear reset bits for cores */
74747503b1SLiam Girdwood 	snd_sof_dsp_update_bits_unlocked(sdev, HDA_DSP_BAR,
75747503b1SLiam Girdwood 					 HDA_DSP_REG_ADSPCS,
76747503b1SLiam Girdwood 					 HDA_DSP_ADSPCS_CRST_MASK(core_mask),
77747503b1SLiam Girdwood 					 0);
78747503b1SLiam Girdwood 
79747503b1SLiam Girdwood 	/* poll with timeout to check if operation successful */
80747503b1SLiam Girdwood 	crst = HDA_DSP_ADSPCS_CRST_MASK(core_mask);
81747503b1SLiam Girdwood 	ret = snd_sof_dsp_read_poll_timeout(sdev, HDA_DSP_BAR,
82747503b1SLiam Girdwood 					    HDA_DSP_REG_ADSPCS, adspcs,
83747503b1SLiam Girdwood 					    !(adspcs & crst),
84747503b1SLiam Girdwood 					    HDA_DSP_REG_POLL_INTERVAL_US,
85747503b1SLiam Girdwood 					    HDA_DSP_RESET_TIMEOUT_US);
86747503b1SLiam Girdwood 
876a414489SPierre-Louis Bossart 	if (ret < 0) {
886a414489SPierre-Louis Bossart 		dev_err(sdev->dev,
896a414489SPierre-Louis Bossart 			"error: %s: timeout on HDA_DSP_REG_ADSPCS read\n",
906a414489SPierre-Louis Bossart 			__func__);
916a414489SPierre-Louis Bossart 		return ret;
926a414489SPierre-Louis Bossart 	}
936a414489SPierre-Louis Bossart 
94747503b1SLiam Girdwood 	/* has core left reset ? */
95747503b1SLiam Girdwood 	adspcs = snd_sof_dsp_read(sdev, HDA_DSP_BAR,
96747503b1SLiam Girdwood 				  HDA_DSP_REG_ADSPCS);
97747503b1SLiam Girdwood 	if ((adspcs & HDA_DSP_ADSPCS_CRST_MASK(core_mask)) != 0) {
98747503b1SLiam Girdwood 		dev_err(sdev->dev,
99747503b1SLiam Girdwood 			"error: reset leave failed: core_mask %x adspcs 0x%x\n",
100747503b1SLiam Girdwood 			core_mask, adspcs);
101747503b1SLiam Girdwood 		ret = -EIO;
102747503b1SLiam Girdwood 	}
103747503b1SLiam Girdwood 
104747503b1SLiam Girdwood 	return ret;
105747503b1SLiam Girdwood }
106747503b1SLiam Girdwood 
107747503b1SLiam Girdwood int hda_dsp_core_stall_reset(struct snd_sof_dev *sdev, unsigned int core_mask)
108747503b1SLiam Girdwood {
109747503b1SLiam Girdwood 	/* stall core */
110747503b1SLiam Girdwood 	snd_sof_dsp_update_bits_unlocked(sdev, HDA_DSP_BAR,
111747503b1SLiam Girdwood 					 HDA_DSP_REG_ADSPCS,
112747503b1SLiam Girdwood 					 HDA_DSP_ADSPCS_CSTALL_MASK(core_mask),
113747503b1SLiam Girdwood 					 HDA_DSP_ADSPCS_CSTALL_MASK(core_mask));
114747503b1SLiam Girdwood 
115747503b1SLiam Girdwood 	/* set reset state */
116747503b1SLiam Girdwood 	return hda_dsp_core_reset_enter(sdev, core_mask);
117747503b1SLiam Girdwood }
118747503b1SLiam Girdwood 
119747503b1SLiam Girdwood int hda_dsp_core_run(struct snd_sof_dev *sdev, unsigned int core_mask)
120747503b1SLiam Girdwood {
121747503b1SLiam Girdwood 	int ret;
122747503b1SLiam Girdwood 
123747503b1SLiam Girdwood 	/* leave reset state */
124747503b1SLiam Girdwood 	ret = hda_dsp_core_reset_leave(sdev, core_mask);
125747503b1SLiam Girdwood 	if (ret < 0)
126747503b1SLiam Girdwood 		return ret;
127747503b1SLiam Girdwood 
128747503b1SLiam Girdwood 	/* run core */
129747503b1SLiam Girdwood 	dev_dbg(sdev->dev, "unstall/run core: core_mask = %x\n", core_mask);
130747503b1SLiam Girdwood 	snd_sof_dsp_update_bits_unlocked(sdev, HDA_DSP_BAR,
131747503b1SLiam Girdwood 					 HDA_DSP_REG_ADSPCS,
132747503b1SLiam Girdwood 					 HDA_DSP_ADSPCS_CSTALL_MASK(core_mask),
133747503b1SLiam Girdwood 					 0);
134747503b1SLiam Girdwood 
135747503b1SLiam Girdwood 	/* is core now running ? */
136747503b1SLiam Girdwood 	if (!hda_dsp_core_is_enabled(sdev, core_mask)) {
137747503b1SLiam Girdwood 		hda_dsp_core_stall_reset(sdev, core_mask);
138747503b1SLiam Girdwood 		dev_err(sdev->dev, "error: DSP start core failed: core_mask %x\n",
139747503b1SLiam Girdwood 			core_mask);
140747503b1SLiam Girdwood 		ret = -EIO;
141747503b1SLiam Girdwood 	}
142747503b1SLiam Girdwood 
143747503b1SLiam Girdwood 	return ret;
144747503b1SLiam Girdwood }
145747503b1SLiam Girdwood 
146747503b1SLiam Girdwood /*
147747503b1SLiam Girdwood  * Power Management.
148747503b1SLiam Girdwood  */
149747503b1SLiam Girdwood 
150747503b1SLiam Girdwood int hda_dsp_core_power_up(struct snd_sof_dev *sdev, unsigned int core_mask)
151747503b1SLiam Girdwood {
152747503b1SLiam Girdwood 	unsigned int cpa;
153747503b1SLiam Girdwood 	u32 adspcs;
154747503b1SLiam Girdwood 	int ret;
155747503b1SLiam Girdwood 
156747503b1SLiam Girdwood 	/* update bits */
157747503b1SLiam Girdwood 	snd_sof_dsp_update_bits(sdev, HDA_DSP_BAR, HDA_DSP_REG_ADSPCS,
158747503b1SLiam Girdwood 				HDA_DSP_ADSPCS_SPA_MASK(core_mask),
159747503b1SLiam Girdwood 				HDA_DSP_ADSPCS_SPA_MASK(core_mask));
160747503b1SLiam Girdwood 
161747503b1SLiam Girdwood 	/* poll with timeout to check if operation successful */
162747503b1SLiam Girdwood 	cpa = HDA_DSP_ADSPCS_CPA_MASK(core_mask);
163747503b1SLiam Girdwood 	ret = snd_sof_dsp_read_poll_timeout(sdev, HDA_DSP_BAR,
164747503b1SLiam Girdwood 					    HDA_DSP_REG_ADSPCS, adspcs,
165747503b1SLiam Girdwood 					    (adspcs & cpa) == cpa,
166747503b1SLiam Girdwood 					    HDA_DSP_REG_POLL_INTERVAL_US,
167747503b1SLiam Girdwood 					    HDA_DSP_RESET_TIMEOUT_US);
1686a414489SPierre-Louis Bossart 	if (ret < 0) {
1696a414489SPierre-Louis Bossart 		dev_err(sdev->dev,
1706a414489SPierre-Louis Bossart 			"error: %s: timeout on HDA_DSP_REG_ADSPCS read\n",
1716a414489SPierre-Louis Bossart 			__func__);
1726a414489SPierre-Louis Bossart 		return ret;
1736a414489SPierre-Louis Bossart 	}
174747503b1SLiam Girdwood 
175747503b1SLiam Girdwood 	/* did core power up ? */
176747503b1SLiam Girdwood 	adspcs = snd_sof_dsp_read(sdev, HDA_DSP_BAR,
177747503b1SLiam Girdwood 				  HDA_DSP_REG_ADSPCS);
178747503b1SLiam Girdwood 	if ((adspcs & HDA_DSP_ADSPCS_CPA_MASK(core_mask)) !=
179747503b1SLiam Girdwood 		HDA_DSP_ADSPCS_CPA_MASK(core_mask)) {
180747503b1SLiam Girdwood 		dev_err(sdev->dev,
181747503b1SLiam Girdwood 			"error: power up core failed core_mask %xadspcs 0x%x\n",
182747503b1SLiam Girdwood 			core_mask, adspcs);
183747503b1SLiam Girdwood 		ret = -EIO;
184747503b1SLiam Girdwood 	}
185747503b1SLiam Girdwood 
186747503b1SLiam Girdwood 	return ret;
187747503b1SLiam Girdwood }
188747503b1SLiam Girdwood 
189747503b1SLiam Girdwood int hda_dsp_core_power_down(struct snd_sof_dev *sdev, unsigned int core_mask)
190747503b1SLiam Girdwood {
191747503b1SLiam Girdwood 	u32 adspcs;
1926a414489SPierre-Louis Bossart 	int ret;
193747503b1SLiam Girdwood 
194747503b1SLiam Girdwood 	/* update bits */
195747503b1SLiam Girdwood 	snd_sof_dsp_update_bits_unlocked(sdev, HDA_DSP_BAR,
196747503b1SLiam Girdwood 					 HDA_DSP_REG_ADSPCS,
197747503b1SLiam Girdwood 					 HDA_DSP_ADSPCS_SPA_MASK(core_mask), 0);
198747503b1SLiam Girdwood 
1996a414489SPierre-Louis Bossart 	ret = snd_sof_dsp_read_poll_timeout(sdev, HDA_DSP_BAR,
200747503b1SLiam Girdwood 				HDA_DSP_REG_ADSPCS, adspcs,
201747503b1SLiam Girdwood 				!(adspcs & HDA_DSP_ADSPCS_SPA_MASK(core_mask)),
202747503b1SLiam Girdwood 				HDA_DSP_REG_POLL_INTERVAL_US,
203747503b1SLiam Girdwood 				HDA_DSP_PD_TIMEOUT * USEC_PER_MSEC);
2046a414489SPierre-Louis Bossart 	if (ret < 0)
2056a414489SPierre-Louis Bossart 		dev_err(sdev->dev,
2066a414489SPierre-Louis Bossart 			"error: %s: timeout on HDA_DSP_REG_ADSPCS read\n",
2076a414489SPierre-Louis Bossart 			__func__);
2086a414489SPierre-Louis Bossart 
2096a414489SPierre-Louis Bossart 	return ret;
210747503b1SLiam Girdwood }
211747503b1SLiam Girdwood 
212747503b1SLiam Girdwood bool hda_dsp_core_is_enabled(struct snd_sof_dev *sdev,
213747503b1SLiam Girdwood 			     unsigned int core_mask)
214747503b1SLiam Girdwood {
215747503b1SLiam Girdwood 	int val;
216747503b1SLiam Girdwood 	bool is_enable;
217747503b1SLiam Girdwood 
218747503b1SLiam Girdwood 	val = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_REG_ADSPCS);
219747503b1SLiam Girdwood 
220747503b1SLiam Girdwood 	is_enable = ((val & HDA_DSP_ADSPCS_CPA_MASK(core_mask)) &&
221747503b1SLiam Girdwood 			(val & HDA_DSP_ADSPCS_SPA_MASK(core_mask)) &&
222747503b1SLiam Girdwood 			!(val & HDA_DSP_ADSPCS_CRST_MASK(core_mask)) &&
223747503b1SLiam Girdwood 			!(val & HDA_DSP_ADSPCS_CSTALL_MASK(core_mask)));
224747503b1SLiam Girdwood 
225747503b1SLiam Girdwood 	dev_dbg(sdev->dev, "DSP core(s) enabled? %d : core_mask %x\n",
226747503b1SLiam Girdwood 		is_enable, core_mask);
227747503b1SLiam Girdwood 
228747503b1SLiam Girdwood 	return is_enable;
229747503b1SLiam Girdwood }
230747503b1SLiam Girdwood 
231747503b1SLiam Girdwood int hda_dsp_enable_core(struct snd_sof_dev *sdev, unsigned int core_mask)
232747503b1SLiam Girdwood {
233747503b1SLiam Girdwood 	int ret;
234747503b1SLiam Girdwood 
235747503b1SLiam Girdwood 	/* return if core is already enabled */
236747503b1SLiam Girdwood 	if (hda_dsp_core_is_enabled(sdev, core_mask))
237747503b1SLiam Girdwood 		return 0;
238747503b1SLiam Girdwood 
239747503b1SLiam Girdwood 	/* power up */
240747503b1SLiam Girdwood 	ret = hda_dsp_core_power_up(sdev, core_mask);
241747503b1SLiam Girdwood 	if (ret < 0) {
242747503b1SLiam Girdwood 		dev_err(sdev->dev, "error: dsp core power up failed: core_mask %x\n",
243747503b1SLiam Girdwood 			core_mask);
244747503b1SLiam Girdwood 		return ret;
245747503b1SLiam Girdwood 	}
246747503b1SLiam Girdwood 
247747503b1SLiam Girdwood 	return hda_dsp_core_run(sdev, core_mask);
248747503b1SLiam Girdwood }
249747503b1SLiam Girdwood 
250747503b1SLiam Girdwood int hda_dsp_core_reset_power_down(struct snd_sof_dev *sdev,
251747503b1SLiam Girdwood 				  unsigned int core_mask)
252747503b1SLiam Girdwood {
253747503b1SLiam Girdwood 	int ret;
254747503b1SLiam Girdwood 
255747503b1SLiam Girdwood 	/* place core in reset prior to power down */
256747503b1SLiam Girdwood 	ret = hda_dsp_core_stall_reset(sdev, core_mask);
257747503b1SLiam Girdwood 	if (ret < 0) {
258747503b1SLiam Girdwood 		dev_err(sdev->dev, "error: dsp core reset failed: core_mask %x\n",
259747503b1SLiam Girdwood 			core_mask);
260747503b1SLiam Girdwood 		return ret;
261747503b1SLiam Girdwood 	}
262747503b1SLiam Girdwood 
263747503b1SLiam Girdwood 	/* power down core */
264747503b1SLiam Girdwood 	ret = hda_dsp_core_power_down(sdev, core_mask);
265747503b1SLiam Girdwood 	if (ret < 0) {
266747503b1SLiam Girdwood 		dev_err(sdev->dev, "error: dsp core power down fail mask %x: %d\n",
267747503b1SLiam Girdwood 			core_mask, ret);
268747503b1SLiam Girdwood 		return ret;
269747503b1SLiam Girdwood 	}
270747503b1SLiam Girdwood 
271747503b1SLiam Girdwood 	/* make sure we are in OFF state */
272747503b1SLiam Girdwood 	if (hda_dsp_core_is_enabled(sdev, core_mask)) {
273747503b1SLiam Girdwood 		dev_err(sdev->dev, "error: dsp core disable fail mask %x: %d\n",
274747503b1SLiam Girdwood 			core_mask, ret);
275747503b1SLiam Girdwood 		ret = -EIO;
276747503b1SLiam Girdwood 	}
277747503b1SLiam Girdwood 
278747503b1SLiam Girdwood 	return ret;
279747503b1SLiam Girdwood }
280747503b1SLiam Girdwood 
281747503b1SLiam Girdwood void hda_dsp_ipc_int_enable(struct snd_sof_dev *sdev)
282747503b1SLiam Girdwood {
283747503b1SLiam Girdwood 	struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata;
284747503b1SLiam Girdwood 	const struct sof_intel_dsp_desc *chip = hda->desc;
285747503b1SLiam Girdwood 
286747503b1SLiam Girdwood 	/* enable IPC DONE and BUSY interrupts */
287747503b1SLiam Girdwood 	snd_sof_dsp_update_bits(sdev, HDA_DSP_BAR, chip->ipc_ctl,
288747503b1SLiam Girdwood 			HDA_DSP_REG_HIPCCTL_DONE | HDA_DSP_REG_HIPCCTL_BUSY,
289747503b1SLiam Girdwood 			HDA_DSP_REG_HIPCCTL_DONE | HDA_DSP_REG_HIPCCTL_BUSY);
290747503b1SLiam Girdwood 
291747503b1SLiam Girdwood 	/* enable IPC interrupt */
292747503b1SLiam Girdwood 	snd_sof_dsp_update_bits(sdev, HDA_DSP_BAR, HDA_DSP_REG_ADSPIC,
293747503b1SLiam Girdwood 				HDA_DSP_ADSPIC_IPC, HDA_DSP_ADSPIC_IPC);
294747503b1SLiam Girdwood }
295747503b1SLiam Girdwood 
296747503b1SLiam Girdwood void hda_dsp_ipc_int_disable(struct snd_sof_dev *sdev)
297747503b1SLiam Girdwood {
298747503b1SLiam Girdwood 	struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata;
299747503b1SLiam Girdwood 	const struct sof_intel_dsp_desc *chip = hda->desc;
300747503b1SLiam Girdwood 
301747503b1SLiam Girdwood 	/* disable IPC interrupt */
302747503b1SLiam Girdwood 	snd_sof_dsp_update_bits(sdev, HDA_DSP_BAR, HDA_DSP_REG_ADSPIC,
303747503b1SLiam Girdwood 				HDA_DSP_ADSPIC_IPC, 0);
304747503b1SLiam Girdwood 
305747503b1SLiam Girdwood 	/* disable IPC BUSY and DONE interrupt */
306747503b1SLiam Girdwood 	snd_sof_dsp_update_bits(sdev, HDA_DSP_BAR, chip->ipc_ctl,
307747503b1SLiam Girdwood 			HDA_DSP_REG_HIPCCTL_BUSY | HDA_DSP_REG_HIPCCTL_DONE, 0);
308747503b1SLiam Girdwood }
309747503b1SLiam Girdwood 
31065c56f5dSRanjani Sridharan static int hda_dsp_wait_d0i3c_done(struct snd_sof_dev *sdev)
31162f8f766SKeyon Jie {
31262f8f766SKeyon Jie 	struct hdac_bus *bus = sof_to_bus(sdev);
31365c56f5dSRanjani Sridharan 	int retry = HDA_DSP_REG_POLL_RETRY_COUNT;
31462f8f766SKeyon Jie 
31562f8f766SKeyon Jie 	while (snd_hdac_chip_readb(bus, VS_D0I3C) & SOF_HDA_VS_D0I3C_CIP) {
31662f8f766SKeyon Jie 		if (!retry--)
31762f8f766SKeyon Jie 			return -ETIMEDOUT;
31862f8f766SKeyon Jie 		usleep_range(10, 15);
31962f8f766SKeyon Jie 	}
32062f8f766SKeyon Jie 
32162f8f766SKeyon Jie 	return 0;
32262f8f766SKeyon Jie }
32362f8f766SKeyon Jie 
324534037fdSKeyon Jie static int hda_dsp_send_pm_gate_ipc(struct snd_sof_dev *sdev, u32 flags)
325534037fdSKeyon Jie {
326534037fdSKeyon Jie 	struct sof_ipc_pm_gate pm_gate;
327534037fdSKeyon Jie 	struct sof_ipc_reply reply;
328534037fdSKeyon Jie 
329534037fdSKeyon Jie 	memset(&pm_gate, 0, sizeof(pm_gate));
330534037fdSKeyon Jie 
331534037fdSKeyon Jie 	/* configure pm_gate ipc message */
332534037fdSKeyon Jie 	pm_gate.hdr.size = sizeof(pm_gate);
333534037fdSKeyon Jie 	pm_gate.hdr.cmd = SOF_IPC_GLB_PM_MSG | SOF_IPC_PM_GATE;
334534037fdSKeyon Jie 	pm_gate.flags = flags;
335534037fdSKeyon Jie 
336534037fdSKeyon Jie 	/* send pm_gate ipc to dsp */
337534037fdSKeyon Jie 	return sof_ipc_tx_message(sdev->ipc, pm_gate.hdr.cmd, &pm_gate,
338534037fdSKeyon Jie 				  sizeof(pm_gate), &reply, sizeof(reply));
339534037fdSKeyon Jie }
340534037fdSKeyon Jie 
34162f8f766SKeyon Jie int hda_dsp_set_power_state(struct snd_sof_dev *sdev,
34262f8f766SKeyon Jie 			    enum sof_d0_substate d0_substate)
34362f8f766SKeyon Jie {
34462f8f766SKeyon Jie 	struct hdac_bus *bus = sof_to_bus(sdev);
345534037fdSKeyon Jie 	u32 flags;
34662f8f766SKeyon Jie 	int ret;
34762f8f766SKeyon Jie 	u8 value;
34862f8f766SKeyon Jie 
34962f8f766SKeyon Jie 	/* Write to D0I3C after Command-In-Progress bit is cleared */
35065c56f5dSRanjani Sridharan 	ret = hda_dsp_wait_d0i3c_done(sdev);
35162f8f766SKeyon Jie 	if (ret < 0) {
352aae7c82dSKeyon Jie 		dev_err(bus->dev, "CIP timeout before D0I3C update!\n");
35362f8f766SKeyon Jie 		return ret;
35462f8f766SKeyon Jie 	}
35562f8f766SKeyon Jie 
35662f8f766SKeyon Jie 	/* Update D0I3C register */
35762f8f766SKeyon Jie 	value = d0_substate == SOF_DSP_D0I3 ? SOF_HDA_VS_D0I3C_I3 : 0;
35862f8f766SKeyon Jie 	snd_hdac_chip_updateb(bus, VS_D0I3C, SOF_HDA_VS_D0I3C_I3, value);
35962f8f766SKeyon Jie 
36062f8f766SKeyon Jie 	/* Wait for cmd in progress to be cleared before exiting the function */
36165c56f5dSRanjani Sridharan 	ret = hda_dsp_wait_d0i3c_done(sdev);
36262f8f766SKeyon Jie 	if (ret < 0) {
363aae7c82dSKeyon Jie 		dev_err(bus->dev, "CIP timeout after D0I3C update!\n");
36462f8f766SKeyon Jie 		return ret;
36562f8f766SKeyon Jie 	}
36662f8f766SKeyon Jie 
36762f8f766SKeyon Jie 	dev_vdbg(bus->dev, "D0I3C updated, register = 0x%x\n",
36862f8f766SKeyon Jie 		 snd_hdac_chip_readb(bus, VS_D0I3C));
36962f8f766SKeyon Jie 
370534037fdSKeyon Jie 	if (d0_substate == SOF_DSP_D0I0)
371534037fdSKeyon Jie 		flags = HDA_PM_PPG;/* prevent power gating in D0 */
372534037fdSKeyon Jie 	else
373534037fdSKeyon Jie 		flags = HDA_PM_NO_DMA_TRACE;/* disable DMA trace in D0I3*/
374534037fdSKeyon Jie 
375534037fdSKeyon Jie 	/* sending pm_gate IPC */
376534037fdSKeyon Jie 	ret = hda_dsp_send_pm_gate_ipc(sdev, flags);
377534037fdSKeyon Jie 	if (ret < 0)
378534037fdSKeyon Jie 		dev_err(sdev->dev,
379534037fdSKeyon Jie 			"error: PM_GATE ipc error %d\n", ret);
380534037fdSKeyon Jie 
381534037fdSKeyon Jie 	return ret;
38262f8f766SKeyon Jie }
38362f8f766SKeyon Jie 
3841c38c922SFred Oh static int hda_suspend(struct snd_sof_dev *sdev, bool runtime_suspend)
385747503b1SLiam Girdwood {
386747503b1SLiam Girdwood 	struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata;
387747503b1SLiam Girdwood 	const struct sof_intel_dsp_desc *chip = hda->desc;
388747503b1SLiam Girdwood #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
389747503b1SLiam Girdwood 	struct hdac_bus *bus = sof_to_bus(sdev);
390747503b1SLiam Girdwood #endif
391747503b1SLiam Girdwood 	int ret;
392747503b1SLiam Girdwood 
393747503b1SLiam Girdwood 	/* disable IPC interrupts */
394747503b1SLiam Girdwood 	hda_dsp_ipc_int_disable(sdev);
395747503b1SLiam Girdwood 
396747503b1SLiam Girdwood #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
397fd15f2f5SRander Wang 	if (runtime_suspend)
398fd15f2f5SRander Wang 		hda_codec_jack_wake_enable(sdev);
399fd15f2f5SRander Wang 
400747503b1SLiam Girdwood 	/* power down all hda link */
401747503b1SLiam Girdwood 	snd_hdac_ext_bus_link_power_down_all(bus);
402747503b1SLiam Girdwood #endif
403747503b1SLiam Girdwood 
404747503b1SLiam Girdwood 	/* power down DSP */
405747503b1SLiam Girdwood 	ret = hda_dsp_core_reset_power_down(sdev, chip->cores_mask);
406747503b1SLiam Girdwood 	if (ret < 0) {
407747503b1SLiam Girdwood 		dev_err(sdev->dev,
408747503b1SLiam Girdwood 			"error: failed to power down core during suspend\n");
409747503b1SLiam Girdwood 		return ret;
410747503b1SLiam Girdwood 	}
411747503b1SLiam Girdwood 
412747503b1SLiam Girdwood 	/* disable ppcap interrupt */
413747503b1SLiam Girdwood 	hda_dsp_ctrl_ppcap_enable(sdev, false);
414747503b1SLiam Girdwood 	hda_dsp_ctrl_ppcap_int_enable(sdev, false);
415747503b1SLiam Girdwood 
4169a50ee58SZhu Yingjiang 	/* disable hda bus irq and streams */
4179a50ee58SZhu Yingjiang 	hda_dsp_ctrl_stop_chip(sdev);
418747503b1SLiam Girdwood 
419747503b1SLiam Girdwood 	/* disable LP retention mode */
420747503b1SLiam Girdwood 	snd_sof_pci_update_bits(sdev, PCI_PGCTL,
421747503b1SLiam Girdwood 				PCI_PGCTL_LSRMD_MASK, PCI_PGCTL_LSRMD_MASK);
422747503b1SLiam Girdwood 
423747503b1SLiam Girdwood 	/* reset controller */
424747503b1SLiam Girdwood 	ret = hda_dsp_ctrl_link_reset(sdev, true);
425747503b1SLiam Girdwood 	if (ret < 0) {
426747503b1SLiam Girdwood 		dev_err(sdev->dev,
427747503b1SLiam Girdwood 			"error: failed to reset controller during suspend\n");
428747503b1SLiam Girdwood 		return ret;
429747503b1SLiam Girdwood 	}
430747503b1SLiam Girdwood 
431*816938b2SKai Vehmanen 	/* display codec can powered off after link reset */
432*816938b2SKai Vehmanen 	hda_codec_i915_display_power(sdev, false);
433*816938b2SKai Vehmanen 
434747503b1SLiam Girdwood 	return 0;
435747503b1SLiam Girdwood }
436747503b1SLiam Girdwood 
437fd15f2f5SRander Wang static int hda_resume(struct snd_sof_dev *sdev, bool runtime_resume)
438747503b1SLiam Girdwood {
439747503b1SLiam Girdwood #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
440747503b1SLiam Girdwood 	struct hdac_bus *bus = sof_to_bus(sdev);
441747503b1SLiam Girdwood 	struct hdac_ext_link *hlink = NULL;
442747503b1SLiam Girdwood #endif
443747503b1SLiam Girdwood 	int ret;
444747503b1SLiam Girdwood 
445*816938b2SKai Vehmanen 	/* display codec must be powered before link reset */
446*816938b2SKai Vehmanen 	hda_codec_i915_display_power(sdev, true);
447*816938b2SKai Vehmanen 
448747503b1SLiam Girdwood 	/*
449747503b1SLiam Girdwood 	 * clear TCSEL to clear playback on some HD Audio
450747503b1SLiam Girdwood 	 * codecs. PCI TCSEL is defined in the Intel manuals.
451747503b1SLiam Girdwood 	 */
452747503b1SLiam Girdwood 	snd_sof_pci_update_bits(sdev, PCI_TCSEL, 0x07, 0);
453747503b1SLiam Girdwood 
454747503b1SLiam Girdwood 	/* reset and start hda controller */
455747503b1SLiam Girdwood 	ret = hda_dsp_ctrl_init_chip(sdev, true);
456747503b1SLiam Girdwood 	if (ret < 0) {
457747503b1SLiam Girdwood 		dev_err(sdev->dev,
458747503b1SLiam Girdwood 			"error: failed to start controller after resume\n");
459747503b1SLiam Girdwood 		return ret;
460747503b1SLiam Girdwood 	}
461747503b1SLiam Girdwood 
462fd15f2f5SRander Wang #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
463fd15f2f5SRander Wang 	/* check jack status */
464fd15f2f5SRander Wang 	if (runtime_resume)
465fd15f2f5SRander Wang 		hda_codec_jack_check(sdev);
4666aa232e1SRander Wang 
4676aa232e1SRander Wang 	/* turn off the links that were off before suspend */
4686aa232e1SRander Wang 	list_for_each_entry(hlink, &bus->hlink_list, list) {
4696aa232e1SRander Wang 		if (!hlink->ref_count)
4706aa232e1SRander Wang 			snd_hdac_ext_bus_link_power_down(hlink);
4716aa232e1SRander Wang 	}
4726aa232e1SRander Wang 
4736aa232e1SRander Wang 	/* check dma status and clean up CORB/RIRB buffers */
4746aa232e1SRander Wang 	if (!bus->cmd_dma_state)
4756aa232e1SRander Wang 		snd_hdac_bus_stop_cmd_io(bus);
47624b6ff68SZhu Yingjiang #endif
477747503b1SLiam Girdwood 
478747503b1SLiam Girdwood 	/* enable ppcap interrupt */
479747503b1SLiam Girdwood 	hda_dsp_ctrl_ppcap_enable(sdev, true);
480747503b1SLiam Girdwood 	hda_dsp_ctrl_ppcap_int_enable(sdev, true);
481747503b1SLiam Girdwood 
482747503b1SLiam Girdwood 	return 0;
483747503b1SLiam Girdwood }
484747503b1SLiam Girdwood 
485747503b1SLiam Girdwood int hda_dsp_resume(struct snd_sof_dev *sdev)
486747503b1SLiam Girdwood {
48716299326SKeyon Jie 	struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata;
48866e40876SKeyon Jie 	struct pci_dev *pci = to_pci_dev(sdev->dev);
48966e40876SKeyon Jie 
49066e40876SKeyon Jie 	if (sdev->s0_suspend) {
491*816938b2SKai Vehmanen 		hda_codec_i915_display_power(sdev, true);
492*816938b2SKai Vehmanen 
49316299326SKeyon Jie 		/* restore L1SEN bit */
49416299326SKeyon Jie 		if (hda->l1_support_changed)
49516299326SKeyon Jie 			snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR,
49616299326SKeyon Jie 						HDA_VS_INTEL_EM2,
49716299326SKeyon Jie 						HDA_VS_INTEL_EM2_L1SEN, 0);
49816299326SKeyon Jie 
49966e40876SKeyon Jie 		/* restore and disable the system wakeup */
50066e40876SKeyon Jie 		pci_restore_state(pci);
50166e40876SKeyon Jie 		disable_irq_wake(pci->irq);
50266e40876SKeyon Jie 		return 0;
50366e40876SKeyon Jie 	}
50466e40876SKeyon Jie 
505747503b1SLiam Girdwood 	/* init hda controller. DSP cores will be powered up during fw boot */
506fd15f2f5SRander Wang 	return hda_resume(sdev, false);
507747503b1SLiam Girdwood }
508747503b1SLiam Girdwood 
509747503b1SLiam Girdwood int hda_dsp_runtime_resume(struct snd_sof_dev *sdev)
510747503b1SLiam Girdwood {
511747503b1SLiam Girdwood 	/* init hda controller. DSP cores will be powered up during fw boot */
512fd15f2f5SRander Wang 	return hda_resume(sdev, true);
513747503b1SLiam Girdwood }
514747503b1SLiam Girdwood 
51587a6fe80SKai Vehmanen int hda_dsp_runtime_idle(struct snd_sof_dev *sdev)
51687a6fe80SKai Vehmanen {
51787a6fe80SKai Vehmanen 	struct hdac_bus *hbus = sof_to_bus(sdev);
51887a6fe80SKai Vehmanen 
51987a6fe80SKai Vehmanen 	if (hbus->codec_powered) {
52087a6fe80SKai Vehmanen 		dev_dbg(sdev->dev, "some codecs still powered (%08X), not idle\n",
52187a6fe80SKai Vehmanen 			(unsigned int)hbus->codec_powered);
52287a6fe80SKai Vehmanen 		return -EBUSY;
52387a6fe80SKai Vehmanen 	}
52487a6fe80SKai Vehmanen 
52587a6fe80SKai Vehmanen 	return 0;
52687a6fe80SKai Vehmanen }
52787a6fe80SKai Vehmanen 
5281c38c922SFred Oh int hda_dsp_runtime_suspend(struct snd_sof_dev *sdev)
529747503b1SLiam Girdwood {
530747503b1SLiam Girdwood 	/* stop hda controller and power dsp off */
5311c38c922SFred Oh 	return hda_suspend(sdev, true);
532747503b1SLiam Girdwood }
533747503b1SLiam Girdwood 
5341c38c922SFred Oh int hda_dsp_suspend(struct snd_sof_dev *sdev)
535747503b1SLiam Girdwood {
53616299326SKeyon Jie 	struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata;
537747503b1SLiam Girdwood 	struct hdac_bus *bus = sof_to_bus(sdev);
53866e40876SKeyon Jie 	struct pci_dev *pci = to_pci_dev(sdev->dev);
539747503b1SLiam Girdwood 	int ret;
540747503b1SLiam Girdwood 
54166e40876SKeyon Jie 	if (sdev->s0_suspend) {
542*816938b2SKai Vehmanen 		/* we can't keep a wakeref to display driver at suspend */
543*816938b2SKai Vehmanen 		hda_codec_i915_display_power(sdev, false);
544*816938b2SKai Vehmanen 
54516299326SKeyon Jie 		/* enable L1SEN to make sure the system can enter S0Ix */
54616299326SKeyon Jie 		hda->l1_support_changed =
54716299326SKeyon Jie 			snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR,
54816299326SKeyon Jie 						HDA_VS_INTEL_EM2,
54916299326SKeyon Jie 						HDA_VS_INTEL_EM2_L1SEN,
55016299326SKeyon Jie 						HDA_VS_INTEL_EM2_L1SEN);
55116299326SKeyon Jie 
55266e40876SKeyon Jie 		/* enable the system waking up via IPC IRQ */
55366e40876SKeyon Jie 		enable_irq_wake(pci->irq);
55466e40876SKeyon Jie 		pci_save_state(pci);
55566e40876SKeyon Jie 		return 0;
55666e40876SKeyon Jie 	}
55766e40876SKeyon Jie 
558747503b1SLiam Girdwood 	/* stop hda controller and power dsp off */
5591c38c922SFred Oh 	ret = hda_suspend(sdev, false);
560747503b1SLiam Girdwood 	if (ret < 0) {
561747503b1SLiam Girdwood 		dev_err(bus->dev, "error: suspending dsp\n");
562747503b1SLiam Girdwood 		return ret;
563747503b1SLiam Girdwood 	}
564747503b1SLiam Girdwood 
565747503b1SLiam Girdwood 	return 0;
566747503b1SLiam Girdwood }
567ed3baacdSRanjani Sridharan 
5687077a07aSRanjani Sridharan int hda_dsp_set_hw_params_upon_resume(struct snd_sof_dev *sdev)
569ed3baacdSRanjani Sridharan {
5707077a07aSRanjani Sridharan #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
571a3ebccb5SKai Vehmanen 	struct hdac_bus *bus = sof_to_bus(sdev);
5727077a07aSRanjani Sridharan 	struct snd_soc_pcm_runtime *rtd;
573a3ebccb5SKai Vehmanen 	struct hdac_ext_stream *stream;
5747077a07aSRanjani Sridharan 	struct hdac_ext_link *link;
575a3ebccb5SKai Vehmanen 	struct hdac_stream *s;
5767077a07aSRanjani Sridharan 	const char *name;
5777077a07aSRanjani Sridharan 	int stream_tag;
5787077a07aSRanjani Sridharan 
579ed3baacdSRanjani Sridharan 	/* set internal flag for BE */
580ed3baacdSRanjani Sridharan 	list_for_each_entry(s, &bus->stream_list, list) {
581ed3baacdSRanjani Sridharan 		stream = stream_to_hdac_ext_stream(s);
582a3ebccb5SKai Vehmanen 
5837077a07aSRanjani Sridharan 		/*
584934bf822SRander Wang 		 * clear stream. This should already be taken care for running
585934bf822SRander Wang 		 * streams when the SUSPEND trigger is called. But paused
586934bf822SRander Wang 		 * streams do not get suspended, so this needs to be done
587934bf822SRander Wang 		 * explicitly during suspend.
5887077a07aSRanjani Sridharan 		 */
5897077a07aSRanjani Sridharan 		if (stream->link_substream) {
5907077a07aSRanjani Sridharan 			rtd = snd_pcm_substream_chip(stream->link_substream);
5917077a07aSRanjani Sridharan 			name = rtd->codec_dai->component->name;
5927077a07aSRanjani Sridharan 			link = snd_hdac_ext_bus_get_link(bus, name);
5937077a07aSRanjani Sridharan 			if (!link)
5947077a07aSRanjani Sridharan 				return -EINVAL;
595810dbea3SRander Wang 
596810dbea3SRander Wang 			stream->link_prepared = 0;
597810dbea3SRander Wang 
598810dbea3SRander Wang 			if (hdac_stream(stream)->direction ==
599810dbea3SRander Wang 				SNDRV_PCM_STREAM_CAPTURE)
600810dbea3SRander Wang 				continue;
601810dbea3SRander Wang 
6027077a07aSRanjani Sridharan 			stream_tag = hdac_stream(stream)->stream_tag;
6037077a07aSRanjani Sridharan 			snd_hdac_ext_link_clear_stream_id(link, stream_tag);
604a3ebccb5SKai Vehmanen 		}
605ed3baacdSRanjani Sridharan 	}
6067077a07aSRanjani Sridharan #endif
6077077a07aSRanjani Sridharan 	return 0;
608ed3baacdSRanjani Sridharan }
609