linux-arm-kernel.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] arm64: dma-mapping: fix dma_to_phys API for IOMMU attached devices
@ 2016-03-17 15:50 Sinan Kaya
  2016-03-17 15:57 ` Robin Murphy
  0 siblings, 1 reply; 6+ messages in thread
From: Sinan Kaya @ 2016-03-17 15:50 UTC (permalink / raw)
  To: linux-arm-kernel

This patch modifies dma_to_phys to call iommu_iova_to_phys to perform dma
to phys conversions for IOMMU attached devices where dma and physical
addresses often have distinct values.

Signed-off-by: Nate Watterson <nwatters@codeaurora.org>
Signed-off-by: Sinan Kaya <okaya@codeaurora.org>
---
 arch/arm64/include/asm/dma-mapping.h |  5 +----
 arch/arm64/mm/dma-mapping.c          | 16 ++++++++++++++++
 2 files changed, 17 insertions(+), 4 deletions(-)

diff --git a/arch/arm64/include/asm/dma-mapping.h b/arch/arm64/include/asm/dma-mapping.h
index ba437f0..a0a486c 100644
--- a/arch/arm64/include/asm/dma-mapping.h
+++ b/arch/arm64/include/asm/dma-mapping.h
@@ -69,10 +69,7 @@ static inline dma_addr_t phys_to_dma(struct device *dev, phys_addr_t paddr)
 	return (dma_addr_t)paddr;
 }
 
-static inline phys_addr_t dma_to_phys(struct device *dev, dma_addr_t dev_addr)
-{
-	return (phys_addr_t)dev_addr;
-}
+phys_addr_t dma_to_phys(struct device *dev, dma_addr_t dev_addr);
 
 static inline bool dma_capable(struct device *dev, dma_addr_t addr, size_t size)
 {
diff --git a/arch/arm64/mm/dma-mapping.c b/arch/arm64/mm/dma-mapping.c
index 331c4ca..8252a92 100644
--- a/arch/arm64/mm/dma-mapping.c
+++ b/arch/arm64/mm/dma-mapping.c
@@ -989,3 +989,19 @@ void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size,
 	dev->archdata.dma_coherent = coherent;
 	__iommu_setup_dma_ops(dev, dma_base, size, iommu);
 }
+
+phys_addr_t dma_to_phys(struct device *dev, dma_addr_t dev_addr)
+{
+	phys_addr_t phys = (phys_addr_t)dev_addr;
+
+#ifdef CONFIG_IOMMU_DMA
+	struct iommu_domain *domain;
+
+	domain = iommu_get_domain_for_dev(dev);
+	if (domain)
+		phys = iommu_iova_to_phys(domain, dev_addr);
+#endif
+
+	return phys;
+}
+EXPORT_SYMBOL(dma_to_phys);
-- 
1.8.2.1

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH] arm64: dma-mapping: fix dma_to_phys API for IOMMU attached devices
  2016-03-17 15:50 [PATCH] arm64: dma-mapping: fix dma_to_phys API for IOMMU attached devices Sinan Kaya
@ 2016-03-17 15:57 ` Robin Murphy
  2016-03-17 16:07   ` Sinan Kaya
  0 siblings, 1 reply; 6+ messages in thread
From: Robin Murphy @ 2016-03-17 15:57 UTC (permalink / raw)
  To: linux-arm-kernel

On 17/03/16 15:50, Sinan Kaya wrote:
> This patch modifies dma_to_phys to call iommu_iova_to_phys to perform dma
> to phys conversions for IOMMU attached devices where dma and physical
> addresses often have distinct values.

What's this for? dma_to_phys() is only used by SWIOTLB, and that's 
mutually exclusive with IOMMU ops.

Also, what about phys_to_dma()?

Robin.

> Signed-off-by: Nate Watterson <nwatters@codeaurora.org>
> Signed-off-by: Sinan Kaya <okaya@codeaurora.org>
> ---
>   arch/arm64/include/asm/dma-mapping.h |  5 +----
>   arch/arm64/mm/dma-mapping.c          | 16 ++++++++++++++++
>   2 files changed, 17 insertions(+), 4 deletions(-)
>
> diff --git a/arch/arm64/include/asm/dma-mapping.h b/arch/arm64/include/asm/dma-mapping.h
> index ba437f0..a0a486c 100644
> --- a/arch/arm64/include/asm/dma-mapping.h
> +++ b/arch/arm64/include/asm/dma-mapping.h
> @@ -69,10 +69,7 @@ static inline dma_addr_t phys_to_dma(struct device *dev, phys_addr_t paddr)
>   	return (dma_addr_t)paddr;
>   }
>
> -static inline phys_addr_t dma_to_phys(struct device *dev, dma_addr_t dev_addr)
> -{
> -	return (phys_addr_t)dev_addr;
> -}
> +phys_addr_t dma_to_phys(struct device *dev, dma_addr_t dev_addr);
>
>   static inline bool dma_capable(struct device *dev, dma_addr_t addr, size_t size)
>   {
> diff --git a/arch/arm64/mm/dma-mapping.c b/arch/arm64/mm/dma-mapping.c
> index 331c4ca..8252a92 100644
> --- a/arch/arm64/mm/dma-mapping.c
> +++ b/arch/arm64/mm/dma-mapping.c
> @@ -989,3 +989,19 @@ void arch_setup_dma_ops(struct device *dev, u64 dma_base, u64 size,
>   	dev->archdata.dma_coherent = coherent;
>   	__iommu_setup_dma_ops(dev, dma_base, size, iommu);
>   }
> +
> +phys_addr_t dma_to_phys(struct device *dev, dma_addr_t dev_addr)
> +{
> +	phys_addr_t phys = (phys_addr_t)dev_addr;
> +
> +#ifdef CONFIG_IOMMU_DMA
> +	struct iommu_domain *domain;
> +
> +	domain = iommu_get_domain_for_dev(dev);
> +	if (domain)
> +		phys = iommu_iova_to_phys(domain, dev_addr);
> +#endif
> +
> +	return phys;
> +}
> +EXPORT_SYMBOL(dma_to_phys);
>

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH] arm64: dma-mapping: fix dma_to_phys API for IOMMU attached devices
  2016-03-17 15:57 ` Robin Murphy
