Skip to contentSkip to conversation

Integration of videobuf2 with DMABUF

PATCH In review Tomasz Stanislawski 5 participants 48 messages Oct 10, 2012 – Nov 8, 2012

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

v10 cover Integration of videobuf2 with DMABUF

Hello everyone,
This patchset adds support for DMABUF [2] importing and exporting to V4L2 stack.

v10:
- rebase on media-next 3.7
- typos and style fixes in Documentation (from Hans Verkuil)
- fix compilation error with __fill_vb2_buffer
- fix uninitialized data_offset for single-planar queue
- support for DMABUF importing in S5P-FIMC mem-to-mem interface
- update layout of v4l2_exportbuffer structure
- update ioctl flags in v4l2_ioctls for VIDIOC_EXPBUF
- support for DMABUF exporting in m2m framework
- allow exporting for file handles that calle REQBUBS/CREATE_BUF
- simplify handling of get_device in vb2_dc_alloc
- reduce pr_err to pr_debug while checking alignment of userptr
- check size of userptr buffer against zero
- align size to PAGE_SIZE at allocation of dma-contig buffer

v9:
- rebase on 3.6
- change type for fs to __s32
- add support for vb2_ioctl_expbuf
- remove patch 'v4l: vb2: add support for DMA_ATTR_NO_KERNEL_MAPPING', it will be posted as a separate patch
- fix typos and style in Documentation (from Hans Verkuil)
- only vb2-core and vb2-dma-contig selects DMA_SHARED_BUFFER in Kconfig
- use data_offset and length while queueing DMABUF
- return the most recently used fd at VIDIOC_DQBUF
- use (buffer-type, index, plane) tuple instead of mem_offset to identify a for exporting (from Hans Verkuil)
- support for DMABUF mmap in vb2-dma-contig (from Laurent Pinchart)
- add testing alignment of vaddr and size while verifying userptr against DMA capabilities (from Laurent Pinchart)
- substitute VM_DONTDUMP with VM_RESERVED
- simplify error handling in vb2_dc_get_dmabuf (from Laurent Pinchart)

v8:
- rebased on 3.6-rc1
- merged importer and exporter patchsets
- fixed missing fields in v4l2_plane32 and v4l2_buffer32 structs
- fixed typos/style in documentation
- significant reduction of warnings from checkpatch.pl
- fixed STREAMOFF issues reported by Dima Zavin [4] by adding __vb2_dqbuf helper to vb2-core
- DC fails if userptr is not correctly aligned
- add support for DMA attributes in DC
- add support for buffers with no kernel mapping
- add reference counting on device from allocator context
- dummy support for mmap
- use dma_get_sgtable, drop vb2_dc_kaddr_to_pages hack and vb2_dc_get_base_sgt helper

v7:
- support for V4L2_MEMORY_DMABUF in v4l2-compact-ioctl32.c
- cosmetic fixes to the documentation
- added importing for vmalloc because vmap support in dmabuf for 3.5 was pull-requested
- support for dmabuf importing for VIVI
- resurrect allocation of dma-contig context
- remove reference of alloc_ctx in dma-contig buffer
- use sg_alloc_table_from_pages
- fix DMA scatterlist calls to use orig_nents instead of nents
- fix memleak in vb2_dc_sgt_foreach_page (use orig_nents instead of nents)

v6:
- fixed missing entry in v4l2_memory_names
- fixed a bug occuring after get_user_pages failure
- fixed a bug caused by using invalid vma for get_user_pages
- prepare/finish no longer call dma_sync for dmabuf buffers

v5:
- removed change of importer/exporter behaviour
- fixes vb2_dc_pages_to_sgt basing on Laurent's hints
- changed pin/unpin words to lock/unlock in Doc

v4:
- rebased on mainline 3.4-rc2
- included missing importing support for s5p-fimc and s5p-tv
- added patch for changing map/unmap for importers
- fixes to Documentation part
- coding style fixes
- pairing {map/unmap}_dmabuf in vb2-core
- fixing variable types and semantic of arguments in videobufb2-dma-contig.c

v3:
- rebased on mainline 3.4-rc1
- split 'code refactor' patch to multiple smaller patches
- squashed fixes to Sumit's patches
- patchset is no longer dependant on 'DMA mapping redesign'
- separated path for handling IO and non-IO mappings
- add documentation for DMABUF importing to V4L
- removed all DMABUF exporter related code
- removed usage of dma_get_pages extension

v2:
- extended VIDIOC_EXPBUF argument from integer memoffset to struct v4l2_exportbuffer
- added patch that breaks DMABUF spec on (un)map_atachment callcacks but allows to work with existing implementation of DMABUF prime in DRM
- all dma-contig code refactoring patches were squashed
- bugfixes

v1: List of changes since [1].
- support for DMA api extension dma_get_pages, the function is used to retrieve pages used to create DMA mapping.
- small fixes/code cleanup to videobuf2
- added prepare and finish callbacks to vb2 allocators, it is used keep consistency between dma-cpu acess to the memory (by Marek Szyprowski)
- support for exporting of DMABUF buffer in V4L2 and Videobuf2, originated from [3].
- support for dma-buf exporting in vb2-dma-contig allocator
- support for DMABUF for s5p-tv and s5p-fimc (capture interface) drivers, originated from [3]
- changed handling for userptr buffers (by Marek Szyprowski, Andrzej Pietrasiewicz)
- let mmap method to use dma_mmap_writecombine call (by Marek Szyprowski)

[1] http://thread.gmane.org/gmane.linux.drivers.video-input-infrastructure/42966/focus=42968
[2] https://lkml.org/lkml/2011/12/26/29
[3] http://thread.gmane.org/gmane.linux.kernel.cross-arch/12819
[4] http://article.gmane.org/gmane.linux.drivers.video-input-infrastructure/49700

Laurent Pinchart (2):
v4l: vb2-dma-contig: shorten vb2_dma_contig prefix to vb2_dc
v4l: vb2-dma-contig: reorder functions

Marek Szyprowski (4):
v4l: vb2: add prepare/finish callbacks to allocators
v4l: vb2-dma-contig: add prepare/finish to dma-contig allocator
v4l: vb2-dma-contig: let mmap method to use dma_mmap_coherent call
v4l: vb2-dma-contig: fail if user ptr buffer is not correctly aligned

Sumit Semwal (4):
v4l: Add DMABUF as a memory type
v4l: vb2: add support for shared buffer (dma_buf)
v4l: vb: remove warnings about MEMORY_DMABUF
v4l: vb2-dma-contig: add support for dma_buf importing

Tomasz Stanislawski (16):
Documentation: media: description of DMABUF importing in V4L2
v4l: vb2-dma-contig: remove reference of alloc_ctx from a buffer
v4l: vb2-dma-contig: add support for scatterlist in userptr mode
v4l: vb2-vmalloc: add support for dmabuf importing
v4l: vivi: support for dmabuf importing
v4l: s5p-tv: mixer: support for dmabuf importing
v4l: s5p-fimc: support for dmabuf importing
Documentation: media: description of DMABUF exporting in V4L2
v4l: add buffer exporting via dmabuf
v4l: vb2: add buffer exporting via dmabuf
v4l: vb2-dma-contig: add support for DMABUF exporting
v4l: vb2-dma-contig: add reference counting for a device from
allocator context
v4l: vb2-dma-contig: align buffer size to PAGE_SIZE
v4l: s5p-fimc: support for dmabuf exporting
v4l: s5p-tv: mixer: support for dmabuf exporting
v4l: s5p-mfc: support for dmabuf exporting

 Documentation/DocBook/media/v4l/compat.xml         |    7 +
 Documentation/DocBook/media/v4l/io.xml             |  184 ++++-
 Documentation/DocBook/media/v4l/v4l2.xml           |    1 +
 .../DocBook/media/v4l/vidioc-create-bufs.xml       |   16 +-
 Documentation/DocBook/media/v4l/vidioc-expbuf.xml  |  212 ++++++
 Documentation/DocBook/media/v4l/vidioc-qbuf.xml    |   17 +
 Documentation/DocBook/media/v4l/vidioc-reqbufs.xml |   47 +-
 drivers/media/platform/s5p-fimc/fimc-capture.c     |   11 +-
 drivers/media/platform/s5p-fimc/fimc-m2m.c         |   14 +-
 drivers/media/platform/s5p-mfc/s5p_mfc_dec.c       |   14 +
 drivers/media/platform/s5p-mfc/s5p_mfc_enc.c       |   14 +
 drivers/media/platform/s5p-tv/mixer_video.c        |   12 +-
 drivers/media/platform/vivi.c                      |    2 +-
 drivers/media/v4l2-core/Kconfig                    |    3 +
 drivers/media/v4l2-core/v4l2-compat-ioctl32.c      |   19 +
 drivers/media/v4l2-core/v4l2-dev.c                 |    1 +
 drivers/media/v4l2-core/v4l2-ioctl.c               |   11 +
 drivers/media/v4l2-core/v4l2-mem2mem.c             |   13 +
 drivers/media/v4l2-core/videobuf-core.c            |    4 +
 drivers/media/v4l2-core/videobuf2-core.c           |  300 ++++++++-
 drivers/media/v4l2-core/videobuf2-dma-contig.c     |  702 ++++++++++++++++++--
 drivers/media/v4l2-core/videobuf2-memops.c         |   40 --
 drivers/media/v4l2-core/videobuf2-vmalloc.c        |   56 ++
 include/linux/videodev2.h                          |   35 +
 include/media/v4l2-ioctl.h                         |    2 +
 include/media/v4l2-mem2mem.h                       |    3 +
 include/media/videobuf2-core.h                     |   38 ++
 include/media/videobuf2-memops.h                   |    5 -
 28 files changed, 1645 insertions(+), 138 deletions(-)
 create mode 100644 Documentation/DocBook/media/v4l/vidioc-expbuf.xml
Signature · 1 line
1.7.9.5
#1 lore raw

PATCH v10 1/26 v4l: Add DMABUF as a memory type

From: Sumit Semwal <[email protected]>

Adds DMABUF memory type to v4l framework. Also adds the related file descriptor in v4l2_plane and v4l2_buffer.

---
 drivers/media/v4l2-core/v4l2-compat-ioctl32.c |   18 ++++++++++++++++++
 drivers/media/v4l2-core/v4l2-ioctl.c          |    1 +
 include/linux/videodev2.h                     |    7 +++++++
 3 files changed, 26 insertions(+)
drivers/media/v4l2-core/v4l2-compat-ioctl32.c +18 -0
index 83ffb64..cc5998b 100644
@@ -297,6 +297,7 @@ struct v4l2_plane32 {
 	union {
 		__u32		mem_offset;
 		compat_long_t	userptr;
+		__s32		fd;
 	} m;
 	__u32			data_offset;
 	__u32			reserved[11];
@@ -318,6 +319,7 @@ struct v4l2_buffer32 {
 		__u32           offset;
 		compat_long_t   userptr;
 		compat_caddr_t  planes;
+		__s32		fd;
 	} m;
 	__u32			length;
 	__u32			reserved2;
@@ -341,6 +343,9 @@ static int get_v4l2_plane32(struct v4l2_plane *up, struct v4l2_plane32 *up32,
 		up_pln = compat_ptr(p);
 		if (put_user((unsigned long)up_pln, &up->m.userptr))
 			return -EFAULT;
+	} else if (memory == V4L2_MEMORY_DMABUF) {
+		if (copy_in_user(&up->m.fd, &up32->m.fd, sizeof(int)))
+			return -EFAULT;
 	} else {
 		if (copy_in_user(&up->m.mem_offset, &up32->m.mem_offset,
 					sizeof(__u32)))
@@ -364,6 +369,11 @@ static int put_v4l2_plane32(struct v4l2_plane *up, struct v4l2_plane32 *up32,
 		if (copy_in_user(&up32->m.mem_offset, &up->m.mem_offset,
 					sizeof(__u32)))
 			return -EFAULT;
+	/* For DMABUF, driver might've set up the fd, so copy it back. */
+	if (memory == V4L2_MEMORY_DMABUF)
+		if (copy_in_user(&up32->m.fd, &up->m.fd,
+					sizeof(int)))
+			return -EFAULT;
 
 	return 0;
 }
@@ -446,6 +456,10 @@ static int get_v4l2_buffer32(struct v4l2_buffer *kp, struct v4l2_buffer32 __user
 			if (get_user(kp->m.offset, &up->m.offset))
 				return -EFAULT;
 			break;
+		case V4L2_MEMORY_DMABUF:
+			if (get_user(kp->m.fd, &up->m.fd))
+				return -EFAULT;
+			break;
 		}
 	}
 
@@ -510,6 +524,10 @@ static int put_v4l2_buffer32(struct v4l2_buffer *kp, struct v4l2_buffer32 __user
 			if (put_user(kp->m.offset, &up->m.offset))
 				return -EFAULT;
 			break;
+		case V4L2_MEMORY_DMABUF:
+			if (put_user(kp->m.fd, &up->m.fd))
+				return -EFAULT;
+			break;
 		}
 	}
 
drivers/media/v4l2-core/v4l2-ioctl.c +1 -0
index 8f388ff..530a67e 100644
@@ -155,6 +155,7 @@ static const char *v4l2_memory_names[] = {
 	[V4L2_MEMORY_MMAP]    = "mmap",
 	[V4L2_MEMORY_USERPTR] = "userptr",
 	[V4L2_MEMORY_OVERLAY] = "overlay",
+	[V4L2_MEMORY_DMABUF] = "dmabuf",
 };
 
 #define prt_names(a, arr) (((unsigned)(a)) < ARRAY_SIZE(arr) ? arr[a] : "unknown")
include/linux/videodev2.h +7 -0
index 873adbe..07bc5d6 100644
@@ -188,6 +188,7 @@ enum v4l2_memory {
 	V4L2_MEMORY_MMAP             = 1,
 	V4L2_MEMORY_USERPTR          = 2,
 	V4L2_MEMORY_OVERLAY          = 3,
+	V4L2_MEMORY_DMABUF           = 4,
 };
 
 /* see also http://vektor.theorem.ca/graphics/ycbcr/ */
@@ -604,6 +605,8 @@ struct v4l2_requestbuffers {
  *			should be passed to mmap() called on the video node)
  * @userptr:		when memory is V4L2_MEMORY_USERPTR, a userspace pointer
  *			pointing to this plane
+ * @fd:			when memory is V4L2_MEMORY_DMABUF, a userspace file
+ *			descriptor associated with this plane
  * @data_offset:	offset in the plane to the start of data; usually 0,
  *			unless there is a header in front of the data
  *
