Skip to contentSkip to conversation

FF-A notifications

PATCH Superseded Jens Wiklander 2 participants 10 messages May 2, 2024 – May 7, 2024

v4 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v4 cover FF-A notifications

Hi,

This patch set adds support for FF-A notifications. We only support global notifications, per vCPU notifications remain unsupported.

The first three patches are further cleanup and can be merged before the rest if desired.

A physical SGI is used to make Xen aware of pending FF-A notifications. The physical SGI is selected by the SPMC in the secure world. Since it must not already be used by Xen the SPMC is in practice forced to donate one of the secure SGIs, but that's normally not a problem. The SGI handling in Xen is updated to support registration of handlers for SGIs that aren't statically assigned, that is, SGI IDs above GIC_SGI_MAX.

Two items stand out in the last patch, "xen/arm: ffa: support notification":
- The interrupt handler must be registered for each online CPU, but this will usually happen for the secondary CPUs after FF-A has been initialized so we may need to take some further action in the error path
- The interrupt handler need to get hold of the domain struct of a guest as indicated by the SPMC. In this patch set we use rcu_lock_live_remote_domain_by_id() to get a safe reference to the domain struct.

Thanks,
Jens

v3->v4:
- "xen/arm: ffa: support notification" and
"xen/arm: allow dynamically assigned SGI handlers" updated as requestsed, details in each patch.

v2->v3:
- "xen/arm: ffa: support notification" and
"xen/arm: allow dynamically assigned SGI handlers" updated as requestsed, details in each patch.

v1->v2:
- "xen/arm: ffa: support notification" and
"xen/arm: allow dynamically assigned SGI handlers" updated as requestsed, details in each patch.
- Added Bertrands R-B for "xen/arm: ffa: refactor ffa_handle_call()", "xen/arm: ffa: use ACCESS_ONCE()", and
"xen/arm: ffa: simplify ffa_handle_mem_share()"

Jens Wiklander (5):
xen/arm: ffa: refactor ffa_handle_call() xen/arm: ffa: use ACCESS_ONCE()
xen/arm: ffa: simplify ffa_handle_mem_share() xen/arm: allow dynamically assigned SGI handlers xen/arm: ffa: support notification

 xen/arch/arm/gic.c              |  12 +-
 xen/arch/arm/include/asm/gic.h  |   2 +-
 xen/arch/arm/irq.c              |  18 +-
 xen/arch/arm/tee/Makefile       |   1 +
 xen/arch/arm/tee/ffa.c          | 108 +++++---
 xen/arch/arm/tee/ffa_notif.c    | 453 ++++++++++++++++++++++++++++++++
 xen/arch/arm/tee/ffa_partinfo.c |   9 +-
 xen/arch/arm/tee/ffa_private.h  |  60 ++++-
 xen/arch/arm/tee/ffa_shm.c      |  33 +--
 xen/include/public/arch-arm.h   |  14 +
 10 files changed, 647 insertions(+), 63 deletions(-)
 create mode 100644 xen/arch/arm/tee/ffa_notif.c
Signature · 1 line
2.34.1
#1 lore raw

PATCH v4 1/5 xen/arm: ffa: refactor ffa_handle_call()

Refactors the large switch block in ffa_handle_call() to use common code for the simple case where it's either an error code or success with no further parameters.

---
 xen/arch/arm/tee/ffa.c | 30 ++++++++++--------------------
 1 file changed, 10 insertions(+), 20 deletions(-)
xen/arch/arm/tee/ffa.c +10 -20
index 8665201e34a9..5209612963e1 100644
@@ -273,18 +273,10 @@ static bool ffa_handle_call(struct cpu_user_regs *regs)
     case FFA_RXTX_MAP_64:
         e = ffa_handle_rxtx_map(fid, get_user_reg(regs, 1),
 				get_user_reg(regs, 2), get_user_reg(regs, 3));
-        if ( e )
-            ffa_set_regs_error(regs, e);
-        else
-            ffa_set_regs_success(regs, 0, 0);
-        return true;
+        break;
     case FFA_RXTX_UNMAP:
         e = ffa_handle_rxtx_unmap();
-        if ( e )
-            ffa_set_regs_error(regs, e);
-        else
-            ffa_set_regs_success(regs, 0, 0);
-        return true;
+        break;
     case FFA_PARTITION_INFO_GET:
         e = ffa_handle_partition_info_get(get_user_reg(regs, 1),
                                           get_user_reg(regs, 2),
@@ -299,11 +291,7 @@ static bool ffa_handle_call(struct cpu_user_regs *regs)
         return true;
     case FFA_RX_RELEASE:
         e = ffa_handle_rx_release();
-        if ( e )
-            ffa_set_regs_error(regs, e);
-        else
-            ffa_set_regs_success(regs, 0, 0);
-        return true;
+        break;
     case FFA_MSG_SEND_DIRECT_REQ_32:
     case FFA_MSG_SEND_DIRECT_REQ_64:
         handle_msg_send_direct_req(regs, fid);
@@ -316,17 +304,19 @@ static bool ffa_handle_call(struct cpu_user_regs *regs)
         e = ffa_handle_mem_reclaim(regpair_to_uint64(get_user_reg(regs, 2),
                                                      get_user_reg(regs, 1)),
                                    get_user_reg(regs, 3));
-        if ( e )
-            ffa_set_regs_error(regs, e);
-        else
-            ffa_set_regs_success(regs, 0, 0);
-        return true;
+        break;
 
     default:
         gprintk(XENLOG_ERR, "ffa: unhandled fid 0x%x\n", fid);
         ffa_set_regs_error(regs, FFA_RET_NOT_SUPPORTED);
         return true;
     }
