xen/arm,arm64: enable SWIOTLB_XEN
authorStefano Stabellini <stefano.stabellini@eu.citrix.com>
Thu, 10 Oct 2013 13:40:44 +0000 (13:40 +0000)
committerStefano Stabellini <stefano.stabellini@eu.citrix.com>
Thu, 10 Oct 2013 13:40:44 +0000 (13:40 +0000)
Xen on arm and arm64 needs SWIOTLB_XEN: when running on Xen we need to
program the hardware with mfns rather than pfns for dma addresses.
Remove SWIOTLB_XEN dependency on X86 and PCI and make XEN select
SWIOTLB_XEN on arm and arm64.

At the moment always rely on swiotlb-xen, but when Xen starts supporting
hardware IOMMUs we'll be able to avoid it conditionally on the presence
of an IOMMU on the platform.

Implement xen_create_contiguous_region on arm and arm64: for the moment
we assume that dom0 has been mapped 1:1 (physical addresses == machine
addresses) therefore we don't need to call XENMEM_exchange. Simply
return the physical address as dma address.

Initialize the xen-swiotlb from xen_early_init (before the native
dma_ops are initialized), set xen_dma_ops to &xen_swiotlb_dma_ops.

Signed-off-by: Stefano Stabellini <stefano.stabellini@eu.citrix.com>
Changes in v8:
- assume dom0 is mapped 1:1, no need to call XENMEM_exchange.

Changes in v7:
- call __set_phys_to_machine_multi from xen_create_contiguous_region and
xen_destroy_contiguous_region to update the P2M;
- don't call XENMEM_unpin, it has been removed;
- call XENMEM_exchange instead of XENMEM_exchange_and_pin;
- set nr_exchanged to 0 before calling the hypercall.

Changes in v6:
- introduce and export xen_dma_ops;
- call xen_mm_init from as arch_initcall.

Changes in v4:
- remove redefinition of DMA_ERROR_CODE;
- update the code to use XENMEM_exchange_and_pin and XENMEM_unpin;
- add a note about hardware IOMMU in the commit message.

Changes in v3:
- code style changes;
- warn on XENMEM_put_dma_buf failures.

arch/arm/Kconfig
arch/arm/include/asm/xen/hypervisor.h
arch/arm/include/asm/xen/page.h
arch/arm/xen/Makefile
arch/arm/xen/mm.c [new file with mode: 0644]
arch/arm64/Kconfig
arch/arm64/xen/Makefile
drivers/xen/Kconfig
drivers/xen/swiotlb-xen.c

index b08374f8fe3b15d2126e2e028264dcd6a7c36250..01f7013c85c7048ae81c7d5e13613ee6ddba590a 100644 (file)
@@ -1888,6 +1888,7 @@ config XEN
        depends on CPU_V7 && !CPU_V6
        depends on !GENERIC_ATOMIC64
        select ARM_PSCI
+       select SWIOTLB_XEN
        help
          Say Y if you want to run Linux in a Virtual Machine on Xen on ARM.
 
index d7ab99a0c9ebe2499b2c4defca40c0cb4a893358..1317ee40f4dfd6051daa9efaa77e52dfcee37fe5 100644 (file)
@@ -16,4 +16,6 @@ static inline enum paravirt_lazy_mode paravirt_get_lazy_mode(void)
        return PARAVIRT_LAZY_NONE;
 }
 
+extern struct dma_map_ops *xen_dma_ops;
+
 #endif /* _ASM_ARM_XEN_HYPERVISOR_H */
index d1b5dd56647227305de7f1f9c6c3875b812a6693..5d0e4c5dc711494274154fefeb0341795141dcac 100644 (file)
@@ -6,6 +6,7 @@
 
 #include <linux/pfn.h>
 #include <linux/types.h>
+#include <linux/dma-mapping.h>
 
 #include <xen/xen.h>
 #include <xen/interface/grant_table.h>
