Received: by 2002:a25:e74b:0:0:0:0:0 with SMTP id e72csp755628ybh; Wed, 22 Jul 2020 12:20:31 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwe/Sk9Z1zzifIfngeCWB4725wnie5s1UlE4FZvaVwUKeQJLSe5fXFmIsw1ND1VFBk0cE8L X-Received: by 2002:a17:906:1704:: with SMTP id c4mr1022913eje.525.1595445630997; Wed, 22 Jul 2020 12:20:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1595445630; cv=none; d=google.com; s=arc-20160816; b=OrjRcMaQ9aebeCNG2wxw33mhCUSJr3aStCgRautmnHZCKBIzPaj6z1tIpwuB0RpSur +igT0T/lkFYoFjn3nvew2oEUGmHHc0d334EY+H00ngGTGWjo/8jKazz+DonYqbHHu7gP B/Rf7V2FwSUYFJwcU9EIjVNaXHFWdea3lWVEmThKUYCgWzS0TJFCQgOR6XoyCmlWjcxp hrTjUqr2vQgpeg6yIbvFXuPYcKky3eGAyWwFlZW6zO7VFRa14SYLkd/M5KNmdD3w3yQ1 hGmKgQYW07rOQOk3l2v3EpGP4MTuJGP/IknQzbUzmv88Rk6C707b66jIU1K4L3YujH4q cfjQ== 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=2EIBszTzyv/AfyhPfuCemZqatYVBUAPBlZr4NfJGxfU=; b=BLGZpSYqvHIVDavktxm9082YV750Io5CmY8y8pN7SfE7EWgbbB0rgwaZouR5QBxQy/ jD62gf1gm+qchFo7WA0LYfnJ0snaV6ABcCUC3+HPGPy8nlGN3qZSVyCv+hq7Wof1nosC DJ6+A3LAL7qs3hOtLwpihP3CtnoAtGum583zJbmXYC1h/gDmpdp3msd/nbCiFATkmf5m N1eLzTxwUZHjK7CQTciDyjeLaWShxwSqtHyTn05i+9yrgIlDaX/GoePszOEVj2OuwaDP mmb6cfGmqPNpt/cpFxsuuT7otmTTkC3mlJWvrqMxVmoppMNRDCz85IYtRugUm0428ivh Vtig== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@foundries-io.20150623.gappssmtp.com header.s=20150623 header.b=V9iKhNrR; 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 c12si566616edy.152.2020.07.22.12.20.08; Wed, 22 Jul 2020 12:20:30 -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=V9iKhNrR; 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 S1732701AbgGVTTy (ORCPT + 99 others); Wed, 22 Jul 2020 15:19:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40648 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732647AbgGVTTv (ORCPT ); Wed, 22 Jul 2020 15:19:51 -0400 Received: from mail-wr1-x441.google.com (mail-wr1-x441.google.com [IPv6:2a00:1450:4864:20::441]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C5B2EC0619DC for ; Wed, 22 Jul 2020 12:19:50 -0700 (PDT) Received: by mail-wr1-x441.google.com with SMTP id f7so2980964wrw.1 for ; Wed, 22 Jul 2020 12:19:50 -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=2EIBszTzyv/AfyhPfuCemZqatYVBUAPBlZr4NfJGxfU=; b=V9iKhNrR2kb/bKZjFyD/LsNBZM007bp0nAQTEnzJfU1Qd6DHFLG0zbKlc/0G32RuOd l7OueXPFFx81C+FPgvaXcuSq6OeGTU5R5xlmwfj9SRwtkU53JpCjV4pPZBwsUiYYveYH yl/kaUL3zPSyghUMu7H02RV5Mw+lD9UtgUI8Q+VJ9UQJjLDk46Ma19iVCIfWxLVyGdmJ ZFv0mPbfpsnBeICG9JgCUsjWwmfCxmcLkyKC/fJ4U0h+i47hWFW3VBGZpymUJRkPalvu u3mhRQNSq7nbitzVqEv0bEbw9zLvkQeLm4Wr9OexhBIGXYS68+j5crDR4h1KaVORkpzk h7/w== 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=2EIBszTzyv/AfyhPfuCemZqatYVBUAPBlZr4NfJGxfU=; b=kGm9kZqU0duww7ONHjPHJZ8RKs2BwTz1Pd/O7IcwLE0tprX5XUmMRYQYWL4cH+nLCg 5SxGBwuDPSznfWf4BiAI15oEGxmjAr3pQ10cqXMKEyPafEsTirwReiTYYOcP+boeTkyN VhcQF3sYvahJo8OeRKq33n69O4suN4emsVabVIsPmpUneZsnqEOus02ZAj8FlqaGFQca wOVBGYUuscel0xUHVkinPBgIlaHPUoHRtG2JrXvlyZv1LEMZqaS7DQJ08nd14c9iYbbu xKtlw9rH+qYHpOmDR7zG/yyySJQLxIUaZEFJ6U+lfphsNKt1ykG7K0DzakFI94dhHQf8 w+lA== X-Gm-Message-State: AOAM531LVsNvq1SIQD1cRpbcAZcIm1tEIXYsmqefJyMT49BQ3lPo8lPn 0jyBFT7wxO6ErsONDtLr2OSo4g== X-Received: by 2002:adf:db86:: with SMTP id u6mr908211wri.27.1595445589526; Wed, 22 Jul 2020 12:19:49 -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 n189sm671825wmf.38.2020.07.22.12.19.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 Jul 2020 12:19:49 -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: [PATCHv5] drivers: optee: allow op-tee to access devices on the i2c bus Date: Wed, 22 Jul 2020 21:19:45 +0200 Message-Id: <20200722191945.15157-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 --- v5: arrange includes alphabetically v4: removed unecessary 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 id drivers/tee/optee/optee_msg.h | 16 +++++++ drivers/tee/optee/rpc.c | 80 +++++++++++++++++++++++++++++++++++ 2 files changed, 96 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..df6250418235 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,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