2 * Copyright (C) 2010, 2013-2014, 2016 ARM Limited. All rights reserved.
4 * This program is free software and is provided to you under the terms of the GNU General Public License version 2
5 * as published by the Free Software Foundation, and any use by you of this program is subject to the terms of such GNU licence.
7 * A copy of the licence is included with the program, and can also be obtained from Free Software
8 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
12 * @file ump_ukk_wrappers.c
13 * Defines the wrapper functions which turn Linux IOCTL calls into _ukk_ calls for the reference implementation
17 #include <asm/uaccess.h> /* user space access */
20 #include "ump_uk_types.h"
22 #include "ump_kernel_common.h"
23 #include <linux/scatterlist.h>
24 #include "ump_kernel_interface_ref_drv.h"
25 #include "mali_osk_list.h"
27 extern struct device *ump_global_mdev;
30 * IOCTL operation; Allocate UMP memory
32 int ump_allocate_wrapper(u32 __user *argument, struct ump_session_data *session_data)
34 _ump_uk_allocate_s user_interaction;
35 _mali_osk_errcode_t err;
37 /* Sanity check input parameters */
38 if (NULL == argument || NULL == session_data) {
39 MSG_ERR(("NULL parameter in ump_ioctl_allocate()\n"));
43 /* Copy the user space memory to kernel space (so we safely can read it) */
44 if (0 != copy_from_user(&user_interaction, argument, sizeof(user_interaction))) {
45 MSG_ERR(("copy_from_user() in ump_ioctl_allocate()\n"));
49 user_interaction.ctx = (void *) session_data;
51 err = _ump_ukk_allocate(&user_interaction);
52 if (_MALI_OSK_ERR_OK != err) {
53 DBG_MSG(1, ("_ump_ukk_allocate() failed in ump_ioctl_allocate()\n"));
54 return ump_map_errcode(err);
56 user_interaction.ctx = NULL;
58 if (0 != copy_to_user(argument, &user_interaction, sizeof(user_interaction))) {
59 /* If the copy fails then we should release the memory. We can use the IOCTL release to accomplish this */
60 _ump_uk_release_s release_args;
62 MSG_ERR(("copy_to_user() failed in ump_ioctl_allocate()\n"));
64 release_args.ctx = (void *) session_data;
65 release_args.secure_id = user_interaction.secure_id;
67 err = _ump_ukk_release(&release_args);
68 if (_MALI_OSK_ERR_OK != err) {
69 MSG_ERR(("_ump_ukk_release() also failed when trying to release newly allocated memory in ump_ioctl_allocate()\n"));
75 return 0; /* success */
78 #ifdef CONFIG_DMA_SHARED_BUFFER
79 static ump_dd_handle get_ump_handle_from_dmabuf(struct ump_session_data *session_data,
80 struct dma_buf *dmabuf)
82 ump_session_memory_list_element *session_mem, *tmp;
83 struct dma_buf_attachment *attach;
84 ump_dd_handle ump_handle;
86 DEBUG_ASSERT_POINTER(session_data);
88 _mali_osk_mutex_wait(session_data->lock);
90 _MALI_OSK_LIST_FOREACHENTRY(session_mem, tmp,
91 &session_data->list_head_session_memory_list,
92 ump_session_memory_list_element, list) {
93 if (session_mem->mem->import_attach) {
94 attach = session_mem->mem->import_attach;
95 if (attach->dmabuf == dmabuf) {
96 _mali_osk_mutex_signal(session_data->lock);
97 ump_handle = (ump_dd_handle)session_mem->mem;
98 ump_random_mapping_get(device.secure_id_map, ump_dd_secure_id_get(ump_handle));
104 _mali_osk_mutex_signal(session_data->lock);
109 int ump_dmabuf_import_wrapper(u32 __user *argument,
110 struct ump_session_data *session_data)
112 ump_session_memory_list_element *session = NULL;
113 _ump_uk_dmabuf_s ump_dmabuf;
114 ump_dd_handle ump_handle;
115 ump_dd_physical_block *blocks = NULL;
116 struct dma_buf_attachment *attach = NULL;
117 struct dma_buf *dma_buf;
118 struct sg_table *sgt = NULL;
119 struct scatterlist *sgl;
123 /* Sanity check input parameters */
124 if (!argument || !session_data) {
125 MSG_ERR(("NULL parameter.\n"));
129 if (copy_from_user(&ump_dmabuf, argument,
130 sizeof(_ump_uk_dmabuf_s))) {
131 MSG_ERR(("copy_from_user() failed.\n"));
135 dma_buf = dma_buf_get(ump_dmabuf.fd);
137 return PTR_ERR(dma_buf);
140 * if already imported then increase a refcount to the ump descriptor
141 * and call dma_buf_put() and then go to found to return previous
144 ump_handle = get_ump_handle_from_dmabuf(session_data, dma_buf);
146 dma_buf_put(dma_buf);
150 attach = dma_buf_attach(dma_buf, ump_global_mdev);
151 if (IS_ERR(attach)) {
152 ret = PTR_ERR(attach);
153 goto err_dma_buf_put;
156 sgt = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
159 goto err_dma_buf_detach;
162 blocks = (ump_dd_physical_block *)_mali_osk_malloc(sizeof(ump_dd_physical_block) * sgt->nents);
164 DBG_MSG(1, ("Failed to allocate blocks.\n"));
166 goto err_dma_buf_unmap;
168 for_each_sg(sgt->sgl, sgl, sgt->nents, i) {
169 blocks[i].addr = sg_phys(sgl);
170 blocks[i].size = sg_dma_len(sgl);
174 * Initialize the session memory list element, and add it
175 * to the session object
177 session = _mali_osk_calloc(1, sizeof(*session));
179 DBG_MSG(1, ("Failed to allocate session.\n"));
184 ump_handle = ump_dd_handle_create_from_phys_blocks(blocks, i);
185 if (UMP_DD_HANDLE_INVALID == ump_handle) {
186 DBG_MSG(1, ("Failed to create ump handle.\n"));
188 goto err_free_session;
191 session->mem = (ump_dd_mem *)ump_handle;
192 session->mem->import_attach = attach;
193 session->mem->sgt = sgt;
195 _mali_osk_mutex_wait(session_data->lock);
196 _mali_osk_list_add(&(session->list),
197 &(session_data->list_head_session_memory_list));
198 _mali_osk_mutex_signal(session_data->lock);
200 _mali_osk_free(blocks);
203 ump_dmabuf.ctx = (void *)session_data;
204 ump_dmabuf.secure_id = ump_dd_secure_id_get(ump_handle);
205 ump_dmabuf.size = ump_dd_size_get(ump_handle);
207 if (copy_to_user(argument, &ump_dmabuf,
208 sizeof(_ump_uk_dmabuf_s))) {
209 MSG_ERR(("copy_to_user() failed.\n"));
211 goto err_release_ump_handle;
216 err_release_ump_handle:
217 ump_dd_reference_release(ump_handle);
219 _mali_osk_free(session);
221 _mali_osk_free(blocks);
223 dma_buf_unmap_attachment(attach, sgt, DMA_BIDIRECTIONAL);
225 dma_buf_detach(dma_buf, attach);
227 dma_buf_put(dma_buf);