Skip to contentSkip to conversation

arm64: do not set dma masks that device connection can't handle

PATCH In review Nikita Yushchenko 6 participants 38 messages Jan 9, 2017 – Jan 13, 2017

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

PATCH v2 1/1 arm64: do not set dma masks that device connection can't handle

It is possible that device is capable of 64-bit DMA addresses, and device driver tries to set wide DMA mask, but bridge or bus used to connect device to the system can't handle wide addresses.

With swiotlb, memory above 4G still can be used by drivers for streaming DMA, but *dev->mask and dev->dma_coherent_mask must still keep values that hardware handles physically.

This patch enforces that. Based on original version by Arnd Bergmann <[email protected]>, extended with coherent mask hadnling.

---
Changes since v1:
- fixed issues noted by Sergei Shtylyov <[email protected]>
- save mask, not size
- remove doube empty line

 arch/arm64/Kconfig              |  3 +++
 arch/arm64/include/asm/device.h |  1 +
 arch/arm64/mm/dma-mapping.c     | 51 +++++++++++++++++++++++++++++++++++++++++
 3 files changed, 55 insertions(+)
arch/arm64/Kconfig +3 -0
index 1117421..afb2c08 100644
@@ -216,6 +216,9 @@ config NEED_DMA_MAP_STATE
 config NEED_SG_DMA_LENGTH
 	def_bool y
 
+config ARCH_HAS_DMA_SET_COHERENT_MASK
+	def_bool y
+
 config SMP
 	def_bool y
 
