Skip to contentSkip to conversation

x86 swiotlb questions

DISCUSSION Jan Beulich 4 participants 17 messages Dec 15, 2006 – Dec 20, 2006

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

I'm not certain when this code was changed significantly from what I remember, but

- What is the purpose of using alloc_bootmem_low variants here? I.e., where is the dependency on physical addresses being below 4G here (machine addresses are being restricted after the allocation anyway)? The panic message text after the failed allocation is confusing me additionally.

- While I can see the idea behind the overflow buffer, it doesn't seem to prevent data corruption, and if I understand it correctly it doesn't even prevent memory corruption (since its machine address doesn't get restricted anywhere, so the fall back return value would not necessarily meet the device requirements).

- With various parameters now being command line configurable, if any of these get set inconsistently or incorrectly the user would probably get a cryptic crash (from the BUG_ON() following the call to xen_create_contiguous_region()).

- The default bit width for DMA (also in Xen itself) was now changed to 30, just because of a single device (b44). Shouldn't it, with the settings being customizable now, rather be 32 (and those who own such ill devices need to make use of the option)?

- The DMA bit widths can be set to different values in Xen and kernel, which can lead to surprising results, I would think. Shouldn't the kernel rather obtain Xen's value, so they are consistent?

Thanks, Jan

#1 lore raw
Quoted 5 lines · Jan Beulich

I'm not certain when this code was changed significantly from what I remember, but

- What is the purpose of using alloc_bootmem_low variants here? I.e., where is the dependency on physical addresses being below 4G here (machine addresses

are

Quoted 3 lines

being restricted after the allocation anyway)? The panic message text after the
failed allocation is confusing me additionally.

This is how it's always been since we took ia64's swiotlb.c.

Quoted 7 lines

- While I can see the idea behind the overflow buffer, it doesn't seem to prevent
data corruption, and if I understand it correctly it doesn't even prevent memory
corruption (since its machine address doesn't get restricted anywhere, so the fall
back return value would not necessarily meet the device requirements).

Same here. We didn't implement this. It doesn't seem to make that much sense. Sync'ing with lib/swiotb.c and throwing away our special one would be very nice. :-)

Quoted 4 lines

- With various parameters now being command line configurable, if any of these get set inconsistently or incorrectly the user would probably get a cryptic crash
(from the BUG_ON() following the call to xen_create_contiguous_region()).

True. Best know what you're doing if you mess with these.

Quoted 5 lines

- The default bit width for DMA (also in Xen itself) was now changed to 30, just
because of a single device (b44). Shouldn't it, with the settings being customizable now, rather be 32 (and those who own such ill devices need to make use of the option)?

Arguable. Depends how common 31- and 30-bit limitations are. Limiting the swiotlb to 1GB of memory doesn't seem that harsh -- how much swiotlb memory are you likely to want, system wide?

Quoted 5 lines

- The DMA bit widths can be set to different values in Xen and kernel, which can
lead to surprising results, I would think. Shouldn't the kernel rather obtain Xen's
value, so they are consistent?

We would like to generalise Xen's heap allocator so that it keeps separate heaps for different bit widths. Then there would be no 'DMA width' or 'DMA pool' in Xen.

-- Keir

#2 lore raw
Quoted 9 lines
Quoted 5 lines

- The DMA bit widths can be set to different values in Xen and kernel, which can
lead to surprising results, I would think. Shouldn't the kernel rather obtain Xen's
value, so they are consistent?

We would like to generalise Xen's heap allocator so that it keeps separate heaps for different bit widths. Then there would be no 'DMA width' or 'DMA pool' in Xen.

I already have patches ready to do this (the DMA thing really is a nice side effect, I mostly wanted it for 32on64, so that I can restrict domain allocations for 32-bit domains). Are you saying I should throw away the DMA specialization then altogether (I already have no special DMA heap anymore)? The leftovers from it are so that one can reserve some portion of low memory to be returned only when the width restriction is low enough (i.e. to retain dma_emergency_pool functionality), which certainly isn't really appropriate anymore now (it should rather be a percentage or something like that, so that the lower you get the more of the memory remains reserved for specialized allocations).

Jan

#3 lore raw
Quoted 10 lines · Jan Beulich

I already have patches ready to do this (the DMA thing really is a nice side effect, I mostly wanted it for 32on64, so that I can restrict domain allocations for 32-bit domains). Are you saying I should throw away the DMA specialization then altogether (I already have no special DMA heap anymore)? The leftovers from it are so that one can reserve some portion of low memory to be returned only when the width restriction is low enough (i.e. to retain dma_emergency_pool functionality), which certainly isn't really appropriate anymore now (it should rather be a percentage or something like that, so that the lower you get the more of the memory remains reserved for specialized allocations).

I think dma_emergency_pool as is can go. Possibly it should be replaced by allocator-management tools in dom0 to allow setting of limits on a per-bitwidth basis.

Is this one of the patches you already sent in your 32-on-64 batch, or an additional one?

-- Keir

#4 lore raw
Quoted 14 lines
Quoted 1 line
Quoted 1 line

Keir Fraser <[email protected]> 15.12.06 15:03 >>>

Quoted 10 lines · Jan Beulich

I already have patches ready to do this (the DMA thing really is a nice side effect, I mostly wanted it for 32on64, so that I can restrict domain allocations for 32-bit domains). Are you saying I should throw away the DMA specialization then altogether (I already have no special DMA heap anymore)? The leftovers from it are so that one can reserve some portion of low memory to be returned only when the width restriction is low enough (i.e. to retain dma_emergency_pool functionality), which certainly isn't really appropriate anymore now (it should rather be a percentage or something like that, so that the lower you get the more of the memory remains reserved for specialized allocations).

Quoted 3 lines

I think dma_emergency_pool as is can go. Possibly it should be replaced by allocator-management tools in dom0 to allow setting of limits on a per-bitwidth basis.

Okay, but I think I'll leave this as a separate change (that we probably first should reach agreement on what it really ought to do and not do).

Quoted 2 lines

Is this one of the patches you already sent in your 32-on-64 batch, or an additional one?

An additional one (or actually, a set of them, to make the individual steps more clear). As a followup, I'm also planning to get rid of the Xen heaps on those arches where they aren't needed (x86-64, not sure about ppc and ia64, but I would assume it's really only x86-32 that needs it).

Jan

#5 lore raw
Quoted 7 lines · Jan Beulich
Quoted 2 lines

Is this one of the patches you already sent in your 32-on-64 batch, or an additional one?

An additional one (or actually, a set of them, to make the individual steps more clear). As a followup, I'm also planning to get rid of the Xen heaps on those arches where they aren't needed (x86-64, not sure about ppc and ia64, but I would assume it's really only x86-32 that needs it).

Yes, that would be great. There are a couple of minor caveats around special semantics of the Xen heap but those could do with cleaning up and being made explicit anyway.

-- Keir

#6 lore raw
Quoted 6 lines
Quoted 1 line
Quoted 1 line

Keir Fraser <[email protected]> 15.12.06 15:19 >>>

Quoted 3 lines · Jan Beulich
Quoted 2 lines

Is this one of the patches you already sent in your 32-on-64 batch, or an additional one?

Quoted 8 lines
Quoted 4 lines

An additional one (or actually, a set of them, to make the individual steps more clear). As a followup, I'm also planning to get rid of the Xen heaps on those arches where they aren't needed (x86-64, not sure about ppc and ia64, but I would assume it's really only x86-32 that needs it).

Yes, that would be great. There are a couple of minor caveats around special semantics of the Xen heap but those could do with cleaning up and being made explicit anyway.

Mind adding a little more detail here?

Jan

#7 lore raw
Quoted 5 lines · Jan Beulich
Quoted 3 lines

Yes, that would be great. There are a couple of minor caveats around special semantics of the Xen heap but those could do with cleaning up and being made explicit anyway.

Mind adding a little more detail here?

Some allocations need to be below 4GB (e.g., domain pointers, as they get pickled into a 32-bit field in page structures). These should use an explicit address-width parameter to the allocator.

Pages allocated from the 'Xen heap' but shared with a domain have special semantics when their refcount falls to zero. This includes shared_info and grant-table pages. We may need an extra PGC_ bit to flag these pages.

That's all I can think of.

-- Keir

#8 lore raw
In reply to Keir Fraser
Quoted 3 lines

Arguable. Depends how common 31- and 30-bit limitations are. Limiting the swiotlb to 1GB of memory doesn't seem that harsh -- how much swiotlb memory are you likely to want, system wide?

31bit pops up now and then, 30bit also. There are a few devices that are even worse but they are older sound cards so not important.

#9 lore raw
In reply to Keir Fraser
Quoted 8 lines
Quoted 2 lines

- What is the purpose of using alloc_bootmem_low variants here? I.e., where is the dependency on physical addresses being below 4G here (machine addresses

are

Quoted 3 lines

being restricted after the allocation anyway)? The panic message text after the
failed allocation is confusing me additionally.

This is how it's always been since we took ia64's swiotlb.c.

Okay, then I must have forgotten about how it looked like. However, the specific panic message has a Xen-specific addition, so I still wonder what its background is...

Quoted 11 lines
Quoted 7 lines

- While I can see the idea behind the overflow buffer, it doesn't seem to prevent
data corruption, and if I understand it correctly it doesn't even prevent memory
corruption (since its machine address doesn't get restricted anywhere, so the fall
back return value would not necessarily meet the device requirements).

Same here. We didn't implement this. It doesn't seem to make that much sense. Sync'ing with lib/swiotb.c and throwing away our special one would be very nice. :-)

Trying to do that I find one extra issue: in_swiotlb_aperture() does its check based on pfn, while lib/swiotlb.c uses the virtual address in the respective checks instead. Is there some subtlety behind that (that then should be commented upon), or is this just due to this originally having been an mfn-based check?

Thanks, Jan

#10 lore raw
Quoted 9 lines · Jan Beulich
Quoted 3 lines

Same here. We didn't implement this. It doesn't seem to make that much sense. Sync'ing with lib/swiotb.c and throwing away our special one would be very nice. :-)

Trying to do that I find one extra issue: in_swiotlb_aperture() does its check based on pfn, while lib/swiotlb.c uses the virtual address in the respective checks instead. Is there some subtlety behind that (that then should be commented upon), or is this just due to this originally having been an mfn-based check?

Yes, it's because we used to do an mfn range check which was okay when the swiotlb aperture was filled with contiguous machine memory. Since it is composed of discontiguous slabs now, we changed to a pfn check but that could equally well be a virtual-address check.

Do we merge okay with lib/swiotlb.c then? One concern I had was with our preferred setup semantics -- we really want the user to be able to forcibly enable the swiotlb via a boot parameter *but* not have to suffer using it for every DMA operation. Last I looked the generic swiotlb didn't have that option. That and our very Xen-specific checks for whether to auto-enable the swiotlb led me to think that the very start-of-day setup of swiotlb would need to be overridable by architecture.

-- Keir

#11 lore raw
Quoted 1 line

Do we merge okay with lib/swiotlb.c then?

Not yet - because of the highmem handling needed for i386. I wonder, however, how native Linux gets away with not handling this through swiotlb, and why nevertheless Xen needs to special case this. Any ideas?

Jan

#12 lore raw
Quoted 5 lines · Jan Beulich
Quoted 1 line

Do we merge okay with lib/swiotlb.c then?

Not yet - because of the highmem handling needed for i386. I wonder, however, how native Linux gets away with not handling this through swiotlb, and why nevertheless Xen needs to special case this. Any ideas?

Probably because GFP_KERNEL and GFP_DMA allocations are guaranteed to be DMAable by 30-bit-capable devices on native, but not on Xen.

-- Keir

#13 lore raw
Quoted 11 lines
Quoted 1 line
Quoted 1 line

Keir Fraser <[email protected]> 19.12.06 15:14 >>>

Quoted 5 lines · Jan Beulich
Quoted 1 line

Do we merge okay with lib/swiotlb.c then?

Not yet - because of the highmem handling needed for i386. I wonder, however, how native Linux gets away with not handling this through swiotlb, and why nevertheless Xen needs to special case this. Any ideas?

Probably because GFP_KERNEL and GFP_DMA allocations are guaranteed to be DMAable by 30-bit-capable devices on native, but not on Xen.

Not sure I understand your thinking here. Nothing prevents user pages (or anything else that I/O may happen against) to come from highmem, hence the bounce logic in mm/highmem.c needs to control this anyway (as I understand it). And since all we're talking about here are physical addresses (and their translations to virtual ones), I would rather conclude that we'll never see a page in the I/O path that page_address() would return NULL for, but if that's the case, then there's no need to kmap such pages or to favor page_to_bus() over virt_to_bus(page_address()).

Jan

#14 lore raw

One thing is we change the value of PCI_BUS_IS_PHYS (or some similar macro which I can't quite remember the name of) which I believe turns off some bounce-buffer logic contained within the block-device subsystem. So that will mean that we get highmem requests hitting the DMA interfaces, where on native they would have got filtered earlier by the highmem/lowmem bounce buffer logic that is specific to block-device requests.

Obviously we want to turn off that lowmem/highmem bounce buffer logic on Xen as it is nonsense when there is no direct correspondence to low/high machine addresses.

-- Keir

Quoted 20 lines · Jan Beulich
Quoted 7 lines
Quoted 4 lines

Not yet - because of the highmem handling needed for i386. I wonder, however,
how native Linux gets away with not handling this through swiotlb, and why nevertheless Xen needs to special case this. Any ideas?

Probably because GFP_KERNEL and GFP_DMA allocations are guaranteed to be DMAable by 30-bit-capable devices on native, but not on Xen.

Not sure I understand your thinking here. Nothing prevents user pages (or anything
else that I/O may happen against) to come from highmem, hence the bounce logic in mm/highmem.c needs to control this anyway (as I understand it). And since all
we're talking about here are physical addresses (and their translations to virtual
ones), I would rather conclude that we'll never see a page in the I/O path that
page_address() would return NULL for, but if that's the case, then there's no need
to kmap such pages or to favor page_to_bus() over virt_to_bus(page_address()).

#15 lore raw
Quoted 7 lines · Keir Fraser

One thing is we change the value of PCI_BUS_IS_PHYS (or some similar macro which I can't quite remember the name of) which I believe turns off some bounce-buffer logic contained within the block-device subsystem. So that will mean that we get highmem requests hitting the DMA interfaces, where on native they would have got filtered earlier by the highmem/lowmem bounce buffer logic that is specific to block-device requests.

PCI_DMA_BUS_IS_PHYS, commonly found in asm-$(arch)/pci.h.

Cheers,
Muli

#16 lore raw
In reply to Keir Fraser
Quoted 7 lines

Do we merge okay with lib/swiotlb.c then? One concern I had was with our preferred setup semantics -- we really want the user to be able to forcibly enable the swiotlb via a boot parameter *but* not have to suffer using it for every DMA operation. Last I looked the generic swiotlb didn't have that option. That and our very Xen-specific checks for whether to auto-enable the swiotlb led me to think that the very start-of-day setup of swiotlb would need to be overridable by architecture.

I think I retained all of the semantics, attached the patches as I have them by now. This is a submission for review only, as the first four patches will need to go to kernel.org (and hopefully will get accepted). The Xen customization is fairly ugly, but I didn't see anything nicer than that while also keeping the amount of changes to lib/swiotlb.c reasonable.

Patch order is
swiotlb-bugs.patch
swiotlb-bus.patch
swiotlb-cleanup.patch
swiotlb-split.patch
xen-swiotlb.patch

This patch adds abstraction so that the file can be used by environments other than IA64 and EM64T, namely for Xen.

include/asm-ia64/swiotlb.h new file +8 -0
Index: sle10-sp1-2006-12-18/include/asm-ia64/swiotlb.h
===================================================================
@@ -0,0 +1,8 @@
+#ifndef _ASM_SWIOTLB_H
+#define _ASM_SWIOTLB_H 1
+
+#include <asm/machvec.h>
+
+#define SWIOTLB_ARCH_WANT_LATE_INIT
+
+#endif /* _ASM_SWIOTLB_H */
lib/swiotlb.c +145 -29

348 lines not on this page · raw message

Convert all phys_to_virt/virt_to_phys uses to bus_to_virt/virt_to_bus.

lib/swiotlb.c +17 -16
Index: sle10-sp1-2006-12-18/lib/swiotlb.c
===================================================================
@@ -36,7 +36,7 @@
 	                   ( (val) & ( (align) - 1)))
 
 #define SG_ENT_VIRT_ADDRESS(sg)	(page_address((sg)->page) + (sg)->offset)
-#define SG_ENT_PHYS_ADDRESS(SG)	virt_to_phys(SG_ENT_VIRT_ADDRESS(SG))
+#define SG_ENT_PHYS_ADDRESS(sg)	virt_to_bus(SG_ENT_VIRT_ADDRESS(sg))
 
 /*
  * Maximum allowable number of contiguous slabs to map,
@@ -163,7 +163,7 @@ swiotlb_init_with_default_size (size_t d
 	 */
 	io_tlb_overflow_buffer = alloc_bootmem_low(io_tlb_overflow);
 	printk(KERN_INFO "Placing software IO TLB between 0x%lx - 0x%lx\n",
-	       virt_to_phys(io_tlb_start), virt_to_phys(io_tlb_end));
+	       virt_to_bus(io_tlb_start), virt_to_bus(io_tlb_end));
 }
 
 void
@@ -244,7 +244,7 @@ swiotlb_late_init_with_default_size (siz
 
 	printk(KERN_INFO "Placing %ldMB software IO TLB between 0x%lx - "
 	       "0x%lx\n", (io_tlb_nslabs * (1 << IO_TLB_SHIFT)) >> 20,
-	       virt_to_phys(io_tlb_start), virt_to_phys(io_tlb_end));
+	       virt_to_bus(io_tlb_start), virt_to_bus(io_tlb_end));
 
 	return 0;
 
@@ -446,7 +446,7 @@ swiotlb_alloc_coherent(struct device *hw
 	flags |= GFP_DMA;
 
 	ret = (void *)__get_free_pages(flags, order);
-	if (ret && address_needs_mapping(hwdev, virt_to_phys(ret))) {
+	if (ret && address_needs_mapping(hwdev, virt_to_bus(ret))) {
 		/*
 		 * The allocated memory isn't reachable by the device.
 		 * Fall back on swiotlb_map_single().
@@ -466,11 +466,11 @@ swiotlb_alloc_coherent(struct device *hw
 		if (swiotlb_dma_mapping_error(handle))
 			return NULL;
 
-		ret = phys_to_virt(handle);
+		ret = bus_to_virt(handle);
 	}
 
 	memset(ret, 0, size);
-	dev_addr = virt_to_phys(ret);
+	dev_addr = virt_to_bus(ret);
 
 	/* Confirm address can be DMA'd by device */
 	if (address_needs_mapping(hwdev, dev_addr)) {
@@ -526,7 +526,7 @@ swiotlb_full(struct device *dev, size_t 
 dma_addr_t
 swiotlb_map_single(struct device *hwdev, void *ptr, size_t size, int dir)
 {
-	unsigned long dev_addr = virt_to_phys(ptr);
+	unsigned long dev_addr = virt_to_bus(ptr);
 	void *map;
 
 	if (dir == DMA_NONE)
@@ -548,7 +548,7 @@ swiotlb_map_single(struct device *hwdev,
 		map = io_tlb_overflow_buffer;
 	}
 
-	dev_addr = virt_to_phys(map);
+	dev_addr = virt_to_bus(map);
 
 	/*
 	 * Ensure that the address returned is DMA'ble
@@ -571,7 +571,7 @@ void
 swiotlb_unmap_single(struct device *hwdev, dma_addr_t dev_addr, size_t size,
 		     int dir)
 {
-	char *dma_addr = phys_to_virt(dev_addr);
+	char *dma_addr = bus_to_virt(dev_addr);
 
 	if (dir == DMA_NONE)
 		BUG();
@@ -595,7 +595,7 @@ static inline void
 swiotlb_sync_single(struct device *hwdev, dma_addr_t dev_addr,
 		    size_t size, int dir, int target)
 {
-	char *dma_addr = phys_to_virt(dev_addr);
+	char *dma_addr = bus_to_virt(dev_addr);
 
 	if (dir == DMA_NONE)
 		BUG();
@@ -627,7 +627,7 @@ swiotlb_sync_single_range(struct device 
 			  unsigned long offset, size_t size,
 			  int dir, int target)
 {
-	char *dma_addr = phys_to_virt(dev_addr) + offset;
+	char *dma_addr = bus_to_virt(dev_addr) + offset;
 
 	if (dir == DMA_NONE)
 		BUG();
@@ -682,7 +682,7 @@ swiotlb_map_sg(struct device *hwdev, str
 
 	for (i = 0; i < nelems; i++, sg++) {
 		addr = SG_ENT_VIRT_ADDRESS(sg);
-		dev_addr = virt_to_phys(addr);
+		dev_addr = virt_to_bus(addr);
 		if (swiotlb_force || address_needs_mapping(hwdev, dev_addr)) {
 			void *map = map_single(hwdev, addr, sg->length, dir);
 			if (!map) {
@@ -716,7 +716,8 @@ swiotlb_unmap_sg(struct device *hwdev, s
 
 	for (i = 0; i < nelems; i++, sg++)
 		if (sg->dma_address != SG_ENT_PHYS_ADDRESS(sg))
-			unmap_single(hwdev, (void *) phys_to_virt(sg->dma_address), sg->dma_length, dir);
+			unmap_single(hwdev, bus_to_virt(sg->dma_address),
+				     sg->dma_length, dir);
 		else if (dir == DMA_FROM_DEVICE)
 			dma_mark_clean(SG_ENT_VIRT_ADDRESS(sg), sg->dma_length);
 }
@@ -739,7 +740,7 @@ swiotlb_sync_sg(struct device *hwdev, st
 
 	for (i = 0; i < nelems; i++, sg++)
 		if (sg->dma_address != SG_ENT_PHYS_ADDRESS(sg))
-			sync_single(hwdev, phys_to_virt(sg->dma_address),
+			sync_single(hwdev, bus_to_virt(sg->dma_address),
 				    sg->dma_length, dir, target);
 		else if (dir == DMA_FROM_DEVICE)
 			dma_mark_clean(SG_ENT_VIRT_ADDRESS(sg), sg->dma_length);
@@ -762,7 +763,7 @@ swiotlb_sync_sg_for_device(struct device
 int
 swiotlb_dma_mapping_error(dma_addr_t dma_addr)
 {
-	return (dma_addr == virt_to_phys(io_tlb_overflow_buffer));
+	return (dma_addr == virt_to_bus(io_tlb_overflow_buffer));
 }
 
 /*
@@ -774,7 +775,7 @@ swiotlb_dma_mapping_error(dma_addr_t dma
 int
 swiotlb_dma_supported (struct device *hwdev, u64 mask)
 {
-	return (virt_to_phys (io_tlb_end) - 1) <= mask;
+	return (virt_to_bus(io_tlb_end) - 1) <= mask;
 }
 
 EXPORT_SYMBOL(swiotlb_init);

This patch
- adds proper __init decoration to swiotlb's init code (and the code calling it, where not already the case)
- replaces uses of 'unsigned long' with dma_addr_t where appropriate
- does miscellaneous simplicfication and cleanup

arch/ia64/mm/init.c +1 -1
Index: sle10-sp1-2006-12-18/arch/ia64/mm/init.c
===================================================================
@@ -586,7 +586,7 @@ nolwsys_setup (char *s)
 
 __setup("nolwsys", nolwsys_setup);
 
-void
+void __init
 mem_init (void)
 {
 	long reserved_pages, codesize, datasize, initsize;
arch/x86_64/kernel/pci-swiotlb.c +1 -1
Index: sle10-sp1-2006-12-18/arch/x86_64/kernel/pci-swiotlb.c
===================================================================
@@ -28,7 +28,7 @@ struct dma_mapping_ops swiotlb_dma_ops =
 	.dma_supported = NULL,
 };
 
-void pci_swiotlb_init(void)
+void __init pci_swiotlb_init(void)
 {
 	/* don't initialize swiotlb if iommu=off (no_iommu=1) */
 	if (!iommu_detected && !no_iommu &&
lib/swiotlb.c +29 -24

156 lines not on this page · raw message

This patch fixes
- marking I-cache clean of pages DMAed to now only done for IA64
- broken multiple inclusion in include/asm-x86_64/swiotlb.h
- missing phys-to-virt translation in swiotlb_sync_sg()
- missing call to mark_clean in swiotlb_sync_sg()

arch/ia64/mm/init.c +19 -0
Index: sle10-sp1-2006-12-18/arch/ia64/mm/init.c
===================================================================
@@ -123,6 +123,25 @@ lazy_mmu_prot_update (pte_t pte)
 	set_bit(PG_arch_1, &page->flags);	/* mark page as clean */
 }
 
+/*
+ * Since DMA is i-cache coherent, any (complete) pages that were written via
+ * DMA can be marked as "clean" so that lazy_mmu_prot_update() doesn't have to
+ * flush them when they get mapped into an executable vm-area.
+ */
+void
+dma_mark_clean(void *addr, size_t size)
+{
+	unsigned long pg_addr, end;
+
+	pg_addr = PAGE_ALIGN((unsigned long) addr);
+	end = (unsigned long) addr + size;
+	while (pg_addr + PAGE_SIZE <= end) {
+		struct page *page = virt_to_page(pg_addr);
+		set_bit(PG_arch_1, &page->flags);
+		pg_addr += PAGE_SIZE;
+	}
+}
+
 inline void
 ia64_set_rbs_bot (void)
 {
include/asm-ia64/dma.h +2 -0
Index: sle10-sp1-2006-12-18/include/asm-ia64/dma.h
===================================================================
@@ -20,4 +20,6 @@ extern unsigned long MAX_DMA_ADDRESS;
 
 #define free_dma(x)
 
+void dma_mark_clean(void *addr, size_t size);
+
 #endif /* _ASM_IA64_DMA_H */
include/asm-x86_64/mach-xen/asm/dma-mapping.h +0 -1
Index: sle10-sp1-2006-12-18/include/asm-x86_64/mach-xen/asm/dma-mapping.h
===================================================================
@@ -10,7 +10,6 @@
 
 #include <asm/scatterlist.h>
 #include <asm/io.h>
-#include <asm/swiotlb.h>
 
 struct dma_mapping_ops {
 	int             (*mapping_error)(dma_addr_t dma_addr);
include/asm-x86_64/swiotlb.h +4 -2
Index: sle10-sp1-2006-12-18/include/asm-x86_64/swiotlb.h
===================================================================
@@ -1,5 +1,5 @@
 #ifndef _ASM_SWIOTLB_H
-#define _ASM_SWTIOLB_H 1
+#define _ASM_SWIOTLB_H 1
 
 #include <linux/config.h>
 
@@ -59,4 +59,6 @@ extern int swiotlb;
 
 extern void pci_swiotlb_init(void);
 
-#endif /* _ASM_SWTIOLB_H */
+static inline void dma_mark_clean(void *addr, size_t size) {}
+
+#endif /* _ASM_SWIOTLB_H */
lib/swiotlb.c +8 -25
Index: sle10-sp1-2006-12-18/lib/swiotlb.c
===================================================================
@@ -560,25 +560,6 @@ swiotlb_map_single(struct device *hwdev,
 }
 
 /*
- * Since DMA is i-cache coherent, any (complete) pages that were written via
- * DMA can be marked as "clean" so that lazy_mmu_prot_update() doesn't have to
- * flush them when they get mapped into an executable vm-area.
- */
-static void
-mark_clean(void *addr, size_t size)
-{
-	unsigned long pg_addr, end;
-
-	pg_addr = PAGE_ALIGN((unsigned long) addr);
-	end = (unsigned long) addr + size;
-	while (pg_addr + PAGE_SIZE <= end) {
-		struct page *page = virt_to_page(pg_addr);
-		set_bit(PG_arch_1, &page->flags);
-		pg_addr += PAGE_SIZE;
-	}
-}
-
-/*
  * Unmap a single streaming mode DMA translation.  The dma_addr and size must
  * match what was provided for in a previous swiotlb_map_single call.  All
  * other usages are undefined.
@@ -597,7 +578,7 @@ swiotlb_unmap_single(struct device *hwde
 	if (dma_addr >= io_tlb_start && dma_addr < io_tlb_end)
 		unmap_single(hwdev, dma_addr, size, dir);
 	else if (dir == DMA_FROM_DEVICE)
-		mark_clean(dma_addr, size);
+		dma_mark_clean(dma_addr, size);
 }
 
 /*
@@ -621,7 +602,7 @@ swiotlb_sync_single(struct device *hwdev
 	if (dma_addr >= io_tlb_start && dma_addr < io_tlb_end)
 		sync_single(hwdev, dma_addr, size, dir, target);
 	else if (dir == DMA_FROM_DEVICE)
-		mark_clean(dma_addr, size);
+		dma_mark_clean(dma_addr, size);
 }
 
 void
@@ -653,7 +634,7 @@ swiotlb_sync_single_range(struct device 
 	if (dma_addr >= io_tlb_start && dma_addr < io_tlb_end)
 		sync_single(hwdev, dma_addr, size, dir, target);
 	else if (dir == DMA_FROM_DEVICE)
-		mark_clean(dma_addr, size);
+		dma_mark_clean(dma_addr, size);
 }
 
 void
@@ -704,7 +685,6 @@ swiotlb_map_sg(struct device *hwdev, str
 		dev_addr = virt_to_phys(addr);
 		if (swiotlb_force || address_needs_mapping(hwdev, dev_addr)) {
 			void *map = map_single(hwdev, addr, sg->length, dir);
-			sg->dma_address = virt_to_bus(map);
 			if (!map) {
 				/* Don't panic here, we expect map_sg users
 				   to do proper error handling. */
@@ -713,6 +693,7 @@ swiotlb_map_sg(struct device *hwdev, str
 				sg[0].dma_length = 0;
 				return 0;
 			}
+			sg->dma_address = virt_to_bus(map);
 		} else
 			sg->dma_address = dev_addr;
 		sg->dma_length = sg->length;
@@ -737,7 +718,7 @@ swiotlb_unmap_sg(struct device *hwdev, s
 		if (sg->dma_address != SG_ENT_PHYS_ADDRESS(sg))
 			unmap_single(hwdev, (void *) phys_to_virt(sg->dma_address), sg->dma_length, dir);
 		else if (dir == DMA_FROM_DEVICE)
-			mark_clean(SG_ENT_VIRT_ADDRESS(sg), sg->dma_length);
+			dma_mark_clean(SG_ENT_VIRT_ADDRESS(sg), sg->dma_length);
 }
 
 /*
@@ -758,8 +739,10 @@ swiotlb_sync_sg(struct device *hwdev, st
 
 	for (i = 0; i < nelems; i++, sg++)
 		if (sg->dma_address != SG_ENT_PHYS_ADDRESS(sg))
-			sync_single(hwdev, (void *) sg->dma_address,
+			sync_single(hwdev, phys_to_virt(sg->dma_address),
 				    sg->dma_length, dir, target);
+		else if (dir == DMA_FROM_DEVICE)
+			dma_mark_clean(SG_ENT_VIRT_ADDRESS(sg), sg->dma_length);
 }
 
 void

This patch eliminates Xen's special version of the swiotlb code. Along with that it adds trivial forwarding of dma_{,un}map_page when not using highmem.

arch/i386/kernel/swiotlb.c deleted +0 -682

682 lines not on this page · raw message

arch/i386/kernel/pci-dma-xen.c +3 -9
Index: sle10-sp1-2006-12-18/arch/i386/kernel/pci-dma-xen.c
===================================================================
@@ -16,7 +16,7 @@
 #include <asm/io.h>
 #include <xen/balloon.h>
 #include <asm/tlbflush.h>
-#include <asm-i386/mach-xen/asm/swiotlb.h>
+#include <asm/dma-mapping.h>
 #include <asm/bug.h>
 
 #ifdef __x86_64__
@@ -93,13 +93,7 @@ dma_unmap_sg(struct device *hwdev, struc
 }
 EXPORT_SYMBOL(dma_unmap_sg);
 
-/*
- * XXX This file is also used by xenLinux/ia64. 
- * "defined(__i386__) || defined (__x86_64__)" means "!defined(__ia64__)".
- * This #if work around should be removed once this file is merbed back into
- * i386' pci-dma or is moved to drivers/xen/core.
- */
-#if defined(__i386__) || defined(__x86_64__)
+#ifdef CONFIG_HIGHMEM
 dma_addr_t
 dma_map_page(struct device *dev, struct page *page, unsigned long offset,
 	     size_t size, enum dma_data_direction direction)
@@ -129,7 +123,7 @@ dma_unmap_page(struct device *dev, dma_a
 		swiotlb_unmap_page(dev, dma_address, size, direction);
 }
 EXPORT_SYMBOL(dma_unmap_page);
-#endif /* defined(__i386__) || defined(__x86_64__) */
+#endif /* CONFIG_HIGHMEM */
 
 int
 dma_mapping_error(dma_addr_t dma_addr)
include/asm-i386/mach-xen/asm/dma-mapping.h +7 -1
Index: sle10-sp1-2006-12-18/include/asm-i386/mach-xen/asm/dma-mapping.h
===================================================================
@@ -24,7 +24,7 @@ address_needs_mapping(struct device *hwd
 }
 
 static inline int
-range_straddles_page_boundary(void *p, size_t size)
+range_straddles_page_boundary(const void *p, size_t size)
 {
 	extern unsigned long *contiguous_bitmap;
 	return (((((unsigned long)p & ~PAGE_MASK) + size) > PAGE_SIZE) &&
@@ -53,6 +53,7 @@ extern int dma_map_sg(struct device *hwd
 extern void dma_unmap_sg(struct device *hwdev, struct scatterlist *sg,
 			 int nents, enum dma_data_direction direction);
 
+#ifdef CONFIG_HIGHMEM
 extern dma_addr_t
 dma_map_page(struct device *dev, struct page *page, unsigned long offset,
 	     size_t size, enum dma_data_direction direction);
@@ -60,6 +61,11 @@ dma_map_page(struct device *dev, struct 
 extern void
 dma_unmap_page(struct device *dev, dma_addr_t dma_address, size_t size,
 	       enum dma_data_direction direction);
+#else
+#define dma_map_page(dev, page, offset, size, dir) \
+	dma_map_single(dev, page_address(page) + (offset), (size), (dir))
+#define dma_unmap_page dma_unmap_single
+#endif
 
 extern void
 dma_sync_single_for_cpu(struct device *dev, dma_addr_t dma_handle, size_t size,
include/asm-i386/mach-xen/asm/swiotlb.h +6 -31
Index: sle10-sp1-2006-12-18/include/asm-i386/mach-xen/asm/swiotlb.h
===================================================================
@@ -1,43 +1,18 @@
-#ifndef _ASM_SWIOTLB_H
-#define _ASM_SWIOTLB_H 1
+#ifndef _ASM_XEN_SWIOTLB_H
+#define _ASM_XEN_SWIOTLB_H 1
 
-#include <linux/config.h>
+#include <xen/swiotlb.h>
 
-/* SWIOTLB interface */
+#ifdef CONFIG_HIGHMEM
+
+#define SG_ENT_PHYS_ADDRESS(sg)	(page_to_bus((sg)->page) + (sg)->offset)
 
-extern dma_addr_t swiotlb_map_single(struct device *hwdev, void *ptr, size_t size,
-				      int dir);
-extern void swiotlb_unmap_single(struct device *hwdev, dma_addr_t dev_addr,
-				  size_t size, int dir);
-extern void swiotlb_sync_single_for_cpu(struct device *hwdev,
-					 dma_addr_t dev_addr,
-					 size_t size, int dir);
-extern void swiotlb_sync_single_for_device(struct device *hwdev,
-					    dma_addr_t dev_addr,
-					    size_t size, int dir);
-extern void swiotlb_sync_sg_for_cpu(struct device *hwdev,
-				     struct scatterlist *sg, int nelems,
-				     int dir);
-extern void swiotlb_sync_sg_for_device(struct device *hwdev,
-					struct scatterlist *sg, int nelems,
-					int dir);
-extern int swiotlb_map_sg(struct device *hwdev, struct scatterlist *sg,
-		      int nents, int direction);
-extern void swiotlb_unmap_sg(struct device *hwdev, struct scatterlist *sg,
-			 int nents, int direction);
-extern int swiotlb_dma_mapping_error(dma_addr_t dma_addr);
 extern dma_addr_t swiotlb_map_page(struct device *hwdev, struct page *page,
                                    unsigned long offset, size_t size,
                                    enum dma_data_direction direction);
 extern void swiotlb_unmap_page(struct device *hwdev, dma_addr_t dma_address,
                                size_t size, enum dma_data_direction direction);
-extern int swiotlb_dma_supported(struct device *hwdev, u64 mask);
-extern void swiotlb_init(void);
 
-#ifdef CONFIG_SWIOTLB
-extern int swiotlb;
-#else
-#define swiotlb 0
 #endif
 
 #endif
include/asm-ia64/swiotlb.h +8 -0
Index: sle10-sp1-2006-12-18/include/asm-ia64/swiotlb.h
===================================================================
@@ -1,8 +1,16 @@
 #ifndef _ASM_SWIOTLB_H
 #define _ASM_SWIOTLB_H 1
 
+#include <linux/config.h>
+
+#ifndef CONFIG_XEN
+
 #include <asm/machvec.h>
 
 #define SWIOTLB_ARCH_WANT_LATE_INIT
 
+#else
+#include <xen/swiotlb.h>
+#endif
+
 #endif /* _ASM_SWIOTLB_H */
include/asm-x86_64/mach-xen/asm/swiotlb.h new file +1 -0
Index: sle10-sp1-2006-12-18/include/asm-x86_64/mach-xen/asm/swiotlb.h
===================================================================
@@ -0,0 +1 @@
+#include <xen/swiotlb.h>
include/asm-x86_64/swiotlb.h +0 -8
Index: sle10-sp1-2006-12-18/include/asm-x86_64/swiotlb.h
===================================================================
@@ -43,14 +43,6 @@ extern void swiotlb_free_coherent (struc
 extern int swiotlb_dma_supported(struct device *hwdev, u64 mask);
 extern void swiotlb_init(void);
 
-#ifdef CONFIG_XEN
-extern dma_addr_t swiotlb_map_page(struct device *hwdev, struct page *page,
-                                   unsigned long offset, size_t size,
-                                   enum dma_data_direction direction);
-extern void swiotlb_unmap_page(struct device *hwdev, dma_addr_t dma_address,
-                               size_t size, enum dma_data_direction direction);
-#endif
-
 #ifdef CONFIG_SWIOTLB
 extern int swiotlb;
 #else
include/xen/swiotlb.h new file +169 -0

169 lines not on this page · raw message

lib/Makefile +0 -1
Index: sle10-sp1-2006-12-18/lib/Makefile
===================================================================
@@ -49,7 +49,6 @@ obj-$(CONFIG_SGRB)	 += sgrb.o
 obj-$(CONFIG_STATISTICS) += statistic.o
 
 obj-$(CONFIG_SWIOTLB) += swiotlb.o
-swiotlb-$(CONFIG_XEN) := ../arch/i386/kernel/swiotlb.o
 
 hostprogs-y	:= gen_crc32table
 clean-files	:= crc32table.h

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel