Skip to content

Commit f58e236

Browse files
lorcjenswi-linaro
authored andcommitted
tee: optee: enable dynamic SHM support
Previous patches added various features that are needed for dynamic SHM. Dynamic SHM allows Normal World to share any buffers with OP-TEE. While original design suggested to use pre-allocated region (usually of 1M to 2M of size), this new approach allows to use all non-secure RAM for command buffers, RPC allocations and TA parameters. This patch checks capability OPTEE_SMC_SEC_CAP_DYNAMIC_SHM. If it was set by OP-TEE, then kernel part of OP-TEE will use kernel page allocator to allocate command buffers. Also it will set TEE_GEN_CAP_REG_MEM capability to tell userspace that it supports shared memory registration. Signed-off-by: Volodymyr Babchuk <[email protected]> Signed-off-by: Jens Wiklander <[email protected]>
1 parent abd135b commit f58e236

File tree

1 file changed

+51
-18
lines changed

1 file changed

+51
-18
lines changed

drivers/tee/optee/core.c

Lines changed: 51 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include <linux/uaccess.h>
2929
#include "optee_private.h"
3030
#include "optee_smc.h"
31+
#include "shm_pool.h"
3132

3233
#define DRIVER_NAME "optee"
3334

@@ -219,6 +220,10 @@ static void optee_get_version(struct tee_device *teedev,
219220
.impl_caps = TEE_OPTEE_CAP_TZ,
220221
.gen_caps = TEE_GEN_CAP_GP,
221222
};
223+
struct optee *optee = tee_get_drvdata(teedev);
224+
225+
if (optee->sec_caps & OPTEE_SMC_SEC_CAP_DYNAMIC_SHM)
226+
v.gen_caps |= TEE_GEN_CAP_REG_MEM;
222227
*vers = v;
223228
}
224229

@@ -397,21 +402,22 @@ static bool optee_msg_exchange_capabilities(optee_invoke_fn *invoke_fn,
397402
}
398403

399404
static struct tee_shm_pool *
400-
optee_config_shm_memremap(optee_invoke_fn *invoke_fn, void **memremaped_shm)
405+
optee_config_shm_memremap(optee_invoke_fn *invoke_fn, void **memremaped_shm,
406+
u32 sec_caps)
401407
{
402408
union {
403409
struct arm_smccc_res smccc;
404410
struct optee_smc_get_shm_config_result result;
405411
} res;
406-
struct tee_shm_pool *pool;
407412
unsigned long vaddr;
408413
phys_addr_t paddr;
409414
size_t size;
410415
phys_addr_t begin;
411416
phys_addr_t end;
412417
void *va;
413-
struct tee_shm_pool_mem_info priv_info;
414-
struct tee_shm_pool_mem_info dmabuf_info;
418+
struct tee_shm_pool_mgr *priv_mgr;
419+
struct tee_shm_pool_mgr *dmabuf_mgr;
420+
void *rc;
415421

416422
invoke_fn(OPTEE_SMC_GET_SHM_CONFIG, 0, 0, 0, 0, 0, 0, 0, &res.smccc);
417423
if (res.result.status != OPTEE_SMC_RETURN_OK) {
@@ -441,22 +447,49 @@ optee_config_shm_memremap(optee_invoke_fn *invoke_fn, void **memremaped_shm)
441447
}
442448
vaddr = (unsigned long)va;
443449

444-
priv_info.vaddr = vaddr;
445-
priv_info.paddr = paddr;
446-
priv_info.size = OPTEE_SHM_NUM_PRIV_PAGES * PAGE_SIZE;
447-
dmabuf_info.vaddr = vaddr + OPTEE_SHM_NUM_PRIV_PAGES * PAGE_SIZE;
448-
dmabuf_info.paddr = paddr + OPTEE_SHM_NUM_PRIV_PAGES * PAGE_SIZE;
449-
dmabuf_info.size = size - OPTEE_SHM_NUM_PRIV_PAGES * PAGE_SIZE;
450-
451-
pool = tee_shm_pool_alloc_res_mem(&priv_info, &dmabuf_info);
452-
if (IS_ERR(pool)) {
453-
memunmap(va);
454-
goto out;
450+
/*
451+
* If OP-TEE can work with unregistered SHM, we will use own pool
452+
* for private shm
453+
*/
454+
if (sec_caps & OPTEE_SMC_SEC_CAP_DYNAMIC_SHM) {
455+
rc = optee_shm_pool_alloc_pages();
456+
if (IS_ERR(rc))
457+
goto err_memunmap;
458+
priv_mgr = rc;
459+
} else {
460+
const size_t sz = OPTEE_SHM_NUM_PRIV_PAGES * PAGE_SIZE;
461+
462+
rc = tee_shm_pool_mgr_alloc_res_mem(vaddr, paddr, sz,
463+
3 /* 8 bytes aligned */);
464+
if (IS_ERR(rc))
465+
goto err_memunmap;
466+
priv_mgr = rc;
467+
468+
vaddr += sz;
469+
paddr += sz;
470+
size -= sz;
455471
}
456472

473+
rc = tee_shm_pool_mgr_alloc_res_mem(vaddr, paddr, size, PAGE_SHIFT);
474+
if (IS_ERR(rc))
475+
goto err_free_priv_mgr;
476+
dmabuf_mgr = rc;
477+
478+
rc = tee_shm_pool_alloc(priv_mgr, dmabuf_mgr);
479+
if (IS_ERR(rc))
480+
goto err_free_dmabuf_mgr;
481+
457482
*memremaped_shm = va;
458-
out:
459-
return pool;
483+
484+
return rc;
485+
486+
err_free_dmabuf_mgr:
487+
tee_shm_pool_mgr_destroy(dmabuf_mgr);
488+
err_free_priv_mgr:
489+
tee_shm_pool_mgr_destroy(priv_mgr);
490+
err_memunmap:
491+
memunmap(va);
492+
return rc;
460493
}
461494

462495
/* Simple wrapper functions to be able to use a function pointer */
@@ -534,7 +567,7 @@ static struct optee *optee_probe(struct device_node *np)
534567
if (!(sec_caps & OPTEE_SMC_SEC_CAP_HAVE_RESERVED_SHM))
535568
return ERR_PTR(-EINVAL);
536569

537-
pool = optee_config_shm_memremap(invoke_fn, &memremaped_shm);
570+
pool = optee_config_shm_memremap(invoke_fn, &memremaped_shm, sec_caps);
538571
if (IS_ERR(pool))
539572
return (void *)pool;
540573

0 commit comments

Comments
 (0)