Skip to contentSkip to conversation

x86-64: dma_ops for DMA mapping -K4

PATCH In review Muli Ben-Yehuda 3 participants 4 messages Nov 17, 2005 – Nov 18, 2005

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86-64: dma_ops for DMA mapping -K4

Hi Andi,

Here's the latest version of the dma_ops patch. The patch is against Linus's tree as of yesterday and applies cleanly to 2.6.15-rc1-git5. Tested on AMD64 and Intel EM64 (x366) with gart, swiotlb, nommu and iommu=off. Please apply...

Summary of changes from -K3 to -K4:

Fix a couple of buglets discovered by Jon Mason:

- don't always initialize nommu, only use nommu if GART is not compiled in and we're not using swiotlb, or if initializing the IOMMU fails and we fall back to nommu.
- don't use swiotlb if user gave us iommu=off even if we have >4GB of memory. Off means off.

Summary of changes:

This patch cleans up x86_64's DMA mapping dispatching code. Right now we have three possible IOMMU types: AGP GART, swiotlb and nommu, and in the future we will also have Xen's x86_64 swiotlb and other HW IOMMUs for x86_64. In order to support all of them cleanly, this patch:

- introduces a struct dma_mapping_ops with function pointers for each of the DMA mapping operations of gart (AMD HW IOMMU), swiotlb (software IOMMU) and nommu (no IOMMU).

- gets rid of:

  if (swiotlb)
      return swiotlb_xxx();

in various places in favor of:

  if (unlikely(dma_ops) && dma_ops->xxx)
      return dma_ops->xxx();

in dma-mapping.h.

- in order to keep the fast path fast, if no dma_ops is specified the default nommu_ops are used.

- PCI_DMA_BUS_IS_PHYS is now checked against the dma_ops being set (gart enabled and swiotlb, BUS_IS_PHYS=0) or not set (nommu and gart disabled via iommu=off, BUS_IS_PHYS=1).

---

 arch/x86_64/Kconfig                |    9 -
 arch/x86_64/kernel/Makefile        |    4 
 arch/x86_64/kernel/pci-dma.c       |   60 --------
 arch/x86_64/kernel/pci-gart.c      |  140 +++++++++++---------
 arch/x86_64/kernel/pci-nommu.c     |   91 ++++++++-----
 arch/x86_64/kernel/setup.c         |   29 ++++
 arch/x86_64/mm/init.c              |   24 +++
 include/asm-x86_64/dma-mapping.h   |  257 ++++++++++++++++++++++++++-----------
 include/asm-x86_64/nommu-mapping.h |   63 +++++++++
 include/asm-x86_64/pci.h           |   11 -
 include/asm-x86_64/swiotlb.h       |    7 -
 11 files changed, 449 insertions(+), 246 deletions(-)
arch/x86_64/Kconfig +0 -9
diff -Naurp --exclude-from /home/muli/w/dontdiff vanilla/arch/x86_64/Kconfig hg/arch/x86_64/Kconfig
@@ -365,15 +365,6 @@ config SWIOTLB
        depends on GART_IOMMU
        default y
 
-config DUMMY_IOMMU
-	bool
-	depends on !GART_IOMMU && !SWIOTLB
-	default y
-	help
-	  Don't use IOMMU code. This will cause problems when you have more than 4GB
-	  of memory and any 32-bit devices. Don't turn on unless you know what you
-	  are doing.
-
 config X86_MCE
 	bool "Machine check support" if EMBEDDED
 	default y
arch/x86_64/kernel/Makefile +2 -2
diff -Naurp --exclude-from /home/muli/w/dontdiff vanilla/arch/x86_64/kernel/Makefile hg/arch/x86_64/kernel/Makefile
@@ -7,7 +7,8 @@ EXTRA_AFLAGS	:= -traditional
 obj-y	:= process.o signal.o entry.o traps.o irq.o \
 		ptrace.o time.o ioport.o ldt.o setup.o i8259.o sys_x86_64.o \
 		x8664_ksyms.o i387.o syscall.o vsyscall.o \
-		setup64.o bootflag.o e820.o reboot.o quirks.o i8237.o
+		setup64.o bootflag.o e820.o reboot.o quirks.o i8237.o \
+		pci-nommu.o
 
 obj-$(CONFIG_X86_MCE)         += mce.o
 obj-$(CONFIG_X86_MCE_INTEL)	+= mce_intel.o
@@ -27,7 +28,6 @@ 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_DUMMY_IOMMU)	+= pci-nommu.o pci-dma.o
 obj-$(CONFIG_KPROBES)		+= kprobes.o
 obj-$(CONFIG_X86_PM_TIMER)	+= pmtimer.o
 
arch/x86_64/kernel/pci-dma.c +0 -60

60 lines not on this page · raw message

arch/x86_64/kernel/pci-gart.c +79 -61

295 lines not on this page · raw message

arch/x86_64/kernel/pci-nommu.c +59 -32

