linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCHv9] drivers: optee: allow op-tee to access devices on the i2c bus
@ 2020-08-14 11:12 Jorge Ramirez-Ortiz
  2020-08-21  6:21 ` Jens Wiklander
  0 siblings, 1 reply; 4+ messages in thread
From: Jorge Ramirez-Ortiz @ 2020-08-14 11:12 UTC (permalink / raw)
  To: jorge, jens.wiklander; +Cc: sumit.garg, ricardo, mike, tee-dev, linux-kernel

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 <jorge@foundries.io>
---
v9: params return value must be written before optee_msg_to_param is called
v8: review fixes:
    fix types and add TEEC_ERROR_NOT_SUPPORTED to GP errors
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/optee_private.h |  1 +
 drivers/tee/optee/rpc.c           | 95 +++++++++++++++++++++++++++++++
 3 files changed, 117 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/optee_private.h b/drivers/tee/optee/optee_private.h
index 8b71839a357e..564116d6cc58 100644
--- a/drivers/tee/optee/optee_private.h
+++ b/drivers/tee/optee/optee_private.h
@@ -17,6 +17,7 @@
 /* Some Global Platform error codes used in this driver */
 #define TEEC_SUCCESS			0x00000000
 #define TEEC_ERROR_BAD_PARAMETERS	0xFFFF0006
+#define TEEC_ERROR_NOT_SUPPORTED        0xFFFF000A
 #define TEEC_ERROR_COMMUNICATION	0xFFFF000E
 #define TEEC_ERROR_OUT_OF_MEMORY	0xFFFF000C
 #define TEEC_ERROR_SHORT_BUFFER		0xFFFF0010
diff --git a/drivers/tee/optee/rpc.c b/drivers/tee/optee/rpc.c
index b4ade54d1f28..64a206c56264 100644
--- a/drivers/tee/optee/rpc.c
+++ b/drivers/tee/optee/rpc.c
@@ -7,6 +7,7 @@
 
 #include <linux/delay.h>
 #include <linux/device.h>
+#include <linux/i2c.h>
 #include <linux/slab.h>
 #include <linux/tee_drv.h>
 #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;
+	size_t i;
+	int ret = -EOPNOTSUPP;
+	u8 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 != 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;
+		if (optee_to_msg_param(arg->params, arg->num_params, params))
+			arg->ret = TEEC_ERROR_BAD_PARAMETERS;
+		else
+			arg->ret = TEEC_SUCCESS;
+	}
+
+	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_NOT_SUPPORTED;
+}
+#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


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCHv9] drivers: optee: allow op-tee to access devices on the i2c bus
  2020-08-14 11:12 [PATCHv9] drivers: optee: allow op-tee to access devices on the i2c bus Jorge Ramirez-Ortiz
@ 2020-08-21  6:21 ` Jens Wiklander
  2020-08-21  6:36   ` Jorge Ramirez-Ortiz, Foundries
  0 siblings, 1 reply; 4+ messages in thread
From: Jens Wiklander @ 2020-08-21  6:21 UTC (permalink / raw)
  To: Jorge Ramirez-Ortiz; +Cc: sumit.garg, ricardo, mike, tee-dev, linux-kernel

On Fri, Aug 14, 2020 at 01:12:21PM +0200, 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.
> 
> Signed-off-by: Jorge Ramirez-Ortiz <jorge@foundries.io>
> ---
> v9: params return value must be written before optee_msg_to_param is called
> v8: review fixes:
>     fix types and add TEEC_ERROR_NOT_SUPPORTED to GP errors
> 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/optee_private.h |  1 +
>  drivers/tee/optee/rpc.c           | 95 +++++++++++++++++++++++++++++++
>  3 files changed, 117 insertions(+)

Looks good. Did you test this with the recently merged
https://github.com/OP-TEE/optee_os/pull/4033 ?

Cheers,
Jens

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCHv9] drivers: optee: allow op-tee to access devices on the i2c bus
  2020-08-21  6:21 ` Jens Wiklander
@ 2020-08-21  6:36   ` Jorge Ramirez-Ortiz, Foundries
  2020-08-21 10:04     ` Jens Wiklander
  0 siblings, 1 reply; 4+ messages in thread
From: Jorge Ramirez-Ortiz, Foundries @ 2020-08-21  6:36 UTC (permalink / raw)
  To: Jens Wiklander
  Cc: Jorge Ramirez-Ortiz, sumit.garg, ricardo, mike, tee-dev, linux-kernel

On 21/08/20, Jens Wiklander wrote:
> On Fri, Aug 14, 2020 at 01:12:21PM +0200, 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.
> > 
> > Signed-off-by: Jorge Ramirez-Ortiz <jorge@foundries.io>
> > ---
> > v9: params return value must be written before optee_msg_to_param is called
> > v8: review fixes:
> >     fix types and add TEEC_ERROR_NOT_SUPPORTED to GP errors
> > 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/optee_private.h |  1 +
> >  drivers/tee/optee/rpc.c           | 95 +++++++++++++++++++++++++++++++
> >  3 files changed, 117 insertions(+)
> 
> Looks good. Did you test this with the recently merged
> https://github.com/OP-TEE/optee_os/pull/4033 ?
>

yes, I did retest and it is good.

thanks!

> Cheers,
> Jens

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCHv9] drivers: optee: allow op-tee to access devices on the i2c bus
  2020-08-21  6:36   ` Jorge Ramirez-Ortiz, Foundries
@ 2020-08-21 10:04     ` Jens Wiklander
  0 siblings, 0 replies; 4+ messages in thread
From: Jens Wiklander @ 2020-08-21 10:04 UTC (permalink / raw)
  To: Jorge Ramirez-Ortiz, Foundries
  Cc: Sumit Garg, ricardo, Michael Scott,
	tee-dev @ lists . linaro . org, Linux Kernel Mailing List

On Fri, Aug 21, 2020 at 8:37 AM Jorge Ramirez-Ortiz, Foundries
<jorge@foundries.io> wrote:
>
> On 21/08/20, Jens Wiklander wrote:
> > On Fri, Aug 14, 2020 at 01:12:21PM +0200, 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.
> > >
> > > Signed-off-by: Jorge Ramirez-Ortiz <jorge@foundries.io>
> > > ---
> > > v9: params return value must be written before optee_msg_to_param is called
> > > v8: review fixes:
> > >     fix types and add TEEC_ERROR_NOT_SUPPORTED to GP errors
> > > 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/optee_private.h |  1 +
> > >  drivers/tee/optee/rpc.c           | 95 +++++++++++++++++++++++++++++++
> > >  3 files changed, 117 insertions(+)
> >
> > Looks good. Did you test this with the recently merged
> > https://github.com/OP-TEE/optee_os/pull/4033 ?
> >
>
> yes, I did retest and it is good.

Good. I'm picking this up.

Thanks,
Jens

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2020-08-21 10:04 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-08-14 11:12 [PATCHv9] drivers: optee: allow op-tee to access devices on the i2c bus Jorge Ramirez-Ortiz
2020-08-21  6:21 ` Jens Wiklander
2020-08-21  6:36   ` Jorge Ramirez-Ortiz, Foundries
2020-08-21 10:04     ` Jens Wiklander

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).