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 void *tee_shm_op_map(struct dma_buf
*dmabuf
, unsigned long pgnum
)
79 static int tee_shm_op_mmap(struct dma_buf
*dmabuf
, struct vm_area_struct
*vma
)
81 struct tee_shm
*shm
= dmabuf
->priv
;
82 size_t size
= vma
->vm_end
- vma
->vm_start
;
84 /* Refuse sharing shared memory provided by application */
85 if (shm
->flags
& TEE_SHM_REGISTER
)
88 return remap_pfn_range(vma
, vma
->vm_start
, shm
->paddr
>> PAGE_SHIFT
,
89 size
, vma
->vm_page_prot
);
92 static const struct dma_buf_ops tee_shm_dma_buf_ops
= {
93 .map_dma_buf
= tee_shm_op_map_dma_buf
,
94 .unmap_dma_buf
= tee_shm_op_unmap_dma_buf
,
95 .release
= tee_shm_op_release
,
96 .map
= tee_shm_op_map
,
97 .mmap
= tee_shm_op_mmap
,
100 static struct tee_shm
*__tee_shm_alloc(struct tee_context
*ctx
,
101 struct tee_device
*teedev
,
102 size_t size
, u32 flags
)
104 struct tee_shm_pool_mgr
*poolm
= NULL
;
109 if (ctx
&& ctx
->teedev
!= teedev
) {
110 dev_err(teedev
->dev
.parent
, "ctx and teedev mismatch\n");
111 return ERR_PTR(-EINVAL
);
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
))) {
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
;
141 shm
->teedev
= teedev
;
143 if (flags
& TEE_SHM_DMA_BUF
)
144 poolm
= teedev
->pool
->dma_buf_mgr
;
146 poolm
= teedev
->pool
->private_mgr
;
148 rc
= poolm
->ops
->alloc(poolm
, shm
, size
);
154 mutex_lock(&teedev
->mutex
);
155 shm
->id
= idr_alloc(&teedev
->idr
, shm
, 1, 0, GFP_KERNEL
);
156 mutex_unlock(&teedev
->mutex
);
158 ret
= ERR_PTR(shm
->id
);
162 if (flags
& TEE_SHM_DMA_BUF
) {
163 DEFINE_DMA_BUF_EXPORT_INFO(exp_info
);
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
);
179 mutex_lock(&teedev
->mutex
);
180 list_add_tail(&shm
->link
, &ctx
->list_shm
);
181 mutex_unlock(&teedev
->mutex
);
186 mutex_lock(&teedev
->mutex
);
187 idr_remove(&teedev
->idr
, shm
->id
);
188 mutex_unlock(&teedev
->mutex
);
190 poolm
->ops
->free(poolm
, shm
);
194 tee_device_put(teedev
);
199 * tee_shm_alloc() - Allocate shared memory
200 * @ctx: Context that allocates the shared memory
201 * @size: Requested size of shared memory
202 * @flags: Flags setting properties for the requested shared memory.
204 * Memory allocated as global shared memory is automatically freed when the
205 * TEE file pointer is closed. The @flags field uses the bits defined by
206 * TEE_SHM_* in <linux/tee_drv.h>. TEE_SHM_MAPPED must currently always be
207 * set. If TEE_SHM_DMA_BUF global shared memory will be allocated and
208 * associated with a dma-buf handle, else driver private memory.
210 struct tee_shm
*tee_shm_alloc(struct tee_context
*ctx
, size_t size
, u32 flags
)
212 return __tee_shm_alloc(ctx
, ctx
->teedev
, size
, flags
);
214 EXPORT_SYMBOL_GPL(tee_shm_alloc
);
216 struct tee_shm
*tee_shm_priv_alloc(struct tee_device
*teedev
, size_t size
)
218 return __tee_shm_alloc(NULL
, teedev
, size
, TEE_SHM_MAPPED
);
220 EXPORT_SYMBOL_GPL(tee_shm_priv_alloc
);
222 struct tee_shm
*tee_shm_register(struct tee_context
*ctx
, unsigned long addr
,
223 size_t length
, u32 flags
)
225 struct tee_device
*teedev
= ctx
->teedev
;
226 const u32 req_flags
= TEE_SHM_DMA_BUF
| TEE_SHM_USER_MAPPED
;
233 if (flags
!= req_flags
)
234 return ERR_PTR(-ENOTSUPP
);
236 if (!tee_device_get(teedev
))
237 return ERR_PTR(-EINVAL
);
239 if (!teedev
->desc
->ops
->shm_register
||
240 !teedev
->desc
->ops
->shm_unregister
) {
241 tee_device_put(teedev
);
242 return ERR_PTR(-ENOTSUPP
);
247 shm
= kzalloc(sizeof(*shm
), GFP_KERNEL
);
249 ret
= ERR_PTR(-ENOMEM
);
253 shm
->flags
= flags
| TEE_SHM_REGISTER
;
254 shm
->teedev
= teedev
;
257 addr
= untagged_addr(addr
);
258 start
= rounddown(addr
, PAGE_SIZE
);
259 shm
->offset
= addr
- start
;
261 num_pages
= (roundup(addr
+ length
, PAGE_SIZE
) - start
) / PAGE_SIZE
;
262 shm
->pages
= kcalloc(num_pages
, sizeof(*shm
->pages
), GFP_KERNEL
);
264 ret
= ERR_PTR(-ENOMEM
);
268 rc
= get_user_pages_fast(start
, num_pages
, FOLL_WRITE
, shm
->pages
);
271 if (rc
!= num_pages
) {
278 mutex_lock(&teedev
->mutex
);
279 shm
->id
= idr_alloc(&teedev
->idr
, shm
, 1, 0, GFP_KERNEL
);
280 mutex_unlock(&teedev
->mutex
);
283 ret
= ERR_PTR(shm
->id
);
287 rc
= teedev
->desc
->ops
->shm_register(ctx
, shm
, shm
->pages
,
288 shm
->num_pages
, start
);
294 if (flags
& TEE_SHM_DMA_BUF
) {
295 DEFINE_DMA_BUF_EXPORT_INFO(exp_info
);
297 exp_info
.ops
= &tee_shm_dma_buf_ops
;
298 exp_info
.size
= shm
->size
;
299 exp_info
.flags
= O_RDWR
;
302 shm
->dmabuf
= dma_buf_export(&exp_info
);
303 if (IS_ERR(shm
->dmabuf
)) {
304 ret
= ERR_CAST(shm
->dmabuf
);
305 teedev
->desc
->ops
->shm_unregister(ctx
, shm
);
310 mutex_lock(&teedev
->mutex
);
311 list_add_tail(&shm
->link
, &ctx
->list_shm
);
312 mutex_unlock(&teedev
->mutex
);
320 mutex_lock(&teedev
->mutex
);
321 idr_remove(&teedev
->idr
, shm
->id
);
322 mutex_unlock(&teedev
->mutex
);
325 for (n
= 0; n
< shm
->num_pages
; n
++)
326 put_page(shm
->pages
[n
]);
332 tee_device_put(teedev
);
335 EXPORT_SYMBOL_GPL(tee_shm_register
);
338 * tee_shm_get_fd() - Increase reference count and return file descriptor
339 * @shm: Shared memory handle
340 * @returns user space file descriptor to shared memory
342 int tee_shm_get_fd(struct tee_shm
*shm
)
346 if (!(shm
->flags
& TEE_SHM_DMA_BUF
))
349 get_dma_buf(shm
->dmabuf
);
350 fd
= dma_buf_fd(shm
->dmabuf
, O_CLOEXEC
);
352 dma_buf_put(shm
->dmabuf
);
357 * tee_shm_free() - Free shared memory
358 * @shm: Handle to shared memory to free
360 void tee_shm_free(struct tee_shm
*shm
)
363 * dma_buf_put() decreases the dmabuf reference counter and will
364 * call tee_shm_release() when the last reference is gone.
366 * In the case of driver private memory we call tee_shm_release
367 * directly instead as it doesn't have a reference counter.
369 if (shm
->flags
& TEE_SHM_DMA_BUF
)
370 dma_buf_put(shm
->dmabuf
);
372 tee_shm_release(shm
);
374 EXPORT_SYMBOL_GPL(tee_shm_free
);
377 * tee_shm_va2pa() - Get physical address of a virtual address
378 * @shm: Shared memory handle
379 * @va: Virtual address to tranlsate
380 * @pa: Returned physical address
381 * @returns 0 on success and < 0 on failure
383 int tee_shm_va2pa(struct tee_shm
*shm
, void *va
, phys_addr_t
*pa
)
385 if (!(shm
->flags
& TEE_SHM_MAPPED
))
387 /* Check that we're in the range of the shm */
388 if ((char *)va
< (char *)shm
->kaddr
)
390 if ((char *)va
>= ((char *)shm
->kaddr
+ shm
->size
))
393 return tee_shm_get_pa(
394 shm
, (unsigned long)va
- (unsigned long)shm
->kaddr
, pa
);
396 EXPORT_SYMBOL_GPL(tee_shm_va2pa
);
399 * tee_shm_pa2va() - Get virtual address of a physical address
400 * @shm: Shared memory handle
401 * @pa: Physical address to tranlsate
402 * @va: Returned virtual address
403 * @returns 0 on success and < 0 on failure
405 int tee_shm_pa2va(struct tee_shm
*shm
, phys_addr_t pa
, void **va
)
407 if (!(shm
->flags
& TEE_SHM_MAPPED
))
409 /* Check that we're in the range of the shm */
412 if (pa
>= (shm
->paddr
+ shm
->size
))
416 void *v
= tee_shm_get_va(shm
, pa
- shm
->paddr
);
424 EXPORT_SYMBOL_GPL(tee_shm_pa2va
);
427 * tee_shm_get_va() - Get virtual address of a shared memory plus an offset
428 * @shm: Shared memory handle
429 * @offs: Offset from start of this shared memory
430 * @returns virtual address of the shared memory + offs if offs is within
431 * the bounds of this shared memory, else an ERR_PTR
433 void *tee_shm_get_va(struct tee_shm
*shm
, size_t offs
)
435 if (!(shm
->flags
& TEE_SHM_MAPPED
))
436 return ERR_PTR(-EINVAL
);
437 if (offs
>= shm
->size
)
438 return ERR_PTR(-EINVAL
);
439 return (char *)shm
->kaddr
+ offs
;
441 EXPORT_SYMBOL_GPL(tee_shm_get_va
);
444 * tee_shm_get_pa() - Get physical address of a shared memory plus an offset
445 * @shm: Shared memory handle
446 * @offs: Offset from start of this shared memory
447 * @pa: Physical address to return
448 * @returns 0 if offs is within the bounds of this shared memory, else an
451 int tee_shm_get_pa(struct tee_shm
*shm
, size_t offs
, phys_addr_t
*pa
)
453 if (offs
>= shm
->size
)
456 *pa
= shm
->paddr
+ offs
;
459 EXPORT_SYMBOL_GPL(tee_shm_get_pa
);
462 * tee_shm_get_from_id() - Find shared memory object and increase reference
464 * @ctx: Context owning the shared memory
465 * @id: Id of shared memory object
466 * @returns a pointer to 'struct tee_shm' on success or an ERR_PTR on failure
468 struct tee_shm
*tee_shm_get_from_id(struct tee_context
*ctx
, int id
)
470 struct tee_device
*teedev
;
474 return ERR_PTR(-EINVAL
);
476 teedev
= ctx
->teedev
;
477 mutex_lock(&teedev
->mutex
);
478 shm
= idr_find(&teedev
->idr
, id
);
479 if (!shm
|| shm
->ctx
!= ctx
)
480 shm
= ERR_PTR(-EINVAL
);
481 else if (shm
->flags
& TEE_SHM_DMA_BUF
)
482 get_dma_buf(shm
->dmabuf
);
483 mutex_unlock(&teedev
->mutex
);
486 EXPORT_SYMBOL_GPL(tee_shm_get_from_id
);
489 * tee_shm_put() - Decrease reference count on a shared memory handle
490 * @shm: Shared memory handle
492 void tee_shm_put(struct tee_shm
*shm
)
494 if (shm
->flags
& TEE_SHM_DMA_BUF
)
495 dma_buf_put(shm
->dmabuf
);
497 EXPORT_SYMBOL_GPL(tee_shm_put
);