Skip to contentSkip to conversation

xen/arm: ffa: Improvements and fixes

PATCH Superseded Bertrand Marquis 3 participants 29 messages Sep 19, 2024 – Sep 26, 2024

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

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.

Bertrand Marquis (10):
xen/arm: ffa: Rework firmware discovery xen/arm: ffa: Rework feature discovery xen/arm: ffa: fix version negociation 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          | 242 ++++++++++++++------------------
 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  |  66 +++++++--
 xen/arch/arm/tee/ffa_rxtx.c     | 139 +++++++++++++++---
 xen/arch/arm/tee/ffa_shm.c      |  39 ++---
 8 files changed, 605 insertions(+), 258 deletions(-)
 create mode 100644 xen/arch/arm/tee/ffa_msg.c
Signature · 1 line
2.39.5 (Apple Git-154)
#1 lore raw

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

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.

---
 xen/arch/arm/tee/ffa.c | 52 +++++++++++++++++++++++++-----------------
 1 file changed, 31 insertions(+), 21 deletions(-)
xen/arch/arm/tee/ffa.c +31 -21
index 022089278e1c..7c84aa6aa43d 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,25 +514,21 @@ 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, "ffa: FFA_VERSION returned not supported\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;
-    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);
-
     /*
      * 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
@@ -546,12 +542,24 @@ 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;
+    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);
+
+    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_INFO "ARM FF-A No firmware support\n");
 
     return false;
 }
Signature · 1 line
2.39.5 (Apple Git-154)
#2 lore raw

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

Hi Bertrand,

Quoted 59 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.

---
  xen/arch/arm/tee/ffa.c | 52 +++++++++++++++++++++++++-----------------
  1 file changed, 31 insertions(+), 21 deletions(-)
xen/arch/arm/tee/ffa.c +6 -6
index 022089278e1c..7c84aa6aa43d 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,25 +514,21 @@ static bool ffa_probe(void)
          printk(XENLOG_ERR

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

Quoted 10 lines
xen/arch/arm/tee/ffa.c +3 -2
                 "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, "ffa: FFA_VERSION returned not supported\n");

This error message relies on the implementation of ffa_get_version(). It made sense in the previous placement, but here, it seems a little bit odd. So if you want to move the error message, then I think it should be reworded to be more generic.

Maybe: "Cannot retrieve the FFA version".

Quoted 4 lines
xen/arch/arm/tee/ffa.c +2 -0
+        goto err_no_fw;
+    }
 
      if ( vers < FFA_MIN_SPMC_VERSION || vers > FFA_MY_VERSION )

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

Quoted 10 lines
xen/arch/arm/tee/ffa.c +1 -5
      {
          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;
-    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);

I kind of understand why we are moving the Medatior version early but...

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

Quoted 2 lines
xen/arch/arm/tee/ffa.c +0 -2
-    printk(XENLOG_INFO "ARM FF-A Firmware version %u.%u\n",
-           major_vers, minor_vers);

... I am not sure why we would move this print later. Wouldn't this be useful to know if there is a missing feature?

Quoted 34 lines
xen/arch/arm/tee/ffa.c +16 -5
-
      /*
       * 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
@@ -546,12 +542,24 @@ 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;
+    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);
+
+    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;

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

Quoted 7 lines
xen/arch/arm/tee/ffa.c +3 -1
@@ -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_INFO "ARM FF-A No firmware support\n");

I am guessing if we are trying to probe FFA, then most likely the user expected to use it. So shouldn't this be a XENLOG_WARN?

Cheers,

Signature · 1 line
Julien Grall
#3 lore raw

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

Quoted 84 lines · Julien Grall

Hi Bertrand,

Quoted 54 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.

---
  xen/arch/arm/tee/ffa.c | 52 +++++++++++++++++++++++++-----------------

  1 file changed, 31 insertions(+), 21 deletions(-)
xen/arch/arm/tee/ffa.c +1 -2
index 022089278e1c..7c84aa6aa43d 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;
  /*

xen/arch/arm/tee/ffa.c +4 -4
@@ -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,25 +514,21 @@ 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);

Quoted 8 lines

-        return false;
+        goto err_no_fw;
      }
      if ( !ffa_get_version(&vers) )
-        return false;
+    {
+        gprintk(XENLOG_ERR, "ffa: FFA_VERSION returned not supported\n");

This error message relies on the implementation of ffa_get_version(). It made sense in the previous placement, but here, it seems a little bit odd. So if you want to move the error message, then I think it should be reworded to be more generic.

Maybe: "Cannot retrieve the FFA version".

Quoted 5 lines

+        goto err_no_fw;
+    }
      if ( vers < FFA_MIN_SPMC_VERSION || vers > FFA_MY_VERSION ) {
          printk(XENLOG_ERR "ffa: Incompatible version %#x found\n", vers);

Quoted 8 lines

-        return false;
+        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 Mediator version %u.%u\n", -           FFA_MY_VERSION_MAJOR, FFA_MY_VERSION_MINOR);

Quoted 6 lines

I kind of understand why we are moving the Medatior version early but...

Quoted 2 lines

-    printk(XENLOG_INFO "ARM FF-A Firmware version %u.%u\n", -           major_vers, minor_vers);

... I am not sure why we would move this print later. Wouldn't this be useful to know if there is a missing feature?

Ah so in patch #2 you re-ordered the code again and it looks the same as today. It would be better if we can avoid moving the code completely.

Cheers,

Signature · 1 line
Julien Grall
#4 lore raw

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

In reply to Julien Grall

Hi Julien,

Quoted 64 lines · Julien Grall

Hi Bertrand,

Quoted 46 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.

---
 xen/arch/arm/tee/ffa.c | 52 +++++++++++++++++++++++++-----------------
 1 file changed, 31 insertions(+), 21 deletions(-)
xen/arch/arm/tee/ffa.c +2 -4
index 022089278e1c..7c84aa6aa43d 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 )

xen/arch/arm/tee/ffa.c +3 -0
+    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.

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

Quoted 10 lines
xen/arch/arm/tee/ffa.c +3 -2
@@ -514,25 +514,21 @@ 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, "ffa: FFA_VERSION returned not supported\n");

This error message relies on the implementation of ffa_get_version(). It made sense in the previous placement, but here, it seems a little bit odd. So if you want to move the error message, then I think it should be reworded to be more generic.

Quoted 1 line

Maybe: "Cannot retrieve the FFA version".

Ack

Quoted 14 lines

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

Quoted 13 lines
xen/arch/arm/tee/ffa.c +3 -4
+        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;
-    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);
Quoted 6 lines

I kind of understand why we are moving the Medatior version early but...

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

Quoted 2 lines
xen/arch/arm/tee/ffa.c +0 -2
-    printk(XENLOG_INFO "ARM FF-A Firmware version %u.%u\n",
-           major_vers, minor_vers);

... I am not sure why we would move this print later. Wouldn't this be useful to know if there is a missing feature?

True I will move it back up.

Quoted 31 lines
Quoted 31 lines
xen/arch/arm/tee/ffa.c +16 -3
-
     /*
      * 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
@@ -546,12 +542,24 @@ 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;
+    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);
+
+    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)
Quoted 8 lines

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

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

I am guessing if we are trying to probe FFA, then most likely the user expected to use it. So shouldn't this be a XENLOG_WARN?

Ack.

Cheers
Bertrand

Quoted 5 lines

Cheers,

Signature · 1 line
Julien Grall
#5 lore raw

PATCH 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.

---
 xen/arch/arm/tee/ffa.c | 61 +++++++++++++++++++++---------------------
 1 file changed, 30 insertions(+), 31 deletions(-)
xen/arch/arm/tee/ffa.c +30 -31
index 7c84aa6aa43d..7ff2529b2055 100644
@@ -74,6 +74,24 @@
 /* Negotiated FF-A version to use with the SPMC, 0 if not there or supported */
 static uint32_t __ro_after_init ffa_fw_version;
 
+/* List of ABI we use from the firmware */
+static const uint32_t ffa_fw_feat_needed[] = {
+    FFA_VERSION,
+    FFA_FEATURES,
+    FFA_NOTIFICATION_BITMAP_CREATE,
+    FFA_NOTIFICATION_BITMAP_DESTROY,
+    FFA_PARTITION_INFO_GET,
+    FFA_NOTIFICATION_INFO_GET_64,
+    FFA_NOTIFICATION_GET,
+    FFA_RX_RELEASE,
+    FFA_RXTX_MAP_64,
+    FFA_RXTX_UNMAP,
+    FFA_MEM_SHARE_32,
+    FFA_MEM_SHARE_64,
+    FFA_MEM_RECLAIM,
+    FFA_MSG_SEND_DIRECT_REQ_32,
+    FFA_MSG_SEND_DIRECT_REQ_64,
+};
 
 /*
  * Our rx/tx buffers shared with the SPMC. FFA_RXTX_PAGE_COUNT is the
@@ -112,20 +130,9 @@ static bool ffa_get_version(uint32_t *vers)
     return true;
 }
 
-static int32_t ffa_features(uint32_t id)
+static bool ffa_feature_supported(uint32_t id)
 {
-    return ffa_simple_call(FFA_FEATURES, id, 0, 0, 0);
-}
-
-static bool check_mandatory_feature(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)
@@ -529,24 +536,6 @@ static bool ffa_probe(void)
         goto err_no_fw;
     }
 
-    /*
-     * 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.
-     */
-    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) )
-    {
-        printk(XENLOG_ERR "ffa: Mandatory feature not supported by fw\n");
-        goto err_no_fw;
-    }
-
     major_vers = (vers >> FFA_VERSION_MAJOR_SHIFT)
                  & FFA_VERSION_MAJOR_MASK;
     minor_vers = vers & FFA_VERSION_MINOR_MASK;
@@ -555,6 +544,16 @@ static bool ffa_probe(void)
 
     ffa_fw_version = vers;
 
+    for ( int i = 0; i < ARRAY_SIZE(ffa_fw_feat_needed); i++ )
+    {
+        if ( !ffa_feature_supported(ffa_fw_feat_needed[i]) )
+        {
+            printk(XENLOG_INFO "ARM FF-A Firmware does not support 0x%08x\n",
+                   ffa_fw_feat_needed[i]);
+            goto err_no_fw;
+        }
+    }
+
     if ( !ffa_rxtx_init() )
     {
         printk(XENLOG_ERR "ffa: Error during RXTX buffer init\n");
Signature · 1 line
2.39.5 (Apple Git-154)
#6 lore raw

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

Hi,

Quoted 26 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.

---
  xen/arch/arm/tee/ffa.c | 61 +++++++++++++++++++++---------------------
  1 file changed, 30 insertions(+), 31 deletions(-)
xen/arch/arm/tee/ffa.c +8 -0
index 7c84aa6aa43d..7ff2529b2055 100644
@@ -74,6 +74,24 @@
  /* Negotiated FF-A version to use with the SPMC, 0 if not there or supported */
  static uint32_t __ro_after_init ffa_fw_version;
 
+/* List of ABI we use from the firmware */
+static const uint32_t ffa_fw_feat_needed[] = {
+    FFA_VERSION,
+    FFA_FEATURES,
+    FFA_NOTIFICATION_BITMAP_CREATE,
+    FFA_NOTIFICATION_BITMAP_DESTROY,
+    FFA_PARTITION_INFO_GET,
+    FFA_NOTIFICATION_INFO_GET_64,

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

Quoted 10 lines
xen/arch/arm/tee/ffa.c +10 -0
+    FFA_NOTIFICATION_GET,
+    FFA_RX_RELEASE,
+    FFA_RXTX_MAP_64,
+    FFA_RXTX_UNMAP,
+    FFA_MEM_SHARE_32,
+    FFA_MEM_SHARE_64,
+    FFA_MEM_RECLAIM,
+    FFA_MSG_SEND_DIRECT_REQ_32,
+    FFA_MSG_SEND_DIRECT_REQ_64,
+};

NIT: As we are creating an array, could be take the opportunity to provide a name for each feature (we could use macro for that)? This would make easier for the user to know which feature is missing.

Quoted 27 lines
xen/arch/arm/tee/ffa.c +2 -13
  /*
   * Our rx/tx buffers shared with the SPMC. FFA_RXTX_PAGE_COUNT is the
@@ -112,20 +130,9 @@ static bool ffa_get_version(uint32_t *vers)
      return true;
  }
 
-static int32_t ffa_features(uint32_t id)
+static bool ffa_feature_supported(uint32_t id)
  {
-    return ffa_simple_call(FFA_FEATURES, id, 0, 0, 0);
-}
-
-static bool check_mandatory_feature(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)

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

Quoted 8 lines
xen/arch/arm/tee/ffa.c +0 -5
@@ -529,24 +536,6 @@ static bool ffa_probe(void)
          goto err_no_fw;
      }
 
-    /*
-     * 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.
-     */

You removed this TODO but I don't think it was addressed. Can you clarify?

Quoted 17 lines
xen/arch/arm/tee/ffa.c +0 -13
-    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) )
-    {
-        printk(XENLOG_ERR "ffa: Mandatory feature not supported by fw\n");
-        goto err_no_fw;
-    }
-
      major_vers = (vers >> FFA_VERSION_MAJOR_SHIFT)
                   & FFA_VERSION_MAJOR_MASK;
      minor_vers = vers & FFA_VERSION_MINOR_MASK;

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

