2 * Copyright (c) 2015-2016, Linaro Limited
4 * This software is licensed under the terms of the GNU General Public
5 * License version 2, as published by the Free Software Foundation, and
6 * may be copied, distributed, and modified under those terms.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
14 #include <linux/device.h>
15 #include <linux/dma-buf.h>
16 #include <linux/fdtable.h>
17 #include <linux/idr.h>
18 #include <linux/sched.h>
19 #include <linux/slab.h>
20 #include <linux/tee_drv.h>
21 #include "tee_private.h"
23 static void tee_shm_release(struct tee_shm
*shm
)
25 struct tee_device
*teedev
= shm
->teedev
;
27 mutex_lock(&teedev
->mutex
);
28 idr_remove(&teedev
->idr
, shm
->id
);
31 mutex_unlock(&teedev
->mutex
);
33 if (shm
->flags
& TEE_SHM_POOL
) {
34 struct tee_shm_pool_mgr
*poolm
;
36 if (shm
->flags
& TEE_SHM_DMA_BUF
)
37 poolm
= teedev
->pool
->dma_buf_mgr
;
39 poolm
= teedev
->pool
->private_mgr
;
41 poolm
->ops
->free(poolm
, shm
);
42 } else if (shm
->flags
& TEE_SHM_REGISTER
) {
44 int rc
= teedev
->desc
->ops
->shm_unregister(shm
->ctx
, shm
);
47 dev_err(teedev
->dev
.parent
,
48 "unregister shm %p failed: %d", shm
, rc
);
50 for (n
= 0; n
< shm
->num_pages
; n
++)
51 put_page(shm
->pages
[n
]);
57 teedev_ctx_put(shm
->ctx
);
61 tee_device_put(teedev
);
64 static struct sg_table
*tee_shm_op_map_dma_buf(struct dma_buf_attachment
65 *attach
, enum dma_data_direction dir
)
70 static void tee_shm_op_unmap_dma_buf(struct dma_buf_attachment
*attach
,
71 struct sg_table
*table
,
72 enum dma_data_direction dir
)
76 static void tee_shm_op_release(struct dma_buf
*dmabuf
)
78 struct tee_shm
*shm
= dmabuf
->priv
;
83 static void *tee_shm_op_map_atomic(struct dma_buf
*dmabuf
, unsigned long pgnum
)
88 static void *tee_shm_op_map(struct dma_buf
*dmabuf
, unsigned long pgnum
)
93 static int tee_shm_op_mmap(struct dma_buf
*dmabuf
, struct vm_area_struct
*vma
)
95 struct tee_shm
*shm
= dmabuf
->priv
;
96 size_t size
= vma
->vm_end
- vma
->vm_start
;
98 /* Refuse sharing shared memory provided by application */
99 if (shm
->flags
& TEE_SHM_REGISTER
)
102 return remap_pfn_range(vma
, vma
->vm_start
, shm
->paddr
>> PAGE_SHIFT
,
103 size
, vma
->vm_page_prot
);
106 static const struct dma_buf_ops tee_shm_dma_buf_ops
= {
107 .map_dma_buf
= tee_shm_op_map_dma_buf
,
108 .unmap_dma_buf
= tee_shm_op_unmap_dma_buf
,
109 .release
= tee_shm_op_release
,
110 .map_atomic
= tee_shm_op_map_atomic
,
111 .map
= tee_shm_op_map
,
112 .mmap
= tee_shm_op_mmap
,
115 static struct tee_shm
*__tee_shm_alloc(struct tee_context
*ctx
,
116 struct tee_device
*teedev
,
117 size_t size
, u32 flags
)
119 struct tee_shm_pool_mgr
*poolm
= NULL
;
124 if (ctx
&& ctx
->teedev
!= teedev
) {
125 dev_err(teedev
->dev
.parent
, "ctx and teedev mismatch\n");
126 return ERR_PTR(-EINVAL
);
129 if (!(flags
& TEE_SHM_MAPPED
)) {
130 dev_err(teedev
->dev
.parent
,
131 "only mapped allocations supported\n");
132 return ERR_PTR(-EINVAL
);
135 if ((flags
& ~(TEE_SHM_MAPPED
| TEE_SHM_DMA_BUF
))) {
136 dev_err(teedev
->dev
.parent
, "invalid shm flags 0x%x", flags
);
137 return ERR_PTR(-EINVAL
);
140 if (!tee_device_get(teedev
))
141 return ERR_PTR(-EINVAL
);
144 /* teedev has been detached from driver */
145 ret
= ERR_PTR(-EINVAL
);
149 shm
= kzalloc(sizeof(*shm
), GFP_KERNEL
);
151 ret
= ERR_PTR(-ENOMEM
);
155 shm
->flags
= flags
| TEE_SHM_POOL
;
156 shm
->teedev
= teedev
;
158 if (flags
& TEE_SHM_DMA_BUF
)
159 poolm
= teedev
->pool
->dma_buf_mgr
;
161 poolm
= teedev
->pool
->private_mgr
;
163 rc
= poolm
->ops
->alloc(poolm
, shm
, size
);
169 mutex_lock(&teedev
->mutex
);
170 shm
->id
= idr_alloc(&teedev
->idr
, shm
, 1, 0, GFP_KERNEL
);
171 mutex_unlock(&teedev
->mutex
);
173 ret
= ERR_PTR(shm
->id
);
177 if (flags
& TEE_SHM_DMA_BUF
) {
178 DEFINE_DMA_BUF_EXPORT_INFO(exp_info
);
180 exp_info
.ops
= &tee_shm_dma_buf_ops
;
181 exp_info
.size
= shm
->size
;
182 exp_info
.flags
= O_RDWR
;
185 shm
->dmabuf
= dma_buf_export(&exp_info
);
186 if (IS_ERR(shm
->dmabuf
)) {
187 ret
= ERR_CAST(shm
->dmabuf
);
194 mutex_lock(&teedev
->mutex
);
195 list_add_tail(&shm
->link
, &ctx
->list_shm
);
196 mutex_unlock(&teedev
->mutex
);
201 mutex_lock(&teedev
->mutex
);
202 idr_remove(&teedev
->idr
, shm
->id
);
203 mutex_unlock(&teedev
->mutex
);
205 poolm
->ops
->free(poolm
, shm
);
209 tee_device_put(teedev
);
214 * tee_shm_alloc() - Allocate shared memory
215 * @ctx: Context that allocates the shared memory
216 * @size: Requested size of shared memory
217 * @flags: Flags setting properties for the requested shared memory.
219 * Memory allocated as global shared memory is automatically freed when the
220 * TEE file pointer is closed. The @flags field uses the bits defined by
221 * TEE_SHM_* in <linux/tee_drv.h>. TEE_SHM_MAPPED must currently always be
222 * set. If TEE_SHM_DMA_BUF global shared memory will be allocated and
223 * associated with a dma-buf handle, else driver private memory.
225 struct tee_shm
*tee_shm_alloc(struct tee_context
*ctx
, size_t size
, u32 flags
)
227 return __tee_shm_alloc(ctx
, ctx
->teedev
, size
, flags
);
229 EXPORT_SYMBOL_GPL(tee_shm_alloc
);
231 struct tee_shm
*tee_shm_priv_alloc(struct tee_device
*teedev
, size_t size
)
233 return __tee_shm_alloc(NULL
, teedev
, size
, TEE_SHM_MAPPED
);
235 EXPORT_SYMBOL_GPL(tee_shm_priv_alloc
);
237 struct tee_shm
*tee_shm_register(struct tee_context
*ctx
, unsigned long addr
,
238 size_t length
, u32 flags
)
240 struct tee_device
*teedev
= ctx
->teedev
;
241 const u32 req_flags
= TEE_SHM_DMA_BUF
| TEE_SHM_USER_MAPPED
;
248 if (flags
!= req_flags
)
249 return ERR_PTR(-ENOTSUPP
);
251 if (!tee_device_get(teedev
))
252 return ERR_PTR(-EINVAL
);
254 if (!teedev
->desc
->ops
->shm_register
||
255 !teedev
->desc
->ops
->shm_unregister
) {
256 tee_device_put(teedev
);
257 return ERR_PTR(-ENOTSUPP
);
262 shm
= kzalloc(sizeof(*shm
), GFP_KERNEL
);
264 ret
= ERR_PTR(-ENOMEM
);
268 shm
->flags
= flags
| TEE_SHM_REGISTER
;
269 shm
->teedev
= teedev
;
272 start
= rounddown(addr
, PAGE_SIZE
);
273 shm
->offset
= addr
- start
;
275 num_pages
= (roundup(addr
+ length
, PAGE_SIZE
) - start
) / PAGE_SIZE
;
276 shm
->pages
= kcalloc(num_pages
, sizeof(*shm
->pages
), GFP_KERNEL
);
278 ret
= ERR_PTR(-ENOMEM
);
282 rc
= get_user_pages_fast(start
, num_pages
, 1, shm
->pages
);
285 if (rc
!= num_pages
) {
292 mutex_lock(&teedev
->mutex
);
293 shm
->id
= idr_alloc(&teedev
->idr
, shm
, 1, 0, GFP_KERNEL
);
294 mutex_unlock(&teedev
->mutex
);
297 ret
= ERR_PTR(shm
->id
);
301 rc
= teedev
->desc
->ops
->shm_register(ctx
, shm
, shm
->pages
,
302 shm
->num_pages
, start
);
308 if (flags
& TEE_SHM_DMA_BUF
) {
309 DEFINE_DMA_BUF_EXPORT_INFO(exp_info
);
311 exp_info
.ops
= &tee_shm_dma_buf_ops
;
312 exp_info
.size
= shm
->size
;
313 exp_info
.flags
= O_RDWR
;
316 shm
->dmabuf
= dma_buf_export(&exp_info
);
317 if (IS_ERR(shm
->dmabuf
)) {
318 ret
= ERR_CAST(shm
->dmabuf
);
319 teedev
->desc
->ops
->shm_unregister(ctx
, shm
);
324 mutex_lock(&teedev
->mutex
);
325 list_add_tail(&shm
->link
, &ctx
->list_shm
);
326 mutex_unlock(&teedev
->mutex
);
334 mutex_lock(&teedev
->mutex
);
335 idr_remove(&teedev
->idr
, shm
->id
);
336 mutex_unlock(&teedev
->mutex
);
339 for (n
= 0; n
< shm
->num_pages
; n
++)
340 put_page(shm
->pages
[n
]);
346 tee_device_put(teedev
);
349 EXPORT_SYMBOL_GPL(tee_shm_register
);
352 * tee_shm_get_fd() - Increase reference count and return file descriptor
353 * @shm: Shared memory handle
354 * @returns user space file descriptor to shared memory
356 int tee_shm_get_fd(struct tee_shm
*shm
)
360 if (!(shm
->flags
& TEE_SHM_DMA_BUF
))
363 get_dma_buf(shm
->dmabuf
);
364 fd
= dma_buf_fd(shm
->dmabuf
, O_CLOEXEC
);
366 dma_buf_put(shm
->dmabuf
);
371 * tee_shm_free() - Free shared memory
372 * @shm: Handle to shared memory to free
374 void tee_shm_free(struct tee_shm
*shm
)
377 * dma_buf_put() decreases the dmabuf reference counter and will
378 * call tee_shm_release() when the last reference is gone.
380 * In the case of driver private memory we call tee_shm_release
381 * directly instead as it doesn't have a reference counter.
383 if (shm
->flags
& TEE_SHM_DMA_BUF
)
384 dma_buf_put(shm
->dmabuf
);
386 tee_shm_release(shm
);
388 EXPORT_SYMBOL_GPL(tee_shm_free
);
391 * tee_shm_va2pa() - Get physical address of a virtual address
392 * @shm: Shared memory handle
393 * @va: Virtual address to tranlsate
394 * @pa: Returned physical address
395 * @returns 0 on success and < 0 on failure
397 int tee_shm_va2pa(struct tee_shm
*shm
, void *va
, phys_addr_t
*pa
)
399 if (!(shm
->flags
& TEE_SHM_MAPPED
))
401 /* Check that we're in the range of the shm */
402 if ((char *)va
< (char *)shm
->kaddr
)
404 if ((char *)va
>= ((char *)shm
->kaddr
+ shm
->size
))
407 return tee_shm_get_pa(
408 shm
, (unsigned long)va
- (unsigned long)shm
->kaddr
, pa
);
410 EXPORT_SYMBOL_GPL(tee_shm_va2pa
);
413 * tee_shm_pa2va() - Get virtual address of a physical address
414 * @shm: Shared memory handle
415 * @pa: Physical address to tranlsate
416 * @va: Returned virtual address
417 * @returns 0 on success and < 0 on failure
419 int tee_shm_pa2va(struct tee_shm
*shm
, phys_addr_t pa
, void **va
)
421 if (!(shm
->flags
& TEE_SHM_MAPPED
))
423 /* Check that we're in the range of the shm */
426 if (pa
>= (shm
->paddr
+ shm
->size
))
430 void *v
= tee_shm_get_va(shm
, pa
- shm
->paddr
);
438 EXPORT_SYMBOL_GPL(tee_shm_pa2va
);
441 * tee_shm_get_va() - Get virtual address of a shared memory plus an offset
442 * @shm: Shared memory handle
443 * @offs: Offset from start of this shared memory
444 * @returns virtual address of the shared memory + offs if offs is within
445 * the bounds of this shared memory, else an ERR_PTR
447 void *tee_shm_get_va(struct tee_shm
*shm
, size_t offs
)
449 if (!(shm
->flags
& TEE_SHM_MAPPED
))
450 return ERR_PTR(-EINVAL
);
451 if (offs
>= shm
->size
)
452 return ERR_PTR(-EINVAL
);
453 return (char *)shm
->kaddr
+ offs
;
455 EXPORT_SYMBOL_GPL(tee_shm_get_va
);
458 * tee_shm_get_pa() - Get physical address of a shared memory plus an offset
459 * @shm: Shared memory handle
460 * @offs: Offset from start of this shared memory
461 * @pa: Physical address to return
462 * @returns 0 if offs is within the bounds of this shared memory, else an
465 int tee_shm_get_pa(struct tee_shm
*shm
, size_t offs
, phys_addr_t
*pa
)
467 if (offs
>= shm
->size
)
470 *pa
= shm
->paddr
+ offs
;
473 EXPORT_SYMBOL_GPL(tee_shm_get_pa
);
476 * tee_shm_get_from_id() - Find shared memory object and increase reference
478 * @ctx: Context owning the shared memory
479 * @id: Id of shared memory object
480 * @returns a pointer to 'struct tee_shm' on success or an ERR_PTR on failure
482 struct tee_shm
*tee_shm_get_from_id(struct tee_context
*ctx
, int id
)
484 struct tee_device
*teedev
;
488 return ERR_PTR(-EINVAL
);
490 teedev
= ctx
->teedev
;
491 mutex_lock(&teedev
->mutex
);
492 shm
= idr_find(&teedev
->idr
, id
);
493 if (!shm
|| shm
->ctx
!= ctx
)
494 shm
= ERR_PTR(-EINVAL
);
495 else if (shm
->flags
& TEE_SHM_DMA_BUF
)
496 get_dma_buf(shm
->dmabuf
);
497 mutex_unlock(&teedev
->mutex
);
500 EXPORT_SYMBOL_GPL(tee_shm_get_from_id
);
503 * tee_shm_put() - Decrease reference count on a shared memory handle
504 * @shm: Shared memory handle
506 void tee_shm_put(struct tee_shm
*shm
)
508 if (shm
->flags
& TEE_SHM_DMA_BUF
)
509 dma_buf_put(shm
->dmabuf
);
511 EXPORT_SYMBOL_GPL(tee_shm_put
);