1
2
3
4
5
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"
13#include "shm_pool.h"
14
15static int pool_op_alloc(struct tee_shm_pool_mgr *poolm,
16 struct tee_shm *shm, size_t size)
17{
18 unsigned int order = get_order(size);
19 struct page *page;
20 int rc = 0;
21
22 page = alloc_pages(GFP_KERNEL | __GFP_ZERO, order);
23 if (!page)
24 return -ENOMEM;
25
26 shm->kaddr = page_address(page);
27 shm->paddr = page_to_phys(page);
28 shm->size = PAGE_SIZE << order;
29
30
31
32
33
34 if (!(shm->flags & TEE_SHM_PRIV)) {
35 unsigned int nr_pages = 1 << order, i;
36 struct page **pages;
37
38 pages = kcalloc(nr_pages, sizeof(pages), GFP_KERNEL);
39 if (!pages) {
40 rc = -ENOMEM;
41 goto err;
42 }
43
44 for (i = 0; i < nr_pages; i++) {
45 pages[i] = page;
46 page++;
47 }
48
49 shm->flags |= TEE_SHM_REGISTER;
50 rc = optee_shm_register(shm->ctx, shm, pages, nr_pages,
51 (unsigned long)shm->kaddr);
52 kfree(pages);
53 if (rc)
54 goto err;
55 }
56
57 return 0;
58
59err:
60 __free_pages(page, order);
61 return rc;
62}
63
64static void pool_op_free(struct tee_shm_pool_mgr *poolm,
65 struct tee_shm *shm)
66{
67 if (!(shm->flags & TEE_SHM_PRIV))
68 optee_shm_unregister(shm->ctx, shm);
69
70 free_pages((unsigned long)shm->kaddr, get_order(shm->size));
71 shm->kaddr = NULL;
72}
73
74static void pool_op_destroy_poolmgr(struct tee_shm_pool_mgr *poolm)
75{
76 kfree(poolm);
77}
78
79static const struct tee_shm_pool_mgr_ops pool_ops = {
80 .alloc = pool_op_alloc,
81 .free = pool_op_free,
82 .destroy_poolmgr = pool_op_destroy_poolmgr,
83};
84
85
86
87
88
89
90
91struct tee_shm_pool_mgr *optee_shm_pool_alloc_pages(void)
92{
93 struct tee_shm_pool_mgr *mgr = kzalloc(sizeof(*mgr), GFP_KERNEL);
94
95 if (!mgr)
96 return ERR_PTR(-ENOMEM);
97
98 mgr->ops = &pool_ops;
99
100 return mgr;
101}
102