Skip to contentSkip to conversation

Xen Calgary IOMMU support

RFC PATCH New Muli Ben-Yehuda 1 participant 3 messages Mar 26, 2006

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover Xen Calgary IOMMU support

The following two patches, against the xen-unstable and linux-2.6-xen repositories, introduce proof-of-concept IOMMU support in Xen. The IOMMU is the Calgary IOMMU found in high-end IBM xSeries servers. This an isolation-capable IOMMU that provides isolation per PCI bus. With such an IOMMU you can safely grant two domains *direct access* to two PCI devices (provided they're on separate busses, which is the default topology on these machines) and they can't DMA into each other's memory.

The patches are based on the bare metal Linux Calgary IOMMU support that is in -mm. They're proof of concept, not to be applied yet, may eat your file system, belch and not say thank you, etc, etc - but they work for us. With these patches dom0 runs with translation enabled.

There's plenty more to be done, but we we're aiming to release early and often. Comments highly appreciated!

Short overview:

- dom0 creates "IO spaces" via iommu_create_io_space, one IO space per BDF. Xen allocates memory for the translation tables, dom0 initializes the IOMMU, Xen programs it.
- when a driver wishes to DMA, it goes through the Linux DMA API as usual. The DMA API makes hypercalls to map and unmap pages into the IO space, and returns the IO addresses to the driver.

TODO list (not necessarily in order of priority):

- finish OLS paper :-)
- cleaner separation of responsibilities between dom0 and Xen - requires improved ioremap support in Xen
- add TCE invalidate entry/range/all hypercalls
- map Calgary register space in Xen so that we can do cache flushing in Xen
- utilize the IOMMU in a direct HW access partition other than dom0
- validate all map/unamp requests (make sure OS is only mapping/unmapping entries it owns)
- integration with grant tables and swiotlb
- integration with pci frontend / backend
- integration with management tools / domain configuration, etc
- support for Intel's and AMD's upcoming IOMMUs

Cheers,
Muli

Signature · 2 lines
Muli Ben-Yehuda
http://www.mulix.org | http://mulix.livejournal.com/
#1 lore raw

RFC PATCH 1/2 Xen Calgary IOMMU support - Xen bits

Quoted 2 lines · Muli Ben-Yehuda

The following two patches, against the xen-unstable and linux-2.6-xen repositories, introduce proof-of-concept IOMMU support in Xen.

xen/arch/x86/x86_64/entry.S +4 -0
diff -Naurp -X /home/muli/w/dontdiff vanilla.xen/xen/arch/x86/x86_64/entry.S xen/xen/arch/x86/x86_64/entry.S
@@ -557,6 +557,8 @@ ENTRY(hypercall_table)
         .quad do_acm_op
         .quad do_nmi_op
         .quad do_arch_sched_op_new
+        .quad do_iommu_map          /* 30 */
+        .quad do_iommu_unmap
         .rept NR_hypercalls-((.-hypercall_table)/8)
         .quad do_ni_hypercall
         .endr
@@ -592,6 +594,8 @@ ENTRY(hypercall_args_table)
         .byte 1 /* do_acm_op            */
         .byte 2 /* do_nmi_op            */
         .byte 2 /* do_arch_sched_op_new */
+        .byte 5 /* do_iommu_map         */  /* 30 */
+        .byte 3 /* do_iommu_unmap       */
         .rept NR_hypercalls-(.-hypercall_args_table)
         .byte 0 /* do_ni_hypercall      */
         .endr
xen/common/calgary.c +144 -0

144 lines not on this page · raw message

xen/common/dom0_ops.c +15 -0
diff -Naurp -X /home/muli/w/dontdiff vanilla.xen/xen/common/dom0_ops.c xen/xen/common/dom0_ops.c
@@ -18,6 +18,7 @@
 #include <xen/console.h>
 #include <xen/iocap.h>
 #include <xen/guest_access.h>
+#include <xen/iommu.h>
 #include <asm/current.h>
 #include <public/dom0_ops.h>
 #include <public/sched_ctl.h>
