hc
2023-12-11 d2ccde1c8e90d38cee87a1b0309ad2827f3fd30d
kernel/drivers/tee/optee/shm_pool.c
....@@ -1,16 +1,7 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * Copyright (c) 2015, Linaro Limited
34 * 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
- *
145 */
156 #include <linux/device.h>
167 #include <linux/dma-buf.h>
....@@ -26,6 +17,7 @@
2617 {
2718 unsigned int order = get_order(size);
2819 struct page *page;
20
+ int rc = 0;
2921
3022 page = alloc_pages(GFP_KERNEL | __GFP_ZERO, order);
3123 if (!page)
....@@ -35,12 +27,44 @@
3527 shm->paddr = page_to_phys(page);
3628 shm->size = PAGE_SIZE << order;
3729
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
+
3855 return 0;
56
+
57
+err:
58
+ __free_pages(page, order);
59
+ return rc;
3960 }
4061
4162 static void pool_op_free(struct tee_shm_pool_mgr *poolm,
4263 struct tee_shm *shm)
4364 {
65
+ if (!(shm->flags & TEE_SHM_PRIV))
66
+ optee_shm_unregister(shm->ctx, shm);
67
+
4468 free_pages((unsigned long)shm->kaddr, get_order(shm->size));
4569 shm->kaddr = NULL;
4670 }