1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
3 // This file is provided under a dual BSD/GPLv2 license. When using or
4 // redistributing this file, you may do so under either license.
6 // Copyright(c) 2018 Intel Corporation. All rights reserved.
8 // Authors: Liam Girdwood <liam.r.girdwood@linux.intel.com>
9 // Ranjani Sridharan <ranjani.sridharan@linux.intel.com>
10 // Rander Wang <rander.wang@intel.com>
11 // Keyon Jie <yang.jie@linux.intel.com>
15 * Hardware interface for HDA DSP code loader
18 #include <linux/firmware.h>
19 #include <sound/hdaudio_ext.h>
20 #include <sound/hda_register.h>
21 #include <sound/sof.h>
22 #include "ext_manifest.h"
24 #include "../sof-priv.h"
27 static void hda_ssp_set_cbp_cfp(struct snd_sof_dev *sdev)
29 struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata;
30 const struct sof_intel_dsp_desc *chip = hda->desc;
33 /* DSP is powered up, set all SSPs to clock consumer/codec provider mode */
34 for (i = 0; i < chip->ssp_count; i++) {
35 snd_sof_dsp_update_bits_unlocked(sdev, HDA_DSP_BAR,
37 + i * SSP_DEV_MEM_SIZE
44 struct hdac_ext_stream *hda_cl_stream_prepare(struct snd_sof_dev *sdev, unsigned int format,
45 unsigned int size, struct snd_dma_buffer *dmab,
48 struct hdac_ext_stream *hext_stream;
49 struct hdac_stream *hstream;
50 struct pci_dev *pci = to_pci_dev(sdev->dev);
53 hext_stream = hda_dsp_stream_get(sdev, direction, 0);
56 dev_err(sdev->dev, "error: no stream available\n");
57 return ERR_PTR(-ENODEV);
59 hstream = &hext_stream->hstream;
60 hstream->substream = NULL;
62 /* allocate DMA buffer */
63 ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV_SG, &pci->dev, size, dmab);
65 dev_err(sdev->dev, "error: memory alloc failed: %d\n", ret);
69 hstream->period_bytes = 0;/* initialize period_bytes */
70 hstream->format_val = format;
71 hstream->bufsize = size;
73 if (direction == SNDRV_PCM_STREAM_CAPTURE) {
74 ret = hda_dsp_iccmax_stream_hw_params(sdev, hext_stream, dmab, NULL);
76 dev_err(sdev->dev, "error: iccmax stream prepare failed: %d\n", ret);
80 ret = hda_dsp_stream_hw_params(sdev, hext_stream, dmab, NULL);
82 dev_err(sdev->dev, "error: hdac prepare failed: %d\n", ret);
85 hda_dsp_stream_spib_config(sdev, hext_stream, HDA_DSP_SPIB_ENABLE, size);
91 snd_dma_free_pages(dmab);
93 hda_dsp_stream_put(sdev, direction, hstream->stream_tag);
98 * first boot sequence has some extra steps.
99 * power on all host managed cores and only unstall/run the boot core to boot the
100 * DSP then turn off all non boot cores (if any) is powered on.
102 static int cl_dsp_init(struct snd_sof_dev *sdev, int stream_tag, bool imr_boot)
104 struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata;
105 const struct sof_intel_dsp_desc *chip = hda->desc;
106 unsigned int status, target_status;
107 u32 flags, ipc_hdr, j;
112 /* step 1: power up corex */
113 ret = hda_dsp_core_power_up(sdev, chip->host_managed_cores_mask);
115 if (hda->boot_iteration == HDA_FW_BOOT_ATTEMPTS)
116 dev_err(sdev->dev, "error: dsp core 0/1 power up failed\n");
120 hda_ssp_set_cbp_cfp(sdev);
122 /* step 2: Send ROM_CONTROL command (stream_tag is ignored for IMR boot) */
123 ipc_hdr = chip->ipc_req_mask | HDA_DSP_ROM_IPC_CONTROL;
125 ipc_hdr |= HDA_DSP_ROM_IPC_PURGE_FW | ((stream_tag - 1) << 9);
127 snd_sof_dsp_write(sdev, HDA_DSP_BAR, chip->ipc_req, ipc_hdr);
129 /* step 3: unset core 0 reset state & unstall/run core 0 */
130 ret = hda_dsp_core_run(sdev, chip->init_core_mask);
132 if (hda->boot_iteration == HDA_FW_BOOT_ATTEMPTS)
134 "error: dsp core start failed %d\n", ret);
139 /* step 4: wait for IPC DONE bit from ROM */
140 ret = snd_sof_dsp_read_poll_timeout(sdev, HDA_DSP_BAR,
141 chip->ipc_ack, status,
142 ((status & chip->ipc_ack_mask)
143 == chip->ipc_ack_mask),
144 HDA_DSP_REG_POLL_INTERVAL_US,
145 HDA_DSP_INIT_TIMEOUT_US);
148 if (hda->boot_iteration == HDA_FW_BOOT_ATTEMPTS)
150 "error: %s: timeout for HIPCIE done\n",
155 /* set DONE bit to clear the reply IPC message */
156 snd_sof_dsp_update_bits_forced(sdev, HDA_DSP_BAR,
161 /* step 5: power down cores that are no longer needed */
162 ret = hda_dsp_core_reset_power_down(sdev, chip->host_managed_cores_mask &
163 ~(chip->init_core_mask));
165 if (hda->boot_iteration == HDA_FW_BOOT_ATTEMPTS)
167 "error: dsp core x power down failed\n");
171 /* step 6: enable IPC interrupts */
172 hda_dsp_ipc_int_enable(sdev);
176 * - Cold/Full boot: wait for ROM init to proceed to download the firmware
177 * - IMR boot: wait for ROM firmware entered (firmware booted up from IMR)
180 target_status = HDA_DSP_ROM_FW_ENTERED;
182 target_status = HDA_DSP_ROM_INIT;
184 ret = snd_sof_dsp_read_poll_timeout(sdev, HDA_DSP_BAR,
185 chip->rom_status_reg, status,
186 ((status & HDA_DSP_ROM_STS_MASK)
188 HDA_DSP_REG_POLL_INTERVAL_US,
189 chip->rom_init_timeout *
192 /* set enabled cores mask and increment ref count for cores in init_core_mask */
193 sdev->enabled_cores_mask |= chip->init_core_mask;
194 mask = sdev->enabled_cores_mask;
195 for_each_set_bit(j, &mask, SOF_MAX_DSP_NUM_CORES)
196 sdev->dsp_core_ref_count[j]++;
200 if (hda->boot_iteration == HDA_FW_BOOT_ATTEMPTS)
202 "%s: timeout with rom_status_reg (%#x) read\n",
203 __func__, chip->rom_status_reg);
206 flags = SOF_DBG_DUMP_PCI | SOF_DBG_DUMP_MBOX | SOF_DBG_DUMP_OPTIONAL;
208 /* after max boot attempts make sure that the dump is printed */
209 if (hda->boot_iteration == HDA_FW_BOOT_ATTEMPTS)
210 flags &= ~SOF_DBG_DUMP_OPTIONAL;
212 dump_msg = kasprintf(GFP_KERNEL, "Boot iteration failed: %d/%d",
213 hda->boot_iteration, HDA_FW_BOOT_ATTEMPTS);
214 snd_sof_dsp_dbg_dump(sdev, dump_msg, flags);
215 hda_dsp_core_reset_power_down(sdev, chip->host_managed_cores_mask);
221 static int cl_trigger(struct snd_sof_dev *sdev,
222 struct hdac_ext_stream *hext_stream, int cmd)
224 struct hdac_stream *hstream = &hext_stream->hstream;
225 int sd_offset = SOF_STREAM_SD_OFFSET(hstream);
227 /* code loader is special case that reuses stream ops */
229 case SNDRV_PCM_TRIGGER_START:
230 snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR, SOF_HDA_INTCTL,
232 1 << hstream->index);
234 snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR,
236 SOF_HDA_SD_CTL_DMA_START |
237 SOF_HDA_CL_DMA_SD_INT_MASK,
238 SOF_HDA_SD_CTL_DMA_START |
239 SOF_HDA_CL_DMA_SD_INT_MASK);
241 hstream->running = true;
244 return hda_dsp_stream_trigger(sdev, hext_stream, cmd);
248 int hda_cl_cleanup(struct snd_sof_dev *sdev, struct snd_dma_buffer *dmab,
249 struct hdac_ext_stream *hext_stream)
251 struct hdac_stream *hstream = &hext_stream->hstream;
252 int sd_offset = SOF_STREAM_SD_OFFSET(hstream);
255 if (hstream->direction == SNDRV_PCM_STREAM_PLAYBACK)
256 ret = hda_dsp_stream_spib_config(sdev, hext_stream, HDA_DSP_SPIB_DISABLE, 0);
258 snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR, sd_offset,
259 SOF_HDA_SD_CTL_DMA_START, 0);
261 hda_dsp_stream_put(sdev, hstream->direction, hstream->stream_tag);
262 hstream->running = 0;
263 hstream->substream = NULL;
265 /* reset BDL address */
266 snd_sof_dsp_write(sdev, HDA_DSP_HDA_BAR,
267 sd_offset + SOF_HDA_ADSP_REG_CL_SD_BDLPL, 0);
268 snd_sof_dsp_write(sdev, HDA_DSP_HDA_BAR,
269 sd_offset + SOF_HDA_ADSP_REG_CL_SD_BDLPU, 0);
271 snd_sof_dsp_write(sdev, HDA_DSP_HDA_BAR, sd_offset, 0);
272 snd_dma_free_pages(dmab);
274 hstream->bufsize = 0;
275 hstream->format_val = 0;
280 int hda_cl_copy_fw(struct snd_sof_dev *sdev, struct hdac_ext_stream *hext_stream)
282 struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata;
283 const struct sof_intel_dsp_desc *chip = hda->desc;
287 ret = cl_trigger(sdev, hext_stream, SNDRV_PCM_TRIGGER_START);
289 dev_err(sdev->dev, "error: DMA trigger start failed\n");
293 status = snd_sof_dsp_read_poll_timeout(sdev, HDA_DSP_BAR,
294 chip->rom_status_reg, reg,
295 ((reg & HDA_DSP_ROM_STS_MASK)
296 == HDA_DSP_ROM_FW_ENTERED),
297 HDA_DSP_REG_POLL_INTERVAL_US,
298 HDA_DSP_BASEFW_TIMEOUT_US);
301 * even in case of errors we still need to stop the DMAs,
302 * but we return the initial error should the DMA stop also fail
307 "%s: timeout with rom_status_reg (%#x) read\n",
308 __func__, chip->rom_status_reg);
311 ret = cl_trigger(sdev, hext_stream, SNDRV_PCM_TRIGGER_STOP);
313 dev_err(sdev->dev, "error: DMA trigger stop failed\n");
321 int hda_dsp_cl_boot_firmware_iccmax(struct snd_sof_dev *sdev)
323 struct snd_sof_pdata *plat_data = sdev->pdata;
324 struct hdac_ext_stream *iccmax_stream;
325 struct hdac_bus *bus = sof_to_bus(sdev);
326 struct firmware stripped_firmware;
327 struct snd_dma_buffer dmab_bdl;
331 /* save the original LTRP guardband value */
332 original_gb = snd_hdac_chip_readb(bus, VS_LTRP) & HDA_VS_INTEL_LTRP_GB_MASK;
334 if (plat_data->fw->size <= plat_data->fw_offset) {
335 dev_err(sdev->dev, "error: firmware size must be greater than firmware offset\n");
339 stripped_firmware.size = plat_data->fw->size - plat_data->fw_offset;
341 /* prepare capture stream for ICCMAX */
342 iccmax_stream = hda_cl_stream_prepare(sdev, HDA_CL_STREAM_FORMAT, stripped_firmware.size,
343 &dmab_bdl, SNDRV_PCM_STREAM_CAPTURE);
344 if (IS_ERR(iccmax_stream)) {
345 dev_err(sdev->dev, "error: dma prepare for ICCMAX stream failed\n");
346 return PTR_ERR(iccmax_stream);
349 ret = hda_dsp_cl_boot_firmware(sdev);
352 * Perform iccmax stream cleanup. This should be done even if firmware loading fails.
353 * If the cleanup also fails, we return the initial error
355 ret1 = hda_cl_cleanup(sdev, &dmab_bdl, iccmax_stream);
357 dev_err(sdev->dev, "error: ICCMAX stream cleanup failed\n");
359 /* set return value to indicate cleanup failure */
364 /* restore the original guardband value after FW boot */
365 snd_hdac_chip_updateb(bus, VS_LTRP, HDA_VS_INTEL_LTRP_GB_MASK, original_gb);
370 static int hda_dsp_boot_imr(struct snd_sof_dev *sdev)
374 ret = cl_dsp_init(sdev, 0, true);
376 hda_sdw_process_wakeen(sdev);
381 int hda_dsp_cl_boot_firmware(struct snd_sof_dev *sdev)
383 struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata;
384 struct snd_sof_pdata *plat_data = sdev->pdata;
385 const struct sof_dev_desc *desc = plat_data->desc;
386 const struct sof_intel_dsp_desc *chip_info;
387 struct hdac_ext_stream *hext_stream;
388 struct firmware stripped_firmware;
389 struct snd_dma_buffer dmab;
392 if (sdev->system_suspend_target < SOF_SUSPEND_S4 &&
393 hda->imrboot_supported && !sdev->first_boot) {
394 dev_dbg(sdev->dev, "IMR restore supported, booting from IMR directly\n");
395 hda->boot_iteration = 0;
396 ret = hda_dsp_boot_imr(sdev);
400 dev_warn(sdev->dev, "IMR restore failed, trying to cold boot\n");
403 chip_info = desc->chip_info;
405 if (plat_data->fw->size <= plat_data->fw_offset) {
406 dev_err(sdev->dev, "error: firmware size must be greater than firmware offset\n");
410 stripped_firmware.data = plat_data->fw->data + plat_data->fw_offset;
411 stripped_firmware.size = plat_data->fw->size - plat_data->fw_offset;
413 /* init for booting wait */
414 init_waitqueue_head(&sdev->boot_wait);
416 /* prepare DMA for code loader stream */
417 hext_stream = hda_cl_stream_prepare(sdev, HDA_CL_STREAM_FORMAT,
418 stripped_firmware.size,
419 &dmab, SNDRV_PCM_STREAM_PLAYBACK);
420 if (IS_ERR(hext_stream)) {
421 dev_err(sdev->dev, "error: dma prepare for fw loading failed\n");
422 return PTR_ERR(hext_stream);
425 memcpy(dmab.area, stripped_firmware.data,
426 stripped_firmware.size);
428 /* try ROM init a few times before giving up */
429 for (i = 0; i < HDA_FW_BOOT_ATTEMPTS; i++) {
431 "Attempting iteration %d of Core En/ROM load...\n", i);
433 hda->boot_iteration = i + 1;
434 ret = cl_dsp_init(sdev, hext_stream->hstream.stream_tag, false);
436 /* don't retry anymore if successful */
441 if (i == HDA_FW_BOOT_ATTEMPTS) {
442 dev_err(sdev->dev, "error: dsp init failed after %d attempts with err: %d\n",
448 * When a SoundWire link is in clock stop state, a Slave
449 * device may trigger in-band wakes for events such as jack
450 * insertion or acoustic event detection. This event will lead
451 * to a WAKEEN interrupt, handled by the PCI device and routed
452 * to PME if the PCI device is in D3. The resume function in
453 * audio PCI driver will be invoked by ACPI for PME event and
454 * initialize the device and process WAKEEN interrupt.
456 * The WAKEEN interrupt should be processed ASAP to prevent an
457 * interrupt flood, otherwise other interrupts, such IPC,
458 * cannot work normally. The WAKEEN is handled after the ROM
459 * is initialized successfully, which ensures power rails are
460 * enabled before accessing the SoundWire SHIM registers
462 if (!sdev->first_boot)
463 hda_sdw_process_wakeen(sdev);
466 * Set the boot_iteration to the last attempt, indicating that the
467 * DSP ROM has been initialized and from this point there will be no
468 * retry done to boot.
470 * Continue with code loading and firmware boot
472 hda->boot_iteration = HDA_FW_BOOT_ATTEMPTS;
473 ret = hda_cl_copy_fw(sdev, hext_stream);
475 dev_dbg(sdev->dev, "Firmware download successful, booting...\n");
477 snd_sof_dsp_dbg_dump(sdev, "Firmware download failed",
478 SOF_DBG_DUMP_PCI | SOF_DBG_DUMP_MBOX);
482 * Perform codeloader stream cleanup.
483 * This should be done even if firmware loading fails.
484 * If the cleanup also fails, we return the initial error
486 ret1 = hda_cl_cleanup(sdev, &dmab, hext_stream);
488 dev_err(sdev->dev, "error: Code loader DSP cleanup failed\n");
490 /* set return value to indicate cleanup failure */
496 * return primary core id if both fw copy
497 * and stream clean up are successful
500 return chip_info->init_core_mask;
503 snd_sof_dsp_update_bits(sdev, HDA_DSP_PP_BAR,
504 SOF_HDA_REG_PP_PPCTL,
505 SOF_HDA_PPCTL_GPROCEN, 0);
509 /* pre fw run operations */
510 int hda_dsp_pre_fw_run(struct snd_sof_dev *sdev)
512 /* disable clock gating and power gating */
513 return hda_dsp_ctrl_clock_power_gating(sdev, false);
516 /* post fw run operations */
517 int hda_dsp_post_fw_run(struct snd_sof_dev *sdev)
521 if (sdev->first_boot) {
522 struct sof_intel_hda_dev *hdev = sdev->pdata->hw_pdata;
524 ret = hda_sdw_startup(sdev);
527 "error: could not startup SoundWire links\n");
531 /* Check if IMR boot is usable */
532 if (!sof_debug_check_flag(SOF_DBG_IGNORE_D3_PERSISTENT) &&
533 sdev->fw_ready.flags & SOF_IPC_INFO_D3_PERSISTENT)
534 hdev->imrboot_supported = true;
537 hda_sdw_int_enable(sdev, true);
539 /* re-enable clock gating and power gating */
540 return hda_dsp_ctrl_clock_power_gating(sdev, true);
543 int hda_dsp_ext_man_get_cavs_config_data(struct snd_sof_dev *sdev,
544 const struct sof_ext_man_elem_header *hdr)
546 const struct sof_ext_man_cavs_config_data *config_data =
547 container_of(hdr, struct sof_ext_man_cavs_config_data, hdr);
548 struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata;
551 /* calculate total number of config data elements */
552 elem_num = (hdr->size - sizeof(struct sof_ext_man_elem_header))
553 / sizeof(struct sof_config_elem);
555 dev_err(sdev->dev, "cavs config data is inconsistent: %d\n", elem_num);
559 for (i = 0; i < elem_num; i++)
560 switch (config_data->elems[i].token) {
561 case SOF_EXT_MAN_CAVS_CONFIG_EMPTY:
562 /* skip empty token */
564 case SOF_EXT_MAN_CAVS_CONFIG_CAVS_LPRO:
565 hda->clk_config_lpro = config_data->elems[i].value;
566 dev_dbg(sdev->dev, "FW clock config: %s\n",
567 hda->clk_config_lpro ? "LPRO" : "HPRO");
569 case SOF_EXT_MAN_CAVS_CONFIG_OUTBOX_SIZE:
570 case SOF_EXT_MAN_CAVS_CONFIG_INBOX_SIZE:
571 /* These elements are defined but not being used yet. No warn is required */
574 dev_info(sdev->dev, "unsupported token type: %d\n",
575 config_data->elems[i].token);