bcm27xx: update 6.1 patches from RPi foundation
[openwrt/staging/xback.git] / target / linux / bcm27xx / patches-6.1 / 950-0273-staging-vc04_services-Add-new-vc-sm-cma-driver.patch
1 From 152e25fc6283a130332df7908cc45d7fa3bfae19 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
5
6 Add Broadcom VideoCore Shared Memory support.
7
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.
12
13 Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.com>
14
15 staging: vcsm-cma: Fix memory leak from not detaching dmabuf
16
17 When importing there was a missing call to detach the buffer,
18 so each import leaked the sg table entry.
19
20 Actually the release process for both locally allocated and
21 imported buffers is identical, so fix them to both use the same
22 function.
23
24 Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.com>
25
26 staging/vc-sm-cma: Avoid log spamming on Pi0/1 over cache alias.
27
28 Pi 0/1 use the 0x80000000 cache alias as the ARM also sees the world
29 through the VPU L2 cache.
30 vc-sm-cma was trying to ensure it was in an uncached alias (0xc), and
31 complaining on every allocation if it weren't. Reduce this logging.
32
33 Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.com>
34
35 vc-sm-cma: Restore correct cache maintainance operations
36
37 We have been using the more expensive flush operations rather than
38 invalidate and clean since kernel rpi-5.9.y
39
40 These are exposed with:
41 52f1453513ba95084ab811a030032fe605b0cbe2 Re-expose some dmi APIs for use in VCSM
42
43 But I believe that commit was dropped when (non-cma) vc-sm was dropped,
44 and didn't get updated when the commit was restored
45
46 Signed-off-by: Dom Cobley <popcornmix@gmail.com>
47
48 staging: vc04_services: Fix clang14 warning
49
50 Insert a break to fix a fallthrough warning from clang14. Since the
51 fallthrough was to another break, this is a cosmetic change.
52
53 See: https://github.com/raspberrypi/linux/issues/5078
54
55 Signed-off-by: Phil Elwell <phil@raspberrypi.com>
56 ---
57 drivers/staging/vc04_services/Kconfig | 2 +
58 drivers/staging/vc04_services/Makefile | 1 +
59 .../include/linux/broadcom/vc_sm_cma_ioctl.h | 114 ++
60 .../staging/vc04_services/vc-sm-cma/Kconfig | 10 +
61 .../staging/vc04_services/vc-sm-cma/Makefile | 12 +
62 drivers/staging/vc04_services/vc-sm-cma/TODO | 1 +
63 .../staging/vc04_services/vc-sm-cma/vc_sm.c | 1716 +++++++++++++++++
64 .../staging/vc04_services/vc-sm-cma/vc_sm.h | 84 +
65 .../vc04_services/vc-sm-cma/vc_sm_cma_vchi.c | 505 +++++
66 .../vc04_services/vc-sm-cma/vc_sm_cma_vchi.h | 63 +
67 .../vc04_services/vc-sm-cma/vc_sm_defs.h | 297 +++
68 .../vc04_services/vc-sm-cma/vc_sm_knl.h | 28 +
69 12 files changed, 2833 insertions(+)
70 create mode 100644 drivers/staging/vc04_services/include/linux/broadcom/vc_sm_cma_ioctl.h
71 create mode 100644 drivers/staging/vc04_services/vc-sm-cma/Kconfig
72 create mode 100644 drivers/staging/vc04_services/vc-sm-cma/Makefile
73 create mode 100644 drivers/staging/vc04_services/vc-sm-cma/TODO
74 create mode 100644 drivers/staging/vc04_services/vc-sm-cma/vc_sm.c
75 create mode 100644 drivers/staging/vc04_services/vc-sm-cma/vc_sm.h
76 create mode 100644 drivers/staging/vc04_services/vc-sm-cma/vc_sm_cma_vchi.c
77 create mode 100644 drivers/staging/vc04_services/vc-sm-cma/vc_sm_cma_vchi.h
78 create mode 100644 drivers/staging/vc04_services/vc-sm-cma/vc_sm_defs.h
79 create mode 100644 drivers/staging/vc04_services/vc-sm-cma/vc_sm_knl.h
80
81 --- a/drivers/staging/vc04_services/Kconfig
82 +++ b/drivers/staging/vc04_services/Kconfig
83 @@ -44,6 +44,8 @@ source "drivers/staging/vc04_services/bc
84
85 source "drivers/staging/vc04_services/bcm2835-camera/Kconfig"
86
87 +source "drivers/staging/vc04_services/vc-sm-cma/Kconfig"
88 +
89 source "drivers/staging/vc04_services/vchiq-mmal/Kconfig"
90
91 endif
92 --- a/drivers/staging/vc04_services/Makefile
93 +++ b/drivers/staging/vc04_services/Makefile
94 @@ -14,6 +14,7 @@ endif
95 obj-$(CONFIG_SND_BCM2835) += bcm2835-audio/
96 obj-$(CONFIG_VIDEO_BCM2835) += bcm2835-camera/
97 obj-$(CONFIG_BCM2835_VCHIQ_MMAL) += vchiq-mmal/
98 +obj-$(CONFIG_BCM_VC_SM_CMA) += vc-sm-cma/
99
100 ccflags-y += -I $(srctree)/$(src)/include
101
102 --- /dev/null
103 +++ b/drivers/staging/vc04_services/include/linux/broadcom/vc_sm_cma_ioctl.h
104 @@ -0,0 +1,114 @@
105 +/* SPDX-License-Identifier: GPL-2.0 */
106 +
107 +/*
108 + * Copyright 2019 Raspberry Pi (Trading) Ltd. All rights reserved.
109 + *
110 + * Based on vmcs_sm_ioctl.h Copyright Broadcom Corporation.
111 + */
112 +
113 +#ifndef __VC_SM_CMA_IOCTL_H
114 +#define __VC_SM_CMA_IOCTL_H
115 +
116 +/* ---- Include Files ---------------------------------------------------- */
117 +
118 +#if defined(__KERNEL__)
119 +#include <linux/types.h> /* Needed for standard types */
120 +#else
121 +#include <stdint.h>
122 +#endif
123 +
124 +#include <linux/ioctl.h>
125 +
126 +/* ---- Constants and Types ---------------------------------------------- */
127 +
128 +#define VC_SM_CMA_RESOURCE_NAME 32
129 +#define VC_SM_CMA_RESOURCE_NAME_DEFAULT "sm-host-resource"
130 +
131 +/* Type define used to create unique IOCTL number */
132 +#define VC_SM_CMA_MAGIC_TYPE 'J'
133 +
134 +/* IOCTL commands on /dev/vc-sm-cma */
135 +enum vc_sm_cma_cmd_e {
136 + VC_SM_CMA_CMD_ALLOC = 0x5A, /* Start at 0x5A arbitrarily */
137 +
138 + VC_SM_CMA_CMD_IMPORT_DMABUF,
139 +
140 + VC_SM_CMA_CMD_CLEAN_INVALID2,
141 +
142 + VC_SM_CMA_CMD_LAST /* Do not delete */
143 +};
144 +
145 +/* Cache type supported, conveniently matches the user space definition in
146 + * user-vcsm.h.
147 + */
148 +enum vc_sm_cma_cache_e {
149 + VC_SM_CMA_CACHE_NONE,
150 + VC_SM_CMA_CACHE_HOST,
151 + VC_SM_CMA_CACHE_VC,
152 + VC_SM_CMA_CACHE_BOTH,
153 +};
154 +
155 +/* IOCTL Data structures */
156 +struct vc_sm_cma_ioctl_alloc {
157 + /* user -> kernel */
158 + __u32 size;
159 + __u32 num;
160 + __u32 cached; /* enum vc_sm_cma_cache_e */
161 + __u32 pad;
162 + __u8 name[VC_SM_CMA_RESOURCE_NAME];
163 +
164 + /* kernel -> user */
165 + __s32 handle;
166 + __u32 vc_handle;
167 + __u64 dma_addr;
168 +};
169 +
170 +struct vc_sm_cma_ioctl_import_dmabuf {
171 + /* user -> kernel */
172 + __s32 dmabuf_fd;
173 + __u32 cached; /* enum vc_sm_cma_cache_e */
174 + __u8 name[VC_SM_CMA_RESOURCE_NAME];
175 +
176 + /* kernel -> user */
177 + __s32 handle;
178 + __u32 vc_handle;
179 + __u32 size;
180 + __u32 pad;
181 + __u64 dma_addr;
182 +};
183 +
184 +/*
185 + * Cache functions to be set to struct vc_sm_cma_ioctl_clean_invalid2
186 + * invalidate_mode.
187 + */
188 +#define VC_SM_CACHE_OP_NOP 0x00
189 +#define VC_SM_CACHE_OP_INV 0x01
190 +#define VC_SM_CACHE_OP_CLEAN 0x02
191 +#define VC_SM_CACHE_OP_FLUSH 0x03
192 +
193 +struct vc_sm_cma_ioctl_clean_invalid2 {
194 + __u32 op_count;
195 + __u32 pad;
196 + struct vc_sm_cma_ioctl_clean_invalid_block {
197 + __u32 invalidate_mode;
198 + __u32 block_count;
199 + void * __user start_address;
200 + __u32 block_size;
201 + __u32 inter_block_stride;
202 + } s[0];
203 +};
204 +
205 +/* IOCTL numbers */
206 +#define VC_SM_CMA_IOCTL_MEM_ALLOC\
207 + _IOR(VC_SM_CMA_MAGIC_TYPE, VC_SM_CMA_CMD_ALLOC,\
208 + struct vc_sm_cma_ioctl_alloc)
209 +
210 +#define VC_SM_CMA_IOCTL_MEM_IMPORT_DMABUF\
211 + _IOR(VC_SM_CMA_MAGIC_TYPE, VC_SM_CMA_CMD_IMPORT_DMABUF,\
212 + struct vc_sm_cma_ioctl_import_dmabuf)
213 +
214 +#define VC_SM_CMA_IOCTL_MEM_CLEAN_INVALID2\
215 + _IOR(VC_SM_CMA_MAGIC_TYPE, VC_SM_CMA_CMD_CLEAN_INVALID2,\
216 + struct vc_sm_cma_ioctl_clean_invalid2)
217 +
218 +#endif /* __VC_SM_CMA_IOCTL_H */
219 --- /dev/null
220 +++ b/drivers/staging/vc04_services/vc-sm-cma/Kconfig
221 @@ -0,0 +1,10 @@
222 +config BCM_VC_SM_CMA
223 + tristate "VideoCore Shared Memory (CMA) driver"
224 + select BCM2835_VCHIQ
225 + select RBTREE
226 + select DMA_SHARED_BUFFER
227 + help
228 + Say Y here to enable the shared memory interface that
229 + supports sharing dmabufs with VideoCore.
230 + This operates over the VCHIQ interface to a service
231 + running on VideoCore.
232 --- /dev/null
233 +++ b/drivers/staging/vc04_services/vc-sm-cma/Makefile
234 @@ -0,0 +1,12 @@
235 +ccflags-y += \
236 + -I$(srctree)/$(src)/../ \
237 + -I$(srctree)/$(src)/../interface/vchiq_arm\
238 + -I$(srctree)/$(src)/../include
239 +
240 +ccflags-y += \
241 + -D__VCCOREVER__=0
242 +
243 +vc-sm-cma-$(CONFIG_BCM_VC_SM_CMA) := \
244 + vc_sm.o vc_sm_cma_vchi.o
245 +
246 +obj-$(CONFIG_BCM_VC_SM_CMA) += vc-sm-cma.o
247 --- /dev/null
248 +++ b/drivers/staging/vc04_services/vc-sm-cma/TODO
249 @@ -0,0 +1 @@
250 +No currently outstanding tasks except some clean-up.
251 --- /dev/null
252 +++ b/drivers/staging/vc04_services/vc-sm-cma/vc_sm.c
253 @@ -0,0 +1,1716 @@
254 +// SPDX-License-Identifier: GPL-2.0
255 +/*
256 + * VideoCore Shared Memory driver using CMA.
257 + *
258 + * Copyright: 2018, Raspberry Pi (Trading) Ltd
259 + * Dave Stevenson <dave.stevenson@raspberrypi.org>
260 + *
261 + * Based on vmcs_sm driver from Broadcom Corporation for some API,
262 + * and taking some code for buffer allocation and dmabuf handling from
263 + * videobuf2.
264 + *
265 + *
266 + * This driver has 3 main uses:
267 + * 1) Allocating buffers for the kernel or userspace that can be shared with the
268 + * VPU.
269 + * 2) Importing dmabufs from elsewhere for sharing with the VPU.
270 + * 3) Allocating buffers for use by the VPU.
271 + *
272 + * In the first and second cases the native handle is a dmabuf. Releasing the
273 + * resource inherently comes from releasing the dmabuf, and this will trigger
274 + * unmapping on the VPU. The underlying allocation and our buffer structure are
275 + * retained until the VPU has confirmed that it has finished with it.
276 + *
277 + * For the VPU allocations the VPU is responsible for triggering the release,
278 + * and therefore the released message decrements the dma_buf refcount (with the
279 + * VPU mapping having already been marked as released).
280 + */
281 +
282 +/* ---- Include Files ----------------------------------------------------- */
283 +#include <linux/cdev.h>
284 +#include <linux/device.h>
285 +#include <linux/debugfs.h>
286 +#include <linux/dma-mapping.h>
287 +#include <linux/dma-buf.h>
288 +#include <linux/errno.h>
289 +#include <linux/fs.h>
290 +#include <linux/kernel.h>
291 +#include <linux/list.h>
292 +#include <linux/miscdevice.h>
293 +#include <linux/module.h>
294 +#include <linux/mm.h>
295 +#include <linux/of_device.h>
296 +#include <linux/platform_device.h>
297 +#include <linux/proc_fs.h>
298 +#include <linux/slab.h>
299 +#include <linux/seq_file.h>
300 +#include <linux/syscalls.h>
301 +#include <linux/types.h>
302 +#include <asm/cacheflush.h>
303 +
304 +#include "vchiq_connected.h"
305 +#include "vc_sm_cma_vchi.h"
306 +
307 +#include "vc_sm.h"
308 +#include "vc_sm_knl.h"
309 +#include <linux/broadcom/vc_sm_cma_ioctl.h>
310 +
311 +MODULE_IMPORT_NS(DMA_BUF);
312 +
313 +/* ---- Private Constants and Types --------------------------------------- */
314 +
315 +#define DEVICE_NAME "vcsm-cma"
316 +#define DEVICE_MINOR 0
317 +
318 +#define VC_SM_RESOURCE_NAME_DEFAULT "sm-host-resource"
319 +
320 +#define VC_SM_DIR_ROOT_NAME "vcsm-cma"
321 +#define VC_SM_STATE "state"
322 +
323 +/* Private file data associated with each opened device. */
324 +struct vc_sm_privdata_t {
325 + pid_t pid; /* PID of creator. */
326 +
327 + int restart_sys; /* Tracks restart on interrupt. */
328 + enum vc_sm_msg_type int_action; /* Interrupted action. */
329 + u32 int_trans_id; /* Interrupted transaction. */
330 +};
331 +
332 +typedef int (*VC_SM_SHOW) (struct seq_file *s, void *v);
333 +struct sm_pde_t {
334 + VC_SM_SHOW show; /* Debug fs function hookup. */
335 + struct dentry *dir_entry; /* Debug fs directory entry. */
336 + void *priv_data; /* Private data */
337 +};
338 +
339 +/* Global state information. */
340 +struct sm_state_t {
341 + struct platform_device *pdev;
342 +
343 + struct miscdevice misc_dev;
344 +
345 + struct sm_instance *sm_handle; /* Handle for videocore service. */
346 +
347 + spinlock_t kernelid_map_lock; /* Spinlock protecting kernelid_map */
348 + struct idr kernelid_map;
349 +
350 + struct mutex map_lock; /* Global map lock. */
351 + struct list_head buffer_list; /* List of buffer. */
352 +
353 + struct vc_sm_privdata_t *data_knl; /* Kernel internal data tracking. */
354 + struct vc_sm_privdata_t *vpu_allocs; /* All allocations from the VPU */
355 + struct dentry *dir_root; /* Debug fs entries root. */
356 + struct sm_pde_t dir_state; /* Debug fs entries state sub-tree. */
357 +
358 + bool require_released_callback; /* VPU will send a released msg when it
359 + * has finished with a resource.
360 + */
361 + u32 int_trans_id; /* Interrupted transaction. */
362 +};
363 +
364 +struct vc_sm_dma_buf_attachment {
365 + struct device *dev;
366 + struct sg_table sg_table;
367 + struct list_head list;
368 + enum dma_data_direction dma_dir;
369 +};
370 +
371 +/* ---- Private Variables ----------------------------------------------- */
372 +
373 +static struct sm_state_t *sm_state;
374 +static int sm_inited;
375 +
376 +/* ---- Private Function Prototypes -------------------------------------- */
377 +
378 +/* ---- Private Functions ------------------------------------------------ */
379 +
380 +static int get_kernel_id(struct vc_sm_buffer *buffer)
381 +{
382 + int handle;
383 +
384 + spin_lock(&sm_state->kernelid_map_lock);
385 + handle = idr_alloc(&sm_state->kernelid_map, buffer, 0, 0, GFP_KERNEL);
386 + spin_unlock(&sm_state->kernelid_map_lock);
387 +
388 + return handle;
389 +}
390 +
391 +static struct vc_sm_buffer *lookup_kernel_id(int handle)
392 +{
393 + return idr_find(&sm_state->kernelid_map, handle);
394 +}
395 +
396 +static void free_kernel_id(int handle)
397 +{
398 + spin_lock(&sm_state->kernelid_map_lock);
399 + idr_remove(&sm_state->kernelid_map, handle);
400 + spin_unlock(&sm_state->kernelid_map_lock);
401 +}
402 +
403 +static int vc_sm_cma_seq_file_show(struct seq_file *s, void *v)
404 +{
405 + struct sm_pde_t *sm_pde;
406 +
407 + sm_pde = (struct sm_pde_t *)(s->private);
408 +
409 + if (sm_pde && sm_pde->show)
410 + sm_pde->show(s, v);
411 +
412 + return 0;
413 +}
414 +
415 +static int vc_sm_cma_single_open(struct inode *inode, struct file *file)
416 +{
417 + return single_open(file, vc_sm_cma_seq_file_show, inode->i_private);
418 +}
419 +
420 +static const struct file_operations vc_sm_cma_debug_fs_fops = {
421 + .open = vc_sm_cma_single_open,
422 + .read = seq_read,
423 + .llseek = seq_lseek,
424 + .release = single_release,
425 +};
426 +
427 +static int vc_sm_cma_global_state_show(struct seq_file *s, void *v)
428 +{
429 + struct vc_sm_buffer *resource = NULL;
430 + int resource_count = 0;
431 +
432 + if (!sm_state)
433 + return 0;
434 +
435 + seq_printf(s, "\nVC-ServiceHandle %p\n", sm_state->sm_handle);
436 +
437 + /* Log all applicable mapping(s). */
438 +
439 + mutex_lock(&sm_state->map_lock);
440 + seq_puts(s, "\nResources\n");
441 + if (!list_empty(&sm_state->buffer_list)) {
442 + list_for_each_entry(resource, &sm_state->buffer_list,
443 + global_buffer_list) {
444 + resource_count++;
445 +
446 + seq_printf(s, "\nResource %p\n",
447 + resource);
448 + seq_printf(s, " NAME %s\n",
449 + resource->name);
450 + seq_printf(s, " SIZE %zu\n",
451 + resource->size);
452 + seq_printf(s, " DMABUF %p\n",
453 + resource->dma_buf);
454 + if (resource->imported) {
455 + seq_printf(s, " ATTACH %p\n",
456 + resource->import.attach);
457 + seq_printf(s, " SGT %p\n",
458 + resource->import.sgt);
459 + } else {
460 + seq_printf(s, " SGT %p\n",
461 + resource->alloc.sg_table);
462 + }
463 + seq_printf(s, " DMA_ADDR %pad\n",
464 + &resource->dma_addr);
465 + seq_printf(s, " VC_HANDLE %08x\n",
466 + resource->vc_handle);
467 + seq_printf(s, " VC_MAPPING %d\n",
468 + resource->vpu_state);
469 + }
470 + }
471 + seq_printf(s, "\n\nTotal resource count: %d\n\n", resource_count);
472 +
473 + mutex_unlock(&sm_state->map_lock);
474 +
475 + return 0;
476 +}
477 +
478 +/*
479 + * Adds a buffer to the private data list which tracks all the allocated
480 + * data.
481 + */
482 +static void vc_sm_add_resource(struct vc_sm_privdata_t *privdata,
483 + struct vc_sm_buffer *buffer)
484 +{
485 + mutex_lock(&sm_state->map_lock);
486 + list_add(&buffer->global_buffer_list, &sm_state->buffer_list);
487 + mutex_unlock(&sm_state->map_lock);
488 +
489 + pr_debug("[%s]: added buffer %p (name %s, size %zu)\n",
490 + __func__, buffer, buffer->name, buffer->size);
491 +}
492 +
493 +/*
494 + * Cleans up imported dmabuf.
495 + * Should be called with mutex held.
496 + */
497 +static void vc_sm_clean_up_dmabuf(struct vc_sm_buffer *buffer)
498 +{
499 + if (!buffer->imported)
500 + return;
501 +
502 + /* Handle cleaning up imported dmabufs */
503 + if (buffer->import.sgt) {
504 + dma_buf_unmap_attachment(buffer->import.attach,
505 + buffer->import.sgt,
506 + DMA_BIDIRECTIONAL);
507 + buffer->import.sgt = NULL;
508 + }
509 + if (buffer->import.attach) {
510 + dma_buf_detach(buffer->dma_buf, buffer->import.attach);
511 + buffer->import.attach = NULL;
512 + }
513 +}
514 +
515 +/*
516 + * Instructs VPU to decrement the refcount on a buffer.
517 + */
518 +static void vc_sm_vpu_free(struct vc_sm_buffer *buffer)
519 +{
520 + if (buffer->vc_handle && buffer->vpu_state == VPU_MAPPED) {
521 + struct vc_sm_free_t free = { buffer->vc_handle, 0 };
522 + int status = vc_sm_cma_vchi_free(sm_state->sm_handle, &free,
523 + &sm_state->int_trans_id);
524 + if (status != 0 && status != -EINTR) {
525 + pr_err("[%s]: failed to free memory on videocore (status: %u, trans_id: %u)\n",
526 + __func__, status, sm_state->int_trans_id);
527 + }
528 +
529 + if (sm_state->require_released_callback) {
530 + /* Need to wait for the VPU to confirm the free. */
531 +
532 + /* Retain a reference on this until the VPU has
533 + * released it
534 + */
535 + buffer->vpu_state = VPU_UNMAPPING;
536 + } else {
537 + buffer->vpu_state = VPU_NOT_MAPPED;
538 + buffer->vc_handle = 0;
539 + }
540 + }
541 +}
542 +
543 +/*
544 + * Release an allocation.
545 + * All refcounting is done via the dma buf object.
546 + *
547 + * Must be called with the mutex held. The function will either release the
548 + * mutex (if defering the release) or destroy it. The caller must therefore not
549 + * reuse the buffer on return.
550 + */
551 +static void vc_sm_release_resource(struct vc_sm_buffer *buffer)
552 +{
553 + pr_debug("[%s]: buffer %p (name %s, size %zu), imported %u\n",
554 + __func__, buffer, buffer->name, buffer->size,
555 + buffer->imported);
556 +
557 + if (buffer->vc_handle) {
558 + /* We've sent the unmap request but not had the response. */
559 + pr_debug("[%s]: Waiting for VPU unmap response on %p\n",
560 + __func__, buffer);
561 + goto defer;
562 + }
563 + if (buffer->in_use) {
564 + /* dmabuf still in use - we await the release */
565 + pr_debug("[%s]: buffer %p is still in use\n", __func__, buffer);
566 + goto defer;
567 + }
568 +
569 + /* Release the allocation (whether imported dmabuf or CMA allocation) */
570 + if (buffer->imported) {
571 + if (buffer->import.dma_buf)
572 + dma_buf_put(buffer->import.dma_buf);
573 + else
574 + pr_err("%s: Imported dmabuf already been put for buf %p\n",
575 + __func__, buffer);
576 + buffer->import.dma_buf = NULL;
577 + } else {
578 + dma_free_coherent(&sm_state->pdev->dev, buffer->size,
579 + buffer->cookie, buffer->dma_addr);
580 + }
581 +
582 + /* Free our buffer. Start by removing it from the list */
583 + mutex_lock(&sm_state->map_lock);
584 + list_del(&buffer->global_buffer_list);
585 + mutex_unlock(&sm_state->map_lock);
586 +
587 + pr_debug("%s: Release our allocation - done\n", __func__);
588 + mutex_unlock(&buffer->lock);
589 +
590 + mutex_destroy(&buffer->lock);
591 +
592 + kfree(buffer);
593 + return;
594 +
595 +defer:
596 + mutex_unlock(&buffer->lock);
597 +}
598 +
599 +/* Create support for private data tracking. */
600 +static struct vc_sm_privdata_t *vc_sm_cma_create_priv_data(pid_t id)
601 +{
602 + char alloc_name[32];
603 + struct vc_sm_privdata_t *file_data = NULL;
604 +
605 + /* Allocate private structure. */
606 + file_data = kzalloc(sizeof(*file_data), GFP_KERNEL);
607 +
608 + if (!file_data)
609 + return NULL;
610 +
611 + snprintf(alloc_name, sizeof(alloc_name), "%d", id);
612 +
613 + file_data->pid = id;
614 +
615 + return file_data;
616 +}
617 +
618 +/* Dma buf operations for use with our own allocations */
619 +
620 +static int vc_sm_dma_buf_attach(struct dma_buf *dmabuf,
621 + struct dma_buf_attachment *attachment)
622 +
623 +{
624 + struct vc_sm_dma_buf_attachment *a;
625 + struct sg_table *sgt;
626 + struct vc_sm_buffer *buf = dmabuf->priv;
627 + struct scatterlist *rd, *wr;
628 + int ret, i;
629 +
630 + a = kzalloc(sizeof(*a), GFP_KERNEL);
631 + if (!a)
632 + return -ENOMEM;
633 +
634 + pr_debug("%s dmabuf %p attachment %p\n", __func__, dmabuf, attachment);
635 +
636 + mutex_lock(&buf->lock);
637 +
638 + INIT_LIST_HEAD(&a->list);
639 +
640 + sgt = &a->sg_table;
641 +
642 + /* Copy the buf->base_sgt scatter list to the attachment, as we can't
643 + * map the same scatter list to multiple attachments at the same time.
644 + */
645 + ret = sg_alloc_table(sgt, buf->alloc.sg_table->orig_nents, GFP_KERNEL);
646 + if (ret) {
647 + kfree(a);
648 + return -ENOMEM;
649 + }
650 +
651 + rd = buf->alloc.sg_table->sgl;
652 + wr = sgt->sgl;
653 + for (i = 0; i < sgt->orig_nents; ++i) {
654 + sg_set_page(wr, sg_page(rd), rd->length, rd->offset);
655 + rd = sg_next(rd);
656 + wr = sg_next(wr);
657 + }
658 +
659 + a->dma_dir = DMA_NONE;
660 + attachment->priv = a;
661 +
662 + list_add(&a->list, &buf->attachments);
663 + mutex_unlock(&buf->lock);
664 +
665 + return 0;
666 +}
667 +
668 +static void vc_sm_dma_buf_detach(struct dma_buf *dmabuf,
669 + struct dma_buf_attachment *attachment)
670 +{
671 + struct vc_sm_dma_buf_attachment *a = attachment->priv;
672 + struct vc_sm_buffer *buf = dmabuf->priv;
673 + struct sg_table *sgt;
674 +
675 + pr_debug("%s dmabuf %p attachment %p\n", __func__, dmabuf, attachment);
676 + if (!a)
677 + return;
678 +
679 + sgt = &a->sg_table;
680 +
681 + /* release the scatterlist cache */
682 + if (a->dma_dir != DMA_NONE)
683 + dma_unmap_sg(attachment->dev, sgt->sgl, sgt->orig_nents,
684 + a->dma_dir);
685 + sg_free_table(sgt);
686 +
687 + mutex_lock(&buf->lock);
688 + list_del(&a->list);
689 + mutex_unlock(&buf->lock);
690 +
691 + kfree(a);
692 +}
693 +
694 +static struct sg_table *vc_sm_map_dma_buf(struct dma_buf_attachment *attachment,
695 + enum dma_data_direction direction)
696 +{
697 + struct vc_sm_dma_buf_attachment *a = attachment->priv;
698 + /* stealing dmabuf mutex to serialize map/unmap operations */
699 + struct mutex *lock = &attachment->dmabuf->lock;
700 + struct sg_table *table;
701 +
702 + mutex_lock(lock);
703 + pr_debug("%s attachment %p\n", __func__, attachment);
704 + table = &a->sg_table;
705 +
706 + /* return previously mapped sg table */
707 + if (a->dma_dir == direction) {
708 + mutex_unlock(lock);
709 + return table;
710 + }
711 +
712 + /* release any previous cache */
713 + if (a->dma_dir != DMA_NONE) {
714 + dma_unmap_sg(attachment->dev, table->sgl, table->orig_nents,
715 + a->dma_dir);
716 + a->dma_dir = DMA_NONE;
717 + }
718 +
719 + /* mapping to the client with new direction */
720 + table->nents = dma_map_sg(attachment->dev, table->sgl,
721 + table->orig_nents, direction);
722 + if (!table->nents) {
723 + pr_err("failed to map scatterlist\n");
724 + mutex_unlock(lock);
725 + return ERR_PTR(-EIO);
726 + }
727 +
728 + a->dma_dir = direction;
729 + mutex_unlock(lock);
730 +
731 + pr_debug("%s attachment %p\n", __func__, attachment);
732 + return table;
733 +}
734 +
735 +static void vc_sm_unmap_dma_buf(struct dma_buf_attachment *attachment,
736 + struct sg_table *table,
737 + enum dma_data_direction direction)
738 +{
739 + pr_debug("%s attachment %p\n", __func__, attachment);
740 + dma_unmap_sg(attachment->dev, table->sgl, table->nents, direction);
741 +}
742 +
743 +static int vc_sm_dmabuf_mmap(struct dma_buf *dmabuf, struct vm_area_struct *vma)
744 +{
745 + struct vc_sm_buffer *buf = dmabuf->priv;
746 + int ret;
747 +
748 + pr_debug("%s dmabuf %p, buf %p, vm_start %08lX\n", __func__, dmabuf,
749 + buf, vma->vm_start);
750 +
751 + mutex_lock(&buf->lock);
752 +
753 + /* now map it to userspace */
754 + vma->vm_pgoff = 0;
755 +
756 + ret = dma_mmap_coherent(&sm_state->pdev->dev, vma, buf->cookie,
757 + buf->dma_addr, buf->size);
758 +
759 + if (ret) {
760 + pr_err("Remapping memory failed, error: %d\n", ret);
761 + return ret;
762 + }
763 +
764 + vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
765 +
766 + mutex_unlock(&buf->lock);
767 +
768 + if (ret)
769 + pr_err("%s: failure mapping buffer to userspace\n",
770 + __func__);
771 +
772 + return ret;
773 +}
774 +
775 +static void vc_sm_dma_buf_release(struct dma_buf *dmabuf)
776 +{
777 + struct vc_sm_buffer *buffer;
778 +
779 + if (!dmabuf)
780 + return;
781 +
782 + buffer = (struct vc_sm_buffer *)dmabuf->priv;
783 +
784 + mutex_lock(&buffer->lock);
785 +
786 + pr_debug("%s dmabuf %p, buffer %p\n", __func__, dmabuf, buffer);
787 +
788 + buffer->in_use = 0;
789 +
790 + /* Unmap on the VPU */
791 + vc_sm_vpu_free(buffer);
792 + pr_debug("%s vpu_free done\n", __func__);
793 +
794 + /* Unmap our dma_buf object (the vc_sm_buffer remains until released
795 + * on the VPU).
796 + */
797 + vc_sm_clean_up_dmabuf(buffer);
798 + pr_debug("%s clean_up dmabuf done\n", __func__);
799 +
800 + /* buffer->lock will be destroyed by vc_sm_release_resource if finished
801 + * with, otherwise unlocked. Do NOT unlock here.
802 + */
803 + vc_sm_release_resource(buffer);
804 + pr_debug("%s done\n", __func__);
805 +}
806 +
807 +static int vc_sm_dma_buf_begin_cpu_access(struct dma_buf *dmabuf,
808 + enum dma_data_direction direction)
809 +{
810 + struct vc_sm_buffer *buf;
811 + struct vc_sm_dma_buf_attachment *a;
812 +
813 + if (!dmabuf)
814 + return -EFAULT;
815 +
816 + buf = dmabuf->priv;
817 + if (!buf)
818 + return -EFAULT;
819 +
820 + mutex_lock(&buf->lock);
821 +
822 + list_for_each_entry(a, &buf->attachments, list) {
823 + dma_sync_sg_for_cpu(a->dev, a->sg_table.sgl,
824 + a->sg_table.nents, direction);
825 + }
826 + mutex_unlock(&buf->lock);
827 +
828 + return 0;
829 +}
830 +
831 +static int vc_sm_dma_buf_end_cpu_access(struct dma_buf *dmabuf,
832 + enum dma_data_direction direction)
833 +{
834 + struct vc_sm_buffer *buf;
835 + struct vc_sm_dma_buf_attachment *a;
836 +
837 + if (!dmabuf)
838 + return -EFAULT;
839 + buf = dmabuf->priv;
840 + if (!buf)
841 + return -EFAULT;
842 +
843 + mutex_lock(&buf->lock);
844 +
845 + list_for_each_entry(a, &buf->attachments, list) {
846 + dma_sync_sg_for_device(a->dev, a->sg_table.sgl,
847 + a->sg_table.nents, direction);
848 + }
849 + mutex_unlock(&buf->lock);
850 +
851 + return 0;
852 +}
853 +
854 +static const struct dma_buf_ops dma_buf_ops = {
855 + .map_dma_buf = vc_sm_map_dma_buf,
856 + .unmap_dma_buf = vc_sm_unmap_dma_buf,
857 + .mmap = vc_sm_dmabuf_mmap,
858 + .release = vc_sm_dma_buf_release,
859 + .attach = vc_sm_dma_buf_attach,
860 + .detach = vc_sm_dma_buf_detach,
861 + .begin_cpu_access = vc_sm_dma_buf_begin_cpu_access,
862 + .end_cpu_access = vc_sm_dma_buf_end_cpu_access,
863 +};
864 +
865 +/* Dma_buf operations for chaining through to an imported dma_buf */
866 +
867 +static
868 +int vc_sm_import_dma_buf_attach(struct dma_buf *dmabuf,
869 + struct dma_buf_attachment *attachment)
870 +{
871 + struct vc_sm_buffer *buf = dmabuf->priv;
872 +
873 + if (!buf->imported)
874 + return -EINVAL;
875 + return buf->import.dma_buf->ops->attach(buf->import.dma_buf,
876 + attachment);
877 +}
878 +
879 +static
880 +void vc_sm_import_dma_buf_detatch(struct dma_buf *dmabuf,
881 + struct dma_buf_attachment *attachment)
882 +{
883 + struct vc_sm_buffer *buf = dmabuf->priv;
884 +
885 + if (!buf->imported)
886 + return;
887 + buf->import.dma_buf->ops->detach(buf->import.dma_buf, attachment);
888 +}
889 +
890 +static
891 +struct sg_table *vc_sm_import_map_dma_buf(struct dma_buf_attachment *attachment,
892 + enum dma_data_direction direction)
893 +{
894 + struct vc_sm_buffer *buf = attachment->dmabuf->priv;
895 +
896 + if (!buf->imported)
897 + return NULL;
898 + return buf->import.dma_buf->ops->map_dma_buf(attachment,
899 + direction);
900 +}
901 +
902 +static
903 +void vc_sm_import_unmap_dma_buf(struct dma_buf_attachment *attachment,
904 + struct sg_table *table,
905 + enum dma_data_direction direction)
906 +{
907 + struct vc_sm_buffer *buf = attachment->dmabuf->priv;
908 +
909 + if (!buf->imported)
910 + return;
911 + buf->import.dma_buf->ops->unmap_dma_buf(attachment, table, direction);
912 +}
913 +
914 +static
915 +int vc_sm_import_dmabuf_mmap(struct dma_buf *dmabuf, struct vm_area_struct *vma)
916 +{
917 + struct vc_sm_buffer *buf = dmabuf->priv;
918 +
919 + pr_debug("%s: mmap dma_buf %p, buf %p, imported db %p\n", __func__,
920 + dmabuf, buf, buf->import.dma_buf);
921 + if (!buf->imported) {
922 + pr_err("%s: mmap dma_buf %p- not an imported buffer\n",
923 + __func__, dmabuf);
924 + return -EINVAL;
925 + }
926 + return buf->import.dma_buf->ops->mmap(buf->import.dma_buf, vma);
927 +}
928 +
929 +static
930 +int vc_sm_import_dma_buf_begin_cpu_access(struct dma_buf *dmabuf,
931 + enum dma_data_direction direction)
932 +{
933 + struct vc_sm_buffer *buf = dmabuf->priv;
934 +
935 + if (!buf->imported)
936 + return -EINVAL;
937 + return buf->import.dma_buf->ops->begin_cpu_access(buf->import.dma_buf,
938 + direction);
939 +}
940 +
941 +static
942 +int vc_sm_import_dma_buf_end_cpu_access(struct dma_buf *dmabuf,
943 + enum dma_data_direction direction)
944 +{
945 + struct vc_sm_buffer *buf = dmabuf->priv;
946 +
947 + if (!buf->imported)
948 + return -EINVAL;
949 + return buf->import.dma_buf->ops->end_cpu_access(buf->import.dma_buf,
950 + direction);
951 +}
952 +
953 +static const struct dma_buf_ops dma_buf_import_ops = {
954 + .map_dma_buf = vc_sm_import_map_dma_buf,
955 + .unmap_dma_buf = vc_sm_import_unmap_dma_buf,
956 + .mmap = vc_sm_import_dmabuf_mmap,
957 + .release = vc_sm_dma_buf_release,
958 + .attach = vc_sm_import_dma_buf_attach,
959 + .detach = vc_sm_import_dma_buf_detatch,
960 + .begin_cpu_access = vc_sm_import_dma_buf_begin_cpu_access,
961 + .end_cpu_access = vc_sm_import_dma_buf_end_cpu_access,
962 +};
963 +
964 +/* Import a dma_buf to be shared with VC. */
965 +int
966 +vc_sm_cma_import_dmabuf_internal(struct vc_sm_privdata_t *private,
967 + struct dma_buf *dma_buf,
968 + int fd,
969 + struct dma_buf **imported_buf)
970 +{
971 + DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
972 + struct vc_sm_buffer *buffer = NULL;
973 + struct vc_sm_import import = { };
974 + struct vc_sm_import_result result = { };
975 + struct dma_buf_attachment *attach = NULL;
976 + struct sg_table *sgt = NULL;
977 + dma_addr_t dma_addr;
978 + u32 cache_alias;
979 + int ret = 0;
980 + int status;
981 +
982 + /* Setup our allocation parameters */
983 + pr_debug("%s: importing dma_buf %p/fd %d\n", __func__, dma_buf, fd);
984 +
985 + if (fd < 0)
986 + get_dma_buf(dma_buf);
987 + else
988 + dma_buf = dma_buf_get(fd);
989 +
990 + if (!dma_buf)
991 + return -EINVAL;
992 +
993 + attach = dma_buf_attach(dma_buf, &sm_state->pdev->dev);
994 + if (IS_ERR(attach)) {
995 + ret = PTR_ERR(attach);
996 + goto error;
997 + }
998 +
999 + sgt = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
1000 + if (IS_ERR(sgt)) {
1001 + ret = PTR_ERR(sgt);
1002 + goto error;
1003 + }
1004 +
1005 + /* Verify that the address block is contiguous */
1006 + if (sgt->nents != 1) {
1007 + ret = -ENOMEM;
1008 + goto error;
1009 + }
1010 +
1011 + /* Allocate local buffer to track this allocation. */
1012 + buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
1013 + if (!buffer) {
1014 + ret = -ENOMEM;
1015 + goto error;
1016 + }
1017 +
1018 + import.type = VC_SM_ALLOC_NON_CACHED;
1019 + dma_addr = sg_dma_address(sgt->sgl);
1020 + import.addr = (u32)dma_addr;
1021 + cache_alias = import.addr & 0xC0000000;
1022 + if (cache_alias != 0xC0000000 && cache_alias != 0x80000000) {
1023 + pr_err("%s: Expecting an uncached alias for dma_addr %pad\n",
1024 + __func__, &dma_addr);
1025 + /* Note that this assumes we're on >= Pi2, but it implies a
1026 + * DT configuration error.
1027 + */
1028 + import.addr |= 0xC0000000;
1029 + }
1030 + import.size = sg_dma_len(sgt->sgl);
1031 + import.allocator = current->tgid;
1032 + import.kernel_id = get_kernel_id(buffer);
1033 +
1034 + memcpy(import.name, VC_SM_RESOURCE_NAME_DEFAULT,
1035 + sizeof(VC_SM_RESOURCE_NAME_DEFAULT));
1036 +
1037 + pr_debug("[%s]: attempt to import \"%s\" data - type %u, addr %pad, size %u.\n",
1038 + __func__, import.name, import.type, &dma_addr, import.size);
1039 +
1040 + /* Allocate the videocore buffer. */
1041 + status = vc_sm_cma_vchi_import(sm_state->sm_handle, &import, &result,
1042 + &sm_state->int_trans_id);
1043 + if (status == -EINTR) {
1044 + pr_debug("[%s]: requesting import memory action restart (trans_id: %u)\n",
1045 + __func__, sm_state->int_trans_id);
1046 + ret = -ERESTARTSYS;
1047 + private->restart_sys = -EINTR;
1048 + private->int_action = VC_SM_MSG_TYPE_IMPORT;
1049 + goto error;
1050 + } else if (status || !result.res_handle) {
1051 + pr_debug("[%s]: failed to import memory on videocore (status: %u, trans_id: %u)\n",
1052 + __func__, status, sm_state->int_trans_id);
1053 + ret = -ENOMEM;
1054 + goto error;
1055 + }
1056 +
1057 + mutex_init(&buffer->lock);
1058 + INIT_LIST_HEAD(&buffer->attachments);
1059 + memcpy(buffer->name, import.name,
1060 + min(sizeof(buffer->name), sizeof(import.name) - 1));
1061 +
1062 + /* Keep track of the buffer we created. */
1063 + buffer->private = private;
1064 + buffer->vc_handle = result.res_handle;
1065 + buffer->size = import.size;
1066 + buffer->vpu_state = VPU_MAPPED;
1067 +
1068 + buffer->imported = 1;
1069 + buffer->import.dma_buf = dma_buf;
1070 +
1071 + buffer->import.attach = attach;
1072 + buffer->import.sgt = sgt;
1073 + buffer->dma_addr = dma_addr;
1074 + buffer->in_use = 1;
1075 + buffer->kernel_id = import.kernel_id;
1076 +
1077 + /*
1078 + * We're done - we need to export a new dmabuf chaining through most
1079 + * functions, but enabling us to release our own internal references
1080 + * here.
1081 + */
1082 + exp_info.ops = &dma_buf_import_ops;
1083 + exp_info.size = import.size;
1084 + exp_info.flags = O_RDWR;
1085 + exp_info.priv = buffer;
1086 +
1087 + buffer->dma_buf = dma_buf_export(&exp_info);
1088 + if (IS_ERR(buffer->dma_buf)) {
1089 + ret = PTR_ERR(buffer->dma_buf);
1090 + goto error;
1091 + }
1092 +
1093 + vc_sm_add_resource(private, buffer);
1094 +
1095 + *imported_buf = buffer->dma_buf;
1096 +
1097 + return 0;
1098 +
1099 +error:
1100 + if (result.res_handle) {
1101 + struct vc_sm_free_t free = { result.res_handle, 0 };
1102 +
1103 + vc_sm_cma_vchi_free(sm_state->sm_handle, &free,
1104 + &sm_state->int_trans_id);
1105 + }
1106 + free_kernel_id(import.kernel_id);
1107 + kfree(buffer);
1108 + if (sgt)
1109 + dma_buf_unmap_attachment(attach, sgt, DMA_BIDIRECTIONAL);
1110 + if (attach)
1111 + dma_buf_detach(dma_buf, attach);
1112 + dma_buf_put(dma_buf);
1113 + return ret;
1114 +}
1115 +
1116 +static int vc_sm_cma_vpu_alloc(u32 size, u32 align, const char *name,
1117 + u32 mem_handle, struct vc_sm_buffer **ret_buffer)
1118 +{
1119 + DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
1120 + struct vc_sm_buffer *buffer = NULL;
1121 + struct sg_table *sgt;
1122 + int aligned_size;
1123 + int ret = 0;
1124 +
1125 + /* Align to the user requested align */
1126 + aligned_size = ALIGN(size, align);
1127 + /* and then to a page boundary */
1128 + aligned_size = PAGE_ALIGN(aligned_size);
1129 +
1130 + if (!aligned_size)
1131 + return -EINVAL;
1132 +
1133 + /* Allocate local buffer to track this allocation. */
1134 + buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
1135 + if (!buffer)
1136 + return -ENOMEM;
1137 +
1138 + mutex_init(&buffer->lock);
1139 + /* Acquire the mutex as vc_sm_release_resource will release it in the
1140 + * error path.
1141 + */
1142 + mutex_lock(&buffer->lock);
1143 +
1144 + buffer->cookie = dma_alloc_coherent(&sm_state->pdev->dev,
1145 + aligned_size, &buffer->dma_addr,
1146 + GFP_KERNEL);
1147 + if (!buffer->cookie) {
1148 + pr_err("[%s]: dma_alloc_coherent alloc of %d bytes failed\n",
1149 + __func__, aligned_size);
1150 + ret = -ENOMEM;
1151 + goto error;
1152 + }
1153 +
1154 + pr_debug("[%s]: alloc of %d bytes success\n",
1155 + __func__, aligned_size);
1156 +
1157 + sgt = kmalloc(sizeof(*sgt), GFP_KERNEL);
1158 + if (!sgt) {
1159 + ret = -ENOMEM;
1160 + goto error;
1161 + }
1162 +
1163 + ret = dma_get_sgtable(&sm_state->pdev->dev, sgt, buffer->cookie,
1164 + buffer->dma_addr, buffer->size);
1165 + if (ret < 0) {
1166 + pr_err("failed to get scatterlist from DMA API\n");
1167 + kfree(sgt);
1168 + ret = -ENOMEM;
1169 + goto error;
1170 + }
1171 + buffer->alloc.sg_table = sgt;
1172 +
1173 + INIT_LIST_HEAD(&buffer->attachments);
1174 +
1175 + memcpy(buffer->name, name,
1176 + min(sizeof(buffer->name), strlen(name)));
1177 +
1178 + exp_info.ops = &dma_buf_ops;
1179 + exp_info.size = aligned_size;
1180 + exp_info.flags = O_RDWR;
1181 + exp_info.priv = buffer;
1182 +
1183 + buffer->dma_buf = dma_buf_export(&exp_info);
1184 + if (IS_ERR(buffer->dma_buf)) {
1185 + ret = PTR_ERR(buffer->dma_buf);
1186 + goto error;
1187 + }
1188 + buffer->dma_addr = (u32)sg_dma_address(buffer->alloc.sg_table->sgl);
1189 + if ((buffer->dma_addr & 0xC0000000) != 0xC0000000) {
1190 + pr_warn_once("%s: Expecting an uncached alias for dma_addr %pad\n",
1191 + __func__, &buffer->dma_addr);
1192 + buffer->dma_addr |= 0xC0000000;
1193 + }
1194 + buffer->private = sm_state->vpu_allocs;
1195 +
1196 + buffer->vc_handle = mem_handle;
1197 + buffer->vpu_state = VPU_MAPPED;
1198 + buffer->vpu_allocated = 1;
1199 + buffer->size = size;
1200 + /*
1201 + * Create an ID that will be passed along with our message so
1202 + * that when we service the release reply, we can look up which
1203 + * resource is being released.
1204 + */
1205 + buffer->kernel_id = get_kernel_id(buffer);
1206 +
1207 + vc_sm_add_resource(sm_state->vpu_allocs, buffer);
1208 +
1209 + mutex_unlock(&buffer->lock);
1210 +
1211 + *ret_buffer = buffer;
1212 + return 0;
1213 +error:
1214 + if (buffer)
1215 + vc_sm_release_resource(buffer);
1216 + return ret;
1217 +}
1218 +
1219 +static void
1220 +vc_sm_vpu_event(struct sm_instance *instance, struct vc_sm_result_t *reply,
1221 + int reply_len)
1222 +{
1223 + switch (reply->trans_id & ~0x80000000) {
1224 + case VC_SM_MSG_TYPE_CLIENT_VERSION:
1225 + {
1226 + /* Acknowledge that the firmware supports the version command */
1227 + pr_debug("%s: firmware acked version msg. Require release cb\n",
1228 + __func__);
1229 + sm_state->require_released_callback = true;
1230 + }
1231 + break;
1232 + case VC_SM_MSG_TYPE_RELEASED:
1233 + {
1234 + struct vc_sm_released *release = (struct vc_sm_released *)reply;
1235 + struct vc_sm_buffer *buffer =
1236 + lookup_kernel_id(release->kernel_id);
1237 + if (!buffer) {
1238 + pr_err("%s: VC released a buffer that is already released, kernel_id %d\n",
1239 + __func__, release->kernel_id);
1240 + break;
1241 + }
1242 + mutex_lock(&buffer->lock);
1243 +
1244 + pr_debug("%s: Released addr %08x, size %u, id %08x, mem_handle %08x\n",
1245 + __func__, release->addr, release->size,
1246 + release->kernel_id, release->vc_handle);
1247 +
1248 + buffer->vc_handle = 0;
1249 + buffer->vpu_state = VPU_NOT_MAPPED;
1250 + free_kernel_id(release->kernel_id);
1251 +
1252 + if (buffer->vpu_allocated) {
1253 + /* VPU allocation, so release the dmabuf which will
1254 + * trigger the clean up.
1255 + */
1256 + mutex_unlock(&buffer->lock);
1257 + dma_buf_put(buffer->dma_buf);
1258 + } else {
1259 + vc_sm_release_resource(buffer);
1260 + }
1261 + }
1262 + break;
1263 + case VC_SM_MSG_TYPE_VC_MEM_REQUEST:
1264 + {
1265 + struct vc_sm_buffer *buffer = NULL;
1266 + struct vc_sm_vc_mem_request *req =
1267 + (struct vc_sm_vc_mem_request *)reply;
1268 + struct vc_sm_vc_mem_request_result reply;
1269 + int ret;
1270 +
1271 + pr_debug("%s: Request %u bytes of memory, align %d name %s, trans_id %08x\n",
1272 + __func__, req->size, req->align, req->name,
1273 + req->trans_id);
1274 + ret = vc_sm_cma_vpu_alloc(req->size, req->align, req->name,
1275 + req->vc_handle, &buffer);
1276 +
1277 + reply.trans_id = req->trans_id;
1278 + if (!ret) {
1279 + reply.addr = buffer->dma_addr;
1280 + reply.kernel_id = buffer->kernel_id;
1281 + pr_debug("%s: Allocated resource buffer %p, addr %pad\n",
1282 + __func__, buffer, &buffer->dma_addr);
1283 + } else {
1284 + pr_err("%s: Allocation failed size %u, name %s, vc_handle %u\n",
1285 + __func__, req->size, req->name, req->vc_handle);
1286 + reply.addr = 0;
1287 + reply.kernel_id = 0;
1288 + }
1289 + vc_sm_vchi_client_vc_mem_req_reply(sm_state->sm_handle, &reply,
1290 + &sm_state->int_trans_id);
1291 + break;
1292 + }
1293 + break;
1294 + default:
1295 + pr_err("%s: Unknown vpu cmd %x\n", __func__, reply->trans_id);
1296 + break;
1297 + }
1298 +}
1299 +
1300 +/* Userspace handling */
1301 +/*
1302 + * Open the device. Creates a private state to help track all allocation
1303 + * associated with this device.
1304 + */
1305 +static int vc_sm_cma_open(struct inode *inode, struct file *file)
1306 +{
1307 + /* Make sure the device was started properly. */
1308 + if (!sm_state) {
1309 + pr_err("[%s]: invalid device\n", __func__);
1310 + return -EPERM;
1311 + }
1312 +
1313 + file->private_data = vc_sm_cma_create_priv_data(current->tgid);
1314 + if (!file->private_data) {
1315 + pr_err("[%s]: failed to create data tracker\n", __func__);
1316 +
1317 + return -ENOMEM;
1318 + }
1319 +
1320 + return 0;
1321 +}
1322 +
1323 +/*
1324 + * Close the vcsm-cma device.
1325 + * All allocations are file descriptors to the dmabuf objects, so we will get
1326 + * the clean up request on those as those are cleaned up.
1327 + */
1328 +static int vc_sm_cma_release(struct inode *inode, struct file *file)
1329 +{
1330 + struct vc_sm_privdata_t *file_data =
1331 + (struct vc_sm_privdata_t *)file->private_data;
1332 + int ret = 0;
1333 +
1334 + /* Make sure the device was started properly. */
1335 + if (!sm_state || !file_data) {
1336 + pr_err("[%s]: invalid device\n", __func__);
1337 + ret = -EPERM;
1338 + goto out;
1339 + }
1340 +
1341 + pr_debug("[%s]: using private data %p\n", __func__, file_data);
1342 +
1343 + /* Terminate the private data. */
1344 + kfree(file_data);
1345 +
1346 +out:
1347 + return ret;
1348 +}
1349 +
1350 +/*
1351 + * Allocate a shared memory handle and block.
1352 + * Allocation is from CMA, and then imported into the VPU mappings.
1353 + */
1354 +int vc_sm_cma_ioctl_alloc(struct vc_sm_privdata_t *private,
1355 + struct vc_sm_cma_ioctl_alloc *ioparam)
1356 +{
1357 + DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
1358 + struct vc_sm_buffer *buffer = NULL;
1359 + struct vc_sm_import import = { 0 };
1360 + struct vc_sm_import_result result = { 0 };
1361 + struct dma_buf *dmabuf = NULL;
1362 + struct sg_table *sgt;
1363 + int aligned_size;
1364 + int ret = 0;
1365 + int status;
1366 + int fd = -1;
1367 +
1368 + aligned_size = PAGE_ALIGN(ioparam->size);
1369 +
1370 + if (!aligned_size)
1371 + return -EINVAL;
1372 +
1373 + /* Allocate local buffer to track this allocation. */
1374 + buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
1375 + if (!buffer) {
1376 + ret = -ENOMEM;
1377 + goto error;
1378 + }
1379 +
1380 + buffer->cookie = dma_alloc_coherent(&sm_state->pdev->dev,
1381 + aligned_size,
1382 + &buffer->dma_addr,
1383 + GFP_KERNEL);
1384 + if (!buffer->cookie) {
1385 + pr_err("[%s]: dma_alloc_coherent alloc of %d bytes failed\n",
1386 + __func__, aligned_size);
1387 + ret = -ENOMEM;
1388 + goto error;
1389 + }
1390 +
1391 + import.type = VC_SM_ALLOC_NON_CACHED;
1392 + import.allocator = current->tgid;
1393 +
1394 + if (*ioparam->name)
1395 + memcpy(import.name, ioparam->name, sizeof(import.name) - 1);
1396 + else
1397 + memcpy(import.name, VC_SM_RESOURCE_NAME_DEFAULT,
1398 + sizeof(VC_SM_RESOURCE_NAME_DEFAULT));
1399 +
1400 + mutex_init(&buffer->lock);
1401 + INIT_LIST_HEAD(&buffer->attachments);
1402 + memcpy(buffer->name, import.name,
1403 + min(sizeof(buffer->name), sizeof(import.name) - 1));
1404 +
1405 + exp_info.ops = &dma_buf_ops;
1406 + exp_info.size = aligned_size;
1407 + exp_info.flags = O_RDWR;
1408 + exp_info.priv = buffer;
1409 +
1410 + dmabuf = dma_buf_export(&exp_info);
1411 + if (IS_ERR(dmabuf)) {
1412 + ret = PTR_ERR(dmabuf);
1413 + goto error;
1414 + }
1415 + buffer->dma_buf = dmabuf;
1416 +
1417 + import.addr = buffer->dma_addr;
1418 + import.size = aligned_size;
1419 + import.kernel_id = get_kernel_id(buffer);
1420 +
1421 + /* Wrap it into a videocore buffer. */
1422 + status = vc_sm_cma_vchi_import(sm_state->sm_handle, &import, &result,
1423 + &sm_state->int_trans_id);
1424 + if (status == -EINTR) {
1425 + pr_debug("[%s]: requesting import memory action restart (trans_id: %u)\n",
1426 + __func__, sm_state->int_trans_id);
1427 + ret = -ERESTARTSYS;
1428 + private->restart_sys = -EINTR;
1429 + private->int_action = VC_SM_MSG_TYPE_IMPORT;
1430 + goto error;
1431 + } else if (status || !result.res_handle) {
1432 + pr_err("[%s]: failed to import memory on videocore (status: %u, trans_id: %u)\n",
1433 + __func__, status, sm_state->int_trans_id);
1434 + ret = -ENOMEM;
1435 + goto error;
1436 + }
1437 +
1438 + /* Keep track of the buffer we created. */
1439 + buffer->private = private;
1440 + buffer->vc_handle = result.res_handle;
1441 + buffer->size = import.size;
1442 + buffer->vpu_state = VPU_MAPPED;
1443 + buffer->kernel_id = import.kernel_id;
1444 +
1445 + sgt = kmalloc(sizeof(*sgt), GFP_KERNEL);
1446 + if (!sgt) {
1447 + ret = -ENOMEM;
1448 + goto error;
1449 + }
1450 +
1451 + ret = dma_get_sgtable(&sm_state->pdev->dev, sgt, buffer->cookie,
1452 + buffer->dma_addr, buffer->size);
1453 + if (ret < 0) {
1454 + /* FIXME: error handling */
1455 + pr_err("failed to get scatterlist from DMA API\n");
1456 + kfree(sgt);
1457 + ret = -ENOMEM;
1458 + goto error;
1459 + }
1460 + buffer->alloc.sg_table = sgt;
1461 +
1462 + fd = dma_buf_fd(dmabuf, O_CLOEXEC);
1463 + if (fd < 0)
1464 + goto error;
1465 +
1466 + vc_sm_add_resource(private, buffer);
1467 +
1468 + pr_debug("[%s]: Added resource as fd %d, buffer %p, private %p, dma_addr %pad\n",
1469 + __func__, fd, buffer, private, &buffer->dma_addr);
1470 +
1471 + /* We're done */
1472 + ioparam->handle = fd;
1473 + ioparam->vc_handle = buffer->vc_handle;
1474 + ioparam->dma_addr = buffer->dma_addr;
1475 + return 0;
1476 +
1477 +error:
1478 + pr_err("[%s]: something failed - cleanup. ret %d\n", __func__, ret);
1479 +
1480 + if (dmabuf) {
1481 + /* dmabuf has been exported, therefore allow dmabuf cleanup to
1482 + * deal with this
1483 + */
1484 + dma_buf_put(dmabuf);
1485 + } else {
1486 + /* No dmabuf, therefore just free the buffer here */
1487 + if (buffer->cookie)
1488 + dma_free_coherent(&sm_state->pdev->dev, buffer->size,
1489 + buffer->cookie, buffer->dma_addr);
1490 + kfree(buffer);
1491 + }
1492 + return ret;
1493 +}
1494 +
1495 +#ifndef CONFIG_ARM64
1496 +/* Converts VCSM_CACHE_OP_* to an operating function. */
1497 +static void (*cache_op_to_func(const unsigned int cache_op))
1498 + (const void*, const void*)
1499 +{
1500 + switch (cache_op) {
1501 + case VC_SM_CACHE_OP_NOP:
1502 + return NULL;
1503 +
1504 + case VC_SM_CACHE_OP_INV:
1505 + return dmac_inv_range;
1506 + case VC_SM_CACHE_OP_CLEAN:
1507 + return dmac_clean_range;
1508 + case VC_SM_CACHE_OP_FLUSH:
1509 + return dmac_flush_range;
1510 +
1511 + default:
1512 + pr_err("[%s]: Invalid cache_op: 0x%08x\n", __func__, cache_op);
1513 + return NULL;
1514 + }
1515 +}
1516 +
1517 +/*
1518 + * Clean/invalid/flush cache of which buffer is already pinned (i.e. accessed).
1519 + */
1520 +static int clean_invalid_contig_2d(const void __user *addr,
1521 + const size_t block_count,
1522 + const size_t block_size,
1523 + const size_t stride,
1524 + const unsigned int cache_op)
1525 +{
1526 + size_t i;
1527 + void (*op_fn)(const void *start, const void *end);
1528 +
1529 + if (!block_size) {
1530 + pr_err("[%s]: size cannot be 0\n", __func__);
1531 + return -EINVAL;
1532 + }
1533 +
1534 + op_fn = cache_op_to_func(cache_op);
1535 + if (!op_fn)
1536 + return -EINVAL;
1537 +
1538 + for (i = 0; i < block_count; i ++, addr += stride)
1539 + op_fn(addr, addr + block_size);
1540 +
1541 + return 0;
1542 +}
1543 +
1544 +static int vc_sm_cma_clean_invalid2(unsigned int cmdnr, unsigned long arg)
1545 +{
1546 + struct vc_sm_cma_ioctl_clean_invalid2 ioparam;
1547 + struct vc_sm_cma_ioctl_clean_invalid_block *block = NULL;
1548 + int i, ret = 0;
1549 +
1550 + /* Get parameter data. */
1551 + if (copy_from_user(&ioparam, (void *)arg, sizeof(ioparam))) {
1552 + pr_err("[%s]: failed to copy-from-user header for cmd %x\n",
1553 + __func__, cmdnr);
1554 + return -EFAULT;
1555 + }
1556 + block = kmalloc(ioparam.op_count * sizeof(*block), GFP_KERNEL);
1557 + if (!block)
1558 + return -EFAULT;
1559 +
1560 + if (copy_from_user(block, (void *)(arg + sizeof(ioparam)),
1561 + ioparam.op_count * sizeof(*block)) != 0) {
1562 + pr_err("[%s]: failed to copy-from-user payload for cmd %x\n",
1563 + __func__, cmdnr);
1564 + ret = -EFAULT;
1565 + goto out;
1566 + }
1567 +
1568 + for (i = 0; i < ioparam.op_count; i++) {
1569 + const struct vc_sm_cma_ioctl_clean_invalid_block * const op =
1570 + block + i;
1571 +
1572 + if (op->invalidate_mode == VC_SM_CACHE_OP_NOP)
1573 + continue;
1574 +
1575 + ret = clean_invalid_contig_2d((void __user *)op->start_address,
1576 + op->block_count, op->block_size,
1577 + op->inter_block_stride,
1578 + op->invalidate_mode);
1579 + if (ret)
1580 + break;
1581 + }
1582 +out:
1583 + kfree(block);
1584 +
1585 + return ret;
1586 +}
1587 +#endif
1588 +
1589 +static long vc_sm_cma_ioctl(struct file *file, unsigned int cmd,
1590 + unsigned long arg)
1591 +{
1592 + int ret = 0;
1593 + unsigned int cmdnr = _IOC_NR(cmd);
1594 + struct vc_sm_privdata_t *file_data =
1595 + (struct vc_sm_privdata_t *)file->private_data;
1596 +
1597 + /* Validate we can work with this device. */
1598 + if (!sm_state || !file_data) {
1599 + pr_err("[%s]: invalid device\n", __func__);
1600 + return -EPERM;
1601 + }
1602 +
1603 + /* Action is a re-post of a previously interrupted action? */
1604 + if (file_data->restart_sys == -EINTR) {
1605 + pr_debug("[%s]: clean up of action %u (trans_id: %u) following EINTR\n",
1606 + __func__, file_data->int_action,
1607 + file_data->int_trans_id);
1608 +
1609 + file_data->restart_sys = 0;
1610 + }
1611 +
1612 + /* Now process the command. */
1613 + switch (cmdnr) {
1614 + /* New memory allocation.
1615 + */
1616 + case VC_SM_CMA_CMD_ALLOC:
1617 + {
1618 + struct vc_sm_cma_ioctl_alloc ioparam;
1619 +
1620 + /* Get the parameter data. */
1621 + if (copy_from_user
1622 + (&ioparam, (void *)arg, sizeof(ioparam)) != 0) {
1623 + pr_err("[%s]: failed to copy-from-user for cmd %x\n",
1624 + __func__, cmdnr);
1625 + ret = -EFAULT;
1626 + break;
1627 + }
1628 +
1629 + ret = vc_sm_cma_ioctl_alloc(file_data, &ioparam);
1630 + if (!ret &&
1631 + (copy_to_user((void *)arg, &ioparam,
1632 + sizeof(ioparam)) != 0)) {
1633 + /* FIXME: Release allocation */
1634 + pr_err("[%s]: failed to copy-to-user for cmd %x\n",
1635 + __func__, cmdnr);
1636 + ret = -EFAULT;
1637 + }
1638 + break;
1639 + }
1640 +
1641 + case VC_SM_CMA_CMD_IMPORT_DMABUF:
1642 + {
1643 + struct vc_sm_cma_ioctl_import_dmabuf ioparam;
1644 + struct dma_buf *new_dmabuf;
1645 +
1646 + /* Get the parameter data. */
1647 + if (copy_from_user
1648 + (&ioparam, (void *)arg, sizeof(ioparam)) != 0) {
1649 + pr_err("[%s]: failed to copy-from-user for cmd %x\n",
1650 + __func__, cmdnr);
1651 + ret = -EFAULT;
1652 + break;
1653 + }
1654 +
1655 + ret = vc_sm_cma_import_dmabuf_internal(file_data,
1656 + NULL,
1657 + ioparam.dmabuf_fd,
1658 + &new_dmabuf);
1659 +
1660 + if (!ret) {
1661 + struct vc_sm_buffer *buf = new_dmabuf->priv;
1662 +
1663 + ioparam.size = buf->size;
1664 + ioparam.handle = dma_buf_fd(new_dmabuf,
1665 + O_CLOEXEC);
1666 + ioparam.vc_handle = buf->vc_handle;
1667 + ioparam.dma_addr = buf->dma_addr;
1668 +
1669 + if (ioparam.handle < 0 ||
1670 + (copy_to_user((void *)arg, &ioparam,
1671 + sizeof(ioparam)) != 0)) {
1672 + dma_buf_put(new_dmabuf);
1673 + /* FIXME: Release allocation */
1674 + ret = -EFAULT;
1675 + }
1676 + }
1677 + break;
1678 + }
1679 +
1680 +#ifndef CONFIG_ARM64
1681 + /*
1682 + * Flush/Invalidate the cache for a given mapping.
1683 + * Blocks must be pinned (i.e. accessed) before this call.
1684 + */
1685 + case VC_SM_CMA_CMD_CLEAN_INVALID2:
1686 + ret = vc_sm_cma_clean_invalid2(cmdnr, arg);
1687 + break;
1688 +#endif
1689 +
1690 + default:
1691 + pr_debug("[%s]: cmd %x tgid %u, owner %u\n", __func__, cmdnr,
1692 + current->tgid, file_data->pid);
1693 +
1694 + ret = -EINVAL;
1695 + break;
1696 + }
1697 +
1698 + return ret;
1699 +}
1700 +
1701 +#ifdef CONFIG_COMPAT
1702 +struct vc_sm_cma_ioctl_clean_invalid2_32 {
1703 + u32 op_count;
1704 + struct vc_sm_cma_ioctl_clean_invalid_block_32 {
1705 + u16 invalidate_mode;
1706 + u16 block_count;
1707 + compat_uptr_t start_address;
1708 + u32 block_size;
1709 + u32 inter_block_stride;
1710 + } s[0];
1711 +};
1712 +
1713 +#define VC_SM_CMA_CMD_CLEAN_INVALID2_32\
1714 + _IOR(VC_SM_CMA_MAGIC_TYPE, VC_SM_CMA_CMD_CLEAN_INVALID2,\
1715 + struct vc_sm_cma_ioctl_clean_invalid2_32)
1716 +
1717 +static long vc_sm_cma_compat_ioctl(struct file *file, unsigned int cmd,
1718 + unsigned long arg)
1719 +{
1720 + switch (cmd) {
1721 + case VC_SM_CMA_CMD_CLEAN_INVALID2_32:
1722 + /* FIXME */
1723 + return -EINVAL;
1724 +
1725 + default:
1726 + return vc_sm_cma_ioctl(file, cmd, arg);
1727 + }
1728 +}
1729 +#endif
1730 +
1731 +/* Device operations that we managed in this driver. */
1732 +static const struct file_operations vc_sm_ops = {
1733 + .owner = THIS_MODULE,
1734 + .unlocked_ioctl = vc_sm_cma_ioctl,
1735 +#ifdef CONFIG_COMPAT
1736 + .compat_ioctl = vc_sm_cma_compat_ioctl,
1737 +#endif
1738 + .open = vc_sm_cma_open,
1739 + .release = vc_sm_cma_release,
1740 +};
1741 +
1742 +/* Driver load/unload functions */
1743 +/* Videocore connected. */
1744 +static void vc_sm_connected_init(void)
1745 +{
1746 + int ret;
1747 + struct vchiq_instance *vchiq_instance;
1748 + struct vc_sm_version version;
1749 + struct vc_sm_result_t version_result;
1750 +
1751 + pr_info("[%s]: start\n", __func__);
1752 +
1753 + /*
1754 + * Initialize and create a VCHI connection for the shared memory service
1755 + * running on videocore.
1756 + */
1757 + ret = vchiq_initialise(&vchiq_instance);
1758 + if (ret) {
1759 + pr_err("[%s]: failed to initialise VCHI instance (ret=%d)\n",
1760 + __func__, ret);
1761 +
1762 + return;
1763 + }
1764 +
1765 + ret = vchiq_connect(vchiq_instance);
1766 + if (ret) {
1767 + pr_err("[%s]: failed to connect VCHI instance (ret=%d)\n",
1768 + __func__, ret);
1769 +
1770 + return;
1771 + }
1772 +
1773 + /* Initialize an instance of the shared memory service. */
1774 + sm_state->sm_handle = vc_sm_cma_vchi_init(vchiq_instance, 1,
1775 + vc_sm_vpu_event);
1776 + if (!sm_state->sm_handle) {
1777 + pr_err("[%s]: failed to initialize shared memory service\n",
1778 + __func__);
1779 +
1780 + return;
1781 + }
1782 +
1783 + /* Create a debug fs directory entry (root). */
1784 + sm_state->dir_root = debugfs_create_dir(VC_SM_DIR_ROOT_NAME, NULL);
1785 +
1786 + sm_state->dir_state.show = &vc_sm_cma_global_state_show;
1787 + sm_state->dir_state.dir_entry =
1788 + debugfs_create_file(VC_SM_STATE, 0444, sm_state->dir_root,
1789 + &sm_state->dir_state,
1790 + &vc_sm_cma_debug_fs_fops);
1791 +
1792 + INIT_LIST_HEAD(&sm_state->buffer_list);
1793 +
1794 + /* Create a shared memory device. */
1795 + sm_state->misc_dev.minor = MISC_DYNAMIC_MINOR;
1796 + sm_state->misc_dev.name = DEVICE_NAME;
1797 + sm_state->misc_dev.fops = &vc_sm_ops;
1798 + sm_state->misc_dev.parent = NULL;
1799 + /* Temporarily set as 666 until udev rules have been sorted */
1800 + sm_state->misc_dev.mode = 0666;
1801 + ret = misc_register(&sm_state->misc_dev);
1802 + if (ret) {
1803 + pr_err("vcsm-cma: failed to register misc device.\n");
1804 + goto err_remove_debugfs;
1805 + }
1806 +
1807 + sm_state->data_knl = vc_sm_cma_create_priv_data(0);
1808 + if (!sm_state->data_knl) {
1809 + pr_err("[%s]: failed to create kernel private data tracker\n",
1810 + __func__);
1811 + goto err_remove_misc_dev;
1812 + }
1813 +
1814 + version.version = 2;
1815 + ret = vc_sm_cma_vchi_client_version(sm_state->sm_handle, &version,
1816 + &version_result,
1817 + &sm_state->int_trans_id);
1818 + if (ret) {
1819 + pr_err("[%s]: Failed to send version request %d\n", __func__,
1820 + ret);
1821 + }
1822 +
1823 + /* Done! */
1824 + sm_inited = 1;
1825 + pr_info("[%s]: installed successfully\n", __func__);
1826 + return;
1827 +
1828 +err_remove_misc_dev:
1829 + misc_deregister(&sm_state->misc_dev);
1830 +err_remove_debugfs:
1831 + debugfs_remove_recursive(sm_state->dir_root);
1832 + vc_sm_cma_vchi_stop(&sm_state->sm_handle);
1833 +}
1834 +
1835 +/* Driver loading. */
1836 +static int bcm2835_vc_sm_cma_probe(struct platform_device *pdev)
1837 +{
1838 + pr_info("%s: Videocore shared memory driver\n", __func__);
1839 +
1840 + sm_state = devm_kzalloc(&pdev->dev, sizeof(*sm_state), GFP_KERNEL);
1841 + if (!sm_state)
1842 + return -ENOMEM;
1843 + sm_state->pdev = pdev;
1844 + mutex_init(&sm_state->map_lock);
1845 +
1846 + spin_lock_init(&sm_state->kernelid_map_lock);
1847 + idr_init_base(&sm_state->kernelid_map, 1);
1848 +
1849 + pdev->dev.dma_parms = devm_kzalloc(&pdev->dev,
1850 + sizeof(*pdev->dev.dma_parms),
1851 + GFP_KERNEL);
1852 + /* dma_set_max_seg_size checks if dma_parms is NULL. */
1853 + dma_set_max_seg_size(&pdev->dev, 0x3FFFFFFF);
1854 +
1855 + vchiq_add_connected_callback(vc_sm_connected_init);
1856 + return 0;
1857 +}
1858 +
1859 +/* Driver unloading. */
1860 +static int bcm2835_vc_sm_cma_remove(struct platform_device *pdev)
1861 +{
1862 + pr_debug("[%s]: start\n", __func__);
1863 + if (sm_inited) {
1864 + misc_deregister(&sm_state->misc_dev);
1865 +
1866 + /* Remove all proc entries. */
1867 + debugfs_remove_recursive(sm_state->dir_root);
1868 +
1869 + /* Stop the videocore shared memory service. */
1870 + vc_sm_cma_vchi_stop(&sm_state->sm_handle);
1871 + }
1872 +
1873 + if (sm_state) {
1874 + idr_destroy(&sm_state->kernelid_map);
1875 +
1876 + /* Free the memory for the state structure. */
1877 + mutex_destroy(&sm_state->map_lock);
1878 + }
1879 +
1880 + pr_debug("[%s]: end\n", __func__);
1881 + return 0;
1882 +}
1883 +
1884 +/* Kernel API calls */
1885 +/* Get an internal resource handle mapped from the external one. */
1886 +int vc_sm_cma_int_handle(void *handle)
1887 +{
1888 + struct dma_buf *dma_buf = (struct dma_buf *)handle;
1889 + struct vc_sm_buffer *buf;
1890 +
1891 + /* Validate we can work with this device. */
1892 + if (!sm_state || !handle) {
1893 + pr_err("[%s]: invalid input\n", __func__);
1894 + return 0;
1895 + }
1896 +
1897 + buf = (struct vc_sm_buffer *)dma_buf->priv;
1898 + return buf->vc_handle;
1899 +}
1900 +EXPORT_SYMBOL_GPL(vc_sm_cma_int_handle);
1901 +
1902 +/* Free a previously allocated shared memory handle and block. */
1903 +int vc_sm_cma_free(void *handle)
1904 +{
1905 + struct dma_buf *dma_buf = (struct dma_buf *)handle;
1906 +
1907 + /* Validate we can work with this device. */
1908 + if (!sm_state || !handle) {
1909 + pr_err("[%s]: invalid input\n", __func__);
1910 + return -EPERM;
1911 + }
1912 +
1913 + pr_debug("%s: handle %p/dmabuf %p\n", __func__, handle, dma_buf);
1914 +
1915 + dma_buf_put(dma_buf);
1916 +
1917 + return 0;
1918 +}
1919 +EXPORT_SYMBOL_GPL(vc_sm_cma_free);
1920 +
1921 +/* Import a dmabuf to be shared with VC. */
1922 +int vc_sm_cma_import_dmabuf(struct dma_buf *src_dmabuf, void **handle)
1923 +{
1924 + struct dma_buf *new_dma_buf;
1925 + int ret;
1926 +
1927 + /* Validate we can work with this device. */
1928 + if (!sm_state || !src_dmabuf || !handle) {
1929 + pr_err("[%s]: invalid input\n", __func__);
1930 + return -EPERM;
1931 + }
1932 +
1933 + ret = vc_sm_cma_import_dmabuf_internal(sm_state->data_knl, src_dmabuf,
1934 + -1, &new_dma_buf);
1935 +
1936 + if (!ret) {
1937 + pr_debug("%s: imported to ptr %p\n", __func__, new_dma_buf);
1938 +
1939 + /* Assign valid handle at this time.*/
1940 + *handle = new_dma_buf;
1941 + } else {
1942 + /*
1943 + * succeeded in importing the dma_buf, but then
1944 + * failed to look it up again. How?
1945 + * Release the fd again.
1946 + */
1947 + pr_err("%s: imported vc_sm_cma_get_buffer failed %d\n",
1948 + __func__, ret);
1949 + }
1950 +
1951 + return ret;
1952 +}
1953 +EXPORT_SYMBOL_GPL(vc_sm_cma_import_dmabuf);
1954 +
1955 +static struct platform_driver bcm2835_vcsm_cma_driver = {
1956 + .probe = bcm2835_vc_sm_cma_probe,
1957 + .remove = bcm2835_vc_sm_cma_remove,
1958 + .driver = {
1959 + .name = DEVICE_NAME,
1960 + .owner = THIS_MODULE,
1961 + },
1962 +};
1963 +
1964 +module_platform_driver(bcm2835_vcsm_cma_driver);
1965 +
1966 +MODULE_AUTHOR("Dave Stevenson");
1967 +MODULE_DESCRIPTION("VideoCore CMA Shared Memory Driver");
1968 +MODULE_LICENSE("GPL v2");
1969 +MODULE_ALIAS("platform:vcsm-cma");
1970 --- /dev/null
1971 +++ b/drivers/staging/vc04_services/vc-sm-cma/vc_sm.h
1972 @@ -0,0 +1,84 @@
1973 +/* SPDX-License-Identifier: GPL-2.0 */
1974 +
1975 +/*
1976 + * VideoCore Shared Memory driver using CMA.
1977 + *
1978 + * Copyright: 2018, Raspberry Pi (Trading) Ltd
1979 + *
1980 + */
1981 +
1982 +#ifndef VC_SM_H
1983 +#define VC_SM_H
1984 +
1985 +#include <linux/device.h>
1986 +#include <linux/dma-direction.h>
1987 +#include <linux/kref.h>
1988 +#include <linux/mm_types.h>
1989 +#include <linux/mutex.h>
1990 +#include <linux/rbtree.h>
1991 +#include <linux/sched.h>
1992 +#include <linux/shrinker.h>
1993 +#include <linux/types.h>
1994 +#include <linux/miscdevice.h>
1995 +
1996 +#define VC_SM_MAX_NAME_LEN 32
1997 +
1998 +enum vc_sm_vpu_mapping_state {
1999 + VPU_NOT_MAPPED,
2000 + VPU_MAPPED,
2001 + VPU_UNMAPPING
2002 +};
2003 +
2004 +struct vc_sm_alloc_data {
2005 + unsigned long num_pages;
2006 + void *priv_virt;
2007 + struct sg_table *sg_table;
2008 +};
2009 +
2010 +struct vc_sm_imported {
2011 + struct dma_buf *dma_buf;
2012 + struct dma_buf_attachment *attach;
2013 + struct sg_table *sgt;
2014 +};
2015 +
2016 +struct vc_sm_buffer {
2017 + struct list_head global_buffer_list; /* Global list of buffers. */
2018 +
2019 + /* Index in the kernel_id idr so that we can find the
2020 + * mmal_msg_context again when servicing the VCHI reply.
2021 + */
2022 + int kernel_id;
2023 +
2024 + size_t size;
2025 +
2026 + /* Lock over all the following state for this buffer */
2027 + struct mutex lock;
2028 + struct list_head attachments;
2029 +
2030 + char name[VC_SM_MAX_NAME_LEN];
2031 +
2032 + int in_use:1; /* Kernel is still using this resource */
2033 + int imported:1; /* Imported dmabuf */
2034 +
2035 + enum vc_sm_vpu_mapping_state vpu_state;
2036 + u32 vc_handle; /* VideoCore handle for this buffer */
2037 + int vpu_allocated; /*
2038 + * The VPU made this allocation. Release the
2039 + * local dma_buf when the VPU releases the
2040 + * resource.
2041 + */
2042 +
2043 + /* DMABUF related fields */
2044 + struct dma_buf *dma_buf;
2045 + dma_addr_t dma_addr;
2046 + void *cookie;
2047 +
2048 + struct vc_sm_privdata_t *private;
2049 +
2050 + union {
2051 + struct vc_sm_alloc_data alloc;
2052 + struct vc_sm_imported import;
2053 + };
2054 +};
2055 +
2056 +#endif
2057 --- /dev/null
2058 +++ b/drivers/staging/vc04_services/vc-sm-cma/vc_sm_cma_vchi.c
2059 @@ -0,0 +1,505 @@
2060 +// SPDX-License-Identifier: GPL-2.0
2061 +/*
2062 + * VideoCore Shared Memory CMA allocator
2063 + *
2064 + * Copyright: 2018, Raspberry Pi (Trading) Ltd
2065 + * Copyright 2011-2012 Broadcom Corporation. All rights reserved.
2066 + *
2067 + * Based on vmcs_sm driver from Broadcom Corporation.
2068 + *
2069 + */
2070 +
2071 +/* ---- Include Files ----------------------------------------------------- */
2072 +#include <linux/completion.h>
2073 +#include <linux/kernel.h>
2074 +#include <linux/kthread.h>
2075 +#include <linux/list.h>
2076 +#include <linux/mutex.h>
2077 +#include <linux/semaphore.h>
2078 +#include <linux/slab.h>
2079 +#include <linux/types.h>
2080 +
2081 +#include "vc_sm_cma_vchi.h"
2082 +
2083 +#define VC_SM_VER 1
2084 +#define VC_SM_MIN_VER 0
2085 +
2086 +/* ---- Private Constants and Types -------------------------------------- */
2087 +
2088 +/* Command blocks come from a pool */
2089 +#define SM_MAX_NUM_CMD_RSP_BLKS 32
2090 +
2091 +/* The number of supported connections */
2092 +#define SM_MAX_NUM_CONNECTIONS 3
2093 +
2094 +struct sm_cmd_rsp_blk {
2095 + struct list_head head; /* To create lists */
2096 + /* To be signaled when the response is there */
2097 + struct completion cmplt;
2098 +
2099 + u32 id;
2100 + u16 length;
2101 +
2102 + u8 msg[VC_SM_MAX_MSG_LEN];
2103 +
2104 + uint32_t wait:1;
2105 + uint32_t sent:1;
2106 + uint32_t alloc:1;
2107 +
2108 +};
2109 +
2110 +struct sm_instance {
2111 + u32 num_connections;
2112 + unsigned int service_handle[SM_MAX_NUM_CONNECTIONS];
2113 + struct task_struct *io_thread;
2114 + struct completion io_cmplt;
2115 +
2116 + vpu_event_cb vpu_event;
2117 +
2118 + /* Mutex over the following lists */
2119 + struct mutex lock;
2120 + u32 trans_id;
2121 + struct list_head cmd_list;
2122 + struct list_head rsp_list;
2123 + struct list_head dead_list;
2124 +
2125 + struct sm_cmd_rsp_blk free_blk[SM_MAX_NUM_CMD_RSP_BLKS];
2126 +
2127 + /* Mutex over the free_list */
2128 + struct mutex free_lock;
2129 + struct list_head free_list;
2130 +
2131 + struct semaphore free_sema;
2132 +
2133 +};
2134 +
2135 +/* ---- Private Variables ------------------------------------------------ */
2136 +
2137 +/* ---- Private Function Prototypes -------------------------------------- */
2138 +
2139 +/* ---- Private Functions ------------------------------------------------ */
2140 +static int
2141 +bcm2835_vchi_msg_queue(unsigned int handle,
2142 + void *data,
2143 + unsigned int size)
2144 +{
2145 + return vchiq_queue_kernel_message(handle, data, size);
2146 +}
2147 +
2148 +static struct
2149 +sm_cmd_rsp_blk *vc_vchi_cmd_create(struct sm_instance *instance,
2150 + enum vc_sm_msg_type id, void *msg,
2151 + u32 size, int wait)
2152 +{
2153 + struct sm_cmd_rsp_blk *blk;
2154 + struct vc_sm_msg_hdr_t *hdr;
2155 +
2156 + if (down_interruptible(&instance->free_sema)) {
2157 + blk = kmalloc(sizeof(*blk), GFP_KERNEL);
2158 + if (!blk)
2159 + return NULL;
2160 +
2161 + blk->alloc = 1;
2162 + init_completion(&blk->cmplt);
2163 + } else {
2164 + mutex_lock(&instance->free_lock);
2165 + blk =
2166 + list_first_entry(&instance->free_list,
2167 + struct sm_cmd_rsp_blk, head);
2168 + list_del(&blk->head);
2169 + mutex_unlock(&instance->free_lock);
2170 + }
2171 +
2172 + blk->sent = 0;
2173 + blk->wait = wait;
2174 + blk->length = sizeof(*hdr) + size;
2175 +
2176 + hdr = (struct vc_sm_msg_hdr_t *)blk->msg;
2177 + hdr->type = id;
2178 + mutex_lock(&instance->lock);
2179 + instance->trans_id++;
2180 + /*
2181 + * Retain the top bit for identifying asynchronous events, or VPU cmds.
2182 + */
2183 + instance->trans_id &= ~0x80000000;
2184 + hdr->trans_id = instance->trans_id;
2185 + blk->id = instance->trans_id;
2186 + mutex_unlock(&instance->lock);
2187 +
2188 + if (size)
2189 + memcpy(hdr->body, msg, size);
2190 +
2191 + return blk;
2192 +}
2193 +
2194 +static void
2195 +vc_vchi_cmd_delete(struct sm_instance *instance, struct sm_cmd_rsp_blk *blk)
2196 +{
2197 + if (blk->alloc) {
2198 + kfree(blk);
2199 + return;
2200 + }
2201 +
2202 + mutex_lock(&instance->free_lock);
2203 + list_add(&blk->head, &instance->free_list);
2204 + mutex_unlock(&instance->free_lock);
2205 + up(&instance->free_sema);
2206 +}
2207 +
2208 +static void vc_sm_cma_vchi_rx_ack(struct sm_instance *instance,
2209 + struct sm_cmd_rsp_blk *cmd,
2210 + struct vc_sm_result_t *reply,
2211 + u32 reply_len)
2212 +{
2213 + mutex_lock(&instance->lock);
2214 + list_for_each_entry(cmd,
2215 + &instance->rsp_list,
2216 + head) {
2217 + if (cmd->id == reply->trans_id)
2218 + break;
2219 + }
2220 + mutex_unlock(&instance->lock);
2221 +
2222 + if (&cmd->head == &instance->rsp_list) {
2223 + //pr_debug("%s: received response %u, throw away...",
2224 + pr_err("%s: received response %u, throw away...",
2225 + __func__,
2226 + reply->trans_id);
2227 + } else if (reply_len > sizeof(cmd->msg)) {
2228 + pr_err("%s: reply too big (%u) %u, throw away...",
2229 + __func__, reply_len,
2230 + reply->trans_id);
2231 + } else {
2232 + memcpy(cmd->msg, reply,
2233 + reply_len);
2234 + complete(&cmd->cmplt);
2235 + }
2236 +}
2237 +
2238 +static int vc_sm_cma_vchi_videocore_io(void *arg)
2239 +{
2240 + struct sm_instance *instance = arg;
2241 + struct sm_cmd_rsp_blk *cmd = NULL, *cmd_tmp;
2242 + struct vc_sm_result_t *reply;
2243 + struct vchiq_header *header;
2244 + s32 status;
2245 + int svc_use = 1;
2246 +
2247 + while (1) {
2248 + if (svc_use)
2249 + vchiq_release_service(instance->service_handle[0]);
2250 + svc_use = 0;
2251 +
2252 + if (wait_for_completion_interruptible(&instance->io_cmplt))
2253 + continue;
2254 + vchiq_use_service(instance->service_handle[0]);
2255 + svc_use = 1;
2256 +
2257 + do {
2258 + /*
2259 + * Get new command and move it to response list
2260 + */
2261 + mutex_lock(&instance->lock);
2262 + if (list_empty(&instance->cmd_list)) {
2263 + /* no more commands to process */
2264 + mutex_unlock(&instance->lock);
2265 + break;
2266 + }
2267 + cmd = list_first_entry(&instance->cmd_list,
2268 + struct sm_cmd_rsp_blk, head);
2269 + list_move(&cmd->head, &instance->rsp_list);
2270 + cmd->sent = 1;
2271 + mutex_unlock(&instance->lock);
2272 + /* Send the command */
2273 + status =
2274 + bcm2835_vchi_msg_queue(instance->service_handle[0],
2275 + cmd->msg, cmd->length);
2276 + if (status) {
2277 + pr_err("%s: failed to queue message (%d)",
2278 + __func__, status);
2279 + }
2280 +
2281 + /* If no reply is needed then we're done */
2282 + if (!cmd->wait) {
2283 + mutex_lock(&instance->lock);
2284 + list_del(&cmd->head);
2285 + mutex_unlock(&instance->lock);
2286 + vc_vchi_cmd_delete(instance, cmd);
2287 + continue;
2288 + }
2289 +
2290 + if (status) {
2291 + complete(&cmd->cmplt);
2292 + continue;
2293 + }
2294 +
2295 + } while (1);
2296 +
2297 + while ((header = vchiq_msg_hold(instance->service_handle[0]))) {
2298 + reply = (struct vc_sm_result_t *)header->data;
2299 + if (reply->trans_id & 0x80000000) {
2300 + /* Async event or cmd from the VPU */
2301 + if (instance->vpu_event)
2302 + instance->vpu_event(instance, reply,
2303 + header->size);
2304 + } else {
2305 + vc_sm_cma_vchi_rx_ack(instance, cmd, reply,
2306 + header->size);
2307 + }
2308 +
2309 + vchiq_release_message(instance->service_handle[0],
2310 + header);
2311 + }
2312 +
2313 + /* Go through the dead list and free them */
2314 + mutex_lock(&instance->lock);
2315 + list_for_each_entry_safe(cmd, cmd_tmp, &instance->dead_list,
2316 + head) {
2317 + list_del(&cmd->head);
2318 + vc_vchi_cmd_delete(instance, cmd);
2319 + }
2320 + mutex_unlock(&instance->lock);
2321 + }
2322 +
2323 + return 0;
2324 +}
2325 +
2326 +static enum vchiq_status vc_sm_cma_vchi_callback(enum vchiq_reason reason,
2327 + struct vchiq_header *header,
2328 + unsigned int handle, void *userdata)
2329 +{
2330 + struct sm_instance *instance = vchiq_get_service_userdata(handle);
2331 +
2332 + switch (reason) {
2333 + case VCHIQ_MESSAGE_AVAILABLE:
2334 + vchiq_msg_queue_push(handle, header);
2335 + complete(&instance->io_cmplt);
2336 + break;
2337 +
2338 + case VCHIQ_SERVICE_CLOSED:
2339 + pr_info("%s: service CLOSED!!", __func__);
2340 + break;
2341 +
2342 + default:
2343 + break;
2344 + }
2345 +
2346 + return VCHIQ_SUCCESS;
2347 +}
2348 +
2349 +struct sm_instance *vc_sm_cma_vchi_init(struct vchiq_instance *vchiq_instance,
2350 + unsigned int num_connections,
2351 + vpu_event_cb vpu_event)
2352 +{
2353 + u32 i;
2354 + struct sm_instance *instance;
2355 + int status;
2356 +
2357 + pr_debug("%s: start", __func__);
2358 +
2359 + if (num_connections > SM_MAX_NUM_CONNECTIONS) {
2360 + pr_err("%s: unsupported number of connections %u (max=%u)",
2361 + __func__, num_connections, SM_MAX_NUM_CONNECTIONS);
2362 +
2363 + goto err_null;
2364 + }
2365 + /* Allocate memory for this instance */
2366 + instance = kzalloc(sizeof(*instance), GFP_KERNEL);
2367 +
2368 + /* Misc initialisations */
2369 + mutex_init(&instance->lock);
2370 + init_completion(&instance->io_cmplt);
2371 + INIT_LIST_HEAD(&instance->cmd_list);
2372 + INIT_LIST_HEAD(&instance->rsp_list);
2373 + INIT_LIST_HEAD(&instance->dead_list);
2374 + INIT_LIST_HEAD(&instance->free_list);
2375 + sema_init(&instance->free_sema, SM_MAX_NUM_CMD_RSP_BLKS);
2376 + mutex_init(&instance->free_lock);
2377 + for (i = 0; i < SM_MAX_NUM_CMD_RSP_BLKS; i++) {
2378 + init_completion(&instance->free_blk[i].cmplt);
2379 + list_add(&instance->free_blk[i].head, &instance->free_list);
2380 + }
2381 +
2382 + /* Open the VCHI service connections */
2383 + instance->num_connections = num_connections;
2384 + for (i = 0; i < num_connections; i++) {
2385 + struct vchiq_service_params_kernel params = {
2386 + .version = VC_SM_VER,
2387 + .version_min = VC_SM_MIN_VER,
2388 + .fourcc = VCHIQ_MAKE_FOURCC('S', 'M', 'E', 'M'),
2389 + .callback = vc_sm_cma_vchi_callback,
2390 + .userdata = instance,
2391 + };
2392 +
2393 + status = vchiq_open_service(vchiq_instance, &params,
2394 + &instance->service_handle[i]);
2395 + if (status) {
2396 + pr_err("%s: failed to open VCHI service (%d)",
2397 + __func__, status);
2398 +
2399 + goto err_close_services;
2400 + }
2401 + }
2402 + /* Create the thread which takes care of all io to/from videoocore. */
2403 + instance->io_thread = kthread_create(&vc_sm_cma_vchi_videocore_io,
2404 + (void *)instance, "SMIO");
2405 + if (!instance->io_thread) {
2406 + pr_err("%s: failed to create SMIO thread", __func__);
2407 +
2408 + goto err_close_services;
2409 + }
2410 + instance->vpu_event = vpu_event;
2411 + set_user_nice(instance->io_thread, -10);
2412 + wake_up_process(instance->io_thread);
2413 +
2414 + pr_debug("%s: success - instance %p", __func__, instance);
2415 + return instance;
2416 +
2417 +err_close_services:
2418 + for (i = 0; i < instance->num_connections; i++) {
2419 + if (instance->service_handle[i])
2420 + vchiq_close_service(instance->service_handle[i]);
2421 + }
2422 + kfree(instance);
2423 +err_null:
2424 + pr_debug("%s: FAILED", __func__);
2425 + return NULL;
2426 +}
2427 +
2428 +int vc_sm_cma_vchi_stop(struct sm_instance **handle)
2429 +{
2430 + struct sm_instance *instance;
2431 + u32 i;
2432 +
2433 + if (!handle) {
2434 + pr_err("%s: invalid pointer to handle %p", __func__, handle);
2435 + goto lock;
2436 + }
2437 +
2438 + if (!*handle) {
2439 + pr_err("%s: invalid handle %p", __func__, *handle);
2440 + goto lock;
2441 + }
2442 +
2443 + instance = *handle;
2444 +
2445 + /* Close all VCHI service connections */
2446 + for (i = 0; i < instance->num_connections; i++) {
2447 + vchiq_use_service(instance->service_handle[i]);
2448 + vchiq_close_service(instance->service_handle[i]);
2449 + }
2450 +
2451 + kfree(instance);
2452 +
2453 + *handle = NULL;
2454 + return 0;
2455 +
2456 +lock:
2457 + return -EINVAL;
2458 +}
2459 +
2460 +static int vc_sm_cma_vchi_send_msg(struct sm_instance *handle,
2461 + enum vc_sm_msg_type msg_id, void *msg,
2462 + u32 msg_size, void *result, u32 result_size,
2463 + u32 *cur_trans_id, u8 wait_reply)
2464 +{
2465 + int status = 0;
2466 + struct sm_instance *instance = handle;
2467 + struct sm_cmd_rsp_blk *cmd_blk;
2468 +
2469 + if (!handle) {
2470 + pr_err("%s: invalid handle", __func__);
2471 + return -EINVAL;
2472 + }
2473 + if (!msg) {
2474 + pr_err("%s: invalid msg pointer", __func__);
2475 + return -EINVAL;
2476 + }
2477 +
2478 + cmd_blk =
2479 + vc_vchi_cmd_create(instance, msg_id, msg, msg_size, wait_reply);
2480 + if (!cmd_blk) {
2481 + pr_err("[%s]: failed to allocate global tracking resource",
2482 + __func__);
2483 + return -ENOMEM;
2484 + }
2485 +
2486 + if (cur_trans_id)
2487 + *cur_trans_id = cmd_blk->id;
2488 +
2489 + mutex_lock(&instance->lock);
2490 + list_add_tail(&cmd_blk->head, &instance->cmd_list);
2491 + mutex_unlock(&instance->lock);
2492 + complete(&instance->io_cmplt);
2493 +
2494 + if (!wait_reply)
2495 + /* We're done */
2496 + return 0;
2497 +
2498 + /* Wait for the response */
2499 + if (wait_for_completion_interruptible(&cmd_blk->cmplt)) {
2500 + mutex_lock(&instance->lock);
2501 + if (!cmd_blk->sent) {
2502 + list_del(&cmd_blk->head);
2503 + mutex_unlock(&instance->lock);
2504 + vc_vchi_cmd_delete(instance, cmd_blk);
2505 + return -ENXIO;
2506 + }
2507 +
2508 + list_move(&cmd_blk->head, &instance->dead_list);
2509 + mutex_unlock(&instance->lock);
2510 + complete(&instance->io_cmplt);
2511 + return -EINTR; /* We're done */
2512 + }
2513 +
2514 + if (result && result_size) {
2515 + memcpy(result, cmd_blk->msg, result_size);
2516 + } else {
2517 + struct vc_sm_result_t *res =
2518 + (struct vc_sm_result_t *)cmd_blk->msg;
2519 + status = (res->success == 0) ? 0 : -ENXIO;
2520 + }
2521 +
2522 + mutex_lock(&instance->lock);
2523 + list_del(&cmd_blk->head);
2524 + mutex_unlock(&instance->lock);
2525 + vc_vchi_cmd_delete(instance, cmd_blk);
2526 + return status;
2527 +}
2528 +
2529 +int vc_sm_cma_vchi_free(struct sm_instance *handle, struct vc_sm_free_t *msg,
2530 + u32 *cur_trans_id)
2531 +{
2532 + return vc_sm_cma_vchi_send_msg(handle, VC_SM_MSG_TYPE_FREE,
2533 + msg, sizeof(*msg), 0, 0, cur_trans_id, 0);
2534 +}
2535 +
2536 +int vc_sm_cma_vchi_import(struct sm_instance *handle, struct vc_sm_import *msg,
2537 + struct vc_sm_import_result *result, u32 *cur_trans_id)
2538 +{
2539 + return vc_sm_cma_vchi_send_msg(handle, VC_SM_MSG_TYPE_IMPORT,
2540 + msg, sizeof(*msg), result, sizeof(*result),
2541 + cur_trans_id, 1);
2542 +}
2543 +
2544 +int vc_sm_cma_vchi_client_version(struct sm_instance *handle,
2545 + struct vc_sm_version *msg,
2546 + struct vc_sm_result_t *result,
2547 + u32 *cur_trans_id)
2548 +{
2549 + return vc_sm_cma_vchi_send_msg(handle, VC_SM_MSG_TYPE_CLIENT_VERSION,
2550 + //msg, sizeof(*msg), result, sizeof(*result),
2551 + //cur_trans_id, 1);
2552 + msg, sizeof(*msg), NULL, 0,
2553 + cur_trans_id, 0);
2554 +}
2555 +
2556 +int vc_sm_vchi_client_vc_mem_req_reply(struct sm_instance *handle,
2557 + struct vc_sm_vc_mem_request_result *msg,
2558 + uint32_t *cur_trans_id)
2559 +{
2560 + return vc_sm_cma_vchi_send_msg(handle,
2561 + VC_SM_MSG_TYPE_VC_MEM_REQUEST_REPLY,
2562 + msg, sizeof(*msg), 0, 0, cur_trans_id,
2563 + 0);
2564 +}
2565 --- /dev/null
2566 +++ b/drivers/staging/vc04_services/vc-sm-cma/vc_sm_cma_vchi.h
2567 @@ -0,0 +1,63 @@
2568 +/* SPDX-License-Identifier: GPL-2.0 */
2569 +
2570 +/*
2571 + * VideoCore Shared Memory CMA allocator
2572 + *
2573 + * Copyright: 2018, Raspberry Pi (Trading) Ltd
2574 + * Copyright 2011-2012 Broadcom Corporation. All rights reserved.
2575 + *
2576 + * Based on vmcs_sm driver from Broadcom Corporation.
2577 + *
2578 + */
2579 +
2580 +#ifndef __VC_SM_CMA_VCHI_H__INCLUDED__
2581 +#define __VC_SM_CMA_VCHI_H__INCLUDED__
2582 +
2583 +#include <linux/raspberrypi/vchiq.h>
2584 +
2585 +#include "vc_sm_defs.h"
2586 +
2587 +/*
2588 + * Forward declare.
2589 + */
2590 +struct sm_instance;
2591 +
2592 +typedef void (*vpu_event_cb)(struct sm_instance *instance,
2593 + struct vc_sm_result_t *reply, int reply_len);
2594 +
2595 +/*
2596 + * Initialize the shared memory service, opens up vchi connection to talk to it.
2597 + */
2598 +struct sm_instance *vc_sm_cma_vchi_init(struct vchiq_instance *vchi_instance,
2599 + unsigned int num_connections,
2600 + vpu_event_cb vpu_event);
2601 +
2602 +/*
2603 + * Terminates the shared memory service.
2604 + */
2605 +int vc_sm_cma_vchi_stop(struct sm_instance **handle);
2606 +
2607 +/*
2608 + * Ask the shared memory service to free up some memory that was previously
2609 + * allocated by the vc_sm_cma_vchi_alloc function call.
2610 + */
2611 +int vc_sm_cma_vchi_free(struct sm_instance *handle, struct vc_sm_free_t *msg,
2612 + u32 *cur_trans_id);
2613 +
2614 +/*
2615 + * Import a contiguous block of memory and wrap it in a GPU MEM_HANDLE_T.
2616 + */
2617 +int vc_sm_cma_vchi_import(struct sm_instance *handle, struct vc_sm_import *msg,
2618 + struct vc_sm_import_result *result,
2619 + u32 *cur_trans_id);
2620 +
2621 +int vc_sm_cma_vchi_client_version(struct sm_instance *handle,
2622 + struct vc_sm_version *msg,
2623 + struct vc_sm_result_t *result,
2624 + u32 *cur_trans_id);
2625 +
2626 +int vc_sm_vchi_client_vc_mem_req_reply(struct sm_instance *handle,
2627 + struct vc_sm_vc_mem_request_result *msg,
2628 + uint32_t *cur_trans_id);
2629 +
2630 +#endif /* __VC_SM_CMA_VCHI_H__INCLUDED__ */
2631 --- /dev/null
2632 +++ b/drivers/staging/vc04_services/vc-sm-cma/vc_sm_defs.h
2633 @@ -0,0 +1,297 @@
2634 +/* SPDX-License-Identifier: GPL-2.0 */
2635 +
2636 +/*
2637 + * VideoCore Shared Memory CMA allocator
2638 + *
2639 + * Copyright: 2018, Raspberry Pi (Trading) Ltd
2640 + *
2641 + * Based on vc_sm_defs.h from the vmcs_sm driver Copyright Broadcom Corporation.
2642 + * All IPC messages are copied across to this file, even if the vc-sm-cma
2643 + * driver is not currently using them.
2644 + *
2645 + ****************************************************************************
2646 + */
2647 +
2648 +#ifndef __VC_SM_DEFS_H__INCLUDED__
2649 +#define __VC_SM_DEFS_H__INCLUDED__
2650 +
2651 +/* Maximum message length */
2652 +#define VC_SM_MAX_MSG_LEN (sizeof(union vc_sm_msg_union_t) + \
2653 + sizeof(struct vc_sm_msg_hdr_t))
2654 +#define VC_SM_MAX_RSP_LEN (sizeof(union vc_sm_msg_union_t))
2655 +
2656 +/* Resource name maximum size */
2657 +#define VC_SM_RESOURCE_NAME 32
2658 +
2659 +/*
2660 + * Version to be reported to the VPU
2661 + * VPU assumes 0 (aka 1) which does not require the released callback, nor
2662 + * expect the client to handle VC_MEM_REQUESTS.
2663 + * Version 2 requires the released callback, and must support VC_MEM_REQUESTS.
2664 + */
2665 +#define VC_SM_PROTOCOL_VERSION 2
2666 +
2667 +enum vc_sm_msg_type {
2668 + /* Message types supported for HOST->VC direction */
2669 +
2670 + /* Allocate shared memory block */
2671 + VC_SM_MSG_TYPE_ALLOC,
2672 + /* Lock allocated shared memory block */
2673 + VC_SM_MSG_TYPE_LOCK,
2674 + /* Unlock allocated shared memory block */
2675 + VC_SM_MSG_TYPE_UNLOCK,
2676 + /* Unlock allocated shared memory block, do not answer command */
2677 + VC_SM_MSG_TYPE_UNLOCK_NOANS,
2678 + /* Free shared memory block */
2679 + VC_SM_MSG_TYPE_FREE,
2680 + /* Resize a shared memory block */
2681 + VC_SM_MSG_TYPE_RESIZE,
2682 + /* Walk the allocated shared memory block(s) */
2683 + VC_SM_MSG_TYPE_WALK_ALLOC,
2684 +
2685 + /* A previously applied action will need to be reverted */
2686 + VC_SM_MSG_TYPE_ACTION_CLEAN,
2687 +
2688 + /*
2689 + * Import a physical address and wrap into a MEM_HANDLE_T.
2690 + * Release with VC_SM_MSG_TYPE_FREE.
2691 + */
2692 + VC_SM_MSG_TYPE_IMPORT,
2693 + /*
2694 + *Tells VC the protocol version supported by this client.
2695 + * 2 supports the async/cmd messages from the VPU for final release
2696 + * of memory, and for VC allocations.
2697 + */
2698 + VC_SM_MSG_TYPE_CLIENT_VERSION,
2699 + /* Response to VC request for memory */
2700 + VC_SM_MSG_TYPE_VC_MEM_REQUEST_REPLY,
2701 +
2702 + /*
2703 + * Asynchronous/cmd messages supported for VC->HOST direction.
2704 + * Signalled by setting the top bit in vc_sm_result_t trans_id.
2705 + */
2706 +
2707 + /*
2708 + * VC has finished with an imported memory allocation.
2709 + * Release any Linux reference counts on the underlying block.
2710 + */
2711 + VC_SM_MSG_TYPE_RELEASED,
2712 + /* VC request for memory */
2713 + VC_SM_MSG_TYPE_VC_MEM_REQUEST,
2714 +
2715 + VC_SM_MSG_TYPE_MAX
2716 +};
2717 +
2718 +/* Type of memory to be allocated */
2719 +enum vc_sm_alloc_type_t {
2720 + VC_SM_ALLOC_CACHED,
2721 + VC_SM_ALLOC_NON_CACHED,
2722 +};
2723 +
2724 +/* Message header for all messages in HOST->VC direction */
2725 +struct vc_sm_msg_hdr_t {
2726 + u32 type;
2727 + u32 trans_id;
2728 + u8 body[0];
2729 +
2730 +};
2731 +
2732 +/* Request to allocate memory (HOST->VC) */
2733 +struct vc_sm_alloc_t {
2734 + /* type of memory to allocate */
2735 + enum vc_sm_alloc_type_t type;
2736 + /* byte amount of data to allocate per unit */
2737 + u32 base_unit;
2738 + /* number of unit to allocate */
2739 + u32 num_unit;
2740 + /* alignment to be applied on allocation */
2741 + u32 alignment;
2742 + /* identity of who allocated this block */
2743 + u32 allocator;
2744 + /* resource name (for easier tracking on vc side) */
2745 + char name[VC_SM_RESOURCE_NAME];
2746 +
2747 +};
2748 +
2749 +/* Result of a requested memory allocation (VC->HOST) */
2750 +struct vc_sm_alloc_result_t {
2751 + /* Transaction identifier */
2752 + u32 trans_id;
2753 +
2754 + /* Resource handle */
2755 + u32 res_handle;
2756 + /* Pointer to resource buffer */
2757 + u32 res_mem;
2758 + /* Resource base size (bytes) */
2759 + u32 res_base_size;
2760 + /* Resource number */
2761 + u32 res_num;
2762 +
2763 +};
2764 +
2765 +/* Request to free a previously allocated memory (HOST->VC) */
2766 +struct vc_sm_free_t {
2767 + /* Resource handle (returned from alloc) */
2768 + u32 res_handle;
2769 + /* Resource buffer (returned from alloc) */
2770 + u32 res_mem;
2771 +
2772 +};
2773 +
2774 +/* Request to lock a previously allocated memory (HOST->VC) */
2775 +struct vc_sm_lock_unlock_t {
2776 + /* Resource handle (returned from alloc) */
2777 + u32 res_handle;
2778 + /* Resource buffer (returned from alloc) */
2779 + u32 res_mem;
2780 +
2781 +};
2782 +
2783 +/* Request to resize a previously allocated memory (HOST->VC) */
2784 +struct vc_sm_resize_t {
2785 + /* Resource handle (returned from alloc) */
2786 + u32 res_handle;
2787 + /* Resource buffer (returned from alloc) */
2788 + u32 res_mem;
2789 + /* Resource *new* size requested (bytes) */
2790 + u32 res_new_size;
2791 +
2792 +};
2793 +
2794 +/* Result of a requested memory lock (VC->HOST) */
2795 +struct vc_sm_lock_result_t {
2796 + /* Transaction identifier */
2797 + u32 trans_id;
2798 +
2799 + /* Resource handle */
2800 + u32 res_handle;
2801 + /* Pointer to resource buffer */
2802 + u32 res_mem;
2803 + /*
2804 + * Pointer to former resource buffer if the memory
2805 + * was reallocated
2806 + */
2807 + u32 res_old_mem;
2808 +
2809 +};
2810 +
2811 +/* Generic result for a request (VC->HOST) */
2812 +struct vc_sm_result_t {
2813 + /* Transaction identifier */
2814 + u32 trans_id;
2815 +
2816 + s32 success;
2817 +
2818 +};
2819 +
2820 +/* Request to revert a previously applied action (HOST->VC) */
2821 +struct vc_sm_action_clean_t {
2822 + /* Action of interest */
2823 + enum vc_sm_msg_type res_action;
2824 + /* Transaction identifier for the action of interest */
2825 + u32 action_trans_id;
2826 +
2827 +};
2828 +
2829 +/* Request to remove all data associated with a given allocator (HOST->VC) */
2830 +struct vc_sm_free_all_t {
2831 + /* Allocator identifier */
2832 + u32 allocator;
2833 +};
2834 +
2835 +/* Request to import memory (HOST->VC) */
2836 +struct vc_sm_import {
2837 + /* type of memory to allocate */
2838 + enum vc_sm_alloc_type_t type;
2839 + /* pointer to the VC (ie physical) address of the allocated memory */
2840 + u32 addr;
2841 + /* size of buffer */
2842 + u32 size;
2843 + /* opaque handle returned in RELEASED messages */
2844 + u32 kernel_id;
2845 + /* Allocator identifier */
2846 + u32 allocator;
2847 + /* resource name (for easier tracking on vc side) */
2848 + char name[VC_SM_RESOURCE_NAME];
2849 +};
2850 +
2851 +/* Result of a requested memory import (VC->HOST) */
2852 +struct vc_sm_import_result {
2853 + /* Transaction identifier */
2854 + u32 trans_id;
2855 +
2856 + /* Resource handle */
2857 + u32 res_handle;
2858 +};
2859 +
2860 +/* Notification that VC has finished with an allocation (VC->HOST) */
2861 +struct vc_sm_released {
2862 + /* cmd type / trans_id */
2863 + u32 cmd;
2864 +
2865 + /* pointer to the VC (ie physical) address of the allocated memory */
2866 + u32 addr;
2867 + /* size of buffer */
2868 + u32 size;
2869 + /* opaque handle returned in RELEASED messages */
2870 + u32 kernel_id;
2871 + u32 vc_handle;
2872 +};
2873 +
2874 +/*
2875 + * Client informing VC as to the protocol version it supports.
2876 + * >=2 requires the released callback, and supports VC asking for memory.
2877 + * Failure means that the firmware doesn't support this call, and therefore the
2878 + * client should either fail, or NOT rely on getting the released callback.
2879 + */
2880 +struct vc_sm_version {
2881 + u32 version;
2882 +};
2883 +
2884 +/* Request FROM VideoCore for some memory */
2885 +struct vc_sm_vc_mem_request {
2886 + /* cmd type */
2887 + u32 cmd;
2888 +
2889 + /* trans_id (from VPU) */
2890 + u32 trans_id;
2891 + /* size of buffer */
2892 + u32 size;
2893 + /* alignment of buffer */
2894 + u32 align;
2895 + /* resource name (for easier tracking) */
2896 + char name[VC_SM_RESOURCE_NAME];
2897 + /* VPU handle for the resource */
2898 + u32 vc_handle;
2899 +};
2900 +
2901 +/* Response from the kernel to provide the VPU with some memory */
2902 +struct vc_sm_vc_mem_request_result {
2903 + /* Transaction identifier for the VPU */
2904 + u32 trans_id;
2905 + /* pointer to the physical address of the allocated memory */
2906 + u32 addr;
2907 + /* opaque handle returned in RELEASED messages */
2908 + u32 kernel_id;
2909 +};
2910 +
2911 +/* Union of ALL messages */
2912 +union vc_sm_msg_union_t {
2913 + struct vc_sm_alloc_t alloc;
2914 + struct vc_sm_alloc_result_t alloc_result;
2915 + struct vc_sm_free_t free;
2916 + struct vc_sm_lock_unlock_t lock_unlock;
2917 + struct vc_sm_action_clean_t action_clean;
2918 + struct vc_sm_resize_t resize;
2919 + struct vc_sm_lock_result_t lock_result;
2920 + struct vc_sm_result_t result;
2921 + struct vc_sm_free_all_t free_all;
2922 + struct vc_sm_import import;
2923 + struct vc_sm_import_result import_result;
2924 + struct vc_sm_version version;
2925 + struct vc_sm_released released;
2926 + struct vc_sm_vc_mem_request vc_request;
2927 + struct vc_sm_vc_mem_request_result vc_request_result;
2928 +};
2929 +
2930 +#endif /* __VC_SM_DEFS_H__INCLUDED__ */
2931 --- /dev/null
2932 +++ b/drivers/staging/vc04_services/vc-sm-cma/vc_sm_knl.h
2933 @@ -0,0 +1,28 @@
2934 +/* SPDX-License-Identifier: GPL-2.0 */
2935 +
2936 +/*
2937 + * VideoCore Shared Memory CMA allocator
2938 + *
2939 + * Copyright: 2018, Raspberry Pi (Trading) Ltd
2940 + *
2941 + * Based on vc_sm_defs.h from the vmcs_sm driver Copyright Broadcom Corporation.
2942 + *
2943 + */
2944 +
2945 +#ifndef __VC_SM_KNL_H__INCLUDED__
2946 +#define __VC_SM_KNL_H__INCLUDED__
2947 +
2948 +#if !defined(__KERNEL__)
2949 +#error "This interface is for kernel use only..."
2950 +#endif
2951 +
2952 +/* Free a previously allocated or imported shared memory handle and block. */
2953 +int vc_sm_cma_free(void *handle);
2954 +
2955 +/* Get an internal resource handle mapped from the external one. */
2956 +int vc_sm_cma_int_handle(void *handle);
2957 +
2958 +/* Import a block of memory into the GPU space. */
2959 +int vc_sm_cma_import_dmabuf(struct dma_buf *dmabuf, void **handle);
2960 +
2961 +#endif /* __VC_SM_KNL_H__INCLUDED__ */