Skip to content

Commit b1ab7a0

Browse files
6by9popcornmix
authored andcommitted
staging: vc04_services: Add new vc-sm-cma driver
Add Broadcom VideoCore Shared Memory support. This new driver allows contiguous memory blocks to be imported into the VideoCore VPU memory map, and manages the lifetime of those objects, only releasing the source dmabuf once the VPU has confirmed it has finished with it. Signed-off-by: Dave Stevenson <[email protected]> staging: vcsm-cma: Fix memory leak from not detaching dmabuf When importing there was a missing call to detach the buffer, so each import leaked the sg table entry. Actually the release process for both locally allocated and imported buffers is identical, so fix them to both use the same function. Signed-off-by: Dave Stevenson <[email protected]> staging/vc-sm-cma: Avoid log spamming on Pi0/1 over cache alias. Pi 0/1 use the 0x80000000 cache alias as the ARM also sees the world through the VPU L2 cache. vc-sm-cma was trying to ensure it was in an uncached alias (0xc), and complaining on every allocation if it weren't. Reduce this logging. Signed-off-by: Dave Stevenson <[email protected]> vc-sm-cma: Restore correct cache maintainance operations We have been using the more expensive flush operations rather than invalidate and clean since kernel rpi-5.9.y These are exposed with: 52f1453 Re-expose some dmi APIs for use in VCSM But I believe that commit was dropped when (non-cma) vc-sm was dropped, and didn't get updated when the commit was restored Signed-off-by: Dom Cobley <[email protected]> staging: vc04_services: Fix clang14 warning Insert a break to fix a fallthrough warning from clang14. Since the fallthrough was to another break, this is a cosmetic change. See: raspberrypi#5078 Signed-off-by: Phil Elwell <[email protected]> vc04_services/vc-sm-cma: Handle upstream require vchiq_instance to be passed around vc04_services/vc-sm-cma: Switch one-bit bitfields to bool Clang 16 warns: ../drivers/staging/vc04_services/vc-sm-cma/vc_sm.c:816:19: warning: implicit truncation from 'int' to a one-bit wide bit-field changes value from 1 to -1 [-Wsingle-bit-bitfield-constant-conversion] buffer->imported = 1; ^ ~ ../drivers/staging/vc04_services/vc-sm-cma/vc_sm.c:822:17: warning: implicit truncation from 'int' to a one-bit wide bit-field changes value from 1 to -1 [-Wsingle-bit-bitfield-constant-conversion] buffer->in_use = 1; ^ ~ 2 warnings generated. Signed-off-by: Alexander Winkowski <[email protected]> vc04_services: vcsm-cma: Detach from the correct dmabuf Commit d3292da ("dma-buf: Make locking consistent in dma_buf_detach()") added checking that the same dmabuf for which dma_buf_attach was called is passed into dma_buf_detach, which flagged up that vcsm-cma was passing in the wrong dmabuf. Correct this so that we don't get the WARN on every dma_buf release. Signed-off-by: Dave Stevenson <[email protected]>
1 parent 40f9fb9 commit b1ab7a0

File tree

12 files changed

+2830
-0
lines changed

12 files changed

+2830
-0
lines changed

drivers/staging/vc04_services/Kconfig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ source "drivers/staging/vc04_services/bcm2835-audio/Kconfig"
5050

5151
source "drivers/staging/vc04_services/bcm2835-camera/Kconfig"
5252

53+
source "drivers/staging/vc04_services/vc-sm-cma/Kconfig"
54+
5355
source "drivers/staging/vc04_services/vchiq-mmal/Kconfig"
5456

5557
endif

