1 From ea4f926ea7fd76d0eeb4825bece0e3eaf36f3b0e Mon Sep 17 00:00:00 2001
2 From: Dave Stevenson <dave.stevenson@raspberrypi.org>
3 Date: Tue, 25 Sep 2018 16:07:55 +0100
4 Subject: [PATCH] staging: mmal-vchiq: Use vc-sm-cma to support zero
7 With the vc-sm-cma driver we can support zero copy of buffers between
8 the kernel and VPU. Add this support to mmal-vchiq.
10 Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.com>
12 .../staging/vc04_services/vchiq-mmal/Kconfig | 1 +
13 .../vc04_services/vchiq-mmal/mmal-common.h | 4 ++
14 .../vc04_services/vchiq-mmal/mmal-vchiq.c | 65 ++++++++++++++++++-
15 .../vc04_services/vchiq-mmal/mmal-vchiq.h | 1 +
16 4 files changed, 69 insertions(+), 2 deletions(-)
18 --- a/drivers/staging/vc04_services/vchiq-mmal/Kconfig
19 +++ b/drivers/staging/vc04_services/vchiq-mmal/Kconfig
21 config BCM2835_VCHIQ_MMAL
22 tristate "BCM2835 MMAL VCHIQ service"
23 depends on BCM2835_VCHIQ
24 + select BCM_VC_SM_CMA
26 Enables the MMAL API over VCHIQ interface as used for the
27 majority of the multimedia services on VideoCore.
28 --- a/drivers/staging/vc04_services/vchiq-mmal/mmal-common.h
29 +++ b/drivers/staging/vc04_services/vchiq-mmal/mmal-common.h
30 @@ -50,6 +50,10 @@ struct mmal_buffer {
32 struct mmal_msg_context *msg_context;
34 + struct dma_buf *dma_buf;/* Exported dmabuf fd from videobuf2 */
35 + void *vcsm_handle; /* VCSM handle having imported the dmabuf */
36 + u32 vc_handle; /* VC handle to that dmabuf */
38 u32 cmd; /* MMAL command. 0=data. */
41 --- a/drivers/staging/vc04_services/vchiq-mmal/mmal-vchiq.c
42 +++ b/drivers/staging/vc04_services/vchiq-mmal/mmal-vchiq.c
44 #include <media/videobuf2-vmalloc.h>
46 #include "mmal-common.h"
47 +#include "mmal-parameters.h"
48 #include "mmal-vchiq.h"
51 +#include "vc-sm-cma/vc_sm_knl.h"
53 * maximum number of components supported.
54 * This matches the maximum permitted by default on the VPU
55 @@ -416,8 +418,13 @@ buffer_from_host(struct vchiq_mmal_insta
58 m.u.buffer_from_host.buffer_header.cmd = 0;
59 - m.u.buffer_from_host.buffer_header.data =
60 - (u32)(unsigned long)buf->buffer;
61 + if (port->zero_copy) {
62 + m.u.buffer_from_host.buffer_header.data = buf->vc_handle;
64 + m.u.buffer_from_host.buffer_header.data =
65 + (u32)(unsigned long)buf->buffer;
68 m.u.buffer_from_host.buffer_header.alloc_size = buf->buffer_size;
69 if (port->type == MMAL_PORT_TYPE_OUTPUT) {
70 m.u.buffer_from_host.buffer_header.length = 0;
71 @@ -583,6 +590,22 @@ static void buffer_to_host_cb(struct vch
73 msg_context->u.bulk.status = msg->h.status;
75 + } else if (msg->u.buffer_from_host.is_zero_copy) {
77 + * Zero copy buffer, so nothing to do.
78 + * Copy buffer info and make callback.
80 + msg_context->u.bulk.buffer_used =
81 + msg->u.buffer_from_host.buffer_header.length;
82 + msg_context->u.bulk.mmal_flags =
83 + msg->u.buffer_from_host.buffer_header.flags;
84 + msg_context->u.bulk.dts =
85 + msg->u.buffer_from_host.buffer_header.dts;
86 + msg_context->u.bulk.pts =
87 + msg->u.buffer_from_host.buffer_header.pts;
88 + msg_context->u.bulk.cmd =
89 + msg->u.buffer_from_host.buffer_header.cmd;
91 } else if (msg->u.buffer_from_host.buffer_header.length == 0) {
93 if (msg->u.buffer_from_host.buffer_header.flags &
94 @@ -1528,6 +1551,9 @@ int vchiq_mmal_port_parameter_set(struct
96 mutex_unlock(&instance->vchiq_mutex);
98 + if (parameter == MMAL_PARAMETER_ZERO_COPY && !ret)
99 + port->zero_copy = !!(*(bool *)value);
103 EXPORT_SYMBOL_GPL(vchiq_mmal_port_parameter_set);
104 @@ -1696,6 +1722,31 @@ int vchiq_mmal_submit_buffer(struct vchi
105 unsigned long flags = 0;
109 + * We really want to do this in mmal_vchi_buffer_init but can't as
110 + * videobuf2 won't let us have the dmabuf there.
112 + if (port->zero_copy && buffer->dma_buf && !buffer->vcsm_handle) {
113 + pr_debug("%s: import dmabuf %p\n", __func__, buffer->dma_buf);
114 + ret = vc_sm_cma_import_dmabuf(buffer->dma_buf,
115 + &buffer->vcsm_handle);
117 + pr_err("%s: vc_sm_import_dmabuf_fd failed, ret %d\n",
122 + buffer->vc_handle = vc_sm_cma_int_handle(buffer->vcsm_handle);
123 + if (!buffer->vc_handle) {
124 + pr_err("%s: vc_sm_int_handle failed %d\n",
126 + vc_sm_cma_free(buffer->vcsm_handle);
129 + pr_debug("%s: import dmabuf %p - got vc handle %08X\n",
130 + __func__, buffer->dma_buf, buffer->vc_handle);
133 ret = buffer_from_host(instance, port, buffer);
134 if (ret == -EINVAL) {
135 /* Port is disabled. Queue for when it is enabled. */
136 @@ -1729,6 +1780,16 @@ int mmal_vchi_buffer_cleanup(struct mmal
137 release_msg_context(msg_context);
138 buf->msg_context = NULL;
140 + if (buf->vcsm_handle) {
143 + pr_debug("%s: vc_sm_cma_free on handle %p\n", __func__,
145 + ret = vc_sm_cma_free(buf->vcsm_handle);
147 + pr_err("%s: vcsm_free failed, ret %d\n", __func__, ret);
148 + buf->vcsm_handle = 0;
152 EXPORT_SYMBOL_GPL(mmal_vchi_buffer_cleanup);
153 --- a/drivers/staging/vc04_services/vchiq-mmal/mmal-vchiq.h
154 +++ b/drivers/staging/vc04_services/vchiq-mmal/mmal-vchiq.h
155 @@ -49,6 +49,7 @@ typedef void (*vchiq_mmal_buffer_cb)(
157 struct vchiq_mmal_port {
161 u32 type; /* port type, cached to use on port info set */
162 u32 index; /* port index, cached to use on port info set */