Skip to contentSkip to conversation

xen/arm: ffa: Improvements and fixes

PATCH Superseded Bertrand Marquis 2 participants 35 messages Oct 16, 2024 – Oct 24, 2024

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover xen/arm: ffa: Improvements and fixes

This serie contains various patches to rework how firmware discovery and feature detection is done and allow to have a more fine granular filtering of the calls we do or not to the firmware.

There is also a patch introducing the use of the "bit 15" convention from the FF-A specification to distinguish between secure and non-secure identifiers as Xen VM IDs cannot have bit 15 set.

Finally we introduce support for indirect messages and for that we transmit the RXTX buffers to the SPMC and we put the message related functions into their own source file.

Changes in v2:
- do not activate FF-A if firmware does not support i
- various clean up and small fixes explained in each patch
- rebase on top of latest staging

Bertrand Marquis (10):
xen/arm: ffa: Rework firmware discovery xen/arm: ffa: Rework feature discovery xen/arm: ffa: Fix version negotiation xen/arm: ffa: Fine granular call support xen/arm: ffa: Rework partition info get xen/arm: ffa: Use bit 15 convention for SPs xen/arm: ffa: Transmit RXTX buffers to the SPMC xen/arm: ffa: move message function into ffa_msg.c xen/arm: ffa: Remove per VM notif_enabled xen/arm: ffa: Add indirect message support

 xen/arch/arm/tee/Makefile       |   1 +
 xen/arch/arm/tee/ffa.c          | 251 +++++++++++++++-----------------
 xen/arch/arm/tee/ffa_msg.c      | 129 ++++++++++++++++
 xen/arch/arm/tee/ffa_notif.c    |  17 +--
 xen/arch/arm/tee/ffa_partinfo.c | 230 ++++++++++++++++++++---------
 xen/arch/arm/tee/ffa_private.h  |  79 ++++++++--
 xen/arch/arm/tee/ffa_rxtx.c     | 162 +++++++++++++++++----
 xen/arch/arm/tee/ffa_shm.c      |  39 +++--
 8 files changed, 642 insertions(+), 266 deletions(-)
 create mode 100644 xen/arch/arm/tee/ffa_msg.c
Signature · 1 line
2.47.0
#8 lore raw

PATCH v2 3/10 xen/arm: ffa: Fix version negotiation

Fix FFA version negotiation with the firmware to follow the specification guidance more closely (see FF-A Specification Version 1.1 in chapter 13.2.1).
When the firmware returns OK we can have several cases:
- the version requested is accepted but the firmware supports a greater one in the same major.
- the firmware supports a greater major version. It could still return OK even if the version requested is not accepted. Reject it.
- the firmware supports a lower version. It will return OK and give that version. Check if we support it and use it or reject it if we do not.

Adapt the code to:
- reject any version lower than the one we support or not with the same major version
- use the version returned if in our supported range (currently 1.1 only)
- use 1.1 if the version returned is greater.

Also adapt the handling of version requests from VM:
- use our version if same major but greater minor is requested
- use requested version if same major but lower minor is requested
- do not use if incompatible major is requested
- always return our version without error to the requester

[1] https://developer.arm.com/documentation/den0077/e/

---
Changes in v2:
- add link in commit message and code to the version and chapter of the spec.
- fix comment coding style
- introduce macros to get the major and minor of a version
- add BUILD_BUG_ON to validate that the SPMC version we want is compatible with our own version
- rework version testing and selecting to be clearer by discarding different major or version lower than what we want and then selecting based on the minor version.
- fix get_version handling to be more generic

---
 xen/arch/arm/tee/ffa.c         | 54 +++++++++++++++++++++++++---------
 xen/arch/arm/tee/ffa_private.h |  3 ++
 2 files changed, 43 insertions(+), 14 deletions(-)
xen/arch/arm/tee/ffa.c +40 -14
index dde932422ecf..1ee6b2895e92 100644
@@ -148,13 +148,20 @@ static void handle_version(struct cpu_user_regs *regs)
     struct ffa_ctx *ctx = d->arch.tee;
     uint32_t vers = get_user_reg(regs, 1);
 
-    if ( vers < FFA_VERSION_1_1 )
-        vers = FFA_VERSION_1_0;
-    else
-        vers = FFA_VERSION_1_1;
-
-    ctx->guest_vers = vers;
-    ffa_set_regs(regs, vers, 0, 0, 0, 0, 0, 0, 0);
+    /*
+     * Guest will use the version it requested if it is our major and minor
+     * lower or equals to ours. If the minor is greater, our version will be
+     * used.
+     * In any case return our version to the caller.
+     */
+    if ( FFA_VERSION_MAJOR(vers) == FFA_MY_VERSION_MAJOR )
+    {
+        if ( FFA_VERSION_MINOR(vers) > FFA_MY_VERSION_MINOR )
+            ctx->guest_vers = FFA_MY_VERSION;
+        else
+            ctx->guest_vers = vers;
+    }
+    ffa_set_regs(regs, FFA_MY_VERSION, 0, 0, 0, 0, 0, 0, 0);
 }
 
 static void handle_msg_send_direct_req(struct cpu_user_regs *regs, uint32_t fid)
@@ -537,18 +544,39 @@ static bool ffa_probe(void)
         goto err_no_fw;
     }
 
-    if ( vers < FFA_MIN_SPMC_VERSION || vers > FFA_MY_VERSION )
+    /* Some sanity check in case we update the version we support */
+    BUILD_BUG_ON(FFA_MIN_SPMC_VERSION > FFA_MY_VERSION);
+    BUILD_BUG_ON(FFA_VERSION_MAJOR(FFA_MIN_SPMC_VERSION) !=
+                                   FFA_MY_VERSION_MAJOR);
+
+    major_vers = FFA_VERSION_MAJOR(vers);
+    minor_vers = FFA_VERSION_MINOR(vers);
+
+    if ( major_vers != FFA_MY_VERSION_MAJOR ||
+         minor_vers < FFA_VERSION_MINOR(FFA_MIN_SPMC_VERSION) )
     {
-        printk(XENLOG_ERR "ffa: Incompatible version %#x found\n", vers);
+        printk(XENLOG_ERR "ffa: Incompatible firmware version %u.%u\n",
+               major_vers, minor_vers);
         goto err_no_fw;
     }
 
-    major_vers = (vers >> FFA_VERSION_MAJOR_SHIFT)
-                 & FFA_VERSION_MAJOR_MASK;
-    minor_vers = vers & FFA_VERSION_MINOR_MASK;
     printk(XENLOG_INFO "ARM FF-A Firmware version %u.%u\n",
            major_vers, minor_vers);
 
+    /*
+     * If the call succeed and the version returned is higher or equal to
+     * the one Xen requested, the version requested by Xen will be the one
+     * used. If the version returned is lower but compatible with Xen, Xen
+     * will use that version instead.
+     * A version with a different major or lower than the minimum version
+     * we support is rejected before.
+     * See https://developer.arm.com/documentation/den0077/e/ chapter 13.2.1
+     */
+    if ( minor_vers > FFA_MY_VERSION_MINOR )
+        ffa_fw_version = FFA_MY_VERSION;
+    else
+        ffa_fw_version = vers;
+
     /*
      * At the moment domains must support the same features used by Xen.
      * TODO: Rework the code to allow domain to use a subset of the
@@ -564,8 +592,6 @@ static bool ffa_probe(void)
         }
     }
 
-    ffa_fw_version = vers;
-
     if ( !ffa_rxtx_init() )
     {
         printk(XENLOG_ERR "ffa: Error during RXTX buffer init\n");
xen/arch/arm/tee/ffa_private.h +3 -0
index 7c6b06f686fc..045d9c4a0b56 100644
@@ -35,6 +35,9 @@
 #define MAKE_FFA_VERSION(major, minor)  \
         ((((major) & FFA_VERSION_MAJOR_MASK) << FFA_VERSION_MAJOR_SHIFT) | \
          ((minor) & FFA_VERSION_MINOR_MASK))
+#define FFA_VERSION_MAJOR(vers) (((vers) >> FFA_VERSION_MAJOR_SHIFT) & \
+                                 FFA_VERSION_MAJOR_MASK)
+#define FFA_VERSION_MINOR(vers) ((vers) & FFA_VERSION_MINOR_MASK)
 
 #define FFA_VERSION_1_0         MAKE_FFA_VERSION(1, 0)
 #define FFA_VERSION_1_1         MAKE_FFA_VERSION(1, 1)
Signature · 1 line
2.47.0
#10 lore raw

PATCH v2 4/10 xen/arm: ffa: Fine granular call support

In reply to Bertrand Marquis

Create a bitmap to store which feature is supported or not by the firmware and use it to filter which calls are done to the firmware.

While there reoder ABI definition by numbers to easily find the min and max ones.

---
Changes in v2:
- rename fw_feat to abi and macros to FFA_ABI to be coherent with the abi needed change done before
- rework the macros to be simpler by directly defining MIN and MAX using only Function ids
- check that requested function ids do not go over the bitmap size in ffa_fw_supports_fid
- add an ASSERT to make sure that we do not try to set bits outside of the bitmap
- turn off FF-A if there is not firmware support and adapt the commit message to reflect this
- add a compile time check that FFA_ABI_MIN < FFA_ABI_MAX
- remove spurious line removal
- restore proper cleanup of rxtx init in case of error
- reorder ABI by numbers

---
 xen/arch/arm/tee/ffa.c          | 28 +++++++++++++++---------
 xen/arch/arm/tee/ffa_notif.c    |  7 ++++++
 xen/arch/arm/tee/ffa_partinfo.c | 30 +++++++++++++++++++++++++-
 xen/arch/arm/tee/ffa_private.h  | 38 ++++++++++++++++++++++++++++-----
 xen/arch/arm/tee/ffa_rxtx.c     |  4 ++++
 xen/arch/arm/tee/ffa_shm.c      | 12 +++++++++++
 6 files changed, 103 insertions(+), 16 deletions(-)
xen/arch/arm/tee/ffa.c +18 -10
index 1ee6b2895e92..267d4435ac08 100644
@@ -72,7 +72,10 @@
 #include "ffa_private.h"
 
 /* Negotiated FF-A version to use with the SPMC, 0 if not there or supported */