@ 2016-03-17 16:07   ` Sinan Kaya
  2016-03-17 16:14     ` Arnd Bergmann
  0 siblings, 1 reply; 6+ messages in thread
From: Sinan Kaya @ 2016-03-17 16:07 UTC (permalink / raw)
  To: linux-arm-kernel

On 3/17/2016 11:57 AM, Robin Murphy wrote:
>> This patch modifies dma_to_phys to call iommu_iova_to_phys to perform dma
>> to phys conversions for IOMMU attached devices where dma and physical
>> addresses often have distinct values.
> 
> What's this for? dma_to_phys() is only used by SWIOTLB, and that's mutually exclusive with IOMMU ops.

If it is SWIOTLB only, then it needs to disappear from common header file as 
it is doing the wrong thing when IOMMU is enabled for a device. 

I have some device driver (HIDMA) that used dma_to_phys during DMA self test.
I needed this patch to get it working properly. I removed the self test from
my code but I still don't feel right about this dma_to_phys API returning physical
addresses.

> 
> Also, what about phys_to_dma()?

Agreed, need to fix phys_to_dma too.

> 
> Robin.

Let me know which direction I should go. 

1. Fix phys_to_dma and keep dma_to_phys as in the patch.
2. Remove both of the API from header file, move it to where it is needed. Rename them as swio_phys_to_dma etc.

-- 
Sinan Kaya
Qualcomm Technologies, Inc. on behalf of Qualcomm Innovation Center, Inc.
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, a Linux Foundation Collaborative Project

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH] arm64: dma-mapping: fix dma_to_phys API for IOMMU attached devices
  2016-03-17 16:07   ` Sinan Kaya
@ 2016-03-17 16:14     ` Arnd Bergmann
  2016-03-17 16:36       ` Sinan Kaya
  0 siblings, 1 reply; 6+ messages in thread
From: Arnd Bergmann @ 2016-03-17 16:14 UTC (permalink / raw)
  To: linux-arm-kernel

On Thursday 17 March 2016 12:07:26 Sinan Kaya wrote:
> On 3/17/2016 11:57 AM, Robin Murphy wrote:
>
> Let me know which direction I should go. 
> 
> 1. Fix phys_to_dma and keep dma_to_phys as in the patch.
> 2. Remove both of the API from header file, move it to where it is needed. Rename them as swio_phys_to_dma etc.

Use 2.

It has been a long way to remove all virt_to_bus/bus_to_virt users
from the common architectures, we are not putting them back under
a different name.

Drivers already know the physical address because that is what they
put into dma_map_*() in the first place.

	Arnd

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH] arm64: dma-mapping: fix dma_to_phys API for IOMMU attached devices
  2016-03-17 16:14     ` Arnd Bergmann