+
+    if ( e )
+        ffa_set_regs_error(regs, e);
+    else
+        ffa_set_regs_success(regs, 0, 0);
+    return true;
 }
 
 static int ffa_domain_init(struct domain *d)
Signature · 1 line
2.34.1
#2 lore raw

PATCH v4 2/5 xen/arm: ffa: use ACCESS_ONCE()

In reply to Jens Wiklander

Replace read_atomic() with ACCESS_ONCE() to match the intended use, that is, to prevent the compiler from (via optimization) reading shared memory more than once.

---
 xen/arch/arm/tee/ffa_shm.c | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)
xen/arch/arm/tee/ffa_shm.c +8 -7
index eed9ad2d2986..75a5b66aeb4c 100644
@@ -7,6 +7,7 @@
 #include <xen/sizes.h>
 #include <xen/types.h>
 #include <xen/mm.h>
+#include <xen/lib.h>
 #include <xen/list.h>
 #include <xen/spinlock.h>
 
@@ -171,8 +172,8 @@ static int get_shm_pages(struct domain *d, struct ffa_shm_mem *shm,
 
     for ( n = 0; n < range_count; n++ )
     {
-        page_count = read_atomic(&range[n].page_count);
-        addr = read_atomic(&range[n].address);
+        page_count = ACCESS_ONCE(range[n].page_count);
+        addr = ACCESS_ONCE(range[n].address);
         for ( m = 0; m < page_count; m++ )
         {
             if ( pg_idx >= shm->page_count )
@@ -527,13 +528,13 @@ void ffa_handle_mem_share(struct cpu_user_regs *regs)
         goto out_unlock;
 
     mem_access = ctx->tx + trans.mem_access_offs;
-    if ( read_atomic(&mem_access->access_perm.perm) != FFA_MEM_ACC_RW )
+    if ( ACCESS_ONCE(mem_access->access_perm.perm) != FFA_MEM_ACC_RW )
     {
         ret = FFA_RET_NOT_SUPPORTED;
         goto out_unlock;
     }
 
-    region_offs = read_atomic(&mem_access->region_offs);
+    region_offs = ACCESS_ONCE(mem_access->region_offs);
     if ( sizeof(*region_descr) + region_offs > frag_len )
     {
         ret = FFA_RET_NOT_SUPPORTED;
@@ -541,8 +542,8 @@ void ffa_handle_mem_share(struct cpu_user_regs *regs)
     }
 
     region_descr = ctx->tx + region_offs;
-    range_count = read_atomic(&region_descr->address_range_count);
-    page_count = read_atomic(&region_descr->total_page_count);
+    range_count = ACCESS_ONCE(region_descr->address_range_count);
+    page_count = ACCESS_ONCE(region_descr->total_page_count);
 
     if ( !page_count )
     {
@@ -557,7 +558,7 @@ void ffa_handle_mem_share(struct cpu_user_regs *regs)
         goto out_unlock;
     }
     shm->sender_id = trans.sender_id;
-    shm->ep_id = read_atomic(&mem_access->access_perm.endpoint_id);
+    shm->ep_id = ACCESS_ONCE(mem_access->access_perm.endpoint_id);
 
     /*
      * Check that the Composite memory region descriptor fits.
Signature · 1 line
2.34.1
#3 lore raw

PATCH v4 3/5 xen/arm: ffa: simplify ffa_handle_mem_share()

In reply to Jens Wiklander

Simplify ffa_handle_mem_share() by removing the start_page_idx and last_page_idx parameters from get_shm_pages() and check that the number of pages matches expectations at the end of get_shm_pages().

---
 xen/arch/arm/tee/ffa_shm.c | 18 ++++++------------
 1 file changed, 6 insertions(+), 12 deletions(-)
xen/arch/arm/tee/ffa_shm.c +6 -12
index 75a5b66aeb4c..370d83ec5cf8 100644
@@ -159,10 +159,9 @@ static int32_t ffa_mem_reclaim(uint32_t handle_lo, uint32_t handle_hi,
  */
 static int get_shm_pages(struct domain *d, struct ffa_shm_mem *shm,
                          const struct ffa_address_range *range,
-                         uint32_t range_count, unsigned int start_page_idx,
-                         unsigned int *last_page_idx)
+                         uint32_t range_count)
 {
-    unsigned int pg_idx = start_page_idx;
+    unsigned int pg_idx = 0;
     gfn_t gfn;
     unsigned int n;
     unsigned int m;
@@ -191,7 +190,9 @@ static int get_shm_pages(struct domain *d, struct ffa_shm_mem *shm,
         }
     }
 
-    *last_page_idx = pg_idx;
+    /* The ranges must add up */
+    if ( pg_idx < shm->page_count )
+            return FFA_RET_INVALID_PARAMETERS;
 
     return FFA_RET_OK;
 }
@@ -460,7 +461,6 @@ void ffa_handle_mem_share(struct cpu_user_regs *regs)
     struct domain *d = current->domain;
     struct ffa_ctx *ctx = d->arch.tee;
     struct ffa_shm_mem *shm = NULL;
-    unsigned int last_page_idx = 0;
     register_t handle_hi = 0;
     register_t handle_lo = 0;
     int ret = FFA_RET_DENIED;
@@ -570,15 +570,9 @@ void ffa_handle_mem_share(struct cpu_user_regs *regs)
         goto out;
     }
 
-    ret = get_shm_pages(d, shm, region_descr->address_range_array, range_count,
-                        0, &last_page_idx);
+    ret = get_shm_pages(d, shm, region_descr->address_range_array, range_count);
     if ( ret )
         goto out;