-static uint32_t __ro_after_init ffa_fw_version;
+uint32_t __ro_after_init ffa_fw_version;
+
+/* Features supported by the SPMC or secure world when present */
+DECLARE_BITMAP(ffa_fw_abi_supported, FFA_ABI_BITMAP_SIZE);
 
 struct ffa_fw_abi {
     const uint32_t id;
@@ -177,6 +180,13 @@ static void handle_msg_send_direct_req(struct cpu_user_regs *regs, uint32_t fid)
     else
         mask = GENMASK_ULL(31, 0);
 
+    if ( !ffa_fw_supports_fid(fid) )
+    {
+        resp.a0 = FFA_ERROR;
+        resp.a2 = FFA_RET_NOT_SUPPORTED;
+        goto out;
+    }
+
     src_dst = get_user_reg(regs, 1);
     if ( (src_dst >> 16) != ffa_get_vm_id(d) )
     {
@@ -577,19 +587,16 @@ static bool ffa_probe(void)
     else
         ffa_fw_version = vers;
 
-    /*
-     * At the moment domains must support the same features used by Xen.
-     * TODO: Rework the code to allow domain to use a subset of the
-     * features supported.
-     */
     for ( unsigned int i = 0; i < ARRAY_SIZE(ffa_fw_abi_needed); i++ )
     {
-        if ( !ffa_abi_supported(ffa_fw_abi_needed[i].id) )
-        {
+        ASSERT(FFA_ABI_BITNUM(ffa_fw_abi_needed[i].id) < FFA_ABI_BITMAP_SIZE);
+
+        if ( ffa_abi_supported(ffa_fw_abi_needed[i].id) )
+            set_bit(FFA_ABI_BITNUM(ffa_fw_abi_needed[i].id),
+                    ffa_fw_abi_supported);
+        else
             printk(XENLOG_INFO "ARM FF-A Firmware does not support %s\n",
                    ffa_fw_abi_needed[i].name);
-            goto err_no_fw;
-        }
     }
 
     if ( !ffa_rxtx_init() )
@@ -611,6 +618,7 @@ err_rxtx_destroy:
     ffa_rxtx_destroy();
 err_no_fw:
     ffa_fw_version = 0;
+    bitmap_zero(ffa_fw_abi_supported, FFA_ABI_BITMAP_SIZE);
     printk(XENLOG_WARNING "ARM FF-A No firmware support\n");
 
     return false;
xen/arch/arm/tee/ffa_notif.c +7 -0
index 541e61d2f606..4b3e46318f4b 100644
@@ -377,6 +377,13 @@ void ffa_notif_init(void)
     unsigned int irq;
     int ret;
 
+    /* Only enable fw notification if all ABIs we need are supported */
+    if ( !(ffa_fw_supports_fid(FFA_NOTIFICATION_BITMAP_CREATE) &&
+           ffa_fw_supports_fid(FFA_NOTIFICATION_BITMAP_DESTROY) &&
+           ffa_fw_supports_fid(FFA_NOTIFICATION_GET) &&
+           ffa_fw_supports_fid(FFA_NOTIFICATION_INFO_GET_64)) )
+        return;
+
     arm_smccc_1_2_smc(&arg, &resp);
     if ( resp.a0 != FFA_SUCCESS_32 )
         return;
xen/arch/arm/tee/ffa_partinfo.c +29 -1
index 93a03c6bc672..99c48f0e5c05 100644
@@ -77,7 +77,15 @@ int32_t ffa_handle_partition_info_get(uint32_t w1, uint32_t w2, uint32_t w3,
      */
     if ( w5 == FFA_PARTITION_INFO_GET_COUNT_FLAG &&
          ctx->guest_vers == FFA_VERSION_1_1 )
-        return ffa_partition_info_get(w1, w2, w3, w4, w5, count, fpi_size);
+    {
+        if ( ffa_fw_supports_fid(FFA_PARTITION_INFO_GET) )
+            return ffa_partition_info_get(w1, w2, w3, w4, w5, count, fpi_size);
+        else
+        {
+            *count = 0;
+            return FFA_RET_OK;
+        }
+    }
     if ( w5 )
         return FFA_RET_INVALID_PARAMETERS;
 
@@ -87,6 +95,18 @@ int32_t ffa_handle_partition_info_get(uint32_t w1, uint32_t w2, uint32_t w3,
     if ( !spin_trylock(&ctx->rx_lock) )
         return FFA_RET_BUSY;
 
+    if ( !ffa_fw_supports_fid(FFA_PARTITION_INFO_GET) )
+    {
+        if ( ctx->guest_vers == FFA_VERSION_1_0 )
+            *fpi_size = sizeof(struct ffa_partition_info_1_0);
+        else
+            *fpi_size = sizeof(struct ffa_partition_info_1_1);
+
+        *count = 0;
+        ret = FFA_RET_OK;
+        goto out;
+    }
+
     if ( !ctx->page_count || !ctx->rx_is_free )
         goto out;
     spin_lock(&ffa_rx_buffer_lock);
@@ -250,6 +270,11 @@ bool ffa_partinfo_init(void)
     uint32_t count;
     int e;
 
+    if ( !ffa_fw_supports_fid(FFA_PARTITION_INFO_GET) ||
+         !ffa_fw_supports_fid(FFA_MSG_SEND_DIRECT_REQ_32) ||
+         !ffa_rx || !ffa_tx )
+        return false;
+
     e = ffa_partition_info_get(0, 0, 0, 0, 0, &count, &fpi_size);
     if ( e )
     {
@@ -313,6 +338,9 @@ int ffa_partinfo_domain_init(struct domain *d)
     unsigned int n;
     int32_t res;
 
+    if ( !ffa_fw_supports_fid(FFA_MSG_SEND_DIRECT_REQ_32) )
+        return 0;
+
     ctx->vm_destroy_bitmap = xzalloc_array(unsigned long, count);
     if ( !ctx->vm_destroy_bitmap )
         return -ENOMEM;
xen/arch/arm/tee/ffa_private.h +33 -5
index 045d9c4a0b56..85eb61c13464 100644
@@ -14,6 +14,7 @@
 #include <xen/spinlock.h>
 #include <xen/sched.h>
 #include <xen/time.h>
+#include <xen/bitmap.h>
 
 /* Error codes */
 #define FFA_RET_OK                      0
@@ -201,18 +202,17 @@
 #define FFA_INTERRUPT                   0x84000062U
 #define FFA_VERSION                     0x84000063U
 #define FFA_FEATURES                    0x84000064U
-#define FFA_RX_ACQUIRE                  0x84000084U
 #define FFA_RX_RELEASE                  0x84000065U
 #define FFA_RXTX_MAP_32                 0x84000066U
 #define FFA_RXTX_MAP_64                 0xC4000066U
 #define FFA_RXTX_UNMAP                  0x84000067U
 #define FFA_PARTITION_INFO_GET          0x84000068U
 #define FFA_ID_GET                      0x84000069U
-#define FFA_SPM_ID_GET                  0x84000085U
+#define FFA_MSG_POLL                    0x8400006AU
 #define FFA_MSG_WAIT                    0x8400006BU
 #define FFA_MSG_YIELD                   0x8400006CU
 #define FFA_RUN                         0x8400006DU
-#define FFA_MSG_SEND2                   0x84000086U
+#define FFA_MSG_SEND                    0x8400006EU
 #define FFA_MSG_SEND_DIRECT_REQ_32      0x8400006FU
 #define FFA_MSG_SEND_DIRECT_REQ_64      0xC400006FU
 #define FFA_MSG_SEND_DIRECT_RESP_32     0x84000070U
@@ -230,8 +230,6 @@
 #define FFA_MEM_RECLAIM                 0x84000077U
 #define FFA_MEM_FRAG_RX                 0x8400007AU
 #define FFA_MEM_FRAG_TX                 0x8400007BU
-#define FFA_MSG_SEND                    0x8400006EU
-#define FFA_MSG_POLL                    0x8400006AU
 #define FFA_NOTIFICATION_BITMAP_CREATE  0x8400007DU
 #define FFA_NOTIFICATION_BITMAP_DESTROY 0x8400007EU
 #define FFA_NOTIFICATION_BIND           0x8400007FU
@@ -240,6 +238,25 @@
 #define FFA_NOTIFICATION_GET            0x84000082U
 #define FFA_NOTIFICATION_INFO_GET_32    0x84000083U
 #define FFA_NOTIFICATION_INFO_GET_64    0xC4000083U
+#define FFA_RX_ACQUIRE                  0x84000084U
+#define FFA_SPM_ID_GET                  0x84000085U
+#define FFA_MSG_SEND2                   0x84000086U
+
+/**
+ * Encoding of features supported or not by the fw in a bitmap:
+ * - Function IDs are going from 0x60 to 0xFF
+ * - A function can be supported in 32 and/or 64bit
+ * The bitmap has one bit for each function in 32 and 64 bit.
+ */
+#define FFA_ABI_ID(id)        ((id) & ARM_SMCCC_FUNC_MASK)
+#define FFA_ABI_CONV(id)      (((id) >> ARM_SMCCC_CONV_SHIFT) & BIT(0,U))
+
+#define FFA_ABI_MIN           FFA_ABI_ID(FFA_ERROR)
+#define FFA_ABI_MAX           FFA_ABI_ID(FFA_MSG_SEND2)
+
+#define FFA_ABI_BITMAP_SIZE   (2 * (FFA_ABI_MAX - FFA_ABI_MIN + 1))
+#define FFA_ABI_BITNUM(id)    ((FFA_ABI_ID(id) - FFA_ABI_MIN) << 1 | \
+                               FFA_ABI_CONV(id))
 
 struct ffa_ctx_notif {
     bool enabled;
@@ -289,6 +306,8 @@ extern void *ffa_rx;
 extern void *ffa_tx;
 extern spinlock_t ffa_rx_buffer_lock;
 extern spinlock_t ffa_tx_buffer_lock;
+extern uint32_t __ro_after_init ffa_fw_version;
+extern DECLARE_BITMAP(ffa_fw_abi_supported, FFA_ABI_BITMAP_SIZE);
 
 bool ffa_shm_domain_destroy(struct domain *d);
 void ffa_handle_mem_share(struct cpu_user_regs *regs);
@@ -401,4 +420,13 @@ static inline int32_t ffa_rx_release(void)
     return ffa_simple_call(FFA_RX_RELEASE, 0, 0, 0, 0);
 }
 
+static inline bool ffa_fw_supports_fid(uint32_t fid)
+{
+    BUILD_BUG_ON(FFA_ABI_MIN > FFA_ABI_MAX);
+
+    if ( FFA_ABI_BITNUM(fid) > FFA_ABI_BITMAP_SIZE)
+        return false;
+    return test_bit(FFA_ABI_BITNUM(fid), ffa_fw_abi_supported);
+}
+
 #endif /*__FFA_PRIVATE_H__*/
xen/arch/arm/tee/ffa_rxtx.c +4 -0
index 661764052e67..b6931c855779 100644
@@ -193,6 +193,10 @@ bool ffa_rxtx_init(void)
 {
     int e;
 
+    /* Firmware not there or not supporting */
+    if ( !ffa_fw_supports_fid(FFA_RXTX_MAP_64) )
+        return false;
+
     ffa_rx = alloc_xenheap_pages(get_order_from_pages(FFA_RXTX_PAGE_COUNT), 0);
     if ( !ffa_rx )
         return false;
xen/arch/arm/tee/ffa_shm.c +12 -0
index 370d83ec5cf8..efa5b67db8e1 100644
@@ -149,6 +149,9 @@ static int32_t ffa_mem_share(uint32_t tot_len, uint32_t frag_len,
 static int32_t ffa_mem_reclaim(uint32_t handle_lo, uint32_t handle_hi,
                                uint32_t flags)
 {
+    if ( !ffa_fw_supports_fid(FFA_MEM_RECLAIM) )
+        return FFA_RET_NOT_SUPPORTED;
+
     return ffa_simple_call(FFA_MEM_RECLAIM, handle_lo, handle_hi, flags, 0);
 }
 
@@ -467,6 +470,12 @@ void ffa_handle_mem_share(struct cpu_user_regs *regs)
     uint32_t range_count;
     uint32_t region_offs;
 
+    if ( !ffa_fw_supports_fid(FFA_MEM_SHARE_64) )
+    {
+        ret = FFA_RET_NOT_SUPPORTED;
+        goto out_set_ret;
+    }
+
     /*
      * We're only accepting memory transaction descriptors via the rx/tx
      * buffer.
@@ -621,6 +630,9 @@ int ffa_handle_mem_reclaim(uint64_t handle, uint32_t flags)
     register_t handle_lo;
     int ret;
 
+    if ( !ffa_fw_supports_fid(FFA_MEM_RECLAIM) )
+        return FFA_RET_NOT_SUPPORTED;
+
     spin_lock(&ctx->lock);
     shm = find_shm_mem(ctx, handle);
     if ( shm )
Signature · 1 line
2.47.0
#5 lore raw

PATCH v2 2/10 xen/arm: ffa: Rework feature discovery

In reply to Bertrand Marquis

Store the list of ABI we need in a list and go through the list instead of having a list of conditions inside the code.

No functional change.

---
Changes in v2:
- Store a string version of ABI needed from firmware and print the name of the ABI not supported instead of the id
- Restore comment with TODO which should not have been removed at this stage
- fix to unsigned int the index in the loop on the array
- use abi instead of feature in the names of the functions and variables as we are not checking features but abis

---
 xen/arch/arm/tee/ffa.c | 57 +++++++++++++++++++++++++-----------------
 1 file changed, 34 insertions(+), 23 deletions(-)
xen/arch/arm/tee/ffa.c +34 -23
index 1cc4023135d5..dde932422ecf 100644
@@ -74,6 +74,31 @@
 /* Negotiated FF-A version to use with the SPMC, 0 if not there or supported */
 static uint32_t __ro_after_init ffa_fw_version;
 
+struct ffa_fw_abi {
+    const uint32_t id;
+    const char *name;
+};
+
+#define FW_ABI(abi) {abi,#abi}
+
+/* List of ABI we use from the firmware */
+static const struct ffa_fw_abi ffa_fw_abi_needed[] = {
+    FW_ABI(FFA_VERSION),
+    FW_ABI(FFA_FEATURES),
+    FW_ABI(FFA_NOTIFICATION_BITMAP_CREATE),
+    FW_ABI(FFA_NOTIFICATION_BITMAP_DESTROY),
+    FW_ABI(FFA_PARTITION_INFO_GET),
+    FW_ABI(FFA_NOTIFICATION_INFO_GET_64),
+    FW_ABI(FFA_NOTIFICATION_GET),
+    FW_ABI(FFA_RX_RELEASE),
+    FW_ABI(FFA_RXTX_MAP_64),
+    FW_ABI(FFA_RXTX_UNMAP),
+    FW_ABI(FFA_MEM_SHARE_32),
+    FW_ABI(FFA_MEM_SHARE_64),
+    FW_ABI(FFA_MEM_RECLAIM),
+    FW_ABI(FFA_MSG_SEND_DIRECT_REQ_32),
+    FW_ABI(FFA_MSG_SEND_DIRECT_REQ_64),
+};
 
 /*
  * Our rx/tx buffers shared with the SPMC. FFA_RXTX_PAGE_COUNT is the
@@ -112,20 +137,9 @@ static bool ffa_get_version(uint32_t *vers)
     return true;
 }
 
-static int32_t ffa_features(uint32_t id)
-{
-    return ffa_simple_call(FFA_FEATURES, id, 0, 0, 0);
-}
-
-static bool check_mandatory_feature(uint32_t id)
+static bool ffa_abi_supported(uint32_t id)
 {
-    int32_t ret = ffa_features(id);
-
-    if ( ret )
-        printk(XENLOG_ERR "ffa: mandatory feature id %#x missing: error %d\n",
-               id, ret);
-
-    return !ret;
+    return !ffa_simple_call(FFA_FEATURES, id, 0, 0, 0);
 }
 
 static void handle_version(struct cpu_user_regs *regs)
@@ -540,17 +554,14 @@ static bool ffa_probe(void)
      * TODO: Rework the code to allow domain to use a subset of the
      * features supported.
      */
-    if ( !check_mandatory_feature(FFA_PARTITION_INFO_GET) ||
-         !check_mandatory_feature(FFA_RX_RELEASE) ||
-         !check_mandatory_feature(FFA_RXTX_MAP_64) ||
-         !check_mandatory_feature(FFA_MEM_SHARE_64) ||
-         !check_mandatory_feature(FFA_RXTX_UNMAP) ||
-         !check_mandatory_feature(FFA_MEM_SHARE_32) ||
-         !check_mandatory_feature(FFA_MEM_RECLAIM) ||
-         !check_mandatory_feature(FFA_MSG_SEND_DIRECT_REQ_32) )
+    for ( unsigned int i = 0; i < ARRAY_SIZE(ffa_fw_abi_needed); i++ )
     {
-        printk(XENLOG_ERR "ffa: Mandatory feature not supported by fw\n");
-        goto err_no_fw;
+        if ( !ffa_abi_supported(ffa_fw_abi_needed[i].id) )
+        {
+            printk(XENLOG_INFO "ARM FF-A Firmware does not support %s\n",
+                   ffa_fw_abi_needed[i].name);
+            goto err_no_fw;
+        }
     }
 
     ffa_fw_version = vers;
Signature · 1 line
2.47.0
#1 lore raw

PATCH v2 1/10 xen/arm: ffa: Rework firmware discovery

In reply to Bertrand Marquis

Rework firmware discovery during probe:
- move prints into the probe
- rename ffa_version to ffa_fw_version as the variable identifies the version of the firmware and not the one we support
- add error prints when allocation fail during probe

No functional changes.

---
Changes in v2:
- Fix error message when we fail to retrieve ffa_version
- Move back printing the firmware version before checking supported features
- Use Warning instead of Info to inform user that FF-A is not supported in firmware.

---
 xen/arch/arm/tee/ffa.c | 44 ++++++++++++++++++++++++++----------------
 1 file changed, 27 insertions(+), 17 deletions(-)
xen/arch/arm/tee/ffa.c +27 -17
index 022089278e1c..1cc4023135d5 100644
@@ -71,8 +71,8 @@
 
 #include "ffa_private.h"
 
-/* Negotiated FF-A version to use with the SPMC */
-static uint32_t __ro_after_init ffa_version;
+/* Negotiated FF-A version to use with the SPMC, 0 if not there or supported */
+static uint32_t __ro_after_init ffa_fw_version;
 
 
 /*
@@ -105,10 +105,7 @@ static bool ffa_get_version(uint32_t *vers)
 
     arm_smccc_1_2_smc(&arg, &resp);
     if ( resp.a0 == FFA_RET_NOT_SUPPORTED )
-    {
-        gprintk(XENLOG_ERR, "ffa: FFA_VERSION returned not supported\n");
         return false;
-    }
 
     *vers = resp.a0;
 
@@ -372,7 +369,7 @@ static int ffa_domain_init(struct domain *d)
     struct ffa_ctx *ctx;
     int ret;
 
-    if ( !ffa_version )
+    if ( !ffa_fw_version )
         return -ENODEV;
      /*
       * We can't use that last possible domain ID or ffa_get_vm_id() would
@@ -505,6 +502,9 @@ static bool ffa_probe(void)
      */
     BUILD_BUG_ON(PAGE_SIZE != FFA_PAGE_SIZE);
 
+    printk(XENLOG_INFO "ARM FF-A Mediator version %u.%u\n",
+           FFA_MY_VERSION_MAJOR, FFA_MY_VERSION_MINOR);
+
     /*
      * psci_init_smccc() updates this value with what's reported by EL-3
      * or secure world.
@@ -514,22 +514,24 @@ static bool ffa_probe(void)
         printk(XENLOG_ERR
                "ffa: unsupported SMCCC version %#x (need at least %#x)\n",
                smccc_ver, ARM_SMCCC_VERSION_1_2);
-        return false;
+        goto err_no_fw;
     }
 
     if ( !ffa_get_version(&vers) )
-        return false;
+    {
+        gprintk(XENLOG_ERR, "Cannot retrieve the FFA version\n");
+        goto err_no_fw;
+    }
 
     if ( vers < FFA_MIN_SPMC_VERSION || vers > FFA_MY_VERSION )
     {
         printk(XENLOG_ERR "ffa: Incompatible version %#x found\n", vers);
-        return false;
+        goto err_no_fw;
     }
 
-    major_vers = (vers >> FFA_VERSION_MAJOR_SHIFT) & FFA_VERSION_MAJOR_MASK;
+    major_vers = (vers >> FFA_VERSION_MAJOR_SHIFT)
+                 & FFA_VERSION_MAJOR_MASK;
     minor_vers = vers & FFA_VERSION_MINOR_MASK;
-    printk(XENLOG_INFO "ARM FF-A Mediator version %u.%u\n",
-           FFA_MY_VERSION_MAJOR, FFA_MY_VERSION_MINOR);
     printk(XENLOG_INFO "ARM FF-A Firmware version %u.%u\n",
            major_vers, minor_vers);
 
@@ -546,12 +548,18 @@ static bool ffa_probe(void)
          !check_mandatory_feature(FFA_MEM_SHARE_32) ||
          !check_mandatory_feature(FFA_MEM_RECLAIM) ||
          !check_mandatory_feature(FFA_MSG_SEND_DIRECT_REQ_32) )
-        return false;
+    {
+        printk(XENLOG_ERR "ffa: Mandatory feature not supported by fw\n");
+        goto err_no_fw;
+    }
 
-    if ( !ffa_rxtx_init() )
-        return false;
+    ffa_fw_version = vers;
 
-    ffa_version = vers;
+    if ( !ffa_rxtx_init() )
+    {
+        printk(XENLOG_ERR "ffa: Error during RXTX buffer init\n");
+        goto err_no_fw;
+    }
 
     if ( !ffa_partinfo_init() )
         goto err_rxtx_destroy;
@@ -564,7 +572,9 @@ static bool ffa_probe(void)
 
 err_rxtx_destroy:
     ffa_rxtx_destroy();
-    ffa_version = 0;
+err_no_fw:
+    ffa_fw_version = 0;
+    printk(XENLOG_WARNING "ARM FF-A No firmware support\n");
 
     return false;
 }
Signature · 1 line
2.47.0
#26 lore raw

PATCH v2 9/10 xen/arm: ffa: Remove per VM notif_enabled

In reply to Bertrand Marquis

Remove the per VM flag to store if notifications are enabled or not as the only case where they are not, if notifications are enabled globally, will make the VM creation fail.
Also use the opportunity to always give the notifications interrupts IDs to VM. If the firmware does not support notifications, there won't be any generated and setting one will give back a NOT_SUPPORTED.

---
Changes in v2:
- rebase

---
 xen/arch/arm/tee/ffa.c         | 17 +++--------------
 xen/arch/arm/tee/ffa_notif.c   | 10 +---------
 xen/arch/arm/tee/ffa_private.h |  2 --
 3 files changed, 4 insertions(+), 25 deletions(-)
xen/arch/arm/tee/ffa.c +3 -14
index 72826b49d2aa..3a9525aa4598 100644
@@ -169,8 +169,6 @@ static void handle_version(struct cpu_user_regs *regs)
 
 static void handle_features(struct cpu_user_regs *regs)
 {
-    struct domain *d = current->domain;
-    struct ffa_ctx *ctx = d->arch.tee;
     uint32_t a1 = get_user_reg(regs, 1);
     unsigned int n;
 
@@ -218,16 +216,10 @@ static void handle_features(struct cpu_user_regs *regs)
         ffa_set_regs_success(regs, 0, 0);
         break;
     case FFA_FEATURE_NOTIF_PEND_INTR:
-        if ( ctx->notif.enabled )
-            ffa_set_regs_success(regs, GUEST_FFA_NOTIF_PEND_INTR_ID, 0);
-        else
-            ffa_set_regs_error(regs, FFA_RET_NOT_SUPPORTED);
+        ffa_set_regs_success(regs, GUEST_FFA_NOTIF_PEND_INTR_ID, 0);
         break;
     case FFA_FEATURE_SCHEDULE_RECV_INTR:
-        if ( ctx->notif.enabled )
-            ffa_set_regs_success(regs, GUEST_FFA_SCHEDULE_RECV_INTR_ID, 0);
-        else
-            ffa_set_regs_error(regs, FFA_RET_NOT_SUPPORTED);
+        ffa_set_regs_success(regs, GUEST_FFA_SCHEDULE_RECV_INTR_ID, 0);
         break;
 
     case FFA_NOTIFICATION_BIND:
@@ -236,10 +228,7 @@ static void handle_features(struct cpu_user_regs *regs)
     case FFA_NOTIFICATION_SET:
     case FFA_NOTIFICATION_INFO_GET_32:
     case FFA_NOTIFICATION_INFO_GET_64:
-        if ( ctx->notif.enabled )
-            ffa_set_regs_success(regs, 0, 0);
-        else
-            ffa_set_regs_error(regs, FFA_RET_NOT_SUPPORTED);
+        ffa_set_regs_success(regs, 0, 0);
         break;
     default:
         ffa_set_regs_error(regs, FFA_RET_NOT_SUPPORTED);
xen/arch/arm/tee/ffa_notif.c +1 -9
index 4b3e46318f4b..3c6418e62e2b 100644
@@ -405,7 +405,6 @@ void ffa_notif_init(void)
 
 int ffa_notif_domain_init(struct domain *d)
 {
-    struct ffa_ctx *ctx = d->arch.tee;
     int32_t res;
 
     if ( !notif_enabled )
@@ -415,18 +414,11 @@ int ffa_notif_domain_init(struct domain *d)
     if ( res )
         return -ENOMEM;
 
-    ctx->notif.enabled = true;
-
     return 0;
 }
 
 void ffa_notif_domain_destroy(struct domain *d)
 {
-    struct ffa_ctx *ctx = d->arch.tee;
-
-    if ( ctx->notif.enabled )
-    {
+    if ( notif_enabled )
         ffa_notification_bitmap_destroy(ffa_get_vm_id(d));
-        ctx->notif.enabled = false;
-    }
 }
xen/arch/arm/tee/ffa_private.h +0 -2
index 02162e0ee4c7..973ee55be09b 100644
@@ -281,8 +281,6 @@ struct ffa_mem_region {
 };
 
 struct ffa_ctx_notif {
-    bool enabled;
-
     /*
      * True if domain is reported by FFA_NOTIFICATION_INFO_GET to have
      * pending global notifications.
Signature · 1 line
2.47.0
#15 lore raw

PATCH v2 5/10 xen/arm: ffa: Rework partition info get

In reply to Bertrand Marquis

Rework the partition info get implementation to use the correct size of structure depending on the version of the protocol and simplifies the structure copy to use only memcpy and prevent recreating the structure each time.
The goal here is to have an implementation that will be easier to maintain in the long term as the specification is only adding fields to structure with versions to simplify support of several protocol versions and as such an SPMC implementation in the future could use this and return a size higher than the one we expect.
The patch is fixing the part_info_get function for this and the subscriber discovery on probe.

No functional changes expected.

---
Changes in v2:
- rebase

---
 xen/arch/arm/tee/ffa.c          |  13 +--
 xen/arch/arm/tee/ffa_partinfo.c | 185 ++++++++++++++++++++------------
 xen/arch/arm/tee/ffa_private.h  |   4 +-
 3 files changed, 118 insertions(+), 84 deletions(-)
xen/arch/arm/tee/ffa.c +1 -12
index 267d4435ac08..4b55e8b48f01 100644
@@ -311,8 +311,6 @@ static bool ffa_handle_call(struct cpu_user_regs *regs)
     uint32_t fid = get_user_reg(regs, 0);
     struct domain *d = current->domain;
     struct ffa_ctx *ctx = d->arch.tee;
-    uint32_t fpi_size;
-    uint32_t count;
     int e;
 
     if ( !ctx )
@@ -338,16 +336,7 @@ static bool ffa_handle_call(struct cpu_user_regs *regs)
         e = ffa_handle_rxtx_unmap();
         break;
     case FFA_PARTITION_INFO_GET:
-        e = ffa_handle_partition_info_get(get_user_reg(regs, 1),
-                                          get_user_reg(regs, 2),
-                                          get_user_reg(regs, 3),
-                                          get_user_reg(regs, 4),
-                                          get_user_reg(regs, 5), &count,
-                                          &fpi_size);
-        if ( e )
-            ffa_set_regs_error(regs, e);
-        else
-            ffa_set_regs_success(regs, count, fpi_size);
+        ffa_handle_partition_info_get(regs);
         return true;
     case FFA_RX_RELEASE:
         e = ffa_handle_rx_release();
xen/arch/arm/tee/ffa_partinfo.c +116 -69

283 lines not on this page · raw message

xen/arch/arm/tee/ffa_private.h +1 -3
index 85eb61c13464..e5bc73f9039e 100644
@@ -316,9 +316,7 @@ int ffa_handle_mem_reclaim(uint64_t handle, uint32_t flags);
 bool ffa_partinfo_init(void);
 int ffa_partinfo_domain_init(struct domain *d);
 bool ffa_partinfo_domain_destroy(struct domain *d);
-int32_t ffa_handle_partition_info_get(uint32_t w1, uint32_t w2, uint32_t w3,
-                                      uint32_t w4, uint32_t w5, uint32_t *count,
-                                      uint32_t *fpi_size);
+void ffa_handle_partition_info_get(struct cpu_user_regs *regs);
 
 bool ffa_rxtx_init(void);
 void ffa_rxtx_destroy(void);
Signature · 1 line
2.47.0
#17 lore raw

PATCH v2 6/10 xen/arm: ffa: Use bit 15 convention for SPs

In reply to Bertrand Marquis

Make use and required to have bit 15 convention respected by secure world (having bit 15 of IDs set for secure endpoints and non-set for non-secure ones).
If any secure partition has an ID with bit 15 not set, it will not be possible to contact or detect them.
Print an error log during probe for each secure endpoint detected with bit 15 not set.

We are switching to this convention because Xen is currently not using VMIDs with bit 15 set so we are sure that no VM will have it set (this is ensured by BUILD_BUG_ON in case this becomes false in the future). It is allowing to easily distinguish between secure and non-secure endpoints, preventing the need to store a list of secure endpoint IDs in Xen.

---
Changes in v2:
- rebase

---
 xen/arch/arm/tee/ffa.c          | 22 +++++++++++---
 xen/arch/arm/tee/ffa_partinfo.c | 54 +++++++++++++++++++++++++--------
 xen/arch/arm/tee/ffa_private.h  |  7 +++++
 xen/arch/arm/tee/ffa_shm.c      | 12 +++++++-
 4 files changed, 77 insertions(+), 18 deletions(-)
xen/arch/arm/tee/ffa.c +17 -5
index 4b55e8b48f01..a292003ca9fe 100644
@@ -195,6 +195,14 @@ static void handle_msg_send_direct_req(struct cpu_user_regs *regs, uint32_t fid)
         goto out;
     }
 
+    /* we do not support direct messages to VMs */
+    if ( !FFA_ID_IS_SECURE(src_dst & GENMASK(15,0)) )
+    {
+        resp.a0 = FFA_ERROR;
+        resp.a2 = FFA_RET_NOT_SUPPORTED;
+        goto out;
+    }
+
     arg.a1 = src_dst;
     arg.a2 = get_user_reg(regs, 2) & mask;
     arg.a3 = get_user_reg(regs, 3) & mask;
@@ -391,11 +399,15 @@ static int ffa_domain_init(struct domain *d)
 
     if ( !ffa_fw_version )
         return -ENODEV;
-     /*
-      * We can't use that last possible domain ID or ffa_get_vm_id() would
-      * cause an overflow.
-      */
-    if ( d->domain_id >= UINT16_MAX)
+    /*
+     * We are using the domain_id + 1 as the FF-A ID for VMs as FF-A ID 0 is
+     * reserved for the hypervisor and we only support secure endpoints using
+     * FF-A IDs with BIT 15 set to 1 so make sure those are not used by Xen.
+     */
+    BUILD_BUG_ON(DOMID_FIRST_RESERVED >= UINT16_MAX);
+    BUILD_BUG_ON((DOMID_MASK & BIT(15, U)) != 0);
+
+    if ( d->domain_id >= DOMID_FIRST_RESERVED )
         return -ERANGE;
 
     ctx = xzalloc(struct ffa_ctx);
xen/arch/arm/tee/ffa_partinfo.c +42 -12

76 lines not on this page · raw message

xen/arch/arm/tee/ffa_private.h +7 -0
index e5bc73f9039e..afe69b43dbef 100644
@@ -108,6 +108,13 @@
  */
 #define FFA_CTX_TEARDOWN_DELAY          SECONDS(1)
 
+/*
+ * We rely on the convention suggested but not mandated by the FF-A
+ * specification that secure world endpoint identifiers have the bit 15
+ * set and normal world have it set to 0.
+ */
+#define FFA_ID_IS_SECURE(id)    ((id) & BIT(15, U))
+
 /* FF-A-1.1-REL0 section 10.9.2 Memory region handle, page 167 */
 #define FFA_HANDLE_HYP_FLAG             BIT(63, ULL)
 #define FFA_HANDLE_INVALID              0xffffffffffffffffULL
xen/arch/arm/tee/ffa_shm.c +11 -1
index efa5b67db8e1..29675f9ba3f7 100644
@@ -469,6 +469,7 @@ void ffa_handle_mem_share(struct cpu_user_regs *regs)
     int ret = FFA_RET_DENIED;
     uint32_t range_count;
     uint32_t region_offs;
+    uint16_t dst_id;
 
     if ( !ffa_fw_supports_fid(FFA_MEM_SHARE_64) )
     {
@@ -537,6 +538,15 @@ void ffa_handle_mem_share(struct cpu_user_regs *regs)
         goto out_unlock;
 
     mem_access = ctx->tx + trans.mem_access_offs;
+
+    dst_id = ACCESS_ONCE(mem_access->access_perm.endpoint_id);
+    if ( !FFA_ID_IS_SECURE(dst_id) )
+    {
+        /* we do not support sharing with VMs */
+        ret = FFA_RET_NOT_SUPPORTED;
+        goto out_unlock;
+    }
+
     if ( ACCESS_ONCE(mem_access->access_perm.perm) != FFA_MEM_ACC_RW )
     {
         ret = FFA_RET_NOT_SUPPORTED;
@@ -567,7 +577,7 @@ void ffa_handle_mem_share(struct cpu_user_regs *regs)
         goto out_unlock;
     }
     shm->sender_id = trans.sender_id;
-    shm->ep_id = ACCESS_ONCE(mem_access->access_perm.endpoint_id);
+    shm->ep_id = dst_id;
 
     /*
      * Check that the Composite memory region descriptor fits.
Signature · 1 line
2.47.0
#20 lore raw

PATCH v2 7/10 xen/arm: ffa: Transmit RXTX buffers to the SPMC

In reply to Bertrand Marquis

When an RXTX buffer is mapped by a VM transmit it to the SPMC when it supports RX_ACQUIRE.
As a consequence of that, we must acquire the RX buffer of a VM from the SPMC when we want to use it:
- create a generic acquire and release function to get the rx buffer of a VM which gets it from the SPMC when supported
- rename the rx_acquire to hyp_rx_acquire to remove confusion
- rework the rx_lock to only lock access to rx_is_free and only allow usage of the rx buffer to one who managed to acquire it, thus removing the trylock and returning busy if rx_is_free is false

As part of this change move some structure definition to ffa_private from ffa_shm as those are need for the MAP call with the SPMC.

---
Changes in v2:
- unmap VM rxtx buffer in SPMC on unmap call or on VM destroy
- rework the unmap call to the SPMC to properly pass the VM ID

---
 xen/arch/arm/tee/ffa.c          |   2 +-
 xen/arch/arm/tee/ffa_partinfo.c |  29 ++----
 xen/arch/arm/tee/ffa_private.h  |  22 ++++-
 xen/arch/arm/tee/ffa_rxtx.c     | 158 ++++++++++++++++++++++++++------
 xen/arch/arm/tee/ffa_shm.c      |  15 ---
 5 files changed, 161 insertions(+), 65 deletions(-)
xen/arch/arm/tee/ffa.c +1 -1
index a292003ca9fe..40ea5398fa21 100644
@@ -347,7 +347,7 @@ static bool ffa_handle_call(struct cpu_user_regs *regs)
         ffa_handle_partition_info_get(regs);
         return true;
     case FFA_RX_RELEASE:
-        e = ffa_handle_rx_release();
+        e = ffa_rx_release(d);
         break;
     case FFA_MSG_SEND_DIRECT_REQ_32:
     case FFA_MSG_SEND_DIRECT_REQ_64:
xen/arch/arm/tee/ffa_partinfo.c +9 -20
index 3cf801523296..fde187dba4e5 100644
@@ -121,11 +121,9 @@ void ffa_handle_partition_info_get(struct cpu_user_regs *regs)
         goto out;
     }
 
-    if ( !spin_trylock(&ctx->rx_lock) )
-    {
-        ret = FFA_RET_BUSY;
+    ret = ffa_rx_acquire(d);
+    if ( ret != FFA_RET_OK )
         goto out;
-    }
 
     dst_buf = ctx->rx;
 
@@ -135,22 +133,16 @@ void ffa_handle_partition_info_get(struct cpu_user_regs *regs)
         goto out_rx_release;
     }
 
-    if ( !ctx->page_count || !ctx->rx_is_free )
-    {
-        ret = FFA_RET_DENIED;
-        goto out_rx_release;
-    }
-
     spin_lock(&ffa_rx_buffer_lock);
 
     ret = ffa_partition_info_get(uuid, 0, &ffa_sp_count, &src_size);
 
     if ( ret )
-        goto out_rx_buf_unlock;
+        goto out_rx_hyp_unlock;
 
     /*
      * ffa_partition_info_get() succeeded so we now own the RX buffer we
-     * share with the SPMC. We must give it back using ffa_rx_release()
+     * share with the SPMC. We must give it back using ffa_hyp_rx_release()
      * once we've copied the content.
      */
 
@@ -193,15 +185,13 @@ void ffa_handle_partition_info_get(struct cpu_user_regs *regs)
         }
     }
 
-    ctx->rx_is_free = false;
-
 out_rx_hyp_release:
-    ffa_rx_release();
-out_rx_buf_unlock:
+    ffa_hyp_rx_release();
+out_rx_hyp_unlock:
     spin_unlock(&ffa_rx_buffer_lock);
 out_rx_release:
-    spin_unlock(&ctx->rx_lock);
-
+    if ( ret != FFA_RET_OK )
+        ffa_rx_release(d);
 out:
     if ( ret )
         ffa_set_regs_error(regs, ret);
@@ -368,8 +358,7 @@ bool ffa_partinfo_init(void)
     ret = init_subscribers(count, fpi_size);
 
 out:
-    ffa_rx_release();
-
+    ffa_hyp_rx_release();
     return ret;
 }
 
xen/arch/arm/tee/ffa_private.h +19 -3
index afe69b43dbef..9adfe687c3c9 100644
@@ -265,6 +265,21 @@
 #define FFA_ABI_BITNUM(id)    ((FFA_ABI_ID(id) - FFA_ABI_MIN) << 1 | \
                                FFA_ABI_CONV(id))
 
+/* Constituent memory region descriptor */
+struct ffa_address_range {
+    uint64_t address;
+    uint32_t page_count;
+    uint32_t reserved;
+};
+
+/* Composite memory region descriptor */
+struct ffa_mem_region {
+    uint32_t total_page_count;
+    uint32_t address_range_count;
+    uint64_t reserved;
+    struct ffa_address_range address_range_array[];
+};
+
 struct ffa_ctx_notif {
     bool enabled;
 
@@ -292,7 +307,7 @@ struct ffa_ctx {
     struct ffa_ctx_notif notif;
     /*
      * tx_lock is used to serialize access to tx
-     * rx_lock is used to serialize access to rx
+     * rx_lock is used to serialize access to rx_is_free
      * lock is used for the rest in this struct
      */
     spinlock_t tx_lock;
@@ -331,7 +346,8 @@ void ffa_rxtx_domain_destroy(struct domain *d);
 uint32_t ffa_handle_rxtx_map(uint32_t fid, register_t tx_addr,
 			     register_t rx_addr, uint32_t page_count);
 uint32_t ffa_handle_rxtx_unmap(void);
-int32_t ffa_handle_rx_release(void);
+int32_t ffa_rx_acquire(struct domain *d);
+int32_t ffa_rx_release(struct domain *d);
 
 void ffa_notif_init(void);
 void ffa_notif_init_interrupt(void);
@@ -420,7 +436,7 @@ static inline int32_t ffa_simple_call(uint32_t fid, register_t a1,
     return ffa_get_ret_code(&resp);
 }
 
-static inline int32_t ffa_rx_release(void)
+static inline int32_t ffa_hyp_rx_release(void)
 {
     return ffa_simple_call(FFA_RX_RELEASE, 0, 0, 0, 0);
 }
xen/arch/arm/tee/ffa_rxtx.c +132 -26

226 lines not on this page · raw message

xen/arch/arm/tee/ffa_shm.c +0 -15
index 29675f9ba3f7..d628c1b70609 100644
@@ -16,21 +16,6 @@
 
 #include "ffa_private.h"
 
-/* Constituent memory region descriptor */
-struct ffa_address_range {
-    uint64_t address;
-    uint32_t page_count;
-    uint32_t reserved;
-};
-
-/* Composite memory region descriptor */
-struct ffa_mem_region {
-    uint32_t total_page_count;
-    uint32_t address_range_count;
-    uint64_t reserved;
-    struct ffa_address_range address_range_array[];
-};
-
 /* Memory access permissions descriptor */
 struct ffa_mem_access_perm {
     uint16_t endpoint_id;
Signature · 1 line
2.47.0
#24 lore raw

PATCH v2 8/10 xen/arm: ffa: move message function into ffa_msg.c

In reply to Bertrand Marquis

Move the direct message handling function in its own source file and rename it to have a ffa_ prefix.
This is a preparation to add support for indirect messages which will go into this newly created source file.

---
Changes in v2:
- rebase

---
 xen/arch/arm/tee/Makefile      |  1 +
 xen/arch/arm/tee/ffa.c         | 69 +----------------------------
 xen/arch/arm/tee/ffa_msg.c     | 80 ++++++++++++++++++++++++++++++++++
 xen/arch/arm/tee/ffa_private.h |  2 +
 4 files changed, 84 insertions(+), 68 deletions(-)
 create mode 100644 xen/arch/arm/tee/ffa_msg.c
xen/arch/arm/tee/Makefile +1 -0
index 7c0f46f7f446..0848c833fec3 100644
@@ -3,5 +3,6 @@ obj-$(CONFIG_FFA) += ffa_shm.o
 obj-$(CONFIG_FFA) += ffa_partinfo.o
 obj-$(CONFIG_FFA) += ffa_rxtx.o
 obj-$(CONFIG_FFA) += ffa_notif.o
+obj-$(CONFIG_FFA) += ffa_msg.o
 obj-y += tee.o
 obj-$(CONFIG_OPTEE) += optee.o
xen/arch/arm/tee/ffa.c +1 -68

81 lines not on this page · raw message

xen/arch/arm/tee/ffa_msg.c new file +80 -0

80 lines not on this page · raw message

xen/arch/arm/tee/ffa_private.h +2 -0
index 9adfe687c3c9..02162e0ee4c7 100644
@@ -360,6 +360,8 @@ void ffa_handle_notification_info_get(struct cpu_user_regs *regs);
 void ffa_handle_notification_get(struct cpu_user_regs *regs);
 int ffa_handle_notification_set(struct cpu_user_regs *regs);
 
+void ffa_handle_msg_send_direct_req(struct cpu_user_regs *regs, uint32_t fid);
+
 static inline uint16_t ffa_get_vm_id(const struct domain *d)
 {
     /* +1 since 0 is reserved for the hypervisor in FF-A */
Signature · 1 line
2.47.0
#30 lore raw

PATCH v2 10/10 xen/arm: ffa: Add indirect message support

In reply to Bertrand Marquis

Add support for FFA_MSG_SEND2 to send indirect messages from a VM to a secure partition.

---
Changes in v2:
- rebase

---
 xen/arch/arm/tee/ffa.c         |  5 ++++
 xen/arch/arm/tee/ffa_msg.c     | 49 ++++++++++++++++++++++++++++++++++
 xen/arch/arm/tee/ffa_private.h |  1 +
 3 files changed, 55 insertions(+)
xen/arch/arm/tee/ffa.c +5 -0
index 3a9525aa4598..21d41b452dc9 100644
@@ -101,6 +101,7 @@ static const struct ffa_fw_abi ffa_fw_abi_needed[] = {
     FW_ABI(FFA_MEM_RECLAIM),
     FW_ABI(FFA_MSG_SEND_DIRECT_REQ_32),
     FW_ABI(FFA_MSG_SEND_DIRECT_REQ_64),
+    FW_ABI(FFA_MSG_SEND2),
 };
 
 /*
@@ -195,6 +196,7 @@ static void handle_features(struct cpu_user_regs *regs)
     case FFA_PARTITION_INFO_GET:
     case FFA_MSG_SEND_DIRECT_REQ_32:
     case FFA_MSG_SEND_DIRECT_REQ_64:
+    case FFA_MSG_SEND2:
         ffa_set_regs_success(regs, 0, 0);
         break;
     case FFA_MEM_SHARE_64:
@@ -275,6 +277,9 @@ static bool ffa_handle_call(struct cpu_user_regs *regs)
     case FFA_MSG_SEND_DIRECT_REQ_64:
         ffa_handle_msg_send_direct_req(regs, fid);
         return true;
+    case FFA_MSG_SEND2:
+        e = ffa_handle_msg_send2(regs);
+        break;
     case FFA_MEM_SHARE_32:
     case FFA_MEM_SHARE_64:
         ffa_handle_mem_share(regs);
xen/arch/arm/tee/ffa_msg.c +49 -0

58 lines not on this page · raw message

xen/arch/arm/tee/ffa_private.h +1 -0
index 973ee55be09b..d441c0ca5598 100644
@@ -359,6 +359,7 @@ void ffa_handle_notification_get(struct cpu_user_regs *regs);
 int ffa_handle_notification_set(struct cpu_user_regs *regs);
 
 void ffa_handle_msg_send_direct_req(struct cpu_user_regs *regs, uint32_t fid);
+int32_t ffa_handle_msg_send2(struct cpu_user_regs *regs);
 
 static inline uint16_t ffa_get_vm_id(const struct domain *d)
 {
Signature · 1 line
2.47.0
#2 lore raw

Re: [PATCH v2 01/10] xen/arm: ffa: Rework firmware discovery

In reply to Bertrand Marquis

Hi Bertrand,

Quoted 81 lines · Bertrand Marquis

Rework firmware discovery during probe:
- move prints into the probe
- rename ffa_version to ffa_fw_version as the variable identifies the version of the firmware and not the one we support
- add error prints when allocation fail during probe

No functional changes.

---
Changes in v2:
- Fix error message when we fail to retrieve ffa_version
- Move back printing the firmware version before checking supported features
- Use Warning instead of Info to inform user that FF-A is not supported in firmware.

---
 xen/arch/arm/tee/ffa.c | 44 ++++++++++++++++++++++++++----------------
 1 file changed, 27 insertions(+), 17 deletions(-)
xen/arch/arm/tee/ffa.c +11 -8
index 022089278e1c..1cc4023135d5 100644
@@ -71,8 +71,8 @@
 
 #include "ffa_private.h"
 
-/* Negotiated FF-A version to use with the SPMC */
-static uint32_t __ro_after_init ffa_version;
+/* Negotiated FF-A version to use with the SPMC, 0 if not there or supported */
+static uint32_t __ro_after_init ffa_fw_version;
 
 
 /*
@@ -105,10 +105,7 @@ static bool ffa_get_version(uint32_t *vers)
 
     arm_smccc_1_2_smc(&arg, &resp);
     if ( resp.a0 == FFA_RET_NOT_SUPPORTED )
-    {
-        gprintk(XENLOG_ERR, "ffa: FFA_VERSION returned not supported\n");
         return false;
-    }
 
     *vers = resp.a0;
 
@@ -372,7 +369,7 @@ static int ffa_domain_init(struct domain *d)
     struct ffa_ctx *ctx;
     int ret;
 
-    if ( !ffa_version )
+    if ( !ffa_fw_version )
         return -ENODEV;
      /*
       * We can't use that last possible domain ID or ffa_get_vm_id() would
@@ -505,6 +502,9 @@ static bool ffa_probe(void)
      */
     BUILD_BUG_ON(PAGE_SIZE != FFA_PAGE_SIZE);
 
+    printk(XENLOG_INFO "ARM FF-A Mediator version %u.%u\n",
+           FFA_MY_VERSION_MAJOR, FFA_MY_VERSION_MINOR);
+
     /*
      * psci_init_smccc() updates this value with what's reported by EL-3
      * or secure world.
@@ -514,22 +514,24 @@ static bool ffa_probe(void)
         printk(XENLOG_ERR
                "ffa: unsupported SMCCC version %#x (need at least %#x)\n",
                smccc_ver, ARM_SMCCC_VERSION_1_2);
-        return false;
+        goto err_no_fw;
     }
 
     if ( !ffa_get_version(&vers) )
-        return false;
+    {
+        gprintk(XENLOG_ERR, "Cannot retrieve the FFA version\n");
+        goto err_no_fw;
+    }
 

xen/arch/arm/tee/ffa.c static bool ffa_probe(void) · line 533

Quoted 10 lines
xen/arch/arm/tee/ffa.c +3 -2
     if ( vers < FFA_MIN_SPMC_VERSION || vers > FFA_MY_VERSION )
     {
         printk(XENLOG_ERR "ffa: Incompatible version %#x found\n", vers);
-        return false;
+        goto err_no_fw;
     }
 
-    major_vers = (vers >> FFA_VERSION_MAJOR_SHIFT) & FFA_VERSION_MAJOR_MASK;
+    major_vers = (vers >> FFA_VERSION_MAJOR_SHIFT)
+                 & FFA_VERSION_MAJOR_MASK;

Spurious change?

xen/arch/arm/tee/ffa.c static bool ffa_probe(void) · line 532

Quoted 3 lines
xen/arch/arm/tee/ffa.c +0 -2
     minor_vers = vers & FFA_VERSION_MINOR_MASK;
-    printk(XENLOG_INFO "ARM FF-A Mediator version %u.%u\n",
-           FFA_MY_VERSION_MAJOR, FFA_MY_VERSION_MINOR);

It's not a big deal, but isn't it useful to know which version we're at? If it's too much with a separate line, how about adding "(our version %u.u%)" at the end of the line below?

Quoted 11 lines
xen/arch/arm/tee/ffa.c +3 -1
     printk(XENLOG_INFO "ARM FF-A Firmware version %u.%u\n",
            major_vers, minor_vers);
 
@@ -546,12 +548,18 @@ static bool ffa_probe(void)
          !check_mandatory_feature(FFA_MEM_SHARE_32) ||
          !check_mandatory_feature(FFA_MEM_RECLAIM) ||
          !check_mandatory_feature(FFA_MSG_SEND_DIRECT_REQ_32) )
-        return false;
+    {
+        printk(XENLOG_ERR "ffa: Mandatory feature not supported by fw\n");
+        goto err_no_fw;

xen/arch/arm/tee/ffa.c static bool ffa_probe(void) · line 560

Quoted 10 lines
xen/arch/arm/tee/ffa.c +5 -3
+    }
 
-    if ( !ffa_rxtx_init() )
-        return false;
+    ffa_fw_version = vers;
 
-    ffa_version = vers;
+    if ( !ffa_rxtx_init() )
+    {
+        printk(XENLOG_ERR "ffa: Error during RXTX buffer init\n");

With this added, wouldn't it make sense to remove the error print in ffa_rxtx_init()?

Cheers,
Jens

Quoted 19 lines
xen/arch/arm/tee/ffa.c +5 -2
+        goto err_no_fw;
+    }
 
     if ( !ffa_partinfo_init() )
         goto err_rxtx_destroy;
@@ -564,7 +572,9 @@ static bool ffa_probe(void)
 
 err_rxtx_destroy:
     ffa_rxtx_destroy();
-    ffa_version = 0;
+err_no_fw:
+    ffa_fw_version = 0;
+    printk(XENLOG_WARNING "ARM FF-A No firmware support\n");
 
     return false;
 }
--

2.47.0

#3 lore raw

Re: [PATCH v2 01/10] xen/arm: ffa: Rework firmware discovery

Quoted 99 lines · Jens Wiklander

Hi Bertrand,

Quoted 81 lines · Bertrand Marquis

Rework firmware discovery during probe:
- move prints into the probe
- rename ffa_version to ffa_fw_version as the variable identifies the version of the firmware and not the one we support
- add error prints when allocation fail during probe

No functional changes.

---
Changes in v2:
- Fix error message when we fail to retrieve ffa_version
- Move back printing the firmware version before checking supported features
- Use Warning instead of Info to inform user that FF-A is not supported in firmware.

---
 xen/arch/arm/tee/ffa.c | 44 ++++++++++++++++++++++++++----------------
 1 file changed, 27 insertions(+), 17 deletions(-)
xen/arch/arm/tee/ffa.c +11 -8
index 022089278e1c..1cc4023135d5 100644
@@ -71,8 +71,8 @@
 
 #include "ffa_private.h"
 
-/* Negotiated FF-A version to use with the SPMC */
-static uint32_t __ro_after_init ffa_version;
+/* Negotiated FF-A version to use with the SPMC, 0 if not there or supported */
+static uint32_t __ro_after_init ffa_fw_version;
 
 
 /*
@@ -105,10 +105,7 @@ static bool ffa_get_version(uint32_t *vers)
 
     arm_smccc_1_2_smc(&arg, &resp);
     if ( resp.a0 == FFA_RET_NOT_SUPPORTED )
-    {
-        gprintk(XENLOG_ERR, "ffa: FFA_VERSION returned not supported\n");
         return false;
-    }
 
     *vers = resp.a0;
 
@@ -372,7 +369,7 @@ static int ffa_domain_init(struct domain *d)
     struct ffa_ctx *ctx;
     int ret;
 
-    if ( !ffa_version )
+    if ( !ffa_fw_version )
         return -ENODEV;
      /*
       * We can't use that last possible domain ID or ffa_get_vm_id() would
@@ -505,6 +502,9 @@ static bool ffa_probe(void)
      */
     BUILD_BUG_ON(PAGE_SIZE != FFA_PAGE_SIZE);
 
+    printk(XENLOG_INFO "ARM FF-A Mediator version %u.%u\n",
+           FFA_MY_VERSION_MAJOR, FFA_MY_VERSION_MINOR);
+
     /*
      * psci_init_smccc() updates this value with what's reported by EL-3
      * or secure world.
@@ -514,22 +514,24 @@ static bool ffa_probe(void)
         printk(XENLOG_ERR
                "ffa: unsupported SMCCC version %#x (need at least %#x)\n",
                smccc_ver, ARM_SMCCC_VERSION_1_2);
-        return false;
+        goto err_no_fw;
     }
 
     if ( !ffa_get_version(&vers) )
-        return false;
+    {
+        gprintk(XENLOG_ERR, "Cannot retrieve the FFA version\n");
+        goto err_no_fw;
+    }
 

xen/arch/arm/tee/ffa.c static bool ffa_probe(void) · line 533

Quoted 10 lines
xen/arch/arm/tee/ffa.c +3 -2
     if ( vers < FFA_MIN_SPMC_VERSION || vers > FFA_MY_VERSION )
     {
         printk(XENLOG_ERR "ffa: Incompatible version %#x found\n", vers);
-        return false;
+        goto err_no_fw;
     }
 
-    major_vers = (vers >> FFA_VERSION_MAJOR_SHIFT) & FFA_VERSION_MAJOR_MASK;
+    major_vers = (vers >> FFA_VERSION_MAJOR_SHIFT)
+                 & FFA_VERSION_MAJOR_MASK;

Spurious change?

Quoted 7 lines

xen/arch/arm/tee/ffa.c static bool ffa_probe(void) · line 532

Quoted 3 lines
xen/arch/arm/tee/ffa.c +0 -2
     minor_vers = vers & FFA_VERSION_MINOR_MASK;
-    printk(XENLOG_INFO "ARM FF-A Mediator version %u.%u\n",
-           FFA_MY_VERSION_MAJOR, FFA_MY_VERSION_MINOR);

It's not a big deal, but isn't it useful to know which version we're at? If it's too much with a separate line, how about adding "(our version %u.u%)" at the end of the line below?

Please ignore this comment, I missed this was moved up a few lines.

Thanks,
Jens

Quoted 48 lines
Quoted 12 lines
xen/arch/arm/tee/ffa.c +3 -1
     printk(XENLOG_INFO "ARM FF-A Firmware version %u.%u\n",
            major_vers, minor_vers);
 
@@ -546,12 +548,18 @@ static bool ffa_probe(void)
          !check_mandatory_feature(FFA_MEM_SHARE_32) ||
          !check_mandatory_feature(FFA_MEM_RECLAIM) ||
          !check_mandatory_feature(FFA_MSG_SEND_DIRECT_REQ_32) )
-        return false;
+    {
+        printk(XENLOG_ERR "ffa: Mandatory feature not supported by fw\n");
+        goto err_no_fw;

xen/arch/arm/tee/ffa.c static bool ffa_probe(void) · line 560

Quoted 10 lines
xen/arch/arm/tee/ffa.c +5 -3
+    }
 
-    if ( !ffa_rxtx_init() )
-        return false;
+    ffa_fw_version = vers;
 
-    ffa_version = vers;
+    if ( !ffa_rxtx_init() )
+    {
+        printk(XENLOG_ERR "ffa: Error during RXTX buffer init\n");

With this added, wouldn't it make sense to remove the error print in ffa_rxtx_init()?

Cheers,
Jens

Quoted 19 lines
xen/arch/arm/tee/ffa.c +5 -2
+        goto err_no_fw;
+    }
 
     if ( !ffa_partinfo_init() )
         goto err_rxtx_destroy;
@@ -564,7 +572,9 @@ static bool ffa_probe(void)
 
 err_rxtx_destroy:
     ffa_rxtx_destroy();
-    ffa_version = 0;
+err_no_fw:
+    ffa_fw_version = 0;
+    printk(XENLOG_WARNING "ARM FF-A No firmware support\n");
 
     return false;
 }
--

2.47.0

#6 lore raw

Re: [PATCH v2 02/10] xen/arm: ffa: Rework feature discovery

In reply to Bertrand Marquis

Hi Bertrand,

Quoted 25 lines · Bertrand Marquis

Store the list of ABI we need in a list and go through the list instead of having a list of conditions inside the code.

No functional change.

---
Changes in v2:
- Store a string version of ABI needed from firmware and print the name of the ABI not supported instead of the id
- Restore comment with TODO which should not have been removed at this stage
- fix to unsigned int the index in the loop on the array
- use abi instead of feature in the names of the functions and variables as we are not checking features but abis

---
 xen/arch/arm/tee/ffa.c | 57 +++++++++++++++++++++++++-----------------
 1 file changed, 34 insertions(+), 23 deletions(-)
xen/arch/arm/tee/ffa.c +0 -0
index 1cc4023135d5..dde932422ecf 100644

xen/arch/arm/tee/ffa.c line 78

Quoted 5 lines
xen/arch/arm/tee/ffa.c +2 -0
@@ -74,6 +74,31 @@
 /* Negotiated FF-A version to use with the SPMC, 0 if not there or supported */
 static uint32_t __ro_after_init ffa_fw_version;
 
+struct ffa_fw_abi {
+    const uint32_t id;

I prefer removing the const attribute for this struct member since it doesn't add anything when the struct itself is const.

Cheers,
Jens

Quoted 77 lines
xen/arch/arm/tee/ffa.c +32 -24
+    const char *name;
+};
+
+#define FW_ABI(abi) {abi,#abi}
+
+/* List of ABI we use from the firmware */
+static const struct ffa_fw_abi ffa_fw_abi_needed[] = {
+    FW_ABI(FFA_VERSION),
+    FW_ABI(FFA_FEATURES),
+    FW_ABI(FFA_NOTIFICATION_BITMAP_CREATE),
+    FW_ABI(FFA_NOTIFICATION_BITMAP_DESTROY),
+    FW_ABI(FFA_PARTITION_INFO_GET),
+    FW_ABI(FFA_NOTIFICATION_INFO_GET_64),
+    FW_ABI(FFA_NOTIFICATION_GET),
+    FW_ABI(FFA_RX_RELEASE),
+    FW_ABI(FFA_RXTX_MAP_64),
+    FW_ABI(FFA_RXTX_UNMAP),
+    FW_ABI(FFA_MEM_SHARE_32),
+    FW_ABI(FFA_MEM_SHARE_64),
+    FW_ABI(FFA_MEM_RECLAIM),
+    FW_ABI(FFA_MSG_SEND_DIRECT_REQ_32),
+    FW_ABI(FFA_MSG_SEND_DIRECT_REQ_64),
+};
 
 /*
  * Our rx/tx buffers shared with the SPMC. FFA_RXTX_PAGE_COUNT is the
@@ -112,20 +137,9 @@ static bool ffa_get_version(uint32_t *vers)
     return true;
 }
 
-static int32_t ffa_features(uint32_t id)
-{
-    return ffa_simple_call(FFA_FEATURES, id, 0, 0, 0);
-}
-
-static bool check_mandatory_feature(uint32_t id)
+static bool ffa_abi_supported(uint32_t id)
 {
-    int32_t ret = ffa_features(id);
-
-    if ( ret )
-        printk(XENLOG_ERR "ffa: mandatory feature id %#x missing: error %d\n",
-               id, ret);
-
-    return !ret;
+    return !ffa_simple_call(FFA_FEATURES, id, 0, 0, 0);
 }
 
 static void handle_version(struct cpu_user_regs *regs)
@@ -540,17 +554,14 @@ static bool ffa_probe(void)
      * TODO: Rework the code to allow domain to use a subset of the
      * features supported.
      */
-    if ( !check_mandatory_feature(FFA_PARTITION_INFO_GET) ||
-         !check_mandatory_feature(FFA_RX_RELEASE) ||
-         !check_mandatory_feature(FFA_RXTX_MAP_64) ||
-         !check_mandatory_feature(FFA_MEM_SHARE_64) ||
-         !check_mandatory_feature(FFA_RXTX_UNMAP) ||
-         !check_mandatory_feature(FFA_MEM_SHARE_32) ||
-         !check_mandatory_feature(FFA_MEM_RECLAIM) ||
-         !check_mandatory_feature(FFA_MSG_SEND_DIRECT_REQ_32) )
+    for ( unsigned int i = 0; i < ARRAY_SIZE(ffa_fw_abi_needed); i++ )
     {
-        printk(XENLOG_ERR "ffa: Mandatory feature not supported by fw\n");
-        goto err_no_fw;
+        if ( !ffa_abi_supported(ffa_fw_abi_needed[i].id) )
+        {
+            printk(XENLOG_INFO "ARM FF-A Firmware does not support %s\n",
+                   ffa_fw_abi_needed[i].name);
+            goto err_no_fw;
+        }
     }
 
     ffa_fw_version = vers;
--

2.47.0

Reviewed-by on v2 3/10
#9 lore raw

Re: [PATCH v2 03/10] xen/arm: ffa: Fix version negotiation

In reply to Bertrand Marquis

Hi Bertrand,

Quoted 40 lines · Bertrand Marquis

Fix FFA version negotiation with the firmware to follow the specification guidance more closely (see FF-A Specification Version 1.1 in chapter 13.2.1).
When the firmware returns OK we can have several cases:
- the version requested is accepted but the firmware supports a greater one in the same major.
- the firmware supports a greater major version. It could still return OK even if the version requested is not accepted. Reject it.
- the firmware supports a lower version. It will return OK and give that version. Check if we support it and use it or reject it if we do not.

Adapt the code to:
- reject any version lower than the one we support or not with the same major version
- use the version returned if in our supported range (currently 1.1 only)
- use 1.1 if the version returned is greater.

Also adapt the handling of version requests from VM:
- use our version if same major but greater minor is requested
- use requested version if same major but lower minor is requested
- do not use if incompatible major is requested
- always return our version without error to the requester

[1] https://developer.arm.com/documentation/den0077/e/

---
Changes in v2:
- add link in commit message and code to the version and chapter of the spec.
- fix comment coding style
- introduce macros to get the major and minor of a version
- add BUILD_BUG_ON to validate that the SPMC version we want is compatible with our own version
- rework version testing and selecting to be clearer by discarding different major or version lower than what we want and then selecting based on the minor version.
- fix get_version handling to be more generic

Quoted 4 lines
---
 xen/arch/arm/tee/ffa.c         | 54 +++++++++++++++++++++++++---------
 xen/arch/arm/tee/ffa_private.h |  3 ++
 2 files changed, 43 insertions(+), 14 deletions(-)

Looks good.

Cheers,
Jens

Quoted 104 lines
xen/arch/arm/tee/ffa.c +40 -14
index dde932422ecf..1ee6b2895e92 100644
@@ -148,13 +148,20 @@ static void handle_version(struct cpu_user_regs *regs)
     struct ffa_ctx *ctx = d->arch.tee;
     uint32_t vers = get_user_reg(regs, 1);
 
-    if ( vers < FFA_VERSION_1_1 )
-        vers = FFA_VERSION_1_0;
-    else
-        vers = FFA_VERSION_1_1;
-
-    ctx->guest_vers = vers;
-    ffa_set_regs(regs, vers, 0, 0, 0, 0, 0, 0, 0);
+    /*
+     * Guest will use the version it requested if it is our major and minor
+     * lower or equals to ours. If the minor is greater, our version will be
+     * used.
+     * In any case return our version to the caller.
+     */
+    if ( FFA_VERSION_MAJOR(vers) == FFA_MY_VERSION_MAJOR )
+    {
+        if ( FFA_VERSION_MINOR(vers) > FFA_MY_VERSION_MINOR )
+            ctx->guest_vers = FFA_MY_VERSION;
+        else
+            ctx->guest_vers = vers;
+    }
+    ffa_set_regs(regs, FFA_MY_VERSION, 0, 0, 0, 0, 0, 0, 0);
 }
 
 static void handle_msg_send_direct_req(struct cpu_user_regs *regs, uint32_t fid)
@@ -537,18 +544,39 @@ static bool ffa_probe(void)
         goto err_no_fw;
     }
 
-    if ( vers < FFA_MIN_SPMC_VERSION || vers > FFA_MY_VERSION )
+    /* Some sanity check in case we update the version we support */
+    BUILD_BUG_ON(FFA_MIN_SPMC_VERSION > FFA_MY_VERSION);
+    BUILD_BUG_ON(FFA_VERSION_MAJOR(FFA_MIN_SPMC_VERSION) !=
+                                   FFA_MY_VERSION_MAJOR);
+
+    major_vers = FFA_VERSION_MAJOR(vers);
+    minor_vers = FFA_VERSION_MINOR(vers);
+
+    if ( major_vers != FFA_MY_VERSION_MAJOR ||
+         minor_vers < FFA_VERSION_MINOR(FFA_MIN_SPMC_VERSION) )
     {
-        printk(XENLOG_ERR "ffa: Incompatible version %#x found\n", vers);
+        printk(XENLOG_ERR "ffa: Incompatible firmware version %u.%u\n",
+               major_vers, minor_vers);
         goto err_no_fw;
     }
 
-    major_vers = (vers >> FFA_VERSION_MAJOR_SHIFT)
-                 & FFA_VERSION_MAJOR_MASK;
-    minor_vers = vers & FFA_VERSION_MINOR_MASK;
     printk(XENLOG_INFO "ARM FF-A Firmware version %u.%u\n",
            major_vers, minor_vers);
 
+    /*
+     * If the call succeed and the version returned is higher or equal to
+     * the one Xen requested, the version requested by Xen will be the one
+     * used. If the version returned is lower but compatible with Xen, Xen
+     * will use that version instead.
+     * A version with a different major or lower than the minimum version
+     * we support is rejected before.
+     * See https://developer.arm.com/documentation/den0077/e/ chapter 13.2.1
+     */
+    if ( minor_vers > FFA_MY_VERSION_MINOR )
+        ffa_fw_version = FFA_MY_VERSION;
+    else
+        ffa_fw_version = vers;
+
     /*
      * At the moment domains must support the same features used by Xen.
      * TODO: Rework the code to allow domain to use a subset of the
@@ -564,8 +592,6 @@ static bool ffa_probe(void)
         }
     }
 
-    ffa_fw_version = vers;
-
     if ( !ffa_rxtx_init() )
     {
         printk(XENLOG_ERR "ffa: Error during RXTX buffer init\n");
xen/arch/arm/tee/ffa_private.h +3 -1
index 7c6b06f686fc..045d9c4a0b56 100644
@@ -35,6 +35,9 @@
 #define MAKE_FFA_VERSION(major, minor)  \
         ((((major) & FFA_VERSION_MAJOR_MASK) << FFA_VERSION_MAJOR_SHIFT) | \
          ((minor) & FFA_VERSION_MINOR_MASK))
+#define FFA_VERSION_MAJOR(vers) (((vers) >> FFA_VERSION_MAJOR_SHIFT) & \
+                                 FFA_VERSION_MAJOR_MASK)
+#define FFA_VERSION_MINOR(vers) ((vers) & FFA_VERSION_MINOR_MASK)
 
 #define FFA_VERSION_1_0         MAKE_FFA_VERSION(1, 0)
 #define FFA_VERSION_1_1         MAKE_FFA_VERSION(1, 1)
--

2.47.0

#4 lore raw

Re: [PATCH v2 01/10] xen/arm: ffa: Rework firmware discovery

In reply to Jens Wiklander

Hi Jens,

Quoted 94 lines · Jens Wiklander

Hi Bertrand,

Quoted 88 lines · Bertrand Marquis

Rework firmware discovery during probe:
- move prints into the probe
- rename ffa_version to ffa_fw_version as the variable identifies the version of the firmware and not the one we support
- add error prints when allocation fail during probe

No functional changes.

---
Changes in v2:
- Fix error message when we fail to retrieve ffa_version
- Move back printing the firmware version before checking supported features
- Use Warning instead of Info to inform user that FF-A is not supported in firmware.
---
xen/arch/arm/tee/ffa.c | 44 ++++++++++++++++++++++++++----------------

1 file changed, 27 insertions(+), 17 deletions(-)
xen/arch/arm/tee/ffa.c +12 -9
index 022089278e1c..1cc4023135d5 100644
@@ -71,8 +71,8 @@
 
 #include "ffa_private.h"
 
-/* Negotiated FF-A version to use with the SPMC */
-static uint32_t __ro_after_init ffa_version;
+/* Negotiated FF-A version to use with the SPMC, 0 if not there or supported */
+static uint32_t __ro_after_init ffa_fw_version;
 
 
 /*
@@ -105,10 +105,7 @@ static bool ffa_get_version(uint32_t *vers)
 
    arm_smccc_1_2_smc(&arg, &resp);
    if ( resp.a0 == FFA_RET_NOT_SUPPORTED )
-    {
-        gprintk(XENLOG_ERR, "ffa: FFA_VERSION returned not supported\n");
        return false;
-    }
 
    *vers = resp.a0;
 
@@ -372,7 +369,7 @@ static int ffa_domain_init(struct domain *d)
    struct ffa_ctx *ctx;
    int ret;
 
-    if ( !ffa_version )
+    if ( !ffa_fw_version )
        return -ENODEV;
     /*
      * We can't use that last possible domain ID or ffa_get_vm_id() would
@@ -505,6 +502,9 @@ static bool ffa_probe(void)
     */
    BUILD_BUG_ON(PAGE_SIZE != FFA_PAGE_SIZE);
 
+    printk(XENLOG_INFO "ARM FF-A Mediator version %u.%u\n",
+           FFA_MY_VERSION_MAJOR, FFA_MY_VERSION_MINOR);
+
    /*
     * psci_init_smccc() updates this value with what's reported by EL-3
     * or secure world.
@@ -514,22 +514,24 @@ static bool ffa_probe(void)
        printk(XENLOG_ERR
               "ffa: unsupported SMCCC version %#x (need at least %#x)\n",
               smccc_ver, ARM_SMCCC_VERSION_1_2);
-        return false;
+        goto err_no_fw;
    }
 
    if ( !ffa_get_version(&vers) )
-        return false;
+    {
+        gprintk(XENLOG_ERR, "Cannot retrieve the FFA version\n");
+        goto err_no_fw;
+    }
 
    if ( vers < FFA_MIN_SPMC_VERSION || vers > FFA_MY_VERSION )
    {
        printk(XENLOG_ERR "ffa: Incompatible version %#x found\n", vers);
-        return false;
+        goto err_no_fw;
    }
 
Quoted 5 lines

xen/arch/arm/tee/ffa.c static bool ffa_probe(void) · line 533

Quoted 3 lines
xen/arch/arm/tee/ffa.c +2 -1
-    major_vers = (vers >> FFA_VERSION_MAJOR_SHIFT) & FFA_VERSION_MAJOR_MASK;
+    major_vers = (vers >> FFA_VERSION_MAJOR_SHIFT)
+                 & FFA_VERSION_MAJOR_MASK;

Spurious change?

Yes, I will fix that in next version.

Quoted 8 lines

xen/arch/arm/tee/ffa.c static bool ffa_probe(void) · line 532

Quoted 4 lines
xen/arch/arm/tee/ffa.c +0 -2
    minor_vers = vers & FFA_VERSION_MINOR_MASK;
-    printk(XENLOG_INFO "ARM FF-A Mediator version %u.%u\n",
-           FFA_MY_VERSION_MAJOR, FFA_MY_VERSION_MINOR);

It's not a big deal, but isn't it useful to know which version we're at? If it's too much with a separate line, how about adding "(our version %u.u%)" at the end of the line below?

This was moved up.

Quoted 18 lines
Quoted 18 lines
xen/arch/arm/tee/ffa.c +5 -3
    printk(XENLOG_INFO "ARM FF-A Firmware version %u.%u\n",
           major_vers, minor_vers);
 
@@ -546,12 +548,18 @@ static bool ffa_probe(void)
         !check_mandatory_feature(FFA_MEM_SHARE_32) ||
         !check_mandatory_feature(FFA_MEM_RECLAIM) ||
         !check_mandatory_feature(FFA_MSG_SEND_DIRECT_REQ_32) )
-        return false;
+    {
+        printk(XENLOG_ERR "ffa: Mandatory feature not supported by fw\n");
+        goto err_no_fw;
+    }
 
-    if ( !ffa_rxtx_init() )
-        return false;
+    ffa_fw_version = vers;
 
Quoted 7 lines

xen/arch/arm/tee/ffa.c static bool ffa_probe(void) · line 560

Quoted 4 lines
xen/arch/arm/tee/ffa.c +3 -1
-    ffa_version = vers;
+    if ( !ffa_rxtx_init() )
+    {
+        printk(XENLOG_ERR "ffa: Error during RXTX buffer init\n");

With this added, wouldn't it make sense to remove the error print in ffa_rxtx_init()?

Definitely, I missed that.
Will fix in v3.

Cheers
Bertrand

Quoted 22 lines

Cheers,
Jens

Quoted 18 lines
xen/arch/arm/tee/ffa.c +2 -0
+        goto err_no_fw;
+    }
 
    if ( !ffa_partinfo_init() )
        goto err_rxtx_destroy;
@@ -564,7 +572,9 @@ static bool ffa_probe(void)
 

err_rxtx_destroy:

    ffa_rxtx_destroy();
xen/arch/arm/tee/ffa.c +3 -1
-    ffa_version = 0;
+err_no_fw:
+    ffa_fw_version = 0;
+    printk(XENLOG_WARNING "ARM FF-A No firmware support\n");
 
    return false;

}
--
2.47.0

#7 lore raw

Re: [PATCH v2 02/10] xen/arm: ffa: Rework feature discovery

In reply to Jens Wiklander

Hi Jens,

Quoted 34 lines · Jens Wiklander

Hi Bertrand,

Quoted 28 lines · Bertrand Marquis

Store the list of ABI we need in a list and go through the list instead of having a list of conditions inside the code.

No functional change.

---
Changes in v2:
- Store a string version of ABI needed from firmware and print the name of the ABI not supported instead of the id
- Restore comment with TODO which should not have been removed at this stage
- fix to unsigned int the index in the loop on the array
- use abi instead of feature in the names of the functions and variables as we are not checking features but abis
---
xen/arch/arm/tee/ffa.c | 57 +++++++++++++++++++++++++-----------------

1 file changed, 34 insertions(+), 23 deletions(-)
xen/arch/arm/tee/ffa.c +0 -0
index 1cc4023135d5..dde932422ecf 100644
@@ -74,6 +74,31 @@
 /* Negotiated FF-A version to use with the SPMC, 0 if not there or supported */
 static uint32_t __ro_after_init ffa_fw_version;
 
Quoted 5 lines

xen/arch/arm/tee/ffa.c line 78

Quoted 2 lines
xen/arch/arm/tee/ffa.c +2 -0
+struct ffa_fw_abi {
+    const uint32_t id;

I prefer removing the const attribute for this struct member since it doesn't add anything when the struct itself is const.

Works for me.
Will fix in v3.

Cheers
Bertrand

Quoted 80 lines

Cheers,
Jens

Quoted 76 lines
xen/arch/arm/tee/ffa.c +32 -24
+    const char *name;
+};
+
+#define FW_ABI(abi) {abi,#abi}
+
+/* List of ABI we use from the firmware */
+static const struct ffa_fw_abi ffa_fw_abi_needed[] = {
+    FW_ABI(FFA_VERSION),
+    FW_ABI(FFA_FEATURES),
+    FW_ABI(FFA_NOTIFICATION_BITMAP_CREATE),
+    FW_ABI(FFA_NOTIFICATION_BITMAP_DESTROY),
+    FW_ABI(FFA_PARTITION_INFO_GET),
+    FW_ABI(FFA_NOTIFICATION_INFO_GET_64),
+    FW_ABI(FFA_NOTIFICATION_GET),
+    FW_ABI(FFA_RX_RELEASE),
+    FW_ABI(FFA_RXTX_MAP_64),
+    FW_ABI(FFA_RXTX_UNMAP),
+    FW_ABI(FFA_MEM_SHARE_32),
+    FW_ABI(FFA_MEM_SHARE_64),
+    FW_ABI(FFA_MEM_RECLAIM),
+    FW_ABI(FFA_MSG_SEND_DIRECT_REQ_32),
+    FW_ABI(FFA_MSG_SEND_DIRECT_REQ_64),
+};
 
 /*
 * Our rx/tx buffers shared with the SPMC. FFA_RXTX_PAGE_COUNT is the
@@ -112,20 +137,9 @@ static bool ffa_get_version(uint32_t *vers)
    return true;
 }
 
-static int32_t ffa_features(uint32_t id)
-{
-    return ffa_simple_call(FFA_FEATURES, id, 0, 0, 0);
-}
-
-static bool check_mandatory_feature(uint32_t id)
+static bool ffa_abi_supported(uint32_t id)
 {
-    int32_t ret = ffa_features(id);
-
-    if ( ret )
-        printk(XENLOG_ERR "ffa: mandatory feature id %#x missing: error %d\n",
-               id, ret);
-
-    return !ret;
+    return !ffa_simple_call(FFA_FEATURES, id, 0, 0, 0);
 }
 
 static void handle_version(struct cpu_user_regs *regs)
@@ -540,17 +554,14 @@ static bool ffa_probe(void)
     * TODO: Rework the code to allow domain to use a subset of the
     * features supported.
     */
-    if ( !check_mandatory_feature(FFA_PARTITION_INFO_GET) ||
-         !check_mandatory_feature(FFA_RX_RELEASE) ||
-         !check_mandatory_feature(FFA_RXTX_MAP_64) ||
-         !check_mandatory_feature(FFA_MEM_SHARE_64) ||
-         !check_mandatory_feature(FFA_RXTX_UNMAP) ||
-         !check_mandatory_feature(FFA_MEM_SHARE_32) ||
-         !check_mandatory_feature(FFA_MEM_RECLAIM) ||
-         !check_mandatory_feature(FFA_MSG_SEND_DIRECT_REQ_32) )
+    for ( unsigned int i = 0; i < ARRAY_SIZE(ffa_fw_abi_needed); i++ )
    {
-        printk(XENLOG_ERR "ffa: Mandatory feature not supported by fw\n");
-        goto err_no_fw;
+        if ( !ffa_abi_supported(ffa_fw_abi_needed[i].id) )
+        {
+            printk(XENLOG_INFO "ARM FF-A Firmware does not support %s\n",
+                   ffa_fw_abi_needed[i].name);
+            goto err_no_fw;
+        }
    }
 
    ffa_fw_version = vers;
--

2.47.0

Reviewed-by on v2 4/10
#11 lore raw

Re: [PATCH v2 04/10] xen/arm: ffa: Fine granular call support

In reply to Bertrand Marquis

Hi Bertrand,

Quoted 24 lines · Bertrand Marquis

Create a bitmap to store which feature is supported or not by the firmware and use it to filter which calls are done to the firmware.

While there reoder ABI definition by numbers to easily find the min and max ones.

---
Changes in v2:
- rename fw_feat to abi and macros to FFA_ABI to be coherent with the abi needed change done before
- rework the macros to be simpler by directly defining MIN and MAX using only Function ids
- check that requested function ids do not go over the bitmap size in ffa_fw_supports_fid
- add an ASSERT to make sure that we do not try to set bits outside of the bitmap
- turn off FF-A if there is not firmware support and adapt the commit message to reflect this
- add a compile time check that FFA_ABI_MIN < FFA_ABI_MAX
- remove spurious line removal
- restore proper cleanup of rxtx init in case of error
- reorder ABI by numbers

Quoted 8 lines
---
 xen/arch/arm/tee/ffa.c          | 28 +++++++++++++++---------
 xen/arch/arm/tee/ffa_notif.c    |  7 ++++++
 xen/arch/arm/tee/ffa_partinfo.c | 30 +++++++++++++++++++++++++-
 xen/arch/arm/tee/ffa_private.h  | 38 ++++++++++++++++++++++++++++-----
 xen/arch/arm/tee/ffa_rxtx.c     |  4 ++++
 xen/arch/arm/tee/ffa_shm.c      | 12 +++++++++++
 6 files changed, 103 insertions(+), 16 deletions(-)

Looks good.

Cheers,
Jens

Quoted 291 lines
xen/arch/arm/tee/ffa.c +18 -10
index 1ee6b2895e92..267d4435ac08 100644
@@ -72,7 +72,10 @@
 #include "ffa_private.h"
 
 /* Negotiated FF-A version to use with the SPMC, 0 if not there or supported */
-static uint32_t __ro_after_init ffa_fw_version;
+uint32_t __ro_after_init ffa_fw_version;
+
+/* Features supported by the SPMC or secure world when present */
+DECLARE_BITMAP(ffa_fw_abi_supported, FFA_ABI_BITMAP_SIZE);
 
 struct ffa_fw_abi {
     const uint32_t id;
@@ -177,6 +180,13 @@ static void handle_msg_send_direct_req(struct cpu_user_regs *regs, uint32_t fid)
     else
         mask = GENMASK_ULL(31, 0);
 
+    if ( !ffa_fw_supports_fid(fid) )
+    {
+        resp.a0 = FFA_ERROR;
+        resp.a2 = FFA_RET_NOT_SUPPORTED;
+        goto out;
+    }
+
     src_dst = get_user_reg(regs, 1);
     if ( (src_dst >> 16) != ffa_get_vm_id(d) )
     {
@@ -577,19 +587,16 @@ static bool ffa_probe(void)
     else
         ffa_fw_version = vers;
 
-    /*
-     * At the moment domains must support the same features used by Xen.
-     * TODO: Rework the code to allow domain to use a subset of the
-     * features supported.
-     */
     for ( unsigned int i = 0; i < ARRAY_SIZE(ffa_fw_abi_needed); i++ )
     {
-        if ( !ffa_abi_supported(ffa_fw_abi_needed[i].id) )
-        {
+        ASSERT(FFA_ABI_BITNUM(ffa_fw_abi_needed[i].id) < FFA_ABI_BITMAP_SIZE);
+
+        if ( ffa_abi_supported(ffa_fw_abi_needed[i].id) )
+            set_bit(FFA_ABI_BITNUM(ffa_fw_abi_needed[i].id),
+                    ffa_fw_abi_supported);
+        else
             printk(XENLOG_INFO "ARM FF-A Firmware does not support %s\n",
                    ffa_fw_abi_needed[i].name);
-            goto err_no_fw;
-        }
     }
 
     if ( !ffa_rxtx_init() )
@@ -611,6 +618,7 @@ err_rxtx_destroy:
     ffa_rxtx_destroy();
 err_no_fw:
     ffa_fw_version = 0;
+    bitmap_zero(ffa_fw_abi_supported, FFA_ABI_BITMAP_SIZE);
     printk(XENLOG_WARNING "ARM FF-A No firmware support\n");
 
     return false;
xen/arch/arm/tee/ffa_notif.c +7 -0
index 541e61d2f606..4b3e46318f4b 100644
@@ -377,6 +377,13 @@ void ffa_notif_init(void)
     unsigned int irq;
     int ret;
 
+    /* Only enable fw notification if all ABIs we need are supported */
+    if ( !(ffa_fw_supports_fid(FFA_NOTIFICATION_BITMAP_CREATE) &&
+           ffa_fw_supports_fid(FFA_NOTIFICATION_BITMAP_DESTROY) &&
+           ffa_fw_supports_fid(FFA_NOTIFICATION_GET) &&
+           ffa_fw_supports_fid(FFA_NOTIFICATION_INFO_GET_64)) )
+        return;
+
     arm_smccc_1_2_smc(&arg, &resp);
     if ( resp.a0 != FFA_SUCCESS_32 )
         return;
xen/arch/arm/tee/ffa_partinfo.c +29 -1
index 93a03c6bc672..99c48f0e5c05 100644
@@ -77,7 +77,15 @@ int32_t ffa_handle_partition_info_get(uint32_t w1, uint32_t w2, uint32_t w3,
      */
     if ( w5 == FFA_PARTITION_INFO_GET_COUNT_FLAG &&
          ctx->guest_vers == FFA_VERSION_1_1 )
-        return ffa_partition_info_get(w1, w2, w3, w4, w5, count, fpi_size);
+    {
+        if ( ffa_fw_supports_fid(FFA_PARTITION_INFO_GET) )
+            return ffa_partition_info_get(w1, w2, w3, w4, w5, count, fpi_size);
+        else
+        {
+            *count = 0;
+            return FFA_RET_OK;
+        }
+    }
     if ( w5 )
         return FFA_RET_INVALID_PARAMETERS;
 
@@ -87,6 +95,18 @@ int32_t ffa_handle_partition_info_get(uint32_t w1, uint32_t w2, uint32_t w3,
     if ( !spin_trylock(&ctx->rx_lock) )
         return FFA_RET_BUSY;
 
+    if ( !ffa_fw_supports_fid(FFA_PARTITION_INFO_GET) )
+    {
+        if ( ctx->guest_vers == FFA_VERSION_1_0 )
+            *fpi_size = sizeof(struct ffa_partition_info_1_0);
+        else
+            *fpi_size = sizeof(struct ffa_partition_info_1_1);
+
+        *count = 0;
+        ret = FFA_RET_OK;
+        goto out;
+    }
+
     if ( !ctx->page_count || !ctx->rx_is_free )
         goto out;
     spin_lock(&ffa_rx_buffer_lock);
@@ -250,6 +270,11 @@ bool ffa_partinfo_init(void)
     uint32_t count;
     int e;
 
+    if ( !ffa_fw_supports_fid(FFA_PARTITION_INFO_GET) ||
+         !ffa_fw_supports_fid(FFA_MSG_SEND_DIRECT_REQ_32) ||
+         !ffa_rx || !ffa_tx )
+        return false;
+
     e = ffa_partition_info_get(0, 0, 0, 0, 0, &count, &fpi_size);
     if ( e )
     {
@@ -313,6 +338,9 @@ int ffa_partinfo_domain_init(struct domain *d)
     unsigned int n;
     int32_t res;
 
+    if ( !ffa_fw_supports_fid(FFA_MSG_SEND_DIRECT_REQ_32) )
+        return 0;
+
     ctx->vm_destroy_bitmap = xzalloc_array(unsigned long, count);
     if ( !ctx->vm_destroy_bitmap )
         return -ENOMEM;
xen/arch/arm/tee/ffa_private.h +33 -5
index 045d9c4a0b56..85eb61c13464 100644
@@ -14,6 +14,7 @@
 #include <xen/spinlock.h>
 #include <xen/sched.h>
 #include <xen/time.h>
+#include <xen/bitmap.h>
 
 /* Error codes */
 #define FFA_RET_OK                      0
@@ -201,18 +202,17 @@
 #define FFA_INTERRUPT                   0x84000062U
 #define FFA_VERSION                     0x84000063U
 #define FFA_FEATURES                    0x84000064U
-#define FFA_RX_ACQUIRE                  0x84000084U
 #define FFA_RX_RELEASE                  0x84000065U
 #define FFA_RXTX_MAP_32                 0x84000066U
 #define FFA_RXTX_MAP_64                 0xC4000066U
 #define FFA_RXTX_UNMAP                  0x84000067U
 #define FFA_PARTITION_INFO_GET          0x84000068U
 #define FFA_ID_GET                      0x84000069U
-#define FFA_SPM_ID_GET                  0x84000085U
+#define FFA_MSG_POLL                    0x8400006AU
 #define FFA_MSG_WAIT                    0x8400006BU
 #define FFA_MSG_YIELD                   0x8400006CU
 #define FFA_RUN                         0x8400006DU
-#define FFA_MSG_SEND2                   0x84000086U
+#define FFA_MSG_SEND                    0x8400006EU
 #define FFA_MSG_SEND_DIRECT_REQ_32      0x8400006FU
 #define FFA_MSG_SEND_DIRECT_REQ_64      0xC400006FU
 #define FFA_MSG_SEND_DIRECT_RESP_32     0x84000070U
@@ -230,8 +230,6 @@
 #define FFA_MEM_RECLAIM                 0x84000077U
 #define FFA_MEM_FRAG_RX                 0x8400007AU
 #define FFA_MEM_FRAG_TX                 0x8400007BU
-#define FFA_MSG_SEND                    0x8400006EU
-#define FFA_MSG_POLL                    0x8400006AU
 #define FFA_NOTIFICATION_BITMAP_CREATE  0x8400007DU
 #define FFA_NOTIFICATION_BITMAP_DESTROY 0x8400007EU
 #define FFA_NOTIFICATION_BIND           0x8400007FU
@@ -240,6 +238,25 @@
 #define FFA_NOTIFICATION_GET            0x84000082U
 #define FFA_NOTIFICATION_INFO_GET_32    0x84000083U
 #define FFA_NOTIFICATION_INFO_GET_64    0xC4000083U
+#define FFA_RX_ACQUIRE                  0x84000084U
+#define FFA_SPM_ID_GET                  0x84000085U
+#define FFA_MSG_SEND2                   0x84000086U
+
+/**
+ * Encoding of features supported or not by the fw in a bitmap:
+ * - Function IDs are going from 0x60 to 0xFF
+ * - A function can be supported in 32 and/or 64bit
+ * The bitmap has one bit for each function in 32 and 64 bit.
+ */
+#define FFA_ABI_ID(id)        ((id) & ARM_SMCCC_FUNC_MASK)
+#define FFA_ABI_CONV(id)      (((id) >> ARM_SMCCC_CONV_SHIFT) & BIT(0,U))
+
+#define FFA_ABI_MIN           FFA_ABI_ID(FFA_ERROR)
+#define FFA_ABI_MAX           FFA_ABI_ID(FFA_MSG_SEND2)
+
+#define FFA_ABI_BITMAP_SIZE   (2 * (FFA_ABI_MAX - FFA_ABI_MIN + 1))
+#define FFA_ABI_BITNUM(id)    ((FFA_ABI_ID(id) - FFA_ABI_MIN) << 1 | \
+                               FFA_ABI_CONV(id))
 
 struct ffa_ctx_notif {
     bool enabled;
@@ -289,6 +306,8 @@ extern void *ffa_rx;
 extern void *ffa_tx;
 extern spinlock_t ffa_rx_buffer_lock;
 extern spinlock_t ffa_tx_buffer_lock;
+extern uint32_t __ro_after_init ffa_fw_version;
+extern DECLARE_BITMAP(ffa_fw_abi_supported, FFA_ABI_BITMAP_SIZE);
 
 bool ffa_shm_domain_destroy(struct domain *d);
 void ffa_handle_mem_share(struct cpu_user_regs *regs);
@@ -401,4 +420,13 @@ static inline int32_t ffa_rx_release(void)
     return ffa_simple_call(FFA_RX_RELEASE, 0, 0, 0, 0);
 }
 
+static inline bool ffa_fw_supports_fid(uint32_t fid)
+{
+    BUILD_BUG_ON(FFA_ABI_MIN > FFA_ABI_MAX);
+
+    if ( FFA_ABI_BITNUM(fid) > FFA_ABI_BITMAP_SIZE)
+        return false;
+    return test_bit(FFA_ABI_BITNUM(fid), ffa_fw_abi_supported);
+}
+
 #endif /*__FFA_PRIVATE_H__*/
xen/arch/arm/tee/ffa_rxtx.c +4 -0
index 661764052e67..b6931c855779 100644
@@ -193,6 +193,10 @@ bool ffa_rxtx_init(void)
 {
     int e;
 
+    /* Firmware not there or not supporting */
+    if ( !ffa_fw_supports_fid(FFA_RXTX_MAP_64) )
+        return false;
+
     ffa_rx = alloc_xenheap_pages(get_order_from_pages(FFA_RXTX_PAGE_COUNT), 0);
     if ( !ffa_rx )
         return false;
xen/arch/arm/tee/ffa_shm.c +12 -1
index 370d83ec5cf8..efa5b67db8e1 100644
@@ -149,6 +149,9 @@ static int32_t ffa_mem_share(uint32_t tot_len, uint32_t frag_len,
 static int32_t ffa_mem_reclaim(uint32_t handle_lo, uint32_t handle_hi,
                                uint32_t flags)
 {
+    if ( !ffa_fw_supports_fid(FFA_MEM_RECLAIM) )
+        return FFA_RET_NOT_SUPPORTED;
+
     return ffa_simple_call(FFA_MEM_RECLAIM, handle_lo, handle_hi, flags, 0);
 }
 
@@ -467,6 +470,12 @@ void ffa_handle_mem_share(struct cpu_user_regs *regs)
     uint32_t range_count;
     uint32_t region_offs;
 
+    if ( !ffa_fw_supports_fid(FFA_MEM_SHARE_64) )
+    {
+        ret = FFA_RET_NOT_SUPPORTED;
+        goto out_set_ret;
+    }
+
     /*
      * We're only accepting memory transaction descriptors via the rx/tx
      * buffer.
@@ -621,6 +630,9 @@ int ffa_handle_mem_reclaim(uint64_t handle, uint32_t flags)
     register_t handle_lo;
     int ret;
 
+    if ( !ffa_fw_supports_fid(FFA_MEM_RECLAIM) )
+        return FFA_RET_NOT_SUPPORTED;
+
     spin_lock(&ctx->lock);
     shm = find_shm_mem(ctx, handle);
     if ( shm )
--

2.47.0

Reviewed-by on v2 5/10
#16 lore raw

Re: [PATCH v2 05/10] xen/arm: ffa: Rework partition info get

In reply to Bertrand Marquis

Hi Bertrand,

Quoted 16 lines · Bertrand Marquis

Rework the partition info get implementation to use the correct size of structure depending on the version of the protocol and simplifies the structure copy to use only memcpy and prevent recreating the structure each time.
The goal here is to have an implementation that will be easier to maintain in the long term as the specification is only adding fields to structure with versions to simplify support of several protocol versions and as such an SPMC implementation in the future could use this and return a size higher than the one we expect.
The patch is fixing the part_info_get function for this and the subscriber discovery on probe.

No functional changes expected.

Quoted 8 lines

---
Changes in v2:
- rebase

---
 xen/arch/arm/tee/ffa.c          |  13 +--
 xen/arch/arm/tee/ffa_partinfo.c | 185 ++++++++++++++++++++------------
 xen/arch/arm/tee/ffa_private.h  |   4 +-
 3 files changed, 118 insertions(+), 84 deletions(-)

Looks good.

Cheers,
Jens

Quoted 344 lines
xen/arch/arm/tee/ffa.c +1 -12
index 267d4435ac08..4b55e8b48f01 100644
@@ -311,8 +311,6 @@ static bool ffa_handle_call(struct cpu_user_regs *regs)
     uint32_t fid = get_user_reg(regs, 0);
     struct domain *d = current->domain;
     struct ffa_ctx *ctx = d->arch.tee;
-    uint32_t fpi_size;
-    uint32_t count;
     int e;
 
     if ( !ctx )
@@ -338,16 +336,7 @@ static bool ffa_handle_call(struct cpu_user_regs *regs)
         e = ffa_handle_rxtx_unmap();
         break;
     case FFA_PARTITION_INFO_GET:
-        e = ffa_handle_partition_info_get(get_user_reg(regs, 1),
-                                          get_user_reg(regs, 2),
-                                          get_user_reg(regs, 3),
-                                          get_user_reg(regs, 4),
-                                          get_user_reg(regs, 5), &count,
-                                          &fpi_size);
-        if ( e )
-            ffa_set_regs_error(regs, e);
-        else
-            ffa_set_regs_success(regs, count, fpi_size);
+        ffa_handle_partition_info_get(regs);
         return true;
     case FFA_RX_RELEASE:
         e = ffa_handle_rx_release();
xen/arch/arm/tee/ffa_partinfo.c +116 -69
index 99c48f0e5c05..75a073d090e0 100644
@@ -33,21 +33,24 @@ static uint16_t subscr_vm_created_count __read_mostly;
 static uint16_t *subscr_vm_destroyed __read_mostly;
 static uint16_t subscr_vm_destroyed_count __read_mostly;
 
-static int32_t ffa_partition_info_get(uint32_t w1, uint32_t w2, uint32_t w3,
-                                      uint32_t w4, uint32_t w5, uint32_t *count,
-                                      uint32_t *fpi_size)
+static int32_t ffa_partition_info_get(uint32_t *uuid, uint32_t flags,
+                                      uint32_t *count, uint32_t *fpi_size)
 {
-    const struct arm_smccc_1_2_regs arg = {
+    struct arm_smccc_1_2_regs arg = {
         .a0 = FFA_PARTITION_INFO_GET,
-        .a1 = w1,
-        .a2 = w2,
-        .a3 = w3,
-        .a4 = w4,
-        .a5 = w5,
+        .a5 = flags,
     };
     struct arm_smccc_1_2_regs resp;
     uint32_t ret;
 
+    if ( uuid )
+    {
+        arg.a1 = uuid[0];
+        arg.a2 = uuid[1];
+        arg.a3 = uuid[2];
+        arg.a4 = uuid[3];
+    }
+
     arm_smccc_1_2_smc(&arg, &resp);
 
     ret = ffa_get_ret_code(&resp);
@@ -60,13 +63,31 @@ static int32_t ffa_partition_info_get(uint32_t w1, uint32_t w2, uint32_t w3,
     return ret;
 }
 
-int32_t ffa_handle_partition_info_get(uint32_t w1, uint32_t w2, uint32_t w3,
-                                      uint32_t w4, uint32_t w5, uint32_t *count,
-                                      uint32_t *fpi_size)
+void ffa_handle_partition_info_get(struct cpu_user_regs *regs)
 {
-    int32_t ret = FFA_RET_DENIED;
+    int32_t ret;
     struct domain *d = current->domain;
     struct ffa_ctx *ctx = d->arch.tee;
+    uint32_t flags = get_user_reg(regs, 5);
+    uint32_t uuid[4] = {
+        get_user_reg(regs, 1),
+        get_user_reg(regs, 2),
+        get_user_reg(regs, 3),
+        get_user_reg(regs, 4),
+    };
+    uint32_t src_size, dst_size;
+    void *dst_buf;
+    uint32_t ffa_sp_count = 0;
+
+    /*
+     * If the guest is v1.0, he does not get back the entry size so we must
+     * use the v1.0 structure size in the destination buffer.
+     * Otherwise use the size of the highest version we support, here 1.1.
+     */
+    if ( ctx->guest_vers == FFA_VERSION_1_0 )
+        dst_size = sizeof(struct ffa_partition_info_1_0);
+    else
+        dst_size = sizeof(struct ffa_partition_info_1_1);
 
     /*
      * FF-A v1.0 has w5 MBZ while v1.1 allows
@@ -75,90 +96,105 @@ int32_t ffa_handle_partition_info_get(uint32_t w1, uint32_t w2, uint32_t w3,
      * FFA_PARTITION_INFO_GET_COUNT is only using registers and not the
      * rxtx buffer so do the partition_info_get directly.
      */
-    if ( w5 == FFA_PARTITION_INFO_GET_COUNT_FLAG &&
+    if ( flags == FFA_PARTITION_INFO_GET_COUNT_FLAG &&
          ctx->guest_vers == FFA_VERSION_1_1 )
     {
         if ( ffa_fw_supports_fid(FFA_PARTITION_INFO_GET) )
-            return ffa_partition_info_get(w1, w2, w3, w4, w5, count, fpi_size);
+            ret = ffa_partition_info_get(uuid, flags, &ffa_sp_count,
+                                        &src_size);
         else
-        {
-            *count = 0;
-            return FFA_RET_OK;
-        }
-    }
-    if ( w5 )
-        return FFA_RET_INVALID_PARAMETERS;
+            ret = FFA_RET_OK;
 
-    if ( !ffa_rx )
-        return FFA_RET_DENIED;
+        goto out;
+    }
 
-    if ( !spin_trylock(&ctx->rx_lock) )
-        return FFA_RET_BUSY;
+    if ( flags )
+    {
+        ret = FFA_RET_INVALID_PARAMETERS;
+        goto out;
+    }
 
     if ( !ffa_fw_supports_fid(FFA_PARTITION_INFO_GET) )
     {
-        if ( ctx->guest_vers == FFA_VERSION_1_0 )
-            *fpi_size = sizeof(struct ffa_partition_info_1_0);
-        else
-            *fpi_size = sizeof(struct ffa_partition_info_1_1);
-
-        *count = 0;
+        /* Just give an empty partition list to the caller */
         ret = FFA_RET_OK;
         goto out;
     }
 
-    if ( !ctx->page_count || !ctx->rx_is_free )
+    if ( !spin_trylock(&ctx->rx_lock) )
+    {
+        ret = FFA_RET_BUSY;
         goto out;
+    }
+
+    dst_buf = ctx->rx;
+
+    if ( !ffa_rx )
+    {
+        ret = FFA_RET_DENIED;
+        goto out_rx_release;
+    }
+
+    if ( !ctx->page_count || !ctx->rx_is_free )
+    {
+        ret = FFA_RET_DENIED;
+        goto out_rx_release;
+    }
+
     spin_lock(&ffa_rx_buffer_lock);
-    ret = ffa_partition_info_get(w1, w2, w3, w4, w5, count, fpi_size);
+
+    ret = ffa_partition_info_get(uuid, 0, &ffa_sp_count, &src_size);
+
     if ( ret )
         goto out_rx_buf_unlock;
+
     /*
      * ffa_partition_info_get() succeeded so we now own the RX buffer we
      * share with the SPMC. We must give it back using ffa_rx_release()
      * once we've copied the content.
      */
 
-    if ( ctx->guest_vers == FFA_VERSION_1_0 )
+    /* we cannot have a size smaller than 1.0 structure */
+    if ( src_size < sizeof(struct ffa_partition_info_1_0) )
     {
-        size_t n;
-        struct ffa_partition_info_1_1 *src = ffa_rx;
-        struct ffa_partition_info_1_0 *dst = ctx->rx;
-
-        if ( ctx->page_count * FFA_PAGE_SIZE < *count * sizeof(*dst) )
-        {
-            ret = FFA_RET_NO_MEMORY;
-            goto out_rx_release;
-        }
+        ret = FFA_RET_NOT_SUPPORTED;
+        goto out_rx_hyp_release;
+    }
 
-        for ( n = 0; n < *count; n++ )
-        {
-            dst[n].id = src[n].id;
-            dst[n].execution_context = src[n].execution_context;
-            dst[n].partition_properties = src[n].partition_properties;
-        }
+    if ( ctx->page_count * FFA_PAGE_SIZE < ffa_sp_count * dst_size )
+    {
+        ret = FFA_RET_NO_MEMORY;
+        goto out_rx_hyp_release;
     }
-    else
+
+    if ( ffa_sp_count > 0 )
     {
-        size_t sz = *count * *fpi_size;
+        uint32_t n;
+        void *src_buf = ffa_rx;
 
-        if ( ctx->page_count * FFA_PAGE_SIZE < sz )
+        /* copy the secure partitions info */
+        for ( n = 0; n < ffa_sp_count; n++ )
         {
-            ret = FFA_RET_NO_MEMORY;
-            goto out_rx_release;
+            memcpy(dst_buf, src_buf, dst_size);
+            dst_buf += dst_size;
+            src_buf += src_size;
         }
-
-        memcpy(ctx->rx, ffa_rx, sz);
     }
+
     ctx->rx_is_free = false;
-out_rx_release:
+
+out_rx_hyp_release:
     ffa_rx_release();
 out_rx_buf_unlock:
     spin_unlock(&ffa_rx_buffer_lock);
-out:
+out_rx_release:
     spin_unlock(&ctx->rx_lock);
 
-    return ret;
+out:
+    if ( ret )
+        ffa_set_regs_error(regs, ret);
+    else
+        ffa_set_regs_success(regs, ffa_sp_count, dst_size);
 }
 
 static int32_t ffa_direct_req_send_vm(uint16_t sp_id, uint16_t vm_id,
@@ -221,19 +257,28 @@ static void uninit_subscribers(void)
         XFREE(subscr_vm_destroyed);
 }
 
-static bool init_subscribers(struct ffa_partition_info_1_1 *fpi, uint16_t count)
+static bool init_subscribers(uint16_t count, uint32_t fpi_size)
 {
     uint16_t n;
     uint16_t c_pos;
     uint16_t d_pos;
+    struct ffa_partition_info_1_1 *fpi;
+
+    if ( fpi_size < sizeof(struct ffa_partition_info_1_1) )
+    {
+        printk(XENLOG_ERR "ffa: partition info size invalid: %u\n", fpi_size);
+        return false;
+    }
 
     subscr_vm_created_count = 0;
     subscr_vm_destroyed_count = 0;
     for ( n = 0; n < count; n++ )
     {
-        if ( fpi[n].partition_properties & FFA_PART_PROP_NOTIF_CREATED )
+        fpi = ffa_rx + n * fpi_size;
+
+        if ( fpi->partition_properties & FFA_PART_PROP_NOTIF_CREATED )
             subscr_vm_created_count++;
-        if ( fpi[n].partition_properties & FFA_PART_PROP_NOTIF_DESTROYED )
+        if ( fpi->partition_properties & FFA_PART_PROP_NOTIF_DESTROYED )
             subscr_vm_destroyed_count++;
     }
 
@@ -252,10 +297,12 @@ static bool init_subscribers(struct ffa_partition_info_1_1 *fpi, uint16_t count)
 
     for ( c_pos = 0, d_pos = 0, n = 0; n < count; n++ )
     {
-        if ( fpi[n].partition_properties & FFA_PART_PROP_NOTIF_CREATED )
-            subscr_vm_created[c_pos++] = fpi[n].id;
-        if ( fpi[n].partition_properties & FFA_PART_PROP_NOTIF_DESTROYED )
-            subscr_vm_destroyed[d_pos++] = fpi[n].id;
+        fpi = ffa_rx + n * fpi_size;
+
+        if ( fpi->partition_properties & FFA_PART_PROP_NOTIF_CREATED )
+            subscr_vm_created[c_pos++] = fpi->id;
+        if ( fpi->partition_properties & FFA_PART_PROP_NOTIF_DESTROYED )
+            subscr_vm_destroyed[d_pos++] = fpi->id;
     }
 
     return true;
@@ -275,7 +322,7 @@ bool ffa_partinfo_init(void)
          !ffa_rx || !ffa_tx )
         return false;
 
-    e = ffa_partition_info_get(0, 0, 0, 0, 0, &count, &fpi_size);
+    e = ffa_partition_info_get(NULL, 0, &count, &fpi_size);
     if ( e )
     {
         printk(XENLOG_ERR "ffa: Failed to get list of SPs: %d\n", e);
@@ -288,7 +335,7 @@ bool ffa_partinfo_init(void)
         goto out;
     }
 
-    ret = init_subscribers(ffa_rx, count);
+    ret = init_subscribers(count, fpi_size);
 
 out:
     ffa_rx_release();
xen/arch/arm/tee/ffa_private.h +1 -4
index 85eb61c13464..e5bc73f9039e 100644
@@ -316,9 +316,7 @@ int ffa_handle_mem_reclaim(uint64_t handle, uint32_t flags);
 bool ffa_partinfo_init(void);
 int ffa_partinfo_domain_init(struct domain *d);
 bool ffa_partinfo_domain_destroy(struct domain *d);
-int32_t ffa_handle_partition_info_get(uint32_t w1, uint32_t w2, uint32_t w3,
-                                      uint32_t w4, uint32_t w5, uint32_t *count,
-                                      uint32_t *fpi_size);
+void ffa_handle_partition_info_get(struct cpu_user_regs *regs);
 
 bool ffa_rxtx_init(void);
 void ffa_rxtx_destroy(void);
--

2.47.0

#18 lore raw

Re: [PATCH v2 06/10] xen/arm: ffa: Use bit 15 convention for SPs

In reply to Bertrand Marquis

Hi Bertrand,

Quoted 72 lines · Bertrand Marquis

Make use and required to have bit 15 convention respected by secure world (having bit 15 of IDs set for secure endpoints and non-set for non-secure ones).
If any secure partition has an ID with bit 15 not set, it will not be possible to contact or detect them.
Print an error log during probe for each secure endpoint detected with bit 15 not set.

We are switching to this convention because Xen is currently not using VMIDs with bit 15 set so we are sure that no VM will have it set (this is ensured by BUILD_BUG_ON in case this becomes false in the future). It is allowing to easily distinguish between secure and non-secure endpoints, preventing the need to store a list of secure endpoint IDs in Xen.

---
Changes in v2:
- rebase

---
 xen/arch/arm/tee/ffa.c          | 22 +++++++++++---
 xen/arch/arm/tee/ffa_partinfo.c | 54 +++++++++++++++++++++++++--------
 xen/arch/arm/tee/ffa_private.h  |  7 +++++
 xen/arch/arm/tee/ffa_shm.c      | 12 +++++++-
 4 files changed, 77 insertions(+), 18 deletions(-)
xen/arch/arm/tee/ffa.c +17 -5
index 4b55e8b48f01..a292003ca9fe 100644
@@ -195,6 +195,14 @@ static void handle_msg_send_direct_req(struct cpu_user_regs *regs, uint32_t fid)
         goto out;
     }
 
+    /* we do not support direct messages to VMs */
+    if ( !FFA_ID_IS_SECURE(src_dst & GENMASK(15,0)) )
+    {
+        resp.a0 = FFA_ERROR;
+        resp.a2 = FFA_RET_NOT_SUPPORTED;
+        goto out;
+    }
+
     arg.a1 = src_dst;
     arg.a2 = get_user_reg(regs, 2) & mask;
     arg.a3 = get_user_reg(regs, 3) & mask;
@@ -391,11 +399,15 @@ static int ffa_domain_init(struct domain *d)
 
     if ( !ffa_fw_version )
         return -ENODEV;
-     /*
-      * We can't use that last possible domain ID or ffa_get_vm_id() would
-      * cause an overflow.
-      */
-    if ( d->domain_id >= UINT16_MAX)
+    /*
+     * We are using the domain_id + 1 as the FF-A ID for VMs as FF-A ID 0 is
+     * reserved for the hypervisor and we only support secure endpoints using
+     * FF-A IDs with BIT 15 set to 1 so make sure those are not used by Xen.
+     */
+    BUILD_BUG_ON(DOMID_FIRST_RESERVED >= UINT16_MAX);
+    BUILD_BUG_ON((DOMID_MASK & BIT(15, U)) != 0);
+
+    if ( d->domain_id >= DOMID_FIRST_RESERVED )
         return -ERANGE;
 
     ctx = xzalloc(struct ffa_ctx);
xen/arch/arm/tee/ffa_partinfo.c +0 -0
index 75a073d090e0..3cf801523296 100644

xen/arch/arm/tee/ffa_partinfo.c void ffa_handle_partition_info_get(struct cpu_user_regs *regs) · line 172

Quoted 5 lines
xen/arch/arm/tee/ffa_partinfo.c +1 -1
@@ -169,14 +169,26 @@ void ffa_handle_partition_info_get(struct cpu_user_regs *regs)
 
     if ( ffa_sp_count > 0 )
     {
-        uint32_t n;
+        uint32_t n, real_num = ffa_sp_count;

Nit: how about n_limit instead of real_num?

Quoted 10 lines
xen/arch/arm/tee/ffa_partinfo.c +3 -3
         void *src_buf = ffa_rx;
 
         /* copy the secure partitions info */
-        for ( n = 0; n < ffa_sp_count; n++ )
+        for ( n = 0; n < real_num; n++ )
         {
-            memcpy(dst_buf, src_buf, dst_size);
-            dst_buf += dst_size;
+            struct ffa_partition_info_1_1 *fpi = src_buf;
+

xen/arch/arm/tee/ffa_partinfo.c void ffa_handle_partition_info_get(struct cpu_user_regs *regs) · line 189

Quoted 10 lines
xen/arch/arm/tee/ffa_partinfo.c +10 -0
+            /* filter out SP not following bit 15 convention if any */
+            if ( FFA_ID_IS_SECURE(fpi->id) )
+            {
+                memcpy(dst_buf, src_buf, dst_size);
+                dst_buf += dst_size;
+            }
+            else
+            {
+                printk(XENLOG_INFO "ffa: sp id 0x%04x skipped, bit 15 is 0\n",
+                       fpi->id);

We have already logged this in init_subscribers() below. Is there a risk of flooding the logs with this?

Cheers,
Jens

Quoted 111 lines
xen/arch/arm/tee/ffa_partinfo.c +28 -8
+                ffa_sp_count--;
+            }
             src_buf += src_size;
         }
     }
@@ -276,10 +288,25 @@ static bool init_subscribers(uint16_t count, uint32_t fpi_size)
     {
         fpi = ffa_rx + n * fpi_size;
 
-        if ( fpi->partition_properties & FFA_PART_PROP_NOTIF_CREATED )
-            subscr_vm_created_count++;
-        if ( fpi->partition_properties & FFA_PART_PROP_NOTIF_DESTROYED )
-            subscr_vm_destroyed_count++;
+        /*
+         * We need to have secure partitions using bit 15 set convention for
+         * secure partition IDs.
+         * Inform the user with a log and discard giving created or destroy
+         * event to those IDs.
+         */
+        if ( !FFA_ID_IS_SECURE(fpi->id) )
+        {
+            printk(XENLOG_ERR "ffa: Firmware is not using bit 15 convention for IDs !!\n"
+                              "ffa: Secure partition with id 0x%04x cannot be used\n",
+                              fpi->id);
+        }
+        else
+        {
+            if ( fpi->partition_properties & FFA_PART_PROP_NOTIF_CREATED )
+                subscr_vm_created_count++;
+            if ( fpi->partition_properties & FFA_PART_PROP_NOTIF_DESTROYED )
+                subscr_vm_destroyed_count++;
+        }
     }
 
     if ( subscr_vm_created_count )
@@ -299,10 +326,13 @@ static bool init_subscribers(uint16_t count, uint32_t fpi_size)
     {
         fpi = ffa_rx + n * fpi_size;
 
-        if ( fpi->partition_properties & FFA_PART_PROP_NOTIF_CREATED )
-            subscr_vm_created[c_pos++] = fpi->id;
-        if ( fpi->partition_properties & FFA_PART_PROP_NOTIF_DESTROYED )
-            subscr_vm_destroyed[d_pos++] = fpi->id;
+        if ( FFA_ID_IS_SECURE(fpi->id) )
+        {
+            if ( fpi->partition_properties & FFA_PART_PROP_NOTIF_CREATED )
+                subscr_vm_created[c_pos++] = fpi->id;
+            if ( fpi->partition_properties & FFA_PART_PROP_NOTIF_DESTROYED )
+                subscr_vm_destroyed[d_pos++] = fpi->id;
+        }
     }
 
     return true;
xen/arch/arm/tee/ffa_private.h +7 -0
index e5bc73f9039e..afe69b43dbef 100644
@@ -108,6 +108,13 @@
  */
 #define FFA_CTX_TEARDOWN_DELAY          SECONDS(1)
 
+/*
+ * We rely on the convention suggested but not mandated by the FF-A
+ * specification that secure world endpoint identifiers have the bit 15
+ * set and normal world have it set to 0.
+ */
+#define FFA_ID_IS_SECURE(id)    ((id) & BIT(15, U))
+
 /* FF-A-1.1-REL0 section 10.9.2 Memory region handle, page 167 */
 #define FFA_HANDLE_HYP_FLAG             BIT(63, ULL)
 #define FFA_HANDLE_INVALID              0xffffffffffffffffULL
xen/arch/arm/tee/ffa_shm.c +11 -2
index efa5b67db8e1..29675f9ba3f7 100644
@@ -469,6 +469,7 @@ void ffa_handle_mem_share(struct cpu_user_regs *regs)
     int ret = FFA_RET_DENIED;
     uint32_t range_count;
     uint32_t region_offs;
+    uint16_t dst_id;
 
     if ( !ffa_fw_supports_fid(FFA_MEM_SHARE_64) )
     {
@@ -537,6 +538,15 @@ void ffa_handle_mem_share(struct cpu_user_regs *regs)
         goto out_unlock;
 
     mem_access = ctx->tx + trans.mem_access_offs;
+
+    dst_id = ACCESS_ONCE(mem_access->access_perm.endpoint_id);
+    if ( !FFA_ID_IS_SECURE(dst_id) )
+    {
+        /* we do not support sharing with VMs */
+        ret = FFA_RET_NOT_SUPPORTED;
+        goto out_unlock;
+    }
+
     if ( ACCESS_ONCE(mem_access->access_perm.perm) != FFA_MEM_ACC_RW )
     {
         ret = FFA_RET_NOT_SUPPORTED;
@@ -567,7 +577,7 @@ void ffa_handle_mem_share(struct cpu_user_regs *regs)
         goto out_unlock;
     }
     shm->sender_id = trans.sender_id;
-    shm->ep_id = ACCESS_ONCE(mem_access->access_perm.endpoint_id);
+    shm->ep_id = dst_id;
 
     /*
      * Check that the Composite memory region descriptor fits.
--

2.47.0

#21 lore raw

Re: [PATCH v2 07/10] xen/arm: ffa: Transmit RXTX buffers to the SPMC

In reply to Bertrand Marquis

Hi Bertrand,

Quoted 91 lines · Bertrand Marquis

When an RXTX buffer is mapped by a VM transmit it to the SPMC when it supports RX_ACQUIRE.
As a consequence of that, we must acquire the RX buffer of a VM from the SPMC when we want to use it:
- create a generic acquire and release function to get the rx buffer of a VM which gets it from the SPMC when supported
- rename the rx_acquire to hyp_rx_acquire to remove confusion
- rework the rx_lock to only lock access to rx_is_free and only allow usage of the rx buffer to one who managed to acquire it, thus removing the trylock and returning busy if rx_is_free is false

As part of this change move some structure definition to ffa_private from ffa_shm as those are need for the MAP call with the SPMC.

---
Changes in v2:
- unmap VM rxtx buffer in SPMC on unmap call or on VM destroy
- rework the unmap call to the SPMC to properly pass the VM ID

---
 xen/arch/arm/tee/ffa.c          |   2 +-
 xen/arch/arm/tee/ffa_partinfo.c |  29 ++----
 xen/arch/arm/tee/ffa_private.h  |  22 ++++-
 xen/arch/arm/tee/ffa_rxtx.c     | 158 ++++++++++++++++++++++++++------
 xen/arch/arm/tee/ffa_shm.c      |  15 ---
 5 files changed, 161 insertions(+), 65 deletions(-)
xen/arch/arm/tee/ffa.c +1 -1
index a292003ca9fe..40ea5398fa21 100644
@@ -347,7 +347,7 @@ static bool ffa_handle_call(struct cpu_user_regs *regs)
         ffa_handle_partition_info_get(regs);
         return true;
     case FFA_RX_RELEASE:
-        e = ffa_handle_rx_release();
+        e = ffa_rx_release(d);
         break;
     case FFA_MSG_SEND_DIRECT_REQ_32:
     case FFA_MSG_SEND_DIRECT_REQ_64:
xen/arch/arm/tee/ffa_partinfo.c +4 -14
index 3cf801523296..fde187dba4e5 100644
@@ -121,11 +121,9 @@ void ffa_handle_partition_info_get(struct cpu_user_regs *regs)
         goto out;
     }
 
-    if ( !spin_trylock(&ctx->rx_lock) )
-    {
-        ret = FFA_RET_BUSY;
+    ret = ffa_rx_acquire(d);
+    if ( ret != FFA_RET_OK )
         goto out;
-    }
 
     dst_buf = ctx->rx;
 
@@ -135,22 +133,16 @@ void ffa_handle_partition_info_get(struct cpu_user_regs *regs)
         goto out_rx_release;
     }
 
-    if ( !ctx->page_count || !ctx->rx_is_free )
-    {
-        ret = FFA_RET_DENIED;
-        goto out_rx_release;
-    }
-
     spin_lock(&ffa_rx_buffer_lock);
 
     ret = ffa_partition_info_get(uuid, 0, &ffa_sp_count, &src_size);
 
     if ( ret )
-        goto out_rx_buf_unlock;
+        goto out_rx_hyp_unlock;
 
     /*
      * ffa_partition_info_get() succeeded so we now own the RX buffer we
-     * share with the SPMC. We must give it back using ffa_rx_release()
+     * share with the SPMC. We must give it back using ffa_hyp_rx_release()
      * once we've copied the content.
      */
 
@@ -193,15 +185,13 @@ void ffa_handle_partition_info_get(struct cpu_user_regs *regs)
         }
     }
 
-    ctx->rx_is_free = false;
-
 out_rx_hyp_release:

xen/arch/arm/tee/ffa_partinfo.c void ffa_handle_partition_info_get(struct cpu_user_regs *regs) · line 194

Quoted 10 lines
xen/arch/arm/tee/ffa_partinfo.c +4 -4
-    ffa_rx_release();
-out_rx_buf_unlock:
+    ffa_hyp_rx_release();
+out_rx_hyp_unlock:
     spin_unlock(&ffa_rx_buffer_lock);
 out_rx_release:
-    spin_unlock(&ctx->rx_lock);
-
+    if ( ret != FFA_RET_OK )
+        ffa_rx_release(d);

Please comment on why ffa_rx_release() must only be called on failure.

Quoted 106 lines
xen/arch/arm/tee/ffa_partinfo.c +1 -2
 out:
     if ( ret )
         ffa_set_regs_error(regs, ret);
@@ -368,8 +358,7 @@ bool ffa_partinfo_init(void)
     ret = init_subscribers(count, fpi_size);
 
 out:
-    ffa_rx_release();
-
+    ffa_hyp_rx_release();
     return ret;
 }
 
xen/arch/arm/tee/ffa_private.h +19 -3
index afe69b43dbef..9adfe687c3c9 100644
@@ -265,6 +265,21 @@
 #define FFA_ABI_BITNUM(id)    ((FFA_ABI_ID(id) - FFA_ABI_MIN) << 1 | \
                                FFA_ABI_CONV(id))
 
+/* Constituent memory region descriptor */
+struct ffa_address_range {
+    uint64_t address;
+    uint32_t page_count;
+    uint32_t reserved;
+};
+
+/* Composite memory region descriptor */
+struct ffa_mem_region {
+    uint32_t total_page_count;
+    uint32_t address_range_count;
+    uint64_t reserved;
+    struct ffa_address_range address_range_array[];
+};
+
 struct ffa_ctx_notif {
     bool enabled;
 
@@ -292,7 +307,7 @@ struct ffa_ctx {
     struct ffa_ctx_notif notif;
     /*
      * tx_lock is used to serialize access to tx
-     * rx_lock is used to serialize access to rx
+     * rx_lock is used to serialize access to rx_is_free
      * lock is used for the rest in this struct
      */
     spinlock_t tx_lock;
@@ -331,7 +346,8 @@ void ffa_rxtx_domain_destroy(struct domain *d);
 uint32_t ffa_handle_rxtx_map(uint32_t fid, register_t tx_addr,
                             register_t rx_addr, uint32_t page_count);
 uint32_t ffa_handle_rxtx_unmap(void);
-int32_t ffa_handle_rx_release(void);
+int32_t ffa_rx_acquire(struct domain *d);
+int32_t ffa_rx_release(struct domain *d);
 
 void ffa_notif_init(void);
 void ffa_notif_init_interrupt(void);
@@ -420,7 +436,7 @@ static inline int32_t ffa_simple_call(uint32_t fid, register_t a1,
     return ffa_get_ret_code(&resp);
 }
 
-static inline int32_t ffa_rx_release(void)
+static inline int32_t ffa_hyp_rx_release(void)
 {
     return ffa_simple_call(FFA_RX_RELEASE, 0, 0, 0, 0);
 }
xen/arch/arm/tee/ffa_rxtx.c +17 -0
index b6931c855779..a5d43e51f843 100644
@@ -30,6 +30,17 @@ struct ffa_endpoint_rxtx_descriptor_1_1 {
     uint32_t tx_region_offs;
 };
 
+static int32_t ffa_rxtx_map(paddr_t tx_addr, paddr_t rx_addr,
+                            uint32_t page_count)
+{
+    return ffa_simple_call(FFA_RXTX_MAP_64, tx_addr, rx_addr, page_count, 0);
+}
+
+static int32_t ffa_rxtx_unmap(uint16_t id)
+{
+    return ffa_simple_call(FFA_RXTX_UNMAP, ((uint64_t)id)<<16, 0, 0, 0);
+}
+
 uint32_t ffa_handle_rxtx_map(uint32_t fid, register_t tx_addr,
                             register_t rx_addr, uint32_t page_count)
 {
@@ -42,6 +53,9 @@ uint32_t ffa_handle_rxtx_map(uint32_t fid, register_t tx_addr,
     void *rx;
     void *tx;
 
+    /* The code is considering that we only get one page for now */
+    BUILD_BUG_ON(FFA_MAX_RXTX_PAGE_COUNT != 1);
+
     if ( !smccc_is_conv_64(fid) )
     {
         /*
@@ -87,6 +101,65 @@ uint32_t ffa_handle_rxtx_map(uint32_t fid, register_t tx_addr,
     if ( !rx )
         goto err_unmap_tx;
 
+    /*
+     * Transmit the RX/TX buffer information to the SPM if acquire is supported
+     * as the spec says that if not there is not need to acquire/release/map

xen/arch/arm/tee/ffa_rxtx.c uint32_t ffa_handle_rxtx_map(uint32_t fid, register_t tx_addr, · line 116

Quoted 10 lines
xen/arch/arm/tee/ffa_rxtx.c +10 -0
+     * rxtx buffers from the SPMC
+     */
+    if ( ffa_fw_supports_fid(FFA_RX_ACQUIRE) )
+    {
+        struct ffa_endpoint_rxtx_descriptor_1_1 *rxtx_desc;
+        struct ffa_mem_region *mem_reg;
+
+        /* All must fit in our TX buffer */
+        BUILD_BUG_ON((sizeof(*rxtx_desc) + sizeof(*mem_reg)*2 +
+                      sizeof(struct ffa_address_range)*2) > FFA_PAGE_SIZE);

Please add space before and after the binary operator "*". The size of the TX buffer is FFA_RXTX_PAGE_COUNT * FFA_PAGE_SIZE.
Nit: the outer parenthesis around the left expression aren't needed.

Quoted 29 lines
xen/arch/arm/tee/ffa_rxtx.c +29 -0
+
+        spin_lock(&ffa_tx_buffer_lock);
+        rxtx_desc = ffa_tx;
+
+        /*
+         * We have only one page for each so we pack everything:
+         * - rx region descriptor
+         * - rx region range
+         * - tx region descriptor
+         * - tx region range
+         */
+        rxtx_desc->sender_id = ffa_get_vm_id(d);
+        rxtx_desc->reserved = 0;
+        rxtx_desc->rx_region_offs = sizeof(*rxtx_desc);
+        rxtx_desc->tx_region_offs = sizeof(*rxtx_desc) +
+                                    offsetof(struct ffa_mem_region,
+                                             address_range_array[1]);
+
+        /* rx buffer */
+        mem_reg = ffa_tx + sizeof(*rxtx_desc);
+        mem_reg->total_page_count = 1;
+        mem_reg->address_range_count = 1;
+        mem_reg->reserved = 0;
+
+        mem_reg->address_range_array[0].address = page_to_maddr(rx_pg);
+        mem_reg->address_range_array[0].page_count = 1;
+        mem_reg->address_range_array[0].reserved = 0;
+
+        /* tx buffer */

xen/arch/arm/tee/ffa_rxtx.c uint32_t ffa_handle_rxtx_map(uint32_t fid, register_t tx_addr, · line 155

Quoted 10 lines
xen/arch/arm/tee/ffa_rxtx.c +10 -0
+        mem_reg = ffa_tx + rxtx_desc->tx_region_offs;
+        mem_reg->total_page_count = 1;
+        mem_reg->address_range_count = 1;
+        mem_reg->reserved = 0;
+
+        mem_reg->address_range_array[0].address = page_to_maddr(tx_pg);
+        mem_reg->address_range_array[0].page_count = 1;
+        mem_reg->address_range_array[0].reserved = 0;
+
+        ret = ffa_rxtx_map(0, 0, 1);

The last parameter is also MBZ when forwarding on behalf of an endpoint.

Cheers,
Jens

Quoted 179 lines
xen/arch/arm/tee/ffa_rxtx.c +66 -26
+
+        spin_unlock(&ffa_tx_buffer_lock);
+
+        if ( ret != FFA_RET_OK )
+            goto err_unmap_rx;
+    }
+
     ctx->rx = rx;
     ctx->tx = tx;
     ctx->rx_pg = rx_pg;
@@ -95,6 +168,8 @@ uint32_t ffa_handle_rxtx_map(uint32_t fid, register_t tx_addr,
     ctx->rx_is_free = true;
     return FFA_RET_OK;
 
+err_unmap_rx:
+    unmap_domain_page_global(rx);
 err_unmap_tx:
     unmap_domain_page_global(tx);
 err_put_rx_pg:
@@ -105,8 +180,22 @@ err_put_tx_pg:
     return ret;
 }
 
-static void rxtx_unmap(struct ffa_ctx *ctx)
+static uint32_t  rxtx_unmap(struct domain *d)
 {
+    struct ffa_ctx *ctx = d->arch.tee;
+
+    if ( !ctx->page_count )
+        return FFA_RET_INVALID_PARAMETERS;
+
+    if ( ffa_fw_supports_fid(FFA_RX_ACQUIRE) )
+    {
+        uint32_t ret;
+
+        ret = ffa_rxtx_unmap(ffa_get_vm_id(d));
+        if ( ret != FFA_RET_OK )
+            return ret;
+    }
+
     unmap_domain_page_global(ctx->rx);
     unmap_domain_page_global(ctx->tx);
     put_page(ctx->rx_pg);
@@ -117,32 +206,63 @@ static void rxtx_unmap(struct ffa_ctx *ctx)
     ctx->tx_pg = NULL;
     ctx->page_count = 0;
     ctx->rx_is_free = false;
+
+    return FFA_RET_OK;
 }
 
 uint32_t ffa_handle_rxtx_unmap(void)
 {
-    struct domain *d = current->domain;
+    return rxtx_unmap(current->domain);
+}
+
+int32_t ffa_rx_acquire(struct domain *d)
+{
+    int32_t ret = FFA_RET_OK;
     struct ffa_ctx *ctx = d->arch.tee;
 
-    if ( !ctx->rx )
-        return FFA_RET_INVALID_PARAMETERS;
+    spin_lock(&ctx->rx_lock);
 
-    rxtx_unmap(ctx);
+    if ( !ctx->page_count )
+    {
+        ret = FFA_RET_DENIED;
+        goto out;
+    }
 
-    return FFA_RET_OK;
+    if ( !ctx->rx_is_free )
+    {
+        ret = FFA_RET_BUSY;
+        goto out;
+    }
+
+    if ( ffa_fw_supports_fid(FFA_RX_ACQUIRE) )
+    {
+        ret = ffa_simple_call(FFA_RX_ACQUIRE, ffa_get_vm_id(d), 0, 0, 0);
+        if ( ret != FFA_RET_OK )
+            goto out;
+    }
+    ctx->rx_is_free = false;
+out:
+    spin_unlock(&ctx->rx_lock);
+
+    return ret;
 }
 
-int32_t ffa_handle_rx_release(void)
+int32_t ffa_rx_release(struct domain *d)
 {
     int32_t ret = FFA_RET_DENIED;
-    struct domain *d = current->domain;
     struct ffa_ctx *ctx = d->arch.tee;
 
-    if ( !spin_trylock(&ctx->rx_lock) )
-        return FFA_RET_BUSY;
+    spin_lock(&ctx->rx_lock);
 
     if ( !ctx->page_count || ctx->rx_is_free )
         goto out;
+
+    if ( ffa_fw_supports_fid(FFA_RX_ACQUIRE) )
+    {
+        ret = ffa_simple_call(FFA_RX_RELEASE, ffa_get_vm_id(d), 0, 0, 0);
+        if ( ret != FFA_RET_OK )
+            goto out;
+    }
     ret = FFA_RET_OK;
     ctx->rx_is_free = true;
 out:
@@ -151,23 +271,9 @@ out:
     return ret;
 }
 
-static int32_t ffa_rxtx_map(paddr_t tx_addr, paddr_t rx_addr,
-                            uint32_t page_count)
-{
-    return ffa_simple_call(FFA_RXTX_MAP_64, tx_addr, rx_addr, page_count, 0);
-}
-
-static int32_t ffa_rxtx_unmap(void)
-{
-    return ffa_simple_call(FFA_RXTX_UNMAP, 0, 0, 0, 0);
-}
-
 void ffa_rxtx_domain_destroy(struct domain *d)
 {
-    struct ffa_ctx *ctx = d->arch.tee;
-
-    if ( ctx->rx )
-        rxtx_unmap(ctx);
+    rxtx_unmap(d);
 }
 
 void ffa_rxtx_destroy(void)
@@ -186,7 +292,7 @@ void ffa_rxtx_destroy(void)
     }
 
     if ( need_unmap )
-        ffa_rxtx_unmap();
+        ffa_rxtx_unmap(0);
 }
 
 bool ffa_rxtx_init(void)
xen/arch/arm/tee/ffa_shm.c +0 -16
index 29675f9ba3f7..d628c1b70609 100644
@@ -16,21 +16,6 @@
 
 #include "ffa_private.h"
 
-/* Constituent memory region descriptor */
-struct ffa_address_range {
-    uint64_t address;
-    uint32_t page_count;
-    uint32_t reserved;
-};
-
-/* Composite memory region descriptor */
-struct ffa_mem_region {
-    uint32_t total_page_count;
-    uint32_t address_range_count;
-    uint64_t reserved;
-    struct ffa_address_range address_range_array[];
-};
-
 /* Memory access permissions descriptor */
 struct ffa_mem_access_perm {
     uint16_t endpoint_id;
--

2.47.0

Reviewed-by on v2 8/10
#25 lore raw

Re: [PATCH v2 08/10] xen/arm: ffa: move message function into ffa_msg.c

In reply to Bertrand Marquis

Hi Bertrand,

Quoted 10 lines · Bertrand Marquis

Move the direct message handling function in its own source file and rename it to have a ffa_ prefix.
This is a preparation to add support for indirect messages which will go into this newly created source file.

---
Changes in v2:
- rebase

Quoted 7 lines
---
 xen/arch/arm/tee/Makefile      |  1 +
 xen/arch/arm/tee/ffa.c         | 69 +----------------------------
 xen/arch/arm/tee/ffa_msg.c     | 80 ++++++++++++++++++++++++++++++++++
 xen/arch/arm/tee/ffa_private.h |  2 +
 4 files changed, 84 insertions(+), 68 deletions(-)
 create mode 100644 xen/arch/arm/tee/ffa_msg.c

Looks good.

Cheers,
Jens

Quoted 201 lines
xen/arch/arm/tee/Makefile +1 -0
index 7c0f46f7f446..0848c833fec3 100644
@@ -3,5 +3,6 @@ obj-$(CONFIG_FFA) += ffa_shm.o
 obj-$(CONFIG_FFA) += ffa_partinfo.o
 obj-$(CONFIG_FFA) += ffa_rxtx.o
 obj-$(CONFIG_FFA) += ffa_notif.o
+obj-$(CONFIG_FFA) += ffa_msg.o
 obj-y += tee.o
 obj-$(CONFIG_OPTEE) += optee.o
xen/arch/arm/tee/ffa.c +1 -68
index 40ea5398fa21..72826b49d2aa 100644
@@ -167,73 +167,6 @@ static void handle_version(struct cpu_user_regs *regs)
     ffa_set_regs(regs, FFA_MY_VERSION, 0, 0, 0, 0, 0, 0, 0);
 }
 
-static void handle_msg_send_direct_req(struct cpu_user_regs *regs, uint32_t fid)
-{
-    struct arm_smccc_1_2_regs arg = { .a0 = fid, };
-    struct arm_smccc_1_2_regs resp = { };
-    struct domain *d = current->domain;
-    uint32_t src_dst;
-    uint64_t mask;
-
-    if ( smccc_is_conv_64(fid) )
-        mask = GENMASK_ULL(63, 0);
-    else
-        mask = GENMASK_ULL(31, 0);
-
-    if ( !ffa_fw_supports_fid(fid) )
-    {
-        resp.a0 = FFA_ERROR;
-        resp.a2 = FFA_RET_NOT_SUPPORTED;
-        goto out;
-    }
-
-    src_dst = get_user_reg(regs, 1);
-    if ( (src_dst >> 16) != ffa_get_vm_id(d) )
-    {
-        resp.a0 = FFA_ERROR;
-        resp.a2 = FFA_RET_INVALID_PARAMETERS;
-        goto out;
-    }
-
-    /* we do not support direct messages to VMs */
-    if ( !FFA_ID_IS_SECURE(src_dst & GENMASK(15,0)) )
-    {
-        resp.a0 = FFA_ERROR;
-        resp.a2 = FFA_RET_NOT_SUPPORTED;
-        goto out;
-    }
-
-    arg.a1 = src_dst;
-    arg.a2 = get_user_reg(regs, 2) & mask;
-    arg.a3 = get_user_reg(regs, 3) & mask;
-    arg.a4 = get_user_reg(regs, 4) & mask;
-    arg.a5 = get_user_reg(regs, 5) & mask;
-    arg.a6 = get_user_reg(regs, 6) & mask;
-    arg.a7 = get_user_reg(regs, 7) & mask;
-
-    arm_smccc_1_2_smc(&arg, &resp);
-    switch ( resp.a0 )
-    {
-    case FFA_ERROR:
-    case FFA_SUCCESS_32:
-    case FFA_SUCCESS_64:
-    case FFA_MSG_SEND_DIRECT_RESP_32:
-    case FFA_MSG_SEND_DIRECT_RESP_64:
-        break;
-    default:
-        /* Bad fid, report back to the caller. */
-        memset(&resp, 0, sizeof(resp));
-        resp.a0 = FFA_ERROR;
-        resp.a1 = src_dst;
-        resp.a2 = FFA_RET_ABORTED;
-    }
-
-out:
-    ffa_set_regs(regs, resp.a0, resp.a1 & mask, resp.a2 & mask, resp.a3 & mask,
-                 resp.a4 & mask, resp.a5 & mask, resp.a6 & mask,
-                 resp.a7 & mask);
-}
-
 static void handle_features(struct cpu_user_regs *regs)
 {
     struct domain *d = current->domain;
@@ -351,7 +284,7 @@ static bool ffa_handle_call(struct cpu_user_regs *regs)
         break;
     case FFA_MSG_SEND_DIRECT_REQ_32:
     case FFA_MSG_SEND_DIRECT_REQ_64:
-        handle_msg_send_direct_req(regs, fid);
+        ffa_handle_msg_send_direct_req(regs, fid);
         return true;
     case FFA_MEM_SHARE_32:
     case FFA_MEM_SHARE_64:
xen/arch/arm/tee/ffa_msg.c new file +80 -0
new file mode 100644
index 000000000000..ae263e54890e
@@ -0,0 +1,80 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright (C) 2024  Linaro Limited
+ */
+
+#include <xen/const.h>
+#include <xen/sizes.h>
+#include <xen/types.h>
+
+#include <asm/smccc.h>
+#include <asm/regs.h>
+
+#include "ffa_private.h"
+
+void ffa_handle_msg_send_direct_req(struct cpu_user_regs *regs, uint32_t fid)
+{
+    struct arm_smccc_1_2_regs arg = { .a0 = fid, };
+    struct arm_smccc_1_2_regs resp = { };
+    struct domain *d = current->domain;
+    uint32_t src_dst;
+    uint64_t mask;
+
+    if ( smccc_is_conv_64(fid) )
+        mask = GENMASK_ULL(63, 0);
+    else
+        mask = GENMASK_ULL(31, 0);
+
+    if ( !ffa_fw_supports_fid(fid) )
+    {
+        resp.a0 = FFA_ERROR;
+        resp.a2 = FFA_RET_NOT_SUPPORTED;
+        goto out;
+    }
+
+    src_dst = get_user_reg(regs, 1);
+    if ( (src_dst >> 16) != ffa_get_vm_id(d) )
+    {
+        resp.a0 = FFA_ERROR;
+        resp.a2 = FFA_RET_INVALID_PARAMETERS;
+        goto out;
+    }
+
+    /* we do not support direct messages to VMs */
+    if ( !FFA_ID_IS_SECURE(src_dst & GENMASK(15,0)) )
+    {
+        resp.a0 = FFA_ERROR;
+        resp.a2 = FFA_RET_NOT_SUPPORTED;
+        goto out;
+    }
+
+    arg.a1 = src_dst;
+    arg.a2 = get_user_reg(regs, 2) & mask;
+    arg.a3 = get_user_reg(regs, 3) & mask;
+    arg.a4 = get_user_reg(regs, 4) & mask;
+    arg.a5 = get_user_reg(regs, 5) & mask;
+    arg.a6 = get_user_reg(regs, 6) & mask;
+    arg.a7 = get_user_reg(regs, 7) & mask;
+
+    arm_smccc_1_2_smc(&arg, &resp);
+    switch ( resp.a0 )
+    {
+    case FFA_ERROR:
+    case FFA_SUCCESS_32:
+    case FFA_SUCCESS_64:
+    case FFA_MSG_SEND_DIRECT_RESP_32:
+    case FFA_MSG_SEND_DIRECT_RESP_64:
+        break;
+    default:
+        /* Bad fid, report back to the caller. */
+        memset(&resp, 0, sizeof(resp));
+        resp.a0 = FFA_ERROR;
+        resp.a1 = src_dst;
+        resp.a2 = FFA_RET_ABORTED;
+    }
+
+out:
+    ffa_set_regs(regs, resp.a0, resp.a1 & mask, resp.a2 & mask, resp.a3 & mask,
+                 resp.a4 & mask, resp.a5 & mask, resp.a6 & mask,
+                 resp.a7 & mask);
+}
xen/arch/arm/tee/ffa_private.h +2 -1
index 9adfe687c3c9..02162e0ee4c7 100644
@@ -360,6 +360,8 @@ void ffa_handle_notification_info_get(struct cpu_user_regs *regs);
 void ffa_handle_notification_get(struct cpu_user_regs *regs);
 int ffa_handle_notification_set(struct cpu_user_regs *regs);
 
+void ffa_handle_msg_send_direct_req(struct cpu_user_regs *regs, uint32_t fid);
+
 static inline uint16_t ffa_get_vm_id(const struct domain *d)
 {
     /* +1 since 0 is reserved for the hypervisor in FF-A */
--

2.47.0

#27 lore raw

Re: [PATCH v2 09/10] xen/arm: ffa: Remove per VM notif_enabled

In reply to Bertrand Marquis

Hi Bertrand,

Quoted 81 lines · Bertrand Marquis

Remove the per VM flag to store if notifications are enabled or not as the only case where they are not, if notifications are enabled globally, will make the VM creation fail.
Also use the opportunity to always give the notifications interrupts IDs to VM. If the firmware does not support notifications, there won't be any generated and setting one will give back a NOT_SUPPORTED.

---
Changes in v2:
- rebase

---
 xen/arch/arm/tee/ffa.c         | 17 +++--------------
 xen/arch/arm/tee/ffa_notif.c   | 10 +---------
 xen/arch/arm/tee/ffa_private.h |  2 --
 3 files changed, 4 insertions(+), 25 deletions(-)
xen/arch/arm/tee/ffa.c +3 -14
index 72826b49d2aa..3a9525aa4598 100644
@@ -169,8 +169,6 @@ static void handle_version(struct cpu_user_regs *regs)
 
 static void handle_features(struct cpu_user_regs *regs)
 {
-    struct domain *d = current->domain;
-    struct ffa_ctx *ctx = d->arch.tee;
     uint32_t a1 = get_user_reg(regs, 1);
     unsigned int n;
 
@@ -218,16 +216,10 @@ static void handle_features(struct cpu_user_regs *regs)
         ffa_set_regs_success(regs, 0, 0);
         break;
     case FFA_FEATURE_NOTIF_PEND_INTR:
-        if ( ctx->notif.enabled )
-            ffa_set_regs_success(regs, GUEST_FFA_NOTIF_PEND_INTR_ID, 0);
-        else
-            ffa_set_regs_error(regs, FFA_RET_NOT_SUPPORTED);
+        ffa_set_regs_success(regs, GUEST_FFA_NOTIF_PEND_INTR_ID, 0);
         break;
     case FFA_FEATURE_SCHEDULE_RECV_INTR:
-        if ( ctx->notif.enabled )
-            ffa_set_regs_success(regs, GUEST_FFA_SCHEDULE_RECV_INTR_ID, 0);
-        else
-            ffa_set_regs_error(regs, FFA_RET_NOT_SUPPORTED);
+        ffa_set_regs_success(regs, GUEST_FFA_SCHEDULE_RECV_INTR_ID, 0);
         break;
 
     case FFA_NOTIFICATION_BIND:
@@ -236,10 +228,7 @@ static void handle_features(struct cpu_user_regs *regs)
     case FFA_NOTIFICATION_SET:
     case FFA_NOTIFICATION_INFO_GET_32:
     case FFA_NOTIFICATION_INFO_GET_64:
-        if ( ctx->notif.enabled )
-            ffa_set_regs_success(regs, 0, 0);
-        else
-            ffa_set_regs_error(regs, FFA_RET_NOT_SUPPORTED);
+        ffa_set_regs_success(regs, 0, 0);
         break;
     default:
         ffa_set_regs_error(regs, FFA_RET_NOT_SUPPORTED);
xen/arch/arm/tee/ffa_notif.c +0 -3
index 4b3e46318f4b..3c6418e62e2b 100644
@@ -405,7 +405,6 @@ void ffa_notif_init(void)
 
 int ffa_notif_domain_init(struct domain *d)
 {
-    struct ffa_ctx *ctx = d->arch.tee;
     int32_t res;
 
     if ( !notif_enabled )
@@ -415,18 +414,11 @@ int ffa_notif_domain_init(struct domain *d)
     if ( res )
         return -ENOMEM;
 
-    ctx->notif.enabled = true;
-
     return 0;

xen/arch/arm/tee/ffa_notif.c int ffa_notif_domain_init(struct domain *d) · line 423

Quoted 10 lines
xen/arch/arm/tee/ffa_notif.c +1 -4
 }
 
 void ffa_notif_domain_destroy(struct domain *d)
 {
-    struct ffa_ctx *ctx = d->arch.tee;
-
-    if ( ctx->notif.enabled )
-    {
+    if ( notif_enabled )
         ffa_notification_bitmap_destroy(ffa_get_vm_id(d));

This call may now be done even if there hasn't been a successful call to ffa_notification_bitmap_create().
A comment mentioning this and that it's harmless (if we can be sure it is) would be nice.

Cheers,
Jens

Quoted 19 lines
xen/arch/arm/tee/ffa_notif.c +0 -2
-        ctx->notif.enabled = false;
-    }
 }
xen/arch/arm/tee/ffa_private.h +0 -3
index 02162e0ee4c7..973ee55be09b 100644
@@ -281,8 +281,6 @@ struct ffa_mem_region {
 };
 
 struct ffa_ctx_notif {
-    bool enabled;
-
     /*
      * True if domain is reported by FFA_NOTIFICATION_INFO_GET to have
      * pending global notifications.
--

2.47.0

#19 lore raw

Re: [PATCH v2 06/10] xen/arm: ffa: Use bit 15 convention for SPs

In reply to Jens Wiklander

Hi Jens,

Quoted 79 lines · Jens Wiklander

Hi Bertrand,

Quoted 73 lines · Bertrand Marquis

Make use and required to have bit 15 convention respected by secure world (having bit 15 of IDs set for secure endpoints and non-set for non-secure ones).
If any secure partition has an ID with bit 15 not set, it will not be possible to contact or detect them.
Print an error log during probe for each secure endpoint detected with bit 15 not set.

We are switching to this convention because Xen is currently not using VMIDs with bit 15 set so we are sure that no VM will have it set (this is ensured by BUILD_BUG_ON in case this becomes false in the future). It is allowing to easily distinguish between secure and non-secure endpoints, preventing the need to store a list of secure endpoint IDs in Xen.

---
Changes in v2:
- rebase
---

xen/arch/arm/tee/ffa.c          | 22 +++++++++++---
xen/arch/arm/tee/ffa_partinfo.c | 54 +++++++++++++++++++++++++--------
xen/arch/arm/tee/ffa_private.h  |  7 +++++
xen/arch/arm/tee/ffa_shm.c      | 12 +++++++-
4 files changed, 77 insertions(+), 18 deletions(-)
xen/arch/arm/tee/ffa.c +17 -5
index 4b55e8b48f01..a292003ca9fe 100644
@@ -195,6 +195,14 @@ static void handle_msg_send_direct_req(struct cpu_user_regs *regs, uint32_t fid)
        goto out;
    }
 
+    /* we do not support direct messages to VMs */
+    if ( !FFA_ID_IS_SECURE(src_dst & GENMASK(15,0)) )
+    {
+        resp.a0 = FFA_ERROR;
+        resp.a2 = FFA_RET_NOT_SUPPORTED;
+        goto out;
+    }
+
    arg.a1 = src_dst;
    arg.a2 = get_user_reg(regs, 2) & mask;
    arg.a3 = get_user_reg(regs, 3) & mask;
@@ -391,11 +399,15 @@ static int ffa_domain_init(struct domain *d)
 
    if ( !ffa_fw_version )
        return -ENODEV;
-     /*
-      * We can't use that last possible domain ID or ffa_get_vm_id() would
-      * cause an overflow.
-      */
-    if ( d->domain_id >= UINT16_MAX)
+    /*
+     * We are using the domain_id + 1 as the FF-A ID for VMs as FF-A ID 0 is
+     * reserved for the hypervisor and we only support secure endpoints using
+     * FF-A IDs with BIT 15 set to 1 so make sure those are not used by Xen.
+     */
+    BUILD_BUG_ON(DOMID_FIRST_RESERVED >= UINT16_MAX);
+    BUILD_BUG_ON((DOMID_MASK & BIT(15, U)) != 0);
+
+    if ( d->domain_id >= DOMID_FIRST_RESERVED )
        return -ERANGE;
 
    ctx = xzalloc(struct ffa_ctx);
xen/arch/arm/tee/ffa_partinfo.c +0 -0
index 75a073d090e0..3cf801523296 100644
@@ -169,14 +169,26 @@ void ffa_handle_partition_info_get(struct cpu_user_regs *regs)
 
Quoted 6 lines

xen/arch/arm/tee/ffa_partinfo.c void ffa_handle_partition_info_get(struct cpu_user_regs *regs) · line 172

Quoted 4 lines
xen/arch/arm/tee/ffa_partinfo.c +1 -1
    if ( ffa_sp_count > 0 )
    {
-        uint32_t n;
+        uint32_t n, real_num = ffa_sp_count;

Nit: how about n_limit instead of real_num?

Sure i will change that in v3.

Quoted 22 lines
Quoted 11 lines
xen/arch/arm/tee/ffa_partinfo.c +3 -3
        void *src_buf = ffa_rx;
 
        /* copy the secure partitions info */
-        for ( n = 0; n < ffa_sp_count; n++ )
+        for ( n = 0; n < real_num; n++ )
        {
-            memcpy(dst_buf, src_buf, dst_size);
-            dst_buf += dst_size;
+            struct ffa_partition_info_1_1 *fpi = src_buf;
+

xen/arch/arm/tee/ffa_partinfo.c void ffa_handle_partition_info_get(struct cpu_user_regs *regs) · line 189

Quoted 10 lines
xen/arch/arm/tee/ffa_partinfo.c +10 -0
+            /* filter out SP not following bit 15 convention if any */
+            if ( FFA_ID_IS_SECURE(fpi->id) )
+            {
+                memcpy(dst_buf, src_buf, dst_size);
+                dst_buf += dst_size;
+            }
+            else
+            {
+                printk(XENLOG_INFO "ffa: sp id 0x%04x skipped, bit 15 is 0\n",
+                       fpi->id);
Quoted 2 lines

We have already logged this in init_subscribers() below. Is there a risk of flooding the logs with this?

You are right, having it printed once is enough.
I will remove this LOG.

Cheers
Bertrand

Quoted 114 lines

Cheers,
Jens

Quoted 110 lines
xen/arch/arm/tee/ffa_partinfo.c +28 -8
+                ffa_sp_count--;
+            }
            src_buf += src_size;
        }
    }
@@ -276,10 +288,25 @@ static bool init_subscribers(uint16_t count, uint32_t fpi_size)
    {
        fpi = ffa_rx + n * fpi_size;
 
-        if ( fpi->partition_properties & FFA_PART_PROP_NOTIF_CREATED )
-            subscr_vm_created_count++;
-        if ( fpi->partition_properties & FFA_PART_PROP_NOTIF_DESTROYED )
-            subscr_vm_destroyed_count++;
+        /*
+         * We need to have secure partitions using bit 15 set convention for
+         * secure partition IDs.
+         * Inform the user with a log and discard giving created or destroy
+         * event to those IDs.
+         */
+        if ( !FFA_ID_IS_SECURE(fpi->id) )
+        {
+            printk(XENLOG_ERR "ffa: Firmware is not using bit 15 convention for IDs !!\n"
+                              "ffa: Secure partition with id 0x%04x cannot be used\n",
+                              fpi->id);
+        }
+        else
+        {
+            if ( fpi->partition_properties & FFA_PART_PROP_NOTIF_CREATED )
+                subscr_vm_created_count++;
+            if ( fpi->partition_properties & FFA_PART_PROP_NOTIF_DESTROYED )
+                subscr_vm_destroyed_count++;
+        }
    }
 
    if ( subscr_vm_created_count )
@@ -299,10 +326,13 @@ static bool init_subscribers(uint16_t count, uint32_t fpi_size)
    {
        fpi = ffa_rx + n * fpi_size;
 
-        if ( fpi->partition_properties & FFA_PART_PROP_NOTIF_CREATED )
-            subscr_vm_created[c_pos++] = fpi->id;
-        if ( fpi->partition_properties & FFA_PART_PROP_NOTIF_DESTROYED )
-            subscr_vm_destroyed[d_pos++] = fpi->id;
+        if ( FFA_ID_IS_SECURE(fpi->id) )
+        {
+            if ( fpi->partition_properties & FFA_PART_PROP_NOTIF_CREATED )
+                subscr_vm_created[c_pos++] = fpi->id;
+            if ( fpi->partition_properties & FFA_PART_PROP_NOTIF_DESTROYED )
+                subscr_vm_destroyed[d_pos++] = fpi->id;
+        }
    }
 
    return true;
xen/arch/arm/tee/ffa_private.h +7 -0
index e5bc73f9039e..afe69b43dbef 100644
@@ -108,6 +108,13 @@
 */
 #define FFA_CTX_TEARDOWN_DELAY          SECONDS(1)
 
+/*
+ * We rely on the convention suggested but not mandated by the FF-A
+ * specification that secure world endpoint identifiers have the bit 15
+ * set and normal world have it set to 0.
+ */
+#define FFA_ID_IS_SECURE(id)    ((id) & BIT(15, U))
+
 /* FF-A-1.1-REL0 section 10.9.2 Memory region handle, page 167 */
 #define FFA_HANDLE_HYP_FLAG             BIT(63, ULL)
 #define FFA_HANDLE_INVALID              0xffffffffffffffffULL
xen/arch/arm/tee/ffa_shm.c +11 -2
index efa5b67db8e1..29675f9ba3f7 100644
@@ -469,6 +469,7 @@ void ffa_handle_mem_share(struct cpu_user_regs *regs)
    int ret = FFA_RET_DENIED;
    uint32_t range_count;
    uint32_t region_offs;
+    uint16_t dst_id;
 
    if ( !ffa_fw_supports_fid(FFA_MEM_SHARE_64) )
    {
@@ -537,6 +538,15 @@ void ffa_handle_mem_share(struct cpu_user_regs *regs)
        goto out_unlock;
 
    mem_access = ctx->tx + trans.mem_access_offs;
+
+    dst_id = ACCESS_ONCE(mem_access->access_perm.endpoint_id);
+    if ( !FFA_ID_IS_SECURE(dst_id) )
+    {
+        /* we do not support sharing with VMs */
+        ret = FFA_RET_NOT_SUPPORTED;
+        goto out_unlock;
+    }
+
    if ( ACCESS_ONCE(mem_access->access_perm.perm) != FFA_MEM_ACC_RW )
    {
        ret = FFA_RET_NOT_SUPPORTED;
@@ -567,7 +577,7 @@ void ffa_handle_mem_share(struct cpu_user_regs *regs)
        goto out_unlock;
    }
    shm->sender_id = trans.sender_id;
-    shm->ep_id = ACCESS_ONCE(mem_access->access_perm.endpoint_id);
+    shm->ep_id = dst_id;
 
    /*
     * Check that the Composite memory region descriptor fits.
--

2.47.0

#12 lore raw

Re: [PATCH v2 04/10] xen/arm: ffa: Fine granular call support

In reply to Jens Wiklander
Quoted 38 lines · Jens Wiklander

Hi Bertrand,

Quoted 24 lines · Bertrand Marquis

Create a bitmap to store which feature is supported or not by the firmware and use it to filter which calls are done to the firmware.

While there reoder ABI definition by numbers to easily find the min and max ones.

---
Changes in v2:
- rename fw_feat to abi and macros to FFA_ABI to be coherent with the abi needed change done before
- rework the macros to be simpler by directly defining MIN and MAX using only Function ids
- check that requested function ids do not go over the bitmap size in ffa_fw_supports_fid
- add an ASSERT to make sure that we do not try to set bits outside of the bitmap
- turn off FF-A if there is not firmware support and adapt the commit message to reflect this
- add a compile time check that FFA_ABI_MIN < FFA_ABI_MAX
- remove spurious line removal
- restore proper cleanup of rxtx init in case of error
- reorder ABI by numbers

Quoted 8 lines
---
 xen/arch/arm/tee/ffa.c          | 28 +++++++++++++++---------
 xen/arch/arm/tee/ffa_notif.c    |  7 ++++++
 xen/arch/arm/tee/ffa_partinfo.c | 30 +++++++++++++++++++++++++-
 xen/arch/arm/tee/ffa_private.h  | 38 ++++++++++++++++++++++++++++-----
 xen/arch/arm/tee/ffa_rxtx.c     |  4 ++++
 xen/arch/arm/tee/ffa_shm.c      | 12 +++++++++++
 6 files changed, 103 insertions(+), 16 deletions(-)
Quoted 2 lines

Looks good.

I'm sorry, I'm having second thoughts about this patch. I have two concerns:
1. Xen will complain at boot with XENLOG_INFO if an ABI function listed in ffa_fw_abi_needed is missing. With the current list of ABI functions that's somewhat OK since it was a cause of disabling FF-A support before. But as the list grows it may become annoying or even confusing since when Xen supports more features it may complain more even if there is no regression compared to previous versions. If we need to print anything perhaps XENLOG_DEBUG is better.
2. FFA_FEATURES may return success for features not supported by the SPMC. How about only returning success for features in the ffa_fw_abi_needed bitmap?

Cheers,
Jens

Quoted 295 lines

Cheers,
Jens

Quoted 291 lines
xen/arch/arm/tee/ffa.c +18 -10
index 1ee6b2895e92..267d4435ac08 100644
@@ -72,7 +72,10 @@
 #include "ffa_private.h"
 
 /* Negotiated FF-A version to use with the SPMC, 0 if not there or supported */
-static uint32_t __ro_after_init ffa_fw_version;
+uint32_t __ro_after_init ffa_fw_version;
+
+/* Features supported by the SPMC or secure world when present */
+DECLARE_BITMAP(ffa_fw_abi_supported, FFA_ABI_BITMAP_SIZE);
 
 struct ffa_fw_abi {
     const uint32_t id;
@@ -177,6 +180,13 @@ static void handle_msg_send_direct_req(struct cpu_user_regs *regs, uint32_t fid)
     else
         mask = GENMASK_ULL(31, 0);
 
+    if ( !ffa_fw_supports_fid(fid) )
+    {
+        resp.a0 = FFA_ERROR;
+        resp.a2 = FFA_RET_NOT_SUPPORTED;
+        goto out;
+    }
+
     src_dst = get_user_reg(regs, 1);
     if ( (src_dst >> 16) != ffa_get_vm_id(d) )
     {
@@ -577,19 +587,16 @@ static bool ffa_probe(void)
     else
         ffa_fw_version = vers;
 
-    /*
-     * At the moment domains must support the same features used by Xen.
-     * TODO: Rework the code to allow domain to use a subset of the
-     * features supported.
-     */
     for ( unsigned int i = 0; i < ARRAY_SIZE(ffa_fw_abi_needed); i++ )
     {
-        if ( !ffa_abi_supported(ffa_fw_abi_needed[i].id) )
-        {
+        ASSERT(FFA_ABI_BITNUM(ffa_fw_abi_needed[i].id) < FFA_ABI_BITMAP_SIZE);
+
+        if ( ffa_abi_supported(ffa_fw_abi_needed[i].id) )
+            set_bit(FFA_ABI_BITNUM(ffa_fw_abi_needed[i].id),
+                    ffa_fw_abi_supported);
+        else
             printk(XENLOG_INFO "ARM FF-A Firmware does not support %s\n",
                    ffa_fw_abi_needed[i].name);
-            goto err_no_fw;
-        }
     }
 
     if ( !ffa_rxtx_init() )
@@ -611,6 +618,7 @@ err_rxtx_destroy:
     ffa_rxtx_destroy();
 err_no_fw:
     ffa_fw_version = 0;
+    bitmap_zero(ffa_fw_abi_supported, FFA_ABI_BITMAP_SIZE);
     printk(XENLOG_WARNING "ARM FF-A No firmware support\n");
 
     return false;
xen/arch/arm/tee/ffa_notif.c +7 -0
index 541e61d2f606..4b3e46318f4b 100644
@@ -377,6 +377,13 @@ void ffa_notif_init(void)
     unsigned int irq;
     int ret;
 
+    /* Only enable fw notification if all ABIs we need are supported */
+    if ( !(ffa_fw_supports_fid(FFA_NOTIFICATION_BITMAP_CREATE) &&
+           ffa_fw_supports_fid(FFA_NOTIFICATION_BITMAP_DESTROY) &&
+           ffa_fw_supports_fid(FFA_NOTIFICATION_GET) &&
+           ffa_fw_supports_fid(FFA_NOTIFICATION_INFO_GET_64)) )
+        return;
+
     arm_smccc_1_2_smc(&arg, &resp);
     if ( resp.a0 != FFA_SUCCESS_32 )
         return;
xen/arch/arm/tee/ffa_partinfo.c +29 -1
index 93a03c6bc672..99c48f0e5c05 100644
@@ -77,7 +77,15 @@ int32_t ffa_handle_partition_info_get(uint32_t w1, uint32_t w2, uint32_t w3,
      */
     if ( w5 == FFA_PARTITION_INFO_GET_COUNT_FLAG &&
          ctx->guest_vers == FFA_VERSION_1_1 )
-        return ffa_partition_info_get(w1, w2, w3, w4, w5, count, fpi_size);
+    {
+        if ( ffa_fw_supports_fid(FFA_PARTITION_INFO_GET) )
+            return ffa_partition_info_get(w1, w2, w3, w4, w5, count, fpi_size);
+        else
+        {
+            *count = 0;
+            return FFA_RET_OK;
+        }
+    }
     if ( w5 )
         return FFA_RET_INVALID_PARAMETERS;
 
@@ -87,6 +95,18 @@ int32_t ffa_handle_partition_info_get(uint32_t w1, uint32_t w2, uint32_t w3,
     if ( !spin_trylock(&ctx->rx_lock) )
         return FFA_RET_BUSY;
 
+    if ( !ffa_fw_supports_fid(FFA_PARTITION_INFO_GET) )
+    {
+        if ( ctx->guest_vers == FFA_VERSION_1_0 )
+            *fpi_size = sizeof(struct ffa_partition_info_1_0);
+        else
+            *fpi_size = sizeof(struct ffa_partition_info_1_1);
+
+        *count = 0;
+        ret = FFA_RET_OK;
+        goto out;
+    }
+
     if ( !ctx->page_count || !ctx->rx_is_free )
         goto out;
     spin_lock(&ffa_rx_buffer_lock);
@@ -250,6 +270,11 @@ bool ffa_partinfo_init(void)
     uint32_t count;
     int e;
 
+    if ( !ffa_fw_supports_fid(FFA_PARTITION_INFO_GET) ||
+         !ffa_fw_supports_fid(FFA_MSG_SEND_DIRECT_REQ_32) ||
+         !ffa_rx || !ffa_tx )
+        return false;
+
     e = ffa_partition_info_get(0, 0, 0, 0, 0, &count, &fpi_size);
     if ( e )
     {
@@ -313,6 +338,9 @@ int ffa_partinfo_domain_init(struct domain *d)
     unsigned int n;
     int32_t res;
 
+    if ( !ffa_fw_supports_fid(FFA_MSG_SEND_DIRECT_REQ_32) )
+        return 0;
+
     ctx->vm_destroy_bitmap = xzalloc_array(unsigned long, count);
     if ( !ctx->vm_destroy_bitmap )
         return -ENOMEM;
xen/arch/arm/tee/ffa_private.h +33 -5
index 045d9c4a0b56..85eb61c13464 100644
@@ -14,6 +14,7 @@
 #include <xen/spinlock.h>
 #include <xen/sched.h>
 #include <xen/time.h>
+#include <xen/bitmap.h>
 
 /* Error codes */
 #define FFA_RET_OK                      0
@@ -201,18 +202,17 @@
 #define FFA_INTERRUPT                   0x84000062U
 #define FFA_VERSION                     0x84000063U
 #define FFA_FEATURES                    0x84000064U
-#define FFA_RX_ACQUIRE                  0x84000084U
 #define FFA_RX_RELEASE                  0x84000065U
 #define FFA_RXTX_MAP_32                 0x84000066U
 #define FFA_RXTX_MAP_64                 0xC4000066U
 #define FFA_RXTX_UNMAP                  0x84000067U
 #define FFA_PARTITION_INFO_GET          0x84000068U
 #define FFA_ID_GET                      0x84000069U
-#define FFA_SPM_ID_GET                  0x84000085U
+#define FFA_MSG_POLL                    0x8400006AU
 #define FFA_MSG_WAIT                    0x8400006BU
 #define FFA_MSG_YIELD                   0x8400006CU
 #define FFA_RUN                         0x8400006DU
-#define FFA_MSG_SEND2                   0x84000086U
+#define FFA_MSG_SEND                    0x8400006EU
 #define FFA_MSG_SEND_DIRECT_REQ_32      0x8400006FU
 #define FFA_MSG_SEND_DIRECT_REQ_64      0xC400006FU
 #define FFA_MSG_SEND_DIRECT_RESP_32     0x84000070U
@@ -230,8 +230,6 @@
 #define FFA_MEM_RECLAIM                 0x84000077U
 #define FFA_MEM_FRAG_RX                 0x8400007AU
 #define FFA_MEM_FRAG_TX                 0x8400007BU
-#define FFA_MSG_SEND                    0x8400006EU
-#define FFA_MSG_POLL                    0x8400006AU
 #define FFA_NOTIFICATION_BITMAP_CREATE  0x8400007DU
 #define FFA_NOTIFICATION_BITMAP_DESTROY 0x8400007EU
 #define FFA_NOTIFICATION_BIND           0x8400007FU
@@ -240,6 +238,25 @@
 #define FFA_NOTIFICATION_GET            0x84000082U
 #define FFA_NOTIFICATION_INFO_GET_32    0x84000083U
 #define FFA_NOTIFICATION_INFO_GET_64    0xC4000083U
+#define FFA_RX_ACQUIRE                  0x84000084U
+#define FFA_SPM_ID_GET                  0x84000085U
+#define FFA_MSG_SEND2                   0x84000086U
+
+/**
+ * Encoding of features supported or not by the fw in a bitmap:
+ * - Function IDs are going from 0x60 to 0xFF
+ * - A function can be supported in 32 and/or 64bit
+ * The bitmap has one bit for each function in 32 and 64 bit.
+ */
+#define FFA_ABI_ID(id)        ((id) & ARM_SMCCC_FUNC_MASK)
+#define FFA_ABI_CONV(id)      (((id) >> ARM_SMCCC_CONV_SHIFT) & BIT(0,U))
+
+#define FFA_ABI_MIN           FFA_ABI_ID(FFA_ERROR)
+#define FFA_ABI_MAX           FFA_ABI_ID(FFA_MSG_SEND2)
+
+#define FFA_ABI_BITMAP_SIZE   (2 * (FFA_ABI_MAX - FFA_ABI_MIN + 1))
+#define FFA_ABI_BITNUM(id)    ((FFA_ABI_ID(id) - FFA_ABI_MIN) << 1 | \
+                               FFA_ABI_CONV(id))
 
 struct ffa_ctx_notif {
     bool enabled;
@@ -289,6 +306,8 @@ extern void *ffa_rx;
 extern void *ffa_tx;
 extern spinlock_t ffa_rx_buffer_lock;
 extern spinlock_t ffa_tx_buffer_lock;
+extern uint32_t __ro_after_init ffa_fw_version;
+extern DECLARE_BITMAP(ffa_fw_abi_supported, FFA_ABI_BITMAP_SIZE);
 
 bool ffa_shm_domain_destroy(struct domain *d);
 void ffa_handle_mem_share(struct cpu_user_regs *regs);
@@ -401,4 +420,13 @@ static inline int32_t ffa_rx_release(void)
     return ffa_simple_call(FFA_RX_RELEASE, 0, 0, 0, 0);
 }
 
+static inline bool ffa_fw_supports_fid(uint32_t fid)
+{
+    BUILD_BUG_ON(FFA_ABI_MIN > FFA_ABI_MAX);
+
+    if ( FFA_ABI_BITNUM(fid) > FFA_ABI_BITMAP_SIZE)
+        return false;
+    return test_bit(FFA_ABI_BITNUM(fid), ffa_fw_abi_supported);
+}
+
 #endif /*__FFA_PRIVATE_H__*/
xen/arch/arm/tee/ffa_rxtx.c +4 -0
index 661764052e67..b6931c855779 100644
@@ -193,6 +193,10 @@ bool ffa_rxtx_init(void)
 {
     int e;
 
+    /* Firmware not there or not supporting */
+    if ( !ffa_fw_supports_fid(FFA_RXTX_MAP_64) )
+        return false;
+
     ffa_rx = alloc_xenheap_pages(get_order_from_pages(FFA_RXTX_PAGE_COUNT), 0);
     if ( !ffa_rx )
         return false;
xen/arch/arm/tee/ffa_shm.c +12 -1
index 370d83ec5cf8..efa5b67db8e1 100644
@@ -149,6 +149,9 @@ static int32_t ffa_mem_share(uint32_t tot_len, uint32_t frag_len,
 static int32_t ffa_mem_reclaim(uint32_t handle_lo, uint32_t handle_hi,
                                uint32_t flags)
 {
+    if ( !ffa_fw_supports_fid(FFA_MEM_RECLAIM) )
+        return FFA_RET_NOT_SUPPORTED;
+
     return ffa_simple_call(FFA_MEM_RECLAIM, handle_lo, handle_hi, flags, 0);
 }
 
@@ -467,6 +470,12 @@ void ffa_handle_mem_share(struct cpu_user_regs *regs)
     uint32_t range_count;
     uint32_t region_offs;
 
+    if ( !ffa_fw_supports_fid(FFA_MEM_SHARE_64) )
+    {
+        ret = FFA_RET_NOT_SUPPORTED;
+        goto out_set_ret;
+    }
+
     /*
      * We're only accepting memory transaction descriptors via the rx/tx
      * buffer.
@@ -621,6 +630,9 @@ int ffa_handle_mem_reclaim(uint64_t handle, uint32_t flags)
     register_t handle_lo;
     int ret;
 
+    if ( !ffa_fw_supports_fid(FFA_MEM_RECLAIM) )
+        return FFA_RET_NOT_SUPPORTED;
+
     spin_lock(&ctx->lock);
     shm = find_shm_mem(ctx, handle);
     if ( shm )
--

2.47.0

#31 lore raw

Re: [PATCH v2 10/10] xen/arm: ffa: Add indirect message support

In reply to Bertrand Marquis

Hi Bertrand,

Quoted 91 lines · Bertrand Marquis

Add support for FFA_MSG_SEND2 to send indirect messages from a VM to a secure partition.

---
Changes in v2:
- rebase

---
 xen/arch/arm/tee/ffa.c         |  5 ++++
 xen/arch/arm/tee/ffa_msg.c     | 49 ++++++++++++++++++++++++++++++++++
 xen/arch/arm/tee/ffa_private.h |  1 +
 3 files changed, 55 insertions(+)
xen/arch/arm/tee/ffa.c +5 -0
index 3a9525aa4598..21d41b452dc9 100644
@@ -101,6 +101,7 @@ static const struct ffa_fw_abi ffa_fw_abi_needed[] = {
     FW_ABI(FFA_MEM_RECLAIM),
     FW_ABI(FFA_MSG_SEND_DIRECT_REQ_32),
     FW_ABI(FFA_MSG_SEND_DIRECT_REQ_64),
+    FW_ABI(FFA_MSG_SEND2),
 };
 
 /*
@@ -195,6 +196,7 @@ static void handle_features(struct cpu_user_regs *regs)
     case FFA_PARTITION_INFO_GET:
     case FFA_MSG_SEND_DIRECT_REQ_32:
     case FFA_MSG_SEND_DIRECT_REQ_64:
+    case FFA_MSG_SEND2:
         ffa_set_regs_success(regs, 0, 0);
         break;
     case FFA_MEM_SHARE_64:
@@ -275,6 +277,9 @@ static bool ffa_handle_call(struct cpu_user_regs *regs)
     case FFA_MSG_SEND_DIRECT_REQ_64:
         ffa_handle_msg_send_direct_req(regs, fid);
         return true;
+    case FFA_MSG_SEND2:
+        e = ffa_handle_msg_send2(regs);
+        break;
     case FFA_MEM_SHARE_32:
     case FFA_MEM_SHARE_64:
         ffa_handle_mem_share(regs);
xen/arch/arm/tee/ffa_msg.c +32 -0
index ae263e54890e..335f246ba657 100644
@@ -12,6 +12,15 @@
 
 #include "ffa_private.h"
 
+/* Encoding of partition message in RX/TX buffer */
+struct ffa_part_msg_rxtx {
+    uint32_t flags;
+    uint32_t reserved;
+    uint32_t msg_offset;
+    uint32_t send_recv_id;
+    uint32_t msg_size;
+};
+
 void ffa_handle_msg_send_direct_req(struct cpu_user_regs *regs, uint32_t fid)
 {
     struct arm_smccc_1_2_regs arg = { .a0 = fid, };
@@ -78,3 +87,43 @@ out:
                  resp.a4 & mask, resp.a5 & mask, resp.a6 & mask,
                  resp.a7 & mask);
 }
+
+int32_t ffa_handle_msg_send2(struct cpu_user_regs *regs)
+{
+    struct domain *src_d = current->domain;
+    struct ffa_ctx *src_ctx = src_d->arch.tee;
+    const struct ffa_part_msg_rxtx *src_msg;
+    uint16_t dst_id, src_id;
+    int32_t ret;
+
+    if ( !ffa_fw_supports_fid(FFA_MSG_SEND2) )
+        return FFA_RET_NOT_SUPPORTED;
+
+    if ( !spin_trylock(&src_ctx->tx_lock) )
+        return FFA_RET_BUSY;
+
+    src_msg = src_ctx->tx;
+    src_id = src_msg->send_recv_id >> 16;
+    dst_id = src_msg->send_recv_id & GENMASK(15,0);
+
+    if ( src_id != ffa_get_vm_id(src_d) || !FFA_ID_IS_SECURE(dst_id) )
+    {
+        ret = FFA_RET_INVALID_PARAMETERS;
+        goto out_unlock_tx;

xen/arch/arm/tee/ffa_msg.c out: · line 122

Quoted 10 lines
xen/arch/arm/tee/ffa_msg.c +10 -0
+    }
+
+    /* check source message fits in buffer */
+    if ( src_ctx->page_count * FFA_PAGE_SIZE <
+         src_msg->msg_offset + src_msg->msg_size ||
+         src_msg->msg_offset < sizeof(struct ffa_part_msg_rxtx) )
+    {
+        ret = FFA_RET_INVALID_PARAMETERS;
+        goto out_unlock_tx;
+    }

The guest can change src_mst at any moment with another CPU so these tests are only sanity checks. The SPMC will also have to lock and do the same tests again. So the tests here will only in the best case (in case the guest is misbehaving) save us from entering the SPMC only to get an error back. The lock makes sense since we could have concurrent calls to FFA_MEM_SHARE. How about removing the tests?

xen/arch/arm/tee/ffa_msg.c out: · line 124

Quoted 2 lines
xen/arch/arm/tee/ffa_msg.c +2 -0
+
+    ret = ffa_simple_call(FFA_MSG_SEND2, ((uint32_t)src_id) << 16, 0, 0, 0);

I'd rather use ffa_get_vm_id(src_d) instead of src_id.

Cheers,
Jens

Quoted 20 lines
xen/arch/arm/tee/ffa_msg.c +5 -0
+
+out_unlock_tx:
+    spin_unlock(&src_ctx->tx_lock);
+    return ret;
+}
xen/arch/arm/tee/ffa_private.h +1 -1
index 973ee55be09b..d441c0ca5598 100644
@@ -359,6 +359,7 @@ void ffa_handle_notification_get(struct cpu_user_regs *regs);
 int ffa_handle_notification_set(struct cpu_user_regs *regs);
 
 void ffa_handle_msg_send_direct_req(struct cpu_user_regs *regs, uint32_t fid);
+int32_t ffa_handle_msg_send2(struct cpu_user_regs *regs);
 
 static inline uint16_t ffa_get_vm_id(const struct domain *d)
 {
--

2.47.0

#22 lore raw

Re: [PATCH v2 07/10] xen/arm: ffa: Transmit RXTX buffers to the SPMC

In reply to Jens Wiklander

Hi Jens,

Quoted 94 lines · Jens Wiklander

Hi Bertrand,

Quoted 88 lines · Bertrand Marquis

When an RXTX buffer is mapped by a VM transmit it to the SPMC when it supports RX_ACQUIRE.
As a consequence of that, we must acquire the RX buffer of a VM from the SPMC when we want to use it:
- create a generic acquire and release function to get the rx buffer of a VM which gets it from the SPMC when supported
- rename the rx_acquire to hyp_rx_acquire to remove confusion
- rework the rx_lock to only lock access to rx_is_free and only allow usage of the rx buffer to one who managed to acquire it, thus removing the trylock and returning busy if rx_is_free is false

As part of this change move some structure definition to ffa_private from ffa_shm as those are need for the MAP call with the SPMC.

---
Changes in v2:
- unmap VM rxtx buffer in SPMC on unmap call or on VM destroy
- rework the unmap call to the SPMC to properly pass the VM ID
---

xen/arch/arm/tee/ffa.c          |   2 +-
xen/arch/arm/tee/ffa_partinfo.c |  29 ++----
xen/arch/arm/tee/ffa_private.h  |  22 ++++-
xen/arch/arm/tee/ffa_rxtx.c     | 158 ++++++++++++++++++++++++++------
xen/arch/arm/tee/ffa_shm.c      |  15 ---
5 files changed, 161 insertions(+), 65 deletions(-)
xen/arch/arm/tee/ffa.c +1 -1
index a292003ca9fe..40ea5398fa21 100644
@@ -347,7 +347,7 @@ static bool ffa_handle_call(struct cpu_user_regs *regs)
        ffa_handle_partition_info_get(regs);
        return true;
    case FFA_RX_RELEASE:
-        e = ffa_handle_rx_release();
+        e = ffa_rx_release(d);
        break;
    case FFA_MSG_SEND_DIRECT_REQ_32:
    case FFA_MSG_SEND_DIRECT_REQ_64:
xen/arch/arm/tee/ffa_partinfo.c +4 -12
index 3cf801523296..fde187dba4e5 100644
@@ -121,11 +121,9 @@ void ffa_handle_partition_info_get(struct cpu_user_regs *regs)
        goto out;
    }
 
-    if ( !spin_trylock(&ctx->rx_lock) )
-    {
-        ret = FFA_RET_BUSY;
+    ret = ffa_rx_acquire(d);
+    if ( ret != FFA_RET_OK )
        goto out;
-    }
 
    dst_buf = ctx->rx;
 
@@ -135,22 +133,16 @@ void ffa_handle_partition_info_get(struct cpu_user_regs *regs)
        goto out_rx_release;
    }
 
-    if ( !ctx->page_count || !ctx->rx_is_free )
-    {
-        ret = FFA_RET_DENIED;
-        goto out_rx_release;
-    }
-
    spin_lock(&ffa_rx_buffer_lock);
 
    ret = ffa_partition_info_get(uuid, 0, &ffa_sp_count, &src_size);
 
    if ( ret )
-        goto out_rx_buf_unlock;
+        goto out_rx_hyp_unlock;
 
    /*
     * ffa_partition_info_get() succeeded so we now own the RX buffer we
-     * share with the SPMC. We must give it back using ffa_rx_release()
+     * share with the SPMC. We must give it back using ffa_hyp_rx_release()
     * once we've copied the content.
     */
 
@@ -193,15 +185,13 @@ void ffa_handle_partition_info_get(struct cpu_user_regs *regs)
        }
    }
 
Quoted 15 lines

xen/arch/arm/tee/ffa_partinfo.c void ffa_handle_partition_info_get(struct cpu_user_regs *regs) · line 194

Quoted 13 lines
xen/arch/arm/tee/ffa_partinfo.c +0 -2
-    ctx->rx_is_free = false;
-

out_rx_hyp_release:

xen/arch/arm/tee/ffa_partinfo.c +2 -2
-    ffa_rx_release();
-out_rx_buf_unlock:
+    ffa_hyp_rx_release();
+out_rx_hyp_unlock:
    spin_unlock(&ffa_rx_buffer_lock);

out_rx_release:

xen/arch/arm/tee/ffa_partinfo.c +2 -2
-    spin_unlock(&ctx->rx_lock);
-
+    if ( ret != FFA_RET_OK )
+        ffa_rx_release(d);

Please comment on why ffa_rx_release() must only be called on failure.

It is because the buffer contains data for the caller in case of success so it must be released by the caller.

Do i get it right that you want me to add a comment saying that in the code and not only tell you here ?

Quoted 104 lines
Quoted 104 lines

out:
if ( ret )
ffa_set_regs_error(regs, ret);

xen/arch/arm/tee/ffa_partinfo.c +0 -0
@@ -368,8 +358,7 @@ bool ffa_partinfo_init(void)
    ret = init_subscribers(count, fpi_size);
 

out:

xen/arch/arm/tee/ffa_partinfo.c +1 -2
-    ffa_rx_release();
-
+    ffa_hyp_rx_release();
    return ret;

}

xen/arch/arm/tee/ffa_private.h +19 -3
index afe69b43dbef..9adfe687c3c9 100644
@@ -265,6 +265,21 @@
 #define FFA_ABI_BITNUM(id)    ((FFA_ABI_ID(id) - FFA_ABI_MIN) << 1 | \
                               FFA_ABI_CONV(id))
 
+/* Constituent memory region descriptor */
+struct ffa_address_range {
+    uint64_t address;
+    uint32_t page_count;
+    uint32_t reserved;
+};
+
+/* Composite memory region descriptor */
+struct ffa_mem_region {
+    uint32_t total_page_count;
+    uint32_t address_range_count;
+    uint64_t reserved;
+    struct ffa_address_range address_range_array[];
+};
+
 struct ffa_ctx_notif {
    bool enabled;
 
@@ -292,7 +307,7 @@ struct ffa_ctx {
    struct ffa_ctx_notif notif;
    /*
     * tx_lock is used to serialize access to tx
-     * rx_lock is used to serialize access to rx
+     * rx_lock is used to serialize access to rx_is_free
     * lock is used for the rest in this struct
     */
    spinlock_t tx_lock;
@@ -331,7 +346,8 @@ void ffa_rxtx_domain_destroy(struct domain *d);
 uint32_t ffa_handle_rxtx_map(uint32_t fid, register_t tx_addr,
                            register_t rx_addr, uint32_t page_count);
 uint32_t ffa_handle_rxtx_unmap(void);
-int32_t ffa_handle_rx_release(void);
+int32_t ffa_rx_acquire(struct domain *d);
+int32_t ffa_rx_release(struct domain *d);
 
 void ffa_notif_init(void);
 void ffa_notif_init_interrupt(void);
@@ -420,7 +436,7 @@ static inline int32_t ffa_simple_call(uint32_t fid, register_t a1,
    return ffa_get_ret_code(&resp);
 }
 
-static inline int32_t ffa_rx_release(void)
+static inline int32_t ffa_hyp_rx_release(void)
 {
    return ffa_simple_call(FFA_RX_RELEASE, 0, 0, 0, 0);
 }
xen/arch/arm/tee/ffa_rxtx.c +14 -0
index b6931c855779..a5d43e51f843 100644
@@ -30,6 +30,17 @@ struct ffa_endpoint_rxtx_descriptor_1_1 {
    uint32_t tx_region_offs;
 };
 
+static int32_t ffa_rxtx_map(paddr_t tx_addr, paddr_t rx_addr,
+                            uint32_t page_count)
+{
+    return ffa_simple_call(FFA_RXTX_MAP_64, tx_addr, rx_addr, page_count, 0);
+}
+
+static int32_t ffa_rxtx_unmap(uint16_t id)
+{
+    return ffa_simple_call(FFA_RXTX_UNMAP, ((uint64_t)id)<<16, 0, 0, 0);
+}
+
 uint32_t ffa_handle_rxtx_map(uint32_t fid, register_t tx_addr,
                            register_t rx_addr, uint32_t page_count)
 {
@@ -42,6 +53,9 @@ uint32_t ffa_handle_rxtx_map(uint32_t fid, register_t tx_addr,
    void *rx;
    void *tx;
 
+    /* The code is considering that we only get one page for now */
+    BUILD_BUG_ON(FFA_MAX_RXTX_PAGE_COUNT != 1);
+
    if ( !smccc_is_conv_64(fid) )
    {
        /*
@@ -87,6 +101,65 @@ uint32_t ffa_handle_rxtx_map(uint32_t fid, register_t tx_addr,
    if ( !rx )
        goto err_unmap_tx;
 
Quoted 15 lines

xen/arch/arm/tee/ffa_rxtx.c uint32_t ffa_handle_rxtx_map(uint32_t fid, register_t tx_addr, · line 116

Quoted 13 lines
xen/arch/arm/tee/ffa_rxtx.c +13 -0
+    /*
+     * Transmit the RX/TX buffer information to the SPM if acquire is supported
+     * as the spec says that if not there is not need to acquire/release/map
+     * rxtx buffers from the SPMC
+     */
+    if ( ffa_fw_supports_fid(FFA_RX_ACQUIRE) )
+    {
+        struct ffa_endpoint_rxtx_descriptor_1_1 *rxtx_desc;
+        struct ffa_mem_region *mem_reg;
+
+        /* All must fit in our TX buffer */
+        BUILD_BUG_ON((sizeof(*rxtx_desc) + sizeof(*mem_reg)*2 +
+                      sizeof(struct ffa_address_range)*2) > FFA_PAGE_SIZE);

Please add space before and after the binary operator "*".

Ack

Quoted 1 line

The size of the TX buffer is FFA_RXTX_PAGE_COUNT * FFA_PAGE_SIZE.

Very right, I will fix that in v3.

Quoted 1 line

Nit: the outer parenthesis around the left expression aren't needed.

Ack

Quoted 34 lines
Quoted 34 lines
xen/arch/arm/tee/ffa_rxtx.c +33 -0
+
+        spin_lock(&ffa_tx_buffer_lock);
+        rxtx_desc = ffa_tx;
+
+        /*
+         * We have only one page for each so we pack everything:
+         * - rx region descriptor
+         * - rx region range
+         * - tx region descriptor
+         * - tx region range
+         */
+        rxtx_desc->sender_id = ffa_get_vm_id(d);
+        rxtx_desc->reserved = 0;
+        rxtx_desc->rx_region_offs = sizeof(*rxtx_desc);
+        rxtx_desc->tx_region_offs = sizeof(*rxtx_desc) +
+                                    offsetof(struct ffa_mem_region,
+                                             address_range_array[1]);
+
+        /* rx buffer */
+        mem_reg = ffa_tx + sizeof(*rxtx_desc);
+        mem_reg->total_page_count = 1;
+        mem_reg->address_range_count = 1;
+        mem_reg->reserved = 0;
+
+        mem_reg->address_range_array[0].address = page_to_maddr(rx_pg);
+        mem_reg->address_range_array[0].page_count = 1;
+        mem_reg->address_range_array[0].reserved = 0;
+
+        /* tx buffer */
+        mem_reg = ffa_tx + rxtx_desc->tx_region_offs;
+        mem_reg->total_page_count = 1;
+        mem_reg->address_range_count = 1;
+        mem_reg->reserved = 0;
Quoted 8 lines

xen/arch/arm/tee/ffa_rxtx.c uint32_t ffa_handle_rxtx_map(uint32_t fid, register_t tx_addr, · line 155

Quoted 6 lines
xen/arch/arm/tee/ffa_rxtx.c +6 -0
+
+        mem_reg->address_range_array[0].address = page_to_maddr(tx_pg);
+        mem_reg->address_range_array[0].page_count = 1;
+        mem_reg->address_range_array[0].reserved = 0;
+
+        ret = ffa_rxtx_map(0, 0, 1);

The last parameter is also MBZ when forwarding on behalf of an endpoint.

Ack, will fix in v3.

Cheers
Bertrand

Quoted 182 lines

Cheers,
Jens

Quoted 178 lines
xen/arch/arm/tee/ffa_rxtx.c +9 -0
+
+        spin_unlock(&ffa_tx_buffer_lock);
+
+        if ( ret != FFA_RET_OK )
+            goto err_unmap_rx;
+    }
+
    ctx->rx = rx;
    ctx->tx = tx;
    ctx->rx_pg = rx_pg;
@@ -95,6 +168,8 @@ uint32_t ffa_handle_rxtx_map(uint32_t fid, register_t tx_addr,
    ctx->rx_is_free = true;
    return FFA_RET_OK;
 
+err_unmap_rx:
+    unmap_domain_page_global(rx);

err_unmap_tx:

    unmap_domain_page_global(tx);

err_put_rx_pg:

xen/arch/arm/tee/ffa_rxtx.c +55 -10
@@ -105,8 +180,22 @@ err_put_tx_pg:
    return ret;
 }
 
-static void rxtx_unmap(struct ffa_ctx *ctx)
+static uint32_t  rxtx_unmap(struct domain *d)
 {
+    struct ffa_ctx *ctx = d->arch.tee;
+
+    if ( !ctx->page_count )
+        return FFA_RET_INVALID_PARAMETERS;
+
+    if ( ffa_fw_supports_fid(FFA_RX_ACQUIRE) )
+    {
+        uint32_t ret;
+
+        ret = ffa_rxtx_unmap(ffa_get_vm_id(d));
+        if ( ret != FFA_RET_OK )
+            return ret;
+    }
+
    unmap_domain_page_global(ctx->rx);
    unmap_domain_page_global(ctx->tx);
    put_page(ctx->rx_pg);
@@ -117,32 +206,63 @@ static void rxtx_unmap(struct ffa_ctx *ctx)
    ctx->tx_pg = NULL;
    ctx->page_count = 0;
    ctx->rx_is_free = false;
+
+    return FFA_RET_OK;
 }
 
 uint32_t ffa_handle_rxtx_unmap(void)
 {
-    struct domain *d = current->domain;
+    return rxtx_unmap(current->domain);
+}
+
+int32_t ffa_rx_acquire(struct domain *d)
+{
+    int32_t ret = FFA_RET_OK;
    struct ffa_ctx *ctx = d->arch.tee;
 
-    if ( !ctx->rx )
-        return FFA_RET_INVALID_PARAMETERS;
+    spin_lock(&ctx->rx_lock);
 
-    rxtx_unmap(ctx);
+    if ( !ctx->page_count )
+    {
+        ret = FFA_RET_DENIED;
+        goto out;
+    }
 
-    return FFA_RET_OK;
+    if ( !ctx->rx_is_free )
+    {
+        ret = FFA_RET_BUSY;
+        goto out;
+    }
+
+    if ( ffa_fw_supports_fid(FFA_RX_ACQUIRE) )
+    {
+        ret = ffa_simple_call(FFA_RX_ACQUIRE, ffa_get_vm_id(d), 0, 0, 0);
+        if ( ret != FFA_RET_OK )
+            goto out;
+    }
+    ctx->rx_is_free = false;
+out:
+    spin_unlock(&ctx->rx_lock);
+
+    return ret;
 }
 
-int32_t ffa_handle_rx_release(void)
+int32_t ffa_rx_release(struct domain *d)
 {
    int32_t ret = FFA_RET_DENIED;
-    struct domain *d = current->domain;
    struct ffa_ctx *ctx = d->arch.tee;
 
-    if ( !spin_trylock(&ctx->rx_lock) )
-        return FFA_RET_BUSY;
+    spin_lock(&ctx->rx_lock);
 
    if ( !ctx->page_count || ctx->rx_is_free )
        goto out;
+
+    if ( ffa_fw_supports_fid(FFA_RX_ACQUIRE) )
+    {
+        ret = ffa_simple_call(FFA_RX_RELEASE, ffa_get_vm_id(d), 0, 0, 0);
+        if ( ret != FFA_RET_OK )
+            goto out;
+    }
    ret = FFA_RET_OK;
    ctx->rx_is_free = true;

out:

xen/arch/arm/tee/ffa_rxtx.c +2 -16
@@ -151,23 +271,9 @@ out:
    return ret;
 }
 
-static int32_t ffa_rxtx_map(paddr_t tx_addr, paddr_t rx_addr,
-                            uint32_t page_count)
-{
-    return ffa_simple_call(FFA_RXTX_MAP_64, tx_addr, rx_addr, page_count, 0);
-}
-
-static int32_t ffa_rxtx_unmap(void)
-{
-    return ffa_simple_call(FFA_RXTX_UNMAP, 0, 0, 0, 0);
-}
-
 void ffa_rxtx_domain_destroy(struct domain *d)
 {
-    struct ffa_ctx *ctx = d->arch.tee;
-
-    if ( ctx->rx )
-        rxtx_unmap(ctx);
+    rxtx_unmap(d);
 }
 
 void ffa_rxtx_destroy(void)
@@ -186,7 +292,7 @@ void ffa_rxtx_destroy(void)
    }
 
    if ( need_unmap )
-        ffa_rxtx_unmap();
+        ffa_rxtx_unmap(0);
 }
 
 bool ffa_rxtx_init(void)
xen/arch/arm/tee/ffa_shm.c +0 -16
index 29675f9ba3f7..d628c1b70609 100644
@@ -16,21 +16,6 @@
 
 #include "ffa_private.h"
 
-/* Constituent memory region descriptor */
-struct ffa_address_range {
-    uint64_t address;
-    uint32_t page_count;
-    uint32_t reserved;
-};
-
-/* Composite memory region descriptor */
-struct ffa_mem_region {
-    uint32_t total_page_count;
-    uint32_t address_range_count;
-    uint64_t reserved;
-    struct ffa_address_range address_range_array[];
-};
-
 /* Memory access permissions descriptor */
 struct ffa_mem_access_perm {
    uint16_t endpoint_id;
--

2.47.0

#28 lore raw

Re: [PATCH v2 09/10] xen/arm: ffa: Remove per VM notif_enabled

In reply to Jens Wiklander

Hi Jens,

Quoted 89 lines · Jens Wiklander

Hi Bertrand,

Quoted 83 lines · Bertrand Marquis

Remove the per VM flag to store if notifications are enabled or not as the only case where they are not, if notifications are enabled globally, will make the VM creation fail.
Also use the opportunity to always give the notifications interrupts IDs to VM. If the firmware does not support notifications, there won't be any generated and setting one will give back a NOT_SUPPORTED.

---
Changes in v2:
- rebase
---

xen/arch/arm/tee/ffa.c         | 17 +++--------------
xen/arch/arm/tee/ffa_notif.c   | 10 +---------
xen/arch/arm/tee/ffa_private.h |  2 --
3 files changed, 4 insertions(+), 25 deletions(-)
xen/arch/arm/tee/ffa.c +3 -14
index 72826b49d2aa..3a9525aa4598 100644
@@ -169,8 +169,6 @@ static void handle_version(struct cpu_user_regs *regs)
 
 static void handle_features(struct cpu_user_regs *regs)
 {
-    struct domain *d = current->domain;
-    struct ffa_ctx *ctx = d->arch.tee;
    uint32_t a1 = get_user_reg(regs, 1);
    unsigned int n;
 
@@ -218,16 +216,10 @@ static void handle_features(struct cpu_user_regs *regs)
        ffa_set_regs_success(regs, 0, 0);
        break;
    case FFA_FEATURE_NOTIF_PEND_INTR:
-        if ( ctx->notif.enabled )
-            ffa_set_regs_success(regs, GUEST_FFA_NOTIF_PEND_INTR_ID, 0);
-        else
-            ffa_set_regs_error(regs, FFA_RET_NOT_SUPPORTED);
+        ffa_set_regs_success(regs, GUEST_FFA_NOTIF_PEND_INTR_ID, 0);
        break;
    case FFA_FEATURE_SCHEDULE_RECV_INTR:
-        if ( ctx->notif.enabled )
-            ffa_set_regs_success(regs, GUEST_FFA_SCHEDULE_RECV_INTR_ID, 0);
-        else
-            ffa_set_regs_error(regs, FFA_RET_NOT_SUPPORTED);
+        ffa_set_regs_success(regs, GUEST_FFA_SCHEDULE_RECV_INTR_ID, 0);
        break;
 
    case FFA_NOTIFICATION_BIND:
@@ -236,10 +228,7 @@ static void handle_features(struct cpu_user_regs *regs)
    case FFA_NOTIFICATION_SET:
    case FFA_NOTIFICATION_INFO_GET_32:
    case FFA_NOTIFICATION_INFO_GET_64:
-        if ( ctx->notif.enabled )
-            ffa_set_regs_success(regs, 0, 0);
-        else
-            ffa_set_regs_error(regs, FFA_RET_NOT_SUPPORTED);
+        ffa_set_regs_success(regs, 0, 0);
        break;
    default:
        ffa_set_regs_error(regs, FFA_RET_NOT_SUPPORTED);
xen/arch/arm/tee/ffa_notif.c +0 -3
index 4b3e46318f4b..3c6418e62e2b 100644
@@ -405,7 +405,6 @@ void ffa_notif_init(void)
 
 int ffa_notif_domain_init(struct domain *d)
 {
-    struct ffa_ctx *ctx = d->arch.tee;
    int32_t res;
 
    if ( !notif_enabled )
@@ -415,18 +414,11 @@ int ffa_notif_domain_init(struct domain *d)
    if ( res )
        return -ENOMEM;
 
-    ctx->notif.enabled = true;
-
    return 0;
 }
 
Quoted 14 lines

xen/arch/arm/tee/ffa_notif.c int ffa_notif_domain_init(struct domain *d) · line 423

Quoted 8 lines
xen/arch/arm/tee/ffa_notif.c +1 -4
 void ffa_notif_domain_destroy(struct domain *d)
 {
-    struct ffa_ctx *ctx = d->arch.tee;
-
-    if ( ctx->notif.enabled )
-    {
+    if ( notif_enabled )
        ffa_notification_bitmap_destroy(ffa_get_vm_id(d));

This call may now be done even if there hasn't been a successful call to ffa_notification_bitmap_create().
A comment mentioning this and that it's harmless (if we can be sure it is) would be nice.

You mean in the case where it failed during domain_init ?

I can add the following comment:
Call bitmap_destroy even if bitmap create failed as the SPMC should return an error that we will ignore

Would that be ok ?

Cheers
Bertrand

Quoted 21 lines

Cheers,
Jens

Quoted 18 lines
xen/arch/arm/tee/ffa_notif.c +0 -2
-        ctx->notif.enabled = false;
-    }
 }
xen/arch/arm/tee/ffa_private.h +0 -3
index 02162e0ee4c7..973ee55be09b 100644
@@ -281,8 +281,6 @@ struct ffa_mem_region {
 };
 
 struct ffa_ctx_notif {
-    bool enabled;
-
    /*
     * True if domain is reported by FFA_NOTIFICATION_INFO_GET to have
     * pending global notifications.
--

2.47.0

#13 lore raw

Re: [PATCH v2 04/10] xen/arm: ffa: Fine granular call support

In reply to Jens Wiklander

Hi Jens,

Quoted 45 lines · Jens Wiklander
Quoted 38 lines · Jens Wiklander

Hi Bertrand,

Quoted 25 lines · Bertrand Marquis

Create a bitmap to store which feature is supported or not by the firmware and use it to filter which calls are done to the firmware.

While there reoder ABI definition by numbers to easily find the min and max ones.

---
Changes in v2:
- rename fw_feat to abi and macros to FFA_ABI to be coherent with the abi needed change done before
- rework the macros to be simpler by directly defining MIN and MAX using only Function ids
- check that requested function ids do not go over the bitmap size in ffa_fw_supports_fid
- add an ASSERT to make sure that we do not try to set bits outside of the bitmap
- turn off FF-A if there is not firmware support and adapt the commit message to reflect this
- add a compile time check that FFA_ABI_MIN < FFA_ABI_MAX
- remove spurious line removal
- restore proper cleanup of rxtx init in case of error
- reorder ABI by numbers
---

Quoted 7 lines
xen/arch/arm/tee/ffa.c          | 28 +++++++++++++++---------
xen/arch/arm/tee/ffa_notif.c    |  7 ++++++
xen/arch/arm/tee/ffa_partinfo.c | 30 +++++++++++++++++++++++++-
xen/arch/arm/tee/ffa_private.h  | 38 ++++++++++++++++++++++++++++-----
xen/arch/arm/tee/ffa_rxtx.c     |  4 ++++
xen/arch/arm/tee/ffa_shm.c      | 12 +++++++++++
6 files changed, 103 insertions(+), 16 deletions(-)
Quoted 2 lines

Looks good.

Quoted 8 lines

I'm sorry, I'm having second thoughts about this patch. I have two concerns:
1. Xen will complain at boot with XENLOG_INFO if an ABI function listed in ffa_fw_abi_needed is missing. With the current list of ABI functions that's somewhat OK since it was a cause of disabling FF-A support before. But as the list grows it may become annoying or even confusing since when Xen supports more features it may complain more even if there is no regression compared to previous versions. If we need to print anything perhaps XENLOG_DEBUG is better.

This is only printed at boot and in the worst case it would list all needed ABI. If the list printed becomes big, it probably means that almost nothing is possible to do which might be interesting for the user.
Only seeing this information with debug prints might lead into normal users not understanding why communication with secure world are not working without having a reason.
I would expect that the most common case will be for the list of printed entries to be limited (right now it only prints something for 64bit sharing which should be solved in Hafnium).
As Xen is already quite verbose in INFO mode during boot and this is not a runtime print, I think it is ok.

Quoted 3 lines

2. FFA_FEATURES may return success for features not supported by the SPMC. How about only returning success for features in the ffa_fw_abi_needed bitmap?

This would be a reinterpretation of the specification and could create issues in some cases (some ABIs might be supported by Xen but not by the SPMC and still work correctly this way) and even more when we will have VM to VM.
The specification is saying that we should return what we support and not what is supported by the SPMC. Filtering based on what is supported by the SPMC and what will still work if not supported by the SPMC and what we do not support even if it is supported by the SPMC might become quickly very complex.

What do you think we would gain from doing what you suggest instead of what we have right now ?

Cheers
Bertrand

Quoted 298 lines

Cheers,
Jens

Quoted 294 lines

Cheers,
Jens

Quoted 290 lines
xen/arch/arm/tee/ffa.c +17 -10
index 1ee6b2895e92..267d4435ac08 100644
@@ -72,7 +72,10 @@
 #include "ffa_private.h"
 
 /* Negotiated FF-A version to use with the SPMC, 0 if not there or supported */
-static uint32_t __ro_after_init ffa_fw_version;
+uint32_t __ro_after_init ffa_fw_version;
+
+/* Features supported by the SPMC or secure world when present */
+DECLARE_BITMAP(ffa_fw_abi_supported, FFA_ABI_BITMAP_SIZE);
 
 struct ffa_fw_abi {
    const uint32_t id;
@@ -177,6 +180,13 @@ static void handle_msg_send_direct_req(struct cpu_user_regs *regs, uint32_t fid)
    else
        mask = GENMASK_ULL(31, 0);
 
+    if ( !ffa_fw_supports_fid(fid) )
+    {
+        resp.a0 = FFA_ERROR;
+        resp.a2 = FFA_RET_NOT_SUPPORTED;
+        goto out;
+    }
+
    src_dst = get_user_reg(regs, 1);
    if ( (src_dst >> 16) != ffa_get_vm_id(d) )
    {
@@ -577,19 +587,16 @@ static bool ffa_probe(void)
    else
        ffa_fw_version = vers;
 
-    /*
-     * At the moment domains must support the same features used by Xen.
-     * TODO: Rework the code to allow domain to use a subset of the
-     * features supported.
-     */
    for ( unsigned int i = 0; i < ARRAY_SIZE(ffa_fw_abi_needed); i++ )
    {
-        if ( !ffa_abi_supported(ffa_fw_abi_needed[i].id) )
-        {
+        ASSERT(FFA_ABI_BITNUM(ffa_fw_abi_needed[i].id) < FFA_ABI_BITMAP_SIZE);
+
+        if ( ffa_abi_supported(ffa_fw_abi_needed[i].id) )
+            set_bit(FFA_ABI_BITNUM(ffa_fw_abi_needed[i].id),
+                    ffa_fw_abi_supported);
+        else
            printk(XENLOG_INFO "ARM FF-A Firmware does not support %s\n",
                   ffa_fw_abi_needed[i].name);
-            goto err_no_fw;
-        }
    }
 
    if ( !ffa_rxtx_init() )
@@ -611,6 +618,7 @@ err_rxtx_destroy:
    ffa_rxtx_destroy();

err_no_fw:

    ffa_fw_version = 0;
xen/arch/arm/tee/ffa.c +1 -0
+    bitmap_zero(ffa_fw_abi_supported, FFA_ABI_BITMAP_SIZE);
    printk(XENLOG_WARNING "ARM FF-A No firmware support\n");
 
    return false;
xen/arch/arm/tee/ffa_notif.c +7 -0
index 541e61d2f606..4b3e46318f4b 100644
@@ -377,6 +377,13 @@ void ffa_notif_init(void)
    unsigned int irq;
    int ret;
 
+    /* Only enable fw notification if all ABIs we need are supported */
+    if ( !(ffa_fw_supports_fid(FFA_NOTIFICATION_BITMAP_CREATE) &&
+           ffa_fw_supports_fid(FFA_NOTIFICATION_BITMAP_DESTROY) &&
+           ffa_fw_supports_fid(FFA_NOTIFICATION_GET) &&
+           ffa_fw_supports_fid(FFA_NOTIFICATION_INFO_GET_64)) )
+        return;
+
    arm_smccc_1_2_smc(&arg, &resp);
    if ( resp.a0 != FFA_SUCCESS_32 )
        return;
xen/arch/arm/tee/ffa_partinfo.c +29 -1
index 93a03c6bc672..99c48f0e5c05 100644
@@ -77,7 +77,15 @@ int32_t ffa_handle_partition_info_get(uint32_t w1, uint32_t w2, uint32_t w3,
     */
    if ( w5 == FFA_PARTITION_INFO_GET_COUNT_FLAG &&
         ctx->guest_vers == FFA_VERSION_1_1 )
-        return ffa_partition_info_get(w1, w2, w3, w4, w5, count, fpi_size);
+    {
+        if ( ffa_fw_supports_fid(FFA_PARTITION_INFO_GET) )
+            return ffa_partition_info_get(w1, w2, w3, w4, w5, count, fpi_size);
+        else
+        {
+            *count = 0;
+            return FFA_RET_OK;
+        }
+    }
    if ( w5 )
        return FFA_RET_INVALID_PARAMETERS;
 
@@ -87,6 +95,18 @@ int32_t ffa_handle_partition_info_get(uint32_t w1, uint32_t w2, uint32_t w3,
    if ( !spin_trylock(&ctx->rx_lock) )
        return FFA_RET_BUSY;
 
+    if ( !ffa_fw_supports_fid(FFA_PARTITION_INFO_GET) )
+    {
+        if ( ctx->guest_vers == FFA_VERSION_1_0 )
+            *fpi_size = sizeof(struct ffa_partition_info_1_0);
+        else
+            *fpi_size = sizeof(struct ffa_partition_info_1_1);
+
+        *count = 0;
+        ret = FFA_RET_OK;
+        goto out;
+    }
+
    if ( !ctx->page_count || !ctx->rx_is_free )
        goto out;
    spin_lock(&ffa_rx_buffer_lock);
@@ -250,6 +270,11 @@ bool ffa_partinfo_init(void)
    uint32_t count;
    int e;
 
+    if ( !ffa_fw_supports_fid(FFA_PARTITION_INFO_GET) ||
+         !ffa_fw_supports_fid(FFA_MSG_SEND_DIRECT_REQ_32) ||
+         !ffa_rx || !ffa_tx )
+        return false;
+
    e = ffa_partition_info_get(0, 0, 0, 0, 0, &count, &fpi_size);
    if ( e )
    {
@@ -313,6 +338,9 @@ int ffa_partinfo_domain_init(struct domain *d)
    unsigned int n;
    int32_t res;
 
+    if ( !ffa_fw_supports_fid(FFA_MSG_SEND_DIRECT_REQ_32) )
+        return 0;
+
    ctx->vm_destroy_bitmap = xzalloc_array(unsigned long, count);
    if ( !ctx->vm_destroy_bitmap )
        return -ENOMEM;
xen/arch/arm/tee/ffa_private.h +33 -5
index 045d9c4a0b56..85eb61c13464 100644
@@ -14,6 +14,7 @@
 #include <xen/spinlock.h>
 #include <xen/sched.h>
 #include <xen/time.h>
+#include <xen/bitmap.h>
 
 /* Error codes */
 #define FFA_RET_OK                      0
@@ -201,18 +202,17 @@
 #define FFA_INTERRUPT                   0x84000062U
 #define FFA_VERSION                     0x84000063U
 #define FFA_FEATURES                    0x84000064U
-#define FFA_RX_ACQUIRE                  0x84000084U
 #define FFA_RX_RELEASE                  0x84000065U
 #define FFA_RXTX_MAP_32                 0x84000066U
 #define FFA_RXTX_MAP_64                 0xC4000066U
 #define FFA_RXTX_UNMAP                  0x84000067U
 #define FFA_PARTITION_INFO_GET          0x84000068U
 #define FFA_ID_GET                      0x84000069U
-#define FFA_SPM_ID_GET                  0x84000085U
+#define FFA_MSG_POLL                    0x8400006AU
 #define FFA_MSG_WAIT                    0x8400006BU
 #define FFA_MSG_YIELD                   0x8400006CU
 #define FFA_RUN                         0x8400006DU
-#define FFA_MSG_SEND2                   0x84000086U
+#define FFA_MSG_SEND                    0x8400006EU
 #define FFA_MSG_SEND_DIRECT_REQ_32      0x8400006FU
 #define FFA_MSG_SEND_DIRECT_REQ_64      0xC400006FU
 #define FFA_MSG_SEND_DIRECT_RESP_32     0x84000070U
@@ -230,8 +230,6 @@
 #define FFA_MEM_RECLAIM                 0x84000077U
 #define FFA_MEM_FRAG_RX                 0x8400007AU
 #define FFA_MEM_FRAG_TX                 0x8400007BU
-#define FFA_MSG_SEND                    0x8400006EU
-#define FFA_MSG_POLL                    0x8400006AU
 #define FFA_NOTIFICATION_BITMAP_CREATE  0x8400007DU
 #define FFA_NOTIFICATION_BITMAP_DESTROY 0x8400007EU
 #define FFA_NOTIFICATION_BIND           0x8400007FU
@@ -240,6 +238,25 @@
 #define FFA_NOTIFICATION_GET            0x84000082U
 #define FFA_NOTIFICATION_INFO_GET_32    0x84000083U
 #define FFA_NOTIFICATION_INFO_GET_64    0xC4000083U
+#define FFA_RX_ACQUIRE                  0x84000084U
+#define FFA_SPM_ID_GET                  0x84000085U
+#define FFA_MSG_SEND2                   0x84000086U
+
+/**
+ * Encoding of features supported or not by the fw in a bitmap:
+ * - Function IDs are going from 0x60 to 0xFF
+ * - A function can be supported in 32 and/or 64bit
+ * The bitmap has one bit for each function in 32 and 64 bit.
+ */
+#define FFA_ABI_ID(id)        ((id) & ARM_SMCCC_FUNC_MASK)
+#define FFA_ABI_CONV(id)      (((id) >> ARM_SMCCC_CONV_SHIFT) & BIT(0,U))
+
+#define FFA_ABI_MIN           FFA_ABI_ID(FFA_ERROR)
+#define FFA_ABI_MAX           FFA_ABI_ID(FFA_MSG_SEND2)
+
+#define FFA_ABI_BITMAP_SIZE   (2 * (FFA_ABI_MAX - FFA_ABI_MIN + 1))
+#define FFA_ABI_BITNUM(id)    ((FFA_ABI_ID(id) - FFA_ABI_MIN) << 1 | \
+                               FFA_ABI_CONV(id))
 
 struct ffa_ctx_notif {
    bool enabled;
@@ -289,6 +306,8 @@ extern void *ffa_rx;
 extern void *ffa_tx;
 extern spinlock_t ffa_rx_buffer_lock;
 extern spinlock_t ffa_tx_buffer_lock;
+extern uint32_t __ro_after_init ffa_fw_version;
+extern DECLARE_BITMAP(ffa_fw_abi_supported, FFA_ABI_BITMAP_SIZE);
 
 bool ffa_shm_domain_destroy(struct domain *d);
 void ffa_handle_mem_share(struct cpu_user_regs *regs);
@@ -401,4 +420,13 @@ static inline int32_t ffa_rx_release(void)
    return ffa_simple_call(FFA_RX_RELEASE, 0, 0, 0, 0);
 }
 
+static inline bool ffa_fw_supports_fid(uint32_t fid)
+{
+    BUILD_BUG_ON(FFA_ABI_MIN > FFA_ABI_MAX);
+
+    if ( FFA_ABI_BITNUM(fid) > FFA_ABI_BITMAP_SIZE)
+        return false;
+    return test_bit(FFA_ABI_BITNUM(fid), ffa_fw_abi_supported);
+}
+
 #endif /*__FFA_PRIVATE_H__*/
xen/arch/arm/tee/ffa_rxtx.c +4 -0
index 661764052e67..b6931c855779 100644
@@ -193,6 +193,10 @@ bool ffa_rxtx_init(void)
 {
    int e;
 
+    /* Firmware not there or not supporting */
+    if ( !ffa_fw_supports_fid(FFA_RXTX_MAP_64) )
+        return false;
+
    ffa_rx = alloc_xenheap_pages(get_order_from_pages(FFA_RXTX_PAGE_COUNT), 0);
    if ( !ffa_rx )
        return false;
xen/arch/arm/tee/ffa_shm.c +12 -1
index 370d83ec5cf8..efa5b67db8e1 100644
@@ -149,6 +149,9 @@ static int32_t ffa_mem_share(uint32_t tot_len, uint32_t frag_len,
 static int32_t ffa_mem_reclaim(uint32_t handle_lo, uint32_t handle_hi,
                               uint32_t flags)
 {
+    if ( !ffa_fw_supports_fid(FFA_MEM_RECLAIM) )
+        return FFA_RET_NOT_SUPPORTED;
+
    return ffa_simple_call(FFA_MEM_RECLAIM, handle_lo, handle_hi, flags, 0);
 }
 
@@ -467,6 +470,12 @@ void ffa_handle_mem_share(struct cpu_user_regs *regs)
    uint32_t range_count;
    uint32_t region_offs;
 
+    if ( !ffa_fw_supports_fid(FFA_MEM_SHARE_64) )
+    {
+        ret = FFA_RET_NOT_SUPPORTED;
+        goto out_set_ret;
+    }
+
    /*
     * We're only accepting memory transaction descriptors via the rx/tx
     * buffer.
@@ -621,6 +630,9 @@ int ffa_handle_mem_reclaim(uint64_t handle, uint32_t flags)
    register_t handle_lo;
    int ret;
 
+    if ( !ffa_fw_supports_fid(FFA_MEM_RECLAIM) )
+        return FFA_RET_NOT_SUPPORTED;
+
    spin_lock(&ctx->lock);
    shm = find_shm_mem(ctx, handle);
    if ( shm )
--

2.47.0

#32 lore raw

Re: [PATCH v2 10/10] xen/arm: ffa: Add indirect message support

In reply to Jens Wiklander

Hi Jens,

Quoted 106 lines · Jens Wiklander

Hi Bertrand,

Quoted 100 lines · Bertrand Marquis

Add support for FFA_MSG_SEND2 to send indirect messages from a VM to a secure partition.

---
Changes in v2:
- rebase
---

xen/arch/arm/tee/ffa.c         |  5 ++++
xen/arch/arm/tee/ffa_msg.c     | 49 ++++++++++++++++++++++++++++++++++
xen/arch/arm/tee/ffa_private.h |  1 +
3 files changed, 55 insertions(+)
xen/arch/arm/tee/ffa.c +5 -0
index 3a9525aa4598..21d41b452dc9 100644
@@ -101,6 +101,7 @@ static const struct ffa_fw_abi ffa_fw_abi_needed[] = {
    FW_ABI(FFA_MEM_RECLAIM),
    FW_ABI(FFA_MSG_SEND_DIRECT_REQ_32),
    FW_ABI(FFA_MSG_SEND_DIRECT_REQ_64),
+    FW_ABI(FFA_MSG_SEND2),
 };
 
 /*
@@ -195,6 +196,7 @@ static void handle_features(struct cpu_user_regs *regs)
    case FFA_PARTITION_INFO_GET:
    case FFA_MSG_SEND_DIRECT_REQ_32:
    case FFA_MSG_SEND_DIRECT_REQ_64:
+    case FFA_MSG_SEND2:
        ffa_set_regs_success(regs, 0, 0);
        break;
    case FFA_MEM_SHARE_64:
@@ -275,6 +277,9 @@ static bool ffa_handle_call(struct cpu_user_regs *regs)
    case FFA_MSG_SEND_DIRECT_REQ_64:
        ffa_handle_msg_send_direct_req(regs, fid);
        return true;
+    case FFA_MSG_SEND2:
+        e = ffa_handle_msg_send2(regs);
+        break;
    case FFA_MEM_SHARE_32:
    case FFA_MEM_SHARE_64:
        ffa_handle_mem_share(regs);
xen/arch/arm/tee/ffa_msg.c +41 -0
index ae263e54890e..335f246ba657 100644
@@ -12,6 +12,15 @@
 
 #include "ffa_private.h"
 
+/* Encoding of partition message in RX/TX buffer */
+struct ffa_part_msg_rxtx {
+    uint32_t flags;
+    uint32_t reserved;
+    uint32_t msg_offset;
+    uint32_t send_recv_id;
+    uint32_t msg_size;
+};
+
 void ffa_handle_msg_send_direct_req(struct cpu_user_regs *regs, uint32_t fid)
 {
    struct arm_smccc_1_2_regs arg = { .a0 = fid, };
@@ -78,3 +87,43 @@ out:
                 resp.a4 & mask, resp.a5 & mask, resp.a6 & mask,
                 resp.a7 & mask);
 }
+
+int32_t ffa_handle_msg_send2(struct cpu_user_regs *regs)
+{
+    struct domain *src_d = current->domain;
+    struct ffa_ctx *src_ctx = src_d->arch.tee;
+    const struct ffa_part_msg_rxtx *src_msg;
+    uint16_t dst_id, src_id;
+    int32_t ret;
+
+    if ( !ffa_fw_supports_fid(FFA_MSG_SEND2) )
+        return FFA_RET_NOT_SUPPORTED;
+
+    if ( !spin_trylock(&src_ctx->tx_lock) )
+        return FFA_RET_BUSY;
+
+    src_msg = src_ctx->tx;
+    src_id = src_msg->send_recv_id >> 16;
+    dst_id = src_msg->send_recv_id & GENMASK(15,0);
+
+    if ( src_id != ffa_get_vm_id(src_d) || !FFA_ID_IS_SECURE(dst_id) )
+    {
+        ret = FFA_RET_INVALID_PARAMETERS;
+        goto out_unlock_tx;
+    }
+
+    /* check source message fits in buffer */
+    if ( src_ctx->page_count * FFA_PAGE_SIZE <
+         src_msg->msg_offset + src_msg->msg_size ||
+         src_msg->msg_offset < sizeof(struct ffa_part_msg_rxtx) )
+    {
+        ret = FFA_RET_INVALID_PARAMETERS;
+        goto out_unlock_tx;
Quoted 8 lines

xen/arch/arm/tee/ffa_msg.c out: · line 122

Quoted 1 line
xen/arch/arm/tee/ffa_msg.c +1 -0
+    }

The guest can change src_mst at any moment with another CPU so these tests are only sanity checks. The SPMC will also have to lock and do the same tests again. So the tests here will only in the best case (in case the guest is misbehaving) save us from entering the SPMC only to get an error back. The lock makes sense since we could have concurrent calls to FFA_MEM_SHARE. How about removing the tests?

I think we should still prevent to forward invalid requests to the SPMC as much as we can to prevent a malicious guest from stilling CPU cycles by doing invalid calls to the secure world.

I could put a comment in there saying that this is just protection but to be fare the SPMC in secure will have the same issues: this can be changed at any time by the caller on another core.

Quoted 5 lines

xen/arch/arm/tee/ffa_msg.c out: · line 124

Quoted 3 lines
xen/arch/arm/tee/ffa_msg.c +2 -0
+
+    ret = ffa_simple_call(FFA_MSG_SEND2, ((uint32_t)src_id) << 16, 0, 0, 0);

I'd rather use ffa_get_vm_id(src_d) instead of src_id.

src_id is a local variable and was checked to be equal to ffa_get_vm_id(src_d) upper so those 2 values are the same.
Why would you rather recall ffa_get_vm_id here ?

Cheers
Bertrand

Quoted 23 lines

Cheers,
Jens

Quoted 19 lines
xen/arch/arm/tee/ffa_msg.c +5 -0
+
+out_unlock_tx:
+    spin_unlock(&src_ctx->tx_lock);
+    return ret;
+}
xen/arch/arm/tee/ffa_private.h +1 -1
index 973ee55be09b..d441c0ca5598 100644
@@ -359,6 +359,7 @@ void ffa_handle_notification_get(struct cpu_user_regs *regs);
 int ffa_handle_notification_set(struct cpu_user_regs *regs);
 
 void ffa_handle_msg_send_direct_req(struct cpu_user_regs *regs, uint32_t fid);
+int32_t ffa_handle_msg_send2(struct cpu_user_regs *regs);
 
 static inline uint16_t ffa_get_vm_id(const struct domain *d)
 {
--

2.47.0

#14 lore raw

Re: [PATCH v2 04/10] xen/arm: ffa: Fine granular call support

In reply to Bertrand Marquis

Hi Bertrand,

Quoted 57 lines · Bertrand Marquis

Hi Jens,

Quoted 45 lines · Jens Wiklander
Quoted 38 lines · Jens Wiklander

Hi Bertrand,

Quoted 25 lines · Bertrand Marquis

Create a bitmap to store which feature is supported or not by the firmware and use it to filter which calls are done to the firmware.

While there reoder ABI definition by numbers to easily find the min and max ones.

---
Changes in v2:
- rename fw_feat to abi and macros to FFA_ABI to be coherent with the abi needed change done before
- rework the macros to be simpler by directly defining MIN and MAX using only Function ids
- check that requested function ids do not go over the bitmap size in ffa_fw_supports_fid
- add an ASSERT to make sure that we do not try to set bits outside of the bitmap
- turn off FF-A if there is not firmware support and adapt the commit message to reflect this
- add a compile time check that FFA_ABI_MIN < FFA_ABI_MAX
- remove spurious line removal
- restore proper cleanup of rxtx init in case of error
- reorder ABI by numbers
---

Quoted 7 lines
xen/arch/arm/tee/ffa.c          | 28 +++++++++++++++---------
xen/arch/arm/tee/ffa_notif.c    |  7 ++++++
xen/arch/arm/tee/ffa_partinfo.c | 30 +++++++++++++++++++++++++-
xen/arch/arm/tee/ffa_private.h  | 38 ++++++++++++++++++++++++++++-----
xen/arch/arm/tee/ffa_rxtx.c     |  4 ++++
xen/arch/arm/tee/ffa_shm.c      | 12 +++++++++++
6 files changed, 103 insertions(+), 16 deletions(-)
Quoted 2 lines

Looks good.

Quoted 8 lines

I'm sorry, I'm having second thoughts about this patch. I have two concerns:
1. Xen will complain at boot with XENLOG_INFO if an ABI function listed in ffa_fw_abi_needed is missing. With the current list of ABI functions that's somewhat OK since it was a cause of disabling FF-A support before. But as the list grows it may become annoying or even confusing since when Xen supports more features it may complain more even if there is no regression compared to previous versions. If we need to print anything perhaps XENLOG_DEBUG is better.

Quoted 11 lines

This is only printed at boot and in the worst case it would list all needed ABI. If the list printed becomes big, it probably means that almost nothing is possible to do which might be interesting for the user.
Only seeing this information with debug prints might lead into normal users not understanding why communication with secure world are not working without having a reason.
I would expect that the most common case will be for the list of printed entries to be limited (right now it only prints something for 64bit sharing which should be solved in Hafnium).
As Xen is already quite verbose in INFO mode during boot and this is not a runtime print, I think it is ok.

With added support for FFA_MSG_SEND2 xen will start to complain that OP-TEE doesn't support that function, even if it's not needed. It should be harmless as long as it's not interpreted as an error.

Quoted 15 lines
Quoted 4 lines

2. FFA_FEATURES may return success for features not supported by the SPMC. How about only returning success for features in the ffa_fw_abi_needed bitmap?

This would be a reinterpretation of the specification and could create issues in some cases (some ABIs might be supported by Xen but not by the SPMC and still work correctly this way) and even more when we will have VM to VM.
The specification is saying that we should return what we support and not what is supported by the SPMC. Filtering based on what is supported by the SPMC and what will still work if not supported by the SPMC and what we do not support even if it is supported by the SPMC might become quickly very complex.

Quoted 2 lines

What do you think we would gain from doing what you suggest instead of what we have right now ?

Yes, you're right I mistook FFA_FEATURE to cover the Framework, but it's only the interface. So returning success for all functions xen might be able to support is within specification.

Cheers,
Jens

Quoted 304 lines

Cheers
Bertrand

Quoted 298 lines

Cheers,
Jens

Quoted 294 lines

Cheers,
Jens

Quoted 290 lines
xen/arch/arm/tee/ffa.c +17 -10
index 1ee6b2895e92..267d4435ac08 100644
@@ -72,7 +72,10 @@
 #include "ffa_private.h"
 
 /* Negotiated FF-A version to use with the SPMC, 0 if not there or supported */
-static uint32_t __ro_after_init ffa_fw_version;
+uint32_t __ro_after_init ffa_fw_version;
+
+/* Features supported by the SPMC or secure world when present */
+DECLARE_BITMAP(ffa_fw_abi_supported, FFA_ABI_BITMAP_SIZE);
 
 struct ffa_fw_abi {
    const uint32_t id;
@@ -177,6 +180,13 @@ static void handle_msg_send_direct_req(struct cpu_user_regs *regs, uint32_t fid)
    else
        mask = GENMASK_ULL(31, 0);
 
+    if ( !ffa_fw_supports_fid(fid) )
+    {
+        resp.a0 = FFA_ERROR;
+        resp.a2 = FFA_RET_NOT_SUPPORTED;
+        goto out;
+    }
+
    src_dst = get_user_reg(regs, 1);
    if ( (src_dst >> 16) != ffa_get_vm_id(d) )
    {
@@ -577,19 +587,16 @@ static bool ffa_probe(void)
    else
        ffa_fw_version = vers;
 
-    /*
-     * At the moment domains must support the same features used by Xen.
-     * TODO: Rework the code to allow domain to use a subset of the
-     * features supported.
-     */
    for ( unsigned int i = 0; i < ARRAY_SIZE(ffa_fw_abi_needed); i++ )
    {
-        if ( !ffa_abi_supported(ffa_fw_abi_needed[i].id) )
-        {
+        ASSERT(FFA_ABI_BITNUM(ffa_fw_abi_needed[i].id) < FFA_ABI_BITMAP_SIZE);
+
+        if ( ffa_abi_supported(ffa_fw_abi_needed[i].id) )
+            set_bit(FFA_ABI_BITNUM(ffa_fw_abi_needed[i].id),
+                    ffa_fw_abi_supported);
+        else
            printk(XENLOG_INFO "ARM FF-A Firmware does not support %s\n",
                   ffa_fw_abi_needed[i].name);
-            goto err_no_fw;
-        }
    }
 
    if ( !ffa_rxtx_init() )
@@ -611,6 +618,7 @@ err_rxtx_destroy:
    ffa_rxtx_destroy();

err_no_fw:

    ffa_fw_version = 0;
xen/arch/arm/tee/ffa.c +1 -0
+    bitmap_zero(ffa_fw_abi_supported, FFA_ABI_BITMAP_SIZE);
    printk(XENLOG_WARNING "ARM FF-A No firmware support\n");
 
    return false;
xen/arch/arm/tee/ffa_notif.c +7 -0
index 541e61d2f606..4b3e46318f4b 100644
@@ -377,6 +377,13 @@ void ffa_notif_init(void)
    unsigned int irq;
    int ret;
 
+    /* Only enable fw notification if all ABIs we need are supported */
+    if ( !(ffa_fw_supports_fid(FFA_NOTIFICATION_BITMAP_CREATE) &&
+           ffa_fw_supports_fid(FFA_NOTIFICATION_BITMAP_DESTROY) &&
+           ffa_fw_supports_fid(FFA_NOTIFICATION_GET) &&
+           ffa_fw_supports_fid(FFA_NOTIFICATION_INFO_GET_64)) )
+        return;
+
    arm_smccc_1_2_smc(&arg, &resp);
    if ( resp.a0 != FFA_SUCCESS_32 )
        return;
xen/arch/arm/tee/ffa_partinfo.c +29 -1
index 93a03c6bc672..99c48f0e5c05 100644
@@ -77,7 +77,15 @@ int32_t ffa_handle_partition_info_get(uint32_t w1, uint32_t w2, uint32_t w3,
     */
    if ( w5 == FFA_PARTITION_INFO_GET_COUNT_FLAG &&
         ctx->guest_vers == FFA_VERSION_1_1 )
-        return ffa_partition_info_get(w1, w2, w3, w4, w5, count, fpi_size);
+    {
+        if ( ffa_fw_supports_fid(FFA_PARTITION_INFO_GET) )
+            return ffa_partition_info_get(w1, w2, w3, w4, w5, count, fpi_size);
+        else
+        {
+            *count = 0;
+            return FFA_RET_OK;
+        }
+    }
    if ( w5 )
        return FFA_RET_INVALID_PARAMETERS;
 
@@ -87,6 +95,18 @@ int32_t ffa_handle_partition_info_get(uint32_t w1, uint32_t w2, uint32_t w3,
    if ( !spin_trylock(&ctx->rx_lock) )
        return FFA_RET_BUSY;
 
+    if ( !ffa_fw_supports_fid(FFA_PARTITION_INFO_GET) )
+    {
+        if ( ctx->guest_vers == FFA_VERSION_1_0 )
+            *fpi_size = sizeof(struct ffa_partition_info_1_0);
+        else
+            *fpi_size = sizeof(struct ffa_partition_info_1_1);
+
+        *count = 0;
+        ret = FFA_RET_OK;
+        goto out;
+    }
+
    if ( !ctx->page_count || !ctx->rx_is_free )
        goto out;
    spin_lock(&ffa_rx_buffer_lock);
@@ -250,6 +270,11 @@ bool ffa_partinfo_init(void)
    uint32_t count;
    int e;
 
+    if ( !ffa_fw_supports_fid(FFA_PARTITION_INFO_GET) ||
+         !ffa_fw_supports_fid(FFA_MSG_SEND_DIRECT_REQ_32) ||
+         !ffa_rx || !ffa_tx )
+        return false;
+
    e = ffa_partition_info_get(0, 0, 0, 0, 0, &count, &fpi_size);
    if ( e )
    {
@@ -313,6 +338,9 @@ int ffa_partinfo_domain_init(struct domain *d)
    unsigned int n;
    int32_t res;
 
+    if ( !ffa_fw_supports_fid(FFA_MSG_SEND_DIRECT_REQ_32) )
+        return 0;
+
    ctx->vm_destroy_bitmap = xzalloc_array(unsigned long, count);
    if ( !ctx->vm_destroy_bitmap )
        return -ENOMEM;
xen/arch/arm/tee/ffa_private.h +33 -5
index 045d9c4a0b56..85eb61c13464 100644
@@ -14,6 +14,7 @@
 #include <xen/spinlock.h>
 #include <xen/sched.h>
 #include <xen/time.h>
+#include <xen/bitmap.h>
 
 /* Error codes */
 #define FFA_RET_OK                      0
@@ -201,18 +202,17 @@
 #define FFA_INTERRUPT                   0x84000062U
 #define FFA_VERSION                     0x84000063U
 #define FFA_FEATURES                    0x84000064U
-#define FFA_RX_ACQUIRE                  0x84000084U
 #define FFA_RX_RELEASE                  0x84000065U
 #define FFA_RXTX_MAP_32                 0x84000066U
 #define FFA_RXTX_MAP_64                 0xC4000066U
 #define FFA_RXTX_UNMAP                  0x84000067U
 #define FFA_PARTITION_INFO_GET          0x84000068U
 #define FFA_ID_GET                      0x84000069U
-#define FFA_SPM_ID_GET                  0x84000085U
+#define FFA_MSG_POLL                    0x8400006AU
 #define FFA_MSG_WAIT                    0x8400006BU
 #define FFA_MSG_YIELD                   0x8400006CU
 #define FFA_RUN                         0x8400006DU
-#define FFA_MSG_SEND2                   0x84000086U
+#define FFA_MSG_SEND                    0x8400006EU
 #define FFA_MSG_SEND_DIRECT_REQ_32      0x8400006FU
 #define FFA_MSG_SEND_DIRECT_REQ_64      0xC400006FU
 #define FFA_MSG_SEND_DIRECT_RESP_32     0x84000070U
@@ -230,8 +230,6 @@
 #define FFA_MEM_RECLAIM                 0x84000077U
 #define FFA_MEM_FRAG_RX                 0x8400007AU
 #define FFA_MEM_FRAG_TX                 0x8400007BU
-#define FFA_MSG_SEND                    0x8400006EU
-#define FFA_MSG_POLL                    0x8400006AU
 #define FFA_NOTIFICATION_BITMAP_CREATE  0x8400007DU
 #define FFA_NOTIFICATION_BITMAP_DESTROY 0x8400007EU
 #define FFA_NOTIFICATION_BIND           0x8400007FU
@@ -240,6 +238,25 @@
 #define FFA_NOTIFICATION_GET            0x84000082U
 #define FFA_NOTIFICATION_INFO_GET_32    0x84000083U
 #define FFA_NOTIFICATION_INFO_GET_64    0xC4000083U
+#define FFA_RX_ACQUIRE                  0x84000084U
+#define FFA_SPM_ID_GET                  0x84000085U
+#define FFA_MSG_SEND2                   0x84000086U
+
+/**
+ * Encoding of features supported or not by the fw in a bitmap:
+ * - Function IDs are going from 0x60 to 0xFF
+ * - A function can be supported in 32 and/or 64bit
+ * The bitmap has one bit for each function in 32 and 64 bit.
+ */
+#define FFA_ABI_ID(id)        ((id) & ARM_SMCCC_FUNC_MASK)
+#define FFA_ABI_CONV(id)      (((id) >> ARM_SMCCC_CONV_SHIFT) & BIT(0,U))
+
+#define FFA_ABI_MIN           FFA_ABI_ID(FFA_ERROR)
+#define FFA_ABI_MAX           FFA_ABI_ID(FFA_MSG_SEND2)
+
+#define FFA_ABI_BITMAP_SIZE   (2 * (FFA_ABI_MAX - FFA_ABI_MIN + 1))
+#define FFA_ABI_BITNUM(id)    ((FFA_ABI_ID(id) - FFA_ABI_MIN) << 1 | \
+                               FFA_ABI_CONV(id))
 
 struct ffa_ctx_notif {
    bool enabled;
@@ -289,6 +306,8 @@ extern void *ffa_rx;
 extern void *ffa_tx;
 extern spinlock_t ffa_rx_buffer_lock;
 extern spinlock_t ffa_tx_buffer_lock;
+extern uint32_t __ro_after_init ffa_fw_version;
+extern DECLARE_BITMAP(ffa_fw_abi_supported, FFA_ABI_BITMAP_SIZE);
 
 bool ffa_shm_domain_destroy(struct domain *d);
 void ffa_handle_mem_share(struct cpu_user_regs *regs);
@@ -401,4 +420,13 @@ static inline int32_t ffa_rx_release(void)
    return ffa_simple_call(FFA_RX_RELEASE, 0, 0, 0, 0);
 }
 
+static inline bool ffa_fw_supports_fid(uint32_t fid)
+{
+    BUILD_BUG_ON(FFA_ABI_MIN > FFA_ABI_MAX);
+
+    if ( FFA_ABI_BITNUM(fid) > FFA_ABI_BITMAP_SIZE)
+        return false;
+    return test_bit(FFA_ABI_BITNUM(fid), ffa_fw_abi_supported);
+}
+
 #endif /*__FFA_PRIVATE_H__*/
xen/arch/arm/tee/ffa_rxtx.c +4 -0
index 661764052e67..b6931c855779 100644
@@ -193,6 +193,10 @@ bool ffa_rxtx_init(void)
 {
    int e;
 
+    /* Firmware not there or not supporting */
+    if ( !ffa_fw_supports_fid(FFA_RXTX_MAP_64) )
+        return false;
+
    ffa_rx = alloc_xenheap_pages(get_order_from_pages(FFA_RXTX_PAGE_COUNT), 0);
    if ( !ffa_rx )
        return false;
xen/arch/arm/tee/ffa_shm.c +12 -1
index 370d83ec5cf8..efa5b67db8e1 100644
@@ -149,6 +149,9 @@ static int32_t ffa_mem_share(uint32_t tot_len, uint32_t frag_len,
 static int32_t ffa_mem_reclaim(uint32_t handle_lo, uint32_t handle_hi,
                               uint32_t flags)
 {
+    if ( !ffa_fw_supports_fid(FFA_MEM_RECLAIM) )
+        return FFA_RET_NOT_SUPPORTED;
+
    return ffa_simple_call(FFA_MEM_RECLAIM, handle_lo, handle_hi, flags, 0);
 }
 
@@ -467,6 +470,12 @@ void ffa_handle_mem_share(struct cpu_user_regs *regs)
    uint32_t range_count;
    uint32_t region_offs;
 
+    if ( !ffa_fw_supports_fid(FFA_MEM_SHARE_64) )
+    {
+        ret = FFA_RET_NOT_SUPPORTED;
+        goto out_set_ret;
+    }
+
    /*
     * We're only accepting memory transaction descriptors via the rx/tx
     * buffer.
@@ -621,6 +630,9 @@ int ffa_handle_mem_reclaim(uint64_t handle, uint32_t flags)
    register_t handle_lo;
    int ret;
 
+    if ( !ffa_fw_supports_fid(FFA_MEM_RECLAIM) )
+        return FFA_RET_NOT_SUPPORTED;
+
    spin_lock(&ctx->lock);
    shm = find_shm_mem(ctx, handle);
    if ( shm )
--

2.47.0

#23 lore raw

Re: [PATCH v2 07/10] xen/arm: ffa: Transmit RXTX buffers to the SPMC

In reply to Bertrand Marquis

Hi Bertrand,

Quoted 113 lines · Bertrand Marquis

Hi Jens,

Quoted 94 lines · Jens Wiklander

Hi Bertrand,

Quoted 88 lines · Bertrand Marquis

When an RXTX buffer is mapped by a VM transmit it to the SPMC when it supports RX_ACQUIRE.
As a consequence of that, we must acquire the RX buffer of a VM from the SPMC when we want to use it:
- create a generic acquire and release function to get the rx buffer of a VM which gets it from the SPMC when supported
- rename the rx_acquire to hyp_rx_acquire to remove confusion
- rework the rx_lock to only lock access to rx_is_free and only allow usage of the rx buffer to one who managed to acquire it, thus removing the trylock and returning busy if rx_is_free is false

As part of this change move some structure definition to ffa_private from ffa_shm as those are need for the MAP call with the SPMC.

---
Changes in v2:
- unmap VM rxtx buffer in SPMC on unmap call or on VM destroy
- rework the unmap call to the SPMC to properly pass the VM ID
---

xen/arch/arm/tee/ffa.c          |   2 +-
xen/arch/arm/tee/ffa_partinfo.c |  29 ++----
xen/arch/arm/tee/ffa_private.h  |  22 ++++-
xen/arch/arm/tee/ffa_rxtx.c     | 158 ++++++++++++++++++++++++++------
xen/arch/arm/tee/ffa_shm.c      |  15 ---
5 files changed, 161 insertions(+), 65 deletions(-)
xen/arch/arm/tee/ffa.c +1 -1
index a292003ca9fe..40ea5398fa21 100644
@@ -347,7 +347,7 @@ static bool ffa_handle_call(struct cpu_user_regs *regs)
        ffa_handle_partition_info_get(regs);
        return true;
    case FFA_RX_RELEASE:
-        e = ffa_handle_rx_release();
+        e = ffa_rx_release(d);
        break;
    case FFA_MSG_SEND_DIRECT_REQ_32:
    case FFA_MSG_SEND_DIRECT_REQ_64:
xen/arch/arm/tee/ffa_partinfo.c +4 -12
index 3cf801523296..fde187dba4e5 100644
@@ -121,11 +121,9 @@ void ffa_handle_partition_info_get(struct cpu_user_regs *regs)
        goto out;
    }
 
-    if ( !spin_trylock(&ctx->rx_lock) )
-    {
-        ret = FFA_RET_BUSY;
+    ret = ffa_rx_acquire(d);
+    if ( ret != FFA_RET_OK )
        goto out;
-    }
 
    dst_buf = ctx->rx;
 
@@ -135,22 +133,16 @@ void ffa_handle_partition_info_get(struct cpu_user_regs *regs)
        goto out_rx_release;
    }
 
-    if ( !ctx->page_count || !ctx->rx_is_free )
-    {
-        ret = FFA_RET_DENIED;
-        goto out_rx_release;
-    }
-
    spin_lock(&ffa_rx_buffer_lock);
 
    ret = ffa_partition_info_get(uuid, 0, &ffa_sp_count, &src_size);
 
    if ( ret )
-        goto out_rx_buf_unlock;
+        goto out_rx_hyp_unlock;
 
    /*
     * ffa_partition_info_get() succeeded so we now own the RX buffer we
-     * share with the SPMC. We must give it back using ffa_rx_release()
+     * share with the SPMC. We must give it back using ffa_hyp_rx_release()
     * once we've copied the content.
     */
 
@@ -193,15 +185,13 @@ void ffa_handle_partition_info_get(struct cpu_user_regs *regs)
        }
    }
 
Quoted 15 lines

xen/arch/arm/tee/ffa_partinfo.c void ffa_handle_partition_info_get(struct cpu_user_regs *regs) · line 194

Quoted 13 lines
xen/arch/arm/tee/ffa_partinfo.c +0 -2
-    ctx->rx_is_free = false;
-

out_rx_hyp_release:

xen/arch/arm/tee/ffa_partinfo.c +2 -2
-    ffa_rx_release();
-out_rx_buf_unlock:
+    ffa_hyp_rx_release();
+out_rx_hyp_unlock:
    spin_unlock(&ffa_rx_buffer_lock);

out_rx_release:

xen/arch/arm/tee/ffa_partinfo.c +2 -2
-    spin_unlock(&ctx->rx_lock);
-
+    if ( ret != FFA_RET_OK )
+        ffa_rx_release(d);

Please comment on why ffa_rx_release() must only be called on failure.

Quoted 2 lines

It is because the buffer contains data for the caller in case of success so it must be released by the caller.

Please mention the transfer of buffer ownership since that might be easy to miss if only skimming the spec.

Quoted 3 lines

Do i get it right that you want me to add a comment saying that in the code and not only tell you here ?

Yes. :-)

Cheers,
Jens

Quoted 360 lines
Quoted 105 lines
Quoted 105 lines

out:
if ( ret )
ffa_set_regs_error(regs, ret);

xen/arch/arm/tee/ffa_partinfo.c +0 -0
@@ -368,8 +358,7 @@ bool ffa_partinfo_init(void)
    ret = init_subscribers(count, fpi_size);
 

out:

xen/arch/arm/tee/ffa_partinfo.c +1 -2
-    ffa_rx_release();
-
+    ffa_hyp_rx_release();
    return ret;

}

xen/arch/arm/tee/ffa_private.h +19 -3
index afe69b43dbef..9adfe687c3c9 100644
@@ -265,6 +265,21 @@
 #define FFA_ABI_BITNUM(id)    ((FFA_ABI_ID(id) - FFA_ABI_MIN) << 1 | \
                               FFA_ABI_CONV(id))
 
+/* Constituent memory region descriptor */
+struct ffa_address_range {
+    uint64_t address;
+    uint32_t page_count;
+    uint32_t reserved;
+};
+
+/* Composite memory region descriptor */
+struct ffa_mem_region {
+    uint32_t total_page_count;
+    uint32_t address_range_count;
+    uint64_t reserved;
+    struct ffa_address_range address_range_array[];
+};
+
 struct ffa_ctx_notif {
    bool enabled;
 
@@ -292,7 +307,7 @@ struct ffa_ctx {
    struct ffa_ctx_notif notif;
    /*
     * tx_lock is used to serialize access to tx
-     * rx_lock is used to serialize access to rx
+     * rx_lock is used to serialize access to rx_is_free
     * lock is used for the rest in this struct
     */
    spinlock_t tx_lock;
@@ -331,7 +346,8 @@ void ffa_rxtx_domain_destroy(struct domain *d);
 uint32_t ffa_handle_rxtx_map(uint32_t fid, register_t tx_addr,
                            register_t rx_addr, uint32_t page_count);
 uint32_t ffa_handle_rxtx_unmap(void);
-int32_t ffa_handle_rx_release(void);
+int32_t ffa_rx_acquire(struct domain *d);
+int32_t ffa_rx_release(struct domain *d);
 
 void ffa_notif_init(void);
 void ffa_notif_init_interrupt(void);
@@ -420,7 +436,7 @@ static inline int32_t ffa_simple_call(uint32_t fid, register_t a1,
    return ffa_get_ret_code(&resp);
 }
 
-static inline int32_t ffa_rx_release(void)
+static inline int32_t ffa_hyp_rx_release(void)
 {
    return ffa_simple_call(FFA_RX_RELEASE, 0, 0, 0, 0);
 }
xen/arch/arm/tee/ffa_rxtx.c +14 -0
index b6931c855779..a5d43e51f843 100644
@@ -30,6 +30,17 @@ struct ffa_endpoint_rxtx_descriptor_1_1 {
    uint32_t tx_region_offs;
 };
 
+static int32_t ffa_rxtx_map(paddr_t tx_addr, paddr_t rx_addr,
+                            uint32_t page_count)
+{
+    return ffa_simple_call(FFA_RXTX_MAP_64, tx_addr, rx_addr, page_count, 0);
+}
+
+static int32_t ffa_rxtx_unmap(uint16_t id)
+{
+    return ffa_simple_call(FFA_RXTX_UNMAP, ((uint64_t)id)<<16, 0, 0, 0);
+}
+
 uint32_t ffa_handle_rxtx_map(uint32_t fid, register_t tx_addr,
                            register_t rx_addr, uint32_t page_count)
 {
@@ -42,6 +53,9 @@ uint32_t ffa_handle_rxtx_map(uint32_t fid, register_t tx_addr,
    void *rx;
    void *tx;
 
+    /* The code is considering that we only get one page for now */
+    BUILD_BUG_ON(FFA_MAX_RXTX_PAGE_COUNT != 1);
+
    if ( !smccc_is_conv_64(fid) )
    {
        /*
@@ -87,6 +101,65 @@ uint32_t ffa_handle_rxtx_map(uint32_t fid, register_t tx_addr,
    if ( !rx )
        goto err_unmap_tx;
 
Quoted 15 lines

xen/arch/arm/tee/ffa_rxtx.c uint32_t ffa_handle_rxtx_map(uint32_t fid, register_t tx_addr, · line 116

Quoted 13 lines
xen/arch/arm/tee/ffa_rxtx.c +13 -0
+    /*
+     * Transmit the RX/TX buffer information to the SPM if acquire is supported
+     * as the spec says that if not there is not need to acquire/release/map
+     * rxtx buffers from the SPMC
+     */
+    if ( ffa_fw_supports_fid(FFA_RX_ACQUIRE) )
+    {
+        struct ffa_endpoint_rxtx_descriptor_1_1 *rxtx_desc;
+        struct ffa_mem_region *mem_reg;
+
+        /* All must fit in our TX buffer */
+        BUILD_BUG_ON((sizeof(*rxtx_desc) + sizeof(*mem_reg)*2 +
+                      sizeof(struct ffa_address_range)*2) > FFA_PAGE_SIZE);

Please add space before and after the binary operator "*".

Ack

Quoted 1 line

The size of the TX buffer is FFA_RXTX_PAGE_COUNT * FFA_PAGE_SIZE.

Very right, I will fix that in v3.

Quoted 1 line

Nit: the outer parenthesis around the left expression aren't needed.

Ack

Quoted 34 lines
Quoted 34 lines
xen/arch/arm/tee/ffa_rxtx.c +33 -0
+
+        spin_lock(&ffa_tx_buffer_lock);
+        rxtx_desc = ffa_tx;
+
+        /*
+         * We have only one page for each so we pack everything:
+         * - rx region descriptor
+         * - rx region range
+         * - tx region descriptor
+         * - tx region range
+         */
+        rxtx_desc->sender_id = ffa_get_vm_id(d);
+        rxtx_desc->reserved = 0;
+        rxtx_desc->rx_region_offs = sizeof(*rxtx_desc);
+        rxtx_desc->tx_region_offs = sizeof(*rxtx_desc) +
+                                    offsetof(struct ffa_mem_region,
+                                             address_range_array[1]);
+
+        /* rx buffer */
+        mem_reg = ffa_tx + sizeof(*rxtx_desc);
+        mem_reg->total_page_count = 1;
+        mem_reg->address_range_count = 1;
+        mem_reg->reserved = 0;
+
+        mem_reg->address_range_array[0].address = page_to_maddr(rx_pg);
+        mem_reg->address_range_array[0].page_count = 1;
+        mem_reg->address_range_array[0].reserved = 0;
+
+        /* tx buffer */
+        mem_reg = ffa_tx + rxtx_desc->tx_region_offs;
+        mem_reg->total_page_count = 1;
+        mem_reg->address_range_count = 1;
+        mem_reg->reserved = 0;
Quoted 8 lines

xen/arch/arm/tee/ffa_rxtx.c uint32_t ffa_handle_rxtx_map(uint32_t fid, register_t tx_addr, · line 155

Quoted 6 lines
xen/arch/arm/tee/ffa_rxtx.c +6 -0
+
+        mem_reg->address_range_array[0].address = page_to_maddr(tx_pg);
+        mem_reg->address_range_array[0].page_count = 1;
+        mem_reg->address_range_array[0].reserved = 0;
+
+        ret = ffa_rxtx_map(0, 0, 1);

The last parameter is also MBZ when forwarding on behalf of an endpoint.

Ack, will fix in v3.

Cheers
Bertrand

Quoted 182 lines

Cheers,
Jens

Quoted 178 lines
xen/arch/arm/tee/ffa_rxtx.c +9 -0
+
+        spin_unlock(&ffa_tx_buffer_lock);
+
+        if ( ret != FFA_RET_OK )
+            goto err_unmap_rx;
+    }
+
    ctx->rx = rx;
    ctx->tx = tx;
    ctx->rx_pg = rx_pg;
@@ -95,6 +168,8 @@ uint32_t ffa_handle_rxtx_map(uint32_t fid, register_t tx_addr,
    ctx->rx_is_free = true;
    return FFA_RET_OK;
 
+err_unmap_rx:
+    unmap_domain_page_global(rx);

err_unmap_tx:

    unmap_domain_page_global(tx);

err_put_rx_pg:

xen/arch/arm/tee/ffa_rxtx.c +55 -10
@@ -105,8 +180,22 @@ err_put_tx_pg:
    return ret;
 }
 
-static void rxtx_unmap(struct ffa_ctx *ctx)
+static uint32_t  rxtx_unmap(struct domain *d)
 {
+    struct ffa_ctx *ctx = d->arch.tee;
+
+    if ( !ctx->page_count )
+        return FFA_RET_INVALID_PARAMETERS;
+
+    if ( ffa_fw_supports_fid(FFA_RX_ACQUIRE) )
+    {
+        uint32_t ret;
+
+        ret = ffa_rxtx_unmap(ffa_get_vm_id(d));
+        if ( ret != FFA_RET_OK )
+            return ret;
+    }
+
    unmap_domain_page_global(ctx->rx);
    unmap_domain_page_global(ctx->tx);
    put_page(ctx->rx_pg);
@@ -117,32 +206,63 @@ static void rxtx_unmap(struct ffa_ctx *ctx)
    ctx->tx_pg = NULL;
    ctx->page_count = 0;
    ctx->rx_is_free = false;
+
+    return FFA_RET_OK;
 }
 
 uint32_t ffa_handle_rxtx_unmap(void)
 {
-    struct domain *d = current->domain;
+    return rxtx_unmap(current->domain);
+}
+
+int32_t ffa_rx_acquire(struct domain *d)
+{
+    int32_t ret = FFA_RET_OK;
    struct ffa_ctx *ctx = d->arch.tee;
 
-    if ( !ctx->rx )
-        return FFA_RET_INVALID_PARAMETERS;
+    spin_lock(&ctx->rx_lock);
 
-    rxtx_unmap(ctx);
+    if ( !ctx->page_count )
+    {
+        ret = FFA_RET_DENIED;
+        goto out;
+    }
 
-    return FFA_RET_OK;
+    if ( !ctx->rx_is_free )
+    {
+        ret = FFA_RET_BUSY;
+        goto out;
+    }
+
+    if ( ffa_fw_supports_fid(FFA_RX_ACQUIRE) )
+    {
+        ret = ffa_simple_call(FFA_RX_ACQUIRE, ffa_get_vm_id(d), 0, 0, 0);
+        if ( ret != FFA_RET_OK )
+            goto out;
+    }
+    ctx->rx_is_free = false;
+out:
+    spin_unlock(&ctx->rx_lock);
+
+    return ret;
 }
 
-int32_t ffa_handle_rx_release(void)
+int32_t ffa_rx_release(struct domain *d)
 {
    int32_t ret = FFA_RET_DENIED;
-    struct domain *d = current->domain;
    struct ffa_ctx *ctx = d->arch.tee;
 
-    if ( !spin_trylock(&ctx->rx_lock) )
-        return FFA_RET_BUSY;
+    spin_lock(&ctx->rx_lock);
 
    if ( !ctx->page_count || ctx->rx_is_free )
        goto out;
+
+    if ( ffa_fw_supports_fid(FFA_RX_ACQUIRE) )
+    {
+        ret = ffa_simple_call(FFA_RX_RELEASE, ffa_get_vm_id(d), 0, 0, 0);
+        if ( ret != FFA_RET_OK )
+            goto out;
+    }
    ret = FFA_RET_OK;
    ctx->rx_is_free = true;

out:

xen/arch/arm/tee/ffa_rxtx.c +2 -16
@@ -151,23 +271,9 @@ out:
    return ret;
 }
 
-static int32_t ffa_rxtx_map(paddr_t tx_addr, paddr_t rx_addr,
-                            uint32_t page_count)
-{
-    return ffa_simple_call(FFA_RXTX_MAP_64, tx_addr, rx_addr, page_count, 0);
-}
-
-static int32_t ffa_rxtx_unmap(void)
-{
-    return ffa_simple_call(FFA_RXTX_UNMAP, 0, 0, 0, 0);
-}
-
 void ffa_rxtx_domain_destroy(struct domain *d)
 {
-    struct ffa_ctx *ctx = d->arch.tee;
-
-    if ( ctx->rx )
-        rxtx_unmap(ctx);
+    rxtx_unmap(d);
 }
 
 void ffa_rxtx_destroy(void)
@@ -186,7 +292,7 @@ void ffa_rxtx_destroy(void)
    }
 
    if ( need_unmap )
-        ffa_rxtx_unmap();
+        ffa_rxtx_unmap(0);
 }
 
 bool ffa_rxtx_init(void)
xen/arch/arm/tee/ffa_shm.c +0 -16
index 29675f9ba3f7..d628c1b70609 100644
@@ -16,21 +16,6 @@
 
 #include "ffa_private.h"
 
-/* Constituent memory region descriptor */
-struct ffa_address_range {
-    uint64_t address;
-    uint32_t page_count;
-    uint32_t reserved;
-};
-
-/* Composite memory region descriptor */
-struct ffa_mem_region {
-    uint32_t total_page_count;
-    uint32_t address_range_count;
-    uint64_t reserved;
-    struct ffa_address_range address_range_array[];
-};
-
 /* Memory access permissions descriptor */
 struct ffa_mem_access_perm {
    uint16_t endpoint_id;
--

2.47.0

#29 lore raw

Re: [PATCH v2 09/10] xen/arm: ffa: Remove per VM notif_enabled

In reply to Bertrand Marquis

Hi Bertrand,

Quoted 107 lines · Bertrand Marquis

Hi Jens,

Quoted 89 lines · Jens Wiklander

Hi Bertrand,

Quoted 83 lines · Bertrand Marquis

Remove the per VM flag to store if notifications are enabled or not as the only case where they are not, if notifications are enabled globally, will make the VM creation fail.
Also use the opportunity to always give the notifications interrupts IDs to VM. If the firmware does not support notifications, there won't be any generated and setting one will give back a NOT_SUPPORTED.

---
Changes in v2:
- rebase
---

xen/arch/arm/tee/ffa.c         | 17 +++--------------
xen/arch/arm/tee/ffa_notif.c   | 10 +---------
xen/arch/arm/tee/ffa_private.h |  2 --
3 files changed, 4 insertions(+), 25 deletions(-)
xen/arch/arm/tee/ffa.c +3 -14
index 72826b49d2aa..3a9525aa4598 100644
@@ -169,8 +169,6 @@ static void handle_version(struct cpu_user_regs *regs)
 
 static void handle_features(struct cpu_user_regs *regs)
 {
-    struct domain *d = current->domain;
-    struct ffa_ctx *ctx = d->arch.tee;
    uint32_t a1 = get_user_reg(regs, 1);
    unsigned int n;
 
@@ -218,16 +216,10 @@ static void handle_features(struct cpu_user_regs *regs)
        ffa_set_regs_success(regs, 0, 0);
        break;
    case FFA_FEATURE_NOTIF_PEND_INTR:
-        if ( ctx->notif.enabled )
-            ffa_set_regs_success(regs, GUEST_FFA_NOTIF_PEND_INTR_ID, 0);
-        else
-            ffa_set_regs_error(regs, FFA_RET_NOT_SUPPORTED);
+        ffa_set_regs_success(regs, GUEST_FFA_NOTIF_PEND_INTR_ID, 0);
        break;
    case FFA_FEATURE_SCHEDULE_RECV_INTR:
-        if ( ctx->notif.enabled )
-            ffa_set_regs_success(regs, GUEST_FFA_SCHEDULE_RECV_INTR_ID, 0);
-        else
-            ffa_set_regs_error(regs, FFA_RET_NOT_SUPPORTED);
+        ffa_set_regs_success(regs, GUEST_FFA_SCHEDULE_RECV_INTR_ID, 0);
        break;
 
    case FFA_NOTIFICATION_BIND:
@@ -236,10 +228,7 @@ static void handle_features(struct cpu_user_regs *regs)
    case FFA_NOTIFICATION_SET:
    case FFA_NOTIFICATION_INFO_GET_32:
    case FFA_NOTIFICATION_INFO_GET_64:
-        if ( ctx->notif.enabled )
-            ffa_set_regs_success(regs, 0, 0);
-        else
-            ffa_set_regs_error(regs, FFA_RET_NOT_SUPPORTED);
+        ffa_set_regs_success(regs, 0, 0);
        break;
    default:
        ffa_set_regs_error(regs, FFA_RET_NOT_SUPPORTED);
xen/arch/arm/tee/ffa_notif.c +0 -3
index 4b3e46318f4b..3c6418e62e2b 100644
@@ -405,7 +405,6 @@ void ffa_notif_init(void)
 
 int ffa_notif_domain_init(struct domain *d)
 {
-    struct ffa_ctx *ctx = d->arch.tee;
    int32_t res;
 
    if ( !notif_enabled )
@@ -415,18 +414,11 @@ int ffa_notif_domain_init(struct domain *d)
    if ( res )
        return -ENOMEM;
 
-    ctx->notif.enabled = true;
-
    return 0;
 }
 
Quoted 14 lines

xen/arch/arm/tee/ffa_notif.c int ffa_notif_domain_init(struct domain *d) · line 423

Quoted 8 lines
xen/arch/arm/tee/ffa_notif.c +1 -4
 void ffa_notif_domain_destroy(struct domain *d)
 {
-    struct ffa_ctx *ctx = d->arch.tee;
-
-    if ( ctx->notif.enabled )
-    {
+    if ( notif_enabled )
        ffa_notification_bitmap_destroy(ffa_get_vm_id(d));

This call may now be done even if there hasn't been a successful call to ffa_notification_bitmap_create().
A comment mentioning this and that it's harmless (if we can be sure it is) would be nice.

Quoted 6 lines

You mean in the case where it failed during domain_init ?

I can add the following comment:
Call bitmap_destroy even if bitmap create failed as the SPMC should return an error that we will ignore

Would that be ok ?

Yes, that's fine.

Cheers,
Jens

Quoted 28 lines

Cheers
Bertrand

Quoted 21 lines

Cheers,
Jens

Quoted 18 lines
xen/arch/arm/tee/ffa_notif.c +0 -2
-        ctx->notif.enabled = false;
-    }
 }
xen/arch/arm/tee/ffa_private.h +0 -3
index 02162e0ee4c7..973ee55be09b 100644
@@ -281,8 +281,6 @@ struct ffa_mem_region {
 };
 
 struct ffa_ctx_notif {
-    bool enabled;
-
    /*
     * True if domain is reported by FFA_NOTIFICATION_INFO_GET to have
     * pending global notifications.
--

2.47.0

#33 lore raw

Re: [PATCH v2 10/10] xen/arm: ffa: Add indirect message support

In reply to Bertrand Marquis

Hi Bertrand,

Quoted 118 lines · Bertrand Marquis

Hi Jens,

Quoted 106 lines · Jens Wiklander

Hi Bertrand,

Quoted 100 lines · Bertrand Marquis

Add support for FFA_MSG_SEND2 to send indirect messages from a VM to a secure partition.

---
Changes in v2:
- rebase
---

xen/arch/arm/tee/ffa.c         |  5 ++++
xen/arch/arm/tee/ffa_msg.c     | 49 ++++++++++++++++++++++++++++++++++
xen/arch/arm/tee/ffa_private.h |  1 +
3 files changed, 55 insertions(+)
xen/arch/arm/tee/ffa.c +5 -0
index 3a9525aa4598..21d41b452dc9 100644
@@ -101,6 +101,7 @@ static const struct ffa_fw_abi ffa_fw_abi_needed[] = {
    FW_ABI(FFA_MEM_RECLAIM),
    FW_ABI(FFA_MSG_SEND_DIRECT_REQ_32),
    FW_ABI(FFA_MSG_SEND_DIRECT_REQ_64),
+    FW_ABI(FFA_MSG_SEND2),
 };
 
 /*
@@ -195,6 +196,7 @@ static void handle_features(struct cpu_user_regs *regs)
    case FFA_PARTITION_INFO_GET:
    case FFA_MSG_SEND_DIRECT_REQ_32:
    case FFA_MSG_SEND_DIRECT_REQ_64:
+    case FFA_MSG_SEND2:
        ffa_set_regs_success(regs, 0, 0);
        break;
    case FFA_MEM_SHARE_64:
@@ -275,6 +277,9 @@ static bool ffa_handle_call(struct cpu_user_regs *regs)
    case FFA_MSG_SEND_DIRECT_REQ_64:
        ffa_handle_msg_send_direct_req(regs, fid);
        return true;
+    case FFA_MSG_SEND2:
+        e = ffa_handle_msg_send2(regs);
+        break;
    case FFA_MEM_SHARE_32:
    case FFA_MEM_SHARE_64:
        ffa_handle_mem_share(regs);
xen/arch/arm/tee/ffa_msg.c +41 -0
index ae263e54890e..335f246ba657 100644
@@ -12,6 +12,15 @@
 
 #include "ffa_private.h"
 
+/* Encoding of partition message in RX/TX buffer */
+struct ffa_part_msg_rxtx {
+    uint32_t flags;
+    uint32_t reserved;
+    uint32_t msg_offset;
+    uint32_t send_recv_id;
+    uint32_t msg_size;
+};
+
 void ffa_handle_msg_send_direct_req(struct cpu_user_regs *regs, uint32_t fid)
 {
    struct arm_smccc_1_2_regs arg = { .a0 = fid, };
@@ -78,3 +87,43 @@ out:
                 resp.a4 & mask, resp.a5 & mask, resp.a6 & mask,
                 resp.a7 & mask);
 }
+
+int32_t ffa_handle_msg_send2(struct cpu_user_regs *regs)
+{
+    struct domain *src_d = current->domain;
+    struct ffa_ctx *src_ctx = src_d->arch.tee;
+    const struct ffa_part_msg_rxtx *src_msg;
+    uint16_t dst_id, src_id;
+    int32_t ret;
+
+    if ( !ffa_fw_supports_fid(FFA_MSG_SEND2) )
+        return FFA_RET_NOT_SUPPORTED;
+
+    if ( !spin_trylock(&src_ctx->tx_lock) )
+        return FFA_RET_BUSY;
+
+    src_msg = src_ctx->tx;
+    src_id = src_msg->send_recv_id >> 16;
+    dst_id = src_msg->send_recv_id & GENMASK(15,0);
+
+    if ( src_id != ffa_get_vm_id(src_d) || !FFA_ID_IS_SECURE(dst_id) )
+    {
+        ret = FFA_RET_INVALID_PARAMETERS;
+        goto out_unlock_tx;
+    }
+
+    /* check source message fits in buffer */
+    if ( src_ctx->page_count * FFA_PAGE_SIZE <
+         src_msg->msg_offset + src_msg->msg_size ||
+         src_msg->msg_offset < sizeof(struct ffa_part_msg_rxtx) )
+    {
+        ret = FFA_RET_INVALID_PARAMETERS;
+        goto out_unlock_tx;
Quoted 8 lines

xen/arch/arm/tee/ffa_msg.c out: · line 122

Quoted 1 line
xen/arch/arm/tee/ffa_msg.c +1 -0
+    }

The guest can change src_mst at any moment with another CPU so these tests are only sanity checks. The SPMC will also have to lock and do the same tests again. So the tests here will only in the best case (in case the guest is misbehaving) save us from entering the SPMC only to get an error back. The lock makes sense since we could have concurrent calls to FFA_MEM_SHARE. How about removing the tests?

Quoted 7 lines

I think we should still prevent to forward invalid requests to the SPMC as much as we can to prevent a malicious guest from stilling CPU cycles by doing invalid calls to the secure world.

I could put a comment in there saying that this is just protection but to be fare the SPMC in secure will have the same issues: this can be changed at any time by the caller on another core.

Fair enough.

Quoted 10 lines
Quoted 6 lines

xen/arch/arm/tee/ffa_msg.c out: · line 124

Quoted 4 lines
xen/arch/arm/tee/ffa_msg.c +2 -0
+
+    ret = ffa_simple_call(FFA_MSG_SEND2, ((uint32_t)src_id) << 16, 0, 0, 0);

I'd rather use ffa_get_vm_id(src_d) instead of src_id.

src_id is a local variable and was checked to be equal to ffa_get_vm_id(src_d) upper so those 2 values are the same.
Why would you rather recall ffa_get_vm_id here ?

I don't think that check is enough to prevent the compiler from loading that value from memory again, potentially opening a time-of-check to time-of-use window. Using ACCESS_ONCE() when reading send_recv_id above should also take care of that, but it seems more direct to use ffa_get_vm_id().

Cheers,
Jens

Quoted 29 lines

Cheers
Bertrand

Quoted 23 lines

Cheers,
Jens

Quoted 19 lines
xen/arch/arm/tee/ffa_msg.c +5 -0
+
+out_unlock_tx:
+    spin_unlock(&src_ctx->tx_lock);
+    return ret;
+}
xen/arch/arm/tee/ffa_private.h +1 -1
index 973ee55be09b..d441c0ca5598 100644
@@ -359,6 +359,7 @@ void ffa_handle_notification_get(struct cpu_user_regs *regs);
 int ffa_handle_notification_set(struct cpu_user_regs *regs);
 
 void ffa_handle_msg_send_direct_req(struct cpu_user_regs *regs, uint32_t fid);
+int32_t ffa_handle_msg_send2(struct cpu_user_regs *regs);
 
 static inline uint16_t ffa_get_vm_id(const struct domain *d)
 {
--

2.47.0

#34 lore raw

Re: [PATCH v2 10/10] xen/arm: ffa: Add indirect message support

Hi Jens,

Quoted 145 lines · Jens Wiklander

Hi Bertrand,

Quoted 118 lines · Bertrand Marquis

Hi Jens,

Quoted 106 lines · Jens Wiklander

Hi Bertrand,

Quoted 100 lines · Bertrand Marquis

Add support for FFA_MSG_SEND2 to send indirect messages from a VM to a secure partition.

---
Changes in v2:
- rebase
---

xen/arch/arm/tee/ffa.c         |  5 ++++
xen/arch/arm/tee/ffa_msg.c     | 49 ++++++++++++++++++++++++++++++++++
xen/arch/arm/tee/ffa_private.h |  1 +
3 files changed, 55 insertions(+)
xen/arch/arm/tee/ffa.c +5 -0
index 3a9525aa4598..21d41b452dc9 100644
@@ -101,6 +101,7 @@ static const struct ffa_fw_abi ffa_fw_abi_needed[] = {
   FW_ABI(FFA_MEM_RECLAIM),
   FW_ABI(FFA_MSG_SEND_DIRECT_REQ_32),
   FW_ABI(FFA_MSG_SEND_DIRECT_REQ_64),
+    FW_ABI(FFA_MSG_SEND2),
 };
 
 /*
@@ -195,6 +196,7 @@ static void handle_features(struct cpu_user_regs *regs)
   case FFA_PARTITION_INFO_GET:
   case FFA_MSG_SEND_DIRECT_REQ_32:
   case FFA_MSG_SEND_DIRECT_REQ_64:
+    case FFA_MSG_SEND2:
       ffa_set_regs_success(regs, 0, 0);
       break;
   case FFA_MEM_SHARE_64:
@@ -275,6 +277,9 @@ static bool ffa_handle_call(struct cpu_user_regs *regs)
   case FFA_MSG_SEND_DIRECT_REQ_64:
       ffa_handle_msg_send_direct_req(regs, fid);
       return true;
+    case FFA_MSG_SEND2:
+        e = ffa_handle_msg_send2(regs);
+        break;
   case FFA_MEM_SHARE_32:
   case FFA_MEM_SHARE_64:
       ffa_handle_mem_share(regs);
xen/arch/arm/tee/ffa_msg.c +41 -0
index ae263e54890e..335f246ba657 100644
@@ -12,6 +12,15 @@
 
 #include "ffa_private.h"
 
+/* Encoding of partition message in RX/TX buffer */
+struct ffa_part_msg_rxtx {
+    uint32_t flags;
+    uint32_t reserved;
+    uint32_t msg_offset;
+    uint32_t send_recv_id;
+    uint32_t msg_size;
+};
+
 void ffa_handle_msg_send_direct_req(struct cpu_user_regs *regs, uint32_t fid)
 {
   struct arm_smccc_1_2_regs arg = { .a0 = fid, };
@@ -78,3 +87,43 @@ out:
                resp.a4 & mask, resp.a5 & mask, resp.a6 & mask,
                resp.a7 & mask);
 }
+
+int32_t ffa_handle_msg_send2(struct cpu_user_regs *regs)
+{
+    struct domain *src_d = current->domain;
+    struct ffa_ctx *src_ctx = src_d->arch.tee;
+    const struct ffa_part_msg_rxtx *src_msg;
+    uint16_t dst_id, src_id;
+    int32_t ret;
+
+    if ( !ffa_fw_supports_fid(FFA_MSG_SEND2) )
+        return FFA_RET_NOT_SUPPORTED;
+
+    if ( !spin_trylock(&src_ctx->tx_lock) )
+        return FFA_RET_BUSY;
+
+    src_msg = src_ctx->tx;
+    src_id = src_msg->send_recv_id >> 16;
+    dst_id = src_msg->send_recv_id & GENMASK(15,0);
+
+    if ( src_id != ffa_get_vm_id(src_d) || !FFA_ID_IS_SECURE(dst_id) )
+    {
+        ret = FFA_RET_INVALID_PARAMETERS;
+        goto out_unlock_tx;
+    }
+
+    /* check source message fits in buffer */
+    if ( src_ctx->page_count * FFA_PAGE_SIZE <
+         src_msg->msg_offset + src_msg->msg_size ||
+         src_msg->msg_offset < sizeof(struct ffa_part_msg_rxtx) )
+    {
+        ret = FFA_RET_INVALID_PARAMETERS;
+        goto out_unlock_tx;
Quoted 8 lines

xen/arch/arm/tee/ffa_msg.c out: · line 122

Quoted 1 line
xen/arch/arm/tee/ffa_msg.c +1 -0
+    }

The guest can change src_mst at any moment with another CPU so these tests are only sanity checks. The SPMC will also have to lock and do the same tests again. So the tests here will only in the best case (in case the guest is misbehaving) save us from entering the SPMC only to get an error back. The lock makes sense since we could have concurrent calls to FFA_MEM_SHARE. How about removing the tests?

Quoted 7 lines

I think we should still prevent to forward invalid requests to the SPMC as much as we can to prevent a malicious guest from stilling CPU cycles by doing invalid calls to the secure world.

I could put a comment in there saying that this is just protection but to be fare the SPMC in secure will have the same issues: this can be changed at any time by the caller on another core.

Fair enough.

Quoted 10 lines
Quoted 6 lines

xen/arch/arm/tee/ffa_msg.c out: · line 124

Quoted 4 lines
xen/arch/arm/tee/ffa_msg.c +2 -0
+
+    ret = ffa_simple_call(FFA_MSG_SEND2, ((uint32_t)src_id) << 16, 0, 0, 0);

I'd rather use ffa_get_vm_id(src_d) instead of src_id.

src_id is a local variable and was checked to be equal to ffa_get_vm_id(src_d) upper so those 2 values are the same.
Why would you rather recall ffa_get_vm_id here ?

Quoted 5 lines

I don't think that check is enough to prevent the compiler from loading that value from memory again, potentially opening a time-of-check to time-of-use window. Using ACCESS_ONCE() when reading send_recv_id above should also take care of that, but it seems more direct to use ffa_get_vm_id().

Ok I will use ffa_get_vm_id in v3.

Thanks a lot for the review.

Cheers
Bertrand

Quoted 31 lines

Cheers,
Jens

Quoted 27 lines

Cheers
Bertrand

Quoted 23 lines

Cheers,
Jens

Quoted 19 lines
xen/arch/arm/tee/ffa_msg.c +5 -0
+
+out_unlock_tx:
+    spin_unlock(&src_ctx->tx_lock);
+    return ret;
+}
xen/arch/arm/tee/ffa_private.h +1 -1
index 973ee55be09b..d441c0ca5598 100644
@@ -359,6 +359,7 @@ void ffa_handle_notification_get(struct cpu_user_regs *regs);
 int ffa_handle_notification_set(struct cpu_user_regs *regs);
 
 void ffa_handle_msg_send_direct_req(struct cpu_user_regs *regs, uint32_t fid);
+int32_t ffa_handle_msg_send2(struct cpu_user_regs *regs);
 
 static inline uint16_t ffa_get_vm_id(const struct domain *d)
 {
--

2.47.0