Skip to content

Commit

Permalink
soc: qcom: mdt_loader: Move the memory allocation into mdt loader
Browse files Browse the repository at this point in the history
By moving the memory allocation to mdt loader we can simplify the scm
call, by just packing arguments provided to it from the clients for
making secuer world calls. We can also simplify the memory allocation
for the qcom metadata, by just doing one memory allocation in the
mdt loader.

Signed-off-by: Gokul krishna Krishnakumar <quic_gokukris@quicinc.com>
  • Loading branch information
Gokul krishna Krishnakumar authored and intel-lab-lkp committed Sep 12, 2022
1 parent f163b1d commit 93615f3
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 13 deletions.
2 changes: 1 addition & 1 deletion drivers/remoteproc/qcom_q6v5_mss.c
Expand Up @@ -947,7 +947,7 @@ static int q6v5_mpss_init_image(struct q6v5 *qproc, const struct firmware *fw,
int ret;
int i;

metadata = qcom_mdt_read_metadata(fw, &size, fw_name, qproc->dev);
metadata = qcom_mdt_read_metadata(fw, &size, fw_name, qproc->dev, NULL);
if (IS_ERR(metadata))
return PTR_ERR(metadata);

Expand Down
41 changes: 31 additions & 10 deletions drivers/soc/qcom/mdt_loader.c
Expand Up @@ -16,6 +16,7 @@
#include <linux/sizes.h>
#include <linux/slab.h>
#include <linux/soc/qcom/mdt_loader.h>
#include <linux/dma-mapping.h>

static bool mdt_phdr_valid(const struct elf32_phdr *phdr)
{
Expand Down Expand Up @@ -110,6 +111,7 @@ EXPORT_SYMBOL_GPL(qcom_mdt_get_size);
* @data_len: length of the read metadata blob
* @fw_name: name of the firmware, for construction of segment file names
* @dev: device handle to associate resources with
* @mdata_phys: phys address for the assigned metadata buffer
*
* The mechanism that performs the authentication of the loading firmware
* expects an ELF header directly followed by the segment of hashes, with no
Expand All @@ -124,11 +126,13 @@ EXPORT_SYMBOL_GPL(qcom_mdt_get_size);
* Return: pointer to data, or ERR_PTR()
*/
void *qcom_mdt_read_metadata(const struct firmware *fw, size_t *data_len,
const char *fw_name, struct device *dev)
const char *fw_name, struct device *dev,
dma_addr_t *mdata_phys)
{
const struct elf32_phdr *phdrs;
const struct elf32_hdr *ehdr;
unsigned int hash_segment = 0;
struct device *scm_dev = NULL;
size_t hash_offset;
size_t hash_size;
size_t ehdr_size;
Expand Down Expand Up @@ -160,9 +164,18 @@ void *qcom_mdt_read_metadata(const struct firmware *fw, size_t *data_len,
ehdr_size = phdrs[0].p_filesz;
hash_size = phdrs[hash_segment].p_filesz;

data = kmalloc(ehdr_size + hash_size, GFP_KERNEL);
if (!data)
return ERR_PTR(-ENOMEM);
/*
* During the scm call memory protection will be enabled for the meta
* data blob, so make sure it's physically contiguous, 4K aligned and
* non-cachable to avoid XPU violations.
*/
scm_dev = qcom_get_scm_device();
data = dma_alloc_coherent(scm_dev, ehdr_size + hash_size, mdata_phys,
GFP_KERNEL);
if (!data) {
dev_err(dev, "Allocation of metadata buffer failed.\n");
return NULL;
}

/* Copy ELF header */
memcpy(data, fw->data, ehdr_size);
Expand All @@ -179,7 +192,7 @@ void *qcom_mdt_read_metadata(const struct firmware *fw, size_t *data_len,
/* Hash is in its own segment, beyond the loaded file */
ret = mdt_load_split_segment(data + ehdr_size, phdrs, hash_segment, fw_name, dev);
if (ret) {
kfree(data);
dma_free_coherent(scm_dev, ehdr_size + hash_size, data, mdata_phys);
return ERR_PTR(ret);
}
}
Expand Down Expand Up @@ -209,10 +222,11 @@ int qcom_mdt_pas_init(struct device *dev, const struct firmware *fw,
const struct elf32_phdr *phdr;
const struct elf32_hdr *ehdr;
phys_addr_t min_addr = PHYS_ADDR_MAX;
struct device *scm_dev = NULL;
phys_addr_t max_addr = 0;
dma_addr_t mdata_phys;
size_t metadata_len;
void *metadata;
void *mdata_buf;
int ret;
int i;

Expand All @@ -232,15 +246,22 @@ int qcom_mdt_pas_init(struct device *dev, const struct firmware *fw,
max_addr = ALIGN(phdr->p_paddr + phdr->p_memsz, SZ_4K);
}

metadata = qcom_mdt_read_metadata(fw, &metadata_len, fw_name, dev);
if (IS_ERR(metadata)) {
ret = PTR_ERR(metadata);
mdata_buf = qcom_mdt_read_metadata(fw, &metadata_len, fw_name, dev, &mdata_phys);
if (IS_ERR(mdata_buf)) {
ret = PTR_ERR(mdata_buf);
dev_err(dev, "error %d reading firmware %s metadata\n", ret, fw_name);
goto out;
}

ret = qcom_scm_pas_init_image(pas_id, mdata_phys);
kfree(metadata);
if (ret || !ctx) {
dma_free_coherent(scm_dev, metadata_len, mdata_buf, mdata_phys);
} else if (ctx) {
ctx->ptr = mdata_buf;
ctx->phys = mdata_phys;
ctx->size = metadata_len;
}

if (ret) {
/* Invalid firmware metadata */
dev_err(dev, "error %d initializing firmware %s\n", ret, fw_name);
Expand Down
5 changes: 3 additions & 2 deletions include/linux/soc/qcom/mdt_loader.h
Expand Up @@ -28,7 +28,8 @@ int qcom_mdt_load_no_init(struct device *dev, const struct firmware *fw,
phys_addr_t mem_phys, size_t mem_size,
phys_addr_t *reloc_base);
void *qcom_mdt_read_metadata(const struct firmware *fw, size_t *data_len,
const char *fw_name, struct device *dev);
const char *fw_name, struct device *dev,
dma_addr_t *mdata_phys);

#else /* !IS_ENABLED(CONFIG_QCOM_MDT_LOADER) */

Expand Down Expand Up @@ -64,7 +65,7 @@ static inline int qcom_mdt_load_no_init(struct device *dev,

static inline void *qcom_mdt_read_metadata(const struct firmware *fw,
size_t *data_len, const char *fw_name,
struct device *dev)
struct device *dev, dma_addr_t *mdata_phys)
{
return ERR_PTR(-ENODEV);
}
Expand Down

0 comments on commit 93615f3

Please sign in to comment.