Skip to contentSkip to conversation

IOMMU support for ARM

PATCH Superseded Julien Grall 2 participants 7 messages May 15, 2014

v6 of 8 Superseded Latest is v8 →

lore
Share & tools
lore
#0 lore raw

v6 cover IOMMU support for ARM

Hello,

This is the sixth version of this patch series to add support for IOMMU on ARM. It also adds driver to support ARM SMMU which is used for instance on Midway.

Major changes in v6:
- The IOMMU core architecture has been pushed upstream
- Drop features callback and add a field in the struct hvm_iommu
- Move PAGE_*_64K definition in a separate patch

For all the other changes see in each patch.

This series also depends on the interrupt reworking series [1].

A working tree can be found here:
git://xenbits.xen.org/people/julieng/xen-unstable.git branch smmu-v6

Sincerely yours,

[1] http://lists.xen.org/archives/html/xen-devel/2014-05/msg01523.html

Julien Grall (4):
xen/arm: p2m: Clean cache PT when the IOMMU doesn't support coherent
walk
xen/arm: grant: Add another entry to map MFN 1:1 in dom0 p2m
xen: iommu: Define PAGE_{SHIFT,SIZE,ALIGN,MASK)_64K drivers/passthrough: arm: Add support for SMMU drivers

 xen/arch/arm/mm.c                    |   33 +-
 xen/arch/arm/p2m.c                   |   38 +-
 xen/drivers/passthrough/arm/Makefile |    1 +
 xen/drivers/passthrough/arm/smmu.c   | 1742 ++++++++++++++++++++++++++++++++++
 xen/drivers/passthrough/iommu.c      |   10 +
 xen/include/xen/hvm/iommu.h          |    6 +
 xen/include/xen/iommu.h              |   23 +-
 7 files changed, 1837 insertions(+), 16 deletions(-)
 create mode 100644 xen/drivers/passthrough/arm/smmu.c
Signature · 1 line
1.7.10.4
#1 lore raw

PATCH v6 1/4 xen/arm: p2m: Clean cache PT when the IOMMU doesn't support coherent walk

Some IOMMU don't suppport coherent PT walk. When the p2m is shared with the CPU, Xen has to make sure the PT changes have reached the memory.

Introduce new IOMMU function that will check if the IOMMU feature is enabled for a specified domain.

On ARM, the platform can contain multiple IOMMUs. Each of them may not have the same set of feature. The domain parameter will be used to get the set of features for IOMMUs used by this domain.

Cc Jan Beulich <[email protected]>
Cc Xiantao Zhang <[email protected]>

---
Changes in v6:
- Rework the condition to flush cache for PT
- Use {set,clear,test}_bit
- Store features in hvm_iommu structure and add accessor
- Don't specificed value in the enum

Changes in v5:
- Flush on every write_pte instead of unmap page. This will avoid to flush a whole page when only few bytes are modified
- Only get iommu feature once.
- Add bits to flush cache when a new table is created
- Fix typoes in commit message and comment
- Use an enum to describe the feature. Each items are a bit position

Changes in v4:
- Patch added

---
 xen/arch/arm/p2m.c              |   38 ++++++++++++++++++++++++++++++--------
 xen/drivers/passthrough/iommu.c |   10 ++++++++++
 xen/include/xen/hvm/iommu.h     |    6 ++++++
 xen/include/xen/iommu.h         |    8 ++++++++
 4 files changed, 54 insertions(+), 8 deletions(-)
xen/arch/arm/p2m.c +30 -8
index 816da21..dd145c1 100644
@@ -253,9 +253,15 @@ static lpae_t mfn_to_p2m_entry(unsigned long mfn, unsigned int mattr,
     return e;
 }
 
+static inline void p2m_write_pte(lpae_t *p, lpae_t pte, bool_t flush_cache)
+{
+    write_pte(p, pte);
+    if ( flush_cache )
+        clean_xen_dcache(*p);
+}
+
 /* Allocate a new page table page and hook it in via the given entry */
