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
))) {
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
);
196 struct tee_shm
*tee_shm_register(struct tee_context
*ctx
, unsigned long addr
,
197 size_t length
, u32 flags
)
199 struct tee_device
*teedev
= ctx
->teedev
;
200 const u32 req_user_flags
= TEE_SHM_DMA_BUF
| TEE_SHM_USER_MAPPED
;
201 const u32 req_kernel_flags
= TEE_SHM_DMA_BUF
| TEE_SHM_KERNEL_MAPPED
;
208 if (flags
!= req_user_flags
&& flags
!= req_kernel_flags
)
209 return ERR_PTR(-ENOTSUPP
);
211 if (!tee_device_get(teedev
))
212 return ERR_PTR(-EINVAL
);
214 if (!teedev
->desc
->ops
->shm_register
||
215 !teedev
->desc
->ops
->shm_unregister
) {
216 tee_device_put(teedev
);
217 return ERR_PTR(-ENOTSUPP
);
222 shm
= kzalloc(sizeof(*shm
), GFP_KERNEL
);
224 ret
= ERR_PTR(-ENOMEM
);
228 shm
->flags
= flags
| TEE_SHM_REGISTER
;
231 addr
= untagged_addr(addr
);
232 start
= rounddown(addr
, PAGE_SIZE
);
233 shm
->offset
= addr
- start
;
235 num_pages
= (roundup(addr
+ length
, PAGE_SIZE
) - start
) / PAGE_SIZE
;
236 shm
->pages
= kcalloc(num_pages
, sizeof(*shm
->pages
), GFP_KERNEL
);
238 ret
= ERR_PTR(-ENOMEM
);
242 if (flags
& TEE_SHM_USER_MAPPED
) {
243 rc
= pin_user_pages_fast(start
, num_pages
, FOLL_WRITE
,
249 kiov
= kcalloc(num_pages
, sizeof(*kiov
), GFP_KERNEL
);
251 ret
= ERR_PTR(-ENOMEM
);
255 for (i
= 0; i
< num_pages
; i
++) {
256 kiov
[i
].iov_base
= (void *)(start
+ i
* PAGE_SIZE
);
257 kiov
[i
].iov_len
= PAGE_SIZE
;
260 rc
= get_kernel_pages(kiov
, num_pages
, 0, 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
);
308 mutex_lock(&teedev
->mutex
);
309 idr_remove(&teedev
->idr
, shm
->id
);
310 mutex_unlock(&teedev
->mutex
);
312 release_registered_pages(shm
);
316 tee_device_put(teedev
);
319 EXPORT_SYMBOL_GPL(tee_shm_register
);
322 * tee_shm_get_fd() - Increase reference count and return file descriptor
323 * @shm: Shared memory handle
324 * @returns user space file descriptor to shared memory
326 int tee_shm_get_fd(struct tee_shm
*shm
)
330 if (!(shm
->flags
& TEE_SHM_DMA_BUF
))
333 get_dma_buf(shm
->dmabuf
);
334 fd
= dma_buf_fd(shm
->dmabuf
, O_CLOEXEC
);
336 dma_buf_put(shm
->dmabuf
);
341 * tee_shm_free() - Free shared memory
342 * @shm: Handle to shared memory to free
344 void tee_shm_free(struct tee_shm
*shm
)
347 * dma_buf_put() decreases the dmabuf reference counter and will
348 * call tee_shm_release() when the last reference is gone.
350 * In the case of driver private memory we call tee_shm_release
351 * directly instead as it doesn't have a reference counter.
353 if (shm
->flags
& TEE_SHM_DMA_BUF
)
354 dma_buf_put(shm
->dmabuf
);
356 tee_shm_release(shm
);
358 EXPORT_SYMBOL_GPL(tee_shm_free
);
361 * tee_shm_va2pa() - Get physical address of a virtual address
362 * @shm: Shared memory handle
363 * @va: Virtual address to tranlsate
364 * @pa: Returned physical address
365 * @returns 0 on success and < 0 on failure
367 int tee_shm_va2pa(struct tee_shm
*shm
, void *va
, phys_addr_t
*pa
)
369 if (!(shm
->flags
& TEE_SHM_MAPPED
))
371 /* Check that we're in the range of the shm */
372 if ((char *)va
< (char *)shm
->kaddr
)
374 if ((char *)va
>= ((char *)shm
->kaddr
+ shm
->size
))
377 return tee_shm_get_pa(
378 shm
, (unsigned long)va
- (unsigned long)shm
->kaddr
, pa
);
380 EXPORT_SYMBOL_GPL(tee_shm_va2pa
);
383 * tee_shm_pa2va() - Get virtual address of a physical address
384 * @shm: Shared memory handle
385 * @pa: Physical address to tranlsate
386 * @va: Returned virtual address
387 * @returns 0 on success and < 0 on failure
389 int tee_shm_pa2va(struct tee_shm
*shm
, phys_addr_t pa
, void **va
)
391 if (!(shm
->flags
& TEE_SHM_MAPPED
))
393 /* Check that we're in the range of the shm */
396 if (pa
>= (shm
->paddr
+ shm
->size
))
400 void *v
= tee_shm_get_va(shm
, pa
- shm
->paddr
);
408 EXPORT_SYMBOL_GPL(tee_shm_pa2va
);
411 * tee_shm_get_va() - Get virtual address of a shared memory plus an offset
412 * @shm: Shared memory handle
413 * @offs: Offset from start of this shared memory
414 * @returns virtual address of the shared memory + offs if offs is within
415 * the bounds of this shared memory, else an ERR_PTR
417 void *tee_shm_get_va(struct tee_shm
*shm
, size_t offs
)
419 if (!(shm
->flags
& TEE_SHM_MAPPED
))
420 return ERR_PTR(-EINVAL
);
421 if (offs
>= shm
->size
)
422 return ERR_PTR(-EINVAL
);
423 return (char *)shm
->kaddr
+ offs
;
425 EXPORT_SYMBOL_GPL(tee_shm_get_va
);
428 * tee_shm_get_pa() - Get physical address of a shared memory plus an offset
429 * @shm: Shared memory handle
430 * @offs: Offset from start of this shared memory
431 * @pa: Physical address to return
432 * @returns 0 if offs is within the bounds of this shared memory, else an
435 int tee_shm_get_pa(struct tee_shm
*shm
, size_t offs
, phys_addr_t
*pa
)
437 if (offs
>= shm
->size
)
440 *pa
= shm
->paddr
+ offs
;
443 EXPORT_SYMBOL_GPL(tee_shm_get_pa
);
446 * tee_shm_get_from_id() - Find shared memory object and increase reference
448 * @ctx: Context owning the shared memory
449 * @id: Id of shared memory object
450 * @returns a pointer to 'struct tee_shm' on success or an ERR_PTR on failure
452 struct tee_shm
*tee_shm_get_from_id(struct tee_context
*ctx
, int id
)
454 struct tee_device
*teedev
;
458 return ERR_PTR(-EINVAL
);
460 teedev
= ctx
->teedev
;
461 mutex_lock(&teedev
->mutex
);
462 shm
= idr_find(&teedev
->idr
, id
);
463 if (!shm
|| shm
->ctx
!= ctx
)
464 shm
= ERR_PTR(-EINVAL
);
465 else if (shm
->flags
& TEE_SHM_DMA_BUF
)
466 get_dma_buf(shm
->dmabuf
);
467 mutex_unlock(&teedev
->mutex
);
470 EXPORT_SYMBOL_GPL(tee_shm_get_from_id
);
473 * tee_shm_put() - Decrease reference count on a shared memory handle
474 * @shm: Shared memory handle
476 void tee_shm_put(struct tee_shm
*shm
)
478 if (shm
->flags
& TEE_SHM_DMA_BUF
)
479 dma_buf_put(shm
->dmabuf
);
481 EXPORT_SYMBOL_GPL(tee_shm_put
);