Quoted 4 lines
xen/arch/arm/tee/ffa.c +1 -0
@@ -555,6 +544,16 @@ static bool ffa_probe(void)
 
      ffa_fw_version = vers;
 
+    for ( int i = 0; i < ARRAY_SIZE(ffa_fw_feat_needed); i++ )

This is an index, so please use unsigned int (even though it should technically be size_t).

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

Quoted 12 lines
xen/arch/arm/tee/ffa.c +9 -0
+    {
+        if ( !ffa_feature_supported(ffa_fw_feat_needed[i]) )
+        {
+            printk(XENLOG_INFO "ARM FF-A Firmware does not support 0x%08x\n",
+                   ffa_fw_feat_needed[i]);
+            goto err_no_fw;
+        }
+    }
+
      if ( !ffa_rxtx_init() )
      {
          printk(XENLOG_ERR "ffa: Error during RXTX buffer init\n");

Cheers,

Signature · 1 line
Julien Grall
#7 lore raw

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

Hi Julien,

Thanks a lot for the review.

Quoted 31 lines · Julien Grall

Hi,

Quoted 26 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.

---
 xen/arch/arm/tee/ffa.c | 61 +++++++++++++++++++++---------------------
 1 file changed, 30 insertions(+), 31 deletions(-)
xen/arch/arm/tee/ffa.c +11 -0
index 7c84aa6aa43d..7ff2529b2055 100644
@@ -74,6 +74,24 @@
 /* Negotiated FF-A version to use with the SPMC, 0 if not there or supported */
 static uint32_t __ro_after_init ffa_fw_version;
 +/* List of ABI we use from the firmware */
+static const uint32_t ffa_fw_feat_needed[] = {
+    FFA_VERSION,
+    FFA_FEATURES,
+    FFA_NOTIFICATION_BITMAP_CREATE,
+    FFA_NOTIFICATION_BITMAP_DESTROY,
+    FFA_PARTITION_INFO_GET,
+    FFA_NOTIFICATION_INFO_GET_64,
+    FFA_NOTIFICATION_GET,
+    FFA_RX_RELEASE,
+    FFA_RXTX_MAP_64,
+    FFA_RXTX_UNMAP,
Quoted 8 lines

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

Quoted 6 lines
xen/arch/arm/tee/ffa.c +6 -0
+    FFA_MEM_SHARE_32,
+    FFA_MEM_SHARE_64,
+    FFA_MEM_RECLAIM,
+    FFA_MSG_SEND_DIRECT_REQ_32,
+    FFA_MSG_SEND_DIRECT_REQ_64,
+};

NIT: As we are creating an array, could be take the opportunity to provide a name for each feature (we could use macro for that)? This would make easier for the user to know which feature is missing.

In fact those are not "features" per say but ABI we need to use with the firmware so maybe i should first rename the variable to say abi instead of feat.

Now we could create some features out of those as in practice several ABIs are needed to be able to use one feature (for example notifications support require the INFO_GET and the GET).

In your mind you wanted to have something like:
"Version", FFA_VERSION
"Direct Messages", FFA_MSG_SEND_DIRECT_REQ_32,
"Direct Messages", FFA_MSG_SEND_DIRECT_REQ_64

So that we could print a more meaningfull name or would you be ok with just printing "FFA_MSG_SEND_DIRECT_REQ_32" ?

Quoted 26 lines
Quoted 26 lines
xen/arch/arm/tee/ffa.c +2 -12
   /*
  * Our rx/tx buffers shared with the SPMC. FFA_RXTX_PAGE_COUNT is the
@@ -112,20 +130,9 @@ static bool ffa_get_version(uint32_t *vers)
     return true;
 }
 -static int32_t ffa_features(uint32_t id)
+static bool ffa_feature_supported(uint32_t id)
 {
-    return ffa_simple_call(FFA_FEATURES, id, 0, 0, 0);
-}
-
-static bool check_mandatory_feature(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)
@@ -529,24 +536,6 @@ static bool ffa_probe(void)
         goto err_no_fw;
Quoted 8 lines

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

Quoted 6 lines
xen/arch/arm/tee/ffa.c +0 -4
     }
 -    /*
-     * 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.
-     */

You removed this TODO but I don't think it was addressed. Can you clarify?

Right this should only be removed with the next patch when we add fine granular call support. I will readd it here and remove it in the fine granular patch.

Quoted 14 lines
Quoted 14 lines
xen/arch/arm/tee/ffa.c +0 -13
-    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) )
-    {
-        printk(XENLOG_ERR "ffa: Mandatory feature not supported by fw\n");
-        goto err_no_fw;
-    }
-
Quoted 8 lines

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

Quoted 6 lines
xen/arch/arm/tee/ffa.c +0 -0
     major_vers = (vers >> FFA_VERSION_MAJOR_SHIFT)
                  & FFA_VERSION_MAJOR_MASK;
     minor_vers = vers & FFA_VERSION_MINOR_MASK;
@@ -555,6 +544,16 @@ static bool ffa_probe(void)
       ffa_fw_version = vers;
 +    for ( int i = 0; i < ARRAY_SIZE(ffa_fw_feat_needed); i++ )

This is an index, so please use unsigned int (even though it should technically be size_t).

Ack

Quoted 14 lines

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

Quoted 13 lines
xen/arch/arm/tee/ffa.c +9 -0
+    {
+        if ( !ffa_feature_supported(ffa_fw_feat_needed[i]) )
+        {
+            printk(XENLOG_INFO "ARM FF-A Firmware does not support 0x%08x\n",
+                   ffa_fw_feat_needed[i]);
+            goto err_no_fw;
+        }
+    }
+
     if ( !ffa_rxtx_init() )
     {
         printk(XENLOG_ERR "ffa: Error during RXTX buffer init\n");
Quoted 1 line

Cheers,

Cheers
Bertrand

Quoted 3 lines
Signature · 2 lines
-- 
Julien Grall
#8 lore raw

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

Hi Bertrand,

Quoted 40 lines · Bertrand Marquis
Quoted 31 lines · Julien Grall

Hi,

Quoted 26 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.

---
  xen/arch/arm/tee/ffa.c | 61 +++++++++++++++++++++---------------------
  1 file changed, 30 insertions(+), 31 deletions(-)
xen/arch/arm/tee/ffa.c +11 -0
index 7c84aa6aa43d..7ff2529b2055 100644
@@ -74,6 +74,24 @@
  /* Negotiated FF-A version to use with the SPMC, 0 if not there or supported */
  static uint32_t __ro_after_init ffa_fw_version;
  +/* List of ABI we use from the firmware */
+static const uint32_t ffa_fw_feat_needed[] = {
+    FFA_VERSION,
+    FFA_FEATURES,
+    FFA_NOTIFICATION_BITMAP_CREATE,
+    FFA_NOTIFICATION_BITMAP_DESTROY,
+    FFA_PARTITION_INFO_GET,
+    FFA_NOTIFICATION_INFO_GET_64,
+    FFA_NOTIFICATION_GET,
+    FFA_RX_RELEASE,
+    FFA_RXTX_MAP_64,
+    FFA_RXTX_UNMAP,
Quoted 8 lines

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

Quoted 6 lines
xen/arch/arm/tee/ffa.c +6 -0
+    FFA_MEM_SHARE_32,
+    FFA_MEM_SHARE_64,
+    FFA_MEM_RECLAIM,
+    FFA_MSG_SEND_DIRECT_REQ_32,
+    FFA_MSG_SEND_DIRECT_REQ_64,
+};

NIT: As we are creating an array, could be take the opportunity to provide a name for each feature (we could use macro for that)? This would make easier for the user to know which feature is missing.

Quoted 1 line

In fact those are not "features" per say but ABI we need to use with the firmware so maybe i should first rename the variable to say abi instead of feat.

Thanks for the clarification! With that in mind...

Quoted 9 lines

Now we could create some features out of those as in practice several ABIs are needed to be able to use one feature (for example notifications support require the INFO_GET and the GET).

In your mind you wanted to have something like:
"Version", FFA_VERSION
"Direct Messages", FFA_MSG_SEND_DIRECT_REQ_32,
"Direct Messages", FFA_MSG_SEND_DIRECT_REQ_64

So that we could print a more meaningfull name or would you be ok with just printing "FFA_MSG_SEND_DIRECT_REQ_32" ?

... I was more thinking about printing which ABI is missing. This is more helpful to the user than knowning which features will be missing.

This has also the advantage that we can use macro to generate the names.

Cheers,

Signature · 1 line
Julien Grall
#9 lore raw

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

Hi Julien,

Quoted 55 lines · Julien Grall

Hi Bertrand,

Quoted 39 lines · Bertrand Marquis
Quoted 31 lines · Julien Grall

Hi,

Quoted 26 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.

---
 xen/arch/arm/tee/ffa.c | 61 +++++++++++++++++++++---------------------
 1 file changed, 30 insertions(+), 31 deletions(-)
xen/arch/arm/tee/ffa.c +11 -0
index 7c84aa6aa43d..7ff2529b2055 100644
@@ -74,6 +74,24 @@
 /* Negotiated FF-A version to use with the SPMC, 0 if not there or supported */
 static uint32_t __ro_after_init ffa_fw_version;
 +/* List of ABI we use from the firmware */
+static const uint32_t ffa_fw_feat_needed[] = {
+    FFA_VERSION,
+    FFA_FEATURES,
+    FFA_NOTIFICATION_BITMAP_CREATE,
+    FFA_NOTIFICATION_BITMAP_DESTROY,
+    FFA_PARTITION_INFO_GET,
+    FFA_NOTIFICATION_INFO_GET_64,
+    FFA_NOTIFICATION_GET,
+    FFA_RX_RELEASE,
+    FFA_RXTX_MAP_64,
+    FFA_RXTX_UNMAP,
Quoted 8 lines

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

Quoted 6 lines
xen/arch/arm/tee/ffa.c +6 -0
+    FFA_MEM_SHARE_32,
+    FFA_MEM_SHARE_64,
+    FFA_MEM_RECLAIM,
+    FFA_MSG_SEND_DIRECT_REQ_32,
+    FFA_MSG_SEND_DIRECT_REQ_64,
+};

NIT: As we are creating an array, could be take the opportunity to provide a name for each feature (we could use macro for that)? This would make easier for the user to know which feature is missing.

Quoted 1 line

In fact those are not "features" per say but ABI we need to use with the firmware so maybe i should first rename the variable to say abi instead of feat.

Thanks for the clarification! With that in mind...

Quoted 6 lines

Now we could create some features out of those as in practice several ABIs are needed to be able to use one feature (for example notifications support require the INFO_GET and the GET). In your mind you wanted to have something like:
"Version", FFA_VERSION
"Direct Messages", FFA_MSG_SEND_DIRECT_REQ_32,
"Direct Messages", FFA_MSG_SEND_DIRECT_REQ_64
So that we could print a more meaningfull name or would you be ok with just printing "FFA_MSG_SEND_DIRECT_REQ_32" ?

Quoted 3 lines

... I was more thinking about printing which ABI is missing. This is more helpful to the user than knowning which features will be missing.

This has also the advantage that we can use macro to generate the names.

Ok then i will build a table of strings of the ABI names and use that to say what ABI is not supported when there is one.

Cheers
Bertrand

Quoted 6 lines

Cheers,

Signature · 1 line
Julien Grall
#10 lore raw

PATCH 3/10 xen/arm: ffa: fix version negotiation

In reply to Bertrand Marquis

Fix FFA version negotiation with the firmware to follow the specification guidance more closely.
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:
- return an error for a different major
- return 1.1 for a version >= 1.1
- return 1.0 if 1.0 was requested

---
 xen/arch/arm/tee/ffa.c | 38 ++++++++++++++++++++++++++++++--------
 1 file changed, 30 insertions(+), 8 deletions(-)
xen/arch/arm/tee/ffa.c +30 -8
index 7ff2529b2055..1f602f25d097 100644
@@ -141,13 +141,24 @@ 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;
+    /**
+     * As of now we only support 1.0 or 1.1.
+     * For any 1.x >= 1.1 return OK with 1.1
+     * For 1.0 return OK with 1.0
+     * For anything else return an error.
+     */
+    if ( (vers >> FFA_VERSION_MAJOR_SHIFT) == FFA_MY_VERSION_MAJOR )
+    {
+        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);
+        ctx->guest_vers = vers;
+        ffa_set_regs(regs, vers, 0, 0, 0, 0, 0, 0, 0);
+    }
+    else
+        ffa_set_regs_error(regs, FFA_RET_NOT_SUPPORTED);
 }
 
 static void handle_msg_send_direct_req(struct cpu_user_regs *regs, uint32_t fid)
