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