Commit b00a9e59 authored by Sergey Senozhatsky's avatar Sergey Senozhatsky Committed by Mauro Carvalho Chehab
Browse files

media: videobuf2: add queue memory coherency parameter



Preparations for future V4L2_MEMORY_FLAG_NON_COHERENT support.

Extend the vb2_core_reqbufs() parameters list to accept requests'
->flags, which will be used for memory coherency configuration.

An attempt to allocate a buffer with coherency requirements
that do not match the queue's consistency model will fail.

Signed-off-by: default avatarSergey Senozhatsky <senozhatsky@chromium.org>
Signed-off-by: default avatarHans Verkuil <hverkuil-cisco@xs4all.nl>
Signed-off-by: default avatarMauro Carvalho Chehab <mchehab+huawei@kernel.org>
parent 965c1e0b
Loading
Loading
Loading
Loading
+31 −6
Original line number Original line Diff line number Diff line
@@ -738,11 +738,30 @@ int vb2_verify_memory_type(struct vb2_queue *q,
}
}
EXPORT_SYMBOL(vb2_verify_memory_type);
EXPORT_SYMBOL(vb2_verify_memory_type);


static void set_queue_coherency(struct vb2_queue *q, bool non_coherent_mem)
{
	q->non_coherent_mem = 0;

	if (!vb2_queue_allows_cache_hints(q))
		return;
	q->non_coherent_mem = non_coherent_mem;
}

static bool verify_coherency_flags(struct vb2_queue *q, bool non_coherent_mem)
{
	if (non_coherent_mem != q->non_coherent_mem) {
		dprintk(q, 1, "memory coherency model mismatch\n");
		return false;
	}
	return true;
}

