71 lines
1.4 KiB
C
Raw Normal View History

2026-01-21 18:59:54 +08:00
// SPDX-License-Identifier: MIT
/*
* Copyright 2019 Advanced Micro Devices, Inc.
*/
#include <linux/slab.h>
#include <linux/tee_drv.h>
2026-01-29 22:25:33 +08:00
#include <linux/psp.h>
2026-01-21 18:59:54 +08:00
#include "amdtee_private.h"
2026-01-29 22:25:33 +08:00
static int pool_op_alloc(struct tee_shm_pool *pool, struct tee_shm *shm,
size_t size, size_t align)
2026-01-21 18:59:54 +08:00
{
unsigned int order = get_order(size);
unsigned long va;
int rc;
2026-01-29 22:25:33 +08:00
/*
* Ignore alignment since this is already going to be page aligned
* and there's no need for any larger alignment.
*/
2026-01-21 18:59:54 +08:00
va = __get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
if (!va)
return -ENOMEM;
shm->kaddr = (void *)va;
shm->paddr = __psp_pa((void *)va);
shm->size = PAGE_SIZE << order;
/* Map the allocated memory in to TEE */
rc = amdtee_map_shmem(shm);
if (rc) {
free_pages(va, order);
shm->kaddr = NULL;
return rc;
}
return 0;
}
2026-01-29 22:25:33 +08:00
static void pool_op_free(struct tee_shm_pool *pool, struct tee_shm *shm)
2026-01-21 18:59:54 +08:00
{
/* Unmap the shared memory from TEE */
amdtee_unmap_shmem(shm);
free_pages((unsigned long)shm->kaddr, get_order(shm->size));
shm->kaddr = NULL;
}
2026-01-29 22:25:33 +08:00
static void pool_op_destroy_pool(struct tee_shm_pool *pool)
2026-01-21 18:59:54 +08:00
{
2026-01-29 22:25:33 +08:00
kfree(pool);
2026-01-21 18:59:54 +08:00
}
2026-01-29 22:25:33 +08:00
static const struct tee_shm_pool_ops pool_ops = {
2026-01-21 18:59:54 +08:00
.alloc = pool_op_alloc,
.free = pool_op_free,
2026-01-29 22:25:33 +08:00
.destroy_pool = pool_op_destroy_pool,
2026-01-21 18:59:54 +08:00
};
struct tee_shm_pool *amdtee_config_shm(void)
{
2026-01-29 22:25:33 +08:00
struct tee_shm_pool *pool = kzalloc(sizeof(*pool), GFP_KERNEL);
2026-01-21 18:59:54 +08:00
2026-01-29 22:25:33 +08:00
if (!pool)
return ERR_PTR(-ENOMEM);
2026-01-21 18:59:54 +08:00
2026-01-29 22:25:33 +08:00
pool->ops = &pool_ops;
2026-01-21 18:59:54 +08:00
2026-01-29 22:25:33 +08:00
return pool;
2026-01-21 18:59:54 +08:00
}