-    if ( last_page_idx != shm->page_count )
-    {
-        ret = FFA_RET_INVALID_PARAMETERS;
-        goto out;
-    }
 
     /* Note that share_shm() uses our tx buffer */
     spin_lock(&ffa_tx_buffer_lock);
Signature · 1 line
2.34.1
#4 lore raw

PATCH v4 4/5 xen/arm: allow dynamically assigned SGI handlers

In reply to Jens Wiklander

Updates so request_irq() can be used with a dynamically assigned SGI irq as input. This prepares for a later patch where an FF-A schedule receiver interrupt handler is installed for an SGI generated by the secure world.

From the Arm Base System Architecture v1.0C [1]:
"The system shall implement at least eight Non-secure SGIs, assigned to interrupt IDs 0-7."

gic_route_irq_to_xen() don't gic_set_irq_type() for SGIs since they are always edge triggered.

gic_interrupt() is updated to route the dynamically assigned SGIs to do_IRQ() instead of do_sgi(). The latter still handles the statically assigned SGI handlers like for instance GIC_SGI_CALL_FUNCTION.

[1] https://developer.arm.com/documentation/den0094/

---
v3->v4
- Use IRQ_TYPE_EDGE_RISING instead of DT_IRQ_TYPE_EDGE_RISING

v2->v3
- Rename GIC_SGI_MAX to GIC_SGI_STATIC_MAX and rename do_sgi() to do_static_sgi()
- Update comment in setup_irq() to mention that SGI irq_desc is banked
- Add ASSERT() in do_IRQ() that the irq isn't an SGI before injecting calling vgic_inject_irq()
- Initialize local_irqs_type[] range for SGIs as IRQ_TYPE_EDGE_RISING
- Adding link to the Arm Base System Architecture v1.0C

v1->v2
- Update patch description as requested

---
 xen/arch/arm/gic.c             | 12 +++++++-----
 xen/arch/arm/include/asm/gic.h |  2 +-
 xen/arch/arm/irq.c             | 18 ++++++++++++++----
 3 files changed, 22 insertions(+), 10 deletions(-)
xen/arch/arm/gic.c +7 -5
index 44c40e86defe..882768252740 100644
@@ -38,7 +38,7 @@ const struct gic_hw_operations *gic_hw_ops;
 static void __init __maybe_unused build_assertions(void)
 {
     /* Check our enum gic_sgi only covers SGIs */
-    BUILD_BUG_ON(GIC_SGI_MAX > NR_GIC_SGI);
+    BUILD_BUG_ON(GIC_SGI_STATIC_MAX > NR_GIC_SGI);
 }
 
 void register_gic_ops(const struct gic_hw_operations *ops)
@@ -117,7 +117,9 @@ void gic_route_irq_to_xen(struct irq_desc *desc, unsigned int priority)
 
     desc->handler = gic_hw_ops->gic_host_irq_type;
 
-    gic_set_irq_type(desc, desc->arch.type);
+    /* SGIs are always edge-triggered, so there is need to set it */
+    if ( desc->irq >= NR_GIC_SGI)
+        gic_set_irq_type(desc, desc->arch.type);
     gic_set_irq_priority(desc, priority);
 }
 
@@ -330,7 +332,7 @@ void gic_disable_cpu(void)
     gic_hw_ops->disable_interface();
 }
 
