@@ -200,7 +200,8 @@ static struct tee_shm *cmd_alloc_suppl(struct tee_context *ctx, size_t sz)
200
200
}
201
201
202
202
static void handle_rpc_func_cmd_shm_alloc (struct tee_context * ctx ,
203
- struct optee_msg_arg * arg )
203
+ struct optee_msg_arg * arg ,
204
+ struct optee_call_ctx * call_ctx )
204
205
{
205
206
phys_addr_t pa ;
206
207
struct tee_shm * shm ;
@@ -245,10 +246,49 @@ static void handle_rpc_func_cmd_shm_alloc(struct tee_context *ctx,
245
246
goto bad ;
246
247
}
247
248
248
- arg -> params [0 ].attr = OPTEE_MSG_ATTR_TYPE_TMEM_OUTPUT ;
249
- arg -> params [0 ].u .tmem .buf_ptr = pa ;
250
- arg -> params [0 ].u .tmem .size = sz ;
251
- arg -> params [0 ].u .tmem .shm_ref = (unsigned long )shm ;
249
+ sz = tee_shm_get_size (shm );
250
+
251
+ if (tee_shm_is_registered (shm )) {
252
+ struct page * * pages ;
253
+ u64 * pages_list ;
254
+ size_t page_num ;
255
+
256
+ pages = tee_shm_get_pages (shm , & page_num );
257
+ if (!pages || !page_num ) {
258
+ arg -> ret = TEEC_ERROR_OUT_OF_MEMORY ;
259
+ goto bad ;
260
+ }
261
+
262
+ pages_list = optee_allocate_pages_list (page_num );
263
+ if (!pages_list ) {
264
+ arg -> ret = TEEC_ERROR_OUT_OF_MEMORY ;
265
+ goto bad ;
266
+ }
267
+
268
+ call_ctx -> pages_list = pages_list ;
269
+ call_ctx -> num_entries = page_num ;
270
+
271
+ arg -> params [0 ].attr = OPTEE_MSG_ATTR_TYPE_TMEM_OUTPUT |
272
+ OPTEE_MSG_ATTR_NONCONTIG ;
273
+ /*
274
+ * In the least bits of u.tmem.buf_ptr we store buffer offset
275
+ * from 4k page, as described in OP-TEE ABI.
276
+ */
277
+ arg -> params [0 ].u .tmem .buf_ptr = virt_to_phys (pages_list ) |
278
+ (tee_shm_get_page_offset (shm ) &
279
+ (OPTEE_MSG_NONCONTIG_PAGE_SIZE - 1 ));
280
+ arg -> params [0 ].u .tmem .size = tee_shm_get_size (shm );
281
+ arg -> params [0 ].u .tmem .shm_ref = (unsigned long )shm ;
282
+
283
+ optee_fill_pages_list (pages_list , pages , page_num ,
284
+ tee_shm_get_page_offset (shm ));
285
+ } else {
286
+ arg -> params [0 ].attr = OPTEE_MSG_ATTR_TYPE_TMEM_OUTPUT ;
287
+ arg -> params [0 ].u .tmem .buf_ptr = pa ;
288
+ arg -> params [0 ].u .tmem .size = sz ;
289
+ arg -> params [0 ].u .tmem .shm_ref = (unsigned long )shm ;
290
+ }
291
+
252
292
arg -> ret = TEEC_SUCCESS ;
253
293
return ;
254
294
bad :
@@ -307,8 +347,24 @@ static void handle_rpc_func_cmd_shm_free(struct tee_context *ctx,
307
347
arg -> ret = TEEC_SUCCESS ;
308
348
}
309
349
350
+ static void free_pages_list (struct optee_call_ctx * call_ctx )
351
+ {
352
+ if (call_ctx -> pages_list ) {
353
+ optee_free_pages_list (call_ctx -> pages_list ,
354
+ call_ctx -> num_entries );
355
+ call_ctx -> pages_list = NULL ;
356
+ call_ctx -> num_entries = 0 ;
357
+ }
358
+ }
359
+
360
+ void optee_rpc_finalize_call (struct optee_call_ctx * call_ctx )
361
+ {
362
+ free_pages_list (call_ctx );
363
+ }
364
+
310
365
static void handle_rpc_func_cmd (struct tee_context * ctx , struct optee * optee ,
311
- struct tee_shm * shm )
366
+ struct tee_shm * shm ,
367
+ struct optee_call_ctx * call_ctx )
312
368
{
313
369
struct optee_msg_arg * arg ;
314
370
@@ -329,7 +385,8 @@ static void handle_rpc_func_cmd(struct tee_context *ctx, struct optee *optee,
329
385
handle_rpc_func_cmd_wait (arg );
330
386
break ;
331
387
case OPTEE_MSG_RPC_CMD_SHM_ALLOC :
332
- handle_rpc_func_cmd_shm_alloc (ctx , arg );
388
+ free_pages_list (call_ctx );
389
+ handle_rpc_func_cmd_shm_alloc (ctx , arg , call_ctx );
333
390
break ;
334
391
case OPTEE_MSG_RPC_CMD_SHM_FREE :
335
392
handle_rpc_func_cmd_shm_free (ctx , arg );
@@ -343,10 +400,12 @@ static void handle_rpc_func_cmd(struct tee_context *ctx, struct optee *optee,
343
400
* optee_handle_rpc() - handle RPC from secure world
344
401
* @ctx: context doing the RPC
345
402
* @param: value of registers for the RPC
403
+ * @call_ctx: call context. Preserved during one OP-TEE invocation
346
404
*
347
405
* Result of RPC is written back into @param.
348
406
*/
349
- void optee_handle_rpc (struct tee_context * ctx , struct optee_rpc_param * param )
407
+ void optee_handle_rpc (struct tee_context * ctx , struct optee_rpc_param * param ,
408
+ struct optee_call_ctx * call_ctx )
350
409
{
351
410
struct tee_device * teedev = ctx -> teedev ;
352
411
struct optee * optee = tee_get_drvdata (teedev );
@@ -381,7 +440,7 @@ void optee_handle_rpc(struct tee_context *ctx, struct optee_rpc_param *param)
381
440
break ;
382
441
case OPTEE_SMC_RPC_FUNC_CMD :
383
442
shm = reg_pair_to_ptr (param -> a1 , param -> a2 );
384
- handle_rpc_func_cmd (ctx , optee , shm );
443
+ handle_rpc_func_cmd (ctx , optee , shm , call_ctx );
385
444
break ;
386
445
default :
387
446
pr_warn ("Unknown RPC func 0x%x\n" ,
0 commit comments