Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp4687271yba; Mon, 20 May 2019 01:50:41 -0700 (PDT) X-Google-Smtp-Source: APXvYqzMlhPLfdlsbP8NW2hcrRXRDyEWEAjHCEe0syWJVDXcG3GvlMSv8SEvrAmLSCH6w4z3s8Cx X-Received: by 2002:aa7:998d:: with SMTP id k13mr66357839pfh.217.1558342241840; Mon, 20 May 2019 01:50:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1558342241; cv=none; d=google.com; s=arc-20160816; b=gE36NwuxkIrMLFTXsGja5CtHT73UMwr7/6HC5guGDUKTO0pSWRDFj3o0Yvi7bYJtUn a2g+jVLuzyOO8jQVXC5dw2BPqFNBu5nHpRnOj3TxoskEQJpf4Wqm+ArU8O7fM4YuybOT QRL6kmfmkIVi64QQKjrGH0xUBdY8hoCCzOw8F2vMGO6Bj6EaGsq2Dk4ocVsFRY69VMJi SPwKHtfCNuWeDF8BFvNvUZib4BrkpQ3p4b/GMw4YeG304Vsi50xzP0ox4ozEjEeCpqMN NuWkDc80AbDB8lcECu1dw0JqS9VYJV4bCRwuuAITl/krqVrV7uvt2UJC+BydMIk5XsAg kp+w== 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=117qtInUSRTjScTlp3+Hy7VbUwgEhdDFPvMFg6m2tcg=; b=wWylgnNEUfD5WQ7smHsmygMrMhtZTX2UDCkULJXa/Gf4uObS61HH2GfATFMfbV8zr/ mk65/uasmUNtKoyfs/eKbdN+MEzZA3Ax3LGxFGPZA7deL5/0NUAayW14s8BNIWYyhUKP u02p/G7ic6gzn9AV1CRu0+Z2ws6aXZmSk1nXBL7ndPtaEx7rf/9QxTuzKLum0ABd7SOH oHSVO5KRWreVIy6eX34/zWOqCFy2q0z7VDmYdxkEE1y28pcTbme2Z6s2awyKks6KrZsO +SJOPIFow8qbLVTs3dlkONlurFDKg6yFgTE6RlcaIudCxF3YnjwXeQ4xZSFL1brf94mG x4Vw== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@infradead.org header.s=bombadil.20170209 header.b=AATotP4W; 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 202si16487729pgh.575.2019.05.20.01.50.26; Mon, 20 May 2019 01:50:41 -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=AATotP4W; 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 S1730945AbfETHbM (ORCPT + 99 others); Mon, 20 May 2019 03:31:12 -0400 Received: from bombadil.infradead.org ([198.137.202.133]:44008 "EHLO bombadil.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730928AbfETHbJ (ORCPT ); Mon, 20 May 2019 03:31:09 -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=117qtInUSRTjScTlp3+Hy7VbUwgEhdDFPvMFg6m2tcg=; b=AATotP4Wpdx1KhD+fcamjdNQN8 D2vz0lUaOJbFTtpNwt6l6B9U3Y5gzFUanEN8NB5ny/THpB0VG3cpSmWZXE/OQ2e6N8zcOCamrcIpS te5/IR1xiLndcR5aqaoYUsl/tzE4ht/ILQB2lidDMOeaOjuS4qOySfcCyYxITgDKSPmrQNltpNOwe NDdZ8ApGA6+fyclJ5RlxVEmAPKhKDsQTAk20CvZcpwm1c5+JOZO8qoRP0RF8XOE/skpotiXp4JRLf II97i1V537caQFhcKOJmAUYxCm3t0kiR16x9ULK5AleT4a3VbjFLK0sJECqStdAaGeNqer/EB7yLe XPj1i7Ag==; Received: from 089144206147.atnat0015.highway.bob.at ([89.144.206.147] helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.90_1 #2 (Red Hat Linux)) id 1hSclG-0004FI-Ab; Mon, 20 May 2019 07:31:06 +0000 From: Christoph Hellwig To: Robin Murphy Cc: Joerg Roedel , Catalin Marinas , Will Deacon , Tom Murphy , iommu@lists.linux-foundation.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org Subject: [PATCH 10/24] iommu/dma: Refactor the page array remapping allocator Date: Mon, 20 May 2019 09:29:34 +0200 Message-Id: <20190520072948.11412-11-hch@lst.de> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190520072948.11412-1-hch@lst.de> References: <20190520072948.11412-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 Move the call to dma_common_pages_remap into __iommu_dma_alloc and rename it to iommu_dma_alloc_remap. This creates a self-contained helper for remapped pages allocation and mapping. Signed-off-by: Christoph Hellwig Reviewed-by: Robin Murphy --- drivers/iommu/dma-iommu.c | 54 +++++++++++++++++++-------------------- 1 file changed, 26 insertions(+), 28 deletions(-) diff --git a/drivers/iommu/dma-iommu.c b/drivers/iommu/dma-iommu.c index 5e81165e6755..0ffb7805de77 100644 --- a/drivers/iommu/dma-iommu.c +++ b/drivers/iommu/dma-iommu.c @@ -564,9 +564,9 @@ static struct page **__iommu_dma_get_pages(void *cpu_addr) } /** - * iommu_dma_free - Free a buffer allocated by __iommu_dma_alloc() + * iommu_dma_free - Free a buffer allocated by iommu_dma_alloc_remap() * @dev: Device which owns this buffer - * @pages: Array of buffer pages as returned by __iommu_dma_alloc() + * @pages: Array of buffer pages as returned by __iommu_dma_alloc_remap() * @size: Size of buffer in bytes * @handle: DMA address of buffer * @@ -582,33 +582,35 @@ static void __iommu_dma_free(struct device *dev, struct page **pages, } /** - * __iommu_dma_alloc - Allocate and map a buffer contiguous in IOVA space + * iommu_dma_alloc_remap - Allocate and map a buffer contiguous in IOVA space * @dev: Device to allocate memory for. Must be a real device * attached to an iommu_dma_domain * @size: Size of buffer in bytes + * @dma_handle: Out argument for allocated DMA handle * @gfp: Allocation flags * @attrs: DMA attributes for this allocation - * @prot: IOMMU mapping flags - * @handle: Out argument for allocated DMA handle * * If @size is less than PAGE_SIZE, then a full CPU page will be allocated, * but an IOMMU which supports smaller pages might not map the whole thing. * - * Return: Array of struct page pointers describing the buffer, - * or NULL on failure. + * Return: Mapped virtual address, or NULL on failure. */ -static struct page **__iommu_dma_alloc(struct device *dev, size_t size, - gfp_t gfp, unsigned long attrs, int prot, dma_addr_t *handle) +static void *iommu_dma_alloc_remap(struct device *dev, size_t size, + dma_addr_t *dma_handle, gfp_t gfp, unsigned long attrs) { struct iommu_domain *domain = iommu_get_dma_domain(dev); struct iommu_dma_cookie *cookie = domain->iova_cookie; struct iova_domain *iovad = &cookie->iovad; + bool coherent = dev_is_dma_coherent(dev); + int ioprot = dma_info_to_prot(DMA_BIDIRECTIONAL, coherent, attrs); + pgprot_t prot = arch_dma_mmap_pgprot(dev, PAGE_KERNEL, attrs); + unsigned int count, min_size, alloc_sizes = domain->pgsize_bitmap; struct page **pages; struct sg_table sgt; dma_addr_t iova; - unsigned int count, min_size, alloc_sizes = domain->pgsize_bitmap; + void *vaddr; - *handle = DMA_MAPPING_ERROR; + *dma_handle = DMA_MAPPING_ERROR; min_size = alloc_sizes & -alloc_sizes; if (min_size < PAGE_SIZE) { @@ -634,7 +636,7 @@ static struct page **__iommu_dma_alloc(struct device *dev, size_t size, if (sg_alloc_table_from_pages(&sgt, pages, count, 0, size, GFP_KERNEL)) goto out_free_iova; - if (!(prot & IOMMU_CACHE)) { + if (!(ioprot & IOMMU_CACHE)) { struct scatterlist *sg; int i; @@ -642,14 +644,21 @@ static struct page **__iommu_dma_alloc(struct device *dev, size_t size, arch_dma_prep_coherent(sg_page(sg), sg->length); } - if (iommu_map_sg(domain, iova, sgt.sgl, sgt.orig_nents, prot) + if (iommu_map_sg(domain, iova, sgt.sgl, sgt.orig_nents, ioprot) < size) goto out_free_sg; - *handle = iova; + vaddr = dma_common_pages_remap(pages, size, VM_USERMAP, prot, + __builtin_return_address(0)); + if (!vaddr) + goto out_unmap; + + *dma_handle = iova; sg_free_table(&sgt); - return pages; + return vaddr; +out_unmap: + __iommu_dma_unmap(dev, iova, size); out_free_sg: sg_free_table(&sgt); out_free_iova: @@ -1008,18 +1017,7 @@ static void *iommu_dma_alloc(struct device *dev, size_t size, size >> PAGE_SHIFT); } } else { - pgprot_t prot = arch_dma_mmap_pgprot(dev, PAGE_KERNEL, attrs); - struct page **pages; - - pages = __iommu_dma_alloc(dev, iosize, gfp, attrs, ioprot, - handle); - if (!pages) - return NULL; - - addr = dma_common_pages_remap(pages, size, VM_USERMAP, prot, - __builtin_return_address(0)); - if (!addr) - __iommu_dma_free(dev, pages, iosize, handle); + addr = iommu_dma_alloc_remap(dev, iosize, handle, gfp, attrs); } return addr; } @@ -1033,7 +1031,7 @@ static void iommu_dma_free(struct device *dev, size_t size, void *cpu_addr, /* * @cpu_addr will be one of 4 things depending on how it was allocated: * - A remapped array of pages for contiguous allocations. - * - A remapped array of pages from __iommu_dma_alloc(), for all + * - A remapped array of pages from iommu_dma_alloc_remap(), for all * non-atomic allocations. * - A non-cacheable alias from the atomic pool, for atomic * allocations by non-coherent devices. -- 2.20.1