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 static int get_cc_info(struct snd_sof_dev *sdev,
36 struct sof_ipc_ext_data_hdr *ext_hdr)
40 struct sof_ipc_cc_version *cc =
41 container_of(ext_hdr, struct sof_ipc_cc_version, ext_hdr);
43 dev_dbg(sdev->dev, "Firmware info: used compiler %s %d:%d:%d%s used optimization flags %s\n",
44 cc->name, cc->major, cc->minor, cc->micro, cc->desc,
47 /* create read-only cc_version debugfs to store compiler version info */
48 /* use local copy of the cc_version to prevent data corruption */
49 if (sdev->first_boot) {
50 sdev->cc_version = devm_kmalloc(sdev->dev, cc->ext_hdr.hdr.size,
53 if (!sdev->cc_version)
56 memcpy(sdev->cc_version, cc, cc->ext_hdr.hdr.size);
57 ret = snd_sof_debugfs_buf_item(sdev, sdev->cc_version,
61 /* errors are only due to memory allocation, not debugfs */
63 dev_err(sdev->dev, "error: snd_sof_debugfs_buf_item failed\n");
71 /* parse the extended FW boot data structures from FW boot message */
72 int snd_sof_fw_parse_ext_data(struct snd_sof_dev *sdev, u32 bar, u32 offset)
74 struct sof_ipc_ext_data_hdr *ext_hdr;
78 ext_data = kzalloc(PAGE_SIZE, GFP_KERNEL);
82 /* get first header */
83 snd_sof_dsp_block_read(sdev, bar, offset, ext_data,
87 while (ext_hdr->hdr.cmd == SOF_IPC_FW_READY) {
88 /* read in ext structure */
89 snd_sof_dsp_block_read(sdev, bar, offset + sizeof(*ext_hdr),
90 (void *)((u8 *)ext_data + sizeof(*ext_hdr)),
91 ext_hdr->hdr.size - sizeof(*ext_hdr));
93 dev_dbg(sdev->dev, "found ext header type %d size 0x%x\n",
94 ext_hdr->type, ext_hdr->hdr.size);
96 /* process structure data */
97 switch (ext_hdr->type) {
98 case SOF_IPC_EXT_WINDOW:
99 ret = get_ext_windows(sdev, ext_hdr);
101 case SOF_IPC_EXT_CC_INFO:
102 ret = get_cc_info(sdev, ext_hdr);
105 dev_warn(sdev->dev, "warning: unknown ext header type %d size 0x%x\n",
106 ext_hdr->type, ext_hdr->hdr.size);
112 dev_err(sdev->dev, "error: failed to parse ext data type %d\n",
117 /* move to next header */
118 offset += ext_hdr->hdr.size;
119 snd_sof_dsp_block_read(sdev, bar, offset, ext_data,
127 EXPORT_SYMBOL(snd_sof_fw_parse_ext_data);
130 * IPC Firmware ready.
132 static void sof_get_windows(struct snd_sof_dev *sdev)
134 struct sof_ipc_window_elem *elem;
135 u32 outbox_offset = 0;
136 u32 stream_offset = 0;
137 u32 inbox_offset = 0;
145 if (!sdev->info_window) {
146 dev_err(sdev->dev, "error: have no window info\n");
150 bar = snd_sof_dsp_get_bar_index(sdev, SOF_FW_BLK_TYPE_SRAM);
152 dev_err(sdev->dev, "error: have no bar mapping\n");
156 for (i = 0; i < sdev->info_window->num_windows; i++) {
157 elem = &sdev->info_window->window[i];
159 window_offset = snd_sof_dsp_get_window_offset(sdev, elem->id);
160 if (window_offset < 0) {
161 dev_warn(sdev->dev, "warn: no offset for window %d\n",
166 switch (elem->type) {
167 case SOF_IPC_REGION_UPBOX:
168 inbox_offset = window_offset + elem->offset;
169 inbox_size = elem->size;
170 snd_sof_debugfs_io_item(sdev,
174 SOF_DEBUGFS_ACCESS_D0_ONLY);
176 case SOF_IPC_REGION_DOWNBOX:
177 outbox_offset = window_offset + elem->offset;
178 outbox_size = elem->size;
179 snd_sof_debugfs_io_item(sdev,
182 elem->size, "outbox",
183 SOF_DEBUGFS_ACCESS_D0_ONLY);
185 case SOF_IPC_REGION_TRACE:
186 snd_sof_debugfs_io_item(sdev,
190 elem->size, "etrace",
191 SOF_DEBUGFS_ACCESS_D0_ONLY);
193 case SOF_IPC_REGION_DEBUG:
194 snd_sof_debugfs_io_item(sdev,
199 SOF_DEBUGFS_ACCESS_D0_ONLY);
201 case SOF_IPC_REGION_STREAM:
202 stream_offset = window_offset + elem->offset;
203 stream_size = elem->size;
204 snd_sof_debugfs_io_item(sdev,
207 elem->size, "stream",
208 SOF_DEBUGFS_ACCESS_D0_ONLY);
210 case SOF_IPC_REGION_REGS:
211 snd_sof_debugfs_io_item(sdev,
216 SOF_DEBUGFS_ACCESS_D0_ONLY);
218 case SOF_IPC_REGION_EXCEPTION:
219 sdev->dsp_oops_offset = window_offset + elem->offset;
220 snd_sof_debugfs_io_item(sdev,
224 elem->size, "exception",
225 SOF_DEBUGFS_ACCESS_D0_ONLY);
228 dev_err(sdev->dev, "error: get illegal window info\n");
233 if (outbox_size == 0 || inbox_size == 0) {
234 dev_err(sdev->dev, "error: get illegal mailbox window\n");
238 snd_sof_dsp_mailbox_init(sdev, inbox_offset, inbox_size,
239 outbox_offset, outbox_size);
240 sdev->stream_box.offset = stream_offset;
241 sdev->stream_box.size = stream_size;
243 dev_dbg(sdev->dev, " mailbox upstream 0x%x - size 0x%x\n",
244 inbox_offset, inbox_size);
245 dev_dbg(sdev->dev, " mailbox downstream 0x%x - size 0x%x\n",
246 outbox_offset, outbox_size);
247 dev_dbg(sdev->dev, " stream region 0x%x - size 0x%x\n",
248 stream_offset, stream_size);
251 /* check for ABI compatibility and create memory windows on first boot */
252 int sof_fw_ready(struct snd_sof_dev *sdev, u32 msg_id)
254 struct sof_ipc_fw_ready *fw_ready = &sdev->fw_ready;
259 /* mailbox must be on 4k boundary */
260 offset = snd_sof_dsp_get_mailbox_offset(sdev);
262 dev_err(sdev->dev, "error: have no mailbox offset\n");
266 bar = snd_sof_dsp_get_bar_index(sdev, SOF_FW_BLK_TYPE_SRAM);
268 dev_err(sdev->dev, "error: have no bar mapping\n");
272 dev_dbg(sdev->dev, "ipc: DSP is ready 0x%8.8x offset 0x%x\n",
275 /* no need to re-check version/ABI for subsequent boots */
276 if (!sdev->first_boot)
279 /* copy data from the DSP FW ready offset */
280 sof_block_read(sdev, bar, offset, fw_ready, sizeof(*fw_ready));
282 /* make sure ABI version is compatible */
283 ret = snd_sof_ipc_valid(sdev);
287 /* now check for extended data */
288 snd_sof_fw_parse_ext_data(sdev, bar, offset +
289 sizeof(struct sof_ipc_fw_ready));
291 sof_get_windows(sdev);
295 EXPORT_SYMBOL(sof_fw_ready);
297 /* generic module parser for mmaped DSPs */
298 int snd_sof_parse_module_memcpy(struct snd_sof_dev *sdev,
299 struct snd_sof_mod_hdr *module)
301 struct snd_sof_blk_hdr *block;
306 dev_dbg(sdev->dev, "new module size 0x%x blocks 0x%x type 0x%x\n",
307 module->size, module->num_blocks, module->type);
309 block = (struct snd_sof_blk_hdr *)((u8 *)module + sizeof(*module));
311 /* module->size doesn't include header size */
312 remaining = module->size;
313 for (count = 0; count < module->num_blocks; count++) {
315 if (remaining < sizeof(*block)) {
316 dev_err(sdev->dev, "error: not enough data remaining\n");
320 /* minus header size of block */
321 remaining -= sizeof(*block);
323 if (block->size == 0) {
325 "warning: block %d size zero\n", count);
326 dev_warn(sdev->dev, " type 0x%x offset 0x%x\n",
327 block->type, block->offset);
331 switch (block->type) {
332 case SOF_FW_BLK_TYPE_RSRVD0:
333 case SOF_FW_BLK_TYPE_ROM...SOF_FW_BLK_TYPE_RSRVD14:
334 continue; /* not handled atm */
335 case SOF_FW_BLK_TYPE_IRAM:
336 case SOF_FW_BLK_TYPE_DRAM:
337 case SOF_FW_BLK_TYPE_SRAM:
338 offset = block->offset;
339 bar = snd_sof_dsp_get_bar_index(sdev, block->type);
342 "error: no BAR mapping for block type 0x%x\n",
348 dev_err(sdev->dev, "error: bad type 0x%x for block 0x%x\n",
354 "block %d type 0x%x size 0x%x ==> offset 0x%x\n",
355 count, block->type, block->size, offset);
357 /* checking block->size to avoid unaligned access */
358 if (block->size % sizeof(u32)) {
359 dev_err(sdev->dev, "error: invalid block size 0x%x\n",
363 snd_sof_dsp_block_write(sdev, bar, offset,
364 block + 1, block->size);
366 if (remaining < block->size) {
367 dev_err(sdev->dev, "error: not enough data remaining\n");
371 /* minus body size of block */
372 remaining -= block->size;
374 block = (struct snd_sof_blk_hdr *)((u8 *)block + sizeof(*block)
380 EXPORT_SYMBOL(snd_sof_parse_module_memcpy);
382 static int check_header(struct snd_sof_dev *sdev, const struct firmware *fw)
384 struct snd_sof_fw_header *header;
386 /* Read the header information from the data pointer */
387 header = (struct snd_sof_fw_header *)fw->data;
390 if (strncmp(header->sig, SND_SOF_FW_SIG, SND_SOF_FW_SIG_SIZE) != 0) {
391 dev_err(sdev->dev, "error: invalid firmware signature\n");
395 /* check size is valid */
396 if (fw->size != header->file_size + sizeof(*header)) {
397 dev_err(sdev->dev, "error: invalid filesize mismatch got 0x%zx expected 0x%zx\n",
398 fw->size, header->file_size + sizeof(*header));
402 dev_dbg(sdev->dev, "header size=0x%x modules=0x%x abi=0x%x size=%zu\n",
403 header->file_size, header->num_modules,
404 header->abi, sizeof(*header));
409 static int load_modules(struct snd_sof_dev *sdev, const struct firmware *fw)
411 struct snd_sof_fw_header *header;
412 struct snd_sof_mod_hdr *module;
413 int (*load_module)(struct snd_sof_dev *sof_dev,
414 struct snd_sof_mod_hdr *hdr);
418 header = (struct snd_sof_fw_header *)fw->data;
419 load_module = sof_ops(sdev)->load_module;
423 /* parse each module */
424 module = (struct snd_sof_mod_hdr *)((u8 *)(fw->data) + sizeof(*header));
425 remaining = fw->size - sizeof(*header);
427 if (remaining > fw->size) {
428 dev_err(sdev->dev, "error: fw size smaller than header size\n");
432 for (count = 0; count < header->num_modules; count++) {
434 if (remaining < sizeof(*module)) {
435 dev_err(sdev->dev, "error: not enough data remaining\n");
439 /* minus header size of module */
440 remaining -= sizeof(*module);
443 ret = load_module(sdev, module);
445 dev_err(sdev->dev, "error: invalid module %d\n", count);
449 if (remaining < module->size) {
450 dev_err(sdev->dev, "error: not enough data remaining\n");
454 /* minus body size of module */
455 remaining -= module->size;
456 module = (struct snd_sof_mod_hdr *)((u8 *)module
457 + sizeof(*module) + module->size);
463 int snd_sof_load_firmware_raw(struct snd_sof_dev *sdev)
465 struct snd_sof_pdata *plat_data = sdev->pdata;
466 const char *fw_filename;
469 /* Don't request firmware again if firmware is already requested */
473 fw_filename = kasprintf(GFP_KERNEL, "%s/%s",
474 plat_data->fw_filename_prefix,
475 plat_data->fw_filename);
479 ret = request_firmware(&plat_data->fw, fw_filename, sdev->dev);
482 dev_err(sdev->dev, "error: request firmware %s failed err: %d\n",
485 dev_dbg(sdev->dev, "request_firmware %s successful\n",
493 EXPORT_SYMBOL(snd_sof_load_firmware_raw);
495 int snd_sof_load_firmware_memcpy(struct snd_sof_dev *sdev)
497 struct snd_sof_pdata *plat_data = sdev->pdata;
500 ret = snd_sof_load_firmware_raw(sdev);
504 /* make sure the FW header and file is valid */
505 ret = check_header(sdev, plat_data->fw);
507 dev_err(sdev->dev, "error: invalid FW header\n");
511 /* prepare the DSP for FW loading */
512 ret = snd_sof_dsp_reset(sdev);
514 dev_err(sdev->dev, "error: failed to reset DSP\n");
518 /* parse and load firmware modules to DSP */
519 ret = load_modules(sdev, plat_data->fw);
521 dev_err(sdev->dev, "error: invalid FW modules\n");
528 release_firmware(plat_data->fw);
529 plat_data->fw = NULL;
533 EXPORT_SYMBOL(snd_sof_load_firmware_memcpy);
535 int snd_sof_load_firmware(struct snd_sof_dev *sdev)
537 dev_dbg(sdev->dev, "loading firmware\n");
539 if (sof_ops(sdev)->load_firmware)
540 return sof_ops(sdev)->load_firmware(sdev);
543 EXPORT_SYMBOL(snd_sof_load_firmware);
545 int snd_sof_run_firmware(struct snd_sof_dev *sdev)
550 init_waitqueue_head(&sdev->boot_wait);
552 /* create read-only fw_version debugfs to store boot version info */
553 if (sdev->first_boot) {
554 ret = snd_sof_debugfs_buf_item(sdev, &sdev->fw_version,
555 sizeof(sdev->fw_version),
557 /* errors are only due to memory allocation, not debugfs */
559 dev_err(sdev->dev, "error: snd_sof_debugfs_buf_item failed\n");
564 /* perform pre fw run operations */
565 ret = snd_sof_dsp_pre_fw_run(sdev);
567 dev_err(sdev->dev, "error: failed pre fw run op\n");
571 dev_dbg(sdev->dev, "booting DSP firmware\n");
573 /* boot the firmware on the DSP */
574 ret = snd_sof_dsp_run(sdev);
576 dev_err(sdev->dev, "error: failed to reset DSP\n");
580 init_core_mask = ret;
583 * now wait for the DSP to boot. There are 3 possible outcomes:
584 * 1. Boot wait times out indicating FW boot failure.
585 * 2. FW boots successfully and fw_ready op succeeds.
586 * 3. FW boots but fw_ready op fails.
588 ret = wait_event_timeout(sdev->boot_wait,
589 sdev->fw_state > SOF_FW_BOOT_IN_PROGRESS,
590 msecs_to_jiffies(sdev->boot_timeout));
592 dev_err(sdev->dev, "error: firmware boot failure\n");
593 snd_sof_dsp_dbg_dump(sdev, SOF_DBG_REGS | SOF_DBG_MBOX |
594 SOF_DBG_TEXT | SOF_DBG_PCI);
595 sdev->fw_state = SOF_FW_BOOT_FAILED;
599 if (sdev->fw_state == SOF_FW_BOOT_COMPLETE)
600 dev_info(sdev->dev, "firmware boot complete\n");
602 return -EIO; /* FW boots but fw_ready op failed */
604 /* perform post fw run operations */
605 ret = snd_sof_dsp_post_fw_run(sdev);
607 dev_err(sdev->dev, "error: failed post fw run op\n");
611 /* fw boot is complete. Update the active cores mask */
612 sdev->enabled_cores_mask = init_core_mask;
616 EXPORT_SYMBOL(snd_sof_run_firmware);
618 void snd_sof_fw_unload(struct snd_sof_dev *sdev)
620 /* TODO: support module unloading at runtime */
622 EXPORT_SYMBOL(snd_sof_fw_unload);