vb2: Handle user cache hints, allow drivers to choose cache coherency
Subsystems (4)
v3 cover vb2: Handle user cache hints, allow drivers to choose cache coherency
Hello,
This is a rebased and partially reworked version of the vb2 cache hints support patch series posted by Laurent more three months ago. The patches have been modified as needed by the upstream changes. There are a number of other changes as well. Since then, patch ccc66e73 ("ARM: 8508/2:
videobuf2-dc: Let drivers specify DMA attrs") that supposedly added DMA attribute support for videobuf2-dma-contig has been merged. For dma-contig memory type, this patchset addresses issues in that patch instead of providing support for the feature from scratch. It also adds support for USERPTR buffer type and makes necessary changes to dma-sg memory type as well. Details below.
I'm still posting this patchset as RFC as the dma-sg patch hasn't been tested apart from compiling it. Otherwise the subject line would have PATCH instead of RFC. Testing especially on a device using dma-sg would be beneficial.
The videobuf2 memory managers use the DMA mapping API to handle cache synchronization on systems that require them transparently for drivers. As cache operations are expensive, system performances can be impacted. Cache synchronization can't be skipped altogether if we want to retain correct behaviour, but optimizations are possible in cases related to buffer sharing between multiple devices without CPU access to the memory.
The first optimization covers cases where the memory never needs to be accessed by the CPU (neither in kernelspace nor in userspace). In those cases, as no CPU memory mappings exist, cache synchronization can be skipped. The situation could be detected in the kernel as we have enough information to determine whether CPU mappings for kernelspace or userspace exist (in the first case because drivers should request them explicitly, in the second case because the mmap() handler hasn't been invoked). This optimization is not implemented currently but should at least be prototyped as it could improve performances automatically in a large number of cases.
The second class of optimizations cover cases where the memory sometimes needs to be accessed by the CPU. In those cases memory mapping must be created and cache handled, but cache synchronization could be skipped for buffer that are not touched by the CPU.
By default the following cache synchronization operations need to be performed related to the buffer management ioctls. For simplicity means of QBUF below apply to buf VIDIOC_QBUF and VIDIOC_PREPARE_BUF.
| QBUF | DQBUF ---------------------------------------- CAPTURE | Invalidate | Invalidate (*) OUTPUT | Clean | -
(*) for systems using speculative pre-fetching only
The following cases can be optimized.
1. CAPTURE, the CPU has not written to the buffer before QBUF
Cache invalidation can be skipped at QBUF time, but becomes required at DQBUF time on all systems, regardless of whether they use speculative prefetching.
2. CAPTURE, the CPU will not read from the buffer after DQBUF
Cache invalidation can be skipped at DQBUF time.
3. CAPTURE, combination of (1) and (2)
Cache invalidation can be skipped at both QBUF and DQBUF time.
4. OUTPUT, the CPU has not written to the buffer before QBUF
Cache clean can be skipped at QBUF time.
The kernel can't detect thoses situations automatically and thus requires hints from userspace to decide whether cache synchronization can be skipped. It should be noted that those hints might not be honoured. In particular, if userspace hints that it hasn't touched the buffer with the CPU, drivers might need to perform memory accesses themselves (adding JPEG or MPEG headers to buffers is a common case where CPU access could be needed in the kernel), in which case the userspace hints will be ignored.
Getting the hints wrong will result in data corruption. Userspace applications are allowed to shoot themselves in the foot, but driver are responsible for deciding whether data corruption can pose a risk to the system in general. For instance if the device could be made to crash, or behave in a way that would jeopardize system security, reliability or performances, when fed with invalid data, cache synchronization shall not be skipped solely due to possibly incorrect userspace hints.
The V4L2 API defines two flags, V4L2_BUF_FLAG_NO_CACHE_INVALIDATE and V4L2_BUF_FLAG_NO_CACHE_SYNC, that can be used to provide cache-related hints to the kernel. However, no kernel has ever implemented support for those flags that are thus most likely unused.
A single flag is enough to cover all the optimization cases described above, provided we keep track of the flag being set at QBUF time to force cache invalidation at DQBUF time for case (1) if the flag isn't set at DQBUF time. This patch series thus cleans up the userspace API and merges both flags into a single one.
One potential issue with case (1) is that cache invalidation at DQBUF time for CAPTURE buffers isn't fully under the control of videobuf2. We can instruct the DMA mapping API to skip cache handling, but we can't force it to invalidate the cache in the sync_for_cpu operation for non speculative prefetching systems. Luckily, on ARM32 the current implementation always invalidates the cache in __dma_page_dev_to_cpu() for CAPTURE buffers so we are safe fot now. However, this is documented by a FIXME comment that might lead to someone fixing the implementation in the future. I believe we will have to the problem at the DMA mapping level, the userspace hint API shouldn't be affected.
This RFC patch set achieves two main objectives:
1. Respect cache flags passed from the user space. As no driver nor videobuf2 has (ever?) implemented them, the two flags are replaced by a single one (V4L2_BUF_FLAG_NO_CACHE_SYNC) and the two old flags are deprecated. This is done since a single flag provides the driver with enough information on what to do.
2. Allow a driver using videobuf2 to choose whether it prefers coherent or non-coherent CPU access to buffer memory for MMAP and USERPTR buffers. This could be later extended to be specified by the user, and per buffer if needed. (Patches 06/11 and 11/11).
Note should be taken that DMABUF imported buffers have their cache synchronization handled by the exported through the dma_buf_map_attachment() and dma_buf_unmap_attachment() functions, and dma-buf lacks an API to perform memory synchronization without unmapping and remapping the buffers. This is not a blocker as far as this patch series is concerned, but importing buffers (usually exported by the CPU) is such an important use case that we can't considered the cache optimization problem anywhere close to being solved if we don't address this case.
changes since RFC v2:
- Nicer looking tests for the need for syncing.
- Also set DMA attributes for USERPTR buffers.
- Unconditionally assign buf->attrs for MMAP buffers.
- Don't call vb2_dc_get_base_sgt() until buf->dev is set.
- Provide {begin,end}_cpu_access() dmabuf ops for cache management.
- Make similar changes to dma-sg memops to support DMA attributes.
Sakari Ailus (13):
vb2: Rename confusingly named internal buffer preparation functions
vb2: Move buffer cache synchronisation to prepare from queue
vb2: Move cache synchronisation from buffer done to dqbuf handler
v4l: Unify cache management hint buffer flags
vb2: Anticipate queue specific DMA attributes for USERPTR buffers
vb2: dma-contig: Assign DMA attrs for a buffer unconditionally
vb2: dma-contig: Remove redundant sgt_base field
vb2: dma-contig: Don't warn on failure in obtaining scatterlist
vb2: dma-contig: Move vb2_dc_get_base_sgt() up
vb2: dma-contig: Fix DMA attribute and cache management
vb2: dma-contig: Add WARN_ON_ONCE() to check for potential bugs
vb2: dma-sg: Let drivers decide DMA attrs of MMAP and USERPTR bufs
vb2: Improve struct vb2_mem_ops documentation; alloc and put are for
MMAP
Samu Onkalo (1):
vb2: Don't sync cache for a buffer if so requested
Documentation/media/uapi/v4l/buffer.rst | 24 ++-- .../media/uapi/v4l/vidioc-prepare-buf.rst | 5 +- drivers/media/v4l2-core/videobuf2-core.c | 129 ++++++++++++++------- drivers/media/v4l2-core/videobuf2-dma-contig.c | 120 ++++++++++++------- drivers/media/v4l2-core/videobuf2-dma-sg.c | 47 ++++++-- drivers/media/v4l2-core/videobuf2-v4l2.c | 14 ++- drivers/media/v4l2-core/videobuf2-vmalloc.c | 3 +- include/media/videobuf2-core.h | 46 +++++--- include/trace/events/v4l2.h | 3 +- include/uapi/linux/videodev2.h | 7 +- 10 files changed, 263 insertions(+), 135 deletions(-)
Signature · 2 lines
Regards, Sakari
RFC v3 1/14 vb2: Rename confusingly named internal buffer preparation functions
Rename __qbuf_*() functions which are specific to a buffer type as __prepare_*() which matches with what they do. The naming was there for historical reasons; the purpose of the functions was changed without renaming them.
--- drivers/media/v4l2-core/videobuf2-core.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-)
drivers/media/v4l2-core/videobuf2-core.c +9 -9
@@ -956,9 +956,9 @@ void vb2_discard_done(struct vb2_queue *q)EXPORT_SYMBOL_GPL (vb2_discard_done);/** -* __qbuf_mmap() - handle qbuf of an MMAP buffer +* __prepare_mmap() - prepare an MMAP buffer */ -static int __qbuf_mmap (struct vb2_buffer *vb,const void *pb) +static int __prepare_mmap (struct vb2_buffer *vb,const void *pb) {int ret =0 ;
@@ -969,9 +969,9 @@ static int __qbuf_mmap(struct vb2_buffer *vb, const void *pb) }/** -* __qbuf_userptr() - handle qbuf of a USERPTR buffer +* __prepare_userptr() - prepare a USERPTR buffer */ -static int __qbuf_userptr (struct vb2_buffer *vb,const void *pb) +static int __prepare_userptr (struct vb2_buffer *vb,const void *pb) {struct vb2_plane planes[VB2_MAX_PLANES ];struct vb2_queue *q = vb->vb2_queue;
@@ -1087,9 +1087,9 @@ static int __qbuf_userptr(struct vb2_buffer *vb, const void *pb) }/** -* __qbuf_dmabuf() - handle qbuf of a DMABUF buffer +* __prepare_dmabuf() - prepare a DMABUF buffer */ -static int __qbuf_dmabuf (struct vb2_buffer *vb,const void *pb) +static int __prepare_dmabuf (struct vb2_buffer *vb,const void *pb) {struct vb2_plane planes[VB2_MAX_PLANES ];struct vb2_queue *q = vb->vb2_queue;
@@ -1255,13 +1255,13 @@ static int __buf_prepare(struct vb2_buffer *vb, const void *pb)switch (q->memory) {case VB2_MEMORY_MMAP : - ret =__qbuf_mmap (vb, pb); + ret =__prepare_mmap (vb, pb);break ;case VB2_MEMORY_USERPTR : - ret =__qbuf_userptr (vb, pb); + ret =__prepare_userptr (vb, pb);break ;case VB2_MEMORY_DMABUF : - ret =__qbuf_dmabuf (vb, pb); + ret =__prepare_dmabuf (vb, pb);break ;default :WARN (1 ,"Invalid queue type\n" );
Signature · 1 line
2.7.4
RFC v3 2/14 vb2: Move buffer cache synchronisation to prepare from queue
In reply to Sakari AilusThe buffer cache should be synchronised in buffer preparation, not when the buffer is queued to the device. Fix this.
Mmap buffers do not need cache synchronisation since they are always coherent.
--- drivers/media/v4l2-core/videobuf2-core.c | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-)
drivers/media/v4l2-core/videobuf2-core.c +12 -8
@@ -1227,23 +1227,19 @@ static int __prepare_dmabuf(struct vb2_buffer *vb, const void *pb)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->owned_by_drv_count);trace_vb2_buf_queue (q, vb); -/* sync buffers */ -for (plane =0 ; plane < vb->num_planes; ++plane) -call_void_memop (vb, prepare, vb->planes[plane].mem_priv); -call_void_vb_qop (vb, buf_queue, vb); }static int __buf_prepare (struct vb2_buffer *vb,const void *pb) {struct vb2_queue *q = vb->vb2_queue; +unsigned int plane;int ret;if (q->error) {
@@ -1268,11 +1264,19 @@ static int __buf_prepare(struct vb2_buffer *vb, const void *pb) ret = -EINVAL ; } -if (ret) +if (ret) {dprintk (1 ,"buffer preparation failed: %d\n" , ret); - vb->state = ret ?VB2_BUF_STATE_DEQUEUED :VB2_BUF_STATE_PREPARED ; + vb->state =VB2_BUF_STATE_DEQUEUED ; +return ret; + } -return ret; +/* sync buffers */ +for (plane =0 ; plane < vb->num_planes; ++plane) +call_void_memop (vb, prepare, vb->planes[plane].mem_priv); + + vb->state =VB2_BUF_STATE_PREPARED ; + +return 0 ; }int vb2_core_prepare_buf (struct vb2_queue *q,unsigned int index,void *pb)
Signature · 1 line
2.7.4
RFC v3 3/14 vb2: Move cache synchronisation from buffer done to dqbuf handler
In reply to Sakari AilusThe cache synchronisation may be a time consuming operation and thus not best performed in an interrupt which is a typical context for vb2_buffer_done() calls. This may consume up to tens of ms on some machines, depending on the buffer size.
--- drivers/media/v4l2-core/videobuf2-core.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-)
drivers/media/v4l2-core/videobuf2-core.c +4 -5
@@ -889,7 +889,6 @@ 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 (WARN_ON (vb->state !=VB2_BUF_STATE_ACTIVE ))return ;
@@ -910,10 +909,6 @@ void vb2_buffer_done(struct vb2_buffer *vb, enum vb2_buffer_state state)dprintk (4 ,"done processing on buffer %d, state: %d\n" , vb->index, state); -/* sync buffers */ -for (plane =0 ; plane < vb->num_planes; ++plane) -call_void_memop (vb, finish, vb->planes[plane].mem_priv); -spin_lock_irqsave (&q->done_lock, flags);if (state ==VB2_BUF_STATE_QUEUED || state ==VB2_BUF_STATE_REQUEUEING ) {
@@ -1573,6 +1568,10 @@ static void __vb2_dqbuf(struct vb2_buffer *vb) vb->state =VB2_BUF_STATE_DEQUEUED ; +/* sync buffers */ +for (i =0 ; i < vb->num_planes; ++i) +call_void_memop (vb, finish, vb->planes[i].mem_priv); +/* unmap DMABUF buffer */ if (q->memory ==VB2_MEMORY_DMABUF )for (i =0 ; i < vb->num_planes; ++i) {
Signature · 1 line
2.7.4
RFC v3 4/14 v4l: Unify cache management hint buffer flags
In reply to Sakari AilusThe V4L2_BUF_FLAG_NO_CACHE_INVALIDATE and V4L2_BUF_FLAG_NO_CACHE_CLEAN buffer flags are currently not used by the kernel. Replace the definitions by a single V4L2_BUF_FLAG_NO_CACHE_SYNC flag to be used by further patches.
Different cache architectures should not be visible to the user space which can make no meaningful use of the differences anyway. In case a device can make use of non-coherent memory accesses, the necessary cache operations depend on the CPU architecture and the buffer type, not the requests of the user. The cache operation itself may be skipped on the user's request which was the purpose of the two flags.
On ARM the invalidate and clean are separate operations whereas on x86(-64) the two are a single operation (flush). Whether the hardware uses the buffer for reading (V4L2_BUF_TYPE_*_OUTPUT*) or writing (V4L2_BUF_TYPE_*CAPTURE*) already defines the required cache operation (clean and invalidate, respectively). No user input is required.
--- Documentation/media/uapi/v4l/buffer.rst | 24 ++++++++-------------- .../media/uapi/v4l/vidioc-prepare-buf.rst | 5 ++--- include/trace/events/v4l2.h | 3 +-- include/uapi/linux/videodev2.h | 7 +++++-- 4 files changed, 17 insertions(+), 22 deletions(-)
Documentation/media/uapi/v4l/buffer.rst +9 -15
@@ -437,23 +437,17 @@ Buffer Flags :ref:`VIDIOC_PREPARE_BUF <VIDIOC_QBUF>`, :ref:`VIDIOC_QBUF` or :ref:`VIDIOC_DQBUF <VIDIOC_QBUF>` ioctl is called. - * .. _`V4L2-BUF-FLAG-NO-CACHE-INVALIDATE`: + * .. _`V4L2-BUF-FLAG-NO-CACHE-SYNC`: - - ``V4L2_BUF_FLAG_NO_CACHE_INVALIDATE`` + - ``V4L2_BUF_FLAG_NO_CACHE_SYNC`` - 0x00000800 - - Caches do not have to be invalidated for this buffer. Typically - applications shall use this flag if the data captured in the - buffer is not going to be touched by the CPU, instead the buffer - will, probably, be passed on to a DMA-capable hardware unit for - further processing or output. - * .. _`V4L2-BUF-FLAG-NO-CACHE-CLEAN`: - - - ``V4L2_BUF_FLAG_NO_CACHE_CLEAN`` - - 0x00001000 - - Caches do not have to be cleaned for this buffer. Typically - applications shall use this flag for output buffers if the data in - this buffer has not been created by the CPU but by some - DMA-capable unit, in which case caches have not been used. + - Do not perform CPU cache synchronisation operations when the buffer is + queued or dequeued. The user is responsible for the correct use of + this flag. It should be only used when the buffer is not accessed + using the CPU, e.g. the buffer is written to by a hardware block and + then read by another one, in which case the flag should be set in both + :ref:`VIDIOC_QBUF` and :ref:`VIDIOC_DQBUF` ioctls. The flag has no + effect on some devices / architectures. * .. _`V4L2-BUF-FLAG-LAST`: - ``V4L2_BUF_FLAG_LAST``
Documentation/media/uapi/v4l/vidioc-prepare-buf.rst +2 -3
@@ -36,9 +36,8 @@ pass ownership of the buffer to the driver before actually enqueuing it, using the :ref:`VIDIOC_QBUF` ioctl, and to prepare it for future I/O. Such preparations may include cache invalidation or cleaning. Performing them in advance saves time during the actual I/O. In case such cache -operations are not required, the application can use one of -``V4L2_BUF_FLAG_NO_CACHE_INVALIDATE`` and -``V4L2_BUF_FLAG_NO_CACHE_CLEAN`` flags to skip the respective step. +operations are not required, the application can use the +``V4L2_BUF_FLAG_NO_CACHE_SYNC`` flag to skip the cache synchronization step. The struct :c:type:`v4l2_buffer` structure is specified in :ref:`buffer`.
include/trace/events/v4l2.h +1 -2
@@ -80,8 +80,7 @@ SHOW_FIELD { V4L2_BUF_FLAG_ERROR, "ERROR" }, \ { V4L2_BUF_FLAG_TIMECODE, "TIMECODE" }, \ { V4L2_BUF_FLAG_PREPARED, "PREPARED" }, \ - { V4L2_BUF_FLAG_NO_CACHE_INVALIDATE, "NO_CACHE_INVALIDATE" }, \ - { V4L2_BUF_FLAG_NO_CACHE_CLEAN, "NO_CACHE_CLEAN" }, \ + { V4L2_BUF_FLAG_NO_CACHE_SYNC, "NO_CACHE_SYNC" }, \ { V4L2_BUF_FLAG_TIMESTAMP_MASK, "TIMESTAMP_MASK" }, \ { V4L2_BUF_FLAG_TIMESTAMP_UNKNOWN, "TIMESTAMP_UNKNOWN" }, \ { V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC, "TIMESTAMP_MONOTONIC" }, \
include/uapi/linux/videodev2.h +5 -2
@@ -934,8 +934,11 @@ struct v4l2_buffer {#define V4L2_BUF_FLAG_TIMECODE 0x00000100/* Buffer is prepared for queuing */ #define V4L2_BUF_FLAG_PREPARED 0x00000400 -/* Cache handling flags */ -#define V4L2_BUF_FLAG_NO_CACHE_INVALIDATE 0x00000800 +/* Cache sync hint */ +#define V4L2_BUF_FLAG_NO_CACHE_SYNC 0x00000800 +/* DEPRECATED. THIS WILL BE REMOVED IN THE FUTURE! */ +#define V4L2_BUF_FLAG_NO_CACHE_INVALIDATE V4L2_BUF_FLAG_NO_CACHE_SYNC +/* DEPRECATED. THIS WILL BE REMOVED IN THE FUTURE! */ #define V4L2_BUF_FLAG_NO_CACHE_CLEAN 0x00001000/* Timestamp type */ #define V4L2_BUF_FLAG_TIMESTAMP_MASK 0x0000e000
Signature · 1 line
2.7.4
RFC v3 5/14 vb2: Anticipate queue specific DMA attributes for USERPTR buffers
In reply to Sakari AilusThe DMA attributes were available for the memop implementation for MMAP buffers but not for USERPTR buffers. Do the same for USERPTR. This patch makes no functional changes.
--- drivers/media/v4l2-core/videobuf2-dma-contig.c | 3 ++- drivers/media/v4l2-core/videobuf2-dma-sg.c | 3 ++- drivers/media/v4l2-core/videobuf2-vmalloc.c | 3 ++- include/media/videobuf2-core.h | 3 ++- 4 files changed, 8 insertions(+), 4 deletions(-)
drivers/media/v4l2-core/videobuf2-dma-contig.c +2 -1
@@ -475,7 +475,8 @@ static inline dma_addr_t vb2_dc_pfn_to_dma(struct device *dev, unsigned long pfn#endif static void *vb2_dc_get_userptr (struct device *dev,unsigned long vaddr, -unsigned long size,enum dma_data_direction dma_dir) +unsigned long size,enum dma_data_direction dma_dir, +unsigned long attrs) {struct vb2_dc_buf *buf;struct frame_vector *vec;
drivers/media/v4l2-core/videobuf2-dma-sg.c +2 -1
@@ -220,7 +220,8 @@ static void vb2_dma_sg_finish(void *buf_priv)static void *vb2_dma_sg_get_userptr (struct device *dev,unsigned long vaddr,unsigned long size, -enum dma_data_direction dma_dir) +enum dma_data_direction dma_dir, +unsigned long dma_attrs) {struct vb2_dma_sg_buf *buf;struct sg_table *sgt;
drivers/media/v4l2-core/videobuf2-vmalloc.c +2 -1
@@ -73,7 +73,8 @@ static void vb2_vmalloc_put(void *buf_priv)static void *vb2_vmalloc_get_userptr (struct device *dev,unsigned long vaddr,unsigned long size, -enum dma_data_direction dma_dir) +enum dma_data_direction dma_dir, +unsigned long dma_attrs) {struct vb2_vmalloc_buf *buf;struct frame_vector *vec;
include/media/videobuf2-core.h +2 -1
@@ -122,7 +122,8 @@ struct vb2_mem_ops {void *(*get_userptr)(struct device *dev,unsigned long vaddr,unsigned long size, -enum dma_data_direction dma_dir); +enum dma_data_direction dma_dir, +unsigned long dma_attrs);void (*put_userptr)(void *buf_priv);void (*prepare)(void *buf_priv);
Signature · 1 line
2.7.4
RFC v3 6/14 vb2: dma-contig: Assign DMA attrs for a buffer unconditionally
In reply to Sakari Ailusattrs used to be a pointer and the caller of vb2_dc_alloc() could optionally provide it, or NULL. This was when struct dma_attrs was used to describe DMA attributes rather than an unsigned long value. There is no longer a need to maintain the condition, assign the value unconditionally. There is no functional difference because the memory was initialised to zero anyway.
--- drivers/media/v4l2-core/videobuf2-dma-contig.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/media/v4l2-core/videobuf2-dma-contig.c +1 -2
@@ -149,8 +149,7 @@ static void *vb2_dc_alloc(struct device *dev, unsigned long attrs,if (!buf)return ERR_PTR (-ENOMEM ); -if (attrs) - buf->attrs = attrs; + buf->attrs = attrs; buf->cookie =dma_alloc_attrs (dev, size, &buf->dma_addr,GFP_KERNEL | gfp_flags, buf->attrs);if (!buf->cookie) {
Signature · 1 line
2.7.4
RFC v3 7/14 vb2: dma-contig: Remove redundant sgt_base field
In reply to Sakari AilusThe struct vb2_dc_buf contains two struct sg_table fields: sgt_base and dma_sgt. The former is used by DMA-BUF buffers whereas the latter is used by USERPTR.
Unify the two, leaving dma_sgt.
MMAP buffers do not need cache flushing since they have been allocated using dma_alloc_coherent().
--- drivers/media/v4l2-core/videobuf2-dma-contig.c | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-)
drivers/media/v4l2-core/videobuf2-dma-contig.c +13 -12
@@ -31,12 +31,13 @@ struct vb2_dc_buf {unsigned long attrs;enum dma_data_direction dma_dir;struct sg_table *dma_sgt; -struct frame_vector *vec;/* MMAP related */ struct vb2_vmarea_handler handler;refcount_t refcount; -struct sg_table *sgt_base; + +/* USERPTR related */ +struct frame_vector *vec;/* DMABUF related */ struct dma_buf_attachment *db_attach;
@@ -96,7 +97,7 @@ static void vb2_dc_prepare(void *buf_priv)struct sg_table *sgt = buf->dma_sgt;/* DMABUF exporter will flush the cache for us */ -if (!sgt || buf->db_attach) +if (!buf->vec)return ; dma_sync_sg_for_device(buf->dev, sgt->sgl, sgt->orig_nents,
@@ -109,7 +110,7 @@ static void vb2_dc_finish(void *buf_priv)struct sg_table *sgt = buf->dma_sgt;/* DMABUF exporter will flush the cache for us */ -if (!sgt || buf->db_attach) +if (!buf->vec)return ;dma_sync_sg_for_cpu (buf->dev, sgt->sgl, sgt->orig_nents, buf->dma_dir);
@@ -126,9 +127,9 @@ static void vb2_dc_put(void *buf_priv)if (!refcount_dec_and_test (&buf->refcount))return ; -if (buf->sgt_base) { -sg_free_table (buf->sgt_base); -kfree (buf->sgt_base); +if (buf->dma_sgt) { +sg_free_table (buf->dma_sgt); +kfree (buf->dma_sgt); }dma_free_attrs (buf->dev, buf->size, buf->cookie, buf->dma_addr, buf->attrs);
@@ -239,13 +240,13 @@ static int vb2_dc_dmabuf_ops_attach(struct dma_buf *dbuf, struct device *dev,/* 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 ); + ret =sg_alloc_table (sgt, buf->dma_sgt->orig_nents,GFP_KERNEL );if (ret) {kfree (attach);return -ENOMEM ; } - rd = buf->sgt_base->sgl; + rd = buf->dma_sgt->sgl; wr = sgt->sgl;for (i =0 ; i < sgt->orig_nents; ++i) {sg_set_page (wr,sg_page (rd), rd->length, rd->offset);
@@ -396,10 +397,10 @@ static struct dma_buf *vb2_dc_get_dmabuf(void *buf_priv, unsigned long flags) exp_info.flags = flags; exp_info.priv = buf; -if (!buf->sgt_base) - buf->sgt_base =vb2_dc_get_base_sgt (buf); +if (!buf->dma_sgt) + buf->dma_sgt =vb2_dc_get_base_sgt (buf); -if (WARN_ON (!buf->sgt_base)) +if (WARN_ON (!buf->dma_sgt))return NULL ; dbuf =dma_buf_export (&exp_info);
Signature · 1 line
2.7.4
RFC v3 8/14 vb2: dma-contig: Don't warn on failure in obtaining scatterlist
In reply to Sakari Ailusvb2_dc_get_base_sgt() which obtains the scatterlist already prints information on why the scatterlist could not be obtained.
Also, remove the useless warning of a failed kmalloc().
--- drivers/media/v4l2-core/videobuf2-dma-contig.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)
drivers/media/v4l2-core/videobuf2-dma-contig.c +2 -4
@@ -370,10 +370,8 @@ static struct sg_table *vb2_dc_get_base_sgt(struct vb2_dc_buf *buf)struct sg_table *sgt; sgt =kmalloc (sizeof (*sgt),GFP_KERNEL ); -if (!sgt) { -dev_err (buf->dev,"failed to alloc sg table\n" ); +if (!sgt)return NULL ; - } ret =dma_get_sgtable_attrs (buf->dev, sgt, buf->cookie, buf->dma_addr, buf->size, buf->attrs);
@@ -400,7 +398,7 @@ static struct dma_buf *vb2_dc_get_dmabuf(void *buf_priv, unsigned long flags)if (!buf->dma_sgt) buf->dma_sgt =vb2_dc_get_base_sgt (buf); -if (WARN_ON (!buf->dma_sgt)) +if (!buf->dma_sgt)return NULL ; dbuf =dma_buf_export (&exp_info);
Signature · 1 line
2.7.4
RFC v3 9/14 vb2: dma-contig: Move vb2_dc_get_base_sgt() up
In reply to Sakari AilusJust move the function up. It'll be soon needed earlier than previously.
--- drivers/media/v4l2-core/videobuf2-dma-contig.c | 40 +++++++++++++------------- 1 file changed, 20 insertions(+), 20 deletions(-)
drivers/media/v4l2-core/videobuf2-dma-contig.c +20 -20
@@ -63,6 +63,26 @@ static unsigned long vb2_dc_get_contiguous_size(struct sg_table *sgt)return size; } +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) +return NULL ; + + ret =dma_get_sgtable_attrs (buf->dev, sgt, buf->cookie, buf->dma_addr, + buf->size, buf->attrs); +if (ret <0 ) { +dev_err (buf->dev,"failed to get scatterlist from DMA API\n" ); +kfree (sgt); +return NULL ; + } + +return sgt; +} +/*********************************************/ /* callbacks for all buffers */ /*********************************************/
@@ -364,26 +384,6 @@ static struct dma_buf_ops vb2_dc_dmabuf_ops = { .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) -return NULL ; - - ret =dma_get_sgtable_attrs (buf->dev, sgt, buf->cookie, buf->dma_addr, - buf->size, buf->attrs); -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,unsigned long flags) {struct vb2_dc_buf *buf = buf_priv;
Signature · 1 line
2.7.4
RFC v3 10/14 vb2: dma-contig: Fix DMA attribute and cache management
In reply to Sakari AilusPatch ccc66e73 ("ARM: 8508/2: videobuf2-dc: Let drivers specify DMA attrs") added support for driver specific DMA attributes to videobuf2-dma-contig but it had several issues in it.
In particular,
- cache operations were only performed on USERPTR buffers,
- DMA attributes were set only for MMAP buffers and
- it did not provide begin_cpu_access() and end_cpu_access() dma_buf_ops callbacks for cache syncronisation on exported MMAP buffers.
This patch corrects these issues.
Also arrange the header files alphabetically.
--- drivers/media/v4l2-core/videobuf2-dma-contig.c | 90 ++++++++++++++++++++------ 1 file changed, 69 insertions(+), 21 deletions(-)
drivers/media/v4l2-core/videobuf2-dma-contig.c +69 -21
@@ -11,12 +11,12 @@*/ #include <linux/dma-buf.h> +#include <linux/dma-mapping.h> #include <linux/module.h> #include <linux/refcount.h> #include <linux/scatterlist.h> #include <linux/sched.h> #include <linux/slab.h> -#include <linux/dma-mapping.h> #include <media/videobuf2-v4l2.h> #include <media/videobuf2-dma-contig.h>
@@ -116,12 +116,13 @@ static void vb2_dc_prepare(void *buf_priv)struct vb2_dc_buf *buf = buf_priv;struct sg_table *sgt = buf->dma_sgt; -/* DMABUF exporter will flush the cache for us */ -if (!buf->vec) -return ; - -dma_sync_sg_for_device (buf->dev, sgt->sgl, sgt->orig_nents, - buf->dma_dir); +/* +* DMABUF exporter will flush the cache for us; only USERPTR +* and MMAP buffers with non-coherent memory will be flushed. +*/ +if (buf->attrs &DMA_ATTR_NON_CONSISTENT ) +dma_sync_sg_for_device (buf->dev, sgt->sgl, sgt->orig_nents, + buf->dma_dir); }static void vb2_dc_finish (void *buf_priv)
@@ -129,11 +130,13 @@ static void vb2_dc_finish(void *buf_priv)struct vb2_dc_buf *buf = buf_priv;struct sg_table *sgt = buf->dma_sgt; -/* DMABUF exporter will flush the cache for us */ -if (!buf->vec) -return ; - -dma_sync_sg_for_cpu (buf->dev, sgt->sgl, sgt->orig_nents, buf->dma_dir); +/* +* DMABUF exporter will flush the cache for us; only USERPTR +* and MMAP buffers with non-coherent memory will be flushed. +*/ +if (buf->attrs &DMA_ATTR_NON_CONSISTENT ) +dma_sync_sg_for_cpu (buf->dev, sgt->sgl, sgt->orig_nents, + buf->dma_dir); }/*********************************************/
@@ -172,9 +175,9 @@ static void *vb2_dc_alloc(struct device *dev, unsigned long attrs, buf->attrs = attrs; buf->cookie =dma_alloc_attrs (dev, size, &buf->dma_addr, -GFP_KERNEL | gfp_flags, buf->attrs); +GFP_KERNEL | gfp_flags, buf->attrs);if (!buf->cookie) { -dev_err (dev,"dma_alloc_coherent of size %ld failed\n" , size); +dev_err (dev,"dma_alloc_attrs of size %ld failed\n" , size);kfree (buf);return ERR_PTR (-ENOMEM ); }
@@ -187,6 +190,14 @@ static void *vb2_dc_alloc(struct device *dev, unsigned long attrs, buf->size = size; buf->dma_dir = dma_dir; + buf->dma_sgt =vb2_dc_get_base_sgt (buf); +if (!buf->dma_sgt) { +dma_free_attrs (dev, size, buf->cookie, buf->dma_addr, + buf->attrs); +put_device (dev); +return ERR_PTR (-ENOMEM ); + } + buf->handler.refcount = &buf->refcount; buf->handler.put = vb2_dc_put; buf->handler.arg = buf;
@@ -359,6 +370,40 @@ static void *vb2_dc_dmabuf_ops_kmap(struct dma_buf *dbuf, unsigned long pgnum)return buf->vaddr ? buf->vaddr + pgnum *PAGE_SIZE :NULL ; } +static int vb2_dc_dmabuf_ops_begin_cpu_access (struct dma_buf *dbuf, +enum dma_data_direction direction) +{ +struct vb2_dc_buf *buf = dbuf->priv; +struct sg_table *sgt = buf->dma_sgt; + +/* +* DMABUF exporter will flush the cache for us; only USERPTR +* and MMAP buffers with non-coherent memory will be flushed. +*/ +if (buf->attrs &DMA_ATTR_NON_CONSISTENT ) +dma_sync_sg_for_cpu (buf->dev, sgt->sgl, sgt->nents, + buf->dma_dir); + +return 0 ; +} + +static int vb2_dc_dmabuf_ops_end_cpu_access (struct dma_buf *dbuf, +enum dma_data_direction direction) +{ +struct vb2_dc_buf *buf = dbuf->priv; +struct sg_table *sgt = buf->dma_sgt; + +/* +* DMABUF exporter will flush the cache for us; only USERPTR +* and MMAP buffers with non-coherent memory will be flushed. +*/ +if (buf->attrs &DMA_ATTR_NON_CONSISTENT ) +dma_sync_sg_for_device (buf->dev, sgt->sgl, sgt->nents, + buf->dma_dir); + +return 0 ; +} +static void *vb2_dc_dmabuf_ops_vmap (struct dma_buf *dbuf) {struct vb2_dc_buf *buf = dbuf->priv;
@@ -379,6 +424,8 @@ static struct dma_buf_ops vb2_dc_dmabuf_ops = { .unmap_dma_buf = vb2_dc_dmabuf_ops_unmap, .kmap = vb2_dc_dmabuf_ops_kmap, .kmap_atomic = vb2_dc_dmabuf_ops_kmap, + .begin_cpu_access = vb2_dc_dmabuf_ops_begin_cpu_access, + .end_cpu_access = vb2_dc_dmabuf_ops_end_cpu_access, .vmap = vb2_dc_dmabuf_ops_vmap, .mmap = vb2_dc_dmabuf_ops_mmap, .release = vb2_dc_dmabuf_ops_release,
@@ -424,11 +471,12 @@ static void vb2_dc_put_userptr(void *buf_priv)if (sgt) {/* -* No need to sync to CPU, it's already synced to the CPU -* since the finish() memop will have been called before this. +* Don't ask to skip cache sync in case if the user +* did ask to skip cache flush the last time the +* buffer was dequeued. */ dma_unmap_sg_attrs (buf->dev, sgt->sgl, sgt->orig_nents, - buf->dma_dir,DMA_ATTR_SKIP_CPU_SYNC ); + buf->dma_dir, buf->attrs); pages =frame_vector_pages (buf->vec);/* sgt should exist only if vector contains pages... */ BUG_ON (IS_ERR (pages));
@@ -505,6 +553,7 @@ static void *vb2_dc_get_userptr(struct device *dev, unsigned long vaddr, buf->dev = dev; buf->dma_dir = dma_dir; + buf->attrs = attrs; offset = vaddr & ~PAGE_MASK ; vec =vb2_create_framevec (vaddr, size, dma_dir ==DMA_FROM_DEVICE );
@@ -544,11 +593,10 @@ static void *vb2_dc_get_userptr(struct device *dev, unsigned long vaddr, }/* -* No need to sync to the device, this will happen later when the -* prepare() memop is called. +* Sync the cache now; the user might not ever ask for it. */ sgt->nents =dma_map_sg_attrs (buf->dev, sgt->sgl, sgt->orig_nents, - buf->dma_dir,DMA_ATTR_SKIP_CPU_SYNC ); + buf->dma_dir, buf->attrs);if (sgt->nents <=0 ) {pr_err ("failed to map scatterlist\n" ); ret = -EIO ;
@@ -572,7 +620,7 @@ static void *vb2_dc_get_userptr(struct device *dev, unsigned long vaddr, fail_map_sg:dma_unmap_sg_attrs (buf->dev, sgt->sgl, sgt->orig_nents, - buf->dma_dir,DMA_ATTR_SKIP_CPU_SYNC ); + buf->dma_dir, buf->attrs); fail_sgt_init:sg_free_table (sgt);
Signature · 1 line
2.7.4
RFC v3 11/14 vb2: dma-contig: Add WARN_ON_ONCE() to check for potential bugs
In reply to Sakari AilusThe scatterlist should always be present when the cache would need to be flushed. Each buffer type has its own means to provide that. Add WARN_ON_ONCE() to check the scatterist exists.
--- drivers/media/v4l2-core/videobuf2-dma-contig.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/media/v4l2-core/videobuf2-dma-contig.c +4 -4
@@ -120,7 +120,7 @@ static void vb2_dc_prepare(void *buf_priv)* DMABUF exporter will flush the cache for us; only USERPTR * and MMAP buffers with non-coherent memory will be flushed. */ -if (buf->attrs &DMA_ATTR_NON_CONSISTENT ) +if (buf->attrs &DMA_ATTR_NON_CONSISTENT && !WARN_ON_ONCE (!sgt))dma_sync_sg_for_device (buf->dev, sgt->sgl, sgt->orig_nents, buf->dma_dir); }
@@ -134,7 +134,7 @@ static void vb2_dc_finish(void *buf_priv)* DMABUF exporter will flush the cache for us; only USERPTR * and MMAP buffers with non-coherent memory will be flushed. */ -if (buf->attrs &DMA_ATTR_NON_CONSISTENT ) +if (buf->attrs &DMA_ATTR_NON_CONSISTENT && !WARN_ON_ONCE (!sgt))dma_sync_sg_for_cpu (buf->dev, sgt->sgl, sgt->orig_nents, buf->dma_dir); }
@@ -380,7 +380,7 @@ static int vb2_dc_dmabuf_ops_begin_cpu_access(struct dma_buf *dbuf,* DMABUF exporter will flush the cache for us; only USERPTR * and MMAP buffers with non-coherent memory will be flushed. */ -if (buf->attrs &DMA_ATTR_NON_CONSISTENT ) +if (buf->attrs &DMA_ATTR_NON_CONSISTENT && !WARN_ON_ONCE (!sgt))dma_sync_sg_for_cpu (buf->dev, sgt->sgl, sgt->nents, buf->dma_dir);
@@ -397,7 +397,7 @@ static int vb2_dc_dmabuf_ops_end_cpu_access(struct dma_buf *dbuf,* DMABUF exporter will flush the cache for us; only USERPTR * and MMAP buffers with non-coherent memory will be flushed. */ -if (buf->attrs &DMA_ATTR_NON_CONSISTENT ) +if (buf->attrs &DMA_ATTR_NON_CONSISTENT && !WARN_ON_ONCE (!sgt))dma_sync_sg_for_device (buf->dev, sgt->sgl, sgt->nents, buf->dma_dir);
Signature · 1 line
2.7.4
RFC v3 12/14 vb2: dma-sg: Let drivers decide DMA attrs of MMAP and USERPTR bufs
In reply to Sakari AilusThe desirable DMA attributes are not generic for all devices using Videobuf2 scatter-gather DMA ops. Let the drivers decide.
As a result, also the DMA-BUF exporter must provide ops for synchronising the cache. This adds begin_cpu_access and end_cpu_access ops to vb2_dc_dmabuf_ops.
--- drivers/media/v4l2-core/videobuf2-dma-sg.c | 81 +++++++++++++++++++++++------- 1 file changed, 62 insertions(+), 19 deletions(-)
drivers/media/v4l2-core/videobuf2-dma-sg.c +62 -19
@@ -38,6 +38,7 @@ struct vb2_dma_sg_buf {struct frame_vector *vec;int offset;enum dma_data_direction dma_dir; +unsigned long dma_attrs;struct sg_table sg_table;/* * This will point to sg_table when used with the MMAP or USERPTR
@@ -114,6 +115,7 @@ static void *vb2_dma_sg_alloc(struct device *dev, unsigned long dma_attrs, buf->vaddr =NULL ; buf->dma_dir = dma_dir; + buf->dma_attrs = dma_attrs; buf->offset =0 ; buf->size = size;/* size is already page aligned */
@@ -143,7 +145,7 @@ static void *vb2_dma_sg_alloc(struct device *dev, unsigned long dma_attrs,* prepare() memop is called. */ sgt->nents =dma_map_sg_attrs (buf->dev, sgt->sgl, sgt->orig_nents, - buf->dma_dir,DMA_ATTR_SKIP_CPU_SYNC ); + buf->dma_dir, dma_attrs);if (!sgt->nents) goto fail_map;
@@ -181,7 +183,7 @@ static void vb2_dma_sg_put(void *buf_priv)dprintk (1 ,"%s: Freeing buffer of %d pages\n" , __func__, buf->num_pages);dma_unmap_sg_attrs (buf->dev, sgt->sgl, sgt->orig_nents, - buf->dma_dir,DMA_ATTR_SKIP_CPU_SYNC ); + buf->dma_dir, buf->dma_attrs);if (buf->vaddr)vm_unmap_ram (buf->vaddr, buf->num_pages);sg_free_table (buf->dma_sgt);
@@ -198,12 +200,13 @@ static void vb2_dma_sg_prepare(void *buf_priv)struct vb2_dma_sg_buf *buf = buf_priv;struct sg_table *sgt = buf->dma_sgt; -/* DMABUF exporter will flush the cache for us */ -if (buf->db_attach) -return ; - -dma_sync_sg_for_device (buf->dev, sgt->sgl, sgt->orig_nents, - buf->dma_dir); +/* +* DMABUF exporter will flush the cache for us; only USERPTR +* and MMAP buffers with non-coherent memory will be flushed. +*/ +if (buf->dma_attrs &DMA_ATTR_NON_CONSISTENT ) +dma_sync_sg_for_device (buf->dev, sgt->sgl, sgt->orig_nents, + buf->dma_dir); }static void vb2_dma_sg_finish (void *buf_priv)
@@ -211,11 +214,13 @@ static void vb2_dma_sg_finish(void *buf_priv)struct vb2_dma_sg_buf *buf = buf_priv;struct sg_table *sgt = buf->dma_sgt; -/* DMABUF exporter will flush the cache for us */ -if (buf->db_attach) -return ; - -dma_sync_sg_for_cpu (buf->dev, sgt->sgl, sgt->orig_nents, buf->dma_dir); +/* +* DMABUF exporter will flush the cache for us; only USERPTR +* and MMAP buffers with non-coherent memory will be flushed. +*/ +if (buf->dma_attrs &DMA_ATTR_NON_CONSISTENT ) +dma_sync_sg_for_cpu (buf->dev, sgt->sgl, sgt->orig_nents, + buf->dma_dir); } static void *vb2_dma_sg_get_userptr(struct device *dev, unsigned long vaddr,
@@ -237,6 +242,7 @@ static void *vb2_dma_sg_get_userptr(struct device *dev, unsigned long vaddr, buf->vaddr =NULL ; buf->dev = dev; buf->dma_dir = dma_dir; + buf->dma_attrs = dma_attrs; buf->offset = vaddr & ~PAGE_MASK ; buf->size = size; buf->dma_sgt = &buf->sg_table;
@@ -260,7 +266,7 @@ static void *vb2_dma_sg_get_userptr(struct device *dev, unsigned long vaddr,* prepare() memop is called. */ sgt->nents =dma_map_sg_attrs (buf->dev, sgt->sgl, sgt->orig_nents, - buf->dma_dir,DMA_ATTR_SKIP_CPU_SYNC ); + buf->dma_dir, dma_attrs);if (!sgt->nents) goto userptr_fail_map;
@@ -288,7 +294,7 @@ static void vb2_dma_sg_put_userptr(void *buf_priv)dprintk (1 ,"%s: Releasing userspace buffer of %d pages\n" , __func__, buf->num_pages);dma_unmap_sg_attrs (buf->dev, sgt->sgl, sgt->orig_nents, buf->dma_dir, -DMA_ATTR_SKIP_CPU_SYNC ); + buf->dma_attrs);if (buf->vaddr)vm_unmap_ram (buf->vaddr, buf->num_pages);sg_free_table (buf->dma_sgt);
@@ -433,6 +439,7 @@ static struct sg_table *vb2_dma_sg_dmabuf_ops_map( struct dma_buf_attachment *db_attach, enum dma_data_direction dma_dir) { struct vb2_dma_sg_attachment *attach = db_attach->priv; +struct vb2_dma_sg_buf *buf = db_attach->dmabuf->priv;/* stealing dmabuf mutex to serialize map/unmap operations */ struct mutex *lock = &db_attach->dmabuf->lock;struct sg_table *sgt;
@@ -448,14 +455,14 @@ static struct sg_table *vb2_dma_sg_dmabuf_ops_map(/* release any previous cache */ if (attach->dma_dir !=DMA_NONE ) { -dma_unmap_sg (db_attach->dev, sgt->sgl, sgt->orig_nents, - attach->dma_dir); +dma_unmap_sg_attrs (db_attach->dev, sgt->sgl, sgt->orig_nents, + attach->dma_dir, buf->dma_attrs); attach->dma_dir =DMA_NONE ; }/* mapping to the client with new direction */ - sgt->nents =dma_map_sg (db_attach->dev, sgt->sgl, sgt->orig_nents, - dma_dir); + sgt->nents =dma_map_sg_attrs (db_attach->dev, sgt->sgl, sgt->orig_nents, + dma_dir, buf->dma_attrs);if (!sgt->nents) {pr_err ("failed to map scatterlist\n" );mutex_unlock (lock);
@@ -488,6 +495,40 @@ static void *vb2_dma_sg_dmabuf_ops_kmap(struct dma_buf *dbuf, unsigned long pgnureturn buf->vaddr ? buf->vaddr + pgnum *PAGE_SIZE :NULL ; } +static int vb2_dma_sg_dmabuf_ops_begin_cpu_access ( +struct dma_buf *dbuf,enum dma_data_direction direction) +{ +struct vb2_dma_sg_buf *buf = dbuf->priv; +struct sg_table *sgt = buf->dma_sgt; + +/* +* DMABUF exporter will flush the cache for us; only USERPTR +* and MMAP buffers with non-coherent memory will be flushed. +*/ +if (buf->dma_attrs &DMA_ATTR_NON_CONSISTENT ) +dma_sync_sg_for_cpu (buf->dev, sgt->sgl, sgt->nents, + buf->dma_dir); + +return 0 ; +} + +static int vb2_dma_sg_dmabuf_ops_end_cpu_access ( +struct dma_buf *dbuf,enum dma_data_direction direction) +{ +struct vb2_dma_sg_buf *buf = dbuf->priv; +struct sg_table *sgt = buf->dma_sgt; + +/* +* DMABUF exporter will flush the cache for us; only USERPTR +* and MMAP buffers with non-coherent memory will be flushed. +*/ +if (buf->dma_attrs &DMA_ATTR_NON_CONSISTENT ) +dma_sync_sg_for_device (buf->dev, sgt->sgl, sgt->nents, + buf->dma_dir); + +return 0 ; +} +static void *vb2_dma_sg_dmabuf_ops_vmap (struct dma_buf *dbuf) {struct vb2_dma_sg_buf *buf = dbuf->priv;
@@ -508,6 +549,8 @@ static struct dma_buf_ops vb2_dma_sg_dmabuf_ops = { .unmap_dma_buf = vb2_dma_sg_dmabuf_ops_unmap, .kmap = vb2_dma_sg_dmabuf_ops_kmap, .kmap_atomic = vb2_dma_sg_dmabuf_ops_kmap, + .begin_cpu_access = vb2_dma_sg_dmabuf_ops_begin_cpu_access, + .end_cpu_access = vb2_dma_sg_dmabuf_ops_end_cpu_access, .vmap = vb2_dma_sg_dmabuf_ops_vmap, .mmap = vb2_dma_sg_dmabuf_ops_mmap, .release = vb2_dma_sg_dmabuf_ops_release,
Signature · 1 line
2.7.4
RFC v3 13/14 vb2: Don't sync cache for a buffer if so requested
In reply to Sakari AilusFrom: Samu Onkalo <[email protected]>
The user may request to the driver (vb2) to skip the cache maintenance operations in case the buffer does not need cache synchronisation, e.g. in cases where the buffer is passed between hardware blocks without it being touched by the CPU.
Also document that the prepare and finish vb2_mem_ops might not get called every time the buffer ownership changes between the kernel and the user space.
--- drivers/media/v4l2-core/videobuf2-core.c | 101 +++++++++++++++++++++---------- drivers/media/v4l2-core/videobuf2-v4l2.c | 14 ++++- include/media/videobuf2-core.h | 23 ++++--- 3 files changed, 97 insertions(+), 41 deletions(-)
drivers/media/v4l2-core/videobuf2-core.c +70 -31
@@ -189,6 +189,28 @@ static void __vb2_queue_cancel(struct vb2_queue *q);static void __enqueue_in_driver (struct vb2_buffer *vb);/** +* __mem_prepare_planes() - call finish mem op for all planes of the buffer +*/ +static void __mem_prepare_planes (struct vb2_buffer *vb) +{ +unsigned int plane; + +for (plane =0 ; plane < vb->num_planes; ++plane) +call_void_memop (vb, prepare, vb->planes[plane].mem_priv); +} + +/** +* __mem_finish_planes() - call finish mem op for all planes of the buffer +*/ +static void __mem_finish_planes (struct vb2_buffer *vb) +{ +unsigned int plane; + +for (plane =0 ; plane < vb->num_planes; ++plane) +call_void_memop (vb, finish, vb->planes[plane].mem_priv); +} + +/** * __vb2_buf_mem_alloc() - allocate video memory for the given buffer */ static int __vb2_buf_mem_alloc (struct vb2_buffer *vb)
@@ -953,20 +975,29 @@ EXPORT_SYMBOL_GPL(vb2_discard_done);/** * __prepare_mmap() - prepare an MMAP buffer */ -static int __prepare_mmap (struct vb2_buffer *vb,const void *pb) +static int __prepare_mmap (struct vb2_buffer *vb,const void *pb, +bool no_cache_sync) { -int ret =0 ; +int ret; -if (pb) +if (pb) { ret =call_bufop (vb->vb2_queue, fill_vb2_buffer, vb, pb, vb->planes); -return ret ? ret :call_vb_qop (vb, buf_prepare, vb); +if (ret) +return ret; + } + +if (!no_cache_sync) +__mem_prepare_planes (vb); + +return call_vb_qop (vb, buf_prepare, vb); }/** * __prepare_userptr() - prepare a USERPTR buffer */ -static int __prepare_userptr (struct vb2_buffer *vb,const void *pb) +static int __prepare_userptr (struct vb2_buffer *vb,const void *pb, +bool no_cache_sync) {struct vb2_plane planes[VB2_MAX_PLANES ];struct vb2_queue *q = vb->vb2_queue;
@@ -1057,6 +1088,11 @@ static int __prepare_userptr(struct vb2_buffer *vb, const void *pb)dprintk (1 ,"buffer initialization failed\n" ); goto err; } + +/* This is new buffer memory --- always synchronise cache. */ +__mem_prepare_planes (vb); + }else if (!no_cache_sync) { +__mem_prepare_planes (vb); } ret =call_vb_qop (vb, buf_prepare, vb);
@@ -1084,7 +1120,8 @@ static int __prepare_userptr(struct vb2_buffer *vb, const void *pb)/** * __prepare_dmabuf() - prepare a DMABUF buffer */ -static int __prepare_dmabuf (struct vb2_buffer *vb,const void *pb) +static int __prepare_dmabuf (struct vb2_buffer *vb,const void *pb, +bool no_cache_sync) {struct vb2_plane planes[VB2_MAX_PLANES ];struct vb2_queue *q = vb->vb2_queue;
@@ -1199,6 +1236,11 @@ static int __prepare_dmabuf(struct vb2_buffer *vb, const void *pb)dprintk (1 ,"buffer initialization failed\n" ); goto err; } + +/* This is new buffer memory --- always synchronise cache. */ +__mem_prepare_planes (vb); + }else if (!no_cache_sync) { +__mem_prepare_planes (vb); } ret =call_vb_qop (vb, buf_prepare, vb);
@@ -1231,10 +1273,10 @@ static void __enqueue_in_driver(struct vb2_buffer *vb)call_void_vb_qop (vb, buf_queue, vb); } -static int __buf_prepare (struct vb2_buffer *vb,const void *pb) +static int __buf_prepare (struct vb2_buffer *vb,const void *pb, +bool no_cache_sync) {struct vb2_queue *q = vb->vb2_queue; -unsigned int plane;int ret;if (q->error) {
@@ -1246,13 +1288,13 @@ static int __buf_prepare(struct vb2_buffer *vb, const void *pb)switch (q->memory) {case VB2_MEMORY_MMAP : - ret =__prepare_mmap (vb, pb); + ret =__prepare_mmap (vb, pb, no_cache_sync);break ;case VB2_MEMORY_USERPTR : - ret =__prepare_userptr (vb, pb); + ret =__prepare_userptr (vb, pb, no_cache_sync);break ;case VB2_MEMORY_DMABUF : - ret =__prepare_dmabuf (vb, pb); + ret =__prepare_dmabuf (vb, pb, no_cache_sync);break ;default :WARN (1 ,"Invalid queue type\n" );
@@ -1265,16 +1307,13 @@ static int __buf_prepare(struct vb2_buffer *vb, const void *pb)return ret; } -/* sync buffers */ -for (plane =0 ; plane < vb->num_planes; ++plane) -call_void_memop (vb, prepare, vb->planes[plane].mem_priv); - vb->state =VB2_BUF_STATE_PREPARED ;return 0 ; } -int vb2_core_prepare_buf (struct vb2_queue *q,unsigned int index,void *pb) +int vb2_core_prepare_buf (struct vb2_queue *q,unsigned int index,void *pb, +bool no_cache_sync) {struct vb2_buffer *vb;int ret;
@@ -1286,7 +1325,7 @@ int vb2_core_prepare_buf(struct vb2_queue *q, unsigned int index, void *pb)return -EINVAL ; } - ret =__buf_prepare (vb, pb); + ret =__buf_prepare (vb, pb, no_cache_sync);if (ret)return ret;
@@ -1362,7 +1401,8 @@ static int vb2_start_streaming(struct vb2_queue *q)return ret; } -int vb2_core_qbuf (struct vb2_queue *q,unsigned int index,void *pb) +int vb2_core_qbuf (struct vb2_queue *q,unsigned int index,void *pb, +bool no_cache_sync) {struct vb2_buffer *vb;int ret;
@@ -1371,7 +1411,7 @@ int vb2_core_qbuf(struct vb2_queue *q, unsigned int index, void *pb)switch (vb->state) {case VB2_BUF_STATE_DEQUEUED : - ret =__buf_prepare (vb, pb); + ret =__buf_prepare (vb, pb, no_cache_sync);if (ret)return ret;break ;
@@ -1557,7 +1597,7 @@ EXPORT_SYMBOL_GPL(vb2_wait_for_all_buffers);/** * __vb2_dqbuf() - bring back the buffer to the DEQUEUED state */ -static void __vb2_dqbuf (struct vb2_buffer *vb) +static void __vb2_dqbuf (struct vb2_buffer *vb,bool no_cache_sync) {struct vb2_queue *q = vb->vb2_queue;unsigned int i;
@@ -1568,9 +1608,8 @@ static void __vb2_dqbuf(struct vb2_buffer *vb) vb->state =VB2_BUF_STATE_DEQUEUED ; -/* sync buffers */ -for (i =0 ; i < vb->num_planes; ++i) -call_void_memop (vb, finish, vb->planes[i].mem_priv); +if (!no_cache_sync) +__mem_finish_planes (vb);/* unmap DMABUF buffer */ if (q->memory ==VB2_MEMORY_DMABUF )
@@ -1583,7 +1622,7 @@ static void __vb2_dqbuf(struct vb2_buffer *vb) }int vb2_core_dqbuf (struct vb2_queue *q,unsigned int *pindex,void *pb, -bool nonblocking) +bool nonblocking,bool no_cache_sync) {struct vb2_buffer *vb =NULL ;int ret;
@@ -1620,7 +1659,7 @@ int vb2_core_dqbuf(struct vb2_queue *q, unsigned int *pindex, void *pb,trace_vb2_dqbuf (q, vb);/* go back to dequeued state */ -__vb2_dqbuf (vb); +__vb2_dqbuf (vb, no_cache_sync);dprintk (1 ,"dqbuf of buffer %d, with state %d\n" , vb->index, vb->state);
@@ -1694,7 +1733,7 @@ static void __vb2_queue_cancel(struct vb2_queue *q) vb->state =VB2_BUF_STATE_PREPARED ;call_void_vb_qop (vb, buf_finish, vb); } -__vb2_dqbuf (vb); +__vb2_dqbuf (vb, false); } }
@@ -2242,7 +2281,7 @@ static int __vb2_init_fileio(struct vb2_queue *q, int read)* Queue all buffers. */ for (i =0 ; i < q->num_buffers; i++) { - ret =vb2_core_qbuf (q, i,NULL ); + ret =vb2_core_qbuf (q, i,NULL , false);if (ret) goto err_reqbufs; fileio->bufs[i].queued =1 ;
@@ -2345,7 +2384,7 @@ static size_t __vb2_perform_fileio(struct vb2_queue *q, char __user *data, size_/* * Call vb2_dqbuf to get buffer back. */ - ret =vb2_core_dqbuf (q, &index,NULL , nonblock); + ret =vb2_core_dqbuf (q, &index,NULL , nonblock, false);dprintk (5 ,"vb2_dqbuf result: %d\n" , ret);if (ret)return ret;
@@ -2421,7 +2460,7 @@ static size_t __vb2_perform_fileio(struct vb2_queue *q, char __user *data, size_if (copy_timestamp) b->timestamp =ktime_get_ns (); - ret =vb2_core_qbuf (q, index,NULL ); + ret =vb2_core_qbuf (q, index,NULL , false);dprintk (5 ,"vb2_dbuf result: %d\n" , ret);if (ret)return ret;
@@ -2507,7 +2546,7 @@ static int vb2_thread(void *data) }else {call_void_qop (q, wait_finish, q);if (!threadio->stop) - ret =vb2_core_dqbuf (q, &index,NULL ,0 ); + ret =vb2_core_dqbuf (q, &index,NULL ,0 , false);call_void_qop (q, wait_prepare, q);dprintk (5 ,"file io: vb2_dqbuf result: %d\n" , ret);if (!ret)
@@ -2524,7 +2563,7 @@ static int vb2_thread(void *data)if (copy_timestamp) vb->timestamp =ktime_get_ns ();;if (!threadio->stop) - ret =vb2_core_qbuf (q, vb->index,NULL ); + ret =vb2_core_qbuf (q, vb->index,NULL , false);call_void_qop (q, wait_prepare, q);if (ret || threadio->stop)break ;
drivers/media/v4l2-core/videobuf2-v4l2.c +11 -3
@@ -499,8 +499,11 @@ int vb2_prepare_buf(struct vb2_queue *q, struct v4l2_buffer *b) } ret =vb2_queue_or_prepare_buf (q, b,"prepare_buf" ); +if (ret) +return ret; -return ret ? ret :vb2_core_prepare_buf (q, b->index, b); +return vb2_core_prepare_buf (q, b->index, b, + b->flags &V4L2_BUF_FLAG_NO_CACHE_SYNC ); }EXPORT_SYMBOL_GPL (vb2_prepare_buf);
@@ -565,7 +568,11 @@ int vb2_qbuf(struct vb2_queue *q, struct v4l2_buffer *b) } ret =vb2_queue_or_prepare_buf (q, b,"qbuf" ); -return ret ? ret :vb2_core_qbuf (q, b->index, b); +if (ret) +return ret; + +return vb2_core_qbuf (q, b->index, b, + b->flags &V4L2_BUF_FLAG_NO_CACHE_SYNC ); }EXPORT_SYMBOL_GPL (vb2_qbuf);
@@ -583,7 +590,8 @@ int vb2_dqbuf(struct vb2_queue *q, struct v4l2_buffer *b, bool nonblocking)return -EINVAL ; } - ret =vb2_core_dqbuf (q,NULL , b, nonblocking); + ret =vb2_core_dqbuf (q,NULL , b, nonblocking, + b->flags &V4L2_BUF_FLAG_NO_CACHE_SYNC );/* * After calling the VIDIOC_DQBUF V4L2_BUF_FLAG_DONE must be
include/media/videobuf2-core.h +16 -7
@@ -83,10 +83,14 @@ struct vb2_threadio_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. +* @prepare: Called on the plane when the buffer ownership is passed from +* the user space to the kernel and the plane must be cache +* syncronised. The V4L2_BUF_FLAG_NO_CACHE_SYNC buffer flag may +* be used to skip this call. Optional. +* @finish: Called on the plane when the buffer ownership is passed from +* the kernel to the user space and the plane must be cache +* syncronised. The V4L2_BUF_FLAG_NO_CACHE_SYNC buffer flag may +* be used to skip this call. 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.
@@ -696,6 +700,7 @@ int vb2_core_create_bufs(struct vb2_queue *q, enum vb2_memory memory,* @index: id number of the buffer * @pb: buffer structure passed from userspace to vidioc_prepare_buf * handler in driver +* @no_cache_sync if true, skip cache synchronization * * Should be called from vidioc_prepare_buf ioctl handler of a driver. * The passed buffer should have been verified.
@@ -705,7 +710,8 @@ int vb2_core_create_bufs(struct vb2_queue *q, enum vb2_memory memory,* The return values from this function are intended to be directly returned * from vidioc_prepare_buf handler in driver. */ -int vb2_core_prepare_buf (struct vb2_queue *q,unsigned int index,void *pb); +int vb2_core_prepare_buf (struct vb2_queue *q,unsigned int index,void *pb, +bool no_cache_sync);/** * vb2_core_qbuf() - Queue a buffer from userspace
@@ -714,6 +720,7 @@ int vb2_core_prepare_buf(struct vb2_queue *q, unsigned int index, void *pb);* @index: id number of the buffer * @pb: buffer structure passed from userspace to vidioc_qbuf handler * in driver +* @no_cache_sync if true, skip cache synchronization * * Should be called from vidioc_qbuf ioctl handler of a driver. * The passed buffer should have been verified.
@@ -728,7 +735,8 @@ int vb2_core_prepare_buf(struct vb2_queue *q, unsigned int index, void *pb);* The return values from this function are intended to be directly returned * from vidioc_qbuf handler in driver. */ -int vb2_core_qbuf (struct vb2_queue *q,unsigned int index,void *pb); +int vb2_core_qbuf (struct vb2_queue *q,unsigned int index,void *pb, +bool no_cache_sync);/** * vb2_core_dqbuf() - Dequeue a buffer to the userspace
@@ -739,6 +747,7 @@ int vb2_core_qbuf(struct vb2_queue *q, unsigned int index, void *pb);* @nonblocking: if true, this call will not sleep waiting for a buffer if no * buffers ready for dequeuing are present. Normally the driver * would be passing (file->f_flags & O_NONBLOCK) here +* @no_cache_sync if true, skip cache synchronization * * Should be called from vidioc_dqbuf ioctl handler of a driver. * The passed buffer should have been verified.
@@ -755,7 +764,7 @@ int vb2_core_qbuf(struct vb2_queue *q, unsigned int index, void *pb);* from vidioc_dqbuf handler in driver. */ int vb2_core_dqbuf (struct vb2_queue *q,unsigned int *pindex,void *pb, -bool nonblocking); +bool nonblocking,bool no_cache_sync);int vb2_core_streamon (struct vb2_queue *q,unsigned int type);int vb2_core_streamoff (struct vb2_queue *q,unsigned int type);
Signature · 1 line
2.7.4
RFC v3 14/14 vb2: Improve struct vb2_mem_ops documentation; alloc and put are for MMAP
In reply to Sakari AilusThe alloc() and put() ops are for MMAP buffers only. Document it.
--- include/media/videobuf2-core.h | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-)
include/media/videobuf2-core.h +10 -10
@@ -46,16 +46,16 @@ struct vb2_threadio_data;/** * struct vb2_mem_ops - memory handling/memory allocator operations -* @alloc: allocate video memory and, optionally, allocator private data, -* return ERR_PTR() on failure or a pointer to allocator private, -* per-buffer data on success; the returned private structure -* will then be passed as @buf_priv argument to other ops in this -* structure. Additional gfp_flags to use when allocating the -* are also passed to this operation. These flags are from the -* gfp_flags field of vb2_queue. -* @put: inform the allocator that the buffer will no longer be used; -* usually will result in the allocator freeing the buffer (if -* no other users of this buffer are present); the @buf_priv +* @alloc: allocate video memory for an MMAP buffer and, optionally, +* allocator private data, return ERR_PTR() on failure or a pointer +* to allocator private, per-buffer data on success; the returned +* private structure will then be passed as @buf_priv argument to +* other ops in this structure. Additional gfp_flags to use when +* allocating the memory are also passed to this operation. These +* flags are from the gfp_flags field of vb2_queue. +* @put: inform the allocator that the MMAP buffer will no longer be +* used; usually will result in the allocator freeing the buffer +* (if no other users of this buffer are present); the @buf_priv * argument is the allocator private per-buffer structure * previously returned from the alloc callback. * @get_dmabuf: acquire userspace memory for a hardware operation; used for
Signature · 1 line
2.7.4
RFC v3 5/14 vb2: Anticipate queue specific DMA attributes for USERPTR buffers
In reply to Sakari AilusThe DMA attributes were available for the memop implementation for MMAP buffers but not for USERPTR buffers. Do the same for USERPTR. This patch makes no functional changes.
---
since RFC v1:
- Add missing q->dma_attrs argument to call_ptr_memop(vb, get_userptr...
drivers/media/v4l2-core/videobuf2-core.c | 2 +- drivers/media/v4l2-core/videobuf2-dma-contig.c | 3 ++- drivers/media/v4l2-core/videobuf2-dma-sg.c | 3 ++- drivers/media/v4l2-core/videobuf2-vmalloc.c | 3 ++- include/media/videobuf2-core.h | 3 ++- 5 files changed, 9 insertions(+), 5 deletions(-)
drivers/media/v4l2-core/videobuf2-core.c +1 -1
@@ -1025,7 +1025,7 @@ static int __prepare_userptr(struct vb2_buffer *vb, const void *pb) mem_priv =call_ptr_memop (vb, get_userptr, q->alloc_devs[plane] ? : q->dev, planes[plane].m.userptr, - planes[plane].length, dma_dir); + planes[plane].length, dma_dir, q->dma_attrs);if (IS_ERR (mem_priv)) {dprintk (1 ,"failed acquiring userspace memory for plane %d\n" , plane);
drivers/media/v4l2-core/videobuf2-dma-contig.c +2 -1
@@ -475,7 +475,8 @@ static inline dma_addr_t vb2_dc_pfn_to_dma(struct device *dev, unsigned long pfn#endif static void *vb2_dc_get_userptr (struct device *dev,unsigned long vaddr, -unsigned long size,enum dma_data_direction dma_dir) +unsigned long size,enum dma_data_direction dma_dir, +unsigned long attrs) {struct vb2_dc_buf *buf;struct frame_vector *vec;
drivers/media/v4l2-core/videobuf2-dma-sg.c +2 -1
@@ -220,7 +220,8 @@ static void vb2_dma_sg_finish(void *buf_priv)static void *vb2_dma_sg_get_userptr (struct device *dev,unsigned long vaddr,unsigned long size, -enum dma_data_direction dma_dir) +enum dma_data_direction dma_dir, +unsigned long dma_attrs) {struct vb2_dma_sg_buf *buf;struct sg_table *sgt;
drivers/media/v4l2-core/videobuf2-vmalloc.c +2 -1
@@ -73,7 +73,8 @@ static void vb2_vmalloc_put(void *buf_priv)static void *vb2_vmalloc_get_userptr (struct device *dev,unsigned long vaddr,unsigned long size, -enum dma_data_direction dma_dir) +enum dma_data_direction dma_dir, +unsigned long dma_attrs) {struct vb2_vmalloc_buf *buf;struct frame_vector *vec;
include/media/videobuf2-core.h +2 -1
@@ -122,7 +122,8 @@ struct vb2_mem_ops {void *(*get_userptr)(struct device *dev,unsigned long vaddr,unsigned long size, -enum dma_data_direction dma_dir); +enum dma_data_direction dma_dir, +unsigned long dma_attrs);void (*put_userptr)(void *buf_priv);void (*prepare)(void *buf_priv);
Signature · 1 line
2.7.4