Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
0002 //
0003 // This file is provided under a dual BSD/GPLv2 license.  When using or
0004 // redistributing this file, you may do so under either license.
0005 //
0006 // Copyright(c) 2022 Intel Corporation. All rights reserved.
0007 
0008 #include <linux/firmware.h>
0009 #include <sound/sof/ext_manifest4.h>
0010 #include <sound/sof/ipc4/header.h>
0011 #include "ipc4-priv.h"
0012 #include "sof-audio.h"
0013 #include "sof-priv.h"
0014 #include "ops.h"
0015 
0016 static size_t sof_ipc4_fw_parse_ext_man(struct snd_sof_dev *sdev)
0017 {
0018     struct sof_ipc4_fw_data *ipc4_data = sdev->private;
0019     struct snd_sof_pdata *plat_data = sdev->pdata;
0020     struct sof_man4_fw_binary_header *fw_header;
0021     const struct firmware *fw = plat_data->fw;
0022     struct sof_ext_manifest4_hdr *ext_man_hdr;
0023     struct sof_man4_module_config *fm_config;
0024     struct sof_ipc4_fw_module *fw_module;
0025     struct sof_man4_module *fm_entry;
0026     ssize_t remaining;
0027     u32 fw_hdr_offset;
0028     int i;
0029 
0030     if (!ipc4_data) {
0031         dev_err(sdev->dev, "%s: ipc4_data is not available\n", __func__);
0032         return -EINVAL;
0033     }
0034 
0035     remaining = fw->size;
0036     if (remaining <= sizeof(*ext_man_hdr)) {
0037         dev_err(sdev->dev, "Firmware size is too small: %zu\n", remaining);
0038         return -EINVAL;
0039     }
0040 
0041     ext_man_hdr = (struct sof_ext_manifest4_hdr *)fw->data;
0042 
0043     fw_hdr_offset = ipc4_data->manifest_fw_hdr_offset;
0044     if (!fw_hdr_offset)
0045         return -EINVAL;
0046 
0047     if (remaining <= ext_man_hdr->len + fw_hdr_offset + sizeof(*fw_header)) {
0048         dev_err(sdev->dev, "Invalid firmware size %zu, should be at least %zu\n",
0049             remaining, ext_man_hdr->len + fw_hdr_offset + sizeof(*fw_header));
0050         return -EINVAL;
0051     }
0052 
0053     fw_header = (struct sof_man4_fw_binary_header *)
0054                 (fw->data + ext_man_hdr->len + fw_hdr_offset);
0055     remaining -= (ext_man_hdr->len + fw_hdr_offset);
0056 
0057     if (remaining <= fw_header->len) {
0058         dev_err(sdev->dev, "Invalid fw_header->len %u\n", fw_header->len);
0059         return -EINVAL;
0060     }
0061 
0062     dev_info(sdev->dev, "Loaded firmware version: %u.%u.%u.%u\n",
0063          fw_header->major_version, fw_header->minor_version,
0064          fw_header->hotfix_version, fw_header->build_version);
0065     dev_dbg(sdev->dev, "Firmware name: %s, header length: %u, module count: %u\n",
0066         fw_header->name, fw_header->len, fw_header->num_module_entries);
0067 
0068     ipc4_data->fw_modules = devm_kmalloc_array(sdev->dev,
0069                            fw_header->num_module_entries,
0070                            sizeof(*fw_module), GFP_KERNEL);
0071     if (!ipc4_data->fw_modules)
0072         return -ENOMEM;
0073 
0074     ipc4_data->num_fw_modules = fw_header->num_module_entries;
0075     fw_module = ipc4_data->fw_modules;
0076 
0077     fm_entry = (struct sof_man4_module *)((u8 *)fw_header + fw_header->len);
0078     remaining -= fw_header->len;
0079 
0080     if (remaining < fw_header->num_module_entries * sizeof(*fm_entry)) {
0081         dev_err(sdev->dev, "Invalid num_module_entries %u\n",
0082             fw_header->num_module_entries);
0083         return -EINVAL;
0084     }
0085 
0086     fm_config = (struct sof_man4_module_config *)
0087                 (fm_entry + fw_header->num_module_entries);
0088     remaining -= (fw_header->num_module_entries * sizeof(*fm_entry));
0089     for (i = 0; i < fw_header->num_module_entries; i++) {
0090         memcpy(&fw_module->man4_module_entry, fm_entry, sizeof(*fm_entry));
0091 
0092         if (fm_entry->cfg_count) {
0093             if (remaining < (fm_entry->cfg_offset + fm_entry->cfg_count) *
0094                 sizeof(*fm_config)) {
0095                 dev_err(sdev->dev, "Invalid module cfg_offset %u\n",
0096                     fm_entry->cfg_offset);
0097                 return -EINVAL;
0098             }
0099 
0100             /* a module's config is always the same size */
0101             fw_module->bss_size = fm_config[fm_entry->cfg_offset].is_bytes;
0102 
0103             dev_dbg(sdev->dev,
0104                 "module %s: UUID %pUL cfg_count: %u, bss_size: %#x\n",
0105                 fm_entry->name, &fm_entry->uuid, fm_entry->cfg_count,
0106                 fw_module->bss_size);
0107         } else {
0108             fw_module->bss_size = 0;
0109 
0110             dev_dbg(sdev->dev, "module %s: UUID %pUL\n", fm_entry->name,
0111                 &fm_entry->uuid);
0112         }
0113 
0114         fw_module->man4_module_entry.id = i;
0115         ida_init(&fw_module->m_ida);
0116         fw_module->private = NULL;
0117 
0118         fw_module++;
0119         fm_entry++;
0120     }
0121 
0122     return ext_man_hdr->len;
0123 }
0124 
0125 static int sof_ipc4_validate_firmware(struct snd_sof_dev *sdev)
0126 {
0127     struct sof_ipc4_fw_data *ipc4_data = sdev->private;
0128     u32 fw_hdr_offset = ipc4_data->manifest_fw_hdr_offset;
0129     struct snd_sof_pdata *plat_data = sdev->pdata;
0130     struct sof_man4_fw_binary_header *fw_header;
0131     const struct firmware *fw = plat_data->fw;
0132     struct sof_ext_manifest4_hdr *ext_man_hdr;
0133 
0134     ext_man_hdr = (struct sof_ext_manifest4_hdr *)fw->data;
0135     fw_header = (struct sof_man4_fw_binary_header *)
0136                 (fw->data + ext_man_hdr->len + fw_hdr_offset);
0137 
0138     /* TODO: Add firmware verification code here */
0139 
0140     dev_dbg(sdev->dev, "Validated firmware version: %u.%u.%u.%u\n",
0141         fw_header->major_version, fw_header->minor_version,
0142         fw_header->hotfix_version, fw_header->build_version);
0143 
0144     return 0;
0145 }
0146 
0147 static int sof_ipc4_query_fw_configuration(struct snd_sof_dev *sdev)
0148 {
0149     const struct sof_ipc_ops *iops = sdev->ipc->ops;
0150     struct sof_ipc4_fw_version *fw_ver;
0151     struct sof_ipc4_tuple *tuple;
0152     struct sof_ipc4_msg msg;
0153     size_t offset = 0;
0154     int ret;
0155 
0156     /* Get the firmware configuration */
0157     msg.primary = SOF_IPC4_MSG_TARGET(SOF_IPC4_MODULE_MSG);
0158     msg.primary |= SOF_IPC4_MSG_DIR(SOF_IPC4_MSG_REQUEST);
0159     msg.primary |= SOF_IPC4_MOD_ID(SOF_IPC4_MOD_INIT_BASEFW_MOD_ID);
0160     msg.primary |= SOF_IPC4_MOD_INSTANCE(SOF_IPC4_MOD_INIT_BASEFW_INSTANCE_ID);
0161     msg.extension = SOF_IPC4_MOD_EXT_MSG_PARAM_ID(SOF_IPC4_FW_PARAM_FW_CONFIG);
0162 
0163     msg.data_size = sdev->ipc->max_payload_size;
0164     msg.data_ptr = kzalloc(msg.data_size, GFP_KERNEL);
0165     if (!msg.data_ptr)
0166         return -ENOMEM;
0167 
0168     ret = iops->set_get_data(sdev, &msg, msg.data_size, false);
0169     if (ret)
0170         goto out;
0171 
0172     while (offset < msg.data_size) {
0173         tuple = (struct sof_ipc4_tuple *)((u8 *)msg.data_ptr + offset);
0174 
0175         switch (tuple->type) {
0176         case SOF_IPC4_FW_CFG_FW_VERSION:
0177             fw_ver = (struct sof_ipc4_fw_version *)tuple->value;
0178 
0179             dev_info(sdev->dev,
0180                  "Booted firmware version: %u.%u.%u.%u\n",
0181                  fw_ver->major, fw_ver->minor, fw_ver->hotfix,
0182                  fw_ver->build);
0183             break;
0184         case SOF_IPC4_FW_CFG_DL_MAILBOX_BYTES:
0185             dev_vdbg(sdev->dev, "DL mailbox size: %u\n", *tuple->value);
0186             break;
0187         case SOF_IPC4_FW_CFG_UL_MAILBOX_BYTES:
0188             dev_vdbg(sdev->dev, "UL mailbox size: %u\n", *tuple->value);
0189             break;
0190         case SOF_IPC4_FW_CFG_TRACE_LOG_BYTES:
0191             dev_vdbg(sdev->dev, "Trace log size: %u\n", *tuple->value);
0192             break;
0193         default:
0194             break;
0195         }
0196 
0197         offset += sizeof(*tuple) + tuple->size;
0198     }
0199 
0200 out:
0201     kfree(msg.data_ptr);
0202 
0203     return ret;
0204 }
0205 
0206 const struct sof_ipc_fw_loader_ops ipc4_loader_ops = {
0207     .validate = sof_ipc4_validate_firmware,
0208     .parse_ext_manifest = sof_ipc4_fw_parse_ext_man,
0209     .query_fw_configuration = sof_ipc4_query_fw_configuration,
0210 };