All of lore.kernel.org
 help / color / mirror / Atom feed
From: Benoit Parrot <bparrot@ti.com>
To: Dave Gerlach <d-gerlach@ti.com>
Cc: Laurent Pinchart <laurent.pinchart@ideasonboard.com>,
	Hans Verkuil <hverkuil@xs4all.nl>, <stable@vger.kernel.org>
Subject: Re: [PATCH v2 01/19] media: ti-vpe: cal: fix DMA memory corruption
Date: Thu, 19 Mar 2020 16:41:04 -0500	[thread overview]
Message-ID: <ef63d690-9e42-920d-545b-c2c0d35177b7@ti.com> (raw)
In-Reply-To: <20200319075023.22151-2-tomi.valkeinen@ti.com>

Thanks for the patch.

On 3/19/20 2:50 AM, Tomi Valkeinen wrote:
> When the CAL driver stops streaming, it will shut everything down
> without waiting for the current frame to finish. This leaves the CAL DMA
> in a slightly undefined state, and when CAL DMA is enabled when the
> stream is started the next time, the old DMA transfer will continue.
> 
> It is not clear if the old DMA transfer continues with the exact
> settings of the original transfer, or is it a mix of old and new
> settings, but in any case the end result is memory corruption as the
> destination memory address is no longer valid.
> 
> I could not find any way to ensure that any old DMA transfer would be
> discarded, except perhaps full CAL reset. But we cannot do a full reset
> when one port is getting enabled, as that would reset both ports.
> 
> This patch tries to make sure that the DMA transfer is finished properly
> when the stream is being stopped. I say "tries", as, as mentioned above,
> I don't see a way to force the DMA transfer to finish. I believe this
> fixes the corruptions for normal cases, but if for some reason the DMA
> of the final frame would stall a lot, resulting in timeout in the code
> waiting for the DMA to finish, we'll again end up with unfinished DMA
> transfer. However, I don't know what could cause such a timeout.
> 
> Signed-off-by: Tomi Valkeinen <tomi.valkeinen@ti.com>
> Cc: stable@vger.kernel.org
> ---
>  drivers/media/platform/ti-vpe/cal.c | 32 +++++++++++++++++++++++++++++
>  1 file changed, 32 insertions(+)
> 
> diff --git a/drivers/media/platform/ti-vpe/cal.c b/drivers/media/platform/ti-vpe/cal.c
> index 6c8f3702eac0..9dd6de14189b 100644
> --- a/drivers/media/platform/ti-vpe/cal.c
> +++ b/drivers/media/platform/ti-vpe/cal.c
> @@ -412,6 +412,8 @@ struct cal_ctx {
>  	struct cal_buffer	*cur_frm;
>  	/* Pointer pointing to next v4l2_buffer */
>  	struct cal_buffer	*next_frm;
> +
> +	bool dma_act;
>  };
>  
>  static const struct cal_fmt *find_format_by_pix(struct cal_ctx *ctx,
> @@ -942,6 +944,7 @@ static void csi2_lane_config(struct cal_ctx *ctx)
>  
>  static void csi2_ppi_enable(struct cal_ctx *ctx)
>  {
> +	reg_write(ctx->dev, CAL_CSI2_PPI_CTRL(ctx->csi2_port), BIT(3));
>  	reg_write_field(ctx->dev, CAL_CSI2_PPI_CTRL(ctx->csi2_port),
>  			CAL_GEN_ENABLE, CAL_CSI2_PPI_CTRL_IF_EN_MASK);
>  }
> @@ -1204,15 +1207,25 @@ static irqreturn_t cal_irq(int irq_cal, void *data)
>  		if (isportirqset(irqst2, 1)) {
>  			ctx = dev->ctx[0];
>  
> +			spin_lock(&ctx->slock);
> +			ctx->dma_act = false;
> +
>  			if (ctx->cur_frm != ctx->next_frm)
>  				cal_process_buffer_complete(ctx);
> +
> +			spin_unlock(&ctx->slock);
>  		}
>  

This totally wrong.

>  		if (isportirqset(irqst2, 2)) {
>  			ctx = dev->ctx[1];
>  
> +			spin_lock(&ctx->slock);
> +			ctx->dma_act = false;
> +
>  			if (ctx->cur_frm != ctx->next_frm)
>  				cal_process_buffer_complete(ctx);
> +
> +			spin_unlock(&ctx->slock);
>  		}
>  	}
>  
> @@ -1228,6 +1241,7 @@ static irqreturn_t cal_irq(int irq_cal, void *data)
>  			dma_q = &ctx->vidq;
>  
>  			spin_lock(&ctx->slock);
> +			ctx->dma_act = true;
>  			if (!list_empty(&dma_q->active) &&
>  			    ctx->cur_frm == ctx->next_frm)
>  				cal_schedule_next_buffer(ctx);
> @@ -1239,6 +1253,7 @@ static irqreturn_t cal_irq(int irq_cal, void *data)
>  			dma_q = &ctx->vidq;
>  
>  			spin_lock(&ctx->slock);
> +			ctx->dma_act = true;
>  			if (!list_empty(&dma_q->active) &&
>  			    ctx->cur_frm == ctx->next_frm)
>  				cal_schedule_next_buffer(ctx);
> @@ -1711,10 +1726,27 @@ static void cal_stop_streaming(struct vb2_queue *vq)
>  	struct cal_ctx *ctx = vb2_get_drv_priv(vq);
>  	struct cal_dmaqueue *dma_q = &ctx->vidq;
>  	struct cal_buffer *buf, *tmp;
> +	unsigned long timeout;
>  	unsigned long flags;
>  	int ret;
> +	bool dma_act;
>  
>  	csi2_ppi_disable(ctx);
> +
> +	/* wait for stream and dma to finish */
> +	dma_act = true;
> +	timeout = jiffies + msecs_to_jiffies(500);
> +	while (dma_act && time_before(jiffies, timeout)) {
> +		msleep(50);
> +
> +		spin_lock_irqsave(&ctx->slock, flags);
> +		dma_act = ctx->dma_act;
> +		spin_unlock_irqrestore(&ctx->slock, flags);
> +	}
> +
> +	if (dma_act)
> +		ctx_err(ctx, "failed to disable dma cleanly\n");
> +
>  	disable_irqs(ctx);
>  	csi2_phy_deinit(ctx);
>  
> 