index 21e6ff503178056fedc49eea7db3800d10fe7326..12969523414cf2d0b972bdfb8b3ff50b9c781c8f 100644 (file)
@@ -1 +1 @@
-obj-y          := enlighten.o hypercall.o grant-table.o p2m.o
+obj-y          := enlighten.o hypercall.o grant-table.o p2m.o mm.o
diff --git a/arch/arm/xen/mm.c b/arch/arm/xen/mm.c
new file mode 100644 (file)
index 0000000..d56b8c6
--- /dev/null
@@ -0,0 +1,64 @@
+#include <linux/bootmem.h>
+#include <linux/gfp.h>
+#include <linux/export.h>
+#include <linux/slab.h>
+#include <linux/types.h>
+#include <linux/dma-mapping.h>
+#include <linux/vmalloc.h>
+#include <linux/swiotlb.h>
+
+#include <xen/xen.h>
+#include <xen/interface/memory.h>
+#include <xen/swiotlb-xen.h>
+
+#include <asm/cacheflush.h>
+#include <asm/xen/page.h>
+#include <asm/xen/hypercall.h>
+#include <asm/xen/interface.h>
+
+int xen_create_contiguous_region(unsigned long vstart, unsigned int order,
+                                unsigned int address_bits,
+                                dma_addr_t *dma_handle)
+{
+       if (!xen_initial_domain())
+               return -EINVAL;
+
+       /* we assume that dom0 is mapped 1:1 for now */
+       *dma_handle = virt_to_phys(pstart);
+       return 0;
+}
+EXPORT_SYMBOL_GPL(xen_create_contiguous_region);
+
+void xen_destroy_contiguous_region(unsigned long vstart, unsigned int order)
+{
+       return;
+}
+EXPORT_SYMBOL_GPL(xen_destroy_contiguous_region);
+
+struct dma_map_ops *xen_dma_ops;
+EXPORT_SYMBOL_GPL(xen_dma_ops);
+
+static struct dma_map_ops xen_swiotlb_dma_ops = {
+       .mapping_error = xen_swiotlb_dma_mapping_error,
+       .alloc = xen_swiotlb_alloc_coherent,
+       .free = xen_swiotlb_free_coherent,
+       .sync_single_for_cpu = xen_swiotlb_sync_single_for_cpu,
+       .sync_single_for_device = xen_swiotlb_sync_single_for_device,
+       .sync_sg_for_cpu = xen_swiotlb_sync_sg_for_cpu,
+       .sync_sg_for_device = xen_swiotlb_sync_sg_for_device,
+       .map_sg = xen_swiotlb_map_sg_attrs,
+       .unmap_sg = xen_swiotlb_unmap_sg_attrs,
+       .map_page = xen_swiotlb_map_page,
+       .unmap_page = xen_swiotlb_unmap_page,
+       .dma_supported = xen_swiotlb_dma_supported,
+};
+
+int __init xen_mm_init(void)
+{
+       if (!xen_initial_domain())
+               return 0;
+       xen_swiotlb_init(1, false);
+       xen_dma_ops = &xen_swiotlb_dma_ops;
+       return 0;
+}
+arch_initcall(xen_mm_init);
index c04454876bcbe6520a52ada910a7d193ff884e85..04ffafb6fbe9b33d154526b6db50b8ba560a4368 100644 (file)
@@ -211,6 +211,7 @@ config XEN_DOM0
 config XEN
        bool "Xen guest support on ARM64 (EXPERIMENTAL)"
        depends on ARM64 && OF
+       select SWIOTLB_XEN
        help
          Say Y if you want to run Linux in a Virtual Machine on Xen on ARM64.
 
index cd866b0c619b0d0b3fdd13bc4996eef31a80ec46..74a8d87e542b726b2022a21d2851ce63f124d6d7 100644 (file)
@@ -1,2 +1,2 @@
-xen-arm-y      += $(addprefix ../../arm/xen/, enlighten.o grant-table.o p2m.o)
+xen-arm-y      += $(addprefix ../../arm/xen/, enlighten.o grant-table.o p2m.o mm.o)
 obj-y          := xen-arm.o hypercall.o
index 23eae5cb69c21e16d2f71ce2c29e1b307ba3c05c..c794ea1821402dc21e47af3d8b398c9d4f3209e6 100644 (file)
@@ -140,7 +140,6 @@ config XEN_GRANT_DEV_ALLOC
 
 config SWIOTLB_XEN
        def_bool y
-       depends on PCI && X86
        select SWIOTLB
 
 config XEN_TMEM
index b72f31c1e018900883c4a258e5ff3ceaaf740117..f0fc1a4f565af0ea9c01eb69d0dc74444d9fc7b1 100644 (file)
 #include <xen/page.h>
 #include <xen/xen-ops.h>
 #include <xen/hvc-console.h>
+#include <asm/dma-mapping.h>
 /*
  * Used to do a quick range check in swiotlb_tbl_unmap_single and
  * swiotlb_tbl_sync_single_*, to see if the memory was in fact allocated by this
  * API.
  */
 
+#ifndef CONFIG_X86
+static unsigned long dma_alloc_coherent_mask(struct device *dev,
+                                           gfp_t gfp)
+{
+       unsigned long dma_mask = 0;
+
+       dma_mask = dev->coherent_dma_mask;
+       if (!dma_mask)
+               dma_mask = (gfp & GFP_DMA) ? DMA_BIT_MASK(24) : DMA_BIT_MASK(32);
+
+       return dma_mask;
+}
+#endif
+
 static char *xen_io_tlb_start, *xen_io_tlb_end;
 static unsigned long xen_io_tlb_nslabs;
 /*