ipc4-loader.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. All rights reserved.
  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. static size_t sof_ipc4_fw_parse_ext_man(struct snd_sof_dev *sdev)
  16. {
  17. struct sof_ipc4_fw_data *ipc4_data = sdev->private;
  18. struct snd_sof_pdata *plat_data = sdev->pdata;
  19. struct sof_man4_fw_binary_header *fw_header;
  20. const struct firmware *fw = plat_data->fw;
  21. struct sof_ext_manifest4_hdr *ext_man_hdr;
  22. struct sof_man4_module_config *fm_config;
  23. struct sof_ipc4_fw_module *fw_module;
  24. struct sof_man4_module *fm_entry;
  25. ssize_t remaining;
  26. u32 fw_hdr_offset;
  27. int i;
  28. if (!ipc4_data) {
  29. dev_err(sdev->dev, "%s: ipc4_data is not available\n", __func__);
  30. return -EINVAL;
  31. }
  32. remaining = fw->size;
  33. if (remaining <= sizeof(*ext_man_hdr)) {
  34. dev_err(sdev->dev, "Firmware size is too small: %zu\n", remaining);
  35. return -EINVAL;
  36. }
  37. ext_man_hdr = (struct sof_ext_manifest4_hdr *)fw->data;
  38. /*
  39. * At the start of the firmware image we must have an extended manifest.
  40. * Verify that the magic number is correct.
  41. */
  42. if (ext_man_hdr->id != SOF_EXT_MAN4_MAGIC_NUMBER) {
  43. dev_err(sdev->dev,
  44. "Unexpected extended manifest magic number: %#x\n",
  45. ext_man_hdr->id);
  46. return -EINVAL;
  47. }
  48. fw_hdr_offset = ipc4_data->manifest_fw_hdr_offset;
  49. if (!fw_hdr_offset)
  50. return -EINVAL;
  51. if (remaining <= ext_man_hdr->len + fw_hdr_offset + sizeof(*fw_header)) {
  52. dev_err(sdev->dev, "Invalid firmware size %zu, should be at least %zu\n",
  53. remaining, ext_man_hdr->len + fw_hdr_offset + sizeof(*fw_header));
  54. return -EINVAL;
  55. }
  56. fw_header = (struct sof_man4_fw_binary_header *)
  57. (fw->data + ext_man_hdr->len + fw_hdr_offset);
  58. remaining -= (ext_man_hdr->len + fw_hdr_offset);
  59. if (remaining <= fw_header->len) {
  60. dev_err(sdev->dev, "Invalid fw_header->len %u\n", fw_header->len);
  61. return -EINVAL;
  62. }
  63. dev_info(sdev->dev, "Loaded firmware version: %u.%u.%u.%u\n",
  64. fw_header->major_version, fw_header->minor_version,
  65. fw_header->hotfix_version, fw_header->build_version);
  66. dev_dbg(sdev->dev, "Firmware name: %s, header length: %u, module count: %u\n",
  67. fw_header->name, fw_header->len, fw_header->num_module_entries);
  68. ipc4_data->fw_modules = devm_kmalloc_array(sdev->dev,
  69. fw_header->num_module_entries,
  70. sizeof(*fw_module), GFP_KERNEL);
  71. if (!ipc4_data->fw_modules)
  72. return -ENOMEM;
  73. ipc4_data->num_fw_modules = fw_header->num_module_entries;
  74. fw_module = ipc4_data->fw_modules;
  75. fm_entry = (struct sof_man4_module *)((u8 *)fw_header + fw_header->len);
  76. remaining -= fw_header->len;
  77. if (remaining < fw_header->num_module_entries * sizeof(*fm_entry)) {
  78. dev_err(sdev->dev, "Invalid num_module_entries %u\n",
  79. fw_header->num_module_entries);
  80. return -EINVAL;
  81. }
  82. fm_config = (struct sof_man4_module_config *)
  83. (fm_entry + fw_header->num_module_entries);
  84. remaining -= (fw_header->num_module_entries * sizeof(*fm_entry));
  85. for (i = 0; i < fw_header->num_module_entries; i++) {
  86. memcpy(&fw_module->man4_module_entry, fm_entry, sizeof(*fm_entry));
  87. if (fm_entry->cfg_count) {
  88. if (remaining < (fm_entry->cfg_offset + fm_entry->cfg_count) *
  89. sizeof(*fm_config)) {
  90. dev_err(sdev->dev, "Invalid module cfg_offset %u\n",
  91. fm_entry->cfg_offset);
  92. return -EINVAL;
  93. }
  94. /* a module's config is always the same size */
  95. fw_module->bss_size = fm_config[fm_entry->cfg_offset].is_bytes;
  96. dev_dbg(sdev->dev,
  97. "module %s: UUID %pUL cfg_count: %u, bss_size: %#x\n",
  98. fm_entry->name, &fm_entry->uuid, fm_entry->cfg_count,
  99. fw_module->bss_size);
  100. } else {
  101. fw_module->bss_size = 0;
  102. dev_dbg(sdev->dev, "module %s: UUID %pUL\n", fm_entry->name,
  103. &fm_entry->uuid);
  104. }
  105. fw_module->man4_module_entry.id = i;
  106. ida_init(&fw_module->m_ida);
  107. fw_module->private = NULL;
  108. fw_module++;
  109. fm_entry++;
  110. }
  111. return ext_man_hdr->len;
  112. }
  113. static int sof_ipc4_validate_firmware(struct snd_sof_dev *sdev)
  114. {
  115. struct sof_ipc4_fw_data *ipc4_data = sdev->private;
  116. u32 fw_hdr_offset = ipc4_data->manifest_fw_hdr_offset;
  117. struct snd_sof_pdata *plat_data = sdev->pdata;
  118. struct sof_man4_fw_binary_header *fw_header;
  119. const struct firmware *fw = plat_data->fw;
  120. struct sof_ext_manifest4_hdr *ext_man_hdr;
  121. ext_man_hdr = (struct sof_ext_manifest4_hdr *)fw->data;
  122. fw_header = (struct sof_man4_fw_binary_header *)
  123. (fw->data + ext_man_hdr->len + fw_hdr_offset);
  124. /* TODO: Add firmware verification code here */
  125. dev_dbg(sdev->dev, "Validated firmware version: %u.%u.%u.%u\n",
  126. fw_header->major_version, fw_header->minor_version,
  127. fw_header->hotfix_version, fw_header->build_version);
  128. return 0;
  129. }
  130. static int sof_ipc4_query_fw_configuration(struct snd_sof_dev *sdev)
  131. {
  132. struct sof_ipc4_fw_data *ipc4_data = sdev->private;
  133. const struct sof_ipc_ops *iops = sdev->ipc->ops;
  134. struct sof_ipc4_fw_version *fw_ver;
  135. struct sof_ipc4_tuple *tuple;
  136. struct sof_ipc4_msg msg;
  137. size_t offset = 0;
  138. int ret;
  139. /* Get the firmware configuration */
  140. msg.primary = SOF_IPC4_MSG_TARGET(SOF_IPC4_MODULE_MSG);
  141. msg.primary |= SOF_IPC4_MSG_DIR(SOF_IPC4_MSG_REQUEST);
  142. msg.primary |= SOF_IPC4_MOD_ID(SOF_IPC4_MOD_INIT_BASEFW_MOD_ID);
  143. msg.primary |= SOF_IPC4_MOD_INSTANCE(SOF_IPC4_MOD_INIT_BASEFW_INSTANCE_ID);
  144. msg.extension = SOF_IPC4_MOD_EXT_MSG_PARAM_ID(SOF_IPC4_FW_PARAM_FW_CONFIG);
  145. msg.data_size = sdev->ipc->max_payload_size;
  146. msg.data_ptr = kzalloc(msg.data_size, GFP_KERNEL);
  147. if (!msg.data_ptr)
  148. return -ENOMEM;
  149. ret = iops->set_get_data(sdev, &msg, msg.data_size, false);
  150. if (ret)
  151. goto out;
  152. while (offset < msg.data_size) {
  153. tuple = (struct sof_ipc4_tuple *)((u8 *)msg.data_ptr + offset);
  154. switch (tuple->type) {
  155. case SOF_IPC4_FW_CFG_FW_VERSION:
  156. fw_ver = (struct sof_ipc4_fw_version *)tuple->value;
  157. dev_info(sdev->dev,
  158. "Booted firmware version: %u.%u.%u.%u\n",
  159. fw_ver->major, fw_ver->minor, fw_ver->hotfix,
  160. fw_ver->build);
  161. break;
  162. case SOF_IPC4_FW_CFG_DL_MAILBOX_BYTES:
  163. trace_sof_ipc4_fw_config(sdev, "DL mailbox size", *tuple->value);
  164. break;
  165. case SOF_IPC4_FW_CFG_UL_MAILBOX_BYTES:
  166. trace_sof_ipc4_fw_config(sdev, "UL mailbox size", *tuple->value);
  167. break;
  168. case SOF_IPC4_FW_CFG_TRACE_LOG_BYTES:
  169. trace_sof_ipc4_fw_config(sdev, "Trace log size", *tuple->value);
  170. ipc4_data->mtrace_log_bytes = *tuple->value;
  171. break;
  172. default:
  173. break;
  174. }
  175. offset += sizeof(*tuple) + tuple->size;
  176. }
  177. out:
  178. kfree(msg.data_ptr);
  179. return ret;
  180. }
  181. const struct sof_ipc_fw_loader_ops ipc4_loader_ops = {
  182. .validate = sof_ipc4_validate_firmware,
  183. .parse_ext_manifest = sof_ipc4_fw_parse_ext_man,
  184. .query_fw_configuration = sof_ipc4_query_fw_configuration,
  185. };