Benoit

  reply	other threads:[~2020-03-19 21:41 UTC|newest]

Thread overview: 42+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-03-19  7:50 [PATCH v2 00/19] CAL fixes and improvements Tomi Valkeinen
2020-03-19  7:50 ` [PATCH v2 01/19] media: ti-vpe: cal: fix DMA memory corruption Tomi Valkeinen
2020-03-19 21:41   ` Benoit Parrot [this message]
2020-03-19 21:46     ` Benoit Parrot
2020-03-19 22:25   ` Benoit Parrot
2020-03-19  7:50 ` [PATCH v2 02/19] media: ti-vpe: cal: improve enable_irqs Tomi Valkeinen
2020-03-19 22:26   ` Benoit Parrot
2020-03-19  7:50 ` [PATCH v2 03/19] media: ti-vpe: cal: fix use of wrong macro Tomi Valkeinen
2020-03-19 22:27   ` Benoit Parrot
2020-03-19  7:50 ` [PATCH v2 04/19] media: ti-vpe: cal: use runtime_resume for errata handling Tomi Valkeinen
2020-03-19 22:28   ` Benoit Parrot
2020-03-19  7:50 ` [PATCH v2 05/19] media: ti-vpe: cal: drop cal_runtime_get/put Tomi Valkeinen
2020-03-19 22:29   ` Benoit Parrot
2020-03-19  7:50 ` [PATCH v2 06/19] media: ti-vpe: cal: catch error irqs and print errors Tomi Valkeinen
2020-03-19 22:32   ` Benoit Parrot
2020-03-19  7:50 ` [PATCH v2 07/19] media: ti-vpe: cal: print errors on timeouts Tomi Valkeinen
2020-03-19 22:38   ` Benoit Parrot
2020-03-19  7:50 ` [PATCH v2 08/19] media: ti-vpe: cal: simplify irq handling Tomi Valkeinen
2020-03-19 22:39   ` Benoit Parrot
2020-03-19  7:50 ` [PATCH v2 09/19] media: ti-vpe: cal: remove useless CAL_GEN_* macros Tomi Valkeinen
2020-03-19 22:40   ` Benoit Parrot
2020-03-19  7:50 ` [PATCH v2 10/19] media: ti-vpe: cal: remove useless IRQ defines Tomi Valkeinen
2020-03-19 22:40   ` Benoit Parrot
2020-03-19  7:50 ` [PATCH v2 11/19] media: ti-vpe: cal: use reg_write_field Tomi Valkeinen
2020-03-19 22:41   ` Benoit Parrot
2020-03-19  7:50 ` [PATCH v2 12/19] media: ti-vpe: cal: cleanup CIO power enable/disable Tomi Valkeinen
2020-03-19 22:42   ` Benoit Parrot
2020-03-19  7:50 ` [PATCH v2 13/19] media: ti-vpe: cal: fix dummy read to phy Tomi Valkeinen
2020-03-19 22:43   ` Benoit Parrot
2020-03-19  7:50 ` [PATCH v2 14/19] media: ti-vpe: cal: program number of lines properly Tomi Valkeinen
2020-03-19 22:44   ` Benoit Parrot
2020-03-19  7:50 ` [PATCH v2 15/19] media: ti-vpe: cal: set DMA max seg size Tomi Valkeinen
2020-03-19 22:44   ` Benoit Parrot
2020-03-19  7:50 ` [PATCH v2 16/19] media: ti-vpe: cal: move code to separate functions Tomi Valkeinen
2020-03-19 22:45   ` Benoit Parrot
2020-03-19  7:50 ` [PATCH v2 17/19] media: ti-vpe: cal: improve wait for CIO resetdone Tomi Valkeinen
2020-03-19 22:46   ` Benoit Parrot
2020-03-19  7:50 ` [PATCH v2 18/19] media: ti-vpe: cal: improve wait for stop-state Tomi Valkeinen
2020-03-19 22:46   ` Benoit Parrot
2020-03-19  7:50 ` [PATCH v2 19/19] media: ti-vpe: cal: fix stop state timeout Tomi Valkeinen
2020-03-19 22:53   ` Benoit Parrot
2020-03-20  9:33     ` Tomi Valkeinen

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=ef63d690-9e42-920d-545b-c2c0d35177b7@ti.com \
    --to=bparrot@ti.com \
    --cc=d-gerlach@ti.com \
    --cc=hverkuil@xs4all.nl \
    --cc=laurent.pinchart@ideasonboard.com \
    --cc=stable@vger.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.