arch/arm64/include/asm/device.h +1 -0
index 243ef25..a57e7bb 100644
@@ -22,6 +22,7 @@ struct dev_archdata {
 	void *iommu;			/* private IOMMU data */
 #endif
 	bool dma_coherent;
+	u64 parent_dma_mask;
 };
 
 struct pdev_archdata {
arch/arm64/mm/dma-mapping.c +51 -0

71 lines not on this page · raw message

Signature · 1 line
2.1.4
#1 lore raw
Quoted 19 lines · Nikita Yushchenko

It is possible that device is capable of 64-bit DMA addresses, and device driver tries to set wide DMA mask, but bridge or bus used to connect device to the system can't handle wide addresses.

With swiotlb, memory above 4G still can be used by drivers for streaming DMA, but *dev->mask and dev->dma_coherent_mask must still keep values that hardware handles physically.

This patch enforces that. Based on original version by Arnd Bergmann <[email protected]>, extended with coherent mask hadnling.

---
Changes since v1:
- fixed issues noted by Sergei Shtylyov <[email protected]>
- save mask, not size
- remove doube empty line

Quoted 4 lines
 arch/arm64/Kconfig              |  3 +++
 arch/arm64/include/asm/device.h |  1 +
 arch/arm64/mm/dma-mapping.c     | 51 +++++++++++++++++++++++++++++++++++++++++
 3 files changed, 55 insertions(+)

I still don't think this patch is general enough. The problem you're seeing with swiotlb seems to be exactly the same problem reported by Feng Kan over at:

http://lkml.kernel.org/r/CAL85gmA_SSCwM80TKdkZqEe+S1beWzDEvdki1kpkmUTDRmSP7g at mail.gmail.com

[read on; it was initially thought to be a hardware erratum, but it's
actually the inability to restrict the DMA mask of the endpoint that's the problem]

The point here is that an IOMMU doesn't solve your issue, and the IOMMU-backed DMA ops need the same treatment. In light of that, it really feels to me like the DMA masks should be restricted in of_dma_configure so that the parent mask is taken into account there, rather than hook into each set of DMA ops to intercept set_dma_mask. We'd still need to do something to stop dma_set_mask widening the mask if it was restricted by of_dma_configure, but I think Robin (cc'd) was playing with that.

Will

#2 lore raw

Hi

Quoted 7 lines

The point here is that an IOMMU doesn't solve your issue, and the IOMMU-backed DMA ops need the same treatment. In light of that, it really feels to me like the DMA masks should be restricted in of_dma_configure so that the parent mask is taken into account there, rather than hook into each set of DMA ops to intercept set_dma_mask. We'd still need to do something to stop dma_set_mask widening the mask if it was restricted by of_dma_configure, but I think Robin (cc'd) was playing with that.

What issue "IOMMU doesn't solve"?

Issue I'm trying to address is - inconsistency within swiotlb dma_map_ops, where (1) any wide mask is silently accepted, but (2) then mask is used to decide if bounce buffers are needed or not. This inconsistency causes NVMe+R-Car cobmo not working (and breaking memory instead).

I just can't think out what similar issue iommu can have.
Do you mean that in iommu case, mask also must not be set to whatever wider than initial value? Why? What is the use of mask in iommu case? Is there any real case when iommu can't address all memory existing in the system?

NVMe maintainer has just stated that they expect set_dma_mask(DMA_BIT_MASK(64)) to always succeed, and are going to error out driver probe if that call fails. They claim that architecture must always be able to dma_map() whatever memory existing in the system - via iommu or swiotlb or whatever. Their direction is to remove bounce buffers from block and other layers.

With this direction, semantics of dma mask becomes even more questionable. I'd say dma_mask is candidate for removal (or to move to swiotlb's or iommu's local area)

Nikita

#3 lore raw
Quoted 6 lines · CET Nikita Yushchenko
Quoted 6 lines

The point here is that an IOMMU doesn't solve your issue, and the IOMMU-backed DMA ops need the same treatment. In light of that, it really feels to me like the DMA masks should be restricted in of_dma_configure so that the parent mask is taken into account there, rather than hook into each set of DMA ops to intercept set_dma_mask.

of_dma_configure() sets up a 32-bit mask, which is assumed to always work in the kernel. We can't change it to a larger mask because that would break drivers that have to restrict devices to 32-bit.

If the bus addressing is narrower than 32 bits however, the initial mask should probably be limited to whatever the bus supports, but that is not the problem we are trying to solve here.

Quoted 11 lines
Quoted 3 lines

We'd still need to
do something to stop dma_set_mask widening the mask if it was restricted by of_dma_configure, but I think Robin (cc'd) was playing with that.

What issue "IOMMU doesn't solve"?

Issue I'm trying to address is - inconsistency within swiotlb dma_map_ops, where (1) any wide mask is silently accepted, but (2) then mask is used to decide if bounce buffers are needed or not. This inconsistency causes NVMe+R-Car cobmo not working (and breaking memory instead).

It's not just an inconsistency, it's a known bug that we really need to fix.

Quoted 5 lines

I just can't think out what similar issue iommu can have.
Do you mean that in iommu case, mask also must not be set to whatever wider than initial value? Why? What is the use of mask in iommu case? Is there any real case when iommu can't address all memory existing in the system?

I think the problem that Will is referring to is when the IOMMU has a virtual address space that is wider than the DMA mask of the device:
In this case, dma_map_single() might return a dma_addr_t that is not reachable by the device.

I'd consider that a separate bug that needs to be worked around in the IOMMU code.

Quoted 10 lines

NVMe maintainer has just stated that they expect set_dma_mask(DMA_BIT_MASK(64)) to always succeed, and are going to error out driver probe if that call fails. They claim that architecture must always be able to dma_map() whatever memory existing in the system - via iommu or swiotlb or whatever. Their direction is to remove bounce buffers from block and other layers.

With this direction, semantics of dma mask becomes even more questionable. I'd say dma_mask is candidate for removal (or to move to swiotlb's or iommu's local area)

Removing dma_mask is not realistic any time soon, there are too many things in the kernel that use it for one thing or another, so any changes here have to be done really carefully. We definitely need the mask to support architectures without swiotlb.

Arnd

reviewer
#4 lore raw
In reply to Nikita Yushchenko
Quoted 10 lines · Nikita Yushchenko

Hi

Quoted 7 lines

The point here is that an IOMMU doesn't solve your issue, and the IOMMU-backed DMA ops need the same treatment. In light of that, it really feels to me like the DMA masks should be restricted in of_dma_configure so that the parent mask is taken into account there, rather than hook into each set of DMA ops to intercept set_dma_mask. We'd still need to do something to stop dma_set_mask widening the mask if it was restricted by of_dma_configure, but I think Robin (cc'd) was playing with that.

Quoted 7 lines

What issue "IOMMU doesn't solve"?

Issue I'm trying to address is - inconsistency within swiotlb dma_map_ops, where (1) any wide mask is silently accepted, but (2) then mask is used to decide if bounce buffers are needed or not. This inconsistency causes NVMe+R-Car cobmo not working (and breaking memory instead).

The fundamental underlying problem is the "any wide mask is silently accepted" part, and that applies equally to IOMMU ops as well.

Quoted 5 lines

I just can't think out what similar issue iommu can have.
Do you mean that in iommu case, mask also must not be set to whatever wider than initial value? Why? What is the use of mask in iommu case? Is there any real case when iommu can't address all memory existing in the system?

There's a very subtle misunderstanding there - the DMA mask does not describe the memory a device can address, it describes the range of addresses the device is capable of generating. Yes, in the non-IOMMU case they are equivalent, but once you put an IOMMU in between, the problem is merely shifted from "what range of physical addresses can this device access" to "what range of IOVAs is valid to give to this device" - the fact that those IOVAs can map to any underlying physical address only obviates the need for any bouncing at the memory end; it doesn't remove the fact that the device has a hardware addressing limitation which needs to be accommodated.

The thread Will linked to describes that equivalent version of your problem - the IOMMU gives the device 48-bit addresses which get erroneously truncated because it doesn't know that only 42 bits are actually wired up. That situation still requires the device's DMA mask to correctly describe its addressing capability just as yours does.

Quoted 6 lines

NVMe maintainer has just stated that they expect set_dma_mask(DMA_BIT_MASK(64)) to always succeed, and are going to error out driver probe if that call fails. They claim that architecture must always be able to dma_map() whatever memory existing in the system - via iommu or swiotlb or whatever. Their direction is to remove bounce buffers from block and other layers.

I have to say I'm in full agreement with that - having subsystems do their own bouncing is generally redundant, although there are some edge-cases like MMC_BLOCK_BOUNCE (which is primarily about aligning and coalescing buffers than working around DMA limitations per se).

Quoted 3 lines

With this direction, semantics of dma mask becomes even more questionable. I'd say dma_mask is candidate for removal (or to move to swiotlb's or iommu's local area)

We still need a way for drivers to communicate a device's probed addressing capability to SWIOTLB, so there's always going to have to be *some* sort of public interface. Personally, the change in semantics I'd like to see is to make dma_set_mask() only fail if DMA is entirely disallowed - in the normal case it would always succeed, but the DMA API implementation would be permitted to set a smaller mask than requested (this is effectively what the x86 IOMMU ops do already). The significant work that would require, though, is changing all the drivers currently using this sort of pattern:

	if (!dma_set_mask(dev, DMA_BIT_MASK(64))
		/* put device into 64-bit mode */
	else if (!dma_set_mask(dev, DMA_BIT_MASK(32))
		/* put device into 32-bit mode */
	else
		/* error */

to something like this:

	if (!dma_set_mask(dev, DMA_BIT_MASK(64))
		/* error */
	if (dma_get_mask(dev) > DMA_BIT_MASK(32))
		/* put device into 64-bit mode */
	else
		/* put device into 32-bit mode */

Which would be a pretty major job.

Robin.

#5 lore raw
Quoted 11 lines · CET Robin Murphy
Quoted 10 lines · Nikita Yushchenko
Quoted 7 lines

The point here is that an IOMMU doesn't solve your issue, and the IOMMU-backed DMA ops need the same treatment. In light of that, it really feels to me like the DMA masks should be restricted in of_dma_configure so that the parent mask is taken into account there, rather than hook into each set of DMA ops to intercept set_dma_mask. We'd still need to do something to stop dma_set_mask widening the mask if it was restricted by of_dma_configure, but I think Robin (cc'd) was playing with that.

What issue "IOMMU doesn't solve"?

Quoted 8 lines
Quoted 5 lines

Issue I'm trying to address is - inconsistency within swiotlb dma_map_ops, where (1) any wide mask is silently accepted, but (2) then mask is used to decide if bounce buffers are needed or not. This inconsistency causes NVMe+R-Car cobmo not working (and breaking memory instead).

The fundamental underlying problem is the "any wide mask is silently accepted" part, and that applies equally to IOMMU ops as well.

It's a much rarer problem for the IOMMU case though, because it only impacts devices that are restricted to addressing of less than 32-bits.

If you have an IOMMU enabled, the dma-mapping interface does not care if the device can do wider than 32 bit addressing, as it will never hand out IOVAs above 0xffffffff.

Quoted 17 lines
Quoted 5 lines

I just can't think out what similar issue iommu can have.
Do you mean that in iommu case, mask also must not be set to whatever wider than initial value? Why? What is the use of mask in iommu case? Is there any real case when iommu can't address all memory existing in the system?

There's a very subtle misunderstanding there - the DMA mask does not describe the memory a device can address, it describes the range of addresses the device is capable of generating. Yes, in the non-IOMMU case they are equivalent, but once you put an IOMMU in between, the problem is merely shifted from "what range of physical addresses can this device access" to "what range of IOVAs is valid to give to this device" - the fact that those IOVAs can map to any underlying physical address only obviates the need for any bouncing at the memory end; it doesn't remove the fact that the device has a hardware addressing limitation which needs to be accommodated.

Quoted 5 lines

The thread Will linked to describes that equivalent version of your problem - the IOMMU gives the device 48-bit addresses which get erroneously truncated because it doesn't know that only 42 bits are actually wired up. That situation still requires the device's DMA mask to correctly describe its addressing capability just as yours does.

That problem should only impact virtual machines which have a guest bus address space covering more than 42 bits of physical RAM, whereas the problem we have with swiotlb is for the dma-mapping interface.

Quoted 11 lines
Quoted 3 lines

With this direction, semantics of dma mask becomes even more questionable. I'd say dma_mask is candidate for removal (or to move to swiotlb's or iommu's local area)

We still need a way for drivers to communicate a device's probed addressing capability to SWIOTLB, so there's always going to have to be *some* sort of public interface. Personally, the change in semantics I'd like to see is to make dma_set_mask() only fail if DMA is entirely disallowed - in the normal case it would always succeed, but the DMA API implementation would be permitted to set a smaller mask than requested (this is effectively what the x86 IOMMU ops do already).

With swiotlb enabled, it only needs to fail if the mask does not contain the swiotlb bounce buffer area, either because the start of RAM is outside of the mask, or the bounce area has been allocated at the end of ZONE_DMA and the mask is smaller than ZONE_DMA.

Arnd

reviewer
#6 lore raw
Quoted 21 lines · Arnd Bergmann
Quoted 11 lines · CET Robin Murphy
Quoted 10 lines · Nikita Yushchenko
Quoted 7 lines

The point here is that an IOMMU doesn't solve your issue, and the IOMMU-backed DMA ops need the same treatment. In light of that, it really feels to me like the DMA masks should be restricted in of_dma_configure so that the parent mask is taken into account there, rather than hook into each set of DMA ops to intercept set_dma_mask. We'd still need to do something to stop dma_set_mask widening the mask if it was restricted by of_dma_configure, but I think Robin (cc'd) was playing with that.

What issue "IOMMU doesn't solve"?

Quoted 8 lines
Quoted 5 lines

Issue I'm trying to address is - inconsistency within swiotlb dma_map_ops, where (1) any wide mask is silently accepted, but (2) then mask is used to decide if bounce buffers are needed or not. This inconsistency causes NVMe+R-Car cobmo not working (and breaking memory instead).

The fundamental underlying problem is the "any wide mask is silently accepted" part, and that applies equally to IOMMU ops as well.

Quoted 6 lines

It's a much rarer problem for the IOMMU case though, because it only impacts devices that are restricted to addressing of less than 32-bits.

If you have an IOMMU enabled, the dma-mapping interface does not care if the device can do wider than 32 bit addressing, as it will never hand out IOVAs above 0xffffffff.

I can assure you that it will - we constrain allocations to the intersection of the IOMMU domain aperture (normally the IOMMU's physical input address width) and the given device's DMA mask. If both of those are >32 bits then >32-bit IOVAs will fall out. For the arm64/common implementation I have prototyped a copy of the x86 optimisation which always first tries to get 32-bit IOVAs for PCI devices, but even then it can start returning higher addresses if the 32-bit space fills up.

Quoted 23 lines
Quoted 17 lines
Quoted 5 lines

I just can't think out what similar issue iommu can have.
Do you mean that in iommu case, mask also must not be set to whatever wider than initial value? Why? What is the use of mask in iommu case? Is there any real case when iommu can't address all memory existing in the system?

There's a very subtle misunderstanding there - the DMA mask does not describe the memory a device can address, it describes the range of addresses the device is capable of generating. Yes, in the non-IOMMU case they are equivalent, but once you put an IOMMU in between, the problem is merely shifted from "what range of physical addresses can this device access" to "what range of IOVAs is valid to give to this device" - the fact that those IOVAs can map to any underlying physical address only obviates the need for any bouncing at the memory end; it doesn't remove the fact that the device has a hardware addressing limitation which needs to be accommodated.

Quoted 5 lines

The thread Will linked to describes that equivalent version of your problem - the IOMMU gives the device 48-bit addresses which get erroneously truncated because it doesn't know that only 42 bits are actually wired up. That situation still requires the device's DMA mask to correctly describe its addressing capability just as yours does.

Quoted 3 lines

That problem should only impact virtual machines which have a guest bus address space covering more than 42 bits of physical RAM, whereas the problem we have with swiotlb is for the dma-mapping interface.

As above, it impacts DMA API use for anything whose addressing capability is narrower than the IOMMU's reported input size and whose driver is able to blindly set a too-big DMA mask. It just happens to be the case that the stars line up on most systems, and for 32-bit devices who keep the default DMA mask.

I actually have a third variation of this problem involving a PCI root complex which *could* drive full-width (40-bit) addresses, but won't, due to the way its PCI<->AXI interface is programmed. That would require even more complicated dma-ranges handling to describe the windows of valid physical addresses which it *will* pass, so I'm not pressing the issue - let's just get the basic DMA mask case fixed first.

Quoted 12 lines
Quoted 11 lines
Quoted 3 lines

With this direction, semantics of dma mask becomes even more questionable. I'd say dma_mask is candidate for removal (or to move to swiotlb's or iommu's local area)

We still need a way for drivers to communicate a device's probed addressing capability to SWIOTLB, so there's always going to have to be *some* sort of public interface. Personally, the change in semantics I'd like to see is to make dma_set_mask() only fail if DMA is entirely disallowed - in the normal case it would always succeed, but the DMA API implementation would be permitted to set a smaller mask than requested (this is effectively what the x86 IOMMU ops do already).

Quoted 4 lines

With swiotlb enabled, it only needs to fail if the mask does not contain the swiotlb bounce buffer area, either because the start of RAM is outside of the mask, or the bounce area has been allocated at the end of ZONE_DMA and the mask is smaller than ZONE_DMA.

Agreed, I'd managed to overlook that specific case, but I'd be inclined to consider "impossible" a subset of "disallowed" still :)

Robin.

#7 lore raw
Quoted 29 lines · CET Robin Murphy
Quoted 21 lines · Arnd Bergmann
Quoted 11 lines · CET Robin Murphy
Quoted 10 lines · Nikita Yushchenko
Quoted 7 lines

The point here is that an IOMMU doesn't solve your issue, and the IOMMU-backed DMA ops need the same treatment. In light of that, it really feels to me like the DMA masks should be restricted in of_dma_configure so that the parent mask is taken into account there, rather than hook into each set of DMA ops to intercept set_dma_mask. We'd still need to do something to stop dma_set_mask widening the mask if it was restricted by of_dma_configure, but I think Robin (cc'd) was playing with that.

What issue "IOMMU doesn't solve"?

Quoted 8 lines
Quoted 5 lines

Issue I'm trying to address is - inconsistency within swiotlb dma_map_ops, where (1) any wide mask is silently accepted, but (2) then mask is used to decide if bounce buffers are needed or not. This inconsistency causes NVMe+R-Car cobmo not working (and breaking memory instead).

The fundamental underlying problem is the "any wide mask is silently accepted" part, and that applies equally to IOMMU ops as well.

Quoted 6 lines

It's a much rarer problem for the IOMMU case though, because it only impacts devices that are restricted to addressing of less than 32-bits.

If you have an IOMMU enabled, the dma-mapping interface does not care if the device can do wider than 32 bit addressing, as it will never hand out IOVAs above 0xffffffff.

Quoted 7 lines

I can assure you that it will - we constrain allocations to the intersection of the IOMMU domain aperture (normally the IOMMU's physical input address width) and the given device's DMA mask. If both of those are >32 bits then >32-bit IOVAs will fall out. For the arm64/common implementation I have prototyped a copy of the x86 optimisation which always first tries to get 32-bit IOVAs for PCI devices, but even then it can start returning higher addresses if the 32-bit space fills up.

Ok, got it. I have to admit that most of my knowledge about the internals of IOMMUs is from PowerPC of a few years ago, which couldn't do this at all. I agree that we need to do the same thing on swiotlb and iommu then.

Quoted 10 lines
Quoted 10 lines
Quoted 5 lines

The thread Will linked to describes that equivalent version of your problem - the IOMMU gives the device 48-bit addresses which get erroneously truncated because it doesn't know that only 42 bits are actually wired up. That situation still requires the device's DMA mask to correctly describe its addressing capability just as yours does.

That problem should only impact virtual machines which have a guest bus address space covering more than 42 bits of physical RAM, whereas the problem we have with swiotlb is for the dma-mapping interface.

Quoted 6 lines

I actually have a third variation of this problem involving a PCI root complex which *could* drive full-width (40-bit) addresses, but won't, due to the way its PCI<->AXI interface is programmed. That would require even more complicated dma-ranges handling to describe the windows of valid physical addresses which it *will* pass, so I'm not pressing the issue - let's just get the basic DMA mask case fixed first.

Can you describe this a little more? We should at least try to not make it harder to solve the next problem while solving this one, so I'd like to understand the exact limitation you are hitting there.

Arnd

#8 lore raw
In reply to Robin Murphy
Quoted 6 lines

I actually have a third variation of this problem involving a PCI root complex which *could* drive full-width (40-bit) addresses, but won't, due to the way its PCI<->AXI interface is programmed. That would require even more complicated dma-ranges handling to describe the windows of valid physical addresses which it *will* pass, so I'm not pressing the issue - let's just get the basic DMA mask case fixed first.

R-Car + NVMe is actually not "basic case".

It has PCI<->AXI interface involved.
PCI addresses are 64-bit and controller does handle 64-bit addresses there. Mapping between PCI addresses and AXI addresses is defined. But AXI is 32-bit.

SoC has iommu that probably could be used between PCIe module and RAM. Although AFAIK nobody made that working yet.

Board I work with has 4G of RAM, in 4 banks, located at different parts of wide address space, and only one of them is below 4G. But if iommu is capable of translating addresses such that 4 gigabyte banks map to first 4 gigabytes of address space, then all memory will become available for DMA from PCIe device.

#9 lore raw
Quoted 14 lines · CET Nikita Yushchenko
Quoted 6 lines

I actually have a third variation of this problem involving a PCI root complex which *could* drive full-width (40-bit) addresses, but won't, due to the way its PCI<->AXI interface is programmed. That would require even more complicated dma-ranges handling to describe the windows of valid physical addresses which it *will* pass, so I'm not pressing the issue - let's just get the basic DMA mask case fixed first.

R-Car + NVMe is actually not "basic case".

It has PCI<->AXI interface involved.
PCI addresses are 64-bit and controller does handle 64-bit addresses there. Mapping between PCI addresses and AXI addresses is defined. But AXI is 32-bit.

Quoted 8 lines

SoC has iommu that probably could be used between PCIe module and RAM. Although AFAIK nobody made that working yet.

Board I work with has 4G of RAM, in 4 banks, located at different parts of wide address space, and only one of them is below 4G. But if iommu is capable of translating addresses such that 4 gigabyte banks map to first 4 gigabytes of address space, then all memory will become available for DMA from PCIe device.

You can in theory handle this by defining your own platform specific dma_map_ops, as we used to do in the old days. Unfortunately, the modern way of using the generic IOVA allocation can't handle really it, so it's unclear if the work that would be necessary to support it (and the long term maintenance cost) outweigh the benefits.

The more likely option here is to try harder to get the IOMMU working (or show that it's impossible but make sure the next chip gets it right).

Arnd

reviewer
#10 lore raw
In reply to Nikita Yushchenko
Quoted 8 lines · Nikita Yushchenko
Quoted 6 lines

I actually have a third variation of this problem involving a PCI root complex which *could* drive full-width (40-bit) addresses, but won't, due to the way its PCI<->AXI interface is programmed. That would require even more complicated dma-ranges handling to describe the windows of valid physical addresses which it *will* pass, so I'm not pressing the issue - let's just get the basic DMA mask case fixed first.

R-Car + NVMe is actually not "basic case".

I meant "basic" in terms of what needs to be done in Linux - simply preventing device drivers from overwriting the DT-configured DMA mask will make everything work as well as well as it possibly can on R-Car, both with or without the IOMMU, since apparently all you need is to ensure a PCI device never gets given a DMA address above 4GB. The situation where PCI devices *can* DMA to all of physical memory, but can't use arbitrary addresses *outside* it - which only becomes a problem with an IOMMU - is considerably trickier.

Quoted 5 lines

It has PCI<->AXI interface involved.
PCI addresses are 64-bit and controller does handle 64-bit addresses there. Mapping between PCI addresses and AXI addresses is defined. But AXI is 32-bit.

Quoted 8 lines

SoC has iommu that probably could be used between PCIe module and RAM. Although AFAIK nobody made that working yet.

Board I work with has 4G of RAM, in 4 banks, located at different parts of wide address space, and only one of them is below 4G. But if iommu is capable of translating addresses such that 4 gigabyte banks map to first 4 gigabytes of address space, then all memory will become available for DMA from PCIe device.

The aforementioned situation on Juno is similar yet different - the PLDA XR3 root complex uses an address-based lookup table to translate outgoing PCI memory space transactions to AXI bus addresses with the appropriate attributes, in power-of-two-sized regions. The firmware configures 3 LUT entries - 2GB at 0x8000_0000 and 8GB at 0x8_8000_0000 with cache-coherent attributes to cover the DRAM areas, plus a small one with device attributes covering the GICv2m MSI frame. The issue is that there is no "no match" translation, so any transaction not within one of those regions never makes it out of the root complex at all.

That's fine in normal operation, as there's nothing outside those regions in the physical memory map a PCI device should be accessing anyway, but turning on the SMMU is another matter - since the IOVA allocator runs top-down, a PCI device with a 64-bit DMA mask will do a dma_map or dma_alloc, get the physical page mapped to an IOVA up around FF_FFFF_F000 (the SMMU will constrain things to the system bus width of 40 bits), then try to access that address and get a termination straight back from the RC. Similarly, A KVM guest which wants to place its memory at arbitrary locations and expect device passthrough to work is going to have a bad time.

I don't know if it's feasible to have the firmware set the LUT up differently, as that might lead to other problems when not using the SMMU, and/or just require far more than the 8 available LUT entries (assuming they have to be non-overlapping - I'm not 100% sure and documentation is sparse). Thus it seems appropriate to describe the currently valid PCI-AXI translations with dma-ranges, but then we'd have multiple entries - last time I looked Linux simply ignores all but the last one in that case - which can't be combined into a simple bitmask, so I'm not entirely sure where to go from there. Especially as so far it seems to be a problem exclusive to one not-widely-available ageing early-access development platform...

It happens that limiting all PCI DMA masks to 32 bits would bodge around this problem thanks to the current IOVA allocator behaviour, but that's pretty yuck, and would force unnecessary bouncing for the non-SMMU case. My other hack to carve up IOVA domains to reserve all addresses not matching memblocks is hardly any more realistic, hence why the SMMU is in the Juno DT in a change-it-at-your-own-peril "disabled" state ;)

Robin.

#11 lore raw
In reply to Arnd Bergmann
Quoted 6 lines · Arnd Bergmann

It's a much rarer problem for the IOMMU case though, because it only impacts devices that are restricted to addressing of less than 32-bits.

If you have an IOMMU enabled, the dma-mapping interface does not care if the device can do wider than 32 bit addressing, as it will never hand out IOVAs above 0xffffffff.

That's absolutely not the case. IOMMUs can and do generate addresses larger than 32-bit. Also various platforms have modes where an IOMMU can be used when <= 32-bit addresses are used and bypassed if full 64-bit addressing is supported and I/O isolation is not explicitly requested.

#12 lore raw

PATCH 1/1 arm64: avoid increasing DMA masks above what hardware supports

In reply to Robin Murphy

There are cases when device supports wide DMA addresses wider than device's connection supports.

In this case driver sets DMA mask based on knowledge of device capabilities. That must succeed to allow drivers to initialize.

However, swiotlb or iommu still need knowledge about actual device capabilities. To avoid breakage, actual mask must not be set wider than device connection allows.

CC Arnd Bergmann <[email protected]>
CC Robin Murphy <[email protected]>
CC Will Deacon <[email protected]>
---
 arch/arm64/Kconfig                   |  3 +++
 arch/arm64/include/asm/device.h      |  1 +
 arch/arm64/include/asm/dma-mapping.h |  3 +++
 arch/arm64/mm/dma-mapping.c          | 43 ++++++++++++++++++++++++++++++++++++
 4 files changed, 50 insertions(+)
arch/arm64/Kconfig +3 -0
index 1117421..afb2c08 100644
@@ -216,6 +216,9 @@ config NEED_DMA_MAP_STATE
 config NEED_SG_DMA_LENGTH
 	def_bool y
 
+config ARCH_HAS_DMA_SET_COHERENT_MASK
+	def_bool y
+
 config SMP
 	def_bool y
 
arch/arm64/include/asm/device.h +1 -0
index 243ef25..a57e7bb 100644
@@ -22,6 +22,7 @@ struct dev_archdata {
 	void *iommu;			/* private IOMMU data */
 #endif
 	bool dma_coherent;
+	u64 parent_dma_mask;
 };
 
 struct pdev_archdata {
arch/arm64/include/asm/dma-mapping.h +3 -0
index ccea82c..eab36d2 100644
@@ -51,6 +51,9 @@ void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size,
 			const struct iommu_ops *iommu, bool coherent);
 #define arch_setup_dma_ops	arch_setup_dma_ops
 
+#define HAVE_ARCH_DMA_SET_MASK 1
+extern int dma_set_mask(struct device *dev, u64 dma_mask);
+
 #ifdef CONFIG_IOMMU_DMA
 void arch_teardown_dma_ops(struct device *dev);
 #define arch_teardown_dma_ops	arch_teardown_dma_ops
arch/arm64/mm/dma-mapping.c +43 -0

55 lines not on this page · raw message

Signature · 1 line
2.1.4
reviewer
#13 lore raw

PATCH 1/1 arm64: avoid increasing DMA masks above what hardware supports

Quoted 104 lines · Nikita Yushchenko

There are cases when device supports wide DMA addresses wider than device's connection supports.

In this case driver sets DMA mask based on knowledge of device capabilities. That must succeed to allow drivers to initialize.

However, swiotlb or iommu still need knowledge about actual device capabilities. To avoid breakage, actual mask must not be set wider than device connection allows.

CC Arnd Bergmann <[email protected]>
CC Robin Murphy <[email protected]>
CC Will Deacon <[email protected]>
---
 arch/arm64/Kconfig                   |  3 +++
 arch/arm64/include/asm/device.h      |  1 +
 arch/arm64/include/asm/dma-mapping.h |  3 +++
 arch/arm64/mm/dma-mapping.c          | 43 ++++++++++++++++++++++++++++++++++++
 4 files changed, 50 insertions(+)
arch/arm64/Kconfig +3 -0
index 1117421..afb2c08 100644
@@ -216,6 +216,9 @@ config NEED_DMA_MAP_STATE
 config NEED_SG_DMA_LENGTH
 	def_bool y
 
+config ARCH_HAS_DMA_SET_COHERENT_MASK
+	def_bool y
+
 config SMP
 	def_bool y
 
arch/arm64/include/asm/device.h +1 -0
index 243ef25..a57e7bb 100644
@@ -22,6 +22,7 @@ struct dev_archdata {
 	void *iommu;			/* private IOMMU data */
 #endif
 	bool dma_coherent;
+	u64 parent_dma_mask;
 };
 
 struct pdev_archdata {
arch/arm64/include/asm/dma-mapping.h +3 -0
index ccea82c..eab36d2 100644
@@ -51,6 +51,9 @@ void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size,
 			const struct iommu_ops *iommu, bool coherent);
 #define arch_setup_dma_ops	arch_setup_dma_ops
 
+#define HAVE_ARCH_DMA_SET_MASK 1
+extern int dma_set_mask(struct device *dev, u64 dma_mask);
+
 #ifdef CONFIG_IOMMU_DMA
 void arch_teardown_dma_ops(struct device *dev);
 #define arch_teardown_dma_ops	arch_teardown_dma_ops
arch/arm64/mm/dma-mapping.c +31 -0
index e040827..7b1bb87 100644
@@ -203,6 +203,37 @@ static void __dma_free(struct device *dev, size_t size,
 	__dma_free_coherent(dev, size, swiotlb_addr, dma_handle, attrs);
 }
 
+int dma_set_mask(struct device *dev, u64 dma_mask)
+{
+	const struct dma_map_ops *ops = get_dma_ops(dev);
+
+	if (mask > dev->archdata.parent_dma_mask)
+		mask = dev->archdata.parent_dma_mask;
+
+	if (ops->set_dma_mask)
+		return ops->set_dma_mask(dev, mask);
+
+	if (!dev->dma_mask || !dma_supported(dev, mask))
+		return -EIO;
+
+	*dev->dma_mask = mask;
+	return 0;
+}
+EXPORT_SYMBOL(dma_set_mask);
+
+int dma_set_coherent_mask(struct device *dev, u64 mask)
+{
+	if (mask > dev->archdata.parent_dma_mask)
+		mask = dev->archdata.parent_dma_mask;
+
+	if (!dma_supported(dev, mask))
+		return -EIO;
+
+	dev->coherent_dma_mask = mask;
+	return 0;
+}
+EXPORT_SYMBOL(dma_set_coherent_mask);
+
 static dma_addr_t __swiotlb_map_page(struct device *dev, struct page *page,
 				     unsigned long offset, size_t size,
 				     enum dma_data_direction dir,

arch/arm64/mm/dma-mapping.c void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size, · line 996

Quoted 8 lines
arch/arm64/mm/dma-mapping.c +5 -0
@@ -958,6 +989,18 @@ void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size,
 	if (!dev->archdata.dma_ops)
 		dev->archdata.dma_ops = &swiotlb_dma_ops;
 
+	/*
+	 * we don't yet support buses that have a non-zero mapping.
+	 *  Let's hope we won't need it
+	 */
+	WARN_ON(dma_base != 0);

I believe we now accomodate the bus remap bits on BCM2837 as a DMA offset, so unfortunately I think this is no longer true.

arch/arm64/mm/dma-mapping.c void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size, · line 1002

Quoted 5 lines
arch/arm64/mm/dma-mapping.c +5 -0
+	/*
+	 * Whatever the parent bus can set. A device must not set
+	 * a DMA mask larger than this.
+	 */
+	dev->archdata.parent_dma_mask = size - 1;

This will effectively constrain *all* DMA masks to be 32-bit, since for 99% of devices we're going to see a size derived from the default mask passed in here. I worry that that's liable to lead to performance and stability regressions (now that the block layer can apparently generate sufficient readahead to ovflow a typical SWIOTLB bounce buffer[1]). Whilst DT users would be able to mitigate that by putting explicit "dma-ranges" properties on every device node, it's less clear what we'd do for ACPI.

I reckon the easiest way forward would be to pass in some flag to arch_setup_dma_ops to indicate whether it's an explicitly-configured range or not - then simply initialising parent_dma_mask to ~0 for the default case *should* keep things working as before.

Robin.

[1]:https://www.mail-archive.com/virtualization at lists.linux-foundation.org/msg26532.html

Quoted 5 lines
arch/arm64/mm/dma-mapping.c +1 -0
+
 	dev->archdata.dma_coherent = coherent;
 	__iommu_setup_dma_ops(dev, dma_base, size, iommu);
 }
#14 lore raw

PATCH 1/1 arm64: avoid increasing DMA masks above what hardware supports

Quoted 8 lines

arch/arm64/mm/dma-mapping.c void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size, · line 996

Quoted 5 lines
arch/arm64/mm/dma-mapping.c +5 -0
+	/*
+	 * we don't yet support buses that have a non-zero mapping.
+	 *  Let's hope we won't need it
+	 */
+	WARN_ON(dma_base != 0);

I believe we now accomodate the bus remap bits on BCM2837 as a DMA offset, so unfortunately I think this is no longer true.

Arnd, this check is from you. Any updates? Perhaps this check can be just dropped?

In swiotlb code, dma address (i.e. with offset already applied) is checked against mask. Not sure what 'dma_base' means in iommu case.

Quoted 10 lines

arch/arm64/mm/dma-mapping.c void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size, · line 1002

Quoted 5 lines
arch/arm64/mm/dma-mapping.c +5 -0
+	/*
+	 * Whatever the parent bus can set. A device must not set
+	 * a DMA mask larger than this.
+	 */
+	dev->archdata.parent_dma_mask = size - 1;

This will effectively constrain *all* DMA masks to be 32-bit, since for 99% of devices we're going to see a size derived from the default mask passed in here. I worry that that's liable to lead to performance and stability regressions

That was exactly my concern when I first tried to address this issue. My first attempt was to alter very locally exact configuration where problem shows, while ensuring that everything else stays as is. See
https://lkml.org/lkml/2016/12/29/218

But looks like people want a generic solution.

Quoted 4 lines

I reckon the easiest way forward would be to pass in some flag to arch_setup_dma_ops to indicate whether it's an explicitly-configured range or not - then simply initialising parent_dma_mask to ~0 for the default case *should* keep things working as before.

Currently only arm, arm64 and mips define arch_setup_dma_ops(). Mips version only checks 'coherent' argument, 'size' is used only by arm and arm64.

Maybe move setting the default from caller to callee?
I.e. pass size=0 if no explicit information exists, and let architecture handle that?

reviewer
#15 lore raw

PATCH 1/1 arm64: avoid increasing DMA masks above what hardware supports

Quoted 38 lines · Nikita Yushchenko
Quoted 8 lines

arch/arm64/mm/dma-mapping.c void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size, · line 996

Quoted 5 lines
arch/arm64/mm/dma-mapping.c +5 -0
+	/*
+	 * we don't yet support buses that have a non-zero mapping.
+	 *  Let's hope we won't need it
+	 */
+	WARN_ON(dma_base != 0);

I believe we now accomodate the bus remap bits on BCM2837 as a DMA offset, so unfortunately I think this is no longer true.

Arnd, this check is from you. Any updates? Perhaps this check can be just dropped?

In swiotlb code, dma address (i.e. with offset already applied) is checked against mask. Not sure what 'dma_base' means in iommu case.

Quoted 10 lines

arch/arm64/mm/dma-mapping.c void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size, · line 1002

Quoted 5 lines
arch/arm64/mm/dma-mapping.c +5 -0
+	/*
+	 * Whatever the parent bus can set. A device must not set
+	 * a DMA mask larger than this.
+	 */
+	dev->archdata.parent_dma_mask = size - 1;

This will effectively constrain *all* DMA masks to be 32-bit, since for 99% of devices we're going to see a size derived from the default mask passed in here. I worry that that's liable to lead to performance and stability regressions

That was exactly my concern when I first tried to address this issue. My first attempt was to alter very locally exact configuration where problem shows, while ensuring that everything else stays as is. See
https://lkml.org/lkml/2016/12/29/218

But looks like people want a generic solution.

Quoted 4 lines

I reckon the easiest way forward would be to pass in some flag to arch_setup_dma_ops to indicate whether it's an explicitly-configured range or not - then simply initialising parent_dma_mask to ~0 for the default case *should* keep things working as before.

Quoted 7 lines

Currently only arm, arm64 and mips define arch_setup_dma_ops(). Mips version only checks 'coherent' argument, 'size' is used only by arm and arm64.

Maybe move setting the default from caller to callee?
I.e. pass size=0 if no explicit information exists, and let architecture handle that?

Yes, I think that ought to work, although the __iommu_setup_dma_ops() call will still want a real size reflecting the default mask, so something like:

if (size == 0) {
	dev->archdata.parent_dma_mask = ~0;
	size = 1ULL << 32;
} else {
	dev->archdata.parent_dma_mask = size - 1;
}

should probably do the trick.

Robin.

#16 lore raw

PATCH 1/1 arm64: avoid increasing DMA masks above what hardware supports

Quoted 2 lines

Yes, I think that ought to work, although the __iommu_setup_dma_ops() call will still want a real size reflecting the default mask

I see iommu_dma_ops do not define set_dma_mask.

So what if setup was done for size reflecting one mask and then driver changes mask? Will things still operate correctly?

reviewer
#17 lore raw

PATCH 1/1 arm64: avoid increasing DMA masks above what hardware supports

Quoted 7 lines · Nikita Yushchenko
Quoted 2 lines

Yes, I think that ought to work, although the __iommu_setup_dma_ops() call will still want a real size reflecting the default mask

I see iommu_dma_ops do not define set_dma_mask.

So what if setup was done for size reflecting one mask and then driver changes mask? Will things still operate correctly?

We've overridden dma_set_mask() at the function level, so it should always apply regardless. Besides, none of the arm64 ops implement .set_dma_mask anyway, so we could possibly drop the references to it altogether.

Conversely, I suppose we could just implement said callback for swiotlb_dma_ops and iommu_dma_ops with the parent_dma_mask-checking function and drop the HAVE_ARCH_DMA_SET_MASK override instead. I'm not sure which approach is preferable - the latter seems arguably cleaner in isolation, but would also be less consistent with how the coherent mask has to be handled. Ho hum.

Robin.

#18 lore raw

PATCH 1/1 arm64: avoid increasing DMA masks above what hardware supports

11.01.2017 17:50, Robin Murphy ?????:

Quoted 14 lines · Nikita Yushchenko
Quoted 7 lines
Quoted 2 lines

Yes, I think that ought to work, although the __iommu_setup_dma_ops() call will still want a real size reflecting the default mask

I see iommu_dma_ops do not define set_dma_mask.

So what if setup was done for size reflecting one mask and then driver changes mask? Will things still operate correctly?

We've overridden dma_set_mask() at the function level, so it should always apply regardless. Besides, none of the arm64 ops implement .set_dma_mask anyway, so we could possibly drop the references to it altogether.

Quoted 6 lines

Conversely, I suppose we could just implement said callback for swiotlb_dma_ops and iommu_dma_ops with the parent_dma_mask-checking function and drop the HAVE_ARCH_DMA_SET_MASK override instead. I'm not sure which approach is preferable - the latter seems arguably cleaner in isolation, but would also be less consistent with how the coherent mask has to be handled. Ho hum.

I mean, before patch is applied.

In the current mainline codebase, arm64 iommu does setup dependent on [default] dma_mask, but does not anyhow react on dma mask change.

I don't know much details about arm64 iommu, but from distant view this combination looks incorrect:
- if behavior of this hardware should depend on dma_mask of device, then it should handle mask change,
- if behavior of this hardware should not depend on dma_mask of device, then what for to pass size to it's setup?

reviewer
#19 lore raw

PATCH 1/1 arm64: avoid increasing DMA masks above what hardware supports

Quoted 29 lines · Nikita Yushchenko

11.01.2017 17:50, Robin Murphy ?????:

Quoted 14 lines · Nikita Yushchenko
Quoted 7 lines
Quoted 2 lines

Yes, I think that ought to work, although the __iommu_setup_dma_ops() call will still want a real size reflecting the default mask

I see iommu_dma_ops do not define set_dma_mask.

So what if setup was done for size reflecting one mask and then driver changes mask? Will things still operate correctly?

We've overridden dma_set_mask() at the function level, so it should always apply regardless. Besides, none of the arm64 ops implement .set_dma_mask anyway, so we could possibly drop the references to it altogether.

Quoted 6 lines

Conversely, I suppose we could just implement said callback for swiotlb_dma_ops and iommu_dma_ops with the parent_dma_mask-checking function and drop the HAVE_ARCH_DMA_SET_MASK override instead. I'm not sure which approach is preferable - the latter seems arguably cleaner in isolation, but would also be less consistent with how the coherent mask has to be handled. Ho hum.

I mean, before patch is applied.

In the current mainline codebase, arm64 iommu does setup dependent on [default] dma_mask, but does not anyhow react on dma mask change.

Quoted 6 lines

I don't know much details about arm64 iommu, but from distant view this combination looks incorrect:
- if behavior of this hardware should depend on dma_mask of device, then it should handle mask change,
- if behavior of this hardware should not depend on dma_mask of device, then what for to pass size to it's setup?

Ah, right, I did indeed misunderstand. The IOMMU code is happy with the DMA masks changing, since it always checks the appropriate one at the point of IOVA allocation. The initial size given to __iommu_setup_dma_ops() really just sets up a caching threshold in the IOVA domain - if there is an explicitly-configured mask, then it's generally good to set the limit based on that, but otherwise the default 32-bit limit is fine even if the driver subsequently alters the device's mask(s) before making DMA API calls. Your patch won't actually change any behaviour in this regard, as long as it still passes a 4GB size by default.

Robin.

#20 lore raw

cover arm64: fix handling of DMA masks wider than bus supports

In reply to Robin Murphy
Quoted 4 lines

I reckon the easiest way forward would be to pass in some flag to arch_setup_dma_ops to indicate whether it's an explicitly-configured range or not - then simply initialising parent_dma_mask to ~0 for the default case *should* keep things working as before.

Tried to do that.

---

Nikita Yushchenko (2):
dma-mapping: let arch know origin of dma range passed to arch_setup_dma_ops()
arm64: avoid increasing DMA masks above what hardware supports

 arch/arm/include/asm/dma-mapping.h             |  1 +
 arch/arm/mm/dma-mapping.c                      |  3 +-
 arch/arm64/Kconfig                             |  3 ++
 arch/arm64/include/asm/device.h                |  1 +
 arch/arm64/include/asm/dma-mapping.h           |  6 +++-
 arch/arm64/mm/dma-mapping.c                    | 43 +++++++++++++++++++++++++-
 arch/mips/include/asm/dma-mapping.h            |  3 +-
 drivers/acpi/scan.c                            |  2 +-
 drivers/iommu/rockchip-iommu.c                 |  2 +-
 drivers/net/ethernet/freescale/dpaa/dpaa_eth.c |  2 +-
 drivers/of/device.c                            |  5 ++-
 drivers/staging/fsl-mc/bus/fsl-mc-bus.c        |  2 +-
 12 files changed, 64 insertions(+), 9 deletions(-)
Signature · 1 line
2.1.4
#21 lore raw

PATCH 1/2 dma-mapping: let arch know origin of dma range passed to arch_setup_dma_ops()

There are cases when device is capable of wide DMA mask (and driver issues corresponding dma_set_mask() call), but bus device sits on can't support wide address. Example: NVMe device behind PCIe controller sitting on 32-bit SoC bus.

To support such case, architecture needs information about such limitations. Such information can originate from dma-ranges property in device tree, and is passed to architecture via arch_setup_dma_ops() call.

Problem is that in wide majority of cases, no dma range is defined. E.g. ACPI has no means to define it. Thus default range (usually full 32-bit range, i.e. 4G starting at zero address) is passed instead.

If architecture enforce this range, all setups currently using wide DMA addresses without explicitly defining support for that via device tree will break. This is bad, especially for ACPI based platforms.

To avoid that, this patch adds additional boolean argument to arch_setup_dma_ops() to show if range originates from authorative source and thus should be enforced, or is just a guess and should be handled as such.

---
 arch/arm/include/asm/dma-mapping.h             | 1 +
 arch/arm/mm/dma-mapping.c                      | 3 ++-
 arch/arm64/include/asm/dma-mapping.h           | 3 ++-
 arch/arm64/mm/dma-mapping.c                    | 3 ++-
 arch/mips/include/asm/dma-mapping.h            | 3 ++-
 drivers/acpi/scan.c                            | 2 +-
 drivers/iommu/rockchip-iommu.c                 | 2 +-
 drivers/net/ethernet/freescale/dpaa/dpaa_eth.c | 2 +-
 drivers/of/device.c                            | 5 ++++-
 drivers/staging/fsl-mc/bus/fsl-mc-bus.c        | 2 +-
 10 files changed, 17 insertions(+), 9 deletions(-)
arch/arm/include/asm/dma-mapping.h +1 -0
index bf02dbd..2a3863e 100644
@@ -117,6 +117,7 @@ static inline unsigned long dma_max_pfn(struct device *dev)
 
 #define arch_setup_dma_ops arch_setup_dma_ops
 extern void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size,
+			       bool enforce_range,
 			       const struct iommu_ops *iommu, bool coherent);
 
 #define arch_teardown_dma_ops arch_teardown_dma_ops
arch/arm/mm/dma-mapping.c +2 -1
index ab77100..b8b11f8 100644
@@ -2380,7 +2380,8 @@ static struct dma_map_ops *arm_get_dma_map_ops(bool coherent)
 }
 
 void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size,
-			const struct iommu_ops *iommu, bool coherent)
+			bool enforce_range, const struct iommu_ops *iommu,
+			bool coherent)
 {
 	struct dma_map_ops *dma_ops;
 
arch/arm64/include/asm/dma-mapping.h +2 -1
index ccea82c..ae1c23f 100644
@@ -48,7 +48,8 @@ static inline struct dma_map_ops *get_dma_ops(struct device *dev)
 }
 
 void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size,
-			const struct iommu_ops *iommu, bool coherent);
+			bool enforce_range, const struct iommu_ops *iommu,
+			bool coherent);
 #define arch_setup_dma_ops	arch_setup_dma_ops
 
 #ifdef CONFIG_IOMMU_DMA
arch/arm64/mm/dma-mapping.c +2 -1
index e040827..ea295f1 100644
@@ -953,7 +953,8 @@ static void __iommu_setup_dma_ops(struct device *dev, u64 dma_base, u64 size,
 #endif  /* CONFIG_IOMMU_DMA */
 
 void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size,
-			const struct iommu_ops *iommu, bool coherent)
+			bool enforce_range, const struct iommu_ops *iommu,
+			bool coherent)
 {
 	if (!dev->archdata.dma_ops)
 		dev->archdata.dma_ops = &swiotlb_dma_ops;
arch/mips/include/asm/dma-mapping.h +2 -1
index 7aa71b9..6af4d87 100644
@@ -34,7 +34,8 @@ extern void dma_cache_sync(struct device *dev, void *vaddr, size_t size,
 
 #define arch_setup_dma_ops arch_setup_dma_ops
 static inline void arch_setup_dma_ops(struct device *dev, u64 dma_base,
-				      u64 size, const struct iommu_ops *iommu,
+				      u64 size, bool enforce_range,
+				      const struct iommu_ops *iommu,
 				      bool coherent)
 {
 #ifdef CONFIG_DMA_PERDEV_COHERENT
drivers/acpi/scan.c +1 -1
index 1926918..dea17a5 100644
@@ -1385,7 +1385,7 @@ void acpi_dma_configure(struct device *dev, enum dev_dma_attr attr)
 	 * Assume dma valid range starts at 0 and covers the whole
 	 * coherent_dma_mask.
 	 */
-	arch_setup_dma_ops(dev, 0, dev->coherent_dma_mask + 1, iommu,
+	arch_setup_dma_ops(dev, 0, dev->coherent_dma_mask + 1, false, iommu,
 			   attr == DEV_DMA_COHERENT);
 }
 EXPORT_SYMBOL_GPL(acpi_dma_configure);
drivers/iommu/rockchip-iommu.c +1 -1
index 9afcbf7..0995ab3 100644
@@ -1096,7 +1096,7 @@ static int rk_iommu_domain_probe(struct platform_device *pdev)
 		return -ENOMEM;
 
 	/* Set dma_ops for dev, otherwise it would be dummy_dma_ops */
-	arch_setup_dma_ops(dev, 0, DMA_BIT_MASK(32), NULL, false);
+	arch_setup_dma_ops(dev, 0, DMA_BIT_MASK(32), false, NULL, false);
 
 	dma_set_max_seg_size(dev, DMA_BIT_MASK(32));
 	dma_coerce_mask_and_coherent(dev, DMA_BIT_MASK(32));
drivers/net/ethernet/freescale/dpaa/dpaa_eth.c +1 -1
index c9b7ad6..19f70d8 100644
@@ -2533,7 +2533,7 @@ static int dpaa_eth_probe(struct platform_device *pdev)
 	priv->buf_layout[TX].priv_data_size = DPAA_TX_PRIV_DATA_SIZE; /* Tx */
 
 	/* device used for DMA mapping */
-	arch_setup_dma_ops(dev, 0, 0, NULL, false);
+	arch_setup_dma_ops(dev, 0, 0, false, NULL, false);
 	err = dma_coerce_mask_and_coherent(dev, DMA_BIT_MASK(40));
 	if (err) {
 		dev_err(dev, "dma_coerce_mask_and_coherent() failed\n");
drivers/of/device.c +4 -1
index fd5cfad..1cc2115 100644
@@ -89,6 +89,7 @@ void of_dma_configure(struct device *dev, struct device_node *np)
 	bool coherent;
 	unsigned long offset;
 	const struct iommu_ops *iommu;
+	bool enforce_range = false;
 
 	/*
 	 * Set default coherent_dma_mask to 32 bit.  Drivers are expected to
@@ -126,6 +127,8 @@ void of_dma_configure(struct device *dev, struct device_node *np)
 			return;
 		}
 		dev_dbg(dev, "dma_pfn_offset(%#08lx)\n", offset);
+
+		enforce_range = true;
 	}
 
 	dev->dma_pfn_offset = offset;
@@ -147,7 +150,7 @@ void of_dma_configure(struct device *dev, struct device_node *np)
 	dev_dbg(dev, "device is%sbehind an iommu\n",
 		iommu ? " " : " not ");
 
-	arch_setup_dma_ops(dev, dma_addr, size, iommu, coherent);
+	arch_setup_dma_ops(dev, dma_addr, size, enforce_range, iommu, coherent);
 }
 EXPORT_SYMBOL_GPL(of_dma_configure);
 
drivers/staging/fsl-mc/bus/fsl-mc-bus.c +1 -1
index 5ac373c..480b644 100644
@@ -540,7 +540,7 @@ int fsl_mc_device_add(struct dprc_obj_desc *obj_desc,
 
 	/* Objects are coherent, unless 'no shareability' flag set. */
 	if (!(obj_desc->flags & DPRC_OBJ_FLAG_NO_MEM_SHAREABILITY))
-		arch_setup_dma_ops(&mc_dev->dev, 0, 0, NULL, true);
+		arch_setup_dma_ops(&mc_dev->dev, 0, 0, false, NULL, true);
 
 	/*
 	 * The device-specific probe callback will get invoked by device_add()
Signature · 1 line
2.1.4
#22 lore raw

Re: [PATCH 1/2] dma-mapping: let arch know origin of dma range passed to arch_setup_dma_ops()

Quoted 31 lines · CET Nikita Yushchenko
drivers/iommu/rockchip-iommu.c +1 -1
index 9afcbf7..0995ab3 100644
@@ -1096,7 +1096,7 @@ static int rk_iommu_domain_probe(struct platform_device *pdev)
 		return -ENOMEM;
 
 	/* Set dma_ops for dev, otherwise it would be dummy_dma_ops */
-	arch_setup_dma_ops(dev, 0, DMA_BIT_MASK(32), NULL, false);
+	arch_setup_dma_ops(dev, 0, DMA_BIT_MASK(32), false, NULL, false);
 
 	dma_set_max_seg_size(dev, DMA_BIT_MASK(32));
 	dma_coerce_mask_and_coherent(dev, DMA_BIT_MASK(32));
drivers/net/ethernet/freescale/dpaa/dpaa_eth.c +1 -1
index c9b7ad6..19f70d8 100644
@@ -2533,7 +2533,7 @@ static int dpaa_eth_probe(struct platform_device *pdev)
 	priv->buf_layout[TX].priv_data_size = DPAA_TX_PRIV_DATA_SIZE; /* Tx */
 
 	/* device used for DMA mapping */
-	arch_setup_dma_ops(dev, 0, 0, NULL, false);
+	arch_setup_dma_ops(dev, 0, 0, false, NULL, false);
 	err = dma_coerce_mask_and_coherent(dev, DMA_BIT_MASK(40));
 	if (err) {
 		dev_err(dev, "dma_coerce_mask_and_coherent() failed\n");
drivers/staging/fsl-mc/bus/fsl-mc-bus.c +0 -0
index 5ac373c..480b644 100644

drivers/staging/fsl-mc/bus/fsl-mc-bus.c int fsl_mc_device_add(struct dprc_obj_desc *obj_desc, · line 546

Quoted 8 lines
drivers/staging/fsl-mc/bus/fsl-mc-bus.c +1 -1
@@ -540,7 +540,7 @@ int fsl_mc_device_add(struct dprc_obj_desc *obj_desc,
 
 	/* Objects are coherent, unless 'no shareability' flag set. */
 	if (!(obj_desc->flags & DPRC_OBJ_FLAG_NO_MEM_SHAREABILITY))
-		arch_setup_dma_ops(&mc_dev->dev, 0, 0, NULL, true);
+		arch_setup_dma_ops(&mc_dev->dev, 0, 0, false, NULL, true);
 
 	/*
 	 * The device-specific probe callback will get invoked by device_add()

Why are these actually calling arch_setup_dma_ops() here in the first place? Are these all devices that are DMA masters without an OF node?

Quoted 13 lines
drivers/of/device.c +1 -0
index fd5cfad..1cc2115 100644
@@ -89,6 +89,7 @@ void of_dma_configure(struct device *dev, struct device_node *np)
 	bool coherent;
 	unsigned long offset;
 	const struct iommu_ops *iommu;
+	bool enforce_range = false;
 
 	/*
 	 * Set default coherent_dma_mask to 32 bit.  Drivers are expected to

drivers/of/device.c void of_dma_configure(struct device *dev, struct device_node *np) · line 134

Quoted 8 lines
drivers/of/device.c +2 -0
@@ -126,6 +127,8 @@ void of_dma_configure(struct device *dev, struct device_node *np)
 			return;
 		}
 		dev_dbg(dev, "dma_pfn_offset(%#08lx)\n", offset);
+
+		enforce_range = true;
 	}
 
 	dev->dma_pfn_offset = offset;

Hmm, I think when the dma-ranges are missing, we should either enforce a 32-bit mask, or disallow DMA completely. It's probably too late for the latter, I wish we had done this earlier in order to force everyone on ARM64 to have a valid dma-ranges property for any DMA master.

Arnd

#23 lore raw

Re: [PATCH 1/2] dma-mapping: let arch know origin of dma range passed to arch_setup_dma_ops()

Quoted 11 lines
Quoted 11 lines
drivers/staging/fsl-mc/bus/fsl-mc-bus.c +1 -1
index 5ac373c..480b644 100644
@@ -540,7 +540,7 @@ int fsl_mc_device_add(struct dprc_obj_desc *obj_desc,
 
 	/* Objects are coherent, unless 'no shareability' flag set. */
 	if (!(obj_desc->flags & DPRC_OBJ_FLAG_NO_MEM_SHAREABILITY))
-		arch_setup_dma_ops(&mc_dev->dev, 0, 0, NULL, true);
+		arch_setup_dma_ops(&mc_dev->dev, 0, 0, false, NULL, true);
 
Quoted 5 lines

drivers/staging/fsl-mc/bus/fsl-mc-bus.c int fsl_mc_device_add(struct dprc_obj_desc *obj_desc, · line 546

Quoted 2 lines
drivers/staging/fsl-mc/bus/fsl-mc-bus.c +0 -0
 	/*
 	 * The device-specific probe callback will get invoked by device_add()

Why are these actually calling arch_setup_dma_ops() here in the first place? Are these all devices that are DMA masters without an OF node?

I don't know, but that's a different topic. This patch just adds argument and sets it to false everywhere but in the location when range should be definitely enforced.

Quoted 8 lines
Quoted 8 lines
drivers/staging/fsl-mc/bus/fsl-mc-bus.c +2 -0
@@ -126,6 +127,8 @@ void of_dma_configure(struct device *dev, struct device_node *np)
 			return;
 		}
 		dev_dbg(dev, "dma_pfn_offset(%#08lx)\n", offset);
+
+		enforce_range = true;
 	}
 
Quoted 6 lines

drivers/staging/fsl-mc/bus/fsl-mc-bus.c void of_dma_configure(struct device *dev, struct device_node *np) · line 134

Quoted 1 line
drivers/staging/fsl-mc/bus/fsl-mc-bus.c +0 -0
 	dev->dma_pfn_offset = offset;

Hmm, I think when the dma-ranges are missing, we should either enforce a 32-bit mask, or disallow DMA completely. It's probably too late for the latter, I wish we had done this earlier in order to force everyone on ARM64 to have a valid dma-ranges property for any DMA master.

This can be done over time.

However the very idea of this version of patch is - keep working pieces as-is, thus for now setting enforce_range to false in case of no defined dma-ranges is intentional.

What I should re-check is - does rcar dtsi set dma-ranges, and add it if it does not.

Nikita

#24 lore raw

Re: [PATCH 1/2] dma-mapping: let arch know origin of dma range passed to arch_setup_dma_ops()

Quoted 36 lines · Nikita Yushchenko
Quoted 11 lines
Quoted 11 lines
drivers/staging/fsl-mc/bus/fsl-mc-bus.c +1 -1
index 5ac373c..480b644 100644
@@ -540,7 +540,7 @@ int fsl_mc_device_add(struct dprc_obj_desc *obj_desc,
 
 	/* Objects are coherent, unless 'no shareability' flag set. */
 	if (!(obj_desc->flags & DPRC_OBJ_FLAG_NO_MEM_SHAREABILITY))
-		arch_setup_dma_ops(&mc_dev->dev, 0, 0, NULL, true);
+		arch_setup_dma_ops(&mc_dev->dev, 0, 0, false, NULL, true);
 
Quoted 5 lines

drivers/staging/fsl-mc/bus/fsl-mc-bus.c int fsl_mc_device_add(struct dprc_obj_desc *obj_desc, · line 546

Quoted 2 lines
drivers/staging/fsl-mc/bus/fsl-mc-bus.c +0 -0
 	/*
 	 * The device-specific probe callback will get invoked by device_add()

Why are these actually calling arch_setup_dma_ops() here in the first place? Are these all devices that are DMA masters without an OF node?

I don't know, but that's a different topic. This patch just adds argument and sets it to false everywhere but in the location when range should be definitely enforced.

Quoted 8 lines
Quoted 8 lines
drivers/staging/fsl-mc/bus/fsl-mc-bus.c +2 -0
@@ -126,6 +127,8 @@ void of_dma_configure(struct device *dev, struct device_node *np)
 			return;
 		}
 		dev_dbg(dev, "dma_pfn_offset(%#08lx)\n", offset);
+
+		enforce_range = true;
 	}
 
Quoted 6 lines

drivers/staging/fsl-mc/bus/fsl-mc-bus.c void of_dma_configure(struct device *dev, struct device_node *np) · line 134

Quoted 1 line
drivers/staging/fsl-mc/bus/fsl-mc-bus.c +0 -0
 	dev->dma_pfn_offset = offset;

Hmm, I think when the dma-ranges are missing, we should either enforce a 32-bit mask, or disallow DMA completely. It's probably too late for the latter, I wish we had done this earlier in order to force everyone on ARM64 to have a valid dma-ranges property for any DMA master.

Quoted 5 lines

This can be done over time.

However the very idea of this version of patch is - keep working pieces as-is, thus for now setting enforce_range to false in case of no defined dma-ranges is intentional.

What we can do is - check bus width (as it is defined in DT) and set enforce_range to true if bus is 32-bit

Quoted 2 lines

What I should re-check is - does rcar dtsi set dma-ranges, and add it if it does not.

It does not, will have to add.

In DT bus is defined as 64-bit. But looks like physically it is 32-bit. Maybe DT needs fixing.

#25 lore raw

Re: [PATCH 1/2] dma-mapping: let arch know origin of dma range passed to arch_setup_dma_ops()

Quoted 14 lines · CET Nikita Yushchenko
Quoted 10 lines
Quoted 4 lines

Hmm, I think when the dma-ranges are missing, we should either enforce a 32-bit mask, or disallow DMA completely. It's probably too late for the latter, I wish we had done this earlier in order to force everyone on ARM64 to have a valid dma-ranges property for any DMA master.

This can be done over time.

However the very idea of this version of patch is - keep working pieces as-is, thus for now setting enforce_range to false in case of no defined dma-ranges is intentional.

What we can do is - check bus width (as it is defined in DT) and set enforce_range to true if bus is 32-bit

Quoted 7 lines
Quoted 2 lines

What I should re-check is - does rcar dtsi set dma-ranges, and add it if it does not.

It does not, will have to add.

In DT bus is defined as 64-bit. But looks like physically it is 32-bit. Maybe DT needs fixing.

I think we always assumed that the lack of a dma-ranges property implied a 32-bit width, as that is the safe fallback as well as the most common case.

AFAICT, this means you are actually fine on rcar, and all other platforms will keep working as we enforce it, but might get slowed down if they relied on the unintended behavior of allowing 64-bit DMA.

Arnd

#26 lore raw

Re: [PATCH 1/2] dma-mapping: let arch know origin of dma range passed to arch_setup_dma_ops()

Quoted 19 lines
Quoted 19 lines
Quoted 7 lines
Quoted 6 lines

Hmm, I think when the dma-ranges are missing, we should either enforce a 32-bit mask, or disallow DMA completely. It's probably too late for the latter, I wish we had done this earlier in order to force everyone on ARM64 to have a valid dma-ranges property for any DMA master.

Quoted 5 lines

This can be done over time.

However the very idea of this version of patch is - keep working pieces as-is, thus for now setting enforce_range to false in case of no defined dma-ranges is intentional.

What we can do is - check bus width (as it is defined in DT) and set enforce_range to true if bus is 32-bit

Quoted 2 lines

What I should re-check is - does rcar dtsi set dma-ranges, and add it if it does not.

Quoted 8 lines
Quoted 4 lines

It does not, will have to add.

In DT bus is defined as 64-bit. But looks like physically it is 32-bit. Maybe DT needs fixing.

I think we always assumed that the lack of a dma-ranges property implied a 32-bit width, as that is the safe fallback as well as the most common case.

Yes we assumed that, but that was combined with blindly accepting wider dma_mask per driver's request. Later is being changed.

Quoted 4 lines

AFAICT, this means you are actually fine on rcar, and all other platforms will keep working as we enforce it, but might get slowed down if they relied on the unintended behavior of allowing 64-bit DMA.

Yesterday Robin raised issue that a change starting to enforce default dma_mask will break existing setups - i.e. those that depend in 64bit DMA being implicitly supported without manually declaring such support.

In reply to that, I suggested this version of patchset that should keep existing behavior by default.

I'm fine with both approaches regarding behavior on hw that I don't have
- but I'm not in position to make any decisions on that.

#27 lore raw

Re: [PATCH 1/2] dma-mapping: let arch know origin of dma range passed to arch_setup_dma_ops()

In reply to Nikita Yushchenko
Quoted 17 lines · Nikita Yushchenko
Quoted 11 lines
Quoted 11 lines
drivers/staging/fsl-mc/bus/fsl-mc-bus.c +1 -1
index 5ac373c..480b644 100644
@@ -540,7 +540,7 @@ int fsl_mc_device_add(struct dprc_obj_desc *obj_desc,
 
 	/* Objects are coherent, unless 'no shareability' flag set. */
 	if (!(obj_desc->flags & DPRC_OBJ_FLAG_NO_MEM_SHAREABILITY))
-		arch_setup_dma_ops(&mc_dev->dev, 0, 0, NULL, true);
+		arch_setup_dma_ops(&mc_dev->dev, 0, 0, false, NULL, true);
 
Quoted 5 lines

drivers/staging/fsl-mc/bus/fsl-mc-bus.c int fsl_mc_device_add(struct dprc_obj_desc *obj_desc, · line 546

Quoted 2 lines
drivers/staging/fsl-mc/bus/fsl-mc-bus.c +0 -0
 	/*
 	 * The device-specific probe callback will get invoked by device_add()

Why are these actually calling arch_setup_dma_ops() here in the first place? Are these all devices that are DMA masters without an OF node?

Quoted 3 lines

I don't know, but that's a different topic. This patch just adds argument and sets it to false everywhere but in the location when range should be definitely enforced.

I also wouldn't lose any sleep over a staging driver.

Will

#28 lore raw

Re: [PATCH 1/2] dma-mapping: let arch know origin of dma range passed to arch_setup_dma_ops()

Quoted 18 lines · CET Will Deacon
Quoted 17 lines · Nikita Yushchenko
Quoted 16 lines

drivers/staging/fsl-mc/bus/fsl-mc-bus.c int fsl_mc_device_add(struct dprc_obj_desc *obj_desc, · line 546

Quoted 13 lines
drivers/staging/fsl-mc/bus/fsl-mc-bus.c +1 -1
index 5ac373c..480b644 100644
@@ -540,7 +540,7 @@ int fsl_mc_device_add(struct dprc_obj_desc *obj_desc,
 
   /* Objects are coherent, unless 'no shareability' flag set. */
   if (!(obj_desc->flags & DPRC_OBJ_FLAG_NO_MEM_SHAREABILITY))
-          arch_setup_dma_ops(&mc_dev->dev, 0, 0, NULL, true);
+          arch_setup_dma_ops(&mc_dev->dev, 0, 0, false, NULL, true);
 
   /*
    * The device-specific probe callback will get invoked by device_add()

Why are these actually calling arch_setup_dma_ops() here in the first place? Are these all devices that are DMA masters without an OF node?

Quoted 5 lines
Quoted 3 lines

I don't know, but that's a different topic. This patch just adds argument and sets it to false everywhere but in the location when range should be definitely enforced.

I also wouldn't lose any sleep over a staging driver.

I think this is in the process of being moved out of staging, and my question was about the other two as well:

drivers/net/ethernet/freescale/dpaa/dpaa_eth.c
drivers/iommu/rockchip-iommu.c

Arnd

reviewer
#29 lore raw

Re: [PATCH 1/2] dma-mapping: let arch know origin of dma range passed to arch_setup_dma_ops()

Quoted 19 lines · Arnd Bergmann
Quoted 18 lines · CET Will Deacon
Quoted 17 lines · Nikita Yushchenko
Quoted 16 lines

drivers/staging/fsl-mc/bus/fsl-mc-bus.c int fsl_mc_device_add(struct dprc_obj_desc *obj_desc, · line 546

Quoted 13 lines
drivers/staging/fsl-mc/bus/fsl-mc-bus.c +1 -1
index 5ac373c..480b644 100644
@@ -540,7 +540,7 @@ int fsl_mc_device_add(struct dprc_obj_desc *obj_desc,
 
   /* Objects are coherent, unless 'no shareability' flag set. */
   if (!(obj_desc->flags & DPRC_OBJ_FLAG_NO_MEM_SHAREABILITY))
-          arch_setup_dma_ops(&mc_dev->dev, 0, 0, NULL, true);
+          arch_setup_dma_ops(&mc_dev->dev, 0, 0, false, NULL, true);
 
   /*
    * The device-specific probe callback will get invoked by device_add()

Why are these actually calling arch_setup_dma_ops() here in the first place? Are these all devices that are DMA masters without an OF node?

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

I don't know, but that's a different topic. This patch just adds argument and sets it to false everywhere but in the location when range should be definitely enforced.

I also wouldn't lose any sleep over a staging driver.

I think this is in the process of being moved out of staging, and my question was about the other two as well:

The fsl-mc is actually a sort-of-bus-controller probing and configuring its (directly DMA-capable) child devices, so is in fact legitimate here.

Quoted 1 line

drivers/net/ethernet/freescale/dpaa/dpaa_eth.c

That one is completely bogus, and should just go away.

Quoted 1 line

drivers/iommu/rockchip-iommu.c

That one is part of some ugly trickery involving creating a fake device to represent multiple separate IOMMU devices. The driver could probably be reworked to not need it (the Exynos IOMMU handles a similar situation without such tricks), but it's non-trivial.

Robin.

Quoted 3 lines

Arnd

#30 lore raw

Re: [PATCH 1/2] dma-mapping: let arch know origin of dma range passed to arch_setup_dma_ops()

In reply to Nikita Yushchenko
url: https://github.com/0day-ci/linux/commits/Nikita-Yushchenko/dma-mapping-let-arch-know-origin-of-dma-range-passed-to-arch_setup_dma_ops/20170113-152733 config: x86_64-randconfig-u0-01131618…

Hi Nikita,

[auto build test ERROR on linus/master]
[also build test ERROR on v4.10-rc3 next-20170112]
[cannot apply to arm64/for-next/core]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url: https://github.com/0day-ci/linux/commits/Nikita-Yushchenko/dma-mapping-let-arch-know-origin-of-dma-range-passed-to-arch_setup_dma_ops/20170113-152733
config: x86_64-randconfig-u0-01131618 (attached as .config)
compiler: gcc-6 (Debian 6.2.0-3) 6.2.0 20160901 reproduce:

        # save the attached .config to linux build tree
        make ARCH=x86_64 

All errors (new ones prefixed by >>):

drivers/acpi/scan.c: In function 'acpi_dma_configure':

>> drivers/acpi/scan.c:1388:2: error: too many arguments to function 'arch_setup_dma_ops'
     arch_setup_dma_ops(dev, 0, dev->coherent_dma_mask + 1, false, iommu,
     ^~~~~~~~~~~~~~~~~~

In file included from drivers/acpi/scan.c:15:0:
include/linux/dma-mapping.h:611:20: note: declared here
static inline void arch_setup_dma_ops(struct device *dev, u64 dma_base,
^~~~~~~~~~~~~~~~~~

vim +/arch_setup_dma_ops +1388 drivers/acpi/scan.c

1382 iommu = iort_iommu_configure(dev);
1383
1384 /*
1385 * Assume dma valid range starts at 0 and covers the whole
1386 * coherent_dma_mask.
1387 */

> 1388		arch_setup_dma_ops(dev, 0, dev->coherent_dma_mask + 1, false, iommu,

1389 attr == DEV_DMA_COHERENT);
1390 }
1391 EXPORT_SYMBOL_GPL(acpi_dma_configure);

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation
-------------- next part --------------
A non-text attachment was scrubbed...
Name: .config.gz
Type: application/gzip
Size: 25665 bytes
Desc: not available
URL: <http://lists.infradead.org/pipermail/linux-arm-kernel/attachments/20170113/7e91dc43/attachment-0001.gz>

#31 lore raw

PATCH 2/2 arm64: avoid increasing DMA masks above what hardware supports

In reply to Nikita Yushchenko

There are cases when device supports wide DMA addresses wider than device's connection supports.

In this case driver sets DMA mask based on knowledge of device capabilities. That must succeed to allow drivers to initialize.

However, swiotlb or iommu still need knowledge about actual device capabilities. To avoid breakage, actual mask must not be set wider than device connection allows.

CC Arnd Bergmann <[email protected]>
CC Robin Murphy <[email protected]>
CC Will Deacon <[email protected]>
---
 arch/arm64/Kconfig                   |  3 +++
 arch/arm64/include/asm/device.h      |  1 +
 arch/arm64/include/asm/dma-mapping.h |  3 +++
 arch/arm64/mm/dma-mapping.c          | 40 ++++++++++++++++++++++++++++++++++++
 4 files changed, 47 insertions(+)
arch/arm64/Kconfig +3 -0
index 1117421..afb2c08 100644
@@ -216,6 +216,9 @@ config NEED_DMA_MAP_STATE
 config NEED_SG_DMA_LENGTH
 	def_bool y
 
+config ARCH_HAS_DMA_SET_COHERENT_MASK
+	def_bool y
+
 config SMP
 	def_bool y
 
arch/arm64/include/asm/device.h +1 -0
index 243ef25..a57e7bb 100644
@@ -22,6 +22,7 @@ struct dev_archdata {
 	void *iommu;			/* private IOMMU data */
 #endif
 	bool dma_coherent;
+	u64 parent_dma_mask;
 };
 
 struct pdev_archdata {
arch/arm64/include/asm/dma-mapping.h +3 -0
index ae1c23f..46b53e6 100644
@@ -52,6 +52,9 @@ void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size,
 			bool coherent);
 #define arch_setup_dma_ops	arch_setup_dma_ops
 
+#define HAVE_ARCH_DMA_SET_MASK 1
+extern int dma_set_mask(struct device *dev, u64 dma_mask);
+
 #ifdef CONFIG_IOMMU_DMA
 void arch_teardown_dma_ops(struct device *dev);
 #define arch_teardown_dma_ops	arch_teardown_dma_ops
arch/arm64/mm/dma-mapping.c +40 -0
index ea295f1..31b96fd 100644
@@ -203,6 +203,37 @@ static void __dma_free(struct device *dev, size_t size,
 	__dma_free_coherent(dev, size, swiotlb_addr, dma_handle, attrs);
 }
 
+int dma_set_mask(struct device *dev, u64 dma_mask)
+{
+	const struct dma_map_ops *ops = get_dma_ops(dev);
+
+	if (mask > dev->archdata.parent_dma_mask)
+		mask = dev->archdata.parent_dma_mask;
+
+	if (ops->set_dma_mask)
+		return ops->set_dma_mask(dev, mask);
+
+	if (!dev->dma_mask || !dma_supported(dev, mask))
+		return -EIO;
+
+	*dev->dma_mask = mask;
+	return 0;
+}
+EXPORT_SYMBOL(dma_set_mask);
+
+int dma_set_coherent_mask(struct device *dev, u64 mask)
+{
+	if (mask > dev->archdata.parent_dma_mask)
+		mask = dev->archdata.parent_dma_mask;
+
+	if (!dma_supported(dev, mask))
+		return -EIO;
+
+	dev->coherent_dma_mask = mask;
+	return 0;
+}
+EXPORT_SYMBOL(dma_set_coherent_mask);
+
 static dma_addr_t __swiotlb_map_page(struct device *dev, struct page *page,
 				     unsigned long offset, size_t size,
 				     enum dma_data_direction dir,
@@ -959,6 +990,15 @@ void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size,
 	if (!dev->archdata.dma_ops)
 		dev->archdata.dma_ops = &swiotlb_dma_ops;
 
+	/*
+	 * Whatever the parent bus can set. A device must not set
+	 * a DMA mask larger than this.
+	 */
+	if (enforce_range)
+		dev->archdata.parent_dma_mask = size - 1;
+	else
+		dev->archdata.parent_dma_mask = DMA_BIT_MASK(64);
+
 	dev->archdata.dma_coherent = coherent;
 	__iommu_setup_dma_ops(dev, dma_base, size, iommu);
 }
Signature · 1 line
2.1.4
#32 lore raw

Re: [PATCH 2/2] arm64: avoid increasing DMA masks above what hardware supports

Quoted 6 lines · CET Nikita Yushchenko
arch/arm64/mm/dma-mapping.c +2 -0
@@ -959,6 +990,15 @@ void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size,
        if (!dev->archdata.dma_ops)
                dev->archdata.dma_ops = &swiotlb_dma_ops;
 
+       /*
+        * Whatever the parent bus can set. A device must not set

arch/arm64/mm/dma-mapping.c void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size, · line 1004

Quoted 10 lines
arch/arm64/mm/dma-mapping.c +7 -0
+        * a DMA mask larger than this.
+        */
+       if (enforce_range)
+               dev->archdata.parent_dma_mask = size - 1;
+       else
+               dev->archdata.parent_dma_mask = DMA_BIT_MASK(64);
+
        dev->archdata.dma_coherent = coherent;
        __iommu_setup_dma_ops(dev, dma_base, size, iommu);
 

Could we just pass the mask instead of the size here?

Arnd

#33 lore raw

Re: [PATCH 2/2] arm64: avoid increasing DMA masks above what hardware supports

Quoted 4 lines
Quoted 4 lines
arch/arm64/mm/dma-mapping.c +0 -0
@@ -959,6 +990,15 @@ void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size,
        if (!dev->archdata.dma_ops)
                dev->archdata.dma_ops = &swiotlb_dma_ops;
 
Quoted 14 lines

arch/arm64/mm/dma-mapping.c void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size,

Quoted 12 lines
arch/arm64/mm/dma-mapping.c +9 -0
+       /*
+        * Whatever the parent bus can set. A device must not set
+        * a DMA mask larger than this.
+        */
+       if (enforce_range)
+               dev->archdata.parent_dma_mask = size - 1;
+       else
+               dev->archdata.parent_dma_mask = DMA_BIT_MASK(64);
+
        dev->archdata.dma_coherent = coherent;
        __iommu_setup_dma_ops(dev, dma_base, size, iommu);
 

Could we just pass the mask instead of the size here?

We don't want to change API now.

Nikita

#34 lore raw

Re: [PATCH 2/2] arm64: avoid increasing DMA masks above what hardware supports

In reply to Nikita Yushchenko
url: https://github.com/0day-ci/linux/commits/Nikita-Yushchenko/dma-mapping-let-arch-know-origin-of-dma-range-passed-to-arch_setup_dma_ops/20170113-152733 config: arm64-defconfig (attached as…

Hi Nikita,

[auto build test ERROR on linus/master]
[also build test ERROR on v4.10-rc3 next-20170112]
[cannot apply to arm64/for-next/core]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url: https://github.com/0day-ci/linux/commits/Nikita-Yushchenko/dma-mapping-let-arch-know-origin-of-dma-range-passed-to-arch_setup_dma_ops/20170113-152733
config: arm64-defconfig (attached as .config)
compiler: aarch64-linux-gnu-gcc (Debian 6.1.1-9) 6.1.1 20160705
reproduce:
wget https://git.kernel.org/cgit/linux/kernel/git/wfg/lkp-tests.git/plain/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
make.cross ARCH=arm64

All errors (new ones prefixed by >>):

arch/arm64/mm/dma-mapping.c: In function 'dma_set_mask':

>> arch/arm64/mm/dma-mapping.c:210:6: error: 'mask' undeclared (first use in this function)
     if (mask > dev->archdata.parent_dma_mask)
         ^~~~

arch/arm64/mm/dma-mapping.c:210:6: note: each undeclared identifier is reported only once for each function it appears in

vim +/mask +210 arch/arm64/mm/dma-mapping.c

204 }
205
206 int dma_set_mask(struct device *dev, u64 dma_mask)
207 {
208 const struct dma_map_ops *ops = get_dma_ops(dev);
209

 > 210		if (mask > dev->archdata.parent_dma_mask)

211 mask = dev->archdata.parent_dma_mask;
212
213 if (ops->set_dma_mask)

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation
-------------- next part --------------
A non-text attachment was scrubbed...
Name: .config.gz
Type: application/gzip
Size: 33713 bytes
Desc: not available
URL: <http://lists.infradead.org/pipermail/linux-arm-kernel/attachments/20170113/4aebcaaa/attachment-0001.gz>

#35 lore raw
In reply to Robin Murphy
Quoted 10 lines
Quoted 7 lines

What issue "IOMMU doesn't solve"?

Issue I'm trying to address is - inconsistency within swiotlb dma_map_ops, where (1) any wide mask is silently accepted, but (2) then mask is used to decide if bounce buffers are needed or not. This inconsistency causes NVMe+R-Car cobmo not working (and breaking memory instead).

The fundamental underlying problem is the "any wide mask is silently accepted" part, and that applies equally to IOMMU ops as well.

Is just posted version better?

It should cover iommu case as well.

Nikita

#36 lore raw
In reply to Robin Murphy
Quoted 7 lines · Robin Murphy

We still need a way for drivers to communicate a device's probed addressing capability to SWIOTLB, so there's always going to have to be *some* sort of public interface. Personally, the change in semantics I'd like to see is to make dma_set_mask() only fail if DMA is entirely disallowed - in the normal case it would always succeed, but the DMA API implementation would be permitted to set a smaller mask than requested (this is effectively what the x86 IOMMU ops do already).

Yes, this sounds reasonable.

Quoted 10 lines

The significant
work that would require, though, is changing all the drivers currently using this sort of pattern:

	if (!dma_set_mask(dev, DMA_BIT_MASK(64))
		/* put device into 64-bit mode */
	else if (!dma_set_mask(dev, DMA_BIT_MASK(32))
		/* put device into 32-bit mode */
	else
		/* error */

While we have this pattern in a lot of places it's already rather pointless on most architectures as the first dma_set_mask call won't ever fail for the most common dma_ops implementations.

Quoted 10 lines

to something like this:

	if (!dma_set_mask(dev, DMA_BIT_MASK(64))
		/* error */
	if (dma_get_mask(dev) > DMA_BIT_MASK(32))
		/* put device into 64-bit mode */
	else
		/* put device into 32-bit mode */

Which would be a pretty major job.

I don't think it's too bad. Also for many modern devices there is no need to put the device into a specific mode. It's mostly a historic issue from the PCI/PCI-X days with the less efficient DAC addressing scheme.

#37 lore raw
In reply to Nikita Yushchenko
Quoted 3 lines · Nikita Yushchenko

With this direction, semantics of dma mask becomes even more questionable. I'd say dma_mask is candidate for removal (or to move to swiotlb's or iommu's local area)

We need the dma mask so that the device can advertise what addresses the device supports. Many old devices only support 32-bit DMA addressing, and some less common ones just 24-bit or other weird ones.