From patchwork Tue Oct 10 14:50:00 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dan Williams X-Patchwork-Id: 9996439 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id C3F3C601AE for ; Tue, 10 Oct 2017 14:56:30 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B47AE28639 for ; Tue, 10 Oct 2017 14:56:30 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id A948A2863C; Tue, 10 Oct 2017 14:56:30 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 4DD4A28639 for ; Tue, 10 Oct 2017 14:56:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932311AbdJJO41 (ORCPT ); Tue, 10 Oct 2017 10:56:27 -0400 Received: from mga04.intel.com ([192.55.52.120]:9706 "EHLO mga04.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932087AbdJJO4Z (ORCPT ); Tue, 10 Oct 2017 10:56:25 -0400 Received: from orsmga005.jf.intel.com ([10.7.209.41]) by fmsmga104.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 10 Oct 2017 07:56:24 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.42,505,1500966000"; d="scan'208";a="158901095" Received: from dwillia2-desk3.jf.intel.com (HELO dwillia2-desk3.amr.corp.intel.com) ([10.54.39.125]) by orsmga005.jf.intel.com with ESMTP; 10 Oct 2017 07:56:24 -0700 Subject: [PATCH v8 11/14] iommu: up-level sg_num_pages() from amd-iommu From: Dan Williams To: linux-nvdimm@lists.01.org Cc: linux-rdma@vger.kernel.org, linux-api@vger.kernel.org, Joerg Roedel , linux-xfs@vger.kernel.org, linux-mm@kvack.org, iommu@lists.linux-foundation.org, linux-fsdevel@vger.kernel.org Date: Tue, 10 Oct 2017 07:50:00 -0700 Message-ID: <150764700026.16882.6848398427693240452.stgit@dwillia2-desk3.amr.corp.intel.com> In-Reply-To: <150764693502.16882.15848797003793552156.stgit@dwillia2-desk3.amr.corp.intel.com> References: <150764693502.16882.15848797003793552156.stgit@dwillia2-desk3.amr.corp.intel.com> User-Agent: StGit/0.17.1-9-g687f MIME-Version: 1.0 Sender: linux-xfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-xfs@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP iommu_sg_num_pages() is a helper that walks a scattlerlist and counts pages taking segment boundaries and iommu_num_pages() into account. Up-level it for determining the IOVA range that dma_map_ops established at dma_map_sg() time. The intent is to iommu_unmap() the IOVA range in advance of freeing IOVA range. Cc: Joerg Roedel Signed-off-by: Dan Williams --- drivers/iommu/amd_iommu.c | 30 ++---------------------------- drivers/iommu/iommu.c | 27 +++++++++++++++++++++++++++ include/linux/iommu.h | 2 ++ 3 files changed, 31 insertions(+), 28 deletions(-) -- To unsubscribe from this list: send the line "unsubscribe linux-xfs" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html diff --git a/drivers/iommu/amd_iommu.c b/drivers/iommu/amd_iommu.c index c8e1a45af182..4795b0823469 100644 --- a/drivers/iommu/amd_iommu.c +++ b/drivers/iommu/amd_iommu.c @@ -2459,32 +2459,6 @@ static void unmap_page(struct device *dev, dma_addr_t dma_addr, size_t size, __unmap_single(dma_dom, dma_addr, size, dir); } -static int sg_num_pages(struct device *dev, - struct scatterlist *sglist, - int nelems) -{ - unsigned long mask, boundary_size; - struct scatterlist *s; - int i, npages = 0; - - mask = dma_get_seg_boundary(dev); - boundary_size = mask + 1 ? ALIGN(mask + 1, PAGE_SIZE) >> PAGE_SHIFT : - 1UL << (BITS_PER_LONG - PAGE_SHIFT); - - for_each_sg(sglist, s, nelems, i) { - int p, n; - - s->dma_address = npages << PAGE_SHIFT; - p = npages % boundary_size; - n = iommu_num_pages(sg_phys(s), s->length, PAGE_SIZE); - if (p + n > boundary_size) - npages += boundary_size - p; - npages += n; - } - - return npages; -} - /* * The exported map_sg function for dma_ops (handles scatter-gather * lists). @@ -2507,7 +2481,7 @@ static int map_sg(struct device *dev, struct scatterlist *sglist, dma_dom = to_dma_ops_domain(domain); dma_mask = *dev->dma_mask; - npages = sg_num_pages(dev, sglist, nelems); + npages = iommu_sg_num_pages(dev, sglist, nelems); address = dma_ops_alloc_iova(dev, dma_dom, npages, dma_mask); if (address == AMD_IOMMU_MAPPING_ERROR) @@ -2585,7 +2559,7 @@ static void unmap_sg(struct device *dev, struct scatterlist *sglist, startaddr = sg_dma_address(sglist) & PAGE_MASK; dma_dom = to_dma_ops_domain(domain); - npages = sg_num_pages(dev, sglist, nelems); + npages = iommu_sg_num_pages(dev, sglist, nelems); __unmap_single(dma_dom, startaddr, npages << PAGE_SHIFT, dir); } diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c index 3de5c0bcb5cc..cfe6eeea3578 100644 --- a/drivers/iommu/iommu.c +++ b/drivers/iommu/iommu.c @@ -33,6 +33,7 @@ #include #include #include +#include static struct kset *iommu_group_kset; static DEFINE_IDA(iommu_group_ida); @@ -1631,6 +1632,32 @@ size_t iommu_unmap_fast(struct iommu_domain *domain, } EXPORT_SYMBOL_GPL(iommu_unmap_fast); +int iommu_sg_num_pages(struct device *dev, struct scatterlist *sglist, + int nelems) +{ + unsigned long mask, boundary_size; + struct scatterlist *s; + int i, npages = 0; + + mask = dma_get_seg_boundary(dev); + boundary_size = mask + 1 ? ALIGN(mask + 1, PAGE_SIZE) >> PAGE_SHIFT + : 1UL << (BITS_PER_LONG - PAGE_SHIFT); + + for_each_sg(sglist, s, nelems, i) { + int p, n; + + s->dma_address = npages << PAGE_SHIFT; + p = npages % boundary_size; + n = iommu_num_pages(sg_phys(s), s->length, PAGE_SIZE); + if (p + n > boundary_size) + npages += boundary_size - p; + npages += n; + } + + return npages; +} +EXPORT_SYMBOL_GPL(iommu_sg_num_pages); + size_t default_iommu_map_sg(struct iommu_domain *domain, unsigned long iova, struct scatterlist *sg, unsigned int nents, int prot) { diff --git a/include/linux/iommu.h b/include/linux/iommu.h index a7f2ac689d29..5b2d20e1475a 100644 --- a/include/linux/iommu.h +++ b/include/linux/iommu.h @@ -303,6 +303,8 @@ extern size_t iommu_unmap(struct iommu_domain *domain, unsigned long iova, size_t size); extern size_t iommu_unmap_fast(struct iommu_domain *domain, unsigned long iova, size_t size); +extern int iommu_sg_num_pages(struct device *dev, struct scatterlist *sglist, + int nelems); extern size_t default_iommu_map_sg(struct iommu_domain *domain, unsigned long iova, struct scatterlist *sg,unsigned int nents, int prot);