fw-file-profile.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. tplg_filename = kasprintf(GFP_KERNEL, "%s/%s", profile->tplg_path,
  67. profile->tplg_name);
  68. if (!tplg_filename)
  69. return -ENOMEM;
  70. ret = firmware_request_nowarn(&fw, tplg_filename, dev);
  71. if (!ret)
  72. release_firmware(fw);
  73. else
  74. dev_dbg(dev, "Failed to open topology file: %s\n", tplg_filename);
  75. kfree(tplg_filename);
  76. return ret;
  77. }
  78. static bool sof_platform_uses_generic_loader(struct snd_sof_dev *sdev)
  79. {
  80. return (sdev->pdata->desc->ops->load_firmware == snd_sof_load_firmware_raw ||
  81. sdev->pdata->desc->ops->load_firmware == snd_sof_load_firmware_memcpy);
  82. }
  83. static int
  84. sof_file_profile_for_ipc_type(struct snd_sof_dev *sdev,
  85. enum sof_ipc_type ipc_type,
  86. const struct sof_dev_desc *desc,
  87. struct sof_loadable_file_profile *base_profile,
  88. struct sof_loadable_file_profile *out_profile)
  89. {
  90. struct snd_sof_pdata *plat_data = sdev->pdata;
  91. bool fw_lib_path_allocated = false;
  92. struct device *dev = sdev->dev;
  93. bool fw_path_allocated = false;
  94. int ret = 0;
  95. /* firmware path */
  96. if (base_profile->fw_path) {
  97. out_profile->fw_path = base_profile->fw_path;
  98. } else if (base_profile->fw_path_postfix) {
  99. out_profile->fw_path = devm_kasprintf(dev, GFP_KERNEL, "%s/%s",
  100. desc->default_fw_path[ipc_type],
  101. base_profile->fw_path_postfix);
  102. if (!out_profile->fw_path)
  103. return -ENOMEM;
  104. fw_path_allocated = true;
  105. } else {
  106. out_profile->fw_path = desc->default_fw_path[ipc_type];
  107. }
  108. /* firmware filename */
  109. if (base_profile->fw_name)
  110. out_profile->fw_name = base_profile->fw_name;
  111. else
  112. out_profile->fw_name = desc->default_fw_filename[ipc_type];
  113. /*
  114. * Check the custom firmware path/filename and adjust the ipc_type to
  115. * match with the existing file for the remaining path configuration.
  116. *
  117. * For default path and firmware name do a verification before
  118. * continuing further.
  119. */
  120. if ((base_profile->fw_path || base_profile->fw_name) &&
  121. sof_platform_uses_generic_loader(sdev)) {
  122. ret = sof_test_firmware_file(dev, out_profile, &ipc_type);
  123. if (ret)
  124. return ret;
  125. if (!(desc->ipc_supported_mask & BIT(ipc_type))) {
  126. dev_err(dev, "Unsupported IPC type %d needed by %s/%s\n",
  127. ipc_type, out_profile->fw_path,
  128. out_profile->fw_name);
  129. return -EINVAL;
  130. }
  131. }
  132. /* firmware library path */
  133. if (base_profile->fw_lib_path) {
  134. out_profile->fw_lib_path = base_profile->fw_lib_path;
  135. } else if (desc->default_lib_path[ipc_type]) {
  136. if (base_profile->fw_lib_path_postfix) {
  137. out_profile->fw_lib_path = devm_kasprintf(dev,
  138. GFP_KERNEL, "%s/%s",
  139. desc->default_lib_path[ipc_type],
  140. base_profile->fw_lib_path_postfix);
  141. if (!out_profile->fw_lib_path) {
  142. ret = -ENOMEM;
  143. goto out;
  144. }
  145. fw_lib_path_allocated = true;
  146. } else {
  147. out_profile->fw_lib_path = desc->default_lib_path[ipc_type];
  148. }
  149. }
  150. if (base_profile->fw_path_postfix)
  151. out_profile->fw_path_postfix = base_profile->fw_path_postfix;
  152. if (base_profile->fw_lib_path_postfix)
  153. out_profile->fw_lib_path_postfix = base_profile->fw_lib_path_postfix;
  154. /* topology path */
  155. if (base_profile->tplg_path)
  156. out_profile->tplg_path = base_profile->tplg_path;
  157. else
  158. out_profile->tplg_path = desc->default_tplg_path[ipc_type];
  159. /* topology name */
  160. out_profile->tplg_name = plat_data->tplg_filename;
  161. out_profile->ipc_type = ipc_type;
  162. /* Test only default firmware file */
  163. if ((!base_profile->fw_path && !base_profile->fw_name) &&
  164. sof_platform_uses_generic_loader(sdev))
  165. ret = sof_test_firmware_file(dev, out_profile, NULL);
  166. if (!ret)
  167. ret = sof_test_topology_file(dev, out_profile);
  168. out:
  169. if (ret) {
  170. /* Free up path strings created with devm_kasprintf */
  171. if (fw_path_allocated)
  172. devm_kfree(dev, out_profile->fw_path);
  173. if (fw_lib_path_allocated)
  174. devm_kfree(dev, out_profile->fw_lib_path);
  175. memset(out_profile, 0, sizeof(*out_profile));
  176. }
  177. return ret;
  178. }
  179. static void
  180. sof_print_missing_firmware_info(struct snd_sof_dev *sdev,
  181. enum sof_ipc_type ipc_type,
  182. struct sof_loadable_file_profile *base_profile)
  183. {
  184. struct snd_sof_pdata *plat_data = sdev->pdata;
  185. const struct sof_dev_desc *desc = plat_data->desc;
  186. struct device *dev = sdev->dev;
  187. int ipc_type_count, i;
  188. char *marker;
  189. dev_err(dev, "SOF firmware and/or topology file not found.\n");
  190. dev_info(dev, "Supported default profiles\n");
  191. if (IS_ENABLED(CONFIG_SND_SOC_SOF_ALLOW_FALLBACK_TO_NEWER_IPC_VERSION))
  192. ipc_type_count = SOF_IPC_TYPE_COUNT - 1;
  193. else
  194. ipc_type_count = base_profile->ipc_type;
  195. for (i = 0; i <= ipc_type_count; i++) {
  196. if (!(desc->ipc_supported_mask & BIT(i)))
  197. continue;
  198. if (i == ipc_type)
  199. marker = "Requested";
  200. else
  201. marker = "Fallback";
  202. dev_info(dev, "- ipc type %d (%s):\n", i, marker);
  203. if (base_profile->fw_path_postfix)
  204. dev_info(dev, " Firmware file: %s/%s/%s\n",
  205. desc->default_fw_path[i],
  206. base_profile->fw_path_postfix,
  207. desc->default_fw_filename[i]);
  208. else
  209. dev_info(dev, " Firmware file: %s/%s\n",
  210. desc->default_fw_path[i],
  211. desc->default_fw_filename[i]);
  212. dev_info(dev, " Topology file: %s/%s\n",
  213. desc->default_tplg_path[i],
  214. plat_data->tplg_filename);
  215. }
  216. if (base_profile->fw_path || base_profile->fw_name ||
  217. base_profile->tplg_path || base_profile->tplg_name)
  218. dev_info(dev, "Verify the path/name override module parameters.\n");
  219. dev_info(dev, "Check if you have 'sof-firmware' package installed.\n");
  220. dev_info(dev, "Optionally it can be manually downloaded from:\n");
  221. dev_info(dev, " https://github.com/thesofproject/sof-bin/\n");
  222. }
  223. static void sof_print_profile_info(struct snd_sof_dev *sdev,
  224. enum sof_ipc_type ipc_type,
  225. struct sof_loadable_file_profile *profile)
  226. {
  227. struct device *dev = sdev->dev;
  228. if (ipc_type != profile->ipc_type)
  229. dev_info(dev,
  230. "Using fallback IPC type %d (requested type was %d)\n",
  231. profile->ipc_type, ipc_type);
  232. dev_info(dev, "Firmware paths/files for ipc type %d:\n", profile->ipc_type);
  233. /* The firmware path is only valid when generic loader is used */
  234. if (sof_platform_uses_generic_loader(sdev))
  235. dev_info(dev, " Firmware file: %s/%s\n",
  236. profile->fw_path, profile->fw_name);
  237. if (profile->fw_lib_path)
  238. dev_info(dev, " Firmware lib path: %s\n", profile->fw_lib_path);
  239. dev_info(dev, " Topology file: %s/%s\n", profile->tplg_path, profile->tplg_name);
  240. }
  241. int sof_create_ipc_file_profile(struct snd_sof_dev *sdev,
  242. struct sof_loadable_file_profile *base_profile,
  243. struct sof_loadable_file_profile *out_profile)
  244. {
  245. const struct sof_dev_desc *desc = sdev->pdata->desc;
  246. int ipc_fallback_start, ret, i;
  247. memset(out_profile, 0, sizeof(*out_profile));
  248. ret = sof_file_profile_for_ipc_type(sdev, base_profile->ipc_type, desc,
  249. base_profile, out_profile);
  250. if (!ret)
  251. goto out;
  252. /*
  253. * No firmware file was found for the requested IPC type, as fallback
  254. * if SND_SOC_SOF_ALLOW_FALLBACK_TO_NEWER_IPC_VERSION is selected, check
  255. * all IPC versions in a backwards direction (from newer to older)
  256. * if SND_SOC_SOF_ALLOW_FALLBACK_TO_NEWER_IPC_VERSION is not selected,
  257. * check only older IPC versions than the selected/default version
  258. */
  259. if (IS_ENABLED(CONFIG_SND_SOC_SOF_ALLOW_FALLBACK_TO_NEWER_IPC_VERSION))
  260. ipc_fallback_start = SOF_IPC_TYPE_COUNT - 1;
  261. else
  262. ipc_fallback_start = (int)base_profile->ipc_type - 1;
  263. for (i = ipc_fallback_start; i >= 0 ; i--) {
  264. if (i == base_profile->ipc_type ||
  265. !(desc->ipc_supported_mask & BIT(i)))
  266. continue;
  267. ret = sof_file_profile_for_ipc_type(sdev, i, desc, base_profile,
  268. out_profile);
  269. if (!ret)
  270. break;
  271. }
  272. out:
  273. if (ret)
  274. sof_print_missing_firmware_info(sdev, base_profile->ipc_type,
  275. base_profile);
  276. else
  277. sof_print_profile_info(sdev, base_profile->ipc_type, out_profile);
  278. return ret;
  279. }
  280. EXPORT_SYMBOL(sof_create_ipc_file_profile);