Received: by 2002:a25:e74b:0:0:0:0:0 with SMTP id e72csp388494ybh; Wed, 22 Jul 2020 03:17:31 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxU2/ug9TfOKisqtUhbu/d5aoMEgh/997GJZPcxfKPnU986Mk3Y4be03za/NSfXXe6xu08r X-Received: by 2002:aa7:d0d1:: with SMTP id u17mr31031623edo.13.1595413051101; Wed, 22 Jul 2020 03:17:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1595413051; cv=none; d=google.com; s=arc-20160816; b=l7GdGohSHbkYpVlCYs9O/lDBFd3p8pKA5sEfsLhXfAIm0Iig/sS7fOu3qv6PcI8cEL dbgM9zaz50jK5fTE5tIeb9CakDdZ70pd6muagQTZyhv9xgSKchh6yuB7vSRU25ewfcVs tDb2z5qOgza7Wuzz+FI1a66qB07IGKkT6+rOSox6/GwMXy0EgJjXogotAoob4F3zZojn 1rW9IoCld903wn/hvzVcgNJGsm6FEDXX+dHbTlA1p1OEbCMdDkUF65d2EJzBgcSqn9hl DNAjEvz9E0lSugV811ef/S10Gxf/jsRCSS1IPKxQt3KC/Dszm37yUp3Wk6zAZa2yUBUn S8xQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :dkim-signature; bh=HBWwmeA/IVVyfDAoIWzsV0dODZtdyPNAP8hglAomxG8=; b=MSUjJ0PyWu9aFMFAE+sV+Jc4ISMR4x0yyIZgoE8GsMppbPaY5WI+nzyqMqF67gTXcu hAdmyBxBk1ww0GkII0CH5o8efzAUOC2Iyh4ywX2Z0QGUCLlqvxJAOXqSc3H8w7oE3cmY vvsNh02EkalctpMZe24Ks4KldYdcpx1RgIyz4ibuahhYhaadMZULaAE64FZL3ibcCqcU ZhvBN4/QC2Y3P8JhATPaqFEUA2Zebm/OcH2atBnXT5+ZwvAX2h6Dn4xqnTXZ8N4BsUW6 qWiqbw7WyGf3aG4H52lJzn/VX8NA2W0yZYV0AqqibkvsscAGIHIZzcThSu9hzql5DPqI U9Jw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@foundries-io.20150623.gappssmtp.com header.s=20150623 header.b=0KIRi2Dl; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id x94si16231749ede.416.2020.07.22.03.17.06; Wed, 22 Jul 2020 03:17:31 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@foundries-io.20150623.gappssmtp.com header.s=20150623 header.b=0KIRi2Dl; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729864AbgGVKQn (ORCPT + 99 others); Wed, 22 Jul 2020 06:16:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40726 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726821AbgGVKQm (ORCPT ); Wed, 22 Jul 2020 06:16:42 -0400 Received: from mail-wr1-x442.google.com (mail-wr1-x442.google.com [IPv6:2a00:1450:4864:20::442]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7587CC0619DC for ; Wed, 22 Jul 2020 03:16:42 -0700 (PDT) Received: by mail-wr1-x442.google.com with SMTP id q5so1336878wru.6 for ; Wed, 22 Jul 2020 03:16:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=foundries-io.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id; bh=HBWwmeA/IVVyfDAoIWzsV0dODZtdyPNAP8hglAomxG8=; b=0KIRi2DlDEyrMEf6ILG7Ierl0wKbynb6oiBADDAlgcBfOOgWrbieQzbK+nV+RtPC57 PtRANAhTkFyK7yrInEj3pOrMUQkPTokTMABsw+Xw5GQcMhEVDEmEA0CKBM/nzxwB8gze uRVCSr/0nK0CmqKMGNwPewuNkT3VGjLz5oBSXp8JIaRIIFA1s8dicdHr2NbF95Q52TaM N8aJH6xBnm4AI5HnjFvU6VYhxpZkqkYxXr33w+gURIr88KO9+y/gdQrkqaul3/tcI5X5 5tfi7S9zW9mAy//S1BR8bng/z90eIa+p/cRJbmtVXMIJ0S5T01TLdW0TrmG0HIJLL/Fj OhKQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=HBWwmeA/IVVyfDAoIWzsV0dODZtdyPNAP8hglAomxG8=; b=ielWfq/gGTaNskW+3hoqKjXTTP0JhtBpExFXVZdpYjYqPFMHe7KNgzDCQZSSgYWvJ5 ABMh90SZgmq9qq1NCpgHiHnABeZ2CmYYFAa7WsYUOuegsTUc/SwkPgOR8fYDeq4P5iJA uuvCVWX+urUSKHDnCdle59v46+FEEYkOYBhXsjqsWmdmrZg+8qa5zh2IV0gPgyY6WJea VPEar2pC0PBJOhlPjPcO9Zd92SJdGKchdhvftAM4LJiwO+jKqxSz1MNnguk58wVd1hwA aUg9IUVGWm/3h/WPcWgdG/p7P2Zem4hiaOTbEbKbEICFwi1os1HRsrT9hoYgnsEcdaD0 HtFA== X-Gm-Message-State: AOAM531g0PgMPHHwxkE9Em21bd96WPqQ/CjJbBQCyRQKN24le8IhLPk0 pN704FakH9EbtqwkoL9lj15md5oDkPQ= X-Received: by 2002:adf:b1ca:: with SMTP id r10mr32661549wra.150.1595413001116; Wed, 22 Jul 2020 03:16:41 -0700 (PDT) Received: from localhost.localdomain (126.red-83-36-179.dynamicip.rima-tde.net. [83.36.179.126]) by smtp.gmail.com with ESMTPSA id x204sm3911759wmg.2.2020.07.22.03.16.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 Jul 2020 03:16:40 -0700 (PDT) From: Jorge Ramirez-Ortiz To: jorge@foundries.io, jens.wiklander@linaro.org, sumit.garg@linaro.org Cc: tee-dev@lists.linaro.org, ricardo@foundries.io, mike@foundries.io, linux-kernel@vger.kernel.org Subject: [PATCHv3] drivers: optee: allow op-tee to access devices on the i2c bus Date: Wed, 22 Jul 2020 12:16:37 +0200 Message-Id: <20200722101637.8173-1-jorge@foundries.io> X-Mailer: git-send-email 2.17.1 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Some secure elements like NXP's SE050 sit on I2C buses. For OP-TEE to control this type of cryptographic devices it needs coordinated access to the bus, so collisions and RUNTIME_PM dont get in the way. This trampoline driver allow OP-TEE to access them. Signed-off-by: Jorge Ramirez-Ortiz --- v3: use from/to msg param to support all types of memory modify OPTEE_MSG_RPC_CMD_I2C_TRANSFER id drivers/tee/optee/optee_msg.h | 17 ++++++++ drivers/tee/optee/rpc.c | 80 +++++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+) diff --git a/drivers/tee/optee/optee_msg.h b/drivers/tee/optee/optee_msg.h index 795bc19ae17a..aa7cb19942b9 100644 --- a/drivers/tee/optee/optee_msg.h +++ b/drivers/tee/optee/optee_msg.h @@ -419,4 +419,21 @@ struct optee_msg_arg { */ #define OPTEE_MSG_RPC_CMD_SHM_FREE 7 +/* + * Access a device on an i2c bus + * + * [in] param[0].u.value.a mode: RD(0), WR(1) + * [in] param[0].u.value.b i2c adapter + * [in] param[0].u.value.c i2c chip + * + * [in/out] memref[1] buffer to exchange the transfer data + * with the secure world + * + * [out] param[0].u.value.a bytes transferred by the driver + */ +#define OPTEE_MSG_RPC_CMD_I2C_TRANSFER 21 +#define OPTEE_MSG_RPC_CMD_I2C_TRANSFER_RD 0 +#define OPTEE_MSG_RPC_CMD_I2C_TRANSFER_WR 1 + + #endif /* _OPTEE_MSG_H */ diff --git a/drivers/tee/optee/rpc.c b/drivers/tee/optee/rpc.c index b4ade54d1f28..2b2939c538f0 100644 --- a/drivers/tee/optee/rpc.c +++ b/drivers/tee/optee/rpc.c @@ -9,6 +9,7 @@ #include #include #include +#include #include "optee_private.h" #include "optee_smc.h" @@ -49,6 +50,82 @@ static void handle_rpc_func_cmd_get_time(struct optee_msg_arg *arg) arg->ret = TEEC_ERROR_BAD_PARAMETERS; } +static void handle_rpc_func_cmd_i2c_transfer(struct tee_context *ctx, + struct optee_msg_arg *arg) +{ + struct i2c_client client; + struct tee_param *params; + uint32_t type; + int i, ret; + size_t len; + char *buf; + uint32_t attr[] = { + TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT, + TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT, + TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_OUTPUT, + }; + + if (arg->num_params != ARRAY_SIZE(attr)) { + arg->ret = TEEC_ERROR_BAD_PARAMETERS; + return; + } + + params = kmalloc_array(arg->num_params, sizeof(struct tee_param), + GFP_KERNEL); + if (!params) { + arg->ret = TEEC_ERROR_OUT_OF_MEMORY; + return; + } + + if (optee_from_msg_param(params, arg->num_params, arg->params)) + goto bad; + + for (i = 0; i < arg->num_params; i++) { + type = params[i].attr & TEE_IOCTL_PARAM_ATTR_TYPE_MASK; + if (type != attr[i]) + goto bad; + } + + client.addr = params[0].u.value.c; + client.adapter = i2c_get_adapter(params[0].u.value.b); + if (!client.adapter) + goto bad; + + snprintf(client.name, I2C_NAME_SIZE, "i2c%d", client.adapter->nr); + + buf = params[1].u.memref.shm->kaddr; + len = params[1].u.memref.size; + + switch (params[0].u.value.a) { + case OPTEE_MSG_RPC_CMD_I2C_TRANSFER_RD: + ret = i2c_master_recv(&client, buf, len); + break; + case OPTEE_MSG_RPC_CMD_I2C_TRANSFER_WR: + ret = i2c_master_send(&client, buf, len); + break; + default: + i2c_put_adapter(client.adapter); + goto bad; + } + + if (ret >= 0) { + params[2].u.value.a = ret; + arg->ret = TEEC_SUCCESS; + } else { + arg->ret = TEEC_ERROR_COMMUNICATION; + } + + if (optee_to_msg_param(arg->params, arg->num_params, params)) + arg->ret = TEEC_ERROR_BAD_PARAMETERS; + + i2c_put_adapter(client.adapter); + kfree(params); + return; +bad: + kfree(params); + arg->ret = TEEC_ERROR_BAD_PARAMETERS; +} + static struct wq_entry *wq_entry_get(struct optee_wait_queue *wq, u32 key) { struct wq_entry *w; @@ -382,6 +459,9 @@ static void handle_rpc_func_cmd(struct tee_context *ctx, struct optee *optee, case OPTEE_MSG_RPC_CMD_SHM_FREE: handle_rpc_func_cmd_shm_free(ctx, arg); break; + case OPTEE_MSG_RPC_CMD_I2C_TRANSFER: + handle_rpc_func_cmd_i2c_transfer(ctx, arg); + break; default: handle_rpc_supp_cmd(ctx, arg); } -- 2.17.1