linux-media.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
To: Jacopo Mondi <jacopo@jmondi.org>
Cc: "Niklas Söderlund" <niklas.soderlund+renesas@ragnatech.se>,
	"Hans Verkuil" <hverkuil-cisco@xs4all.nl>,
	"Jacopo Mondi" <jacopo+renesas@jmondi.org>,
	linux-media@vger.kernel.org, linux-renesas-soc@vger.kernel.org
Subject: Re: [PATCH 1/3] media: rcar-vin: Refactor link notify
Date: Wed, 10 Nov 2021 19:48:05 +0200	[thread overview]
Message-ID: <YYwF1dg1/2b68rgH@pendragon.ideasonboard.com> (raw)
In-Reply-To: <20211104163654.fjjcpmj2gnt5aaul@uno.localdomain>

On Thu, Nov 04, 2021 at 05:36:54PM +0100, Jacopo Mondi wrote:
> On Wed, Oct 20, 2021 at 10:02:23PM +0200, Niklas Söderlund wrote:
> > The code have grown organically and a lot of checks are preformed for
> 
> code -> has
> preformed -> performed
> 
> > the CSI-2 use-case even if the link notify is for a subdevice connected
> > to the parallel interface.
> >
> > Before reworking the CSI-2 routing logic split the CSI-2 and parallel
> > link notify code in two separate blocks to make it clearer. There is no
> > functional change.
> >
> > Signed-off-by: Niklas Söderlund <niklas.soderlund+renesas@ragnatech.se>
> > ---
> >  drivers/media/platform/rcar-vin/rcar-core.c | 87 +++++++++++----------
> >  1 file changed, 45 insertions(+), 42 deletions(-)
> >
> > diff --git a/drivers/media/platform/rcar-vin/rcar-core.c b/drivers/media/platform/rcar-vin/rcar-core.c
> > index 1d92cc8ede8f8a3e..bd960c348ba5228c 100644
> > --- a/drivers/media/platform/rcar-vin/rcar-core.c
> > +++ b/drivers/media/platform/rcar-vin/rcar-core.c
> > @@ -795,12 +795,10 @@ static int rvin_csi2_link_notify(struct media_link *link, u32 flags,
> >  {
> >  	struct rvin_group *group = container_of(link->graph_obj.mdev,
> >  						struct rvin_group, mdev);
> > -	unsigned int master_id, channel, mask_new, i;
> > -	unsigned int mask = ~0;
> >  	struct media_entity *entity;
> >  	struct video_device *vdev;
> > -	struct media_pad *csi_pad;
> > -	struct rvin_dev *vin = NULL;
> > +	struct rvin_dev *vin;
> > +	unsigned int i;
> >  	int csi_id, ret;
> >
> >  	ret = v4l2_pipeline_link_notify(link, flags, notification);
> > @@ -826,33 +824,9 @@ static int rvin_csi2_link_notify(struct media_link *link, u32 flags,
> >  	/* Find the master VIN that controls the routes. */
> >  	vdev = media_entity_to_video_device(link->sink->entity);
> >  	vin = container_of(vdev, struct rvin_dev, vdev);
> > -	master_id = rvin_group_id_to_master(vin->id);
> >
> > -	if (WARN_ON(!group->vin[master_id])) {
> > -		ret = -ENODEV;
> > -		goto out;
> > -	}
> >
> > -	/* Build a mask for already enabled links. */
> > -	for (i = master_id; i < master_id + 4; i++) {
> > -		if (!group->vin[i])
> > -			continue;
> > -
> > -		/* Get remote CSI-2, if any. */
> > -		csi_pad = media_entity_remote_pad(
> > -				&group->vin[i]->vdev.entity.pads[0]);
> > -		if (!csi_pad)
> > -			continue;
> > -
> > -		csi_id = rvin_group_entity_to_remote_id(group, csi_pad->entity);
> > -		channel = rvin_group_csi_pad_to_channel(csi_pad->index);
> > -
> > -		mask &= rvin_csi2_get_mask(group->vin[i], csi_id, channel);
> > -	}
> > -
> 
> There are now two empty lines here
> 
> > -	/* Add the new link to the existing mask and check if it works. */
> >  	csi_id = rvin_group_entity_to_remote_id(group, link->source->entity);
> > -
> >  	if (csi_id == -ENODEV) {
> >  		struct v4l2_subdev *sd;
> >
> > @@ -877,25 +851,54 @@ static int rvin_csi2_link_notify(struct media_link *link, u32 flags,
> >  		vin_err(vin, "Subdevice %s not registered to any VIN\n",
> >  			link->source->entity->name);
> >  		ret = -ENODEV;
> > -		goto out;
> > -	}
> > +	} else {
> 
> I wonder if the group mutex should be locked in the else branch only
> and simplify the whole thing as:

Isn't the lock required to call rvin_group_entity_to_remote_id() ? I
think the two lines before:

	vdev = media_entity_to_video_device(link->sink->entity);
	vin = container_of(vdev, struct rvin_dev, vdev);

could however be moved before mutex_lock().

>         if (csi_id == -ENODEV) {
>                 /* subdev is parallel. */
>                 for () {

Unless I'm mistaken, this needs locking in order to access group->vin[]
safely.

>                         return 0;
>                 }
> 
>                 return -ENODEV;
>         }
> 
>         /* subdev is CSI-2 */
>         mutex_lock(group->mutex);
> 
>         ...
> 
>         mutex_unlock(group->mutex);
> 
> Even if I see above:
> 
> 	/*
> 	 * Don't allow link changes if any stream in the graph is active as
> 	 * modifying the CHSEL register fields can disrupt running streams.
> 	 */
> 	media_device_for_each_entity(entity, &group->mdev) {
> 		struct media_pad *iter;
> 
> 		media_entity_for_each_pad(entity, iter) {
> 			if (iter->stream_count)
> 				return -EBUSY;
> 		}
> 	}
> 
> Being run out of the critical session, but that was already the case.
> 
> 
> 
> > +		unsigned int master_id, channel, mask_new;
> > +		unsigned int mask = ~0;
> > +		struct media_pad *csi_pad;
> >
> > -	channel = rvin_group_csi_pad_to_channel(link->source->index);
> > -	mask_new = mask & rvin_csi2_get_mask(vin, csi_id, channel);
> > -	vin_dbg(vin, "Try link change mask: 0x%x new: 0x%x\n", mask, mask_new);
> > +		master_id = rvin_group_id_to_master(vin->id);
> >
> > -	if (!mask_new) {
> > -		ret = -EMLINK;
> > -		goto out;
> > -	}
> > +		if (WARN_ON(!group->vin[master_id])) {
> > +			ret = -ENODEV;
> > +			goto out;
> > +		}
> > +
> > +		/* Build a mask for already enabled links. */
> > +		for (i = master_id; i < master_id + 4; i++) {
> > +			if (!group->vin[i])
> > +				continue;
> > +
> > +			/* Get remote CSI-2, if any. */
> > +			csi_pad = media_entity_remote_pad(
> > +					&group->vin[i]->vdev.entity.pads[0]);
> > +			if (!csi_pad)
> > +				continue;
> >
> > -	/* New valid CHSEL found, set the new value. */
> > -	ret = rvin_set_channel_routing(group->vin[master_id], __ffs(mask_new));
> > -	if (ret)
> > -		goto out;
> > +			csi_id = rvin_group_entity_to_remote_id(group,
> > +								csi_pad->entity);
> > +			channel = rvin_group_csi_pad_to_channel(csi_pad->index);
> >
> > -	vin->is_csi = true;
> > +			mask &= rvin_csi2_get_mask(group->vin[i], csi_id, channel);
> > +		}
> >
> > +		channel = rvin_group_csi_pad_to_channel(link->source->index);
> > +		mask_new = mask & rvin_csi2_get_mask(vin, csi_id, channel);
> > +		vin_dbg(vin, "Try link change mask: 0x%x new: 0x%x\n", mask,
> > +			mask_new);
> > +
> > +		if (!mask_new) {
> > +			ret = -EMLINK;
> > +			goto out;
> > +		}
> > +
> > +		/* New valid CHSEL found, set the new value. */
> > +		ret = rvin_set_channel_routing(group->vin[master_id],
> > +					       __ffs(mask_new));
> > +		if (ret)
> > +			goto out;
> > +
> > +		vin->is_csi = true;

I'm tempted to factor code out to two functions for the parallel and
CSI-2 case, but it could be done later too.

Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>

> > +	}
> >  out:
> >  	mutex_unlock(&group->lock);
> >

-- 
Regards,

Laurent Pinchart

  reply	other threads:[~2021-11-10 17:50 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-10-20 20:02 [PATCH 0/3] media: rcar-{csi2,vin}: Move to full Virtual Channel routing per CSI-2 IP Niklas Söderlund
2021-10-20 20:02 ` [PATCH 1/3] media: rcar-vin: Refactor link notify Niklas Söderlund
2021-11-04 16:36   ` Jacopo Mondi
2021-11-10 17:48     ` Laurent Pinchart [this message]
2021-11-27 15:40       ` Niklas Söderlund
2021-10-20 20:02 ` [PATCH 2/3] media: rcar-vin: Breakout media link creation Niklas Söderlund
2021-11-04 16:43   ` Jacopo Mondi
2021-11-10 17:52     ` Laurent Pinchart
2021-11-27 15:49       ` Niklas Söderlund
2021-10-20 20:02 ` [PATCH 3/3] media: rcar-{csi2,vin}: Move to full Virtual Channel routing per CSI-2 IP Niklas Söderlund
2021-11-04 17:29   ` Jacopo Mondi
2021-11-27 16:15     ` Niklas Söderlund
2021-11-04 18:05 ` [PATCH 0/3] " Jacopo Mondi
2021-11-10 15:25 ` Laurent Pinchart
2021-11-10 16:01   ` Niklas Söderlund
2021-11-10 16:57     ` Laurent Pinchart
2021-11-27 15:30       ` Niklas Söderlund

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=YYwF1dg1/2b68rgH@pendragon.ideasonboard.com \
    --to=laurent.pinchart@ideasonboard.com \
    --cc=hverkuil-cisco@xs4all.nl \
    --cc=jacopo+renesas@jmondi.org \
    --cc=jacopo@jmondi.org \
    --cc=linux-media@vger.kernel.org \
    --cc=linux-renesas-soc@vger.kernel.org \
    --cc=niklas.soderlund+renesas@ragnatech.se \
    /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;
as well as URLs for NNTP newsgroup(s).