Skip to content

Commit 9733b07

Browse files
lorcjenswi-linaro
authored andcommitted
optee: allow to work without static shared memory
On virtualized systems it is possible that OP-TEE will provide only dynamic shared memory support. So it is fine to boot without static SHM enabled if dymanic one is supported. Signed-off-by: Volodymyr Babchuk <[email protected]> Signed-off-by: Jens Wiklander <[email protected]>
1 parent 1c163f4 commit 9733b07

File tree

1 file changed

+49
-31
lines changed

1 file changed

+49
-31
lines changed

drivers/tee/optee/core.c

Lines changed: 49 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -419,9 +419,35 @@ static bool optee_msg_exchange_capabilities(optee_invoke_fn *invoke_fn,
419419
return true;
420420
}
421421

422+
static struct tee_shm_pool *optee_config_dyn_shm(void)
423+
{
424+
struct tee_shm_pool_mgr *priv_mgr;
425+
struct tee_shm_pool_mgr *dmabuf_mgr;
426+
void *rc;
427+
428+
rc = optee_shm_pool_alloc_pages();
429+
if (IS_ERR(rc))
430+
return rc;
431+
priv_mgr = rc;
432+
433+
rc = optee_shm_pool_alloc_pages();
434+
if (IS_ERR(rc)) {
435+
tee_shm_pool_mgr_destroy(priv_mgr);
436+
return rc;
437+
}
438+
dmabuf_mgr = rc;
439+
440+
rc = tee_shm_pool_alloc(priv_mgr, dmabuf_mgr);
441+
if (IS_ERR(rc)) {
442+
tee_shm_pool_mgr_destroy(priv_mgr);
443+
tee_shm_pool_mgr_destroy(dmabuf_mgr);
444+
}
445+
446+
return rc;
447+
}
448+
422449
static struct tee_shm_pool *
423-
optee_config_shm_memremap(optee_invoke_fn *invoke_fn, void **memremaped_shm,
424-
u32 sec_caps)
450+
optee_config_shm_memremap(optee_invoke_fn *invoke_fn, void **memremaped_shm)
425451
{
426452
union {
427453
struct arm_smccc_res smccc;
@@ -436,10 +462,11 @@ optee_config_shm_memremap(optee_invoke_fn *invoke_fn, void **memremaped_shm,
436462
struct tee_shm_pool_mgr *priv_mgr;
437463
struct tee_shm_pool_mgr *dmabuf_mgr;
438464
void *rc;
465+
const int sz = OPTEE_SHM_NUM_PRIV_PAGES * PAGE_SIZE;
439466

440467
invoke_fn(OPTEE_SMC_GET_SHM_CONFIG, 0, 0, 0, 0, 0, 0, 0, &res.smccc);
441468
if (res.result.status != OPTEE_SMC_RETURN_OK) {
442-
pr_info("shm service not available\n");
469+
pr_err("static shm service not available\n");
443470
return ERR_PTR(-ENOENT);
444471
}
445472

@@ -465,28 +492,15 @@ optee_config_shm_memremap(optee_invoke_fn *invoke_fn, void **memremaped_shm,
465492
}
466493
vaddr = (unsigned long)va;
467494

468-
/*
469-
* If OP-TEE can work with unregistered SHM, we will use own pool
470-
* for private shm
471-
*/
472-
if (sec_caps & OPTEE_SMC_SEC_CAP_DYNAMIC_SHM) {
473-
rc = optee_shm_pool_alloc_pages();
474-
if (IS_ERR(rc))
475-
goto err_memunmap;
476-
priv_mgr = rc;
477-
} else {
478-
const size_t sz = OPTEE_SHM_NUM_PRIV_PAGES * PAGE_SIZE;
479-
480-
rc = tee_shm_pool_mgr_alloc_res_mem(vaddr, paddr, sz,
481-
3 /* 8 bytes aligned */);
482-
if (IS_ERR(rc))
483-
goto err_memunmap;
484-
priv_mgr = rc;
485-
486-
vaddr += sz;
487-
paddr += sz;
488-
size -= sz;
489-
}
495+
rc = tee_shm_pool_mgr_alloc_res_mem(vaddr, paddr, sz,
496+
3 /* 8 bytes aligned */);
497+
if (IS_ERR(rc))
498+
goto err_memunmap;
499+
priv_mgr = rc;
500+
501+
vaddr += sz;
502+
paddr += sz;
503+
size -= sz;
490504

491505
rc = tee_shm_pool_mgr_alloc_res_mem(vaddr, paddr, size, PAGE_SHIFT);
492506
if (IS_ERR(rc))
@@ -552,7 +566,7 @@ static optee_invoke_fn *get_invoke_func(struct device_node *np)
552566
static struct optee *optee_probe(struct device_node *np)
553567
{
554568
optee_invoke_fn *invoke_fn;
555-
struct tee_shm_pool *pool;
569+
struct tee_shm_pool *pool = ERR_PTR(-EINVAL);
556570
struct optee *optee = NULL;
557571
void *memremaped_shm = NULL;
558572
struct tee_device *teedev;
@@ -581,13 +595,17 @@ static struct optee *optee_probe(struct device_node *np)
581595
}
582596

583597
/*
584-
* We have no other option for shared memory, if secure world
585-
* doesn't have any reserved memory we can use we can't continue.
598+
* Try to use dynamic shared memory if possible
586599
*/
587-
if (!(sec_caps & OPTEE_SMC_SEC_CAP_HAVE_RESERVED_SHM))
588-
return ERR_PTR(-EINVAL);
600+
if (sec_caps & OPTEE_SMC_SEC_CAP_DYNAMIC_SHM)
601+
pool = optee_config_dyn_shm();
602+
603+
/*
604+
* If dynamic shared memory is not available or failed - try static one
605+
*/
606+
if (IS_ERR(pool) && (sec_caps & OPTEE_SMC_SEC_CAP_HAVE_RESERVED_SHM))
607+
pool = optee_config_shm_memremap(invoke_fn, &memremaped_shm);
589608

590-
pool = optee_config_shm_memremap(invoke_fn, &memremaped_shm, sec_caps);
591609
if (IS_ERR(pool))
592610
return (void *)pool;
593611

0 commit comments

Comments
 (0)