1 // SPDX-License-Identifier: (GPL-2.0-only 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) 2022 Intel Corporation. All rights reserved.
8 #include <linux/firmware.h>
9 #include <sound/sof/ext_manifest4.h>
10 #include <sound/sof/ipc4/header.h>
11 #include "ipc4-priv.h"
12 #include "sof-audio.h"
16 static size_t sof_ipc4_fw_parse_ext_man(struct snd_sof_dev *sdev)
18 struct sof_ipc4_fw_data *ipc4_data = sdev->private;
19 struct snd_sof_pdata *plat_data = sdev->pdata;
20 struct sof_man4_fw_binary_header *fw_header;
21 const struct firmware *fw = plat_data->fw;
22 struct sof_ext_manifest4_hdr *ext_man_hdr;
23 struct sof_man4_module_config *fm_config;
24 struct sof_ipc4_fw_module *fw_module;
25 struct sof_man4_module *fm_entry;
31 dev_err(sdev->dev, "%s: ipc4_data is not available\n", __func__);
36 if (remaining <= sizeof(*ext_man_hdr)) {
37 dev_err(sdev->dev, "Firmware size is too small: %zu\n", remaining);
41 ext_man_hdr = (struct sof_ext_manifest4_hdr *)fw->data;
43 fw_hdr_offset = ipc4_data->manifest_fw_hdr_offset;
47 if (remaining <= ext_man_hdr->len + fw_hdr_offset + sizeof(*fw_header)) {
48 dev_err(sdev->dev, "Invalid firmware size %zu, should be at least %zu\n",
49 remaining, ext_man_hdr->len + fw_hdr_offset + sizeof(*fw_header));
53 fw_header = (struct sof_man4_fw_binary_header *)
54 (fw->data + ext_man_hdr->len + fw_hdr_offset);
55 remaining -= (ext_man_hdr->len + fw_hdr_offset);
57 if (remaining <= fw_header->len) {
58 dev_err(sdev->dev, "Invalid fw_header->len %u\n", fw_header->len);
62 dev_info(sdev->dev, "Loaded firmware version: %u.%u.%u.%u\n",
63 fw_header->major_version, fw_header->minor_version,
64 fw_header->hotfix_version, fw_header->build_version);
65 dev_dbg(sdev->dev, "Firmware name: %s, header length: %u, module count: %u\n",
66 fw_header->name, fw_header->len, fw_header->num_module_entries);
68 ipc4_data->fw_modules = devm_kmalloc_array(sdev->dev,
69 fw_header->num_module_entries,
70 sizeof(*fw_module), GFP_KERNEL);
71 if (!ipc4_data->fw_modules)
74 ipc4_data->num_fw_modules = fw_header->num_module_entries;
75 fw_module = ipc4_data->fw_modules;
77 fm_entry = (struct sof_man4_module *)((u8 *)fw_header + fw_header->len);
78 remaining -= fw_header->len;
80 if (remaining < fw_header->num_module_entries * sizeof(*fm_entry)) {
81 dev_err(sdev->dev, "Invalid num_module_entries %u\n",
82 fw_header->num_module_entries);
86 fm_config = (struct sof_man4_module_config *)
87 (fm_entry + fw_header->num_module_entries);
88 remaining -= (fw_header->num_module_entries * sizeof(*fm_entry));
89 for (i = 0; i < fw_header->num_module_entries; i++) {
90 memcpy(&fw_module->man4_module_entry, fm_entry, sizeof(*fm_entry));
92 if (fm_entry->cfg_count) {
93 if (remaining < (fm_entry->cfg_offset + fm_entry->cfg_count) *
95 dev_err(sdev->dev, "Invalid module cfg_offset %u\n",
96 fm_entry->cfg_offset);
100 /* a module's config is always the same size */
101 fw_module->bss_size = fm_config[fm_entry->cfg_offset].is_bytes;
104 "module %s: UUID %pUL cfg_count: %u, bss_size: %#x\n",
105 fm_entry->name, &fm_entry->uuid, fm_entry->cfg_count,
106 fw_module->bss_size);
108 fw_module->bss_size = 0;
110 dev_dbg(sdev->dev, "module %s: UUID %pUL\n", fm_entry->name,
114 fw_module->man4_module_entry.id = i;
115 ida_init(&fw_module->m_ida);
116 fw_module->private = NULL;
122 return ext_man_hdr->len;
125 static int sof_ipc4_validate_firmware(struct snd_sof_dev *sdev)
127 struct sof_ipc4_fw_data *ipc4_data = sdev->private;
128 u32 fw_hdr_offset = ipc4_data->manifest_fw_hdr_offset;
129 struct snd_sof_pdata *plat_data = sdev->pdata;
130 struct sof_man4_fw_binary_header *fw_header;
131 const struct firmware *fw = plat_data->fw;
132 struct sof_ext_manifest4_hdr *ext_man_hdr;
134 ext_man_hdr = (struct sof_ext_manifest4_hdr *)fw->data;
135 fw_header = (struct sof_man4_fw_binary_header *)
136 (fw->data + ext_man_hdr->len + fw_hdr_offset);
138 /* TODO: Add firmware verification code here */
140 dev_dbg(sdev->dev, "Validated firmware version: %u.%u.%u.%u\n",
141 fw_header->major_version, fw_header->minor_version,
142 fw_header->hotfix_version, fw_header->build_version);
147 static int sof_ipc4_query_fw_configuration(struct snd_sof_dev *sdev)
149 const struct sof_ipc_ops *iops = sdev->ipc->ops;
150 struct sof_ipc4_fw_version *fw_ver;
151 struct sof_ipc4_tuple *tuple;
152 struct sof_ipc4_msg msg;
156 /* Get the firmware configuration */
157 msg.primary = SOF_IPC4_MSG_TARGET(SOF_IPC4_MODULE_MSG);
158 msg.primary |= SOF_IPC4_MSG_DIR(SOF_IPC4_MSG_REQUEST);
159 msg.primary |= SOF_IPC4_MOD_ID(SOF_IPC4_MOD_INIT_BASEFW_MOD_ID);
160 msg.primary |= SOF_IPC4_MOD_INSTANCE(SOF_IPC4_MOD_INIT_BASEFW_INSTANCE_ID);
161 msg.extension = SOF_IPC4_MOD_EXT_MSG_PARAM_ID(SOF_IPC4_FW_PARAM_FW_CONFIG);
163 msg.data_size = sdev->ipc->max_payload_size;
164 msg.data_ptr = kzalloc(msg.data_size, GFP_KERNEL);
168 ret = iops->set_get_data(sdev, &msg, msg.data_size, false);
172 while (offset < msg.data_size) {
173 tuple = (struct sof_ipc4_tuple *)((u8 *)msg.data_ptr + offset);
175 switch (tuple->type) {
176 case SOF_IPC4_FW_CFG_FW_VERSION:
177 fw_ver = (struct sof_ipc4_fw_version *)tuple->value;
180 "Booted firmware version: %u.%u.%u.%u\n",
181 fw_ver->major, fw_ver->minor, fw_ver->hotfix,
184 case SOF_IPC4_FW_CFG_DL_MAILBOX_BYTES:
185 dev_vdbg(sdev->dev, "DL mailbox size: %u\n", *tuple->value);
187 case SOF_IPC4_FW_CFG_UL_MAILBOX_BYTES:
188 dev_vdbg(sdev->dev, "UL mailbox size: %u\n", *tuple->value);
190 case SOF_IPC4_FW_CFG_TRACE_LOG_BYTES:
191 dev_vdbg(sdev->dev, "Trace log size: %u\n", *tuple->value);
197 offset += sizeof(*tuple) + tuple->size;
206 const struct sof_ipc_fw_loader_ops ipc4_loader_ops = {
207 .validate = sof_ipc4_validate_firmware,
208 .parse_ext_manifest = sof_ipc4_fw_parse_ext_man,
209 .query_fw_configuration = sof_ipc4_query_fw_configuration,