| From 21d0abb187f1aff8ca9d3b1e7bbae95cf50eb732 Mon Sep 17 00:00:00 2001 |
| From: Devaraj Rangasamy <Devaraj.Rangasamy@amd.com> |
| Date: Wed, 30 Aug 2023 01:49:43 +0530 |
| Subject: [PATCH] FROMLIST: tee: amdtee: add support for CMA buffer allocations |
| |
| amdtee driver shall use CMA region for contiguous |
| buffer allocation, if CMA is available. |
| |
| since CMA and DMA contiguous APIs are not exported, |
| this support is enabled only when amdtee is built |
| as a builtin driver. |
| |
| Signed-off-by: Devaraj Rangasamy <Devaraj.Rangasamy@amd.com> |
| Signed-off-by: SivaSangeetha SK <SivaSangeetha.SK@amd.com> |
| Reviewed-by: Rijo Thomas <Rijo-john.Thomas@amd.com> |
| (am from https://lore.kernel.org/r/8c7f0641582d53c20c6657f0ebb4d3a1279e6f9b.1693340098.git.Devaraj.Rangasamy@amd.com) |
| |
| BUG=b:285231416 |
| UPSTREAM-TASK=b:308177821 |
| TEST=CQ |
| |
| Change-Id: I01ac17b777569ee668b53aac24eddd9ece37df11 |
| Signed-off-by: Tim Van Patten <timvp@google.com> |
| Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/kernel/+/4982593 |
| Reviewed-by: Mark Hasemeyer <markhas@google.com> |
| --- |
| drivers/tee/amdtee/shm_pool.c | 51 +++++++++++++++++++++++++++++++++-- |
| 1 file changed, 49 insertions(+), 2 deletions(-) |
| |
| diff --git a/drivers/tee/amdtee/shm_pool.c b/drivers/tee/amdtee/shm_pool.c |
| index 7251dedd2890cc9eb88cdb3a8936b46cb262576d..613ce162b807f9546bd1f1fb4affb0fa2432f10f 100644 |
| --- a/drivers/tee/amdtee/shm_pool.c |
| +++ b/drivers/tee/amdtee/shm_pool.c |
| @@ -5,10 +5,50 @@ |
| |
| #include <linux/slab.h> |
| #include <linux/mm.h> |
| +#include <linux/dma-map-ops.h> |
| #include <linux/tee_core.h> |
| #include <linux/psp.h> |
| #include "amdtee_private.h" |
| |
| +#if IS_BUILTIN(CONFIG_AMDTEE) && IS_ENABLED(CONFIG_DMA_CMA) |
| +static void *alloc_from_cma(size_t size) |
| +{ |
| + |
| + int nr_pages = size >> PAGE_SHIFT; |
| + struct page *page; |
| + |
| + page = dma_alloc_from_contiguous(NULL, nr_pages, 0, false); |
| + if (page) |
| + return page_to_virt(page); |
| + |
| + return NULL; |
| +} |
| + |
| +static bool free_from_cma(struct tee_shm *shm) |
| +{ |
| + |
| + int nr_pages; |
| + struct page *page; |
| + |
| + if (!dev_get_cma_area(NULL)) |
| + return false; |
| + |
| + nr_pages = shm->size >> PAGE_SHIFT; |
| + page = virt_to_page(shm->kaddr); |
| + return dma_release_from_contiguous(NULL, page, nr_pages); |
| +} |
| +#else |
| +static void *alloc_from_cma(size_t size) |
| +{ |
| + return NULL; |
| +} |
| + |
| +static bool free_from_cma(struct tee_shm *shm) |
| +{ |
| + return false; |
| +} |
| +#endif |
| + |
| static int pool_op_alloc(struct tee_shm_pool *pool, struct tee_shm *shm, |
| size_t size, size_t align) |
| { |
| @@ -17,7 +57,11 @@ static int pool_op_alloc(struct tee_shm_pool *pool, struct tee_shm *shm, |
| |
| size = PAGE_ALIGN(size); |
| |
| - va = alloc_pages_exact(size, GFP_KERNEL | __GFP_ZERO); |
| + va = alloc_from_cma(size); |
| + |
| + if (!va) |
| + va = alloc_pages_exact(size, GFP_KERNEL | __GFP_ZERO); |
| + |
| if (!va) |
| return -ENOMEM; |
| |
| @@ -40,7 +84,10 @@ static void pool_op_free(struct tee_shm_pool *pool, struct tee_shm *shm) |
| { |
| /* Unmap the shared memory from TEE */ |
| amdtee_unmap_shmem(shm); |
| - free_pages_exact(shm->kaddr, shm->size); |
| + |
| + if (!free_from_cma(shm)) |
| + free_pages_exact(shm->kaddr, shm->size); |
| + |
| shm->kaddr = NULL; |
| } |
| |
| -- |
| 2.45.1.288.g0e0cd299f1-goog |
| |