Skip to contentSkip to conversation

Add SMMUv3 Stage 1 Support for Xen guests

PATCH Superseded Milan Djokic 8 participants 124 messages Mar 23 – Jul 29 Page 2 of 2

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#95 lore raw

Re: [PATCH v3 11/23] xen/arm: vsmmuv3: Attach Stage-1 configuration to SMMUv3 hardware

In reply to Milan Djokic
Quoted 11 lines · Milan Djokic
xen/include/xen/iommu.h +1 -0
@@ -312,6 +312,7 @@ static inline int iommu_add_dt_pci_sideband_ids(struct pci_dev *pdev)
 #endif /* HAS_DEVICE_TREE_DISCOVERY */
 
 struct page_info;
+struct iommu_guest_config;
 
 /*
  * Any non-zero value returned from callbacks of this type will cause the

xen/include/xen/iommu.h struct iommu_ops { · line 393

Quoted 6 lines
xen/include/xen/iommu.h +3 -0
@@ -387,6 +388,11 @@ struct iommu_ops {
 #endif
     /* Inhibit all interrupt generation, to be used at shutdown. */
     void (*quiesce)(void);
+
+#ifdef CONFIG_ARM
+    int (*attach_guest_config)(struct domain *d, u32 sid,

Nit: uint32_t please, provided a fixed-width type is actually warranted here (see ./CODING_STYLE).

xen/include/xen/iommu.h struct iommu_ops { · line 394

Quoted 1 line
xen/include/xen/iommu.h +1 -0
+                               struct iommu_guest_config *cfg);

Pointer-to-const?

Jan

#96 lore raw

Re: [PATCH v3 11/23] xen/arm: vsmmuv3: Attach Stage-1 configuration to SMMUv3 hardware

In reply to Milan Djokic

Hi Milan,

Quoted 40 lines · Milan Djokic

From: Rahul Singh <[email protected]>

Attach the Stage-1 configuration to device STE to support nested translation for the guests.

---

xen/arch/arm/include/asm/iommu.h       |  7 +++
xen/drivers/passthrough/arm/smmu-v3.c  | 79 ++++++++++++++++++++++++++
xen/drivers/passthrough/arm/smmu-v3.h  |  1 +
xen/drivers/passthrough/arm/vsmmu-v3.c | 18 ++++++
xen/include/xen/iommu.h                |  6 ++
5 files changed, 111 insertions(+)
xen/arch/arm/include/asm/iommu.h +7 -0
index ad15477e24..56bc9314a7 100644
@@ -20,6 +20,13 @@ struct arch_iommu
    void *priv;
 };
 
+struct iommu_guest_config {
+    paddr_t     s1ctxptr;
+    uint8_t     config;
+    uint8_t     s1fmt;
+    uint8_t     s1cdmax;
+};
+
 const struct iommu_ops *iommu_get_ops(void);
 void iommu_set_ops(const struct iommu_ops *ops);
 
xen/drivers/passthrough/arm/smmu-v3.c +0 -0
index 87612df21d..cf8f638a49 100644

xen/drivers/passthrough/arm/smmu-v3.c static struct arm_smmu_device *arm_smmu_get_by_dev(const struct device *dev) · line 2814

Quoted 5 lines
xen/drivers/passthrough/arm/smmu-v3.c +2 -0
@@ -2810,6 +2810,37 @@ static struct arm_smmu_device *arm_smmu_get_by_dev(const struct device *dev)
 return NULL;
 }
 
+static struct iommu_domain *arm_smmu_get_domain_by_sid(struct domain *d,
+ u32 sid)

I think this might be wrong, a system can have multiple SMMU and the SID is unique only on each SMMU, not on the overall platform, I think arm_smmu_attach_guest_config should pass also the smmu for the selected sid.

Quoted 46 lines
xen/drivers/passthrough/arm/smmu-v3.c +39 -0
+{
+ int i;
+ unsigned long flags;
+ struct iommu_domain *io_domain;
+ struct arm_smmu_domain *smmu_domain;
+ struct arm_smmu_master *master;
+ struct arm_smmu_xen_domain *xen_domain = dom_iommu(d)->arch.priv;
+
+ /*
+ * Loop through the &xen_domain->contexts to locate a context
+ * assigned to this SMMU
+ */
+ list_for_each_entry(io_domain, &xen_domain->contexts, list) {
+ smmu_domain = to_smmu_domain(io_domain);
+
+ spin_lock_irqsave(&smmu_domain->devices_lock, flags);
+ list_for_each_entry(master, &smmu_domain->devices, domain_head) {
+ for (i = 0; i < master->num_streams; i++) {
+ if (sid != master->streams[i].id)
+ continue;
+ spin_unlock_irqrestore(&smmu_domain->devices_lock, flags);
+ return io_domain;
+ }
+ }
+ spin_unlock_irqrestore(&smmu_domain->devices_lock, flags);
+ }
+ return NULL;
+}
+
 static struct iommu_domain *arm_smmu_get_domain(struct domain *d,
 struct device *dev)
 {
@@ -3022,6 +3053,53 @@ static void arm_smmu_iommu_xen_domain_teardown(struct domain *d)
 xfree(xen_domain);
 }
 
+static int arm_smmu_attach_guest_config(struct domain *d, u32 sid,
+ struct iommu_guest_config *cfg)
+{
+ int ret = -EINVAL;
+ unsigned long flags;
+ struct arm_smmu_master *master;
+ struct arm_smmu_domain *smmu_domain;
+ struct arm_smmu_xen_domain *xen_domain = dom_iommu(d)->arch.priv;
+ struct iommu_domain *io_domain = arm_smmu_get_domain_by_sid(d, sid);
+

xen/drivers/passthrough/arm/smmu-v3.c static void arm_smmu_iommu_xen_domain_teardown(struct domain *d) · line 3075

Quoted 10 lines
xen/drivers/passthrough/arm/smmu-v3.c +10 -0
+ if (!io_domain)
+ return -ENODEV;
+
+ smmu_domain = to_smmu_domain(io_domain);
+
+ spin_lock(&xen_domain->lock);
+
+ switch (cfg->config) {
+ case ARM_SMMU_DOMAIN_ABORT:
+ smmu_domain->abort = true;

Shold we change also smmu_domain->stage here and ...

xen/drivers/passthrough/arm/smmu-v3.c static void arm_smmu_iommu_xen_domain_teardown(struct domain *d) · line 3078

Quoted 3 lines
xen/drivers/passthrough/arm/smmu-v3.c +3 -0
+ break;
+ case ARM_SMMU_DOMAIN_BYPASS:
+ smmu_domain->abort = false;

Here? I see arm_smmu_write_strtab_ent() uses it to select s1_cfg or s2_cfg configuration

Quoted 28 lines
xen/drivers/passthrough/arm/smmu-v3.c +24 -0
+ break;
+ case ARM_SMMU_DOMAIN_NESTED:
+ /* Enable Nested stage translation. */
+ smmu_domain->stage = ARM_SMMU_DOMAIN_NESTED;
+ smmu_domain->s1_cfg.s1ctxptr = cfg->s1ctxptr;
+ smmu_domain->s1_cfg.s1fmt = cfg->s1fmt;
+ smmu_domain->s1_cfg.s1cdmax = cfg->s1cdmax;
+ smmu_domain->abort = false;
+ break;
+ default:
+ goto out;
+ }
+
+ spin_lock_irqsave(&smmu_domain->devices_lock, flags);
+ list_for_each_entry(master, &smmu_domain->devices, domain_head)
+ arm_smmu_install_ste_for_dev(master);
+ spin_unlock_irqrestore(&smmu_domain->devices_lock, flags);
+
+ ret = 0;
+out:
+ spin_unlock(&xen_domain->lock);
+ return ret;
+}
+
 static const struct iommu_ops arm_smmu_iommu_ops = {
 .page_sizes = PAGE_SIZE_4K,
 .init = arm_smmu_iommu_xen_domain_init,

xen/drivers/passthrough/arm/smmu-v3.c static const struct iommu_ops arm_smmu_iommu_ops = { · line 3118

Quoted 7 lines
xen/drivers/passthrough/arm/smmu-v3.c +1 -0
@@ -3034,6 +3112,7 @@ static const struct iommu_ops arm_smmu_iommu_ops = {
 .unmap_page = arm_iommu_unmap_page,
 .dt_xlate = arm_smmu_dt_xlate,
 .add_device = arm_smmu_add_device,
+ .attach_guest_config = arm_smmu_attach_guest_config
 };
 
 static __init int arm_smmu_dt_init(struct dt_device_node *dev,

Cheers,
Luca

#97 lore raw

Re: [PATCH v3 11/23] xen/arm: vsmmuv3: Attach Stage-1 configuration to SMMUv3 hardware

HI Milan,

Quoted 48 lines · Luca Fancellu

Hi Milan,

Quoted 43 lines · Milan Djokic

From: Rahul Singh <[email protected]>

Attach the Stage-1 configuration to device STE to support nested translation for the guests.

---

xen/arch/arm/include/asm/iommu.h       |  7 +++
xen/drivers/passthrough/arm/smmu-v3.c  | 79 ++++++++++++++++++++++++++
xen/drivers/passthrough/arm/smmu-v3.h  |  1 +
xen/drivers/passthrough/arm/vsmmu-v3.c | 18 ++++++
xen/include/xen/iommu.h                |  6 ++
5 files changed, 111 insertions(+)
xen/arch/arm/include/asm/iommu.h +7 -0
index ad15477e24..56bc9314a7 100644
@@ -20,6 +20,13 @@ struct arch_iommu
   void *priv;
 };
 
+struct iommu_guest_config {
+    paddr_t     s1ctxptr;
+    uint8_t     config;
+    uint8_t     s1fmt;
+    uint8_t     s1cdmax;
+};
+
 const struct iommu_ops *iommu_get_ops(void);
 void iommu_set_ops(const struct iommu_ops *ops);
 
xen/drivers/passthrough/arm/smmu-v3.c +0 -0
index 87612df21d..cf8f638a49 100644
@@ -2810,6 +2810,37 @@ static struct arm_smmu_device *arm_smmu_get_by_dev(const struct device *dev)
 return NULL;
 }
 
Quoted 6 lines

xen/drivers/passthrough/arm/smmu-v3.c static struct arm_smmu_device *arm_smmu_get_by_dev(const struct device *dev) · line 2814

Quoted 2 lines
xen/drivers/passthrough/arm/smmu-v3.c +2 -0
+static struct iommu_domain *arm_smmu_get_domain_by_sid(struct domain *d,
+ u32 sid)

I think this might be wrong, a system can have multiple SMMU and the SID is unique only on each SMMU, not on the overall platform, I think arm_smmu_attach_guest_config should pass also the smmu for the selected sid.

Apologies, I’ve read the design, I think this is intentional. I withdraw this comment.

Cheers,
Luca

#98 lore raw

PATCH v3 12/23 xen/arm: vsmmuv3: Add support for event queue and global error

In reply to Milan Djokic

From: Rahul Singh <[email protected]>

Event queue is used to send the events to guest when there is an events/ faults. Add support for event queue to send events to guest.

Global error in SMMUv3 hw will be updated in smmu_gerror and smmu_gerrorn register. Add support for global error registers to send global error to guest.

---
 xen/drivers/passthrough/arm/smmu-v3.h  |  20 +++
 xen/drivers/passthrough/arm/vsmmu-v3.c | 163 ++++++++++++++++++++++++-
 xen/include/public/arch-arm.h          |   5 +-
 3 files changed, 183 insertions(+), 5 deletions(-)
xen/drivers/passthrough/arm/smmu-v3.h +20 -0
index df3b7ec1b5..8d3e1877aa 100644
@@ -354,6 +354,26 @@
 
 #define EVTQ_0_ID			GENMASK_ULL(7, 0)
 
+#define EVT_ID_BAD_STREAMID		0x02
+#define EVT_ID_BAD_STE		    	0x04
+#define EVT_ID_TRANSLATION_FAULT	0x10
+#define EVT_ID_ADDR_SIZE_FAULT		0x11
+#define EVT_ID_ACCESS_FAULT		0x12
+#define EVT_ID_PERMISSION_FAULT		0x13
+
+#define EVTQ_0_SSV			(1UL << 11)
+#define EVTQ_0_SSID			GENMASK_ULL(31, 12)
+#define EVTQ_0_SID			GENMASK_ULL(63, 32)
+#define EVTQ_1_STAG			GENMASK_ULL(15, 0)
+#define EVTQ_1_STALL			(1UL << 31)
+#define EVTQ_1_PnU			(1UL << 33)
+#define EVTQ_1_InD			(1UL << 34)
+#define EVTQ_1_RnW			(1UL << 35)
+#define EVTQ_1_S2			(1UL << 39)
+#define EVTQ_1_CLASS			GENMASK_ULL(41, 40)
+#define EVTQ_1_TT_READ			(1UL << 44)
+#define EVTQ_2_ADDR			GENMASK_ULL(63, 0)
+#define EVTQ_3_IPA			GENMASK_ULL(51, 12)
 /* PRI queue */
 #define PRIQ_ENT_SZ_SHIFT		4
 #define PRIQ_ENT_DWORDS			((1 << PRIQ_ENT_SZ_SHIFT) >> 3)
xen/drivers/passthrough/arm/vsmmu-v3.c +160 -3

251 lines not on this page · raw message

xen/include/public/arch-arm.h +3 -2
index ebac02ed63..1b606e20fd 100644
@@ -527,9 +527,10 @@ typedef uint64_t xen_callback_t;
 #define GUEST_EVTCHN_PPI        31
 
 #define GUEST_VPL011_SPI        32
+#define GUEST_VSMMU_SPI         33
 
-#define GUEST_VIRTIO_MMIO_SPI_FIRST   33
-#define GUEST_VIRTIO_MMIO_SPI_LAST    43
+#define GUEST_VIRTIO_MMIO_SPI_FIRST   34
+#define GUEST_VIRTIO_MMIO_SPI_LAST    44
 
 /*
  * SGI is the preferred delivery mechanism of FF-A pending notifications or
Signature · 1 line
2.43.0
#99 lore raw

Re: [PATCH v3 12/23] xen/arm: vsmmuv3: Add support for event queue and global error

Hi Milan,

Quoted 88 lines
xen/drivers/passthrough/arm/vsmmu-v3.c +47 -0
index 6d3636b18b..7a6c18df53 100644
@@ -44,6 +44,7 @@ extern const struct viommu_desc __read_mostly *cur_viommu;
 
 /* Helper Macros */
 #define smmu_get_cmdq_enabled(x)    FIELD_GET(CR0_CMDQEN, x)
+#define smmu_get_evtq_enabled(x)    FIELD_GET(CR0_EVTQEN, x)
 #define smmu_cmd_get_command(x)     FIELD_GET(CMDQ_0_OP, x)
 #define smmu_cmd_get_sid(x)         FIELD_GET(CMDQ_PREFETCH_0_SID, x)
 #define smmu_get_ste_s1cdmax(x)     FIELD_GET(STRTAB_STE_0_S1CDMAX, x)
@@ -52,6 +53,35 @@ extern const struct viommu_desc __read_mostly *cur_viommu;
 #define smmu_get_ste_s1ctxptr(x)    FIELD_PREP(STRTAB_STE_0_S1CTXPTR_MASK, \
                                    FIELD_GET(STRTAB_STE_0_S1CTXPTR_MASK, x))
 
