@@ -420,9 +420,35 @@ static bool optee_msg_exchange_capabilities(optee_invoke_fn *invoke_fn,
420420 return true;
421421}
422422
423+ static struct tee_shm_pool * optee_config_dyn_shm (void )
424+ {
425+ struct tee_shm_pool_mgr * priv_mgr ;
426+ struct tee_shm_pool_mgr * dmabuf_mgr ;
427+ void * rc ;
428+
429+ rc = optee_shm_pool_alloc_pages ();
430+ if (IS_ERR (rc ))
431+ return rc ;
432+ priv_mgr = rc ;
433+
434+ rc = optee_shm_pool_alloc_pages ();
435+ if (IS_ERR (rc )) {
436+ tee_shm_pool_mgr_destroy (priv_mgr );
437+ return rc ;
438+ }
439+ dmabuf_mgr = rc ;
440+
441+ rc = tee_shm_pool_alloc (priv_mgr , dmabuf_mgr );
442+ if (IS_ERR (rc )) {
443+ tee_shm_pool_mgr_destroy (priv_mgr );
444+ tee_shm_pool_mgr_destroy (dmabuf_mgr );
445+ }
446+
447+ return rc ;
448+ }
449+
423450static struct tee_shm_pool *
424- optee_config_shm_memremap (optee_invoke_fn * invoke_fn , void * * memremaped_shm ,
425- u32 sec_caps )
451+ optee_config_shm_memremap (optee_invoke_fn * invoke_fn , void * * memremaped_shm )
426452{
427453 union {
428454 struct arm_smccc_res smccc ;
@@ -437,10 +463,11 @@ optee_config_shm_memremap(optee_invoke_fn *invoke_fn, void **memremaped_shm,
437463 struct tee_shm_pool_mgr * priv_mgr ;
438464 struct tee_shm_pool_mgr * dmabuf_mgr ;
439465 void * rc ;
466+ const int sz = OPTEE_SHM_NUM_PRIV_PAGES * PAGE_SIZE ;
440467
441468 invoke_fn (OPTEE_SMC_GET_SHM_CONFIG , 0 , 0 , 0 , 0 , 0 , 0 , 0 , & res .smccc );
442469 if (res .result .status != OPTEE_SMC_RETURN_OK ) {
443- pr_info ( " shm service not available\n" );
470+ pr_err ( "static shm service not available\n" );
444471 return ERR_PTR (- ENOENT );
445472 }
446473
@@ -466,28 +493,15 @@ optee_config_shm_memremap(optee_invoke_fn *invoke_fn, void **memremaped_shm,
466493 }
467494 vaddr = (unsigned long )va ;
468495
469- /*
470- * If OP-TEE can work with unregistered SHM, we will use own pool
471- * for private shm
472- */
473- if (sec_caps & OPTEE_SMC_SEC_CAP_DYNAMIC_SHM ) {
474- rc = optee_shm_pool_alloc_pages ();
475- if (IS_ERR (rc ))
476- goto err_memunmap ;
477- priv_mgr = rc ;
478- } else {
479- const size_t sz = OPTEE_SHM_NUM_PRIV_PAGES * PAGE_SIZE ;
480-
481- rc = tee_shm_pool_mgr_alloc_res_mem (vaddr , paddr , sz ,
482- 3 /* 8 bytes aligned */ );
483- if (IS_ERR (rc ))
484- goto err_memunmap ;
485- priv_mgr = rc ;
486-
487- vaddr += sz ;
488- paddr += sz ;
489- size -= sz ;
490- }
496+ rc = tee_shm_pool_mgr_alloc_res_mem (vaddr , paddr , sz ,
497+ 3 /* 8 bytes aligned */ );
498+ if (IS_ERR (rc ))
499+ goto err_memunmap ;
500+ priv_mgr = rc ;
501+
502+ vaddr += sz ;
503+ paddr += sz ;
504+ size -= sz ;
491505
492506 rc = tee_shm_pool_mgr_alloc_res_mem (vaddr , paddr , size , PAGE_SHIFT );
493507 if (IS_ERR (rc ))
@@ -553,7 +567,7 @@ static optee_invoke_fn *get_invoke_func(struct device_node *np)
553567static struct optee * optee_probe (struct device_node * np )
554568{
555569 optee_invoke_fn * invoke_fn ;
556- struct tee_shm_pool * pool ;
570+ struct tee_shm_pool * pool = ERR_PTR ( - EINVAL ) ;
557571 struct optee * optee = NULL ;
558572 void * memremaped_shm = NULL ;
559573 struct tee_device * teedev ;
@@ -582,13 +596,17 @@ static struct optee *optee_probe(struct device_node *np)
582596 }
583597
584598 /*
585- * We have no other option for shared memory, if secure world
586- * doesn't have any reserved memory we can use we can't continue.
599+ * Try to use dynamic shared memory if possible
587600 */
588- if (!(sec_caps & OPTEE_SMC_SEC_CAP_HAVE_RESERVED_SHM ))
589- return ERR_PTR (- EINVAL );
601+ if (sec_caps & OPTEE_SMC_SEC_CAP_DYNAMIC_SHM )
602+ pool = optee_config_dyn_shm ();
603+
604+ /*
605+ * If dynamic shared memory is not available or failed - try static one
606+ */
607+ if (IS_ERR (pool ) && (sec_caps & OPTEE_SMC_SEC_CAP_HAVE_RESERVED_SHM ))
608+ pool = optee_config_shm_memremap (invoke_fn , & memremaped_shm );
590609
591- pool = optee_config_shm_memremap (invoke_fn , & memremaped_shm , sec_caps );
592610 if (IS_ERR (pool ))
593611 return (void * )pool ;
594612
0 commit comments