@@ -530,7 +541,8 @@ static bool ffa_probe(void)
         goto err_no_fw;
     }
 
-    if ( vers < FFA_MIN_SPMC_VERSION || vers > FFA_MY_VERSION )
+    if ( vers < FFA_MIN_SPMC_VERSION ||
+              (vers >> FFA_VERSION_MAJOR_SHIFT) != FFA_MY_VERSION_MAJOR )
     {
         printk(XENLOG_ERR "ffa: Incompatible version %#x found\n", vers);
         goto err_no_fw;
@@ -542,7 +554,17 @@ static bool ffa_probe(void)
     printk(XENLOG_INFO "ARM FF-A Firmware version %u.%u\n",
            major_vers, minor_vers);
 
-    ffa_fw_version = 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 is rejected before.
+     */
+    if ( vers > FFA_MY_VERSION )
+        ffa_fw_version = FFA_MY_VERSION;
+    else
+        ffa_fw_version = vers;
 
     for ( int i = 0; i < ARRAY_SIZE(ffa_fw_feat_needed); i++ )
     {
Signature · 1 line
2.39.5 (Apple Git-154)
#11 lore raw

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

Hi Bertrand,

NIT Typo: s/fix/Fix/ to match the other title

Quoted 2 lines · Bertrand Marquis

Fix FFA version negotiation with the firmware to follow the specification guidance more closely.

To confirm, below is based on 13.2.1 in DEN0077A, is that correct? If so, can you add a link in the commit message (and maybe code).

Quoted 30 lines

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:
- return an error for a different major
- return 1.1 for a version >= 1.1
- return 1.0 if 1.0 was requested

---
  xen/arch/arm/tee/ffa.c | 38 ++++++++++++++++++++++++++++++--------
  1 file changed, 30 insertions(+), 8 deletions(-)
xen/arch/arm/tee/ffa.c +0 -0
index 7ff2529b2055..1f602f25d097 100644

xen/arch/arm/tee/ffa.c static void handle_version(struct cpu_user_regs *regs) · line 144

Quoted 8 lines
xen/arch/arm/tee/ffa.c +1 -4
@@ -141,13 +141,24 @@ 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;
+    /**

Coding style: We are use a single '*' to start comment.

xen/arch/arm/tee/ffa.c static void handle_version(struct cpu_user_regs *regs) · line 155

Quoted 10 lines
xen/arch/arm/tee/ffa.c +10 -0
+     * As of now we only support 1.0 or 1.1.
+     * For any 1.x >= 1.1 return OK with 1.1
+     * For 1.0 return OK with 1.0
+     * For anything else return an error.
+     */
+    if ( (vers >> FFA_VERSION_MAJOR_SHIFT) == FFA_MY_VERSION_MAJOR )
+    {> +        if ( vers < FFA_VERSION_1_1 )
+            vers = FFA_VERSION_1_0;
+        else
+            vers = FFA_VERSION_1_1;

I feel the logic is fragile. The first ``if`` is generic and I think it would be easy to update the major version without updating handle_version(). To some extend, the same problem would happen with the minor version.

AFAICT, this is not a new issue, but as you touch the code, we should probably harden it. I could settle with a BUILD_BUG_ON() to catch any change of the minor/major.

Quoted 12 lines
xen/arch/arm/tee/ffa.c +5 -2
-    ctx->guest_vers = vers;
-    ffa_set_regs(regs, vers, 0, 0, 0, 0, 0, 0, 0);
+        ctx->guest_vers = vers;
+        ffa_set_regs(regs, vers, 0, 0, 0, 0, 0, 0, 0);
+    }
+    else
+        ffa_set_regs_error(regs, FFA_RET_NOT_SUPPORTED);
  }
 
  static void handle_msg_send_direct_req(struct cpu_user_regs *regs, uint32_t fid)

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

Quoted 6 lines
xen/arch/arm/tee/ffa.c +2 -1
@@ -530,7 +541,8 @@ static bool ffa_probe(void)
          goto err_no_fw;
      }
 
-    if ( vers < FFA_MIN_SPMC_VERSION || vers > FFA_MY_VERSION )
+    if ( vers < FFA_MIN_SPMC_VERSION ||
+              (vers >> FFA_VERSION_MAJOR_SHIFT) != FFA_MY_VERSION_MAJOR )

Coding style: the second line should be aligned with 'vers' rather than indented.

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

Quoted 9 lines
xen/arch/arm/tee/ffa.c +1 -1
      {
          printk(XENLOG_ERR "ffa: Incompatible version %#x found\n", vers);
          goto err_no_fw;
@@ -542,7 +554,17 @@ static bool ffa_probe(void)
      printk(XENLOG_INFO "ARM FF-A Firmware version %u.%u\n",
             major_vers, minor_vers);
 
-    ffa_fw_version = vers;
+    /**

Coding style: We start comment with /*.

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

Quoted 10 lines
xen/arch/arm/tee/ffa.c +10 -0
+     * 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 is rejected before.
+     */
+    if ( vers > FFA_MY_VERSION )
+        ffa_fw_version = FFA_MY_VERSION;
+    else
+        ffa_fw_version = vers;

Looking at the code after your series (didn't check before). We don't seem to use ffa_fw_version for other than checking that FFA was detected. So wouldn't it be better to stop storing the version?

Cheers,

Signature · 1 line
Julien Grall
#12 lore raw

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

Hi Julien,

Quoted 5 lines · Julien Grall

Hi Bertrand,

NIT Typo: s/fix/Fix/ to match the other title

Ack

Quoted 6 lines · Bertrand Marquis
Quoted 2 lines

Fix FFA version negotiation with the firmware to follow the specification guidance more closely.

To confirm, below is based on 13.2.1 in DEN0077A, is that correct? If so, can you add a link in the commit message (and maybe code).

Yes it and i will add a link and description to the commit message.

Quoted 28 lines
Quoted 28 lines

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:
- return an error for a different major
- return 1.1 for a version >= 1.1
- return 1.0 if 1.0 was requested

---
 xen/arch/arm/tee/ffa.c | 38 ++++++++++++++++++++++++++++++--------
 1 file changed, 30 insertions(+), 8 deletions(-)
xen/arch/arm/tee/ffa.c +0 -0
index 7ff2529b2055..1f602f25d097 100644
@@ -141,13 +141,24 @@ static void handle_version(struct cpu_user_regs *regs)
     struct ffa_ctx *ctx = d->arch.tee;
Quoted 8 lines

xen/arch/arm/tee/ffa.c static void handle_version(struct cpu_user_regs *regs) · line 144

Quoted 6 lines
xen/arch/arm/tee/ffa.c +1 -3
     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;
+    /**

Coding style: We are use a single '*' to start comment.

Ack

Quoted 12 lines

xen/arch/arm/tee/ffa.c static void handle_version(struct cpu_user_regs *regs) · line 155

Quoted 11 lines
xen/arch/arm/tee/ffa.c +10 -0
+     * As of now we only support 1.0 or 1.1.
+     * For any 1.x >= 1.1 return OK with 1.1
+     * For 1.0 return OK with 1.0
+     * For anything else return an error.
+     */
+    if ( (vers >> FFA_VERSION_MAJOR_SHIFT) == FFA_MY_VERSION_MAJOR )
+    {> +        if ( vers < FFA_VERSION_1_1 )
+            vers = FFA_VERSION_1_0;
+        else
+            vers = FFA_VERSION_1_1;
Quoted 1 line

I feel the logic is fragile. The first ``if`` is generic and I think it would be easy to update the major version without updating handle_version(). To some extend, the same problem would happen with the minor version.

so something like:
if (MAJOR(vers) == MY_MAJOR)
{
if (MINOR(vers) < MY_MIN || MINOR(vers)>MY_MIN)
vers = MY_VERSION
else
keep requested version
}

Quoted 2 lines

AFAICT, this is not a new issue, but as you touch the code, we should probably harden it. I could settle with a BUILD_BUG_ON() to catch any change of the minor/major.

i could see a BUILD_BUG_ON(MAJOR(MIN_VERSION) != MAJOR(MAX_VERSION)) Is that what you have in mind ?

Quoted 10 lines
Quoted 10 lines
xen/arch/arm/tee/ffa.c +5 -1
 -    ctx->guest_vers = vers;
-    ffa_set_regs(regs, vers, 0, 0, 0, 0, 0, 0, 0);
+        ctx->guest_vers = vers;
+        ffa_set_regs(regs, vers, 0, 0, 0, 0, 0, 0, 0);
+    }
+    else
+        ffa_set_regs_error(regs, FFA_RET_NOT_SUPPORTED);
 }
   static void handle_msg_send_direct_req(struct cpu_user_regs *regs, uint32_t fid)
Quoted 8 lines

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

Quoted 6 lines
xen/arch/arm/tee/ffa.c +2 -0
@@ -530,7 +541,8 @@ static bool ffa_probe(void)
         goto err_no_fw;
     }
 -    if ( vers < FFA_MIN_SPMC_VERSION || vers > FFA_MY_VERSION )
+    if ( vers < FFA_MIN_SPMC_VERSION ||
+              (vers >> FFA_VERSION_MAJOR_SHIFT) != FFA_MY_VERSION_MAJOR )

Coding style: the second line should be aligned with 'vers' rather than indented.

Ack

Quoted 11 lines

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