-static void do_sgi(struct cpu_user_regs *regs, enum gic_sgi sgi)
+static void do_static_sgi(struct cpu_user_regs *regs, enum gic_sgi sgi)
 {
     struct irq_desc *desc = irq_to_desc(sgi);
 
@@ -375,7 +377,7 @@ void gic_interrupt(struct cpu_user_regs *regs, int is_fiq)
         /* Reading IRQ will ACK it */
         irq = gic_hw_ops->read_irq();
 
-        if ( likely(irq >= 16 && irq < 1020) )
+        if ( likely(irq >= GIC_SGI_STATIC_MAX && irq < 1020) )
         {
             isb();
             do_IRQ(regs, irq, is_fiq);
@@ -387,7 +389,7 @@ void gic_interrupt(struct cpu_user_regs *regs, int is_fiq)
         }
         else if ( unlikely(irq < 16) )
         {
-            do_sgi(regs, irq);
+            do_static_sgi(regs, irq);
         }
         else
         {
xen/arch/arm/include/asm/gic.h +1 -1
index 03f209529b13..541f0eeb808a 100644
@@ -285,7 +285,7 @@ enum gic_sgi {
     GIC_SGI_EVENT_CHECK,
     GIC_SGI_DUMP_STATE,
     GIC_SGI_CALL_FUNCTION,
-    GIC_SGI_MAX,
+    GIC_SGI_STATIC_MAX,
 };
 
 /* SGI irq mode types */
xen/arch/arm/irq.c +14 -4
index bcce80a4d624..5224898265a5 100644
@@ -152,7 +152,13 @@ void __init init_IRQ(void)
 
     spin_lock(&local_irqs_type_lock);
     for ( irq = 0; irq < NR_LOCAL_IRQS; irq++ )
-        local_irqs_type[irq] = IRQ_TYPE_INVALID;
+    {
+        /* SGIs are always edge-triggered */
+        if ( irq < NR_GIC_SGI )
+            local_irqs_type[irq] = IRQ_TYPE_EDGE_RISING;
+        else
+            local_irqs_type[irq] = IRQ_TYPE_INVALID;
+    }
     spin_unlock(&local_irqs_type_lock);
 
     BUG_ON(init_local_irq_data(smp_processor_id()) < 0);
@@ -224,9 +230,12 @@ void do_IRQ(struct cpu_user_regs *regs, unsigned int irq, int is_fiq)
 
     perfc_incr(irqs);
 
-    ASSERT(irq >= 16); /* SGIs do not come down this path */
+    /* Statically assigned SGIs do not come down this path */
+    ASSERT(irq >= GIC_SGI_STATIC_MAX);
 
-    if ( irq < 32 )
+    if ( irq < NR_GIC_SGI )
+        perfc_incr(ipis);
+    else if ( irq < NR_GIC_LOCAL_IRQS )
         perfc_incr(ppis);
     else
         perfc_incr(spis);
@@ -260,6 +269,7 @@ void do_IRQ(struct cpu_user_regs *regs, unsigned int irq, int is_fiq)
          * The irq cannot be a PPI, we only support delivery of SPIs to
          * guests.
          */
+        ASSERT(irq >= NR_GIC_SGI);
         vgic_inject_irq(info->d, NULL, info->virq, true);
         goto out_no_end;
     }
@@ -396,7 +406,7 @@ int setup_irq(unsigned int irq, unsigned int irqflags, struct irqaction *new)
     {
         gic_route_irq_to_xen(desc, GIC_PRI_IRQ);
         /* It's fine to use smp_processor_id() because:
-         * For PPI: irq_desc is banked
+         * For SGI and PPI: irq_desc is banked
          * For SPI: we don't care for now which CPU will receive the
          * interrupt
          * TODO: Handle case where SPI is setup on different CPU than
Signature · 1 line
2.34.1
#5 lore raw

PATCH v4 5/5 xen/arm: ffa: support notification

In reply to Jens Wiklander

Add support for FF-A notifications, currently limited to an SP (Secure Partition) sending an asynchronous notification to a guest.

Guests and Xen itself are made aware of pending notifications with an interrupt. The interrupt handler retrieves the notifications using the FF-A ABI and deliver them to their destinations.

Update ffa_partinfo_domain_init() to return error code like ffa_notif_domain_init().

Initialize FF-A with ffa_init() registered in presmp_initcall(). ffa_probe() just returns the probed version which is 0 if initialization failed. Early FF-A initialization is needed to install the interrupt handler on each CPU through a function registered with register_cpu_notifier().

---
v3->v4:
- Add another note on FF-A limitations
- Clear secure_pending in ffa_handle_notification_get() if both SP and SPM bitmaps are retrieved
- ASSERT that ffa_rcu_lock_domain_by_vm_id() isn't passed the vm_id FF-A uses for Xen itself
- Replace the get_domain_by_id() call done via ffa_get_domain_by_vm_id() in notif_irq_handler() with a call to rcu_lock_live_remote_domain_by_id() via ffa_rcu_lock_domain_by_vm_id()
- Remove spinlock in struct ffa_ctx_notif and use atomic functions as needed to access and update the secure_pending field
- In notif_irq_handler(), look for the first online CPU instead of assuming that the first CPU is online
- Initialize FF-A via presmp_initcall() before the other CPUs are online, use register_cpu_notifier() to install the interrupt handler notif_irq_handler()
- Update commit message to reflect recent updates

v2->v3:
- Add a GUEST_ prefix and move FFA_NOTIF_PEND_INTR_ID and FFA_SCHEDULE_RECV_INTR_ID to public/arch-arm.h
- Register the Xen SRI handler on each CPU using on_selected_cpus() and setup_irq()
- Check that the SGI ID retrieved with FFA_FEATURE_SCHEDULE_RECV_INTR doesn't conflict with static SGI handlers

v1->v2:
- Addressing review comments
- Change ffa_handle_notification_{bind,unbind,set}() to take struct cpu_user_regs *regs as argument.
- Update ffa_partinfo_domain_init() and ffa_notif_domain_init() to return an error code.
- Fixing a bug in handle_features() for FFA_FEATURE_SCHEDULE_RECV_INTR.

---
 xen/arch/arm/tee/Makefile       |   1 +
 xen/arch/arm/tee/ffa.c          |  80 +++++-
 xen/arch/arm/tee/ffa_notif.c    | 453 ++++++++++++++++++++++++++++++++
 xen/arch/arm/tee/ffa_partinfo.c |   9 +-
 xen/arch/arm/tee/ffa_private.h  |  60 ++++-
 xen/include/public/arch-arm.h   |  14 +
 6 files changed, 602 insertions(+), 15 deletions(-)
 create mode 100644 xen/arch/arm/tee/ffa_notif.c
xen/arch/arm/tee/Makefile +1 -0
index f0112a2f922d..7c0f46f7f446 100644
@@ -2,5 +2,6 @@ obj-$(CONFIG_FFA) += ffa.o
 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-y += tee.o
 obj-$(CONFIG_OPTEE) += optee.o
xen/arch/arm/tee/ffa.c +69 -11

164 lines not on this page · raw message

xen/arch/arm/tee/ffa_notif.c new file +453 -0

453 lines not on this page · raw message

xen/arch/arm/tee/ffa_partinfo.c +6 -3
index dc1059584828..93a03c6bc672 100644
@@ -306,7 +306,7 @@ static void vm_destroy_bitmap_init(struct ffa_ctx *ctx,
     }
 }
 
-bool ffa_partinfo_domain_init(struct domain *d)
+int ffa_partinfo_domain_init(struct domain *d)
 {
     unsigned int count = BITS_TO_LONGS(subscr_vm_destroyed_count);
     struct ffa_ctx *ctx = d->arch.tee;
@@ -315,7 +315,7 @@ bool ffa_partinfo_domain_init(struct domain *d)
 
     ctx->vm_destroy_bitmap = xzalloc_array(unsigned long, count);
     if ( !ctx->vm_destroy_bitmap )
-        return false;
+        return -ENOMEM;
 
     for ( n = 0; n < subscr_vm_created_count; n++ )
     {
@@ -330,7 +330,10 @@ bool ffa_partinfo_domain_init(struct domain *d)
     }
     vm_destroy_bitmap_init(ctx, n);
 
-    return n == subscr_vm_created_count;
+    if ( n != subscr_vm_created_count )
+        return -EIO;
+
+    return 0;
 }
 
 bool ffa_partinfo_domain_destroy(struct domain *d)
xen/arch/arm/tee/ffa_private.h +59 -1

102 lines not on this page · raw message

xen/include/public/arch-arm.h +14 -0
index 289af81bd69d..e2412a17474e 100644
@@ -505,6 +505,7 @@ typedef uint64_t xen_callback_t;
 #define GUEST_MAX_VCPUS 128
 
 /* Interrupts */
+
 #define GUEST_TIMER_VIRT_PPI    27
 #define GUEST_TIMER_PHYS_S_PPI  29
 #define GUEST_TIMER_PHYS_NS_PPI 30
@@ -515,6 +516,19 @@ typedef uint64_t xen_callback_t;
 #define GUEST_VIRTIO_MMIO_SPI_FIRST   33
 #define GUEST_VIRTIO_MMIO_SPI_LAST    43
 
+/*
+ * SGI is the preferred delivery mechanism of FF-A pending notifications or
+ * schedule recveive interrupt. SGIs 8-15 are normally not used by a guest
+ * as they in a non-virtualized system typically are assigned to the secure
+ * world. Here we're free to use SGI 8-15 since they are virtual and have
+ * nothing to do with the secure world.
+ *
+ * For partitioning of SGIs see also Arm Base System Architecture v1.0C,
+ * https://developer.arm.com/documentation/den0094/
+ */
+#define GUEST_FFA_NOTIF_PEND_INTR_ID      8
+#define GUEST_FFA_SCHEDULE_RECV_INTR_ID   9
+
 /* PSCI functions */
 #define PSCI_cpu_suspend 0
 #define PSCI_cpu_off     1
Signature · 1 line
2.34.1
#6 lore raw

Re: [XEN PATCH v4 5/5] xen/arm: ffa: support notification

Hi Jens,

Quoted 46 lines · Jens Wiklander
xen/arch/arm/tee/ffa.c +7 -7
-static bool ffa_probe(void)
+static int __init ffa_init(void)
  {
      uint32_t vers;
      unsigned int major_vers;
@@ -460,16 +511,16 @@ 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;
+        return 0;
      }
 
      if ( !ffa_get_version(&vers) )
-        return false;
+        return 0;
 
      if ( vers < FFA_MIN_SPMC_VERSION || vers > FFA_MY_VERSION )
      {
          printk(XENLOG_ERR "ffa: Incompatible version %#x found\n", vers);
-        return false;
+        return 0;
      }
 
      major_vers = (vers >> FFA_VERSION_MAJOR_SHIFT) & FFA_VERSION_MAJOR_MASK;
@@ -492,26 +543,33 @@ 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;
+        return 0;
 
      if ( !ffa_rxtx_init() )
-        return false;
+        return 0;
 
      ffa_version = vers;
 
      if ( !ffa_partinfo_init() )
          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 static bool ffa_probe(void) · line 568

Quoted 10 lines
xen/arch/arm/tee/ffa.c +4 -1
+    return 0;
 
  err_rxtx_destroy:
      ffa_rxtx_destroy();
      ffa_version = 0;
 
-    return false;
+    return 0;
+}
+presmp_initcall(ffa_init);

I would rather prefer if tee_init() is called from presmp_initcall(). This would avoid FFA to have to try to initialize itself before all the others.

This is what I had in mind with my original request, but I guess I wasn't clear enough. Sorry for that.

[...]

Quoted 30 lines
xen/arch/arm/tee/ffa_notif.c new file +30 -0
+static void notif_irq_handler(int irq, void *data)
+{
+    const struct arm_smccc_1_2_regs arg = {
+        .a0 = FFA_NOTIFICATION_INFO_GET_64,
+    };
+    struct arm_smccc_1_2_regs resp;
+    unsigned int id_pos;
+    unsigned int list_count;
+    uint64_t ids_count;
+    unsigned int n;
+    int32_t res;
+
+    do {
+        arm_smccc_1_2_smc(&arg, &resp);
+        res = ffa_get_ret_code(&resp);
+        if ( res )
+        {
+            if ( res != FFA_RET_NO_DATA )
+                printk(XENLOG_ERR "ffa: notification info get failed: error %d\n",
+                       res);
+            return;
+        }
+
+        ids_count = resp.a2 >> FFA_NOTIF_INFO_GET_ID_LIST_SHIFT;
+        list_count = ( resp.a2 >> FFA_NOTIF_INFO_GET_ID_COUNT_SHIFT ) &
+                     FFA_NOTIF_INFO_GET_ID_COUNT_MASK;
+
+        id_pos = 0;
+        for ( n = 0; n < list_count; n++ )
+        {

xen/arch/arm/tee/ffa_notif.c line 259

Quoted 10 lines
xen/arch/arm/tee/ffa_notif.c new file +10 -0
+            unsigned int count = ((ids_count >> 2 * n) & 0x3) + 1;
+            uint16_t vm_id = get_id_from_resp(&resp, id_pos);
+            struct domain *d;
+
+            /*
+             * vm_id == 0 means a notifications pending for Xen itself, but
+             * we don't support that yet.
+             */
+            if (vm_id)
+                d = ffa_rcu_lock_domain_by_vm_id(vm_id);

I am still unconvinced that this is sufficient. This will prevent "struct domain *" to be freed. But ...

xen/arch/arm/tee/ffa_notif.c line 265

Quoted 6 lines
xen/arch/arm/tee/ffa_notif.c new file +6 -0
+            else
+                d = NULL;
+
+            if ( d )
+            {
+                struct ffa_ctx *ctx = d->arch.tee;

... I don't think it will protect d->arch.tee to be freed as this is happening during teardorwn. You mostly need some other sort of locking to avoid d->arch.tee been freed behind your back.

Quoted 29 lines
xen/arch/arm/tee/ffa_notif.c new file +29 -0
+                struct vcpu *v;
+
+                ACCESS_ONCE(ctx->notif.secure_pending) = true;
+
+                /*
+                 * Since we're only delivering global notification, always
+                 * deliver to the first online vCPU. It doesn't matter
+                 * which we chose, as long as it's available.
+                 */
+                for_each_vcpu(d, v)
+                {
+                    if ( is_vcpu_online(v) )
+                    {
+                        vgic_inject_irq(d, v, GUEST_FFA_NOTIF_PEND_INTR_ID,
+                                        true);
+                        break;
+                    }
+                }
+                if ( !v )
+                    printk(XENLOG_ERR "ffa: can't inject NPI, all vCPUs offline\n");
+
+                rcu_unlock_domain(d);
+            }
+
+            id_pos += count;
+        }
+
+    } while (resp.a2 & FFA_NOTIF_INFO_GET_MORE_FLAG);
+}

[...]

xen/arch/arm/tee/ffa_notif.c line 335

Quoted 9 lines
xen/arch/arm/tee/ffa_notif.c new file +9 -0
+static int ffa_setup_irq_callback(struct notifier_block *nfb,
+                                  unsigned long action, void *hcpu)
+{
+    unsigned int cpu = (unsigned long)hcpu;
+    struct notif_irq_info irq_info = { };
+
+    switch ( action )
+    {
+    case CPU_ONLINE:

Can't you execute the notifier in CPU_STARTING? This will be called on the CPU directly, so you should be able to use request_irq(...).

Quoted 45 lines
xen/arch/arm/tee/ffa_notif.c new file +45 -0
+        /*
+         * The notifier call is done on the primary or initiating CPU when
+         * the target CPU have come online, but the SGI must be setup on
+         * the target CPU.
+         *
+         * We make an IPI call to the target CPU to setup the SGI. The call
+         * is executed in interrupt context on the target CPU, so we can't
+         * call request_irq() directly since it allocates memory.
+         *
+         * We preallocate the needed irqaction here and pass it via the
+         * temporary struct notif_irq_info. The call is synchronous in the
+         * sense that when on_selected_cpus() returns the callback
+         * notif_irq_enable() has done the same on the target CPU.
+         *
+         * We deal with two errors, one where notif_irq_enable() hasn't
+         * been called for some reason, that error is logged below. The
+         * other where setup_irq() fails is logged in the callback. We must
+         * free the irqaction in both cases since it has failed to become
+         * registered.
+         *
+         * Failures leads to a problem notifications, the CPUs with failure
+         * will trigger on the SGI indicating that there are notifications
+         * pending, while the SPMC in the secure world will not notice that
+         * the interrupt was lost.
+         */
+        irq_info.action = xmalloc(struct irqaction);
+        if ( !irq_info.action )
+        {
+            printk(XENLOG_ERR "ffa: setup irq %u failed, out of memory\n",
+                   notif_sri_irq);
+            return -ENOMEM;
+        }
+
+        *irq_info.action = (struct irqaction){
+            .handler = notif_irq_handler,
+            .name = "FF-A notif",
+            .free_on_release = 1,
+        };
+
+        on_selected_cpus(cpumask_of(cpu), notif_irq_enable, &irq_info, 1);
+        if (!irq_info.called)
+            printk(XENLOG_ERR "ffa: on_selected_cpus(cpumask_of(%u)) failed\n",
+                   cpu);
+        /*
+         * The irqaction is unused and must be freed if irq_info.action is

xen/arch/arm/tee/ffa_notif.c line 390

Quoted 10 lines
xen/arch/arm/tee/ffa_notif.c new file +10 -0
+         * non-NULL at this stage.
+         */
+        XFREE(irq_info.action);
+        break;
+    default:
+        break;
+    }
+
+    return 0;
+}

Cheers,

Signature · 1 line
Julien Grall
#7 lore raw

Re: [XEN PATCH v4 5/5] xen/arm: ffa: support notification

Hi Julien,

Quoted 56 lines · Julien Grall

Hi Jens,

Quoted 52 lines · Jens Wiklander
xen/arch/arm/tee/ffa.c +8 -7
-static bool ffa_probe(void)
+static int __init ffa_init(void)
  {
      uint32_t vers;
      unsigned int major_vers;
@@ -460,16 +511,16 @@ 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;
+        return 0;
      }
 
      if ( !ffa_get_version(&vers) )
-        return false;
+        return 0;
 
      if ( vers < FFA_MIN_SPMC_VERSION || vers > FFA_MY_VERSION )
      {
          printk(XENLOG_ERR "ffa: Incompatible version %#x found\n", vers);
-        return false;
+        return 0;
      }
 
      major_vers = (vers >> FFA_VERSION_MAJOR_SHIFT) & FFA_VERSION_MAJOR_MASK;
@@ -492,26 +543,33 @@ 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;
+        return 0;
 
      if ( !ffa_rxtx_init() )
-        return false;
+        return 0;
 
      ffa_version = vers;
 
      if ( !ffa_partinfo_init() )
          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;
+    return 0;
 
  err_rxtx_destroy:
      ffa_rxtx_destroy();
      ffa_version = 0;
 
Quoted 7 lines

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

Quoted 4 lines
xen/arch/arm/tee/ffa.c +3 -1
-    return false;
+    return 0;
+}
+presmp_initcall(ffa_init);

