Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp662045pxa; Wed, 5 Aug 2020 09:49:11 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxoLAjVqmR9r9yhSsKE58WKVhnS8Cy2ogCDuoViAsFxWbwlqhMNe4RqfBRBsgynyFSLYbPj X-Received: by 2002:a05:6402:3070:: with SMTP id bs16mr120192edb.269.1596646151189; Wed, 05 Aug 2020 09:49:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1596646151; cv=none; d=google.com; s=arc-20160816; b=tfq2QssOIoiBkqogaCk+IOM+aa7TLCVonL+F29z6RGC3YaG2d87LDO66mpFRiGDUZK 9yH9lnCZt7xTI/oFe6m/Sy8noHBSwz+/wM0z0IQg16UAZjT8x7bKcTgQnaE7K6D/ACwq Hla6eBWmy+PKU/PwANnHBrig/I3QHN6Pf1g/gLOSdXdrXFWJbSa51uUAigiDU4MOe/BI fobyMAlefZ5tv94OzBZGFFQTOCMqbJUuCeGNt5qD1JELKTvbg9pt5RY5P1ENtMTnkHxY /Jb4ueoyAZiu9+CU3qTBoJ/LAFHGVPuspw/6Hwb5ERNO6G7iYEghPRAuSYLtlOSPZnVx rwUw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=4sZV1/gQN/258iL0rr5Xs5RZ6T6r7SkqFDya4T9+hhQ=; b=V2BhJHzy6iTxRPIqQxZXTrD5ZnMy1Q9z7ym7MopR1b3Wn2F63cc+RLJLwXEr9JrtMX W9+xoX8sKbuy6LNVKzGjQtfnjlGE29Iaa1tUUqqEb+I7DG35+aiM7srN4zhNSIrN2Zuy Ua0UFSZJDnQh3a4mG63jvgcp61t2rYoPqbJ1dm9evYYsHm55oZmyGf/kPxTVbqr7SGOC T4IrCea0ZdTVY79x5PZVVKubkLg7lmlN0TowcNYb979qTqoIZzXJptteEd6t2PWY14NI j4HGg7GprNOYGROoDEph6r2YgB71K2TvzrC4HOQJOuvHqoltCuNOSQLXmJqmWqVqY8mJ qE2Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=E9b9xRUZ; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id e19si1675214ejc.215.2020.08.05.09.48.46; Wed, 05 Aug 2020 09:49:11 -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=@linaro.org header.s=google header.b=E9b9xRUZ; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726396AbgHEQsh (ORCPT + 99 others); Wed, 5 Aug 2020 12:48:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46370 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728037AbgHEQqQ (ORCPT ); Wed, 5 Aug 2020 12:46:16 -0400 Received: from mail-lj1-x242.google.com (mail-lj1-x242.google.com [IPv6:2a00:1450:4864:20::242]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D62E5C0086D6 for ; Wed, 5 Aug 2020 07:24:09 -0700 (PDT) Received: by mail-lj1-x242.google.com with SMTP id v9so13425676ljk.6 for ; Wed, 05 Aug 2020 07:24:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=4sZV1/gQN/258iL0rr5Xs5RZ6T6r7SkqFDya4T9+hhQ=; b=E9b9xRUZtCoNc7BmcnjKZKhPMW4C6kTS55eaU+9K6uRzDuW4TNj3CRV97WvzO/of2Y zwdRFO6qTxVL/8AJOZFe2VwM18ZDpa2zt7HE8TMzl/K9XNvba6QLBigNU4LDkz9fEp8h TozZAI8dmNwaLYwAj+rpf5m+fZxw1RFzua2mawTi4OcXBq0HskcqEmw7dfMK3gjhlI1f 6+d48Ygix+b6dZvLFiZNPH1GLGnwyTP2eBGGnUlCxJzjYvmkqZ5LhvG60bflC5NfrouR qteTlbkjDme/q3Om8n6GR6GNVSRNYYXYVd93qBD8bFFI+1Ok7+GkCFm4Mbwy/lZUE3se BxTQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=4sZV1/gQN/258iL0rr5Xs5RZ6T6r7SkqFDya4T9+hhQ=; b=BagE2tJGSGl1v1FIKI3lsO5tFWuaajOWfP6T7ecXxthnEf6rSmDeNly6ZPbc1x2Tgo Uq8NoEkSulX5mokfH2KPxGrlSOQRAHYCEm4/qC6Wb4soiAsuVRIYhwK2Kod7HJ26nXhh nwII8xgXjnf3DySfLn1OcASD5mQBkVJZbGMb2IUNRv8PAfGM3Ps8sJ31g2BbmlJ7fDCu J4GZ24weZRemVCsMPpIh+LcC4s7LRqVriCIIKVA5ce9EOKKMqgEtHEctz0oamSbSfWmW JxfUDCi5XJs7aPsXlJY0kCUCEyQMyD8VdnpzM3U5yURrHvfrNJJwiXut7oXMGbAD2wyL Sd6w== X-Gm-Message-State: AOAM533bEFgE+r0vQ12opG4O+h8ZjlXo9d554FLkX8/y226ZVkpcqzub oekwywQlRq1uAn8q6GoRQ2SzYw== X-Received: by 2002:a05:651c:3c5:: with SMTP id f5mr1467608ljp.209.1596637447389; Wed, 05 Aug 2020 07:24:07 -0700 (PDT) Received: from jade (h-249-223.A175.priv.bahnhof.se. [98.128.249.223]) by smtp.gmail.com with ESMTPSA id e69sm1172866lfd.21.2020.08.05.07.24.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Aug 2020 07:24:06 -0700 (PDT) Date: Wed, 5 Aug 2020 16:24:04 +0200 From: Jens Wiklander To: "Jorge Ramirez-Ortiz, Foundries" Cc: sumit.garg@linaro.org, ricardo@foundries.io, mike@foundries.io, tee-dev@lists.linaro.org, linux-kernel@vger.kernel.org Subject: Re: [PATCHv6] drivers: optee: allow op-tee to access devices on the i2c bus Message-ID: <20200805142404.GA550721@jade> References: <20200722212105.1798-1-jorge@foundries.io> <20200805133501.GA8360@trex> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20200805133501.GA8360@trex> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Aug 05, 2020 at 03:35:01PM +0200, Jorge Ramirez-Ortiz, Foundries wrote: > On 22/07/20, Jorge Ramirez-Ortiz wrote: > > 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. > > Tested on imx8mm LPDDR4 > > > > Signed-off-by: Jorge Ramirez-Ortiz > > --- > > v6: compile out if CONFIG_I2C not enabled > > v5: alphabetic order of includes > > v4: remove unnecessary extra line in optee_msg.h > > v3: use from/to msg param to support all types of memory > > modify OPTEE_MSG_RPC_CMD_I2C_TRANSFER message id > > > > drivers/tee/optee/optee_msg.h | 16 +++++++ > > drivers/tee/optee/rpc.c | 88 +++++++++++++++++++++++++++++++++++ > > 2 files changed, 104 insertions(+) > > > > diff --git a/drivers/tee/optee/optee_msg.h b/drivers/tee/optee/optee_msg.h > > index 795bc19ae17a..14b580f55356 100644 > > --- a/drivers/tee/optee/optee_msg.h > > +++ b/drivers/tee/optee/optee_msg.h > > @@ -419,4 +419,20 @@ 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..5fd5c6c93896 100644 > > --- a/drivers/tee/optee/rpc.c > > +++ b/drivers/tee/optee/rpc.c > > @@ -7,6 +7,7 @@ > > > > #include > > #include > > +#include > > #include > > #include > > #include "optee_private.h" > > @@ -49,6 +50,90 @@ static void handle_rpc_func_cmd_get_time(struct optee_msg_arg *arg) > > arg->ret = TEEC_ERROR_BAD_PARAMETERS; > > } > > > > +#if IS_ENABLED(CONFIG_I2C) > > +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; > > +} > > +#else > > +static void handle_rpc_func_cmd_i2c_transfer(struct tee_context *ctx, > > + struct optee_msg_arg *arg) > > +{ > > + arg->ret = TEEC_ERROR_COMMUNICATION; > > +} > > +#endif > > + > > static struct wq_entry *wq_entry_get(struct optee_wait_queue *wq, u32 key) > > { > > struct wq_entry *w; > > @@ -382,6 +467,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); > > } > > > any comments please? As you know we're still reviewing the secure world counterpart at https://github.com/OP-TEE/optee_os/pull/3905 Where we're sorting out the ABI. Thanks for your patience. Cheers, Jens