Quoted 9 lines
xen/arch/arm/tee/ffa.c +1 -0
     {
         printk(XENLOG_ERR "ffa: Incompatible version %#x found\n", vers);
         goto err_no_fw;
@@ -542,7 +554,17 @@ static bool ffa_probe(void)
     printk(XENLOG_INFO "ARM FF-A Firmware version %u.%u\n",
            major_vers, minor_vers);
 -    ffa_fw_version = vers;
+    /**

Coding style: We start comment with /*.

Ack

Quoted 12 lines

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

Quoted 11 lines
xen/arch/arm/tee/ffa.c +10 -0
+     * 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 is rejected before.
+     */
+    if ( vers > FFA_MY_VERSION )
+        ffa_fw_version = FFA_MY_VERSION;
+    else
+        ffa_fw_version = vers;
Quoted 1 line

Looking at the code after your series (didn't check before). We don't seem to use ffa_fw_version for other than checking that FFA was detected. So wouldn't it be better to stop storing the version?

We are only supporting a firmware version with 1.1 at the moment but when we will add support for FFA version 1.2 in the next weeks this will not be true anymore so if this is ok with you i would rather keep it.

Cheers
Bertrand

Quoted 5 lines

Cheers,

Signature · 1 line
Julien Grall
#13 lore raw

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

Quoted 1 line · Bertrand Marquis

Hi Julien,

Hi Bertrand,

Quoted 80 lines
Quoted 5 lines · Julien Grall

Hi Bertrand,

NIT Typo: s/fix/Fix/ to match the other title

Ack

Quoted 6 lines · Bertrand Marquis
Quoted 2 lines

Fix FFA version negotiation with the firmware to follow the specification guidance more closely.

To confirm, below is based on 13.2.1 in DEN0077A, is that correct? If so, can you add a link in the commit message (and maybe code).

Yes it and i will add a link and description to the commit message.

Quoted 28 lines
Quoted 28 lines

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:
- return an error for a different major
- return 1.1 for a version >= 1.1
- return 1.0 if 1.0 was requested

---
  xen/arch/arm/tee/ffa.c | 38 ++++++++++++++++++++++++++++++--------
  1 file changed, 30 insertions(+), 8 deletions(-)
xen/arch/arm/tee/ffa.c +0 -0
index 7ff2529b2055..1f602f25d097 100644
@@ -141,13 +141,24 @@ static void handle_version(struct cpu_user_regs *regs)
      struct ffa_ctx *ctx = d->arch.tee;
Quoted 8 lines

xen/arch/arm/tee/ffa.c static void handle_version(struct cpu_user_regs *regs) · line 144

Quoted 6 lines
xen/arch/arm/tee/ffa.c +1 -3
      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;
+    /**

Coding style: We are use a single '*' to start comment.

Ack

Quoted 12 lines

xen/arch/arm/tee/ffa.c static void handle_version(struct cpu_user_regs *regs) · line 155

Quoted 11 lines
xen/arch/arm/tee/ffa.c +10 -0
+     * As of now we only support 1.0 or 1.1.
+     * For any 1.x >= 1.1 return OK with 1.1
+     * For 1.0 return OK with 1.0
+     * For anything else return an error.
+     */
+    if ( (vers >> FFA_VERSION_MAJOR_SHIFT) == FFA_MY_VERSION_MAJOR )
+    {> +        if ( vers < FFA_VERSION_1_1 )
+            vers = FFA_VERSION_1_0;
+        else
+            vers = FFA_VERSION_1_1;
Quoted 1 line

I feel the logic is fragile. The first ``if`` is generic and I think it would be easy to update the major version without updating handle_version(). To some extend, the same problem would happen with the minor version.

so something like:
if (MAJOR(vers) == MY_MAJOR)
{
if (MINOR(vers) < MY_MIN || MINOR(vers)>MY_MIN)
vers = MY_VERSION
else
keep requested version
}

Quoted 1 line
Quoted 4 lines
Quoted 1 line

AFAICT, this is not a new issue, but as you touch the code, we should probably harden it. I could settle with a BUILD_BUG_ON() to catch any change of the minor/major.

i could see a BUILD_BUG_ON(MAJOR(MIN_VERSION) != MAJOR(MAX_VERSION)) Is that what you have in mind ?

I had in mind to check specifically that FFA_MY_VERSION_{MAJOR, MINOR} were both 1. But I think your proposal is better.

Quoted 48 lines
Quoted 11 lines
Quoted 11 lines
xen/arch/arm/tee/ffa.c +5 -1
  -    ctx->guest_vers = vers;
-    ffa_set_regs(regs, vers, 0, 0, 0, 0, 0, 0, 0);
+        ctx->guest_vers = vers;
+        ffa_set_regs(regs, vers, 0, 0, 0, 0, 0, 0, 0);
+    }
+    else
+        ffa_set_regs_error(regs, FFA_RET_NOT_SUPPORTED);
  }
    static void handle_msg_send_direct_req(struct cpu_user_regs *regs, uint32_t fid)
Quoted 8 lines

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

Quoted 6 lines
xen/arch/arm/tee/ffa.c +2 -0
@@ -530,7 +541,8 @@ static bool ffa_probe(void)
          goto err_no_fw;
      }
  -    if ( vers < FFA_MIN_SPMC_VERSION || vers > FFA_MY_VERSION )
+    if ( vers < FFA_MIN_SPMC_VERSION ||
+              (vers >> FFA_VERSION_MAJOR_SHIFT) != FFA_MY_VERSION_MAJOR )

Coding style: the second line should be aligned with 'vers' rather than indented.

Ack

Quoted 11 lines

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

Quoted 9 lines
xen/arch/arm/tee/ffa.c +1 -0
      {
          printk(XENLOG_ERR "ffa: Incompatible version %#x found\n", vers);
          goto err_no_fw;
@@ -542,7 +554,17 @@ static bool ffa_probe(void)
      printk(XENLOG_INFO "ARM FF-A Firmware version %u.%u\n",
             major_vers, minor_vers);
  -    ffa_fw_version = vers;
+    /**

Coding style: We start comment with /*.

Ack

Quoted 12 lines
Quoted 11 lines
xen/arch/arm/tee/ffa.c +10 -0
+     * 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 is rejected before.
+     */
+    if ( vers > FFA_MY_VERSION )
+        ffa_fw_version = FFA_MY_VERSION;
+    else
+        ffa_fw_version = vers;
Quoted 3 lines
Quoted 1 line

Looking at the code after your series (didn't check before). We don't seem to use ffa_fw_version for other than checking that FFA was detected. So wouldn't it be better to stop storing the version?

We are only supporting a firmware version with 1.1 at the moment but when we will add support for FFA version 1.2 in the next weeks this will not be true anymore so if this is ok with you i would rather keep it.

I am fine to keep ffa_fw_version as-is given there is a future use.

Cheers,

Signature · 1 line
Julien Grall
#14 lore raw

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

Hi Julien,

Quoted 82 lines · Julien Grall
Quoted 1 line · Bertrand Marquis

Hi Julien,

Hi Bertrand,

Quoted 72 lines
Quoted 5 lines · Julien Grall

Hi Bertrand,

NIT Typo: s/fix/Fix/ to match the other title

Ack

Quoted 6 lines · Bertrand Marquis
Quoted 2 lines

Fix FFA version negotiation with the firmware to follow the specification guidance more closely.

To confirm, below is based on 13.2.1 in DEN0077A, is that correct? If so, can you add a link in the commit message (and maybe code).

Yes it and i will add a link and description to the commit message.

Quoted 28 lines
Quoted 28 lines

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:
- return an error for a different major
- return 1.1 for a version >= 1.1
- return 1.0 if 1.0 was requested

---
 xen/arch/arm/tee/ffa.c | 38 ++++++++++++++++++++++++++++++--------
 1 file changed, 30 insertions(+), 8 deletions(-)
xen/arch/arm/tee/ffa.c +0 -0
index 7ff2529b2055..1f602f25d097 100644
@@ -141,13 +141,24 @@ static void handle_version(struct cpu_user_regs *regs)
     struct ffa_ctx *ctx = d->arch.tee;
Quoted 8 lines

xen/arch/arm/tee/ffa.c static void handle_version(struct cpu_user_regs *regs) · line 144

Quoted 6 lines
xen/arch/arm/tee/ffa.c +1 -3
     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;
+    /**

Coding style: We are use a single '*' to start comment.

Ack

Quoted 12 lines

xen/arch/arm/tee/ffa.c static void handle_version(struct cpu_user_regs *regs) · line 155

Quoted 11 lines
xen/arch/arm/tee/ffa.c +10 -0
+     * As of now we only support 1.0 or 1.1.
+     * For any 1.x >= 1.1 return OK with 1.1
+     * For 1.0 return OK with 1.0
+     * For anything else return an error.
+     */
+    if ( (vers >> FFA_VERSION_MAJOR_SHIFT) == FFA_MY_VERSION_MAJOR )
+    {> +        if ( vers < FFA_VERSION_1_1 )
+            vers = FFA_VERSION_1_0;
+        else
+            vers = FFA_VERSION_1_1;
Quoted 1 line

I feel the logic is fragile. The first ``if`` is generic and I think it would be easy to update the major version without updating handle_version(). To some extend, the same problem would happen with the minor version.

so something like:
if (MAJOR(vers) == MY_MAJOR)

{
   if (MINOR(vers) < MY_MIN || MINOR(vers)>MY_MIN)

vers = MY_VERSION
else
keep requested version
}

Quoted 1 line
Quoted 5 lines
Quoted 3 lines
Quoted 1 line

AFAICT, this is not a new issue, but as you touch the code, we should probably harden it. I could settle with a BUILD_BUG_ON() to catch any change of the minor/major.

i could see a BUILD_BUG_ON(MAJOR(MIN_VERSION) != MAJOR(MAX_VERSION)) Is that what you have in mind ?

I had in mind to check specifically that FFA_MY_VERSION_{MAJOR, MINOR} were both 1. But I think your proposal is better.

Ok.

Quoted 44 lines
Quoted 44 lines
Quoted 11 lines
Quoted 11 lines
xen/arch/arm/tee/ffa.c +5 -1
 -    ctx->guest_vers = vers;
-    ffa_set_regs(regs, vers, 0, 0, 0, 0, 0, 0, 0);
+        ctx->guest_vers = vers;
+        ffa_set_regs(regs, vers, 0, 0, 0, 0, 0, 0, 0);
+    }
+    else
+        ffa_set_regs_error(regs, FFA_RET_NOT_SUPPORTED);
 }
   static void handle_msg_send_direct_req(struct cpu_user_regs *regs, uint32_t fid)
Quoted 8 lines

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

Quoted 6 lines
xen/arch/arm/tee/ffa.c +2 -0
@@ -530,7 +541,8 @@ static bool ffa_probe(void)
         goto err_no_fw;
     }
 -    if ( vers < FFA_MIN_SPMC_VERSION || vers > FFA_MY_VERSION )
+    if ( vers < FFA_MIN_SPMC_VERSION ||
+              (vers >> FFA_VERSION_MAJOR_SHIFT) != FFA_MY_VERSION_MAJOR )

Coding style: the second line should be aligned with 'vers' rather than indented.

Ack

Quoted 11 lines

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