drivers/staging/vc04_services/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ endif
1414
obj-$(CONFIG_SND_BCM2835) += bcm2835-audio/
1515
obj-$(CONFIG_VIDEO_BCM2835) += bcm2835-camera/
1616
obj-$(CONFIG_BCM2835_VCHIQ_MMAL) += vchiq-mmal/
17+
obj-$(CONFIG_BCM_VC_SM_CMA) += vc-sm-cma/
1718

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
3+
/*
4+
* Copyright 2019 Raspberry Pi (Trading) Ltd. All rights reserved.
5+
*
6+
* Based on vmcs_sm_ioctl.h Copyright Broadcom Corporation.
7+
*/
8+
9+
#ifndef __VC_SM_CMA_IOCTL_H
10+
#define __VC_SM_CMA_IOCTL_H
11+
12+
/* ---- Include Files ---------------------------------------------------- */
13+
14+
#if defined(__KERNEL__)
15+
#include <linux/types.h> /* Needed for standard types */
16+
#else
17+
#include <stdint.h>
18+
#endif
19+
20+
#include <linux/ioctl.h>
21+
22+
/* ---- Constants and Types ---------------------------------------------- */
23+
24+
#define VC_SM_CMA_RESOURCE_NAME 32
25+
#define VC_SM_CMA_RESOURCE_NAME_DEFAULT "sm-host-resource"
26+
27+
/* Type define used to create unique IOCTL number */
28+
#define VC_SM_CMA_MAGIC_TYPE 'J'
29+
30+
/* IOCTL commands on /dev/vc-sm-cma */
31+
enum vc_sm_cma_cmd_e {
32+
VC_SM_CMA_CMD_ALLOC = 0x5A, /* Start at 0x5A arbitrarily */
33+
34+
VC_SM_CMA_CMD_IMPORT_DMABUF,
35+
36+
VC_SM_CMA_CMD_CLEAN_INVALID2,
37+
38+
VC_SM_CMA_CMD_LAST /* Do not delete */
39+
};
40+
41+
/* Cache type supported, conveniently matches the user space definition in
42+
* user-vcsm.h.
43+
*/
44+
enum vc_sm_cma_cache_e {
45+
VC_SM_CMA_CACHE_NONE,
46+
VC_SM_CMA_CACHE_HOST,
47+
VC_SM_CMA_CACHE_VC,
48+
VC_SM_CMA_CACHE_BOTH,
49+
};
50+
51+
/* IOCTL Data structures */
52+
struct vc_sm_cma_ioctl_alloc {
53+
/* user -> kernel */
54+
__u32 size;
55+
__u32 num;
56+
__u32 cached; /* enum vc_sm_cma_cache_e */
57+
__u32 pad;
58+
__u8 name[VC_SM_CMA_RESOURCE_NAME];
59+
60+
/* kernel -> user */
61+
__s32 handle;
62+
__u32 vc_handle;
63+
__u64 dma_addr;
64+
};
65+
66+
struct vc_sm_cma_ioctl_import_dmabuf {
67+
/* user -> kernel */
68+
__s32 dmabuf_fd;
69+
__u32 cached; /* enum vc_sm_cma_cache_e */
70+
__u8 name[VC_SM_CMA_RESOURCE_NAME];
71+
72+
/* kernel -> user */
73+
__s32 handle;
74+
__u32 vc_handle;
75+
__u32 size;
76+
__u32 pad;
77+
__u64 dma_addr;
78+
};
79+
80+
/*
81+
* Cache functions to be set to struct vc_sm_cma_ioctl_clean_invalid2
82+
* invalidate_mode.
83+
*/
84+
#define VC_SM_CACHE_OP_NOP 0x00
85+
#define VC_SM_CACHE_OP_INV 0x01
86+
#define VC_SM_CACHE_OP_CLEAN 0x02
87+
#define VC_SM_CACHE_OP_FLUSH 0x03
88+
89+
struct vc_sm_cma_ioctl_clean_invalid2 {
90+
__u32 op_count;
91+
__u32 pad;
92+
struct vc_sm_cma_ioctl_clean_invalid_block {
93+
__u32 invalidate_mode;
94+
__u32 block_count;
95+
void * __user start_address;
96+
__u32 block_size;
97+
__u32 inter_block_stride;
98+
} s[0];
99+
};
100+
101+
/* IOCTL numbers */
102+
#define VC_SM_CMA_IOCTL_MEM_ALLOC\
103+
_IOR(VC_SM_CMA_MAGIC_TYPE, VC_SM_CMA_CMD_ALLOC,\
104+
struct vc_sm_cma_ioctl_alloc)
105+
106+
#define VC_SM_CMA_IOCTL_MEM_IMPORT_DMABUF\
107+
_IOR(VC_SM_CMA_MAGIC_TYPE, VC_SM_CMA_CMD_IMPORT_DMABUF,\
108+
struct vc_sm_cma_ioctl_import_dmabuf)
109+
110+
#define VC_SM_CMA_IOCTL_MEM_CLEAN_INVALID2\
111+
_IOR(VC_SM_CMA_MAGIC_TYPE, VC_SM_CMA_CMD_CLEAN_INVALID2,\
112+
struct vc_sm_cma_ioctl_clean_invalid2)
113+
114+
#endif /* __VC_SM_CMA_IOCTL_H */
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
config BCM_VC_SM_CMA
2+
tristate "VideoCore Shared Memory (CMA) driver"
3+
select BCM2835_VCHIQ
4+
select RBTREE
5+
select DMA_SHARED_BUFFER
6+
help
7+
Say Y here to enable the shared memory interface that
8+
supports sharing dmabufs with VideoCore.
9+
This operates over the VCHIQ interface to a service
10+
running on VideoCore.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
ccflags-y += \
2+
-I$(srctree)/$(src)/../ \
3+
-I$(srctree)/$(src)/../interface/vchiq_arm\
4+
-I$(srctree)/$(src)/../include
5+
6+
ccflags-y += \
7+
-D__VCCOREVER__=0
8+
9+
vc-sm-cma-$(CONFIG_BCM_VC_SM_CMA) := \
10+
vc_sm.o vc_sm_cma_vchi.o
11+
12+
obj-$(CONFIG_BCM_VC_SM_CMA) += vc-sm-cma.o
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
No currently outstanding tasks except some clean-up.

0 commit comments

Comments
 (0)