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 <dave.stevenson@raspberrypi.com>

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 <dave.stevenson@raspberrypi.com>

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 <dave.stevenson@raspberrypi.com>

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:
52f1453513 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 <popcornmix@gmail.com>

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: https://github.com/raspberrypi/linux/issues/5078

Signed-off-by: Phil Elwell <phil@raspberrypi.com>

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 <dereference23@outlook.com>

vc04_services: vcsm-cma: Detach from the correct dmabuf

Commit d3292daee3 ("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 <dave.stevenson@raspberrypi.com>

staging: vc04_services: vc-sm-cma: Remove deprecated header

The vchiq_connected.h header was removed in f875976ecf ("staging:
vc04_services: Drop vchiq_connected.[ch] files") to simplify the
implementation.

Update the vc_sm driver accordingly which can still use the same
functions through the vchiq_arm.h header declarations.

Fixes: b1ab7a05eb6c ("staging: vc04_services: Add new vc-sm-cma driver")
Signed-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com>

staging: vc04_services: vc-sm-cma: Drop include Makefile directive

Drop the include directive. They can break the build, when one only
wants to build a subdirectory. Replace with "../" for the includes in
the vc_sm files instead.

The fix is equivalent to the four patches between 29d49a76c5
("staging: vc04_services: bcm2835-audio: Drop include Makefile
directive")...2529ca211402 ("staging: vc04_services: interface: Drop
include Makefile directive")

Fixes: b1ab7a05eb6c ("staging: vc04_services: Add new vc-sm-cma driver")
Suggested-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com>

staging: vc04_services: vc-sm-cma: Register with vchiq_bus_type

Register the vcsm rive with the vchiq_bus_type instead of useing the
platform driver/device.

Signed-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com>

staging: vc04_services: vc-sm-cma: Explicitly set DMA mask

The platform model originally handled the DMA mask. Now that
we are on the vchiq_bus we need to explicitly set this.

Signed-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com>

staging: vc04_services: vc-sm-cma: Use [map|unmap]_attachment_unlocked

lockdep throws warnings when using libcamera as buffers are
mapped and unmapped as the dmabuf->resv lock hasn't been taken.

Switch to using the _unlocked variants so that the framework takes
the lock.

https://github.com/raspberrypi/linux/issues/6814

Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.com>

staging: vc04_services: vc-sm-cma: Use a mutex instead of spinlock

There are no contexts where we should be calling the kernelid_map
IDR functions where we can't sleep, so switch from using a spinlock
to using a mutex.

https://github.com/raspberrypi/linux/issues/6815

Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.com>

staging: vc-sm-cma: Fix field-spanning write warning

Replace one-element array with flexible-array member to fix:

[   11.725017] ------------[ cut here ]------------
[   11.725038] memcpy: detected field-spanning write (size 4) of single field "hdr->body" at drivers/staging/vc04_services/vc-sm-cma/vc_sm_cma_vchi.c:130 (size 0)
[   11.725113] WARNING: CPU: 3 PID: 455 at drivers/staging/vc04_services/vc-sm-cma/vc_sm_cma_vchi.c:130 vc_vchi_cmd_create+0x1a8/0x1d0 [vc_sm_cma]

Signed-off-by: Juerg Haefliger <juerg.haefliger@canonical.com>
This commit is contained in:
Dave Stevenson
2020-10-08 18:49:52 +01:00
committed by Dom Cobley
parent 18931fe031
commit 7381f351fd
12 changed files with 2736 additions and 0 deletions

View File

@@ -50,6 +50,8 @@ source "drivers/staging/vc04_services/bcm2835-audio/Kconfig"
source "drivers/staging/vc04_services/bcm2835-camera/Kconfig"
source "drivers/staging/vc04_services/vc-sm-cma/Kconfig"
source "drivers/staging/vc04_services/vchiq-mmal/Kconfig"
endif

View File

@@ -14,4 +14,5 @@ endif
obj-$(CONFIG_SND_BCM2835) += bcm2835-audio/
obj-$(CONFIG_VIDEO_BCM2835) += bcm2835-camera/
obj-$(CONFIG_BCM2835_VCHIQ_MMAL) += vchiq-mmal/
obj-$(CONFIG_BCM_VC_SM_CMA) += vc-sm-cma/

View File

@@ -0,0 +1,114 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright 2019 Raspberry Pi (Trading) Ltd. All rights reserved.
*
* Based on vmcs_sm_ioctl.h Copyright Broadcom Corporation.
*/
#ifndef __VC_SM_CMA_IOCTL_H
#define __VC_SM_CMA_IOCTL_H
/* ---- Include Files ---------------------------------------------------- */
#if defined(__KERNEL__)
#include <linux/types.h> /* Needed for standard types */
#else
#include <stdint.h>
#endif
#include <linux/ioctl.h>
/* ---- Constants and Types ---------------------------------------------- */
#define VC_SM_CMA_RESOURCE_NAME 32
#define VC_SM_CMA_RESOURCE_NAME_DEFAULT "sm-host-resource"
/* Type define used to create unique IOCTL number */
#define VC_SM_CMA_MAGIC_TYPE 'J'
/* IOCTL commands on /dev/vc-sm-cma */
enum vc_sm_cma_cmd_e {
VC_SM_CMA_CMD_ALLOC = 0x5A, /* Start at 0x5A arbitrarily */
VC_SM_CMA_CMD_IMPORT_DMABUF,
VC_SM_CMA_CMD_CLEAN_INVALID2,
VC_SM_CMA_CMD_LAST /* Do not delete */
};
/* Cache type supported, conveniently matches the user space definition in
* user-vcsm.h.
*/
enum vc_sm_cma_cache_e {
VC_SM_CMA_CACHE_NONE,
VC_SM_CMA_CACHE_HOST,
VC_SM_CMA_CACHE_VC,
VC_SM_CMA_CACHE_BOTH,
};
/* IOCTL Data structures */
struct vc_sm_cma_ioctl_alloc {
/* user -> kernel */
__u32 size;
__u32 num;
__u32 cached; /* enum vc_sm_cma_cache_e */
__u32 pad;
__u8 name[VC_SM_CMA_RESOURCE_NAME];
/* kernel -> user */
__s32 handle;
__u32 vc_handle;
__u64 dma_addr;
};
struct vc_sm_cma_ioctl_import_dmabuf {
/* user -> kernel */
__s32 dmabuf_fd;
__u32 cached; /* enum vc_sm_cma_cache_e */
__u8 name[VC_SM_CMA_RESOURCE_NAME];
/* kernel -> user */
__s32 handle;
__u32 vc_handle;
__u32 size;
__u32 pad;
__u64 dma_addr;
};
/*
* Cache functions to be set to struct vc_sm_cma_ioctl_clean_invalid2
* invalidate_mode.
*/
#define VC_SM_CACHE_OP_NOP 0x00
#define VC_SM_CACHE_OP_INV 0x01
#define VC_SM_CACHE_OP_CLEAN 0x02
#define VC_SM_CACHE_OP_FLUSH 0x03
struct vc_sm_cma_ioctl_clean_invalid2 {
__u32 op_count;
__u32 pad;
struct vc_sm_cma_ioctl_clean_invalid_block {
__u32 invalidate_mode;
__u32 block_count;
void * __user start_address;
__u32 block_size;
__u32 inter_block_stride;
} s[0];
};
/* IOCTL numbers */
#define VC_SM_CMA_IOCTL_MEM_ALLOC\
_IOR(VC_SM_CMA_MAGIC_TYPE, VC_SM_CMA_CMD_ALLOC,\
struct vc_sm_cma_ioctl_alloc)
#define VC_SM_CMA_IOCTL_MEM_IMPORT_DMABUF\
_IOR(VC_SM_CMA_MAGIC_TYPE, VC_SM_CMA_CMD_IMPORT_DMABUF,\
struct vc_sm_cma_ioctl_import_dmabuf)
#define VC_SM_CMA_IOCTL_MEM_CLEAN_INVALID2\
_IOR(VC_SM_CMA_MAGIC_TYPE, VC_SM_CMA_CMD_CLEAN_INVALID2,\
struct vc_sm_cma_ioctl_clean_invalid2)
#endif /* __VC_SM_CMA_IOCTL_H */

View File

@@ -0,0 +1,10 @@
config BCM_VC_SM_CMA
tristate "VideoCore Shared Memory (CMA) driver"
select BCM2835_VCHIQ
select RBTREE
select DMA_SHARED_BUFFER
help
Say Y here to enable the shared memory interface that
supports sharing dmabufs with VideoCore.
This operates over the VCHIQ interface to a service
running on VideoCore.

View File

@@ -0,0 +1,7 @@
ccflags-y += \
-D__VCCOREVER__=0
vc-sm-cma-$(CONFIG_BCM_VC_SM_CMA) := \
vc_sm.o vc_sm_cma_vchi.o
obj-$(CONFIG_BCM_VC_SM_CMA) += vc-sm-cma.o

View File

@@ -0,0 +1 @@
No currently outstanding tasks except some clean-up.

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,84 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* VideoCore Shared Memory driver using CMA.
*
* Copyright: 2018, Raspberry Pi (Trading) Ltd
*
*/
#ifndef VC_SM_H
#define VC_SM_H
#include <linux/device.h>
#include <linux/dma-direction.h>
#include <linux/kref.h>
#include <linux/mm_types.h>
#include <linux/mutex.h>
#include <linux/rbtree.h>
#include <linux/sched.h>
#include <linux/shrinker.h>
#include <linux/types.h>
#include <linux/miscdevice.h>
#define VC_SM_MAX_NAME_LEN 32
enum vc_sm_vpu_mapping_state {
VPU_NOT_MAPPED,
VPU_MAPPED,
VPU_UNMAPPING
};
struct vc_sm_alloc_data {
unsigned long num_pages;
void *priv_virt;
struct sg_table *sg_table;
};
struct vc_sm_imported {
struct dma_buf *dma_buf;
struct dma_buf_attachment *attach;
struct sg_table *sgt;
};
struct vc_sm_buffer {
struct list_head global_buffer_list; /* Global list of buffers. */
/* Index in the kernel_id idr so that we can find the
* mmal_msg_context again when servicing the VCHI reply.
*/
int kernel_id;
size_t size;
/* Lock over all the following state for this buffer */
struct mutex lock;
struct list_head attachments;
char name[VC_SM_MAX_NAME_LEN];
bool in_use:1; /* Kernel is still using this resource */
bool imported:1; /* Imported dmabuf */
enum vc_sm_vpu_mapping_state vpu_state;
u32 vc_handle; /* VideoCore handle for this buffer */
int vpu_allocated; /*
* The VPU made this allocation. Release the
* local dma_buf when the VPU releases the
* resource.
*/
/* DMABUF related fields */
struct dma_buf *dma_buf;
dma_addr_t dma_addr;
void *cookie;
struct vc_sm_privdata_t *private;
union {
struct vc_sm_alloc_data alloc;
struct vc_sm_imported import;
};
};
#endif

View File

@@ -0,0 +1,511 @@
// SPDX-License-Identifier: GPL-2.0
/*
* VideoCore Shared Memory CMA allocator
*
* Copyright: 2018, Raspberry Pi (Trading) Ltd
* Copyright 2011-2012 Broadcom Corporation. All rights reserved.
*
* Based on vmcs_sm driver from Broadcom Corporation.
*
*/
/* ---- Include Files ----------------------------------------------------- */
#include <linux/completion.h>
#include <linux/kernel.h>
#include <linux/kthread.h>
#include <linux/list.h>
#include <linux/mutex.h>
#include <linux/semaphore.h>
#include <linux/slab.h>
#include <linux/types.h>
#include "vc_sm_cma_vchi.h"
#define VC_SM_VER 1
#define VC_SM_MIN_VER 0
/* ---- Private Constants and Types -------------------------------------- */
/* Command blocks come from a pool */
#define SM_MAX_NUM_CMD_RSP_BLKS 32
/* The number of supported connections */
#define SM_MAX_NUM_CONNECTIONS 3
struct sm_cmd_rsp_blk {
struct list_head head; /* To create lists */
/* To be signaled when the response is there */
struct completion cmplt;
u32 id;
u16 length;
u8 msg[VC_SM_MAX_MSG_LEN];
uint32_t wait:1;
uint32_t sent:1;
uint32_t alloc:1;
};
struct sm_instance {
u32 num_connections;
unsigned int service_handle[SM_MAX_NUM_CONNECTIONS];
struct task_struct *io_thread;
struct completion io_cmplt;
vpu_event_cb vpu_event;
/* Mutex over the following lists */
struct mutex lock;
u32 trans_id;
struct list_head cmd_list;
struct list_head rsp_list;
struct list_head dead_list;
struct sm_cmd_rsp_blk free_blk[SM_MAX_NUM_CMD_RSP_BLKS];
/* Mutex over the free_list */
struct mutex free_lock;
struct list_head free_list;
struct semaphore free_sema;
struct vchiq_instance *vchiq_instance;
};
/* ---- Private Variables ------------------------------------------------ */
/* ---- Private Function Prototypes -------------------------------------- */
/* ---- Private Functions ------------------------------------------------ */
static int
bcm2835_vchi_msg_queue(struct vchiq_instance *vchiq_instance, unsigned int handle,
void *data,
unsigned int size)
{
return vchiq_queue_kernel_message(vchiq_instance, handle, data, size);
}
static struct
sm_cmd_rsp_blk *vc_vchi_cmd_create(struct sm_instance *instance,
enum vc_sm_msg_type id, void *msg,
u32 size, int wait)
{
struct sm_cmd_rsp_blk *blk;
struct vc_sm_msg_hdr_t *hdr;
if (down_interruptible(&instance->free_sema)) {
blk = kmalloc(sizeof(*blk), GFP_KERNEL);
if (!blk)
return NULL;
blk->alloc = 1;
init_completion(&blk->cmplt);
} else {
mutex_lock(&instance->free_lock);
blk =
list_first_entry(&instance->free_list,
struct sm_cmd_rsp_blk, head);
list_del(&blk->head);
mutex_unlock(&instance->free_lock);
}
blk->sent = 0;
blk->wait = wait;
blk->length = sizeof(*hdr) + size;
hdr = (struct vc_sm_msg_hdr_t *)blk->msg;
hdr->type = id;
mutex_lock(&instance->lock);
instance->trans_id++;
/*
* Retain the top bit for identifying asynchronous events, or VPU cmds.
*/
instance->trans_id &= ~0x80000000;
hdr->trans_id = instance->trans_id;
blk->id = instance->trans_id;
mutex_unlock(&instance->lock);
if (size)
memcpy(hdr->body, msg, size);
return blk;
}
static void
vc_vchi_cmd_delete(struct sm_instance *instance, struct sm_cmd_rsp_blk *blk)
{
if (blk->alloc) {
kfree(blk);
return;
}
mutex_lock(&instance->free_lock);
list_add(&blk->head, &instance->free_list);
mutex_unlock(&instance->free_lock);
up(&instance->free_sema);
}
static void vc_sm_cma_vchi_rx_ack(struct sm_instance *instance,
struct sm_cmd_rsp_blk *cmd,
struct vc_sm_result_t *reply,
u32 reply_len)
{
mutex_lock(&instance->lock);
list_for_each_entry(cmd,
&instance->rsp_list,
head) {
if (cmd->id == reply->trans_id)
break;
}
mutex_unlock(&instance->lock);
if (&cmd->head == &instance->rsp_list) {
//pr_debug("%s: received response %u, throw away...",
pr_err("%s: received response %u, throw away...",
__func__,
reply->trans_id);
} else if (reply_len > sizeof(cmd->msg)) {
pr_err("%s: reply too big (%u) %u, throw away...",
__func__, reply_len,
reply->trans_id);
} else {
memcpy(cmd->msg, reply,
reply_len);
complete(&cmd->cmplt);
}
}
static int vc_sm_cma_vchi_videocore_io(void *arg)
{
struct sm_instance *instance = arg;
struct sm_cmd_rsp_blk *cmd = NULL, *cmd_tmp;
struct vc_sm_result_t *reply;
struct vchiq_header *header;
s32 status;
int svc_use = 1;
while (1) {
if (svc_use)
vchiq_release_service(instance->vchiq_instance, instance->service_handle[0]);
svc_use = 0;
if (wait_for_completion_interruptible(&instance->io_cmplt))
continue;
vchiq_use_service(instance->vchiq_instance, instance->service_handle[0]);
svc_use = 1;
do {
/*
* Get new command and move it to response list
*/
mutex_lock(&instance->lock);
if (list_empty(&instance->cmd_list)) {
/* no more commands to process */
mutex_unlock(&instance->lock);
break;
}
cmd = list_first_entry(&instance->cmd_list,
struct sm_cmd_rsp_blk, head);
list_move(&cmd->head, &instance->rsp_list);
cmd->sent = 1;
mutex_unlock(&instance->lock);
/* Send the command */
status =
bcm2835_vchi_msg_queue(instance->vchiq_instance,
instance->service_handle[0],
cmd->msg, cmd->length);
if (status) {
pr_err("%s: failed to queue message (%d)",
__func__, status);
}
/* If no reply is needed then we're done */
if (!cmd->wait) {
mutex_lock(&instance->lock);
list_del(&cmd->head);
mutex_unlock(&instance->lock);
vc_vchi_cmd_delete(instance, cmd);
continue;
}
if (status) {
complete(&cmd->cmplt);
continue;
}
} while (1);
while ((header = vchiq_msg_hold(instance->vchiq_instance,
instance->service_handle[0]))) {
reply = (struct vc_sm_result_t *)header->data;
if (reply->trans_id & 0x80000000) {
/* Async event or cmd from the VPU */
if (instance->vpu_event)
instance->vpu_event(instance, reply,
header->size);
} else {
vc_sm_cma_vchi_rx_ack(instance, cmd, reply,
header->size);
}
vchiq_release_message(instance->vchiq_instance,
instance->service_handle[0],
header);
}
/* Go through the dead list and free them */
mutex_lock(&instance->lock);
list_for_each_entry_safe(cmd, cmd_tmp, &instance->dead_list,
head) {
list_del(&cmd->head);
vc_vchi_cmd_delete(instance, cmd);
}
mutex_unlock(&instance->lock);
}
return 0;
}
static int vc_sm_cma_vchi_callback(struct vchiq_instance *vchiq_instance,
enum vchiq_reason reason,
struct vchiq_header *header,
unsigned int handle, void *cb_data, void __user *cb_userdata)
{
struct sm_instance *instance = vchiq_get_service_userdata(vchiq_instance, handle);
switch (reason) {
case VCHIQ_MESSAGE_AVAILABLE:
vchiq_msg_queue_push(vchiq_instance, handle, header);
complete(&instance->io_cmplt);
break;
case VCHIQ_SERVICE_CLOSED:
pr_info("%s: service CLOSED!!", __func__);
break;
default:
break;
}
return 0;
}
struct sm_instance *vc_sm_cma_vchi_init(struct vchiq_instance *vchiq_instance,
unsigned int num_connections,
vpu_event_cb vpu_event)
{
u32 i;
struct sm_instance *instance;
int status;
pr_debug("%s: start", __func__);
if (num_connections > SM_MAX_NUM_CONNECTIONS) {
pr_err("%s: unsupported number of connections %u (max=%u)",
__func__, num_connections, SM_MAX_NUM_CONNECTIONS);
goto err_null;
}
/* Allocate memory for this instance */
instance = kzalloc(sizeof(*instance), GFP_KERNEL);
/* Misc initialisations */
mutex_init(&instance->lock);
init_completion(&instance->io_cmplt);
INIT_LIST_HEAD(&instance->cmd_list);
INIT_LIST_HEAD(&instance->rsp_list);
INIT_LIST_HEAD(&instance->dead_list);
INIT_LIST_HEAD(&instance->free_list);
sema_init(&instance->free_sema, SM_MAX_NUM_CMD_RSP_BLKS);
mutex_init(&instance->free_lock);
for (i = 0; i < SM_MAX_NUM_CMD_RSP_BLKS; i++) {
init_completion(&instance->free_blk[i].cmplt);
list_add(&instance->free_blk[i].head, &instance->free_list);
}
instance->vchiq_instance = vchiq_instance;
/* Open the VCHI service connections */
instance->num_connections = num_connections;
for (i = 0; i < num_connections; i++) {
struct vchiq_service_params_kernel params = {
.version = VC_SM_VER,
.version_min = VC_SM_MIN_VER,
.fourcc = VCHIQ_MAKE_FOURCC('S', 'M', 'E', 'M'),
.callback = vc_sm_cma_vchi_callback,
.userdata = instance,
};
status = vchiq_open_service(vchiq_instance, &params,
&instance->service_handle[i]);
if (status) {
pr_err("%s: failed to open VCHI service (%d)",
__func__, status);
goto err_close_services;
}
}
/* Create the thread which takes care of all io to/from videoocore. */
instance->io_thread = kthread_create(&vc_sm_cma_vchi_videocore_io,
(void *)instance, "SMIO");
if (!instance->io_thread) {
pr_err("%s: failed to create SMIO thread", __func__);
goto err_close_services;
}
instance->vpu_event = vpu_event;
set_user_nice(instance->io_thread, -10);
wake_up_process(instance->io_thread);
pr_debug("%s: success - instance %p", __func__, instance);
return instance;
err_close_services:
for (i = 0; i < instance->num_connections; i++) {
if (instance->service_handle[i])
vchiq_close_service(vchiq_instance, instance->service_handle[i]);
}
kfree(instance);
err_null:
pr_debug("%s: FAILED", __func__);
return NULL;
}
int vc_sm_cma_vchi_stop(struct vchiq_instance *vchiq_instance, struct sm_instance **handle)
{
struct sm_instance *instance;
u32 i;
if (!handle) {
pr_err("%s: invalid pointer to handle %p", __func__, handle);
goto lock;
}
if (!*handle) {
pr_err("%s: invalid handle %p", __func__, *handle);
goto lock;
}
instance = *handle;
/* Close all VCHI service connections */
for (i = 0; i < instance->num_connections; i++) {
vchiq_use_service(vchiq_instance, instance->service_handle[i]);
vchiq_close_service(vchiq_instance, instance->service_handle[i]);
}
kfree(instance);
*handle = NULL;
return 0;
lock:
return -EINVAL;
}
static int vc_sm_cma_vchi_send_msg(struct sm_instance *handle,
enum vc_sm_msg_type msg_id, void *msg,
u32 msg_size, void *result, u32 result_size,
u32 *cur_trans_id, u8 wait_reply)
{
int status = 0;
struct sm_instance *instance = handle;
struct sm_cmd_rsp_blk *cmd_blk;
if (!handle) {
pr_err("%s: invalid handle", __func__);
return -EINVAL;
}
if (!msg) {
pr_err("%s: invalid msg pointer", __func__);
return -EINVAL;
}
cmd_blk =
vc_vchi_cmd_create(instance, msg_id, msg, msg_size, wait_reply);
if (!cmd_blk) {
pr_err("[%s]: failed to allocate global tracking resource",
__func__);
return -ENOMEM;
}
if (cur_trans_id)
*cur_trans_id = cmd_blk->id;
mutex_lock(&instance->lock);
list_add_tail(&cmd_blk->head, &instance->cmd_list);
mutex_unlock(&instance->lock);
complete(&instance->io_cmplt);
if (!wait_reply)
/* We're done */
return 0;
/* Wait for the response */
if (wait_for_completion_interruptible(&cmd_blk->cmplt)) {
mutex_lock(&instance->lock);
if (!cmd_blk->sent) {
list_del(&cmd_blk->head);
mutex_unlock(&instance->lock);
vc_vchi_cmd_delete(instance, cmd_blk);
return -ENXIO;
}
list_move(&cmd_blk->head, &instance->dead_list);
mutex_unlock(&instance->lock);
complete(&instance->io_cmplt);
return -EINTR; /* We're done */
}
if (result && result_size) {
memcpy(result, cmd_blk->msg, result_size);
} else {
struct vc_sm_result_t *res =
(struct vc_sm_result_t *)cmd_blk->msg;
status = (res->success == 0) ? 0 : -ENXIO;
}
mutex_lock(&instance->lock);
list_del(&cmd_blk->head);
mutex_unlock(&instance->lock);
vc_vchi_cmd_delete(instance, cmd_blk);
return status;
}
int vc_sm_cma_vchi_free(struct sm_instance *handle, struct vc_sm_free_t *msg,
u32 *cur_trans_id)
{
return vc_sm_cma_vchi_send_msg(handle, VC_SM_MSG_TYPE_FREE,
msg, sizeof(*msg), 0, 0, cur_trans_id, 0);
}
int vc_sm_cma_vchi_import(struct sm_instance *handle, struct vc_sm_import *msg,
struct vc_sm_import_result *result, u32 *cur_trans_id)
{
return vc_sm_cma_vchi_send_msg(handle, VC_SM_MSG_TYPE_IMPORT,
msg, sizeof(*msg), result, sizeof(*result),
cur_trans_id, 1);
}
int vc_sm_cma_vchi_client_version(struct sm_instance *handle,
struct vc_sm_version *msg,
struct vc_sm_result_t *result,
u32 *cur_trans_id)
{
return vc_sm_cma_vchi_send_msg(handle, VC_SM_MSG_TYPE_CLIENT_VERSION,
//msg, sizeof(*msg), result, sizeof(*result),
//cur_trans_id, 1);
msg, sizeof(*msg), NULL, 0,
cur_trans_id, 0);
}
int vc_sm_vchi_client_vc_mem_req_reply(struct sm_instance *handle,
struct vc_sm_vc_mem_request_result *msg,
uint32_t *cur_trans_id)
{
return vc_sm_cma_vchi_send_msg(handle,
VC_SM_MSG_TYPE_VC_MEM_REQUEST_REPLY,
msg, sizeof(*msg), 0, 0, cur_trans_id,
0);
}

View File

@@ -0,0 +1,63 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* VideoCore Shared Memory CMA allocator
*
* Copyright: 2018, Raspberry Pi (Trading) Ltd
* Copyright 2011-2012 Broadcom Corporation. All rights reserved.
*
* Based on vmcs_sm driver from Broadcom Corporation.
*
*/
#ifndef __VC_SM_CMA_VCHI_H__INCLUDED__
#define __VC_SM_CMA_VCHI_H__INCLUDED__
#include "../include/linux/raspberrypi/vchiq.h"
#include "vc_sm_defs.h"
/*
* Forward declare.
*/
struct sm_instance;
typedef void (*vpu_event_cb)(struct sm_instance *instance,
struct vc_sm_result_t *reply, int reply_len);
/*
* Initialize the shared memory service, opens up vchi connection to talk to it.
*/
struct sm_instance *vc_sm_cma_vchi_init(struct vchiq_instance *vchi_instance,
unsigned int num_connections,
vpu_event_cb vpu_event);
/*
* Terminates the shared memory service.
*/
int vc_sm_cma_vchi_stop(struct vchiq_instance *vchi_instance, struct sm_instance **handle);
/*
* Ask the shared memory service to free up some memory that was previously
* allocated by the vc_sm_cma_vchi_alloc function call.
*/
int vc_sm_cma_vchi_free(struct sm_instance *handle, struct vc_sm_free_t *msg,
u32 *cur_trans_id);
/*
* Import a contiguous block of memory and wrap it in a GPU MEM_HANDLE_T.
*/
int vc_sm_cma_vchi_import(struct sm_instance *handle, struct vc_sm_import *msg,
struct vc_sm_import_result *result,
u32 *cur_trans_id);
int vc_sm_cma_vchi_client_version(struct sm_instance *handle,
struct vc_sm_version *msg,
struct vc_sm_result_t *result,
u32 *cur_trans_id);
int vc_sm_vchi_client_vc_mem_req_reply(struct sm_instance *handle,
struct vc_sm_vc_mem_request_result *msg,
uint32_t *cur_trans_id);
#endif /* __VC_SM_CMA_VCHI_H__INCLUDED__ */

View File

@@ -0,0 +1,296 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* VideoCore Shared Memory CMA allocator
*
* Copyright: 2018, Raspberry Pi (Trading) Ltd
*
* Based on vc_sm_defs.h from the vmcs_sm driver Copyright Broadcom Corporation.
* All IPC messages are copied across to this file, even if the vc-sm-cma
* driver is not currently using them.
*
****************************************************************************
*/
#ifndef __VC_SM_DEFS_H__INCLUDED__
#define __VC_SM_DEFS_H__INCLUDED__
/* Maximum message length */
#define VC_SM_MAX_MSG_LEN (sizeof(union vc_sm_msg_union_t) + \
sizeof(struct vc_sm_msg_hdr_t))
#define VC_SM_MAX_RSP_LEN (sizeof(union vc_sm_msg_union_t))
/* Resource name maximum size */
#define VC_SM_RESOURCE_NAME 32
/*
* Version to be reported to the VPU
* VPU assumes 0 (aka 1) which does not require the released callback, nor
* expect the client to handle VC_MEM_REQUESTS.
* Version 2 requires the released callback, and must support VC_MEM_REQUESTS.
*/
#define VC_SM_PROTOCOL_VERSION 2
enum vc_sm_msg_type {
/* Message types supported for HOST->VC direction */
/* Allocate shared memory block */
VC_SM_MSG_TYPE_ALLOC,
/* Lock allocated shared memory block */
VC_SM_MSG_TYPE_LOCK,
/* Unlock allocated shared memory block */
VC_SM_MSG_TYPE_UNLOCK,
/* Unlock allocated shared memory block, do not answer command */
VC_SM_MSG_TYPE_UNLOCK_NOANS,
/* Free shared memory block */
VC_SM_MSG_TYPE_FREE,
/* Resize a shared memory block */
VC_SM_MSG_TYPE_RESIZE,
/* Walk the allocated shared memory block(s) */
VC_SM_MSG_TYPE_WALK_ALLOC,
/* A previously applied action will need to be reverted */
VC_SM_MSG_TYPE_ACTION_CLEAN,
/*
* Import a physical address and wrap into a MEM_HANDLE_T.
* Release with VC_SM_MSG_TYPE_FREE.
*/
VC_SM_MSG_TYPE_IMPORT,
/*
*Tells VC the protocol version supported by this client.
* 2 supports the async/cmd messages from the VPU for final release
* of memory, and for VC allocations.
*/
VC_SM_MSG_TYPE_CLIENT_VERSION,
/* Response to VC request for memory */
VC_SM_MSG_TYPE_VC_MEM_REQUEST_REPLY,
/*
* Asynchronous/cmd messages supported for VC->HOST direction.
* Signalled by setting the top bit in vc_sm_result_t trans_id.
*/
/*
* VC has finished with an imported memory allocation.
* Release any Linux reference counts on the underlying block.
*/
VC_SM_MSG_TYPE_RELEASED,
/* VC request for memory */
VC_SM_MSG_TYPE_VC_MEM_REQUEST,
VC_SM_MSG_TYPE_MAX
};
/* Type of memory to be allocated */
enum vc_sm_alloc_type_t {
VC_SM_ALLOC_CACHED,
VC_SM_ALLOC_NON_CACHED,
};
/* Message header for all messages in HOST->VC direction */
struct vc_sm_msg_hdr_t {
u32 type;
u32 trans_id;
u8 body[];
};
/* Request to allocate memory (HOST->VC) */
struct vc_sm_alloc_t {
/* type of memory to allocate */
enum vc_sm_alloc_type_t type;
/* byte amount of data to allocate per unit */
u32 base_unit;
/* number of unit to allocate */
u32 num_unit;
/* alignment to be applied on allocation */
u32 alignment;
/* identity of who allocated this block */
u32 allocator;
/* resource name (for easier tracking on vc side) */
char name[VC_SM_RESOURCE_NAME];
};
/* Result of a requested memory allocation (VC->HOST) */
struct vc_sm_alloc_result_t {
/* Transaction identifier */
u32 trans_id;
/* Resource handle */
u32 res_handle;
/* Pointer to resource buffer */
u32 res_mem;
/* Resource base size (bytes) */
u32 res_base_size;
/* Resource number */
u32 res_num;
};
/* Request to free a previously allocated memory (HOST->VC) */
struct vc_sm_free_t {
/* Resource handle (returned from alloc) */
u32 res_handle;
/* Resource buffer (returned from alloc) */
u32 res_mem;
};
/* Request to lock a previously allocated memory (HOST->VC) */
struct vc_sm_lock_unlock_t {
/* Resource handle (returned from alloc) */
u32 res_handle;
/* Resource buffer (returned from alloc) */
u32 res_mem;
};
/* Request to resize a previously allocated memory (HOST->VC) */
struct vc_sm_resize_t {
/* Resource handle (returned from alloc) */
u32 res_handle;
/* Resource buffer (returned from alloc) */
u32 res_mem;
/* Resource *new* size requested (bytes) */
u32 res_new_size;
};
/* Result of a requested memory lock (VC->HOST) */
struct vc_sm_lock_result_t {
/* Transaction identifier */
u32 trans_id;
/* Resource handle */
u32 res_handle;
/* Pointer to resource buffer */
u32 res_mem;
/*
* Pointer to former resource buffer if the memory
* was reallocated
*/
u32 res_old_mem;
};
/* Generic result for a request (VC->HOST) */
struct vc_sm_result_t {
/* Transaction identifier */
u32 trans_id;
s32 success;
};
/* Request to revert a previously applied action (HOST->VC) */
struct vc_sm_action_clean_t {
/* Action of interest */
enum vc_sm_msg_type res_action;
/* Transaction identifier for the action of interest */
u32 action_trans_id;
};
/* Request to remove all data associated with a given allocator (HOST->VC) */
struct vc_sm_free_all_t {
/* Allocator identifier */
u32 allocator;
};
/* Request to import memory (HOST->VC) */
struct vc_sm_import {
/* type of memory to allocate */
enum vc_sm_alloc_type_t type;
/* pointer to the VC (ie physical) address of the allocated memory */
u32 addr;
/* size of buffer */
u32 size;
/* opaque handle returned in RELEASED messages */
u32 kernel_id;
/* Allocator identifier */
u32 allocator;
/* resource name (for easier tracking on vc side) */
char name[VC_SM_RESOURCE_NAME];
};
/* Result of a requested memory import (VC->HOST) */
struct vc_sm_import_result {
/* Transaction identifier */
u32 trans_id;
/* Resource handle */
u32 res_handle;
};
/* Notification that VC has finished with an allocation (VC->HOST) */
struct vc_sm_released {
/* cmd type / trans_id */
u32 cmd;
/* pointer to the VC (ie physical) address of the allocated memory */
u32 addr;
/* size of buffer */
u32 size;
/* opaque handle returned in RELEASED messages */
u32 kernel_id;
u32 vc_handle;
};
/*
* Client informing VC as to the protocol version it supports.
* >=2 requires the released callback, and supports VC asking for memory.
* Failure means that the firmware doesn't support this call, and therefore the
* client should either fail, or NOT rely on getting the released callback.
*/
struct vc_sm_version {
u32 version;
};
/* Request FROM VideoCore for some memory */
struct vc_sm_vc_mem_request {
/* cmd type */
u32 cmd;
/* trans_id (from VPU) */
u32 trans_id;
/* size of buffer */
u32 size;
/* alignment of buffer */
u32 align;
/* resource name (for easier tracking) */
char name[VC_SM_RESOURCE_NAME];
/* VPU handle for the resource */
u32 vc_handle;
};
/* Response from the kernel to provide the VPU with some memory */
struct vc_sm_vc_mem_request_result {
/* Transaction identifier for the VPU */
u32 trans_id;
/* pointer to the physical address of the allocated memory */
u32 addr;
/* opaque handle returned in RELEASED messages */
u32 kernel_id;
};
/* Union of ALL messages */
union vc_sm_msg_union_t {
struct vc_sm_alloc_t alloc;
struct vc_sm_alloc_result_t alloc_result;
struct vc_sm_free_t free;
struct vc_sm_lock_unlock_t lock_unlock;
struct vc_sm_action_clean_t action_clean;
struct vc_sm_resize_t resize;
struct vc_sm_lock_result_t lock_result;
struct vc_sm_result_t result;
struct vc_sm_free_all_t free_all;
struct vc_sm_import import;
struct vc_sm_import_result import_result;
struct vc_sm_version version;
struct vc_sm_released released;
struct vc_sm_vc_mem_request vc_request;
struct vc_sm_vc_mem_request_result vc_request_result;
};
#endif /* __VC_SM_DEFS_H__INCLUDED__ */

View File

@@ -0,0 +1,28 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* VideoCore Shared Memory CMA allocator
*
* Copyright: 2018, Raspberry Pi (Trading) Ltd
*
* Based on vc_sm_defs.h from the vmcs_sm driver Copyright Broadcom Corporation.
*
*/
#ifndef __VC_SM_KNL_H__INCLUDED__
#define __VC_SM_KNL_H__INCLUDED__
#if !defined(__KERNEL__)
#error "This interface is for kernel use only..."
#endif
/* Free a previously allocated or imported shared memory handle and block. */
int vc_sm_cma_free(void *handle);
/* Get an internal resource handle mapped from the external one. */
int vc_sm_cma_int_handle(void *handle);
/* Import a block of memory into the GPU space. */
int vc_sm_cma_import_dmabuf(struct dma_buf *dmabuf, void **handle);
#endif /* __VC_SM_KNL_H__INCLUDED__ */