ipc4-loader.c 14 KB

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