-static int p2m_create_table(struct domain *d,
-                            lpae_t *entry)
+static int p2m_create_table(struct domain *d, lpae_t *entry, bool_t flush_cache)
 {
     struct p2m_domain *p2m = &d->arch.p2m;
     struct page_info *page;
@@ -272,11 +278,13 @@ static int p2m_create_table(struct domain *d,
 
     p = __map_domain_page(page);
     clear_page(p);
+    if ( flush_cache )
+        clean_xen_dcache_va_range(p, PAGE_SIZE);
     unmap_domain_page(p);
 
     pte = mfn_to_p2m_entry(page_to_mfn(page), MATTR_MEM, p2m_invalid);
 
-    write_pte(entry, pte);
+    p2m_write_pte(entry, pte, flush_cache);
 
     return 0;
 }
@@ -308,6 +316,13 @@ static int apply_p2m_changes(struct domain *d,
     unsigned int flush = 0;
     bool_t populate = (op == INSERT || op == ALLOCATE);
     lpae_t pte;
+    bool_t flush_pt;
+
+    /* Some IOMMU don't support coherent PT walk. When the p2m is
+     * shared with the CPU, Xen has to make sure that the PT changes have
+     * reached the memory
+     */
+    flush_pt = iommu_enabled && !iommu_has_feature(d, IOMMU_FEAT_COHERENT_WALK);
 
     spin_lock(&p2m->lock);
 
@@ -334,7 +349,8 @@ static int apply_p2m_changes(struct domain *d,
                 continue;
             }
 
-            rc = p2m_create_table(d, &first[first_table_offset(addr)]);
+            rc = p2m_create_table(d, &first[first_table_offset(addr)],
+                                  flush_pt);
             if ( rc < 0 )
             {
                 printk("p2m_populate_ram: L1 failed\n");
@@ -360,7 +376,8 @@ static int apply_p2m_changes(struct domain *d,
                 continue;
             }
 
-            rc = p2m_create_table(d, &second[second_table_offset(addr)]);
+            rc = p2m_create_table(d, &second[second_table_offset(addr)],
+                                  flush_pt);
             if ( rc < 0 ) {
                 printk("p2m_populate_ram: L2 failed\n");
                 goto out;
@@ -411,13 +428,15 @@ static int apply_p2m_changes(struct domain *d,
 
                     pte = mfn_to_p2m_entry(page_to_mfn(page), mattr, t);
 
-                    write_pte(&third[third_table_offset(addr)], pte);
+                    p2m_write_pte(&third[third_table_offset(addr)],
+                                  pte, flush_pt);
                 }
                 break;
             case INSERT:
                 {
                     pte = mfn_to_p2m_entry(maddr >> PAGE_SHIFT, mattr, t);
-                    write_pte(&third[third_table_offset(addr)], pte);
+                    p2m_write_pte(&third[third_table_offset(addr)],
+                                  pte, flush_pt);
                     maddr += PAGE_SIZE;
                 }
                 break;
@@ -433,7 +452,8 @@ static int apply_p2m_changes(struct domain *d,
                     count += 0x10;
 
                     memset(&pte, 0x00, sizeof(pte));
-                    write_pte(&third[third_table_offset(addr)], pte);
+                    p2m_write_pte(&third[third_table_offset(addr)],
+                                  pte, flush_pt);
                     count++;
                 }
                 break;
@@ -548,10 +568,12 @@ int p2m_alloc_table(struct domain *d)
     /* Clear both first level pages */
     p = __map_domain_page(page);
     clear_page(p);
+    clean_xen_dcache_va_range(p, PAGE_SIZE);
     unmap_domain_page(p);
 
     p = __map_domain_page(page + 1);
     clear_page(p);
+    clean_xen_dcache_va_range(p, PAGE_SIZE);
     unmap_domain_page(p);
 
     p2m->first_level = page;
xen/drivers/passthrough/iommu.c +10 -0
index 59f1c3e..b21a167 100644
@@ -344,6 +344,16 @@ void iommu_crash_shutdown(void)
     iommu_enabled = iommu_intremap = 0;
 }
 
+bool_t iommu_has_feature(struct domain *d, enum iommu_feature feature)
+{
+    const struct hvm_iommu *hd = domain_hvm_iommu(d);
+
+    if ( !iommu_enabled )
+        return 0;
+
+    return test_bit(feature, &hd->features);
+}
+
 static void iommu_dump_p2m_table(unsigned char key)
 {
     struct domain *d;
xen/include/xen/hvm/iommu.h +6 -0
index 1259e16..61d8cd6 100644
@@ -34,6 +34,12 @@ struct hvm_iommu {
     /* List of DT devices assigned to this domain */
     struct list_head dt_devices;
 #endif
+
+    /* Features supported by the IOMMU */
+    unsigned long features;
 };
 
+#define iommu_set_feature(d, f)   set_bit((f), &domain_hvm_iommu(d)->features)
+#define iommu_clear_feature(d, f) clear_bit((f), &domain_hvm_iommu(d)->features)
+
 #endif /* __XEN_HVM_IOMMU_H__ */
xen/include/xen/iommu.h +8 -0
index b7481dac..a431ac6 100644
@@ -67,6 +67,14 @@ int iommu_map_page(struct domain *d, unsigned long gfn, unsigned long mfn,
                    unsigned int flags);
 int iommu_unmap_page(struct domain *d, unsigned long gfn);
 
+enum iommu_feature
+{
+    IOMMU_FEAT_COHERENT_WALK,
+};
+
+bool_t iommu_has_feature(struct domain *d, enum iommu_feature feature);
+
+
 #ifdef HAS_PCI
 void pt_pci_init(void);
 
Signature · 1 line
1.7.10.4
#2 lore raw

Re: [PATCH v6 1/4] xen/arm: p2m: Clean cache PT when the IOMMU doesn't support coherent walk

xen/include/xen/hvm/iommu.h struct hvm_iommu { · line 39

Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 15.05.14 at 14:56, <[email protected]> wrote:
xen/include/xen/hvm/iommu.h +3 -0
@@ -34,6 +34,12 @@ struct hvm_iommu {
     /* List of DT devices assigned to this domain */
     struct list_head dt_devices;
 #endif
+
+    /* Features supported by the IOMMU */
+    unsigned long features;

This wants to be DECLARE_BITMAP(features, IOMMU_FEAT_count) or some such, with ...

xen/include/xen/iommu.h int iommu_map_page(struct domain *d, unsigned long gfn, unsigned long mfn, · line 72

Quoted 9 lines
xen/include/xen/iommu.h +3 -0
@@ -67,6 +67,14 @@ int iommu_map_page(struct domain *d, unsigned long gfn, unsigned long mfn,
                    unsigned int flags);
 int iommu_unmap_page(struct domain *d, unsigned long gfn);
 
+enum iommu_feature
+{
+    IOMMU_FEAT_COHERENT_WALK,

... IOMMU_FEAT_count added here.

xen/include/xen/iommu.h int iommu_map_page(struct domain *d, unsigned long gfn, unsigned long mfn, · line 73

Quoted 1 line
xen/include/xen/iommu.h +1 -0
+};

Jan

#3 lore raw

PATCH v6 2/4 xen/arm: grant: Add another entry to map MFN 1:1 in dom0 p2m

In reply to Julien Grall

Grant mapping can be used for DMA request. The dev_bus_addr returned by the hypercall is the MFN (not the IPA). Currently Linux is using this address (via swiotlb) to program the DMA.
When the device is protected by IOMMU the request will fail. We have to add 1:1 mapping in the domain p2m to allow DMA request working.

This is valid because DOM0 has its memory mapped 1:1 and therefore we know that RAM and devices cannot clash.

---
Changes in v5:
- Update commit message

Changes in v4:
- Patch added

---
 xen/arch/arm/mm.c |   33 ++++++++++++++++++++++++++++-----
 1 file changed, 28 insertions(+), 5 deletions(-)
xen/arch/arm/mm.c +28 -5
index eac228c..4ce3962 100644
@@ -1199,6 +1199,7 @@ int create_grant_host_mapping(unsigned long addr, unsigned long frame,
 {
     int rc;
     p2m_type_t t = p2m_grant_map_rw;
+    struct domain *d = current->domain;
 
     if ( cache_flags  || (flags & ~GNTMAP_readonly) != GNTMAP_host_map )
         return GNTST_general_error;
@@ -1206,13 +1207,32 @@ int create_grant_host_mapping(unsigned long addr, unsigned long frame,
     if ( flags & GNTMAP_readonly )
         t = p2m_grant_map_ro;
 
-    rc = guest_physmap_add_entry(current->domain, addr >> PAGE_SHIFT,
-                                 frame, 0, t);
+    rc = guest_physmap_add_entry(d, addr >> PAGE_SHIFT, frame, 0, t);
 
     if ( rc )
-        return GNTST_general_error;
-    else
-        return GNTST_okay;
+        goto gerror;
+
+    /* Grant mapping can be used for DMA request. The dev_bus_addr returned by
+     * the hypercall is the MFN (not the IPA). For device protected by
+     * an IOMMU, Xen needs to add a 1:1 mapping in the domain p2m to
+     * allow DMA request working.
+     * This is only valid when the domain is directed mapped
+     */
+    if ( is_domain_direct_mapped(d) && need_iommu(d) )
+    {
+        rc = guest_physmap_add_entry(d, frame, frame, 0, t);
+        if ( rc )
+            goto unmap;
+    }
+
+    return GNTST_okay;
+
+unmap:
+    guest_physmap_remove_page(d, addr >> PAGE_SHIFT, frame, 0);
+
+gerror:
+    return GNTST_general_error;
+
 }
 
 int replace_grant_host_mapping(unsigned long addr, unsigned long mfn,
@@ -1226,6 +1246,9 @@ int replace_grant_host_mapping(unsigned long addr, unsigned long mfn,
 
     guest_physmap_remove_page(d, gfn, mfn, 0);
 
+    if ( is_domain_direct_mapped(d) && need_iommu(d) )
+        guest_physmap_remove_page(d, mfn, mfn, 0);
+
     return GNTST_okay;
 }
 
Signature · 1 line
1.7.10.4
#4 lore raw

PATCH v6 3/4 xen: iommu: Define PAGE_{SHIFT, SIZE, ALIGN, MASK)_64K

In reply to Julien Grall

Also add IOMMU_PAGE_* helper macros to help creating PAGE_* defines.

Cc Xiantao Zhang <[email protected]>
Cc Jan Beulich <[email protected]>
---
 xen/include/xen/iommu.h |   15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)
xen/include/xen/iommu.h +12 -3
index a431ac6..b93a436 100644
@@ -35,10 +35,19 @@ extern bool_t iommu_hap_pt_share;
 extern bool_t iommu_debug;
 extern bool_t amd_iommu_perdev_intremap;
 
+#define IOMMU_PAGE_SIZE(sz) (1UL << PAGE_SHIFT_##sz)
+#define IOMMU_PAGE_MASK(sz) (~(u64)0 << PAGE_SHIFT_##sz)
+#define IOMMU_PAGE_ALIGN(sz, addr)  (((addr) + ~PAGE_MASK_##sz) & PAGE_MASK_##sz)
+
 #define PAGE_SHIFT_4K       (12)
-#define PAGE_SIZE_4K        (1UL << PAGE_SHIFT_4K)
-#define PAGE_MASK_4K        (((u64)-1) << PAGE_SHIFT_4K)
-#define PAGE_ALIGN_4K(addr) (((addr) + PAGE_SIZE_4K - 1) & PAGE_MASK_4K)
+#define PAGE_SIZE_4K        IOMMU_PAGE_SIZE(4K)
+#define PAGE_MASK_4K        IOMMU_PAGE_MASK(4K)
+#define PAGE_ALIGN_4K(addr) IOMMU_PAGE_ALIGN(4K, (addr))
+
+#define PAGE_SHIFT_64K          (16)
+#define PAGE_SIZE_64K           IOMMU_PAGE_SIZE(64K)
+#define PAGE_MASK_64K           IOMMU_PAGE_MASK(64K)
+#define PAGE_ALIGN_64K(addr)    IOMMU_PAGE_ALIGN(64K, (addr))
 
 int iommu_setup(void);
 
Signature · 1 line
1.7.10.4
Acked-by on v6 3/4
#5 lore raw

Re: [PATCH v6 3/4] xen: iommu: Define PAGE_{SHIFT, SIZE, ALIGN, MASK)_64K

Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 15.05.14 at 14:56, <[email protected]> wrote:

Also add IOMMU_PAGE_* helper macros to help creating PAGE_* defines.

with a small nit:

xen/include/xen/iommu.h extern bool_t iommu_hap_pt_share; · line 45

Quoted 7 lines

#define PAGE_SHIFT_4K (12)

xen/include/xen/iommu.h +3 -3
-#define PAGE_SIZE_4K        (1UL << PAGE_SHIFT_4K)
-#define PAGE_MASK_4K        (((u64)-1) << PAGE_SHIFT_4K)
-#define PAGE_ALIGN_4K(addr) (((addr) + PAGE_SIZE_4K - 1) & PAGE_MASK_4K)
+#define PAGE_SIZE_4K        IOMMU_PAGE_SIZE(4K)
+#define PAGE_MASK_4K        IOMMU_PAGE_MASK(4K)
+#define PAGE_ALIGN_4K(addr) IOMMU_PAGE_ALIGN(4K, (addr))

Pointless parentheses around addr.

xen/include/xen/iommu.h extern bool_t iommu_hap_pt_share; · line 50

Quoted 4 lines
xen/include/xen/iommu.h +4 -0
+#define PAGE_SHIFT_64K          (16)
+#define PAGE_SIZE_64K           IOMMU_PAGE_SIZE(64K)
+#define PAGE_MASK_64K           IOMMU_PAGE_MASK(64K)
+#define PAGE_ALIGN_64K(addr)    IOMMU_PAGE_ALIGN(64K, (addr))

Same here.

Jan

#6 lore raw

PATCH v6 4/4 drivers/passthrough: arm: Add support for SMMU drivers

In reply to Julien Grall

This patch add support for ARM architected SMMU driver. It's based on the linux drivers (drivers/iommu/arm-smmu) commit 89ac23cd.

The major differences with the Linux driver are:
- Fault by default if the SMMU is enabled to translate an address (Linux is bypassing the SMMU)
- Using P2M page table instead of creating new one
- Dropped stage-1 support
- Dropped chained SMMUs support for now
- Reworking device assignment and the different structures

Xen is programming each IOMMU by:
- Using stage-2 mode translation
- Sharing the page table with the processor
- Injecting a fault if the device has made a wrong translation

---
Changes in v6:
- Move the definition page PAGE_*_64K in another patch. This patch only contains ARM specific code now.
- Use the new way to set feature
Changes in v5:
- Fix arm_smmu_domain_remove_master by using num_s2crs instead of num_streamids
- IOMMU_FEATURE_COHERENT_WALK is became a bit position
Changes in v4:
- Use the new IRQ API e.g function without _dt_ in the name
- Use IRQF_SHARED to notify that the IRQ can be shared (i.e handle multiple actions)
- Update comment in smmu_init
- Update comment for upgrade barrier in smmu_device_reset
- Update CB_TCR to reflect change in VTCR by commit 8200dae
- Drop gr0_base which is not unused in arm_smmu_init_context_bank
- Rebase on the latest Xen (hwdom series was pushed)
- On 32 bit IPA is encoded on 40-bits. Read CB_FAR_HI on all arch
- Add verbosity in CB fault handler
- Handle 40 bits address space on ARM{32,64}.
- Implement new callback features
- platform_get_irq now returns -1 if an error occured
Changes in v3:
- Missing some static
Changes in v2:
- Update commit message
- Update some comments in the code
- Add new callbacks to assign/reassign DT device
- Rework init_dom0 and domain_teardown. The assignment/deassignement is now made in the generic code
- Set protected field in dt_device_node when the device is under an IOMMU
- Use SZ_64K and SZ_4K by the global PAGE_SIZE_{64,4}K in xen/iommu.h. The first one was not defined.

---
 xen/drivers/passthrough/arm/Makefile |    1 +
 xen/drivers/passthrough/arm/smmu.c   | 1742 ++++++++++++++++++++++++++++++++++
 2 files changed, 1743 insertions(+)
 create mode 100644 xen/drivers/passthrough/arm/smmu.c
xen/drivers/passthrough/arm/Makefile +1 -0
index 0484b79..f4cd26e 100644
@@ -1 +1,2 @@
 obj-y += iommu.o
+obj-y += smmu.o
xen/drivers/passthrough/arm/smmu.c new file +1742 -0

1,742 lines not on this page · raw message

Signature · 1 line
1.7.10.4