1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2015-2017, 2019-2021 Linaro Limited
5 #include <linux/anon_inodes.h>
6 #include <linux/device.h>
9 #include <linux/sched.h>
10 #include <linux/slab.h>
11 #include <linux/tee_drv.h>
12 #include "tee_private.h"
14 static void tee_shm_release(struct tee_device *teedev, struct tee_shm *shm)
16 if (shm->flags & TEE_SHM_POOL) {
17 struct tee_shm_pool_mgr *poolm;
19 if (shm->flags & TEE_SHM_DMA_BUF)
20 poolm = teedev->pool->dma_buf_mgr;
22 poolm = teedev->pool->private_mgr;
24 poolm->ops->free(poolm, shm);
25 } else if (shm->flags & TEE_SHM_REGISTER) {
27 int rc = teedev->desc->ops->shm_unregister(shm->ctx, shm);
30 dev_err(teedev->dev.parent,
31 "unregister shm %p failed: %d", shm, rc);
33 for (n = 0; n < shm->num_pages; n++)
34 put_page(shm->pages[n]);
40 teedev_ctx_put(shm->ctx);
44 tee_device_put(teedev);
47 static struct tee_shm *__tee_shm_alloc(struct tee_context *ctx,
48 struct tee_device *teedev,
49 size_t size, u32 flags)
51 struct tee_shm_pool_mgr *poolm = NULL;
56 if (ctx && ctx->teedev != teedev) {
57 dev_err(teedev->dev.parent, "ctx and teedev mismatch\n");
58 return ERR_PTR(-EINVAL);
61 if (!(flags & TEE_SHM_MAPPED)) {
62 dev_err(teedev->dev.parent,
63 "only mapped allocations supported\n");
64 return ERR_PTR(-EINVAL);
67 if ((flags & ~(TEE_SHM_MAPPED | TEE_SHM_DMA_BUF | TEE_SHM_PRIV))) {
68 dev_err(teedev->dev.parent, "invalid shm flags 0x%x", flags);
69 return ERR_PTR(-EINVAL);
72 if (!tee_device_get(teedev))
73 return ERR_PTR(-EINVAL);
76 /* teedev has been detached from driver */
77 ret = ERR_PTR(-EINVAL);
81 shm = kzalloc(sizeof(*shm), GFP_KERNEL);
83 ret = ERR_PTR(-ENOMEM);
87 refcount_set(&shm->refcount, 1);
88 shm->flags = flags | TEE_SHM_POOL;
91 if (flags & TEE_SHM_DMA_BUF)
92 poolm = teedev->pool->dma_buf_mgr;
94 poolm = teedev->pool->private_mgr;
96 rc = poolm->ops->alloc(poolm, shm, size);
102 mutex_lock(&teedev->mutex);
103 shm->id = idr_alloc(&teedev->idr, shm, 1, 0, GFP_KERNEL);
104 mutex_unlock(&teedev->mutex);
106 ret = ERR_PTR(shm->id);
112 mutex_lock(&teedev->mutex);
113 list_add_tail(&shm->link, &ctx->list_shm);
114 mutex_unlock(&teedev->mutex);
119 poolm->ops->free(poolm, shm);
123 tee_device_put(teedev);
128 * tee_shm_alloc() - Allocate shared memory
129 * @ctx: Context that allocates the shared memory
130 * @size: Requested size of shared memory
131 * @flags: Flags setting properties for the requested shared memory.
133 * Memory allocated as global shared memory is automatically freed when the
134 * TEE file pointer is closed. The @flags field uses the bits defined by
135 * TEE_SHM_* in <linux/tee_drv.h>. TEE_SHM_MAPPED must currently always be
136 * set. If TEE_SHM_DMA_BUF global shared memory will be allocated and
137 * associated with a dma-buf handle, else driver private memory.
139 struct tee_shm *tee_shm_alloc(struct tee_context *ctx, size_t size, u32 flags)
141 return __tee_shm_alloc(ctx, ctx->teedev, size, flags);
143 EXPORT_SYMBOL_GPL(tee_shm_alloc);
145 struct tee_shm *tee_shm_priv_alloc(struct tee_device *teedev, size_t size)
147 return __tee_shm_alloc(NULL, teedev, size, TEE_SHM_MAPPED);
149 EXPORT_SYMBOL_GPL(tee_shm_priv_alloc);
152 * tee_shm_alloc_kernel_buf() - Allocate shared memory for kernel buffer
153 * @ctx: Context that allocates the shared memory
154 * @size: Requested size of shared memory
156 * The returned memory registered in secure world and is suitable to be
157 * passed as a memory buffer in parameter argument to
158 * tee_client_invoke_func(). The memory allocated is later freed with a
159 * call to tee_shm_free().
161 * @returns a pointer to 'struct tee_shm'
163 struct tee_shm *tee_shm_alloc_kernel_buf(struct tee_context *ctx, size_t size)
165 return tee_shm_alloc(ctx, size, TEE_SHM_MAPPED);
167 EXPORT_SYMBOL_GPL(tee_shm_alloc_kernel_buf);
169 struct tee_shm *tee_shm_register(struct tee_context *ctx, unsigned long addr,
170 size_t length, u32 flags)
172 struct tee_device *teedev = ctx->teedev;
173 const u32 req_flags = TEE_SHM_DMA_BUF | TEE_SHM_USER_MAPPED;
180 if (flags != req_flags)
181 return ERR_PTR(-ENOTSUPP);
183 if (!tee_device_get(teedev))
184 return ERR_PTR(-EINVAL);
186 if (!teedev->desc->ops->shm_register ||
187 !teedev->desc->ops->shm_unregister) {
188 tee_device_put(teedev);
189 return ERR_PTR(-ENOTSUPP);
194 shm = kzalloc(sizeof(*shm), GFP_KERNEL);
196 ret = ERR_PTR(-ENOMEM);
200 refcount_set(&shm->refcount, 1);
201 shm->flags = flags | TEE_SHM_REGISTER;
202 shm->teedev = teedev;
205 addr = untagged_addr(addr);
206 start = rounddown(addr, PAGE_SIZE);
207 shm->offset = addr - start;
209 num_pages = (roundup(addr + length, PAGE_SIZE) - start) / PAGE_SIZE;
210 shm->pages = kcalloc(num_pages, sizeof(*shm->pages), GFP_KERNEL);
212 ret = ERR_PTR(-ENOMEM);
216 rc = get_user_pages_fast(start, num_pages, FOLL_WRITE, shm->pages);
219 if (rc != num_pages) {
226 mutex_lock(&teedev->mutex);
227 shm->id = idr_alloc(&teedev->idr, shm, 1, 0, GFP_KERNEL);
228 mutex_unlock(&teedev->mutex);
231 ret = ERR_PTR(shm->id);
235 rc = teedev->desc->ops->shm_register(ctx, shm, shm->pages,
236 shm->num_pages, start);
242 mutex_lock(&teedev->mutex);
243 list_add_tail(&shm->link, &ctx->list_shm);
244 mutex_unlock(&teedev->mutex);
252 mutex_lock(&teedev->mutex);
253 idr_remove(&teedev->idr, shm->id);
254 mutex_unlock(&teedev->mutex);
257 for (n = 0; n < shm->num_pages; n++)
258 put_page(shm->pages[n]);
264 tee_device_put(teedev);
267 EXPORT_SYMBOL_GPL(tee_shm_register);
269 static int tee_shm_fop_release(struct inode *inode, struct file *filp)
271 tee_shm_put(filp->private_data);
275 static int tee_shm_fop_mmap(struct file *filp, struct vm_area_struct *vma)
277 struct tee_shm *shm = filp->private_data;
278 size_t size = vma->vm_end - vma->vm_start;
280 /* Refuse sharing shared memory provided by application */
281 if (shm->flags & TEE_SHM_USER_MAPPED)
284 /* check for overflowing the buffer's size */
285 if (vma->vm_pgoff + vma_pages(vma) > shm->size >> PAGE_SHIFT)
288 return remap_pfn_range(vma, vma->vm_start, shm->paddr >> PAGE_SHIFT,
289 size, vma->vm_page_prot);
292 static const struct file_operations tee_shm_fops = {
293 .owner = THIS_MODULE,
294 .release = tee_shm_fop_release,
295 .mmap = tee_shm_fop_mmap,
299 * tee_shm_get_fd() - Increase reference count and return file descriptor
300 * @shm: Shared memory handle
301 * @returns user space file descriptor to shared memory
303 int tee_shm_get_fd(struct tee_shm *shm)
307 if (!(shm->flags & TEE_SHM_DMA_BUF))
310 /* matched by tee_shm_put() in tee_shm_op_release() */
311 refcount_inc(&shm->refcount);
312 fd = anon_inode_getfd("tee_shm", &tee_shm_fops, shm, O_RDWR);
319 * tee_shm_free() - Free shared memory
320 * @shm: Handle to shared memory to free
322 void tee_shm_free(struct tee_shm *shm)
326 EXPORT_SYMBOL_GPL(tee_shm_free);
329 * tee_shm_va2pa() - Get physical address of a virtual address
330 * @shm: Shared memory handle
331 * @va: Virtual address to tranlsate
332 * @pa: Returned physical address
333 * @returns 0 on success and < 0 on failure
335 int tee_shm_va2pa(struct tee_shm *shm, void *va, phys_addr_t *pa)
337 if (!(shm->flags & TEE_SHM_MAPPED))
339 /* Check that we're in the range of the shm */
340 if ((char *)va < (char *)shm->kaddr)
342 if ((char *)va >= ((char *)shm->kaddr + shm->size))
345 return tee_shm_get_pa(
346 shm, (unsigned long)va - (unsigned long)shm->kaddr, pa);
348 EXPORT_SYMBOL_GPL(tee_shm_va2pa);
351 * tee_shm_pa2va() - Get virtual address of a physical address
352 * @shm: Shared memory handle
353 * @pa: Physical address to tranlsate
354 * @va: Returned virtual address
355 * @returns 0 on success and < 0 on failure
357 int tee_shm_pa2va(struct tee_shm *shm, phys_addr_t pa, void **va)
359 if (!(shm->flags & TEE_SHM_MAPPED))
361 /* Check that we're in the range of the shm */
364 if (pa >= (shm->paddr + shm->size))
368 void *v = tee_shm_get_va(shm, pa - shm->paddr);
376 EXPORT_SYMBOL_GPL(tee_shm_pa2va);
379 * tee_shm_get_va() - Get virtual address of a shared memory plus an offset
380 * @shm: Shared memory handle
381 * @offs: Offset from start of this shared memory
382 * @returns virtual address of the shared memory + offs if offs is within
383 * the bounds of this shared memory, else an ERR_PTR
385 void *tee_shm_get_va(struct tee_shm *shm, size_t offs)
387 if (!(shm->flags & TEE_SHM_MAPPED))
388 return ERR_PTR(-EINVAL);
389 if (offs >= shm->size)
390 return ERR_PTR(-EINVAL);
391 return (char *)shm->kaddr + offs;
393 EXPORT_SYMBOL_GPL(tee_shm_get_va);
396 * tee_shm_get_pa() - Get physical address of a shared memory plus an offset
397 * @shm: Shared memory handle
398 * @offs: Offset from start of this shared memory
399 * @pa: Physical address to return
400 * @returns 0 if offs is within the bounds of this shared memory, else an
403 int tee_shm_get_pa(struct tee_shm *shm, size_t offs, phys_addr_t *pa)
405 if (offs >= shm->size)
408 *pa = shm->paddr + offs;
411 EXPORT_SYMBOL_GPL(tee_shm_get_pa);
414 * tee_shm_get_from_id() - Find shared memory object and increase reference
416 * @ctx: Context owning the shared memory
417 * @id: Id of shared memory object
418 * @returns a pointer to 'struct tee_shm' on success or an ERR_PTR on failure
420 struct tee_shm *tee_shm_get_from_id(struct tee_context *ctx, int id)
422 struct tee_device *teedev;
426 return ERR_PTR(-EINVAL);
428 teedev = ctx->teedev;
429 mutex_lock(&teedev->mutex);
430 shm = idr_find(&teedev->idr, id);
432 * If the tee_shm was found in the IDR it must have a refcount
433 * larger than 0 due to the guarantee in tee_shm_put() below. So
434 * it's safe to use refcount_inc().
436 if (!shm || shm->ctx != ctx)
437 shm = ERR_PTR(-EINVAL);
439 refcount_inc(&shm->refcount);
440 mutex_unlock(&teedev->mutex);
443 EXPORT_SYMBOL_GPL(tee_shm_get_from_id);
446 * tee_shm_put() - Decrease reference count on a shared memory handle
447 * @shm: Shared memory handle
449 void tee_shm_put(struct tee_shm *shm)
451 struct tee_device *teedev = shm->teedev;
452 bool do_release = false;
454 mutex_lock(&teedev->mutex);
455 if (refcount_dec_and_test(&shm->refcount)) {
457 * refcount has reached 0, we must now remove it from the
458 * IDR before releasing the mutex. This will guarantee that
459 * the refcount_inc() in tee_shm_get_from_id() never starts
462 idr_remove(&teedev->idr, shm->id);
464 list_del(&shm->link);
467 mutex_unlock(&teedev->mutex);
470 tee_shm_release(teedev, shm);
472 EXPORT_SYMBOL_GPL(tee_shm_put);