diff mbox series

[RFC,1/4] media: vb2: Add helpers to access unselected buffers

Message ID 20190114133839.29967-2-paul.kocialkowski@bootlin.com (mailing list archive)
State RFC
Headers show
Series media: Ensure access to dma-buf-imported reference buffers | expand

Commit Message

Paul Kocialkowski Jan. 14, 2019, 1:38 p.m. UTC
Introduce helpers to request and release access to buffers that are
not currently selected as current output or capture buffers.

This is useful to ensure proper access to buffers imported via dma-buf
that are used as reference and thus require associated map/unmap calls
before access.

Signed-off-by: Paul Kocialkowski <paul.kocialkowski@bootlin.com>
---
 .../media/common/videobuf2/videobuf2-core.c   | 46 +++++++++++++++++++
 include/media/videobuf2-core.h                | 15 ++++++
 2 files changed, 61 insertions(+)
diff mbox series

Patch

diff --git a/drivers/media/common/videobuf2/videobuf2-core.c b/drivers/media/common/videobuf2/videobuf2-core.c
index 70e8c3366f9c..2a0c5de4d683 100644
--- a/drivers/media/common/videobuf2/videobuf2-core.c
+++ b/drivers/media/common/videobuf2/videobuf2-core.c
@@ -986,6 +986,52 @@  void vb2_discard_done(struct vb2_queue *q)
 }
 EXPORT_SYMBOL_GPL(vb2_discard_done);
 
+int vb2_buffer_access_request(struct vb2_buffer *vb)
+{
+	struct vb2_queue *q = vb->vb2_queue;
+	unsigned int plane;
+	int ret;
+
+	/* Only dmabuf-imported buffers need to be mapped before access. */
+	if (q->memory != VB2_MEMORY_DMABUF)
+		return -EINVAL;
+
+	for (plane = 0; plane < vb->num_planes; ++plane) {
+		if (vb->planes[plane].dbuf_mapped)
+			continue;
+
+		ret = call_memop(vb, map_dmabuf, vb->planes[plane].mem_priv);
+		if (ret) {
+			dprintk(1, "failed to map dmabuf for plane %d\n",
+				plane);
+			return ret;
+		}
+		vb->planes[plane].dbuf_mapped = 1;
+	}
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(vb2_buffer_access_request);
+
+void vb2_buffer_access_release(struct vb2_buffer *vb)
+{
+	struct vb2_queue *q = vb->vb2_queue;
+	unsigned int plane;
+
+	/* Only dmabuf-imported buffers need to be unmapped after access. */
+	if (q->memory != VB2_MEMORY_DMABUF)
+		return;
+
+	for (plane = 0; plane < vb->num_planes; ++plane) {
+		if (!vb->planes[plane].dbuf_mapped)
+			continue;
+
+		call_void_memop(vb, unmap_dmabuf, vb->planes[plane].mem_priv);
+		vb->planes[plane].dbuf_mapped = 0;
+	}
+}
+EXPORT_SYMBOL_GPL(vb2_buffer_access_release);
+
 /*
  * __prepare_mmap() - prepare an MMAP buffer
  */
diff --git a/include/media/videobuf2-core.h b/include/media/videobuf2-core.h
index 4a737b2c610b..bf378c1e718b 100644
--- a/include/media/videobuf2-core.h
+++ b/include/media/videobuf2-core.h
@@ -1199,4 +1199,19 @@  bool vb2_request_object_is_buffer(struct media_request_object *obj);
  */
 unsigned int vb2_request_buffer_cnt(struct media_request *req);
 
+/**
+ * vb2_buffer_access_request() - request out-of-band data access to a buffer
+ *
+ * @vb:		buffer to request data access for
+ */
+int vb2_buffer_access_request(struct vb2_buffer *vb);
+
+
+/**
+ * vb2_buffer_access_release() - release out-of-band data access to a buffer
+ *
+ * @vb:		buffer to release data access for
+ */
+void vb2_buffer_access_release(struct vb2_buffer *vb);
+
 #endif /* _MEDIA_VIDEOBUF2_CORE_H */