1 From 253337e15008d614c846d07a44493008a7b803b5 Mon Sep 17 00:00:00 2001
2 From: Dave Stevenson <dave.stevenson@raspberrypi.com>
3 Date: Thu, 8 Oct 2020 18:49:52 +0100
4 Subject: [PATCH] staging: vc04_services: Add new vc-sm-cma driver
6 Add Broadcom VideoCore Shared Memory support.
8 This new driver allows contiguous memory blocks to be imported
9 into the VideoCore VPU memory map, and manages the lifetime of
10 those objects, only releasing the source dmabuf once the VPU has
11 confirmed it has finished with it.
13 Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.com>
15 drivers/staging/vc04_services/Kconfig | 2 +
16 drivers/staging/vc04_services/Makefile | 1 +
17 .../include/linux/broadcom/vc_sm_cma_ioctl.h | 114 ++
18 .../staging/vc04_services/vc-sm-cma/Kconfig | 10 +
19 .../staging/vc04_services/vc-sm-cma/Makefile | 12 +
20 drivers/staging/vc04_services/vc-sm-cma/TODO | 1 +
21 .../staging/vc04_services/vc-sm-cma/vc_sm.c | 1725 +++++++++++++++++
22 .../staging/vc04_services/vc-sm-cma/vc_sm.h | 84 +
23 .../vc04_services/vc-sm-cma/vc_sm_cma_vchi.c | 503 +++++
24 .../vc04_services/vc-sm-cma/vc_sm_cma_vchi.h | 63 +
25 .../vc04_services/vc-sm-cma/vc_sm_defs.h | 297 +++
26 .../vc04_services/vc-sm-cma/vc_sm_knl.h | 28 +
27 12 files changed, 2840 insertions(+)
28 create mode 100644 drivers/staging/vc04_services/include/linux/broadcom/vc_sm_cma_ioctl.h
29 create mode 100644 drivers/staging/vc04_services/vc-sm-cma/Kconfig
30 create mode 100644 drivers/staging/vc04_services/vc-sm-cma/Makefile
31 create mode 100644 drivers/staging/vc04_services/vc-sm-cma/TODO
32 create mode 100644 drivers/staging/vc04_services/vc-sm-cma/vc_sm.c
33 create mode 100644 drivers/staging/vc04_services/vc-sm-cma/vc_sm.h
34 create mode 100644 drivers/staging/vc04_services/vc-sm-cma/vc_sm_cma_vchi.c
35 create mode 100644 drivers/staging/vc04_services/vc-sm-cma/vc_sm_cma_vchi.h
36 create mode 100644 drivers/staging/vc04_services/vc-sm-cma/vc_sm_defs.h
37 create mode 100644 drivers/staging/vc04_services/vc-sm-cma/vc_sm_knl.h
39 --- a/drivers/staging/vc04_services/Kconfig
40 +++ b/drivers/staging/vc04_services/Kconfig
41 @@ -23,6 +23,8 @@ source "drivers/staging/vc04_services/bc
43 source "drivers/staging/vc04_services/bcm2835-camera/Kconfig"
45 +source "drivers/staging/vc04_services/vc-sm-cma/Kconfig"
47 source "drivers/staging/vc04_services/vchiq-mmal/Kconfig"
50 --- a/drivers/staging/vc04_services/Makefile
51 +++ b/drivers/staging/vc04_services/Makefile
52 @@ -11,6 +11,7 @@ vchiq-objs := \
53 obj-$(CONFIG_SND_BCM2835) += bcm2835-audio/
54 obj-$(CONFIG_VIDEO_BCM2835) += bcm2835-camera/
55 obj-$(CONFIG_BCM2835_VCHIQ_MMAL) += vchiq-mmal/
56 +obj-$(CONFIG_BCM_VC_SM_CMA) += vc-sm-cma/
58 ccflags-y += -I $(srctree)/$(src)/include -D__VCCOREVER__=0x04000000
61 +++ b/drivers/staging/vc04_services/include/linux/broadcom/vc_sm_cma_ioctl.h
63 +/* SPDX-License-Identifier: GPL-2.0 */
66 + * Copyright 2019 Raspberry Pi (Trading) Ltd. All rights reserved.
68 + * Based on vmcs_sm_ioctl.h Copyright Broadcom Corporation.
71 +#ifndef __VC_SM_CMA_IOCTL_H
72 +#define __VC_SM_CMA_IOCTL_H
74 +/* ---- Include Files ---------------------------------------------------- */
76 +#if defined(__KERNEL__)
77 +#include <linux/types.h> /* Needed for standard types */
82 +#include <linux/ioctl.h>
84 +/* ---- Constants and Types ---------------------------------------------- */
86 +#define VC_SM_CMA_RESOURCE_NAME 32
87 +#define VC_SM_CMA_RESOURCE_NAME_DEFAULT "sm-host-resource"
89 +/* Type define used to create unique IOCTL number */
90 +#define VC_SM_CMA_MAGIC_TYPE 'J'
92 +/* IOCTL commands on /dev/vc-sm-cma */
93 +enum vc_sm_cma_cmd_e {
94 + VC_SM_CMA_CMD_ALLOC = 0x5A, /* Start at 0x5A arbitrarily */
96 + VC_SM_CMA_CMD_IMPORT_DMABUF,
98 + VC_SM_CMA_CMD_CLEAN_INVALID2,
100 + VC_SM_CMA_CMD_LAST /* Do not delete */
103 +/* Cache type supported, conveniently matches the user space definition in
106 +enum vc_sm_cma_cache_e {
107 + VC_SM_CMA_CACHE_NONE,
108 + VC_SM_CMA_CACHE_HOST,
109 + VC_SM_CMA_CACHE_VC,
110 + VC_SM_CMA_CACHE_BOTH,
113 +/* IOCTL Data structures */
114 +struct vc_sm_cma_ioctl_alloc {
115 + /* user -> kernel */
118 + __u32 cached; /* enum vc_sm_cma_cache_e */
120 + __u8 name[VC_SM_CMA_RESOURCE_NAME];
122 + /* kernel -> user */
128 +struct vc_sm_cma_ioctl_import_dmabuf {
129 + /* user -> kernel */
131 + __u32 cached; /* enum vc_sm_cma_cache_e */
132 + __u8 name[VC_SM_CMA_RESOURCE_NAME];
134 + /* kernel -> user */
143 + * Cache functions to be set to struct vc_sm_cma_ioctl_clean_invalid2
146 +#define VC_SM_CACHE_OP_NOP 0x00
147 +#define VC_SM_CACHE_OP_INV 0x01
148 +#define VC_SM_CACHE_OP_CLEAN 0x02
149 +#define VC_SM_CACHE_OP_FLUSH 0x03
151 +struct vc_sm_cma_ioctl_clean_invalid2 {
154 + struct vc_sm_cma_ioctl_clean_invalid_block {
155 + __u32 invalidate_mode;
157 + void * __user start_address;
159 + __u32 inter_block_stride;
164 +#define VC_SM_CMA_IOCTL_MEM_ALLOC\
165 + _IOR(VC_SM_CMA_MAGIC_TYPE, VC_SM_CMA_CMD_ALLOC,\
166 + struct vc_sm_cma_ioctl_alloc)
168 +#define VC_SM_CMA_IOCTL_MEM_IMPORT_DMABUF\
169 + _IOR(VC_SM_CMA_MAGIC_TYPE, VC_SM_CMA_CMD_IMPORT_DMABUF,\
170 + struct vc_sm_cma_ioctl_import_dmabuf)
172 +#define VC_SM_CMA_IOCTL_MEM_CLEAN_INVALID2\
173 + _IOR(VC_SM_CMA_MAGIC_TYPE, VC_SM_CMA_CMD_CLEAN_INVALID2,\
174 + struct vc_sm_cma_ioctl_clean_invalid2)
176 +#endif /* __VC_SM_CMA_IOCTL_H */
178 +++ b/drivers/staging/vc04_services/vc-sm-cma/Kconfig
180 +config BCM_VC_SM_CMA
181 + tristate "VideoCore Shared Memory (CMA) driver"
182 + select BCM2835_VCHIQ
184 + select DMA_SHARED_BUFFER
186 + Say Y here to enable the shared memory interface that
187 + supports sharing dmabufs with VideoCore.
188 + This operates over the VCHIQ interface to a service
189 + running on VideoCore.
191 +++ b/drivers/staging/vc04_services/vc-sm-cma/Makefile
194 + -I$(srctree)/$(src)/../ \
195 + -I$(srctree)/$(src)/../interface/vchiq_arm\
196 + -I$(srctree)/$(src)/../include
201 +vc-sm-cma-$(CONFIG_BCM_VC_SM_CMA) := \
202 + vc_sm.o vc_sm_cma_vchi.o
204 +obj-$(CONFIG_BCM_VC_SM_CMA) += vc-sm-cma.o
206 +++ b/drivers/staging/vc04_services/vc-sm-cma/TODO
208 +No currently outstanding tasks except some clean-up.
210 +++ b/drivers/staging/vc04_services/vc-sm-cma/vc_sm.c
212 +// SPDX-License-Identifier: GPL-2.0
214 + * VideoCore Shared Memory driver using CMA.
216 + * Copyright: 2018, Raspberry Pi (Trading) Ltd
217 + * Dave Stevenson <dave.stevenson@raspberrypi.org>
219 + * Based on vmcs_sm driver from Broadcom Corporation for some API,
220 + * and taking some code for buffer allocation and dmabuf handling from
224 + * This driver has 3 main uses:
225 + * 1) Allocating buffers for the kernel or userspace that can be shared with the
227 + * 2) Importing dmabufs from elsewhere for sharing with the VPU.
228 + * 3) Allocating buffers for use by the VPU.
230 + * In the first and second cases the native handle is a dmabuf. Releasing the
231 + * resource inherently comes from releasing the dmabuf, and this will trigger
232 + * unmapping on the VPU. The underlying allocation and our buffer structure are
233 + * retained until the VPU has confirmed that it has finished with it.
235 + * For the VPU allocations the VPU is responsible for triggering the release,
236 + * and therefore the released message decrements the dma_buf refcount (with the
237 + * VPU mapping having already been marked as released).
240 +/* ---- Include Files ----------------------------------------------------- */
241 +#include <linux/cdev.h>
242 +#include <linux/device.h>
243 +#include <linux/debugfs.h>
244 +#include <linux/dma-mapping.h>
245 +#include <linux/dma-buf.h>
246 +#include <linux/errno.h>
247 +#include <linux/fs.h>
248 +#include <linux/kernel.h>
249 +#include <linux/list.h>
250 +#include <linux/miscdevice.h>
251 +#include <linux/module.h>
252 +#include <linux/mm.h>
253 +#include <linux/of_device.h>
254 +#include <linux/platform_device.h>
255 +#include <linux/proc_fs.h>
256 +#include <linux/slab.h>
257 +#include <linux/seq_file.h>
258 +#include <linux/syscalls.h>
259 +#include <linux/types.h>
260 +#include <asm/cacheflush.h>
262 +#include "vchiq_connected.h"
263 +#include "vc_sm_cma_vchi.h"
266 +#include "vc_sm_knl.h"
267 +#include <linux/broadcom/vc_sm_cma_ioctl.h>
269 +/* ---- Private Constants and Types --------------------------------------- */
271 +#define DEVICE_NAME "vcsm-cma"
272 +#define DEVICE_MINOR 0
274 +#define VC_SM_RESOURCE_NAME_DEFAULT "sm-host-resource"
276 +#define VC_SM_DIR_ROOT_NAME "vcsm-cma"
277 +#define VC_SM_STATE "state"
279 +/* Private file data associated with each opened device. */
280 +struct vc_sm_privdata_t {
281 + pid_t pid; /* PID of creator. */
283 + int restart_sys; /* Tracks restart on interrupt. */
284 + enum vc_sm_msg_type int_action; /* Interrupted action. */
285 + u32 int_trans_id; /* Interrupted transaction. */
288 +typedef int (*VC_SM_SHOW) (struct seq_file *s, void *v);
290 + VC_SM_SHOW show; /* Debug fs function hookup. */
291 + struct dentry *dir_entry; /* Debug fs directory entry. */
292 + void *priv_data; /* Private data */
295 +/* Global state information. */
297 + struct platform_device *pdev;
299 + struct miscdevice misc_dev;
301 + struct sm_instance *sm_handle; /* Handle for videocore service. */
303 + spinlock_t kernelid_map_lock; /* Spinlock protecting kernelid_map */
304 + struct idr kernelid_map;
306 + struct mutex map_lock; /* Global map lock. */
307 + struct list_head buffer_list; /* List of buffer. */
309 + struct vc_sm_privdata_t *data_knl; /* Kernel internal data tracking. */
310 + struct vc_sm_privdata_t *vpu_allocs; /* All allocations from the VPU */
311 + struct dentry *dir_root; /* Debug fs entries root. */
312 + struct sm_pde_t dir_state; /* Debug fs entries state sub-tree. */
314 + bool require_released_callback; /* VPU will send a released msg when it
315 + * has finished with a resource.
317 + u32 int_trans_id; /* Interrupted transaction. */
320 +struct vc_sm_dma_buf_attachment {
321 + struct device *dev;
322 + struct sg_table sg_table;
323 + struct list_head list;
324 + enum dma_data_direction dma_dir;
327 +/* ---- Private Variables ----------------------------------------------- */
329 +static struct sm_state_t *sm_state;
330 +static int sm_inited;
332 +/* ---- Private Function Prototypes -------------------------------------- */
334 +/* ---- Private Functions ------------------------------------------------ */
336 +static int get_kernel_id(struct vc_sm_buffer *buffer)
340 + spin_lock(&sm_state->kernelid_map_lock);
341 + handle = idr_alloc(&sm_state->kernelid_map, buffer, 0, 0, GFP_KERNEL);
342 + spin_unlock(&sm_state->kernelid_map_lock);
347 +static struct vc_sm_buffer *lookup_kernel_id(int handle)
349 + return idr_find(&sm_state->kernelid_map, handle);
352 +static void free_kernel_id(int handle)
354 + spin_lock(&sm_state->kernelid_map_lock);
355 + idr_remove(&sm_state->kernelid_map, handle);
356 + spin_unlock(&sm_state->kernelid_map_lock);
359 +static int vc_sm_cma_seq_file_show(struct seq_file *s, void *v)
361 + struct sm_pde_t *sm_pde;
363 + sm_pde = (struct sm_pde_t *)(s->private);
365 + if (sm_pde && sm_pde->show)
366 + sm_pde->show(s, v);
371 +static int vc_sm_cma_single_open(struct inode *inode, struct file *file)
373 + return single_open(file, vc_sm_cma_seq_file_show, inode->i_private);
376 +static const struct file_operations vc_sm_cma_debug_fs_fops = {
377 + .open = vc_sm_cma_single_open,
379 + .llseek = seq_lseek,
380 + .release = single_release,
383 +static int vc_sm_cma_global_state_show(struct seq_file *s, void *v)
385 + struct vc_sm_buffer *resource = NULL;
386 + int resource_count = 0;
391 + seq_printf(s, "\nVC-ServiceHandle %p\n", sm_state->sm_handle);
393 + /* Log all applicable mapping(s). */
395 + mutex_lock(&sm_state->map_lock);
396 + seq_puts(s, "\nResources\n");
397 + if (!list_empty(&sm_state->buffer_list)) {
398 + list_for_each_entry(resource, &sm_state->buffer_list,
399 + global_buffer_list) {
402 + seq_printf(s, "\nResource %p\n",
404 + seq_printf(s, " NAME %s\n",
406 + seq_printf(s, " SIZE %zu\n",
408 + seq_printf(s, " DMABUF %p\n",
409 + resource->dma_buf);
410 + if (resource->imported) {
411 + seq_printf(s, " ATTACH %p\n",
412 + resource->import.attach);
413 + seq_printf(s, " SGT %p\n",
414 + resource->import.sgt);
416 + seq_printf(s, " SGT %p\n",
417 + resource->alloc.sg_table);
419 + seq_printf(s, " DMA_ADDR %pad\n",
420 + &resource->dma_addr);
421 + seq_printf(s, " VC_HANDLE %08x\n",
422 + resource->vc_handle);
423 + seq_printf(s, " VC_MAPPING %d\n",
424 + resource->vpu_state);
427 + seq_printf(s, "\n\nTotal resource count: %d\n\n", resource_count);
429 + mutex_unlock(&sm_state->map_lock);
435 + * Adds a buffer to the private data list which tracks all the allocated
438 +static void vc_sm_add_resource(struct vc_sm_privdata_t *privdata,
439 + struct vc_sm_buffer *buffer)
441 + mutex_lock(&sm_state->map_lock);
442 + list_add(&buffer->global_buffer_list, &sm_state->buffer_list);
443 + mutex_unlock(&sm_state->map_lock);
445 + pr_debug("[%s]: added buffer %p (name %s, size %zu)\n",
446 + __func__, buffer, buffer->name, buffer->size);
450 + * Cleans up imported dmabuf.
452 +static void vc_sm_clean_up_dmabuf(struct vc_sm_buffer *buffer)
454 + if (!buffer->imported)
457 + /* Handle cleaning up imported dmabufs */
458 + mutex_lock(&buffer->lock);
459 + if (buffer->import.sgt) {
460 + dma_buf_unmap_attachment(buffer->import.attach,
461 + buffer->import.sgt,
462 + DMA_BIDIRECTIONAL);
463 + buffer->import.sgt = NULL;
465 + if (buffer->import.attach) {
466 + dma_buf_detach(buffer->dma_buf, buffer->import.attach);
467 + buffer->import.attach = NULL;
469 + mutex_unlock(&buffer->lock);
473 + * Instructs VPU to decrement the refcount on a buffer.
475 +static void vc_sm_vpu_free(struct vc_sm_buffer *buffer)
477 + if (buffer->vc_handle && buffer->vpu_state == VPU_MAPPED) {
478 + struct vc_sm_free_t free = { buffer->vc_handle, 0 };
479 + int status = vc_sm_cma_vchi_free(sm_state->sm_handle, &free,
480 + &sm_state->int_trans_id);
481 + if (status != 0 && status != -EINTR) {
482 + pr_err("[%s]: failed to free memory on videocore (status: %u, trans_id: %u)\n",
483 + __func__, status, sm_state->int_trans_id);
486 + if (sm_state->require_released_callback) {
487 + /* Need to wait for the VPU to confirm the free. */
489 + /* Retain a reference on this until the VPU has
492 + buffer->vpu_state = VPU_UNMAPPING;
494 + buffer->vpu_state = VPU_NOT_MAPPED;
495 + buffer->vc_handle = 0;
501 + * Release an allocation.
502 + * All refcounting is done via the dma buf object.
504 + * Must be called with the mutex held. The function will either release the
505 + * mutex (if defering the release) or destroy it. The caller must therefore not
506 + * reuse the buffer on return.
508 +static void vc_sm_release_resource(struct vc_sm_buffer *buffer)
510 + pr_debug("[%s]: buffer %p (name %s, size %zu), imported %u\n",
511 + __func__, buffer, buffer->name, buffer->size,
514 + if (buffer->vc_handle) {
515 + /* We've sent the unmap request but not had the response. */
516 + pr_debug("[%s]: Waiting for VPU unmap response on %p\n",
520 + if (buffer->in_use) {
521 + /* dmabuf still in use - we await the release */
522 + pr_debug("[%s]: buffer %p is still in use\n", __func__, buffer);
526 + /* Release the allocation (whether imported dmabuf or CMA allocation) */
527 + if (buffer->imported) {
528 + if (buffer->import.dma_buf)
529 + dma_buf_put(buffer->import.dma_buf);
531 + pr_err("%s: Imported dmabuf already been put for buf %p\n",
533 + buffer->import.dma_buf = NULL;
535 + dma_free_coherent(&sm_state->pdev->dev, buffer->size,
536 + buffer->cookie, buffer->dma_addr);
539 + /* Free our buffer. Start by removing it from the list */
540 + mutex_lock(&sm_state->map_lock);
541 + list_del(&buffer->global_buffer_list);
542 + mutex_unlock(&sm_state->map_lock);
544 + pr_debug("%s: Release our allocation - done\n", __func__);
545 + mutex_unlock(&buffer->lock);
547 + mutex_destroy(&buffer->lock);
553 + mutex_unlock(&buffer->lock);
556 +/* Create support for private data tracking. */
557 +static struct vc_sm_privdata_t *vc_sm_cma_create_priv_data(pid_t id)
559 + char alloc_name[32];
560 + struct vc_sm_privdata_t *file_data = NULL;
562 + /* Allocate private structure. */
563 + file_data = kzalloc(sizeof(*file_data), GFP_KERNEL);
568 + snprintf(alloc_name, sizeof(alloc_name), "%d", id);
570 + file_data->pid = id;
575 +/* Dma buf operations for use with our own allocations */
577 +static int vc_sm_dma_buf_attach(struct dma_buf *dmabuf,
578 + struct dma_buf_attachment *attachment)
581 + struct vc_sm_dma_buf_attachment *a;
582 + struct sg_table *sgt;
583 + struct vc_sm_buffer *buf = dmabuf->priv;
584 + struct scatterlist *rd, *wr;
587 + a = kzalloc(sizeof(*a), GFP_KERNEL);
591 + pr_debug("%s dmabuf %p attachment %p\n", __func__, dmabuf, attachment);
593 + mutex_lock(&buf->lock);
595 + INIT_LIST_HEAD(&a->list);
597 + sgt = &a->sg_table;
599 + /* Copy the buf->base_sgt scatter list to the attachment, as we can't
600 + * map the same scatter list to multiple attachments at the same time.
602 + ret = sg_alloc_table(sgt, buf->alloc.sg_table->orig_nents, GFP_KERNEL);
608 + rd = buf->alloc.sg_table->sgl;
610 + for (i = 0; i < sgt->orig_nents; ++i) {
611 + sg_set_page(wr, sg_page(rd), rd->length, rd->offset);
616 + a->dma_dir = DMA_NONE;
617 + attachment->priv = a;
619 + list_add(&a->list, &buf->attachments);
620 + mutex_unlock(&buf->lock);
625 +static void vc_sm_dma_buf_detach(struct dma_buf *dmabuf,
626 + struct dma_buf_attachment *attachment)
628 + struct vc_sm_dma_buf_attachment *a = attachment->priv;
629 + struct vc_sm_buffer *buf = dmabuf->priv;
630 + struct sg_table *sgt;
632 + pr_debug("%s dmabuf %p attachment %p\n", __func__, dmabuf, attachment);
636 + sgt = &a->sg_table;
638 + /* release the scatterlist cache */
639 + if (a->dma_dir != DMA_NONE)
640 + dma_unmap_sg(attachment->dev, sgt->sgl, sgt->orig_nents,
642 + sg_free_table(sgt);
644 + mutex_lock(&buf->lock);
645 + list_del(&a->list);
646 + mutex_unlock(&buf->lock);
651 +static struct sg_table *vc_sm_map_dma_buf(struct dma_buf_attachment *attachment,
652 + enum dma_data_direction direction)
654 + struct vc_sm_dma_buf_attachment *a = attachment->priv;
655 + /* stealing dmabuf mutex to serialize map/unmap operations */
656 + struct mutex *lock = &attachment->dmabuf->lock;
657 + struct sg_table *table;
660 + pr_debug("%s attachment %p\n", __func__, attachment);
661 + table = &a->sg_table;
663 + /* return previously mapped sg table */
664 + if (a->dma_dir == direction) {
665 + mutex_unlock(lock);
669 + /* release any previous cache */
670 + if (a->dma_dir != DMA_NONE) {
671 + dma_unmap_sg(attachment->dev, table->sgl, table->orig_nents,
673 + a->dma_dir = DMA_NONE;
676 + /* mapping to the client with new direction */
677 + table->nents = dma_map_sg(attachment->dev, table->sgl,
678 + table->orig_nents, direction);
679 + if (!table->nents) {
680 + pr_err("failed to map scatterlist\n");
681 + mutex_unlock(lock);
682 + return ERR_PTR(-EIO);
685 + a->dma_dir = direction;
686 + mutex_unlock(lock);
688 + pr_debug("%s attachment %p\n", __func__, attachment);
692 +static void vc_sm_unmap_dma_buf(struct dma_buf_attachment *attachment,
693 + struct sg_table *table,
694 + enum dma_data_direction direction)
696 + pr_debug("%s attachment %p\n", __func__, attachment);
697 + dma_unmap_sg(attachment->dev, table->sgl, table->nents, direction);
700 +static int vc_sm_dmabuf_mmap(struct dma_buf *dmabuf, struct vm_area_struct *vma)
702 + struct vc_sm_buffer *buf = dmabuf->priv;
705 + pr_debug("%s dmabuf %p, buf %p, vm_start %08lX\n", __func__, dmabuf,
706 + buf, vma->vm_start);
708 + mutex_lock(&buf->lock);
710 + /* now map it to userspace */
713 + ret = dma_mmap_coherent(&sm_state->pdev->dev, vma, buf->cookie,
714 + buf->dma_addr, buf->size);
717 + pr_err("Remapping memory failed, error: %d\n", ret);
721 + vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
723 + mutex_unlock(&buf->lock);
726 + pr_err("%s: failure mapping buffer to userspace\n",
732 +static void vc_sm_dma_buf_release(struct dma_buf *dmabuf)
734 + struct vc_sm_buffer *buffer;
739 + buffer = (struct vc_sm_buffer *)dmabuf->priv;
741 + mutex_lock(&buffer->lock);
743 + pr_debug("%s dmabuf %p, buffer %p\n", __func__, dmabuf, buffer);
745 + buffer->in_use = 0;
747 + /* Unmap on the VPU */
748 + vc_sm_vpu_free(buffer);
749 + pr_debug("%s vpu_free done\n", __func__);
751 + /* Unmap our dma_buf object (the vc_sm_buffer remains until released
754 + vc_sm_clean_up_dmabuf(buffer);
755 + pr_debug("%s clean_up dmabuf done\n", __func__);
757 + /* buffer->lock will be destroyed by vc_sm_release_resource if finished
758 + * with, otherwise unlocked. Do NOT unlock here.
760 + vc_sm_release_resource(buffer);
761 + pr_debug("%s done\n", __func__);
764 +static int vc_sm_dma_buf_begin_cpu_access(struct dma_buf *dmabuf,
765 + enum dma_data_direction direction)
767 + struct vc_sm_buffer *buf;
768 + struct vc_sm_dma_buf_attachment *a;
773 + buf = dmabuf->priv;
777 + mutex_lock(&buf->lock);
779 + list_for_each_entry(a, &buf->attachments, list) {
780 + dma_sync_sg_for_cpu(a->dev, a->sg_table.sgl,
781 + a->sg_table.nents, direction);
783 + mutex_unlock(&buf->lock);
788 +static int vc_sm_dma_buf_end_cpu_access(struct dma_buf *dmabuf,
789 + enum dma_data_direction direction)
791 + struct vc_sm_buffer *buf;
792 + struct vc_sm_dma_buf_attachment *a;
796 + buf = dmabuf->priv;
800 + mutex_lock(&buf->lock);
802 + list_for_each_entry(a, &buf->attachments, list) {
803 + dma_sync_sg_for_device(a->dev, a->sg_table.sgl,
804 + a->sg_table.nents, direction);
806 + mutex_unlock(&buf->lock);
811 +static const struct dma_buf_ops dma_buf_ops = {
812 + .map_dma_buf = vc_sm_map_dma_buf,
813 + .unmap_dma_buf = vc_sm_unmap_dma_buf,
814 + .mmap = vc_sm_dmabuf_mmap,
815 + .release = vc_sm_dma_buf_release,
816 + .attach = vc_sm_dma_buf_attach,
817 + .detach = vc_sm_dma_buf_detach,
818 + .begin_cpu_access = vc_sm_dma_buf_begin_cpu_access,
819 + .end_cpu_access = vc_sm_dma_buf_end_cpu_access,
822 +/* Dma_buf operations for chaining through to an imported dma_buf */
825 +int vc_sm_import_dma_buf_attach(struct dma_buf *dmabuf,
826 + struct dma_buf_attachment *attachment)
828 + struct vc_sm_buffer *buf = dmabuf->priv;
830 + if (!buf->imported)
832 + return buf->import.dma_buf->ops->attach(buf->import.dma_buf,
837 +void vc_sm_import_dma_buf_detatch(struct dma_buf *dmabuf,
838 + struct dma_buf_attachment *attachment)
840 + struct vc_sm_buffer *buf = dmabuf->priv;
842 + if (!buf->imported)
844 + buf->import.dma_buf->ops->detach(buf->import.dma_buf, attachment);
848 +struct sg_table *vc_sm_import_map_dma_buf(struct dma_buf_attachment *attachment,
849 + enum dma_data_direction direction)
851 + struct vc_sm_buffer *buf = attachment->dmabuf->priv;
853 + if (!buf->imported)
855 + return buf->import.dma_buf->ops->map_dma_buf(attachment,
860 +void vc_sm_import_unmap_dma_buf(struct dma_buf_attachment *attachment,
861 + struct sg_table *table,
862 + enum dma_data_direction direction)
864 + struct vc_sm_buffer *buf = attachment->dmabuf->priv;
866 + if (!buf->imported)
868 + buf->import.dma_buf->ops->unmap_dma_buf(attachment, table, direction);
872 +int vc_sm_import_dmabuf_mmap(struct dma_buf *dmabuf, struct vm_area_struct *vma)
874 + struct vc_sm_buffer *buf = dmabuf->priv;
876 + pr_debug("%s: mmap dma_buf %p, buf %p, imported db %p\n", __func__,
877 + dmabuf, buf, buf->import.dma_buf);
878 + if (!buf->imported) {
879 + pr_err("%s: mmap dma_buf %p- not an imported buffer\n",
883 + return buf->import.dma_buf->ops->mmap(buf->import.dma_buf, vma);
887 +void vc_sm_import_dma_buf_release(struct dma_buf *dmabuf)
889 + struct vc_sm_buffer *buf = dmabuf->priv;
891 + pr_debug("%s: Relasing dma_buf %p\n", __func__, dmabuf);
892 + mutex_lock(&buf->lock);
893 + if (!buf->imported)
898 + vc_sm_vpu_free(buf);
900 + vc_sm_release_resource(buf);
904 +int vc_sm_import_dma_buf_begin_cpu_access(struct dma_buf *dmabuf,
905 + enum dma_data_direction direction)
907 + struct vc_sm_buffer *buf = dmabuf->priv;
909 + if (!buf->imported)
911 + return buf->import.dma_buf->ops->begin_cpu_access(buf->import.dma_buf,
916 +int vc_sm_import_dma_buf_end_cpu_access(struct dma_buf *dmabuf,
917 + enum dma_data_direction direction)
919 + struct vc_sm_buffer *buf = dmabuf->priv;
921 + if (!buf->imported)
923 + return buf->import.dma_buf->ops->end_cpu_access(buf->import.dma_buf,
927 +static const struct dma_buf_ops dma_buf_import_ops = {
928 + .map_dma_buf = vc_sm_import_map_dma_buf,
929 + .unmap_dma_buf = vc_sm_import_unmap_dma_buf,
930 + .mmap = vc_sm_import_dmabuf_mmap,
931 + .release = vc_sm_import_dma_buf_release,
932 + .attach = vc_sm_import_dma_buf_attach,
933 + .detach = vc_sm_import_dma_buf_detatch,
934 + .begin_cpu_access = vc_sm_import_dma_buf_begin_cpu_access,
935 + .end_cpu_access = vc_sm_import_dma_buf_end_cpu_access,
938 +/* Import a dma_buf to be shared with VC. */
940 +vc_sm_cma_import_dmabuf_internal(struct vc_sm_privdata_t *private,
941 + struct dma_buf *dma_buf,
943 + struct dma_buf **imported_buf)
945 + DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
946 + struct vc_sm_buffer *buffer = NULL;
947 + struct vc_sm_import import = { };
948 + struct vc_sm_import_result result = { };
949 + struct dma_buf_attachment *attach = NULL;
950 + struct sg_table *sgt = NULL;
951 + dma_addr_t dma_addr;
955 + /* Setup our allocation parameters */
956 + pr_debug("%s: importing dma_buf %p/fd %d\n", __func__, dma_buf, fd);
959 + get_dma_buf(dma_buf);
961 + dma_buf = dma_buf_get(fd);
966 + attach = dma_buf_attach(dma_buf, &sm_state->pdev->dev);
967 + if (IS_ERR(attach)) {
968 + ret = PTR_ERR(attach);
972 + sgt = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
974 + ret = PTR_ERR(sgt);
978 + /* Verify that the address block is contiguous */
979 + if (sgt->nents != 1) {
984 + /* Allocate local buffer to track this allocation. */
985 + buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
991 + import.type = VC_SM_ALLOC_NON_CACHED;
992 + dma_addr = sg_dma_address(sgt->sgl);
993 + import.addr = (u32)dma_addr;
994 + if ((import.addr & 0xC0000000) != 0xC0000000) {
995 + pr_err("%s: Expecting an uncached alias for dma_addr %pad\n",
996 + __func__, &dma_addr);
997 + import.addr |= 0xC0000000;
999 + import.size = sg_dma_len(sgt->sgl);
1000 + import.allocator = current->tgid;
1001 + import.kernel_id = get_kernel_id(buffer);
1003 + memcpy(import.name, VC_SM_RESOURCE_NAME_DEFAULT,
1004 + sizeof(VC_SM_RESOURCE_NAME_DEFAULT));
1006 + pr_debug("[%s]: attempt to import \"%s\" data - type %u, addr %pad, size %u.\n",
1007 + __func__, import.name, import.type, &dma_addr, import.size);
1009 + /* Allocate the videocore buffer. */
1010 + status = vc_sm_cma_vchi_import(sm_state->sm_handle, &import, &result,
1011 + &sm_state->int_trans_id);
1012 + if (status == -EINTR) {
1013 + pr_debug("[%s]: requesting import memory action restart (trans_id: %u)\n",
1014 + __func__, sm_state->int_trans_id);
1015 + ret = -ERESTARTSYS;
1016 + private->restart_sys = -EINTR;
1017 + private->int_action = VC_SM_MSG_TYPE_IMPORT;
1019 + } else if (status || !result.res_handle) {
1020 + pr_debug("[%s]: failed to import memory on videocore (status: %u, trans_id: %u)\n",
1021 + __func__, status, sm_state->int_trans_id);
1026 + mutex_init(&buffer->lock);
1027 + INIT_LIST_HEAD(&buffer->attachments);
1028 + memcpy(buffer->name, import.name,
1029 + min(sizeof(buffer->name), sizeof(import.name) - 1));
1031 + /* Keep track of the buffer we created. */
1032 + buffer->private = private;
1033 + buffer->vc_handle = result.res_handle;
1034 + buffer->size = import.size;
1035 + buffer->vpu_state = VPU_MAPPED;
1037 + buffer->imported = 1;
1038 + buffer->import.dma_buf = dma_buf;
1040 + buffer->import.attach = attach;
1041 + buffer->import.sgt = sgt;
1042 + buffer->dma_addr = dma_addr;
1043 + buffer->in_use = 1;
1044 + buffer->kernel_id = import.kernel_id;
1047 + * We're done - we need to export a new dmabuf chaining through most
1048 + * functions, but enabling us to release our own internal references
1051 + exp_info.ops = &dma_buf_import_ops;
1052 + exp_info.size = import.size;
1053 + exp_info.flags = O_RDWR;
1054 + exp_info.priv = buffer;
1056 + buffer->dma_buf = dma_buf_export(&exp_info);
1057 + if (IS_ERR(buffer->dma_buf)) {
1058 + ret = PTR_ERR(buffer->dma_buf);
1062 + vc_sm_add_resource(private, buffer);
1064 + *imported_buf = buffer->dma_buf;
1069 + if (result.res_handle) {
1070 + struct vc_sm_free_t free = { result.res_handle, 0 };
1072 + vc_sm_cma_vchi_free(sm_state->sm_handle, &free,
1073 + &sm_state->int_trans_id);
1075 + free_kernel_id(import.kernel_id);
1078 + dma_buf_unmap_attachment(attach, sgt, DMA_BIDIRECTIONAL);
1080 + dma_buf_detach(dma_buf, attach);
1081 + dma_buf_put(dma_buf);
1085 +static int vc_sm_cma_vpu_alloc(u32 size, u32 align, const char *name,
1086 + u32 mem_handle, struct vc_sm_buffer **ret_buffer)
1088 + DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
1089 + struct vc_sm_buffer *buffer = NULL;
1090 + struct sg_table *sgt;
1094 + /* Align to the user requested align */
1095 + aligned_size = ALIGN(size, align);
1096 + /* and then to a page boundary */
1097 + aligned_size = PAGE_ALIGN(aligned_size);
1099 + if (!aligned_size)
1102 + /* Allocate local buffer to track this allocation. */
1103 + buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
1107 + mutex_init(&buffer->lock);
1108 + /* Acquire the mutex as vc_sm_release_resource will release it in the
1111 + mutex_lock(&buffer->lock);
1113 + buffer->cookie = dma_alloc_coherent(&sm_state->pdev->dev,
1114 + aligned_size, &buffer->dma_addr,
1116 + if (!buffer->cookie) {
1117 + pr_err("[%s]: dma_alloc_coherent alloc of %d bytes failed\n",
1118 + __func__, aligned_size);
1123 + pr_debug("[%s]: alloc of %d bytes success\n",
1124 + __func__, aligned_size);
1126 + sgt = kmalloc(sizeof(*sgt), GFP_KERNEL);
1132 + ret = dma_get_sgtable(&sm_state->pdev->dev, sgt, buffer->cookie,
1133 + buffer->dma_addr, buffer->size);
1135 + pr_err("failed to get scatterlist from DMA API\n");
1140 + buffer->alloc.sg_table = sgt;
1142 + INIT_LIST_HEAD(&buffer->attachments);
1144 + memcpy(buffer->name, name,
1145 + min(sizeof(buffer->name), strlen(name)));
1147 + exp_info.ops = &dma_buf_ops;
1148 + exp_info.size = aligned_size;
1149 + exp_info.flags = O_RDWR;
1150 + exp_info.priv = buffer;
1152 + buffer->dma_buf = dma_buf_export(&exp_info);
1153 + if (IS_ERR(buffer->dma_buf)) {
1154 + ret = PTR_ERR(buffer->dma_buf);
1157 + buffer->dma_addr = (u32)sg_dma_address(buffer->alloc.sg_table->sgl);
1158 + if ((buffer->dma_addr & 0xC0000000) != 0xC0000000) {
1159 + pr_warn_once("%s: Expecting an uncached alias for dma_addr %pad\n",
1160 + __func__, &buffer->dma_addr);
1161 + buffer->dma_addr |= 0xC0000000;
1163 + buffer->private = sm_state->vpu_allocs;
1165 + buffer->vc_handle = mem_handle;
1166 + buffer->vpu_state = VPU_MAPPED;
1167 + buffer->vpu_allocated = 1;
1168 + buffer->size = size;
1170 + * Create an ID that will be passed along with our message so
1171 + * that when we service the release reply, we can look up which
1172 + * resource is being released.
1174 + buffer->kernel_id = get_kernel_id(buffer);
1176 + vc_sm_add_resource(sm_state->vpu_allocs, buffer);
1178 + mutex_unlock(&buffer->lock);
1180 + *ret_buffer = buffer;
1184 + vc_sm_release_resource(buffer);
1189 +vc_sm_vpu_event(struct sm_instance *instance, struct vc_sm_result_t *reply,
1192 + switch (reply->trans_id & ~0x80000000) {
1193 + case VC_SM_MSG_TYPE_CLIENT_VERSION:
1195 + /* Acknowledge that the firmware supports the version command */
1196 + pr_debug("%s: firmware acked version msg. Require release cb\n",
1198 + sm_state->require_released_callback = true;
1201 + case VC_SM_MSG_TYPE_RELEASED:
1203 + struct vc_sm_released *release = (struct vc_sm_released *)reply;
1204 + struct vc_sm_buffer *buffer =
1205 + lookup_kernel_id(release->kernel_id);
1207 + pr_err("%s: VC released a buffer that is already released, kernel_id %d\n",
1208 + __func__, release->kernel_id);
1211 + mutex_lock(&buffer->lock);
1213 + pr_debug("%s: Released addr %08x, size %u, id %08x, mem_handle %08x\n",
1214 + __func__, release->addr, release->size,
1215 + release->kernel_id, release->vc_handle);
1217 + buffer->vc_handle = 0;
1218 + buffer->vpu_state = VPU_NOT_MAPPED;
1219 + free_kernel_id(release->kernel_id);
1221 + if (buffer->vpu_allocated) {
1222 + /* VPU allocation, so release the dmabuf which will
1223 + * trigger the clean up.
1225 + mutex_unlock(&buffer->lock);
1226 + dma_buf_put(buffer->dma_buf);
1228 + vc_sm_release_resource(buffer);
1232 + case VC_SM_MSG_TYPE_VC_MEM_REQUEST:
1234 + struct vc_sm_buffer *buffer = NULL;
1235 + struct vc_sm_vc_mem_request *req =
1236 + (struct vc_sm_vc_mem_request *)reply;
1237 + struct vc_sm_vc_mem_request_result reply;
1240 + pr_debug("%s: Request %u bytes of memory, align %d name %s, trans_id %08x\n",
1241 + __func__, req->size, req->align, req->name,
1243 + ret = vc_sm_cma_vpu_alloc(req->size, req->align, req->name,
1244 + req->vc_handle, &buffer);
1246 + reply.trans_id = req->trans_id;
1248 + reply.addr = buffer->dma_addr;
1249 + reply.kernel_id = buffer->kernel_id;
1250 + pr_debug("%s: Allocated resource buffer %p, addr %pad\n",
1251 + __func__, buffer, &buffer->dma_addr);
1253 + pr_err("%s: Allocation failed size %u, name %s, vc_handle %u\n",
1254 + __func__, req->size, req->name, req->vc_handle);
1256 + reply.kernel_id = 0;
1258 + vc_sm_vchi_client_vc_mem_req_reply(sm_state->sm_handle, &reply,
1259 + &sm_state->int_trans_id);
1264 + pr_err("%s: Unknown vpu cmd %x\n", __func__, reply->trans_id);
1269 +/* Userspace handling */
1271 + * Open the device. Creates a private state to help track all allocation
1272 + * associated with this device.
1274 +static int vc_sm_cma_open(struct inode *inode, struct file *file)
1276 + /* Make sure the device was started properly. */
1278 + pr_err("[%s]: invalid device\n", __func__);
1282 + file->private_data = vc_sm_cma_create_priv_data(current->tgid);
1283 + if (!file->private_data) {
1284 + pr_err("[%s]: failed to create data tracker\n", __func__);
1293 + * Close the vcsm-cma device.
1294 + * All allocations are file descriptors to the dmabuf objects, so we will get
1295 + * the clean up request on those as those are cleaned up.
1297 +static int vc_sm_cma_release(struct inode *inode, struct file *file)
1299 + struct vc_sm_privdata_t *file_data =
1300 + (struct vc_sm_privdata_t *)file->private_data;
1303 + /* Make sure the device was started properly. */
1304 + if (!sm_state || !file_data) {
1305 + pr_err("[%s]: invalid device\n", __func__);
1310 + pr_debug("[%s]: using private data %p\n", __func__, file_data);
1312 + /* Terminate the private data. */
1320 + * Allocate a shared memory handle and block.
1321 + * Allocation is from CMA, and then imported into the VPU mappings.
1323 +int vc_sm_cma_ioctl_alloc(struct vc_sm_privdata_t *private,
1324 + struct vc_sm_cma_ioctl_alloc *ioparam)
1326 + DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
1327 + struct vc_sm_buffer *buffer = NULL;
1328 + struct vc_sm_import import = { 0 };
1329 + struct vc_sm_import_result result = { 0 };
1330 + struct dma_buf *dmabuf = NULL;
1331 + struct sg_table *sgt;
1337 + aligned_size = PAGE_ALIGN(ioparam->size);
1339 + if (!aligned_size)
1342 + /* Allocate local buffer to track this allocation. */
1343 + buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
1349 + buffer->cookie = dma_alloc_coherent(&sm_state->pdev->dev,
1351 + &buffer->dma_addr,
1353 + if (!buffer->cookie) {
1354 + pr_err("[%s]: dma_alloc_coherent alloc of %d bytes failed\n",
1355 + __func__, aligned_size);
1360 + import.type = VC_SM_ALLOC_NON_CACHED;
1361 + import.allocator = current->tgid;
1363 + if (*ioparam->name)
1364 + memcpy(import.name, ioparam->name, sizeof(import.name) - 1);
1366 + memcpy(import.name, VC_SM_RESOURCE_NAME_DEFAULT,
1367 + sizeof(VC_SM_RESOURCE_NAME_DEFAULT));
1369 + mutex_init(&buffer->lock);
1370 + INIT_LIST_HEAD(&buffer->attachments);
1371 + memcpy(buffer->name, import.name,
1372 + min(sizeof(buffer->name), sizeof(import.name) - 1));
1374 + exp_info.ops = &dma_buf_ops;
1375 + exp_info.size = aligned_size;
1376 + exp_info.flags = O_RDWR;
1377 + exp_info.priv = buffer;
1379 + dmabuf = dma_buf_export(&exp_info);
1380 + if (IS_ERR(dmabuf)) {
1381 + ret = PTR_ERR(dmabuf);
1384 + buffer->dma_buf = dmabuf;
1386 + import.addr = buffer->dma_addr;
1387 + import.size = aligned_size;
1388 + import.kernel_id = get_kernel_id(buffer);
1390 + /* Wrap it into a videocore buffer. */
1391 + status = vc_sm_cma_vchi_import(sm_state->sm_handle, &import, &result,
1392 + &sm_state->int_trans_id);
1393 + if (status == -EINTR) {
1394 + pr_debug("[%s]: requesting import memory action restart (trans_id: %u)\n",
1395 + __func__, sm_state->int_trans_id);
1396 + ret = -ERESTARTSYS;
1397 + private->restart_sys = -EINTR;
1398 + private->int_action = VC_SM_MSG_TYPE_IMPORT;
1400 + } else if (status || !result.res_handle) {
1401 + pr_err("[%s]: failed to import memory on videocore (status: %u, trans_id: %u)\n",
1402 + __func__, status, sm_state->int_trans_id);
1407 + /* Keep track of the buffer we created. */
1408 + buffer->private = private;
1409 + buffer->vc_handle = result.res_handle;
1410 + buffer->size = import.size;
1411 + buffer->vpu_state = VPU_MAPPED;
1412 + buffer->kernel_id = import.kernel_id;
1414 + sgt = kmalloc(sizeof(*sgt), GFP_KERNEL);
1420 + ret = dma_get_sgtable(&sm_state->pdev->dev, sgt, buffer->cookie,
1421 + buffer->dma_addr, buffer->size);
1423 + /* FIXME: error handling */
1424 + pr_err("failed to get scatterlist from DMA API\n");
1429 + buffer->alloc.sg_table = sgt;
1431 + fd = dma_buf_fd(dmabuf, O_CLOEXEC);
1435 + vc_sm_add_resource(private, buffer);
1437 + pr_debug("[%s]: Added resource as fd %d, buffer %p, private %p, dma_addr %pad\n",
1438 + __func__, fd, buffer, private, &buffer->dma_addr);
1441 + ioparam->handle = fd;
1442 + ioparam->vc_handle = buffer->vc_handle;
1443 + ioparam->dma_addr = buffer->dma_addr;
1447 + pr_err("[%s]: something failed - cleanup. ret %d\n", __func__, ret);
1450 + /* dmabuf has been exported, therefore allow dmabuf cleanup to
1453 + dma_buf_put(dmabuf);
1455 + /* No dmabuf, therefore just free the buffer here */
1456 + if (buffer->cookie)
1457 + dma_free_coherent(&sm_state->pdev->dev, buffer->size,
1458 + buffer->cookie, buffer->dma_addr);
1464 +#ifndef CONFIG_ARM64
1465 +/* Converts VCSM_CACHE_OP_* to an operating function. */
1466 +static void (*cache_op_to_func(const unsigned int cache_op))
1467 + (const void*, const void*)
1469 + switch (cache_op) {
1470 + case VC_SM_CACHE_OP_NOP:
1473 + case VC_SM_CACHE_OP_INV:
1474 + case VC_SM_CACHE_OP_CLEAN:
1475 + case VC_SM_CACHE_OP_FLUSH:
1476 + return dmac_flush_range;
1479 + pr_err("[%s]: Invalid cache_op: 0x%08x\n", __func__, cache_op);
1485 + * Clean/invalid/flush cache of which buffer is already pinned (i.e. accessed).
1487 +static int clean_invalid_contig_2d(const void __user *addr,
1488 + const size_t block_count,
1489 + const size_t block_size,
1490 + const size_t stride,
1491 + const unsigned int cache_op)
1494 + void (*op_fn)(const void *start, const void *end);
1496 + if (!block_size) {
1497 + pr_err("[%s]: size cannot be 0\n", __func__);
1501 + op_fn = cache_op_to_func(cache_op);
1505 + for (i = 0; i < block_count; i ++, addr += stride)
1506 + op_fn(addr, addr + block_size);
1511 +static int vc_sm_cma_clean_invalid2(unsigned int cmdnr, unsigned long arg)
1513 + struct vc_sm_cma_ioctl_clean_invalid2 ioparam;
1514 + struct vc_sm_cma_ioctl_clean_invalid_block *block = NULL;
1517 + /* Get parameter data. */
1518 + if (copy_from_user(&ioparam, (void *)arg, sizeof(ioparam))) {
1519 + pr_err("[%s]: failed to copy-from-user header for cmd %x\n",
1523 + block = kmalloc(ioparam.op_count * sizeof(*block), GFP_KERNEL);
1527 + if (copy_from_user(block, (void *)(arg + sizeof(ioparam)),
1528 + ioparam.op_count * sizeof(*block)) != 0) {
1529 + pr_err("[%s]: failed to copy-from-user payload for cmd %x\n",
1535 + for (i = 0; i < ioparam.op_count; i++) {
1536 + const struct vc_sm_cma_ioctl_clean_invalid_block * const op =
1539 + if (op->invalidate_mode == VC_SM_CACHE_OP_NOP)
1542 + ret = clean_invalid_contig_2d((void __user *)op->start_address,
1543 + op->block_count, op->block_size,
1544 + op->inter_block_stride,
1545 + op->invalidate_mode);
1556 +static long vc_sm_cma_ioctl(struct file *file, unsigned int cmd,
1557 + unsigned long arg)
1560 + unsigned int cmdnr = _IOC_NR(cmd);
1561 + struct vc_sm_privdata_t *file_data =
1562 + (struct vc_sm_privdata_t *)file->private_data;
1564 + /* Validate we can work with this device. */
1565 + if (!sm_state || !file_data) {
1566 + pr_err("[%s]: invalid device\n", __func__);
1570 + /* Action is a re-post of a previously interrupted action? */
1571 + if (file_data->restart_sys == -EINTR) {
1572 + pr_debug("[%s]: clean up of action %u (trans_id: %u) following EINTR\n",
1573 + __func__, file_data->int_action,
1574 + file_data->int_trans_id);
1576 + file_data->restart_sys = 0;
1579 + /* Now process the command. */
1581 + /* New memory allocation.
1583 + case VC_SM_CMA_CMD_ALLOC:
1585 + struct vc_sm_cma_ioctl_alloc ioparam;
1587 + /* Get the parameter data. */
1588 + if (copy_from_user
1589 + (&ioparam, (void *)arg, sizeof(ioparam)) != 0) {
1590 + pr_err("[%s]: failed to copy-from-user for cmd %x\n",
1596 + ret = vc_sm_cma_ioctl_alloc(file_data, &ioparam);
1598 + (copy_to_user((void *)arg, &ioparam,
1599 + sizeof(ioparam)) != 0)) {
1600 + /* FIXME: Release allocation */
1601 + pr_err("[%s]: failed to copy-to-user for cmd %x\n",
1608 + case VC_SM_CMA_CMD_IMPORT_DMABUF:
1610 + struct vc_sm_cma_ioctl_import_dmabuf ioparam;
1611 + struct dma_buf *new_dmabuf;
1613 + /* Get the parameter data. */
1614 + if (copy_from_user
1615 + (&ioparam, (void *)arg, sizeof(ioparam)) != 0) {
1616 + pr_err("[%s]: failed to copy-from-user for cmd %x\n",
1622 + ret = vc_sm_cma_import_dmabuf_internal(file_data,
1624 + ioparam.dmabuf_fd,
1628 + struct vc_sm_buffer *buf = new_dmabuf->priv;
1630 + ioparam.size = buf->size;
1631 + ioparam.handle = dma_buf_fd(new_dmabuf,
1633 + ioparam.vc_handle = buf->vc_handle;
1634 + ioparam.dma_addr = buf->dma_addr;
1636 + if (ioparam.handle < 0 ||
1637 + (copy_to_user((void *)arg, &ioparam,
1638 + sizeof(ioparam)) != 0)) {
1639 + dma_buf_put(new_dmabuf);
1640 + /* FIXME: Release allocation */
1647 +#ifndef CONFIG_ARM64
1649 + * Flush/Invalidate the cache for a given mapping.
1650 + * Blocks must be pinned (i.e. accessed) before this call.
1652 + case VC_SM_CMA_CMD_CLEAN_INVALID2:
1653 + ret = vc_sm_cma_clean_invalid2(cmdnr, arg);
1658 + pr_debug("[%s]: cmd %x tgid %u, owner %u\n", __func__, cmdnr,
1659 + current->tgid, file_data->pid);
1668 +#ifdef CONFIG_COMPAT
1669 +struct vc_sm_cma_ioctl_clean_invalid2_32 {
1671 + struct vc_sm_cma_ioctl_clean_invalid_block_32 {
1672 + u16 invalidate_mode;
1674 + compat_uptr_t start_address;
1676 + u32 inter_block_stride;
1680 +#define VC_SM_CMA_CMD_CLEAN_INVALID2_32\
1681 + _IOR(VC_SM_CMA_MAGIC_TYPE, VC_SM_CMA_CMD_CLEAN_INVALID2,\
1682 + struct vc_sm_cma_ioctl_clean_invalid2_32)
1684 +static long vc_sm_cma_compat_ioctl(struct file *file, unsigned int cmd,
1685 + unsigned long arg)
1688 + case VC_SM_CMA_CMD_CLEAN_INVALID2_32:
1693 + return vc_sm_cma_ioctl(file, cmd, arg);
1698 +/* Device operations that we managed in this driver. */
1699 +static const struct file_operations vc_sm_ops = {
1700 + .owner = THIS_MODULE,
1701 + .unlocked_ioctl = vc_sm_cma_ioctl,
1702 +#ifdef CONFIG_COMPAT
1703 + .compat_ioctl = vc_sm_cma_compat_ioctl,
1705 + .open = vc_sm_cma_open,
1706 + .release = vc_sm_cma_release,
1709 +/* Driver load/unload functions */
1710 +/* Videocore connected. */
1711 +static void vc_sm_connected_init(void)
1714 + struct vchiq_instance *vchiq_instance;
1715 + struct vc_sm_version version;
1716 + struct vc_sm_result_t version_result;
1718 + pr_info("[%s]: start\n", __func__);
1721 + * Initialize and create a VCHI connection for the shared memory service
1722 + * running on videocore.
1724 + ret = vchiq_initialise(&vchiq_instance);
1726 + pr_err("[%s]: failed to initialise VCHI instance (ret=%d)\n",
1732 + ret = vchiq_connect(vchiq_instance);
1734 + pr_err("[%s]: failed to connect VCHI instance (ret=%d)\n",
1740 + /* Initialize an instance of the shared memory service. */
1741 + sm_state->sm_handle = vc_sm_cma_vchi_init(vchiq_instance, 1,
1743 + if (!sm_state->sm_handle) {
1744 + pr_err("[%s]: failed to initialize shared memory service\n",
1750 + /* Create a debug fs directory entry (root). */
1751 + sm_state->dir_root = debugfs_create_dir(VC_SM_DIR_ROOT_NAME, NULL);
1753 + sm_state->dir_state.show = &vc_sm_cma_global_state_show;
1754 + sm_state->dir_state.dir_entry =
1755 + debugfs_create_file(VC_SM_STATE, 0444, sm_state->dir_root,
1756 + &sm_state->dir_state,
1757 + &vc_sm_cma_debug_fs_fops);
1759 + INIT_LIST_HEAD(&sm_state->buffer_list);
1761 + /* Create a shared memory device. */
1762 + sm_state->misc_dev.minor = MISC_DYNAMIC_MINOR;
1763 + sm_state->misc_dev.name = DEVICE_NAME;
1764 + sm_state->misc_dev.fops = &vc_sm_ops;
1765 + sm_state->misc_dev.parent = NULL;
1766 + /* Temporarily set as 666 until udev rules have been sorted */
1767 + sm_state->misc_dev.mode = 0666;
1768 + ret = misc_register(&sm_state->misc_dev);
1770 + pr_err("vcsm-cma: failed to register misc device.\n");
1771 + goto err_remove_debugfs;
1774 + sm_state->data_knl = vc_sm_cma_create_priv_data(0);
1775 + if (!sm_state->data_knl) {
1776 + pr_err("[%s]: failed to create kernel private data tracker\n",
1778 + goto err_remove_misc_dev;
1781 + version.version = 2;
1782 + ret = vc_sm_cma_vchi_client_version(sm_state->sm_handle, &version,
1784 + &sm_state->int_trans_id);
1786 + pr_err("[%s]: Failed to send version request %d\n", __func__,
1792 + pr_info("[%s]: installed successfully\n", __func__);
1795 +err_remove_misc_dev:
1796 + misc_deregister(&sm_state->misc_dev);
1797 +err_remove_debugfs:
1798 + debugfs_remove_recursive(sm_state->dir_root);
1799 + vc_sm_cma_vchi_stop(&sm_state->sm_handle);
1802 +/* Driver loading. */
1803 +static int bcm2835_vc_sm_cma_probe(struct platform_device *pdev)
1805 + pr_info("%s: Videocore shared memory driver\n", __func__);
1807 + sm_state = devm_kzalloc(&pdev->dev, sizeof(*sm_state), GFP_KERNEL);
1810 + sm_state->pdev = pdev;
1811 + mutex_init(&sm_state->map_lock);
1813 + spin_lock_init(&sm_state->kernelid_map_lock);
1814 + idr_init_base(&sm_state->kernelid_map, 1);
1816 + pdev->dev.dma_parms = devm_kzalloc(&pdev->dev,
1817 + sizeof(*pdev->dev.dma_parms),
1819 + /* dma_set_max_seg_size checks if dma_parms is NULL. */
1820 + dma_set_max_seg_size(&pdev->dev, 0x3FFFFFFF);
1822 + vchiq_add_connected_callback(vc_sm_connected_init);
1826 +/* Driver unloading. */
1827 +static int bcm2835_vc_sm_cma_remove(struct platform_device *pdev)
1829 + pr_debug("[%s]: start\n", __func__);
1831 + misc_deregister(&sm_state->misc_dev);
1833 + /* Remove all proc entries. */
1834 + debugfs_remove_recursive(sm_state->dir_root);
1836 + /* Stop the videocore shared memory service. */
1837 + vc_sm_cma_vchi_stop(&sm_state->sm_handle);
1841 + idr_destroy(&sm_state->kernelid_map);
1843 + /* Free the memory for the state structure. */
1844 + mutex_destroy(&sm_state->map_lock);
1847 + pr_debug("[%s]: end\n", __func__);
1851 +/* Kernel API calls */
1852 +/* Get an internal resource handle mapped from the external one. */
1853 +int vc_sm_cma_int_handle(void *handle)
1855 + struct dma_buf *dma_buf = (struct dma_buf *)handle;
1856 + struct vc_sm_buffer *buf;
1858 + /* Validate we can work with this device. */
1859 + if (!sm_state || !handle) {
1860 + pr_err("[%s]: invalid input\n", __func__);
1864 + buf = (struct vc_sm_buffer *)dma_buf->priv;
1865 + return buf->vc_handle;
1867 +EXPORT_SYMBOL_GPL(vc_sm_cma_int_handle);
1869 +/* Free a previously allocated shared memory handle and block. */
1870 +int vc_sm_cma_free(void *handle)
1872 + struct dma_buf *dma_buf = (struct dma_buf *)handle;
1874 + /* Validate we can work with this device. */
1875 + if (!sm_state || !handle) {
1876 + pr_err("[%s]: invalid input\n", __func__);
1880 + pr_debug("%s: handle %p/dmabuf %p\n", __func__, handle, dma_buf);
1882 + dma_buf_put(dma_buf);
1886 +EXPORT_SYMBOL_GPL(vc_sm_cma_free);
1888 +/* Import a dmabuf to be shared with VC. */
1889 +int vc_sm_cma_import_dmabuf(struct dma_buf *src_dmabuf, void **handle)
1891 + struct dma_buf *new_dma_buf;
1894 + /* Validate we can work with this device. */
1895 + if (!sm_state || !src_dmabuf || !handle) {
1896 + pr_err("[%s]: invalid input\n", __func__);
1900 + ret = vc_sm_cma_import_dmabuf_internal(sm_state->data_knl, src_dmabuf,
1901 + -1, &new_dma_buf);
1904 + pr_debug("%s: imported to ptr %p\n", __func__, new_dma_buf);
1906 + /* Assign valid handle at this time.*/
1907 + *handle = new_dma_buf;
1910 + * succeeded in importing the dma_buf, but then
1911 + * failed to look it up again. How?
1912 + * Release the fd again.
1914 + pr_err("%s: imported vc_sm_cma_get_buffer failed %d\n",
1920 +EXPORT_SYMBOL_GPL(vc_sm_cma_import_dmabuf);
1922 +static struct platform_driver bcm2835_vcsm_cma_driver = {
1923 + .probe = bcm2835_vc_sm_cma_probe,
1924 + .remove = bcm2835_vc_sm_cma_remove,
1926 + .name = DEVICE_NAME,
1927 + .owner = THIS_MODULE,
1931 +module_platform_driver(bcm2835_vcsm_cma_driver);
1933 +MODULE_AUTHOR("Dave Stevenson");
1934 +MODULE_DESCRIPTION("VideoCore CMA Shared Memory Driver");
1935 +MODULE_LICENSE("GPL v2");
1936 +MODULE_ALIAS("platform:vcsm-cma");
1938 +++ b/drivers/staging/vc04_services/vc-sm-cma/vc_sm.h
1940 +/* SPDX-License-Identifier: GPL-2.0 */
1943 + * VideoCore Shared Memory driver using CMA.
1945 + * Copyright: 2018, Raspberry Pi (Trading) Ltd
1952 +#include <linux/device.h>
1953 +#include <linux/dma-direction.h>
1954 +#include <linux/kref.h>
1955 +#include <linux/mm_types.h>
1956 +#include <linux/mutex.h>
1957 +#include <linux/rbtree.h>
1958 +#include <linux/sched.h>
1959 +#include <linux/shrinker.h>
1960 +#include <linux/types.h>
1961 +#include <linux/miscdevice.h>
1963 +#define VC_SM_MAX_NAME_LEN 32
1965 +enum vc_sm_vpu_mapping_state {
1971 +struct vc_sm_alloc_data {
1972 + unsigned long num_pages;
1974 + struct sg_table *sg_table;
1977 +struct vc_sm_imported {
1978 + struct dma_buf *dma_buf;
1979 + struct dma_buf_attachment *attach;
1980 + struct sg_table *sgt;
1983 +struct vc_sm_buffer {
1984 + struct list_head global_buffer_list; /* Global list of buffers. */
1986 + /* Index in the kernel_id idr so that we can find the
1987 + * mmal_msg_context again when servicing the VCHI reply.
1993 + /* Lock over all the following state for this buffer */
1994 + struct mutex lock;
1995 + struct list_head attachments;
1997 + char name[VC_SM_MAX_NAME_LEN];
1999 + int in_use:1; /* Kernel is still using this resource */
2000 + int imported:1; /* Imported dmabuf */
2002 + enum vc_sm_vpu_mapping_state vpu_state;
2003 + u32 vc_handle; /* VideoCore handle for this buffer */
2004 + int vpu_allocated; /*
2005 + * The VPU made this allocation. Release the
2006 + * local dma_buf when the VPU releases the
2010 + /* DMABUF related fields */
2011 + struct dma_buf *dma_buf;
2012 + dma_addr_t dma_addr;
2015 + struct vc_sm_privdata_t *private;
2018 + struct vc_sm_alloc_data alloc;
2019 + struct vc_sm_imported import;
2025 +++ b/drivers/staging/vc04_services/vc-sm-cma/vc_sm_cma_vchi.c
2027 +// SPDX-License-Identifier: GPL-2.0
2029 + * VideoCore Shared Memory CMA allocator
2031 + * Copyright: 2018, Raspberry Pi (Trading) Ltd
2032 + * Copyright 2011-2012 Broadcom Corporation. All rights reserved.
2034 + * Based on vmcs_sm driver from Broadcom Corporation.
2038 +/* ---- Include Files ----------------------------------------------------- */
2039 +#include <linux/completion.h>
2040 +#include <linux/kernel.h>
2041 +#include <linux/kthread.h>
2042 +#include <linux/list.h>
2043 +#include <linux/mutex.h>
2044 +#include <linux/semaphore.h>
2045 +#include <linux/slab.h>
2046 +#include <linux/types.h>
2048 +#include "vc_sm_cma_vchi.h"
2050 +#define VC_SM_VER 1
2051 +#define VC_SM_MIN_VER 0
2053 +/* ---- Private Constants and Types -------------------------------------- */
2055 +/* Command blocks come from a pool */
2056 +#define SM_MAX_NUM_CMD_RSP_BLKS 32
2058 +/* The number of supported connections */
2059 +#define SM_MAX_NUM_CONNECTIONS 3
2061 +struct sm_cmd_rsp_blk {
2062 + struct list_head head; /* To create lists */
2063 + /* To be signaled when the response is there */
2064 + struct completion cmplt;
2069 + u8 msg[VC_SM_MAX_MSG_LEN];
2077 +struct sm_instance {
2078 + u32 num_connections;
2079 + unsigned int service_handle[SM_MAX_NUM_CONNECTIONS];
2080 + struct task_struct *io_thread;
2081 + struct completion io_cmplt;
2083 + vpu_event_cb vpu_event;
2085 + /* Mutex over the following lists */
2086 + struct mutex lock;
2088 + struct list_head cmd_list;
2089 + struct list_head rsp_list;
2090 + struct list_head dead_list;
2092 + struct sm_cmd_rsp_blk free_blk[SM_MAX_NUM_CMD_RSP_BLKS];
2094 + /* Mutex over the free_list */
2095 + struct mutex free_lock;
2096 + struct list_head free_list;
2098 + struct semaphore free_sema;
2102 +/* ---- Private Variables ------------------------------------------------ */
2104 +/* ---- Private Function Prototypes -------------------------------------- */
2106 +/* ---- Private Functions ------------------------------------------------ */
2108 +bcm2835_vchi_msg_queue(unsigned int handle,
2110 + unsigned int size)
2112 + return vchiq_queue_kernel_message(handle, data, size);
2116 +sm_cmd_rsp_blk *vc_vchi_cmd_create(struct sm_instance *instance,
2117 + enum vc_sm_msg_type id, void *msg,
2118 + u32 size, int wait)
2120 + struct sm_cmd_rsp_blk *blk;
2121 + struct vc_sm_msg_hdr_t *hdr;
2123 + if (down_interruptible(&instance->free_sema)) {
2124 + blk = kmalloc(sizeof(*blk), GFP_KERNEL);
2129 + init_completion(&blk->cmplt);
2131 + mutex_lock(&instance->free_lock);
2133 + list_first_entry(&instance->free_list,
2134 + struct sm_cmd_rsp_blk, head);
2135 + list_del(&blk->head);
2136 + mutex_unlock(&instance->free_lock);
2141 + blk->length = sizeof(*hdr) + size;
2143 + hdr = (struct vc_sm_msg_hdr_t *)blk->msg;
2145 + mutex_lock(&instance->lock);
2146 + instance->trans_id++;
2148 + * Retain the top bit for identifying asynchronous events, or VPU cmds.
2150 + instance->trans_id &= ~0x80000000;
2151 + hdr->trans_id = instance->trans_id;
2152 + blk->id = instance->trans_id;
2153 + mutex_unlock(&instance->lock);
2156 + memcpy(hdr->body, msg, size);
2162 +vc_vchi_cmd_delete(struct sm_instance *instance, struct sm_cmd_rsp_blk *blk)
2169 + mutex_lock(&instance->free_lock);
2170 + list_add(&blk->head, &instance->free_list);
2171 + mutex_unlock(&instance->free_lock);
2172 + up(&instance->free_sema);
2175 +static void vc_sm_cma_vchi_rx_ack(struct sm_instance *instance,
2176 + struct sm_cmd_rsp_blk *cmd,
2177 + struct vc_sm_result_t *reply,
2180 + mutex_lock(&instance->lock);
2181 + list_for_each_entry(cmd,
2182 + &instance->rsp_list,
2184 + if (cmd->id == reply->trans_id)
2187 + mutex_unlock(&instance->lock);
2189 + if (&cmd->head == &instance->rsp_list) {
2190 + //pr_debug("%s: received response %u, throw away...",
2191 + pr_err("%s: received response %u, throw away...",
2194 + } else if (reply_len > sizeof(cmd->msg)) {
2195 + pr_err("%s: reply too big (%u) %u, throw away...",
2196 + __func__, reply_len,
2199 + memcpy(cmd->msg, reply,
2201 + complete(&cmd->cmplt);
2205 +static int vc_sm_cma_vchi_videocore_io(void *arg)
2207 + struct sm_instance *instance = arg;
2208 + struct sm_cmd_rsp_blk *cmd = NULL, *cmd_tmp;
2209 + struct vc_sm_result_t *reply;
2210 + struct vchiq_header *header;
2216 + vchiq_release_service(instance->service_handle[0]);
2219 + if (wait_for_completion_interruptible(&instance->io_cmplt))
2221 + vchiq_use_service(instance->service_handle[0]);
2226 + * Get new command and move it to response list
2228 + mutex_lock(&instance->lock);
2229 + if (list_empty(&instance->cmd_list)) {
2230 + /* no more commands to process */
2231 + mutex_unlock(&instance->lock);
2234 + cmd = list_first_entry(&instance->cmd_list,
2235 + struct sm_cmd_rsp_blk, head);
2236 + list_move(&cmd->head, &instance->rsp_list);
2238 + mutex_unlock(&instance->lock);
2239 + /* Send the command */
2241 + bcm2835_vchi_msg_queue(instance->service_handle[0],
2242 + cmd->msg, cmd->length);
2244 + pr_err("%s: failed to queue message (%d)",
2245 + __func__, status);
2248 + /* If no reply is needed then we're done */
2250 + mutex_lock(&instance->lock);
2251 + list_del(&cmd->head);
2252 + mutex_unlock(&instance->lock);
2253 + vc_vchi_cmd_delete(instance, cmd);
2258 + complete(&cmd->cmplt);
2264 + while ((header = vchiq_msg_hold(instance->service_handle[0]))) {
2265 + reply = (struct vc_sm_result_t *)header->data;
2266 + if (reply->trans_id & 0x80000000) {
2267 + /* Async event or cmd from the VPU */
2268 + if (instance->vpu_event)
2269 + instance->vpu_event(instance, reply,
2272 + vc_sm_cma_vchi_rx_ack(instance, cmd, reply,
2276 + vchiq_release_message(instance->service_handle[0],
2280 + /* Go through the dead list and free them */
2281 + mutex_lock(&instance->lock);
2282 + list_for_each_entry_safe(cmd, cmd_tmp, &instance->dead_list,
2284 + list_del(&cmd->head);
2285 + vc_vchi_cmd_delete(instance, cmd);
2287 + mutex_unlock(&instance->lock);
2293 +static enum vchiq_status vc_sm_cma_vchi_callback(enum vchiq_reason reason,
2294 + struct vchiq_header *header,
2295 + unsigned int handle, void *userdata)
2297 + struct sm_instance *instance = vchiq_get_service_userdata(handle);
2300 + case VCHIQ_MESSAGE_AVAILABLE:
2301 + vchiq_msg_queue_push(handle, header);
2302 + complete(&instance->io_cmplt);
2305 + case VCHIQ_SERVICE_CLOSED:
2306 + pr_info("%s: service CLOSED!!", __func__);
2311 + return VCHIQ_SUCCESS;
2314 +struct sm_instance *vc_sm_cma_vchi_init(struct vchiq_instance *vchiq_instance,
2315 + unsigned int num_connections,
2316 + vpu_event_cb vpu_event)
2319 + struct sm_instance *instance;
2322 + pr_debug("%s: start", __func__);
2324 + if (num_connections > SM_MAX_NUM_CONNECTIONS) {
2325 + pr_err("%s: unsupported number of connections %u (max=%u)",
2326 + __func__, num_connections, SM_MAX_NUM_CONNECTIONS);
2330 + /* Allocate memory for this instance */
2331 + instance = kzalloc(sizeof(*instance), GFP_KERNEL);
2333 + /* Misc initialisations */
2334 + mutex_init(&instance->lock);
2335 + init_completion(&instance->io_cmplt);
2336 + INIT_LIST_HEAD(&instance->cmd_list);
2337 + INIT_LIST_HEAD(&instance->rsp_list);
2338 + INIT_LIST_HEAD(&instance->dead_list);
2339 + INIT_LIST_HEAD(&instance->free_list);
2340 + sema_init(&instance->free_sema, SM_MAX_NUM_CMD_RSP_BLKS);
2341 + mutex_init(&instance->free_lock);
2342 + for (i = 0; i < SM_MAX_NUM_CMD_RSP_BLKS; i++) {
2343 + init_completion(&instance->free_blk[i].cmplt);
2344 + list_add(&instance->free_blk[i].head, &instance->free_list);
2347 + /* Open the VCHI service connections */
2348 + instance->num_connections = num_connections;
2349 + for (i = 0; i < num_connections; i++) {
2350 + struct vchiq_service_params_kernel params = {
2351 + .version = VC_SM_VER,
2352 + .version_min = VC_SM_MIN_VER,
2353 + .fourcc = VCHIQ_MAKE_FOURCC('S', 'M', 'E', 'M'),
2354 + .callback = vc_sm_cma_vchi_callback,
2355 + .userdata = instance,
2358 + status = vchiq_open_service(vchiq_instance, ¶ms,
2359 + &instance->service_handle[i]);
2361 + pr_err("%s: failed to open VCHI service (%d)",
2362 + __func__, status);
2364 + goto err_close_services;
2367 + /* Create the thread which takes care of all io to/from videoocore. */
2368 + instance->io_thread = kthread_create(&vc_sm_cma_vchi_videocore_io,
2369 + (void *)instance, "SMIO");
2370 + if (!instance->io_thread) {
2371 + pr_err("%s: failed to create SMIO thread", __func__);
2373 + goto err_close_services;
2375 + instance->vpu_event = vpu_event;
2376 + set_user_nice(instance->io_thread, -10);
2377 + wake_up_process(instance->io_thread);
2379 + pr_debug("%s: success - instance %p", __func__, instance);
2382 +err_close_services:
2383 + for (i = 0; i < instance->num_connections; i++) {
2384 + if (instance->service_handle[i])
2385 + vchiq_close_service(instance->service_handle[i]);
2389 + pr_debug("%s: FAILED", __func__);
2393 +int vc_sm_cma_vchi_stop(struct sm_instance **handle)
2395 + struct sm_instance *instance;
2399 + pr_err("%s: invalid pointer to handle %p", __func__, handle);
2404 + pr_err("%s: invalid handle %p", __func__, *handle);
2408 + instance = *handle;
2410 + /* Close all VCHI service connections */
2411 + for (i = 0; i < instance->num_connections; i++) {
2412 + vchiq_use_service(instance->service_handle[i]);
2413 + vchiq_close_service(instance->service_handle[i]);
2425 +static int vc_sm_cma_vchi_send_msg(struct sm_instance *handle,
2426 + enum vc_sm_msg_type msg_id, void *msg,
2427 + u32 msg_size, void *result, u32 result_size,
2428 + u32 *cur_trans_id, u8 wait_reply)
2431 + struct sm_instance *instance = handle;
2432 + struct sm_cmd_rsp_blk *cmd_blk;
2435 + pr_err("%s: invalid handle", __func__);
2439 + pr_err("%s: invalid msg pointer", __func__);
2444 + vc_vchi_cmd_create(instance, msg_id, msg, msg_size, wait_reply);
2446 + pr_err("[%s]: failed to allocate global tracking resource",
2452 + *cur_trans_id = cmd_blk->id;
2454 + mutex_lock(&instance->lock);
2455 + list_add_tail(&cmd_blk->head, &instance->cmd_list);
2456 + mutex_unlock(&instance->lock);
2457 + complete(&instance->io_cmplt);
2463 + /* Wait for the response */
2464 + if (wait_for_completion_interruptible(&cmd_blk->cmplt)) {
2465 + mutex_lock(&instance->lock);
2466 + if (!cmd_blk->sent) {
2467 + list_del(&cmd_blk->head);
2468 + mutex_unlock(&instance->lock);
2469 + vc_vchi_cmd_delete(instance, cmd_blk);
2473 + list_move(&cmd_blk->head, &instance->dead_list);
2474 + mutex_unlock(&instance->lock);
2475 + complete(&instance->io_cmplt);
2476 + return -EINTR; /* We're done */
2479 + if (result && result_size) {
2480 + memcpy(result, cmd_blk->msg, result_size);
2482 + struct vc_sm_result_t *res =
2483 + (struct vc_sm_result_t *)cmd_blk->msg;
2484 + status = (res->success == 0) ? 0 : -ENXIO;
2487 + mutex_lock(&instance->lock);
2488 + list_del(&cmd_blk->head);
2489 + mutex_unlock(&instance->lock);
2490 + vc_vchi_cmd_delete(instance, cmd_blk);
2494 +int vc_sm_cma_vchi_free(struct sm_instance *handle, struct vc_sm_free_t *msg,
2495 + u32 *cur_trans_id)
2497 + return vc_sm_cma_vchi_send_msg(handle, VC_SM_MSG_TYPE_FREE,
2498 + msg, sizeof(*msg), 0, 0, cur_trans_id, 0);
2501 +int vc_sm_cma_vchi_import(struct sm_instance *handle, struct vc_sm_import *msg,
2502 + struct vc_sm_import_result *result, u32 *cur_trans_id)
2504 + return vc_sm_cma_vchi_send_msg(handle, VC_SM_MSG_TYPE_IMPORT,
2505 + msg, sizeof(*msg), result, sizeof(*result),
2509 +int vc_sm_cma_vchi_client_version(struct sm_instance *handle,
2510 + struct vc_sm_version *msg,
2511 + struct vc_sm_result_t *result,
2512 + u32 *cur_trans_id)
2514 + return vc_sm_cma_vchi_send_msg(handle, VC_SM_MSG_TYPE_CLIENT_VERSION,
2515 + //msg, sizeof(*msg), result, sizeof(*result),
2516 + //cur_trans_id, 1);
2517 + msg, sizeof(*msg), NULL, 0,
2521 +int vc_sm_vchi_client_vc_mem_req_reply(struct sm_instance *handle,
2522 + struct vc_sm_vc_mem_request_result *msg,
2523 + uint32_t *cur_trans_id)
2525 + return vc_sm_cma_vchi_send_msg(handle,
2526 + VC_SM_MSG_TYPE_VC_MEM_REQUEST_REPLY,
2527 + msg, sizeof(*msg), 0, 0, cur_trans_id,
2531 +++ b/drivers/staging/vc04_services/vc-sm-cma/vc_sm_cma_vchi.h
2533 +/* SPDX-License-Identifier: GPL-2.0 */
2536 + * VideoCore Shared Memory CMA allocator
2538 + * Copyright: 2018, Raspberry Pi (Trading) Ltd
2539 + * Copyright 2011-2012 Broadcom Corporation. All rights reserved.
2541 + * Based on vmcs_sm driver from Broadcom Corporation.
2545 +#ifndef __VC_SM_CMA_VCHI_H__INCLUDED__
2546 +#define __VC_SM_CMA_VCHI_H__INCLUDED__
2548 +#include <linux/raspberrypi/vchiq.h>
2550 +#include "vc_sm_defs.h"
2553 + * Forward declare.
2555 +struct sm_instance;
2557 +typedef void (*vpu_event_cb)(struct sm_instance *instance,
2558 + struct vc_sm_result_t *reply, int reply_len);
2561 + * Initialize the shared memory service, opens up vchi connection to talk to it.
2563 +struct sm_instance *vc_sm_cma_vchi_init(struct vchiq_instance *vchi_instance,
2564 + unsigned int num_connections,
2565 + vpu_event_cb vpu_event);
2568 + * Terminates the shared memory service.
2570 +int vc_sm_cma_vchi_stop(struct sm_instance **handle);
2573 + * Ask the shared memory service to free up some memory that was previously
2574 + * allocated by the vc_sm_cma_vchi_alloc function call.
2576 +int vc_sm_cma_vchi_free(struct sm_instance *handle, struct vc_sm_free_t *msg,
2577 + u32 *cur_trans_id);
2580 + * Import a contiguous block of memory and wrap it in a GPU MEM_HANDLE_T.
2582 +int vc_sm_cma_vchi_import(struct sm_instance *handle, struct vc_sm_import *msg,
2583 + struct vc_sm_import_result *result,
2584 + u32 *cur_trans_id);
2586 +int vc_sm_cma_vchi_client_version(struct sm_instance *handle,
2587 + struct vc_sm_version *msg,
2588 + struct vc_sm_result_t *result,
2589 + u32 *cur_trans_id);
2591 +int vc_sm_vchi_client_vc_mem_req_reply(struct sm_instance *handle,
2592 + struct vc_sm_vc_mem_request_result *msg,
2593 + uint32_t *cur_trans_id);
2595 +#endif /* __VC_SM_CMA_VCHI_H__INCLUDED__ */
2597 +++ b/drivers/staging/vc04_services/vc-sm-cma/vc_sm_defs.h
2599 +/* SPDX-License-Identifier: GPL-2.0 */
2602 + * VideoCore Shared Memory CMA allocator
2604 + * Copyright: 2018, Raspberry Pi (Trading) Ltd
2606 + * Based on vc_sm_defs.h from the vmcs_sm driver Copyright Broadcom Corporation.
2607 + * All IPC messages are copied across to this file, even if the vc-sm-cma
2608 + * driver is not currently using them.
2610 + ****************************************************************************
2613 +#ifndef __VC_SM_DEFS_H__INCLUDED__
2614 +#define __VC_SM_DEFS_H__INCLUDED__
2616 +/* Maximum message length */
2617 +#define VC_SM_MAX_MSG_LEN (sizeof(union vc_sm_msg_union_t) + \
2618 + sizeof(struct vc_sm_msg_hdr_t))
2619 +#define VC_SM_MAX_RSP_LEN (sizeof(union vc_sm_msg_union_t))
2621 +/* Resource name maximum size */
2622 +#define VC_SM_RESOURCE_NAME 32
2625 + * Version to be reported to the VPU
2626 + * VPU assumes 0 (aka 1) which does not require the released callback, nor
2627 + * expect the client to handle VC_MEM_REQUESTS.
2628 + * Version 2 requires the released callback, and must support VC_MEM_REQUESTS.
2630 +#define VC_SM_PROTOCOL_VERSION 2
2632 +enum vc_sm_msg_type {
2633 + /* Message types supported for HOST->VC direction */
2635 + /* Allocate shared memory block */
2636 + VC_SM_MSG_TYPE_ALLOC,
2637 + /* Lock allocated shared memory block */
2638 + VC_SM_MSG_TYPE_LOCK,
2639 + /* Unlock allocated shared memory block */
2640 + VC_SM_MSG_TYPE_UNLOCK,
2641 + /* Unlock allocated shared memory block, do not answer command */
2642 + VC_SM_MSG_TYPE_UNLOCK_NOANS,
2643 + /* Free shared memory block */
2644 + VC_SM_MSG_TYPE_FREE,
2645 + /* Resize a shared memory block */
2646 + VC_SM_MSG_TYPE_RESIZE,
2647 + /* Walk the allocated shared memory block(s) */
2648 + VC_SM_MSG_TYPE_WALK_ALLOC,
2650 + /* A previously applied action will need to be reverted */
2651 + VC_SM_MSG_TYPE_ACTION_CLEAN,
2654 + * Import a physical address and wrap into a MEM_HANDLE_T.
2655 + * Release with VC_SM_MSG_TYPE_FREE.
2657 + VC_SM_MSG_TYPE_IMPORT,
2659 + *Tells VC the protocol version supported by this client.
2660 + * 2 supports the async/cmd messages from the VPU for final release
2661 + * of memory, and for VC allocations.
2663 + VC_SM_MSG_TYPE_CLIENT_VERSION,
2664 + /* Response to VC request for memory */
2665 + VC_SM_MSG_TYPE_VC_MEM_REQUEST_REPLY,
2668 + * Asynchronous/cmd messages supported for VC->HOST direction.
2669 + * Signalled by setting the top bit in vc_sm_result_t trans_id.
2673 + * VC has finished with an imported memory allocation.
2674 + * Release any Linux reference counts on the underlying block.
2676 + VC_SM_MSG_TYPE_RELEASED,
2677 + /* VC request for memory */
2678 + VC_SM_MSG_TYPE_VC_MEM_REQUEST,
2680 + VC_SM_MSG_TYPE_MAX
2683 +/* Type of memory to be allocated */
2684 +enum vc_sm_alloc_type_t {
2685 + VC_SM_ALLOC_CACHED,
2686 + VC_SM_ALLOC_NON_CACHED,
2689 +/* Message header for all messages in HOST->VC direction */
2690 +struct vc_sm_msg_hdr_t {
2697 +/* Request to allocate memory (HOST->VC) */
2698 +struct vc_sm_alloc_t {
2699 + /* type of memory to allocate */
2700 + enum vc_sm_alloc_type_t type;
2701 + /* byte amount of data to allocate per unit */
2703 + /* number of unit to allocate */
2705 + /* alignment to be applied on allocation */
2707 + /* identity of who allocated this block */
2709 + /* resource name (for easier tracking on vc side) */
2710 + char name[VC_SM_RESOURCE_NAME];
2714 +/* Result of a requested memory allocation (VC->HOST) */
2715 +struct vc_sm_alloc_result_t {
2716 + /* Transaction identifier */
2719 + /* Resource handle */
2721 + /* Pointer to resource buffer */
2723 + /* Resource base size (bytes) */
2724 + u32 res_base_size;
2725 + /* Resource number */
2730 +/* Request to free a previously allocated memory (HOST->VC) */
2731 +struct vc_sm_free_t {
2732 + /* Resource handle (returned from alloc) */
2734 + /* Resource buffer (returned from alloc) */
2739 +/* Request to lock a previously allocated memory (HOST->VC) */
2740 +struct vc_sm_lock_unlock_t {
2741 + /* Resource handle (returned from alloc) */
2743 + /* Resource buffer (returned from alloc) */
2748 +/* Request to resize a previously allocated memory (HOST->VC) */
2749 +struct vc_sm_resize_t {
2750 + /* Resource handle (returned from alloc) */
2752 + /* Resource buffer (returned from alloc) */
2754 + /* Resource *new* size requested (bytes) */
2759 +/* Result of a requested memory lock (VC->HOST) */
2760 +struct vc_sm_lock_result_t {
2761 + /* Transaction identifier */
2764 + /* Resource handle */
2766 + /* Pointer to resource buffer */
2769 + * Pointer to former resource buffer if the memory
2776 +/* Generic result for a request (VC->HOST) */
2777 +struct vc_sm_result_t {
2778 + /* Transaction identifier */
2785 +/* Request to revert a previously applied action (HOST->VC) */
2786 +struct vc_sm_action_clean_t {
2787 + /* Action of interest */
2788 + enum vc_sm_msg_type res_action;
2789 + /* Transaction identifier for the action of interest */
2790 + u32 action_trans_id;
2794 +/* Request to remove all data associated with a given allocator (HOST->VC) */
2795 +struct vc_sm_free_all_t {
2796 + /* Allocator identifier */
2800 +/* Request to import memory (HOST->VC) */
2801 +struct vc_sm_import {
2802 + /* type of memory to allocate */
2803 + enum vc_sm_alloc_type_t type;
2804 + /* pointer to the VC (ie physical) address of the allocated memory */
2806 + /* size of buffer */
2808 + /* opaque handle returned in RELEASED messages */
2810 + /* Allocator identifier */
2812 + /* resource name (for easier tracking on vc side) */
2813 + char name[VC_SM_RESOURCE_NAME];
2816 +/* Result of a requested memory import (VC->HOST) */
2817 +struct vc_sm_import_result {
2818 + /* Transaction identifier */
2821 + /* Resource handle */
2825 +/* Notification that VC has finished with an allocation (VC->HOST) */
2826 +struct vc_sm_released {
2827 + /* cmd type / trans_id */
2830 + /* pointer to the VC (ie physical) address of the allocated memory */
2832 + /* size of buffer */
2834 + /* opaque handle returned in RELEASED messages */
2840 + * Client informing VC as to the protocol version it supports.
2841 + * >=2 requires the released callback, and supports VC asking for memory.
2842 + * Failure means that the firmware doesn't support this call, and therefore the
2843 + * client should either fail, or NOT rely on getting the released callback.
2845 +struct vc_sm_version {
2849 +/* Request FROM VideoCore for some memory */
2850 +struct vc_sm_vc_mem_request {
2854 + /* trans_id (from VPU) */
2856 + /* size of buffer */
2858 + /* alignment of buffer */
2860 + /* resource name (for easier tracking) */
2861 + char name[VC_SM_RESOURCE_NAME];
2862 + /* VPU handle for the resource */
2866 +/* Response from the kernel to provide the VPU with some memory */
2867 +struct vc_sm_vc_mem_request_result {
2868 + /* Transaction identifier for the VPU */
2870 + /* pointer to the physical address of the allocated memory */
2872 + /* opaque handle returned in RELEASED messages */
2876 +/* Union of ALL messages */
2877 +union vc_sm_msg_union_t {
2878 + struct vc_sm_alloc_t alloc;
2879 + struct vc_sm_alloc_result_t alloc_result;
2880 + struct vc_sm_free_t free;
2881 + struct vc_sm_lock_unlock_t lock_unlock;
2882 + struct vc_sm_action_clean_t action_clean;
2883 + struct vc_sm_resize_t resize;
2884 + struct vc_sm_lock_result_t lock_result;
2885 + struct vc_sm_result_t result;
2886 + struct vc_sm_free_all_t free_all;
2887 + struct vc_sm_import import;
2888 + struct vc_sm_import_result import_result;
2889 + struct vc_sm_version version;
2890 + struct vc_sm_released released;
2891 + struct vc_sm_vc_mem_request vc_request;
2892 + struct vc_sm_vc_mem_request_result vc_request_result;
2895 +#endif /* __VC_SM_DEFS_H__INCLUDED__ */
2897 +++ b/drivers/staging/vc04_services/vc-sm-cma/vc_sm_knl.h
2899 +/* SPDX-License-Identifier: GPL-2.0 */
2902 + * VideoCore Shared Memory CMA allocator
2904 + * Copyright: 2018, Raspberry Pi (Trading) Ltd
2906 + * Based on vc_sm_defs.h from the vmcs_sm driver Copyright Broadcom Corporation.
2910 +#ifndef __VC_SM_KNL_H__INCLUDED__
2911 +#define __VC_SM_KNL_H__INCLUDED__
2913 +#if !defined(__KERNEL__)
2914 +#error "This interface is for kernel use only..."
2917 +/* Free a previously allocated or imported shared memory handle and block. */
2918 +int vc_sm_cma_free(void *handle);
2920 +/* Get an internal resource handle mapped from the external one. */
2921 +int vc_sm_cma_int_handle(void *handle);
2923 +/* Import a block of memory into the GPU space. */
2924 +int vc_sm_cma_import_dmabuf(struct dma_buf *dmabuf, void **handle);
2926 +#endif /* __VC_SM_KNL_H__INCLUDED__ */