linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Marek Szyprowski <m.szyprowski@samsung.com>
To: linux-arm-kernel@lists.infradead.org,
	linaro-mm-sig@lists.linaro.org, linux-mm@kvack.org,
	linux-arch@vger.kernel.org, iommu@lists.linux-foundation.org
Cc: Marek Szyprowski <m.szyprowski@samsung.com>,
	Kyungmin Park <kyungmin.park@samsung.com>,
	Arnd Bergmann <arnd@arndb.de>, Joerg Roedel <joro@8bytes.org>,
	Russell King - ARM Linux <linux@arm.linux.org.uk>,
	Chunsang Jeong <chunsang.jeong@linaro.org>,
	Krishna Reddy <vdumpa@nvidia.com>,
	KyongHo Cho <pullip.cho@samsung.com>,
	Andrzej Pietrasiewicz <andrzej.p@samsung.com>,
	Benjamin Herrenschmidt <benh@kernel.crashing.org>,
	Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>,
	Hiroshi Doyu <hdoyu@nvidia.com>,
	Subash Patel <subashrp@gmail.com>
Subject: [PATCHv8 01/10] common: add dma_mmap_from_coherent() function
Date: Tue, 10 Apr 2012 13:04:03 +0200	[thread overview]
Message-ID: <1334055852-19500-2-git-send-email-m.szyprowski@samsung.com> (raw)
In-Reply-To: <1334055852-19500-1-git-send-email-m.szyprowski@samsung.com>

Add a common helper for dma-mapping core for mapping a coherent buffer
to userspace.

Reported-by: Subash Patel <subashrp@gmail.com>
Signed-off-by: Marek Szyprowski <m.szyprowski@samsung.com>
Acked-by: Kyungmin Park <kyungmin.park@samsung.com>
---
 drivers/base/dma-coherent.c        |   42 ++++++++++++++++++++++++++++++++++++
 include/asm-generic/dma-coherent.h |    4 ++-
 2 files changed, 45 insertions(+), 1 deletions(-)

