1 // SPDX-License-Identifier: MIT
3 * Copyright 2019 Advanced Micro Devices, Inc.
6 #include <linux/slab.h>
7 #include <linux/tee_drv.h>
8 #include <linux/psp-sev.h>
9 #include "amdtee_private.h"
11 static int pool_op_alloc(struct tee_shm_pool_mgr *poolm, struct tee_shm *shm,
14 unsigned int order = get_order(size);
18 va = __get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
22 shm->kaddr = (void *)va;
23 shm->paddr = __psp_pa((void *)va);
24 shm->size = PAGE_SIZE << order;
26 /* Map the allocated memory in to TEE */
27 rc = amdtee_map_shmem(shm);
29 free_pages(va, order);
37 static void pool_op_free(struct tee_shm_pool_mgr *poolm, struct tee_shm *shm)
39 /* Unmap the shared memory from TEE */
40 amdtee_unmap_shmem(shm);
41 free_pages((unsigned long)shm->kaddr, get_order(shm->size));
45 static void pool_op_destroy_poolmgr(struct tee_shm_pool_mgr *poolm)
50 static const struct tee_shm_pool_mgr_ops pool_ops = {
51 .alloc = pool_op_alloc,
53 .destroy_poolmgr = pool_op_destroy_poolmgr,
56 static struct tee_shm_pool_mgr *pool_mem_mgr_alloc(void)
58 struct tee_shm_pool_mgr *mgr = kzalloc(sizeof(*mgr), GFP_KERNEL);
61 return ERR_PTR(-ENOMEM);
68 struct tee_shm_pool *amdtee_config_shm(void)
70 struct tee_shm_pool_mgr *priv_mgr;
71 struct tee_shm_pool_mgr *dmabuf_mgr;
74 rc = pool_mem_mgr_alloc();
79 rc = pool_mem_mgr_alloc();
81 tee_shm_pool_mgr_destroy(priv_mgr);
86 rc = tee_shm_pool_alloc(priv_mgr, dmabuf_mgr);
88 tee_shm_pool_mgr_destroy(priv_mgr);
89 tee_shm_pool_mgr_destroy(dmabuf_mgr);