Linux Media Controller development
 help / color / mirror / Atom feed
From: Nicolas Dufresne <nicolas.dufresne@collabora.com>
To: "Kyrie Wu (吴晗)" <Kyrie.Wu@mediatek.com>,
	"linux-media@vger.kernel.org" <linux-media@vger.kernel.org>,
	"linux-mediatek@lists.infradead.org"
	<linux-mediatek@lists.infradead.org>,
	"AngeloGioacchino Del Regno"
	<angelogioacchino.delregno@collabora.com>,
	"robh@kernel.org" <robh@kernel.org>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
	"krzk+dt@kernel.org" <krzk+dt@kernel.org>,
	"linux-arm-kernel@lists.infradead.org"
	<linux-arm-kernel@lists.infradead.org>,
	"mchehab@kernel.org" <mchehab@kernel.org>,
	"matthias.bgg@gmail.com" <matthias.bgg@gmail.com>,
	"hverkuil-cisco@xs4all.nl" <hverkuil-cisco@xs4all.nl>,
	"conor+dt@kernel.org" <conor+dt@kernel.org>,
	"devicetree@vger.kernel.org" <devicetree@vger.kernel.org>
Subject: Re: [PATCH v15 01/12] media: mediatek: jpeg: fix jpeg cores' amounts setting
Date: Mon, 13 Jul 2026 10:46:08 -0400	[thread overview]
Message-ID: <e90e392c6c9257f0383c8cbdfee0707ea6147237.camel@collabora.com> (raw)
In-Reply-To: <05928d76a6cb103e1f4cafc38fd4ad4013b87c05.camel@mediatek.com>

