@@ -44,49 +44,18 @@ static void pool_op_gen_free(struct tee_shm_pool_mgr *poolm,
4444 shm -> kaddr = NULL ;
4545}
4646
47+ static void pool_op_gen_destroy_poolmgr (struct tee_shm_pool_mgr * poolm )
48+ {
49+ gen_pool_destroy (poolm -> private_data );
50+ kfree (poolm );
51+ }
52+
4753static const struct tee_shm_pool_mgr_ops pool_ops_generic = {
4854 .alloc = pool_op_gen_alloc ,
4955 .free = pool_op_gen_free ,
56+ .destroy_poolmgr = pool_op_gen_destroy_poolmgr ,
5057};
5158
52- static void pool_res_mem_destroy (struct tee_shm_pool * pool )
53- {
54- gen_pool_destroy (pool -> private_mgr .private_data );
55- gen_pool_destroy (pool -> dma_buf_mgr .private_data );
56- }
57-
58- static int pool_res_mem_mgr_init (struct tee_shm_pool_mgr * mgr ,
59- struct tee_shm_pool_mem_info * info ,
60- int min_alloc_order )
61- {
62- size_t page_mask = PAGE_SIZE - 1 ;
63- struct gen_pool * genpool = NULL ;
64- int rc ;
65-
66- /*
67- * Start and end must be page aligned
68- */
69- if ((info -> vaddr & page_mask ) || (info -> paddr & page_mask ) ||
70- (info -> size & page_mask ))
71- return - EINVAL ;
72-
73- genpool = gen_pool_create (min_alloc_order , -1 );
74- if (!genpool )
75- return - ENOMEM ;
76-
77- gen_pool_set_algo (genpool , gen_pool_best_fit , NULL );
78- rc = gen_pool_add_virt (genpool , info -> vaddr , info -> paddr , info -> size ,
79- -1 );
80- if (rc ) {
81- gen_pool_destroy (genpool );
82- return rc ;
83- }
84-
85- mgr -> private_data = genpool ;
86- mgr -> ops = & pool_ops_generic ;
87- return 0 ;
88- }
89-
9059/**
9160 * tee_shm_pool_alloc_res_mem() - Create a shared memory pool from reserved
9261 * memory range
@@ -104,42 +73,109 @@ struct tee_shm_pool *
10473tee_shm_pool_alloc_res_mem (struct tee_shm_pool_mem_info * priv_info ,
10574 struct tee_shm_pool_mem_info * dmabuf_info )
10675{
107- struct tee_shm_pool * pool = NULL ;
108- int ret ;
109-
110- pool = kzalloc (sizeof (* pool ), GFP_KERNEL );
111- if (!pool ) {
112- ret = - ENOMEM ;
113- goto err ;
114- }
76+ struct tee_shm_pool_mgr * priv_mgr ;
77+ struct tee_shm_pool_mgr * dmabuf_mgr ;
78+ void * rc ;
11579
11680 /*
11781 * Create the pool for driver private shared memory
11882 */
119- ret = pool_res_mem_mgr_init (& pool -> private_mgr , priv_info ,
120- 3 /* 8 byte aligned */ );
121- if (ret )
122- goto err ;
83+ rc = tee_shm_pool_mgr_alloc_res_mem (priv_info -> vaddr , priv_info -> paddr ,
84+ priv_info -> size ,
85+ 3 /* 8 byte aligned */ );
86+ if (IS_ERR (rc ))
87+ return rc ;
88+ priv_mgr = rc ;
12389
12490 /*
12591 * Create the pool for dma_buf shared memory
12692 */
127- ret = pool_res_mem_mgr_init (& pool -> dma_buf_mgr , dmabuf_info ,
128- PAGE_SHIFT );
129- if (ret )
93+ rc = tee_shm_pool_mgr_alloc_res_mem (dmabuf_info -> vaddr ,
94+ dmabuf_info -> paddr ,
95+ dmabuf_info -> size , PAGE_SHIFT );
96+ if (IS_ERR (rc ))
97+ goto err_free_priv_mgr ;
98+ dmabuf_mgr = rc ;
99+
100+ rc = tee_shm_pool_alloc (priv_mgr , dmabuf_mgr );
101+ if (IS_ERR (rc ))
102+ goto err_free_dmabuf_mgr ;
103+
104+ return rc ;
105+
106+ err_free_dmabuf_mgr :
107+ tee_shm_pool_mgr_destroy (dmabuf_mgr );
108+ err_free_priv_mgr :
109+ tee_shm_pool_mgr_destroy (priv_mgr );
110+
111+ return rc ;
112+ }
113+ EXPORT_SYMBOL_GPL (tee_shm_pool_alloc_res_mem );
114+
115+ struct tee_shm_pool_mgr * tee_shm_pool_mgr_alloc_res_mem (unsigned long vaddr ,
116+ phys_addr_t paddr ,
117+ size_t size ,
118+ int min_alloc_order )
119+ {
120+ const size_t page_mask = PAGE_SIZE - 1 ;
121+ struct tee_shm_pool_mgr * mgr ;
122+ int rc ;
123+
124+ /* Start and end must be page aligned */
125+ if (vaddr & page_mask || paddr & page_mask || size & page_mask )
126+ return ERR_PTR (- EINVAL );
127+
128+ mgr = kzalloc (sizeof (* mgr ), GFP_KERNEL );
129+ if (!mgr )
130+ return ERR_PTR (- ENOMEM );
131+
132+ mgr -> private_data = gen_pool_create (min_alloc_order , -1 );
133+ if (!mgr -> private_data ) {
134+ rc = - ENOMEM ;
130135 goto err ;
136+ }
131137
132- pool -> destroy = pool_res_mem_destroy ;
133- return pool ;
138+ gen_pool_set_algo (mgr -> private_data , gen_pool_best_fit , NULL );
139+ rc = gen_pool_add_virt (mgr -> private_data , vaddr , paddr , size , -1 );
140+ if (rc ) {
141+ gen_pool_destroy (mgr -> private_data );
142+ goto err ;
143+ }
144+
145+ mgr -> ops = & pool_ops_generic ;
146+
147+ return mgr ;
134148err :
135- if (ret == - ENOMEM )
136- pr_err ("%s: can't allocate memory for res_mem shared memory pool\n" , __func__ );
137- if (pool && pool -> private_mgr .private_data )
138- gen_pool_destroy (pool -> private_mgr .private_data );
139- kfree (pool );
140- return ERR_PTR (ret );
149+ kfree (mgr );
150+
151+ return ERR_PTR (rc );
141152}
142- EXPORT_SYMBOL_GPL (tee_shm_pool_alloc_res_mem );
153+ EXPORT_SYMBOL_GPL (tee_shm_pool_mgr_alloc_res_mem );
154+
155+ static bool check_mgr_ops (struct tee_shm_pool_mgr * mgr )
156+ {
157+ return mgr && mgr -> ops && mgr -> ops -> alloc && mgr -> ops -> free &&
158+ mgr -> ops -> destroy_poolmgr ;
159+ }
160+
161+ struct tee_shm_pool * tee_shm_pool_alloc (struct tee_shm_pool_mgr * priv_mgr ,
162+ struct tee_shm_pool_mgr * dmabuf_mgr )
163+ {
164+ struct tee_shm_pool * pool ;
165+
166+ if (!check_mgr_ops (priv_mgr ) || !check_mgr_ops (dmabuf_mgr ))
167+ return ERR_PTR (- EINVAL );
168+
169+ pool = kzalloc (sizeof (* pool ), GFP_KERNEL );
170+ if (!pool )
171+ return ERR_PTR (- ENOMEM );
172+
173+ pool -> private_mgr = priv_mgr ;
174+ pool -> dma_buf_mgr = dmabuf_mgr ;
175+
176+ return pool ;
177+ }
178+ EXPORT_SYMBOL_GPL (tee_shm_pool_alloc );
143179
144180/**
145181 * tee_shm_pool_free() - Free a shared memory pool
@@ -150,7 +186,10 @@ EXPORT_SYMBOL_GPL(tee_shm_pool_alloc_res_mem);
150186 */
151187void tee_shm_pool_free (struct tee_shm_pool * pool )
152188{
153- pool -> destroy (pool );
189+ if (pool -> private_mgr )
190+ tee_shm_pool_mgr_destroy (pool -> private_mgr );
191+ if (pool -> dma_buf_mgr )
192+ tee_shm_pool_mgr_destroy (pool -> dma_buf_mgr );
154193 kfree (pool );
155194}
156195EXPORT_SYMBOL_GPL (tee_shm_pool_free );
0 commit comments