xref: /OK3568_Linux_fs/kernel/drivers/gpu/arm/mali400/ump/linux/ump_ukk_ref_wrappers.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  * Copyright (C) 2010, 2013-2014, 2016-2017 ARM Limited. All rights reserved.
3  *
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.
6  *
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.
9  */
10 
11 /**
12  * @file ump_ukk_wrappers.c
13  * Defines the wrapper functions which turn Linux IOCTL calls into _ukk_ calls for the reference implementation
14  */
15 
16 
17 #include <linux/uaccess.h>           /* user space access */
18 
19 #include "ump_osk.h"
20 #include "ump_uk_types.h"
21 #include "ump_ukk.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"
26 
27 extern struct device *ump_global_mdev;
28 
29 /*
30  * IOCTL operation; Allocate UMP memory
31  */
ump_allocate_wrapper(u32 __user * argument,struct ump_session_data * session_data)32 int ump_allocate_wrapper(u32 __user *argument, struct ump_session_data   *session_data)
33 {
34 	_ump_uk_allocate_s user_interaction;
35 	_mali_osk_errcode_t err;
36 
37 	/* Sanity check input parameters */
38 	if (NULL == argument || NULL == session_data) {
39 		MSG_ERR(("NULL parameter in ump_ioctl_allocate()\n"));
40 		return -ENOTTY;
41 	}
42 
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"));
46 		return -EFAULT;
47 	}
48 
49 	user_interaction.ctx = (void *) session_data;
50 
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);
55 	}
56 	user_interaction.ctx = NULL;
57 
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;
61 
62 		MSG_ERR(("copy_to_user() failed in ump_ioctl_allocate()\n"));
63 
64 		release_args.ctx = (void *) session_data;
65 		release_args.secure_id = user_interaction.secure_id;
66 
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"));
70 		}
71 
72 		return -EFAULT;
73 	}
74 
75 	return 0; /* success */
76 }
77 
78 #ifdef CONFIG_DMA_SHARED_BUFFER
get_ump_handle_from_dmabuf(struct ump_session_data * session_data,struct dma_buf * dmabuf)79 static ump_dd_handle get_ump_handle_from_dmabuf(struct ump_session_data *session_data,
80 		struct dma_buf *dmabuf)
81 {
82 	ump_session_memory_list_element *session_mem, *tmp;
83 	struct dma_buf_attachment *attach;
84 	ump_dd_handle ump_handle;
85 
86 	DEBUG_ASSERT_POINTER(session_data);
87 
88 	_mali_osk_mutex_wait(session_data->lock);
89 
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));
99 				return ump_handle;
100 			}
101 		}
102 	}
103 
104 	_mali_osk_mutex_signal(session_data->lock);
105 
106 	return NULL;
107 }
108 
ump_dmabuf_import_wrapper(u32 __user * argument,struct ump_session_data * session_data)109 int ump_dmabuf_import_wrapper(u32 __user *argument,
110 			      struct ump_session_data  *session_data)
111 {
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;
120 	unsigned int i = 0;
121 	int ret = 0;
122 
123 	/* Sanity check input parameters */
124 	if (!argument || !session_data) {
125 		MSG_ERR(("NULL parameter.\n"));
126 		return -EINVAL;
127 	}
128 
129 	if (copy_from_user(&ump_dmabuf, argument,
130 			   sizeof(_ump_uk_dmabuf_s))) {
131 		MSG_ERR(("copy_from_user() failed.\n"));
132 		return -EFAULT;
133 	}
134 
135 	dma_buf = dma_buf_get(ump_dmabuf.fd);
136 	if (IS_ERR(dma_buf))
137 		return PTR_ERR(dma_buf);
138 
139 	/*
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
142 	 * ump secure id.
143 	 */
144 	ump_handle = get_ump_handle_from_dmabuf(session_data, dma_buf);
145 	if (ump_handle) {
146 		dma_buf_put(dma_buf);
147 		goto found;
148 	}
149 
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;
154 	}
155 
156 	sgt = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
157 	if (IS_ERR(sgt)) {
158 		ret = PTR_ERR(sgt);
159 		goto err_dma_buf_detach;
160 	}
161 
162 	blocks = (ump_dd_physical_block *)_mali_osk_malloc(sizeof(ump_dd_physical_block) * sgt->nents);
163 	if (!blocks) {
164 		DBG_MSG(1, ("Failed to allocate blocks.\n"));
165 		ret = -EFAULT;
166 		goto err_dma_buf_unmap;
167 	}
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);
171 	}
172 
173 	/*
174 	 * Initialize the session memory list element, and add it
175 	 * to the session object
176 	 */
177 	session = _mali_osk_calloc(1, sizeof(*session));
178 	if (!session) {
179 		DBG_MSG(1, ("Failed to allocate session.\n"));
180 		ret = -EFAULT;
181 		goto err_free_block;
182 	}
183 
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"));
187 		ret = -EFAULT;
188 		goto err_free_session;
189 	}
190 
191 	session->mem = (ump_dd_mem *)ump_handle;
192 	session->mem->import_attach = attach;
193 	session->mem->sgt = sgt;
194 
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);
199 
200 	_mali_osk_free(blocks);
201 
202 found:
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);
206 
207 	if (copy_to_user(argument, &ump_dmabuf,
208 			 sizeof(_ump_uk_dmabuf_s))) {
209 		MSG_ERR(("copy_to_user() failed.\n"));
210 		ret =  -EFAULT;
211 		goto err_release_ump_handle;
212 	}
213 
214 	return ret;
215 
216 err_release_ump_handle:
217 	ump_dd_reference_release(ump_handle);
218 err_free_session:
219 	_mali_osk_free(session);
220 err_free_block:
221 	_mali_osk_free(blocks);
222 err_dma_buf_unmap:
223 	dma_buf_unmap_attachment(attach, sgt, DMA_BIDIRECTIONAL);
224 err_dma_buf_detach:
225 	dma_buf_detach(dma_buf, attach);
226 err_dma_buf_put:
227 	dma_buf_put(dma_buf);
228 	return ret;
229 }
230 #endif
231