Skip to contentSkip to conversation

use generic DMA mapping code in powerpc

PATCH In review Christoph Hellwig 4 participants 52 messages Jul 30, 2018 – Aug 27, 2018

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

Hi all,

this series switches the powerpc port to use the generic swiotlb and noncoherent dma ops, and to use more generic code for the coherent direct mapping, as well as removing dead code.

#1 lore raw

PATCH 1/20 kernel/dma/direct: take DMA offset into account in dma_direct_supported

When a device has a DMA offset the dma capable result will change due to the difference between the physical and DMA address. Take that into account.

---
 kernel/dma/direct.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
kernel/dma/direct.c +3 -3
index 8be8106270c2..d32d4f0d2c0c 100644
@@ -167,7 +167,7 @@ int dma_direct_map_sg(struct device *dev, struct scatterlist *sgl, int nents,
 int dma_direct_supported(struct device *dev, u64 mask)
 {
 #ifdef CONFIG_ZONE_DMA
-	if (mask < DMA_BIT_MASK(ARCH_ZONE_DMA_BITS))
+	if (mask < phys_to_dma(dev, DMA_BIT_MASK(ARCH_ZONE_DMA_BITS)))
 		return 0;
 #else
 	/*
@@ -176,14 +176,14 @@ int dma_direct_supported(struct device *dev, u64 mask)
 	 * memory, or by providing a ZONE_DMA32.  If neither is the case, the
 	 * architecture needs to use an IOMMU instead of the direct mapping.
 	 */
-	if (mask < DMA_BIT_MASK(32))
+	if (mask < phys_to_dma(dev, DMA_BIT_MASK(32)))
 		return 0;
 #endif
 	/*
 	 * Various PCI/PCIe bridges have broken support for > 32bit DMA even
 	 * if the device itself might support it.
 	 */
-	if (dev->dma_32bit_limit && mask > DMA_BIT_MASK(32))
+	if (dev->dma_32bit_limit && mask > phys_to_dma(dev, DMA_BIT_MASK(32)))
 		return 0;
 	return 1;
 }
Signature · 1 line
2.18.0
Reviewed-by on v1 1/20
#2 lore raw

Re: [PATCH 01/20] kernel/dma/direct: take DMA offset into account in dma_direct_supported

Quoted 3 lines · Christoph Hellwig

When a device has a DMA offset the dma capable result will change due to the difference between the physical and DMA address. Take that into account.

The patch in itself makes sense.

However, there are a number of things in that dma_direct.c file that I don't quite get:

- looking more generally at what that function does, I worry about the
switch of ppc32 to this later on:

We do have the occasional device with things like 31-bit DMA limitation. We know they happens to work because those systems can't have enough memory to be a problem. This is why our current DMA direct ops in powerpc just unconditionally return true on ppc32.

The test against a full 32-bit mask here will break them I think.

Thing is, I'm not sure I still have access to one of these things to test, I'll have to dig (from memory things like b43 wifi).

Also those platforms don't have an iommu.

- What is this trying to achieve ?

	/*
	 * Various PCI/PCIe bridges have broken support for > 32bit DMA even
	 * if the device itself might support it.
	 */
	if (dev->dma_32bit_limit && mask > phys_to_dma(dev, DMA_BIT_MASK(32)))
		return 0;

IE, if the device has a 32-bit limit, we fail an attempt at checking if a >32-bit mask works ? That doesn't quite seem to be the right thing to do... Shouldn't this be in dma_set_mask() and just clamp the mask down ?

IE, dma_set_mask() is what a driver uses to establish the device capability, so it makes sense tot have dma_32bit_limit just reduce that capability, not fail because the device can do more than what the bridge can....

Sorry if I'm a bit confused here.

- How is that file supposed to work on 64-bit platforms ? From what I can
tell, dma_supported() will unconditionally return true if the mask is 32-bit or larger (appart from the above issue). This doesn't look right, the mask needs to be compared to the max memory address. There are a bunch of devices out there with masks anywhere bettween 40 and 64 bits, and some of these will not work "out of the box" if the offseted top of memory is beyond the mask limit. Or am I missing something ?

Cheers,
Ben.

Quoted 1 line
Quoted 34 lines
---
 kernel/dma/direct.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
kernel/dma/direct.c +3 -3
index 8be8106270c2..d32d4f0d2c0c 100644
@@ -167,7 +167,7 @@ int dma_direct_map_sg(struct device *dev, struct scatterlist *sgl, int nents,
 int dma_direct_supported(struct device *dev, u64 mask)
 {
 #ifdef CONFIG_ZONE_DMA
-	if (mask < DMA_BIT_MASK(ARCH_ZONE_DMA_BITS))
+	if (mask < phys_to_dma(dev, DMA_BIT_MASK(ARCH_ZONE_DMA_BITS)))
 		return 0;
 #else
 	/*
@@ -176,14 +176,14 @@ int dma_direct_supported(struct device *dev, u64 mask)
 	 * memory, or by providing a ZONE_DMA32.  If neither is the case, the
 	 * architecture needs to use an IOMMU instead of the direct mapping.
 	 */
-	if (mask < DMA_BIT_MASK(32))
+	if (mask < phys_to_dma(dev, DMA_BIT_MASK(32)))
 		return 0;
 #endif
 	/*
 	 * Various PCI/PCIe bridges have broken support for > 32bit DMA even
 	 * if the device itself might support it.
 	 */
-	if (dev->dma_32bit_limit && mask > DMA_BIT_MASK(32))
+	if (dev->dma_32bit_limit && mask > phys_to_dma(dev, DMA_BIT_MASK(32)))
 		return 0;
 	return 1;
 }
#3 lore raw

Re: [PATCH 01/20] kernel/dma/direct: take DMA offset into account in dma_direct_supported

Quoted 9 lines · Benjamin Herrenschmidt

We do have the occasional device with things like 31-bit DMA limitation. We know they happens to work because those systems can't have enough memory to be a problem. This is why our current DMA direct ops in powerpc just unconditionally return true on ppc32.

The test against a full 32-bit mask here will break them I think.

Thing is, I'm not sure I still have access to one of these things to test, I'll have to dig (from memory things like b43 wifi).

Yeah, the other platforms that support these devices support ZONE_DMA to reliably handle these devices. But there is two other ways the current code would actually handle these fine despite the dma_direct checks:

1) if the device only has physical addresses up to 31-bit anyway
2) by trying again to find a lower address. But this only works for coherent allocations and not streaming maps (unless we have swiotlb with a buffer below 31-bits).

It seems powerpc can have ZONE_DMA, though and we will cover these devices just fine. If it didn't have that the current powerpc code would not work either.

Quoted 9 lines

- What is this trying to achieve ?

	/*
	 * Various PCI/PCIe bridges have broken support for > 32bit DMA even
	 * if the device itself might support it.
	 */
	if (dev->dma_32bit_limit && mask > phys_to_dma(dev, DMA_BIT_MASK(32)))
		return 0;
Quoted 7 lines

IE, if the device has a 32-bit limit, we fail an attempt at checking if a >32-bit mask works ? That doesn't quite seem to be the right thing to do... Shouldn't this be in dma_set_mask() and just clamp the mask down ?

IE, dma_set_mask() is what a driver uses to establish the device capability, so it makes sense tot have dma_32bit_limit just reduce that capability, not fail because the device can do more than what the bridge can....

If your PCI bridge / PCIe root port doesn't support dma to addresses larger than 32-bit the device capabilities above that don't matter, it just won't work. We have this case at least for some old VIA x86 chipsets and some relatively modern Xilinx FPGAs with PCIe.

Quoted 7 lines

- How is that file supposed to work on 64-bit platforms ? From what I can
tell, dma_supported() will unconditionally return true if the mask is 32-bit or larger (appart from the above issue). This doesn't look right, the mask needs to be compared to the max memory address. There are a bunch of devices out there with masks anywhere bettween 40 and 64 bits, and some of these will not work "out of the box" if the offseted top of memory is beyond the mask limit. Or am I missing something ?

Your are not missing anything except for the history of this code.

Your observation is right, but there always has been the implicit assumption that architectures with more than 4GB of physical address space must either support and iommu or swiotlb and use that. It's never been document anywhere, but I'm working on integrating all this code to make more sense.

#4 lore raw

Re: [PATCH 01/20] kernel/dma/direct: take DMA offset into account in dma_direct_supported

Quoted 16 lines · Christoph Hellwig
Quoted 9 lines · Benjamin Herrenschmidt

We do have the occasional device with things like 31-bit DMA limitation. We know they happens to work because those systems can't have enough memory to be a problem. This is why our current DMA direct ops in powerpc just unconditionally return true on ppc32.

The test against a full 32-bit mask here will break them I think.

Thing is, I'm not sure I still have access to one of these things to test, I'll have to dig (from memory things like b43 wifi).

Yeah, the other platforms that support these devices support ZONE_DMA to reliably handle these devices. But there is two other ways the current code would actually handle these fine despite the dma_direct checks:

Quoted 8 lines

1) if the device only has physical addresses up to 31-bit anyway
2) by trying again to find a lower address. But this only works for coherent allocations and not streaming maps (unless we have swiotlb with a buffer below 31-bits).

It seems powerpc can have ZONE_DMA, though and we will cover these devices just fine. If it didn't have that the current powerpc code would not work either.

Not exactly. powerpc has ZONE_DMA covering all of system memory.

What happens in ppc32 is that we somewhat "know" that none of the systems with those stupid 31-bit limited pieces of HW is capable of having more than 2GB of memory anyway.

So we get away with just returning "1".

Quoted 14 lines
Quoted 14 lines

- What is this trying to achieve ?

	/*
	 * Various PCI/PCIe bridges have broken support for > 32bit DMA even
	 * if the device itself might support it.
	 */
	if (dev->dma_32bit_limit && mask > phys_to_dma(dev, DMA_BIT_MASK(32)))
		return 0;

IE, if the device has a 32-bit limit, we fail an attempt at checking if a >32-bit mask works ? That doesn't quite seem to be the right thing to do... Shouldn't this be in dma_set_mask() and just clamp the mask down ?

Quoted 8 lines
Quoted 3 lines

IE, dma_set_mask() is what a driver uses to establish the device capability, so it makes sense tot have dma_32bit_limit just reduce that capability, not fail because the device can do more than what the bridge can....

If your PCI bridge / PCIe root port doesn't support dma to addresses larger than 32-bit the device capabilities above that don't matter, it just won't work. We have this case at least for some old VIA x86 chipsets and some relatively modern Xilinx FPGAs with PCIe.

Hrm... that's the usual confusion dma_capable() vs. dma_set_mask().

It's always been perfectly fine for a driver to do a dma_set_mask(64- bit) on a system where the bridge can only do 32-bits ...

We shouldn't fail there, we should instead "clamp" the mask to 32-bit, see what I mean ? It doesn't matter that the device itself is capable of issuing >32 addresses, I agree, but what we need to express is that the combination device+bridge doesn't want addresses above 32-bit, so it's equivalent to making the device do a set_mask(32-bit).

This will succeed if the system can limit the addresses (for example because memory is never above 32-bit) and will fail if the system can't.

So that's equivalent of writing

	if (dev->dma_32bit_limit && mask > phys_to_dma(dev, DMA_BIT_MASK(32)))
		mask = phys_to_dma(dev, DMA_BIT_MASK(32)); 

Effectively meaning "don't give me addresses aboe 32-bit".

Still, your code doesn't check the mask against the memory size. Which means it will fail for 32-bit masks even on systems that do not have memory above 4G.

Quoted 8 lines
Quoted 7 lines

- How is that file supposed to work on 64-bit platforms ? From what I can
tell, dma_supported() will unconditionally return true if the mask is 32-bit or larger (appart from the above issue). This doesn't look right, the mask needs to be compared to the max memory address. There are a bunch of devices out there with masks anywhere bettween 40 and 64 bits, and some of these will not work "out of the box" if the offseted top of memory is beyond the mask limit. Or am I missing something ?

Quoted 7 lines

Your are not missing anything except for the history of this code.

Your observation is right, but there always has been the implicit assumption that architectures with more than 4GB of physical address space must either support and iommu or swiotlb and use that. It's never been document anywhere, but I'm working on integrating all this code to make more sense.

Well, iommus can have bypass regions, which we also use for performance, so we do at dma_set_mask() time "swap" the ops around, and in that case, we do want to check the mask against the actual top of memory...

Cheers,
Ben.

#5 lore raw

Re: [PATCH 01/20] kernel/dma/direct: take DMA offset into account in dma_direct_supported

Quoted 14 lines · Benjamin Herrenschmidt
Quoted 5 lines

Yeah, the other platforms that support these devices support ZONE_DMA to reliably handle these devices. But there is two other ways the current code would actually handle these fine despite the dma_direct checks:

Quoted 8 lines

1) if the device only has physical addresses up to 31-bit anyway
2) by trying again to find a lower address. But this only works for coherent allocations and not streaming maps (unless we have swiotlb with a buffer below 31-bits).

It seems powerpc can have ZONE_DMA, though and we will cover these devices just fine. If it didn't have that the current powerpc code would not work either.

Quoted 7 lines

Not exactly. powerpc has ZONE_DMA covering all of system memory.

What happens in ppc32 is that we somewhat "know" that none of the systems with those stupid 31-bit limited pieces of HW is capable of having more than 2GB of memory anyway.

So we get away with just returning "1".

I think I can up with a proper way of handling that by checking the actual amount of physical memory present instead of the hard coded 32-bit.

Quoted 9 lines
Quoted 4 lines

If your PCI bridge / PCIe root port doesn't support dma to addresses larger than 32-bit the device capabilities above that don't matter, it just won't work. We have this case at least for some old VIA x86 chipsets and some relatively modern Xilinx FPGAs with PCIe.

Hrm... that's the usual confusion dma_capable() vs. dma_set_mask().

It's always been perfectly fine for a driver to do a dma_set_mask(64- bit) on a system where the bridge can only do 32-bits ...

No, it hasn't. That's why we have this pattern of trying a 64-bit mask first and then setting a 32-bit mask if that fails all over drivers/. However with all the work we've done over the last month we are getting really close to a world where:

- the driver just does one dma_set_mask for the capabilities and stores that in the dma_mask
- other limitations go elsewhere and will be automatically taken into account.

Which is I guess what you always wanted, but which wasn't how things actually worked before.

Quoted 5 lines

We shouldn't fail there, we should instead "clamp" the mask to 32-bit, see what I mean ? It doesn't matter that the device itself is capable of issuing >32 addresses, I agree, but what we need to express is that the combination device+bridge doesn't want addresses above 32-bit, so it's equivalent to making the device do a set_mask(32-bit).

As said, we'll get there (but with the new separate bus_dma_mask in 4.19), but this is not how things currently work.

Quoted 10 lines
Quoted 5 lines

Your observation is right, but there always has been the implicit assumption that architectures with more than 4GB of physical address space must either support and iommu or swiotlb and use that. It's never been document anywhere, but I'm working on integrating all this code to make more sense.

Well, iommus can have bypass regions, which we also use for performance, so we do at dma_set_mask() time "swap" the ops around, and in that case, we do want to check the mask against the actual top of memory...

That is a bit of a powerpc special case (we also had one other arch doing that, but it got removed in the great purge, can't rember which one right now). Everyone else has one set of ops, and they just switch to the direct mapping inside the iommu ops.

#6 lore raw

Re: [PATCH 01/20] kernel/dma/direct: take DMA offset into account in dma_direct_supported

Quoted 9 lines · Christoph Hellwig
Quoted 4 lines

Well, iommus can have bypass regions, which we also use for performance, so we do at dma_set_mask() time "swap" the ops around, and in that case, we do want to check the mask against the actual top of memory...

That is a bit of a powerpc special case (we also had one other arch doing that, but it got removed in the great purge, can't rember which one right now). Everyone else has one set of ops, and they just switch to the direct mapping inside the iommu ops.

We more or less do that too in some of ours these days bcs of the whole coherent_mask vs mask where a given device might need either depending on the type of mapping.

Ben.

#7 lore raw

PATCH 2/20 kernel/dma/direct: refine dma_direct_alloc zone selection

In reply to Christoph Hellwig

We need to take the DMA offset and encryption bit into account when selecting a zone. Add a helper that takes those into account and use it.

---
 kernel/dma/direct.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)
kernel/dma/direct.c +12 -4
index d32d4f0d2c0c..c2c1df8827f2 100644
@@ -58,6 +58,14 @@ static bool dma_coherent_ok(struct device *dev, phys_addr_t phys, size_t size)
 	return addr + size - 1 <= dev->coherent_dma_mask;
 }
 