Quoted 9 lines
xen/arch/arm/tee/ffa.c +1 -0
     {
         printk(XENLOG_ERR "ffa: Incompatible version %#x found\n", vers);
         goto err_no_fw;
@@ -542,7 +554,17 @@ static bool ffa_probe(void)
     printk(XENLOG_INFO "ARM FF-A Firmware version %u.%u\n",
            major_vers, minor_vers);
 -    ffa_fw_version = vers;
+    /**

Coding style: We start comment with /*.

Ack

Quoted 12 lines
Quoted 11 lines
xen/arch/arm/tee/ffa.c +10 -0
+     * 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 is rejected before.
+     */
+    if ( vers > FFA_MY_VERSION )
+        ffa_fw_version = FFA_MY_VERSION;
+    else
+        ffa_fw_version = vers;
Quoted 4 lines
Quoted 2 lines
Quoted 1 line

Looking at the code after your series (didn't check before). We don't seem to use ffa_fw_version for other than checking that FFA was detected. So wouldn't it be better to stop storing the version?

We are only supporting a firmware version with 1.1 at the moment but when we will add support for FFA version 1.2 in the next weeks this will not be true anymore so if this is ok with you i would rather keep it.

I am fine to keep ffa_fw_version as-is given there is a future use.

Good thanks.

Cheers
Bertrand

Quoted 5 lines

Cheers,

Signature · 1 line
Julien Grall
#15 lore raw

PATCH 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 done to the firmware.

With this enabled. allow FF-A support to be activated for guest even if the firmware does not support it.

As a consequence, if the firmware is not there or not supported, we return an empty list of partitions to VMs requesting it through PARTINFO_GET ABI.

---
 xen/arch/arm/tee/ffa.c          | 31 ++++++++++++++++++++-----------
 xen/arch/arm/tee/ffa_notif.c    |  7 +++++++
 xen/arch/arm/tee/ffa_partinfo.c | 31 +++++++++++++++++++++++++++++--
 xen/arch/arm/tee/ffa_private.h  | 28 ++++++++++++++++++++++++++++
 xen/arch/arm/tee/ffa_rxtx.c     | 13 ++++++-------
 xen/arch/arm/tee/ffa_shm.c      | 12 ++++++++++++
 6 files changed, 102 insertions(+), 20 deletions(-)
xen/arch/arm/tee/ffa.c +20 -11
index 1f602f25d097..53960b146220 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_feat_supported, FEAT_FUNC_BITMAP_SIZE);
 
 /* List of ABI we use from the firmware */
 static const uint32_t ffa_fw_feat_needed[] = {
@@ -174,6 +177,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) )
     {
@@ -387,8 +397,6 @@ static int ffa_domain_init(struct domain *d)
     struct ffa_ctx *ctx;
     int ret;
 
-    if ( !ffa_fw_version )
-        return -ENODEV;
      /*
       * We can't use that last possible domain ID or ffa_get_vm_id() would
       * cause an overflow.
@@ -523,6 +531,9 @@ static bool ffa_probe(void)
     printk(XENLOG_INFO "ARM FF-A Mediator version %u.%u\n",
            FFA_MY_VERSION_MAJOR, FFA_MY_VERSION_MINOR);
 
+    INIT_LIST_HEAD(&ffa_teardown_head);
+    init_timer(&ffa_teardown_timer, ffa_teardown_timer_callback, NULL, 0);
+
     /*
      * psci_init_smccc() updates this value with what's reported by EL-3
      * or secure world.
@@ -568,12 +579,12 @@ static bool ffa_probe(void)
 
     for ( int i = 0; i < ARRAY_SIZE(ffa_fw_feat_needed); i++ )
     {
-        if ( !ffa_feature_supported(ffa_fw_feat_needed[i]) )
-        {
+        if ( ffa_feature_supported(ffa_fw_feat_needed[i]) )
+            set_bit(FEAT_FUNC_BITNUM(ffa_fw_feat_needed[i]),
+                    ffa_fw_feat_supported);
+        else
             printk(XENLOG_INFO "ARM FF-A Firmware does not support 0x%08x\n",
-                   ffa_fw_feat_needed[i]);
-            goto err_no_fw;
-        }
+                       ffa_fw_feat_needed[i]);
     }
 
     if ( !ffa_rxtx_init() )
@@ -586,8 +597,6 @@ static bool ffa_probe(void)
         goto err_rxtx_destroy;
 
     ffa_notif_init();
-    INIT_LIST_HEAD(&ffa_teardown_head);
-    init_timer(&ffa_teardown_timer, ffa_teardown_timer_callback, NULL, 0);
 
     return true;
 
@@ -597,7 +606,7 @@ err_no_fw:
     ffa_fw_version = 0;
     printk(XENLOG_INFO "ARM FF-A No firmware support\n");
 
-    return false;
+    return true;
 }
 
 static const struct tee_mediator_ops ffa_ops =
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 -2
index 93a03c6bc672..a42bd92ab8cf 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 )
     {
@@ -267,7 +292,6 @@ bool ffa_partinfo_init(void)
 
 out:
     ffa_rx_release();
-
     return ret;
 }
 
@@ -313,6 +337,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 +28 -0
index 7c6b06f686fc..d4dc9c8cd67b 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
@@ -238,6 +239,23 @@
 #define FFA_NOTIFICATION_INFO_GET_32    0x84000083U
 #define FFA_NOTIFICATION_INFO_GET_64    0xC4000083U
 
+/**
+ * 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_FUNC_MIN           FFA_ERROR
+#define FFA_FUNC_MAX           FFA_NOTIFICATION_INFO_GET_64
+#define FFA_FUNC_ID(id)        ((id) & ARM_SMCCC_FUNC_MASK)
+#define FFA_FUNC_CONV(id)      (((id) >> ARM_SMCCC_CONV_SHIFT) & BIT(0,U))
+
+#define FEAT_FUNC_BITMAP_SIZE   (2 * (FFA_FUNC_ID(FFA_FUNC_MAX) - \
+                                    FFA_FUNC_ID(FFA_FUNC_MIN) + 1))
+#define FEAT_FUNC_BITNUM(id)    ((FFA_FUNC_ID(id) - \
+                                    FFA_FUNC_ID(FFA_FUNC_MIN)) << 1 | \
+                                    FFA_FUNC_CONV(id))
+
 struct ffa_ctx_notif {
     bool enabled;
 
@@ -286,6 +304,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_feat_supported, FEAT_FUNC_BITMAP_SIZE);
 
 bool ffa_shm_domain_destroy(struct domain *d);
 void ffa_handle_mem_share(struct cpu_user_regs *regs);
@@ -398,4 +418,12 @@ 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)
+{
+    if ( ffa_fw_version == 0 )
+        return false;
+    else
+        return test_bit(FEAT_FUNC_BITNUM(fid), ffa_fw_feat_supported);
+}
+
 #endif /*__FFA_PRIVATE_H__*/
xen/arch/arm/tee/ffa_rxtx.c +6 -7
index 661764052e67..cb54c76911fd 100644
@@ -193,24 +193,23 @@ 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;
 
     ffa_tx = alloc_xenheap_pages(get_order_from_pages(FFA_RXTX_PAGE_COUNT), 0);
     if ( !ffa_tx )
-        goto err;
+        return false;
 
     e = ffa_rxtx_map(__pa(ffa_tx), __pa(ffa_rx), FFA_RXTX_PAGE_COUNT);
     if ( e )
     {
         printk(XENLOG_ERR "ffa: Failed to map rxtx: error %d\n", e);
-        goto err;
+        return false;
     }
     return true;
-
-err:
-    ffa_rxtx_destroy();
-
-    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.39.5 (Apple Git-154)
#16 lore raw

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

Hi Bertrand,

Quoted 4 lines · Bertrand Marquis

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

With this enabled. allow FF-A support to be activated for guest even if

Typo: s/./,/ I think.

Quoted 1 line

the firmware does not support it.

Can you explain why you want to do this?

The TEE mediator was designed to interpose with the HW. Without the HW it doesn't entirely make sense to try to use it.

It would also not work if the host system is using OP-TEE and expose to some VM FF-A. So it feels that the mediator may not be the right place to handle this case.

Quoted 91 lines

As a consequence, if the firmware is not there or not supported, we return an empty list of partitions to VMs requesting it through PARTINFO_GET ABI.

---
  xen/arch/arm/tee/ffa.c          | 31 ++++++++++++++++++++-----------
  xen/arch/arm/tee/ffa_notif.c    |  7 +++++++
  xen/arch/arm/tee/ffa_partinfo.c | 31 +++++++++++++++++++++++++++++--
  xen/arch/arm/tee/ffa_private.h  | 28 ++++++++++++++++++++++++++++
  xen/arch/arm/tee/ffa_rxtx.c     | 13 ++++++-------
  xen/arch/arm/tee/ffa_shm.c      | 12 ++++++++++++
  6 files changed, 102 insertions(+), 20 deletions(-)
xen/arch/arm/tee/ffa.c +19 -10
index 1f602f25d097..53960b146220 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_feat_supported, FEAT_FUNC_BITMAP_SIZE);
 
  /* List of ABI we use from the firmware */
  static const uint32_t ffa_fw_feat_needed[] = {
@@ -174,6 +177,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) )
      {
@@ -387,8 +397,6 @@ static int ffa_domain_init(struct domain *d)
      struct ffa_ctx *ctx;
      int ret;
 
-    if ( !ffa_fw_version )
-        return -ENODEV;
       /*>         * We can't use that last possible domain ID or ffa_get_vm_id() would
        * cause an overflow.
@@ -523,6 +531,9 @@ static bool ffa_probe(void)
      printk(XENLOG_INFO "ARM FF-A Mediator version %u.%u\n",
             FFA_MY_VERSION_MAJOR, FFA_MY_VERSION_MINOR);
 
+    INIT_LIST_HEAD(&ffa_teardown_head);
+    init_timer(&ffa_teardown_timer, ffa_teardown_timer_callback, NULL, 0);
+
      /*
       * psci_init_smccc() updates this value with what's reported by EL-3
       * or secure world.
@@ -568,12 +579,12 @@ static bool ffa_probe(void)
 
      for ( int i = 0; i < ARRAY_SIZE(ffa_fw_feat_needed); i++ )
      {
-        if ( !ffa_feature_supported(ffa_fw_feat_needed[i]) )
-        {
+        if ( ffa_feature_supported(ffa_fw_feat_needed[i]) )
+            set_bit(FEAT_FUNC_BITNUM(ffa_fw_feat_needed[i]),
+                    ffa_fw_feat_supported);
+        else
              printk(XENLOG_INFO "ARM FF-A Firmware does not support 0x%08x\n",
-                   ffa_fw_feat_needed[i]);
-            goto err_no_fw;
-        }
+                       ffa_fw_feat_needed[i]);
      }
 
      if ( !ffa_rxtx_init() )
@@ -586,8 +597,6 @@ static bool ffa_probe(void)
          goto err_rxtx_destroy;
 
      ffa_notif_init();
-    INIT_LIST_HEAD(&ffa_teardown_head);
-    init_timer(&ffa_teardown_timer, ffa_teardown_timer_callback, NULL, 0);
 
      return true;
 

xen/arch/arm/tee/ffa.c err_no_fw: · line 609

Quoted 5 lines
xen/arch/arm/tee/ffa.c +1 -1
@@ -597,7 +606,7 @@ err_no_fw:
      ffa_fw_version = 0;
      printk(XENLOG_INFO "ARM FF-A No firmware support\n");
 
-    return false;
+    return true;

So effectively now ffa_probe() will always return true. If we end up to probe FF-A before OP-Tee, then we would always say "FF-A" is the TEE mediator.

Quoted 74 lines
xen/arch/arm/tee/ffa.c +0 -0
  }
 
  static const struct tee_mediator_ops ffa_ops =
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 +26 -1
index 93a03c6bc672..a42bd92ab8cf 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 )
      {

xen/arch/arm/tee/ffa_partinfo.c bool ffa_partinfo_init(void) · line 270

Quoted 4 lines
xen/arch/arm/tee/ffa_partinfo.c +0 -1
@@ -267,7 +292,6 @@ bool ffa_partinfo_init(void)
 
  out:
      ffa_rx_release();
-

Spurious change?

Quoted 27 lines
xen/arch/arm/tee/ffa_partinfo.c +3 -0
      return ret;
  }
 
@@ -313,6 +337,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 +1 -0
index 7c6b06f686fc..d4dc9c8cd67b 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
@@ -238,6 +239,23 @@
  #define FFA_NOTIFICATION_INFO_GET_32    0x84000083U

xen/arch/arm/tee/ffa_private.h line 249

Quoted 10 lines
xen/arch/arm/tee/ffa_private.h +8 -0
  #define FFA_NOTIFICATION_INFO_GET_64    0xC4000083U
 
+/**
+ * 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_FUNC_MIN           FFA_ERROR
+#define FFA_FUNC_MAX           FFA_NOTIFICATION_INFO_GET_64

These two defines confused me because FAA_ERROR is 0x84000060U and FFA_NOTIFICATION_INFO_GET_64 is 0xC4000083U. I think it would be better if we define them using FFA_FUNC_ID.

We also probably want to have a compiler time check that FFA_FUNC_MIN is < FFA_FUNC_MAX.

xen/arch/arm/tee/ffa_private.h line 257

Quoted 8 lines
xen/arch/arm/tee/ffa_private.h +8 -0
+#define FFA_FUNC_ID(id)        ((id) & ARM_SMCCC_FUNC_MASK)
+#define FFA_FUNC_CONV(id)      (((id) >> ARM_SMCCC_CONV_SHIFT) & BIT(0,U))
+
+#define FEAT_FUNC_BITMAP_SIZE   (2 * (FFA_FUNC_ID(FFA_FUNC_MAX) - \
+                                    FFA_FUNC_ID(FFA_FUNC_MIN) + 1))
+#define FEAT_FUNC_BITNUM(id)    ((FFA_FUNC_ID(id) - \
+                                    FFA_FUNC_ID(FFA_FUNC_MIN)) << 1 | \
+                                    FFA_FUNC_CONV(id))

The code seem to make two assumptions:
1. id is a constant
2. id is always valid

I think it would be good to have a BUILD_BUG_ON(). This should avoid the two assumptions.

Quoted 14 lines
xen/arch/arm/tee/ffa_private.h +3 -0
+
  struct ffa_ctx_notif {
      bool enabled;
 
@@ -286,6 +304,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_feat_supported, FEAT_FUNC_BITMAP_SIZE);
 
  bool ffa_shm_domain_destroy(struct domain *d);
  void ffa_handle_mem_share(struct cpu_user_regs *regs);

xen/arch/arm/tee/ffa_private.h static inline int32_t ffa_rx_release(void) · line 424

Quoted 7 lines
xen/arch/arm/tee/ffa_private.h +4 -0
@@ -398,4 +418,12 @@ 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)
+{
+    if ( ffa_fw_version == 0 )
+        return false;

You could avoid this check if you ensure that ...

xen/arch/arm/tee/ffa_private.h static inline int32_t ffa_rx_release(void) · line 425

Quoted 1 line
xen/arch/arm/tee/ffa_private.h +1 -0
+    else> +        return test_bit(FEAT_FUNC_BITNUM(fid), ffa_fw_feat_supported);

.. the bitmap is always zeroed if ffa_fw_version is 0. You also want to check that the fid is valid (could be done at build time if fid is always a constant).

Quoted 25 lines
xen/arch/arm/tee/ffa_private.h +2 -0
+}
+
  #endif /*__FFA_PRIVATE_H__*/
xen/arch/arm/tee/ffa_rxtx.c +5 -1
index 661764052e67..cb54c76911fd 100644
@@ -193,24 +193,23 @@ 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;
 
      ffa_tx = alloc_xenheap_pages(get_order_from_pages(FFA_RXTX_PAGE_COUNT), 0);
      if ( !ffa_tx )
-        goto err;
+        return false;
 
      e = ffa_rxtx_map(__pa(ffa_tx), __pa(ffa_rx), FFA_RXTX_PAGE_COUNT);

xen/arch/arm/tee/ffa_rxtx.c bool ffa_rxtx_init(void) · line 213

Quoted 10 lines
xen/arch/arm/tee/ffa_rxtx.c +1 -4
      if ( e )
      {
          printk(XENLOG_ERR "ffa: Failed to map rxtx: error %d\n", e);
-        goto err;
+        return false;
      }
      return true;
-
-err:
-    ffa_rxtx_destroy();

This seems to be unrelated to the change. Can you explain why we don't need to call ffa_rxtx_destroy()?

Quoted 30 lines
xen/arch/arm/tee/ffa_rxtx.c +0 -2
-
-    return false;
  }> diff --git a/xen/arch/arm/tee/ffa_shm.c b/xen/arch/arm/tee/ffa_shm.c

