Jens Wiklander | fb93896 | 2015-03-11 14:39:39 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015-2016, Linaro Limited |
| 3 | * |
| 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. |
| 7 | * |
| 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. |
| 12 | * |
| 13 | */ |
| 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" |
| 22 | |
| 23 | static void tee_shm_release(struct tee_shm *shm) |
| 24 | { |
| 25 | struct tee_device *teedev = shm->teedev; |
Jens Wiklander | fb93896 | 2015-03-11 14:39:39 +0100 | [diff] [blame] | 26 | |
| 27 | mutex_lock(&teedev->mutex); |
| 28 | idr_remove(&teedev->idr, shm->id); |
| 29 | if (shm->ctx) |
| 30 | list_del(&shm->link); |
| 31 | mutex_unlock(&teedev->mutex); |
| 32 | |
Jens Wiklander | f902700 | 2017-11-29 14:48:26 +0200 | [diff] [blame^] | 33 | if (shm->flags & TEE_SHM_POOL) { |
| 34 | struct tee_shm_pool_mgr *poolm; |
Jens Wiklander | fb93896 | 2015-03-11 14:39:39 +0100 | [diff] [blame] | 35 | |
Jens Wiklander | f902700 | 2017-11-29 14:48:26 +0200 | [diff] [blame^] | 36 | if (shm->flags & TEE_SHM_DMA_BUF) |
| 37 | poolm = teedev->pool->dma_buf_mgr; |
| 38 | else |
| 39 | poolm = teedev->pool->private_mgr; |
| 40 | |
| 41 | poolm->ops->free(poolm, shm); |
| 42 | } else if (shm->flags & TEE_SHM_REGISTER) { |
| 43 | size_t n; |
| 44 | int rc = teedev->desc->ops->shm_unregister(shm->ctx, shm); |
| 45 | |
| 46 | if (rc) |
| 47 | dev_err(teedev->dev.parent, |
| 48 | "unregister shm %p failed: %d", shm, rc); |
| 49 | |
| 50 | for (n = 0; n < shm->num_pages; n++) |
| 51 | put_page(shm->pages[n]); |
| 52 | |
| 53 | kfree(shm->pages); |
| 54 | } |
| 55 | |
Jens Wiklander | fb93896 | 2015-03-11 14:39:39 +0100 | [diff] [blame] | 56 | kfree(shm); |
| 57 | |
| 58 | tee_device_put(teedev); |
| 59 | } |
| 60 | |
| 61 | static struct sg_table *tee_shm_op_map_dma_buf(struct dma_buf_attachment |
| 62 | *attach, enum dma_data_direction dir) |
| 63 | { |
| 64 | return NULL; |
| 65 | } |
| 66 | |
| 67 | static void tee_shm_op_unmap_dma_buf(struct dma_buf_attachment *attach, |
| 68 | struct sg_table *table, |
| 69 | enum dma_data_direction dir) |
| 70 | { |
| 71 | } |
| 72 | |
| 73 | static void tee_shm_op_release(struct dma_buf *dmabuf) |
| 74 | { |
| 75 | struct tee_shm *shm = dmabuf->priv; |
| 76 | |
| 77 | tee_shm_release(shm); |
| 78 | } |
| 79 | |
| 80 | static void *tee_shm_op_kmap_atomic(struct dma_buf *dmabuf, unsigned long pgnum) |
| 81 | { |
| 82 | return NULL; |
| 83 | } |
| 84 | |
| 85 | static void *tee_shm_op_kmap(struct dma_buf *dmabuf, unsigned long pgnum) |
| 86 | { |
| 87 | return NULL; |
| 88 | } |
| 89 | |
| 90 | static int tee_shm_op_mmap(struct dma_buf *dmabuf, struct vm_area_struct *vma) |
| 91 | { |
| 92 | struct tee_shm *shm = dmabuf->priv; |
| 93 | size_t size = vma->vm_end - vma->vm_start; |
| 94 | |
Jens Wiklander | f902700 | 2017-11-29 14:48:26 +0200 | [diff] [blame^] | 95 | /* Refuse sharing shared memory provided by application */ |
| 96 | if (shm->flags & TEE_SHM_REGISTER) |
| 97 | return -EINVAL; |
| 98 | |
Jens Wiklander | fb93896 | 2015-03-11 14:39:39 +0100 | [diff] [blame] | 99 | return remap_pfn_range(vma, vma->vm_start, shm->paddr >> PAGE_SHIFT, |
| 100 | size, vma->vm_page_prot); |
| 101 | } |
| 102 | |
Arvind Yadav | 736d117 | 2017-07-01 17:56:06 +0530 | [diff] [blame] | 103 | static const struct dma_buf_ops tee_shm_dma_buf_ops = { |
Jens Wiklander | fb93896 | 2015-03-11 14:39:39 +0100 | [diff] [blame] | 104 | .map_dma_buf = tee_shm_op_map_dma_buf, |
| 105 | .unmap_dma_buf = tee_shm_op_unmap_dma_buf, |
| 106 | .release = tee_shm_op_release, |
| 107 | .kmap_atomic = tee_shm_op_kmap_atomic, |
| 108 | .kmap = tee_shm_op_kmap, |
| 109 | .mmap = tee_shm_op_mmap, |
| 110 | }; |
| 111 | |
Jens Wiklander | f902700 | 2017-11-29 14:48:26 +0200 | [diff] [blame^] | 112 | struct tee_shm *__tee_shm_alloc(struct tee_context *ctx, |
| 113 | struct tee_device *teedev, |
| 114 | size_t size, u32 flags) |
Jens Wiklander | fb93896 | 2015-03-11 14:39:39 +0100 | [diff] [blame] | 115 | { |
Jens Wiklander | fb93896 | 2015-03-11 14:39:39 +0100 | [diff] [blame] | 116 | struct tee_shm_pool_mgr *poolm = NULL; |
| 117 | struct tee_shm *shm; |
| 118 | void *ret; |
| 119 | int rc; |
| 120 | |
Jens Wiklander | f902700 | 2017-11-29 14:48:26 +0200 | [diff] [blame^] | 121 | if (ctx && ctx->teedev != teedev) { |
| 122 | dev_err(teedev->dev.parent, "ctx and teedev mismatch\n"); |
| 123 | return ERR_PTR(-EINVAL); |
| 124 | } |
| 125 | |
Jens Wiklander | fb93896 | 2015-03-11 14:39:39 +0100 | [diff] [blame] | 126 | if (!(flags & TEE_SHM_MAPPED)) { |
| 127 | dev_err(teedev->dev.parent, |
| 128 | "only mapped allocations supported\n"); |
| 129 | return ERR_PTR(-EINVAL); |
| 130 | } |
| 131 | |
| 132 | if ((flags & ~(TEE_SHM_MAPPED | TEE_SHM_DMA_BUF))) { |
| 133 | dev_err(teedev->dev.parent, "invalid shm flags 0x%x", flags); |
| 134 | return ERR_PTR(-EINVAL); |
| 135 | } |
| 136 | |
| 137 | if (!tee_device_get(teedev)) |
| 138 | return ERR_PTR(-EINVAL); |
| 139 | |
| 140 | if (!teedev->pool) { |
| 141 | /* teedev has been detached from driver */ |
| 142 | ret = ERR_PTR(-EINVAL); |
| 143 | goto err_dev_put; |
| 144 | } |
| 145 | |
| 146 | shm = kzalloc(sizeof(*shm), GFP_KERNEL); |
| 147 | if (!shm) { |
| 148 | ret = ERR_PTR(-ENOMEM); |
| 149 | goto err_dev_put; |
| 150 | } |
| 151 | |
Jens Wiklander | f902700 | 2017-11-29 14:48:26 +0200 | [diff] [blame^] | 152 | shm->flags = flags | TEE_SHM_POOL; |
Jens Wiklander | fb93896 | 2015-03-11 14:39:39 +0100 | [diff] [blame] | 153 | shm->teedev = teedev; |
| 154 | shm->ctx = ctx; |
| 155 | if (flags & TEE_SHM_DMA_BUF) |
Jens Wiklander | 2d46c7f | 2017-11-29 14:48:25 +0200 | [diff] [blame] | 156 | poolm = teedev->pool->dma_buf_mgr; |
Jens Wiklander | fb93896 | 2015-03-11 14:39:39 +0100 | [diff] [blame] | 157 | else |
Jens Wiklander | 2d46c7f | 2017-11-29 14:48:25 +0200 | [diff] [blame] | 158 | poolm = teedev->pool->private_mgr; |
Jens Wiklander | fb93896 | 2015-03-11 14:39:39 +0100 | [diff] [blame] | 159 | |
| 160 | rc = poolm->ops->alloc(poolm, shm, size); |
| 161 | if (rc) { |
| 162 | ret = ERR_PTR(rc); |
| 163 | goto err_kfree; |
| 164 | } |
| 165 | |
| 166 | mutex_lock(&teedev->mutex); |
| 167 | shm->id = idr_alloc(&teedev->idr, shm, 1, 0, GFP_KERNEL); |
| 168 | mutex_unlock(&teedev->mutex); |
| 169 | if (shm->id < 0) { |
| 170 | ret = ERR_PTR(shm->id); |
| 171 | goto err_pool_free; |
| 172 | } |
| 173 | |
| 174 | if (flags & TEE_SHM_DMA_BUF) { |
| 175 | DEFINE_DMA_BUF_EXPORT_INFO(exp_info); |
| 176 | |
| 177 | exp_info.ops = &tee_shm_dma_buf_ops; |
| 178 | exp_info.size = shm->size; |
| 179 | exp_info.flags = O_RDWR; |
| 180 | exp_info.priv = shm; |
| 181 | |
| 182 | shm->dmabuf = dma_buf_export(&exp_info); |
| 183 | if (IS_ERR(shm->dmabuf)) { |
| 184 | ret = ERR_CAST(shm->dmabuf); |
| 185 | goto err_rem; |
| 186 | } |
| 187 | } |
Jens Wiklander | f902700 | 2017-11-29 14:48:26 +0200 | [diff] [blame^] | 188 | |
| 189 | if (ctx) { |
| 190 | mutex_lock(&teedev->mutex); |
| 191 | list_add_tail(&shm->link, &ctx->list_shm); |
| 192 | mutex_unlock(&teedev->mutex); |
| 193 | } |
Jens Wiklander | fb93896 | 2015-03-11 14:39:39 +0100 | [diff] [blame] | 194 | |
| 195 | return shm; |
| 196 | err_rem: |
| 197 | mutex_lock(&teedev->mutex); |
| 198 | idr_remove(&teedev->idr, shm->id); |
| 199 | mutex_unlock(&teedev->mutex); |
| 200 | err_pool_free: |
| 201 | poolm->ops->free(poolm, shm); |
| 202 | err_kfree: |
| 203 | kfree(shm); |
| 204 | err_dev_put: |
| 205 | tee_device_put(teedev); |
| 206 | return ret; |
| 207 | } |
Jens Wiklander | f902700 | 2017-11-29 14:48:26 +0200 | [diff] [blame^] | 208 | |
| 209 | /** |
| 210 | * tee_shm_alloc() - Allocate shared memory |
| 211 | * @ctx: Context that allocates the shared memory |
| 212 | * @size: Requested size of shared memory |
| 213 | * @flags: Flags setting properties for the requested shared memory. |
| 214 | * |
| 215 | * Memory allocated as global shared memory is automatically freed when the |
| 216 | * TEE file pointer is closed. The @flags field uses the bits defined by |
| 217 | * TEE_SHM_* in <linux/tee_drv.h>. TEE_SHM_MAPPED must currently always be |
| 218 | * set. If TEE_SHM_DMA_BUF global shared memory will be allocated and |
| 219 | * associated with a dma-buf handle, else driver private memory. |
| 220 | */ |
| 221 | struct tee_shm *tee_shm_alloc(struct tee_context *ctx, size_t size, u32 flags) |
| 222 | { |
| 223 | return __tee_shm_alloc(ctx, ctx->teedev, size, flags); |
| 224 | } |
Jens Wiklander | fb93896 | 2015-03-11 14:39:39 +0100 | [diff] [blame] | 225 | EXPORT_SYMBOL_GPL(tee_shm_alloc); |
| 226 | |
Jens Wiklander | f902700 | 2017-11-29 14:48:26 +0200 | [diff] [blame^] | 227 | struct tee_shm *tee_shm_priv_alloc(struct tee_device *teedev, size_t size) |
| 228 | { |
| 229 | return __tee_shm_alloc(NULL, teedev, size, TEE_SHM_MAPPED); |
| 230 | } |
| 231 | EXPORT_SYMBOL_GPL(tee_shm_priv_alloc); |
| 232 | |
| 233 | struct tee_shm *tee_shm_register(struct tee_context *ctx, unsigned long addr, |
| 234 | size_t length, u32 flags) |
| 235 | { |
| 236 | struct tee_device *teedev = ctx->teedev; |
| 237 | const u32 req_flags = TEE_SHM_DMA_BUF | TEE_SHM_USER_MAPPED; |
| 238 | struct tee_shm *shm; |
| 239 | void *ret; |
| 240 | int rc; |
| 241 | int num_pages; |
| 242 | unsigned long start; |
| 243 | |
| 244 | if (flags != req_flags) |
| 245 | return ERR_PTR(-ENOTSUPP); |
| 246 | |
| 247 | if (!tee_device_get(teedev)) |
| 248 | return ERR_PTR(-EINVAL); |
| 249 | |
| 250 | if (!teedev->desc->ops->shm_register || |
| 251 | !teedev->desc->ops->shm_unregister) { |
| 252 | tee_device_put(teedev); |
| 253 | return ERR_PTR(-ENOTSUPP); |
| 254 | } |
| 255 | |
| 256 | shm = kzalloc(sizeof(*shm), GFP_KERNEL); |
| 257 | if (!shm) { |
| 258 | ret = ERR_PTR(-ENOMEM); |
| 259 | goto err; |
| 260 | } |
| 261 | |
| 262 | shm->flags = flags | TEE_SHM_REGISTER; |
| 263 | shm->teedev = teedev; |
| 264 | shm->ctx = ctx; |
| 265 | shm->id = -1; |
| 266 | start = rounddown(addr, PAGE_SIZE); |
| 267 | shm->offset = addr - start; |
| 268 | shm->size = length; |
| 269 | num_pages = (roundup(addr + length, PAGE_SIZE) - start) / PAGE_SIZE; |
| 270 | shm->pages = kcalloc(num_pages, sizeof(*shm->pages), GFP_KERNEL); |
| 271 | if (!shm->pages) { |
| 272 | ret = ERR_PTR(-ENOMEM); |
| 273 | goto err; |
| 274 | } |
| 275 | |
| 276 | rc = get_user_pages_fast(start, num_pages, 1, shm->pages); |
| 277 | if (rc > 0) |
| 278 | shm->num_pages = rc; |
| 279 | if (rc != num_pages) { |
| 280 | if (rc > 0) |
| 281 | rc = -ENOMEM; |
| 282 | ret = ERR_PTR(rc); |
| 283 | goto err; |
| 284 | } |
| 285 | |
| 286 | mutex_lock(&teedev->mutex); |
| 287 | shm->id = idr_alloc(&teedev->idr, shm, 1, 0, GFP_KERNEL); |
| 288 | mutex_unlock(&teedev->mutex); |
| 289 | |
| 290 | if (shm->id < 0) { |
| 291 | ret = ERR_PTR(shm->id); |
| 292 | goto err; |
| 293 | } |
| 294 | |
| 295 | rc = teedev->desc->ops->shm_register(ctx, shm, shm->pages, |
| 296 | shm->num_pages); |
| 297 | if (rc) { |
| 298 | ret = ERR_PTR(rc); |
| 299 | goto err; |
| 300 | } |
| 301 | |
| 302 | if (flags & TEE_SHM_DMA_BUF) { |
| 303 | DEFINE_DMA_BUF_EXPORT_INFO(exp_info); |
| 304 | |
| 305 | exp_info.ops = &tee_shm_dma_buf_ops; |
| 306 | exp_info.size = shm->size; |
| 307 | exp_info.flags = O_RDWR; |
| 308 | exp_info.priv = shm; |
| 309 | |
| 310 | shm->dmabuf = dma_buf_export(&exp_info); |
| 311 | if (IS_ERR(shm->dmabuf)) { |
| 312 | ret = ERR_CAST(shm->dmabuf); |
| 313 | teedev->desc->ops->shm_unregister(ctx, shm); |
| 314 | goto err; |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | mutex_lock(&teedev->mutex); |
| 319 | list_add_tail(&shm->link, &ctx->list_shm); |
| 320 | mutex_unlock(&teedev->mutex); |
| 321 | |
| 322 | return shm; |
| 323 | err: |
| 324 | if (shm) { |
| 325 | size_t n; |
| 326 | |
| 327 | if (shm->id >= 0) { |
| 328 | mutex_lock(&teedev->mutex); |
| 329 | idr_remove(&teedev->idr, shm->id); |
| 330 | mutex_unlock(&teedev->mutex); |
| 331 | } |
| 332 | for (n = 0; n < shm->num_pages; n++) |
| 333 | put_page(shm->pages[n]); |
| 334 | kfree(shm->pages); |
| 335 | } |
| 336 | kfree(shm); |
| 337 | tee_device_put(teedev); |
| 338 | return ret; |
| 339 | } |
| 340 | EXPORT_SYMBOL_GPL(tee_shm_register); |
| 341 | |
Jens Wiklander | fb93896 | 2015-03-11 14:39:39 +0100 | [diff] [blame] | 342 | /** |
| 343 | * tee_shm_get_fd() - Increase reference count and return file descriptor |
| 344 | * @shm: Shared memory handle |
| 345 | * @returns user space file descriptor to shared memory |
| 346 | */ |
| 347 | int tee_shm_get_fd(struct tee_shm *shm) |
| 348 | { |
Jens Wiklander | fb93896 | 2015-03-11 14:39:39 +0100 | [diff] [blame] | 349 | int fd; |
| 350 | |
Jens Wiklander | f902700 | 2017-11-29 14:48:26 +0200 | [diff] [blame^] | 351 | if (!(shm->flags & TEE_SHM_DMA_BUF)) |
Jens Wiklander | fb93896 | 2015-03-11 14:39:39 +0100 | [diff] [blame] | 352 | return -EINVAL; |
| 353 | |
| 354 | fd = dma_buf_fd(shm->dmabuf, O_CLOEXEC); |
| 355 | if (fd >= 0) |
| 356 | get_dma_buf(shm->dmabuf); |
| 357 | return fd; |
| 358 | } |
| 359 | |
| 360 | /** |
| 361 | * tee_shm_free() - Free shared memory |
| 362 | * @shm: Handle to shared memory to free |
| 363 | */ |
| 364 | void tee_shm_free(struct tee_shm *shm) |
| 365 | { |
| 366 | /* |
| 367 | * dma_buf_put() decreases the dmabuf reference counter and will |
| 368 | * call tee_shm_release() when the last reference is gone. |
| 369 | * |
| 370 | * In the case of driver private memory we call tee_shm_release |
| 371 | * directly instead as it doesn't have a reference counter. |
| 372 | */ |
| 373 | if (shm->flags & TEE_SHM_DMA_BUF) |
| 374 | dma_buf_put(shm->dmabuf); |
| 375 | else |
| 376 | tee_shm_release(shm); |
| 377 | } |
| 378 | EXPORT_SYMBOL_GPL(tee_shm_free); |
| 379 | |
| 380 | /** |
| 381 | * tee_shm_va2pa() - Get physical address of a virtual address |
| 382 | * @shm: Shared memory handle |
| 383 | * @va: Virtual address to tranlsate |
| 384 | * @pa: Returned physical address |
| 385 | * @returns 0 on success and < 0 on failure |
| 386 | */ |
| 387 | int tee_shm_va2pa(struct tee_shm *shm, void *va, phys_addr_t *pa) |
| 388 | { |
Jens Wiklander | f902700 | 2017-11-29 14:48:26 +0200 | [diff] [blame^] | 389 | if (!(shm->flags & TEE_SHM_MAPPED)) |
| 390 | return -EINVAL; |
Jens Wiklander | fb93896 | 2015-03-11 14:39:39 +0100 | [diff] [blame] | 391 | /* Check that we're in the range of the shm */ |
| 392 | if ((char *)va < (char *)shm->kaddr) |
| 393 | return -EINVAL; |
| 394 | if ((char *)va >= ((char *)shm->kaddr + shm->size)) |
| 395 | return -EINVAL; |
| 396 | |
| 397 | return tee_shm_get_pa( |
| 398 | shm, (unsigned long)va - (unsigned long)shm->kaddr, pa); |
| 399 | } |
| 400 | EXPORT_SYMBOL_GPL(tee_shm_va2pa); |
| 401 | |
| 402 | /** |
| 403 | * tee_shm_pa2va() - Get virtual address of a physical address |
| 404 | * @shm: Shared memory handle |
| 405 | * @pa: Physical address to tranlsate |
| 406 | * @va: Returned virtual address |
| 407 | * @returns 0 on success and < 0 on failure |
| 408 | */ |
| 409 | int tee_shm_pa2va(struct tee_shm *shm, phys_addr_t pa, void **va) |
| 410 | { |
Jens Wiklander | f902700 | 2017-11-29 14:48:26 +0200 | [diff] [blame^] | 411 | if (!(shm->flags & TEE_SHM_MAPPED)) |
| 412 | return -EINVAL; |
Jens Wiklander | fb93896 | 2015-03-11 14:39:39 +0100 | [diff] [blame] | 413 | /* Check that we're in the range of the shm */ |
| 414 | if (pa < shm->paddr) |
| 415 | return -EINVAL; |
| 416 | if (pa >= (shm->paddr + shm->size)) |
| 417 | return -EINVAL; |
| 418 | |
| 419 | if (va) { |
| 420 | void *v = tee_shm_get_va(shm, pa - shm->paddr); |
| 421 | |
| 422 | if (IS_ERR(v)) |
| 423 | return PTR_ERR(v); |
| 424 | *va = v; |
| 425 | } |
| 426 | return 0; |
| 427 | } |
| 428 | EXPORT_SYMBOL_GPL(tee_shm_pa2va); |
| 429 | |
| 430 | /** |
| 431 | * tee_shm_get_va() - Get virtual address of a shared memory plus an offset |
| 432 | * @shm: Shared memory handle |
| 433 | * @offs: Offset from start of this shared memory |
| 434 | * @returns virtual address of the shared memory + offs if offs is within |
| 435 | * the bounds of this shared memory, else an ERR_PTR |
| 436 | */ |
| 437 | void *tee_shm_get_va(struct tee_shm *shm, size_t offs) |
| 438 | { |
Jens Wiklander | f902700 | 2017-11-29 14:48:26 +0200 | [diff] [blame^] | 439 | if (!(shm->flags & TEE_SHM_MAPPED)) |
| 440 | return ERR_PTR(-EINVAL); |
Jens Wiklander | fb93896 | 2015-03-11 14:39:39 +0100 | [diff] [blame] | 441 | if (offs >= shm->size) |
| 442 | return ERR_PTR(-EINVAL); |
| 443 | return (char *)shm->kaddr + offs; |
| 444 | } |
| 445 | EXPORT_SYMBOL_GPL(tee_shm_get_va); |
| 446 | |
| 447 | /** |
| 448 | * tee_shm_get_pa() - Get physical address of a shared memory plus an offset |
| 449 | * @shm: Shared memory handle |
| 450 | * @offs: Offset from start of this shared memory |
| 451 | * @pa: Physical address to return |
| 452 | * @returns 0 if offs is within the bounds of this shared memory, else an |
| 453 | * error code. |
| 454 | */ |
| 455 | int tee_shm_get_pa(struct tee_shm *shm, size_t offs, phys_addr_t *pa) |
| 456 | { |
| 457 | if (offs >= shm->size) |
| 458 | return -EINVAL; |
| 459 | if (pa) |
| 460 | *pa = shm->paddr + offs; |
| 461 | return 0; |
| 462 | } |
| 463 | EXPORT_SYMBOL_GPL(tee_shm_get_pa); |
| 464 | |
| 465 | /** |
| 466 | * tee_shm_get_from_id() - Find shared memory object and increase reference |
| 467 | * count |
| 468 | * @ctx: Context owning the shared memory |
| 469 | * @id: Id of shared memory object |
| 470 | * @returns a pointer to 'struct tee_shm' on success or an ERR_PTR on failure |
| 471 | */ |
| 472 | struct tee_shm *tee_shm_get_from_id(struct tee_context *ctx, int id) |
| 473 | { |
| 474 | struct tee_device *teedev; |
| 475 | struct tee_shm *shm; |
| 476 | |
| 477 | if (!ctx) |
| 478 | return ERR_PTR(-EINVAL); |
| 479 | |
| 480 | teedev = ctx->teedev; |
| 481 | mutex_lock(&teedev->mutex); |
| 482 | shm = idr_find(&teedev->idr, id); |
| 483 | if (!shm || shm->ctx != ctx) |
| 484 | shm = ERR_PTR(-EINVAL); |
| 485 | else if (shm->flags & TEE_SHM_DMA_BUF) |
| 486 | get_dma_buf(shm->dmabuf); |
| 487 | mutex_unlock(&teedev->mutex); |
| 488 | return shm; |
| 489 | } |
| 490 | EXPORT_SYMBOL_GPL(tee_shm_get_from_id); |
| 491 | |
| 492 | /** |
| 493 | * tee_shm_get_id() - Get id of a shared memory object |
| 494 | * @shm: Shared memory handle |
| 495 | * @returns id |
| 496 | */ |
| 497 | int tee_shm_get_id(struct tee_shm *shm) |
| 498 | { |
| 499 | return shm->id; |
| 500 | } |
| 501 | EXPORT_SYMBOL_GPL(tee_shm_get_id); |
| 502 | |
| 503 | /** |
| 504 | * tee_shm_put() - Decrease reference count on a shared memory handle |
| 505 | * @shm: Shared memory handle |
| 506 | */ |
| 507 | void tee_shm_put(struct tee_shm *shm) |
| 508 | { |
| 509 | if (shm->flags & TEE_SHM_DMA_BUF) |
| 510 | dma_buf_put(shm->dmabuf); |
| 511 | } |
| 512 | EXPORT_SYMBOL_GPL(tee_shm_put); |