Skip to contentSkip to conversation

arc: Implement arch-specific dma_map_ops.mmap

PATCH In review Alexey Brodkin 3 participants 4 messages Nov 3, 2016 – Nov 15, 2016

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

PATCH v2 1/1 arc: Implement arch-specific dma_map_ops.mmap

We used to use generic implementation of dma_map_ops.mmap which is dma_common_mmap() but that only worked for simpler cached mappings when vaddr = paddr.

If a driver requests uncached DMA buffer kernel maps it to virtual address so that MMU gets involved and page uncached status takes into account. In that case usage of dma_common_mmap() lead to mapping of vaddr to vaddr for user-space which is obviously wrong. For more detals please refer to verbose explanation here [1].

So here we implement our own version of mmap() which always deals with dma_addr and maps underlying memory to user-space properly (note that DMA buffer mapped to user-space is always uncached because there's no way to properly manage cache from user-space).

[1] https://lkml.org/lkml/2016/10/26/973

Signed-off-by Alexey Brodkin <abrodkin at synopsys.com>
Reviewed-by Catalin Marinas <catalin.marinas at arm.com>
Cc Marek Szyprowski <m.szyprowski at samsung.com>
Cc Vineet Gupta <vgupta at synopsys.com>
Cc <stable at vger.kernel.org>

---

Changes v1 -> v2:
* Added plat_dma_to_phys wrapper around dma_addr

 arch/arc/mm/dma.c | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)
arch/arc/mm/dma.c +26 -0
index 20afc65e22dc..9288851d43a0 100644
@@ -105,6 +105,31 @@ static void arc_dma_free(struct device *dev, size_t size, void *vaddr,
 	__free_pages(page, get_order(size));
 }
 
