xref: /openbmc/linux/sound/soc/sof/intel/hda-loader.c (revision 410a321c)
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  */
102ab222a4aSBard 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)
18043a03d24SPeter Ujfalusi 		target_status = FSR_STATE_FW_ENTERED;
1812a68ff84SPeter Ujfalusi 	else
18243a03d24SPeter Ujfalusi 		target_status = FSR_STATE_INIT_DONE;
1832a68ff84SPeter Ujfalusi 
184d16046ffSLiam Girdwood 	ret = snd_sof_dsp_read_poll_timeout(sdev, HDA_DSP_BAR,
18571778f79SRanjani Sridharan 					chip->rom_status_reg, status,
18643a03d24SPeter Ujfalusi 					(FSR_TO_STATE_CODE(status) == target_status),
187d16046ffSLiam Girdwood 					HDA_DSP_REG_POLL_INTERVAL_US,
188d16046ffSLiam Girdwood 					chip->rom_init_timeout *
189d16046ffSLiam Girdwood 					USEC_PER_MSEC);
190d4165199SRanjani Sridharan 	if (!ret) {
191d4165199SRanjani Sridharan 		/* set enabled cores mask and increment ref count for cores in init_core_mask */
192d4165199SRanjani Sridharan 		sdev->enabled_cores_mask |= chip->init_core_mask;
193d4165199SRanjani Sridharan 		mask = sdev->enabled_cores_mask;
194d4165199SRanjani Sridharan 		for_each_set_bit(j, &mask, SOF_MAX_DSP_NUM_CORES)
195d4165199SRanjani Sridharan 			sdev->dsp_core_ref_count[j]++;
196d16046ffSLiam Girdwood 		return 0;
197d4165199SRanjani Sridharan 	}
198d16046ffSLiam Girdwood 
199776100a4SPierre-Louis Bossart 	if (hda->boot_iteration == HDA_FW_BOOT_ATTEMPTS)
2006a414489SPierre-Louis Bossart 		dev_err(sdev->dev,
20171778f79SRanjani Sridharan 			"%s: timeout with rom_status_reg (%#x) read\n",
20271778f79SRanjani Sridharan 			__func__, chip->rom_status_reg);
2036a414489SPierre-Louis Bossart 
204d16046ffSLiam Girdwood err:
2057511b0edSPeter Ujfalusi 	flags = SOF_DBG_DUMP_PCI | SOF_DBG_DUMP_MBOX | SOF_DBG_DUMP_OPTIONAL;
2068f7ef6fcSRanjani Sridharan 
20723013335SPeter Ujfalusi 	/* after max boot attempts make sure that the dump is printed */
20823013335SPeter Ujfalusi 	if (hda->boot_iteration == HDA_FW_BOOT_ATTEMPTS)
2090ecaa2ffSPeter Ujfalusi 		flags &= ~SOF_DBG_DUMP_OPTIONAL;
2108f7ef6fcSRanjani Sridharan 
2112f148430SPeter Ujfalusi 	dump_msg = kasprintf(GFP_KERNEL, "Boot iteration failed: %d/%d",
2122f148430SPeter Ujfalusi 			     hda->boot_iteration, HDA_FW_BOOT_ATTEMPTS);
2132f148430SPeter Ujfalusi 	snd_sof_dsp_dbg_dump(sdev, dump_msg, flags);
214d4165199SRanjani Sridharan 	hda_dsp_core_reset_power_down(sdev, chip->host_managed_cores_mask);
215d16046ffSLiam Girdwood 
2162f148430SPeter Ujfalusi 	kfree(dump_msg);
217d16046ffSLiam Girdwood 	return ret;
218d16046ffSLiam Girdwood }
219d16046ffSLiam Girdwood 
220d16046ffSLiam Girdwood static int cl_trigger(struct snd_sof_dev *sdev,
2217d88b960SPierre-Louis Bossart 		      struct hdac_ext_stream *hext_stream, int cmd)
222d16046ffSLiam Girdwood {
2237d88b960SPierre-Louis Bossart 	struct hdac_stream *hstream = &hext_stream->hstream;
224d16046ffSLiam Girdwood 	int sd_offset = SOF_STREAM_SD_OFFSET(hstream);
225d16046ffSLiam Girdwood 
226d16046ffSLiam Girdwood 	/* code loader is special case that reuses stream ops */
227d16046ffSLiam Girdwood 	switch (cmd) {
228d16046ffSLiam Girdwood 	case SNDRV_PCM_TRIGGER_START:
229d16046ffSLiam Girdwood 		snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR, SOF_HDA_INTCTL,
230d16046ffSLiam Girdwood 					1 << hstream->index,
231d16046ffSLiam Girdwood 					1 << hstream->index);
232d16046ffSLiam Girdwood 
233d16046ffSLiam Girdwood 		snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR,
234d16046ffSLiam Girdwood 					sd_offset,
235d16046ffSLiam Girdwood 					SOF_HDA_SD_CTL_DMA_START |
236d16046ffSLiam Girdwood 					SOF_HDA_CL_DMA_SD_INT_MASK,
237d16046ffSLiam Girdwood 					SOF_HDA_SD_CTL_DMA_START |
238d16046ffSLiam Girdwood 					SOF_HDA_CL_DMA_SD_INT_MASK);
239d16046ffSLiam Girdwood 
240d16046ffSLiam Girdwood 		hstream->running = true;
241d16046ffSLiam Girdwood 		return 0;
242d16046ffSLiam Girdwood 	default:
2437d88b960SPierre-Louis Bossart 		return hda_dsp_stream_trigger(sdev, hext_stream, cmd);
244d16046ffSLiam Girdwood 	}
245d16046ffSLiam Girdwood }
246d16046ffSLiam Girdwood 
247b4e4c0b9SRanjani Sridharan int hda_cl_cleanup(struct snd_sof_dev *sdev, struct snd_dma_buffer *dmab,
2487d88b960SPierre-Louis Bossart 		   struct hdac_ext_stream *hext_stream)
249d16046ffSLiam Girdwood {
2507d88b960SPierre-Louis Bossart 	struct hdac_stream *hstream = &hext_stream->hstream;
251d16046ffSLiam Girdwood 	int sd_offset = SOF_STREAM_SD_OFFSET(hstream);
252acf705a4SRanjani Sridharan 	int ret = 0;
253d16046ffSLiam Girdwood 
254acf705a4SRanjani Sridharan 	if (hstream->direction == SNDRV_PCM_STREAM_PLAYBACK)
2557d88b960SPierre-Louis Bossart 		ret = hda_dsp_stream_spib_config(sdev, hext_stream, HDA_DSP_SPIB_DISABLE, 0);
256acf705a4SRanjani Sridharan 	else
257acf705a4SRanjani Sridharan 		snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR, sd_offset,
258acf705a4SRanjani Sridharan 					SOF_HDA_SD_CTL_DMA_START, 0);
259d16046ffSLiam Girdwood 
260acf705a4SRanjani Sridharan 	hda_dsp_stream_put(sdev, hstream->direction, hstream->stream_tag);
261d16046ffSLiam Girdwood 	hstream->running = 0;
262d16046ffSLiam Girdwood 	hstream->substream = NULL;
263d16046ffSLiam Girdwood 
264d16046ffSLiam Girdwood 	/* reset BDL address */
265d16046ffSLiam Girdwood 	snd_sof_dsp_write(sdev, HDA_DSP_HDA_BAR,
266d16046ffSLiam Girdwood 			  sd_offset + SOF_HDA_ADSP_REG_CL_SD_BDLPL, 0);
267d16046ffSLiam Girdwood 	snd_sof_dsp_write(sdev, HDA_DSP_HDA_BAR,
268d16046ffSLiam Girdwood 			  sd_offset + SOF_HDA_ADSP_REG_CL_SD_BDLPU, 0);
269d16046ffSLiam Girdwood 
270d16046ffSLiam Girdwood 	snd_sof_dsp_write(sdev, HDA_DSP_HDA_BAR, sd_offset, 0);
271d16046ffSLiam Girdwood 	snd_dma_free_pages(dmab);
272d16046ffSLiam Girdwood 	dmab->area = NULL;
273d16046ffSLiam Girdwood 	hstream->bufsize = 0;
274d16046ffSLiam Girdwood 	hstream->format_val = 0;
275d16046ffSLiam Girdwood 
276d16046ffSLiam Girdwood 	return ret;
277d16046ffSLiam Girdwood }
278d16046ffSLiam Girdwood 
279b4e4c0b9SRanjani Sridharan int hda_cl_copy_fw(struct snd_sof_dev *sdev, struct hdac_ext_stream *hext_stream)
280d16046ffSLiam Girdwood {
28171778f79SRanjani Sridharan 	struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata;
28271778f79SRanjani Sridharan 	const struct sof_intel_dsp_desc *chip = hda->desc;
283d16046ffSLiam Girdwood 	unsigned int reg;
284d16046ffSLiam Girdwood 	int ret, status;
285d16046ffSLiam Girdwood 
2867d88b960SPierre-Louis Bossart 	ret = cl_trigger(sdev, hext_stream, SNDRV_PCM_TRIGGER_START);
287d16046ffSLiam Girdwood 	if (ret < 0) {
288d16046ffSLiam Girdwood 		dev_err(sdev->dev, "error: DMA trigger start failed\n");
289d16046ffSLiam Girdwood 		return ret;
290d16046ffSLiam Girdwood 	}
291d16046ffSLiam Girdwood 
292d16046ffSLiam Girdwood 	status = snd_sof_dsp_read_poll_timeout(sdev, HDA_DSP_BAR,
29371778f79SRanjani Sridharan 					chip->rom_status_reg, reg,
29443a03d24SPeter Ujfalusi 					(FSR_TO_STATE_CODE(reg) == FSR_STATE_FW_ENTERED),
295d16046ffSLiam Girdwood 					HDA_DSP_REG_POLL_INTERVAL_US,
296d16046ffSLiam Girdwood 					HDA_DSP_BASEFW_TIMEOUT_US);
297d16046ffSLiam Girdwood 
29876dc6a2bSPierre-Louis Bossart 	/*
29976dc6a2bSPierre-Louis Bossart 	 * even in case of errors we still need to stop the DMAs,
30076dc6a2bSPierre-Louis Bossart 	 * but we return the initial error should the DMA stop also fail
30176dc6a2bSPierre-Louis Bossart 	 */
30276dc6a2bSPierre-Louis Bossart 
3036a414489SPierre-Louis Bossart 	if (status < 0) {
3046a414489SPierre-Louis Bossart 		dev_err(sdev->dev,
30571778f79SRanjani Sridharan 			"%s: timeout with rom_status_reg (%#x) read\n",
30671778f79SRanjani Sridharan 			__func__, chip->rom_status_reg);
3076a414489SPierre-Louis Bossart 	}
3086a414489SPierre-Louis Bossart 
3097d88b960SPierre-Louis Bossart 	ret = cl_trigger(sdev, hext_stream, SNDRV_PCM_TRIGGER_STOP);
310d16046ffSLiam Girdwood 	if (ret < 0) {
311d16046ffSLiam Girdwood 		dev_err(sdev->dev, "error: DMA trigger stop failed\n");
31276dc6a2bSPierre-Louis Bossart 		if (!status)
31376dc6a2bSPierre-Louis Bossart 			status = ret;
314d16046ffSLiam Girdwood 	}
315d16046ffSLiam Girdwood 
316d16046ffSLiam Girdwood 	return status;
317d16046ffSLiam Girdwood }
318d16046ffSLiam Girdwood 
319acf705a4SRanjani Sridharan int hda_dsp_cl_boot_firmware_iccmax(struct snd_sof_dev *sdev)
320acf705a4SRanjani Sridharan {
321acf705a4SRanjani Sridharan 	struct hdac_ext_stream *iccmax_stream;
322acf705a4SRanjani Sridharan 	struct hdac_bus *bus = sof_to_bus(sdev);
323acf705a4SRanjani Sridharan 	struct firmware stripped_firmware;
324ea5ffef0SPeter Ujfalusi 	struct snd_dma_buffer dmab_bdl;
325acf705a4SRanjani Sridharan 	int ret, ret1;
326acf705a4SRanjani Sridharan 	u8 original_gb;
327acf705a4SRanjani Sridharan 
328acf705a4SRanjani Sridharan 	/* save the original LTRP guardband value */
329acf705a4SRanjani Sridharan 	original_gb = snd_hdac_chip_readb(bus, VS_LTRP) & HDA_VS_INTEL_LTRP_GB_MASK;
330acf705a4SRanjani Sridharan 
331*410a321cSPeter Ujfalusi 	if (sdev->basefw.fw->size <= sdev->basefw.payload_offset) {
332acf705a4SRanjani Sridharan 		dev_err(sdev->dev, "error: firmware size must be greater than firmware offset\n");
333acf705a4SRanjani Sridharan 		return -EINVAL;
334acf705a4SRanjani Sridharan 	}
335acf705a4SRanjani Sridharan 
336*410a321cSPeter Ujfalusi 	stripped_firmware.size = sdev->basefw.fw->size - sdev->basefw.payload_offset;
337acf705a4SRanjani Sridharan 
338acf705a4SRanjani Sridharan 	/* prepare capture stream for ICCMAX */
339b4e4c0b9SRanjani Sridharan 	iccmax_stream = hda_cl_stream_prepare(sdev, HDA_CL_STREAM_FORMAT, stripped_firmware.size,
340ea5ffef0SPeter Ujfalusi 					      &dmab_bdl, SNDRV_PCM_STREAM_CAPTURE);
34101d42d5aSRanjani Sridharan 	if (IS_ERR(iccmax_stream)) {
34201d42d5aSRanjani Sridharan 		dev_err(sdev->dev, "error: dma prepare for ICCMAX stream failed\n");
34301d42d5aSRanjani Sridharan 		return PTR_ERR(iccmax_stream);
344acf705a4SRanjani Sridharan 	}
345acf705a4SRanjani Sridharan 
346acf705a4SRanjani Sridharan 	ret = hda_dsp_cl_boot_firmware(sdev);
347acf705a4SRanjani Sridharan 
348acf705a4SRanjani Sridharan 	/*
349acf705a4SRanjani Sridharan 	 * Perform iccmax stream cleanup. This should be done even if firmware loading fails.
350acf705a4SRanjani Sridharan 	 * If the cleanup also fails, we return the initial error
351acf705a4SRanjani Sridharan 	 */
352b4e4c0b9SRanjani Sridharan 	ret1 = hda_cl_cleanup(sdev, &dmab_bdl, iccmax_stream);
353acf705a4SRanjani Sridharan 	if (ret1 < 0) {
354acf705a4SRanjani Sridharan 		dev_err(sdev->dev, "error: ICCMAX stream cleanup failed\n");
355acf705a4SRanjani Sridharan 
356acf705a4SRanjani Sridharan 		/* set return value to indicate cleanup failure */
357acf705a4SRanjani Sridharan 		if (!ret)
358acf705a4SRanjani Sridharan 			ret = ret1;
359acf705a4SRanjani Sridharan 	}
360acf705a4SRanjani Sridharan 
361acf705a4SRanjani Sridharan 	/* restore the original guardband value after FW boot */
362acf705a4SRanjani Sridharan 	snd_hdac_chip_updateb(bus, VS_LTRP, HDA_VS_INTEL_LTRP_GB_MASK, original_gb);
363acf705a4SRanjani Sridharan 
364acf705a4SRanjani Sridharan 	return ret;
365acf705a4SRanjani Sridharan }
366acf705a4SRanjani Sridharan 
3675fb5f511SKeyon Jie static int hda_dsp_boot_imr(struct snd_sof_dev *sdev)
3685fb5f511SKeyon Jie {
369ab222a4aSBard Liao 	const struct sof_intel_dsp_desc *chip_info;
3705fb5f511SKeyon Jie 	int ret;
3715fb5f511SKeyon Jie 
372ab222a4aSBard Liao 	chip_info = get_chip_info(sdev->pdata);
373ab222a4aSBard Liao 	if (chip_info->cl_init)
374ab222a4aSBard Liao 		ret = chip_info->cl_init(sdev, 0, true);
375ab222a4aSBard Liao 	else
376ab222a4aSBard Liao 		ret = -EINVAL;
377ab222a4aSBard Liao 
378fd2cea16SPeter Ujfalusi 	if (!ret)
3795fb5f511SKeyon Jie 		hda_sdw_process_wakeen(sdev);
3805fb5f511SKeyon Jie 
3815fb5f511SKeyon Jie 	return ret;
3825fb5f511SKeyon Jie }
3835fb5f511SKeyon Jie 
384d16046ffSLiam Girdwood int hda_dsp_cl_boot_firmware(struct snd_sof_dev *sdev)
385d16046ffSLiam Girdwood {
386776100a4SPierre-Louis Bossart 	struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata;
387d16046ffSLiam Girdwood 	struct snd_sof_pdata *plat_data = sdev->pdata;
388d16046ffSLiam Girdwood 	const struct sof_dev_desc *desc = plat_data->desc;
389d16046ffSLiam Girdwood 	const struct sof_intel_dsp_desc *chip_info;
3907d88b960SPierre-Louis Bossart 	struct hdac_ext_stream *hext_stream;
391d16046ffSLiam Girdwood 	struct firmware stripped_firmware;
392ea5ffef0SPeter Ujfalusi 	struct snd_dma_buffer dmab;
39301d42d5aSRanjani Sridharan 	int ret, ret1, i;
394d16046ffSLiam Girdwood 
39557724db1SPeter Ujfalusi 	if (hda->imrboot_supported && !sdev->first_boot && !hda->skip_imr_boot) {
3965fb5f511SKeyon Jie 		dev_dbg(sdev->dev, "IMR restore supported, booting from IMR directly\n");
3972a68ff84SPeter Ujfalusi 		hda->boot_iteration = 0;
3982a68ff84SPeter Ujfalusi 		ret = hda_dsp_boot_imr(sdev);
399fd2cea16SPeter Ujfalusi 		if (!ret)
400fd2cea16SPeter Ujfalusi 			return 0;
4012a68ff84SPeter Ujfalusi 
4022a68ff84SPeter Ujfalusi 		dev_warn(sdev->dev, "IMR restore failed, trying to cold boot\n");
4035fb5f511SKeyon Jie 	}
4045fb5f511SKeyon Jie 
405d16046ffSLiam Girdwood 	chip_info = desc->chip_info;
406d16046ffSLiam Girdwood 
407*410a321cSPeter Ujfalusi 	if (sdev->basefw.fw->size <= sdev->basefw.payload_offset) {
40892be17a5SKarol Trzcinski 		dev_err(sdev->dev, "error: firmware size must be greater than firmware offset\n");
40992be17a5SKarol Trzcinski 		return -EINVAL;
41092be17a5SKarol Trzcinski 	}
41192be17a5SKarol Trzcinski 
412*410a321cSPeter Ujfalusi 	stripped_firmware.data = sdev->basefw.fw->data + sdev->basefw.payload_offset;
413*410a321cSPeter Ujfalusi 	stripped_firmware.size = sdev->basefw.fw->size - sdev->basefw.payload_offset;
414d16046ffSLiam Girdwood 
415d16046ffSLiam Girdwood 	/* init for booting wait */
416d16046ffSLiam Girdwood 	init_waitqueue_head(&sdev->boot_wait);
417d16046ffSLiam Girdwood 
418d16046ffSLiam Girdwood 	/* prepare DMA for code loader stream */
419b4e4c0b9SRanjani Sridharan 	hext_stream = hda_cl_stream_prepare(sdev, HDA_CL_STREAM_FORMAT,
420b4e4c0b9SRanjani Sridharan 					    stripped_firmware.size,
421ea5ffef0SPeter Ujfalusi 					    &dmab, SNDRV_PCM_STREAM_PLAYBACK);
4227d88b960SPierre-Louis Bossart 	if (IS_ERR(hext_stream)) {
42301d42d5aSRanjani Sridharan 		dev_err(sdev->dev, "error: dma prepare for fw loading failed\n");
4247d88b960SPierre-Louis Bossart 		return PTR_ERR(hext_stream);
425d16046ffSLiam Girdwood 	}
426d16046ffSLiam Girdwood 
427ea5ffef0SPeter Ujfalusi 	memcpy(dmab.area, stripped_firmware.data,
428d16046ffSLiam Girdwood 	       stripped_firmware.size);
429d16046ffSLiam Girdwood 
430d16046ffSLiam Girdwood 	/* try ROM init a few times before giving up */
431d16046ffSLiam Girdwood 	for (i = 0; i < HDA_FW_BOOT_ATTEMPTS; i++) {
43253ec7531SRanjani Sridharan 		dev_dbg(sdev->dev,
43353ec7531SRanjani Sridharan 			"Attempting iteration %d of Core En/ROM load...\n", i);
43453ec7531SRanjani Sridharan 
435776100a4SPierre-Louis Bossart 		hda->boot_iteration = i + 1;
436ab222a4aSBard Liao 		if (chip_info->cl_init)
437ab222a4aSBard Liao 			ret = chip_info->cl_init(sdev, hext_stream->hstream.stream_tag, false);
438ab222a4aSBard Liao 		else
439ab222a4aSBard Liao 			ret = -EINVAL;
440d16046ffSLiam Girdwood 
441d16046ffSLiam Girdwood 		/* don't retry anymore if successful */
442d16046ffSLiam Girdwood 		if (!ret)
443d16046ffSLiam Girdwood 			break;
444d16046ffSLiam Girdwood 	}
445d16046ffSLiam Girdwood 
446d16046ffSLiam Girdwood 	if (i == HDA_FW_BOOT_ATTEMPTS) {
447d16046ffSLiam Girdwood 		dev_err(sdev->dev, "error: dsp init failed after %d attempts with err: %d\n",
448d16046ffSLiam Girdwood 			i, ret);
449d16046ffSLiam Girdwood 		goto cleanup;
450d16046ffSLiam Girdwood 	}
451d16046ffSLiam Girdwood 
452d16046ffSLiam Girdwood 	/*
453bbd19cdcSRander Wang 	 * When a SoundWire link is in clock stop state, a Slave
454bbd19cdcSRander Wang 	 * device may trigger in-band wakes for events such as jack
455bbd19cdcSRander Wang 	 * insertion or acoustic event detection. This event will lead
456bbd19cdcSRander Wang 	 * to a WAKEEN interrupt, handled by the PCI device and routed
457bbd19cdcSRander Wang 	 * to PME if the PCI device is in D3. The resume function in
458bbd19cdcSRander Wang 	 * audio PCI driver will be invoked by ACPI for PME event and
459bbd19cdcSRander Wang 	 * initialize the device and process WAKEEN interrupt.
460bbd19cdcSRander Wang 	 *
461bbd19cdcSRander Wang 	 * The WAKEEN interrupt should be processed ASAP to prevent an
462bbd19cdcSRander Wang 	 * interrupt flood, otherwise other interrupts, such IPC,
463bbd19cdcSRander Wang 	 * cannot work normally.  The WAKEEN is handled after the ROM
464bbd19cdcSRander Wang 	 * is initialized successfully, which ensures power rails are
465bbd19cdcSRander Wang 	 * enabled before accessing the SoundWire SHIM registers
466bbd19cdcSRander Wang 	 */
467bbd19cdcSRander Wang 	if (!sdev->first_boot)
468bbd19cdcSRander Wang 		hda_sdw_process_wakeen(sdev);
469bbd19cdcSRander Wang 
470bbd19cdcSRander Wang 	/*
471b2b10aa7SPeter Ujfalusi 	 * Set the boot_iteration to the last attempt, indicating that the
472b2b10aa7SPeter Ujfalusi 	 * DSP ROM has been initialized and from this point there will be no
473b2b10aa7SPeter Ujfalusi 	 * retry done to boot.
474b2b10aa7SPeter Ujfalusi 	 *
475b2b10aa7SPeter Ujfalusi 	 * Continue with code loading and firmware boot
476d16046ffSLiam Girdwood 	 */
477b2b10aa7SPeter Ujfalusi 	hda->boot_iteration = HDA_FW_BOOT_ATTEMPTS;
478b4e4c0b9SRanjani Sridharan 	ret = hda_cl_copy_fw(sdev, hext_stream);
47957724db1SPeter Ujfalusi 	if (!ret) {
480d16046ffSLiam Girdwood 		dev_dbg(sdev->dev, "Firmware download successful, booting...\n");
48157724db1SPeter Ujfalusi 		hda->skip_imr_boot = false;
48257724db1SPeter Ujfalusi 	} else {
4832f148430SPeter Ujfalusi 		snd_sof_dsp_dbg_dump(sdev, "Firmware download failed",
4842f148430SPeter Ujfalusi 				     SOF_DBG_DUMP_PCI | SOF_DBG_DUMP_MBOX);
48557724db1SPeter Ujfalusi 		hda->skip_imr_boot = true;
48657724db1SPeter Ujfalusi 	}
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) &&
5412964e31cSPeter Ujfalusi 		    (sdev->fw_ready.flags & SOF_IPC_INFO_D3_PERSISTENT ||
5422964e31cSPeter Ujfalusi 		     sdev->pdata->ipc_type == SOF_INTEL_IPC4))
5432a68ff84SPeter Ujfalusi 			hdev->imrboot_supported = true;
54451dfed1eSPierre-Louis Bossart 	}
54551dfed1eSPierre-Louis Bossart 
54651dfed1eSPierre-Louis Bossart 	hda_sdw_int_enable(sdev, true);
54751dfed1eSPierre-Louis Bossart 
548d16046ffSLiam Girdwood 	/* re-enable clock gating and power gating */
549d16046ffSLiam Girdwood 	return hda_dsp_ctrl_clock_power_gating(sdev, true);
550d16046ffSLiam Girdwood }
551edbaaadaSFred Oh 
552edbaaadaSFred Oh int hda_dsp_ext_man_get_cavs_config_data(struct snd_sof_dev *sdev,
553edbaaadaSFred Oh 					 const struct sof_ext_man_elem_header *hdr)
554edbaaadaSFred Oh {
555edbaaadaSFred Oh 	const struct sof_ext_man_cavs_config_data *config_data =
556edbaaadaSFred Oh 		container_of(hdr, struct sof_ext_man_cavs_config_data, hdr);
557edbaaadaSFred Oh 	struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata;
558edbaaadaSFred Oh 	int i, elem_num;
559edbaaadaSFred Oh 
560edbaaadaSFred Oh 	/* calculate total number of config data elements */
561edbaaadaSFred Oh 	elem_num = (hdr->size - sizeof(struct sof_ext_man_elem_header))
562edbaaadaSFred Oh 		   / sizeof(struct sof_config_elem);
563edbaaadaSFred Oh 	if (elem_num <= 0) {
564edbaaadaSFred Oh 		dev_err(sdev->dev, "cavs config data is inconsistent: %d\n", elem_num);
565edbaaadaSFred Oh 		return -EINVAL;
566edbaaadaSFred Oh 	}
567edbaaadaSFred Oh 
568edbaaadaSFred Oh 	for (i = 0; i < elem_num; i++)
569edbaaadaSFred Oh 		switch (config_data->elems[i].token) {
570edbaaadaSFred Oh 		case SOF_EXT_MAN_CAVS_CONFIG_EMPTY:
571edbaaadaSFred Oh 			/* skip empty token */
572edbaaadaSFred Oh 			break;
573edbaaadaSFred Oh 		case SOF_EXT_MAN_CAVS_CONFIG_CAVS_LPRO:
574edbaaadaSFred Oh 			hda->clk_config_lpro = config_data->elems[i].value;
575edbaaadaSFred Oh 			dev_dbg(sdev->dev, "FW clock config: %s\n",
576edbaaadaSFred Oh 				hda->clk_config_lpro ? "LPRO" : "HPRO");
577edbaaadaSFred Oh 			break;
578e3a85dbeSFred Oh 		case SOF_EXT_MAN_CAVS_CONFIG_OUTBOX_SIZE:
579e3a85dbeSFred Oh 		case SOF_EXT_MAN_CAVS_CONFIG_INBOX_SIZE:
580e3a85dbeSFred Oh 			/* These elements are defined but not being used yet. No warn is required */
581e3a85dbeSFred Oh 			break;
582edbaaadaSFred Oh 		default:
583e3a85dbeSFred Oh 			dev_info(sdev->dev, "unsupported token type: %d\n",
584edbaaadaSFred Oh 				 config_data->elems[i].token);
585edbaaadaSFred Oh 		}
586edbaaadaSFred Oh 
587edbaaadaSFred Oh 	return 0;
588edbaaadaSFred Oh }
589