124 lines not on this page · raw message

arch/x86_64/kernel/setup.c +28 -1
diff -Naurp --exclude-from /home/muli/w/dontdiff vanilla/arch/x86_64/kernel/setup.c hg/arch/x86_64/kernel/setup.c
@@ -42,6 +42,7 @@
 #include <linux/edd.h>
 #include <linux/mmzone.h>
 #include <linux/kexec.h>
+#include <linux/dma-mapping.h>
 
 #include <asm/mtrr.h>
 #include <asm/uaccess.h>
@@ -60,6 +61,7 @@
 #include <asm/setup.h>
 #include <asm/mach_apic.h>
 #include <asm/numa.h>
+#include <asm/swiotlb.h>
 #include <asm/sections.h>
 
 /*
@@ -87,6 +89,32 @@ unsigned long saved_video_mode;
 
 #ifdef CONFIG_SWIOTLB
 int swiotlb;
+
+/* these two are temporarily used by swiotlb */
+extern void* 
+gart_alloc_coherent(struct device *dev, size_t size, 
+		    dma_addr_t *dma_handle, gfp_t gfp);
+
+extern int gart_dma_supported(struct device *hwdev, u64 mask);
+
+struct dma_mapping_ops swiotlb_dma_ops = {
+	.mapping_error = swiotlb_dma_mapping_error,
+	.alloc_coherent = gart_alloc_coherent, /* FIXME: we are called via gart_alloc_coherent */
+	.free_coherent = swiotlb_free_coherent,
+	.map_single = swiotlb_map_single,
+	.unmap_single = swiotlb_unmap_single,
+	.sync_single_for_cpu = swiotlb_sync_single_for_cpu,
+	.sync_single_for_device = swiotlb_sync_single_for_device,
+	.sync_single_range_for_cpu = swiotlb_sync_single_range_for_cpu,
+	.sync_single_range_for_device = swiotlb_sync_single_range_for_device,
+	.sync_sg_for_cpu = swiotlb_sync_sg_for_cpu,
+	.sync_sg_for_device = swiotlb_sync_sg_for_device,
+	.map_sg = swiotlb_map_sg,
+	.unmap_sg = swiotlb_unmap_sg,
+	/* FIXME: historically we used gart_dma_supported, keep it the same way */
+	.dma_supported = gart_dma_supported,
+};
+
 EXPORT_SYMBOL(swiotlb);
 #endif
 
@@ -366,7 +394,6 @@ static __init void parse_cmdline_early (
 			iommu_setup(from+6); 
 		}
 #endif
-
 		if (!memcmp(from,"oops=panic", 10))
 			panic_on_oops = 1;
 
arch/x86_64/mm/init.c +21 -3
diff -Naurp --exclude-from /home/muli/w/dontdiff vanilla/arch/x86_64/mm/init.c hg/arch/x86_64/mm/init.c
@@ -23,6 +23,7 @@
 #include <linux/bootmem.h>
 #include <linux/proc_fs.h>
 #include <linux/pci.h>
+#include <linux/dma-mapping.h>
 
 #include <asm/processor.h>
 #include <asm/system.h>
@@ -38,11 +39,23 @@
 #include <asm/proto.h>
 #include <asm/smp.h>
 #include <asm/sections.h>
+#include <asm/dma-mapping.h>
 
 #ifndef Dprintk
 #define Dprintk(x...)
 #endif
 
+#ifdef CONFIG_GART_IOMMU
+extern int swiotlb;
+#endif
+
+extern void no_iommu_init(void);
+
+struct dma_mapping_ops* dma_ops;
+EXPORT_SYMBOL(dma_ops);
+
+extern char _stext[];
+
 static unsigned long dma_reserve __initdata;
 
 DEFINE_PER_CPU(struct mmu_gather, mmu_gathers);
@@ -423,11 +436,16 @@ void __init mem_init(void)
 	long codesize, reservedpages, datasize, initsize;
 
 #ifdef CONFIG_SWIOTLB
