1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2015-2016, Linaro Limited
5 #include <linux/device.h>
6 #include <linux/dma-buf.h>
7 #include <linux/fdtable.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)
18 if (shm->flags & TEE_SHM_USER_MAPPED) {
19 unpin_user_pages(shm->pages, shm->num_pages);
23 for (n = 0; n < shm->num_pages; n++)
24 put_page(shm->pages[n]);
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;
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);
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);
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)
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;
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)
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;
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 | TEE_SHM_PRIV))) {
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);
129 /* teedev has been detached from driver */
130 ret = ERR_PTR(-EINVAL);
134 shm = kzalloc(sizeof(*shm), GFP_KERNEL);
136 ret = ERR_PTR(-ENOMEM);
140 shm->flags = flags | TEE_SHM_POOL;
142 if (flags & TEE_SHM_DMA_BUF)
143 poolm = teedev->pool->dma_buf_mgr;
145 poolm = teedev->pool->private_mgr;
147 rc = poolm->ops->alloc(poolm, shm, size);
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);
161 ret = ERR_PTR(shm->id);
165 exp_info.ops = &tee_shm_dma_buf_ops;
166 exp_info.size = shm->size;
167 exp_info.flags = O_RDWR;
170 shm->dmabuf = dma_buf_export(&exp_info);
171 if (IS_ERR(shm->dmabuf)) {
172 ret = ERR_CAST(shm->dmabuf);
181 if (flags & TEE_SHM_DMA_BUF) {
182 mutex_lock(&teedev->mutex);
183 idr_remove(&teedev->idr, shm->id);
184 mutex_unlock(&teedev->mutex);
187 poolm->ops->free(poolm, shm);
191 tee_device_put(teedev);
194 EXPORT_SYMBOL_GPL(tee_shm_alloc);
197 * tee_shm_alloc_kernel_buf() - Allocate shared memory for kernel buffer
198 * @ctx: Context that allocates the shared memory
199 * @size: Requested size of shared memory
201 * The returned memory registered in secure world and is suitable to be
202 * passed as a memory buffer in parameter argument to
203 * tee_client_invoke_func(). The memory allocated is later freed with a
204 * call to tee_shm_free().
206 * @returns a pointer to 'struct tee_shm'
208 struct tee_shm *tee_shm_alloc_kernel_buf(struct tee_context *ctx, size_t size)
210 return tee_shm_alloc(ctx, size, TEE_SHM_MAPPED);
212 EXPORT_SYMBOL_GPL(tee_shm_alloc_kernel_buf);
214 struct tee_shm *tee_shm_register(struct tee_context *ctx, unsigned long addr,
215 size_t length, u32 flags)
217 struct tee_device *teedev = ctx->teedev;
218 const u32 req_user_flags = TEE_SHM_DMA_BUF | TEE_SHM_USER_MAPPED;
219 const u32 req_kernel_flags = TEE_SHM_DMA_BUF | TEE_SHM_KERNEL_MAPPED;
226 if (flags != req_user_flags && flags != req_kernel_flags)
227 return ERR_PTR(-ENOTSUPP);
229 if (!tee_device_get(teedev))
230 return ERR_PTR(-EINVAL);
232 if (!teedev->desc->ops->shm_register ||
233 !teedev->desc->ops->shm_unregister) {
234 tee_device_put(teedev);
235 return ERR_PTR(-ENOTSUPP);
240 shm = kzalloc(sizeof(*shm), GFP_KERNEL);
242 ret = ERR_PTR(-ENOMEM);
246 shm->flags = flags | TEE_SHM_REGISTER;
249 addr = untagged_addr(addr);
250 start = rounddown(addr, PAGE_SIZE);
251 shm->offset = addr - start;
253 num_pages = (roundup(addr + length, PAGE_SIZE) - start) / PAGE_SIZE;
254 shm->pages = kcalloc(num_pages, sizeof(*shm->pages), GFP_KERNEL);
256 ret = ERR_PTR(-ENOMEM);
260 if (flags & TEE_SHM_USER_MAPPED) {
261 rc = pin_user_pages_fast(start, num_pages, FOLL_WRITE,
267 kiov = kcalloc(num_pages, sizeof(*kiov), GFP_KERNEL);
269 ret = ERR_PTR(-ENOMEM);
273 for (i = 0; i < num_pages; i++) {
274 kiov[i].iov_base = (void *)(start + i * PAGE_SIZE);
275 kiov[i].iov_len = PAGE_SIZE;
278 rc = get_kernel_pages(kiov, num_pages, 0, shm->pages);
283 if (rc != num_pages) {
290 mutex_lock(&teedev->mutex);
291 shm->id = idr_alloc(&teedev->idr, shm, 1, 0, GFP_KERNEL);
292 mutex_unlock(&teedev->mutex);
295 ret = ERR_PTR(shm->id);
299 rc = teedev->desc->ops->shm_register(ctx, shm, shm->pages,
300 shm->num_pages, start);
306 if (flags & TEE_SHM_DMA_BUF) {
307 DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
309 exp_info.ops = &tee_shm_dma_buf_ops;
310 exp_info.size = shm->size;
311 exp_info.flags = O_RDWR;
314 shm->dmabuf = dma_buf_export(&exp_info);
315 if (IS_ERR(shm->dmabuf)) {
316 ret = ERR_CAST(shm->dmabuf);
317 teedev->desc->ops->shm_unregister(ctx, shm);
326 mutex_lock(&teedev->mutex);
327 idr_remove(&teedev->idr, shm->id);
328 mutex_unlock(&teedev->mutex);
330 release_registered_pages(shm);
334 tee_device_put(teedev);
337 EXPORT_SYMBOL_GPL(tee_shm_register);
340 * tee_shm_get_fd() - Increase reference count and return file descriptor
341 * @shm: Shared memory handle
342 * @returns user space file descriptor to shared memory
344 int tee_shm_get_fd(struct tee_shm *shm)
348 if (!(shm->flags & TEE_SHM_DMA_BUF))
351 get_dma_buf(shm->dmabuf);
352 fd = dma_buf_fd(shm->dmabuf, O_CLOEXEC);
354 dma_buf_put(shm->dmabuf);
359 * tee_shm_free() - Free shared memory
360 * @shm: Handle to shared memory to free
362 void tee_shm_free(struct tee_shm *shm)
365 * dma_buf_put() decreases the dmabuf reference counter and will
366 * call tee_shm_release() when the last reference is gone.
368 * In the case of driver private memory we call tee_shm_release
369 * directly instead as it doesn't have a reference counter.
371 if (shm->flags & TEE_SHM_DMA_BUF)
372 dma_buf_put(shm->dmabuf);
374 tee_shm_release(shm);
376 EXPORT_SYMBOL_GPL(tee_shm_free);
379 * tee_shm_va2pa() - Get physical address of a virtual address
380 * @shm: Shared memory handle
381 * @va: Virtual address to tranlsate
382 * @pa: Returned physical address
383 * @returns 0 on success and < 0 on failure
385 int tee_shm_va2pa(struct tee_shm *shm, void *va, phys_addr_t *pa)
387 if (!(shm->flags & TEE_SHM_MAPPED))
389 /* Check that we're in the range of the shm */
390 if ((char *)va < (char *)shm->kaddr)
392 if ((char *)va >= ((char *)shm->kaddr + shm->size))
395 return tee_shm_get_pa(
396 shm, (unsigned long)va - (unsigned long)shm->kaddr, pa);
398 EXPORT_SYMBOL_GPL(tee_shm_va2pa);
401 * tee_shm_pa2va() - Get virtual address of a physical address
402 * @shm: Shared memory handle
403 * @pa: Physical address to tranlsate
404 * @va: Returned virtual address
405 * @returns 0 on success and < 0 on failure
407 int tee_shm_pa2va(struct tee_shm *shm, phys_addr_t pa, void **va)
409 if (!(shm->flags & TEE_SHM_MAPPED))
411 /* Check that we're in the range of the shm */
414 if (pa >= (shm->paddr + shm->size))
418 void *v = tee_shm_get_va(shm, pa - shm->paddr);
426 EXPORT_SYMBOL_GPL(tee_shm_pa2va);
429 * tee_shm_get_va() - Get virtual address of a shared memory plus an offset
430 * @shm: Shared memory handle
431 * @offs: Offset from start of this shared memory
432 * @returns virtual address of the shared memory + offs if offs is within
433 * the bounds of this shared memory, else an ERR_PTR
435 void *tee_shm_get_va(struct tee_shm *shm, size_t offs)
437 if (!(shm->flags & TEE_SHM_MAPPED))
438 return ERR_PTR(-EINVAL);
439 if (offs >= shm->size)
440 return ERR_PTR(-EINVAL);
441 return (char *)shm->kaddr + offs;
443 EXPORT_SYMBOL_GPL(tee_shm_get_va);
446 * tee_shm_get_pa() - Get physical address of a shared memory plus an offset
447 * @shm: Shared memory handle
448 * @offs: Offset from start of this shared memory
449 * @pa: Physical address to return
450 * @returns 0 if offs is within the bounds of this shared memory, else an
453 int tee_shm_get_pa(struct tee_shm *shm, size_t offs, phys_addr_t *pa)
455 if (offs >= shm->size)
458 *pa = shm->paddr + offs;
461 EXPORT_SYMBOL_GPL(tee_shm_get_pa);
464 * tee_shm_get_from_id() - Find shared memory object and increase reference
466 * @ctx: Context owning the shared memory
467 * @id: Id of shared memory object
468 * @returns a pointer to 'struct tee_shm' on success or an ERR_PTR on failure
470 struct tee_shm *tee_shm_get_from_id(struct tee_context *ctx, int id)
472 struct tee_device *teedev;
476 return ERR_PTR(-EINVAL);
478 teedev = ctx->teedev;
479 mutex_lock(&teedev->mutex);
480 shm = idr_find(&teedev->idr, id);
481 if (!shm || shm->ctx != ctx)
482 shm = ERR_PTR(-EINVAL);
483 else if (shm->flags & TEE_SHM_DMA_BUF)
484 get_dma_buf(shm->dmabuf);
485 mutex_unlock(&teedev->mutex);
488 EXPORT_SYMBOL_GPL(tee_shm_get_from_id);
491 * tee_shm_put() - Decrease reference count on a shared memory handle
492 * @shm: Shared memory handle
494 void tee_shm_put(struct tee_shm *shm)
496 if (shm->flags & TEE_SHM_DMA_BUF)
497 dma_buf_put(shm->dmabuf);
499 EXPORT_SYMBOL_GPL(tee_shm_put);