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);
69 dev_warn(sdev->dev, "warning: unknown ext header type %d size 0x%x\n",
70 ext_hdr->type, ext_hdr->hdr.size);
75 dev_err(sdev->dev, "error: failed to parse ext data type %d\n",
80 /* move to next header */
81 offset += ext_hdr->hdr.size;
82 snd_sof_dsp_block_read(sdev, bar, offset, ext_data,
90 EXPORT_SYMBOL(snd_sof_fw_parse_ext_data);
95 static void sof_get_windows(struct snd_sof_dev *sdev)
97 struct sof_ipc_window_elem *elem;
98 u32 outbox_offset = 0;
99 u32 stream_offset = 0;
100 u32 inbox_offset = 0;
108 if (!sdev->info_window) {
109 dev_err(sdev->dev, "error: have no window info\n");
113 bar = snd_sof_dsp_get_bar_index(sdev, SOF_FW_BLK_TYPE_SRAM);
115 dev_err(sdev->dev, "error: have no bar mapping\n");
119 for (i = 0; i < sdev->info_window->num_windows; i++) {
120 elem = &sdev->info_window->window[i];
122 window_offset = snd_sof_dsp_get_window_offset(sdev, elem->id);
123 if (window_offset < 0) {
124 dev_warn(sdev->dev, "warn: no offset for window %d\n",
129 switch (elem->type) {
130 case SOF_IPC_REGION_UPBOX:
131 inbox_offset = window_offset + elem->offset;
132 inbox_size = elem->size;
133 snd_sof_debugfs_io_item(sdev,
137 SOF_DEBUGFS_ACCESS_D0_ONLY);
139 case SOF_IPC_REGION_DOWNBOX:
140 outbox_offset = window_offset + elem->offset;
141 outbox_size = elem->size;
142 snd_sof_debugfs_io_item(sdev,
145 elem->size, "outbox",
146 SOF_DEBUGFS_ACCESS_D0_ONLY);
148 case SOF_IPC_REGION_TRACE:
149 snd_sof_debugfs_io_item(sdev,
153 elem->size, "etrace",
154 SOF_DEBUGFS_ACCESS_D0_ONLY);
156 case SOF_IPC_REGION_DEBUG:
157 snd_sof_debugfs_io_item(sdev,
162 SOF_DEBUGFS_ACCESS_D0_ONLY);
164 case SOF_IPC_REGION_STREAM:
165 stream_offset = window_offset + elem->offset;
166 stream_size = elem->size;
167 snd_sof_debugfs_io_item(sdev,
170 elem->size, "stream",
171 SOF_DEBUGFS_ACCESS_D0_ONLY);
173 case SOF_IPC_REGION_REGS:
174 snd_sof_debugfs_io_item(sdev,
179 SOF_DEBUGFS_ACCESS_D0_ONLY);
181 case SOF_IPC_REGION_EXCEPTION:
182 sdev->dsp_oops_offset = window_offset + elem->offset;
183 snd_sof_debugfs_io_item(sdev,
187 elem->size, "exception",
188 SOF_DEBUGFS_ACCESS_D0_ONLY);
191 dev_err(sdev->dev, "error: get illegal window info\n");
196 if (outbox_size == 0 || inbox_size == 0) {
197 dev_err(sdev->dev, "error: get illegal mailbox window\n");
201 snd_sof_dsp_mailbox_init(sdev, inbox_offset, inbox_size,
202 outbox_offset, outbox_size);
203 sdev->stream_box.offset = stream_offset;
204 sdev->stream_box.size = stream_size;
206 dev_dbg(sdev->dev, " mailbox upstream 0x%x - size 0x%x\n",
207 inbox_offset, inbox_size);
208 dev_dbg(sdev->dev, " mailbox downstream 0x%x - size 0x%x\n",
209 outbox_offset, outbox_size);
210 dev_dbg(sdev->dev, " stream region 0x%x - size 0x%x\n",
211 stream_offset, stream_size);
214 /* check for ABI compatibility and create memory windows on first boot */
215 int sof_fw_ready(struct snd_sof_dev *sdev, u32 msg_id)
217 struct sof_ipc_fw_ready *fw_ready = &sdev->fw_ready;
222 /* mailbox must be on 4k boundary */
223 offset = snd_sof_dsp_get_mailbox_offset(sdev);
225 dev_err(sdev->dev, "error: have no mailbox offset\n");
229 bar = snd_sof_dsp_get_bar_index(sdev, SOF_FW_BLK_TYPE_SRAM);
231 dev_err(sdev->dev, "error: have no bar mapping\n");
235 dev_dbg(sdev->dev, "ipc: DSP is ready 0x%8.8x offset 0x%x\n",
238 /* no need to re-check version/ABI for subsequent boots */
239 if (!sdev->first_boot)
242 /* copy data from the DSP FW ready offset */
243 sof_block_read(sdev, bar, offset, fw_ready, sizeof(*fw_ready));
245 /* make sure ABI version is compatible */
246 ret = snd_sof_ipc_valid(sdev);
250 /* now check for extended data */
251 snd_sof_fw_parse_ext_data(sdev, bar, offset +
252 sizeof(struct sof_ipc_fw_ready));
254 sof_get_windows(sdev);
258 EXPORT_SYMBOL(sof_fw_ready);
260 /* generic module parser for mmaped DSPs */
261 int snd_sof_parse_module_memcpy(struct snd_sof_dev *sdev,
262 struct snd_sof_mod_hdr *module)
264 struct snd_sof_blk_hdr *block;
269 dev_dbg(sdev->dev, "new module size 0x%x blocks 0x%x type 0x%x\n",
270 module->size, module->num_blocks, module->type);
272 block = (struct snd_sof_blk_hdr *)((u8 *)module + sizeof(*module));
274 /* module->size doesn't include header size */
275 remaining = module->size;
276 for (count = 0; count < module->num_blocks; count++) {
278 if (remaining < sizeof(*block)) {
279 dev_err(sdev->dev, "error: not enough data remaining\n");
283 /* minus header size of block */
284 remaining -= sizeof(*block);
286 if (block->size == 0) {
288 "warning: block %d size zero\n", count);
289 dev_warn(sdev->dev, " type 0x%x offset 0x%x\n",
290 block->type, block->offset);
294 switch (block->type) {
295 case SOF_FW_BLK_TYPE_RSRVD0:
296 case SOF_FW_BLK_TYPE_ROM...SOF_FW_BLK_TYPE_RSRVD14:
297 continue; /* not handled atm */
298 case SOF_FW_BLK_TYPE_IRAM:
299 case SOF_FW_BLK_TYPE_DRAM:
300 case SOF_FW_BLK_TYPE_SRAM:
301 offset = block->offset;
302 bar = snd_sof_dsp_get_bar_index(sdev, block->type);
305 "error: no BAR mapping for block type 0x%x\n",
311 dev_err(sdev->dev, "error: bad type 0x%x for block 0x%x\n",
317 "block %d type 0x%x size 0x%x ==> offset 0x%x\n",
318 count, block->type, block->size, offset);
320 /* checking block->size to avoid unaligned access */
321 if (block->size % sizeof(u32)) {
322 dev_err(sdev->dev, "error: invalid block size 0x%x\n",
326 snd_sof_dsp_block_write(sdev, bar, offset,
327 block + 1, block->size);
329 if (remaining < block->size) {
330 dev_err(sdev->dev, "error: not enough data remaining\n");
334 /* minus body size of block */
335 remaining -= block->size;
337 block = (struct snd_sof_blk_hdr *)((u8 *)block + sizeof(*block)
343 EXPORT_SYMBOL(snd_sof_parse_module_memcpy);
345 static int check_header(struct snd_sof_dev *sdev, const struct firmware *fw)
347 struct snd_sof_fw_header *header;
349 /* Read the header information from the data pointer */
350 header = (struct snd_sof_fw_header *)fw->data;
353 if (strncmp(header->sig, SND_SOF_FW_SIG, SND_SOF_FW_SIG_SIZE) != 0) {
354 dev_err(sdev->dev, "error: invalid firmware signature\n");
358 /* check size is valid */
359 if (fw->size != header->file_size + sizeof(*header)) {
360 dev_err(sdev->dev, "error: invalid filesize mismatch got 0x%zx expected 0x%zx\n",
361 fw->size, header->file_size + sizeof(*header));
365 dev_dbg(sdev->dev, "header size=0x%x modules=0x%x abi=0x%x size=%zu\n",
366 header->file_size, header->num_modules,
367 header->abi, sizeof(*header));
372 static int load_modules(struct snd_sof_dev *sdev, const struct firmware *fw)
374 struct snd_sof_fw_header *header;
375 struct snd_sof_mod_hdr *module;
376 int (*load_module)(struct snd_sof_dev *sof_dev,
377 struct snd_sof_mod_hdr *hdr);
381 header = (struct snd_sof_fw_header *)fw->data;
382 load_module = sof_ops(sdev)->load_module;
386 /* parse each module */
387 module = (struct snd_sof_mod_hdr *)((u8 *)(fw->data) + sizeof(*header));
388 remaining = fw->size - sizeof(*header);
390 if (remaining > fw->size) {
391 dev_err(sdev->dev, "error: fw size smaller than header size\n");
395 for (count = 0; count < header->num_modules; count++) {
397 if (remaining < sizeof(*module)) {
398 dev_err(sdev->dev, "error: not enough data remaining\n");
402 /* minus header size of module */
403 remaining -= sizeof(*module);
406 ret = load_module(sdev, module);
408 dev_err(sdev->dev, "error: invalid module %d\n", count);
412 if (remaining < module->size) {
413 dev_err(sdev->dev, "error: not enough data remaining\n");
417 /* minus body size of module */
418 remaining -= module->size;
419 module = (struct snd_sof_mod_hdr *)((u8 *)module
420 + sizeof(*module) + module->size);
426 int snd_sof_load_firmware_raw(struct snd_sof_dev *sdev)
428 struct snd_sof_pdata *plat_data = sdev->pdata;
429 const char *fw_filename;
432 /* set code loading condition to true */
433 sdev->code_loading = 1;
435 /* Don't request firmware again if firmware is already requested */
439 fw_filename = kasprintf(GFP_KERNEL, "%s/%s",
440 plat_data->fw_filename_prefix,
441 plat_data->fw_filename);
445 ret = request_firmware(&plat_data->fw, fw_filename, sdev->dev);
448 dev_err(sdev->dev, "error: request firmware %s failed err: %d\n",
456 EXPORT_SYMBOL(snd_sof_load_firmware_raw);
458 int snd_sof_load_firmware_memcpy(struct snd_sof_dev *sdev)
460 struct snd_sof_pdata *plat_data = sdev->pdata;
463 ret = snd_sof_load_firmware_raw(sdev);
467 /* make sure the FW header and file is valid */
468 ret = check_header(sdev, plat_data->fw);
470 dev_err(sdev->dev, "error: invalid FW header\n");
474 /* prepare the DSP for FW loading */
475 ret = snd_sof_dsp_reset(sdev);
477 dev_err(sdev->dev, "error: failed to reset DSP\n");
481 /* parse and load firmware modules to DSP */
482 ret = load_modules(sdev, plat_data->fw);
484 dev_err(sdev->dev, "error: invalid FW modules\n");
491 release_firmware(plat_data->fw);
492 plat_data->fw = NULL;
496 EXPORT_SYMBOL(snd_sof_load_firmware_memcpy);
498 int snd_sof_load_firmware(struct snd_sof_dev *sdev)
500 dev_dbg(sdev->dev, "loading firmware\n");
502 if (sof_ops(sdev)->load_firmware)
503 return sof_ops(sdev)->load_firmware(sdev);
506 EXPORT_SYMBOL(snd_sof_load_firmware);
508 int snd_sof_run_firmware(struct snd_sof_dev *sdev)
513 init_waitqueue_head(&sdev->boot_wait);
515 /* create read-only fw_version debugfs to store boot version info */
516 if (sdev->first_boot) {
517 ret = snd_sof_debugfs_buf_item(sdev, &sdev->fw_version,
518 sizeof(sdev->fw_version),
520 /* errors are only due to memory allocation, not debugfs */
522 dev_err(sdev->dev, "error: snd_sof_debugfs_buf_item failed\n");
527 /* perform pre fw run operations */
528 ret = snd_sof_dsp_pre_fw_run(sdev);
530 dev_err(sdev->dev, "error: failed pre fw run op\n");
534 dev_dbg(sdev->dev, "booting DSP firmware\n");
536 /* boot the firmware on the DSP */
537 ret = snd_sof_dsp_run(sdev);
539 dev_err(sdev->dev, "error: failed to reset DSP\n");
543 init_core_mask = ret;
546 * now wait for the DSP to boot. There are 3 possible outcomes:
547 * 1. Boot wait times out indicating FW boot failure.
548 * 2. FW boots successfully and fw_ready op succeeds.
549 * 3. FW boots but fw_ready op fails.
551 ret = wait_event_timeout(sdev->boot_wait,
552 sdev->fw_state > SOF_FW_BOOT_IN_PROGRESS,
553 msecs_to_jiffies(sdev->boot_timeout));
555 dev_err(sdev->dev, "error: firmware boot failure\n");
556 snd_sof_dsp_dbg_dump(sdev, SOF_DBG_REGS | SOF_DBG_MBOX |
557 SOF_DBG_TEXT | SOF_DBG_PCI);
558 sdev->fw_state = SOF_FW_BOOT_FAILED;
562 if (sdev->fw_state == SOF_FW_BOOT_COMPLETE)
563 dev_info(sdev->dev, "firmware boot complete\n");
565 return -EIO; /* FW boots but fw_ready op failed */
567 /* perform post fw run operations */
568 ret = snd_sof_dsp_post_fw_run(sdev);
570 dev_err(sdev->dev, "error: failed post fw run op\n");
574 /* fw boot is complete. Update the active cores mask */
575 sdev->enabled_cores_mask = init_core_mask;
579 EXPORT_SYMBOL(snd_sof_run_firmware);
581 void snd_sof_fw_unload(struct snd_sof_dev *sdev)
583 /* TODO: support module unloading at runtime */
585 EXPORT_SYMBOL(snd_sof_fw_unload);