int vb2_core_reqbufs(struct vb2_queue *q, enum vb2_memory memory,
int vb2_core_reqbufs(struct vb2_queue *q, enum vb2_memory memory,
		     unsigned int *count)
		     unsigned int flags, unsigned int *count)
{
{
	unsigned int num_buffers, allocated_buffers, num_planes = 0;
	unsigned int num_buffers, allocated_buffers, num_planes = 0;
	unsigned plane_sizes[VB2_MAX_PLANES] = { };
	unsigned plane_sizes[VB2_MAX_PLANES] = { };
	bool non_coherent_mem = false;
	unsigned int i;
	unsigned int i;
	int ret;
	int ret;


@@ -757,7 +776,8 @@ int vb2_core_reqbufs(struct vb2_queue *q, enum vb2_memory memory,
	}
	}


	if (*count == 0 || q->num_buffers != 0 ||
	if (*count == 0 || q->num_buffers != 0 ||
	    (q->memory != VB2_MEMORY_UNKNOWN && q->memory != memory)) {
	    (q->memory != VB2_MEMORY_UNKNOWN && q->memory != memory) ||
	    !verify_coherency_flags(q, non_coherent_mem)) {
		/*
		/*
		 * We already have buffers allocated, so first check if they
		 * We already have buffers allocated, so first check if they
		 * are not in use and can be freed.
		 * are not in use and can be freed.
@@ -794,6 +814,7 @@ int vb2_core_reqbufs(struct vb2_queue *q, enum vb2_memory memory,
	num_buffers = min_t(unsigned int, num_buffers, VB2_MAX_FRAME);
	num_buffers = min_t(unsigned int, num_buffers, VB2_MAX_FRAME);
	memset(q->alloc_devs, 0, sizeof(q->alloc_devs));
	memset(q->alloc_devs, 0, sizeof(q->alloc_devs));
	q->memory = memory;
	q->memory = memory;
	set_queue_coherency(q, non_coherent_mem);


	/*
	/*
	 * Ask the driver how many buffers and planes per buffer it requires.
	 * Ask the driver how many buffers and planes per buffer it requires.
@@ -878,12 +899,13 @@ int vb2_core_reqbufs(struct vb2_queue *q, enum vb2_memory memory,
EXPORT_SYMBOL_GPL(vb2_core_reqbufs);
EXPORT_SYMBOL_GPL(vb2_core_reqbufs);


int vb2_core_create_bufs(struct vb2_queue *q, enum vb2_memory memory,
int vb2_core_create_bufs(struct vb2_queue *q, enum vb2_memory memory,
			 unsigned int *count,
			 unsigned int flags, unsigned int *count,
			 unsigned int requested_planes,
			 unsigned int requested_planes,
			 const unsigned int requested_sizes[])
			 const unsigned int requested_sizes[])
{
{
	unsigned int num_planes = 0, num_buffers, allocated_buffers;
	unsigned int num_planes = 0, num_buffers, allocated_buffers;
	unsigned plane_sizes[VB2_MAX_PLANES] = { };
	unsigned plane_sizes[VB2_MAX_PLANES] = { };
	bool non_coherent_mem = false;
	int ret;
	int ret;


	if (q->num_buffers == VB2_MAX_FRAME) {
	if (q->num_buffers == VB2_MAX_FRAME) {
@@ -899,11 +921,14 @@ int vb2_core_create_bufs(struct vb2_queue *q, enum vb2_memory memory,
		memset(q->alloc_devs, 0, sizeof(q->alloc_devs));
		memset(q->alloc_devs, 0, sizeof(q->alloc_devs));
		q->memory = memory;
		q->memory = memory;
		q->waiting_for_buffers = !q->is_output;
		q->waiting_for_buffers = !q->is_output;
		set_queue_coherency(q, non_coherent_mem);
	} else {
	} else {
		if (q->memory != memory) {
		if (q->memory != memory) {
			dprintk(q, 1, "memory model mismatch\n");
			dprintk(q, 1, "memory model mismatch\n");
			return -EINVAL;
			return -EINVAL;
		}
		}
		if (!verify_coherency_flags(q, non_coherent_mem))
			return -EINVAL;
	}
	}


	num_buffers = min(*count, VB2_MAX_FRAME - q->num_buffers);
	num_buffers = min(*count, VB2_MAX_FRAME - q->num_buffers);
@@ -2608,7 +2633,7 @@ static int __vb2_init_fileio(struct vb2_queue *q, int read)
	fileio->memory = VB2_MEMORY_MMAP;
	fileio->memory = VB2_MEMORY_MMAP;
	fileio->type = q->type;
	fileio->type = q->type;
	q->fileio = fileio;
	q->fileio = fileio;
	ret = vb2_core_reqbufs(q, fileio->memory, &fileio->count);
	ret = vb2_core_reqbufs(q, fileio->memory, 0, &fileio->count);
	if (ret)
	if (ret)
		goto err_kfree;
		goto err_kfree;


@@ -2665,7 +2690,7 @@ static int __vb2_init_fileio(struct vb2_queue *q, int read)


err_reqbufs:
err_reqbufs:
	fileio->count = 0;
	fileio->count = 0;
	vb2_core_reqbufs(q, fileio->memory, &fileio->count);
	vb2_core_reqbufs(q, fileio->memory, 0, &fileio->count);


err_kfree:
err_kfree:
	q->fileio = NULL;
	q->fileio = NULL;
@@ -2685,7 +2710,7 @@ static int __vb2_cleanup_fileio(struct vb2_queue *q)
		vb2_core_streamoff(q, q->type);
		vb2_core_streamoff(q, q->type);
		q->fileio = NULL;
		q->fileio = NULL;
		fileio->count = 0;
		fileio->count = 0;
		vb2_core_reqbufs(q, fileio->memory, &fileio->count);
		vb2_core_reqbufs(q, fileio->memory, 0, &fileio->count);
		kfree(fileio);
		kfree(fileio);
		dprintk(q, 3, "file io emulator closed\n");
		dprintk(q, 3, "file io emulator closed\n");
	}
	}
+3 −2
Original line number Original line Diff line number Diff line
@@ -697,7 +697,7 @@ int vb2_reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req)
	int ret = vb2_verify_memory_type(q, req->memory, req->type);
	int ret = vb2_verify_memory_type(q, req->memory, req->type);


	fill_buf_caps(q, &req->capabilities);
	fill_buf_caps(q, &req->capabilities);
	return ret ? ret : vb2_core_reqbufs(q, req->memory, &req->count);
	return ret ? ret : vb2_core_reqbufs(q, req->memory, 0, &req->count);
}
}
EXPORT_SYMBOL_GPL(vb2_reqbufs);
EXPORT_SYMBOL_GPL(vb2_reqbufs);


@@ -772,6 +772,7 @@ int vb2_create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create)
		if (requested_sizes[i] == 0)
		if (requested_sizes[i] == 0)
			return -EINVAL;
			return -EINVAL;
	return ret ? ret : vb2_core_create_bufs(q, create->memory,
	return ret ? ret : vb2_core_create_bufs(q, create->memory,
						0,
						&create->count,
						&create->count,
						requested_planes,
						requested_planes,
						requested_sizes);
						requested_sizes);
@@ -974,7 +975,7 @@ int vb2_ioctl_reqbufs(struct file *file, void *priv,
		return res;
		return res;
	if (vb2_queue_is_busy(vdev, file))
	if (vb2_queue_is_busy(vdev, file))
		return -EBUSY;
		return -EBUSY;
	res = vb2_core_reqbufs(vdev->queue, p->memory, &p->count);
	res = vb2_core_reqbufs(vdev->queue, p->memory, 0, &p->count);
	/* If count == 0, then the owner has released all buffers and he
	/* If count == 0, then the owner has released all buffers and he
	   is no longer owner of the queue. Otherwise we have a new owner. */
	   is no longer owner of the queue. Otherwise we have a new owner. */
	if (res == 0)
	if (res == 0)
+1 −1
Original line number Original line Diff line number Diff line
@@ -342,7 +342,7 @@ int dvb_vb2_reqbufs(struct dvb_vb2_ctx *ctx, struct dmx_requestbuffers *req)


	ctx->buf_siz = req->size;
	ctx->buf_siz = req->size;
	ctx->buf_cnt = req->count;
	ctx->buf_cnt = req->count;
	ret = vb2_core_reqbufs(&ctx->vb_q, VB2_MEMORY_MMAP, &req->count);
	ret = vb2_core_reqbufs(&ctx->vb_q, VB2_MEMORY_MMAP, 0, &req->count);
	if (ret) {
	if (ret) {
		ctx->state = DVB_VB2_STATE_NONE;
		ctx->state = DVB_VB2_STATE_NONE;
		dprintk(1, "[%s] count=%d size=%d errno=%d\n", ctx->name,
		dprintk(1, "[%s] count=%d size=%d errno=%d\n", ctx->name,
+8 −2
Original line number Original line Diff line number Diff line
@@ -504,6 +504,8 @@ struct vb2_buf_ops {
 * @allow_cache_hints: when set user-space can pass cache management hints in
 * @allow_cache_hints: when set user-space can pass cache management hints in
 *		order to skip cache flush/invalidation on ->prepare() or/and
 *		order to skip cache flush/invalidation on ->prepare() or/and
 *		->finish().
 *		->finish().
 * @non_coherent_mem: when set queue will attempt to allocate buffers using
 *		non-coherent memory.
 * @lock:	pointer to a mutex that protects the &struct vb2_queue. The
 * @lock:	pointer to a mutex that protects the &struct vb2_queue. The
 *		driver can set this to a mutex to let the v4l2 core serialize
 *		driver can set this to a mutex to let the v4l2 core serialize
 *		the queuing ioctls. If the driver wants to handle locking
 *		the queuing ioctls. If the driver wants to handle locking
@@ -583,6 +585,7 @@ struct vb2_queue {
	unsigned int			uses_qbuf:1;
	unsigned int			uses_qbuf:1;
	unsigned int			uses_requests:1;
	unsigned int			uses_requests:1;
	unsigned int			allow_cache_hints:1;
	unsigned int			allow_cache_hints:1;
	unsigned int			non_coherent_mem:1;


	struct mutex			*lock;
	struct mutex			*lock;
	void				*owner;
	void				*owner;
@@ -748,6 +751,8 @@ void vb2_core_querybuf(struct vb2_queue *q, unsigned int index, void *pb);
 * vb2_core_reqbufs() - Initiate streaming.
 * vb2_core_reqbufs() - Initiate streaming.
 * @q:		pointer to &struct vb2_queue with videobuf2 queue.
 * @q:		pointer to &struct vb2_queue with videobuf2 queue.
 * @memory:	memory type, as defined by &enum vb2_memory.
 * @memory:	memory type, as defined by &enum vb2_memory.
 * @flags:	auxiliary queue/buffer management flags. Currently, the only
 *		used flag is %V4L2_MEMORY_FLAG_NON_COHERENT.
 * @count:	requested buffer count.
 * @count:	requested buffer count.
 *
 *
 * Videobuf2 core helper to implement VIDIOC_REQBUF() operation. It is called
 * Videobuf2 core helper to implement VIDIOC_REQBUF() operation. It is called
@@ -772,12 +777,13 @@ void vb2_core_querybuf(struct vb2_queue *q, unsigned int index, void *pb);
 * Return: returns zero on success; an error code otherwise.
 * Return: returns zero on success; an error code otherwise.
 */
 */
int vb2_core_reqbufs(struct vb2_queue *q, enum vb2_memory memory,
int vb2_core_reqbufs(struct vb2_queue *q, enum vb2_memory memory,
		    unsigned int *count);
		     unsigned int flags, unsigned int *count);


/**
/**
 * vb2_core_create_bufs() - Allocate buffers and any required auxiliary structs
 * vb2_core_create_bufs() - Allocate buffers and any required auxiliary structs
 * @q: pointer to &struct vb2_queue with videobuf2 queue.
 * @q: pointer to &struct vb2_queue with videobuf2 queue.
 * @memory: memory type, as defined by &enum vb2_memory.
 * @memory: memory type, as defined by &enum vb2_memory.
 * @flags: auxiliary queue/buffer management flags.
 * @count: requested buffer count.
 * @count: requested buffer count.
 * @requested_planes: number of planes requested.
 * @requested_planes: number of planes requested.
 * @requested_sizes: array with the size of the planes.
 * @requested_sizes: array with the size of the planes.
@@ -795,7 +801,7 @@ int vb2_core_reqbufs(struct vb2_queue *q, enum vb2_memory memory,
 * Return: returns zero on success; an error code otherwise.
 * Return: returns zero on success; an error code otherwise.
 */
 */
int vb2_core_create_bufs(struct vb2_queue *q, enum vb2_memory memory,
int vb2_core_create_bufs(struct vb2_queue *q, enum vb2_memory memory,
			 unsigned int *count,
			 unsigned int flags, unsigned int *count,
			 unsigned int requested_planes,
			 unsigned int requested_planes,
			 const unsigned int requested_sizes[]);
			 const unsigned int requested_sizes[]);