Brad Bishop | bec4ebc | 2022-08-03 09:55:16 -0400 | [diff] [blame] | 1 | From eafffa586795e3cb485310fbd287322c9c7dc3bb Mon Sep 17 00:00:00 2001 |
| 2 | From: Jens Wiklander <jens.wiklander@linaro.org> |
| 3 | Date: Thu, 25 Mar 2021 15:08:52 +0100 |
| 4 | Subject: [PATCH 18/22] optee: add a FF-A memory pool |
| 5 | |
| 6 | Adds a memory pool to be used when the driver uses FF-A [1] as transport |
| 7 | layer. |
| 8 | |
| 9 | [1] https://developer.arm.com/documentation/den0077/latest |
| 10 | Signed-off-by: Jens Wiklander <jens.wiklander@linaro.org> |
| 11 | |
| 12 | Upstream-Status: Pending [Not submitted to upstream yet] |
| 13 | --- |
| 14 | drivers/tee/optee/shm_pool.c | 65 +++++++++++++++++++++++++++++++++--- |
| 15 | drivers/tee/optee/shm_pool.h | 1 + |
| 16 | 2 files changed, 61 insertions(+), 5 deletions(-) |
| 17 | |
| 18 | diff --git a/drivers/tee/optee/shm_pool.c b/drivers/tee/optee/shm_pool.c |
| 19 | index d767eebf30bd..d2116cb39c8b 100644 |
| 20 | --- a/drivers/tee/optee/shm_pool.c |
| 21 | +++ b/drivers/tee/optee/shm_pool.c |
| 22 | @@ -12,8 +12,14 @@ |
| 23 | #include "optee_smc.h" |
| 24 | #include "shm_pool.h" |
| 25 | |
| 26 | -static int pool_op_alloc(struct tee_shm_pool_mgr *poolm, |
| 27 | - struct tee_shm *shm, size_t size) |
| 28 | +static int |
| 29 | +pool_op_alloc_helper(struct tee_shm_pool_mgr *poolm, |
| 30 | + struct tee_shm *shm, size_t size, |
| 31 | + int (*shm_register)(struct tee_context *ctx, |
| 32 | + struct tee_shm *shm, |
| 33 | + struct page **pages, |
| 34 | + size_t num_pages, |
| 35 | + unsigned long start)) |
| 36 | { |
| 37 | unsigned int order = get_order(size); |
| 38 | struct page *page; |
| 39 | @@ -27,7 +33,7 @@ static int pool_op_alloc(struct tee_shm_pool_mgr *poolm, |
| 40 | shm->paddr = page_to_phys(page); |
| 41 | shm->size = PAGE_SIZE << order; |
| 42 | |
| 43 | - if (shm->flags & TEE_SHM_DMA_BUF) { |
| 44 | + if (shm_register) { |
| 45 | unsigned int nr_pages = 1 << order, i; |
| 46 | struct page **pages; |
| 47 | |
| 48 | @@ -41,14 +47,23 @@ static int pool_op_alloc(struct tee_shm_pool_mgr *poolm, |
| 49 | } |
| 50 | |
| 51 | shm->flags |= TEE_SHM_REGISTER; |
| 52 | - rc = optee_shm_register(shm->ctx, shm, pages, nr_pages, |
| 53 | - (unsigned long)shm->kaddr); |
| 54 | + rc = shm_register(shm->ctx, shm, pages, nr_pages, |
| 55 | + (unsigned long)shm->kaddr); |
| 56 | kfree(pages); |
| 57 | } |
| 58 | |
| 59 | return rc; |
| 60 | } |
| 61 | |
| 62 | +static int pool_op_alloc(struct tee_shm_pool_mgr *poolm, |
| 63 | + struct tee_shm *shm, size_t size) |
| 64 | +{ |
| 65 | + if (!(shm->flags & TEE_SHM_DMA_BUF)) |
| 66 | + return pool_op_alloc_helper(poolm, shm, size, NULL); |
| 67 | + |
| 68 | + return pool_op_alloc_helper(poolm, shm, size, optee_shm_register); |
| 69 | +} |
| 70 | + |
| 71 | static void pool_op_free(struct tee_shm_pool_mgr *poolm, |
| 72 | struct tee_shm *shm) |
| 73 | { |
| 74 | @@ -87,3 +102,43 @@ struct tee_shm_pool_mgr *optee_shm_pool_alloc_pages(void) |
| 75 | |
| 76 | return mgr; |
| 77 | } |
| 78 | + |
| 79 | +#ifdef CONFIG_ARM_FFA_TRANSPORT |
| 80 | +static int pool_ffa_op_alloc(struct tee_shm_pool_mgr *poolm, |
| 81 | + struct tee_shm *shm, size_t size) |
| 82 | +{ |
| 83 | + return pool_op_alloc_helper(poolm, shm, size, optee_ffa_shm_register); |
| 84 | +} |
| 85 | + |
| 86 | +static void pool_ffa_op_free(struct tee_shm_pool_mgr *poolm, |
| 87 | + struct tee_shm *shm) |
| 88 | +{ |
| 89 | + optee_ffa_shm_unregister(shm->ctx, shm); |
| 90 | + free_pages((unsigned long)shm->kaddr, get_order(shm->size)); |
| 91 | + shm->kaddr = NULL; |
| 92 | +} |
| 93 | + |
| 94 | +static const struct tee_shm_pool_mgr_ops pool_ffa_ops = { |
| 95 | + .alloc = pool_ffa_op_alloc, |
| 96 | + .free = pool_ffa_op_free, |
| 97 | + .destroy_poolmgr = pool_op_destroy_poolmgr, |
| 98 | +}; |
| 99 | + |
| 100 | +/** |
| 101 | + * optee_ffa_shm_pool_alloc_pages() - create page-based allocator pool |
| 102 | + * |
| 103 | + * This pool is used with OP-TEE over FF-A. In this case command buffers |
| 104 | + * and such are allocated from kernel's own memory. |
| 105 | + */ |
| 106 | +struct tee_shm_pool_mgr *optee_ffa_shm_pool_alloc_pages(void) |
| 107 | +{ |
| 108 | + struct tee_shm_pool_mgr *mgr = kzalloc(sizeof(*mgr), GFP_KERNEL); |
| 109 | + |
| 110 | + if (!mgr) |
| 111 | + return ERR_PTR(-ENOMEM); |
| 112 | + |
| 113 | + mgr->ops = &pool_ffa_ops; |
| 114 | + |
| 115 | + return mgr; |
| 116 | +} |
| 117 | +#endif /*CONFIG_ARM_FFA_TRANSPORT*/ |
| 118 | diff --git a/drivers/tee/optee/shm_pool.h b/drivers/tee/optee/shm_pool.h |
| 119 | index 28109d991c4b..34c5fd74a3ff 100644 |
| 120 | --- a/drivers/tee/optee/shm_pool.h |
| 121 | +++ b/drivers/tee/optee/shm_pool.h |
| 122 | @@ -10,5 +10,6 @@ |
| 123 | #include <linux/tee_drv.h> |
| 124 | |
| 125 | struct tee_shm_pool_mgr *optee_shm_pool_alloc_pages(void); |
| 126 | +struct tee_shm_pool_mgr *optee_ffa_shm_pool_alloc_pages(void); |
| 127 | |
| 128 | #endif |
| 129 | -- |
| 130 | 2.17.1 |
| 131 | |