Received: by 2002:ac0:bc90:0:0:0:0:0 with SMTP id a16csp5067272img; Wed, 27 Mar 2019 01:09:35 -0700 (PDT) X-Google-Smtp-Source: APXvYqzabyToRUcAv0wGDHX+Fvv1bHeqpihXC47QyRESdDQ5lrFDfq9xlu+Gqrxxos0incO79UvJ X-Received: by 2002:a63:d1f:: with SMTP id c31mr9939210pgl.353.1553674175919; Wed, 27 Mar 2019 01:09:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1553674175; cv=none; d=google.com; s=arc-20160816; b=rlAg9D08gLCOnRdS/jdOOQA9qjFghYuRxlMXXzrcCGMW6q/bpMbBZ/Cg8NBmZEr0Kk QqtN0jJDYLOKP0B2y941TWu6ZNo+v1B7FGDjYH+9iPa/6470qxcaJj/OP2FtODMfyAyc ZvkPtipW6E2kDFaYDNKCSDEYOme7E/Ra51OEGN9fDtG8v9l92B33TkXmziWYqM5jvRja ARx4q7ljlFbgAOCZL/C4arJJSzrW//INaxMpMeRZWD2pk/MSDGiIHEV0lqXAjEVagrCP 7cLpqMCK6A41ynsqfoDfywNaNIBv0WYL93Al/yK9QbsyuINpNgSf4b/izr18w/GJTaCd F0xw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=O42Slp47rdpGVQXhCTqYzFJ3XiOG/rHsbVDLPAlj2TE=; b=hEMGab31+YI+IKcfcftFyG14JTFWIi+jUqn+04bInw9OF7OMry3OfvxOkByADqeiSZ Y5qLVwSg5ze14+DlF9k2nCXavJ5WqetEN4JZhC3yW0b2eg+Yr3CsalP7JJGZtHPzXkjj HIEEfcFUNoF7AY6XaLJ/yZvADoQhi4M9rZCAy3wXGLyGBcDwtvYi+aPtNuO8BqtRoJ9s J7mA9UOxbpQMbYH+VnrD6D4vT0iybmoHybCP9xvh+TaXu9FtXSqGZGlPnlB3rEaVA/y9 qlVzwDssrir1Wl0SUFTgXgC/ydVLQwlNtqdk/RlGbU+v05BZnzcMyjLTMjqQYEHaxe+q 3pVA== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@infradead.org header.s=bombadil.20170209 header.b=GcLYamIy; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 89si271747pld.265.2019.03.27.01.09.20; Wed, 27 Mar 2019 01:09:35 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=fail header.i=@infradead.org header.s=bombadil.20170209 header.b=GcLYamIy; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732661AbfC0IFy (ORCPT + 99 others); Wed, 27 Mar 2019 04:05:54 -0400 Received: from bombadil.infradead.org ([198.137.202.133]:33122 "EHLO bombadil.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732432AbfC0IFx (ORCPT ); Wed, 27 Mar 2019 04:05:53 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20170209; h=Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender :Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From :Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help: List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=O42Slp47rdpGVQXhCTqYzFJ3XiOG/rHsbVDLPAlj2TE=; b=GcLYamIyh1ULXOvSHen3dNyRUJ yXUpNDT94IQSfmrhaYvmrQQBu2XhRgDxmiAnkyN0egzHw6qrAMVKlX78L+EDLvsC9XDCVWuRGziLe yoKsIAlGFR93iWPHynUas4AYZj/3r+VDhKDa39bqpesCedgRPGPX7taslGDDA8MWbNt6gLrP8aMcm ix7RAvbn90xq3TRELjKQXWtfjb6VJ0OBH3Wiy9hiQghxECo25Wf3rwy051pNj3SsD30yIQEOX2pEd j+en0NE04Ot9f8t3+OTn26B1cCaspi5TrZokkCKcHVPGjmlAFgiT+0EdcoubRgWVXKcwylnEEWawo UtzvzcGA==; Received: from 213-225-14-48.nat.highway.a1.net ([213.225.14.48] helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.90_1 #2 (Red Hat Linux)) id 1h93ZE-0006r6-KY; Wed, 27 Mar 2019 08:05:49 +0000 From: Christoph Hellwig To: Robin Murphy Cc: Joerg Roedel , Catalin Marinas , Will Deacon , Tom Lendacky , iommu@lists.linux-foundation.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org Subject: [PATCH 13/21] dma-iommu: factor contiguous allocations into helpers Date: Wed, 27 Mar 2019 09:04:40 +0100 Message-Id: <20190327080448.5500-14-hch@lst.de> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190327080448.5500-1-hch@lst.de> References: <20190327080448.5500-1-hch@lst.de> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SRS-Rewrite: SMTP reverse-path rewritten from by bombadil.infradead.org. See http://www.infradead.org/rpr.html Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This keeps the code together and will simplify using it in different ways. Signed-off-by: Christoph Hellwig --- drivers/iommu/dma-iommu.c | 110 ++++++++++++++++++++------------------ 1 file changed, 59 insertions(+), 51 deletions(-) diff --git a/drivers/iommu/dma-iommu.c b/drivers/iommu/dma-iommu.c index 8ec69176673d..da2e0f4a63b6 100644 --- a/drivers/iommu/dma-iommu.c +++ b/drivers/iommu/dma-iommu.c @@ -458,6 +458,48 @@ static dma_addr_t __iommu_dma_map(struct device *dev, phys_addr_t phys, return iova + iova_off; } +static void iommu_dma_free_contiguous(struct device *dev, size_t size, + struct page *page, dma_addr_t dma_handle) +{ + unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT; + + __iommu_dma_unmap(iommu_get_domain_for_dev(dev), dma_handle, size); + if (!dma_release_from_contiguous(dev, page, count)) + __free_pages(page, get_order(size)); +} + + +static void *iommu_dma_alloc_contiguous(struct device *dev, size_t size, + dma_addr_t *dma_handle, gfp_t gfp, unsigned long attrs) +{ + bool coherent = dev_is_dma_coherent(dev); + int ioprot = dma_info_to_prot(DMA_BIDIRECTIONAL, coherent, attrs); + unsigned int count = PAGE_ALIGN(size) >> PAGE_SHIFT; + unsigned int page_order = get_order(size); + struct page *page = NULL; + + if (gfpflags_allow_blocking(gfp)) + page = dma_alloc_from_contiguous(dev, count, page_order, + gfp & __GFP_NOWARN); + + if (page) + memset(page_address(page), 0, PAGE_ALIGN(size)); + else + page = alloc_pages(gfp, page_order); + if (!page) + return NULL; + + *dma_handle = __iommu_dma_map(dev, page_to_phys(page), size, ioprot, + iommu_get_dma_domain(dev)); + if (*dma_handle == DMA_MAPPING_ERROR) { + if (!dma_release_from_contiguous(dev, page, count)) + __free_pages(page, page_order); + return NULL; + } + + return page_address(page); +} + static void __iommu_dma_free_pages(struct page **pages, int count) { while (count--) @@ -754,19 +796,6 @@ static void iommu_dma_sync_sg_for_device(struct device *dev, arch_sync_dma_for_device(dev, sg_phys(sg), sg->length, dir); } -static dma_addr_t __iommu_dma_map_page(struct device *dev, struct page *page, - unsigned long offset, size_t size, int prot) -{ - return __iommu_dma_map(dev, page_to_phys(page) + offset, size, prot, - iommu_get_dma_domain(dev)); -} - -static void __iommu_dma_unmap_page(struct device *dev, dma_addr_t handle, - size_t size, enum dma_data_direction dir, unsigned long attrs) -{ - __iommu_dma_unmap(iommu_get_dma_domain(dev), handle, size); -} - static dma_addr_t iommu_dma_map_page(struct device *dev, struct page *page, unsigned long offset, size_t size, enum dma_data_direction dir, unsigned long attrs) @@ -991,7 +1020,6 @@ static void *iommu_dma_alloc(struct device *dev, size_t size, dma_addr_t *handle, gfp_t gfp, unsigned long attrs) { bool coherent = dev_is_dma_coherent(dev); - int ioprot = dma_info_to_prot(DMA_BIDIRECTIONAL, coherent, attrs); size_t iosize = size; void *addr; @@ -1004,7 +1032,6 @@ static void *iommu_dma_alloc(struct device *dev, size_t size, gfp |= __GFP_ZERO; if (!gfpflags_allow_blocking(gfp)) { - struct page *page; /* * In atomic context we can't remap anything, so we'll only * get the virtually contiguous buffer we need by way of a @@ -1013,44 +1040,27 @@ static void *iommu_dma_alloc(struct device *dev, size_t size, if (!coherent) return iommu_dma_alloc_pool(dev, iosize, handle, gfp, attrs); - - page = alloc_pages(gfp, get_order(size)); - if (!page) - return NULL; - - addr = page_address(page); - *handle = __iommu_dma_map_page(dev, page, 0, iosize, ioprot); - if (*handle == DMA_MAPPING_ERROR) { - __free_pages(page, get_order(size)); - addr = NULL; - } + return iommu_dma_alloc_contiguous(dev, iosize, handle, gfp, + attrs); } else if (attrs & DMA_ATTR_FORCE_CONTIGUOUS) { pgprot_t prot = arch_dma_mmap_pgprot(dev, PAGE_KERNEL, attrs); struct page *page; - page = dma_alloc_from_contiguous(dev, size >> PAGE_SHIFT, - get_order(size), gfp & __GFP_NOWARN); - if (!page) + addr = iommu_dma_alloc_contiguous(dev, iosize, handle, gfp, + attrs); + if (!addr) return NULL; + page = virt_to_page(addr); - *handle = __iommu_dma_map_page(dev, page, 0, iosize, ioprot); - if (*handle == DMA_MAPPING_ERROR) { - dma_release_from_contiguous(dev, page, - size >> PAGE_SHIFT); + addr = dma_common_contiguous_remap(page, size, VM_USERMAP, prot, + __builtin_return_address(0)); + if (!addr) { + iommu_dma_free_contiguous(dev, iosize, page, *handle); return NULL; } - addr = dma_common_contiguous_remap(page, size, VM_USERMAP, - prot, - __builtin_return_address(0)); - if (addr) { - if (!coherent) - arch_dma_prep_coherent(page, iosize); - memset(addr, 0, size); - } else { - __iommu_dma_unmap_page(dev, *handle, iosize, 0, attrs); - dma_release_from_contiguous(dev, page, - size >> PAGE_SHIFT); - } + + if (!coherent) + arch_dma_prep_coherent(page, iosize); } else { addr = iommu_dma_alloc_remap(dev, iosize, handle, gfp, attrs); } @@ -1077,16 +1087,14 @@ static void iommu_dma_free(struct device *dev, size_t size, void *cpu_addr, if (dma_in_atomic_pool(cpu_addr, size)) { iommu_dma_free_pool(dev, size, cpu_addr, handle); } else if (attrs & DMA_ATTR_FORCE_CONTIGUOUS) { - struct page *page = vmalloc_to_page(cpu_addr); - - __iommu_dma_unmap_page(dev, handle, iosize, 0, attrs); - dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT); + iommu_dma_free_contiguous(dev, iosize, + vmalloc_to_page(cpu_addr), handle); dma_common_free_remap(cpu_addr, size, VM_USERMAP); } else if (is_vmalloc_addr(cpu_addr)){ iommu_dma_free_remap(dev, iosize, cpu_addr, handle); } else { - __iommu_dma_unmap_page(dev, handle, iosize, 0, 0); - __free_pages(virt_to_page(cpu_addr), get_order(size)); + iommu_dma_free_contiguous(dev, iosize, virt_to_page(cpu_addr), + handle); } } -- 2.20.1