From patchwork Fri Mar 23 12:26:02 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Szyprowski X-Patchwork-Id: 7432 Return-Path: X-Original-To: patchwork@peony.canonical.com Delivered-To: patchwork@peony.canonical.com Received: from fiordland.canonical.com (fiordland.canonical.com [91.189.94.145]) by peony.canonical.com (Postfix) with ESMTP id 57FA323E00 for ; Fri, 23 Mar 2012 12:26:23 +0000 (UTC) Received: from mail-iy0-f180.google.com (mail-iy0-f180.google.com [209.85.210.180]) by fiordland.canonical.com (Postfix) with ESMTP id 0518AA18505 for ; Fri, 23 Mar 2012 12:26:22 +0000 (UTC) Received: by iage36 with SMTP id e36so6064136iag.11 for ; Fri, 23 Mar 2012 05:26:22 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=x-forwarded-to:x-forwarded-for:delivered-to:received-spf :mime-version:date:from:in-reply-to:to:message-id:x-mailer :references:cc:subject:x-beenthere:x-mailman-version:precedence :list-id:list-unsubscribe:list-archive:list-post:list-help :list-subscribe:content-type:content-transfer-encoding:sender :errors-to:x-gm-message-state; bh=Fu8cnrUVj2Rppa/ih2373KtFatVw/9SjSVZMRGo7Y5U=; b=cYh2HXa9Fs3HkonclEEsMMifiw4Dhfo4PMIz0Od72YqjjgUO+IhedZki9oIBoepmAo OBX3v0LKNDSbSk0bo+9qkaFFhnhe8ghqWTX8lAkdGVX2a023uOYgV044DMcaUJhE52J6 jguK3EszFLpeQRSje+rY1aPIrhhRcoF4qpDi3wZ5GFV2hacd0/CXJnDsirQ4As0NhEPt nXoJH20B7yiu4K6QFY0G6Xb6a/brfXLXcWIG0JnXF+Dx4zGomysdI37k3Teo78qQ+zSK YxYSkhNPT+dst7vLL0Iwsd8HujCUKXx0hA0QC7xLn3HJ8B6isy0piPzLPtf5KfaBRiLc 3+ag== Received: by 10.50.153.132 with SMTP id vg4mr1493954igb.2.1332505582463; Fri, 23 Mar 2012 05:26:22 -0700 (PDT) X-Forwarded-To: linaro-patchwork@canonical.com X-Forwarded-For: patch@linaro.org linaro-patchwork@canonical.com Delivered-To: patches@linaro.org Received: by 10.231.203.79 with SMTP id fh15csp21795ibb; Fri, 23 Mar 2012 05:26:21 -0700 (PDT) Received: by 10.216.137.156 with SMTP id y28mr6848049wei.61.1332505576204; Fri, 23 Mar 2012 05:26:16 -0700 (PDT) Received: from mombin.canonical.com (mombin.canonical.com. [91.189.95.16]) by mx.google.com with ESMTP id ci1si5747061wib.7.2012.03.23.05.26.15; Fri, 23 Mar 2012 05:26:16 -0700 (PDT) Received-SPF: neutral (google.com: 91.189.95.16 is neither permitted nor denied by best guess record for domain of linaro-mm-sig-bounces@lists.linaro.org) client-ip=91.189.95.16; Authentication-Results: mx.google.com; spf=neutral (google.com: 91.189.95.16 is neither permitted nor denied by best guess record for domain of linaro-mm-sig-bounces@lists.linaro.org) smtp.mail=linaro-mm-sig-bounces@lists.linaro.org Received: from localhost ([127.0.0.1] helo=mombin.canonical.com) by mombin.canonical.com with esmtp (Exim 4.71) (envelope-from ) id 1SB3ZN-0006Vt-VM; Fri, 23 Mar 2012 12:26:14 +0000 Received: from mailout3.w1.samsung.com ([210.118.77.13]) by mombin.canonical.com with esmtp (Exim 4.71) (envelope-from ) id 1SB3ZM-0005SA-E4 for linaro-mm-sig@lists.linaro.org; Fri, 23 Mar 2012 12:26:12 +0000 MIME-version: 1.0 Received: from euspt1 ([210.118.77.13]) by mailout3.w1.samsung.com (Sun Java(tm) System Messaging Server 6.3-8.04 (built Jul 29 2009; 32bit)) with ESMTP id <0M1C00CT97VE8W30@mailout3.w1.samsung.com> for linaro-mm-sig@lists.linaro.org; Fri, 23 Mar 2012 12:26:02 +0000 (GMT) Received: from linux.samsung.com ([106.116.38.10]) by spt1.w1.samsung.com (iPlanet Messaging Server 5.2 Patch 2 (built Jul 14 2004)) with ESMTPA id <0M1C00MBR7VLND@spt1.w1.samsung.com> for linaro-mm-sig@lists.linaro.org; Fri, 23 Mar 2012 12:26:10 +0000 (GMT) Received: from mcdsrvbld02.digital.local (unknown [106.116.37.23]) by linux.samsung.com (Postfix) with ESMTP id 023E0270058; Fri, 23 Mar 2012 13:27:10 +0100 (CET) Date: Fri, 23 Mar 2012 13:26:02 +0100 From: Marek Szyprowski In-reply-to: <1332505563-17646-1-git-send-email-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, linux-samsung-soc@vger.kernel.org, iommu@lists.linux-foundation.org Message-id: <1332505563-17646-2-git-send-email-m.szyprowski@samsung.com> X-Mailer: git-send-email 1.7.9.1 References: <08af01cd08ee$2fd04770$8f70d650$%szyprowski@samsung.com> <1332505563-17646-1-git-send-email-m.szyprowski@samsung.com> Cc: Russell King - ARM Linux , Arnd Bergmann , Konrad Rzeszutek Wilk , Benjamin Herrenschmidt , Kyungmin Park , Andrzej Pietrasiewicz , KyongHo Cho , Joerg Roedel Subject: [Linaro-mm-sig] [PATCH 1/2] common: add dma_mmap_from_coherent() function X-BeenThere: linaro-mm-sig@lists.linaro.org X-Mailman-Version: 2.1.13 Precedence: list List-Id: "Unified memory management interest group." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: linaro-mm-sig-bounces@lists.linaro.org Errors-To: linaro-mm-sig-bounces@lists.linaro.org X-Gm-Message-State: ALoCoQkYRe1qrU1h92+COggbHc/XRZSmasHUDbaoj6NFCaJcj27ESHckthU+rPrVkMYpLM9TG871 Add a common helper for dma-mapping core for mapping a coherent buffer to userspace. Signed-off-by: Marek Szyprowski --- 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..ef5ae05 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..b82051d 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 */