Replace android makefile with one to build modules inline
[GitHub/LineageOS/G12/android_hardware_amlogic_kernel-modules_optee.git] / tee_shm.c
1 /*
2 * Copyright (c) 2015-2016, Linaro Limited
3 *
4 * This software is licensed under the terms of the GNU General Public
5 * License version 2, as published by the Free Software Foundation, and
6 * may be copied, distributed, and modified under those terms.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 */
14 #include <linux/device.h>
15 #include <linux/dma-buf.h>
16 #include <linux/fdtable.h>
17 #include <linux/idr.h>
18 #include <linux/sched.h>
19 #include <linux/slab.h>
20 #include <generated/uapi/linux/version.h>
21 #include "tee_drv.h"
22 #include "tee_private.h"
23
24 /* extra references appended to shm object for registered shared memory */
25 struct tee_shm_dmabuf_ref {
26 struct tee_shm shm;
27 struct dma_buf *dmabuf;
28 struct dma_buf_attachment *attach;
29 struct sg_table *sgt;
30 };
31
32 static void tee_shm_release(struct tee_shm *shm)
33 {
34 struct tee_device *teedev = shm->teedev;
35
36 mutex_lock(&teedev->mutex);
37 idr_remove(&teedev->idr, shm->id);
38 if (shm->ctx)
39 list_del(&shm->link);
40 mutex_unlock(&teedev->mutex);
41
42 if (shm->flags & TEE_SHM_EXT_DMA_BUF) {
43 struct tee_shm_dmabuf_ref *ref;
44
45 ref = container_of(shm, struct tee_shm_dmabuf_ref, shm);
46 dma_buf_unmap_attachment(ref->attach, ref->sgt,
47 DMA_BIDIRECTIONAL);
48 dma_buf_detach(shm->dmabuf, ref->attach);
49 dma_buf_put(ref->dmabuf);
50 } else {
51 struct tee_shm_pool_mgr *poolm;
52
53 if (shm->flags & TEE_SHM_DMA_BUF)
54 poolm = &teedev->pool->dma_buf_mgr;
55 else
56 poolm = &teedev->pool->private_mgr;
57
58 poolm->ops->free(poolm, shm);
59 }
60
61 kfree(shm);
62 tee_device_put(teedev);
63 }
64
65 static struct sg_table *tee_shm_op_map_dma_buf(struct dma_buf_attachment
66 *attach, enum dma_data_direction dir)
67 {
68 return NULL;
69 }
70
71 static void tee_shm_op_unmap_dma_buf(struct dma_buf_attachment *attach,
72 struct sg_table *table,
73 enum dma_data_direction dir)
74 {
75 }
76
77 static void tee_shm_op_release(struct dma_buf *dmabuf)
78 {
79 struct tee_shm *shm = dmabuf->priv;
80
81 tee_shm_release(shm);
82 }
83
84 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 19, 12)
85 static void *tee_shm_op_map(struct dma_buf *dmabuf, unsigned long pgnum)
86 {
87 return NULL;
88 }
89 #else
90 static void *tee_shm_op_kmap_atomic(struct dma_buf *dmabuf, unsigned long pgnum)
91 {
92 return NULL;
93 }
94
95 static void *tee_shm_op_kmap(struct dma_buf *dmabuf, unsigned long pgnum)
96 {
97 return NULL;
98 }
99 #endif
100
101 static int tee_shm_op_mmap(struct dma_buf *dmabuf, struct vm_area_struct *vma)
102 {
103 struct tee_shm *shm = dmabuf->priv;
104 size_t size = vma->vm_end - vma->vm_start;
105
106 return remap_pfn_range(vma, vma->vm_start, shm->paddr >> PAGE_SHIFT,
107 size, vma->vm_page_prot);
108 }
109
110 static struct dma_buf_ops tee_shm_dma_buf_ops = {
111 .map_dma_buf = tee_shm_op_map_dma_buf,
112 .unmap_dma_buf = tee_shm_op_unmap_dma_buf,
113 .release = tee_shm_op_release,
114 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 19, 12)
115 .map = tee_shm_op_map,
116 #else
117 .kmap_atomic = tee_shm_op_kmap_atomic,
118 .kmap = tee_shm_op_kmap,
119 #endif
120 .mmap = tee_shm_op_mmap,
121 };
122
123 /**
124 * tee_shm_alloc() - Allocate shared memory
125 * @ctx: Context that allocates the shared memory
126 * @size: Requested size of shared memory
127 * @flags: Flags setting properties for the requested shared memory.
128 *
129 * Memory allocated as global shared memory is automatically freed when the
130 * TEE file pointer is closed. The @flags field uses the bits defined by
131 * TEE_SHM_* in <linux/tee_drv.h>. TEE_SHM_MAPPED must currently always be
132 * set. If TEE_SHM_DMA_BUF global shared memory will be allocated and
133 * associated with a dma-buf handle, else driver private memory.
134 */
135 struct tee_shm *tee_shm_alloc(struct tee_context *ctx, size_t size, u32 flags)
136 {
137 struct tee_device *teedev = ctx->teedev;
138 struct tee_shm_pool_mgr *poolm = NULL;
139 struct tee_shm *shm;
140 void *ret;
141 int rc;
142
143 if (!(flags & TEE_SHM_MAPPED)) {
144 dev_err(teedev->dev.parent,
145 "only mapped allocations supported\n");
146 return ERR_PTR(-EINVAL);
147 }
148
149 if ((flags & ~(TEE_SHM_MAPPED | TEE_SHM_DMA_BUF))) {
150 dev_err(teedev->dev.parent, "invalid shm flags 0x%x", flags);
151 return ERR_PTR(-EINVAL);
152 }
153
154 if (!tee_device_get(teedev))
155 return ERR_PTR(-EINVAL);
156
157 if (!teedev->pool) {
158 /* teedev has been detached from driver */
159 ret = ERR_PTR(-EINVAL);
160 goto err_dev_put;
161 }
162
163 shm = kzalloc(sizeof(*shm), GFP_KERNEL);
164 if (!shm) {
165 ret = ERR_PTR(-ENOMEM);
166 goto err_dev_put;
167 }
168
169 shm->flags = flags;
170 shm->teedev = teedev;
171 shm->ctx = ctx;
172 if (flags & TEE_SHM_DMA_BUF)
173 poolm = &teedev->pool->dma_buf_mgr;
174 else
175 poolm = &teedev->pool->private_mgr;
176
177 rc = poolm->ops->alloc(poolm, shm, size);
178 if (rc) {
179 ret = ERR_PTR(rc);
180 goto err_kfree;
181 }
182
183 mutex_lock(&teedev->mutex);
184 shm->id = idr_alloc(&teedev->idr, shm, 1, 0, GFP_KERNEL);
185 mutex_unlock(&teedev->mutex);
186 if (shm->id < 0) {
187 ret = ERR_PTR(shm->id);
188 goto err_pool_free;
189 }
190
191 if (flags & TEE_SHM_DMA_BUF) {
192 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,4,16))
193 DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
194
195 exp_info.ops = &tee_shm_dma_buf_ops;
196 exp_info.size = shm->size;
197 exp_info.flags = O_RDWR;
198 exp_info.priv = shm;
199
200 shm->dmabuf = dma_buf_export(&exp_info);
201 #else
202 shm->dmabuf = dma_buf_export(shm, &tee_shm_dma_buf_ops,
203 shm->size, O_RDWR);
204 #endif
205 if (IS_ERR(shm->dmabuf)) {
206 ret = ERR_CAST(shm->dmabuf);
207 goto err_rem;
208 }
209 }
210 mutex_lock(&teedev->mutex);
211 list_add_tail(&shm->link, &ctx->list_shm);
212 mutex_unlock(&teedev->mutex);
213
214 return shm;
215 err_rem:
216 mutex_lock(&teedev->mutex);
217 idr_remove(&teedev->idr, shm->id);
218 mutex_unlock(&teedev->mutex);
219 err_pool_free:
220 poolm->ops->free(poolm, shm);
221 err_kfree:
222 kfree(shm);
223 err_dev_put:
224 tee_device_put(teedev);
225 return ret;
226 }
227 EXPORT_SYMBOL_GPL(tee_shm_alloc);
228
229 struct tee_shm *tee_shm_register_fd(struct tee_context *ctx, int fd)
230 {
231 struct tee_shm_dmabuf_ref *ref;
232 void *rc;
233 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,4,16))
234 DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
235 #endif
236
237 if (!tee_device_get(ctx->teedev))
238 return ERR_PTR(-EINVAL);
239
240 ref = kzalloc(sizeof(*ref), GFP_KERNEL);
241 if (!ref) {
242 rc = ERR_PTR(-ENOMEM);
243 goto err;
244 }
245
246 ref->shm.ctx = ctx;
247 ref->shm.teedev = ctx->teedev;
248 ref->shm.id = -1;
249
250 ref->dmabuf = dma_buf_get(fd);
251 if (!ref->dmabuf) {
252 rc = ERR_PTR(-EINVAL);
253 goto err;
254 }
255
256 ref->attach = dma_buf_attach(ref->dmabuf, &ref->shm.teedev->dev);
257 if (IS_ERR_OR_NULL(ref->attach)) {
258 rc = ERR_PTR(-EINVAL);
259 goto err;
260 }
261
262 ref->sgt = dma_buf_map_attachment(ref->attach, DMA_BIDIRECTIONAL);
263 if (IS_ERR_OR_NULL(ref->sgt)) {
264 rc = ERR_PTR(-EINVAL);
265 goto err;
266 }
267
268 if (sg_nents(ref->sgt->sgl) != 1) {
269 rc = ERR_PTR(-EINVAL);
270 goto err;
271 }
272
273 ref->shm.paddr = sg_dma_address(ref->sgt->sgl);
274 ref->shm.size = sg_dma_len(ref->sgt->sgl);
275 ref->shm.flags = TEE_SHM_DMA_BUF | TEE_SHM_EXT_DMA_BUF;
276
277 mutex_lock(&ref->shm.teedev->mutex);
278 ref->shm.id = idr_alloc(&ref->shm.teedev->idr, &ref->shm,
279 1, 0, GFP_KERNEL);
280 mutex_unlock(&ref->shm.teedev->mutex);
281 if (ref->shm.id < 0) {
282 rc = ERR_PTR(ref->shm.id);
283 goto err;
284 }
285 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,4,16))
286 /* export a dmabuf to later get a userland ref */
287 exp_info.ops = &tee_shm_dma_buf_ops;
288 exp_info.size = ref->shm.size;
289 exp_info.flags = O_RDWR;
290 exp_info.priv = &ref->shm;
291
292 ref->shm.dmabuf = dma_buf_export(&exp_info);
293 #else
294 ref->shm.dmabuf = dma_buf_export(&ref->shm, &tee_shm_dma_buf_ops,
295 ref->shm.size, O_RDWR);
296 #endif
297 if (IS_ERR(ref->shm.dmabuf)) {
298 rc = ERR_PTR(-EINVAL);
299 goto err;
300 }
301
302 mutex_lock(&ref->shm.teedev->mutex);
303 list_add_tail(&ref->shm.link, &ctx->list_shm);
304 mutex_unlock(&ref->shm.teedev->mutex);
305
306 return &ref->shm;
307
308 err:
309 if (ref) {
310 if (ref->shm.id >= 0) {
311 mutex_lock(&ctx->teedev->mutex);
312 idr_remove(&ctx->teedev->idr, ref->shm.id);
313 mutex_unlock(&ctx->teedev->mutex);
314 }
315 if (ref->sgt)
316 dma_buf_unmap_attachment(ref->attach, ref->sgt,
317 DMA_BIDIRECTIONAL);
318 if (ref->attach)
319 dma_buf_detach(ref->dmabuf, ref->attach);
320 if (ref->dmabuf)
321 dma_buf_put(ref->dmabuf);
322 }
323 kfree(ref);
324 tee_device_put(ctx->teedev);
325 return rc;
326 }
327 EXPORT_SYMBOL_GPL(tee_shm_register_fd);
328
329 /**
330 * tee_shm_get_fd() - Increase reference count and return file descriptor
331 * @shm: Shared memory handle
332 * @returns user space file descriptor to shared memory
333 */
334 int tee_shm_get_fd(struct tee_shm *shm)
335 {
336 int fd;
337
338 if (!(shm->flags & TEE_SHM_DMA_BUF))
339 return -EINVAL;
340
341 fd = dma_buf_fd(shm->dmabuf, O_CLOEXEC);
342 if (fd >= 0)
343 get_dma_buf(shm->dmabuf);
344 return fd;
345 }
346
347 /**
348 * tee_shm_free() - Free shared memory
349 * @shm: Handle to shared memory to free
350 */
351 void tee_shm_free(struct tee_shm *shm)
352 {
353 /*
354 * dma_buf_put() decreases the dmabuf reference counter and will
355 * call tee_shm_release() when the last reference is gone.
356 *
357 * In the case of driver private memory we call tee_shm_release
358 * directly instead as it doesn't have a reference counter.
359 */
360 if (shm->flags & TEE_SHM_DMA_BUF)
361 dma_buf_put(shm->dmabuf);
362 else
363 tee_shm_release(shm);
364 }
365 EXPORT_SYMBOL_GPL(tee_shm_free);
366
367 /**
368 * tee_shm_va2pa() - Get physical address of a virtual address
369 * @shm: Shared memory handle
370 * @va: Virtual address to tranlsate
371 * @pa: Returned physical address
372 * @returns 0 on success and < 0 on failure
373 */
374 int tee_shm_va2pa(struct tee_shm *shm, void *va, phys_addr_t *pa)
375 {
376 if (!(shm->flags & TEE_SHM_MAPPED))
377 return -EINVAL;
378 /* Check that we're in the range of the shm */
379 if ((char *)va < (char *)shm->kaddr)
380 return -EINVAL;
381 if ((char *)va >= ((char *)shm->kaddr + shm->size))
382 return -EINVAL;
383
384 return tee_shm_get_pa(
385 shm, (unsigned long)va - (unsigned long)shm->kaddr, pa);
386 }
387 EXPORT_SYMBOL_GPL(tee_shm_va2pa);
388
389 /**
390 * tee_shm_pa2va() - Get virtual address of a physical address
391 * @shm: Shared memory handle
392 * @pa: Physical address to tranlsate
393 * @va: Returned virtual address
394 * @returns 0 on success and < 0 on failure
395 */
396 int tee_shm_pa2va(struct tee_shm *shm, phys_addr_t pa, void **va)
397 {
398 if (!(shm->flags & TEE_SHM_MAPPED))
399 return -EINVAL;
400 /* Check that we're in the range of the shm */
401 if (pa < shm->paddr)
402 return -EINVAL;
403 if (pa >= (shm->paddr + shm->size))
404 return -EINVAL;
405
406 if (va) {
407 void *v = tee_shm_get_va(shm, pa - shm->paddr);
408
409 if (IS_ERR(v))
410 return PTR_ERR(v);
411 *va = v;
412 }
413 return 0;
414 }
415 EXPORT_SYMBOL_GPL(tee_shm_pa2va);
416
417 /**
418 * tee_shm_get_va() - Get virtual address of a shared memory plus an offset
419 * @shm: Shared memory handle
420 * @offs: Offset from start of this shared memory
421 * @returns virtual address of the shared memory + offs if offs is within
422 * the bounds of this shared memory, else an ERR_PTR
423 */
424 void *tee_shm_get_va(struct tee_shm *shm, size_t offs)
425 {
426 if (!(shm->flags & TEE_SHM_MAPPED))
427 return ERR_PTR(-EINVAL);
428 if (offs >= shm->size)
429 return ERR_PTR(-EINVAL);
430 return (char *)shm->kaddr + offs;
431 }
432 EXPORT_SYMBOL_GPL(tee_shm_get_va);
433
434 /**
435 * tee_shm_get_pa() - Get physical address of a shared memory plus an offset
436 * @shm: Shared memory handle
437 * @offs: Offset from start of this shared memory
438 * @pa: Physical address to return
439 * @returns 0 if offs is within the bounds of this shared memory, else an
440 * error code.
441 */
442 int tee_shm_get_pa(struct tee_shm *shm, size_t offs, phys_addr_t *pa)
443 {
444 if (offs >= shm->size)
445 return -EINVAL;
446 if (pa)
447 *pa = shm->paddr + offs;
448 return 0;
449 }
450 EXPORT_SYMBOL_GPL(tee_shm_get_pa);
451
452 /**
453 * tee_shm_get_from_id() - Find shared memory object and increase reference
454 * count
455 * @ctx: Context owning the shared memory
456 * @id: Id of shared memory object
457 * @returns a pointer to 'struct tee_shm' on success or an ERR_PTR on failure
458 */
459 struct tee_shm *tee_shm_get_from_id(struct tee_context *ctx, int id)
460 {
461 struct tee_device *teedev;
462 struct tee_shm *shm;
463
464 if (!ctx)
465 return ERR_PTR(-EINVAL);
466
467 teedev = ctx->teedev;
468 mutex_lock(&teedev->mutex);
469 shm = idr_find(&teedev->idr, id);
470 if (!shm || shm->ctx != ctx)
471 shm = ERR_PTR(-EINVAL);
472 else if (shm->flags & TEE_SHM_DMA_BUF)
473 get_dma_buf(shm->dmabuf);
474 mutex_unlock(&teedev->mutex);
475 return shm;
476 }
477 EXPORT_SYMBOL_GPL(tee_shm_get_from_id);
478
479 /**
480 * tee_shm_get_id() - Get id of a shared memory object
481 * @shm: Shared memory handle
482 * @returns id
483 */
484 int tee_shm_get_id(struct tee_shm *shm)
485 {
486 return shm->id;
487 }
488 EXPORT_SYMBOL_GPL(tee_shm_get_id);
489
490 /**
491 * tee_shm_put() - Decrease reference count on a shared memory handle
492 * @shm: Shared memory handle
493 */
494 void tee_shm_put(struct tee_shm *shm)
495 {
496 if (shm->flags & TEE_SHM_DMA_BUF)
497 dma_buf_put(shm->dmabuf);
498 }
499 EXPORT_SYMBOL_GPL(tee_shm_put);