fw-file-profile.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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) 2023 Intel Corporation
  7. //
  8. #include <linux/firmware.h>
  9. #include <sound/sof.h>
  10. #include <sound/sof/ext_manifest4.h>
  11. #include "sof-priv.h"
  12. static int sof_test_firmware_file(struct device *dev,
  13. struct sof_loadable_file_profile *profile,
  14. enum sof_ipc_type *ipc_type_to_adjust)
  15. {
  16. enum sof_ipc_type fw_ipc_type;
  17. const struct firmware *fw;
  18. const char *fw_filename;
  19. const u32 *magic;
  20. int ret;
  21. fw_filename = kasprintf(GFP_KERNEL, "%s/%s", profile->fw_path,
  22. profile->fw_name);
  23. if (!fw_filename)
  24. return -ENOMEM;
  25. ret = firmware_request_nowarn(&fw, fw_filename, dev);
  26. if (ret < 0) {
  27. dev_dbg(dev, "Failed to open firmware file: %s\n", fw_filename);
  28. kfree(fw_filename);
  29. return ret;
  30. }
  31. /* firmware file exists, check the magic number */
  32. magic = (const u32 *)fw->data;
  33. switch (*magic) {
  34. case SOF_EXT_MAN_MAGIC_NUMBER:
  35. fw_ipc_type = SOF_IPC_TYPE_3;
  36. break;
  37. case SOF_EXT_MAN4_MAGIC_NUMBER:
  38. fw_ipc_type = SOF_IPC_TYPE_4;
  39. break;
  40. default:
  41. dev_err(dev, "Invalid firmware magic: %#x\n", *magic);
  42. ret = -EINVAL;
  43. goto out;
  44. }
  45. if (ipc_type_to_adjust) {
  46. *ipc_type_to_adjust = fw_ipc_type;
  47. } else if (fw_ipc_type != profile->ipc_type) {
  48. dev_err(dev,
  49. "ipc type mismatch between %s and expected: %d vs %d\n",
  50. fw_filename, fw_ipc_type, profile->ipc_type);
  51. ret = -EINVAL;
  52. }
  53. out:
  54. release_firmware(fw);
  55. kfree(fw_filename);
  56. return ret;
  57. }
  58. static int sof_test_topology_file(struct device *dev,
  59. struct sof_loadable_file_profile *profile)
  60. {
  61. const struct firmware *fw;
  62. const char *tplg_filename;
  63. int ret;
  64. if (!profile->tplg_path || !profile->tplg_name)
  65. return 0;
  66. /* Dummy topology does not exist and should not be used */
  67. if (strstr(profile->tplg_name, "dummy"))
  68. return 0;
  69. tplg_filename = kasprintf(GFP_KERNEL, "%s/%s", profile->tplg_path,
  70. profile->tplg_name);
  71. if (!tplg_filename)
  72. return -ENOMEM;
  73. ret = firmware_request_nowarn(&fw, tplg_filename, dev);
  74. if (!ret)
  75. release_firmware(fw);
  76. else
  77. dev_dbg(dev, "Failed to open topology file: %s\n", tplg_filename);
  78. kfree(tplg_filename);
  79. return ret;
  80. }
  81. static bool sof_platform_uses_generic_loader(struct snd_sof_dev *sdev)
  82. {
  83. return (sdev->pdata->desc->ops->load_firmware == snd_sof_load_firmware_raw ||
  84. sdev->pdata->desc->ops->load_firmware == snd_sof_load_firmware_memcpy);
  85. }
  86. static int
  87. sof_file_profile_for_ipc_type(struct snd_sof_dev *sdev,
  88. enum sof_ipc_type ipc_type,
  89. const struct sof_dev_desc *desc,
  90. struct sof_loadable_file_profile *base_profile,
  91. struct sof_loadable_file_profile *out_profile)
  92. {
  93. struct snd_sof_pdata *plat_data = sdev->pdata;
  94. bool fw_lib_path_allocated = false;
  95. struct device *dev = sdev->dev;
  96. bool fw_path_allocated = false;
  97. int ret = 0;
  98. /* firmware path */
  99. if (base_profile->fw_path) {
  100. out_profile->fw_path = base_profile->fw_path;
  101. } else if (base_profile->fw_path_postfix) {
  102. out_profile->fw_path = devm_kasprintf(dev, GFP_KERNEL, "%s/%s",
  103. desc->default_fw_path[ipc_type],
  104. base_profile->fw_path_postfix);
  105. if (!out_profile->fw_path)
  106. return -ENOMEM;
  107. fw_path_allocated = true;
  108. } else {
  109. out_profile->fw_path = desc->default_fw_path[ipc_type];
  110. }
  111. /* firmware filename */
  112. if (base_profile->fw_name)
  113. out_profile->fw_name = base_profile->fw_name;
  114. else
  115. out_profile->fw_name = desc->default_fw_filename[ipc_type];
  116. /*
  117. * Check the custom firmware path/filename and adjust the ipc_type to
  118. * match with the existing file for the remaining path configuration.
  119. *
  120. * For default path and firmware name do a verification before
  121. * continuing further.
  122. */
  123. if ((base_profile->fw_path || base_profile->fw_name) &&
  124. sof_platform_uses_generic_loader(sdev)) {
  125. ret = sof_test_firmware_file(dev, out_profile, &ipc_type);
  126. if (ret)
  127. return ret;
  128. if (!(desc->ipc_supported_mask & BIT(ipc_type))) {
  129. dev_err(dev, "Unsupported IPC type %d needed by %s/%s\n",
  130. ipc_type, out_profile->fw_path,
  131. out_profile->fw_name);
  132. return -EINVAL;
  133. }
  134. }
  135. /* firmware library path */
  136. if (base_profile->fw_lib_path) {
  137. out_profile->fw_lib_path = base_profile->fw_lib_path;
  138. } else if (desc->default_lib_path[ipc_type]) {
  139. if (base_profile->fw_lib_path_postfix) {
  140. out_profile->fw_lib_path = devm_kasprintf(dev,
  141. GFP_KERNEL, "%s/%s",
  142. desc->default_lib_path[ipc_type],
  143. base_profile->fw_lib_path_postfix);
  144. if (!out_profile->fw_lib_path) {
  145. ret = -ENOMEM;
  146. goto out;
  147. }
  148. fw_lib_path_allocated = true;
  149. } else {
  150. out_profile->fw_lib_path = desc->default_lib_path[ipc_type];
  151. }
  152. }
  153. if (base_profile->fw_path_postfix)
  154. out_profile->fw_path_postfix = base_profile->fw_path_postfix;
  155. if (base_profile->fw_lib_path_postfix)
  156. out_profile->fw_lib_path_postfix = base_profile->fw_lib_path_postfix;
  157. /* topology path */
  158. if (base_profile->tplg_path)
  159. out_profile->tplg_path = base_profile->tplg_path;
  160. else
  161. out_profile->tplg_path = desc->default_tplg_path[ipc_type];
  162. /* topology name */
  163. out_profile->tplg_name = plat_data->tplg_filename;
  164. out_profile->ipc_type = ipc_type;
  165. /* Test only default firmware file */
  166. if ((!base_profile->fw_path && !base_profile->fw_name) &&
  167. sof_platform_uses_generic_loader(sdev))
  168. ret = sof_test_firmware_file(dev, out_profile, NULL);
  169. if (!ret)
  170. ret = sof_test_topology_file(dev, out_profile);
  171. out:
  172. if (ret) {
  173. /* Free up path strings created with devm_kasprintf */
  174. if (fw_path_allocated)
  175. devm_kfree(dev, out_profile->fw_path);
  176. if (fw_lib_path_allocated)
  177. devm_kfree(dev, out_profile->fw_lib_path);
  178. memset(out_profile, 0, sizeof(*out_profile));
  179. }
  180. return ret;
  181. }
  182. static void
  183. sof_print_missing_firmware_info(struct snd_sof_dev *sdev,
  184. enum sof_ipc_type ipc_type,
  185. struct sof_loadable_file_profile *base_profile)
  186. {
  187. struct snd_sof_pdata *plat_data = sdev->pdata;
  188. const struct sof_dev_desc *desc = plat_data->desc;
  189. struct device *dev = sdev->dev;
  190. int ipc_type_count, i;
  191. char *marker;
  192. dev_err(dev, "SOF firmware and/or topology file not found.\n");
  193. dev_info(dev, "Supported default profiles\n");
  194. if (IS_ENABLED(CONFIG_SND_SOC_SOF_ALLOW_FALLBACK_TO_NEWER_IPC_VERSION))
  195. ipc_type_count = SOF_IPC_TYPE_COUNT - 1;
  196. else
  197. ipc_type_count = base_profile->ipc_type;
  198. for (i = 0; i <= ipc_type_count; i++) {
  199. if (!(desc->ipc_supported_mask & BIT(i)))
  200. continue;
  201. if (i == ipc_type)
  202. marker = "Requested";
  203. else
  204. marker = "Fallback";
  205. dev_info(dev, "- ipc type %d (%s):\n", i, marker);
  206. if (base_profile->fw_path_postfix)
  207. dev_info(dev, " Firmware file: %s/%s/%s\n",
  208. desc->default_fw_path[i],
  209. base_profile->fw_path_postfix,
  210. desc->default_fw_filename[i]);
  211. else
  212. dev_info(dev, " Firmware file: %s/%s\n",
  213. desc->default_fw_path[i],
  214. desc->default_fw_filename[i]);
  215. dev_info(dev, " Topology file: %s/%s\n",
  216. desc->default_tplg_path[i],
  217. plat_data->tplg_filename);
  218. }
  219. if (base_profile->fw_path || base_profile->fw_name ||
  220. base_profile->tplg_path || base_profile->tplg_name)
  221. dev_info(dev, "Verify the path/name override module parameters.\n");
  222. dev_info(dev, "Check if you have 'sof-firmware' package installed.\n");
  223. dev_info(dev, "Optionally it can be manually downloaded from:\n");
  224. dev_info(dev, " https://github.com/thesofproject/sof-bin/\n");
  225. }
  226. static void sof_print_profile_info(struct snd_sof_dev *sdev,
  227. enum sof_ipc_type ipc_type,
  228. struct sof_loadable_file_profile *profile)
  229. {
  230. struct snd_sof_pdata *plat_data = sdev->pdata;
  231. struct device *dev = sdev->dev;
  232. if (ipc_type != profile->ipc_type)
  233. dev_info(dev,
  234. "Using fallback IPC type %d (requested type was %d)\n",
  235. profile->ipc_type, ipc_type);
  236. dev_info(dev, "Firmware paths/files for ipc type %d:\n", profile->ipc_type);
  237. /* The firmware path is only valid when generic loader is used */
  238. if (sof_platform_uses_generic_loader(sdev))
  239. dev_info(dev, " Firmware file: %s/%s\n",
  240. profile->fw_path, profile->fw_name);
  241. if (profile->fw_lib_path)
  242. dev_info(dev, " Firmware lib path: %s\n", profile->fw_lib_path);
  243. if (plat_data->machine && plat_data->machine->get_function_tplg_files &&
  244. !plat_data->disable_function_topology)
  245. dev_info(dev, " Topology file: function topologies\n");
  246. else
  247. dev_info(dev, " Topology file: %s/%s\n",
  248. profile->tplg_path, profile->tplg_name);
  249. }
  250. int sof_create_ipc_file_profile(struct snd_sof_dev *sdev,
  251. struct sof_loadable_file_profile *base_profile,
  252. struct sof_loadable_file_profile *out_profile)
  253. {
  254. const struct sof_dev_desc *desc = sdev->pdata->desc;
  255. int ipc_fallback_start, ret, i;
  256. memset(out_profile, 0, sizeof(*out_profile));
  257. ret = sof_file_profile_for_ipc_type(sdev, base_profile->ipc_type, desc,
  258. base_profile, out_profile);
  259. if (!ret)
  260. goto out;
  261. /*
  262. * No firmware file was found for the requested IPC type, as fallback
  263. * if SND_SOC_SOF_ALLOW_FALLBACK_TO_NEWER_IPC_VERSION is selected, check
  264. * all IPC versions in a backwards direction (from newer to older)
  265. * if SND_SOC_SOF_ALLOW_FALLBACK_TO_NEWER_IPC_VERSION is not selected,
  266. * check only older IPC versions than the selected/default version
  267. */
  268. if (IS_ENABLED(CONFIG_SND_SOC_SOF_ALLOW_FALLBACK_TO_NEWER_IPC_VERSION))
  269. ipc_fallback_start = SOF_IPC_TYPE_COUNT - 1;
  270. else
  271. ipc_fallback_start = (int)base_profile->ipc_type - 1;
  272. for (i = ipc_fallback_start; i >= 0 ; i--) {
  273. if (i == base_profile->ipc_type ||
  274. !(desc->ipc_supported_mask & BIT(i)))
  275. continue;
  276. ret = sof_file_profile_for_ipc_type(sdev, i, desc, base_profile,
  277. out_profile);
  278. if (!ret)
  279. break;
  280. }
  281. out:
  282. if (ret)
  283. sof_print_missing_firmware_info(sdev, base_profile->ipc_type,
  284. base_profile);
  285. else
  286. sof_print_profile_info(sdev, base_profile->ipc_type, out_profile);
  287. return ret;
  288. }
  289. EXPORT_SYMBOL(sof_create_ipc_file_profile);