Skip to contentSkip to conversation

TEE driver for Trusted Services

PATCH Superseded Balint Dobszay 2 participants 11 messages Mar 5, 2024 – Mar 12, 2024

Subsystems (4)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Balint Dobszay

author maintainer
#0 lore raw

v3 cover TEE driver for Trusted Services

This series introduces a TEE driver for Trusted Services [1].

Trusted Services is a TrustedFirmware.org project that provides a framework for developing and deploying device Root of Trust services in FF-A [2] Secure Partitions. The project hosts the reference implementation of Arm Platform Security Architecture [3] for Arm A-profile devices.

The FF-A Secure Partitions are accessible through the FF-A driver in Linux. However, the FF-A driver doesn't have a user space interface so user space clients currently cannot access Trusted Services. The goal of this TEE driver is to bridge this gap and make Trusted Services functionality accessible from user space.

Changelog:
v2[6] -> v3:
- Add patch "tee: Refactor TEE subsystem header files" from Sumit
- Remove unnecessary includes from core.c
- Remove the mutex from "struct ts_context_data" since the same mechanism could be implemented by reusing the XArray's internal lock
- Rename tee_shm_pool_op_*_helper functions as suggested by Sumit
- Replace pr_* with dev_* as previously suggested by Krzysztof

v1[5] -> v2:
- Refactor session handling to use XArray instead of IDR and linked list (the linked list was redundant as pointed out by Jens, and IDR is now deprecated in favor of XArray)
- Refactor tstee_probe() to not call tee_device_unregister() before calling tee_device_register()
- Address comments from Krzysztof and Jens
- Address documentation comments from Randy
- Use module_ffa_driver() macro instead of separate module init / exit functions
- Reformat max line length 100 -> 80

RFC[4] -> v1:
- Add patch for moving pool_op helper functions to the TEE subsystem, as suggested by Jens
- Address comments from Sumit, add patch for documentation

[1] https://www.trustedfirmware.org/projects/trusted-services/
[2] https://developer.arm.com/documentation/den0077/
[3] https://www.arm.com/architecture/security-features/platform-security
[4] https://lore.kernel.org/linux-arm-kernel/20230927152145.111777-1-balint.dobszay(a)arm.com/
[5] https://lore.kernel.org/lkml/20240213145239.379875-1-balint.dobszay(a)arm.com/
[6] https://lore.kernel.org/lkml/20240223095133.109046-1-balint.dobszay(a)arm.com/

Balint Dobszay (3):
tee: optee: Move pool_op helper functions
tee: tstee: Add Trusted Services TEE driver
Documentation: tee: Add TS-TEE driver

Sumit Garg (1):
tee: Refactor TEE subsystem header files

 Documentation/tee/index.rst         |   1 +
 Documentation/tee/ts-tee.rst        |  71 ++++
 MAINTAINERS                         |   1 +
 drivers/tee/Kconfig                 |   1 +
 drivers/tee/Makefile                |   1 +
 drivers/tee/amdtee/amdtee_private.h |   2 +-
 drivers/tee/amdtee/call.c           |   2 +-
 drivers/tee/amdtee/core.c           |   3 +-
 drivers/tee/amdtee/shm_pool.c       |   2 +-
 drivers/tee/optee/call.c            |   2 +-
 drivers/tee/optee/core.c            |  66 +---
 drivers/tee/optee/device.c          |   2 +-
 drivers/tee/optee/ffa_abi.c         |   8 +-
 drivers/tee/optee/notif.c           |   2 +-
 drivers/tee/optee/optee_private.h   |  14 +-
 drivers/tee/optee/rpc.c             |   2 +-
 drivers/tee/optee/smc_abi.c         |  11 +-
 drivers/tee/tee_core.c              |   2 +-
 drivers/tee/tee_private.h           |  35 --
 drivers/tee/tee_shm.c               |  66 +++-
 drivers/tee/tee_shm_pool.c          |   2 +-
 drivers/tee/tstee/Kconfig           |  11 +
 drivers/tee/tstee/Makefile          |   3 +
 drivers/tee/tstee/core.c            | 482 ++++++++++++++++++++++++++++
 drivers/tee/tstee/tstee_private.h   |  92 ++++++
 include/linux/tee_core.h            | 306 ++++++++++++++++++
 include/linux/tee_drv.h             | 285 ++--------------
 include/uapi/linux/tee.h            |   1 +
 28 files changed, 1087 insertions(+), 389 deletions(-)
 create mode 100644 Documentation/tee/ts-tee.rst
 create mode 100644 drivers/tee/tstee/Kconfig
 create mode 100644 drivers/tee/tstee/Makefile
 create mode 100644 drivers/tee/tstee/core.c
 create mode 100644 drivers/tee/tstee/tstee_private.h
 create mode 100644 include/linux/tee_core.h
Signature · 1 line
2.34.1
author maintainer
#1 lore raw

PATCH v3 1/4 tee: Refactor TEE subsystem header files

From: Sumit Garg <[email protected]>

From: Sumit Garg <[email protected]>

Since commit 25559c22cef8 ("tee: add kernel internal client interface"), it has been a common include/linux/tee_drv.h header file which is shared to hold TEE subsystem internal bits along with the APIs exposed to the TEE client drivers. However, this practice is prone to TEE subsystem internal APIs abuse and especially so with the new TEE implementation drivers being added to reuse existing functionality.

In order to address this split TEE subsystem internal bits as a separate header file: include/linux/tee_core.h which should be the one used by TEE implementation drivers. With that include/linux/tee_drv.h lists only APIs exposed by TEE subsystem to the TEE client drivers.

---
 MAINTAINERS                         |   1 +
 drivers/tee/amdtee/amdtee_private.h |   2 +-
 drivers/tee/amdtee/call.c           |   2 +-
 drivers/tee/amdtee/core.c           |   3 +-
 drivers/tee/amdtee/shm_pool.c       |   2 +-
 drivers/tee/optee/call.c            |   2 +-
 drivers/tee/optee/core.c            |   2 +-
 drivers/tee/optee/device.c          |   2 +-
 drivers/tee/optee/ffa_abi.c         |   2 +-
 drivers/tee/optee/notif.c           |   2 +-
 drivers/tee/optee/optee_private.h   |   2 +-
 drivers/tee/optee/rpc.c             |   2 +-
 drivers/tee/optee/smc_abi.c         |   2 +-
 drivers/tee/tee_core.c              |   2 +-
 drivers/tee/tee_private.h           |  35 ----
 drivers/tee/tee_shm.c               |   2 +-
 drivers/tee/tee_shm_pool.c          |   2 +-
 include/linux/tee_core.h            | 296 ++++++++++++++++++++++++++++
 include/linux/tee_drv.h             | 285 +++-----------------------
 19 files changed, 343 insertions(+), 305 deletions(-)
 create mode 100644 include/linux/tee_core.h
MAINTAINERS +1 -0
index 4f298c4187fb..43f08ed271ad 100644
@@ -21557,6 +21557,7 @@ F:	Documentation/driver-api/tee.rst
 F:	Documentation/tee/
 F:	Documentation/userspace-api/tee.rst
 F:	drivers/tee/
+F:	include/linux/tee_core.h
 F:	include/linux/tee_drv.h
 F:	include/uapi/linux/tee.h
 
drivers/tee/amdtee/amdtee_private.h +1 -1
index 6d0f7062bb87..d87050033894 100644
@@ -9,7 +9,7 @@
 
 #include <linux/mutex.h>
 #include <linux/spinlock.h>
-#include <linux/tee_drv.h>
+#include <linux/tee_core.h>
 #include <linux/kref.h>
 #include <linux/types.h>
 #include "amdtee_if.h"
drivers/tee/amdtee/call.c +1 -1
index e9b63dcb3194..4c21b02be4af 100644
@@ -5,7 +5,7 @@
 
 #include <linux/device.h>
 #include <linux/tee.h>
-#include <linux/tee_drv.h>
+#include <linux/tee_core.h>
 #include <linux/psp-tee.h>
 #include <linux/slab.h>
 #include <linux/psp.h>
drivers/tee/amdtee/core.c +1 -2
index 3c15f6a9e91c..e487231d25dc 100644
@@ -9,13 +9,12 @@
 #include <linux/slab.h>
 #include <linux/string.h>
 #include <linux/device.h>
-#include <linux/tee_drv.h>
+#include <linux/tee_core.h>
 #include <linux/types.h>
 #include <linux/mm.h>
 #include <linux/uaccess.h>
 #include <linux/firmware.h>
 #include "amdtee_private.h"
-#include "../tee_private.h"
 #include <linux/psp-tee.h>
 
 static struct amdtee_driver_data *drv_data;
drivers/tee/amdtee/shm_pool.c +1 -1
index f0303126f199..6346e0bc8a64 100644
@@ -4,7 +4,7 @@
  */
 
 #include <linux/slab.h>
-#include <linux/tee_drv.h>
+#include <linux/tee_core.h>
 #include <linux/psp.h>
 #include "amdtee_private.h"
 
drivers/tee/optee/call.c +1 -1
index a91e50be11be..16eb953e14bb 100644
@@ -7,7 +7,7 @@
 #include <linux/errno.h>
 #include <linux/mm.h>
 #include <linux/slab.h>
-#include <linux/tee_drv.h>
+#include <linux/tee_core.h>
 #include <linux/types.h>
 #include "optee_private.h"
 
drivers/tee/optee/core.c +1 -1
index 3aed554bc8d8..f762e3a25119 100644
@@ -13,7 +13,7 @@
 #include <linux/module.h>
 #include <linux/slab.h>
 #include <linux/string.h>
-#include <linux/tee_drv.h>
+#include <linux/tee_core.h>
 #include <linux/types.h>
 #include "optee_private.h"
 
drivers/tee/optee/device.c +1 -1
index 4b1092127694..9d2afac96acc 100644
@@ -7,7 +7,7 @@
 
 #include <linux/kernel.h>
 #include <linux/slab.h>
-#include <linux/tee_drv.h>
+#include <linux/tee_core.h>
 #include <linux/uuid.h>
 #include "optee_private.h"
 
drivers/tee/optee/ffa_abi.c +1 -1
index ecb5eb079408..cee8ccb84cb8 100644
@@ -11,7 +11,7 @@
 #include <linux/sched.h>
 #include <linux/slab.h>
 #include <linux/string.h>
-#include <linux/tee_drv.h>
+#include <linux/tee_core.h>
 #include <linux/types.h>
 #include "optee_private.h"
 #include "optee_ffa.h"
drivers/tee/optee/notif.c +1 -1
index 05212842b0a5..0d7878e770cd 100644
@@ -9,7 +9,7 @@
 #include <linux/errno.h>
 #include <linux/slab.h>
 #include <linux/spinlock.h>
