treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / sound / soc / sof / loader.c
blobfc4ab51bacf477325333df26cbfcada989867743
1 // SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
2 //
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.
5 //
6 // Copyright(c) 2018 Intel Corporation. All rights reserved.
7 //
8 // Author: Liam Girdwood <liam.r.girdwood@linux.intel.com>
9 //
10 // Generic firmware loader.
13 #include <linux/firmware.h>
14 #include <sound/sof.h>
15 #include "ops.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)
24 return -EINVAL;
26 /* keep a local copy of the data */
27 sdev->info_window = kmemdup(w, struct_size(w, window, w->num_windows),
28 GFP_KERNEL);
29 if (!sdev->info_window)
30 return -ENOMEM;
32 return 0;
35 static int get_cc_info(struct snd_sof_dev *sdev,
36 struct sof_ipc_ext_data_hdr *ext_hdr)
38 int ret;
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,
45 cc->optim);
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,
51 GFP_KERNEL);
53 if (!sdev->cc_version)
54 return -ENOMEM;
56 memcpy(sdev->cc_version, cc, cc->ext_hdr.hdr.size);
57 ret = snd_sof_debugfs_buf_item(sdev, sdev->cc_version,
58 cc->ext_hdr.hdr.size,
59 "cc_version", 0444);
61 /* errors are only due to memory allocation, not debugfs */
62 if (ret < 0) {
63 dev_err(sdev->dev, "error: snd_sof_debugfs_buf_item failed\n");
64 return ret;
68 return 0;
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;
75 void *ext_data;
76 int ret = 0;
78 ext_data = kzalloc(PAGE_SIZE, GFP_KERNEL);
79 if (!ext_data)
80 return -ENOMEM;
82 /* get first header */
83 snd_sof_dsp_block_read(sdev, bar, offset, ext_data,
84 sizeof(*ext_hdr));
85 ext_hdr = 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:
99 ret = 0;
100 break;
101 case SOF_IPC_EXT_WINDOW:
102 ret = get_ext_windows(sdev, ext_hdr);
103 break;
104 case SOF_IPC_EXT_CC_INFO:
105 ret = get_cc_info(sdev, ext_hdr);
106 break;
107 default:
108 dev_warn(sdev->dev, "warning: unknown ext header type %d size 0x%x\n",
109 ext_hdr->type, ext_hdr->hdr.size);
110 ret = 0;
111 break;
114 if (ret < 0) {
115 dev_err(sdev->dev, "error: failed to parse ext data type %d\n",
116 ext_hdr->type);
117 break;
120 /* move to next header */
121 offset += ext_hdr->hdr.size;
122 snd_sof_dsp_block_read(sdev, bar, offset, ext_data,
123 sizeof(*ext_hdr));
124 ext_hdr = ext_data;
127 kfree(ext_data);
128 return ret;
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;
141 u32 outbox_size = 0;
142 u32 stream_size = 0;
143 u32 inbox_size = 0;
144 int window_offset;
145 int bar;
146 int i;
148 if (!sdev->info_window) {
149 dev_err(sdev->dev, "error: have no window info\n");
150 return;
153 bar = snd_sof_dsp_get_bar_index(sdev, SOF_FW_BLK_TYPE_SRAM);
154 if (bar < 0) {
155 dev_err(sdev->dev, "error: have no bar mapping\n");
156 return;
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",
165 elem->id);
166 continue;
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,
174 sdev->bar[bar] +
175 inbox_offset,
176 elem->size, "inbox",
177 SOF_DEBUGFS_ACCESS_D0_ONLY);
178 break;
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,
183 sdev->bar[bar] +
184 outbox_offset,
185 elem->size, "outbox",
186 SOF_DEBUGFS_ACCESS_D0_ONLY);
187 break;
188 case SOF_IPC_REGION_TRACE:
189 snd_sof_debugfs_io_item(sdev,
190 sdev->bar[bar] +
191 window_offset +
192 elem->offset,
193 elem->size, "etrace",
194 SOF_DEBUGFS_ACCESS_D0_ONLY);
195 break;
196 case SOF_IPC_REGION_DEBUG:
197 snd_sof_debugfs_io_item(sdev,
198 sdev->bar[bar] +
199 window_offset +
200 elem->offset,
201 elem->size, "debug",
202 SOF_DEBUGFS_ACCESS_D0_ONLY);
203 break;
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,
208 sdev->bar[bar] +
209 stream_offset,
210 elem->size, "stream",
211 SOF_DEBUGFS_ACCESS_D0_ONLY);
212 break;
213 case SOF_IPC_REGION_REGS:
214 snd_sof_debugfs_io_item(sdev,
215 sdev->bar[bar] +
216 window_offset +
217 elem->offset,
218 elem->size, "regs",
219 SOF_DEBUGFS_ACCESS_D0_ONLY);
220 break;
221 case SOF_IPC_REGION_EXCEPTION:
222 sdev->dsp_oops_offset = window_offset + elem->offset;
223 snd_sof_debugfs_io_item(sdev,
224 sdev->bar[bar] +
225 window_offset +
226 elem->offset,
227 elem->size, "exception",
228 SOF_DEBUGFS_ACCESS_D0_ONLY);
229 break;
230 default:
231 dev_err(sdev->dev, "error: get illegal window info\n");
232 return;
236 if (outbox_size == 0 || inbox_size == 0) {
237 dev_err(sdev->dev, "error: get illegal mailbox window\n");
238 return;
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;
258 int offset;
259 int bar;
260 int ret;
262 /* mailbox must be on 4k boundary */
263 offset = snd_sof_dsp_get_mailbox_offset(sdev);
264 if (offset < 0) {
265 dev_err(sdev->dev, "error: have no mailbox offset\n");
266 return offset;
269 bar = snd_sof_dsp_get_bar_index(sdev, SOF_FW_BLK_TYPE_SRAM);
270 if (bar < 0) {
271 dev_err(sdev->dev, "error: have no bar mapping\n");
272 return -EINVAL;
275 dev_dbg(sdev->dev, "ipc: DSP is ready 0x%8.8x offset 0x%x\n",
276 msg_id, offset);
278 /* no need to re-check version/ABI for subsequent boots */
279 if (!sdev->first_boot)
280 return 0;
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);
287 if (ret < 0)
288 return ret;
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);
296 return 0;
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;
305 int count, bar;
306 u32 offset;
307 size_t remaining;
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++) {
317 /* check for wrap */
318 if (remaining < sizeof(*block)) {
319 dev_err(sdev->dev, "error: not enough data remaining\n");
320 return -EINVAL;
323 /* minus header size of block */
324 remaining -= sizeof(*block);
326 if (block->size == 0) {
327 dev_warn(sdev->dev,
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);
331 continue;
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);
343 if (bar < 0) {
344 dev_err(sdev->dev,
345 "error: no BAR mapping for block type 0x%x\n",
346 block->type);
347 return bar;
349 break;
350 default:
351 dev_err(sdev->dev, "error: bad type 0x%x for block 0x%x\n",
352 block->type, count);
353 return -EINVAL;
356 dev_dbg(sdev->dev,
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",
363 block->size);
364 return -EINVAL;
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");
371 return -EINVAL;
374 /* minus body size of block */
375 remaining -= block->size;
376 /* next block */
377 block = (struct snd_sof_blk_hdr *)((u8 *)block + sizeof(*block)
378 + block->size);
381 return 0;
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;
392 /* verify FW sig */
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");
395 return -EINVAL;
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));
402 return -EINVAL;
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));
409 return 0;
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);
418 int ret, count;
419 size_t remaining;
421 header = (struct snd_sof_fw_header *)fw->data;
422 load_module = sof_ops(sdev)->load_module;
423 if (!load_module)
424 return -EINVAL;
426 /* parse each module */
427 module = (struct snd_sof_mod_hdr *)((u8 *)(fw->data) + sizeof(*header));
428 remaining = fw->size - sizeof(*header);
429 /* check for wrap */
430 if (remaining > fw->size) {
431 dev_err(sdev->dev, "error: fw size smaller than header size\n");
432 return -EINVAL;
435 for (count = 0; count < header->num_modules; count++) {
436 /* check for wrap */
437 if (remaining < sizeof(*module)) {
438 dev_err(sdev->dev, "error: not enough data remaining\n");
439 return -EINVAL;
442 /* minus header size of module */
443 remaining -= sizeof(*module);
445 /* module */
446 ret = load_module(sdev, module);
447 if (ret < 0) {
448 dev_err(sdev->dev, "error: invalid module %d\n", count);
449 return ret;
452 if (remaining < module->size) {
453 dev_err(sdev->dev, "error: not enough data remaining\n");
454 return -EINVAL;
457 /* minus body size of module */
458 remaining -= module->size;
459 module = (struct snd_sof_mod_hdr *)((u8 *)module
460 + sizeof(*module) + module->size);
463 return 0;
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;
470 int ret;
472 /* set code loading condition to true */
473 sdev->code_loading = 1;
475 /* Don't request firmware again if firmware is already requested */
476 if (plat_data->fw)
477 return 0;
479 fw_filename = kasprintf(GFP_KERNEL, "%s/%s",
480 plat_data->fw_filename_prefix,
481 plat_data->fw_filename);
482 if (!fw_filename)
483 return -ENOMEM;
485 ret = request_firmware(&plat_data->fw, fw_filename, sdev->dev);
487 if (ret < 0) {
488 dev_err(sdev->dev, "error: request firmware %s failed err: %d\n",
489 fw_filename, ret);
490 } else {
491 dev_dbg(sdev->dev, "request_firmware %s successful\n",
492 fw_filename);
495 kfree(fw_filename);
497 return ret;
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;
504 int ret;
506 ret = snd_sof_load_firmware_raw(sdev);
507 if (ret < 0)
508 return ret;
510 /* make sure the FW header and file is valid */
511 ret = check_header(sdev, plat_data->fw);
512 if (ret < 0) {
513 dev_err(sdev->dev, "error: invalid FW header\n");
514 goto error;
517 /* prepare the DSP for FW loading */
518 ret = snd_sof_dsp_reset(sdev);
519 if (ret < 0) {
520 dev_err(sdev->dev, "error: failed to reset DSP\n");
521 goto error;
524 /* parse and load firmware modules to DSP */
525 ret = load_modules(sdev, plat_data->fw);
526 if (ret < 0) {
527 dev_err(sdev->dev, "error: invalid FW modules\n");
528 goto error;
531 return 0;
533 error:
534 release_firmware(plat_data->fw);
535 plat_data->fw = NULL;
536 return ret;
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);
547 return 0;
549 EXPORT_SYMBOL(snd_sof_load_firmware);
551 int snd_sof_run_firmware(struct snd_sof_dev *sdev)
553 int ret;
554 int init_core_mask;
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),
562 "fw_version", 0444);
563 /* errors are only due to memory allocation, not debugfs */
564 if (ret < 0) {
565 dev_err(sdev->dev, "error: snd_sof_debugfs_buf_item failed\n");
566 return ret;
570 /* perform pre fw run operations */
571 ret = snd_sof_dsp_pre_fw_run(sdev);
572 if (ret < 0) {
573 dev_err(sdev->dev, "error: failed pre fw run op\n");
574 return ret;
577 dev_dbg(sdev->dev, "booting DSP firmware\n");
579 /* boot the firmware on the DSP */
580 ret = snd_sof_dsp_run(sdev);
581 if (ret < 0) {
582 dev_err(sdev->dev, "error: failed to reset DSP\n");
583 return ret;
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));
597 if (ret == 0) {
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;
602 return -EIO;
605 if (sdev->fw_state == SOF_FW_BOOT_COMPLETE)
606 dev_info(sdev->dev, "firmware boot complete\n");
607 else
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);
612 if (ret < 0) {
613 dev_err(sdev->dev, "error: failed post fw run op\n");
614 return ret;
617 /* fw boot is complete. Update the active cores mask */
618 sdev->enabled_cores_mask = init_core_mask;
620 return 0;
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);