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->ctx->teedev;
18 if (shm->flags & TEE_SHM_DMA_BUF) {
19 mutex_lock(&teedev->mutex);
20 idr_remove(&teedev->idr, shm->id);
21 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]);
47 teedev_ctx_put(shm->ctx);
51 tee_device_put(teedev);
54 static struct sg_table *tee_shm_op_map_dma_buf(struct dma_buf_attachment
55 *attach, enum dma_data_direction dir)
60 static void tee_shm_op_unmap_dma_buf(struct dma_buf_attachment *attach,
61 struct sg_table *table,
62 enum dma_data_direction dir)
66 static void tee_shm_op_release(struct dma_buf *dmabuf)
68 struct tee_shm *shm = dmabuf->priv;
73 static int tee_shm_op_mmap(struct dma_buf *dmabuf, struct vm_area_struct *vma)
75 struct tee_shm *shm = dmabuf->priv;
76 size_t size = vma->vm_end - vma->vm_start;
78 /* Refuse sharing shared memory provided by application */
79 if (shm->flags & TEE_SHM_USER_MAPPED)
82 return remap_pfn_range(vma, vma->vm_start, shm->paddr >> PAGE_SHIFT,
83 size, vma->vm_page_prot);
86 static const struct dma_buf_ops tee_shm_dma_buf_ops = {
87 .map_dma_buf = tee_shm_op_map_dma_buf,
88 .unmap_dma_buf = tee_shm_op_unmap_dma_buf,
89 .release = tee_shm_op_release,
90 .mmap = tee_shm_op_mmap,
93 struct tee_shm *tee_shm_alloc(struct tee_context *ctx, size_t size, u32 flags)
95 struct tee_device *teedev = ctx->teedev;
96 struct tee_shm_pool_mgr *poolm = NULL;
101 if (!(flags & TEE_SHM_MAPPED)) {
102 dev_err(teedev->dev.parent,
103 "only mapped allocations supported\n");
104 return ERR_PTR(-EINVAL);
107 if ((flags & ~(TEE_SHM_MAPPED | TEE_SHM_DMA_BUF))) {
108 dev_err(teedev->dev.parent, "invalid shm flags 0x%x", flags);
109 return ERR_PTR(-EINVAL);
112 if (!tee_device_get(teedev))
113 return ERR_PTR(-EINVAL);
116 /* teedev has been detached from driver */
117 ret = ERR_PTR(-EINVAL);
121 shm = kzalloc(sizeof(*shm), GFP_KERNEL);
123 ret = ERR_PTR(-ENOMEM);
127 shm->flags = flags | TEE_SHM_POOL;
129 if (flags & TEE_SHM_DMA_BUF)
130 poolm = teedev->pool->dma_buf_mgr;
132 poolm = teedev->pool->private_mgr;
134 rc = poolm->ops->alloc(poolm, shm, size);
141 if (flags & TEE_SHM_DMA_BUF) {
142 DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
144 mutex_lock(&teedev->mutex);
145 shm->id = idr_alloc(&teedev->idr, shm, 1, 0, GFP_KERNEL);
146 mutex_unlock(&teedev->mutex);
148 ret = ERR_PTR(shm->id);
152 exp_info.ops = &tee_shm_dma_buf_ops;
153 exp_info.size = shm->size;
154 exp_info.flags = O_RDWR;
157 shm->dmabuf = dma_buf_export(&exp_info);
158 if (IS_ERR(shm->dmabuf)) {
159 ret = ERR_CAST(shm->dmabuf);
169 if (flags & TEE_SHM_DMA_BUF) {
170 mutex_lock(&teedev->mutex);
171 idr_remove(&teedev->idr, shm->id);
172 mutex_unlock(&teedev->mutex);
175 poolm->ops->free(poolm, shm);
179 tee_device_put(teedev);
182 EXPORT_SYMBOL_GPL(tee_shm_alloc);
184 struct tee_shm *tee_shm_register(struct tee_context *ctx, unsigned long addr,
185 size_t length, u32 flags)
187 struct tee_device *teedev = ctx->teedev;
188 const u32 req_flags = TEE_SHM_DMA_BUF | TEE_SHM_USER_MAPPED;
195 if (flags != req_flags)
196 return ERR_PTR(-ENOTSUPP);
198 if (!tee_device_get(teedev))
199 return ERR_PTR(-EINVAL);
201 if (!teedev->desc->ops->shm_register ||
202 !teedev->desc->ops->shm_unregister) {
203 tee_device_put(teedev);
204 return ERR_PTR(-ENOTSUPP);
209 shm = kzalloc(sizeof(*shm), GFP_KERNEL);
211 ret = ERR_PTR(-ENOMEM);
215 shm->flags = flags | TEE_SHM_REGISTER;
218 addr = untagged_addr(addr);
219 start = rounddown(addr, PAGE_SIZE);
220 shm->offset = addr - start;
222 num_pages = (roundup(addr + length, PAGE_SIZE) - start) / PAGE_SIZE;
223 shm->pages = kcalloc(num_pages, sizeof(*shm->pages), GFP_KERNEL);
225 ret = ERR_PTR(-ENOMEM);
229 rc = get_user_pages_fast(start, num_pages, FOLL_WRITE, shm->pages);
232 if (rc != num_pages) {
239 mutex_lock(&teedev->mutex);
240 shm->id = idr_alloc(&teedev->idr, shm, 1, 0, GFP_KERNEL);
241 mutex_unlock(&teedev->mutex);
244 ret = ERR_PTR(shm->id);
248 rc = teedev->desc->ops->shm_register(ctx, shm, shm->pages,
249 shm->num_pages, start);
255 if (flags & TEE_SHM_DMA_BUF) {
256 DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
258 exp_info.ops = &tee_shm_dma_buf_ops;
259 exp_info.size = shm->size;
260 exp_info.flags = O_RDWR;
263 shm->dmabuf = dma_buf_export(&exp_info);
264 if (IS_ERR(shm->dmabuf)) {
265 ret = ERR_CAST(shm->dmabuf);
266 teedev->desc->ops->shm_unregister(ctx, shm);
277 mutex_lock(&teedev->mutex);
278 idr_remove(&teedev->idr, shm->id);
279 mutex_unlock(&teedev->mutex);
282 for (n = 0; n < shm->num_pages; n++)
283 put_page(shm->pages[n]);
289 tee_device_put(teedev);
292 EXPORT_SYMBOL_GPL(tee_shm_register);
295 * tee_shm_get_fd() - Increase reference count and return file descriptor
296 * @shm: Shared memory handle
297 * @returns user space file descriptor to shared memory
299 int tee_shm_get_fd(struct tee_shm *shm)
303 if (!(shm->flags & TEE_SHM_DMA_BUF))
306 get_dma_buf(shm->dmabuf);
307 fd = dma_buf_fd(shm->dmabuf, O_CLOEXEC);
309 dma_buf_put(shm->dmabuf);
314 * tee_shm_free() - Free shared memory
315 * @shm: Handle to shared memory to free
317 void tee_shm_free(struct tee_shm *shm)
320 * dma_buf_put() decreases the dmabuf reference counter and will
321 * call tee_shm_release() when the last reference is gone.
323 * In the case of driver private memory we call tee_shm_release
324 * directly instead as it doesn't have a reference counter.
326 if (shm->flags & TEE_SHM_DMA_BUF)
327 dma_buf_put(shm->dmabuf);
329 tee_shm_release(shm);
331 EXPORT_SYMBOL_GPL(tee_shm_free);
334 * tee_shm_va2pa() - Get physical address of a virtual address
335 * @shm: Shared memory handle
336 * @va: Virtual address to tranlsate
337 * @pa: Returned physical address
338 * @returns 0 on success and < 0 on failure
340 int tee_shm_va2pa(struct tee_shm *shm, void *va, phys_addr_t *pa)
342 if (!(shm->flags & TEE_SHM_MAPPED))
344 /* Check that we're in the range of the shm */
345 if ((char *)va < (char *)shm->kaddr)
347 if ((char *)va >= ((char *)shm->kaddr + shm->size))
350 return tee_shm_get_pa(
351 shm, (unsigned long)va - (unsigned long)shm->kaddr, pa);
353 EXPORT_SYMBOL_GPL(tee_shm_va2pa);
356 * tee_shm_pa2va() - Get virtual address of a physical address
357 * @shm: Shared memory handle
358 * @pa: Physical address to tranlsate
359 * @va: Returned virtual address
360 * @returns 0 on success and < 0 on failure
362 int tee_shm_pa2va(struct tee_shm *shm, phys_addr_t pa, void **va)
364 if (!(shm->flags & TEE_SHM_MAPPED))
366 /* Check that we're in the range of the shm */
369 if (pa >= (shm->paddr + shm->size))
373 void *v = tee_shm_get_va(shm, pa - shm->paddr);
381 EXPORT_SYMBOL_GPL(tee_shm_pa2va);
384 * tee_shm_get_va() - Get virtual address of a shared memory plus an offset
385 * @shm: Shared memory handle
386 * @offs: Offset from start of this shared memory
387 * @returns virtual address of the shared memory + offs if offs is within
388 * the bounds of this shared memory, else an ERR_PTR
390 void *tee_shm_get_va(struct tee_shm *shm, size_t offs)
392 if (!(shm->flags & TEE_SHM_MAPPED))
393 return ERR_PTR(-EINVAL);
394 if (offs >= shm->size)
395 return ERR_PTR(-EINVAL);
396 return (char *)shm->kaddr + offs;
398 EXPORT_SYMBOL_GPL(tee_shm_get_va);
401 * tee_shm_get_pa() - Get physical address of a shared memory plus an offset
402 * @shm: Shared memory handle
403 * @offs: Offset from start of this shared memory
404 * @pa: Physical address to return
405 * @returns 0 if offs is within the bounds of this shared memory, else an
408 int tee_shm_get_pa(struct tee_shm *shm, size_t offs, phys_addr_t *pa)
410 if (offs >= shm->size)
413 *pa = shm->paddr + offs;
416 EXPORT_SYMBOL_GPL(tee_shm_get_pa);
419 * tee_shm_get_from_id() - Find shared memory object and increase reference
421 * @ctx: Context owning the shared memory
422 * @id: Id of shared memory object
423 * @returns a pointer to 'struct tee_shm' on success or an ERR_PTR on failure
425 struct tee_shm *tee_shm_get_from_id(struct tee_context *ctx, int id)
427 struct tee_device *teedev;
431 return ERR_PTR(-EINVAL);
433 teedev = ctx->teedev;
434 mutex_lock(&teedev->mutex);
435 shm = idr_find(&teedev->idr, id);
436 if (!shm || shm->ctx != ctx)
437 shm = ERR_PTR(-EINVAL);
438 else if (shm->flags & TEE_SHM_DMA_BUF)
439 get_dma_buf(shm->dmabuf);
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 if (shm->flags & TEE_SHM_DMA_BUF)
452 dma_buf_put(shm->dmabuf);
454 EXPORT_SYMBOL_GPL(tee_shm_put);