From patchwork Thu May 17 16:53:06 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Szyprowski X-Patchwork-Id: 8768 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 9027823E49 for ; Thu, 17 May 2012 16:53:39 +0000 (UTC) Received: from mail-yx0-f180.google.com (mail-yx0-f180.google.com [209.85.213.180]) by fiordland.canonical.com (Postfix) with ESMTP id 4A0D5A18B7E for ; Thu, 17 May 2012 16:53:39 +0000 (UTC) Received: by yenq6 with SMTP id q6so2409582yen.11 for ; Thu, 17 May 2012 09:53:38 -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=gg2JzoDX/PrIHfgmaO3bDainn8nJpL/KCaoHAUgJCs0=; b=MfRmA00jLvSOOikoMTBN9JpEOprwTSG+4fPL9zlg4AwvPnpUc/CZfNY/49D549fPYr F6L5yRp2G75MUIk+tFmKd4O/RnQ5N1G+SRmunVcQ2AwbIuvotT/lus19moAgBvF+SJ0Y GGEguPNw7khxfL4Z3SBO5r8QXufrUwxYQULXRL9yiWRYM3ZqcRf+nJS5nyqbOcsp4U2t Q7/8Ts0zMRllgHxeiL6AIMBTyP6FF/tS/KSVoyvrPpkuQ7sTSW6bfGPjsShq4O5yy8yt mxxrfS8ZGTpJiC3Q9zzReFNgj/8nCj4Azs7fOea3Yrq1QBZotbmr9YpVqvilEdLMOKYJ bkLg== Received: by 10.50.85.163 with SMTP id i3mr6186270igz.57.1337273618470; Thu, 17 May 2012 09:53:38 -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.35.72 with SMTP id o8csp41936ibd; Thu, 17 May 2012 09:53:35 -0700 (PDT) Received: by 10.180.81.234 with SMTP id d10mr19972246wiy.10.1337273615048; Thu, 17 May 2012 09:53:35 -0700 (PDT) Received: from mombin.canonical.com (mombin.canonical.com. [91.189.95.16]) by mx.google.com with ESMTP id r2si9064469wir.21.2012.05.17.09.53.33; Thu, 17 May 2012 09:53:35 -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 1SV3xD-000079-9P; Thu, 17 May 2012 16:53:31 +0000 Received: from mailout4.w1.samsung.com ([210.118.77.14]) by mombin.canonical.com with esmtp (Exim 4.71) (envelope-from ) id 1SV3xC-000065-4G for linaro-mm-sig@lists.linaro.org; Thu, 17 May 2012 16:53:30 +0000 MIME-version: 1.0 Received: from euspt1 ([210.118.77.14]) by mailout4.w1.samsung.com (Sun Java(tm) System Messaging Server 6.3-8.04 (built Jul 29 2009; 32bit)) with ESMTP id <0M46005KDEXJHY70@mailout4.w1.samsung.com> for linaro-mm-sig@lists.linaro.org; Thu, 17 May 2012 17:53:43 +0100 (BST) Received: from ubuntu.arm.acom ([106.210.236.191]) by spt1.w1.samsung.com (iPlanet Messaging Server 5.2 Patch 2 (built Jul 14 2004)) with ESMTPA id <0M46008IQEWVMY@spt1.w1.samsung.com> for linaro-mm-sig@lists.linaro.org; Thu, 17 May 2012 17:53:28 +0100 (BST) Date: Thu, 17 May 2012 18:53:06 +0200 From: Marek Szyprowski In-reply-to: <1337273586-11089-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-kernel@vger.kernel.org Message-id: <1337273586-11089-4-git-send-email-m.szyprowski@samsung.com> X-Mailer: git-send-email 1.7.10.1 References: <1337273586-11089-1-git-send-email-m.szyprowski@samsung.com> Cc: Abhinav Kochhar , Russell King - ARM Linux , Arnd Bergmann , Konrad Rzeszutek Wilk , Benjamin Herrenschmidt , Kyungmin Park , Subash Patel Subject: [Linaro-mm-sig] [PATCH 3/3] ARM: dma-mapping: add support for dma_get_sgtable() 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: ALoCoQkRUv0WjSRvXxy7tnOZYwO28kb8QYBFLkD/09glehfnkHuyd2HsbGxcTDEB8WYJkOUZooO7 This patch adds dma_get_sgtable() function which is required to let drivers to share the buffers allocated by DMA-mapping subsystem. Right now the driver gets a dma address of the allocated buffer and the kernel virtual mapping for it. If it wants to share it with other device (= map into its dma address space) it usually hacks around kernel virtual addresses to get pointers to pages or assumes that both devices share the DMA address space. Both solutions are just hacks for the special cases, which should be avoided in the final version of buffer sharing. To solve this issue in a generic way, a new call to DMA mapping has been introduced - dma_get_sgtable(). It allocates a scatter-list which describes the allocated buffer and lets the driver(s) to use it with other device(s) by calling dma_map_sg() on it. This patch adds this extension only to ARM architecture, mainly to demonstrate the buffer sharing. I plan to provide some generic implementations for other architectures once this idea gets accepted. Signed-off-by: Marek Szyprowski --- arch/arm/include/asm/dma-mapping.h | 12 ++++++++++++ arch/arm/mm/dma-mapping.c | 31 +++++++++++++++++++++++++++++++ include/linux/dma-mapping.h | 3 +++ 3 files changed, 46 insertions(+) diff --git a/arch/arm/include/asm/dma-mapping.h b/arch/arm/include/asm/dma-mapping.h index 80777d87..2e37778 100644 --- a/arch/arm/include/asm/dma-mapping.h +++ b/arch/arm/include/asm/dma-mapping.h @@ -221,6 +221,15 @@ static inline int dma_mmap_writecombine(struct device *dev, struct vm_area_struc return dma_mmap_attrs(dev, vma, cpu_addr, dma_addr, size, &attrs); } +static inline int dma_get_sgtable(struct device *dev, struct sg_table *sgt, + void *cpu_addr, dma_addr_t dma_addr, + size_t size, struct dma_attrs *attrs) +{ + struct dma_map_ops *ops = get_dma_ops(dev); + BUG_ON(!ops); + return ops->get_sgtable(dev, sgt, cpu_addr, dma_addr, size, attrs); +} + /* * This can be called during boot to increase the size of the consistent * DMA region above it's default value of 2MB. It must be called before the @@ -280,6 +289,9 @@ extern void arm_dma_sync_sg_for_cpu(struct device *, struct scatterlist *, int, enum dma_data_direction); extern void arm_dma_sync_sg_for_device(struct device *, struct scatterlist *, int, enum dma_data_direction); +extern int arm_dma_get_sgtable(struct device *dev, struct sg_table *sgt, + void *cpu_addr, dma_addr_t dma_addr, size_t size, + struct dma_attrs *attrs); #endif /* __KERNEL__ */ #endif diff --git a/arch/arm/mm/dma-mapping.c b/arch/arm/mm/dma-mapping.c index 23d0ace..b140440 100644 --- a/arch/arm/mm/dma-mapping.c +++ b/arch/arm/mm/dma-mapping.c @@ -120,6 +120,7 @@ struct dma_map_ops arm_dma_ops = { .alloc = arm_dma_alloc, .free = arm_dma_free, .mmap = arm_dma_mmap, + .get_sgtable = arm_dma_get_sgtable, .map_page = arm_dma_map_page, .unmap_page = arm_dma_unmap_page, .map_sg = arm_dma_map_sg, @@ -529,6 +530,21 @@ void arm_dma_free(struct device *dev, size_t size, void *cpu_addr, __dma_free_buffer(pfn_to_page(dma_to_pfn(dev, handle)), size); } +int arm_dma_get_sgtable(struct device *dev, struct sg_table *sgt, + void *cpu_addr, dma_addr_t handle, size_t size, + struct dma_attrs *attrs) +{ + struct page *page = pfn_to_page(dma_to_pfn(dev, handle)); + int ret; + + ret = sg_alloc_table(sgt, 1, GFP_KERNEL); + if (unlikely(ret)) + return ret; + + sg_set_page(sgt->sgl, page, PAGE_ALIGN(size), 0); + return 0; +} + static void dma_cache_maint_page(struct page *page, unsigned long offset, size_t size, enum dma_data_direction dir, void (*op)(const void *, size_t, int)) @@ -1042,6 +1058,20 @@ void arm_iommu_free_attrs(struct device *dev, size_t size, void *cpu_addr, __iommu_free_buffer(dev, pages, size); } +static int arm_iommu_get_sgtable(struct device *dev, struct sg_table *sgt, + void *cpu_addr, dma_addr_t dma_addr, + size_t size, struct dma_attrs *attrs) +{ + unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT; + struct page **pages = __iommu_get_pages(cpu_addr, attrs); + + if (!pages) + return -ENXIO; + + return sg_alloc_table_from_pages(sgt, pages, count, 0, size, + GFP_KERNEL); +} + /* * Map a part of the scatter-gather list into contiguous io address space */ @@ -1301,6 +1331,7 @@ struct dma_map_ops iommu_ops = { .alloc = arm_iommu_alloc_attrs, .free = arm_iommu_free_attrs, .mmap = arm_iommu_mmap_attrs, + .get_sgtable = arm_iommu_get_sgtable, .map_page = arm_iommu_map_page, .unmap_page = arm_iommu_unmap_page, diff --git a/include/linux/dma-mapping.h b/include/linux/dma-mapping.h index dfc099e..94af418 100644 --- a/include/linux/dma-mapping.h +++ b/include/linux/dma-mapping.h @@ -18,6 +18,9 @@ struct dma_map_ops { int (*mmap)(struct device *, struct vm_area_struct *, void *, dma_addr_t, size_t, struct dma_attrs *attrs); + int (*get_sgtable)(struct device *dev, struct sg_table *sgt, void *, + dma_addr_t, size_t, struct dma_attrs *attrs); + dma_addr_t (*map_page)(struct device *dev, struct page *page, unsigned long offset, size_t size, enum dma_data_direction dir,