From mboxrd@z Thu Jan 1 00:00:00 1970 From: Marek Szyprowski Subject: RE: [PATCHv7 9/9] ARM: dma-mapping: add support for IOMMU mapper Date: Thu, 29 Mar 2012 10:00:14 +0200 Message-ID: <01b301cd0d81$f935d750$eba185f0$%szyprowski@samsung.com> References: <1330527862-16234-1-git-send-email-m.szyprowski@samsung.com> <1330527862-16234-10-git-send-email-m.szyprowski@samsung.com> <20120329101927.8ab6b1993475b7e16ae2258f@nvidia.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7BIT Return-path: In-reply-to: <20120329101927.8ab6b1993475b7e16ae2258f@nvidia.com> Content-language: pl Sender: owner-linux-mm@kvack.org To: 'Hiroshi Doyu' Cc: 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, 'Shariq Hasnain' , 'Arnd Bergmann' , 'Benjamin Herrenschmidt' , 'Krishna Reddy' , 'Kyungmin Park' , Andrzej Pietrasiewicz , 'Russell King - ARM Linux' , 'KyongHo Cho' , 'Chunsang Jeong' List-Id: linux-arch.vger.kernel.org Hello, On Thursday, March 29, 2012 9:19 AM Hiroshi Doyu wrote: > On Wed, 29 Feb 2012 16:04:22 +0100 > Marek Szyprowski wrote: > > > This patch add a complete implementation of DMA-mapping API for > > devices that have IOMMU support. All DMA-mapping calls are supported. > > > > This patch contains some of the code kindly provided by Krishna Reddy > > and Andrzej Pietrasiewicz > > > > Signed-off-by: Marek Szyprowski > > Signed-off-by: Kyungmin Park > > Reviewed-by: Konrad Rzeszutek Wilk > > --- > > arch/arm/Kconfig | 8 + > > arch/arm/include/asm/device.h | 3 + > > arch/arm/include/asm/dma-iommu.h | 34 ++ > > arch/arm/mm/dma-mapping.c | 726 +++++++++++++++++++++++++++++++++++++- > > arch/arm/mm/vmregion.h | 2 +- > > 5 files changed, 758 insertions(+), 15 deletions(-) > > create mode 100644 arch/arm/include/asm/dma-iommu.h > > > > > > > +/* > > + * Map a part of the scatter-gather list into contiguous io address space > > + */ > > +static int __map_sg_chunk(struct device *dev, struct scatterlist *sg, > > + size_t size, dma_addr_t *handle, > > + enum dma_data_direction dir) > > +{ > > + struct dma_iommu_mapping *mapping = dev->archdata.mapping; > > + dma_addr_t iova, iova_base; > > + int ret = 0; > > + unsigned int count; > > + struct scatterlist *s; > > + > > + size = PAGE_ALIGN(size); > > + *handle = ARM_DMA_ERROR; > > + > > + iova_base = iova = __alloc_iova(mapping, size); > > + if (iova == ARM_DMA_ERROR) > > + return -ENOMEM; > > + > > + for (count = 0, s = sg; count < (size >> PAGE_SHIFT); s = sg_next(s)) > > + { > > + phys_addr_t phys = page_to_phys(sg_page(s)); > > + unsigned int len = PAGE_ALIGN(s->offset + s->length); > > + > > + if (!arch_is_coherent()) > > + __dma_page_cpu_to_dev(sg_page(s), s->offset, s->length, dir); > > + > > + ret = iommu_map(mapping->domain, iova, phys, len, 0); > > + if (ret < 0) > > + goto fail; > > + count += len >> PAGE_SHIFT; > > + iova += len; > > + } > > + *handle = iova_base; > > + > > + return 0; > > +fail: > > + iommu_unmap(mapping->domain, iova_base, count * PAGE_SIZE); > > + __free_iova(mapping, iova_base, size); > > + return ret; > > +} > > Do we need to set dma_address as below? Nope, this one is not correct. Please check the arm_iommu_map_sg() function. It calls __map_sg_chunk() and gives it &dma->dma_address as one of the arguments, so the dma address is correctly stored in the scatter list. Please note that scatterlist is really non-trivial structure and information about physical memory pages/chunks is disjoint from the information about dma address space chunks, although both are stored on the same list. In arm_iommu_map_sg() 's' pointer is used for physical memory chunks and 'dma' pointer is used for dma address space chunks. The number of dma address space chunks (returned by arm_iommu_map_sq) can be lower than the number of physical memory chunks (given as nents argument). In case of IOMMU you usually construct the scatter list in such a way, that you get only one dma address chunk (so in fact you get a buffer mapped contiguously in virtual io address space). > From e8bcc3cdac5375b5d7f5ac5b3f716a11c1008f38 Mon Sep 17 00:00:00 2001 > From: Hiroshi DOYU > Date: Thu, 29 Mar 2012 09:59:04 +0300 > Subject: [PATCH 1/1] ARM: dma-mapping: Fix dma_address in sglist > > s->dma_address wasn't set at mapping. > > Signed-off-by: Hiroshi DOYU > --- > arch/arm/mm/dma-mapping.c | 2 ++ > 1 files changed, 2 insertions(+), 0 deletions(-) > > diff --git a/arch/arm/mm/dma-mapping.c b/arch/arm/mm/dma-mapping.c > index 3347c37..11a9d65 100644 > --- a/arch/arm/mm/dma-mapping.c > +++ b/arch/arm/mm/dma-mapping.c > @@ -1111,6 +1111,8 @@ static int __map_sg_chunk(struct device *dev, struct scatterlist *sg, > ret = iommu_map(mapping->domain, iova, phys, len, 0); > if (ret < 0) > goto fail; > + s->dma_address = iova; > + > count += len >> PAGE_SHIFT; > iova += len; > } The above patch is not needed at all. Best regards -- Marek Szyprowski Samsung Poland R&D Center -- 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: email@kvack.org From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mailout4.w1.samsung.com ([210.118.77.14]:35030 "EHLO mailout4.w1.samsung.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750796Ab2C2IAV (ORCPT ); Thu, 29 Mar 2012 04:00:21 -0400 MIME-version: 1.0 Content-transfer-encoding: 7BIT Content-type: text/plain; charset=us-ascii Date: Thu, 29 Mar 2012 10:00:14 +0200 From: Marek Szyprowski Subject: RE: [PATCHv7 9/9] ARM: dma-mapping: add support for IOMMU mapper In-reply-to: <20120329101927.8ab6b1993475b7e16ae2258f@nvidia.com> Message-ID: <01b301cd0d81$f935d750$eba185f0$%szyprowski@samsung.com> Content-language: pl References: <1330527862-16234-1-git-send-email-m.szyprowski@samsung.com> <1330527862-16234-10-git-send-email-m.szyprowski@samsung.com> <20120329101927.8ab6b1993475b7e16ae2258f@nvidia.com> Sender: linux-arch-owner@vger.kernel.org List-ID: To: 'Hiroshi Doyu' Cc: 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, 'Shariq Hasnain' , 'Arnd Bergmann' , 'Benjamin Herrenschmidt' , 'Krishna Reddy' , 'Kyungmin Park' , Andrzej Pietrasiewicz , 'Russell King - ARM Linux' , 'KyongHo Cho' , 'Chunsang Jeong' Message-ID: <20120329080014.DcjusO46gsom3X2mBs6qdvegq49aMK_ClZff_olHf_c@z> Hello, On Thursday, March 29, 2012 9:19 AM Hiroshi Doyu wrote: > On Wed, 29 Feb 2012 16:04:22 +0100 > Marek Szyprowski wrote: > > > This patch add a complete implementation of DMA-mapping API for > > devices that have IOMMU support. All DMA-mapping calls are supported. > > > > This patch contains some of the code kindly provided by Krishna Reddy > > and Andrzej Pietrasiewicz > > > > Signed-off-by: Marek Szyprowski > > Signed-off-by: Kyungmin Park > > Reviewed-by: Konrad Rzeszutek Wilk > > --- > > arch/arm/Kconfig | 8 + > > arch/arm/include/asm/device.h | 3 + > > arch/arm/include/asm/dma-iommu.h | 34 ++ > > arch/arm/mm/dma-mapping.c | 726 +++++++++++++++++++++++++++++++++++++- > > arch/arm/mm/vmregion.h | 2 +- > > 5 files changed, 758 insertions(+), 15 deletions(-) > > create mode 100644 arch/arm/include/asm/dma-iommu.h > > > > > > > +/* > > + * Map a part of the scatter-gather list into contiguous io address space > > + */ > > +static int __map_sg_chunk(struct device *dev, struct scatterlist *sg, > > + size_t size, dma_addr_t *handle, > > + enum dma_data_direction dir) > > +{ > > + struct dma_iommu_mapping *mapping = dev->archdata.mapping; > > + dma_addr_t iova, iova_base; > > + int ret = 0; > > + unsigned int count; > > + struct scatterlist *s; > > + > > + size = PAGE_ALIGN(size); > > + *handle = ARM_DMA_ERROR; > > + > > + iova_base = iova = __alloc_iova(mapping, size); > > + if (iova == ARM_DMA_ERROR) > > + return -ENOMEM; > > + > > + for (count = 0, s = sg; count < (size >> PAGE_SHIFT); s = sg_next(s)) > > + { > > + phys_addr_t phys = page_to_phys(sg_page(s)); > > + unsigned int len = PAGE_ALIGN(s->offset + s->length); > > + > > + if (!arch_is_coherent()) > > + __dma_page_cpu_to_dev(sg_page(s), s->offset, s->length, dir); > > + > > + ret = iommu_map(mapping->domain, iova, phys, len, 0); > > + if (ret < 0) > > + goto fail; > > + count += len >> PAGE_SHIFT; > > + iova += len; > > + } > > + *handle = iova_base; > > + > > + return 0; > > +fail: > > + iommu_unmap(mapping->domain, iova_base, count * PAGE_SIZE); > > + __free_iova(mapping, iova_base, size); > > + return ret; > > +} > > Do we need to set dma_address as below? Nope, this one is not correct. Please check the arm_iommu_map_sg() function. It calls __map_sg_chunk() and gives it &dma->dma_address as one of the arguments, so the dma address is correctly stored in the scatter list. Please note that scatterlist is really non-trivial structure and information about physical memory pages/chunks is disjoint from the information about dma address space chunks, although both are stored on the same list. In arm_iommu_map_sg() 's' pointer is used for physical memory chunks and 'dma' pointer is used for dma address space chunks. The number of dma address space chunks (returned by arm_iommu_map_sq) can be lower than the number of physical memory chunks (given as nents argument). In case of IOMMU you usually construct the scatter list in such a way, that you get only one dma address chunk (so in fact you get a buffer mapped contiguously in virtual io address space). > From e8bcc3cdac5375b5d7f5ac5b3f716a11c1008f38 Mon Sep 17 00:00:00 2001 > From: Hiroshi DOYU > Date: Thu, 29 Mar 2012 09:59:04 +0300 > Subject: [PATCH 1/1] ARM: dma-mapping: Fix dma_address in sglist > > s->dma_address wasn't set at mapping. > > Signed-off-by: Hiroshi DOYU > --- > arch/arm/mm/dma-mapping.c | 2 ++ > 1 files changed, 2 insertions(+), 0 deletions(-) > > diff --git a/arch/arm/mm/dma-mapping.c b/arch/arm/mm/dma-mapping.c > index 3347c37..11a9d65 100644 > --- a/arch/arm/mm/dma-mapping.c > +++ b/arch/arm/mm/dma-mapping.c > @@ -1111,6 +1111,8 @@ static int __map_sg_chunk(struct device *dev, struct scatterlist *sg, > ret = iommu_map(mapping->domain, iova, phys, len, 0); > if (ret < 0) > goto fail; > + s->dma_address = iova; > + > count += len >> PAGE_SHIFT; > iova += len; > } The above patch is not needed at all. Best regards -- Marek Szyprowski Samsung Poland R&D Center