Drivers that transfer buffer contents with the CPU must bracket access to imported DMA-BUFs with dma_buf_begin_cpu_access() and dma_buf_end_cpu_access().
Add a uses_cpu_access queue flag for vb2-vmalloc queues to bracket the interval from buf_prepare() through buf_finish(), using read access for output queues and write access for capture queues. Keep this opt-in because some vb2-vmalloc drivers pass imported buffers to hardware, and end access in process context because DMA-BUF callbacks may sleep and return errors. Signed-off-by: Karl Mehltretter <[email protected]> --- v2: - Scope uses_cpu_access to vb2-vmalloc queues and drop the attach_dmabuf dma_dir parameter from vb2_mem_ops and the allocators (Tomasz Figa). - Call __vb2_buf_end_cpu_access() from __vb2_buf_cancel() (Tomasz Figa). .../media/common/videobuf2/videobuf2-core.c | 76 +++++++++++++++++++ include/media/videobuf2-core.h | 5 ++ 2 files changed, 81 insertions(+) diff --git a/drivers/media/common/videobuf2/videobuf2-core.c b/drivers/media/common/videobuf2/videobuf2-core.c index 29e539ee002e..6c32d3cccb93 100644 --- a/drivers/media/common/videobuf2/videobuf2-core.c +++ b/drivers/media/common/videobuf2/videobuf2-core.c @@ -371,6 +371,65 @@ static void __vb2_buf_mem_finish(struct vb2_buffer *vb) call_void_memop(vb, finish, vb->planes[plane].mem_priv); } +/* Output buffers are read by the driver; capture buffers are written. */ +static enum dma_data_direction vb2_cpu_access_dir(const struct vb2_queue *q) +{ + return q->is_output ? DMA_FROM_DEVICE : DMA_TO_DEVICE; +} + +static bool __vb2_dmabuf_is_duplicate(const struct vb2_buffer *vb, + unsigned int plane) +{ + unsigned int i; + + for (i = 0; i < plane; i++) + if (vb->planes[i].dbuf == vb->planes[plane].dbuf) + return true; + + return false; +} + +static void __vb2_buf_end_cpu_access(struct vb2_buffer *vb, + unsigned int num_planes) +{ + struct vb2_queue *q = vb->vb2_queue; + enum dma_data_direction dir = vb2_cpu_access_dir(q); + unsigned int plane; + + for (plane = 0; plane < num_planes; plane++) { + int ret; + + if (__vb2_dmabuf_is_duplicate(vb, plane)) + continue; + + ret = dma_buf_end_cpu_access(vb->planes[plane].dbuf, dir); + if (ret) + pr_warn_ratelimited("[%s] failed to end CPU access to dmabuf plane %u: %d\n", + q->name, plane, ret); + } +} + +static int __vb2_buf_begin_cpu_access(struct vb2_buffer *vb) +{ + struct vb2_queue *q = vb->vb2_queue; + enum dma_data_direction dir = vb2_cpu_access_dir(q); + unsigned int plane; + int ret; + + for (plane = 0; plane < vb->num_planes; plane++) { + if (__vb2_dmabuf_is_duplicate(vb, plane)) + continue; + + ret = dma_buf_begin_cpu_access(vb->planes[plane].dbuf, dir); + if (ret) { + __vb2_buf_end_cpu_access(vb, plane); + return ret; + } + } + + return 0; +} + /* * __setup_offsets() - setup unique offsets ("cookies") for every plane in * the buffer. @@ -1504,9 +1563,21 @@ static int __prepare_dmabuf(struct vb2_buffer *vb) } } + if (q->uses_cpu_access) { + ret = __vb2_buf_begin_cpu_access(vb); + if (ret) { + dprintk(q, 1, "failed to begin CPU access to dmabuf: %d\n", + ret); + call_void_vb_qop(vb, buf_cleanup, vb); + goto err_put_vb2_buf; + } + } + ret = call_vb_qop(vb, buf_prepare, vb); if (ret) { dprintk(q, 1, "buffer preparation failed\n"); + if (q->uses_cpu_access) + __vb2_buf_end_cpu_access(vb, vb->num_planes); call_void_vb_qop(vb, buf_cleanup, vb); goto err_put_vb2_buf; } @@ -1720,6 +1791,7 @@ EXPORT_SYMBOL_GPL(vb2_core_prepare_buf); static void __vb2_buf_cancel(struct vb2_buffer *vb) { struct media_request *req = vb->req_obj.req; + struct vb2_queue *q = vb->vb2_queue; /* * If a request is associated with this buffer, then @@ -1743,6 +1815,8 @@ static void __vb2_buf_cancel(struct vb2_buffer *vb) if (vb->prepared) { call_void_vb_qop(vb, buf_finish, vb); + if (q->memory == VB2_MEMORY_DMABUF && q->uses_cpu_access) + __vb2_buf_end_cpu_access(vb, vb->num_planes); vb->prepared = 0; } __vb2_dqbuf(vb); @@ -2191,6 +2265,8 @@ int vb2_core_dqbuf(struct vb2_queue *q, unsigned int *pindex, void *pb, } call_void_vb_qop(vb, buf_finish, vb); + if (q->memory == VB2_MEMORY_DMABUF && q->uses_cpu_access) + __vb2_buf_end_cpu_access(vb, vb->num_planes); vb->prepared = 0; if (pindex) diff --git a/include/media/videobuf2-core.h b/include/media/videobuf2-core.h index 4b4f4c15c53a..bbcdd8c8989a 100644 --- a/include/media/videobuf2-core.h +++ b/include/media/videobuf2-core.h @@ -510,6 +510,10 @@ struct vb2_buf_ops { * ->finish(). * @non_coherent_mem: when set queue will attempt to allocate buffers using * non-coherent memory. + * @uses_cpu_access: the driver transfers buffer data with the CPU instead of + * DMA. For imported dma-bufs on vb2-vmalloc queues, videobuf2 + * brackets the interval from buf_prepare through buf_finish with + * dma_buf_begin_cpu_access() and dma_buf_end_cpu_access(). * @lock: pointer to a mutex that protects the &struct vb2_queue. The * driver must set this to a mutex to let the v4l2 core serialize * the queuing ioctls. This lock is used when waiting for a new @@ -614,6 +618,7 @@ struct vb2_queue { unsigned int uses_requests:1; unsigned int allow_cache_hints:1; unsigned int non_coherent_mem:1; + unsigned int uses_cpu_access:1; struct mutex *lock; void *owner; -- 2.39.5 (Apple Git-154)
