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
738}738}
739EXPORT_SYMBOL(vb2_verify_memory_type);739EXPORT_SYMBOL(vb2_verify_memory_type);
740 740
741static 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
750static 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
741int vb2_core_reqbufs(struct vb2_queue *q, enum vb2_memory memory,759int vb2_core_reqbufs(struct vb2_queue *q, enum vb2_memory memory,
742 unsigned int *count)760 unsigned int flags, unsigned int *count)
743{761{
744 unsigned int num_buffers, allocated_buffers, num_planes = 0;762 unsigned int num_buffers, allocated_buffers, num_planes = 0;
745 unsigned plane_sizes[VB2_MAX_PLANES] = { };763 unsigned plane_sizes[VB2_MAX_PLANES] = { };
764 bool non_coherent_mem = false;
746 unsigned int i;765 unsigned int i;
747 int ret;766 int ret;
748 767
@@ -757,7 +776,8 @@ int vb2_core_reqbufs(struct vb2_queue *q, enum vb2_memory memory, · 8 unmodified lines
757 }776 }
758 777
759 if (*count == 0 || q->num_buffers != 0 ||778 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)) {
761 /*781 /*
762 * We already have buffers allocated, so first check if they782 * We already have buffers allocated, so first check if they
763 * are not in use and can be freed.783 * 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
794 num_buffers = min_t(unsigned int, num_buffers, VB2_MAX_FRAME);814 num_buffers = min_t(unsigned int, num_buffers, VB2_MAX_FRAME);
795 memset(q->alloc_devs, 0, sizeof(q->alloc_devs));815 memset(q->alloc_devs, 0, sizeof(q->alloc_devs));
796 q->memory = memory;816 q->memory = memory;
817 set_queue_coherency(q, non_coherent_mem);
797 818
798 /*819 /*
799 * Ask the driver how many buffers and planes per buffer it requires.820 * 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
878EXPORT_SYMBOL_GPL(vb2_core_reqbufs);899EXPORT_SYMBOL_GPL(vb2_core_reqbufs);
879 900
880int vb2_core_create_bufs(struct vb2_queue *q, enum vb2_memory memory,901int vb2_core_create_bufs(struct vb2_queue *q, enum vb2_memory memory,
881 unsigned int *count,902 unsigned int flags, unsigned int *count,
882 unsigned int requested_planes,903 unsigned int requested_planes,
883 const unsigned int requested_sizes[])904 const unsigned int requested_sizes[])
884{905{
885 unsigned int num_planes = 0, num_buffers, allocated_buffers;906 unsigned int num_planes = 0, num_buffers, allocated_buffers;
886 unsigned plane_sizes[VB2_MAX_PLANES] = { };907 unsigned plane_sizes[VB2_MAX_PLANES] = { };
908 bool non_coherent_mem = false;
887 int ret;909 int ret;
888 910
889 if (q->num_buffers == VB2_MAX_FRAME) {911 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
899 memset(q->alloc_devs, 0, sizeof(q->alloc_devs));921 memset(q->alloc_devs, 0, sizeof(q->alloc_devs));
900 q->memory = memory;922 q->memory = memory;
901 q->waiting_for_buffers = !q->is_output;923 q->waiting_for_buffers = !q->is_output;
924 set_queue_coherency(q, non_coherent_mem);
902 } else {925 } else {
903 if (q->memory != memory) {926 if (q->memory != memory) {
904 dprintk(q, 1, "memory model mismatch\n");927 dprintk(q, 1, "memory model mismatch\n");
905 return -EINVAL;928 return -EINVAL;
906 }929 }
930 if (!verify_coherency_flags(q, non_coherent_mem))
931 return -EINVAL;
907 }932 }
908 933
909 num_buffers = min(*count, VB2_MAX_FRAME - q->num_buffers);934 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
2608 fileio->memory = VB2_MEMORY_MMAP;2633 fileio->memory = VB2_MEMORY_MMAP;
2609 fileio->type = q->type;2634 fileio->type = q->type;
2610 q->fileio = fileio;2635 q->fileio = fileio;
2611 ret = vb2_core_reqbufs(q, fileio->memory, &fileio->count);2636 ret = vb2_core_reqbufs(q, fileio->memory, 0, &fileio->count);
2612 if (ret)2637 if (ret)
2613 goto err_kfree;2638 goto err_kfree;
2614 2639
@@ -2665,7 +2690,7 @@ static int __vb2_init_fileio(struct vb2_queue *q, int read) · 50 unmodified lines
2665 2690
2666err_reqbufs:2691err_reqbufs:
2667 fileio->count = 0;2692 fileio->count = 0;
2668 vb2_core_reqbufs(q, fileio->memory, &fileio->count);2693 vb2_core_reqbufs(q, fileio->memory, 0, &fileio->count);
2669 2694
2670err_kfree:2695err_kfree:
2671 q->fileio = NULL;2696 q->fileio = NULL;
@@ -2685,7 +2710,7 @@ static int __vb2_cleanup_fileio(struct vb2_queue *q) · 13 unmodified lines
2685 vb2_core_streamoff(q, q->type);2710 vb2_core_streamoff(q, q->type);
2686 q->fileio = NULL;2711 q->fileio = NULL;
2687 fileio->count = 0;2712 fileio->count = 0;
2688 vb2_core_reqbufs(q, fileio->memory, &fileio->count);2713 vb2_core_reqbufs(q, fileio->memory, 0, &fileio->count);
2689 kfree(fileio);2714 kfree(fileio);
2690 dprintk(q, 3, "file io emulator closed\n");2715 dprintk(q, 3, "file io emulator closed\n");
2691 }2716 }
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
697 int ret = vb2_verify_memory_type(q, req->memory, req->type);697 int ret = vb2_verify_memory_type(q, req->memory, req->type);
698 698
699 fill_buf_caps(q, &req->capabilities);699 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);
701}701}
702EXPORT_SYMBOL_GPL(vb2_reqbufs);702EXPORT_SYMBOL_GPL(vb2_reqbufs);
703 703
@@ -772,6 +772,7 @@ int vb2_create_bufs(struct vb2_queue *q, struct v4l2_create_buffers *create) · 68 unmodified lines
772 if (requested_sizes[i] == 0)772 if (requested_sizes[i] == 0)
773 return -EINVAL;773 return -EINVAL;
774 return ret ? ret : vb2_core_create_bufs(q, create->memory,774 return ret ? ret : vb2_core_create_bufs(q, create->memory,
775 0,
775 &create->count,776 &create->count,
776 requested_planes,777 requested_planes,
777 requested_sizes);778 requested_sizes);
@@ -974,7 +975,7 @@ int vb2_ioctl_reqbufs(struct file *file, void *priv, · 196 unmodified lines
974 return res;975 return res;
975 if (vb2_queue_is_busy(vdev, file))976 if (vb2_queue_is_busy(vdev, file))
976 return -EBUSY;977 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);
978 /* If count == 0, then the owner has released all buffers and he979 /* If count == 0, then the owner has released all buffers and he
979 is no longer owner of the queue. Otherwise we have a new owner. */980 is no longer owner of the queue. Otherwise we have a new owner. */
980 if (res == 0)981 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
342 342
343 ctx->buf_siz = req->size;343 ctx->buf_siz = req->size;
344 ctx->buf_cnt = req->count;344 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);
346 if (ret) {346 if (ret) {
347 ctx->state = DVB_VB2_STATE_NONE;347 ctx->state = DVB_VB2_STATE_NONE;
348 dprintk(1, "[%s] count=%d size=%d errno=%d\n", ctx->name,348 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
504 * @allow_cache_hints: when set user-space can pass cache management hints in504 * @allow_cache_hints: when set user-space can pass cache management hints in
505 * order to skip cache flush/invalidation on ->prepare() or/and505 * order to skip cache flush/invalidation on ->prepare() or/and
506 * ->finish().506 * ->finish().
507 * @non_coherent_mem: when set queue will attempt to allocate buffers using
508 * non-coherent memory.
507 * @lock: pointer to a mutex that protects the &struct vb2_queue. The509 * @lock: pointer to a mutex that protects the &struct vb2_queue. The
508 * driver can set this to a mutex to let the v4l2 core serialize510 * driver can set this to a mutex to let the v4l2 core serialize
509 * the queuing ioctls. If the driver wants to handle locking511 * the queuing ioctls. If the driver wants to handle locking
@@ -583,6 +585,7 @@ struct vb2_queue { · 73 unmodified lines
583 unsigned int uses_qbuf:1;585 unsigned int uses_qbuf:1;
584 unsigned int uses_requests:1;586 unsigned int uses_requests:1;
585 unsigned int allow_cache_hints:1;587 unsigned int allow_cache_hints:1;
588 unsigned int non_coherent_mem:1;
586 589
587 struct mutex *lock;590 struct mutex *lock;
588 void *owner;591 void *owner;
@@ -748,6 +751,8 @@ void vb2_core_querybuf(struct vb2_queue *q, unsigned int index, void *pb); · 159 unmodified lines
748 * vb2_core_reqbufs() - Initiate streaming.751 * vb2_core_reqbufs() - Initiate streaming.
749 * @q: pointer to &struct vb2_queue with videobuf2 queue.752 * @q: pointer to &struct vb2_queue with videobuf2 queue.
750 * @memory: memory type, as defined by &enum vb2_memory.753 * @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.
751 * @count: requested buffer count.756 * @count: requested buffer count.
752 *757 *
753 * Videobuf2 core helper to implement VIDIOC_REQBUF() operation. It is called758 * 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
772 * Return: returns zero on success; an error code otherwise.777 * Return: returns zero on success; an error code otherwise.
773 */778 */
774int vb2_core_reqbufs(struct vb2_queue *q, enum vb2_memory memory,779int vb2_core_reqbufs(struct vb2_queue *q, enum vb2_memory memory,
775 unsigned int *count);780 unsigned int flags, unsigned int *count);
776 781
777/**782/**
778 * vb2_core_create_bufs() - Allocate buffers and any required auxiliary structs783 * vb2_core_create_bufs() - Allocate buffers and any required auxiliary structs
779 * @q: pointer to &struct vb2_queue with videobuf2 queue.784 * @q: pointer to &struct vb2_queue with videobuf2 queue.
780 * @memory: memory type, as defined by &enum vb2_memory.785 * @memory: memory type, as defined by &enum vb2_memory.
786 * @flags: auxiliary queue/buffer management flags.
781 * @count: requested buffer count.787 * @count: requested buffer count.
782 * @requested_planes: number of planes requested.788 * @requested_planes: number of planes requested.
783 * @requested_sizes: array with the size of the planes.789 * @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
795 * Return: returns zero on success; an error code otherwise.801 * Return: returns zero on success; an error code otherwise.
796 */802 */
797int vb2_core_create_bufs(struct vb2_queue *q, enum vb2_memory memory,803int vb2_core_create_bufs(struct vb2_queue *q, enum vb2_memory memory,
798 unsigned int *count,804 unsigned int flags, unsigned int *count,
799 unsigned int requested_planes,805 unsigned int requested_planes,
800 const unsigned int requested_sizes[]);806 const unsigned int requested_sizes[]);
801 807