18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (c) 2015, Linaro Limited
48c2ecf20Sopenharmony_ci */
58c2ecf20Sopenharmony_ci#include <linux/arm-smccc.h>
68c2ecf20Sopenharmony_ci#include <linux/device.h>
78c2ecf20Sopenharmony_ci#include <linux/err.h>
88c2ecf20Sopenharmony_ci#include <linux/errno.h>
98c2ecf20Sopenharmony_ci#include <linux/mm.h>
108c2ecf20Sopenharmony_ci#include <linux/sched.h>
118c2ecf20Sopenharmony_ci#include <linux/slab.h>
128c2ecf20Sopenharmony_ci#include <linux/tee_drv.h>
138c2ecf20Sopenharmony_ci#include <linux/types.h>
148c2ecf20Sopenharmony_ci#include <linux/uaccess.h>
158c2ecf20Sopenharmony_ci#include "optee_private.h"
168c2ecf20Sopenharmony_ci#include "optee_smc.h"
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_cistruct optee_call_waiter {
198c2ecf20Sopenharmony_ci	struct list_head list_node;
208c2ecf20Sopenharmony_ci	struct completion c;
218c2ecf20Sopenharmony_ci};
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_cistatic void optee_cq_wait_init(struct optee_call_queue *cq,
248c2ecf20Sopenharmony_ci			       struct optee_call_waiter *w)
258c2ecf20Sopenharmony_ci{
268c2ecf20Sopenharmony_ci	/*
278c2ecf20Sopenharmony_ci	 * We're preparing to make a call to secure world. In case we can't
288c2ecf20Sopenharmony_ci	 * allocate a thread in secure world we'll end up waiting in
298c2ecf20Sopenharmony_ci	 * optee_cq_wait_for_completion().
308c2ecf20Sopenharmony_ci	 *
318c2ecf20Sopenharmony_ci	 * Normally if there's no contention in secure world the call will
328c2ecf20Sopenharmony_ci	 * complete and we can cleanup directly with optee_cq_wait_final().
338c2ecf20Sopenharmony_ci	 */
348c2ecf20Sopenharmony_ci	mutex_lock(&cq->mutex);
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci	/*
378c2ecf20Sopenharmony_ci	 * We add ourselves to the queue, but we don't wait. This
388c2ecf20Sopenharmony_ci	 * guarantees that we don't lose a completion if secure world
398c2ecf20Sopenharmony_ci	 * returns busy and another thread just exited and try to complete
408c2ecf20Sopenharmony_ci	 * someone.
418c2ecf20Sopenharmony_ci	 */
428c2ecf20Sopenharmony_ci	init_completion(&w->c);
438c2ecf20Sopenharmony_ci	list_add_tail(&w->list_node, &cq->waiters);
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci	mutex_unlock(&cq->mutex);
468c2ecf20Sopenharmony_ci}
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_cistatic void optee_cq_wait_for_completion(struct optee_call_queue *cq,
498c2ecf20Sopenharmony_ci					 struct optee_call_waiter *w)
508c2ecf20Sopenharmony_ci{
518c2ecf20Sopenharmony_ci	wait_for_completion(&w->c);
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci	mutex_lock(&cq->mutex);
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci	/* Move to end of list to get out of the way for other waiters */
568c2ecf20Sopenharmony_ci	list_del(&w->list_node);
578c2ecf20Sopenharmony_ci	reinit_completion(&w->c);
588c2ecf20Sopenharmony_ci	list_add_tail(&w->list_node, &cq->waiters);
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci	mutex_unlock(&cq->mutex);
618c2ecf20Sopenharmony_ci}
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_cistatic void optee_cq_complete_one(struct optee_call_queue *cq)
648c2ecf20Sopenharmony_ci{
658c2ecf20Sopenharmony_ci	struct optee_call_waiter *w;
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci	list_for_each_entry(w, &cq->waiters, list_node) {
688c2ecf20Sopenharmony_ci		if (!completion_done(&w->c)) {
698c2ecf20Sopenharmony_ci			complete(&w->c);
708c2ecf20Sopenharmony_ci			break;
718c2ecf20Sopenharmony_ci		}
728c2ecf20Sopenharmony_ci	}
738c2ecf20Sopenharmony_ci}
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_cistatic void optee_cq_wait_final(struct optee_call_queue *cq,
768c2ecf20Sopenharmony_ci				struct optee_call_waiter *w)
778c2ecf20Sopenharmony_ci{
788c2ecf20Sopenharmony_ci	/*
798c2ecf20Sopenharmony_ci	 * We're done with the call to secure world. The thread in secure
808c2ecf20Sopenharmony_ci	 * world that was used for this call is now available for some
818c2ecf20Sopenharmony_ci	 * other task to use.
828c2ecf20Sopenharmony_ci	 */
838c2ecf20Sopenharmony_ci	mutex_lock(&cq->mutex);
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci	/* Get out of the list */
868c2ecf20Sopenharmony_ci	list_del(&w->list_node);
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci	/* Wake up one eventual waiting task */
898c2ecf20Sopenharmony_ci	optee_cq_complete_one(cq);
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci	/*
928c2ecf20Sopenharmony_ci	 * If we're completed we've got a completion from another task that
938c2ecf20Sopenharmony_ci	 * was just done with its call to secure world. Since yet another
948c2ecf20Sopenharmony_ci	 * thread now is available in secure world wake up another eventual
958c2ecf20Sopenharmony_ci	 * waiting task.
968c2ecf20Sopenharmony_ci	 */
978c2ecf20Sopenharmony_ci	if (completion_done(&w->c))
988c2ecf20Sopenharmony_ci		optee_cq_complete_one(cq);
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci	mutex_unlock(&cq->mutex);
1018c2ecf20Sopenharmony_ci}
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci/* Requires the filpstate mutex to be held */
1048c2ecf20Sopenharmony_cistatic struct optee_session *find_session(struct optee_context_data *ctxdata,
1058c2ecf20Sopenharmony_ci					  u32 session_id)
1068c2ecf20Sopenharmony_ci{
1078c2ecf20Sopenharmony_ci	struct optee_session *sess;
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci	list_for_each_entry(sess, &ctxdata->sess_list, list_node)
1108c2ecf20Sopenharmony_ci		if (sess->session_id == session_id)
1118c2ecf20Sopenharmony_ci			return sess;
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci	return NULL;
1148c2ecf20Sopenharmony_ci}
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci/**
1178c2ecf20Sopenharmony_ci * optee_do_call_with_arg() - Do an SMC to OP-TEE in secure world
1188c2ecf20Sopenharmony_ci * @ctx:	calling context
1198c2ecf20Sopenharmony_ci * @parg:	physical address of message to pass to secure world
1208c2ecf20Sopenharmony_ci *
1218c2ecf20Sopenharmony_ci * Does and SMC to OP-TEE in secure world and handles eventual resulting
1228c2ecf20Sopenharmony_ci * Remote Procedure Calls (RPC) from OP-TEE.
1238c2ecf20Sopenharmony_ci *
1248c2ecf20Sopenharmony_ci * Returns return code from secure world, 0 is OK
1258c2ecf20Sopenharmony_ci */
1268c2ecf20Sopenharmony_ciu32 optee_do_call_with_arg(struct tee_context *ctx, phys_addr_t parg)
1278c2ecf20Sopenharmony_ci{
1288c2ecf20Sopenharmony_ci	struct optee *optee = tee_get_drvdata(ctx->teedev);
1298c2ecf20Sopenharmony_ci	struct optee_call_waiter w;
1308c2ecf20Sopenharmony_ci	struct optee_rpc_param param = { };
1318c2ecf20Sopenharmony_ci	struct optee_call_ctx call_ctx = { };
1328c2ecf20Sopenharmony_ci	u32 ret;
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci	param.a0 = OPTEE_SMC_CALL_WITH_ARG;
1358c2ecf20Sopenharmony_ci	reg_pair_from_64(&param.a1, &param.a2, parg);
1368c2ecf20Sopenharmony_ci	/* Initialize waiter */
1378c2ecf20Sopenharmony_ci	optee_cq_wait_init(&optee->call_queue, &w);
1388c2ecf20Sopenharmony_ci	while (true) {
1398c2ecf20Sopenharmony_ci		struct arm_smccc_res res;
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ci		optee->invoke_fn(param.a0, param.a1, param.a2, param.a3,
1428c2ecf20Sopenharmony_ci				 param.a4, param.a5, param.a6, param.a7,
1438c2ecf20Sopenharmony_ci				 &res);
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci		if (res.a0 == OPTEE_SMC_RETURN_ETHREAD_LIMIT) {
1468c2ecf20Sopenharmony_ci			/*
1478c2ecf20Sopenharmony_ci			 * Out of threads in secure world, wait for a thread
1488c2ecf20Sopenharmony_ci			 * become available.
1498c2ecf20Sopenharmony_ci			 */
1508c2ecf20Sopenharmony_ci			optee_cq_wait_for_completion(&optee->call_queue, &w);
1518c2ecf20Sopenharmony_ci		} else if (OPTEE_SMC_RETURN_IS_RPC(res.a0)) {
1528c2ecf20Sopenharmony_ci			if (need_resched())
1538c2ecf20Sopenharmony_ci				cond_resched();
1548c2ecf20Sopenharmony_ci			param.a0 = res.a0;
1558c2ecf20Sopenharmony_ci			param.a1 = res.a1;
1568c2ecf20Sopenharmony_ci			param.a2 = res.a2;
1578c2ecf20Sopenharmony_ci			param.a3 = res.a3;
1588c2ecf20Sopenharmony_ci			optee_handle_rpc(ctx, &param, &call_ctx);
1598c2ecf20Sopenharmony_ci		} else {
1608c2ecf20Sopenharmony_ci			ret = res.a0;
1618c2ecf20Sopenharmony_ci			break;
1628c2ecf20Sopenharmony_ci		}
1638c2ecf20Sopenharmony_ci	}
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci	optee_rpc_finalize_call(&call_ctx);
1668c2ecf20Sopenharmony_ci	/*
1678c2ecf20Sopenharmony_ci	 * We're done with our thread in secure world, if there's any
1688c2ecf20Sopenharmony_ci	 * thread waiters wake up one.
1698c2ecf20Sopenharmony_ci	 */
1708c2ecf20Sopenharmony_ci	optee_cq_wait_final(&optee->call_queue, &w);
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_ci	return ret;
1738c2ecf20Sopenharmony_ci}
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_cistatic struct tee_shm *get_msg_arg(struct tee_context *ctx, size_t num_params,
1768c2ecf20Sopenharmony_ci				   struct optee_msg_arg **msg_arg,
1778c2ecf20Sopenharmony_ci				   phys_addr_t *msg_parg)
1788c2ecf20Sopenharmony_ci{
1798c2ecf20Sopenharmony_ci	int rc;
1808c2ecf20Sopenharmony_ci	struct tee_shm *shm;
1818c2ecf20Sopenharmony_ci	struct optee_msg_arg *ma;
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_ci	shm = tee_shm_alloc(ctx, OPTEE_MSG_GET_ARG_SIZE(num_params),
1848c2ecf20Sopenharmony_ci			    TEE_SHM_MAPPED | TEE_SHM_PRIV);
1858c2ecf20Sopenharmony_ci	if (IS_ERR(shm))
1868c2ecf20Sopenharmony_ci		return shm;
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci	ma = tee_shm_get_va(shm, 0);
1898c2ecf20Sopenharmony_ci	if (IS_ERR(ma)) {
1908c2ecf20Sopenharmony_ci		rc = PTR_ERR(ma);
1918c2ecf20Sopenharmony_ci		goto out;
1928c2ecf20Sopenharmony_ci	}
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci	rc = tee_shm_get_pa(shm, 0, msg_parg);
1958c2ecf20Sopenharmony_ci	if (rc)
1968c2ecf20Sopenharmony_ci		goto out;
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_ci	memset(ma, 0, OPTEE_MSG_GET_ARG_SIZE(num_params));
1998c2ecf20Sopenharmony_ci	ma->num_params = num_params;
2008c2ecf20Sopenharmony_ci	*msg_arg = ma;
2018c2ecf20Sopenharmony_ciout:
2028c2ecf20Sopenharmony_ci	if (rc) {
2038c2ecf20Sopenharmony_ci		tee_shm_free(shm);
2048c2ecf20Sopenharmony_ci		return ERR_PTR(rc);
2058c2ecf20Sopenharmony_ci	}
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_ci	return shm;
2088c2ecf20Sopenharmony_ci}
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_ciint optee_open_session(struct tee_context *ctx,
2118c2ecf20Sopenharmony_ci		       struct tee_ioctl_open_session_arg *arg,
2128c2ecf20Sopenharmony_ci		       struct tee_param *param)
2138c2ecf20Sopenharmony_ci{
2148c2ecf20Sopenharmony_ci	struct optee_context_data *ctxdata = ctx->data;
2158c2ecf20Sopenharmony_ci	int rc;
2168c2ecf20Sopenharmony_ci	struct tee_shm *shm;
2178c2ecf20Sopenharmony_ci	struct optee_msg_arg *msg_arg;
2188c2ecf20Sopenharmony_ci	phys_addr_t msg_parg;
2198c2ecf20Sopenharmony_ci	struct optee_session *sess = NULL;
2208c2ecf20Sopenharmony_ci	uuid_t client_uuid;
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_ci	/* +2 for the meta parameters added below */
2238c2ecf20Sopenharmony_ci	shm = get_msg_arg(ctx, arg->num_params + 2, &msg_arg, &msg_parg);
2248c2ecf20Sopenharmony_ci	if (IS_ERR(shm))
2258c2ecf20Sopenharmony_ci		return PTR_ERR(shm);
2268c2ecf20Sopenharmony_ci
2278c2ecf20Sopenharmony_ci	msg_arg->cmd = OPTEE_MSG_CMD_OPEN_SESSION;
2288c2ecf20Sopenharmony_ci	msg_arg->cancel_id = arg->cancel_id;
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_ci	/*
2318c2ecf20Sopenharmony_ci	 * Initialize and add the meta parameters needed when opening a
2328c2ecf20Sopenharmony_ci	 * session.
2338c2ecf20Sopenharmony_ci	 */
2348c2ecf20Sopenharmony_ci	msg_arg->params[0].attr = OPTEE_MSG_ATTR_TYPE_VALUE_INPUT |
2358c2ecf20Sopenharmony_ci				  OPTEE_MSG_ATTR_META;
2368c2ecf20Sopenharmony_ci	msg_arg->params[1].attr = OPTEE_MSG_ATTR_TYPE_VALUE_INPUT |
2378c2ecf20Sopenharmony_ci				  OPTEE_MSG_ATTR_META;
2388c2ecf20Sopenharmony_ci	memcpy(&msg_arg->params[0].u.value, arg->uuid, sizeof(arg->uuid));
2398c2ecf20Sopenharmony_ci	msg_arg->params[1].u.value.c = arg->clnt_login;
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_ci	rc = tee_session_calc_client_uuid(&client_uuid, arg->clnt_login,
2428c2ecf20Sopenharmony_ci					  arg->clnt_uuid);
2438c2ecf20Sopenharmony_ci	if (rc)
2448c2ecf20Sopenharmony_ci		goto out;
2458c2ecf20Sopenharmony_ci	export_uuid(msg_arg->params[1].u.octets, &client_uuid);
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_ci	rc = optee_to_msg_param(msg_arg->params + 2, arg->num_params, param);
2488c2ecf20Sopenharmony_ci	if (rc)
2498c2ecf20Sopenharmony_ci		goto out;
2508c2ecf20Sopenharmony_ci
2518c2ecf20Sopenharmony_ci	sess = kzalloc(sizeof(*sess), GFP_KERNEL);
2528c2ecf20Sopenharmony_ci	if (!sess) {
2538c2ecf20Sopenharmony_ci		rc = -ENOMEM;
2548c2ecf20Sopenharmony_ci		goto out;
2558c2ecf20Sopenharmony_ci	}
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_ci	if (optee_do_call_with_arg(ctx, msg_parg)) {
2588c2ecf20Sopenharmony_ci		msg_arg->ret = TEEC_ERROR_COMMUNICATION;
2598c2ecf20Sopenharmony_ci		msg_arg->ret_origin = TEEC_ORIGIN_COMMS;
2608c2ecf20Sopenharmony_ci	}
2618c2ecf20Sopenharmony_ci
2628c2ecf20Sopenharmony_ci	if (msg_arg->ret == TEEC_SUCCESS) {
2638c2ecf20Sopenharmony_ci		/* A new session has been created, add it to the list. */
2648c2ecf20Sopenharmony_ci		sess->session_id = msg_arg->session;
2658c2ecf20Sopenharmony_ci		mutex_lock(&ctxdata->mutex);
2668c2ecf20Sopenharmony_ci		list_add(&sess->list_node, &ctxdata->sess_list);
2678c2ecf20Sopenharmony_ci		mutex_unlock(&ctxdata->mutex);
2688c2ecf20Sopenharmony_ci	} else {
2698c2ecf20Sopenharmony_ci		kfree(sess);
2708c2ecf20Sopenharmony_ci	}
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_ci	if (optee_from_msg_param(param, arg->num_params, msg_arg->params + 2)) {
2738c2ecf20Sopenharmony_ci		arg->ret = TEEC_ERROR_COMMUNICATION;
2748c2ecf20Sopenharmony_ci		arg->ret_origin = TEEC_ORIGIN_COMMS;
2758c2ecf20Sopenharmony_ci		/* Close session again to avoid leakage */
2768c2ecf20Sopenharmony_ci		optee_close_session(ctx, msg_arg->session);
2778c2ecf20Sopenharmony_ci	} else {
2788c2ecf20Sopenharmony_ci		arg->session = msg_arg->session;
2798c2ecf20Sopenharmony_ci		arg->ret = msg_arg->ret;
2808c2ecf20Sopenharmony_ci		arg->ret_origin = msg_arg->ret_origin;
2818c2ecf20Sopenharmony_ci	}
2828c2ecf20Sopenharmony_ciout:
2838c2ecf20Sopenharmony_ci	tee_shm_free(shm);
2848c2ecf20Sopenharmony_ci
2858c2ecf20Sopenharmony_ci	return rc;
2868c2ecf20Sopenharmony_ci}
2878c2ecf20Sopenharmony_ci
2888c2ecf20Sopenharmony_ciint optee_close_session(struct tee_context *ctx, u32 session)
2898c2ecf20Sopenharmony_ci{
2908c2ecf20Sopenharmony_ci	struct optee_context_data *ctxdata = ctx->data;
2918c2ecf20Sopenharmony_ci	struct tee_shm *shm;
2928c2ecf20Sopenharmony_ci	struct optee_msg_arg *msg_arg;
2938c2ecf20Sopenharmony_ci	phys_addr_t msg_parg;
2948c2ecf20Sopenharmony_ci	struct optee_session *sess;
2958c2ecf20Sopenharmony_ci
2968c2ecf20Sopenharmony_ci	/* Check that the session is valid and remove it from the list */
2978c2ecf20Sopenharmony_ci	mutex_lock(&ctxdata->mutex);
2988c2ecf20Sopenharmony_ci	sess = find_session(ctxdata, session);
2998c2ecf20Sopenharmony_ci	if (sess)
3008c2ecf20Sopenharmony_ci		list_del(&sess->list_node);
3018c2ecf20Sopenharmony_ci	mutex_unlock(&ctxdata->mutex);
3028c2ecf20Sopenharmony_ci	if (!sess)
3038c2ecf20Sopenharmony_ci		return -EINVAL;
3048c2ecf20Sopenharmony_ci	kfree(sess);
3058c2ecf20Sopenharmony_ci
3068c2ecf20Sopenharmony_ci	shm = get_msg_arg(ctx, 0, &msg_arg, &msg_parg);
3078c2ecf20Sopenharmony_ci	if (IS_ERR(shm))
3088c2ecf20Sopenharmony_ci		return PTR_ERR(shm);
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_ci	msg_arg->cmd = OPTEE_MSG_CMD_CLOSE_SESSION;
3118c2ecf20Sopenharmony_ci	msg_arg->session = session;
3128c2ecf20Sopenharmony_ci	optee_do_call_with_arg(ctx, msg_parg);
3138c2ecf20Sopenharmony_ci
3148c2ecf20Sopenharmony_ci	tee_shm_free(shm);
3158c2ecf20Sopenharmony_ci	return 0;
3168c2ecf20Sopenharmony_ci}
3178c2ecf20Sopenharmony_ci
3188c2ecf20Sopenharmony_ciint optee_invoke_func(struct tee_context *ctx, struct tee_ioctl_invoke_arg *arg,
3198c2ecf20Sopenharmony_ci		      struct tee_param *param)
3208c2ecf20Sopenharmony_ci{
3218c2ecf20Sopenharmony_ci	struct optee_context_data *ctxdata = ctx->data;
3228c2ecf20Sopenharmony_ci	struct tee_shm *shm;
3238c2ecf20Sopenharmony_ci	struct optee_msg_arg *msg_arg;
3248c2ecf20Sopenharmony_ci	phys_addr_t msg_parg;
3258c2ecf20Sopenharmony_ci	struct optee_session *sess;
3268c2ecf20Sopenharmony_ci	int rc;
3278c2ecf20Sopenharmony_ci
3288c2ecf20Sopenharmony_ci	/* Check that the session is valid */
3298c2ecf20Sopenharmony_ci	mutex_lock(&ctxdata->mutex);
3308c2ecf20Sopenharmony_ci	sess = find_session(ctxdata, arg->session);
3318c2ecf20Sopenharmony_ci	mutex_unlock(&ctxdata->mutex);
3328c2ecf20Sopenharmony_ci	if (!sess)
3338c2ecf20Sopenharmony_ci		return -EINVAL;
3348c2ecf20Sopenharmony_ci
3358c2ecf20Sopenharmony_ci	shm = get_msg_arg(ctx, arg->num_params, &msg_arg, &msg_parg);
3368c2ecf20Sopenharmony_ci	if (IS_ERR(shm))
3378c2ecf20Sopenharmony_ci		return PTR_ERR(shm);
3388c2ecf20Sopenharmony_ci	msg_arg->cmd = OPTEE_MSG_CMD_INVOKE_COMMAND;
3398c2ecf20Sopenharmony_ci	msg_arg->func = arg->func;
3408c2ecf20Sopenharmony_ci	msg_arg->session = arg->session;
3418c2ecf20Sopenharmony_ci	msg_arg->cancel_id = arg->cancel_id;
3428c2ecf20Sopenharmony_ci
3438c2ecf20Sopenharmony_ci	rc = optee_to_msg_param(msg_arg->params, arg->num_params, param);
3448c2ecf20Sopenharmony_ci	if (rc)
3458c2ecf20Sopenharmony_ci		goto out;
3468c2ecf20Sopenharmony_ci
3478c2ecf20Sopenharmony_ci	if (optee_do_call_with_arg(ctx, msg_parg)) {
3488c2ecf20Sopenharmony_ci		msg_arg->ret = TEEC_ERROR_COMMUNICATION;
3498c2ecf20Sopenharmony_ci		msg_arg->ret_origin = TEEC_ORIGIN_COMMS;
3508c2ecf20Sopenharmony_ci	}
3518c2ecf20Sopenharmony_ci
3528c2ecf20Sopenharmony_ci	if (optee_from_msg_param(param, arg->num_params, msg_arg->params)) {
3538c2ecf20Sopenharmony_ci		msg_arg->ret = TEEC_ERROR_COMMUNICATION;
3548c2ecf20Sopenharmony_ci		msg_arg->ret_origin = TEEC_ORIGIN_COMMS;
3558c2ecf20Sopenharmony_ci	}
3568c2ecf20Sopenharmony_ci
3578c2ecf20Sopenharmony_ci	arg->ret = msg_arg->ret;
3588c2ecf20Sopenharmony_ci	arg->ret_origin = msg_arg->ret_origin;
3598c2ecf20Sopenharmony_ciout:
3608c2ecf20Sopenharmony_ci	tee_shm_free(shm);
3618c2ecf20Sopenharmony_ci	return rc;
3628c2ecf20Sopenharmony_ci}
3638c2ecf20Sopenharmony_ci
3648c2ecf20Sopenharmony_ciint optee_cancel_req(struct tee_context *ctx, u32 cancel_id, u32 session)
3658c2ecf20Sopenharmony_ci{
3668c2ecf20Sopenharmony_ci	struct optee_context_data *ctxdata = ctx->data;
3678c2ecf20Sopenharmony_ci	struct tee_shm *shm;
3688c2ecf20Sopenharmony_ci	struct optee_msg_arg *msg_arg;
3698c2ecf20Sopenharmony_ci	phys_addr_t msg_parg;
3708c2ecf20Sopenharmony_ci	struct optee_session *sess;
3718c2ecf20Sopenharmony_ci
3728c2ecf20Sopenharmony_ci	/* Check that the session is valid */
3738c2ecf20Sopenharmony_ci	mutex_lock(&ctxdata->mutex);
3748c2ecf20Sopenharmony_ci	sess = find_session(ctxdata, session);
3758c2ecf20Sopenharmony_ci	mutex_unlock(&ctxdata->mutex);
3768c2ecf20Sopenharmony_ci	if (!sess)
3778c2ecf20Sopenharmony_ci		return -EINVAL;
3788c2ecf20Sopenharmony_ci
3798c2ecf20Sopenharmony_ci	shm = get_msg_arg(ctx, 0, &msg_arg, &msg_parg);
3808c2ecf20Sopenharmony_ci	if (IS_ERR(shm))
3818c2ecf20Sopenharmony_ci		return PTR_ERR(shm);
3828c2ecf20Sopenharmony_ci
3838c2ecf20Sopenharmony_ci	msg_arg->cmd = OPTEE_MSG_CMD_CANCEL;
3848c2ecf20Sopenharmony_ci	msg_arg->session = session;
3858c2ecf20Sopenharmony_ci	msg_arg->cancel_id = cancel_id;
3868c2ecf20Sopenharmony_ci	optee_do_call_with_arg(ctx, msg_parg);
3878c2ecf20Sopenharmony_ci
3888c2ecf20Sopenharmony_ci	tee_shm_free(shm);
3898c2ecf20Sopenharmony_ci	return 0;
3908c2ecf20Sopenharmony_ci}
3918c2ecf20Sopenharmony_ci
3928c2ecf20Sopenharmony_ci/**
3938c2ecf20Sopenharmony_ci * optee_enable_shm_cache() - Enables caching of some shared memory allocation
3948c2ecf20Sopenharmony_ci *			      in OP-TEE
3958c2ecf20Sopenharmony_ci * @optee:	main service struct
3968c2ecf20Sopenharmony_ci */
3978c2ecf20Sopenharmony_civoid optee_enable_shm_cache(struct optee *optee)
3988c2ecf20Sopenharmony_ci{
3998c2ecf20Sopenharmony_ci	struct optee_call_waiter w;
4008c2ecf20Sopenharmony_ci
4018c2ecf20Sopenharmony_ci	/* We need to retry until secure world isn't busy. */
4028c2ecf20Sopenharmony_ci	optee_cq_wait_init(&optee->call_queue, &w);
4038c2ecf20Sopenharmony_ci	while (true) {
4048c2ecf20Sopenharmony_ci		struct arm_smccc_res res;
4058c2ecf20Sopenharmony_ci
4068c2ecf20Sopenharmony_ci		optee->invoke_fn(OPTEE_SMC_ENABLE_SHM_CACHE, 0, 0, 0, 0, 0, 0,
4078c2ecf20Sopenharmony_ci				 0, &res);
4088c2ecf20Sopenharmony_ci		if (res.a0 == OPTEE_SMC_RETURN_OK)
4098c2ecf20Sopenharmony_ci			break;
4108c2ecf20Sopenharmony_ci		optee_cq_wait_for_completion(&optee->call_queue, &w);
4118c2ecf20Sopenharmony_ci	}
4128c2ecf20Sopenharmony_ci	optee_cq_wait_final(&optee->call_queue, &w);
4138c2ecf20Sopenharmony_ci}
4148c2ecf20Sopenharmony_ci
4158c2ecf20Sopenharmony_ci/**
4168c2ecf20Sopenharmony_ci * __optee_disable_shm_cache() - Disables caching of some shared memory
4178c2ecf20Sopenharmony_ci *                               allocation in OP-TEE
4188c2ecf20Sopenharmony_ci * @optee:	main service struct
4198c2ecf20Sopenharmony_ci * @is_mapped:	true if the cached shared memory addresses were mapped by this
4208c2ecf20Sopenharmony_ci *		kernel, are safe to dereference, and should be freed
4218c2ecf20Sopenharmony_ci */
4228c2ecf20Sopenharmony_cistatic void __optee_disable_shm_cache(struct optee *optee, bool is_mapped)
4238c2ecf20Sopenharmony_ci{
4248c2ecf20Sopenharmony_ci	struct optee_call_waiter w;
4258c2ecf20Sopenharmony_ci
4268c2ecf20Sopenharmony_ci	/* We need to retry until secure world isn't busy. */
4278c2ecf20Sopenharmony_ci	optee_cq_wait_init(&optee->call_queue, &w);
4288c2ecf20Sopenharmony_ci	while (true) {
4298c2ecf20Sopenharmony_ci		union {
4308c2ecf20Sopenharmony_ci			struct arm_smccc_res smccc;
4318c2ecf20Sopenharmony_ci			struct optee_smc_disable_shm_cache_result result;
4328c2ecf20Sopenharmony_ci		} res;
4338c2ecf20Sopenharmony_ci
4348c2ecf20Sopenharmony_ci		optee->invoke_fn(OPTEE_SMC_DISABLE_SHM_CACHE, 0, 0, 0, 0, 0, 0,
4358c2ecf20Sopenharmony_ci				 0, &res.smccc);
4368c2ecf20Sopenharmony_ci		if (res.result.status == OPTEE_SMC_RETURN_ENOTAVAIL)
4378c2ecf20Sopenharmony_ci			break; /* All shm's freed */
4388c2ecf20Sopenharmony_ci		if (res.result.status == OPTEE_SMC_RETURN_OK) {
4398c2ecf20Sopenharmony_ci			struct tee_shm *shm;
4408c2ecf20Sopenharmony_ci
4418c2ecf20Sopenharmony_ci			/*
4428c2ecf20Sopenharmony_ci			 * Shared memory references that were not mapped by
4438c2ecf20Sopenharmony_ci			 * this kernel must be ignored to prevent a crash.
4448c2ecf20Sopenharmony_ci			 */
4458c2ecf20Sopenharmony_ci			if (!is_mapped)
4468c2ecf20Sopenharmony_ci				continue;
4478c2ecf20Sopenharmony_ci
4488c2ecf20Sopenharmony_ci			shm = reg_pair_to_ptr(res.result.shm_upper32,
4498c2ecf20Sopenharmony_ci					      res.result.shm_lower32);
4508c2ecf20Sopenharmony_ci			tee_shm_free(shm);
4518c2ecf20Sopenharmony_ci		} else {
4528c2ecf20Sopenharmony_ci			optee_cq_wait_for_completion(&optee->call_queue, &w);
4538c2ecf20Sopenharmony_ci		}
4548c2ecf20Sopenharmony_ci	}
4558c2ecf20Sopenharmony_ci	optee_cq_wait_final(&optee->call_queue, &w);
4568c2ecf20Sopenharmony_ci}
4578c2ecf20Sopenharmony_ci
4588c2ecf20Sopenharmony_ci/**
4598c2ecf20Sopenharmony_ci * optee_disable_shm_cache() - Disables caching of mapped shared memory
4608c2ecf20Sopenharmony_ci *                             allocations in OP-TEE
4618c2ecf20Sopenharmony_ci * @optee:	main service struct
4628c2ecf20Sopenharmony_ci */
4638c2ecf20Sopenharmony_civoid optee_disable_shm_cache(struct optee *optee)
4648c2ecf20Sopenharmony_ci{
4658c2ecf20Sopenharmony_ci	return __optee_disable_shm_cache(optee, true);
4668c2ecf20Sopenharmony_ci}
4678c2ecf20Sopenharmony_ci
4688c2ecf20Sopenharmony_ci/**
4698c2ecf20Sopenharmony_ci * optee_disable_unmapped_shm_cache() - Disables caching of shared memory
4708c2ecf20Sopenharmony_ci *                                      allocations in OP-TEE which are not
4718c2ecf20Sopenharmony_ci *                                      currently mapped
4728c2ecf20Sopenharmony_ci * @optee:	main service struct
4738c2ecf20Sopenharmony_ci */
4748c2ecf20Sopenharmony_civoid optee_disable_unmapped_shm_cache(struct optee *optee)
4758c2ecf20Sopenharmony_ci{
4768c2ecf20Sopenharmony_ci	return __optee_disable_shm_cache(optee, false);
4778c2ecf20Sopenharmony_ci}
4788c2ecf20Sopenharmony_ci
4798c2ecf20Sopenharmony_ci#define PAGELIST_ENTRIES_PER_PAGE				\
4808c2ecf20Sopenharmony_ci	((OPTEE_MSG_NONCONTIG_PAGE_SIZE / sizeof(u64)) - 1)
4818c2ecf20Sopenharmony_ci
4828c2ecf20Sopenharmony_ci/**
4838c2ecf20Sopenharmony_ci * optee_fill_pages_list() - write list of user pages to given shared
4848c2ecf20Sopenharmony_ci * buffer.
4858c2ecf20Sopenharmony_ci *
4868c2ecf20Sopenharmony_ci * @dst: page-aligned buffer where list of pages will be stored
4878c2ecf20Sopenharmony_ci * @pages: array of pages that represents shared buffer
4888c2ecf20Sopenharmony_ci * @num_pages: number of entries in @pages
4898c2ecf20Sopenharmony_ci * @page_offset: offset of user buffer from page start
4908c2ecf20Sopenharmony_ci *
4918c2ecf20Sopenharmony_ci * @dst should be big enough to hold list of user page addresses and
4928c2ecf20Sopenharmony_ci *	links to the next pages of buffer
4938c2ecf20Sopenharmony_ci */
4948c2ecf20Sopenharmony_civoid optee_fill_pages_list(u64 *dst, struct page **pages, int num_pages,
4958c2ecf20Sopenharmony_ci			   size_t page_offset)
4968c2ecf20Sopenharmony_ci{
4978c2ecf20Sopenharmony_ci	int n = 0;
4988c2ecf20Sopenharmony_ci	phys_addr_t optee_page;
4998c2ecf20Sopenharmony_ci	/*
5008c2ecf20Sopenharmony_ci	 * Refer to OPTEE_MSG_ATTR_NONCONTIG description in optee_msg.h
5018c2ecf20Sopenharmony_ci	 * for details.
5028c2ecf20Sopenharmony_ci	 */
5038c2ecf20Sopenharmony_ci	struct {
5048c2ecf20Sopenharmony_ci		u64 pages_list[PAGELIST_ENTRIES_PER_PAGE];
5058c2ecf20Sopenharmony_ci		u64 next_page_data;
5068c2ecf20Sopenharmony_ci	} *pages_data;
5078c2ecf20Sopenharmony_ci
5088c2ecf20Sopenharmony_ci	/*
5098c2ecf20Sopenharmony_ci	 * Currently OP-TEE uses 4k page size and it does not looks
5108c2ecf20Sopenharmony_ci	 * like this will change in the future.  On other hand, there are
5118c2ecf20Sopenharmony_ci	 * no know ARM architectures with page size < 4k.
5128c2ecf20Sopenharmony_ci	 * Thus the next built assert looks redundant. But the following
5138c2ecf20Sopenharmony_ci	 * code heavily relies on this assumption, so it is better be
5148c2ecf20Sopenharmony_ci	 * safe than sorry.
5158c2ecf20Sopenharmony_ci	 */
5168c2ecf20Sopenharmony_ci	BUILD_BUG_ON(PAGE_SIZE < OPTEE_MSG_NONCONTIG_PAGE_SIZE);
5178c2ecf20Sopenharmony_ci
5188c2ecf20Sopenharmony_ci	pages_data = (void *)dst;
5198c2ecf20Sopenharmony_ci	/*
5208c2ecf20Sopenharmony_ci	 * If linux page is bigger than 4k, and user buffer offset is
5218c2ecf20Sopenharmony_ci	 * larger than 4k/8k/12k/etc this will skip first 4k pages,
5228c2ecf20Sopenharmony_ci	 * because they bear no value data for OP-TEE.
5238c2ecf20Sopenharmony_ci	 */
5248c2ecf20Sopenharmony_ci	optee_page = page_to_phys(*pages) +
5258c2ecf20Sopenharmony_ci		round_down(page_offset, OPTEE_MSG_NONCONTIG_PAGE_SIZE);
5268c2ecf20Sopenharmony_ci
5278c2ecf20Sopenharmony_ci	while (true) {
5288c2ecf20Sopenharmony_ci		pages_data->pages_list[n++] = optee_page;
5298c2ecf20Sopenharmony_ci
5308c2ecf20Sopenharmony_ci		if (n == PAGELIST_ENTRIES_PER_PAGE) {
5318c2ecf20Sopenharmony_ci			pages_data->next_page_data =
5328c2ecf20Sopenharmony_ci				virt_to_phys(pages_data + 1);
5338c2ecf20Sopenharmony_ci			pages_data++;
5348c2ecf20Sopenharmony_ci			n = 0;
5358c2ecf20Sopenharmony_ci		}
5368c2ecf20Sopenharmony_ci
5378c2ecf20Sopenharmony_ci		optee_page += OPTEE_MSG_NONCONTIG_PAGE_SIZE;
5388c2ecf20Sopenharmony_ci		if (!(optee_page & ~PAGE_MASK)) {
5398c2ecf20Sopenharmony_ci			if (!--num_pages)
5408c2ecf20Sopenharmony_ci				break;
5418c2ecf20Sopenharmony_ci			pages++;
5428c2ecf20Sopenharmony_ci			optee_page = page_to_phys(*pages);
5438c2ecf20Sopenharmony_ci		}
5448c2ecf20Sopenharmony_ci	}
5458c2ecf20Sopenharmony_ci}
5468c2ecf20Sopenharmony_ci
5478c2ecf20Sopenharmony_ci/*
5488c2ecf20Sopenharmony_ci * The final entry in each pagelist page is a pointer to the next
5498c2ecf20Sopenharmony_ci * pagelist page.
5508c2ecf20Sopenharmony_ci */
5518c2ecf20Sopenharmony_cistatic size_t get_pages_list_size(size_t num_entries)
5528c2ecf20Sopenharmony_ci{
5538c2ecf20Sopenharmony_ci	int pages = DIV_ROUND_UP(num_entries, PAGELIST_ENTRIES_PER_PAGE);
5548c2ecf20Sopenharmony_ci
5558c2ecf20Sopenharmony_ci	return pages * OPTEE_MSG_NONCONTIG_PAGE_SIZE;
5568c2ecf20Sopenharmony_ci}
5578c2ecf20Sopenharmony_ci
5588c2ecf20Sopenharmony_ciu64 *optee_allocate_pages_list(size_t num_entries)
5598c2ecf20Sopenharmony_ci{
5608c2ecf20Sopenharmony_ci	return alloc_pages_exact(get_pages_list_size(num_entries), GFP_KERNEL);
5618c2ecf20Sopenharmony_ci}
5628c2ecf20Sopenharmony_ci
5638c2ecf20Sopenharmony_civoid optee_free_pages_list(void *list, size_t num_entries)
5648c2ecf20Sopenharmony_ci{
5658c2ecf20Sopenharmony_ci	free_pages_exact(list, get_pages_list_size(num_entries));
5668c2ecf20Sopenharmony_ci}
5678c2ecf20Sopenharmony_ci
5688c2ecf20Sopenharmony_cistatic bool is_normal_memory(pgprot_t p)
5698c2ecf20Sopenharmony_ci{
5708c2ecf20Sopenharmony_ci#if defined(CONFIG_ARM)
5718c2ecf20Sopenharmony_ci	return (((pgprot_val(p) & L_PTE_MT_MASK) == L_PTE_MT_WRITEALLOC) ||
5728c2ecf20Sopenharmony_ci		((pgprot_val(p) & L_PTE_MT_MASK) == L_PTE_MT_WRITEBACK));
5738c2ecf20Sopenharmony_ci#elif defined(CONFIG_ARM64)
5748c2ecf20Sopenharmony_ci	return (pgprot_val(p) & PTE_ATTRINDX_MASK) == PTE_ATTRINDX(MT_NORMAL);
5758c2ecf20Sopenharmony_ci#else
5768c2ecf20Sopenharmony_ci#error "Unuspported architecture"
5778c2ecf20Sopenharmony_ci#endif
5788c2ecf20Sopenharmony_ci}
5798c2ecf20Sopenharmony_ci
5808c2ecf20Sopenharmony_cistatic int __check_mem_type(struct vm_area_struct *vma, unsigned long end)
5818c2ecf20Sopenharmony_ci{
5828c2ecf20Sopenharmony_ci	while (vma && is_normal_memory(vma->vm_page_prot)) {
5838c2ecf20Sopenharmony_ci		if (vma->vm_end >= end)
5848c2ecf20Sopenharmony_ci			return 0;
5858c2ecf20Sopenharmony_ci		vma = vma->vm_next;
5868c2ecf20Sopenharmony_ci	}
5878c2ecf20Sopenharmony_ci
5888c2ecf20Sopenharmony_ci	return -EINVAL;
5898c2ecf20Sopenharmony_ci}
5908c2ecf20Sopenharmony_ci
5918c2ecf20Sopenharmony_cistatic int check_mem_type(unsigned long start, size_t num_pages)
5928c2ecf20Sopenharmony_ci{
5938c2ecf20Sopenharmony_ci	struct mm_struct *mm = current->mm;
5948c2ecf20Sopenharmony_ci	int rc;
5958c2ecf20Sopenharmony_ci
5968c2ecf20Sopenharmony_ci	/*
5978c2ecf20Sopenharmony_ci	 * Allow kernel address to register with OP-TEE as kernel
5988c2ecf20Sopenharmony_ci	 * pages are configured as normal memory only.
5998c2ecf20Sopenharmony_ci	 */
6008c2ecf20Sopenharmony_ci	if (virt_addr_valid(start))
6018c2ecf20Sopenharmony_ci		return 0;
6028c2ecf20Sopenharmony_ci
6038c2ecf20Sopenharmony_ci	mmap_read_lock(mm);
6048c2ecf20Sopenharmony_ci	rc = __check_mem_type(find_vma(mm, start),
6058c2ecf20Sopenharmony_ci			      start + num_pages * PAGE_SIZE);
6068c2ecf20Sopenharmony_ci	mmap_read_unlock(mm);
6078c2ecf20Sopenharmony_ci
6088c2ecf20Sopenharmony_ci	return rc;
6098c2ecf20Sopenharmony_ci}
6108c2ecf20Sopenharmony_ci
6118c2ecf20Sopenharmony_ciint optee_shm_register(struct tee_context *ctx, struct tee_shm *shm,
6128c2ecf20Sopenharmony_ci		       struct page **pages, size_t num_pages,
6138c2ecf20Sopenharmony_ci		       unsigned long start)
6148c2ecf20Sopenharmony_ci{
6158c2ecf20Sopenharmony_ci	struct tee_shm *shm_arg = NULL;
6168c2ecf20Sopenharmony_ci	struct optee_msg_arg *msg_arg;
6178c2ecf20Sopenharmony_ci	u64 *pages_list;
6188c2ecf20Sopenharmony_ci	phys_addr_t msg_parg;
6198c2ecf20Sopenharmony_ci	int rc;
6208c2ecf20Sopenharmony_ci
6218c2ecf20Sopenharmony_ci	if (!num_pages)
6228c2ecf20Sopenharmony_ci		return -EINVAL;
6238c2ecf20Sopenharmony_ci
6248c2ecf20Sopenharmony_ci	rc = check_mem_type(start, num_pages);
6258c2ecf20Sopenharmony_ci	if (rc)
6268c2ecf20Sopenharmony_ci		return rc;
6278c2ecf20Sopenharmony_ci
6288c2ecf20Sopenharmony_ci	pages_list = optee_allocate_pages_list(num_pages);
6298c2ecf20Sopenharmony_ci	if (!pages_list)
6308c2ecf20Sopenharmony_ci		return -ENOMEM;
6318c2ecf20Sopenharmony_ci
6328c2ecf20Sopenharmony_ci	shm_arg = get_msg_arg(ctx, 1, &msg_arg, &msg_parg);
6338c2ecf20Sopenharmony_ci	if (IS_ERR(shm_arg)) {
6348c2ecf20Sopenharmony_ci		rc = PTR_ERR(shm_arg);
6358c2ecf20Sopenharmony_ci		goto out;
6368c2ecf20Sopenharmony_ci	}
6378c2ecf20Sopenharmony_ci
6388c2ecf20Sopenharmony_ci	optee_fill_pages_list(pages_list, pages, num_pages,
6398c2ecf20Sopenharmony_ci			      tee_shm_get_page_offset(shm));
6408c2ecf20Sopenharmony_ci
6418c2ecf20Sopenharmony_ci	msg_arg->cmd = OPTEE_MSG_CMD_REGISTER_SHM;
6428c2ecf20Sopenharmony_ci	msg_arg->params->attr = OPTEE_MSG_ATTR_TYPE_TMEM_OUTPUT |
6438c2ecf20Sopenharmony_ci				OPTEE_MSG_ATTR_NONCONTIG;
6448c2ecf20Sopenharmony_ci	msg_arg->params->u.tmem.shm_ref = (unsigned long)shm;
6458c2ecf20Sopenharmony_ci	msg_arg->params->u.tmem.size = tee_shm_get_size(shm);
6468c2ecf20Sopenharmony_ci	/*
6478c2ecf20Sopenharmony_ci	 * In the least bits of msg_arg->params->u.tmem.buf_ptr we
6488c2ecf20Sopenharmony_ci	 * store buffer offset from 4k page, as described in OP-TEE ABI.
6498c2ecf20Sopenharmony_ci	 */
6508c2ecf20Sopenharmony_ci	msg_arg->params->u.tmem.buf_ptr = virt_to_phys(pages_list) |
6518c2ecf20Sopenharmony_ci	  (tee_shm_get_page_offset(shm) & (OPTEE_MSG_NONCONTIG_PAGE_SIZE - 1));
6528c2ecf20Sopenharmony_ci
6538c2ecf20Sopenharmony_ci	if (optee_do_call_with_arg(ctx, msg_parg) ||
6548c2ecf20Sopenharmony_ci	    msg_arg->ret != TEEC_SUCCESS)
6558c2ecf20Sopenharmony_ci		rc = -EINVAL;
6568c2ecf20Sopenharmony_ci
6578c2ecf20Sopenharmony_ci	tee_shm_free(shm_arg);
6588c2ecf20Sopenharmony_ciout:
6598c2ecf20Sopenharmony_ci	optee_free_pages_list(pages_list, num_pages);
6608c2ecf20Sopenharmony_ci	return rc;
6618c2ecf20Sopenharmony_ci}
6628c2ecf20Sopenharmony_ci
6638c2ecf20Sopenharmony_ciint optee_shm_unregister(struct tee_context *ctx, struct tee_shm *shm)
6648c2ecf20Sopenharmony_ci{
6658c2ecf20Sopenharmony_ci	struct tee_shm *shm_arg;
6668c2ecf20Sopenharmony_ci	struct optee_msg_arg *msg_arg;
6678c2ecf20Sopenharmony_ci	phys_addr_t msg_parg;
6688c2ecf20Sopenharmony_ci	int rc = 0;
6698c2ecf20Sopenharmony_ci
6708c2ecf20Sopenharmony_ci	shm_arg = get_msg_arg(ctx, 1, &msg_arg, &msg_parg);
6718c2ecf20Sopenharmony_ci	if (IS_ERR(shm_arg))
6728c2ecf20Sopenharmony_ci		return PTR_ERR(shm_arg);
6738c2ecf20Sopenharmony_ci
6748c2ecf20Sopenharmony_ci	msg_arg->cmd = OPTEE_MSG_CMD_UNREGISTER_SHM;
6758c2ecf20Sopenharmony_ci
6768c2ecf20Sopenharmony_ci	msg_arg->params[0].attr = OPTEE_MSG_ATTR_TYPE_RMEM_INPUT;
6778c2ecf20Sopenharmony_ci	msg_arg->params[0].u.rmem.shm_ref = (unsigned long)shm;
6788c2ecf20Sopenharmony_ci
6798c2ecf20Sopenharmony_ci	if (optee_do_call_with_arg(ctx, msg_parg) ||
6808c2ecf20Sopenharmony_ci	    msg_arg->ret != TEEC_SUCCESS)
6818c2ecf20Sopenharmony_ci		rc = -EINVAL;
6828c2ecf20Sopenharmony_ci	tee_shm_free(shm_arg);
6838c2ecf20Sopenharmony_ci	return rc;
6848c2ecf20Sopenharmony_ci}
6858c2ecf20Sopenharmony_ci
6868c2ecf20Sopenharmony_ciint optee_shm_register_supp(struct tee_context *ctx, struct tee_shm *shm,
6878c2ecf20Sopenharmony_ci			    struct page **pages, size_t num_pages,
6888c2ecf20Sopenharmony_ci			    unsigned long start)
6898c2ecf20Sopenharmony_ci{
6908c2ecf20Sopenharmony_ci	/*
6918c2ecf20Sopenharmony_ci	 * We don't want to register supplicant memory in OP-TEE.
6928c2ecf20Sopenharmony_ci	 * Instead information about it will be passed in RPC code.
6938c2ecf20Sopenharmony_ci	 */
6948c2ecf20Sopenharmony_ci	return check_mem_type(start, num_pages);
6958c2ecf20Sopenharmony_ci}
6968c2ecf20Sopenharmony_ci
6978c2ecf20Sopenharmony_ciint optee_shm_unregister_supp(struct tee_context *ctx, struct tee_shm *shm)
6988c2ecf20Sopenharmony_ci{
6998c2ecf20Sopenharmony_ci	return 0;
7008c2ecf20Sopenharmony_ci}
701