@@ -618,6 +621,7 @@ struct v4l2_plane {
 	union {
 		__u32		mem_offset;
 		unsigned long	userptr;
+		__s32		fd;
 	} m;
 	__u32			data_offset;
 	__u32			reserved[11];
@@ -642,6 +646,8 @@ struct v4l2_plane {
  *		(or a "cookie" that should be passed to mmap() as offset)
  * @userptr:	for non-multiplanar buffers with memory == V4L2_MEMORY_USERPTR;
  *		a userspace pointer pointing to this buffer
+ * @fd:		for non-multiplanar buffers with memory == V4L2_MEMORY_DMABUF;
+ *		a userspace file descriptor associated with this buffer
  * @planes:	for multiplanar buffers; userspace pointer to the array of plane
  *		info structs for this buffer
  * @length:	size in bytes of the buffer (NOT its payload) for single-plane
@@ -668,6 +674,7 @@ struct v4l2_buffer {
 		__u32           offset;
 		unsigned long   userptr;
 		struct v4l2_plane *planes;
+		__s32		fd;
 	} m;
 	__u32			length;
 	__u32			reserved2;
Signature · 1 line
1.7.9.5
#2 lore raw

PATCH v10 2/26 Documentation: media: description of DMABUF importing in V4L2

In reply to Tomasz Stanislawski

This patch adds description and usage examples for importing DMABUF file descriptor in V4L2.

---
 Documentation/DocBook/media/v4l/compat.xml         |    4 +
 Documentation/DocBook/media/v4l/io.xml             |  181 +++++++++++++++++++-
 .../DocBook/media/v4l/vidioc-create-bufs.xml       |   16 +-
 Documentation/DocBook/media/v4l/vidioc-qbuf.xml    |   17 ++
 Documentation/DocBook/media/v4l/vidioc-reqbufs.xml |   47 ++---
 5 files changed, 235 insertions(+), 30 deletions(-)
Documentation/DocBook/media/v4l/compat.xml +4 -0
index 4fdf6b5..50eb630 100644
@@ -2586,6 +2586,10 @@ ioctls.</para>
 	  <para>Vendor and device specific media bus pixel formats.
 	    <xref linkend="v4l2-mbus-vendor-spec-fmts" />.</para>
         </listitem>
+        <listitem>
+	  <para>Importing DMABUF file descriptors as a new IO method described
+	  in <xref linkend="dmabuf" />.</para>
+        </listitem>
       </itemizedlist>
     </section>
 
Documentation/DocBook/media/v4l/io.xml +180 -1

211 lines not on this page · raw message

Documentation/DocBook/media/v4l/vidioc-create-bufs.xml +9 -7
index a8cda1a..cd99436 100644
@@ -6,7 +6,8 @@
 
   <refnamediv>
     <refname>VIDIOC_CREATE_BUFS</refname>
-    <refpurpose>Create buffers for Memory Mapped or User Pointer I/O</refpurpose>
+    <refpurpose>Create buffers for Memory Mapped or User Pointer or DMA Buffer
+    I/O</refpurpose>
   </refnamediv>
 
   <refsynopsisdiv>
@@ -55,11 +56,11 @@
     </note>
 
     <para>This ioctl is used to create buffers for <link linkend="mmap">memory
-mapped</link> or <link linkend="userp">user pointer</link>
-I/O. It can be used as an alternative or in addition to the
-<constant>VIDIOC_REQBUFS</constant> ioctl, when a tighter control over buffers
-is required. This ioctl can be called multiple times to create buffers of
-different sizes.</para>
+mapped</link> or <link linkend="userp">user pointer</link> or <link
+linkend="dmabuf">DMA buffer</link> I/O. It can be used as an alternative or in
+addition to the <constant>VIDIOC_REQBUFS</constant> ioctl, when a tighter
+control over buffers is required. This ioctl can be called multiple times to
+create buffers of different sizes.</para>
 
     <para>To allocate device buffers applications initialize relevant fields of
 the <structname>v4l2_create_buffers</structname> structure. They set the
@@ -109,7 +110,8 @@ information.</para>
 	    <entry>__u32</entry>
 	    <entry><structfield>memory</structfield></entry>
 	    <entry>Applications set this field to
-<constant>V4L2_MEMORY_MMAP</constant> or
+<constant>V4L2_MEMORY_MMAP</constant>,
+<constant>V4L2_MEMORY_DMABUF</constant> or
 <constant>V4L2_MEMORY_USERPTR</constant>. See <xref linkend="v4l2-memory"
 /></entry>
 	  </row>
Documentation/DocBook/media/v4l/vidioc-qbuf.xml +17 -0
index 2d37abe..3504a7f 100644
@@ -109,6 +109,23 @@ they cannot be swapped out to disk. Buffers remain locked until
 dequeued, until the &VIDIOC-STREAMOFF; or &VIDIOC-REQBUFS; ioctl is
 called, or until the device is closed.</para>
 
+    <para>To enqueue a <link linkend="dmabuf">DMABUF</link> buffer applications
+set the <structfield>memory</structfield> field to
+<constant>V4L2_MEMORY_DMABUF</constant> and the <structfield>m.fd</structfield>
+field to a file descriptor associated with a DMABUF buffer. When the
+multi-planar API is used the <structfield>m.fd</structfield> fields of the
+passed array of &v4l2-plane; have to be used instead. When
+<constant>VIDIOC_QBUF</constant> is called with a pointer to this structure the
+driver sets the <constant>V4L2_BUF_FLAG_QUEUED</constant> flag and clears the
+<constant>V4L2_BUF_FLAG_MAPPED</constant> and
+<constant>V4L2_BUF_FLAG_DONE</constant> flags in the
+<structfield>flags</structfield> field, or it returns an error code.  This
+ioctl locks the buffer. Locking a buffer means passing it to a driver for a
+hardware access (usually DMA).  If an application accesses (reads/writes) a
+locked buffer then the result is undefined.  Buffers remain locked until
+dequeued, until the &VIDIOC-STREAMOFF; or &VIDIOC-REQBUFS; ioctl is called, or
+until the device is closed.</para>
+
     <para>Applications call the <constant>VIDIOC_DQBUF</constant>
 ioctl to dequeue a filled (capturing) or displayed (output) buffer
 from the driver's outgoing queue. They just set the
Documentation/DocBook/media/v4l/vidioc-reqbufs.xml +25 -22

60 lines not on this page · raw message

Signature · 1 line
1.7.9.5
Acked-by on v10 2/26
#3 lore raw

Re: [PATCHv10 02/26] Documentation: media: description of DMABUF importing in V4L2

Quoted 6 lines · On Wed October 10 2012 16:46:21 Tomasz Stanislawski wrote:

This patch adds description and usage examples for importing DMABUF file descriptor in V4L2.

Quoted 8 lines
---
 Documentation/DocBook/media/v4l/compat.xml         |    4 +
 Documentation/DocBook/media/v4l/io.xml             |  181 +++++++++++++++++++-
 .../DocBook/media/v4l/vidioc-create-bufs.xml       |   16 +-
 Documentation/DocBook/media/v4l/vidioc-qbuf.xml    |   17 ++
 Documentation/DocBook/media/v4l/vidioc-reqbufs.xml |   47 ++---
 5 files changed, 235 insertions(+), 30 deletions(-)
#4 lore raw

PATCH v10 3/26 v4l: vb2: add support for shared buffer (dma_buf)

In reply to Tomasz Stanislawski

From: Sumit Semwal <[email protected]>

This patch adds support for DMABUF memory type in videobuf2. It calls relevant APIs of dma_buf for v4l reqbuf / qbuf / dqbuf operations.

For this version, the support is for videobuf2 as a user of the shared buffer; so the allocation of the buffer is done outside of V4L2. [A sample allocator of dma-buf shared buffer is given at [1]]

[1]: Rob Clark's DRM:
https://github.com/robclark/kernel-omap4/commits/drmplane-dmabuf

---
 drivers/media/v4l2-core/Kconfig          |    1 +
 drivers/media/v4l2-core/videobuf2-core.c |  206 +++++++++++++++++++++++++++++-
 include/media/videobuf2-core.h           |   27 ++++
 3 files changed, 231 insertions(+), 3 deletions(-)
drivers/media/v4l2-core/Kconfig +1 -0
index 0c54e19..2e787cc 100644
@@ -59,6 +59,7 @@ config VIDEOBUF_DVB
 
 # Used by drivers that need Videobuf2 modules
 config VIDEOBUF2_CORE
+	select DMA_SHARED_BUFFER
 	tristate
 
 config VIDEOBUF2_MEMOPS
drivers/media/v4l2-core/videobuf2-core.c +203 -3

288 lines not on this page · raw message

include/media/videobuf2-core.h +27 -0
index e04252a..689ae4a 100644
@@ -16,6 +16,7 @@
 #include <linux/mutex.h>
 #include <linux/poll.h>
 #include <linux/videodev2.h>
+#include <linux/dma-buf.h>
 
 struct vb2_alloc_ctx;
 struct vb2_fileio_data;
@@ -41,6 +42,20 @@ struct vb2_fileio_data;
  *		 argument to other ops in this structure
  * @put_userptr: inform the allocator that a USERPTR buffer will no longer
  *		 be used
+ * @attach_dmabuf: attach a shared struct dma_buf for a hardware operation;
+ *		   used for DMABUF memory types; alloc_ctx is the alloc context
+ *		   dbuf is the shared dma_buf; returns NULL on failure;
+ *		   allocator private per-buffer structure on success;
+ *		   this needs to be used for further accesses to the buffer
+ * @detach_dmabuf: inform the exporter of the buffer that the current DMABUF
+ *		   buffer is no longer used; the buf_priv argument is the
+ *		   allocator private per-buffer structure previously returned
+ *		   from the attach_dmabuf callback
+ * @map_dmabuf: request for access to the dmabuf from allocator; the allocator
+ *		of dmabuf is informed that this driver is going to use the
+ *		dmabuf
+ * @unmap_dmabuf: releases access control to the dmabuf - allocator is notified
+ *		  that this driver is done using the dmabuf for now
  * @vaddr:	return a kernel virtual address to a given memory buffer
  *		associated with the passed private structure or NULL if no
  *		such mapping exists
@@ -56,6 +71,8 @@ struct vb2_fileio_data;
  * Required ops for USERPTR types: get_userptr, put_userptr.
  * Required ops for MMAP types: alloc, put, num_users, mmap.
  * Required ops for read/write access types: alloc, put, num_users, vaddr
+ * Required ops for DMABUF types: attach_dmabuf, detach_dmabuf, map_dmabuf,
+ *				  unmap_dmabuf.
  */
 struct vb2_mem_ops {
 	void		*(*alloc)(void *alloc_ctx, unsigned long size);
@@ -65,6 +82,12 @@ struct vb2_mem_ops {
 					unsigned long size, int write);
 	void		(*put_userptr)(void *buf_priv);
 
+	void		*(*attach_dmabuf)(void *alloc_ctx, struct dma_buf *dbuf,
+				unsigned long size, int write);
+	void		(*detach_dmabuf)(void *buf_priv);
+	int		(*map_dmabuf)(void *buf_priv);
+	void		(*unmap_dmabuf)(void *buf_priv);
+
 	void		*(*vaddr)(void *buf_priv);
 	void		*(*cookie)(void *buf_priv);
 
@@ -75,6 +98,8 @@ struct vb2_mem_ops {
 
 struct vb2_plane {
 	void			*mem_priv;
+	struct dma_buf		*dbuf;
+	unsigned int		dbuf_mapped;
 };
 
 /**
@@ -83,12 +108,14 @@ struct vb2_plane {
  * @VB2_USERPTR:	driver supports USERPTR with streaming API
  * @VB2_READ:		driver supports read() style access
  * @VB2_WRITE:		driver supports write() style access
+ * @VB2_DMABUF:		driver supports DMABUF with streaming API
  */
 enum vb2_io_modes {
 	VB2_MMAP	= (1 << 0),
 	VB2_USERPTR	= (1 << 1),
 	VB2_READ	= (1 << 2),
 	VB2_WRITE	= (1 << 3),
+	VB2_DMABUF	= (1 << 4),
 };
 
 /**
Signature · 1 line
1.7.9.5
#5 lore raw

PATCH v10 4/26 v4l: vb: remove warnings about MEMORY_DMABUF

In reply to Tomasz Stanislawski

From: Sumit Semwal <[email protected]>

Adding DMABUF memory type causes videobuf to complain about not using it in some switch cases. This patch removes these warnings.

---
 drivers/media/v4l2-core/videobuf-core.c |    4 ++++
 1 file changed, 4 insertions(+)
drivers/media/v4l2-core/videobuf-core.c +4 -0
index bf7a326..5449e8a 100644
@@ -335,6 +335,9 @@ static void videobuf_status(struct videobuf_queue *q, struct v4l2_buffer *b,
 	case V4L2_MEMORY_OVERLAY:
 		b->m.offset  = vb->boff;
 		break;
+	case V4L2_MEMORY_DMABUF:
+		/* DMABUF is not handled in videobuf framework */
+		break;
 	}
 
 	b->flags    = 0;
@@ -405,6 +408,7 @@ int __videobuf_mmap_setup(struct videobuf_queue *q,
 			break;
 		case V4L2_MEMORY_USERPTR:
 		case V4L2_MEMORY_OVERLAY:
+		case V4L2_MEMORY_DMABUF:
 			/* nothing */
 			break;
 		}
Signature · 1 line
1.7.9.5
#6 lore raw

PATCH v10 5/26 v4l: vb2-dma-contig: shorten vb2_dma_contig prefix to vb2_dc

In reply to Tomasz Stanislawski

From: Laurent Pinchart <[email protected]>

---
 drivers/media/v4l2-core/videobuf2-dma-contig.c |   36 ++++++++++++------------
 1 file changed, 18 insertions(+), 18 deletions(-)
drivers/media/v4l2-core/videobuf2-dma-contig.c +18 -18
index 4b71326..a05784f 100644
@@ -32,9 +32,9 @@ struct vb2_dc_buf {
 	struct vb2_vmarea_handler	handler;
 };
 
-static void vb2_dma_contig_put(void *buf_priv);
+static void vb2_dc_put(void *buf_priv);
 
-static void *vb2_dma_contig_alloc(void *alloc_ctx, unsigned long size)
+static void *vb2_dc_alloc(void *alloc_ctx, unsigned long size)
 {
 	struct vb2_dc_conf *conf = alloc_ctx;
 	struct vb2_dc_buf *buf;
@@ -56,7 +56,7 @@ static void *vb2_dma_contig_alloc(void *alloc_ctx, unsigned long size)
 	buf->size = size;
 
 	buf->handler.refcount = &buf->refcount;
-	buf->handler.put = vb2_dma_contig_put;
+	buf->handler.put = vb2_dc_put;
 	buf->handler.arg = buf;
 
 	atomic_inc(&buf->refcount);
@@ -64,7 +64,7 @@ static void *vb2_dma_contig_alloc(void *alloc_ctx, unsigned long size)
 	return buf;
 }
 
-static void vb2_dma_contig_put(void *buf_priv)
+static void vb2_dc_put(void *buf_priv)
 {
 	struct vb2_dc_buf *buf = buf_priv;
 
@@ -75,14 +75,14 @@ static void vb2_dma_contig_put(void *buf_priv)
 	}
 }
 
-static void *vb2_dma_contig_cookie(void *buf_priv)
+static void *vb2_dc_cookie(void *buf_priv)
 {
 	struct vb2_dc_buf *buf = buf_priv;
 
 	return &buf->dma_addr;
 }
 
-static void *vb2_dma_contig_vaddr(void *buf_priv)
+static void *vb2_dc_vaddr(void *buf_priv)
 {
 	struct vb2_dc_buf *buf = buf_priv;
 	if (!buf)
@@ -91,14 +91,14 @@ static void *vb2_dma_contig_vaddr(void *buf_priv)
 	return buf->vaddr;
 }
 
-static unsigned int vb2_dma_contig_num_users(void *buf_priv)
+static unsigned int vb2_dc_num_users(void *buf_priv)
 {
 	struct vb2_dc_buf *buf = buf_priv;
 
 	return atomic_read(&buf->refcount);
 }
 
-static int vb2_dma_contig_mmap(void *buf_priv, struct vm_area_struct *vma)
+static int vb2_dc_mmap(void *buf_priv, struct vm_area_struct *vma)
 {
 	struct vb2_dc_buf *buf = buf_priv;
 
@@ -111,7 +111,7 @@ static int vb2_dma_contig_mmap(void *buf_priv, struct vm_area_struct *vma)
 				  &vb2_common_vm_ops, &buf->handler);
 }
 
-static void *vb2_dma_contig_get_userptr(void *alloc_ctx, unsigned long vaddr,
+static void *vb2_dc_get_userptr(void *alloc_ctx, unsigned long vaddr,
 					unsigned long size, int write)
 {
 	struct vb2_dc_buf *buf;
@@ -138,7 +138,7 @@ static void *vb2_dma_contig_get_userptr(void *alloc_ctx, unsigned long vaddr,
 	return buf;
 }
 
-static void vb2_dma_contig_put_userptr(void *mem_priv)
+static void vb2_dc_put_userptr(void *mem_priv)
 {
 	struct vb2_dc_buf *buf = mem_priv;
 
@@ -150,14 +150,14 @@ static void vb2_dma_contig_put_userptr(void *mem_priv)
 }
 
 const struct vb2_mem_ops vb2_dma_contig_memops = {
-	.alloc		= vb2_dma_contig_alloc,
-	.put		= vb2_dma_contig_put,
-	.cookie		= vb2_dma_contig_cookie,
-	.vaddr		= vb2_dma_contig_vaddr,
-	.mmap		= vb2_dma_contig_mmap,
-	.get_userptr	= vb2_dma_contig_get_userptr,
-	.put_userptr	= vb2_dma_contig_put_userptr,
-	.num_users	= vb2_dma_contig_num_users,
+	.alloc		= vb2_dc_alloc,
+	.put		= vb2_dc_put,
+	.cookie		= vb2_dc_cookie,
+	.vaddr		= vb2_dc_vaddr,
+	.mmap		= vb2_dc_mmap,
+	.get_userptr	= vb2_dc_get_userptr,
+	.put_userptr	= vb2_dc_put_userptr,
+	.num_users	= vb2_dc_num_users,
 };
 EXPORT_SYMBOL_GPL(vb2_dma_contig_memops);
 
Signature · 1 line
1.7.9.5
#7 lore raw

PATCH v10 6/26 v4l: vb2-dma-contig: remove reference of alloc_ctx from a buffer

In reply to Tomasz Stanislawski

This patch removes a reference to alloc_ctx from an instance of a DMA contiguous buffer. It helps to avoid a risk of a dangling pointer if the context is released while the buffer is still valid. Moreover it removes one dereference step while accessing a device structure.

---
 drivers/media/v4l2-core/videobuf2-dma-contig.c |   13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)
drivers/media/v4l2-core/videobuf2-dma-contig.c +6 -7
index a05784f..20c95da 100644
@@ -23,7 +23,7 @@ struct vb2_dc_conf {
 };
 
 struct vb2_dc_buf {
-	struct vb2_dc_conf		*conf;
+	struct device			*dev;
 	void				*vaddr;
 	dma_addr_t			dma_addr;
 	unsigned long			size;
@@ -37,22 +37,21 @@ static void vb2_dc_put(void *buf_priv);
 static void *vb2_dc_alloc(void *alloc_ctx, unsigned long size)
 {
 	struct vb2_dc_conf *conf = alloc_ctx;
+	struct device *dev = conf->dev;
 	struct vb2_dc_buf *buf;
 
 	buf = kzalloc(sizeof *buf, GFP_KERNEL);
 	if (!buf)
 		return ERR_PTR(-ENOMEM);
 
-	buf->vaddr = dma_alloc_coherent(conf->dev, size, &buf->dma_addr,
-					GFP_KERNEL);
+	buf->vaddr = dma_alloc_coherent(dev, size, &buf->dma_addr, GFP_KERNEL);
 	if (!buf->vaddr) {
-		dev_err(conf->dev, "dma_alloc_coherent of size %ld failed\n",
-			size);
+		dev_err(dev, "dma_alloc_coherent of size %ld failed\n", size);
 		kfree(buf);
 		return ERR_PTR(-ENOMEM);
 	}
 
-	buf->conf = conf;
+	buf->dev = dev;
 	buf->size = size;
 
 	buf->handler.refcount = &buf->refcount;
@@ -69,7 +68,7 @@ static void vb2_dc_put(void *buf_priv)
 	struct vb2_dc_buf *buf = buf_priv;
 
 	if (atomic_dec_and_test(&buf->refcount)) {
-		dma_free_coherent(buf->conf->dev, buf->size, buf->vaddr,
+		dma_free_coherent(buf->dev, buf->size, buf->vaddr,
 				  buf->dma_addr);
 		kfree(buf);
 	}
Signature · 1 line
1.7.9.5
#8 lore raw

PATCH v10 7/26 v4l: vb2-dma-contig: reorder functions

In reply to Tomasz Stanislawski

From: Laurent Pinchart <[email protected]>

Group functions by buffer type.

---
 drivers/media/v4l2-core/videobuf2-dma-contig.c |   92 ++++++++++++++----------
 1 file changed, 54 insertions(+), 38 deletions(-)
drivers/media/v4l2-core/videobuf2-dma-contig.c +54 -38
index 20c95da..daac2b2 100644
@@ -25,14 +25,56 @@ struct vb2_dc_conf {
 struct vb2_dc_buf {
 	struct device			*dev;
 	void				*vaddr;
-	dma_addr_t			dma_addr;
 	unsigned long			size;
-	struct vm_area_struct		*vma;
-	atomic_t			refcount;
+	dma_addr_t			dma_addr;
+
+	/* MMAP related */
 	struct vb2_vmarea_handler	handler;
+	atomic_t			refcount;
+
+	/* USERPTR related */
+	struct vm_area_struct		*vma;
 };
 
-static void vb2_dc_put(void *buf_priv);
+/*********************************************/
+/*         callbacks for all buffers         */
+/*********************************************/
+
+static void *vb2_dc_cookie(void *buf_priv)
+{
+	struct vb2_dc_buf *buf = buf_priv;
+
+	return &buf->dma_addr;
+}
+
+static void *vb2_dc_vaddr(void *buf_priv)
+{
+	struct vb2_dc_buf *buf = buf_priv;
+
+	return buf->vaddr;
+}
+
+static unsigned int vb2_dc_num_users(void *buf_priv)
+{
+	struct vb2_dc_buf *buf = buf_priv;
+
+	return atomic_read(&buf->refcount);
+}
+
+/*********************************************/
+/*        callbacks for MMAP buffers         */
+/*********************************************/
+
+static void vb2_dc_put(void *buf_priv)
+{
+	struct vb2_dc_buf *buf = buf_priv;
+
+	if (!atomic_dec_and_test(&buf->refcount))
+		return;
+
+	dma_free_coherent(buf->dev, buf->size, buf->vaddr, buf->dma_addr);
+	kfree(buf);
+}
 
 static void *vb2_dc_alloc(void *alloc_ctx, unsigned long size)
 {
@@ -63,40 +105,6 @@ static void *vb2_dc_alloc(void *alloc_ctx, unsigned long size)
 	return buf;
 }
 
-static void vb2_dc_put(void *buf_priv)
-{
-	struct vb2_dc_buf *buf = buf_priv;
-
-	if (atomic_dec_and_test(&buf->refcount)) {
-		dma_free_coherent(buf->dev, buf->size, buf->vaddr,
-				  buf->dma_addr);
-		kfree(buf);
-	}
-}
-
-static void *vb2_dc_cookie(void *buf_priv)
-{
-	struct vb2_dc_buf *buf = buf_priv;
-
-	return &buf->dma_addr;
-}
-
-static void *vb2_dc_vaddr(void *buf_priv)
-{
-	struct vb2_dc_buf *buf = buf_priv;
-	if (!buf)
-		return NULL;
-
-	return buf->vaddr;
-}
-
-static unsigned int vb2_dc_num_users(void *buf_priv)
-{
-	struct vb2_dc_buf *buf = buf_priv;
-
-	return atomic_read(&buf->refcount);
-}
-
 static int vb2_dc_mmap(void *buf_priv, struct vm_area_struct *vma)
 {
 	struct vb2_dc_buf *buf = buf_priv;
@@ -110,6 +118,10 @@ static int vb2_dc_mmap(void *buf_priv, struct vm_area_struct *vma)
 				  &vb2_common_vm_ops, &buf->handler);
 }
 
+/*********************************************/
+/*       callbacks for USERPTR buffers       */
+/*********************************************/
+
 static void *vb2_dc_get_userptr(void *alloc_ctx, unsigned long vaddr,
 					unsigned long size, int write)
 {
@@ -148,6 +160,10 @@ static void vb2_dc_put_userptr(void *mem_priv)
 	kfree(buf);
 }
 
+/*********************************************/
+/*       DMA CONTIG exported functions       */
+/*********************************************/
+
 const struct vb2_mem_ops vb2_dma_contig_memops = {
 	.alloc		= vb2_dc_alloc,
 	.put		= vb2_dc_put,
Signature · 1 line
1.7.9.5
#9 lore raw

PATCH v10 8/26 v4l: vb2-dma-contig: add support for scatterlist in userptr mode

In reply to Tomasz Stanislawski

This patch introduces usage of dma_map_sg to map memory behind a userspace pointer to a device as dma-contiguous mapping.

This patch contains some of the code kindly provided by Marek Szyprowski <[email protected]> and Kamil Debski <[email protected]> and Andrzej Pietrasiewicz <[email protected]>. Kind thanks for bug reports from Laurent Pinchart <[email protected]> and Seung-Woo Kim <[email protected]>.

---
 drivers/media/v4l2-core/videobuf2-dma-contig.c |  226 ++++++++++++++++++++++--
 1 file changed, 210 insertions(+), 16 deletions(-)
drivers/media/v4l2-core/videobuf2-dma-contig.c +210 -16
index daac2b2..8486e06 100644
@@ -11,6 +11,8 @@
  */
 
 #include <linux/module.h>
+#include <linux/scatterlist.h>
+#include <linux/sched.h>
 #include <linux/slab.h>
 #include <linux/dma-mapping.h>
 
@@ -27,6 +29,8 @@ struct vb2_dc_buf {
 	void				*vaddr;
 	unsigned long			size;
 	dma_addr_t			dma_addr;
+	enum dma_data_direction		dma_dir;
+	struct sg_table			*dma_sgt;
 
 	/* MMAP related */
 	struct vb2_vmarea_handler	handler;
@@ -37,6 +41,44 @@ struct vb2_dc_buf {
 };
 
 /*********************************************/
+/*        scatterlist table functions        */
+/*********************************************/
+
+
+static void vb2_dc_sgt_foreach_page(struct sg_table *sgt,
+	void (*cb)(struct page *pg))
+{
+	struct scatterlist *s;
+	unsigned int i;
+
+	for_each_sg(sgt->sgl, s, sgt->orig_nents, i) {
+		struct page *page = sg_page(s);
+		unsigned int n_pages = PAGE_ALIGN(s->offset + s->length)
+			>> PAGE_SHIFT;
+		unsigned int j;
+
+		for (j = 0; j < n_pages; ++j, ++page)
+			cb(page);
+	}
+}
+
+static unsigned long vb2_dc_get_contiguous_size(struct sg_table *sgt)
+{
+	struct scatterlist *s;
+	dma_addr_t expected = sg_dma_address(sgt->sgl);
+	unsigned int i;
+	unsigned long size = 0;
+
+	for_each_sg(sgt->sgl, s, sgt->nents, i) {
+		if (sg_dma_address(s) != expected)
+			break;
+		expected = sg_dma_address(s) + sg_dma_len(s);
+		size += sg_dma_len(s);
+	}
+	return size;
+}
+
+/*********************************************/
 /*         callbacks for all buffers         */
 /*********************************************/
 
@@ -122,42 +164,194 @@ static int vb2_dc_mmap(void *buf_priv, struct vm_area_struct *vma)
 /*       callbacks for USERPTR buffers       */
 /*********************************************/
 
+static inline int vma_is_io(struct vm_area_struct *vma)
+{
+	return !!(vma->vm_flags & (VM_IO | VM_PFNMAP));
+}
+
+static int vb2_dc_get_user_pages(unsigned long start, struct page **pages,
+	int n_pages, struct vm_area_struct *vma, int write)
+{
+	if (vma_is_io(vma)) {
+		unsigned int i;
+
+		for (i = 0; i < n_pages; ++i, start += PAGE_SIZE) {
+			unsigned long pfn;
+			int ret = follow_pfn(vma, start, &pfn);
+
+			if (ret) {
+				pr_err("no page for address %lu\n", start);
+				return ret;
+			}
+			pages[i] = pfn_to_page(pfn);
+		}
+	} else {
+		int n;
+
+		n = get_user_pages(current, current->mm, start & PAGE_MASK,
+			n_pages, write, 1, pages, NULL);
+		/* negative error means that no page was pinned */
+		n = max(n, 0);
+		if (n != n_pages) {
+			pr_err("got only %d of %d user pages\n", n, n_pages);
+			while (n)
+				put_page(pages[--n]);
+			return -EFAULT;
+		}
+	}
+
+	return 0;
+}
+
+static void vb2_dc_put_dirty_page(struct page *page)
+{
+	set_page_dirty_lock(page);
+	put_page(page);
+}
+
+static void vb2_dc_put_userptr(void *buf_priv)
+{
+	struct vb2_dc_buf *buf = buf_priv;
+	struct sg_table *sgt = buf->dma_sgt;
+
+	dma_unmap_sg(buf->dev, sgt->sgl, sgt->orig_nents, buf->dma_dir);
+	if (!vma_is_io(buf->vma))
+		vb2_dc_sgt_foreach_page(sgt, vb2_dc_put_dirty_page);
+
+	sg_free_table(sgt);
+	kfree(sgt);
+	vb2_put_vma(buf->vma);
+	kfree(buf);
+}
+
 static void *vb2_dc_get_userptr(void *alloc_ctx, unsigned long vaddr,
-					unsigned long size, int write)
+	unsigned long size, int write)
 {
+	struct vb2_dc_conf *conf = alloc_ctx;
 	struct vb2_dc_buf *buf;
+	unsigned long start;
+	unsigned long end;
+	unsigned long offset;
+	struct page **pages;
+	int n_pages;
+	int ret = 0;
 	struct vm_area_struct *vma;
-	dma_addr_t dma_addr = 0;
-	int ret;
+	struct sg_table *sgt;
+	unsigned long contig_size;
 
 	buf = kzalloc(sizeof *buf, GFP_KERNEL);
 	if (!buf)
 		return ERR_PTR(-ENOMEM);
 
-	ret = vb2_get_contig_userptr(vaddr, size, &vma, &dma_addr);
+	buf->dev = conf->dev;
+	buf->dma_dir = write ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
+
+	start = vaddr & PAGE_MASK;
+	offset = vaddr & ~PAGE_MASK;
+	end = PAGE_ALIGN(vaddr + size);
+	n_pages = (end - start) >> PAGE_SHIFT;
+
+	pages = kmalloc(n_pages * sizeof(pages[0]), GFP_KERNEL);
+	if (!pages) {
+		ret = -ENOMEM;
+		pr_err("failed to allocate pages table\n");
+		goto fail_buf;
+	}
+
+	/* current->mm->mmap_sem is taken by videobuf2 core */
+	vma = find_vma(current->mm, vaddr);
+	if (!vma) {
+		pr_err("no vma for address %lu\n", vaddr);
+		ret = -EFAULT;
+		goto fail_pages;
+	}
+
+	if (vma->vm_end < vaddr + size) {
+		pr_err("vma at %lu is too small for %lu bytes\n", vaddr, size);
+		ret = -EFAULT;
+		goto fail_pages;
+	}
+
+	buf->vma = vb2_get_vma(vma);
+	if (!buf->vma) {
+		pr_err("failed to copy vma\n");
+		ret = -ENOMEM;
+		goto fail_pages;
+	}
+
+	/* extract page list from userspace mapping */
+	ret = vb2_dc_get_user_pages(start, pages, n_pages, vma, write);
 	if (ret) {
-		printk(KERN_ERR "Failed acquiring VMA for vaddr 0x%08lx\n",
-				vaddr);
-		kfree(buf);
-		return ERR_PTR(ret);
+		pr_err("failed to get user pages\n");
+		goto fail_vma;
+	}
+
+	sgt = kzalloc(sizeof(*sgt), GFP_KERNEL);
+	if (!sgt) {
+		pr_err("failed to allocate sg table\n");
+		ret = -ENOMEM;
+		goto fail_get_user_pages;
+	}
+
+	ret = sg_alloc_table_from_pages(sgt, pages, n_pages,
+		offset, size, GFP_KERNEL);
+	if (ret) {
+		pr_err("failed to initialize sg table\n");
+		goto fail_sgt;
+	}
+
+	/* pages are no longer needed */
+	kfree(pages);
+	pages = NULL;
+
+	sgt->nents = dma_map_sg(buf->dev, sgt->sgl, sgt->orig_nents,
+		buf->dma_dir);
+	if (sgt->nents <= 0) {
+		pr_err("failed to map scatterlist\n");
+		ret = -EIO;
+		goto fail_sgt_init;
+	}
+
+	contig_size = vb2_dc_get_contiguous_size(sgt);
+	if (contig_size < size) {
+		pr_err("contiguous mapping is too small %lu/%lu\n",
+			contig_size, size);
+		ret = -EFAULT;
+		goto fail_map_sg;
 	}
 
+	buf->dma_addr = sg_dma_address(sgt->sgl);
 	buf->size = size;
-	buf->dma_addr = dma_addr;
-	buf->vma = vma;
+	buf->dma_sgt = sgt;
 
 	return buf;
-}
 
-static void vb2_dc_put_userptr(void *mem_priv)
-{
-	struct vb2_dc_buf *buf = mem_priv;
+fail_map_sg:
+	dma_unmap_sg(buf->dev, sgt->sgl, sgt->orig_nents, buf->dma_dir);
 
-	if (!buf)
-		return;
+fail_sgt_init:
+	if (!vma_is_io(buf->vma))
+		vb2_dc_sgt_foreach_page(sgt, put_page);
+	sg_free_table(sgt);
+
+fail_sgt:
+	kfree(sgt);
 
+fail_get_user_pages:
+	if (pages && !vma_is_io(buf->vma))
+		while (n_pages)
+			put_page(pages[--n_pages]);
+
+fail_vma:
 	vb2_put_vma(buf->vma);
+
+fail_pages:
+	kfree(pages); /* kfree is NULL-proof */
+
+fail_buf:
 	kfree(buf);
+
+	return ERR_PTR(ret);
 }
 
 /*********************************************/
Signature · 1 line
1.7.9.5
#10 lore raw

Re: [PATCHv10 08/26] v4l: vb2-dma-contig: add support for scatterlist in userptr mode

Hi Tomasz,

Quoted 3 lines · Tomasz Stanislawski

This patch introduces usage of dma_map_sg to map memory behind a userspace pointer to a device as dma-contiguous mapping.

Perhaps I'm missing something, but I don't understand the purpose of this patch. If the device can do DMA SG, why use videobuf2-dma-contig and not videobuf2-dma-sg? What would be the difference design-wise between them if this patch is merged?

Signature · 2 lines
Best regards,
Pawel Osciak
maintainer
#11 lore raw

Re: [PATCHv10 08/26] v4l: vb2-dma-contig: add support for scatterlist in userptr mode

Hello,

Quoted 11 lines · Pawel Osciak

Hi Tomasz,

Quoted 3 lines · Tomasz Stanislawski

This patch introduces usage of dma_map_sg to map memory behind a userspace pointer to a device as dma-contiguous mapping.

Perhaps I'm missing something, but I don't understand the purpose of this patch. If the device can do DMA SG, why use videobuf2-dma-contig and not videobuf2-dma-sg?

This patch is for devices which doesn't do DMA SG, but might be behind IOMMU. In such case one can call dma_map_sg() with scatterlist of individual pages gathered from user pointer (anonymous memory of the process) which in turn will be mapped into contiguous dma adress space (dma_map_sg() returns only one chunk in such case). This is not very intuitive, but it was best way to fit such case into existing dma-mapping design.

Quoted 2 lines

What would be the difference design-wise
between them if this patch is merged?

Best regards

Signature · 2 lines
Marek Szyprowski
Samsung Poland R&D Center
#12 lore raw

PATCH v10 9/26 v4l: vb2: add prepare/finish callbacks to allocators

In reply to Tomasz Stanislawski

From: Marek Szyprowski <[email protected]>

This patch adds support for prepare/finish callbacks in VB2 allocators. These callback are used for buffer flushing.

---
 drivers/media/v4l2-core/videobuf2-core.c |   11 +++++++++++
 include/media/videobuf2-core.h           |    7 +++++++
 2 files changed, 18 insertions(+)
drivers/media/v4l2-core/videobuf2-core.c +11 -0
index a51dad6..613dea1 100644
@@ -844,6 +844,7 @@ void vb2_buffer_done(struct vb2_buffer *vb, enum vb2_buffer_state state)
 {
 	struct vb2_queue *q = vb->vb2_queue;
 	unsigned long flags;
+	unsigned int plane;
 
 	if (vb->state != VB2_BUF_STATE_ACTIVE)
 		return;
@@ -854,6 +855,10 @@ void vb2_buffer_done(struct vb2_buffer *vb, enum vb2_buffer_state state)
 	dprintk(4, "Done processing on buffer %d, state: %d\n",
 			vb->v4l2_buf.index, vb->state);
 
+	/* sync buffers */
+	for (plane = 0; plane < vb->num_planes; ++plane)
+		call_memop(q, finish, vb->planes[plane].mem_priv);
+
 	/* Add the buffer to the done buffers list */
 	spin_lock_irqsave(&q->done_lock, flags);
 	vb->state = state;
@@ -1136,9 +1141,15 @@ err:
 static void __enqueue_in_driver(struct vb2_buffer *vb)
 {
 	struct vb2_queue *q = vb->vb2_queue;
+	unsigned int plane;
 
 	vb->state = VB2_BUF_STATE_ACTIVE;
 	atomic_inc(&q->queued_count);
+
+	/* sync buffers */
+	for (plane = 0; plane < vb->num_planes; ++plane)
+		call_memop(q, prepare, vb->planes[plane].mem_priv);
+
 	q->ops->buf_queue(vb);
 }
 
include/media/videobuf2-core.h +7 -0
index 689ae4a..24b9c90 100644
@@ -56,6 +56,10 @@ struct vb2_fileio_data;
  *		dmabuf
  * @unmap_dmabuf: releases access control to the dmabuf - allocator is notified
  *		  that this driver is done using the dmabuf for now
+ * @prepare:	called every time the buffer is passed from userspace to the
+ *		driver, useful for cache synchronisation, optional
+ * @finish:	called every time the buffer is passed back from the driver
+ *		to the userspace, also optional
  * @vaddr:	return a kernel virtual address to a given memory buffer
  *		associated with the passed private structure or NULL if no
  *		such mapping exists
@@ -82,6 +86,9 @@ struct vb2_mem_ops {
 					unsigned long size, int write);
 	void		(*put_userptr)(void *buf_priv);
 
+	void		(*prepare)(void *buf_priv);
+	void		(*finish)(void *buf_priv);
+
 	void		*(*attach_dmabuf)(void *alloc_ctx, struct dma_buf *dbuf,
 				unsigned long size, int write);
 	void		(*detach_dmabuf)(void *buf_priv);
Signature · 1 line
1.7.9.5
#13 lore raw

PATCH v10 10/26 v4l: vb2-dma-contig: add prepare/finish to dma-contig allocator

In reply to Tomasz Stanislawski

From: Marek Szyprowski <[email protected]>

Add prepare/finish callbacks to vb2-dma-contig allocator.

---
 drivers/media/v4l2-core/videobuf2-dma-contig.c |   24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)
drivers/media/v4l2-core/videobuf2-dma-contig.c +24 -0
index 8486e06..494a824 100644
@@ -103,6 +103,28 @@ static unsigned int vb2_dc_num_users(void *buf_priv)
 	return atomic_read(&buf->refcount);
 }
 
+static void vb2_dc_prepare(void *buf_priv)
+{
+	struct vb2_dc_buf *buf = buf_priv;
+	struct sg_table *sgt = buf->dma_sgt;
+
+	if (!sgt)
+		return;
+
+	dma_sync_sg_for_device(buf->dev, sgt->sgl, sgt->nents, buf->dma_dir);
+}
+
+static void vb2_dc_finish(void *buf_priv)
+{
+	struct vb2_dc_buf *buf = buf_priv;
+	struct sg_table *sgt = buf->dma_sgt;
+
+	if (!sgt)
+		return;
+
+	dma_sync_sg_for_cpu(buf->dev, sgt->sgl, sgt->nents, buf->dma_dir);
+}
+
 /*********************************************/
 /*        callbacks for MMAP buffers         */
 /*********************************************/
@@ -366,6 +388,8 @@ const struct vb2_mem_ops vb2_dma_contig_memops = {
 	.mmap		= vb2_dc_mmap,
 	.get_userptr	= vb2_dc_get_userptr,
 	.put_userptr	= vb2_dc_put_userptr,
+	.prepare	= vb2_dc_prepare,
+	.finish		= vb2_dc_finish,
 	.num_users	= vb2_dc_num_users,
 };
 EXPORT_SYMBOL_GPL(vb2_dma_contig_memops);
Signature · 1 line
1.7.9.5
#14 lore raw

PATCH v10 11/26 v4l: vb2-dma-contig: add support for dma_buf importing

In reply to Tomasz Stanislawski

From: Sumit Semwal <[email protected]>

This patch makes changes for adding dma-contig as a dma_buf user. It provides function implementations for the {attach, detach, map, unmap}_dmabuf() mem_ops of DMABUF memory type.

[author of the original patch]
[integration with refactored dma-contig allocator]
---
 drivers/media/v4l2-core/Kconfig                |    1 +
 drivers/media/v4l2-core/videobuf2-dma-contig.c |  120 +++++++++++++++++++++++-
 2 files changed, 119 insertions(+), 2 deletions(-)
drivers/media/v4l2-core/Kconfig +1 -0
index 2e787cc..e30583b 100644
@@ -69,6 +69,7 @@ config VIDEOBUF2_DMA_CONTIG
 	tristate
 	select VIDEOBUF2_CORE
 	select VIDEOBUF2_MEMOPS
+	select DMA_SHARED_BUFFER
 
 config VIDEOBUF2_VMALLOC
 	tristate
drivers/media/v4l2-core/videobuf2-dma-contig.c +118 -2

156 lines not on this page · raw message

Signature · 1 line
1.7.9.5
#15 lore raw

PATCH v10 12/26 v4l: vb2-vmalloc: add support for dmabuf importing

In reply to Tomasz Stanislawski

This patch adds support for importing DMABUF files for vmalloc allocator in Videobuf2.

---
 drivers/media/v4l2-core/Kconfig             |    1 +
 drivers/media/v4l2-core/videobuf2-vmalloc.c |   56 +++++++++++++++++++++++++++
 2 files changed, 57 insertions(+)
drivers/media/v4l2-core/Kconfig +1 -0
index e30583b..65875c3 100644
@@ -75,6 +75,7 @@ config VIDEOBUF2_VMALLOC
 	tristate
 	select VIDEOBUF2_CORE
 	select VIDEOBUF2_MEMOPS
+	select DMA_SHARED_BUFFER
 
 config VIDEOBUF2_DMA_SG
 	tristate
drivers/media/v4l2-core/videobuf2-vmalloc.c +56 -0

73 lines not on this page · raw message

Signature · 1 line
1.7.9.5
#16 lore raw

PATCH v10 13/26 v4l: vivi: support for dmabuf importing

In reply to Tomasz Stanislawski

This patch enhances VIVI driver with a support for importing a buffer from DMABUF file descriptors.

---
 drivers/media/platform/vivi.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/media/platform/vivi.c +1 -1
index b366b05..9e077bb 100644
@@ -1308,7 +1308,7 @@ static int __init vivi_create_instance(int inst)
 	/* initialize queue */
 	q = &dev->vb_vidq;
 	q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
-	q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ;
+	q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | VB2_READ;
 	q->drv_priv = dev;
 	q->buf_struct_size = sizeof(struct vivi_buffer);
 	q->ops = &vivi_video_qops;
Signature · 1 line
1.7.9.5
#17 lore raw

PATCH v10 14/26 v4l: s5p-tv: mixer: support for dmabuf importing

In reply to Tomasz Stanislawski

This patch enhances s5p-tv with support for DMABUF importing via V4L2_MEMORY_DMABUF memory type.

---
 drivers/media/platform/s5p-tv/mixer_video.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/media/platform/s5p-tv/mixer_video.c +1 -1
index 0c1cd89..2421e527 100644
@@ -1093,7 +1093,7 @@ struct mxr_layer *mxr_base_layer_create(struct mxr_device *mdev,
 
 	layer->vb_queue = (struct vb2_queue) {
 		.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE,
-		.io_modes = VB2_MMAP | VB2_USERPTR,
+		.io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF,
 		.drv_priv = layer,
 		.buf_struct_size = sizeof(struct mxr_buffer),
 		.ops = &mxr_video_qops,
Signature · 1 line
1.7.9.5
#18 lore raw

PATCH v10 15/26 v4l: s5p-fimc: support for dmabuf importing

In reply to Tomasz Stanislawski

This patch enhances s5p-fimc with support for DMABUF importing via V4L2_MEMORY_DMABUF memory type.

---
 drivers/media/platform/s5p-fimc/fimc-capture.c |    2 +-
 drivers/media/platform/s5p-fimc/fimc-m2m.c     |    4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)
drivers/media/platform/s5p-fimc/fimc-capture.c +1 -1
index 367efd1..246bb32 100644
@@ -1730,7 +1730,7 @@ static int fimc_register_capture_device(struct fimc_dev *fimc,
 	q = &fimc->vid_cap.vbq;
 	memset(q, 0, sizeof(*q));
 	q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
-	q->io_modes = VB2_MMAP | VB2_USERPTR;
+	q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
 	q->drv_priv = fimc->vid_cap.ctx;
 	q->ops = &fimc_capture_qops;
 	q->mem_ops = &vb2_dma_contig_memops;
drivers/media/platform/s5p-fimc/fimc-m2m.c +2 -2
index 4500e44..17067a7 100644
@@ -622,7 +622,7 @@ static int queue_init(void *priv, struct vb2_queue *src_vq,
 	int ret;
 
 	src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
-	src_vq->io_modes = VB2_MMAP | VB2_USERPTR;
+	src_vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
 	src_vq->drv_priv = ctx;
 	src_vq->ops = &fimc_qops;
 	src_vq->mem_ops = &vb2_dma_contig_memops;
@@ -633,7 +633,7 @@ static int queue_init(void *priv, struct vb2_queue *src_vq,
 		return ret;
 
 	dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
-	dst_vq->io_modes = VB2_MMAP | VB2_USERPTR;
+	dst_vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
 	dst_vq->drv_priv = ctx;
 	dst_vq->ops = &fimc_qops;
 	dst_vq->mem_ops = &vb2_dma_contig_memops;
Signature · 1 line
1.7.9.5
#19 lore raw

PATCH v10 16/26 v4l: vb2-dma-contig: let mmap method to use dma_mmap_coherent call

In reply to Tomasz Stanislawski

From: Marek Szyprowski <[email protected]>

Let mmap method to use dma_mmap_coherent call. Moreover, this patch removes vb2_mmap_pfn_range from videobuf2 helpers as it was suggested by Laurent Pinchart. The function is no longer used in vb2 code.

---
 drivers/media/v4l2-core/videobuf2-dma-contig.c |   28 +++++++++++++++--
 drivers/media/v4l2-core/videobuf2-memops.c     |   40 ------------------------
 include/media/videobuf2-memops.h               |    5 ---
 3 files changed, 26 insertions(+), 47 deletions(-)
drivers/media/v4l2-core/videobuf2-dma-contig.c +26 -2
index a5804cf..0e065ce 100644
@@ -178,14 +178,38 @@ static void *vb2_dc_alloc(void *alloc_ctx, unsigned long size)
 static int vb2_dc_mmap(void *buf_priv, struct vm_area_struct *vma)
 {
 	struct vb2_dc_buf *buf = buf_priv;
+	int ret;
 
 	if (!buf) {
 		printk(KERN_ERR "No buffer to map\n");
 		return -EINVAL;
 	}
 
-	return vb2_mmap_pfn_range(vma, buf->dma_addr, buf->size,
-				  &vb2_common_vm_ops, &buf->handler);
+	/*
+	 * dma_mmap_* uses vm_pgoff as in-buffer offset, but we want to
+	 * map whole buffer
+	 */
+	vma->vm_pgoff = 0;
+
+	ret = dma_mmap_coherent(buf->dev, vma, buf->vaddr,
+		buf->dma_addr, buf->size);
+
+	if (ret) {
+		pr_err("Remapping memory failed, error: %d\n", ret);
+		return ret;
+	}
+
+	vma->vm_flags		|= VM_DONTEXPAND | VM_RESERVED;
+	vma->vm_private_data	= &buf->handler;
+	vma->vm_ops		= &vb2_common_vm_ops;
+
+	vma->vm_ops->open(vma);
+
+	pr_debug("%s: mapped dma addr 0x%08lx at 0x%08lx, size %ld\n",
+		__func__, (unsigned long)buf->dma_addr, vma->vm_start,
+		buf->size);
+
+	return 0;
 }
 
 /*********************************************/
drivers/media/v4l2-core/videobuf2-memops.c +0 -40
index 504cd4c..81c1ad8 100644
@@ -137,46 +137,6 @@ int vb2_get_contig_userptr(unsigned long vaddr, unsigned long size,
 EXPORT_SYMBOL_GPL(vb2_get_contig_userptr);
 
 /**
- * vb2_mmap_pfn_range() - map physical pages to userspace
- * @vma:	virtual memory region for the mapping
- * @paddr:	starting physical address of the memory to be mapped
- * @size:	size of the memory to be mapped
- * @vm_ops:	vm operations to be assigned to the created area
- * @priv:	private data to be associated with the area
- *
- * Returns 0 on success.
- */
-int vb2_mmap_pfn_range(struct vm_area_struct *vma, unsigned long paddr,
-				unsigned long size,
-				const struct vm_operations_struct *vm_ops,
-				void *priv)
-{
-	int ret;
-
-	size = min_t(unsigned long, vma->vm_end - vma->vm_start, size);
-
-	vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
-	ret = remap_pfn_range(vma, vma->vm_start, paddr >> PAGE_SHIFT,
-				size, vma->vm_page_prot);
-	if (ret) {
-		printk(KERN_ERR "Remapping memory failed, error: %d\n", ret);
-		return ret;
-	}
-
-	vma->vm_flags		|= VM_DONTEXPAND | VM_RESERVED;
-	vma->vm_private_data	= priv;
-	vma->vm_ops		= vm_ops;
-
-	vma->vm_ops->open(vma);
-
-	pr_debug("%s: mapped paddr 0x%08lx at 0x%08lx, size %ld\n",
-			__func__, paddr, vma->vm_start, size);
-
-	return 0;
-}
-EXPORT_SYMBOL_GPL(vb2_mmap_pfn_range);
-
-/**
  * vb2_common_vm_open() - increase refcount of the vma
  * @vma:	virtual memory region for the mapping
  *
include/media/videobuf2-memops.h +0 -5
index 84e1f6c..f05444c 100644
@@ -33,11 +33,6 @@ extern const struct vm_operations_struct vb2_common_vm_ops;
 int vb2_get_contig_userptr(unsigned long vaddr, unsigned long size,
 			   struct vm_area_struct **res_vma, dma_addr_t *res_pa);
 
-int vb2_mmap_pfn_range(struct vm_area_struct *vma, unsigned long paddr,
-				unsigned long size,
-				const struct vm_operations_struct *vm_ops,
-				void *priv);
-
 struct vm_area_struct *vb2_get_vma(struct vm_area_struct *vma);
 void vb2_put_vma(struct vm_area_struct *vma);
 
Signature · 1 line
1.7.9.5
#20 lore raw

PATCH v10 17/26 Documentation: media: description of DMABUF exporting in V4L2

In reply to Tomasz Stanislawski

This patch adds description and usage examples for exporting DMABUF file descriptor in V4L2.

---
 Documentation/DocBook/media/v4l/compat.xml        |    3 +
 Documentation/DocBook/media/v4l/io.xml            |    3 +
 Documentation/DocBook/media/v4l/v4l2.xml          |    1 +
 Documentation/DocBook/media/v4l/vidioc-expbuf.xml |  212 +++++++++++++++++++++
 4 files changed, 219 insertions(+)
 create mode 100644 Documentation/DocBook/media/v4l/vidioc-expbuf.xml
Documentation/DocBook/media/v4l/compat.xml +3 -0
index 50eb630..3dd9e78 100644
@@ -2590,6 +2590,9 @@ ioctls.</para>
 	  <para>Importing DMABUF file descriptors as a new IO method described
 	  in <xref linkend="dmabuf" />.</para>
         </listitem>
+        <listitem>
+	  <para>Exporting DMABUF files using &VIDIOC-EXPBUF; ioctl.</para>
+        </listitem>
       </itemizedlist>
     </section>
 
Documentation/DocBook/media/v4l/io.xml +3 -0
index 0abb5cb..81d0ed4 100644
@@ -488,6 +488,9 @@ DMA buffer from userspace using a file descriptor previously exported for a
 different or the same device (known as the importer role), or both. This
 section describes the DMABUF importer role API in V4L2.</para>
 
+    <para>Refer to <link linked="vidioc-expbuf"> DMABUF exporting </link> for
+details about exporting V4L2 buffers as DMABUF file descriptors.</para>
+
 <para>Input and output devices support the streaming I/O method when the
 <constant>V4L2_CAP_STREAMING</constant> flag in the
 <structfield>capabilities</structfield> field of &v4l2-capability; returned by
Documentation/DocBook/media/v4l/v4l2.xml +1 -0
index 10ccde9..4d110b1 100644
@@ -543,6 +543,7 @@ and discussions on the V4L mailing list.</revremark>
     &sub-enuminput;
     &sub-enumoutput;
     &sub-enumstd;
+    &sub-expbuf;
     &sub-g-audio;
     &sub-g-audioout;
     &sub-g-crop;
Documentation/DocBook/media/v4l/vidioc-expbuf.xml new file +212 -0

212 lines not on this page · raw message

Signature · 1 line
1.7.9.5
Acked-by on v10 17/26
#21 lore raw

Re: [PATCHv10 17/26] Documentation: media: description of DMABUF exporting in V4L2

Quoted 6 lines · On Wed October 10 2012 16:46:36 Tomasz Stanislawski wrote:

This patch adds description and usage examples for exporting DMABUF file descriptor in V4L2.

Quoted 8 lines
---
 Documentation/DocBook/media/v4l/compat.xml        |    3 +
 Documentation/DocBook/media/v4l/io.xml            |    3 +
 Documentation/DocBook/media/v4l/v4l2.xml          |    1 +
 Documentation/DocBook/media/v4l/vidioc-expbuf.xml |  212 +++++++++++++++++++++
 4 files changed, 219 insertions(+)
 create mode 100644 Documentation/DocBook/media/v4l/vidioc-expbuf.xml
#22 lore raw

PATCH v10 18/26 v4l: add buffer exporting via dmabuf

In reply to Tomasz Stanislawski

This patch adds extension to V4L2 api. It allow to export a mmap buffer as file descriptor. New ioctl VIDIOC_EXPBUF is added. It takes a buffer offset used by mmap and return a file descriptor on success.

---
 drivers/media/v4l2-core/v4l2-compat-ioctl32.c |    1 +
 drivers/media/v4l2-core/v4l2-dev.c            |    1 +
 drivers/media/v4l2-core/v4l2-ioctl.c          |   10 +++++++++
 include/linux/videodev2.h                     |   28 +++++++++++++++++++++++++
 include/media/v4l2-ioctl.h                    |    2 ++
 5 files changed, 42 insertions(+)
drivers/media/v4l2-core/v4l2-compat-ioctl32.c +1 -0
index cc5998b..7157af3 100644
@@ -1018,6 +1018,7 @@ long v4l2_compat_ioctl32(struct file *file, unsigned int cmd, unsigned long arg)
 	case VIDIOC_S_FBUF32:
 	case VIDIOC_OVERLAY32:
 	case VIDIOC_QBUF32:
+	case VIDIOC_EXPBUF:
 	case VIDIOC_DQBUF32:
 	case VIDIOC_STREAMON32:
 	case VIDIOC_STREAMOFF32:
drivers/media/v4l2-core/v4l2-dev.c +1 -0
index a2df842..98dcad9 100644
@@ -571,6 +571,7 @@ static void determine_valid_ioctls(struct video_device *vdev)
 	SET_VALID_IOCTL(ops, VIDIOC_REQBUFS, vidioc_reqbufs);
 	SET_VALID_IOCTL(ops, VIDIOC_QUERYBUF, vidioc_querybuf);
 	SET_VALID_IOCTL(ops, VIDIOC_QBUF, vidioc_qbuf);
+	SET_VALID_IOCTL(ops, VIDIOC_EXPBUF, vidioc_expbuf);
 	SET_VALID_IOCTL(ops, VIDIOC_DQBUF, vidioc_dqbuf);
 	SET_VALID_IOCTL(ops, VIDIOC_STREAMON, vidioc_streamon);
 	SET_VALID_IOCTL(ops, VIDIOC_STREAMOFF, vidioc_streamoff);
drivers/media/v4l2-core/v4l2-ioctl.c +10 -0
index 530a67e..aa6e7c7 100644
@@ -454,6 +454,15 @@ static void v4l_print_buffer(const void *arg, bool write_only)
 			tc->type, tc->flags, tc->frames, *(__u32 *)tc->userbits);
 }
 
+static void v4l_print_exportbuffer(const void *arg, bool write_only)
+{
+	const struct v4l2_exportbuffer *p = arg;
+
+	pr_cont("fd=%d, type=%s, index=%u, plane=%u, flags=0x%08x\n",
+		p->fd, prt_names(p->type, v4l2_type_names),
+		p->index, p->plane, p->flags);
+}
+
 static void v4l_print_create_buffers(const void *arg, bool write_only)
 {
 	const struct v4l2_create_buffers *p = arg;
@@ -1961,6 +1970,7 @@ static struct v4l2_ioctl_info v4l2_ioctls[] = {
 	IOCTL_INFO_STD(VIDIOC_S_FBUF, vidioc_s_fbuf, v4l_print_framebuffer, INFO_FL_PRIO),
 	IOCTL_INFO_FNC(VIDIOC_OVERLAY, v4l_overlay, v4l_print_u32, INFO_FL_PRIO),
 	IOCTL_INFO_FNC(VIDIOC_QBUF, v4l_qbuf, v4l_print_buffer, INFO_FL_QUEUE),
+	IOCTL_INFO_STD(VIDIOC_EXPBUF, vidioc_expbuf, v4l_print_exportbuffer, INFO_FL_QUEUE | INFO_FL_CLEAR(v4l2_exportbuffer, flags)),
 	IOCTL_INFO_FNC(VIDIOC_DQBUF, v4l_dqbuf, v4l_print_buffer, INFO_FL_QUEUE),
 	IOCTL_INFO_FNC(VIDIOC_STREAMON, v4l_streamon, v4l_print_buftype, INFO_FL_PRIO | INFO_FL_QUEUE),
 	IOCTL_INFO_FNC(VIDIOC_STREAMOFF, v4l_streamoff, v4l_print_buftype, INFO_FL_PRIO | INFO_FL_QUEUE),
include/linux/videodev2.h +28 -0
index 07bc5d6..19765df 100644
@@ -696,6 +696,33 @@ struct v4l2_buffer {
 #define V4L2_BUF_FLAG_NO_CACHE_INVALIDATE	0x0800
 #define V4L2_BUF_FLAG_NO_CACHE_CLEAN		0x1000
 
+/**
+ * struct v4l2_exportbuffer - export of video buffer as DMABUF file descriptor
+ *
+ * @index:	id number of the buffer
+ * @type:	enum v4l2_buf_type; buffer type (type == *_MPLANE for
+ *		multiplanar buffers);
+ * @plane:	index of the plane to be exported, 0 for single plane queues
+ * @flags:	flags for newly created file, currently only O_CLOEXEC is
+ *		supported, refer to manual of open syscall for more details
+ * @fd:		file descriptor associated with DMABUF (set by driver)
+ *
+ * Contains data used for exporting a video buffer as DMABUF file descriptor.
+ * The buffer is identified by a 'cookie' returned by VIDIOC_QUERYBUF
+ * (identical to the cookie used to mmap() the buffer to userspace). All
+ * reserved fields must be set to zero. The field reserved0 is expected to
+ * become a structure 'type' allowing an alternative layout of the structure
+ * content. Therefore this field should not be used for any other extensions.
+ */
+struct v4l2_exportbuffer {
+	__u32		type; /* enum v4l2_buf_type */
+	__u32		index;
+	__u32		plane;
+	__u32		flags;
+	__s32		fd;
+	__u32		reserved[11];
+};
+
 /*
  *	O V E R L A Y   P R E V I E W
  */
@@ -1897,6 +1924,7 @@ struct v4l2_create_buffers {
 #define VIDIOC_S_FBUF		 _IOW('V', 11, struct v4l2_framebuffer)
 #define VIDIOC_OVERLAY		 _IOW('V', 14, int)
 #define VIDIOC_QBUF		_IOWR('V', 15, struct v4l2_buffer)
+#define VIDIOC_EXPBUF		_IOWR('V', 16, struct v4l2_exportbuffer)
 #define VIDIOC_DQBUF		_IOWR('V', 17, struct v4l2_buffer)
 #define VIDIOC_STREAMON		 _IOW('V', 18, int)
 #define VIDIOC_STREAMOFF	 _IOW('V', 19, int)
include/media/v4l2-ioctl.h +2 -0
index e48b571..4118ad1 100644
@@ -111,6 +111,8 @@ struct v4l2_ioctl_ops {
 	int (*vidioc_reqbufs) (struct file *file, void *fh, struct v4l2_requestbuffers *b);
 	int (*vidioc_querybuf)(struct file *file, void *fh, struct v4l2_buffer *b);
 	int (*vidioc_qbuf)    (struct file *file, void *fh, struct v4l2_buffer *b);
+	int (*vidioc_expbuf)  (struct file *file, void *fh,
+				struct v4l2_exportbuffer *e);
 	int (*vidioc_dqbuf)   (struct file *file, void *fh, struct v4l2_buffer *b);
 
 	int (*vidioc_create_bufs)(struct file *file, void *fh, struct v4l2_create_buffers *b);
Signature · 1 line
1.7.9.5
Acked-by on v10 18/26
#23 lore raw

Re: [PATCHv10 18/26] v4l: add buffer exporting via dmabuf

Quoted 6 lines · On Wed October 10 2012 16:46:37 Tomasz Stanislawski wrote:

This patch adds extension to V4L2 api. It allow to export a mmap buffer as file descriptor. New ioctl VIDIOC_EXPBUF is added. It takes a buffer offset used by mmap and return a file descriptor on success.

Quoted 7 lines
---
 drivers/media/v4l2-core/v4l2-compat-ioctl32.c |    1 +
 drivers/media/v4l2-core/v4l2-dev.c            |    1 +
 drivers/media/v4l2-core/v4l2-ioctl.c          |   10 +++++++++
 include/linux/videodev2.h                     |   28 +++++++++++++++++++++++++
 include/media/v4l2-ioctl.h                    |    2 ++
 5 files changed, 42 insertions(+)
#24 lore raw

PATCH v10 19/26 v4l: vb2: add buffer exporting via dmabuf

In reply to Tomasz Stanislawski

This patch adds extension to videobuf2-core. It allow to export a mmap buffer as a file descriptor.

---
 drivers/media/v4l2-core/v4l2-mem2mem.c   |   13 +++++
 drivers/media/v4l2-core/videobuf2-core.c |   83 ++++++++++++++++++++++++++++++
 include/media/v4l2-mem2mem.h             |    3 ++
 include/media/videobuf2-core.h           |    4 ++
 4 files changed, 103 insertions(+)
drivers/media/v4l2-core/v4l2-mem2mem.c +13 -0
index 3ac8358..9aa7cc7 100644
@@ -369,6 +369,19 @@ int v4l2_m2m_dqbuf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
 EXPORT_SYMBOL_GPL(v4l2_m2m_dqbuf);
 
 /**
+ * v4l2_m2m_expbuf() - export a source or destination buffer, depending on
+ * the type
+ */
+int v4l2_m2m_expbuf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
+		  struct v4l2_exportbuffer *eb)
+{
+	struct vb2_queue *vq;
+
+	vq = v4l2_m2m_get_vq(m2m_ctx, eb->type);
+	return vb2_expbuf(vq, eb);
+}
+EXPORT_SYMBOL_GPL(v4l2_m2m_expbuf);
+/**
  * v4l2_m2m_streamon() - turn on streaming for a video queue
  */
 int v4l2_m2m_streamon(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
drivers/media/v4l2-core/videobuf2-core.c +83 -0

95 lines not on this page · raw message

include/media/v4l2-mem2mem.h +3 -0
index 131cc4a..7e82d2b 100644
@@ -111,6 +111,9 @@ int v4l2_m2m_qbuf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
 int v4l2_m2m_dqbuf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
 		   struct v4l2_buffer *buf);
 
+int v4l2_m2m_expbuf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
+		   struct v4l2_exportbuffer *eb);
+
 int v4l2_m2m_streamon(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
 		      enum v4l2_buf_type type);
 int v4l2_m2m_streamoff(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
include/media/videobuf2-core.h +4 -0
index 24b9c90..9cfd4ee 100644
@@ -81,6 +81,7 @@ struct vb2_fileio_data;
 struct vb2_mem_ops {
 	void		*(*alloc)(void *alloc_ctx, unsigned long size);
 	void		(*put)(void *buf_priv);
+	struct dma_buf *(*get_dmabuf)(void *buf_priv);
 
 	void		*(*get_userptr)(void *alloc_ctx, unsigned long vaddr,
 					unsigned long size, int write);
@@ -363,6 +364,7 @@ int __must_check vb2_queue_init(struct vb2_queue *q);
 void vb2_queue_release(struct vb2_queue *q);
 
 int vb2_qbuf(struct vb2_queue *q, struct v4l2_buffer *b);
+int vb2_expbuf(struct vb2_queue *q, struct v4l2_exportbuffer *eb);
 int vb2_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b, bool nonblocking);
 
 int vb2_streamon(struct vb2_queue *q, enum v4l2_buf_type type);
@@ -472,6 +474,8 @@ int vb2_ioctl_qbuf(struct file *file, void *priv, struct v4l2_buffer *p);
 int vb2_ioctl_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p);
 int vb2_ioctl_streamon(struct file *file, void *priv, enum v4l2_buf_type i);
 int vb2_ioctl_streamoff(struct file *file, void *priv, enum v4l2_buf_type i);
+int vb2_ioctl_expbuf(struct file *file, void *priv,
+	struct v4l2_exportbuffer *p);
 
 /* struct v4l2_file_operations helpers */
 
Signature · 1 line
1.7.9.5
#25 lore raw

PATCH v10 20/26 v4l: vb2-dma-contig: add support for DMABUF exporting

In reply to Tomasz Stanislawski

This patch adds support for exporting a dma-contig buffer using DMABUF interface.

---
 drivers/media/v4l2-core/videobuf2-dma-contig.c |  200 ++++++++++++++++++++++++
 1 file changed, 200 insertions(+)
drivers/media/v4l2-core/videobuf2-dma-contig.c +200 -0
index 0e065ce..b138b5c 100644
@@ -36,6 +36,7 @@ struct vb2_dc_buf {
 	/* MMAP related */
 	struct vb2_vmarea_handler	handler;
 	atomic_t			refcount;
+	struct sg_table			*sgt_base;
 
 	/* USERPTR related */
 	struct vm_area_struct		*vma;
@@ -142,6 +143,10 @@ static void vb2_dc_put(void *buf_priv)
 	if (!atomic_dec_and_test(&buf->refcount))
 		return;
 
+	if (buf->sgt_base) {
+		sg_free_table(buf->sgt_base);
+		kfree(buf->sgt_base);
+	}
 	dma_free_coherent(buf->dev, buf->size, buf->vaddr, buf->dma_addr);
 	kfree(buf);
 }
@@ -213,6 +218,200 @@ static int vb2_dc_mmap(void *buf_priv, struct vm_area_struct *vma)
 }
 
 /*********************************************/
+/*         DMABUF ops for exporters          */
+/*********************************************/
+
+struct vb2_dc_attachment {
+	struct sg_table sgt;
+	enum dma_data_direction dir;
+};
+
+static int vb2_dc_dmabuf_ops_attach(struct dma_buf *dbuf, struct device *dev,
+	struct dma_buf_attachment *dbuf_attach)
+{
+	struct vb2_dc_attachment *attach;
+	unsigned int i;
+	struct scatterlist *rd, *wr;
+	struct sg_table *sgt;
+	struct vb2_dc_buf *buf = dbuf->priv;
+	int ret;
+
+	attach = kzalloc(sizeof(*attach), GFP_KERNEL);
+	if (!attach)
+		return -ENOMEM;
+
+	sgt = &attach->sgt;
+	/* Copy the buf->base_sgt scatter list to the attachment, as we can't
+	 * map the same scatter list to multiple attachments at the same time.
+	 */
+	ret = sg_alloc_table(sgt, buf->sgt_base->orig_nents, GFP_KERNEL);
+	if (ret) {
+		kfree(attach);
+		return -ENOMEM;
+	}
+
+	rd = buf->sgt_base->sgl;
+	wr = sgt->sgl;
+	for (i = 0; i < sgt->orig_nents; ++i) {
+		sg_set_page(wr, sg_page(rd), rd->length, rd->offset);
+		rd = sg_next(rd);
+		wr = sg_next(wr);
+	}
+
+	attach->dir = DMA_NONE;
+	dbuf_attach->priv = attach;
+
+	return 0;
+}
+
+static void vb2_dc_dmabuf_ops_detach(struct dma_buf *dbuf,
+	struct dma_buf_attachment *db_attach)
+{
+	struct vb2_dc_attachment *attach = db_attach->priv;
+	struct sg_table *sgt;
+
+	if (!attach)
+		return;
+
+	sgt = &attach->sgt;
+
+	/* release the scatterlist cache */
+	if (attach->dir != DMA_NONE)
+		dma_unmap_sg(db_attach->dev, sgt->sgl, sgt->orig_nents,
+			attach->dir);
+	sg_free_table(sgt);
+	kfree(attach);
+	db_attach->priv = NULL;
+}
+
+static struct sg_table *vb2_dc_dmabuf_ops_map(
+	struct dma_buf_attachment *db_attach, enum dma_data_direction dir)
+{
+	struct vb2_dc_attachment *attach = db_attach->priv;
+	/* stealing dmabuf mutex to serialize map/unmap operations */
+	struct mutex *lock = &db_attach->dmabuf->lock;
+	struct sg_table *sgt;
+	int ret;
+
+	mutex_lock(lock);
+
+	sgt = &attach->sgt;
+	/* return previously mapped sg table */
+	if (attach->dir == dir) {
+		mutex_unlock(lock);
+		return sgt;
+	}
+
+	/* release any previous cache */
+	if (attach->dir != DMA_NONE) {
+		dma_unmap_sg(db_attach->dev, sgt->sgl, sgt->orig_nents,
+			attach->dir);
+		attach->dir = DMA_NONE;
+	}
+
+	/* mapping to the client with new direction */
+	ret = dma_map_sg(db_attach->dev, sgt->sgl, sgt->orig_nents, dir);
+	if (ret <= 0) {
+		pr_err("failed to map scatterlist\n");
+		mutex_unlock(lock);
+		return ERR_PTR(-EIO);
+	}
+
+	attach->dir = dir;
+
+	mutex_unlock(lock);
+
+	return sgt;
+}
+
+static void vb2_dc_dmabuf_ops_unmap(struct dma_buf_attachment *db_attach,
+	struct sg_table *sgt, enum dma_data_direction dir)
+{
+	/* nothing to be done here */
+}
+
+static void vb2_dc_dmabuf_ops_release(struct dma_buf *dbuf)
+{
+	/* drop reference obtained in vb2_dc_get_dmabuf */
+	vb2_dc_put(dbuf->priv);
+}
+
+static void *vb2_dc_dmabuf_ops_kmap(struct dma_buf *dbuf, unsigned long pgnum)
+{
+	struct vb2_dc_buf *buf = dbuf->priv;
+
+	return buf->vaddr + pgnum * PAGE_SIZE;
+}
+
+static void *vb2_dc_dmabuf_ops_vmap(struct dma_buf *dbuf)
+{
+	struct vb2_dc_buf *buf = dbuf->priv;
+
+	return buf->vaddr;
+}
+
+static int vb2_dc_dmabuf_ops_mmap(struct dma_buf *dbuf,
+	struct vm_area_struct *vma)
+{
+	return vb2_dc_mmap(dbuf->priv, vma);
+}
+
+static struct dma_buf_ops vb2_dc_dmabuf_ops = {
+	.attach = vb2_dc_dmabuf_ops_attach,
+	.detach = vb2_dc_dmabuf_ops_detach,
+	.map_dma_buf = vb2_dc_dmabuf_ops_map,
+	.unmap_dma_buf = vb2_dc_dmabuf_ops_unmap,
+	.kmap = vb2_dc_dmabuf_ops_kmap,
+	.kmap_atomic = vb2_dc_dmabuf_ops_kmap,
+	.vmap = vb2_dc_dmabuf_ops_vmap,
+	.mmap = vb2_dc_dmabuf_ops_mmap,
+	.release = vb2_dc_dmabuf_ops_release,
+};
+
+static struct sg_table *vb2_dc_get_base_sgt(struct vb2_dc_buf *buf)
+{
+	int ret;
+	struct sg_table *sgt;
+
+	sgt = kmalloc(sizeof(*sgt), GFP_KERNEL);
+	if (!sgt) {
+		dev_err(buf->dev, "failed to alloc sg table\n");
+		return NULL;
+	}
+
+	ret = dma_get_sgtable(buf->dev, sgt, buf->vaddr, buf->dma_addr,
+		buf->size);
+	if (ret < 0) {
+		dev_err(buf->dev, "failed to get scatterlist from DMA API\n");
+		kfree(sgt);
+		return NULL;
+	}
+
+	return sgt;
+}
+
+static struct dma_buf *vb2_dc_get_dmabuf(void *buf_priv)
+{
+	struct vb2_dc_buf *buf = buf_priv;
+	struct dma_buf *dbuf;
+
+	if (!buf->sgt_base)
+		buf->sgt_base = vb2_dc_get_base_sgt(buf);
+
+	if (WARN_ON(!buf->sgt_base))
+		return NULL;
+
+	dbuf = dma_buf_export(buf, &vb2_dc_dmabuf_ops, buf->size, 0);
+	if (IS_ERR(dbuf))
+		return NULL;
+
+	/* dmabuf keeps reference to vb2 buffer */
+	atomic_inc(&buf->refcount);
+
+	return dbuf;
+}
+
+/*********************************************/
 /*       callbacks for USERPTR buffers       */
 /*********************************************/
 
@@ -519,6 +718,7 @@ static void *vb2_dc_attach_dmabuf(void *alloc_ctx, struct dma_buf *dbuf,
 const struct vb2_mem_ops vb2_dma_contig_memops = {
 	.alloc		= vb2_dc_alloc,
 	.put		= vb2_dc_put,
+	.get_dmabuf	= vb2_dc_get_dmabuf,
 	.cookie		= vb2_dc_cookie,
 	.vaddr		= vb2_dc_vaddr,
 	.mmap		= vb2_dc_mmap,
Signature · 1 line
1.7.9.5
Acked-by on v10 20/26
#26 lore raw

Re: [PATCHv10 20/26] v4l: vb2-dma-contig: add support for DMABUF exporting

Hi Tomasz,

Quoted 6 lines · On Wednesday 10 October 2012 16:46:39 Tomasz Stanislawski wrote:

This patch adds support for exporting a dma-contig buffer using DMABUF interface.

Quoted 237 lines
---
 drivers/media/v4l2-core/videobuf2-dma-contig.c |  200 +++++++++++++++++++++
 1 file changed, 200 insertions(+)
(quoted diff) +0 -0

b/drivers/media/v4l2-core/videobuf2-dma-contig.c index 0e065ce..b138b5c 100644

drivers/media/v4l2-core/videobuf2-dma-contig.c +198 -0
@@ -36,6 +36,7 @@ struct vb2_dc_buf {
 	/* MMAP related */
 	struct vb2_vmarea_handler	handler;
 	atomic_t			refcount;
+	struct sg_table			*sgt_base;
 
 	/* USERPTR related */
 	struct vm_area_struct		*vma;
@@ -142,6 +143,10 @@ static void vb2_dc_put(void *buf_priv)
 	if (!atomic_dec_and_test(&buf->refcount))
 		return;
 
+	if (buf->sgt_base) {
+		sg_free_table(buf->sgt_base);
+		kfree(buf->sgt_base);
+	}
 	dma_free_coherent(buf->dev, buf->size, buf->vaddr, buf->dma_addr);
 	kfree(buf);
 }
@@ -213,6 +218,200 @@ static int vb2_dc_mmap(void *buf_priv, struct
 vm_area_struct *vma) }
 
 /*********************************************/
+/*         DMABUF ops for exporters          */
+/*********************************************/
+
+struct vb2_dc_attachment {
+	struct sg_table sgt;
+	enum dma_data_direction dir;
+};
+
+static int vb2_dc_dmabuf_ops_attach(struct dma_buf *dbuf, struct device
 *dev, +	struct dma_buf_attachment *dbuf_attach)
+{
+	struct vb2_dc_attachment *attach;
+	unsigned int i;
+	struct scatterlist *rd, *wr;
+	struct sg_table *sgt;
+	struct vb2_dc_buf *buf = dbuf->priv;
+	int ret;
+
+	attach = kzalloc(sizeof(*attach), GFP_KERNEL);
+	if (!attach)
+		return -ENOMEM;
+
+	sgt = &attach->sgt;
+	/* Copy the buf->base_sgt scatter list to the attachment, as we can't
+	 * map the same scatter list to multiple attachments at the same time.
+	 */
+	ret = sg_alloc_table(sgt, buf->sgt_base->orig_nents, GFP_KERNEL);
+	if (ret) {
+		kfree(attach);
+		return -ENOMEM;
+	}
+
+	rd = buf->sgt_base->sgl;
+	wr = sgt->sgl;
+	for (i = 0; i < sgt->orig_nents; ++i) {
+		sg_set_page(wr, sg_page(rd), rd->length, rd->offset);
+		rd = sg_next(rd);
+		wr = sg_next(wr);
+	}
+
+	attach->dir = DMA_NONE;
+	dbuf_attach->priv = attach;
+
+	return 0;
+}
+
+static void vb2_dc_dmabuf_ops_detach(struct dma_buf *dbuf,
+	struct dma_buf_attachment *db_attach)
+{
+	struct vb2_dc_attachment *attach = db_attach->priv;
+	struct sg_table *sgt;
+
+	if (!attach)
+		return;
+
+	sgt = &attach->sgt;
+
+	/* release the scatterlist cache */
+	if (attach->dir != DMA_NONE)
+		dma_unmap_sg(db_attach->dev, sgt->sgl, sgt->orig_nents,
+			attach->dir);
+	sg_free_table(sgt);
+	kfree(attach);
+	db_attach->priv = NULL;
+}
+
+static struct sg_table *vb2_dc_dmabuf_ops_map(
+	struct dma_buf_attachment *db_attach, enum dma_data_direction dir)
+{
+	struct vb2_dc_attachment *attach = db_attach->priv;
+	/* stealing dmabuf mutex to serialize map/unmap operations */
+	struct mutex *lock = &db_attach->dmabuf->lock;
+	struct sg_table *sgt;
+	int ret;
+
+	mutex_lock(lock);
+
+	sgt = &attach->sgt;
+	/* return previously mapped sg table */
+	if (attach->dir == dir) {
+		mutex_unlock(lock);
+		return sgt;
+	}
+
+	/* release any previous cache */
+	if (attach->dir != DMA_NONE) {
+		dma_unmap_sg(db_attach->dev, sgt->sgl, sgt->orig_nents,
+			attach->dir);
+		attach->dir = DMA_NONE;
+	}
+
+	/* mapping to the client with new direction */
+	ret = dma_map_sg(db_attach->dev, sgt->sgl, sgt->orig_nents, dir);
+	if (ret <= 0) {
+		pr_err("failed to map scatterlist\n");
+		mutex_unlock(lock);
+		return ERR_PTR(-EIO);
+	}
+
+	attach->dir = dir;
+
+	mutex_unlock(lock);
+
+	return sgt;
+}
+
+static void vb2_dc_dmabuf_ops_unmap(struct dma_buf_attachment *db_attach,
+	struct sg_table *sgt, enum dma_data_direction dir)
+{
+	/* nothing to be done here */
+}
+
+static void vb2_dc_dmabuf_ops_release(struct dma_buf *dbuf)
+{
+	/* drop reference obtained in vb2_dc_get_dmabuf */
+	vb2_dc_put(dbuf->priv);
+}
+
+static void *vb2_dc_dmabuf_ops_kmap(struct dma_buf *dbuf, unsigned long
 pgnum) +{
+	struct vb2_dc_buf *buf = dbuf->priv;
+
+	return buf->vaddr + pgnum * PAGE_SIZE;
+}
+
+static void *vb2_dc_dmabuf_ops_vmap(struct dma_buf *dbuf)
+{
+	struct vb2_dc_buf *buf = dbuf->priv;
+
+	return buf->vaddr;
+}
+
+static int vb2_dc_dmabuf_ops_mmap(struct dma_buf *dbuf,
+	struct vm_area_struct *vma)
+{
+	return vb2_dc_mmap(dbuf->priv, vma);
+}
+
+static struct dma_buf_ops vb2_dc_dmabuf_ops = {
+	.attach = vb2_dc_dmabuf_ops_attach,
+	.detach = vb2_dc_dmabuf_ops_detach,
+	.map_dma_buf = vb2_dc_dmabuf_ops_map,
+	.unmap_dma_buf = vb2_dc_dmabuf_ops_unmap,
+	.kmap = vb2_dc_dmabuf_ops_kmap,
+	.kmap_atomic = vb2_dc_dmabuf_ops_kmap,
+	.vmap = vb2_dc_dmabuf_ops_vmap,
+	.mmap = vb2_dc_dmabuf_ops_mmap,
+	.release = vb2_dc_dmabuf_ops_release,
+};
+
+static struct sg_table *vb2_dc_get_base_sgt(struct vb2_dc_buf *buf)
+{
+	int ret;
+	struct sg_table *sgt;
+
+	sgt = kmalloc(sizeof(*sgt), GFP_KERNEL);
+	if (!sgt) {
+		dev_err(buf->dev, "failed to alloc sg table\n");
+		return NULL;
+	}
+
+	ret = dma_get_sgtable(buf->dev, sgt, buf->vaddr, buf->dma_addr,
+		buf->size);
+	if (ret < 0) {
+		dev_err(buf->dev, "failed to get scatterlist from DMA API\n");
+		kfree(sgt);
+		return NULL;
+	}
+
+	return sgt;
+}
+
+static struct dma_buf *vb2_dc_get_dmabuf(void *buf_priv)
+{
+	struct vb2_dc_buf *buf = buf_priv;
+	struct dma_buf *dbuf;
+
+	if (!buf->sgt_base)
+		buf->sgt_base = vb2_dc_get_base_sgt(buf);
+
+	if (WARN_ON(!buf->sgt_base))
+		return NULL;
+
+	dbuf = dma_buf_export(buf, &vb2_dc_dmabuf_ops, buf->size, 0);
+	if (IS_ERR(dbuf))
+		return NULL;
+
+	/* dmabuf keeps reference to vb2 buffer */
+	atomic_inc(&buf->refcount);
+
+	return dbuf;
+}
+
+/*********************************************/
 /*       callbacks for USERPTR buffers       */
 /*********************************************/
@@ -519,6 +718,7 @@ static void *vb2_dc_attach_dmabuf(void *alloc_ctx,
 struct dma_buf *dbuf, const struct vb2_mem_ops vb2_dma_contig_memops = {
 	.alloc		= vb2_dc_alloc,
 	.put		= vb2_dc_put,
+	.get_dmabuf	= vb2_dc_get_dmabuf,
 	.cookie		= vb2_dc_cookie,
 	.vaddr		= vb2_dc_vaddr,
 	.mmap		= vb2_dc_mmap,
Signature · 3 lines
Regards,

Laurent Pinchart
#27 lore raw

PATCH v10 21/26 v4l: vb2-dma-contig: add reference counting for a device from allocator context

In reply to Tomasz Stanislawski

This patch adds taking reference to the device for MMAP buffers.

Such buffers, may be exported using DMABUF mechanism. If the driver that created a queue is unloaded then the queue is released, the device might be released too. However, buffers cannot be released if they are referenced by DMABUF descriptor(s). The device pointer kept in a buffer must be valid for the whole buffer's lifetime. Therefore MMAP buffers should take a reference to the device to avoid risk of dangling pointers.

---
 drivers/media/v4l2-core/videobuf2-dma-contig.c |    4 ++++
 1 file changed, 4 insertions(+)
drivers/media/v4l2-core/videobuf2-dma-contig.c +4 -0
index b138b5c..2d661fd 100644
@@ -148,6 +148,7 @@ static void vb2_dc_put(void *buf_priv)
 		kfree(buf->sgt_base);
 	}
 	dma_free_coherent(buf->dev, buf->size, buf->vaddr, buf->dma_addr);
+	put_device(buf->dev);
 	kfree(buf);
 }
 
@@ -168,6 +169,9 @@ static void *vb2_dc_alloc(void *alloc_ctx, unsigned long size)
 		return ERR_PTR(-ENOMEM);
 	}
 
+	/* prevent the device from release while the buffer is exported */
+	get_device(dev);
+
 	buf->dev = dev;
 	buf->size = size;
 
Signature · 1 line
1.7.9.5
Acked-by on v10 21/26
#28 lore raw

Re: [PATCHv10 21/26] v4l: vb2-dma-contig: add reference counting for a device from allocator context

Hi Tomasz,

Thanks for the patch.

Quoted 11 lines · On Wednesday 10 October 2012 16:46:40 Tomasz Stanislawski wrote:

This patch adds taking reference to the device for MMAP buffers.

Such buffers, may be exported using DMABUF mechanism. If the driver that created a queue is unloaded then the queue is released, the device might be released too. However, buffers cannot be released if they are referenced by DMABUF descriptor(s). The device pointer kept in a buffer must be valid for the whole buffer's lifetime. Therefore MMAP buffers should take a reference to the device to avoid risk of dangling pointers.

But two small comments below.

Quoted 18 lines
---
 drivers/media/v4l2-core/videobuf2-dma-contig.c |    4 ++++
 1 file changed, 4 insertions(+)
(quoted diff) +0 -0

b/drivers/media/v4l2-core/videobuf2-dma-contig.c index b138b5c..2d661fd 100644

drivers/media/v4l2-core/videobuf2-dma-contig.c +1 -0
@@ -148,6 +148,7 @@ static void vb2_dc_put(void *buf_priv)
 		kfree(buf->sgt_base);
 	}
 	dma_free_coherent(buf->dev, buf->size, buf->vaddr, buf->dma_addr);
+	put_device(buf->dev);
 	kfree(buf);
 }
 

drivers/media/v4l2-core/videobuf2-dma-contig.c static void *vb2_dc_alloc(void *alloc_ctx, unsigned long · line 172

Quoted 4 lines
drivers/media/v4l2-core/videobuf2-dma-contig.c +1 -0
@@ -168,6 +169,9 @@ static void *vb2_dc_alloc(void *alloc_ctx, unsigned long
 size) return ERR_PTR(-ENOMEM);
 	}
 
+	/* prevent the device from release while the buffer is exported */

s/prevent/Prevent/ ?

drivers/media/v4l2-core/videobuf2-dma-contig.c static void *vb2_dc_alloc(void *alloc_ctx, unsigned long size) · line 175

Quoted 3 lines
drivers/media/v4l2-core/videobuf2-dma-contig.c +2 -0
+	get_device(dev);
+
 	buf->dev = dev;

What about just

	buf->dev = get_device(dev);
Quoted 1 line
drivers/media/v4l2-core/videobuf2-dma-contig.c +0 -0
 	buf->size = size;
Signature · 3 lines
Regards,

Laurent Pinchart
#29 lore raw

Re: [PATCHv10 21/26] v4l: vb2-dma-contig: add reference counting for a device from allocator context

Hi Laurent,
Thank your your review.

Quoted 38 lines · Laurent Pinchart

Hi Tomasz,

Thanks for the patch.

Quoted 11 lines · On Wednesday 10 October 2012 16:46:40 Tomasz Stanislawski wrote:

This patch adds taking reference to the device for MMAP buffers.

Such buffers, may be exported using DMABUF mechanism. If the driver that created a queue is unloaded then the queue is released, the device might be released too. However, buffers cannot be released if they are referenced by DMABUF descriptor(s). The device pointer kept in a buffer must be valid for the whole buffer's lifetime. Therefore MMAP buffers should take a reference to the device to avoid risk of dangling pointers.

But two small comments below.

Quoted 17 lines
---
 drivers/media/v4l2-core/videobuf2-dma-contig.c |    4 ++++
 1 file changed, 4 insertions(+)
(quoted diff) +0 -0

b/drivers/media/v4l2-core/videobuf2-dma-contig.c index b138b5c..2d661fd 100644

drivers/media/v4l2-core/videobuf2-dma-contig.c +1 -0
@@ -148,6 +148,7 @@ static void vb2_dc_put(void *buf_priv)
 		kfree(buf->sgt_base);
 	}
 	dma_free_coherent(buf->dev, buf->size, buf->vaddr, buf->dma_addr);
+	put_device(buf->dev);
 	kfree(buf);
 }
 
Quoted 8 lines

drivers/media/v4l2-core/videobuf2-dma-contig.c static void *vb2_dc_alloc(void *alloc_ctx, unsigned long · line 172

Quoted 5 lines
drivers/media/v4l2-core/videobuf2-dma-contig.c +1 -0
@@ -168,6 +169,9 @@ static void *vb2_dc_alloc(void *alloc_ctx, unsigned long
 size) return ERR_PTR(-ENOMEM);
 	}
 
+	/* prevent the device from release while the buffer is exported */

s/prevent/Prevent/ ?

s/release/being released/ ?

Quoted 8 lines

drivers/media/v4l2-core/videobuf2-dma-contig.c static void *vb2_dc_alloc(void *alloc_ctx, unsigned long size) · line 175

Quoted 3 lines
drivers/media/v4l2-core/videobuf2-dma-contig.c +2 -0
+	get_device(dev);
+
 	buf->dev = dev;

What about just

	buf->dev = get_device(dev);

Right, sorry I missed that from your previous review :).

Regards,
Tomasz Stanislawski

Quoted 1 line
Quoted 1 line
drivers/media/v4l2-core/videobuf2-dma-contig.c +0 -0
 	buf->size = size;
#30 lore raw

Re: [PATCHv10 21/26] v4l: vb2-dma-contig: add reference counting for a device from allocator context

Hi Tomasz,

Quoted 42 lines · On Friday 12 October 2012 08:28:23 Tomasz Stanislawski wrote:
Quoted 41 lines · Laurent Pinchart
Quoted 10 lines · On Wednesday 10 October 2012 16:46:40 Tomasz Stanislawski wrote:

This patch adds taking reference to the device for MMAP buffers.

Such buffers, may be exported using DMABUF mechanism. If the driver that created a queue is unloaded then the queue is released, the device might be
released too. However, buffers cannot be released if they are referenced by DMABUF descriptor(s). The device pointer kept in a buffer must be valid for the whole buffer's lifetime. Therefore MMAP buffers should take a reference to the device to avoid risk of dangling pointers.

Quoted 2 lines

But two small comments below.

Quoted 23 lines

---

 drivers/media/v4l2-core/videobuf2-dma-contig.c |    4 ++++
 1 file changed, 4 insertions(+)
(quoted diff) +0 -0

b/drivers/media/v4l2-core/videobuf2-dma-contig.c index b138b5c..2d661fd 100644

drivers/media/v4l2-core/videobuf2-dma-contig.c +1 -0
@@ -148,6 +148,7 @@ static void vb2_dc_put(void *buf_priv)
 		kfree(buf->sgt_base);
 	}
 	dma_free_coherent(buf->dev, buf->size, buf->vaddr, buf->dma_addr);
+	put_device(buf->dev);
 	kfree(buf);
 }
 
@@ -168,6 +169,9 @@ static void *vb2_dc_alloc(void *alloc_ctx, unsigned
 long size)
 		return ERR_PTR(-ENOMEM);
 	}
 
Quoted 5 lines
Quoted 3 lines

drivers/media/v4l2-core/videobuf2-dma-contig.c static void *vb2_dc_alloc(void *alloc_ctx, unsigned long size) · line 172

Quoted 1 line
drivers/media/v4l2-core/videobuf2-dma-contig.c +1 -0
+	/* prevent the device from release while the buffer is exported */

s/prevent/Prevent/ ?

s/release/being released/ ?

Oops. Of course :-)

