Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: MIT
0002 /*
0003  * Copyright 2019 Advanced Micro Devices, Inc.
0004  */
0005 
0006 #include <linux/slab.h>
0007 #include <linux/tee_drv.h>
0008 #include <linux/psp-sev.h>
0009 #include "amdtee_private.h"
0010 
0011 static int pool_op_alloc(struct tee_shm_pool *pool, struct tee_shm *shm,
0012              size_t size, size_t align)
0013 {
0014     unsigned int order = get_order(size);
0015     unsigned long va;
0016     int rc;
0017 
0018     /*
0019      * Ignore alignment since this is already going to be page aligned
0020      * and there's no need for any larger alignment.
0021      */
0022     va = __get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
0023     if (!va)
0024         return -ENOMEM;
0025 
0026     shm->kaddr = (void *)va;
0027     shm->paddr = __psp_pa((void *)va);
0028     shm->size = PAGE_SIZE << order;
0029 
0030     /* Map the allocated memory in to TEE */
0031     rc = amdtee_map_shmem(shm);
0032     if (rc) {
0033         free_pages(va, order);
0034         shm->kaddr = NULL;
0035         return rc;
0036     }
0037 
0038     return 0;
0039 }
0040 
0041 static void pool_op_free(struct tee_shm_pool *pool, struct tee_shm *shm)
0042 {
0043     /* Unmap the shared memory from TEE */
0044     amdtee_unmap_shmem(shm);
0045     free_pages((unsigned long)shm->kaddr, get_order(shm->size));
0046     shm->kaddr = NULL;
0047 }
0048 
0049 static void pool_op_destroy_pool(struct tee_shm_pool *pool)
0050 {
0051     kfree(pool);
0052 }
0053 
0054 static const struct tee_shm_pool_ops pool_ops = {
0055     .alloc = pool_op_alloc,
0056     .free = pool_op_free,
0057     .destroy_pool = pool_op_destroy_pool,
0058 };
0059 
0060 struct tee_shm_pool *amdtee_config_shm(void)
0061 {
0062     struct tee_shm_pool *pool = kzalloc(sizeof(*pool), GFP_KERNEL);
0063 
0064     if (!pool)
0065         return ERR_PTR(-ENOMEM);
0066 
0067     pool->ops = &pool_ops;
0068 
0069     return pool;
0070 }