I would rather prefer if tee_init() is called from presmp_initcall(). This would avoid FFA to have to try to initialize itself before all the others.

OK, I'll update.

Quoted 3 lines

This is what I had in mind with my original request, but I guess I wasn't clear enough. Sorry for that.

No worries.

Quoted 54 lines

[...]

Quoted 30 lines
xen/arch/arm/tee/ffa_notif.c new file +30 -0
+static void notif_irq_handler(int irq, void *data)
+{
+    const struct arm_smccc_1_2_regs arg = {
+        .a0 = FFA_NOTIFICATION_INFO_GET_64,
+    };
+    struct arm_smccc_1_2_regs resp;
+    unsigned int id_pos;
+    unsigned int list_count;
+    uint64_t ids_count;
+    unsigned int n;
+    int32_t res;
+
+    do {
+        arm_smccc_1_2_smc(&arg, &resp);
+        res = ffa_get_ret_code(&resp);
+        if ( res )
+        {
+            if ( res != FFA_RET_NO_DATA )
+                printk(XENLOG_ERR "ffa: notification info get failed: error %d\n",
+                       res);
+            return;
+        }
+
+        ids_count = resp.a2 >> FFA_NOTIF_INFO_GET_ID_LIST_SHIFT;
+        list_count = ( resp.a2 >> FFA_NOTIF_INFO_GET_ID_COUNT_SHIFT ) &
+                     FFA_NOTIF_INFO_GET_ID_COUNT_MASK;
+
+        id_pos = 0;
+        for ( n = 0; n < list_count; n++ )
+        {

xen/arch/arm/tee/ffa_notif.c line 259

Quoted 10 lines
xen/arch/arm/tee/ffa_notif.c new file +10 -0
+            unsigned int count = ((ids_count >> 2 * n) & 0x3) + 1;
+            uint16_t vm_id = get_id_from_resp(&resp, id_pos);
+            struct domain *d;
+
+            /*
+             * vm_id == 0 means a notifications pending for Xen itself, but
+             * we don't support that yet.
+             */
+            if (vm_id)
+                d = ffa_rcu_lock_domain_by_vm_id(vm_id);

I am still unconvinced that this is sufficient. This will prevent "struct domain *" to be freed. But ...

xen/arch/arm/tee/ffa_notif.c line 265

Quoted 6 lines
xen/arch/arm/tee/ffa_notif.c new file +6 -0
+            else
+                d = NULL;
+
+            if ( d )
+            {
+                struct ffa_ctx *ctx = d->arch.tee;
Quoted 3 lines

... I don't think it will protect d->arch.tee to be freed as this is happening during teardorwn. You mostly need some other sort of locking to avoid d->arch.tee been freed behind your back.

OK, I'll need to work more on this. The outcome of the [1] thread may affect this too.

[1] https://lists.xenproject.org/archives/html/xen-devel/2024-05/msg00156.html

Quoted 33 lines
Quoted 30 lines
xen/arch/arm/tee/ffa_notif.c new file +29 -0
+                struct vcpu *v;
+
+                ACCESS_ONCE(ctx->notif.secure_pending) = true;
+
+                /*
+                 * Since we're only delivering global notification, always
+                 * deliver to the first online vCPU. It doesn't matter
+                 * which we chose, as long as it's available.
+                 */
+                for_each_vcpu(d, v)
+                {
+                    if ( is_vcpu_online(v) )
+                    {
+                        vgic_inject_irq(d, v, GUEST_FFA_NOTIF_PEND_INTR_ID,
+                                        true);
+                        break;
+                    }
+                }
+                if ( !v )
+                    printk(XENLOG_ERR "ffa: can't inject NPI, all vCPUs offline\n");
+
+                rcu_unlock_domain(d);
+            }
+
+            id_pos += count;
+        }
+
+    } while (resp.a2 & FFA_NOTIF_INFO_GET_MORE_FLAG);
+}

