xref: /linux/sound/soc/sof/ipc4-loader.c (revision 177bf8620cf4ed290ee170a6c5966adc0924b336)
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 
sof_ipc4_fw_parse_ext_man(struct snd_sof_dev * sdev,struct sof_ipc4_fw_library * fw_lib)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 
sof_ipc4_fw_parse_basefw_ext_man(struct snd_sof_dev * sdev)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 
sof_ipc4_load_library(struct snd_sof_dev * sdev,unsigned long lib_id,const char * lib_filename,bool optional)172 static int sof_ipc4_load_library(struct snd_sof_dev *sdev, unsigned long lib_id,
173 				 const char *lib_filename, bool optional)
174 {
175 	struct sof_ipc4_fw_data *ipc4_data = sdev->private;
176 	struct sof_ipc4_fw_library *fw_lib;
177 	ssize_t payload_offset;
178 	int ret, i, err;
179 
180 	if (!ipc4_data->load_library) {
181 		dev_err(sdev->dev, "Library loading is not supported on this platform\n");
182 		return -EOPNOTSUPP;
183 	}
184 
185 	fw_lib = devm_kzalloc(sdev->dev, sizeof(*fw_lib), GFP_KERNEL);
186 	if (!fw_lib)
187 		return -ENOMEM;
188 
189 	if (optional) {
190 		ret = firmware_request_nowarn(&fw_lib->sof_fw.fw, lib_filename,
191 					      sdev->dev);
192 		if (ret < 0) {
193 			/* optional library, override the error */
194 			ret = 0;
195 			goto free_fw_lib;
196 		}
197 	} else {
198 		ret = request_firmware(&fw_lib->sof_fw.fw, lib_filename,
199 				       sdev->dev);
200 		if (ret < 0) {
201 			dev_err(sdev->dev, "Library file '%s' is missing\n",
202 				lib_filename);
203 			goto free_fw_lib;
204 		}
205 	}
206 
207 	dev_dbg(sdev->dev, "Library file '%s' loaded\n", lib_filename);
208 
209 	payload_offset = sof_ipc4_fw_parse_ext_man(sdev, fw_lib);
210 	if (payload_offset <= 0) {
211 		if (!payload_offset)
212 			ret = -EINVAL;
213 		else
214 			ret = payload_offset;
215 
216 		goto release;
217 	}
218 
219 	fw_lib->sof_fw.payload_offset = payload_offset;
220 	fw_lib->id = lib_id;
221 
222 	/* Fix up the module ID numbers within the library */
223 	for (i = 0; i < fw_lib->num_modules; i++)
224 		fw_lib->modules[i].man4_module_entry.id |= (lib_id << SOF_IPC4_MOD_LIB_ID_SHIFT);
225 
226 	/*
227 	 * Make sure that the DSP is booted and stays up while attempting the
228 	 * loading the library for the first time
229 	 */
230 	ret = pm_runtime_resume_and_get(sdev->dev);
231 	if (ret < 0 && ret != -EACCES) {
232 		dev_err_ratelimited(sdev->dev, "%s: pm_runtime resume failed: %d\n",
233 				    __func__, ret);
234 		goto release;
235 	}
236 
237 	ret = ipc4_data->load_library(sdev, fw_lib, false);
238 
239 	err = pm_runtime_put_autosuspend(sdev->dev);
240 	if (err < 0)
241 		dev_err_ratelimited(sdev->dev, "%s: pm_runtime idle failed: %d\n",
242 				    __func__, err);
243 
244 	if (ret)
245 		goto release;
246 
247 	ret = xa_insert(&ipc4_data->fw_lib_xa, lib_id, fw_lib, GFP_KERNEL);
248 	if (unlikely(ret))
249 		goto release;
250 
251 	return 0;
252 
253 release:
254 	release_firmware(fw_lib->sof_fw.fw);
255 	/* Allocated within sof_ipc4_fw_parse_ext_man() */
256 	devm_kfree(sdev->dev, fw_lib->modules);
257 free_fw_lib:
258 	devm_kfree(sdev->dev, fw_lib);
259 
260 	return ret;
261 }
262 
263 /**
264  * sof_ipc4_complete_split_release - loads the library parts of a split firmware
265  * @sdev: SOF device
266  *
267  * With IPC4 the firmware can be a single binary or a split release.
268  * - single binary: only the basefw
269  * - split release: basefw and two libraries (openmodules, debug)
270  *
271  * With split firmware release it is also allowed that for example only the
272  * debug library is present (the openmodules content is built in the basefw).
273  *
274  * To handle the permutations try to load the openmodules then the debug
275  * libraries as optional ones after the basefw boot.
276  *
277  * The libraries for the split release are stored alongside the basefw on the
278  * filesystem.
279  */
sof_ipc4_complete_split_release(struct snd_sof_dev * sdev)280 int sof_ipc4_complete_split_release(struct snd_sof_dev *sdev)
281 {
282 	static const char * const lib_bundle[] = { "openmodules", "debug" };
283 	const char *fw_filename = sdev->pdata->fw_filename;
284 	const char *lib_filename, *p;
285 	size_t lib_name_base_size;
286 	unsigned long lib_id = 1;
287 	char *lib_name_base;
288 	int i;
289 
290 	p = strstr(fw_filename, ".ri");
291 	if (!p || strlen(p) != 3) {
292 		dev_info(sdev->dev,
293 			 "%s: Firmware name '%s' is missing .ri extension\n",
294 			 __func__, fw_filename);
295 		return 0;
296 	}
297 
298 	/* Space for the firmware basename + '\0', without the extension */
299 	lib_name_base_size = strlen(fw_filename) - 2;
300 	lib_name_base = kzalloc(lib_name_base_size, GFP_KERNEL);
301 	if (!lib_name_base)
302 		return -ENOMEM;
303 
304 	/*
305 	 * strscpy will 0 terminate the copied string, removing the '.ri' from
306 	 * the end of the fw_filename, for example:
307 	 * fw_filename:		"sof-ptl.ri\0"
308 	 * lib_name_base:	"sof-ptl\0"
309 	 */
310 	strscpy(lib_name_base, fw_filename, lib_name_base_size);
311 
312 	for (i = 0; i < ARRAY_SIZE(lib_bundle); i++) {
313 		int ret;
314 
315 		lib_filename = kasprintf(GFP_KERNEL, "%s/%s-%s.ri",
316 					 sdev->pdata->fw_filename_prefix,
317 					 lib_name_base, lib_bundle[i]);
318 		if (!lib_filename) {
319 			kfree(lib_name_base);
320 			return -ENOMEM;
321 		}
322 
323 		ret = sof_ipc4_load_library(sdev, lib_id, lib_filename, true);
324 		if (ret)
325 			dev_warn(sdev->dev, "%s: Failed to load %s: %d\n",
326 				 __func__, lib_filename, ret);
327 		else
328 			lib_id++;
329 
330 		kfree(lib_filename);
331 	}
332 
333 	kfree(lib_name_base);
334 
335 	return 0;
336 }
337 
sof_ipc4_load_library_by_uuid(struct snd_sof_dev * sdev,unsigned long lib_id,const guid_t * uuid)338 static int sof_ipc4_load_library_by_uuid(struct snd_sof_dev *sdev,
339 					 unsigned long lib_id, const guid_t *uuid)
340 {
341 	const char *lib_filename;
342 	int ret;
343 
344 	if (!sdev->pdata->fw_lib_prefix) {
345 		dev_err(sdev->dev,
346 			"Library loading is not supported due to not set library path\n");
347 		return -EINVAL;
348 	}
349 
350 	lib_filename = kasprintf(GFP_KERNEL, "%s/%pUL.bin",
351 				 sdev->pdata->fw_lib_prefix, uuid);
352 	if (!lib_filename)
353 		return -ENOMEM;
354 
355 	ret = sof_ipc4_load_library(sdev, lib_id, lib_filename, false);
356 
357 	kfree(lib_filename);
358 
359 	return ret;
360 }
361 
sof_ipc4_find_module_by_uuid(struct snd_sof_dev * sdev,const guid_t * uuid)362 struct sof_ipc4_fw_module *sof_ipc4_find_module_by_uuid(struct snd_sof_dev *sdev,
363 							const guid_t *uuid)
364 {
365 	struct sof_ipc4_fw_data *ipc4_data = sdev->private;
366 	struct sof_ipc4_fw_library *fw_lib;
367 	unsigned long lib_id;
368 	int i, ret;
369 
370 	if (guid_is_null(uuid))
371 		return NULL;
372 
373 	xa_for_each(&ipc4_data->fw_lib_xa, lib_id, fw_lib) {
374 		for (i = 0; i < fw_lib->num_modules; i++) {
375 			if (guid_equal(uuid, &fw_lib->modules[i].man4_module_entry.uuid))
376 				return &fw_lib->modules[i];
377 		}
378 	}
379 
380 	/*
381 	 * Do not attempt to load external library in case the maximum number of
382 	 * firmware libraries have been already loaded
383 	 */
384 	if ((lib_id + 1) == ipc4_data->max_libs_count) {
385 		dev_err(sdev->dev,
386 			"%s: Maximum allowed number of libraries reached (%u)\n",
387 			__func__, ipc4_data->max_libs_count);
388 		return NULL;
389 	}
390 
391 	/* The module cannot be found, try to load it as a library */
392 	ret = sof_ipc4_load_library_by_uuid(sdev, lib_id + 1, uuid);
393 	if (ret)
394 		return NULL;
395 
396 	/* Look for the module in the newly loaded library, it should be available now */
397 	xa_for_each_start(&ipc4_data->fw_lib_xa, lib_id, fw_lib, lib_id) {
398 		for (i = 0; i < fw_lib->num_modules; i++) {
399 			if (guid_equal(uuid, &fw_lib->modules[i].man4_module_entry.uuid))
400 				return &fw_lib->modules[i];
401 		}
402 	}
403 
404 	return NULL;
405 }
406 
sof_ipc4_validate_firmware(struct snd_sof_dev * sdev)407 static int sof_ipc4_validate_firmware(struct snd_sof_dev *sdev)
408 {
409 	struct sof_ipc4_fw_data *ipc4_data = sdev->private;
410 	u32 fw_hdr_offset = ipc4_data->manifest_fw_hdr_offset;
411 	struct sof_man4_fw_binary_header *fw_header;
412 	const struct firmware *fw = sdev->basefw.fw;
413 	struct sof_ext_manifest4_hdr *ext_man_hdr;
414 
415 	ext_man_hdr = (struct sof_ext_manifest4_hdr *)fw->data;
416 	fw_header = (struct sof_man4_fw_binary_header *)
417 				(fw->data + ext_man_hdr->len + fw_hdr_offset);
418 
419 	/* TODO: Add firmware verification code here */
420 
421 	dev_dbg(sdev->dev, "Validated firmware version: %u.%u.%u.%u\n",
422 		fw_header->major_version, fw_header->minor_version,
423 		fw_header->hotfix_version, fw_header->build_version);
424 
425 	return 0;
426 }
427 
sof_ipc4_query_fw_configuration(struct snd_sof_dev * sdev)428 int sof_ipc4_query_fw_configuration(struct snd_sof_dev *sdev)
429 {
430 	struct sof_ipc4_fw_data *ipc4_data = sdev->private;
431 	const struct sof_ipc_ops *iops = sdev->ipc->ops;
432 	struct sof_ipc4_fw_version *fw_ver;
433 	struct sof_ipc4_tuple *tuple;
434 	struct sof_ipc4_msg msg;
435 	size_t offset = 0;
436 	int ret;
437 
438 	/* Get the firmware configuration */
439 	msg.primary = SOF_IPC4_MSG_TARGET(SOF_IPC4_MODULE_MSG);
440 	msg.primary |= SOF_IPC4_MSG_DIR(SOF_IPC4_MSG_REQUEST);
441 	msg.primary |= SOF_IPC4_MOD_ID(SOF_IPC4_MOD_INIT_BASEFW_MOD_ID);
442 	msg.primary |= SOF_IPC4_MOD_INSTANCE(SOF_IPC4_MOD_INIT_BASEFW_INSTANCE_ID);
443 	msg.extension = SOF_IPC4_MOD_EXT_MSG_PARAM_ID(SOF_IPC4_FW_PARAM_FW_CONFIG);
444 
445 	msg.data_size = sdev->ipc->max_payload_size;
446 	msg.data_ptr = kzalloc(msg.data_size, GFP_KERNEL);
447 	if (!msg.data_ptr)
448 		return -ENOMEM;
449 
450 	ret = iops->set_get_data(sdev, &msg, msg.data_size, false);
451 	if (ret)
452 		goto out;
453 
454 	while (offset < msg.data_size) {
455 		tuple = (struct sof_ipc4_tuple *)((u8 *)msg.data_ptr + offset);
456 
457 		switch (tuple->type) {
458 		case SOF_IPC4_FW_CFG_FW_VERSION:
459 			fw_ver = (struct sof_ipc4_fw_version *)tuple->value;
460 
461 			dev_info(sdev->dev,
462 				 "Booted firmware version: %u.%u.%u.%u\n",
463 				 fw_ver->major, fw_ver->minor, fw_ver->hotfix,
464 				 fw_ver->build);
465 			break;
466 		case SOF_IPC4_FW_CFG_DL_MAILBOX_BYTES:
467 			trace_sof_ipc4_fw_config(sdev, "DL mailbox size", *tuple->value);
468 			break;
469 		case SOF_IPC4_FW_CFG_UL_MAILBOX_BYTES:
470 			trace_sof_ipc4_fw_config(sdev, "UL mailbox size", *tuple->value);
471 			break;
472 		case SOF_IPC4_FW_CFG_TRACE_LOG_BYTES:
473 			trace_sof_ipc4_fw_config(sdev, "Trace log size", *tuple->value);
474 			ipc4_data->mtrace_log_bytes = *tuple->value;
475 			break;
476 		case SOF_IPC4_FW_CFG_MAX_LIBS_COUNT:
477 			trace_sof_ipc4_fw_config(sdev, "maximum number of libraries",
478 						 *tuple->value);
479 			ipc4_data->max_libs_count = *tuple->value;
480 			if (!ipc4_data->max_libs_count)
481 				ipc4_data->max_libs_count = 1;
482 			break;
483 		case SOF_IPC4_FW_CFG_MAX_PPL_COUNT:
484 			ipc4_data->max_num_pipelines = *tuple->value;
485 			trace_sof_ipc4_fw_config(sdev, "Max PPL count %d",
486 						 ipc4_data->max_num_pipelines);
487 			if (ipc4_data->max_num_pipelines <= 0) {
488 				dev_err(sdev->dev, "Invalid max_num_pipelines %d",
489 					ipc4_data->max_num_pipelines);
490 				ret = -EINVAL;
491 				goto out;
492 			}
493 			break;
494 		case SOF_IPC4_FW_CONTEXT_SAVE:
495 			ipc4_data->fw_context_save = *tuple->value;
496 			/*
497 			 * Set the default libraries_restored value - if full
498 			 * context save is supported then it means that
499 			 * libraries are restored
500 			 */
501 			ipc4_data->libraries_restored = ipc4_data->fw_context_save;
502 			break;
503 		default:
504 			break;
505 		}
506 
507 		offset += sizeof(*tuple) + tuple->size;
508 	}
509 
510 	/* Get the hardware configuration */
511 	msg.primary = SOF_IPC4_MSG_TARGET(SOF_IPC4_MODULE_MSG);
512 	msg.primary |= SOF_IPC4_MSG_DIR(SOF_IPC4_MSG_REQUEST);
513 	msg.primary |= SOF_IPC4_MOD_ID(SOF_IPC4_MOD_INIT_BASEFW_MOD_ID);
514 	msg.primary |= SOF_IPC4_MOD_INSTANCE(SOF_IPC4_MOD_INIT_BASEFW_INSTANCE_ID);
515 	msg.extension = SOF_IPC4_MOD_EXT_MSG_PARAM_ID(SOF_IPC4_FW_PARAM_HW_CONFIG_GET);
516 
517 	msg.data_size = sdev->ipc->max_payload_size;
518 
519 	ret = iops->set_get_data(sdev, &msg, msg.data_size, false);
520 	if (ret)
521 		goto out;
522 
523 	offset = 0;
524 	while (offset < msg.data_size) {
525 		tuple = (struct sof_ipc4_tuple *)((u8 *)msg.data_ptr + offset);
526 
527 		switch (tuple->type) {
528 		case SOF_IPC4_HW_CFG_INTEL_MIC_PRIVACY_CAPS:
529 			if (ipc4_data->intel_configure_mic_privacy) {
530 				struct sof_ipc4_intel_mic_privacy_cap *caps;
531 
532 				caps = (struct sof_ipc4_intel_mic_privacy_cap *)tuple->value;
533 				ipc4_data->intel_configure_mic_privacy(sdev, caps);
534 			}
535 			break;
536 		default:
537 			break;
538 		}
539 
540 		offset += sizeof(*tuple) + tuple->size;
541 	}
542 
543 out:
544 	kfree(msg.data_ptr);
545 
546 	return ret;
547 }
548 
sof_ipc4_reload_fw_libraries(struct snd_sof_dev * sdev)549 int sof_ipc4_reload_fw_libraries(struct snd_sof_dev *sdev)
550 {
551 	struct sof_ipc4_fw_data *ipc4_data = sdev->private;
552 	struct sof_ipc4_fw_library *fw_lib;
553 	unsigned long lib_id;
554 	int ret = 0;
555 
556 	xa_for_each_start(&ipc4_data->fw_lib_xa, lib_id, fw_lib, 1) {
557 		ret = ipc4_data->load_library(sdev, fw_lib, true);
558 		if (ret) {
559 			dev_err(sdev->dev, "%s: Failed to reload library: %s, %d\n",
560 				__func__, fw_lib->name, ret);
561 			break;
562 		}
563 	}
564 
565 	return ret;
566 }
567 
568 /**
569  * sof_ipc4_update_cpc_from_manifest - Update the cpc in base config from manifest
570  * @sdev: SOF device
571  * @fw_module: pointer struct sof_ipc4_fw_module to parse
572  * @basecfg: Pointer to the base_config to update
573  */
sof_ipc4_update_cpc_from_manifest(struct snd_sof_dev * sdev,struct sof_ipc4_fw_module * fw_module,struct sof_ipc4_base_module_cfg * basecfg)574 void sof_ipc4_update_cpc_from_manifest(struct snd_sof_dev *sdev,
575 				       struct sof_ipc4_fw_module *fw_module,
576 				       struct sof_ipc4_base_module_cfg *basecfg)
577 {
578 	const struct sof_man4_module_config *fw_mod_cfg;
579 	u32 cpc_pick = 0;
580 	u32 max_cpc = 0;
581 	const char *msg;
582 	int i;
583 
584 	if (!fw_module->fw_mod_cfg) {
585 		msg = "No mod_cfg available for CPC lookup in the firmware file's manifest";
586 		goto no_cpc;
587 	}
588 
589 	/*
590 	 * Find the best matching (highest) CPC value based on the module's
591 	 * IBS/OBS configuration inferred from the audio format selection.
592 	 *
593 	 * The CPC value in each module config entry has been measured and
594 	 * recorded as a IBS/OBS/CPC triplet and stored in the firmware file's
595 	 * manifest
596 	 */
597 	fw_mod_cfg = fw_module->fw_mod_cfg;
598 	for (i = 0; i < fw_module->man4_module_entry.cfg_count; i++) {
599 		if (basecfg->obs == fw_mod_cfg[i].obs &&
600 		    basecfg->ibs == fw_mod_cfg[i].ibs &&
601 		    cpc_pick < fw_mod_cfg[i].cpc)
602 			cpc_pick = fw_mod_cfg[i].cpc;
603 
604 		if (max_cpc < fw_mod_cfg[i].cpc)
605 			max_cpc = fw_mod_cfg[i].cpc;
606 	}
607 
608 	basecfg->cpc = cpc_pick;
609 
610 	/* We have a matching configuration for CPC */
611 	if (basecfg->cpc)
612 		return;
613 
614 	/*
615 	 * No matching IBS/OBS found, the firmware manifest is missing
616 	 * information in the module's module configuration table.
617 	 */
618 	if (!max_cpc)
619 		msg = "No CPC value available in the firmware file's manifest";
620 	else if (!cpc_pick)
621 		msg = "No CPC match in the firmware file's manifest";
622 
623 no_cpc:
624 	dev_dbg(sdev->dev, "%s (UUID: %pUL): %s (ibs/obs: %u/%u)\n",
625 		fw_module->man4_module_entry.name,
626 		&fw_module->man4_module_entry.uuid, msg, basecfg->ibs,
627 		basecfg->obs);
628 }
629 
630 const struct sof_ipc_fw_loader_ops ipc4_loader_ops = {
631 	.validate = sof_ipc4_validate_firmware,
632 	.parse_ext_manifest = sof_ipc4_fw_parse_basefw_ext_man,
633 };
634