1 // SPDX-License-Identifier: (GPL-2.0-only 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) 2022 Intel Corporation. All rights reserved. 7 8 #include <linux/firmware.h> 9 #include <sound/sof/ext_manifest4.h> 10 #include <sound/sof/ipc4/header.h> 11 #include <trace/events/sof.h> 12 #include "ipc4-priv.h" 13 #include "sof-audio.h" 14 #include "sof-priv.h" 15 #include "ops.h" 16 17 static size_t sof_ipc4_fw_parse_ext_man(struct snd_sof_dev *sdev, 18 struct sof_ipc4_fw_library *fw_lib) 19 { 20 struct sof_ipc4_fw_data *ipc4_data = sdev->private; 21 const struct firmware *fw = fw_lib->sof_fw.fw; 22 struct sof_man4_fw_binary_header *fw_header; 23 struct sof_ext_manifest4_hdr *ext_man_hdr; 24 struct sof_man4_module_config *fm_config; 25 struct sof_ipc4_fw_module *fw_module; 26 struct sof_man4_module *fm_entry; 27 ssize_t remaining; 28 u32 fw_hdr_offset; 29 int i; 30 31 if (!ipc4_data) { 32 dev_err(sdev->dev, "%s: ipc4_data is not available\n", __func__); 33 return -EINVAL; 34 } 35 36 remaining = fw->size; 37 if (remaining <= sizeof(*ext_man_hdr)) { 38 dev_err(sdev->dev, "Firmware size is too small: %zu\n", remaining); 39 return -EINVAL; 40 } 41 42 ext_man_hdr = (struct sof_ext_manifest4_hdr *)fw->data; 43 44 /* 45 * At the start of the firmware image we must have an extended manifest. 46 * Verify that the magic number is correct. 47 */ 48 if (ext_man_hdr->id != SOF_EXT_MAN4_MAGIC_NUMBER) { 49 dev_err(sdev->dev, 50 "Unexpected extended manifest magic number: %#x\n", 51 ext_man_hdr->id); 52 return -EINVAL; 53 } 54 55 fw_hdr_offset = ipc4_data->manifest_fw_hdr_offset; 56 if (!fw_hdr_offset) 57 return -EINVAL; 58 59 if (remaining <= ext_man_hdr->len + fw_hdr_offset + sizeof(*fw_header)) { 60 dev_err(sdev->dev, "Invalid firmware size %zu, should be at least %zu\n", 61 remaining, ext_man_hdr->len + fw_hdr_offset + sizeof(*fw_header)); 62 return -EINVAL; 63 } 64 65 fw_header = (struct sof_man4_fw_binary_header *) 66 (fw->data + ext_man_hdr->len + fw_hdr_offset); 67 remaining -= (ext_man_hdr->len + fw_hdr_offset); 68 69 if (remaining <= fw_header->len) { 70 dev_err(sdev->dev, "Invalid fw_header->len %u\n", fw_header->len); 71 return -EINVAL; 72 } 73 74 dev_info(sdev->dev, "Loaded firmware version: %u.%u.%u.%u\n", 75 fw_header->major_version, fw_header->minor_version, 76 fw_header->hotfix_version, fw_header->build_version); 77 dev_dbg(sdev->dev, "Firmware name: %s, header length: %u, module count: %u\n", 78 fw_header->name, fw_header->len, fw_header->num_module_entries); 79 80 fw_lib->modules = devm_kmalloc_array(sdev->dev, fw_header->num_module_entries, 81 sizeof(*fw_module), GFP_KERNEL); 82 if (!fw_lib->modules) 83 return -ENOMEM; 84 85 fw_lib->num_modules = fw_header->num_module_entries; 86 fw_module = fw_lib->modules; 87 88 fm_entry = (struct sof_man4_module *)((u8 *)fw_header + fw_header->len); 89 remaining -= fw_header->len; 90 91 if (remaining < fw_header->num_module_entries * sizeof(*fm_entry)) { 92 dev_err(sdev->dev, "Invalid num_module_entries %u\n", 93 fw_header->num_module_entries); 94 return -EINVAL; 95 } 96 97 fm_config = (struct sof_man4_module_config *) 98 (fm_entry + fw_header->num_module_entries); 99 remaining -= (fw_header->num_module_entries * sizeof(*fm_entry)); 100 for (i = 0; i < fw_header->num_module_entries; i++) { 101 memcpy(&fw_module->man4_module_entry, fm_entry, sizeof(*fm_entry)); 102 103 if (fm_entry->cfg_count) { 104 if (remaining < (fm_entry->cfg_offset + fm_entry->cfg_count) * 105 sizeof(*fm_config)) { 106 dev_err(sdev->dev, "Invalid module cfg_offset %u\n", 107 fm_entry->cfg_offset); 108 return -EINVAL; 109 } 110 111 /* a module's config is always the same size */ 112 fw_module->bss_size = fm_config[fm_entry->cfg_offset].is_bytes; 113 114 dev_dbg(sdev->dev, 115 "module %s: UUID %pUL cfg_count: %u, bss_size: %#x\n", 116 fm_entry->name, &fm_entry->uuid, fm_entry->cfg_count, 117 fw_module->bss_size); 118 } else { 119 fw_module->bss_size = 0; 120 121 dev_dbg(sdev->dev, "module %s: UUID %pUL\n", fm_entry->name, 122 &fm_entry->uuid); 123 } 124 125 fw_module->man4_module_entry.id = i; 126 ida_init(&fw_module->m_ida); 127 fw_module->private = NULL; 128 129 fw_module++; 130 fm_entry++; 131 } 132 133 return ext_man_hdr->len; 134 } 135 136 static size_t sof_ipc4_fw_parse_basefw_ext_man(struct snd_sof_dev *sdev) 137 { 138 struct sof_ipc4_fw_data *ipc4_data = sdev->private; 139 struct sof_ipc4_fw_library *fw_lib; 140 size_t payload_offset; 141 int ret; 142 143 fw_lib = devm_kzalloc(sdev->dev, sizeof(*fw_lib), GFP_KERNEL); 144 if (!fw_lib) 145 return -ENOMEM; 146 147 fw_lib->sof_fw.fw = sdev->basefw.fw; 148 149 payload_offset = sof_ipc4_fw_parse_ext_man(sdev, fw_lib); 150 if (payload_offset > 0) { 151 fw_lib->sof_fw.payload_offset = payload_offset; 152 153 /* basefw ID is 0 */ 154 fw_lib->id = 0; 155 ret = xa_insert(&ipc4_data->fw_lib_xa, 0, fw_lib, GFP_KERNEL); 156 if (ret) 157 return ret; 158 } 159 160 return payload_offset; 161 } 162 163 struct sof_ipc4_fw_module *sof_ipc4_find_module_by_uuid(struct snd_sof_dev *sdev, 164 const guid_t *uuid) 165 { 166 struct sof_ipc4_fw_data *ipc4_data = sdev->private; 167 struct sof_ipc4_fw_library *fw_lib; 168 unsigned long lib_id; 169 int i; 170 171 if (guid_is_null(uuid)) 172 return NULL; 173 174 xa_for_each(&ipc4_data->fw_lib_xa, lib_id, fw_lib) { 175 for (i = 0; i < fw_lib->num_modules; i++) { 176 if (guid_equal(uuid, &fw_lib->modules[i].man4_module_entry.uuid)) 177 return &fw_lib->modules[i]; 178 } 179 } 180 181 return NULL; 182 } 183 184 static int sof_ipc4_validate_firmware(struct snd_sof_dev *sdev) 185 { 186 struct sof_ipc4_fw_data *ipc4_data = sdev->private; 187 u32 fw_hdr_offset = ipc4_data->manifest_fw_hdr_offset; 188 struct sof_man4_fw_binary_header *fw_header; 189 const struct firmware *fw = sdev->basefw.fw; 190 struct sof_ext_manifest4_hdr *ext_man_hdr; 191 192 ext_man_hdr = (struct sof_ext_manifest4_hdr *)fw->data; 193 fw_header = (struct sof_man4_fw_binary_header *) 194 (fw->data + ext_man_hdr->len + fw_hdr_offset); 195 196 /* TODO: Add firmware verification code here */ 197 198 dev_dbg(sdev->dev, "Validated firmware version: %u.%u.%u.%u\n", 199 fw_header->major_version, fw_header->minor_version, 200 fw_header->hotfix_version, fw_header->build_version); 201 202 return 0; 203 } 204 205 static int sof_ipc4_query_fw_configuration(struct snd_sof_dev *sdev) 206 { 207 struct sof_ipc4_fw_data *ipc4_data = sdev->private; 208 const struct sof_ipc_ops *iops = sdev->ipc->ops; 209 struct sof_ipc4_fw_version *fw_ver; 210 struct sof_ipc4_tuple *tuple; 211 struct sof_ipc4_msg msg; 212 size_t offset = 0; 213 int ret; 214 215 /* Get the firmware configuration */ 216 msg.primary = SOF_IPC4_MSG_TARGET(SOF_IPC4_MODULE_MSG); 217 msg.primary |= SOF_IPC4_MSG_DIR(SOF_IPC4_MSG_REQUEST); 218 msg.primary |= SOF_IPC4_MOD_ID(SOF_IPC4_MOD_INIT_BASEFW_MOD_ID); 219 msg.primary |= SOF_IPC4_MOD_INSTANCE(SOF_IPC4_MOD_INIT_BASEFW_INSTANCE_ID); 220 msg.extension = SOF_IPC4_MOD_EXT_MSG_PARAM_ID(SOF_IPC4_FW_PARAM_FW_CONFIG); 221 222 msg.data_size = sdev->ipc->max_payload_size; 223 msg.data_ptr = kzalloc(msg.data_size, GFP_KERNEL); 224 if (!msg.data_ptr) 225 return -ENOMEM; 226 227 ret = iops->set_get_data(sdev, &msg, msg.data_size, false); 228 if (ret) 229 goto out; 230 231 while (offset < msg.data_size) { 232 tuple = (struct sof_ipc4_tuple *)((u8 *)msg.data_ptr + offset); 233 234 switch (tuple->type) { 235 case SOF_IPC4_FW_CFG_FW_VERSION: 236 fw_ver = (struct sof_ipc4_fw_version *)tuple->value; 237 238 dev_info(sdev->dev, 239 "Booted firmware version: %u.%u.%u.%u\n", 240 fw_ver->major, fw_ver->minor, fw_ver->hotfix, 241 fw_ver->build); 242 break; 243 case SOF_IPC4_FW_CFG_DL_MAILBOX_BYTES: 244 trace_sof_ipc4_fw_config(sdev, "DL mailbox size", *tuple->value); 245 break; 246 case SOF_IPC4_FW_CFG_UL_MAILBOX_BYTES: 247 trace_sof_ipc4_fw_config(sdev, "UL mailbox size", *tuple->value); 248 break; 249 case SOF_IPC4_FW_CFG_TRACE_LOG_BYTES: 250 trace_sof_ipc4_fw_config(sdev, "Trace log size", *tuple->value); 251 ipc4_data->mtrace_log_bytes = *tuple->value; 252 break; 253 case SOF_IPC4_FW_CFG_MAX_LIBS_COUNT: 254 trace_sof_ipc4_fw_config(sdev, "maximum number of libraries", 255 *tuple->value); 256 ipc4_data->max_libs_count = *tuple->value; 257 if (!ipc4_data->max_libs_count) 258 ipc4_data->max_libs_count = 1; 259 break; 260 default: 261 break; 262 } 263 264 offset += sizeof(*tuple) + tuple->size; 265 } 266 267 out: 268 kfree(msg.data_ptr); 269 270 return ret; 271 } 272 273 const struct sof_ipc_fw_loader_ops ipc4_loader_ops = { 274 .validate = sof_ipc4_validate_firmware, 275 .parse_ext_manifest = sof_ipc4_fw_parse_basefw_ext_man, 276 .query_fw_configuration = sof_ipc4_query_fw_configuration, 277 }; 278