Skip to contentSkip to conversation

media: mediatek: vcodec: support video decoder in mt8196

PATCH Superseded Kyrie Wu 3 participants 26 messages Oct 10 – Oct 16

Subsystems (4)

v8 of 9 Superseded Latest is v9 →

lore
Share & tools
lore

Kyrie Wu

author
#0 lore raw

v8 cover media: mediatek: vcodec: support video decoder in mt8196

From: Kyrie Wu (吴晗) <[email protected]>

This patch set adds support for the video decoder on the MediaTek MT8196 platform, leveraging the VCP microprocessor for enhanced performance. It introduces new firmware interfaces for kernel-side communication with VCP, adds MT8196 compatible, codec levels/profiles, and private data. Rewrites the AV1 driver to support extended VSI structures, then change irq table and cdf table size.

This patch set depends on "Add MediaTek VCP remoteproc driver support"[1]

[1] https://patchwork.kernel.org/project/linux-mediatek/cover/[email protected]/

Compliance Test Result:
- v4l2-compliance: 48/49 tests passed, 1 failed (known issue)
- fail: v4l2-test-controls.cpp(981): ret (got 13)
- test VIDIOC_G/S/TRY_EXT_CTRLS: FAIL

Fluster Test Result:
- H264:
- Ran 95/135 tests successfully in 73.540 secs
- H265:
- Ran 142/147 tests successfully in 100.168 secs
- VP9:
- Ran 276/305 tests successfully in 106.804 secs
- AV1:
- Ran 240/242 tests successfully in 23.991 secs

These test results were reported for v7. The v8 change only updates the DT binding subject and description; no tests were rerun for v8.

Changed in v8:
- Rebased on Linux master at 3857c2f.
- Update patch 8/14's subject and describe VCP as the coprocessor running the video decoder firmware, per Rob Herring's review.

Changed in v7:
- Rebased on top of the latest media tree
- Update the MT8196 decoder binding to require VCP, reject SCP, and validate the four fixed hardware clock names on each decoder child node.

Changed in v6:
- Rebase to latest codebase
- Change patch authors and Signed-off-by trailers to Kyrie Wu
- Use safer firmware selection flow for decoder and encoder initialization
- Add VCP IPI id validation and keep invalid messages from stopping the message processing thread
- Fix VCP ACK state handling, stale ACK handling, and interruptible ACK wait retry handling
- Stop the VCP message thread, unregister callbacks, and release the VCP remoteproc reference during cleanup
- Replace VCP readiness busy-wait polling with sleepable polling
- Add VCP encoder capability callback
- Fix MT2110R debug output string
- Update the MT8196 decoder binding commit message according to review
- Keep MT8196 compatible definition before patches that use it
- Fill AV1 reference C plane buffer size

Changed in v5:
- Rebase to latest codebase
- Move vcp init method to driver pdata structures
- change decoder dt-bindings patch's commit message
- MT8189 base on MT8196 patch set

Changed in v4:
- Rebase to latest codebase
- Move dt-bindings and compatible to the after of vcp driver ready
- MT8189 base on MT8196 patch set

Changed in v3:
- Depends on change no update
- Add Compliance and Fluster test result
- Re-write the commit message for patch 12
- Change the patches according to review suggestion for patch: 3/4/5/6/14

Changed in v2:
- re-write the commit message for patch 1

Kyrie Wu (14):
media: mediatek: vcodec: support vcp architecture
media: mediatek: vcodec: add driver to support vcp
media: mediatek: vcodec: add driver to support vcp encoder
media: mediatek: vcodec: get different firmware ipi id
media: mediatek: vcodec: get share memory address
media: mediatek: vcodec: add debug information
media: mediatek: vcodec: send share memory address to vcp
dt-bindings: media: mediatek,vcodec-subdev-decoder: Add MT8196
media: mediatek: vcodec: add decoder compatible to support mt8196
media: mediatek: vcodec: define MT8196 vcodec levels.
media: mediatek: vcodec: support 36bit iova address
media: mediatek: vcodec: clean xpc status
media: mediatek: decoder: fill av1 buffer size with picinfo
media: mediatek: decoder: support av1 extend vsi

 .../media/mediatek,vcodec-subdev-decoder.yaml |  49 +-
 .../media/platform/mediatek/vcodec/Kconfig    |   4 +
 .../platform/mediatek/vcodec/common/Makefile  |   4 +
 .../mediatek/vcodec/common/mtk_vcodec_dbgfs.c |  21 +-
 .../mediatek/vcodec/common/mtk_vcodec_fw.c    |  33 +-
 .../mediatek/vcodec/common/mtk_vcodec_fw.h    |   8 +-
 .../vcodec/common/mtk_vcodec_fw_priv.h        |  12 +
 .../vcodec/common/mtk_vcodec_fw_scp.c         |   1 +
 .../vcodec/common/mtk_vcodec_fw_vcp.c         | 631 ++++++++++++++++++
 .../vcodec/common/mtk_vcodec_fw_vcp.h         | 166 +++++
 .../vcodec/common/mtk_vcodec_fw_vpu.c         |   1 +
 .../vcodec/decoder/mtk_vcodec_dec_drv.c       |  21 +-
 .../vcodec/decoder/mtk_vcodec_dec_drv.h       |   3 +
 .../vcodec/decoder/mtk_vcodec_dec_hw.c        |  31 +
 .../vcodec/decoder/mtk_vcodec_dec_hw.h        |  13 +-
 .../vcodec/decoder/mtk_vcodec_dec_stateless.c |   6 +
 .../vcodec/decoder/vdec/vdec_av1_req_lat_if.c | 114 +++-
 .../decoder/vdec/vdec_h264_req_multi_if.c     |  11 +-
 .../decoder/vdec/vdec_hevc_req_multi_if.c     |  12 +-
 .../vcodec/decoder/vdec/vdec_vp8_req_if.c     |   5 +-
 .../vcodec/decoder/vdec/vdec_vp9_req_lat_if.c |  27 +-
 .../mediatek/vcodec/decoder/vdec_ipi_msg.h    |   2 +
 .../mediatek/vcodec/decoder/vdec_vpu_if.c     |  12 +-
 .../mediatek/vcodec/encoder/mtk_vcodec_enc.c  |   1 -
 .../mediatek/vcodec/encoder/mtk_vcodec_enc.h  |   2 +
 .../vcodec/encoder/mtk_vcodec_enc_drv.c       |   4 +-
 .../vcodec/encoder/mtk_vcodec_enc_drv.h       |   2 +
 27 files changed, 1140 insertions(+), 56 deletions(-)
 create mode 100644 drivers/media/platform/mediatek/vcodec/common/mtk_vcodec_fw_vcp.c
 create mode 100644 drivers/media/platform/mediatek/vcodec/common/mtk_vcodec_fw_vcp.h
author
#1 lore raw

PATCH v8 1/14 media: mediatek: vcodec: support vcp architecture

Some platforms expose the video codec through the VCP coprocessor. Use the VCP architecture when the VCP coprocessor is found.

---
 drivers/media/platform/mediatek/vcodec/common/mtk_vcodec_fw.h  | 1 +
 .../platform/mediatek/vcodec/decoder/mtk_vcodec_dec_drv.c      | 3 +++
 2 files changed, 4 insertions(+)