+static int arc_dma_mmap(struct device *dev, struct vm_area_struct *vma,
+			void *cpu_addr, dma_addr_t dma_addr, size_t size,
+			unsigned long attrs)
+{
+	unsigned long user_count = vma_pages(vma);
+	unsigned long count = PAGE_ALIGN(size) >> PAGE_SHIFT;
+	unsigned long pfn = __phys_to_pfn(plat_dma_to_phys(dev, dma_addr));
+	unsigned long off = vma->vm_pgoff;
+	int ret = -ENXIO;
+
+	vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
+
+	if (dma_mmap_from_coherent(dev, vma, cpu_addr, size, &ret))
+		return ret;
+
+	if (off < count && user_count <= (count - off)) {
+		ret = remap_pfn_range(vma, vma->vm_start,
+				      pfn + off,
+				      user_count << PAGE_SHIFT,
+				      vma->vm_page_prot);
+	}
+
+	return ret;
+}
+
 /*
  * streaming DMA Mapping API...
  * CPU accesses page via normal paddr, thus needs to explicitly made
@@ -193,6 +218,7 @@ static int arc_dma_supported(struct device *dev, u64 dma_mask)
 struct dma_map_ops arc_dma_ops = {
 	.alloc			= arc_dma_alloc,
 	.free			= arc_dma_free,
+	.mmap			= arc_dma_mmap,
 	.map_page		= arc_dma_map_page,
 	.map_sg			= arc_dma_map_sg,
 	.sync_single_for_device	= arc_dma_sync_single_for_device,
Signature · 1 line
2.7.4
#1 lore raw
Quoted 15 lines · Alexey Brodkin

We used to use generic implementation of dma_map_ops.mmap which is dma_common_mmap() but that only worked for simpler cached mappings when vaddr = paddr.

If a driver requests uncached DMA buffer kernel maps it to virtual address so that MMU gets involved and page uncached status takes into account. In that case usage of dma_common_mmap() lead to mapping of vaddr to vaddr for user-space which is obviously wrong. For more detals please refer to verbose explanation here [1].

So here we implement our own version of mmap() which always deals with dma_addr and maps underlying memory to user-space properly (note that DMA buffer mapped to user-space is always uncached because there's no way to properly manage cache from user-space).

Quoted 7 lines

[1] https://lkml.org/lkml/2016/10/26/973

Signed-off-by Alexey Brodkin <abrodkin at synopsys.com>
Reviewed-by Catalin Marinas <catalin.marinas at arm.com>
Cc Marek Szyprowski <m.szyprowski at samsung.com>
Cc Vineet Gupta <vgupta at synopsys.com>
Cc <stable at vger.kernel.org>

I've added a stable 4.5+, since ARC didn't use dma ops until 4.5-rc1. Pushed to for-curr !

Thx,
-Vineet

Quoted 53 lines

---

Changes v1 -> v2:
* Added plat_dma_to_phys wrapper around dma_addr

 arch/arc/mm/dma.c | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)
arch/arc/mm/dma.c +26 -0
index 20afc65e22dc..9288851d43a0 100644
@@ -105,6 +105,31 @@ static void arc_dma_free(struct device *dev, size_t size, void *vaddr,
 	__free_pages(page, get_order(size));
 }
 
+static int arc_dma_mmap(struct device *dev, struct vm_area_struct *vma,
+			void *cpu_addr, dma_addr_t dma_addr, size_t size,
+			unsigned long attrs)
+{
+	unsigned long user_count = vma_pages(vma);
+	unsigned long count = PAGE_ALIGN(size) >> PAGE_SHIFT;
+	unsigned long pfn = __phys_to_pfn(plat_dma_to_phys(dev, dma_addr));
+	unsigned long off = vma->vm_pgoff;
+	int ret = -ENXIO;
+
+	vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
+
+	if (dma_mmap_from_coherent(dev, vma, cpu_addr, size, &ret))
+		return ret;
+
+	if (off < count && user_count <= (count - off)) {
+		ret = remap_pfn_range(vma, vma->vm_start,
+				      pfn + off,
+				      user_count << PAGE_SHIFT,
+				      vma->vm_page_prot);
+	}
+
+	return ret;
+}
+
 /*
  * streaming DMA Mapping API...
  * CPU accesses page via normal paddr, thus needs to explicitly made
@@ -193,6 +218,7 @@ static int arc_dma_supported(struct device *dev, u64 dma_mask)
 struct dma_map_ops arc_dma_ops = {
 	.alloc			= arc_dma_alloc,
 	.free			= arc_dma_free,
+	.mmap			= arc_dma_mmap,
 	.map_page		= arc_dma_map_page,
 	.map_sg			= arc_dma_map_sg,
 	.sync_single_for_device	= arc_dma_sync_single_for_device,
#2 lore raw

Hi Vineet,

Quoted 26 lines

-----Original Message-----
From: Vineet Gupta [mailto:vgupta at synopsys.com]
Sent: Thursday, November 03, 2016 8:04 PM
To: Alexey Brodkin <Alexey.Brodkin at synopsys.com>; linux-snps-arc at lists.infradead.org
Cc: linux-kernel at vger.kernel.org; linux-arch at vger.kernel.org; Vineet Gupta <Vineet.Gupta1 at synopsys.com>; Marek Szyprowski <m.szyprowski at samsung.com>; stable at vger.kernel.org
Subject: Re: [PATCH v2] arc: Implement arch-specific dma_map_ops.mmap

Quoted 17 lines · Alexey Brodkin

We used to use generic implementation of dma_map_ops.mmap which is dma_common_mmap() but that only worked for simpler cached mappings when vaddr = paddr.

If a driver requests uncached DMA buffer kernel maps it to virtual address so that MMU gets involved and page uncached status takes into account. In that case usage of dma_common_mmap() lead to mapping of vaddr to vaddr for user-space which is obviously wrong. For more detals please refer to verbose explanation here [1].

So here we implement our own version of mmap() which always deals with dma_addr and maps underlying memory to user-space properly (note that DMA buffer mapped to user-space is always uncached because there's no way to properly manage cache from user-space).

[1] https://lkml.org/lkml/2016/10/26/973

Quoted 7 lines
Quoted 5 lines
Signed-off-by Alexey Brodkin <abrodkin at synopsys.com>
Reviewed-by Catalin Marinas <catalin.marinas at arm.com>
Cc Marek Szyprowski <m.szyprowski at samsung.com>
Cc Vineet Gupta <vgupta at synopsys.com>
Cc <stable at vger.kernel.org>

I've added a stable 4.5+, since ARC didn't use dma ops until 4.5-rc1.

Again I was hitting a strange problem when sending patch via "git send-email" to address "<stable at vger.kernel.org> # 3.3.x". Mail server complains on wrong email. Thus I settled to just mention stable at vger.kernel.org.

Anyways, thanks for doing that!

-Alexey

Tested-by on v2 1/1
#3 lore raw
In reply to Alexey Brodkin
Quoted 15 lines · Alexey Brodkin

We used to use generic implementation of dma_map_ops.mmap which is dma_common_mmap() but that only worked for simpler cached mappings when vaddr = paddr.

If a driver requests uncached DMA buffer kernel maps it to virtual address so that MMU gets involved and page uncached status takes into account. In that case usage of dma_common_mmap() lead to mapping of vaddr to vaddr for user-space which is obviously wrong. For more detals please refer to verbose explanation here [1].

So here we implement our own version of mmap() which always deals with dma_addr and maps underlying memory to user-space properly (note that DMA buffer mapped to user-space is always uncached because there's no way to properly manage cache from user-space).

Quoted 8 lines

[1] https://lkml.org/lkml/2016/10/26/973

Signed-off-by Alexey Brodkin <abrodkin at synopsys.com>
Reviewed-by Catalin Marinas <catalin.marinas at arm.com>
Cc Marek Szyprowski <m.szyprowski at samsung.com>
Cc Vineet Gupta <vgupta at synopsys.com>
Cc <stable at vger.kernel.org>

---

Tested-by Ramiro Oliveira <Ramiro.Oliveira at synopsys.com>