+static bool dma_coherent_below(struct device *dev, u64 mask)
+{
+	dma_addr_t addr = force_dma_unencrypted() ?
+		__phys_to_dma(dev, mask) : phys_to_dma(dev, mask);
+
+	return dev->coherent_dma_mask <= addr;
+}
+
 void *dma_direct_alloc(struct device *dev, size_t size, dma_addr_t *dma_handle,
 		gfp_t gfp, unsigned long attrs)
 {
@@ -70,9 +78,9 @@ void *dma_direct_alloc(struct device *dev, size_t size, dma_addr_t *dma_handle,
 	gfp &= ~__GFP_ZERO;
 
 	/* GFP_DMA32 and GFP_DMA are no ops without the corresponding zones: */
-	if (dev->coherent_dma_mask <= DMA_BIT_MASK(ARCH_ZONE_DMA_BITS))
+	if (dma_coherent_below(dev, DMA_BIT_MASK(ARCH_ZONE_DMA_BITS)))
 		gfp |= GFP_DMA;
-	if (dev->coherent_dma_mask <= DMA_BIT_MASK(32) && !(gfp & GFP_DMA))
+	if (dma_coherent_below(dev, DMA_BIT_MASK(32) && !(gfp & GFP_DMA)))
 		gfp |= GFP_DMA32;
 
 again:
@@ -92,14 +100,14 @@ void *dma_direct_alloc(struct device *dev, size_t size, dma_addr_t *dma_handle,
 		page = NULL;
 
 		if (IS_ENABLED(CONFIG_ZONE_DMA32) &&
-		    dev->coherent_dma_mask < DMA_BIT_MASK(64) &&
+		    dma_coherent_below(dev, DMA_BIT_MASK(64)) &&
 		    !(gfp & (GFP_DMA32 | GFP_DMA))) {
 			gfp |= GFP_DMA32;
 			goto again;
 		}
 
 		if (IS_ENABLED(CONFIG_ZONE_DMA) &&
-		    dev->coherent_dma_mask < DMA_BIT_MASK(32) &&
+		    dma_coherent_below(dev, DMA_BIT_MASK(32)) &&
 		    !(gfp & GFP_DMA)) {
 			gfp = (gfp & ~GFP_DMA32) | GFP_DMA;
 			goto again;
Signature · 1 line
2.18.0
#8 lore raw

Re: [PATCH 02/20] kernel/dma/direct: refine dma_direct_alloc zone selection

Quoted 2 lines · Christoph Hellwig

We need to take the DMA offset and encryption bit into account when selecting a zone. Add a helper that takes those into account and use it.

That whole "encryption" stuff seems to be completely specific to the way x86 does memory encryption, or am I mistaken ? It's not clear to me what that does in practice and how it relates to DMA mappings.

I'm also not sure about that whole business with ZONE_DMA and ARCH_ZONE_DMA_BITS...

On ppc64, unless you enable swiotlb (which we only do currently on some embedded platforms), you have all of memory in ZONE_DMA.

[    0.000000] Zone ranges:
[    0.000000]   DMA      [mem 0x0000000000000000-0x0000001fffffffff]
[    0.000000]   DMA32    empty
[    0.000000]   Normal   empty
[    0.000000]   Device   empty

I'm not sure how this will work with that dma direct code.

I also see a number of tests against a 64-bit mask rather than the top of memory...

Ben.

Quoted 53 lines
---
 kernel/dma/direct.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)
kernel/dma/direct.c +12 -4
index d32d4f0d2c0c..c2c1df8827f2 100644
@@ -58,6 +58,14 @@ static bool dma_coherent_ok(struct device *dev, phys_addr_t phys, size_t size)
 	return addr + size - 1 <= dev->coherent_dma_mask;
 }
 
+static bool dma_coherent_below(struct device *dev, u64 mask)
+{
+	dma_addr_t addr = force_dma_unencrypted() ?
+		__phys_to_dma(dev, mask) : phys_to_dma(dev, mask);
+
+	return dev->coherent_dma_mask <= addr;
+}
+
 void *dma_direct_alloc(struct device *dev, size_t size, dma_addr_t *dma_handle,
 		gfp_t gfp, unsigned long attrs)
 {
@@ -70,9 +78,9 @@ void *dma_direct_alloc(struct device *dev, size_t size, dma_addr_t *dma_handle,
 	gfp &= ~__GFP_ZERO;
 
 	/* GFP_DMA32 and GFP_DMA are no ops without the corresponding zones: */
-	if (dev->coherent_dma_mask <= DMA_BIT_MASK(ARCH_ZONE_DMA_BITS))
+	if (dma_coherent_below(dev, DMA_BIT_MASK(ARCH_ZONE_DMA_BITS)))
 		gfp |= GFP_DMA;
-	if (dev->coherent_dma_mask <= DMA_BIT_MASK(32) && !(gfp & GFP_DMA))
+	if (dma_coherent_below(dev, DMA_BIT_MASK(32) && !(gfp & GFP_DMA)))
 		gfp |= GFP_DMA32;
 
 again:
@@ -92,14 +100,14 @@ void *dma_direct_alloc(struct device *dev, size_t size, dma_addr_t *dma_handle,
 		page = NULL;
 
 		if (IS_ENABLED(CONFIG_ZONE_DMA32) &&
-		    dev->coherent_dma_mask < DMA_BIT_MASK(64) &&
+		    dma_coherent_below(dev, DMA_BIT_MASK(64)) &&
 		    !(gfp & (GFP_DMA32 | GFP_DMA))) {
 			gfp |= GFP_DMA32;
 			goto again;
 		}
 
 		if (IS_ENABLED(CONFIG_ZONE_DMA) &&
-		    dev->coherent_dma_mask < DMA_BIT_MASK(32) &&
+		    dma_coherent_below(dev, DMA_BIT_MASK(32)) &&
 		    !(gfp & GFP_DMA)) {
 			gfp = (gfp & ~GFP_DMA32) | GFP_DMA;
 			goto again;
#9 lore raw

Re: [PATCH 02/20] kernel/dma/direct: refine dma_direct_alloc zone selection

Quoted 7 lines · Benjamin Herrenschmidt
Quoted 2 lines · Christoph Hellwig

We need to take the DMA offset and encryption bit into account when selecting a zone. Add a helper that takes those into account and use it.

That whole "encryption" stuff seems to be completely specific to the way x86 does memory encryption, or am I mistaken ? It's not clear to me what that does in practice and how it relates to DMA mappings.

Not even all of x86, but AMD in particular, Intel does it yet another way. But it still is easier to take this into the core with a few overrides than duplicating all the code.

Quoted 2 lines

I'm also not sure about that whole business with ZONE_DMA and ARCH_ZONE_DMA_BITS...

ZONE_DMA usually (but not always) maps to 24-bits of address space, if it doesn't (I mostly through about s390 with it's odd 31-bits) the architecture can override it if it cares).

Quoted 8 lines

On ppc64, unless you enable swiotlb (which we only do currently on some embedded platforms), you have all of memory in ZONE_DMA.

[    0.000000] Zone ranges:
[    0.000000]   DMA      [mem 0x0000000000000000-0x0000001fffffffff]
[    0.000000]   DMA32    empty
[    0.000000]   Normal   empty
[    0.000000]   Device   empty

This is really weird. Why would you wire up ZONE_DMA like this? The general scheme that architectures should implement is:

ZONE_DMA: Any memory below a magic threshold that is lower than
32-bit. Only enabled if actually required (usually either 24-bit for ISA, or some other weird architecture specific value like 32-bit for S/390)
ZONE_DMA32: Memory <= 32-bit if the architecture supports more than
32-bits worth of physical address space. Should generally be enabled on all 64-bit architectures unless you have a very good reason not to.
ZONE_NORMAL: Everything above 32-bit not falling into HIGHMEM or
MOVEABLE.

#10 lore raw

Re: [PATCH 02/20] kernel/dma/direct: refine dma_direct_alloc zone selection

Quoted 23 lines · Christoph Hellwig
Quoted 7 lines · Benjamin Herrenschmidt
Quoted 2 lines · Christoph Hellwig

We need to take the DMA offset and encryption bit into account when selecting a zone. Add a helper that takes those into account and use it.

That whole "encryption" stuff seems to be completely specific to the way x86 does memory encryption, or am I mistaken ? It's not clear to me what that does in practice and how it relates to DMA mappings.

Not even all of x86, but AMD in particular, Intel does it yet another way. But it still is easier to take this into the core with a few overrides than duplicating all the code.

Quoted 2 lines

I'm also not sure about that whole business with ZONE_DMA and ARCH_ZONE_DMA_BITS...

ZONE_DMA usually (but not always) maps to 24-bits of address space, if it doesn't (I mostly through about s390 with it's odd 31-bits) the architecture can override it if it cares).

Quoted 3 lines

On ppc64, unless you enable swiotlb (which we only do currently on some embedded platforms), you have all of memory in ZONE_DMA.

Quoted 7 lines
Quoted 5 lines
[    0.000000] Zone ranges:
[    0.000000]   DMA      [mem 0x0000000000000000-0x0000001fffffffff]
[    0.000000]   DMA32    empty
[    0.000000]   Normal   empty
[    0.000000]   Device   empty

This is really weird. Why would you wire up ZONE_DMA like this?

We always did :-) It predates my involvement and I think it predates even Pauls. It's quite silly actually since the first powerpc machines actually had ISA devices in them, but that's how it's been for ever. I suppose we could change it but that would mean digging out some old stuff to test.

Quoted 6 lines

The general scheme that architectures should implement is:

ZONE_DMA: Any memory below a magic threshold that is lower than
32-bit. Only enabled if actually required (usually either 24-bit for ISA, or some other weird architecture specific value like 32-bit for S/390)

It should have been ZONE_ISA_DMA :-)

Quoted 4 lines

ZONE_DMA32: Memory <= 32-bit if the architecture supports more than
32-bits worth of physical address space. Should generally be enabled on all 64-bit architectures unless you have a very good reason not to.

Yeah so we sort-of enable the config option but only populate the zone on platforms using swiotlb (freescale stuff). It's a bit messy at the moment I must admit.

Quoted 2 lines

ZONE_NORMAL: Everything above 32-bit not falling into HIGHMEM or
MOVEABLE.

Cheers,
Ben.

#11 lore raw

Re: [PATCH 02/20] kernel/dma/direct: refine dma_direct_alloc zone selection

Quoted 8 lines · Benjamin Herrenschmidt
Quoted 6 lines

The general scheme that architectures should implement is:

ZONE_DMA: Any memory below a magic threshold that is lower than
32-bit. Only enabled if actually required (usually either 24-bit for ISA, or some other weird architecture specific value like 32-bit for S/390)

It should have been ZONE_ISA_DMA :-)

For most of these use cases it should have been indeed, and that would avoid a lot of confusion where people use GFP_DMA just because they do DMA.

Anyway, switching powerpc to this scheme would be great, but I don't think it is required - GFP_KERNEL will silently fall back to ZONE_DMA, so except for an additional GFP_DMA fallback allocation when the GFP_KERNEL one fails the code should just work.

#12 lore raw

PATCH 3/20 dma-mapping: make the get_required_mask method available unconditionally

In reply to Christoph Hellwig

This save some duplication for ia64. In the long run this method will need some additional work including moving over to kernel/dma, but that will require some additional prep work, so let's do this minimal change for now.

---
 drivers/base/platform.c     | 11 ++++++++++-
 include/linux/dma-mapping.h |  2 --
 2 files changed, 10 insertions(+), 3 deletions(-)
drivers/base/platform.c +10 -1
index dff82a3c2caa..921ddb0c051b 100644
@@ -1180,7 +1180,7 @@ int __init platform_bus_init(void)
 }
 
 #ifndef ARCH_HAS_DMA_GET_REQUIRED_MASK
-u64 dma_get_required_mask(struct device *dev)
+static u64 default_dma_get_required_mask(struct device *dev)
 {
 	u32 low_totalram = ((max_pfn - 1) << PAGE_SHIFT);
 	u32 high_totalram = ((max_pfn - 1) >> (32 - PAGE_SHIFT));
@@ -1198,6 +1198,15 @@ u64 dma_get_required_mask(struct device *dev)
 	}
 	return mask;
 }
+
+u64 dma_get_required_mask(struct device *dev)
+{
+	const struct dma_map_ops *ops = get_dma_ops(dev);
+
+	if (ops->get_required_mask)
+		return ops->get_required_mask(dev);
+	return default_dma_get_required_mask(dev);
+}
 EXPORT_SYMBOL_GPL(dma_get_required_mask);
 #endif
 
include/linux/dma-mapping.h +0 -2
index f9cc309507d9..00d3065e1510 100644
@@ -130,9 +130,7 @@ struct dma_map_ops {
 			enum dma_data_direction direction);
 	int (*mapping_error)(struct device *dev, dma_addr_t dma_addr);
 	int (*dma_supported)(struct device *dev, u64 mask);
-#ifdef ARCH_HAS_DMA_GET_REQUIRED_MASK
 	u64 (*get_required_mask)(struct device *dev);
-#endif
 };
 
 extern const struct dma_map_ops dma_direct_ops;
Signature · 1 line
2.18.0
#13 lore raw

PATCH 4/20 ia64: remove get_required_mask implementation

In reply to Christoph Hellwig

ia64 can use the generic implementation in general, and SN2 can just override it in the dma_map_ops now.

---
 arch/ia64/include/asm/dma-mapping.h  |  2 --
 arch/ia64/include/asm/machvec.h      |  7 -------
 arch/ia64/include/asm/machvec_init.h |  1 -
 arch/ia64/include/asm/machvec_sn2.h  |  2 --
 arch/ia64/pci/pci.c                  | 26 --------------------------
 arch/ia64/sn/pci/pci_dma.c           |  4 ++--
 6 files changed, 2 insertions(+), 40 deletions(-)
arch/ia64/include/asm/dma-mapping.h +0 -2
index 76e4d6632d68..522745ae67bb 100644
@@ -10,8 +10,6 @@
 #include <linux/scatterlist.h>
 #include <linux/dma-debug.h>
 
-#define ARCH_HAS_DMA_GET_REQUIRED_MASK
-
 extern const struct dma_map_ops *dma_ops;
 extern struct ia64_machine_vector ia64_mv;
 extern void set_iommu_machvec(void);
arch/ia64/include/asm/machvec.h +0 -7
index 267f4f170191..5133739966bc 100644
@@ -44,7 +44,6 @@ typedef void ia64_mv_kernel_launch_event_t(void);
 
 /* DMA-mapping interface: */
 typedef void ia64_mv_dma_init (void);
-typedef u64 ia64_mv_dma_get_required_mask (struct device *);
 typedef const struct dma_map_ops *ia64_mv_dma_get_ops(struct device *);
 
 /*
@@ -127,7 +126,6 @@ extern void machvec_tlb_migrate_finish (struct mm_struct *);
 #  define platform_global_tlb_purge	ia64_mv.global_tlb_purge
 #  define platform_tlb_migrate_finish	ia64_mv.tlb_migrate_finish
 #  define platform_dma_init		ia64_mv.dma_init
-#  define platform_dma_get_required_mask ia64_mv.dma_get_required_mask
 #  define platform_dma_get_ops		ia64_mv.dma_get_ops
 #  define platform_irq_to_vector	ia64_mv.irq_to_vector
 #  define platform_local_vector_to_irq	ia64_mv.local_vector_to_irq
@@ -171,7 +169,6 @@ struct ia64_machine_vector {
 	ia64_mv_global_tlb_purge_t *global_tlb_purge;
 	ia64_mv_tlb_migrate_finish_t *tlb_migrate_finish;
 	ia64_mv_dma_init *dma_init;
-	ia64_mv_dma_get_required_mask *dma_get_required_mask;
 	ia64_mv_dma_get_ops *dma_get_ops;
 	ia64_mv_irq_to_vector *irq_to_vector;
 	ia64_mv_local_vector_to_irq *local_vector_to_irq;
@@ -211,7 +208,6 @@ struct ia64_machine_vector {
 	platform_global_tlb_purge,		\
 	platform_tlb_migrate_finish,		\
 	platform_dma_init,			\
-	platform_dma_get_required_mask,		\
 	platform_dma_get_ops,			\
 	platform_irq_to_vector,			\
 	platform_local_vector_to_irq,		\
@@ -286,9 +282,6 @@ extern const struct dma_map_ops *dma_get_ops(struct device *);
 #ifndef platform_dma_get_ops
 # define platform_dma_get_ops		dma_get_ops
 #endif
-#ifndef platform_dma_get_required_mask
-# define  platform_dma_get_required_mask	ia64_dma_get_required_mask
-#endif
 #ifndef platform_irq_to_vector
 # define platform_irq_to_vector		__ia64_irq_to_vector
 #endif
arch/ia64/include/asm/machvec_init.h +0 -1
index 2b32fd06b7c6..2aafb69a3787 100644
@@ -4,7 +4,6 @@
 
 extern ia64_mv_send_ipi_t ia64_send_ipi;
 extern ia64_mv_global_tlb_purge_t ia64_global_tlb_purge;
-extern ia64_mv_dma_get_required_mask ia64_dma_get_required_mask;
 extern ia64_mv_irq_to_vector __ia64_irq_to_vector;
 extern ia64_mv_local_vector_to_irq __ia64_local_vector_to_irq;
 extern ia64_mv_pci_get_legacy_mem_t ia64_pci_get_legacy_mem;
arch/ia64/include/asm/machvec_sn2.h +0 -2
index ece9fa85be88..b5153d300289 100644
@@ -55,7 +55,6 @@ extern ia64_mv_readb_t __sn_readb_relaxed;
 extern ia64_mv_readw_t __sn_readw_relaxed;
 extern ia64_mv_readl_t __sn_readl_relaxed;
 extern ia64_mv_readq_t __sn_readq_relaxed;
-extern ia64_mv_dma_get_required_mask	sn_dma_get_required_mask;
 extern ia64_mv_dma_init			sn_dma_init;
 extern ia64_mv_migrate_t		sn_migrate;
 extern ia64_mv_kernel_launch_event_t	sn_kernel_launch_event;
@@ -100,7 +99,6 @@ extern ia64_mv_pci_fixup_bus_t		sn_pci_fixup_bus;
 #define platform_pci_get_legacy_mem	sn_pci_get_legacy_mem
 #define platform_pci_legacy_read	sn_pci_legacy_read
 #define platform_pci_legacy_write	sn_pci_legacy_write
-#define platform_dma_get_required_mask	sn_dma_get_required_mask
 #define platform_dma_init		sn_dma_init
 #define platform_migrate		sn_migrate
 #define platform_kernel_launch_event    sn_kernel_launch_event
arch/ia64/pci/pci.c +0 -26
index 7ccc64d5fe3e..5d71800df431 100644
@@ -568,32 +568,6 @@ static void __init set_pci_dfl_cacheline_size(void)
 	pci_dfl_cache_line_size = (1 << cci.pcci_line_size) / 4;
 }
 
-u64 ia64_dma_get_required_mask(struct device *dev)
-{
-	u32 low_totalram = ((max_pfn - 1) << PAGE_SHIFT);
-	u32 high_totalram = ((max_pfn - 1) >> (32 - PAGE_SHIFT));
-	u64 mask;
-
-	if (!high_totalram) {
-		/* convert to mask just covering totalram */
-		low_totalram = (1 << (fls(low_totalram) - 1));
-		low_totalram += low_totalram - 1;
-		mask = low_totalram;
-	} else {
-		high_totalram = (1 << (fls(high_totalram) - 1));
-		high_totalram += high_totalram - 1;
-		mask = (((u64)high_totalram) << 32) + 0xffffffff;
-	}
-	return mask;
-}
-EXPORT_SYMBOL_GPL(ia64_dma_get_required_mask);
-
-u64 dma_get_required_mask(struct device *dev)
-{
-	return platform_dma_get_required_mask(dev);
-}
-EXPORT_SYMBOL_GPL(dma_get_required_mask);
-
 static int __init pcibios_init(void)
 {
 	set_pci_dfl_cacheline_size();
arch/ia64/sn/pci/pci_dma.c +2 -2
index 74c934a997bb..96eb2567718a 100644
@@ -344,11 +344,10 @@ static int sn_dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
 	return 0;
 }
 
-u64 sn_dma_get_required_mask(struct device *dev)
+static u64 sn_dma_get_required_mask(struct device *dev)
 {
 	return DMA_BIT_MASK(64);
 }
