media: mediatek: jpeg: add MT8196 JPEG support
Subsystems (4)
PATCH v18 3/13 media: mediatek: jpeg: fix buffer structure size and layout
In reply to Kyrie WuUpdates the buf_struct_size in the JPEG driver to use the correct structure mtk_jpeg_src_buf instead of v4l2_m2m_buffer, ensuring proper handling of source buffers. The previous buffer size led to mismatches and potential issues during video buffer handling.
Moves frame_num in the mtk_jpeg_src_buf structure to align with other members appropriately. The first field of the driver-specific buffer structure must be the subsystem-specificstruct (vb2_v4l2_buffer in the case of V4L2).
--- drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c | 2 +- drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c +1 -1
@@ -1092,7 +1092,7 @@ static int mtk_jpeg_queue_init(void *priv, struct vb2_queue *src_vq, dst_vq->type =V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE ; dst_vq->io_modes =VB2_DMABUF |VB2_MMAP ; dst_vq->drv_priv = ctx; - dst_vq->buf_struct_size =sizeof (struct v4l2_m2m_buffer ); + dst_vq->buf_struct_size =sizeof (struct mtk_jpeg_src_buf ); dst_vq->ops = jpeg->variant->qops; dst_vq->mem_ops = &vb2_dma_contig_memops; dst_vq->timestamp_flags =V4L2_BUF_FLAG_TIMESTAMP_COPY ;
drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.h +1 -1
@@ -85,10 +85,10 @@ struct mtk_jpeg_variant { };struct mtk_jpeg_src_buf { -u32 frame_num;struct vb2_v4l2_buffer b;struct list_head list;u32 bs_size; +u32 frame_num;struct mtk_jpeg_dec_param dec_param;struct mtk_jpeg_ctx *curr_ctx;
Signature · 1 line
2.51.0.windows.2
PATCH v18 4/13 media: mediatek: jpeg: Fix buffer completion on multi-core streaming stop
In reply to Kyrie WuEnhances the Mediatek JPEG driver's stability and reliability by ensuring that all queued buffers are processed before stopping the streaming in multi-core environments. It introduces a call to vb2_wait_for_all_buffers() in the mtk_jpeg_enc_stop_streaming() and mtk_jpeg_dec_stop_streaming() functions when the multi_core variant is enabled. This change ensures that no buffers are left unprocessed, preventing potential data loss or corruption during multi-core flow.
--- drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c | 8 ++++++++ 1 file changed, 8 insertions(+)
drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c +8 -0
@@ -850,8 +850,12 @@ static struct vb2_v4l2_buffer *mtk_jpeg_buf_remove(struct mtk_jpeg_ctx *ctx,static void mtk_jpeg_enc_stop_streaming (struct vb2_queue *q) {struct mtk_jpeg_ctx *ctx =vb2_get_drv_priv (q); +struct mtk_jpeg_dev *jpeg = ctx->jpeg;struct vb2_v4l2_buffer *vb; +if (jpeg->variant->multi_core) +vb2_wait_for_all_buffers (q); +while ((vb =mtk_jpeg_buf_remove (ctx, q->type)))v4l2_m2m_buf_done (vb,VB2_BUF_STATE_ERROR ); }
@@ -859,6 +863,7 @@ static void mtk_jpeg_enc_stop_streaming(struct vb2_queue *q)static void mtk_jpeg_dec_stop_streaming (struct vb2_queue *q) {struct mtk_jpeg_ctx *ctx =vb2_get_drv_priv (q); +struct mtk_jpeg_dev *jpeg = ctx->jpeg;struct vb2_v4l2_buffer *vb;/*
@@ -866,6 +871,9 @@ static void mtk_jpeg_dec_stop_streaming(struct vb2_queue *q)* Before STREAMOFF, we still have to return the old resolution and * subsampling. Update capture queue when the stream is off. */ +if (jpeg->variant->multi_core) +vb2_wait_for_all_buffers (q); +if (ctx->state ==MTK_JPEG_SOURCE_CHANGE &&V4L2_TYPE_IS_CAPTURE (q->type)) {struct mtk_jpeg_src_buf *src_buf;
Signature · 1 line
2.51.0.windows.2
v18 cover media: mediatek: jpeg: add MT8196 JPEG support
From: Kyrie Wu (吴晗) <[email protected]>
This series have the follow changing:
Firstly fix some bugs, including resolution change handleing, stop streaming sw flow, fix buffer layout and clock setting to support multi-hw jpeg working and others.
Secondly add mt8196 jpegdec and jpegenc compatible to support MT8196 kernel driver.
Lastly, Add smmu setting to support smmu and iommu at the same time.
This series has been tested with MT8196 tast test. Encoding and decoding worked for this chip.
Patches 1 fix jpeg hw count setting to support different chips. Patches 2 fix jpeg buffer payload setting to handle buffer
size bug while resolution changed.
Patches 3 fix jpeg dst buffer layout.
Patches 4 fix multi-core stop streaming flow
Patches 5 fix multi-core clk suspend/resume setting
Patches 6 fix buffer state update timing
Patches 7 fix decoding resolution change operation
Patches 8 fix remove buffer operation
Patches 9-11 add jpeg encoder and decoder compatible.
Patch 12 adds MT8196 JPEG SMMU bindings.
Patch 13 adds jpeg smmu sid setting.
---
Changes compared with v17:
--Keep suspend/resume callbacks and clock ownership on multi-hw component
devices. Leave single-hw callbacks on the master, and wait for the active job before disabling component clocks.
--Add a standalone DT binding patch for mediatek,smmu-config and the
dedicated MT8196 JPEG SMMU configuration syscon at 0x38d0000.
Changes compared with v16:
--Rebased on top of the latest media tree
Changes compared with v15:
--Set max_hw_count from the DT child count and update hw_rdy from child hardware probes in patch 1. --Fix patch 2 commit message wording per review.
--Rework patch 6 commit message per review.
--Remove redundant devm_clk_bulk_get() calls in patch 5 per review comments.
--Use %pe to print smmu_regmap error pointer in patch 12 to fix media-ci/static warnings.
Changes compared with v14:
--Rebased on top of the latest media tree
Changes compared with v13:
--Rebased on top of the latest media tree
Changes compared with v12:
--Rebased on top of the latest media tree
--fix kernel rebot build warnings in patch 5
Changes compared with v11:
--Rebased on top of the latest media tree --Some modifications for patch v11's review comments. --add reviewer to commit messages
Changes compared with v10:
--Rebased on top of the latest media tree
--add reviewer to commit messages
Changes compared with v9:
--Rebased on top of the latest media tree
Changes compared with v8:
--Rebased on top of the latest media tree
Changes compared with v7:
--Rebased on top of the latest media tree
Changes compared with v6:
--Rebased on top of the latest media tree
Changes compared with v5:
--reorder the patches set.
--fix commit message of patch 1-8.
Changes compared with v4:
--fix kernel robot build errors for patch 4.
--add reviewer for patch 1 and patch 2.
Changes compared with v3:
--change patch subject of jpeg encoder and decoder compatible.
Changes compared with v2:
--refactor smmu sid setting function interface --Some modifications for patch v2's review comments.
Changes compared with v1:
--refine jpeg dt-bindings for MT8196 --optimize software code to manage jpeg HW count --refactor smmu sid setting function interface --Some modifications for patch v1's review comments.
Kyrie Wu (13):
media: mediatek: jpeg: fix jpeg cores' amounts setting
media: mediatek: jpeg: fix jpeg buffer payload size setting
media: mediatek: jpeg: fix buffer structure size and layout
media: mediatek: jpeg: Fix buffer completion on multi-core streaming
stop
media: mediatek: jpeg: Fix multi-core clk suspend and resume setting
media: mediatek: jpeg: fix buffer state update timing
media: mediatek: jpeg: fix resolution change event handling in decoder
media: mediatek: jpeg: fix remove buffer removal timing for multi-core
media: dt-bindings: mediatek,jpeg: Add mediatek, mt8196-jpgdec
compatible
media: dt-bindings: mediatek,jpeg: Add mediatek, mt8196-jpgenc
compatible
media: mediatek: jpeg: add jpeg compatible
dt-bindings: mediatek: Add MT8196 JPEG SMMU bindings
media: mediatek: jpeg: add jpeg smmu sid setting
.../media/mediatek,mt8195-jpegdec.yaml | 24 ++- .../media/mediatek,mt8195-jpegenc.yaml | 24 ++- .../mediatek/mediatek,mt8196-jpeg-smmu.yaml | 41 +++++ MAINTAINERS | 2 + .../platform/mediatek/jpeg/mtk_jpeg_core.c | 156 +++++++++++++----- .../platform/mediatek/jpeg/mtk_jpeg_core.h | 19 ++- .../platform/mediatek/jpeg/mtk_jpeg_dec_hw.c | 85 +++++++++- .../platform/mediatek/jpeg/mtk_jpeg_enc_hw.c | 83 +++++++++- 8 files changed, 377 insertions(+), 57 deletions(-) create mode 100644 Documentation/devicetree/bindings/soc/mediatek/mediatek,mt8196-jpeg-smmu.yaml
Signature · 1 line
2.51.0.windows.2
PATCH v18 1/13 media: mediatek: jpeg: fix jpeg cores' amounts setting
Different ICs have different amounts of cores,
use a variable to set the cores' amounts.
--- drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c | 11 ++++++----- drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.h | 2 ++ .../media/platform/mediatek/jpeg/mtk_jpeg_dec_hw.c | 2 ++ .../media/platform/mediatek/jpeg/mtk_jpeg_enc_hw.c | 2 ++ 4 files changed, 12 insertions(+), 5 deletions(-)
drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c +6 -5
@@ -1325,7 +1325,8 @@ static int mtk_jpeg_probe(struct platform_device *pdev)for_each_child_of_node (pdev->dev.of_node, child) num_child++; -atomic_set (&jpeg->hw_rdy, num_child); + jpeg->max_hw_count = num_child; +atomic_set (&jpeg->hw_rdy,0 );atomic_set (&jpeg->hw_index,0 ); jpeg->workqueue = alloc_ordered_workqueue(MTK_JPEG_NAME,
@@ -1462,7 +1463,7 @@ static int mtk_jpegenc_get_hw(struct mtk_jpeg_ctx *ctx)int i;spin_lock_irqsave (&jpeg->hw_lock, flags); -for (i =0 ; i <MTK_JPEGENC_HW_MAX ; i++) { +for (i =0 ; i < jpeg->max_hw_count; i++) { comp_jpeg = jpeg->enc_hw_dev[i];if (comp_jpeg->hw_state ==MTK_JPEG_HW_IDLE ) { hw_id = i;
@@ -1509,7 +1510,7 @@ static int mtk_jpegdec_get_hw(struct mtk_jpeg_ctx *ctx)int i;spin_lock_irqsave (&jpeg->hw_lock, flags); -for (i =0 ; i <MTK_JPEGDEC_HW_MAX ; i++) { +for (i =0 ; i < jpeg->max_hw_count; i++) { comp_jpeg = jpeg->dec_hw_dev[i];if (comp_jpeg->hw_state ==MTK_JPEG_HW_IDLE ) { hw_id = i;
@@ -1592,7 +1593,7 @@ static void mtk_jpegenc_worker(struct work_struct *work) jpeg_work);struct mtk_jpeg_dev *jpeg = ctx->jpeg; -for (i =0 ; i <MTK_JPEGENC_HW_MAX ; i++) +for (i =0 ; i < jpeg->max_hw_count; i++) comp_jpeg[i] = jpeg->enc_hw_dev[i]; i =0 ;
@@ -1687,7 +1688,7 @@ static void mtk_jpegdec_worker(struct work_struct *work)struct mtk_jpeg_fb fb;unsigned long flags; -for (i =0 ; i <MTK_JPEGDEC_HW_MAX ; i++) +for (i =0 ; i < jpeg->max_hw_count; i++) comp_jpeg[i] = jpeg->dec_hw_dev[i]; i =0 ;
drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.h +2 -0
@@ -212,6 +212,7 @@ struct mtk_jpegdec_comp_dev {* @reg_decbase: jpg decode register base addr * @dec_hw_dev: jpg decode hardware device * @hw_index: jpg hw index +* @max_hw_count: jpeg hw-core count */ struct mtk_jpeg_dev {struct mutex lock;
@@ -234,6 +235,7 @@ struct mtk_jpeg_dev {void __iomem *reg_decbase[MTK_JPEGDEC_HW_MAX ];struct mtk_jpegdec_comp_dev *dec_hw_dev[MTK_JPEGDEC_HW_MAX ];atomic_t hw_index; +u32 max_hw_count; };/**
drivers/media/platform/mediatek/jpeg/mtk_jpeg_dec_hw.c +2 -0
@@ -668,6 +668,8 @@ static int mtk_jpegdec_hw_probe(struct platform_device *pdev)platform_set_drvdata (pdev, dev);pm_runtime_enable (&pdev->dev); +atomic_inc (&master_dev->hw_rdy); +return 0 ; }
drivers/media/platform/mediatek/jpeg/mtk_jpeg_enc_hw.c +2 -0
@@ -389,6 +389,8 @@ static int mtk_jpegenc_hw_probe(struct platform_device *pdev)platform_set_drvdata (pdev, dev);pm_runtime_enable (&pdev->dev); +atomic_inc (&master_dev->hw_rdy); +return 0 ; }
Signature · 1 line
2.51.0.windows.2
PATCH v18 2/13 media: mediatek: jpeg: fix jpeg buffer payload size setting
In reply to Kyrie WuFor multi-core jpegdec, if one core gets resolution change event, the payload size, representing the size of Y/C data, needs to change. But others are decoding at the same time and it can not be changed immediately, which results in the payload size not matching the real buffer length.
The payload size must be less than the real buffer length to avoid warning logs.
--- .../platform/mediatek/jpeg/mtk_jpeg_core.c | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-)
drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c +14 -5
@@ -702,6 +702,7 @@ static int mtk_jpeg_buf_prepare(struct vb2_buffer *vb)struct mtk_jpeg_ctx *ctx =vb2_get_drv_priv (vb->vb2_queue);struct mtk_jpeg_q_data *q_data =NULL ;struct v4l2_plane_pix_format plane_fmt = {}; +size_t max_size;int i; q_data =mtk_jpeg_get_q_data (ctx, vb->vb2_queue->type);
@@ -710,12 +711,20 @@ static int mtk_jpeg_buf_prepare(struct vb2_buffer *vb)for (i =0 ; i < q_data->fmt->colplanes; i++) { plane_fmt = q_data->pix_mp.plane_fmt[i]; + max_size = plane_fmt.sizeimage; +if (ctx->enable_exif && - q_data->fmt->fourcc ==V4L2_PIX_FMT_JPEG ) -vb2_set_plane_payload (vb, i, plane_fmt.sizeimage + -MTK_JPEG_MAX_EXIF_SIZE ); -else -vb2_set_plane_payload (vb, i, plane_fmt.sizeimage); + q_data->fmt->fourcc ==V4L2_PIX_FMT_JPEG ) { + max_size +=MTK_JPEG_MAX_EXIF_SIZE ; + +vb2_set_plane_payload (vb, i, +MIN (vb->planes[i].length, + max_size)); + }else { +vb2_set_plane_payload (vb, i, +MIN (plane_fmt.sizeimage, + vb->planes[i].length)); + } }return 0 ;
Signature · 1 line
2.51.0.windows.2
PATCH v18 5/13 media: mediatek: jpeg: Fix multi-core clk suspend and resume setting
In reply to Kyrie WuMake multi-hw component devices own their clocks and suspend callbacks. Keep single-core clock callbacks on the master device.
Stop new jobs and wait for the active job with v4l2_m2m_suspend() before disabling component clocks. Runtime-PM references keep clocks enabled until job completion or timeout.
--- .../platform/mediatek/jpeg/mtk_jpeg_core.c | 28 +++------- .../platform/mediatek/jpeg/mtk_jpeg_dec_hw.c | 56 +++++++++++++++++-- .../platform/mediatek/jpeg/mtk_jpeg_enc_hw.c | 54 ++++++++++++++++-- 3 files changed, 109 insertions(+), 29 deletions(-)
drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c +9 -19
@@ -1115,6 +1115,9 @@ static void mtk_jpeg_clk_on(struct mtk_jpeg_dev *jpeg) {int ret; +if (jpeg->variant->multi_core) +return ; + ret =clk_bulk_prepare_enable (jpeg->variant->num_clks, jpeg->variant->clks);if (ret)
@@ -1123,6 +1126,9 @@ static void mtk_jpeg_clk_on(struct mtk_jpeg_dev *jpeg)static void mtk_jpeg_clk_off (struct mtk_jpeg_dev *jpeg) { +if (jpeg->variant->multi_core) +return ; +clk_bulk_disable_unprepare (jpeg->variant->num_clks, jpeg->variant->clks); }
@@ -1648,13 +1654,6 @@ static void mtk_jpegenc_worker(struct work_struct *work) goto enc_end; } - ret =clk_prepare_enable (comp_jpeg[hw_id]->venc_clk.clks->clk); -if (ret) { -dev_err (jpeg->dev,"%s : %d, jpegenc clk_prepare_enable fail\n" , - __func__, __LINE__); - goto enc_end; - } -v4l2_m2m_src_buf_remove (ctx->fh.m2m_ctx);v4l2_m2m_dst_buf_remove (ctx->fh.m2m_ctx);
@@ -1751,20 +1750,13 @@ static void mtk_jpegdec_worker(struct work_struct *work) jpeg_dst_buf->frame_num = ctx->total_frame_num;mtk_jpegdec_set_hw_param (ctx, hw_id, src_buf, dst_buf); - ret =pm_runtime_get_sync (comp_jpeg[hw_id]->dev); + ret =pm_runtime_resume_and_get (comp_jpeg[hw_id]->dev);if (ret <0 ) {dev_err (jpeg->dev,"%s : %d, pm_runtime_get_sync fail !!!\n" , __func__, __LINE__); goto dec_end; } - ret =clk_prepare_enable (comp_jpeg[hw_id]->jdec_clk.clks->clk); -if (ret) { -dev_err (jpeg->dev,"%s : %d, jpegdec clk_prepare_enable fail\n" , - __func__, __LINE__); - goto clk_end; - } -v4l2_m2m_src_buf_remove (ctx->fh.m2m_ctx);v4l2_m2m_dst_buf_remove (ctx->fh.m2m_ctx);
@@ -1774,7 +1766,7 @@ static void mtk_jpegdec_worker(struct work_struct *work) &dst_buf->vb2_buf, &fb)) {dev_err (jpeg->dev,"%s : %d, mtk_jpeg_set_dec_dst fail\n" , __func__, __LINE__); - goto setdst_end; + goto set_dst_fail; } schedule_delayed_work(&comp_jpeg[hw_id]->job_timeout_work,
@@ -1795,9 +1787,7 @@ static void mtk_jpegdec_worker(struct work_struct *work)return ; -setdst_end: -clk_disable_unprepare (comp_jpeg[hw_id]->jdec_clk.clks->clk); -clk_end: +set_dst_fail:pm_runtime_put (comp_jpeg[hw_id]->dev); dec_end:v4l2_m2m_src_buf_remove (ctx->fh.m2m_ctx);
drivers/media/platform/mediatek/jpeg/mtk_jpeg_dec_hw.c +50 -6
100 lines not on this page · raw message
drivers/media/platform/mediatek/jpeg/mtk_jpeg_enc_hw.c +50 -4
86 lines not on this page · raw message
Signature · 1 line
2.51.0.windows.2
PATCH v18 6/13 media: mediatek: jpeg: fix buffer state update timing
In reply to Kyrie WuUpdate the destination buffer state only after the decoder has selected a hardware core and resumed its runtime PM state successfully. This avoids assigning frame tracking data to buffers that will be returned early, such as when a source resolution change is detected.
Protect the destination buffer state update with the hardware spinlock so the completion path observes a consistent context and frame number. Stop walking the done queue after completing the next expected frame, since the remaining buffers still need to wait for their own frame order.
--- drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c | 9 +++------ drivers/media/platform/mediatek/jpeg/mtk_jpeg_dec_hw.c | 1 + drivers/media/platform/mediatek/jpeg/mtk_jpeg_enc_hw.c | 1 + 3 files changed, 5 insertions(+), 6 deletions(-)
drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c +3 -6
@@ -1735,7 +1735,6 @@ static void mtk_jpegdec_worker(struct work_struct *work)v4l2_m2m_buf_copy_metadata (src_buf, dst_buf); jpeg_src_buf =mtk_jpeg_vb2_to_srcbuf (&src_buf->vb2_buf); - jpeg_dst_buf =mtk_jpeg_vb2_to_srcbuf (&dst_buf->vb2_buf);if (mtk_jpeg_check_resolution_change (ctx, &jpeg_src_buf->dec_param)) {
@@ -1744,11 +1743,6 @@ static void mtk_jpegdec_worker(struct work_struct *work) goto getbuf_fail; } - jpeg_src_buf->curr_ctx = ctx; - jpeg_src_buf->frame_num = ctx->total_frame_num; - jpeg_dst_buf->curr_ctx = ctx; - jpeg_dst_buf->frame_num = ctx->total_frame_num; -mtk_jpegdec_set_hw_param (ctx, hw_id, src_buf, dst_buf); ret =pm_runtime_resume_and_get (comp_jpeg[hw_id]->dev);if (ret <0 ) {
@@ -1773,6 +1767,9 @@ static void mtk_jpegdec_worker(struct work_struct *work) msecs_to_jiffies(MTK_JPEG_HW_TIMEOUT_MSEC));spin_lock_irqsave (&comp_jpeg[hw_id]->hw_lock, flags); + jpeg_dst_buf =mtk_jpeg_vb2_to_srcbuf (&dst_buf->vb2_buf); + jpeg_dst_buf->curr_ctx = ctx; + jpeg_dst_buf->frame_num = ctx->total_frame_num; ctx->total_frame_num++;mtk_jpeg_dec_reset (comp_jpeg[hw_id]->reg_base); mtk_jpeg_dec_set_config(comp_jpeg[hw_id]->reg_base,
drivers/media/platform/mediatek/jpeg/mtk_jpeg_dec_hw.c +1 -0
@@ -513,6 +513,7 @@ static void mtk_jpegdec_put_buf(struct mtk_jpegdec_comp_dev *jpeg)v4l2_m2m_buf_done (&tmp_dst_done_buf->b,VB2_BUF_STATE_DONE ); ctx->last_done_frame_num++; +break ; } } }
drivers/media/platform/mediatek/jpeg/mtk_jpeg_enc_hw.c +1 -0
@@ -241,6 +241,7 @@ static void mtk_jpegenc_put_buf(struct mtk_jpegenc_comp_dev *jpeg)v4l2_m2m_buf_done (&tmp_dst_done_buf->b,VB2_BUF_STATE_DONE ); ctx->last_done_frame_num++; +break ; } } }
Signature · 1 line
2.51.0.windows.2
PATCH v18 7/13 media: mediatek: jpeg: fix resolution change event handling in decoder
In reply to Kyrie WuThis patch refines the handling of resolution change events within JPEG decoder worker. The mtk_jpeg_set_queue_data function is now called to set up queue data before signaling a source change through mtk_jpeg_queue_src_chg_event. By reorganizing these calls, the patch ensures that necessary queue information is updated prior to transitioning the context state to MTK_JPEG_SOURCE_CHANGE. A condition is added to exit early if the context is already in the MTK_JPEG_SOURCE_CHANGE state, preventing redundant operations and improving processing efficiency.
--- drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c +5 -1
@@ -1738,11 +1738,15 @@ static void mtk_jpegdec_worker(struct work_struct *work)if (mtk_jpeg_check_resolution_change (ctx, &jpeg_src_buf->dec_param)) { -mtk_jpeg_queue_src_chg_event (ctx); +mtk_jpeg_set_queue_data (ctx, &jpeg_src_buf->dec_param); ctx->state =MTK_JPEG_SOURCE_CHANGE ; +mtk_jpeg_queue_src_chg_event (ctx); goto getbuf_fail; } +if (ctx->state ==MTK_JPEG_SOURCE_CHANGE ) + goto getbuf_fail; +mtk_jpegdec_set_hw_param (ctx, hw_id, src_buf, dst_buf); ret =pm_runtime_resume_and_get (comp_jpeg[hw_id]->dev);if (ret <0 ) {
Signature · 1 line
2.51.0.windows.2
PATCH v18 9/13 media: dt-bindings: mediatek,jpeg: Add mediatek, mt8196-jpgdec compatible
In reply to Kyrie WuCompared to the previous generation IC, the MT8196 uses SMMU instead of IOMMU and supports features such as dynamic voltage and frequency scaling. Therefore, add "mediatek,mt8196-jpgdec" compatible to the binding document.
--- .../bindings/media/mediatek,mt8195-jpegdec.yaml | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
Documentation/devicetree/bindings/media/mediatek,mt8195-jpegdec.yaml +6 -2
@@ -14,7 +14,9 @@ description: properties: compatible: - const: mediatek,mt8195-jpgdec + enum: + - mediatek,mt8195-jpgdec + - mediatek,mt8196-jpgdec power-domains: maxItems: 1
@@ -44,7 +46,9 @@ patternProperties: properties: compatible: - const: mediatek,mt8195-jpgdec-hw + enum: + - mediatek,mt8195-jpgdec-hw + - mediatek,mt8196-jpgdec-hw reg: maxItems: 1
Signature · 1 line
2.51.0.windows.2
PATCH v18 10/13 media: dt-bindings: mediatek,jpeg: Add mediatek, mt8196-jpgenc compatible
In reply to Kyrie WuCompared to the previous generation IC, the MT8196 uses SMMU instead of IOMMU and supports features such as dynamic voltage and frequency scaling. Therefore, add "mediatek,mt8196-jpgenc" compatible to the binding document.
--- .../bindings/media/mediatek,mt8195-jpegenc.yaml | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
Documentation/devicetree/bindings/media/mediatek,mt8195-jpegenc.yaml +6 -2
@@ -14,7 +14,9 @@ description: properties: compatible: - const: mediatek,mt8195-jpgenc + enum: + - mediatek,mt8195-jpgenc + - mediatek,mt8196-jpgenc power-domains: maxItems: 1
@@ -44,7 +46,9 @@ patternProperties: properties: compatible: - const: mediatek,mt8195-jpgenc-hw + enum: + - mediatek,mt8195-jpgenc-hw + - mediatek,mt8196-jpgenc-hw reg: maxItems: 1
Signature · 1 line
2.51.0.windows.2
PATCH v18 11/13 media: mediatek: jpeg: add jpeg compatible
In reply to Kyrie WuAdd jpeg dec and enc compatible for mt8196
--- .../platform/mediatek/jpeg/mtk_jpeg_core.c | 36 +++++++++++++++++++ .../platform/mediatek/jpeg/mtk_jpeg_dec_hw.c | 3 ++ .../platform/mediatek/jpeg/mtk_jpeg_enc_hw.c | 3 ++ 3 files changed, 42 insertions(+)
drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c +36 -0
@@ -1919,6 +1919,20 @@ static struct mtk_jpeg_variant mtk8195_jpegenc_drvdata = { .jpeg_worker = mtk_jpegenc_worker, }; +static struct mtk_jpeg_variant mtk8196_jpegenc_drvdata = { + .formats = mtk_jpeg_enc_formats, + .num_formats =MTK_JPEG_ENC_NUM_FORMATS , + .qops = &mtk_jpeg_enc_qops, + .m2m_ops = &mtk_jpeg_multicore_enc_m2m_ops, + .dev_name ="mtk-jpeg-enc" , + .ioctl_ops = &mtk_jpeg_enc_ioctl_ops, + .out_q_default_fourcc =V4L2_PIX_FMT_YUYV , + .cap_q_default_fourcc =V4L2_PIX_FMT_JPEG , + .multi_core = true, + .jpeg_worker = mtk_jpegenc_worker, + .support_34bit = true, +}; +static const struct mtk_jpeg_variant mtk8195_jpegdec_drvdata = { .formats = mtk_jpeg_dec_formats, .num_formats =MTK_JPEG_DEC_NUM_FORMATS ,
@@ -1932,6 +1946,20 @@ static const struct mtk_jpeg_variant mtk8195_jpegdec_drvdata = { .jpeg_worker = mtk_jpegdec_worker, }; +static const struct mtk_jpeg_variant mtk8196_jpegdec_drvdata = { + .formats = mtk_jpeg_dec_formats, + .num_formats =MTK_JPEG_DEC_NUM_FORMATS , + .qops = &mtk_jpeg_dec_qops, + .m2m_ops = &mtk_jpeg_multicore_dec_m2m_ops, + .dev_name ="mtk-jpeg-dec" , + .ioctl_ops = &mtk_jpeg_dec_ioctl_ops, + .out_q_default_fourcc =V4L2_PIX_FMT_JPEG , + .cap_q_default_fourcc =V4L2_PIX_FMT_YUV420M , + .multi_core = true, + .jpeg_worker = mtk_jpegdec_worker, + .support_34bit = true, +}; +static const struct of_device_id mtk_jpeg_match[] = { { .compatible ="mediatek,mt8173-jpgdec" ,
@@ -1953,6 +1981,14 @@ static const struct of_device_id mtk_jpeg_match[] = { .compatible = "mediatek,mt8195-jpgdec", .data = &mtk8195_jpegdec_drvdata, }, + { + .compatible = "mediatek,mt8196-jpgenc", + .data = &mtk8196_jpegenc_drvdata, + }, + { + .compatible = "mediatek,mt8196-jpgdec", + .data = &mtk8196_jpegdec_drvdata, + }, {}, };
drivers/media/platform/mediatek/jpeg/mtk_jpeg_dec_hw.c +3 -0
@@ -45,6 +45,9 @@ static const struct of_device_id mtk_jpegdec_hw_ids[] = { { .compatible = "mediatek,mt8195-jpgdec-hw", }, + { + .compatible = "mediatek,mt8196-jpgdec-hw", + }, {}, };MODULE_DEVICE_TABLE (of, mtk_jpegdec_hw_ids);
drivers/media/platform/mediatek/jpeg/mtk_jpeg_enc_hw.c +3 -0
@@ -51,6 +51,9 @@ static const struct of_device_id mtk_jpegenc_drv_ids[] = { { .compatible = "mediatek,mt8195-jpgenc-hw", }, + { + .compatible = "mediatek,mt8196-jpgenc-hw", + }, {}, };MODULE_DEVICE_TABLE (of, mtk_jpegenc_drv_ids);
Signature · 1 line
2.51.0.windows.2
PATCH v18 13/13 media: mediatek: jpeg: add jpeg smmu sid setting
In reply to Kyrie WuAdd a configuration to set jpeg dec & enc smmu sid
--- .../platform/mediatek/jpeg/mtk_jpeg_core.c | 29 +++++++++++++++++++ .../platform/mediatek/jpeg/mtk_jpeg_core.h | 15 ++++++++++ .../platform/mediatek/jpeg/mtk_jpeg_dec_hw.c | 23 +++++++++++++++ .../platform/mediatek/jpeg/mtk_jpeg_enc_hw.c | 23 +++++++++++++++ 4 files changed, 90 insertions(+)
drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c +29 -0
@@ -15,6 +15,7 @@#include <linux/of_platform.h> #include <linux/platform_device.h> #include <linux/pm_runtime.h> +#include <linux/regmap.h> #include <linux/slab.h> #include <linux/spinlock.h> #include <media/v4l2-event.h>
@@ -1602,6 +1603,22 @@ static irqreturn_t mtk_jpeg_enc_done(struct mtk_jpeg_dev *jpeg)return IRQ_HANDLED ; } +static void mtk_jpeg_set_smmu_sid (struct regmap *smmu_regmap,unsigned int sid) +{ +if (sid ==JPG_REG_GUSER_ID_DEC_SID ) +regmap_update_bits (smmu_regmap,JPEG_DEC_SMMU_SID , +JPG_REG_GUSER_ID_MASK << +JPG_REG_DEC_GUSER_ID_SHIFT , +JPG_REG_GUSER_ID_DEC_SID << +JPG_REG_DEC_GUSER_ID_SHIFT ); +else +regmap_update_bits (smmu_regmap,JPEG_ENC_SMMU_SID , +JPG_REG_GUSER_ID_MASK << +JPG_REG_ENC_GUSER_ID_SHIFT , +JPG_REG_GUSER_ID_ENC_SID << +JPG_REG_ENC_GUSER_ID_SHIFT ); +} +static void mtk_jpegenc_worker (struct work_struct *work) {struct mtk_jpegenc_comp_dev *comp_jpeg[MTK_JPEGENC_HW_MAX ];
@@ -1663,6 +1680,11 @@ static void mtk_jpegenc_worker(struct work_struct *work) jpeg_dst_buf->frame_num = ctx->total_frame_num; ctx->total_frame_num++;mtk_jpeg_enc_reset (comp_jpeg[hw_id]->reg_base); + +if (jpeg->variant->support_smmu && comp_jpeg[hw_id]->smmu_regmap) +mtk_jpeg_set_smmu_sid (comp_jpeg[hw_id]->smmu_regmap, +JPG_REG_GUSER_ID_ENC_SID ); +mtk_jpeg_set_enc_dst (ctx, comp_jpeg[hw_id]->reg_base, &dst_buf->vb2_buf);
@@ -1772,6 +1794,11 @@ static void mtk_jpegdec_worker(struct work_struct *work) jpeg_dst_buf->frame_num = ctx->total_frame_num; ctx->total_frame_num++;mtk_jpeg_dec_reset (comp_jpeg[hw_id]->reg_base); + +if (jpeg->variant->support_smmu && comp_jpeg[hw_id]->smmu_regmap) +mtk_jpeg_set_smmu_sid (comp_jpeg[hw_id]->smmu_regmap, +JPG_REG_GUSER_ID_DEC_SID ); + mtk_jpeg_dec_set_config(comp_jpeg[hw_id]->reg_base, jpeg->variant->support_34bit, &jpeg_src_buf->dec_param,
@@ -1931,6 +1958,7 @@ static struct mtk_jpeg_variant mtk8196_jpegenc_drvdata = { .multi_core = true, .jpeg_worker = mtk_jpegenc_worker, .support_34bit = true, + .support_smmu = true, };static const struct mtk_jpeg_variant mtk8195_jpegdec_drvdata = {
@@ -1958,6 +1986,7 @@ static const struct mtk_jpeg_variant mtk8196_jpegdec_drvdata = { .multi_core = true, .jpeg_worker = mtk_jpegdec_worker, .support_34bit = true, + .support_smmu = true, };static const struct of_device_id mtk_jpeg_match[] = {
drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.h +15 -0
@@ -11,6 +11,7 @@#include <linux/clk.h> #include <linux/interrupt.h> +#include <linux/mfd/syscon.h> #include <media/v4l2-ctrls.h> #include <media/v4l2-device.h> #include <media/v4l2-fh.h>
@@ -34,6 +35,14 @@#define MTK_JPEG_MAX_EXIF_SIZE (64 * 1024) +#define JPEG_DEC_SMMU_SID 0 +#define JPEG_ENC_SMMU_SID 0 +#define JPG_REG_GUSER_ID_MASK 0x7 +#define JPG_REG_GUSER_ID_DEC_SID 0x4 +#define JPG_REG_GUSER_ID_ENC_SID 0x5 +#define JPG_REG_DEC_GUSER_ID_SHIFT 8 +#define JPG_REG_ENC_GUSER_ID_SHIFT 4 +#define MTK_JPEG_ADDR_MASK GENMASK(1, 0)/**
@@ -65,6 +74,7 @@ enum mtk_jpeg_ctx_state {* @multi_core: mark jpeg hw is multi_core or not * @jpeg_worker: jpeg dec or enc worker * @support_34bit: flag to check support for 34-bit DMA address +* @support_smmu: flag to check if support smmu */ struct mtk_jpeg_variant {struct clk_bulk_data *clks;
@@ -82,6 +92,7 @@ struct mtk_jpeg_variant {bool multi_core;void (*jpeg_worker)(struct work_struct *work);bool support_34bit; +bool support_smmu; };struct mtk_jpeg_src_buf {
@@ -150,6 +161,7 @@ struct mtk_jpegdec_clk {* @hw_param: jpeg encode hw parameters * @hw_state: record hw state * @hw_lock: spinlock protecting the hw device resource +* @smmu_regmap: SMMU registers mapping */ struct mtk_jpegenc_comp_dev {struct device *dev;
@@ -163,6 +175,7 @@ struct mtk_jpegenc_comp_dev {enum mtk_jpeg_hw_state hw_state;/* spinlock protecting the hw device resource */ spinlock_t hw_lock; +struct regmap *smmu_regmap; };/**
@@ -177,6 +190,7 @@ struct mtk_jpegenc_comp_dev {* @hw_param: jpeg decode hw parameters * @hw_state: record hw state * @hw_lock: spinlock protecting hw +* @smmu_regmap: SMMU registers mapping */ struct mtk_jpegdec_comp_dev {struct device *dev;
@@ -190,6 +204,7 @@ struct mtk_jpegdec_comp_dev {enum mtk_jpeg_hw_state hw_state;/* spinlock protecting the hw device resource */ spinlock_t hw_lock; +struct regmap *smmu_regmap; };/**
drivers/media/platform/mediatek/jpeg/mtk_jpeg_dec_hw.c +23 -0
@@ -613,6 +613,25 @@ static int mtk_jpegdec_hw_init_irq(struct mtk_jpegdec_comp_dev *dev)return 0 ; } +static int mtk_jpegdec_smmu_init (struct mtk_jpegdec_comp_dev *dev) +{ +struct mtk_jpeg_dev *master_dev = dev->master_dev; + +if (!master_dev->variant->support_smmu) +return 0 ; + + dev->smmu_regmap = +syscon_regmap_lookup_by_phandle (dev->plat_dev->dev.of_node, +"mediatek,smmu-config" ); +if (IS_ERR (dev->smmu_regmap)) { +return dev_err_probe (dev->dev,PTR_ERR (dev->smmu_regmap), +"mmap smmu_base failed(%pe)\n" , + dev->smmu_regmap); + } + +return 0 ; +} +static int mtk_jpegdec_hw_probe (struct platform_device *pdev) {struct mtk_jpegdec_clk *jpegdec_clk;
@@ -665,6 +684,10 @@ static int mtk_jpegdec_hw_probe(struct platform_device *pdev) master_dev->reg_decbase[i] = dev->reg_base; dev->master_dev = master_dev; + ret =mtk_jpegdec_smmu_init (dev); +if (ret) +return ret; +platform_set_drvdata (pdev, dev);pm_runtime_enable (&pdev->dev);
drivers/media/platform/mediatek/jpeg/mtk_jpeg_enc_hw.c +23 -0
@@ -338,6 +338,25 @@ static int mtk_jpegenc_hw_init_irq(struct mtk_jpegenc_comp_dev *dev)return 0 ; } +static int mtk_jpegenc_smmu_init (struct mtk_jpegenc_comp_dev *dev) +{ +struct mtk_jpeg_dev *master_dev = dev->master_dev; + +if (!master_dev->variant->support_smmu) +return 0 ; + + dev->smmu_regmap = +syscon_regmap_lookup_by_phandle (dev->plat_dev->dev.of_node, +"mediatek,smmu-config" ); +if (IS_ERR (dev->smmu_regmap)) { +return dev_err_probe (dev->dev,PTR_ERR (dev->smmu_regmap), +"mmap smmu_base failed(%pe)\n" , + dev->smmu_regmap); + } + +return 0 ; +} +static int mtk_jpegenc_hw_probe (struct platform_device *pdev) {struct mtk_jpegenc_clk *jpegenc_clk;
@@ -388,6 +407,10 @@ static int mtk_jpegenc_hw_probe(struct platform_device *pdev) master_dev->reg_encbase[i] = dev->reg_base; dev->master_dev = master_dev; + ret =mtk_jpegenc_smmu_init (dev); +if (ret) +return ret; +platform_set_drvdata (pdev, dev);pm_runtime_enable (&pdev->dev);
Signature · 1 line
2.51.0.windows.2
PATCH v18 8/13 media: mediatek: jpeg: fix remove buffer removal timing for multi-core
In reply to Kyrie WuMove calls to v4l2_m2m_src/dst_buf_remove() inside of the spinlock protected scope to ensure all necessary operations are performed before buffers are removed from their queues and ensure proper synchronization of buffer handling to avoid buffer lost.
--- drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-)
drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c +4 -6
@@ -1654,9 +1654,6 @@ static void mtk_jpegenc_worker(struct work_struct *work) goto enc_end; } -v4l2_m2m_src_buf_remove (ctx->fh.m2m_ctx); -v4l2_m2m_dst_buf_remove (ctx->fh.m2m_ctx); -schedule_delayed_work (&comp_jpeg[hw_id]->job_timeout_work,msecs_to_jiffies (MTK_JPEG_HW_TIMEOUT_MSEC ));
@@ -1674,6 +1671,8 @@ static void mtk_jpegenc_worker(struct work_struct *work) &src_buf->vb2_buf);mtk_jpeg_set_enc_params (ctx, comp_jpeg[hw_id]->reg_base);mtk_jpeg_enc_start (comp_jpeg[hw_id]->reg_base); +v4l2_m2m_src_buf_remove (ctx->fh.m2m_ctx); +v4l2_m2m_dst_buf_remove (ctx->fh.m2m_ctx);v4l2_m2m_job_finish (jpeg->m2m_dev, ctx->fh.m2m_ctx);spin_unlock_irqrestore (&comp_jpeg[hw_id]->hw_lock, flags);
@@ -1755,9 +1754,6 @@ static void mtk_jpegdec_worker(struct work_struct *work) goto dec_end; } -v4l2_m2m_src_buf_remove (ctx->fh.m2m_ctx); -v4l2_m2m_dst_buf_remove (ctx->fh.m2m_ctx); -mtk_jpeg_set_dec_src (ctx, &src_buf->vb2_buf, &bs); if (mtk_jpeg_set_dec_dst(ctx, &jpeg_src_buf->dec_param,
@@ -1782,6 +1778,8 @@ static void mtk_jpegdec_worker(struct work_struct *work) jpeg_src_buf->bs_size, &bs, &fb); +v4l2_m2m_src_buf_remove (ctx->fh.m2m_ctx); +v4l2_m2m_dst_buf_remove (ctx->fh.m2m_ctx);mtk_jpeg_dec_start (comp_jpeg[hw_id]->reg_base);v4l2_m2m_job_finish (jpeg->m2m_dev, ctx->fh.m2m_ctx);spin_unlock_irqrestore (&comp_jpeg[hw_id]->hw_lock, flags);
Signature · 1 line
2.51.0.windows.2
PATCH v18 12/13 dt-bindings: mediatek: Add MT8196 JPEG SMMU bindings
In reply to Kyrie WuDocument the decoder and encoder phandles and describe the dedicated JPEG SMMU configuration syscon for MT8196.
--- .../media/mediatek,mt8195-jpegdec.yaml | 16 ++++++++ .../media/mediatek,mt8195-jpegenc.yaml | 16 ++++++++ .../mediatek/mediatek,mt8196-jpeg-smmu.yaml | 41 +++++++++++++++++++ MAINTAINERS | 2 + 4 files changed, 75 insertions(+) create mode 100644 Documentation/devicetree/bindings/soc/mediatek/mediatek,mt8196-jpeg-smmu.yaml
Documentation/devicetree/bindings/media/mediatek,mt8195-jpegdec.yaml +16 -0
@@ -73,6 +73,22 @@ patternProperties: power-domains: maxItems: 1 + mediatek,smmu-config: + $ref: /schemas/types.yaml#/definitions/phandle + description: + Phandle to the MT8196 JPEG SMMU configuration syscon used to + program this decoder's GUSER_ID fields. + + allOf: + - if: + properties: + compatible: + contains: + const: mediatek,mt8196-jpgdec-hw + then: + required: + - mediatek,smmu-config + required: - compatible - reg
Documentation/devicetree/bindings/media/mediatek,mt8195-jpegenc.yaml +16 -0
@@ -73,6 +73,22 @@ patternProperties: power-domains: maxItems: 1 + mediatek,smmu-config: + $ref: /schemas/types.yaml#/definitions/phandle + description: + Phandle to the MT8196 JPEG SMMU configuration syscon used to + program this encoder's GUSER_ID fields. + + allOf: + - if: + properties: + compatible: + contains: + const: mediatek,mt8196-jpgenc-hw + then: + required: + - mediatek,smmu-config + required: - compatible - reg
Documentation/devicetree/bindings/soc/mediatek/mediatek,mt8196-jpeg-smmu.yaml new file +41 -0
41 lines not on this page · raw message
MAINTAINERS +2 -0
@@ -3164,6 +3164,7 @@ L: [email protected] (moderated for non-subscribers) S: Maintained W: https://mtk.wiki.kernel.org/ C: irc://irc.libera.chat/linux-mediatek +F: Documentation/devicetree/bindings/soc/mediatek/mediatek,mt8196-jpeg-smmu.yaml F: arch/arm/boot/dts/mediatek/ F: arch/arm/mach-mediatek/ F: arch/arm64/boot/dts/mediatek/
@@ -16665,6 +16666,7 @@ F: include/dt-bindings/memory/mt*-port.h MEDIATEK JPEG DRIVER M: Bin Liu <[email protected]> S: Supported +F: Documentation/devicetree/bindings/media/mediatek,mt8195-jpeg*.yaml F: Documentation/devicetree/bindings/media/mediatek-jpeg-*.yaml F: drivers/media/platform/mediatek/jpeg/
Signature · 1 line
2.51.0.windows.2
[email protected]
AI reviewRe: [PATCH v18 01/13] media: mediatek: jpeg: fix jpeg cores' amounts setting
In reply to Kyrie WuThank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [High] media: mtk-jpeg: Parent probe resets hw_rdy and hw_index after child probes - [High] media…
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] media: mtk-jpeg: Parent probe resets hw_rdy and hw_index after child probes
- [High] media: mtk-jpeg: Stack buffer overflow from unbounded max_hw_count
- [High] media: mtk-jpeg: NULL pointer dereference on deferred or failed child probes --
commit cdbdedb11a1804a4b9d2eaffc176211c725722a4
Author: Kyrie Wu <[email protected]>
media: mediatek: jpeg: fix jpeg cores' amounts setting
Different ICs have different amounts of cores,
use a variable to set the cores' amounts.
drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c static int mtk_jpeg_probe(struct platform_device *pdev) · line 1328
Quoted 10 lines
drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c +1
-1@@ -1325,7 +1325,8 @@ static int mtk_jpeg_probe(struct platform_device *pdev)for_each_child_of_node (pdev->dev.of_node, child) num_child++; -atomic_set (&jpeg->hw_rdy, num_child); + jpeg->max_hw_count = num_child;
[Severity: High]
Does this code overflow the comp_jpeg[] stack arrays in mtk_jpegenc_worker() and mtk_jpegdec_worker()?
The child node count from the device tree is assigned to max_hw_count without verifying it fits within the driver's hardcoded limits (MTK_JPEGENC_HW_MAX or MTK_JPEGDEC_HW_MAX).
If a malformed device tree provides more nodes than these limits, the worker functions will perform out-of-bounds writes on their fixed-size stack arrays.
drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c static int mtk_jpeg_probe(struct platform_device *pdev) · line 1330
Quoted 2 lines
drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c +1
-0+atomic_set (&jpeg->hw_rdy,0 );atomic_set (&jpeg->hw_index,0 );
[Severity: High]
Is it safe to reset these atomic variables here?
Earlier in mtk_jpeg_probe(), devm_of_platform_populate() synchronously probes available child devices. When those child probes succeed, they increment hw_rdy and hw_index, as seen in the changes to mtk_jpegdec_hw_probe() and mtk_jpegenc_hw_probe() below.
By unconditionally resetting hw_rdy and hw_index to 0 here, the state established by the successfully probed children is wiped out. This leaves hw_rdy at 0 despite having ready cores.
When workers subsequently try to consume hardware, they will decrement hw_rdy to a negative value, causing other workers calling wait_event_interruptible() to block indefinitely, leading to a deadlock.
Also, deferred child probes will mistakenly receive index 0, overwriting pointers of previously probed children.
[ ... ]
drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c static int mtk_jpegenc_get_hw(struct mtk_jpeg_ctx *ctx) · line 1468
Quoted 8 lines
drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c +1
-1@@ -1462,7 +1463,7 @@ static int mtk_jpegenc_get_hw(struct mtk_jpeg_ctx *ctx)int i;spin_lock_irqsave (&jpeg->hw_lock, flags); -for (i =0 ; i <MTK_JPEGENC_HW_MAX ; i++) { +for (i =0 ; i < jpeg->max_hw_count; i++) { comp_jpeg = jpeg->enc_hw_dev[i];if (comp_jpeg->hw_state ==MTK_JPEG_HW_IDLE ) {
[Severity: High]
Can this dereference a NULL pointer if a child node fails to probe or is disabled?
max_hw_count is the total number of child nodes in the device tree, but enc_hw_dev[] is only populated when a child driver successfully probes. If a child node has status = "disabled" or fails to probe, its corresponding slot in enc_hw_dev[] remains NULL.
Iterating up to max_hw_count and unconditionally dereferencing comp_jpeg will cause a NULL pointer dereference in this scenario.
Quoted 1 line
drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c +0
-0hw_id = i;
[ ... ]
drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c static int mtk_jpegdec_get_hw(struct mtk_jpeg_ctx *ctx) · line 1515
Quoted 8 lines
drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c +1
-1@@ -1509,7 +1510,7 @@ static int mtk_jpegdec_get_hw(struct mtk_jpeg_ctx *ctx)int i;spin_lock_irqsave (&jpeg->hw_lock, flags); -for (i =0 ; i <MTK_JPEGDEC_HW_MAX ; i++) { +for (i =0 ; i < jpeg->max_hw_count; i++) { comp_jpeg = jpeg->dec_hw_dev[i];if (comp_jpeg->hw_state ==MTK_JPEG_HW_IDLE ) {
[Severity: High]
Similarly, can this trigger a NULL pointer dereference if a decoder child node fails to probe or is disabled?
Quoted 1 line
drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c +0
-0hw_id = i;
[ ... ]
drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c static void mtk_jpegenc_worker(struct work_struct *work) · line 1597
Quoted 7 lines
drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c +1
-1@@ -1592,7 +1593,7 @@ static void mtk_jpegenc_worker(struct work_struct *work) jpeg_work);struct mtk_jpeg_dev *jpeg = ctx->jpeg; -for (i =0 ; i <MTK_JPEGENC_HW_MAX ; i++) +for (i =0 ; i < jpeg->max_hw_count; i++) comp_jpeg[i] = jpeg->enc_hw_dev[i];
[Severity: High]
As mentioned earlier, does this overflow the fixed-size comp_jpeg stack array if max_hw_count exceeds MTK_JPEGENC_HW_MAX?
Additionally, this lockless copy will cache NULL pointers if there were failed child probes, leading to a crash later in the worker when pm_runtime_get_sync(comp_jpeg[hw_id]->dev) is called.
Quoted 1 line
drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c +0
-0i =0 ;
[ ... ]
drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c static void mtk_jpegdec_worker(struct work_struct *work) · line 1692
Quoted 7 lines
drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c +1
-1@@ -1687,7 +1688,7 @@ static void mtk_jpegdec_worker(struct work_struct *work)struct mtk_jpeg_fb fb;unsigned long flags; -for (i =0 ; i <MTK_JPEGDEC_HW_MAX ; i++) +for (i =0 ; i < jpeg->max_hw_count; i++) comp_jpeg[i] = jpeg->dec_hw_dev[i];
[Severity: High]
Similarly, does this overflow the comp_jpeg stack array if max_hw_count exceeds MTK_JPEGDEC_HW_MAX, and can it cache NULL pointers?
Quoted 1 line
drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c +0
-0i =0 ;
[ ... ]
Quoted 25 lines
drivers/media/platform/mediatek/jpeg/mtk_jpeg_dec_hw.c +2
-0@@ -668,6 +668,8 @@ static int mtk_jpegdec_hw_probe(struct platform_device *pdev)platform_set_drvdata (pdev, dev);pm_runtime_enable (&pdev->dev); +atomic_inc (&master_dev->hw_rdy); +return 0 ; }drivers/media/platform/mediatek/jpeg/mtk_jpeg_enc_hw.c +2
-0@@ -389,6 +389,8 @@ static int mtk_jpegenc_hw_probe(struct platform_device *pdev)platform_set_drvdata (pdev, dev);pm_runtime_enable (&pdev->dev); +atomic_inc (&master_dev->hw_rdy); +return 0 ; }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
[email protected]
AI reviewRe: [PATCH v18 04/13] media: mediatek: jpeg: Fix buffer completion on multi-core streaming stop
In reply to Kyrie WuThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] media: deadlock in stop_streaming due to vb2_wait_for_all_buffers() before draining software…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] media: deadlock in stop_streaming due to vb2_wait_for_all_buffers() before draining software queues --
commit 6489658be7c3e86842a167c26d82109d2bba7681
Author: Kyrie Wu <[email protected]>
media: mediatek: jpeg: Fix buffer completion on multi-core streaming stop
This commit calls vb2_wait_for_all_buffers() during stop_streaming to ensure queued buffers are processed before stopping in multi-core environments.
Quoted 5 lines
drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c +0
-0
drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c static struct vb2_v4l2_buffer *mtk_jpeg_buf_remove(struct mtk_jpeg_ctx *ctx, · line 858
Quoted 9 lines
drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c +4
-0@@ -850,8 +850,12 @@ static struct vb2_v4l2_buffer *mtk_jpeg_buf_remove(struct mtk_jpeg_ctx *ctx,static void mtk_jpeg_enc_stop_streaming (struct vb2_queue *q) {struct mtk_jpeg_ctx *ctx =vb2_get_drv_priv (q); +struct mtk_jpeg_dev *jpeg = ctx->jpeg;struct vb2_v4l2_buffer *vb; +if (jpeg->variant->multi_core) +vb2_wait_for_all_buffers (q); +
[Severity: High]
Does this blocking wait introduce a deadlock when buffers are only queued to one side?
In the V4L2 M2M framework, if a userspace application queues a buffer to only the output or capture queue, the hardware worker is never scheduled to run. The buffer remains in the driver's software queue.
Because vb2_wait_for_all_buffers() is called unconditionally before the loop that manually drains the software queue in mtk_jpeg_enc_stop_streaming(), could it block indefinitely waiting for the hardware worker to process buffers it cannot reach?
Quoted 12 lines
drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c +1
-0while ((vb =mtk_jpeg_buf_remove (ctx, q->type)))v4l2_m2m_buf_done (vb,VB2_BUF_STATE_ERROR ); }@@ -859,6 +863,7 @@ static void mtk_jpeg_enc_stop_streaming(struct vb2_queue *q)static void mtk_jpeg_dec_stop_streaming (struct vb2_queue *q) {struct mtk_jpeg_ctx *ctx =vb2_get_drv_priv (q); +struct mtk_jpeg_dev *jpeg = ctx->jpeg;struct vb2_v4l2_buffer *vb;/*
drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c static void mtk_jpeg_dec_stop_streaming(struct vb2_queue *q) · line 876
Quoted 6 lines
drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c +3
-0@@ -866,6 +871,9 @@ static void mtk_jpeg_dec_stop_streaming(struct vb2_queue *q)* Before STREAMOFF, we still have to return the old resolution and * subsampling. Update capture queue when the stream is off. */ +if (jpeg->variant->multi_core) +vb2_wait_for_all_buffers (q); +
[Severity: High]
This appears to have the exact same deadlock potential in mtk_jpeg_dec_stop_streaming() as in the encoder.
Since vb2_wait_for_all_buffers() blocks until the owned driver count reaches zero, won't this block indefinitely when streamoff is called if there are unprocessed buffers trapped in the software queue?
Quoted 3 lines
drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c +0
-0if (ctx->state ==MTK_JPEG_SOURCE_CHANGE &&V4L2_TYPE_IS_CAPTURE (q->type)) {struct mtk_jpeg_src_buf *src_buf;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
[email protected]
AI reviewRe: [PATCH v18 10/13] media: dt-bindings: mediatek,jpeg: Add mediatek, mt8196-jpgenc compatible
In reply to Kyrie WuThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] dt-bindings: media: hardcoded M4U iommus description applied to SMMU --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] dt-bindings: media: hardcoded M4U iommus description applied to SMMU --
commit 4d170d7f6a62a55e32063d072935c6eaecadd30f
Author: Kyrie Wu <[email protected]>
media: dt-bindings: mediatek,jpeg: Add mediatek, mt8196-jpgenc compatible
This commit adds the "mediatek,mt8196-jpgenc" compatible string to the MediaTek JPEG encoder dt-binding document. The commit message notes that the MT8196 uses an SMMU and supports features like dynamic voltage and frequency scaling.
Quoted 5 lines
Documentation/devicetree/bindings/media/mediatek,mt8195-jpegenc.yaml +0
-0
Documentation/devicetree/bindings/media/mediatek,mt8195-jpegenc.yaml description: · line 22
Quoted 10 lines
Documentation/devicetree/bindings/media/mediatek,mt8195-jpegenc.yaml +3
-1@@ -14,7 +14,9 @@ description: properties: compatible: - const: mediatek,mt8195-jpgenc + enum: + - mediatek,mt8195-jpgenc + - mediatek,mt8196-jpgenc power-domains: maxItems: 1
[Severity: Low]
Since the commit message states that MT8196 uses an SMMU instead of an IOMMU, is it correct to retain the existing iommus description for this new compatible?
Looking at properties:iommus in this file, the top-level description is:
iommus:
maxItems: 4
description:
Points to the respective IOMMU block with master port as argument, see Documentation/devicetree/bindings/iommu/mediatek,iommu.yaml for details. Ports are according to the HW.
Because MT8196 uses an SMMU, the "master port" and M4U reference (bindings/iommu/mediatek,iommu.yaml) seem misleading for the newly added mediatek,mt8196-jpgenc compatible. While a later patch in this series adds SMMU configuration properties, it does not appear to fix this descriptive discrepancy.
Could this be updated or conditionally overridden for MT8196?
Documentation/devicetree/bindings/media/mediatek,mt8195-jpegenc.yaml patternProperties: · line 54
Quoted 11 lines
Documentation/devicetree/bindings/media/mediatek,mt8195-jpegenc.yaml +3
-1@@ -44,7 +46,9 @@ patternProperties: properties: compatible: - const: mediatek,mt8195-jpgenc-hw + enum: + - mediatek,mt8195-jpgenc-hw + - mediatek,mt8196-jpgenc-hw reg: maxItems: 1
[Severity: Low]
The child node iommus property (under patternProperties:^jpgenc@[0-9a-f]+$:properties:iommus) also has an M4U-specific description:
iommus:
minItems: 1
maxItems: 32
description:
List of the hardware port in respective IOMMU block for current Socs. Refer to bindings/iommu/mediatek,iommu.yaml.
Would it make sense to update this description to reflect the SMMU stream IDs used by the newly added mediatek,mt8196-jpgenc-hw compatible?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=10
[email protected]
AI reviewRe: [PATCH v18 08/13] media: mediatek: jpeg: fix remove buffer removal timing for multi-core
In reply to Kyrie WuThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Critical] media: mtk-jpeg: M2M buffer list corruption due to late removal --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Critical] media: mtk-jpeg: M2M buffer list corruption due to late removal --
commit efc50151e6e8cc8e10e2f2bc55033a7e5675f87a
Author: Kyrie Wu <[email protected]>
media: mediatek: jpeg: fix remove buffer removal timing for multi-core
This commit moves the calls to v4l2_m2m_src_buf_remove() and v4l2_m2m_dst_buf_remove() inside the spinlock to synchronize buffer handling.
drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c static void mtk_jpegenc_worker(struct work_struct *work) · line 1658
Quoted 13 lines
drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c +0
-3@@ -1654,9 +1654,6 @@ static void mtk_jpegenc_worker(struct work_struct *work) goto enc_end; } -v4l2_m2m_src_buf_remove (ctx->fh.m2m_ctx); -v4l2_m2m_dst_buf_remove (ctx->fh.m2m_ctx); -schedule_delayed_work (&comp_jpeg[hw_id]->job_timeout_work,msecs_to_jiffies (MTK_JPEG_HW_TIMEOUT_MSEC ));
[Severity: Critical]
Does scheduling the timeout work before removing the buffers create a race condition?
If this worker thread stalls for the timeout duration, mtk_jpegenc_timeout_work() can execute concurrently and return the buffers to userspace. If userspace then immediately requeues the buffer with VIDIOC_QBUF, it will be added to the queue via list_add_tail().
Since this worker thread hasn't yet reached v4l2_m2m_src_buf_remove(), the buffer's node would still be linked in the M2M rdy_queue, which could result in list corruption from double linking.
drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c static void mtk_jpegenc_worker(struct work_struct *work) · line 1675
Quoted 7 lines
drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c +2
-0@@ -1674,6 +1671,8 @@ static void mtk_jpegenc_worker(struct work_struct *work) &src_buf->vb2_buf);mtk_jpeg_set_enc_params (ctx, comp_jpeg[hw_id]->reg_base);mtk_jpeg_enc_start (comp_jpeg[hw_id]->reg_base); +v4l2_m2m_src_buf_remove (ctx->fh.m2m_ctx); +v4l2_m2m_dst_buf_remove (ctx->fh.m2m_ctx);
[Severity: Critical]
Are we exposed to a similar list corruption race here by starting the hardware before removing the buffers from the M2M queue?
mtk_jpeg_enc_start() asynchronously kicks off the hardware. If the hardware finishes immediately, it can trigger mtk_jpegenc_hw_irq_handler() on another CPU.
The lockless IRQ handler retrieves the buffers and calls v4l2_m2m_buf_done(), returning them to userspace. If userspace immediately requeues the buffer, VIDIOC_QBUF will add it to the list while it's still linked in the rdy_queue, which can cause list corruption and kernel panics.
It looks like this ordering was handled correctly in mtk_jpegdec_worker(), where mtk_jpeg_dec_start() is called after the buffers are removed. Should mtk_jpeg_enc_start() follow the same pattern here?
[ ... ]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
[email protected]
AI reviewRe: [PATCH v18 06/13] media: mediatek: jpeg: fix buffer state update timing
In reply to Kyrie WuThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] media: Placebo lock and UAF in MTK JPEG worker timeout race --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] media: Placebo lock and UAF in MTK JPEG worker timeout race
--
commit 8295f8055bf1ad5ac4c76dd2772ead51a8aecccb
Author: Kyrie Wu <[email protected]>
media: mediatek: jpeg: fix buffer state update timing
This commit delays the update of the destination buffer state until after the decoder has selected a hardware core and successfully resumed its runtime PM state. It aims to protect the destination buffer state update with the hardware spinlock to avoid assigning frame tracking data to buffers that are returned early.
Quoted 4 lines
drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c +0
-0
[ ... ]
drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c static void mtk_jpegdec_worker(struct work_struct *work) · line 1772
Quoted 7 lines
drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c +3
-0@@ -1773,6 +1767,9 @@ static void mtk_jpegdec_worker(struct work_struct *work) msecs_to_jiffies(MTK_JPEG_HW_TIMEOUT_MSEC));spin_lock_irqsave (&comp_jpeg[hw_id]->hw_lock, flags); + jpeg_dst_buf =mtk_jpeg_vb2_to_srcbuf (&dst_buf->vb2_buf); + jpeg_dst_buf->curr_ctx = ctx; + jpeg_dst_buf->frame_num = ctx->total_frame_num;
[Severity: High]
The commit message states the lock is used so the completion path observes a consistent context and frame number. However, looking at the completion paths in mtk_jpegdec_hw_irq_handler() and mtk_jpegdec_timeout_work(), they do not appear to acquire comp_jpeg[hw_id]->hw_lock. Does this lock provide any mutual exclusion against those paths?
Also, because schedule_delayed_work() arms the timeout before the buffer state is initialized, is there a risk if the worker thread is preempted immediately after arming the timer?
For example, could this sequence occur:
1. The timeout worker fires and executes without hw_lock.
2. It reads the uninitialized frame_num.
3. It adds the buffer to dst_done_queue, resets the hardware, and returns the buffer to userspace via v4l2_m2m_buf_done().
4. The preempted worker thread resumes and concurrently writes to jpeg_dst_buf->frame_num, corrupting the buffer that is already on the completion list or in userspace.
5. It then starts the hardware via mtk_jpeg_dec_start() on the already-returned buffer.
Could this lead to DMA into freed memory?
Quoted 3 lines
drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c +0
-0ctx->total_frame_num++;mtk_jpeg_dec_reset (comp_jpeg[hw_id]->reg_base); mtk_jpeg_dec_set_config(comp_jpeg[hw_id]->reg_base,
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
[email protected]
AI reviewRe: [PATCH v18 11/13] media: mediatek: jpeg: add jpeg compatible
In reply to Kyrie WuThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] media: mediatek: jpeg: incorrect STALL address extension calculation in 34-bit mode --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] media: mediatek: jpeg: incorrect STALL address extension calculation in 34-bit mode --
commit ab319417645f402435984f080997cd803d5661ce
Author: Kyrie Wu <[email protected]>
media: mediatek: jpeg: add jpeg compatible
Add jpeg dec and enc compatible for mt8196
Quoted 10 lines
drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c +2
-0@@ -1919,6 +1919,20 @@ static struct mtk_jpeg_variant mtk8195_jpegenc_drvdata = { .jpeg_worker = mtk_jpegenc_worker, }; +static struct mtk_jpeg_variant mtk8196_jpegenc_drvdata = { + .formats = mtk_jpeg_enc_formats,
drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c static struct mtk_jpeg_variant mtk8195_jpegenc_drvdata = { · line 1933
Quoted 10 lines
drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c +10
-0+ .num_formats = MTK_JPEG_ENC_NUM_FORMATS, + .qops = &mtk_jpeg_enc_qops, + .m2m_ops = &mtk_jpeg_multicore_enc_m2m_ops, + .dev_name = "mtk-jpeg-enc", + .ioctl_ops = &mtk_jpeg_enc_ioctl_ops, + .out_q_default_fourcc = V4L2_PIX_FMT_YUYV, + .cap_q_default_fourcc = V4L2_PIX_FMT_JPEG, + .multi_core = true, + .jpeg_worker = mtk_jpegenc_worker, + .support_34bit = true,
[Severity: High]
This problem wasn't introduced by this patch, but setting support_34bit to true makes a broken 34-bit code path reachable for the first time.
In drivers/media/platform/mediatek/jpeg/mtk_jpeg_enc_hw.c:mtk_jpeg_set_enc_dst(), the STALL address extension is calculated by adding the buffer size directly to the 2-bit extension address:
if (support_34bit) {
addr_ext = FIELD_PREP(MTK_JPEG_ADDR_MASK, upper_32_bits(dma_addr));
writel(addr_ext, base + JPEG_ENC_DEST_ADDR0_EXT);
writel(addr_ext + size, base + JPEG_ENC_STALL_ADDR0_EXT);
}Since size is in bytes, won't adding it to the 2-bit addr_ext write a massively out-of-bounds value to the hardware extension register, rather than correctly extracting the upper 32 bits of dma_addr + size?
Could this corrupt the DMA engine's STALL boundary setup and risk memory corruption or IOMMU faults?
Quoted 2 lines
drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c +2
-0+}; +
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=11
[email protected]
AI reviewRe: [PATCH v18 13/13] media: mediatek: jpeg: add jpeg smmu sid setting
In reply to Kyrie WuThank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] media: mtk-jpeg: UAF and OOB write on smmu_init probe failure - [High] media: mtk-jpeg: stack and…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] media: mtk-jpeg: UAF and OOB write on smmu_init probe failure
- [High] media: mtk-jpeg: stack and heap overflow due to unbounded DT child nodes --
commit 5c85022fd040c3aae35226e60e232c6d2771df6e
Author: Kyrie Wu <[email protected]>
media: mediatek: jpeg: add jpeg smmu sid setting
This commit adds a configuration to set the JPEG decoder and encoder SMMU SID.
Quoted 3 lines
drivers/media/platform/mediatek/jpeg/mtk_jpeg_dec_hw.c +0
-0
[ ... ]
drivers/media/platform/mediatek/jpeg/mtk_jpeg_dec_hw.c static int mtk_jpegdec_hw_probe(struct platform_device *pdev) · line 690
Quoted 8 lines
drivers/media/platform/mediatek/jpeg/mtk_jpeg_dec_hw.c +4
-0@@ -665,6 +684,10 @@ static int mtk_jpegdec_hw_probe(struct platform_device *pdev) master_dev->reg_decbase[i] = dev->reg_base; dev->master_dev = master_dev; + ret =mtk_jpegdec_smmu_init (dev); +if (ret) +return ret; +
[Severity: High]
If mtk_jpegdec_smmu_init() returns an error (such as -EPROBE_DEFER when the SMMU isn't ready), the probe function returns immediately.
Will this leave a dangling pointer in the global master_dev->dec_hw_dev[i] array after devm frees the dev structure?
Additionally, master_dev->hw_index is incremented via atomic_add_return() before this initialization step. Because it is not decremented on this error path, could subsequent probe retries keep incrementing hw_index and eventually cause an out-of-bounds write on the fixed-size dec_hw_dev array?
Quoted 2 lines
drivers/media/platform/mediatek/jpeg/mtk_jpeg_dec_hw.c +0
-0platform_set_drvdata (pdev, dev);pm_runtime_enable (&pdev->dev);
[ ... ]
Quoted 3 lines
drivers/media/platform/mediatek/jpeg/mtk_jpeg_enc_hw.c +0
-0
[ ... ]
drivers/media/platform/mediatek/jpeg/mtk_jpeg_enc_hw.c static int mtk_jpegenc_hw_probe(struct platform_device *pdev) · line 413
Quoted 8 lines
drivers/media/platform/mediatek/jpeg/mtk_jpeg_enc_hw.c +4
-0@@ -388,6 +407,10 @@ static int mtk_jpegenc_hw_probe(struct platform_device *pdev) master_dev->reg_encbase[i] = dev->reg_base; dev->master_dev = master_dev; + ret =mtk_jpegenc_smmu_init (dev); +if (ret) +return ret; +
[Severity: High]
Similar to the decoder probe, will returning early here on SMMU init failure cause a use-after-free by leaving the freed dev pointer in master_dev->enc_hw_dev[i]?
Also, are there any checks to ensure the index 'i' doesn't exceed the bounds of the master_dev->enc_hw_dev array?
The device tree child nodes are counted and assigned to jpeg->max_hw_count without validation. Later, in mtk_jpegenc_worker(), it appears this count is used to populate a fixed-size stack array:
drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c:mtk_jpegenc_worker() {
...
struct mtk_jpegenc_comp_dev *comp_jpeg[MTK_JPEGENC_HW_MAX]; ... for (i = 0; i < jpeg->max_hw_count; i++) comp_jpeg[i] = jpeg->enc_hw_dev[i];
...
}
Could an excessively large number of DT child nodes cause a heap buffer overflow during probe, and subsequently a stack buffer overflow here?
Quoted 2 lines
drivers/media/platform/mediatek/jpeg/mtk_jpeg_enc_hw.c +0
-0platform_set_drvdata (pdev, dev);pm_runtime_enable (&pdev->dev);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=13
[email protected]
AI reviewRe: [PATCH v18 05/13] media: mediatek: jpeg: Fix multi-core clk suspend and resume setting
In reply to Kyrie WuThank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider: - [Critical] media: mediatek: jpeg: system suspend disables clock while HW is running - [High] media…
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [Critical] media: mediatek: jpeg: system suspend disables clock while HW is running
- [High] media: mediatek: jpeg: Unsafe PM suspend forced while hardware is active
- [High] media: mediatek: jpeg: M2M queue unpaused before components resume
- [Low] media: mediatek: jpeg: dev_pm_ops assigned unconditionally causing dead code --
commit 4893ce2818141ce09ed0c83da36108870b5d7724
Author: Kyrie Wu <[email protected]>
Subject: media: mediatek: jpeg: Fix multi-core clk suspend and resume setting
This patch updates clock management and power management suspend/resume callbacks for multi-hardware component devices, leaving single-core callbacks on the master device. It aims to synchronize job suspension using v4l2_m2m_suspend() before disabling component clocks. Runtime PM references are used to keep clocks enabled until a job completes or times out.
Quoted 3 lines
drivers/media/platform/mediatek/jpeg/mtk_jpeg_dec_hw.c +0
-0
[ ... ]
drivers/media/platform/mediatek/jpeg/mtk_jpeg_dec_hw.c static int mtk_jpegdec_hw_probe(struct platform_device *pdev) · line 695
Quoted 6 lines
drivers/media/platform/mediatek/jpeg/mtk_jpeg_dec_hw.c +6
-0+static int mtk_jpegdec_suspend (struct device *dev) +{ +struct mtk_jpegdec_comp_dev *jpeg =dev_get_drvdata (dev); + +/* Stop new jobs and wait for the current job before clock-off. */ +v4l2_m2m_suspend (jpeg->master_dev->m2m_dev);
[Severity: Critical]
Does this wait mechanism function correctly if v4l2_m2m_job_finish() is called immediately after starting the hardware?
In mtk_jpegdec_worker(), v4l2_m2m_job_finish() is invoked right after mtk_jpeg_dec_start(), which clears the active M2M context early to allow scheduling on other cores. Consequently, v4l2_m2m_suspend() will see no active job and return immediately without waiting.
This can lead to pm_runtime_force_suspend() abruptly shutting off hardware clocks while the hardware is still actively processing a frame, which can lock up the bus.
Additionally, if the hardware is left running without clocks, the pending job_timeout_work scheduled on the system workqueue will eventually fire. When it calls mtk_jpeg_dec_reset(), it will attempt to access unclocked device registers, which can trigger a synchronous external abort and crash the system.
Quoted 4 lines
drivers/media/platform/mediatek/jpeg/mtk_jpeg_dec_hw.c +4
-0+ +return pm_runtime_force_suspend (dev); +} +
drivers/media/platform/mediatek/jpeg/mtk_jpeg_dec_hw.c static int mtk_jpegdec_hw_probe(struct platform_device *pdev) · line 709
Quoted 10 lines
drivers/media/platform/mediatek/jpeg/mtk_jpeg_dec_hw.c +10
-0+static int mtk_jpegdec_resume (struct device *dev) +{ +struct mtk_jpegdec_comp_dev *jpeg =dev_get_drvdata (dev); +int ret; + + ret =pm_runtime_force_resume (dev); +if (ret <0 ) +return ret; + +v4l2_m2m_resume (jpeg->master_dev->m2m_dev);
[Severity: High]
Can this unpause the global M2M queue before all hardware components are fully resumed?
During system resume, the PM core sequentially resumes component devices. If the first resumed component calls v4l2_m2m_resume(), it unpauses the global M2M queue and schedules the worker thread on a non-freezable workqueue.
The worker thread could execute immediately, select a sibling component that is still suspended, and call pm_runtime_resume_and_get(). If this occurs concurrently with the PM core executing pm_runtime_force_resume() on that same component, the runtime PM call fails with -EACCES, resulting in dropped V4L2 frames.
Quoted 5 lines
drivers/media/platform/mediatek/jpeg/mtk_jpeg_dec_hw.c +5
-0+ +return 0 ; +} + +static const struct dev_pm_ops mtk_jpegdec_pm_ops = {
drivers/media/platform/mediatek/jpeg/mtk_jpeg_dec_hw.c static int mtk_jpegdec_hw_probe(struct platform_device *pdev) · line 724
Quoted 10 lines
drivers/media/platform/mediatek/jpeg/mtk_jpeg_dec_hw.c +5
-0+ SYSTEM_SLEEP_PM_OPS(mtk_jpegdec_suspend, mtk_jpegdec_resume) +RUNTIME_PM_OPS (mtk_jpegdec_pm_suspend, mtk_jpegdec_pm_resume,NULL ) +}; +static struct platform_driver mtk_jpegdec_hw_driver = { .probe = mtk_jpegdec_hw_probe, .driver = { .name ="mtk-jpegdec-hw" , .of_match_table = mtk_jpegdec_hw_ids, + .pm = &mtk_jpegdec_pm_ops,
[Severity: Low]
Should this be wrapped with pm_ptr() to avoid compiler warnings and dead code retention when CONFIG_PM is disabled?
Quoted 6 lines
drivers/media/platform/mediatek/jpeg/mtk_jpeg_dec_hw.c +0
-0}, };drivers/media/platform/mediatek/jpeg/mtk_jpeg_enc_hw.c +0
-0
[ ... ]
drivers/media/platform/mediatek/jpeg/mtk_jpeg_enc_hw.c static int mtk_jpegenc_hw_probe(struct platform_device *pdev) · line 447
Quoted 11 lines
drivers/media/platform/mediatek/jpeg/mtk_jpeg_enc_hw.c +6
-0+static const struct dev_pm_ops mtk_jpegenc_pm_ops = { + SYSTEM_SLEEP_PM_OPS(mtk_jpegenc_suspend, mtk_jpegenc_resume) +RUNTIME_PM_OPS (mtk_jpegenc_pm_suspend, mtk_jpegenc_pm_resume,NULL ) +}; +static struct platform_driver mtk_jpegenc_hw_driver = { .probe = mtk_jpegenc_hw_probe, .driver = { .name ="mtk-jpegenc-hw" , .of_match_table = mtk_jpegenc_drv_ids, + .pm = &mtk_jpegenc_pm_ops,
[Severity: Low]
Should this also be wrapped with pm_ptr()?
Quoted 2 lines
},
};
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
[email protected]
AI reviewRe: [PATCH v18 07/13] media: mediatek: jpeg: fix resolution change event handling in decoder
In reply to Kyrie WuThank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] media: mtk-jpeg: infinite loop in worker on MTK_JPEG_SOURCE_CHANGE - [High] media: mtk-jpeg…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] media: mtk-jpeg: infinite loop in worker on MTK_JPEG_SOURCE_CHANGE
- [High] media: mtk-jpeg: unprotected queue data mutation in async worker --
commit 1072cbb260c137f5bbe5c089ef41101b9ac96729
Author: Kyrie Wu <[email protected]>
media: mediatek: jpeg: fix resolution change event handling in decoder
This commit refines the handling of resolution change events in the JPEG decoder worker. It updates the queue data before queuing the source change event and setting the state to MTK_JPEG_SOURCE_CHANGE. It also adds an early exit when the context is already in the source change state.
Quoted 4 lines
drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c +0
-0
[ ... ]
drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c static void mtk_jpegdec_worker(struct work_struct *work) · line 1741
Quoted 6 lines
drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c +1
-1@@ -1738,11 +1738,15 @@ static void mtk_jpegdec_worker(struct work_struct *work)if (mtk_jpeg_check_resolution_change (ctx, &jpeg_src_buf->dec_param)) { -mtk_jpeg_queue_src_chg_event (ctx); +mtk_jpeg_set_queue_data (ctx, &jpeg_src_buf->dec_param);
[Severity: High]
Does calling mtk_jpeg_set_queue_data() here mutate ctx->out_q and ctx->cap_q without holding jpeg->lock? Since this runs in an asynchronous worker thread, can concurrent V4L2 ioctls (like VIDIOC_G_FMT) issued from user space race with these updates?
Updating the resolution before user space acknowledges the change with STREAMOFF might also violate the driver's documented architectural contract.
drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c static void mtk_jpegdec_worker(struct work_struct *work) · line 1748
Quoted 7 lines
drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c +3
-0ctx->state =MTK_JPEG_SOURCE_CHANGE ; +mtk_jpeg_queue_src_chg_event (ctx); goto getbuf_fail; } +if (ctx->state ==MTK_JPEG_SOURCE_CHANGE ) + goto getbuf_fail;
[Severity: High]
Could this introduce an infinite loop? If we jump to getbuf_fail here, the job finishes without consuming the pending buffers.
Because mtk_jpeg_multicore_dec_m2m_ops lacks a .job_ready callback:
static const struct v4l2_m2m_ops mtk_jpeg_multicore_dec_m2m_ops = {
.device_run = mtk_jpeg_multicore_dec_device_run,
};
the V4L2 M2M core assumes the device is always ready. Will the M2M core immediately requeue the context, causing the worker to run again, hit this same check, and loop infinitely consuming 100% CPU?
Quoted 4 lines
drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c +1
-0+mtk_jpegdec_set_hw_param (ctx, hw_id, src_buf, dst_buf); ret =pm_runtime_resume_and_get (comp_jpeg[hw_id]->dev);if (ret <0 ) {
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
Re: [PATCH v18 12/13] dt-bindings: mediatek: Add MT8196 JPEG SMMU bindings
In reply to Kyrie WuQuoted 25 lines · Kyrie Wu
Document the decoder and encoder phandles and describe the dedicated JPEG SMMU configuration syscon for MT8196.
Signed-off-by Kyrie Wu <[email protected]>--- .../media/mediatek,mt8195-jpegdec.yaml | 16 ++++++++ .../media/mediatek,mt8195-jpegenc.yaml | 16 ++++++++ .../mediatek/mediatek,mt8196-jpeg-smmu.yaml | 41 +++++++++++++++++++ MAINTAINERS | 2 + 4 files changed, 75 insertions(+) create mode 100644 Documentation/devicetree/bindings/soc/mediatek/mediatek,mt8196-jpeg-smmu.yamlDocumentation/devicetree/bindings/media/mediatek,mt8195-jpegdec.yaml +5
-0@@ -73,6 +73,22 @@ patternProperties: power-domains: maxItems: 1 + mediatek,smmu-config: + $ref: /schemas/types.yaml#/definitions/phandle + description: + Phandle to the MT8196 JPEG SMMU configuration syscon used to + program this decoder's GUSER_ID fields.
Documentation/devicetree/bindings/media/mediatek,mt8195-jpegdec.yaml patternProperties: · line 90
Quoted 10 lines
Documentation/devicetree/bindings/media/mediatek,mt8195-jpegdec.yaml +10
-0+ + allOf: + - if: + properties: + compatible: + contains: + const: mediatek,mt8196-jpgdec-hw + then: + required: + - mediatek,smmu-config
Why are you adding incomplete hardware in one patch and then immediately fixing it later?
Please read writing bindings doc (and part about complete hardware support).
Best regards,
Krzysztof
Re: [PATCH v18 09/13] media: dt-bindings: mediatek,jpeg: Add mediatek, mt8196-jpgdec compatible
In reply to Kyrie WuQuoted 8 lines · Kyrie Wu
Compared to the previous generation IC, the MT8196 uses SMMU instead of IOMMU and supports features such as dynamic voltage and frequency scaling. Therefore, add "mediatek,mt8196-jpgdec" compatible to the binding document.
Signed-off-by Kyrie Wu <[email protected]>Reviewed-by Krzysztof Kozlowski <[email protected]>Reviewed-by AngeloGioacchino Del Regno <[email protected]>
So this has subject "media: dt-bindings:" but a new patch you added here has completely different subject prefix. Including the re-introduced nit, I am sure I already asked you.
A nit, subject: drop second/last, redundant "bindings". The "dt-bindings" prefix is already stating that these are bindings. See also:
https://elixir.bootlin.com/linux/v7.1-rc7/source/Documentation/devicetree/bindings/submitting-patches.rst#L23
This is random way of coding, not even checking your git history before sending to lists.
Best regards,
Krzysztof
Re: [PATCH v18 09/13] media: dt-bindings: mediatek,jpeg: Add mediatek, mt8196-jpgdec compatible
Quoted 22 lines · Krzysztof Kozlowski
Quoted 9 lines · Kyrie Wu
Compared to the previous generation IC, the MT8196 uses SMMU instead of IOMMU and supports features such as dynamic voltage and frequency scaling. Therefore, add "mediatek,mt8196-jpgdec" compatible to the binding document.
Signed-off-by Kyrie Wu <[email protected]>Reviewed-by Krzysztof Kozlowski <[email protected]>Reviewed-by AngeloGioacchino Del RegnoSo this has subject "media: dt-bindings:" but a new patch you added here
has completely different subject prefix. Including the re-introduced nit, I am sure I already asked you.A nit, subject: drop second/last, redundant "bindings". The "dt-bindings" prefix is already stating that these are bindings. See also:
https://urldefense.com/v3/__https://elixir.bootlin.com/linux/v7.1-rc7/source/Documentation/devicetree/bindings/submitting-patches.rst*L23__;Iw!!CTRNKA9wMg0ARbw!h_fknzbP8753NFS_O2e4gssF2OksmsGX8wmW7BASM61ZHZc4edEsgjEe1PxHWHrio8KJjCzeHmSg8w$
Quoted 7 lines
This is random way of coding, not even checking your git history before
sending to lists.Best regards,
Krzysztof
Hi Krzysztof,
You are right. I missed the inconsistent subject prefix and repeated the
redundant "bindings" wording when preparing v18.
In v19, I will change the SMMU patch subject to:
dt-bindings: soc: mediatek: Add MT8196 JPEG SMMU
I will also recheck the decoder and encoder binding subjects against submitting-patches.rst for consistency.
Sorry for the oversight, and thank you for pointing it out.
Best regards,
Kyrie
Re: [PATCH v18 12/13] dt-bindings: mediatek: Add MT8196 JPEG SMMU bindings
In reply to Krzysztof KozlowskiQuoted 52 lines · Krzysztof Kozlowski
Quoted 38 lines · Kyrie Wu
Document the decoder and encoder phandles and describe the dedicated
JPEG SMMU configuration syscon for MT8196.Signed-off-by Kyrie Wu <[email protected]>---
.../media/mediatek,mt8195-jpegdec.yaml | 16 ++++++++ .../media/mediatek,mt8195-jpegenc.yaml | 16 ++++++++ .../mediatek/mediatek,mt8196-jpeg-smmu.yaml | 41
+++++++++++++++++++
MAINTAINERS | 2 +4 files changed, 75 insertions(+)create mode 100644
Documentation/devicetree/bindings/soc/mediatek/mediatek,mt8196-jpeg-smmu.yamldiff --git a/Documentation/devicetree/bindings/media/mediatek,mt8195-
jpegdec.yamlb/Documentation/devicetree/bindings/media/mediatek,mt8195-jpegdec.yaml
index 28a9a9b..b1dccc3 100644
--- a/Documentation/devicetree/bindings/media/mediatek,mt8195-jpegdec.yaml
+++ b/Documentation/devicetree/bindings/media/mediatek,mt8195-jpegdec.yaml(quoted diff) +0
-0@@ -73,6 +73,22 @@ patternProperties:power-domains:
maxItems: 1
+ mediatek,smmu-config:
+ $ref: /schemas/types.yaml#/definitions/phandle + description:
+ Phandle to the MT8196 JPEG SMMU configuration syscon used to
+ program this decoder'sGUSER_IDfields.
+
+ allOf:Quoted 8 lines
+ - if:
+ properties:
+ compatible:
+ contains:
+ const: mediatek,mt8196-jpgdec-hw
+ then:
+ required:
+ - mediatek,smmu-configWhy are you adding incomplete hardware in one patch and then immediately
fixing it later?
Quoted 6 lines
Please read writing bindings doc (and part about complete hardware support).
Best regards,
Krzysztof
Hi Krzysztof,
You are right. The MT8196 decoder and encoder compatibles should not be introduced before their schemas contain all properties and constraints required to describe the hardware.
In v19, I will introduce the shared JPEG SMMU syscon schema first. I will then fold the mediatek,smmu-config property and its required constraint into the decoder-compatible patch, and do the same for the encoder patch.
This will make each compatible-introduction commit complete on its own, instead of fixing an incomplete binding in a later patch.
Thank you for the review.
Best regards,
Kyrie