[...]

Quoted 12 lines

xen/arch/arm/tee/ffa_notif.c line 335

Quoted 9 lines
xen/arch/arm/tee/ffa_notif.c new file +9 -0
+static int ffa_setup_irq_callback(struct notifier_block *nfb,
+                                  unsigned long action, void *hcpu)
+{
+    unsigned int cpu = (unsigned long)hcpu;
+    struct notif_irq_info irq_info = { };
+
+    switch ( action )
+    {
+    case CPU_ONLINE:

Can't you execute the notifier in CPU_STARTING? This will be called on the CPU directly, so you should be able to use request_irq(...).

I tried that first but it failed with the ASSERT_ALLOC_CONTEXT() in _xmalloc().

I've also tested a three-step solution with CPU_UP_PREPARE, CPU_STARTING, and CPU_UP_CANCELED.
My approach here is more direct, but it still suffers from a weakness in error handling even if it seems quite unlikely to run out of heap or for setup_irq() to fail at this stage.

Thanks,
Jens

Quoted 61 lines
Quoted 46 lines
xen/arch/arm/tee/ffa_notif.c new file +45 -0
+        /*
+         * The notifier call is done on the primary or initiating CPU when
+         * the target CPU have come online, but the SGI must be setup on
+         * the target CPU.
+         *
+         * We make an IPI call to the target CPU to setup the SGI. The call
+         * is executed in interrupt context on the target CPU, so we can't
+         * call request_irq() directly since it allocates memory.
+         *
+         * We preallocate the needed irqaction here and pass it via the
+         * temporary struct notif_irq_info. The call is synchronous in the
+         * sense that when on_selected_cpus() returns the callback
+         * notif_irq_enable() has done the same on the target CPU.
+         *
+         * We deal with two errors, one where notif_irq_enable() hasn't
+         * been called for some reason, that error is logged below. The
+         * other where setup_irq() fails is logged in the callback. We must
+         * free the irqaction in both cases since it has failed to become
+         * registered.
+         *
+         * Failures leads to a problem notifications, the CPUs with failure
+         * will trigger on the SGI indicating that there are notifications
+         * pending, while the SPMC in the secure world will not notice that
+         * the interrupt was lost.
+         */
+        irq_info.action = xmalloc(struct irqaction);
+        if ( !irq_info.action )
+        {
+            printk(XENLOG_ERR "ffa: setup irq %u failed, out of memory\n",
+                   notif_sri_irq);
+            return -ENOMEM;
+        }
+
+        *irq_info.action = (struct irqaction){
+            .handler = notif_irq_handler,
+            .name = "FF-A notif",
+            .free_on_release = 1,
+        };
+
+        on_selected_cpus(cpumask_of(cpu), notif_irq_enable, &irq_info, 1);
+        if (!irq_info.called)
+            printk(XENLOG_ERR "ffa: on_selected_cpus(cpumask_of(%u)) failed\n",
+                   cpu);
+        /*
+         * The irqaction is unused and must be freed if irq_info.action is

xen/arch/arm/tee/ffa_notif.c line 390

Quoted 10 lines
xen/arch/arm/tee/ffa_notif.c new file +10 -0
+         * non-NULL at this stage.
+         */
+        XFREE(irq_info.action);
+        break;
+    default:
+        break;
+    }
+
+    return 0;
+}