diff --git a/drivers/base/dma-coherent.c b/drivers/base/dma-coherent.c
index bb0025c..1b85949 100644
--- a/drivers/base/dma-coherent.c
+++ b/drivers/base/dma-coherent.c
@@ -10,6 +10,7 @@
 struct dma_coherent_mem {
 	void		*virt_base;
 	dma_addr_t	device_base;
+	phys_addr_t	pfn_base;
 	int		size;
 	int		flags;
 	unsigned long	*bitmap;
@@ -44,6 +45,7 @@ int dma_declare_coherent_memory(struct device *dev, dma_addr_t bus_addr,
 
 	dev->dma_mem->virt_base = mem_base;
 	dev->dma_mem->device_base = device_addr;
+	dev->dma_mem->pfn_base = PFN_DOWN(bus_addr);
 	dev->dma_mem->size = pages;
 	dev->dma_mem->flags = flags;
 
@@ -176,3 +178,43 @@ int dma_release_from_coherent(struct device *dev, int order, void *vaddr)
 	return 0;
 }
 EXPORT_SYMBOL(dma_release_from_coherent);
+
+/**
+ * dma_mmap_from_coherent() - try to mmap the memory allocated from
+ * per-device coherent memory pool to userspace
+ * @dev:	device from which the memory was allocated
+ * @vma:	vm_area for the userspace memory
+ * @vaddr:	cpu address returned by dma_alloc_from_coherent
+ * @size:	size of the memory buffer allocated by dma_alloc_from_coherent
+ *
+ * This checks whether the memory was allocated from the per-device
+ * coherent memory pool and if so, maps that memory to the provided vma.
+ *
+ * Returns 1 if we correctly mapped the memory, or 0 if
+ * dma_release_coherent() should proceed with mapping memory from
+ * generic pools.
+ */
+int dma_mmap_from_coherent(struct device *dev, struct vm_area_struct *vma,
+			   void *vaddr, size_t size, int *ret)
+{
+	struct dma_coherent_mem *mem = dev ? dev->dma_mem : NULL;
+
+	if (mem && vaddr >= mem->virt_base && vaddr + size <=
+		   (mem->virt_base + (mem->size << PAGE_SHIFT))) {
+		unsigned long off = vma->vm_pgoff;
+		int start = (vaddr - mem->virt_base) >> PAGE_SHIFT;
+		int user_count = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
+		int count = size >> PAGE_SHIFT;
+
+		*ret = -ENXIO;
+		if (off < count && user_count <= count - off) {
+			unsigned pfn = mem->pfn_base + start + off;
+			*ret = remap_pfn_range(vma, vma->vm_start, pfn,
+					       user_count << PAGE_SHIFT,
+					       vma->vm_page_prot);
+		}
+		return 1;
+	}
+	return 0;
+}
+EXPORT_SYMBOL(dma_mmap_from_coherent);
diff --git a/include/asm-generic/dma-coherent.h b/include/asm-generic/dma-coherent.h
index 85a3ffa..abfb268 100644
--- a/include/asm-generic/dma-coherent.h
+++ b/include/asm-generic/dma-coherent.h
@@ -3,13 +3,15 @@
 
 #ifdef CONFIG_HAVE_GENERIC_DMA_COHERENT
 /*
- * These two functions are only for dma allocator.
+ * These three functions are only for dma allocator.
  * Don't use them in device drivers.
  */
 int dma_alloc_from_coherent(struct device *dev, ssize_t size,
 				       dma_addr_t *dma_handle, void **ret);
 int dma_release_from_coherent(struct device *dev, int order, void *vaddr);
 
+int dma_mmap_from_coherent(struct device *dev, struct vm_area_struct *vma,
+			    void *cpu_addr, size_t size, int *ret);
 /*
  * Standard interface
  */
-- 
1.7.1.569.g6f426

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org.  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>

  reply	other threads:[~2012-04-10 11:04 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-04-10 11:04 [PATCHv8 00/10] ARM: DMA-mapping framework redesign Marek Szyprowski
2012-04-10 11:04 ` Marek Szyprowski [this message]
2012-04-10 11:04 ` [PATCHv8 02/10] ARM: dma-mapping: use pr_* instread of printk Marek Szyprowski
2012-04-10 11:41   ` Arnd Bergmann
2012-04-10 11:04 ` [PATCHv8 03/10] ARM: dma-mapping: introduce ARM_DMA_ERROR constant Marek Szyprowski
2012-04-10 11:31   ` Arnd Bergmann
2012-04-10 12:53     ` Marek Szyprowski
2012-04-10 11:04 ` [PATCHv8 04/10] ARM: dma-mapping: remove offset parameter to prepare for generic dma_ops Marek Szyprowski
2012-04-10 11:43   ` Arnd Bergmann
2012-04-11 12:05     ` Marek Szyprowski
2012-04-11 12:18       ` Arnd Bergmann
2012-04-11 13:05         ` Marek Szyprowski
2012-04-10 11:04 ` [PATCHv8 05/10] ARM: dma-mapping: use asm-generic/dma-mapping-common.h Marek Szyprowski
2012-04-10 11:47   ` Arnd Bergmann
2012-04-11 12:07     ` Marek Szyprowski
2012-04-10 11:04 ` [PATCHv8 06/10] ARM: dma-mapping: implement dma sg methods on top of any generic dma ops Marek Szyprowski
2012-04-10 11:04 ` [PATCHv8 07/10] ARM: dma-mapping: move all dma bounce code to separate dma ops structure Marek Szyprowski
2012-04-10 12:24   ` Arnd Bergmann
2012-04-10 12:51     ` Marek Szyprowski
2012-04-10 13:17       ` Arnd Bergmann
2012-04-10 11:04 ` [PATCHv8 08/10] ARM: dma-mapping: remove redundant code and cleanup Marek Szyprowski
2012-04-10 12:22   ` Arnd Bergmann
2012-04-10 11:04 ` [PATCHv8 09/10] ARM: dma-mapping: use alloc, mmap, free from dma_ops Marek Szyprowski
2012-04-10 12:21   ` Arnd Bergmann
2012-04-10 11:04 ` [PATCHv8 10/10] ARM: dma-mapping: add support for IOMMU mapper Marek Szyprowski
2012-04-10 11:58   ` Arnd Bergmann
2012-04-10 12:57     ` Marek Szyprowski
2012-04-12  9:49     ` Marek Szyprowski
2012-04-18  7:14 ` [PATCHv8 00/10] ARM: DMA-mapping framework redesign Subash Patel

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1334055852-19500-2-git-send-email-m.szyprowski@samsung.com \
    --to=m.szyprowski@samsung.com \
    --cc=andrzej.p@samsung.com \
    --cc=arnd@arndb.de \
    --cc=benh@kernel.crashing.org \
    --cc=chunsang.jeong@linaro.org \
    --cc=hdoyu@nvidia.com \
    --cc=iommu@lists.linux-foundation.org \
    --cc=joro@8bytes.org \
    --cc=konrad.wilk@oracle.com \
    --cc=kyungmin.park@samsung.com \
    --cc=linaro-mm-sig@lists.linaro.org \
    --cc=linux-arch@vger.kernel.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-mm@kvack.org \
    --cc=linux@arm.linux.org.uk \
    --cc=pullip.cho@samsung.com \
    --cc=subashrp@gmail.com \
    --cc=vdumpa@nvidia.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox