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 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 /* The module ID includes the id of the library it is part of at offset 12 */ 18 #define SOF_IPC4_MOD_LIB_ID_SHIFT 12 19 20 static ssize_t sof_ipc4_fw_parse_ext_man(struct snd_sof_dev *sdev, 21 struct sof_ipc4_fw_library *fw_lib) 22 { 23 struct sof_ipc4_fw_data *ipc4_data = sdev->private; 24 const struct firmware *fw = fw_lib->sof_fw.fw; 25 struct sof_man4_fw_binary_header *fw_header; 26 struct sof_ext_manifest4_hdr *ext_man_hdr; 27 struct sof_man4_module_config *fm_config; 28 struct sof_ipc4_fw_module *fw_module; 29 struct sof_man4_module *fm_entry; 30 ssize_t remaining; 31 u32 fw_hdr_offset; 32 int i; 33 34 if (!ipc4_data) { 35 dev_err(sdev->dev, "%s: ipc4_data is not available\n", __func__); 36 return -EINVAL; 37 } 38 39 remaining = fw->size; 40 if (remaining <= sizeof(*ext_man_hdr)) { 41 dev_err(sdev->dev, "Firmware size is too small: %zu\n", remaining); 42 return -EINVAL; 43 } 44 45 ext_man_hdr = (struct sof_ext_manifest4_hdr *)fw->data; 46 47 /* 48 * At the start of the firmware image we must have an extended manifest. 49 * Verify that the magic number is correct. 50 */ 51 if (ext_man_hdr->id != SOF_EXT_MAN4_MAGIC_NUMBER) { 52 dev_err(sdev->dev, 53 "Unexpected extended manifest magic number: %#x\n", 54 ext_man_hdr->id); 55 return -EINVAL; 56 } 57 58 fw_hdr_offset = ipc4_data->manifest_fw_hdr_offset; 59 if (!fw_hdr_offset) 60 return -EINVAL; 61 62 if (remaining <= ext_man_hdr->len + fw_hdr_offset + sizeof(*fw_header)) { 63 dev_err(sdev->dev, "Invalid firmware size %zu, should be at least %zu\n", 64 remaining, ext_man_hdr->len + fw_hdr_offset + sizeof(*fw_header)); 65 return -EINVAL; 66 } 67 68 fw_header = (struct sof_man4_fw_binary_header *) 69 (fw->data + ext_man_hdr->len + fw_hdr_offset); 70 remaining -= (ext_man_hdr->len + fw_hdr_offset); 71 72 if (remaining <= fw_header->len) { 73 dev_err(sdev->dev, "Invalid fw_header->len %u\n", fw_header->len); 74 return -EINVAL; 75 } 76 77 dev_info(sdev->dev, "Loaded firmware library: %s, version: %u.%u.%u.%u\n", 78 fw_header->name, fw_header->major_version, fw_header->minor_version, 79 fw_header->hotfix_version, fw_header->build_version); 80 dev_dbg(sdev->dev, "Header length: %u, module count: %u\n", 81 fw_header->len, fw_header->num_module_entries); 82 83 /* copy the fw_version of basefw into debugfs at first boot */ 84 if (fw == sdev->basefw.fw) { 85 sdev->fw_version.major = fw_header->major_version; 86 sdev->fw_version.minor = fw_header->minor_version; 87 sdev->fw_version.micro = fw_header->hotfix_version; 88 sdev->fw_version.build = fw_header->build_version; 89 } 90 91 fw_lib->modules = devm_kmalloc_array(sdev->dev, fw_header->num_module_entries, 92 sizeof(*fw_module), GFP_KERNEL); 93 if (!fw_lib->modules) 94 return -ENOMEM; 95 96 fw_lib->name = fw_header->name; 97 fw_lib->num_modules = fw_header->num_module_entries; 98 fw_module = fw_lib->modules; 99 100 fm_entry = (struct sof_man4_module *)((u8 *)fw_header + fw_header->len); 101 remaining -= fw_header->len; 102 103 if (remaining < fw_header->num_module_entries * sizeof(*fm_entry)) { 104 dev_err(sdev->dev, "Invalid num_module_entries %u\n", 105 fw_header->num_module_entries); 106 return -EINVAL; 107 } 108 109 fm_config = (struct sof_man4_module_config *) 110 (fm_entry + fw_header->num_module_entries); 111 remaining -= (fw_header->num_module_entries * sizeof(*fm_entry)); 112 for (i = 0; i < fw_header->num_module_entries; i++) { 113 memcpy(&fw_module->man4_module_entry, fm_entry, sizeof(*fm_entry)); 114 115 if (fm_entry->cfg_count) { 116 if (remaining < (fm_entry->cfg_offset + fm_entry->cfg_count) * 117 sizeof(*fm_config)) { 118 dev_err(sdev->dev, "Invalid module cfg_offset %u\n", 119 fm_entry->cfg_offset); 120 return -EINVAL; 121 } 122 123 fw_module->fw_mod_cfg = &fm_config[fm_entry->cfg_offset]; 124 125 dev_dbg(sdev->dev, 126 "module %s: UUID %pUL cfg_count: %u, bss_size: %#x\n", 127 fm_entry->name, &fm_entry->uuid, fm_entry->cfg_count, 128 fm_config[fm_entry->cfg_offset].is_bytes); 129 } else { 130 dev_dbg(sdev->dev, "module %s: UUID %pUL\n", fm_entry->name, 131 &fm_entry->uuid); 132 } 133 134 fw_module->man4_module_entry.id = i; 135 ida_init(&fw_module->m_ida); 136 fw_module->private = NULL; 137 138 fw_module++; 139 fm_entry++; 140 } 141 142 return ext_man_hdr->len; 143 } 144 145 static size_t sof_ipc4_fw_parse_basefw_ext_man(struct snd_sof_dev *sdev) 146 { 147 struct sof_ipc4_fw_data *ipc4_data = sdev->private; 148 struct sof_ipc4_fw_library *fw_lib; 149 ssize_t payload_offset; 150 int ret; 151 152 fw_lib = devm_kzalloc(sdev->dev, sizeof(*fw_lib), GFP_KERNEL); 153 if (!fw_lib) 154 return -ENOMEM; 155 156 fw_lib->sof_fw.fw = sdev->basefw.fw; 157 158 payload_offset = sof_ipc4_fw_parse_ext_man(sdev, fw_lib); 159 if (payload_offset > 0) { 160 fw_lib->sof_fw.payload_offset = payload_offset; 161 162 /* basefw ID is 0 */ 163 fw_lib->id = 0; 164 ret = xa_insert(&ipc4_data->fw_lib_xa, 0, fw_lib, GFP_KERNEL); 165 if (ret) 166 return ret; 167 } 168 169 return payload_offset; 170 } 171 172 static int sof_ipc4_load_library_by_uuid(struct snd_sof_dev *sdev, 173 unsigned long lib_id, const guid_t *uuid) 174 { 175 struct sof_ipc4_fw_data *ipc4_data = sdev->private; 176 struct sof_ipc4_fw_library *fw_lib; 177 const char *fw_filename; 178 ssize_t payload_offset; 179 int ret, i, err; 180 181 if (!sdev->pdata->fw_lib_prefix) { 182 dev_err(sdev->dev, 183 "Library loading is not supported due to not set library path\n"); 184 return -EINVAL; 185 } 186 187 if (!ipc4_data->load_library) { 188 dev_err(sdev->dev, "Library loading is not supported on this platform\n"); 189 return -EOPNOTSUPP; 190 } 191 192 fw_lib = devm_kzalloc(sdev->dev, sizeof(*fw_lib), GFP_KERNEL); 193 if (!fw_lib) 194 return -ENOMEM; 195 196 fw_filename = kasprintf(GFP_KERNEL, "%s/%pUL.bin", 197 sdev->pdata->fw_lib_prefix, uuid); 198 if (!fw_filename) { 199 ret = -ENOMEM; 200 goto free_fw_lib; 201 } 202 203 ret = request_firmware(&fw_lib->sof_fw.fw, fw_filename, sdev->dev); 204 if (ret < 0) { 205 dev_err(sdev->dev, "Library file '%s' is missing\n", fw_filename); 206 goto free_filename; 207 } else { 208 dev_dbg(sdev->dev, "Library file '%s' loaded\n", fw_filename); 209 } 210 211 payload_offset = sof_ipc4_fw_parse_ext_man(sdev, fw_lib); 212 if (payload_offset <= 0) { 213 if (!payload_offset) 214 ret = -EINVAL; 215 else 216 ret = payload_offset; 217 218 goto release; 219 } 220 221 fw_lib->sof_fw.payload_offset = payload_offset; 222 fw_lib->id = lib_id; 223 224 /* Fix up the module ID numbers within the library */ 225 for (i = 0; i < fw_lib->num_modules; i++) 226 fw_lib->modules[i].man4_module_entry.id |= (lib_id << SOF_IPC4_MOD_LIB_ID_SHIFT); 227 228 /* 229 * Make sure that the DSP is booted and stays up while attempting the 230 * loading the library for the first time 231 */ 232 ret = pm_runtime_resume_and_get(sdev->dev); 233 if (ret < 0 && ret != -EACCES) { 234 dev_err_ratelimited(sdev->dev, "%s: pm_runtime resume failed: %d\n", 235 __func__, ret); 236 goto release; 237 } 238 239 ret = ipc4_data->load_library(sdev, fw_lib, false); 240 241 pm_runtime_mark_last_busy(sdev->dev); 242 err = pm_runtime_put_autosuspend(sdev->dev); 243 if (err < 0) 244 dev_err_ratelimited(sdev->dev, "%s: pm_runtime idle failed: %d\n", 245 __func__, err); 246 247 if (ret) 248 goto release; 249 250 ret = xa_insert(&ipc4_data->fw_lib_xa, lib_id, fw_lib, GFP_KERNEL); 251 if (unlikely(ret)) 252 goto release; 253 254 kfree(fw_filename); 255 256 return 0; 257 258 release: 259 release_firmware(fw_lib->sof_fw.fw); 260 /* Allocated within sof_ipc4_fw_parse_ext_man() */ 261 devm_kfree(sdev->dev, fw_lib->modules); 262 free_filename: 263 kfree(fw_filename); 264 free_fw_lib: 265 devm_kfree(sdev->dev, fw_lib); 266 267 return ret; 268 } 269 270 struct sof_ipc4_fw_module *sof_ipc4_find_module_by_uuid(struct snd_sof_dev *sdev, 271 const guid_t *uuid) 272 { 273 struct sof_ipc4_fw_data *ipc4_data = sdev->private; 274 struct sof_ipc4_fw_library *fw_lib; 275 unsigned long lib_id; 276 int i, ret; 277 278 if (guid_is_null(uuid)) 279 return NULL; 280 281 xa_for_each(&ipc4_data->fw_lib_xa, lib_id, fw_lib) { 282 for (i = 0; i < fw_lib->num_modules; i++) { 283 if (guid_equal(uuid, &fw_lib->modules[i].man4_module_entry.uuid)) 284 return &fw_lib->modules[i]; 285 } 286 } 287 288 /* 289 * Do not attempt to load external library in case the maximum number of 290 * firmware libraries have been already loaded 291 */ 292 if ((lib_id + 1) == ipc4_data->max_libs_count) { 293 dev_err(sdev->dev, 294 "%s: Maximum allowed number of libraries reached (%u)\n", 295 __func__, ipc4_data->max_libs_count); 296 return NULL; 297 } 298 299 /* The module cannot be found, try to load it as a library */ 300 ret = sof_ipc4_load_library_by_uuid(sdev, lib_id + 1, uuid); 301 if (ret) 302 return NULL; 303 304 /* Look for the module in the newly loaded library, it should be available now */ 305 xa_for_each_start(&ipc4_data->fw_lib_xa, lib_id, fw_lib, lib_id) { 306 for (i = 0; i < fw_lib->num_modules; i++) { 307 if (guid_equal(uuid, &fw_lib->modules[i].man4_module_entry.uuid)) 308 return &fw_lib->modules[i]; 309 } 310 } 311 312 return NULL; 313 } 314 315 static int sof_ipc4_validate_firmware(struct snd_sof_dev *sdev) 316 { 317 struct sof_ipc4_fw_data *ipc4_data = sdev->private; 318 u32 fw_hdr_offset = ipc4_data->manifest_fw_hdr_offset; 319 struct sof_man4_fw_binary_header *fw_header; 320 const struct firmware *fw = sdev->basefw.fw; 321 struct sof_ext_manifest4_hdr *ext_man_hdr; 322 323 ext_man_hdr = (struct sof_ext_manifest4_hdr *)fw->data; 324 fw_header = (struct sof_man4_fw_binary_header *) 325 (fw->data + ext_man_hdr->len + fw_hdr_offset); 326 327 /* TODO: Add firmware verification code here */ 328 329 dev_dbg(sdev->dev, "Validated firmware version: %u.%u.%u.%u\n", 330 fw_header->major_version, fw_header->minor_version, 331 fw_header->hotfix_version, fw_header->build_version); 332 333 return 0; 334 } 335 336 int sof_ipc4_query_fw_configuration(struct snd_sof_dev *sdev) 337 { 338 struct sof_ipc4_fw_data *ipc4_data = sdev->private; 339 const struct sof_ipc_ops *iops = sdev->ipc->ops; 340 struct sof_ipc4_fw_version *fw_ver; 341 struct sof_ipc4_tuple *tuple; 342 struct sof_ipc4_msg msg; 343 size_t offset = 0; 344 int ret; 345 346 /* Get the firmware configuration */ 347 msg.primary = SOF_IPC4_MSG_TARGET(SOF_IPC4_MODULE_MSG); 348 msg.primary |= SOF_IPC4_MSG_DIR(SOF_IPC4_MSG_REQUEST); 349 msg.primary |= SOF_IPC4_MOD_ID(SOF_IPC4_MOD_INIT_BASEFW_MOD_ID); 350 msg.primary |= SOF_IPC4_MOD_INSTANCE(SOF_IPC4_MOD_INIT_BASEFW_INSTANCE_ID); 351 msg.extension = SOF_IPC4_MOD_EXT_MSG_PARAM_ID(SOF_IPC4_FW_PARAM_FW_CONFIG); 352 353 msg.data_size = sdev->ipc->max_payload_size; 354 msg.data_ptr = kzalloc(msg.data_size, GFP_KERNEL); 355 if (!msg.data_ptr) 356 return -ENOMEM; 357 358 ret = iops->set_get_data(sdev, &msg, msg.data_size, false); 359 if (ret) 360 goto out; 361 362 while (offset < msg.data_size) { 363 tuple = (struct sof_ipc4_tuple *)((u8 *)msg.data_ptr + offset); 364 365 switch (tuple->type) { 366 case SOF_IPC4_FW_CFG_FW_VERSION: 367 fw_ver = (struct sof_ipc4_fw_version *)tuple->value; 368 369 dev_info(sdev->dev, 370 "Booted firmware version: %u.%u.%u.%u\n", 371 fw_ver->major, fw_ver->minor, fw_ver->hotfix, 372 fw_ver->build); 373 break; 374 case SOF_IPC4_FW_CFG_DL_MAILBOX_BYTES: 375 trace_sof_ipc4_fw_config(sdev, "DL mailbox size", *tuple->value); 376 break; 377 case SOF_IPC4_FW_CFG_UL_MAILBOX_BYTES: 378 trace_sof_ipc4_fw_config(sdev, "UL mailbox size", *tuple->value); 379 break; 380 case SOF_IPC4_FW_CFG_TRACE_LOG_BYTES: 381 trace_sof_ipc4_fw_config(sdev, "Trace log size", *tuple->value); 382 ipc4_data->mtrace_log_bytes = *tuple->value; 383 break; 384 case SOF_IPC4_FW_CFG_MAX_LIBS_COUNT: 385 trace_sof_ipc4_fw_config(sdev, "maximum number of libraries", 386 *tuple->value); 387 ipc4_data->max_libs_count = *tuple->value; 388 if (!ipc4_data->max_libs_count) 389 ipc4_data->max_libs_count = 1; 390 break; 391 case SOF_IPC4_FW_CFG_MAX_PPL_COUNT: 392 ipc4_data->max_num_pipelines = *tuple->value; 393 trace_sof_ipc4_fw_config(sdev, "Max PPL count %d", 394 ipc4_data->max_num_pipelines); 395 if (ipc4_data->max_num_pipelines <= 0) { 396 dev_err(sdev->dev, "Invalid max_num_pipelines %d", 397 ipc4_data->max_num_pipelines); 398 ret = -EINVAL; 399 goto out; 400 } 401 break; 402 case SOF_IPC4_FW_CONTEXT_SAVE: 403 ipc4_data->fw_context_save = *tuple->value; 404 break; 405 default: 406 break; 407 } 408 409 offset += sizeof(*tuple) + tuple->size; 410 } 411 412 out: 413 kfree(msg.data_ptr); 414 415 return ret; 416 } 417 418 int sof_ipc4_reload_fw_libraries(struct snd_sof_dev *sdev) 419 { 420 struct sof_ipc4_fw_data *ipc4_data = sdev->private; 421 struct sof_ipc4_fw_library *fw_lib; 422 unsigned long lib_id; 423 int ret = 0; 424 425 xa_for_each_start(&ipc4_data->fw_lib_xa, lib_id, fw_lib, 1) { 426 ret = ipc4_data->load_library(sdev, fw_lib, true); 427 if (ret) { 428 dev_err(sdev->dev, "%s: Failed to reload library: %s, %d\n", 429 __func__, fw_lib->name, ret); 430 break; 431 } 432 } 433 434 return ret; 435 } 436 437 /** 438 * sof_ipc4_update_cpc_from_manifest - Update the cpc in base config from manifest 439 * @sdev: SOF device 440 * @fw_module: pointer struct sof_ipc4_fw_module to parse 441 * @basecfg: Pointer to the base_config to update 442 */ 443 void sof_ipc4_update_cpc_from_manifest(struct snd_sof_dev *sdev, 444 struct sof_ipc4_fw_module *fw_module, 445 struct sof_ipc4_base_module_cfg *basecfg) 446 { 447 const struct sof_man4_module_config *fw_mod_cfg; 448 u32 cpc_pick = 0; 449 u32 max_cpc = 0; 450 const char *msg; 451 int i; 452 453 if (!fw_module->fw_mod_cfg) { 454 msg = "No mod_cfg available for CPC lookup in the firmware file's manifest"; 455 goto no_cpc; 456 } 457 458 /* 459 * Find the best matching (highest) CPC value based on the module's 460 * IBS/OBS configuration inferred from the audio format selection. 461 * 462 * The CPC value in each module config entry has been measured and 463 * recorded as a IBS/OBS/CPC triplet and stored in the firmware file's 464 * manifest 465 */ 466 fw_mod_cfg = fw_module->fw_mod_cfg; 467 for (i = 0; i < fw_module->man4_module_entry.cfg_count; i++) { 468 if (basecfg->obs == fw_mod_cfg[i].obs && 469 basecfg->ibs == fw_mod_cfg[i].ibs && 470 cpc_pick < fw_mod_cfg[i].cpc) 471 cpc_pick = fw_mod_cfg[i].cpc; 472 473 if (max_cpc < fw_mod_cfg[i].cpc) 474 max_cpc = fw_mod_cfg[i].cpc; 475 } 476 477 basecfg->cpc = cpc_pick; 478 479 /* We have a matching configuration for CPC */ 480 if (basecfg->cpc) 481 return; 482 483 /* 484 * No matching IBS/OBS found, the firmware manifest is missing 485 * information in the module's module configuration table. 486 */ 487 if (!max_cpc) 488 msg = "No CPC value available in the firmware file's manifest"; 489 else if (!cpc_pick) 490 msg = "No CPC match in the firmware file's manifest"; 491 492 no_cpc: 493 dev_dbg(sdev->dev, "%s (UUID: %pUL): %s (ibs/obs: %u/%u)\n", 494 fw_module->man4_module_entry.name, 495 &fw_module->man4_module_entry.uuid, msg, basecfg->ibs, 496 basecfg->obs); 497 } 498 499 const struct sof_ipc_fw_loader_ops ipc4_loader_ops = { 500 .validate = sof_ipc4_validate_firmware, 501 .parse_ext_manifest = sof_ipc4_fw_parse_basefw_ext_man, 502 }; 503