-#include <linux/tee_drv.h>
+#include <linux/tee_core.h>
 #include "optee_private.h"
 
 struct notif_entry {
drivers/tee/optee/optee_private.h +1 -1
index 7a5243c78b55..a0698ac18993 100644
@@ -9,7 +9,7 @@
 #include <linux/arm-smccc.h>
 #include <linux/rhashtable.h>
 #include <linux/semaphore.h>
-#include <linux/tee_drv.h>
+#include <linux/tee_core.h>
 #include <linux/types.h>
 #include "optee_msg.h"
 
drivers/tee/optee/rpc.c +1 -1
index e69bc6380683..f086812f1179 100644
@@ -8,7 +8,7 @@
 #include <linux/delay.h>
 #include <linux/i2c.h>
 #include <linux/slab.h>
-#include <linux/tee_drv.h>
+#include <linux/tee_core.h>
 #include "optee_private.h"
 #include "optee_rpc_cmd.h"
 
drivers/tee/optee/smc_abi.c +1 -1
index a37f87087e5c..9c296b887dc1 100644
@@ -23,7 +23,7 @@
 #include <linux/sched.h>
 #include <linux/slab.h>
 #include <linux/string.h>
-#include <linux/tee_drv.h>
+#include <linux/tee_core.h>
 #include <linux/types.h>
 #include <linux/workqueue.h>
 #include "optee_private.h"
drivers/tee/tee_core.c +1 -1
index 792d6fae4354..dd6e4ad4dceb 100644
@@ -11,7 +11,7 @@
 #include <linux/idr.h>
 #include <linux/module.h>
 #include <linux/slab.h>
-#include <linux/tee_drv.h>
+#include <linux/tee_core.h>
 #include <linux/uaccess.h>
 #include <crypto/hash.h>
 #include <crypto/sha1.h>
drivers/tee/tee_private.h +0 -35
index 754e11dcb240..9bc50605227c 100644
@@ -12,41 +12,6 @@
 #include <linux/mutex.h>
 #include <linux/types.h>
 
-#define TEE_DEVICE_FLAG_REGISTERED	0x1
-#define TEE_MAX_DEV_NAME_LEN		32
-
-/**
- * struct tee_device - TEE Device representation
- * @name:	name of device
- * @desc:	description of device
- * @id:		unique id of device
- * @flags:	represented by TEE_DEVICE_FLAG_REGISTERED above
- * @dev:	embedded basic device structure
- * @cdev:	embedded cdev
- * @num_users:	number of active users of this device
- * @c_no_user:	completion used when unregistering the device
- * @mutex:	mutex protecting @num_users and @idr
- * @idr:	register of user space shared memory objects allocated or
- *		registered on this device
- * @pool:	shared memory pool
- */
-struct tee_device {
-	char name[TEE_MAX_DEV_NAME_LEN];
-	const struct tee_desc *desc;
-	int id;
-	unsigned int flags;
-
-	struct device dev;
-	struct cdev cdev;
-
-	size_t num_users;
-	struct completion c_no_users;
-	struct mutex mutex;	/* protects num_users and idr */
-
-	struct idr idr;
-	struct tee_shm_pool *pool;
-};
-
 int tee_shm_get_fd(struct tee_shm *shm);
 
 bool tee_device_get(struct tee_device *teedev);
drivers/tee/tee_shm.c +1 -1
index 731d9028b67f..96a45c817427 100644
@@ -8,7 +8,7 @@
 #include <linux/mm.h>
 #include <linux/sched.h>
 #include <linux/slab.h>
-#include <linux/tee_drv.h>
+#include <linux/tee_core.h>
 #include <linux/uaccess.h>
 #include <linux/uio.h>
 #include <linux/highmem.h>
drivers/tee/tee_shm_pool.c +1 -1
index 058bfbac657a..80004b55628d 100644
@@ -6,7 +6,7 @@
 #include <linux/dma-buf.h>
 #include <linux/genalloc.h>
 #include <linux/slab.h>
-#include <linux/tee_drv.h>
+#include <linux/tee_core.h>
 #include "tee_private.h"
 
 static int pool_op_gen_alloc(struct tee_shm_pool *pool, struct tee_shm *shm,
include/linux/tee_core.h new file +296 -0

296 lines not on this page · raw message

include/linux/tee_drv.h +31 -254

368 lines not on this page · raw message

--
2.34.1

author maintainer
#2 lore raw

PATCH v3 2/4 tee: optee: Move pool_op helper functions

In reply to Balint Dobszay

Move the pool alloc and free helper functions from the OP-TEE driver to the TEE subsystem, since these could be reused in other TEE drivers. This patch is not supposed to change behavior, it's only reorganizing the code.

---
 drivers/tee/optee/core.c          | 64 -------------------------------
 drivers/tee/optee/ffa_abi.c       |  6 +--
 drivers/tee/optee/optee_private.h | 12 ------
 drivers/tee/optee/smc_abi.c       |  9 ++---
 drivers/tee/tee_shm.c             | 64 +++++++++++++++++++++++++++++++
 include/linux/tee_core.h          | 10 +++++
 6 files changed, 81 insertions(+), 84 deletions(-)
drivers/tee/optee/core.c +0 -64
index f762e3a25119..39e688d4e974 100644
@@ -9,7 +9,6 @@
 #include <linux/crash_dump.h>
 #include <linux/errno.h>
 #include <linux/io.h>
-#include <linux/mm.h>
 #include <linux/module.h>
 #include <linux/slab.h>
 #include <linux/string.h>
@@ -17,69 +16,6 @@
 #include <linux/types.h>
 #include "optee_private.h"
 
-int optee_pool_op_alloc_helper(struct tee_shm_pool *pool, struct tee_shm *shm,
-			       size_t size, size_t align,
-			       int (*shm_register)(struct tee_context *ctx,
-						   struct tee_shm *shm,
-						   struct page **pages,
-						   size_t num_pages,
-						   unsigned long start))
-{
-	size_t nr_pages = roundup(size, PAGE_SIZE) / PAGE_SIZE;
-	struct page **pages;
-	unsigned int i;
-	int rc = 0;
-
-	/*
-	 * Ignore alignment since this is already going to be page aligned
-	 * and there's no need for any larger alignment.
-	 */
-	shm->kaddr = alloc_pages_exact(nr_pages * PAGE_SIZE,
-				       GFP_KERNEL | __GFP_ZERO);
-	if (!shm->kaddr)
-		return -ENOMEM;
-
-	shm->paddr = virt_to_phys(shm->kaddr);
-	shm->size = nr_pages * PAGE_SIZE;
-
-	pages = kcalloc(nr_pages, sizeof(*pages), GFP_KERNEL);
-	if (!pages) {
-		rc = -ENOMEM;
-		goto err;
-	}
-
-	for (i = 0; i < nr_pages; i++)
-		pages[i] = virt_to_page((u8 *)shm->kaddr + i * PAGE_SIZE);
-
-	shm->pages = pages;
-	shm->num_pages = nr_pages;
-
-	if (shm_register) {
-		rc = shm_register(shm->ctx, shm, pages, nr_pages,
-				  (unsigned long)shm->kaddr);
-		if (rc)
-			goto err;
-	}
-
-	return 0;
-err:
-	free_pages_exact(shm->kaddr, shm->size);
-	shm->kaddr = NULL;
-	return rc;
-}
-
-void optee_pool_op_free_helper(struct tee_shm_pool *pool, struct tee_shm *shm,
-			       int (*shm_unregister)(struct tee_context *ctx,
-						     struct tee_shm *shm))
-{
-	if (shm_unregister)
-		shm_unregister(shm->ctx, shm);
-	free_pages_exact(shm->kaddr, shm->size);
-	shm->kaddr = NULL;
-	kfree(shm->pages);
-	shm->pages = NULL;
-}
-
 static void optee_bus_scan(struct work_struct *work)
 {
 	WARN_ON(optee_enumerate_devices(PTA_CMD_GET_DEVICES_SUPP));
drivers/tee/optee/ffa_abi.c +3 -3
index cee8ccb84cb8..3235e1c719e8 100644
@@ -374,14 +374,14 @@ static int optee_ffa_shm_unregister_supp(struct tee_context *ctx,
 static int pool_ffa_op_alloc(struct tee_shm_pool *pool,
 			     struct tee_shm *shm, size_t size, size_t align)
 {
-	return optee_pool_op_alloc_helper(pool, shm, size, align,
-					  optee_ffa_shm_register);
+	return tee_dyn_shm_alloc_helper(shm, size, align,
+					optee_ffa_shm_register);
 }
 
 static void pool_ffa_op_free(struct tee_shm_pool *pool,
 			     struct tee_shm *shm)
 {
-	optee_pool_op_free_helper(pool, shm, optee_ffa_shm_unregister);
+	tee_dyn_shm_free_helper(shm, optee_ffa_shm_unregister);
 }
 
 static void pool_ffa_op_destroy_pool(struct tee_shm_pool *pool)
drivers/tee/optee/optee_private.h +0 -12
index a0698ac18993..429cc20be5cc 100644
@@ -283,18 +283,6 @@ int optee_cancel_req(struct tee_context *ctx, u32 cancel_id, u32 session);
 int optee_enumerate_devices(u32 func);
 void optee_unregister_devices(void);
 
-int optee_pool_op_alloc_helper(struct tee_shm_pool *pool, struct tee_shm *shm,
-			       size_t size, size_t align,
-			       int (*shm_register)(struct tee_context *ctx,
-						   struct tee_shm *shm,
-						   struct page **pages,
-						   size_t num_pages,
-						   unsigned long start));
-void optee_pool_op_free_helper(struct tee_shm_pool *pool, struct tee_shm *shm,
-			       int (*shm_unregister)(struct tee_context *ctx,
-						     struct tee_shm *shm));
-
-
 void optee_remove_common(struct optee *optee);
 int optee_open(struct tee_context *ctx, bool cap_memref_null);
 void optee_release(struct tee_context *ctx);
drivers/tee/optee/smc_abi.c +4 -5
index 9c296b887dc1..734c484ed0f6 100644
@@ -592,19 +592,18 @@ static int pool_op_alloc(struct tee_shm_pool *pool,
 	 * to be registered with OP-TEE.
 	 */
 	if (shm->flags & TEE_SHM_PRIV)
-		return optee_pool_op_alloc_helper(pool, shm, size, align, NULL);
+		return tee_dyn_shm_alloc_helper(shm, size, align, NULL);
 
-	return optee_pool_op_alloc_helper(pool, shm, size, align,
-					  optee_shm_register);
+	return tee_dyn_shm_alloc_helper(shm, size, align, optee_shm_register);
 }
 
 static void pool_op_free(struct tee_shm_pool *pool,
 			 struct tee_shm *shm)
 {
 	if (!(shm->flags & TEE_SHM_PRIV))
-		optee_pool_op_free_helper(pool, shm, optee_shm_unregister);
+		tee_dyn_shm_free_helper(shm, optee_shm_unregister);
 	else
-		optee_pool_op_free_helper(pool, shm, NULL);
+		tee_dyn_shm_free_helper(shm, NULL);
 }
 
 static void pool_op_destroy_pool(struct tee_shm_pool *pool)
drivers/tee/tee_shm.c +64 -0

70 lines not on this page · raw message

include/linux/tee_core.h +10 -0
index d9b3ba8e8fa9..efd16ed52315 100644
@@ -232,6 +232,16 @@ void *tee_get_drvdata(struct tee_device *teedev);
  */
 struct tee_shm *tee_shm_alloc_priv_buf(struct tee_context *ctx, size_t size);
 
+int tee_dyn_shm_alloc_helper(struct tee_shm *shm, size_t size, size_t align,
+			     int (*shm_register)(struct tee_context *ctx,
+						 struct tee_shm *shm,
+						 struct page **pages,
+						 size_t num_pages,
+						 unsigned long start));
+void tee_dyn_shm_free_helper(struct tee_shm *shm,
+			     int (*shm_unregister)(struct tee_context *ctx,
+						   struct tee_shm *shm));
+
 /**
  * tee_shm_is_dynamic() - Check if shared memory object is of the dynamic kind
  * @shm:	Shared memory handle
Signature · 1 line
2.34.1
Reviewed-by on v3 2/4
#3 lore raw

Re: [PATCH v3 2/4] tee: optee: Move pool_op helper functions

Quoted 8 lines · Balint Dobszay

Move the pool alloc and free helper functions from the OP-TEE driver to the TEE subsystem, since these could be reused in other TEE drivers. This patch is not supposed to change behavior, it's only reorganizing the code.

Quoted 9 lines
---
 drivers/tee/optee/core.c          | 64 -------------------------------
 drivers/tee/optee/ffa_abi.c       |  6 +--
 drivers/tee/optee/optee_private.h | 12 ------
 drivers/tee/optee/smc_abi.c       |  9 ++---
 drivers/tee/tee_shm.c             | 64 +++++++++++++++++++++++++++++++
 include/linux/tee_core.h          | 10 +++++
 6 files changed, 81 insertions(+), 84 deletions(-)

-Sumit

Quoted 254 lines
drivers/tee/optee/core.c +0 -64
index f762e3a25119..39e688d4e974 100644
@@ -9,7 +9,6 @@
 #include <linux/crash_dump.h>
 #include <linux/errno.h>
 #include <linux/io.h>
-#include <linux/mm.h>
 #include <linux/module.h>
 #include <linux/slab.h>
 #include <linux/string.h>
@@ -17,69 +16,6 @@
 #include <linux/types.h>
 #include "optee_private.h"
 
-int optee_pool_op_alloc_helper(struct tee_shm_pool *pool, struct tee_shm *shm,
-                              size_t size, size_t align,
-                              int (*shm_register)(struct tee_context *ctx,
-                                                  struct tee_shm *shm,
-                                                  struct page **pages,
-                                                  size_t num_pages,
-                                                  unsigned long start))
-{
-       size_t nr_pages = roundup(size, PAGE_SIZE) / PAGE_SIZE;
-       struct page **pages;
-       unsigned int i;
-       int rc = 0;
-
-       /*
-        * Ignore alignment since this is already going to be page aligned
-        * and there's no need for any larger alignment.
-        */
-       shm->kaddr = alloc_pages_exact(nr_pages * PAGE_SIZE,
-                                      GFP_KERNEL | __GFP_ZERO);
-       if (!shm->kaddr)
-               return -ENOMEM;
-
-       shm->paddr = virt_to_phys(shm->kaddr);
-       shm->size = nr_pages * PAGE_SIZE;
-
-       pages = kcalloc(nr_pages, sizeof(*pages), GFP_KERNEL);
-       if (!pages) {
-               rc = -ENOMEM;
-               goto err;
-       }
-
-       for (i = 0; i < nr_pages; i++)
-               pages[i] = virt_to_page((u8 *)shm->kaddr + i * PAGE_SIZE);
-
-       shm->pages = pages;
-       shm->num_pages = nr_pages;
-
-       if (shm_register) {
-               rc = shm_register(shm->ctx, shm, pages, nr_pages,
-                                 (unsigned long)shm->kaddr);
-               if (rc)
-                       goto err;
-       }
-
-       return 0;
-err:
-       free_pages_exact(shm->kaddr, shm->size);
-       shm->kaddr = NULL;
-       return rc;
-}
-
-void optee_pool_op_free_helper(struct tee_shm_pool *pool, struct tee_shm *shm,
-                              int (*shm_unregister)(struct tee_context *ctx,
-                                                    struct tee_shm *shm))
-{
-       if (shm_unregister)
-               shm_unregister(shm->ctx, shm);
-       free_pages_exact(shm->kaddr, shm->size);
-       shm->kaddr = NULL;
-       kfree(shm->pages);
-       shm->pages = NULL;
-}
-
 static void optee_bus_scan(struct work_struct *work)
 {
        WARN_ON(optee_enumerate_devices(PTA_CMD_GET_DEVICES_SUPP));
drivers/tee/optee/ffa_abi.c +3 -3
index cee8ccb84cb8..3235e1c719e8 100644
@@ -374,14 +374,14 @@ static int optee_ffa_shm_unregister_supp(struct tee_context *ctx,
 static int pool_ffa_op_alloc(struct tee_shm_pool *pool,
                             struct tee_shm *shm, size_t size, size_t align)
 {
-       return optee_pool_op_alloc_helper(pool, shm, size, align,
-                                         optee_ffa_shm_register);
+       return tee_dyn_shm_alloc_helper(shm, size, align,
+                                       optee_ffa_shm_register);
 }
 
 static void pool_ffa_op_free(struct tee_shm_pool *pool,
                             struct tee_shm *shm)
 {
-       optee_pool_op_free_helper(pool, shm, optee_ffa_shm_unregister);
+       tee_dyn_shm_free_helper(shm, optee_ffa_shm_unregister);
 }
 
 static void pool_ffa_op_destroy_pool(struct tee_shm_pool *pool)
drivers/tee/optee/optee_private.h +0 -12
index a0698ac18993..429cc20be5cc 100644
@@ -283,18 +283,6 @@ int optee_cancel_req(struct tee_context *ctx, u32 cancel_id, u32 session);
 int optee_enumerate_devices(u32 func);
 void optee_unregister_devices(void);
 
-int optee_pool_op_alloc_helper(struct tee_shm_pool *pool, struct tee_shm *shm,
-                              size_t size, size_t align,
-                              int (*shm_register)(struct tee_context *ctx,
-                                                  struct tee_shm *shm,
-                                                  struct page **pages,
-                                                  size_t num_pages,
-                                                  unsigned long start));
-void optee_pool_op_free_helper(struct tee_shm_pool *pool, struct tee_shm *shm,
-                              int (*shm_unregister)(struct tee_context *ctx,
-                                                    struct tee_shm *shm));
-
-
 void optee_remove_common(struct optee *optee);
 int optee_open(struct tee_context *ctx, bool cap_memref_null);
 void optee_release(struct tee_context *ctx);
drivers/tee/optee/smc_abi.c +4 -5
index 9c296b887dc1..734c484ed0f6 100644
@@ -592,19 +592,18 @@ static int pool_op_alloc(struct tee_shm_pool *pool,
         * to be registered with OP-TEE.
         */
        if (shm->flags & TEE_SHM_PRIV)
-               return optee_pool_op_alloc_helper(pool, shm, size, align, NULL);
+               return tee_dyn_shm_alloc_helper(shm, size, align, NULL);
 
-       return optee_pool_op_alloc_helper(pool, shm, size, align,
-                                         optee_shm_register);
+       return tee_dyn_shm_alloc_helper(shm, size, align, optee_shm_register);
 }
 
 static void pool_op_free(struct tee_shm_pool *pool,
                         struct tee_shm *shm)
 {
        if (!(shm->flags & TEE_SHM_PRIV))
-               optee_pool_op_free_helper(pool, shm, optee_shm_unregister);
+               tee_dyn_shm_free_helper(shm, optee_shm_unregister);
        else
-               optee_pool_op_free_helper(pool, shm, NULL);
+               tee_dyn_shm_free_helper(shm, NULL);
 }
 
 static void pool_op_destroy_pool(struct tee_shm_pool *pool)
drivers/tee/tee_shm.c +64 -0
index 96a45c817427..5bf76c35cd9e 100644
@@ -202,6 +202,70 @@ struct tee_shm *tee_shm_alloc_priv_buf(struct tee_context *ctx, size_t size)
 }
 EXPORT_SYMBOL_GPL(tee_shm_alloc_priv_buf);
 
+int tee_dyn_shm_alloc_helper(struct tee_shm *shm, size_t size, size_t align,
+                            int (*shm_register)(struct tee_context *ctx,
+                                                struct tee_shm *shm,
+                                                struct page **pages,
+                                                size_t num_pages,
+                                                unsigned long start))
+{
+       size_t nr_pages = roundup(size, PAGE_SIZE) / PAGE_SIZE;
+       struct page **pages;
+       unsigned int i;
+       int rc = 0;
+
+       /*
+        * Ignore alignment since this is already going to be page aligned
+        * and there's no need for any larger alignment.
+        */
+       shm->kaddr = alloc_pages_exact(nr_pages * PAGE_SIZE,
+                                      GFP_KERNEL | __GFP_ZERO);
+       if (!shm->kaddr)
+               return -ENOMEM;
+
+       shm->paddr = virt_to_phys(shm->kaddr);
+       shm->size = nr_pages * PAGE_SIZE;
+
+       pages = kcalloc(nr_pages, sizeof(*pages), GFP_KERNEL);
+       if (!pages) {
+               rc = -ENOMEM;
+               goto err;
+       }
+
+       for (i = 0; i < nr_pages; i++)
+               pages[i] = virt_to_page((u8 *)shm->kaddr + i * PAGE_SIZE);
+
+       shm->pages = pages;
+       shm->num_pages = nr_pages;
+
+       if (shm_register) {
+               rc = shm_register(shm->ctx, shm, pages, nr_pages,
+                                 (unsigned long)shm->kaddr);
+               if (rc)
+                       goto err;
+       }
+
+       return 0;
+err:
+       free_pages_exact(shm->kaddr, shm->size);
+       shm->kaddr = NULL;
+       return rc;
+}
+EXPORT_SYMBOL_GPL(tee_dyn_shm_alloc_helper);
+
+void tee_dyn_shm_free_helper(struct tee_shm *shm,
+                            int (*shm_unregister)(struct tee_context *ctx,
+                                                  struct tee_shm *shm))
+{
+       if (shm_unregister)
+               shm_unregister(shm->ctx, shm);
+       free_pages_exact(shm->kaddr, shm->size);
+       shm->kaddr = NULL;
+       kfree(shm->pages);
+       shm->pages = NULL;
+}
+EXPORT_SYMBOL_GPL(tee_dyn_shm_free_helper);
+
 static struct tee_shm *
 register_shm_helper(struct tee_context *ctx, struct iov_iter *iter, u32 flags,
                    int id)
include/linux/tee_core.h +10 -1
index d9b3ba8e8fa9..efd16ed52315 100644
@@ -232,6 +232,16 @@ void *tee_get_drvdata(struct tee_device *teedev);
  */
 struct tee_shm *tee_shm_alloc_priv_buf(struct tee_context *ctx, size_t size);
 
+int tee_dyn_shm_alloc_helper(struct tee_shm *shm, size_t size, size_t align,
+                            int (*shm_register)(struct tee_context *ctx,
+                                                struct tee_shm *shm,
+                                                struct page **pages,
+                                                size_t num_pages,
+                                                unsigned long start));
+void tee_dyn_shm_free_helper(struct tee_shm *shm,
+                            int (*shm_unregister)(struct tee_context *ctx,
+                                                  struct tee_shm *shm));
+
 /**
  * tee_shm_is_dynamic() - Check if shared memory object is of the dynamic kind
  * @shm:       Shared memory handle
--

2.34.1

author maintainer
#4 lore raw

Re: [PATCH v3 2/4] tee: optee: Move pool_op helper functions

Quoted 19 lines · Sumit Garg
Quoted 8 lines · Balint Dobszay

Move the pool alloc and free helper functions from the OP-TEE driver to the TEE subsystem, since these could be reused in other TEE drivers. This patch is not supposed to change behavior, it's only reorganizing the code.

Quoted 9 lines
---
 drivers/tee/optee/core.c          | 64 -------------------------------
 drivers/tee/optee/ffa_abi.c       |  6 +--
 drivers/tee/optee/optee_private.h | 12 ------
 drivers/tee/optee/smc_abi.c       |  9 ++---
 drivers/tee/tee_shm.c             | 64 +++++++++++++++++++++++++++++++
 include/linux/tee_core.h          | 10 +++++
 6 files changed, 81 insertions(+), 84 deletions(-)
Quoted 1 line

Thanks for the review. I'll add your tag for the next version.

Regards,
Balint

Quoted 254 lines
Quoted 254 lines
drivers/tee/optee/core.c +0 -64
index f762e3a25119..39e688d4e974 100644
@@ -9,7 +9,6 @@
 #include <linux/crash_dump.h>
 #include <linux/errno.h>
 #include <linux/io.h>
-#include <linux/mm.h>
 #include <linux/module.h>
 #include <linux/slab.h>
 #include <linux/string.h>
@@ -17,69 +16,6 @@
 #include <linux/types.h>
 #include "optee_private.h"
 
-int optee_pool_op_alloc_helper(struct tee_shm_pool *pool, struct tee_shm *shm,
-                              size_t size, size_t align,
-                              int (*shm_register)(struct tee_context *ctx,
-                                                  struct tee_shm *shm,
-                                                  struct page **pages,
-                                                  size_t num_pages,
-                                                  unsigned long start))
-{
-       size_t nr_pages = roundup(size, PAGE_SIZE) / PAGE_SIZE;
-       struct page **pages;
-       unsigned int i;
-       int rc = 0;
-
-       /*
-        * Ignore alignment since this is already going to be page aligned
-        * and there's no need for any larger alignment.
-        */
-       shm->kaddr = alloc_pages_exact(nr_pages * PAGE_SIZE,
-                                      GFP_KERNEL | __GFP_ZERO);
-       if (!shm->kaddr)
-               return -ENOMEM;
-
-       shm->paddr = virt_to_phys(shm->kaddr);
-       shm->size = nr_pages * PAGE_SIZE;
-
-       pages = kcalloc(nr_pages, sizeof(*pages), GFP_KERNEL);
-       if (!pages) {
-               rc = -ENOMEM;
-               goto err;
-       }
-
-       for (i = 0; i < nr_pages; i++)
-               pages[i] = virt_to_page((u8 *)shm->kaddr + i * PAGE_SIZE);
-
-       shm->pages = pages;
-       shm->num_pages = nr_pages;
-
-       if (shm_register) {
-               rc = shm_register(shm->ctx, shm, pages, nr_pages,
-                                 (unsigned long)shm->kaddr);
-               if (rc)
-                       goto err;
-       }
-
-       return 0;
-err:
-       free_pages_exact(shm->kaddr, shm->size);
-       shm->kaddr = NULL;
-       return rc;
-}
-
-void optee_pool_op_free_helper(struct tee_shm_pool *pool, struct tee_shm *shm,
-                              int (*shm_unregister)(struct tee_context *ctx,
-                                                    struct tee_shm *shm))
-{
-       if (shm_unregister)
-               shm_unregister(shm->ctx, shm);
-       free_pages_exact(shm->kaddr, shm->size);
-       shm->kaddr = NULL;
-       kfree(shm->pages);
-       shm->pages = NULL;
-}
-
 static void optee_bus_scan(struct work_struct *work)
 {
        WARN_ON(optee_enumerate_devices(PTA_CMD_GET_DEVICES_SUPP));
drivers/tee/optee/ffa_abi.c +3 -3
index cee8ccb84cb8..3235e1c719e8 100644
@@ -374,14 +374,14 @@ static int optee_ffa_shm_unregister_supp(struct tee_context *ctx,
 static int pool_ffa_op_alloc(struct tee_shm_pool *pool,
                             struct tee_shm *shm, size_t size, size_t align)
 {
-       return optee_pool_op_alloc_helper(pool, shm, size, align,
-                                         optee_ffa_shm_register);
+       return tee_dyn_shm_alloc_helper(shm, size, align,
+                                       optee_ffa_shm_register);
 }
 
 static void pool_ffa_op_free(struct tee_shm_pool *pool,
                             struct tee_shm *shm)
 {
-       optee_pool_op_free_helper(pool, shm, optee_ffa_shm_unregister);
+       tee_dyn_shm_free_helper(shm, optee_ffa_shm_unregister);
 }
 
 static void pool_ffa_op_destroy_pool(struct tee_shm_pool *pool)
drivers/tee/optee/optee_private.h +0 -12
index a0698ac18993..429cc20be5cc 100644
@@ -283,18 +283,6 @@ int optee_cancel_req(struct tee_context *ctx, u32 cancel_id, u32 session);
 int optee_enumerate_devices(u32 func);
 void optee_unregister_devices(void);
 
-int optee_pool_op_alloc_helper(struct tee_shm_pool *pool, struct tee_shm *shm,
-                              size_t size, size_t align,
-                              int (*shm_register)(struct tee_context *ctx,
-                                                  struct tee_shm *shm,
-                                                  struct page **pages,
-                                                  size_t num_pages,
-                                                  unsigned long start));
-void optee_pool_op_free_helper(struct tee_shm_pool *pool, struct tee_shm *shm,
-                              int (*shm_unregister)(struct tee_context *ctx,
-                                                    struct tee_shm *shm));
-
-
 void optee_remove_common(struct optee *optee);
 int optee_open(struct tee_context *ctx, bool cap_memref_null);
 void optee_release(struct tee_context *ctx);
drivers/tee/optee/smc_abi.c +4 -5
index 9c296b887dc1..734c484ed0f6 100644
@@ -592,19 +592,18 @@ static int pool_op_alloc(struct tee_shm_pool *pool,
         * to be registered with OP-TEE.
         */
        if (shm->flags & TEE_SHM_PRIV)
-               return optee_pool_op_alloc_helper(pool, shm, size, align, NULL);
+               return tee_dyn_shm_alloc_helper(shm, size, align, NULL);
 
-       return optee_pool_op_alloc_helper(pool, shm, size, align,
-                                         optee_shm_register);
+       return tee_dyn_shm_alloc_helper(shm, size, align, optee_shm_register);
 }
 
 static void pool_op_free(struct tee_shm_pool *pool,
                         struct tee_shm *shm)
 {
        if (!(shm->flags & TEE_SHM_PRIV))
-               optee_pool_op_free_helper(pool, shm, optee_shm_unregister);
+               tee_dyn_shm_free_helper(shm, optee_shm_unregister);
        else
-               optee_pool_op_free_helper(pool, shm, NULL);
+               tee_dyn_shm_free_helper(shm, NULL);
 }
 
 static void pool_op_destroy_pool(struct tee_shm_pool *pool)
drivers/tee/tee_shm.c +64 -0
index 96a45c817427..5bf76c35cd9e 100644
@@ -202,6 +202,70 @@ struct tee_shm *tee_shm_alloc_priv_buf(struct tee_context *ctx, size_t size)
 }
 EXPORT_SYMBOL_GPL(tee_shm_alloc_priv_buf);
 
+int tee_dyn_shm_alloc_helper(struct tee_shm *shm, size_t size, size_t align,
+                            int (*shm_register)(struct tee_context *ctx,
+                                                struct tee_shm *shm,
+                                                struct page **pages,
+                                                size_t num_pages,
+                                                unsigned long start))
+{
+       size_t nr_pages = roundup(size, PAGE_SIZE) / PAGE_SIZE;
+       struct page **pages;
+       unsigned int i;
+       int rc = 0;
+
+       /*
+        * Ignore alignment since this is already going to be page aligned
+        * and there's no need for any larger alignment.
+        */
+       shm->kaddr = alloc_pages_exact(nr_pages * PAGE_SIZE,
+                                      GFP_KERNEL | __GFP_ZERO);
+       if (!shm->kaddr)
+               return -ENOMEM;
+
+       shm->paddr = virt_to_phys(shm->kaddr);
+       shm->size = nr_pages * PAGE_SIZE;
+
+       pages = kcalloc(nr_pages, sizeof(*pages), GFP_KERNEL);
+       if (!pages) {
+               rc = -ENOMEM;
+               goto err;
+       }
+
+       for (i = 0; i < nr_pages; i++)
+               pages[i] = virt_to_page((u8 *)shm->kaddr + i * PAGE_SIZE);
+
+       shm->pages = pages;
+       shm->num_pages = nr_pages;
+
+       if (shm_register) {
+               rc = shm_register(shm->ctx, shm, pages, nr_pages,
+                                 (unsigned long)shm->kaddr);
+               if (rc)
+                       goto err;
+       }
+
+       return 0;
+err:
+       free_pages_exact(shm->kaddr, shm->size);
+       shm->kaddr = NULL;
+       return rc;
+}
+EXPORT_SYMBOL_GPL(tee_dyn_shm_alloc_helper);
+
+void tee_dyn_shm_free_helper(struct tee_shm *shm,
+                            int (*shm_unregister)(struct tee_context *ctx,
+                                                  struct tee_shm *shm))
+{
+       if (shm_unregister)
+               shm_unregister(shm->ctx, shm);
+       free_pages_exact(shm->kaddr, shm->size);
+       shm->kaddr = NULL;
+       kfree(shm->pages);
+       shm->pages = NULL;
+}
+EXPORT_SYMBOL_GPL(tee_dyn_shm_free_helper);
+
 static struct tee_shm *
 register_shm_helper(struct tee_context *ctx, struct iov_iter *iter, u32 flags,
                    int id)
include/linux/tee_core.h +10 -1
index d9b3ba8e8fa9..efd16ed52315 100644
@@ -232,6 +232,16 @@ void *tee_get_drvdata(struct tee_device *teedev);
  */
 struct tee_shm *tee_shm_alloc_priv_buf(struct tee_context *ctx, size_t size);
 
+int tee_dyn_shm_alloc_helper(struct tee_shm *shm, size_t size, size_t align,
+                            int (*shm_register)(struct tee_context *ctx,
+                                                struct tee_shm *shm,
+                                                struct page **pages,
+                                                size_t num_pages,
+                                                unsigned long start));
+void tee_dyn_shm_free_helper(struct tee_shm *shm,
+                            int (*shm_unregister)(struct tee_context *ctx,
+                                                  struct tee_shm *shm));
+
 /**
  * tee_shm_is_dynamic() - Check if shared memory object is of the dynamic kind
  * @shm:       Shared memory handle
--

2.34.1

author maintainer
#5 lore raw

PATCH v3 3/4 tee: tstee: Add Trusted Services TEE driver

In reply to Balint Dobszay

The Trusted Services project provides a framework for developing and deploying device Root of Trust services in FF-A Secure Partitions. The FF-A SPs are accessible through the FF-A driver, but this doesn't provide a user space interface. The goal of this TEE driver is to make Trusted Services SPs accessible for user space clients.

All TS SPs have the same FF-A UUID, it identifies the RPC protocol used by TS. A TS SP can host one or more services, a service is identified by its service UUID. The same type of service cannot be present twice in the same SP. During SP boot each service in an SP is assigned an interface ID, this is just a short ID to simplify message addressing. There is 1:1 mapping between TS SPs and TEE devices, i.e. a separate TEE device is registered for each TS SP. This is required since contrary to the generic TEE design where memory is shared with the whole TEE implementation, in case of FF-A, memory is shared with a specific SP. A user space client has to be able to separately share memory with each SP based on its endpoint ID.

---
 drivers/tee/Kconfig               |   1 +
 drivers/tee/Makefile              |   1 +
 drivers/tee/tstee/Kconfig         |  11 +
 drivers/tee/tstee/Makefile        |   3 +
 drivers/tee/tstee/core.c          | 482 ++++++++++++++++++++++++++++++
 drivers/tee/tstee/tstee_private.h |  92 ++++++
 include/uapi/linux/tee.h          |   1 +
 7 files changed, 591 insertions(+)
 create mode 100644 drivers/tee/tstee/Kconfig
 create mode 100644 drivers/tee/tstee/Makefile
 create mode 100644 drivers/tee/tstee/core.c
 create mode 100644 drivers/tee/tstee/tstee_private.h
drivers/tee/Kconfig +1 -0
index 73a147202e88..61b507c18780 100644
@@ -15,5 +15,6 @@ if TEE
 
 source "drivers/tee/optee/Kconfig"
 source "drivers/tee/amdtee/Kconfig"
+source "drivers/tee/tstee/Kconfig"
 
 endif
drivers/tee/Makefile +1 -0
index 68da044afbfa..5488cba30bd2 100644
@@ -5,3 +5,4 @@ tee-objs += tee_shm.o
 tee-objs += tee_shm_pool.o
 obj-$(CONFIG_OPTEE) += optee/
 obj-$(CONFIG_AMDTEE) += amdtee/
+obj-$(CONFIG_ARM_TSTEE) += tstee/
drivers/tee/tstee/Kconfig new file +11 -0
new file mode 100644
index 000000000000..d32f91d47398
@@ -0,0 +1,11 @@
+# SPDX-License-Identifier: GPL-2.0-only
+config ARM_TSTEE
+	tristate "Arm Trusted Services TEE driver"
+	depends on ARM_FFA_TRANSPORT
+	default n
+	help
+	  The Trusted Services project provides a framework for developing and
+	  deploying device Root of Trust services in FF-A Secure Partitions.
+	  This driver provides an interface to make Trusted Services Secure
+	  Partitions accessible for user space clients, since the FF-A driver
+	  doesn't implement a user space interface directly.
drivers/tee/tstee/Makefile new file +3 -0
new file mode 100644
index 000000000000..5227020ebd30
@@ -0,0 +1,3 @@
+# SPDX-License-Identifier: GPL-2.0-only
+arm-tstee-objs := core.o
+obj-$(CONFIG_ARM_TSTEE) = arm-tstee.o
drivers/tee/tstee/core.c new file +482 -0

482 lines not on this page · raw message

drivers/tee/tstee/tstee_private.h new file +92 -0

92 lines not on this page · raw message

include/uapi/linux/tee.h +1 -0
index 23e57164693c..d0430bee8292 100644
@@ -56,6 +56,7 @@
  */
 #define TEE_IMPL_ID_OPTEE	1
 #define TEE_IMPL_ID_AMDTEE	2
+#define TEE_IMPL_ID_TSTEE	3
 
 /*
  * OP-TEE specific capabilities
Signature · 1 line
2.34.1
Acked-by on v3 3/4
#6 lore raw

Re: [PATCH v3 3/4] tee: tstee: Add Trusted Services TEE driver

Hi Balint,

Quoted 128 lines · Balint Dobszay

The Trusted Services project provides a framework for developing and deploying device Root of Trust services in FF-A Secure Partitions. The FF-A SPs are accessible through the FF-A driver, but this doesn't provide a user space interface. The goal of this TEE driver is to make Trusted Services SPs accessible for user space clients.

All TS SPs have the same FF-A UUID, it identifies the RPC protocol used by TS. A TS SP can host one or more services, a service is identified by its service UUID. The same type of service cannot be present twice in the same SP. During SP boot each service in an SP is assigned an interface ID, this is just a short ID to simplify message addressing. There is 1:1 mapping between TS SPs and TEE devices, i.e. a separate TEE device is registered for each TS SP. This is required since contrary to the generic TEE design where memory is shared with the whole TEE implementation, in case of FF-A, memory is shared with a specific SP. A user space client has to be able to separately share memory with each SP based on its endpoint ID.

---
 drivers/tee/Kconfig               |   1 +
 drivers/tee/Makefile              |   1 +
 drivers/tee/tstee/Kconfig         |  11 +
 drivers/tee/tstee/Makefile        |   3 +
 drivers/tee/tstee/core.c          | 482 ++++++++++++++++++++++++++++++
 drivers/tee/tstee/tstee_private.h |  92 ++++++
 include/uapi/linux/tee.h          |   1 +
 7 files changed, 591 insertions(+)
 create mode 100644 drivers/tee/tstee/Kconfig
 create mode 100644 drivers/tee/tstee/Makefile
 create mode 100644 drivers/tee/tstee/core.c
 create mode 100644 drivers/tee/tstee/tstee_private.h
drivers/tee/Kconfig +1 -0
index 73a147202e88..61b507c18780 100644
@@ -15,5 +15,6 @@ if TEE
 
 source "drivers/tee/optee/Kconfig"
 source "drivers/tee/amdtee/Kconfig"
+source "drivers/tee/tstee/Kconfig"
 
 endif
drivers/tee/Makefile +1 -0
index 68da044afbfa..5488cba30bd2 100644
@@ -5,3 +5,4 @@ tee-objs += tee_shm.o
 tee-objs += tee_shm_pool.o
 obj-$(CONFIG_OPTEE) += optee/
 obj-$(CONFIG_AMDTEE) += amdtee/
+obj-$(CONFIG_ARM_TSTEE) += tstee/
drivers/tee/tstee/Kconfig new file +11 -0
new file mode 100644
index 000000000000..d32f91d47398
@@ -0,0 +1,11 @@
+# SPDX-License-Identifier: GPL-2.0-only
+config ARM_TSTEE
+       tristate "Arm Trusted Services TEE driver"
+       depends on ARM_FFA_TRANSPORT
+       default n
+       help
+         The Trusted Services project provides a framework for developing and
+         deploying device Root of Trust services in FF-A Secure Partitions.
+         This driver provides an interface to make Trusted Services Secure
+         Partitions accessible for user space clients, since the FF-A driver
+         doesn't implement a user space interface directly.
drivers/tee/tstee/Makefile new file +3 -0
new file mode 100644
index 000000000000..5227020ebd30
@@ -0,0 +1,3 @@
+# SPDX-License-Identifier: GPL-2.0-only
+arm-tstee-objs := core.o
+obj-$(CONFIG_ARM_TSTEE) = arm-tstee.o
drivers/tee/tstee/core.c new file +42 -0
new file mode 100644
index 000000000000..577c9d52673e
@@ -0,0 +1,482 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (c) 2023, Arm Limited
+ */
+
+#include <linux/arm_ffa.h>
+#include <linux/err.h>
+#include <linux/errno.h>
+#include <linux/kernel.h>
+#include <linux/limits.h>
+#include <linux/mm.h>
+#include <linux/module.h>
+#include <linux/scatterlist.h>
+#include <linux/slab.h>
+#include <linux/tee_core.h>
+#include <linux/types.h>
+#include <linux/uuid.h>
+#include <linux/xarray.h>
+#include "tstee_private.h"
+
+#define FFA_DIRECT_REQ_ARG_NUM 5
+#define FFA_INVALID_MEM_HANDLE U64_MAX
+
+static void arg_list_to_ffa_data(const u32 *args,
+                                struct ffa_send_direct_data *data)
+{
+       data->data0 = args[0];
+       data->data1 = args[1];
+       data->data2 = args[2];
+       data->data3 = args[3];
+       data->data4 = args[4];
+}
+
+static void arg_list_from_ffa_data(const struct ffa_send_direct_data *data,
+                                  u32 *args)
+{
+       args[0] = lower_32_bits(data->data0);
+       args[1] = lower_32_bits(data->data1);
+       args[2] = lower_32_bits(data->data2);
+       args[3] = lower_32_bits(data->data3);
+       args[4] = lower_32_bits(data->data4);
+}

drivers/tee/tstee/core.c line 52

Quoted 10 lines
drivers/tee/tstee/core.c new file +10 -0
+
+static void tstee_get_version(struct tee_device *teedev,
+                             struct tee_ioctl_version_data *vers)
+{
+       struct tstee *tstee = tee_get_drvdata(teedev);
+       struct tee_ioctl_version_data v = {
+               .impl_id = TEE_IMPL_ID_TSTEE,
+               /* FF-A endpoint ID only uses the lower 16 bits */
+               .impl_caps = lower_16_bits(tstee->ffa_dev->vm_id),
+               .gen_caps = 0,

As I mentioned during the RFC review, since you aren't supporting TEE_GEN_CAP_REG_MEM capability here...

Quoted 286 lines
drivers/tee/tstee/core.c new file +286 -0
+       };
+
+       *vers = v;
+}
+
+static int tstee_open(struct tee_context *ctx)
+{
+       struct ts_context_data *ctxdata;
+
+       ctxdata = kzalloc(sizeof(*ctxdata), GFP_KERNEL);
+       if (!ctxdata)
+               return -ENOMEM;
+
+       xa_init_flags(&ctxdata->sess_list, XA_FLAGS_ALLOC);
+
+       ctx->data = ctxdata;
+
+       return 0;
+}
+
+static void tstee_release(struct tee_context *ctx)
+{
+       struct ts_context_data *ctxdata = ctx->data;
+       struct ts_session *sess;
+       unsigned long idx;
+
+       if (!ctxdata)
+               return;
+
+       xa_for_each(&ctxdata->sess_list, idx, sess) {
+               xa_erase(&ctxdata->sess_list, idx);
+               kfree(sess);
+       }
+
+       xa_destroy(&ctxdata->sess_list);
+
+       kfree(ctxdata);
+       ctx->data = NULL;
+}
+
+static int tstee_open_session(struct tee_context *ctx,
+                             struct tee_ioctl_open_session_arg *arg,
+                             struct tee_param *param __always_unused)
+{
+       struct tstee *tstee = tee_get_drvdata(ctx->teedev);
+       struct ffa_device *ffa_dev = tstee->ffa_dev;
+       struct ts_context_data *ctxdata = ctx->data;
+       struct ffa_send_direct_data ffa_data;
+       struct ts_session *sess = NULL;
+       u32 ffa_args[FFA_DIRECT_REQ_ARG_NUM] = {};
+       u32 sess_id;
+       int rc;
+
+       ffa_args[TS_RPC_CTRL_REG] =
+               TS_RPC_CTRL_PACK_IFACE_OPCODE(TS_RPC_MGMT_IFACE_ID,
+                                             TS_RPC_OP_SERVICE_INFO);
+
+       memcpy(ffa_args + TS_RPC_SERVICE_INFO_UUID0, arg->uuid, UUID_SIZE);
+
+       arg_list_to_ffa_data(ffa_args, &ffa_data);
+       rc = ffa_dev->ops->msg_ops->sync_send_receive(ffa_dev, &ffa_data);
+       if (rc)
+               return rc;
+
+       arg_list_from_ffa_data(&ffa_data, ffa_args);
+
+       if (ffa_args[TS_RPC_SERVICE_INFO_RPC_STATUS] != TS_RPC_OK)
+               return -ENODEV;
+
+       if (ffa_args[TS_RPC_SERVICE_INFO_IFACE] > U8_MAX)
+               return -EINVAL;
+
+       sess = kzalloc(sizeof(*sess), GFP_KERNEL);
+       if (!sess)
+               return -ENOMEM;
+
+       sess->iface_id = ffa_args[TS_RPC_SERVICE_INFO_IFACE];
+
+       rc = xa_alloc(&ctxdata->sess_list, &sess_id, sess, xa_limit_32b,
+                     GFP_KERNEL);
+       if (rc) {
+               kfree(sess);
+               return rc;
+       }
+
+       arg->session = sess_id;
+       arg->ret = 0;
+
+       return 0;
+}
+
+static int tstee_close_session(struct tee_context *ctx, u32 session)
+{
+       struct ts_context_data *ctxdata = ctx->data;
+       struct ts_session *sess;
+
+       /* Calls xa_lock() internally */
+       sess = xa_erase(&ctxdata->sess_list, session);
+       if (!sess)
+               return -EINVAL;
+
+       kfree(sess);
+
+       return 0;
+}
+
+static int tstee_invoke_func(struct tee_context *ctx,
+                            struct tee_ioctl_invoke_arg *arg,
+                            struct tee_param *param)
+{
+       struct tstee *tstee = tee_get_drvdata(ctx->teedev);
+       struct ffa_device *ffa_dev = tstee->ffa_dev;
+       struct ts_context_data *ctxdata = ctx->data;
+       struct ffa_send_direct_data ffa_data;
+       struct tee_shm *shm = NULL;
+       struct ts_session *sess;
+       u32 req_len, ffa_args[FFA_DIRECT_REQ_ARG_NUM] = {};
+       int shm_id, rc;
+       u8 iface_id;
+       u64 handle;
+       u16 opcode;
+
+       xa_lock(&ctxdata->sess_list);
+       sess = xa_load(&ctxdata->sess_list, arg->session);
+
+       /*
+        * Do this while holding the lock to make sure that the session wasn't
+        * closed meanwhile
+        */
+       if (sess)
+               iface_id = sess->iface_id;
+
+       xa_unlock(&ctxdata->sess_list);
+       if (!sess)
+               return -EINVAL;
+
+       opcode = lower_16_bits(arg->func);
+       shm_id = lower_32_bits(param[0].u.value.a);
+       req_len = lower_32_bits(param[0].u.value.b);
+
+       if (shm_id != 0) {
+               shm = tee_shm_get_from_id(ctx, shm_id);
+               if (IS_ERR(shm))
+                       return PTR_ERR(shm);
+
+               if (shm->size < req_len) {
+                       dev_err(&ffa_dev->dev,
+                               "request doesn't fit into shared memory buffer\n");
+                       rc = -EINVAL;
+                       goto out;
+               }
+
+               handle = shm->sec_world_id;
+       } else {
+               handle = FFA_INVALID_MEM_HANDLE;
+       }
+
+       ffa_args[TS_RPC_CTRL_REG] = TS_RPC_CTRL_PACK_IFACE_OPCODE(iface_id,
+                                                                 opcode);
+       ffa_args[TS_RPC_SERVICE_MEM_HANDLE_LSW] = lower_32_bits(handle);
+       ffa_args[TS_RPC_SERVICE_MEM_HANDLE_MSW] = upper_32_bits(handle);
+       ffa_args[TS_RPC_SERVICE_REQ_LEN] = req_len;
+       ffa_args[TS_RPC_SERVICE_CLIENT_ID] = 0;
+
+       arg_list_to_ffa_data(ffa_args, &ffa_data);
+       rc = ffa_dev->ops->msg_ops->sync_send_receive(ffa_dev, &ffa_data);
+       if (rc)
+               goto out;
+
+       arg_list_from_ffa_data(&ffa_data, ffa_args);
+
+       if (ffa_args[TS_RPC_SERVICE_RPC_STATUS] != TS_RPC_OK) {
+               dev_err(&ffa_dev->dev, "invoke_func rpc status: %d\n",
+                       ffa_args[TS_RPC_SERVICE_RPC_STATUS]);
+               rc = -EINVAL;
+               goto out;
+       }
+
+       arg->ret = ffa_args[TS_RPC_SERVICE_STATUS];
+       if (shm && shm->size >= ffa_args[TS_RPC_SERVICE_RESP_LEN])
+               param[0].u.value.a = ffa_args[TS_RPC_SERVICE_RESP_LEN];
+
+out:
+       if (shm)
+               tee_shm_put(shm);
+
+       return rc;
+}
+
+static int tstee_shm_register(struct tee_context *ctx, struct tee_shm *shm,
+                             struct page **pages, size_t num_pages,
+                             unsigned long start __always_unused)
+{
+       struct tstee *tstee = tee_get_drvdata(ctx->teedev);
+       struct ffa_device *ffa_dev = tstee->ffa_dev;
+       struct ffa_mem_region_attributes mem_attr = {
+               .receiver = tstee->ffa_dev->vm_id,
+               .attrs = FFA_MEM_RW,
+               .flag = 0,
+       };
+       struct ffa_mem_ops_args mem_args = {
+               .attrs = &mem_attr,
+               .use_txbuf = true,
+               .nattrs = 1,
+               .flags = 0,
+       };
+       struct ffa_send_direct_data ffa_data;
+       struct sg_table sgt;
+       u32 ffa_args[FFA_DIRECT_REQ_ARG_NUM] = {};
+       int rc;
+
+       rc = sg_alloc_table_from_pages(&sgt, pages, num_pages, 0,
+                                      num_pages * PAGE_SIZE, GFP_KERNEL);
+       if (rc)
+               return rc;
+
+       mem_args.sg = sgt.sgl;
+       rc = ffa_dev->ops->mem_ops->memory_share(&mem_args);
+       sg_free_table(&sgt);
+       if (rc)
+               return rc;
+
+       shm->sec_world_id = mem_args.g_handle;
+
+       ffa_args[TS_RPC_CTRL_REG] =
+                       TS_RPC_CTRL_PACK_IFACE_OPCODE(TS_RPC_MGMT_IFACE_ID,
+                                                     TS_RPC_OP_RETRIEVE_MEM);
+       ffa_args[TS_RPC_RETRIEVE_MEM_HANDLE_LSW] =
+                       lower_32_bits(shm->sec_world_id);
+       ffa_args[TS_RPC_RETRIEVE_MEM_HANDLE_MSW] =
+                       upper_32_bits(shm->sec_world_id);
+       ffa_args[TS_RPC_RETRIEVE_MEM_TAG_LSW] = 0;
+       ffa_args[TS_RPC_RETRIEVE_MEM_TAG_MSW] = 0;
+
+       arg_list_to_ffa_data(ffa_args, &ffa_data);
+       rc = ffa_dev->ops->msg_ops->sync_send_receive(ffa_dev, &ffa_data);
+       if (rc) {
+               (void)ffa_dev->ops->mem_ops->memory_reclaim(shm->sec_world_id,
+                                                           0);
+               return rc;
+       }
+
+       arg_list_from_ffa_data(&ffa_data, ffa_args);
+
+       if (ffa_args[TS_RPC_RETRIEVE_MEM_RPC_STATUS] != TS_RPC_OK) {
+               dev_err(&ffa_dev->dev, "shm_register rpc status: %d\n",
+                       ffa_args[TS_RPC_RETRIEVE_MEM_RPC_STATUS]);
+               ffa_dev->ops->mem_ops->memory_reclaim(shm->sec_world_id, 0);
+               return -EINVAL;
+       }
+
+       return 0;
+}
+
+static int tstee_shm_unregister(struct tee_context *ctx, struct tee_shm *shm)
+{
+       struct tstee *tstee = tee_get_drvdata(ctx->teedev);
+       struct ffa_device *ffa_dev = tstee->ffa_dev;
+       struct ffa_send_direct_data ffa_data;
+       u32 ffa_args[FFA_DIRECT_REQ_ARG_NUM] = {};
+       int rc;
+
+       ffa_args[TS_RPC_CTRL_REG] =
+                       TS_RPC_CTRL_PACK_IFACE_OPCODE(TS_RPC_MGMT_IFACE_ID,
+                                                     TS_RPC_OP_RELINQ_MEM);
+       ffa_args[TS_RPC_RELINQ_MEM_HANDLE_LSW] =
+                       lower_32_bits(shm->sec_world_id);
+       ffa_args[TS_RPC_RELINQ_MEM_HANDLE_MSW] =
+                       upper_32_bits(shm->sec_world_id);
+
+       arg_list_to_ffa_data(ffa_args, &ffa_data);
+       rc = ffa_dev->ops->msg_ops->sync_send_receive(ffa_dev, &ffa_data);
+       if (rc)
+               return rc;
+       arg_list_from_ffa_data(&ffa_data, ffa_args);
+
+       if (ffa_args[TS_RPC_RELINQ_MEM_RPC_STATUS] != TS_RPC_OK) {
+               dev_err(&ffa_dev->dev, "shm_unregister rpc status: %d\n",
+                       ffa_args[TS_RPC_RELINQ_MEM_RPC_STATUS]);
+               return -EINVAL;
+       }
+
+       rc = ffa_dev->ops->mem_ops->memory_reclaim(shm->sec_world_id, 0);
+
+       return rc;
+}

drivers/tee/tstee/core.c line 348

Quoted 10 lines
drivers/tee/tstee/core.c new file +10 -0
+
+static const struct tee_driver_ops tstee_ops = {
+       .get_version = tstee_get_version,
+       .open = tstee_open,
+       .release = tstee_release,
+       .open_session = tstee_open_session,
+       .close_session = tstee_close_session,
+       .invoke_func = tstee_invoke_func,
+       .shm_register = tstee_shm_register,
+       .shm_unregister = tstee_shm_unregister,

...then there isn't a need to register .shm_register and .shm_unregister callbacks. But apart from that the driver is looking good to me.

With above incorporated, feel free to add:

-Sumit

Quoted 247 lines
drivers/tee/tstee/core.c new file +134 -0
+};
+
+static const struct tee_desc tstee_desc = {
+       .name = "tstee-clnt",
+       .ops = &tstee_ops,
+       .owner = THIS_MODULE,
+};
+
+static int pool_op_alloc(struct tee_shm_pool *pool, struct tee_shm *shm,
+                        size_t size, size_t align)
+{
+       return tee_dyn_shm_alloc_helper(shm, size, align, tstee_shm_register);
+}
+
+static void pool_op_free(struct tee_shm_pool *pool, struct tee_shm *shm)
+{
+       tee_dyn_shm_free_helper(shm, tstee_shm_unregister);
+}
+
+static void pool_op_destroy_pool(struct tee_shm_pool *pool)
+{
+       kfree(pool);
+}
+
+static const struct tee_shm_pool_ops pool_ops = {
+       .alloc = pool_op_alloc,
+       .free = pool_op_free,
+       .destroy_pool = pool_op_destroy_pool,
+};
+
+static struct tee_shm_pool *tstee_create_shm_pool(void)
+{
+       struct tee_shm_pool *pool = kzalloc(sizeof(*pool), GFP_KERNEL);
+
+       if (!pool)
+               return ERR_PTR(-ENOMEM);
+
+       pool->ops = &pool_ops;
+
+       return pool;
+}
+
+static bool tstee_check_rpc_compatible(struct ffa_device *ffa_dev)
+{
+       struct ffa_send_direct_data ffa_data;
+       u32 ffa_args[FFA_DIRECT_REQ_ARG_NUM] = {};
+
+       ffa_args[TS_RPC_CTRL_REG] =
+                       TS_RPC_CTRL_PACK_IFACE_OPCODE(TS_RPC_MGMT_IFACE_ID,
+                                                     TS_RPC_OP_GET_VERSION);
+
+       arg_list_to_ffa_data(ffa_args, &ffa_data);
+       if (ffa_dev->ops->msg_ops->sync_send_receive(ffa_dev, &ffa_data))
+               return false;
+
+       arg_list_from_ffa_data(&ffa_data, ffa_args);
+
+       return ffa_args[TS_RPC_GET_VERSION_RESP] == TS_RPC_PROTOCOL_VERSION;
+}
+
+static int tstee_probe(struct ffa_device *ffa_dev)
+{
+       struct tstee *tstee;
+       int rc;
+
+       ffa_dev->ops->msg_ops->mode_32bit_set(ffa_dev);
+
+       if (!tstee_check_rpc_compatible(ffa_dev))
+               return -EINVAL;
+
+       tstee = kzalloc(sizeof(*tstee), GFP_KERNEL);
+       if (!tstee)
+               return -ENOMEM;
+
+       tstee->ffa_dev = ffa_dev;
+
+       tstee->pool = tstee_create_shm_pool();
+       if (IS_ERR(tstee->pool)) {
+               rc = PTR_ERR(tstee->pool);
+               tstee->pool = NULL;
+               goto err_free_tstee;
+       }
+
+       tstee->teedev = tee_device_alloc(&tstee_desc, NULL, tstee->pool, tstee);
+       if (IS_ERR(tstee->teedev)) {
+               rc = PTR_ERR(tstee->teedev);
+               tstee->teedev = NULL;
+               goto err_free_pool;
+       }
+
+       rc = tee_device_register(tstee->teedev);
+       if (rc)
+               goto err_unreg_teedev;
+
+       ffa_dev_set_drvdata(ffa_dev, tstee);
+
+       return 0;
+
+err_unreg_teedev:
+       tee_device_unregister(tstee->teedev);
+err_free_pool:
+       tee_shm_pool_free(tstee->pool);
+err_free_tstee:
+       kfree(tstee);
+       return rc;
+}
+
+static void tstee_remove(struct ffa_device *ffa_dev)
+{
+       struct tstee *tstee = ffa_dev->dev.driver_data;
+
+       tee_device_unregister(tstee->teedev);
+       tee_shm_pool_free(tstee->pool);
+       kfree(tstee);
+}
+
+static const struct ffa_device_id tstee_device_ids[] = {
+       /* TS RPC protocol UUID: bdcd76d7-825e-4751-963b-86d4f84943ac */
+       { TS_RPC_UUID },
+       {}
+};
+
+static struct ffa_driver tstee_driver = {
+       .name = "arm_tstee",
+       .probe = tstee_probe,
+       .remove = tstee_remove,
+       .id_table = tstee_device_ids,
+};
+
+module_ffa_driver(tstee_driver);
+
+MODULE_AUTHOR("Balint Dobszay <[email protected]>");
+MODULE_DESCRIPTION("Arm Trusted Services TEE driver");
+MODULE_LICENSE("GPL");
drivers/tee/tstee/tstee_private.h new file +92 -0
new file mode 100644
index 000000000000..8e58725b57eb
@@ -0,0 +1,92 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright (c) 2023, Arm Limited
+ */
+
+#ifndef TSTEE_PRIVATE_H
+#define TSTEE_PRIVATE_H
+
+#include <linux/arm_ffa.h>
+#include <linux/bitops.h>
+#include <linux/tee_core.h>
+#include <linux/types.h>
+#include <linux/uuid.h>
+#include <linux/xarray.h>
+
+/*
+ * The description of the ABI implemented in this file is available at
+ * https://trusted-services.readthedocs.io/en/v1.0.0/developer/service-access-protocols.html#abi
+ */
+
+/* UUID of this protocol */
+#define TS_RPC_UUID UUID_INIT(0xbdcd76d7, 0x825e, 0x4751, \
+                             0x96, 0x3b, 0x86, 0xd4, 0xf8, 0x49, 0x43, 0xac)
+
+/* Protocol version*/
+#define TS_RPC_PROTOCOL_VERSION                (1)
+
+/* Status codes */
+#define TS_RPC_OK                      (0)
+
+/* RPC control register */
+#define TS_RPC_CTRL_REG                        (0)
+#define OPCODE_MASK                    GENMASK(15, 0)
+#define IFACE_ID_MASK                  GENMASK(23, 16)
+#define TS_RPC_CTRL_OPCODE(x)          ((u16)(FIELD_GET(OPCODE_MASK, (x))))
+#define TS_RPC_CTRL_IFACE_ID(x)                ((u8)(FIELD_GET(IFACE_ID_MASK, (x))))
+#define TS_RPC_CTRL_PACK_IFACE_OPCODE(i, o)    \
+       (FIELD_PREP(IFACE_ID_MASK, (i)) | FIELD_PREP(OPCODE_MASK, (o)))
+#define TS_RPC_CTRL_SAP_RC             BIT(30)
+#define TS_RPC_CTRL_SAP_ERR            BIT(31)
+
+/* Interface ID for RPC management operations */
+#define TS_RPC_MGMT_IFACE_ID           (0xff)
+
+/* Management calls */
+#define TS_RPC_OP_GET_VERSION          (0x0000)
+#define TS_RPC_GET_VERSION_RESP                (1)
+
+#define TS_RPC_OP_RETRIEVE_MEM         (0x0001)
+#define TS_RPC_RETRIEVE_MEM_HANDLE_LSW (1)
+#define TS_RPC_RETRIEVE_MEM_HANDLE_MSW (2)
+#define TS_RPC_RETRIEVE_MEM_TAG_LSW    (3)
+#define TS_RPC_RETRIEVE_MEM_TAG_MSW    (4)
+#define TS_RPC_RETRIEVE_MEM_RPC_STATUS (1)
+
+#define TS_RPC_OP_RELINQ_MEM           (0x0002)
+#define TS_RPC_RELINQ_MEM_HANDLE_LSW   (1)
+#define TS_RPC_RELINQ_MEM_HANDLE_MSW   (2)
+#define TS_RPC_RELINQ_MEM_RPC_STATUS   (1)
+
+#define TS_RPC_OP_SERVICE_INFO         (0x0003)
+#define TS_RPC_SERVICE_INFO_UUID0      (1)
+#define TS_RPC_SERVICE_INFO_UUID1      (2)
+#define TS_RPC_SERVICE_INFO_UUID2      (3)
+#define TS_RPC_SERVICE_INFO_UUID3      (4)
+#define TS_RPC_SERVICE_INFO_RPC_STATUS (1)
+#define TS_RPC_SERVICE_INFO_IFACE      (2)
+
+/* Service call */
+#define TS_RPC_SERVICE_MEM_HANDLE_LSW  (1)
+#define TS_RPC_SERVICE_MEM_HANDLE_MSW  (2)
+#define TS_RPC_SERVICE_REQ_LEN         (3)
+#define TS_RPC_SERVICE_CLIENT_ID       (4)
+#define TS_RPC_SERVICE_RPC_STATUS      (1)
+#define TS_RPC_SERVICE_STATUS          (2)
+#define TS_RPC_SERVICE_RESP_LEN                (3)
+
+struct tstee {
+       struct ffa_device *ffa_dev;
+       struct tee_device *teedev;
+       struct tee_shm_pool *pool;
+};
+
+struct ts_session {
+       u8 iface_id;
+};
+
+struct ts_context_data {
+       struct xarray sess_list;
+};
+
+#endif /* TSTEE_PRIVATE_H */
include/uapi/linux/tee.h +1 -1
index 23e57164693c..d0430bee8292 100644
@@ -56,6 +56,7 @@
  */
 #define TEE_IMPL_ID_OPTEE      1
 #define TEE_IMPL_ID_AMDTEE     2
+#define TEE_IMPL_ID_TSTEE      3
 
 /*
  * OP-TEE specific capabilities
--

2.34.1

author maintainer
#7 lore raw

Re: [PATCH v3 3/4] tee: tstee: Add Trusted Services TEE driver

Quoted 436 lines · Sumit Garg

Hi Balint,

Quoted 128 lines · Balint Dobszay

The Trusted Services project provides a framework for developing and deploying device Root of Trust services in FF-A Secure Partitions. The FF-A SPs are accessible through the FF-A driver, but this doesn't provide a user space interface. The goal of this TEE driver is to make Trusted Services SPs accessible for user space clients.

All TS SPs have the same FF-A UUID, it identifies the RPC protocol used by TS. A TS SP can host one or more services, a service is identified by its service UUID. The same type of service cannot be present twice in the same SP. During SP boot each service in an SP is assigned an interface ID, this is just a short ID to simplify message addressing. There is 1:1 mapping between TS SPs and TEE devices, i.e. a separate TEE device is registered for each TS SP. This is required since contrary to the generic TEE design where memory is shared with the whole TEE implementation, in case of FF-A, memory is shared with a specific SP. A user space client has to be able to separately share memory with each SP based on its endpoint ID.

---
 drivers/tee/Kconfig               |   1 +
 drivers/tee/Makefile              |   1 +
 drivers/tee/tstee/Kconfig         |  11 +
 drivers/tee/tstee/Makefile        |   3 +
 drivers/tee/tstee/core.c          | 482 ++++++++++++++++++++++++++++++
 drivers/tee/tstee/tstee_private.h |  92 ++++++
 include/uapi/linux/tee.h          |   1 +
 7 files changed, 591 insertions(+)
 create mode 100644 drivers/tee/tstee/Kconfig
 create mode 100644 drivers/tee/tstee/Makefile
 create mode 100644 drivers/tee/tstee/core.c
 create mode 100644 drivers/tee/tstee/tstee_private.h
drivers/tee/Kconfig +1 -0
index 73a147202e88..61b507c18780 100644
@@ -15,5 +15,6 @@ if TEE
 
 source "drivers/tee/optee/Kconfig"
 source "drivers/tee/amdtee/Kconfig"
+source "drivers/tee/tstee/Kconfig"
 
 endif
drivers/tee/Makefile +1 -0
index 68da044afbfa..5488cba30bd2 100644
@@ -5,3 +5,4 @@ tee-objs += tee_shm.o
 tee-objs += tee_shm_pool.o
 obj-$(CONFIG_OPTEE) += optee/
 obj-$(CONFIG_AMDTEE) += amdtee/
+obj-$(CONFIG_ARM_TSTEE) += tstee/
drivers/tee/tstee/Kconfig new file +11 -0
new file mode 100644
index 000000000000..d32f91d47398
@@ -0,0 +1,11 @@
+# SPDX-License-Identifier: GPL-2.0-only
+config ARM_TSTEE
+       tristate "Arm Trusted Services TEE driver"
+       depends on ARM_FFA_TRANSPORT
+       default n
+       help
+         The Trusted Services project provides a framework for developing and
+         deploying device Root of Trust services in FF-A Secure Partitions.
+         This driver provides an interface to make Trusted Services Secure
+         Partitions accessible for user space clients, since the FF-A driver
+         doesn't implement a user space interface directly.
drivers/tee/tstee/Makefile new file +3 -0
new file mode 100644
index 000000000000..5227020ebd30
@@ -0,0 +1,3 @@
+# SPDX-License-Identifier: GPL-2.0-only
+arm-tstee-objs := core.o
+obj-$(CONFIG_ARM_TSTEE) = arm-tstee.o
drivers/tee/tstee/core.c new file +42 -0
new file mode 100644
index 000000000000..577c9d52673e
@@ -0,0 +1,482 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (c) 2023, Arm Limited
+ */
+
+#include <linux/arm_ffa.h>
+#include <linux/err.h>
+#include <linux/errno.h>
+#include <linux/kernel.h>
+#include <linux/limits.h>
+#include <linux/mm.h>
+#include <linux/module.h>
+#include <linux/scatterlist.h>
+#include <linux/slab.h>
+#include <linux/tee_core.h>
+#include <linux/types.h>
+#include <linux/uuid.h>
+#include <linux/xarray.h>
+#include "tstee_private.h"
+
+#define FFA_DIRECT_REQ_ARG_NUM 5
+#define FFA_INVALID_MEM_HANDLE U64_MAX
+
+static void arg_list_to_ffa_data(const u32 *args,
+                                struct ffa_send_direct_data *data)
+{
+       data->data0 = args[0];
+       data->data1 = args[1];
+       data->data2 = args[2];
+       data->data3 = args[3];
+       data->data4 = args[4];
+}
+
+static void arg_list_from_ffa_data(const struct ffa_send_direct_data *data,
+                                  u32 *args)
+{
+       args[0] = lower_32_bits(data->data0);
+       args[1] = lower_32_bits(data->data1);
+       args[2] = lower_32_bits(data->data2);
+       args[3] = lower_32_bits(data->data3);
+       args[4] = lower_32_bits(data->data4);
+}

drivers/tee/tstee/core.c line 52

Quoted 10 lines
drivers/tee/tstee/core.c new file +10 -0
+
+static void tstee_get_version(struct tee_device *teedev,
+                             struct tee_ioctl_version_data *vers)
+{
+       struct tstee *tstee = tee_get_drvdata(teedev);
+       struct tee_ioctl_version_data v = {
+               .impl_id = TEE_IMPL_ID_TSTEE,
+               /* FF-A endpoint ID only uses the lower 16 bits */
+               .impl_caps = lower_16_bits(tstee->ffa_dev->vm_id),
+               .gen_caps = 0,

As I mentioned during the RFC review, since you aren't supporting TEE_GEN_CAP_REG_MEM capability here...

Quoted 291 lines
drivers/tee/tstee/core.c new file +291 -0
+       };
+
+       *vers = v;
+}
+
+static int tstee_open(struct tee_context *ctx)
+{
+       struct ts_context_data *ctxdata;
+
+       ctxdata = kzalloc(sizeof(*ctxdata), GFP_KERNEL);
+       if (!ctxdata)
+               return -ENOMEM;
+
+       xa_init_flags(&ctxdata->sess_list, XA_FLAGS_ALLOC);
+
+       ctx->data = ctxdata;
+
+       return 0;
+}
+
+static void tstee_release(struct tee_context *ctx)
+{
+       struct ts_context_data *ctxdata = ctx->data;
+       struct ts_session *sess;
+       unsigned long idx;
+
+       if (!ctxdata)
+               return;
+
+       xa_for_each(&ctxdata->sess_list, idx, sess) {
+               xa_erase(&ctxdata->sess_list, idx);
+               kfree(sess);
+       }
+
+       xa_destroy(&ctxdata->sess_list);
+
+       kfree(ctxdata);
+       ctx->data = NULL;
+}
+
+static int tstee_open_session(struct tee_context *ctx,
+                             struct tee_ioctl_open_session_arg *arg,
+                             struct tee_param *param __always_unused)
+{
+       struct tstee *tstee = tee_get_drvdata(ctx->teedev);
+       struct ffa_device *ffa_dev = tstee->ffa_dev;
+       struct ts_context_data *ctxdata = ctx->data;
+       struct ffa_send_direct_data ffa_data;
+       struct ts_session *sess = NULL;
+       u32 ffa_args[FFA_DIRECT_REQ_ARG_NUM] = {};
+       u32 sess_id;
+       int rc;
+
+       ffa_args[TS_RPC_CTRL_REG] =
+               TS_RPC_CTRL_PACK_IFACE_OPCODE(TS_RPC_MGMT_IFACE_ID,
+                                             TS_RPC_OP_SERVICE_INFO);
+
+       memcpy(ffa_args + TS_RPC_SERVICE_INFO_UUID0, arg->uuid, UUID_SIZE);
+
+       arg_list_to_ffa_data(ffa_args, &ffa_data);
+       rc = ffa_dev->ops->msg_ops->sync_send_receive(ffa_dev, &ffa_data);
+       if (rc)
+               return rc;
+
+       arg_list_from_ffa_data(&ffa_data, ffa_args);
+
+       if (ffa_args[TS_RPC_SERVICE_INFO_RPC_STATUS] != TS_RPC_OK)
+               return -ENODEV;
+
+       if (ffa_args[TS_RPC_SERVICE_INFO_IFACE] > U8_MAX)
+               return -EINVAL;
+
+       sess = kzalloc(sizeof(*sess), GFP_KERNEL);
+       if (!sess)
+               return -ENOMEM;
+
+       sess->iface_id = ffa_args[TS_RPC_SERVICE_INFO_IFACE];
+
+       rc = xa_alloc(&ctxdata->sess_list, &sess_id, sess, xa_limit_32b,
+                     GFP_KERNEL);
+       if (rc) {
+               kfree(sess);
+               return rc;
+       }
+
+       arg->session = sess_id;
+       arg->ret = 0;
+
+       return 0;
+}
+
+static int tstee_close_session(struct tee_context *ctx, u32 session)
+{
+       struct ts_context_data *ctxdata = ctx->data;
+       struct ts_session *sess;
+
+       /* Calls xa_lock() internally */
+       sess = xa_erase(&ctxdata->sess_list, session);
+       if (!sess)
+               return -EINVAL;
+
+       kfree(sess);
+
+       return 0;
+}
+
+static int tstee_invoke_func(struct tee_context *ctx,
+                            struct tee_ioctl_invoke_arg *arg,
+                            struct tee_param *param)
+{
+       struct tstee *tstee = tee_get_drvdata(ctx->teedev);
+       struct ffa_device *ffa_dev = tstee->ffa_dev;
+       struct ts_context_data *ctxdata = ctx->data;
+       struct ffa_send_direct_data ffa_data;
+       struct tee_shm *shm = NULL;
+       struct ts_session *sess;
+       u32 req_len, ffa_args[FFA_DIRECT_REQ_ARG_NUM] = {};
+       int shm_id, rc;
+       u8 iface_id;
+       u64 handle;
+       u16 opcode;
+
+       xa_lock(&ctxdata->sess_list);
+       sess = xa_load(&ctxdata->sess_list, arg->session);
+
+       /*
+        * Do this while holding the lock to make sure that the session wasn't
+        * closed meanwhile
+        */
+       if (sess)
+               iface_id = sess->iface_id;
+
+       xa_unlock(&ctxdata->sess_list);
+       if (!sess)
+               return -EINVAL;
+
+       opcode = lower_16_bits(arg->func);
+       shm_id = lower_32_bits(param[0].u.value.a);
+       req_len = lower_32_bits(param[0].u.value.b);
+
+       if (shm_id != 0) {
+               shm = tee_shm_get_from_id(ctx, shm_id);
+               if (IS_ERR(shm))
+                       return PTR_ERR(shm);
+
+               if (shm->size < req_len) {
+                       dev_err(&ffa_dev->dev,
+                               "request doesn't fit into shared memory buffer\n");
+                       rc = -EINVAL;
+                       goto out;
+               }
+
+               handle = shm->sec_world_id;
+       } else {
+               handle = FFA_INVALID_MEM_HANDLE;
+       }
+
+       ffa_args[TS_RPC_CTRL_REG] = TS_RPC_CTRL_PACK_IFACE_OPCODE(iface_id,
+                                                                 opcode);
+       ffa_args[TS_RPC_SERVICE_MEM_HANDLE_LSW] = lower_32_bits(handle);
+       ffa_args[TS_RPC_SERVICE_MEM_HANDLE_MSW] = upper_32_bits(handle);
+       ffa_args[TS_RPC_SERVICE_REQ_LEN] = req_len;
+       ffa_args[TS_RPC_SERVICE_CLIENT_ID] = 0;
+
+       arg_list_to_ffa_data(ffa_args, &ffa_data);
+       rc = ffa_dev->ops->msg_ops->sync_send_receive(ffa_dev, &ffa_data);
+       if (rc)
+               goto out;
+
+       arg_list_from_ffa_data(&ffa_data, ffa_args);
+
+       if (ffa_args[TS_RPC_SERVICE_RPC_STATUS] != TS_RPC_OK) {
+               dev_err(&ffa_dev->dev, "invoke_func rpc status: %d\n",
+                       ffa_args[TS_RPC_SERVICE_RPC_STATUS]);
+               rc = -EINVAL;
+               goto out;
+       }
+
+       arg->ret = ffa_args[TS_RPC_SERVICE_STATUS];
+       if (shm && shm->size >= ffa_args[TS_RPC_SERVICE_RESP_LEN])
+               param[0].u.value.a = ffa_args[TS_RPC_SERVICE_RESP_LEN];
+
+out:
+       if (shm)
+               tee_shm_put(shm);
+
+       return rc;
+}
+
+static int tstee_shm_register(struct tee_context *ctx, struct tee_shm *shm,
+                             struct page **pages, size_t num_pages,
+                             unsigned long start __always_unused)
+{
+       struct tstee *tstee = tee_get_drvdata(ctx->teedev);
+       struct ffa_device *ffa_dev = tstee->ffa_dev;
+       struct ffa_mem_region_attributes mem_attr = {
+               .receiver = tstee->ffa_dev->vm_id,
+               .attrs = FFA_MEM_RW,
+               .flag = 0,
+       };
+       struct ffa_mem_ops_args mem_args = {
+               .attrs = &mem_attr,
+               .use_txbuf = true,
+               .nattrs = 1,
+               .flags = 0,
+       };
+       struct ffa_send_direct_data ffa_data;
+       struct sg_table sgt;
+       u32 ffa_args[FFA_DIRECT_REQ_ARG_NUM] = {};
+       int rc;
+
+       rc = sg_alloc_table_from_pages(&sgt, pages, num_pages, 0,
+                                      num_pages * PAGE_SIZE, GFP_KERNEL);
+       if (rc)
+               return rc;
+
+       mem_args.sg = sgt.sgl;
+       rc = ffa_dev->ops->mem_ops->memory_share(&mem_args);
+       sg_free_table(&sgt);
+       if (rc)
+               return rc;
+
+       shm->sec_world_id = mem_args.g_handle;
+
+       ffa_args[TS_RPC_CTRL_REG] =
+                       TS_RPC_CTRL_PACK_IFACE_OPCODE(TS_RPC_MGMT_IFACE_ID,
+                                                     TS_RPC_OP_RETRIEVE_MEM);
+       ffa_args[TS_RPC_RETRIEVE_MEM_HANDLE_LSW] =
+                       lower_32_bits(shm->sec_world_id);
+       ffa_args[TS_RPC_RETRIEVE_MEM_HANDLE_MSW] =
+                       upper_32_bits(shm->sec_world_id);
+       ffa_args[TS_RPC_RETRIEVE_MEM_TAG_LSW] = 0;
+       ffa_args[TS_RPC_RETRIEVE_MEM_TAG_MSW] = 0;
+
+       arg_list_to_ffa_data(ffa_args, &ffa_data);
+       rc = ffa_dev->ops->msg_ops->sync_send_receive(ffa_dev, &ffa_data);
+       if (rc) {
+               (void)ffa_dev->ops->mem_ops->memory_reclaim(shm->sec_world_id,
+                                                           0);
+               return rc;
+       }
+
+       arg_list_from_ffa_data(&ffa_data, ffa_args);
+
+       if (ffa_args[TS_RPC_RETRIEVE_MEM_RPC_STATUS] != TS_RPC_OK) {
+               dev_err(&ffa_dev->dev, "shm_register rpc status: %d\n",
+                       ffa_args[TS_RPC_RETRIEVE_MEM_RPC_STATUS]);
+               ffa_dev->ops->mem_ops->memory_reclaim(shm->sec_world_id, 0);
+               return -EINVAL;
+       }
+
+       return 0;
+}
+
+static int tstee_shm_unregister(struct tee_context *ctx, struct tee_shm *shm)
+{
+       struct tstee *tstee = tee_get_drvdata(ctx->teedev);
+       struct ffa_device *ffa_dev = tstee->ffa_dev;
+       struct ffa_send_direct_data ffa_data;
+       u32 ffa_args[FFA_DIRECT_REQ_ARG_NUM] = {};
+       int rc;
+
+       ffa_args[TS_RPC_CTRL_REG] =
+                       TS_RPC_CTRL_PACK_IFACE_OPCODE(TS_RPC_MGMT_IFACE_ID,
+                                                     TS_RPC_OP_RELINQ_MEM);
+       ffa_args[TS_RPC_RELINQ_MEM_HANDLE_LSW] =
+                       lower_32_bits(shm->sec_world_id);
+       ffa_args[TS_RPC_RELINQ_MEM_HANDLE_MSW] =
+                       upper_32_bits(shm->sec_world_id);
+
+       arg_list_to_ffa_data(ffa_args, &ffa_data);
+       rc = ffa_dev->ops->msg_ops->sync_send_receive(ffa_dev, &ffa_data);
+       if (rc)
+               return rc;
+       arg_list_from_ffa_data(&ffa_data, ffa_args);
+
+       if (ffa_args[TS_RPC_RELINQ_MEM_RPC_STATUS] != TS_RPC_OK) {
+               dev_err(&ffa_dev->dev, "shm_unregister rpc status: %d\n",
+                       ffa_args[TS_RPC_RELINQ_MEM_RPC_STATUS]);
+               return -EINVAL;
+       }
+
+       rc = ffa_dev->ops->mem_ops->memory_reclaim(shm->sec_world_id, 0);
+
+       return rc;
+}
+
+static const struct tee_driver_ops tstee_ops = {
+       .get_version = tstee_get_version,
+       .open = tstee_open,
+       .release = tstee_release,
Quoted 8 lines

drivers/tee/tstee/core.c line 348

Quoted 5 lines
drivers/tee/tstee/core.c new file +5 -0
+       .open_session = tstee_open_session,
+       .close_session = tstee_close_session,
+       .invoke_func = tstee_invoke_func,
+       .shm_register = tstee_shm_register,
+       .shm_unregister = tstee_shm_unregister,

...then there isn't a need to register .shm_register and .shm_unregister callbacks.

You're right, I missed that. I'll remove these callbacks.

Quoted 7 lines

But apart from that the driver is looking
good to me.

With above incorporated, feel free to add:

Okay, thanks.

Regards,
Balint

Quoted 247 lines
Quoted 247 lines
drivers/tee/tstee/core.c new file +134 -0
+};
+
+static const struct tee_desc tstee_desc = {
+       .name = "tstee-clnt",
+       .ops = &tstee_ops,
+       .owner = THIS_MODULE,
+};
+
+static int pool_op_alloc(struct tee_shm_pool *pool, struct tee_shm *shm,
+                        size_t size, size_t align)
+{
+       return tee_dyn_shm_alloc_helper(shm, size, align, tstee_shm_register);
+}
+
+static void pool_op_free(struct tee_shm_pool *pool, struct tee_shm *shm)
+{
+       tee_dyn_shm_free_helper(shm, tstee_shm_unregister);
+}
+
+static void pool_op_destroy_pool(struct tee_shm_pool *pool)
+{
+       kfree(pool);
+}
+
+static const struct tee_shm_pool_ops pool_ops = {
+       .alloc = pool_op_alloc,
+       .free = pool_op_free,
+       .destroy_pool = pool_op_destroy_pool,
+};
+
+static struct tee_shm_pool *tstee_create_shm_pool(void)
+{
+       struct tee_shm_pool *pool = kzalloc(sizeof(*pool), GFP_KERNEL);
+
+       if (!pool)
+               return ERR_PTR(-ENOMEM);
+
+       pool->ops = &pool_ops;
+
+       return pool;
+}
+
+static bool tstee_check_rpc_compatible(struct ffa_device *ffa_dev)
+{
+       struct ffa_send_direct_data ffa_data;
+       u32 ffa_args[FFA_DIRECT_REQ_ARG_NUM] = {};
+
+       ffa_args[TS_RPC_CTRL_REG] =
+                       TS_RPC_CTRL_PACK_IFACE_OPCODE(TS_RPC_MGMT_IFACE_ID,
+                                                     TS_RPC_OP_GET_VERSION);
+
+       arg_list_to_ffa_data(ffa_args, &ffa_data);
+       if (ffa_dev->ops->msg_ops->sync_send_receive(ffa_dev, &ffa_data))
+               return false;
+
+       arg_list_from_ffa_data(&ffa_data, ffa_args);
+
+       return ffa_args[TS_RPC_GET_VERSION_RESP] == TS_RPC_PROTOCOL_VERSION;
+}
+
+static int tstee_probe(struct ffa_device *ffa_dev)
+{
+       struct tstee *tstee;
+       int rc;
+
+       ffa_dev->ops->msg_ops->mode_32bit_set(ffa_dev);
+
+       if (!tstee_check_rpc_compatible(ffa_dev))
+               return -EINVAL;
+
+       tstee = kzalloc(sizeof(*tstee), GFP_KERNEL);
+       if (!tstee)
+               return -ENOMEM;
+
+       tstee->ffa_dev = ffa_dev;
+
+       tstee->pool = tstee_create_shm_pool();
+       if (IS_ERR(tstee->pool)) {
+               rc = PTR_ERR(tstee->pool);
+               tstee->pool = NULL;
+               goto err_free_tstee;
+       }
+
+       tstee->teedev = tee_device_alloc(&tstee_desc, NULL, tstee->pool, tstee);
+       if (IS_ERR(tstee->teedev)) {
+               rc = PTR_ERR(tstee->teedev);
+               tstee->teedev = NULL;
+               goto err_free_pool;
+       }
+
+       rc = tee_device_register(tstee->teedev);
+       if (rc)
+               goto err_unreg_teedev;
+
+       ffa_dev_set_drvdata(ffa_dev, tstee);
+
+       return 0;
+
+err_unreg_teedev:
+       tee_device_unregister(tstee->teedev);
+err_free_pool:
+       tee_shm_pool_free(tstee->pool);
+err_free_tstee:
+       kfree(tstee);
+       return rc;
+}
+
+static void tstee_remove(struct ffa_device *ffa_dev)
+{
+       struct tstee *tstee = ffa_dev->dev.driver_data;
+
+       tee_device_unregister(tstee->teedev);
+       tee_shm_pool_free(tstee->pool);
+       kfree(tstee);
+}
+
+static const struct ffa_device_id tstee_device_ids[] = {
+       /* TS RPC protocol UUID: bdcd76d7-825e-4751-963b-86d4f84943ac */
+       { TS_RPC_UUID },
+       {}
+};
+
+static struct ffa_driver tstee_driver = {
+       .name = "arm_tstee",
+       .probe = tstee_probe,
+       .remove = tstee_remove,
+       .id_table = tstee_device_ids,
+};
+
+module_ffa_driver(tstee_driver);
+
+MODULE_AUTHOR("Balint Dobszay <[email protected]>");
+MODULE_DESCRIPTION("Arm Trusted Services TEE driver");
+MODULE_LICENSE("GPL");
drivers/tee/tstee/tstee_private.h new file +92 -0
new file mode 100644
index 000000000000..8e58725b57eb
@@ -0,0 +1,92 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright (c) 2023, Arm Limited
+ */
+
+#ifndef TSTEE_PRIVATE_H
+#define TSTEE_PRIVATE_H
+
+#include <linux/arm_ffa.h>
+#include <linux/bitops.h>
+#include <linux/tee_core.h>
+#include <linux/types.h>
+#include <linux/uuid.h>
+#include <linux/xarray.h>
+
+/*
+ * The description of the ABI implemented in this file is available at
+ * https://trusted-services.readthedocs.io/en/v1.0.0/developer/service-access-protocols.html#abi
+ */
+
+/* UUID of this protocol */
+#define TS_RPC_UUID UUID_INIT(0xbdcd76d7, 0x825e, 0x4751, \
+                             0x96, 0x3b, 0x86, 0xd4, 0xf8, 0x49, 0x43, 0xac)
+
+/* Protocol version*/
+#define TS_RPC_PROTOCOL_VERSION                (1)
+
+/* Status codes */
+#define TS_RPC_OK                      (0)
+
+/* RPC control register */
+#define TS_RPC_CTRL_REG                        (0)
+#define OPCODE_MASK                    GENMASK(15, 0)
+#define IFACE_ID_MASK                  GENMASK(23, 16)
+#define TS_RPC_CTRL_OPCODE(x)          ((u16)(FIELD_GET(OPCODE_MASK, (x))))
+#define TS_RPC_CTRL_IFACE_ID(x)                ((u8)(FIELD_GET(IFACE_ID_MASK, (x))))
+#define TS_RPC_CTRL_PACK_IFACE_OPCODE(i, o)    \
+       (FIELD_PREP(IFACE_ID_MASK, (i)) | FIELD_PREP(OPCODE_MASK, (o)))
+#define TS_RPC_CTRL_SAP_RC             BIT(30)
+#define TS_RPC_CTRL_SAP_ERR            BIT(31)
+
+/* Interface ID for RPC management operations */
+#define TS_RPC_MGMT_IFACE_ID           (0xff)
+
+/* Management calls */
+#define TS_RPC_OP_GET_VERSION          (0x0000)
+#define TS_RPC_GET_VERSION_RESP                (1)
+
+#define TS_RPC_OP_RETRIEVE_MEM         (0x0001)
+#define TS_RPC_RETRIEVE_MEM_HANDLE_LSW (1)
+#define TS_RPC_RETRIEVE_MEM_HANDLE_MSW (2)
+#define TS_RPC_RETRIEVE_MEM_TAG_LSW    (3)
+#define TS_RPC_RETRIEVE_MEM_TAG_MSW    (4)
+#define TS_RPC_RETRIEVE_MEM_RPC_STATUS (1)
+
+#define TS_RPC_OP_RELINQ_MEM           (0x0002)
+#define TS_RPC_RELINQ_MEM_HANDLE_LSW   (1)
+#define TS_RPC_RELINQ_MEM_HANDLE_MSW   (2)
+#define TS_RPC_RELINQ_MEM_RPC_STATUS   (1)
+
+#define TS_RPC_OP_SERVICE_INFO         (0x0003)
+#define TS_RPC_SERVICE_INFO_UUID0      (1)
+#define TS_RPC_SERVICE_INFO_UUID1      (2)
+#define TS_RPC_SERVICE_INFO_UUID2      (3)
+#define TS_RPC_SERVICE_INFO_UUID3      (4)
+#define TS_RPC_SERVICE_INFO_RPC_STATUS (1)
+#define TS_RPC_SERVICE_INFO_IFACE      (2)
+
+/* Service call */
+#define TS_RPC_SERVICE_MEM_HANDLE_LSW  (1)
+#define TS_RPC_SERVICE_MEM_HANDLE_MSW  (2)
+#define TS_RPC_SERVICE_REQ_LEN         (3)
+#define TS_RPC_SERVICE_CLIENT_ID       (4)
+#define TS_RPC_SERVICE_RPC_STATUS      (1)
+#define TS_RPC_SERVICE_STATUS          (2)
+#define TS_RPC_SERVICE_RESP_LEN                (3)
+
+struct tstee {
+       struct ffa_device *ffa_dev;
+       struct tee_device *teedev;
+       struct tee_shm_pool *pool;
+};
+
+struct ts_session {
+       u8 iface_id;
+};
+
+struct ts_context_data {
+       struct xarray sess_list;
+};
+
+#endif /* TSTEE_PRIVATE_H */
include/uapi/linux/tee.h +1 -1
index 23e57164693c..d0430bee8292 100644
@@ -56,6 +56,7 @@
  */
 #define TEE_IMPL_ID_OPTEE      1
 #define TEE_IMPL_ID_AMDTEE     2
+#define TEE_IMPL_ID_TSTEE      3
 
 /*
  * OP-TEE specific capabilities
--

2.34.1

author maintainer
#8 lore raw

PATCH v3 4/4 Documentation: tee: Add TS-TEE driver

In reply to Balint Dobszay

Add documentation for the Trusted Services TEE driver.

---
 Documentation/tee/index.rst  |  1 +
 Documentation/tee/ts-tee.rst | 71 ++++++++++++++++++++++++++++++++++++
 2 files changed, 72 insertions(+)
 create mode 100644 Documentation/tee/ts-tee.rst
Documentation/tee/index.rst +1 -0
index a23bd08847e5..4be6e69d7837 100644
@@ -10,6 +10,7 @@ TEE Subsystem
    tee
    op-tee
    amd-tee
+   ts-tee
 
 .. only::  subproject and html
 
Documentation/tee/ts-tee.rst new file +71 -0

71 lines not on this page · raw message

Signature · 1 line
2.34.1
#9 lore raw
In reply to Balint Dobszay

Hi Balint,

Quoted 15 lines · Balint Dobszay

This series introduces a TEE driver for Trusted Services [1].

Trusted Services is a TrustedFirmware.org project that provides a framework for developing and deploying device Root of Trust services in FF-A [2] Secure Partitions. The project hosts the reference implementation of Arm Platform Security Architecture [3] for Arm A-profile devices.

The FF-A Secure Partitions are accessible through the FF-A driver in Linux. However, the FF-A driver doesn't have a user space interface so user space clients currently cannot access Trusted Services. The goal of this TEE driver is to bridge this gap and make Trusted Services functionality accessible from user space.

Quoted 9 lines

Changelog:
v2[6] -> v3:
- Add patch "tee: Refactor TEE subsystem header files" from Sumit
- Remove unnecessary includes from core.c
- Remove the mutex from "struct ts_context_data" since the same mechanism could be implemented by reusing the XArray's internal lock
- Rename tee_shm_pool_op_*_helper functions as suggested by Sumit
- Replace pr_* with dev_* as previously suggested by Krzysztof

I will also suggest you to add a maintainer's entry for this new Trusted Services TEE driver.

-Sumit

Quoted 72 lines

v1[5] -> v2:
- Refactor session handling to use XArray instead of IDR and linked list (the linked list was redundant as pointed out by Jens, and IDR is now deprecated in favor of XArray)
- Refactor tstee_probe() to not call tee_device_unregister() before calling tee_device_register()
- Address comments from Krzysztof and Jens
- Address documentation comments from Randy
- Use module_ffa_driver() macro instead of separate module init / exit functions
- Reformat max line length 100 -> 80

RFC[4] -> v1:
- Add patch for moving pool_op helper functions to the TEE subsystem, as suggested by Jens
- Address comments from Sumit, add patch for documentation

[1] https://www.trustedfirmware.org/projects/trusted-services/
[2] https://developer.arm.com/documentation/den0077/
[3] https://www.arm.com/architecture/security-features/platform-security
[4] https://lore.kernel.org/linux-arm-kernel/20230927152145.111777-1-balint.dobszay(a)arm.com/
[5] https://lore.kernel.org/lkml/20240213145239.379875-1-balint.dobszay(a)arm.com/
[6] https://lore.kernel.org/lkml/20240223095133.109046-1-balint.dobszay(a)arm.com/

Balint Dobszay (3):
tee: optee: Move pool_op helper functions
tee: tstee: Add Trusted Services TEE driver
Documentation: tee: Add TS-TEE driver

Sumit Garg (1):
tee: Refactor TEE subsystem header files

 Documentation/tee/index.rst         |   1 +
 Documentation/tee/ts-tee.rst        |  71 ++++
 MAINTAINERS                         |   1 +
 drivers/tee/Kconfig                 |   1 +
 drivers/tee/Makefile                |   1 +
 drivers/tee/amdtee/amdtee_private.h |   2 +-
 drivers/tee/amdtee/call.c           |   2 +-
 drivers/tee/amdtee/core.c           |   3 +-
 drivers/tee/amdtee/shm_pool.c       |   2 +-
 drivers/tee/optee/call.c            |   2 +-
 drivers/tee/optee/core.c            |  66 +---
 drivers/tee/optee/device.c          |   2 +-
 drivers/tee/optee/ffa_abi.c         |   8 +-
 drivers/tee/optee/notif.c           |   2 +-
 drivers/tee/optee/optee_private.h   |  14 +-
 drivers/tee/optee/rpc.c             |   2 +-
 drivers/tee/optee/smc_abi.c         |  11 +-
 drivers/tee/tee_core.c              |   2 +-
 drivers/tee/tee_private.h           |  35 --
 drivers/tee/tee_shm.c               |  66 +++-
 drivers/tee/tee_shm_pool.c          |   2 +-
 drivers/tee/tstee/Kconfig           |  11 +
 drivers/tee/tstee/Makefile          |   3 +
 drivers/tee/tstee/core.c            | 482 ++++++++++++++++++++++++++++
 drivers/tee/tstee/tstee_private.h   |  92 ++++++
 include/linux/tee_core.h            | 306 ++++++++++++++++++
 include/linux/tee_drv.h             | 285 ++--------------
 include/uapi/linux/tee.h            |   1 +
 28 files changed, 1087 insertions(+), 389 deletions(-)
 create mode 100644 Documentation/tee/ts-tee.rst
 create mode 100644 drivers/tee/tstee/Kconfig
 create mode 100644 drivers/tee/tstee/Makefile
 create mode 100644 drivers/tee/tstee/core.c
 create mode 100644 drivers/tee/tstee/tstee_private.h
 create mode 100644 include/linux/tee_core.h

--
2.34.1

author maintainer
#10 lore raw

Hi Sumit,

Quoted 28 lines · Sumit Garg

Hi Balint,

Quoted 15 lines · Balint Dobszay

This series introduces a TEE driver for Trusted Services [1].

Trusted Services is a TrustedFirmware.org project that provides a framework for developing and deploying device Root of Trust services in FF-A [2] Secure Partitions. The project hosts the reference implementation of Arm Platform Security Architecture [3] for Arm A-profile devices.

The FF-A Secure Partitions are accessible through the FF-A driver in Linux. However, the FF-A driver doesn't have a user space interface so user space clients currently cannot access Trusted Services. The goal of this TEE driver is to bridge this gap and make Trusted Services functionality accessible from user space.

Quoted 9 lines

Changelog:
v2[6] -> v3:
- Add patch "tee: Refactor TEE subsystem header files" from Sumit
- Remove unnecessary includes from core.c
- Remove the mutex from "struct ts_context_data" since the same mechanism could be implemented by reusing the XArray's internal lock
- Rename tee_shm_pool_op_*_helper functions as suggested by Sumit
- Replace pr_* with dev_* as previously suggested by Krzysztof

Quoted 2 lines

I will also suggest you to add a maintainer's entry for this new Trusted Services TEE driver.

Sure, will do. I plan to post v4 next week.

Regards,
Balint

Quoted 72 lines
Quoted 72 lines

v1[5] -> v2:
- Refactor session handling to use XArray instead of IDR and linked list (the linked list was redundant as pointed out by Jens, and IDR is now deprecated in favor of XArray)
- Refactor tstee_probe() to not call tee_device_unregister() before calling tee_device_register()
- Address comments from Krzysztof and Jens
- Address documentation comments from Randy
- Use module_ffa_driver() macro instead of separate module init / exit functions
- Reformat max line length 100 -> 80

RFC[4] -> v1:
- Add patch for moving pool_op helper functions to the TEE subsystem, as suggested by Jens
- Address comments from Sumit, add patch for documentation

[1] https://www.trustedfirmware.org/projects/trusted-services/
[2] https://developer.arm.com/documentation/den0077/
[3] https://www.arm.com/architecture/security-features/platform-security
[4] https://lore.kernel.org/linux-arm-kernel/20230927152145.111777-1-balint.dobszay(a)arm.com/
[5] https://lore.kernel.org/lkml/20240213145239.379875-1-balint.dobszay(a)arm.com/
[6] https://lore.kernel.org/lkml/20240223095133.109046-1-balint.dobszay(a)arm.com/

Balint Dobszay (3):
tee: optee: Move pool_op helper functions
tee: tstee: Add Trusted Services TEE driver
Documentation: tee: Add TS-TEE driver

Sumit Garg (1):
tee: Refactor TEE subsystem header files

 Documentation/tee/index.rst         |   1 +
 Documentation/tee/ts-tee.rst        |  71 ++++
 MAINTAINERS                         |   1 +
 drivers/tee/Kconfig                 |   1 +
 drivers/tee/Makefile                |   1 +
 drivers/tee/amdtee/amdtee_private.h |   2 +-
 drivers/tee/amdtee/call.c           |   2 +-
 drivers/tee/amdtee/core.c           |   3 +-
 drivers/tee/amdtee/shm_pool.c       |   2 +-
 drivers/tee/optee/call.c            |   2 +-
 drivers/tee/optee/core.c            |  66 +---
 drivers/tee/optee/device.c          |   2 +-
 drivers/tee/optee/ffa_abi.c         |   8 +-
 drivers/tee/optee/notif.c           |   2 +-
 drivers/tee/optee/optee_private.h   |  14 +-
 drivers/tee/optee/rpc.c             |   2 +-
 drivers/tee/optee/smc_abi.c         |  11 +-
 drivers/tee/tee_core.c              |   2 +-
 drivers/tee/tee_private.h           |  35 --
 drivers/tee/tee_shm.c               |  66 +++-
 drivers/tee/tee_shm_pool.c          |   2 +-
 drivers/tee/tstee/Kconfig           |  11 +
 drivers/tee/tstee/Makefile          |   3 +
 drivers/tee/tstee/core.c            | 482 ++++++++++++++++++++++++++++
 drivers/tee/tstee/tstee_private.h   |  92 ++++++
 include/linux/tee_core.h            | 306 ++++++++++++++++++
 include/linux/tee_drv.h             | 285 ++--------------
 include/uapi/linux/tee.h            |   1 +
 28 files changed, 1087 insertions(+), 389 deletions(-)
 create mode 100644 Documentation/tee/ts-tee.rst
 create mode 100644 drivers/tee/tstee/Kconfig
 create mode 100644 drivers/tee/tstee/Makefile
 create mode 100644 drivers/tee/tstee/core.c
 create mode 100644 drivers/tee/tstee/tstee_private.h
 create mode 100644 include/linux/tee_core.h

--
2.34.1