+/* event queue entry */
+struct arm_smmu_evtq_ent {
+    /* Common fields */
+    uint8_t     opcode;
+    uint32_t    sid;
+
+    /* Event-specific fields */
+    union {
+        struct {
+            uint32_t ssid;
+            bool ssv;
+        } c_bad_ste_streamid;
+
+        struct {
+            bool stall;
+            uint16_t stag;
+            uint32_t ssid;
+            bool ssv;
+            bool s2;
+            uint64_t addr;
+            bool rnw;
+            bool pnu;
+            bool ind;
+            uint8_t class;
+            uint64_t addr2;
+        } f_translation;
+    };
+};
+
 /* stage-1 translation configuration */
 struct arm_vsmmu_s1_trans_cfg {
    paddr_t s1ctxptr;
@@ -82,6 +112,7 @@ struct virt_smmu {
    uint32_t    strtab_base_cfg;
    uint64_t    strtab_base;
    uint32_t    irq_ctrl;
+    uint32_t    virq;
    uint64_t    gerror_irq_cfg0;
    uint64_t    evtq_irq_cfg0;
    struct      arm_vsmmu_queue evtq, cmdq;
@@ -89,6 +120,12 @@ struct virt_smmu {
 };
 
 /* Queue manipulation functions */
+static bool queue_full(struct arm_vsmmu_queue *q)
+{
+    return Q_IDX(q, q->prod) == Q_IDX(q, q->cons) &&
+           Q_WRP(q, q->prod) != Q_WRP(q, q->cons);
+}
+
 static bool queue_empty(struct arm_vsmmu_queue *q)
 {
    return Q_IDX(q, q->prod) == Q_IDX(q, q->cons) &&
@@ -101,11 +138,105 @@ static void queue_inc_cons(struct arm_vsmmu_queue *q)
    q->cons = Q_OVF(q->cons) | Q_WRP(q, cons) | Q_IDX(q, cons);
 }
 
+static void queue_inc_prod(struct arm_vsmmu_queue *q)
+{
+    u32 prod = (Q_WRP(q, q->prod) | Q_IDX(q, q->prod)) + 1;
+    q->prod = Q_OVF(q->prod) | Q_WRP(q, prod) | Q_IDX(q, prod);
+}
+
 static void dump_smmu_command(uint64_t *command)
 {
    gdprintk(XENLOG_ERR, "cmd 0x%02llx: %016lx %016lx\n",
             smmu_cmd_get_command(command[0]), command[0], command[1]);
 }
+
+static void arm_vsmmu_inject_irq(struct virt_smmu *smmu, bool is_gerror,
+                                uint32_t gerror_err)
+{

xen/drivers/passthrough/arm/vsmmu-v3.c static void queue_inc_cons(struct arm_vsmmu_queue *q) · line 165

Quoted 10 lines
xen/drivers/passthrough/arm/vsmmu-v3.c +10 -0
+    uint32_t new_gerrors, pending;
+
+    if ( is_gerror )
+    {
+        /* trigger global error irq to guest */
+        pending = smmu->gerror ^ smmu->gerrorn;
+        new_gerrors = ~pending & gerror_err;
+
+        /* only toggle non pending errors */
+        if (!new_gerrors)

NIT: codestyle, spaces inside parentheses

xen/drivers/passthrough/arm/vsmmu-v3.c static void queue_inc_cons(struct arm_vsmmu_queue *q) · line 171

Quoted 6 lines
xen/drivers/passthrough/arm/vsmmu-v3.c +6 -0
+            return;
+
+        smmu->gerror ^= new_gerrors;
+    }
+
+    vgic_inject_irq(smmu->d, NULL, smmu->virq, true);

I don’t understand this, shouldn’t we have the irq level high or low depending on event queue or global error pending? Also this doesn’t take into consideration irq enabled (IRQ_CTRL_EVTQ_IRQEN / IRQ_CTRL_GERROR_IRQEN)

Quoted 35 lines
xen/drivers/passthrough/arm/vsmmu-v3.c +35 -0
+}
+
+static int arm_vsmmu_write_evtq(struct virt_smmu *smmu, uint64_t *evt)
+{
+    struct arm_vsmmu_queue *q = &smmu->evtq;
+    struct domain *d = smmu->d;
+    paddr_t addr;
+    int ret;
+
+    if ( !smmu_get_evtq_enabled(smmu->cr[0]) )
+        return -EINVAL;
+
+    if ( queue_full(q) )
+        return -EINVAL;
+
+    addr = Q_PROD_ENT(q);
+    ret = access_guest_memory_by_gpa(d, addr, evt,
+                                     sizeof(*evt) * EVTQ_ENT_DWORDS, true);
+    if ( ret )
+        return ret;
+
+    queue_inc_prod(q);
+
+    /* trigger eventq irq to guest */
+    if ( !queue_empty(q) )
+        arm_vsmmu_inject_irq(smmu, false, 0);
+
+    return 0;
+}
+
+void arm_vsmmu_send_event(struct virt_smmu *smmu,
+                          struct arm_smmu_evtq_ent *ent)
+{
+    uint64_t evt[EVTQ_ENT_DWORDS];
+    int ret;

xen/drivers/passthrough/arm/vsmmu-v3.c static void queue_inc_cons(struct arm_vsmmu_queue *q) · line 216

Quoted 10 lines
xen/drivers/passthrough/arm/vsmmu-v3.c +10 -0
+
+    memset(evt, 0, 1 << EVTQ_ENT_SZ_SHIFT);
+
+    if ( !smmu_get_evtq_enabled(smmu->cr[0]) )
+        return;
+
+    evt[0] |= FIELD_PREP(EVTQ_0_ID, ent->opcode);
+    evt[0] |= FIELD_PREP(EVTQ_0_SID, ent->sid);
+
+    switch (ent->opcode)

NIT: codestyle, spaces inside parentheses

xen/drivers/passthrough/arm/vsmmu-v3.c static int vsmmuv3_init_single(struct domain *d, paddr_t addr, paddr_t size)

Quoted 92 lines
xen/drivers/passthrough/arm/vsmmu-v3.c +44 -0
+    {
+    case EVT_ID_BAD_STREAMID:
+    case EVT_ID_BAD_STE:
+        evt[0] |= FIELD_PREP(EVTQ_0_SSID, ent->c_bad_ste_streamid.ssid);
+        evt[0] |= FIELD_PREP(EVTQ_0_SSV, ent->c_bad_ste_streamid.ssv);
+        break;
+    case EVT_ID_TRANSLATION_FAULT:
+    case EVT_ID_ADDR_SIZE_FAULT:
+    case EVT_ID_ACCESS_FAULT:
+    case EVT_ID_PERMISSION_FAULT:
+        break;
+    default:
+        gdprintk(XENLOG_WARNING, "vSMMUv3: event opcode is bad\n");
+        break;
+    }
+
+    ret = arm_vsmmu_write_evtq(smmu, evt);
+    if ( ret )
+        arm_vsmmu_inject_irq(smmu, true, GERROR_EVTQ_ABT_ERR);
+
+    return;
+}
+
 static int arm_vsmmu_find_ste(struct virt_smmu *smmu, uint32_t sid,
                              uint64_t *ste)
 {
@@ -114,11 +245,22 @@ static int arm_vsmmu_find_ste(struct virt_smmu *smmu, uint32_t sid,
    uint32_t log2size;
    int strtab_size_shift;
    int ret;
+    struct arm_smmu_evtq_ent ent = {
+        .sid = sid,
+        .c_bad_ste_streamid = {
+            .ssid = 0,
+            .ssv = false,
+        },
+    };
 
    log2size = FIELD_GET(STRTAB_BASE_CFG_LOG2SIZE, smmu->strtab_base_cfg);
 
    if ( sid >= (1 << MIN(log2size, SMMU_IDR1_SIDSIZE)) )
+    {
+        ent.opcode = EVT_ID_BAD_STE;
+        arm_vsmmu_send_event(smmu, &ent);
        return -EINVAL;
+    }
 
    if ( smmu->features & STRTAB_BASE_CFG_FMT_2LVL )
    {
@@ -156,6 +298,8 @@ static int arm_vsmmu_find_ste(struct virt_smmu *smmu, uint32_t sid,
        {
            gdprintk(XENLOG_ERR, "idx=%d > max_l2_ste=%d\n",
                     idx, max_l2_ste);
+            ent.opcode = EVT_ID_BAD_STREAMID;
+            arm_vsmmu_send_event(smmu, &ent);
            return -EINVAL;
        }
        addr = l2ptr + idx * sizeof(*ste) * STRTAB_STE_DWORDS;
@@ -183,6 +327,14 @@ static int arm_vsmmu_decode_ste(struct virt_smmu *smmu, uint32_t sid,
                                uint64_t *ste)
 {
    uint64_t val = ste[0];
+    struct arm_smmu_evtq_ent ent = {
+        .opcode = EVT_ID_BAD_STE,
+        .sid = sid,
+        .c_bad_ste_streamid = {
+            .ssid = 0,
+            .ssv = false,
+        },
+    };
 
    if ( !(val & STRTAB_STE_0_V) )
        return -EAGAIN;
@@ -217,6 +369,7 @@ static int arm_vsmmu_decode_ste(struct virt_smmu *smmu, uint32_t sid,
    return 0;
 

bad_ste:

+    arm_vsmmu_send_event(smmu, &ent);
    return -EINVAL;
}
xen/drivers/passthrough/arm/vsmmu-v3.c +2 -1
@@ -577,7 +730,8 @@ static const struct mmio_handler_ops vsmmuv3_mmio_handler = {
    .write = vsmmuv3_mmio_write,
 };
 
-static int vsmmuv3_init_single(struct domain *d, paddr_t addr, paddr_t size)
+static int vsmmuv3_init_single(struct domain *d, paddr_t addr,
+                               paddr_t size, uint32_t virq)
 {
    struct virt_smmu *smmu;
 
@@ -586,6 +740,7 @@ static int vsmmuv3_init_single(struct domain *d, paddr_t addr, paddr_t size)

We are now injecting irqs due to this patch, we should vgic_reserve_virq() it as we do in patch “xen/arm: vsmmuv3: Alloc virq for virtual SMMUv3”, so maybe better to move these changes here.

Quoted 31 lines
xen/drivers/passthrough/arm/vsmmu-v3.c +5 -2
        return -ENOMEM;
 
    smmu->d = d;
+    smmu->virq = virq;
    smmu->cmdq.q_base = FIELD_PREP(Q_BASE_LOG2SIZE, SMMU_CMDQS);
    smmu->cmdq.ent_size = CMDQ_ENT_DWORDS * DWORDS_BYTES;
    smmu->evtq.q_base = FIELD_PREP(Q_BASE_LOG2SIZE, SMMU_EVTQS);
@@ -612,14 +767,16 @@ int domain_vsmmuv3_init(struct domain *d)
 
        list_for_each_entry(hw_iommu, &host_iommu_list, entry)
        {
-            ret = vsmmuv3_init_single(d, hw_iommu->addr, hw_iommu->size);
+            ret = vsmmuv3_init_single(d, hw_iommu->addr, hw_iommu->size,
+                                      hw_iommu->irq);
            if ( ret )
                return ret;
        }
    }
    else
    {
-        ret = vsmmuv3_init_single(d, GUEST_VSMMUV3_BASE, GUEST_VSMMUV3_SIZE);
+        ret = vsmmuv3_init_single(d, GUEST_VSMMUV3_BASE, GUEST_VSMMUV3_SIZE,
+                                  GUEST_VSMMU_SPI);
        if ( ret )
            return ret;
    }
xen/include/public/arch-arm.h +0 -0
index ebac02ed63..1b606e20fd 100644

xen/include/public/arch-arm.h typedef uint64_t xen_callback_t; · line 530

Quoted 4 lines
xen/include/public/arch-arm.h +1 -0
@@ -527,9 +527,10 @@ typedef uint64_t xen_callback_t;
 #define GUEST_EVTCHN_PPI        31
 
 #define GUEST_VPL011_SPI        32
+#define GUEST_VSMMU_SPI         33

This change alone breaks already built toolstack, can we just have GUEST_VSMMU_SPI as 44 and leave GUEST_VIRTIO_MMIO_SPI_FIRST and GUEST_VIRTIO_MMIO_SPI_LAST as they are?

xen/include/public/arch-arm.h typedef uint64_t xen_callback_t;

Quoted 9 lines
xen/include/public/arch-arm.h +2 -2
-#define GUEST_VIRTIO_MMIO_SPI_FIRST   33
-#define GUEST_VIRTIO_MMIO_SPI_LAST    43
+#define GUEST_VIRTIO_MMIO_SPI_FIRST   34
+#define GUEST_VIRTIO_MMIO_SPI_LAST    44
 
 /*
 * SGI is the preferred delivery mechanism of FF-A pending notifications or
 

Cheers,
Luca

#100 lore raw

PATCH v3 13/23 xen/arm: vsmmuv3: Add "iommus" property node for dom0 devices

In reply to Milan Djokic

From: Rahul Singh <[email protected]>

"iommus" property will be added for dom0 devices to virtual IOMMU node to enable the dom0 linux kernel to configure the IOMMU

---
 xen/arch/arm/domain_build.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
xen/arch/arm/domain_build.c +5 -2
index a51563ee3d..30a4f1fd09 100644
@@ -538,9 +538,12 @@ static int __init write_properties(struct domain *d, struct kernel_info *kinfo,
             continue;
         }
 
-        if ( iommu_node )
+        /*
+         * Expose IOMMU specific properties to hwdom when vIOMMU is
+         * enabled.
+         */
+        if ( iommu_node && !is_viommu_enabled() )
         {
-            /* Don't expose IOMMU specific properties to hwdom */
             if ( dt_property_name_is_equal(prop, "iommus") )
                 continue;
 
Signature · 1 line
2.43.0
#101 lore raw

Re: [PATCH v3 13/23] xen/arm: vsmmuv3: Add "iommus" property node for dom0 devices

Hi Milan,

Quoted 23 lines · Milan Djokic

From: Rahul Singh <[email protected]>

"iommus" property will be added for dom0 devices to virtual IOMMU node to enable the dom0 linux kernel to configure the IOMMU

---
xen/arch/arm/domain_build.c | 7 +++++--

1 file changed, 5 insertions(+), 2 deletions(-)
xen/arch/arm/domain_build.c +0 -1
index a51563ee3d..30a4f1fd09 100644
@@ -538,9 +538,12 @@ static int __init write_properties(struct domain *d, struct kernel_info *kinfo,
            continue;
        }
 
-        if ( iommu_node )

xen/arch/arm/domain_build.c static int __init write_properties(struct domain *d, struct kernel_info *kinfo, · line 549

Quoted 10 lines
xen/arch/arm/domain_build.c +5 -1
+        /*
+         * Expose IOMMU specific properties to hwdom when vIOMMU is
+         * enabled.
+         */
+        if ( iommu_node && !is_viommu_enabled() )
        {
-            /* Don't expose IOMMU specific properties to hwdom */
            if ( dt_property_name_is_equal(prop, "iommus") )
                continue;
 

I think this commit should be squashed with "xen/arm: vIOMMU: IOMMU device tree node for dom0” so that we can have a consistent handling of the DT properties.

Cheers,
Luca

#102 lore raw

PATCH v3 14/23 xen/arm: vIOMMU: IOMMU device tree node for dom0

In reply to Milan Djokic

From: Rahul Singh <[email protected]>

XEN will create an IOMMU device tree node in the device tree to enable the dom0 to discover the virtual SMMUv3 during dom0 boot. IOMMU device tree node will only be created when cmdline option viommu is enabled.

---
 xen/arch/arm/domain_build.c       | 94 +++++++++++++++++++++++++++++++
 xen/arch/arm/include/asm/viommu.h |  1 +
 2 files changed, 95 insertions(+)
xen/arch/arm/domain_build.c +94 -0
index 30a4f1fd09..54ae3b5033 100644
@@ -1440,6 +1440,95 @@ int __init make_timer_node(const struct kernel_info *kinfo)
     return res;
 }
 
+#ifdef CONFIG_ARM_VIRTUAL_IOMMU
+static int make_hwdom_viommu_node(const struct kernel_info *kinfo)
+{
+    uint32_t len;
+    int res;
+    char buf[24];
+    void *fdt = kinfo->fdt;
+    const void *prop = NULL;
+    const struct dt_device_node *iommu = NULL;
+    struct host_iommu *iommu_data;
+    gic_interrupt_t intr;
+
+    if ( list_empty(&host_iommu_list) )
+        return 0;
+
+    list_for_each_entry( iommu_data, &host_iommu_list, entry )
+    {
+        if ( iommu_data->hwdom_node_created )
+            return 0;
+
+        iommu = iommu_data->dt_node;
+
+        snprintf(buf, sizeof(buf), "iommu@%"PRIx64, iommu_data->addr);
+
+        res = fdt_begin_node(fdt, buf);
+        if ( res )
+            return res;
+
+        prop = dt_get_property(iommu, "compatible", &len);
+        if ( !prop )
+        {
+            res = -FDT_ERR_XEN(ENOENT);
+            return res;
+        }
+
+        res = fdt_property(fdt, "compatible", prop, len);
+        if ( res )
+            return res;
+
+        if ( iommu->phandle )
+        {
+            res = fdt_property_cell(fdt, "phandle", iommu->phandle);
+            if ( res )
+                return res;
+        }
+
+        /* Use the same reg regions as the IOMMU node in host DTB. */
+        prop = dt_get_property(iommu, "reg", &len);
+        if ( !prop )
+        {
+            printk(XENLOG_ERR "vIOMMU: Can't find IOMMU reg property.\n");
+            res = -FDT_ERR_XEN(ENOENT);
+            return res;
+        }
+
+        res = fdt_property(fdt, "reg", prop, len);
+        if ( res )
+            return res;
+
+        prop = dt_get_property(iommu, "#iommu-cells", &len);
+        if ( !prop )
+        {
+            res = -FDT_ERR_XEN(ENOENT);
+            return res;
+        }
+
+        res = fdt_property(fdt, "#iommu-cells", prop, len);
+        if ( res )
+            return res;
+
+        res = fdt_property_string(fdt, "interrupt-names", "combined");
+        if ( res )
+            return res;
+
+        set_interrupt(intr, iommu_data->irq, 0xf, DT_IRQ_TYPE_LEVEL_HIGH);
+
+        res = fdt_property_interrupts(kinfo, &intr, 1);
+        if ( res )
+            return res;
+
+        iommu_data->hwdom_node_created = true;
+
+        fdt_end_node(fdt);
+    }
+
+    return res;
+}
+#endif
+
 static int __init handle_node(struct domain *d, struct kernel_info *kinfo,
                               struct dt_device_node *node,
                               p2m_type_t p2mt)
@@ -1508,6 +1597,11 @@ static int __init handle_node(struct domain *d, struct kernel_info *kinfo,
     if ( dt_match_node(timer_matches, node) )
         return make_timer_node(kinfo);
 
+#ifdef CONFIG_ARM_VIRTUAL_IOMMU
+    if ( device_get_class(node) == DEVICE_IOMMU && is_viommu_enabled() )
+        return make_hwdom_viommu_node(kinfo);
+#endif
+
     /* Skip nodes used by Xen */
     if ( dt_device_used_by(node) == DOMID_XEN )
     {
xen/arch/arm/include/asm/viommu.h +1 -0
index ed338fe0ec..01d4d0dfef 100644
@@ -19,6 +19,7 @@ struct host_iommu {
     paddr_t addr;
     paddr_t size;
     uint32_t irq;
+    bool hwdom_node_created;
 };
 
 struct viommu_ops {
Signature · 1 line
2.43.0
Reviewed-by on v3 14/23
#103 lore raw

Re: [PATCH v3 14/23] xen/arm: vIOMMU: IOMMU device tree node for dom0

Hi Milan,

Quoted 21 lines · Milan Djokic

From: Rahul Singh <[email protected]>

XEN will create an IOMMU device tree node in the device tree to enable the dom0 to discover the virtual SMMUv3 during dom0 boot. IOMMU device tree node will only be created when cmdline option viommu is enabled.

---
xen/arch/arm/domain_build.c | 94 +++++++++++++++++++++++++++++++
xen/arch/arm/include/asm/viommu.h | 1 +

2 files changed, 95 insertions(+)
xen/arch/arm/domain_build.c +0 -0
index 30a4f1fd09..54ae3b5033 100644

xen/arch/arm/domain_build.c int __init make_timer_node(const struct kernel_info *kinfo) · line 1444

Quoted 5 lines
xen/arch/arm/domain_build.c +2 -0
@@ -1440,6 +1440,95 @@ int __init make_timer_node(const struct kernel_info *kinfo)
    return res;
 }
 
+#ifdef CONFIG_ARM_VIRTUAL_IOMMU
+static int make_hwdom_viommu_node(const struct kernel_info *kinfo)

__init ?

The rest looks ok to me, with that fixed:

Cheers,
Luca

#104 lore raw

PATCH v3 15/23 xen/arm: vsmmuv3: Emulated SMMUv3 device tree node for dom0less

In reply to Milan Djokic

From: Rahul Singh <[email protected]>

XEN will create an Emulated SMMUv3 device tree node in the device tree to enable the dom0less domains to discover the virtual SMMUv3 during boot. Emulated SMMUv3 device tree node will only be created when cmdline option vsmmuv3 is enabled.

---
 xen/arch/arm/dom0less-build.c         | 53 +++++++++++++++++++++++++++
 xen/include/public/device_tree_defs.h |  1 +
 2 files changed, 54 insertions(+)
xen/arch/arm/dom0less-build.c +53 -0
index 067835e5d0..a48edb9568 100644
@@ -218,10 +218,63 @@ static int __init make_vpl011_uart_node(struct kernel_info *kinfo)
 }
 #endif
 
+#ifdef CONFIG_VIRTUAL_ARM_SMMU_V3
+static int __init make_vsmmuv3_node(const struct kernel_info *kinfo)
+{
+    int res;
+    char buf[24];
+    __be32 reg[GUEST_ROOT_ADDRESS_CELLS + GUEST_ROOT_SIZE_CELLS];
+    __be32 *cells;
+    void *fdt = kinfo->fdt;
+
+    snprintf(buf, sizeof(buf), "iommu@%llx", GUEST_VSMMUV3_BASE);
+
+    res = fdt_begin_node(fdt, buf);
+    if ( res )
+        return res;
+
+    res = fdt_property_string(fdt, "compatible", "arm,smmu-v3");
+    if ( res )
+        return res;
+
+    /* Create reg property */
+    cells = &reg[0];
+    dt_child_set_range(&cells, GUEST_ROOT_ADDRESS_CELLS, GUEST_ROOT_SIZE_CELLS,
+                       GUEST_VSMMUV3_BASE, GUEST_VSMMUV3_SIZE);
+    res = fdt_property(fdt, "reg", reg,
+                       (GUEST_ROOT_ADDRESS_CELLS +
+                       GUEST_ROOT_SIZE_CELLS) * sizeof(*reg));
+    if ( res )
+        return res;
+
+    res = fdt_property_cell(fdt, "phandle", GUEST_PHANDLE_VSMMUV3);
+    if ( res )
+        return res;
+
+    res = fdt_property_cell(fdt, "#iommu-cells", 1);
+    if ( res )
+        return res;
+
+    res = fdt_end_node(fdt);
+
+    return res;
+}
+#endif
+
 int __init make_arch_nodes(struct kernel_info *kinfo)
 {
     int ret;
 
+
+#ifdef CONFIG_VIRTUAL_ARM_SMMU_V3
+    if ( is_viommu_enabled() )
+    {
+        ret = make_vsmmuv3_node(kinfo);
+        if ( ret )
+            return -EINVAL;
+    }
+#endif
+
     ret = make_psci_node(kinfo->fdt);
     if ( ret )
         return -EINVAL;
xen/include/public/device_tree_defs.h +1 -0
index 9e80d0499d..7846a0425c 100644
@@ -14,6 +14,7 @@
  */
 #define GUEST_PHANDLE_GIC (65000)
 #define GUEST_PHANDLE_IOMMU (GUEST_PHANDLE_GIC + 1)
+#define GUEST_PHANDLE_VSMMUV3 (GUEST_PHANDLE_IOMMU + 1)
 
 #define GUEST_ROOT_ADDRESS_CELLS 2
 #define GUEST_ROOT_SIZE_CELLS 2
Signature · 1 line
2.43.0
Reviewed-by on v3 15/23
#105 lore raw

Re: [PATCH v3 15/23] xen/arm: vsmmuv3: Emulated SMMUv3 device tree node for dom0less

Hi Milan,

Quoted 24 lines · Milan Djokic

From: Rahul Singh <[email protected]>

XEN will create an Emulated SMMUv3 device tree node in the device tree to enable the dom0less domains to discover the virtual SMMUv3 during boot. Emulated SMMUv3 device tree node will only be created when cmdline option vsmmuv3 is enabled.

---
xen/arch/arm/dom0less-build.c | 53 +++++++++++++++++++++++++++
xen/include/public/device_tree_defs.h | 1 +

2 files changed, 54 insertions(+)
xen/arch/arm/dom0less-build.c +0 -0
index 067835e5d0..a48edb9568 100644
@@ -218,10 +218,63 @@ static int __init make_vpl011_uart_node(struct kernel_info *kinfo)
 }
 #endif
 

xen/arch/arm/dom0less-build.c static int __init make_vpl011_uart_node(struct kernel_info *kinfo) · line 230

Quoted 10 lines
xen/arch/arm/dom0less-build.c +10 -0
+#ifdef CONFIG_VIRTUAL_ARM_SMMU_V3
+static int __init make_vsmmuv3_node(const struct kernel_info *kinfo)
+{
+    int res;
+    char buf[24];
+    __be32 reg[GUEST_ROOT_ADDRESS_CELLS + GUEST_ROOT_SIZE_CELLS];
+    __be32 *cells;
+    void *fdt = kinfo->fdt;
+
+    snprintf(buf, sizeof(buf), "iommu@%llx", GUEST_VSMMUV3_BASE);

PRIx64 ?

The rest looks ok to me, with that fixed:

Cheers,
Luca

#106 lore raw

PATCH v3 16/23 arm/libxl: vsmmuv3: Emulated SMMUv3 device tree node in libxl

In reply to Milan Djokic

From: Rahul Singh <[email protected]>

libxl will create an Emulated SMMUv3 device tree node in the device tree to enable the guest OS to discover the virtual SMMUv3 during guest boot.

Emulated SMMUv3 device tree node will only be created when "viommu=smmuv3" is set in xl domain configuration.

---
 tools/libs/light/libxl_arm.c | 37 ++++++++++++++++++++++++++++++++++++
 1 file changed, 37 insertions(+)
tools/libs/light/libxl_arm.c +37 -0
index a248793588..eb879473f5 100644
@@ -901,6 +901,36 @@ static int make_vpl011_uart_node(libxl__gc *gc, void *fdt,
     return 0;
 }
 
+static int make_vsmmuv3_node(libxl__gc *gc, void *fdt,
+                             const struct arch_info *ainfo,
+                             struct xc_dom_image *dom)
+{
+    int res;
+    const char *name = GCSPRINTF("iommu@%llx", GUEST_VSMMUV3_BASE);
+
+    res = fdt_begin_node(fdt, name);
+    if (res) return res;
+
+    res = fdt_property_compat(gc, fdt, 1, "arm,smmu-v3");
+    if (res) return res;
+
+    res = fdt_property_regs(gc, fdt, GUEST_ROOT_ADDRESS_CELLS,
+                            GUEST_ROOT_SIZE_CELLS, 1, GUEST_VSMMUV3_BASE,
+                            GUEST_VSMMUV3_SIZE);
+    if (res) return res;
+
+    res = fdt_property_cell(fdt, "phandle", GUEST_PHANDLE_VSMMUV3);
+    if (res) return res;
+
+    res = fdt_property_cell(fdt, "#iommu-cells", 1);
+    if (res) return res;
+
+    res = fdt_end_node(fdt);
+    if (res) return res;
+
+    return 0;
+}
+
 static int make_vpci_node(libxl__gc *gc, void *fdt,
                           const struct arch_info *ainfo,
                           struct xc_dom_image *dom)
@@ -942,6 +972,10 @@ static int make_vpci_node(libxl__gc *gc, void *fdt,
         GUEST_VPCI_PREFETCH_MEM_SIZE);
     if (res) return res;
 
+    res = fdt_property_values(gc, fdt, "iommu-map", 4, 0,
+                              GUEST_PHANDLE_VSMMUV3, 0, 0x10000);
+    if (res) return res;
+
     res = fdt_end_node(fdt);
     if (res) return res;
 
@@ -1408,6 +1442,9 @@ next_resize:
         if (d_config->num_pcidevs)
             FDT( make_vpci_node(gc, fdt, ainfo, dom) );
 
+        if (info->arch_arm.viommu_type == LIBXL_VIOMMU_TYPE_SMMUV3)
+            FDT( make_vsmmuv3_node(gc, fdt, ainfo, dom) );
+
         for (i = 0; i < d_config->num_disks; i++) {
             libxl_device_disk *disk = &d_config->disks[i];
 
Signature · 1 line
2.43.0
#107 lore raw

Re: [PATCH v3 16/23] arm/libxl: vsmmuv3: Emulated SMMUv3 device tree node in libxl

Hi Milan,

Quoted 22 lines · Milan Djokic

From: Rahul Singh <[email protected]>

libxl will create an Emulated SMMUv3 device tree node in the device tree to enable the guest OS to discover the virtual SMMUv3 during guest boot.

Emulated SMMUv3 device tree node will only be created when "viommu=smmuv3" is set in xl domain configuration.

---
tools/libs/light/libxl_arm.c | 37 ++++++++++++++++++++++++++++++++++++

1 file changed, 37 insertions(+)
tools/libs/light/libxl_arm.c +0 -0
index a248793588..eb879473f5 100644

tools/libs/light/libxl_arm.c static int make_vpl011_uart_node(libxl__gc *gc, void *fdt, · line 909

Quoted 9 lines
tools/libs/light/libxl_arm.c +6 -0
@@ -901,6 +901,36 @@ static int make_vpl011_uart_node(libxl__gc *gc, void *fdt,
    return 0;
 }
 
+static int make_vsmmuv3_node(libxl__gc *gc, void *fdt,
+                             const struct arch_info *ainfo,
+                             struct xc_dom_image *dom)
+{
+    int res;
+    const char *name = GCSPRINTF("iommu@%llx", GUEST_VSMMUV3_BASE);

PRIx64 ?

Quoted 28 lines
tools/libs/light/libxl_arm.c +24 -0
+
+    res = fdt_begin_node(fdt, name);
+    if (res) return res;
+
+    res = fdt_property_compat(gc, fdt, 1, "arm,smmu-v3");
+    if (res) return res;
+
+    res = fdt_property_regs(gc, fdt, GUEST_ROOT_ADDRESS_CELLS,
+                            GUEST_ROOT_SIZE_CELLS, 1, GUEST_VSMMUV3_BASE,
+                            GUEST_VSMMUV3_SIZE);
+    if (res) return res;
+
+    res = fdt_property_cell(fdt, "phandle", GUEST_PHANDLE_VSMMUV3);
+    if (res) return res;
+
+    res = fdt_property_cell(fdt, "#iommu-cells", 1);
+    if (res) return res;
+
+    res = fdt_end_node(fdt);
+    if (res) return res;
+
+    return 0;
+}
+
 static int make_vpci_node(libxl__gc *gc, void *fdt,
                          const struct arch_info *ainfo,
                          struct xc_dom_image *dom)

tools/libs/light/libxl_arm.c static int make_vpci_node(libxl__gc *gc, void *fdt, · line 977

Quoted 6 lines
tools/libs/light/libxl_arm.c +3 -0
@@ -942,6 +972,10 @@ static int make_vpci_node(libxl__gc *gc, void *fdt,
        GUEST_VPCI_PREFETCH_MEM_SIZE);
    if (res) return res;
 
+    res = fdt_property_values(gc, fdt, "iommu-map", 4, 0,
+                              GUEST_PHANDLE_VSMMUV3, 0, 0x10000);
+    if (res) return res;

Shouldn’t this one be protected by (info->arch_arm.viommu_type == LIBXL_VIOMMU_TYPE_SMMUV3)?

Cheers,
Luca

#108 lore raw

PATCH v3 17/23 xen/arm: vsmmuv3: Alloc virq for virtual SMMUv3

In reply to Milan Djokic

From: Rahul Singh <[email protected]>

Alloc and reserve virq for event queue and global error to send event to guests. Also Modify the libxl to accomadate the new define virq.

---
 tools/libs/light/libxl_arm.c           | 28 ++++++++++++++++++++++++--
 xen/arch/arm/dom0less-build.c          | 17 ++++++++++++++++
 xen/drivers/passthrough/arm/vsmmu-v3.c | 13 ++++++++++++
 3 files changed, 56 insertions(+), 2 deletions(-)
tools/libs/light/libxl_arm.c +26 -2
index eb879473f5..803c3b39b7 100644
@@ -86,8 +86,8 @@ int libxl__arch_domain_prepare_config(libxl__gc *gc,
 {
     uint32_t nr_spis = 0, cfg_nr_spis = d_config->b_info.arch_arm.nr_spis;
     unsigned int i;
-    uint32_t vuart_irq, virtio_irq = 0;
-    bool vuart_enabled = false, virtio_enabled = false;
+    uint32_t vuart_irq, virtio_irq = 0, vsmmu_irq = 0;
+    bool vuart_enabled = false, virtio_enabled = false, vsmmu_enabled = false;
     uint64_t virtio_mmio_base = GUEST_VIRTIO_MMIO_BASE;
     uint32_t virtio_mmio_irq = GUEST_VIRTIO_MMIO_SPI_FIRST;
     int rc;
@@ -102,6 +102,16 @@ int libxl__arch_domain_prepare_config(libxl__gc *gc,
         vuart_enabled = true;
     }
 
+    /*
+     * If smmuv3 viommu is enabled then increment the nr_spis to allow allocation
+     * of SPI VIRQ for VSMMU.
+     */
+    if (d_config->b_info.arch_arm.viommu_type == LIBXL_VIOMMU_TYPE_SMMUV3) {
+        nr_spis += (GUEST_VSMMU_SPI - 32) + 1;
+        vsmmu_irq = GUEST_VSMMU_SPI;
+        vsmmu_enabled = true;
+    }
+
     for (i = 0; i < d_config->num_disks; i++) {
         libxl_device_disk *disk = &d_config->disks[i];
 
@@ -170,6 +180,11 @@ int libxl__arch_domain_prepare_config(libxl__gc *gc,
             return ERROR_FAIL;
         }
 
+        if (vsmmu_enabled && irq == vsmmu_irq) {
+            LOG(ERROR, "Physical IRQ %u conflicting with vSMMUv3 SPI\n", irq);
+            return ERROR_FAIL;
+        }
+
         if (irq < 32)
             continue;
 
@@ -907,6 +922,7 @@ static int make_vsmmuv3_node(libxl__gc *gc, void *fdt,
 {
     int res;
     const char *name = GCSPRINTF("iommu@%llx", GUEST_VSMMUV3_BASE);
+    gic_interrupt intr;
 
     res = fdt_begin_node(fdt, name);
     if (res) return res;
@@ -925,6 +941,14 @@ static int make_vsmmuv3_node(libxl__gc *gc, void *fdt,
     res = fdt_property_cell(fdt, "#iommu-cells", 1);
     if (res) return res;
 
+    res = fdt_property_string(fdt, "interrupt-names", "combined");
+    if (res) return res;
+
+    set_interrupt(intr, GUEST_VSMMU_SPI, 0xf, DT_IRQ_TYPE_LEVEL_HIGH);
+
+    res = fdt_property_interrupts(gc, fdt, &intr, 1);
+    if (res) return res;
+
     res = fdt_end_node(fdt);
     if (res) return res;
 
xen/arch/arm/dom0less-build.c +17 -0
index a48edb9568..7380753fa2 100644
@@ -225,6 +225,7 @@ static int __init make_vsmmuv3_node(const struct kernel_info *kinfo)
     char buf[24];
     __be32 reg[GUEST_ROOT_ADDRESS_CELLS + GUEST_ROOT_SIZE_CELLS];
     __be32 *cells;
+    gic_interrupt_t intr;
     void *fdt = kinfo->fdt;
 
     snprintf(buf, sizeof(buf), "iommu@%llx", GUEST_VSMMUV3_BASE);
@@ -255,6 +256,22 @@ static int __init make_vsmmuv3_node(const struct kernel_info *kinfo)
     if ( res )
         return res;
 
+    res = fdt_property_string(fdt, "interrupt-names", "combined");
+    if ( res )
+        return res;
+
+    set_interrupt(intr, GUEST_VSMMU_SPI, 0xf, DT_IRQ_TYPE_LEVEL_HIGH);
+
+    res = fdt_property(kinfo->fdt, "interrupts",
+                       intr, sizeof(intr));
+    if ( res )
+        return res;
+
+    res = fdt_property_cell(kinfo->fdt, "interrupt-parent",
+                            kinfo->phandle_intc);
+    if ( res )
+        return res;
+
     res = fdt_end_node(fdt);
 
     return res;
xen/drivers/passthrough/arm/vsmmu-v3.c +13 -0
index 7a6c18df53..a5b9700369 100644
@@ -733,6 +733,7 @@ static const struct mmio_handler_ops vsmmuv3_mmio_handler = {
 static int vsmmuv3_init_single(struct domain *d, paddr_t addr,
                                paddr_t size, uint32_t virq)
 {
+    int ret;
     struct virt_smmu *smmu;
 
     smmu = xzalloc(struct virt_smmu);
@@ -748,12 +749,24 @@ static int vsmmuv3_init_single(struct domain *d, paddr_t addr,
 
     spin_lock_init(&smmu->cmd_queue_lock);
 
+    ret = vgic_reserve_virq(d, virq);
+    if ( !ret )
+    {
+        ret = -EINVAL;
+        goto out;
+    }
+
     register_mmio_handler(d, &vsmmuv3_mmio_handler, addr, size, smmu);
 
     /* Register the vIOMMU to be able to clean it up later. */
     list_add_tail(&smmu->viommu_list, &d->arch.viommu_list);
 
     return 0;
+
+out:
+    xfree(smmu);
+    vgic_free_virq(d, virq);
+    return ret;
 }
 
 int domain_vsmmuv3_init(struct domain *d)
Signature · 1 line
2.43.0
#109 lore raw

Re: [PATCH v3 17/23] xen/arm: vsmmuv3: Alloc virq for virtual SMMUv3

Hi Milan,

Quoted 33 lines · Milan Djokic

From: Rahul Singh <[email protected]>

Alloc and reserve virq for event queue and global error to send event to guests. Also Modify the libxl to accomadate the new define virq.

---

tools/libs/light/libxl_arm.c           | 28 ++++++++++++++++++++++++--
xen/arch/arm/dom0less-build.c          | 17 ++++++++++++++++
xen/drivers/passthrough/arm/vsmmu-v3.c | 13 ++++++++++++
3 files changed, 56 insertions(+), 2 deletions(-)
tools/libs/light/libxl_arm.c +2 -2
index eb879473f5..803c3b39b7 100644
@@ -86,8 +86,8 @@ int libxl__arch_domain_prepare_config(libxl__gc *gc,
 {
    uint32_t nr_spis = 0, cfg_nr_spis = d_config->b_info.arch_arm.nr_spis;
    unsigned int i;
-    uint32_t vuart_irq, virtio_irq = 0;
-    bool vuart_enabled = false, virtio_enabled = false;
+    uint32_t vuart_irq, virtio_irq = 0, vsmmu_irq = 0;
+    bool vuart_enabled = false, virtio_enabled = false, vsmmu_enabled = false;
    uint64_t virtio_mmio_base = GUEST_VIRTIO_MMIO_BASE;
    uint32_t virtio_mmio_irq = GUEST_VIRTIO_MMIO_SPI_FIRST;
    int rc;
@@ -102,6 +102,16 @@ int libxl__arch_domain_prepare_config(libxl__gc *gc,
        vuart_enabled = true;
    }

tools/libs/light/libxl_arm.c int libxl__arch_domain_prepare_config(libxl__gc *gc, · line 113

Quoted 10 lines
tools/libs/light/libxl_arm.c +9 -0
 
+    /*
+     * If smmuv3 viommu is enabled then increment the nr_spis to allow allocation
+     * of SPI VIRQ for VSMMU.
+     */
+    if (d_config->b_info.arch_arm.viommu_type == LIBXL_VIOMMU_TYPE_SMMUV3) {
+        nr_spis += (GUEST_VSMMU_SPI - 32) + 1;
+        vsmmu_irq = GUEST_VSMMU_SPI;
+        vsmmu_enabled = true;
+    }

Now we would inflate too much nr_spis if also vPL011 is present, I think this commit should modify that part and this part in this way:

if (d_config->b_info.arch_arm.vuart == LIBXL_VUART_TYPE_SBSA_UART) {
    nr_spis = max(nr_spis, GUEST_VPL011_SPI - 32 + 1);
    vuart_irq = GUEST_VPL011_SPI;
    vuart_enabled = true;
}
if (d_config->b_info.arch_arm.viommu_type == LIBXL_VIOMMU_TYPE_SMMUV3) {
    nr_spis = max(nr_spis, GUEST_VSMMU_SPI - 32 + 1);
    vsmmu_irq = GUEST_VSMMU_SPI;
    vsmmu_enabled = true;
}

as done here https://xenbits.xen.org/gitweb/?p=xen.git;a=blob;f=tools/libs/light/libxl_arm.c;h=7e9f8a1bc3666ac9a3f8aa487366399dc255b149;hb=refs/heads/staging#l144

Quoted 97 lines
tools/libs/light/libxl_arm.c +15 -0
+
    for (i = 0; i < d_config->num_disks; i++) {
        libxl_device_disk *disk = &d_config->disks[i];
 
@@ -170,6 +180,11 @@ int libxl__arch_domain_prepare_config(libxl__gc *gc,
            return ERROR_FAIL;
        }
 
+        if (vsmmu_enabled && irq == vsmmu_irq) {
+            LOG(ERROR, "Physical IRQ %u conflicting with vSMMUv3 SPI\n", irq);
+            return ERROR_FAIL;
+        }
+
        if (irq < 32)
            continue;
 
@@ -907,6 +922,7 @@ static int make_vsmmuv3_node(libxl__gc *gc, void *fdt,
 {
    int res;
    const char *name = GCSPRINTF("iommu@%llx", GUEST_VSMMUV3_BASE);
+    gic_interrupt intr;
 
    res = fdt_begin_node(fdt, name);
    if (res) return res;
@@ -925,6 +941,14 @@ static int make_vsmmuv3_node(libxl__gc *gc, void *fdt,
    res = fdt_property_cell(fdt, "#iommu-cells", 1);
    if (res) return res;
 
+    res = fdt_property_string(fdt, "interrupt-names", "combined");
+    if (res) return res;
+
+    set_interrupt(intr, GUEST_VSMMU_SPI, 0xf, DT_IRQ_TYPE_LEVEL_HIGH);
+
+    res = fdt_property_interrupts(gc, fdt, &intr, 1);
+    if (res) return res;
+
    res = fdt_end_node(fdt);
    if (res) return res;
 
xen/arch/arm/dom0less-build.c +17 -0
index a48edb9568..7380753fa2 100644
@@ -225,6 +225,7 @@ static int __init make_vsmmuv3_node(const struct kernel_info *kinfo)
    char buf[24];
    __be32 reg[GUEST_ROOT_ADDRESS_CELLS + GUEST_ROOT_SIZE_CELLS];
    __be32 *cells;
+    gic_interrupt_t intr;
    void *fdt = kinfo->fdt;
 
    snprintf(buf, sizeof(buf), "iommu@%llx", GUEST_VSMMUV3_BASE);
@@ -255,6 +256,22 @@ static int __init make_vsmmuv3_node(const struct kernel_info *kinfo)
    if ( res )
        return res;
 
+    res = fdt_property_string(fdt, "interrupt-names", "combined");
+    if ( res )
+        return res;
+
+    set_interrupt(intr, GUEST_VSMMU_SPI, 0xf, DT_IRQ_TYPE_LEVEL_HIGH);
+
+    res = fdt_property(kinfo->fdt, "interrupts",
+                       intr, sizeof(intr));
+    if ( res )
+        return res;
+
+    res = fdt_property_cell(kinfo->fdt, "interrupt-parent",
+                            kinfo->phandle_intc);
+    if ( res )
+        return res;
+
    res = fdt_end_node(fdt);
 
    return res;
xen/drivers/passthrough/arm/vsmmu-v3.c +8 -0
index 7a6c18df53..a5b9700369 100644
@@ -733,6 +733,7 @@ static const struct mmio_handler_ops vsmmuv3_mmio_handler = {
 static int vsmmuv3_init_single(struct domain *d, paddr_t addr,
                               paddr_t size, uint32_t virq)
 {
+    int ret;
    struct virt_smmu *smmu;
 
    smmu = xzalloc(struct virt_smmu);
@@ -748,12 +749,24 @@ static int vsmmuv3_init_single(struct domain *d, paddr_t addr,
 
    spin_lock_init(&smmu->cmd_queue_lock);
 
+    ret = vgic_reserve_virq(d, virq);
+    if ( !ret )
+    {
+        ret = -EINVAL;
+        goto out;
+    }
+

xen/drivers/passthrough/arm/vsmmu-v3.c static int vsmmuv3_init_single(struct domain *d, paddr_t addr, · line 768

Quoted 10 lines
xen/drivers/passthrough/arm/vsmmu-v3.c +4 -0
    register_mmio_handler(d, &vsmmuv3_mmio_handler, addr, size, smmu);
 
    /* Register the vIOMMU to be able to clean it up later. */
    list_add_tail(&smmu->viommu_list, &d->arch.viommu_list);
 
    return 0;
+
+out:
+    xfree(smmu);
+    vgic_free_virq(d, virq);

I don’t think we should use vgic_free_virq here, if it was reserved already there is some bug, the virq can be GUEST_VSMMU_SPI for guests and hw_iommu->irq for HW domain, so if it’s reserved it should be a configuration issue and freeing it would remove “someone else” reserved irq.
Probably I would print something in the if above and also I would just have the content of this Label-path inside the if branch, since it is the only consumer of this label.

xen/drivers/passthrough/arm/vsmmu-v3.c static int vsmmuv3_init_single(struct domain *d, paddr_t addr, · line 772

Quoted 5 lines
xen/drivers/passthrough/arm/vsmmu-v3.c +1 -0
+    return ret;
 }
 
 int domain_vsmmuv3_init(struct domain *d)

Cheers,
Luca

#110 lore raw

PATCH v3 18/23 xen/arm: vsmmuv3: Add support to send stage-1 event to guest

In reply to Milan Djokic

From: Rahul Singh <[email protected]>

Stage-1 translation is handled by guest, therefore stage-1 fault has to be forwarded to guest.

---
 xen/drivers/passthrough/arm/smmu-v3.c  | 48 ++++++++++++++++++++++++--
 xen/drivers/passthrough/arm/vsmmu-v3.c | 45 ++++++++++++++++++++++++
 xen/drivers/passthrough/arm/vsmmu-v3.h | 12 +++++++
 3 files changed, 103 insertions(+), 2 deletions(-)
xen/drivers/passthrough/arm/smmu-v3.c +46 -2
index cf8f638a49..4c1951d753 100644
@@ -869,7 +869,6 @@ static int arm_smmu_init_l2_strtab(struct arm_smmu_device *smmu, u32 sid)
 	return 0;
 }
 
-__maybe_unused
 static struct arm_smmu_master *
 arm_smmu_find_master(struct arm_smmu_device *smmu, u32 sid)
 {
@@ -890,10 +889,51 @@ arm_smmu_find_master(struct arm_smmu_device *smmu, u32 sid)
 	return NULL;
 }
 
+static int arm_smmu_handle_evt(struct arm_smmu_device *smmu, u64 *evt)
+{
+	int ret;
+	struct arm_smmu_master *master;
+	u32 sid = FIELD_GET(EVTQ_0_SID, evt[0]);
+
+	switch (FIELD_GET(EVTQ_0_ID, evt[0])) {
+	case EVT_ID_TRANSLATION_FAULT:
+		break;
+	case EVT_ID_ADDR_SIZE_FAULT:
+		break;
+	case EVT_ID_ACCESS_FAULT:
+		break;
+	case EVT_ID_PERMISSION_FAULT:
+		break;
+	default:
+		return -EOPNOTSUPP;
+	}
+
+	/* Stage-2 event */
+	if (evt[1] & EVTQ_1_S2)
+		return -EFAULT;
+
+	mutex_lock(&smmu->streams_mutex);
+	master = arm_smmu_find_master(smmu, sid);
+	if (!master) {
+		ret = -EINVAL;
+		goto out_unlock;
+	}
+
+	ret = arm_vsmmu_handle_evt(master->domain->d, smmu->dev, evt);
+	if (ret) {
+		ret = -EINVAL;
+		goto out_unlock;
+	}
+
+out_unlock:
+	mutex_unlock(&smmu->streams_mutex);
+	return ret;
+}
+
 /* IRQ and event handlers */
 static void arm_smmu_evtq_tasklet(void *dev)
 {
-	int i;
+	int i, ret;
 	struct arm_smmu_device *smmu = dev;
 	struct arm_smmu_queue *q = &smmu->evtq.q;
 	struct arm_smmu_ll_queue *llq = &q->llq;
@@ -903,6 +943,10 @@ static void arm_smmu_evtq_tasklet(void *dev)
 		while (!queue_remove_raw(q, evt)) {
 			u8 id = FIELD_GET(EVTQ_0_ID, evt[0]);
 
+			ret = arm_smmu_handle_evt(smmu, evt);
+			if (!ret)
+				continue;
+
 			dev_info(smmu->dev, "event 0x%02x received:\n", id);
 			for (i = 0; i < ARRAY_SIZE(evt); ++i)
 				dev_info(smmu->dev, "\t0x%016llx\n",
xen/drivers/passthrough/arm/vsmmu-v3.c +45 -0

63 lines not on this page · raw message

xen/drivers/passthrough/arm/vsmmu-v3.h +12 -0
index e11f85b431..c7bfd3fb59 100644
@@ -8,6 +8,12 @@
 
 void vsmmuv3_set_type(void);
 
+static inline int arm_vsmmu_handle_evt(struct domain *d,
+                                       struct device *dev, uint64_t *evt)
+{
+    return -EINVAL;
+}
+
 #else
 
 static inline void vsmmuv3_set_type(void)
@@ -15,6 +21,12 @@ static inline void vsmmuv3_set_type(void)
     return;
 }
 
+static inline int arm_vsmmu_handle_evt(struct domain *d,
+                                       struct device *dev, uint64_t *evt)
+{
+    return -EINVAL;
+}
+
 #endif /* CONFIG_VIRTUAL_ARM_SMMU_V3 */
 
 #endif /* __ARCH_ARM_VSMMU_V3_H__ */
Signature · 1 line
2.43.0
#111 lore raw

Re: [PATCH v3 18/23] xen/arm: vsmmuv3: Add support to send stage-1 event to guest

HI Milan,

Quoted 166 lines · Milan Djokic

From: Rahul Singh <[email protected]>

Stage-1 translation is handled by guest, therefore stage-1 fault has to be forwarded to guest.

---

xen/drivers/passthrough/arm/smmu-v3.c  | 48 ++++++++++++++++++++++++--
xen/drivers/passthrough/arm/vsmmu-v3.c | 45 ++++++++++++++++++++++++
xen/drivers/passthrough/arm/vsmmu-v3.h | 12 +++++++
3 files changed, 103 insertions(+), 2 deletions(-)
xen/drivers/passthrough/arm/smmu-v3.c +0 -1
index cf8f638a49..4c1951d753 100644
@@ -869,7 +869,6 @@ static int arm_smmu_init_l2_strtab(struct arm_smmu_device *smmu, u32 sid)
 return 0;
 }
 
-__maybe_unused

static struct arm_smmu_master * arm_smmu_find_master(struct arm_smmu_device *smmu, u32 sid) {

xen/drivers/passthrough/arm/smmu-v3.c +46 -1
@@ -890,10 +889,51 @@ arm_smmu_find_master(struct arm_smmu_device *smmu, u32 sid)
 return NULL;
 }
 
+static int arm_smmu_handle_evt(struct arm_smmu_device *smmu, u64 *evt)
+{
+ int ret;
+ struct arm_smmu_master *master;
+ u32 sid = FIELD_GET(EVTQ_0_SID, evt[0]);
+
+ switch (FIELD_GET(EVTQ_0_ID, evt[0])) {
+ case EVT_ID_TRANSLATION_FAULT:
+ break;
+ case EVT_ID_ADDR_SIZE_FAULT:
+ break;
+ case EVT_ID_ACCESS_FAULT:
+ break;
+ case EVT_ID_PERMISSION_FAULT:
+ break;
+ default:
+ return -EOPNOTSUPP;
+ }
+
+ /* Stage-2 event */
+ if (evt[1] & EVTQ_1_S2)
+ return -EFAULT;
+
+ mutex_lock(&smmu->streams_mutex);
+ master = arm_smmu_find_master(smmu, sid);
+ if (!master) {
+ ret = -EINVAL;
+ goto out_unlock;
+ }
+
+ ret = arm_vsmmu_handle_evt(master->domain->d, smmu->dev, evt);
+ if (ret) {
+ ret = -EINVAL;
+ goto out_unlock;
+ }
+
+out_unlock:
+ mutex_unlock(&smmu->streams_mutex);
+ return ret;
+}
+
 /* IRQ and event handlers */
 static void arm_smmu_evtq_tasklet(void *dev)
 {
- int i;
+ int i, ret;
 struct arm_smmu_device *smmu = dev;
 struct arm_smmu_queue *q = &smmu->evtq.q;
 struct arm_smmu_ll_queue *llq = &q->llq;
@@ -903,6 +943,10 @@ static void arm_smmu_evtq_tasklet(void *dev)
 while (!queue_remove_raw(q, evt)) {
 u8 id = FIELD_GET(EVTQ_0_ID, evt[0]);
 
+ ret = arm_smmu_handle_evt(smmu, evt);
+ if (!ret)
+ continue;
+
 dev_info(smmu->dev, "event 0x%02x received:\n", id);
 for (i = 0; i < ARRAY_SIZE(evt); ++i)
 dev_info(smmu->dev, "\t0x%016llx\n",
xen/drivers/passthrough/arm/vsmmu-v3.c +45 -0
index a5b9700369..5d0dabd2b2 100644
@@ -103,6 +103,7 @@ struct arm_vsmmu_queue {
 struct virt_smmu {
    struct      domain *d;
    struct      list_head viommu_list;
+    paddr_t     addr;
    uint8_t     sid_split;
    uint32_t    features;
    uint32_t    cr[3];
@@ -237,6 +238,49 @@ void arm_vsmmu_send_event(struct virt_smmu *smmu,
    return;
 }
 
+static struct virt_smmu *vsmmuv3_find_by_addr(struct domain *d, paddr_t paddr)
+{
+    struct virt_smmu *smmu;
+
+    list_for_each_entry( smmu, &d->arch.viommu_list, viommu_list )
+    {
+        if ( smmu->addr == paddr )
+            return smmu;
+    }
+
+    return NULL;
+}
+
+int arm_vsmmu_handle_evt(struct domain *d, struct device *dev, uint64_t *evt)
+{
+    int ret;
+    struct virt_smmu *smmu;
+
+    if ( is_hardware_domain(d) )
+    {
+        paddr_t paddr;
+        /* Base address */
+        ret = dt_device_get_address(dev_to_dt(dev), 0, &paddr, NULL);
+        if ( ret )
+            return -EINVAL;
+
+        smmu = vsmmuv3_find_by_addr(d, paddr);
+        if ( !smmu )
+            return -ENODEV;
+    }
+    else
+    {
+        smmu = list_entry(d->arch.viommu_list.next,
+                          struct virt_smmu, viommu_list);
+    }
+
+    ret = arm_vsmmu_write_evtq(smmu, evt);
+    if ( ret )
+        arm_vsmmu_inject_irq(smmu, true, GERROR_EVTQ_ABT_ERR);
+
+    return 0;
+}
+
 static int arm_vsmmu_find_ste(struct virt_smmu *smmu, uint32_t sid,
                              uint64_t *ste)
 {
@@ -742,6 +786,7 @@ static int vsmmuv3_init_single(struct domain *d, paddr_t addr,
 
    smmu->d = d;
    smmu->virq = virq;
+    smmu->addr = addr;
    smmu->cmdq.q_base = FIELD_PREP(Q_BASE_LOG2SIZE, SMMU_CMDQS);
    smmu->cmdq.ent_size = CMDQ_ENT_DWORDS * DWORDS_BYTES;
    smmu->evtq.q_base = FIELD_PREP(Q_BASE_LOG2SIZE, SMMU_EVTQS);
xen/drivers/passthrough/arm/vsmmu-v3.h +0 -0
index e11f85b431..c7bfd3fb59 100644

xen/drivers/passthrough/arm/vsmmu-v3.h line 15

Quoted 8 lines
xen/drivers/passthrough/arm/vsmmu-v3.h +5 -0
@@ -8,6 +8,12 @@
 
 void vsmmuv3_set_type(void);
 
+static inline int arm_vsmmu_handle_evt(struct domain *d,
+                                       struct device *dev, uint64_t *evt)
+{
+    return -EINVAL;
+}

Shouldn’t this be the prototype of arm_vsmmu_handle_evt() instead of a stub implementation?

Quoted 8 lines
xen/drivers/passthrough/arm/vsmmu-v3.h +1 -0
+
 #else
 
 static inline void vsmmuv3_set_type(void)
@@ -15,6 +21,12 @@ static inline void vsmmuv3_set_type(void)
    return;
 }

xen/drivers/passthrough/arm/vsmmu-v3.h static inline void vsmmuv3_set_type(void) · line 32

Quoted 10 lines
xen/drivers/passthrough/arm/vsmmu-v3.h +6 -0
 
+static inline int arm_vsmmu_handle_evt(struct domain *d,
+                                       struct device *dev, uint64_t *evt)
+{
+    return -EINVAL;
+}
+
 #endif /* CONFIG_VIRTUAL_ARM_SMMU_V3 */
 
 #endif /* __ARCH_ARM_VSMMU_V3_H__ */

Cheers,
Luca

#112 lore raw

PATCH v3 19/23 libxl/arm: vIOMMU: Modify the partial device tree for iommus

In reply to Milan Djokic

From: Rahul Singh <[email protected]>

To configure IOMMU in guest for passthrough devices, user will need to copy the unmodified "iommus" property from host device tree to partial device tree. To enable the dom0 linux kernel to confiure the IOMMU correctly replace the phandle in partial device tree with virtual IOMMU phandle when "iommus" property is set.

---
 tools/libs/light/libxl_arm.c | 47 +++++++++++++++++++++++++++++++++++-
 1 file changed, 46 insertions(+), 1 deletion(-)
tools/libs/light/libxl_arm.c +46 -1
index 803c3b39b7..7b887898bb 100644
@@ -1326,6 +1326,41 @@ static int copy_partial_fdt(libxl__gc *gc, void *fdt, void *pfdt)
     return 0;
 }
 
+static int modify_partial_fdt(libxl__gc *gc, void *pfdt)
+{
+    int nodeoff, proplen, i, r;
+    const fdt32_t *prop;
+    fdt32_t *prop_c;
+
+    nodeoff = fdt_path_offset(pfdt, "/passthrough");
+    if (nodeoff < 0)
+        return nodeoff;
+
+    for (nodeoff = fdt_first_subnode(pfdt, nodeoff);
+         nodeoff >= 0;
+         nodeoff = fdt_next_subnode(pfdt, nodeoff)) {
+
+        prop = fdt_getprop(pfdt, nodeoff, "iommus", &proplen);
+        if (!prop)
+            continue;
+
+        prop_c = libxl__zalloc(gc, proplen);
+
+        for (i = 0; i < proplen / 8; ++i) {
+            prop_c[i * 2] = cpu_to_fdt32(GUEST_PHANDLE_VSMMUV3);
+            prop_c[i * 2 + 1] = prop[i * 2 + 1];
+        }
+
+        r = fdt_setprop(pfdt, nodeoff, "iommus", prop_c, proplen);
+        if (r) {
+            LOG(ERROR, "Can't set the iommus property in partial FDT");
+            return r;
+        }
+    }
+
+    return 0;
+}
+
 #else
 
 static int check_partial_fdt(libxl__gc *gc, void *fdt, size_t size)
@@ -1344,6 +1379,13 @@ static int copy_partial_fdt(libxl__gc *gc, void *fdt, void *pfdt)
     return -FDT_ERR_INTERNAL;
 }
 
+static int modify_partial_fdt(libxl__gc *gc, void *pfdt)
+{
+    LOG(ERROR, "partial device tree not supported");
+
+    return ERROR_FAIL;
+}
+
 #endif /* ENABLE_PARTIAL_DEVICE_TREE */
 
 #define FDT_MAX_SIZE (1<<20)
@@ -1466,8 +1508,11 @@ next_resize:
         if (d_config->num_pcidevs)
             FDT( make_vpci_node(gc, fdt, ainfo, dom) );
 
-        if (info->arch_arm.viommu_type == LIBXL_VIOMMU_TYPE_SMMUV3)
+        if (info->arch_arm.viommu_type == LIBXL_VIOMMU_TYPE_SMMUV3) {
             FDT( make_vsmmuv3_node(gc, fdt, ainfo, dom) );
+            if (pfdt)
+                FDT( modify_partial_fdt(gc, pfdt) );
+        }
 
         for (i = 0; i < d_config->num_disks; i++) {
             libxl_device_disk *disk = &d_config->disks[i];
Signature · 1 line
2.43.0
Reviewed-by on v3 19/23
#113 lore raw

Re: [PATCH v3 19/23] libxl/arm: vIOMMU: Modify the partial device tree for iommus

Hi Milan,

Quoted 7 lines · Milan Djokic

From: Rahul Singh <[email protected]>

To configure IOMMU in guest for passthrough devices, user will need to copy the unmodified "iommus" property from host device tree to partial device tree. To enable the dom0 linux kernel to confiure the IOMMU

Typo s/confiure/configure/

The rest looks ok to me

with that addressed:

Cheers,
Luca

#114 lore raw

PATCH v3 20/23 xen/arm: vIOMMU: Modify the partial device tree for dom0less

In reply to Milan Djokic

From: Rahul Singh <[email protected]>

To configure IOMMU in guest for passthrough devices, user will need to copy the unmodified "iommus" property from host device tree to partial device tree. To enable the dom0 linux kernel to confiure the IOMMU correctly replace the phandle in partial device tree with virtual IOMMU phandle when "iommus" property is set.

---
 xen/common/device-tree/dom0less-build.c | 31 ++++++++++++++++++++++++-
 1 file changed, 30 insertions(+), 1 deletion(-)
xen/common/device-tree/dom0less-build.c +30 -1
index 840d14419d..4b74d2f705 100644
@@ -318,7 +318,35 @@ static int __init handle_prop_pfdt(struct kernel_info *kinfo,
     return ( propoff != -FDT_ERR_NOTFOUND ) ? propoff : 0;
 }
 
-static int __init scan_pfdt_node(struct kernel_info *kinfo, const void *pfdt,
+static void modify_pfdt_node(void *pfdt, int nodeoff)
+{
+    int proplen, i, rc;
+    const fdt32_t *prop;
+    fdt32_t *prop_c;
+
+    prop = fdt_getprop(pfdt, nodeoff, "iommus", &proplen);
+    if ( !prop )
+        return;
+
+    prop_c = xzalloc_bytes(proplen);
+
+    for ( i = 0; i < proplen / 8; ++i )
+    {
+        prop_c[i * 2] = cpu_to_fdt32(GUEST_PHANDLE_VSMMUV3);
+        prop_c[i * 2 + 1] = prop[i * 2 + 1];
+    }
+
+    rc = fdt_setprop(pfdt, nodeoff, "iommus", prop_c, proplen);
+    if ( rc )
+    {
+        dprintk(XENLOG_ERR, "Can't set the iommus property in partial FDT");
+        return;
+    }
+
+    return;
+}
+
+static int __init scan_pfdt_node(struct kernel_info *kinfo, void *pfdt,
                                  int nodeoff,
                                  uint32_t address_cells, uint32_t size_cells,
                                  bool scan_passthrough_prop)
@@ -344,6 +372,7 @@ static int __init scan_pfdt_node(struct kernel_info *kinfo, const void *pfdt,
     node_next = fdt_first_subnode(pfdt, nodeoff);
     while ( node_next > 0 )
     {
+        modify_pfdt_node(pfdt, node_next);
         rc = scan_pfdt_node(kinfo, pfdt, node_next, address_cells, size_cells,
                             scan_passthrough_prop);
         if ( rc )
Signature · 1 line
2.43.0
#115 lore raw

Re: [PATCH v3 20/23] xen/arm: vIOMMU: Modify the partial device tree for dom0less

Hi Milan,

Quoted 7 lines · Milan Djokic

From: Rahul Singh <[email protected]>

To configure IOMMU in guest for passthrough devices, user will need to copy the unmodified "iommus" property from host device tree to partial device tree. To enable the dom0 linux kernel to confiure the IOMMU

typo s/confiure/configure/

Quoted 19 lines

correctly replace the phandle in partial device tree with virtual IOMMU phandle when "iommus" property is set.

---
xen/common/device-tree/dom0less-build.c | 31 ++++++++++++++++++++++++-

1 file changed, 30 insertions(+), 1 deletion(-)
xen/common/device-tree/dom0less-build.c +1 -1
index 840d14419d..4b74d2f705 100644
@@ -318,7 +318,35 @@ static int __init handle_prop_pfdt(struct kernel_info *kinfo,
    return ( propoff != -FDT_ERR_NOTFOUND ) ? propoff : 0;
 }
 
-static int __init scan_pfdt_node(struct kernel_info *kinfo, const void *pfdt,
+static void modify_pfdt_node(void *pfdt, int nodeoff)

xen/common/device-tree/dom0less-build.c static int __init handle_prop_pfdt(struct kernel_info *kinfo, · line 331

Quoted 10 lines
xen/common/device-tree/dom0less-build.c +10 -0
+{
+    int proplen, i, rc;
+    const fdt32_t *prop;
+    fdt32_t *prop_c;
+
+    prop = fdt_getprop(pfdt, nodeoff, "iommus", &proplen);
+    if ( !prop )
+        return;
+
+    prop_c = xzalloc_bytes(proplen);

we should check if the allocation was ok

Quoted 23 lines
xen/common/device-tree/dom0less-build.c +18 -0
+
+    for ( i = 0; i < proplen / 8; ++i )
+    {
+        prop_c[i * 2] = cpu_to_fdt32(GUEST_PHANDLE_VSMMUV3);
+        prop_c[i * 2 + 1] = prop[i * 2 + 1];
+    }
+
+    rc = fdt_setprop(pfdt, nodeoff, "iommus", prop_c, proplen);
+    if ( rc )
+    {
+        dprintk(XENLOG_ERR, "Can't set the iommus property in partial FDT");
+        return;
+    }
+
+    return;
+}
+
+static int __init scan_pfdt_node(struct kernel_info *kinfo, void *pfdt,
                                 int nodeoff,
                                 uint32_t address_cells, uint32_t size_cells,
                                 bool scan_passthrough_prop)

xen/common/device-tree/dom0less-build.c static int __init scan_pfdt_node(struct kernel_info *kinfo, const void *pfdt, · line 378

Quoted 7 lines
xen/common/device-tree/dom0less-build.c +1 -0
@@ -344,6 +372,7 @@ static int __init scan_pfdt_node(struct kernel_info *kinfo, const void *pfdt,
    node_next = fdt_first_subnode(pfdt, nodeoff);
    while ( node_next > 0 )
    {
+        modify_pfdt_node(pfdt, node_next);
        rc = scan_pfdt_node(kinfo, pfdt, node_next, address_cells, size_cells,
                            scan_passthrough_prop);
        if ( rc )

Cheers,
Luca

#116 lore raw

PATCH v3 21/23 xen/arm: vIOMMU vSID->pSID mapping layer

In reply to Milan Djokic

Introduce vIOMMU mapping layer in order to support passthrough of IOMMU devices attached to different physical IOMMUs (e.g. devices with the same streamID). New generic vIOMMU API is added: viommu_allocate_free_vid().
This function will allocate a new guest vSID and map it to input pSID. Once mapping is established, guest will use vSID for stage-1 commands and xen will translate vSID->pSID and propagate it towards stage-2. Introduced naming is generic (vID/pID), since this API could be used for other IOMMU types in the future.
Implemented usage of the new API for dom0less guests. vSIDs are allocated on guest device tree creation and the original pSID is
replaced with vSID which shall be used by the guest driver.

---
 xen/arch/arm/include/asm/viommu.h       | 10 ++++
 xen/common/device-tree/dom0less-build.c | 32 +++++++++---
 xen/drivers/passthrough/arm/viommu.c    |  7 +++
 xen/drivers/passthrough/arm/vsmmu-v3.c  | 67 ++++++++++++++++++++++++-
 4 files changed, 106 insertions(+), 10 deletions(-)
xen/arch/arm/include/asm/viommu.h +10 -0
index 01d4d0dfef..6a2fc56e38 100644
@@ -33,6 +33,15 @@ struct viommu_ops {
      * Called during domain destruction to free resources used by vIOMMU.
      */
     int (*relinquish_resources)(struct domain *d);
+
+    /*
+     * Allocate free vSID/vRID for the guest device and establish vID->pID mapping
+     * Called during domain device assignment.
+     * Returns 0 on success and sets vid argument to newly allocated vSID/vRID
+     * mapped to physical ID (id argument).
+     * Negative error code returned if allocation fails.
+     */
+    int (*allocate_free_vid)(struct domain *d, uint32_t id, uint32_t *vid);
 };
 
 struct viommu_desc {
@@ -48,6 +57,7 @@ struct viommu_desc {
 
 int domain_viommu_init(struct domain *d, uint16_t viommu_type);
 int viommu_relinquish_resources(struct domain *d);
+int viommu_allocate_free_vid(struct domain *d, uint32_t id, uint32_t *vid);
 uint16_t viommu_get_type(void);
 void add_to_host_iommu_list(paddr_t addr, paddr_t size,
                             const struct dt_device_node *node);
xen/common/device-tree/dom0less-build.c +24 -8
index 4b74d2f705..f5afdf381c 100644
@@ -31,6 +31,8 @@
 #include <xen/static-memory.h>
 #include <xen/static-shmem.h>
 
+#include <asm/viommu.h>
+
 #define XENSTORE_PFN_LATE_ALLOC UINT64_MAX
 
 static domid_t __initdata xs_domid = DOMID_INVALID;
@@ -318,22 +320,33 @@ static int __init handle_prop_pfdt(struct kernel_info *kinfo,
     return ( propoff != -FDT_ERR_NOTFOUND ) ? propoff : 0;
 }
 
-static void modify_pfdt_node(void *pfdt, int nodeoff)
+#ifdef CONFIG_ARM_VIRTUAL_IOMMU
+static void modify_pfdt_node(void *pfdt, int nodeoff, struct domain *d)
 {
     int proplen, i, rc;
     const fdt32_t *prop;
     fdt32_t *prop_c;
+    uint32_t vsid;
 
-    prop = fdt_getprop(pfdt, nodeoff, "iommus", &proplen);
+    prop = fdt_getprop(pfdt, nodeoff, "iommus", &proplen); 
     if ( !prop )
         return;
 
     prop_c = xzalloc_bytes(proplen);
 
+    /* 
+     * Assign <vIOMMU vSID> pairs to iommus property and establish
+     * vSID->pSID mappings
+    */
     for ( i = 0; i < proplen / 8; ++i )
     {
         prop_c[i * 2] = cpu_to_fdt32(GUEST_PHANDLE_VSMMUV3);
-        prop_c[i * 2 + 1] = prop[i * 2 + 1];
+        rc = viommu_allocate_free_vid(d, fdt32_to_cpu(prop[i * 2 + 1]), &vsid);
+        if( rc ) {
+            dprintk(XENLOG_ERR, "Failed to allocate new vSID for iommu device");
+            return;
+        }
+        prop_c[i * 2 + 1] = cpu_to_fdt32(vsid);
     }
 
     rc = fdt_setprop(pfdt, nodeoff, "iommus", prop_c, proplen);
@@ -345,11 +358,14 @@ static void modify_pfdt_node(void *pfdt, int nodeoff)
 
     return;
 }
+#else
+    static void modify_pfdt_node(void *pfdt, int nodeoff, struct domain *d) {}
+#endif
 
 static int __init scan_pfdt_node(struct kernel_info *kinfo, void *pfdt,
                                  int nodeoff,
                                  uint32_t address_cells, uint32_t size_cells,
-                                 bool scan_passthrough_prop)
+                                 bool scan_passthrough_prop, struct domain *d)
 {
     int rc = 0;
     void *fdt = kinfo->fdt;
@@ -372,9 +388,9 @@ static int __init scan_pfdt_node(struct kernel_info *kinfo, void *pfdt,
     node_next = fdt_first_subnode(pfdt, nodeoff);
     while ( node_next > 0 )
     {
-        modify_pfdt_node(pfdt, node_next);
+        modify_pfdt_node(pfdt, node_next, d);
         rc = scan_pfdt_node(kinfo, pfdt, node_next, address_cells, size_cells,
-                            scan_passthrough_prop);
+                            scan_passthrough_prop, d);
         if ( rc )
             return rc;
 
@@ -443,7 +459,7 @@ static int __init domain_handle_dtb_boot_module(struct domain *d,
             res = scan_pfdt_node(kinfo, pfdt, node_next,
                                  DT_ROOT_NODE_ADDR_CELLS_DEFAULT,
                                  DT_ROOT_NODE_SIZE_CELLS_DEFAULT,
-                                 false);
+                                 false, d);
             if ( res )
                 goto out;
             continue;
@@ -453,7 +469,7 @@ static int __init domain_handle_dtb_boot_module(struct domain *d,
             res = scan_pfdt_node(kinfo, pfdt, node_next,
                                  DT_ROOT_NODE_ADDR_CELLS_DEFAULT,
                                  DT_ROOT_NODE_SIZE_CELLS_DEFAULT,
-                                 true);
+                                 true, d);
             if ( res )
                 goto out;
             continue;
xen/drivers/passthrough/arm/viommu.c +7 -0
index 5f5892fbb2..4b7837a91f 100644
@@ -71,6 +71,13 @@ int viommu_relinquish_resources(struct domain *d)
     return cur_viommu->ops->relinquish_resources(d);
 }
 
+int viommu_allocate_free_vid(struct domain *d, uint32_t id, uint32_t *vid) {
+    if ( !cur_viommu )
+        return -ENODEV;
+
+    return cur_viommu->ops->allocate_free_vid(d, id, vid);
+}
+
 uint16_t viommu_get_type(void)
 {
     if ( !cur_viommu )
xen/drivers/passthrough/arm/vsmmu-v3.c +65 -2

118 lines not on this page · raw message

Signature · 1 line
2.43.0
#117 lore raw

Re: [PATCH v3 21/23] xen/arm: vIOMMU vSID->pSID mapping layer

Hi Milan,

Quoted 83 lines · Milan Djokic

Introduce vIOMMU mapping layer in order to support passthrough of IOMMU devices attached to different physical IOMMUs (e.g. devices with the same streamID). New generic vIOMMU API is added: viommu_allocate_free_vid().
This function will allocate a new guest vSID and map it to input pSID. Once mapping is established, guest will use vSID for stage-1 commands and xen will translate vSID->pSID and propagate it towards stage-2. Introduced naming is generic (vID/pID), since this API could be used for other IOMMU types in the future.
Implemented usage of the new API for dom0less guests. vSIDs are allocated on guest device tree creation and the original pSID is
replaced with vSID which shall be used by the guest driver.

---

xen/arch/arm/include/asm/viommu.h       | 10 ++++
xen/common/device-tree/dom0less-build.c | 32 +++++++++---
xen/drivers/passthrough/arm/viommu.c    |  7 +++
xen/drivers/passthrough/arm/vsmmu-v3.c  | 67 ++++++++++++++++++++++++-
4 files changed, 106 insertions(+), 10 deletions(-)
xen/arch/arm/include/asm/viommu.h +10 -0
index 01d4d0dfef..6a2fc56e38 100644
@@ -33,6 +33,15 @@ struct viommu_ops {
     * Called during domain destruction to free resources used by vIOMMU.
     */
    int (*relinquish_resources)(struct domain *d);
+
+    /*
+     * Allocate free vSID/vRID for the guest device and establish vID->pID mapping
+     * Called during domain device assignment.
+     * Returns 0 on success and sets vid argument to newly allocated vSID/vRID
+     * mapped to physical ID (id argument).
+     * Negative error code returned if allocation fails.
+     */
+    int (*allocate_free_vid)(struct domain *d, uint32_t id, uint32_t *vid);
 };
 
 struct viommu_desc {
@@ -48,6 +57,7 @@ struct viommu_desc {
 
 int domain_viommu_init(struct domain *d, uint16_t viommu_type);
 int viommu_relinquish_resources(struct domain *d);
+int viommu_allocate_free_vid(struct domain *d, uint32_t id, uint32_t *vid);
 uint16_t viommu_get_type(void);
 void add_to_host_iommu_list(paddr_t addr, paddr_t size,
                            const struct dt_device_node *node);
xen/common/device-tree/dom0less-build.c +6 -2
index 4b74d2f705..f5afdf381c 100644
@@ -31,6 +31,8 @@
 #include <xen/static-memory.h>
 #include <xen/static-shmem.h>
 
+#include <asm/viommu.h>
+
 #define XENSTORE_PFN_LATE_ALLOC UINT64_MAX
 
 static domid_t __initdata xs_domid = DOMID_INVALID;
@@ -318,22 +320,33 @@ static int __init handle_prop_pfdt(struct kernel_info *kinfo,
    return ( propoff != -FDT_ERR_NOTFOUND ) ? propoff : 0;
 }
 
-static void modify_pfdt_node(void *pfdt, int nodeoff)
+#ifdef CONFIG_ARM_VIRTUAL_IOMMU
+static void modify_pfdt_node(void *pfdt, int nodeoff, struct domain *d)
 {
    int proplen, i, rc;
    const fdt32_t *prop;
    fdt32_t *prop_c;
+    uint32_t vsid;
 
-    prop = fdt_getprop(pfdt, nodeoff, "iommus", &proplen);
+    prop = fdt_getprop(pfdt, nodeoff, "iommus", &proplen); 
    if ( !prop )
        return;
 
    prop_c = xzalloc_bytes(proplen);
 

xen/common/device-tree/dom0less-build.c static int __init handle_prop_pfdt(struct kernel_info *kinfo, · line 345

Quoted 10 lines
xen/common/device-tree/dom0less-build.c +6 -1
+    /* 
+     * Assign <vIOMMU vSID> pairs to iommus property and establish
+     * vSID->pSID mappings
+    */
    for ( i = 0; i < proplen / 8; ++i )
    {
        prop_c[i * 2] = cpu_to_fdt32(GUEST_PHANDLE_VSMMUV3);
-        prop_c[i * 2 + 1] = prop[i * 2 + 1];
+        rc = viommu_allocate_free_vid(d, fdt32_to_cpu(prop[i * 2 + 1]), &vsid);
+        if( rc ) {

NIT: codestyle, space after ‘if'

Quoted 147 lines
xen/common/device-tree/dom0less-build.c +12 -5
+            dprintk(XENLOG_ERR, "Failed to allocate new vSID for iommu device");
+            return;
+        }
+        prop_c[i * 2 + 1] = cpu_to_fdt32(vsid);
    }
 
    rc = fdt_setprop(pfdt, nodeoff, "iommus", prop_c, proplen);
@@ -345,11 +358,14 @@ static void modify_pfdt_node(void *pfdt, int nodeoff)
 
    return;
 }
+#else
+    static void modify_pfdt_node(void *pfdt, int nodeoff, struct domain *d) {}
+#endif
 
 static int __init scan_pfdt_node(struct kernel_info *kinfo, void *pfdt,
                                 int nodeoff,
                                 uint32_t address_cells, uint32_t size_cells,
-                                 bool scan_passthrough_prop)
+                                 bool scan_passthrough_prop, struct domain *d)
 {
    int rc = 0;
    void *fdt = kinfo->fdt;
@@ -372,9 +388,9 @@ static int __init scan_pfdt_node(struct kernel_info *kinfo, void *pfdt,
    node_next = fdt_first_subnode(pfdt, nodeoff);
    while ( node_next > 0 )
    {
-        modify_pfdt_node(pfdt, node_next);
+        modify_pfdt_node(pfdt, node_next, d);
        rc = scan_pfdt_node(kinfo, pfdt, node_next, address_cells, size_cells,
-                            scan_passthrough_prop);
+                            scan_passthrough_prop, d);
        if ( rc )
            return rc;
 
@@ -443,7 +459,7 @@ static int __init domain_handle_dtb_boot_module(struct domain *d,
            res = scan_pfdt_node(kinfo, pfdt, node_next,
                                 DT_ROOT_NODE_ADDR_CELLS_DEFAULT,
                                 DT_ROOT_NODE_SIZE_CELLS_DEFAULT,
-                                 false);
+                                 false, d);
            if ( res )
                goto out;
            continue;
@@ -453,7 +469,7 @@ static int __init domain_handle_dtb_boot_module(struct domain *d,
            res = scan_pfdt_node(kinfo, pfdt, node_next,
                                 DT_ROOT_NODE_ADDR_CELLS_DEFAULT,
                                 DT_ROOT_NODE_SIZE_CELLS_DEFAULT,
-                                 true);
+                                 true, d);
            if ( res )
                goto out;
            continue;
xen/drivers/passthrough/arm/viommu.c +7 -0
index 5f5892fbb2..4b7837a91f 100644
@@ -71,6 +71,13 @@ int viommu_relinquish_resources(struct domain *d)
    return cur_viommu->ops->relinquish_resources(d);
 }
 
+int viommu_allocate_free_vid(struct domain *d, uint32_t id, uint32_t *vid) {
+    if ( !cur_viommu )
+        return -ENODEV;
+
+    return cur_viommu->ops->allocate_free_vid(d, id, vid);
+}
+
 uint16_t viommu_get_type(void)
 {
    if ( !cur_viommu )
xen/drivers/passthrough/arm/vsmmu-v3.c +35 -1
index 5d0dabd2b2..604f09e980 100644
@@ -53,6 +53,8 @@ extern const struct viommu_desc __read_mostly *cur_viommu;
 #define smmu_get_ste_s1ctxptr(x)    FIELD_PREP(STRTAB_STE_0_S1CTXPTR_MASK, \
                                    FIELD_GET(STRTAB_STE_0_S1CTXPTR_MASK, x))
 
+#define MAX_VSID   (1 << SMMU_IDR1_SIDSIZE)
+
 /* event queue entry */
 struct arm_smmu_evtq_ent {
    /* Common fields */
@@ -100,6 +102,14 @@ struct arm_vsmmu_queue {
    uint8_t     max_n_shift;
 };
 
+/* vSID->pSID mapping entry */
+struct vsid_entry {
+    bool        valid;
+    uint32_t    vsid;
+    struct host_iommu *phys_smmu;
+    uint32_t    psid;
+};
+
 struct virt_smmu {
    struct      domain *d;
    struct      list_head viommu_list;
@@ -118,6 +128,7 @@ struct virt_smmu {
    uint64_t    evtq_irq_cfg0;
    struct      arm_vsmmu_queue evtq, cmdq;
    spinlock_t  cmd_queue_lock;
+    struct vsid_entry *vsids;
 };
 
 /* Queue manipulation functions */
@@ -426,6 +437,29 @@ static int arm_vsmmu_handle_cfgi_ste(struct virt_smmu *smmu, uint64_t *cmdptr)
    struct arm_vsmmu_s1_trans_cfg s1_cfg = {0};
    uint32_t sid = smmu_cmd_get_sid(cmdptr[0]);
    struct iommu_guest_config guest_cfg = {0};
+    uint32_t psid;
+    struct arm_smmu_evtq_ent ent = {
+        .opcode = EVT_ID_BAD_STE,
+        .sid = sid,
+        .c_bad_ste_streamid = {
+            .ssid = 0,
+            .ssv = false,
+        },
+    };
+
+    /* SIDs identity mapped for HW domain */
+    if ( is_hardware_domain(d) )
+        psid = sid;
+    else {
+        /* vSID out of range or not mapped to pSID */
+        if ( sid >= MAX_VSID || !smmu->vsids[sid].valid )
+        {
+            arm_vsmmu_send_event(smmu, &ent);
+            return -EINVAL;
+        }
+
+        psid = smmu->vsids[sid].psid;
+    }
 
    ret = arm_vsmmu_find_ste(smmu, sid, ste);
    if ( ret )
@@ -446,7 +480,7 @@ static int arm_vsmmu_handle_cfgi_ste(struct virt_smmu *smmu, uint64_t *cmdptr)
    else
        guest_cfg.config = ARM_SMMU_DOMAIN_NESTED;
 
-    ret = hd->platform_ops->attach_guest_config(d, sid, &guest_cfg);
+    ret = hd->platform_ops->attach_guest_config(d, psid, &guest_cfg);
    if ( ret )
        return ret;
 

xen/drivers/passthrough/arm/vsmmu-v3.c static int vsmmuv3_init_single(struct domain *d, paddr_t addr, · line 828

Quoted 4 lines
xen/drivers/passthrough/arm/vsmmu-v3.c +1 -0
@@ -791,6 +825,7 @@ static int vsmmuv3_init_single(struct domain *d, paddr_t addr,
    smmu->cmdq.ent_size = CMDQ_ENT_DWORDS * DWORDS_BYTES;
    smmu->evtq.q_base = FIELD_PREP(Q_BASE_LOG2SIZE, SMMU_EVTQS);
    smmu->evtq.ent_size = EVTQ_ENT_DWORDS * DWORDS_BYTES;
+    smmu->vsids = xzalloc_array(struct vsid_entry, MAX_VSID);

we should check if allocation is ok

Quoted 19 lines
xen/drivers/passthrough/arm/vsmmu-v3.c +3 -1
    spin_lock_init(&smmu->cmd_queue_lock);
 
@@ -850,8 +885,9 @@ int vsmmuv3_relinquish_resources(struct domain *d)
    if ( list_head_is_null(&d->arch.viommu_list) )
        return 0;
 
-    list_for_each_entry_safe(pos, temp, &d->arch.viommu_list, viommu_list )
+    list_for_each_entry_safe(pos, temp, &d->arch.viommu_list, viommu_list)
    {
+        xfree(pos->vsids);
        list_del(&pos->viommu_list);
        xfree(pos);
    }
@@ -859,8 +895,35 @@ int vsmmuv3_relinquish_resources(struct domain *d)
    return 0;
 }
 
+int vsmmuv3_allocate_free_vid(struct domain *d, uint32_t id, uint32_t *vid) {

xen/drivers/passthrough/arm/vsmmu-v3.c int vsmmuv3_relinquish_resources(struct domain *d) · line 908

Quoted 10 lines
xen/drivers/passthrough/arm/vsmmu-v3.c +10 -0
+    uint16_t i = 0;
+    struct virt_smmu *smmu;
+
+    if ( list_head_is_null(&d->arch.viommu_list) )
+        return -ENODEV;
+
+    smmu = list_first_entry(&d->arch.viommu_list, struct virt_smmu, viommu_list);
+
+    /* Get first free vSID index */
+    while ( smmu->vsids[i].valid && i++ < MAX_VSID );

This will access OOB when you will reach MAX_VSID and later evaluate smmu->vsids[i].valid, I suggest to modify like this:

while ( i < MAX_VSID && smmu->vsids[i].valid )

    i++;

xen/drivers/passthrough/arm/vsmmu-v3.c int vsmmuv3_relinquish_resources(struct domain *d) · line 911

Quoted 3 lines
xen/drivers/passthrough/arm/vsmmu-v3.c +3 -0
+
+    /* Max number of vSIDs already allocated? */
+    if ( i == MAX_VSID) {

NIT: code style, space before ‘)'

xen/drivers/passthrough/arm/vsmmu-v3.c int vsmmuv3_relinquish_resources(struct domain *d) · line 919

Quoted 8 lines
xen/drivers/passthrough/arm/vsmmu-v3.c +8 -0
+        return -ENOMEM;
+    }
+
+    /* Establish vSID->pSID mapping */
+    smmu->vsids[i].valid = true;
+    smmu->vsids[i].vsid = i;
+    smmu->vsids[i].psid = id;
+    *vid = smmu->vsids[i].vsid;

Why is phys_smmu never populated and used?

xen/drivers/passthrough/arm/vsmmu-v3.c int vsmmuv3_relinquish_resources(struct domain *d) · line 929

Quoted 10 lines
xen/drivers/passthrough/arm/vsmmu-v3.c +5 -0
+
+    return 0;
+}
+
 static const struct viommu_ops vsmmuv3_ops = {
    .domain_init = domain_vsmmuv3_init,
+    .allocate_free_vid = vsmmuv3_allocate_free_vid,
    .relinquish_resources = vsmmuv3_relinquish_resources,
 };
 

Cheers,
Luca

#118 lore raw

PATCH v3 22/23 libxl/arm: Introduce domctl command for IOMMU vSID/vRID mapping

In reply to Milan Djokic

For guests created via control domain (xl, zephyr xenlib), partial device tree is parsed and loaded on control domain side.
SIDs in guests device tree have to be replaced with
virtual SIDs which are mapped to physical SIDs. In order
to do that, control domain has to request from Xen to create
a new vSID and map it to original pSID for every guest device IOMMU stream ID. For this purpose, new domctl command (XEN_DOMCTL_viommu_allocate_vid) is introduced which control domain can use to request a new vSID mapping and insert a new vSID into guest device tree once mapped.
Requested vSID allocation using this interface for vPCI/DT devices.

---
 tools/include/xenctrl.h             |  12 +++
 tools/libs/ctrl/xc_domain.c         |  23 +++++
 tools/libs/light/libxl_arm.c        | 127 +++++++++++++++++++++++++---
 xen/arch/arm/domctl.c               |  34 ++++++++
 xen/include/public/domctl.h         |  20 +++++
 xen/xsm/flask/hooks.c               |   4 +
 xen/xsm/flask/policy/access_vectors |   2 +
 7 files changed, 212 insertions(+), 10 deletions(-)
tools/include/xenctrl.h +12 -0
index d5dbf69c89..61be892cc8 100644
@@ -2659,6 +2659,18 @@ int xc_domain_set_llc_colors(xc_interface *xch, uint32_t domid,
                              const uint32_t *llc_colors,
                              uint32_t num_llc_colors);
 
+/*
+ * Allocate guest IOMMU vSID and establish its mapping to pSID.
+ * It can only be used on domain DT creation.
+ * Currently used for ARM only, possibly for RISC-V in the
+ * future. Function has no effect for x86.
+ */
+int xc_domain_viommu_allocate_vsid_range(xc_interface *xch,
+                                         uint32_t domid,
+                                         uint16_t nr_sids,
+                                         uint32_t first_psid,
+                                         uint32_t *first_vsid);
+
 #if defined(__arm__) || defined(__aarch64__)
 int xc_dt_overlay(xc_interface *xch, void *overlay_fdt,
                   uint32_t overlay_fdt_size, uint8_t overlay_op);
tools/libs/ctrl/xc_domain.c +23 -0
index 01c0669c88..39ffe80e6d 100644
@@ -2222,6 +2222,29 @@ out:
 
     return ret;
 }
+
+int xc_domain_viommu_allocate_vsid_range(xc_interface *xch,
+                                         uint32_t domid,
+                                         uint16_t nr_sids,
+                                         uint32_t first_psid,
+                                         uint32_t *first_vsid)
+{
+    int err;
+    struct xen_domctl domctl = {};
+
+    domctl.cmd = XEN_DOMCTL_viommu_alloc_vsid_range;
+    domctl.domain = domid;
+    domctl.u.viommu_alloc_vsid_range.first_psid = first_psid;
+    domctl.u.viommu_alloc_vsid_range.nr_sids = nr_sids;
+
+    if ( (err = do_domctl(xch, &domctl)) != 0 )
+        return err;
+
+    *first_vsid = domctl.u.viommu_alloc_vsid_range.first_vsid;
+
+    return 0;
+}
+
 /*
  * Local variables:
  * mode: C
tools/libs/light/libxl_arm.c +117 -10

180 lines not on this page · raw message

xen/arch/arm/domctl.c +34 -0
index ad914c915f..c85853e4cb 100644
@@ -16,6 +16,7 @@
 #include <xen/types.h>
 #include <xsm/xsm.h>
 #include <public/domctl.h>
+#include <asm/viommu.h>
 
 void arch_get_domain_info(const struct domain *d,
                           struct xen_domctl_getdomaininfo *info)
@@ -179,6 +180,39 @@ long arch_do_domctl(struct xen_domctl *domctl, struct domain *d,
     }
     case XEN_DOMCTL_dt_overlay:
         return dt_overlay_domctl(d, &domctl->u.dt_overlay);
+
+#ifdef CONFIG_ARM_VIRTUAL_IOMMU
+    case XEN_DOMCTL_viommu_alloc_vsid_range:
+    {
+        int rc = 0;
+        uint16_t i;
+        uint32_t vsid;
+        struct xen_domctl_viommu_alloc_vsid_range *viommu_alloc_vsid_range =
+            &domctl->u.viommu_alloc_vsid_range;
+
+        if ( viommu_alloc_vsid_range->pad )
+            return -EINVAL;
+
+        for ( i = 0; i < viommu_alloc_vsid_range->nr_sids; i++ )
+        {
+            rc = viommu_allocate_free_vid(d, viommu_alloc_vsid_range->first_psid
+                                            + i, &vsid);
+            if( rc )
+                return rc;
+        }
+
+        if ( !rc )
+        {
+            /* Calculate first vSID from allocated range */
+            viommu_alloc_vsid_range->first_vsid = vsid -
+                viommu_alloc_vsid_range->nr_sids + 1;
+            rc = copy_to_guest(u_domctl, domctl, 1);
+        }
+
+        return rc;
+    }
+#endif
+
     default:
         return subarch_do_domctl(domctl, d, u_domctl);
     }
xen/include/public/domctl.h +20 -0
index 23124547f3..190aed1e59 100644
@@ -1276,6 +1276,24 @@ struct xen_domctl_get_domain_state {
     uint64_t unique_id;      /* Unique domain identifier. */
 };
 
+/*
+ * XEN_DOMCTL_viommu_alloc_vsid_range
+ *
+ * Allocate guest vSID range and
+ * establish pSID->vSID mapping for target range.
+ * Allocated range is continous
+ */
+struct xen_domctl_viommu_alloc_vsid_range {
+    /* IN: Range first pSID  */
+    uint32_t first_psid;
+    /* IN: Number of vSIDs to allocate */
+    uint16_t nr_sids;
+    /* padding, must be 0 */
+    uint16_t pad;
+    /* OUT: Mapped range first vSID */
+    uint32_t first_vsid;
+};
+
 struct xen_domctl {
 /* Stable domctl ops: interface_version is required to be 0.  */
     uint32_t cmd;
@@ -1368,6 +1386,7 @@ struct xen_domctl {
 #define XEN_DOMCTL_gsi_permission                88
 #define XEN_DOMCTL_set_llc_colors                89
 #define XEN_DOMCTL_get_domain_state              90 /* stable interface */
+#define XEN_DOMCTL_viommu_alloc_vsid_range       91
 #define XEN_DOMCTL_gdbsx_guestmemio            1000
 #define XEN_DOMCTL_gdbsx_pausevcpu             1001
 #define XEN_DOMCTL_gdbsx_unpausevcpu           1002
@@ -1436,6 +1455,7 @@ struct xen_domctl {
 #endif
         struct xen_domctl_set_llc_colors    set_llc_colors;
         struct xen_domctl_get_domain_state  get_domain_state;
+        struct xen_domctl_viommu_alloc_vsid_range viommu_alloc_vsid_range;
         uint8_t                             pad[128];
     } u;
 };
xen/xsm/flask/hooks.c +4 -0
index b250b27065..91e80ea80d 100644
@@ -820,6 +820,10 @@ static int cf_check flask_domctl(struct domain *d, unsigned int cmd,
     case XEN_DOMCTL_set_llc_colors:
         return current_has_perm(d, SECCLASS_DOMAIN2, DOMAIN2__SET_LLC_COLORS);
 
+    case XEN_DOMCTL_viommu_alloc_vsid_range:
+        return current_has_perm(d, SECCLASS_DOMAIN2,
+            DOMAIN2__VIOMMU_ALLOC_VSID_RANGE);
+
     default:
         return avc_unknown_permission("domctl", cmd);
     }
xen/xsm/flask/policy/access_vectors +2 -0
index ce907d50a4..e4ffe2f5db 100644
@@ -255,6 +255,8 @@ class domain2
     set_llc_colors
 # XEN_DOMCTL_get_domain_state
     get_domain_state
+# XEN_DOMCTL_viommu_alloc_vsid_range
+    viommu_alloc_vsid_range
 }
 
 # Similar to class domain, but primarily contains domctls related to HVM domains
Signature · 1 line
2.43.0
#119 lore raw

Re: [PATCH v3 22/23] libxl/arm: Introduce domctl command for IOMMU vSID/vRID mapping

Hi Milan,

Quoted 86 lines · Milan Djokic

For guests created via control domain (xl, zephyr xenlib), partial device tree is parsed and loaded on control domain side.
SIDs in guests device tree have to be replaced with
virtual SIDs which are mapped to physical SIDs. In order
to do that, control domain has to request from Xen to create
a new vSID and map it to original pSID for every guest device IOMMU stream ID. For this purpose, new domctl command (XEN_DOMCTL_viommu_allocate_vid) is introduced which control domain can use to request a new vSID mapping and insert a new vSID into guest device tree once mapped.
Requested vSID allocation using this interface for vPCI/DT devices.

---

tools/include/xenctrl.h             |  12 +++
tools/libs/ctrl/xc_domain.c         |  23 +++++
tools/libs/light/libxl_arm.c        | 127 +++++++++++++++++++++++++---
xen/arch/arm/domctl.c               |  34 ++++++++
xen/include/public/domctl.h         |  20 +++++
xen/xsm/flask/hooks.c               |   4 +
xen/xsm/flask/policy/access_vectors |   2 +
7 files changed, 212 insertions(+), 10 deletions(-)
tools/include/xenctrl.h +12 -0
index d5dbf69c89..61be892cc8 100644
@@ -2659,6 +2659,18 @@ int xc_domain_set_llc_colors(xc_interface *xch, uint32_t domid,
                             const uint32_t *llc_colors,
                             uint32_t num_llc_colors);
 
+/*
+ * Allocate guest IOMMU vSID and establish its mapping to pSID.
+ * It can only be used on domain DT creation.
+ * Currently used for ARM only, possibly for RISC-V in the
+ * future. Function has no effect for x86.
+ */
+int xc_domain_viommu_allocate_vsid_range(xc_interface *xch,
+                                         uint32_t domid,
+                                         uint16_t nr_sids,
+                                         uint32_t first_psid,
+                                         uint32_t *first_vsid);
+
 #if defined(__arm__) || defined(__aarch64__)
 int xc_dt_overlay(xc_interface *xch, void *overlay_fdt,
                  uint32_t overlay_fdt_size, uint8_t overlay_op);
tools/libs/ctrl/xc_domain.c +23 -0
index 01c0669c88..39ffe80e6d 100644
@@ -2222,6 +2222,29 @@ out:
 
    return ret;
 }
+
+int xc_domain_viommu_allocate_vsid_range(xc_interface *xch,
+                                         uint32_t domid,
+                                         uint16_t nr_sids,
+                                         uint32_t first_psid,
+                                         uint32_t *first_vsid)
+{
+    int err;
+    struct xen_domctl domctl = {};
+
+    domctl.cmd = XEN_DOMCTL_viommu_alloc_vsid_range;
+    domctl.domain = domid;
+    domctl.u.viommu_alloc_vsid_range.first_psid = first_psid;
+    domctl.u.viommu_alloc_vsid_range.nr_sids = nr_sids;
+
+    if ( (err = do_domctl(xch, &domctl)) != 0 )
+        return err;
+
+    *first_vsid = domctl.u.viommu_alloc_vsid_range.first_vsid;
+
+    return 0;
+}
+
 /*
 * Local variables:
 * mode: C
tools/libs/light/libxl_arm.c +0 -0
index 7b887898bb..79904b746c 100644

tools/libs/light/libxl_arm.c static int make_vsmmuv3_node(libxl__gc *gc, void *fdt, · line 963

Quoted 9 lines
tools/libs/light/libxl_arm.c +6 -0
@@ -955,6 +955,13 @@ static int make_vsmmuv3_node(libxl__gc *gc, void *fdt,
    return 0;
 }
 
+/*
+ * Stores starting vSID of vPCI IOMMU SID range
+ * Used as a lookup value to avoid repeated
+ * vSID range allocation on every fdt resize.
+ */
+static int vpci_first_vsid = -1;

What happen between one guest creation and another since this and viommu_stream_list are global?

Quoted 77 lines
tools/libs/light/libxl_arm.c +53 -2
+
 static int make_vpci_node(libxl__gc *gc, void *fdt,
                          const struct arch_info *ainfo,
                          struct xc_dom_image *dom)
@@ -963,6 +970,9 @@ static int make_vpci_node(libxl__gc *gc, void *fdt,
    const uint64_t vpci_ecam_base = GUEST_VPCI_ECAM_BASE;
    const uint64_t vpci_ecam_size = GUEST_VPCI_ECAM_SIZE;
    const char *name = GCSPRINTF("pcie@%"PRIx64, vpci_ecam_base);
+    uint16_t iommu_range_size = 0x1000;
+    uint32_t first_vsid;
+    uint32_t first_psid = 0;
 
    res = fdt_begin_node(fdt, name);
    if (res) return res;
@@ -996,8 +1006,20 @@ static int make_vpci_node(libxl__gc *gc, void *fdt,
        GUEST_VPCI_PREFETCH_MEM_SIZE);
    if (res) return res;
 
+    /* request vSID range allocation if not already allocated */
+    if (vpci_first_vsid < 0) {
+        res = xc_domain_viommu_allocate_vsid_range(CTX->xch, dom->guest_domid,
+            iommu_range_size, first_psid, &first_vsid);
+        if (res)
+            return res;
+        vpci_first_vsid = first_vsid;
+    }
+    else {
+        first_vsid = vpci_first_vsid;
+    }
+
    res = fdt_property_values(gc, fdt, "iommu-map", 4, 0,
-                              GUEST_PHANDLE_VSMMUV3, 0, 0x10000);
+                             GUEST_PHANDLE_VSMMUV3, first_vsid, iommu_range_size);
    if (res) return res;
 
    res = fdt_end_node(fdt);
@@ -1326,11 +1348,92 @@ static int copy_partial_fdt(libxl__gc *gc, void *fdt, void *pfdt)
    return 0;
 }
 
-static int modify_partial_fdt(libxl__gc *gc, void *pfdt)
+/*
+ * Store virtualized 'iommus' properties for every node attached to IOMMU
+ * and passthroughed to guest.
+ * Used as a lookup table for mapping <phandle pSID> -> <vhandle vSID>
+ */
+struct viommu_stream {
+    XEN_LIST_ENTRY(struct viommu_stream) entry;
+    char path[128];          /* DT path, stable across resizes */
+    fdt32_t *iommus;         /* fully virtualized iommus property */
+};
+
+static XEN_LIST_HEAD(, struct viommu_stream) viommu_stream_list;
+
+/*
+ * Helper function which creates mapping of dt node to
+ * to virtualized 'iommus' property
+ * Mappings stored in a global 'viommu_stream_list' to
+ * make it reusable for every fdt resize
+ */
+static int viommu_get_stream(libxl__gc *gc,
+                             uint32_t domid,
+                             const fdt32_t *prop,
+                             int proplen,
+                             const char* path, fdt32_t **iommus)
+{
+    int i, r;
+    uint32_t vsid, psid;
+    struct viommu_stream *viommu_stream;
+
+    /* Lookup if stream for target device is already allocated */
+    XEN_LIST_FOREACH(viommu_stream, &viommu_stream_list, entry)
+    {
+        if (!strcmp(viommu_stream->path, path)) {
+            *iommus = viommu_stream->iommus;
+            return 0;
+        }

tools/libs/light/libxl_arm.c static int copy_partial_fdt(libxl__gc *gc, void *fdt, void *pfdt) · line 1396

Quoted 10 lines
tools/libs/light/libxl_arm.c +10 -0
+    }
+
+    /* Allocate new viommu stream */
+    viommu_stream = malloc(sizeof(struct viommu_stream));
+    if (!viommu_stream)
+        return ERROR_NOMEM;
+    memset(viommu_stream, 0, sizeof(struct viommu_stream));
+    viommu_stream->iommus = malloc(proplen);
+    if (!viommu_stream->iommus)
+        return ERROR_NOMEM;

we should free viommu_stream here

Quoted 114 lines
tools/libs/light/libxl_arm.c +48 -8
+    memset(viommu_stream->iommus, 0, proplen);
+
+    LOG(DEBUG, "Creating vIOMMU stream for device %s",
+        path);
+
+    /*
+     * Virtualize device "iommus" property
+     * (replace pIOMMU with vIOMMU phandle and pSIDs with mapped vSIDs)
+     */
+    for (i = 0; i < proplen / 8; ++i) {
+        viommu_stream->iommus[i * 2] = cpu_to_fdt32(GUEST_PHANDLE_VSMMUV3);
+        /* Allocate new vSID mapped to pSID */
+        psid = fdt32_to_cpu(prop[i * 2 + 1]);
+        r = xc_domain_viommu_allocate_vsid_range(CTX->xch, domid, 1, psid, &vsid);
+        if (r) {
+            LOG(ERROR, "Can't allocate new vSID/vRID for guest IOMMU device");
+            return r;
+        }
+        viommu_stream->iommus[i * 2 + 1] = cpu_to_fdt32(vsid);
+        LOG(DEBUG, "Mapped vSID: %u to pSID: %u", vsid, psid);
+    }
+
+    strcpy(viommu_stream->path, path);
+    *iommus =  viommu_stream->iommus;
+
+    XEN_LIST_INSERT_HEAD(&viommu_stream_list, viommu_stream, entry);
+
+    return 0;
+}
+
+/*
+ * Used to update partial fdt when vIOMMU is enabled
+ * Maps dt properties of IOMMU devices to virtual IOMMU
+ */
+static int viommu_modify_partial_fdt(libxl__gc *gc, void *pfdt, uint32_t domid)
 {
-    int nodeoff, proplen, i, r;
+    int nodeoff, proplen, r;
    const fdt32_t *prop;
    fdt32_t *prop_c;
+    char path[128];
 
    nodeoff = fdt_path_offset(pfdt, "/passthrough");
    if (nodeoff < 0)
@@ -1344,11 +1447,16 @@ static int modify_partial_fdt(libxl__gc *gc, void *pfdt)
        if (!prop)
            continue;
 
-        prop_c = libxl__zalloc(gc, proplen);
+        r = fdt_get_path(pfdt, nodeoff, path, sizeof(path));
+        if ( r < 0 ) {
+            LOG(ERROR, "Can't get passthrough node path");
+            return r;
+        }
 
-        for (i = 0; i < proplen / 8; ++i) {
-            prop_c[i * 2] = cpu_to_fdt32(GUEST_PHANDLE_VSMMUV3);
-            prop_c[i * 2 + 1] = prop[i * 2 + 1];
+        r = viommu_get_stream(gc, domid, prop, proplen, path, &prop_c);
+        if (r) {
+            LOG(ERROR, "Can't get viommu stream");
+            return r;
        }
 
        r = fdt_setprop(pfdt, nodeoff, "iommus", prop_c, proplen);
@@ -1360,7 +1468,6 @@ static int modify_partial_fdt(libxl__gc *gc, void *pfdt)
 
    return 0;
 }
-
 #else
 
 static int check_partial_fdt(libxl__gc *gc, void *fdt, size_t size)
@@ -1379,7 +1486,7 @@ static int copy_partial_fdt(libxl__gc *gc, void *fdt, void *pfdt)
    return -FDT_ERR_INTERNAL;
 }
 
-static int modify_partial_fdt(libxl__gc *gc, void *pfdt)
+static int viommu_modify_partial_fdt(libxl__gc *gc, void *pfdt, uint32_t domid)
 {
    LOG(ERROR, "partial device tree not supported");
 
@@ -1511,7 +1618,7 @@ next_resize:
        if (info->arch_arm.viommu_type == LIBXL_VIOMMU_TYPE_SMMUV3) {
            FDT( make_vsmmuv3_node(gc, fdt, ainfo, dom) );
            if (pfdt)
-                FDT( modify_partial_fdt(gc, pfdt) );
+                FDT( viommu_modify_partial_fdt(gc, pfdt, dom->guest_domid) );
        }
 
        for (i = 0; i < d_config->num_disks; i++) {
xen/arch/arm/domctl.c +8 -0
index ad914c915f..c85853e4cb 100644
@@ -16,6 +16,7 @@
 #include <xen/types.h>
 #include <xsm/xsm.h>
 #include <public/domctl.h>
+#include <asm/viommu.h>
 
 void arch_get_domain_info(const struct domain *d,
                          struct xen_domctl_getdomaininfo *info)
@@ -179,6 +180,39 @@ long arch_do_domctl(struct xen_domctl *domctl, struct domain *d,
    }
    case XEN_DOMCTL_dt_overlay:
        return dt_overlay_domctl(d, &domctl->u.dt_overlay);
+
+#ifdef CONFIG_ARM_VIRTUAL_IOMMU
+    case XEN_DOMCTL_viommu_alloc_vsid_range:
+    {
+        int rc = 0;
+        uint16_t i;
+        uint32_t vsid;

xen/arch/arm/domctl.c long arch_do_domctl(struct xen_domctl *domctl, struct domain *d, · line 199

Quoted 10 lines
xen/arch/arm/domctl.c +10 -0
+        struct xen_domctl_viommu_alloc_vsid_range *viommu_alloc_vsid_range =
+            &domctl->u.viommu_alloc_vsid_range;
+
+        if ( viommu_alloc_vsid_range->pad )
+            return -EINVAL;
+
+        for ( i = 0; i < viommu_alloc_vsid_range->nr_sids; i++ )
+        {
+            rc = viommu_allocate_free_vid(d, viommu_alloc_vsid_range->first_psid
+                                            + i, &vsid);

Since we are using like this, wouldn’t have been better to have an “viommu_allocate_free_vid_range()”?

xen/arch/arm/domctl.c long arch_do_domctl(struct xen_domctl *domctl, struct domain *d, · line 209

Quoted 10 lines
xen/arch/arm/domctl.c +10 -0
+            if( rc )
+                return rc;
+        }
+
+        if ( !rc )
+        {
+            /* Calculate first vSID from allocated range */
+            viommu_alloc_vsid_range->first_vsid = vsid -
+                viommu_alloc_vsid_range->nr_sids + 1;
+            rc = copy_to_guest(u_domctl, domctl, 1);

we should use something like:
rc = copy_to_guest(u_domctl, domctl, 1);
if ( rc )

    rc = -EFAULT;

Cheers,
Luca

#120 lore raw

PATCH v3 23/23 doc/arm: vIOMMU design document

In reply to Milan Djokic

This document outlines the design of the emulated IOMMU, including security considerations and future improvements.

---
 docs/designs/arm-viommu.rst | 390 ++++++++++++++++++++++++++++++++++++
 1 file changed, 390 insertions(+)
 create mode 100644 docs/designs/arm-viommu.rst
docs/designs/arm-viommu.rst new file +390 -0
new file mode 100644
index 0000000000..0cf55d7108
@@ -0,0 +1,390 @@
+==========================================================
+Design Proposal: Add SMMUv3 Stage-1 Support for XEN Guests
+==========================================================
+
+:Author:     Milan Djokic <[email protected]>
+:Date:       2026-02-13
+:Status:     Draft
+
+Introduction
+============
+
+The SMMUv3 supports two stages of translation. Each stage of translation 
+can be
+independently enabled. An incoming address is logically translated from 
+VA to
+IPA in stage 1, then the IPA is input to stage 2 which translates the IPA to
+the output PA. Stage 1 translation support is required to provide 
+isolation between different
+devices within OS. XEN already supports Stage 2 translation but there is no
+support for Stage 1 translation.
+This design proposal outlines the introduction of Stage-1 SMMUv3 support 
+in Xen for ARM guests.
+
+Motivation
+==========
+
+ARM systems utilizing SMMUv3 require stage-1 address translation to 
+ensure secure DMA and
+guest managed I/O memory mappings.
+With stage-1 enabled, guest manages IOVA to IPA mappings through its own 
+IOMMU driver.
+
+This feature enables:
+
+- Stage-1 translation for the guest domain
+- Device passthrough with per-device I/O address space
+
+Design Overview
+===============
+
+These changes provide emulated SMMUv3 support:
+
+- **SMMUv3 Stage-1 Translation**: stage-1 and nested translation support
+  in SMMUv3 driver.
+- **vIOMMU Abstraction**: Virtual IOMMU framework for guest stage-1
+  handling.
+- **Register/Command Emulation**: SMMUv3 register emulation and command
+  queue handling.
+- **Device Tree Extensions**: Adds `iommus` and virtual SMMUv3 nodes to
+  device trees for dom0 and dom0less scenarios.
+- **Runtime Configuration**: Introduces a `viommu` boot parameter for
+  dynamic enablement.
+
+A single vIOMMU device is exposed to the guest and mapped to one or more
+physical IOMMUs through a Xen-managed translation layer.
+The vIOMMU feature provides a generic framework together with a backend
+implementation specific to the target IOMMU type. The backend is responsible
+for implementing the hardware-specific data structures and command handling
+logic (currently only SMMUv3 is supported).
+
+This modular design allows the stage-1 support to be reused
+for other IOMMU architectures in the future.
+
+vIOMMU architecture
+===================
+
+Responsibilities:
+
+Guest:
+ - Configures stage-1 via vIOMMU commands.
+ - Handles stage-1 faults received from Xen.
+
+Xen:
+ - Emulates the IOMMU interface (registers, commands, events).
+ - Provides vSID->pSID mappings.
+ - Programs stage-1/stage-2 configuration in the physical IOMMU.
+ - Propagate stage-1 faults to guest.
+
+vIOMMU commands and faults are transmitted between guest and Xen via
+command and event queues (one command/event queue created per guest).
+
+vIOMMU command Flow:
+
+::
+
+    Guest:
+        smmu_cmd(vSID, IOVA -> IPA)
+
+    Xen:
+        trap MMIO read/write
+        translate vSID->pSID
+        store stage-1 state
+        program pIOMMU for (pSID, IPA -> PA)
+
+All hardware programming of the physical IOMMU is performed exclusively by Xen.
+
+vIOMMU Stage-1 fault handling flow:
+
+::
+
+    Xen:
+        receives stage-1 fault
+        triggers vIOMMU callback
+        injects virtual fault
+
+    Guest:
+        receives and handles fault
+
+vSID Mapping Layer
+------------------
+
+Each guest-visible Stream ID (vSID) is mapped by Xen to a physical Stream ID
+(pSID). The mapping is maintained per-domain. The allocation policy guarantees
+vSID uniqueness within a domain while allowing reuse of pSIDs for different
+pIOMMUs.
+
+* Platform devices receive individually allocated vSIDs.
+* PCI devices receive a contiguous vSID range derived from RID space.
+
+
+Supported Device Model
+======================
+
+Currently, the vIOMMU framework supports only devices described via the
+Device Tree (DT) model. This includes platform devices and basic PCI
+devices support instantiated through the vPCI DT node. ACPI-described
+devices are not supported.
+
+Guest assigned platform devices are mapped via `iommus` property:
+
+::
+
+    <&pIOMMU pSID> -> <&vIOMMU vSID>
+
+PCI devices use RID-based mapping via the root complex `iommu-map`:
+
+::
+
+    <RID-base &viommu vSID-base length>
+
+PCI Topology Assumptions and Constraints:
+
+- RID space must be contiguous
+- Pre-defined continuous pSID space (0-0x1000)
+- No runtime PCI reconfiguration
+- Single root complex assumed
+- Mapping is fixed at guest DT construction
+
+Constraints for PCI devices will be addressed as part of the future work on
+this feature.
+
+Security Considerations
+=======================
+
+Stage-1 translation provides isolation between guest devices by
+enforcing a per-device I/O address space, preventing unauthorized DMA.
+With the introduction of emulated IOMMU, additional protection
+mechanisms are required to minimize security risks.
+
+1. Observation:
+---------------
+Support for Stage-1 translation in SMMUv3 introduces new data structures 
+(`s1_cfg` alongside `s2_cfg`)
+and logic to write both Stage-1 and Stage-2 entries in the Stream Table 
+Entry (STE), including an `abort`
+field to handle partial configuration states.
+
+**Risk:**
+Without proper handling, a partially applied configuration
+might leave guest DMA mappings in an inconsistent state, potentially
+enabling unauthorized access or causing cross-domain interference.
+
+**Mitigation:** *(Handled by design)*
+This feature introduces logic that writes both `s1_cfg` and `s2_cfg` to
+STE and manages the `abort` field - only considering
+configuration if fully attached. This ensures  incomplete or invalid
+device configurations are safely ignored by the hypervisor.
+
+2. Observation:
+---------------
+Guests can now invalidate Stage-1 caches; invalidation needs forwarding
+to SMMUv3 hardware to maintain coherence.
+
+**Risk:**
+Failing to propagate cache invalidation could allow stale mappings,
+enabling access to old mappings and possibly
+data leakage or misrouting between devices assigned to the same guest.
+
+**Mitigation:**
+The guest must issue appropriate invalidation commands whenever
+its stage-1 I/O mappings are modified to ensure that translation caches
+remain coherent.
+
+3. Observation:
+---------------
+Introducing optional per-guest enabled features (`viommu` argument in xl 
+guest config) means some guests
+may opt-out.
+
+**Risk:**
+Guests without vIOMMU enabled (stage-2 only) could potentially dominate
+access to the physical command and event queues, since they bypass the
+emulation layer and processing is faster comparing to vIOMMU-enabled guests.
+
+**Mitigation:**
+Audit the impact of emulation overhead effect on IOMMU processing fairness
+in a multi-guest environment.
+Consider enabling/disabling stage-1 on a system level, instead of per-domain.
+
+4. Observation:
+---------------
+Guests have the ability to issue Stage-1 IOMMU commands like cache 
+invalidation, stream table entries
+configuration, etc. An adversarial guest may issue a high volume of 
+commands in rapid succession.
+
+**Risk:**
+Excessive commands requests can cause high hypervisor CPU consumption 
+and disrupt scheduling,
+leading to degraded system responsiveness and potential 
+denial-of-service scenarios.
+
+**Mitigation:**
+
+- Implement vIOMMU commands execution restart and continuation support:
+
+  - Introduce processing budget with only a limited amount of commands
+    handled per invocation.
+  - If additional commands remain pending after the budget is exhausted,
+    defer further processing and resume it asynchronously, e.g. via a
+    per-domain tasklet.
+
+- Batch multiple commands of same type to reduce emulation overhead:
+
+  - Inspect the command queue and group commands that can be processed
+    together (e.g. multiple successive invalidation requests or STE
+    updates for the same SID).
+  - Execute the entire batch in one go, reducing repeated accesses to
+    guest memory and emulation overhead per command.
+  - This reduces CPU time spent in the vIOMMU command processing loop.
+    The optimization is applicable only when consecutive commands of the
+    same type operate on the same SID/context.
+
+5. Observation:
+---------------
+Some guest commands issued towards vIOMMU are propagated to pIOMMU 
+command queue (e.g. TLB invalidate).
+
+**Risk:**
+Excessive commands requests from abusive guest can cause flooding of 
+physical IOMMU command queue,
+leading to degraded pIOMMU responsiveness on commands issued from other 
+guests.
+
+**Mitigation:**
+
+- Batch commands that are propagated to the pIOMMU command queue and
+  implement batch execution pause/continuation.
+  Rely on the same mechanisms as in the previous observation
+  (command continuation and batching of pIOMMU-related commands of the same
+  type and context).
+- If possible, implement domain penalization by adding a per-domain budget
+  for vIOMMU/pIOMMU usage:
+
+  - Apply per-domain dynamic budgeting of allowed IOMMU commands to
+    execute per invocation, reducing the budget for guests with
+    excessive command requests over a longer period of time
+  - Combine with command continuation mechanism
+
+6. Observation:
+---------------
+The vIOMMU feature includes an event queue used to forward IOMMU events
+to the guest (e.g. translation faults, invalid Stream IDs, permission errors).
+A malicious guest may misconfigure its IOMMU state or intentionally trigger
+faults at a high rate.
+
+**Risk:**
+Occurrence of IOMMU events with high frequency can cause Xen to flood the
+event queue and disrupt scheduling with
+high hypervisor CPU load for events handling.
+
+**Mitigation:**
+
+- Implement fail-safe state by disabling events forwarding when faults 
+  are occurred with high frequency and
+  not processed by guest:
+
+  - Introduce a per-domain pending event counter.
+  - Stop forwarding events to the guest once the number of unprocessed
+    events reaches a predefined threshold.
+
+- Consider disabling the emulated event queue for untrusted guests.
+- Note that this risk is more general and may also apply to stage-2-only
+  guests. This section addresses mitigations in the emulated IOMMU layer
+  only. Mitigation of physical event queue flooding should also be considered
+  in the target pIOMMU driver.
+
+Performance Impact
+==================
+
+With iommu stage-1 and nested translation inclusion, performance 
+overhead is introduced comparing to existing,
+stage-2 only usage in Xen. Once mappings are established, translations 
+should not introduce significant overhead.
+Emulated paths may introduce moderate overhead, primarily affecting 
+device initialization and event/command handling.
+Testing is performed on Renesas R-Car platform.
+Performance is mostly impacted by emulated vIOMMU operations, results 
+shown in the following table.
+
++-------------------------------+---------------------------------+
+| vIOMMU Operation              | Execution time in guest         |
++===============================+=================================+
+| Reg read                      | median: 645ns, worst-case: 2us  |
++-------------------------------+---------------------------------+
+| Reg write                     | median: 630ns, worst-case: 1us  |
++-------------------------------+---------------------------------+
+| Invalidate TLB                | median: 2us, worst-case: 10us   |
++-------------------------------+---------------------------------+
+| Invalidate STE                | median: 5us worst_case: 100us   |
++-------------------------------+---------------------------------+
+
+With vIOMMU exposed to guest, guest OS has to initialize IOMMU device
+and configure stage-1 mappings for the devices
+attached to it.
+Following table shows initialization stages which impact stage-1 enabled 
+guest boot time and compares it with
+stage-1 disabled guest.
+
+NOTE: Device probe execution time varies depending on device complexity.
+A USB host controller was selected as the test device in this case.
+
++---------------------+-----------------------+------------------------+
+| Stage               | Stage-1 Enabled Guest | Stage-1 Disabled Guest |
++=====================+=======================+========================+
+| IOMMU Init          | ~10ms                 | /                      |
++---------------------+-----------------------+------------------------+
+| Dev Attach / Mapping| ~100ms                | ~90ms                  |
++---------------------+-----------------------+------------------------+
+
+For devices configured with dynamic DMA mappings, DMA allocate/map/unmap 
+operations performance is
+also impacted on stage-1 enabled guests.
+Dynamic DMA mapping operation trigger emulated IOMMU functions like mmio 
+write/read and TLB invalidations.
+
++---------------+---------------------------+--------------------------+
+| DMA Op        | Stage-1 Enabled Guest     | Stage-1 Disabled Guest   |
++===============+===========================+==========================+
+| dma_alloc     | median: 20us, worst: 5ms  | median: 8us, worst: 60us |
++---------------+---------------------------+--------------------------+
+| dma_free      | median: 500us, worst: 10ms| median: 6us, worst: 30us |
++---------------+---------------------------+--------------------------+
+| dma_map       | median: 12us, worst: 60us | median: 3us, worst: 20us |
++---------------+---------------------------+--------------------------+
+| dma_unmap     | median: 400us, worst: 5ms | median: 5us, worst: 20us |
++---------------+---------------------------+--------------------------+
+
+Testing
+=======
+
+- QEMU-based ARM system tests for Stage-1 translation.
+- Actual hardware validation to ensure compatibility with real SMMUv3 
+implementations.
+- Unit/Functional tests validating correct translations (not implemented).
+
+Migration and Compatibility
+===========================
+
+This optional feature defaults to disabled (`viommu=""`) for backward 
+compatibility.
+
+Future improvements
+===================
+
+- Implement the proposed mitigations to address security risks that are 
+  not covered by the current design
+  (events batching, commands execution continuation)
+- PCI support
+- Support for other IOMMU HW (Renesas, RISC-V, etc.)
+
+References
+==========
+
+- Original feature implemented by Rahul Singh:
+  
+https://patchwork.kernel.org/project/xen-devel/cover/[email protected]/ 
+
+- SMMUv3 architecture documentation
+- Existing vIOMMU code patterns (KVM, QEMU)
\ No newline at end of file
Signature · 1 line
2.43.0
#121 lore raw

Re: [PATCH v3 23/23] doc/arm: vIOMMU design document

docs/designs/arm-viommu.rst line 307

Quoted 10 lines
docs/designs/arm-viommu.rst new file +10 -0
+Performance Impact
+==================
+
+With iommu stage-1 and nested translation inclusion, performance
+overhead is introduced comparing to existing,
+stage-2 only usage in Xen. Once mappings are established, translations
+should not introduce significant overhead.
+Emulated paths may introduce moderate overhead, primarily affecting
+device initialization and event/command handling.
+Testing is performed on Renesas R-Car platform.

Hi Milan,

I tested this series on a TI platform. I was not able to get the DMA test inside a DOM1 guest

to work with nested virtualization.

On the top level, my changes are:

- Enabled CONFIG_VIRTUAL_IOMMU and CONFIG_VIRTUAL_ARM_SMMU_V3 in Xen

- Added viommu=true in xen-bootargs

- Added a "smmu" node in my DOM1 passthrough node, so that it can be replaced by the viommu.

- Added "iommus" for my DMA controller.

I can see that the smmuv3 is probed successfully inside of DOM1 and my dma-controller is added to the iommu group.

""

[    0.161594] arm-smmu-v3 4040000.iommu: probing device
[    0.162067] arm-smmu-v3 4040000.iommu: ias 48-bit, oas 48-bit (features 0x0000020d) [    0.162993] arm-smmu-v3 4040000.iommu: allocated 65536 entries for cmdq [    0.163730] arm-smmu-v3 4040000.iommu: allocated 32768 entries for evtq [    0.164281] arm_smmu_device_reset
[    0.164559] ARM_SMMU_GBPA reg = 0
[    0.164924] smmu->evtq.q.q_base = 45a0000f
[    0.165609] platform 30100000.dma-controller: Adding to iommu group 0

""

Did you have any changes in the kernel tree as well?

Could you provide the testing setup that you used?

Best Regards,

Abhash

#122 lore raw

Re: [PATCH v3 23/23] doc/arm: vIOMMU design document

Hi Abhash,

Quoted 21 lines · abhash

[You don't often get email from [email protected]. Learn why this is important at https://aka.ms/LearnAboutSenderIdentification ]

docs/designs/arm-viommu.rst line 307

Quoted 10 lines
docs/designs/arm-viommu.rst new file +10 -0
+Performance Impact
+==================
+
+With iommu stage-1 and nested translation inclusion, performance
+overhead is introduced comparing to existing,
+stage-2 only usage in Xen. Once mappings are established, translations
+should not introduce significant overhead.
+Emulated paths may introduce moderate overhead, primarily affecting
+device initialization and event/command handling.
+Testing is performed on Renesas R-Car platform.

Hi Milan,

I tested this series on a TI platform. I was not able to get the DMA test inside a DOM1 guest

to work with nested virtualization.

Quoted 9 lines

On the top level, my changes are:

- Enabled CONFIG_VIRTUAL_IOMMU and CONFIG_VIRTUAL_ARM_SMMU_V3 in Xen

- Added viommu=true in xen-bootargs

- Added a "smmu" node in my DOM1 passthrough node, so that it can be replaced by the viommu.

Xen inserts the emulated "smmu" node into the domain device tree automatically, so there is no need to add it manually. I'm not sure whether this is the cause of the DMA failure, but I suspect that the device tree node could be overwritten during domain creation. Could you try removing the manually added "smmu" node?

Quoted 18 lines

- Added "iommus" for my DMA controller.

I can see that the smmuv3 is probed successfully inside of DOM1 and my dma-controller is added to the iommu group.

""

[  0.161594] arm-smmu-v3 4040000.iommu: probing device
[  0.162067] arm-smmu-v3 4040000.iommu: ias 48-bit, oas 48-bit
(features 0x0000020d)

[    0.162993] arm-smmu-v3 4040000.iommu: allocated 65536 entries for cmdq
[    0.163730] arm-smmu-v3 4040000.iommu: allocated 32768 entries for evtq
[    0.164281] arm_smmu_device_reset
[    0.164559] ARM_SMMU_GBPA reg = 0
[    0.164924] smmu->evtq.q.q_base = 45a0000f
[    0.165609] platform 30100000.dma-controller: Adding to iommu group 0
Quoted 7 lines

""

Did you have any changes in the kernel tree as well?

Could you provide the testing setup that you used?

There are no changes in the kernel tree. The mainline SMMUv3 driver is expected to handle the emulated IOMMU device without any issues.

So far, we have tested this feature on the QEMU virt machine and the Renesas Gen5 board.

Just as a note, I will be uploading a reworked version of this patch series updated according to maintainers suggestions, as well as additional fixes identified during more extensive testing. I expect to post it sometime next week.

In any case, DMA should work for DomU with the current revision. If you still encounter issues after applying the change I suggested, feel free to contact me directly.

Quoted 5 lines

Best Regards,

Abhash

Best regards,
Milan

#123 lore raw
In reply to Milan Djokic

Hi Milan,

Quoted 43 lines · Milan Djokic

This patch series provides emulated SMMUv3 support in Xen, enabling stage-1 translation for the guest OS.

Stage 1 translation support is required to provide isolation between different devices within OS. Xen already supports Stage 2 translation but there is no support for Stage 1 translation. The goal of this work is to support Stage 1 translation for Xen guests.

This patch series represents a continuation of work from Rahul Singh:
https://patchwork.kernel.org/project/xen-devel/cover/[email protected]/
Original patch series is aligned with the newest Xen structure, with the addition of translation layer which provides 1:N vIOMMU->pIOMMU mapping, in order to support passthrough of the devices attached to different physical IOMMUs.

We cannot trust the guest OS to control the SMMUv3 hardware directly as compromised guest OS can corrupt the SMMUv3 configuration and make the system vulnerable. The guest gets the ownership of the stage 1 page tables and also owns stage 1 configuration structures. The Xen handles the root configuration structure (for security reasons), including the stage 2 configuration.

XEN will emulate the SMMUv3 hardware and expose the virtual SMMUv3 to the guest. Guest can use the native SMMUv3 driver to configure the stage 1 translation. When the guest configures the SMMUv3 for Stage 1, XEN will trap the access and configure hardware.

SMMUv3 Driver(Guest OS) -> Configure the Stage-1 translation -> XEN trap access -> XEN SMMUv3 driver configure the HW.

The final patch series commit provides a design document for the emulated IOMMU (arm-viommu.rst), which was previously discussed with the maintainers. Details regarding implementation, future work and security risks are outlined in this document.

---
Changes in v2:
- Updated design and implementation with vIOMMU->pIOMMU mapping layer
- Addressed security risks in the design, provided initial performance measurements
- Addressed comments from previous version
- Tested on Renesas R-Car platform, initial performance measurements for stage-1 vs stage-1-less guests
---

Quoted 8 lines

---
Changes in v3:
- Bump domctl version, added explicit padding for the new domctl structures
- Remove unnecessary changes according to review comments
- Add "ARM" prefix for vIOMMU Kconfig options, since only ARM architecture is supported at this point
- Re-generate go code
- Add missing commit sign-off tags

Can you please avoid posting new revision in reply-to a previous one? This makes tracking quite difficult on the reviewer side. So I ended up starting to post comments on v2. Please have a look at them.

Cheers,

Signature · 1 line
Julien Grall