Skip to content

Commit abd135b

Browse files
lorcjenswi-linaro
authored andcommitted
tee: optee: add optee-specific shared pool implementation
This is simple pool that uses kernel page allocator. This pool can be used in case OP-TEE supports dynamic shared memory. Signed-off-by: Volodymyr Babchuk <[email protected]> Signed-off-by: Jens Wiklander <[email protected]>
1 parent d885cc5 commit abd135b

File tree

3 files changed

+99
-0
lines changed

3 files changed

+99
-0
lines changed

drivers/tee/optee/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ optee-objs += core.o
44
optee-objs += call.o
55
optee-objs += rpc.o
66
optee-objs += supp.o
7+
optee-objs += shm_pool.o

drivers/tee/optee/shm_pool.c

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright (c) 2015, Linaro Limited
3+
* Copyright (c) 2017, EPAM Systems
4+
*
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.
8+
*
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.
13+
*
14+
*/
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"
23+
24+
static int pool_op_alloc(struct tee_shm_pool_mgr *poolm,
25+
struct tee_shm *shm, size_t size)
26+
{
27+
unsigned int order = get_order(size);
28+
struct page *page;
29+
30+
page = alloc_pages(GFP_KERNEL | __GFP_ZERO, order);
31+
if (!page)
32+
return -ENOMEM;
33+
34+
shm->kaddr = page_address(page);
35+
shm->paddr = page_to_phys(page);
36+
shm->size = PAGE_SIZE << order;
37+
38+
return 0;
39+
}
40+
41+
static void pool_op_free(struct tee_shm_pool_mgr *poolm,
42+
struct tee_shm *shm)
43+
{
44+
free_pages((unsigned long)shm->kaddr, get_order(shm->size));
45+
shm->kaddr = NULL;
46+
}
47+
48+
static void pool_op_destroy_poolmgr(struct tee_shm_pool_mgr *poolm)
49+
{
50+
kfree(poolm);
51+
}
52+
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,
57+
};
58+
59+
/**
60+
* optee_shm_pool_alloc_pages() - create page-based allocator pool
61+
*
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.
64+
*/
65+
struct tee_shm_pool_mgr *optee_shm_pool_alloc_pages(void)
66+
{
67+
struct tee_shm_pool_mgr *mgr = kzalloc(sizeof(*mgr), GFP_KERNEL);
68+
69+
if (!mgr)
70+
return ERR_PTR(-ENOMEM);
71+
72+
mgr->ops = &pool_ops;
73+
74+
return mgr;
75+
}

drivers/tee/optee/shm_pool.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright (c) 2015, Linaro Limited
3+
* Copyright (c) 2016, EPAM Systems
4+
*
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.
8+
*
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.
13+
*
14+
*/
15+
16+
#ifndef SHM_POOL_H
17+
#define SHM_POOL_H
18+
19+
#include <linux/tee_drv.h>
20+
21+
struct tee_shm_pool_mgr *optee_shm_pool_alloc_pages(void);
22+
23+
#endif

0 commit comments

Comments
 (0)