Re: [PATCH v3] media: mtk-vpu: Ensure alignment of 8 for DTCM buffer

From: Dafna Hirschfeld
Date: Mon Sep 20 2021 - 11:19:57 EST




On 01.09.21 10:50, Hans Verkuil wrote:
On 25/08/2021 12:17, Dafna Hirschfeld wrote:
From: Alexandre Courbot <acourbot@xxxxxxxxxxxx>

When running memcpy_toio:
memcpy_toio(send_obj->share_buf, buf, len);
it was found that errors appear if len is not a multiple of 8:

[58.350841] mtk-mdp 14001000.rdma: processing failed: -22

This patch ensures the copy of a multile of 8 size by calling

multile -> multiple

round_up(len, 8) when copying

Fixes: e6599adfad30 ("media: mtk-vpu: avoid unaligned access to DTCM buffer.")
Signed-off-by: Alexandre Courbot <acourbot@xxxxxxxxxxxx>
Signed-off-by: Enric Balletbo i Serra <enric.balletbo@xxxxxxxxxxxxx>
Signed-off-by: Dafna Hirschfeld <dafna.hirschfeld@xxxxxxxxxxxxx>
---
changes since v2:
1. do the extra copy only if len is not multiple of 8

changes since v1:
1. change sign-off-by tags
2. change values to memset

drivers/media/platform/mtk-vpu/mtk_vpu.c | 11 ++++++++++-
1 file changed, 10 insertions(+), 1 deletion(-)

diff --git a/drivers/media/platform/mtk-vpu/mtk_vpu.c b/drivers/media/platform/mtk-vpu/mtk_vpu.c
index ec290dde59cf..658161ee3e4e 100644
--- a/drivers/media/platform/mtk-vpu/mtk_vpu.c
+++ b/drivers/media/platform/mtk-vpu/mtk_vpu.c
@@ -349,7 +349,16 @@ int vpu_ipi_send(struct platform_device *pdev,
}
} while (vpu_cfg_readl(vpu, HOST_TO_VPU));
- memcpy_toio(send_obj->share_buf, buf, len);
+ if (len % 8 != 0) {

You need to add a comment here explaining why this is done (basically what
you also say in the commit log).

Otherwise people would have to dig into the git log to figure out why this
code is the way it is.

Is len often not a multiple of 8? If that's the case, then it might be easier
to just always do the memset/memcpy.

I wrote a program that prints the sizes of all possible messages: http://ix.io/3zsr
compiling it to arm64 gives:

sizeof(mdp_ipi_comm) is 24
sizeof(mdp_ipi_init) is 16

sizeof(venc_ap_ipi_msg_deinit) is 8
sizeof(venc_ap_ipi_msg_enc_ext) is 164
sizeof(venc_ap_ipi_msg_enc) is 32
sizeof(venc_ap_ipi_msg_set_param_ext) is 144
sizeof(venc_ap_ipi_msg_set_param) is 48
sizeof(venc_ap_ipi_msg_init) is 16

sizeof(vdec_ap_ipi_cmd) is 8
sizeof(vdec_ap_ipi_init) is 16
sizeof(vdec_ap_ipi_dec_start) is 24

BUT, when printing the size in the kernel I got "sizeof(mdp_ipi_comm) = 20" and found out
that this is due to the macro #pragma pack(push, 4) in mtk_mdp_ipi.h

so for mdp it makes sens to always do the memset/memcpy but for mtk-vcodec it is not necessary.
In addition, if the message is one of 'venc_ap_ipi_msg_enc_ext' or 'venc_ap_ipi_msg_set_param_ext' then vpu_ipi_send
will fail in the beginning of the function since in those cases we have 'len > sizeof(send_obj->share_buf)'.
so I should send a patch that set SHARE_BUF_SIZE to e.g. 200

I also found out that adding #pragma pack(push, 4) to the definitions of the mtk-vcodec messages has no influence but
maybe this should be added since we copy those messages to hw , so might avoid future bugs.

thanks,
Dafna


Regards,

Hans

+ unsigned char data[SHARE_BUF_SIZE];
+
+ memset(data + len, 0, sizeof(data) - len);
+ memcpy(data, buf, len);
+ memcpy_toio(send_obj->share_buf, data, round_up(len, 8));
+ } else {
+ memcpy_toio(send_obj->share_buf, buf, len);
+ }
+
writel(len, &send_obj->len);
writel(id, &send_obj->id);