Linux 4.16.11
[linux/fpc-iii.git] / drivers / tee / optee / shm_pool.c
blob49397813fff1e5e0f65098c27a29641c407d9ac6
1 /*
2 * Copyright (c) 2015, Linaro Limited
3 * Copyright (c) 2017, EPAM Systems
5 * This software is licensed under the terms of the GNU General Public
6 * License version 2, as published by the Free Software Foundation, and
7 * may be copied, distributed, and modified under those terms.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
15 #include <linux/device.h>
16 #include <linux/dma-buf.h>
17 #include <linux/genalloc.h>
18 #include <linux/slab.h>
19 #include <linux/tee_drv.h>
20 #include "optee_private.h"
21 #include "optee_smc.h"
22 #include "shm_pool.h"
24 static int pool_op_alloc(struct tee_shm_pool_mgr *poolm,
25 struct tee_shm *shm, size_t size)
27 unsigned int order = get_order(size);
28 struct page *page;
30 page = alloc_pages(GFP_KERNEL | __GFP_ZERO, order);
31 if (!page)
32 return -ENOMEM;
34 shm->kaddr = page_address(page);
35 shm->paddr = page_to_phys(page);
36 shm->size = PAGE_SIZE << order;
38 return 0;
41 static void pool_op_free(struct tee_shm_pool_mgr *poolm,
42 struct tee_shm *shm)
44 free_pages((unsigned long)shm->kaddr, get_order(shm->size));
45 shm->kaddr = NULL;
48 static void pool_op_destroy_poolmgr(struct tee_shm_pool_mgr *poolm)
50 kfree(poolm);
53 static const struct tee_shm_pool_mgr_ops pool_ops = {
54 .alloc = pool_op_alloc,
55 .free = pool_op_free,
56 .destroy_poolmgr = pool_op_destroy_poolmgr,
59 /**
60 * optee_shm_pool_alloc_pages() - create page-based allocator pool
62 * This pool is used when OP-TEE supports dymanic SHM. In this case
63 * command buffers and such are allocated from kernel's own memory.
65 struct tee_shm_pool_mgr *optee_shm_pool_alloc_pages(void)
67 struct tee_shm_pool_mgr *mgr = kzalloc(sizeof(*mgr), GFP_KERNEL);
69 if (!mgr)
70 return ERR_PTR(-ENOMEM);
72 mgr->ops = &pool_ops;
74 return mgr;