[V10,5/7] mtk-jpegenc: add jpeg encode worker interface

AngeloGioacchino Del Regno angelogioacchino.delregno at collabora.com
Tue Jul 5 06:27:28 PDT 2022


Il 27/06/22 04:56, Irui Wang ha scritto:
> From: kyrie wu <kyrie.wu at mediatek.com>
> 
> Add jpeg encoding worker to ensure that two HWs
> run in parallel in MT8195.
> 
> Signed-off-by: kyrie wu <kyrie.wu at mediatek.com>
> ---
>   .../platform/mediatek/jpeg/mtk_jpeg_core.c    | 208 ++++++++++++++++--
>   .../platform/mediatek/jpeg/mtk_jpeg_core.h    |  30 ++-
>   .../platform/mediatek/jpeg/mtk_jpeg_enc_hw.c  |  39 +++-
>   3 files changed, 240 insertions(+), 37 deletions(-)
> 
> diff --git a/drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c b/drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c
> index 0c5c85a112ca..544673a527a0 100644
> --- a/drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c
> +++ b/drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.c

..snip..

> @@ -946,9 +1050,58 @@ static void mtk_jpeg_enc_device_run(void *priv)
>   	v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
>   	v4l2_m2m_buf_done(src_buf, buf_state);
>   	v4l2_m2m_buf_done(dst_buf, buf_state);
> +getbuf_fail:
> +	atomic_inc(&jpeg->enchw_rdy);
> +	mtk_jpegenc_put_hw(jpeg, hw_id);
>   	v4l2_m2m_job_finish(jpeg->m2m_dev, ctx->fh.m2m_ctx);
>   }
>   
> +static void mtk_jpeg_enc_device_run(void *priv)
> +{

That's not really the best thing to do here... if we're managing multi-core
hardware, this function would be a one-liner and we would never see variable
jpeg->is_jpgenc_multihw changing during runtime.

At this point, we can reduce checks and improve human readability by simply
having two separated functions for single-core vs multi-core...

static void mtk_jpeg_singlecore_enc_dev_run(void *priv) { .... blah .... }

and

static void mtk_jpeg_multicore_enc_dev_run(void *priv)
{
	struct mtk_jpeg_ctx *ctx = priv;

	queue_work(ctx->jpeg->workqueue, &ctx->jpeg_work);
}

Also because we're defining `.m2m_ops` into mt8195_jpegenc_drvdata, so it's
going to be as trivial as defining a new m2m_ops for it:

static const struct v4l2_m2m_ops mtk_jpeg_multicore_enc_m2m_ops = {

	.device_run = mtk_jpeg_multicore_enc_dev_run,

};

Please fix that :-)

> +	struct mtk_jpeg_ctx *ctx = priv;
> +	struct mtk_jpeg_dev *jpeg = ctx->jpeg;
> +	struct vb2_v4l2_buffer *src_buf, *dst_buf;
> +	enum vb2_buffer_state buf_state = VB2_BUF_STATE_ERROR;
> +	unsigned long flags;
> +	int ret;
> +
> +	if (!jpeg->is_jpgenc_multihw) {
> +		src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
> +		dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
> +
> +		ret = pm_runtime_resume_and_get(jpeg->dev);
> +		if (ret < 0)
> +			goto enc_end;
> +
> +		schedule_delayed_work(&jpeg->job_timeout_work,
> +				      msecs_to_jiffies(MTK_JPEG_HW_TIMEOUT_MSEC));
> +
> +		spin_lock_irqsave(&jpeg->hw_lock, flags);
> +
> +		/*
> +		 * Resetting the hardware every frame is to ensure that all the
> +		 * registers are cleared. This is a hardware requirement.
> +		 */
> +		mtk_jpeg_enc_reset(jpeg->reg_base);
> +
> +		mtk_jpeg_set_enc_src(ctx, jpeg->reg_base, &src_buf->vb2_buf);
> +		mtk_jpeg_set_enc_dst(ctx, jpeg->reg_base, &dst_buf->vb2_buf);
> +		mtk_jpeg_set_enc_params(ctx, jpeg->reg_base);
> +		mtk_jpeg_enc_start(jpeg->reg_base);
> +		spin_unlock_irqrestore(&jpeg->hw_lock, flags);
> +		return;
> +
> +enc_end:
> +		v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
> +		v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
> +		v4l2_m2m_buf_done(src_buf, buf_state);
> +		v4l2_m2m_buf_done(dst_buf, buf_state);
> +		v4l2_m2m_job_finish(jpeg->m2m_dev, ctx->fh.m2m_ctx);
> +	} else {
> +		queue_work(jpeg->workqueue, &ctx->jpeg_work);
> +	}
> +}
> +
>   static void mtk_jpeg_dec_device_run(void *priv)
>   {
>   	struct mtk_jpeg_ctx *ctx = priv;
> @@ -1211,6 +1364,9 @@ static int mtk_jpeg_open(struct file *file)
>   		goto free;
>   	}
>   
> +	if (jpeg->is_jpgenc_multihw)
> +		INIT_WORK(&ctx->jpeg_work, mtk_jpegenc_worker);
> +
>   	v4l2_fh_init(&ctx->fh, vfd);
>   	file->private_data = &ctx->fh;
>   	v4l2_fh_add(&ctx->fh);
> @@ -1355,6 +1511,18 @@ static int mtk_jpeg_probe(struct platform_device *pdev)
>   			dev_err(&pdev->dev, "Failed to init clk\n");
>   			goto err_clk_init;
>   		}
> +	} else {
> +		if (of_property_read_bool(pdev->dev.of_node,
> +					  "mediatek,jpegenc-multi-core")) {

This check is redundant (and will be wrong in the jpegdec series): you're entering
this branch only `if (jpeg->is_jpgenc_multihw)`, and this is initialized a little
earlier when reading the very same devicetree property.

So, for this series, remove the redundant check; for the jpegdec series, don't
recheck with of_property_read_bool, but check for `if (jpeg->is_jpgenc_multihw)`
and then theck for `if (jpeg->is_jpgdec_multihw)`.

> +			init_waitqueue_head(&jpeg->enc_hw_wq);
> +			jpeg->workqueue = alloc_ordered_workqueue(MTK_JPEG_NAME,
> +								  WQ_MEM_RECLAIM
> +								  | WQ_FREEZABLE);
> +			if (!jpeg->workqueue) {
> +				ret = -EINVAL;
> +				goto err_alloc_workqueue;
> +			}
> +		}
>   	}
>   
>   	ret = v4l2_device_register(&pdev->dev, &jpeg->v4l2_dev);
> @@ -1423,6 +1591,8 @@ static int mtk_jpeg_probe(struct platform_device *pdev)
>   
>   err_clk_init:
>   
> +err_alloc_workqueue:
> +

