| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * Copyright (c) 2015, Linaro Limited |
|---|
| 3 | 4 | * Copyright (c) 2017, EPAM Systems |
|---|
| 4 | | - * |
|---|
| 5 | | - * This software is licensed under the terms of the GNU General Public |
|---|
| 6 | | - * License version 2, as published by the Free Software Foundation, and |
|---|
| 7 | | - * may be copied, distributed, and modified under those terms. |
|---|
| 8 | | - * |
|---|
| 9 | | - * This program is distributed in the hope that it will be useful, |
|---|
| 10 | | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 11 | | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 12 | | - * GNU General Public License for more details. |
|---|
| 13 | | - * |
|---|
| 14 | 5 | */ |
|---|
| 15 | 6 | #include <linux/device.h> |
|---|
| 16 | 7 | #include <linux/dma-buf.h> |
|---|
| .. | .. |
|---|
| 26 | 17 | { |
|---|
| 27 | 18 | unsigned int order = get_order(size); |
|---|
| 28 | 19 | struct page *page; |
|---|
| 20 | + int rc = 0; |
|---|
| 29 | 21 | |
|---|
| 30 | 22 | page = alloc_pages(GFP_KERNEL | __GFP_ZERO, order); |
|---|
| 31 | 23 | if (!page) |
|---|
| .. | .. |
|---|
| 35 | 27 | shm->paddr = page_to_phys(page); |
|---|
| 36 | 28 | shm->size = PAGE_SIZE << order; |
|---|
| 37 | 29 | |
|---|
| 30 | + /* |
|---|
| 31 | + * Shared memory private to the OP-TEE driver doesn't need |
|---|
| 32 | + * to be registered with OP-TEE. |
|---|
| 33 | + */ |
|---|
| 34 | + if (!(shm->flags & TEE_SHM_PRIV)) { |
|---|
| 35 | + unsigned int nr_pages = 1 << order, i; |
|---|
| 36 | + struct page **pages; |
|---|
| 37 | + |
|---|
| 38 | + pages = kcalloc(nr_pages, sizeof(pages), GFP_KERNEL); |
|---|
| 39 | + if (!pages) { |
|---|
| 40 | + rc = -ENOMEM; |
|---|
| 41 | + goto err; |
|---|
| 42 | + } |
|---|
| 43 | + |
|---|
| 44 | + for (i = 0; i < nr_pages; i++) |
|---|
| 45 | + pages[i] = page + i; |
|---|
| 46 | + |
|---|
| 47 | + shm->flags |= TEE_SHM_REGISTER; |
|---|
| 48 | + rc = optee_shm_register(shm->ctx, shm, pages, nr_pages, |
|---|
| 49 | + (unsigned long)shm->kaddr); |
|---|
| 50 | + kfree(pages); |
|---|
| 51 | + if (rc) |
|---|
| 52 | + goto err; |
|---|
| 53 | + } |
|---|
| 54 | + |
|---|
| 38 | 55 | return 0; |
|---|
| 56 | + |
|---|
| 57 | +err: |
|---|
| 58 | + __free_pages(page, order); |
|---|
| 59 | + return rc; |
|---|
| 39 | 60 | } |
|---|
| 40 | 61 | |
|---|
| 41 | 62 | static void pool_op_free(struct tee_shm_pool_mgr *poolm, |
|---|
| 42 | 63 | struct tee_shm *shm) |
|---|
| 43 | 64 | { |
|---|
| 65 | + if (!(shm->flags & TEE_SHM_PRIV)) |
|---|
| 66 | + optee_shm_unregister(shm->ctx, shm); |
|---|
| 67 | + |
|---|
| 44 | 68 | free_pages((unsigned long)shm->kaddr, get_order(shm->size)); |
|---|
| 45 | 69 | shm->kaddr = NULL; |
|---|
| 46 | 70 | } |
|---|