-EXPORT_SYMBOL_GPL(sn_dma_get_required_mask);
 
 char *sn_pci_get_legacy_mem(struct pci_bus *bus)
 {
@@ -473,6 +472,7 @@ static struct dma_map_ops sn_dma_ops = {
 	.sync_sg_for_device	= sn_dma_sync_sg_for_device,
 	.mapping_error		= sn_dma_mapping_error,
 	.dma_supported		= sn_dma_supported,
+	.get_required_mask	= sn_dma_get_required_mask,
 };
 
 void sn_dma_init(void)
Signature · 1 line
2.18.0
#14 lore raw

PATCH 5/20 swiotlb: allow the architecture to provide a get_required_mask hook

In reply to Christoph Hellwig

For now this allows consolidating the powerpc code. In the long run we should grow a generic implementation of dma_get_required_mask that returns the dma mask required to avoid bounce buffering.

---
 kernel/dma/swiotlb.c | 4 ++++
 1 file changed, 4 insertions(+)
kernel/dma/swiotlb.c +4 -0
index 904541055792..1bb420244753 100644
@@ -1084,5 +1084,9 @@ const struct dma_map_ops swiotlb_dma_ops = {
 	.map_page		= swiotlb_map_page,
 	.unmap_page		= swiotlb_unmap_page,
 	.dma_supported		= dma_direct_supported,
+#ifdef swiotlb_get_required_mask
+	.get_required_mask	= swiotlb_get_required_mask,
+#endif
+
 };
 EXPORT_SYMBOL(swiotlb_dma_ops);
Signature · 1 line
2.18.0
Reviewed-by on v1 5/20
#15 lore raw

Re: [PATCH 05/20] swiotlb: allow the architecture to provide a get_required_mask hook

Quoted 5 lines · Christoph Hellwig

For now this allows consolidating the powerpc code. In the long run we should grow a generic implementation of dma_get_required_mask that returns the dma mask required to avoid bounce buffering.

Thank you!

Quoted 21 lines
---
 kernel/dma/swiotlb.c | 4 ++++
 1 file changed, 4 insertions(+)
kernel/dma/swiotlb.c +4 -0
index 904541055792..1bb420244753 100644
@@ -1084,5 +1084,9 @@ const struct dma_map_ops swiotlb_dma_ops = {
 	.map_page		= swiotlb_map_page,
 	.unmap_page		= swiotlb_unmap_page,
 	.dma_supported		= dma_direct_supported,
+#ifdef swiotlb_get_required_mask
+	.get_required_mask	= swiotlb_get_required_mask,
+#endif
+
 };
 EXPORT_SYMBOL(swiotlb_dma_ops);
Signature · 1 line
2.18.0
#16 lore raw

PATCH 6/20 dma-noncoherent: add an optional arch hook for ->get_required_mask

In reply to Christoph Hellwig

This is need for powerpc for now. Hopefully we can come up with a clean generic implementation mid-term.

---
 include/linux/dma-noncoherent.h | 6 ++++++
 kernel/dma/Kconfig              | 4 ++++
 kernel/dma/noncoherent.c        | 1 +
 3 files changed, 11 insertions(+)
include/linux/dma-noncoherent.h +6 -0
index 10b2654d549b..61394c6e56df 100644
@@ -17,6 +17,12 @@ int arch_dma_mmap(struct device *dev, struct vm_area_struct *vma,
 #define arch_dma_mmap NULL
 #endif /* CONFIG_DMA_NONCOHERENT_MMAP */
 
+#ifdef CONFIG_DMA_NONCOHERENT_GET_REQUIRED
+u64 arch_get_required_mask(struct device *dev);
+#else
+#define arch_get_required_mask NULL
+#endif
+
 #ifdef CONFIG_DMA_NONCOHERENT_CACHE_SYNC
 void arch_dma_cache_sync(struct device *dev, void *vaddr, size_t size,
 		enum dma_data_direction direction);
kernel/dma/Kconfig +4 -0
index 9bd54304446f..b523104d6199 100644
@@ -36,6 +36,10 @@ config DMA_NONCOHERENT_MMAP
 	bool
 	depends on DMA_NONCOHERENT_OPS
 
+config DMA_NONCOHERENT_GET_REQUIRED
+	bool
+	depends on DMA_NONCOHERENT_OPS
+
 config DMA_NONCOHERENT_CACHE_SYNC
 	bool
 	depends on DMA_NONCOHERENT_OPS
kernel/dma/noncoherent.c +1 -0
index 79e9a757387f..cf4ffbe4a09d 100644
@@ -98,5 +98,6 @@ const struct dma_map_ops dma_noncoherent_ops = {
 	.dma_supported		= dma_direct_supported,
 	.mapping_error		= dma_direct_mapping_error,
 	.cache_sync		= arch_dma_cache_sync,
+	.get_required_mask	= arch_get_required_mask,
 };
 EXPORT_SYMBOL(dma_noncoherent_ops);
Signature · 1 line
2.18.0
#17 lore raw

PATCH 7/20 powerpc/dma: remove the unused ARCH_HAS_DMA_MMAP_COHERENT define

In reply to Christoph Hellwig
---
 arch/powerpc/include/asm/dma-mapping.h | 2 --
 1 file changed, 2 deletions(-)
arch/powerpc/include/asm/dma-mapping.h +0 -2
index 8fa394520af6..f2a4a7142b1e 100644
@@ -112,7 +112,5 @@ extern int dma_set_mask(struct device *dev, u64 dma_mask);
 
 extern u64 __dma_get_required_mask(struct device *dev);
 
-#define ARCH_HAS_DMA_MMAP_COHERENT
-
 #endif /* __KERNEL__ */
 #endif	/* _ASM_DMA_MAPPING_H */
Signature · 1 line
2.18.0
Acked-by on v1 7/20
#18 lore raw

Re: [PATCH 07/20] powerpc/dma: remove the unused ARCH_HAS_DMA_MMAP_COHERENT define

Quoted 1 line · Christoph Hellwig
Quoted 16 lines
---
 arch/powerpc/include/asm/dma-mapping.h | 2 --
 1 file changed, 2 deletions(-)
arch/powerpc/include/asm/dma-mapping.h +0 -2
index 8fa394520af6..f2a4a7142b1e 100644
@@ -112,7 +112,5 @@ extern int dma_set_mask(struct device *dev, u64 dma_mask);
 
 extern u64 __dma_get_required_mask(struct device *dev);
 
-#define ARCH_HAS_DMA_MMAP_COHERENT
-
 #endif /* __KERNEL__ */
 #endif	/* _ASM_DMA_MAPPING_H */
#19 lore raw

PATCH 8/20 powerpc/dma: remove the unused dma_nommu_ops export

In reply to Christoph Hellwig
---
 arch/powerpc/kernel/dma.c | 1 -
 1 file changed, 1 deletion(-)
arch/powerpc/kernel/dma.c +0 -1
index dbfc7056d7df..3939589aab04 100644
@@ -286,7 +286,6 @@ const struct dma_map_ops dma_nommu_ops = {
 	.sync_sg_for_device 		= dma_nommu_sync_sg,
 #endif
 };
-EXPORT_SYMBOL(dma_nommu_ops);
 
 int dma_set_coherent_mask(struct device *dev, u64 mask)
 {
Signature · 1 line
2.18.0
#20 lore raw

Re: [PATCH 08/20] powerpc/dma: remove the unused dma_nommu_ops export

It turns out cxl actually uses it. So for now skip this patch, although random code in drivers messing with dma ops will need to be sorted out sooner or later.

#21 lore raw

Re: [PATCH 08/20] powerpc/dma: remove the unused dma_nommu_ops export

Quoted 3 lines · Christoph Hellwig

It turns out cxl actually uses it. So for now skip this patch, although random code in drivers messing with dma ops will need to be sorted out sooner or later.

CXL devices are "special", they bypass the classic iommu in favor of allowing the device to operate using the main processor page tables using an MMU context (so basically the device can use userspace addresses directly), akin to ATS.

I think the code currently uses the nommu ops as a way to do a simple kernel mapping for kernel drivers using CXL (not userspace stuff) though.

Ben.

#22 lore raw

Re: [PATCH 08/20] powerpc/dma: remove the unused dma_nommu_ops export

Quoted 5 lines · Benjamin Herrenschmidt
Quoted 3 lines · Christoph Hellwig

It turns out cxl actually uses it. So for now skip this patch, although random code in drivers messing with dma ops will need to be sorted out sooner or later.

Quoted 8 lines

CXL devices are "special", they bypass the classic iommu in favor of allowing the device to operate using the main processor page tables using an MMU context (so basically the device can use userspace addresses directly), akin to ATS.

I think the code currently uses the nommu ops as a way to do a simple kernel mapping for kernel drivers using CXL (not userspace stuff) though.

Its still a horrible idea to have this in drivers/, we need some core API to mediate this behavior. Also if the device supports using virtual addresses dma_nommu_ops seems wrong as it won't do the right thing for e.g. vmalloc addresses not mapped into the kernel linear mapping (which I guess can't currently happen on powerpc, but still..)

#23 lore raw

Re: [PATCH 08/20] powerpc/dma: remove the unused dma_nommu_ops export

Quoted 15 lines · Christoph Hellwig
Quoted 5 lines · Benjamin Herrenschmidt
Quoted 3 lines · Christoph Hellwig

It turns out cxl actually uses it. So for now skip this patch, although random code in drivers messing with dma ops will need to be sorted out sooner or later.

Quoted 8 lines

CXL devices are "special", they bypass the classic iommu in favor of allowing the device to operate using the main processor page tables using an MMU context (so basically the device can use userspace addresses directly), akin to ATS.

I think the code currently uses the nommu ops as a way to do a simple kernel mapping for kernel drivers using CXL (not userspace stuff) though.

Quoted 6 lines

Its still a horrible idea to have this in drivers/, we need some core API to mediate this behavior. Also if the device supports using virtual addresses dma_nommu_ops seems wrong as it won't do the right thing for e.g. vmalloc addresses not mapped into the kernel linear mapping (which I guess can't currently happen on powerpc, but still..)

You are right it won't do the right thing, but neither will standard DMA ops, will they ? Drivers know not to try to dma_map vmalloc addresses without first getting the underlying page, nothing unusal there.

Yes I agree having this in drivers somewhat sucks though.

Cheers,
Ben.

#24 lore raw

PATCH 9/20 powerpc/dma: remove the unused ISA_DMA_THRESHOLD export

In reply to Christoph Hellwig
---
 arch/powerpc/kernel/setup_32.c | 1 -
 1 file changed, 1 deletion(-)
arch/powerpc/kernel/setup_32.c +0 -1
index 74457485574b..3c2d093f74c7 100644
@@ -55,7 +55,6 @@ unsigned long ISA_DMA_THRESHOLD;
 unsigned int DMA_MODE_READ;
 unsigned int DMA_MODE_WRITE;
 
-EXPORT_SYMBOL(ISA_DMA_THRESHOLD);
 EXPORT_SYMBOL(DMA_MODE_READ);
 EXPORT_SYMBOL(DMA_MODE_WRITE);
 
Signature · 1 line
2.18.0
Acked-by on v1 9/20
#25 lore raw

Re: [PATCH 09/20] powerpc/dma: remove the unused ISA_DMA_THRESHOLD export

Quoted 1 line · Christoph Hellwig
Quoted 16 lines
---
 arch/powerpc/kernel/setup_32.c | 1 -
 1 file changed, 1 deletion(-)
arch/powerpc/kernel/setup_32.c +0 -1
index 74457485574b..3c2d093f74c7 100644
@@ -55,7 +55,6 @@ unsigned long ISA_DMA_THRESHOLD;
 unsigned int DMA_MODE_READ;
 unsigned int DMA_MODE_WRITE;
 
-EXPORT_SYMBOL(ISA_DMA_THRESHOLD);
 EXPORT_SYMBOL(DMA_MODE_READ);
 EXPORT_SYMBOL(DMA_MODE_WRITE);
 fooBenjam
#26 lore raw

PATCH 10/20 powerpc/dma-noncoherent: don't disable irqs over kmap_atomic

In reply to Christoph Hellwig

The requirement to disable local irqs over kmap_atomic is long gone, so remove those calls.

---
 arch/powerpc/mm/dma-noncoherent.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
arch/powerpc/mm/dma-noncoherent.c +1 -5
index 382528475433..d1c16456abac 100644
@@ -357,12 +357,10 @@ static inline void __dma_sync_page_highmem(struct page *page,
 {
 	size_t seg_size = min((size_t)(PAGE_SIZE - offset), size);
 	size_t cur_size = seg_size;
-	unsigned long flags, start, seg_offset = offset;
+	unsigned long start, seg_offset = offset;
 	int nr_segs = 1 + ((size - seg_size) + PAGE_SIZE - 1)/PAGE_SIZE;
 	int seg_nr = 0;
 
-	local_irq_save(flags);
-
 	do {
 		start = (unsigned long)kmap_atomic(page + seg_nr) + seg_offset;
 
@@ -378,8 +376,6 @@ static inline void __dma_sync_page_highmem(struct page *page,
 		cur_size += seg_size;
 		seg_offset = 0;
 	} while (seg_nr < nr_segs);
-
-	local_irq_restore(flags);
 }
 #endif /* CONFIG_HIGHMEM */
 
Signature · 1 line
2.18.0
#27 lore raw

Re: [PATCH 10/20] powerpc/dma-noncoherent: don't disable irqs over kmap_atomic

Quoted 2 lines · Christoph Hellwig

The requirement to disable local irqs over kmap_atomic is long gone, so remove those calls.

Really ? I'm trying to verify that and getting lost in a mess of macros from hell in the per-cpu stuff but if you look at our implementation of kmap_atomic_prot(), all it does is a preempt_disable(), and then it uses kmap_atomic_idx_push():

	int idx = __this_cpu_inc_return(__kmap_atomic_idx) - 1;

Note the use of __this_cpu_inc_return(), not this_cpu_inc_return(), ie this is the non-interrupt safe version...

Ben.

Quoted 32 lines
---
 arch/powerpc/mm/dma-noncoherent.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
arch/powerpc/mm/dma-noncoherent.c +1 -5
index 382528475433..d1c16456abac 100644
@@ -357,12 +357,10 @@ static inline void __dma_sync_page_highmem(struct page *page,
 {
 	size_t seg_size = min((size_t)(PAGE_SIZE - offset), size);
 	size_t cur_size = seg_size;
-	unsigned long flags, start, seg_offset = offset;
+	unsigned long start, seg_offset = offset;
 	int nr_segs = 1 + ((size - seg_size) + PAGE_SIZE - 1)/PAGE_SIZE;
 	int seg_nr = 0;
 
-	local_irq_save(flags);
-
 	do {
 		start = (unsigned long)kmap_atomic(page + seg_nr) + seg_offset;
 
@@ -378,8 +376,6 @@ static inline void __dma_sync_page_highmem(struct page *page,
 		cur_size += seg_size;
 		seg_offset = 0;
 	} while (seg_nr < nr_segs);
-
-	local_irq_restore(flags);
 }
 #endif /* CONFIG_HIGHMEM */
 
#28 lore raw

Re: [PATCH 10/20] powerpc/dma-noncoherent: don't disable irqs over kmap_atomic

Quoted 9 lines · Benjamin Herrenschmidt
Quoted 2 lines · Christoph Hellwig

The requirement to disable local irqs over kmap_atomic is long gone, so remove those calls.

Really ? I'm trying to verify that and getting lost in a mess of macros from hell in the per-cpu stuff but if you look at our implementation of kmap_atomic_prot(), all it does is a preempt_disable(), and then it uses kmap_atomic_idx_push():

Quoted 4 lines
	int idx = __this_cpu_inc_return(__kmap_atomic_idx) - 1;

Note the use of __this_cpu_inc_return(), not this_cpu_inc_return(), ie this is the non-interrupt safe version...

Looks like the powerpc variant indeed isn't save.

I did look a bit more through the code and history, and it seems like we remove the need to disable irqs when called from process context a while ago, but we still require disabling irqs when called from irq context. Given that this code can also be called from irq context we'll have to keep the local_irq_save.

#29 lore raw

Re: [PATCH 10/20] powerpc/dma-noncoherent: don't disable irqs over kmap_atomic

Quoted 15 lines · Christoph Hellwig
Quoted 9 lines · Benjamin Herrenschmidt
Quoted 2 lines · Christoph Hellwig

The requirement to disable local irqs over kmap_atomic is long gone, so remove those calls.

Really ? I'm trying to verify that and getting lost in a mess of macros from hell in the per-cpu stuff but if you look at our implementation of kmap_atomic_prot(), all it does is a preempt_disable(), and then it uses kmap_atomic_idx_push():

Quoted 4 lines
	int idx = __this_cpu_inc_return(__kmap_atomic_idx) - 1;

Note the use of __this_cpu_inc_return(), not this_cpu_inc_return(), ie this is the non-interrupt safe version...

Quoted 7 lines

Looks like the powerpc variant indeed isn't save.

I did look a bit more through the code and history, and it seems like we remove the need to disable irqs when called from process context a while ago, but we still require disabling irqs when called from irq context. Given that this code can also be called from irq context we'll have to keep the local_irq_save.

This is the same with x86 no ?

32-bit x86 kmap_atomic_prot is the same as ours...

In fact I wonder why the preempt_disable() in there since it needs to be protected against interrupt ?

Or is it that we never actually call kmap_atomic_* these days from interrupt, and the atomic versions are just about dealing with spinlocks ?

Cheers,
Ben.

#30 lore raw

PATCH 11/20 powerpc/dma: split the two __dma_alloc_coherent implementations

In reply to Christoph Hellwig

The implemementation for the CONFIG_NOT_COHERENT_CACHE case doesn't share any code with the one for systems with coherent caches. Split it off and merge it with the helpers in dma-noncoherent.c that have no other callers.

---
 arch/powerpc/include/asm/dma-mapping.h |  5 -----
 arch/powerpc/kernel/dma.c              | 14 ++------------
 arch/powerpc/mm/dma-noncoherent.c      | 15 +++++++--------
 arch/powerpc/platforms/44x/warp.c      |  2 +-
 4 files changed, 10 insertions(+), 26 deletions(-)
arch/powerpc/include/asm/dma-mapping.h +0 -5
index f2a4a7142b1e..dacd0f93f2b2 100644
@@ -39,9 +39,6 @@ extern int dma_nommu_mmap_coherent(struct device *dev,
  * to ensure it is consistent.
  */
 struct device;
-extern void *__dma_alloc_coherent(struct device *dev, size_t size,
-				  dma_addr_t *handle, gfp_t gfp);
-extern void __dma_free_coherent(size_t size, void *vaddr);
 extern void __dma_sync(void *vaddr, size_t size, int direction);
 extern void __dma_sync_page(struct page *page, unsigned long offset,
 				 size_t size, int direction);
@@ -52,8 +49,6 @@ extern unsigned long __dma_get_coherent_pfn(unsigned long cpu_addr);
  * Cache coherent cores.
  */
 
-#define __dma_alloc_coherent(dev, gfp, size, handle)	NULL
-#define __dma_free_coherent(size, addr)		((void)0)
 #define __dma_sync(addr, size, rw)		((void)0)
 #define __dma_sync_page(pg, off, sz, rw)	((void)0)
 
arch/powerpc/kernel/dma.c +2 -12
index 3939589aab04..eceaa92e6986 100644
@@ -62,18 +62,12 @@ static int dma_nommu_dma_supported(struct device *dev, u64 mask)
 #endif
 }
 
+#ifndef CONFIG_NOT_COHERENT_CACHE
 void *__dma_nommu_alloc_coherent(struct device *dev, size_t size,
 				  dma_addr_t *dma_handle, gfp_t flag,
 				  unsigned long attrs)
 {
 	void *ret;
-#ifdef CONFIG_NOT_COHERENT_CACHE
-	ret = __dma_alloc_coherent(dev, size, dma_handle, flag);
-	if (ret = NULL)
-		return NULL;
-	*dma_handle += get_dma_offset(dev);
-	return ret;
-#else
 	struct page *page;
 	int node = dev_to_node(dev);
 #ifdef CONFIG_FSL_SOC
@@ -113,19 +107,15 @@ void *__dma_nommu_alloc_coherent(struct device *dev, size_t size,
 	*dma_handle = __pa(ret) + get_dma_offset(dev);
 
 	return ret;
-#endif
 }
 
 void __dma_nommu_free_coherent(struct device *dev, size_t size,
 				void *vaddr, dma_addr_t dma_handle,
 				unsigned long attrs)
 {
-#ifdef CONFIG_NOT_COHERENT_CACHE
-	__dma_free_coherent(size, vaddr);
-#else
 	free_pages((unsigned long)vaddr, get_order(size));
-#endif
 }
+#endif /* !CONFIG_NOT_COHERENT_CACHE */
 
 static void *dma_nommu_alloc_coherent(struct device *dev, size_t size,
 				       dma_addr_t *dma_handle, gfp_t flag,
arch/powerpc/mm/dma-noncoherent.c +7 -8
index d1c16456abac..cfc48a253707 100644
@@ -29,7 +29,7 @@
 #include <linux/string.h>
 #include <linux/types.h>
 #include <linux/highmem.h>
-#include <linux/dma-mapping.h>
+#include <linux/dma-direct.h>
 #include <linux/export.h>
 
 #include <asm/tlbflush.h>
@@ -151,8 +151,8 @@ static struct ppc_vm_region *ppc_vm_region_find(struct ppc_vm_region *head, unsi
  * Allocate DMA-coherent memory space and return both the kernel remapped
  * virtual and bus address for that space.
  */
-void *
-__dma_alloc_coherent(struct device *dev, size_t size, dma_addr_t *handle, gfp_t gfp)
+void *__dma_nommu_alloc_coherent(struct device *dev, size_t size,
+		dma_addr_t *dma_handle, gfp_t gfp, unsigned long attrs)
 {
 	struct page *page;
 	struct ppc_vm_region *c;
@@ -223,7 +223,7 @@ __dma_alloc_coherent(struct device *dev, size_t size, dma_addr_t *handle, gfp_t
 		/*
 		 * Set the "dma handle"
 		 */
-		*handle = page_to_phys(page);
+		*dma_handle = phys_to_dma(dev, page_to_phys(page));
 
 		do {
 			SetPageReserved(page);
@@ -249,12 +249,12 @@ __dma_alloc_coherent(struct device *dev, size_t size, dma_addr_t *handle, gfp_t
  no_page:
 	return NULL;
 }
-EXPORT_SYMBOL(__dma_alloc_coherent);
 
 /*
  * free a page as defined by the above mapping.
  */
-void __dma_free_coherent(size_t size, void *vaddr)
+void __dma_nommu_free_coherent(struct device *dev, size_t size, void *vaddr,
+		dma_addr_t dma_handle, unsigned long attrs)
 {
 	struct ppc_vm_region *c;
 	unsigned long flags, addr;
@@ -309,7 +309,6 @@ void __dma_free_coherent(size_t size, void *vaddr)
 	       __func__, vaddr);
 	dump_stack();
 }
-EXPORT_SYMBOL(__dma_free_coherent);
 
 /*
  * make an area consistent.
@@ -397,7 +396,7 @@ EXPORT_SYMBOL(__dma_sync_page);
 
 /*
  * Return the PFN for a given cpu virtual address returned by
- * __dma_alloc_coherent. This is used by dma_mmap_coherent()
+ * __dma_nommu_alloc_coherent. This is used by dma_mmap_coherent()
  */
 unsigned long __dma_get_coherent_pfn(unsigned long cpu_addr)
 {
arch/powerpc/platforms/44x/warp.c +1 -1
index a886c2c22097..7e4f8ca19ce8 100644
@@ -47,7 +47,7 @@ static int __init warp_probe(void)
 	if (!of_machine_is_compatible("pika,warp"))
 		return 0;
 
-	/* For __dma_alloc_coherent */
+	/* For __dma_nommu_alloc_coherent */
 	ISA_DMA_THRESHOLD = ~0L;
 
 	return 1;
Signature · 1 line
2.18.0
Acked-by on v1 11/20
#31 lore raw

Re: [PATCH 11/20] powerpc/dma: split the two __dma_alloc_coherent implementations

Quoted 6 lines · Christoph Hellwig

The implemementation for the CONFIG_NOT_COHERENT_CACHE case doesn't share any code with the one for systems with coherent caches. Split it off and merge it with the helpers in dma-noncoherent.c that have no other callers.

Quoted 153 lines
---
 arch/powerpc/include/asm/dma-mapping.h |  5 -----
 arch/powerpc/kernel/dma.c              | 14 ++------------
 arch/powerpc/mm/dma-noncoherent.c      | 15 +++++++--------
 arch/powerpc/platforms/44x/warp.c      |  2 +-
 4 files changed, 10 insertions(+), 26 deletions(-)
arch/powerpc/include/asm/dma-mapping.h +0 -5
index f2a4a7142b1e..dacd0f93f2b2 100644
@@ -39,9 +39,6 @@ extern int dma_nommu_mmap_coherent(struct device *dev,
  * to ensure it is consistent.
  */
 struct device;
-extern void *__dma_alloc_coherent(struct device *dev, size_t size,
-				  dma_addr_t *handle, gfp_t gfp);
-extern void __dma_free_coherent(size_t size, void *vaddr);
 extern void __dma_sync(void *vaddr, size_t size, int direction);
 extern void __dma_sync_page(struct page *page, unsigned long offset,
 				 size_t size, int direction);
@@ -52,8 +49,6 @@ extern unsigned long __dma_get_coherent_pfn(unsigned long cpu_addr);
  * Cache coherent cores.
  */
 
-#define __dma_alloc_coherent(dev, gfp, size, handle)	NULL
-#define __dma_free_coherent(size, addr)		((void)0)
 #define __dma_sync(addr, size, rw)		((void)0)
 #define __dma_sync_page(pg, off, sz, rw)	((void)0)
 
arch/powerpc/kernel/dma.c +2 -12
index 3939589aab04..eceaa92e6986 100644
@@ -62,18 +62,12 @@ static int dma_nommu_dma_supported(struct device *dev, u64 mask)
 #endif
 }
 
+#ifndef CONFIG_NOT_COHERENT_CACHE
 void *__dma_nommu_alloc_coherent(struct device *dev, size_t size,
 				  dma_addr_t *dma_handle, gfp_t flag,
 				  unsigned long attrs)
 {
 	void *ret;
-#ifdef CONFIG_NOT_COHERENT_CACHE
-	ret = __dma_alloc_coherent(dev, size, dma_handle, flag);
-	if (ret = NULL)
-		return NULL;
-	*dma_handle += get_dma_offset(dev);
-	return ret;
-#else
 	struct page *page;
 	int node = dev_to_node(dev);
 #ifdef CONFIG_FSL_SOC
@@ -113,19 +107,15 @@ void *__dma_nommu_alloc_coherent(struct device *dev, size_t size,
 	*dma_handle = __pa(ret) + get_dma_offset(dev);
 
 	return ret;
-#endif
 }
 
 void __dma_nommu_free_coherent(struct device *dev, size_t size,
 				void *vaddr, dma_addr_t dma_handle,
 				unsigned long attrs)
 {
-#ifdef CONFIG_NOT_COHERENT_CACHE
-	__dma_free_coherent(size, vaddr);
-#else
 	free_pages((unsigned long)vaddr, get_order(size));
-#endif
 }
+#endif /* !CONFIG_NOT_COHERENT_CACHE */
 
 static void *dma_nommu_alloc_coherent(struct device *dev, size_t size,
 				       dma_addr_t *dma_handle, gfp_t flag,
arch/powerpc/mm/dma-noncoherent.c +7 -8
index d1c16456abac..cfc48a253707 100644
@@ -29,7 +29,7 @@
 #include <linux/string.h>
 #include <linux/types.h>
 #include <linux/highmem.h>
-#include <linux/dma-mapping.h>
+#include <linux/dma-direct.h>
 #include <linux/export.h>
 
 #include <asm/tlbflush.h>
@@ -151,8 +151,8 @@ static struct ppc_vm_region *ppc_vm_region_find(struct ppc_vm_region *head, unsi
  * Allocate DMA-coherent memory space and return both the kernel remapped
  * virtual and bus address for that space.
  */
-void *
-__dma_alloc_coherent(struct device *dev, size_t size, dma_addr_t *handle, gfp_t gfp)
+void *__dma_nommu_alloc_coherent(struct device *dev, size_t size,
+		dma_addr_t *dma_handle, gfp_t gfp, unsigned long attrs)
 {
 	struct page *page;
 	struct ppc_vm_region *c;
@@ -223,7 +223,7 @@ __dma_alloc_coherent(struct device *dev, size_t size, dma_addr_t *handle, gfp_t
 		/*
 		 * Set the "dma handle"
 		 */
-		*handle = page_to_phys(page);
+		*dma_handle = phys_to_dma(dev, page_to_phys(page));
 
 		do {
 			SetPageReserved(page);
@@ -249,12 +249,12 @@ __dma_alloc_coherent(struct device *dev, size_t size, dma_addr_t *handle, gfp_t
  no_page:
 	return NULL;
 }
-EXPORT_SYMBOL(__dma_alloc_coherent);
 
 /*
  * free a page as defined by the above mapping.
  */
-void __dma_free_coherent(size_t size, void *vaddr)
+void __dma_nommu_free_coherent(struct device *dev, size_t size, void *vaddr,
+		dma_addr_t dma_handle, unsigned long attrs)
 {
 	struct ppc_vm_region *c;
 	unsigned long flags, addr;
@@ -309,7 +309,6 @@ void __dma_free_coherent(size_t size, void *vaddr)
 	       __func__, vaddr);
 	dump_stack();
 }
-EXPORT_SYMBOL(__dma_free_coherent);
 
 /*
  * make an area consistent.
@@ -397,7 +396,7 @@ EXPORT_SYMBOL(__dma_sync_page);
 
 /*
  * Return the PFN for a given cpu virtual address returned by
- * __dma_alloc_coherent. This is used by dma_mmap_coherent()
+ * __dma_nommu_alloc_coherent. This is used by dma_mmap_coherent()
  */
 unsigned long __dma_get_coherent_pfn(unsigned long cpu_addr)
 {
arch/powerpc/platforms/44x/warp.c +1 -1
index a886c2c22097..7e4f8ca19ce8 100644
@@ -47,7 +47,7 @@ static int __init warp_probe(void)
 	if (!of_machine_is_compatible("pika,warp"))
 		return 0;
 
-	/* For __dma_alloc_coherent */
+	/* For __dma_nommu_alloc_coherent */
 	ISA_DMA_THRESHOLD = ~0L;
 
 	return 1;
#32 lore raw

PATCH 12/20 powerpc/dma: use phys_to_dma instead of get_dma_offset

In reply to Christoph Hellwig

Use the standard portable helper instead of the powerpc specific one, which is about to go away.

---
 arch/powerpc/kernel/dma-swiotlb.c |  5 ++---
 arch/powerpc/kernel/dma.c         | 12 ++++++------
 2 files changed, 8 insertions(+), 9 deletions(-)
arch/powerpc/kernel/dma-swiotlb.c +2 -3
index 88f3963ca30f..f6e0701c5303 100644
@@ -11,7 +11,7 @@
  *
  */
 
-#include <linux/dma-mapping.h>
+#include <linux/dma-direct.h>
 #include <linux/memblock.h>
 #include <linux/pfn.h>
 #include <linux/of_platform.h>
@@ -31,9 +31,8 @@ static u64 swiotlb_powerpc_get_required(struct device *dev)
 	end = memblock_end_of_DRAM();
 	if (max_direct_dma_addr && end > max_direct_dma_addr)
 		end = max_direct_dma_addr;
-	end += get_dma_offset(dev);
 
-	mask = 1ULL << (fls64(end) - 1);
+	mask = 1ULL << (fls64(phys_to_dma(dev, end)) - 1);
 	mask += mask - 1;
 
 	return mask;
arch/powerpc/kernel/dma.c +6 -6
index eceaa92e6986..3487de83bb37 100644
@@ -6,7 +6,7 @@
  */
 
 #include <linux/device.h>
-#include <linux/dma-mapping.h>
+#include <linux/dma-direct.h>
 #include <linux/dma-debug.h>
 #include <linux/gfp.h>
 #include <linux/memblock.h>
@@ -43,7 +43,7 @@ static u64 __maybe_unused get_pfn_limit(struct device *dev)
 static int dma_nommu_dma_supported(struct device *dev, u64 mask)
 {
 #ifdef CONFIG_PPC64
-	u64 limit = get_dma_offset(dev) + (memblock_end_of_DRAM() - 1);
+	u64 limit = phys_to_dma(dev, (memblock_end_of_DRAM() - 1));
 
 	/* Limit fits in the mask, we are good */
 	if (mask >= limit)
@@ -104,7 +104,7 @@ void *__dma_nommu_alloc_coherent(struct device *dev, size_t size,
 		return NULL;
 	ret = page_address(page);
 	memset(ret, 0, size);
-	*dma_handle = __pa(ret) + get_dma_offset(dev);
+	*dma_handle = phys_to_dma(dev,__pa(ret));
 
 	return ret;
 }
@@ -188,7 +188,7 @@ static int dma_nommu_map_sg(struct device *dev, struct scatterlist *sgl,
 	int i;
 
 	for_each_sg(sgl, sg, nents, i) {
-		sg->dma_address = sg_phys(sg) + get_dma_offset(dev);
+		sg->dma_address = phys_to_dma(dev, sg_phys(sg));
 		sg->dma_length = sg->length;
 
 		if (attrs & DMA_ATTR_SKIP_CPU_SYNC)
@@ -210,7 +210,7 @@ static u64 dma_nommu_get_required_mask(struct device *dev)
 {
 	u64 end, mask;
 
-	end = memblock_end_of_DRAM() + get_dma_offset(dev);
+	end = phys_to_dma(dev, memblock_end_of_DRAM());
 
 	mask = 1ULL << (fls64(end) - 1);
 	mask += mask - 1;
@@ -228,7 +228,7 @@ static inline dma_addr_t dma_nommu_map_page(struct device *dev,
 	if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC))
 		__dma_sync_page(page, offset, size, dir);
 
-	return page_to_phys(page) + offset + get_dma_offset(dev);
+	return phys_to_dma(dev, page_to_phys(page)) + offset;
 }
 
 static inline void dma_nommu_unmap_page(struct device *dev,
Signature · 1 line
2.18.0
Acked-by on v1 12/20
#33 lore raw

Re: [PATCH 12/20] powerpc/dma: use phys_to_dma instead of get_dma_offset

Quoted 4 lines · Christoph Hellwig

Use the standard portable helper instead of the powerpc specific one, which is about to go away.

Quoted 87 lines
---
 arch/powerpc/kernel/dma-swiotlb.c |  5 ++---
 arch/powerpc/kernel/dma.c         | 12 ++++++------
 2 files changed, 8 insertions(+), 9 deletions(-)
arch/powerpc/kernel/dma-swiotlb.c +2 -3
index 88f3963ca30f..f6e0701c5303 100644
@@ -11,7 +11,7 @@
  *
  */
 
-#include <linux/dma-mapping.h>
+#include <linux/dma-direct.h>
 #include <linux/memblock.h>
 #include <linux/pfn.h>
 #include <linux/of_platform.h>
@@ -31,9 +31,8 @@ static u64 swiotlb_powerpc_get_required(struct device *dev)
        end = memblock_end_of_DRAM();
        if (max_direct_dma_addr && end > max_direct_dma_addr)
                end = max_direct_dma_addr;
-       end += get_dma_offset(dev);
 
-       mask = 1ULL << (fls64(end) - 1);
+       mask = 1ULL << (fls64(phys_to_dma(dev, end)) - 1);
        mask += mask - 1;
 
        return mask;
arch/powerpc/kernel/dma.c +6 -6
index eceaa92e6986..3487de83bb37 100644
@@ -6,7 +6,7 @@
  */
 
 #include <linux/device.h>
-#include <linux/dma-mapping.h>
+#include <linux/dma-direct.h>
 #include <linux/dma-debug.h>
 #include <linux/gfp.h>
 #include <linux/memblock.h>
@@ -43,7 +43,7 @@ static u64 __maybe_unused get_pfn_limit(struct device *dev)
 static int dma_nommu_dma_supported(struct device *dev, u64 mask)
 {
 #ifdef CONFIG_PPC64
-       u64 limit = get_dma_offset(dev) + (memblock_end_of_DRAM() - 1);
+       u64 limit = phys_to_dma(dev, (memblock_end_of_DRAM() - 1));
 
        /* Limit fits in the mask, we are good */
        if (mask >= limit)
@@ -104,7 +104,7 @@ void *__dma_nommu_alloc_coherent(struct device *dev, size_t size,
                return NULL;
        ret = page_address(page);
        memset(ret, 0, size);
-       *dma_handle = __pa(ret) + get_dma_offset(dev);
+       *dma_handle = phys_to_dma(dev,__pa(ret));
 
        return ret;
 }
@@ -188,7 +188,7 @@ static int dma_nommu_map_sg(struct device *dev, struct scatterlist *sgl,
        int i;
 
        for_each_sg(sgl, sg, nents, i) {
-               sg->dma_address = sg_phys(sg) + get_dma_offset(dev);
+               sg->dma_address = phys_to_dma(dev, sg_phys(sg));
                sg->dma_length = sg->length;
 
                if (attrs & DMA_ATTR_SKIP_CPU_SYNC)
@@ -210,7 +210,7 @@ static u64 dma_nommu_get_required_mask(struct device *dev)
 {
        u64 end, mask;
 
-       end = memblock_end_of_DRAM() + get_dma_offset(dev);
+       end = phys_to_dma(dev, memblock_end_of_DRAM());
 
        mask = 1ULL << (fls64(end) - 1);
        mask += mask - 1;
@@ -228,7 +228,7 @@ static inline dma_addr_t dma_nommu_map_page(struct device *dev,
        if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC))
                __dma_sync_page(page, offset, size, dir);
 
-       return page_to_phys(page) + offset + get_dma_offset(dev);
+       return phys_to_dma(dev, page_to_phys(page)) + offset;
 }
 
 static inline void dma_nommu_unmap_page(struct device *dev,
#34 lore raw

PATCH 13/20 powerpc/dma: remove get_dma_offset

In reply to Christoph Hellwig

Just fold the calculation into __phys_to_dma/__dma_to_phys as those are the only places that should know about it.

---
 arch/powerpc/include/asm/dma-direct.h  |  8 ++++++--
 arch/powerpc/include/asm/dma-mapping.h | 16 ----------------
 2 files changed, 6 insertions(+), 18 deletions(-)
arch/powerpc/include/asm/dma-direct.h +6 -2
index 7702875aabb7..0fba19445ae8 100644
@@ -19,11 +19,15 @@ static inline bool dma_capable(struct device *dev, dma_addr_t addr, size_t size)
 
 static inline dma_addr_t __phys_to_dma(struct device *dev, phys_addr_t paddr)
 {
-	return paddr + get_dma_offset(dev);
+	if (!dev)
+		return paddr + PCI_DRAM_OFFSET;
+	return paddr + dev->archdata.dma_offset;
 }
 
 static inline phys_addr_t __dma_to_phys(struct device *dev, dma_addr_t daddr)
 {
-	return daddr - get_dma_offset(dev);
+	if (!dev)
+		return daddr - PCI_DRAM_OFFSET;
+	return daddr - dev->archdata.dma_offset;
 }
 #endif /* ASM_POWERPC_DMA_DIRECT_H */
arch/powerpc/include/asm/dma-mapping.h +0 -16
index dacd0f93f2b2..f0bf7ac2686c 100644
@@ -80,22 +80,6 @@ static inline const struct dma_map_ops *get_arch_dma_ops(struct bus_type *bus)
 	return NULL;
 }
 
-/*
- * get_dma_offset()
- *
- * Get the dma offset on configurations where the dma address can be determined
- * from the physical address by looking at a simple offset.  Direct dma and
- * swiotlb use this function, but it is typically not used by implementations
- * with an iommu.
- */
-static inline dma_addr_t get_dma_offset(struct device *dev)
-{
-	if (dev)
-		return dev->archdata.dma_offset;
-
-	return PCI_DRAM_OFFSET;
-}
-
 static inline void set_dma_offset(struct device *dev, dma_addr_t off)
 {
 	if (dev)
Signature · 1 line
2.18.0
Acked-by on v1 13/20
#35 lore raw

Re: [PATCH 13/20] powerpc/dma: remove get_dma_offset

Quoted 4 lines · Christoph Hellwig

Just fold the calculation into __phys_to_dma/__dma_to_phys as those are the only places that should know about it.

Quoted 54 lines
---
 arch/powerpc/include/asm/dma-direct.h  |  8 ++++++--
 arch/powerpc/include/asm/dma-mapping.h | 16 ----------------
 2 files changed, 6 insertions(+), 18 deletions(-)
arch/powerpc/include/asm/dma-direct.h +6 -2
index 7702875aabb7..0fba19445ae8 100644
@@ -19,11 +19,15 @@ static inline bool dma_capable(struct device *dev, dma_addr_t addr, size_t size)
 
 static inline dma_addr_t __phys_to_dma(struct device *dev, phys_addr_t paddr)
 {
-	return paddr + get_dma_offset(dev);
+	if (!dev)
+		return paddr + PCI_DRAM_OFFSET;
+	return paddr + dev->archdata.dma_offset;
 }
 
 static inline phys_addr_t __dma_to_phys(struct device *dev, dma_addr_t daddr)
 {
-	return daddr - get_dma_offset(dev);
+	if (!dev)
+		return daddr - PCI_DRAM_OFFSET;
+	return daddr - dev->archdata.dma_offset;
 }
 #endif /* ASM_POWERPC_DMA_DIRECT_H */
arch/powerpc/include/asm/dma-mapping.h +0 -16
index dacd0f93f2b2..f0bf7ac2686c 100644
@@ -80,22 +80,6 @@ static inline const struct dma_map_ops *get_arch_dma_ops(struct bus_type *bus)
 	return NULL;
 }
 
-/*
- * get_dma_offset()
- *
- * Get the dma offset on configurations where the dma address can be determined
- * from the physical address by looking at a simple offset.  Direct dma and
- * swiotlb use this function, but it is typically not used by implementations
- * with an iommu.
- */
-static inline dma_addr_t get_dma_offset(struct device *dev)
-{
-	if (dev)
-		return dev->archdata.dma_offset;
-
-	return PCI_DRAM_OFFSET;
-}
-
 static inline void set_dma_offset(struct device *dev, dma_addr_t off)
 {
 	if (dev)
#36 lore raw

PATCH 14/20 powerpc/dma: replace dma_nommu_dma_supported with dma_direct_supported

In reply to Christoph Hellwig

The ppc32 case of dma_nommu_dma_supported already was a no-op, and the 64-bit case came to the same conclusion as dma_direct_supported, so replace it with the generic version.

---
 arch/powerpc/Kconfig      |  1 +
 arch/powerpc/kernel/dma.c | 28 +++-------------------------
 2 files changed, 4 insertions(+), 25 deletions(-)
arch/powerpc/Kconfig +1 -0
index f9cae7edd735..bbfa6a8df4da 100644
@@ -158,6 +158,7 @@ config PPC
 	select CLONE_BACKWARDS
 	select DCACHE_WORD_ACCESS		if PPC64 && CPU_LITTLE_ENDIAN
 	select DYNAMIC_FTRACE			if FUNCTION_TRACER
+	select DMA_DIRECT_OPS
 	select EDAC_ATOMIC_SCRUB
 	select EDAC_SUPPORT
 	select GENERIC_ATOMIC64			if PPC32
arch/powerpc/kernel/dma.c +3 -25
index 3487de83bb37..511a4972560d 100644
@@ -40,28 +40,6 @@ static u64 __maybe_unused get_pfn_limit(struct device *dev)
 	return pfn;
 }
 
-static int dma_nommu_dma_supported(struct device *dev, u64 mask)
-{
-#ifdef CONFIG_PPC64
-	u64 limit = phys_to_dma(dev, (memblock_end_of_DRAM() - 1));
-
-	/* Limit fits in the mask, we are good */
-	if (mask >= limit)
-		return 1;
-
-#ifdef CONFIG_FSL_SOC
-	/* Freescale gets another chance via ZONE_DMA/ZONE_DMA32, however
-	 * that will have to be refined if/when they support iommus
-	 */
-	return 1;
-#endif
-	/* Sorry ... */
-	return 0;
-#else
-	return 1;
-#endif
-}
-
 #ifndef CONFIG_NOT_COHERENT_CACHE
 void *__dma_nommu_alloc_coherent(struct device *dev, size_t size,
 				  dma_addr_t *dma_handle, gfp_t flag,
@@ -126,7 +104,7 @@ static void *dma_nommu_alloc_coherent(struct device *dev, size_t size,
 	/* The coherent mask may be smaller than the real mask, check if
 	 * we can really use the direct ops
 	 */
-	if (dma_nommu_dma_supported(dev, dev->coherent_dma_mask))
+	if (dma_direct_supported(dev, dev->coherent_dma_mask))
 		return __dma_nommu_alloc_coherent(dev, size, dma_handle,
 						   flag, attrs);
 
@@ -148,7 +126,7 @@ static void dma_nommu_free_coherent(struct device *dev, size_t size,
 	struct iommu_table *iommu;
 
 	/* See comments in dma_nommu_alloc_coherent() */
-	if (dma_nommu_dma_supported(dev, dev->coherent_dma_mask))
+	if (dma_direct_supported(dev, dev->coherent_dma_mask))
 		return __dma_nommu_free_coherent(dev, size, vaddr, dma_handle,
 						  attrs);
 	/* Maybe we used an iommu ... */
@@ -265,7 +243,7 @@ const struct dma_map_ops dma_nommu_ops = {
 	.mmap				= dma_nommu_mmap_coherent,
 	.map_sg				= dma_nommu_map_sg,
 	.unmap_sg			= dma_nommu_unmap_sg,
-	.dma_supported			= dma_nommu_dma_supported,
+	.dma_supported			= dma_direct_supported,
 	.map_page			= dma_nommu_map_page,
 	.unmap_page			= dma_nommu_unmap_page,
 	.get_required_mask		= dma_nommu_get_required_mask,
Signature · 1 line
2.18.0
#37 lore raw

Re: [PATCH 14/20] powerpc/dma: replace dma_nommu_dma_supported with dma_direct_supported

Quoted 3 lines · Christoph Hellwig

The ppc32 case of dma_nommu_dma_supported already was a no-op, and the 64-bit case came to the same conclusion as dma_direct_supported, so replace it with the generic version.

It's not at all equivalent (see my review on your earlier patch) or am I missing something ?

- ppc32 always return 1, but dma_direct_supported() will not for
devices with a <32-bit mask (and yes ppc32 isn't quite right to do so, it should check against memory size, but in practice it worked as the only limited devices we deal with on systems we still support have a 31-bit limitation)

- ppc64 needs to check against the end of DRAM as some devices will
fail the check, dma_direct_supported() doesn't seem to be doing that.

Also as I mentioned, I'm not sure about the business with ZONE_DMA, and that arbitrary 24-bit limit since our entire memory is in ZONE_DMA but that's a different can of worms I suppose.

Quoted 80 lines
---
 arch/powerpc/Kconfig      |  1 +
 arch/powerpc/kernel/dma.c | 28 +++-------------------------
 2 files changed, 4 insertions(+), 25 deletions(-)
arch/powerpc/Kconfig +1 -0
index f9cae7edd735..bbfa6a8df4da 100644
@@ -158,6 +158,7 @@ config PPC
 	select CLONE_BACKWARDS
 	select DCACHE_WORD_ACCESS		if PPC64 && CPU_LITTLE_ENDIAN
 	select DYNAMIC_FTRACE			if FUNCTION_TRACER
+	select DMA_DIRECT_OPS
 	select EDAC_ATOMIC_SCRUB
 	select EDAC_SUPPORT
 	select GENERIC_ATOMIC64			if PPC32
arch/powerpc/kernel/dma.c +3 -25
index 3487de83bb37..511a4972560d 100644
@@ -40,28 +40,6 @@ static u64 __maybe_unused get_pfn_limit(struct device *dev)
 	return pfn;
 }
 
-static int dma_nommu_dma_supported(struct device *dev, u64 mask)
-{
-#ifdef CONFIG_PPC64
-	u64 limit = phys_to_dma(dev, (memblock_end_of_DRAM() - 1));
-
-	/* Limit fits in the mask, we are good */
-	if (mask >= limit)
-		return 1;
-
-#ifdef CONFIG_FSL_SOC
-	/* Freescale gets another chance via ZONE_DMA/ZONE_DMA32, however
-	 * that will have to be refined if/when they support iommus
-	 */
-	return 1;
-#endif
-	/* Sorry ... */
-	return 0;
-#else
-	return 1;
-#endif
-}
-
 #ifndef CONFIG_NOT_COHERENT_CACHE
 void *__dma_nommu_alloc_coherent(struct device *dev, size_t size,
 				  dma_addr_t *dma_handle, gfp_t flag,
@@ -126,7 +104,7 @@ static void *dma_nommu_alloc_coherent(struct device *dev, size_t size,
 	/* The coherent mask may be smaller than the real mask, check if
 	 * we can really use the direct ops
 	 */
-	if (dma_nommu_dma_supported(dev, dev->coherent_dma_mask))
+	if (dma_direct_supported(dev, dev->coherent_dma_mask))
 		return __dma_nommu_alloc_coherent(dev, size, dma_handle,
 						   flag, attrs);
 
@@ -148,7 +126,7 @@ static void dma_nommu_free_coherent(struct device *dev, size_t size,
 	struct iommu_table *iommu;
 
 	/* See comments in dma_nommu_alloc_coherent() */
-	if (dma_nommu_dma_supported(dev, dev->coherent_dma_mask))
+	if (dma_direct_supported(dev, dev->coherent_dma_mask))
 		return __dma_nommu_free_coherent(dev, size, vaddr, dma_handle,
 						  attrs);
 	/* Maybe we used an iommu ... */
@@ -265,7 +243,7 @@ const struct dma_map_ops dma_nommu_ops = {
 	.mmap				= dma_nommu_mmap_coherent,
 	.map_sg				= dma_nommu_map_sg,
 	.unmap_sg			= dma_nommu_unmap_sg,
-	.dma_supported			= dma_nommu_dma_supported,
+	.dma_supported			= dma_direct_supported,
 	.map_page			= dma_nommu_map_page,
 	.unmap_page			= dma_nommu_unmap_page,
 	.get_required_mask		= dma_nommu_get_required_mask,
#38 lore raw

PATCH 15/20 powerpc/dma: remove the unused unmap_page and unmap_sg methods

In reply to Christoph Hellwig

These methods are optional to start with, no need to implement no-op versions.

---
 arch/powerpc/kernel/dma.c | 16 ----------------
 1 file changed, 16 deletions(-)
arch/powerpc/kernel/dma.c +0 -16
index 511a4972560d..2cfc45acbb52 100644
@@ -178,12 +178,6 @@ static int dma_nommu_map_sg(struct device *dev, struct scatterlist *sgl,
 	return nents;
 }
 
-static void dma_nommu_unmap_sg(struct device *dev, struct scatterlist *sg,
-				int nents, enum dma_data_direction direction,
-				unsigned long attrs)
-{
-}
-
 static u64 dma_nommu_get_required_mask(struct device *dev)
 {
 	u64 end, mask;
@@ -209,14 +203,6 @@ static inline dma_addr_t dma_nommu_map_page(struct device *dev,
 	return phys_to_dma(dev, page_to_phys(page)) + offset;
 }
 
-static inline void dma_nommu_unmap_page(struct device *dev,
-					 dma_addr_t dma_address,
-					 size_t size,
-					 enum dma_data_direction direction,
-					 unsigned long attrs)
-{
-}
-
 #ifdef CONFIG_NOT_COHERENT_CACHE
 static inline void dma_nommu_sync_sg(struct device *dev,
 		struct scatterlist *sgl, int nents,
@@ -242,10 +228,8 @@ const struct dma_map_ops dma_nommu_ops = {
 	.free				= dma_nommu_free_coherent,
 	.mmap				= dma_nommu_mmap_coherent,
 	.map_sg				= dma_nommu_map_sg,
-	.unmap_sg			= dma_nommu_unmap_sg,
 	.dma_supported			= dma_direct_supported,
 	.map_page			= dma_nommu_map_page,
-	.unmap_page			= dma_nommu_unmap_page,
 	.get_required_mask		= dma_nommu_get_required_mask,
 #ifdef CONFIG_NOT_COHERENT_CACHE
 	.sync_single_for_cpu 		= dma_nommu_sync_single,
Signature · 1 line
2.18.0
Acked-by on v1 15/20
#39 lore raw

Re: [PATCH 15/20] powerpc/dma: remove the unused unmap_page and unmap_sg methods

Quoted 4 lines · Christoph Hellwig

These methods are optional to start with, no need to implement no-op versions.

Quoted 47 lines
---
 arch/powerpc/kernel/dma.c | 16 ----------------
 1 file changed, 16 deletions(-)
arch/powerpc/kernel/dma.c +0 -16
index 511a4972560d..2cfc45acbb52 100644
@@ -178,12 +178,6 @@ static int dma_nommu_map_sg(struct device *dev, struct scatterlist *sgl,
 	return nents;
 }
 
-static void dma_nommu_unmap_sg(struct device *dev, struct scatterlist *sg,
-				int nents, enum dma_data_direction direction,
-				unsigned long attrs)
-{
-}
-
 static u64 dma_nommu_get_required_mask(struct device *dev)
 {
 	u64 end, mask;
@@ -209,14 +203,6 @@ static inline dma_addr_t dma_nommu_map_page(struct device *dev,
 	return phys_to_dma(dev, page_to_phys(page)) + offset;
 }
 
-static inline void dma_nommu_unmap_page(struct device *dev,
-					 dma_addr_t dma_address,
-					 size_t size,
-					 enum dma_data_direction direction,
-					 unsigned long attrs)
-{
-}
-
 #ifdef CONFIG_NOT_COHERENT_CACHE
 static inline void dma_nommu_sync_sg(struct device *dev,
 		struct scatterlist *sgl, int nents,
@@ -242,10 +228,8 @@ const struct dma_map_ops dma_nommu_ops = {
 	.free				= dma_nommu_free_coherent,
 	.mmap				= dma_nommu_mmap_coherent,
 	.map_sg				= dma_nommu_map_sg,
-	.unmap_sg			= dma_nommu_unmap_sg,
 	.dma_supported			= dma_direct_supported,
 	.map_page			= dma_nommu_map_page,
-	.unmap_page			= dma_nommu_unmap_page,
 	.get_required_mask		= dma_nommu_get_required_mask,
 #ifdef CONFIG_NOT_COHERENT_CACHE
 	.sync_single_for_cpu 		= dma_nommu_sync_single,
#40 lore raw

PATCH 16/20 powerpc/dma: use dma_direct_{alloc,free}

In reply to Christoph Hellwig

These do the same functionality as the existing helpers, but do it simpler, and also allow the (optional) use of CMA.

Note that the swiotlb code now calls into the dma_direct code directly, given that it doesn't work with noncoherent caches at all, and isn't called when we have an iommu either, so the iommu special case in dma_nommu_alloc_coherent isn't required for swiotlb.

---
 arch/powerpc/include/asm/pgtable.h |  1 -
 arch/powerpc/kernel/dma-swiotlb.c  |  4 +-
 arch/powerpc/kernel/dma.c          | 78 ++++--------------------------
 arch/powerpc/mm/mem.c              | 19 --------
 4 files changed, 11 insertions(+), 91 deletions(-)
arch/powerpc/include/asm/pgtable.h +0 -1
index 14c79a7dc855..123de4958d2e 100644
@@ -38,7 +38,6 @@ extern unsigned long empty_zero_page[];
 extern pgd_t swapper_pg_dir[];
 
 void limit_zone_pfn(enum zone_type zone, unsigned long max_pfn);
-int dma_pfn_limit_to_zone(u64 pfn_limit);
 extern void paging_init(void);
 
 /*
arch/powerpc/kernel/dma-swiotlb.c +2 -2
index f6e0701c5303..25986fcd1e5e 100644
@@ -46,8 +46,8 @@ static u64 swiotlb_powerpc_get_required(struct device *dev)
  * for everything else.
  */
 const struct dma_map_ops powerpc_swiotlb_dma_ops = {
-	.alloc = __dma_nommu_alloc_coherent,
-	.free = __dma_nommu_free_coherent,
+	.alloc = dma_direct_alloc,
+	.free = dma_direct_free,
 	.mmap = dma_nommu_mmap_coherent,
 	.map_sg = swiotlb_map_sg_attrs,
 	.unmap_sg = swiotlb_unmap_sg_attrs,
arch/powerpc/kernel/dma.c +9 -69

100 lines not on this page · raw message

arch/powerpc/mm/mem.c +0 -19
index 5c8530d0c611..ec8ed9d7abef 100644
@@ -276,25 +276,6 @@ void __init limit_zone_pfn(enum zone_type zone, unsigned long pfn_limit)
 	}
 }
 
-/*
- * Find the least restrictive zone that is entirely below the
- * specified pfn limit.  Returns < 0 if no suitable zone is found.
- *
- * pfn_limit must be u64 because it can exceed 32 bits even on 32-bit
- * systems -- the DMA limit can be higher than any possible real pfn.
- */
-int dma_pfn_limit_to_zone(u64 pfn_limit)
-{
-	int i;
-
-	for (i = TOP_ZONE; i >= 0; i--) {
-		if (max_zone_pfns[i] <= pfn_limit)
-			return i;
-	}
-
-	return -EPERM;
-}
-
 /*
  * paging_init() sets up the page tables - in fact we've already done this.
  */
Signature · 1 line
2.18.0
#41 lore raw

Re: [PATCH 16/20] powerpc/dma: use dma_direct_{alloc,free}

Quoted 7 lines · Christoph Hellwig

These do the same functionality as the existing helpers, but do it simpler, and also allow the (optional) use of CMA.

Note that the swiotlb code now calls into the dma_direct code directly, given that it doesn't work with noncoherent caches at all, and isn't called when we have an iommu either, so the iommu special case in dma_nommu_alloc_coherent isn't required for swiotlb.

I am not convinced that this will produce the same results due to the way the zone picking works.

As for the interaction with swiotlb, we'll need the FSL guys to have a look. Scott, do you remember what this is about ?

Quoted 172 lines
---
 arch/powerpc/include/asm/pgtable.h |  1 -
 arch/powerpc/kernel/dma-swiotlb.c  |  4 +-
 arch/powerpc/kernel/dma.c          | 78 ++++--------------------------
 arch/powerpc/mm/mem.c              | 19 --------
 4 files changed, 11 insertions(+), 91 deletions(-)
arch/powerpc/include/asm/pgtable.h +0 -1
index 14c79a7dc855..123de4958d2e 100644
@@ -38,7 +38,6 @@ extern unsigned long empty_zero_page[];
 extern pgd_t swapper_pg_dir[];
 
 void limit_zone_pfn(enum zone_type zone, unsigned long max_pfn);
-int dma_pfn_limit_to_zone(u64 pfn_limit);
 extern void paging_init(void);
 
 /*
arch/powerpc/kernel/dma-swiotlb.c +2 -2
index f6e0701c5303..25986fcd1e5e 100644
@@ -46,8 +46,8 @@ static u64 swiotlb_powerpc_get_required(struct device *dev)
  * for everything else.
  */
 const struct dma_map_ops powerpc_swiotlb_dma_ops = {
-	.alloc = __dma_nommu_alloc_coherent,
-	.free = __dma_nommu_free_coherent,
+	.alloc = dma_direct_alloc,
+	.free = dma_direct_free,
 	.mmap = dma_nommu_mmap_coherent,
 	.map_sg = swiotlb_map_sg_attrs,
 	.unmap_sg = swiotlb_unmap_sg_attrs,
arch/powerpc/kernel/dma.c +9 -69
index 2cfc45acbb52..2b90a403cdac 100644
@@ -26,75 +26,6 @@
  * can set archdata.dma_data to an unsigned long holding the offset. By
  * default the offset is PCI_DRAM_OFFSET.
  */
-
-static u64 __maybe_unused get_pfn_limit(struct device *dev)
-{
-	u64 pfn = (dev->coherent_dma_mask >> PAGE_SHIFT) + 1;
-	struct dev_archdata __maybe_unused *sd = &dev->archdata;
-
-#ifdef CONFIG_SWIOTLB
-	if (sd->max_direct_dma_addr && dev->dma_ops = &powerpc_swiotlb_dma_ops)
-		pfn = min_t(u64, pfn, sd->max_direct_dma_addr >> PAGE_SHIFT);
-#endif
-
-	return pfn;
-}
-
-#ifndef CONFIG_NOT_COHERENT_CACHE
-void *__dma_nommu_alloc_coherent(struct device *dev, size_t size,
-				  dma_addr_t *dma_handle, gfp_t flag,
-				  unsigned long attrs)
-{
-	void *ret;
-	struct page *page;
-	int node = dev_to_node(dev);
-#ifdef CONFIG_FSL_SOC
-	u64 pfn = get_pfn_limit(dev);
-	int zone;
-
-	/*
-	 * This code should be OK on other platforms, but we have drivers that
-	 * don't set coherent_dma_mask. As a workaround we just ifdef it. This
-	 * whole routine needs some serious cleanup.
-	 */
-
-	zone = dma_pfn_limit_to_zone(pfn);
-	if (zone < 0) {
-		dev_err(dev, "%s: No suitable zone for pfn %#llx\n",
-			__func__, pfn);
-		return NULL;
-	}
-
-	switch (zone) {
-	case ZONE_DMA:
-		flag |= GFP_DMA;
-		break;
-#ifdef CONFIG_ZONE_DMA32
-	case ZONE_DMA32:
-		flag |= GFP_DMA32;
-		break;
-#endif
-	};
-#endif /* CONFIG_FSL_SOC */
-
-	page = alloc_pages_node(node, flag, get_order(size));
-	if (page = NULL)
-		return NULL;
-	ret = page_address(page);
-	memset(ret, 0, size);
-	*dma_handle = phys_to_dma(dev,__pa(ret));
-
-	return ret;
-}
-
-void __dma_nommu_free_coherent(struct device *dev, size_t size,
-				void *vaddr, dma_addr_t dma_handle,
-				unsigned long attrs)
-{
-	free_pages((unsigned long)vaddr, get_order(size));
-}
-#endif /* !CONFIG_NOT_COHERENT_CACHE */
-
 static void *dma_nommu_alloc_coherent(struct device *dev, size_t size,
 				       dma_addr_t *dma_handle, gfp_t flag,
 				       unsigned long attrs)
@@ -105,8 +36,12 @@ static void *dma_nommu_alloc_coherent(struct device *dev, size_t size,
 	 * we can really use the direct ops
 	 */
 	if (dma_direct_supported(dev, dev->coherent_dma_mask))
+#ifdef CONFIG_NOT_COHERENT_CACHE
 		return __dma_nommu_alloc_coherent(dev, size, dma_handle,
 						   flag, attrs);
+#else
+		return dma_direct_alloc(dev, size, dma_handle, flag, attrs);
+#endif
 
 	/* Ok we can't ... do we have an iommu ? If not, fail */
 	iommu = get_iommu_table_base(dev);
@@ -127,8 +62,13 @@ static void dma_nommu_free_coherent(struct device *dev, size_t size,
 
 	/* See comments in dma_nommu_alloc_coherent() */
 	if (dma_direct_supported(dev, dev->coherent_dma_mask))
+#ifdef CONFIG_NOT_COHERENT_CACHE
 		return __dma_nommu_free_coherent(dev, size, vaddr, dma_handle,
 						  attrs);
+#else
+		return dma_direct_free(dev, size, vaddr, dma_handle, attrs);
+#endif
+
 	/* Maybe we used an iommu ... */
 	iommu = get_iommu_table_base(dev);
 
arch/powerpc/mm/mem.c +0 -19
index 5c8530d0c611..ec8ed9d7abef 100644
@@ -276,25 +276,6 @@ void __init limit_zone_pfn(enum zone_type zone, unsigned long pfn_limit)
 	}
 }
 
-/*
- * Find the least restrictive zone that is entirely below the
- * specified pfn limit.  Returns < 0 if no suitable zone is found.
- *
- * pfn_limit must be u64 because it can exceed 32 bits even on 32-bit
- * systems -- the DMA limit can be higher than any possible real pfn.
- */
-int dma_pfn_limit_to_zone(u64 pfn_limit)
-{
-	int i;
-
-	for (i = TOP_ZONE; i >= 0; i--) {
-		if (max_zone_pfns[i] <= pfn_limit)
-			return i;
-	}
-
-	return -EPERM;
-}
-
 /*
  * paging_init() sets up the page tables - in fact we've already done this.
  */
#42 lore raw

Re: [PATCH 16/20] powerpc/dma: use dma_direct_{alloc,free}

Quoted 10 lines · Benjamin Herrenschmidt
Quoted 8 lines · Christoph Hellwig

These do the same functionality as the existing helpers, but do it simpler, and also allow the (optional) use of CMA.

Note that the swiotlb code now calls into the dma_direct code directly, given that it doesn't work with noncoherent caches at all, and isn't called
when we have an iommu either, so the iommu special case in dma_nommu_alloc_coherent isn't required for swiotlb.

Quoted 5 lines

I am not convinced that this will produce the same results due to the way the zone picking works.

As for the interaction with swiotlb, we'll need the FSL guys to have a look. Scott, do you remember what this is about ?

dma_direct_alloc() has similar (though not identical[1]) zone picking, so I think it will work. Needs testing though, and I no longer have a book3e machine with a PCIe card in it.

The odd thing about this platform (fsl book3e) is the 31-bit[2] limitation on PCI. We currently use ZONE_DMA32 for this, rather than ZONE_DMA, at Ben's request[3]. dma_direct_alloc() regards ZONE_DMA32 as being fixed at 32-bits, but it doesn't really matter as long as limit_zone_pfn() still works, and the allocation is made below 2 GiB. If we were to switch to ZONE_DMA, and have both 31-bit and 32-bit zones, then dma_direct_alloc() would have a problem knowing when to use the 31-bit zone since it's based on a non-power-of-2 limit that isn't reflected in the dma mask.

-Scott

[1] The logic in dma_direct_alloc() seems wrong -- the zone should need to fit in the mask, not the other way around. If ARCH_ZONE_DMA_BITS is 24, then 0x007fffff should be a failure rather than GFP_DMA, 0x7fffffff should be GFP_DMA rather than GFP_DMA32, and 0x3ffffffff should be GFP_DMA32 rather than an unrestricted allocation (in each case assuming that the end of RAM is beyond the mask).

[2] The actual limit is closer to 4 GiB, but not quite due to special windows. swiotlb still uses the real limit when deciding whether to bounce, so the dma
mask is still 32 bits.

[3] https://lists.ozlabs.org/pipermail/linuxppc-dev/2012-July/099593.html

#43 lore raw

PATCH 17/20 powerpc/dma-swiotlb: use generic swiotlb_dma_ops

In reply to Christoph Hellwig

These are identical to the arch specific ones, so remove them.

---
 arch/powerpc/include/asm/dma-direct.h |  4 ++++
 arch/powerpc/include/asm/swiotlb.h    |  2 --
 arch/powerpc/kernel/dma-swiotlb.c     | 28 ++-------------------------
 arch/powerpc/sysdev/fsl_pci.c         |  2 +-
 4 files changed, 7 insertions(+), 29 deletions(-)
arch/powerpc/include/asm/dma-direct.h +4 -0
index 0fba19445ae8..657f84ddb20d 100644
@@ -30,4 +30,8 @@ static inline phys_addr_t __dma_to_phys(struct device *dev, dma_addr_t daddr)
 		return daddr - PCI_DRAM_OFFSET;
 	return daddr - dev->archdata.dma_offset;
 }
+
+u64 swiotlb_powerpc_get_required(struct device *dev);
+#define swiotlb_get_required_mask swiotlb_powerpc_get_required
+
 #endif /* ASM_POWERPC_DMA_DIRECT_H */
arch/powerpc/include/asm/swiotlb.h +0 -2
index f65ecf57b66c..1d8c1da26ab3 100644
@@ -13,8 +13,6 @@
 
 #include <linux/swiotlb.h>
 
-extern const struct dma_map_ops powerpc_swiotlb_dma_ops;
-
 extern unsigned int ppc_swiotlb_enable;
 int __init swiotlb_setup_bus_notifier(void);
 
arch/powerpc/kernel/dma-swiotlb.c +2 -26
index 25986fcd1e5e..0c269de61f39 100644
@@ -24,7 +24,7 @@
 
 unsigned int ppc_swiotlb_enable;
 
-static u64 swiotlb_powerpc_get_required(struct device *dev)
+u64 swiotlb_powerpc_get_required(struct device *dev)
 {
 	u64 end, mask, max_direct_dma_addr = dev->archdata.max_direct_dma_addr;
 
@@ -38,30 +38,6 @@ static u64 swiotlb_powerpc_get_required(struct device *dev)
 	return mask;
 }
 
-/*
- * At the moment, all platforms that use this code only require
- * swiotlb to be used if we're operating on HIGHMEM.  Since
- * we don't ever call anything other than map_sg, unmap_sg,
- * map_page, and unmap_page on highmem, use normal dma_ops
- * for everything else.
- */
-const struct dma_map_ops powerpc_swiotlb_dma_ops = {
-	.alloc = dma_direct_alloc,
-	.free = dma_direct_free,
-	.mmap = dma_nommu_mmap_coherent,
-	.map_sg = swiotlb_map_sg_attrs,
-	.unmap_sg = swiotlb_unmap_sg_attrs,
-	.dma_supported = swiotlb_dma_supported,
-	.map_page = swiotlb_map_page,
-	.unmap_page = swiotlb_unmap_page,
-	.sync_single_for_cpu = swiotlb_sync_single_for_cpu,
-	.sync_single_for_device = swiotlb_sync_single_for_device,
-	.sync_sg_for_cpu = swiotlb_sync_sg_for_cpu,
-	.sync_sg_for_device = swiotlb_sync_sg_for_device,
-	.mapping_error = swiotlb_dma_mapping_error,
-	.get_required_mask = swiotlb_powerpc_get_required,
-};
-
 void pci_dma_dev_setup_swiotlb(struct pci_dev *pdev)
 {
 	struct pci_controller *hose;
@@ -88,7 +64,7 @@ static int ppc_swiotlb_bus_notify(struct notifier_block *nb,
 
 	/* May need to bounce if the device can't address all of DRAM */
 	if ((dma_get_mask(dev) + 1) < memblock_end_of_DRAM())
-		set_dma_ops(dev, &powerpc_swiotlb_dma_ops);
+		set_dma_ops(dev, &swiotlb_dma_ops);
 
 	return NOTIFY_DONE;
 }
arch/powerpc/sysdev/fsl_pci.c +1 -1
index 918be816b097..daf44bc0108d 100644
@@ -118,7 +118,7 @@ static void setup_swiotlb_ops(struct pci_controller *hose)
 {
 	if (ppc_swiotlb_enable) {
 		hose->controller_ops.dma_dev_setup = pci_dma_dev_setup_swiotlb;
-		set_pci_dma_ops(&powerpc_swiotlb_dma_ops);
+		set_pci_dma_ops(&swiotlb_dma_ops);
 	}
 }
 #else
Signature · 1 line
2.18.0
Acked-by on v1 17/20
#44 lore raw

Re: [PATCH 17/20] powerpc/dma-swiotlb: use generic swiotlb_dma_ops

Quoted 3 lines · Christoph Hellwig

These are identical to the arch specific ones, so remove them.

Quoted 99 lines
---
 arch/powerpc/include/asm/dma-direct.h |  4 ++++
 arch/powerpc/include/asm/swiotlb.h    |  2 --
 arch/powerpc/kernel/dma-swiotlb.c     | 28 ++-------------------------
 arch/powerpc/sysdev/fsl_pci.c         |  2 +-
 4 files changed, 7 insertions(+), 29 deletions(-)
arch/powerpc/include/asm/dma-direct.h +4 -0
index 0fba19445ae8..657f84ddb20d 100644
@@ -30,4 +30,8 @@ static inline phys_addr_t __dma_to_phys(struct device *dev, dma_addr_t daddr)
 		return daddr - PCI_DRAM_OFFSET;
 	return daddr - dev->archdata.dma_offset;
 }
+
+u64 swiotlb_powerpc_get_required(struct device *dev);
+#define swiotlb_get_required_mask swiotlb_powerpc_get_required
+
 #endif /* ASM_POWERPC_DMA_DIRECT_H */
arch/powerpc/include/asm/swiotlb.h +0 -2
index f65ecf57b66c..1d8c1da26ab3 100644
@@ -13,8 +13,6 @@
 
 #include <linux/swiotlb.h>
 
-extern const struct dma_map_ops powerpc_swiotlb_dma_ops;
-
 extern unsigned int ppc_swiotlb_enable;
 int __init swiotlb_setup_bus_notifier(void);
 
arch/powerpc/kernel/dma-swiotlb.c +2 -26
index 25986fcd1e5e..0c269de61f39 100644
@@ -24,7 +24,7 @@
 
 unsigned int ppc_swiotlb_enable;
 
-static u64 swiotlb_powerpc_get_required(struct device *dev)
+u64 swiotlb_powerpc_get_required(struct device *dev)
 {
 	u64 end, mask, max_direct_dma_addr = dev->archdata.max_direct_dma_addr;
 
@@ -38,30 +38,6 @@ static u64 swiotlb_powerpc_get_required(struct device *dev)
 	return mask;
 }
 
-/*
- * At the moment, all platforms that use this code only require
- * swiotlb to be used if we're operating on HIGHMEM.  Since
- * we don't ever call anything other than map_sg, unmap_sg,
- * map_page, and unmap_page on highmem, use normal dma_ops
- * for everything else.
- */
-const struct dma_map_ops powerpc_swiotlb_dma_ops = {
-	.alloc = dma_direct_alloc,
-	.free = dma_direct_free,
-	.mmap = dma_nommu_mmap_coherent,
-	.map_sg = swiotlb_map_sg_attrs,
-	.unmap_sg = swiotlb_unmap_sg_attrs,
-	.dma_supported = swiotlb_dma_supported,
-	.map_page = swiotlb_map_page,
-	.unmap_page = swiotlb_unmap_page,
-	.sync_single_for_cpu = swiotlb_sync_single_for_cpu,
-	.sync_single_for_device = swiotlb_sync_single_for_device,
-	.sync_sg_for_cpu = swiotlb_sync_sg_for_cpu,
-	.sync_sg_for_device = swiotlb_sync_sg_for_device,
-	.mapping_error = swiotlb_dma_mapping_error,
-	.get_required_mask = swiotlb_powerpc_get_required,
-};
-
 void pci_dma_dev_setup_swiotlb(struct pci_dev *pdev)
 {
 	struct pci_controller *hose;
@@ -88,7 +64,7 @@ static int ppc_swiotlb_bus_notify(struct notifier_block *nb,
 
 	/* May need to bounce if the device can't address all of DRAM */
 	if ((dma_get_mask(dev) + 1) < memblock_end_of_DRAM())
-		set_dma_ops(dev, &powerpc_swiotlb_dma_ops);
+		set_dma_ops(dev, &swiotlb_dma_ops);
 
 	return NOTIFY_DONE;
 }
arch/powerpc/sysdev/fsl_pci.c +1 -1
index 918be816b097..daf44bc0108d 100644
@@ -118,7 +118,7 @@ static void setup_swiotlb_ops(struct pci_controller *hose)
 {
 	if (ppc_swiotlb_enable) {
 		hose->controller_ops.dma_dev_setup = pci_dma_dev_setup_swiotlb;
-		set_pci_dma_ops(&powerpc_swiotlb_dma_ops);
+		set_pci_dma_ops(&swiotlb_dma_ops);
 	}
 }
 #else
#45 lore raw

Re: [PATCH 17/20] powerpc/dma-swiotlb: use generic swiotlb_dma_ops

Quoted 6 lines · Benjamin Herrenschmidt
Quoted 3 lines · Christoph Hellwig

These are identical to the arch specific ones, so remove them.

Note: We will still need to implement some custom variant of this for our secure VMs ...

Basically we'll need to use the existing bounce bufferring as-is but the condition will be different, it won't be whether the address is below a certain limit, it will be *always*.

Cheers,
Ben.

Quoted 99 lines
Quoted 99 lines
---
 arch/powerpc/include/asm/dma-direct.h |  4 ++++
 arch/powerpc/include/asm/swiotlb.h    |  2 --
 arch/powerpc/kernel/dma-swiotlb.c     | 28 ++-------------------------
 arch/powerpc/sysdev/fsl_pci.c         |  2 +-
 4 files changed, 7 insertions(+), 29 deletions(-)
arch/powerpc/include/asm/dma-direct.h +4 -0
index 0fba19445ae8..657f84ddb20d 100644
@@ -30,4 +30,8 @@ static inline phys_addr_t __dma_to_phys(struct device *dev, dma_addr_t daddr)
 		return daddr - PCI_DRAM_OFFSET;
 	return daddr - dev->archdata.dma_offset;
 }
+
+u64 swiotlb_powerpc_get_required(struct device *dev);
+#define swiotlb_get_required_mask swiotlb_powerpc_get_required
+
 #endif /* ASM_POWERPC_DMA_DIRECT_H */
arch/powerpc/include/asm/swiotlb.h +0 -2
index f65ecf57b66c..1d8c1da26ab3 100644
@@ -13,8 +13,6 @@
 
 #include <linux/swiotlb.h>
 
-extern const struct dma_map_ops powerpc_swiotlb_dma_ops;
-
 extern unsigned int ppc_swiotlb_enable;
 int __init swiotlb_setup_bus_notifier(void);
 
arch/powerpc/kernel/dma-swiotlb.c +2 -26
index 25986fcd1e5e..0c269de61f39 100644
@@ -24,7 +24,7 @@
 
 unsigned int ppc_swiotlb_enable;
 
-static u64 swiotlb_powerpc_get_required(struct device *dev)
+u64 swiotlb_powerpc_get_required(struct device *dev)
 {
 	u64 end, mask, max_direct_dma_addr = dev->archdata.max_direct_dma_addr;
 
@@ -38,30 +38,6 @@ static u64 swiotlb_powerpc_get_required(struct device *dev)
 	return mask;
 }
 
-/*
- * At the moment, all platforms that use this code only require
- * swiotlb to be used if we're operating on HIGHMEM.  Since
- * we don't ever call anything other than map_sg, unmap_sg,
- * map_page, and unmap_page on highmem, use normal dma_ops
- * for everything else.
- */
-const struct dma_map_ops powerpc_swiotlb_dma_ops = {
-	.alloc = dma_direct_alloc,
-	.free = dma_direct_free,
-	.mmap = dma_nommu_mmap_coherent,
-	.map_sg = swiotlb_map_sg_attrs,
-	.unmap_sg = swiotlb_unmap_sg_attrs,
-	.dma_supported = swiotlb_dma_supported,
-	.map_page = swiotlb_map_page,
-	.unmap_page = swiotlb_unmap_page,
-	.sync_single_for_cpu = swiotlb_sync_single_for_cpu,
-	.sync_single_for_device = swiotlb_sync_single_for_device,
-	.sync_sg_for_cpu = swiotlb_sync_sg_for_cpu,
-	.sync_sg_for_device = swiotlb_sync_sg_for_device,
-	.mapping_error = swiotlb_dma_mapping_error,
-	.get_required_mask = swiotlb_powerpc_get_required,
-};
-
 void pci_dma_dev_setup_swiotlb(struct pci_dev *pdev)
 {
 	struct pci_controller *hose;
@@ -88,7 +64,7 @@ static int ppc_swiotlb_bus_notify(struct notifier_block *nb,
 
 	/* May need to bounce if the device can't address all of DRAM */
 	if ((dma_get_mask(dev) + 1) < memblock_end_of_DRAM())
-		set_dma_ops(dev, &powerpc_swiotlb_dma_ops);
+		set_dma_ops(dev, &swiotlb_dma_ops);
 
 	return NOTIFY_DONE;
 }
arch/powerpc/sysdev/fsl_pci.c +1 -1
index 918be816b097..daf44bc0108d 100644
@@ -118,7 +118,7 @@ static void setup_swiotlb_ops(struct pci_controller *hose)
 {
 	if (ppc_swiotlb_enable) {
 		hose->controller_ops.dma_dev_setup = pci_dma_dev_setup_swiotlb;
-		set_pci_dma_ops(&powerpc_swiotlb_dma_ops);
+		set_pci_dma_ops(&swiotlb_dma_ops);
 	}
 }
 #else
#46 lore raw

Re: [PATCH 17/20] powerpc/dma-swiotlb: use generic swiotlb_dma_ops

Quoted 6 lines · Benjamin Herrenschmidt

Note: We will still need to implement some custom variant of this for our secure VMs ...

Basically we'll need to use the existing bounce bufferring as-is but the condition will be different, it won't be whether the address is below a certain limit, it will be *always*.

The conditions are in the dma_capable() helper that the architecture can override (and which powerpc already does override).

#47 lore raw

PATCH 18/20 powerpc/dma-noncoherent: use generic dma_noncoherent_ops

In reply to Christoph Hellwig

The generic dma-noncoherent code provides all that is needed by powerpc.

Note that the cache maintainance in the existing code is a bit odd as it implements both the sync_to_device and sync_to_cpu callouts, but never flushes caches when unmapping. This patch keeps both directions arounds, which will lead to more flushing than the previous implementation. Someone more familar with the required CPUs should eventually take a look and optimize the cache flush handling if needed.

---
 arch/powerpc/Kconfig                   |  2 +-
 arch/powerpc/include/asm/dma-mapping.h | 29 -------------
 arch/powerpc/kernel/dma.c              | 59 +++-----------------------
 arch/powerpc/kernel/pci-common.c       |  5 ++-
 arch/powerpc/kernel/setup-common.c     |  4 ++
 arch/powerpc/mm/dma-noncoherent.c      | 52 +++++++++++++++++------
 arch/powerpc/platforms/44x/warp.c      |  2 +-
 arch/powerpc/platforms/Kconfig.cputype |  6 ++-
 8 files changed, 60 insertions(+), 99 deletions(-)
arch/powerpc/Kconfig +1 -1
index bbfa6a8df4da..33c6017ffce6 100644
@@ -129,7 +129,7 @@ config PPC
 	# Please keep this list sorted alphabetically.
 	#
 	select ARCH_HAS_DEVMEM_IS_ALLOWED
-	select ARCH_HAS_DMA_SET_COHERENT_MASK
+	select ARCH_HAS_DMA_SET_COHERENT_MASK if !NOT_COHERENT_CACHE
 	select ARCH_HAS_ELF_RANDOMIZE
 	select ARCH_HAS_FORTIFY_SOURCE
 	select ARCH_HAS_GCOV_PROFILE_ALL
arch/powerpc/include/asm/dma-mapping.h +0 -29
index f0bf7ac2686c..879c4efba785 100644
@@ -19,40 +19,11 @@
 #include <asm/swiotlb.h>
 
 /* Some dma direct funcs must be visible for use in other dma_ops */
-extern void *__dma_nommu_alloc_coherent(struct device *dev, size_t size,
-					 dma_addr_t *dma_handle, gfp_t flag,
-					 unsigned long attrs);
-extern void __dma_nommu_free_coherent(struct device *dev, size_t size,
-				       void *vaddr, dma_addr_t dma_handle,
-				       unsigned long attrs);
 extern int dma_nommu_mmap_coherent(struct device *dev,
 				    struct vm_area_struct *vma,
 				    void *cpu_addr, dma_addr_t handle,
 				    size_t size, unsigned long attrs);
 
-#ifdef CONFIG_NOT_COHERENT_CACHE
-/*
- * DMA-consistent mapping functions for PowerPCs that don't support
- * cache snooping.  These allocate/free a region of uncached mapped
- * memory space for use with DMA devices.  Alternatively, you could
- * allocate the space "normally" and use the cache management functions
- * to ensure it is consistent.
- */
-struct device;
-extern void __dma_sync(void *vaddr, size_t size, int direction);
-extern void __dma_sync_page(struct page *page, unsigned long offset,
-				 size_t size, int direction);
-extern unsigned long __dma_get_coherent_pfn(unsigned long cpu_addr);
-
-#else /* ! CONFIG_NOT_COHERENT_CACHE */
-/*
- * Cache coherent cores.
- */
-
-#define __dma_sync(addr, size, rw)		((void)0)
-#define __dma_sync_page(pg, off, sz, rw)	((void)0)
-
-#endif /* ! CONFIG_NOT_COHERENT_CACHE */
 
 static inline unsigned long device_to_mask(struct device *dev)
 {
arch/powerpc/kernel/dma.c +6 -53

114 lines not on this page · raw message

arch/powerpc/kernel/pci-common.c +4 -1
index fe9733ffffaa..898ffb636b75 100644
@@ -59,8 +59,11 @@ static DECLARE_BITMAP(phb_bitmap, MAX_PHBS);
 resource_size_t isa_mem_base;
 EXPORT_SYMBOL(isa_mem_base);
 
-
+#ifdef CONFIG_NOT_COHERENT_CACHE
+static const struct dma_map_ops *pci_dma_ops = &dma_noncoherent_ops;
+#else
 static const struct dma_map_ops *pci_dma_ops = &dma_nommu_ops;
+#endif
 
 void set_pci_dma_ops(const struct dma_map_ops *dma_ops)
 {
arch/powerpc/kernel/setup-common.c +4 -0
index 40b44bb53a4e..2488826fa543 100644
@@ -792,7 +792,11 @@ void arch_setup_pdev_archdata(struct platform_device *pdev)
 {
 	pdev->archdata.dma_mask = DMA_BIT_MASK(32);
 	pdev->dev.dma_mask = &pdev->archdata.dma_mask;
+#ifdef CONFIG_NOT_COHERENT_CACHE
+	set_dma_ops(&pdev->dev, &dma_noncoherent_ops);
+#else
  	set_dma_ops(&pdev->dev, &dma_nommu_ops);
+#endif
 }
 
 static __init void print_system_info(void)
arch/powerpc/mm/dma-noncoherent.c +39 -13

100 lines not on this page · raw message

arch/powerpc/platforms/44x/warp.c +1 -1
index 7e4f8ca19ce8..c0e6fb270d59 100644
@@ -47,7 +47,7 @@ static int __init warp_probe(void)
 	if (!of_machine_is_compatible("pika,warp"))
 		return 0;
 
-	/* For __dma_nommu_alloc_coherent */
+	/* For arch_dma_alloc */
 	ISA_DMA_THRESHOLD = ~0L;
 
 	return 1;
arch/powerpc/platforms/Kconfig.cputype +5 -1
index a2578bf8d560..9d83f54ccf11 100644
@@ -387,7 +387,11 @@ config NOT_COHERENT_CACHE
 	depends on 4xx || PPC_8xx || E200 || PPC_MPC512x || GAMECUBE_COMMON
 	default n if PPC_47x
 	default y
-	select NEED_DMA_MAP_STATE
+	select ARCH_HAS_SYNC_DMA_FOR_DEVICE
+	select ARCH_HAS_SYNC_DMA_FOR_CPU
+	select DMA_NONCOHERENT_GET_REQUIRED
+	select DMA_NONCOHERENT_MMAP
+	select DMA_NONCOHERENT_OPS
 
 config CHECK_CACHE_COHERENCY
 	bool
Signature · 1 line
2.18.0
Acked-by on v1 18/20
#48 lore raw

Re: [PATCH 18/20] powerpc/dma-noncoherent: use generic dma_noncoherent_ops

Quoted 8 lines · Christoph Hellwig

The generic dma-noncoherent code provides all that is needed by powerpc.

Note that the cache maintainance in the existing code is a bit odd as it implements both the sync_to_device and sync_to_cpu callouts, but never flushes caches when unmapping. This patch keeps both directions arounds, which will lead to more flushing than the previous implementation. Someone more familar with the required CPUs should eventually take a look and optimize the cache flush handling if needed.

The original code looks bogus indeed.

I think we got away with it because those older CPUs wouldn't speculate or prefetch aggressively enough (or at all) so the flush on map was sufficient, the stuff wouldn't come back into the cache.

But safe is better than sorry, so ... tentative Ack, I do need to try to dig one of these things to test, which might take a while.

Quoted 369 lines
---
 arch/powerpc/Kconfig                   |  2 +-
 arch/powerpc/include/asm/dma-mapping.h | 29 -------------
 arch/powerpc/kernel/dma.c              | 59 +++-----------------------
 arch/powerpc/kernel/pci-common.c       |  5 ++-
 arch/powerpc/kernel/setup-common.c     |  4 ++
 arch/powerpc/mm/dma-noncoherent.c      | 52 +++++++++++++++++------
 arch/powerpc/platforms/44x/warp.c      |  2 +-
 arch/powerpc/platforms/Kconfig.cputype |  6 ++-
 8 files changed, 60 insertions(+), 99 deletions(-)
arch/powerpc/Kconfig +1 -1
index bbfa6a8df4da..33c6017ffce6 100644
@@ -129,7 +129,7 @@ config PPC
 	# Please keep this list sorted alphabetically.
 	#
 	select ARCH_HAS_DEVMEM_IS_ALLOWED
-	select ARCH_HAS_DMA_SET_COHERENT_MASK
+	select ARCH_HAS_DMA_SET_COHERENT_MASK if !NOT_COHERENT_CACHE
 	select ARCH_HAS_ELF_RANDOMIZE
 	select ARCH_HAS_FORTIFY_SOURCE
 	select ARCH_HAS_GCOV_PROFILE_ALL
arch/powerpc/include/asm/dma-mapping.h +0 -29
index f0bf7ac2686c..879c4efba785 100644
@@ -19,40 +19,11 @@
 #include <asm/swiotlb.h>
 
 /* Some dma direct funcs must be visible for use in other dma_ops */
-extern void *__dma_nommu_alloc_coherent(struct device *dev, size_t size,
-					 dma_addr_t *dma_handle, gfp_t flag,
-					 unsigned long attrs);
-extern void __dma_nommu_free_coherent(struct device *dev, size_t size,
-				       void *vaddr, dma_addr_t dma_handle,
-				       unsigned long attrs);
 extern int dma_nommu_mmap_coherent(struct device *dev,
 				    struct vm_area_struct *vma,
 				    void *cpu_addr, dma_addr_t handle,
 				    size_t size, unsigned long attrs);
 
-#ifdef CONFIG_NOT_COHERENT_CACHE
-/*
- * DMA-consistent mapping functions for PowerPCs that don't support
- * cache snooping.  These allocate/free a region of uncached mapped
- * memory space for use with DMA devices.  Alternatively, you could
- * allocate the space "normally" and use the cache management functions
- * to ensure it is consistent.
- */
-struct device;
-extern void __dma_sync(void *vaddr, size_t size, int direction);
-extern void __dma_sync_page(struct page *page, unsigned long offset,
-				 size_t size, int direction);
-extern unsigned long __dma_get_coherent_pfn(unsigned long cpu_addr);
-
-#else /* ! CONFIG_NOT_COHERENT_CACHE */
-/*
- * Cache coherent cores.
- */
-
-#define __dma_sync(addr, size, rw)		((void)0)
-#define __dma_sync_page(pg, off, sz, rw)	((void)0)
-
-#endif /* ! CONFIG_NOT_COHERENT_CACHE */
 
 static inline unsigned long device_to_mask(struct device *dev)
 {
arch/powerpc/kernel/dma.c +6 -53
index 2b90a403cdac..b2e88075b2ea 100644
@@ -36,12 +36,7 @@ static void *dma_nommu_alloc_coherent(struct device *dev, size_t size,
 	 * we can really use the direct ops
 	 */
 	if (dma_direct_supported(dev, dev->coherent_dma_mask))
-#ifdef CONFIG_NOT_COHERENT_CACHE
-		return __dma_nommu_alloc_coherent(dev, size, dma_handle,
-						   flag, attrs);
-#else
 		return dma_direct_alloc(dev, size, dma_handle, flag, attrs);
-#endif
 
 	/* Ok we can't ... do we have an iommu ? If not, fail */
 	iommu = get_iommu_table_base(dev);
@@ -62,12 +57,7 @@ static void dma_nommu_free_coherent(struct device *dev, size_t size,
 
 	/* See comments in dma_nommu_alloc_coherent() */
 	if (dma_direct_supported(dev, dev->coherent_dma_mask))
-#ifdef CONFIG_NOT_COHERENT_CACHE
-		return __dma_nommu_free_coherent(dev, size, vaddr, dma_handle,
-						  attrs);
-#else
 		return dma_direct_free(dev, size, vaddr, dma_handle, attrs);
-#endif
 
 	/* Maybe we used an iommu ... */
 	iommu = get_iommu_table_base(dev);
@@ -84,14 +74,8 @@ int dma_nommu_mmap_coherent(struct device *dev, struct vm_area_struct *vma,
 			     void *cpu_addr, dma_addr_t handle, size_t size,
 			     unsigned long attrs)
 {
-	unsigned long pfn;
+	unsigned long pfn = page_to_pfn(virt_to_page(cpu_addr));
 
-#ifdef CONFIG_NOT_COHERENT_CACHE
-	vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
-	pfn = __dma_get_coherent_pfn((unsigned long)cpu_addr);
-#else
-	pfn = page_to_pfn(virt_to_page(cpu_addr));
-#endif
 	return remap_pfn_range(vma, vma->vm_start,
 			       pfn + vma->vm_pgoff,
 			       vma->vm_end - vma->vm_start,
@@ -108,17 +92,13 @@ static int dma_nommu_map_sg(struct device *dev, struct scatterlist *sgl,
 	for_each_sg(sgl, sg, nents, i) {
 		sg->dma_address = phys_to_dma(dev, sg_phys(sg));
 		sg->dma_length = sg->length;
-
-		if (attrs & DMA_ATTR_SKIP_CPU_SYNC)
-			continue;
-
-		__dma_sync_page(sg_page(sg), sg->offset, sg->length, direction);
 	}
 
 	return nents;
 }
 
-static u64 dma_nommu_get_required_mask(struct device *dev)
+/* note: needs to be called arch_get_required_mask for dma-noncoherent.c */
+u64 arch_get_required_mask(struct device *dev)
 {
 	u64 end, mask;
 
@@ -137,32 +117,9 @@ static inline dma_addr_t dma_nommu_map_page(struct device *dev,
 					     enum dma_data_direction dir,
 					     unsigned long attrs)
 {
-	if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC))
-		__dma_sync_page(page, offset, size, dir);
-
 	return phys_to_dma(dev, page_to_phys(page)) + offset;
 }
 
-#ifdef CONFIG_NOT_COHERENT_CACHE
-static inline void dma_nommu_sync_sg(struct device *dev,
-		struct scatterlist *sgl, int nents,
-		enum dma_data_direction direction)
-{
-	struct scatterlist *sg;
-	int i;
-
-	for_each_sg(sgl, sg, nents, i)
-		__dma_sync_page(sg_page(sg), sg->offset, sg->length, direction);
-}
-
-static inline void dma_nommu_sync_single(struct device *dev,
-					  dma_addr_t dma_handle, size_t size,
-					  enum dma_data_direction direction)
-{
-	__dma_sync(bus_to_virt(dma_handle), size, direction);
-}
-#endif
-
 const struct dma_map_ops dma_nommu_ops = {
 	.alloc				= dma_nommu_alloc_coherent,
 	.free				= dma_nommu_free_coherent,
@@ -170,15 +127,10 @@ const struct dma_map_ops dma_nommu_ops = {
 	.map_sg				= dma_nommu_map_sg,
 	.dma_supported			= dma_direct_supported,
 	.map_page			= dma_nommu_map_page,
-	.get_required_mask		= dma_nommu_get_required_mask,
-#ifdef CONFIG_NOT_COHERENT_CACHE
-	.sync_single_for_cpu 		= dma_nommu_sync_single,
-	.sync_single_for_device 	= dma_nommu_sync_single,
-	.sync_sg_for_cpu 		= dma_nommu_sync_sg,
-	.sync_sg_for_device 		= dma_nommu_sync_sg,
-#endif
+	.get_required_mask		= arch_get_required_mask,
 };
 
+#ifndef CONFIG_NOT_COHERENT_CACHE
 int dma_set_coherent_mask(struct device *dev, u64 mask)
 {
 	if (!dma_supported(dev, mask)) {
@@ -197,6 +149,7 @@ int dma_set_coherent_mask(struct device *dev, u64 mask)
 	return 0;
 }
 EXPORT_SYMBOL(dma_set_coherent_mask);
+#endif
 
 int dma_set_mask(struct device *dev, u64 dma_mask)
 {
arch/powerpc/kernel/pci-common.c +4 -1
index fe9733ffffaa..898ffb636b75 100644
@@ -59,8 +59,11 @@ static DECLARE_BITMAP(phb_bitmap, MAX_PHBS);
 resource_size_t isa_mem_base;
 EXPORT_SYMBOL(isa_mem_base);
 
-
+#ifdef CONFIG_NOT_COHERENT_CACHE
+static const struct dma_map_ops *pci_dma_ops = &dma_noncoherent_ops;
+#else
 static const struct dma_map_ops *pci_dma_ops = &dma_nommu_ops;
+#endif
 
 void set_pci_dma_ops(const struct dma_map_ops *dma_ops)
 {
arch/powerpc/kernel/setup-common.c +4 -0
index 40b44bb53a4e..2488826fa543 100644
@@ -792,7 +792,11 @@ void arch_setup_pdev_archdata(struct platform_device *pdev)
 {
 	pdev->archdata.dma_mask = DMA_BIT_MASK(32);
 	pdev->dev.dma_mask = &pdev->archdata.dma_mask;
+#ifdef CONFIG_NOT_COHERENT_CACHE
+	set_dma_ops(&pdev->dev, &dma_noncoherent_ops);
+#else
  	set_dma_ops(&pdev->dev, &dma_nommu_ops);
+#endif
 }
 
 static __init void print_system_info(void)
arch/powerpc/mm/dma-noncoherent.c +39 -13
index cfc48a253707..1ceea32c0112 100644
@@ -30,6 +30,7 @@
 #include <linux/types.h>
 #include <linux/highmem.h>
 #include <linux/dma-direct.h>
+#include <linux/dma-noncoherent.h>
 #include <linux/export.h>
 
 #include <asm/tlbflush.h>
@@ -151,8 +152,8 @@ static struct ppc_vm_region *ppc_vm_region_find(struct ppc_vm_region *head, unsi
  * Allocate DMA-coherent memory space and return both the kernel remapped
  * virtual and bus address for that space.
  */
-void *__dma_nommu_alloc_coherent(struct device *dev, size_t size,
-		dma_addr_t *dma_handle, gfp_t gfp, unsigned long attrs)
+void *arch_dma_alloc(struct device *dev, size_t size, dma_addr_t *dma_handle,
+		gfp_t gfp, unsigned long attrs)
 {
 	struct page *page;
 	struct ppc_vm_region *c;
@@ -253,7 +254,7 @@ void *__dma_nommu_alloc_coherent(struct device *dev, size_t size,
 /*
  * free a page as defined by the above mapping.
  */
-void __dma_nommu_free_coherent(struct device *dev, size_t size, void *vaddr,
+void arch_dma_free(struct device *dev, size_t size, void *vaddr,
 		dma_addr_t dma_handle, unsigned long attrs)
 {
 	struct ppc_vm_region *c;
@@ -313,7 +314,7 @@ void __dma_nommu_free_coherent(struct device *dev, size_t size, void *vaddr,
 /*
  * make an area consistent.
  */
-void __dma_sync(void *vaddr, size_t size, int direction)
+static void __dma_sync(void *vaddr, size_t size, int direction)
 {
 	unsigned long start = (unsigned long)vaddr;
 	unsigned long end   = start + size;
@@ -339,7 +340,6 @@ void __dma_sync(void *vaddr, size_t size, int direction)
 		break;
 	}
 }
-EXPORT_SYMBOL(__dma_sync);
 
 #ifdef CONFIG_HIGHMEM
 /*
@@ -382,23 +382,36 @@ static inline void __dma_sync_page_highmem(struct page *page,
  * __dma_sync_page makes memory consistent. identical to __dma_sync, but
  * takes a struct page instead of a virtual address
  */
-void __dma_sync_page(struct page *page, unsigned long offset,
-	size_t size, int direction)
+static void __dma_sync_page(phys_addr_t paddr, size_t size, int dir)
 {
+	struct page *page = pfn_to_page(paddr >> PAGE_SHIFT);
+	unsigned offset = paddr & ~PAGE_MASK;
+
 #ifdef CONFIG_HIGHMEM
-	__dma_sync_page_highmem(page, offset, size, direction);
+	__dma_sync_page_highmem(page, offset, size, dir);
 #else
 	unsigned long start = (unsigned long)page_address(page) + offset;
-	__dma_sync((void *)start, size, direction);
+	__dma_sync((void *)start, size, dir);
 #endif
 }
-EXPORT_SYMBOL(__dma_sync_page);
+
+void arch_sync_dma_for_device(struct device *dev, phys_addr_t paddr,
+		size_t size, enum dma_data_direction dir)
+{
+	__dma_sync_page(paddr, size, dir);
+}
+
+void arch_sync_dma_for_cpu(struct device *dev, phys_addr_t paddr,
+		size_t size, enum dma_data_direction dir)
+{
+	__dma_sync_page(paddr, size, dir);
+}
 
 /*
- * Return the PFN for a given cpu virtual address returned by
- * __dma_nommu_alloc_coherent. This is used by dma_mmap_coherent()
+ * Return the PFN for a given cpu virtual address returned by __arch_dma_alloc.
+ * This is used by dma_mmap_coherent()
  */
-unsigned long __dma_get_coherent_pfn(unsigned long cpu_addr)
+static unsigned long __dma_get_coherent_pfn(unsigned long cpu_addr)
 {
 	/* This should always be populated, so we don't test every
 	 * level. If that fails, we'll have a nice crash which
@@ -413,3 +426,16 @@ unsigned long __dma_get_coherent_pfn(unsigned long cpu_addr)
 		return 0;
 	return pte_pfn(*ptep);
 }
+
+int arch_dma_mmap(struct device *dev, struct vm_area_struct *vma,
+			     void *cpu_addr, dma_addr_t handle, size_t size,
+			     unsigned long attrs)
+{
+	unsigned long pfn = __dma_get_coherent_pfn((unsigned long)cpu_addr);
+
+	vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
+	return remap_pfn_range(vma, vma->vm_start,
+			       pfn + vma->vm_pgoff,
+			       vma->vm_end - vma->vm_start,
+			       vma->vm_page_prot);
+}
arch/powerpc/platforms/44x/warp.c +1 -1
index 7e4f8ca19ce8..c0e6fb270d59 100644
@@ -47,7 +47,7 @@ static int __init warp_probe(void)
 	if (!of_machine_is_compatible("pika,warp"))
 		return 0;
 
-	/* For __dma_nommu_alloc_coherent */
+	/* For arch_dma_alloc */
 	ISA_DMA_THRESHOLD = ~0L;
 
 	return 1;
arch/powerpc/platforms/Kconfig.cputype +5 -1
index a2578bf8d560..9d83f54ccf11 100644
@@ -387,7 +387,11 @@ config NOT_COHERENT_CACHE
 	depends on 4xx || PPC_8xx || E200 || PPC_MPC512x || GAMECUBE_COMMON
 	default n if PPC_47x
 	default y
-	select NEED_DMA_MAP_STATE
+	select ARCH_HAS_SYNC_DMA_FOR_DEVICE
+	select ARCH_HAS_SYNC_DMA_FOR_CPU
+	select DMA_NONCOHERENT_GET_REQUIRED
+	select DMA_NONCOHERENT_MMAP
+	select DMA_NONCOHERENT_OPS
 
 config CHECK_CACHE_COHERENCY
 	bool
#49 lore raw

PATCH 19/20 powerpc/dma: use the generic dma-direct map_page and map_sg routines

In reply to Christoph Hellwig

These are indentical except for additional error checking, so migrate to the common code, and wire up the get_mapping_error method as well.

---
 arch/powerpc/kernel/dma.c | 32 ++++----------------------------
 1 file changed, 4 insertions(+), 28 deletions(-)
arch/powerpc/kernel/dma.c +4 -28
index b2e88075b2ea..08b12cbd7abf 100644
@@ -82,21 +82,6 @@ int dma_nommu_mmap_coherent(struct device *dev, struct vm_area_struct *vma,
 			       vma->vm_page_prot);
 }
 
-static int dma_nommu_map_sg(struct device *dev, struct scatterlist *sgl,
-			     int nents, enum dma_data_direction direction,
-			     unsigned long attrs)
-{
-	struct scatterlist *sg;
-	int i;
-
-	for_each_sg(sgl, sg, nents, i) {
-		sg->dma_address = phys_to_dma(dev, sg_phys(sg));
-		sg->dma_length = sg->length;
-	}
-
-	return nents;
-}
-
 /* note: needs to be called arch_get_required_mask for dma-noncoherent.c */
 u64 arch_get_required_mask(struct device *dev)
 {
@@ -110,24 +95,15 @@ u64 arch_get_required_mask(struct device *dev)
 	return mask;
 }
 
-static inline dma_addr_t dma_nommu_map_page(struct device *dev,
-					     struct page *page,
-					     unsigned long offset,
-					     size_t size,
-					     enum dma_data_direction dir,
-					     unsigned long attrs)
-{
-	return phys_to_dma(dev, page_to_phys(page)) + offset;
-}
-
 const struct dma_map_ops dma_nommu_ops = {
 	.alloc				= dma_nommu_alloc_coherent,
 	.free				= dma_nommu_free_coherent,
 	.mmap				= dma_nommu_mmap_coherent,
-	.map_sg				= dma_nommu_map_sg,
-	.dma_supported			= dma_direct_supported,
-	.map_page			= dma_nommu_map_page,
+	.map_sg				= dma_direct_map_sg,
+	.map_page			= dma_direct_map_page,
 	.get_required_mask		= arch_get_required_mask,
+	.dma_supported			= dma_direct_supported,
+	.mapping_error			= dma_direct_mapping_error,
 };
 
 #ifndef CONFIG_NOT_COHERENT_CACHE
Signature · 1 line
2.18.0
#50 lore raw

PATCH 20/20 powerpc/dma: remove dma_nommu_mmap_coherent

In reply to Christoph Hellwig

The remaining implementation for coherent caches is functionally identical to the default provided in common code.

---
 arch/powerpc/include/asm/dma-mapping.h |  7 -------
 arch/powerpc/kernel/dma-iommu.c        |  1 -
 arch/powerpc/kernel/dma.c              | 13 -------------
 arch/powerpc/platforms/pseries/vio.c   |  1 -
 4 files changed, 22 deletions(-)
arch/powerpc/include/asm/dma-mapping.h +0 -7
index 879c4efba785..e62e23aa3714 100644
@@ -18,13 +18,6 @@
 #include <asm/io.h>
 #include <asm/swiotlb.h>
 
-/* Some dma direct funcs must be visible for use in other dma_ops */
-extern int dma_nommu_mmap_coherent(struct device *dev,
-				    struct vm_area_struct *vma,
-				    void *cpu_addr, dma_addr_t handle,
-				    size_t size, unsigned long attrs);
-
-
 static inline unsigned long device_to_mask(struct device *dev)
 {
 	if (dev->dma_mask && *dev->dma_mask)
arch/powerpc/kernel/dma-iommu.c +0 -1
index f9fe2080ceb9..bf5234e1f71b 100644
@@ -114,7 +114,6 @@ int dma_iommu_mapping_error(struct device *dev, dma_addr_t dma_addr)
 struct dma_map_ops dma_iommu_ops = {
 	.alloc			= dma_iommu_alloc_coherent,
 	.free			= dma_iommu_free_coherent,
-	.mmap			= dma_nommu_mmap_coherent,
 	.map_sg			= dma_iommu_map_sg,
 	.unmap_sg		= dma_iommu_unmap_sg,
 	.dma_supported		= dma_iommu_dma_supported,
arch/powerpc/kernel/dma.c +0 -13
index 08b12cbd7abf..5b71c9d1b8cc 100644
@@ -70,18 +70,6 @@ static void dma_nommu_free_coherent(struct device *dev, size_t size,
 	iommu_free_coherent(iommu, size, vaddr, dma_handle);
 }
 
-int dma_nommu_mmap_coherent(struct device *dev, struct vm_area_struct *vma,
-			     void *cpu_addr, dma_addr_t handle, size_t size,
-			     unsigned long attrs)
-{
-	unsigned long pfn = page_to_pfn(virt_to_page(cpu_addr));
-
-	return remap_pfn_range(vma, vma->vm_start,
-			       pfn + vma->vm_pgoff,
-			       vma->vm_end - vma->vm_start,
-			       vma->vm_page_prot);
-}
-
 /* note: needs to be called arch_get_required_mask for dma-noncoherent.c */
 u64 arch_get_required_mask(struct device *dev)
 {
@@ -98,7 +86,6 @@ u64 arch_get_required_mask(struct device *dev)
 const struct dma_map_ops dma_nommu_ops = {
 	.alloc				= dma_nommu_alloc_coherent,
 	.free				= dma_nommu_free_coherent,
-	.mmap				= dma_nommu_mmap_coherent,
 	.map_sg				= dma_direct_map_sg,
 	.map_page			= dma_direct_map_page,
 	.get_required_mask		= arch_get_required_mask,
arch/powerpc/platforms/pseries/vio.c +0 -1
index 49e04ec19238..51d564313bd0 100644
@@ -618,7 +618,6 @@ static u64 vio_dma_get_required_mask(struct device *dev)
 static const struct dma_map_ops vio_dma_mapping_ops = {
 	.alloc             = vio_dma_iommu_alloc_coherent,
 	.free              = vio_dma_iommu_free_coherent,
-	.mmap		   = dma_nommu_mmap_coherent,
 	.map_sg            = vio_dma_iommu_map_sg,
 	.unmap_sg          = vio_dma_iommu_unmap_sg,
 	.map_page          = vio_dma_iommu_map_page,
Signature · 1 line
2.18.0
Acked-by on v1 20/20
#51 lore raw

Re: [PATCH 20/20] powerpc/dma: remove dma_nommu_mmap_coherent

Quoted 4 lines · Christoph Hellwig

The remaining implementation for coherent caches is functionally identical to the default provided in common code.

Quoted 80 lines
---
 arch/powerpc/include/asm/dma-mapping.h |  7 -------
 arch/powerpc/kernel/dma-iommu.c        |  1 -
 arch/powerpc/kernel/dma.c              | 13 -------------
 arch/powerpc/platforms/pseries/vio.c   |  1 -
 4 files changed, 22 deletions(-)
arch/powerpc/include/asm/dma-mapping.h +0 -7
index 879c4efba785..e62e23aa3714 100644
@@ -18,13 +18,6 @@
 #include <asm/io.h>
 #include <asm/swiotlb.h>
 
-/* Some dma direct funcs must be visible for use in other dma_ops */
-extern int dma_nommu_mmap_coherent(struct device *dev,
-				    struct vm_area_struct *vma,
-				    void *cpu_addr, dma_addr_t handle,
-				    size_t size, unsigned long attrs);
-
-
 static inline unsigned long device_to_mask(struct device *dev)
 {
 	if (dev->dma_mask && *dev->dma_mask)
arch/powerpc/kernel/dma-iommu.c +0 -1
index f9fe2080ceb9..bf5234e1f71b 100644
@@ -114,7 +114,6 @@ int dma_iommu_mapping_error(struct device *dev, dma_addr_t dma_addr)
 struct dma_map_ops dma_iommu_ops = {
 	.alloc			= dma_iommu_alloc_coherent,
 	.free			= dma_iommu_free_coherent,
-	.mmap			= dma_nommu_mmap_coherent,
 	.map_sg			= dma_iommu_map_sg,
 	.unmap_sg		= dma_iommu_unmap_sg,
 	.dma_supported		= dma_iommu_dma_supported,
arch/powerpc/kernel/dma.c +0 -13
index 08b12cbd7abf..5b71c9d1b8cc 100644
@@ -70,18 +70,6 @@ static void dma_nommu_free_coherent(struct device *dev, size_t size,
 	iommu_free_coherent(iommu, size, vaddr, dma_handle);
 }
 
-int dma_nommu_mmap_coherent(struct device *dev, struct vm_area_struct *vma,
-			     void *cpu_addr, dma_addr_t handle, size_t size,
-			     unsigned long attrs)
-{
-	unsigned long pfn = page_to_pfn(virt_to_page(cpu_addr));
-
-	return remap_pfn_range(vma, vma->vm_start,
-			       pfn + vma->vm_pgoff,
-			       vma->vm_end - vma->vm_start,
-			       vma->vm_page_prot);
-}
-
 /* note: needs to be called arch_get_required_mask for dma-noncoherent.c */
 u64 arch_get_required_mask(struct device *dev)
 {
@@ -98,7 +86,6 @@ u64 arch_get_required_mask(struct device *dev)
 const struct dma_map_ops dma_nommu_ops = {
 	.alloc				= dma_nommu_alloc_coherent,
 	.free				= dma_nommu_free_coherent,
-	.mmap				= dma_nommu_mmap_coherent,
 	.map_sg				= dma_direct_map_sg,
 	.map_page			= dma_direct_map_page,
 	.get_required_mask		= arch_get_required_mask,
arch/powerpc/platforms/pseries/vio.c +0 -1
index 49e04ec19238..51d564313bd0 100644
@@ -618,7 +618,6 @@ static u64 vio_dma_get_required_mask(struct device *dev)
 static const struct dma_map_ops vio_dma_mapping_ops = {
 	.alloc             = vio_dma_iommu_alloc_coherent,
 	.free              = vio_dma_iommu_free_coherent,
-	.mmap		   = dma_nommu_mmap_coherent,
 	.map_sg            = vio_dma_iommu_map_sg,
 	.unmap_sg          = vio_dma_iommu_unmap_sg,
 	.map_page          = vio_dma_iommu_map_page,