1 // SPDX-License-Identifier: (GPL-2.0 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>
14 #include <sound/sof.h>
17 static int get_ext_windows(struct snd_sof_dev *sdev,
18 struct sof_ipc_ext_data_hdr *ext_hdr)
20 struct sof_ipc_window *w =
21 container_of(ext_hdr, struct sof_ipc_window, ext_hdr);
23 if (w->num_windows == 0 || w->num_windows > SOF_IPC_MAX_ELEMS)
26 /* keep a local copy of the data */
27 sdev->info_window = kmemdup(w, struct_size(w, window, w->num_windows),
29 if (!sdev->info_window)
35 /* parse the extended FW boot data structures from FW boot message */
36 int snd_sof_fw_parse_ext_data(struct snd_sof_dev *sdev, u32 bar, u32 offset)
38 struct sof_ipc_ext_data_hdr *ext_hdr;
42 ext_data = kzalloc(PAGE_SIZE, GFP_KERNEL);
46 /* get first header */
47 snd_sof_dsp_block_read(sdev, bar, offset, ext_data,
51 while (ext_hdr->hdr.cmd == SOF_IPC_FW_READY) {
52 /* read in ext structure */
53 offset += sizeof(*ext_hdr);
54 snd_sof_dsp_block_read(sdev, bar, offset,
55 (void *)((u8 *)ext_data + sizeof(*ext_hdr)),
56 ext_hdr->hdr.size - sizeof(*ext_hdr));
58 dev_dbg(sdev->dev, "found ext header type %d size 0x%x\n",
59 ext_hdr->type, ext_hdr->hdr.size);
61 /* process structure data */
62 switch (ext_hdr->type) {
63 case SOF_IPC_EXT_DMA_BUFFER:
65 case SOF_IPC_EXT_WINDOW:
66 ret = get_ext_windows(sdev, ext_hdr);
73 dev_err(sdev->dev, "error: failed to parse ext data type %d\n",
78 /* move to next header */
79 offset += ext_hdr->hdr.size;
80 snd_sof_dsp_block_read(sdev, bar, offset, ext_data,
88 EXPORT_SYMBOL(snd_sof_fw_parse_ext_data);
90 /* generic module parser for mmaped DSPs */
91 int snd_sof_parse_module_memcpy(struct snd_sof_dev *sdev,
92 struct snd_sof_mod_hdr *module)
94 struct snd_sof_blk_hdr *block;
99 dev_dbg(sdev->dev, "new module size 0x%x blocks 0x%x type 0x%x\n",
100 module->size, module->num_blocks, module->type);
102 block = (struct snd_sof_blk_hdr *)((u8 *)module + sizeof(*module));
104 /* module->size doesn't include header size */
105 remaining = module->size;
106 for (count = 0; count < module->num_blocks; count++) {
108 if (remaining < sizeof(*block)) {
109 dev_err(sdev->dev, "error: not enough data remaining\n");
113 /* minus header size of block */
114 remaining -= sizeof(*block);
116 if (block->size == 0) {
118 "warning: block %d size zero\n", count);
119 dev_warn(sdev->dev, " type 0x%x offset 0x%x\n",
120 block->type, block->offset);
124 switch (block->type) {
125 case SOF_FW_BLK_TYPE_RSRVD0:
126 case SOF_FW_BLK_TYPE_SRAM...SOF_FW_BLK_TYPE_RSRVD14:
127 continue; /* not handled atm */
128 case SOF_FW_BLK_TYPE_IRAM:
129 case SOF_FW_BLK_TYPE_DRAM:
130 offset = block->offset;
133 dev_err(sdev->dev, "error: bad type 0x%x for block 0x%x\n",
139 "block %d type 0x%x size 0x%x ==> offset 0x%x\n",
140 count, block->type, block->size, offset);
142 /* checking block->size to avoid unaligned access */
143 if (block->size % sizeof(u32)) {
144 dev_err(sdev->dev, "error: invalid block size 0x%x\n",
148 snd_sof_dsp_block_write(sdev, sdev->mmio_bar, offset,
149 block + 1, block->size);
151 if (remaining < block->size) {
152 dev_err(sdev->dev, "error: not enough data remaining\n");
156 /* minus body size of block */
157 remaining -= block->size;
159 block = (struct snd_sof_blk_hdr *)((u8 *)block + sizeof(*block)
165 EXPORT_SYMBOL(snd_sof_parse_module_memcpy);
167 static int check_header(struct snd_sof_dev *sdev, const struct firmware *fw)
169 struct snd_sof_fw_header *header;
171 /* Read the header information from the data pointer */
172 header = (struct snd_sof_fw_header *)fw->data;
175 if (strncmp(header->sig, SND_SOF_FW_SIG, SND_SOF_FW_SIG_SIZE) != 0) {
176 dev_err(sdev->dev, "error: invalid firmware signature\n");
180 /* check size is valid */
181 if (fw->size != header->file_size + sizeof(*header)) {
182 dev_err(sdev->dev, "error: invalid filesize mismatch got 0x%zx expected 0x%zx\n",
183 fw->size, header->file_size + sizeof(*header));
187 dev_dbg(sdev->dev, "header size=0x%x modules=0x%x abi=0x%x size=%zu\n",
188 header->file_size, header->num_modules,
189 header->abi, sizeof(*header));
194 static int load_modules(struct snd_sof_dev *sdev, const struct firmware *fw)
196 struct snd_sof_fw_header *header;
197 struct snd_sof_mod_hdr *module;
198 int (*load_module)(struct snd_sof_dev *sof_dev,
199 struct snd_sof_mod_hdr *hdr);
203 header = (struct snd_sof_fw_header *)fw->data;
204 load_module = sof_ops(sdev)->load_module;
208 /* parse each module */
209 module = (struct snd_sof_mod_hdr *)((u8 *)(fw->data) + sizeof(*header));
210 remaining = fw->size - sizeof(*header);
212 if (remaining > fw->size) {
213 dev_err(sdev->dev, "error: fw size smaller than header size\n");
217 for (count = 0; count < header->num_modules; count++) {
219 if (remaining < sizeof(*module)) {
220 dev_err(sdev->dev, "error: not enough data remaining\n");
224 /* minus header size of module */
225 remaining -= sizeof(*module);
228 ret = load_module(sdev, module);
230 dev_err(sdev->dev, "error: invalid module %d\n", count);
234 if (remaining < module->size) {
235 dev_err(sdev->dev, "error: not enough data remaining\n");
239 /* minus body size of module */
240 remaining -= module->size;
241 module = (struct snd_sof_mod_hdr *)((u8 *)module
242 + sizeof(*module) + module->size);
248 int snd_sof_load_firmware_raw(struct snd_sof_dev *sdev)
250 struct snd_sof_pdata *plat_data = sdev->pdata;
251 const char *fw_filename;
254 /* set code loading condition to true */
255 sdev->code_loading = 1;
257 /* Don't request firmware again if firmware is already requested */
261 fw_filename = kasprintf(GFP_KERNEL, "%s/%s",
262 plat_data->fw_filename_prefix,
263 plat_data->fw_filename);
267 ret = request_firmware(&plat_data->fw, fw_filename, sdev->dev);
270 dev_err(sdev->dev, "error: request firmware %s failed err: %d\n",
278 EXPORT_SYMBOL(snd_sof_load_firmware_raw);
280 int snd_sof_load_firmware_memcpy(struct snd_sof_dev *sdev)
282 struct snd_sof_pdata *plat_data = sdev->pdata;
285 ret = snd_sof_load_firmware_raw(sdev);
289 /* make sure the FW header and file is valid */
290 ret = check_header(sdev, plat_data->fw);
292 dev_err(sdev->dev, "error: invalid FW header\n");
296 /* prepare the DSP for FW loading */
297 ret = snd_sof_dsp_reset(sdev);
299 dev_err(sdev->dev, "error: failed to reset DSP\n");
303 /* parse and load firmware modules to DSP */
304 ret = load_modules(sdev, plat_data->fw);
306 dev_err(sdev->dev, "error: invalid FW modules\n");
313 release_firmware(plat_data->fw);
314 plat_data->fw = NULL;
318 EXPORT_SYMBOL(snd_sof_load_firmware_memcpy);
320 int snd_sof_load_firmware(struct snd_sof_dev *sdev)
322 dev_dbg(sdev->dev, "loading firmware\n");
324 if (sof_ops(sdev)->load_firmware)
325 return sof_ops(sdev)->load_firmware(sdev);
328 EXPORT_SYMBOL(snd_sof_load_firmware);
330 int snd_sof_run_firmware(struct snd_sof_dev *sdev)
335 init_waitqueue_head(&sdev->boot_wait);
336 sdev->boot_complete = false;
338 /* create read-only fw_version debugfs to store boot version info */
339 if (sdev->first_boot) {
340 ret = snd_sof_debugfs_buf_item(sdev, &sdev->fw_version,
341 sizeof(sdev->fw_version),
343 /* errors are only due to memory allocation, not debugfs */
345 dev_err(sdev->dev, "error: snd_sof_debugfs_buf_item failed\n");
350 /* perform pre fw run operations */
351 ret = snd_sof_dsp_pre_fw_run(sdev);
353 dev_err(sdev->dev, "error: failed pre fw run op\n");
357 dev_dbg(sdev->dev, "booting DSP firmware\n");
359 /* boot the firmware on the DSP */
360 ret = snd_sof_dsp_run(sdev);
362 dev_err(sdev->dev, "error: failed to reset DSP\n");
366 init_core_mask = ret;
368 /* now wait for the DSP to boot */
369 ret = wait_event_timeout(sdev->boot_wait, sdev->boot_complete,
370 msecs_to_jiffies(sdev->boot_timeout));
372 dev_err(sdev->dev, "error: firmware boot failure\n");
373 /* after this point FW_READY msg should be ignored */
374 sdev->boot_complete = true;
375 snd_sof_dsp_dbg_dump(sdev, SOF_DBG_REGS | SOF_DBG_MBOX |
376 SOF_DBG_TEXT | SOF_DBG_PCI);
380 dev_info(sdev->dev, "firmware boot complete\n");
382 /* perform post fw run operations */
383 ret = snd_sof_dsp_post_fw_run(sdev);
385 dev_err(sdev->dev, "error: failed post fw run op\n");
389 /* fw boot is complete. Update the active cores mask */
390 sdev->enabled_cores_mask = init_core_mask;
394 EXPORT_SYMBOL(snd_sof_run_firmware);
396 void snd_sof_fw_unload(struct snd_sof_dev *sdev)
398 /* TODO: support module unloading at runtime */
400 EXPORT_SYMBOL(snd_sof_fw_unload);