Cheers,

--
Julien Grall

#8 lore raw

Re: [XEN PATCH v4 5/5] xen/arm: ffa: support notification

Hi Jens,

Quoted 13 lines · Jens Wiklander
Quoted 4 lines
Quoted 4 lines
(quoted diff) +4 -0
+static int ffa_setup_irq_callback(struct notifier_block *nfb,
+                                  unsigned long action, void *hcpu)
+{
+    unsigned int cpu = (unsigned long)hcpu;
Quoted 8 lines

Review

Quoted 5 lines
(quoted diff) +5 -0
+    struct notif_irq_info irq_info = { };
+
+    switch ( action )
+    {
+    case CPU_ONLINE:

Can't you execute the notifier in CPU_STARTING? This will be called on the CPU directly, so you should be able to use request_irq(...).

Quoted 7 lines

I tried that first but it failed with the ASSERT_ALLOC_CONTEXT() in _xmalloc().

I've also tested a three-step solution with CPU_UP_PREPARE, CPU_STARTING, and CPU_UP_CANCELED.
My approach here is more direct, but it still suffers from a weakness in error handling even if it seems quite unlikely to run out of heap or for setup_irq() to fail at this stage.

Ah I didn't notice that notify_cpu_starting() is called with IRQ disabled. I assumed they would be enabled.

Then I would consider to do:

xen/arch/arm/smpboot.c +1 -0
index 6efed876782e..db322672e508 100644
@@ -389,6 +389,7 @@ void asmlinkage start_secondary(void)
      */
     init_maintenance_interrupt();
     init_timer_interrupt();
+    init_tee_interrupt();
 
     local_abort_enable();
 

And plumb through the TEE subsystem.

Cheers,

Signature · 1 line
Julien Grall
#9 lore raw

Re: [XEN PATCH v4 5/5] xen/arm: ffa: support notification

Hi Julien,

Quoted 42 lines · Julien Grall

Hi Jens,

Quoted 13 lines · Jens Wiklander
Quoted 4 lines
Quoted 4 lines
(quoted diff) +4 -0
+static int ffa_setup_irq_callback(struct notifier_block *nfb,
+                                  unsigned long action, void *hcpu)
+{
+    unsigned int cpu = (unsigned long)hcpu;
Quoted 8 lines

Review

Quoted 5 lines
(quoted diff) +5 -0
+    struct notif_irq_info irq_info = { };
+
+    switch ( action )
+    {
+    case CPU_ONLINE:

Can't you execute the notifier in CPU_STARTING? This will be called on the CPU directly, so you should be able to use request_irq(...).

Quoted 7 lines

I tried that first but it failed with the ASSERT_ALLOC_CONTEXT() in _xmalloc().

I've also tested a three-step solution with CPU_UP_PREPARE, CPU_STARTING, and CPU_UP_CANCELED.
My approach here is more direct, but it still suffers from a weakness in error handling even if it seems quite unlikely to run out of heap or for setup_irq() to fail at this stage.

Ah I didn't notice that notify_cpu_starting() is called with IRQ disabled. I assumed they would be enabled.

Then I would consider to do:

xen/arch/arm/smpboot.c +1 -0
index 6efed876782e..db322672e508 100644
@@ -389,6 +389,7 @@ void asmlinkage start_secondary(void)
       */
      init_maintenance_interrupt();
      init_timer_interrupt();
+    init_tee_interrupt();
 
      local_abort_enable();
 
Quoted 1 line

And plumb through the TEE subsystem.

I'll use that in the next version, it should remove a lot of complex code.

Thanks,
Jens