drivers/media/platform/mediatek/vcodec/common/mtk_vcodec_fw.h +1 -0
index 300363a4..c1642fb0 100644
@@ -14,6 +14,7 @@ struct mtk_vcodec_enc_dev;
 enum mtk_vcodec_fw_type {
 	VPU,
 	SCP,
+	VCP,
 };
 
 enum mtk_vcodec_fw_use {
drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_drv.c +3 -0
index e936ed8d..d220b645 100644
@@ -379,6 +379,9 @@ static int mtk_vcodec_probe(struct platform_device *pdev)
 	} else if (!of_property_read_u32(pdev->dev.of_node, "mediatek,scp",
 					 &rproc_phandle)) {
 		fw_type = SCP;
+	} else if (!of_property_read_u32(pdev->dev.of_node, "mediatek,vcp",
+					 &rproc_phandle)) {
+		fw_type = VCP;
 	} else {
 		dev_dbg(&pdev->dev, "Could not get vdec IPI device");
 		return -ENODEV;
author
#4 lore raw

PATCH v8 3/14 media: mediatek: vcodec: add driver to support vcp encoder

In reply to Kyrie Wu

Encoder also need to call vcp interface to communicate with vcp, add driver to support encoder.

---
 .../vcodec/common/mtk_vcodec_fw_vcp.c         | 52 ++++++++++++++++---
 .../vcodec/common/mtk_vcodec_fw_vcp.h         |  1 +
 .../mediatek/vcodec/encoder/mtk_vcodec_enc.c  |  1 -
 .../mediatek/vcodec/encoder/mtk_vcodec_enc.h  |  2 +
 4 files changed, 47 insertions(+), 9 deletions(-)
drivers/media/platform/mediatek/vcodec/common/mtk_vcodec_fw_vcp.c +44 -8
index 150d842c..ece4e3e8 100644
@@ -13,6 +13,8 @@
 
 #include "../decoder/mtk_vcodec_dec_drv.h"
 #include "../decoder/vdec_ipi_msg.h"
+#include "../encoder/mtk_vcodec_enc.h"
+#include "../encoder/mtk_vcodec_enc_drv.h"
 #include "mtk_vcodec_fw_priv.h"
 
 #define IPI_SEND_TIMEOUT_MS  100U
@@ -223,8 +225,13 @@ static int mtk_vcodec_vcp_msg_ipi_send(struct mtk_vcodec_fw *fw, int id, void *b
 	}
 
 	mutex_lock(&vcp->ipi_mutex);
-	feature_id = VDEC_FEATURE_ID;
-	mailbox_id = IPI_OUT_VDEC_1;
+	if (fw->fw_use == ENCODER) {
+		feature_id = VENC_FEATURE_ID;
+		mailbox_id = IPI_OUT_VENC_0;
+	} else {
+		feature_id = VDEC_FEATURE_ID;
+		mailbox_id = IPI_OUT_VDEC_1;
+	}
 
 	timeout_jiffies = jiffies + msecs_to_jiffies(VCP_SYNC_TIMEOUT_MS);
 	while (!vcp_device->ops->vcp_is_ready(vcp_device, feature_id)) {
@@ -348,7 +355,7 @@ static void mtk_vcodec_vcp_put_device(struct mtk_vcodec_fw *fw)
 static int mtk_vcodec_vcp_load_firmware(struct mtk_vcodec_fw *fw)
 {
 	struct mtk_vcp_device *vcp_device;
-	int ret, feature_id, mem_id, mailbox_id, ipi_id;
+	int ret, feature_id, mem_id, mailbox_id;
 	int i;
 
 	if (fw->vcp->is_init_done) {
@@ -363,10 +370,15 @@ static int mtk_vcodec_vcp_load_firmware(struct mtk_vcodec_fw *fw)
 
 	vcp_device = fw->vcp->vcp_device;
 
-	feature_id = VDEC_FEATURE_ID;
-	mem_id = VDEC_MEM_ID;
-	mailbox_id = IPI_IN_VDEC_1;
-	ipi_id = VCP_IPI_LAT_DECODER;
+	if (fw->fw_use == ENCODER) {
+		feature_id = VENC_FEATURE_ID;
+		mem_id = VENC_MEM_ID;
+		mailbox_id = IPI_IN_VENC_0;
+	} else {
+		feature_id = VDEC_FEATURE_ID;
+		mem_id = VDEC_MEM_ID;
+		mailbox_id = IPI_IN_VDEC_1;
+	}
 
 	ret = mtk_vcp_mbox_ipc_register(vcp_get_ipidev(vcp_device), mailbox_id,
 					mtk_vcodec_vcp_msg_ack_isr, fw, &fw->vcp->share_data);
@@ -402,6 +414,26 @@ static int mtk_vcodec_vcp_load_firmware(struct mtk_vcodec_fw *fw)
 		mutex_init(&fw->vcp->ipi_desc[i].lock);
 	mutex_init(&fw->vcp->ipi_mutex);
 
+	if (fw->fw_use == ENCODER) {
+		init_waitqueue_head(&fw->vcp->msg_wq[VCP_IPI_ENCODER]);
+		fw->vcp->msg_thread =
+			kthread_run(mtk_vcodec_vcp_msg_process_thread, fw, "vcp_enc_msq_thread");
+		if (IS_ERR(fw->vcp->msg_thread)) {
+			ret = PTR_ERR(fw->vcp->msg_thread);
+			fw->vcp->msg_thread = NULL;
+			goto err_deregister_feature;
+		}
+
+		fw->vcp->vsi_addr = vcp_device->ops->get_mem_virt(vcp_device, mem_id);
+		fw->vcp->vsi_size = vcp_device->ops->get_mem_size(vcp_device, mem_id);
+		fw->vcp->iova_addr = vcp_device->ops->get_mem_iova(vcp_device, mem_id);
+
+		dev_dbg(&fw->pdev->dev, "enc vcp init done => va: %p size:0x%x iova:%pad.\n",
+			fw->vcp->vsi_addr, fw->vcp->vsi_size, &fw->vcp->iova_addr);
+
+		return 0;
+	}
+
 	init_waitqueue_head(&fw->vcp->msg_wq[VCP_IPI_LAT_DECODER]);
 	init_waitqueue_head(&fw->vcp->msg_wq[VCP_IPI_CORE_DECODER]);
 	fw->vcp->msg_thread =
@@ -530,7 +562,11 @@ struct mtk_vcodec_fw *mtk_vcodec_fw_vcp_init(void *priv, enum mtk_vcodec_fw_use
 	struct mtk_vcodec_fw *fw;
 	int i;
 
-	if (fw_use == DECODER) {
+	if (fw_use == ENCODER) {
+		struct mtk_vcodec_enc_dev *enc_dev = priv;
+
+		plat_dev = enc_dev->plat_dev;
+	} else if (fw_use == DECODER) {
 		struct mtk_vcodec_dec_dev *dec_dev = priv;
 
 		plat_dev = dec_dev->plat_dev;
drivers/media/platform/mediatek/vcodec/common/mtk_vcodec_fw_vcp.h +1 -0
index 83742096..97ba642d 100644
@@ -16,6 +16,7 @@ typedef void (*vcp_ipi_handler_t) (void *data, unsigned int len, void *priv);
 
 /* enum mtk_vcp_ipi_index - index used to separate different hardware */
 enum mtk_vcp_ipi_index {
+	VCP_IPI_ENCODER,
 	VCP_IPI_LAT_DECODER,
 	VCP_IPI_CORE_DECODER,
 	VCP_IPI_MAX,
drivers/media/platform/mediatek/vcodec/encoder/mtk_vcodec_enc.c +0 -1
index 0d4e9446..48cb5dde 100644
@@ -26,7 +26,6 @@
 
 #define MTK_DEFAULT_FRAMERATE_NUM 1001
 #define MTK_DEFAULT_FRAMERATE_DENOM 30000
-#define MTK_VENC_4K_CAPABILITY_ENABLE BIT(0)
 
 static void mtk_venc_worker(struct work_struct *work);
 
drivers/media/platform/mediatek/vcodec/encoder/mtk_vcodec_enc.h +2 -0
index 908d8179..84156c10 100644
@@ -23,6 +23,8 @@
 #define MTK_VENC_IRQ_STATUS_OFFSET	0x05C
 #define MTK_VENC_IRQ_ACK_OFFSET	0x060
 
+#define MTK_VENC_4K_CAPABILITY_ENABLE BIT(0)
+
 /**
  * struct mtk_video_enc_buf - Private data related to each VB2 buffer.
  * @m2m_buf:	M2M buffer
author
#2 lore raw

PATCH v8 2/14 media: mediatek: vcodec: add driver to support vcp

In reply to Kyrie Wu

The processor is changed from scp to vcp in mt8196 platform. Adding new firmware interface to communicate kernel with vcp for the communication method is changed.

---
 .../media/platform/mediatek/vcodec/Kconfig    |   4 +
 .../platform/mediatek/vcodec/common/Makefile  |   4 +
 .../mediatek/vcodec/common/mtk_vcodec_fw.c    |  23 +-
 .../mediatek/vcodec/common/mtk_vcodec_fw.h    |   6 +-
 .../vcodec/common/mtk_vcodec_fw_priv.h        |  12 +
 .../vcodec/common/mtk_vcodec_fw_scp.c         |   1 +
 .../vcodec/common/mtk_vcodec_fw_vcp.c         | 571 ++++++++++++++++++
 .../vcodec/common/mtk_vcodec_fw_vcp.h         | 152 +++++
 .../vcodec/common/mtk_vcodec_fw_vpu.c         |   1 +
 .../vcodec/decoder/mtk_vcodec_dec_drv.c       |   5 +-
 .../vcodec/decoder/mtk_vcodec_dec_drv.h       |   2 +
 .../vcodec/encoder/mtk_vcodec_enc_drv.c       |   4 +-
 .../vcodec/encoder/mtk_vcodec_enc_drv.h       |   2 +
 13 files changed, 768 insertions(+), 19 deletions(-)
 create mode 100644 drivers/media/platform/mediatek/vcodec/common/mtk_vcodec_fw_vcp.c
 create mode 100644 drivers/media/platform/mediatek/vcodec/common/mtk_vcodec_fw_vcp.h
drivers/media/platform/mediatek/vcodec/Kconfig +4 -0
index bc829223..d23dad5c 100644
@@ -1,4 +1,7 @@
 # SPDX-License-Identifier: GPL-2.0-only
+config VIDEO_MEDIATEK_VCODEC_VCP
+	bool
+
 config VIDEO_MEDIATEK_VCODEC_SCP
 	bool
 
@@ -21,6 +24,7 @@ config VIDEO_MEDIATEK_VCODEC
 	select V4L2_MEM2MEM_DEV
 	select VIDEO_MEDIATEK_VCODEC_VPU if VIDEO_MEDIATEK_VPU
 	select VIDEO_MEDIATEK_VCODEC_SCP if MTK_SCP
+	select VIDEO_MEDIATEK_VCODEC_VCP if MTK_VCP_RPROC
 	select V4L2_H264
 	select V4L2_VP9
 	select MEDIA_CONTROLLER
drivers/media/platform/mediatek/vcodec/common/Makefile +4 -0
index d0479914..2f68692e 100644
@@ -14,6 +14,10 @@ ifneq ($(CONFIG_VIDEO_MEDIATEK_VCODEC_SCP),)
 mtk-vcodec-common-y += mtk_vcodec_fw_scp.o
 endif
 
+ifneq ($(CONFIG_VIDEO_MEDIATEK_VCODEC_VCP),)
+mtk-vcodec-common-y += mtk_vcodec_fw_vcp.o
+endif
+
 ifneq ($(CONFIG_DEBUG_FS),)
 obj-$(CONFIG_VIDEO_MEDIATEK_VCODEC) += mtk-vcodec-dbgfs.o
 
drivers/media/platform/mediatek/vcodec/common/mtk_vcodec_fw.c +8 -15
index 08949b08..552d0d8a 100644
@@ -4,25 +4,18 @@
 #include "../encoder/mtk_vcodec_enc_drv.h"
 #include "mtk_vcodec_fw_priv.h"
 
-struct mtk_vcodec_fw *mtk_vcodec_fw_select(void *priv, enum mtk_vcodec_fw_type type,
-					   enum mtk_vcodec_fw_use fw_use)
+struct mtk_vcodec_fw *mtk_vcodec_fw_select(void *priv, enum mtk_vcodec_fw_use fw_use,
+					   mtk_vcodec_fw_init_func fw_init)
 {
-	struct platform_device *plat_dev;
-
-	if (fw_use == ENCODER)
-		plat_dev = ((struct mtk_vcodec_enc_dev *)priv)->plat_dev;
-	else
-		plat_dev = ((struct mtk_vcodec_dec_dev *)priv)->plat_dev;
+	if (!fw_init)
+		return ERR_PTR(-EINVAL);
 
-	switch (type) {
-	case VPU:
-		return mtk_vcodec_fw_vpu_init(priv, fw_use);
-	case SCP:
-		return mtk_vcodec_fw_scp_init(priv, fw_use);
-	default:
-		dev_err(&plat_dev->dev, "Invalid vcodec fw type");
+	if (fw_use != ENCODER && fw_use != DECODER) {
+		pr_err("Invalid firmware use %d\n", fw_use);
 		return ERR_PTR(-EINVAL);
 	}
+
+	return fw_init(priv, fw_use);
 }
 EXPORT_SYMBOL_GPL(mtk_vcodec_fw_select);
 
drivers/media/platform/mediatek/vcodec/common/mtk_vcodec_fw.h +4 -2
index c1642fb0..50d93d47 100644
@@ -24,11 +24,13 @@ enum mtk_vcodec_fw_use {
 
 struct mtk_vcodec_fw;
 
+typedef struct mtk_vcodec_fw *(*mtk_vcodec_fw_init_func)(void *priv,
+							 enum mtk_vcodec_fw_use fw_use);
 typedef void (*mtk_vcodec_ipi_handler) (void *data,
 	unsigned int len, void *priv);
 
-struct mtk_vcodec_fw *mtk_vcodec_fw_select(void *priv, enum mtk_vcodec_fw_type type,
-					   enum mtk_vcodec_fw_use fw_use);
+struct mtk_vcodec_fw *mtk_vcodec_fw_select(void *priv, enum mtk_vcodec_fw_use fw_use,
+					   mtk_vcodec_fw_init_func fw_init);
 void mtk_vcodec_fw_release(struct mtk_vcodec_fw *fw);
 
 int mtk_vcodec_fw_load_firmware(struct mtk_vcodec_fw *fw);
drivers/media/platform/mediatek/vcodec/common/mtk_vcodec_fw_priv.h +12 -0
index 99603acc..0a2a9b01 100644
@@ -4,6 +4,7 @@
 #define _MTK_VCODEC_FW_PRIV_H_
 
 #include "mtk_vcodec_fw.h"
+#include "mtk_vcodec_fw_vcp.h"
 
 struct mtk_vcodec_dec_dev;
 struct mtk_vcodec_enc_dev;
@@ -13,6 +14,7 @@ struct mtk_vcodec_fw {
 	const struct mtk_vcodec_fw_ops *ops;
 	struct platform_device *pdev;
 	struct mtk_scp *scp;
+	struct mtk_vcp *vcp;
 	enum mtk_vcodec_fw_use fw_use;
 };
 
@@ -49,4 +51,14 @@ mtk_vcodec_fw_scp_init(void *priv, enum mtk_vcodec_fw_use fw_use)
 }
 #endif /* CONFIG_VIDEO_MEDIATEK_VCODEC_SCP */
 
+#if IS_ENABLED(CONFIG_VIDEO_MEDIATEK_VCODEC_VCP)
+struct mtk_vcodec_fw *mtk_vcodec_fw_vcp_init(void *priv, enum mtk_vcodec_fw_use fw_use);
+#else
+static inline struct mtk_vcodec_fw *
+mtk_vcodec_fw_vcp_init(void *priv, enum mtk_vcodec_fw_use fw_use)
+{
+	return ERR_PTR(-ENODEV);
+}
+#endif /* CONFIG_VIDEO_MEDIATEK_VCODEC_VCP */
+
 #endif /* _MTK_VCODEC_FW_PRIV_H_ */
drivers/media/platform/mediatek/vcodec/common/mtk_vcodec_fw_scp.c +1 -0
index 1b0bc473..1aad5c3e 100644
@@ -90,3 +90,4 @@ struct mtk_vcodec_fw *mtk_vcodec_fw_scp_init(void *priv, enum mtk_vcodec_fw_use
 
 	return fw;
 }
+EXPORT_SYMBOL_GPL(mtk_vcodec_fw_scp_init);
drivers/media/platform/mediatek/vcodec/common/mtk_vcodec_fw_vcp.c new file +571 -0

571 lines not on this page · raw message

drivers/media/platform/mediatek/vcodec/common/mtk_vcodec_fw_vcp.h new file +152 -0

152 lines not on this page · raw message

drivers/media/platform/mediatek/vcodec/common/mtk_vcodec_fw_vpu.c +1 -0
index 3632037f..41643db9 100644
@@ -130,3 +130,4 @@ struct mtk_vcodec_fw *mtk_vcodec_fw_vpu_init(void *priv, enum mtk_vcodec_fw_use
 
 	return fw;
 }
+EXPORT_SYMBOL_GPL(mtk_vcodec_fw_vpu_init);
drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_drv.c +4 -1
index d220b645..ab5f6c01 100644
@@ -376,19 +376,22 @@ static int mtk_vcodec_probe(struct platform_device *pdev)
 	if (!of_property_read_u32(pdev->dev.of_node, "mediatek,vpu",
 				  &rproc_phandle)) {
 		fw_type = VPU;
+		dev->fw_init = mtk_vcodec_fw_vpu_init;
 	} else if (!of_property_read_u32(pdev->dev.of_node, "mediatek,scp",
 					 &rproc_phandle)) {
 		fw_type = SCP;
+		dev->fw_init = mtk_vcodec_fw_scp_init;
 	} else if (!of_property_read_u32(pdev->dev.of_node, "mediatek,vcp",
 					 &rproc_phandle)) {
 		fw_type = VCP;
+		dev->fw_init = mtk_vcodec_fw_vcp_init;
 	} else {
 		dev_dbg(&pdev->dev, "Could not get vdec IPI device");
 		return -ENODEV;
 	}
 	dma_set_max_seg_size(&pdev->dev, UINT_MAX);
 
-	dev->fw_handler = mtk_vcodec_fw_select(dev, fw_type, DECODER);
+	dev->fw_handler = mtk_vcodec_fw_select(dev, DECODER, dev->fw_init);
 	if (IS_ERR(dev->fw_handler))
 		return PTR_ERR(dev->fw_handler);
 
drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_drv.h +2 -0
index c9d27534..15e43732 100644
@@ -251,6 +251,7 @@ struct mtk_vcodec_dec_ctx {
  * @vdecsys_regmap: VDEC_SYS register space passed through syscon
  *
  * @fw_handler: used to communicate with the firmware.
+ * @fw_init: firmware-specific init callback selected at probe time
  * @id_counter: used to identify current opened instance
  *
  * @dec_mutex: decoder hardware lock
@@ -292,6 +293,7 @@ struct mtk_vcodec_dec_dev {
 	struct regmap *vdecsys_regmap;
 
 	struct mtk_vcodec_fw *fw_handler;
+	struct mtk_vcodec_fw *(*fw_init)(void *priv, enum mtk_vcodec_fw_use fw_use);
 	u64 id_counter;
 
 	/* decoder hardware mutex lock */
drivers/media/platform/mediatek/vcodec/encoder/mtk_vcodec_enc_drv.c +3 -1
index 4e4541b2..811bc62a 100644
@@ -259,16 +259,18 @@ static int mtk_vcodec_probe(struct platform_device *pdev)
 	if (!of_property_read_u32(pdev->dev.of_node, "mediatek,vpu",
 				  &rproc_phandle)) {
 		fw_type = VPU;
+		dev->fw_init = mtk_vcodec_fw_vpu_init;
 	} else if (!of_property_read_u32(pdev->dev.of_node, "mediatek,scp",
 					 &rproc_phandle)) {
 		fw_type = SCP;
+		dev->fw_init = mtk_vcodec_fw_scp_init;
 	} else {
 		dev_err(&pdev->dev, "[MTK VCODEC] Could not get venc IPI device");
 		return -ENODEV;
 	}
 	dma_set_max_seg_size(&pdev->dev, UINT_MAX);
 
-	dev->fw_handler = mtk_vcodec_fw_select(dev, fw_type, ENCODER);
+	dev->fw_handler = mtk_vcodec_fw_select(dev, ENCODER, dev->fw_init);
 	if (IS_ERR(dev->fw_handler))
 		return PTR_ERR(dev->fw_handler);
 
drivers/media/platform/mediatek/vcodec/encoder/mtk_vcodec_enc_drv.h +2 -0
index 0cddfa13..934ff648 100644
@@ -174,6 +174,7 @@ struct mtk_vcodec_enc_ctx {
  * @venc_pdata: encoder IC-specific data
  *
  * @fw_handler: used to communicate with the firmware.
+ * @fw_init: firmware-specific init callback selected at probe time
  * @id_counter: used to identify current opened instance
  *
  * @enc_mutex: encoder hardware lock.
@@ -201,6 +202,7 @@ struct mtk_vcodec_enc_dev {
 	const struct mtk_vcodec_enc_pdata *venc_pdata;
 
 	struct mtk_vcodec_fw *fw_handler;
+	struct mtk_vcodec_fw *(*fw_init)(void *priv, enum mtk_vcodec_fw_use fw_use);
 	u64 id_counter;
 
 	/* encoder hardware mutex lock */
author
#6 lore raw

PATCH v8 4/14 media: mediatek: vcodec: get different firmware ipi id

In reply to Kyrie Wu

Getting ipi(inter-processor interrupt) id according to firmware type and hardware index for different architecture.

---
 .../mediatek/vcodec/common/mtk_vcodec_fw.c         | 14 ++++++++++++++
 .../mediatek/vcodec/common/mtk_vcodec_fw.h         |  1 +
 .../vcodec/decoder/vdec/vdec_av1_req_lat_if.c      |  5 +++--
 .../vcodec/decoder/vdec/vdec_h264_req_multi_if.c   |  5 +++--
 .../vcodec/decoder/vdec/vdec_hevc_req_multi_if.c   |  5 +++--
 .../mediatek/vcodec/decoder/vdec/vdec_vp8_req_if.c |  5 +++--
 .../vcodec/decoder/vdec/vdec_vp9_req_lat_if.c      |  5 +++--
 7 files changed, 30 insertions(+), 10 deletions(-)
drivers/media/platform/mediatek/vcodec/common/mtk_vcodec_fw.c +14 -0
index 552d0d8a..8630e871 100644
@@ -3,6 +3,20 @@
 #include "../decoder/mtk_vcodec_dec_drv.h"
 #include "../encoder/mtk_vcodec_enc_drv.h"
 #include "mtk_vcodec_fw_priv.h"
+#include "mtk_vcodec_fw_vcp.h"
+
+int mtk_vcodec_fw_get_ipi(enum mtk_vcodec_fw_type type, int hw_id)
+{
+	switch (type) {
+	case SCP:
+		return hw_id == MTK_VDEC_LAT0 ? SCP_IPI_VDEC_LAT : SCP_IPI_VDEC_CORE;
+	case VCP:
+		return hw_id == MTK_VDEC_LAT0 ? VCP_IPI_LAT_DECODER : VCP_IPI_CORE_DECODER;
+	default:
+		return -EINVAL;
+	}
+}
+EXPORT_SYMBOL_GPL(mtk_vcodec_fw_get_ipi);
 
 struct mtk_vcodec_fw *mtk_vcodec_fw_select(void *priv, enum mtk_vcodec_fw_use fw_use,
 					   mtk_vcodec_fw_init_func fw_init)
drivers/media/platform/mediatek/vcodec/common/mtk_vcodec_fw.h +1 -0
index 50d93d47..005ac996 100644
@@ -43,5 +43,6 @@ int mtk_vcodec_fw_ipi_register(struct mtk_vcodec_fw *fw, int id,
 int mtk_vcodec_fw_ipi_send(struct mtk_vcodec_fw *fw, int id,
 			   void *buf, unsigned int len, unsigned int wait);
 int mtk_vcodec_fw_get_type(struct mtk_vcodec_fw *fw);
+int mtk_vcodec_fw_get_ipi(enum mtk_vcodec_fw_type type, int hw_id);
 
 #endif /* _MTK_VCODEC_FW_H_ */
drivers/media/platform/mediatek/vcodec/decoder/vdec/vdec_av1_req_lat_if.c +3 -2
index 49d9b4a7..84d28a0b 100644
@@ -1878,6 +1878,7 @@ static int vdec_av1_slice_init(struct mtk_vcodec_dec_ctx *ctx)
 {
 	struct vdec_av1_slice_instance *instance;
 	struct vdec_av1_slice_init_vsi *vsi;
+	enum mtk_vcodec_fw_type fw_type = ctx->dev->fw_handler->type;
 	int ret;
 
 	instance = kzalloc_obj(*instance);
@@ -1885,8 +1886,8 @@ static int vdec_av1_slice_init(struct mtk_vcodec_dec_ctx *ctx)
 		return -ENOMEM;
 
 	instance->ctx = ctx;
-	instance->vpu.id = SCP_IPI_VDEC_LAT;
-	instance->vpu.core_id = SCP_IPI_VDEC_CORE;
+	instance->vpu.id = mtk_vcodec_fw_get_ipi(fw_type, MTK_VDEC_LAT0);
+	instance->vpu.core_id = mtk_vcodec_fw_get_ipi(fw_type, MTK_VDEC_CORE);
 	instance->vpu.ctx = ctx;
 	instance->vpu.codec_type = ctx->current_codec;
 
drivers/media/platform/mediatek/vcodec/decoder/vdec/vdec_h264_req_multi_if.c +3 -2
index 10359ce9..69d60717 100644
@@ -1204,6 +1204,7 @@ static int vdec_h264_slice_single_decode(void *h_vdec, struct mtk_vcodec_mem *bs
 
 static int vdec_h264_slice_init(struct mtk_vcodec_dec_ctx *ctx)
 {
+	enum mtk_vcodec_fw_type fw_type = ctx->dev->fw_handler->type;
 	struct vdec_h264_slice_inst *inst;
 	int err, vsi_size;
 	unsigned char *temp;
@@ -1214,8 +1215,8 @@ static int vdec_h264_slice_init(struct mtk_vcodec_dec_ctx *ctx)
 
 	inst->ctx = ctx;
 
-	inst->vpu.id = SCP_IPI_VDEC_LAT;
-	inst->vpu.core_id = SCP_IPI_VDEC_CORE;
+	inst->vpu.id = mtk_vcodec_fw_get_ipi(fw_type, MTK_VDEC_LAT0);
+	inst->vpu.core_id = mtk_vcodec_fw_get_ipi(fw_type, MTK_VDEC_CORE);
 	inst->vpu.ctx = ctx;
 	inst->vpu.codec_type = ctx->current_codec;
 	inst->vpu.capture_type = ctx->capture_fourcc;
drivers/media/platform/mediatek/vcodec/decoder/vdec/vdec_hevc_req_multi_if.c +3 -2
index 02f39954..dd638ef4 100644
@@ -855,6 +855,7 @@ static int vdec_hevc_slice_setup_core_buffer(struct vdec_hevc_slice_inst *inst,
 
 static int vdec_hevc_slice_init(struct mtk_vcodec_dec_ctx *ctx)
 {
+	enum mtk_vcodec_fw_type fw_type = ctx->dev->fw_handler->type;
 	struct vdec_hevc_slice_inst *inst;
 	int err, vsi_size;
 
@@ -864,8 +865,8 @@ static int vdec_hevc_slice_init(struct mtk_vcodec_dec_ctx *ctx)
 
 	inst->ctx = ctx;
 
-	inst->vpu.id = SCP_IPI_VDEC_LAT;
-	inst->vpu.core_id = SCP_IPI_VDEC_CORE;
+	inst->vpu.id = mtk_vcodec_fw_get_ipi(fw_type, MTK_VDEC_LAT0);
+	inst->vpu.core_id = mtk_vcodec_fw_get_ipi(fw_type, MTK_VDEC_CORE);
 	inst->vpu.ctx = ctx;
 	inst->vpu.codec_type = ctx->current_codec;
 	inst->vpu.capture_type = ctx->capture_fourcc;
drivers/media/platform/mediatek/vcodec/decoder/vdec/vdec_vp8_req_if.c +3 -2
index 391e789a..d65e276f 100644
@@ -272,6 +272,7 @@ static int vdec_vp8_slice_get_decode_parameters(struct vdec_vp8_slice_inst *inst
 
 static int vdec_vp8_slice_init(struct mtk_vcodec_dec_ctx *ctx)
 {
+	enum mtk_vcodec_fw_type fw_type = ctx->dev->fw_handler->type;
 	struct vdec_vp8_slice_inst *inst;
 	int err;
 
@@ -281,8 +282,8 @@ static int vdec_vp8_slice_init(struct mtk_vcodec_dec_ctx *ctx)
 
 	inst->ctx = ctx;
 
-	inst->vpu.id = SCP_IPI_VDEC_LAT;
-	inst->vpu.core_id = SCP_IPI_VDEC_CORE;
+	inst->vpu.id = mtk_vcodec_fw_get_ipi(fw_type, MTK_VDEC_LAT0);
+	inst->vpu.core_id = mtk_vcodec_fw_get_ipi(fw_type, MTK_VDEC_CORE);
 	inst->vpu.ctx = ctx;
 	inst->vpu.codec_type = ctx->current_codec;
 	inst->vpu.capture_type = ctx->capture_fourcc;
drivers/media/platform/mediatek/vcodec/decoder/vdec/vdec_vp9_req_lat_if.c +3 -2
index ba7a1cd9..b4926867 100644
@@ -1833,6 +1833,7 @@ static int vdec_vp9_slice_update_core(struct vdec_vp9_slice_instance *instance,
 
 static int vdec_vp9_slice_init(struct mtk_vcodec_dec_ctx *ctx)
 {
+	enum mtk_vcodec_fw_type fw_type = ctx->dev->fw_handler->type;
 	struct vdec_vp9_slice_instance *instance;
 	struct vdec_vp9_slice_init_vsi *vsi;
 	int ret;
@@ -1842,8 +1843,8 @@ static int vdec_vp9_slice_init(struct mtk_vcodec_dec_ctx *ctx)
 		return -ENOMEM;
 
 	instance->ctx = ctx;
-	instance->vpu.id = SCP_IPI_VDEC_LAT;
-	instance->vpu.core_id = SCP_IPI_VDEC_CORE;
+	instance->vpu.id = mtk_vcodec_fw_get_ipi(fw_type, MTK_VDEC_LAT0);
+	instance->vpu.core_id = mtk_vcodec_fw_get_ipi(fw_type, MTK_VDEC_CORE);
 	instance->vpu.ctx = ctx;
 	instance->vpu.codec_type = ctx->current_codec;
 
author
#12 lore raw

PATCH v8 7/14 media: mediatek: vcodec: send share memory address to vcp

In reply to Kyrie Wu

The share memory is allocated in kernel for vcp architecture, it's different with vpu which share memors is reserved in vpu micro processor. Need to send share memory address to vcp.

---
 drivers/media/platform/mediatek/vcodec/decoder/vdec_ipi_msg.h | 2 ++
 drivers/media/platform/mediatek/vcodec/decoder/vdec_vpu_if.c  | 2 ++
 2 files changed, 4 insertions(+)
drivers/media/platform/mediatek/vcodec/decoder/vdec_ipi_msg.h +2 -0
index 47070be2..097561a1 100644
@@ -67,11 +67,13 @@ struct vdec_vpu_ipi_ack {
  * @msg_id	: AP_IPIMSG_DEC_INIT
  * @codec_type	: codec fourcc
  * @ap_inst_addr	: AP video decoder instance address
+ * @shared_iova	: reserved share memory address
  */
 struct vdec_ap_ipi_init {
 	uint32_t msg_id;
 	u32 codec_type;
 	uint64_t ap_inst_addr;
+	u64 shared_iova;
 };
 
 /**
drivers/media/platform/mediatek/vcodec/decoder/vdec_vpu_if.c +2 -0
index cdb673e6..3a10b32b 100644
@@ -236,6 +236,8 @@ int vpu_dec_init(struct vdec_vpu_inst *vpu)
 	msg.msg_id = AP_IPIMSG_DEC_INIT;
 	msg.ap_inst_addr = (unsigned long)vpu;
 	msg.codec_type = vpu->codec_type;
+	if (mtk_vcodec_fw_get_type(vpu->ctx->dev->fw_handler) == VCP)
+		msg.shared_iova = vpu->ctx->dev->fw_handler->vcp->iova_addr;
 
 	mtk_vdec_debug(vpu->ctx, "vdec_inst=%p", vpu);
 
author
#8 lore raw

PATCH v8 5/14 media: mediatek: vcodec: get share memory address

In reply to Kyrie Wu

There is only one share memory for vcp architecture, need to divide it into many different functions.

---
 .../vcodec/common/mtk_vcodec_fw_vcp.c         | 26 +++++++++++++-
 .../vcodec/common/mtk_vcodec_fw_vcp.h         | 13 +++++++
 .../vcodec/decoder/vdec/vdec_av1_req_lat_if.c | 35 ++++++++++++++++---
 .../decoder/vdec/vdec_h264_req_multi_if.c     |  6 +++-
 .../decoder/vdec/vdec_hevc_req_multi_if.c     |  7 ++--
 .../vcodec/decoder/vdec/vdec_vp9_req_lat_if.c | 22 ++++++++++--
 .../mediatek/vcodec/decoder/vdec_vpu_if.c     | 10 +++++-
 7 files changed, 108 insertions(+), 11 deletions(-)
drivers/media/platform/mediatek/vcodec/common/mtk_vcodec_fw_vcp.c +25 -1
index ece4e3e8..468c4bf4 100644
@@ -483,8 +483,31 @@ static unsigned int mtk_vcodec_vcp_get_vdec_capa(struct mtk_vcodec_fw *fw)
 	       MTK_VDEC_IS_SUPPORT_10BIT | MTK_VDEC_IS_SUPPORT_EXT;
 }
 
-static void *mtk_vcodec_vcp_dm_addr(struct mtk_vcodec_fw *fw, u32 dtcm_dmem_addr)
+static unsigned int mtk_vcodec_vcp_get_venc_capa(struct mtk_vcodec_fw *fw)
 {
+	return 0;
+}
+
+static void *mtk_vcodec_vcp_dm_addr(struct mtk_vcodec_fw *fw, u32 mem_type)
+{
+	unsigned char *vsi_core = fw->vcp->vsi_core_addr;
+
+	switch (mem_type) {
+	case ENCODER_MEM:
+	case VCODEC_LAT_MEM:
+		return fw->vcp->vsi_addr;
+	case VCODEC_CORE_MEM:
+		return vsi_core;
+	case VP9_FRAME_MEM:
+		return vsi_core + VCODEC_VSI_LEN;
+	case AV1_CDF_MEM:
+		return vsi_core + VCODEC_VSI_LEN + VP9_FRAME_SIZE;
+	case AV1_IQ_MEM:
+		return vsi_core + VCODEC_VSI_LEN + VP9_FRAME_SIZE + AV1_CDF_SIZE;
+	default:
+		break;
+	}
+
 	return NULL;
 }
 
@@ -549,6 +572,7 @@ static void mtk_vcodec_vcp_release(struct mtk_vcodec_fw *fw)
 static const struct mtk_vcodec_fw_ops mtk_vcodec_vcp_msg = {
 	.load_firmware = mtk_vcodec_vcp_load_firmware,
 	.get_vdec_capa = mtk_vcodec_vcp_get_vdec_capa,
+	.get_venc_capa = mtk_vcodec_vcp_get_venc_capa,
 	.map_dm_addr = mtk_vcodec_vcp_dm_addr,
 	.ipi_register = mtk_vcodec_vcp_set_ipi_register,
 	.ipi_send = mtk_vcodec_vcp_ipi_send,
drivers/media/platform/mediatek/vcodec/common/mtk_vcodec_fw_vcp.h +13 -0
index 97ba642d..79504bb2 100644
@@ -13,6 +13,19 @@ typedef void (*vcp_ipi_handler_t) (void *data, unsigned int len, void *priv);
 #define VCP_SHARE_BUF_SIZE 64
 
 #define VCODEC_VSI_LEN (0x2000)
+#define VP9_FRAME_SIZE (0x1000)
+#define AV1_CDF_SIZE (0xFE80)
+#define AV1_IQ_TABLE_SIZE (0x12200)
+
+/* enum mtk_vcp_mem_type - memory type for different hardware */
+enum mtk_vcp_mem_type {
+	ENCODER_MEM,
+	VCODEC_LAT_MEM,
+	VCODEC_CORE_MEM,
+	VP9_FRAME_MEM,
+	AV1_CDF_MEM,
+	AV1_IQ_MEM,
+};
 
 /* enum mtk_vcp_ipi_index - index used to separate different hardware */
 enum mtk_vcp_ipi_index {
drivers/media/platform/mediatek/vcodec/decoder/vdec/vdec_av1_req_lat_if.c +31 -4
index 84d28a0b..ffb28bad 100644
@@ -765,6 +765,15 @@ static void *vdec_av1_get_ctrl_ptr(struct mtk_vcodec_dec_ctx *ctx, int id)
 	return ctrl->p_cur.p;
 }
 
+static u32 vdec_av1_get_cdf_table_addr(struct mtk_vcodec_dec_ctx *ctx,
+				       struct vdec_av1_slice_init_vsi *vsi)
+{
+	if (mtk_vcodec_fw_get_type(ctx->dev->fw_handler) == VCP)
+		return AV1_CDF_MEM;
+	else
+		return (u32)vsi->cdf_table_addr;
+}
+
 static int vdec_av1_slice_init_cdf_table(struct vdec_av1_slice_instance *instance)
 {
 	u8 *remote_cdf_table;
@@ -775,7 +784,7 @@ static int vdec_av1_slice_init_cdf_table(struct vdec_av1_slice_instance *instanc
 	ctx = instance->ctx;
 	vsi = instance->vpu.vsi;
 	remote_cdf_table = mtk_vcodec_fw_map_dm_addr(ctx->dev->fw_handler,
-						     (u32)vsi->cdf_table_addr);
+						     vdec_av1_get_cdf_table_addr(ctx, vsi));
 	if (IS_ERR(remote_cdf_table)) {
 		mtk_vdec_err(ctx, "failed to map cdf table\n");
 		return PTR_ERR(remote_cdf_table);
@@ -796,6 +805,15 @@ static int vdec_av1_slice_init_cdf_table(struct vdec_av1_slice_instance *instanc
 	return 0;
 }
 
+static u32 vdec_av1_get_iq_table_addr(struct mtk_vcodec_dec_ctx *ctx,
+				      struct vdec_av1_slice_init_vsi *vsi)
+{
+	if (mtk_vcodec_fw_get_type(ctx->dev->fw_handler) == VCP)
+		return AV1_IQ_MEM;
+	else
+		return (u32)vsi->iq_table_addr;
+}
+
 static int vdec_av1_slice_init_iq_table(struct vdec_av1_slice_instance *instance)
 {
 	u8 *remote_iq_table;
@@ -806,7 +824,7 @@ static int vdec_av1_slice_init_iq_table(struct vdec_av1_slice_instance *instance
 	ctx = instance->ctx;
 	vsi = instance->vpu.vsi;
 	remote_iq_table = mtk_vcodec_fw_map_dm_addr(ctx->dev->fw_handler,
-						    (u32)vsi->iq_table_addr);
+						    vdec_av1_get_iq_table_addr(ctx, vsi));
 	if (IS_ERR(remote_iq_table)) {
 		mtk_vdec_err(ctx, "failed to map iq table\n");
 		return PTR_ERR(remote_iq_table);
@@ -1874,6 +1892,15 @@ static int vdec_av1_slice_update_core(struct vdec_av1_slice_instance *instance,
 	return 0;
 }
 
+static u32 vdec_av1_get_core_vsi_addr(struct mtk_vcodec_dec_ctx *ctx,
+				      struct vdec_av1_slice_init_vsi *vsi)
+{
+	if (mtk_vcodec_fw_get_type(ctx->dev->fw_handler) == VCP)
+		return VCODEC_CORE_MEM;
+	else
+		return (u32)vsi->core_vsi;
+}
+
 static int vdec_av1_slice_init(struct mtk_vcodec_dec_ctx *ctx)
 {
 	struct vdec_av1_slice_instance *instance;
@@ -1905,8 +1932,8 @@ static int vdec_av1_slice_init(struct mtk_vcodec_dec_ctx *ctx)
 		goto error_vsi;
 	}
 	instance->init_vsi = vsi;
-	instance->core_vsi = mtk_vcodec_fw_map_dm_addr(ctx->dev->fw_handler, (u32)vsi->core_vsi);
-
+	instance->core_vsi = mtk_vcodec_fw_map_dm_addr(ctx->dev->fw_handler,
+						       vdec_av1_get_core_vsi_addr(ctx, vsi));
 	if (!instance->core_vsi) {
 		mtk_vdec_err(ctx, "failed to get AV1 core vsi\n");
 		ret = -EINVAL;
drivers/media/platform/mediatek/vcodec/decoder/vdec/vdec_h264_req_multi_if.c +5 -1
index 69d60717..544d3bc0 100644
@@ -1233,7 +1233,11 @@ static int vdec_h264_slice_init(struct mtk_vcodec_dec_ctx *ctx)
 		vsi_size = round_up(vsi_size, VCODEC_DEC_ALIGNED_64);
 		inst->vsi_ext = inst->vpu.vsi;
 		temp = (unsigned char *)inst->vsi_ext;
-		inst->vsi_core_ext = (struct vdec_h264_slice_vsi_ext *)(temp + vsi_size);
+		if (mtk_vcodec_fw_get_type(ctx->dev->fw_handler) == VCP)
+			inst->vsi_core_ext =
+				mtk_vcodec_fw_map_dm_addr(ctx->dev->fw_handler, VCODEC_CORE_MEM);
+		else
+			inst->vsi_core_ext = (struct vdec_h264_slice_vsi_ext *)(temp + vsi_size);
 
 		if (inst->ctx->dev->vdec_pdata->hw_arch == MTK_VDEC_PURE_SINGLE_CORE)
 			inst->decode = vdec_h264_slice_single_decode_ext;
drivers/media/platform/mediatek/vcodec/decoder/vdec/vdec_hevc_req_multi_if.c +5 -2
index dd638ef4..a5dd4298 100644
@@ -879,8 +879,11 @@ static int vdec_hevc_slice_init(struct mtk_vcodec_dec_ctx *ctx)
 
 	vsi_size = round_up(sizeof(struct vdec_hevc_slice_vsi), VCODEC_DEC_ALIGNED_64);
 	inst->vsi = inst->vpu.vsi;
-	inst->vsi_core =
-		(struct vdec_hevc_slice_vsi *)(((char *)inst->vpu.vsi) + vsi_size);
+	if (mtk_vcodec_fw_get_type(ctx->dev->fw_handler) == VCP)
+		inst->vsi_core = mtk_vcodec_fw_map_dm_addr(ctx->dev->fw_handler, VCODEC_CORE_MEM);
+	else
+		inst->vsi_core =
+			(struct vdec_hevc_slice_vsi *)(((char *)inst->vpu.vsi) + vsi_size);
 
 	inst->resolution_changed = true;
 	inst->realloc_mv_buf = true;
drivers/media/platform/mediatek/vcodec/decoder/vdec/vdec_vp9_req_lat_if.c +20 -2
index b4926867..ee4d1cd8 100644
@@ -500,6 +500,15 @@ static DEFINE_MUTEX(vdec_vp9_slice_frame_ctx_lock);
 
 static int vdec_vp9_slice_core_decode(struct vdec_lat_buf *lat_buf);
 
+static u32 vdec_vp9_get_frame_ctx_addr(struct mtk_vcodec_dec_ctx *ctx,
+				       struct vdec_vp9_slice_init_vsi *vsi)
+{
+	if (mtk_vcodec_fw_get_type(ctx->dev->fw_handler) == VCP)
+		return VP9_FRAME_MEM;
+	else
+		return (u32)vsi->default_frame_ctx;
+}
+
 static int vdec_vp9_slice_init_default_frame_ctx(struct vdec_vp9_slice_instance *instance)
 {
 	struct vdec_vp9_slice_frame_ctx *remote_frame_ctx;
@@ -514,7 +523,7 @@ static int vdec_vp9_slice_init_default_frame_ctx(struct vdec_vp9_slice_instance
 		return -EINVAL;
 
 	remote_frame_ctx = mtk_vcodec_fw_map_dm_addr(ctx->dev->fw_handler,
-						     (u32)vsi->default_frame_ctx);
+						     vdec_vp9_get_frame_ctx_addr(ctx, vsi));
 	if (!remote_frame_ctx) {
 		mtk_vdec_err(ctx, "failed to map default frame ctx\n");
 		return -EINVAL;
@@ -1831,6 +1840,15 @@ static int vdec_vp9_slice_update_core(struct vdec_vp9_slice_instance *instance,
 	return 0;
 }
 
+static u32 vdec_vp9_get_core_vsi_addr(struct mtk_vcodec_dec_ctx *ctx,
+				      struct vdec_vp9_slice_init_vsi *vsi)
+{
+	if (mtk_vcodec_fw_get_type(ctx->dev->fw_handler) == VCP)
+		return VCODEC_CORE_MEM;
+	else
+		return (u32)vsi->core_vsi;
+}
+
 static int vdec_vp9_slice_init(struct mtk_vcodec_dec_ctx *ctx)
 {
 	enum mtk_vcodec_fw_type fw_type = ctx->dev->fw_handler->type;
@@ -1864,7 +1882,7 @@ static int vdec_vp9_slice_init(struct mtk_vcodec_dec_ctx *ctx)
 	}
 	instance->init_vsi = vsi;
 	instance->core_vsi = mtk_vcodec_fw_map_dm_addr(ctx->dev->fw_handler,
-						       (u32)vsi->core_vsi);
+						       vdec_vp9_get_core_vsi_addr(ctx, vsi));
 	if (!instance->core_vsi) {
 		mtk_vdec_err(ctx, "failed to get VP9 core vsi\n");
 		ret = -EINVAL;
drivers/media/platform/mediatek/vcodec/decoder/vdec_vpu_if.c +9 -1
index b35759a0..cdb673e6 100644
@@ -9,6 +9,14 @@
 #include "vdec_ipi_msg.h"
 #include "vdec_vpu_if.h"
 
+static u32 vpu_dec_get_vsi_addr(struct vdec_vpu_inst *vpu, const struct vdec_vpu_ipi_init_ack *msg)
+{
+	if (mtk_vcodec_fw_get_type(vpu->ctx->dev->fw_handler) == VCP)
+		return VCODEC_LAT_MEM;
+	else
+		return msg->vpu_inst_addr;
+}
+
 static void handle_init_ack_msg(const struct vdec_vpu_ipi_init_ack *msg)
 {
 	struct vdec_vpu_inst *vpu = (struct vdec_vpu_inst *)
@@ -19,7 +27,7 @@ static void handle_init_ack_msg(const struct vdec_vpu_ipi_init_ack *msg)
 	/* mapping VPU address to kernel virtual address */
 	/* the content in vsi is initialized to 0 in VPU */
 	vpu->vsi = mtk_vcodec_fw_map_dm_addr(vpu->ctx->dev->fw_handler,
-					     msg->vpu_inst_addr);
+					     vpu_dec_get_vsi_addr(vpu, msg));
 	vpu->inst_addr = msg->vpu_inst_addr;
 
 	mtk_vdec_debug(vpu->ctx, "- vpu_inst_addr = 0x%x", vpu->inst_addr);
author
#10 lore raw

PATCH v8 6/14 media: mediatek: vcodec: add debug information

In reply to Kyrie Wu

Print hevc/av1 output format and 10bit capture format information to debug.

---
 .../mediatek/vcodec/common/mtk_vcodec_dbgfs.c | 21 +++++++++++++++++--
 1 file changed, 19 insertions(+), 2 deletions(-)
drivers/media/platform/mediatek/vcodec/common/mtk_vcodec_dbgfs.c +19 -2
index 2da11521..a9e52a32 100644
@@ -29,6 +29,14 @@ static void mtk_vdec_dbgfs_get_format_type(struct mtk_vcodec_dec_ctx *ctx, char
 		curr_len = snprintf(buf + *used, total - *used,
 				    "\toutput format: vp9 slice\n");
 		break;
+	case V4L2_PIX_FMT_HEVC_SLICE:
+		curr_len = snprintf(buf + *used, total - *used,
+				    "\toutput format: hevc slice\n");
+		break;
+	case V4L2_PIX_FMT_AV1_FRAME:
+		curr_len = snprintf(buf + *used, total - *used,
+				    "\toutput format: av1 slice\n");
+		break;
 	default:
 		curr_len = snprintf(buf + *used, total - *used,
 				    "\tunsupported output format: 0x%x\n",
@@ -45,6 +53,14 @@ static void mtk_vdec_dbgfs_get_format_type(struct mtk_vcodec_dec_ctx *ctx, char
 		curr_len = snprintf(buf + *used, total - *used,
 				    "\tcapture format: MT21C\n");
 		break;
+	case V4L2_PIX_FMT_MT2110T:
+		curr_len = snprintf(buf + *used, total - *used,
+				    "\tcapture format: MT2110T (10bit tile mode)\n");
+		break;
+	case V4L2_PIX_FMT_MT2110R:
+		curr_len = snprintf(buf + *used, total - *used,
+				    "\tcapture format: MT2110R (10bit raster mode)\n");
+		break;
 	default:
 		curr_len = snprintf(buf + *used, total - *used,
 				    "\tunsupported capture format: 0x%x\n",
@@ -122,9 +138,10 @@ static ssize_t mtk_vdec_dbgfs_read(struct file *filp, char __user *ubuf,
 
 		if (dbgfs_index[MTK_VDEC_DBGFS_PICINFO]) {
 			curr_len = snprintf(buf + used_len, total_len - used_len,
-					    "\treal(%dx%d)=>align(%dx%d)\n",
+					    "\treal(%dx%d)=>align(%dx%d) 10bit(%d)\n",
 					    ctx->picinfo.pic_w, ctx->picinfo.pic_h,
-					    ctx->picinfo.buf_w, ctx->picinfo.buf_h);
+					    ctx->picinfo.buf_w, ctx->picinfo.buf_h,
+					    ctx->is_10bit_bitstream);
 			used_len += curr_len;
 		}
 
author
#14 lore raw

PATCH v8 8/14 dt-bindings: media: mediatek,vcodec-subdev-decoder: Add MT8196

In reply to Kyrie Wu

Compared to previous ICs, the MT8196 supports a 10-bit decoder and has a decoding capability of 4K@120fps. It also supports 36-bit DRAM IOVA address and Video Power Control to optimize bandwidth and voltage usage.

---
 .../media/mediatek,vcodec-subdev-decoder.yaml | 49 ++++++++++++++++++-
 1 file changed, 48 insertions(+), 1 deletion(-)
Documentation/devicetree/bindings/media/mediatek,vcodec-subdev-decoder.yaml +48 -1
index d1d209cb..d8f25ccd 100644
@@ -76,6 +76,7 @@ properties:
       - mediatek,mt8186-vcodec-dec
       - mediatek,mt8188-vcodec-dec
       - mediatek,mt8195-vcodec-dec
+      - mediatek,mt8196-vcodec-dec
 
   reg:
     minItems: 1
@@ -96,6 +97,11 @@ properties:
       The node of system control processor (SCP), using
       the remoteproc & rpmsg framework.
 
+  mediatek,vcp:
+    $ref: /schemas/types.yaml#/definitions/phandle
+    description: Phandle to the Video Companion Processor (VCP) that runs the
+      video decoder firmware.
+
   "#address-cells":
     const: 2
 
@@ -163,7 +169,6 @@ required:
   - compatible
   - reg
   - iommus
-  - mediatek,scp
   - ranges
 
 if:
@@ -179,6 +184,48 @@ then:
     - interrupts
 
 allOf:
+  - if:
+      properties:
+        compatible:
+          contains:
+            const: mediatek,mt8196-vcodec-dec
+
+    then:
+      required:
+        - mediatek,vcp
+
+      properties:
+        mediatek,scp: false
+
+      patternProperties:
+        '^video-codec@[0-9a-f]+$':
+          properties:
+            compatible:
+              enum:
+                - mediatek,mtk-vcodec-core
+                - mediatek,mtk-vcodec-lat
+
+            clock-names:
+              items:
+                - const: sel
+                - const: vdec
+                - const: lat
+                - const: top
+
+  - if:
+      properties:
+        compatible:
+          contains:
+            enum:
+              - mediatek,mt8192-vcodec-dec
+              - mediatek,mt8186-vcodec-dec
+              - mediatek,mt8188-vcodec-dec
+              - mediatek,mt8195-vcodec-dec
+
+    then:
+      required:
+        - mediatek,scp
+
   - if:
       properties:
         compatible:
author
#18 lore raw

PATCH v8 10/14 media: mediatek: vcodec: define MT8196 vcodec levels.

In reply to Kyrie Wu

The supported level and profile are not the same for different codecs and architecture. Select the correct one.

---
 .../mediatek/vcodec/decoder/mtk_vcodec_dec_stateless.c      | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_stateless.c +6 -0
index ab1894fb..472ece57 100644
@@ -577,6 +577,7 @@ static void mtk_vcodec_dec_fill_h264_level(struct v4l2_ctrl_config *cfg,
 		cfg->max = V4L2_MPEG_VIDEO_H264_LEVEL_5_2;
 		break;
 	case MTK_VDEC_MT8195:
+	case MTK_VDEC_MT8196:
 		cfg->max = V4L2_MPEG_VIDEO_H264_LEVEL_6_0;
 		break;
 	case MTK_VDEC_MT8183:
@@ -595,6 +596,7 @@ static void mtk_vcodec_dec_fill_h264_profile(struct v4l2_ctrl_config *cfg,
 	switch (ctx->dev->chip_name) {
 	case MTK_VDEC_MT8188:
 	case MTK_VDEC_MT8195:
+	case MTK_VDEC_MT8196:
 		cfg->max = V4L2_MPEG_VIDEO_H264_PROFILE_HIGH_10;
 		break;
 	default:
@@ -611,6 +613,7 @@ static void mtk_vcodec_dec_fill_h265_level(struct v4l2_ctrl_config *cfg,
 		cfg->max = V4L2_MPEG_VIDEO_HEVC_LEVEL_5_1;
 		break;
 	case MTK_VDEC_MT8195:
+	case MTK_VDEC_MT8196:
 		cfg->max = V4L2_MPEG_VIDEO_HEVC_LEVEL_5_2;
 		break;
 	default:
@@ -625,6 +628,7 @@ static void mtk_vcodec_dec_fill_h265_profile(struct v4l2_ctrl_config *cfg,
 	switch (ctx->dev->chip_name) {
 	case MTK_VDEC_MT8188:
 	case MTK_VDEC_MT8195:
+	case MTK_VDEC_MT8196:
 		cfg->max = V4L2_MPEG_VIDEO_HEVC_PROFILE_MAIN_10;
 		break;
 	default:
@@ -642,6 +646,7 @@ static void mtk_vcodec_dec_fill_vp9_level(struct v4l2_ctrl_config *cfg,
 		cfg->max = V4L2_MPEG_VIDEO_VP9_LEVEL_5_1;
 		break;
 	case MTK_VDEC_MT8195:
+	case MTK_VDEC_MT8196:
 		cfg->max = V4L2_MPEG_VIDEO_VP9_LEVEL_5_2;
 		break;
 	case MTK_VDEC_MT8186:
@@ -659,6 +664,7 @@ static void mtk_vcodec_dec_fill_vp9_profile(struct v4l2_ctrl_config *cfg,
 	switch (ctx->dev->chip_name) {
 	case MTK_VDEC_MT8188:
 	case MTK_VDEC_MT8195:
+	case MTK_VDEC_MT8196:
 		cfg->max = V4L2_MPEG_VIDEO_VP9_PROFILE_2;
 		break;
 	default:
author
#19 lore raw

PATCH v8 11/14 media: mediatek: vcodec: support 36bit iova address

In reply to Kyrie Wu

Need to set dma mask to support 36bit iova address for decoder hardware can use 36bit address to decode for mt8196.

---
 .../platform/mediatek/vcodec/decoder/mtk_vcodec_dec_drv.c  | 7 +++++++
 1 file changed, 7 insertions(+)
drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_drv.c +7 -0
index 0d27adbf..4d65c102 100644
@@ -392,6 +392,13 @@ static int mtk_vcodec_probe(struct platform_device *pdev)
 		return -ENODEV;
 	}
 	dma_set_max_seg_size(&pdev->dev, UINT_MAX);
+	if (dev->chip_name == MTK_VDEC_MT8196) {
+		ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(36));
+		if (ret) {
+			dev_err(&pdev->dev, "Failed to enable 36-bit DMA: %d\n", ret);
+			return ret;
+		}
+	}
 
 	dev->fw_handler = mtk_vcodec_fw_select(dev, DECODER, dev->fw_init);
 	if (IS_ERR(dev->fw_handler))
author
#20 lore raw

PATCH v8 12/14 media: mediatek: vcodec: clean xpc status

In reply to Kyrie Wu

The driver need to clean xpc status when receive decoder hardware interrupt for mt8196 platform.

---
 .../vcodec/decoder/mtk_vcodec_dec_hw.c        | 31 +++++++++++++++++++
 .../vcodec/decoder/mtk_vcodec_dec_hw.h        | 13 ++++++--
 2 files changed, 42 insertions(+), 2 deletions(-)
drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_hw.c +31 -0
index 881d5de4..46ac3e41 100644
@@ -61,6 +61,34 @@ static int mtk_vdec_hw_prob_done(struct mtk_vcodec_dec_dev *vdec_dev)
 	return 0;
 }
 
+static void mtk_vdec_hw_write_reg_mask(void __iomem *reg_base, u32 reg_offset, u32 val, u32 mask)
+{
+	void __iomem *reg_addr = reg_base + reg_offset;
+	u32 reg_val;
+
+	reg_val =  readl(reg_addr);
+	reg_val &= ~mask;
+	reg_val |= (val & mask);
+	writel(reg_val, reg_addr);
+}
+
+static void mtk_vdec_hw_clean_xpc(struct mtk_vdec_hw_dev *dev)
+{
+	unsigned long flags;
+	u32 val, mask, addr = VDEC_XPC_CLEAN_ADDR;
+
+	if (dev->main_dev->chip_name != MTK_VDEC_MT8196)
+		return;
+
+	val = dev->hw_idx == MTK_VDEC_LAT0 ? VDEC_XPC_LAT_VAL : VDEC_XPC_CORE_VAL;
+	mask = dev->hw_idx == MTK_VDEC_LAT0 ? VDEC_XPC_LAT_MASK : VDEC_XPC_CORE_MASK;
+
+	spin_lock_irqsave(&dev->main_dev->irqlock, flags);
+	mtk_vdec_hw_write_reg_mask(dev->reg_base[VDEC_HW_XPC], addr, val, mask);
+	mtk_vdec_hw_write_reg_mask(dev->reg_base[VDEC_HW_XPC], addr, 0, mask);
+	spin_unlock_irqrestore(&dev->main_dev->irqlock, flags);
+}
+
 static irqreturn_t mtk_vdec_hw_irq_handler(int irq, void *priv)
 {
 	struct mtk_vdec_hw_dev *dev = priv;
@@ -88,6 +116,8 @@ static irqreturn_t mtk_vdec_hw_irq_handler(int irq, void *priv)
 	writel(dec_done_status | VDEC_IRQ_CFG, vdec_misc_addr);
 	writel(dec_done_status & ~VDEC_IRQ_CLR, vdec_misc_addr);
 
+	mtk_vdec_hw_clean_xpc(dev);
+
 	wake_up_dec_ctx(ctx, MTK_INST_IRQ_RECEIVED, dev->hw_idx);
 
 	mtk_v4l2_vdec_dbg(3, ctx, "wake up ctx %d, dec_done_status=%x",
@@ -166,6 +196,7 @@ static int mtk_vdec_hw_probe(struct platform_device *pdev)
 	subdev_dev->hw_idx = hw_idx;
 	subdev_dev->main_dev = main_dev;
 	subdev_dev->reg_base[VDEC_HW_SYS] = main_dev->reg_base[VDEC_HW_SYS];
+	subdev_dev->reg_base[VDEC_HW_XPC] = main_dev->reg_base[VDEC_HW_MISC];
 	set_bit(subdev_dev->hw_idx, main_dev->subdev_bitmap);
 
 	if (IS_SUPPORT_VDEC_HW_IRQ(hw_idx)) {
drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_hw.h +11 -2
index 83fe8b94..5c906143 100644
@@ -18,17 +18,26 @@
 #define VDEC_IRQ_CLR 0x10
 #define VDEC_IRQ_CFG_REG 0xa4
 
+#define VDEC_XPC_CLEAN_ADDR 0xc
+#define VDEC_XPC_LAT_VAL BIT(0)
+#define VDEC_XPC_LAT_MASK BIT(0)
+
+#define VDEC_XPC_CORE_VAL BIT(4)
+#define VDEC_XPC_CORE_MASK BIT(4)
+
 #define IS_SUPPORT_VDEC_HW_IRQ(hw_idx) ((hw_idx) != MTK_VDEC_LAT_SOC)
 
 /**
  * enum mtk_vdec_hw_reg_idx - subdev hardware register base index
- * @VDEC_HW_SYS : vdec soc register index
+ * @VDEC_HW_SYS:  vdec soc register index
  * @VDEC_HW_MISC: vdec misc register index
- * @VDEC_HW_MAX : vdec supported max register index
+ * @VDEC_HW_XPC:  vdec xpc register index
+ * @VDEC_HW_MAX:  vdec supported max register index
  */
 enum mtk_vdec_hw_reg_idx {
 	VDEC_HW_SYS,
 	VDEC_HW_MISC,
+	VDEC_HW_XPC,
 	VDEC_HW_MAX
 };
 
author
#22 lore raw

PATCH v8 13/14 media: mediatek: decoder: fill av1 buffer size with picinfo

In reply to Kyrie Wu

The buffer size of y and c plane has been calculated in vcp/scp, can fill each frame buffer size with picinfo directly.

---
 .../vcodec/decoder/vdec/vdec_av1_req_lat_if.c     | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)
drivers/media/platform/mediatek/vcodec/decoder/vdec/vdec_av1_req_lat_if.c +9 -6
index ffb28bad..2d6e08e7 100644
@@ -1812,18 +1812,19 @@ static int vdec_av1_slice_setup_core_buffer(struct vdec_av1_slice_instance *inst
 {
 	struct vb2_buffer *vb;
 	struct vb2_queue *vq;
-	int w, h, plane, size;
+	int plane;
 	int i;
 
 	plane = instance->ctx->q_data[MTK_Q_DATA_DST].fmt->num_planes;
-	w = vsi->frame.uh.upscaled_width;
-	h = vsi->frame.uh.frame_height;
-	size = ALIGN(w, VCODEC_DEC_ALIGNED_64) * ALIGN(h, VCODEC_DEC_ALIGNED_64);
 
 	/* frame buffer */
 	vsi->fb.y.dma_addr = fb->base_y.dma_addr;
+
+	vsi->fb.y.size = instance->ctx->picinfo.fb_sz[0];
+	vsi->fb.c.size = instance->ctx->picinfo.fb_sz[1];
+
 	if (plane == 1)
-		vsi->fb.c.dma_addr = fb->base_y.dma_addr + size;
+		vsi->fb.c.dma_addr = fb->base_y.dma_addr + vsi->fb.y.size;
 	else
 		vsi->fb.c.dma_addr = fb->base_c.dma_addr;
 
@@ -1846,8 +1847,10 @@ static int vdec_av1_slice_setup_core_buffer(struct vdec_av1_slice_instance *inst
 		}
 
 		vref->y.dma_addr = vb2_dma_contig_plane_dma_addr(vb, 0);
+		vref->y.size = vsi->fb.y.size;
+		vref->c.size = vsi->fb.c.size;
 		if (plane == 1)
-			vref->c.dma_addr = vref->y.dma_addr + size;
+			vref->c.dma_addr = vref->y.dma_addr + vsi->fb.y.size;
 		else
 			vref->c.dma_addr = vb2_dma_contig_plane_dma_addr(vb, 1);
 	}
author
#17 lore raw

PATCH v8 9/14 media: mediatek: vcodec: add decoder compatible to support mt8196

In reply to Kyrie Wu

MT8196 is lat single core architecture. Support its compatible and use mtk_lat_sig_core_pdata to initialize platform data.

---
 .../platform/mediatek/vcodec/decoder/mtk_vcodec_dec_drv.c   | 6 ++++++
 .../platform/mediatek/vcodec/decoder/mtk_vcodec_dec_drv.h   | 1 +
 2 files changed, 7 insertions(+)
drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_drv.c +6 -0
index ab5f6c01..0d27adbf 100644
@@ -347,6 +347,8 @@ static void mtk_vcodec_dec_get_chip_name(struct mtk_vcodec_dec_dev *vdec_dev)
 		vdec_dev->chip_name = MTK_VDEC_MT8186;
 	else if (of_device_is_compatible(dev->of_node, "mediatek,mt8188-vcodec-dec"))
 		vdec_dev->chip_name = MTK_VDEC_MT8188;
+	else if (of_device_is_compatible(dev->of_node, "mediatek,mt8196-vcodec-dec"))
+		vdec_dev->chip_name = MTK_VDEC_MT8196;
 	else
 		vdec_dev->chip_name = MTK_VDEC_INVAL;
 }
@@ -566,6 +568,10 @@ static const struct of_device_id mtk_vcodec_match[] = {
 		.compatible = "mediatek,mt8188-vcodec-dec",
 		.data = &mtk_lat_sig_core_pdata,
 	},
+	{
+		.compatible = "mediatek,mt8196-vcodec-dec",
+		.data = &mtk_lat_sig_core_pdata,
+	},
 	{},
 };
 
drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_drv.h +1 -0
index 15e43732..53d1708b 100644
@@ -29,6 +29,7 @@ enum mtk_vcodec_dec_chip_name {
 	MTK_VDEC_MT8188 = 8188,
 	MTK_VDEC_MT8192 = 8192,
 	MTK_VDEC_MT8195 = 8195,
+	MTK_VDEC_MT8196 = 8196,
 };
 
 /*
author
#24 lore raw

PATCH v8 14/14 media: mediatek: decoder: support av1 extend vsi

In reply to Kyrie Wu

The driver can't access tile buffer address for extend architecture, set tile group information in vcp and share it with kernel.

---
 .../vcodec/decoder/vdec/vdec_av1_req_lat_if.c | 59 ++++++++++++++++---
 1 file changed, 52 insertions(+), 7 deletions(-)
drivers/media/platform/mediatek/vcodec/decoder/vdec/vdec_av1_req_lat_if.c +52 -7
index 2d6e08e7..664adde3 100644
@@ -25,6 +25,9 @@
 
 #define AV1_INVALID_IDX			-1
 
+#define AV1_NON_EXT_VSI_SIZE		0xD50
+#define AV1_TILE_SIZE			64
+
 #define AV1_DIV_ROUND_UP_POW2(value, n)			\
 ({							\
 	typeof(n) _n  = n;				\
@@ -641,6 +644,8 @@ struct vdec_av1_slice_fb {
  * @frame:		current frame info
  * @state:		status after decode done
  * @cur_lst_tile_id:	tile id for large scale
+ * @tile_group:	tile group info
+ * @reserved:		reserved
  */
 struct vdec_av1_slice_vsi {
 	/* lat */
@@ -665,6 +670,8 @@ struct vdec_av1_slice_vsi {
 	struct vdec_av1_slice_frame frame;
 	struct vdec_av1_slice_state state;
 	u32 cur_lst_tile_id;
+	struct vdec_av1_slice_tile_group tile_group;
+	unsigned int reserved[4];
 };
 
 /**
@@ -1403,17 +1410,29 @@ static void vdec_av1_slice_setup_uh(struct vdec_av1_slice_instance *instance,
 	vdec_av1_slice_setup_tile(frame, &ctrl_fh->tile_info);
 }
 
+static
+struct vdec_av1_slice_tile_group *vdec_av1_get_tile_group(struct vdec_av1_slice_instance *instance,
+							  struct vdec_av1_slice_vsi *vsi)
+{
+	if (IS_VDEC_SUPPORT_EXT(instance->ctx->dev->dec_capability))
+		return &vsi->tile_group;
+	else
+		return &instance->tile_group;
+}
+
 static int vdec_av1_slice_setup_tile_group(struct vdec_av1_slice_instance *instance,
 					   struct vdec_av1_slice_vsi *vsi)
 {
 	struct v4l2_ctrl_av1_tile_group_entry *ctrl_tge;
-	struct vdec_av1_slice_tile_group *tile_group = &instance->tile_group;
+	struct vdec_av1_slice_tile_group *tile_group;
 	struct vdec_av1_slice_uncompressed_header *uh = &vsi->frame.uh;
 	struct vdec_av1_slice_tile *tile = &uh->tile;
 	struct v4l2_ctrl *ctrl;
 	u32 tge_size;
 	int i;
 
+	tile_group = vdec_av1_get_tile_group(instance, vsi);
+
 	ctrl = v4l2_ctrl_find(&instance->ctx->ctrl_hdl, V4L2_CID_STATELESS_AV1_TILE_GROUP_ENTRY);
 	if (!ctrl)
 		return -EINVAL;
@@ -1608,6 +1627,15 @@ static int vdec_av1_slice_setup_pfc(struct vdec_av1_slice_instance *instance,
 	return ret;
 }
 
+static u32 vdec_av1_get_tiles_num(struct vdec_av1_slice_instance *instance,
+				  struct vdec_av1_slice_vsi *vsi)
+{
+	if (IS_VDEC_SUPPORT_EXT(instance->ctx->dev->dec_capability))
+		return vsi->tile_group.num_tiles;
+	else
+		return instance->tile_group.num_tiles;
+}
+
 static void vdec_av1_slice_setup_lat_buffer(struct vdec_av1_slice_instance *instance,
 					    struct vdec_av1_slice_vsi *vsi,
 					    struct mtk_vcodec_mem *bs,
@@ -1648,12 +1676,18 @@ static void vdec_av1_slice_setup_lat_buffer(struct vdec_av1_slice_instance *inst
 
 	vsi->tile.buf = instance->tile.dma_addr;
 	vsi->tile.size = instance->tile.size;
-	memcpy(lat_buf->tile_addr.va, instance->tile.va, 64 * instance->tile_group.num_tiles);
 
 	vsi->cdf_table.buf = instance->cdf_table.dma_addr;
 	vsi->cdf_table.size = instance->cdf_table.size;
 	vsi->iq_table.buf = instance->iq_table.dma_addr;
 	vsi->iq_table.size = instance->iq_table.size;
+
+	/* lat_buf is used to share hardware decoder syntax between lat and core,
+	 * there isn't only one. But there is only one tile.va for each instance.
+	 * Need to copy tile information to lat_buf every time.
+	 */
+	memcpy(lat_buf->tile_addr.va, instance->tile.va,
+	       AV1_TILE_SIZE * vdec_av1_get_tiles_num(instance, vsi));
 }
 
 static void vdec_av1_slice_setup_seg_buffer(struct vdec_av1_slice_instance *instance,
@@ -1676,7 +1710,7 @@ static void vdec_av1_slice_setup_tile_buffer(struct vdec_av1_slice_instance *ins
 					     struct vdec_av1_slice_vsi *vsi,
 					     struct mtk_vcodec_mem *bs)
 {
-	struct vdec_av1_slice_tile_group *tile_group = &instance->tile_group;
+	struct vdec_av1_slice_tile_group *tile_group;
 	struct vdec_av1_slice_uncompressed_header *uh = &vsi->frame.uh;
 	struct vdec_av1_slice_tile *tile = &uh->tile;
 	u32 tile_num, tile_row, tile_col;
@@ -1687,6 +1721,8 @@ static void vdec_av1_slice_setup_tile_buffer(struct vdec_av1_slice_instance *ins
 	u32 *tile_info_buf = instance->tile.va;
 	u64 pa = (u64)bs->dma_addr;
 
+	tile_group = vdec_av1_get_tile_group(instance, vsi);
+
 	if (uh->disable_cdf_update == 0)
 		allow_update_cdf = 1;
 
@@ -1909,7 +1945,7 @@ static int vdec_av1_slice_init(struct mtk_vcodec_dec_ctx *ctx)
 	struct vdec_av1_slice_instance *instance;
 	struct vdec_av1_slice_init_vsi *vsi;
 	enum mtk_vcodec_fw_type fw_type = ctx->dev->fw_handler->type;
-	int ret;
+	int ret, vsi_size = AV1_NON_EXT_VSI_SIZE;
 
 	instance = kzalloc_obj(*instance);
 	if (!instance)
@@ -1943,9 +1979,18 @@ static int vdec_av1_slice_init(struct mtk_vcodec_dec_ctx *ctx)
 		goto error_vsi;
 	}
 
-	if (vsi->vsi_size != sizeof(struct vdec_av1_slice_vsi))
-		mtk_vdec_err(ctx, "remote vsi size 0x%x mismatch! expected: 0x%zx\n",
-			     vsi->vsi_size, sizeof(struct vdec_av1_slice_vsi));
+	if (IS_VDEC_SUPPORT_EXT(ctx->dev->dec_capability)) {
+		vsi_size = sizeof(struct vdec_av1_slice_vsi);
+		vsi->iq_table_size = AV1_IQ_TABLE_SIZE;
+		vsi->cdf_table_size = AV1_CDF_SIZE;
+	}
+
+	if (vsi->vsi_size != vsi_size) {
+		mtk_vdec_err(ctx, "remote vsi size 0x%x mismatch! expected: 0x%x\n",
+			     vsi->vsi_size, vsi_size);
+		ret = -EINVAL;
+		goto error_vsi;
+	}
 
 	instance->irq_enabled = 1;
 	instance->inneracing_mode = IS_VDEC_INNER_RACING(instance->ctx->dev->dec_capability);
#13 lore raw

Re: [PATCH v8 07/14] media: mediatek: vcodec: send share memory address to vcp

In reply to Kyrie Wu
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] media: vcodec: IPI msg size increase may break older firmware --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] media: vcodec: IPI msg size increase may break older firmware
--

commit 82cf992a706e24d85ad99ada3a7b106540bc8145
Author: Kyrie Wu <[email protected]>

media: mediatek: vcodec: send share memory address to vcp

The share memory is allocated in kernel for vcp architecture, it's different with vpu which share memors is reserved in vpu micro processor. Need to send share memory address to vcp.

Quoted 6 lines
drivers/media/platform/mediatek/vcodec/decoder/vdec_ipi_msg.h +0 -0
index c2c2a3a63dfd3..8b771d47053b0 100644
@@ -67,11 +67,13 @@ struct vdec_vpu_ipi_ack {
  * @msg_id	: AP_IPIMSG_DEC_INIT

drivers/media/platform/mediatek/vcodec/decoder/vdec_ipi_msg.h struct vdec_vpu_ipi_ack { · line 77

Quoted 10 lines
drivers/media/platform/mediatek/vcodec/decoder/vdec_ipi_msg.h +2 -0
  * @codec_type	: codec fourcc
  * @ap_inst_addr	: AP video decoder instance address
+ * @shared_iova	: reserved share memory address
  */
 struct vdec_ap_ipi_init {
 	uint32_t msg_id;
 	u32 codec_type;
 	uint64_t ap_inst_addr;
+	u64 shared_iova;
 };

[Severity: Medium]
Does this struct size change break older firmware?

The size of struct vdec_ap_ipi_init is increased from 16 to 24 bytes here.

[ ... ]

drivers/media/platform/mediatek/vcodec/decoder/vdec_vpu_if.c int vpu_dec_init(struct vdec_vpu_inst *vpu) · line 242

Quoted 12 lines
drivers/media/platform/mediatek/vcodec/decoder/vdec_vpu_if.c +2 -0
index cdb673e6b4770..3a10b32be0945 100644
@@ -236,6 +236,8 @@ int vpu_dec_init(struct vdec_vpu_inst *vpu)
 	msg.msg_id = AP_IPIMSG_DEC_INIT;
 	msg.ap_inst_addr = (unsigned long)vpu;
 	msg.codec_type = vpu->codec_type;
+	if (mtk_vcodec_fw_get_type(vpu->ctx->dev->fw_handler) == VCP)
+		msg.shared_iova = vpu->ctx->dev->fw_handler->vcp->iova_addr;
 
 	mtk_vdec_debug(vpu->ctx, "vdec_inst=%p", vpu);

[Severity: Medium]
Will this unconditionally send the new 24-byte size to all firmware types?

In vpu_dec_init(), vcodec_vpu_send_msg() is called right below this:

	err = vcodec_vpu_send_msg(vpu, (void *)&msg, sizeof(msg));

If older VPU or SCP firmwares strictly validate the IPI message length, could the video decoder initialization fail and render the device unusable?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
#11 lore raw

Re: [PATCH v8 06/14] media: mediatek: vcodec: add debug information

In reply to Kyrie Wu
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] media: mediatek: vcodec: UAF due to lock mismatch in debugfs iteration --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] media: mediatek: vcodec: UAF due to lock mismatch in debugfs iteration --

commit 3dd0220ecce9f2ca273e1906b8312cd6795ec662
Author: Kyrie Wu <[email protected]>

media: mediatek: vcodec: add debug information

This commit adds debug information for HEVC/AV1 output formats and 10-bit capture formats. It updates the debugfs read function to include 10-bit bitstream information.

Quoted 5 lines
drivers/media/platform/mediatek/vcodec/common/mtk_vcodec_dbgfs.c +0 -0
index 2da11521fc7b9..a9e52a32079d3 100644

drivers/media/platform/mediatek/vcodec/common/mtk_vcodec_dbgfs.c static ssize_t mtk_vdec_dbgfs_read(struct file *filp, char __user *ubuf, · line 144

Quoted 9 lines
drivers/media/platform/mediatek/vcodec/common/mtk_vcodec_dbgfs.c +3 -2
@@ -122,9 +138,10 @@ static ssize_t mtk_vdec_dbgfs_read(struct file *filp, char __user *ubuf,
 
 		if (dbgfs_index[MTK_VDEC_DBGFS_PICINFO]) {
 			curr_len = snprintf(buf + used_len, total_len - used_len,
-					    "\treal(%dx%d)=>align(%dx%d)\n",
+					    "\treal(%dx%d)=>align(%dx%d) 10bit(%d)\n",
 					    ctx->picinfo.pic_w, ctx->picinfo.pic_h,
-					    ctx->picinfo.buf_w, ctx->picinfo.buf_h);
+					    ctx->picinfo.buf_w, ctx->picinfo.buf_h,
+					    ctx->is_10bit_bitstream);

[Severity: High]
Can a concurrent release of the video device cause a use-after-free when accessing ctx in this debugfs read path?

In mtk_vdec_dbgfs_read(), the iteration over dbgfs_head is protected by dbgfs->dbgfs_lock. However, a concurrent device release calling mtk_vcodec_dbgfs_remove() removes and frees dbgfs_inst without acquiring dbgfs_lock:

drivers/media/platform/mediatek/vcodec/common/mtk_vcodec_dbgfs.c:mtk_vcodec_dbgfs_remove() {
    list_for_each_entry(dbgfs_inst, &vcodec_dev->dbgfs.dbgfs_head, node) {
        if (dbgfs_inst->inst_id == ctx_id) {
            ...
            list_del(&dbgfs_inst->node);
            kfree(dbgfs_inst);
            return;
        }
    }
}

Because mtk_vcodec_dbgfs_remove() does not take dbgfs_lock, mtk_vdec_dbgfs_read() can dereference a freed dbgfs_inst and a freed ctx when a user reads the debugfs file at the same time the device is closed.

Should the list removal in mtk_vcodec_dbgfs_remove() be protected by dbgfs_lock?

Quoted 3 lines
drivers/media/platform/mediatek/vcodec/common/mtk_vcodec_dbgfs.c +0 -0
 			used_len += curr_len;
 		}
 
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
#21 lore raw

Re: [PATCH v8 12/14] media: mediatek: vcodec: clean xpc status

In reply to Kyrie Wu
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] media: mtk-vcodec: NULL pointer deref in mtk_vdec_hw_irq_handler() - [High] media: mtk-vcodec…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] media: mtk-vcodec: NULL pointer deref in mtk_vdec_hw_irq_handler()
- [High] media: mtk-vcodec: NULL pointer dereference in mtk_vdec_hw_clean_xpc() on incomplete DT --

commit 48b9203929193f7043af88ad2f6320fd0d5acb54
Author: Kyrie Wu <[email protected]>

media: mediatek: vcodec: clean xpc status

The driver need to clean xpc status when receive decoder hardware interrupt for mt8196 platform.

Quoted 5 lines
drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_hw.c +0 -0
index 881d5de41e050..46ac3e41c401c 100644

drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_hw.c static int mtk_vdec_hw_prob_done(struct mtk_vcodec_dec_dev *vdec_dev) · line 69

Quoted 9 lines
drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_hw.c +6 -0
@@ -61,6 +61,34 @@ static int mtk_vdec_hw_prob_done(struct mtk_vcodec_dec_dev *vdec_dev)
 	return 0;
 }
 
+static void mtk_vdec_hw_write_reg_mask(void __iomem *reg_base, u32 reg_offset, u32 val, u32 mask)
+{
+	void __iomem *reg_addr = reg_base + reg_offset;
+	u32 reg_val;
+
+	reg_val =  readl(reg_addr);

[Severity: High]
Does this code dereference a NULL pointer if the parent device tree node provides only one reg entry?

If the MT8196 parent device tree node provides only one reg entry (which is permitted by the DT bindings' minItems: 1), mtk_vcodec_get_reg_bases() will map only VDEC_SYS and leave main_dev->reg_base[VDEC_MISC] as NULL.

This NULL pointer is propagated to subdev_dev->reg_base[VDEC_HW_XPC] during mtk_vdec_hw_probe():

    subdev_dev->reg_base[VDEC_HW_XPC] = main_dev->reg_base[VDEC_HW_MISC];

When the hardware interrupt fires, mtk_vdec_hw_clean_xpc() is unconditionally called for MT8196 and passes this NULL base to mtk_vdec_hw_write_reg_mask(), which dereferences it here in readl().

Would it be better to validate the register base before use, or enforce reg_num >= 2 for MT8196 during probe?

Quoted 4 lines
drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_hw.c +4 -0
+	reg_val &= ~mask;
+	reg_val |= (val & mask);
+	writel(reg_val, reg_addr);
+}

[ ... ]

drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_hw.c static irqreturn_t mtk_vdec_hw_irq_handler(int irq, void *priv) · line 121

Quoted 7 lines
drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_hw.c +2 -0
@@ -88,6 +116,8 @@ static irqreturn_t mtk_vdec_hw_irq_handler(int irq, void *priv)
 	writel(dec_done_status | VDEC_IRQ_CFG, vdec_misc_addr);
 	writel(dec_done_status & ~VDEC_IRQ_CLR, vdec_misc_addr);
 
+	mtk_vdec_hw_clean_xpc(dev);
+
 	wake_up_dec_ctx(ctx, MTK_INST_IRQ_RECEIVED, dev->hw_idx);

[Severity: High]
Can ctx be NULL here during the context teardown race window?

If a delayed or spurious interrupt fires during the teardown path window (after mtk_vcodec_set_curr_ctx(dev, NULL, MTK_VDEC_CORE) clears the current context, but before mtk_vcodec_dec_disable_irq() is called), or if hardware continues generating events before IRQs are disabled, mtk_vdec_hw_irq_handler() fetches a NULL ctx.

If the HW is inactive, the NULL ctx is unconditionally dereferenced earlier by the mtk_v4l2_vdec_err macro to log an error. If the HW is active, it is unconditionally dereferenced here to wake up the context queue.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=12
#25 lore raw

Re: [PATCH v8 14/14] media: mediatek: decoder: support av1 extend vsi

In reply to Kyrie Wu
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Critical] media: Out-of-bounds write to VPU firmware memory due to oversized memcpy --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Critical] media: Out-of-bounds write to VPU firmware memory due to oversized memcpy --

commit 482da53176ed6a6bd943ba24dd065fc679bf7158
Author: Kyrie Wu <[email protected]>

media: mediatek: decoder: support av1 extend vsi

The patch expands the AV1 VSI state structure to include tile group information to support the extended architecture. It also updates the initialization routine to allow smaller shared memory buffers when running on non-extended firmware.

Quoted 11 lines
drivers/media/platform/mediatek/vcodec/decoder/vdec/vdec_av1_req_lat_if.c +2 -0
index 14639098fd463..1ef561dae18c7 100644
@@ -665,6 +670,8 @@ struct vdec_av1_slice_vsi {
 	struct vdec_av1_slice_frame frame;
 	struct vdec_av1_slice_state state;
 	u32 cur_lst_tile_id;
+	struct vdec_av1_slice_tile_group tile_group;
+	unsigned int reserved[4];
 };

[ ... ]

Quoted 9 lines
drivers/media/platform/mediatek/vcodec/decoder/vdec/vdec_av1_req_lat_if.c +2 -3
@@ -1942,9 +1978,18 @@ static int vdec_av1_slice_init(struct mtk_vcodec_dec_ctx *ctx)
 		goto error_vsi;
 	}
 
-	if (vsi->vsi_size != sizeof(struct vdec_av1_slice_vsi))
-		mtk_vdec_err(ctx, "remote vsi size 0x%x mismatch! expected: 0x%zx\n",
-			     vsi->vsi_size, sizeof(struct vdec_av1_slice_vsi));
+	if (IS_VDEC_SUPPORT_EXT(ctx->dev->dec_capability)) {
+		vsi_size = sizeof(struct vdec_av1_slice_vsi);

drivers/media/platform/mediatek/vcodec/decoder/vdec/vdec_av1_req_lat_if.c static int vdec_av1_slice_init(struct mtk_vcodec_dec_ctx *ctx) · line 1992

Quoted 10 lines
drivers/media/platform/mediatek/vcodec/decoder/vdec/vdec_av1_req_lat_if.c +10 -0
+		vsi->iq_table_size = AV1_IQ_TABLE_SIZE;
+		vsi->cdf_table_size = AV1_CDF_SIZE;
+	}
+
+	if (vsi->vsi_size != vsi_size) {
+		mtk_vdec_err(ctx, "remote vsi size 0x%x mismatch! expected: 0x%x\n",
+			     vsi->vsi_size, vsi_size);
+		ret = -EINVAL;
+		goto error_vsi;
+	}

[Severity: Critical]
This correctly enforces that older firmware provides a 0xD50-byte buffer for the non-extended architecture.

However, since struct vdec_av1_slice_vsi was expanded with tile_group, does vdec_av1_slice_vsi_to_remote() now copy too much data?

vdec_av1_slice_vsi_to_remote() {
	memcpy(remote_vsi, vsi, sizeof(*vsi));
}

Since sizeof(*vsi) evaluates to the new, enlarged compile-time size of the struct, will this unconditionally write ~7.5KB of data into the 0xD50 (3.4KB) firmware buffer when decoding on non-extended hardware?

Could this out-of-bounds write corrupt adjacent data in the VPU shared memory or firmware SRAM?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=14
#3 lore raw

Re: [PATCH v8 02/14] media: mediatek: vcodec: add driver to support vcp

In reply to Kyrie Wu
Thank you for your contribution! Sashiko AI review found 11 potential issue(s) to consider: - [High] media: vcodec: vcp: Modprobe bomb in mtk_vcodec_vcp_get_vcp_device retry loop - [High] media…

Thank you for your contribution! Sashiko AI review found 11 potential issue(s) to consider:
- [High] media: vcodec: vcp: Modprobe bomb in mtk_vcodec_vcp_get_vcp_device retry loop
- [High] media: vcodec: vcp: IPC desync and UAF from unhandled late ACKs
- [High] media: vcodec: vcp: Out-of-bounds read in mtk_vcodec_vcp_msg_ack_isr
- [High] media: vcodec: vcp: Unvalidated firmware payload length enables OOB read
- [High] build: missing headers and identifiers in mtk_vcodec_fw_vcp.c
- [High] module: 10000-iteration modprobe fork bomb in mtk_vcodec_vcp_get_vcp_device
- [Medium] media: vcodec: vcp: Remote processor reference leak in mtk_vcodec_vcp_release
- [Medium] media: vcodec: vcp: Signal retry loop in mtk_vcodec_vcp_msg_ipi_send instantly bypasses timeout
- [Medium] media: vcodec: vcp: Race condition from premature is_init_done flag assignment
- [Low] media: vcodec: vcp: Incorrect printk format specifier for dma_addr_t
- [Low] media: vcodec: vcp: Unused fw_type variable in probe functions
--

commit bd79ec43ee8b23d8e7b2f7ace79397de331b886a
Author: Kyrie Wu <[email protected]>
Subject: media: mediatek: vcodec: add driver to support vcp

The processor is changed from scp to vcp in mt8196 platform. Adding new firmware interface to communicate kernel with vcp for the communication method is changed.

Quoted 8 lines
drivers/media/platform/mediatek/vcodec/common/mtk_vcodec_fw_vcp.c new file +2 -0
new file mode 100644
index 0000000000000..150d842c7d226
@@ -0,0 +1,571 @@
+// SPDX-License-Identifier: GPL-2.0
+/*

drivers/media/platform/mediatek/vcodec/common/mtk_vcodec_fw_vcp.c line 12

Quoted 10 lines
drivers/media/platform/mediatek/vcodec/common/mtk_vcodec_fw_vcp.c new file +10 -0
+ * Copyright (c) 2025 MediaTek Inc.
+ * Author: Kyrie Wu <[email protected]>
+ */
+
+#include <linux/delay.h>
+#include <linux/dma-direction.h>
+#include <linux/dma-mapping.h>
+#include <linux/iommu.h>
+#include <linux/remoteproc/mtk_vcp_public.h>
+#include <linux/firmware/mediatek/mtk-vcp-ipc.h>

[Severity: High]
Are these included headers missing from the kernel tree? It looks like mtk_vcp_public.h and mtk-vcp-ipc.h are completely missing, along with identifiers like VDEC_FEATURE_ID and IPI_OUT_VDEC_1, which will cause compilation failures.

[ ... ]

drivers/media/platform/mediatek/vcodec/common/mtk_vcodec_fw_vcp.c line 151

Quoted 8 lines
drivers/media/platform/mediatek/vcodec/common/mtk_vcodec_fw_vcp.c new file +8 -0
+		if (!handler) {
+			dev_err(&fw->pdev->dev, "invalid ack ipi handler id = %d\n", obj->id);
+			mtk_vcodec_vcp_ipi_unlock(fw->vcp, obj->id);
+			mtk_vcodec_vcp_free_msg_node(fw, msg_node);
+			continue;
+		}
+
+		handler(msg, obj->len, fw->vcp->ipi_desc[obj->id].priv);

[Severity: High]
Does this unvalidated length field allow an out-of-bounds read? The obj->len value is extracted directly from the untrusted IPC payload and passed to the handler without bounds checking against the physical constraints of the share_buf array.

drivers/media/platform/mediatek/vcodec/common/mtk_vcodec_fw_vcp.c line 155

Quoted 4 lines
drivers/media/platform/mediatek/vcodec/common/mtk_vcodec_fw_vcp.c new file +4 -0
+		mtk_vcodec_vcp_ipi_unlock(fw->vcp, obj->id);
+
+		fw->vcp->msg_signaled[obj->id] = true;
+		wake_up(&fw->vcp->msg_wq[obj->id]);

[Severity: High]
Can unhandled late ACKs cause IPC desynchronization and a use-after-free? If wait_event_interruptible_timeout() in mtk_vcodec_vcp_msg_ipi_send() times out, the wait is abandoned. A subsequent delayed ACK processed here would signal completion prematurely for a new wait, or cast the stale msg->ap_inst_addr to a potentially freed context.

[ ... ]

Quoted 2 lines
drivers/media/platform/mediatek/vcodec/common/mtk_vcodec_fw_vcp.c new file +2 -0
+static int mtk_vcodec_vcp_msg_ack_isr(unsigned int id, void *prdata, void *data, unsigned int len)
+{

[ ... ]

drivers/media/platform/mediatek/vcodec/common/mtk_vcodec_fw_vcp.c line 183

Quoted 4 lines
drivers/media/platform/mediatek/vcodec/common/mtk_vcodec_fw_vcp.c new file +4 -0
+	if (!list_empty(&msg_queue->node_list)) {
+		msg_node = list_entry(msg_queue->node_list.next, struct mtk_vcp_msg_node, list);
+
+		memcpy(&msg_node->ipi_data, obj, sizeof(*obj));

[Severity: High]
Will this memcpy() perform an out-of-bounds read if the provided len is smaller than sizeof(*obj)? The length parameter passed to the ISR is ignored, which could lead to a page fault if the payload sits at the end of a mapped page.

[ ... ]

Quoted 3 lines
drivers/media/platform/mediatek/vcodec/common/mtk_vcodec_fw_vcp.c new file +3 -0
+static int mtk_vcodec_vcp_msg_ipi_send(struct mtk_vcodec_fw *fw, int id, void *buf,
+				       unsigned int len, unsigned int wait)
+{

[ ... ]

drivers/media/platform/mediatek/vcodec/common/mtk_vcodec_fw_vcp.c line 275

Quoted 12 lines
drivers/media/platform/mediatek/vcodec/common/mtk_vcodec_fw_vcp.c new file +12 -0
+wait_ack:
+	/* wait for VCP's ACK */
+	ret = wait_event_interruptible_timeout(*msg_wq, *msg_signaled,
+					       msecs_to_jiffies(IPI_TIMEOUT_MS));
+	if (!ret) {
+		vcp->ipi_id_ack[id] = VCODEC_IPI_MSG_STATUS_FAIL;
+		dev_err(&fw->pdev->dev, "wait ipi ack timeout! %d %d\n", ret, vcp->ipi_id_ack[id]);
+	} else if (ret == -ERESTARTSYS) {
+		if (retry_cnt++ < 5)
+			goto wait_ack;
+
+		dev_err(&fw->pdev->dev, "wait ipi ack err (%d)\n", vcp->ipi_id_ack[id]);

[Severity: Medium]
Will this retry loop instantly exhaust all its attempts? Since the pending signal on the task is never cleared, subsequent calls to wait_event_interruptible_timeout() will immediately see the signal and return -ERESTARTSYS again without sleeping.

[ ... ]

drivers/media/platform/mediatek/vcodec/common/mtk_vcodec_fw_vcp.c line 323

Quoted 13 lines
drivers/media/platform/mediatek/vcodec/common/mtk_vcodec_fw_vcp.c new file +13 -0
+static int mtk_vcodec_vcp_get_vcp_device(struct mtk_vcodec_fw *fw)
+{
+	struct device *dev = &fw->pdev->dev;
+	int retry = 0, retry_cnt = 10000;
+	phandle vcp_phandle;
+
+	while (!try_then_request_module(mtk_vcodec_vcp_driver_loaded(fw), "mtk-vcp")) {
+		if (++retry > retry_cnt) {
+			dev_err(dev, "failed to load mtk-vcp module");
+			return -EPROBE_DEFER;
+		}
+		usleep_range(1000, 2000);
+	}

[Severity: High]
Does this loop act as a local denial-of-service fork bomb? The try_then_request_module macro internally calls __request_module() synchronously every time its condition evaluates to false. If the module is missing or fails to probe, this will spawn up to 10,000 modprobe helper processes in rapid succession.

[ ... ]

Quoted 2 lines
drivers/media/platform/mediatek/vcodec/common/mtk_vcodec_fw_vcp.c new file +2 -0
+static int mtk_vcodec_vcp_load_firmware(struct mtk_vcodec_fw *fw)
+{

[ ... ]

drivers/media/platform/mediatek/vcodec/common/mtk_vcodec_fw_vcp.c line 403

Quoted 8 lines
drivers/media/platform/mediatek/vcodec/common/mtk_vcodec_fw_vcp.c new file +8 -0
+		fw->vcp->is_register_done = true;
+	}
+
+	fw->vcp->is_init_done = true;
+
+	for (i = 0; i < VCP_IPI_MAX; i++)
+		mutex_init(&fw->vcp->ipi_desc[i].lock);
+	mutex_init(&fw->vcp->ipi_mutex);

[Severity: Medium]
Is it safe to set is_init_done to true before initializing the mutexes and waitqueues? A concurrent operation observing is_init_done == true could attempt to use these uninitialized primitives, leading to lockdep crashes or NULL pointer dereferences.

[ ... ]

drivers/media/platform/mediatek/vcodec/common/mtk_vcodec_fw_vcp.c line 421

Quoted 7 lines
drivers/media/platform/mediatek/vcodec/common/mtk_vcodec_fw_vcp.c new file +7 -0
+	fw->vcp->vsi_addr = vcp_device->ops->get_mem_virt(vcp_device, mem_id);
+	fw->vcp->vsi_core_addr = fw->vcp->vsi_addr + VCODEC_VSI_LEN;
+	fw->vcp->vsi_size = vcp_device->ops->get_mem_size(vcp_device, mem_id);
+	fw->vcp->iova_addr = vcp_device->ops->get_mem_iova(vcp_device, mem_id);
+
+	dev_dbg(&fw->pdev->dev, "vdec vcp init done => va: %p size:0x%x iova:%p.\n",
+		fw->vcp->vsi_addr, fw->vcp->vsi_size, &fw->vcp->iova_addr);

[Severity: Low]
Does this print the memory address of the iova_addr variable instead of its value? The %p format specifier prints the pointer itself, but %pad should be used for printing a dma_addr_t passed by reference.

[ ... ]

Quoted 2 lines
drivers/media/platform/mediatek/vcodec/common/mtk_vcodec_fw_vcp.c new file +2 -0
+static void mtk_vcodec_vcp_release(struct mtk_vcodec_fw *fw)
+{

[ ... ]

drivers/media/platform/mediatek/vcodec/common/mtk_vcodec_fw_vcp.c line 515

Quoted 12 lines
drivers/media/platform/mediatek/vcodec/common/mtk_vcodec_fw_vcp.c new file +12 -0
+	ret = vcp_device->ops->deregister_feature(vcp_device, fw->vcp->feature_id);
+	if (ret < 0) {
+		dev_err(dev, "deregister feature_id(%d) fail(%d)\n", fw->vcp->feature_id, ret);
+		return;
+	}
+
+	fw->vcp->is_register_done = false;
+	fw->vcp->is_init_done = false;
+
+put_device:
+	mtk_vcodec_vcp_put_device(fw);
+}

[Severity: Medium]
Will this early return leak the remoteproc reference? By returning early when deregister_feature fails, the function bypasses the put_device cleanup label, preventing the remote processor driver from being cleanly unloaded.

Quoted 7 lines
drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_drv.c +0 -0
index 426d060d937c1..0f9cb651e59b0 100644
@@ -376,16 +376,22 @@ static int mtk_vcodec_probe(struct platform_device *pdev)
 	if (!of_property_read_u32(pdev->dev.of_node, "mediatek,vpu",
 				  &rproc_phandle)) {

drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_drv.c static int mtk_vcodec_probe(struct platform_device *pdev) · line 387

Quoted 10 lines
drivers/media/platform/mediatek/vcodec/decoder/mtk_vcodec_dec_drv.c +6 -0
 		fw_type = VPU;
+		dev->fw_init = mtk_vcodec_fw_vpu_init;
 	} else if (!of_property_read_u32(pdev->dev.of_node, "mediatek,scp",
 					 &rproc_phandle)) {
 		fw_type = SCP;
+		dev->fw_init = mtk_vcodec_fw_scp_init;
+	} else if (!of_property_read_u32(pdev->dev.of_node, "mediatek,vcp",
+					 &rproc_phandle)) {
+		fw_type = VCP;
+		dev->fw_init = mtk_vcodec_fw_vcp_init;

[Severity: Low]
Is the fw_type variable unused after this assignment? The mtk_vcodec_fw_select call below was updated to use dev->fw_init instead, which leaves fw_type orphaned and could trigger a -Wunused-but-set-variable compiler warning.

Quoted 11 lines
drivers/media/platform/mediatek/vcodec/encoder/mtk_vcodec_enc_drv.c +1 -0
index 4e4541b2fc8ea..811bc62a196f5 100644
@@ -259,16 +259,18 @@ static int mtk_vcodec_probe(struct platform_device *pdev)
 	if (!of_property_read_u32(pdev->dev.of_node, "mediatek,vpu",
 				  &rproc_phandle)) {
 		fw_type = VPU;
+		dev->fw_init = mtk_vcodec_fw_vpu_init;
 	} else if (!of_property_read_u32(pdev->dev.of_node, "mediatek,scp",
 					 &rproc_phandle)) {

drivers/media/platform/mediatek/vcodec/encoder/mtk_vcodec_enc_drv.c static int mtk_vcodec_probe(struct platform_device *pdev) · line 273

Quoted 10 lines
drivers/media/platform/mediatek/vcodec/encoder/mtk_vcodec_enc_drv.c +2 -1
 		fw_type = SCP;
+		dev->fw_init = mtk_vcodec_fw_scp_init;
 	} else {
 		dev_err(&pdev->dev, "[MTK VCODEC] Could not get venc IPI device");
 		return -ENODEV;
 	}
 	dma_set_max_seg_size(&pdev->dev, UINT_MAX);
 
-	dev->fw_handler = mtk_vcodec_fw_select(dev, fw_type, ENCODER);
+	dev->fw_handler = mtk_vcodec_fw_select(dev, ENCODER, dev->fw_init);

[Severity: Low]
Similarly here, is fw_type unused after assignment now that mtk_vcodec_fw_select has been updated to take the dev->fw_init callback?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#7 lore raw

Re: [PATCH v8 04/14] media: mediatek: vcodec: get different firmware ipi id

In reply to Kyrie Wu
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] media: mtk-vcodec: OOB write via unvalidated -EINVAL from fw_get_ipi --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] media: mtk-vcodec: OOB write via unvalidated -EINVAL from fw_get_ipi --

commit b7bb8fd08e359be31186a13bf22647a6a15432b2
Author: Kyrie Wu <[email protected]>

media: mediatek: vcodec: get different firmware ipi id

This commit updates the driver to get the inter-processor interrupt (IPI) ID dynamically based on the firmware type and hardware index. This allows supporting different architectures instead of using hardcoded IDs.

Quoted 22 lines
drivers/media/platform/mediatek/vcodec/common/mtk_vcodec_fw.c +14 -0
index 552d0d8a84444..8630e871d78e2 100644
@@ -3,6 +3,20 @@
 #include "../decoder/mtk_vcodec_dec_drv.h"
 #include "../encoder/mtk_vcodec_enc_drv.h"
 #include "mtk_vcodec_fw_priv.h"
+#include "mtk_vcodec_fw_vcp.h"
+
+int mtk_vcodec_fw_get_ipi(enum mtk_vcodec_fw_type type, int hw_id)
+{
+	switch (type) {
+	case SCP:
+		return hw_id == MTK_VDEC_LAT0 ? SCP_IPI_VDEC_LAT : SCP_IPI_VDEC_CORE;
+	case VCP:
+		return hw_id == MTK_VDEC_LAT0 ? VCP_IPI_LAT_DECODER : VCP_IPI_CORE_DECODER;
+	default:
+		return -EINVAL;
+	}
+}
+EXPORT_SYMBOL_GPL(mtk_vcodec_fw_get_ipi);

[ ... ]

Quoted 13 lines
drivers/media/platform/mediatek/vcodec/decoder/vdec/vdec_h264_req_multi_if.c +1 -0
index 10359ce9b934e..69d60717181a6 100644
@@ -1204,6 +1204,7 @@ static int vdec_h264_slice_single_decode(void *h_vdec, struct mtk_vcodec_mem *bs
 
 static int vdec_h264_slice_init(struct mtk_vcodec_dec_ctx *ctx)
 {
+	enum mtk_vcodec_fw_type fw_type = ctx->dev->fw_handler->type;
 	struct vdec_h264_slice_inst *inst;
 	int err, vsi_size;
 	unsigned char *temp;

drivers/media/platform/mediatek/vcodec/decoder/vdec/vdec_h264_req_multi_if.c static int vdec_h264_slice_init(struct mtk_vcodec_dec_ctx *ctx) · line 1219

Quoted 7 lines
drivers/media/platform/mediatek/vcodec/decoder/vdec/vdec_h264_req_multi_if.c +2 -2
@@ -1214,8 +1215,8 @@ static int vdec_h264_slice_init(struct mtk_vcodec_dec_ctx *ctx)
 
 	inst->ctx = ctx;
 
-	inst->vpu.id = SCP_IPI_VDEC_LAT;
-	inst->vpu.core_id = SCP_IPI_VDEC_CORE;
+	inst->vpu.id = mtk_vcodec_fw_get_ipi(fw_type, MTK_VDEC_LAT0);
+	inst->vpu.core_id = mtk_vcodec_fw_get_ipi(fw_type, MTK_VDEC_CORE);

[Severity: High]
Does this code handle cases where mtk_vcodec_fw_get_ipi() returns an error code?

If an unsupported firmware type (such as VPU) is provided, mtk_vcodec_fw_get_ipi() will return -EINVAL (-22), which is then assigned directly to inst->vpu.id and inst->vpu.core_id without validation.

When this propagates to vpu_ipi_register() via vpu_dec_init():

drivers/media/platform/mediatek/vpu/mtk_vpu.c:vpu_ipi_register() {
    ...
    if (id < IPI_VPU_MAX && handler) {
        ipi_desc = vpu->ipi_desc;
        ipi_desc[id].name = name;
    ...
}

On ARM64 (AArch64), the AAPCS64 dictates that the underlying type for an enum whose values all fit in a signed 32-bit integer is a signed int, making enum ipi_id signed.

The bounds check evaluates -22 < IPI_VPU_MAX as true. This bypasses the check and executes a negative index out-of-bounds write to the ipi_desc array.

Could this lead to memory corruption (slab underflow) before the mtk_vpu struct if a malformed Device Tree pairs a stateless decoder compatible string with a VPU firmware handler?

Quoted 3 lines
drivers/media/platform/mediatek/vcodec/decoder/vdec/vdec_h264_req_multi_if.c +0 -0
 	inst->vpu.ctx = ctx;
 	inst->vpu.codec_type = ctx->current_codec;
 	inst->vpu.capture_type = ctx->capture_fourcc;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
#9 lore raw

Re: [PATCH v8 05/14] media: mediatek: vcodec: get share memory address

In reply to Kyrie Wu
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] media: vcodec: missing IS_ERR check on fw_map_dm_addr return in VP9/AV1 - [High] media: mediatek…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] media: vcodec: missing IS_ERR check on fw_map_dm_addr return in VP9/AV1
- [High] media: mediatek: OOB read via unvalidated firmware sizes and VCP offsets --

commit e6e728fceb78092db0d746297086675978804b45
Author: Kyrie Wu <[email protected]>

media: mediatek: vcodec: get share memory address

This commit divides the single shared memory for the VCP architecture into multiple functional areas. It introduces helpers to retrieve specific memory addresses and offsets for different codec components.

Quoted 4 lines
drivers/media/platform/mediatek/vcodec/common/mtk_vcodec_fw_vcp.c +0 -0
index ece4e3e87e62d..468c4bf4e48ba 100644

[ ... ]

Quoted 6 lines
drivers/media/platform/mediatek/vcodec/common/mtk_vcodec_fw_vcp.c +6 -0
+static void *mtk_vcodec_vcp_dm_addr(struct mtk_vcodec_fw *fw, u32 mem_type)
+{
+	unsigned char *vsi_core = fw->vcp->vsi_core_addr;
+
+	switch (mem_type) {
+	case ENCODER_MEM:

drivers/media/platform/mediatek/vcodec/common/mtk_vcodec_fw_vcp.c static unsigned int mtk_vcodec_vcp_get_vdec_capa(struct mtk_vcodec_fw *fw) · line 506

Quoted 10 lines
drivers/media/platform/mediatek/vcodec/common/mtk_vcodec_fw_vcp.c +10 -0
+	case VCODEC_LAT_MEM:
+		return fw->vcp->vsi_addr;
+	case VCODEC_CORE_MEM:
+		return vsi_core;
+	case VP9_FRAME_MEM:
+		return vsi_core + VCODEC_VSI_LEN;
+	case AV1_CDF_MEM:
+		return vsi_core + VCODEC_VSI_LEN + VP9_FRAME_SIZE;
+	case AV1_IQ_MEM:
+		return vsi_core + VCODEC_VSI_LEN + VP9_FRAME_SIZE + AV1_CDF_SIZE;

[Severity: High]
Does this code compute memory pointers based on hardcoded offsets without first verifying that the configured VCP memory size (fw->vcp->vsi_size) is actually large enough to contain them?

If the device tree memory region is insufficiently sized, could this expose the kernel to out-of-bounds reads?

Quoted 6 lines
drivers/media/platform/mediatek/vcodec/common/mtk_vcodec_fw_vcp.c +4 -0
+	default:
+		break;
+	}
+
 	return NULL;
 }

[ ... ]

Quoted 4 lines
drivers/media/platform/mediatek/vcodec/decoder/vdec/vdec_av1_req_lat_if.c +0 -0
index 756fbb7778b1f..4932ef4695946 100644

[ ... ]

drivers/media/platform/mediatek/vcodec/decoder/vdec/vdec_av1_req_lat_if.c static int vdec_av1_slice_init_cdf_table(struct vdec_av1_slice_instance *instanc · line 787

Quoted 6 lines
drivers/media/platform/mediatek/vcodec/decoder/vdec/vdec_av1_req_lat_if.c +1 -1
@@ -775,7 +784,7 @@ static int vdec_av1_slice_init_cdf_table(struct vdec_av1_slice_instance *instanc
 	ctx = instance->ctx;
 	vsi = instance->vpu.vsi;
 	remote_cdf_table = mtk_vcodec_fw_map_dm_addr(ctx->dev->fw_handler,
-						     (u32)vsi->cdf_table_addr);
+						     vdec_av1_get_cdf_table_addr(ctx, vsi));

[Severity: High]
Is memory being copied based on an untrusted size parameter (vsi->cdf_table_size) provided by firmware?

Looking at vdec_av1_slice_init_cdf_table(), if a compromised firmware supplies an excessively large size, couldn't this cause the kernel to read out of bounds from remote_cdf_table into the dynamically sized DMA buffer? This seems unmitigated for non-EXT capability devices.

Quoted 3 lines
drivers/media/platform/mediatek/vcodec/decoder/vdec/vdec_av1_req_lat_if.c +0 -0
 	if (IS_ERR(remote_cdf_table)) {
 		mtk_vdec_err(ctx, "failed to map cdf table\n");
 		return PTR_ERR(remote_cdf_table);

[ ... ]

drivers/media/platform/mediatek/vcodec/decoder/vdec/vdec_av1_req_lat_if.c static int vdec_av1_slice_init(struct mtk_vcodec_dec_ctx *ctx) · line 1936

Quoted 9 lines
drivers/media/platform/mediatek/vcodec/decoder/vdec/vdec_av1_req_lat_if.c +2 -2
@@ -1904,8 +1931,8 @@ static int vdec_av1_slice_init(struct mtk_vcodec_dec_ctx *ctx)
 		goto error_vsi;
 	}
 	instance->init_vsi = vsi;
-	instance->core_vsi = mtk_vcodec_fw_map_dm_addr(ctx->dev->fw_handler, (u32)vsi->core_vsi);
-
+	instance->core_vsi = mtk_vcodec_fw_map_dm_addr(ctx->dev->fw_handler,
+						       vdec_av1_get_core_vsi_addr(ctx, vsi));
 	if (!instance->core_vsi) {

[Severity: High]
Could an error pointer bypass this check when mtk_vcodec_fw_map_dm_addr() fails?

Since mtk_vcodec_fw_map_dm_addr() returns ERR_PTR(-EINVAL) on VPU and SCP failures, it seems an error pointer would slip past this NULL check and lead to invalid memory accesses in subsequent memcpy operations.

Quoted 2 lines
drivers/media/platform/mediatek/vcodec/decoder/vdec/vdec_av1_req_lat_if.c +0 -0
 		mtk_vdec_err(ctx, "failed to get AV1 core vsi\n");
 		ret = -EINVAL;

[ ... ]

Quoted 4 lines
drivers/media/platform/mediatek/vcodec/decoder/vdec/vdec_vp9_req_lat_if.c +0 -0
index b492686784ec3..ee4d1cd82426f 100644

[ ... ]

drivers/media/platform/mediatek/vcodec/decoder/vdec/vdec_vp9_req_lat_if.c static int vdec_vp9_slice_init_default_frame_ctx(struct vdec_vp9_slice_instance · line 527

Quoted 7 lines
drivers/media/platform/mediatek/vcodec/decoder/vdec/vdec_vp9_req_lat_if.c +1 -1
@@ -514,7 +523,7 @@ static int vdec_vp9_slice_init_default_frame_ctx(struct vdec_vp9_slice_instance
 		return -EINVAL;
 
 	remote_frame_ctx = mtk_vcodec_fw_map_dm_addr(ctx->dev->fw_handler,
-						     (u32)vsi->default_frame_ctx);
+						     vdec_vp9_get_frame_ctx_addr(ctx, vsi));
 	if (!remote_frame_ctx) {

[Severity: High]
Is it possible for mtk_vcodec_fw_map_dm_addr() to return an ERR_PTR here?

If mapping fails, does this check miss the error pointer, allowing an invalid memory location to be passed to kmemdup() below?

Quoted 2 lines
drivers/media/platform/mediatek/vcodec/decoder/vdec/vdec_vp9_req_lat_if.c +0 -0
 		mtk_vdec_err(ctx, "failed to map default frame ctx\n");
 		return -EINVAL;

[ ... ]

drivers/media/platform/mediatek/vcodec/decoder/vdec/vdec_vp9_req_lat_if.c static int vdec_vp9_slice_init(struct mtk_vcodec_dec_ctx *ctx) · line 1886

Quoted 7 lines
drivers/media/platform/mediatek/vcodec/decoder/vdec/vdec_vp9_req_lat_if.c +1 -1
@@ -1864,7 +1882,7 @@ static int vdec_vp9_slice_init(struct mtk_vcodec_dec_ctx *ctx)
 	}
 	instance->init_vsi = vsi;
 	instance->core_vsi = mtk_vcodec_fw_map_dm_addr(ctx->dev->fw_handler,
-						       (u32)vsi->core_vsi);
+						       vdec_vp9_get_core_vsi_addr(ctx, vsi));
 	if (!instance->core_vsi) {

[Severity: High]
What happens if mtk_vcodec_fw_map_dm_addr() returns an error pointer?

If it only checks for NULL, it appears the error pointer could be stored in instance->core_vsi and erroneously used as a valid memory location later.

Quoted 2 lines
drivers/media/platform/mediatek/vcodec/decoder/vdec/vdec_vp9_req_lat_if.c +0 -0
 		mtk_vdec_err(ctx, "failed to get VP9 core vsi\n");
 		ret = -EINVAL;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
#23 lore raw

Re: [PATCH v8 13/14] media: mediatek: decoder: fill av1 buffer size with picinfo

In reply to Kyrie Wu
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Critical] media: mtk-vcodec: OOB DMA write due to concurrent global picinfo mutation in stateless AV1…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Critical] media: mtk-vcodec: OOB DMA write due to concurrent global picinfo mutation in stateless AV1 decoder --

commit 4ecde7c3d617adc20b638103986012affff901df
Author: Kyrie Wu <[email protected]>

media: mediatek: decoder: fill av1 buffer size with picinfo

This patch modifies the AV1 decoder to use the buffer sizes calculated in the vcp/scp, filling each frame buffer size directly from picinfo rather than computing it locally from the frame width and height.

Quoted 3 lines
drivers/media/platform/mediatek/vcodec/decoder/vdec/vdec_av1_req_lat_if.c +0 -0

[ ... ]

Quoted 2 lines
drivers/media/platform/mediatek/vcodec/decoder/vdec/vdec_av1_req_lat_if.c +0 -0
@@ -1811,18 +1811,19 @@ static int vdec_av1_slice_setup_core_buffer(struct vdec_av1_slice_instance *inst
 {

[ ... ]

drivers/media/platform/mediatek/vcodec/decoder/vdec/vdec_av1_req_lat_if.c static int vdec_av1_slice_setup_core_buffer(struct vdec_av1_slice_instance *inst · line 1825

Quoted 6 lines
drivers/media/platform/mediatek/vcodec/decoder/vdec/vdec_av1_req_lat_if.c +4 -0
 	/* frame buffer */
 	vsi->fb.y.dma_addr = fb->base_y.dma_addr;
+
+	vsi->fb.y.size = instance->ctx->picinfo.fb_sz[0];
+	vsi->fb.c.size = instance->ctx->picinfo.fb_sz[1];
+

[Severity: Critical]
Does this introduce a race condition with concurrent requests?

The driver uses an asynchronous architecture where requests are processed sequentially in the lat phase by mtk_vdec_worker, but core work is queued and executed asynchronously.

If mtk_vdec_worker processes a new request, it synchronously updates the global instance->ctx->picinfo to match the new sequence controls. Concurrently, if the core worker processing the previous request executes vdec_av1_slice_setup_core_buffer(), it will now read the mutated instance->ctx->picinfo.fb_sz instead of the request-local dimension data.

drivers/media/platform/mediatek/vcodec/decoder/vdec/vdec_av1_req_lat_if.c static int vdec_av1_slice_setup_core_buffer(struct vdec_av1_slice_instance *inst · line 1827

Quoted 3 lines
drivers/media/platform/mediatek/vcodec/decoder/vdec/vdec_av1_req_lat_if.c +1 -1
 	if (plane == 1)
-		vsi->fb.c.dma_addr = fb->base_y.dma_addr + size;
+		vsi->fb.c.dma_addr = fb->base_y.dma_addr + vsi->fb.y.size;

[Severity: Critical]
Can this regression lead to an out-of-bounds dma write?

If the subsequent request specifies a larger resolution, vsi->fb.y.size here will be larger than the actually allocated vb2 capture buffer for the current request.

Because this calculated c-plane dma address is not validated against the physical capacity of the underlying vb2 buffer, the hardware could perform an out-of-bounds write when it processes this offset. Userspace could potentially trigger this by queuing sequential requests with different resolutions.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=13
#5 lore raw

Re: [PATCH v8 03/14] media: mediatek: vcodec: add driver to support vcp encoder

In reply to Kyrie Wu
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider: - [High] media: mtk_vcodec: encoder initialization fails due to unmapped IPI ID for VCP - [High] media…

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [High] media: mtk_vcodec: encoder initialization fails due to unmapped IPI ID for VCP
- [High] media: mtk_vcodec: delayed ACK causes protocol desynchronization in VCP IPC
- [High] media: vcodec: vcp encoder fails to initialize due to out-of-bounds ipi_id
- [High] media: mediatek: vcodec: UAF in IPI message handlers on device close due to IPC timeout --

commit c1db7dc6cad08382fb97ce9b64a0f609116bfae6
Author: Kyrie Wu <[email protected]>

media: mediatek: vcodec: add driver to support vcp encoder

This commit adds encoder support for the VCP interface to allow communication between the encoder and VCP hardware.

Quoted 4 lines
drivers/media/platform/mediatek/vcodec/common/mtk_vcodec_fw_vcp.c +0 -0
index 150d842c7d226..ece4e3e87e62d 100644

[ ... ]

Quoted 5 lines
drivers/media/platform/mediatek/vcodec/common/mtk_vcodec_fw_vcp.c +0 -1
@@ -223,8 +225,13 @@ static int mtk_vcodec_vcp_msg_ipi_send(struct mtk_vcodec_fw *fw, int id, void *b
 	}
 
 	mutex_lock(&vcp->ipi_mutex);
-	feature_id = VDEC_FEATURE_ID;

drivers/media/platform/mediatek/vcodec/common/mtk_vcodec_fw_vcp.c static int mtk_vcodec_vcp_msg_ipi_send(struct mtk_vcodec_fw *fw, int id, void *b · line 236

Quoted 10 lines
drivers/media/platform/mediatek/vcodec/common/mtk_vcodec_fw_vcp.c +7 -1
-	mailbox_id = IPI_OUT_VDEC_1;
+	if (fw->fw_use == ENCODER) {
+		feature_id = VENC_FEATURE_ID;
+		mailbox_id = IPI_OUT_VENC_0;
+	} else {
+		feature_id = VDEC_FEATURE_ID;
+		mailbox_id = IPI_OUT_VDEC_1;
+	}
 
 	timeout_jiffies = jiffies + msecs_to_jiffies(VCP_SYNC_TIMEOUT_MS);

[Severity: High]
Will this initialization succeed for encoders?

The encoder driver appears to initialize its ID to SCP_IPI_VENC_H264 (4) or IPI_VENC_H264 (3), as seen in h264_enc_init():

    if (is_ext)
        inst->vpu_inst.id = SCP_IPI_VENC_H264;
    else
        inst->vpu_inst.id = IPI_VENC_H264;

However, earlier in mtk_vcodec_vcp_msg_ipi_send() and in mtk_vcodec_vcp_ipi_register(), the code checks mtk_vcodec_vcp_ipi_id_valid(), which strictly requires the ID to be less than VCP_IPI_MAX.

Since VCP_IPI_MAX is 3, these encoder IDs will always fail the validation check, returning -EINVAL and preventing the VCP encoder from communicating.

[Severity: High]
Could a delayed ACK from the firmware cause memory corruption or state desynchronization?

When waiting for an ACK later in mtk_vcodec_vcp_msg_ipi_send(), the function uses a hardcoded 100ms timeout and relies on a shared boolean:

wait_ack:
ret = wait_event_interruptible_timeout(*msg_wq, *msg_signaled,
msecs_to_jiffies(IPI_TIMEOUT_MS));

If the firmware takes longer than 100ms to reply, the wait times out and returns an error. However, the firmware might still process the message and send the ACK later.

This appears to lead to two potential issues:

1. If the timeout occurs during device teardown, such as when sending a DEINIT message before closing the device, the driver proceeds to free the instance via kfree(inst). When the delayed ACK eventually arrives, the asynchronous message thread calls the IPI handler:

vpu_enc_ipi_handler()

    vpu = (struct venc_vpu_inst *)(unsigned long)msg->venc_inst;
    mtk_venc_debug(vpu->ctx, ...);

The handler extracts the freed instance from the message and dereferences vpu->ctx, resulting in a use-after-free.

2. Because there are no sequence numbers matching ACKs to requests, a delayed ACK could arrive during a subsequent message on the same ID. The new message resets the shared flag and waits, but the delayed ACK from the previous message would immediately wake it up prematurely, desynchronizing the state between the driver and the firmware.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
#15 lore raw

Re: [PATCH v8 08/14] dt-bindings: media: mediatek,vcodec-subdev-decoder: Add MT8196

In reply to Kyrie Wu
Quoted 10 lines · Kyrie Wu

Compared to previous ICs, the MT8196 supports a 10-bit decoder and has a decoding capability of 4K@120fps. It also supports 36-bit DRAM IOVA address and Video Power Control to optimize bandwidth and voltage usage.

---
 .../media/mediatek,vcodec-subdev-decoder.yaml | 49 ++++++++++++++++++-
 1 file changed, 48 insertions(+), 1 deletion(-)

Honestly, I see random approach your patches visible in how inconsistently you use subject prefixes.

Where did you describe REASON to drop the tag?

Quoted 48 lines
Documentation/devicetree/bindings/media/mediatek,vcodec-subdev-decoder.yaml +17 -1
index d1d209cb..d8f25ccd 100644
@@ -76,6 +76,7 @@ properties:
       - mediatek,mt8186-vcodec-dec
       - mediatek,mt8188-vcodec-dec
       - mediatek,mt8195-vcodec-dec
+      - mediatek,mt8196-vcodec-dec
 
   reg:
     minItems: 1
@@ -96,6 +97,11 @@ properties:
       The node of system control processor (SCP), using
       the remoteproc & rpmsg framework.
 
+  mediatek,vcp:
+    $ref: /schemas/types.yaml#/definitions/phandle
+    description: Phandle to the Video Companion Processor (VCP) that runs the
+      video decoder firmware.
+
   "#address-cells":
     const: 2
 
@@ -163,7 +169,6 @@ required:
   - compatible
   - reg
   - iommus
-  - mediatek,scp
   - ranges
 
 if:
@@ -179,6 +184,48 @@ then:
     - interrupts
 
 allOf:
+  - if:
+      properties:
+        compatible:
+          contains:
+            const: mediatek,mt8196-vcodec-dec
+
+    then:
+      required:
+        - mediatek,vcp
+
+      properties:

Documentation/devicetree/bindings/media/mediatek,vcodec-subdev-decoder.yaml then: · line 207

Quoted 10 lines
Documentation/devicetree/bindings/media/mediatek,vcodec-subdev-decoder.yaml +10 -0
+        mediatek,scp: false
+
+      patternProperties:
+        '^video-codec@[0-9a-f]+$':
+          properties:
+            compatible:
+              enum:
+                - mediatek,mtk-vcodec-core
+                - mediatek,mtk-vcodec-lat
+

Missing constraints for clocks

Documentation/devicetree/bindings/media/mediatek,vcodec-subdev-decoder.yaml then: · line 213

Quoted 6 lines
Documentation/devicetree/bindings/media/mediatek,vcodec-subdev-decoder.yaml +6 -0
+            clock-names:
+              items:
+                - const: sel
+                - const: vdec
+                - const: lat
+                - const: top

Best regards,
Krzysztof

#16 lore raw

Re: [PATCH v8 08/14] dt-bindings: media: mediatek,vcodec-subdev-decoder: Add MT8196

Quoted 95 lines · Krzysztof Kozlowski
Quoted 11 lines · Kyrie Wu

Compared to previous ICs, the MT8196 supports a 10-bit decoder and has a decoding capability of 4K@120fps. It also supports 36-bit DRAM IOVA address and Video Power Control to optimize bandwidth and voltage usage.

---
 .../media/mediatek,vcodec-subdev-decoder.yaml | 49
++++++++++++++++++-

 1 file changed, 48 insertions(+), 1 deletion(-)

Honestly, I see random approach your patches visible in how inconsistently you use subject prefixes.

Where did you describe REASON to drop the tag?

Quoted 55 lines

diff --git a/Documentation/devicetree/bindings/media/mediatek,vcodec-subdev- decoder.yaml b/Documentation/devicetree/bindings/media/mediatek,vcodec-subdev- decoder.yaml
index d1d209cb..d8f25ccd 100644
--- a/Documentation/devicetree/bindings/media/mediatek,vcodec- subdev-decoder.yaml
+++ b/Documentation/devicetree/bindings/media/mediatek,vcodec- subdev-decoder.yaml

(quoted diff) +0 -0
@@ -76,6 +76,7 @@ properties:

       - mediatek,mt8186-vcodec-dec
       - mediatek,mt8188-vcodec-dec
       - mediatek,mt8195-vcodec-dec
+      - mediatek,mt8196-vcodec-dec

   reg:
     minItems: 1

(quoted diff) +0 -0
@@ -96,6 +97,11 @@ properties:

       The node of system control processor (SCP), using the remoteproc & rpmsg framework.

+  mediatek,vcp:
+    $ref: /schemas/types.yaml#/definitions/phandle
+    description: Phandle to the Video Companion Processor (VCP) that runs the
+      video decoder firmware.
+
   "#address-cells":
     const: 2

(quoted diff) +0 -0
@@ -163,7 +169,6 @@ required:

   - compatible
   - reg
   - iommus
-  - mediatek,scp
   - ranges

 if:

(quoted diff) +0 -0
@@ -179,6 +184,48 @@ then:

     - interrupts

 allOf:
+  - if:
+      properties:
+        compatible:
+          contains:
+            const: mediatek,mt8196-vcodec-dec

Documentation/devicetree/bindings/media/mediatek,vcodec-subdev-decoder.yaml +6 -0
+
+    then:
+      required:
+        - mediatek,vcp
+
+      properties:

Documentation/devicetree/bindings/media/mediatek,vcodec-subdev-decoder.yaml then: · line 207

Quoted 10 lines
Documentation/devicetree/bindings/media/mediatek,vcodec-subdev-decoder.yaml +10 -0
+        mediatek,scp: false
+
+      patternProperties:
+        '^video-codec@[0-9a-f]+$':
+          properties:
+            compatible:
+              enum:
+                - mediatek,mtk-vcodec-core
+                - mediatek,mtk-vcodec-lat
+

Missing constraints for clocks

Quoted 6 lines

+            clock-names:
+              items:
+                - const: sel
+                - const: vdec
+                - const: lat
+                - const: top

Quoted 3 lines

Best regards,
Krzysztof

Hi Krzysztof,

I did not explain why mediatek,scp was removed from the common required list. MT8196 uses mediatek,vcp, while the older SoCs still require mediatek,scp. I will describe this explicitly in the v9 commit message.

I will also change the subject to:

media: dt-bindings: mediatek,vcodec-subdev-decoder: Add MT8196

and constrain the MT8196 child-node clocks array to exactly four entries by adding maxItems: 4, matching the four clock-names.

Thank you for the review.

Best regards,
Kyrie