1 From b55553b491ccfb9fc7b9b3b399c4796bd802c812 Mon Sep 17 00:00:00 2001
2 From: Dave Stevenson <dave.stevenson@raspberrypi.org>
3 Date: Mon, 11 Mar 2019 16:35:23 +0000
4 Subject: [PATCH 626/773] staging: vc-sm-cma: Add in userspace allocation API
6 Replacing the functionality from the older vc-sm driver,
7 add in a userspace API that allows allocation of buffers,
8 and importing of dma-bufs.
9 The driver hands out dma-buf fds, therefore much of the
10 handling around lifespan and odd mmaps from the old driver
13 Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.org>
15 .../staging/vc04_services/vc-sm-cma/vc_sm.c | 371 ++++++++++++++++--
16 .../vc04_services/vc-sm-cma/vc_sm_cma.c | 3 +-
17 .../vc04_services/vc-sm-cma/vc_sm_cma.h | 2 +-
18 include/linux/broadcom/vc_sm_cma_ioctl.h | 87 ++++
19 4 files changed, 435 insertions(+), 28 deletions(-)
20 create mode 100644 include/linux/broadcom/vc_sm_cma_ioctl.h
22 --- a/drivers/staging/vc04_services/vc-sm-cma/vc_sm.c
23 +++ b/drivers/staging/vc04_services/vc-sm-cma/vc_sm.c
26 #include <linux/kernel.h>
27 #include <linux/list.h>
28 +#include <linux/miscdevice.h>
29 #include <linux/module.h>
31 #include <linux/of_device.h>
34 #include "vc_sm_cma.h"
35 #include "vc_sm_knl.h"
36 +#include <linux/broadcom/vc_sm_cma_ioctl.h>
38 /* ---- Private Constants and Types --------------------------------------- */
40 @@ -83,6 +85,8 @@ struct sm_pde_t {
42 struct platform_device *pdev;
44 + struct miscdevice misc_dev;
46 struct sm_instance *sm_handle; /* Handle for videocore service. */
49 @@ -346,7 +350,6 @@ static void vc_sm_release_resource(struc
52 mutex_unlock(&buffer->lock);
56 /* Create support for private data tracking. */
57 @@ -381,7 +384,7 @@ static struct sg_table *dup_sg_table(str
58 ret = sg_alloc_table(new_table, table->nents, GFP_KERNEL);
61 - return ERR_PTR(-ENOMEM);
62 + return ERR_PTR(ret);
65 new_sg = new_table->sgl;
66 @@ -417,7 +420,7 @@ static int vc_sm_dma_buf_attach(struct d
67 table = dup_sg_table(buf->sg_table);
71 + return PTR_ERR(table);
75 @@ -433,8 +436,8 @@ static int vc_sm_dma_buf_attach(struct d
79 -static void vc_sm_dma_buf_detatch(struct dma_buf *dmabuf,
80 - struct dma_buf_attachment *attachment)
81 +static void vc_sm_dma_buf_detach(struct dma_buf *dmabuf,
82 + struct dma_buf_attachment *attachment)
84 struct vc_sm_dma_buf_attachment *a = attachment->priv;
85 struct vc_sm_buffer *buf = dmabuf->priv;
86 @@ -544,6 +547,9 @@ static void vc_sm_dma_buf_release(struct
87 vc_sm_clean_up_dmabuf(buffer);
88 pr_debug("%s clean_up dmabuf done\n", __func__);
90 + /* buffer->lock will be destroyed by vc_sm_release_resource if finished
91 + * with, otherwise unlocked. Do NOT unlock here.
93 vc_sm_release_resource(buffer);
94 pr_debug("%s done\n", __func__);
96 @@ -613,7 +619,7 @@ static const struct dma_buf_ops dma_buf_
97 .mmap = vc_sm_dmabuf_mmap,
98 .release = vc_sm_dma_buf_release,
99 .attach = vc_sm_dma_buf_attach,
100 - .detach = vc_sm_dma_buf_detatch,
101 + .detach = vc_sm_dma_buf_detach,
102 .begin_cpu_access = vc_sm_dma_buf_begin_cpu_access,
103 .end_cpu_access = vc_sm_dma_buf_end_cpu_access,
104 .map = vc_sm_dma_buf_kmap,
105 @@ -762,6 +768,7 @@ static const struct dma_buf_ops dma_buf_
107 vc_sm_cma_import_dmabuf_internal(struct vc_sm_privdata_t *private,
108 struct dma_buf *dma_buf,
110 struct dma_buf **imported_buf)
112 DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
113 @@ -775,10 +782,15 @@ vc_sm_cma_import_dmabuf_internal(struct
116 /* Setup our allocation parameters */
117 - pr_debug("%s: importing dma_buf %p\n", __func__, dma_buf);
118 + pr_debug("%s: importing dma_buf %p/fd %d\n", __func__, dma_buf, fd);
120 - get_dma_buf(dma_buf);
123 + get_dma_buf(dma_buf);
125 + dma_buf = dma_buf_get(fd);
130 attach = dma_buf_attach(dma_buf, &sm_state->pdev->dev);
131 if (IS_ERR(attach)) {
132 @@ -921,6 +933,10 @@ static int vc_sm_cma_vpu_alloc(u32 size,
135 mutex_init(&buffer->lock);
136 + /* Acquire the mutex as vc_sm_release_resource will release it in the
139 + mutex_lock(&buffer->lock);
141 if (vc_sm_cma_buffer_allocate(sm_state->cma_heap, &buffer->alloc,
143 @@ -976,6 +992,8 @@ static int vc_sm_cma_vpu_alloc(u32 size,
145 vc_sm_add_resource(sm_state->vpu_allocs, buffer);
147 + mutex_unlock(&buffer->lock);
149 *ret_buffer = buffer;
152 @@ -1065,6 +1083,297 @@ vc_sm_vpu_event(struct sm_instance *inst
156 +/* Userspace handling */
158 + * Open the device. Creates a private state to help track all allocation
159 + * associated with this device.
161 +static int vc_sm_cma_open(struct inode *inode, struct file *file)
163 + /* Make sure the device was started properly. */
165 + pr_err("[%s]: invalid device\n", __func__);
169 + file->private_data = vc_sm_cma_create_priv_data(current->tgid);
170 + if (!file->private_data) {
171 + pr_err("[%s]: failed to create data tracker\n", __func__);
180 + * Close the vcsm-cma device.
181 + * All allocations are file descriptors to the dmabuf objects, so we will get
182 + * the clean up request on those as those are cleaned up.
184 +static int vc_sm_cma_release(struct inode *inode, struct file *file)
186 + struct vc_sm_privdata_t *file_data =
187 + (struct vc_sm_privdata_t *)file->private_data;
190 + /* Make sure the device was started properly. */
191 + if (!sm_state || !file_data) {
192 + pr_err("[%s]: invalid device\n", __func__);
197 + pr_debug("[%s]: using private data %p\n", __func__, file_data);
199 + /* Terminate the private data. */
207 + * Allocate a shared memory handle and block.
208 + * Allocation is from CMA, and then imported into the VPU mappings.
210 +int vc_sm_cma_ioctl_alloc(struct vc_sm_privdata_t *private,
211 + struct vc_sm_cma_ioctl_alloc *ioparam)
213 + DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
214 + struct vc_sm_buffer *buffer = NULL;
215 + struct vc_sm_import import = { 0 };
216 + struct vc_sm_import_result result = { 0 };
217 + struct dma_buf *dmabuf = NULL;
223 + aligned_size = PAGE_ALIGN(ioparam->size);
228 + /* Allocate local buffer to track this allocation. */
229 + buffer = kzalloc(sizeof(*buffer), GFP_KERNEL);
235 + if (vc_sm_cma_buffer_allocate(sm_state->cma_heap, &buffer->alloc,
237 + pr_err("[%s]: cma alloc of %d bytes failed\n",
238 + __func__, aligned_size);
242 + buffer->sg_table = buffer->alloc.sg_table;
244 + if (dma_map_sg(&sm_state->pdev->dev, buffer->sg_table->sgl,
245 + buffer->sg_table->nents, DMA_BIDIRECTIONAL) <= 0) {
246 + pr_err("[%s]: dma_map_sg failed\n", __func__);
251 + import.type = VC_SM_ALLOC_NON_CACHED;
252 + import.allocator = current->tgid;
254 + if (*ioparam->name)
255 + memcpy(import.name, ioparam->name, sizeof(import.name) - 1);
257 + memcpy(import.name, VC_SM_RESOURCE_NAME_DEFAULT,
258 + sizeof(VC_SM_RESOURCE_NAME_DEFAULT));
260 + mutex_init(&buffer->lock);
261 + INIT_LIST_HEAD(&buffer->attachments);
262 + memcpy(buffer->name, import.name,
263 + min(sizeof(buffer->name), sizeof(import.name) - 1));
265 + exp_info.ops = &dma_buf_ops;
266 + exp_info.size = aligned_size;
267 + exp_info.flags = O_RDWR;
268 + exp_info.priv = buffer;
270 + dmabuf = dma_buf_export(&exp_info);
271 + if (IS_ERR(dmabuf)) {
272 + ret = PTR_ERR(dmabuf);
275 + buffer->dma_buf = dmabuf;
277 + import.addr = (uint32_t)sg_dma_address(buffer->sg_table->sgl);
278 + import.size = aligned_size;
279 + import.kernel_id = (uint32_t)buffer;
281 + /* Wrap it into a videocore buffer. */
282 + status = vc_sm_cma_vchi_import(sm_state->sm_handle, &import, &result,
283 + &sm_state->int_trans_id);
284 + if (status == -EINTR) {
285 + pr_debug("[%s]: requesting import memory action restart (trans_id: %u)\n",
286 + __func__, sm_state->int_trans_id);
287 + ret = -ERESTARTSYS;
288 + private->restart_sys = -EINTR;
289 + private->int_action = VC_SM_MSG_TYPE_IMPORT;
291 + } else if (status || !result.res_handle) {
292 + pr_err("[%s]: failed to import memory on videocore (status: %u, trans_id: %u)\n",
293 + __func__, status, sm_state->int_trans_id);
298 + /* Keep track of the buffer we created. */
299 + buffer->private = private;
300 + buffer->vc_handle = result.res_handle;
301 + buffer->size = import.size;
302 + buffer->dma_addr = import.addr;
303 + buffer->vpu_state = VPU_MAPPED;
304 + //buffer->res_cached = ioparam->cached;
306 + fd = dma_buf_fd(dmabuf, O_CLOEXEC);
310 + vc_sm_add_resource(private, buffer);
312 + pr_debug("[%s]: Added resource as fd %d, buffer %p, private %p, dma_addr %pad\n",
313 + __func__, fd, buffer, private, &buffer->dma_addr);
316 + ioparam->handle = fd;
317 + ioparam->vc_handle = buffer->vc_handle;
318 + ioparam->dma_addr = buffer->dma_addr;
323 + pr_err("[%s]: something failed - cleanup. ret %d\n", __func__,
326 + dma_buf_put(dmabuf);
331 +static long vc_sm_cma_ioctl(struct file *file, unsigned int cmd,
335 + unsigned int cmdnr = _IOC_NR(cmd);
336 + struct vc_sm_privdata_t *file_data =
337 + (struct vc_sm_privdata_t *)file->private_data;
339 + /* Validate we can work with this device. */
340 + if (!sm_state || !file_data) {
341 + pr_err("[%s]: invalid device\n", __func__);
345 + pr_debug("[%s]: cmd %x tgid %u, owner %u\n", __func__, cmdnr,
346 + current->tgid, file_data->pid);
348 + /* Action is a re-post of a previously interrupted action? */
349 + if (file_data->restart_sys == -EINTR) {
350 + struct vc_sm_action_clean_t action_clean;
352 + pr_debug("[%s]: clean up of action %u (trans_id: %u) following EINTR\n",
353 + __func__, file_data->int_action,
354 + file_data->int_trans_id);
356 + action_clean.res_action = file_data->int_action;
357 + action_clean.action_trans_id = file_data->int_trans_id;
359 + file_data->restart_sys = 0;
362 + /* Now process the command. */
364 + /* New memory allocation.
366 + case VC_SM_CMA_CMD_ALLOC:
368 + struct vc_sm_cma_ioctl_alloc ioparam;
370 + /* Get the parameter data. */
372 + (&ioparam, (void *)arg, sizeof(ioparam)) != 0) {
373 + pr_err("[%s]: failed to copy-from-user for cmd %x\n",
379 + ret = vc_sm_cma_ioctl_alloc(file_data, &ioparam);
381 + (copy_to_user((void *)arg, &ioparam,
382 + sizeof(ioparam)) != 0)) {
383 + /* FIXME: Release allocation */
384 + pr_err("[%s]: failed to copy-to-user for cmd %x\n",
391 + case VC_SM_CMA_CMD_IMPORT_DMABUF:
393 + struct vc_sm_cma_ioctl_import_dmabuf ioparam;
394 + struct dma_buf *new_dmabuf;
396 + /* Get the parameter data. */
398 + (&ioparam, (void *)arg, sizeof(ioparam)) != 0) {
399 + pr_err("[%s]: failed to copy-from-user for cmd %x\n",
405 + ret = vc_sm_cma_import_dmabuf_internal(file_data,
411 + struct vc_sm_buffer *buf = new_dmabuf->priv;
413 + ioparam.size = buf->size;
414 + ioparam.handle = dma_buf_fd(new_dmabuf,
416 + ioparam.vc_handle = buf->vc_handle;
417 + ioparam.dma_addr = buf->dma_addr;
419 + if (ioparam.handle < 0 ||
420 + (copy_to_user((void *)arg, &ioparam,
421 + sizeof(ioparam)) != 0)) {
422 + dma_buf_put(new_dmabuf);
423 + /* FIXME: Release allocation */
438 +/* Device operations that we managed in this driver. */
439 +static const struct file_operations vc_sm_ops = {
440 + .owner = THIS_MODULE,
441 + .unlocked_ioctl = vc_sm_cma_ioctl,
442 + .open = vc_sm_cma_open,
443 + .release = vc_sm_cma_release,
446 +/* Driver load/unload functions */
447 /* Videocore connected. */
448 static void vc_sm_connected_init(void)
450 @@ -1075,12 +1384,11 @@ static void vc_sm_connected_init(void)
452 pr_info("[%s]: start\n", __func__);
454 - if (vc_sm_cma_add_heaps(&sm_state->cma_heap) ||
455 - !sm_state->cma_heap) {
456 - pr_err("[%s]: failed to initialise CMA heaps\n",
457 + vc_sm_cma_add_heaps(&sm_state->cma_heap);
458 + if (!sm_state->cma_heap) {
459 + pr_err("[%s]: failed to initialise CMA heap\n",
467 @@ -1092,8 +1400,7 @@ static void vc_sm_connected_init(void)
468 pr_err("[%s]: failed to initialise VCHI instance (ret=%d)\n",
476 ret = vchi_connect(NULL, 0, vchi_instance);
477 @@ -1101,8 +1408,7 @@ static void vc_sm_connected_init(void)
478 pr_err("[%s]: failed to connect VCHI instance (ret=%d)\n",
486 /* Initialize an instance of the shared memory service. */
487 @@ -1112,8 +1418,7 @@ static void vc_sm_connected_init(void)
488 pr_err("[%s]: failed to initialize shared memory service\n",
496 /* Create a debug fs directory entry (root). */
497 @@ -1127,11 +1432,22 @@ static void vc_sm_connected_init(void)
499 INIT_LIST_HEAD(&sm_state->buffer_list);
501 + /* Create a shared memory device. */
502 + sm_state->misc_dev.minor = MISC_DYNAMIC_MINOR;
503 + sm_state->misc_dev.name = DEVICE_NAME;
504 + sm_state->misc_dev.fops = &vc_sm_ops;
505 + sm_state->misc_dev.parent = NULL;
506 + ret = misc_register(&sm_state->misc_dev);
508 + pr_err("vcsm-cma: failed to register misc device.\n");
509 + goto err_remove_debugfs;
512 sm_state->data_knl = vc_sm_cma_create_priv_data(0);
513 if (!sm_state->data_knl) {
514 pr_err("[%s]: failed to create kernel private data tracker\n",
516 - goto err_remove_shared_memory;
517 + goto err_remove_misc_dev;
521 @@ -1148,11 +1464,13 @@ static void vc_sm_connected_init(void)
522 pr_info("[%s]: installed successfully\n", __func__);
525 -err_remove_shared_memory:
526 +err_remove_misc_dev:
527 + misc_deregister(&sm_state->misc_dev);
529 debugfs_remove_recursive(sm_state->dir_root);
530 vc_sm_cma_vchi_stop(&sm_state->sm_handle);
532 - pr_info("[%s]: failed, ret %d\n", __func__, ret);
537 /* Driver loading. */
538 @@ -1184,6 +1502,8 @@ static int bcm2835_vc_sm_cma_remove(stru
540 pr_debug("[%s]: start\n", __func__);
542 + misc_deregister(&sm_state->misc_dev);
544 /* Remove all proc entries. */
545 debugfs_remove_recursive(sm_state->dir_root);
547 @@ -1202,6 +1522,7 @@ static int bcm2835_vc_sm_cma_remove(stru
551 +/* Kernel API calls */
552 /* Get an internal resource handle mapped from the external one. */
553 int vc_sm_cma_int_handle(void *handle)
555 @@ -1252,7 +1573,7 @@ int vc_sm_cma_import_dmabuf(struct dma_b
558 ret = vc_sm_cma_import_dmabuf_internal(sm_state->data_knl, src_dmabuf,
563 pr_debug("%s: imported to ptr %p\n", __func__, new_dma_buf);
564 --- a/drivers/staging/vc04_services/vc-sm-cma/vc_sm_cma.c
565 +++ b/drivers/staging/vc04_services/vc-sm-cma/vc_sm_cma.c
566 @@ -92,8 +92,7 @@ int __vc_sm_cma_add_heaps(struct cma *cm
570 -int vc_sm_cma_add_heaps(struct cma **cma_heap)
571 +void vc_sm_cma_add_heaps(struct cma **cma_heap)
573 cma_for_each_area(__vc_sm_cma_add_heaps, cma_heap);
576 --- a/drivers/staging/vc04_services/vc-sm-cma/vc_sm_cma.h
577 +++ b/drivers/staging/vc04_services/vc-sm-cma/vc_sm_cma.h
578 @@ -34,6 +34,6 @@ int vc_sm_cma_buffer_allocate(struct cma
580 void vc_sm_cma_buffer_free(struct vc_sm_cma_alloc_data *buffer);
582 -int vc_sm_cma_add_heaps(struct cma **cma_heap);
583 +void vc_sm_cma_add_heaps(struct cma **cma_heap);
587 +++ b/include/linux/broadcom/vc_sm_cma_ioctl.h
589 +/* SPDX-License-Identifier: GPL-2.0 */
592 + * Copyright 2019 Raspberry Pi (Trading) Ltd. All rights reserved.
594 + * Based on vmcs_sm_ioctl.h Copyright Broadcom Corporation.
597 +#ifndef __VC_SM_CMA_IOCTL_H
598 +#define __VC_SM_CMA_IOCTL_H
600 +/* ---- Include Files ---------------------------------------------------- */
602 +#if defined(__KERNEL__)
603 +#include <linux/types.h> /* Needed for standard types */
608 +#include <linux/ioctl.h>
610 +/* ---- Constants and Types ---------------------------------------------- */
612 +#define VC_SM_CMA_RESOURCE_NAME 32
613 +#define VC_SM_CMA_RESOURCE_NAME_DEFAULT "sm-host-resource"
615 +/* Type define used to create unique IOCTL number */
616 +#define VC_SM_CMA_MAGIC_TYPE 'J'
618 +/* IOCTL commands on /dev/vc-sm-cma */
619 +enum vc_sm_cma_cmd_e {
620 + VC_SM_CMA_CMD_ALLOC = 0x5A, /* Start at 0x5A arbitrarily */
622 + VC_SM_CMA_CMD_IMPORT_DMABUF,
624 + VC_SM_CMA_CMD_LAST /* Do not delete */
627 +/* Cache type supported, conveniently matches the user space definition in
630 +enum vc_sm_cma_cache_e {
631 + VC_SM_CMA_CACHE_NONE,
632 + VC_SM_CMA_CACHE_HOST,
633 + VC_SM_CMA_CACHE_VC,
634 + VC_SM_CMA_CACHE_BOTH,
637 +/* IOCTL Data structures */
638 +struct vc_sm_cma_ioctl_alloc {
639 + /* user -> kernel */
642 + __u32 cached; /* enum vc_sm_cma_cache_e */
644 + __u8 name[VC_SM_CMA_RESOURCE_NAME];
646 + /* kernel -> user */
652 +struct vc_sm_cma_ioctl_import_dmabuf {
653 + /* user -> kernel */
655 + __u32 cached; /* enum vc_sm_cma_cache_e */
656 + __u8 name[VC_SM_CMA_RESOURCE_NAME];
658 + /* kernel -> user */
667 +#define VC_SM_CMA_IOCTL_MEM_ALLOC\
668 + _IOR(VC_SM_CMA_MAGIC_TYPE, VC_SM_CMA_CMD_ALLOC,\
669 + struct vc_sm_cma_ioctl_alloc)
671 +#define VC_SM_CMA_IOCTL_MEM_IMPORT_DMABUF\
672 + _IOR(VC_SM_CMA_MAGIC_TYPE, VC_SM_CMA_CMD_IMPORT_DMABUF,\
673 + struct vc_sm_cma_ioctl_import_dmabuf)
675 +#endif /* __VC_SM_CMA_IOCTL_H */