Quoted 9 lines
Quoted 7 lines

drivers/media/v4l2-core/videobuf2-dma-contig.c static void *vb2_dc_alloc(void *alloc_ctx, unsigned long size) · line 175

Quoted 3 lines
drivers/media/v4l2-core/videobuf2-dma-contig.c +2 -0
+	get_device(dev);
+
 	buf->dev = dev;

What about just

	buf->dev = get_device(dev);

Right, sorry I missed that from your previous review :).

Signature · 3 lines
Regards,

Laurent Pinchart
#31 lore raw

PATCH v10 22/26 v4l: vb2-dma-contig: fail if user ptr buffer is not correctly aligned

In reply to Tomasz Stanislawski

From: Marek Szyprowski <[email protected]>

The DMA transfer must be aligned to a specific value. If userptr is not aligned to DMA requirements then unexpected corruptions of the memory may occur before or after a buffer. To prevent such situations, all unligned userptr buffers are rejected at VIDIOC_QBUF.

---
 drivers/media/v4l2-core/videobuf2-dma-contig.c |   12 ++++++++++++
 1 file changed, 12 insertions(+)
drivers/media/v4l2-core/videobuf2-dma-contig.c +12 -0
index 2d661fd..571a919 100644
@@ -493,6 +493,18 @@ static void *vb2_dc_get_userptr(void *alloc_ctx, unsigned long vaddr,
 	struct vm_area_struct *vma;
 	struct sg_table *sgt;
 	unsigned long contig_size;
+	unsigned long dma_align = dma_get_cache_alignment();
+
+	/* Only cache aligned DMA transfers are reliable */
+	if (!IS_ALIGNED(vaddr | size, dma_align)) {
+		pr_debug("user data must be aligned to %lu bytes\n", dma_align);
+		return ERR_PTR(-EINVAL);
+	}
+
+	if (!size) {
+		pr_debug("size is zero\n");
+		return ERR_PTR(-EINVAL);
+	}
 
 	buf = kzalloc(sizeof *buf, GFP_KERNEL);
 	if (!buf)
Signature · 1 line
1.7.9.5
#32 lore raw

Re: [PATCHv10 22/26] v4l: vb2-dma-contig: fail if user ptr buffer is not correctly aligned

Hi Tomasz,

Thanks for the patch.

Quoted 19 lines · On Wednesday 10 October 2012 16:46:41 Tomasz Stanislawski wrote:

From: Marek Szyprowski <[email protected]>

The DMA transfer must be aligned to a specific value. If userptr is not aligned to DMA requirements then unexpected corruptions of the memory may occur before or after a buffer. To prevent such situations, all unligned userptr buffers are rejected at VIDIOC_QBUF.

---
 drivers/media/v4l2-core/videobuf2-dma-contig.c |   12 ++++++++++++
 1 file changed, 12 insertions(+)
(quoted diff) +0 -0

b/drivers/media/v4l2-core/videobuf2-dma-contig.c index 2d661fd..571a919 100644

drivers/media/v4l2-core/videobuf2-dma-contig.c static void *vb2_dc_get_userptr(void *alloc_ctx, · line 502

Quoted 10 lines
drivers/media/v4l2-core/videobuf2-dma-contig.c +7 -0
@@ -493,6 +493,18 @@ static void *vb2_dc_get_userptr(void *alloc_ctx,
 unsigned long vaddr, struct vm_area_struct *vma;
 	struct sg_table *sgt;
 	unsigned long contig_size;
+	unsigned long dma_align = dma_get_cache_alignment();
+
+	/* Only cache aligned DMA transfers are reliable */
+	if (!IS_ALIGNED(vaddr | size, dma_align)) {
+		pr_debug("user data must be aligned to %lu bytes\n", dma_align);
+		return ERR_PTR(-EINVAL);
+	}

Looks good to me.

drivers/media/v4l2-core/videobuf2-dma-contig.c static void *vb2_dc_get_userptr(void *alloc_ctx, unsigned long vaddr, · line 507

Quoted 4 lines
drivers/media/v4l2-core/videobuf2-dma-contig.c +4 -0
+	if (!size) {
+		pr_debug("size is zero\n");
+		return ERR_PTR(-EINVAL);
+	}

Can this happen ? The vb2 core already has

                /* Check if the provided plane buffer is large enough */
                if (planes[plane].length < q->plane_sizes[plane]) {
                        ret = -EINVAL;
                        goto err;
                }

Unless queue_setup sets plane_sizes to 0 we can't reach vb2_dc_get_userptr.

Quoted 2 lines
drivers/media/v4l2-core/videobuf2-dma-contig.c +0 -0
 	buf = kzalloc(sizeof *buf, GFP_KERNEL);
 	if (!buf)
Signature · 3 lines
Regards,

Laurent Pinchart
#33 lore raw

Re: [PATCHv10 22/26] v4l: vb2-dma-contig: fail if user ptr buffer is not correctly aligned

Hi Laurent,
Thank you for the review.
Please refer to the comments below.

Quoted 44 lines · Laurent Pinchart

Hi Tomasz,

Thanks for the patch.

Quoted 19 lines · On Wednesday 10 October 2012 16:46:41 Tomasz Stanislawski wrote:

From: Marek Szyprowski <[email protected]>

The DMA transfer must be aligned to a specific value. If userptr is not aligned to DMA requirements then unexpected corruptions of the memory may occur before or after a buffer. To prevent such situations, all unligned userptr buffers are rejected at VIDIOC_QBUF.

---
 drivers/media/v4l2-core/videobuf2-dma-contig.c |   12 ++++++++++++
 1 file changed, 12 insertions(+)
(quoted diff) +0 -0

b/drivers/media/v4l2-core/videobuf2-dma-contig.c index 2d661fd..571a919 100644

drivers/media/v4l2-core/videobuf2-dma-contig.c static void *vb2_dc_get_userptr(void *alloc_ctx, · line 502

Quoted 10 lines
drivers/media/v4l2-core/videobuf2-dma-contig.c +7 -0
@@ -493,6 +493,18 @@ static void *vb2_dc_get_userptr(void *alloc_ctx,
 unsigned long vaddr, struct vm_area_struct *vma;
 	struct sg_table *sgt;
 	unsigned long contig_size;
+	unsigned long dma_align = dma_get_cache_alignment();
+
+	/* Only cache aligned DMA transfers are reliable */
+	if (!IS_ALIGNED(vaddr | size, dma_align)) {
+		pr_debug("user data must be aligned to %lu bytes\n", dma_align);
+		return ERR_PTR(-EINVAL);
+	}

Looks good to me.

drivers/media/v4l2-core/videobuf2-dma-contig.c static void *vb2_dc_get_userptr(void *alloc_ctx, unsigned long vaddr, · line 507

Quoted 4 lines
drivers/media/v4l2-core/videobuf2-dma-contig.c +4 -0
+	if (!size) {
+		pr_debug("size is zero\n");
+		return ERR_PTR(-EINVAL);
+	}

Can this happen ? The vb2 core already has

Quoted 8 lines
                /* Check if the provided plane buffer is large enough */
                if (planes[plane].length < q->plane_sizes[plane]) {
                        ret = -EINVAL;
                        goto err;
                }

Unless queue_setup sets plane_sizes to 0 we can't reach vb2_dc_get_userptr.

Yes.. unfortunately, some drivers set plane_size to 0 at queue_setup. Especially, if REQBUFS is called before any S_FMT.
Maybe it is just a driver bug.

However, VB2 makes no sanity check if plane_sizes[] is zero. I was not able to find in Documentation nor code comments any explicit statement that plane_size cannot be zero.

Therefore I have to reject reject a 0-bytes-long user pointer at vb2_dc_get_userptr before creating an empty scatterlist and passing it to the DMA layer.

Regards,
Tomasz Stanislawski

Quoted 3 lines
Quoted 2 lines
drivers/media/v4l2-core/videobuf2-dma-contig.c +0 -0
 	buf = kzalloc(sizeof *buf, GFP_KERNEL);
 	if (!buf)
#34 lore raw

Re: [PATCHv10 22/26] v4l: vb2-dma-contig: fail if user ptr buffer is not correctly aligned

Quoted 55 lines · On Fri 12 October 2012 09:44:05 Tomasz Stanislawski wrote:

Hi Laurent,
Thank you for the review.
Please refer to the comments below.

Quoted 50 lines · Laurent Pinchart

Hi Tomasz,

Thanks for the patch.

Quoted 19 lines · On Wednesday 10 October 2012 16:46:41 Tomasz Stanislawski wrote:

From: Marek Szyprowski <[email protected]>

The DMA transfer must be aligned to a specific value. If userptr is not aligned to DMA requirements then unexpected corruptions of the memory may occur before or after a buffer. To prevent such situations, all unligned userptr buffers are rejected at VIDIOC_QBUF.

---
 drivers/media/v4l2-core/videobuf2-dma-contig.c |   12 ++++++++++++
 1 file changed, 12 insertions(+)
(quoted diff) +0 -0

b/drivers/media/v4l2-core/videobuf2-dma-contig.c index 2d661fd..571a919 100644

drivers/media/v4l2-core/videobuf2-dma-contig.c static void *vb2_dc_get_userptr(void *alloc_ctx, · line 502

Quoted 10 lines
drivers/media/v4l2-core/videobuf2-dma-contig.c +7 -0
@@ -493,6 +493,18 @@ static void *vb2_dc_get_userptr(void *alloc_ctx,
 unsigned long vaddr, struct vm_area_struct *vma;
 	struct sg_table *sgt;
 	unsigned long contig_size;
+	unsigned long dma_align = dma_get_cache_alignment();
+
+	/* Only cache aligned DMA transfers are reliable */
+	if (!IS_ALIGNED(vaddr | size, dma_align)) {
+		pr_debug("user data must be aligned to %lu bytes\n", dma_align);
+		return ERR_PTR(-EINVAL);
+	}

Looks good to me.

drivers/media/v4l2-core/videobuf2-dma-contig.c static void *vb2_dc_get_userptr(void *alloc_ctx, unsigned long vaddr, · line 507

Quoted 4 lines
drivers/media/v4l2-core/videobuf2-dma-contig.c +4 -0
+	if (!size) {
+		pr_debug("size is zero\n");
+		return ERR_PTR(-EINVAL);
+	}

Can this happen ? The vb2 core already has

                /* Check if the provided plane buffer is large enough */
                if (planes[plane].length < q->plane_sizes[plane]) {
                        ret = -EINVAL;
                        goto err;
                }
Quoted 6 lines
Quoted 2 lines

Unless queue_setup sets plane_sizes to 0 we can't reach vb2_dc_get_userptr.

Yes.. unfortunately, some drivers set plane_size to 0 at queue_setup. Especially, if REQBUFS is called before any S_FMT.
Maybe it is just a driver bug.

That's a driver bug. Planes with size 0 make no sense whatsoever. vb2 should WARN_ON on that and return an error.

My guess is that these drivers do not set up a default format as they should.

Regards,

Hans

Quoted 15 lines

However, VB2 makes no sanity check if plane_sizes[] is zero. I was not able to find in Documentation nor code comments any explicit statement that plane_size cannot be zero.

Therefore I have to reject reject a 0-bytes-long user pointer at vb2_dc_get_userptr before creating an empty scatterlist and passing it to the DMA layer.

Regards,
Tomasz Stanislawski

Quoted 3 lines
Quoted 2 lines
drivers/media/v4l2-core/videobuf2-dma-contig.c +0 -0
 	buf = kzalloc(sizeof *buf, GFP_KERNEL);
 	if (!buf)
#35 lore raw

Re: [PATCHv10 22/26] v4l: vb2-dma-contig: fail if user ptr buffer is not correctly aligned

In reply to Tomasz Stanislawski

Hi Tomasz,

Quoted 59 lines · On Friday 12 October 2012 09:44:05 Tomasz Stanislawski wrote:
Quoted 51 lines · Laurent Pinchart
Quoted 22 lines · On Wednesday 10 October 2012 16:46:41 Tomasz Stanislawski wrote:

From: Marek Szyprowski <[email protected]>

The DMA transfer must be aligned to a specific value. If userptr is not aligned to DMA requirements then unexpected corruptions of the memory may occur before or after a buffer. To prevent such situations, all unligned userptr buffers are rejected at VIDIOC_QBUF.

---

 drivers/media/v4l2-core/videobuf2-dma-contig.c |   12 ++++++++++++
 1 file changed, 12 insertions(+)
(quoted diff) +0 -0

b/drivers/media/v4l2-core/videobuf2-dma-contig.c index 2d661fd..571a919 100644

drivers/media/v4l2-core/videobuf2-dma-contig.c +0 -0
@@ -493,6 +493,18 @@ static void *vb2_dc_get_userptr(void *alloc_ctx,
 unsigned long vaddr, struct vm_area_struct *vma;
 

drivers/media/v4l2-core/videobuf2-dma-contig.c static void *vb2_dc_get_userptr(void *alloc_ctx, · line 504

Quoted 10 lines
drivers/media/v4l2-core/videobuf2-dma-contig.c +7 -0
 	struct sg_table *sgt;
 	unsigned long contig_size;
 
+	unsigned long dma_align = dma_get_cache_alignment();
+
+	/* Only cache aligned DMA transfers are reliable */
+	if (!IS_ALIGNED(vaddr | size, dma_align)) {
+		pr_debug("user data must be aligned to %lu bytes\n", dma_align);
+		return ERR_PTR(-EINVAL);
+	}

Looks good to me.

drivers/media/v4l2-core/videobuf2-dma-contig.c static void *vb2_dc_get_userptr(void *alloc_ctx, unsigned long vaddr, · line 507

Quoted 4 lines
drivers/media/v4l2-core/videobuf2-dma-contig.c +4 -0
+	if (!size) {
+		pr_debug("size is zero\n");
+		return ERR_PTR(-EINVAL);
+	}

Can this happen ? The vb2 core already has

                /* Check if the provided plane buffer is large enough */
                if (planes[plane].length < q->plane_sizes[plane]) {
                
                        ret = -EINVAL;
                        goto err;
                
                }
Quoted 2 lines

Unless queue_setup sets plane_sizes to 0 we can't reach vb2_dc_get_userptr.

Yes.. unfortunately, some drivers set plane_size to 0 at queue_setup. Especially, if REQBUFS is called before any S_FMT.
Maybe it is just a driver bug.

Quoted 7 lines

However, VB2 makes no sanity check if plane_sizes[] is zero. I was not able to find in Documentation nor code comments any explicit statement that plane_size cannot be zero.

Therefore I have to reject reject a 0-bytes-long user pointer at vb2_dc_get_userptr before creating an empty scatterlist and passing it to the DMA layer.

Wouldn't it then be better to add the sanity checks in the core ?

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
drivers/media/v4l2-core/videobuf2-dma-contig.c +0 -0
 	buf = kzalloc(sizeof *buf, GFP_KERNEL);
 	if (!buf)
Signature · 3 lines
Regards,

Laurent Pinchart
#36 lore raw

PATCH v10 23/26 v4l: vb2-dma-contig: align buffer size to PAGE_SIZE

In reply to Tomasz Stanislawski

Most operations on DMA and DMABUF framework need page aligned buffers. This fix guarantees this requirement for vb2-dma-contig buffers.

---
 drivers/media/v4l2-core/videobuf2-dma-contig.c |    3 +++
 1 file changed, 3 insertions(+)
drivers/media/v4l2-core/videobuf2-dma-contig.c +3 -0
index 571a919..002ee50 100644
@@ -162,6 +162,9 @@ static void *vb2_dc_alloc(void *alloc_ctx, unsigned long size)
 	if (!buf)
 		return ERR_PTR(-ENOMEM);
 
+	/* align image size to PAGE_SIZE */
+	size = PAGE_ALIGN(size);
+
 	buf->vaddr = dma_alloc_coherent(dev, size, &buf->dma_addr, GFP_KERNEL);
 	if (!buf->vaddr) {
 		dev_err(dev, "dma_alloc_coherent of size %ld failed\n", size);
Signature · 1 line
1.7.9.5
#37 lore raw

Re: [PATCHv10 23/26] v4l: vb2-dma-contig: align buffer size to PAGE_SIZE

Hi Tomasz,

Quoted 2 lines · On Wednesday 10 October 2012 16:46:42 Tomasz Stanislawski wrote:

Most operations on DMA and DMABUF framework need page aligned buffers.

The comment is a bit misleading, the buffer is already page-aligned (unless I'm mistaken dma_alloc_coherent() returns a page-aligned buffer) but its size isn't a multiple of the page size.

Do we really need a page size multiple ? Isn't it enough to make the size a multiple of the cache line size ?

Quoted 23 lines

This fix guarantees this requirement
for vb2-dma-contig buffers.

---
 drivers/media/v4l2-core/videobuf2-dma-contig.c |    3 +++
 1 file changed, 3 insertions(+)
(quoted diff) +0 -0

b/drivers/media/v4l2-core/videobuf2-dma-contig.c index 571a919..002ee50 100644

drivers/media/v4l2-core/videobuf2-dma-contig.c +3 -0
@@ -162,6 +162,9 @@ static void *vb2_dc_alloc(void *alloc_ctx, unsigned long
 size) if (!buf)
 		return ERR_PTR(-ENOMEM);
 
+	/* align image size to PAGE_SIZE */
+	size = PAGE_ALIGN(size);
+
 	buf->vaddr = dma_alloc_coherent(dev, size, &buf->dma_addr, GFP_KERNEL);
 	if (!buf->vaddr) {
 		dev_err(dev, "dma_alloc_coherent of size %ld failed\n", size);
Signature · 3 lines
Regards,

Laurent Pinchart
#38 lore raw

Re: [PATCHv10 23/26] v4l: vb2-dma-contig: align buffer size to PAGE_SIZE

Hi Laurent,

Quoted 9 lines · Laurent Pinchart

Hi Tomasz,

Quoted 2 lines · On Wednesday 10 October 2012 16:46:42 Tomasz Stanislawski wrote:

Most operations on DMA and DMABUF framework need page aligned buffers.

The comment is a bit misleading, the buffer is already page-aligned (unless I'm mistaken dma_alloc_coherent() returns a page-aligned buffer) but its size isn't a multiple of the page size.

Ok. I will update the commit message that only buffer size is going to be page aligned.

Quoted 4 lines

Do we really need a page size multiple ? Isn't it enough to make the size a multiple of the cache line size ?

Frankly, I strongly oppose forcing a size of a DMA buffer to be rounded up.

However, I discovered a problem while testing mmap() interface in dma-buf. The test in dma_buf_mmap() will fail if the size is not a multiple of 4k.

Maybe the value from dma-buf.c:456 should be changed from:

dmabuf->size >> PAGE_SHIFT

to

PAGE_ALIGN(dmabuf->size) >> PAGE_SHIFT

However, I preferred to avoid any changes outside of the media tree hoping that the patchset gets merged. Rounding the buffer size to a page size was quick workaround for the issue with DMABUF mmap().

Regards,
Tomasz Stanislawski

Quoted 23 lines
Quoted 23 lines

This fix guarantees this requirement
for vb2-dma-contig buffers.

---
 drivers/media/v4l2-core/videobuf2-dma-contig.c |    3 +++
 1 file changed, 3 insertions(+)
(quoted diff) +0 -0

b/drivers/media/v4l2-core/videobuf2-dma-contig.c index 571a919..002ee50 100644

drivers/media/v4l2-core/videobuf2-dma-contig.c +3 -0
@@ -162,6 +162,9 @@ static void *vb2_dc_alloc(void *alloc_ctx, unsigned long
 size) if (!buf)
 		return ERR_PTR(-ENOMEM);
 
+	/* align image size to PAGE_SIZE */
+	size = PAGE_ALIGN(size);
+
 	buf->vaddr = dma_alloc_coherent(dev, size, &buf->dma_addr, GFP_KERNEL);
 	if (!buf->vaddr) {
 		dev_err(dev, "dma_alloc_coherent of size %ld failed\n", size);
#39 lore raw

Re: [PATCHv10 23/26] v4l: vb2-dma-contig: align buffer size to PAGE_SIZE

Hi Tomasz,

Quoted 24 lines · On Friday 12 October 2012 10:24:34 Tomasz Stanislawski wrote:
Quoted 7 lines · Laurent Pinchart
Quoted 2 lines · On Wednesday 10 October 2012 16:46:42 Tomasz Stanislawski wrote:

Most operations on DMA and DMABUF framework need page aligned buffers.

The comment is a bit misleading, the buffer is already page-aligned (unless I'm mistaken dma_alloc_coherent() returns a page-aligned buffer) but its size isn't a multiple of the page size.

Ok. I will update the commit message that only buffer size is going to be page aligned.

Quoted 2 lines

Do we really need a page size multiple ? Isn't it enough to make the size a multiple of the cache line size ?

Frankly, I strongly oppose forcing a size of a DMA buffer to be rounded up.

However, I discovered a problem while testing mmap() interface in dma-buf. The test in dma_buf_mmap() will fail if the size is not a multiple of 4k.

Maybe the value from dma-buf.c:456 should be changed from:

dmabuf->size >> PAGE_SHIFT

Quoted 7 lines

to

PAGE_ALIGN(dmabuf->size) >> PAGE_SHIFT

However, I preferred to avoid any changes outside of the media tree hoping that the patchset gets merged. Rounding the buffer size to a page size was quick workaround for the issue with DMABUF mmap().

After some more thoughts I'm not sure whether this patch does the right thing. We have two sizes that we neeed to care about, the user usable buffer size and the allocated memory size.

When a user of a buffer requests buffer allocation with an explicit or implicit size we might need to allocate a larger buffer to fulfill usage requirements. We can thus end up with an allocated memory size larger than what the user requested. Such usage requirements include

- DMA and CPU access: when accessing the buffer both through DMA and directly by the CPU cache management comes into play, and the buffer address and size then need to be aligned to a cache line size boundary.

- Mapping to userspace: we can only map complete pages to userspace, the buffer address and size need to be aligned to a page size boundary to make sure that we won't leak unrelated data to userspace.

As the cache line size is smaller than a page fulfilling the second requirement always fulfills the first. There might be other requirements that escape my mind right now.

As we don't precisely know at allocation time how the buffer will be used (for instance whether it will eventually be mapped to userspace or not), we have two options:

- Align the buffer size to a page size boundary unconditionally.

- Let the user handle that requirement by specifying an allocation size aligned to a page size boundary.

Given the complexity associated with the second solution and the very small, if not inexistent, expected memory gain (when using the DMA allocation APIs we always get complete pages anyway, so there would be no gain in not aligning the allocation size to a page size boundary), I think the first solution should be preferred.

This leaves us with two questions related to buffer size: what size (between the requested size and the actually allocated size) do we report back to the user (in the v4l2_buffer length field for instance), and what size do we report to the dma-buf core (and thus to the other subsystems and other buffer users) ?

Signature · 3 lines
Regards,

Laurent Pinchart
#40 lore raw

PATCH v10 24/26 v4l: s5p-fimc: support for dmabuf exporting

In reply to Tomasz Stanislawski

This patch enhances s5p-fimc with support for DMABUF exporting via VIDIOC_EXPBUF ioctl.

---
 drivers/media/platform/s5p-fimc/fimc-capture.c |    9 +++++++++
 drivers/media/platform/s5p-fimc/fimc-m2m.c     |   10 ++++++++++
 2 files changed, 19 insertions(+)
drivers/media/platform/s5p-fimc/fimc-capture.c +9 -0
index 246bb32..e5fd159 100644
@@ -1231,6 +1231,14 @@ static int fimc_cap_qbuf(struct file *file, void *priv,
 	return vb2_qbuf(&fimc->vid_cap.vbq, buf);
 }
 
+static int fimc_cap_expbuf(struct file *file, void *priv,
+			  struct v4l2_exportbuffer *eb)
+{
+	struct fimc_dev *fimc = video_drvdata(file);
+
+	return vb2_expbuf(&fimc->vid_cap.vbq, eb);
+}
+
 static int fimc_cap_dqbuf(struct file *file, void *priv,
 			   struct v4l2_buffer *buf)
 {
@@ -1355,6 +1363,7 @@ static const struct v4l2_ioctl_ops fimc_capture_ioctl_ops = {
 
 	.vidioc_qbuf			= fimc_cap_qbuf,
 	.vidioc_dqbuf			= fimc_cap_dqbuf,
+	.vidioc_expbuf			= fimc_cap_expbuf,
 
 	.vidioc_prepare_buf		= fimc_cap_prepare_buf,
 	.vidioc_create_bufs		= fimc_cap_create_bufs,
drivers/media/platform/s5p-fimc/fimc-m2m.c +10 -0
index 17067a7..1cd4fcf 100644
@@ -439,6 +439,15 @@ static int fimc_m2m_dqbuf(struct file *file, void *fh,
 	return v4l2_m2m_dqbuf(file, ctx->m2m_ctx, buf);
 }
 
+static int fimc_m2m_expbuf(struct file *file, void *fh,
+			    struct v4l2_exportbuffer *eb)
+{
+	struct fimc_ctx *ctx = fh_to_ctx(fh);
+
+	return v4l2_m2m_expbuf(file, ctx->m2m_ctx, eb);
+}
+
+
 static int fimc_m2m_streamon(struct file *file, void *fh,
 			     enum v4l2_buf_type type)
 {
@@ -607,6 +616,7 @@ static const struct v4l2_ioctl_ops fimc_m2m_ioctl_ops = {
 	.vidioc_querybuf		= fimc_m2m_querybuf,
 	.vidioc_qbuf			= fimc_m2m_qbuf,
 	.vidioc_dqbuf			= fimc_m2m_dqbuf,
+	.vidioc_expbuf			= fimc_m2m_expbuf,
 	.vidioc_streamon		= fimc_m2m_streamon,
 	.vidioc_streamoff		= fimc_m2m_streamoff,
 	.vidioc_g_crop			= fimc_m2m_g_crop,
Signature · 1 line
1.7.9.5
#41 lore raw

PATCH v10 25/26 v4l: s5p-tv: mixer: support for dmabuf exporting

In reply to Tomasz Stanislawski

This patch enhances s5p-tv with support for DMABUF exporting via VIDIOC_EXPBUF ioctl.

---
 drivers/media/platform/s5p-tv/mixer_video.c |   10 ++++++++++
 1 file changed, 10 insertions(+)
drivers/media/platform/s5p-tv/mixer_video.c +10 -0
index 2421e527..5e3cdb2 100644
@@ -698,6 +698,15 @@ static int mxr_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
 	return vb2_dqbuf(&layer->vb_queue, p, file->f_flags & O_NONBLOCK);
 }
 
+static int mxr_expbuf(struct file *file, void *priv,
+	struct v4l2_exportbuffer *eb)
+{
+	struct mxr_layer *layer = video_drvdata(file);
+
+	mxr_dbg(layer->mdev, "%s:%d\n", __func__, __LINE__);
+	return vb2_expbuf(&layer->vb_queue, eb);
+}
+
 static int mxr_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
 {
 	struct mxr_layer *layer = video_drvdata(file);
@@ -725,6 +734,7 @@ static const struct v4l2_ioctl_ops mxr_ioctl_ops = {
 	.vidioc_querybuf = mxr_querybuf,
 	.vidioc_qbuf = mxr_qbuf,
 	.vidioc_dqbuf = mxr_dqbuf,
+	.vidioc_expbuf = mxr_expbuf,
 	/* Streaming control */
 	.vidioc_streamon = mxr_streamon,
 	.vidioc_streamoff = mxr_streamoff,
Signature · 1 line
1.7.9.5
#42 lore raw

PATCH v10 26/26 v4l: s5p-mfc: support for dmabuf exporting

In reply to Tomasz Stanislawski

This patch enhances s5p-mfc with support for DMABUF exporting via VIDIOC_EXPBUF ioctl.

---
 drivers/media/platform/s5p-mfc/s5p_mfc_dec.c |   14 ++++++++++++++
 drivers/media/platform/s5p-mfc/s5p_mfc_enc.c |   14 ++++++++++++++
 2 files changed, 28 insertions(+)
drivers/media/platform/s5p-mfc/s5p_mfc_dec.c +14 -0
index eb6a70b..6dad9a7 100644
@@ -636,6 +636,19 @@ static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
 	return -EINVAL;
 }
 
+/* Export DMA buffer */
+static int vidioc_expbuf(struct file *file, void *priv,
+	struct v4l2_exportbuffer *eb)
+{
+	struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
+
+	if (eb->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
+		return vb2_expbuf(&ctx->vq_src, eb);
+	if (eb->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
+		return vb2_expbuf(&ctx->vq_dst, eb);
+	return -EINVAL;
+}
+
 /* Stream on */
 static int vidioc_streamon(struct file *file, void *priv,
 			   enum v4l2_buf_type type)
@@ -813,6 +826,7 @@ static const struct v4l2_ioctl_ops s5p_mfc_dec_ioctl_ops = {
 	.vidioc_querybuf = vidioc_querybuf,
 	.vidioc_qbuf = vidioc_qbuf,
 	.vidioc_dqbuf = vidioc_dqbuf,
+	.vidioc_expbuf = vidioc_expbuf,
 	.vidioc_streamon = vidioc_streamon,
 	.vidioc_streamoff = vidioc_streamoff,
 	.vidioc_g_crop = vidioc_g_crop,
drivers/media/platform/s5p-mfc/s5p_mfc_enc.c +14 -0
index 2af6d52..22bf684 100644
@@ -1165,6 +1165,19 @@ static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
 	return ret;
 }
 
+/* Export DMA buffer */
+static int vidioc_expbuf(struct file *file, void *priv,
+	struct v4l2_exportbuffer *eb)
+{
+	struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
+
+	if (eb->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
+		return vb2_expbuf(&ctx->vq_src, eb);
+	if (eb->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
+		return vb2_expbuf(&ctx->vq_dst, eb);
+	return -EINVAL;
+}
+
 /* Stream on */
 static int vidioc_streamon(struct file *file, void *priv,
 			   enum v4l2_buf_type type)
@@ -1568,6 +1581,7 @@ static const struct v4l2_ioctl_ops s5p_mfc_enc_ioctl_ops = {
 	.vidioc_querybuf = vidioc_querybuf,
 	.vidioc_qbuf = vidioc_qbuf,
 	.vidioc_dqbuf = vidioc_dqbuf,
+	.vidioc_expbuf = vidioc_expbuf,
 	.vidioc_streamon = vidioc_streamon,
 	.vidioc_streamoff = vidioc_streamoff,
 	.vidioc_s_parm = vidioc_s_parm,
Signature · 1 line
1.7.9.5
#43 lore raw

[PATCH 27/26] v4l: vb2: Set data_offset to 0 for single-plane buffers

In reply to Tomasz Stanislawski

Single-planar V4L2 buffers are converted to multi-planar vb2 buffers with a single plane when queued. The plane data_offset field is not available in the single-planar API and must be set to 0 for dmabuf buffers and all output buffers.

---
 drivers/media/v4l2-core/videobuf2-core.c |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)
drivers/media/v4l2-core/videobuf2-core.c +2 -0
index b0402f2..3eae3d8 100644
@@ -931,6 +931,7 @@ static int __fill_vb2_buffer(struct vb2_buffer *vb, const struct v4l2_buffer *b,
 		 */
 		if (V4L2_TYPE_IS_OUTPUT(b->type))
 			v4l2_planes[0].bytesused = b->bytesused;
+			v4l2_planes[0].data_offset = 0;
 
 		if (b->memory == V4L2_MEMORY_USERPTR) {
 			v4l2_planes[0].m.userptr = b->m.userptr;
@@ -940,6 +941,7 @@ static int __fill_vb2_buffer(struct vb2_buffer *vb, const struct v4l2_buffer *b,
 		if (b->memory == V4L2_MEMORY_DMABUF) {
 			v4l2_planes[0].m.fd = b->m.fd;
 			v4l2_planes[0].length = b->length;
+			v4l2_planes[0].data_offset = 0;
 		}
 
 	}
Signature · 3 lines
Regards,

Laurent Pinchart
maintainer Acked-by
#44 lore raw

Re: [PATCH 27/26] v4l: vb2: Set data_offset to 0 for single-plane buffers

Hello,

Quoted 6 lines · Laurent Pinchart

Single-planar V4L2 buffers are converted to multi-planar vb2 buffers with a single plane when queued. The plane data_offset field is not available in the single-planar API and must be set to 0 for dmabuf buffers and all output buffers.

Quoted 17 lines
---
  drivers/media/v4l2-core/videobuf2-core.c |    2 ++
  1 files changed, 2 insertions(+), 0 deletions(-)
drivers/media/v4l2-core/videobuf2-core.c +1 -0
index b0402f2..3eae3d8 100644
@@ -931,6 +931,7 @@ static int __fill_vb2_buffer(struct vb2_buffer *vb, const struct v4l2_buffer *b,
  		 */
  		if (V4L2_TYPE_IS_OUTPUT(b->type))
  			v4l2_planes[0].bytesused = b->bytesused;
+			v4l2_planes[0].data_offset = 0;
 
  		if (b->memory == V4L2_MEMORY_USERPTR) {
  			v4l2_planes[0].m.userptr = b->m.userptr;

drivers/media/v4l2-core/videobuf2-core.c static int __fill_vb2_buffer(struct vb2_buffer *vb, const struct v4l2_buffer *b, · line 947

Quoted 7 lines
drivers/media/v4l2-core/videobuf2-core.c +1 -0
@@ -940,6 +941,7 @@ static int __fill_vb2_buffer(struct vb2_buffer *vb, const struct v4l2_buffer *b,
  		if (b->memory == V4L2_MEMORY_DMABUF) {
  			v4l2_planes[0].m.fd = b->m.fd;
  			v4l2_planes[0].length = b->length;
+			v4l2_planes[0].data_offset = 0;
  		}
 
  	}

Best regards

Signature · 2 lines
Marek Szyprowski
Samsung Poland R&D Center
#45 lore raw

Re: [PATCH 27/26] v4l: vb2: Set data_offset to 0 for single-plane buffers

Hello,

Quoted 9 lines · On Thursday 08 November 2012 13:13:50 Marek Szyprowski wrote:
Quoted 6 lines · Laurent Pinchart

Single-planar V4L2 buffers are converted to multi-planar vb2 buffers with a single plane when queued. The plane data_offset field is not available in the single-planar API and must be set to 0 for dmabuf buffers and all output buffers.

Thank you.

Tomasz, could you please take this in your dma-buf branch ? Sorry for forgetting to CC you on the patch.

Quoted 35 lines
Quoted 35 lines

---

  drivers/media/v4l2-core/videobuf2-core.c |    2 ++
  1 files changed, 2 insertions(+), 0 deletions(-)
(quoted diff) +0 -0

b/drivers/media/v4l2-core/videobuf2-core.c index b0402f2..3eae3d8 100644

drivers/media/v4l2-core/videobuf2-core.c +0 -0
@@ -931,6 +931,7 @@ static int __fill_vb2_buffer(struct vb2_buffer *vb,

const struct v4l2_buffer *b,>
*/

if (V4L2_TYPE_IS_OUTPUT(b->type))

  			v4l2_planes[0].bytesused = b->bytesused;
drivers/media/v4l2-core/videobuf2-core.c +1 -0
+			v4l2_planes[0].data_offset = 0;
 
  		if (b->memory == V4L2_MEMORY_USERPTR) {
 
  			v4l2_planes[0].m.userptr = b->m.userptr;
@@ -940,6 +941,7 @@ static int __fill_vb2_buffer(struct vb2_buffer *vb,

const struct v4l2_buffer *b,>
if (b->memory == V4L2_MEMORY_DMABUF) {

  			v4l2_planes[0].m.fd = b->m.fd;
  			v4l2_planes[0].length = b->length;
drivers/media/v4l2-core/videobuf2-core.c +1 -0
+			v4l2_planes[0].data_offset = 0;
 
  		}
 
  	}
Signature · 3 lines
Regards,

Laurent Pinchart
#46 lore raw

Re: [PATCH 27/26] v4l: vb2: Set data_offset to 0 for single-plane buffers

Hi Laurent,
The fix was partially applied in "[PATCHv10 03/26] v4l: vb2: add support for shared buffer (dma_buf)". The data_offset is set to 0 for DMABUF capture/output for single-planar API.

We should define the meaning of data_offset in case of USERPTR and MMAP buffers. For output device it is pretty intuitive.

For DMABUF capture devices data_offset maybe used to inform a driver to capture the image at some offset inside the DMABUF buffer.

BTW. Should {} be added after "if (V4L2_TYPE_IS_OUTPUT(b->type))" to avoid 'interesting' behavior? :)

Regards,
Tomasz Stanislawski

#47 lore raw

Re: [PATCH 27/26] v4l: vb2: Set data_offset to 0 for single-plane buffers

Hi Tomasz,

Quoted 4 lines · On Thursday 08 November 2012 16:28:12 Tomasz Stanislawski wrote:

Hi Laurent,
The fix was partially applied in "[PATCHv10 03/26] v4l: vb2: add support for shared buffer (dma_buf)". The data_offset is set to 0 for DMABUF capture/output for single-planar API.

My bad, I had an old version of your patch set in my git tree. The other half of the patch should still be applied, but it can go through Mauro's tree without going through yours.

Quoted 5 lines

We should define the meaning of data_offset in case of USERPTR and MMAP buffers. For output device it is pretty intuitive.

For DMABUF capture devices data_offset maybe used to inform a driver to capture the image at some offset inside the DMABUF buffer.

That's indeed missing from the documentation.

Quoted 2 lines

BTW. Should {} be added after "if (V4L2_TYPE_IS_OUTPUT(b->type))" to avoid 'interesting' behavior? :)

Oops :-) I'll fix that.

Signature · 3 lines
Regards,

Laurent Pinchart