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 // Author: Liam Girdwood <liam.r.girdwood@linux.intel.com>
10 // Generic firmware loader.
13 #include <linux/firmware.h>
17 int snd_sof_load_firmware_raw(struct snd_sof_dev *sdev)
19 struct snd_sof_pdata *plat_data = sdev->pdata;
20 const char *fw_filename;
24 /* Don't request firmware again if firmware is already requested */
28 fw_filename = kasprintf(GFP_KERNEL, "%s/%s",
29 plat_data->fw_filename_prefix,
30 plat_data->fw_filename);
34 ret = request_firmware(&sdev->basefw.fw, fw_filename, sdev->dev);
38 "error: sof firmware file is missing, you might need to\n");
40 " download it from https://github.com/thesofproject/sof-bin/\n");
43 dev_dbg(sdev->dev, "request_firmware %s successful\n",
47 /* check for extended manifest */
48 ext_man_size = sdev->ipc->ops->fw_loader->parse_ext_manifest(sdev);
49 if (ext_man_size > 0) {
50 /* when no error occurred, drop extended manifest */
51 sdev->basefw.payload_offset = ext_man_size;
52 } else if (!ext_man_size) {
53 /* No extended manifest, so nothing to skip during FW load */
54 dev_dbg(sdev->dev, "firmware doesn't contain extended manifest\n");
57 dev_err(sdev->dev, "error: firmware %s contains unsupported or invalid extended manifest: %d\n",
66 EXPORT_SYMBOL(snd_sof_load_firmware_raw);
68 int snd_sof_load_firmware_memcpy(struct snd_sof_dev *sdev)
72 ret = snd_sof_load_firmware_raw(sdev);
76 /* make sure the FW header and file is valid */
77 ret = sdev->ipc->ops->fw_loader->validate(sdev);
79 dev_err(sdev->dev, "error: invalid FW header\n");
83 /* prepare the DSP for FW loading */
84 ret = snd_sof_dsp_reset(sdev);
86 dev_err(sdev->dev, "error: failed to reset DSP\n");
90 /* parse and load firmware modules to DSP */
91 if (sdev->ipc->ops->fw_loader->load_fw_to_dsp) {
92 ret = sdev->ipc->ops->fw_loader->load_fw_to_dsp(sdev);
94 dev_err(sdev->dev, "Firmware loading failed\n");
102 release_firmware(sdev->basefw.fw);
103 sdev->basefw.fw = NULL;
107 EXPORT_SYMBOL(snd_sof_load_firmware_memcpy);
109 int snd_sof_run_firmware(struct snd_sof_dev *sdev)
113 init_waitqueue_head(&sdev->boot_wait);
115 /* (re-)enable dsp dump */
116 sdev->dbg_dump_printed = false;
117 sdev->ipc_dump_printed = false;
119 /* create read-only fw_version debugfs to store boot version info */
120 if (sdev->first_boot) {
121 ret = snd_sof_debugfs_buf_item(sdev, &sdev->fw_version,
122 sizeof(sdev->fw_version),
124 /* errors are only due to memory allocation, not debugfs */
126 dev_err(sdev->dev, "error: snd_sof_debugfs_buf_item failed\n");
131 /* perform pre fw run operations */
132 ret = snd_sof_dsp_pre_fw_run(sdev);
134 dev_err(sdev->dev, "error: failed pre fw run op\n");
138 dev_dbg(sdev->dev, "booting DSP firmware\n");
140 /* boot the firmware on the DSP */
141 ret = snd_sof_dsp_run(sdev);
143 snd_sof_dsp_dbg_dump(sdev, "Failed to start DSP",
144 SOF_DBG_DUMP_MBOX | SOF_DBG_DUMP_PCI);
149 * now wait for the DSP to boot. There are 3 possible outcomes:
150 * 1. Boot wait times out indicating FW boot failure.
151 * 2. FW boots successfully and fw_ready op succeeds.
152 * 3. FW boots but fw_ready op fails.
154 ret = wait_event_timeout(sdev->boot_wait,
155 sdev->fw_state > SOF_FW_BOOT_IN_PROGRESS,
156 msecs_to_jiffies(sdev->boot_timeout));
158 snd_sof_dsp_dbg_dump(sdev, "Firmware boot failure due to timeout",
159 SOF_DBG_DUMP_REGS | SOF_DBG_DUMP_MBOX |
160 SOF_DBG_DUMP_TEXT | SOF_DBG_DUMP_PCI);
164 if (sdev->fw_state == SOF_FW_BOOT_READY_FAILED)
165 return -EIO; /* FW boots but fw_ready op failed */
167 dev_dbg(sdev->dev, "firmware boot complete\n");
168 sof_set_fw_state(sdev, SOF_FW_BOOT_COMPLETE);
170 /* perform post fw run operations */
171 ret = snd_sof_dsp_post_fw_run(sdev);
173 dev_err(sdev->dev, "error: failed post fw run op\n");
177 if (sdev->ipc->ops->post_fw_boot)
178 return sdev->ipc->ops->post_fw_boot(sdev);
182 EXPORT_SYMBOL(snd_sof_run_firmware);
184 void snd_sof_fw_unload(struct snd_sof_dev *sdev)
186 /* TODO: support module unloading at runtime */
187 release_firmware(sdev->basefw.fw);
188 sdev->basefw.fw = NULL;
190 EXPORT_SYMBOL(snd_sof_fw_unload);