index 370d83ec5cf8..efa5b67db8e1 100644

xen/arch/arm/tee/ffa_shm.c +9 -0
@@ -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.

xen/arch/arm/tee/ffa_shm.c int ffa_handle_mem_reclaim(uint64_t handle, uint32_t flags) · line 638

Quoted 9 lines
xen/arch/arm/tee/ffa_shm.c +3 -0
@@ -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 )

Cheers,

Signature · 1 line
Julien Grall
#17 lore raw

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

Hi,

Quoted 13 lines · Julien Grall

Hi Bertrand,

Quoted 4 lines · Bertrand Marquis

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

With this enabled. allow FF-A support to be activated for guest even if

Typo: s/./,/ I think.

Quoted 1 line

the firmware does not support it.

Quoted 8 lines

Can you explain why you want to do this?

The TEE mediator was designed to interpose with the HW. Without the HW it doesn't entirely make sense to try to use it.

It would also not work if the host system is using OP-TEE and expose to some VM FF-A. So it feels that the mediator may not be the right place to handle this case.

That's a good point, but all the FF-A handling should be in the same module since there's bound to be code and state to share. The problem is that FF-A tries to be a TEE mediator while it's about to become more than that. We can work around it by probing the OP-TEE mediator first, but it's adding another exception or special case. Would it make sense to move the FF-A mediator out of the TEE mediator framework and establish it separately?

Cheers,
Jens

Quoted 361 lines
Quoted 47 lines

As a consequence, if the firmware is not there or not supported, we return an empty list of partitions to VMs requesting it through PARTINFO_GET ABI.

---
  xen/arch/arm/tee/ffa.c          | 31 ++++++++++++++++++++-----------
  xen/arch/arm/tee/ffa_notif.c    |  7 +++++++
  xen/arch/arm/tee/ffa_partinfo.c | 31 +++++++++++++++++++++++++++++--
  xen/arch/arm/tee/ffa_private.h  | 28 ++++++++++++++++++++++++++++
  xen/arch/arm/tee/ffa_rxtx.c     | 13 ++++++-------
  xen/arch/arm/tee/ffa_shm.c      | 12 ++++++++++++
  6 files changed, 102 insertions(+), 20 deletions(-)
xen/arch/arm/tee/ffa.c +11 -1
index 1f602f25d097..53960b146220 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_feat_supported, FEAT_FUNC_BITMAP_SIZE);
 
  /* List of ABI we use from the firmware */
  static const uint32_t ffa_fw_feat_needed[] = {
@@ -174,6 +177,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) )
      {

xen/arch/arm/tee/ffa.c static int ffa_domain_init(struct domain *d) · line 400

Quoted 6 lines
xen/arch/arm/tee/ffa.c +0 -2
@@ -387,8 +397,6 @@ static int ffa_domain_init(struct domain *d)
      struct ffa_ctx *ctx;
      int ret;
 
-    if ( !ffa_fw_version )
-        return -ENODEV;
       /*>         * We can't use that last possible domain ID or

ffa_get_vm_id() would

Quoted 39 lines
xen/arch/arm/tee/ffa.c +8 -7
        * cause an overflow.
@@ -523,6 +531,9 @@ static bool ffa_probe(void)
      printk(XENLOG_INFO "ARM FF-A Mediator version %u.%u\n",
             FFA_MY_VERSION_MAJOR, FFA_MY_VERSION_MINOR);
 
+    INIT_LIST_HEAD(&ffa_teardown_head);
+    init_timer(&ffa_teardown_timer, ffa_teardown_timer_callback, NULL, 0);
+
      /*
       * psci_init_smccc() updates this value with what's reported by EL-3
       * or secure world.
@@ -568,12 +579,12 @@ static bool ffa_probe(void)
 
      for ( int i = 0; i < ARRAY_SIZE(ffa_fw_feat_needed); i++ )
      {
-        if ( !ffa_feature_supported(ffa_fw_feat_needed[i]) )
-        {
+        if ( ffa_feature_supported(ffa_fw_feat_needed[i]) )
+            set_bit(FEAT_FUNC_BITNUM(ffa_fw_feat_needed[i]),
+                    ffa_fw_feat_supported);
+        else
              printk(XENLOG_INFO "ARM FF-A Firmware does not support 0x%08x\n",
-                   ffa_fw_feat_needed[i]);
-            goto err_no_fw;
-        }
+                       ffa_fw_feat_needed[i]);
      }
 
      if ( !ffa_rxtx_init() )
@@ -586,8 +597,6 @@ static bool ffa_probe(void)
          goto err_rxtx_destroy;
 
      ffa_notif_init();
-    INIT_LIST_HEAD(&ffa_teardown_head);
-    init_timer(&ffa_teardown_timer, ffa_teardown_timer_callback, NULL, 0);
 
      return true;
 

xen/arch/arm/tee/ffa.c err_no_fw: · line 609

Quoted 5 lines
xen/arch/arm/tee/ffa.c +1 -1
@@ -597,7 +606,7 @@ err_no_fw:
      ffa_fw_version = 0;
      printk(XENLOG_INFO "ARM FF-A No firmware support\n");
 
-    return false;
+    return true;

So effectively now ffa_probe() will always return true. If we end up to probe FF-A before OP-Tee, then we would always say "FF-A" is the TEE mediator.

Quoted 74 lines
xen/arch/arm/tee/ffa.c +0 -0
  }
 
  static const struct tee_mediator_ops ffa_ops =
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 +26 -1
index 93a03c6bc672..a42bd92ab8cf 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 )
      {

xen/arch/arm/tee/ffa_partinfo.c bool ffa_partinfo_init(void) · line 270

Quoted 4 lines
xen/arch/arm/tee/ffa_partinfo.c +0 -1
@@ -267,7 +292,6 @@ bool ffa_partinfo_init(void)
 
  out:
      ffa_rx_release();
-

Spurious change?

Quoted 27 lines
xen/arch/arm/tee/ffa_partinfo.c +3 -0
      return ret;
  }
 
@@ -313,6 +337,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 +1 -0
index 7c6b06f686fc..d4dc9c8cd67b 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
@@ -238,6 +239,23 @@
  #define FFA_NOTIFICATION_INFO_GET_32    0x84000083U

xen/arch/arm/tee/ffa_private.h line 249

Quoted 10 lines
xen/arch/arm/tee/ffa_private.h +8 -0
  #define FFA_NOTIFICATION_INFO_GET_64    0xC4000083U
 
+/**
+ * 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_FUNC_MIN           FFA_ERROR
+#define FFA_FUNC_MAX           FFA_NOTIFICATION_INFO_GET_64

These two defines confused me because FAA_ERROR is 0x84000060U and FFA_NOTIFICATION_INFO_GET_64 is 0xC4000083U. I think it would be better if we define them using FFA_FUNC_ID.

We also probably want to have a compiler time check that FFA_FUNC_MIN is < FFA_FUNC_MAX.

xen/arch/arm/tee/ffa_private.h line 257

Quoted 8 lines
xen/arch/arm/tee/ffa_private.h +8 -0
+#define FFA_FUNC_ID(id)        ((id) & ARM_SMCCC_FUNC_MASK)
+#define FFA_FUNC_CONV(id)      (((id) >> ARM_SMCCC_CONV_SHIFT) & BIT(0,U))
+
+#define FEAT_FUNC_BITMAP_SIZE   (2 * (FFA_FUNC_ID(FFA_FUNC_MAX) - \
+                                    FFA_FUNC_ID(FFA_FUNC_MIN) + 1))
+#define FEAT_FUNC_BITNUM(id)    ((FFA_FUNC_ID(id) - \
+                                    FFA_FUNC_ID(FFA_FUNC_MIN)) << 1 | \
+                                    FFA_FUNC_CONV(id))

The code seem to make two assumptions:
1. id is a constant
2. id is always valid

I think it would be good to have a BUILD_BUG_ON(). This should avoid the two assumptions.

Quoted 14 lines
xen/arch/arm/tee/ffa_private.h +3 -0
+
  struct ffa_ctx_notif {
      bool enabled;
 
@@ -286,6 +304,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_feat_supported, FEAT_FUNC_BITMAP_SIZE);
 
  bool ffa_shm_domain_destroy(struct domain *d);
  void ffa_handle_mem_share(struct cpu_user_regs *regs);

xen/arch/arm/tee/ffa_private.h static inline int32_t ffa_rx_release(void) · line 424

Quoted 7 lines
xen/arch/arm/tee/ffa_private.h +4 -0
@@ -398,4 +418,12 @@ 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)
+{
+    if ( ffa_fw_version == 0 )
+        return false;

You could avoid this check if you ensure that ...

xen/arch/arm/tee/ffa_private.h static inline int32_t ffa_rx_release(void) · line 425

Quoted 1 line
xen/arch/arm/tee/ffa_private.h +1 -0
+    else> +        return test_bit(FEAT_FUNC_BITNUM(fid),

ffa_fw_feat_supported);

.. the bitmap is always zeroed if ffa_fw_version is 0. You also want to check that the fid is valid (could be done at build time if fid is always a constant).

Quoted 25 lines
xen/arch/arm/tee/ffa_private.h +2 -0
+}
+
  #endif /*__FFA_PRIVATE_H__*/
xen/arch/arm/tee/ffa_rxtx.c +5 -1
index 661764052e67..cb54c76911fd 100644
@@ -193,24 +193,23 @@ 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;
 
      ffa_tx = alloc_xenheap_pages(get_order_from_pages(FFA_RXTX_PAGE_COUNT), 0);
      if ( !ffa_tx )
-        goto err;
+        return false;
 
      e = ffa_rxtx_map(__pa(ffa_tx), __pa(ffa_rx), FFA_RXTX_PAGE_COUNT);

xen/arch/arm/tee/ffa_rxtx.c bool ffa_rxtx_init(void) · line 213

Quoted 10 lines
xen/arch/arm/tee/ffa_rxtx.c +1 -4
      if ( e )
      {
          printk(XENLOG_ERR "ffa: Failed to map rxtx: error %d\n", e);
-        goto err;
+        return false;
      }
      return true;
-
-err:
-    ffa_rxtx_destroy();

This seems to be unrelated to the change. Can you explain why we don't need to call ffa_rxtx_destroy()?

Quoted 30 lines
xen/arch/arm/tee/ffa_rxtx.c +0 -2
-
-    return false;
  }> diff --git a/xen/arch/arm/tee/ffa_shm.c b/xen/arch/arm/tee/ffa_shm.c