@ 2016-03-17 16:36       ` Sinan Kaya
  2016-03-17 17:01         ` Arnd Bergmann
  0 siblings, 1 reply; 6+ messages in thread
From: Sinan Kaya @ 2016-03-17 16:36 UTC (permalink / raw)
  To: linux-arm-kernel

On 3/17/2016 12:14 PM, Arnd Bergmann wrote:
> On Thursday 17 March 2016 12:07:26 Sinan Kaya wrote:
>> On 3/17/2016 11:57 AM, Robin Murphy wrote:
>>
>> Let me know which direction I should go. 
>>
>> 1. Fix phys_to_dma and keep dma_to_phys as in the patch.
>> 2. Remove both of the API from header file, move it to where it is needed. Rename them as swio_phys_to_dma etc.
> 
> Use 2.
> 
> It has been a long way to remove all virt_to_bus/bus_to_virt users
> from the common architectures, we are not putting them back under
> a different name.
> 
> Drivers already know the physical address because that is what they
> put into dma_map_*() in the first place.
> 
> 	Arnd
> 

The first solution that comes to my mind is to implement a weak function in
swiotlb.c with these contents

dma_addr_t __weak swio_phys_to_dma(struct device *dev, phys_addr_t paddr)
{
        return paddr;
}

 
phys_addr_t __weak  swio_dma_to_phys(struct device *dev, dma_addr_t daddr)
{
        return daddr;
}

then clean up all the duplicates in dma-mapping.h for all ARCHs that have
identical code. 

For others move the implementation to some source file.


-- 
Sinan Kaya
Qualcomm Technologies, Inc. on behalf of Qualcomm Innovation Center, Inc.
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, a Linux Foundation Collaborative Project

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH] arm64: dma-mapping: fix dma_to_phys API for IOMMU attached devices
  2016-03-17 16:36       ` Sinan Kaya
@ 2016-03-17 17:01         ` Arnd Bergmann
  0 siblings, 0 replies; 6+ messages in thread
From: Arnd Bergmann @ 2016-03-17 17:01 UTC (permalink / raw)
  To: linux-arm-kernel

On Thursday 17 March 2016 12:36:28 Sinan Kaya wrote:
> 
> The first solution that comes to my mind is to implement a weak function in
> swiotlb.c with these contents
> 
> dma_addr_t __weak swio_phys_to_dma(struct device *dev, phys_addr_t paddr)
> {
>         return paddr;
> }
> 
>  
> phys_addr_t __weak  swio_dma_to_phys(struct device *dev, dma_addr_t daddr)
> {
>         return daddr;
> }
> 
> then clean up all the duplicates in dma-mapping.h for all ARCHs that have
> identical code. 
> 
> For others move the implementation to some source file.
> 
> 

Sounds ok to me, but I'd prefer using a macro instead of a __weak symbol:

#ifndef swiotlb_phys_to_dma
static inline dma_addr_t swiotlb_phys_to_dma(struct device *dev, phys_addr_t paddr)
{
         return paddr;
}
#endif

and then let the architectures that override it provide a self-referencing
macro:

#define swiotlb_phys_to_dma swiotlb_phys_to_dma

Also note swiotlb instead of swio, to match the existing naming.

	Arnd

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2016-03-17 17:01 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-03-17 15:50 [PATCH] arm64: dma-mapping: fix dma_to_phys API for IOMMU attached devices Sinan Kaya
2016-03-17 15:57 ` Robin Murphy
2016-03-17 16:07   ` Sinan Kaya
2016-03-17 16:14     ` Arnd Bergmann
2016-03-17 16:36       ` Sinan Kaya
2016-03-17 17:01         ` Arnd Bergmann

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).