Re: [PATCH v2 06/11] firmware: qcom: scm: make qcom_scm_pas_init_image() use the SCM allocator

From: Bartosz Golaszewski
Date: Fri Sep 29 2023 - 15:22:23 EST


On Fri, 29 Sep 2023 21:16:51 +0200, Andrew Halaney <ahalaney@xxxxxxxxxx> said:
> On Thu, Sep 28, 2023 at 11:20:35AM +0200, Bartosz Golaszewski wrote:
>> From: Bartosz Golaszewski <bartosz.golaszewski@xxxxxxxxxx>
>>
>> Let's use the new SCM memory allocator to obtain a buffer for this call
>> instead of using dma_alloc_coherent().
>>
>> Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@xxxxxxxxxx>
>> ---
>> drivers/firmware/qcom/qcom_scm.c | 16 +++++-----------
>> 1 file changed, 5 insertions(+), 11 deletions(-)
>>
>> diff --git a/drivers/firmware/qcom/qcom_scm.c b/drivers/firmware/qcom/qcom_scm.c
>> index 02a773ba1383..c0eb81069847 100644
>> --- a/drivers/firmware/qcom/qcom_scm.c
>> +++ b/drivers/firmware/qcom/qcom_scm.c
>> @@ -532,7 +532,7 @@ static void qcom_scm_set_download_mode(bool enable)
>> int qcom_scm_pas_init_image(u32 peripheral, const void *metadata, size_t size,
>> struct qcom_scm_pas_metadata *ctx)
>> {
>> - dma_addr_t mdata_phys;
>> + phys_addr_t mdata_phys;
>
>> void *mdata_buf;
>> int ret;
>> struct qcom_scm_desc desc = {
>> @@ -544,13 +544,7 @@ int qcom_scm_pas_init_image(u32 peripheral, const void *metadata, size_t size,
>> };
>> struct qcom_scm_res res;
>>
>> - /*
>> - * 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.
>> - */
>> - mdata_buf = dma_alloc_coherent(__scm->dev, size, &mdata_phys,
>> - GFP_KERNEL);
>> + mdata_buf = qcom_scm_mem_alloc(size, GFP_KERNEL);
>
> mdata_phys is never initialized now, and its what's being shoved into
> desc.args[1] later, which I believe is what triggered the -EINVAL
> with qcom_scm_call() that I reported in my cover letter reply this
> morning.
>
> Prior with the DMA API that would have been the device view of the buffer.
>

Gah! Thanks for finding this.

Can you try the following diff?

diff --git a/drivers/firmware/qcom/qcom_scm.c b/drivers/firmware/qcom/qcom_scm.c
index 794388c3212f..b0d4ea237034 100644
--- a/drivers/firmware/qcom/qcom_scm.c
+++ b/drivers/firmware/qcom/qcom_scm.c
@@ -556,6 +556,7 @@ int qcom_scm_pas_init_image(u32 peripheral, const
void *metadata, size_t size,
dev_err(__scm->dev, "Allocation of metadata buffer failed.\n");
return -ENOMEM;
}
+ mdata_phys = qcom_scm_mem_to_phys(mdata_buf);
memcpy(mdata_buf, metadata, size);

ret = qcom_scm_clk_enable();
@@ -578,7 +579,7 @@ int qcom_scm_pas_init_image(u32 peripheral, const
void *metadata, size_t size,
qcom_scm_mem_free(mdata_buf);
} else if (ctx) {
ctx->ptr = mdata_buf;
- ctx->phys = qcom_scm_mem_to_phys(mdata_buf);
+ ctx->phys = mdata_phys;
ctx->size = size;
}

Bart

>> if (!mdata_buf) {
>> dev_err(__scm->dev, "Allocation of metadata buffer failed.\n");
>> return -ENOMEM;
>> @@ -574,10 +568,10 @@ int qcom_scm_pas_init_image(u32 peripheral, const void *metadata, size_t size,
>>
>> out:
>> if (ret < 0 || !ctx) {
>> - dma_free_coherent(__scm->dev, size, mdata_buf, mdata_phys);
>> + qcom_scm_mem_free(mdata_buf);
>> } else if (ctx) {
>> ctx->ptr = mdata_buf;
>> - ctx->phys = mdata_phys;
>> + ctx->phys = qcom_scm_mem_to_phys(mdata_buf);
>> ctx->size = size;
>> }
>>
>> @@ -594,7 +588,7 @@ void qcom_scm_pas_metadata_release(struct qcom_scm_pas_metadata *ctx)
>> if (!ctx->ptr)
>> return;
>>
>> - dma_free_coherent(__scm->dev, ctx->size, ctx->ptr, ctx->phys);
>> + qcom_scm_mem_free(ctx->ptr);
>>
>> ctx->ptr = NULL;
>> ctx->phys = 0;
>> --
>> 2.39.2
>>
>
>