index 370d83ec5cf8..efa5b67db8e1 100644

xen/arch/arm/tee/ffa_shm.c +9 -0
@@ -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.

xen/arch/arm/tee/ffa_shm.c int ffa_handle_mem_reclaim(uint64_t handle, uint32_t flags) · line 638

Quoted 9 lines
xen/arch/arm/tee/ffa_shm.c +3 -0
@@ -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 )

Cheers,

--
Julien Grall

#18 lore raw

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

Quoted 1 line · Jens Wiklander

Hi,

Hi,

Quoted 23 lines · Julien Grall
Quoted 13 lines

Hi Bertrand,

Quoted 4 lines · Bertrand Marquis

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

With this enabled. allow FF-A support to be activated for guest even if

Typo: s/./,/ I think.

Quoted 1 line

the firmware does not support it.

Quoted 8 lines

Can you explain why you want to do this?

The TEE mediator was designed to interpose with the HW. Without the HW it doesn't entirely make sense to try to use it.

It would also not work if the host system is using OP-TEE and expose to some VM FF-A. So it feels that the mediator may not be the right place to handle this case.

Quoted 7 lines

That's a good point, but all the FF-A handling should be in the same module since there's bound to be code and state to share. The problem is that FF-A tries to be a TEE mediator while it's about to become more than that. We can work around it by probing the OP-TEE mediator first, but it's adding another exception or special case. Would it make sense to move the FF-A mediator out of the TEE mediator framework and establish it separately?

I don't particularly want to have the FF-A mediator out of the TEE mediator. At the moment, it is unclear to me how much of the SMC handling code can really be shared between a domain talking with the host firmware and an emulated version. Are we just going to end up to have "if firmware then to this else do that"?

Cheers,

Signature · 1 line
Julien Grall
#19 lore raw

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

Hi Julien,

I will answer after to your previous comments but I wanted to jump in on this subject first.

Quoted 39 lines · Julien Grall
Quoted 1 line · Jens Wiklander

Hi,

Hi,

Quoted 22 lines · Julien Grall
Quoted 13 lines

Hi Bertrand,

Quoted 4 lines · Bertrand Marquis

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

With this enabled. allow FF-A support to be activated for guest even if

Typo: s/./,/ I think.

Quoted 1 line

the firmware does not support it.

Quoted 8 lines

Can you explain why you want to do this?

The TEE mediator was designed to interpose with the HW. Without the HW it doesn't entirely make sense to try to use it.

It would also not work if the host system is using OP-TEE and expose to some VM FF-A. So it feels that the mediator may not be the right place to handle this case.

Quoted 7 lines

That's a good point, but all the FF-A handling should be in the same module since there's bound to be code and state to share. The problem is that FF-A tries to be a TEE mediator while it's about to become more than that. We can work around it by probing the OP-TEE mediator first, but it's adding another exception or special case. Would it make sense to move the FF-A mediator out of the TEE mediator framework and establish it separately?

Quoted 1 line

I don't particularly want to have the FF-A mediator out of the TEE mediator. At the moment, it is unclear to me how much of the SMC handling code can really be shared between a domain talking with the host firmware and an emulated version. Are we just going to end up to have "if firmware then to this else do that"?

We will need to have a debate on how to handle Optee Mediator versus FF-A once we have FF-A support between VM.
In this case we could have cases where the firmware and secure world does not support FF-A and optee is using the current protocol but someone wants to use FF-A between VMs. My guts feeling right now is that we could (and probably should) not allow this use case as it would be realistic to say that a firmware support FF-A at some point would not support the old optee protocol.

At the status of this patch (and i do not plan to push VM to VM support in anything else than an RFC in the next weeks), I think FF-A should stay as a TEE mediator and I will revert the change enabling FF-A support in Xen when the firmware does not support it as we do not need that before VM to VM FF-A support is added.

Now even with that comes a question: What to do if the firmware supports FF-A but secure world has optee but using the old protocol. Should we probe optee first and FF-A second ?

Cheers
Bertrand

Quoted 5 lines

Cheers,

Signature · 1 line
Julien Grall
#20 lore raw

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

In reply to Julien Grall

Hi Julien,

Quoted 10 lines · Julien Grall

Hi Bertrand,

Quoted 3 lines · Bertrand Marquis

Create a bitmap to store which feature is supported or not by the firmware and use it to filter which calls done to the firmware. With this enabled. allow FF-A support to be activated for guest even if

Typo: s/./,/ I think.

Ack

Quoted 8 lines
Quoted 2 lines

the firmware does not support it.

Can you explain why you want to do this?

The TEE mediator was designed to interpose with the HW. Without the HW it doesn't entirely make sense to try to use it.

It would also not work if the host system is using OP-TEE and expose to some VM FF-A. So it feels that the mediator may not be the right place to handle this case.

As said in the answer to the thread on the subject, I will revert this change for now and only introduce this once we have VM to VM support. The goal of this change was to ensure that we could support VM to VM case even if the firmware does not support FF-A but that only make sense with VM to VM support in.

Quoted 77 lines
Quoted 77 lines

As a consequence, if the firmware is not there or not supported, we return an empty list of partitions to VMs requesting it through PARTINFO_GET ABI.

---
 xen/arch/arm/tee/ffa.c          | 31 ++++++++++++++++++++-----------
 xen/arch/arm/tee/ffa_notif.c    |  7 +++++++
 xen/arch/arm/tee/ffa_partinfo.c | 31 +++++++++++++++++++++++++++++--
 xen/arch/arm/tee/ffa_private.h  | 28 ++++++++++++++++++++++++++++
 xen/arch/arm/tee/ffa_rxtx.c     | 13 ++++++-------
 xen/arch/arm/tee/ffa_shm.c      | 12 ++++++++++++
 6 files changed, 102 insertions(+), 20 deletions(-)
xen/arch/arm/tee/ffa.c +17 -9
index 1f602f25d097..53960b146220 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_feat_supported, FEAT_FUNC_BITMAP_SIZE);
   /* List of ABI we use from the firmware */
 static const uint32_t ffa_fw_feat_needed[] = {
@@ -174,6 +177,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) )
     {
@@ -387,8 +397,6 @@ static int ffa_domain_init(struct domain *d)
     struct ffa_ctx *ctx;
     int ret;
 -    if ( !ffa_fw_version )
-        return -ENODEV;
       /*>         * We can't use that last possible domain ID or ffa_get_vm_id() would
       * cause an overflow.
@@ -523,6 +531,9 @@ static bool ffa_probe(void)
     printk(XENLOG_INFO "ARM FF-A Mediator version %u.%u\n",
            FFA_MY_VERSION_MAJOR, FFA_MY_VERSION_MINOR);
 +    INIT_LIST_HEAD(&ffa_teardown_head);
+    init_timer(&ffa_teardown_timer, ffa_teardown_timer_callback, NULL, 0);
+
     /*
      * psci_init_smccc() updates this value with what's reported by EL-3
      * or secure world.
@@ -568,12 +579,12 @@ static bool ffa_probe(void)
       for ( int i = 0; i < ARRAY_SIZE(ffa_fw_feat_needed); i++ )
     {
-        if ( !ffa_feature_supported(ffa_fw_feat_needed[i]) )
-        {
+        if ( ffa_feature_supported(ffa_fw_feat_needed[i]) )
+            set_bit(FEAT_FUNC_BITNUM(ffa_fw_feat_needed[i]),
+                    ffa_fw_feat_supported);
+        else
             printk(XENLOG_INFO "ARM FF-A Firmware does not support 0x%08x\n",
-                   ffa_fw_feat_needed[i]);
-            goto err_no_fw;
-        }
+                       ffa_fw_feat_needed[i]);
     }
       if ( !ffa_rxtx_init() )
@@ -586,8 +597,6 @@ static bool ffa_probe(void)
         goto err_rxtx_destroy;
       ffa_notif_init();
-    INIT_LIST_HEAD(&ffa_teardown_head);
-    init_timer(&ffa_teardown_timer, ffa_teardown_timer_callback, NULL, 0);
Quoted 8 lines
Quoted 6 lines
xen/arch/arm/tee/ffa.c +0 -0
       return true;
 @@ -597,7 +606,7 @@ err_no_fw:
     ffa_fw_version = 0;
     printk(XENLOG_INFO "ARM FF-A No firmware support\n");

- return false;
+ return true;

So effectively now ffa_probe() will always return true. If we end up to probe FF-A before OP-Tee, then we would always say "FF-A" is the TEE mediator.

Ack, i will revert that for now.

Quoted 67 lines
Quoted 67 lines

}
static const struct tee_mediator_ops ffa_ops =

xen/arch/arm/tee/ffa_notif.c +6 -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 +9 -1
index 93a03c6bc672..a42bd92ab8cf 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) )

xen/arch/arm/tee/ffa_partinfo.c +15 -0
+    {
+        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);
Quoted 8 lines

xen/arch/arm/tee/ffa_partinfo.c bool ffa_partinfo_init(void) · line 269

Quoted 6 lines
xen/arch/arm/tee/ffa_partinfo.c +0 -1
     if ( e )
     {
@@ -267,7 +292,6 @@ bool ffa_partinfo_init(void)
   out:
     ffa_rx_release();
-

Spurious change?

Yes

Quoted 37 lines
Quoted 24 lines
xen/arch/arm/tee/ffa_partinfo.c +0 -0
     return ret;
 }
 @@ -313,6 +337,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) )

xen/arch/arm/tee/ffa_partinfo.c +2 -0
+        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 +1 -0
index 7c6b06f686fc..d4dc9c8cd67b 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

xen/arch/arm/tee/ffa_private.h line 248

Quoted 10 lines
xen/arch/arm/tee/ffa_private.h +7 -0
@@ -238,6 +239,23 @@
 #define FFA_NOTIFICATION_INFO_GET_32    0x84000083U
 #define FFA_NOTIFICATION_INFO_GET_64    0xC4000083U
 +/**
+ * 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_FUNC_MIN           FFA_ERROR
+#define FFA_FUNC_MAX           FFA_NOTIFICATION_INFO_GET_64

These two defines confused me because FAA_ERROR is 0x84000060U and FFA_NOTIFICATION_INFO_GET_64 is 0xC4000083U. I think it would be better if we define them using FFA_FUNC_ID.

Quoted 1 line

We also probably want to have a compiler time check that FFA_FUNC_MIN is < FFA_FUNC_MAX.

Ack.

Quoted 14 lines

xen/arch/arm/tee/ffa_private.h line 257

Quoted 9 lines
xen/arch/arm/tee/ffa_private.h +8 -0
+#define FFA_FUNC_ID(id)        ((id) & ARM_SMCCC_FUNC_MASK)
+#define FFA_FUNC_CONV(id)      (((id) >> ARM_SMCCC_CONV_SHIFT) & BIT(0,U))
+
+#define FEAT_FUNC_BITMAP_SIZE   (2 * (FFA_FUNC_ID(FFA_FUNC_MAX) - \
+                                    FFA_FUNC_ID(FFA_FUNC_MIN) + 1))
+#define FEAT_FUNC_BITNUM(id)    ((FFA_FUNC_ID(id) - \
+                                    FFA_FUNC_ID(FFA_FUNC_MIN)) << 1 | \
+                                    FFA_FUNC_CONV(id))

The code seem to make two assumptions:
1. id is a constant
2. id is always valid

Quoted 1 line

I think it would be good to have a BUILD_BUG_ON(). This should avoid the two assumptions.

Yes you are right.

I guess i could make a BUILD_BUG_ON(id > FFA_FUNC_MAX) but the macro will definitely look ugly. I think i will switch to static inline functions doing the thing needed to have something cleaner.

Quoted 22 lines
Quoted 13 lines
xen/arch/arm/tee/ffa_private.h +3 -0
+
 struct ffa_ctx_notif {
     bool enabled;
 @@ -286,6 +304,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_feat_supported, FEAT_FUNC_BITMAP_SIZE);
   bool ffa_shm_domain_destroy(struct domain *d);
 void ffa_handle_mem_share(struct cpu_user_regs *regs);

xen/arch/arm/tee/ffa_private.h static inline int32_t ffa_rx_release(void) · line 423

Quoted 6 lines
xen/arch/arm/tee/ffa_private.h +3 -0
@@ -398,4 +418,12 @@ 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)
+{
+    if ( ffa_fw_version == 0 )
+        return false;

You could avoid this check if you ensure that ...

Quoted 3 lines

xen/arch/arm/tee/ffa_private.h static inline int32_t ffa_rx_release(void) · line 424

Quoted 1 line
xen/arch/arm/tee/ffa_private.h +1 -0
+    else> +        return test_bit(FEAT_FUNC_BITNUM(fid), ffa_fw_feat_supported);

.. the bitmap is always zeroed if ffa_fw_version is 0. You also want to check that the fid is valid (could be done at build time if fid is always a constant).

Agree, the point here was to optimize a bit and prevent the bitmap check when possible but that is probably not worth it. Will fix.

Quoted 27 lines
Quoted 27 lines
xen/arch/arm/tee/ffa_private.h +2 -0
+}
+
 #endif /*__FFA_PRIVATE_H__*/
xen/arch/arm/tee/ffa_rxtx.c +4 -2
index 661764052e67..cb54c76911fd 100644
@@ -193,24 +193,23 @@ 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;
       ffa_tx = alloc_xenheap_pages(get_order_from_pages(FFA_RXTX_PAGE_COUNT), 0);
     if ( !ffa_tx )
-        goto err;
+        return false;
       e = ffa_rxtx_map(__pa(ffa_tx), __pa(ffa_rx), FFA_RXTX_PAGE_COUNT);
     if ( e )
     {
         printk(XENLOG_ERR "ffa: Failed to map rxtx: error %d\n", e);
-        goto err;
Quoted 8 lines

xen/arch/arm/tee/ffa_rxtx.c bool ffa_rxtx_init(void) · line 213

Quoted 6 lines
xen/arch/arm/tee/ffa_rxtx.c +1 -3
+        return false;
     }
     return true;
-
-err:
-    ffa_rxtx_destroy();

This seems to be unrelated to the change. Can you explain why we don't need to call ffa_rxtx_destroy()?

Yes this is definitely a mistake. I will fix that.

Quoted 29 lines
Quoted 29 lines
xen/arch/arm/tee/ffa_rxtx.c +0 -2
-
-    return false;
  }> diff --git a/xen/arch/arm/tee/ffa_shm.c b/xen/arch/arm/tee/ffa_shm.c

