Received: by 2002:a25:e74b:0:0:0:0:0 with SMTP id e72csp823280ybh; Wed, 22 Jul 2020 14:22:42 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx9UX4rlaxJZqWNsjUk72MUBP9Ab6eQCqKp7cCeo/XqN+dkF+WFTlb2YGwV77GsMHBc1WdE X-Received: by 2002:a17:906:e0c7:: with SMTP id gl7mr1393465ejb.264.1595452962354; Wed, 22 Jul 2020 14:22:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1595452962; cv=none; d=google.com; s=arc-20160816; b=W/29Es4b7P8cSCgZvNST3+kLUq5nK+Tw7wqdzhHQs6XrLxupCqmTpF5XqtIDyPr0VR FdzUmH2jAhPDOgyR+MnB/G+XlDVFTjuTL1tHH1f5WSoguU4R1bmu0TYXua3JJfX8tkVN LPagZGj/Jc1L//YQQd8ijRnsQ0vy9U3PIqgQoXVlcMcvTDWQI7MOnHp1j307gteUxuXg rSRmY3WLrbjEQ4uSRkAYXLZ6gXA78YwiTswHzy+qsrv2bQ4pmI+7d2x0yTnL22DYvhzc /Rqo1YYzlUBJIVqs/ObEWuU35Xfyh8ln6zJiOB8txatxL0qNZ5LnLpffnLbWr/fbuUpO /YYg== 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=RQ5EqUygspbnIShpkO6on2zzEAPxkgSj/dTotJ3WQQY=; b=i+dHHB5Yj8ZmnOvDoUZPNM2bd4X4Hn8rRAB2OWaTk+zZgKbjb28oWSHqjAmHmkPtJS sRZEWoAwPfC46crUEHUPDM/wK47hKSKjFBRNYGowEtIKPCUKKrZvz/34V05Lu/Yhet4g 5w5xhBAk11EIGQlzLgjdfoBrWBdUJTcblPB9Z9InRhCHkGsjpGdMuAO63C0372aR5ZZH i0lp6HNSvEPLCeIy+vrVIvnwKmySgSBbQqnsVEUoXxoQKCDXLrYmQtpKkjz8EvThc8+x l4FrzxVU4y2zNDniDwkmlT/nAw68TTFgOPtShy+JaegsiesdE18LwWdTtnA66ZZfnY7K RJzQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@foundries-io.20150623.gappssmtp.com header.s=20150623 header.b=1FfGlNl2; 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 js4si732317ejb.120.2020.07.22.14.22.18; Wed, 22 Jul 2020 14:22:42 -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=1FfGlNl2; 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 S1732896AbgGVVVM (ORCPT + 99 others); Wed, 22 Jul 2020 17:21:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59570 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726447AbgGVVVM (ORCPT ); Wed, 22 Jul 2020 17:21:12 -0400 Received: from mail-wm1-x343.google.com (mail-wm1-x343.google.com [IPv6:2a00:1450:4864:20::343]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C1075C0619DC for ; Wed, 22 Jul 2020 14:21:11 -0700 (PDT) Received: by mail-wm1-x343.google.com with SMTP id o2so3323099wmh.2 for ; Wed, 22 Jul 2020 14:21:11 -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=RQ5EqUygspbnIShpkO6on2zzEAPxkgSj/dTotJ3WQQY=; b=1FfGlNl2FJ+MFxdOWkLcBC4eZNznbhFaRr/lU2r0GOYLIDhO8IxJfRVf/LYZWwPzIU Uvo0x8mH85vOFmu6dNazveYW/iSvLcAXW5ZTf+Hld57Arf/mgjB6UFhrbZ9lzzrdshNV GarqDA1XscxSyH3zp+3pMnkqsAYj8IeeJSYJyXVwBM8DOZR7QRl5VOCgB5UiP3IHCc4M rXb7+JY+yq/yKgLyP6SMmFtFCFsxDJngb9M7wtnskk44ncyohbEPDwmtxP/T7oSIBpqZ dtlun+YjvUkuAjXy39NKHcKsltozO8g96nYbVhnYrfX0moRgOS+wdM3q1euH8EI1HL08 JYJQ== 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=RQ5EqUygspbnIShpkO6on2zzEAPxkgSj/dTotJ3WQQY=; b=bSKkg3+ChBOZYBq5xnqw9QRrzsBK6iwYBLBRn/0QgDntGuMdsgIx4+XHx2NCnBSm7A ttbUEqoZhjlnlkWwCzXO8r6fgDE0cGtMVXBipuWIpOyk82stzIQ5naq9GlXyaVAmLYAu JplhNvobLiGnynhB4WhqW6AsmbleFSMUj4MCD36QtuoL1kP44aOalir47Hn8sYVEMPyy Nhcvvog9TgRsJxlulGM2aEOkvCmGt/cs6Wd06fSe6/nbpbpLoYEZ2sXmlDWGs4Wsaqnr lRTg1aR84/ixVtRxxTkhKvRZDDx+KRKMGYmwY7w6ckDMIKUjU71DHsFvrQMjgmucP0+f P8Bg== X-Gm-Message-State: AOAM533hYGYGKtKZakd9yIFI9apobmFcxmxfVfxCi90opQ5L9O98GD9b pvqcm4J2cWVQzAJmV1atXlahThQ3wTs= X-Received: by 2002:a7b:c205:: with SMTP id x5mr1350566wmi.161.1595452870500; Wed, 22 Jul 2020 14:21:10 -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 a11sm1346507wrq.0.2020.07.22.14.21.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 Jul 2020 14:21:10 -0700 (PDT) From: Jorge Ramirez-Ortiz To: jorge@foundries.io, jens.wiklander@linaro.org, sumit.garg@linaro.org Cc: ricardo@foundries.io, mike@foundries.io, tee-dev@lists.linaro.org, linux-kernel@vger.kernel.org Subject: [PATCHv6] drivers: optee: allow op-tee to access devices on the i2c bus Date: Wed, 22 Jul 2020 23:21:05 +0200 Message-Id: <20200722212105.1798-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. 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); } -- 2.17.1