diff mbox

[v5,2/7] dma: Add simple dma_noop_mmap

Message ID 1495621472-9323-3-git-send-email-vladimir.murzin@arm.com (mailing list archive)
State New, archived
Headers show

Commit Message

Vladimir Murzin May 24, 2017, 10:24 a.m. UTC
This patch adds a simple implementation of mmap to dma_noop_ops.

Cc: Joerg Roedel <jroedel@suse.de>
Cc: Christian Borntraeger <borntraeger@de.ibm.com>
Reported-by: Benjamin Gaignard <benjamin.gaignard@linaro.org>
Tested-by: Benjamin Gaignard <benjamin.gaignard@linaro.org>
Tested-by: Andras Szemzo <sza@esh.hu>
Tested-by: Alexandre TORGUE <alexandre.torgue@st.com>
Signed-off-by: Vladimir Murzin <vladimir.murzin@arm.com>
---
 lib/dma-noop.c | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)

Comments

Christoph Hellwig June 20, 2017, 1:23 p.m. UTC | #1
On Wed, May 24, 2017 at 11:24:27AM +0100, Vladimir Murzin wrote:
> This patch adds a simple implementation of mmap to dma_noop_ops.

Currently we use dma_common_mmap as the generic fallback if a dma_ops
instance doesn't implement a mmap method.  Can you just fix up
dma_common_mmap for your nommu case - it looks like yours is basically
a subset.

In fact I suspect yours should call dma_mmap_from_coherent as well,
so the only different is the lack of pgprot_noncached call.
Vladimir Murzin June 22, 2017, 12:46 p.m. UTC | #2
On 20/06/17 14:23, Christoph Hellwig wrote:
> On Wed, May 24, 2017 at 11:24:27AM +0100, Vladimir Murzin wrote:
>> This patch adds a simple implementation of mmap to dma_noop_ops.
> 
> Currently we use dma_common_mmap as the generic fallback if a dma_ops
> instance doesn't implement a mmap method.  Can you just fix up
> dma_common_mmap for your nommu case - it looks like yours is basically
> a subset.

dma_common_map() is guarded with CONFIG_MMU and I'm not dare to change that
since I have no idea 1) why it was done 2) how it affects other arches :(

> 
> In fact I suspect yours should call dma_mmap_from_coherent as well,
> so the only different is the lack of pgprot_noncached call.
> 

OK, I'll add a call to dma_mmap_from_coherent().

Cheers
Vladimir
Christoph Hellwig June 26, 2017, 9:45 a.m. UTC | #3
On Thu, Jun 22, 2017 at 01:46:09PM +0100, Vladimir Murzin wrote:
> On 20/06/17 14:23, Christoph Hellwig wrote:
> > On Wed, May 24, 2017 at 11:24:27AM +0100, Vladimir Murzin wrote:
> >> This patch adds a simple implementation of mmap to dma_noop_ops.
> > 
> > Currently we use dma_common_mmap as the generic fallback if a dma_ops
> > instance doesn't implement a mmap method.  Can you just fix up
> > dma_common_mmap for your nommu case - it looks like yours is basically
> > a subset.
> 
> dma_common_map() is guarded with CONFIG_MMU and I'm not dare to change that
> since I have no idea 1) why it was done 2) how it affects other arches :(

That's still not a good reason for duplicating it.  Please instead
make all the existing nommu architecture select
CONFIG_ARCH_NO_COHERENT_DMA_MMAP as a prep patch, then provide an
arch helper to abstract away the pgprot_noncached, then lift
CONFIG_ARCH_NO_COHERENT_DMA_MMAP for arm nommu.  Bonus points
for lifting it elsewhere or consolidating various copy & paste
versions of the function.
diff mbox

Patch

diff --git a/lib/dma-noop.c b/lib/dma-noop.c
index ff4ef5e..0acc3f6 100644
--- a/lib/dma-noop.c
+++ b/lib/dma-noop.c
@@ -66,6 +66,26 @@  static int dma_noop_supported(struct device *dev, u64 mask)
 	return 1;
 }
 
+static int dma_noop_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 = page_to_pfn(virt_to_page(cpu_addr));
+	unsigned long off = vma->vm_pgoff;
+	int ret = -ENXIO;
+
+	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;
+}
+
 const struct dma_map_ops dma_noop_ops = {
 	.alloc			= dma_noop_alloc,
 	.free			= dma_noop_free,
@@ -73,6 +93,7 @@  const struct dma_map_ops dma_noop_ops = {
 	.map_sg			= dma_noop_map_sg,
 	.mapping_error		= dma_noop_mapping_error,
 	.dma_supported		= dma_noop_supported,
+	.mmap			= dma_noop_mmap,
 };
 
 EXPORT_SYMBOL(dma_noop_ops);