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_DMA_BUFFER
:
101 case SOF_IPC_EXT_WINDOW
:
102 ret
= get_ext_windows(sdev
, ext_hdr
);
104 case SOF_IPC_EXT_CC_INFO
:
105 ret
= get_cc_info(sdev
, ext_hdr
);
108 dev_warn(sdev
->dev
, "warning: unknown ext header type %d size 0x%x\n",
109 ext_hdr
->type
, ext_hdr
->hdr
.size
);
115 dev_err(sdev
->dev
, "error: failed to parse ext data type %d\n",
120 /* move to next header */
121 offset
+= ext_hdr
->hdr
.size
;
122 snd_sof_dsp_block_read(sdev
, bar
, offset
, ext_data
,
130 EXPORT_SYMBOL(snd_sof_fw_parse_ext_data
);
133 * IPC Firmware ready.
135 static void sof_get_windows(struct snd_sof_dev
*sdev
)
137 struct sof_ipc_window_elem
*elem
;
138 u32 outbox_offset
= 0;
139 u32 stream_offset
= 0;
140 u32 inbox_offset
= 0;
148 if (!sdev
->info_window
) {
149 dev_err(sdev
->dev
, "error: have no window info\n");
153 bar
= snd_sof_dsp_get_bar_index(sdev
, SOF_FW_BLK_TYPE_SRAM
);
155 dev_err(sdev
->dev
, "error: have no bar mapping\n");
159 for (i
= 0; i
< sdev
->info_window
->num_windows
; i
++) {
160 elem
= &sdev
->info_window
->window
[i
];
162 window_offset
= snd_sof_dsp_get_window_offset(sdev
, elem
->id
);
163 if (window_offset
< 0) {
164 dev_warn(sdev
->dev
, "warn: no offset for window %d\n",
169 switch (elem
->type
) {
170 case SOF_IPC_REGION_UPBOX
:
171 inbox_offset
= window_offset
+ elem
->offset
;
172 inbox_size
= elem
->size
;
173 snd_sof_debugfs_io_item(sdev
,
177 SOF_DEBUGFS_ACCESS_D0_ONLY
);
179 case SOF_IPC_REGION_DOWNBOX
:
180 outbox_offset
= window_offset
+ elem
->offset
;
181 outbox_size
= elem
->size
;
182 snd_sof_debugfs_io_item(sdev
,
185 elem
->size
, "outbox",
186 SOF_DEBUGFS_ACCESS_D0_ONLY
);
188 case SOF_IPC_REGION_TRACE
:
189 snd_sof_debugfs_io_item(sdev
,
193 elem
->size
, "etrace",
194 SOF_DEBUGFS_ACCESS_D0_ONLY
);
196 case SOF_IPC_REGION_DEBUG
:
197 snd_sof_debugfs_io_item(sdev
,
202 SOF_DEBUGFS_ACCESS_D0_ONLY
);
204 case SOF_IPC_REGION_STREAM
:
205 stream_offset
= window_offset
+ elem
->offset
;
206 stream_size
= elem
->size
;
207 snd_sof_debugfs_io_item(sdev
,
210 elem
->size
, "stream",
211 SOF_DEBUGFS_ACCESS_D0_ONLY
);
213 case SOF_IPC_REGION_REGS
:
214 snd_sof_debugfs_io_item(sdev
,
219 SOF_DEBUGFS_ACCESS_D0_ONLY
);
221 case SOF_IPC_REGION_EXCEPTION
:
222 sdev
->dsp_oops_offset
= window_offset
+ elem
->offset
;
223 snd_sof_debugfs_io_item(sdev
,
227 elem
->size
, "exception",
228 SOF_DEBUGFS_ACCESS_D0_ONLY
);
231 dev_err(sdev
->dev
, "error: get illegal window info\n");
236 if (outbox_size
== 0 || inbox_size
== 0) {
237 dev_err(sdev
->dev
, "error: get illegal mailbox window\n");
241 snd_sof_dsp_mailbox_init(sdev
, inbox_offset
, inbox_size
,
242 outbox_offset
, outbox_size
);
243 sdev
->stream_box
.offset
= stream_offset
;
244 sdev
->stream_box
.size
= stream_size
;
246 dev_dbg(sdev
->dev
, " mailbox upstream 0x%x - size 0x%x\n",
247 inbox_offset
, inbox_size
);
248 dev_dbg(sdev
->dev
, " mailbox downstream 0x%x - size 0x%x\n",
249 outbox_offset
, outbox_size
);
250 dev_dbg(sdev
->dev
, " stream region 0x%x - size 0x%x\n",
251 stream_offset
, stream_size
);
254 /* check for ABI compatibility and create memory windows on first boot */
255 int sof_fw_ready(struct snd_sof_dev
*sdev
, u32 msg_id
)
257 struct sof_ipc_fw_ready
*fw_ready
= &sdev
->fw_ready
;
262 /* mailbox must be on 4k boundary */
263 offset
= snd_sof_dsp_get_mailbox_offset(sdev
);
265 dev_err(sdev
->dev
, "error: have no mailbox offset\n");
269 bar
= snd_sof_dsp_get_bar_index(sdev
, SOF_FW_BLK_TYPE_SRAM
);
271 dev_err(sdev
->dev
, "error: have no bar mapping\n");
275 dev_dbg(sdev
->dev
, "ipc: DSP is ready 0x%8.8x offset 0x%x\n",
278 /* no need to re-check version/ABI for subsequent boots */
279 if (!sdev
->first_boot
)
282 /* copy data from the DSP FW ready offset */
283 sof_block_read(sdev
, bar
, offset
, fw_ready
, sizeof(*fw_ready
));
285 /* make sure ABI version is compatible */
286 ret
= snd_sof_ipc_valid(sdev
);
290 /* now check for extended data */
291 snd_sof_fw_parse_ext_data(sdev
, bar
, offset
+
292 sizeof(struct sof_ipc_fw_ready
));
294 sof_get_windows(sdev
);
298 EXPORT_SYMBOL(sof_fw_ready
);
300 /* generic module parser for mmaped DSPs */
301 int snd_sof_parse_module_memcpy(struct snd_sof_dev
*sdev
,
302 struct snd_sof_mod_hdr
*module
)
304 struct snd_sof_blk_hdr
*block
;
309 dev_dbg(sdev
->dev
, "new module size 0x%x blocks 0x%x type 0x%x\n",
310 module
->size
, module
->num_blocks
, module
->type
);
312 block
= (struct snd_sof_blk_hdr
*)((u8
*)module
+ sizeof(*module
));
314 /* module->size doesn't include header size */
315 remaining
= module
->size
;
316 for (count
= 0; count
< module
->num_blocks
; count
++) {
318 if (remaining
< sizeof(*block
)) {
319 dev_err(sdev
->dev
, "error: not enough data remaining\n");
323 /* minus header size of block */
324 remaining
-= sizeof(*block
);
326 if (block
->size
== 0) {
328 "warning: block %d size zero\n", count
);
329 dev_warn(sdev
->dev
, " type 0x%x offset 0x%x\n",
330 block
->type
, block
->offset
);
334 switch (block
->type
) {
335 case SOF_FW_BLK_TYPE_RSRVD0
:
336 case SOF_FW_BLK_TYPE_ROM
...SOF_FW_BLK_TYPE_RSRVD14
:
337 continue; /* not handled atm */
338 case SOF_FW_BLK_TYPE_IRAM
:
339 case SOF_FW_BLK_TYPE_DRAM
:
340 case SOF_FW_BLK_TYPE_SRAM
:
341 offset
= block
->offset
;
342 bar
= snd_sof_dsp_get_bar_index(sdev
, block
->type
);
345 "error: no BAR mapping for block type 0x%x\n",
351 dev_err(sdev
->dev
, "error: bad type 0x%x for block 0x%x\n",
357 "block %d type 0x%x size 0x%x ==> offset 0x%x\n",
358 count
, block
->type
, block
->size
, offset
);
360 /* checking block->size to avoid unaligned access */
361 if (block
->size
% sizeof(u32
)) {
362 dev_err(sdev
->dev
, "error: invalid block size 0x%x\n",
366 snd_sof_dsp_block_write(sdev
, bar
, offset
,
367 block
+ 1, block
->size
);
369 if (remaining
< block
->size
) {
370 dev_err(sdev
->dev
, "error: not enough data remaining\n");
374 /* minus body size of block */
375 remaining
-= block
->size
;
377 block
= (struct snd_sof_blk_hdr
*)((u8
*)block
+ sizeof(*block
)
383 EXPORT_SYMBOL(snd_sof_parse_module_memcpy
);
385 static int check_header(struct snd_sof_dev
*sdev
, const struct firmware
*fw
)
387 struct snd_sof_fw_header
*header
;
389 /* Read the header information from the data pointer */
390 header
= (struct snd_sof_fw_header
*)fw
->data
;
393 if (strncmp(header
->sig
, SND_SOF_FW_SIG
, SND_SOF_FW_SIG_SIZE
) != 0) {
394 dev_err(sdev
->dev
, "error: invalid firmware signature\n");
398 /* check size is valid */
399 if (fw
->size
!= header
->file_size
+ sizeof(*header
)) {
400 dev_err(sdev
->dev
, "error: invalid filesize mismatch got 0x%zx expected 0x%zx\n",
401 fw
->size
, header
->file_size
+ sizeof(*header
));
405 dev_dbg(sdev
->dev
, "header size=0x%x modules=0x%x abi=0x%x size=%zu\n",
406 header
->file_size
, header
->num_modules
,
407 header
->abi
, sizeof(*header
));
412 static int load_modules(struct snd_sof_dev
*sdev
, const struct firmware
*fw
)
414 struct snd_sof_fw_header
*header
;
415 struct snd_sof_mod_hdr
*module
;
416 int (*load_module
)(struct snd_sof_dev
*sof_dev
,
417 struct snd_sof_mod_hdr
*hdr
);
421 header
= (struct snd_sof_fw_header
*)fw
->data
;
422 load_module
= sof_ops(sdev
)->load_module
;
426 /* parse each module */
427 module
= (struct snd_sof_mod_hdr
*)((u8
*)(fw
->data
) + sizeof(*header
));
428 remaining
= fw
->size
- sizeof(*header
);
430 if (remaining
> fw
->size
) {
431 dev_err(sdev
->dev
, "error: fw size smaller than header size\n");
435 for (count
= 0; count
< header
->num_modules
; count
++) {
437 if (remaining
< sizeof(*module
)) {
438 dev_err(sdev
->dev
, "error: not enough data remaining\n");
442 /* minus header size of module */
443 remaining
-= sizeof(*module
);
446 ret
= load_module(sdev
, module
);
448 dev_err(sdev
->dev
, "error: invalid module %d\n", count
);
452 if (remaining
< module
->size
) {
453 dev_err(sdev
->dev
, "error: not enough data remaining\n");
457 /* minus body size of module */
458 remaining
-= module
->size
;
459 module
= (struct snd_sof_mod_hdr
*)((u8
*)module
460 + sizeof(*module
) + module
->size
);
466 int snd_sof_load_firmware_raw(struct snd_sof_dev
*sdev
)
468 struct snd_sof_pdata
*plat_data
= sdev
->pdata
;
469 const char *fw_filename
;
472 /* set code loading condition to true */
473 sdev
->code_loading
= 1;
475 /* Don't request firmware again if firmware is already requested */
479 fw_filename
= kasprintf(GFP_KERNEL
, "%s/%s",
480 plat_data
->fw_filename_prefix
,
481 plat_data
->fw_filename
);
485 ret
= request_firmware(&plat_data
->fw
, fw_filename
, sdev
->dev
);
488 dev_err(sdev
->dev
, "error: request firmware %s failed err: %d\n",
491 dev_dbg(sdev
->dev
, "request_firmware %s successful\n",
499 EXPORT_SYMBOL(snd_sof_load_firmware_raw
);
501 int snd_sof_load_firmware_memcpy(struct snd_sof_dev
*sdev
)
503 struct snd_sof_pdata
*plat_data
= sdev
->pdata
;
506 ret
= snd_sof_load_firmware_raw(sdev
);
510 /* make sure the FW header and file is valid */
511 ret
= check_header(sdev
, plat_data
->fw
);
513 dev_err(sdev
->dev
, "error: invalid FW header\n");
517 /* prepare the DSP for FW loading */
518 ret
= snd_sof_dsp_reset(sdev
);
520 dev_err(sdev
->dev
, "error: failed to reset DSP\n");
524 /* parse and load firmware modules to DSP */
525 ret
= load_modules(sdev
, plat_data
->fw
);
527 dev_err(sdev
->dev
, "error: invalid FW modules\n");
534 release_firmware(plat_data
->fw
);
535 plat_data
->fw
= NULL
;
539 EXPORT_SYMBOL(snd_sof_load_firmware_memcpy
);
541 int snd_sof_load_firmware(struct snd_sof_dev
*sdev
)
543 dev_dbg(sdev
->dev
, "loading firmware\n");
545 if (sof_ops(sdev
)->load_firmware
)
546 return sof_ops(sdev
)->load_firmware(sdev
);
549 EXPORT_SYMBOL(snd_sof_load_firmware
);
551 int snd_sof_run_firmware(struct snd_sof_dev
*sdev
)
556 init_waitqueue_head(&sdev
->boot_wait
);
558 /* create read-only fw_version debugfs to store boot version info */
559 if (sdev
->first_boot
) {
560 ret
= snd_sof_debugfs_buf_item(sdev
, &sdev
->fw_version
,
561 sizeof(sdev
->fw_version
),
563 /* errors are only due to memory allocation, not debugfs */
565 dev_err(sdev
->dev
, "error: snd_sof_debugfs_buf_item failed\n");
570 /* perform pre fw run operations */
571 ret
= snd_sof_dsp_pre_fw_run(sdev
);
573 dev_err(sdev
->dev
, "error: failed pre fw run op\n");
577 dev_dbg(sdev
->dev
, "booting DSP firmware\n");
579 /* boot the firmware on the DSP */
580 ret
= snd_sof_dsp_run(sdev
);
582 dev_err(sdev
->dev
, "error: failed to reset DSP\n");
586 init_core_mask
= ret
;
589 * now wait for the DSP to boot. There are 3 possible outcomes:
590 * 1. Boot wait times out indicating FW boot failure.
591 * 2. FW boots successfully and fw_ready op succeeds.
592 * 3. FW boots but fw_ready op fails.
594 ret
= wait_event_timeout(sdev
->boot_wait
,
595 sdev
->fw_state
> SOF_FW_BOOT_IN_PROGRESS
,
596 msecs_to_jiffies(sdev
->boot_timeout
));
598 dev_err(sdev
->dev
, "error: firmware boot failure\n");
599 snd_sof_dsp_dbg_dump(sdev
, SOF_DBG_REGS
| SOF_DBG_MBOX
|
600 SOF_DBG_TEXT
| SOF_DBG_PCI
);
601 sdev
->fw_state
= SOF_FW_BOOT_FAILED
;
605 if (sdev
->fw_state
== SOF_FW_BOOT_COMPLETE
)
606 dev_info(sdev
->dev
, "firmware boot complete\n");
608 return -EIO
; /* FW boots but fw_ready op failed */
610 /* perform post fw run operations */
611 ret
= snd_sof_dsp_post_fw_run(sdev
);
613 dev_err(sdev
->dev
, "error: failed post fw run op\n");
617 /* fw boot is complete. Update the active cores mask */
618 sdev
->enabled_cores_mask
= init_core_mask
;
622 EXPORT_SYMBOL(snd_sof_run_firmware
);
624 void snd_sof_fw_unload(struct snd_sof_dev
*sdev
)
626 /* TODO: support module unloading at runtime */
628 EXPORT_SYMBOL(snd_sof_fw_unload
);