Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp625931pxa; Tue, 11 Aug 2020 10:57:25 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw/ooP41K3FohKyUWXkRDQ4voSk1bJVY/6+s2z3WljcIaExtI8nuELA6tvSbz6RDeBXTcRo X-Received: by 2002:a17:906:7e05:: with SMTP id e5mr28812932ejr.252.1597168644838; Tue, 11 Aug 2020 10:57:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1597168644; cv=none; d=google.com; s=arc-20160816; b=Gi81kz7CMZahT7bU076mu6fQF1iaaDsNsmxnsGmv4JCr810HmtmYnOtBySA8sk3HZT IA7uvI1+NTSnNHUFpFwlTKdcnURqsK4bp6JOkPj3jLG/6ja+3GdJ4gG1I2pzeFMeZaDU ec3z7aua0m9bxp6nWTHd9NvqXwLa06X6YZcWMn4TY1OMQuQBueb9PHhwLVeGxv2ywMje FMl/iZk57JgnUA1Z4xZO7nOlNXCZEkcCcaCOqy6UphegA+eMOB1yY43thODfWE001euZ /cHMCemO9DMy2631pzbKpUekpuJv8Hfptmn0qXXL0aPJianqs+UkDfHRx3Y6UwsMiR8m bPnQ== 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=tRnNNG+EOiqKnsOW3JVBYP08XhjCezeVkvW5W8pYVGc=; b=XBX8UZ5DaX8J9UFTfr2lSsDXqzmSC/fRfNeCaVjAjldsIpqA75SgiSD9Fk5Yymez1c G2i8qqIqI2TQQ0Hn+VYfSn0SBH34o/8d+ZSrTNdB6ZmZxdYkUrkQb9lD4SUX0E2Dht5z yo+jKUgdoHXMcwD8usHaIHr4ffsW3vuxiYh2qi4uqAQ9nc1pVr5dvrl1h61UckLZRgZk oHWtTskRwdOqpUHc4hznS94gCbtw+O1cjFiA4DDtHXj7SdY8fPaS/MJ7i62vbdjTNDJT GiOCAIDF7sqhXOe56fiJl9HwkVAfRBcgrSLAR0bP+bUMnxzxJjIcuh3Ck/ANLVIeP5P0 SOjw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@foundries-io.20150623.gappssmtp.com header.s=20150623 header.b=O9jcFWmV; 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 f2si12721998edw.385.2020.08.11.10.57.01; Tue, 11 Aug 2020 10:57:24 -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=O9jcFWmV; 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 S1726368AbgHKRzj (ORCPT + 99 others); Tue, 11 Aug 2020 13:55:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54370 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725837AbgHKRzh (ORCPT ); Tue, 11 Aug 2020 13:55:37 -0400 Received: from mail-wr1-x444.google.com (mail-wr1-x444.google.com [IPv6:2a00:1450:4864:20::444]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E3A84C061788 for ; Tue, 11 Aug 2020 10:55:36 -0700 (PDT) Received: by mail-wr1-x444.google.com with SMTP id c15so12287269wrs.11 for ; Tue, 11 Aug 2020 10:55:36 -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=tRnNNG+EOiqKnsOW3JVBYP08XhjCezeVkvW5W8pYVGc=; b=O9jcFWmVEfspmlgOb02iR4u1Q7xQgY1Md3YJprx0aU2Q6Hr8UjAYHga8I5V7E7uF6g js3ZaCDaoTJ6w3csCcN81SroXwFCsh3GGIdA7d9h1jaGMOzoYVnzpCy+JqJpVqCKO65I y2QqZ9zew4ZIrKvRhgIX/ZnOhn8Mslx/bgdG85caTrFnQ1oO2h37CraR71xFZEN70yqX nrRGe4rHj2c9AV+KQdV7XqegZrJV+xuMhzRwP018ytjJ76t+ucjD9I9SbP3X+9bR3Qia EfzB8ViTxMb+QeD1qRF1OhO4AYJTPKSBa5wgrLXhLDUgmSp6kVEfLv8vj6tQWnNZSGyA 1h/Q== 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=tRnNNG+EOiqKnsOW3JVBYP08XhjCezeVkvW5W8pYVGc=; b=sEzXpTbExg5MdmBEq/rwks3DnYuBqq/Of5S0/2QTYFKaRQOa5A/oVRZ0R2TMMwvO1z 4GttFVsxT+/8UC0Vs57KhZrsdxzYHKhH2LtkvuDL7Q1XMmw2z4YbwGH7ndOwYD68Rvhv vENgPGlExKe4dmrD/+AORBq5VYGRFUNpVu4iGm4AnklEBZ234FyKFRLMMfWNowdAJkkE RbLSqvV1e0g57BElD/Qm3tltCYpuPv+FmMMQpC8a0dZLS3Wt+aAbEQ9JjLk7nvLc91qo AWhqO3LE/VxWUd5HNuhD7IjgkxKv0XYl46MUbrPWhvuWojRsSceykLFtEdnHjY5yVpju Vm5A== X-Gm-Message-State: AOAM533kLNAF8e7LNU63kcPJTWbY4EyCfWtrlShUzMia3aKUxTw1ja18 holfjqot0RAkPTlGsOJr7vxbUA== X-Received: by 2002:a5d:6443:: with SMTP id d3mr29905083wrw.322.1597168535403; Tue, 11 Aug 2020 10:55:35 -0700 (PDT) Received: from localhost.localdomain (239.red-83-34-184.dynamicip.rima-tde.net. [83.34.184.239]) by smtp.gmail.com with ESMTPSA id y203sm6958814wmc.29.2020.08.11.10.55.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 11 Aug 2020 10:55:34 -0700 (PDT) From: Jorge Ramirez-Ortiz To: jorge@foundries.io, jens.wiklander@linaro.org Cc: sumit.garg@linaro.org, ricardo@foundries.io, mike@foundries.io, tee-dev@lists.linaro.org, linux-kernel@vger.kernel.org Subject: [PATCHv7] drivers: optee: allow op-tee to access devices on the i2c bus Date: Tue, 11 Aug 2020 19:55:31 +0200 Message-Id: <20200811175531.10771-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 --- v7: add support for ten bit i2c slave addressing 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 | 21 ++++++++ drivers/tee/optee/rpc.c | 95 +++++++++++++++++++++++++++++++++++ 2 files changed, 116 insertions(+) diff --git a/drivers/tee/optee/optee_msg.h b/drivers/tee/optee/optee_msg.h index 795bc19ae17a..7b2d919da2ac 100644 --- a/drivers/tee/optee/optee_msg.h +++ b/drivers/tee/optee/optee_msg.h @@ -419,4 +419,25 @@ 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] param[1].u.value.a i2c control flags + * + * [in/out] memref[2] buffer to exchange the transfer data + * with the secure world + * + * [out] param[3].u.value.a bytes transferred by the driver + */ +#define OPTEE_MSG_RPC_CMD_I2C_TRANSFER 21 +/* I2C master transfer modes */ +#define OPTEE_MSG_RPC_CMD_I2C_TRANSFER_RD 0 +#define OPTEE_MSG_RPC_CMD_I2C_TRANSFER_WR 1 +/* I2C master control flags */ +#define OPTEE_MSG_RPC_CMD_I2C_FLAGS_TEN_BIT BIT(0) + #endif /* _OPTEE_MSG_H */ diff --git a/drivers/tee/optee/rpc.c b/drivers/tee/optee/rpc.c index b4ade54d1f28..b6178761d79f 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,97 @@ 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 = { 0 }; + struct tee_param *params; + int i, ret = -EOPNOTSUPP; + uint32_t attr[] = { + TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT, + 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++) { + if ((params[i].attr & TEE_IOCTL_PARAM_ATTR_TYPE_MASK) + != attr[i]) + goto bad; + } + + client.adapter = i2c_get_adapter(params[0].u.value.b); + if (!client.adapter) + goto bad; + + if (params[1].u.value.a & OPTEE_MSG_RPC_CMD_I2C_FLAGS_TEN_BIT) { + if (!i2c_check_functionality(client.adapter, + I2C_FUNC_10BIT_ADDR)) { + i2c_put_adapter(client.adapter); + goto bad; + } + + client.flags = I2C_CLIENT_TEN; + } + + client.addr = params[0].u.value.c; + snprintf(client.name, I2C_NAME_SIZE, "i2c%d", client.adapter->nr); + + switch (params[0].u.value.a) { + case OPTEE_MSG_RPC_CMD_I2C_TRANSFER_RD: + ret = i2c_master_recv(&client, params[2].u.memref.shm->kaddr, + params[2].u.memref.size); + break; + case OPTEE_MSG_RPC_CMD_I2C_TRANSFER_WR: + ret = i2c_master_send(&client, params[2].u.memref.shm->kaddr, + params[2].u.memref.size); + break; + default: + i2c_put_adapter(client.adapter); + goto bad; + } + + if (ret < 0) { + arg->ret = TEEC_ERROR_COMMUNICATION; + } else { + params[3].u.value.a = ret; + arg->ret = TEEC_SUCCESS; + + 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 +474,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