index 370d83ec5cf8..efa5b67db8e1 100644

xen/arch/arm/tee/ffa_shm.c +3 -0
@@ -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) )

xen/arch/arm/tee/ffa_shm.c +5 -0
+    {
+        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)
Quoted 8 lines

xen/arch/arm/tee/ffa_shm.c int ffa_handle_mem_reclaim(uint64_t handle, uint32_t flags) · line 638

Quoted 8 lines
xen/arch/arm/tee/ffa_shm.c +2 -0
     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 )

Thanks again for the review.

Cheers
Bertrand

Quoted 5 lines

Cheers,

Signature · 1 line
Julien Grall
#21 lore raw

PATCH 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.

---
 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 53960b146220..beaed63a85ae 100644
@@ -308,8 +308,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 )
@@ -335,16 +333,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 d4dc9c8cd67b..089607c1c321 100644
@@ -314,9 +314,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.39.5 (Apple Git-154)
#22 lore raw

PATCH 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.

---
 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 beaed63a85ae..45f9c1db8a6e 100644
@@ -192,6 +192,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;
@@ -386,11 +394,15 @@ static int ffa_domain_init(struct domain *d)
     struct ffa_ctx *ctx;
     int ret;
 
-     /*
-      * 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 089607c1c321..c6903e335489 100644
@@ -105,6 +105,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.39.5 (Apple Git-154)
#23 lore raw

PATCH 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.

---
 xen/arch/arm/tee/ffa.c          |   2 +-
 xen/arch/arm/tee/ffa_partinfo.c |  28 +++----
 xen/arch/arm/tee/ffa_private.h  |  22 +++++-
 xen/arch/arm/tee/ffa_rxtx.c     | 126 ++++++++++++++++++++++++++++----
 xen/arch/arm/tee/ffa_shm.c      |  15 ----
 5 files changed, 142 insertions(+), 51 deletions(-)
xen/arch/arm/tee/ffa.c +1 -1
index 45f9c1db8a6e..4a769e20007b 100644
@@ -344,7 +344,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 -19
index b391b1adf9f2..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,7 +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 c6903e335489..84b0f866a71e 100644
@@ -263,6 +263,21 @@
                                     FFA_FUNC_ID(FFA_FUNC_MIN)) << 1 | \
                                     FFA_FUNC_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;
 
@@ -290,7 +305,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;
@@ -329,7 +344,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);
@@ -418,7 +434,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 +113 -13

165 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.39.5 (Apple Git-154)
#24 lore raw

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

Hi,

Quoted 241 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.

---
 xen/arch/arm/tee/ffa.c          |   2 +-
 xen/arch/arm/tee/ffa_partinfo.c |  28 +++----
 xen/arch/arm/tee/ffa_private.h  |  22 +++++-
 xen/arch/arm/tee/ffa_rxtx.c     | 126 ++++++++++++++++++++++++++++----
 xen/arch/arm/tee/ffa_shm.c      |  15 ----
 5 files changed, 142 insertions(+), 51 deletions(-)
xen/arch/arm/tee/ffa.c +1 -1
index 45f9c1db8a6e..4a769e20007b 100644
@@ -344,7 +344,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 -19
index b391b1adf9f2..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,7 +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 c6903e335489..84b0f866a71e 100644
@@ -263,6 +263,21 @@
                                     FFA_FUNC_ID(FFA_FUNC_MIN)) << 1 | \
                                     FFA_FUNC_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;
 
@@ -290,7 +305,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;
@@ -329,7 +344,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);
@@ -418,7 +434,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 +56 -0
index cb54c76911fd..dacf33cc9efc 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(void)
+{
+    return ffa_simple_call(FFA_RXTX_UNMAP, 0, 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
+     * 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);
+
+        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);

I can't find the corresponding ffa_rxtx_unmap() of a VM rxtx buffer.

Cheers,
Jens

Quoted 115 lines
xen/arch/arm/tee/ffa_rxtx.c +47 -13
+
+        spin_unlock(&ffa_tx_buffer_lock);
+
+        if ( ret != FFA_RET_OK )
+            goto err_unmap_tx;
+    }
+
     ctx->rx = rx;
     ctx->tx = tx;
     ctx->rx_pg = rx_pg;
@@ -132,34 +205,61 @@ uint32_t ffa_handle_rxtx_unmap(void)
     return FFA_RET_OK;
 }
 
-int32_t ffa_handle_rx_release(void)
+int32_t ffa_rx_acquire(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 )
+    if ( !ctx->page_count )
+    {
+        ret = FFA_RET_DENIED;
+        goto out;
+    }
+
+    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;
+    }
     ret = FFA_RET_OK;
-    ctx->rx_is_free = true;
+    ctx->rx_is_free = false;
 out:
     spin_unlock(&ctx->rx_lock);
 
     return ret;
 }
 
-static int32_t ffa_rxtx_map(paddr_t tx_addr, paddr_t rx_addr,
-                            uint32_t page_count)
+int32_t ffa_rx_release(struct domain *d)
 {
-    return ffa_simple_call(FFA_RXTX_MAP_64, tx_addr, rx_addr, page_count, 0);
-}
+    int32_t ret = FFA_RET_DENIED;
+    struct ffa_ctx *ctx = d->arch.tee;
 
-static int32_t ffa_rxtx_unmap(void)
-{
-    return ffa_simple_call(FFA_RXTX_UNMAP, 0, 0, 0, 0);
+    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:
+    spin_unlock(&ctx->rx_lock);
+
+    return ret;
 }
 
 void ffa_rxtx_domain_destroy(struct domain *d)
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.39.5 (Apple Git-154)

#25 lore raw

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

Hi Jens,

Quoted 251 lines · Jens Wiklander

Hi,

Quoted 245 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.

---

xen/arch/arm/tee/ffa.c          |   2 +-
xen/arch/arm/tee/ffa_partinfo.c |  28 +++----
xen/arch/arm/tee/ffa_private.h  |  22 +++++-
xen/arch/arm/tee/ffa_rxtx.c     | 126 ++++++++++++++++++++++++++++----
xen/arch/arm/tee/ffa_shm.c      |  15 ----
5 files changed, 142 insertions(+), 51 deletions(-)
xen/arch/arm/tee/ffa.c +1 -1
index 45f9c1db8a6e..4a769e20007b 100644
@@ -344,7 +344,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 b391b1adf9f2..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 +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);

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

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

out:

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

}

xen/arch/arm/tee/ffa_private.h +19 -3
index c6903e335489..84b0f866a71e 100644
@@ -263,6 +263,21 @@
                                    FFA_FUNC_ID(FFA_FUNC_MIN)) << 1 | \
                                    FFA_FUNC_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;
 
@@ -290,7 +305,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;
@@ -329,7 +344,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);
@@ -418,7 +434,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 +60 -0
index cb54c76911fd..dacf33cc9efc 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(void)
+{
+    return ffa_simple_call(FFA_RXTX_UNMAP, 0, 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
+     * 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);
+
+        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);

I can't find the corresponding ffa_rxtx_unmap() of a VM rxtx buffer.

Right, i will fix that in the next version.

Cheers
Bertrand

Quoted 119 lines

Cheers,
Jens

Quoted 115 lines
xen/arch/arm/tee/ffa_rxtx.c +27 -6
+
+        spin_unlock(&ffa_tx_buffer_lock);
+
+        if ( ret != FFA_RET_OK )
+            goto err_unmap_tx;
+    }
+
    ctx->rx = rx;
    ctx->tx = tx;
    ctx->rx_pg = rx_pg;
@@ -132,34 +205,61 @@ uint32_t ffa_handle_rxtx_unmap(void)
    return FFA_RET_OK;
 }
 
-int32_t ffa_handle_rx_release(void)
+int32_t ffa_rx_acquire(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 )
+    if ( !ctx->page_count )
+    {
+        ret = FFA_RET_DENIED;
+        goto out;
+    }
+
+    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;
+    }
    ret = FFA_RET_OK;
-    ctx->rx_is_free = true;
+    ctx->rx_is_free = false;

out:

    spin_unlock(&ctx->rx_lock);

    return ret;
}
xen/arch/arm/tee/ffa_rxtx.c +1 -2
-static int32_t ffa_rxtx_map(paddr_t tx_addr, paddr_t rx_addr,
-                            uint32_t page_count)
+int32_t ffa_rx_release(struct domain *d)

{

xen/arch/arm/tee/ffa_rxtx.c +19 -5
-    return ffa_simple_call(FFA_RXTX_MAP_64, tx_addr, rx_addr, page_count, 0);
-}
+    int32_t ret = FFA_RET_DENIED;
+    struct ffa_ctx *ctx = d->arch.tee;
 
-static int32_t ffa_rxtx_unmap(void)
-{
-    return ffa_simple_call(FFA_RXTX_UNMAP, 0, 0, 0, 0);
+    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:
+    spin_unlock(&ctx->rx_lock);
+
+    return ret;

}

void ffa_rxtx_domain_destroy(struct domain *d)

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.39.5 (Apple Git-154)

#26 lore raw

PATCH 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.

---
 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 84b0f866a71e..0be246ff0ae7 100644
@@ -358,6 +358,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.39.5 (Apple Git-154)
#27 lore raw

PATCH 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.

---
 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 89c6a1cf06a1..5d7633dbb3a5 100644
@@ -166,8 +166,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;
 
@@ -215,16 +213,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:
@@ -233,10 +225,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 0be246ff0ae7..5054254540ef 100644
@@ -279,8 +279,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.39.5 (Apple Git-154)
#28 lore raw

PATCH 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.

---
 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 5d7633dbb3a5..abb05e09f6f4 100644
@@ -94,6 +94,7 @@ static const uint32_t ffa_fw_feat_needed[] = {
     FFA_MEM_RECLAIM,
     FFA_MSG_SEND_DIRECT_REQ_32,
     FFA_MSG_SEND_DIRECT_REQ_64,
+    FFA_MSG_SEND2,
 };
 
 /*
@@ -192,6 +193,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:
@@ -272,6 +274,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 5054254540ef..1bc7b33ec2c7 100644
@@ -357,6 +357,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.39.5 (Apple Git-154)