Commit 5b4018b9 authored by Jens Wiklander's avatar Jens Wiklander

optee: cache argument shared memory structs

Implements a cache to handle shared memory used to pass the argument
struct needed when doing a normal yielding call into secure world.
Signed-off-by: default avatarJens Wiklander <jens.wiklander@linaro.org>
parent a639b2b1
This diff is collapsed.
...@@ -171,6 +171,7 @@ void optee_remove_common(struct optee *optee) ...@@ -171,6 +171,7 @@ void optee_remove_common(struct optee *optee)
optee_unregister_devices(); optee_unregister_devices();
optee_notif_uninit(optee); optee_notif_uninit(optee);
optee_shm_arg_cache_uninit(optee);
teedev_close_context(optee->ctx); teedev_close_context(optee->ctx);
/* /*
* The two devices have to be unregistered before we can free the * The two devices have to be unregistered before we can free the
......
...@@ -601,6 +601,7 @@ static int optee_ffa_yielding_call(struct tee_context *ctx, ...@@ -601,6 +601,7 @@ static int optee_ffa_yielding_call(struct tee_context *ctx,
* optee_ffa_do_call_with_arg() - Do a FF-A call to enter OP-TEE in secure world * optee_ffa_do_call_with_arg() - Do a FF-A call to enter OP-TEE in secure world
* @ctx: calling context * @ctx: calling context
* @shm: shared memory holding the message to pass to secure world * @shm: shared memory holding the message to pass to secure world
* @offs: offset of the message in @shm
* *
* Does a FF-A call to OP-TEE in secure world and handles eventual resulting * Does a FF-A call to OP-TEE in secure world and handles eventual resulting
* Remote Procedure Calls (RPC) from OP-TEE. * Remote Procedure Calls (RPC) from OP-TEE.
...@@ -609,13 +610,13 @@ static int optee_ffa_yielding_call(struct tee_context *ctx, ...@@ -609,13 +610,13 @@ static int optee_ffa_yielding_call(struct tee_context *ctx,
*/ */
static int optee_ffa_do_call_with_arg(struct tee_context *ctx, static int optee_ffa_do_call_with_arg(struct tee_context *ctx,
struct tee_shm *shm) struct tee_shm *shm, u_int offs)
{ {
struct ffa_send_direct_data data = { struct ffa_send_direct_data data = {
.data0 = OPTEE_FFA_YIELDING_CALL_WITH_ARG, .data0 = OPTEE_FFA_YIELDING_CALL_WITH_ARG,
.data1 = (u32)shm->sec_world_id, .data1 = (u32)shm->sec_world_id,
.data2 = (u32)(shm->sec_world_id >> 32), .data2 = (u32)(shm->sec_world_id >> 32),
.data3 = 0, .data3 = offs,
}; };
struct optee_msg_arg *arg; struct optee_msg_arg *arg;
unsigned int rpc_arg_offs; unsigned int rpc_arg_offs;
...@@ -630,12 +631,12 @@ static int optee_ffa_do_call_with_arg(struct tee_context *ctx, ...@@ -630,12 +631,12 @@ static int optee_ffa_do_call_with_arg(struct tee_context *ctx,
if (shm->offset) if (shm->offset)
return -EINVAL; return -EINVAL;
arg = tee_shm_get_va(shm, 0); arg = tee_shm_get_va(shm, offs);
if (IS_ERR(arg)) if (IS_ERR(arg))
return PTR_ERR(arg); return PTR_ERR(arg);
rpc_arg_offs = OPTEE_MSG_GET_ARG_SIZE(arg->num_params); rpc_arg_offs = OPTEE_MSG_GET_ARG_SIZE(arg->num_params);
rpc_arg = tee_shm_get_va(shm, rpc_arg_offs); rpc_arg = tee_shm_get_va(shm, offs + rpc_arg_offs);
if (IS_ERR(rpc_arg)) if (IS_ERR(rpc_arg))
return PTR_ERR(rpc_arg); return PTR_ERR(rpc_arg);
...@@ -787,6 +788,7 @@ static int optee_ffa_probe(struct ffa_device *ffa_dev) ...@@ -787,6 +788,7 @@ static int optee_ffa_probe(struct ffa_device *ffa_dev)
struct tee_shm_pool *pool; struct tee_shm_pool *pool;
struct tee_device *teedev; struct tee_device *teedev;
struct tee_context *ctx; struct tee_context *ctx;
u32 arg_cache_flags = 0;
struct optee *optee; struct optee *optee;
u32 sec_caps; u32 sec_caps;
int rc; int rc;
...@@ -803,6 +805,8 @@ static int optee_ffa_probe(struct ffa_device *ffa_dev) ...@@ -803,6 +805,8 @@ static int optee_ffa_probe(struct ffa_device *ffa_dev)
if (!optee_ffa_exchange_caps(ffa_dev, ffa_ops, &sec_caps, if (!optee_ffa_exchange_caps(ffa_dev, ffa_ops, &sec_caps,
&rpc_param_count)) &rpc_param_count))
return -EINVAL; return -EINVAL;
if (sec_caps & OPTEE_FFA_SEC_CAP_ARG_OFFSET)
arg_cache_flags |= OPTEE_SHM_ARG_SHARED;
optee = kzalloc(sizeof(*optee), GFP_KERNEL); optee = kzalloc(sizeof(*optee), GFP_KERNEL);
if (!optee) if (!optee)
...@@ -851,6 +855,7 @@ static int optee_ffa_probe(struct ffa_device *ffa_dev) ...@@ -851,6 +855,7 @@ static int optee_ffa_probe(struct ffa_device *ffa_dev)
mutex_init(&optee->call_queue.mutex); mutex_init(&optee->call_queue.mutex);
INIT_LIST_HEAD(&optee->call_queue.waiters); INIT_LIST_HEAD(&optee->call_queue.waiters);
optee_supp_init(&optee->supp); optee_supp_init(&optee->supp);
optee_shm_arg_cache_init(optee, arg_cache_flags);
ffa_dev_set_drvdata(ffa_dev, optee); ffa_dev_set_drvdata(ffa_dev, optee);
ctx = teedev_open(optee->teedev); ctx = teedev_open(optee->teedev);
if (IS_ERR(ctx)) { if (IS_ERR(ctx)) {
......
...@@ -59,6 +59,16 @@ struct optee_notif { ...@@ -59,6 +59,16 @@ struct optee_notif {
u_long *bitmap; u_long *bitmap;
}; };
#define OPTEE_SHM_ARG_ALLOC_PRIV BIT(0)
#define OPTEE_SHM_ARG_SHARED BIT(1)
struct optee_shm_arg_entry;
struct optee_shm_arg_cache {
u32 flags;
/* Serializes access to this struct */
struct mutex mutex;
struct list_head shm_args;
};
/** /**
* struct optee_supp - supplicant synchronization struct * struct optee_supp - supplicant synchronization struct
* @ctx the context of current connected supplicant. * @ctx the context of current connected supplicant.
...@@ -121,7 +131,7 @@ struct optee; ...@@ -121,7 +131,7 @@ struct optee;
*/ */
struct optee_ops { struct optee_ops {
int (*do_call_with_arg)(struct tee_context *ctx, int (*do_call_with_arg)(struct tee_context *ctx,
struct tee_shm *shm_arg); struct tee_shm *shm_arg, u_int offs);
int (*to_msg_param)(struct optee *optee, int (*to_msg_param)(struct optee *optee,
struct optee_msg_param *msg_params, struct optee_msg_param *msg_params,
size_t num_params, const struct tee_param *params); size_t num_params, const struct tee_param *params);
...@@ -157,6 +167,7 @@ struct optee { ...@@ -157,6 +167,7 @@ struct optee {
struct optee_smc smc; struct optee_smc smc;
struct optee_ffa ffa; struct optee_ffa ffa;
}; };
struct optee_shm_arg_cache shm_arg_cache;
struct optee_call_queue call_queue; struct optee_call_queue call_queue;
struct optee_notif notif; struct optee_notif notif;
struct optee_supp supp; struct optee_supp supp;
...@@ -273,8 +284,18 @@ void optee_cq_wait_for_completion(struct optee_call_queue *cq, ...@@ -273,8 +284,18 @@ void optee_cq_wait_for_completion(struct optee_call_queue *cq,
void optee_cq_wait_final(struct optee_call_queue *cq, void optee_cq_wait_final(struct optee_call_queue *cq,
struct optee_call_waiter *w); struct optee_call_waiter *w);
int optee_check_mem_type(unsigned long start, size_t num_pages); int optee_check_mem_type(unsigned long start, size_t num_pages);
struct tee_shm *optee_get_msg_arg(struct tee_context *ctx, size_t num_params,
struct optee_msg_arg **msg_arg); void optee_shm_arg_cache_init(struct optee *optee, u32 flags);
void optee_shm_arg_cache_uninit(struct optee *optee);
struct optee_msg_arg *optee_get_msg_arg(struct tee_context *ctx,
size_t num_params,
struct optee_shm_arg_entry **entry,
struct tee_shm **shm_ret,
u_int *offs);
void optee_free_msg_arg(struct tee_context *ctx,
struct optee_shm_arg_entry *entry, u_int offs);
size_t optee_msg_arg_size(size_t rpc_param_count);
struct tee_shm *optee_rpc_cmd_alloc_suppl(struct tee_context *ctx, size_t sz); struct tee_shm *optee_rpc_cmd_alloc_suppl(struct tee_context *ctx, size_t sz);
void optee_rpc_cmd_free_suppl(struct tee_context *ctx, struct tee_shm *shm); void optee_rpc_cmd_free_suppl(struct tee_context *ctx, struct tee_shm *shm);
......
...@@ -437,6 +437,7 @@ static int optee_shm_register(struct tee_context *ctx, struct tee_shm *shm, ...@@ -437,6 +437,7 @@ static int optee_shm_register(struct tee_context *ctx, struct tee_shm *shm,
struct optee_msg_arg *msg_arg; struct optee_msg_arg *msg_arg;
struct tee_shm *shm_arg; struct tee_shm *shm_arg;
u64 *pages_list; u64 *pages_list;
size_t sz;
int rc; int rc;
if (!num_pages) if (!num_pages)
...@@ -450,15 +451,30 @@ static int optee_shm_register(struct tee_context *ctx, struct tee_shm *shm, ...@@ -450,15 +451,30 @@ static int optee_shm_register(struct tee_context *ctx, struct tee_shm *shm,
if (!pages_list) if (!pages_list)
return -ENOMEM; return -ENOMEM;
shm_arg = optee_get_msg_arg(ctx, 1, &msg_arg); /*
* We're about to register shared memory we can't register shared
* memory for this request or there's a catch-22.
*
* So in this we'll have to do the good old temporary private
* allocation instead of using optee_get_msg_arg().
*/
sz = optee_msg_arg_size(optee->rpc_param_count);
shm_arg = tee_shm_alloc_priv_buf(ctx, sz);
if (IS_ERR(shm_arg)) { if (IS_ERR(shm_arg)) {
rc = PTR_ERR(shm_arg); rc = PTR_ERR(shm_arg);
goto out; goto out;
} }
msg_arg = tee_shm_get_va(shm_arg, 0);
if (IS_ERR(msg_arg)) {
rc = PTR_ERR(msg_arg);
goto out;
}
optee_fill_pages_list(pages_list, pages, num_pages, optee_fill_pages_list(pages_list, pages, num_pages,
tee_shm_get_page_offset(shm)); tee_shm_get_page_offset(shm));
memset(msg_arg, 0, OPTEE_MSG_GET_ARG_SIZE(1));
msg_arg->num_params = 1;
msg_arg->cmd = OPTEE_MSG_CMD_REGISTER_SHM; msg_arg->cmd = OPTEE_MSG_CMD_REGISTER_SHM;
msg_arg->params->attr = OPTEE_MSG_ATTR_TYPE_TMEM_OUTPUT | msg_arg->params->attr = OPTEE_MSG_ATTR_TYPE_TMEM_OUTPUT |
OPTEE_MSG_ATTR_NONCONTIG; OPTEE_MSG_ATTR_NONCONTIG;
...@@ -471,7 +487,7 @@ static int optee_shm_register(struct tee_context *ctx, struct tee_shm *shm, ...@@ -471,7 +487,7 @@ static int optee_shm_register(struct tee_context *ctx, struct tee_shm *shm,
msg_arg->params->u.tmem.buf_ptr = virt_to_phys(pages_list) | msg_arg->params->u.tmem.buf_ptr = virt_to_phys(pages_list) |
(tee_shm_get_page_offset(shm) & (OPTEE_MSG_NONCONTIG_PAGE_SIZE - 1)); (tee_shm_get_page_offset(shm) & (OPTEE_MSG_NONCONTIG_PAGE_SIZE - 1));
if (optee->ops->do_call_with_arg(ctx, shm_arg) || if (optee->ops->do_call_with_arg(ctx, shm_arg, 0) ||
msg_arg->ret != TEEC_SUCCESS) msg_arg->ret != TEEC_SUCCESS)
rc = -EINVAL; rc = -EINVAL;
...@@ -487,19 +503,37 @@ static int optee_shm_unregister(struct tee_context *ctx, struct tee_shm *shm) ...@@ -487,19 +503,37 @@ static int optee_shm_unregister(struct tee_context *ctx, struct tee_shm *shm)
struct optee_msg_arg *msg_arg; struct optee_msg_arg *msg_arg;
struct tee_shm *shm_arg; struct tee_shm *shm_arg;
int rc = 0; int rc = 0;
size_t sz;
shm_arg = optee_get_msg_arg(ctx, 1, &msg_arg); /*
* We're about to unregister shared memory and we may not be able
* register shared memory for this request in case we're called
* from optee_shm_arg_cache_uninit().
*
* So in order to keep things simple in this function just as in
* optee_shm_register() we'll use temporary private allocation
* instead of using optee_get_msg_arg().
*/
sz = optee_msg_arg_size(optee->rpc_param_count);
shm_arg = tee_shm_alloc_priv_buf(ctx, sz);
if (IS_ERR(shm_arg)) if (IS_ERR(shm_arg))
return PTR_ERR(shm_arg); return PTR_ERR(shm_arg);
msg_arg = tee_shm_get_va(shm_arg, 0);
if (IS_ERR(msg_arg)) {
rc = PTR_ERR(msg_arg);
goto out;
}
memset(msg_arg, 0, sz);
msg_arg->num_params = 1;
msg_arg->cmd = OPTEE_MSG_CMD_UNREGISTER_SHM; msg_arg->cmd = OPTEE_MSG_CMD_UNREGISTER_SHM;
msg_arg->params[0].attr = OPTEE_MSG_ATTR_TYPE_RMEM_INPUT; msg_arg->params[0].attr = OPTEE_MSG_ATTR_TYPE_RMEM_INPUT;
msg_arg->params[0].u.rmem.shm_ref = (unsigned long)shm; msg_arg->params[0].u.rmem.shm_ref = (unsigned long)shm;
if (optee->ops->do_call_with_arg(ctx, shm_arg) || if (optee->ops->do_call_with_arg(ctx, shm_arg, 0) ||
msg_arg->ret != TEEC_SUCCESS) msg_arg->ret != TEEC_SUCCESS)
rc = -EINVAL; rc = -EINVAL;
out:
tee_shm_free(shm_arg); tee_shm_free(shm_arg);
return rc; return rc;
} }
...@@ -823,6 +857,7 @@ static void optee_handle_rpc(struct tee_context *ctx, ...@@ -823,6 +857,7 @@ static void optee_handle_rpc(struct tee_context *ctx,
* optee_smc_do_call_with_arg() - Do an SMC to OP-TEE in secure world * optee_smc_do_call_with_arg() - Do an SMC to OP-TEE in secure world
* @ctx: calling context * @ctx: calling context
* @shm: shared memory holding the message to pass to secure world * @shm: shared memory holding the message to pass to secure world
* @offs: offset of the message in @shm
* *
* Does and SMC to OP-TEE in secure world and handles eventual resulting * Does and SMC to OP-TEE in secure world and handles eventual resulting
* Remote Procedure Calls (RPC) from OP-TEE. * Remote Procedure Calls (RPC) from OP-TEE.
...@@ -830,7 +865,7 @@ static void optee_handle_rpc(struct tee_context *ctx, ...@@ -830,7 +865,7 @@ static void optee_handle_rpc(struct tee_context *ctx,
* Returns return code from secure world, 0 is OK * Returns return code from secure world, 0 is OK
*/ */
static int optee_smc_do_call_with_arg(struct tee_context *ctx, static int optee_smc_do_call_with_arg(struct tee_context *ctx,
struct tee_shm *shm) struct tee_shm *shm, u_int offs)
{ {
struct optee *optee = tee_get_drvdata(ctx->teedev); struct optee *optee = tee_get_drvdata(ctx->teedev);
struct optee_call_waiter w; struct optee_call_waiter w;
...@@ -843,12 +878,12 @@ static int optee_smc_do_call_with_arg(struct tee_context *ctx, ...@@ -843,12 +878,12 @@ static int optee_smc_do_call_with_arg(struct tee_context *ctx,
struct optee_msg_arg *arg; struct optee_msg_arg *arg;
unsigned int rpc_arg_offs; unsigned int rpc_arg_offs;
arg = tee_shm_get_va(shm, 0); arg = tee_shm_get_va(shm, offs);
if (IS_ERR(arg)) if (IS_ERR(arg))
return PTR_ERR(arg); return PTR_ERR(arg);
rpc_arg_offs = OPTEE_MSG_GET_ARG_SIZE(arg->num_params); rpc_arg_offs = OPTEE_MSG_GET_ARG_SIZE(arg->num_params);
rpc_arg = tee_shm_get_va(shm, rpc_arg_offs); rpc_arg = tee_shm_get_va(shm, offs + rpc_arg_offs);
if (IS_ERR(arg)) if (IS_ERR(arg))
return PTR_ERR(arg); return PTR_ERR(arg);
} }
...@@ -856,11 +891,11 @@ static int optee_smc_do_call_with_arg(struct tee_context *ctx, ...@@ -856,11 +891,11 @@ static int optee_smc_do_call_with_arg(struct tee_context *ctx,
if (rpc_arg && tee_shm_is_dynamic(shm)) { if (rpc_arg && tee_shm_is_dynamic(shm)) {
param.a0 = OPTEE_SMC_CALL_WITH_REGD_ARG; param.a0 = OPTEE_SMC_CALL_WITH_REGD_ARG;
reg_pair_from_64(&param.a1, &param.a2, (u_long)shm); reg_pair_from_64(&param.a1, &param.a2, (u_long)shm);
param.a3 = 0; param.a3 = offs;
} else { } else {
phys_addr_t parg; phys_addr_t parg;
rc = tee_shm_get_pa(shm, 0, &parg); rc = tee_shm_get_pa(shm, offs, &parg);
if (rc) if (rc)
return rc; return rc;
...@@ -912,17 +947,19 @@ static int optee_smc_do_call_with_arg(struct tee_context *ctx, ...@@ -912,17 +947,19 @@ static int optee_smc_do_call_with_arg(struct tee_context *ctx,
static int simple_call_with_arg(struct tee_context *ctx, u32 cmd) static int simple_call_with_arg(struct tee_context *ctx, u32 cmd)
{ {
struct optee_shm_arg_entry *entry;
struct optee_msg_arg *msg_arg; struct optee_msg_arg *msg_arg;
struct tee_shm *shm; struct tee_shm *shm;
u_int offs;
shm = optee_get_msg_arg(ctx, 0, &msg_arg); msg_arg = optee_get_msg_arg(ctx, 0, &entry, &shm, &offs);
if (IS_ERR(shm)) if (IS_ERR(msg_arg))
return PTR_ERR(shm); return PTR_ERR(msg_arg);
msg_arg->cmd = cmd; msg_arg->cmd = cmd;
optee_smc_do_call_with_arg(ctx, shm); optee_smc_do_call_with_arg(ctx, shm, offs);
tee_shm_free(shm); optee_free_msg_arg(ctx, entry, offs);
return 0; return 0;
} }
...@@ -1327,6 +1364,7 @@ static int optee_probe(struct platform_device *pdev) ...@@ -1327,6 +1364,7 @@ static int optee_probe(struct platform_device *pdev)
struct tee_device *teedev; struct tee_device *teedev;
struct tee_context *ctx; struct tee_context *ctx;
u32 max_notif_value; u32 max_notif_value;
u32 arg_cache_flags;
u32 sec_caps; u32 sec_caps;
int rc; int rc;
...@@ -1356,14 +1394,48 @@ static int optee_probe(struct platform_device *pdev) ...@@ -1356,14 +1394,48 @@ static int optee_probe(struct platform_device *pdev)
/* /*
* Try to use dynamic shared memory if possible * Try to use dynamic shared memory if possible
*/ */
if (sec_caps & OPTEE_SMC_SEC_CAP_DYNAMIC_SHM) if (sec_caps & OPTEE_SMC_SEC_CAP_DYNAMIC_SHM) {
/*
* If we have OPTEE_SMC_SEC_CAP_RPC_ARG we can ask
* optee_get_msg_arg() to pre-register (by having
* OPTEE_SHM_ARG_ALLOC_PRIV cleared) the page used to pass
* an argument struct.
*
* With the page is pre-registered we can use a non-zero
* offset for argument struct, this is indicated with
* OPTEE_SHM_ARG_SHARED.
*
* This means that optee_smc_do_call_with_arg() will use
* OPTEE_SMC_CALL_WITH_REGD_ARG for pre-registered pages.
*/
if (sec_caps & OPTEE_SMC_SEC_CAP_RPC_ARG)
arg_cache_flags = OPTEE_SHM_ARG_SHARED;
else
arg_cache_flags = OPTEE_SHM_ARG_ALLOC_PRIV;
pool = optee_shm_pool_alloc_pages(); pool = optee_shm_pool_alloc_pages();
}
/* /*
* If dynamic shared memory is not available or failed - try static one * If dynamic shared memory is not available or failed - try static one
*/ */
if (IS_ERR(pool) && (sec_caps & OPTEE_SMC_SEC_CAP_HAVE_RESERVED_SHM)) if (IS_ERR(pool) && (sec_caps & OPTEE_SMC_SEC_CAP_HAVE_RESERVED_SHM)) {
/*
* The static memory pool can use non-zero page offsets so
* let optee_get_msg_arg() know that with OPTEE_SHM_ARG_SHARED.
*
* optee_get_msg_arg() should not pre-register the
* allocated page used to pass an argument struct, this is
* indicated with OPTEE_SHM_ARG_ALLOC_PRIV.
*
* This means that optee_smc_do_call_with_arg() will use
* OPTEE_SMC_CALL_WITH_ARG if rpc_param_count is 0, else
* OPTEE_SMC_CALL_WITH_RPC_ARG.
*/
arg_cache_flags = OPTEE_SHM_ARG_SHARED |
OPTEE_SHM_ARG_ALLOC_PRIV;
pool = optee_config_shm_memremap(invoke_fn, &memremaped_shm); pool = optee_config_shm_memremap(invoke_fn, &memremaped_shm);
}
if (IS_ERR(pool)) if (IS_ERR(pool))
return PTR_ERR(pool); return PTR_ERR(pool);
...@@ -1406,6 +1478,7 @@ static int optee_probe(struct platform_device *pdev) ...@@ -1406,6 +1478,7 @@ static int optee_probe(struct platform_device *pdev)
optee_supp_init(&optee->supp); optee_supp_init(&optee->supp);
optee->smc.memremaped_shm = memremaped_shm; optee->smc.memremaped_shm = memremaped_shm;
optee->pool = pool; optee->pool = pool;
optee_shm_arg_cache_init(optee, arg_cache_flags);
platform_set_drvdata(pdev, optee); platform_set_drvdata(pdev, optee);
ctx = teedev_open(optee->teedev); ctx = teedev_open(optee->teedev);
...@@ -1473,6 +1546,7 @@ static int optee_probe(struct platform_device *pdev) ...@@ -1473,6 +1546,7 @@ static int optee_probe(struct platform_device *pdev)
err_close_ctx: err_close_ctx:
teedev_close_context(ctx); teedev_close_context(ctx);
err_supp_uninit: err_supp_uninit:
optee_shm_arg_cache_uninit(optee);
optee_supp_uninit(&optee->supp); optee_supp_uninit(&optee->supp);
mutex_destroy(&optee->call_queue.mutex); mutex_destroy(&optee->call_queue.mutex);
err_unreg_supp_teedev: err_unreg_supp_teedev:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment