xref: /openbmc/linux/sound/soc/sof/intel/hda-loader.c (revision ab222a4a)
1e149ca29SPierre-Louis Bossart // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
2d16046ffSLiam Girdwood //
3d16046ffSLiam Girdwood // This file is provided under a dual BSD/GPLv2 license.  When using or
4d16046ffSLiam Girdwood // redistributing this file, you may do so under either license.
5d16046ffSLiam Girdwood //
6d16046ffSLiam Girdwood // Copyright(c) 2018 Intel Corporation. All rights reserved.
7d16046ffSLiam Girdwood //
8d16046ffSLiam Girdwood // Authors: Liam Girdwood <liam.r.girdwood@linux.intel.com>
9d16046ffSLiam Girdwood //	    Ranjani Sridharan <ranjani.sridharan@linux.intel.com>
10d16046ffSLiam Girdwood //	    Rander Wang <rander.wang@intel.com>
11d16046ffSLiam Girdwood //          Keyon Jie <yang.jie@linux.intel.com>
12d16046ffSLiam Girdwood //
13d16046ffSLiam Girdwood 
14d16046ffSLiam Girdwood /*
15d16046ffSLiam Girdwood  * Hardware interface for HDA DSP code loader
16d16046ffSLiam Girdwood  */
17d16046ffSLiam Girdwood 
18d16046ffSLiam Girdwood #include <linux/firmware.h>
19d16046ffSLiam Girdwood #include <sound/hdaudio_ext.h>
20acf705a4SRanjani Sridharan #include <sound/hda_register.h>
21d16046ffSLiam Girdwood #include <sound/sof.h>
22edbaaadaSFred Oh #include "ext_manifest.h"
23d16046ffSLiam Girdwood #include "../ops.h"
24d7a8fbd1SKeyon Jie #include "../sof-priv.h"
25d16046ffSLiam Girdwood #include "hda.h"
26d16046ffSLiam Girdwood 
27a749d744SKeyon Jie static void hda_ssp_set_cbp_cfp(struct snd_sof_dev *sdev)
28a749d744SKeyon Jie {
29a749d744SKeyon Jie 	struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata;
30a749d744SKeyon Jie 	const struct sof_intel_dsp_desc *chip = hda->desc;
31a749d744SKeyon Jie 	int i;
32a749d744SKeyon Jie 
33a749d744SKeyon Jie 	/* DSP is powered up, set all SSPs to clock consumer/codec provider mode */
34a749d744SKeyon Jie 	for (i = 0; i < chip->ssp_count; i++) {
35a749d744SKeyon Jie 		snd_sof_dsp_update_bits_unlocked(sdev, HDA_DSP_BAR,
36a749d744SKeyon Jie 						 chip->ssp_base_offset
37a749d744SKeyon Jie 						 + i * SSP_DEV_MEM_SIZE
38a749d744SKeyon Jie 						 + SSP_SSC1_OFFSET,
39a749d744SKeyon Jie 						 SSP_SET_CBP_CFP,
40a749d744SKeyon Jie 						 SSP_SET_CBP_CFP);
41a749d744SKeyon Jie 	}
42a749d744SKeyon Jie }
43a749d744SKeyon Jie 
44b4e4c0b9SRanjani Sridharan struct hdac_ext_stream *hda_cl_stream_prepare(struct snd_sof_dev *sdev, unsigned int format,
45d16046ffSLiam Girdwood 					      unsigned int size, struct snd_dma_buffer *dmab,
46d16046ffSLiam Girdwood 					      int direction)
47d16046ffSLiam Girdwood {
487d88b960SPierre-Louis Bossart 	struct hdac_ext_stream *hext_stream;
49d16046ffSLiam Girdwood 	struct hdac_stream *hstream;
50d16046ffSLiam Girdwood 	struct pci_dev *pci = to_pci_dev(sdev->dev);
51d16046ffSLiam Girdwood 	int ret;
52d16046ffSLiam Girdwood 
537d88b960SPierre-Louis Bossart 	hext_stream = hda_dsp_stream_get(sdev, direction, 0);
54d16046ffSLiam Girdwood 
557d88b960SPierre-Louis Bossart 	if (!hext_stream) {
56d16046ffSLiam Girdwood 		dev_err(sdev->dev, "error: no stream available\n");
5701d42d5aSRanjani Sridharan 		return ERR_PTR(-ENODEV);
58d16046ffSLiam Girdwood 	}
597d88b960SPierre-Louis Bossart 	hstream = &hext_stream->hstream;
604ff5f643SKai Vehmanen 	hstream->substream = NULL;
61d16046ffSLiam Girdwood 
62d16046ffSLiam Girdwood 	/* allocate DMA buffer */
63d16046ffSLiam Girdwood 	ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV_SG, &pci->dev, size, dmab);
64d16046ffSLiam Girdwood 	if (ret < 0) {
65ce1f55baSCurtis Malainey 		dev_err(sdev->dev, "error: memory alloc failed: %d\n", ret);
66b7fb0ae0SAmmar Faizi 		goto out_put;
67d16046ffSLiam Girdwood 	}
68d16046ffSLiam Girdwood 
69d16046ffSLiam Girdwood 	hstream->period_bytes = 0;/* initialize period_bytes */
70d16046ffSLiam Girdwood 	hstream->format_val = format;
71d16046ffSLiam Girdwood 	hstream->bufsize = size;
72d16046ffSLiam Girdwood 
73acf705a4SRanjani Sridharan 	if (direction == SNDRV_PCM_STREAM_CAPTURE) {
747d88b960SPierre-Louis Bossart 		ret = hda_dsp_iccmax_stream_hw_params(sdev, hext_stream, dmab, NULL);
75acf705a4SRanjani Sridharan 		if (ret < 0) {
76ce1f55baSCurtis Malainey 			dev_err(sdev->dev, "error: iccmax stream prepare failed: %d\n", ret);
77b7fb0ae0SAmmar Faizi 			goto out_free;
78acf705a4SRanjani Sridharan 		}
79acf705a4SRanjani Sridharan 	} else {
807d88b960SPierre-Louis Bossart 		ret = hda_dsp_stream_hw_params(sdev, hext_stream, dmab, NULL);
81d16046ffSLiam Girdwood 		if (ret < 0) {
82ce1f55baSCurtis Malainey 			dev_err(sdev->dev, "error: hdac prepare failed: %d\n", ret);
83b7fb0ae0SAmmar Faizi 			goto out_free;
84d16046ffSLiam Girdwood 		}
857d88b960SPierre-Louis Bossart 		hda_dsp_stream_spib_config(sdev, hext_stream, HDA_DSP_SPIB_ENABLE, size);
86acf705a4SRanjani Sridharan 	}
87d16046ffSLiam Girdwood 
887d88b960SPierre-Louis Bossart 	return hext_stream;
89d16046ffSLiam Girdwood 
90b7fb0ae0SAmmar Faizi out_free:
91d16046ffSLiam Girdwood 	snd_dma_free_pages(dmab);
92b7fb0ae0SAmmar Faizi out_put:
93b7fb0ae0SAmmar Faizi 	hda_dsp_stream_put(sdev, direction, hstream->stream_tag);
9401d42d5aSRanjani Sridharan 	return ERR_PTR(ret);
95d16046ffSLiam Girdwood }
96d16046ffSLiam Girdwood 
97d16046ffSLiam Girdwood /*
984643e10aSPeter Ujfalusi  * first boot sequence has some extra steps.
994643e10aSPeter Ujfalusi  * power on all host managed cores and only unstall/run the boot core to boot the
1004643e10aSPeter Ujfalusi  * DSP then turn off all non boot cores (if any) is powered on.
101d16046ffSLiam Girdwood  */
102*ab222a4aSBard Liao int cl_dsp_init(struct snd_sof_dev *sdev, int stream_tag, bool imr_boot)
103d16046ffSLiam Girdwood {
104d16046ffSLiam Girdwood 	struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata;
105d16046ffSLiam Girdwood 	const struct sof_intel_dsp_desc *chip = hda->desc;
1062a68ff84SPeter Ujfalusi 	unsigned int status, target_status;
1072a68ff84SPeter Ujfalusi 	u32 flags, ipc_hdr, j;
108d4165199SRanjani Sridharan 	unsigned long mask;
1092f148430SPeter Ujfalusi 	char *dump_msg;
110d16046ffSLiam Girdwood 	int ret;
111d16046ffSLiam Girdwood 
112d16046ffSLiam Girdwood 	/* step 1: power up corex */
113fcb3c775SPeter Ujfalusi 	ret = hda_dsp_core_power_up(sdev, chip->host_managed_cores_mask);
114d16046ffSLiam Girdwood 	if (ret < 0) {
115776100a4SPierre-Louis Bossart 		if (hda->boot_iteration == HDA_FW_BOOT_ATTEMPTS)
116d16046ffSLiam Girdwood 			dev_err(sdev->dev, "error: dsp core 0/1 power up failed\n");
117d16046ffSLiam Girdwood 		goto err;
118d16046ffSLiam Girdwood 	}
119d16046ffSLiam Girdwood 
120a749d744SKeyon Jie 	hda_ssp_set_cbp_cfp(sdev);
12174ed4097SZhu Yingjiang 
1222a68ff84SPeter Ujfalusi 	/* step 2: Send ROM_CONTROL command (stream_tag is ignored for IMR boot) */
1232a68ff84SPeter Ujfalusi 	ipc_hdr = chip->ipc_req_mask | HDA_DSP_ROM_IPC_CONTROL;
1242a68ff84SPeter Ujfalusi 	if (!imr_boot)
1252a68ff84SPeter Ujfalusi 		ipc_hdr |= HDA_DSP_ROM_IPC_PURGE_FW | ((stream_tag - 1) << 9);
1262a68ff84SPeter Ujfalusi 
1272a68ff84SPeter Ujfalusi 	snd_sof_dsp_write(sdev, HDA_DSP_BAR, chip->ipc_req, ipc_hdr);
128d16046ffSLiam Girdwood 
129d16046ffSLiam Girdwood 	/* step 3: unset core 0 reset state & unstall/run core 0 */
1304643e10aSPeter Ujfalusi 	ret = hda_dsp_core_run(sdev, chip->init_core_mask);
131d16046ffSLiam Girdwood 	if (ret < 0) {
132776100a4SPierre-Louis Bossart 		if (hda->boot_iteration == HDA_FW_BOOT_ATTEMPTS)
13353ec7531SRanjani Sridharan 			dev_err(sdev->dev,
13453ec7531SRanjani Sridharan 				"error: dsp core start failed %d\n", ret);
135d16046ffSLiam Girdwood 		ret = -EIO;
136d16046ffSLiam Girdwood 		goto err;
137d16046ffSLiam Girdwood 	}
138d16046ffSLiam Girdwood 
139d16046ffSLiam Girdwood 	/* step 4: wait for IPC DONE bit from ROM */
140d16046ffSLiam Girdwood 	ret = snd_sof_dsp_read_poll_timeout(sdev, HDA_DSP_BAR,
141d16046ffSLiam Girdwood 					    chip->ipc_ack, status,
142d16046ffSLiam Girdwood 					    ((status & chip->ipc_ack_mask)
143d16046ffSLiam Girdwood 						    == chip->ipc_ack_mask),
144d16046ffSLiam Girdwood 					    HDA_DSP_REG_POLL_INTERVAL_US,
145d16046ffSLiam Girdwood 					    HDA_DSP_INIT_TIMEOUT_US);
146d16046ffSLiam Girdwood 
147d16046ffSLiam Girdwood 	if (ret < 0) {
148776100a4SPierre-Louis Bossart 		if (hda->boot_iteration == HDA_FW_BOOT_ATTEMPTS)
14953ec7531SRanjani Sridharan 			dev_err(sdev->dev,
15053ec7531SRanjani Sridharan 				"error: %s: timeout for HIPCIE done\n",
1516a414489SPierre-Louis Bossart 				__func__);
152d16046ffSLiam Girdwood 		goto err;
153d16046ffSLiam Girdwood 	}
154d16046ffSLiam Girdwood 
1558354d9b4SKeyon Jie 	/* set DONE bit to clear the reply IPC message */
1568354d9b4SKeyon Jie 	snd_sof_dsp_update_bits_forced(sdev, HDA_DSP_BAR,
1578354d9b4SKeyon Jie 				       chip->ipc_ack,
1588354d9b4SKeyon Jie 				       chip->ipc_ack_mask,
1598354d9b4SKeyon Jie 				       chip->ipc_ack_mask);
1608354d9b4SKeyon Jie 
161cedd502dSBard Liao 	/* step 5: power down cores that are no longer needed */
162d4165199SRanjani Sridharan 	ret = hda_dsp_core_reset_power_down(sdev, chip->host_managed_cores_mask &
163cedd502dSBard Liao 					   ~(chip->init_core_mask));
164d16046ffSLiam Girdwood 	if (ret < 0) {
165776100a4SPierre-Louis Bossart 		if (hda->boot_iteration == HDA_FW_BOOT_ATTEMPTS)
16653ec7531SRanjani Sridharan 			dev_err(sdev->dev,
16753ec7531SRanjani Sridharan 				"error: dsp core x power down failed\n");
168d16046ffSLiam Girdwood 		goto err;
169d16046ffSLiam Girdwood 	}
170d16046ffSLiam Girdwood 
171d16046ffSLiam Girdwood 	/* step 6: enable IPC interrupts */
172d16046ffSLiam Girdwood 	hda_dsp_ipc_int_enable(sdev);
173d16046ffSLiam Girdwood 
1742a68ff84SPeter Ujfalusi 	/*
1752a68ff84SPeter Ujfalusi 	 * step 7:
1762a68ff84SPeter Ujfalusi 	 * - Cold/Full boot: wait for ROM init to proceed to download the firmware
1772a68ff84SPeter Ujfalusi 	 * - IMR boot: wait for ROM firmware entered (firmware booted up from IMR)
1782a68ff84SPeter Ujfalusi 	 */
1792a68ff84SPeter Ujfalusi 	if (imr_boot)
1802a68ff84SPeter Ujfalusi 		target_status = HDA_DSP_ROM_FW_ENTERED;
1812a68ff84SPeter Ujfalusi 	else
1822a68ff84SPeter Ujfalusi 		target_status = HDA_DSP_ROM_INIT;
1832a68ff84SPeter Ujfalusi 
184d16046ffSLiam Girdwood 	ret = snd_sof_dsp_read_poll_timeout(sdev, HDA_DSP_BAR,
18571778f79SRanjani Sridharan 					chip->rom_status_reg, status,
186d16046ffSLiam Girdwood 					((status & HDA_DSP_ROM_STS_MASK)
1872a68ff84SPeter Ujfalusi 						== target_status),
188d16046ffSLiam Girdwood 					HDA_DSP_REG_POLL_INTERVAL_US,
189d16046ffSLiam Girdwood 					chip->rom_init_timeout *
190d16046ffSLiam Girdwood 					USEC_PER_MSEC);
191d4165199SRanjani Sridharan 	if (!ret) {
192d4165199SRanjani Sridharan 		/* set enabled cores mask and increment ref count for cores in init_core_mask */
193d4165199SRanjani Sridharan 		sdev->enabled_cores_mask |= chip->init_core_mask;
194d4165199SRanjani Sridharan 		mask = sdev->enabled_cores_mask;
195d4165199SRanjani Sridharan 		for_each_set_bit(j, &mask, SOF_MAX_DSP_NUM_CORES)
196d4165199SRanjani Sridharan 			sdev->dsp_core_ref_count[j]++;
197d16046ffSLiam Girdwood 		return 0;
198d4165199SRanjani Sridharan 	}
199d16046ffSLiam Girdwood 
200776100a4SPierre-Louis Bossart 	if (hda->boot_iteration == HDA_FW_BOOT_ATTEMPTS)
2016a414489SPierre-Louis Bossart 		dev_err(sdev->dev,
20271778f79SRanjani Sridharan 			"%s: timeout with rom_status_reg (%#x) read\n",
20371778f79SRanjani Sridharan 			__func__, chip->rom_status_reg);
2046a414489SPierre-Louis Bossart 
205d16046ffSLiam Girdwood err:
2067511b0edSPeter Ujfalusi 	flags = SOF_DBG_DUMP_PCI | SOF_DBG_DUMP_MBOX | SOF_DBG_DUMP_OPTIONAL;
2078f7ef6fcSRanjani Sridharan 
20823013335SPeter Ujfalusi 	/* after max boot attempts make sure that the dump is printed */
20923013335SPeter Ujfalusi 	if (hda->boot_iteration == HDA_FW_BOOT_ATTEMPTS)
2100ecaa2ffSPeter Ujfalusi 		flags &= ~SOF_DBG_DUMP_OPTIONAL;
2118f7ef6fcSRanjani Sridharan 
2122f148430SPeter Ujfalusi 	dump_msg = kasprintf(GFP_KERNEL, "Boot iteration failed: %d/%d",
2132f148430SPeter Ujfalusi 			     hda->boot_iteration, HDA_FW_BOOT_ATTEMPTS);
2142f148430SPeter Ujfalusi 	snd_sof_dsp_dbg_dump(sdev, dump_msg, flags);
215d4165199SRanjani Sridharan 	hda_dsp_core_reset_power_down(sdev, chip->host_managed_cores_mask);
216d16046ffSLiam Girdwood 
2172f148430SPeter Ujfalusi 	kfree(dump_msg);
218d16046ffSLiam Girdwood 	return ret;
219d16046ffSLiam Girdwood }
220d16046ffSLiam Girdwood 
221d16046ffSLiam Girdwood static int cl_trigger(struct snd_sof_dev *sdev,
2227d88b960SPierre-Louis Bossart 		      struct hdac_ext_stream *hext_stream, int cmd)
223d16046ffSLiam Girdwood {
2247d88b960SPierre-Louis Bossart 	struct hdac_stream *hstream = &hext_stream->hstream;
225d16046ffSLiam Girdwood 	int sd_offset = SOF_STREAM_SD_OFFSET(hstream);
226d16046ffSLiam Girdwood 
227d16046ffSLiam Girdwood 	/* code loader is special case that reuses stream ops */
228d16046ffSLiam Girdwood 	switch (cmd) {
229d16046ffSLiam Girdwood 	case SNDRV_PCM_TRIGGER_START:
230d16046ffSLiam Girdwood 		snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR, SOF_HDA_INTCTL,
231d16046ffSLiam Girdwood 					1 << hstream->index,
232d16046ffSLiam Girdwood 					1 << hstream->index);
233d16046ffSLiam Girdwood 
234d16046ffSLiam Girdwood 		snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR,
235d16046ffSLiam Girdwood 					sd_offset,
236d16046ffSLiam Girdwood 					SOF_HDA_SD_CTL_DMA_START |
237d16046ffSLiam Girdwood 					SOF_HDA_CL_DMA_SD_INT_MASK,
238d16046ffSLiam Girdwood 					SOF_HDA_SD_CTL_DMA_START |
239d16046ffSLiam Girdwood 					SOF_HDA_CL_DMA_SD_INT_MASK);
240d16046ffSLiam Girdwood 
241d16046ffSLiam Girdwood 		hstream->running = true;
242d16046ffSLiam Girdwood 		return 0;
243d16046ffSLiam Girdwood 	default:
2447d88b960SPierre-Louis Bossart 		return hda_dsp_stream_trigger(sdev, hext_stream, cmd);
245d16046ffSLiam Girdwood 	}
246d16046ffSLiam Girdwood }
247d16046ffSLiam Girdwood 
248b4e4c0b9SRanjani Sridharan int hda_cl_cleanup(struct snd_sof_dev *sdev, struct snd_dma_buffer *dmab,
2497d88b960SPierre-Louis Bossart 		   struct hdac_ext_stream *hext_stream)
250d16046ffSLiam Girdwood {
2517d88b960SPierre-Louis Bossart 	struct hdac_stream *hstream = &hext_stream->hstream;
252d16046ffSLiam Girdwood 	int sd_offset = SOF_STREAM_SD_OFFSET(hstream);
253acf705a4SRanjani Sridharan 	int ret = 0;
254d16046ffSLiam Girdwood 
255acf705a4SRanjani Sridharan 	if (hstream->direction == SNDRV_PCM_STREAM_PLAYBACK)
2567d88b960SPierre-Louis Bossart 		ret = hda_dsp_stream_spib_config(sdev, hext_stream, HDA_DSP_SPIB_DISABLE, 0);
257acf705a4SRanjani Sridharan 	else
258acf705a4SRanjani Sridharan 		snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR, sd_offset,
259acf705a4SRanjani Sridharan 					SOF_HDA_SD_CTL_DMA_START, 0);
260d16046ffSLiam Girdwood 
261acf705a4SRanjani Sridharan 	hda_dsp_stream_put(sdev, hstream->direction, hstream->stream_tag);
262d16046ffSLiam Girdwood 	hstream->running = 0;
263d16046ffSLiam Girdwood 	hstream->substream = NULL;
264d16046ffSLiam Girdwood 
265d16046ffSLiam Girdwood 	/* reset BDL address */
266d16046ffSLiam Girdwood 	snd_sof_dsp_write(sdev, HDA_DSP_HDA_BAR,
267d16046ffSLiam Girdwood 			  sd_offset + SOF_HDA_ADSP_REG_CL_SD_BDLPL, 0);
268d16046ffSLiam Girdwood 	snd_sof_dsp_write(sdev, HDA_DSP_HDA_BAR,
269d16046ffSLiam Girdwood 			  sd_offset + SOF_HDA_ADSP_REG_CL_SD_BDLPU, 0);
270d16046ffSLiam Girdwood 
271d16046ffSLiam Girdwood 	snd_sof_dsp_write(sdev, HDA_DSP_HDA_BAR, sd_offset, 0);
272d16046ffSLiam Girdwood 	snd_dma_free_pages(dmab);
273d16046ffSLiam Girdwood 	dmab->area = NULL;
274d16046ffSLiam Girdwood 	hstream->bufsize = 0;
275d16046ffSLiam Girdwood 	hstream->format_val = 0;
276d16046ffSLiam Girdwood 
277d16046ffSLiam Girdwood 	return ret;
278d16046ffSLiam Girdwood }
279d16046ffSLiam Girdwood 
280b4e4c0b9SRanjani Sridharan int hda_cl_copy_fw(struct snd_sof_dev *sdev, struct hdac_ext_stream *hext_stream)
281d16046ffSLiam Girdwood {
28271778f79SRanjani Sridharan 	struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata;
28371778f79SRanjani Sridharan 	const struct sof_intel_dsp_desc *chip = hda->desc;
284d16046ffSLiam Girdwood 	unsigned int reg;
285d16046ffSLiam Girdwood 	int ret, status;
286d16046ffSLiam Girdwood 
2877d88b960SPierre-Louis Bossart 	ret = cl_trigger(sdev, hext_stream, SNDRV_PCM_TRIGGER_START);
288d16046ffSLiam Girdwood 	if (ret < 0) {
289d16046ffSLiam Girdwood 		dev_err(sdev->dev, "error: DMA trigger start failed\n");
290d16046ffSLiam Girdwood 		return ret;
291d16046ffSLiam Girdwood 	}
292d16046ffSLiam Girdwood 
293d16046ffSLiam Girdwood 	status = snd_sof_dsp_read_poll_timeout(sdev, HDA_DSP_BAR,
29471778f79SRanjani Sridharan 					chip->rom_status_reg, reg,
295d16046ffSLiam Girdwood 					((reg & HDA_DSP_ROM_STS_MASK)
296d16046ffSLiam Girdwood 						== HDA_DSP_ROM_FW_ENTERED),
297d16046ffSLiam Girdwood 					HDA_DSP_REG_POLL_INTERVAL_US,
298d16046ffSLiam Girdwood 					HDA_DSP_BASEFW_TIMEOUT_US);
299d16046ffSLiam Girdwood 
30076dc6a2bSPierre-Louis Bossart 	/*
30176dc6a2bSPierre-Louis Bossart 	 * even in case of errors we still need to stop the DMAs,
30276dc6a2bSPierre-Louis Bossart 	 * but we return the initial error should the DMA stop also fail
30376dc6a2bSPierre-Louis Bossart 	 */
30476dc6a2bSPierre-Louis Bossart 
3056a414489SPierre-Louis Bossart 	if (status < 0) {
3066a414489SPierre-Louis Bossart 		dev_err(sdev->dev,
30771778f79SRanjani Sridharan 			"%s: timeout with rom_status_reg (%#x) read\n",
30871778f79SRanjani Sridharan 			__func__, chip->rom_status_reg);
3096a414489SPierre-Louis Bossart 	}
3106a414489SPierre-Louis Bossart 
3117d88b960SPierre-Louis Bossart 	ret = cl_trigger(sdev, hext_stream, SNDRV_PCM_TRIGGER_STOP);
312d16046ffSLiam Girdwood 	if (ret < 0) {
313d16046ffSLiam Girdwood 		dev_err(sdev->dev, "error: DMA trigger stop failed\n");
31476dc6a2bSPierre-Louis Bossart 		if (!status)
31576dc6a2bSPierre-Louis Bossart 			status = ret;
316d16046ffSLiam Girdwood 	}
317d16046ffSLiam Girdwood 
318d16046ffSLiam Girdwood 	return status;
319d16046ffSLiam Girdwood }
320d16046ffSLiam Girdwood 
321acf705a4SRanjani Sridharan int hda_dsp_cl_boot_firmware_iccmax(struct snd_sof_dev *sdev)
322acf705a4SRanjani Sridharan {
323acf705a4SRanjani Sridharan 	struct snd_sof_pdata *plat_data = sdev->pdata;
324acf705a4SRanjani Sridharan 	struct hdac_ext_stream *iccmax_stream;
325acf705a4SRanjani Sridharan 	struct hdac_bus *bus = sof_to_bus(sdev);
326acf705a4SRanjani Sridharan 	struct firmware stripped_firmware;
327ea5ffef0SPeter Ujfalusi 	struct snd_dma_buffer dmab_bdl;
328acf705a4SRanjani Sridharan 	int ret, ret1;
329acf705a4SRanjani Sridharan 	u8 original_gb;
330acf705a4SRanjani Sridharan 
331acf705a4SRanjani Sridharan 	/* save the original LTRP guardband value */
332acf705a4SRanjani Sridharan 	original_gb = snd_hdac_chip_readb(bus, VS_LTRP) & HDA_VS_INTEL_LTRP_GB_MASK;
333acf705a4SRanjani Sridharan 
334acf705a4SRanjani Sridharan 	if (plat_data->fw->size <= plat_data->fw_offset) {
335acf705a4SRanjani Sridharan 		dev_err(sdev->dev, "error: firmware size must be greater than firmware offset\n");
336acf705a4SRanjani Sridharan 		return -EINVAL;
337acf705a4SRanjani Sridharan 	}
338acf705a4SRanjani Sridharan 
339acf705a4SRanjani Sridharan 	stripped_firmware.size = plat_data->fw->size - plat_data->fw_offset;
340acf705a4SRanjani Sridharan 
341acf705a4SRanjani Sridharan 	/* prepare capture stream for ICCMAX */
342b4e4c0b9SRanjani Sridharan 	iccmax_stream = hda_cl_stream_prepare(sdev, HDA_CL_STREAM_FORMAT, stripped_firmware.size,
343ea5ffef0SPeter Ujfalusi 					      &dmab_bdl, SNDRV_PCM_STREAM_CAPTURE);
34401d42d5aSRanjani Sridharan 	if (IS_ERR(iccmax_stream)) {
34501d42d5aSRanjani Sridharan 		dev_err(sdev->dev, "error: dma prepare for ICCMAX stream failed\n");
34601d42d5aSRanjani Sridharan 		return PTR_ERR(iccmax_stream);
347acf705a4SRanjani Sridharan 	}
348acf705a4SRanjani Sridharan 
349acf705a4SRanjani Sridharan 	ret = hda_dsp_cl_boot_firmware(sdev);
350acf705a4SRanjani Sridharan 
351acf705a4SRanjani Sridharan 	/*
352acf705a4SRanjani Sridharan 	 * Perform iccmax stream cleanup. This should be done even if firmware loading fails.
353acf705a4SRanjani Sridharan 	 * If the cleanup also fails, we return the initial error
354acf705a4SRanjani Sridharan 	 */
355b4e4c0b9SRanjani Sridharan 	ret1 = hda_cl_cleanup(sdev, &dmab_bdl, iccmax_stream);
356acf705a4SRanjani Sridharan 	if (ret1 < 0) {
357acf705a4SRanjani Sridharan 		dev_err(sdev->dev, "error: ICCMAX stream cleanup failed\n");
358acf705a4SRanjani Sridharan 
359acf705a4SRanjani Sridharan 		/* set return value to indicate cleanup failure */
360acf705a4SRanjani Sridharan 		if (!ret)
361acf705a4SRanjani Sridharan 			ret = ret1;
362acf705a4SRanjani Sridharan 	}
363acf705a4SRanjani Sridharan 
364acf705a4SRanjani Sridharan 	/* restore the original guardband value after FW boot */
365acf705a4SRanjani Sridharan 	snd_hdac_chip_updateb(bus, VS_LTRP, HDA_VS_INTEL_LTRP_GB_MASK, original_gb);
366acf705a4SRanjani Sridharan 
367acf705a4SRanjani Sridharan 	return ret;
368acf705a4SRanjani Sridharan }
369acf705a4SRanjani Sridharan 
3705fb5f511SKeyon Jie static int hda_dsp_boot_imr(struct snd_sof_dev *sdev)
3715fb5f511SKeyon Jie {
372*ab222a4aSBard Liao 	const struct sof_intel_dsp_desc *chip_info;
3735fb5f511SKeyon Jie 	int ret;
3745fb5f511SKeyon Jie 
375*ab222a4aSBard Liao 	chip_info = get_chip_info(sdev->pdata);
376*ab222a4aSBard Liao 	if (chip_info->cl_init)
377*ab222a4aSBard Liao 		ret = chip_info->cl_init(sdev, 0, true);
378*ab222a4aSBard Liao 	else
379*ab222a4aSBard Liao 		ret = -EINVAL;
380*ab222a4aSBard Liao 
381fd2cea16SPeter Ujfalusi 	if (!ret)
3825fb5f511SKeyon Jie 		hda_sdw_process_wakeen(sdev);
3835fb5f511SKeyon Jie 
3845fb5f511SKeyon Jie 	return ret;
3855fb5f511SKeyon Jie }
3865fb5f511SKeyon Jie 
387d16046ffSLiam Girdwood int hda_dsp_cl_boot_firmware(struct snd_sof_dev *sdev)
388d16046ffSLiam Girdwood {
389776100a4SPierre-Louis Bossart 	struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata;
390d16046ffSLiam Girdwood 	struct snd_sof_pdata *plat_data = sdev->pdata;
391d16046ffSLiam Girdwood 	const struct sof_dev_desc *desc = plat_data->desc;
392d16046ffSLiam Girdwood 	const struct sof_intel_dsp_desc *chip_info;
3937d88b960SPierre-Louis Bossart 	struct hdac_ext_stream *hext_stream;
394d16046ffSLiam Girdwood 	struct firmware stripped_firmware;
395ea5ffef0SPeter Ujfalusi 	struct snd_dma_buffer dmab;
39601d42d5aSRanjani Sridharan 	int ret, ret1, i;
397d16046ffSLiam Girdwood 
3982a68ff84SPeter Ujfalusi 	if (hda->imrboot_supported && !sdev->first_boot) {
3995fb5f511SKeyon Jie 		dev_dbg(sdev->dev, "IMR restore supported, booting from IMR directly\n");
4002a68ff84SPeter Ujfalusi 		hda->boot_iteration = 0;
4012a68ff84SPeter Ujfalusi 		ret = hda_dsp_boot_imr(sdev);
402fd2cea16SPeter Ujfalusi 		if (!ret)
403fd2cea16SPeter Ujfalusi 			return 0;
4042a68ff84SPeter Ujfalusi 
4052a68ff84SPeter Ujfalusi 		dev_warn(sdev->dev, "IMR restore failed, trying to cold boot\n");
4065fb5f511SKeyon Jie 	}
4075fb5f511SKeyon Jie 
408d16046ffSLiam Girdwood 	chip_info = desc->chip_info;
409d16046ffSLiam Girdwood 
410523773b9SKarol Trzcinski 	if (plat_data->fw->size <= plat_data->fw_offset) {
41192be17a5SKarol Trzcinski 		dev_err(sdev->dev, "error: firmware size must be greater than firmware offset\n");
41292be17a5SKarol Trzcinski 		return -EINVAL;
41392be17a5SKarol Trzcinski 	}
41492be17a5SKarol Trzcinski 
41592be17a5SKarol Trzcinski 	stripped_firmware.data = plat_data->fw->data + plat_data->fw_offset;
41692be17a5SKarol Trzcinski 	stripped_firmware.size = plat_data->fw->size - plat_data->fw_offset;
417d16046ffSLiam Girdwood 
418d16046ffSLiam Girdwood 	/* init for booting wait */
419d16046ffSLiam Girdwood 	init_waitqueue_head(&sdev->boot_wait);
420d16046ffSLiam Girdwood 
421d16046ffSLiam Girdwood 	/* prepare DMA for code loader stream */
422b4e4c0b9SRanjani Sridharan 	hext_stream = hda_cl_stream_prepare(sdev, HDA_CL_STREAM_FORMAT,
423b4e4c0b9SRanjani Sridharan 					    stripped_firmware.size,
424ea5ffef0SPeter Ujfalusi 					    &dmab, SNDRV_PCM_STREAM_PLAYBACK);
4257d88b960SPierre-Louis Bossart 	if (IS_ERR(hext_stream)) {
42601d42d5aSRanjani Sridharan 		dev_err(sdev->dev, "error: dma prepare for fw loading failed\n");
4277d88b960SPierre-Louis Bossart 		return PTR_ERR(hext_stream);
428d16046ffSLiam Girdwood 	}
429d16046ffSLiam Girdwood 
430ea5ffef0SPeter Ujfalusi 	memcpy(dmab.area, stripped_firmware.data,
431d16046ffSLiam Girdwood 	       stripped_firmware.size);
432d16046ffSLiam Girdwood 
433d16046ffSLiam Girdwood 	/* try ROM init a few times before giving up */
434d16046ffSLiam Girdwood 	for (i = 0; i < HDA_FW_BOOT_ATTEMPTS; i++) {
43553ec7531SRanjani Sridharan 		dev_dbg(sdev->dev,
43653ec7531SRanjani Sridharan 			"Attempting iteration %d of Core En/ROM load...\n", i);
43753ec7531SRanjani Sridharan 
438776100a4SPierre-Louis Bossart 		hda->boot_iteration = i + 1;
439*ab222a4aSBard Liao 		if (chip_info->cl_init)
440*ab222a4aSBard Liao 			ret = chip_info->cl_init(sdev, hext_stream->hstream.stream_tag, false);
441*ab222a4aSBard Liao 		else
442*ab222a4aSBard Liao 			ret = -EINVAL;
443d16046ffSLiam Girdwood 
444d16046ffSLiam Girdwood 		/* don't retry anymore if successful */
445d16046ffSLiam Girdwood 		if (!ret)
446d16046ffSLiam Girdwood 			break;
447d16046ffSLiam Girdwood 	}
448d16046ffSLiam Girdwood 
449d16046ffSLiam Girdwood 	if (i == HDA_FW_BOOT_ATTEMPTS) {
450d16046ffSLiam Girdwood 		dev_err(sdev->dev, "error: dsp init failed after %d attempts with err: %d\n",
451d16046ffSLiam Girdwood 			i, ret);
452d16046ffSLiam Girdwood 		goto cleanup;
453d16046ffSLiam Girdwood 	}
454d16046ffSLiam Girdwood 
455d16046ffSLiam Girdwood 	/*
456bbd19cdcSRander Wang 	 * When a SoundWire link is in clock stop state, a Slave
457bbd19cdcSRander Wang 	 * device may trigger in-band wakes for events such as jack
458bbd19cdcSRander Wang 	 * insertion or acoustic event detection. This event will lead
459bbd19cdcSRander Wang 	 * to a WAKEEN interrupt, handled by the PCI device and routed
460bbd19cdcSRander Wang 	 * to PME if the PCI device is in D3. The resume function in
461bbd19cdcSRander Wang 	 * audio PCI driver will be invoked by ACPI for PME event and
462bbd19cdcSRander Wang 	 * initialize the device and process WAKEEN interrupt.
463bbd19cdcSRander Wang 	 *
464bbd19cdcSRander Wang 	 * The WAKEEN interrupt should be processed ASAP to prevent an
465bbd19cdcSRander Wang 	 * interrupt flood, otherwise other interrupts, such IPC,
466bbd19cdcSRander Wang 	 * cannot work normally.  The WAKEEN is handled after the ROM
467bbd19cdcSRander Wang 	 * is initialized successfully, which ensures power rails are
468bbd19cdcSRander Wang 	 * enabled before accessing the SoundWire SHIM registers
469bbd19cdcSRander Wang 	 */
470bbd19cdcSRander Wang 	if (!sdev->first_boot)
471bbd19cdcSRander Wang 		hda_sdw_process_wakeen(sdev);
472bbd19cdcSRander Wang 
473bbd19cdcSRander Wang 	/*
474b2b10aa7SPeter Ujfalusi 	 * Set the boot_iteration to the last attempt, indicating that the
475b2b10aa7SPeter Ujfalusi 	 * DSP ROM has been initialized and from this point there will be no
476b2b10aa7SPeter Ujfalusi 	 * retry done to boot.
477b2b10aa7SPeter Ujfalusi 	 *
478b2b10aa7SPeter Ujfalusi 	 * Continue with code loading and firmware boot
479d16046ffSLiam Girdwood 	 */
480b2b10aa7SPeter Ujfalusi 	hda->boot_iteration = HDA_FW_BOOT_ATTEMPTS;
481b4e4c0b9SRanjani Sridharan 	ret = hda_cl_copy_fw(sdev, hext_stream);
4822f148430SPeter Ujfalusi 	if (!ret)
483d16046ffSLiam Girdwood 		dev_dbg(sdev->dev, "Firmware download successful, booting...\n");
4842f148430SPeter Ujfalusi 	else
4852f148430SPeter Ujfalusi 		snd_sof_dsp_dbg_dump(sdev, "Firmware download failed",
4862f148430SPeter Ujfalusi 				     SOF_DBG_DUMP_PCI | SOF_DBG_DUMP_MBOX);
487d16046ffSLiam Girdwood 
488d16046ffSLiam Girdwood cleanup:
489d16046ffSLiam Girdwood 	/*
490d16046ffSLiam Girdwood 	 * Perform codeloader stream cleanup.
491d16046ffSLiam Girdwood 	 * This should be done even if firmware loading fails.
49276dc6a2bSPierre-Louis Bossart 	 * If the cleanup also fails, we return the initial error
493d16046ffSLiam Girdwood 	 */
494b4e4c0b9SRanjani Sridharan 	ret1 = hda_cl_cleanup(sdev, &dmab, hext_stream);
495d16046ffSLiam Girdwood 	if (ret1 < 0) {
496d16046ffSLiam Girdwood 		dev_err(sdev->dev, "error: Code loader DSP cleanup failed\n");
497d16046ffSLiam Girdwood 
498d16046ffSLiam Girdwood 		/* set return value to indicate cleanup failure */
49976dc6a2bSPierre-Louis Bossart 		if (!ret)
500d16046ffSLiam Girdwood 			ret = ret1;
501d16046ffSLiam Girdwood 	}
502d16046ffSLiam Girdwood 
503d16046ffSLiam Girdwood 	/*
50452e4d0aeSPierre-Louis Bossart 	 * return primary core id if both fw copy
505d16046ffSLiam Girdwood 	 * and stream clean up are successful
506d16046ffSLiam Girdwood 	 */
507d16046ffSLiam Girdwood 	if (!ret)
508d16046ffSLiam Girdwood 		return chip_info->init_core_mask;
509d16046ffSLiam Girdwood 
510d16046ffSLiam Girdwood 	/* disable DSP */
511d16046ffSLiam Girdwood 	snd_sof_dsp_update_bits(sdev, HDA_DSP_PP_BAR,
512d16046ffSLiam Girdwood 				SOF_HDA_REG_PP_PPCTL,
513d16046ffSLiam Girdwood 				SOF_HDA_PPCTL_GPROCEN, 0);
514d16046ffSLiam Girdwood 	return ret;
515d16046ffSLiam Girdwood }
516d16046ffSLiam Girdwood 
517d16046ffSLiam Girdwood /* pre fw run operations */
518d16046ffSLiam Girdwood int hda_dsp_pre_fw_run(struct snd_sof_dev *sdev)
519d16046ffSLiam Girdwood {
520d16046ffSLiam Girdwood 	/* disable clock gating and power gating */
521d16046ffSLiam Girdwood 	return hda_dsp_ctrl_clock_power_gating(sdev, false);
522d16046ffSLiam Girdwood }
523d16046ffSLiam Girdwood 
524d16046ffSLiam Girdwood /* post fw run operations */
525d16046ffSLiam Girdwood int hda_dsp_post_fw_run(struct snd_sof_dev *sdev)
526d16046ffSLiam Girdwood {
52751dfed1eSPierre-Louis Bossart 	int ret;
52851dfed1eSPierre-Louis Bossart 
52951dfed1eSPierre-Louis Bossart 	if (sdev->first_boot) {
5302a68ff84SPeter Ujfalusi 		struct sof_intel_hda_dev *hdev = sdev->pdata->hw_pdata;
5312a68ff84SPeter Ujfalusi 
53251dfed1eSPierre-Louis Bossart 		ret = hda_sdw_startup(sdev);
53351dfed1eSPierre-Louis Bossart 		if (ret < 0) {
53451dfed1eSPierre-Louis Bossart 			dev_err(sdev->dev,
53551dfed1eSPierre-Louis Bossart 				"error: could not startup SoundWire links\n");
53651dfed1eSPierre-Louis Bossart 			return ret;
53751dfed1eSPierre-Louis Bossart 		}
5382a68ff84SPeter Ujfalusi 
5392a68ff84SPeter Ujfalusi 		/* Check if IMR boot is usable */
5402a68ff84SPeter Ujfalusi 		if (!sof_debug_check_flag(SOF_DBG_IGNORE_D3_PERSISTENT) &&
5412a68ff84SPeter Ujfalusi 		    sdev->fw_ready.flags & SOF_IPC_INFO_D3_PERSISTENT)
5422a68ff84SPeter Ujfalusi 			hdev->imrboot_supported = true;
54351dfed1eSPierre-Louis Bossart 	}
54451dfed1eSPierre-Louis Bossart 
54551dfed1eSPierre-Louis Bossart 	hda_sdw_int_enable(sdev, true);
54651dfed1eSPierre-Louis Bossart 
547d16046ffSLiam Girdwood 	/* re-enable clock gating and power gating */
548d16046ffSLiam Girdwood 	return hda_dsp_ctrl_clock_power_gating(sdev, true);
549d16046ffSLiam Girdwood }
550edbaaadaSFred Oh 
551edbaaadaSFred Oh int hda_dsp_ext_man_get_cavs_config_data(struct snd_sof_dev *sdev,
552edbaaadaSFred Oh 					 const struct sof_ext_man_elem_header *hdr)
553edbaaadaSFred Oh {
554edbaaadaSFred Oh 	const struct sof_ext_man_cavs_config_data *config_data =
555edbaaadaSFred Oh 		container_of(hdr, struct sof_ext_man_cavs_config_data, hdr);
556edbaaadaSFred Oh 	struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata;
557edbaaadaSFred Oh 	int i, elem_num;
558edbaaadaSFred Oh 
559edbaaadaSFred Oh 	/* calculate total number of config data elements */
560edbaaadaSFred Oh 	elem_num = (hdr->size - sizeof(struct sof_ext_man_elem_header))
561edbaaadaSFred Oh 		   / sizeof(struct sof_config_elem);
562edbaaadaSFred Oh 	if (elem_num <= 0) {
563edbaaadaSFred Oh 		dev_err(sdev->dev, "cavs config data is inconsistent: %d\n", elem_num);
564edbaaadaSFred Oh 		return -EINVAL;
565edbaaadaSFred Oh 	}
566edbaaadaSFred Oh 
567edbaaadaSFred Oh 	for (i = 0; i < elem_num; i++)
568edbaaadaSFred Oh 		switch (config_data->elems[i].token) {
569edbaaadaSFred Oh 		case SOF_EXT_MAN_CAVS_CONFIG_EMPTY:
570edbaaadaSFred Oh 			/* skip empty token */
571edbaaadaSFred Oh 			break;
572edbaaadaSFred Oh 		case SOF_EXT_MAN_CAVS_CONFIG_CAVS_LPRO:
573edbaaadaSFred Oh 			hda->clk_config_lpro = config_data->elems[i].value;
574edbaaadaSFred Oh 			dev_dbg(sdev->dev, "FW clock config: %s\n",
575edbaaadaSFred Oh 				hda->clk_config_lpro ? "LPRO" : "HPRO");
576edbaaadaSFred Oh 			break;
577e3a85dbeSFred Oh 		case SOF_EXT_MAN_CAVS_CONFIG_OUTBOX_SIZE:
578e3a85dbeSFred Oh 		case SOF_EXT_MAN_CAVS_CONFIG_INBOX_SIZE:
579e3a85dbeSFred Oh 			/* These elements are defined but not being used yet. No warn is required */
580e3a85dbeSFred Oh 			break;
581edbaaadaSFred Oh 		default:
582e3a85dbeSFred Oh 			dev_info(sdev->dev, "unsupported token type: %d\n",
583edbaaadaSFred Oh 				 config_data->elems[i].token);
584edbaaadaSFred Oh 		}
585edbaaadaSFred Oh 
586edbaaadaSFred Oh 	return 0;
587edbaaadaSFred Oh }
588