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 <linux/uaccess.h>
13 #include <linux/uio.h>
14 #include "tee_private.h"
16 static void release_registered_pages(struct tee_shm *shm)
19 if (shm->flags & TEE_SHM_USER_MAPPED) {
20 unpin_user_pages(shm->pages, shm->num_pages);
24 for (n = 0; n < shm->num_pages; n++)
25 put_page(shm->pages[n]);
32 static void tee_shm_release(struct tee_device *teedev, struct tee_shm *shm)
34 if (shm->flags & TEE_SHM_POOL) {
35 struct tee_shm_pool_mgr *poolm;
37 if (shm->flags & TEE_SHM_DMA_BUF)
38 poolm = teedev->pool->dma_buf_mgr;
40 poolm = teedev->pool->private_mgr;
42 poolm->ops->free(poolm, shm);
43 } else if (shm->flags & TEE_SHM_REGISTER) {
44 int rc = teedev->desc->ops->shm_unregister(shm->ctx, shm);
47 dev_err(teedev->dev.parent,
48 "unregister shm %p failed: %d", shm, rc);
50 release_registered_pages(shm);
53 teedev_ctx_put(shm->ctx);
57 tee_device_put(teedev);
60 struct tee_shm *tee_shm_alloc(struct tee_context *ctx, size_t size, u32 flags)
62 struct tee_device *teedev = ctx->teedev;
63 struct tee_shm_pool_mgr *poolm = NULL;
68 if (!(flags & TEE_SHM_MAPPED)) {
69 dev_err(teedev->dev.parent,
70 "only mapped allocations supported\n");
71 return ERR_PTR(-EINVAL);
74 if ((flags & ~(TEE_SHM_MAPPED | TEE_SHM_DMA_BUF | TEE_SHM_PRIV))) {
75 dev_err(teedev->dev.parent, "invalid shm flags 0x%x", flags);
76 return ERR_PTR(-EINVAL);
79 if (!tee_device_get(teedev))
80 return ERR_PTR(-EINVAL);
83 /* teedev has been detached from driver */
84 ret = ERR_PTR(-EINVAL);
88 shm = kzalloc(sizeof(*shm), GFP_KERNEL);
90 ret = ERR_PTR(-ENOMEM);
94 refcount_set(&shm->refcount, 1);
95 shm->flags = flags | TEE_SHM_POOL;
97 if (flags & TEE_SHM_DMA_BUF)
98 poolm = teedev->pool->dma_buf_mgr;
100 poolm = teedev->pool->private_mgr;
102 rc = poolm->ops->alloc(poolm, shm, size);
108 if (flags & TEE_SHM_DMA_BUF) {
109 mutex_lock(&teedev->mutex);
110 shm->id = idr_alloc(&teedev->idr, shm, 1, 0, GFP_KERNEL);
111 mutex_unlock(&teedev->mutex);
113 ret = ERR_PTR(shm->id);
122 poolm->ops->free(poolm, shm);
126 tee_device_put(teedev);
129 EXPORT_SYMBOL_GPL(tee_shm_alloc);
132 * tee_shm_alloc_kernel_buf() - Allocate shared memory for kernel buffer
133 * @ctx: Context that allocates the shared memory
134 * @size: Requested size of shared memory
136 * The returned memory registered in secure world and is suitable to be
137 * passed as a memory buffer in parameter argument to
138 * tee_client_invoke_func(). The memory allocated is later freed with a
139 * call to tee_shm_free().
141 * @returns a pointer to 'struct tee_shm'
143 struct tee_shm *tee_shm_alloc_kernel_buf(struct tee_context *ctx, size_t size)
145 return tee_shm_alloc(ctx, size, TEE_SHM_MAPPED);
147 EXPORT_SYMBOL_GPL(tee_shm_alloc_kernel_buf);
149 struct tee_shm *tee_shm_register(struct tee_context *ctx, unsigned long addr,
150 size_t length, u32 flags)
152 struct tee_device *teedev = ctx->teedev;
153 const u32 req_user_flags = TEE_SHM_DMA_BUF | TEE_SHM_USER_MAPPED;
154 const u32 req_kernel_flags = TEE_SHM_DMA_BUF | TEE_SHM_KERNEL_MAPPED;
161 if (flags != req_user_flags && flags != req_kernel_flags)
162 return ERR_PTR(-ENOTSUPP);
164 if (!tee_device_get(teedev))
165 return ERR_PTR(-EINVAL);
167 if (!teedev->desc->ops->shm_register ||
168 !teedev->desc->ops->shm_unregister) {
169 tee_device_put(teedev);
170 return ERR_PTR(-ENOTSUPP);
175 shm = kzalloc(sizeof(*shm), GFP_KERNEL);
177 ret = ERR_PTR(-ENOMEM);
181 refcount_set(&shm->refcount, 1);
182 shm->flags = flags | TEE_SHM_REGISTER;
185 addr = untagged_addr(addr);
186 start = rounddown(addr, PAGE_SIZE);
187 shm->offset = addr - start;
189 num_pages = (roundup(addr + length, PAGE_SIZE) - start) / PAGE_SIZE;
190 shm->pages = kcalloc(num_pages, sizeof(*shm->pages), GFP_KERNEL);
192 ret = ERR_PTR(-ENOMEM);
196 if (flags & TEE_SHM_USER_MAPPED) {
197 rc = pin_user_pages_fast(start, num_pages, FOLL_WRITE,
203 kiov = kcalloc(num_pages, sizeof(*kiov), GFP_KERNEL);
205 ret = ERR_PTR(-ENOMEM);
209 for (i = 0; i < num_pages; i++) {
210 kiov[i].iov_base = (void *)(start + i * PAGE_SIZE);
211 kiov[i].iov_len = PAGE_SIZE;
214 rc = get_kernel_pages(kiov, num_pages, 0, 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);
246 mutex_lock(&teedev->mutex);
247 idr_remove(&teedev->idr, shm->id);
248 mutex_unlock(&teedev->mutex);
250 release_registered_pages(shm);
254 tee_device_put(teedev);
257 EXPORT_SYMBOL_GPL(tee_shm_register);
259 static int tee_shm_fop_release(struct inode *inode, struct file *filp)
261 tee_shm_put(filp->private_data);
265 static int tee_shm_fop_mmap(struct file *filp, struct vm_area_struct *vma)
267 struct tee_shm *shm = filp->private_data;
268 size_t size = vma->vm_end - vma->vm_start;
270 /* Refuse sharing shared memory provided by application */
271 if (shm->flags & TEE_SHM_USER_MAPPED)
274 /* check for overflowing the buffer's size */
275 if (vma->vm_pgoff + vma_pages(vma) > shm->size >> PAGE_SHIFT)
278 return remap_pfn_range(vma, vma->vm_start, shm->paddr >> PAGE_SHIFT,
279 size, vma->vm_page_prot);
282 static const struct file_operations tee_shm_fops = {
283 .owner = THIS_MODULE,
284 .release = tee_shm_fop_release,
285 .mmap = tee_shm_fop_mmap,
289 * tee_shm_get_fd() - Increase reference count and return file descriptor
290 * @shm: Shared memory handle
291 * @returns user space file descriptor to shared memory
293 int tee_shm_get_fd(struct tee_shm *shm)
297 if (!(shm->flags & TEE_SHM_DMA_BUF))
300 /* matched by tee_shm_put() in tee_shm_op_release() */
301 refcount_inc(&shm->refcount);
302 fd = anon_inode_getfd("tee_shm", &tee_shm_fops, shm, O_RDWR);
309 * tee_shm_free() - Free shared memory
310 * @shm: Handle to shared memory to free
312 void tee_shm_free(struct tee_shm *shm)
316 EXPORT_SYMBOL_GPL(tee_shm_free);
319 * tee_shm_va2pa() - Get physical address of a virtual address
320 * @shm: Shared memory handle
321 * @va: Virtual address to tranlsate
322 * @pa: Returned physical address
323 * @returns 0 on success and < 0 on failure
325 int tee_shm_va2pa(struct tee_shm *shm, void *va, phys_addr_t *pa)
327 if (!(shm->flags & TEE_SHM_MAPPED))
329 /* Check that we're in the range of the shm */
330 if ((char *)va < (char *)shm->kaddr)
332 if ((char *)va >= ((char *)shm->kaddr + shm->size))
335 return tee_shm_get_pa(
336 shm, (unsigned long)va - (unsigned long)shm->kaddr, pa);
338 EXPORT_SYMBOL_GPL(tee_shm_va2pa);
341 * tee_shm_pa2va() - Get virtual address of a physical address
342 * @shm: Shared memory handle
343 * @pa: Physical address to tranlsate
344 * @va: Returned virtual address
345 * @returns 0 on success and < 0 on failure
347 int tee_shm_pa2va(struct tee_shm *shm, phys_addr_t pa, void **va)
349 if (!(shm->flags & TEE_SHM_MAPPED))
351 /* Check that we're in the range of the shm */
354 if (pa >= (shm->paddr + shm->size))
358 void *v = tee_shm_get_va(shm, pa - shm->paddr);
366 EXPORT_SYMBOL_GPL(tee_shm_pa2va);
369 * tee_shm_get_va() - Get virtual address of a shared memory plus an offset
370 * @shm: Shared memory handle
371 * @offs: Offset from start of this shared memory
372 * @returns virtual address of the shared memory + offs if offs is within
373 * the bounds of this shared memory, else an ERR_PTR
375 void *tee_shm_get_va(struct tee_shm *shm, size_t offs)
377 if (!(shm->flags & TEE_SHM_MAPPED))
378 return ERR_PTR(-EINVAL);
379 if (offs >= shm->size)
380 return ERR_PTR(-EINVAL);
381 return (char *)shm->kaddr + offs;
383 EXPORT_SYMBOL_GPL(tee_shm_get_va);
386 * tee_shm_get_pa() - Get physical address of a shared memory plus an offset
387 * @shm: Shared memory handle
388 * @offs: Offset from start of this shared memory
389 * @pa: Physical address to return
390 * @returns 0 if offs is within the bounds of this shared memory, else an
393 int tee_shm_get_pa(struct tee_shm *shm, size_t offs, phys_addr_t *pa)
395 if (offs >= shm->size)
398 *pa = shm->paddr + offs;
401 EXPORT_SYMBOL_GPL(tee_shm_get_pa);
404 * tee_shm_get_from_id() - Find shared memory object and increase reference
406 * @ctx: Context owning the shared memory
407 * @id: Id of shared memory object
408 * @returns a pointer to 'struct tee_shm' on success or an ERR_PTR on failure
410 struct tee_shm *tee_shm_get_from_id(struct tee_context *ctx, int id)
412 struct tee_device *teedev;
416 return ERR_PTR(-EINVAL);
418 teedev = ctx->teedev;
419 mutex_lock(&teedev->mutex);
420 shm = idr_find(&teedev->idr, id);
422 * If the tee_shm was found in the IDR it must have a refcount
423 * larger than 0 due to the guarantee in tee_shm_put() below. So
424 * it's safe to use refcount_inc().
426 if (!shm || shm->ctx != ctx)
427 shm = ERR_PTR(-EINVAL);
429 refcount_inc(&shm->refcount);
430 mutex_unlock(&teedev->mutex);
433 EXPORT_SYMBOL_GPL(tee_shm_get_from_id);
436 * tee_shm_put() - Decrease reference count on a shared memory handle
437 * @shm: Shared memory handle
439 void tee_shm_put(struct tee_shm *shm)
441 struct tee_device *teedev = shm->ctx->teedev;
442 bool do_release = false;
444 mutex_lock(&teedev->mutex);
445 if (refcount_dec_and_test(&shm->refcount)) {
447 * refcount has reached 0, we must now remove it from the
448 * IDR before releasing the mutex. This will guarantee that
449 * the refcount_inc() in tee_shm_get_from_id() never starts
452 if (shm->flags & TEE_SHM_DMA_BUF)
453 idr_remove(&teedev->idr, shm->id);
456 mutex_unlock(&teedev->mutex);
459 tee_shm_release(teedev, shm);
461 EXPORT_SYMBOL_GPL(tee_shm_put);