@@ -682,6 +683,20 @@ long do_dom0_op(GUEST_HANDLE(dom0_op_t) 
     break;
 #endif
 
+    case DOM0_IOMMU_CREATE_IO_SPACE:
+    {
+        ret = iommu_create_io_space(u_dom0_op, op);
+    }
+    break;
+
+    case DOM0_IOMMU_DESTROY_IO_SPACE:
+    {
+        u32 bdf  = op->u.iommu_destroy_io_space.bdf;
+        
+        ret = iommu_destroy_io_space(bdf);
+    }
+    break;
+
     default:
         ret = arch_do_dom0_op(op, u_dom0_op);
         break;
xen/common/iommu.c +111 -0

111 lines not on this page · raw message

xen/common/Makefile +2 -0
diff -Naurp -X /home/muli/w/dontdiff vanilla.xen/xen/common/Makefile xen/xen/common/Makefile
@@ -2,11 +2,13 @@ include $(BASEDIR)/Rules.mk
 
 obj-y += acm_ops.o
 obj-y += bitmap.o
+obj-y += calgary.o
 obj-y += dom0_ops.o
 obj-y += domain.o
 obj-y += elf.o
 obj-y += event_channel.o
 obj-y += grant_table.o
+obj-y += iommu.o
 obj-y += kernel.o
 obj-y += keyhandler.o
 obj-y += lib.o
xen/include/public/dom0_ops.h +16 -0
diff -Naurp -X /home/muli/w/dontdiff vanilla.xen/xen/include/public/dom0_ops.h xen/xen/include/public/dom0_ops.h
@@ -470,6 +470,20 @@ typedef struct dom0_hypercall_init {
     unsigned long mfn;        /* machine frame to be initialised */
 } dom0_hypercall_init_t;
 DEFINE_GUEST_HANDLE(dom0_hypercall_init_t);
+ 
+#define DOM0_IOMMU_CREATE_IO_SPACE 49
+typedef struct dom0_iommu_create_io_space {
+    u64 size;       /* size of the IO address space in bytes */
+    u32 bdf;        /* bus/dev/func this space translates for */
+    u64 rootmfn;    /* mfn of the root of the iommu table */
+} dom0_iommu_create_io_space_t;
+DEFINE_GUEST_HANDLE(dom0_iommu_create_io_space_t);
+
+#define DOM0_IOMMU_DESTROY_IO_SPACE 50
+typedef struct dom0_iommu_destroy_io_space {
+    u32 bdf;
+} dom0_iommu_destroy_io_space_t;
+DEFINE_GUEST_HANDLE(dom0_iommu_destroy_io_space_t);
 
 typedef struct dom0_op {
     uint32_t cmd;
@@ -512,6 +526,8 @@ typedef struct dom0_op {
         struct dom0_irq_permission    irq_permission;
         struct dom0_iomem_permission  iomem_permission;
         struct dom0_hypercall_init    hypercall_init;
+        struct dom0_iommu_create_io_space  iommu_create_io_space;
+        struct dom0_iommu_destroy_io_space iommu_destroy_io_space;
         uint8_t                       pad[128];
     } u;
 } dom0_op_t;
xen/include/public/xen.h +2 -0
diff -Naurp -X /home/muli/w/dontdiff vanilla.xen/xen/include/public/xen.h xen/xen/include/public/xen.h
@@ -60,6 +60,8 @@
 #define __HYPERVISOR_acm_op               27
 #define __HYPERVISOR_nmi_op               28
 #define __HYPERVISOR_sched_op_new         29
+#define __HYPERVISOR_iommu_map            30
+#define __HYPERVISOR_iommu_unmap          31
 
 /* 
  * VIRTUAL INTERRUPTS
xen/include/xen/calgary.h +37 -0
diff -Naurp -X /home/muli/w/dontdiff vanilla.xen/xen/include/xen/calgary.h xen/xen/include/xen/calgary.h
@@ -0,0 +1,37 @@
+/*
+ * Copyright (C) 2006 Muli Ben-Yehuda <[email protected]>, IBM Corporation
+ */
+
+#define TCE_MAX_TABLE_SIZE 7
+
+struct iommu_table {
+    u32 size; /* in number of entries */
+    void* ptr;
+};
+
+#define TCE_ENTRY_SIZE   8   /* in bytes */
+
+#define TCE_READ_SHIFT   0
+#define TCE_WRITE_SHIFT  1
+#define TCE_HUBID_SHIFT  2   /* unused */
+#define TCE_RSVD_SHIFT   8   /* unused */
+#define TCE_RPN_SHIFT    12
+#define TCE_UNUSED_SHIFT 48  /* unused */
+
+#define TCE_RPN_MASK     0x0000fffffffff000ULL
+
+int calgary_create_io_space(u64 size, u32 bdf, void** space, u64* rootmfn);
+void calgary_destroy_io_space(void* space);
+u64 calgary_map(void* space, u64 ioaddr, u64 mfn, u32 access, u32 bdf,
+        u32 size);
+int calgary_unmap(void* space, u64 ioaddr, u32 bdf, u32 size);
+
+/*
+ * Local variables:
+ * mode: C
+ * c-set-style: "BSD"
+ * c-basic-offset: 4
+ * tab-width: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
xen/include/xen/iommu.h +23 -0
diff -Naurp -X /home/muli/w/dontdiff vanilla.xen/xen/include/xen/iommu.h xen/xen/include/xen/iommu.h
@@ -0,0 +1,23 @@
+/*
+ * Copyright (C) 2006 Muli Ben-Yehuda <[email protected]>, IBM Corporation
+ */
+
+#include <asm/types.h>
+#include <asm/page.h>
+#include <public/dom0_ops.h>
+
+int iommu_create_io_space(GUEST_HANDLE(dom0_op_t) u_dom0_op,
+    struct dom0_op *op);
+int iommu_destroy_io_space(u32 bdf);
+u64 do_iommu_map(u64 ioaddr, u64 mfn, u32 access, u32 bdf, u32 size);
+int do_iommu_unmap(u64 ioaddr, u32 bdf, u32 size);
+
+/*
+ * Local variables:
+ * mode: C
+ * c-set-style: "BSD"
+ * c-basic-offset: 4
+ * tab-width: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
Signature · 2 lines
Muli Ben-Yehuda
http://www.mulix.org | http://mulix.livejournal.com/
#2 lore raw

RFC PATCH 2/2 Xen Calgary IOMMU support - xenlinux bits

In reply to Muli Ben-Yehuda
Quoted 2 lines · Muli Ben-Yehuda

The following two patches, against the xen-unstable and linux-2.6-xen repositories, introduce proof-of-concept IOMMU support in Xen.

arch/x86_64/Kconfig +18 -1
diff -Naurp -X /home/muli/w/dontdiff vanilla.xenlinux/arch/x86_64/Kconfig xenlinux/arch/x86_64/Kconfig
@@ -121,7 +121,6 @@ endchoice
 
 config X86_64_XEN
 	bool "Enable Xen compatible kernel"
-	select SWIOTLB
 	help
 	  This option will compile a kernel compatible with Xen hypervisor
 
@@ -399,6 +398,24 @@ config GART_IOMMU
 	  and a software emulation used on other systems.
 	  If unsure, say Y.
 
+config CALGARY_IOMMU
+	bool "IBM x366 server IOMMU"
+	default y
+	depends on PCI && EXPERIMENTAL
+	help
+	  Support for hardware IOMMUs in IBM's xSeries x366 and x460
+	  systems. Needed to run systems with more than 3GB of memory
+	  properly with 32-bit PCI devices that do not support DAC
+	  (Double Address Cycle). Calgary also supports bus level 
+	  isolation, where all DMAs pass through the IOMMU.  This 
+	  prevents them from going anywhere except their intended 
+	  destination. This catches hard-to-find kernel bugs and
+	  mis-behaving drivers and devices that do not use the DMA-API
+	  properly to set up their DMA buffers.  The IOMMU can be 
+	  turned off at boot time with the iommu=off parameter.  
+	  Normally the kernel will make the right choice by itself.
+	  If unsure, say Y.
+
 # need this always enabled with GART_IOMMU for the VIA workaround
 config SWIOTLB
 	bool
arch/x86_64/kernel/Makefile +2 -1
diff -Naurp -X /home/muli/w/dontdiff vanilla.xenlinux/arch/x86_64/kernel/Makefile xenlinux/arch/x86_64/kernel/Makefile
@@ -31,6 +31,7 @@ obj-$(CONFIG_SOFTWARE_SUSPEND)	+= suspen
 obj-$(CONFIG_CPU_FREQ)		+= cpufreq/
 obj-$(CONFIG_EARLY_PRINTK)	+= early_printk.o
 obj-$(CONFIG_GART_IOMMU)	+= pci-gart.o aperture.o
+obj-$(CONFIG_CALGARY_IOMMU)	+= pci-calgary.o tce.o
 obj-$(CONFIG_SWIOTLB)		+= pci-swiotlb.o
 obj-$(CONFIG_KPROBES)		+= kprobes.o
 obj-$(CONFIG_X86_PM_TIMER)	+= pmtimer.o
@@ -55,7 +56,7 @@ dmi_scan-y			+= ../../i386/kernel/dmi_sc
 
 ifdef CONFIG_XEN
 time-y				+= ../../i386/kernel/time-xen.o
-pci-dma-y			+= ../../i386/kernel/pci-dma-xen.o
+# pci-dma-y			+= ../../i386/kernel/pci-dma-xen.o
 microcode-$(subst m,y,$(CONFIG_MICROCODE))  := ../../i386/kernel/microcode-xen.o
 quirks-y			:= ../../i386/kernel/quirks-xen.o
 
arch/x86_64/kernel/pci-calgary.c +970 -0

970 lines not on this page · raw message

arch/x86_64/kernel/pci-dma.c +32 -9

70 lines not on this page · raw message

arch/x86_64/kernel/pci-gart.c +6 -5
diff -Naurp -X /home/muli/w/dontdiff vanilla.xenlinux/arch/x86_64/kernel/pci-gart.c xenlinux/arch/x86_64/kernel/pci-gart.c
@@ -605,7 +605,7 @@ static struct dma_mapping_ops gart_dma_o
 	.unmap_sg = gart_unmap_sg,
 };
 
-static int __init pci_iommu_init(void)
+int __init gart_iommu_init(void)
 { 
 	struct agp_kern_info info;
 	unsigned long aper_size;
@@ -627,6 +627,10 @@ static int __init pci_iommu_init(void)
 	if (swiotlb)
 		return -1; 
 
+        /* Did we detect a different HW IOMMU? */
+	if (iommu_detected && !iommu_aperture)
+		return -1;
+ 
 	if (no_iommu ||
 	    (!force_iommu && end_pfn <= MAX_DMA32_PFN) ||
 	    !iommu_aperture ||
@@ -721,10 +725,7 @@ static int __init pci_iommu_init(void)
 	return 0;
 } 
 
-/* Must execute after PCI subsystem */
-fs_initcall(pci_iommu_init);
-
-void gart_parse_options(char *p)
+void __init gart_parse_options(char *p)
 {
 	int arg;
 
arch/x86_64/kernel/setup.c +10 -0
diff -Naurp -X /home/muli/w/dontdiff vanilla.xenlinux/arch/x86_64/kernel/setup.c xenlinux/arch/x86_64/kernel/setup.c
@@ -67,6 +67,7 @@
 #include <asm/swiotlb.h>
 #include <asm/sections.h>
 #include <asm/gart-mapping.h>
+#include <asm/calgary.h>
 
 /*
  * Machine setup..
@@ -388,6 +389,12 @@ static __init void parse_cmdline_early (
 			iommu_setup(from+6); 
 		}
 
+#ifdef CONFIG_CALGARY_IOMMU 
+		if (!memcmp(from,"calgary=",8)) { 
+			calgary_parse_options(from+8);
+		}
+#endif
+
 		if (!memcmp(from,"oops=panic", 10))
 			panic_on_oops = 1;
 
@@ -747,6 +754,9 @@ void __init setup_arch(char **cmdline_p)
 #ifdef CONFIG_GART_IOMMU
 	iommu_hole_init();
 #endif
+#ifdef CONFIG_CALGARY_IOMMU
+	detect_calgary();
+#endif
 
 #ifdef CONFIG_VT
 #if defined(CONFIG_VGA_CONSOLE)
arch/x86_64/kernel/setup-xen.c +1 -0
diff -Naurp -X /home/muli/w/dontdiff vanilla.xenlinux/arch/x86_64/kernel/setup-xen.c xenlinux/arch/x86_64/kernel/setup-xen.c
@@ -67,6 +67,7 @@
 #include <asm/swiotlb.h>
 #include <asm/sections.h>
 #include <asm/gart-mapping.h>
+#include <asm/calgary.h>
 #ifdef CONFIG_XEN
 #include <linux/percpu.h>
 #include <xen/interface/physdev.h>
arch/x86_64/kernel/tce.c +194 -0

194 lines not on this page · raw message

arch/x86_64/mm/init-xen.c +3 -0
diff -Naurp -X /home/muli/w/dontdiff vanilla.xenlinux/arch/x86_64/mm/init-xen.c xenlinux/arch/x86_64/mm/init-xen.c
@@ -46,6 +46,7 @@
 #include <asm/sections.h>
 #include <asm/dma-mapping.h>
 #include <asm/swiotlb.h>
+#include <asm/calgary.h>
 
 #include <xen/features.h>
 
@@ -864,6 +865,8 @@ void __init mem_init(void)
 #endif
 	no_iommu_init();
 
+	detect_calgary();
+
 	/* How many end-of-memory variables you have, grandma! */
 	max_low_pfn = end_pfn;
 	max_pfn = end_pfn;
Documentation/x86_64/boot-options.txt +21 -0
diff -Naurp -X /home/muli/w/dontdiff vanilla.xenlinux/Documentation/x86_64/boot-options.txt xenlinux/Documentation/x86_64/boot-options.txt
@@ -200,6 +200,27 @@ IOMMU
   pages  Prereserve that many 128K pages for the software IO bounce buffering.
   force  Force all IO through the software TLB.
 
+  calgary=[64k,128k,256k,512k,1M,2M,4M,8M]
+  calgary=[translate_empty_slots,translate_phb0]
+
+    64k,...,8M - Set the size of each PCI slot's translation table
+    when using the Calgary IOMMU. This is the size of the translation
+    table itself in main memory. The smallest table, 64k, covers an IO
+    space of 32MB; the largest, 8MB table, can cover an IO space of
+    4GB. Normally the kernel will make the right choice by itself.
+
+    translate_empty_slots - Enable translation even on slots that have
+    no devices attached to them, in case a device will be hotplugged
+    in the future.
+
+    translate_phb0 - Enable translation on PHB0. The built-in graphics
+    adapter resides on the first bridge (PHB0); if translation
+    (isolation) is enabled on this PHB, X servers that access the
+    hardware directly from user space might stop working. Enable this
+    option if your X server only accesses hardware through the kernel
+    (or you're not using X) and you wants isolation for this bridge as
+    well.
+
 Debugging
 
   oops=panic Always panic on oopses. Default is to just kill the process,
include/asm-x86_64/calgary.h +64 -0

64 lines not on this page · raw message

include/asm-x86_64/dma-mapping.h +1 -0
diff -Naurp -X /home/muli/w/dontdiff vanilla.xenlinux/include/asm-x86_64/dma-mapping.h xenlinux/include/asm-x86_64/dma-mapping.h
@@ -183,5 +183,6 @@ dma_cache_sync(void *vaddr, size_t size,
 
 extern struct device fallback_dev;
 extern int panic_on_overflow;
+extern int iommu_detected;
 
 #endif /* _X8664_DMA_MAPPING_H */
include/asm-x86_64/pci.h +1 -1
diff -Naurp -X /home/muli/w/dontdiff vanilla.xenlinux/include/asm-x86_64/pci.h xenlinux/include/asm-x86_64/pci.h
@@ -53,7 +53,7 @@ extern int iommu_setup(char *opt);
  */
 #define PCI_DMA_BUS_IS_PHYS (dma_ops->is_phys)
 
-#ifdef CONFIG_GART_IOMMU
+#if defined(CONFIG_GART_IOMMU) || defined(CONFIG_CALGARY_IOMMU)
 
 /*
  * x86-64 always supports DAC, but sometimes it is useful to force
include/asm-x86_64/proto.h +3 -1
diff -Naurp -X /home/muli/w/dontdiff vanilla.xenlinux/include/asm-x86_64/proto.h xenlinux/include/asm-x86_64/proto.h
@@ -104,7 +104,9 @@ extern int unsynchronized_tsc(void);
 extern void select_idle_routine(const struct cpuinfo_x86 *c);
 
 extern void gart_parse_options(char *);
-extern void __init no_iommu_init(void);
+extern int gart_iommu_init(void);
+extern void no_iommu_init(void);
+extern void detect_calgary(void);
 
 extern unsigned long table_start, table_end;
 
include/asm-x86_64/tce.h +47 -0

47 lines not on this page · raw message

include/xen/interface/dom0_ops.h +16 -0
diff -Naurp -X /home/muli/w/dontdiff vanilla.xenlinux/include/xen/interface/dom0_ops.h xenlinux/include/xen/interface/dom0_ops.h
@@ -471,6 +471,20 @@ typedef struct dom0_hypercall_init {
 } dom0_hypercall_init_t;
 DEFINE_GUEST_HANDLE(dom0_hypercall_init_t);
 
+#define DOM0_IOMMU_CREATE_IO_SPACE 49
+typedef struct dom0_iommu_create_io_space {
+    u64 size;       /* size of the IO address space in bytes */
+    u32 bdf;        /* bus/dev/func this space translates for */
+    u64 rootmfn;    /* mfn of the root of the iommu table */
+} dom0_iommu_create_io_space_t;
+DEFINE_GUEST_HANDLE(dom0_iommu_create_io_space_t);
+
+#define DOM0_IOMMU_DESTROY_IO_SPACE 50
+typedef struct dom0_iommu_destroy_io_space {
+    u64 rootmfn;    /* mfn of the root of the iommu table */
+} dom0_iommu_destroy_io_space_t;
+DEFINE_GUEST_HANDLE(dom0_iommu_destroy_io_space_t);
+
 typedef struct dom0_op {
     uint32_t cmd;
     uint32_t interface_version; /* DOM0_INTERFACE_VERSION */
@@ -512,6 +526,8 @@ typedef struct dom0_op {
         struct dom0_irq_permission    irq_permission;
         struct dom0_iomem_permission  iomem_permission;
         struct dom0_hypercall_init    hypercall_init;
+        struct dom0_iommu_create_io_space  iommu_create_io_space;
+        struct dom0_iommu_destroy_io_space iommu_destroy_io_space;
         uint8_t                       pad[128];
     } u;
 } dom0_op_t;
include/xen/interface/xen.h +2 -0
diff -Naurp -X /home/muli/w/dontdiff vanilla.xenlinux/include/xen/interface/xen.h xenlinux/include/xen/interface/xen.h
@@ -60,6 +60,8 @@
 #define __HYPERVISOR_acm_op               27
 #define __HYPERVISOR_nmi_op               28
 #define __HYPERVISOR_sched_op_new         29
+#define __HYPERVISOR_iommu_map            30
+#define __HYPERVISOR_iommu_unmap          31
 
 /* 
  * VIRTUAL INTERRUPTS
Signature · 2 lines
Muli Ben-Yehuda
http://www.mulix.org | http://mulix.livejournal.com/