Merge tag 'trace-v5.11-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git/rostedt...
[linux/fpc-iii.git] / drivers / tee / tee_shm.c
blob00472f5ce22e498b1dfd314bc353a60680446a56
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (c) 2015-2016, Linaro Limited
4 */
5 #include <linux/device.h>
6 #include <linux/dma-buf.h>
7 #include <linux/fdtable.h>
8 #include <linux/idr.h>
9 #include <linux/sched.h>
10 #include <linux/slab.h>
11 #include <linux/tee_drv.h>
12 #include <linux/uio.h>
13 #include "tee_private.h"
15 static void release_registered_pages(struct tee_shm *shm)
17 if (shm->pages) {
18 if (shm->flags & TEE_SHM_USER_MAPPED) {
19 unpin_user_pages(shm->pages, shm->num_pages);
20 } else {
21 size_t n;
23 for (n = 0; n < shm->num_pages; n++)
24 put_page(shm->pages[n]);
27 kfree(shm->pages);
31 static void tee_shm_release(struct tee_shm *shm)
33 struct tee_device *teedev = shm->ctx->teedev;
35 if (shm->flags & TEE_SHM_DMA_BUF) {
36 mutex_lock(&teedev->mutex);
37 idr_remove(&teedev->idr, shm->id);
38 mutex_unlock(&teedev->mutex);
41 if (shm->flags & TEE_SHM_POOL) {
42 struct tee_shm_pool_mgr *poolm;
44 if (shm->flags & TEE_SHM_DMA_BUF)
45 poolm = teedev->pool->dma_buf_mgr;
46 else
47 poolm = teedev->pool->private_mgr;
49 poolm->ops->free(poolm, shm);
50 } else if (shm->flags & TEE_SHM_REGISTER) {
51 int rc = teedev->desc->ops->shm_unregister(shm->ctx, shm);
53 if (rc)
54 dev_err(teedev->dev.parent,
55 "unregister shm %p failed: %d", shm, rc);
57 release_registered_pages(shm);
60 teedev_ctx_put(shm->ctx);
62 kfree(shm);
64 tee_device_put(teedev);
67 static struct sg_table *tee_shm_op_map_dma_buf(struct dma_buf_attachment
68 *attach, enum dma_data_direction dir)
70 return NULL;
73 static void tee_shm_op_unmap_dma_buf(struct dma_buf_attachment *attach,
74 struct sg_table *table,
75 enum dma_data_direction dir)
79 static void tee_shm_op_release(struct dma_buf *dmabuf)
81 struct tee_shm *shm = dmabuf->priv;
83 tee_shm_release(shm);
86 static int tee_shm_op_mmap(struct dma_buf *dmabuf, struct vm_area_struct *vma)
88 struct tee_shm *shm = dmabuf->priv;
89 size_t size = vma->vm_end - vma->vm_start;
91 /* Refuse sharing shared memory provided by application */
92 if (shm->flags & TEE_SHM_USER_MAPPED)
93 return -EINVAL;
95 return remap_pfn_range(vma, vma->vm_start, shm->paddr >> PAGE_SHIFT,
96 size, vma->vm_page_prot);
99 static const struct dma_buf_ops tee_shm_dma_buf_ops = {
100 .map_dma_buf = tee_shm_op_map_dma_buf,
101 .unmap_dma_buf = tee_shm_op_unmap_dma_buf,
102 .release = tee_shm_op_release,
103 .mmap = tee_shm_op_mmap,
106 struct tee_shm *tee_shm_alloc(struct tee_context *ctx, size_t size, u32 flags)
108 struct tee_device *teedev = ctx->teedev;
109 struct tee_shm_pool_mgr *poolm = NULL;
110 struct tee_shm *shm;
111 void *ret;
112 int rc;
114 if (!(flags & TEE_SHM_MAPPED)) {
115 dev_err(teedev->dev.parent,
116 "only mapped allocations supported\n");
117 return ERR_PTR(-EINVAL);
120 if ((flags & ~(TEE_SHM_MAPPED | TEE_SHM_DMA_BUF))) {
121 dev_err(teedev->dev.parent, "invalid shm flags 0x%x", flags);
122 return ERR_PTR(-EINVAL);
125 if (!tee_device_get(teedev))
126 return ERR_PTR(-EINVAL);
128 if (!teedev->pool) {
129 /* teedev has been detached from driver */
130 ret = ERR_PTR(-EINVAL);
131 goto err_dev_put;
134 shm = kzalloc(sizeof(*shm), GFP_KERNEL);
135 if (!shm) {
136 ret = ERR_PTR(-ENOMEM);
137 goto err_dev_put;
140 shm->flags = flags | TEE_SHM_POOL;
141 shm->ctx = ctx;
142 if (flags & TEE_SHM_DMA_BUF)
143 poolm = teedev->pool->dma_buf_mgr;
144 else
145 poolm = teedev->pool->private_mgr;
147 rc = poolm->ops->alloc(poolm, shm, size);
148 if (rc) {
149 ret = ERR_PTR(rc);
150 goto err_kfree;
154 if (flags & TEE_SHM_DMA_BUF) {
155 DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
157 mutex_lock(&teedev->mutex);
158 shm->id = idr_alloc(&teedev->idr, shm, 1, 0, GFP_KERNEL);
159 mutex_unlock(&teedev->mutex);
160 if (shm->id < 0) {
161 ret = ERR_PTR(shm->id);
162 goto err_pool_free;
165 exp_info.ops = &tee_shm_dma_buf_ops;
166 exp_info.size = shm->size;
167 exp_info.flags = O_RDWR;
168 exp_info.priv = shm;
170 shm->dmabuf = dma_buf_export(&exp_info);
171 if (IS_ERR(shm->dmabuf)) {
172 ret = ERR_CAST(shm->dmabuf);
173 goto err_rem;
177 teedev_ctx_get(ctx);
179 return shm;
180 err_rem:
181 if (flags & TEE_SHM_DMA_BUF) {
182 mutex_lock(&teedev->mutex);
183 idr_remove(&teedev->idr, shm->id);
184 mutex_unlock(&teedev->mutex);
186 err_pool_free:
187 poolm->ops->free(poolm, shm);
188 err_kfree:
189 kfree(shm);
190 err_dev_put:
191 tee_device_put(teedev);
192 return ret;
194 EXPORT_SYMBOL_GPL(tee_shm_alloc);
196 struct tee_shm *tee_shm_register(struct tee_context *ctx, unsigned long addr,
197 size_t length, u32 flags)
199 struct tee_device *teedev = ctx->teedev;
200 const u32 req_user_flags = TEE_SHM_DMA_BUF | TEE_SHM_USER_MAPPED;
201 const u32 req_kernel_flags = TEE_SHM_DMA_BUF | TEE_SHM_KERNEL_MAPPED;
202 struct tee_shm *shm;
203 void *ret;
204 int rc;
205 int num_pages;
206 unsigned long start;
208 if (flags != req_user_flags && flags != req_kernel_flags)
209 return ERR_PTR(-ENOTSUPP);
211 if (!tee_device_get(teedev))
212 return ERR_PTR(-EINVAL);
214 if (!teedev->desc->ops->shm_register ||
215 !teedev->desc->ops->shm_unregister) {
216 tee_device_put(teedev);
217 return ERR_PTR(-ENOTSUPP);
220 teedev_ctx_get(ctx);
222 shm = kzalloc(sizeof(*shm), GFP_KERNEL);
223 if (!shm) {
224 ret = ERR_PTR(-ENOMEM);
225 goto err;
228 shm->flags = flags | TEE_SHM_REGISTER;
229 shm->ctx = ctx;
230 shm->id = -1;
231 addr = untagged_addr(addr);
232 start = rounddown(addr, PAGE_SIZE);
233 shm->offset = addr - start;
234 shm->size = length;
235 num_pages = (roundup(addr + length, PAGE_SIZE) - start) / PAGE_SIZE;
236 shm->pages = kcalloc(num_pages, sizeof(*shm->pages), GFP_KERNEL);
237 if (!shm->pages) {
238 ret = ERR_PTR(-ENOMEM);
239 goto err;
242 if (flags & TEE_SHM_USER_MAPPED) {
243 rc = pin_user_pages_fast(start, num_pages, FOLL_WRITE,
244 shm->pages);
245 } else {
246 struct kvec *kiov;
247 int i;
249 kiov = kcalloc(num_pages, sizeof(*kiov), GFP_KERNEL);
250 if (!kiov) {
251 ret = ERR_PTR(-ENOMEM);
252 goto err;
255 for (i = 0; i < num_pages; i++) {
256 kiov[i].iov_base = (void *)(start + i * PAGE_SIZE);
257 kiov[i].iov_len = PAGE_SIZE;
260 rc = get_kernel_pages(kiov, num_pages, 0, shm->pages);
261 kfree(kiov);
263 if (rc > 0)
264 shm->num_pages = rc;
265 if (rc != num_pages) {
266 if (rc >= 0)
267 rc = -ENOMEM;
268 ret = ERR_PTR(rc);
269 goto err;
272 mutex_lock(&teedev->mutex);
273 shm->id = idr_alloc(&teedev->idr, shm, 1, 0, GFP_KERNEL);
274 mutex_unlock(&teedev->mutex);
276 if (shm->id < 0) {
277 ret = ERR_PTR(shm->id);
278 goto err;
281 rc = teedev->desc->ops->shm_register(ctx, shm, shm->pages,
282 shm->num_pages, start);
283 if (rc) {
284 ret = ERR_PTR(rc);
285 goto err;
288 if (flags & TEE_SHM_DMA_BUF) {
289 DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
291 exp_info.ops = &tee_shm_dma_buf_ops;
292 exp_info.size = shm->size;
293 exp_info.flags = O_RDWR;
294 exp_info.priv = shm;
296 shm->dmabuf = dma_buf_export(&exp_info);
297 if (IS_ERR(shm->dmabuf)) {
298 ret = ERR_CAST(shm->dmabuf);
299 teedev->desc->ops->shm_unregister(ctx, shm);
300 goto err;
304 return shm;
305 err:
306 if (shm) {
307 if (shm->id >= 0) {
308 mutex_lock(&teedev->mutex);
309 idr_remove(&teedev->idr, shm->id);
310 mutex_unlock(&teedev->mutex);
312 release_registered_pages(shm);
314 kfree(shm);
315 teedev_ctx_put(ctx);
316 tee_device_put(teedev);
317 return ret;
319 EXPORT_SYMBOL_GPL(tee_shm_register);
322 * tee_shm_get_fd() - Increase reference count and return file descriptor
323 * @shm: Shared memory handle
324 * @returns user space file descriptor to shared memory
326 int tee_shm_get_fd(struct tee_shm *shm)
328 int fd;
330 if (!(shm->flags & TEE_SHM_DMA_BUF))
331 return -EINVAL;
333 get_dma_buf(shm->dmabuf);
334 fd = dma_buf_fd(shm->dmabuf, O_CLOEXEC);
335 if (fd < 0)
336 dma_buf_put(shm->dmabuf);
337 return fd;
341 * tee_shm_free() - Free shared memory
342 * @shm: Handle to shared memory to free
344 void tee_shm_free(struct tee_shm *shm)
347 * dma_buf_put() decreases the dmabuf reference counter and will
348 * call tee_shm_release() when the last reference is gone.
350 * In the case of driver private memory we call tee_shm_release
351 * directly instead as it doesn't have a reference counter.
353 if (shm->flags & TEE_SHM_DMA_BUF)
354 dma_buf_put(shm->dmabuf);
355 else
356 tee_shm_release(shm);
358 EXPORT_SYMBOL_GPL(tee_shm_free);
361 * tee_shm_va2pa() - Get physical address of a virtual address
362 * @shm: Shared memory handle
363 * @va: Virtual address to tranlsate
364 * @pa: Returned physical address
365 * @returns 0 on success and < 0 on failure
367 int tee_shm_va2pa(struct tee_shm *shm, void *va, phys_addr_t *pa)
369 if (!(shm->flags & TEE_SHM_MAPPED))
370 return -EINVAL;
371 /* Check that we're in the range of the shm */
372 if ((char *)va < (char *)shm->kaddr)
373 return -EINVAL;
374 if ((char *)va >= ((char *)shm->kaddr + shm->size))
375 return -EINVAL;
377 return tee_shm_get_pa(
378 shm, (unsigned long)va - (unsigned long)shm->kaddr, pa);
380 EXPORT_SYMBOL_GPL(tee_shm_va2pa);
383 * tee_shm_pa2va() - Get virtual address of a physical address
384 * @shm: Shared memory handle
385 * @pa: Physical address to tranlsate
386 * @va: Returned virtual address
387 * @returns 0 on success and < 0 on failure
389 int tee_shm_pa2va(struct tee_shm *shm, phys_addr_t pa, void **va)
391 if (!(shm->flags & TEE_SHM_MAPPED))
392 return -EINVAL;
393 /* Check that we're in the range of the shm */
394 if (pa < shm->paddr)
395 return -EINVAL;
396 if (pa >= (shm->paddr + shm->size))
397 return -EINVAL;
399 if (va) {
400 void *v = tee_shm_get_va(shm, pa - shm->paddr);
402 if (IS_ERR(v))
403 return PTR_ERR(v);
404 *va = v;
406 return 0;
408 EXPORT_SYMBOL_GPL(tee_shm_pa2va);
411 * tee_shm_get_va() - Get virtual address of a shared memory plus an offset
412 * @shm: Shared memory handle
413 * @offs: Offset from start of this shared memory
414 * @returns virtual address of the shared memory + offs if offs is within
415 * the bounds of this shared memory, else an ERR_PTR
417 void *tee_shm_get_va(struct tee_shm *shm, size_t offs)
419 if (!(shm->flags & TEE_SHM_MAPPED))
420 return ERR_PTR(-EINVAL);
421 if (offs >= shm->size)
422 return ERR_PTR(-EINVAL);
423 return (char *)shm->kaddr + offs;
425 EXPORT_SYMBOL_GPL(tee_shm_get_va);
428 * tee_shm_get_pa() - Get physical address of a shared memory plus an offset
429 * @shm: Shared memory handle
430 * @offs: Offset from start of this shared memory
431 * @pa: Physical address to return
432 * @returns 0 if offs is within the bounds of this shared memory, else an
433 * error code.
435 int tee_shm_get_pa(struct tee_shm *shm, size_t offs, phys_addr_t *pa)
437 if (offs >= shm->size)
438 return -EINVAL;
439 if (pa)
440 *pa = shm->paddr + offs;
441 return 0;
443 EXPORT_SYMBOL_GPL(tee_shm_get_pa);
446 * tee_shm_get_from_id() - Find shared memory object and increase reference
447 * count
448 * @ctx: Context owning the shared memory
449 * @id: Id of shared memory object
450 * @returns a pointer to 'struct tee_shm' on success or an ERR_PTR on failure
452 struct tee_shm *tee_shm_get_from_id(struct tee_context *ctx, int id)
454 struct tee_device *teedev;
455 struct tee_shm *shm;
457 if (!ctx)
458 return ERR_PTR(-EINVAL);
460 teedev = ctx->teedev;
461 mutex_lock(&teedev->mutex);
462 shm = idr_find(&teedev->idr, id);
463 if (!shm || shm->ctx != ctx)
464 shm = ERR_PTR(-EINVAL);
465 else if (shm->flags & TEE_SHM_DMA_BUF)
466 get_dma_buf(shm->dmabuf);
467 mutex_unlock(&teedev->mutex);
468 return shm;
470 EXPORT_SYMBOL_GPL(tee_shm_get_from_id);
473 * tee_shm_put() - Decrease reference count on a shared memory handle
474 * @shm: Shared memory handle
476 void tee_shm_put(struct tee_shm *shm)
478 if (shm->flags & TEE_SHM_DMA_BUF)
479 dma_buf_put(shm->dmabuf);
481 EXPORT_SYMBOL_GPL(tee_shm_put);