Skip to content

Commit 618917d

Browse files
6by9popcornmix
authored andcommitted
staging: mmal-vchiq: Use vc-sm-cma to support zero copy
With the vc-sm-cma driver we can support zero copy of buffers between the kernel and VPU. Add this support to mmal-vchiq. Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.com>
1 parent 1f6c3dd commit 618917d

4 files changed

Lines changed: 69 additions & 2 deletions

File tree

‎drivers/staging/vc04_services/vchiq-mmal/Kconfig‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
config BCM2835_VCHIQ_MMAL
22
tristate "BCM2835 MMAL VCHIQ service"
33
depends on BCM2835_VCHIQ
4+
select BCM_VC_SM_CMA
45
help
56
Enables the MMAL API over VCHIQ interface as used for the
67
majority of the multimedia services on VideoCore.

‎drivers/staging/vc04_services/vchiq-mmal/mmal-common.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ struct mmal_buffer {
5050

5151
struct mmal_msg_context *msg_context;
5252

53+
struct dma_buf *dma_buf;/* Exported dmabuf fd from videobuf2 */
54+
void *vcsm_handle; /* VCSM handle having imported the dmabuf */
55+
u32 vc_handle; /* VC handle to that dmabuf */
56+
5357
u32 cmd; /* MMAL command. 0=data. */
5458
unsigned long length;
5559
u32 mmal_flags;

‎drivers/staging/vc04_services/vchiq-mmal/mmal-vchiq.c‎

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,11 @@
2727
#include <media/videobuf2-vmalloc.h>
2828

2929
#include "mmal-common.h"
30+
#include "mmal-parameters.h"
3031
#include "mmal-vchiq.h"
3132
#include "mmal-msg.h"
3233

34+
#include "vc-sm-cma/vc_sm_knl.h"
3335
/*
3436
* maximum number of components supported.
3537
* This matches the maximum permitted by default on the VPU
@@ -419,8 +421,13 @@ buffer_from_host(struct vchiq_mmal_instance *instance,
419421

420422
/* buffer header */
421423
m.u.buffer_from_host.buffer_header.cmd = 0;
422-
m.u.buffer_from_host.buffer_header.data =
423-
(u32)(unsigned long)buf->buffer;
424+
if (port->zero_copy) {
425+
m.u.buffer_from_host.buffer_header.data = buf->vc_handle;
426+
} else {
427+
m.u.buffer_from_host.buffer_header.data =
428+
(u32)(unsigned long)buf->buffer;
429+
}
430+
424431
m.u.buffer_from_host.buffer_header.alloc_size = buf->buffer_size;
425432
if (port->type == MMAL_PORT_TYPE_OUTPUT) {
426433
m.u.buffer_from_host.buffer_header.length = 0;
@@ -586,6 +593,22 @@ static void buffer_to_host_cb(struct vchiq_mmal_instance *instance,
586593

587594
msg_context->u.bulk.status = msg->h.status;
588595

596+
} else if (msg->u.buffer_from_host.is_zero_copy) {
597+
/*
598+
* Zero copy buffer, so nothing to do.
599+
* Copy buffer info and make callback.
600+
*/
601+
msg_context->u.bulk.buffer_used =
602+
msg->u.buffer_from_host.buffer_header.length;
603+
msg_context->u.bulk.mmal_flags =
604+
msg->u.buffer_from_host.buffer_header.flags;
605+
msg_context->u.bulk.dts =
606+
msg->u.buffer_from_host.buffer_header.dts;
607+
msg_context->u.bulk.pts =
608+
msg->u.buffer_from_host.buffer_header.pts;
609+
msg_context->u.bulk.cmd =
610+
msg->u.buffer_from_host.buffer_header.cmd;
611+
589612
} else if (msg->u.buffer_from_host.buffer_header.length == 0) {
590613
/* empty buffer */
591614
if (msg->u.buffer_from_host.buffer_header.flags &
@@ -1530,6 +1553,9 @@ int vchiq_mmal_port_parameter_set(struct vchiq_mmal_instance *instance,
15301553

15311554
mutex_unlock(&instance->vchiq_mutex);
15321555

1556+
if (parameter == MMAL_PARAMETER_ZERO_COPY && !ret)
1557+
port->zero_copy = !!(*(bool *)value);
1558+
15331559
return ret;
15341560
}
15351561
EXPORT_SYMBOL_GPL(vchiq_mmal_port_parameter_set);
@@ -1698,6 +1724,31 @@ int vchiq_mmal_submit_buffer(struct vchiq_mmal_instance *instance,
16981724
unsigned long flags = 0;
16991725
int ret;
17001726

1727+
/*
1728+
* We really want to do this in mmal_vchi_buffer_init but can't as
1729+
* videobuf2 won't let us have the dmabuf there.
1730+
*/
1731+
if (port->zero_copy && buffer->dma_buf && !buffer->vcsm_handle) {
1732+
pr_debug("%s: import dmabuf %p\n", __func__, buffer->dma_buf);
1733+
ret = vc_sm_cma_import_dmabuf(buffer->dma_buf,
1734+
&buffer->vcsm_handle);
1735+
if (ret) {
1736+
pr_err("%s: vc_sm_import_dmabuf_fd failed, ret %d\n",
1737+
__func__, ret);
1738+
return ret;
1739+
}
1740+
1741+
buffer->vc_handle = vc_sm_cma_int_handle(buffer->vcsm_handle);
1742+
if (!buffer->vc_handle) {
1743+
pr_err("%s: vc_sm_int_handle failed %d\n",
1744+
__func__, ret);
1745+
vc_sm_cma_free(buffer->vcsm_handle);
1746+
return ret;
1747+
}
1748+
pr_debug("%s: import dmabuf %p - got vc handle %08X\n",
1749+
__func__, buffer->dma_buf, buffer->vc_handle);
1750+
}
1751+
17011752
ret = buffer_from_host(instance, port, buffer);
17021753
if (ret == -EINVAL) {
17031754
/* Port is disabled. Queue for when it is enabled. */
@@ -1731,6 +1782,16 @@ int mmal_vchi_buffer_cleanup(struct mmal_buffer *buf)
17311782
release_msg_context(msg_context);
17321783
buf->msg_context = NULL;
17331784

1785+
if (buf->vcsm_handle) {
1786+
int ret;
1787+
1788+
pr_debug("%s: vc_sm_cma_free on handle %p\n", __func__,
1789+
buf->vcsm_handle);
1790+
ret = vc_sm_cma_free(buf->vcsm_handle);
1791+
if (ret)
1792+
pr_err("%s: vcsm_free failed, ret %d\n", __func__, ret);
1793+
buf->vcsm_handle = 0;
1794+
}
17341795
return 0;
17351796
}
17361797
EXPORT_SYMBOL_GPL(mmal_vchi_buffer_cleanup);

‎drivers/staging/vc04_services/vchiq-mmal/mmal-vchiq.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ typedef void (*vchiq_mmal_buffer_cb)(
4949

5050
struct vchiq_mmal_port {
5151
u32 enabled:1;
52+
u32 zero_copy:1;
5253
u32 handle;
5354
u32 type; /* port type, cached to use on port info set */
5455
u32 index; /* port index, cached to use on port info set */

0 commit comments

Comments
 (0)