You aren't doing anything at all here, so this label is not required. Remove it.

>   err_req_irq:
>   
>   	return ret;
> diff --git a/drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.h b/drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.h
> index 55719bd1b0fb..2fc20c6e06f4 100644
> --- a/drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.h
> +++ b/drivers/media/platform/mediatek/jpeg/mtk_jpeg_core.h
> @@ -75,6 +75,11 @@ struct mtk_jpeg_variant {
>   	u32 cap_q_default_fourcc;
>   };
>   
> +enum mtk_jpeg_hw_state {
> +	MTK_JPEG_HW_IDLE = 0,
> +	MTK_JPEG_HW_BUSY = 1,
> +};
> +
>   struct mtk_jpeg_hw_param {
>   	struct vb2_v4l2_buffer *src_buffer;
>   	struct vb2_v4l2_buffer *dst_buffer;
> @@ -97,12 +102,17 @@ struct mtk_jpegenc_clk {
>   
>   /**
>    * struct mtk_jpegenc_comp_dev - JPEG COREX abstraction
> - * @dev:		        JPEG device
> - * @plat_dev:		    platform device data
> - * @reg_base:		    JPEG registers mapping
> - * @master_dev:		    mtk_jpeg_dev device
> - * @pm:	                mtk_jpegenc_pm
> - * @jpegenc_irq:	    jpeg encode irq num
> + * @dev:		JPEG device
> + * @plat_dev:		platform device data
> + * @reg_base:		JPEG registers mapping
> + * @master_dev:		mtk_jpeg_dev device
> + * @venc_clk:	        mtk_jpegenc_clk
> + * @jpegenc_irq:	jpeg encode irq num
> + * @job_timeout_work:	handle jpeg encode work
> + * @hw_param:		record hw param
> + * @hw_rdy:		record hw ready
> + * @hw_state:		record hw state
> + * @hw_lock:		spinlock protecting the hw device resource
>    */
>   struct mtk_jpegenc_comp_dev {
>   	struct device *dev;
> @@ -114,6 +124,9 @@ struct mtk_jpegenc_comp_dev {
>   	int hw_id;
>   	struct delayed_work job_timeout_work;
>   	struct mtk_jpeg_hw_param hw_param;
> +	enum mtk_jpeg_hw_state hw_state;
> +	//spinlock protecting the hw device resource
> +	spinlock_t hw_lock;
>   };
>   
>   /**
> @@ -146,6 +159,8 @@ struct mtk_jpeg_dev {
>   	void __iomem *reg_encbase[MTK_JPEGENC_HW_MAX];
>   	struct mtk_jpegenc_comp_dev *enc_hw_dev[MTK_JPEGENC_HW_MAX];
>   	bool is_jpgenc_multihw;
> +	wait_queue_head_t enc_hw_wq;
> +	atomic_t enchw_rdy;
>   };
>   
>   /**
> @@ -204,6 +219,9 @@ struct mtk_jpeg_ctx {
>   	u8 enc_quality;
>   	u8 restart_interval;
>   	struct v4l2_ctrl_handler ctrl_hdl;
> +
> +	struct work_struct jpeg_work;
> +	u32 total_frame_num;
>   };
>   
>   #endif /* _MTK_JPEG_CORE_H */
> 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 bea62013ce7d..8e1840137c34 100644
> --- a/drivers/media/platform/mediatek/jpeg/mtk_jpeg_enc_hw.c
> +++ b/drivers/media/platform/mediatek/jpeg/mtk_jpeg_enc_hw.c
> @@ -192,6 +192,7 @@ static void mtk_jpegenc_timeout_work(struct work_struct *work)
>   		container_of(dly_work,
>   			     struct mtk_jpegenc_comp_dev,
>   			     job_timeout_work);
> +	struct mtk_jpeg_dev *master_jpeg = cjpeg->master_dev;
>   	enum vb2_buffer_state buf_state = VB2_BUF_STATE_ERROR;
>   	struct vb2_v4l2_buffer *src_buf, *dst_buf;
>   
> @@ -202,6 +203,9 @@ static void mtk_jpegenc_timeout_work(struct work_struct *work)
>   	mtk_jpeg_enc_reset(cjpeg->reg_base);
>   	clk_disable_unprepare(cjpeg->venc_clk.clks->clk);
>   	pm_runtime_put(cjpeg->dev);
> +	cjpeg->hw_state = MTK_JPEG_HW_IDLE;
> +	atomic_inc(&master_jpeg->enchw_rdy);
> +	wake_up(&master_jpeg->enc_hw_wq);
>   	v4l2_m2m_buf_done(src_buf, buf_state);
>   }
>   
> @@ -218,30 +222,37 @@ static irqreturn_t mtk_jpegenc_hw_irq_handler(int irq, void *priv)
>   
>   	cancel_delayed_work(&jpeg->job_timeout_work);
>   
> +	ctx = jpeg->hw_param.curr_ctx;
> +	src_buf = jpeg->hw_param.src_buffer;
> +	dst_buf = jpeg->hw_param.dst_buffer;
> +	v4l2_m2m_buf_copy_metadata(src_buf, dst_buf, true);
> +
>   	irq_status = readl(jpeg->reg_base + JPEG_ENC_INT_STS) &
>   		JPEG_ENC_INT_STATUS_MASK_ALLIRQ;
>   	if (irq_status)
>   		writel(0, jpeg->reg_base + JPEG_ENC_INT_STS);
> -	if (!(irq_status & JPEG_ENC_INT_STATUS_DONE))
> -		return IRQ_NONE;
> -
> -	ctx = v4l2_m2m_get_curr_priv(master_jpeg->m2m_dev);
> -	if (!ctx) {
> -		v4l2_err(&master_jpeg->v4l2_dev, "Context is NULL\n");
> -		return IRQ_HANDLED;
> +	if (!(irq_status & JPEG_ENC_INT_STATUS_DONE)) {
> +		dev_err(jpeg->dev, " Not JPEG_ENC_INT_STATUS_DONE\n");

Please be more descriptive in error messages. Also, should this really be
a dev_err?!?

> +		goto irq_handled;
>   	}
>   
> -	src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
> -	dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
> -	v4l2_m2m_buf_copy_metadata(src_buf, dst_buf, true);
> -
>   	result_size = mtk_jpeg_enc_get_file_size(jpeg->reg_base);
>   	vb2_set_plane_payload(&dst_buf->vb2_buf, 0, result_size);
> +
> +irq_handled:
>   	buf_state = VB2_BUF_STATE_DONE;
>   	v4l2_m2m_buf_done(src_buf, buf_state);
>   	v4l2_m2m_buf_done(dst_buf, buf_state);
> -	v4l2_m2m_job_finish(master_jpeg->m2m_dev, ctx->fh.m2m_ctx);
>   	pm_runtime_put(ctx->jpeg->dev);
> +	clk_disable_unprepare(jpeg->venc_clk.clks->clk);
> +	if (!list_empty(&ctx->fh.m2m_ctx->out_q_ctx.rdy_queue) ||
> +	    !list_empty(&ctx->fh.m2m_ctx->cap_q_ctx.rdy_queue)) {
> +		queue_work(master_jpeg->workqueue, &ctx->jpeg_work);
> +	}
> +
> +	jpeg->hw_state = MTK_JPEG_HW_IDLE;
> +	wake_up(&master_jpeg->enc_hw_wq);
> +	atomic_inc(&master_jpeg->enchw_rdy);
>   
>   	return IRQ_HANDLED;
>   }
> @@ -294,6 +305,10 @@ static int mtk_jpegenc_hw_probe(struct platform_device *pdev)
>   	dev->plat_dev = pdev;
>   	dev->dev = &pdev->dev;
>   
> +	atomic_set(&master_dev->enchw_rdy, MTK_JPEGENC_HW_MAX);
> +	spin_lock_init(&dev->hw_lock);
> +	dev->hw_state = MTK_JPEG_HW_IDLE;
> +
>   	INIT_DELAYED_WORK(&dev->job_timeout_work,
>   			  mtk_jpegenc_timeout_work);
>   





More information about the linux-arm-kernel mailing list