History

media: videobuf2: add queue memory coherency parameter

Preparations for future V4L2_MEMORY_FLAG_NON_COHERENT support. Extend the vb2_core_reqbufs() parameters list to accept requests' ->flags, which will be used for memory coherency configuration. An attempt to allocate a buffer with coherency requirements that do not match the queue's consistency model will fail.

Sergey Senozhatsky committed 5 years ago

Commit b00a9e51 parent 965c1e04 files · +43 −11Browse files

Unified · Split

4 files
  1. drivers/media/common/videobuf2/videobuf2-core.c +31 −6
  2. drivers/media/common/videobuf2/videobuf2-v4l2.c +3 −2
  3. drivers/media/dvb-core/dvb_vb2.c +1 −1
  4. include/media/videobuf2-core.h +8 −2
drivers/media/common/videobuf2/videobuf2-core.c+31−6
@@ -738,11 +738,30 @@ int vb2_verify_memory_type(struct vb2_queue *q, · 737 unmodified lines
738738 }
739739 EXPORT_SYMBOL(vb2_verify_memory_type);
740740
741+static void set_queue_coherency(struct vb2_queue *q, bool non_coherent_mem)
742+{
743+ q->non_coherent_mem = 0;
744+
745+ if (!vb2_queue_allows_cache_hints(q))
746+ return;
747+ q->non_coherent_mem = non_coherent_mem;
748+}
749+
750+static bool verify_coherency_flags(struct vb2_queue *q, bool non_coherent_mem)
751+{
752+ if (non_coherent_mem != q->non_coherent_mem) {
753+ dprintk(q, 1, "memory coherency model mismatch\n");
754+ return false;
755+ }
756+ return true;
757+}
758+
741759 int vb2_core_reqbufs(struct vb2_queue *q, enum vb2_memory memory,
742− unsigned int *count)
760+ unsigned int flags, unsigned int *count)
743761 {
744762 unsigned int num_buffers, allocated_buffers, num_planes = 0;
745763 unsigned plane_sizes[VB2_MAX_PLANES] = { };
764+ bool non_coherent_mem = false;
746765 unsigned int i;
747766 int ret;
748767
@@ -757,7 +776,8 @@ int vb2_core_reqbufs(struct vb2_queue *q, enum vb2_memory memory, · 8 unmodified lines
757776 }
758777
759778 if (*count == 0 || q->num_buffers != 0 ||
760− (q->memory != VB2_MEMORY_UNKNOWN && q->memory != memory)) {
779+ (q->memory != VB2_MEMORY_UNKNOWN && q->memory != memory) ||
780+ !verify_coherency_flags(q, non_coherent_mem)) {
761781 /*
762782 * We already have buffers allocated, so first check if they
763783 * are not in use and can be freed.
@@ -794,6 +814,7 @@ int vb2_core_reqbufs(struct vb2_queue *q, enum vb2_memory memory, · 30 unmodified lines
794814 num_buffers = min_t(unsigned int, num_buffers, VB2_MAX_FRAME);
795815 memset(q->alloc_devs, 0, sizeof(q->alloc_devs));
796816 q->memory = memory;
817+ set_queue_coherency(q, non_coherent_mem);
797818
798819 /*
799820 * Ask the driver how many buffers and planes per buffer it requires.
@@ -878,12 +899,13 @@ int vb2_core_reqbufs(struct vb2_queue *q, enum vb2_memory memory, · 78 unmodified lines
878899 EXPORT_SYMBOL_GPL(vb2_core_reqbufs);
879900
880901 int vb2_core_create_bufs(struct vb2_queue *q, enum vb2_memory memory,
881− unsigned int *count,
902+ unsigned int flags, unsigned int *count,
882903 unsigned int requested_planes,
883904 const unsigned int requested_sizes[])
884905 {
885906 unsigned int num_planes = 0, num_buffers, allocated_buffers;
886907 unsigned plane_sizes[VB2_MAX_PLANES] = { };
908+ bool non_coherent_mem = false;
887909 int ret;
888910
889911 if (q->num_buffers == VB2_MAX_FRAME) {
@@ -899,11 +921,14 @@ int vb2_core_create_bufs(struct vb2_queue *q, enum vb2_memory memory, · 9 unmodified lines
899921 memset(q->alloc_devs, 0, sizeof(q->alloc_devs));
900922 q->memory = memory;
901923 q->waiting_for_buffers = !q->is_output;
924+ set_queue_coherency(q, non_coherent_mem);
902925 } else {
903926 if (q->memory != memory) {
904927 dprintk(q, 1, "memory model mismatch\n");
905928 return -EINVAL;
906929 }
930+ if (!verify_coherency_flags(q, non_coherent_mem))
931+ return -EINVAL;
907932 }
908933
909934 num_buffers = min(*count, VB2_MAX_FRAME - q->num_buffers);
@@ -2608,7 +2633,7 @@ static int __vb2_init_fileio(struct vb2_queue *q, int read) · 1698 unmodified lines
26082633 fileio->memory = VB2_MEMORY_MMAP;
26092634 fileio->type = q->type;
26102635 q->fileio = fileio;
2611− ret = vb2_core_reqbufs(q, fileio->memory, &fileio->count);
2636+ ret = vb2_core_reqbufs(q, fileio->memory, 0, &fileio->count);
26122637 if (ret)
26132638 goto err_kfree;
26142639
@@ -2665,7 +2690,7 @@ static int __vb2_init_fileio(struct vb2_queue *q, int read) · 50 unmodified lines
26652690
26662691 err_reqbufs:
26672692 fileio->count = 0;
2668− vb2_core_reqbufs(q, fileio->memory, &fileio->count);
2693+ vb2_core_reqbufs(q, fileio->memory, 0, &fileio->count);
26692694
26702695 err_kfree:
26712696 q->fileio = NULL;
@@ -2685,7 +2710,7 @@ static int __vb2_cleanup_fileio(struct vb2_queue *q) · 13 unmodified lines
26852710 vb2_core_streamoff(q, q->type);
26862711 q->fileio = NULL;
26872712 fileio->count = 0;
2688− vb2_core_reqbufs(q, fileio->memory, &fileio->count);
2713+ vb2_core_reqbufs(q, fileio->memory, 0, &fileio->count);
26892714 kfree(fileio);
26902715 dprintk(q, 3, "file io emulator closed\n");
26912716 }
drivers/media/common/videobuf2/videobuf2-v4l2.c+3−2
@@ -697,7 +697,7 @@ int vb2_reqbufs(struct vb2_queue *q, struct v4l2_requestbuffers *req) · 696 unmodified lines
697697 int ret = vb2_verify_memory_type(q, req->memory, req->type);
698698
699699 fill_buf_caps(q, &req->capabilities);
700− return ret ? ret : vb2_core_reqbufs(q, req->memory, &req->count);
700+ return ret ? ret : vb2_core_reqbufs(q, req->memory, 0, &req->count);
701701 }
702702 EXPORT_SYMBOL_GPL(vb2_reqbufs);
703703
@@ -772,6 +772,7 @@ int vb2_create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create) · 68 unmodified lines
772772 if (requested_sizes[i] == 0)
773773 return -EINVAL;
774774 return ret ? ret : vb2_core_create_bufs(q, create->memory,
775+ 0,
775776 &create->count,
776777 requested_planes,
777778 requested_sizes);
@@ -974,7 +975,7 @@ int vb2_ioctl_reqbufs(struct file *file, void *priv, · 196 unmodified lines
974975 return res;
975976 if (vb2_queue_is_busy(vdev, file))
976977 return -EBUSY;
977− res = vb2_core_reqbufs(vdev->queue, p->memory, &p->count);
978+ res = vb2_core_reqbufs(vdev->queue, p->memory, 0, &p->count);
978979 /* If count == 0, then the owner has released all buffers and he
979980 is no longer owner of the queue. Otherwise we have a new owner. */
980981 if (res == 0)
drivers/media/dvb-core/dvb_vb2.c+1−1
@@ -342,7 +342,7 @@ int dvb_vb2_reqbufs(struct dvb_vb2_ctx *ctx, struct dmx_requestbuffers *req) · 341 unmodified lines
342342
343343 ctx->buf_siz = req->size;
344344 ctx->buf_cnt = req->count;
345− ret = vb2_core_reqbufs(&ctx->vb_q, VB2_MEMORY_MMAP, &req->count);
345+ ret = vb2_core_reqbufs(&ctx->vb_q, VB2_MEMORY_MMAP, 0, &req->count);
346346 if (ret) {
347347 ctx->state = DVB_VB2_STATE_NONE;
348348 dprintk(1, "[%s] count=%d size=%d errno=%d\n", ctx->name,
include/media/videobuf2-core.h+8−2
@@ -504,6 +504,8 @@ struct vb2_buf_ops { · 503 unmodified lines
504504 * @allow_cache_hints: when set user-space can pass cache management hints in
505505 * order to skip cache flush/invalidation on ->prepare() or/and
506506 * ->finish().
507+ * @non_coherent_mem: when set queue will attempt to allocate buffers using
508+ * non-coherent memory.
507509 * @lock: pointer to a mutex that protects the &struct vb2_queue. The
508510 * driver can set this to a mutex to let the v4l2 core serialize
509511 * the queuing ioctls. If the driver wants to handle locking
@@ -583,6 +585,7 @@ struct vb2_queue { · 73 unmodified lines
583585 unsigned int uses_qbuf:1;
584586 unsigned int uses_requests:1;
585587 unsigned int allow_cache_hints:1;
588+ unsigned int non_coherent_mem:1;
586589
587590 struct mutex *lock;
588591 void *owner;
@@ -748,6 +751,8 @@ void vb2_core_querybuf(struct vb2_queue *q, unsigned int index, void *pb); · 159 unmodified lines
748751 * vb2_core_reqbufs() - Initiate streaming.
749752 * @q: pointer to &struct vb2_queue with videobuf2 queue.
750753 * @memory: memory type, as defined by &enum vb2_memory.
754+ * @flags: auxiliary queue/buffer management flags. Currently, the only
755+ * used flag is %V4L2_MEMORY_FLAG_NON_COHERENT.
751756 * @count: requested buffer count.
752757 *
753758 * Videobuf2 core helper to implement VIDIOC_REQBUF() operation. It is called
@@ -772,12 +777,13 @@ void vb2_core_querybuf(struct vb2_queue *q, unsigned int index, void *pb); · 18 unmodified lines
772777 * Return: returns zero on success; an error code otherwise.
773778 */
774779 int vb2_core_reqbufs(struct vb2_queue *q, enum vb2_memory memory,
775− unsigned int *count);
780+ unsigned int flags, unsigned int *count);
776781
777782 /**
778783 * vb2_core_create_bufs() - Allocate buffers and any required auxiliary structs
779784 * @q: pointer to &struct vb2_queue with videobuf2 queue.
780785 * @memory: memory type, as defined by &enum vb2_memory.
786+ * @flags: auxiliary queue/buffer management flags.
781787 * @count: requested buffer count.
782788 * @requested_planes: number of planes requested.
783789 * @requested_sizes: array with the size of the planes.
@@ -795,7 +801,7 @@ int vb2_core_reqbufs(struct vb2_queue *q, enum vb2_memory memory, · 11 unmodified lines
795801 * Return: returns zero on success; an error code otherwise.
796802 */
797803 int vb2_core_create_bufs(struct vb2_queue *q, enum vb2_memory memory,
798− unsigned int *count,
804+ unsigned int flags, unsigned int *count,
799805 unsigned int requested_planes,
800806 const unsigned int requested_sizes[]);
801807