-	if (!iommu_aperture &&
+	if (!iommu_aperture && !no_iommu &&
 	    (end_pfn >= 0xffffffff>>PAGE_SHIFT || force_iommu))
 	       swiotlb = 1;
-	if (swiotlb)
-		swiotlb_init();	
+	if (swiotlb) {
+		swiotlb_init();
+		printk(KERN_INFO "PCI-DMA: Using software bounce buffering for IO (SWIOTLB)\n");
+		dma_ops = &swiotlb_dma_ops;
+	}
+#else /* no SWIOTLB implied no HW IOMMU */
+	no_iommu_init();
 #endif
 
 	/* How many end-of-memory variables you have, grandma! */
include/asm-x86_64/dma-mapping.h +185 -72

337 lines not on this page · raw message

include/asm-x86_64/nommu-mapping.h +63 -0

63 lines not on this page · raw message

include/asm-x86_64/pci.h +6 -5
diff -Naurp --exclude-from /home/muli/w/dontdiff vanilla/include/asm-x86_64/pci.h hg/include/asm-x86_64/pci.h
@@ -42,18 +42,20 @@ int pcibios_set_irq_routing(struct pci_d
 #include <asm/scatterlist.h>
 #include <linux/string.h>
 #include <asm/page.h>
+#include <linux/dma-mapping.h> /* for have_iommu */
 
 extern int iommu_setup(char *opt);
 
-#ifdef CONFIG_GART_IOMMU
 /* The PCI address space does equal the physical memory
  * address space.  The networking and block device layers use
  * this boolean for bounce buffer decisions
  *
- * On AMD64 it mostly equals, but we set it to zero to tell some subsystems
- * that an IOMMU is available.
+ * On AMD64 it mostly equals, but we set it to zero if a hardware
+ * IOMMU (gart) of sotware IOMMU (swiotlb) is available.
  */
-#define PCI_DMA_BUS_IS_PHYS	(no_iommu ? 1 : 0)
+#define PCI_DMA_BUS_IS_PHYS (have_iommu ? 0 : 1)
+
+#ifdef CONFIG_GART_IOMMU
 
 /*
  * x86-64 always supports DAC, but sometimes it is useful to force
@@ -79,7 +81,6 @@ extern int iommu_sac_force;
 #else
 /* No IOMMU */
 
-#define PCI_DMA_BUS_IS_PHYS	1
 #define pci_dac_dma_supported(pci_dev, mask)    1
 
 #define DECLARE_PCI_UNMAP_ADDR(ADDR_NAME)
include/asm-x86_64/swiotlb.h +6 -1
diff -Naurp --exclude-from /home/muli/w/dontdiff vanilla/include/asm-x86_64/swiotlb.h hg/include/asm-x86_64/swiotlb.h
@@ -3,6 +3,8 @@
 
 #include <linux/config.h>
 
+#include <asm/dma-mapping.h>
+
 /* SWIOTLB interface */
 
 extern dma_addr_t swiotlb_map_single(struct device *hwdev, void *ptr, size_t size,
@@ -38,6 +40,9 @@ extern void *swiotlb_alloc_coherent (str
 				     dma_addr_t *dma_handle, gfp_t flags);
 extern void swiotlb_free_coherent (struct device *hwdev, size_t size,
 				   void *vaddr, dma_addr_t dma_handle);
+extern int swiotlb_dma_supported(struct device *hwdev, u64 mask);
+
+extern struct dma_mapping_ops swiotlb_dma_ops;
 
 #ifdef CONFIG_SWIOTLB
 extern int swiotlb;
@@ -45,4 +50,4 @@ extern int swiotlb;
 #define swiotlb 0
 #endif
 
-#endif
+#endif /* _ASM_SWTIOLB_H */
Signature · 2 lines
Muli Ben-Yehuda
http://www.mulix.org | http://mulix.livejournal.com/
#1 lore raw
Quoted 6 lines · Muli Ben-Yehuda

Hi Andi,

Here's the latest version of the dma_ops patch. The patch is against Linus's tree as of yesterday and applies cleanly to 2.6.15-rc1-git5. Tested on AMD64 and Intel EM64 (x366) with gart, swiotlb, nommu and iommu=off. Please apply...

Any chance you could move struct dma_mapping_ops to generic code and implement the dma_ operations ontop of them in linux/dma-mapping.h if the arch sets a WANT_DMA_MAPPING_OPS symbol? This kind of switch is duplicated in far too many architectures.

#2 lore raw
Quoted 4 lines · Christoph Hellwig

Any chance you could move struct dma_mapping_ops to generic code and implement the dma_ operations ontop of them in linux/dma-mapping.h if the arch sets a WANT_DMA_MAPPING_OPS symbol? This kind of switch is duplicated in far too many architectures.

If anything asm-generic, not ifdef.

-Andi

#3 lore raw
In reply to Christoph Hellwig
Quoted 8 lines · Christoph Hellwig
Quoted 6 lines · Muli Ben-Yehuda

Hi Andi,

Here's the latest version of the dma_ops patch. The patch is against Linus's tree as of yesterday and applies cleanly to 2.6.15-rc1-git5. Tested on AMD64 and Intel EM64 (x366) with gart, swiotlb, nommu and iommu=off. Please apply...

Quoted 4 lines

Any chance you could move struct dma_mapping_ops to generic code and implement the dma_ operations ontop of them in linux/dma-mapping.h if the arch sets a WANT_DMA_MAPPING_OPS symbol? This kind of switch is duplicated in far too many architectures.

I'm looking at this now. PPC, IA64 and parisc have something like this, as well as x86-64 with my patch applied. The others either have dma_ops with completely different semantics or choose the dma ops implementation at compile time. However, each arch needs a slightly different definition of dma_mapping_ops - I'm not sure having one definition catering to all archs is better than what we have now, considering the DMA mapping implementation is inherently arch specific.

Cheers,
Muli

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