Skip to content

Commit 9dc9a95

Browse files
6by9popcornmix
authored andcommitted
staging: vc04_services: 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 vchiq-mmal. Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.org> staging: vc-sm-cma: Use a void* pointer as the handle within the kernel The driver was using an unsigned int as the handle to the outside world, and doing a nasty cast to the struct dmabuf when handed it back. This breaks badly with a 64 bit kernel where the pointer doesn't fit in an unsigned int. Switch to using a void* within the kernel. Reality is that it is a struct dma_buf*, but advertising it as such to other drivers seems to encourage the use of it as such, and I'm not sure on the implications of that. Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.org>
1 parent 5c4c033 commit 9dc9a95

4 files changed

Lines changed: 70 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
@@ -2,6 +2,7 @@ config BCM2835_VCHIQ_MMAL
22
tristate "BCM2835 MMAL VCHIQ service"
33
depends on (ARCH_BCM2835 || COMPILE_TEST)
44
select BCM2835_VCHIQ
5+
select BCM_VC_SM_CMA
56
help
67
Enables the MMAL API over VCHIQ as used for the
78
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: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,12 @@
2626
#include <media/videobuf2-vmalloc.h>
2727

2828
#include "mmal-common.h"
29+
#include "mmal-parameters.h"
2930
#include "mmal-vchiq.h"
3031
#include "mmal-msg.h"
3132

33+
#include "vc-sm-cma/vc_sm_knl.h"
34+
3235
#define USE_VCHIQ_ARM
3336
#include "interface/vchi/vchi.h"
3437

@@ -424,8 +427,13 @@ buffer_from_host(struct vchiq_mmal_instance *instance,
424427

425428
/* buffer header */
426429
m.u.buffer_from_host.buffer_header.cmd = 0;
427-
m.u.buffer_from_host.buffer_header.data =
428-
(u32)(unsigned long)buf->buffer;
430+
if (port->zero_copy) {
431+
m.u.buffer_from_host.buffer_header.data = buf->vc_handle;
432+
} else {
433+
m.u.buffer_from_host.buffer_header.data =
434+
(u32)(unsigned long)buf->buffer;
435+
}
436+
429437
m.u.buffer_from_host.buffer_header.alloc_size = buf->buffer_size;
430438
if (port->type == MMAL_PORT_TYPE_OUTPUT) {
431439
m.u.buffer_from_host.buffer_header.length = 0;
@@ -590,6 +598,22 @@ static void buffer_to_host_cb(struct vchiq_mmal_instance *instance,
590598

591599
msg_context->u.bulk.status = msg->h.status;
592600

601+
} else if (msg->u.buffer_from_host.is_zero_copy) {
602+
/*
603+
* Zero copy buffer, so nothing to do.
604+
* Copy buffer info and make callback.
605+
*/
606+
msg_context->u.bulk.buffer_used =
607+
msg->u.buffer_from_host.buffer_header.length;
608+
msg_context->u.bulk.mmal_flags =
609+
msg->u.buffer_from_host.buffer_header.flags;
610+
msg_context->u.bulk.dts =
611+
msg->u.buffer_from_host.buffer_header.dts;
612+
msg_context->u.bulk.pts =
613+
msg->u.buffer_from_host.buffer_header.pts;
614+
msg_context->u.bulk.cmd =
615+
msg->u.buffer_from_host.buffer_header.cmd;
616+
593617
} else if (msg->u.buffer_from_host.buffer_header.length == 0) {
594618
/* empty buffer */
595619
if (msg->u.buffer_from_host.buffer_header.flags &
@@ -1537,6 +1561,9 @@ int vchiq_mmal_port_parameter_set(struct vchiq_mmal_instance *instance,
15371561

15381562
mutex_unlock(&instance->vchiq_mutex);
15391563

1564+
if (parameter == MMAL_PARAMETER_ZERO_COPY && !ret)
1565+
port->zero_copy = !!(*(bool *)value);
1566+
15401567
return ret;
15411568
}
15421569
EXPORT_SYMBOL_GPL(vchiq_mmal_port_parameter_set);
@@ -1705,6 +1732,31 @@ int vchiq_mmal_submit_buffer(struct vchiq_mmal_instance *instance,
17051732
unsigned long flags = 0;
17061733
int ret;
17071734

1735+
/*
1736+
* We really want to do this in mmal_vchi_buffer_init but can't as
1737+
* videobuf2 won't let us have the dmabuf there.
1738+
*/
1739+
if (port->zero_copy && buffer->dma_buf && !buffer->vcsm_handle) {
1740+
pr_debug("%s: import dmabuf %p\n", __func__, buffer->dma_buf);
1741+
ret = vc_sm_cma_import_dmabuf(buffer->dma_buf,
1742+
&buffer->vcsm_handle);
1743+
if (ret) {
1744+
pr_err("%s: vc_sm_import_dmabuf_fd failed, ret %d\n",
1745+
__func__, ret);
1746+
return ret;
1747+
}
1748+
1749+
buffer->vc_handle = vc_sm_cma_int_handle(buffer->vcsm_handle);
1750+
if (!buffer->vc_handle) {
1751+
pr_err("%s: vc_sm_int_handle failed %d\n",
1752+
__func__, ret);
1753+
vc_sm_cma_free(buffer->vcsm_handle);
1754+
return ret;
1755+
}
1756+
pr_debug("%s: import dmabuf %p - got vc handle %08X\n",
1757+
__func__, buffer->dma_buf, buffer->vc_handle);
1758+
}
1759+
17081760
ret = buffer_from_host(instance, port, buffer);
17091761
if (ret == -EINVAL) {
17101762
/* Port is disabled. Queue for when it is enabled. */
@@ -1738,6 +1790,16 @@ int mmal_vchi_buffer_cleanup(struct mmal_buffer *buf)
17381790
release_msg_context(msg_context);
17391791
buf->msg_context = NULL;
17401792

1793+
if (buf->vcsm_handle) {
1794+
int ret;
1795+
1796+
pr_debug("%s: vc_sm_cma_free on handle %p\n", __func__,
1797+
buf->vcsm_handle);
1798+
ret = vc_sm_cma_free(buf->vcsm_handle);
1799+
if (ret)
1800+
pr_err("%s: vcsm_free failed, ret %d\n", __func__, ret);
1801+
buf->vcsm_handle = 0;
1802+
}
17411803
return 0;
17421804
}
17431805
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)