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 "tee_private.h"
14 static void tee_shm_release(struct tee_shm *shm)
16 struct tee_device *teedev = shm->teedev;
18 mutex_lock(&teedev->mutex);
19 idr_remove(&teedev->idr, shm->id);
22 mutex_unlock(&teedev->mutex);
24 if (shm->flags & TEE_SHM_POOL) {
25 struct tee_shm_pool_mgr *poolm;
27 if (shm->flags & TEE_SHM_DMA_BUF)
28 poolm = teedev->pool->dma_buf_mgr;
30 poolm = teedev->pool->private_mgr;
32 poolm->ops->free(poolm, shm);
33 } else if (shm->flags & TEE_SHM_REGISTER) {
35 int rc = teedev->desc->ops->shm_unregister(shm->ctx, shm);
38 dev_err(teedev->dev.parent,
39 "unregister shm %p failed: %d", shm, rc);
41 for (n = 0; n < shm->num_pages; n++)
42 put_page(shm->pages[n]);
48 teedev_ctx_put(shm->ctx);
52 tee_device_put(teedev);
55 static struct sg_table *tee_shm_op_map_dma_buf(struct dma_buf_attachment
56 *attach, enum dma_data_direction dir)
61 static void tee_shm_op_unmap_dma_buf(struct dma_buf_attachment *attach,
62 struct sg_table *table,
63 enum dma_data_direction dir)
67 static void tee_shm_op_release(struct dma_buf *dmabuf)
69 struct tee_shm *shm = dmabuf->priv;
74 static int tee_shm_op_mmap(struct dma_buf *dmabuf, struct vm_area_struct *vma)
76 struct tee_shm *shm = dmabuf->priv;
77 size_t size = vma->vm_end - vma->vm_start;
79 /* Refuse sharing shared memory provided by application */
80 if (shm->flags & TEE_SHM_REGISTER)
83 return remap_pfn_range(vma, vma->vm_start, shm->paddr >> PAGE_SHIFT,
84 size, vma->vm_page_prot);
87 static const struct dma_buf_ops tee_shm_dma_buf_ops = {
88 .map_dma_buf = tee_shm_op_map_dma_buf,
89 .unmap_dma_buf = tee_shm_op_unmap_dma_buf,
90 .release = tee_shm_op_release,
91 .mmap = tee_shm_op_mmap,
94 static struct tee_shm *__tee_shm_alloc(struct tee_context *ctx,
95 struct tee_device *teedev,
96 size_t size, u32 flags)
98 struct tee_shm_pool_mgr *poolm = NULL;
103 if (ctx && ctx->teedev != teedev) {
104 dev_err(teedev->dev.parent, "ctx and teedev mismatch\n");
105 return ERR_PTR(-EINVAL);
108 if (!(flags & TEE_SHM_MAPPED)) {
109 dev_err(teedev->dev.parent,
110 "only mapped allocations supported\n");
111 return ERR_PTR(-EINVAL);
114 if ((flags & ~(TEE_SHM_MAPPED | TEE_SHM_DMA_BUF))) {
115 dev_err(teedev->dev.parent, "invalid shm flags 0x%x", flags);
116 return ERR_PTR(-EINVAL);
119 if (!tee_device_get(teedev))
120 return ERR_PTR(-EINVAL);
123 /* teedev has been detached from driver */
124 ret = ERR_PTR(-EINVAL);
128 shm = kzalloc(sizeof(*shm), GFP_KERNEL);
130 ret = ERR_PTR(-ENOMEM);
134 shm->flags = flags | TEE_SHM_POOL;
135 shm->teedev = teedev;
137 if (flags & TEE_SHM_DMA_BUF)
138 poolm = teedev->pool->dma_buf_mgr;
140 poolm = teedev->pool->private_mgr;
142 rc = poolm->ops->alloc(poolm, shm, size);
148 mutex_lock(&teedev->mutex);
149 shm->id = idr_alloc(&teedev->idr, shm, 1, 0, GFP_KERNEL);
150 mutex_unlock(&teedev->mutex);
152 ret = ERR_PTR(shm->id);
156 if (flags & TEE_SHM_DMA_BUF) {
157 DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
159 exp_info.ops = &tee_shm_dma_buf_ops;
160 exp_info.size = shm->size;
161 exp_info.flags = O_RDWR;
164 shm->dmabuf = dma_buf_export(&exp_info);
165 if (IS_ERR(shm->dmabuf)) {
166 ret = ERR_CAST(shm->dmabuf);
173 mutex_lock(&teedev->mutex);
174 list_add_tail(&shm->link, &ctx->list_shm);
175 mutex_unlock(&teedev->mutex);
180 mutex_lock(&teedev->mutex);
181 idr_remove(&teedev->idr, shm->id);
182 mutex_unlock(&teedev->mutex);
184 poolm->ops->free(poolm, shm);
188 tee_device_put(teedev);
193 * tee_shm_alloc() - Allocate shared memory
194 * @ctx: Context that allocates the shared memory
195 * @size: Requested size of shared memory
196 * @flags: Flags setting properties for the requested shared memory.
198 * Memory allocated as global shared memory is automatically freed when the
199 * TEE file pointer is closed. The @flags field uses the bits defined by
200 * TEE_SHM_* in <linux/tee_drv.h>. TEE_SHM_MAPPED must currently always be
201 * set. If TEE_SHM_DMA_BUF global shared memory will be allocated and
202 * associated with a dma-buf handle, else driver private memory.
204 struct tee_shm *tee_shm_alloc(struct tee_context *ctx, size_t size, u32 flags)
206 return __tee_shm_alloc(ctx, ctx->teedev, size, flags);
208 EXPORT_SYMBOL_GPL(tee_shm_alloc);
210 struct tee_shm *tee_shm_priv_alloc(struct tee_device *teedev, size_t size)
212 return __tee_shm_alloc(NULL, teedev, size, TEE_SHM_MAPPED);
214 EXPORT_SYMBOL_GPL(tee_shm_priv_alloc);
216 struct tee_shm *tee_shm_register(struct tee_context *ctx, unsigned long addr,
217 size_t length, u32 flags)
219 struct tee_device *teedev = ctx->teedev;
220 const u32 req_flags = TEE_SHM_DMA_BUF | TEE_SHM_USER_MAPPED;
227 if (flags != req_flags)
228 return ERR_PTR(-ENOTSUPP);
230 if (!tee_device_get(teedev))
231 return ERR_PTR(-EINVAL);
233 if (!teedev->desc->ops->shm_register ||
234 !teedev->desc->ops->shm_unregister) {
235 tee_device_put(teedev);
236 return ERR_PTR(-ENOTSUPP);
241 shm = kzalloc(sizeof(*shm), GFP_KERNEL);
243 ret = ERR_PTR(-ENOMEM);
247 shm->flags = flags | TEE_SHM_REGISTER;
248 shm->teedev = teedev;
251 addr = untagged_addr(addr);
252 start = rounddown(addr, PAGE_SIZE);
253 shm->offset = addr - start;
255 num_pages = (roundup(addr + length, PAGE_SIZE) - start) / PAGE_SIZE;
256 shm->pages = kcalloc(num_pages, sizeof(*shm->pages), GFP_KERNEL);
258 ret = ERR_PTR(-ENOMEM);
262 rc = get_user_pages_fast(start, num_pages, FOLL_WRITE, shm->pages);
265 if (rc != num_pages) {
272 mutex_lock(&teedev->mutex);
273 shm->id = idr_alloc(&teedev->idr, shm, 1, 0, GFP_KERNEL);
274 mutex_unlock(&teedev->mutex);
277 ret = ERR_PTR(shm->id);
281 rc = teedev->desc->ops->shm_register(ctx, shm, shm->pages,
282 shm->num_pages, start);
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;
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);
304 mutex_lock(&teedev->mutex);
305 list_add_tail(&shm->link, &ctx->list_shm);
306 mutex_unlock(&teedev->mutex);
314 mutex_lock(&teedev->mutex);
315 idr_remove(&teedev->idr, shm->id);
316 mutex_unlock(&teedev->mutex);
319 for (n = 0; n < shm->num_pages; n++)
320 put_page(shm->pages[n]);
326 tee_device_put(teedev);
329 EXPORT_SYMBOL_GPL(tee_shm_register);
332 * tee_shm_get_fd() - Increase reference count and return file descriptor
333 * @shm: Shared memory handle
334 * @returns user space file descriptor to shared memory
336 int tee_shm_get_fd(struct tee_shm *shm)
340 if (!(shm->flags & TEE_SHM_DMA_BUF))
343 get_dma_buf(shm->dmabuf);
344 fd = dma_buf_fd(shm->dmabuf, O_CLOEXEC);
346 dma_buf_put(shm->dmabuf);
351 * tee_shm_free() - Free shared memory
352 * @shm: Handle to shared memory to free
354 void tee_shm_free(struct tee_shm *shm)
357 * dma_buf_put() decreases the dmabuf reference counter and will
358 * call tee_shm_release() when the last reference is gone.
360 * In the case of driver private memory we call tee_shm_release
361 * directly instead as it doesn't have a reference counter.
363 if (shm->flags & TEE_SHM_DMA_BUF)
364 dma_buf_put(shm->dmabuf);
366 tee_shm_release(shm);
368 EXPORT_SYMBOL_GPL(tee_shm_free);
371 * tee_shm_va2pa() - Get physical address of a virtual address
372 * @shm: Shared memory handle
373 * @va: Virtual address to tranlsate
374 * @pa: Returned physical address
375 * @returns 0 on success and < 0 on failure
377 int tee_shm_va2pa(struct tee_shm *shm, void *va, phys_addr_t *pa)
379 if (!(shm->flags & TEE_SHM_MAPPED))
381 /* Check that we're in the range of the shm */
382 if ((char *)va < (char *)shm->kaddr)
384 if ((char *)va >= ((char *)shm->kaddr + shm->size))
387 return tee_shm_get_pa(
388 shm, (unsigned long)va - (unsigned long)shm->kaddr, pa);
390 EXPORT_SYMBOL_GPL(tee_shm_va2pa);
393 * tee_shm_pa2va() - Get virtual address of a physical address
394 * @shm: Shared memory handle
395 * @pa: Physical address to tranlsate
396 * @va: Returned virtual address
397 * @returns 0 on success and < 0 on failure
399 int tee_shm_pa2va(struct tee_shm *shm, phys_addr_t pa, void **va)
401 if (!(shm->flags & TEE_SHM_MAPPED))
403 /* Check that we're in the range of the shm */
406 if (pa >= (shm->paddr + shm->size))
410 void *v = tee_shm_get_va(shm, pa - shm->paddr);
418 EXPORT_SYMBOL_GPL(tee_shm_pa2va);
421 * tee_shm_get_va() - Get virtual address of a shared memory plus an offset
422 * @shm: Shared memory handle
423 * @offs: Offset from start of this shared memory
424 * @returns virtual address of the shared memory + offs if offs is within
425 * the bounds of this shared memory, else an ERR_PTR
427 void *tee_shm_get_va(struct tee_shm *shm, size_t offs)
429 if (!(shm->flags & TEE_SHM_MAPPED))
430 return ERR_PTR(-EINVAL);
431 if (offs >= shm->size)
432 return ERR_PTR(-EINVAL);
433 return (char *)shm->kaddr + offs;
435 EXPORT_SYMBOL_GPL(tee_shm_get_va);
438 * tee_shm_get_pa() - Get physical address of a shared memory plus an offset
439 * @shm: Shared memory handle
440 * @offs: Offset from start of this shared memory
441 * @pa: Physical address to return
442 * @returns 0 if offs is within the bounds of this shared memory, else an
445 int tee_shm_get_pa(struct tee_shm *shm, size_t offs, phys_addr_t *pa)
447 if (offs >= shm->size)
450 *pa = shm->paddr + offs;
453 EXPORT_SYMBOL_GPL(tee_shm_get_pa);
456 * tee_shm_get_from_id() - Find shared memory object and increase reference
458 * @ctx: Context owning the shared memory
459 * @id: Id of shared memory object
460 * @returns a pointer to 'struct tee_shm' on success or an ERR_PTR on failure
462 struct tee_shm *tee_shm_get_from_id(struct tee_context *ctx, int id)
464 struct tee_device *teedev;
468 return ERR_PTR(-EINVAL);
470 teedev = ctx->teedev;
471 mutex_lock(&teedev->mutex);
472 shm = idr_find(&teedev->idr, id);
473 if (!shm || shm->ctx != ctx)
474 shm = ERR_PTR(-EINVAL);
475 else if (shm->flags & TEE_SHM_DMA_BUF)
476 get_dma_buf(shm->dmabuf);
477 mutex_unlock(&teedev->mutex);
480 EXPORT_SYMBOL_GPL(tee_shm_get_from_id);
483 * tee_shm_put() - Decrease reference count on a shared memory handle
484 * @shm: Shared memory handle
486 void tee_shm_put(struct tee_shm *shm)
488 if (shm->flags & TEE_SHM_DMA_BUF)
489 dma_buf_put(shm->dmabuf);
491 EXPORT_SYMBOL_GPL(tee_shm_put);