Skip to content

Commit f5f5fa7

Browse files
sergey-senozhatskymchehab
authored andcommitted
media: videobuf2: handle V4L2 buffer cache flags
Set video buffer cache management flags corresponding to V4L2 cache flags. Both ->prepare() and ->finish() cache management hints should be passed during this stage (buffer preparation), because there is no other way for user-space to tell V4L2 to avoid ->finish() cache flush. Signed-off-by: Sergey Senozhatsky <senozhatsky@chromium.org> Signed-off-by: Hans Verkuil <hverkuil-cisco@xs4all.nl> Signed-off-by: Mauro Carvalho Chehab <mchehab+huawei@kernel.org>
1 parent 6d21998 commit f5f5fa7

2 files changed

Lines changed: 59 additions & 0 deletions

File tree

‎drivers/media/common/videobuf2/videobuf2-v4l2.c‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,53 @@ static int vb2_fill_vb2_v4l2_buffer(struct vb2_buffer *vb, struct v4l2_buffer *b
337337
return 0;
338338
}
339339

340+
static void set_buffer_cache_hints(struct vb2_queue *q,
341+
struct vb2_buffer *vb,
342+
struct v4l2_buffer *b)
343+
{
344+
/*
345+
* DMA exporter should take care of cache syncs, so we can avoid
346+
* explicit ->prepare()/->finish() syncs. For other ->memory types
347+
* we always need ->prepare() or/and ->finish() cache sync.
348+
*/
349+
if (q->memory == VB2_MEMORY_DMABUF) {
350+
vb->need_cache_sync_on_finish = 0;
351+
vb->need_cache_sync_on_prepare = 0;
352+
return;
353+
}
354+
355+
/*
356+
* Cache sync/invalidation flags are set by default in order to
357+
* preserve existing behaviour for old apps/drivers.
358+
*/
359+
vb->need_cache_sync_on_prepare = 1;
360+
vb->need_cache_sync_on_finish = 1;
361+
362+
if (!vb2_queue_allows_cache_hints(q)) {
363+
/*
364+
* Clear buffer cache flags if queue does not support user
365+
* space hints. That's to indicate to userspace that these
366+
* flags won't work.
367+
*/
368+
b->flags &= ~V4L2_BUF_FLAG_NO_CACHE_INVALIDATE;
369+
b->flags &= ~V4L2_BUF_FLAG_NO_CACHE_CLEAN;
370+
return;
371+
}
372+
373+
/*
374+
* ->finish() cache sync can be avoided when queue direction is
375+
* TO_DEVICE.
376+
*/
377+
if (q->dma_dir == DMA_TO_DEVICE)
378+
vb->need_cache_sync_on_finish = 0;
379+
380+
if (b->flags & V4L2_BUF_FLAG_NO_CACHE_INVALIDATE)
381+
vb->need_cache_sync_on_finish = 0;
382+
383+
if (b->flags & V4L2_BUF_FLAG_NO_CACHE_CLEAN)
384+
vb->need_cache_sync_on_prepare = 0;
385+
}
386+
340387
static int vb2_queue_or_prepare_buf(struct vb2_queue *q, struct media_device *mdev,
341388
struct v4l2_buffer *b, bool is_prepare,
342389
struct media_request **p_req)
@@ -381,6 +428,7 @@ static int vb2_queue_or_prepare_buf(struct vb2_queue *q, struct media_device *md
381428
}
382429

383430
if (!vb->prepared) {
431+
set_buffer_cache_hints(q, vb, b);
384432
/* Copy relevant information provided by the userspace */
385433
memset(vbuf->planes, 0,
386434
sizeof(vbuf->planes[0]) * vb->num_planes);

‎include/media/videobuf2-core.h‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,17 @@ struct vb2_queue {
635635
#endif
636636
};
637637

638+
/**
639+
* vb2_queue_allows_cache_hints() - Return true if the queue allows cache
640+
* and memory consistency hints.
641+
*
642+
* @q: pointer to &struct vb2_queue with videobuf2 queue
643+
*/
644+
static inline bool vb2_queue_allows_cache_hints(struct vb2_queue *q)
645+
{
646+
return q->allow_cache_hints && q->memory == VB2_MEMORY_MMAP;
647+
}
648+
638649
/**
639650
* vb2_plane_vaddr() - Return a kernel virtual address of a given plane.
640651
* @vb: pointer to &struct vb2_buffer to which the plane in

0 commit comments

Comments
 (0)