1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2015, Linaro Limited
4 * Copyright (c) 2017, EPAM Systems
6 #include <linux/device.h>
7 #include <linux/dma-buf.h>
8 #include <linux/genalloc.h>
9 #include <linux/slab.h>
10 #include <linux/tee_drv.h>
11 #include "optee_private.h"
12 #include "optee_smc.h"
15 static int pool_op_alloc(struct tee_shm_pool_mgr *poolm,
16 struct tee_shm *shm, size_t size)
18 unsigned int order = get_order(size);
22 page = alloc_pages(GFP_KERNEL | __GFP_ZERO, order);
26 shm->kaddr = page_address(page);
27 shm->paddr = page_to_phys(page);
28 shm->size = PAGE_SIZE << order;
30 if (shm->flags & TEE_SHM_DMA_BUF) {
31 shm->flags |= TEE_SHM_REGISTER;
32 rc = optee_shm_register(shm->ctx, shm, &page, 1 << order,
33 (unsigned long)shm->kaddr);
39 static void pool_op_free(struct tee_shm_pool_mgr *poolm,
42 if (shm->flags & TEE_SHM_DMA_BUF)
43 optee_shm_unregister(shm->ctx, shm);
45 free_pages((unsigned long)shm->kaddr, get_order(shm->size));
49 static void pool_op_destroy_poolmgr(struct tee_shm_pool_mgr *poolm)
54 static const struct tee_shm_pool_mgr_ops pool_ops = {
55 .alloc = pool_op_alloc,
57 .destroy_poolmgr = pool_op_destroy_poolmgr,
61 * optee_shm_pool_alloc_pages() - create page-based allocator pool
63 * This pool is used when OP-TEE supports dymanic SHM. In this case
64 * command buffers and such are allocated from kernel's own memory.
66 struct tee_shm_pool_mgr *optee_shm_pool_alloc_pages(void)
68 struct tee_shm_pool_mgr *mgr = kzalloc(sizeof(*mgr), GFP_KERNEL);
71 return ERR_PTR(-ENOMEM);