[-- Attachment #1: Type: text/plain, Size: 7486 bytes --]

Hi,

Le lundi 13 juillet 2026 à 03:30 +0000, Kyrie Wu (吴晗) a écrit :
> On Sat, 2026-07-11 at 17:43 -0400, Nicolas Dufresne wrote:
> > Hi,
> > 
> > Le jeudi 02 juillet 2026 à 15:26 +0800, Kyrie Wu a écrit :
> > > Different ICs have different amounts of cores,
> > > use a variable to set the cores' amounts.
> > > 
> > > Fixes: 934e8bccac95 ("mtk-jpegenc: support jpegenc multi-hardware")
> > > Fixes: 0fa49df4222f ("media: mtk-jpegdec: support jpegdec multi-
> > > hardware")
> > > Signed-off-by: Kyrie Wu <kyrie.wu@mediatek.com>
> > > Reviewed-by: AngeloGioacchino Del Regno
> > > <angelogioacchino.delregno@collabora.com>
> > > ---
> > >  drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c   | 8 ++++---
> > > -
> > >  drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.h   | 2 ++
> > >  drivers/media/platform/mediatek/jpeg/mtk_jpeg_dec_hw.c | 1 +
> > >  drivers/media/platform/mediatek/jpeg/mtk_jpeg_enc_hw.c | 1 +
> > >  4 files changed, 8 insertions(+), 4 deletions(-)
> > > 
> > > diff --git a/drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c
> > > b/drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c
> > > index d147ec483081..1e014fd698ae 100644
> > > --- a/drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c
> > > +++ b/drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c
> > > @@ -1462,7 +1462,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 +1509,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 +1592,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 +1687,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;
> > >  
> > > diff --git a/drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.h
> > > b/drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.h
> > > index 02ed0ed5b736..6be5cf30dea1 100644
> > > --- a/drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.h
> > > +++ b/drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.h
> > > @@ -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;
> > >  };
> > >  
> > >  /**
> > > diff --git a/drivers/media/platform/mediatek/jpeg/mtk_jpeg_dec_hw.c
> > > b/drivers/media/platform/mediatek/jpeg/mtk_jpeg_dec_hw.c
> > > index 32372781daf5..4534caeb104f 100644
> > > --- a/drivers/media/platform/mediatek/jpeg/mtk_jpeg_dec_hw.c
> > > +++ b/drivers/media/platform/mediatek/jpeg/mtk_jpeg_dec_hw.c
> > > @@ -664,6 +664,7 @@ static int mtk_jpegdec_hw_probe(struct
> > > platform_device *pdev)
> > >  	master_dev->dec_hw_dev[i] = dev;
> > >  	master_dev->reg_decbase[i] = dev->reg_base;
> > >  	dev->master_dev = master_dev;
> > > +	master_dev->max_hw_count++;
> > 
> > Just one question, can the mtk_jpegenc_worker() starts before all
> > cores have
> > been probed ?
> > 
> > Nicolas
> 
> Dear Nicolas,
> 
> Thanks for the question.
> 
> mtk_jpegenc_worker() is not started from the probe path. For the
> multicore encoder, the worker is only initialized when userspace
> opens the video node:
>     mtk_jpeg_open()
>       INIT_WORK(&ctx->jpeg_work, jpeg->variant->jpeg_worker)
> 
> It is then queued only through the V4L2 mem2mem run path:
>     userspace QBUF/STREAMON
>       -> v4l2_m2m_try_schedule()
>       -> .device_run = mtk_jpeg_multicore_enc_device_run()
>       -> queue_work(jpeg->workqueue, &ctx->jpeg_work)
>       -> mtk_jpegenc_worker()
> 
> So the worker can run only after the video device has been registered 
> and after a userspace application opens the device and starts
> streaming buffers.
> 
> The JPEG hw core probe is part of the platform device probe flow during
> system boot. The userspace open/streaming path happens later, after the
> system has booted and the video node is available. Therefore we do not
> have a case where mtk_jpegenc_worker() starts before the JPEG hw probe
> has completed.

When I check the core implementation though, the video device is registered and
ready to open before childs driver, so even if small, there is gap where you
could race between probes and open.

I think I'd like to suggest a different approach, which to me is easier to
understand (and so should make it easier to maintain). In mtk_jpeg_probe(), we
already compute num_child. We could immediately set max_hw_count to its expected
value. That would replace this patch entirely. We should add some bug on if that
number is bigger then MTK_JPEGDEC_HW_MAX (bound checks, see shashiko report).

Then, in follow up patch, to fix the minor race I mention, we could avoid
setting hw_rdy counter to num_child immediately, and increment that at child
probe time (where you increase max_hw_count in this patch). The worker will then
wait properly for child driver to have probed (instead of spinning the
device_run() by calling v4l2_m2m_job_finish() when the max retry is reached.).

hope this make sense, at least to me, having a max value that cannot increase
sounds more logical.

Nicolas

> 
> Regards,
> Kyrie.
> > 
> > >  
> > >  	platform_set_drvdata(pdev, dev);
> > >  	pm_runtime_enable(&pdev->dev);
> > > diff --git a/drivers/media/platform/mediatek/jpeg/mtk_jpeg_enc_hw.c
> > > b/drivers/media/platform/mediatek/jpeg/mtk_jpeg_enc_hw.c
> > > index b6f5b2249f1f..2765dafab4ad 100644
> > > --- a/drivers/media/platform/mediatek/jpeg/mtk_jpeg_enc_hw.c
> > > +++ b/drivers/media/platform/mediatek/jpeg/mtk_jpeg_enc_hw.c
> > > @@ -386,6 +386,7 @@ static int mtk_jpegenc_hw_probe(struct
> > > platform_device *pdev)
> > >  	master_dev->enc_hw_dev[i] = dev;
> > >  	master_dev->reg_encbase[i] = dev->reg_base;
> > >  	dev->master_dev = master_dev;
> > > +	master_dev->max_hw_count++;
> > >  
> > >  	platform_set_drvdata(pdev, dev);
> > >  	pm_runtime_enable(&pdev->dev);

[-- Attachment #2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 228 bytes --]

  reply	other threads:[~2026-07-13 14:46 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-02  7:26 [PATCH v15 00/12] Enable jpeg enc & dec multi-hardwares for MT8196 Kyrie Wu
2026-07-02  7:26 ` [PATCH v15 01/12] media: mediatek: jpeg: fix jpeg cores' amounts setting Kyrie Wu
2026-07-11 21:43   ` Nicolas Dufresne
2026-07-13  3:30     ` Kyrie Wu (吴晗)
2026-07-13 14:46       ` Nicolas Dufresne [this message]
2026-07-02  7:26 ` [PATCH v15 02/12] media: mediatek: jpeg: fix jpeg buffer payload size setting Kyrie Wu
2026-07-13 19:26   ` Nicolas Dufresne
2026-07-02  7:26 ` [PATCH v15 03/12] media: mediatek: jpeg: fix buffer structure size and layout Kyrie Wu
2026-07-02  7:26 ` [PATCH v15 04/12] media: mediatek: jpeg: Fix buffer completion on multi-core streaming stop Kyrie Wu
2026-07-13 19:30   ` Nicolas Dufresne
2026-07-02  7:26 ` [PATCH v15 05/12] media: mediatek: jpeg: Fix multi-core clk suspend and resume setting Kyrie Wu
2026-07-13 19:43   ` Nicolas Dufresne
2026-07-02  7:26 ` [PATCH v15 06/12] media: mediatek: jpeg: fix decoding buffer number setting timing issue Kyrie Wu
2026-07-13 19:58   ` Nicolas Dufresne
2026-07-02  7:26 ` [PATCH v15 07/12] media: mediatek: jpeg: fix resolution change event handling in decoder Kyrie Wu
2026-07-13 20:26   ` Nicolas Dufresne
2026-07-02  7:26 ` [PATCH v15 08/12] media: mediatek: jpeg: fix remove buffer removal timing for multi-core Kyrie Wu
2026-07-02  7:26 ` [PATCH v15 09/12] media: dt-bindings: mediatek,jpeg: Add mediatek, mt8196-jpgdec compatible Kyrie Wu
2026-07-02  7:26 ` [PATCH v15 10/12] media: dt-bindings: mediatek,jpeg: Add mediatek, mt8196-jpgenc compatible Kyrie Wu
2026-07-02  7:26 ` [PATCH v15 11/12] media: mediatek: jpeg: add jpeg compatible Kyrie Wu
2026-07-02  7:26 ` [PATCH v15 12/12] media: mediatek: jpeg: add jpeg smmu sid setting Kyrie Wu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=e90e392c6c9257f0383c8cbdfee0707ea6147237.camel@collabora.com \
    --to=nicolas.dufresne@collabora.com \
    --cc=Kyrie.Wu@mediatek.com \
    --cc=angelogioacchino.delregno@collabora.com \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=hverkuil-cisco@xs4all.nl \
    --cc=krzk+dt@kernel.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-media@vger.kernel.org \
    --cc=linux-mediatek@lists.infradead.org \
    --cc=matthias.bgg@gmail.com \
    --cc=mchehab@kernel.org \
    --cc=robh@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox