diff --git a/include/sound/sof.h b/include/sound/sof.h index 813680ab9aad4c..189006a5d2b24e 100644 --- a/include/sound/sof.h +++ b/include/sound/sof.h @@ -77,6 +77,11 @@ struct snd_sof_pdata { void *hw_pdata; }; +struct snd_sof_ext_man_pdata { + int default_ops_index; + u32 fw_hdr_offset; +}; + /* * Descriptor used for setting up SOF platform data. This is used when * ACPI/PCI data is missing or mapped differently. @@ -115,6 +120,8 @@ struct sof_dev_desc { const char *default_fw_filename; const struct snd_sof_dsp_ops *ops; + + const struct snd_sof_ext_man_pdata *ext_man_pdata; }; int sof_dai_get_mclk(struct snd_soc_pcm_runtime *rtd); diff --git a/include/sound/sof/ext_manifest4.h b/include/sound/sof/ext_manifest4.h new file mode 100644 index 00000000000000..f8f3ec29790e93 --- /dev/null +++ b/include/sound/sof/ext_manifest4.h @@ -0,0 +1,122 @@ +/* SPDX-License-Identifier: ((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Clause) */ +/* + * This file is provided under a dual BSD/GPLv2 license. When using or + * redistributing this file, you may do so under either license. + * + * Copyright(c) 2021 Intel Corporation. All rights reserved. + */ + +/* + * Extended manifest is a place to store metadata about firmware, known during + * compilation time - for example firmware version or used compiler. + * Given information are read on host side before firmware startup. + * This part of output binary is not signed. + * Note that not all items in the struct are 32 bits length, and we assume + * the firmware is little-endian. + */ + +#ifndef __SOF_FIRMWARE_EXT_MANIFEST4_H__ +#define __SOF_FIRMWARE_EXT_MANIFEST4_H__ + +#include + +/* In ASCII $AE1 */ +#define SOF_MAN4_MAGIC_NUMBER 0x31454124 + +#define SOF_MAN4_MAX_MODULE_NAME_LEN 8 +#define SOF_MAN4_MAX_FW_BINARY_NAME 8 +#define SOF_MAN4_DEFAULT_HASH_SHA256_LEN 32 +#define SOF_MAN4_SEGMENT_NUM 3 +#define SOF_MAN4_MODULE_PARAMS_NUM 4 +#define SOF_MAN4_FW_HDR_OFFSET 0x2000 + +/********************************************************************* + * extended manifest + *------------------- + * css_manifest hdr + *------------------- + * offset reserved for future + *------------------- + * fw_hdr + *------------------- + * module_entry[0] + *------------------- + * module_entry[1] + *------------------- + * ... + *------------------- + * module_entry[n] + *------------------- + * module_config[0] + *------------------- + * module_config[1] + *------------------- + * ... + *------------------- + * module_config[m] + *------------------- + * FW content + *------------------- + *********************************************************************/ + +struct sof_ext_manifest4_hdr { + u32 id; + u32 len; /* length of extension manifest */ + u16 version_major; + u16 version_minor; + u32 entries; +} __packed; + +struct sof_man4_fw_binary_header { + /* This part must be unchanged to be backward compatible with SPT-LP ROM */ + uint32_t id; + uint32_t len; /* sizeof(sof_man4_fw_binary_header) in bytes */ + uint8_t name[SOF_MAN4_MAX_FW_BINARY_NAME]; + uint32_t preload_page_count; /* number of pages of preloaded image */ + uint32_t fw_image_flags; + uint32_t feature_mask; + uint16_t major_version; + uint16_t minor_version; + uint16_t hotfix_version; + uint16_t build_version; + uint32_t num_module_entries; + + /* This part may change to contain any additional data for BaseFw that is skipped by ROM */ + uint32_t hw_buf_base_addr; + uint32_t hw_buf_length; + uint32_t load_offset; /* This value is used by ROM */ +} __packed; + +struct sof_man4_segment_desc { + uint32_t flags; + uint32_t v_base_addr; + uint32_t file_offset; +} __packed; + +struct sof_man4_module { + uint32_t id; + uint8_t name[SOF_MAN4_MAX_MODULE_NAME_LEN]; + uint8_t uuid[UUID_SIZE]; + uint32_t type; + uint8_t hash[SOF_MAN4_DEFAULT_HASH_SHA256_LEN]; + uint32_t entry_point; + uint16_t cfg_offset; + uint16_t cfg_count; + uint32_t affinity_mask; + uint16_t instance_max_count; + uint16_t instance_stack_size; + struct sof_man4_segment_desc segments[SOF_MAN4_SEGMENT_NUM]; +} __packed; + +struct sof_man4_module_config { + uint32_t par[SOF_MAN4_MODULE_PARAMS_NUM]; /* module parameters */ + uint32_t is_bytes; /* actual size of instance .bss (bytes) */ + uint32_t cps; /* cycles per second */ + uint32_t ibs; /* input buffer size (bytes) */ + uint32_t obs; /* output buffer size (bytes) */ + uint32_t module_flags; /* flags, reserved for future use */ + uint32_t cpc; /* max cycles per scheduling period */ + uint32_t obls; /* output block size, reserved for future use */ +} __packed; + +#endif /* __SOF_FIRMWARE_EXT_MANIFEST4_H__ */ diff --git a/sound/soc/sof/loader.c b/sound/soc/sof/loader.c index 4e8ba0e806ef11..f5886235dc2514 100644 --- a/sound/soc/sof/loader.c +++ b/sound/soc/sof/loader.c @@ -10,9 +10,11 @@ // Generic firmware loader. // +#include #include #include #include +#include #include "sof-priv.h" #include "ops.h" @@ -272,8 +274,8 @@ static ssize_t snd_sof_ext_man_size(const struct firmware *fw) } /* parse extended FW manifest data structures */ -static int snd_sof_fw_ext_man_parse(struct snd_sof_dev *sdev, - const struct firmware *fw) +static int snd_sof_fw_ext_man3_parse(struct snd_sof_dev *sdev, + const struct firmware *fw) { const struct sof_ext_man_elem_header *elem_hdr; const struct sof_ext_man_header *head; @@ -358,6 +360,109 @@ static int snd_sof_fw_ext_man_parse(struct snd_sof_dev *sdev, return ext_man_size; } +static int snd_sof_fw_ext_man4_parse(struct snd_sof_dev *sdev, const struct firmware *fw) +{ + struct snd_sof_pdata *sof_pdata = sdev->pdata; + const struct sof_dev_desc *desc = sof_pdata->desc; + const struct snd_sof_ext_man_pdata *ext_man_pdata = desc->ext_man_pdata; + struct sof_ipc4_fw_modules *module_entry; + struct sof_man4_fw_binary_header *fw_header; + struct sof_ext_manifest4_hdr *hdr; + struct sof_man4_module_config *fm_config; + struct sof_man4_module *fm_entry; + ssize_t remaining; + u32 fw_hdr_offset; + int i; + + remaining = fw->size; + if (remaining <= sizeof(*hdr)) { + dev_err(sdev->dev, "Invalid fw size\n"); + return -EINVAL; + } + + hdr = (struct sof_ext_manifest4_hdr *)fw->data; + + if (ext_man_pdata) + fw_hdr_offset = ext_man_pdata->fw_hdr_offset; + else + fw_hdr_offset = SOF_MAN4_FW_HDR_OFFSET; + + if (remaining <= hdr->len + fw_hdr_offset + sizeof(*fw_header)) { + dev_err(sdev->dev, "Invalid hdr->len %d\n", hdr->len); + return -EINVAL; + } + + fw_header = (struct sof_man4_fw_binary_header *)(fw->data + hdr->len + + fw_hdr_offset); + remaining -= (hdr->len + fw_hdr_offset); + + if (remaining <= fw_header->len) { + dev_err(sdev->dev, "Invalid fw_header->len %d\n", fw_header->len); + return -EINVAL; + } + + dev_dbg(sdev->dev, " fw %s: header length %x, module num %d", fw_header->name, + fw_header->len, fw_header->num_module_entries); + + sdev->fw_modules = devm_kmalloc_array(sdev->dev, fw_header->num_module_entries, + sizeof(*module_entry), GFP_KERNEL); + if (!sdev->fw_modules) + return -ENOMEM; + + sdev->fw_module_num = fw_header->num_module_entries; + module_entry = sdev->fw_modules; + + fm_entry = (struct sof_man4_module *)((u8 *)fw_header + fw_header->len); + remaining -= fw_header->len; + + if (remaining < fw_header->num_module_entries * sizeof(*fm_entry)) { + dev_err(sdev->dev, "Invalid num_module_entries %d\n", + fw_header->num_module_entries); + return -EINVAL; + } + + fm_config = (struct sof_man4_module_config *)(fm_entry + fw_header->num_module_entries); + remaining -= (fw_header->num_module_entries * sizeof(*fm_entry)); + for (i = 0; i < fw_header->num_module_entries; i++) { + dev_dbg(sdev->dev, "module %s : UUID %pUL, ", fm_entry->name, + fm_entry->uuid); + + memcpy(&module_entry->man4_module_entry, fm_entry, sizeof(*fm_entry)); + + if (fm_entry->cfg_count) { + if (remaining < (fm_entry->cfg_offset + fm_entry->cfg_count) * + sizeof(*fm_config)) { + dev_err(sdev->dev, "Invalid cfg_offset %d\n", fm_entry->cfg_offset); + return -EINVAL; + } + + module_entry->bss_size = fm_config[fm_entry->cfg_offset].is_bytes; + } + + module_entry->man4_module_entry.id = i; + + module_entry++; + fm_entry++; + } + + return hdr->len; +} + +static int snd_sof_fw_ext_man_parse(struct snd_sof_dev *sdev, + const struct firmware *fw) +{ + const struct sof_ext_manifest4_hdr *hdr = (struct sof_ext_manifest4_hdr *)fw->data; + + /* + * The fw size should be at least larger than the size of *hdr, and then + * we can check the hdr->id to see if it is a manifest4 firmware. + */ + if (fw->size > sizeof(*hdr) && hdr->id == SOF_MAN4_MAGIC_NUMBER) + return snd_sof_fw_ext_man4_parse(sdev, fw); + + return snd_sof_fw_ext_man3_parse(sdev, fw); +} + /* * IPC Firmware ready. */ diff --git a/sound/soc/sof/sof-priv.h b/sound/soc/sof/sof-priv.h index 5061b0078f26a3..9f0d6f3fcb8844 100644 --- a/sound/soc/sof/sof-priv.h +++ b/sound/soc/sof/sof-priv.h @@ -19,6 +19,7 @@ #include #include #include +#include #include /* Flag definitions used in sof_core_debug (sof_debug module parameter) */ @@ -392,6 +393,12 @@ struct snd_sof_ipc { struct snd_sof_ipc_msg msg; }; +struct sof_ipc4_fw_modules { + struct sof_man4_module man4_module_entry; + u32 bss_size; + void *private; +}; + /* * SOF Device Level. */ @@ -468,6 +475,10 @@ struct snd_sof_dev { /* FW configuration */ struct sof_ipc_window *info_window; + /* FW module info */ + int fw_module_num; + void *fw_modules; + /* IPC timeouts in ms */ int ipc_timeout; int boot_timeout;